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

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 10 00:41:43 PST 2024


================
@@ -4585,6 +4585,57 @@ static Value *simplifySelectWithFakeICmpEq(Value *CmpLHS, Value *CmpRHS,
                                Pred == ICmpInst::ICMP_EQ);
 }
 
+/// Try to simplify the Select instruction consisting of add/or/xor.
+static Value *simplifySpecificBitOp(Value *CmpLHS, Value *CmpRHS,
+                                    Value *TrueVal, Value *FalseVal) {
+  Value *X, *Y;
+  // (X & Y) == 0 ? X | Y : X ^ Y   --> X ^ Y
+  // (X & Y) == 0 ? X ^ Y : X | Y   --> X | Y
+  // (X & Y) != 0 ? X | Y : X ^ Y   --> X | Y
+  // (X & Y) != 0 ? X ^ Y : X | Y   --> X ^ Y
+  if (match(CmpLHS, m_And(m_Value(X), m_Value(Y))) && match(CmpRHS, m_Zero())) {
+    if ((match(TrueVal, m_c_Or(m_Specific(X), m_Specific(Y))) &&
+         match(FalseVal, m_c_Xor(m_Specific(X), m_Specific(Y)))) ||
+        (match(FalseVal, m_c_Or(m_Specific(X), m_Specific(Y))) &&
+         match(TrueVal, m_c_Xor(m_Specific(X), m_Specific(Y)))))
+      return FalseVal;
+  }
+  // (X | Y) == 0 ? X & Y : X ^ Y   --> X ^ Y
+  // (X | Y) == 0 ? X ^ Y : X & Y   --> X & Y
+  // (X | Y) != 0 ? X & Y : X ^ Y   --> X & Y
+  // (X | Y) != 0 ? X ^ Y : X & Y   --> X ^ Y
+  if (match(CmpLHS, m_Or(m_Value(X), m_Value(Y))) && match(CmpRHS, m_Zero())) {
+    if ((match(TrueVal, m_c_And(m_Specific(X), m_Specific(Y))) &&
+         match(FalseVal, m_c_Xor(m_Specific(X), m_Specific(Y)))) ||
+        (match(FalseVal, m_c_And(m_Specific(X), m_Specific(Y))) &&
+         match(TrueVal, m_c_Xor(m_Specific(X), m_Specific(Y)))))
+      return FalseVal;
+  }
+  // (X ^ Y) == 0 ? X | Y : X & Y   --> X & Y
+  // (X ^ Y) == 0 ? X & Y : X | Y   --> X | Y
+  // (X ^ Y) != 0 ? X | Y : X & Y   --> X | Y
+  // (X ^ Y) != 0 ? X & Y : X | Y   --> X & Y
+  if (match(CmpLHS, m_Xor(m_Value(X), m_Value(Y))) && match(CmpRHS, m_Zero())) {
+    if ((match(TrueVal, m_c_And(m_Specific(X), m_Specific(Y))) &&
+         match(FalseVal, m_c_Or(m_Specific(X), m_Specific(Y)))) ||
+        (match(FalseVal, m_c_And(m_Specific(X), m_Specific(Y))) &&
+         match(TrueVal, m_c_Or(m_Specific(X), m_Specific(Y)))))
+      return FalseVal;
+  }
+
+  // (X ^ Y) == 0 could be transformed to X == Y
+  // (X ^ Y) != 0 could be transformed to X != Y
+  if (match(CmpLHS, m_Value(X)) && match(CmpRHS, m_Value(Y))) {
+    if ((match(TrueVal, m_c_And(m_Specific(X), m_Specific(Y))) &&
+         match(FalseVal, m_c_Or(m_Specific(X), m_Specific(Y)))) ||
+        (match(FalseVal, m_c_And(m_Specific(X), m_Specific(Y))) &&
+         match(TrueVal, m_c_Or(m_Specific(X), m_Specific(Y)))))
+      return FalseVal;
+  }
----------------
dtcxzyw wrote:

Did you forget to fix it?


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


More information about the llvm-commits mailing list