Pass.h 16 KB

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