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

Duncan Sands baldrick at free.fr
Fri Jan 14 07:26:10 PST 2011


Author: baldrick
Date: Fri Jan 14 09:26:10 2011
New Revision: 123442

URL: http://llvm.org/viewvc/llvm-project?rev=123442&view=rev
Log:
Turn X-(X-Y) into Y.  According to my auto-simplifier this is the most common
simplification present in fully optimized code (I think instcombine fails to
transform some of these when "X-Y" has more than one use).  Fires here and
there all over the test-suite, for example it eliminates 8 subtractions in
the final IR for 445.gobmk, 2 subs in 447.dealII, 2 in paq8p etc.

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

Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=123442&r1=123441&r2=123442&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Fri Jan 14 09:26:10 2011
@@ -593,11 +593,25 @@
       match(Op0, m_Add(m_Specific(Op1), m_Value(X))))
     return X;
 
-  /// i1 sub -> xor.
+  // i1 sub -> xor.
   if (MaxRecurse && Op0->getType()->isIntegerTy(1))
     if (Value *V = SimplifyXorInst(Op0, Op1, TD, DT, MaxRecurse-1))
       return V;
 
+  // X - (X - Y) -> Y.  More generally Z - (X - Y) -> (Z - X) + Y if everything
+  // simplifies.
+  Value *Y = 0, *Z = Op0;
+  if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y)
+    // See if "V === Z - X" simplifies.
+    if (Value *V = SimplifyBinOp(Instruction::Sub, Z, X, TD, DT, MaxRecurse-1))
+      // It does!  Now see if "W === V + Y" simplifies.
+      if (Value *W = SimplifyBinOp(Instruction::Add, V, Y, TD, DT,
+                                   MaxRecurse-1)) {
+        // It does, we successfully reassociated!
+        ++NumReassoc;
+        return W;
+      }
+
   // Mul distributes over Sub.  Try some generic simplifications based on this.
   if (Value *V = FactorizeBinOp(Instruction::Sub, Op0, Op1, Instruction::Mul,
                                 TD, DT, MaxRecurse))

Modified: llvm/trunk/test/Transforms/InstSimplify/2010-12-20-Reassociate.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/2010-12-20-Reassociate.ll?rev=123442&r1=123441&r2=123442&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/2010-12-20-Reassociate.ll (original)
+++ llvm/trunk/test/Transforms/InstSimplify/2010-12-20-Reassociate.ll Fri Jan 14 09:26:10 2011
@@ -62,3 +62,11 @@
   ret i32 %l
 ; CHECK: ret i32 %y
 }
+
+define i32 @sub1(i32 %x, i32 %y) {
+; CHECK: @sub1
+  %d = sub i32 %x, %y
+  %r = sub i32 %x, %d
+  ret i32 %r
+; CHECK: ret i32 %y
+}





More information about the llvm-commits mailing list