14#ifndef LLVM_PASSES_CODEGENPASSBUILDER_H
15#define LLVM_PASSES_CODEGENPASSBUILDER_H
103#define DUMMY_FUNCTION_PASS(NAME, PASS_NAME) \
104 struct PASS_NAME : public PassInfoMixin<PASS_NAME> { \
105 template <typename... Ts> PASS_NAME(Ts &&...) {} \
106 PreservedAnalyses run(Function &, FunctionAnalysisManager &) { \
107 return PreservedAnalyses::all(); \
110#define DUMMY_MACHINE_MODULE_PASS(NAME, PASS_NAME) \
111 struct PASS_NAME : public PassInfoMixin<PASS_NAME> { \
112 template <typename... Ts> PASS_NAME(Ts &&...) {} \
113 PreservedAnalyses run(Module &, ModuleAnalysisManager &) { \
114 return PreservedAnalyses::all(); \
117#define DUMMY_MACHINE_FUNCTION_PASS(NAME, PASS_NAME) \
118 struct PASS_NAME : public PassInfoMixin<PASS_NAME> { \
119 template <typename... Ts> PASS_NAME(Ts &&...) {} \
120 PreservedAnalyses run(MachineFunction &, \
121 MachineFunctionAnalysisManager &) { \
122 return PreservedAnalyses::all(); \
125#include "llvm/Passes/MachinePassRegistry.def"
163 template <
typename PassT>
166 template <
typename PassT>
168 std::declval<Module &>(), std::declval<ModuleAnalysisManager &>()));
170 template <
typename PassT>
172 std::declval<Function &>(), std::declval<FunctionAnalysisManager &>()));
174 template <
typename PassT>
176 std::declval<MachineFunction &>(),
177 std::declval<MachineFunctionAnalysisManager &>()));
191 template <
typename PassT>
195 "Only module pass and function pass are supported.");
196 bool Required =
false;
198 Required = PassT::isRequired();
199 if (!PB.runBeforeAdding(
Name) && !Required)
226 : MPM(MPM),
PB(
PB) {}
237 template <
typename PassT>
242 "Only module pass and function pass are supported.");
244 if (!Force && !PB.runBeforeAdding(
Name))
261 for (
auto &
C : PB.AfterCallbacks)
275 template <
typename TMC> TMC &
getTM()
const {
return static_cast<TMC &
>(
TM); }
295 return make_error<StringError>(
"addInstSelector is not overridden",
363 return make_error<StringError>(
"addIRTranslator is not overridden",
374 return make_error<StringError>(
"addLegalizeMachineIR is not overridden",
386 return make_error<StringError>(
"addRegBankSelect is not overridden",
399 return make_error<StringError>(
400 "addGlobalInstructionSelect is not overridden",
466 std::function<Expected<std::unique_ptr<MCStreamer>>(
MCContext &)>;
490 BeforeCallbacks.emplace_back(
496 template <
typename TargetPassT,
typename InsertedPassT>
498 AfterCallbacks.emplace_back(
500 if (
Name == TargetPassT::name())
506 DerivedT &derived() {
return static_cast<DerivedT &
>(*this); }
507 const DerivedT &derived()
const {
508 return static_cast<const DerivedT &
>(*this);
511 bool runBeforeAdding(StringRef
Name)
const {
512 bool ShouldAdd =
true;
513 for (
auto &
C : BeforeCallbacks)
514 ShouldAdd &=
C(
Name);
518 void setStartStopPasses(
const TargetPassConfig::StartStopInfo &
Info)
const;
520 Error verifyStartStop(
const TargetPassConfig::StartStopInfo &
Info)
const;
529 mutable bool Started =
true;
530 mutable bool Stopped =
true;
533template <
typename Derived,
typename TargetMachineT>
539 return StartStopInfo.takeError();
540 setStartStopPasses(*StartStopInfo);
550 addISelPasses(addIRPass);
558 if (
auto Err = addCoreISelPasses(addPass))
559 return std::move(Err);
561 if (
auto Err = derived().addMachinePasses(addPass))
562 return std::move(Err);
568 derived().addAsmPrinter(
569 addPass, [
this, &Out, DwoOut, FileType](
MCContext &Ctx) {
570 return this->TM.createMCStreamer(Out, DwoOut, FileType, Ctx);
577 return verifyStartStop(*StartStopInfo);
580template <
typename Derived,
typename TargetMachineT>
583 if (!
Info.StartPass.empty()) {
585 BeforeCallbacks.emplace_back([
this, &
Info, AfterFlag =
Info.StartAfter,
586 Count = 0u](
StringRef ClassName)
mutable {
587 if (Count == Info.StartInstanceNum) {
597 Started = !
Info.StartAfter;
603 if (!
Info.StopPass.empty()) {
605 BeforeCallbacks.emplace_back([
this, &
Info, AfterFlag =
Info.StopAfter,
606 Count = 0u](StringRef ClassName)
mutable {
607 if (Count == Info.StopInstanceNum) {
617 Stopped = !
Info.StopAfter;
623template <
typename Derived,
typename TargetMachineT>
624Error CodeGenPassBuilder<Derived, TargetMachineT>::verifyStartStop(
625 const TargetPassConfig::StartStopInfo &
Info)
const {
626 if (Started && Stopped)
627 return Error::success();
630 return make_error<StringError>(
631 "Can't find start pass \"" +
Info.StartPass +
"\".",
632 std::make_error_code(std::errc::invalid_argument));
634 return make_error<StringError>(
635 "Can't find stop pass \"" +
Info.StopPass +
"\".",
636 std::make_error_code(std::errc::invalid_argument));
637 return Error::success();
640template <
typename Derived,
typename TargetMachineT>
644 if (TM.useEmulatedTLS())
651 derived().addIRPasses(addPass);
652 derived().addCodeGenPrepare(addPass);
653 addPassesToHandleExceptions(addPass);
654 derived().addISelPrepare(addPass);
659template <
typename Derived,
typename TargetMachineT>
668 if (getOptLevel() != CodeGenOptLevel::None && !Opt.
DisableLSR) {
676 if (getOptLevel() != CodeGenOptLevel::None) {
701 if (getOptLevel() != CodeGenOptLevel::None)
704 if (getOptLevel() != CodeGenOptLevel::None &&
729template <
typename Derived,
typename TargetMachineT>
732 const MCAsmInfo *MCAI = TM.getMCAsmInfo();
733 assert(MCAI &&
"No MCAsmInfo");
735 case ExceptionHandling::SjLj:
744 case ExceptionHandling::DwarfCFI:
745 case ExceptionHandling::ARM:
746 case ExceptionHandling::AIX:
747 case ExceptionHandling::ZOS:
750 case ExceptionHandling::WinEH:
757 case ExceptionHandling::Wasm:
765 case ExceptionHandling::None:
776template <
typename Derived,
typename TargetMachineT>
779 if (getOptLevel() != CodeGenOptLevel::None && !Opt.
DisableCGP)
787template <
typename Derived,
typename TargetMachineT>
800 "\n\n*** Final LLVM Code input to ISel ***\n"));
808template <
typename Derived,
typename TargetMachineT>
816 SelectorType Selector;
819 Selector = SelectorType::FastISel;
822 (TM.Options.EnableGlobalISel &&
825 Selector = SelectorType::GlobalISel;
826 else if (TM.getOptLevel() == CodeGenOptLevel::None && TM.getO0WantsFastISel())
827 Selector = SelectorType::FastISel;
829 Selector = SelectorType::SelectionDAG;
832 if (Selector == SelectorType::FastISel) {
833 TM.setFastISel(
true);
834 TM.setGlobalISel(
false);
835 }
else if (Selector == SelectorType::GlobalISel) {
836 TM.setFastISel(
false);
837 TM.setGlobalISel(
true);
841 if (Selector == SelectorType::GlobalISel) {
842 if (
auto Err = derived().addIRTranslator(addPass))
843 return std::move(Err);
845 derived().addPreLegalizeMachineIR(addPass);
847 if (
auto Err = derived().addLegalizeMachineIR(addPass))
848 return std::move(Err);
852 derived().addPreRegBankSelect(addPass);
854 if (
auto Err = derived().addRegBankSelect(addPass))
855 return std::move(Err);
857 derived().addPreGlobalInstructionSelect(addPass);
859 if (
auto Err = derived().addGlobalInstructionSelect(addPass))
860 return std::move(Err);
863 addPass(ResetMachineFunctionPass(reportDiagnosticWhenGlobalISelFallback(),
864 isGlobalISelAbortEnabled()));
868 if (!isGlobalISelAbortEnabled())
869 if (
auto Err = derived().addInstSelector(addPass))
870 return std::move(Err);
872 }
else if (
auto Err = derived().addInstSelector(addPass))
873 return std::move(Err);
882 return Error::success();
901template <
typename Derived,
typename TargetMachineT>
905 if (getOptLevel() != CodeGenOptLevel::None) {
913 if (TM.Options.EnableIPRA) {
918 derived().addPreRegAlloc(addPass);
923 derived().addOptimizedRegAlloc(addPass);
925 if (
auto Err = derived().addFastRegAlloc(addPass))
930 derived().addPostRegAlloc(addPass);
932 addPass(RemoveRedundantDebugValuesPass());
935 if (getOptLevel() != CodeGenOptLevel::None) {
936 addPass(PostRAMachineSinkingPass());
937 addPass(ShrinkWrapPass());
940 addPass(PrologEpilogInserterPass());
943 if (getOptLevel() != CodeGenOptLevel::None)
944 derived().addMachineLateOptimization(addPass);
947 addPass(ExpandPostRAPseudosPass());
950 derived().addPreSched2(addPass);
953 addPass(ImplicitNullChecksPass());
958 if (getOptLevel() != CodeGenOptLevel::None &&
959 !TM.targetSchedulesPostRAScheduling()) {
961 addPass(PostMachineSchedulerPass());
963 addPass(PostRASchedulerPass());
967 derived().addGCPasses(addPass);
970 if (getOptLevel() != CodeGenOptLevel::None)
971 derived().addBlockPlacement(addPass);
974 addPass(FEntryInserterPass());
976 addPass(XRayInstrumentationPass());
977 addPass(PatchableFunctionPass());
979 derived().addPreEmitPass(addPass);
981 if (TM.Options.EnableIPRA)
986 addPass(FuncletLayoutPass());
988 addPass(StackMapLivenessPass());
989 addPass(LiveDebugValuesPass());
990 addPass(MachineSanitizerBinaryMetadata());
992 if (TM.Options.EnableMachineOutliner &&
993 getOptLevel() != CodeGenOptLevel::None &&
995 bool RunOnAllFunctions =
997 bool AddOutliner = RunOnAllFunctions || TM.Options.SupportsDefaultOutlining;
999 addPass(MachineOutlinerPass(RunOnAllFunctions));
1003 derived().addPreEmitPass2(addPass);
1005 return Error::success();
1009template <
typename Derived,
typename TargetMachineT>
1036 derived().addILPOpts(addPass);
1041 addPass(MachineSinkingPass());
1061template <
typename Derived,
typename TargetMachineT>
1065 addPass(RAGreedyPass());
1073template <
typename Derived,
typename TargetMachineT>
1079template <
typename Derived,
typename TargetMachineT>
1083 addRegAllocPass(addPass,
false);
1084 return Error::success();
1087template <
typename Derived,
typename TargetMachineT>
1091 addRegAllocPass(addPass,
true);
1094 derived().addPreRewrite(addPass);
1097 addPass(VirtRegRewriterPass());
1102 addPass(StackSlotColoringPass());
1104 return Error::success();
1109template <
typename Derived,
typename TargetMachineT>
1114 return derived().addRegAssignmentFast(addPass);
1120template <
typename Derived,
typename TargetMachineT>
1123 addPass(DetectDeadLanesPass());
1125 addPass(InitUndefPass());
1127 addPass(ProcessImplicitDefsPass());
1137 addPass(RegisterCoalescerPass());
1142 addPass(RenameIndependentSubregsPass());
1145 addPass(MachineSchedulerPass());
1147 if (derived().addRegAssignmentOptimized(addPass)) {
1150 derived().addPostRewrite(addPass);
1154 addPass(MachineCopyPropagationPass());
1168template <
typename Derived,
typename TargetMachineT>
1172 addPass(BranchFolderPass());
1178 if (!TM.requiresStructuredCFG())
1182 addPass(MachineLateInstrsCleanupPass());
1185 addPass(MachineCopyPropagationPass());
1189template <
typename Derived,
typename TargetMachineT>
1192 addPass(MachineBlockPlacementPass());
1195 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 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.
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.
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
bool EnableGlobalMergeFunc
std::optional< bool > EnableFastISelOption
std::optional< GlobalISelAbortMode > EnableGlobalISelAbort
bool EnableImplicitNullChecks
bool EnableBlockPlacementStats
bool DisableSelectOptimize
Global function merging pass for new pass manager.
A no-op pass template which simply forces a specific analysis result to be invalidated.
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.
A utility pass template to force an analysis result to be available.