14#ifndef LLVM_PASSES_CODEGENPASSBUILDER_H
15#define LLVM_PASSES_CODEGENPASSBUILDER_H
89#define DUMMY_FUNCTION_PASS(NAME, PASS_NAME) \
90 struct PASS_NAME : public PassInfoMixin<PASS_NAME> { \
91 template <typename... Ts> PASS_NAME(Ts &&...) {} \
92 PreservedAnalyses run(Function &, FunctionAnalysisManager &) { \
93 return PreservedAnalyses::all(); \
96#define DUMMY_MACHINE_MODULE_PASS(NAME, PASS_NAME) \
97 struct PASS_NAME : public PassInfoMixin<PASS_NAME> { \
98 template <typename... Ts> PASS_NAME(Ts &&...) {} \
99 PreservedAnalyses run(Module &, ModuleAnalysisManager &) { \
100 return PreservedAnalyses::all(); \
103#define DUMMY_MACHINE_FUNCTION_PASS(NAME, PASS_NAME) \
104 struct PASS_NAME : public PassInfoMixin<PASS_NAME> { \
105 template <typename... Ts> PASS_NAME(Ts &&...) {} \
106 PreservedAnalyses run(MachineFunction &, \
107 MachineFunctionAnalysisManager &) { \
108 return PreservedAnalyses::all(); \
111#include "llvm/Passes/MachinePassRegistry.def"
149 template <
typename PassT>
152 template <
typename PassT>
154 std::declval<Module &>(), std::declval<ModuleAnalysisManager &>()));
156 template <
typename PassT>
158 std::declval<Function &>(), std::declval<FunctionAnalysisManager &>()));
160 template <
typename PassT>
162 std::declval<MachineFunction &>(),
163 std::declval<MachineFunctionAnalysisManager &>()));
177 template <
typename PassT>
181 "Only module pass and function pass are supported.");
182 bool Required =
false;
184 Required = PassT::isRequired();
185 if (!PB.runBeforeAdding(
Name) && !Required)
223 template <
typename PassT>
228 "Only module pass and function pass are supported.");
230 if (!Force && !PB.runBeforeAdding(
Name))
247 for (
auto &
C : PB.AfterCallbacks)
261 template <
typename TMC> TMC &
getTM()
const {
return static_cast<TMC &
>(
TM); }
281 return make_error<StringError>(
"addInstSelector is not overridden",
349 return make_error<StringError>(
"addIRTranslator is not overridden",
360 return make_error<StringError>(
"addLegalizeMachineIR is not overridden",
372 return make_error<StringError>(
"addRegBankSelect is not overridden",
385 return make_error<StringError>(
386 "addGlobalInstructionSelect is not overridden",
451 std::function<Expected<std::unique_ptr<MCStreamer>>(
MCContext &)>;
475 BeforeCallbacks.emplace_back(
481 template <
typename TargetPassT,
typename InsertedPassT>
483 AfterCallbacks.emplace_back(
485 if (
Name == TargetPassT::name())
491 DerivedT &derived() {
return static_cast<DerivedT &
>(*this); }
492 const DerivedT &derived()
const {
493 return static_cast<const DerivedT &
>(*this);
496 bool runBeforeAdding(StringRef
Name)
const {
497 bool ShouldAdd =
true;
498 for (
auto &
C : BeforeCallbacks)
499 ShouldAdd &=
C(
Name);
503 void setStartStopPasses(
const TargetPassConfig::StartStopInfo &
Info)
const;
505 Error verifyStartStop(
const TargetPassConfig::StartStopInfo &
Info)
const;
514 mutable bool Started =
true;
515 mutable bool Stopped =
true;
518template <
typename Derived,
typename TargetMachineT>
524 return StartStopInfo.takeError();
525 setStartStopPasses(*StartStopInfo);
535 addISelPasses(addIRPass);
543 if (
auto Err = addCoreISelPasses(addPass))
544 return std::move(Err);
546 if (
auto Err = derived().addMachinePasses(addPass))
547 return std::move(Err);
550 derived().addAsmPrinter(
551 addPass, [
this, &Out, DwoOut, FileType](
MCContext &Ctx) {
552 return this->TM.createMCStreamer(Out, DwoOut, FileType, Ctx);
559 return verifyStartStop(*StartStopInfo);
562template <
typename Derived,
typename TargetMachineT>
565 if (!
Info.StartPass.empty()) {
567 BeforeCallbacks.emplace_back([
this, &
Info, AfterFlag =
Info.StartAfter,
568 Count = 0u](
StringRef ClassName)
mutable {
569 if (Count == Info.StartInstanceNum) {
579 Started = !
Info.StartAfter;
585 if (!
Info.StopPass.empty()) {
587 BeforeCallbacks.emplace_back([
this, &
Info, AfterFlag =
Info.StopAfter,
588 Count = 0u](StringRef ClassName)
mutable {
589 if (Count == Info.StopInstanceNum) {
599 Stopped = !
Info.StopAfter;
605template <
typename Derived,
typename TargetMachineT>
606Error CodeGenPassBuilder<Derived, TargetMachineT>::verifyStartStop(
607 const TargetPassConfig::StartStopInfo &
Info)
const {
608 if (Started && Stopped)
609 return Error::success();
612 return make_error<StringError>(
613 "Can't find start pass \"" +
Info.StartPass +
"\".",
614 std::make_error_code(std::errc::invalid_argument));
616 return make_error<StringError>(
617 "Can't find stop pass \"" +
Info.StopPass +
"\".",
618 std::make_error_code(std::errc::invalid_argument));
619 return Error::success();
622template <
typename Derived,
typename TargetMachineT>
626 if (
TM.useEmulatedTLS())
631 derived().addIRPasses(addPass);
632 derived().addCodeGenPrepare(addPass);
633 addPassesToHandleExceptions(addPass);
634 derived().addISelPrepare(addPass);
639template <
typename Derived,
typename TargetMachineT>
648 if (getOptLevel() != CodeGenOptLevel::None && !Opt.
DisableLSR) {
656 if (getOptLevel() != CodeGenOptLevel::None) {
681 if (getOptLevel() != CodeGenOptLevel::None)
684 if (getOptLevel() != CodeGenOptLevel::None &&
706template <
typename Derived,
typename TargetMachineT>
710 assert(MCAI &&
"No MCAsmInfo");
712 case ExceptionHandling::SjLj:
721 case ExceptionHandling::DwarfCFI:
722 case ExceptionHandling::ARM:
723 case ExceptionHandling::AIX:
724 case ExceptionHandling::ZOS:
727 case ExceptionHandling::WinEH:
734 case ExceptionHandling::Wasm:
742 case ExceptionHandling::None:
753template <
typename Derived,
typename TargetMachineT>
756 if (getOptLevel() != CodeGenOptLevel::None && !Opt.
DisableCGP)
764template <
typename Derived,
typename TargetMachineT>
777 "\n\n*** Final LLVM Code input to ISel ***\n"));
785template <
typename Derived,
typename TargetMachineT>
793 SelectorType Selector;
796 Selector = SelectorType::FastISel;
799 (
TM.Options.EnableGlobalISel &&
802 Selector = SelectorType::GlobalISel;
803 else if (
TM.getOptLevel() == CodeGenOptLevel::None &&
TM.getO0WantsFastISel())
804 Selector = SelectorType::FastISel;
806 Selector = SelectorType::SelectionDAG;
809 if (Selector == SelectorType::FastISel) {
810 TM.setFastISel(
true);
811 TM.setGlobalISel(
false);
812 }
else if (Selector == SelectorType::GlobalISel) {
813 TM.setFastISel(
false);
814 TM.setGlobalISel(
true);
818 if (Selector == SelectorType::GlobalISel) {
819 if (
auto Err = derived().addIRTranslator(addPass))
820 return std::move(Err);
822 derived().addPreLegalizeMachineIR(addPass);
824 if (
auto Err = derived().addLegalizeMachineIR(addPass))
825 return std::move(Err);
829 derived().addPreRegBankSelect(addPass);
831 if (
auto Err = derived().addRegBankSelect(addPass))
832 return std::move(Err);
834 derived().addPreGlobalInstructionSelect(addPass);
836 if (
auto Err = derived().addGlobalInstructionSelect(addPass))
837 return std::move(Err);
840 addPass(ResetMachineFunctionPass(reportDiagnosticWhenGlobalISelFallback(),
841 isGlobalISelAbortEnabled()));
845 if (!isGlobalISelAbortEnabled())
846 if (
auto Err = derived().addInstSelector(addPass))
847 return std::move(Err);
849 }
else if (
auto Err = derived().addInstSelector(addPass))
850 return std::move(Err);
859 return Error::success();
878template <
typename Derived,
typename TargetMachineT>
882 if (getOptLevel() != CodeGenOptLevel::None) {
890 if (
TM.Options.EnableIPRA)
891 addPass(RegUsageInfoPropagationPass());
894 derived().addPreRegAlloc(addPass);
899 derived().addOptimizedRegAlloc(addPass);
901 if (
auto Err = derived().addFastRegAlloc(addPass))
906 derived().addPostRegAlloc(addPass);
908 addPass(RemoveRedundantDebugValuesPass());
911 if (getOptLevel() != CodeGenOptLevel::None) {
912 addPass(PostRAMachineSinkingPass());
913 addPass(ShrinkWrapPass());
916 addPass(PrologEpilogInserterPass());
919 if (getOptLevel() != CodeGenOptLevel::None)
920 derived().addMachineLateOptimization(addPass);
923 addPass(ExpandPostRAPseudosPass());
926 derived().addPreSched2(addPass);
929 addPass(ImplicitNullChecksPass());
934 if (getOptLevel() != CodeGenOptLevel::None &&
935 !
TM.targetSchedulesPostRAScheduling()) {
937 addPass(PostMachineSchedulerPass());
939 addPass(PostRASchedulerPass());
943 derived().addGCPasses(addPass);
946 if (getOptLevel() != CodeGenOptLevel::None)
947 derived().addBlockPlacement(addPass);
950 addPass(FEntryInserterPass());
952 addPass(XRayInstrumentationPass());
953 addPass(PatchableFunctionPass());
955 derived().addPreEmitPass(addPass);
957 if (
TM.Options.EnableIPRA)
960 addPass(RegUsageInfoCollectorPass());
962 addPass(FuncletLayoutPass());
964 addPass(StackMapLivenessPass());
965 addPass(LiveDebugValuesPass());
966 addPass(MachineSanitizerBinaryMetadata());
968 if (
TM.Options.EnableMachineOutliner &&
969 getOptLevel() != CodeGenOptLevel::None &&
971 bool RunOnAllFunctions =
973 bool AddOutliner = RunOnAllFunctions ||
TM.Options.SupportsDefaultOutlining;
975 addPass(MachineOutlinerPass(RunOnAllFunctions));
979 derived().addPreEmitPass2(addPass);
981 return Error::success();
985template <
typename Derived,
typename TargetMachineT>
989 addPass(EarlyTailDuplicatePass());
993 addPass(OptimizePHIsPass());
997 addPass(StackColoringPass());
1012 derived().addILPOpts(addPass);
1014 addPass(EarlyMachineLICMPass());
1015 addPass(MachineCSEPass());
1017 addPass(MachineSinkingPass());
1019 addPass(PeepholeOptimizerPass());
1037template <
typename Derived,
typename TargetMachineT>
1041 addPass(RAGreedyPass());
1049template <
typename Derived,
typename TargetMachineT>
1055template <
typename Derived,
typename TargetMachineT>
1059 addRegAllocPass(addPass,
false);
1060 return Error::success();
1063template <
typename Derived,
typename TargetMachineT>
1067 addRegAllocPass(addPass,
true);
1070 derived().addPreRewrite(addPass);
1073 addPass(VirtRegRewriterPass());
1078 addPass(StackSlotColoringPass());
1080 return Error::success();
1085template <
typename Derived,
typename TargetMachineT>
1090 return derived().addRegAssignmentFast(addPass);
1096template <
typename Derived,
typename TargetMachineT>
1099 addPass(DetectDeadLanesPass());
1101 addPass(InitUndefPass());
1103 addPass(ProcessImplicitDefsPass());
1113 addPass(RegisterCoalescerPass());
1118 addPass(RenameIndependentSubregsPass());
1121 addPass(MachineSchedulerPass());
1123 if (derived().addRegAssignmentOptimized(addPass)) {
1126 derived().addPostRewrite(addPass);
1130 addPass(MachineCopyPropagationPass());
1135 addPass(MachineLICMPass());
1144template <
typename Derived,
typename TargetMachineT>
1148 addPass(BranchFolderPass());
1154 if (!
TM.requiresStructuredCFG())
1155 addPass(TailDuplicatePass());
1158 addPass(MachineLateInstrsCleanupPass());
1161 addPass(MachineCopyPropagationPass());
1165template <
typename Derived,
typename TargetMachineT>
1168 addPass(MachineBlockPlacementPass());
1171 addPass(MachineBlockPlacementStatsPass());
This is the interface for LLVM's primary stateless and local alias analysis.
Analysis containing CSE Info
Defines an IR pass for CodeGen Prepare.
This file defines passes to print out IR in various granularities.
This file contains the declaration of the InterleavedAccessPass class, its corresponding pass name is...
This header provides classes for managing a pipeline of passes over loops in LLVM IR.
The header file for the LowerConstantIntrinsics pass as used by the new pass manager.
const char LLVMTargetMachineRef TM
PassInstrumentationCallbacks PIC
PassBuilder PB(Machine, PassOpts->PTO, std::nullopt, &PIC)
This header defines various interfaces for pass management in LLVM.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This is the interface for a metadata-based scoped no-alias analysis.
This file contains the declaration of the SelectOptimizePass class, its corresponding pass name is se...
This file defines the SmallVector class.
Target-Independent Code Generator Pass Configuration Options pass.
This is the interface for a metadata-based TBAA.
static const char PassName[]
AddIRPass(ModulePassManager &MPM, const DerivedT &PB)
void operator()(PassT &&Pass, StringRef Name=PassT::name())
AddMachinePass(ModulePassManager &MPM, const DerivedT &PB)
void operator()(PassT &&Pass, bool Force=false, StringRef Name=PassT::name())
This class provides access to building LLVM's passes.
void addPreRewrite(AddMachinePass &) const
addPreRewrite - Add passes to the optimized register allocation pipeline after register allocation is...
void addPostRegAlloc(AddMachinePass &) const
This method may be implemented by targets that want to run passes after register allocation pass pipe...
void insertPass(InsertedPassT &&Pass)
Insert InsertedPass pass after TargetPass pass.
void addPreGlobalInstructionSelect(AddMachinePass &) const
This method may be implemented by targets that want to run passes immediately before the (global) ins...
Error addRegAssignmentFast(AddMachinePass &) const
Add core register alloator passes which do the actual register assignment and rewriting.
Error addFastRegAlloc(AddMachinePass &) const
addFastRegAlloc - Add the minimum set of target-independent passes that are required for fast registe...
Error addIRTranslator(AddMachinePass &) const
This method should install an IR translator pass, which converts from LLVM code to machine instructio...
void addILPOpts(AddMachinePass &) const
Add passes that optimize instruction level parallelism for out-of-order targets.
void addRegAllocPass(AddMachinePass &, bool Optimized) const
addMachinePasses helper to create the target-selected or overriden regalloc pass.
void addPreSched2(AddMachinePass &) const
This method may be implemented by targets that want to run passes after prolog-epilog insertion and b...
std::function< Expected< std::unique_ptr< MCStreamer > >(MCContext &)> CreateMCStreamer
Error addRegBankSelect(AddMachinePass &) const
This method should install a register bank selector pass, which assigns register banks to virtual reg...
decltype(std::declval< PassT & >().isRequired()) has_required_t
bool isGlobalISelAbortEnabled() const
Check whether or not GlobalISel should abort on error.
void addPreRegAlloc(AddMachinePass &) const
This method may be implemented by targets that want to run passes immediately before register allocat...
void addGCPasses(AddMachinePass &) const
addGCPasses - Add late codegen passes that analyze code for garbage collection.
Error buildPipeline(ModulePassManager &MPM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, CodeGenFileType FileType) const
void addGlobalMergePass(AddIRPass &) const
Target can override this to add GlobalMergePass before all IR passes.
Error addLegalizeMachineIR(AddMachinePass &) const
This method should install a legalize pass, which converts the instruction sequence into one that can...
Error addCoreISelPasses(AddMachinePass &) const
Add the actual instruction selection passes.
void addOptimizedRegAlloc(AddMachinePass &) const
addOptimizedRegAlloc - Add passes related to register allocation.
void addISelPasses(AddIRPass &) const
High level function that adds all passes necessary to go from llvm IR representation to the MI repres...
PassInstrumentationCallbacks * PIC
void addMachineSSAOptimization(AddMachinePass &) const
Methods with trivial inline returns are convenient points in the common codegen pass pipeline where t...
decltype(std::declval< PassT & >().run(std::declval< Module & >(), std::declval< ModuleAnalysisManager & >())) is_module_pass_t
CodeGenPassBuilder(TargetMachineT &TM, const CGPassBuilderOption &Opts, PassInstrumentationCallbacks *PIC)
Error addRegAssignmentOptimized(AddMachinePass &) const
void addCodeGenPrepare(AddIRPass &) const
Add pass to prepare the LLVM IR for code generation.
Error addMachinePasses(AddMachinePass &) const
Add the complete, standard set of LLVM CodeGen passes.
void addISelPrepare(AddIRPass &) const
Add common passes that perform LLVM IR to IR transforms in preparation for instruction selection.
void disablePass()
Allow the target to disable a specific pass by default.
void addBlockPlacement(AddMachinePass &) const
Add standard basic block placement passes.
Error addInstSelector(AddMachinePass &) const
addInstSelector - This method should install an instruction selector pass, which converts from LLVM c...
void addTargetRegisterAllocator(AddMachinePass &, bool Optimized) const
Utilities for targets to add passes to the pass manager.
void addPreEmitPass2(AddMachinePass &) const
Targets may add passes immediately before machine code is emitted in this callback.
void addPostRewrite(AddMachinePass &) const
Add passes to be run immediately after virtual registers are rewritten to physical registers.
void addPreRegBankSelect(AddMachinePass &) const
This method may be implemented by targets that want to run passes immediately before the register ban...
void addPreEmitPass(AddMachinePass &) const
This pass may be implemented by targets that want to run passes immediately before machine code is em...
Error addGlobalInstructionSelect(AddMachinePass &) const
This method should install a (global) instruction selector pass, which converts possibly generic inst...
void addMachineLateOptimization(AddMachinePass &) const
Add passes that optimize machine instructions after register allocation.
void addIRPasses(AddIRPass &) const
Add common target configurable passes that perform LLVM IR to IR transforms following machine indepen...
decltype(std::declval< PassT & >().run(std::declval< Function & >(), std::declval< FunctionAnalysisManager & >())) is_function_pass_t
decltype(std::declval< PassT & >().run(std::declval< MachineFunction & >(), std::declval< MachineFunctionAnalysisManager & >())) is_machine_function_pass_t
void addPreLegalizeMachineIR(AddMachinePass &) const
This method may be implemented by targets that want to run passes immediately before legalization.
CodeGenOptLevel getOptLevel() const
void addPassesToHandleExceptions(AddIRPass &) const
Add passes to lower exception handling for the code generator.
PassInstrumentationCallbacks * getPassInstrumentationCallbacks() const
bool reportDiagnosticWhenGlobalISelFallback() const
Check whether or not a diagnostic should be emitted when GlobalISel uses the fallback path.
void addPreISel(AddIRPass &) const
{{@ For GlobalISel
void addAsmPrinter(AddMachinePass &, CreateMCStreamer) const
Lightweight error class with error context and mandatory checking.
This is a fast-path instruction selection class that generates poor code and doesn't support illegal ...
LowerIntrinsics - This pass rewrites calls to the llvm.gcread or llvm.gcwrite intrinsics,...
Performs Loop Strength Reduce Pass.
This class is intended to be used as a base class for asm properties and features specific to the tar...
ExceptionHandling getExceptionHandlingType() const
Context object for machine code objects.
This class manages callbacks registration, as well as provides a way for PassInstrumentation to pass ...
StringRef getPassNameForClassName(StringRef ClassName)
Get the pass name for a given pass class name.
LLVM_ATTRIBUTE_MINSIZE std::enable_if_t<!std::is_same_v< PassT, PassManager > > addPass(PassT &&Pass)
bool isEmpty() const
Returns if the pass manager contains any passes.
Pass interface - Implemented by all 'passes'.
Pass (for the new pass manager) for printing a Function as LLVM's text IR assembly.
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
StringRef - Represent a constant reference to a string, i.e.
static Expected< StartStopInfo > getStartStopInfo(PassInstrumentationCallbacks &PIC)
Returns pass name in -stop-before or -stop-after NOTE: New pass manager migration only.
static bool willCompleteCodeGenPipeline()
Returns true if none of the -stop-before and -stop-after options is set.
An abstract base class for streams implementations that also support a pwrite operation.
unique_function is a type-erasing functor similar to std::function.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
PassManager< Function > FunctionPassManager
Convenience typedef for a pass manager over functions.
ModuleToFunctionPassAdaptor createModuleToFunctionPassAdaptor(FunctionPassT &&Pass, bool EagerlyInvalidate=false)
A function to deduce a function pass type and wrap it in the templated adaptor.
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
PassManager< MachineFunction > MachineFunctionPassManager
Convenience typedef for a pass manager over functions.
typename detail::detector< void, Op, Args... >::value_t is_detected
Detects if a given trait holds for some set of arguments 'Args'.
CodeGenFileType
These enums are meant to be passed into addPassesToEmitFile to indicate what type of file to emit,...
FunctionToMachineFunctionPassAdaptor createFunctionToMachineFunctionPassAdaptor(MachineFunctionPassT &&Pass)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
CodeGenOptLevel
Code generation optimization level.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
std::enable_if_t< is_detected< HasRunOnLoopT, LoopPassT >::value, FunctionToLoopPassAdaptor > createFunctionToLoopPassAdaptor(LoopPassT &&Pass, bool UseMemorySSA=false, bool UseBlockFrequencyInfo=false, bool UseBranchProbabilityInfo=false)
A function to deduce a loop pass type and wrap it in the templated adaptor.
bool DisablePartialLibcallInlining
std::optional< bool > EnableGlobalISelOption
std::optional< bool > EnableIPRA
bool DisableConstantHoisting
std::optional< bool > OptimizeRegAlloc
RunOutliner EnableMachineOutliner
std::optional< bool > EnableFastISelOption
std::optional< GlobalISelAbortMode > EnableGlobalISelAbort
bool EnableImplicitNullChecks
bool EnableBlockPlacementStats
bool DisableSelectOptimize
A no-op pass template which simply forces a specific analysis result to be invalidated.
A utility pass template to force an analysis result to be available.