[llvm] 82292d1 - [InstCombine] Remove requirement on `trunc` in `slt/sgt` case in `foldSelectICmpAndOr`

Noah Goldstein via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 16 20:41:52 PDT 2023


Author: Noah Goldstein
Date: 2023-08-16T22:43:05-05:00
New Revision: 82292d1ae5ac00e0257b008343d095c5037bd017

URL: https://github.com/llvm/llvm-project/commit/82292d1ae5ac00e0257b008343d095c5037bd017
DIFF: https://github.com/llvm/llvm-project/commit/82292d1ae5ac00e0257b008343d095c5037bd017.diff

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

AFAICT, the trunc is not needed for correctness/performance and just
blocks what should be handlable cases.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D148413

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 5df6574c75d14c..d32435895576d5 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -713,7 +713,6 @@ static Value *foldSelectICmpAndOr(const ICmpInst *IC, Value *TrueVal,
   Value *CmpLHS = IC->getOperand(0);
   Value *CmpRHS = IC->getOperand(1);
 
-  Value *V;
   unsigned C1Log;
   bool IsEqualZero;
   bool NeedAnd = false;
@@ -725,12 +724,10 @@ static Value *foldSelectICmpAndOr(const ICmpInst *IC, Value *TrueVal,
     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 @@ static Value *foldSelectICmpAndOr(const ICmpInst *IC, Value *TrueVal,
     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 @@ static Value *foldSelectICmpAndOr(const ICmpInst *IC, Value *TrueVal,
                        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;
 

diff  --git a/llvm/test/Transforms/InstCombine/select-with-bitwise-ops.ll b/llvm/test/Transforms/InstCombine/select-with-bitwise-ops.ll
index f1f89085cb32a6..b83ac3b92637a4 100644
--- a/llvm/test/Transforms/InstCombine/select-with-bitwise-ops.ll
+++ b/llvm/test/Transforms/InstCombine/select-with-bitwise-ops.ll
@@ -760,9 +760,9 @@ define i32 @test69_and(i32 %x, i32 %y) {
 
 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


        


More information about the llvm-commits mailing list