[llvm-commits] [llvm] r119984 - in /llvm/trunk: lib/Target/README.txt lib/Transforms/InstCombine/InstCombineAddSub.cpp test/Transforms/InstCombine/sub.ll

Benjamin Kramer benny.kra at googlemail.com
Mon Nov 22 12:31:28 PST 2010


Author: d0k
Date: Mon Nov 22 14:31:27 2010
New Revision: 119984

URL: http://llvm.org/viewvc/llvm-project?rev=119984&view=rev
Log:
InstCombine: Implement X - A*-B -> X + A*B.

Modified:
    llvm/trunk/lib/Target/README.txt
    llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
    llvm/trunk/test/Transforms/InstCombine/sub.ll

Modified: llvm/trunk/lib/Target/README.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/README.txt?rev=119984&r1=119983&r2=119984&view=diff
==============================================================================
--- llvm/trunk/lib/Target/README.txt (original)
+++ llvm/trunk/lib/Target/README.txt Mon Nov 22 14:31:27 2010
@@ -1736,22 +1736,6 @@
 ...
   %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:

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp?rev=119984&r1=119983&r2=119984&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp Mon Nov 22 14:31:27 2010
@@ -675,6 +675,15 @@
                                              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);
+      }
     }
   }
 

Modified: llvm/trunk/test/Transforms/InstCombine/sub.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/sub.ll?rev=119984&r1=119983&r2=119984&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/sub.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/sub.ll Mon Nov 22 14:31:27 2010
@@ -281,3 +281,23 @@
 ; 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
+}





More information about the llvm-commits mailing list