InstCombine: Implement X - A*-B -> X + A*B.
authorBenjamin Kramer <benny.kra@googlemail.com>
Mon, 22 Nov 2010 20:31:27 +0000 (20:31 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Mon, 22 Nov 2010 20:31:27 +0000 (20:31 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@119984 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Target/README.txt
lib/Transforms/InstCombine/InstCombineAddSub.cpp
test/Transforms/InstCombine/sub.ll

index 1ec4cdf4ba95232528efbef248e7cd07cb8329bb..4d5b17ab3671b4b1ac2d4a46f6dc606bb8d8a9ae 100644 (file)
@@ -1736,22 +1736,6 @@ Another similar case involves truncations on 64-bit targets:
 ...
   %367 = icmp eq i32 %362, 0                      ; [#uses=1]
 
-
-//===---------------------------------------------------------------------===//
-
-Missed instcombine/dagcombine transformation:
-define i32 @a(i32 %x, i32 %y) nounwind readnone {
-entry:
-  %mul = mul i32 %y, -8
-  %sub = sub i32 %x, %mul
-  ret i32 %sub
-}
-
-Should compile to something like x+y*8, but currently compiles to an
-inefficient result.  Testcase derived from gcc.  C testcase:
-
-int a(int x, int y) { return y-x*-8; }
-
 //===---------------------------------------------------------------------===//
 
 Missed instcombine/dagcombine transformation:
index 9b72eb924ac4a68aebfaf47767d097122af17943..c04a6b2a627e85ca10e8c74c3279b8b0da718662 100644 (file)
@@ -675,6 +675,15 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) {
                                              C2);
         return BinaryOperator::CreateMul(Op0, CP1);
       }
+
+      // X - A*-B -> X + A*B
+      // X - -A*B -> X + A*B
+      Value *A, *B;
+      if (match(Op1I, m_Mul(m_Value(A), m_Neg(m_Value(B)))) ||
+          match(Op1I, m_Mul(m_Neg(m_Value(A)), m_Value(B)))) {
+        Value *NewMul = Builder->CreateMul(A, B);
+        return BinaryOperator::CreateAdd(Op0, NewMul);
+      }
     }
   }
 
index 29bd7be2ff84c8c471c653974db4f126a5cf4c9c..d88860056a1d5f72d27f92bbd35a6d1f11288313 100644 (file)
@@ -281,3 +281,23 @@ define i32 @test26(i32 %x) {
 ; CHECK-NEXT: ret i32
 }
 
+define i32 @test27(i32 %x, i32 %y) {
+  %mul = mul i32 %y, -8
+  %sub = sub i32 %x, %mul
+  ret i32 %sub
+; CHECK: @test27
+; CHECK-NEXT: shl i32 %y, 3
+; CHECK-NEXT: add i32
+; CHECK-NEXT: ret i32
+}
+
+define i32 @test28(i32 %x, i32 %y, i32 %z) {
+  %neg = sub i32 0, %z
+  %mul = mul i32 %neg, %y
+  %sub = sub i32 %x, %mul
+  ret i32 %sub
+; CHECK: @test28
+; CHECK-NEXT: mul i32 %z, %y
+; CHECK-NEXT: add i32
+; CHECK-NEXT: ret i32
+}