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

via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 28 04:34:31 PST 2023


================
@@ -4425,6 +4425,60 @@ Value *llvm::simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
                                   RecursionLimit);
 }
 
+/// Try simplifying the selection command when condition, trueValue, and
+/// falseValue are combinations of special bitwise operators.
+static Value *simplifySelectBitTestSpec(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_c_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)))) {
+      return FalseVal;
+    } else if (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_c_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)))) {
+      return FalseVal;
+    } else if (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
+  // (X ^ Y) == 0 could be transformed to X == Y
+  // (X ^ Y) != 0 could be transformed to X != Y
+  if (match(CmpLHS, m_CombineOr(m_c_Xor(m_Value(X), m_Value(Y)), m_Value(X)))) {
+    if (Y && match(CmpRHS, m_Zero()) || match(CmpRHS, m_Value(Y))) {
----------------
ParkHanbum wrote:

fixed it! thanks for advise!

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


More information about the llvm-commits mailing list