# [llvm] r213677 - Added InstCombine transform for pattern "(A & B) ^ (A ^ B) -> (A | B)"

Suyog Sarda suyog.sarda at samsung.com
Tue Jul 22 11:30:54 PDT 2014

```Author: suyog
Date: Tue Jul 22 13:30:54 2014
New Revision: 213677

URL: http://llvm.org/viewvc/llvm-project?rev=213677&view=rev
Log:
Added InstCombine transform for pattern "(A & B) ^ (A ^ B) -> (A | B)"

Patch idea by Ankit Jain !

Differential Revision: http://reviews.llvm.org/D4618

Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
llvm/trunk/test/Transforms/InstCombine/xor2.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp?rev=213677&r1=213676&r2=213677&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp Tue Jul 22 13:30:54 2014
@@ -2454,6 +2454,14 @@ Instruction *InstCombiner::visitXor(Bina
if ((A == C && B == D) || (A == D && B == C))
return BinaryOperator::CreateXor(A, B);
}
+    // (A & B) ^ (A ^ B) -> (A | B)
+    if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
+        match(Op1I, m_Xor(m_Specific(A), m_Specific(B))))
+      return BinaryOperator::CreateOr(A, B);
+    // (A ^ B) ^ (A & B) -> (A | B)
+    if (match(Op0I, m_Xor(m_Value(A), m_Value(B))) &&
+        match(Op1I, m_And(m_Specific(A), m_Specific(B))))
+      return BinaryOperator::CreateOr(A, B);
}

// (A | B)^(~A) -> (A | ~B)

Modified: llvm/trunk/test/Transforms/InstCombine/xor2.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/xor2.ll?rev=213677&r1=213676&r2=213677&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/xor2.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/xor2.ll Tue Jul 22 13:30:54 2014
@@ -105,3 +105,23 @@ define i32 @test8(i32 %a, i32 %b) #0 {
; CHECK-NEXT: %1 = xor i32 %b, -1
; CHECK-NEXT: %xor = or i32 %a, %1
}
+
+; (A & B) ^ (A ^ B) -> (A | B)
+define i32 @test9(i32 %b, i32 %c) {
+ %and = and i32 %b, %c
+ %xor = xor i32 %b, %c
+ %xor2 = xor i32 %and, %xor
+ ret i32 %xor2
+; CHECK-LABEL: @test9(
+; CHECK-NEXT: %xor2 = or i32 %b, %c
+}
+
+; (A ^ B) ^ (A & B) -> (A | B)
+define i32 @test10(i32 %b, i32 %c) {
+ %xor = xor i32 %b, %c
+ %and = and i32 %b, %c
+ %xor2 = xor i32 %xor, %and
+ ret i32 %xor2
+; CHECK-LABEL: @test10(
+; CHECK-NEXT: %xor2 = or i32 %b, %c
+}

```