[llvm-commits] [llvm] r123754 - in /llvm/trunk: lib/Analysis/InstructionSimplify.cpp test/Transforms/InstSimplify/2010-12-20-Distribute.ll

Duncan Sands baldrick at free.fr
Tue Jan 18 01:24:58 PST 2011


Author: baldrick
Date: Tue Jan 18 03:24:58 2011
New Revision: 123754

URL: http://llvm.org/viewvc/llvm-project?rev=123754&view=rev
Log:
Simplify (X<<1)-X into X.  According to my auto-simplier this is the most common missed
simplification in fully optimized code.  It occurs sporadically in the testsuite, and
many times in 403.gcc: the final bitcode has 131 fewer subtractions after this change.
The reason that the multiplies are not eliminated is the same reason that instcombine
did not catch this: they are used by other instructions (instcombine catches this with
a more general transform which in general is only profitable if the operands have only
one use).

Modified:
    llvm/trunk/lib/Analysis/InstructionSimplify.cpp
    llvm/trunk/test/Transforms/InstSimplify/2010-12-20-Distribute.ll

Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=123754&r1=123753&r2=123754&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Tue Jan 18 03:24:58 2011
@@ -593,6 +593,12 @@
       match(Op0, m_Add(m_Specific(Op1), m_Value(X))))
     return X;
 
+  // (X*2) - X -> X
+  // (X<<1) - X -> X
+  if (match(Op0, m_Mul(m_Specific(Op1), m_ConstantInt<2>())) ||
+      match(Op0, m_Shl(m_Specific(Op1), m_One())))
+    return Op1;
+
   // i1 sub -> xor.
   if (MaxRecurse && Op0->getType()->isIntegerTy(1))
     if (Value *V = SimplifyXorInst(Op0, Op1, TD, DT, MaxRecurse-1))

Modified: llvm/trunk/test/Transforms/InstSimplify/2010-12-20-Distribute.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/2010-12-20-Distribute.ll?rev=123754&r1=123753&r2=123754&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/2010-12-20-Distribute.ll (original)
+++ llvm/trunk/test/Transforms/InstSimplify/2010-12-20-Distribute.ll Tue Jan 18 03:24:58 2011
@@ -31,6 +31,26 @@
 ; CHECK: ret i32 %r
 }
 
+define i32 @factorize4(i32 %x, i32 %y) {
+; CHECK: @factorize4
+  %sh = shl i32 %y, 1
+  %ml = mul i32 %sh, %x
+  %mr = mul i32 %x, %y
+  %s = sub i32 %ml, %mr
+  ret i32 %s
+; CHECK: ret i32 %mr
+}
+
+define i32 @factorize5(i32 %x, i32 %y) {
+; CHECK: @factorize5
+  %sh = mul i32 %y, 2
+  %ml = mul i32 %sh, %x
+  %mr = mul i32 %x, %y
+  %s = sub i32 %ml, %mr
+  ret i32 %s
+; CHECK: ret i32 %mr
+}
+
 define i32 @expand(i32 %x) {
 ; CHECK: @expand
 ; ((X & 1) | 2) & 1 -> ((X & 1) & 1) | (2 & 1) -> (X & 1) | 0 -> X & 1





More information about the llvm-commits mailing list