blob: c5d71b25e022fe076765c26f181ba9db068dc770 [file] [log] [blame]
Tobias Grosser23c83412010-10-20 01:54:44 +00001//===- 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.
David Majnemerf29b7ba2016-07-19 17:50:27 +000013// Most of this code has been COPIED from LoopPass.cpp
Tobias Grosser23c83412010-10-20 01:54:44 +000014//
15//===----------------------------------------------------------------------===//
16#include "llvm/Analysis/RegionPass.h"
Eli Friedman0d823d62017-06-01 21:22:26 +000017#include "llvm/IR/OptBisect.h"
Tobias Grosser23c83412010-10-20 01:54:44 +000018#include "llvm/Support/Debug.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000019#include "llvm/Support/Timer.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000020#include "llvm/Support/raw_ostream.h"
Tobias Grosser23c83412010-10-20 01:54:44 +000021using namespace llvm;
22
Chandler Carruthf1221bd2014-04-22 02:48:03 +000023#define DEBUG_TYPE "regionpassmgr"
24
Tobias Grosser23c83412010-10-20 01:54:44 +000025//===----------------------------------------------------------------------===//
26// RGPassManager
27//
28
29char RGPassManager::ID = 0;
30
Andrew Trick08966212011-08-29 17:07:00 +000031RGPassManager::RGPassManager()
32 : FunctionPass(ID), PMDataManager() {
Tobias Grosser23c83412010-10-20 01:54:44 +000033 skipThisRegion = false;
34 redoThisRegion = false;
Craig Topper9f008862014-04-15 04:59:12 +000035 RI = nullptr;
36 CurrentRegion = nullptr;
Tobias Grosser23c83412010-10-20 01:54:44 +000037}
38
39// Recurse through all subregions and all regions into RQ.
David Blaikieec649ac2014-04-15 18:32:43 +000040static void addRegionIntoQueue(Region &R, std::deque<Region *> &RQ) {
41 RQ.push_back(&R);
42 for (const auto &E : R)
43 addRegionIntoQueue(*E, RQ);
Tobias Grosser23c83412010-10-20 01:54:44 +000044}
45
46/// Pass Manager itself does not invalidate any analysis info.
47void RGPassManager::getAnalysisUsage(AnalysisUsage &Info) const {
Matt Arsenault1b8d8372014-07-19 18:29:29 +000048 Info.addRequired<RegionInfoPass>();
Tobias Grosser23c83412010-10-20 01:54:44 +000049 Info.setPreservesAll();
50}
51
52/// run - Execute all of the passes scheduled for execution. Keep track of
53/// whether any of the passes modifies the function, and if so, return true.
54bool RGPassManager::runOnFunction(Function &F) {
Matt Arsenault1b8d8372014-07-19 18:29:29 +000055 RI = &getAnalysis<RegionInfoPass>().getRegionInfo();
Tobias Grosser23c83412010-10-20 01:54:44 +000056 bool Changed = false;
57
58 // Collect inherited analysis from Module level pass manager.
59 populateInheritedAnalysis(TPM->activeStack);
60
David Blaikieec649ac2014-04-15 18:32:43 +000061 addRegionIntoQueue(*RI->getTopLevelRegion(), RQ);
Tobias Grosser23c83412010-10-20 01:54:44 +000062
63 if (RQ.empty()) // No regions, skip calling finalizers
64 return false;
65
66 // Initialization
David Majnemerf29b7ba2016-07-19 17:50:27 +000067 for (Region *R : RQ) {
Tobias Grosser23c83412010-10-20 01:54:44 +000068 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
69 RegionPass *RP = (RegionPass *)getContainedPass(Index);
70 Changed |= RP->doInitialization(R, *this);
71 }
72 }
73
74 // Walk Regions
75 while (!RQ.empty()) {
76
77 CurrentRegion = RQ.back();
78 skipThisRegion = false;
79 redoThisRegion = false;
80
81 // Run all passes on the current Region.
82 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
83 RegionPass *P = (RegionPass*)getContainedPass(Index);
84
Chad Rosier99b3e022015-03-06 16:15:04 +000085 if (isPassDebuggingExecutionsOrMore()) {
86 dumpPassInfo(P, EXECUTION_MSG, ON_REGION_MSG,
87 CurrentRegion->getNameStr());
88 dumpRequiredSet(P);
89 }
Tobias Grosser23c83412010-10-20 01:54:44 +000090
91 initializeAnalysisImpl(P);
92
93 {
94 PassManagerPrettyStackEntry X(P, *CurrentRegion->getEntry());
95
96 TimeRegion PassTimer(getPassTimer(P));
97 Changed |= P->runOnRegion(CurrentRegion, *this);
98 }
99
Chad Rosier99b3e022015-03-06 16:15:04 +0000100 if (isPassDebuggingExecutionsOrMore()) {
101 if (Changed)
102 dumpPassInfo(P, MODIFICATION_MSG, ON_REGION_MSG,
103 skipThisRegion ? "<deleted>" :
104 CurrentRegion->getNameStr());
105 dumpPreservedSet(P);
106 }
Tobias Grosser23c83412010-10-20 01:54:44 +0000107
108 if (!skipThisRegion) {
109 // Manually check that this region is still healthy. This is done
110 // instead of relying on RegionInfo::verifyRegion since RegionInfo
111 // is a function pass and it's really expensive to verify every
112 // Region in the function every time. That level of checking can be
113 // enabled with the -verify-region-info option.
114 {
115 TimeRegion PassTimer(getPassTimer(P));
116 CurrentRegion->verifyRegion();
117 }
118
119 // Then call the regular verifyAnalysis functions.
120 verifyPreservedAnalysis(P);
121 }
122
123 removeNotPreservedAnalysis(P);
124 recordAvailableAnalysis(P);
125 removeDeadPasses(P,
Chad Rosier99b3e022015-03-06 16:15:04 +0000126 (!isPassDebuggingExecutionsOrMore() || skipThisRegion) ?
127 "<deleted>" : CurrentRegion->getNameStr(),
Tobias Grosser23c83412010-10-20 01:54:44 +0000128 ON_REGION_MSG);
129
130 if (skipThisRegion)
131 // Do not run other passes on this region.
132 break;
133 }
134
135 // If the region was deleted, release all the region passes. This frees up
136 // some memory, and avoids trouble with the pass manager trying to call
137 // verifyAnalysis on them.
138 if (skipThisRegion)
139 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
140 Pass *P = getContainedPass(Index);
141 freePass(P, "<deleted>", ON_REGION_MSG);
142 }
143
144 // Pop the region from queue after running all passes.
145 RQ.pop_back();
146
147 if (redoThisRegion)
148 RQ.push_back(CurrentRegion);
149
150 // Free all region nodes created in region passes.
151 RI->clearNodeCache();
152 }
153
154 // Finalization
155 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
156 RegionPass *P = (RegionPass*)getContainedPass(Index);
157 Changed |= P->doFinalization();
158 }
159
160 // Print the region tree after all pass.
161 DEBUG(
162 dbgs() << "\nRegion tree of function " << F.getName()
163 << " after all region Pass:\n";
164 RI->dump();
165 dbgs() << "\n";
166 );
167
168 return Changed;
169}
170
171/// Print passes managed by this manager
172void RGPassManager::dumpPassStructure(unsigned Offset) {
173 errs().indent(Offset*2) << "Region Pass Manager\n";
174 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
175 Pass *P = getContainedPass(Index);
176 P->dumpPassStructure(Offset + 1);
177 dumpLastUses(P, Offset+1);
178 }
179}
180
181namespace {
182//===----------------------------------------------------------------------===//
183// PrintRegionPass
184class PrintRegionPass : public RegionPass {
185private:
186 std::string Banner;
187 raw_ostream &Out; // raw_ostream to print on.
188
189public:
190 static char ID;
Tobias Grosser23c83412010-10-20 01:54:44 +0000191 PrintRegionPass(const std::string &B, raw_ostream &o)
192 : RegionPass(ID), Banner(B), Out(o) {}
193
Craig Toppere9ba7592014-03-05 07:30:04 +0000194 void getAnalysisUsage(AnalysisUsage &AU) const override {
Tobias Grosser23c83412010-10-20 01:54:44 +0000195 AU.setPreservesAll();
196 }
197
Craig Toppere9ba7592014-03-05 07:30:04 +0000198 bool runOnRegion(Region *R, RGPassManager &RGM) override {
Tobias Grosser23c83412010-10-20 01:54:44 +0000199 Out << Banner;
Richard Trieu6b1aa5f2015-04-15 01:21:15 +0000200 for (const auto *BB : R->blocks()) {
Richard Trieuc1485222014-06-21 02:43:02 +0000201 if (BB)
202 BB->print(Out);
203 else
204 Out << "Printing <null> Block";
Richard Trieua23043c2014-06-09 22:53:16 +0000205 }
Tobias Grosser23c83412010-10-20 01:54:44 +0000206
207 return false;
208 }
Yaron Keren1de47922017-03-10 07:09:20 +0000209
Michael Krusec0a6aab2017-08-25 12:38:53 +0000210 StringRef getPassName() const override { return "Print Region IR"; }
Tobias Grosser23c83412010-10-20 01:54:44 +0000211};
212
213char PrintRegionPass::ID = 0;
214} //end anonymous namespace
215
216//===----------------------------------------------------------------------===//
217// RegionPass
218
219// Check if this pass is suitable for the current RGPassManager, if
220// available. This pass P is not suitable for a RGPassManager if P
221// is not preserving higher level analysis info used by other
222// RGPassManager passes. In such case, pop RGPassManager from the
223// stack. This will force assignPassManager() to create new
224// LPPassManger as expected.
225void RegionPass::preparePassManager(PMStack &PMS) {
226
227 // Find RGPassManager
228 while (!PMS.empty() &&
229 PMS.top()->getPassManagerType() > PMT_RegionPassManager)
230 PMS.pop();
231
232
233 // If this pass is destroying high level information that is used
234 // by other passes that are managed by LPM then do not insert
235 // this pass in current LPM. Use new RGPassManager.
236 if (PMS.top()->getPassManagerType() == PMT_RegionPassManager &&
237 !PMS.top()->preserveHigherLevelAnalysis(this))
238 PMS.pop();
239}
240
241/// Assign pass manager to manage this pass.
242void RegionPass::assignPassManager(PMStack &PMS,
243 PassManagerType PreferredType) {
244 // Find RGPassManager
245 while (!PMS.empty() &&
246 PMS.top()->getPassManagerType() > PMT_RegionPassManager)
247 PMS.pop();
248
249 RGPassManager *RGPM;
250
251 // Create new Region Pass Manager if it does not exist.
252 if (PMS.top()->getPassManagerType() == PMT_RegionPassManager)
253 RGPM = (RGPassManager*)PMS.top();
254 else {
255
256 assert (!PMS.empty() && "Unable to create Region Pass Manager");
257 PMDataManager *PMD = PMS.top();
258
Hongbin Zhengcd5afc52011-05-05 13:59:38 +0000259 // [1] Create new Region Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +0000260 RGPM = new RGPassManager();
Tobias Grosser23c83412010-10-20 01:54:44 +0000261 RGPM->populateInheritedAnalysis(PMS);
262
263 // [2] Set up new manager's top level manager
264 PMTopLevelManager *TPM = PMD->getTopLevelManager();
265 TPM->addIndirectPassManager(RGPM);
266
267 // [3] Assign manager to manage this new manager. This may create
268 // and push new managers into PMS
Tobias Grosserf3e1ada2010-12-12 21:58:28 +0000269 TPM->schedulePass(RGPM);
Tobias Grosser23c83412010-10-20 01:54:44 +0000270
271 // [4] Push new manager into PMS
272 PMS.push(RGPM);
273 }
274
275 RGPM->add(this);
276}
277
278/// Get the printer pass
279Pass *RegionPass::createPrinterPass(raw_ostream &O,
280 const std::string &Banner) const {
281 return new PrintRegionPass(Banner, O);
282}
Eli Friedman0d823d62017-06-01 21:22:26 +0000283
284bool RegionPass::skipRegion(Region &R) const {
285 Function &F = *R.getEntry()->getParent();
286 if (!F.getContext().getOptBisect().shouldRunPass(this, R))
287 return true;
288
289 if (F.hasFnAttribute(Attribute::OptimizeNone)) {
290 // Report this only once per function.
291 if (R.getEntry() == &F.getEntryBlock())
292 DEBUG(dbgs() << "Skipping pass '" << getPassName()
293 << "' on function " << F.getName() << "\n");
294 return true;
295 }
296 return false;
297}