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,13 +4,16 @@ 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 i33 @llvm.ctlz.i33(i33, i1) declare i8 @llvm.ctpop.i8(i8) declare i11 @llvm.ctpop.i11(i11) declare <2 x i32> @llvm.cttz.v2i32(<2 x i32>, i1) declare <2 x i32> @llvm.ctlz.v2i32(<2 x i32>, i1) declare <2 x i32> @llvm.ctpop.v2i32(<2 x i32>) +declare void @use6(i6) define i1 @bswap_eq_i16(i16 %x) { ; CHECK-LABEL: @bswap_eq_i16( @@ -538,16 +541,84 @@ ret i1 %cmp } -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 +define i1 @trunc_cttz_ult_other_i33_i6(i33 %x) { +; CHECK-LABEL: @trunc_cttz_ult_other_i33_i6( +; 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) - %trunc = trunc i33 %tz to i15 - %cmp = icmp ult i15 %trunc, 7 + %tz = tail call i33 @llvm.cttz.i33(i33 %x, i1 true) + %trunc = trunc i33 %tz to i6 + %cmp = icmp ult i6 %trunc, 7 + ret i1 %cmp +} + +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]] +; CHECK-NEXT: [[TRUNC:%.*]] = trunc i33 [[TZ]] to i5 +; CHECK-NEXT: [[CMP:%.*]] = icmp ult i5 [[TRUNC]], 7 +; CHECK-NEXT: ret i1 [[CMP]] +; + %tz = tail call i33 @llvm.cttz.i33(i33 %x, i1 true) + %trunc = trunc i33 %tz to i5 + %cmp = icmp ult i5 %trunc, 7 + 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 +} + +; negative case, is_zero_poison == false && log2(src bitwidth) == dest bitwidth + +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_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 +} + +; negative case, trunc is not one use + +define i1 @trunc_cttz_false_ult_other_i32_i6_extra_use(i32 %x) { +; CHECK-LABEL: @trunc_cttz_false_ult_other_i32_i6_extra_use( +; 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: call void @use6(i6 [[TRUNC]]) +; CHECK-NEXT: [[CMP:%.*]] = icmp ult i6 [[TRUNC]], 7 +; CHECK-NEXT: ret i1 [[CMP]] +; + %tz = tail call i32 @llvm.cttz.i32(i32 %x, i1 false) + %trunc = trunc i32 %tz to i6 + call void @use6(i6 %trunc) + %cmp = icmp ult i6 %trunc, 7 ret i1 %cmp } @@ -573,16 +644,81 @@ ret i1 %cmp } -define i1 @trunc_ctlz_ugt_other_i32(i32 %x) { -; CHECK-LABEL: @trunc_ctlz_ugt_other_i32( +define i1 @trunc_ctlz_ugt_other_i33_i6(i33 %x) { +; CHECK-LABEL: @trunc_ctlz_ugt_other_i33_i6( +; CHECK-NEXT: [[CMP:%.*]] = icmp ult i33 [[X:%.*]], 268435456 +; CHECK-NEXT: ret i1 [[CMP]] +; + %lz = tail call i33 @llvm.ctlz.i33(i33 %x, i1 true) + %trunc = trunc i33 %lz to i6 + %cmp = icmp ugt i6 %trunc, 4 + ret i1 %cmp +} + +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]] +; CHECK-NEXT: [[TRUNC:%.*]] = trunc i33 [[LZ]] to i5 +; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i5 [[TRUNC]], 4 +; CHECK-NEXT: ret i1 [[CMP]] +; + %lz = tail call i33 @llvm.ctlz.i33(i33 %x, i1 true) + %trunc = trunc i33 %lz to i5 + %cmp = icmp ugt i5 %trunc, 4 + 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 +} + +; negative case, is_zero_poison == false && log2(src bitwidth) == dest bitwidth + +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 i15 -; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i15 [[TRUNC]], 4 +; 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 i15 - %cmp = icmp ugt i15 %trunc, 4 + %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 +} + +; negative case, trunc is not one use + +define i1 @trunc_ctlz_false_ugt_other_i32_i6_extra_use(i32 %x) { +; CHECK-LABEL: @trunc_ctlz_false_ugt_other_i32_i6_extra_use( +; 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: call void @use6(i6 [[TRUNC]]) +; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i6 [[TRUNC]], 4 +; CHECK-NEXT: ret i1 [[CMP]] +; + %lz = tail call i32 @llvm.ctlz.i32(i32 %x, i1 false) + %trunc = trunc i32 %lz to i6 + call void @use6(i6 %trunc) + %cmp = icmp ugt i6 %trunc, 4 ret i1 %cmp }