[PATCH] Instcombine ( ( ~A ^ B ) ^ ( A & ~B ) ) to A | ~B

Karthik Bhat kv.bhat at samsung.com
Thu Aug 14 08:47:38 PDT 2014


Hi David,
Thanks for the review and comments. Yes reassociate does reduce the number of patterns to match. Updated the patch accordingly.
Also updated the test case variable names which was missed out in my previous commit. Please let me know your i/p's on the same.

Thanks once again for your time and support.
Regards
Karthik Bhat

http://reviews.llvm.org/D4878

Files:
  lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
  test/Transforms/InstCombine/or-xor.ll

Index: lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -2542,6 +2542,14 @@
     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) ^ (~B & A) -> (A | ~B)
+    if (match(Op0I, m_Xor(m_Not(m_Value(A)), m_Value(B))) &&
+        match(Op1I, m_And(m_Not(m_Specific(B)), m_Specific(A))))
+      return BinaryOperator::CreateOr(A, Builder->CreateNot(B));
+    // (A & ~B) ^ (B ^ ~A) -> (A | ~B)
+    if (match(Op0I, m_And(m_Value(A), m_Not(m_Value(B)))) &&
+        match(Op1I, m_Xor(m_Specific(B), m_Not(m_Specific(A)))))
+      return BinaryOperator::CreateOr(A, Builder->CreateNot(B));
   }
 
   // (A | B)^(~A) -> (A | ~B)
Index: test/Transforms/InstCombine/or-xor.ll
===================================================================
--- test/Transforms/InstCombine/or-xor.ll
+++ test/Transforms/InstCombine/or-xor.ll
@@ -139,13 +139,13 @@
 
 ; ((x | y) ^ (x ^ y)) -> (x & y)
 define i32 @test15(i32 %x, i32 %y) {
-  %1 = xor i32 %y, %x
-  %2 = or i32 %y, %x
-  %3 = xor i32 %2, %1
-  ret i32 %3
+  %xor = xor i32 %y, %x
+  %or = or i32 %y, %x
+  %xor1 = xor i32 %or, %xor
+  ret i32 %xor1
 ; CHECK-LABEL: @test15(
-; CHECK-NEXT: %1 = and i32 %y, %x
-; CHECK-NEXT: ret i32 %1
+; CHECK-NEXT: %xor1 = and i32 %y, %x
+; CHECK-NEXT: ret i32 %xor1
 }
 
 ; ((x | ~y) ^ (~x | y)) -> x ^ y
@@ -160,3 +160,17 @@
 ; CHECK-NEXT: %xor = xor i32 %x, %y
 ; CHECK-NEXT: ret i32 %xor
 }
+
+; ((~x ^ y) ^ (~y & x)) -> (x | ~y)
+define i32 @test17(i32 %x, i32 %y) {
+  %notx = xor i32 %x, -1
+  %xor1 = xor i32 %notx, %y
+  %noty = xor i32 %y, -1
+  %and = and i32 %noty, %x
+  %xor2 = xor i32 %xor1, %and
+  ret i32 %xor2
+; CHECK-LABEL: @test17(
+; CHECK-NEXT: %1 = xor i32 %y, -1
+; CHECK-NEXT: %xor2 = or i32 %x, %1
+; CHECK-NEXT: ret i32 %xor2
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D4878.12508.patch
Type: text/x-patch
Size: 2038 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140814/73bb0089/attachment.bin>


More information about the llvm-commits mailing list