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