From daafbe61687c9f44b6ec7e25817aa239278d9524 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Fri, 26 Jun 2009 22:53:46 +0000 Subject: Incorporate the insertion point into the key of SCEVExpander's CSE map. This helps it avoid reusing an instruction that doesn't dominate all of the users, in cases where the original instruction was inserted before all of the users were known. This may result in redundant expansions of sub-expressions that depend on loop-unpredictable values in some cases, however this isn't very common, and it primarily impacts IndVarSimplify, so GVN can be expected to clean these up. This eliminates the need for IndVarSimplify's FixUsesBeforeDefs, which fixes several bugs. llvm-svn: 74352 --- llvm/lib/Analysis/ScalarEvolutionExpander.cpp | 33 +++++++++++++++------------ 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'llvm/lib/Analysis/ScalarEvolutionExpander.cpp') diff --git a/llvm/lib/Analysis/ScalarEvolutionExpander.cpp b/llvm/lib/Analysis/ScalarEvolutionExpander.cpp index 6d7abc02ebe..4cc5ebc2953 100644 --- a/llvm/lib/Analysis/ScalarEvolutionExpander.cpp +++ b/llvm/lib/Analysis/ScalarEvolutionExpander.cpp @@ -468,13 +468,13 @@ Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) { const SCEV* Step = SE.getAnyExtendExpr(S->getStepRecurrence(SE), CanonicalIV->getType()); Value *V = expand(SE.getAddRecExpr(Start, Step, S->getLoop())); - BasicBlock::iterator SaveInsertPt = getInsertionPoint(); + BasicBlock::iterator SaveInsertPt = InsertPt; BasicBlock::iterator NewInsertPt = next(BasicBlock::iterator(cast(V))); while (isa(NewInsertPt)) ++NewInsertPt; V = expandCodeFor(SE.getTruncateExpr(SE.getUnknown(V), Ty), 0, NewInsertPt); - setInsertionPoint(SaveInsertPt); + InsertPt = SaveInsertPt; return V; } @@ -652,16 +652,10 @@ Value *SCEVExpander::expandCodeFor(const SCEV* SH, const Type *Ty) { } Value *SCEVExpander::expand(const SCEV *S) { - // Check to see if we already expanded this. - std::map >::iterator I = - InsertedExpressions.find(S); - if (I != InsertedExpressions.end()) - return I->second; + BasicBlock::iterator SaveInsertPt = InsertPt; // Compute an insertion point for this SCEV object. Hoist the instructions // as far out in the loop nest as possible. - BasicBlock::iterator InsertPt = getInsertionPoint(); - BasicBlock::iterator SaveInsertPt = InsertPt; for (Loop *L = SE.LI->getLoopFor(InsertPt->getParent()); ; L = L->getParentLoop()) if (S->isLoopInvariant(L)) { @@ -677,12 +671,23 @@ Value *SCEVExpander::expand(const SCEV *S) { while (isInsertedInstruction(InsertPt)) ++InsertPt; break; } - setInsertionPoint(InsertPt); + // Check to see if we already expanded this here. + std::map, + AssertingVH >::iterator I = + InsertedExpressions.find(std::make_pair(S, InsertPt)); + if (I != InsertedExpressions.end()) { + InsertPt = SaveInsertPt; + return I->second; + } + + // Expand the expression into instructions. Value *V = visit(S); - setInsertionPoint(SaveInsertPt); - InsertedExpressions[S] = V; + // Remember the expanded value for this SCEV at this location. + InsertedExpressions[std::make_pair(S, InsertPt)] = V; + + InsertPt = SaveInsertPt; return V; } @@ -696,8 +701,8 @@ SCEVExpander::getOrInsertCanonicalInductionVariable(const Loop *L, assert(Ty->isInteger() && "Can only insert integer induction variables!"); const SCEV* H = SE.getAddRecExpr(SE.getIntegerSCEV(0, Ty), SE.getIntegerSCEV(1, Ty), L); - BasicBlock::iterator SaveInsertPt = getInsertionPoint(); + BasicBlock::iterator SaveInsertPt = InsertPt; Value *V = expandCodeFor(H, 0, L->getHeader()->begin()); - setInsertionPoint(SaveInsertPt); + InsertPt = SaveInsertPt; return V; } -- cgit v1.2.3