LLVM API Documentation

LoopPass.cpp

Go to the documentation of this file.
00001 //===- LoopPass.cpp - Loop Pass and Loop Pass Manager ---------------------===//
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 file implements LoopPass and LPPassManager. All loop optimization
00011 // and transformation passes are derived from LoopPass. LPPassManager is
00012 // responsible for managing LoopPasses.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #include "llvm/Analysis/LoopPass.h"
00017 #include "llvm/Analysis/ScalarEvolution.h"
00018 using namespace llvm;
00019 
00020 //===----------------------------------------------------------------------===//
00021 // LPPassManager
00022 //
00023 
00024 char LPPassManager::ID = 0;
00025 /// LPPassManager manages FPPassManagers and CalLGraphSCCPasses.
00026 
00027 LPPassManager::LPPassManager(int Depth) 
00028   : FunctionPass(&ID), PMDataManager(Depth) { 
00029   skipThisLoop = false;
00030   redoThisLoop = false;
00031   LI = NULL;
00032   CurrentLoop = NULL;
00033 }
00034 
00035 /// Delete loop from the loop queue and loop hierarchy (LoopInfo). 
00036 void LPPassManager::deleteLoopFromQueue(Loop *L) {
00037 
00038   if (Loop *ParentLoop = L->getParentLoop()) { // Not a top-level loop.
00039     // Reparent all of the blocks in this loop.  Since BBLoop had a parent,
00040     // they are now all in it.
00041     for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); 
00042          I != E; ++I)
00043       if (LI->getLoopFor(*I) == L)    // Don't change blocks in subloops.
00044         LI->changeLoopFor(*I, ParentLoop);
00045     
00046     // Remove the loop from its parent loop.
00047     for (Loop::iterator I = ParentLoop->begin(), E = ParentLoop->end();;
00048          ++I) {
00049       assert(I != E && "Couldn't find loop");
00050       if (*I == L) {
00051         ParentLoop->removeChildLoop(I);
00052         break;
00053       }
00054     }
00055     
00056     // Move all subloops into the parent loop.
00057     while (!L->empty())
00058       ParentLoop->addChildLoop(L->removeChildLoop(L->end()-1));
00059   } else {
00060     // Reparent all of the blocks in this loop.  Since BBLoop had no parent,
00061     // they no longer in a loop at all.
00062     
00063     for (unsigned i = 0; i != L->getBlocks().size(); ++i) {
00064       // Don't change blocks in subloops.
00065       if (LI->getLoopFor(L->getBlocks()[i]) == L) {
00066         LI->removeBlock(L->getBlocks()[i]);
00067         --i;
00068       }
00069     }
00070 
00071     // Remove the loop from the top-level LoopInfo object.
00072     for (LoopInfo::iterator I = LI->begin(), E = LI->end();; ++I) {
00073       assert(I != E && "Couldn't find loop");
00074       if (*I == L) {
00075         LI->removeLoop(I);
00076         break;
00077       }
00078     }
00079 
00080     // Move all of the subloops to the top-level.
00081     while (!L->empty())
00082       LI->addTopLevelLoop(L->removeChildLoop(L->end()-1));
00083   }
00084 
00085   delete L;
00086 
00087   // If L is current loop then skip rest of the passes and let
00088   // runOnFunction remove L from LQ. Otherwise, remove L from LQ now
00089   // and continue applying other passes on CurrentLoop.
00090   if (CurrentLoop == L) {
00091     skipThisLoop = true;
00092     return;
00093   }
00094 
00095   for (std::deque<Loop *>::iterator I = LQ.begin(),
00096          E = LQ.end(); I != E; ++I) {
00097     if (*I == L) {
00098       LQ.erase(I);
00099       break;
00100     }
00101   }
00102 }
00103 
00104 // Inset loop into loop nest (LoopInfo) and loop queue (LQ).
00105 void LPPassManager::insertLoop(Loop *L, Loop *ParentLoop) {
00106 
00107   assert (CurrentLoop != L && "Cannot insert CurrentLoop");
00108 
00109   // Insert into loop nest
00110   if (ParentLoop)
00111     ParentLoop->addChildLoop(L);
00112   else
00113     LI->addTopLevelLoop(L);
00114 
00115   // Insert L into loop queue
00116   if (L == CurrentLoop) 
00117     redoLoop(L);
00118   else if (!ParentLoop)
00119     // This is top level loop. 
00120     LQ.push_front(L);
00121   else {
00122     // Insert L after ParentLoop
00123     for (std::deque<Loop *>::iterator I = LQ.begin(),
00124            E = LQ.end(); I != E; ++I) {
00125       if (*I == ParentLoop) {
00126         // deque does not support insert after.
00127         ++I;
00128         LQ.insert(I, 1, L);
00129         break;
00130       }
00131     }
00132   }
00133 }
00134 
00135 // Reoptimize this loop. LPPassManager will re-insert this loop into the
00136 // queue. This allows LoopPass to change loop nest for the loop. This
00137 // utility may send LPPassManager into infinite loops so use caution.
00138 void LPPassManager::redoLoop(Loop *L) {
00139   assert (CurrentLoop == L && "Can redo only CurrentLoop");
00140   redoThisLoop = true;
00141 }
00142 
00143 /// cloneBasicBlockSimpleAnalysis - Invoke cloneBasicBlockAnalysis hook for
00144 /// all loop passes.
00145 void LPPassManager::cloneBasicBlockSimpleAnalysis(BasicBlock *From, 
00146                                                   BasicBlock *To, Loop *L) {
00147   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {  
00148     Pass *P = getContainedPass(Index);
00149     LoopPass *LP = dynamic_cast<LoopPass *>(P);
00150     LP->cloneBasicBlockAnalysis(From, To, L);
00151   }
00152 }
00153 
00154 /// deleteSimpleAnalysisValue - Invoke deleteAnalysisValue hook for all passes.
00155 void LPPassManager::deleteSimpleAnalysisValue(Value *V, Loop *L) {
00156   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {  
00157     Pass *P = getContainedPass(Index);
00158     LoopPass *LP = dynamic_cast<LoopPass *>(P);
00159     LP->deleteAnalysisValue(V, L);
00160   }
00161 }
00162 
00163 
00164 // Recurse through all subloops and all loops  into LQ.
00165 static void addLoopIntoQueue(Loop *L, std::deque<Loop *> &LQ) {
00166   LQ.push_back(L);
00167   for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
00168     addLoopIntoQueue(*I, LQ);
00169 }
00170 
00171 /// Pass Manager itself does not invalidate any analysis info.
00172 void LPPassManager::getAnalysisUsage(AnalysisUsage &Info) const {
00173   // LPPassManager needs LoopInfo. In the long term LoopInfo class will 
00174   // become part of LPPassManager.
00175   Info.addRequired<LoopInfo>();
00176   // Used by IndVar doInitialization.
00177   Info.addRequired<ScalarEvolution>();
00178   Info.setPreservesAll();
00179 }
00180 
00181 /// run - Execute all of the passes scheduled for execution.  Keep track of
00182 /// whether any of the passes modifies the function, and if so, return true.
00183 bool LPPassManager::runOnFunction(Function &F) {
00184   LI = &getAnalysis<LoopInfo>();
00185   bool Changed = false;
00186 
00187   // Collect inherited analysis from Module level pass manager.
00188   populateInheritedAnalysis(TPM->activeStack);
00189 
00190   // Populate Loop Queue
00191   for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
00192     addLoopIntoQueue(*I, LQ);
00193 
00194   // Initialization
00195   for (std::deque<Loop *>::const_iterator I = LQ.begin(), E = LQ.end();
00196        I != E; ++I) {
00197     Loop *L = *I;
00198     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {  
00199       Pass *P = getContainedPass(Index);
00200       LoopPass *LP = dynamic_cast<LoopPass *>(P);
00201       if (LP)
00202         Changed |= LP->doInitialization(L, *this);
00203     }
00204   }
00205 
00206   // Walk Loops
00207   while (!LQ.empty()) {
00208       
00209     CurrentLoop  = LQ.back();
00210     skipThisLoop = false;
00211     redoThisLoop = false;
00212 
00213     // Run all passes on current SCC
00214     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {  
00215         
00216       Pass *P = getContainedPass(Index);
00217 
00218       dumpPassInfo(P, EXECUTION_MSG, ON_LOOP_MSG, "");
00219       dumpRequiredSet(P);
00220 
00221       initializeAnalysisImpl(P);
00222 
00223       StartPassTimer(P);
00224       LoopPass *LP = dynamic_cast<LoopPass *>(P);
00225       assert (LP && "Invalid LPPassManager member");
00226       Changed |= LP->runOnLoop(CurrentLoop, *this);
00227       StopPassTimer(P);
00228 
00229       if (Changed)
00230         dumpPassInfo(P, MODIFICATION_MSG, ON_LOOP_MSG, "");
00231       dumpPreservedSet(P);
00232 
00233       verifyPreservedAnalysis(LP);
00234       removeNotPreservedAnalysis(P);
00235       recordAvailableAnalysis(P);
00236       removeDeadPasses(P, "", ON_LOOP_MSG);
00237 
00238       // If dominator information is available then verify the info if requested.
00239       verifyDomInfo(*LP, F);
00240 
00241       if (skipThisLoop)
00242         // Do not run other passes on this loop.
00243         break;
00244     }
00245     
00246     // Pop the loop from queue after running all passes.
00247     LQ.pop_back();
00248     
00249     if (redoThisLoop)
00250       LQ.push_back(CurrentLoop);
00251   }
00252   
00253   // Finalization
00254   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
00255     Pass *P = getContainedPass(Index);
00256     LoopPass *LP = dynamic_cast <LoopPass *>(P);
00257     if (LP)
00258       Changed |= LP->doFinalization();
00259   }
00260 
00261   return Changed;
00262 }
00263 
00264 
00265 //===----------------------------------------------------------------------===//
00266 // LoopPass
00267 
00268 // Check if this pass is suitable for the current LPPassManager, if
00269 // available. This pass P is not suitable for a LPPassManager if P
00270 // is not preserving higher level analysis info used by other
00271 // LPPassManager passes. In such case, pop LPPassManager from the
00272 // stack. This will force assignPassManager() to create new
00273 // LPPassManger as expected.
00274 void LoopPass::preparePassManager(PMStack &PMS) {
00275 
00276   // Find LPPassManager 
00277   while (!PMS.empty() &&
00278          PMS.top()->getPassManagerType() > PMT_LoopPassManager)
00279     PMS.pop();
00280 
00281   LPPassManager *LPPM = dynamic_cast<LPPassManager *>(PMS.top());
00282 
00283   // If this pass is destroying high level information that is used
00284   // by other passes that are managed by LPM then do not insert
00285   // this pass in current LPM. Use new LPPassManager.
00286   if (LPPM && !LPPM->preserveHigherLevelAnalysis(this)) 
00287     PMS.pop();
00288 }
00289 
00290 /// Assign pass manager to manage this pass.
00291 void LoopPass::assignPassManager(PMStack &PMS,
00292                                  PassManagerType PreferredType) {
00293   // Find LPPassManager 
00294   while (!PMS.empty() &&
00295          PMS.top()->getPassManagerType() > PMT_LoopPassManager)
00296     PMS.pop();
00297 
00298   LPPassManager *LPPM = dynamic_cast<LPPassManager *>(PMS.top());
00299 
00300   // Create new Loop Pass Manager if it does not exist. 
00301   if (!LPPM) {
00302 
00303     assert (!PMS.empty() && "Unable to create Loop Pass Manager");
00304     PMDataManager *PMD = PMS.top();
00305 
00306     // [1] Create new Call Graph Pass Manager
00307     LPPM = new LPPassManager(PMD->getDepth() + 1);
00308     LPPM->populateInheritedAnalysis(PMS);
00309 
00310     // [2] Set up new manager's top level manager
00311     PMTopLevelManager *TPM = PMD->getTopLevelManager();
00312     TPM->addIndirectPassManager(LPPM);
00313 
00314     // [3] Assign manager to manage this new manager. This may create
00315     // and push new managers into PMS
00316     Pass *P = dynamic_cast<Pass *>(LPPM);
00317     TPM->schedulePass(P);
00318 
00319     // [4] Push new manager into PMS
00320     PMS.push(LPPM);
00321   }
00322 
00323   LPPM->add(this);
00324 }