CodeGenPassBuilder.h 45 KB

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