Fix a bug where we could corrupt a parent loop's header info if we unrolled
authorChris Lattner <sabre@nondot.org>
Sun, 6 Mar 2005 20:57:32 +0000 (20:57 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 6 Mar 2005 20:57:32 +0000 (20:57 +0000)
a nested loop.  This fixes Transforms/LoopUnroll/2005-03-06-BadLoopInfoUpdate.ll
and PR532

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

lib/Transforms/Scalar/LoopUnroll.cpp

index 8e625202455e38a0f51767a448c200f07d687d19..da80ee342b9ab442c6305aabfd2f69ba6534e835 100644 (file)
@@ -283,16 +283,27 @@ bool LoopUnroll::visitLoop(Loop *L) {
   // Preheader.
   Preheader->replaceAllUsesWith(LoopExit);
 
+  Function *F = LoopExit->getParent();
+  if (Parent) {
+    // Otherwise, if this is a sub-loop, and the preheader was the loop header
+    // of the parent loop, move the exit block to be the new parent loop header.
+    if (Parent->getHeader() == Preheader) {
+      assert(Parent->contains(LoopExit) &&
+             "Exit block isn't contained in parent?");
+      Parent->moveToHeader(LoopExit);
+    }
+  } else {
+    // If the preheader was the entry block of this function, move the exit
+    // block to be the new entry of the function.
+    if (Preheader == &F->front())
+      F->getBasicBlockList().splice(F->begin(),
+                                    F->getBasicBlockList(), LoopExit);
+  }
+
   // Remove BB and LoopExit from our analyses.
   LI->removeBlock(Preheader);
   LI->removeBlock(BB);
 
-  // If the preheader was the entry block of this function, move the exit block
-  // to be the new entry of the loop.
-  Function *F = LoopExit->getParent();
-  if (Preheader == &F->front())
-    F->getBasicBlockList().splice(F->begin(), F->getBasicBlockList(), LoopExit);
-
   // Actually delete the blocks now.
   F->getBasicBlockList().erase(Preheader);
   F->getBasicBlockList().erase(BB);