Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -4877,8 +4877,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); @@ -4916,6 +4915,18 @@ return new ICmpInst(ICmpInst::ICMP_EQ, And, MaskC); } + if (auto *II = dyn_cast(X)) { + if (II->getIntrinsicID() == Intrinsic::ctpop || + II->getIntrinsicID() == Intrinsic::cttz || + II->getIntrinsicID() == Intrinsic::ctlz) { + // Make sure the dest bits is enough to save the intrinsic output's range + if (llvm::Log2_32_Ceil(SrcBits) < Op0->getType()->getScalarSizeInBits()) + if (Instruction *I = + foldICmpIntrinsicWithConstant(ICmp, II, C->zext(SrcBits))) + return I; + } + } + return nullptr; } @@ -5073,7 +5084,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 @@ -116,7 +116,7 @@ define i1 @ctlz_eq_other_i32_multiuse(i32 %x, ptr %p) { ; CHECK-LABEL: @ctlz_eq_other_i32_multiuse( -; CHECK-NEXT: [[LZ:%.*]] = tail call i32 @llvm.ctlz.i32(i32 [[X:%.*]], i1 false), [[RNG0:!range !.*]] +; CHECK-NEXT: [[LZ:%.*]] = tail call i32 @llvm.ctlz.i32(i32 [[X:%.*]], i1 false), !range [[RNG0:![0-9]+]] ; CHECK-NEXT: store i32 [[LZ]], ptr [[P:%.*]], align 4 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[LZ]], 24 ; CHECK-NEXT: ret i1 [[CMP]] @@ -169,7 +169,7 @@ define i1 @ctlz_ugt_other_multiuse_i32(i32 %x, ptr %p) { ; CHECK-LABEL: @ctlz_ugt_other_multiuse_i32( -; CHECK-NEXT: [[LZ:%.*]] = tail call i32 @llvm.ctlz.i32(i32 [[X:%.*]], i1 false), [[RNG0]] +; CHECK-NEXT: [[LZ:%.*]] = tail call i32 @llvm.ctlz.i32(i32 [[X:%.*]], i1 false), !range [[RNG0]] ; CHECK-NEXT: store i32 [[LZ]], ptr [[P:%.*]], align 4 ; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X]], 32768 ; CHECK-NEXT: ret i1 [[CMP]] @@ -329,7 +329,7 @@ define i1 @cttz_eq_other_i33_multiuse(i33 %x, ptr %p) { ; CHECK-LABEL: @cttz_eq_other_i33_multiuse( -; CHECK-NEXT: [[TZ:%.*]] = tail call i33 @llvm.cttz.i33(i33 [[X:%.*]], i1 false), [[RNG1:!range !.*]] +; CHECK-NEXT: [[TZ:%.*]] = tail call i33 @llvm.cttz.i33(i33 [[X:%.*]], i1 false), !range [[RNG1:![0-9]+]] ; CHECK-NEXT: store i33 [[TZ]], ptr [[P:%.*]], align 4 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i33 [[TZ]], 4 ; CHECK-NEXT: ret i1 [[CMP]] @@ -375,7 +375,7 @@ define i1 @cttz_ugt_other_multiuse_i33(i33 %x, ptr %p) { ; CHECK-LABEL: @cttz_ugt_other_multiuse_i33( -; CHECK-NEXT: [[TZ:%.*]] = tail call i33 @llvm.cttz.i33(i33 [[X:%.*]], i1 false), [[RNG1]] +; CHECK-NEXT: [[TZ:%.*]] = tail call i33 @llvm.cttz.i33(i33 [[X:%.*]], i1 false), !range [[RNG1]] ; CHECK-NEXT: store i33 [[TZ]], ptr [[P:%.*]], align 4 ; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i33 [[TZ]], 16 ; CHECK-NEXT: ret i1 [[CMP]] @@ -493,7 +493,7 @@ define i1 @ctpop_ugt_bitwidth_minus_one_i8(i8 %x, ptr %p) { ; CHECK-LABEL: @ctpop_ugt_bitwidth_minus_one_i8( -; CHECK-NEXT: [[POP:%.*]] = tail call i8 @llvm.ctpop.i8(i8 [[X:%.*]]), [[RNG2:!range !.*]] +; CHECK-NEXT: [[POP:%.*]] = tail call i8 @llvm.ctpop.i8(i8 [[X:%.*]]), !range [[RNG2:![0-9]+]] ; CHECK-NEXT: store i8 [[POP]], ptr [[P:%.*]], align 1 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[X]], -1 ; CHECK-NEXT: ret i1 [[CMP]] @@ -513,3 +513,107 @@ %cmp = icmp ult <2 x i32> %pop, ret <2 x i1> %cmp } + +define i1 @trunc_cttz_eq_other_i33_i15(i33 %x) { +; CHECK-LABEL: @trunc_cttz_eq_other_i33_i15( +; CHECK-NEXT: [[TMP1:%.*]] = and i33 [[X:%.*]], 31 +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i33 [[TMP1]], 16 +; CHECK-NEXT: ret i1 [[CMP]] +; + %tz = tail call i33 @llvm.cttz.i33(i33 %x, i1 false) + %trunc = trunc i33 %tz to i15 + %cmp = icmp eq i15 %trunc, 4 + ret i1 %cmp +} + +define i1 @trunc_cttz_ugt_other_i33_i15(i33 %x) { +; CHECK-LABEL: @trunc_cttz_ugt_other_i33_i15( +; CHECK-NEXT: [[TMP1:%.*]] = and i33 [[X:%.*]], 15 +; CHECK-NEXT: [[CMP:%.*]] = icmp eq 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 ugt i15 %trunc, 3 + ret i1 %cmp +} + +define i1 @trunc_cttz_ult_other_i33_i15(i33 %x) { +; CHECK-LABEL: @trunc_cttz_ult_other_i33_i15( +; 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 + 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 +; 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, 0 + ret i1 %cmp +} + +define i1 @trunc_ctlz_ugt_one_i32(i32 %x) { +; CHECK-LABEL: @trunc_ctlz_ugt_one_i32( +; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X:%.*]], 1073741824 +; 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, 1 + ret i1 %cmp +} + +define i1 @trunc_ctlz_ugt_other_i32(i32 %x) { +; CHECK-LABEL: @trunc_ctlz_ugt_other_i32( +; 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 i15 + %cmp = icmp ugt i15 %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 +; CHECK-NEXT: ret i1 [[CMP]] +; + %pop = tail call i11 @llvm.ctpop.i11(i11 %x) + %trunc = trunc i11 %pop to i5 + %cmp = icmp eq i5 %trunc, 0 + ret i1 %cmp +} + +define i1 @trunc_ctpop_eq_bitwidth_i8(i8 %x) { +; CHECK-LABEL: @trunc_ctpop_eq_bitwidth_i8( +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[X:%.*]], -1 +; CHECK-NEXT: ret i1 [[CMP]] +; + %pop = tail call i8 @llvm.ctpop.i8(i8 %x) + %trunc = trunc i8 %pop to i5 + %cmp = icmp eq i5 %trunc, 8 + ret i1 %cmp +} + +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]] +; CHECK-NEXT: [[TRUNC:%.*]] = trunc i33 [[TZ]] to i4 +; CHECK-NEXT: [[CMP:%.*]] = icmp ult i4 [[TRUNC]], 7 +; CHECK-NEXT: ret i1 [[CMP]] +; + %tz = tail call i33 @llvm.cttz.i33(i33 %x, i1 false) + %trunc = trunc i33 %tz to i4 + %cmp = icmp ult i4 %trunc, 7 + ret i1 %cmp +}