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

via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 27 08:41:20 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:

Rebase so that the test commit is before the implementation commit.
That way we can see the test changes the implementation causes.

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


More information about the llvm-commits mailing list