[PATCH] D127188: [InstCombine] improve fold for icmp-ugt-ashr

Chenbing.Zheng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 7 01:03:28 PDT 2022


Chenbing.Zheng created this revision.
Chenbing.Zheng added reviewers: nadav, spatel, craig.topper, RKSimon, benshi001.
Chenbing.Zheng added a project: LLVM.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
Chenbing.Zheng requested review of this revision.
Herald added subscribers: llvm-commits, jacquesguan.

Existing conditions for
fold icmp ugt (ashr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 1
missed some boundary. It cause this fold don,t work for some cases. and the
reason is due to signed number overflow.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D127188

Files:
  llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
  llvm/test/Transforms/InstCombine/icmp-shr.ll


Index: llvm/test/Transforms/InstCombine/icmp-shr.ll
===================================================================
--- llvm/test/Transforms/InstCombine/icmp-shr.ll
+++ llvm/test/Transforms/InstCombine/icmp-shr.ll
@@ -594,13 +594,9 @@
   ret i1 %r
 }
 
-; negative test
-; TODO: This is a sign-bit test, but we don't recognize the pattern.
-
 define i1 @ashr_ugt_3(i4 %x) {
 ; CHECK-LABEL: @ashr_ugt_3(
-; CHECK-NEXT:    [[S:%.*]] = ashr i4 [[X:%.*]], 1
-; CHECK-NEXT:    [[R:%.*]] = icmp ugt i4 [[S]], 3
+; CHECK-NEXT:    [[R:%.*]] = icmp slt i4 [[X:%.*]], 0
 ; CHECK-NEXT:    ret i1 [[R]]
 ;
   %s = ashr i4 %x, 1
Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -2255,8 +2255,12 @@
     }
     if (Pred == CmpInst::ICMP_UGT) {
       // icmp ugt (ashr X, ShAmtC), C --> icmp ugt X, ((C + 1) << ShAmtC) - 1
+      // Note: 'ShiftedC + 1' as an signed number may overflow, so than 'ashr'
+      // can not get expect value. But it still can fold, 'lshr' works for it.
       APInt ShiftedC = (C + 1).shl(ShAmtVal) - 1;
-      if ((ShiftedC + 1).ashr(ShAmtVal) == (C + 1))
+      if ((ShiftedC + 1).ashr(ShAmtVal) == (C + 1) ||
+          (ShiftedC == ShiftedC.getMaxValue(ShiftedC.getBitWidth() - 1) &&
+           (ShiftedC + 1).lshr(ShAmtVal) == (C + 1)))
         return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
     }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127188.434715.patch
Type: text/x-patch
Size: 1551 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220607/3d0da33f/attachment.bin>


More information about the llvm-commits mailing list