[llvm-commits] CVS: llvm/lib/Analysis/ScalarEvolutionExpander.cpp

Chris Lattner lattner at cs.uiuc.edu
Sat Oct 29 23:24:45 PDT 2005



Changes in directory llvm/lib/Analysis:

ScalarEvolutionExpander.cpp updated: 1.1 -> 1.2
---
Log message:

Fix a problem that Nate noticed with LSR:

When inserting code for an addrec expression with a non-unit stride, be
more careful where we insert the multiply.  In particular, insert the multiply
in the outermost loop we can, instead of the requested insertion point.

This allows LSR to notice the mul in the right loop, reducing it when it gets
to it.  This allows it to reduce the multiply, where before it missed it.

This happens quite a bit in the test suite, for example, eliminating 2 
multiplies in art, 3 in ammp, 4 in apsi, reducing from 1050 multiplies to
910 muls in galgel (!), from 877 to 859 in applu, and 36 to 30 in bzip2.

This speeds up galgel from 16.45s to 16.01s, applu from 14.21 to 13.94s and
fourinarow from 66.67s to 63.48s.

This implements Transforms/LoopStrengthReduce/nested-reduce.ll



---
Diffs of the changes:  (+26 -1)

 ScalarEvolutionExpander.cpp |   27 ++++++++++++++++++++++++++-
 1 files changed, 26 insertions(+), 1 deletion(-)


Index: llvm/lib/Analysis/ScalarEvolutionExpander.cpp
diff -u llvm/lib/Analysis/ScalarEvolutionExpander.cpp:1.1 llvm/lib/Analysis/ScalarEvolutionExpander.cpp:1.2
--- llvm/lib/Analysis/ScalarEvolutionExpander.cpp:1.1	Fri Jul 29 19:12:19 2005
+++ llvm/lib/Analysis/ScalarEvolutionExpander.cpp	Sun Oct 30 01:24:33 2005
@@ -87,9 +87,34 @@
   // Get the canonical induction variable I for this loop.
   Value *I = getOrInsertCanonicalInductionVariable(L, Ty);
 
+  // If this is a simple linear addrec, emit it now as a special case.
   if (S->getNumOperands() == 2) {   // {0,+,F} --> i*F
     Value *F = expandInTy(S->getOperand(1), Ty);
-    return BinaryOperator::createMul(I, F, "tmp.", InsertPt);
+    
+    // IF the step is by one, just return the inserted IV.
+    if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(F))
+      if (CI->getRawValue() == 1)
+        return I;
+    
+    // If the insert point is directly inside of the loop, emit the multiply at
+    // the insert point.  Otherwise, L is a loop that is a parent of the insert
+    // point loop.  If we can, move the multiply to the outer most loop that it
+    // is safe to be in.
+    Instruction *MulInsertPt = InsertPt;
+    Loop *InsertPtLoop = LI.getLoopFor(MulInsertPt->getParent());
+    if (InsertPtLoop != L && InsertPtLoop &&
+        L->contains(InsertPtLoop->getHeader())) {
+      while (InsertPtLoop != L) {
+        // If we cannot hoist the multiply out of this loop, don't.
+        if (!InsertPtLoop->isLoopInvariant(F)) break;
+
+        // Otherwise, move the insert point to the preheader of the loop.
+        MulInsertPt = InsertPtLoop->getLoopPreheader()->getTerminator();
+        InsertPtLoop = InsertPtLoop->getParentLoop();
+      }
+    }
+    
+    return BinaryOperator::createMul(I, F, "tmp.", MulInsertPt);
   }
 
   // If this is a chain of recurrences, turn it into a closed form, using the






More information about the llvm-commits mailing list