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 @@ -543,9 +543,8 @@ define i1 @trunc_cttz_ult_other_i33_i6(i33 %x) { ; CHECK-LABEL: @trunc_cttz_ult_other_i33_i6( -; CHECK-NEXT: [[TZ:%.*]] = tail call i33 @llvm.cttz.i33(i33 [[X:%.*]], i1 true), !range [[RNG1]] -; CHECK-NEXT: [[TRUNC:%.*]] = trunc i33 [[TZ]] to i6 -; CHECK-NEXT: [[CMP:%.*]] = icmp ult i6 [[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 true) @@ -554,6 +553,8 @@ ret i1 %cmp } +; negative case: log2(33 - is_zero_poison ? 1 : 0) + 1 > 5 + define i1 @trunc_cttz_ult_other_i33_i5(i33 %x) { ; CHECK-LABEL: @trunc_cttz_ult_other_i33_i5( ; CHECK-NEXT: [[TZ:%.*]] = tail call i33 @llvm.cttz.i33(i33 [[X:%.*]], i1 true), !range [[RNG1]] @@ -569,9 +570,8 @@ define i1 @trunc_cttz_true_ult_other_i32_i5(i32 %x) { ; CHECK-LABEL: @trunc_cttz_true_ult_other_i32_i5( -; CHECK-NEXT: [[TZ:%.*]] = tail call i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 true), !range [[RNG0]] -; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 [[TZ]] to i5 -; CHECK-NEXT: [[CMP:%.*]] = icmp ult i5 [[TRUNC]], 7 +; 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) @@ -597,9 +597,8 @@ define i1 @trunc_cttz_false_ult_other_i32_i6(i32 %x) { ; CHECK-LABEL: @trunc_cttz_false_ult_other_i32_i6( -; CHECK-NEXT: [[TZ:%.*]] = tail call i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 false), !range [[RNG0]] -; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 [[TZ]] to i6 -; CHECK-NEXT: [[CMP:%.*]] = icmp ult i6 [[TRUNC]], 7 +; 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) @@ -649,9 +648,7 @@ define i1 @trunc_ctlz_ugt_other_i33_i6(i33 %x) { ; CHECK-LABEL: @trunc_ctlz_ugt_other_i33_i6( -; CHECK-NEXT: [[LZ:%.*]] = tail call i33 @llvm.ctlz.i33(i33 [[X:%.*]], i1 true), !range [[RNG1]] -; CHECK-NEXT: [[TRUNC:%.*]] = trunc i33 [[LZ]] to i6 -; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i6 [[TRUNC]], 4 +; CHECK-NEXT: [[CMP:%.*]] = icmp ult i33 [[X:%.*]], 268435456 ; CHECK-NEXT: ret i1 [[CMP]] ; %lz = tail call i33 @llvm.ctlz.i33(i33 %x, i1 true) @@ -660,6 +657,8 @@ ret i1 %cmp } +; negative case: log2(33 - is_zero_poison ? 1 : 0) + 1 > 5 + define i1 @trunc_ctlz_ugt_other_i33_i5(i33 %x) { ; CHECK-LABEL: @trunc_ctlz_ugt_other_i33_i5( ; CHECK-NEXT: [[LZ:%.*]] = tail call i33 @llvm.ctlz.i33(i33 [[X:%.*]], i1 true), !range [[RNG1]] @@ -675,9 +674,7 @@ define i1 @trunc_ctlz_true_ugt_other_i32_i5(i32 %x) { ; CHECK-LABEL: @trunc_ctlz_true_ugt_other_i32_i5( -; CHECK-NEXT: [[LZ:%.*]] = tail call i32 @llvm.ctlz.i32(i32 [[X:%.*]], i1 true), !range [[RNG0]] -; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 [[LZ]] to i5 -; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i5 [[TRUNC]], 4 +; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X:%.*]], 134217728 ; CHECK-NEXT: ret i1 [[CMP]] ; %lz = tail call i32 @llvm.ctlz.i32(i32 %x, i1 true) @@ -703,9 +700,7 @@ define i1 @trunc_ctlz_false_ugt_other_i32_i6(i32 %x) { ; CHECK-LABEL: @trunc_ctlz_false_ugt_other_i32_i6( -; CHECK-NEXT: [[LZ:%.*]] = tail call i32 @llvm.ctlz.i32(i32 [[X:%.*]], i1 false), !range [[RNG0]] -; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 [[LZ]] to i6 -; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i6 [[TRUNC]], 4 +; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X:%.*]], 134217728 ; CHECK-NEXT: ret i1 [[CMP]] ; %lz = tail call i32 @llvm.ctlz.i32(i32 %x, i1 false) @@ -753,6 +748,8 @@ ret i1 %cmp } +; negative case: log2(33) + 1 > 4 + define i1 @trunc_negative_destbits_not_enough(i33 %x) { ; CHECK-LABEL: @trunc_negative_destbits_not_enough( ; CHECK-NEXT: [[TZ:%.*]] = tail call i33 @llvm.cttz.i33(i33 [[X:%.*]], i1 false), !range [[RNG1]]