[PATCH] D148413: [InstCombine] Remove requirement on `trunc` in `slt/sgt` case in `foldSelectICmpAndOr`

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 16 20:43:14 PDT 2023


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG82292d1ae5ac: [InstCombine] Remove requirement on `trunc` in `slt/sgt` case in… (authored by goldstein.w.n).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D148413

Files:
  llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
  llvm/test/Transforms/InstCombine/select-with-bitwise-ops.ll


Index: llvm/test/Transforms/InstCombine/select-with-bitwise-ops.ll
===================================================================
--- llvm/test/Transforms/InstCombine/select-with-bitwise-ops.ll
+++ llvm/test/Transforms/InstCombine/select-with-bitwise-ops.ll
@@ -760,9 +760,9 @@
 
 define i8 @test70(i8 %x, i8 %y) {
 ; CHECK-LABEL: @test70(
-; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 0
-; CHECK-NEXT:    [[OR:%.*]] = or i8 [[Y:%.*]], 2
-; CHECK-NEXT:    [[SELECT:%.*]] = select i1 [[CMP]], i8 [[OR]], i8 [[Y]]
+; CHECK-NEXT:    [[TMP1:%.*]] = lshr i8 [[X:%.*]], 6
+; CHECK-NEXT:    [[TMP2:%.*]] = and i8 [[TMP1]], 2
+; CHECK-NEXT:    [[SELECT:%.*]] = or i8 [[TMP2]], [[Y:%.*]]
 ; CHECK-NEXT:    ret i8 [[SELECT]]
 ;
   %cmp = icmp slt i8 %x, 0
Index: llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -713,7 +713,6 @@
   Value *CmpLHS = IC->getOperand(0);
   Value *CmpRHS = IC->getOperand(1);
 
-  Value *V;
   unsigned C1Log;
   bool IsEqualZero;
   bool NeedAnd = false;
@@ -725,12 +724,10 @@
     if (!match(CmpLHS, m_And(m_Value(), m_Power2(C1))))
       return nullptr;
 
-    V = CmpLHS;
     C1Log = C1->logBase2();
     IsEqualZero = IC->getPredicate() == ICmpInst::ICMP_EQ;
   } else {
-    // We also need to recognize (icmp slt (trunc (X)), 0) and
-    // (icmp sgt (trunc (X)), -1).
+    // We also need to recognize (icmp slt X, 0) and (icmp sgt X, -1).
     if (IC->getPredicate() == ICmpInst::ICMP_SGT && match(CmpRHS, m_AllOnes()))
       IsEqualZero = true;
     if (IC->getPredicate() == ICmpInst::ICMP_SLT && match(CmpRHS, m_Zero()))
@@ -738,14 +735,11 @@
     else
       return nullptr;
 
-    if (!match(CmpLHS, m_OneUse(m_Trunc(m_Value(V)))))
-      return nullptr;
-
     C1Log = CmpLHS->getType()->getScalarSizeInBits() - 1;
     NeedAnd = true;
   }
 
-  Value *Or, *Y;
+  Value *Or, *Y, *V = CmpLHS;
   const APInt *C2;
   bool NeedXor;
   if (match(FalseVal, m_Or(m_Specific(TrueVal), m_Power2(C2)))) {
@@ -767,7 +761,7 @@
                        V->getType()->getScalarSizeInBits();
 
   // Make sure we don't create more instructions than we save.
-  if ((NeedShift + NeedXor + NeedZExtTrunc) >
+  if ((NeedShift + NeedXor + NeedZExtTrunc + NeedAnd) >
       (IC->hasOneUse() + Or->hasOneUse()))
     return nullptr;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148413.550977.patch
Type: text/x-patch
Size: 2457 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230817/2acecf77/attachment.bin>


More information about the llvm-commits mailing list