[PATCH] Added InstCombine transform for pattern "(A ^ B) | ((~A) ^ B) -> True".

sonam kumari sonam.kumari at samsung.com
Tue Aug 5 03:21:48 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 @test14(i32 %x, i32 %y) {
  %nega = xor i32 %x, -1
  %xor = xor i32 %nega, %y
  %xor1 = xor i32 %x, %y
  %or = or i32 %xor, %xor1
  ret i32 %or
}

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

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

Thanks,
Sonam.

http://reviews.llvm.org/D4691

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

Index: test/Transforms/InstCombine/or-xor.ll
===================================================================
--- test/Transforms/InstCombine/or-xor.ll
+++ test/Transforms/InstCombine/or-xor.ll
@@ -136,3 +136,23 @@
 ; CHECK-NEXT: %and = and i32 %x, %y
 ; CHECK-NEXT: ret i32 %and
 }
+
+define i32 @test14(i32 %x, i32 %y) {
+  %nega = xor i32 %x, -1
+  %xor = xor i32 %nega, %y
+  %xor1 = xor i32 %x, %y
+  %or = or i32 %xor, %xor1
+  ret i32 %or
+; CHECK-LABEL: @test14(
+; CHECK-NEXT: ret i32 -1
+}
+
+define i32 @test15(i32 %x, i32 %y) {
+  %xor = xor i32 %x, %y
+  %nega = xor i32 %x, -1
+  %xor1 = xor i32 %nega, %y
+  %or = or i32 %xor, %xor1
+  ret i32 %or
+; CHECK-LABEL: @test15(
+; CHECK-NEXT: ret i32 -1
+}
Index: lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -2113,14 +2113,18 @@
   }
 
   // (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::CreateOr(Op0, 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::CreateOr(Op1, C);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D4691.12192.patch
Type: text/x-patch
Size: 1972 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140805/6ae45957/attachment.bin>


More information about the llvm-commits mailing list