[PATCH] D143368: [InstCombine] Look through truncate to fold icmp with intrinsics

chenglin.bi via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 6 01:31:16 PST 2023


bcl5980 updated this revision to Diff 495031.
bcl5980 added a comment.

rebase with precommit tests.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D143368/new/

https://reviews.llvm.org/D143368

Files:
  llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
  llvm/lib/Transforms/InstCombine/InstCombineInternal.h
  llvm/test/Transforms/InstCombine/cmp-intrinsic.ll


Index: llvm/test/Transforms/InstCombine/cmp-intrinsic.ll
===================================================================
--- llvm/test/Transforms/InstCombine/cmp-intrinsic.ll
+++ llvm/test/Transforms/InstCombine/cmp-intrinsic.ll
@@ -540,9 +540,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)
@@ -575,9 +574,7 @@
 
 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
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i32 [[X:%.*]], 134217728
 ; CHECK-NEXT:    ret i1 [[CMP]]
 ;
   %lz = tail call i32 @llvm.ctlz.i32(i32 %x, i1 false)
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/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<IntrinsicInst>(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);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143368.495031.patch
Type: text/x-patch
Size: 3349 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230206/b31c647c/attachment.bin>


More information about the llvm-commits mailing list