TargetPassConfig.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. //===- TargetPassConfig.h - Code Generation pass options --------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. /// \file
  9. /// Target-Independent Code Generator Pass Configuration Options pass.
  10. ///
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CODEGEN_TARGETPASSCONFIG_H
  13. #define LLVM_CODEGEN_TARGETPASSCONFIG_H
  14. #include "llvm/Pass.h"
  15. #include "llvm/Support/CodeGen.h"
  16. #include <cassert>
  17. #include <string>
  18. namespace llvm {
  19. class LLVMTargetMachine;
  20. struct MachineSchedContext;
  21. class PassConfigImpl;
  22. class ScheduleDAGInstrs;
  23. class CSEConfigBase;
  24. class PassInstrumentationCallbacks;
  25. // The old pass manager infrastructure is hidden in a legacy namespace now.
  26. namespace legacy {
  27. class PassManagerBase;
  28. } // end namespace legacy
  29. using legacy::PassManagerBase;
  30. /// Discriminated union of Pass ID types.
  31. ///
  32. /// The PassConfig API prefers dealing with IDs because they are safer and more
  33. /// efficient. IDs decouple configuration from instantiation. This way, when a
  34. /// pass is overriden, it isn't unnecessarily instantiated. It is also unsafe to
  35. /// refer to a Pass pointer after adding it to a pass manager, which deletes
  36. /// redundant pass instances.
  37. ///
  38. /// However, it is convient to directly instantiate target passes with
  39. /// non-default ctors. These often don't have a registered PassInfo. Rather than
  40. /// force all target passes to implement the pass registry boilerplate, allow
  41. /// the PassConfig API to handle either type.
  42. ///
  43. /// AnalysisID is sadly char*, so PointerIntPair won't work.
  44. class IdentifyingPassPtr {
  45. union {
  46. AnalysisID ID;
  47. Pass *P;
  48. };
  49. bool IsInstance = false;
  50. public:
  51. IdentifyingPassPtr() : P(nullptr) {}
  52. IdentifyingPassPtr(AnalysisID IDPtr) : ID(IDPtr) {}
  53. IdentifyingPassPtr(Pass *InstancePtr) : P(InstancePtr), IsInstance(true) {}
  54. bool isValid() const { return P; }
  55. bool isInstance() const { return IsInstance; }
  56. AnalysisID getID() const {
  57. assert(!IsInstance && "Not a Pass ID");
  58. return ID;
  59. }
  60. Pass *getInstance() const {
  61. assert(IsInstance && "Not a Pass Instance");
  62. return P;
  63. }
  64. };
  65. /// Target-Independent Code Generator Pass Configuration Options.
  66. ///
  67. /// This is an ImmutablePass solely for the purpose of exposing CodeGen options
  68. /// to the internals of other CodeGen passes.
  69. class TargetPassConfig : public ImmutablePass {
  70. private:
  71. PassManagerBase *PM = nullptr;
  72. AnalysisID StartBefore = nullptr;
  73. AnalysisID StartAfter = nullptr;
  74. AnalysisID StopBefore = nullptr;
  75. AnalysisID StopAfter = nullptr;
  76. unsigned StartBeforeInstanceNum = 0;
  77. unsigned StartBeforeCount = 0;
  78. unsigned StartAfterInstanceNum = 0;
  79. unsigned StartAfterCount = 0;
  80. unsigned StopBeforeInstanceNum = 0;
  81. unsigned StopBeforeCount = 0;
  82. unsigned StopAfterInstanceNum = 0;
  83. unsigned StopAfterCount = 0;
  84. bool Started = true;
  85. bool Stopped = false;
  86. bool AddingMachinePasses = false;
  87. bool DebugifyIsSafe = true;
  88. /// Set the StartAfter, StartBefore and StopAfter passes to allow running only
  89. /// a portion of the normal code-gen pass sequence.
  90. ///
  91. /// If the StartAfter and StartBefore pass ID is zero, then compilation will
  92. /// begin at the normal point; otherwise, clear the Started flag to indicate
  93. /// that passes should not be added until the starting pass is seen. If the
  94. /// Stop pass ID is zero, then compilation will continue to the end.
  95. ///
  96. /// This function expects that at least one of the StartAfter or the
  97. /// StartBefore pass IDs is null.
  98. void setStartStopPasses();
  99. protected:
  100. LLVMTargetMachine *TM;
  101. PassConfigImpl *Impl = nullptr; // Internal data structures
  102. bool Initialized = false; // Flagged after all passes are configured.
  103. // Target Pass Options
  104. // Targets provide a default setting, user flags override.
  105. bool DisableVerify = false;
  106. /// Default setting for -enable-tail-merge on this target.
  107. bool EnableTailMerge = true;
  108. /// Require processing of functions such that callees are generated before
  109. /// callers.
  110. bool RequireCodeGenSCCOrder = false;
  111. /// Add the actual instruction selection passes. This does not include
  112. /// preparation passes on IR.
  113. bool addCoreISelPasses();
  114. public:
  115. TargetPassConfig(LLVMTargetMachine &TM, PassManagerBase &pm);
  116. // Dummy constructor.
  117. TargetPassConfig();
  118. ~TargetPassConfig() override;
  119. static char ID;
  120. /// Get the right type of TargetMachine for this target.
  121. template<typename TMC> TMC &getTM() const {
  122. return *static_cast<TMC*>(TM);
  123. }
  124. //
  125. void setInitialized() { Initialized = true; }
  126. CodeGenOpt::Level getOptLevel() const;
  127. /// Returns true if one of the `-start-after`, `-start-before`, `-stop-after`
  128. /// or `-stop-before` options is set.
  129. static bool hasLimitedCodeGenPipeline();
  130. /// Returns true if none of the `-stop-before` and `-stop-after` options is
  131. /// set.
  132. static bool willCompleteCodeGenPipeline();
  133. /// If hasLimitedCodeGenPipeline is true, this method
  134. /// returns a string with the name of the options, separated
  135. /// by \p Separator that caused this pipeline to be limited.
  136. static std::string
  137. getLimitedCodeGenPipelineReason(const char *Separator = "/");
  138. void setDisableVerify(bool Disable) { setOpt(DisableVerify, Disable); }
  139. bool getEnableTailMerge() const { return EnableTailMerge; }
  140. void setEnableTailMerge(bool Enable) { setOpt(EnableTailMerge, Enable); }
  141. bool requiresCodeGenSCCOrder() const { return RequireCodeGenSCCOrder; }
  142. void setRequiresCodeGenSCCOrder(bool Enable = true) {
  143. setOpt(RequireCodeGenSCCOrder, Enable);
  144. }
  145. /// Allow the target to override a specific pass without overriding the pass
  146. /// pipeline. When passes are added to the standard pipeline at the
  147. /// point where StandardID is expected, add TargetID in its place.
  148. void substitutePass(AnalysisID StandardID, IdentifyingPassPtr TargetID);
  149. /// Insert InsertedPassID pass after TargetPassID pass.
  150. void insertPass(AnalysisID TargetPassID, IdentifyingPassPtr InsertedPassID,
  151. bool VerifyAfter = true);
  152. /// Allow the target to enable a specific standard pass by default.
  153. void enablePass(AnalysisID PassID) { substitutePass(PassID, PassID); }
  154. /// Allow the target to disable a specific standard pass by default.
  155. void disablePass(AnalysisID PassID) {
  156. substitutePass(PassID, IdentifyingPassPtr());
  157. }
  158. /// Return the pass substituted for StandardID by the target.
  159. /// If no substitution exists, return StandardID.
  160. IdentifyingPassPtr getPassSubstitution(AnalysisID StandardID) const;
  161. /// Return true if the pass has been substituted by the target or
  162. /// overridden on the command line.
  163. bool isPassSubstitutedOrOverridden(AnalysisID ID) const;
  164. /// Return true if the optimized regalloc pipeline is enabled.
  165. bool getOptimizeRegAlloc() const;
  166. /// Return true if the default global register allocator is in use and
  167. /// has not be overriden on the command line with '-regalloc=...'
  168. bool usingDefaultRegAlloc() const;
  169. /// High level function that adds all passes necessary to go from llvm IR
  170. /// representation to the MI representation.
  171. /// Adds IR based lowering and target specific optimization passes and finally
  172. /// the core instruction selection passes.
  173. /// \returns true if an error occurred, false otherwise.
  174. bool addISelPasses();
  175. /// Add common target configurable passes that perform LLVM IR to IR
  176. /// transforms following machine independent optimization.
  177. virtual void addIRPasses();
  178. /// Add passes to lower exception handling for the code generator.
  179. void addPassesToHandleExceptions();
  180. /// Add pass to prepare the LLVM IR for code generation. This should be done
  181. /// before exception handling preparation passes.
  182. virtual void addCodeGenPrepare();
  183. /// Add common passes that perform LLVM IR to IR transforms in preparation for
  184. /// instruction selection.
  185. virtual void addISelPrepare();
  186. /// addInstSelector - This method should install an instruction selector pass,
  187. /// which converts from LLVM code to machine instructions.
  188. virtual bool addInstSelector() {
  189. return true;
  190. }
  191. /// This method should install an IR translator pass, which converts from
  192. /// LLVM code to machine instructions with possibly generic opcodes.
  193. virtual bool addIRTranslator() { return true; }
  194. /// This method may be implemented by targets that want to run passes
  195. /// immediately before legalization.
  196. virtual void addPreLegalizeMachineIR() {}
  197. /// This method should install a legalize pass, which converts the instruction
  198. /// sequence into one that can be selected by the target.
  199. virtual bool addLegalizeMachineIR() { return true; }
  200. /// This method may be implemented by targets that want to run passes
  201. /// immediately before the register bank selection.
  202. virtual void addPreRegBankSelect() {}
  203. /// This method should install a register bank selector pass, which
  204. /// assigns register banks to virtual registers without a register
  205. /// class or register banks.
  206. virtual bool addRegBankSelect() { return true; }
  207. /// This method may be implemented by targets that want to run passes
  208. /// immediately before the (global) instruction selection.
  209. virtual void addPreGlobalInstructionSelect() {}
  210. /// This method should install a (global) instruction selector pass, which
  211. /// converts possibly generic instructions to fully target-specific
  212. /// instructions, thereby constraining all generic virtual registers to
  213. /// register classes.
  214. virtual bool addGlobalInstructionSelect() { return true; }
  215. /// Add the complete, standard set of LLVM CodeGen passes.
  216. /// Fully developed targets will not generally override this.
  217. virtual void addMachinePasses();
  218. /// Create an instance of ScheduleDAGInstrs to be run within the standard
  219. /// MachineScheduler pass for this function and target at the current
  220. /// optimization level.
  221. ///
  222. /// This can also be used to plug a new MachineSchedStrategy into an instance
  223. /// of the standard ScheduleDAGMI:
  224. /// return new ScheduleDAGMI(C, std::make_unique<MyStrategy>(C), /*RemoveKillFlags=*/false)
  225. ///
  226. /// Return NULL to select the default (generic) machine scheduler.
  227. virtual ScheduleDAGInstrs *
  228. createMachineScheduler(MachineSchedContext *C) const {
  229. return nullptr;
  230. }
  231. /// Similar to createMachineScheduler but used when postRA machine scheduling
  232. /// is enabled.
  233. virtual ScheduleDAGInstrs *
  234. createPostMachineScheduler(MachineSchedContext *C) const {
  235. return nullptr;
  236. }
  237. /// printAndVerify - Add a pass to dump then verify the machine function, if
  238. /// those steps are enabled.
  239. void printAndVerify(const std::string &Banner);
  240. /// Add a pass to print the machine function if printing is enabled.
  241. void addPrintPass(const std::string &Banner);
  242. /// Add a pass to perform basic verification of the machine function if
  243. /// verification is enabled.
  244. void addVerifyPass(const std::string &Banner);
  245. /// Add a pass to add synthesized debug info to the MIR.
  246. void addDebugifyPass();
  247. /// Add a pass to remove debug info from the MIR.
  248. void addStripDebugPass();
  249. /// Add a pass to check synthesized debug info for MIR.
  250. void addCheckDebugPass();
  251. /// Add standard passes before a pass that's about to be added. For example,
  252. /// the DebugifyMachineModulePass if it is enabled.
  253. void addMachinePrePasses(bool AllowDebugify = true);
  254. /// Add standard passes after a pass that has just been added. For example,
  255. /// the MachineVerifier if it is enabled.
  256. void addMachinePostPasses(const std::string &Banner, bool AllowVerify = true,
  257. bool AllowStrip = true);
  258. /// Check whether or not GlobalISel should abort on error.
  259. /// When this is disabled, GlobalISel will fall back on SDISel instead of
  260. /// erroring out.
  261. bool isGlobalISelAbortEnabled() const;
  262. /// Check whether or not a diagnostic should be emitted when GlobalISel
  263. /// uses the fallback path. In other words, it will emit a diagnostic
  264. /// when GlobalISel failed and isGlobalISelAbortEnabled is false.
  265. virtual bool reportDiagnosticWhenGlobalISelFallback() const;
  266. /// Check whether continuous CSE should be enabled in GISel passes.
  267. /// By default, it's enabled for non O0 levels.
  268. virtual bool isGISelCSEEnabled() const;
  269. /// Returns the CSEConfig object to use for the current optimization level.
  270. virtual std::unique_ptr<CSEConfigBase> getCSEConfig() const;
  271. protected:
  272. // Helper to verify the analysis is really immutable.
  273. void setOpt(bool &Opt, bool Val);
  274. /// Methods with trivial inline returns are convenient points in the common
  275. /// codegen pass pipeline where targets may insert passes. Methods with
  276. /// out-of-line standard implementations are major CodeGen stages called by
  277. /// addMachinePasses. Some targets may override major stages when inserting
  278. /// passes is insufficient, but maintaining overriden stages is more work.
  279. ///
  280. /// addPreISelPasses - This method should add any "last minute" LLVM->LLVM
  281. /// passes (which are run just before instruction selector).
  282. virtual bool addPreISel() {
  283. return true;
  284. }
  285. /// addMachineSSAOptimization - Add standard passes that optimize machine
  286. /// instructions in SSA form.
  287. virtual void addMachineSSAOptimization();
  288. /// Add passes that optimize instruction level parallelism for out-of-order
  289. /// targets. These passes are run while the machine code is still in SSA
  290. /// form, so they can use MachineTraceMetrics to control their heuristics.
  291. ///
  292. /// All passes added here should preserve the MachineDominatorTree,
  293. /// MachineLoopInfo, and MachineTraceMetrics analyses.
  294. virtual bool addILPOpts() {
  295. return false;
  296. }
  297. /// This method may be implemented by targets that want to run passes
  298. /// immediately before register allocation.
  299. virtual void addPreRegAlloc() { }
  300. /// createTargetRegisterAllocator - Create the register allocator pass for
  301. /// this target at the current optimization level.
  302. virtual FunctionPass *createTargetRegisterAllocator(bool Optimized);
  303. /// addFastRegAlloc - Add the minimum set of target-independent passes that
  304. /// are required for fast register allocation.
  305. virtual void addFastRegAlloc();
  306. /// addOptimizedRegAlloc - Add passes related to register allocation.
  307. /// LLVMTargetMachine provides standard regalloc passes for most targets.
  308. virtual void addOptimizedRegAlloc();
  309. /// addPreRewrite - Add passes to the optimized register allocation pipeline
  310. /// after register allocation is complete, but before virtual registers are
  311. /// rewritten to physical registers.
  312. ///
  313. /// These passes must preserve VirtRegMap and LiveIntervals, and when running
  314. /// after RABasic or RAGreedy, they should take advantage of LiveRegMatrix.
  315. /// When these passes run, VirtRegMap contains legal physreg assignments for
  316. /// all virtual registers.
  317. ///
  318. /// Note if the target overloads addRegAssignAndRewriteOptimized, this may not
  319. /// be honored. This is also not generally used for the the fast variant,
  320. /// where the allocation and rewriting are done in one pass.
  321. virtual bool addPreRewrite() {
  322. return false;
  323. }
  324. /// addPostFastRegAllocRewrite - Add passes to the optimized register
  325. /// allocation pipeline after fast register allocation is complete.
  326. virtual bool addPostFastRegAllocRewrite() { return false; }
  327. /// Add passes to be run immediately after virtual registers are rewritten
  328. /// to physical registers.
  329. virtual void addPostRewrite() { }
  330. /// This method may be implemented by targets that want to run passes after
  331. /// register allocation pass pipeline but before prolog-epilog insertion.
  332. virtual void addPostRegAlloc() { }
  333. /// Add passes that optimize machine instructions after register allocation.
  334. virtual void addMachineLateOptimization();
  335. /// This method may be implemented by targets that want to run passes after
  336. /// prolog-epilog insertion and before the second instruction scheduling pass.
  337. virtual void addPreSched2() { }
  338. /// addGCPasses - Add late codegen passes that analyze code for garbage
  339. /// collection. This should return true if GC info should be printed after
  340. /// these passes.
  341. virtual bool addGCPasses();
  342. /// Add standard basic block placement passes.
  343. virtual void addBlockPlacement();
  344. /// This pass may be implemented by targets that want to run passes
  345. /// immediately before machine code is emitted.
  346. virtual void addPreEmitPass() { }
  347. /// Targets may add passes immediately before machine code is emitted in this
  348. /// callback. This is called even later than `addPreEmitPass`.
  349. // FIXME: Rename `addPreEmitPass` to something more sensible given its actual
  350. // position and remove the `2` suffix here as this callback is what
  351. // `addPreEmitPass` *should* be but in reality isn't.
  352. virtual void addPreEmitPass2() {}
  353. /// Utilities for targets to add passes to the pass manager.
  354. ///
  355. /// Add a CodeGen pass at this point in the pipeline after checking overrides.
  356. /// Return the pass that was added, or zero if no pass was added.
  357. /// @p verifyAfter if true and adding a machine function pass add an extra
  358. /// machine verification pass afterwards.
  359. AnalysisID addPass(AnalysisID PassID, bool verifyAfter = true);
  360. /// Add a pass to the PassManager if that pass is supposed to be run, as
  361. /// determined by the StartAfter and StopAfter options. Takes ownership of the
  362. /// pass.
  363. /// @p verifyAfter if true and adding a machine function pass add an extra
  364. /// machine verification pass afterwards.
  365. void addPass(Pass *P, bool verifyAfter = true);
  366. /// addMachinePasses helper to create the target-selected or overriden
  367. /// regalloc pass.
  368. virtual FunctionPass *createRegAllocPass(bool Optimized);
  369. /// Add core register allocator passes which do the actual register assignment
  370. /// and rewriting. \returns true if any passes were added.
  371. virtual bool addRegAssignAndRewriteFast();
  372. virtual bool addRegAssignAndRewriteOptimized();
  373. };
  374. void registerCodeGenCallback(PassInstrumentationCallbacks &PIC,
  375. LLVMTargetMachine &);
  376. } // end namespace llvm
  377. #endif // LLVM_CODEGEN_TARGETPASSCONFIG_H