2
0

Pass.h 16 KB

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