[llvm] 00f0381 - [InstCombine] Refactor foldSelectICmpAndOr to use `decomposeBitTestICmp` instead of bespoke logic

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


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

URL: https://github.com/llvm/llvm-project/commit/00f03814611e47e1a459b4d287b0e847a34ab9d4
DIFF: https://github.com/llvm/llvm-project/commit/00f03814611e47e1a459b4d287b0e847a34ab9d4.diff

LOG: [InstCombine] Refactor foldSelectICmpAndOr to use `decomposeBitTestICmp` instead of bespoke logic

This is essentially NFC as the cases `decomposeBitTestICmp` covers
that weren't already covered explicitly, will be canonicalized into
the cases explicitly covered. As well the unsigned cases don't apply
as the `Mask` is not a power of 2.

That being said, using a well established helper is less bug prone and
if some canonicalization changes, will prevent regressions here.

Reviewed By: nikic

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

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index d32435895576d5..d5be03dd425773 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -714,8 +714,8 @@ static Value *foldSelectICmpAndOr(const ICmpInst *IC, Value *TrueVal,
   Value *CmpRHS = IC->getOperand(1);
 
   unsigned C1Log;
-  bool IsEqualZero;
   bool NeedAnd = false;
+  CmpInst::Predicate Pred = IC->getPredicate();
   if (IC->isEquality()) {
     if (!match(CmpRHS, m_Zero()))
       return nullptr;
@@ -725,17 +725,13 @@ static Value *foldSelectICmpAndOr(const ICmpInst *IC, Value *TrueVal,
       return nullptr;
 
     C1Log = C1->logBase2();
-    IsEqualZero = IC->getPredicate() == ICmpInst::ICMP_EQ;
   } else {
-    // 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()))
-      IsEqualZero = false;
-    else
+    APInt C1;
+    if (!decomposeBitTestICmp(CmpLHS, CmpRHS, Pred, CmpLHS, C1) ||
+        !C1.isPowerOf2())
       return nullptr;
 
-    C1Log = CmpLHS->getType()->getScalarSizeInBits() - 1;
+    C1Log = C1.logBase2();
     NeedAnd = true;
   }
 
@@ -745,11 +741,11 @@ static Value *foldSelectICmpAndOr(const ICmpInst *IC, Value *TrueVal,
   if (match(FalseVal, m_Or(m_Specific(TrueVal), m_Power2(C2)))) {
     Y = TrueVal;
     Or = FalseVal;
-    NeedXor = !IsEqualZero;
+    NeedXor = Pred == ICmpInst::ICMP_NE;
   } else if (match(TrueVal, m_Or(m_Specific(FalseVal), m_Power2(C2)))) {
     Y = FalseVal;
     Or = TrueVal;
-    NeedXor = IsEqualZero;
+    NeedXor = Pred == ICmpInst::ICMP_EQ;
   } else {
     return nullptr;
   }


        


More information about the llvm-commits mailing list