[PATCH] [Instcombine/InstSimplify] Move optimization from instcombine to instsimplify

Rahul Jain rahul1.jain at samsung.com
Wed Jun 11 03:33:59 PDT 2014

```Hi rafael, majnemer, bkramer,

Hi all,

This patch moves the following optimization from visitOr to SimplifyOrInst.
(A & C1)|(B & C2)
If we have: ((V + N) & C1) | (V & C2)
.. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0.

This follows the statement that: InstComb optimization which do not create new instructions should
be moved to InstructionSimplify.

Suyog and myself have planned to come up with more such refactoring patches as and when we
encounter possible cases in InstComb which should be moved to InstSimplify.

Any suggestions/help/pointers on the same will be very much appreciated.

Thanks,
Rahul

http://reviews.llvm.org/D4100

Files:
lib/Analysis/InstructionSimplify.cpp
lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Index: lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -1996,29 +1996,6 @@
C1 = dyn_cast<ConstantInt>(C);
C2 = dyn_cast<ConstantInt>(D);
if (C1 && C2) {  // (A & C1)|(B & C2)
-      // If we have: ((V + N) & C1) | (V & C2)
-      // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
-      // replace with V+N.
-      if (C1->getValue() == ~C2->getValue()) {
-        if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+
-          // Add commutes, try both ways.
-          if (V1 == B && MaskedValueIsZero(V2, C2->getValue()))
-            return ReplaceInstUsesWith(I, A);
-          if (V2 == B && MaskedValueIsZero(V1, C2->getValue()))
-            return ReplaceInstUsesWith(I, A);
-        }
-        // Or commutes, try both ways.
-        if ((C1->getValue() & (C1->getValue()+1)) == 0 &&
-          // Add commutes, try both ways.
-          if (V1 == A && MaskedValueIsZero(V2, C1->getValue()))
-            return ReplaceInstUsesWith(I, B);
-          if (V2 == A && MaskedValueIsZero(V1, C1->getValue()))
-            return ReplaceInstUsesWith(I, B);
-        }
-      }
-
if ((C1->getValue() & C2->getValue()) == 0) {
// ((V | N) & C1) | (V & C2) --> (V|N) & (C1|C2)
// iff (C1&C2) == 0 and (N&~C1) == 0
Index: lib/Analysis/InstructionSimplify.cpp
===================================================================
--- lib/Analysis/InstructionSimplify.cpp
+++ lib/Analysis/InstructionSimplify.cpp
@@ -1625,6 +1625,40 @@
MaxRecurse))
return V;

+  // (A & C)|(B & D)
+  Value *C = nullptr, *D = nullptr;
+  ConstantInt *C1 = nullptr, *C2 = nullptr;
+  if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
+      match(Op1, m_And(m_Value(B), m_Value(D)))) {
+    Value *V1 = nullptr, *V2 = nullptr;
+    C1 = dyn_cast<ConstantInt>(C);
+    C2 = dyn_cast<ConstantInt>(D);
+    if (C1 && C2) { // (A & C1)|(B & C2)
+      // If we have: ((V + N) & C1) | (V & C2)
+      // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
+      // replace with V+N.
+      if (C1->getValue() == ~C2->getValue()) {
+        if ((C2->getValue() & (C2->getValue() + 1)) == 0 && // C2 == 0+1+
+          // Add commutes, try both ways.
+          if (V1 == B && MaskedValueIsZero(V2, C2->getValue()))
+            return A;
+          if (V2 == B && MaskedValueIsZero(V1, C2->getValue()))
+            return A;
+        }
+        // Or commutes, try both ways.
+        if ((C1->getValue() & (C1->getValue() + 1)) == 0 &&
+          // Add commutes, try both ways.
+          if (V1 == A && MaskedValueIsZero(V2, C1->getValue()))
+            return B;
+          if (V2 == A && MaskedValueIsZero(V1, C1->getValue()))
+            return B;
+        }
+      }
+    }
+  }
+
// If the operation is with the result of a phi instruction, check whether
// operating on all incoming values of the phi always yields the same value.
if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D4100.10319.patch
Type: text/x-patch
Size: 3386 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140611/4b47f61b/attachment.bin>
```