[PATCH] D129726: [InstCombine] (ShiftValC >> Y) >s -1 --> Y != 0 with ShiftValC < 0

Chenbing.Zheng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 17 23:31:47 PDT 2022


Chenbing.Zheng updated this revision to Diff 445392.
Chenbing.Zheng retitled this revision from "[InstCombine] add a constraint for x <u minSigned --> x >s -1" to "[InstCombine] (ShiftValC >> Y) >s -1 --> Y != 0 with ShiftValC < 0".
Chenbing.Zheng edited the summary of this revision.
Chenbing.Zheng added a comment.

address comment,
add fold (ShiftValC >> Y) >s -1 --> Y != 0 with ShiftValC < 0
https://alive2.llvm.org/ce/z/TTnvgs


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

https://reviews.llvm.org/D129726

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
@@ -1146,12 +1146,9 @@
   ret i1 %r
 }
 
-; TODO: This should reduce to X != 0.
-
 define i1 @lshr_pow2_ult_smin(i8 %x) {
 ; CHECK-LABEL: @lshr_pow2_ult_smin(
-; CHECK-NEXT:    [[S:%.*]] = lshr i8 -128, [[X:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = icmp sgt i8 [[S]], -1
+; CHECK-NEXT:    [[R:%.*]] = icmp ne i8 [[X:%.*]], 0
 ; CHECK-NEXT:    ret i1 [[R]]
 ;
   %s = lshr i8 128, %x
Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -2235,13 +2235,19 @@
 
   bool IsAShr = Shr->getOpcode() == Instruction::AShr;
   const APInt *ShiftValC;
-  if (match(Shr->getOperand(0), m_APInt(ShiftValC))) {
+  if (match(X, m_APInt(ShiftValC))) {
     if (Cmp.isEquality())
       return foldICmpShrConstConst(Cmp, Shr->getOperand(1), C, *ShiftValC);
 
+    // (ShiftValC >> Y) >s -1 --> Y != 0 with ShiftValC < 0
+    if (!IsAShr && Pred == CmpInst::ICMP_SGT && C.isAllOnes() &&
+        ShiftValC->isNegative())
+      return new ICmpInst(CmpInst::ICMP_NE, Shr->getOperand(1),
+                          ConstantInt::getNullValue(X->getType()));
+
     // If the shifted constant is a power-of-2, test the shift amount directly:
-    // (ShiftValC >> X) >u C --> X <u (LZ(C) - LZ(ShiftValC))
-    // (ShiftValC >> X) <u C --> X >=u (LZ(C-1) - LZ(ShiftValC))
+    // (ShiftValC >> Y) >u C --> X <u (LZ(C) - LZ(ShiftValC))
+    // (ShiftValC >> Y) <u C --> X >=u (LZ(C-1) - LZ(ShiftValC))
     if (!IsAShr && ShiftValC->isPowerOf2() &&
         (Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_ULT)) {
       bool IsUGT = Pred == CmpInst::ICMP_UGT;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129726.445392.patch
Type: text/x-patch
Size: 1955 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220718/342950f0/attachment.bin>


More information about the llvm-commits mailing list