Fix a problem that Nate noticed with LSR:
authorChris Lattner <sabre@nondot.org>
Sun, 30 Oct 2005 06:24:33 +0000 (06:24 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 30 Oct 2005 06:24:33 +0000 (06:24 +0000)
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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@24102 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/ScalarEvolutionExpander.cpp

index b153c6f1568b51cdb25881cd54b83cd7c105a5c5..e2a98d4d862d76296eba4b0d2296c31869c10221 100644 (file)
@@ -87,9 +87,34 @@ Value *SCEVExpander::visitAddRecExpr(SCEVAddRecExpr *S) {
   // 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