14#ifndef LLVM_PASSES_CODEGENPASSBUILDER_H
15#define LLVM_PASSES_CODEGENPASSBUILDER_H
130#include <type_traits>
137#define DUMMY_FUNCTION_PASS(NAME, PASS_NAME) \
138 struct PASS_NAME : public PassInfoMixin<PASS_NAME> { \
139 template <typename... Ts> PASS_NAME(Ts &&...) {} \
140 PreservedAnalyses run(Function &, FunctionAnalysisManager &) { \
141 return PreservedAnalyses::all(); \
144#define DUMMY_MACHINE_MODULE_PASS(NAME, PASS_NAME) \
145 struct PASS_NAME : public PassInfoMixin<PASS_NAME> { \
146 template <typename... Ts> PASS_NAME(Ts &&...) {} \
147 PreservedAnalyses run(Module &, ModuleAnalysisManager &) { \
148 return PreservedAnalyses::all(); \
151#define DUMMY_MACHINE_FUNCTION_PASS(NAME, PASS_NAME) \
152 struct PASS_NAME : public PassInfoMixin<PASS_NAME> { \
153 template <typename... Ts> PASS_NAME(Ts &&...) {} \
154 PreservedAnalyses run(MachineFunction &, \
155 MachineFunctionAnalysisManager &) { \
156 return PreservedAnalyses::all(); \
159#include "llvm/Passes/MachinePassRegistry.def"
182 TM.Options.EnableIPRA |=
TM.useIPRA();
201 template <
typename PassT>
203 std::declval<Module &>(), std::declval<ModuleAnalysisManager &>()));
205 template <
typename PassT>
207 std::declval<Function &>(), std::declval<FunctionAnalysisManager &>()));
209 template <
typename PassT>
211 std::declval<MachineFunction &>(),
212 std::declval<MachineFunctionAnalysisManager &>()));
223 template <
typename PassT>
228 "Only module pass and function pass are supported.");
229 if (!Force && !PB.runBeforeAdding(
Name))
244 if (PB.AddInCGSCCOrder)
247 PB.AddInCGSCCOrder =
true;
253 if (!PB.AddInCGSCCOrder)
256 PB.AddInCGSCCOrder =
false;
260 void flushFPMToMPM() {
263 if (PB.AddInCGSCCOrder) {
280 : MPM(MPM),
PB(
PB) {}
288 if (this->PB.AddInCGSCCOrder) {
295 template <
typename PassT>
300 "Only module pass and function pass are supported.");
302 if (!Force && !PB.runBeforeAdding(
Name))
314 for (
auto &
C : PB.AfterCallbacks)
320 if (PB.AddInCGSCCOrder)
323 PB.AddInCGSCCOrder =
true;
329 if (!PB.AddInCGSCCOrder)
332 PB.AddInCGSCCOrder =
false;
336 void flushMFPMToMPM() {
340 if (PB.AddInCGSCCOrder) {
360 template <
typename TMC> TMC &
getTM()
const {
return static_cast<TMC &
>(
TM); }
380 return make_error<StringError>(
"addInstSelector is not overridden",
448 return make_error<StringError>(
"addIRTranslator is not overridden",
459 return make_error<StringError>(
"addLegalizeMachineIR is not overridden",
471 return make_error<StringError>(
"addRegBankSelect is not overridden",
484 return make_error<StringError>(
485 "addGlobalInstructionSelect is not overridden",
551 std::function<Expected<std::unique_ptr<MCStreamer>>(
MCContext &)>;
575 BeforeCallbacks.emplace_back(
581 template <
typename TargetPassT,
typename InsertedPassT>
583 AfterCallbacks.emplace_back(
585 if (
Name == TargetPassT::name() &&
586 runBeforeAdding(InsertedPassT::name())) {
593 DerivedT &derived() {
return static_cast<DerivedT &
>(*this); }
594 const DerivedT &derived()
const {
595 return static_cast<const DerivedT &
>(*this);
599 bool ShouldAdd =
true;
600 for (
auto &
C : BeforeCallbacks)
601 ShouldAdd &=
C(
Name);
616 mutable bool Started =
true;
617 mutable bool Stopped =
true;
618 mutable bool AddInCGSCCOrder =
false;
621template <
typename Derived,
typename TargetMachineT>
627 return StartStopInfo.takeError();
628 setStartStopPasses(*StartStopInfo);
641 addISelPasses(addIRPass);
649 if (
auto Err = addCoreISelPasses(addPass))
650 return std::move(Err);
652 if (
auto Err = derived().addMachinePasses(addPass))
653 return std::move(Err);
659 derived().addAsmPrinter(
660 addPass, [
this, &Out, DwoOut, FileType](
MCContext &Ctx) {
661 return this->TM.createMCStreamer(Out, DwoOut, FileType, Ctx);
668 return verifyStartStop(*StartStopInfo);
671template <
typename Derived,
typename TargetMachineT>
674 if (!
Info.StartPass.empty()) {
676 BeforeCallbacks.emplace_back([
this, &Info, AfterFlag =
Info.StartAfter,
677 Count = 0u](
StringRef ClassName)
mutable {
678 if (Count == Info.StartInstanceNum) {
688 Started = !
Info.StartAfter;
694 if (!
Info.StopPass.empty()) {
696 BeforeCallbacks.emplace_back([
this, &Info, AfterFlag =
Info.StopAfter,
697 Count = 0u](
StringRef ClassName)
mutable {
698 if (Count == Info.StopInstanceNum) {
708 Stopped = !
Info.StopAfter;
714template <
typename Derived,
typename TargetMachineT>
717 if (Started && Stopped)
721 return make_error<StringError>(
722 "Can't find start pass \"" +
Info.StartPass +
"\".",
723 std::make_error_code(std::errc::invalid_argument));
725 return make_error<StringError>(
726 "Can't find stop pass \"" +
Info.StopPass +
"\".",
727 std::make_error_code(std::errc::invalid_argument));
731template <
typename Derived,
typename TargetMachineT>
734 derived().addGlobalMergePass(addPass);
735 if (TM.useEmulatedTLS())
742 derived().addIRPasses(addPass);
743 derived().addCodeGenPrepare(addPass);
744 addPassesToHandleExceptions(addPass);
745 derived().addISelPrepare(addPass);
750template <
typename Derived,
typename TargetMachineT>
759 if (getOptLevel() != CodeGenOptLevel::None && !Opt.
DisableLSR) {
769 if (getOptLevel() != CodeGenOptLevel::None) {
794 if (getOptLevel() != CodeGenOptLevel::None)
797 if (getOptLevel() != CodeGenOptLevel::None &&
823template <
typename Derived,
typename TargetMachineT>
826 const MCAsmInfo *MCAI = TM.getMCAsmInfo();
827 assert(MCAI &&
"No MCAsmInfo");
829 case ExceptionHandling::SjLj:
838 case ExceptionHandling::DwarfCFI:
839 case ExceptionHandling::ARM:
840 case ExceptionHandling::AIX:
841 case ExceptionHandling::ZOS:
844 case ExceptionHandling::WinEH:
851 case ExceptionHandling::Wasm:
859 case ExceptionHandling::None:
870template <
typename Derived,
typename TargetMachineT>
873 if (getOptLevel() != CodeGenOptLevel::None && !Opt.
DisableCGP)
881template <
typename Derived,
typename TargetMachineT>
884 derived().addPreISel(addPass);
889 if (getOptLevel() != CodeGenOptLevel::None)
900 "\n\n*** Final LLVM Code input to ISel ***\n"));
908template <
typename Derived,
typename TargetMachineT>
916 SelectorType Selector;
919 Selector = SelectorType::FastISel;
922 (TM.Options.EnableGlobalISel &&
925 Selector = SelectorType::GlobalISel;
926 else if (TM.getOptLevel() == CodeGenOptLevel::None && TM.getO0WantsFastISel())
927 Selector = SelectorType::FastISel;
929 Selector = SelectorType::SelectionDAG;
932 if (Selector == SelectorType::FastISel) {
933 TM.setFastISel(
true);
934 TM.setGlobalISel(
false);
935 }
else if (Selector == SelectorType::GlobalISel) {
936 TM.setFastISel(
false);
937 TM.setGlobalISel(
true);
941 if (Selector == SelectorType::GlobalISel) {
942 if (
auto Err = derived().addIRTranslator(addPass))
943 return std::move(Err);
945 derived().addPreLegalizeMachineIR(addPass);
947 if (
auto Err = derived().addLegalizeMachineIR(addPass))
948 return std::move(Err);
952 derived().addPreRegBankSelect(addPass);
954 if (
auto Err = derived().addRegBankSelect(addPass))
955 return std::move(Err);
957 derived().addPreGlobalInstructionSelect(addPass);
959 if (
auto Err = derived().addGlobalInstructionSelect(addPass))
960 return std::move(Err);
963 addPass(ResetMachineFunctionPass(reportDiagnosticWhenGlobalISelFallback(),
964 isGlobalISelAbortEnabled()));
968 if (!isGlobalISelAbortEnabled())
969 if (
auto Err = derived().addInstSelector(addPass))
970 return std::move(Err);
972 }
else if (
auto Err = derived().addInstSelector(addPass))
973 return std::move(Err);
1001template <
typename Derived,
typename TargetMachineT>
1005 if (getOptLevel() != CodeGenOptLevel::None) {
1006 derived().addMachineSSAOptimization(addPass);
1013 if (TM.Options.EnableIPRA) {
1018 derived().addPreRegAlloc(addPass);
1023 derived().addOptimizedRegAlloc(addPass);
1025 if (
auto Err = derived().addFastRegAlloc(addPass))
1030 derived().addPostRegAlloc(addPass);
1036 if (getOptLevel() != CodeGenOptLevel::None) {
1044 if (getOptLevel() != CodeGenOptLevel::None)
1045 derived().addMachineLateOptimization(addPass);
1051 derived().addPreSched2(addPass);
1054 addPass(ImplicitNullChecksPass());
1059 if (getOptLevel() != CodeGenOptLevel::None &&
1060 !TM.targetSchedulesPostRAScheduling()) {
1068 derived().addGCPasses(addPass);
1071 if (getOptLevel() != CodeGenOptLevel::None)
1072 derived().addBlockPlacement(addPass);
1080 derived().addPreEmitPass(addPass);
1082 if (TM.Options.EnableIPRA)
1087 addPass(FuncletLayoutPass());
1090 addPass(StackMapLivenessPass());
1092 getTM<TargetMachine>().
Options.ShouldEmitDebugEntryValues()));
1095 if (TM.Options.EnableMachineOutliner &&
1096 getOptLevel() != CodeGenOptLevel::None &&
1098 bool RunOnAllFunctions =
1100 bool AddOutliner = RunOnAllFunctions || TM.Options.SupportsDefaultOutlining;
1102 addPass(MachineOutlinerPass(RunOnAllFunctions));
1108 derived().addPreEmitPass2(addPass);
1114template <
typename Derived,
typename TargetMachineT>
1141 derived().addILPOpts(addPass);
1168template <
typename Derived,
typename TargetMachineT>
1183template <
typename Derived,
typename TargetMachineT>
1187 if (Opt.
RegAlloc > RegAllocType::Default) {
1189 case RegAllocType::Fast:
1192 case RegAllocType::Greedy:
1202 derived().addTargetRegisterAllocator(addPass, Optimized);
1205template <
typename Derived,
typename TargetMachineT>
1209 addRegAllocPass(addPass,
false);
1213template <
typename Derived,
typename TargetMachineT>
1217 addRegAllocPass(addPass,
true);
1220 derived().addPreRewrite(addPass);
1235template <
typename Derived,
typename TargetMachineT>
1240 return derived().addRegAssignmentFast(addPass);
1246template <
typename Derived,
typename TargetMachineT>
1287 if (
auto E = derived().addRegAssignmentOptimized(addPass)) {
1293 derived().addPostRewrite(addPass);
1310template <
typename Derived,
typename TargetMachineT>
1320 if (!TM.requiresStructuredCFG())
1331template <
typename Derived,
typename TargetMachineT>
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This is the interface for LLVM's primary stateless and local alias analysis.
This header provides classes for managing passes over SCCs of the call graph.
Analysis containing CSE Info
Defines an IR pass for CodeGen Prepare.
Analysis that tracks defined/used subregister lanes across COPY instructions and instructions that ge...
This file defines passes to print out IR in various granularities.
This header defines various interfaces for pass management in LLVM.
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.
PassInstrumentationCallbacks PIC
PassBuilder PB(Machine, PassOpts->PTO, std::nullopt, &PIC)
This pass is required to take advantage of the interprocedural register allocation infrastructure.
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[]
A pass that canonicalizes freeze instructions in a loop.
AddIRPass(ModulePassManager &MPM, const DerivedT &PB)
void operator()(PassT &&Pass, bool Force=false, StringRef Name=PassT::name())
void requireCGSCCOrder()
Setting this will add passes to the CGSCC pass manager.
void stopAddingInCGSCCOrder()
Stop adding passes to the CGSCC pass manager.
void stopAddingInCGSCCOrder()
Stop adding passes to the CGSCC pass manager.
AddMachinePass(ModulePassManager &MPM, const DerivedT &PB)
void operator()(PassT &&Pass, bool Force=false, StringRef Name=PassT::name())
void requireCGSCCOrder()
Setting this will add passes to the CGSCC pass manager.
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 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...
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 insertPass(InsertedPassT &&Pass) const
Insert InsertedPass pass after TargetPass pass.
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.
static ErrorSuccess success()
Create a success value.
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.
LLVM_ATTRIBUTE_MINSIZE void addPass(PassT &&Pass)
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...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
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.
LLVM_ABI 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'.
ModuleToPostOrderCGSCCPassAdaptor createModuleToPostOrderCGSCCPassAdaptor(CGSCCPassT &&Pass)
A function to deduce a function pass type and wrap it in the templated adaptor.
CGSCCToFunctionPassAdaptor createCGSCCToFunctionPassAdaptor(FunctionPassT &&Pass, bool EagerlyInvalidate=false, bool NoRerun=false)
A function to deduce a function pass type and wrap it in the templated adaptor.
CodeGenFileType
These enums are meant to be passed into addPassesToEmitFile to indicate what type of file to emit,...
FunctionToMachineFunctionPassAdaptor createFunctionToMachineFunctionPassAdaptor(MachineFunctionPassT &&Pass)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
CodeGenOptLevel
Code generation optimization level.
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.
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
bool DisablePartialLibcallInlining
bool RequiresCodeGenSCCOrder
std::optional< bool > EnableGlobalISelOption
std::optional< bool > EnableIPRA
bool DisableConstantHoisting
std::optional< bool > OptimizeRegAlloc
RunOutliner EnableMachineOutliner
bool EnableGlobalMergeFunc
bool EnableLoopTermFold
Enable LoopTermFold immediately after LSR.
std::optional< bool > EnableFastISelOption
std::optional< GlobalISelAbortMode > EnableGlobalISelAbort
bool DisableExpandReductions
bool EnableImplicitNullChecks
bool EnableBlockPlacementStats
bool DisableSelectOptimize
Global function merging pass for new pass manager.
This class manages callbacks registration, as well as provides a way for PassInstrumentation to pass ...
LLVM_ABI StringRef getPassNameForClassName(StringRef ClassName)
Get the pass name for a given pass class name. Empty if no match found.
A utility pass template to force an analysis result to be available.