Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:22

0001 //===- Construction of codegen pass pipelines ------------------*- C++ -*--===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 /// \file
0009 ///
0010 /// Interfaces for producing common pass manager configurations.
0011 ///
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_PASSES_CODEGENPASSBUILDER_H
0015 #define LLVM_PASSES_CODEGENPASSBUILDER_H
0016 
0017 #include "llvm/ADT/SmallVector.h"
0018 #include "llvm/ADT/StringRef.h"
0019 #include "llvm/Analysis/AliasAnalysis.h"
0020 #include "llvm/Analysis/BasicAliasAnalysis.h"
0021 #include "llvm/Analysis/ProfileSummaryInfo.h"
0022 #include "llvm/Analysis/ScopedNoAliasAA.h"
0023 #include "llvm/Analysis/TargetTransformInfo.h"
0024 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
0025 #include "llvm/CodeGen/AssignmentTrackingAnalysis.h"
0026 #include "llvm/CodeGen/CallBrPrepare.h"
0027 #include "llvm/CodeGen/CodeGenPrepare.h"
0028 #include "llvm/CodeGen/DeadMachineInstructionElim.h"
0029 #include "llvm/CodeGen/DwarfEHPrepare.h"
0030 #include "llvm/CodeGen/EarlyIfConversion.h"
0031 #include "llvm/CodeGen/ExpandLargeDivRem.h"
0032 #include "llvm/CodeGen/ExpandLargeFpConvert.h"
0033 #include "llvm/CodeGen/ExpandMemCmp.h"
0034 #include "llvm/CodeGen/ExpandReductions.h"
0035 #include "llvm/CodeGen/FinalizeISel.h"
0036 #include "llvm/CodeGen/GCMetadata.h"
0037 #include "llvm/CodeGen/GlobalMerge.h"
0038 #include "llvm/CodeGen/GlobalMergeFunctions.h"
0039 #include "llvm/CodeGen/IndirectBrExpand.h"
0040 #include "llvm/CodeGen/InterleavedAccess.h"
0041 #include "llvm/CodeGen/InterleavedLoadCombine.h"
0042 #include "llvm/CodeGen/JMCInstrumenter.h"
0043 #include "llvm/CodeGen/LiveIntervals.h"
0044 #include "llvm/CodeGen/LocalStackSlotAllocation.h"
0045 #include "llvm/CodeGen/LowerEmuTLS.h"
0046 #include "llvm/CodeGen/MIRPrinter.h"
0047 #include "llvm/CodeGen/MachineCSE.h"
0048 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
0049 #include "llvm/CodeGen/MachineLICM.h"
0050 #include "llvm/CodeGen/MachineModuleInfo.h"
0051 #include "llvm/CodeGen/MachinePassManager.h"
0052 #include "llvm/CodeGen/MachineVerifier.h"
0053 #include "llvm/CodeGen/OptimizePHIs.h"
0054 #include "llvm/CodeGen/PHIElimination.h"
0055 #include "llvm/CodeGen/PeepholeOptimizer.h"
0056 #include "llvm/CodeGen/PreISelIntrinsicLowering.h"
0057 #include "llvm/CodeGen/RegAllocFast.h"
0058 #include "llvm/CodeGen/RegUsageInfoCollector.h"
0059 #include "llvm/CodeGen/RegUsageInfoPropagate.h"
0060 #include "llvm/CodeGen/RegisterUsageInfo.h"
0061 #include "llvm/CodeGen/ReplaceWithVeclib.h"
0062 #include "llvm/CodeGen/SafeStack.h"
0063 #include "llvm/CodeGen/SelectOptimize.h"
0064 #include "llvm/CodeGen/ShadowStackGCLowering.h"
0065 #include "llvm/CodeGen/SjLjEHPrepare.h"
0066 #include "llvm/CodeGen/StackColoring.h"
0067 #include "llvm/CodeGen/StackProtector.h"
0068 #include "llvm/CodeGen/TailDuplication.h"
0069 #include "llvm/CodeGen/TargetPassConfig.h"
0070 #include "llvm/CodeGen/TwoAddressInstructionPass.h"
0071 #include "llvm/CodeGen/UnreachableBlockElim.h"
0072 #include "llvm/CodeGen/WasmEHPrepare.h"
0073 #include "llvm/CodeGen/WinEHPrepare.h"
0074 #include "llvm/IR/PassManager.h"
0075 #include "llvm/IR/Verifier.h"
0076 #include "llvm/IRPrinter/IRPrintingPasses.h"
0077 #include "llvm/MC/MCAsmInfo.h"
0078 #include "llvm/MC/MCTargetOptions.h"
0079 #include "llvm/Support/CodeGen.h"
0080 #include "llvm/Support/Debug.h"
0081 #include "llvm/Support/Error.h"
0082 #include "llvm/Support/ErrorHandling.h"
0083 #include "llvm/Target/CGPassBuilderOption.h"
0084 #include "llvm/Target/TargetMachine.h"
0085 #include "llvm/Transforms/CFGuard.h"
0086 #include "llvm/Transforms/Scalar/ConstantHoisting.h"
0087 #include "llvm/Transforms/Scalar/LoopPassManager.h"
0088 #include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
0089 #include "llvm/Transforms/Scalar/LowerConstantIntrinsics.h"
0090 #include "llvm/Transforms/Scalar/MergeICmps.h"
0091 #include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
0092 #include "llvm/Transforms/Scalar/ScalarizeMaskedMemIntrin.h"
0093 #include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
0094 #include "llvm/Transforms/Utils/LowerInvoke.h"
0095 #include <cassert>
0096 #include <type_traits>
0097 #include <utility>
0098 
0099 namespace llvm {
0100 
0101 // FIXME: Dummy target independent passes definitions that have not yet been
0102 // ported to new pass manager. Once they do, remove these.
0103 #define DUMMY_FUNCTION_PASS(NAME, PASS_NAME)                                   \
0104   struct PASS_NAME : public PassInfoMixin<PASS_NAME> {                         \
0105     template <typename... Ts> PASS_NAME(Ts &&...) {}                           \
0106     PreservedAnalyses run(Function &, FunctionAnalysisManager &) {             \
0107       return PreservedAnalyses::all();                                         \
0108     }                                                                          \
0109   };
0110 #define DUMMY_MACHINE_MODULE_PASS(NAME, PASS_NAME)                             \
0111   struct PASS_NAME : public PassInfoMixin<PASS_NAME> {                         \
0112     template <typename... Ts> PASS_NAME(Ts &&...) {}                           \
0113     PreservedAnalyses run(Module &, ModuleAnalysisManager &) {                 \
0114       return PreservedAnalyses::all();                                         \
0115     }                                                                          \
0116   };
0117 #define DUMMY_MACHINE_FUNCTION_PASS(NAME, PASS_NAME)                           \
0118   struct PASS_NAME : public PassInfoMixin<PASS_NAME> {                         \
0119     template <typename... Ts> PASS_NAME(Ts &&...) {}                           \
0120     PreservedAnalyses run(MachineFunction &,                                   \
0121                           MachineFunctionAnalysisManager &) {                  \
0122       return PreservedAnalyses::all();                                         \
0123     }                                                                          \
0124   };
0125 #include "llvm/Passes/MachinePassRegistry.def"
0126 
0127 /// This class provides access to building LLVM's passes.
0128 ///
0129 /// Its members provide the baseline state available to passes during their
0130 /// construction. The \c MachinePassRegistry.def file specifies how to construct
0131 /// all of the built-in passes, and those may reference these members during
0132 /// construction.
0133 template <typename DerivedT, typename TargetMachineT> class CodeGenPassBuilder {
0134 public:
0135   explicit CodeGenPassBuilder(TargetMachineT &TM,
0136                               const CGPassBuilderOption &Opts,
0137                               PassInstrumentationCallbacks *PIC)
0138       : TM(TM), Opt(Opts), PIC(PIC) {
0139     // Target could set CGPassBuilderOption::MISchedPostRA to true to achieve
0140     //     substitutePass(&PostRASchedulerID, &PostMachineSchedulerID)
0141 
0142     // Target should override TM.Options.EnableIPRA in their target-specific
0143     // LLVMTM ctor. See TargetMachine::setGlobalISel for example.
0144     if (Opt.EnableIPRA)
0145       TM.Options.EnableIPRA = *Opt.EnableIPRA;
0146 
0147     if (Opt.EnableGlobalISelAbort)
0148       TM.Options.GlobalISelAbort = *Opt.EnableGlobalISelAbort;
0149 
0150     if (!Opt.OptimizeRegAlloc)
0151       Opt.OptimizeRegAlloc = getOptLevel() != CodeGenOptLevel::None;
0152   }
0153 
0154   Error buildPipeline(ModulePassManager &MPM, raw_pwrite_stream &Out,
0155                       raw_pwrite_stream *DwoOut,
0156                       CodeGenFileType FileType) const;
0157 
0158   PassInstrumentationCallbacks *getPassInstrumentationCallbacks() const {
0159     return PIC;
0160   }
0161 
0162 protected:
0163   template <typename PassT>
0164   using has_required_t = decltype(std::declval<PassT &>().isRequired());
0165 
0166   template <typename PassT>
0167   using is_module_pass_t = decltype(std::declval<PassT &>().run(
0168       std::declval<Module &>(), std::declval<ModuleAnalysisManager &>()));
0169 
0170   template <typename PassT>
0171   using is_function_pass_t = decltype(std::declval<PassT &>().run(
0172       std::declval<Function &>(), std::declval<FunctionAnalysisManager &>()));
0173 
0174   template <typename PassT>
0175   using is_machine_function_pass_t = decltype(std::declval<PassT &>().run(
0176       std::declval<MachineFunction &>(),
0177       std::declval<MachineFunctionAnalysisManager &>()));
0178 
0179   // Function object to maintain state while adding codegen IR passes.
0180   // TODO: add a Function -> MachineFunction adaptor and merge
0181   // AddIRPass/AddMachinePass so we can have a function pipeline that runs both
0182   // function passes and machine function passes.
0183   class AddIRPass {
0184   public:
0185     AddIRPass(ModulePassManager &MPM, const DerivedT &PB) : MPM(MPM), PB(PB) {}
0186     ~AddIRPass() {
0187       if (!FPM.isEmpty())
0188         MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
0189     }
0190 
0191     template <typename PassT>
0192     void operator()(PassT &&Pass, StringRef Name = PassT::name()) {
0193       static_assert((is_detected<is_function_pass_t, PassT>::value ||
0194                      is_detected<is_module_pass_t, PassT>::value) &&
0195                     "Only module pass and function pass are supported.");
0196       bool Required = false;
0197       if constexpr (is_detected<has_required_t, PassT>::value)
0198         Required = PassT::isRequired();
0199       if (!PB.runBeforeAdding(Name) && !Required)
0200         return;
0201 
0202       // Add Function Pass
0203       if constexpr (is_detected<is_function_pass_t, PassT>::value) {
0204         FPM.addPass(std::forward<PassT>(Pass));
0205       } else {
0206         // Add Module Pass
0207         if (!FPM.isEmpty()) {
0208           MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
0209           FPM = FunctionPassManager();
0210         }
0211 
0212         MPM.addPass(std::forward<PassT>(Pass));
0213       }
0214     }
0215 
0216   private:
0217     ModulePassManager &MPM;
0218     FunctionPassManager FPM;
0219     const DerivedT &PB;
0220   };
0221 
0222   // Function object to maintain state while adding codegen machine passes.
0223   class AddMachinePass {
0224   public:
0225     AddMachinePass(ModulePassManager &MPM, const DerivedT &PB)
0226         : MPM(MPM), PB(PB) {}
0227     ~AddMachinePass() {
0228       if (!MFPM.isEmpty()) {
0229         FunctionPassManager FPM;
0230         FPM.addPass(
0231             createFunctionToMachineFunctionPassAdaptor(std::move(MFPM)));
0232         FPM.addPass(InvalidateAnalysisPass<MachineFunctionAnalysis>());
0233         MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
0234       }
0235     }
0236 
0237     template <typename PassT>
0238     void operator()(PassT &&Pass, bool Force = false,
0239                     StringRef Name = PassT::name()) {
0240       static_assert((is_detected<is_machine_function_pass_t, PassT>::value ||
0241                      is_detected<is_module_pass_t, PassT>::value) &&
0242                     "Only module pass and function pass are supported.");
0243 
0244       if (!Force && !PB.runBeforeAdding(Name))
0245         return;
0246 
0247       // Add Function Pass
0248       if constexpr (is_detected<is_machine_function_pass_t, PassT>::value) {
0249         MFPM.addPass(std::forward<PassT>(Pass));
0250       } else {
0251         // Add Module Pass
0252         if (!MFPM.isEmpty()) {
0253           MPM.addPass(createModuleToFunctionPassAdaptor(
0254               createFunctionToMachineFunctionPassAdaptor(std::move(MFPM))));
0255           MFPM = MachineFunctionPassManager();
0256         }
0257 
0258         MPM.addPass(std::forward<PassT>(Pass));
0259       }
0260 
0261       for (auto &C : PB.AfterCallbacks)
0262         C(Name, MFPM);
0263     }
0264 
0265   private:
0266     ModulePassManager &MPM;
0267     MachineFunctionPassManager MFPM;
0268     const DerivedT &PB;
0269   };
0270 
0271   TargetMachineT &TM;
0272   CGPassBuilderOption Opt;
0273   PassInstrumentationCallbacks *PIC;
0274 
0275   template <typename TMC> TMC &getTM() const { return static_cast<TMC &>(TM); }
0276   CodeGenOptLevel getOptLevel() const { return TM.getOptLevel(); }
0277 
0278   /// Check whether or not GlobalISel should abort on error.
0279   /// When this is disabled, GlobalISel will fall back on SDISel instead of
0280   /// erroring out.
0281   bool isGlobalISelAbortEnabled() const {
0282     return TM.Options.GlobalISelAbort == GlobalISelAbortMode::Enable;
0283   }
0284 
0285   /// Check whether or not a diagnostic should be emitted when GlobalISel
0286   /// uses the fallback path. In other words, it will emit a diagnostic
0287   /// when GlobalISel failed and isGlobalISelAbortEnabled is false.
0288   bool reportDiagnosticWhenGlobalISelFallback() const {
0289     return TM.Options.GlobalISelAbort == GlobalISelAbortMode::DisableWithDiag;
0290   }
0291 
0292   /// addInstSelector - This method should install an instruction selector pass,
0293   /// which converts from LLVM code to machine instructions.
0294   Error addInstSelector(AddMachinePass &) const {
0295     return make_error<StringError>("addInstSelector is not overridden",
0296                                    inconvertibleErrorCode());
0297   }
0298 
0299   /// Target can override this to add GlobalMergePass before all IR passes.
0300   void addGlobalMergePass(AddIRPass &) const {}
0301 
0302   /// Add passes that optimize instruction level parallelism for out-of-order
0303   /// targets. These passes are run while the machine code is still in SSA
0304   /// form, so they can use MachineTraceMetrics to control their heuristics.
0305   ///
0306   /// All passes added here should preserve the MachineDominatorTree,
0307   /// MachineLoopInfo, and MachineTraceMetrics analyses.
0308   void addILPOpts(AddMachinePass &) const {}
0309 
0310   /// This method may be implemented by targets that want to run passes
0311   /// immediately before register allocation.
0312   void addPreRegAlloc(AddMachinePass &) const {}
0313 
0314   /// addPreRewrite - Add passes to the optimized register allocation pipeline
0315   /// after register allocation is complete, but before virtual registers are
0316   /// rewritten to physical registers.
0317   ///
0318   /// These passes must preserve VirtRegMap and LiveIntervals, and when running
0319   /// after RABasic or RAGreedy, they should take advantage of LiveRegMatrix.
0320   /// When these passes run, VirtRegMap contains legal physreg assignments for
0321   /// all virtual registers.
0322   ///
0323   /// Note if the target overloads addRegAssignAndRewriteOptimized, this may not
0324   /// be honored. This is also not generally used for the fast variant,
0325   /// where the allocation and rewriting are done in one pass.
0326   void addPreRewrite(AddMachinePass &) const {}
0327 
0328   /// Add passes to be run immediately after virtual registers are rewritten
0329   /// to physical registers.
0330   void addPostRewrite(AddMachinePass &) const {}
0331 
0332   /// This method may be implemented by targets that want to run passes after
0333   /// register allocation pass pipeline but before prolog-epilog insertion.
0334   void addPostRegAlloc(AddMachinePass &) const {}
0335 
0336   /// This method may be implemented by targets that want to run passes after
0337   /// prolog-epilog insertion and before the second instruction scheduling pass.
0338   void addPreSched2(AddMachinePass &) const {}
0339 
0340   /// This pass may be implemented by targets that want to run passes
0341   /// immediately before machine code is emitted.
0342   void addPreEmitPass(AddMachinePass &) const {}
0343 
0344   /// Targets may add passes immediately before machine code is emitted in this
0345   /// callback. This is called even later than `addPreEmitPass`.
0346   // FIXME: Rename `addPreEmitPass` to something more sensible given its actual
0347   // position and remove the `2` suffix here as this callback is what
0348   // `addPreEmitPass` *should* be but in reality isn't.
0349   void addPreEmitPass2(AddMachinePass &) const {}
0350 
0351   /// {{@ For GlobalISel
0352   ///
0353 
0354   /// addPreISel - This method should add any "last minute" LLVM->LLVM
0355   /// passes (which are run just before instruction selector).
0356   void addPreISel(AddIRPass &) const {
0357     llvm_unreachable("addPreISel is not overridden");
0358   }
0359 
0360   /// This method should install an IR translator pass, which converts from
0361   /// LLVM code to machine instructions with possibly generic opcodes.
0362   Error addIRTranslator(AddMachinePass &) const {
0363     return make_error<StringError>("addIRTranslator is not overridden",
0364                                    inconvertibleErrorCode());
0365   }
0366 
0367   /// This method may be implemented by targets that want to run passes
0368   /// immediately before legalization.
0369   void addPreLegalizeMachineIR(AddMachinePass &) const {}
0370 
0371   /// This method should install a legalize pass, which converts the instruction
0372   /// sequence into one that can be selected by the target.
0373   Error addLegalizeMachineIR(AddMachinePass &) const {
0374     return make_error<StringError>("addLegalizeMachineIR is not overridden",
0375                                    inconvertibleErrorCode());
0376   }
0377 
0378   /// This method may be implemented by targets that want to run passes
0379   /// immediately before the register bank selection.
0380   void addPreRegBankSelect(AddMachinePass &) const {}
0381 
0382   /// This method should install a register bank selector pass, which
0383   /// assigns register banks to virtual registers without a register
0384   /// class or register banks.
0385   Error addRegBankSelect(AddMachinePass &) const {
0386     return make_error<StringError>("addRegBankSelect is not overridden",
0387                                    inconvertibleErrorCode());
0388   }
0389 
0390   /// This method may be implemented by targets that want to run passes
0391   /// immediately before the (global) instruction selection.
0392   void addPreGlobalInstructionSelect(AddMachinePass &) const {}
0393 
0394   /// This method should install a (global) instruction selector pass, which
0395   /// converts possibly generic instructions to fully target-specific
0396   /// instructions, thereby constraining all generic virtual registers to
0397   /// register classes.
0398   Error addGlobalInstructionSelect(AddMachinePass &) const {
0399     return make_error<StringError>(
0400         "addGlobalInstructionSelect is not overridden",
0401         inconvertibleErrorCode());
0402   }
0403   /// @}}
0404 
0405   /// High level function that adds all passes necessary to go from llvm IR
0406   /// representation to the MI representation.
0407   /// Adds IR based lowering and target specific optimization passes and finally
0408   /// the core instruction selection passes.
0409   void addISelPasses(AddIRPass &) const;
0410 
0411   /// Add the actual instruction selection passes. This does not include
0412   /// preparation passes on IR.
0413   Error addCoreISelPasses(AddMachinePass &) const;
0414 
0415   /// Add the complete, standard set of LLVM CodeGen passes.
0416   /// Fully developed targets will not generally override this.
0417   Error addMachinePasses(AddMachinePass &) const;
0418 
0419   /// Add passes to lower exception handling for the code generator.
0420   void addPassesToHandleExceptions(AddIRPass &) const;
0421 
0422   /// Add common target configurable passes that perform LLVM IR to IR
0423   /// transforms following machine independent optimization.
0424   void addIRPasses(AddIRPass &) const;
0425 
0426   /// Add pass to prepare the LLVM IR for code generation. This should be done
0427   /// before exception handling preparation passes.
0428   void addCodeGenPrepare(AddIRPass &) const;
0429 
0430   /// Add common passes that perform LLVM IR to IR transforms in preparation for
0431   /// instruction selection.
0432   void addISelPrepare(AddIRPass &) const;
0433 
0434   /// Methods with trivial inline returns are convenient points in the common
0435   /// codegen pass pipeline where targets may insert passes. Methods with
0436   /// out-of-line standard implementations are major CodeGen stages called by
0437   /// addMachinePasses. Some targets may override major stages when inserting
0438   /// passes is insufficient, but maintaining overriden stages is more work.
0439   ///
0440 
0441   /// addMachineSSAOptimization - Add standard passes that optimize machine
0442   /// instructions in SSA form.
0443   void addMachineSSAOptimization(AddMachinePass &) const;
0444 
0445   /// addFastRegAlloc - Add the minimum set of target-independent passes that
0446   /// are required for fast register allocation.
0447   Error addFastRegAlloc(AddMachinePass &) const;
0448 
0449   /// addOptimizedRegAlloc - Add passes related to register allocation.
0450   /// CodeGenTargetMachineImpl provides standard regalloc passes for most
0451   /// targets.
0452   void addOptimizedRegAlloc(AddMachinePass &) const;
0453 
0454   /// Add passes that optimize machine instructions after register allocation.
0455   void addMachineLateOptimization(AddMachinePass &) const;
0456 
0457   /// addGCPasses - Add late codegen passes that analyze code for garbage
0458   /// collection. This should return true if GC info should be printed after
0459   /// these passes.
0460   void addGCPasses(AddMachinePass &) const {}
0461 
0462   /// Add standard basic block placement passes.
0463   void addBlockPlacement(AddMachinePass &) const;
0464 
0465   using CreateMCStreamer =
0466       std::function<Expected<std::unique_ptr<MCStreamer>>(MCContext &)>;
0467   void addAsmPrinter(AddMachinePass &, CreateMCStreamer) const {
0468     llvm_unreachable("addAsmPrinter is not overridden");
0469   }
0470 
0471   /// Utilities for targets to add passes to the pass manager.
0472   ///
0473 
0474   /// createTargetRegisterAllocator - Create the register allocator pass for
0475   /// this target at the current optimization level.
0476   void addTargetRegisterAllocator(AddMachinePass &, bool Optimized) const;
0477 
0478   /// addMachinePasses helper to create the target-selected or overriden
0479   /// regalloc pass.
0480   void addRegAllocPass(AddMachinePass &, bool Optimized) const;
0481 
0482   /// Add core register alloator passes which do the actual register assignment
0483   /// and rewriting. \returns true if any passes were added.
0484   Error addRegAssignmentFast(AddMachinePass &) const;
0485   Error addRegAssignmentOptimized(AddMachinePass &) const;
0486 
0487   /// Allow the target to disable a specific pass by default.
0488   /// Backend can declare unwanted passes in constructor.
0489   template <typename... PassTs> void disablePass() {
0490     BeforeCallbacks.emplace_back(
0491         [](StringRef Name) { return ((Name != PassTs::name()) && ...); });
0492   }
0493 
0494   /// Insert InsertedPass pass after TargetPass pass.
0495   /// Only machine function passes are supported.
0496   template <typename TargetPassT, typename InsertedPassT>
0497   void insertPass(InsertedPassT &&Pass) {
0498     AfterCallbacks.emplace_back(
0499         [&](StringRef Name, MachineFunctionPassManager &MFPM) mutable {
0500           if (Name == TargetPassT::name())
0501             MFPM.addPass(std::forward<InsertedPassT>(Pass));
0502         });
0503   }
0504 
0505 private:
0506   DerivedT &derived() { return static_cast<DerivedT &>(*this); }
0507   const DerivedT &derived() const {
0508     return static_cast<const DerivedT &>(*this);
0509   }
0510 
0511   bool runBeforeAdding(StringRef Name) const {
0512     bool ShouldAdd = true;
0513     for (auto &C : BeforeCallbacks)
0514       ShouldAdd &= C(Name);
0515     return ShouldAdd;
0516   }
0517 
0518   void setStartStopPasses(const TargetPassConfig::StartStopInfo &Info) const;
0519 
0520   Error verifyStartStop(const TargetPassConfig::StartStopInfo &Info) const;
0521 
0522   mutable SmallVector<llvm::unique_function<bool(StringRef)>, 4>
0523       BeforeCallbacks;
0524   mutable SmallVector<
0525       llvm::unique_function<void(StringRef, MachineFunctionPassManager &)>, 4>
0526       AfterCallbacks;
0527 
0528   /// Helper variable for `-start-before/-start-after/-stop-before/-stop-after`
0529   mutable bool Started = true;
0530   mutable bool Stopped = true;
0531 };
0532 
0533 template <typename Derived, typename TargetMachineT>
0534 Error CodeGenPassBuilder<Derived, TargetMachineT>::buildPipeline(
0535     ModulePassManager &MPM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
0536     CodeGenFileType FileType) const {
0537   auto StartStopInfo = TargetPassConfig::getStartStopInfo(*PIC);
0538   if (!StartStopInfo)
0539     return StartStopInfo.takeError();
0540   setStartStopPasses(*StartStopInfo);
0541 
0542   bool PrintAsm = TargetPassConfig::willCompleteCodeGenPipeline();
0543   bool PrintMIR = !PrintAsm && FileType != CodeGenFileType::Null;
0544 
0545   {
0546     AddIRPass addIRPass(MPM, derived());
0547     addIRPass(RequireAnalysisPass<MachineModuleAnalysis, Module>());
0548     addIRPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
0549     addIRPass(RequireAnalysisPass<CollectorMetadataAnalysis, Module>());
0550     addISelPasses(addIRPass);
0551   }
0552 
0553   AddMachinePass addPass(MPM, derived());
0554 
0555   if (PrintMIR)
0556     addPass(PrintMIRPreparePass(Out), /*Force=*/true);
0557 
0558   if (auto Err = addCoreISelPasses(addPass))
0559     return std::move(Err);
0560 
0561   if (auto Err = derived().addMachinePasses(addPass))
0562     return std::move(Err);
0563 
0564   if (!Opt.DisableVerify)
0565     addPass(MachineVerifierPass());
0566 
0567   if (PrintAsm) {
0568     derived().addAsmPrinter(
0569         addPass, [this, &Out, DwoOut, FileType](MCContext &Ctx) {
0570           return this->TM.createMCStreamer(Out, DwoOut, FileType, Ctx);
0571         });
0572   }
0573 
0574   if (PrintMIR)
0575     addPass(PrintMIRPass(Out), /*Force=*/true);
0576 
0577   return verifyStartStop(*StartStopInfo);
0578 }
0579 
0580 template <typename Derived, typename TargetMachineT>
0581 void CodeGenPassBuilder<Derived, TargetMachineT>::setStartStopPasses(
0582     const TargetPassConfig::StartStopInfo &Info) const {
0583   if (!Info.StartPass.empty()) {
0584     Started = false;
0585     BeforeCallbacks.emplace_back([this, &Info, AfterFlag = Info.StartAfter,
0586                                   Count = 0u](StringRef ClassName) mutable {
0587       if (Count == Info.StartInstanceNum) {
0588         if (AfterFlag) {
0589           AfterFlag = false;
0590           Started = true;
0591         }
0592         return Started;
0593       }
0594 
0595       auto PassName = PIC->getPassNameForClassName(ClassName);
0596       if (Info.StartPass == PassName && ++Count == Info.StartInstanceNum)
0597         Started = !Info.StartAfter;
0598 
0599       return Started;
0600     });
0601   }
0602 
0603   if (!Info.StopPass.empty()) {
0604     Stopped = false;
0605     BeforeCallbacks.emplace_back([this, &Info, AfterFlag = Info.StopAfter,
0606                                   Count = 0u](StringRef ClassName) mutable {
0607       if (Count == Info.StopInstanceNum) {
0608         if (AfterFlag) {
0609           AfterFlag = false;
0610           Stopped = true;
0611         }
0612         return !Stopped;
0613       }
0614 
0615       auto PassName = PIC->getPassNameForClassName(ClassName);
0616       if (Info.StopPass == PassName && ++Count == Info.StopInstanceNum)
0617         Stopped = !Info.StopAfter;
0618       return !Stopped;
0619     });
0620   }
0621 }
0622 
0623 template <typename Derived, typename TargetMachineT>
0624 Error CodeGenPassBuilder<Derived, TargetMachineT>::verifyStartStop(
0625     const TargetPassConfig::StartStopInfo &Info) const {
0626   if (Started && Stopped)
0627     return Error::success();
0628 
0629   if (!Started)
0630     return make_error<StringError>(
0631         "Can't find start pass \"" + Info.StartPass + "\".",
0632         std::make_error_code(std::errc::invalid_argument));
0633   if (!Stopped)
0634     return make_error<StringError>(
0635         "Can't find stop pass \"" + Info.StopPass + "\".",
0636         std::make_error_code(std::errc::invalid_argument));
0637   return Error::success();
0638 }
0639 
0640 template <typename Derived, typename TargetMachineT>
0641 void CodeGenPassBuilder<Derived, TargetMachineT>::addISelPasses(
0642     AddIRPass &addPass) const {
0643   derived().addGlobalMergePass(addPass);
0644   if (TM.useEmulatedTLS())
0645     addPass(LowerEmuTLSPass());
0646 
0647   addPass(PreISelIntrinsicLoweringPass(&TM));
0648   addPass(ExpandLargeDivRemPass(&TM));
0649   addPass(ExpandLargeFpConvertPass(&TM));
0650 
0651   derived().addIRPasses(addPass);
0652   derived().addCodeGenPrepare(addPass);
0653   addPassesToHandleExceptions(addPass);
0654   derived().addISelPrepare(addPass);
0655 }
0656 
0657 /// Add common target configurable passes that perform LLVM IR to IR transforms
0658 /// following machine independent optimization.
0659 template <typename Derived, typename TargetMachineT>
0660 void CodeGenPassBuilder<Derived, TargetMachineT>::addIRPasses(
0661     AddIRPass &addPass) const {
0662   // Before running any passes, run the verifier to determine if the input
0663   // coming from the front-end and/or optimizer is valid.
0664   if (!Opt.DisableVerify)
0665     addPass(VerifierPass());
0666 
0667   // Run loop strength reduction before anything else.
0668   if (getOptLevel() != CodeGenOptLevel::None && !Opt.DisableLSR) {
0669     addPass(createFunctionToLoopPassAdaptor(LoopStrengthReducePass(),
0670                                             /*UseMemorySSA=*/true));
0671   }
0672 
0673   if (getOptLevel() != CodeGenOptLevel::None) {
0674     // The MergeICmpsPass tries to create memcmp calls by grouping sequences of
0675     // loads and compares. ExpandMemCmpPass then tries to expand those calls
0676     // into optimally-sized loads and compares. The transforms are enabled by a
0677     // target lowering hook.
0678     if (!Opt.DisableMergeICmps)
0679       addPass(MergeICmpsPass());
0680     addPass(ExpandMemCmpPass(&TM));
0681   }
0682 
0683   // Run GC lowering passes for builtin collectors
0684   // TODO: add a pass insertion point here
0685   addPass(GCLoweringPass());
0686   addPass(ShadowStackGCLoweringPass());
0687   addPass(LowerConstantIntrinsicsPass());
0688 
0689   // Make sure that no unreachable blocks are instruction selected.
0690   addPass(UnreachableBlockElimPass());
0691 
0692   // Prepare expensive constants for SelectionDAG.
0693   if (getOptLevel() != CodeGenOptLevel::None && !Opt.DisableConstantHoisting)
0694     addPass(ConstantHoistingPass());
0695 
0696   // Replace calls to LLVM intrinsics (e.g., exp, log) operating on vector
0697   // operands with calls to the corresponding functions in a vector library.
0698   if (getOptLevel() != CodeGenOptLevel::None)
0699     addPass(ReplaceWithVeclib());
0700 
0701   if (getOptLevel() != CodeGenOptLevel::None &&
0702       !Opt.DisablePartialLibcallInlining)
0703     addPass(PartiallyInlineLibCallsPass());
0704 
0705   // Instrument function entry and exit, e.g. with calls to mcount().
0706   addPass(EntryExitInstrumenterPass(/*PostInlining=*/true));
0707 
0708   // Add scalarization of target's unsupported masked memory intrinsics pass.
0709   // the unsupported intrinsic will be replaced with a chain of basic blocks,
0710   // that stores/loads element one-by-one if the appropriate mask bit is set.
0711   addPass(ScalarizeMaskedMemIntrinPass());
0712 
0713   // Expand reduction intrinsics into shuffle sequences if the target wants to.
0714   addPass(ExpandReductionsPass());
0715 
0716   // Convert conditional moves to conditional jumps when profitable.
0717   if (getOptLevel() != CodeGenOptLevel::None && !Opt.DisableSelectOptimize)
0718     addPass(SelectOptimizePass(&TM));
0719 
0720   if (Opt.EnableGlobalMergeFunc)
0721     addPass(GlobalMergeFuncPass());
0722 }
0723 
0724 /// Turn exception handling constructs into something the code generators can
0725 /// handle.
0726 template <typename Derived, typename TargetMachineT>
0727 void CodeGenPassBuilder<Derived, TargetMachineT>::addPassesToHandleExceptions(
0728     AddIRPass &addPass) const {
0729   const MCAsmInfo *MCAI = TM.getMCAsmInfo();
0730   assert(MCAI && "No MCAsmInfo");
0731   switch (MCAI->getExceptionHandlingType()) {
0732   case ExceptionHandling::SjLj:
0733     // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both
0734     // Dwarf EH prepare needs to be run after SjLj prepare. Otherwise,
0735     // catch info can get misplaced when a selector ends up more than one block
0736     // removed from the parent invoke(s). This could happen when a landing
0737     // pad is shared by multiple invokes and is also a target of a normal
0738     // edge from elsewhere.
0739     addPass(SjLjEHPreparePass(&TM));
0740     [[fallthrough]];
0741   case ExceptionHandling::DwarfCFI:
0742   case ExceptionHandling::ARM:
0743   case ExceptionHandling::AIX:
0744   case ExceptionHandling::ZOS:
0745     addPass(DwarfEHPreparePass(&TM));
0746     break;
0747   case ExceptionHandling::WinEH:
0748     // We support using both GCC-style and MSVC-style exceptions on Windows, so
0749     // add both preparation passes. Each pass will only actually run if it
0750     // recognizes the personality function.
0751     addPass(WinEHPreparePass());
0752     addPass(DwarfEHPreparePass(&TM));
0753     break;
0754   case ExceptionHandling::Wasm:
0755     // Wasm EH uses Windows EH instructions, but it does not need to demote PHIs
0756     // on catchpads and cleanuppads because it does not outline them into
0757     // funclets. Catchswitch blocks are not lowered in SelectionDAG, so we
0758     // should remove PHIs there.
0759     addPass(WinEHPreparePass(/*DemoteCatchSwitchPHIOnly=*/false));
0760     addPass(WasmEHPreparePass());
0761     break;
0762   case ExceptionHandling::None:
0763     addPass(LowerInvokePass());
0764 
0765     // The lower invoke pass may create unreachable code. Remove it.
0766     addPass(UnreachableBlockElimPass());
0767     break;
0768   }
0769 }
0770 
0771 /// Add pass to prepare the LLVM IR for code generation. This should be done
0772 /// before exception handling preparation passes.
0773 template <typename Derived, typename TargetMachineT>
0774 void CodeGenPassBuilder<Derived, TargetMachineT>::addCodeGenPrepare(
0775     AddIRPass &addPass) const {
0776   if (getOptLevel() != CodeGenOptLevel::None && !Opt.DisableCGP)
0777     addPass(CodeGenPreparePass(&TM));
0778   // TODO: Default ctor'd RewriteSymbolPass is no-op.
0779   // addPass(RewriteSymbolPass());
0780 }
0781 
0782 /// Add common passes that perform LLVM IR to IR transforms in preparation for
0783 /// instruction selection.
0784 template <typename Derived, typename TargetMachineT>
0785 void CodeGenPassBuilder<Derived, TargetMachineT>::addISelPrepare(
0786     AddIRPass &addPass) const {
0787   derived().addPreISel(addPass);
0788 
0789   addPass(CallBrPreparePass());
0790   // Add both the safe stack and the stack protection passes: each of them will
0791   // only protect functions that have corresponding attributes.
0792   addPass(SafeStackPass(&TM));
0793   addPass(StackProtectorPass(&TM));
0794 
0795   if (Opt.PrintISelInput)
0796     addPass(PrintFunctionPass(dbgs(),
0797                               "\n\n*** Final LLVM Code input to ISel ***\n"));
0798 
0799   // All passes which modify the LLVM IR are now complete; run the verifier
0800   // to ensure that the IR is valid.
0801   if (!Opt.DisableVerify)
0802     addPass(VerifierPass());
0803 }
0804 
0805 template <typename Derived, typename TargetMachineT>
0806 Error CodeGenPassBuilder<Derived, TargetMachineT>::addCoreISelPasses(
0807     AddMachinePass &addPass) const {
0808   // Enable FastISel with -fast-isel, but allow that to be overridden.
0809   TM.setO0WantsFastISel(Opt.EnableFastISelOption.value_or(true));
0810 
0811   // Determine an instruction selector.
0812   enum class SelectorType { SelectionDAG, FastISel, GlobalISel };
0813   SelectorType Selector;
0814 
0815   if (Opt.EnableFastISelOption && *Opt.EnableFastISelOption == true)
0816     Selector = SelectorType::FastISel;
0817   else if ((Opt.EnableGlobalISelOption &&
0818             *Opt.EnableGlobalISelOption == true) ||
0819            (TM.Options.EnableGlobalISel &&
0820             (!Opt.EnableGlobalISelOption ||
0821              *Opt.EnableGlobalISelOption == false)))
0822     Selector = SelectorType::GlobalISel;
0823   else if (TM.getOptLevel() == CodeGenOptLevel::None && TM.getO0WantsFastISel())
0824     Selector = SelectorType::FastISel;
0825   else
0826     Selector = SelectorType::SelectionDAG;
0827 
0828   // Set consistently TM.Options.EnableFastISel and EnableGlobalISel.
0829   if (Selector == SelectorType::FastISel) {
0830     TM.setFastISel(true);
0831     TM.setGlobalISel(false);
0832   } else if (Selector == SelectorType::GlobalISel) {
0833     TM.setFastISel(false);
0834     TM.setGlobalISel(true);
0835   }
0836 
0837   // Add instruction selector passes.
0838   if (Selector == SelectorType::GlobalISel) {
0839     if (auto Err = derived().addIRTranslator(addPass))
0840       return std::move(Err);
0841 
0842     derived().addPreLegalizeMachineIR(addPass);
0843 
0844     if (auto Err = derived().addLegalizeMachineIR(addPass))
0845       return std::move(Err);
0846 
0847     // Before running the register bank selector, ask the target if it
0848     // wants to run some passes.
0849     derived().addPreRegBankSelect(addPass);
0850 
0851     if (auto Err = derived().addRegBankSelect(addPass))
0852       return std::move(Err);
0853 
0854     derived().addPreGlobalInstructionSelect(addPass);
0855 
0856     if (auto Err = derived().addGlobalInstructionSelect(addPass))
0857       return std::move(Err);
0858 
0859     // Pass to reset the MachineFunction if the ISel failed.
0860     addPass(ResetMachineFunctionPass(reportDiagnosticWhenGlobalISelFallback(),
0861                                      isGlobalISelAbortEnabled()));
0862 
0863     // Provide a fallback path when we do not want to abort on
0864     // not-yet-supported input.
0865     if (!isGlobalISelAbortEnabled())
0866       if (auto Err = derived().addInstSelector(addPass))
0867         return std::move(Err);
0868 
0869   } else if (auto Err = derived().addInstSelector(addPass))
0870     return std::move(Err);
0871 
0872   // Expand pseudo-instructions emitted by ISel. Don't run the verifier before
0873   // FinalizeISel.
0874   addPass(FinalizeISelPass());
0875 
0876   // // Print the instruction selected machine code...
0877   // printAndVerify("After Instruction Selection");
0878 
0879   return Error::success();
0880 }
0881 
0882 /// Add the complete set of target-independent postISel code generator passes.
0883 ///
0884 /// This can be read as the standard order of major LLVM CodeGen stages. Stages
0885 /// with nontrivial configuration or multiple passes are broken out below in
0886 /// add%Stage routines.
0887 ///
0888 /// Any CodeGenPassBuilder<Derived, TargetMachine>::addXX routine may be
0889 /// overriden by the Target. The addPre/Post methods with empty header
0890 /// implementations allow injecting target-specific fixups just before or after
0891 /// major stages. Additionally, targets have the flexibility to change pass
0892 /// order within a stage by overriding default implementation of add%Stage
0893 /// routines below. Each technique has maintainability tradeoffs because
0894 /// alternate pass orders are not well supported. addPre/Post works better if
0895 /// the target pass is easily tied to a common pass. But if it has subtle
0896 /// dependencies on multiple passes, the target should override the stage
0897 /// instead.
0898 template <typename Derived, typename TargetMachineT>
0899 Error CodeGenPassBuilder<Derived, TargetMachineT>::addMachinePasses(
0900     AddMachinePass &addPass) const {
0901   // Add passes that optimize machine instructions in SSA form.
0902   if (getOptLevel() != CodeGenOptLevel::None) {
0903     derived().addMachineSSAOptimization(addPass);
0904   } else {
0905     // If the target requests it, assign local variables to stack slots relative
0906     // to one another and simplify frame index references where possible.
0907     addPass(LocalStackSlotAllocationPass());
0908   }
0909 
0910   if (TM.Options.EnableIPRA) {
0911     addPass(RequireAnalysisPass<PhysicalRegisterUsageAnalysis, Module>());
0912     addPass(RegUsageInfoPropagationPass());
0913   }
0914   // Run pre-ra passes.
0915   derived().addPreRegAlloc(addPass);
0916 
0917   // Run register allocation and passes that are tightly coupled with it,
0918   // including phi elimination and scheduling.
0919   if (*Opt.OptimizeRegAlloc) {
0920     derived().addOptimizedRegAlloc(addPass);
0921   } else {
0922     if (auto Err = derived().addFastRegAlloc(addPass))
0923       return Err;
0924   }
0925 
0926   // Run post-ra passes.
0927   derived().addPostRegAlloc(addPass);
0928 
0929   addPass(RemoveRedundantDebugValuesPass());
0930 
0931   // Insert prolog/epilog code.  Eliminate abstract frame index references...
0932   if (getOptLevel() != CodeGenOptLevel::None) {
0933     addPass(PostRAMachineSinkingPass());
0934     addPass(ShrinkWrapPass());
0935   }
0936 
0937   addPass(PrologEpilogInserterPass());
0938 
0939   /// Add passes that optimize machine instructions after register allocation.
0940   if (getOptLevel() != CodeGenOptLevel::None)
0941     derived().addMachineLateOptimization(addPass);
0942 
0943   // Expand pseudo instructions before second scheduling pass.
0944   addPass(ExpandPostRAPseudosPass());
0945 
0946   // Run pre-sched2 passes.
0947   derived().addPreSched2(addPass);
0948 
0949   if (Opt.EnableImplicitNullChecks)
0950     addPass(ImplicitNullChecksPass());
0951 
0952   // Second pass scheduler.
0953   // Let Target optionally insert this pass by itself at some other
0954   // point.
0955   if (getOptLevel() != CodeGenOptLevel::None &&
0956       !TM.targetSchedulesPostRAScheduling()) {
0957     if (Opt.MISchedPostRA)
0958       addPass(PostMachineSchedulerPass());
0959     else
0960       addPass(PostRASchedulerPass());
0961   }
0962 
0963   // GC
0964   derived().addGCPasses(addPass);
0965 
0966   // Basic block placement.
0967   if (getOptLevel() != CodeGenOptLevel::None)
0968     derived().addBlockPlacement(addPass);
0969 
0970   // Insert before XRay Instrumentation.
0971   addPass(FEntryInserterPass());
0972 
0973   addPass(XRayInstrumentationPass());
0974   addPass(PatchableFunctionPass());
0975 
0976   derived().addPreEmitPass(addPass);
0977 
0978   if (TM.Options.EnableIPRA)
0979     // Collect register usage information and produce a register mask of
0980     // clobbered registers, to be used to optimize call sites.
0981     addPass(RegUsageInfoCollectorPass());
0982 
0983   addPass(FuncletLayoutPass());
0984 
0985   addPass(StackMapLivenessPass());
0986   addPass(LiveDebugValuesPass());
0987   addPass(MachineSanitizerBinaryMetadata());
0988 
0989   if (TM.Options.EnableMachineOutliner &&
0990       getOptLevel() != CodeGenOptLevel::None &&
0991       Opt.EnableMachineOutliner != RunOutliner::NeverOutline) {
0992     bool RunOnAllFunctions =
0993         (Opt.EnableMachineOutliner == RunOutliner::AlwaysOutline);
0994     bool AddOutliner = RunOnAllFunctions || TM.Options.SupportsDefaultOutlining;
0995     if (AddOutliner)
0996       addPass(MachineOutlinerPass(RunOnAllFunctions));
0997   }
0998 
0999   // Add passes that directly emit MI after all other MI passes.
1000   derived().addPreEmitPass2(addPass);
1001 
1002   return Error::success();
1003 }
1004 
1005 /// Add passes that optimize machine instructions in SSA form.
1006 template <typename Derived, typename TargetMachineT>
1007 void CodeGenPassBuilder<Derived, TargetMachineT>::addMachineSSAOptimization(
1008     AddMachinePass &addPass) const {
1009   // Pre-ra tail duplication.
1010   addPass(EarlyTailDuplicatePass());
1011 
1012   // Optimize PHIs before DCE: removing dead PHI cycles may make more
1013   // instructions dead.
1014   addPass(OptimizePHIsPass());
1015 
1016   // This pass merges large allocas. StackSlotColoring is a different pass
1017   // which merges spill slots.
1018   addPass(StackColoringPass());
1019 
1020   // If the target requests it, assign local variables to stack slots relative
1021   // to one another and simplify frame index references where possible.
1022   addPass(LocalStackSlotAllocationPass());
1023 
1024   // With optimization, dead code should already be eliminated. However
1025   // there is one known exception: lowered code for arguments that are only
1026   // used by tail calls, where the tail calls reuse the incoming stack
1027   // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll).
1028   addPass(DeadMachineInstructionElimPass());
1029 
1030   // Allow targets to insert passes that improve instruction level parallelism,
1031   // like if-conversion. Such passes will typically need dominator trees and
1032   // loop info, just like LICM and CSE below.
1033   derived().addILPOpts(addPass);
1034 
1035   addPass(EarlyMachineLICMPass());
1036   addPass(MachineCSEPass());
1037 
1038   addPass(MachineSinkingPass());
1039 
1040   addPass(PeepholeOptimizerPass());
1041   // Clean-up the dead code that may have been generated by peephole
1042   // rewriting.
1043   addPass(DeadMachineInstructionElimPass());
1044 }
1045 
1046 //===---------------------------------------------------------------------===//
1047 /// Register Allocation Pass Configuration
1048 //===---------------------------------------------------------------------===//
1049 
1050 /// Instantiate the default register allocator pass for this target for either
1051 /// the optimized or unoptimized allocation path. This will be added to the pass
1052 /// manager by addFastRegAlloc in the unoptimized case or addOptimizedRegAlloc
1053 /// in the optimized case.
1054 ///
1055 /// A target that uses the standard regalloc pass order for fast or optimized
1056 /// allocation may still override this for per-target regalloc
1057 /// selection. But -regalloc=... always takes precedence.
1058 template <typename Derived, typename TargetMachineT>
1059 void CodeGenPassBuilder<Derived, TargetMachineT>::addTargetRegisterAllocator(
1060     AddMachinePass &addPass, bool Optimized) const {
1061   if (Optimized)
1062     addPass(RAGreedyPass());
1063   else
1064     addPass(RegAllocFastPass());
1065 }
1066 
1067 /// Find and instantiate the register allocation pass requested by this target
1068 /// at the current optimization level.  Different register allocators are
1069 /// defined as separate passes because they may require different analysis.
1070 template <typename Derived, typename TargetMachineT>
1071 void CodeGenPassBuilder<Derived, TargetMachineT>::addRegAllocPass(
1072     AddMachinePass &addPass, bool Optimized) const {
1073   // TODO: Parse Opt.RegAlloc to add register allocator.
1074 }
1075 
1076 template <typename Derived, typename TargetMachineT>
1077 Error CodeGenPassBuilder<Derived, TargetMachineT>::addRegAssignmentFast(
1078     AddMachinePass &addPass) const {
1079   // TODO: Ensure allocator is default or fast.
1080   addRegAllocPass(addPass, false);
1081   return Error::success();
1082 }
1083 
1084 template <typename Derived, typename TargetMachineT>
1085 Error CodeGenPassBuilder<Derived, TargetMachineT>::addRegAssignmentOptimized(
1086     AddMachinePass &addPass) const {
1087   // Add the selected register allocation pass.
1088   addRegAllocPass(addPass, true);
1089 
1090   // Allow targets to change the register assignments before rewriting.
1091   derived().addPreRewrite(addPass);
1092 
1093   // Finally rewrite virtual registers.
1094   addPass(VirtRegRewriterPass());
1095   // Perform stack slot coloring and post-ra machine LICM.
1096   //
1097   // FIXME: Re-enable coloring with register when it's capable of adding
1098   // kill markers.
1099   addPass(StackSlotColoringPass());
1100 
1101   return Error::success();
1102 }
1103 
1104 /// Add the minimum set of target-independent passes that are required for
1105 /// register allocation. No coalescing or scheduling.
1106 template <typename Derived, typename TargetMachineT>
1107 Error CodeGenPassBuilder<Derived, TargetMachineT>::addFastRegAlloc(
1108     AddMachinePass &addPass) const {
1109   addPass(PHIEliminationPass());
1110   addPass(TwoAddressInstructionPass());
1111   return derived().addRegAssignmentFast(addPass);
1112 }
1113 
1114 /// Add standard target-independent passes that are tightly coupled with
1115 /// optimized register allocation, including coalescing, machine instruction
1116 /// scheduling, and register allocation itself.
1117 template <typename Derived, typename TargetMachineT>
1118 void CodeGenPassBuilder<Derived, TargetMachineT>::addOptimizedRegAlloc(
1119     AddMachinePass &addPass) const {
1120   addPass(DetectDeadLanesPass());
1121 
1122   addPass(InitUndefPass());
1123 
1124   addPass(ProcessImplicitDefsPass());
1125 
1126   // Edge splitting is smarter with machine loop info.
1127   addPass(PHIEliminationPass());
1128 
1129   // Eventually, we want to run LiveIntervals before PHI elimination.
1130   if (Opt.EarlyLiveIntervals)
1131     addPass(RequireAnalysisPass<LiveIntervalsAnalysis, MachineFunction>());
1132 
1133   addPass(TwoAddressInstructionPass());
1134   addPass(RegisterCoalescerPass());
1135 
1136   // The machine scheduler may accidentally create disconnected components
1137   // when moving subregister definitions around, avoid this by splitting them to
1138   // separate vregs before. Splitting can also improve reg. allocation quality.
1139   addPass(RenameIndependentSubregsPass());
1140 
1141   // PreRA instruction scheduling.
1142   addPass(MachineSchedulerPass());
1143 
1144   if (derived().addRegAssignmentOptimized(addPass)) {
1145     // Allow targets to expand pseudo instructions depending on the choice of
1146     // registers before MachineCopyPropagation.
1147     derived().addPostRewrite(addPass);
1148 
1149     // Copy propagate to forward register uses and try to eliminate COPYs that
1150     // were not coalesced.
1151     addPass(MachineCopyPropagationPass());
1152 
1153     // Run post-ra machine LICM to hoist reloads / remats.
1154     //
1155     // FIXME: can this move into MachineLateOptimization?
1156     addPass(MachineLICMPass());
1157   }
1158 }
1159 
1160 //===---------------------------------------------------------------------===//
1161 /// Post RegAlloc Pass Configuration
1162 //===---------------------------------------------------------------------===//
1163 
1164 /// Add passes that optimize machine instructions after register allocation.
1165 template <typename Derived, typename TargetMachineT>
1166 void CodeGenPassBuilder<Derived, TargetMachineT>::addMachineLateOptimization(
1167     AddMachinePass &addPass) const {
1168   // Branch folding must be run after regalloc and prolog/epilog insertion.
1169   addPass(BranchFolderPass());
1170 
1171   // Tail duplication.
1172   // Note that duplicating tail just increases code size and degrades
1173   // performance for targets that require Structured Control Flow.
1174   // In addition it can also make CFG irreducible. Thus we disable it.
1175   if (!TM.requiresStructuredCFG())
1176     addPass(TailDuplicatePass());
1177 
1178   // Cleanup of redundant (identical) address/immediate loads.
1179   addPass(MachineLateInstrsCleanupPass());
1180 
1181   // Copy propagation.
1182   addPass(MachineCopyPropagationPass());
1183 }
1184 
1185 /// Add standard basic block placement passes.
1186 template <typename Derived, typename TargetMachineT>
1187 void CodeGenPassBuilder<Derived, TargetMachineT>::addBlockPlacement(
1188     AddMachinePass &addPass) const {
1189   addPass(MachineBlockPlacementPass());
1190   // Run a separate pass to collect block placement statistics.
1191   if (Opt.EnableBlockPlacementStats)
1192     addPass(MachineBlockPlacementStatsPass());
1193 }
1194 
1195 } // namespace llvm
1196 
1197 #endif // LLVM_PASSES_CODEGENPASSBUILDER_H