[llvm-commits] [llvm] r60312 - in /llvm/trunk: lib/Target/README.txt lib/Transforms/Scalar/InstructionCombining.cpp test/Transforms/InstCombine/and-or.ll

Bill Wendling isanbard at gmail.com
Sun Nov 30 17:07:11 PST 2008


Author: void
Date: Sun Nov 30 19:07:11 2008
New Revision: 60312

URL: http://llvm.org/viewvc/llvm-project?rev=60312&view=rev
Log:
Implement ((A|B)&1)|(B&-2) -> (A&1) | B transformation. This also takes care of
permutations of this pattern.

Added:
    llvm/trunk/test/Transforms/InstCombine/and-or.ll
Modified:
    llvm/trunk/lib/Target/README.txt
    llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp

Modified: llvm/trunk/lib/Target/README.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/README.txt?rev=60312&r1=60311&r2=60312&view=diff

==============================================================================
--- llvm/trunk/lib/Target/README.txt (original)
+++ llvm/trunk/lib/Target/README.txt Sun Nov 30 19:07:11 2008
@@ -1147,12 +1147,6 @@
 
 //===---------------------------------------------------------------------===//
 
-int a(int a, int b) {return ((a|b)&1) | (b&-2);}
-Should be combined to "(a&1)|b".  Currently not optimized with "clang
--emit-llvm-bc | opt -std-compile-opts".
-
-//===---------------------------------------------------------------------===//
-
 int a(unsigned b) {return ((b << 31) | (b << 30)) >> 31;}
 Should be combined to  "((b >> 1) | b) & 1".  Currently not optimized
 with "clang -emit-llvm-bc | opt -std-compile-opts".

Modified: llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp?rev=60312&r1=60311&r2=60312&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Sun Nov 30 19:07:11 2008
@@ -4649,6 +4649,73 @@
       }
   }
 
+  // ((A|B)&1)|(B&-2) -> (A&1) | B
+  if (match(Op0, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C))) ||
+      match(Op0, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))))) {
+    if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
+      if (CI->getValue() == 1) {
+        Value *V1 = 0, *C2 = 0;
+        if (match(Op1, m_And(m_Value(V1), m_Value(C2)))) {
+          ConstantInt *CI2 = dyn_cast<ConstantInt>(C2);
+
+          if (!CI2) {
+            std::swap(V1, C2);
+            CI2 = dyn_cast<ConstantInt>(C2);
+          }
+
+          if (CI2) {
+            APInt NegTwo = -APInt(CI2->getValue().getBitWidth(), 2, true);
+            if (CI2->getValue().eq(NegTwo)) {
+              if (V1 == B) {
+                Instruction *NewOp =
+                  InsertNewInstBefore(BinaryOperator::CreateAnd(A, CI), I);
+                return BinaryOperator::CreateOr(NewOp, B);
+              }
+              if (V1 == A) {
+                Instruction *NewOp =
+                  InsertNewInstBefore(BinaryOperator::CreateAnd(B, CI), I);
+                return BinaryOperator::CreateOr(NewOp, A);
+              }
+            }
+          }
+        }
+      }
+    }
+  }
+  // (B&-2)|((A|B)&1) -> (A&1) | B
+  if (match(Op1, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C))) ||
+      match(Op1, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))))) {
+    if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
+      if (CI->getValue() == 1) {
+        Value *V1 = 0, *C2 = 0;
+        if (match(Op0, m_And(m_Value(V1), m_Value(C2)))) {
+          ConstantInt *CI2 = dyn_cast<ConstantInt>(C2);
+
+          if (!CI2) {
+            std::swap(V1, C2);
+            CI2 = dyn_cast<ConstantInt>(C2);
+          }
+
+          if (CI2) {
+            APInt NegTwo = -APInt(CI2->getValue().getBitWidth(), 2, true);
+            if (CI2->getValue().eq(NegTwo)) {
+              if (V1 == B) {
+                Instruction *NewOp =
+                  InsertNewInstBefore(BinaryOperator::CreateAnd(A, CI), I);
+                return BinaryOperator::CreateOr(NewOp, B);
+              }
+              if (V1 == A) {
+                Instruction *NewOp =
+                  InsertNewInstBefore(BinaryOperator::CreateAnd(B, CI), I);
+                return BinaryOperator::CreateOr(NewOp, A);
+              }
+            }
+          }
+        }
+      }
+    }
+  }
+
   if (match(Op0, m_Not(m_Value(A)))) {   // ~A | Op1
     if (A == Op1)   // ~A | A == -1
       return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));

Added: llvm/trunk/test/Transforms/InstCombine/and-or.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/and-or.ll?rev=60312&view=auto

==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/and-or.ll (added)
+++ llvm/trunk/test/Transforms/InstCombine/and-or.ll Sun Nov 30 19:07:11 2008
@@ -0,0 +1,39 @@
+; RUN: llvm-as < %s | opt -instcombine | llvm-dis | grep {and i32 %a, 1} | count 4
+; RUN: llvm-as < %s | opt -instcombine | llvm-dis | grep {or i32 %0, %b} | count 4
+
+
+define i32 @func1(i32 %a, i32 %b) nounwind readnone {
+entry:
+	%0 = or i32 %b, %a		; <i32> [#uses=1]
+	%1 = and i32 %0, 1		; <i32> [#uses=1]
+	%2 = and i32 %b, -2		; <i32> [#uses=1]
+	%3 = or i32 %1, %2		; <i32> [#uses=1]
+	ret i32 %3
+}
+
+define i32 @func2(i32 %a, i32 %b) nounwind readnone {
+entry:
+	%0 = or i32 %a, %b		; <i32> [#uses=1]
+	%1 = and i32 1, %0		; <i32> [#uses=1]
+	%2 = and i32 -2, %b		; <i32> [#uses=1]
+	%3 = or i32 %1, %2		; <i32> [#uses=1]
+	ret i32 %3
+}
+
+define i32 @func3(i32 %a, i32 %b) nounwind readnone {
+entry:
+	%0 = or i32 %b, %a		; <i32> [#uses=1]
+	%1 = and i32 %0, 1		; <i32> [#uses=1]
+	%2 = and i32 %b, -2		; <i32> [#uses=1]
+	%3 = or i32 %2, %1		; <i32> [#uses=1]
+	ret i32 %3
+}
+
+define i32 @func4(i32 %a, i32 %b) nounwind readnone {
+entry:
+	%0 = or i32 %a, %b		; <i32> [#uses=1]
+	%1 = and i32 1, %0		; <i32> [#uses=1]
+	%2 = and i32 -2, %b		; <i32> [#uses=1]
+	%3 = or i32 %2, %1		; <i32> [#uses=1]
+	ret i32 %3
+}





More information about the llvm-commits mailing list