Skip to content

Commit f0b77f9

Browse files
committed
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 llvm-svn: 24102
1 parent 67315dc commit f0b77f9

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

llvm/lib/Analysis/ScalarEvolutionExpander.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,34 @@ Value *SCEVExpander::visitAddRecExpr(SCEVAddRecExpr *S) {
8787
// Get the canonical induction variable I for this loop.
8888
Value *I = getOrInsertCanonicalInductionVariable(L, Ty);
8989

90+
// If this is a simple linear addrec, emit it now as a special case.
9091
if (S->getNumOperands() == 2) { // {0,+,F} --> i*F
9192
Value *F = expandInTy(S->getOperand(1), Ty);
92-
return BinaryOperator::createMul(I, F, "tmp.", InsertPt);
93+
94+
// IF the step is by one, just return the inserted IV.
95+
if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(F))
96+
if (CI->getRawValue() == 1)
97+
return I;
98+
99+
// If the insert point is directly inside of the loop, emit the multiply at
100+
// the insert point. Otherwise, L is a loop that is a parent of the insert
101+
// point loop. If we can, move the multiply to the outer most loop that it
102+
// is safe to be in.
103+
Instruction *MulInsertPt = InsertPt;
104+
Loop *InsertPtLoop = LI.getLoopFor(MulInsertPt->getParent());
105+
if (InsertPtLoop != L && InsertPtLoop &&
106+
L->contains(InsertPtLoop->getHeader())) {
107+
while (InsertPtLoop != L) {
108+
// If we cannot hoist the multiply out of this loop, don't.
109+
if (!InsertPtLoop->isLoopInvariant(F)) break;
110+
111+
// Otherwise, move the insert point to the preheader of the loop.
112+
MulInsertPt = InsertPtLoop->getLoopPreheader()->getTerminator();
113+
InsertPtLoop = InsertPtLoop->getParentLoop();
114+
}
115+
}
116+
117+
return BinaryOperator::createMul(I, F, "tmp.", MulInsertPt);
93118
}
94119

95120
// If this is a chain of recurrences, turn it into a closed form, using the

0 commit comments

Comments
 (0)