Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -4909,8 +4909,7 @@ return nullptr; } -static Instruction *foldICmpWithTrunc(ICmpInst &ICmp, - InstCombiner::BuilderTy &Builder) { +Instruction *InstCombinerImpl::foldICmpWithTrunc(ICmpInst &ICmp) { ICmpInst::Predicate Pred = ICmp.getPredicate(); Value *Op0 = ICmp.getOperand(0), *Op1 = ICmp.getOperand(1); @@ -4948,6 +4947,21 @@ return new ICmpInst(ICmpInst::ICMP_EQ, And, MaskC); } + if (auto *II = dyn_cast(X)) { + if (II->getIntrinsicID() == Intrinsic::cttz || + II->getIntrinsicID() == Intrinsic::ctlz) { + unsigned MaxRet = SrcBits; + if (match(II->getArgOperand(1), m_One())) + MaxRet--; + + // Make sure the dest bits is enough to save the intrinsic output's range + if (llvm::Log2_32(MaxRet) + 1 <= Op0->getType()->getScalarSizeInBits()) + if (Instruction *I = + foldICmpIntrinsicWithConstant(ICmp, II, C->zext(SrcBits))) + return I; + } + } + return nullptr; } @@ -5105,7 +5119,7 @@ return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1); } - if (Instruction *R = foldICmpWithTrunc(ICmp, Builder)) + if (Instruction *R = foldICmpWithTrunc(ICmp)) return R; return foldICmpWithZextOrSext(ICmp); Index: llvm/lib/Transforms/InstCombine/InstCombineInternal.h =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineInternal.h +++ llvm/lib/Transforms/InstCombine/InstCombineInternal.h @@ -624,6 +624,7 @@ Instruction *foldICmpEqIntrinsicWithConstant(ICmpInst &ICI, IntrinsicInst *II, const APInt &C); Instruction *foldICmpBitCast(ICmpInst &Cmp); + Instruction *foldICmpWithTrunc(ICmpInst &Cmp); // Helpers of visitSelectInst(). Instruction *foldSelectOfBools(SelectInst &SI); Index: llvm/test/Transforms/InstCombine/cmp-intrinsic.ll =================================================================== --- llvm/test/Transforms/InstCombine/cmp-intrinsic.ll +++ llvm/test/Transforms/InstCombine/cmp-intrinsic.ll @@ -4,6 +4,7 @@ declare i16 @llvm.bswap.i16(i16) declare i32 @llvm.bswap.i32(i32) declare <2 x i64> @llvm.bswap.v2i64(<2 x i64>) +declare i32 @llvm.cttz.i32(i32, i1) declare i33 @llvm.cttz.i33(i33, i1) declare i32 @llvm.ctlz.i32(i32, i1) declare i8 @llvm.ctpop.i8(i8) @@ -540,9 +541,8 @@ define i1 @trunc_cttz_ult_other_i33_i15(i33 %x) { ; CHECK-LABEL: @trunc_cttz_ult_other_i33_i15( -; CHECK-NEXT: [[TZ:%.*]] = tail call i33 @llvm.cttz.i33(i33 [[X:%.*]], i1 false), !range [[RNG1]] -; CHECK-NEXT: [[TRUNC:%.*]] = trunc i33 [[TZ]] to i15 -; CHECK-NEXT: [[CMP:%.*]] = icmp ult i15 [[TRUNC]], 7 +; CHECK-NEXT: [[TMP1:%.*]] = and i33 [[X:%.*]], 127 +; CHECK-NEXT: [[CMP:%.*]] = icmp ne i33 [[TMP1]], 0 ; CHECK-NEXT: ret i1 [[CMP]] ; %tz = tail call i33 @llvm.cttz.i33(i33 %x, i1 false) @@ -551,6 +551,44 @@ ret i1 %cmp } +define i1 @trunc_cttz_true_ult_other_i32_i5(i32 %x) { +; CHECK-LABEL: @trunc_cttz_true_ult_other_i32_i5( +; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[X:%.*]], 127 +; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[TMP1]], 0 +; CHECK-NEXT: ret i1 [[CMP]] +; + %tz = tail call i32 @llvm.cttz.i32(i32 %x, i1 true) + %trunc = trunc i32 %tz to i5 + %cmp = icmp ult i5 %trunc, 7 + ret i1 %cmp +} + +define i1 @trunc_cttz_false_ult_other_i32_i6(i32 %x) { +; CHECK-LABEL: @trunc_cttz_false_ult_other_i32_i6( +; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[X:%.*]], 127 +; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[TMP1]], 0 +; CHECK-NEXT: ret i1 [[CMP]] +; + %tz = tail call i32 @llvm.cttz.i32(i32 %x, i1 false) + %trunc = trunc i32 %tz to i6 + %cmp = icmp ult i6 %trunc, 7 + ret i1 %cmp +} + + +define i1 @trunc_cttz_false_ult_other_i32_i5(i32 %x) { +; CHECK-LABEL: @trunc_cttz_false_ult_other_i32_i5( +; CHECK-NEXT: [[TZ:%.*]] = tail call i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 false), !range [[RNG0]] +; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 [[TZ]] to i5 +; CHECK-NEXT: [[CMP:%.*]] = icmp ult i5 [[TRUNC]], 7 +; CHECK-NEXT: ret i1 [[CMP]] +; + %tz = tail call i32 @llvm.cttz.i32(i32 %x, i1 false) + %trunc = trunc i32 %tz to i5 + %cmp = icmp ult i5 %trunc, 7 + ret i1 %cmp +} + define i1 @trunc_ctlz_ugt_zero_i32(i32 %x) { ; CHECK-LABEL: @trunc_ctlz_ugt_zero_i32( ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X:%.*]], -1 @@ -573,11 +611,9 @@ ret i1 %cmp } -define i1 @trunc_ctlz_ugt_other_i32(i32 %x) { -; CHECK-LABEL: @trunc_ctlz_ugt_other_i32( -; CHECK-NEXT: [[LZ:%.*]] = tail call i32 @llvm.ctlz.i32(i32 [[X:%.*]], i1 false), !range [[RNG0]] -; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 [[LZ]] to i15 -; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i15 [[TRUNC]], 4 +define i1 @trunc_ctlz_ugt_other_i32_i15(i32 %x) { +; CHECK-LABEL: @trunc_ctlz_ugt_other_i32_i15( +; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X:%.*]], 134217728 ; CHECK-NEXT: ret i1 [[CMP]] ; %lz = tail call i32 @llvm.ctlz.i32(i32 %x, i1 false) @@ -586,6 +622,42 @@ ret i1 %cmp } +define i1 @trunc_ctlz_true_ugt_other_i32_i5(i32 %x) { +; CHECK-LABEL: @trunc_ctlz_true_ugt_other_i32_i5( +; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X:%.*]], 134217728 +; CHECK-NEXT: ret i1 [[CMP]] +; + %lz = tail call i32 @llvm.ctlz.i32(i32 %x, i1 true) + %trunc = trunc i32 %lz to i5 + %cmp = icmp ugt i5 %trunc, 4 + ret i1 %cmp +} + +define i1 @trunc_ctlz_false_ugt_other_i32_i5(i32 %x) { +; CHECK-LABEL: @trunc_ctlz_false_ugt_other_i32_i5( +; CHECK-NEXT: [[LZ:%.*]] = tail call i32 @llvm.ctlz.i32(i32 [[X:%.*]], i1 false), !range [[RNG0]] +; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 [[LZ]] to i5 +; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i5 [[TRUNC]], 4 +; CHECK-NEXT: ret i1 [[CMP]] +; + %lz = tail call i32 @llvm.ctlz.i32(i32 %x, i1 false) + %trunc = trunc i32 %lz to i5 + %cmp = icmp ugt i5 %trunc, 4 + ret i1 %cmp +} + +define i1 @trunc_ctlz_false_ugt_other_i32_i6(i32 %x) { +; CHECK-LABEL: @trunc_ctlz_false_ugt_other_i32_i6( +; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X:%.*]], 134217728 +; CHECK-NEXT: ret i1 [[CMP]] +; + %lz = tail call i32 @llvm.ctlz.i32(i32 %x, i1 false) + %trunc = trunc i32 %lz to i6 + %cmp = icmp ugt i6 %trunc, 4 + ret i1 %cmp +} + + define i1 @trunc_ctpop_eq_zero_i11(i11 %x) { ; CHECK-LABEL: @trunc_ctpop_eq_zero_i11( ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i11 [[X:%.*]], 0