blob: b38e6225c8403376a3cc420a3e724a62741a9f86 [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"
17#include "llvm/Analysis/RegionIterator.h"
Eli Friedman0d823d62017-06-01 21:22:26 +000018#include "llvm/IR/OptBisect.h"
Tobias Grosser23c83412010-10-20 01:54:44 +000019#include "llvm/Support/Debug.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000020#include "llvm/Support/Timer.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000021#include "llvm/Support/raw_ostream.h"
Tobias Grosser23c83412010-10-20 01:54:44 +000022using namespace llvm;
23
Chandler Carruthf1221bd2014-04-22 02:48:03 +000024#define DEBUG_TYPE "regionpassmgr"
25
Tobias Grosser23c83412010-10-20 01:54:44 +000026//===----------------------------------------------------------------------===//
27// RGPassManager
28//
29
30char RGPassManager::ID = 0;
31
Andrew Trick08966212011-08-29 17:07:00 +000032RGPassManager::RGPassManager()
33 : FunctionPass(ID), PMDataManager() {
Tobias Grosser23c83412010-10-20 01:54:44 +000034 skipThisRegion = false;
35 redoThisRegion = false;
Craig Topper9f008862014-04-15 04:59:12 +000036 RI = nullptr;
37 CurrentRegion = nullptr;
Tobias Grosser23c83412010-10-20 01:54:44 +000038}
39
40// Recurse through all subregions and all regions into RQ.
David Blaikieec649ac2014-04-15 18:32:43 +000041static void addRegionIntoQueue(Region &R, std::deque<Region *> &RQ) {
42 RQ.push_back(&R);
43 for (const auto &E : R)
44 addRegionIntoQueue(*E, RQ);
Tobias Grosser23c83412010-10-20 01:54:44 +000045}
46
47/// Pass Manager itself does not invalidate any analysis info.
48void RGPassManager::getAnalysisUsage(AnalysisUsage &Info) const {
Matt Arsenault1b8d8372014-07-19 18:29:29 +000049 Info.addRequired<RegionInfoPass>();
Tobias Grosser23c83412010-10-20 01:54:44 +000050 Info.setPreservesAll();
51}
52
53/// run - Execute all of the passes scheduled for execution. Keep track of
54/// whether any of the passes modifies the function, and if so, return true.
55bool RGPassManager::runOnFunction(Function &F) {
Matt Arsenault1b8d8372014-07-19 18:29:29 +000056 RI = &getAnalysis<RegionInfoPass>().getRegionInfo();
Tobias Grosser23c83412010-10-20 01:54:44 +000057 bool Changed = false;
58
59 // Collect inherited analysis from Module level pass manager.
60 populateInheritedAnalysis(TPM->activeStack);
61
David Blaikieec649ac2014-04-15 18:32:43 +000062 addRegionIntoQueue(*RI->getTopLevelRegion(), RQ);
Tobias Grosser23c83412010-10-20 01:54:44 +000063
64 if (RQ.empty()) // No regions, skip calling finalizers
65 return false;
66
67 // Initialization
David Majnemerf29b7ba2016-07-19 17:50:27 +000068 for (Region *R : RQ) {
Tobias Grosser23c83412010-10-20 01:54:44 +000069 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
Chad Rosier99b3e022015-03-06 16:15:04 +000086 if (isPassDebuggingExecutionsOrMore()) {
87 dumpPassInfo(P, EXECUTION_MSG, ON_REGION_MSG,
88 CurrentRegion->getNameStr());
89 dumpRequiredSet(P);
90 }
Tobias Grosser23c83412010-10-20 01:54:44 +000091
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
Chad Rosier99b3e022015-03-06 16:15:04 +0000101 if (isPassDebuggingExecutionsOrMore()) {
102 if (Changed)
103 dumpPassInfo(P, MODIFICATION_MSG, ON_REGION_MSG,
104 skipThisRegion ? "<deleted>" :
105 CurrentRegion->getNameStr());
106 dumpPreservedSet(P);
107 }
Tobias Grosser23c83412010-10-20 01:54:44 +0000108
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,
Chad Rosier99b3e022015-03-06 16:15:04 +0000127 (!isPassDebuggingExecutionsOrMore() || skipThisRegion) ?
128 "<deleted>" : CurrentRegion->getNameStr(),
Tobias Grosser23c83412010-10-20 01:54:44 +0000129 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
173void 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
182namespace {
183//===----------------------------------------------------------------------===//
184// PrintRegionPass
185class PrintRegionPass : public RegionPass {
186private:
187 std::string Banner;
188 raw_ostream &Out; // raw_ostream to print on.
189
190public:
191 static char ID;
Tobias Grosser23c83412010-10-20 01:54:44 +0000192 PrintRegionPass(const std::string &B, raw_ostream &o)
193 : RegionPass(ID), Banner(B), Out(o) {}
194
Craig Toppere9ba7592014-03-05 07:30:04 +0000195 void getAnalysisUsage(AnalysisUsage &AU) const override {
Tobias Grosser23c83412010-10-20 01:54:44 +0000196 AU.setPreservesAll();
197 }
198
Craig Toppere9ba7592014-03-05 07:30:04 +0000199 bool runOnRegion(Region *R, RGPassManager &RGM) override {
Tobias Grosser23c83412010-10-20 01:54:44 +0000200 Out << Banner;
Richard Trieu6b1aa5f2015-04-15 01:21:15 +0000201 for (const auto *BB : R->blocks()) {
Richard Trieuc1485222014-06-21 02:43:02 +0000202 if (BB)
203 BB->print(Out);
204 else
205 Out << "Printing <null> Block";
Richard Trieua23043c2014-06-09 22:53:16 +0000206 }
Tobias Grosser23c83412010-10-20 01:54:44 +0000207
208 return false;
209 }
Yaron Keren1de47922017-03-10 07:09:20 +0000210
211 StringRef getPassName() const override { return "Print Region IR"; }
Tobias Grosser23c83412010-10-20 01:54:44 +0000212};
213
214char PrintRegionPass::ID = 0;
215} //end anonymous namespace
216
217//===----------------------------------------------------------------------===//
218// RegionPass
219
220// Check if this pass is suitable for the current RGPassManager, if
221// available. This pass P is not suitable for a RGPassManager if P
222// is not preserving higher level analysis info used by other
223// RGPassManager passes. In such case, pop RGPassManager from the
224// stack. This will force assignPassManager() to create new
225// LPPassManger as expected.
226void RegionPass::preparePassManager(PMStack &PMS) {
227
228 // Find RGPassManager
229 while (!PMS.empty() &&
230 PMS.top()->getPassManagerType() > PMT_RegionPassManager)
231 PMS.pop();
232
233
234 // If this pass is destroying high level information that is used
235 // by other passes that are managed by LPM then do not insert
236 // this pass in current LPM. Use new RGPassManager.
237 if (PMS.top()->getPassManagerType() == PMT_RegionPassManager &&
238 !PMS.top()->preserveHigherLevelAnalysis(this))
239 PMS.pop();
240}
241
242/// Assign pass manager to manage this pass.
243void RegionPass::assignPassManager(PMStack &PMS,
244 PassManagerType PreferredType) {
245 // Find RGPassManager
246 while (!PMS.empty() &&
247 PMS.top()->getPassManagerType() > PMT_RegionPassManager)
248 PMS.pop();
249
250 RGPassManager *RGPM;
251
252 // Create new Region Pass Manager if it does not exist.
253 if (PMS.top()->getPassManagerType() == PMT_RegionPassManager)
254 RGPM = (RGPassManager*)PMS.top();
255 else {
256
257 assert (!PMS.empty() && "Unable to create Region Pass Manager");
258 PMDataManager *PMD = PMS.top();
259
Hongbin Zhengcd5afc52011-05-05 13:59:38 +0000260 // [1] Create new Region Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +0000261 RGPM = new RGPassManager();
Tobias Grosser23c83412010-10-20 01:54:44 +0000262 RGPM->populateInheritedAnalysis(PMS);
263
264 // [2] Set up new manager's top level manager
265 PMTopLevelManager *TPM = PMD->getTopLevelManager();
266 TPM->addIndirectPassManager(RGPM);
267
268 // [3] Assign manager to manage this new manager. This may create
269 // and push new managers into PMS
Tobias Grosserf3e1ada2010-12-12 21:58:28 +0000270 TPM->schedulePass(RGPM);
Tobias Grosser23c83412010-10-20 01:54:44 +0000271
272 // [4] Push new manager into PMS
273 PMS.push(RGPM);
274 }
275
276 RGPM->add(this);
277}
278
279/// Get the printer pass
280Pass *RegionPass::createPrinterPass(raw_ostream &O,
281 const std::string &Banner) const {
282 return new PrintRegionPass(Banner, O);
283}
Eli Friedman0d823d62017-06-01 21:22:26 +0000284
285bool RegionPass::skipRegion(Region &R) const {
286 Function &F = *R.getEntry()->getParent();
287 if (!F.getContext().getOptBisect().shouldRunPass(this, R))
288 return true;
289
290 if (F.hasFnAttribute(Attribute::OptimizeNone)) {
291 // Report this only once per function.
292 if (R.getEntry() == &F.getEntryBlock())
293 DEBUG(dbgs() << "Skipping pass '" << getPassName()
294 << "' on function " << F.getName() << "\n");
295 return true;
296 }
297 return false;
298}