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 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/RegionPass.h"
16 #include "llvm/IR/OptBisect.h"
17 #include "llvm/IR/PassTimingInfo.h"
18 #include "llvm/IR/StructuralHash.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/Timer.h"
21 #include "llvm/Support/raw_ostream.h"
24 #define DEBUG_TYPE "regionpassmgr"
26 //===----------------------------------------------------------------------===//
30 char RGPassManager::ID
= 0;
32 RGPassManager::RGPassManager()
33 : FunctionPass(ID
), PMDataManager() {
35 CurrentRegion
= nullptr;
38 // Recurse through all subregions and all regions into RQ.
39 static void addRegionIntoQueue(Region
&R
, std::deque
<Region
*> &RQ
) {
41 for (const auto &E
: R
)
42 addRegionIntoQueue(*E
, RQ
);
45 /// Pass Manager itself does not invalidate any analysis info.
46 void RGPassManager::getAnalysisUsage(AnalysisUsage
&Info
) const {
47 Info
.addRequired
<RegionInfoPass
>();
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
<RegionInfoPass
>().getRegionInfo();
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 (Region
*R
: RQ
) {
67 for (unsigned Index
= 0; Index
< getNumContainedPasses(); ++Index
) {
68 RegionPass
*RP
= (RegionPass
*)getContainedPass(Index
);
69 Changed
|= RP
->doInitialization(R
, *this);
76 CurrentRegion
= RQ
.back();
78 // Run all passes on the current Region.
79 for (unsigned Index
= 0; Index
< getNumContainedPasses(); ++Index
) {
80 RegionPass
*P
= (RegionPass
*)getContainedPass(Index
);
82 if (isPassDebuggingExecutionsOrMore()) {
83 dumpPassInfo(P
, EXECUTION_MSG
, ON_REGION_MSG
,
84 CurrentRegion
->getNameStr());
88 initializeAnalysisImpl(P
);
90 bool LocalChanged
= false;
92 PassManagerPrettyStackEntry
X(P
, *CurrentRegion
->getEntry());
94 TimeRegion
PassTimer(getPassTimer(P
));
95 #ifdef EXPENSIVE_CHECKS
96 uint64_t RefHash
= StructuralHash(F
);
98 LocalChanged
= P
->runOnRegion(CurrentRegion
, *this);
100 #ifdef EXPENSIVE_CHECKS
101 if (!LocalChanged
&& (RefHash
!= StructuralHash(F
))) {
102 llvm::errs() << "Pass modifies its input and doesn't report it: "
103 << P
->getPassName() << "\n";
104 llvm_unreachable("Pass modifies its input and doesn't report it");
108 Changed
|= LocalChanged
;
111 if (isPassDebuggingExecutionsOrMore()) {
113 dumpPassInfo(P
, MODIFICATION_MSG
, ON_REGION_MSG
,
114 CurrentRegion
->getNameStr());
118 // Manually check that this region is still healthy. This is done
119 // instead of relying on RegionInfo::verifyRegion since RegionInfo
120 // is a function pass and it's really expensive to verify every
121 // Region in the function every time. That level of checking can be
122 // enabled with the -verify-region-info option.
124 TimeRegion
PassTimer(getPassTimer(P
));
125 CurrentRegion
->verifyRegion();
128 // Then call the regular verifyAnalysis functions.
129 verifyPreservedAnalysis(P
);
132 removeNotPreservedAnalysis(P
);
133 recordAvailableAnalysis(P
);
135 (!isPassDebuggingExecutionsOrMore())
137 : CurrentRegion
->getNameStr(),
141 // Pop the region from queue after running all passes.
144 // Free all region nodes created in region passes.
145 RI
->clearNodeCache();
149 for (unsigned Index
= 0; Index
< getNumContainedPasses(); ++Index
) {
150 RegionPass
*P
= (RegionPass
*)getContainedPass(Index
);
151 Changed
|= P
->doFinalization();
154 // Print the region tree after all pass.
155 LLVM_DEBUG(dbgs() << "\nRegion tree of function " << F
.getName()
156 << " after all region Pass:\n";
157 RI
->dump(); dbgs() << "\n";);
162 /// Print passes managed by this manager
163 void RGPassManager::dumpPassStructure(unsigned Offset
) {
164 errs().indent(Offset
*2) << "Region Pass Manager\n";
165 for (unsigned Index
= 0; Index
< getNumContainedPasses(); ++Index
) {
166 Pass
*P
= getContainedPass(Index
);
167 P
->dumpPassStructure(Offset
+ 1);
168 dumpLastUses(P
, Offset
+1);
173 //===----------------------------------------------------------------------===//
175 class PrintRegionPass
: public RegionPass
{
178 raw_ostream
&Out
; // raw_ostream to print on.
182 PrintRegionPass(const std::string
&B
, raw_ostream
&o
)
183 : RegionPass(ID
), Banner(B
), Out(o
) {}
185 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
186 AU
.setPreservesAll();
189 bool runOnRegion(Region
*R
, RGPassManager
&RGM
) override
{
191 for (const auto *BB
: R
->blocks()) {
195 Out
<< "Printing <null> Block";
201 StringRef
getPassName() const override
{ return "Print Region IR"; }
204 char PrintRegionPass::ID
= 0;
205 } //end anonymous namespace
207 //===----------------------------------------------------------------------===//
210 // Check if this pass is suitable for the current RGPassManager, if
211 // available. This pass P is not suitable for a RGPassManager if P
212 // is not preserving higher level analysis info used by other
213 // RGPassManager passes. In such case, pop RGPassManager from the
214 // stack. This will force assignPassManager() to create new
215 // LPPassManger as expected.
216 void RegionPass::preparePassManager(PMStack
&PMS
) {
218 // Find RGPassManager
219 while (!PMS
.empty() &&
220 PMS
.top()->getPassManagerType() > PMT_RegionPassManager
)
224 // If this pass is destroying high level information that is used
225 // by other passes that are managed by LPM then do not insert
226 // this pass in current LPM. Use new RGPassManager.
227 if (PMS
.top()->getPassManagerType() == PMT_RegionPassManager
&&
228 !PMS
.top()->preserveHigherLevelAnalysis(this))
232 /// Assign pass manager to manage this pass.
233 void RegionPass::assignPassManager(PMStack
&PMS
,
234 PassManagerType PreferredType
) {
235 // Find RGPassManager
236 while (!PMS
.empty() &&
237 PMS
.top()->getPassManagerType() > PMT_RegionPassManager
)
242 // Create new Region Pass Manager if it does not exist.
243 if (PMS
.top()->getPassManagerType() == PMT_RegionPassManager
)
244 RGPM
= (RGPassManager
*)PMS
.top();
247 assert (!PMS
.empty() && "Unable to create Region Pass Manager");
248 PMDataManager
*PMD
= PMS
.top();
250 // [1] Create new Region Pass Manager
251 RGPM
= new RGPassManager();
252 RGPM
->populateInheritedAnalysis(PMS
);
254 // [2] Set up new manager's top level manager
255 PMTopLevelManager
*TPM
= PMD
->getTopLevelManager();
256 TPM
->addIndirectPassManager(RGPM
);
258 // [3] Assign manager to manage this new manager. This may create
259 // and push new managers into PMS
260 TPM
->schedulePass(RGPM
);
262 // [4] Push new manager into PMS
269 /// Get the printer pass
270 Pass
*RegionPass::createPrinterPass(raw_ostream
&O
,
271 const std::string
&Banner
) const {
272 return new PrintRegionPass(Banner
, O
);
275 static std::string
getDescription(const Region
&R
) {
279 bool RegionPass::skipRegion(Region
&R
) const {
280 Function
&F
= *R
.getEntry()->getParent();
281 OptPassGate
&Gate
= F
.getContext().getOptPassGate();
282 if (Gate
.isEnabled() && !Gate
.shouldRunPass(this, getDescription(R
)))
285 if (F
.hasOptNone()) {
286 // Report this only once per function.
287 if (R
.getEntry() == &F
.getEntryBlock())
288 LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName()
289 << "' on function " << F
.getName() << "\n");