CodeGenPassBuilder.h 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Construction of codegen pass pipelines ------------------*- 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. /// \file
  14. ///
  15. /// Interfaces for registering analysis passes, producing common pass manager
  16. /// configurations, and parsing of pass pipelines.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_CODEGEN_CODEGENPASSBUILDER_H
  20. #define LLVM_CODEGEN_CODEGENPASSBUILDER_H
  21. #include "llvm/ADT/FunctionExtras.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/ADT/StringRef.h"
  24. #include "llvm/Analysis/AliasAnalysis.h"
  25. #include "llvm/Analysis/BasicAliasAnalysis.h"
  26. #include "llvm/Analysis/CFLAndersAliasAnalysis.h"
  27. #include "llvm/Analysis/CFLSteensAliasAnalysis.h"
  28. #include "llvm/Analysis/ScopedNoAliasAA.h"
  29. #include "llvm/Analysis/TargetTransformInfo.h"
  30. #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
  31. #include "llvm/CodeGen/ExpandReductions.h"
  32. #include "llvm/CodeGen/MachineModuleInfo.h"
  33. #include "llvm/CodeGen/MachinePassManager.h"
  34. #include "llvm/CodeGen/PreISelIntrinsicLowering.h"
  35. #include "llvm/CodeGen/ReplaceWithVeclib.h"
  36. #include "llvm/CodeGen/UnreachableBlockElim.h"
  37. #include "llvm/IR/IRPrintingPasses.h"
  38. #include "llvm/IR/PassManager.h"
  39. #include "llvm/IR/Verifier.h"
  40. #include "llvm/MC/MCAsmInfo.h"
  41. #include "llvm/MC/MCStreamer.h"
  42. #include "llvm/MC/MCTargetOptions.h"
  43. #include "llvm/Support/CodeGen.h"
  44. #include "llvm/Support/Debug.h"
  45. #include "llvm/Support/Error.h"
  46. #include "llvm/Support/ErrorHandling.h"
  47. #include "llvm/Target/CGPassBuilderOption.h"
  48. #include "llvm/Target/TargetMachine.h"
  49. #include "llvm/Transforms/Scalar.h"
  50. #include "llvm/Transforms/Scalar/ConstantHoisting.h"
  51. #include "llvm/Transforms/Scalar/LoopPassManager.h"
  52. #include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
  53. #include "llvm/Transforms/Scalar/LowerConstantIntrinsics.h"
  54. #include "llvm/Transforms/Scalar/MergeICmps.h"
  55. #include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
  56. #include "llvm/Transforms/Scalar/ScalarizeMaskedMemIntrin.h"
  57. #include "llvm/Transforms/Utils.h"
  58. #include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
  59. #include "llvm/Transforms/Utils/LowerInvoke.h"
  60. #include <cassert>
  61. #include <type_traits>
  62. #include <utility>
  63. namespace llvm {
  64. // FIXME: Dummy target independent passes definitions that have not yet been
  65. // ported to new pass manager. Once they do, remove these.
  66. #define DUMMY_FUNCTION_PASS(NAME, PASS_NAME, CONSTRUCTOR) \
  67. struct PASS_NAME : public PassInfoMixin<PASS_NAME> { \
  68. template <typename... Ts> PASS_NAME(Ts &&...) {} \
  69. PreservedAnalyses run(Function &, FunctionAnalysisManager &) { \
  70. return PreservedAnalyses::all(); \
  71. } \
  72. };
  73. #define DUMMY_MODULE_PASS(NAME, PASS_NAME, CONSTRUCTOR) \
  74. struct PASS_NAME : public PassInfoMixin<PASS_NAME> { \
  75. template <typename... Ts> PASS_NAME(Ts &&...) {} \
  76. PreservedAnalyses run(Module &, ModuleAnalysisManager &) { \
  77. return PreservedAnalyses::all(); \
  78. } \
  79. };
  80. #define DUMMY_MACHINE_MODULE_PASS(NAME, PASS_NAME, CONSTRUCTOR) \
  81. struct PASS_NAME : public PassInfoMixin<PASS_NAME> { \
  82. template <typename... Ts> PASS_NAME(Ts &&...) {} \
  83. Error run(Module &, MachineFunctionAnalysisManager &) { \
  84. return Error::success(); \
  85. } \
  86. PreservedAnalyses run(MachineFunction &, \
  87. MachineFunctionAnalysisManager &) { \
  88. llvm_unreachable("this api is to make new PM api happy"); \
  89. } \
  90. static AnalysisKey Key; \
  91. };
  92. #define DUMMY_MACHINE_FUNCTION_PASS(NAME, PASS_NAME, CONSTRUCTOR) \
  93. struct PASS_NAME : public PassInfoMixin<PASS_NAME> { \
  94. template <typename... Ts> PASS_NAME(Ts &&...) {} \
  95. PreservedAnalyses run(MachineFunction &, \
  96. MachineFunctionAnalysisManager &) { \
  97. return PreservedAnalyses::all(); \
  98. } \
  99. static AnalysisKey Key; \
  100. };
  101. #include "MachinePassRegistry.def"
  102. /// This class provides access to building LLVM's passes.
  103. ///
  104. /// Its members provide the baseline state available to passes during their
  105. /// construction. The \c MachinePassRegistry.def file specifies how to construct
  106. /// all of the built-in passes, and those may reference these members during
  107. /// construction.
  108. template <typename DerivedT> class CodeGenPassBuilder {
  109. public:
  110. explicit CodeGenPassBuilder(LLVMTargetMachine &TM, CGPassBuilderOption Opts,
  111. PassInstrumentationCallbacks *PIC)
  112. : TM(TM), Opt(Opts), PIC(PIC) {
  113. // Target could set CGPassBuilderOption::MISchedPostRA to true to achieve
  114. // substitutePass(&PostRASchedulerID, &PostMachineSchedulerID)
  115. // Target should override TM.Options.EnableIPRA in their target-specific
  116. // LLVMTM ctor. See TargetMachine::setGlobalISel for example.
  117. if (Opt.EnableIPRA)
  118. TM.Options.EnableIPRA = *Opt.EnableIPRA;
  119. if (Opt.EnableGlobalISelAbort)
  120. TM.Options.GlobalISelAbort = *Opt.EnableGlobalISelAbort;
  121. if (!Opt.OptimizeRegAlloc)
  122. Opt.OptimizeRegAlloc = getOptLevel() != CodeGenOpt::None;
  123. }
  124. Error buildPipeline(ModulePassManager &MPM, MachineFunctionPassManager &MFPM,
  125. raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
  126. CodeGenFileType FileType) const;
  127. void registerModuleAnalyses(ModuleAnalysisManager &) const;
  128. void registerFunctionAnalyses(FunctionAnalysisManager &) const;
  129. void registerMachineFunctionAnalyses(MachineFunctionAnalysisManager &) const;
  130. std::pair<StringRef, bool> getPassNameFromLegacyName(StringRef) const;
  131. void registerAnalyses(MachineFunctionAnalysisManager &MFAM) const {
  132. registerModuleAnalyses(*MFAM.MAM);
  133. registerFunctionAnalyses(*MFAM.FAM);
  134. registerMachineFunctionAnalyses(MFAM);
  135. }
  136. PassInstrumentationCallbacks *getPassInstrumentationCallbacks() const {
  137. return PIC;
  138. }
  139. protected:
  140. template <typename PassT> using has_key_t = decltype(PassT::Key);
  141. template <typename PassT>
  142. using is_module_pass_t = decltype(std::declval<PassT &>().run(
  143. std::declval<Module &>(), std::declval<ModuleAnalysisManager &>()));
  144. template <typename PassT>
  145. using is_function_pass_t = decltype(std::declval<PassT &>().run(
  146. std::declval<Function &>(), std::declval<FunctionAnalysisManager &>()));
  147. // Function object to maintain state while adding codegen IR passes.
  148. class AddIRPass {
  149. public:
  150. AddIRPass(ModulePassManager &MPM, bool DebugPM, bool Check = true)
  151. : MPM(MPM) {
  152. if (Check)
  153. AddingFunctionPasses = false;
  154. }
  155. ~AddIRPass() {
  156. MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
  157. }
  158. // Add Function Pass
  159. template <typename PassT>
  160. std::enable_if_t<is_detected<is_function_pass_t, PassT>::value>
  161. operator()(PassT &&Pass) {
  162. if (AddingFunctionPasses && !*AddingFunctionPasses)
  163. AddingFunctionPasses = true;
  164. FPM.addPass(std::forward<PassT>(Pass));
  165. }
  166. // Add Module Pass
  167. template <typename PassT>
  168. std::enable_if_t<is_detected<is_module_pass_t, PassT>::value &&
  169. !is_detected<is_function_pass_t, PassT>::value>
  170. operator()(PassT &&Pass) {
  171. assert((!AddingFunctionPasses || !*AddingFunctionPasses) &&
  172. "could not add module pass after adding function pass");
  173. MPM.addPass(std::forward<PassT>(Pass));
  174. }
  175. private:
  176. ModulePassManager &MPM;
  177. FunctionPassManager FPM;
  178. // The codegen IR pipeline are mostly function passes with the exceptions of
  179. // a few loop and module passes. `AddingFunctionPasses` make sures that
  180. // we could only add module passes at the beginning of the pipeline. Once
  181. // we begin adding function passes, we could no longer add module passes.
  182. // This special-casing introduces less adaptor passes. If we have the need
  183. // of adding module passes after function passes, we could change the
  184. // implementation to accommodate that.
  185. Optional<bool> AddingFunctionPasses;
  186. };
  187. // Function object to maintain state while adding codegen machine passes.
  188. class AddMachinePass {
  189. public:
  190. AddMachinePass(MachineFunctionPassManager &PM) : PM(PM) {}
  191. template <typename PassT> void operator()(PassT &&Pass) {
  192. static_assert(
  193. is_detected<has_key_t, PassT>::value,
  194. "Machine function pass must define a static member variable `Key`.");
  195. for (auto &C : BeforeCallbacks)
  196. if (!C(&PassT::Key))
  197. return;
  198. PM.addPass(std::forward<PassT>(Pass));
  199. for (auto &C : AfterCallbacks)
  200. C(&PassT::Key);
  201. }
  202. template <typename PassT> void insertPass(AnalysisKey *ID, PassT Pass) {
  203. AfterCallbacks.emplace_back(
  204. [this, ID, Pass = std::move(Pass)](AnalysisKey *PassID) {
  205. if (PassID == ID)
  206. this->PM.addPass(std::move(Pass));
  207. });
  208. }
  209. void disablePass(AnalysisKey *ID) {
  210. BeforeCallbacks.emplace_back(
  211. [ID](AnalysisKey *PassID) { return PassID != ID; });
  212. }
  213. MachineFunctionPassManager releasePM() { return std::move(PM); }
  214. private:
  215. MachineFunctionPassManager &PM;
  216. SmallVector<llvm::unique_function<bool(AnalysisKey *)>, 4> BeforeCallbacks;
  217. SmallVector<llvm::unique_function<void(AnalysisKey *)>, 4> AfterCallbacks;
  218. };
  219. LLVMTargetMachine &TM;
  220. CGPassBuilderOption Opt;
  221. PassInstrumentationCallbacks *PIC;
  222. /// Target override these hooks to parse target-specific analyses.
  223. void registerTargetAnalysis(ModuleAnalysisManager &) const {}
  224. void registerTargetAnalysis(FunctionAnalysisManager &) const {}
  225. void registerTargetAnalysis(MachineFunctionAnalysisManager &) const {}
  226. std::pair<StringRef, bool> getTargetPassNameFromLegacyName(StringRef) const {
  227. return {"", false};
  228. }
  229. template <typename TMC> TMC &getTM() const { return static_cast<TMC &>(TM); }
  230. CodeGenOpt::Level getOptLevel() const { return TM.getOptLevel(); }
  231. /// Check whether or not GlobalISel should abort on error.
  232. /// When this is disabled, GlobalISel will fall back on SDISel instead of
  233. /// erroring out.
  234. bool isGlobalISelAbortEnabled() const {
  235. return TM.Options.GlobalISelAbort == GlobalISelAbortMode::Enable;
  236. }
  237. /// Check whether or not a diagnostic should be emitted when GlobalISel
  238. /// uses the fallback path. In other words, it will emit a diagnostic
  239. /// when GlobalISel failed and isGlobalISelAbortEnabled is false.
  240. bool reportDiagnosticWhenGlobalISelFallback() const {
  241. return TM.Options.GlobalISelAbort == GlobalISelAbortMode::DisableWithDiag;
  242. }
  243. /// addInstSelector - This method should install an instruction selector pass,
  244. /// which converts from LLVM code to machine instructions.
  245. Error addInstSelector(AddMachinePass &) const {
  246. return make_error<StringError>("addInstSelector is not overridden",
  247. inconvertibleErrorCode());
  248. }
  249. /// Add passes that optimize instruction level parallelism for out-of-order
  250. /// targets. These passes are run while the machine code is still in SSA
  251. /// form, so they can use MachineTraceMetrics to control their heuristics.
  252. ///
  253. /// All passes added here should preserve the MachineDominatorTree,
  254. /// MachineLoopInfo, and MachineTraceMetrics analyses.
  255. void addILPOpts(AddMachinePass &) const {}
  256. /// This method may be implemented by targets that want to run passes
  257. /// immediately before register allocation.
  258. void addPreRegAlloc(AddMachinePass &) const {}
  259. /// addPreRewrite - Add passes to the optimized register allocation pipeline
  260. /// after register allocation is complete, but before virtual registers are
  261. /// rewritten to physical registers.
  262. ///
  263. /// These passes must preserve VirtRegMap and LiveIntervals, and when running
  264. /// after RABasic or RAGreedy, they should take advantage of LiveRegMatrix.
  265. /// When these passes run, VirtRegMap contains legal physreg assignments for
  266. /// all virtual registers.
  267. ///
  268. /// Note if the target overloads addRegAssignAndRewriteOptimized, this may not
  269. /// be honored. This is also not generally used for the the fast variant,
  270. /// where the allocation and rewriting are done in one pass.
  271. void addPreRewrite(AddMachinePass &) const {}
  272. /// Add passes to be run immediately after virtual registers are rewritten
  273. /// to physical registers.
  274. void addPostRewrite(AddMachinePass &) const {}
  275. /// This method may be implemented by targets that want to run passes after
  276. /// register allocation pass pipeline but before prolog-epilog insertion.
  277. void addPostRegAlloc(AddMachinePass &) const {}
  278. /// This method may be implemented by targets that want to run passes after
  279. /// prolog-epilog insertion and before the second instruction scheduling pass.
  280. void addPreSched2(AddMachinePass &) const {}
  281. /// This pass may be implemented by targets that want to run passes
  282. /// immediately before machine code is emitted.
  283. void addPreEmitPass(AddMachinePass &) const {}
  284. /// Targets may add passes immediately before machine code is emitted in this
  285. /// callback. This is called even later than `addPreEmitPass`.
  286. // FIXME: Rename `addPreEmitPass` to something more sensible given its actual
  287. // position and remove the `2` suffix here as this callback is what
  288. // `addPreEmitPass` *should* be but in reality isn't.
  289. void addPreEmitPass2(AddMachinePass &) const {}
  290. /// {{@ For GlobalISel
  291. ///
  292. /// addPreISel - This method should add any "last minute" LLVM->LLVM
  293. /// passes (which are run just before instruction selector).
  294. void addPreISel(AddIRPass &) const {
  295. llvm_unreachable("addPreISel is not overridden");
  296. }
  297. /// This method should install an IR translator pass, which converts from
  298. /// LLVM code to machine instructions with possibly generic opcodes.
  299. Error addIRTranslator(AddMachinePass &) const {
  300. return make_error<StringError>("addIRTranslator is not overridden",
  301. inconvertibleErrorCode());
  302. }
  303. /// This method may be implemented by targets that want to run passes
  304. /// immediately before legalization.
  305. void addPreLegalizeMachineIR(AddMachinePass &) const {}
  306. /// This method should install a legalize pass, which converts the instruction
  307. /// sequence into one that can be selected by the target.
  308. Error addLegalizeMachineIR(AddMachinePass &) const {
  309. return make_error<StringError>("addLegalizeMachineIR is not overridden",
  310. inconvertibleErrorCode());
  311. }
  312. /// This method may be implemented by targets that want to run passes
  313. /// immediately before the register bank selection.
  314. void addPreRegBankSelect(AddMachinePass &) const {}
  315. /// This method should install a register bank selector pass, which
  316. /// assigns register banks to virtual registers without a register
  317. /// class or register banks.
  318. Error addRegBankSelect(AddMachinePass &) const {
  319. return make_error<StringError>("addRegBankSelect is not overridden",
  320. inconvertibleErrorCode());
  321. }
  322. /// This method may be implemented by targets that want to run passes
  323. /// immediately before the (global) instruction selection.
  324. void addPreGlobalInstructionSelect(AddMachinePass &) const {}
  325. /// This method should install a (global) instruction selector pass, which
  326. /// converts possibly generic instructions to fully target-specific
  327. /// instructions, thereby constraining all generic virtual registers to
  328. /// register classes.
  329. Error addGlobalInstructionSelect(AddMachinePass &) const {
  330. return make_error<StringError>(
  331. "addGlobalInstructionSelect is not overridden",
  332. inconvertibleErrorCode());
  333. }
  334. /// @}}
  335. /// High level function that adds all passes necessary to go from llvm IR
  336. /// representation to the MI representation.
  337. /// Adds IR based lowering and target specific optimization passes and finally
  338. /// the core instruction selection passes.
  339. void addISelPasses(AddIRPass &) const;
  340. /// Add the actual instruction selection passes. This does not include
  341. /// preparation passes on IR.
  342. Error addCoreISelPasses(AddMachinePass &) const;
  343. /// Add the complete, standard set of LLVM CodeGen passes.
  344. /// Fully developed targets will not generally override this.
  345. Error addMachinePasses(AddMachinePass &) const;
  346. /// Add passes to lower exception handling for the code generator.
  347. void addPassesToHandleExceptions(AddIRPass &) const;
  348. /// Add common target configurable passes that perform LLVM IR to IR
  349. /// transforms following machine independent optimization.
  350. void addIRPasses(AddIRPass &) const;
  351. /// Add pass to prepare the LLVM IR for code generation. This should be done
  352. /// before exception handling preparation passes.
  353. void addCodeGenPrepare(AddIRPass &) const;
  354. /// Add common passes that perform LLVM IR to IR transforms in preparation for
  355. /// instruction selection.
  356. void addISelPrepare(AddIRPass &) const;
  357. /// Methods with trivial inline returns are convenient points in the common
  358. /// codegen pass pipeline where targets may insert passes. Methods with
  359. /// out-of-line standard implementations are major CodeGen stages called by
  360. /// addMachinePasses. Some targets may override major stages when inserting
  361. /// passes is insufficient, but maintaining overriden stages is more work.
  362. ///
  363. /// addMachineSSAOptimization - Add standard passes that optimize machine
  364. /// instructions in SSA form.
  365. void addMachineSSAOptimization(AddMachinePass &) const;
  366. /// addFastRegAlloc - Add the minimum set of target-independent passes that
  367. /// are required for fast register allocation.
  368. Error addFastRegAlloc(AddMachinePass &) const;
  369. /// addOptimizedRegAlloc - Add passes related to register allocation.
  370. /// LLVMTargetMachine provides standard regalloc passes for most targets.
  371. void addOptimizedRegAlloc(AddMachinePass &) const;
  372. /// Add passes that optimize machine instructions after register allocation.
  373. void addMachineLateOptimization(AddMachinePass &) const;
  374. /// addGCPasses - Add late codegen passes that analyze code for garbage
  375. /// collection. This should return true if GC info should be printed after
  376. /// these passes.
  377. void addGCPasses(AddMachinePass &) const {}
  378. /// Add standard basic block placement passes.
  379. void addBlockPlacement(AddMachinePass &) const;
  380. using CreateMCStreamer =
  381. std::function<Expected<std::unique_ptr<MCStreamer>>(MCContext &)>;
  382. void addAsmPrinter(AddMachinePass &, CreateMCStreamer) const {
  383. llvm_unreachable("addAsmPrinter is not overridden");
  384. }
  385. /// Utilities for targets to add passes to the pass manager.
  386. ///
  387. /// createTargetRegisterAllocator - Create the register allocator pass for
  388. /// this target at the current optimization level.
  389. void addTargetRegisterAllocator(AddMachinePass &, bool Optimized) const;
  390. /// addMachinePasses helper to create the target-selected or overriden
  391. /// regalloc pass.
  392. void addRegAllocPass(AddMachinePass &, bool Optimized) const;
  393. /// Add core register alloator passes which do the actual register assignment
  394. /// and rewriting. \returns true if any passes were added.
  395. Error addRegAssignmentFast(AddMachinePass &) const;
  396. Error addRegAssignmentOptimized(AddMachinePass &) const;
  397. private:
  398. DerivedT &derived() { return static_cast<DerivedT &>(*this); }
  399. const DerivedT &derived() const {
  400. return static_cast<const DerivedT &>(*this);
  401. }
  402. };
  403. template <typename Derived>
  404. Error CodeGenPassBuilder<Derived>::buildPipeline(
  405. ModulePassManager &MPM, MachineFunctionPassManager &MFPM,
  406. raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
  407. CodeGenFileType FileType) const {
  408. AddIRPass addIRPass(MPM, Opt.DebugPM);
  409. addISelPasses(addIRPass);
  410. AddMachinePass addPass(MFPM);
  411. if (auto Err = addCoreISelPasses(addPass))
  412. return std::move(Err);
  413. if (auto Err = derived().addMachinePasses(addPass))
  414. return std::move(Err);
  415. derived().addAsmPrinter(
  416. addPass, [this, &Out, DwoOut, FileType](MCContext &Ctx) {
  417. return this->TM.createMCStreamer(Out, DwoOut, FileType, Ctx);
  418. });
  419. addPass(FreeMachineFunctionPass());
  420. return Error::success();
  421. }
  422. static inline AAManager registerAAAnalyses(CFLAAType UseCFLAA) {
  423. AAManager AA;
  424. // The order in which these are registered determines their priority when
  425. // being queried.
  426. switch (UseCFLAA) {
  427. case CFLAAType::Steensgaard:
  428. AA.registerFunctionAnalysis<CFLSteensAA>();
  429. break;
  430. case CFLAAType::Andersen:
  431. AA.registerFunctionAnalysis<CFLAndersAA>();
  432. break;
  433. case CFLAAType::Both:
  434. AA.registerFunctionAnalysis<CFLAndersAA>();
  435. AA.registerFunctionAnalysis<CFLSteensAA>();
  436. break;
  437. default:
  438. break;
  439. }
  440. // Basic AliasAnalysis support.
  441. // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
  442. // BasicAliasAnalysis wins if they disagree. This is intended to help
  443. // support "obvious" type-punning idioms.
  444. AA.registerFunctionAnalysis<TypeBasedAA>();
  445. AA.registerFunctionAnalysis<ScopedNoAliasAA>();
  446. AA.registerFunctionAnalysis<BasicAA>();
  447. return AA;
  448. }
  449. template <typename Derived>
  450. void CodeGenPassBuilder<Derived>::registerModuleAnalyses(
  451. ModuleAnalysisManager &MAM) const {
  452. #define MODULE_ANALYSIS(NAME, PASS_NAME, CONSTRUCTOR) \
  453. MAM.registerPass([&] { return PASS_NAME CONSTRUCTOR; });
  454. #include "MachinePassRegistry.def"
  455. derived().registerTargetAnalysis(MAM);
  456. }
  457. template <typename Derived>
  458. void CodeGenPassBuilder<Derived>::registerFunctionAnalyses(
  459. FunctionAnalysisManager &FAM) const {
  460. FAM.registerPass([this] { return registerAAAnalyses(this->Opt.UseCFLAA); });
  461. #define FUNCTION_ANALYSIS(NAME, PASS_NAME, CONSTRUCTOR) \
  462. FAM.registerPass([&] { return PASS_NAME CONSTRUCTOR; });
  463. #include "MachinePassRegistry.def"
  464. derived().registerTargetAnalysis(FAM);
  465. }
  466. template <typename Derived>
  467. void CodeGenPassBuilder<Derived>::registerMachineFunctionAnalyses(
  468. MachineFunctionAnalysisManager &MFAM) const {
  469. #define MACHINE_FUNCTION_ANALYSIS(NAME, PASS_NAME, CONSTRUCTOR) \
  470. MFAM.registerPass([&] { return PASS_NAME CONSTRUCTOR; });
  471. #include "MachinePassRegistry.def"
  472. derived().registerTargetAnalysis(MFAM);
  473. }
  474. // FIXME: For new PM, use pass name directly in commandline seems good.
  475. // Translate stringfied pass name to its old commandline name. Returns the
  476. // matching legacy name and a boolean value indicating if the pass is a machine
  477. // pass.
  478. template <typename Derived>
  479. std::pair<StringRef, bool>
  480. CodeGenPassBuilder<Derived>::getPassNameFromLegacyName(StringRef Name) const {
  481. std::pair<StringRef, bool> Ret;
  482. if (Name.empty())
  483. return Ret;
  484. #define FUNCTION_PASS(NAME, PASS_NAME, CONSTRUCTOR) \
  485. if (Name == NAME) \
  486. Ret = {#PASS_NAME, false};
  487. #define DUMMY_FUNCTION_PASS(NAME, PASS_NAME, CONSTRUCTOR) \
  488. if (Name == NAME) \
  489. Ret = {#PASS_NAME, false};
  490. #define MODULE_PASS(NAME, PASS_NAME, CONSTRUCTOR) \
  491. if (Name == NAME) \
  492. Ret = {#PASS_NAME, false};
  493. #define DUMMY_MODULE_PASS(NAME, PASS_NAME, CONSTRUCTOR) \
  494. if (Name == NAME) \
  495. Ret = {#PASS_NAME, false};
  496. #define MACHINE_MODULE_PASS(NAME, PASS_NAME, CONSTRUCTOR) \
  497. if (Name == NAME) \
  498. Ret = {#PASS_NAME, true};
  499. #define DUMMY_MACHINE_MODULE_PASS(NAME, PASS_NAME, CONSTRUCTOR) \
  500. if (Name == NAME) \
  501. Ret = {#PASS_NAME, true};
  502. #define MACHINE_FUNCTION_PASS(NAME, PASS_NAME, CONSTRUCTOR) \
  503. if (Name == NAME) \
  504. Ret = {#PASS_NAME, true};
  505. #define DUMMY_MACHINE_FUNCTION_PASS(NAME, PASS_NAME, CONSTRUCTOR) \
  506. if (Name == NAME) \
  507. Ret = {#PASS_NAME, true};
  508. #include "llvm/CodeGen/MachinePassRegistry.def"
  509. if (Ret.first.empty())
  510. Ret = derived().getTargetPassNameFromLegacyName(Name);
  511. if (Ret.first.empty())
  512. report_fatal_error(Twine('\"') + Twine(Name) +
  513. Twine("\" pass could not be found."));
  514. return Ret;
  515. }
  516. template <typename Derived>
  517. void CodeGenPassBuilder<Derived>::addISelPasses(AddIRPass &addPass) const {
  518. if (TM.useEmulatedTLS())
  519. addPass(LowerEmuTLSPass());
  520. addPass(PreISelIntrinsicLoweringPass());
  521. derived().addIRPasses(addPass);
  522. derived().addCodeGenPrepare(addPass);
  523. addPassesToHandleExceptions(addPass);
  524. derived().addISelPrepare(addPass);
  525. }
  526. /// Add common target configurable passes that perform LLVM IR to IR transforms
  527. /// following machine independent optimization.
  528. template <typename Derived>
  529. void CodeGenPassBuilder<Derived>::addIRPasses(AddIRPass &addPass) const {
  530. // Before running any passes, run the verifier to determine if the input
  531. // coming from the front-end and/or optimizer is valid.
  532. if (!Opt.DisableVerify)
  533. addPass(VerifierPass());
  534. // Run loop strength reduction before anything else.
  535. if (getOptLevel() != CodeGenOpt::None && !Opt.DisableLSR) {
  536. addPass(createFunctionToLoopPassAdaptor(
  537. LoopStrengthReducePass(), /*UseMemorySSA*/ true, Opt.DebugPM));
  538. // FIXME: use -stop-after so we could remove PrintLSR
  539. if (Opt.PrintLSR)
  540. addPass(PrintFunctionPass(dbgs(), "\n\n*** Code after LSR ***\n"));
  541. }
  542. if (getOptLevel() != CodeGenOpt::None) {
  543. // The MergeICmpsPass tries to create memcmp calls by grouping sequences of
  544. // loads and compares. ExpandMemCmpPass then tries to expand those calls
  545. // into optimally-sized loads and compares. The transforms are enabled by a
  546. // target lowering hook.
  547. if (!Opt.DisableMergeICmps)
  548. addPass(MergeICmpsPass());
  549. addPass(ExpandMemCmpPass());
  550. }
  551. // Run GC lowering passes for builtin collectors
  552. // TODO: add a pass insertion point here
  553. addPass(GCLoweringPass());
  554. addPass(ShadowStackGCLoweringPass());
  555. addPass(LowerConstantIntrinsicsPass());
  556. // Make sure that no unreachable blocks are instruction selected.
  557. addPass(UnreachableBlockElimPass());
  558. // Prepare expensive constants for SelectionDAG.
  559. if (getOptLevel() != CodeGenOpt::None && !Opt.DisableConstantHoisting)
  560. addPass(ConstantHoistingPass());
  561. // Replace calls to LLVM intrinsics (e.g., exp, log) operating on vector
  562. // operands with calls to the corresponding functions in a vector library.
  563. if (getOptLevel() != CodeGenOpt::None)
  564. addPass(ReplaceWithVeclib());
  565. if (getOptLevel() != CodeGenOpt::None && !Opt.DisablePartialLibcallInlining)
  566. addPass(PartiallyInlineLibCallsPass());
  567. // Instrument function entry and exit, e.g. with calls to mcount().
  568. addPass(EntryExitInstrumenterPass(/*PostInlining=*/true));
  569. // Add scalarization of target's unsupported masked memory intrinsics pass.
  570. // the unsupported intrinsic will be replaced with a chain of basic blocks,
  571. // that stores/loads element one-by-one if the appropriate mask bit is set.
  572. addPass(ScalarizeMaskedMemIntrinPass());
  573. // Expand reduction intrinsics into shuffle sequences if the target wants to.
  574. addPass(ExpandReductionsPass());
  575. }
  576. /// Turn exception handling constructs into something the code generators can
  577. /// handle.
  578. template <typename Derived>
  579. void CodeGenPassBuilder<Derived>::addPassesToHandleExceptions(
  580. AddIRPass &addPass) const {
  581. const MCAsmInfo *MCAI = TM.getMCAsmInfo();
  582. assert(MCAI && "No MCAsmInfo");
  583. switch (MCAI->getExceptionHandlingType()) {
  584. case ExceptionHandling::SjLj:
  585. // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both
  586. // Dwarf EH prepare needs to be run after SjLj prepare. Otherwise,
  587. // catch info can get misplaced when a selector ends up more than one block
  588. // removed from the parent invoke(s). This could happen when a landing
  589. // pad is shared by multiple invokes and is also a target of a normal
  590. // edge from elsewhere.
  591. addPass(SjLjEHPreparePass());
  592. LLVM_FALLTHROUGH;
  593. case ExceptionHandling::DwarfCFI:
  594. case ExceptionHandling::ARM:
  595. case ExceptionHandling::AIX:
  596. addPass(DwarfEHPass(getOptLevel()));
  597. break;
  598. case ExceptionHandling::WinEH:
  599. // We support using both GCC-style and MSVC-style exceptions on Windows, so
  600. // add both preparation passes. Each pass will only actually run if it
  601. // recognizes the personality function.
  602. addPass(WinEHPass());
  603. addPass(DwarfEHPass(getOptLevel()));
  604. break;
  605. case ExceptionHandling::Wasm:
  606. // Wasm EH uses Windows EH instructions, but it does not need to demote PHIs
  607. // on catchpads and cleanuppads because it does not outline them into
  608. // funclets. Catchswitch blocks are not lowered in SelectionDAG, so we
  609. // should remove PHIs there.
  610. addPass(WinEHPass(/*DemoteCatchSwitchPHIOnly=*/false));
  611. addPass(WasmEHPass());
  612. break;
  613. case ExceptionHandling::None:
  614. addPass(LowerInvokePass());
  615. // The lower invoke pass may create unreachable code. Remove it.
  616. addPass(UnreachableBlockElimPass());
  617. break;
  618. }
  619. }
  620. /// Add pass to prepare the LLVM IR for code generation. This should be done
  621. /// before exception handling preparation passes.
  622. template <typename Derived>
  623. void CodeGenPassBuilder<Derived>::addCodeGenPrepare(AddIRPass &addPass) const {
  624. if (getOptLevel() != CodeGenOpt::None && !Opt.DisableCGP)
  625. addPass(CodeGenPreparePass());
  626. // TODO: Default ctor'd RewriteSymbolPass is no-op.
  627. // addPass(RewriteSymbolPass());
  628. }
  629. /// Add common passes that perform LLVM IR to IR transforms in preparation for
  630. /// instruction selection.
  631. template <typename Derived>
  632. void CodeGenPassBuilder<Derived>::addISelPrepare(AddIRPass &addPass) const {
  633. derived().addPreISel(addPass);
  634. // Add both the safe stack and the stack protection passes: each of them will
  635. // only protect functions that have corresponding attributes.
  636. addPass(SafeStackPass());
  637. addPass(StackProtectorPass());
  638. if (Opt.PrintISelInput)
  639. addPass(PrintFunctionPass(dbgs(),
  640. "\n\n*** Final LLVM Code input to ISel ***\n"));
  641. // All passes which modify the LLVM IR are now complete; run the verifier
  642. // to ensure that the IR is valid.
  643. if (!Opt.DisableVerify)
  644. addPass(VerifierPass());
  645. }
  646. template <typename Derived>
  647. Error CodeGenPassBuilder<Derived>::addCoreISelPasses(
  648. AddMachinePass &addPass) const {
  649. // Enable FastISel with -fast-isel, but allow that to be overridden.
  650. TM.setO0WantsFastISel(Opt.EnableFastISelOption.getValueOr(true));
  651. // Determine an instruction selector.
  652. enum class SelectorType { SelectionDAG, FastISel, GlobalISel };
  653. SelectorType Selector;
  654. if (Opt.EnableFastISelOption && *Opt.EnableFastISelOption == true)
  655. Selector = SelectorType::FastISel;
  656. else if ((Opt.EnableGlobalISelOption &&
  657. *Opt.EnableGlobalISelOption == true) ||
  658. (TM.Options.EnableGlobalISel &&
  659. (!Opt.EnableGlobalISelOption ||
  660. *Opt.EnableGlobalISelOption == false)))
  661. Selector = SelectorType::GlobalISel;
  662. else if (TM.getOptLevel() == CodeGenOpt::None && TM.getO0WantsFastISel())
  663. Selector = SelectorType::FastISel;
  664. else
  665. Selector = SelectorType::SelectionDAG;
  666. // Set consistently TM.Options.EnableFastISel and EnableGlobalISel.
  667. if (Selector == SelectorType::FastISel) {
  668. TM.setFastISel(true);
  669. TM.setGlobalISel(false);
  670. } else if (Selector == SelectorType::GlobalISel) {
  671. TM.setFastISel(false);
  672. TM.setGlobalISel(true);
  673. }
  674. // Add instruction selector passes.
  675. if (Selector == SelectorType::GlobalISel) {
  676. if (auto Err = derived().addIRTranslator(addPass))
  677. return std::move(Err);
  678. derived().addPreLegalizeMachineIR(addPass);
  679. if (auto Err = derived().addLegalizeMachineIR(addPass))
  680. return std::move(Err);
  681. // Before running the register bank selector, ask the target if it
  682. // wants to run some passes.
  683. derived().addPreRegBankSelect(addPass);
  684. if (auto Err = derived().addRegBankSelect(addPass))
  685. return std::move(Err);
  686. derived().addPreGlobalInstructionSelect(addPass);
  687. if (auto Err = derived().addGlobalInstructionSelect(addPass))
  688. return std::move(Err);
  689. // Pass to reset the MachineFunction if the ISel failed.
  690. addPass(ResetMachineFunctionPass(reportDiagnosticWhenGlobalISelFallback(),
  691. isGlobalISelAbortEnabled()));
  692. // Provide a fallback path when we do not want to abort on
  693. // not-yet-supported input.
  694. if (!isGlobalISelAbortEnabled())
  695. if (auto Err = derived().addInstSelector(addPass))
  696. return std::move(Err);
  697. } else if (auto Err = derived().addInstSelector(addPass))
  698. return std::move(Err);
  699. // Expand pseudo-instructions emitted by ISel. Don't run the verifier before
  700. // FinalizeISel.
  701. addPass(FinalizeISelPass());
  702. // // Print the instruction selected machine code...
  703. // printAndVerify("After Instruction Selection");
  704. return Error::success();
  705. }
  706. /// Add the complete set of target-independent postISel code generator passes.
  707. ///
  708. /// This can be read as the standard order of major LLVM CodeGen stages. Stages
  709. /// with nontrivial configuration or multiple passes are broken out below in
  710. /// add%Stage routines.
  711. ///
  712. /// Any CodeGenPassBuilder<Derived>::addXX routine may be overriden by the
  713. /// Target. The addPre/Post methods with empty header implementations allow
  714. /// injecting target-specific fixups just before or after major stages.
  715. /// Additionally, targets have the flexibility to change pass order within a
  716. /// stage by overriding default implementation of add%Stage routines below. Each
  717. /// technique has maintainability tradeoffs because alternate pass orders are
  718. /// not well supported. addPre/Post works better if the target pass is easily
  719. /// tied to a common pass. But if it has subtle dependencies on multiple passes,
  720. /// the target should override the stage instead.
  721. template <typename Derived>
  722. Error CodeGenPassBuilder<Derived>::addMachinePasses(
  723. AddMachinePass &addPass) const {
  724. // Add passes that optimize machine instructions in SSA form.
  725. if (getOptLevel() != CodeGenOpt::None) {
  726. derived().addMachineSSAOptimization(addPass);
  727. } else {
  728. // If the target requests it, assign local variables to stack slots relative
  729. // to one another and simplify frame index references where possible.
  730. addPass(LocalStackSlotPass());
  731. }
  732. if (TM.Options.EnableIPRA)
  733. addPass(RegUsageInfoPropagationPass());
  734. // Run pre-ra passes.
  735. derived().addPreRegAlloc(addPass);
  736. // Run register allocation and passes that are tightly coupled with it,
  737. // including phi elimination and scheduling.
  738. if (*Opt.OptimizeRegAlloc) {
  739. derived().addOptimizedRegAlloc(addPass);
  740. } else {
  741. if (auto Err = derived().addFastRegAlloc(addPass))
  742. return Err;
  743. }
  744. // Run post-ra passes.
  745. derived().addPostRegAlloc(addPass);
  746. addPass(RemoveRedundantDebugValuesPass());
  747. // Insert prolog/epilog code. Eliminate abstract frame index references...
  748. if (getOptLevel() != CodeGenOpt::None) {
  749. addPass(PostRAMachineSinkingPass());
  750. addPass(ShrinkWrapPass());
  751. }
  752. addPass(PrologEpilogInserterPass());
  753. /// Add passes that optimize machine instructions after register allocation.
  754. if (getOptLevel() != CodeGenOpt::None)
  755. derived().addMachineLateOptimization(addPass);
  756. // Expand pseudo instructions before second scheduling pass.
  757. addPass(ExpandPostRAPseudosPass());
  758. // Run pre-sched2 passes.
  759. derived().addPreSched2(addPass);
  760. if (Opt.EnableImplicitNullChecks)
  761. addPass(ImplicitNullChecksPass());
  762. // Second pass scheduler.
  763. // Let Target optionally insert this pass by itself at some other
  764. // point.
  765. if (getOptLevel() != CodeGenOpt::None &&
  766. !TM.targetSchedulesPostRAScheduling()) {
  767. if (Opt.MISchedPostRA)
  768. addPass(PostMachineSchedulerPass());
  769. else
  770. addPass(PostRASchedulerPass());
  771. }
  772. // GC
  773. derived().addGCPasses(addPass);
  774. // Basic block placement.
  775. if (getOptLevel() != CodeGenOpt::None)
  776. derived().addBlockPlacement(addPass);
  777. // Insert before XRay Instrumentation.
  778. addPass(FEntryInserterPass());
  779. addPass(XRayInstrumentationPass());
  780. addPass(PatchableFunctionPass());
  781. derived().addPreEmitPass(addPass);
  782. if (TM.Options.EnableIPRA)
  783. // Collect register usage information and produce a register mask of
  784. // clobbered registers, to be used to optimize call sites.
  785. addPass(RegUsageInfoCollectorPass());
  786. addPass(FuncletLayoutPass());
  787. addPass(StackMapLivenessPass());
  788. addPass(LiveDebugValuesPass());
  789. if (TM.Options.EnableMachineOutliner && getOptLevel() != CodeGenOpt::None &&
  790. Opt.EnableMachineOutliner != RunOutliner::NeverOutline) {
  791. bool RunOnAllFunctions =
  792. (Opt.EnableMachineOutliner == RunOutliner::AlwaysOutline);
  793. bool AddOutliner = RunOnAllFunctions || TM.Options.SupportsDefaultOutlining;
  794. if (AddOutliner)
  795. addPass(MachineOutlinerPass(RunOnAllFunctions));
  796. }
  797. // Add passes that directly emit MI after all other MI passes.
  798. derived().addPreEmitPass2(addPass);
  799. return Error::success();
  800. }
  801. /// Add passes that optimize machine instructions in SSA form.
  802. template <typename Derived>
  803. void CodeGenPassBuilder<Derived>::addMachineSSAOptimization(
  804. AddMachinePass &addPass) const {
  805. // Pre-ra tail duplication.
  806. addPass(EarlyTailDuplicatePass());
  807. // Optimize PHIs before DCE: removing dead PHI cycles may make more
  808. // instructions dead.
  809. addPass(OptimizePHIsPass());
  810. // This pass merges large allocas. StackSlotColoring is a different pass
  811. // which merges spill slots.
  812. addPass(StackColoringPass());
  813. // If the target requests it, assign local variables to stack slots relative
  814. // to one another and simplify frame index references where possible.
  815. addPass(LocalStackSlotPass());
  816. // With optimization, dead code should already be eliminated. However
  817. // there is one known exception: lowered code for arguments that are only
  818. // used by tail calls, where the tail calls reuse the incoming stack
  819. // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll).
  820. addPass(DeadMachineInstructionElimPass());
  821. // Allow targets to insert passes that improve instruction level parallelism,
  822. // like if-conversion. Such passes will typically need dominator trees and
  823. // loop info, just like LICM and CSE below.
  824. derived().addILPOpts(addPass);
  825. addPass(EarlyMachineLICMPass());
  826. addPass(MachineCSEPass());
  827. addPass(MachineSinkingPass());
  828. addPass(PeepholeOptimizerPass());
  829. // Clean-up the dead code that may have been generated by peephole
  830. // rewriting.
  831. addPass(DeadMachineInstructionElimPass());
  832. }
  833. //===---------------------------------------------------------------------===//
  834. /// Register Allocation Pass Configuration
  835. //===---------------------------------------------------------------------===//
  836. /// Instantiate the default register allocator pass for this target for either
  837. /// the optimized or unoptimized allocation path. This will be added to the pass
  838. /// manager by addFastRegAlloc in the unoptimized case or addOptimizedRegAlloc
  839. /// in the optimized case.
  840. ///
  841. /// A target that uses the standard regalloc pass order for fast or optimized
  842. /// allocation may still override this for per-target regalloc
  843. /// selection. But -regalloc=... always takes precedence.
  844. template <typename Derived>
  845. void CodeGenPassBuilder<Derived>::addTargetRegisterAllocator(
  846. AddMachinePass &addPass, bool Optimized) const {
  847. if (Optimized)
  848. addPass(RAGreedyPass());
  849. else
  850. addPass(RAFastPass());
  851. }
  852. /// Find and instantiate the register allocation pass requested by this target
  853. /// at the current optimization level. Different register allocators are
  854. /// defined as separate passes because they may require different analysis.
  855. template <typename Derived>
  856. void CodeGenPassBuilder<Derived>::addRegAllocPass(AddMachinePass &addPass,
  857. bool Optimized) const {
  858. if (Opt.RegAlloc == RegAllocType::Default)
  859. // With no -regalloc= override, ask the target for a regalloc pass.
  860. derived().addTargetRegisterAllocator(addPass, Optimized);
  861. else if (Opt.RegAlloc == RegAllocType::Basic)
  862. addPass(RABasicPass());
  863. else if (Opt.RegAlloc == RegAllocType::Fast)
  864. addPass(RAFastPass());
  865. else if (Opt.RegAlloc == RegAllocType::Greedy)
  866. addPass(RAGreedyPass());
  867. else if (Opt.RegAlloc == RegAllocType::PBQP)
  868. addPass(RAPBQPPass());
  869. else
  870. llvm_unreachable("unknonwn register allocator type");
  871. }
  872. template <typename Derived>
  873. Error CodeGenPassBuilder<Derived>::addRegAssignmentFast(
  874. AddMachinePass &addPass) const {
  875. if (Opt.RegAlloc != RegAllocType::Default &&
  876. Opt.RegAlloc != RegAllocType::Fast)
  877. return make_error<StringError>(
  878. "Must use fast (default) register allocator for unoptimized regalloc.",
  879. inconvertibleErrorCode());
  880. addRegAllocPass(addPass, false);
  881. return Error::success();
  882. }
  883. template <typename Derived>
  884. Error CodeGenPassBuilder<Derived>::addRegAssignmentOptimized(
  885. AddMachinePass &addPass) const {
  886. // Add the selected register allocation pass.
  887. addRegAllocPass(addPass, true);
  888. // Allow targets to change the register assignments before rewriting.
  889. derived().addPreRewrite(addPass);
  890. // Finally rewrite virtual registers.
  891. addPass(VirtRegRewriterPass());
  892. // Perform stack slot coloring and post-ra machine LICM.
  893. //
  894. // FIXME: Re-enable coloring with register when it's capable of adding
  895. // kill markers.
  896. addPass(StackSlotColoringPass());
  897. return Error::success();
  898. }
  899. /// Add the minimum set of target-independent passes that are required for
  900. /// register allocation. No coalescing or scheduling.
  901. template <typename Derived>
  902. Error CodeGenPassBuilder<Derived>::addFastRegAlloc(
  903. AddMachinePass &addPass) const {
  904. addPass(PHIEliminationPass());
  905. addPass(TwoAddressInstructionPass());
  906. return derived().addRegAssignmentFast(addPass);
  907. }
  908. /// Add standard target-independent passes that are tightly coupled with
  909. /// optimized register allocation, including coalescing, machine instruction
  910. /// scheduling, and register allocation itself.
  911. template <typename Derived>
  912. void CodeGenPassBuilder<Derived>::addOptimizedRegAlloc(
  913. AddMachinePass &addPass) const {
  914. addPass(DetectDeadLanesPass());
  915. addPass(ProcessImplicitDefsPass());
  916. // Edge splitting is smarter with machine loop info.
  917. addPass(PHIEliminationPass());
  918. // Eventually, we want to run LiveIntervals before PHI elimination.
  919. if (Opt.EarlyLiveIntervals)
  920. addPass(LiveIntervalsPass());
  921. addPass(TwoAddressInstructionPass());
  922. addPass(RegisterCoalescerPass());
  923. // The machine scheduler may accidentally create disconnected components
  924. // when moving subregister definitions around, avoid this by splitting them to
  925. // separate vregs before. Splitting can also improve reg. allocation quality.
  926. addPass(RenameIndependentSubregsPass());
  927. // PreRA instruction scheduling.
  928. addPass(MachineSchedulerPass());
  929. if (derived().addRegAssignmentOptimized(addPass)) {
  930. // Allow targets to expand pseudo instructions depending on the choice of
  931. // registers before MachineCopyPropagation.
  932. derived().addPostRewrite(addPass);
  933. // Copy propagate to forward register uses and try to eliminate COPYs that
  934. // were not coalesced.
  935. addPass(MachineCopyPropagationPass());
  936. // Run post-ra machine LICM to hoist reloads / remats.
  937. //
  938. // FIXME: can this move into MachineLateOptimization?
  939. addPass(MachineLICMPass());
  940. }
  941. }
  942. //===---------------------------------------------------------------------===//
  943. /// Post RegAlloc Pass Configuration
  944. //===---------------------------------------------------------------------===//
  945. /// Add passes that optimize machine instructions after register allocation.
  946. template <typename Derived>
  947. void CodeGenPassBuilder<Derived>::addMachineLateOptimization(
  948. AddMachinePass &addPass) const {
  949. // Branch folding must be run after regalloc and prolog/epilog insertion.
  950. addPass(BranchFolderPass());
  951. // Tail duplication.
  952. // Note that duplicating tail just increases code size and degrades
  953. // performance for targets that require Structured Control Flow.
  954. // In addition it can also make CFG irreducible. Thus we disable it.
  955. if (!TM.requiresStructuredCFG())
  956. addPass(TailDuplicatePass());
  957. // Copy propagation.
  958. addPass(MachineCopyPropagationPass());
  959. }
  960. /// Add standard basic block placement passes.
  961. template <typename Derived>
  962. void CodeGenPassBuilder<Derived>::addBlockPlacement(
  963. AddMachinePass &addPass) const {
  964. addPass(MachineBlockPlacementPass());
  965. // Run a separate pass to collect block placement statistics.
  966. if (Opt.EnableBlockPlacementStats)
  967. addPass(MachineBlockPlacementStatsPass());
  968. }
  969. } // namespace llvm
  970. #endif // LLVM_CODEGEN_CODEGENPASSBUILDER_H
  971. #ifdef __GNUC__
  972. #pragma GCC diagnostic pop
  973. #endif