[llvm] [InstCombine] Simplify select if it combinated `and/or/xor` (PR #73362)

via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 26 09:32:53 PST 2023


================
@@ -4572,12 +4614,37 @@ static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
                                          unsigned MaxRecurse) {
   ICmpInst::Predicate Pred;
   Value *CmpLHS, *CmpRHS;
+  const APInt *C;
   if (!match(CondVal, m_ICmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS))))
     return nullptr;
 
   if (Value *V = simplifyCmpSelOfMaxMin(CmpLHS, CmpRHS, Pred, TrueVal, FalseVal))
     return V;
 
+  if (ICmpInst::isEquality(Pred) && match(CmpRHS, m_APInt(C)) &&
+      (C->isZero() | C->isOne())) {
+    bool which;
+    Value *X, *Y;
+
+    if (Pred == ICmpInst::ICMP_EQ)
+      which = C->isZero() ? false : true;
+    else
+      which = C->isZero() ? true : false;
+
+    if (match(CmpLHS, m_And(m_Value(X), m_Value(Y)))) {
+      if (Value *V = simplifySelectBitTestSpec(TrueVal, FalseVal, X, Y, which))
+        return V;
+    }
+    if (match(CmpLHS, m_Or(m_Value(X), m_Value(Y)))) {
+      if (Value *V = simplifySelectBitTestSpec(TrueVal, FalseVal, X, Y, which))
+        return V;
+    }
+    if (match(CmpLHS, m_Xor(m_Value(X), m_Value(Y)))) {
+      if (Value *V = simplifySelectBitTestSpec(TrueVal, FalseVal, X, Y, which))
+        return V;
+    }
+  }
+
   // Canonicalize ne to eq predicate.
   if (Pred == ICmpInst::ICMP_NE) {
     Pred = ICmpInst::ICMP_EQ;
----------------
goldsteinn wrote:

You need tests. For the tests please make them a seperate commit. This allows us to see the changes this commit produces.

I.e 
Commit 1: Tests for simplifying `select (BitWidth), (BitWise), (BitWise)`
Commit 2: This commit.

Note, use `update_test_checks.py` for generating the tests.

https://github.com/llvm/llvm-project/pull/73362


More information about the llvm-commits mailing list