1 //===- RegionPass.cpp - Region Pass and Region Pass Manager ---------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements RegionPass and RGPassManager. All region optimization
10 // and transformation passes are derived from RegionPass. RGPassManager is
11 // responsible for managing RegionPasses.
12 // Most of this code has been COPIED from LoopPass.cpp
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Analysis/RegionPass.h"
17 #include "llvm/Analysis/RegionInfo.h"
18 #include "llvm/IR/OptBisect.h"
19 #include "llvm/IR/PassTimingInfo.h"
20 #include "llvm/IR/PrintPasses.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/Timer.h"
23 #include "llvm/Support/raw_ostream.h"
27 #define DEBUG_TYPE "regionpassmgr"
29 //===----------------------------------------------------------------------===//
33 char RGPassManager::ID
= 0;
35 RGPassManager::RGPassManager() : FunctionPass(ID
) {
37 CurrentRegion
= nullptr;
40 // Recurse through all subregions and all regions into RQ.
41 static void addRegionIntoQueue(Region
&R
, std::deque
<Region
*> &RQ
) {
43 for (const auto &E
: R
)
44 addRegionIntoQueue(*E
, RQ
);
47 /// Pass Manager itself does not invalidate any analysis info.
48 void RGPassManager::getAnalysisUsage(AnalysisUsage
&Info
) const {
49 Info
.addRequired
<RegionInfoPass
>();
50 Info
.setPreservesAll();
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.
55 bool RGPassManager::runOnFunction(Function
&F
) {
56 RI
= &getAnalysis
<RegionInfoPass
>().getRegionInfo();
59 // Collect inherited analysis from Module level pass manager.
60 populateInheritedAnalysis(TPM
->activeStack
);
62 addRegionIntoQueue(*RI
->getTopLevelRegion(), RQ
);
64 if (RQ
.empty()) // No regions, skip calling finalizers
68 for (Region
*R
: RQ
) {
69 for (unsigned Index
= 0; Index
< getNumContainedPasses(); ++Index
) {
70 RegionPass
*RP
= (RegionPass
*)getContainedPass(Index
);
71 Changed
|= RP
->doInitialization(R
, *this);
78 CurrentRegion
= RQ
.back();
80 // Run all passes on the current Region.
81 for (unsigned Index
= 0; Index
< getNumContainedPasses(); ++Index
) {
82 RegionPass
*P
= (RegionPass
*)getContainedPass(Index
);
84 if (isPassDebuggingExecutionsOrMore()) {
85 dumpPassInfo(P
, EXECUTION_MSG
, ON_REGION_MSG
,
86 CurrentRegion
->getNameStr());
90 initializeAnalysisImpl(P
);
92 bool LocalChanged
= false;
94 PassManagerPrettyStackEntry
X(P
, *CurrentRegion
->getEntry());
96 TimeRegion
PassTimer(getPassTimer(P
));
97 #ifdef EXPENSIVE_CHECKS
98 uint64_t RefHash
= P
->structuralHash(F
);
100 LocalChanged
= P
->runOnRegion(CurrentRegion
, *this);
102 #ifdef EXPENSIVE_CHECKS
103 if (!LocalChanged
&& (RefHash
!= P
->structuralHash(F
))) {
104 llvm::errs() << "Pass modifies its input and doesn't report it: "
105 << P
->getPassName() << "\n";
106 llvm_unreachable("Pass modifies its input and doesn't report it");
110 Changed
|= LocalChanged
;
113 if (isPassDebuggingExecutionsOrMore()) {
115 dumpPassInfo(P
, MODIFICATION_MSG
, ON_REGION_MSG
,
116 CurrentRegion
->getNameStr());
120 // Manually check that this region is still healthy. This is done
121 // instead of relying on RegionInfo::verifyRegion since RegionInfo
122 // is a function pass and it's really expensive to verify every
123 // Region in the function every time. That level of checking can be
124 // enabled with the -verify-region-info option.
126 TimeRegion
PassTimer(getPassTimer(P
));
127 CurrentRegion
->verifyRegion();
130 // Then call the regular verifyAnalysis functions.
131 verifyPreservedAnalysis(P
);
134 removeNotPreservedAnalysis(P
);
135 recordAvailableAnalysis(P
);
137 (!isPassDebuggingExecutionsOrMore())
139 : CurrentRegion
->getNameStr(),
143 // Pop the region from queue after running all passes.
146 // Free all region nodes created in region passes.
147 RI
->clearNodeCache();
151 for (unsigned Index
= 0; Index
< getNumContainedPasses(); ++Index
) {
152 RegionPass
*P
= (RegionPass
*)getContainedPass(Index
);
153 Changed
|= P
->doFinalization();
156 // Print the region tree after all pass.
157 LLVM_DEBUG(dbgs() << "\nRegion tree of function " << F
.getName()
158 << " after all region Pass:\n";
159 RI
->dump(); dbgs() << "\n";);
164 /// Print passes managed by this manager
165 void RGPassManager::dumpPassStructure(unsigned Offset
) {
166 errs().indent(Offset
*2) << "Region Pass Manager\n";
167 for (unsigned Index
= 0; Index
< getNumContainedPasses(); ++Index
) {
168 Pass
*P
= getContainedPass(Index
);
169 P
->dumpPassStructure(Offset
+ 1);
170 dumpLastUses(P
, Offset
+1);
175 //===----------------------------------------------------------------------===//
177 class PrintRegionPass
: public RegionPass
{
180 raw_ostream
&Out
; // raw_ostream to print on.
184 PrintRegionPass(const std::string
&B
, raw_ostream
&o
)
185 : RegionPass(ID
), Banner(B
), Out(o
) {}
187 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
188 AU
.setPreservesAll();
191 bool runOnRegion(Region
*R
, RGPassManager
&RGM
) override
{
192 if (!isFunctionInPrintList(R
->getEntry()->getParent()->getName()))
195 for (const auto *BB
: R
->blocks()) {
199 Out
<< "Printing <null> Block";
205 StringRef
getPassName() const override
{ return "Print Region IR"; }
208 char PrintRegionPass::ID
= 0;
209 } //end anonymous namespace
211 //===----------------------------------------------------------------------===//
214 // Check if this pass is suitable for the current RGPassManager, if
215 // available. This pass P is not suitable for a RGPassManager if P
216 // is not preserving higher level analysis info used by other
217 // RGPassManager passes. In such case, pop RGPassManager from the
218 // stack. This will force assignPassManager() to create new
219 // LPPassManger as expected.
220 void RegionPass::preparePassManager(PMStack
&PMS
) {
222 // Find RGPassManager
223 while (!PMS
.empty() &&
224 PMS
.top()->getPassManagerType() > PMT_RegionPassManager
)
228 // If this pass is destroying high level information that is used
229 // by other passes that are managed by LPM then do not insert
230 // this pass in current LPM. Use new RGPassManager.
231 if (PMS
.top()->getPassManagerType() == PMT_RegionPassManager
&&
232 !PMS
.top()->preserveHigherLevelAnalysis(this))
236 /// Assign pass manager to manage this pass.
237 void RegionPass::assignPassManager(PMStack
&PMS
,
238 PassManagerType PreferredType
) {
239 // Find RGPassManager
240 while (!PMS
.empty() &&
241 PMS
.top()->getPassManagerType() > PMT_RegionPassManager
)
246 // Create new Region Pass Manager if it does not exist.
247 if (PMS
.top()->getPassManagerType() == PMT_RegionPassManager
)
248 RGPM
= (RGPassManager
*)PMS
.top();
251 assert (!PMS
.empty() && "Unable to create Region Pass Manager");
252 PMDataManager
*PMD
= PMS
.top();
254 // [1] Create new Region Pass Manager
255 RGPM
= new RGPassManager();
256 RGPM
->populateInheritedAnalysis(PMS
);
258 // [2] Set up new manager's top level manager
259 PMTopLevelManager
*TPM
= PMD
->getTopLevelManager();
260 TPM
->addIndirectPassManager(RGPM
);
262 // [3] Assign manager to manage this new manager. This may create
263 // and push new managers into PMS
264 TPM
->schedulePass(RGPM
);
266 // [4] Push new manager into PMS
273 /// Get the printer pass
274 Pass
*RegionPass::createPrinterPass(raw_ostream
&O
,
275 const std::string
&Banner
) const {
276 return new PrintRegionPass(Banner
, O
);
279 static std::string
getDescription(const Region
&R
) {
283 bool RegionPass::skipRegion(Region
&R
) const {
284 Function
&F
= *R
.getEntry()->getParent();
285 OptPassGate
&Gate
= F
.getContext().getOptPassGate();
286 if (Gate
.isEnabled() &&
287 !Gate
.shouldRunPass(this->getPassName(), getDescription(R
)))
290 if (F
.hasOptNone()) {
291 // Report this only once per function.
292 if (R
.getEntry() == &F
.getEntryBlock())
293 LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName()
294 << "' on function " << F
.getName() << "\n");