1 //===- RegionPass.cpp - Region Pass and Region Pass Manager ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements RegionPass and RGPassManager. All region optimization
11 // and transformation passes are derived from RegionPass. RGPassManager is
12 // responsible for managing RegionPasses.
13 // most of these codes are COPY from LoopPass.cpp
15 //===----------------------------------------------------------------------===//
16 #include "llvm/Analysis/RegionPass.h"
17 #include "llvm/Analysis/RegionIterator.h"
18 #include "llvm/Support/Timer.h"
20 #define DEBUG_TYPE "regionpassmgr"
21 #include "llvm/Support/Debug.h"
24 //===----------------------------------------------------------------------===//
28 char RGPassManager::ID = 0;
30 RGPassManager::RGPassManager()
31 : FunctionPass(ID), PMDataManager() {
32 skipThisRegion = false;
33 redoThisRegion = false;
38 // Recurse through all subregions and all regions into RQ.
39 static void addRegionIntoQueue(Region *R, std::deque<Region *> &RQ) {
41 for (Region::iterator I = R->begin(), E = R->end(); I != E; ++I)
42 addRegionIntoQueue(*I, RQ);
45 /// Pass Manager itself does not invalidate any analysis info.
46 void RGPassManager::getAnalysisUsage(AnalysisUsage &Info) const {
47 Info.addRequired<RegionInfo>();
48 Info.setPreservesAll();
51 /// run - Execute all of the passes scheduled for execution. Keep track of
52 /// whether any of the passes modifies the function, and if so, return true.
53 bool RGPassManager::runOnFunction(Function &F) {
54 RI = &getAnalysis<RegionInfo>();
57 // Collect inherited analysis from Module level pass manager.
58 populateInheritedAnalysis(TPM->activeStack);
60 addRegionIntoQueue(RI->getTopLevelRegion(), RQ);
62 if (RQ.empty()) // No regions, skip calling finalizers
66 for (std::deque<Region *>::const_iterator I = RQ.begin(), E = RQ.end();
69 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
70 RegionPass *RP = (RegionPass *)getContainedPass(Index);
71 Changed |= RP->doInitialization(R, *this);
78 CurrentRegion = RQ.back();
79 skipThisRegion = false;
80 redoThisRegion = false;
82 // Run all passes on the current Region.
83 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
84 RegionPass *P = (RegionPass*)getContainedPass(Index);
86 dumpPassInfo(P, EXECUTION_MSG, ON_REGION_MSG,
87 CurrentRegion->getNameStr());
90 initializeAnalysisImpl(P);
93 PassManagerPrettyStackEntry X(P, *CurrentRegion->getEntry());
95 TimeRegion PassTimer(getPassTimer(P));
96 Changed |= P->runOnRegion(CurrentRegion, *this);
100 dumpPassInfo(P, MODIFICATION_MSG, ON_REGION_MSG,
101 skipThisRegion ? "<deleted>" :
102 CurrentRegion->getNameStr());
105 if (!skipThisRegion) {
106 // Manually check that this region is still healthy. This is done
107 // instead of relying on RegionInfo::verifyRegion since RegionInfo
108 // is a function pass and it's really expensive to verify every
109 // Region in the function every time. That level of checking can be
110 // enabled with the -verify-region-info option.
112 TimeRegion PassTimer(getPassTimer(P));
113 CurrentRegion->verifyRegion();
116 // Then call the regular verifyAnalysis functions.
117 verifyPreservedAnalysis(P);
120 removeNotPreservedAnalysis(P);
121 recordAvailableAnalysis(P);
123 skipThisRegion ? "<deleted>" :
124 CurrentRegion->getNameStr(),
128 // Do not run other passes on this region.
132 // If the region was deleted, release all the region passes. This frees up
133 // some memory, and avoids trouble with the pass manager trying to call
134 // verifyAnalysis on them.
136 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
137 Pass *P = getContainedPass(Index);
138 freePass(P, "<deleted>", ON_REGION_MSG);
141 // Pop the region from queue after running all passes.
145 RQ.push_back(CurrentRegion);
147 // Free all region nodes created in region passes.
148 RI->clearNodeCache();
152 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
153 RegionPass *P = (RegionPass*)getContainedPass(Index);
154 Changed |= P->doFinalization();
157 // Print the region tree after all pass.
159 dbgs() << "\nRegion tree of function " << F.getName()
160 << " after all region Pass:\n";
168 /// Print passes managed by this manager
169 void RGPassManager::dumpPassStructure(unsigned Offset) {
170 errs().indent(Offset*2) << "Region Pass Manager\n";
171 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
172 Pass *P = getContainedPass(Index);
173 P->dumpPassStructure(Offset + 1);
174 dumpLastUses(P, Offset+1);
179 //===----------------------------------------------------------------------===//
181 class PrintRegionPass : public RegionPass {
184 raw_ostream &Out; // raw_ostream to print on.
188 PrintRegionPass() : RegionPass(ID), Out(dbgs()) {}
189 PrintRegionPass(const std::string &B, raw_ostream &o)
190 : RegionPass(ID), Banner(B), Out(o) {}
192 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
193 AU.setPreservesAll();
196 virtual bool runOnRegion(Region *R, RGPassManager &RGM) {
198 for (Region::block_node_iterator I = R->block_node_begin(),
199 E = R->block_node_end();
201 (*I)->getEntry()->print(Out);
207 char PrintRegionPass::ID = 0;
208 } //end anonymous namespace
210 //===----------------------------------------------------------------------===//
213 // Check if this pass is suitable for the current RGPassManager, if
214 // available. This pass P is not suitable for a RGPassManager if P
215 // is not preserving higher level analysis info used by other
216 // RGPassManager passes. In such case, pop RGPassManager from the
217 // stack. This will force assignPassManager() to create new
218 // LPPassManger as expected.
219 void RegionPass::preparePassManager(PMStack &PMS) {
221 // Find RGPassManager
222 while (!PMS.empty() &&
223 PMS.top()->getPassManagerType() > PMT_RegionPassManager)
227 // If this pass is destroying high level information that is used
228 // by other passes that are managed by LPM then do not insert
229 // this pass in current LPM. Use new RGPassManager.
230 if (PMS.top()->getPassManagerType() == PMT_RegionPassManager &&
231 !PMS.top()->preserveHigherLevelAnalysis(this))
235 /// Assign pass manager to manage this pass.
236 void RegionPass::assignPassManager(PMStack &PMS,
237 PassManagerType PreferredType) {
238 // Find RGPassManager
239 while (!PMS.empty() &&
240 PMS.top()->getPassManagerType() > PMT_RegionPassManager)
245 // Create new Region Pass Manager if it does not exist.
246 if (PMS.top()->getPassManagerType() == PMT_RegionPassManager)
247 RGPM = (RGPassManager*)PMS.top();
250 assert (!PMS.empty() && "Unable to create Region Pass Manager");
251 PMDataManager *PMD = PMS.top();
253 // [1] Create new Region Pass Manager
254 RGPM = new RGPassManager();
255 RGPM->populateInheritedAnalysis(PMS);
257 // [2] Set up new manager's top level manager
258 PMTopLevelManager *TPM = PMD->getTopLevelManager();
259 TPM->addIndirectPassManager(RGPM);
261 // [3] Assign manager to manage this new manager. This may create
262 // and push new managers into PMS
263 TPM->schedulePass(RGPM);
265 // [4] Push new manager into PMS
272 /// Get the printer pass
273 Pass *RegionPass::createPrinterPass(raw_ostream &O,
274 const std::string &Banner) const {
275 return new PrintRegionPass(Banner, O);