[llvm] 511ee87 - [InstCombine] reduce code duplication with commutative matcher; NFC

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 1 05:48:19 PDT 2021


Author: Sanjay Patel
Date: 2021-11-01T08:26:41-04:00
New Revision: 511ee8759f714a8bd17f8669ecc0022e07eb2362

URL: https://github.com/llvm/llvm-project/commit/511ee8759f714a8bd17f8669ecc0022e07eb2362
DIFF: https://github.com/llvm/llvm-project/commit/511ee8759f714a8bd17f8669ecc0022e07eb2362.diff

LOG: [InstCombine] reduce code duplication with commutative matcher; NFC

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 56b6f7d156744..6745b1b394cfe 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -2728,28 +2728,24 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
     // (A & C1)|(B & C2)
     ConstantInt *C1, *C2;
     if (match(C, m_ConstantInt(C1)) && match(D, m_ConstantInt(C2))) {
-      Value *V1 = nullptr, *V2 = nullptr;
+      Value *N;
       if ((C1->getValue() & C2->getValue()).isZero()) {
-        // ((V | N) & C1) | (V & C2) --> (V|N) & (C1|C2)
-        // iff (C1&C2) == 0 and (N&~C1) == 0
-        if (match(A, m_Or(m_Value(V1), m_Value(V2))) &&
-            ((V1 == B &&
-              MaskedValueIsZero(V2, ~C1->getValue(), 0, &I)) || // (V|N)
-             (V2 == B &&
-              MaskedValueIsZero(V1, ~C1->getValue(), 0, &I))))  // (N|V)
-          return BinaryOperator::CreateAnd(A,
-                                Builder.getInt(C1->getValue()|C2->getValue()));
-        // Or commutes, try both ways.
-        if (match(B, m_Or(m_Value(V1), m_Value(V2))) &&
-            ((V1 == A &&
-              MaskedValueIsZero(V2, ~C2->getValue(), 0, &I)) || // (V|N)
-             (V2 == A &&
-              MaskedValueIsZero(V1, ~C2->getValue(), 0, &I))))  // (N|V)
-          return BinaryOperator::CreateAnd(B,
-                                 Builder.getInt(C1->getValue()|C2->getValue()));
+        // ((B | N) & C1) | (B & C2) --> (B | N) & (C1 | C2)
+        // iff (C1 & C2) == 0 and (N & ~C1) == 0
+        if (match(A, m_c_Or(m_Specific(B), m_Value(N))) &&
+            MaskedValueIsZero(N, ~C1->getValue(), 0, &I))
+          return BinaryOperator::CreateAnd(
+              A, Builder.getInt(C1->getValue() | C2->getValue()));
+        // (A & C1) | ((A | N) & C2) --> (A | N) & (C1 | C2)
+        // iff (C1 & C2) == 0 and (N & ~C1) == 0
+        if (match(B, m_c_Or(m_Specific(A), m_Value(N))) &&
+            MaskedValueIsZero(N, ~C2->getValue(), 0, &I))
+          return BinaryOperator::CreateAnd(
+              B, Builder.getInt(C1->getValue() | C2->getValue()));
 
         // ((V|C3)&C1) | ((V|C4)&C2) --> (V|C3|C4)&(C1|C2)
         // iff (C1&C2) == 0 and (C3&~C1) == 0 and (C4&~C2) == 0.
+        Value *V1 = nullptr, *V2 = nullptr;
         ConstantInt *C3 = nullptr, *C4 = nullptr;
         if (match(A, m_Or(m_Value(V1), m_ConstantInt(C3))) &&
             (C3->getValue() & ~C1->getValue()).isZero() &&


        


More information about the llvm-commits mailing list