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

suyog suyog.sarda at samsung.com
Fri Jul 18 12:16:01 PDT 2014


Hi nicholas, rafael, dexonsmith,

This patch implements transform for pattern "(A | B) ^ (~A) = (~A) ^ (A | B) -> (A | ~B)"

Please help in reviewing the same.

Thanks,
Suyog

http://reviews.llvm.org/D4588

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
@@ -2446,6 +2446,12 @@
     }
   }
 
+  // (A | B)^(~A) = (~A)^(A | B) -> (A | ~B)
+  Value *A = nullptr, *B = nullptr;
+  if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
+      match(Op1, m_Not(m_Specific(A))))
+    return BinaryOperator::CreateOr(A, Builder->CreateNot(B));
+
   // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B)
   if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
     if (ICmpInst *LHS = dyn_cast<ICmpInst>(I.getOperand(0)))
Index: test/Transforms/InstCombine/xor2.ll
===================================================================
--- test/Transforms/InstCombine/xor2.ll
+++ test/Transforms/InstCombine/xor2.ll
@@ -82,3 +82,25 @@
 ; CHECK: lshr i32 %x, 16
 ; CHECK: ret
 }
+
+; (A | B) ^ (~A) -> (A | ~B)
+define i32 @test7(i32 %a, i32 %b) #0 {
+  %or = or i32 %a, %b
+  %neg = xor i32 %a, -1
+  %xor = xor i32 %or, %neg
+  ret i32 %xor
+; CHECK-LABEL: @test7(
+; CHECK-NEXT: %1 = xor i32 %b, -1
+; CHECK-NEXT: %xor = or i32 %a, %1
+}
+
+; (~A) ^ (A | B) -> (A | ~B)
+define i32 @test8(i32 %a, i32 %b) #0 {
+  %neg = xor i32 %a, -1
+  %or = or i32 %a, %b
+  %xor = xor i32 %neg, %or
+  ret i32 %xor
+; CHECK-LABEL: @test8(
+; CHECK-NEXT: %1 = xor i32 %b, -1
+; CHECK-NEXT: %xor = or i32 %a, %1
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D4588.11657.patch
Type: text/x-patch
Size: 1464 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140718/74994838/attachment.bin>


More information about the llvm-commits mailing list