LLVM API Documentation

LoopSimplify.cpp
Go to the documentation of this file.
00001 //===- LoopSimplify.cpp - Loop Canonicalization Pass ----------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This pass performs several transformations to transform natural loops into a
00011 // simpler form, which makes subsequent analyses and transformations simpler and
00012 // more effective.
00013 //
00014 // Loop pre-header insertion guarantees that there is a single, non-critical
00015 // entry edge from outside of the loop to the loop header.  This simplifies a
00016 // number of analyses and transformations, such as LICM.
00017 //
00018 // Loop exit-block insertion guarantees that all exit blocks from the loop
00019 // (blocks which are outside of the loop that have predecessors inside of the
00020 // loop) only have predecessors from inside of the loop (and are thus dominated
00021 // by the loop header).  This simplifies transformations such as store-sinking
00022 // that are built into LICM.
00023 //
00024 // This pass also guarantees that loops will have exactly one backedge.
00025 //
00026 // Indirectbr instructions introduce several complications. If the loop
00027 // contains or is entered by an indirectbr instruction, it may not be possible
00028 // to transform the loop and make these guarantees. Client code should check
00029 // that these conditions are true before relying on them.
00030 //
00031 // Note that the simplifycfg pass will clean up blocks which are split out but
00032 // end up being unnecessary, so usage of this pass should not pessimize
00033 // generated code.
00034 //
00035 // This pass obviously modifies the CFG, but updates loop information and
00036 // dominator information.
00037 //
00038 //===----------------------------------------------------------------------===//
00039 
00040 #define DEBUG_TYPE "loop-simplify"
00041 #include "llvm/Transforms/Scalar.h"
00042 #include "llvm/ADT/DepthFirstIterator.h"
00043 #include "llvm/ADT/SetOperations.h"
00044 #include "llvm/ADT/SetVector.h"
00045 #include "llvm/ADT/Statistic.h"
00046 #include "llvm/Analysis/AliasAnalysis.h"
00047 #include "llvm/Analysis/DependenceAnalysis.h"
00048 #include "llvm/Analysis/Dominators.h"
00049 #include "llvm/Analysis/InstructionSimplify.h"
00050 #include "llvm/Analysis/LoopPass.h"
00051 #include "llvm/Analysis/ScalarEvolution.h"
00052 #include "llvm/IR/Constants.h"
00053 #include "llvm/IR/Function.h"
00054 #include "llvm/IR/Instructions.h"
00055 #include "llvm/IR/IntrinsicInst.h"
00056 #include "llvm/IR/LLVMContext.h"
00057 #include "llvm/IR/Type.h"
00058 #include "llvm/Support/CFG.h"
00059 #include "llvm/Support/Debug.h"
00060 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
00061 #include "llvm/Transforms/Utils/Local.h"
00062 using namespace llvm;
00063 
00064 STATISTIC(NumInserted, "Number of pre-header or exit blocks inserted");
00065 STATISTIC(NumNested  , "Number of nested loops split out");
00066 
00067 namespace {
00068   struct LoopSimplify : public LoopPass {
00069     static char ID; // Pass identification, replacement for typeid
00070     LoopSimplify() : LoopPass(ID) {
00071       initializeLoopSimplifyPass(*PassRegistry::getPassRegistry());
00072     }
00073 
00074     // AA - If we have an alias analysis object to update, this is it, otherwise
00075     // this is null.
00076     AliasAnalysis *AA;
00077     LoopInfo *LI;
00078     DominatorTree *DT;
00079     ScalarEvolution *SE;
00080     Loop *L;
00081     virtual bool runOnLoop(Loop *L, LPPassManager &LPM);
00082 
00083     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00084       // We need loop information to identify the loops...
00085       AU.addRequired<DominatorTree>();
00086       AU.addPreserved<DominatorTree>();
00087 
00088       AU.addRequired<LoopInfo>();
00089       AU.addPreserved<LoopInfo>();
00090 
00091       AU.addPreserved<AliasAnalysis>();
00092       AU.addPreserved<ScalarEvolution>();
00093       AU.addPreserved<DependenceAnalysis>();
00094       AU.addPreservedID(BreakCriticalEdgesID);  // No critical edges added.
00095     }
00096 
00097     /// verifyAnalysis() - Verify LoopSimplifyForm's guarantees.
00098     void verifyAnalysis() const;
00099 
00100   private:
00101     bool ProcessLoop(Loop *L, LPPassManager &LPM);
00102     BasicBlock *RewriteLoopExitBlock(Loop *L, BasicBlock *Exit);
00103     BasicBlock *InsertPreheaderForLoop(Loop *L);
00104     Loop *SeparateNestedLoop(Loop *L, LPPassManager &LPM,
00105                              BasicBlock *Preheader);
00106     BasicBlock *InsertUniqueBackedgeBlock(Loop *L, BasicBlock *Preheader);
00107     void PlaceSplitBlockCarefully(BasicBlock *NewBB,
00108                                   SmallVectorImpl<BasicBlock*> &SplitPreds,
00109                                   Loop *L);
00110   };
00111 }
00112 
00113 char LoopSimplify::ID = 0;
00114 INITIALIZE_PASS_BEGIN(LoopSimplify, "loop-simplify",
00115                 "Canonicalize natural loops", true, false)
00116 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
00117 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
00118 INITIALIZE_PASS_END(LoopSimplify, "loop-simplify",
00119                 "Canonicalize natural loops", true, false)
00120 
00121 // Publicly exposed interface to pass...
00122 char &llvm::LoopSimplifyID = LoopSimplify::ID;
00123 Pass *llvm::createLoopSimplifyPass() { return new LoopSimplify(); }
00124 
00125 /// runOnLoop - Run down all loops in the CFG (recursively, but we could do
00126 /// it in any convenient order) inserting preheaders...
00127 ///
00128 bool LoopSimplify::runOnLoop(Loop *l, LPPassManager &LPM) {
00129   L = l;
00130   bool Changed = false;
00131   LI = &getAnalysis<LoopInfo>();
00132   AA = getAnalysisIfAvailable<AliasAnalysis>();
00133   DT = &getAnalysis<DominatorTree>();
00134   SE = getAnalysisIfAvailable<ScalarEvolution>();
00135 
00136   Changed |= ProcessLoop(L, LPM);
00137 
00138   return Changed;
00139 }
00140 
00141 /// ProcessLoop - Walk the loop structure in depth first order, ensuring that
00142 /// all loops have preheaders.
00143 ///
00144 bool LoopSimplify::ProcessLoop(Loop *L, LPPassManager &LPM) {
00145   bool Changed = false;
00146 ReprocessLoop:
00147 
00148   // Check to see that no blocks (other than the header) in this loop have
00149   // predecessors that are not in the loop.  This is not valid for natural
00150   // loops, but can occur if the blocks are unreachable.  Since they are
00151   // unreachable we can just shamelessly delete those CFG edges!
00152   for (Loop::block_iterator BB = L->block_begin(), E = L->block_end();
00153        BB != E; ++BB) {
00154     if (*BB == L->getHeader()) continue;
00155 
00156     SmallPtrSet<BasicBlock*, 4> BadPreds;
00157     for (pred_iterator PI = pred_begin(*BB),
00158          PE = pred_end(*BB); PI != PE; ++PI) {
00159       BasicBlock *P = *PI;
00160       if (!L->contains(P))
00161         BadPreds.insert(P);
00162     }
00163 
00164     // Delete each unique out-of-loop (and thus dead) predecessor.
00165     for (SmallPtrSet<BasicBlock*, 4>::iterator I = BadPreds.begin(),
00166          E = BadPreds.end(); I != E; ++I) {
00167 
00168       DEBUG(dbgs() << "LoopSimplify: Deleting edge from dead predecessor "
00169                    << (*I)->getName() << "\n");
00170 
00171       // Inform each successor of each dead pred.
00172       for (succ_iterator SI = succ_begin(*I), SE = succ_end(*I); SI != SE; ++SI)
00173         (*SI)->removePredecessor(*I);
00174       // Zap the dead pred's terminator and replace it with unreachable.
00175       TerminatorInst *TI = (*I)->getTerminator();
00176        TI->replaceAllUsesWith(UndefValue::get(TI->getType()));
00177       (*I)->getTerminator()->eraseFromParent();
00178       new UnreachableInst((*I)->getContext(), *I);
00179       Changed = true;
00180     }
00181   }
00182 
00183   // If there are exiting blocks with branches on undef, resolve the undef in
00184   // the direction which will exit the loop. This will help simplify loop
00185   // trip count computations.
00186   SmallVector<BasicBlock*, 8> ExitingBlocks;
00187   L->getExitingBlocks(ExitingBlocks);
00188   for (SmallVectorImpl<BasicBlock *>::iterator I = ExitingBlocks.begin(),
00189        E = ExitingBlocks.end(); I != E; ++I)
00190     if (BranchInst *BI = dyn_cast<BranchInst>((*I)->getTerminator()))
00191       if (BI->isConditional()) {
00192         if (UndefValue *Cond = dyn_cast<UndefValue>(BI->getCondition())) {
00193 
00194           DEBUG(dbgs() << "LoopSimplify: Resolving \"br i1 undef\" to exit in "
00195                        << (*I)->getName() << "\n");
00196 
00197           BI->setCondition(ConstantInt::get(Cond->getType(),
00198                                             !L->contains(BI->getSuccessor(0))));
00199 
00200           // This may make the loop analyzable, force SCEV recomputation.
00201           if (SE)
00202             SE->forgetLoop(L);
00203 
00204           Changed = true;
00205         }
00206       }
00207 
00208   // Does the loop already have a preheader?  If so, don't insert one.
00209   BasicBlock *Preheader = L->getLoopPreheader();
00210   if (!Preheader) {
00211     Preheader = InsertPreheaderForLoop(L);
00212     if (Preheader) {
00213       ++NumInserted;
00214       Changed = true;
00215     }
00216   }
00217 
00218   // Next, check to make sure that all exit nodes of the loop only have
00219   // predecessors that are inside of the loop.  This check guarantees that the
00220   // loop preheader/header will dominate the exit blocks.  If the exit block has
00221   // predecessors from outside of the loop, split the edge now.
00222   SmallVector<BasicBlock*, 8> ExitBlocks;
00223   L->getExitBlocks(ExitBlocks);
00224 
00225   SmallSetVector<BasicBlock *, 8> ExitBlockSet(ExitBlocks.begin(),
00226                                                ExitBlocks.end());
00227   for (SmallSetVector<BasicBlock *, 8>::iterator I = ExitBlockSet.begin(),
00228          E = ExitBlockSet.end(); I != E; ++I) {
00229     BasicBlock *ExitBlock = *I;
00230     for (pred_iterator PI = pred_begin(ExitBlock), PE = pred_end(ExitBlock);
00231          PI != PE; ++PI)
00232       // Must be exactly this loop: no subloops, parent loops, or non-loop preds
00233       // allowed.
00234       if (!L->contains(*PI)) {
00235         if (RewriteLoopExitBlock(L, ExitBlock)) {
00236           ++NumInserted;
00237           Changed = true;
00238         }
00239         break;
00240       }
00241   }
00242 
00243   // If the header has more than two predecessors at this point (from the
00244   // preheader and from multiple backedges), we must adjust the loop.
00245   BasicBlock *LoopLatch = L->getLoopLatch();
00246   if (!LoopLatch) {
00247     // If this is really a nested loop, rip it out into a child loop.  Don't do
00248     // this for loops with a giant number of backedges, just factor them into a
00249     // common backedge instead.
00250     if (L->getNumBackEdges() < 8) {
00251       if (SeparateNestedLoop(L, LPM, Preheader)) {
00252         ++NumNested;
00253         // This is a big restructuring change, reprocess the whole loop.
00254         Changed = true;
00255         // GCC doesn't tail recursion eliminate this.
00256         goto ReprocessLoop;
00257       }
00258     }
00259 
00260     // If we either couldn't, or didn't want to, identify nesting of the loops,
00261     // insert a new block that all backedges target, then make it jump to the
00262     // loop header.
00263     LoopLatch = InsertUniqueBackedgeBlock(L, Preheader);
00264     if (LoopLatch) {
00265       ++NumInserted;
00266       Changed = true;
00267     }
00268   }
00269 
00270   // Scan over the PHI nodes in the loop header.  Since they now have only two
00271   // incoming values (the loop is canonicalized), we may have simplified the PHI
00272   // down to 'X = phi [X, Y]', which should be replaced with 'Y'.
00273   PHINode *PN;
00274   for (BasicBlock::iterator I = L->getHeader()->begin();
00275        (PN = dyn_cast<PHINode>(I++)); )
00276     if (Value *V = SimplifyInstruction(PN, 0, 0, DT)) {
00277       if (AA) AA->deleteValue(PN);
00278       if (SE) SE->forgetValue(PN);
00279       PN->replaceAllUsesWith(V);
00280       PN->eraseFromParent();
00281     }
00282 
00283   // If this loop has multiple exits and the exits all go to the same
00284   // block, attempt to merge the exits. This helps several passes, such
00285   // as LoopRotation, which do not support loops with multiple exits.
00286   // SimplifyCFG also does this (and this code uses the same utility
00287   // function), however this code is loop-aware, where SimplifyCFG is
00288   // not. That gives it the advantage of being able to hoist
00289   // loop-invariant instructions out of the way to open up more
00290   // opportunities, and the disadvantage of having the responsibility
00291   // to preserve dominator information.
00292   bool UniqueExit = true;
00293   if (!ExitBlocks.empty())
00294     for (unsigned i = 1, e = ExitBlocks.size(); i != e; ++i)
00295       if (ExitBlocks[i] != ExitBlocks[0]) {
00296         UniqueExit = false;
00297         break;
00298       }
00299   if (UniqueExit) {
00300     for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
00301       BasicBlock *ExitingBlock = ExitingBlocks[i];
00302       if (!ExitingBlock->getSinglePredecessor()) continue;
00303       BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
00304       if (!BI || !BI->isConditional()) continue;
00305       CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition());
00306       if (!CI || CI->getParent() != ExitingBlock) continue;
00307 
00308       // Attempt to hoist out all instructions except for the
00309       // comparison and the branch.
00310       bool AllInvariant = true;
00311       for (BasicBlock::iterator I = ExitingBlock->begin(); &*I != BI; ) {
00312         Instruction *Inst = I++;
00313         // Skip debug info intrinsics.
00314         if (isa<DbgInfoIntrinsic>(Inst))
00315           continue;
00316         if (Inst == CI)
00317           continue;
00318         if (!L->makeLoopInvariant(Inst, Changed,
00319                                   Preheader ? Preheader->getTerminator() : 0)) {
00320           AllInvariant = false;
00321           break;
00322         }
00323       }
00324       if (!AllInvariant) continue;
00325 
00326       // The block has now been cleared of all instructions except for
00327       // a comparison and a conditional branch. SimplifyCFG may be able
00328       // to fold it now.
00329       if (!FoldBranchToCommonDest(BI)) continue;
00330 
00331       // Success. The block is now dead, so remove it from the loop,
00332       // update the dominator tree and delete it.
00333       DEBUG(dbgs() << "LoopSimplify: Eliminating exiting block "
00334                    << ExitingBlock->getName() << "\n");
00335 
00336       // If any reachable control flow within this loop has changed, notify
00337       // ScalarEvolution. Currently assume the parent loop doesn't change
00338       // (spliting edges doesn't count). If blocks, CFG edges, or other values
00339       // in the parent loop change, then we need call to forgetLoop() for the
00340       // parent instead.
00341       if (SE)
00342         SE->forgetLoop(L);
00343 
00344       assert(pred_begin(ExitingBlock) == pred_end(ExitingBlock));
00345       Changed = true;
00346       LI->removeBlock(ExitingBlock);
00347 
00348       DomTreeNode *Node = DT->getNode(ExitingBlock);
00349       const std::vector<DomTreeNodeBase<BasicBlock> *> &Children =
00350         Node->getChildren();
00351       while (!Children.empty()) {
00352         DomTreeNode *Child = Children.front();
00353         DT->changeImmediateDominator(Child, Node->getIDom());
00354       }
00355       DT->eraseNode(ExitingBlock);
00356 
00357       BI->getSuccessor(0)->removePredecessor(ExitingBlock);
00358       BI->getSuccessor(1)->removePredecessor(ExitingBlock);
00359       ExitingBlock->eraseFromParent();
00360     }
00361   }
00362 
00363   return Changed;
00364 }
00365 
00366 /// InsertPreheaderForLoop - Once we discover that a loop doesn't have a
00367 /// preheader, this method is called to insert one.  This method has two phases:
00368 /// preheader insertion and analysis updating.
00369 ///
00370 BasicBlock *LoopSimplify::InsertPreheaderForLoop(Loop *L) {
00371   BasicBlock *Header = L->getHeader();
00372 
00373   // Compute the set of predecessors of the loop that are not in the loop.
00374   SmallVector<BasicBlock*, 8> OutsideBlocks;
00375   for (pred_iterator PI = pred_begin(Header), PE = pred_end(Header);
00376        PI != PE; ++PI) {
00377     BasicBlock *P = *PI;
00378     if (!L->contains(P)) {         // Coming in from outside the loop?
00379       // If the loop is branched to from an indirect branch, we won't
00380       // be able to fully transform the loop, because it prohibits
00381       // edge splitting.
00382       if (isa<IndirectBrInst>(P->getTerminator())) return 0;
00383 
00384       // Keep track of it.
00385       OutsideBlocks.push_back(P);
00386     }
00387   }
00388 
00389   // Split out the loop pre-header.
00390   BasicBlock *PreheaderBB;
00391   if (!Header->isLandingPad()) {
00392     PreheaderBB = SplitBlockPredecessors(Header, OutsideBlocks, ".preheader",
00393                                          this);
00394   } else {
00395     SmallVector<BasicBlock*, 2> NewBBs;
00396     SplitLandingPadPredecessors(Header, OutsideBlocks, ".preheader",
00397                                 ".split-lp", this, NewBBs);
00398     PreheaderBB = NewBBs[0];
00399   }
00400 
00401   PreheaderBB->getTerminator()->setDebugLoc(
00402                                       Header->getFirstNonPHI()->getDebugLoc());
00403   DEBUG(dbgs() << "LoopSimplify: Creating pre-header "
00404                << PreheaderBB->getName() << "\n");
00405 
00406   // Make sure that NewBB is put someplace intelligent, which doesn't mess up
00407   // code layout too horribly.
00408   PlaceSplitBlockCarefully(PreheaderBB, OutsideBlocks, L);
00409 
00410   return PreheaderBB;
00411 }
00412 
00413 /// RewriteLoopExitBlock - Ensure that the loop preheader dominates all exit
00414 /// blocks.  This method is used to split exit blocks that have predecessors
00415 /// outside of the loop.
00416 BasicBlock *LoopSimplify::RewriteLoopExitBlock(Loop *L, BasicBlock *Exit) {
00417   SmallVector<BasicBlock*, 8> LoopBlocks;
00418   for (pred_iterator I = pred_begin(Exit), E = pred_end(Exit); I != E; ++I) {
00419     BasicBlock *P = *I;
00420     if (L->contains(P)) {
00421       // Don't do this if the loop is exited via an indirect branch.
00422       if (isa<IndirectBrInst>(P->getTerminator())) return 0;
00423 
00424       LoopBlocks.push_back(P);
00425     }
00426   }
00427 
00428   assert(!LoopBlocks.empty() && "No edges coming in from outside the loop?");
00429   BasicBlock *NewExitBB = 0;
00430 
00431   if (Exit->isLandingPad()) {
00432     SmallVector<BasicBlock*, 2> NewBBs;
00433     SplitLandingPadPredecessors(Exit, ArrayRef<BasicBlock*>(&LoopBlocks[0],
00434                                                             LoopBlocks.size()),
00435                                 ".loopexit", ".nonloopexit",
00436                                 this, NewBBs);
00437     NewExitBB = NewBBs[0];
00438   } else {
00439     NewExitBB = SplitBlockPredecessors(Exit, LoopBlocks, ".loopexit", this);
00440   }
00441 
00442   DEBUG(dbgs() << "LoopSimplify: Creating dedicated exit block "
00443                << NewExitBB->getName() << "\n");
00444   return NewExitBB;
00445 }
00446 
00447 /// AddBlockAndPredsToSet - Add the specified block, and all of its
00448 /// predecessors, to the specified set, if it's not already in there.  Stop
00449 /// predecessor traversal when we reach StopBlock.
00450 static void AddBlockAndPredsToSet(BasicBlock *InputBB, BasicBlock *StopBlock,
00451                                   std::set<BasicBlock*> &Blocks) {
00452   std::vector<BasicBlock *> WorkList;
00453   WorkList.push_back(InputBB);
00454   do {
00455     BasicBlock *BB = WorkList.back(); WorkList.pop_back();
00456     if (Blocks.insert(BB).second && BB != StopBlock)
00457       // If BB is not already processed and it is not a stop block then
00458       // insert its predecessor in the work list
00459       for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
00460         BasicBlock *WBB = *I;
00461         WorkList.push_back(WBB);
00462       }
00463   } while(!WorkList.empty());
00464 }
00465 
00466 /// FindPHIToPartitionLoops - The first part of loop-nestification is to find a
00467 /// PHI node that tells us how to partition the loops.
00468 static PHINode *FindPHIToPartitionLoops(Loop *L, DominatorTree *DT,
00469                                         AliasAnalysis *AA, LoopInfo *LI) {
00470   for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ) {
00471     PHINode *PN = cast<PHINode>(I);
00472     ++I;
00473     if (Value *V = SimplifyInstruction(PN, 0, 0, DT)) {
00474       // This is a degenerate PHI already, don't modify it!
00475       PN->replaceAllUsesWith(V);
00476       if (AA) AA->deleteValue(PN);
00477       PN->eraseFromParent();
00478       continue;
00479     }
00480 
00481     // Scan this PHI node looking for a use of the PHI node by itself.
00482     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
00483       if (PN->getIncomingValue(i) == PN &&
00484           L->contains(PN->getIncomingBlock(i)))
00485         // We found something tasty to remove.
00486         return PN;
00487   }
00488   return 0;
00489 }
00490 
00491 // PlaceSplitBlockCarefully - If the block isn't already, move the new block to
00492 // right after some 'outside block' block.  This prevents the preheader from
00493 // being placed inside the loop body, e.g. when the loop hasn't been rotated.
00494 void LoopSimplify::PlaceSplitBlockCarefully(BasicBlock *NewBB,
00495                                        SmallVectorImpl<BasicBlock*> &SplitPreds,
00496                                             Loop *L) {
00497   // Check to see if NewBB is already well placed.
00498   Function::iterator BBI = NewBB; --BBI;
00499   for (unsigned i = 0, e = SplitPreds.size(); i != e; ++i) {
00500     if (&*BBI == SplitPreds[i])
00501       return;
00502   }
00503 
00504   // If it isn't already after an outside block, move it after one.  This is
00505   // always good as it makes the uncond branch from the outside block into a
00506   // fall-through.
00507 
00508   // Figure out *which* outside block to put this after.  Prefer an outside
00509   // block that neighbors a BB actually in the loop.
00510   BasicBlock *FoundBB = 0;
00511   for (unsigned i = 0, e = SplitPreds.size(); i != e; ++i) {
00512     Function::iterator BBI = SplitPreds[i];
00513     if (++BBI != NewBB->getParent()->end() &&
00514         L->contains(BBI)) {
00515       FoundBB = SplitPreds[i];
00516       break;
00517     }
00518   }
00519 
00520   // If our heuristic for a *good* bb to place this after doesn't find
00521   // anything, just pick something.  It's likely better than leaving it within
00522   // the loop.
00523   if (!FoundBB)
00524     FoundBB = SplitPreds[0];
00525   NewBB->moveAfter(FoundBB);
00526 }
00527 
00528 
00529 /// SeparateNestedLoop - If this loop has multiple backedges, try to pull one of
00530 /// them out into a nested loop.  This is important for code that looks like
00531 /// this:
00532 ///
00533 ///  Loop:
00534 ///     ...
00535 ///     br cond, Loop, Next
00536 ///     ...
00537 ///     br cond2, Loop, Out
00538 ///
00539 /// To identify this common case, we look at the PHI nodes in the header of the
00540 /// loop.  PHI nodes with unchanging values on one backedge correspond to values
00541 /// that change in the "outer" loop, but not in the "inner" loop.
00542 ///
00543 /// If we are able to separate out a loop, return the new outer loop that was
00544 /// created.
00545 ///
00546 Loop *LoopSimplify::SeparateNestedLoop(Loop *L, LPPassManager &LPM,
00547                                        BasicBlock *Preheader) {
00548   // Don't try to separate loops without a preheader.
00549   if (!Preheader)
00550     return 0;
00551 
00552   // The header is not a landing pad; preheader insertion should ensure this.
00553   assert(!L->getHeader()->isLandingPad() &&
00554          "Can't insert backedge to landing pad");
00555 
00556   PHINode *PN = FindPHIToPartitionLoops(L, DT, AA, LI);
00557   if (PN == 0) return 0;  // No known way to partition.
00558 
00559   // Pull out all predecessors that have varying values in the loop.  This
00560   // handles the case when a PHI node has multiple instances of itself as
00561   // arguments.
00562   SmallVector<BasicBlock*, 8> OuterLoopPreds;
00563   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
00564     if (PN->getIncomingValue(i) != PN ||
00565         !L->contains(PN->getIncomingBlock(i))) {
00566       // We can't split indirectbr edges.
00567       if (isa<IndirectBrInst>(PN->getIncomingBlock(i)->getTerminator()))
00568         return 0;
00569       OuterLoopPreds.push_back(PN->getIncomingBlock(i));
00570     }
00571   }
00572   DEBUG(dbgs() << "LoopSimplify: Splitting out a new outer loop\n");
00573 
00574   // If ScalarEvolution is around and knows anything about values in
00575   // this loop, tell it to forget them, because we're about to
00576   // substantially change it.
00577   if (SE)
00578     SE->forgetLoop(L);
00579 
00580   BasicBlock *Header = L->getHeader();
00581   BasicBlock *NewBB =
00582     SplitBlockPredecessors(Header, OuterLoopPreds,  ".outer", this);
00583 
00584   // Make sure that NewBB is put someplace intelligent, which doesn't mess up
00585   // code layout too horribly.
00586   PlaceSplitBlockCarefully(NewBB, OuterLoopPreds, L);
00587 
00588   // Create the new outer loop.
00589   Loop *NewOuter = new Loop();
00590 
00591   // Change the parent loop to use the outer loop as its child now.
00592   if (Loop *Parent = L->getParentLoop())
00593     Parent->replaceChildLoopWith(L, NewOuter);
00594   else
00595     LI->changeTopLevelLoop(L, NewOuter);
00596 
00597   // L is now a subloop of our outer loop.
00598   NewOuter->addChildLoop(L);
00599 
00600   // Add the new loop to the pass manager queue.
00601   LPM.insertLoopIntoQueue(NewOuter);
00602 
00603   for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
00604        I != E; ++I)
00605     NewOuter->addBlockEntry(*I);
00606 
00607   // Now reset the header in L, which had been moved by
00608   // SplitBlockPredecessors for the outer loop.
00609   L->moveToHeader(Header);
00610 
00611   // Determine which blocks should stay in L and which should be moved out to
00612   // the Outer loop now.
00613   std::set<BasicBlock*> BlocksInL;
00614   for (pred_iterator PI=pred_begin(Header), E = pred_end(Header); PI!=E; ++PI) {
00615     BasicBlock *P = *PI;
00616     if (DT->dominates(Header, P))
00617       AddBlockAndPredsToSet(P, Header, BlocksInL);
00618   }
00619 
00620   // Scan all of the loop children of L, moving them to OuterLoop if they are
00621   // not part of the inner loop.
00622   const std::vector<Loop*> &SubLoops = L->getSubLoops();
00623   for (size_t I = 0; I != SubLoops.size(); )
00624     if (BlocksInL.count(SubLoops[I]->getHeader()))
00625       ++I;   // Loop remains in L
00626     else
00627       NewOuter->addChildLoop(L->removeChildLoop(SubLoops.begin() + I));
00628 
00629   // Now that we know which blocks are in L and which need to be moved to
00630   // OuterLoop, move any blocks that need it.
00631   for (unsigned i = 0; i != L->getBlocks().size(); ++i) {
00632     BasicBlock *BB = L->getBlocks()[i];
00633     if (!BlocksInL.count(BB)) {
00634       // Move this block to the parent, updating the exit blocks sets
00635       L->removeBlockFromLoop(BB);
00636       if ((*LI)[BB] == L)
00637         LI->changeLoopFor(BB, NewOuter);
00638       --i;
00639     }
00640   }
00641 
00642   return NewOuter;
00643 }
00644 
00645 
00646 
00647 /// InsertUniqueBackedgeBlock - This method is called when the specified loop
00648 /// has more than one backedge in it.  If this occurs, revector all of these
00649 /// backedges to target a new basic block and have that block branch to the loop
00650 /// header.  This ensures that loops have exactly one backedge.
00651 ///
00652 BasicBlock *
00653 LoopSimplify::InsertUniqueBackedgeBlock(Loop *L, BasicBlock *Preheader) {
00654   assert(L->getNumBackEdges() > 1 && "Must have > 1 backedge!");
00655 
00656   // Get information about the loop
00657   BasicBlock *Header = L->getHeader();
00658   Function *F = Header->getParent();
00659 
00660   // Unique backedge insertion currently depends on having a preheader.
00661   if (!Preheader)
00662     return 0;
00663 
00664   // The header is not a landing pad; preheader insertion should ensure this.
00665   assert(!Header->isLandingPad() && "Can't insert backedge to landing pad");
00666 
00667   // Figure out which basic blocks contain back-edges to the loop header.
00668   std::vector<BasicBlock*> BackedgeBlocks;
00669   for (pred_iterator I = pred_begin(Header), E = pred_end(Header); I != E; ++I){
00670     BasicBlock *P = *I;
00671 
00672     // Indirectbr edges cannot be split, so we must fail if we find one.
00673     if (isa<IndirectBrInst>(P->getTerminator()))
00674       return 0;
00675 
00676     if (P != Preheader) BackedgeBlocks.push_back(P);
00677   }
00678 
00679   // Create and insert the new backedge block...
00680   BasicBlock *BEBlock = BasicBlock::Create(Header->getContext(),
00681                                            Header->getName()+".backedge", F);
00682   BranchInst *BETerminator = BranchInst::Create(Header, BEBlock);
00683 
00684   DEBUG(dbgs() << "LoopSimplify: Inserting unique backedge block "
00685                << BEBlock->getName() << "\n");
00686 
00687   // Move the new backedge block to right after the last backedge block.
00688   Function::iterator InsertPos = BackedgeBlocks.back(); ++InsertPos;
00689   F->getBasicBlockList().splice(InsertPos, F->getBasicBlockList(), BEBlock);
00690 
00691   // Now that the block has been inserted into the function, create PHI nodes in
00692   // the backedge block which correspond to any PHI nodes in the header block.
00693   for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
00694     PHINode *PN = cast<PHINode>(I);
00695     PHINode *NewPN = PHINode::Create(PN->getType(), BackedgeBlocks.size(),
00696                                      PN->getName()+".be", BETerminator);
00697     if (AA) AA->copyValue(PN, NewPN);
00698 
00699     // Loop over the PHI node, moving all entries except the one for the
00700     // preheader over to the new PHI node.
00701     unsigned PreheaderIdx = ~0U;
00702     bool HasUniqueIncomingValue = true;
00703     Value *UniqueValue = 0;
00704     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
00705       BasicBlock *IBB = PN->getIncomingBlock(i);
00706       Value *IV = PN->getIncomingValue(i);
00707       if (IBB == Preheader) {
00708         PreheaderIdx = i;
00709       } else {
00710         NewPN->addIncoming(IV, IBB);
00711         if (HasUniqueIncomingValue) {
00712           if (UniqueValue == 0)
00713             UniqueValue = IV;
00714           else if (UniqueValue != IV)
00715             HasUniqueIncomingValue = false;
00716         }
00717       }
00718     }
00719 
00720     // Delete all of the incoming values from the old PN except the preheader's
00721     assert(PreheaderIdx != ~0U && "PHI has no preheader entry??");
00722     if (PreheaderIdx != 0) {
00723       PN->setIncomingValue(0, PN->getIncomingValue(PreheaderIdx));
00724       PN->setIncomingBlock(0, PN->getIncomingBlock(PreheaderIdx));
00725     }
00726     // Nuke all entries except the zero'th.
00727     for (unsigned i = 0, e = PN->getNumIncomingValues()-1; i != e; ++i)
00728       PN->removeIncomingValue(e-i, false);
00729 
00730     // Finally, add the newly constructed PHI node as the entry for the BEBlock.
00731     PN->addIncoming(NewPN, BEBlock);
00732 
00733     // As an optimization, if all incoming values in the new PhiNode (which is a
00734     // subset of the incoming values of the old PHI node) have the same value,
00735     // eliminate the PHI Node.
00736     if (HasUniqueIncomingValue) {
00737       NewPN->replaceAllUsesWith(UniqueValue);
00738       if (AA) AA->deleteValue(NewPN);
00739       BEBlock->getInstList().erase(NewPN);
00740     }
00741   }
00742 
00743   // Now that all of the PHI nodes have been inserted and adjusted, modify the
00744   // backedge blocks to just to the BEBlock instead of the header.
00745   for (unsigned i = 0, e = BackedgeBlocks.size(); i != e; ++i) {
00746     TerminatorInst *TI = BackedgeBlocks[i]->getTerminator();
00747     for (unsigned Op = 0, e = TI->getNumSuccessors(); Op != e; ++Op)
00748       if (TI->getSuccessor(Op) == Header)
00749         TI->setSuccessor(Op, BEBlock);
00750   }
00751 
00752   //===--- Update all analyses which we must preserve now -----------------===//
00753 
00754   // Update Loop Information - we know that this block is now in the current
00755   // loop and all parent loops.
00756   L->addBasicBlockToLoop(BEBlock, LI->getBase());
00757 
00758   // Update dominator information
00759   DT->splitBlock(BEBlock);
00760 
00761   return BEBlock;
00762 }
00763 
00764 void LoopSimplify::verifyAnalysis() const {
00765   // It used to be possible to just assert L->isLoopSimplifyForm(), however
00766   // with the introduction of indirectbr, there are now cases where it's
00767   // not possible to transform a loop as necessary. We can at least check
00768   // that there is an indirectbr near any time there's trouble.
00769 
00770   // Indirectbr can interfere with preheader and unique backedge insertion.
00771   if (!L->getLoopPreheader() || !L->getLoopLatch()) {
00772     bool HasIndBrPred = false;
00773     for (pred_iterator PI = pred_begin(L->getHeader()),
00774          PE = pred_end(L->getHeader()); PI != PE; ++PI)
00775       if (isa<IndirectBrInst>((*PI)->getTerminator())) {
00776         HasIndBrPred = true;
00777         break;
00778       }
00779     assert(HasIndBrPred &&
00780            "LoopSimplify has no excuse for missing loop header info!");
00781     (void)HasIndBrPred;
00782   }
00783 
00784   // Indirectbr can interfere with exit block canonicalization.
00785   if (!L->hasDedicatedExits()) {
00786     bool HasIndBrExiting = false;
00787     SmallVector<BasicBlock*, 8> ExitingBlocks;
00788     L->getExitingBlocks(ExitingBlocks);
00789     for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
00790       if (isa<IndirectBrInst>((ExitingBlocks[i])->getTerminator())) {
00791         HasIndBrExiting = true;
00792         break;
00793       }
00794     }
00795 
00796     assert(HasIndBrExiting &&
00797            "LoopSimplify has no excuse for missing exit block info!");
00798     (void)HasIndBrExiting;
00799   }
00800 }