LLVM  mainline
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/Debug.h"
00019 #include "llvm/Support/Timer.h"
00020 #include "llvm/Support/raw_ostream.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<RegionInfoPass>();
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<RegionInfoPass>().getRegionInfo();
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       if (isPassDebuggingExecutionsOrMore()) {
00088         dumpPassInfo(P, EXECUTION_MSG, ON_REGION_MSG,
00089                      CurrentRegion->getNameStr());
00090         dumpRequiredSet(P);
00091       }
00092 
00093       initializeAnalysisImpl(P);
00094 
00095       {
00096         PassManagerPrettyStackEntry X(P, *CurrentRegion->getEntry());
00097 
00098         TimeRegion PassTimer(getPassTimer(P));
00099         Changed |= P->runOnRegion(CurrentRegion, *this);
00100       }
00101 
00102       if (isPassDebuggingExecutionsOrMore()) {
00103         if (Changed)
00104           dumpPassInfo(P, MODIFICATION_MSG, ON_REGION_MSG,
00105                        skipThisRegion ? "<deleted>" :
00106                                       CurrentRegion->getNameStr());
00107         dumpPreservedSet(P);
00108       }
00109 
00110       if (!skipThisRegion) {
00111         // Manually check that this region is still healthy. This is done
00112         // instead of relying on RegionInfo::verifyRegion since RegionInfo
00113         // is a function pass and it's really expensive to verify every
00114         // Region in the function every time. That level of checking can be
00115         // enabled with the -verify-region-info option.
00116         {
00117           TimeRegion PassTimer(getPassTimer(P));
00118           CurrentRegion->verifyRegion();
00119         }
00120 
00121         // Then call the regular verifyAnalysis functions.
00122         verifyPreservedAnalysis(P);
00123       }
00124 
00125       removeNotPreservedAnalysis(P);
00126       recordAvailableAnalysis(P);
00127       removeDeadPasses(P,
00128                        (!isPassDebuggingExecutionsOrMore() || skipThisRegion) ?
00129                        "<deleted>" :  CurrentRegion->getNameStr(),
00130                        ON_REGION_MSG);
00131 
00132       if (skipThisRegion)
00133         // Do not run other passes on this region.
00134         break;
00135     }
00136 
00137     // If the region was deleted, release all the region passes. This frees up
00138     // some memory, and avoids trouble with the pass manager trying to call
00139     // verifyAnalysis on them.
00140     if (skipThisRegion)
00141       for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
00142         Pass *P = getContainedPass(Index);
00143         freePass(P, "<deleted>", ON_REGION_MSG);
00144       }
00145 
00146     // Pop the region from queue after running all passes.
00147     RQ.pop_back();
00148 
00149     if (redoThisRegion)
00150       RQ.push_back(CurrentRegion);
00151 
00152     // Free all region nodes created in region passes.
00153     RI->clearNodeCache();
00154   }
00155 
00156   // Finalization
00157   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
00158     RegionPass *P = (RegionPass*)getContainedPass(Index);
00159     Changed |= P->doFinalization();
00160   }
00161 
00162   // Print the region tree after all pass.
00163   DEBUG(
00164     dbgs() << "\nRegion tree of function " << F.getName()
00165            << " after all region Pass:\n";
00166     RI->dump();
00167     dbgs() << "\n";
00168     );
00169 
00170   return Changed;
00171 }
00172 
00173 /// Print passes managed by this manager
00174 void RGPassManager::dumpPassStructure(unsigned Offset) {
00175   errs().indent(Offset*2) << "Region Pass Manager\n";
00176   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
00177     Pass *P = getContainedPass(Index);
00178     P->dumpPassStructure(Offset + 1);
00179     dumpLastUses(P, Offset+1);
00180   }
00181 }
00182 
00183 namespace {
00184 //===----------------------------------------------------------------------===//
00185 // PrintRegionPass
00186 class PrintRegionPass : public RegionPass {
00187 private:
00188   std::string Banner;
00189   raw_ostream &Out;       // raw_ostream to print on.
00190 
00191 public:
00192   static char ID;
00193   PrintRegionPass(const std::string &B, raw_ostream &o)
00194       : RegionPass(ID), Banner(B), Out(o) {}
00195 
00196   void getAnalysisUsage(AnalysisUsage &AU) const override {
00197     AU.setPreservesAll();
00198   }
00199 
00200   bool runOnRegion(Region *R, RGPassManager &RGM) override {
00201     Out << Banner;
00202     for (const auto *BB : R->blocks()) {
00203       if (BB)
00204         BB->print(Out);
00205       else
00206         Out << "Printing <null> Block";
00207     }
00208 
00209     return false;
00210   }
00211 };
00212 
00213 char PrintRegionPass::ID = 0;
00214 }  //end anonymous namespace
00215 
00216 //===----------------------------------------------------------------------===//
00217 // RegionPass
00218 
00219 // Check if this pass is suitable for the current RGPassManager, if
00220 // available. This pass P is not suitable for a RGPassManager if P
00221 // is not preserving higher level analysis info used by other
00222 // RGPassManager passes. In such case, pop RGPassManager from the
00223 // stack. This will force assignPassManager() to create new
00224 // LPPassManger as expected.
00225 void RegionPass::preparePassManager(PMStack &PMS) {
00226 
00227   // Find RGPassManager
00228   while (!PMS.empty() &&
00229          PMS.top()->getPassManagerType() > PMT_RegionPassManager)
00230     PMS.pop();
00231 
00232 
00233   // If this pass is destroying high level information that is used
00234   // by other passes that are managed by LPM then do not insert
00235   // this pass in current LPM. Use new RGPassManager.
00236   if (PMS.top()->getPassManagerType() == PMT_RegionPassManager &&
00237     !PMS.top()->preserveHigherLevelAnalysis(this))
00238     PMS.pop();
00239 }
00240 
00241 /// Assign pass manager to manage this pass.
00242 void RegionPass::assignPassManager(PMStack &PMS,
00243                                  PassManagerType PreferredType) {
00244   // Find RGPassManager
00245   while (!PMS.empty() &&
00246          PMS.top()->getPassManagerType() > PMT_RegionPassManager)
00247     PMS.pop();
00248 
00249   RGPassManager *RGPM;
00250 
00251   // Create new Region Pass Manager if it does not exist.
00252   if (PMS.top()->getPassManagerType() == PMT_RegionPassManager)
00253     RGPM = (RGPassManager*)PMS.top();
00254   else {
00255 
00256     assert (!PMS.empty() && "Unable to create Region Pass Manager");
00257     PMDataManager *PMD = PMS.top();
00258 
00259     // [1] Create new Region Pass Manager
00260     RGPM = new RGPassManager();
00261     RGPM->populateInheritedAnalysis(PMS);
00262 
00263     // [2] Set up new manager's top level manager
00264     PMTopLevelManager *TPM = PMD->getTopLevelManager();
00265     TPM->addIndirectPassManager(RGPM);
00266 
00267     // [3] Assign manager to manage this new manager. This may create
00268     // and push new managers into PMS
00269     TPM->schedulePass(RGPM);
00270 
00271     // [4] Push new manager into PMS
00272     PMS.push(RGPM);
00273   }
00274 
00275   RGPM->add(this);
00276 }
00277 
00278 /// Get the printer pass
00279 Pass *RegionPass::createPrinterPass(raw_ostream &O,
00280                                   const std::string &Banner) const {
00281   return new PrintRegionPass(Banner, O);
00282 }