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 = nullptr;
00035   CurrentRegion = nullptr;
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 (const auto &E : R)
00042     addRegionIntoQueue(*E, 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(const std::string &B, raw_ostream &o)
00189       : RegionPass(ID), Banner(B), Out(o) {}
00190 
00191   void getAnalysisUsage(AnalysisUsage &AU) const override {
00192     AU.setPreservesAll();
00193   }
00194 
00195   bool runOnRegion(Region *R, RGPassManager &RGM) override {
00196     Out << Banner;
00197     for (const auto &BB : R->blocks())
00198       BB->print(Out);
00199 
00200     return false;
00201   }
00202 };
00203 
00204 char PrintRegionPass::ID = 0;
00205 }  //end anonymous namespace
00206 
00207 //===----------------------------------------------------------------------===//
00208 // RegionPass
00209 
00210 // Check if this pass is suitable for the current RGPassManager, if
00211 // available. This pass P is not suitable for a RGPassManager if P
00212 // is not preserving higher level analysis info used by other
00213 // RGPassManager passes. In such case, pop RGPassManager from the
00214 // stack. This will force assignPassManager() to create new
00215 // LPPassManger as expected.
00216 void RegionPass::preparePassManager(PMStack &PMS) {
00217 
00218   // Find RGPassManager
00219   while (!PMS.empty() &&
00220          PMS.top()->getPassManagerType() > PMT_RegionPassManager)
00221     PMS.pop();
00222 
00223 
00224   // If this pass is destroying high level information that is used
00225   // by other passes that are managed by LPM then do not insert
00226   // this pass in current LPM. Use new RGPassManager.
00227   if (PMS.top()->getPassManagerType() == PMT_RegionPassManager &&
00228     !PMS.top()->preserveHigherLevelAnalysis(this))
00229     PMS.pop();
00230 }
00231 
00232 /// Assign pass manager to manage this pass.
00233 void RegionPass::assignPassManager(PMStack &PMS,
00234                                  PassManagerType PreferredType) {
00235   // Find RGPassManager
00236   while (!PMS.empty() &&
00237          PMS.top()->getPassManagerType() > PMT_RegionPassManager)
00238     PMS.pop();
00239 
00240   RGPassManager *RGPM;
00241 
00242   // Create new Region Pass Manager if it does not exist.
00243   if (PMS.top()->getPassManagerType() == PMT_RegionPassManager)
00244     RGPM = (RGPassManager*)PMS.top();
00245   else {
00246 
00247     assert (!PMS.empty() && "Unable to create Region Pass Manager");
00248     PMDataManager *PMD = PMS.top();
00249 
00250     // [1] Create new Region Pass Manager
00251     RGPM = new RGPassManager();
00252     RGPM->populateInheritedAnalysis(PMS);
00253 
00254     // [2] Set up new manager's top level manager
00255     PMTopLevelManager *TPM = PMD->getTopLevelManager();
00256     TPM->addIndirectPassManager(RGPM);
00257 
00258     // [3] Assign manager to manage this new manager. This may create
00259     // and push new managers into PMS
00260     TPM->schedulePass(RGPM);
00261 
00262     // [4] Push new manager into PMS
00263     PMS.push(RGPM);
00264   }
00265 
00266   RGPM->add(this);
00267 }
00268 
00269 /// Get the printer pass
00270 Pass *RegionPass::createPrinterPass(raw_ostream &O,
00271                                   const std::string &Banner) const {
00272   return new PrintRegionPass(Banner, O);
00273 }