Pass.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/Pass.h - Base class for Passes ----------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines a base class that indicates that a specified class is a
  15. // transformation pass implementation.
  16. //
  17. // Passes are designed this way so that it is possible to run passes in a cache
  18. // and organizationally optimal order without having to specify it at the front
  19. // end. This allows arbitrary passes to be strung together and have them
  20. // executed as efficiently as possible.
  21. //
  22. // Passes should extend one of the classes below, depending on the guarantees
  23. // that it can make about what will be modified as it is run. For example, most
  24. // global optimizations should derive from FunctionPass, because they do not add
  25. // or delete functions, they operate on the internals of the function.
  26. //
  27. // Note that this file #includes PassSupport.h and PassAnalysisSupport.h (at the
  28. // bottom), so the APIs exposed by these files are also automatically available
  29. // to all users of this file.
  30. //
  31. //===----------------------------------------------------------------------===//
  32. #ifndef LLVM_PASS_H
  33. #define LLVM_PASS_H
  34. #include <string>
  35. namespace llvm {
  36. class AnalysisResolver;
  37. class AnalysisUsage;
  38. class Function;
  39. class ImmutablePass;
  40. class Module;
  41. class PassInfo;
  42. class PMDataManager;
  43. class PMStack;
  44. class raw_ostream;
  45. class StringRef;
  46. // AnalysisID - Use the PassInfo to identify a pass...
  47. using AnalysisID = const void *;
  48. /// Different types of internal pass managers. External pass managers
  49. /// (PassManager and FunctionPassManager) are not represented here.
  50. /// Ordering of pass manager types is important here.
  51. enum PassManagerType {
  52. PMT_Unknown = 0,
  53. PMT_ModulePassManager = 1, ///< MPPassManager
  54. PMT_CallGraphPassManager, ///< CGPassManager
  55. PMT_FunctionPassManager, ///< FPPassManager
  56. PMT_LoopPassManager, ///< LPPassManager
  57. PMT_RegionPassManager, ///< RGPassManager
  58. PMT_Last
  59. };
  60. // Different types of passes.
  61. enum PassKind {
  62. PT_Region,
  63. PT_Loop,
  64. PT_Function,
  65. PT_CallGraphSCC,
  66. PT_Module,
  67. PT_PassManager
  68. };
  69. /// This enumerates the LLVM full LTO or ThinLTO optimization phases.
  70. enum class ThinOrFullLTOPhase {
  71. /// No LTO/ThinLTO behavior needed.
  72. None,
  73. /// ThinLTO prelink (summary) phase.
  74. ThinLTOPreLink,
  75. /// ThinLTO postlink (backend compile) phase.
  76. ThinLTOPostLink,
  77. /// Full LTO prelink phase.
  78. FullLTOPreLink,
  79. /// Full LTO postlink (backend compile) phase.
  80. FullLTOPostLink
  81. };
  82. //===----------------------------------------------------------------------===//
  83. /// Pass interface - Implemented by all 'passes'. Subclass this if you are an
  84. /// interprocedural optimization or you do not fit into any of the more
  85. /// constrained passes described below.
  86. ///
  87. class Pass {
  88. AnalysisResolver *Resolver = nullptr; // Used to resolve analysis
  89. const void *PassID;
  90. PassKind Kind;
  91. public:
  92. explicit Pass(PassKind K, char &pid) : PassID(&pid), Kind(K) {}
  93. Pass(const Pass &) = delete;
  94. Pass &operator=(const Pass &) = delete;
  95. virtual ~Pass();
  96. PassKind getPassKind() const { return Kind; }
  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. virtual StringRef getPassName() const;
  101. /// getPassID - Return the PassID number that corresponds to this pass.
  102. AnalysisID getPassID() const {
  103. return PassID;
  104. }
  105. /// doInitialization - Virtual method overridden by subclasses to do
  106. /// any necessary initialization before any pass is run.
  107. virtual bool doInitialization(Module &) { return false; }
  108. /// doFinalization - Virtual method overriden by subclasses to do any
  109. /// necessary clean up after all passes have run.
  110. virtual bool doFinalization(Module &) { return false; }
  111. /// print - Print out the internal state of the pass. This is called by
  112. /// Analyze to print out the contents of an analysis. Otherwise it is not
  113. /// necessary to implement this method. Beware that the module pointer MAY be
  114. /// null. This automatically forwards to a virtual function that does not
  115. /// provide the Module* in case the analysis doesn't need it it can just be
  116. /// ignored.
  117. virtual void print(raw_ostream &OS, const Module *M) const;
  118. void dump() const; // dump - Print to stderr.
  119. /// createPrinterPass - Get a Pass appropriate to print the IR this
  120. /// pass operates on (Module, Function or MachineFunction).
  121. virtual Pass *createPrinterPass(raw_ostream &OS,
  122. const std::string &Banner) const = 0;
  123. /// Each pass is responsible for assigning a pass manager to itself.
  124. /// PMS is the stack of available pass manager.
  125. virtual void assignPassManager(PMStack &,
  126. PassManagerType) {}
  127. /// Check if available pass managers are suitable for this pass or not.
  128. virtual void preparePassManager(PMStack &);
  129. /// Return what kind of Pass Manager can manage this pass.
  130. virtual PassManagerType getPotentialPassManagerType() const;
  131. // Access AnalysisResolver
  132. void setResolver(AnalysisResolver *AR);
  133. AnalysisResolver *getResolver() const { return Resolver; }
  134. /// getAnalysisUsage - This function should be overriden by passes that need
  135. /// analysis information to do their job. If a pass specifies that it uses a
  136. /// particular analysis result to this function, it can then use the
  137. /// getAnalysis<AnalysisType>() function, below.
  138. virtual void getAnalysisUsage(AnalysisUsage &) const;
  139. /// releaseMemory() - This member can be implemented by a pass if it wants to
  140. /// be able to release its memory when it is no longer needed. The default
  141. /// behavior of passes is to hold onto memory for the entire duration of their
  142. /// lifetime (which is the entire compile time). For pipelined passes, this
  143. /// is not a big deal because that memory gets recycled every time the pass is
  144. /// invoked on another program unit. For IP passes, it is more important to
  145. /// free memory when it is unused.
  146. ///
  147. /// Optionally implement this function to release pass memory when it is no
  148. /// longer used.
  149. virtual void releaseMemory();
  150. /// getAdjustedAnalysisPointer - This method is used when a pass implements
  151. /// an analysis interface through multiple inheritance. If needed, it should
  152. /// override this to adjust the this pointer as needed for the specified pass
  153. /// info.
  154. virtual void *getAdjustedAnalysisPointer(AnalysisID ID);
  155. virtual ImmutablePass *getAsImmutablePass();
  156. virtual PMDataManager *getAsPMDataManager();
  157. /// verifyAnalysis() - This member can be implemented by a analysis pass to
  158. /// check state of analysis information.
  159. virtual void verifyAnalysis() const;
  160. // dumpPassStructure - Implement the -debug-passes=PassStructure option
  161. virtual void dumpPassStructure(unsigned Offset = 0);
  162. // lookupPassInfo - Return the pass info object for the specified pass class,
  163. // or null if it is not known.
  164. static const PassInfo *lookupPassInfo(const void *TI);
  165. // lookupPassInfo - Return the pass info object for the pass with the given
  166. // argument string, or null if it is not known.
  167. static const PassInfo *lookupPassInfo(StringRef Arg);
  168. // createPass - Create a object for the specified pass class,
  169. // or null if it is not known.
  170. static Pass *createPass(AnalysisID ID);
  171. /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
  172. /// get analysis information that might be around, for example to update it.
  173. /// This is different than getAnalysis in that it can fail (if the analysis
  174. /// results haven't been computed), so should only be used if you can handle
  175. /// the case when the analysis is not available. This method is often used by
  176. /// transformation APIs to update analysis results for a pass automatically as
  177. /// the transform is performed.
  178. template<typename AnalysisType> AnalysisType *
  179. getAnalysisIfAvailable() const; // Defined in PassAnalysisSupport.h
  180. /// mustPreserveAnalysisID - This method serves the same function as
  181. /// getAnalysisIfAvailable, but works if you just have an AnalysisID. This
  182. /// obviously cannot give you a properly typed instance of the class if you
  183. /// don't have the class name available (use getAnalysisIfAvailable if you
  184. /// do), but it can tell you if you need to preserve the pass at least.
  185. bool mustPreserveAnalysisID(char &AID) const;
  186. /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
  187. /// to the analysis information that they claim to use by overriding the
  188. /// getAnalysisUsage function.
  189. template<typename AnalysisType>
  190. AnalysisType &getAnalysis() const; // Defined in PassAnalysisSupport.h
  191. template <typename AnalysisType>
  192. AnalysisType &
  193. getAnalysis(Function &F,
  194. bool *Changed = nullptr); // Defined in PassAnalysisSupport.h
  195. template<typename AnalysisType>
  196. AnalysisType &getAnalysisID(AnalysisID PI) const;
  197. template <typename AnalysisType>
  198. AnalysisType &getAnalysisID(AnalysisID PI, Function &F,
  199. bool *Changed = nullptr);
  200. };
  201. //===----------------------------------------------------------------------===//
  202. /// ModulePass class - This class is used to implement unstructured
  203. /// interprocedural optimizations and analyses. ModulePasses may do anything
  204. /// they want to the program.
  205. ///
  206. class ModulePass : public Pass {
  207. public:
  208. explicit ModulePass(char &pid) : Pass(PT_Module, pid) {}
  209. // Force out-of-line virtual method.
  210. ~ModulePass() override;
  211. /// createPrinterPass - Get a module printer pass.
  212. Pass *createPrinterPass(raw_ostream &OS,
  213. const std::string &Banner) const override;
  214. /// runOnModule - Virtual method overriden by subclasses to process the module
  215. /// being operated on.
  216. virtual bool runOnModule(Module &M) = 0;
  217. void assignPassManager(PMStack &PMS, PassManagerType T) override;
  218. /// Return what kind of Pass Manager can manage this pass.
  219. PassManagerType getPotentialPassManagerType() const override;
  220. protected:
  221. /// Optional passes call this function to check whether the pass should be
  222. /// skipped. This is the case when optimization bisect is over the limit.
  223. bool skipModule(Module &M) const;
  224. };
  225. //===----------------------------------------------------------------------===//
  226. /// ImmutablePass class - This class is used to provide information that does
  227. /// not need to be run. This is useful for things like target information and
  228. /// "basic" versions of AnalysisGroups.
  229. ///
  230. class ImmutablePass : public ModulePass {
  231. public:
  232. explicit ImmutablePass(char &pid) : ModulePass(pid) {}
  233. // Force out-of-line virtual method.
  234. ~ImmutablePass() override;
  235. /// initializePass - This method may be overriden by immutable passes to allow
  236. /// them to perform various initialization actions they require. This is
  237. /// primarily because an ImmutablePass can "require" another ImmutablePass,
  238. /// and if it does, the overloaded version of initializePass may get access to
  239. /// these passes with getAnalysis<>.
  240. virtual void initializePass();
  241. ImmutablePass *getAsImmutablePass() override { return this; }
  242. /// ImmutablePasses are never run.
  243. bool runOnModule(Module &) override { return false; }
  244. };
  245. //===----------------------------------------------------------------------===//
  246. /// FunctionPass class - This class is used to implement most global
  247. /// optimizations. Optimizations should subclass this class if they meet the
  248. /// following constraints:
  249. ///
  250. /// 1. Optimizations are organized globally, i.e., a function at a time
  251. /// 2. Optimizing a function does not cause the addition or removal of any
  252. /// functions in the module
  253. ///
  254. class FunctionPass : public Pass {
  255. public:
  256. explicit FunctionPass(char &pid) : Pass(PT_Function, pid) {}
  257. /// createPrinterPass - Get a function printer pass.
  258. Pass *createPrinterPass(raw_ostream &OS,
  259. const std::string &Banner) const override;
  260. /// runOnFunction - Virtual method overriden by subclasses to do the
  261. /// per-function processing of the pass.
  262. virtual bool runOnFunction(Function &F) = 0;
  263. void assignPassManager(PMStack &PMS, PassManagerType T) override;
  264. /// Return what kind of Pass Manager can manage this pass.
  265. PassManagerType getPotentialPassManagerType() const override;
  266. protected:
  267. /// Optional passes call this function to check whether the pass should be
  268. /// skipped. This is the case when Attribute::OptimizeNone is set or when
  269. /// optimization bisect is over the limit.
  270. bool skipFunction(const Function &F) const;
  271. };
  272. /// If the user specifies the -time-passes argument on an LLVM tool command line
  273. /// then the value of this boolean will be true, otherwise false.
  274. /// This is the storage for the -time-passes option.
  275. extern bool TimePassesIsEnabled;
  276. /// If TimePassesPerRun is true, there would be one line of report for
  277. /// each pass invocation.
  278. /// If TimePassesPerRun is false, there would be only one line of
  279. /// report for each pass (even there are more than one pass objects).
  280. /// (For new pass manager only)
  281. extern bool TimePassesPerRun;
  282. } // end namespace llvm
  283. // Include support files that contain important APIs commonly used by Passes,
  284. // but that we want to separate out to make it easier to read the header files.
  285. #include "llvm/PassAnalysisSupport.h"
  286. #include "llvm/PassSupport.h"
  287. #endif // LLVM_PASS_H
  288. #ifdef __GNUC__
  289. #pragma GCC diagnostic pop
  290. #endif