LoopRotate requires canonical loop form, so it always has preheaders
authorChris Lattner <sabre@nondot.org>
Sat, 8 Jan 2011 18:06:22 +0000 (18:06 +0000)
committerChris Lattner <sabre@nondot.org>
Sat, 8 Jan 2011 18:06:22 +0000 (18:06 +0000)
and latch blocks.  Reorder entry conditions to make hte pass faster
and more logical.

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

lib/Transforms/Scalar/LoopRotation.cpp

index f38944178267d2e6889b939de4d696e90b522fea..1af468aa2a99cb99043d343ecac1f57f938c1d11 100644 (file)
@@ -98,29 +98,22 @@ bool LoopRotate::runOnLoop(Loop *L, LPPassManager &LPM) {
 
 /// Rotate loop LP. Return true if the loop is rotated.
 bool LoopRotate::rotateLoop(Loop *L) {
-  BasicBlock *OrigPreHeader = L->getLoopPreheader();
-  if (!OrigPreHeader) return false;
-
-  BasicBlock *OrigLatch = L->getLoopLatch();
-  if (!OrigLatch) return false;
-
-  BasicBlock *OrigHeader =  L->getHeader();
-
   // If the loop has only one block then there is not much to rotate.
   if (L->getBlocks().size() == 1)
     return false;
-
+  
+  BasicBlock *OrigHeader = L->getHeader();
+  
+  BranchInst *BI = dyn_cast<BranchInst>(OrigHeader->getTerminator());
+  if (BI == 0 || BI->isUnconditional())
+    return false;
+  
   // If the loop header is not one of the loop exiting blocks then
   // either this loop is already rotated or it is not
   // suitable for loop rotation transformations.
   if (!L->isLoopExiting(OrigHeader))
     return false;
 
-  BranchInst *BI = dyn_cast<BranchInst>(OrigHeader->getTerminator());
-  if (!BI)
-    return false;
-  assert(BI->isConditional() && "Branch Instruction is not conditional");
-
   // Updating PHInodes in loops with multiple exits adds complexity. 
   // Keep it simple, and restrict loop rotation to loops with one exit only.
   // In future, lift this restriction and support for multiple exits if
@@ -139,6 +132,9 @@ bool LoopRotate::rotateLoop(Loop *L) {
   }
 
   // Now, this loop is suitable for rotation.
+  BasicBlock *OrigPreHeader = L->getLoopPreheader();
+  BasicBlock *OrigLatch = L->getLoopLatch();
+  assert(OrigPreHeader && OrigLatch && "Loop not in canonical form?");
 
   // Anything ScalarEvolution may know about this loop or the PHI nodes
   // in its header will soon be invalidated.
@@ -300,7 +296,7 @@ bool LoopRotate::rotateLoop(Loop *L) {
   // Also, since this original header only has one predecessor, zap its
   // PHI nodes, which are now trivial.
   FoldSingleEntryPHINodes(OrigHeader);
-
+  
   // TODO: We could just go ahead and merge OrigHeader into its predecessor
   // at this point, if we don't mind updating dominator info.