[PATCH] Added InstCombine transform for pattern "(A ^ B) & ((~A) ^ B) -> False"

sonam kumari sonam.kumari at samsung.com
Tue Aug 5 03:19:16 PDT 2014


Hi David,

Thanks for your valuable comments.I will try to consider general case while doing any optimization in future.
But the patch which you uploaded in rL214342 fails for the following test cases as it doesn't take into consideration the symmetric case. 

The test cases for which it fails are :

define i32 @test15(i32 %x, i32 %y) {
 %nega = xor i32 %x, -1
 %xor = xor i32 %nega, %y
 %xor1 = xor i32 %x, %y
 %and = and i32 %xor, %xor1
 ret i32 %and
}

define i32 @test16(i32 %x, i32 %y) {
 %xor = xor i32 %x, %y
 %nega = xor i32 %x, -1
 %xor1 = xor i32 %nega, %y
 %and = and i32 %xor, %xor1
 ret i32 %and
}

So, I modified the patch and submitting the same.
Kindly review it.

Thanks,
Sonam.

http://reviews.llvm.org/D4690

Files:
  lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
  test/Transforms/InstCombine/xor2.ll

Index: lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -1285,14 +1285,18 @@
       return BinaryOperator::CreateAnd(A, Op0);
 
     // (A ^ B) & ((B ^ C) ^ A) -> (A ^ B) & ~C
-    if (match(Op0, m_Xor(m_Value(A), m_Value(B))))
-      if (match(Op1, m_Xor(m_Xor(m_Specific(B), m_Value(C)), m_Specific(A))))
+    // (B ^ A) & ((B ^ C) ^ A) -> (A ^ B) & ~C
+    if (match(Op1, m_Xor(m_Xor(m_Value(B), m_Value(C)), m_Value(A))))
+      if (match(Op0, m_Xor(m_Specific(A), m_Specific(B))) ||
+          match(Op0, m_Xor(m_Specific(B), m_Specific(A))))
         if (Op1->hasOneUse() || cast<BinaryOperator>(Op1)->hasOneUse())
           return BinaryOperator::CreateAnd(Op0, Builder->CreateNot(C));
 
     // ((A ^ C) ^ B) & (B ^ A) -> (B ^ A) & ~C
+    // ((A ^ C) ^ B) & (A ^ B) -> (B ^ A) & ~C
     if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B))))
-      if (match(Op1, m_Xor(m_Specific(B), m_Specific(A))))
+      if (match(Op1, m_Xor(m_Specific(B), m_Specific(A))) ||
+          match(Op1, m_Xor(m_Specific(A), m_Specific(B))))
         if (Op0->hasOneUse() || cast<BinaryOperator>(Op0)->hasOneUse())
           return BinaryOperator::CreateAnd(Op1, Builder->CreateNot(C));
 
Index: test/Transforms/InstCombine/xor2.ll
===================================================================
--- test/Transforms/InstCombine/xor2.ll
+++ test/Transforms/InstCombine/xor2.ll
@@ -167,3 +167,23 @@
 ; CHECK-NEXT: %1 = and i32 %a, %b
 ; CHECK-NEXT: %xor = xor i32 %1, -1
 }
+
+define i32 @test15(i32 %x, i32 %y) {
+ %nega = xor i32 %x, -1
+ %xor = xor i32 %nega, %y
+ %xor1 = xor i32 %x, %y
+ %and = and i32 %xor, %xor1
+ ret i32 %and
+; CHECK-LABEL: @test15(
+; CHECK-NEXT: ret i32 0
+}
+
+define i32 @test16(i32 %x, i32 %y) {
+ %xor = xor i32 %x, %y
+ %nega = xor i32 %x, -1
+ %xor1 = xor i32 %nega, %y
+ %and = and i32 %xor, %xor1
+ ret i32 %and
+; CHECK-LABEL: @test16(
+; CHECK-NEXT: ret i32 0
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D4690.12191.patch
Type: text/x-patch
Size: 2091 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140805/ee558dc9/attachment.bin>


More information about the llvm-commits mailing list