Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

369 lines
14 KiB

  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. #ifndef LLVM_PASS_H
  29. #define LLVM_PASS_H
  30. #include "llvm/Support/Compiler.h"
  31. #include <string>
  32. namespace llvm {
  33. class BasicBlock;
  34. class Function;
  35. class Module;
  36. class AnalysisUsage;
  37. class PassInfo;
  38. class ImmutablePass;
  39. class PMStack;
  40. class AnalysisResolver;
  41. class PMDataManager;
  42. class raw_ostream;
  43. class StringRef;
  44. // AnalysisID - Use the PassInfo to identify a pass...
  45. typedef const void* AnalysisID;
  46. /// Different types of internal pass managers. External pass managers
  47. /// (PassManager and FunctionPassManager) are not represented here.
  48. /// Ordering of pass manager types is important here.
  49. enum PassManagerType {
  50. PMT_Unknown = 0,
  51. PMT_ModulePassManager = 1, ///< MPPassManager
  52. PMT_CallGraphPassManager, ///< CGPassManager
  53. PMT_FunctionPassManager, ///< FPPassManager
  54. PMT_LoopPassManager, ///< LPPassManager
  55. PMT_RegionPassManager, ///< RGPassManager
  56. PMT_BasicBlockPassManager, ///< BBPassManager
  57. PMT_Last
  58. };
  59. // Different types of passes.
  60. enum PassKind {
  61. PT_BasicBlock,
  62. PT_Region,
  63. PT_Loop,
  64. PT_Function,
  65. PT_CallGraphSCC,
  66. PT_Module,
  67. PT_PassManager
  68. };
  69. //===----------------------------------------------------------------------===//
  70. /// Pass interface - Implemented by all 'passes'. Subclass this if you are an
  71. /// interprocedural optimization or you do not fit into any of the more
  72. /// constrained passes described below.
  73. ///
  74. class Pass {
  75. AnalysisResolver *Resolver; // Used to resolve analysis
  76. const void *PassID;
  77. PassKind Kind;
  78. void operator=(const Pass&) LLVM_DELETED_FUNCTION;
  79. Pass(const Pass &) LLVM_DELETED_FUNCTION;
  80. public:
  81. explicit Pass(PassKind K, char &pid) : Resolver(0), PassID(&pid), Kind(K) { }
  82. virtual ~Pass();
  83. PassKind getPassKind() const { return Kind; }
  84. /// getPassName - Return a nice clean name for a pass. This usually
  85. /// implemented in terms of the name that is registered by one of the
  86. /// Registration templates, but can be overloaded directly.
  87. ///
  88. virtual const char *getPassName() const;
  89. /// getPassID - Return the PassID number that corresponds to this pass.
  90. AnalysisID getPassID() const {
  91. return PassID;
  92. }
  93. /// doInitialization - Virtual method overridden by subclasses to do
  94. /// any necessary initialization before any pass is run.
  95. ///
  96. virtual bool doInitialization(Module &) { return false; }
  97. /// doFinalization - Virtual method overriden by subclasses to do any
  98. /// necessary clean up after all passes have run.
  99. ///
  100. virtual bool doFinalization(Module &) { return false; }
  101. /// print - Print out the internal state of the pass. This is called by
  102. /// Analyze to print out the contents of an analysis. Otherwise it is not
  103. /// necessary to implement this method. Beware that the module pointer MAY be
  104. /// null. This automatically forwards to a virtual function that does not
  105. /// provide the Module* in case the analysis doesn't need it it can just be
  106. /// ignored.
  107. ///
  108. virtual void print(raw_ostream &O, const Module *M) const;
  109. void dump() const; // dump - Print to stderr.
  110. /// createPrinterPass - Get a Pass appropriate to print the IR this
  111. /// pass operates on (Module, Function or MachineFunction).
  112. virtual Pass *createPrinterPass(raw_ostream &O,
  113. const std::string &Banner) const = 0;
  114. /// Each pass is responsible for assigning a pass manager to itself.
  115. /// PMS is the stack of available pass manager.
  116. virtual void assignPassManager(PMStack &,
  117. PassManagerType) {}
  118. /// Check if available pass managers are suitable for this pass or not.
  119. virtual void preparePassManager(PMStack &);
  120. /// Return what kind of Pass Manager can manage this pass.
  121. virtual PassManagerType getPotentialPassManagerType() const;
  122. // Access AnalysisResolver
  123. void setResolver(AnalysisResolver *AR);
  124. AnalysisResolver *getResolver() const { return Resolver; }
  125. /// getAnalysisUsage - This function should be overriden by passes that need
  126. /// analysis information to do their job. If a pass specifies that it uses a
  127. /// particular analysis result to this function, it can then use the
  128. /// getAnalysis<AnalysisType>() function, below.
  129. ///
  130. virtual void getAnalysisUsage(AnalysisUsage &) const;
  131. /// releaseMemory() - This member can be implemented by a pass if it wants to
  132. /// be able to release its memory when it is no longer needed. The default
  133. /// behavior of passes is to hold onto memory for the entire duration of their
  134. /// lifetime (which is the entire compile time). For pipelined passes, this
  135. /// is not a big deal because that memory gets recycled every time the pass is
  136. /// invoked on another program unit. For IP passes, it is more important to
  137. /// free memory when it is unused.
  138. ///
  139. /// Optionally implement this function to release pass memory when it is no
  140. /// longer used.
  141. ///
  142. virtual void releaseMemory();
  143. /// getAdjustedAnalysisPointer - This method is used when a pass implements
  144. /// an analysis interface through multiple inheritance. If needed, it should
  145. /// override this to adjust the this pointer as needed for the specified pass
  146. /// info.
  147. virtual void *getAdjustedAnalysisPointer(AnalysisID ID);
  148. virtual ImmutablePass *getAsImmutablePass();
  149. virtual PMDataManager *getAsPMDataManager();
  150. /// verifyAnalysis() - This member can be implemented by a analysis pass to
  151. /// check state of analysis information.
  152. virtual void verifyAnalysis() const;
  153. // dumpPassStructure - Implement the -debug-passes=PassStructure option
  154. virtual void dumpPassStructure(unsigned Offset = 0);
  155. // lookupPassInfo - Return the pass info object for the specified pass class,
  156. // or null if it is not known.
  157. static const PassInfo *lookupPassInfo(const void *TI);
  158. // lookupPassInfo - Return the pass info object for the pass with the given
  159. // argument string, or null if it is not known.
  160. static const PassInfo *lookupPassInfo(StringRef Arg);
  161. // createPass - Create a object for the specified pass class,
  162. // or null if it is not known.
  163. static Pass *createPass(AnalysisID ID);
  164. /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
  165. /// get analysis information that might be around, for example to update it.
  166. /// This is different than getAnalysis in that it can fail (if the analysis
  167. /// results haven't been computed), so should only be used if you can handle
  168. /// the case when the analysis is not available. This method is often used by
  169. /// transformation APIs to update analysis results for a pass automatically as
  170. /// the transform is performed.
  171. ///
  172. template<typename AnalysisType> AnalysisType *
  173. getAnalysisIfAvailable() const; // Defined in PassAnalysisSupport.h
  174. /// mustPreserveAnalysisID - This method serves the same function as
  175. /// getAnalysisIfAvailable, but works if you just have an AnalysisID. This
  176. /// obviously cannot give you a properly typed instance of the class if you
  177. /// don't have the class name available (use getAnalysisIfAvailable if you
  178. /// do), but it can tell you if you need to preserve the pass at least.
  179. ///
  180. bool mustPreserveAnalysisID(char &AID) const;
  181. /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
  182. /// to the analysis information that they claim to use by overriding the
  183. /// getAnalysisUsage function.
  184. ///
  185. template<typename AnalysisType>
  186. AnalysisType &getAnalysis() const; // Defined in PassAnalysisSupport.h
  187. template<typename AnalysisType>
  188. AnalysisType &getAnalysis(Function &F); // Defined in PassAnalysisSupport.h
  189. template<typename AnalysisType>
  190. AnalysisType &getAnalysisID(AnalysisID PI) const;
  191. template<typename AnalysisType>
  192. AnalysisType &getAnalysisID(AnalysisID PI, Function &F);
  193. };
  194. //===----------------------------------------------------------------------===//
  195. /// ModulePass class - This class is used to implement unstructured
  196. /// interprocedural optimizations and analyses. ModulePasses may do anything
  197. /// they want to the program.
  198. ///
  199. class ModulePass : public Pass {
  200. public:
  201. /// createPrinterPass - Get a module printer pass.
  202. Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
  203. /// runOnModule - Virtual method overriden by subclasses to process the module
  204. /// being operated on.
  205. virtual bool runOnModule(Module &M) = 0;
  206. virtual void assignPassManager(PMStack &PMS,
  207. PassManagerType T);
  208. /// Return what kind of Pass Manager can manage this pass.
  209. virtual PassManagerType getPotentialPassManagerType() const;
  210. explicit ModulePass(char &pid) : Pass(PT_Module, pid) {}
  211. // Force out-of-line virtual method.
  212. virtual ~ModulePass();
  213. };
  214. //===----------------------------------------------------------------------===//
  215. /// ImmutablePass class - This class is used to provide information that does
  216. /// not need to be run. This is useful for things like target information and
  217. /// "basic" versions of AnalysisGroups.
  218. ///
  219. class ImmutablePass : public ModulePass {
  220. public:
  221. /// initializePass - This method may be overriden by immutable passes to allow
  222. /// them to perform various initialization actions they require. This is
  223. /// primarily because an ImmutablePass can "require" another ImmutablePass,
  224. /// and if it does, the overloaded version of initializePass may get access to
  225. /// these passes with getAnalysis<>.
  226. ///
  227. virtual void initializePass();
  228. virtual ImmutablePass *getAsImmutablePass() { return this; }
  229. /// ImmutablePasses are never run.
  230. ///
  231. bool runOnModule(Module &) { return false; }
  232. explicit ImmutablePass(char &pid)
  233. : ModulePass(pid) {}
  234. // Force out-of-line virtual method.
  235. virtual ~ImmutablePass();
  236. };
  237. //===----------------------------------------------------------------------===//
  238. /// FunctionPass class - This class is used to implement most global
  239. /// optimizations. Optimizations should subclass this class if they meet the
  240. /// following constraints:
  241. ///
  242. /// 1. Optimizations are organized globally, i.e., a function at a time
  243. /// 2. Optimizing a function does not cause the addition or removal of any
  244. /// functions in the module
  245. ///
  246. class FunctionPass : public Pass {
  247. public:
  248. explicit FunctionPass(char &pid) : Pass(PT_Function, pid) {}
  249. /// createPrinterPass - Get a function printer pass.
  250. Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
  251. /// runOnFunction - Virtual method overriden by subclasses to do the
  252. /// per-function processing of the pass.
  253. ///
  254. virtual bool runOnFunction(Function &F) = 0;
  255. virtual void assignPassManager(PMStack &PMS,
  256. PassManagerType T);
  257. /// Return what kind of Pass Manager can manage this pass.
  258. virtual PassManagerType getPotentialPassManagerType() const;
  259. };
  260. //===----------------------------------------------------------------------===//
  261. /// BasicBlockPass class - This class is used to implement most local
  262. /// optimizations. Optimizations should subclass this class if they
  263. /// meet the following constraints:
  264. /// 1. Optimizations are local, operating on either a basic block or
  265. /// instruction at a time.
  266. /// 2. Optimizations do not modify the CFG of the contained function, or any
  267. /// other basic block in the function.
  268. /// 3. Optimizations conform to all of the constraints of FunctionPasses.
  269. ///
  270. class BasicBlockPass : public Pass {
  271. public:
  272. explicit BasicBlockPass(char &pid) : Pass(PT_BasicBlock, pid) {}
  273. /// createPrinterPass - Get a basic block printer pass.
  274. Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
  275. using llvm::Pass::doInitialization;
  276. using llvm::Pass::doFinalization;
  277. /// doInitialization - Virtual method overridden by BasicBlockPass subclasses
  278. /// to do any necessary per-function initialization.
  279. ///
  280. virtual bool doInitialization(Function &);
  281. /// runOnBasicBlock - Virtual method overriden by subclasses to do the
  282. /// per-basicblock processing of the pass.
  283. ///
  284. virtual bool runOnBasicBlock(BasicBlock &BB) = 0;
  285. /// doFinalization - Virtual method overriden by BasicBlockPass subclasses to
  286. /// do any post processing needed after all passes have run.
  287. ///
  288. virtual bool doFinalization(Function &);
  289. virtual void assignPassManager(PMStack &PMS,
  290. PassManagerType T);
  291. /// Return what kind of Pass Manager can manage this pass.
  292. virtual PassManagerType getPotentialPassManagerType() const;
  293. };
  294. /// If the user specifies the -time-passes argument on an LLVM tool command line
  295. /// then the value of this boolean will be true, otherwise false.
  296. /// @brief This is the storage for the -time-passes option.
  297. extern bool TimePassesIsEnabled;
  298. } // End llvm namespace
  299. // Include support files that contain important APIs commonly used by Passes,
  300. // but that we want to separate out to make it easier to read the header files.
  301. //
  302. #include "llvm/PassSupport.h"
  303. #include "llvm/PassAnalysisSupport.h"
  304. #endif