[llvm] 6f149a1 - [InstCombine] Look through truncate to fold icmp with intrinsics
via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 9 18:33:15 PST 2023
Author: chenglin.bi
Date: 2023-02-10T10:33:07+08:00
New Revision: 6f149a17d4b1a254dd41a3b910021481c564a691
URL: https://github.com/llvm/llvm-project/commit/6f149a17d4b1a254dd41a3b910021481c564a691
DIFF: https://github.com/llvm/llvm-project/commit/6f149a17d4b1a254dd41a3b910021481c564a691.diff
LOG: [InstCombine] Look through truncate to fold icmp with intrinsics
The output of intrinsic functions like ctpop, cttz, ctlz have limited range from 0 to bitwidth. So if the truncate destination type can hold the source bitwidth size, we can just ignore the truncate and use the truncate src to do combination.
Alive2 proofs:
https://alive2.llvm.org/ce/z/9D_-qP
Reviewed By: spatel
Differential Revision: https://reviews.llvm.org/D143368
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/lib/Transforms/InstCombine/InstCombineInternal.h
llvm/test/Transforms/InstCombine/cmp-intrinsic.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 6bd3c826c9e4e..4429b31cf8c60 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -4909,8 +4909,7 @@ Instruction *InstCombinerImpl::foldICmpEquality(ICmpInst &I) {
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,25 @@ static Instruction *foldICmpWithTrunc(ICmpInst &ICmp,
return new ICmpInst(ICmpInst::ICMP_EQ, And, MaskC);
}
+ if (auto *II = dyn_cast<IntrinsicInst>(X)) {
+ if (II->getIntrinsicID() == Intrinsic::cttz ||
+ II->getIntrinsicID() == Intrinsic::ctlz) {
+ unsigned MaxRet = SrcBits;
+ // If the "is_zero_poison" argument is set, then we know at least
+ // one bit is set in the input, so the result is always at least one
+ // less than the full bitwidth of that input.
+ if (match(II->getArgOperand(1), m_One()))
+ MaxRet--;
+
+ // Make sure the destination is wide enough to hold the largest output of
+ // the intrinsic.
+ if (llvm::Log2_32(MaxRet) + 1 <= Op0->getType()->getScalarSizeInBits())
+ if (Instruction *I =
+ foldICmpIntrinsicWithConstant(ICmp, II, C->zext(SrcBits)))
+ return I;
+ }
+ }
+
return nullptr;
}
@@ -5105,7 +5123,7 @@ Instruction *InstCombinerImpl::foldICmpWithCastOp(ICmpInst &ICmp) {
return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
}
- if (Instruction *R = foldICmpWithTrunc(ICmp, Builder))
+ if (Instruction *R = foldICmpWithTrunc(ICmp))
return R;
return foldICmpWithZextOrSext(ICmp);
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
index a7aa250a7c684..5a566e37927bf 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
+++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
@@ -624,6 +624,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
Instruction *foldICmpEqIntrinsicWithConstant(ICmpInst &ICI, IntrinsicInst *II,
const APInt &C);
Instruction *foldICmpBitCast(ICmpInst &Cmp);
+ Instruction *foldICmpWithTrunc(ICmpInst &Cmp);
// Helpers of visitSelectInst().
Instruction *foldSelectOfBools(SelectInst &SI);
diff --git a/llvm/test/Transforms/InstCombine/cmp-intrinsic.ll b/llvm/test/Transforms/InstCombine/cmp-intrinsic.ll
index 1910da3d543d6..accbfdc5d3ef4 100644
--- a/llvm/test/Transforms/InstCombine/cmp-intrinsic.ll
+++ b/llvm/test/Transforms/InstCombine/cmp-intrinsic.ll
@@ -543,9 +543,8 @@ define i1 @trunc_cttz_ugt_other_i33_i15(i33 %x) {
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 @@ define i1 @trunc_cttz_ult_other_i33_i6(i33 %x) {
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_ult_other_i33_i5(i33 %x) {
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_i5(i32 %x) {
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_one_i32(i32 %x) {
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 @@ define i1 @trunc_ctlz_ugt_other_i33_i6(i33 %x) {
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_ugt_other_i33_i5(i33 %x) {
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_i5(i32 %x) {
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 @@ define i1 @trunc_ctpop_eq_bitwidth_i8(i8 %x) {
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]]
More information about the llvm-commits
mailing list