InstCombine: (sub (or A B) (xor A B)) --> (and A B)
authorDavid Majnemer <david.majnemer@gmail.com>
Sun, 19 Oct 2014 08:32:32 +0000 (08:32 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Sun, 19 Oct 2014 08:32:32 +0000 (08:32 +0000)
The following implements the transformation:
(sub (or A B) (xor A B)) --> (and A B).

Patch by Ankur Garg!

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@220163 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/InstCombine/InstCombineAddSub.cpp
test/Transforms/InstCombine/sub.ll

index ace759783314e5e3dcc1a465f5fecadaa03d0497..ef6875238dddebc83196c20e6d2d98dd94378d5f 100644 (file)
@@ -1621,6 +1621,15 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) {
       return BinaryOperator::CreateNeg(Y);
   }
 
+  // (sub (or A, B) (xor A, B)) --> (and A, B)
+  {
+    Value *A = nullptr, *B = nullptr;
+    if (match(Op1, m_Xor(m_Value(A), m_Value(B))) &&
+        (match(Op0, m_Or(m_Specific(A), m_Specific(B))) ||
+         match(Op0, m_Or(m_Specific(B), m_Specific(A)))))
+      return BinaryOperator::CreateAnd(A, B);
+  }
+
   if (Op1->hasOneUse()) {
     Value *X = nullptr, *Y = nullptr, *Z = nullptr;
     Constant *C = nullptr;
index 1d1bedc0a7bb1d06fcac0b1237182428af8d9d3b..95a61b1747c77e52c02d15088c55cfeb7a770aa7 100644 (file)
@@ -530,3 +530,13 @@ define i32 @test44(i32 %x) {
 ; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 %x, -32768
 ; CHECK: ret i32 [[ADD]]
 }
+
+define i32 @test45(i32 %x, i32 %y) {
+  %or = or i32 %x, %y
+  %xor = xor i32 %x, %y
+  %sub = sub i32 %or, %xor
+  ret i32 %sub
+; CHECK-LABEL: @test45(
+; CHECK-NEXT: %sub = and i32 %x, %y
+; CHECK: ret i32 %sub
+}