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