LLVM API Documentation

RegionPass.cpp
Go to the documentation of this file.
00001 //===- RegionPass.cpp - Region Pass and Region 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 RegionPass and RGPassManager. All region optimization
00011 // and transformation passes are derived from RegionPass. RGPassManager is
00012 // responsible for managing RegionPasses.
00013 // most of these codes are COPY from LoopPass.cpp
00014 //
00015 //===----------------------------------------------------------------------===//
00016 #include "llvm/Analysis/RegionPass.h"
00017 #include "llvm/Analysis/RegionIterator.h"
00018 #include "llvm/Support/Timer.h"
00019 
00020 #define DEBUG_TYPE "regionpassmgr"
00021 #include "llvm/Support/Debug.h"
00022 using namespace llvm;
00023 
00024 //===----------------------------------------------------------------------===//
00025 // RGPassManager
00026 //
00027 
00028 char RGPassManager::ID = 0;
00029 
00030 RGPassManager::RGPassManager()
00031   : FunctionPass(ID), PMDataManager() {
00032   skipThisRegion = false;
00033   redoThisRegion = false;
00034   RI = NULL;
00035   CurrentRegion = NULL;
00036 }
00037 
00038 // Recurse through all subregions and all regions  into RQ.
00039 static void addRegionIntoQueue(Region *R, std::deque<Region *> &RQ) {
00040   RQ.push_back(R);
00041   for (Region::iterator I = R->begin(), E = R->end(); I != E; ++I)
00042     addRegionIntoQueue(*I, RQ);
00043 }
00044 
00045 /// Pass Manager itself does not invalidate any analysis info.
00046 void RGPassManager::getAnalysisUsage(AnalysisUsage &Info) const {
00047   Info.addRequired<RegionInfo>();
00048   Info.setPreservesAll();
00049 }
00050 
00051 /// run - Execute all of the passes scheduled for execution.  Keep track of
00052 /// whether any of the passes modifies the function, and if so, return true.
00053 bool RGPassManager::runOnFunction(Function &F) {
00054   RI = &getAnalysis<RegionInfo>();
00055   bool Changed = false;
00056 
00057   // Collect inherited analysis from Module level pass manager.
00058   populateInheritedAnalysis(TPM->activeStack);
00059 
00060   addRegionIntoQueue(RI->getTopLevelRegion(), RQ);
00061 
00062   if (RQ.empty()) // No regions, skip calling finalizers
00063     return false;
00064 
00065   // Initialization
00066   for (std::deque<Region *>::const_iterator I = RQ.begin(), E = RQ.end();
00067        I != E; ++I) {
00068     Region *R = *I;
00069     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
00070       RegionPass *RP = (RegionPass *)getContainedPass(Index);
00071       Changed |= RP->doInitialization(R, *this);
00072     }
00073   }
00074 
00075   // Walk Regions
00076   while (!RQ.empty()) {
00077 
00078     CurrentRegion  = RQ.back();
00079     skipThisRegion = false;
00080     redoThisRegion = false;
00081 
00082     // Run all passes on the current Region.
00083     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
00084       RegionPass *P = (RegionPass*)getContainedPass(Index);
00085 
00086       dumpPassInfo(P, EXECUTION_MSG, ON_REGION_MSG,
00087                    CurrentRegion->getNameStr());
00088       dumpRequiredSet(P);
00089 
00090       initializeAnalysisImpl(P);
00091 
00092       {
00093         PassManagerPrettyStackEntry X(P, *CurrentRegion->getEntry());
00094 
00095         TimeRegion PassTimer(getPassTimer(P));
00096         Changed |= P->runOnRegion(CurrentRegion, *this);
00097       }
00098 
00099       if (Changed)
00100         dumpPassInfo(P, MODIFICATION_MSG, ON_REGION_MSG,
00101                      skipThisRegion ? "<deleted>" :
00102                                     CurrentRegion->getNameStr());
00103       dumpPreservedSet(P);
00104 
00105       if (!skipThisRegion) {
00106         // Manually check that this region is still healthy. This is done
00107         // instead of relying on RegionInfo::verifyRegion since RegionInfo
00108         // is a function pass and it's really expensive to verify every
00109         // Region in the function every time. That level of checking can be
00110         // enabled with the -verify-region-info option.
00111         {
00112           TimeRegion PassTimer(getPassTimer(P));
00113           CurrentRegion->verifyRegion();
00114         }
00115 
00116         // Then call the regular verifyAnalysis functions.
00117         verifyPreservedAnalysis(P);
00118       }
00119 
00120       removeNotPreservedAnalysis(P);
00121       recordAvailableAnalysis(P);
00122       removeDeadPasses(P,
00123                        skipThisRegion ? "<deleted>" :
00124                                       CurrentRegion->getNameStr(),
00125                        ON_REGION_MSG);
00126 
00127       if (skipThisRegion)
00128         // Do not run other passes on this region.
00129         break;
00130     }
00131 
00132     // If the region was deleted, release all the region passes. This frees up
00133     // some memory, and avoids trouble with the pass manager trying to call
00134     // verifyAnalysis on them.
00135     if (skipThisRegion)
00136       for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
00137         Pass *P = getContainedPass(Index);
00138         freePass(P, "<deleted>", ON_REGION_MSG);
00139       }
00140 
00141     // Pop the region from queue after running all passes.
00142     RQ.pop_back();
00143 
00144     if (redoThisRegion)
00145       RQ.push_back(CurrentRegion);
00146 
00147     // Free all region nodes created in region passes.
00148     RI->clearNodeCache();
00149   }
00150 
00151   // Finalization
00152   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
00153     RegionPass *P = (RegionPass*)getContainedPass(Index);
00154     Changed |= P->doFinalization();
00155   }
00156 
00157   // Print the region tree after all pass.
00158   DEBUG(
00159     dbgs() << "\nRegion tree of function " << F.getName()
00160            << " after all region Pass:\n";
00161     RI->dump();
00162     dbgs() << "\n";
00163     );
00164 
00165   return Changed;
00166 }
00167 
00168 /// Print passes managed by this manager
00169 void RGPassManager::dumpPassStructure(unsigned Offset) {
00170   errs().indent(Offset*2) << "Region Pass Manager\n";
00171   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
00172     Pass *P = getContainedPass(Index);
00173     P->dumpPassStructure(Offset + 1);
00174     dumpLastUses(P, Offset+1);
00175   }
00176 }
00177 
00178 namespace {
00179 //===----------------------------------------------------------------------===//
00180 // PrintRegionPass
00181 class PrintRegionPass : public RegionPass {
00182 private:
00183   std::string Banner;
00184   raw_ostream &Out;       // raw_ostream to print on.
00185 
00186 public:
00187   static char ID;
00188   PrintRegionPass() : RegionPass(ID), Out(dbgs()) {}
00189   PrintRegionPass(const std::string &B, raw_ostream &o)
00190       : RegionPass(ID), Banner(B), Out(o) {}
00191 
00192   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00193     AU.setPreservesAll();
00194   }
00195 
00196   virtual bool runOnRegion(Region *R, RGPassManager &RGM) {
00197     Out << Banner;
00198     for (Region::block_iterator I = R->block_begin(), E = R->block_end();
00199          I != E; ++I)
00200       (*I)->print(Out);
00201 
00202     return false;
00203   }
00204 };
00205 
00206 char PrintRegionPass::ID = 0;
00207 }  //end anonymous namespace
00208 
00209 //===----------------------------------------------------------------------===//
00210 // RegionPass
00211 
00212 // Check if this pass is suitable for the current RGPassManager, if
00213 // available. This pass P is not suitable for a RGPassManager if P
00214 // is not preserving higher level analysis info used by other
00215 // RGPassManager passes. In such case, pop RGPassManager from the
00216 // stack. This will force assignPassManager() to create new
00217 // LPPassManger as expected.
00218 void RegionPass::preparePassManager(PMStack &PMS) {
00219 
00220   // Find RGPassManager
00221   while (!PMS.empty() &&
00222          PMS.top()->getPassManagerType() > PMT_RegionPassManager)
00223     PMS.pop();
00224 
00225 
00226   // If this pass is destroying high level information that is used
00227   // by other passes that are managed by LPM then do not insert
00228   // this pass in current LPM. Use new RGPassManager.
00229   if (PMS.top()->getPassManagerType() == PMT_RegionPassManager &&
00230     !PMS.top()->preserveHigherLevelAnalysis(this))
00231     PMS.pop();
00232 }
00233 
00234 /// Assign pass manager to manage this pass.
00235 void RegionPass::assignPassManager(PMStack &PMS,
00236                                  PassManagerType PreferredType) {
00237   // Find RGPassManager
00238   while (!PMS.empty() &&
00239          PMS.top()->getPassManagerType() > PMT_RegionPassManager)
00240     PMS.pop();
00241 
00242   RGPassManager *RGPM;
00243 
00244   // Create new Region Pass Manager if it does not exist.
00245   if (PMS.top()->getPassManagerType() == PMT_RegionPassManager)
00246     RGPM = (RGPassManager*)PMS.top();
00247   else {
00248 
00249     assert (!PMS.empty() && "Unable to create Region Pass Manager");
00250     PMDataManager *PMD = PMS.top();
00251 
00252     // [1] Create new Region Pass Manager
00253     RGPM = new RGPassManager();
00254     RGPM->populateInheritedAnalysis(PMS);
00255 
00256     // [2] Set up new manager's top level manager
00257     PMTopLevelManager *TPM = PMD->getTopLevelManager();
00258     TPM->addIndirectPassManager(RGPM);
00259 
00260     // [3] Assign manager to manage this new manager. This may create
00261     // and push new managers into PMS
00262     TPM->schedulePass(RGPM);
00263 
00264     // [4] Push new manager into PMS
00265     PMS.push(RGPM);
00266   }
00267 
00268   RGPM->add(this);
00269 }
00270 
00271 /// Get the printer pass
00272 Pass *RegionPass::createPrinterPass(raw_ostream &O,
00273                                   const std::string &Banner) const {
00274   return new PrintRegionPass(Banner, O);
00275 }