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