When restoring a saved insert location, check to see if the saved
authorDan Gohman <gohman@apple.com>
Mon, 15 Feb 2010 00:21:43 +0000 (00:21 +0000)
committerDan Gohman <gohman@apple.com>
Mon, 15 Feb 2010 00:21:43 +0000 (00:21 +0000)
insert location has become an "inserted" instruction since the time
it was saved. If so, advance to the first non-"inserted" instruction.

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

include/llvm/Analysis/ScalarEvolutionExpander.h
lib/Analysis/ScalarEvolutionExpander.cpp

index 303bc1e20a7324b8588c9c67dec1e1fef590d16c..26dc0c4a54a682af155e664bd55958d4d7addb5b 100644 (file)
@@ -170,6 +170,8 @@ namespace llvm {
 
     void rememberInstruction(Value *I);
 
+    void restoreInsertPoint(BasicBlock *BB, BasicBlock::iterator I);
+
     Value *expandAddRecExprLiterally(const SCEVAddRecExpr *);
     PHINode *getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
                                        const Loop *L,
index 88c22b160382e94c68f9b6ea794bebb222bdf5be..15384c1920fb2ee4be7526d3f7b8d12755c45f2b 100644 (file)
@@ -729,7 +729,7 @@ SCEVExpander::getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
 
   // Restore the original insert point.
   if (SaveInsertBB)
-    Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt);
+    restoreInsertPoint(SaveInsertBB, SaveInsertPt);
 
   // Remember this PHI, even in post-inc mode.
   InsertedValues.insert(PN);
@@ -845,7 +845,7 @@ Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) {
     while (isa<PHINode>(NewInsertPt)) ++NewInsertPt;
     V = expandCodeFor(SE.getTruncateExpr(SE.getUnknown(V), Ty), 0,
                       NewInsertPt);
-    Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt);
+    restoreInsertPoint(SaveInsertBB, SaveInsertPt);
     return V;
   }
 
@@ -1071,7 +1071,7 @@ Value *SCEVExpander::expand(const SCEV *S) {
   if (!PostIncLoop)
     InsertedExpressions[std::make_pair(S, InsertPt)] = V;
 
-  Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt);
+  restoreInsertPoint(SaveInsertBB, SaveInsertPt);
   return V;
 }
 
@@ -1089,6 +1089,14 @@ void SCEVExpander::rememberInstruction(Value *I) {
   }
 }
 
+void SCEVExpander::restoreInsertPoint(BasicBlock *BB, BasicBlock::iterator I) {
+  // If we aquired more instructions since the old insert point was saved,
+  // advance past them.
+  while (isInsertedInstruction(I)) ++I;
+
+  Builder.SetInsertPoint(BB, I);
+}
+
 /// getOrInsertCanonicalInductionVariable - This method returns the
 /// canonical induction variable of the specified type for the specified
 /// loop (inserting one if there is none).  A canonical induction variable
@@ -1103,6 +1111,6 @@ SCEVExpander::getOrInsertCanonicalInductionVariable(const Loop *L,
   BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
   Value *V = expandCodeFor(H, 0, L->getHeader()->begin());
   if (SaveInsertBB)
-    Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt);
+    restoreInsertPoint(SaveInsertBB, SaveInsertPt);
   return V;
 }