SCEV: Use AssertingVH to catch dangling BasicBlock* when passes forget
authorAndrew Trick <atrick@apple.com>
Wed, 3 Aug 2011 18:32:11 +0000 (18:32 +0000)
committerAndrew Trick <atrick@apple.com>
Wed, 3 Aug 2011 18:32:11 +0000 (18:32 +0000)
to notify SCEV of a change. Add forgetLoop in a couple of those places.

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

include/llvm/Analysis/ScalarEvolution.h
lib/Transforms/Utils/LoopSimplify.cpp
lib/Transforms/Utils/LoopUnroll.cpp
test/Transforms/LoopSimplify/preserve-scev.ll

index fa9e402abdd15bd3a351d8f55cfec3235019095b..c621bec86a73f314c0e00eaf63eda5640345e30f 100644 (file)
@@ -264,7 +264,7 @@ namespace llvm {
     /// ExitNotTakenInfo - Information about the number of times a particular
     /// loop exit may be reached before exiting the loop.
     struct ExitNotTakenInfo {
-      BasicBlock *ExitingBlock;
+      AssertingVH<BasicBlock> ExitingBlock;
       const SCEV *ExactNotTaken;
       PointerIntPair<ExitNotTakenInfo*, 1> NextExit;
 
index 9af338ec7f4c884a5a2301e5e258395981c67e97..9b16c7cbe130624457ef78113dcd922d2fdb2d63 100644 (file)
@@ -325,6 +325,14 @@ ReprocessLoop:
       DEBUG(dbgs() << "LoopSimplify: Eliminating exiting block "
                    << ExitingBlock->getName() << "\n");
 
+      // If any reachable control flow within this loop has changed, notify
+      // ScalarEvolution. Currently assume the parent loop doesn't change
+      // (spliting edges doesn't count). If blocks, CFG edges, or other values
+      // in the parent loop change, then we need call to forgetLoop() for the
+      // parent instead.
+      if (SE)
+        SE->forgetLoop(L);
+
       assert(pred_begin(ExitingBlock) == pred_end(ExitingBlock));
       Changed = true;
       LI->removeBlock(ExitingBlock);
index dddf43ea573a4186951f80d79a83695254129f31..5a40a09580571bf02ed591e959a3f0dfbbe8067c 100644 (file)
@@ -58,7 +58,8 @@ static inline void RemapInstruction(Instruction *I,
 /// only has one predecessor, and that predecessor only has one successor.
 /// The LoopInfo Analysis that is passed will be kept consistent.
 /// Returns the new combined block.
-static BasicBlock *FoldBlockIntoPredecessor(BasicBlock *BB, LoopInfo* LI) {
+static BasicBlock *FoldBlockIntoPredecessor(BasicBlock *BB, LoopInfo* LI,
+                                            LPPassManager *LPM) {
   // Merge basic blocks into their predecessor if there is only one distinct
   // pred, and if there is only one distinct successor of the predecessor, and
   // if there are no PHI nodes.
@@ -90,6 +91,12 @@ static BasicBlock *FoldBlockIntoPredecessor(BasicBlock *BB, LoopInfo* LI) {
   std::string OldName = BB->getName();
 
   // Erase basic block from the function...
+
+  // ScalarEvolution holds references to loop exit blocks.
+  if (ScalarEvolution *SE = LPM->getAnalysisIfAvailable<ScalarEvolution>()) {
+    if (Loop *L = LI->getLoopFor(BB))
+      SE->forgetLoop(L);
+  }
   LI->removeBlock(BB);
   BB->eraseFromParent();
 
@@ -364,7 +371,7 @@ bool llvm::UnrollLoop(Loop *L, unsigned Count, unsigned TripCount,
     BranchInst *Term = cast<BranchInst>(Latches[i]->getTerminator());
     if (Term->isUnconditional()) {
       BasicBlock *Dest = Term->getSuccessor(0);
-      if (BasicBlock *Fold = FoldBlockIntoPredecessor(Dest, LI))
+      if (BasicBlock *Fold = FoldBlockIntoPredecessor(Dest, LI, LPM))
         std::replace(Latches.begin(), Latches.end(), Dest, Fold);
     }
   }
index 017a0d210849887aa7882d1d526aba18ccaf67aa..23ac7f257a38498ca4a89eacdf4d7b2eae11e050 100644 (file)
@@ -48,3 +48,91 @@ return:                                           ; preds = %for.body18, %for.bo
 }
 
 declare void @foo() nounwind
+
+; Notify SCEV when removing an ExitingBlock.
+; CHECK: @mergeExit
+; CHECK: while.cond191:
+; CHECK: br i1 %or.cond, label %while.body197
+; CHECK-NOT: land.rhs:
+; CHECK: ret
+define void @mergeExit(i32 %MapAttrCount) nounwind uwtable ssp {
+entry:
+  br i1 undef, label %if.then124, label %if.end126
+
+if.then124:                                       ; preds = %entry
+  unreachable
+
+if.end126:                                        ; preds = %entry
+  br i1 undef, label %while.body.lr.ph, label %if.end591
+
+while.body.lr.ph:                                 ; preds = %if.end126
+  br i1 undef, label %if.end140, label %if.then137
+
+if.then137:                                       ; preds = %while.body.lr.ph
+  unreachable
+
+if.end140:                                        ; preds = %while.body.lr.ph
+  br i1 undef, label %while.cond191.outer, label %if.then148
+
+if.then148:                                       ; preds = %if.end140
+  unreachable
+
+while.cond191.outer:                              ; preds = %if.then205, %if.end140
+  br label %while.cond191
+
+while.cond191:                                    ; preds = %while.body197, %while.cond191.outer
+  %CppIndex.0 = phi i32 [ %inc, %while.body197 ], [ undef, %while.cond191.outer ]
+  br i1 undef, label %land.rhs, label %if.then216
+
+land.rhs:                                         ; preds = %while.cond191
+  %inc = add i32 %CppIndex.0, 1
+  %cmp196 = icmp ult i32 %inc, %MapAttrCount
+  br i1 %cmp196, label %while.body197, label %if.then216
+
+while.body197:                                    ; preds = %land.rhs
+  br i1 undef, label %if.then205, label %while.cond191
+
+if.then205:                                       ; preds = %while.body197
+  br label %while.cond191.outer
+
+if.then216:                                       ; preds = %land.rhs, %while.cond191
+  br i1 undef, label %if.else, label %if.then221
+
+if.then221:                                       ; preds = %if.then216
+  unreachable
+
+if.else:                                          ; preds = %if.then216
+  br i1 undef, label %if.then266, label %if.end340
+
+if.then266:                                       ; preds = %if.else
+  switch i32 undef, label %if.else329 [
+    i32 17, label %if.then285
+    i32 19, label %if.then285
+    i32 18, label %if.then285
+    i32 15, label %if.then285
+  ]
+
+if.then285:                                       ; preds = %if.then266, %if.then266, %if.then266, %if.then266
+  br i1 undef, label %if.then317, label %if.else324
+
+if.then317:                                       ; preds = %if.then285
+  br label %if.end340
+
+if.else324:                                       ; preds = %if.then285
+  unreachable
+
+if.else329:                                       ; preds = %if.then266
+  unreachable
+
+if.end340:                                        ; preds = %if.then317, %if.else
+  unreachable
+
+if.end591:                                        ; preds = %if.end126
+  br i1 undef, label %cond.end, label %cond.false
+
+cond.false:                                       ; preds = %if.end591
+  unreachable
+
+cond.end:                                         ; preds = %if.end591
+  ret void
+}