OSDN Git Service

Add instcombine patterns for the following transformations:
authorChad Rosier <mcrosier@apple.com>
Thu, 26 Apr 2012 23:29:14 +0000 (23:29 +0000)
committerChad Rosier <mcrosier@apple.com>
Thu, 26 Apr 2012 23:29:14 +0000 (23:29 +0000)
 (x & y) | (x ^ y) -> x | y
 (x & y) + (x ^ y) -> x | y

Patch by Manman Ren.
rdar://10770603

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@155674 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/InstCombine/InstCombineAddSub.cpp
lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
test/Transforms/InstCombine/and-xor-or.ll [new file with mode: 0644]

index 05e702f..6a39fc3 100644 (file)
@@ -329,6 +329,20 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
     }
   }
 
+  // Check for (x & y) + (x ^ y)
+  {
+    Value *A = 0, *B = 0;
+    if (match(RHS, m_Xor(m_Value(A), m_Value(B))) &&
+        (match(LHS, m_And(m_Specific(A), m_Specific(B))) ||
+         match(LHS, m_And(m_Specific(B), m_Specific(A)))))
+      return BinaryOperator::CreateOr(A, B);
+
+    if (match(LHS, m_Xor(m_Value(A), m_Value(B))) &&
+        (match(RHS, m_And(m_Specific(A), m_Specific(B))) ||
+         match(RHS, m_And(m_Specific(B), m_Specific(A)))))
+      return BinaryOperator::CreateOr(A, B);
+  }
+
   return Changed ? &I : 0;
 }
 
index 0dbe11d..665b3c6 100644 (file)
@@ -1932,10 +1932,15 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) {
 
   // A | ( A ^ B) -> A |  B
   // A | (~A ^ B) -> A | ~B
+  // (A & B) | (A ^ B)
   if (match(Op1, m_Xor(m_Value(A), m_Value(B)))) {
     if (Op0 == A || Op0 == B)
       return BinaryOperator::CreateOr(A, B);
 
+    if (match(Op0, m_And(m_Specific(A), m_Specific(B))) ||
+        match(Op0, m_And(m_Specific(B), m_Specific(A))))
+      return BinaryOperator::CreateOr(A, B);
+
     if (Op1->hasOneUse() && match(A, m_Not(m_Specific(Op0)))) {
       Value *Not = Builder->CreateNot(B, B->getName()+".not");
       return BinaryOperator::CreateOr(Not, Op0);
diff --git a/test/Transforms/InstCombine/and-xor-or.ll b/test/Transforms/InstCombine/and-xor-or.ll
new file mode 100644 (file)
index 0000000..7ff810b
--- /dev/null
@@ -0,0 +1,24 @@
+; RUN: opt < %s -instcombine -S | FileCheck %s
+
+; rdar://10770603
+; (x & y) | (x ^ y) -> x | y 
+define i64 @or(i64 %x, i64 %y) nounwind uwtable readnone ssp {
+  %1 = and i64 %y, %x
+  %2 = xor i64 %y, %x
+  %3 = add i64 %1, %2
+  ret i64 %3
+; CHECK: @or
+; CHECK: or i64
+; CHECK-NEXT: ret
+}
+
+; (x & y) + (x ^ y) -> x | y 
+define i64 @or2(i64 %x, i64 %y) nounwind uwtable readnone ssp {
+  %1 = and i64 %y, %x
+  %2 = xor i64 %y, %x
+  %3 = or i64 %1, %2
+  ret i64 %3
+; CHECK: @or2
+; CHECK: or i64
+; CHECK-NEXT: ret
+}