Make llvm.eh.actions an intrinsic and add docs for it
[oota-llvm.git] / lib / Analysis / RegionPass.cpp
1 //===- RegionPass.cpp - Region Pass and Region Pass Manager ---------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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
14 //
15 //===----------------------------------------------------------------------===//
16 #include "llvm/Analysis/RegionPass.h"
17 #include "llvm/Analysis/RegionIterator.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/Timer.h"
20 using namespace llvm;
21
22 #define DEBUG_TYPE "regionpassmgr"
23
24 //===----------------------------------------------------------------------===//
25 // RGPassManager
26 //
27
28 char RGPassManager::ID = 0;
29
30 RGPassManager::RGPassManager()
31   : FunctionPass(ID), PMDataManager() {
32   skipThisRegion = false;
33   redoThisRegion = false;
34   RI = nullptr;
35   CurrentRegion = nullptr;
36 }
37
38 // Recurse through all subregions and all regions  into RQ.
39 static void addRegionIntoQueue(Region &R, std::deque<Region *> &RQ) {
40   RQ.push_back(&R);
41   for (const auto &E : R)
42     addRegionIntoQueue(*E, RQ);
43 }
44
45 /// Pass Manager itself does not invalidate any analysis info.
46 void RGPassManager::getAnalysisUsage(AnalysisUsage &Info) const {
47   Info.addRequired<RegionInfoPass>();
48   Info.setPreservesAll();
49 }
50
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<RegionInfoPass>().getRegionInfo();
55   bool Changed = false;
56
57   // Collect inherited analysis from Module level pass manager.
58   populateInheritedAnalysis(TPM->activeStack);
59
60   addRegionIntoQueue(*RI->getTopLevelRegion(), RQ);
61
62   if (RQ.empty()) // No regions, skip calling finalizers
63     return false;
64
65   // Initialization
66   for (std::deque<Region *>::const_iterator I = RQ.begin(), E = RQ.end();
67        I != E; ++I) {
68     Region *R = *I;
69     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
70       RegionPass *RP = (RegionPass *)getContainedPass(Index);
71       Changed |= RP->doInitialization(R, *this);
72     }
73   }
74
75   // Walk Regions
76   while (!RQ.empty()) {
77
78     CurrentRegion  = RQ.back();
79     skipThisRegion = false;
80     redoThisRegion = false;
81
82     // Run all passes on the current Region.
83     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
84       RegionPass *P = (RegionPass*)getContainedPass(Index);
85
86       if (isPassDebuggingExecutionsOrMore()) {
87         dumpPassInfo(P, EXECUTION_MSG, ON_REGION_MSG,
88                      CurrentRegion->getNameStr());
89         dumpRequiredSet(P);
90       }
91
92       initializeAnalysisImpl(P);
93
94       {
95         PassManagerPrettyStackEntry X(P, *CurrentRegion->getEntry());
96
97         TimeRegion PassTimer(getPassTimer(P));
98         Changed |= P->runOnRegion(CurrentRegion, *this);
99       }
100
101       if (isPassDebuggingExecutionsOrMore()) {
102         if (Changed)
103           dumpPassInfo(P, MODIFICATION_MSG, ON_REGION_MSG,
104                        skipThisRegion ? "<deleted>" :
105                                       CurrentRegion->getNameStr());
106         dumpPreservedSet(P);
107       }
108
109       if (!skipThisRegion) {
110         // Manually check that this region is still healthy. This is done
111         // instead of relying on RegionInfo::verifyRegion since RegionInfo
112         // is a function pass and it's really expensive to verify every
113         // Region in the function every time. That level of checking can be
114         // enabled with the -verify-region-info option.
115         {
116           TimeRegion PassTimer(getPassTimer(P));
117           CurrentRegion->verifyRegion();
118         }
119
120         // Then call the regular verifyAnalysis functions.
121         verifyPreservedAnalysis(P);
122       }
123
124       removeNotPreservedAnalysis(P);
125       recordAvailableAnalysis(P);
126       removeDeadPasses(P,
127                        (!isPassDebuggingExecutionsOrMore() || skipThisRegion) ?
128                        "<deleted>" :  CurrentRegion->getNameStr(),
129                        ON_REGION_MSG);
130
131       if (skipThisRegion)
132         // Do not run other passes on this region.
133         break;
134     }
135
136     // If the region was deleted, release all the region passes. This frees up
137     // some memory, and avoids trouble with the pass manager trying to call
138     // verifyAnalysis on them.
139     if (skipThisRegion)
140       for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
141         Pass *P = getContainedPass(Index);
142         freePass(P, "<deleted>", ON_REGION_MSG);
143       }
144
145     // Pop the region from queue after running all passes.
146     RQ.pop_back();
147
148     if (redoThisRegion)
149       RQ.push_back(CurrentRegion);
150
151     // Free all region nodes created in region passes.
152     RI->clearNodeCache();
153   }
154
155   // Finalization
156   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
157     RegionPass *P = (RegionPass*)getContainedPass(Index);
158     Changed |= P->doFinalization();
159   }
160
161   // Print the region tree after all pass.
162   DEBUG(
163     dbgs() << "\nRegion tree of function " << F.getName()
164            << " after all region Pass:\n";
165     RI->dump();
166     dbgs() << "\n";
167     );
168
169   return Changed;
170 }
171
172 /// Print passes managed by this manager
173 void RGPassManager::dumpPassStructure(unsigned Offset) {
174   errs().indent(Offset*2) << "Region Pass Manager\n";
175   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
176     Pass *P = getContainedPass(Index);
177     P->dumpPassStructure(Offset + 1);
178     dumpLastUses(P, Offset+1);
179   }
180 }
181
182 namespace {
183 //===----------------------------------------------------------------------===//
184 // PrintRegionPass
185 class PrintRegionPass : public RegionPass {
186 private:
187   std::string Banner;
188   raw_ostream &Out;       // raw_ostream to print on.
189
190 public:
191   static char ID;
192   PrintRegionPass(const std::string &B, raw_ostream &o)
193       : RegionPass(ID), Banner(B), Out(o) {}
194
195   void getAnalysisUsage(AnalysisUsage &AU) const override {
196     AU.setPreservesAll();
197   }
198
199   bool runOnRegion(Region *R, RGPassManager &RGM) override {
200     Out << Banner;
201     for (const auto &BB : R->blocks()) {
202       if (BB)
203         BB->print(Out);
204       else
205         Out << "Printing <null> Block";
206     }
207
208     return false;
209   }
210 };
211
212 char PrintRegionPass::ID = 0;
213 }  //end anonymous namespace
214
215 //===----------------------------------------------------------------------===//
216 // RegionPass
217
218 // Check if this pass is suitable for the current RGPassManager, if
219 // available. This pass P is not suitable for a RGPassManager if P
220 // is not preserving higher level analysis info used by other
221 // RGPassManager passes. In such case, pop RGPassManager from the
222 // stack. This will force assignPassManager() to create new
223 // LPPassManger as expected.
224 void RegionPass::preparePassManager(PMStack &PMS) {
225
226   // Find RGPassManager
227   while (!PMS.empty() &&
228          PMS.top()->getPassManagerType() > PMT_RegionPassManager)
229     PMS.pop();
230
231
232   // If this pass is destroying high level information that is used
233   // by other passes that are managed by LPM then do not insert
234   // this pass in current LPM. Use new RGPassManager.
235   if (PMS.top()->getPassManagerType() == PMT_RegionPassManager &&
236     !PMS.top()->preserveHigherLevelAnalysis(this))
237     PMS.pop();
238 }
239
240 /// Assign pass manager to manage this pass.
241 void RegionPass::assignPassManager(PMStack &PMS,
242                                  PassManagerType PreferredType) {
243   // Find RGPassManager
244   while (!PMS.empty() &&
245          PMS.top()->getPassManagerType() > PMT_RegionPassManager)
246     PMS.pop();
247
248   RGPassManager *RGPM;
249
250   // Create new Region Pass Manager if it does not exist.
251   if (PMS.top()->getPassManagerType() == PMT_RegionPassManager)
252     RGPM = (RGPassManager*)PMS.top();
253   else {
254
255     assert (!PMS.empty() && "Unable to create Region Pass Manager");
256     PMDataManager *PMD = PMS.top();
257
258     // [1] Create new Region Pass Manager
259     RGPM = new RGPassManager();
260     RGPM->populateInheritedAnalysis(PMS);
261
262     // [2] Set up new manager's top level manager
263     PMTopLevelManager *TPM = PMD->getTopLevelManager();
264     TPM->addIndirectPassManager(RGPM);
265
266     // [3] Assign manager to manage this new manager. This may create
267     // and push new managers into PMS
268     TPM->schedulePass(RGPM);
269
270     // [4] Push new manager into PMS
271     PMS.push(RGPM);
272   }
273
274   RGPM->add(this);
275 }
276
277 /// Get the printer pass
278 Pass *RegionPass::createPrinterPass(raw_ostream &O,
279                                   const std::string &Banner) const {
280   return new PrintRegionPass(Banner, O);
281 }