LLVM  3.7.0
Pass.h
Go to the documentation of this file.
1 //===- llvm/Pass.h - Base class for Passes ----------------------*- C++ -*-===//
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 defines a base class that indicates that a specified class is a
11 // transformation pass implementation.
12 //
13 // Passes are designed this way so that it is possible to run passes in a cache
14 // and organizationally optimal order without having to specify it at the front
15 // end. This allows arbitrary passes to be strung together and have them
16 // executed as efficiently as possible.
17 //
18 // Passes should extend one of the classes below, depending on the guarantees
19 // that it can make about what will be modified as it is run. For example, most
20 // global optimizations should derive from FunctionPass, because they do not add
21 // or delete functions, they operate on the internals of the function.
22 //
23 // Note that this file #includes PassSupport.h and PassAnalysisSupport.h (at the
24 // bottom), so the APIs exposed by these files are also automatically available
25 // to all users of this file.
26 //
27 //===----------------------------------------------------------------------===//
28 
29 #ifndef LLVM_PASS_H
30 #define LLVM_PASS_H
31 
32 #include "llvm/Support/Compiler.h"
33 #include <string>
34 
35 namespace llvm {
36 
37 class BasicBlock;
38 class Function;
39 class Module;
40 class AnalysisUsage;
41 class PassInfo;
42 class ImmutablePass;
43 class PMStack;
44 class AnalysisResolver;
45 class PMDataManager;
46 class raw_ostream;
47 class StringRef;
48 
49 // AnalysisID - Use the PassInfo to identify a pass...
50 typedef const void* AnalysisID;
51 
52 /// Different types of internal pass managers. External pass managers
53 /// (PassManager and FunctionPassManager) are not represented here.
54 /// Ordering of pass manager types is important here.
57  PMT_ModulePassManager = 1, ///< MPPassManager
58  PMT_CallGraphPassManager, ///< CGPassManager
59  PMT_FunctionPassManager, ///< FPPassManager
60  PMT_LoopPassManager, ///< LPPassManager
61  PMT_RegionPassManager, ///< RGPassManager
62  PMT_BasicBlockPassManager, ///< BBPassManager
64 };
65 
66 // Different types of passes.
67 enum PassKind {
75 };
76 
77 //===----------------------------------------------------------------------===//
78 /// Pass interface - Implemented by all 'passes'. Subclass this if you are an
79 /// interprocedural optimization or you do not fit into any of the more
80 /// constrained passes described below.
81 ///
82 class Pass {
83  AnalysisResolver *Resolver; // Used to resolve analysis
84  const void *PassID;
85  PassKind Kind;
86  void operator=(const Pass&) = delete;
87  Pass(const Pass &) = delete;
88 
89 public:
90  explicit Pass(PassKind K, char &pid)
91  : Resolver(nullptr), PassID(&pid), Kind(K) { }
92  virtual ~Pass();
93 
94 
95  PassKind getPassKind() const { return Kind; }
96 
97  /// getPassName - Return a nice clean name for a pass. This usually
98  /// implemented in terms of the name that is registered by one of the
99  /// Registration templates, but can be overloaded directly.
100  ///
101  virtual const char *getPassName() const;
102 
103  /// getPassID - Return the PassID number that corresponds to this pass.
105  return PassID;
106  }
107 
108  /// doInitialization - Virtual method overridden by subclasses to do
109  /// any necessary initialization before any pass is run.
110  ///
111  virtual bool doInitialization(Module &) { return false; }
112 
113  /// doFinalization - Virtual method overriden by subclasses to do any
114  /// necessary clean up after all passes have run.
115  ///
116  virtual bool doFinalization(Module &) { return false; }
117 
118  /// print - Print out the internal state of the pass. This is called by
119  /// Analyze to print out the contents of an analysis. Otherwise it is not
120  /// necessary to implement this method. Beware that the module pointer MAY be
121  /// null. This automatically forwards to a virtual function that does not
122  /// provide the Module* in case the analysis doesn't need it it can just be
123  /// ignored.
124  ///
125  virtual void print(raw_ostream &O, const Module *M) const;
126  void dump() const; // dump - Print to stderr.
127 
128  /// createPrinterPass - Get a Pass appropriate to print the IR this
129  /// pass operates on (Module, Function or MachineFunction).
130  virtual Pass *createPrinterPass(raw_ostream &O,
131  const std::string &Banner) const = 0;
132 
133  /// Each pass is responsible for assigning a pass manager to itself.
134  /// PMS is the stack of available pass manager.
135  virtual void assignPassManager(PMStack &,
136  PassManagerType) {}
137  /// Check if available pass managers are suitable for this pass or not.
138  virtual void preparePassManager(PMStack &);
139 
140  /// Return what kind of Pass Manager can manage this pass.
142 
143  // Access AnalysisResolver
144  void setResolver(AnalysisResolver *AR);
145  AnalysisResolver *getResolver() const { return Resolver; }
146 
147  /// getAnalysisUsage - This function should be overriden by passes that need
148  /// analysis information to do their job. If a pass specifies that it uses a
149  /// particular analysis result to this function, it can then use the
150  /// getAnalysis<AnalysisType>() function, below.
151  ///
152  virtual void getAnalysisUsage(AnalysisUsage &) const;
153 
154  /// releaseMemory() - This member can be implemented by a pass if it wants to
155  /// be able to release its memory when it is no longer needed. The default
156  /// behavior of passes is to hold onto memory for the entire duration of their
157  /// lifetime (which is the entire compile time). For pipelined passes, this
158  /// is not a big deal because that memory gets recycled every time the pass is
159  /// invoked on another program unit. For IP passes, it is more important to
160  /// free memory when it is unused.
161  ///
162  /// Optionally implement this function to release pass memory when it is no
163  /// longer used.
164  ///
165  virtual void releaseMemory();
166 
167  /// getAdjustedAnalysisPointer - This method is used when a pass implements
168  /// an analysis interface through multiple inheritance. If needed, it should
169  /// override this to adjust the this pointer as needed for the specified pass
170  /// info.
174 
175  /// verifyAnalysis() - This member can be implemented by a analysis pass to
176  /// check state of analysis information.
177  virtual void verifyAnalysis() const;
178 
179  // dumpPassStructure - Implement the -debug-passes=PassStructure option
180  virtual void dumpPassStructure(unsigned Offset = 0);
181 
182  // lookupPassInfo - Return the pass info object for the specified pass class,
183  // or null if it is not known.
184  static const PassInfo *lookupPassInfo(const void *TI);
185 
186  // lookupPassInfo - Return the pass info object for the pass with the given
187  // argument string, or null if it is not known.
188  static const PassInfo *lookupPassInfo(StringRef Arg);
189 
190  // createPass - Create a object for the specified pass class,
191  // or null if it is not known.
192  static Pass *createPass(AnalysisID ID);
193 
194  /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
195  /// get analysis information that might be around, for example to update it.
196  /// This is different than getAnalysis in that it can fail (if the analysis
197  /// results haven't been computed), so should only be used if you can handle
198  /// the case when the analysis is not available. This method is often used by
199  /// transformation APIs to update analysis results for a pass automatically as
200  /// the transform is performed.
201  ///
202  template<typename AnalysisType> AnalysisType *
203  getAnalysisIfAvailable() const; // Defined in PassAnalysisSupport.h
204 
205  /// mustPreserveAnalysisID - This method serves the same function as
206  /// getAnalysisIfAvailable, but works if you just have an AnalysisID. This
207  /// obviously cannot give you a properly typed instance of the class if you
208  /// don't have the class name available (use getAnalysisIfAvailable if you
209  /// do), but it can tell you if you need to preserve the pass at least.
210  ///
211  bool mustPreserveAnalysisID(char &AID) const;
212 
213  /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
214  /// to the analysis information that they claim to use by overriding the
215  /// getAnalysisUsage function.
216  ///
217  template<typename AnalysisType>
218  AnalysisType &getAnalysis() const; // Defined in PassAnalysisSupport.h
219 
220  template<typename AnalysisType>
221  AnalysisType &getAnalysis(Function &F); // Defined in PassAnalysisSupport.h
222 
223  template<typename AnalysisType>
224  AnalysisType &getAnalysisID(AnalysisID PI) const;
225 
226  template<typename AnalysisType>
227  AnalysisType &getAnalysisID(AnalysisID PI, Function &F);
228 };
229 
230 
231 //===----------------------------------------------------------------------===//
232 /// ModulePass class - This class is used to implement unstructured
233 /// interprocedural optimizations and analyses. ModulePasses may do anything
234 /// they want to the program.
235 ///
236 class ModulePass : public Pass {
237 public:
238  /// createPrinterPass - Get a module printer pass.
240  const std::string &Banner) const override;
241 
242  /// runOnModule - Virtual method overriden by subclasses to process the module
243  /// being operated on.
244  virtual bool runOnModule(Module &M) = 0;
245 
246  void assignPassManager(PMStack &PMS, PassManagerType T) override;
247 
248  /// Return what kind of Pass Manager can manage this pass.
250 
251  explicit ModulePass(char &pid) : Pass(PT_Module, pid) {}
252  // Force out-of-line virtual method.
253  ~ModulePass() override;
254 };
255 
256 
257 //===----------------------------------------------------------------------===//
258 /// ImmutablePass class - This class is used to provide information that does
259 /// not need to be run. This is useful for things like target information and
260 /// "basic" versions of AnalysisGroups.
261 ///
262 class ImmutablePass : public ModulePass {
263 public:
264  /// initializePass - This method may be overriden by immutable passes to allow
265  /// them to perform various initialization actions they require. This is
266  /// primarily because an ImmutablePass can "require" another ImmutablePass,
267  /// and if it does, the overloaded version of initializePass may get access to
268  /// these passes with getAnalysis<>.
269  ///
270  virtual void initializePass();
271 
272  ImmutablePass *getAsImmutablePass() override { return this; }
273 
274  /// ImmutablePasses are never run.
275  ///
276  bool runOnModule(Module &) override { return false; }
277 
278  explicit ImmutablePass(char &pid)
279  : ModulePass(pid) {}
280 
281  // Force out-of-line virtual method.
282  ~ImmutablePass() override;
283 };
284 
285 //===----------------------------------------------------------------------===//
286 /// FunctionPass class - This class is used to implement most global
287 /// optimizations. Optimizations should subclass this class if they meet the
288 /// following constraints:
289 ///
290 /// 1. Optimizations are organized globally, i.e., a function at a time
291 /// 2. Optimizing a function does not cause the addition or removal of any
292 /// functions in the module
293 ///
294 class FunctionPass : public Pass {
295 public:
296  explicit FunctionPass(char &pid) : Pass(PT_Function, pid) {}
297 
298  /// createPrinterPass - Get a function printer pass.
300  const std::string &Banner) const override;
301 
302  /// runOnFunction - Virtual method overriden by subclasses to do the
303  /// per-function processing of the pass.
304  ///
305  virtual bool runOnFunction(Function &F) = 0;
306 
307  void assignPassManager(PMStack &PMS, PassManagerType T) override;
308 
309  /// Return what kind of Pass Manager can manage this pass.
311 
312 protected:
313  /// skipOptnoneFunction - This function has Attribute::OptimizeNone
314  /// and most transformation passes should skip it.
315  bool skipOptnoneFunction(const Function &F) const;
316 };
317 
318 
319 
320 //===----------------------------------------------------------------------===//
321 /// BasicBlockPass class - This class is used to implement most local
322 /// optimizations. Optimizations should subclass this class if they
323 /// meet the following constraints:
324 /// 1. Optimizations are local, operating on either a basic block or
325 /// instruction at a time.
326 /// 2. Optimizations do not modify the CFG of the contained function, or any
327 /// other basic block in the function.
328 /// 3. Optimizations conform to all of the constraints of FunctionPasses.
329 ///
330 class BasicBlockPass : public Pass {
331 public:
332  explicit BasicBlockPass(char &pid) : Pass(PT_BasicBlock, pid) {}
333 
334  /// createPrinterPass - Get a basic block printer pass.
336  const std::string &Banner) const override;
337 
340 
341  /// doInitialization - Virtual method overridden by BasicBlockPass subclasses
342  /// to do any necessary per-function initialization.
343  ///
344  virtual bool doInitialization(Function &);
345 
346  /// runOnBasicBlock - Virtual method overriden by subclasses to do the
347  /// per-basicblock processing of the pass.
348  ///
349  virtual bool runOnBasicBlock(BasicBlock &BB) = 0;
350 
351  /// doFinalization - Virtual method overriden by BasicBlockPass subclasses to
352  /// do any post processing needed after all passes have run.
353  ///
354  virtual bool doFinalization(Function &);
355 
356  void assignPassManager(PMStack &PMS, PassManagerType T) override;
357 
358  /// Return what kind of Pass Manager can manage this pass.
360 
361 protected:
362  /// skipOptnoneFunction - Containing function has Attribute::OptimizeNone
363  /// and most transformation passes should skip it.
364  bool skipOptnoneFunction(const BasicBlock &BB) const;
365 };
366 
367 /// If the user specifies the -time-passes argument on an LLVM tool command line
368 /// then the value of this boolean will be true, otherwise false.
369 /// @brief This is the storage for the -time-passes option.
370 extern bool TimePassesIsEnabled;
371 
372 } // End llvm namespace
373 
374 // Include support files that contain important APIs commonly used by Passes,
375 // but that we want to separate out to make it easier to read the header files.
376 //
377 #include "llvm/PassSupport.h"
379 
380 #endif
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:82
PassManagerType
Different types of internal pass managers.
Definition: Pass.h:55
BBPassManager.
Definition: Pass.h:62
CGPassManager.
Definition: Pass.h:58
virtual Pass * createPrinterPass(raw_ostream &O, const std::string &Banner) const =0
createPrinterPass - Get a Pass appropriate to print the IR this pass operates on (Module, Function or MachineFunction).
virtual PMDataManager * getAsPMDataManager()
Definition: Pass.cpp:98
Various leaf nodes.
Definition: ISDOpcodes.h:60
virtual void dumpPassStructure(unsigned Offset=0)
Definition: Pass.cpp:53
virtual bool runOnModule(Module &M)=0
runOnModule - Virtual method overriden by subclasses to process the module being operated on...
AnalysisType & getAnalysisID(AnalysisID PI) const
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
AnalysisID getPassID() const
getPassID - Return the PassID number that corresponds to this pass.
Definition: Pass.h:104
virtual void releaseMemory()
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
Definition: Pass.cpp:82
void assignPassManager(PMStack &PMS, PassManagerType T) override
Find appropriate Function Pass Manager or Call Graph Pass Manager in the PM Stack and add self into t...
virtual void verifyAnalysis() const
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
Definition: Pass.cpp:86
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:78
virtual bool doInitialization(Function &)
doInitialization - Virtual method overridden by BasicBlockPass subclasses to do any necessary per-fun...
Definition: Pass.cpp:161
virtual const char * getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:61
F(f)
virtual void preparePassManager(PMStack &)
Check if available pass managers are suitable for this pass or not.
Definition: Pass.cpp:69
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:44
virtual void initializePass()
initializePass - This method may be overriden by immutable passes to allow them to perform various in...
Definition: Pass.cpp:126
void setResolver(AnalysisResolver *AR)
Definition: Pass.cpp:102
Pass * createPrinterPass(raw_ostream &O, const std::string &Banner) const override
createPrinterPass - Get a basic block printer pass.
Definition: Pass.cpp:156
AnalysisType * getAnalysisIfAvailable() const
getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to get analysis information tha...
virtual bool doFinalization(Module &)
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
Definition: Pass.h:116
bool mustPreserveAnalysisID(char &AID) const
mustPreserveAnalysisID - This method serves the same function as getAnalysisIfAvailable, but works if you just have an AnalysisID.
Definition: Pass.cpp:48
ModulePass(char &pid)
Definition: Pass.h:251
MPPassManager.
Definition: Pass.h:57
PMStack - This class implements a stack data structure of PMDataManager pointers. ...
void assignPassManager(PMStack &PMS, PassManagerType T) override
Find appropriate Module Pass Manager in the PM Stack and add self into that manager.
bool runOnModule(Module &) override
ImmutablePasses are never run.
Definition: Pass.h:276
PassKind getPassKind() const
Definition: Pass.h:95
virtual bool doFinalization(Function &)
doFinalization - Virtual method overriden by BasicBlockPass subclasses to do any post processing need...
Definition: Pass.cpp:166
virtual ImmutablePass * getAsImmutablePass()
Definition: Pass.cpp:94
static const PassInfo * lookupPassInfo(const void *TI)
Definition: Pass.cpp:187
virtual bool doInitialization(Module &)
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
Definition: Pass.h:111
PassKind
Definition: Pass.h:67
bool skipOptnoneFunction(const BasicBlock &BB) const
skipOptnoneFunction - Containing function has Attribute::OptimizeNone and most transformation passes ...
Definition: Pass.cpp:171
void assignPassManager(PMStack &PMS, PassManagerType T) override
Find appropriate Basic Pass Manager or Call Graph Pass Manager in the PM Stack and add self into that...
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:139
AnalysisResolver * getResolver() const
Definition: Pass.h:145
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
PassInfo class - An instance of this class exists for every pass known by the system, and can be obtained from a live Pass by calling its getPassInfo() method.
Definition: PassInfo.h:30
BasicBlockPass class - This class is used to implement most local optimizations.
Definition: Pass.h:330
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
LPPassManager.
Definition: Pass.h:60
ImmutablePass class - This class is used to provide information that does not need to be run...
Definition: Pass.h:262
FPPassManager.
Definition: Pass.h:59
Pass(PassKind K, char &pid)
Definition: Pass.h:90
bool skipOptnoneFunction(const Function &F) const
skipOptnoneFunction - This function has Attribute::OptimizeNone and most transformation passes should...
Definition: Pass.cpp:143
ImmutablePass(char &pid)
Definition: Pass.h:278
BasicBlockPass(char &pid)
Definition: Pass.h:332
virtual ~Pass()
Definition: Pass.cpp:32
Pass * createPrinterPass(raw_ostream &O, const std::string &Banner) const override
createPrinterPass - Get a module printer pass.
Definition: Pass.cpp:39
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:236
RGPassManager.
Definition: Pass.h:61
~ImmutablePass() override
Definition: Pass.cpp:124
const void * AnalysisID
Definition: Pass.h:47
PMDataManager provides the common place to manage the analysis data used by pass managers.
static Pass * createPass(AnalysisID ID)
Definition: Pass.cpp:195
~ModulePass() override
Definition: Pass.cpp:37
FunctionPass(char &pid)
Definition: Pass.h:296
Pass * createPrinterPass(raw_ostream &O, const std::string &Banner) const override
createPrinterPass - Get a function printer pass.
Definition: Pass.cpp:134
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
virtual bool runOnBasicBlock(BasicBlock &BB)=0
runOnBasicBlock - Virtual method overriden by subclasses to do the per-basicblock processing of the p...
void dump() const
Definition: Pass.cpp:116
virtual void * getAdjustedAnalysisPointer(AnalysisID ID)
getAdjustedAnalysisPointer - This method is used when a pass implements an analysis interface through...
Definition: Pass.cpp:90
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
ImmutablePass * getAsImmutablePass() override
Definition: Pass.h:272
virtual void print(raw_ostream &O, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:111
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:183
bool TimePassesIsEnabled
If the user specifies the -time-passes argument on an LLVM tool command line then the value of this b...
Definition: IRReader.cpp:26
virtual PassManagerType getPotentialPassManagerType() const
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:73
virtual void assignPassManager(PMStack &, PassManagerType)
Each pass is responsible for assigning a pass manager to itself.
Definition: Pass.h:135