• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
55 enum PassManagerType {
56   PMT_Unknown = 0,
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
63   PMT_Last
64 };
65 
66 // Different types of passes.
67 enum PassKind {
68   PT_BasicBlock,
69   PT_Region,
70   PT_Loop,
71   PT_Function,
72   PT_CallGraphSCC,
73   PT_Module,
74   PT_PassManager
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:
Pass(PassKind K,char & pid)90   explicit Pass(PassKind K, char &pid)
91     : Resolver(nullptr), PassID(&pid), Kind(K) { }
92   virtual ~Pass();
93 
94 
getPassKind()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.
getPassID()104   AnalysisID getPassID() const {
105     return PassID;
106   }
107 
108   /// doInitialization - Virtual method overridden by subclasses to do
109   /// any necessary initialization before any pass is run.
110   ///
doInitialization(Module &)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   ///
doFinalization(Module &)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.
assignPassManager(PMStack &,PassManagerType)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.
141   virtual PassManagerType getPotentialPassManagerType() const;
142 
143   // Access AnalysisResolver
144   void setResolver(AnalysisResolver *AR);
getResolver()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.
171   virtual void *getAdjustedAnalysisPointer(AnalysisID ID);
172   virtual ImmutablePass *getAsImmutablePass();
173   virtual PMDataManager *getAsPMDataManager();
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.
239   Pass *createPrinterPass(raw_ostream &O,
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.
249   PassManagerType getPotentialPassManagerType() const override;
250 
ModulePass(char & pid)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 
getAsImmutablePass()272   ImmutablePass *getAsImmutablePass() override { return this; }
273 
274   /// ImmutablePasses are never run.
275   ///
runOnModule(Module &)276   bool runOnModule(Module &) override { return false; }
277 
ImmutablePass(char & pid)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:
FunctionPass(char & pid)296   explicit FunctionPass(char &pid) : Pass(PT_Function, pid) {}
297 
298   /// createPrinterPass - Get a function printer pass.
299   Pass *createPrinterPass(raw_ostream &O,
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.
310   PassManagerType getPotentialPassManagerType() const override;
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:
BasicBlockPass(char & pid)332   explicit BasicBlockPass(char &pid) : Pass(PT_BasicBlock, pid) {}
333 
334   /// createPrinterPass - Get a basic block printer pass.
335   Pass *createPrinterPass(raw_ostream &O,
336                           const std::string &Banner) const override;
337 
338   using llvm::Pass::doInitialization;
339   using llvm::Pass::doFinalization;
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.
359   PassManagerType getPotentialPassManagerType() const override;
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"
378 #include "llvm/PassAnalysisSupport.h"
379 
380 #endif
381