]>
Commit | Line | Data |
---|---|---|
223e47cc LB |
1 | //===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===// |
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 the LLVM Pass infrastructure. It is primarily | |
11 | // responsible with ensuring that passes are executed and batched together | |
12 | // optimally. | |
13 | // | |
14 | //===----------------------------------------------------------------------===// | |
15 | ||
16 | #include "llvm/Pass.h" | |
1a4d82fc JJ |
17 | #include "llvm/IR/Function.h" |
18 | #include "llvm/IR/IRPrintingPasses.h" | |
19 | #include "llvm/IR/LegacyPassNameParser.h" | |
970d7e83 | 20 | #include "llvm/PassRegistry.h" |
223e47cc | 21 | #include "llvm/Support/Debug.h" |
223e47cc LB |
22 | #include "llvm/Support/raw_ostream.h" |
23 | using namespace llvm; | |
24 | ||
1a4d82fc JJ |
25 | #define DEBUG_TYPE "ir" |
26 | ||
223e47cc LB |
27 | //===----------------------------------------------------------------------===// |
28 | // Pass Implementation | |
29 | // | |
30 | ||
31 | // Force out-of-line virtual method. | |
32 | Pass::~Pass() { | |
33 | delete Resolver; | |
34 | } | |
35 | ||
36 | // Force out-of-line virtual method. | |
37 | ModulePass::~ModulePass() { } | |
38 | ||
39 | Pass *ModulePass::createPrinterPass(raw_ostream &O, | |
40 | const std::string &Banner) const { | |
1a4d82fc | 41 | return createPrintModulePass(O, Banner); |
223e47cc LB |
42 | } |
43 | ||
44 | PassManagerType ModulePass::getPotentialPassManagerType() const { | |
45 | return PMT_ModulePassManager; | |
46 | } | |
47 | ||
48 | bool Pass::mustPreserveAnalysisID(char &AID) const { | |
1a4d82fc | 49 | return Resolver->getAnalysisIfAvailable(&AID, true) != nullptr; |
223e47cc LB |
50 | } |
51 | ||
52 | // dumpPassStructure - Implement the -debug-pass=Structure option | |
53 | void Pass::dumpPassStructure(unsigned Offset) { | |
54 | dbgs().indent(Offset*2) << getPassName() << "\n"; | |
55 | } | |
56 | ||
57 | /// getPassName - Return a nice clean name for a pass. This usually | |
58 | /// implemented in terms of the name that is registered by one of the | |
59 | /// Registration templates, but can be overloaded directly. | |
60 | /// | |
61 | const char *Pass::getPassName() const { | |
62 | AnalysisID AID = getPassID(); | |
63 | const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID); | |
64 | if (PI) | |
65 | return PI->getPassName(); | |
66 | return "Unnamed pass: implement Pass::getPassName()"; | |
67 | } | |
68 | ||
69 | void Pass::preparePassManager(PMStack &) { | |
70 | // By default, don't do anything. | |
71 | } | |
72 | ||
73 | PassManagerType Pass::getPotentialPassManagerType() const { | |
74 | // Default implementation. | |
75 | return PMT_Unknown; | |
76 | } | |
77 | ||
78 | void Pass::getAnalysisUsage(AnalysisUsage &) const { | |
79 | // By default, no analysis results are used, all are invalidated. | |
80 | } | |
81 | ||
82 | void Pass::releaseMemory() { | |
83 | // By default, don't do anything. | |
84 | } | |
85 | ||
86 | void Pass::verifyAnalysis() const { | |
87 | // By default, don't do anything. | |
88 | } | |
89 | ||
90 | void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) { | |
91 | return this; | |
92 | } | |
93 | ||
94 | ImmutablePass *Pass::getAsImmutablePass() { | |
1a4d82fc | 95 | return nullptr; |
223e47cc LB |
96 | } |
97 | ||
98 | PMDataManager *Pass::getAsPMDataManager() { | |
1a4d82fc | 99 | return nullptr; |
223e47cc LB |
100 | } |
101 | ||
102 | void Pass::setResolver(AnalysisResolver *AR) { | |
103 | assert(!Resolver && "Resolver is already set"); | |
104 | Resolver = AR; | |
105 | } | |
106 | ||
107 | // print - Print out the internal state of the pass. This is called by Analyze | |
108 | // to print out the contents of an analysis. Otherwise it is not necessary to | |
109 | // implement this method. | |
110 | // | |
111 | void Pass::print(raw_ostream &O,const Module*) const { | |
112 | O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n"; | |
113 | } | |
114 | ||
115 | // dump - call print(cerr); | |
116 | void Pass::dump() const { | |
1a4d82fc | 117 | print(dbgs(), nullptr); |
223e47cc LB |
118 | } |
119 | ||
120 | //===----------------------------------------------------------------------===// | |
121 | // ImmutablePass Implementation | |
122 | // | |
123 | // Force out-of-line virtual method. | |
124 | ImmutablePass::~ImmutablePass() { } | |
125 | ||
126 | void ImmutablePass::initializePass() { | |
127 | // By default, don't do anything. | |
128 | } | |
129 | ||
130 | //===----------------------------------------------------------------------===// | |
131 | // FunctionPass Implementation | |
132 | // | |
133 | ||
134 | Pass *FunctionPass::createPrinterPass(raw_ostream &O, | |
135 | const std::string &Banner) const { | |
1a4d82fc | 136 | return createPrintFunctionPass(O, Banner); |
223e47cc LB |
137 | } |
138 | ||
223e47cc LB |
139 | PassManagerType FunctionPass::getPotentialPassManagerType() const { |
140 | return PMT_FunctionPassManager; | |
141 | } | |
142 | ||
1a4d82fc JJ |
143 | bool FunctionPass::skipOptnoneFunction(const Function &F) const { |
144 | if (F.hasFnAttribute(Attribute::OptimizeNone)) { | |
145 | DEBUG(dbgs() << "Skipping pass '" << getPassName() | |
146 | << "' on function " << F.getName() << "\n"); | |
147 | return true; | |
148 | } | |
149 | return false; | |
150 | } | |
151 | ||
223e47cc LB |
152 | //===----------------------------------------------------------------------===// |
153 | // BasicBlockPass Implementation | |
154 | // | |
155 | ||
156 | Pass *BasicBlockPass::createPrinterPass(raw_ostream &O, | |
157 | const std::string &Banner) const { | |
1a4d82fc | 158 | return createPrintBasicBlockPass(O, Banner); |
223e47cc LB |
159 | } |
160 | ||
161 | bool BasicBlockPass::doInitialization(Function &) { | |
162 | // By default, don't do anything. | |
163 | return false; | |
164 | } | |
165 | ||
166 | bool BasicBlockPass::doFinalization(Function &) { | |
167 | // By default, don't do anything. | |
168 | return false; | |
169 | } | |
170 | ||
1a4d82fc JJ |
171 | bool BasicBlockPass::skipOptnoneFunction(const BasicBlock &BB) const { |
172 | const Function *F = BB.getParent(); | |
173 | if (F && F->hasFnAttribute(Attribute::OptimizeNone)) { | |
174 | // Report this only once per function. | |
175 | if (&BB == &F->getEntryBlock()) | |
176 | DEBUG(dbgs() << "Skipping pass '" << getPassName() | |
177 | << "' on function " << F->getName() << "\n"); | |
178 | return true; | |
179 | } | |
180 | return false; | |
181 | } | |
182 | ||
223e47cc LB |
183 | PassManagerType BasicBlockPass::getPotentialPassManagerType() const { |
184 | return PMT_BasicBlockPassManager; | |
185 | } | |
186 | ||
187 | const PassInfo *Pass::lookupPassInfo(const void *TI) { | |
188 | return PassRegistry::getPassRegistry()->getPassInfo(TI); | |
189 | } | |
190 | ||
191 | const PassInfo *Pass::lookupPassInfo(StringRef Arg) { | |
192 | return PassRegistry::getPassRegistry()->getPassInfo(Arg); | |
193 | } | |
194 | ||
195 | Pass *Pass::createPass(AnalysisID ID) { | |
196 | const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID); | |
197 | if (!PI) | |
1a4d82fc | 198 | return nullptr; |
223e47cc LB |
199 | return PI->createPass(); |
200 | } | |
201 | ||
223e47cc LB |
202 | //===----------------------------------------------------------------------===// |
203 | // Analysis Group Implementation Code | |
204 | //===----------------------------------------------------------------------===// | |
205 | ||
206 | // RegisterAGBase implementation | |
207 | // | |
208 | RegisterAGBase::RegisterAGBase(const char *Name, const void *InterfaceID, | |
209 | const void *PassID, bool isDefault) | |
210 | : PassInfo(Name, InterfaceID) { | |
211 | PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID, | |
212 | *this, isDefault); | |
213 | } | |
214 | ||
215 | //===----------------------------------------------------------------------===// | |
216 | // PassRegistrationListener implementation | |
217 | // | |
218 | ||
223e47cc LB |
219 | // enumeratePasses - Iterate over the registered passes, calling the |
220 | // passEnumerate callback on each PassInfo object. | |
221 | // | |
222 | void PassRegistrationListener::enumeratePasses() { | |
223 | PassRegistry::getPassRegistry()->enumerateWith(this); | |
224 | } | |
225 | ||
1a4d82fc JJ |
226 | PassNameParser::PassNameParser() |
227 | : Opt(nullptr) { | |
228 | PassRegistry::getPassRegistry()->addRegistrationListener(this); | |
229 | } | |
230 | ||
231 | PassNameParser::~PassNameParser() { | |
232 | // This only gets called during static destruction, in which case the | |
233 | // PassRegistry will have already been destroyed by llvm_shutdown(). So | |
234 | // attempting to remove the registration listener is an error. | |
235 | } | |
223e47cc LB |
236 | |
237 | //===----------------------------------------------------------------------===// | |
238 | // AnalysisUsage Class Implementation | |
239 | // | |
240 | ||
241 | namespace { | |
242 | struct GetCFGOnlyPasses : public PassRegistrationListener { | |
243 | typedef AnalysisUsage::VectorType VectorType; | |
244 | VectorType &CFGOnlyList; | |
245 | GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {} | |
246 | ||
1a4d82fc | 247 | void passEnumerate(const PassInfo *P) override { |
223e47cc LB |
248 | if (P->isCFGOnlyPass()) |
249 | CFGOnlyList.push_back(P->getTypeInfo()); | |
250 | } | |
251 | }; | |
252 | } | |
253 | ||
254 | // setPreservesCFG - This function should be called to by the pass, iff they do | |
255 | // not: | |
256 | // | |
257 | // 1. Add or remove basic blocks from the function | |
258 | // 2. Modify terminator instructions in any way. | |
259 | // | |
260 | // This function annotates the AnalysisUsage info object to say that analyses | |
261 | // that only depend on the CFG are preserved by this pass. | |
262 | // | |
263 | void AnalysisUsage::setPreservesCFG() { | |
264 | // Since this transformation doesn't modify the CFG, it preserves all analyses | |
265 | // that only depend on the CFG (like dominators, loop info, etc...) | |
266 | GetCFGOnlyPasses(Preserved).enumeratePasses(); | |
267 | } | |
268 | ||
269 | AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) { | |
270 | const PassInfo *PI = Pass::lookupPassInfo(Arg); | |
271 | // If the pass exists, preserve it. Otherwise silently do nothing. | |
272 | if (PI) Preserved.push_back(PI->getTypeInfo()); | |
273 | return *this; | |
274 | } | |
275 | ||
276 | AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) { | |
277 | Required.push_back(ID); | |
278 | return *this; | |
279 | } | |
280 | ||
281 | AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) { | |
282 | Required.push_back(&ID); | |
283 | return *this; | |
284 | } | |
285 | ||
286 | AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) { | |
287 | Required.push_back(&ID); | |
288 | RequiredTransitive.push_back(&ID); | |
289 | return *this; | |
290 | } |