1 //===- TargetPassConfig.cpp - Target independent code generation passes ---===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines interfaces to access the target independent code
10 // generation passes provided by the LLVM backend.
12 //===---------------------------------------------------------------------===//
14 #include "llvm/CodeGen/TargetPassConfig.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Analysis/BasicAliasAnalysis.h"
19 #include "llvm/Analysis/CallGraphSCCPass.h"
20 #include "llvm/Analysis/ScopedNoAliasAA.h"
21 #include "llvm/Analysis/TargetTransformInfo.h"
22 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
23 #include "llvm/CodeGen/BasicBlockSectionsProfileReader.h"
24 #include "llvm/CodeGen/CSEConfigBase.h"
25 #include "llvm/CodeGen/MachineFunctionPass.h"
26 #include "llvm/CodeGen/MachinePassRegistry.h"
27 #include "llvm/CodeGen/Passes.h"
28 #include "llvm/CodeGen/RegAllocRegistry.h"
29 #include "llvm/IR/IRPrintingPasses.h"
30 #include "llvm/IR/LegacyPassManager.h"
31 #include "llvm/IR/PassInstrumentation.h"
32 #include "llvm/IR/Verifier.h"
33 #include "llvm/InitializePasses.h"
34 #include "llvm/MC/MCAsmInfo.h"
35 #include "llvm/MC/MCTargetOptions.h"
36 #include "llvm/Pass.h"
37 #include "llvm/Support/CodeGen.h"
38 #include "llvm/Support/CommandLine.h"
39 #include "llvm/Support/Compiler.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/Discriminator.h"
42 #include "llvm/Support/ErrorHandling.h"
43 #include "llvm/Support/SaveAndRestore.h"
44 #include "llvm/Support/Threading.h"
45 #include "llvm/Support/VirtualFileSystem.h"
46 #include "llvm/Support/WithColor.h"
47 #include "llvm/Target/CGPassBuilderOption.h"
48 #include "llvm/Target/TargetMachine.h"
49 #include "llvm/Transforms/Scalar.h"
50 #include "llvm/Transforms/Utils.h"
58 EnableIPRA("enable-ipra", cl::init(false), cl::Hidden
,
59 cl::desc("Enable interprocedural register allocation "
60 "to reduce load/store at procedure calls."));
61 static cl::opt
<bool> DisablePostRASched("disable-post-ra", cl::Hidden
,
62 cl::desc("Disable Post Regalloc Scheduler"));
63 static cl::opt
<bool> DisableBranchFold("disable-branch-fold", cl::Hidden
,
64 cl::desc("Disable branch folding"));
65 static cl::opt
<bool> DisableTailDuplicate("disable-tail-duplicate", cl::Hidden
,
66 cl::desc("Disable tail duplication"));
67 static cl::opt
<bool> DisableEarlyTailDup("disable-early-taildup", cl::Hidden
,
68 cl::desc("Disable pre-register allocation tail duplication"));
69 static cl::opt
<bool> DisableBlockPlacement("disable-block-placement",
70 cl::Hidden
, cl::desc("Disable probability-driven block placement"));
71 static cl::opt
<bool> EnableBlockPlacementStats("enable-block-placement-stats",
72 cl::Hidden
, cl::desc("Collect probability-driven block placement stats"));
73 static cl::opt
<bool> DisableSSC("disable-ssc", cl::Hidden
,
74 cl::desc("Disable Stack Slot Coloring"));
75 static cl::opt
<bool> DisableMachineDCE("disable-machine-dce", cl::Hidden
,
76 cl::desc("Disable Machine Dead Code Elimination"));
77 static cl::opt
<bool> DisableEarlyIfConversion("disable-early-ifcvt", cl::Hidden
,
78 cl::desc("Disable Early If-conversion"));
79 static cl::opt
<bool> DisableMachineLICM("disable-machine-licm", cl::Hidden
,
80 cl::desc("Disable Machine LICM"));
81 static cl::opt
<bool> DisableMachineCSE("disable-machine-cse", cl::Hidden
,
82 cl::desc("Disable Machine Common Subexpression Elimination"));
83 static cl::opt
<cl::boolOrDefault
> OptimizeRegAlloc(
84 "optimize-regalloc", cl::Hidden
,
85 cl::desc("Enable optimized register allocation compilation path."));
86 static cl::opt
<bool> DisablePostRAMachineLICM("disable-postra-machine-licm",
88 cl::desc("Disable Machine LICM"));
89 static cl::opt
<bool> DisableMachineSink("disable-machine-sink", cl::Hidden
,
90 cl::desc("Disable Machine Sinking"));
91 static cl::opt
<bool> DisablePostRAMachineSink("disable-postra-machine-sink",
93 cl::desc("Disable PostRA Machine Sinking"));
94 static cl::opt
<bool> DisableLSR("disable-lsr", cl::Hidden
,
95 cl::desc("Disable Loop Strength Reduction Pass"));
96 static cl::opt
<bool> DisableConstantHoisting("disable-constant-hoisting",
97 cl::Hidden
, cl::desc("Disable ConstantHoisting"));
98 static cl::opt
<bool> DisableCGP("disable-cgp", cl::Hidden
,
99 cl::desc("Disable Codegen Prepare"));
100 static cl::opt
<bool> DisableCopyProp("disable-copyprop", cl::Hidden
,
101 cl::desc("Disable Copy Propagation pass"));
102 static cl::opt
<bool> DisablePartialLibcallInlining("disable-partial-libcall-inlining",
103 cl::Hidden
, cl::desc("Disable Partial Libcall Inlining"));
104 static cl::opt
<bool> DisableAtExitBasedGlobalDtorLowering(
105 "disable-atexit-based-global-dtor-lowering", cl::Hidden
,
106 cl::desc("For MachO, disable atexit()-based global destructor lowering"));
107 static cl::opt
<bool> EnableImplicitNullChecks(
108 "enable-implicit-null-checks",
109 cl::desc("Fold null checks into faulting memory operations"),
110 cl::init(false), cl::Hidden
);
111 static cl::opt
<bool> DisableMergeICmps("disable-mergeicmps",
112 cl::desc("Disable MergeICmps Pass"),
113 cl::init(false), cl::Hidden
);
114 static cl::opt
<bool> PrintLSR("print-lsr-output", cl::Hidden
,
115 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
117 PrintISelInput("print-isel-input", cl::Hidden
,
118 cl::desc("Print LLVM IR input to isel pass"));
119 static cl::opt
<cl::boolOrDefault
>
120 VerifyMachineCode("verify-machineinstrs", cl::Hidden
,
121 cl::desc("Verify generated machine code"));
122 static cl::opt
<cl::boolOrDefault
>
123 DebugifyAndStripAll("debugify-and-strip-all-safe", cl::Hidden
,
124 cl::desc("Debugify MIR before and Strip debug after "
125 "each pass except those known to be unsafe "
126 "when debug info is present"));
127 static cl::opt
<cl::boolOrDefault
> DebugifyCheckAndStripAll(
128 "debugify-check-and-strip-all-safe", cl::Hidden
,
130 "Debugify MIR before, by checking and stripping the debug info after, "
131 "each pass except those known to be unsafe when debug info is "
133 // Enable or disable the MachineOutliner.
134 static cl::opt
<RunOutliner
> EnableMachineOutliner(
135 "enable-machine-outliner", cl::desc("Enable the machine outliner"),
136 cl::Hidden
, cl::ValueOptional
, cl::init(RunOutliner::TargetDefault
),
137 cl::values(clEnumValN(RunOutliner::AlwaysOutline
, "always",
138 "Run on all functions guaranteed to be beneficial"),
139 clEnumValN(RunOutliner::NeverOutline
, "never",
140 "Disable all outlining"),
141 // Sentinel value for unspecified option.
142 clEnumValN(RunOutliner::AlwaysOutline
, "", "")));
143 // Disable the pass to fix unwind information. Whether the pass is included in
144 // the pipeline is controlled via the target options, this option serves as
146 static cl::opt
<bool> DisableCFIFixup("disable-cfi-fixup", cl::Hidden
,
147 cl::desc("Disable the CFI fixup pass"));
148 // Enable or disable FastISel. Both options are needed, because
149 // FastISel is enabled by default with -fast, and we wish to be
150 // able to enable or disable fast-isel independently from -O0.
151 static cl::opt
<cl::boolOrDefault
>
152 EnableFastISelOption("fast-isel", cl::Hidden
,
153 cl::desc("Enable the \"fast\" instruction selector"));
155 static cl::opt
<cl::boolOrDefault
> EnableGlobalISelOption(
156 "global-isel", cl::Hidden
,
157 cl::desc("Enable the \"global\" instruction selector"));
159 // FIXME: remove this after switching to NPM or GlobalISel, whichever gets there
162 PrintAfterISel("print-after-isel", cl::init(false), cl::Hidden
,
163 cl::desc("Print machine instrs after ISel"));
165 static cl::opt
<GlobalISelAbortMode
> EnableGlobalISelAbort(
166 "global-isel-abort", cl::Hidden
,
167 cl::desc("Enable abort calls when \"global\" instruction selection "
168 "fails to lower/select an instruction"),
170 clEnumValN(GlobalISelAbortMode::Disable
, "0", "Disable the abort"),
171 clEnumValN(GlobalISelAbortMode::Enable
, "1", "Enable the abort"),
172 clEnumValN(GlobalISelAbortMode::DisableWithDiag
, "2",
173 "Disable the abort but emit a diagnostic on failure")));
175 // Disable MIRProfileLoader before RegAlloc. This is for for debugging and
177 static cl::opt
<bool> DisableRAFSProfileLoader(
178 "disable-ra-fsprofile-loader", cl::init(false), cl::Hidden
,
179 cl::desc("Disable MIRProfileLoader before RegAlloc"));
180 // Disable MIRProfileLoader before BloackPlacement. This is for for debugging
181 // and tuning purpose.
182 static cl::opt
<bool> DisableLayoutFSProfileLoader(
183 "disable-layout-fsprofile-loader", cl::init(false), cl::Hidden
,
184 cl::desc("Disable MIRProfileLoader before BlockPlacement"));
185 // Specify FSProfile file name.
186 static cl::opt
<std::string
>
187 FSProfileFile("fs-profile-file", cl::init(""), cl::value_desc("filename"),
188 cl::desc("Flow Sensitive profile file name."), cl::Hidden
);
189 // Specify Remapping file for FSProfile.
190 static cl::opt
<std::string
> FSRemappingFile(
191 "fs-remapping-file", cl::init(""), cl::value_desc("filename"),
192 cl::desc("Flow Sensitive profile remapping file name."), cl::Hidden
);
194 // Temporary option to allow experimenting with MachineScheduler as a post-RA
195 // scheduler. Targets can "properly" enable this with
196 // substitutePass(&PostRASchedulerID, &PostMachineSchedulerID).
197 // Targets can return true in targetSchedulesPostRAScheduling() and
198 // insert a PostRA scheduling pass wherever it wants.
199 static cl::opt
<bool> MISchedPostRA(
200 "misched-postra", cl::Hidden
,
202 "Run MachineScheduler post regalloc (independent of preRA sched)"));
204 // Experimental option to run live interval analysis early.
205 static cl::opt
<bool> EarlyLiveIntervals("early-live-intervals", cl::Hidden
,
206 cl::desc("Run live interval analysis earlier in the pipeline"));
208 static cl::opt
<bool> DisableReplaceWithVecLib(
209 "disable-replace-with-vec-lib", cl::Hidden
,
210 cl::desc("Disable replace with vector math call pass"));
212 /// Option names for limiting the codegen pipeline.
213 /// Those are used in error reporting and we didn't want
214 /// to duplicate their names all over the place.
215 static const char StartAfterOptName
[] = "start-after";
216 static const char StartBeforeOptName
[] = "start-before";
217 static const char StopAfterOptName
[] = "stop-after";
218 static const char StopBeforeOptName
[] = "stop-before";
220 static cl::opt
<std::string
>
221 StartAfterOpt(StringRef(StartAfterOptName
),
222 cl::desc("Resume compilation after a specific pass"),
223 cl::value_desc("pass-name"), cl::init(""), cl::Hidden
);
225 static cl::opt
<std::string
>
226 StartBeforeOpt(StringRef(StartBeforeOptName
),
227 cl::desc("Resume compilation before a specific pass"),
228 cl::value_desc("pass-name"), cl::init(""), cl::Hidden
);
230 static cl::opt
<std::string
>
231 StopAfterOpt(StringRef(StopAfterOptName
),
232 cl::desc("Stop compilation after a specific pass"),
233 cl::value_desc("pass-name"), cl::init(""), cl::Hidden
);
235 static cl::opt
<std::string
>
236 StopBeforeOpt(StringRef(StopBeforeOptName
),
237 cl::desc("Stop compilation before a specific pass"),
238 cl::value_desc("pass-name"), cl::init(""), cl::Hidden
);
240 /// Enable the machine function splitter pass.
241 static cl::opt
<bool> EnableMachineFunctionSplitter(
242 "enable-split-machine-functions", cl::Hidden
,
243 cl::desc("Split out cold blocks from machine functions based on profile "
246 /// Disable the expand reductions pass for testing.
247 static cl::opt
<bool> DisableExpandReductions(
248 "disable-expand-reductions", cl::init(false), cl::Hidden
,
249 cl::desc("Disable the expand reduction intrinsics pass from running"));
251 /// Disable the select optimization pass.
252 static cl::opt
<bool> DisableSelectOptimize(
253 "disable-select-optimize", cl::init(true), cl::Hidden
,
254 cl::desc("Disable the select-optimization pass from running"));
256 /// Enable garbage-collecting empty basic blocks.
258 GCEmptyBlocks("gc-empty-basic-blocks", cl::init(false), cl::Hidden
,
259 cl::desc("Enable garbage-collecting empty basic blocks"));
261 /// Allow standard passes to be disabled by command line options. This supports
262 /// simple binary flags that either suppress the pass or do nothing.
263 /// i.e. -disable-mypass=false has no effect.
264 /// These should be converted to boolOrDefault in order to use applyOverride.
265 static IdentifyingPassPtr
applyDisable(IdentifyingPassPtr PassID
,
268 return IdentifyingPassPtr();
272 /// Allow standard passes to be disabled by the command line, regardless of who
273 /// is adding the pass.
275 /// StandardID is the pass identified in the standard pass pipeline and provided
276 /// to addPass(). It may be a target-specific ID in the case that the target
277 /// directly adds its own pass, but in that case we harmlessly fall through.
279 /// TargetID is the pass that the target has configured to override StandardID.
281 /// StandardID may be a pseudo ID. In that case TargetID is the name of the real
282 /// pass to run. This allows multiple options to control a single pass depending
283 /// on where in the pipeline that pass is added.
284 static IdentifyingPassPtr
overridePass(AnalysisID StandardID
,
285 IdentifyingPassPtr TargetID
) {
286 if (StandardID
== &PostRASchedulerID
)
287 return applyDisable(TargetID
, DisablePostRASched
);
289 if (StandardID
== &BranchFolderPassID
)
290 return applyDisable(TargetID
, DisableBranchFold
);
292 if (StandardID
== &TailDuplicateID
)
293 return applyDisable(TargetID
, DisableTailDuplicate
);
295 if (StandardID
== &EarlyTailDuplicateID
)
296 return applyDisable(TargetID
, DisableEarlyTailDup
);
298 if (StandardID
== &MachineBlockPlacementID
)
299 return applyDisable(TargetID
, DisableBlockPlacement
);
301 if (StandardID
== &StackSlotColoringID
)
302 return applyDisable(TargetID
, DisableSSC
);
304 if (StandardID
== &DeadMachineInstructionElimID
)
305 return applyDisable(TargetID
, DisableMachineDCE
);
307 if (StandardID
== &EarlyIfConverterID
)
308 return applyDisable(TargetID
, DisableEarlyIfConversion
);
310 if (StandardID
== &EarlyMachineLICMID
)
311 return applyDisable(TargetID
, DisableMachineLICM
);
313 if (StandardID
== &MachineCSEID
)
314 return applyDisable(TargetID
, DisableMachineCSE
);
316 if (StandardID
== &MachineLICMID
)
317 return applyDisable(TargetID
, DisablePostRAMachineLICM
);
319 if (StandardID
== &MachineSinkingID
)
320 return applyDisable(TargetID
, DisableMachineSink
);
322 if (StandardID
== &PostRAMachineSinkingID
)
323 return applyDisable(TargetID
, DisablePostRAMachineSink
);
325 if (StandardID
== &MachineCopyPropagationID
)
326 return applyDisable(TargetID
, DisableCopyProp
);
331 // Find the FSProfile file name. The internal option takes the precedence
332 // before getting from TargetMachine.
333 static std::string
getFSProfileFile(const TargetMachine
*TM
) {
334 if (!FSProfileFile
.empty())
335 return FSProfileFile
.getValue();
336 const std::optional
<PGOOptions
> &PGOOpt
= TM
->getPGOOption();
337 if (PGOOpt
== std::nullopt
|| PGOOpt
->Action
!= PGOOptions::SampleUse
)
338 return std::string();
339 return PGOOpt
->ProfileFile
;
342 // Find the Profile remapping file name. The internal option takes the
343 // precedence before getting from TargetMachine.
344 static std::string
getFSRemappingFile(const TargetMachine
*TM
) {
345 if (!FSRemappingFile
.empty())
346 return FSRemappingFile
.getValue();
347 const std::optional
<PGOOptions
> &PGOOpt
= TM
->getPGOOption();
348 if (PGOOpt
== std::nullopt
|| PGOOpt
->Action
!= PGOOptions::SampleUse
)
349 return std::string();
350 return PGOOpt
->ProfileRemappingFile
;
353 //===---------------------------------------------------------------------===//
355 //===---------------------------------------------------------------------===//
357 INITIALIZE_PASS(TargetPassConfig
, "targetpassconfig",
358 "Target Pass Configuration", false, false)
359 char TargetPassConfig::ID
= 0;
363 struct InsertedPass
{
364 AnalysisID TargetPassID
;
365 IdentifyingPassPtr InsertedPassID
;
367 InsertedPass(AnalysisID TargetPassID
, IdentifyingPassPtr InsertedPassID
)
368 : TargetPassID(TargetPassID
), InsertedPassID(InsertedPassID
) {}
370 Pass
*getInsertedPass() const {
371 assert(InsertedPassID
.isValid() && "Illegal Pass ID!");
372 if (InsertedPassID
.isInstance())
373 return InsertedPassID
.getInstance();
374 Pass
*NP
= Pass::createPass(InsertedPassID
.getID());
375 assert(NP
&& "Pass ID not registered");
380 } // end anonymous namespace
384 extern cl::opt
<bool> EnableFSDiscriminator
;
386 class PassConfigImpl
{
388 // List of passes explicitly substituted by this target. Normally this is
389 // empty, but it is a convenient way to suppress or replace specific passes
390 // that are part of a standard pass pipeline without overridding the entire
391 // pipeline. This mechanism allows target options to inherit a standard pass's
392 // user interface. For example, a target may disable a standard pass by
393 // default by substituting a pass ID of zero, and the user may still enable
394 // that standard pass with an explicit command line option.
395 DenseMap
<AnalysisID
,IdentifyingPassPtr
> TargetPasses
;
397 /// Store the pairs of <AnalysisID, AnalysisID> of which the second pass
398 /// is inserted after each instance of the first one.
399 SmallVector
<InsertedPass
, 4> InsertedPasses
;
402 } // end namespace llvm
404 // Out of line virtual method.
405 TargetPassConfig::~TargetPassConfig() {
409 static const PassInfo
*getPassInfo(StringRef PassName
) {
410 if (PassName
.empty())
413 const PassRegistry
&PR
= *PassRegistry::getPassRegistry();
414 const PassInfo
*PI
= PR
.getPassInfo(PassName
);
416 report_fatal_error(Twine('\"') + Twine(PassName
) +
417 Twine("\" pass is not registered."));
421 static AnalysisID
getPassIDFromName(StringRef PassName
) {
422 const PassInfo
*PI
= getPassInfo(PassName
);
423 return PI
? PI
->getTypeInfo() : nullptr;
426 static std::pair
<StringRef
, unsigned>
427 getPassNameAndInstanceNum(StringRef PassName
) {
428 StringRef Name
, InstanceNumStr
;
429 std::tie(Name
, InstanceNumStr
) = PassName
.split(',');
431 unsigned InstanceNum
= 0;
432 if (!InstanceNumStr
.empty() && InstanceNumStr
.getAsInteger(10, InstanceNum
))
433 report_fatal_error("invalid pass instance specifier " + PassName
);
435 return std::make_pair(Name
, InstanceNum
);
438 void TargetPassConfig::setStartStopPasses() {
439 StringRef StartBeforeName
;
440 std::tie(StartBeforeName
, StartBeforeInstanceNum
) =
441 getPassNameAndInstanceNum(StartBeforeOpt
);
443 StringRef StartAfterName
;
444 std::tie(StartAfterName
, StartAfterInstanceNum
) =
445 getPassNameAndInstanceNum(StartAfterOpt
);
447 StringRef StopBeforeName
;
448 std::tie(StopBeforeName
, StopBeforeInstanceNum
)
449 = getPassNameAndInstanceNum(StopBeforeOpt
);
451 StringRef StopAfterName
;
452 std::tie(StopAfterName
, StopAfterInstanceNum
)
453 = getPassNameAndInstanceNum(StopAfterOpt
);
455 StartBefore
= getPassIDFromName(StartBeforeName
);
456 StartAfter
= getPassIDFromName(StartAfterName
);
457 StopBefore
= getPassIDFromName(StopBeforeName
);
458 StopAfter
= getPassIDFromName(StopAfterName
);
459 if (StartBefore
&& StartAfter
)
460 report_fatal_error(Twine(StartBeforeOptName
) + Twine(" and ") +
461 Twine(StartAfterOptName
) + Twine(" specified!"));
462 if (StopBefore
&& StopAfter
)
463 report_fatal_error(Twine(StopBeforeOptName
) + Twine(" and ") +
464 Twine(StopAfterOptName
) + Twine(" specified!"));
465 Started
= (StartAfter
== nullptr) && (StartBefore
== nullptr);
468 CGPassBuilderOption
llvm::getCGPassBuilderOption() {
469 CGPassBuilderOption Opt
;
471 #define SET_OPTION(Option) \
472 if (Option.getNumOccurrences()) \
475 SET_OPTION(EnableFastISelOption
)
476 SET_OPTION(EnableGlobalISelAbort
)
477 SET_OPTION(EnableGlobalISelOption
)
478 SET_OPTION(EnableIPRA
)
479 SET_OPTION(OptimizeRegAlloc
)
480 SET_OPTION(VerifyMachineCode
)
481 SET_OPTION(DisableAtExitBasedGlobalDtorLowering
)
482 SET_OPTION(DisableExpandReductions
)
483 SET_OPTION(PrintAfterISel
)
484 SET_OPTION(FSProfileFile
)
485 SET_OPTION(GCEmptyBlocks
)
487 #define SET_BOOLEAN_OPTION(Option) Opt.Option = Option;
489 SET_BOOLEAN_OPTION(EarlyLiveIntervals
)
490 SET_BOOLEAN_OPTION(EnableBlockPlacementStats
)
491 SET_BOOLEAN_OPTION(EnableImplicitNullChecks
)
492 SET_BOOLEAN_OPTION(EnableMachineOutliner
)
493 SET_BOOLEAN_OPTION(MISchedPostRA
)
494 SET_BOOLEAN_OPTION(DisableMergeICmps
)
495 SET_BOOLEAN_OPTION(DisableLSR
)
496 SET_BOOLEAN_OPTION(DisableConstantHoisting
)
497 SET_BOOLEAN_OPTION(DisableCGP
)
498 SET_BOOLEAN_OPTION(DisablePartialLibcallInlining
)
499 SET_BOOLEAN_OPTION(DisableSelectOptimize
)
500 SET_BOOLEAN_OPTION(PrintLSR
)
501 SET_BOOLEAN_OPTION(PrintISelInput
)
502 SET_BOOLEAN_OPTION(DebugifyAndStripAll
)
503 SET_BOOLEAN_OPTION(DebugifyCheckAndStripAll
)
504 SET_BOOLEAN_OPTION(DisableRAFSProfileLoader
)
505 SET_BOOLEAN_OPTION(DisableCFIFixup
)
506 SET_BOOLEAN_OPTION(EnableMachineFunctionSplitter
)
511 void llvm::registerCodeGenCallback(PassInstrumentationCallbacks
&PIC
,
512 LLVMTargetMachine
&LLVMTM
) {
514 // Register a callback for disabling passes.
515 PIC
.registerShouldRunOptionalPassCallback([](StringRef P
, Any
) {
517 #define DISABLE_PASS(Option, Name) \
518 if (Option && P.contains(#Name)) \
520 DISABLE_PASS(DisableBlockPlacement
, MachineBlockPlacementPass
)
521 DISABLE_PASS(DisableBranchFold
, BranchFolderPass
)
522 DISABLE_PASS(DisableCopyProp
, MachineCopyPropagationPass
)
523 DISABLE_PASS(DisableEarlyIfConversion
, EarlyIfConverterPass
)
524 DISABLE_PASS(DisableEarlyTailDup
, EarlyTailDuplicatePass
)
525 DISABLE_PASS(DisableMachineCSE
, MachineCSEPass
)
526 DISABLE_PASS(DisableMachineDCE
, DeadMachineInstructionElimPass
)
527 DISABLE_PASS(DisableMachineLICM
, EarlyMachineLICMPass
)
528 DISABLE_PASS(DisableMachineSink
, MachineSinkingPass
)
529 DISABLE_PASS(DisablePostRAMachineLICM
, MachineLICMPass
)
530 DISABLE_PASS(DisablePostRAMachineSink
, PostRAMachineSinkingPass
)
531 DISABLE_PASS(DisablePostRASched
, PostRASchedulerPass
)
532 DISABLE_PASS(DisableSSC
, StackSlotColoringPass
)
533 DISABLE_PASS(DisableTailDuplicate
, TailDuplicatePass
)
539 Expected
<TargetPassConfig::StartStopInfo
>
540 TargetPassConfig::getStartStopInfo(PassInstrumentationCallbacks
&PIC
) {
541 auto [StartBefore
, StartBeforeInstanceNum
] =
542 getPassNameAndInstanceNum(StartBeforeOpt
);
543 auto [StartAfter
, StartAfterInstanceNum
] =
544 getPassNameAndInstanceNum(StartAfterOpt
);
545 auto [StopBefore
, StopBeforeInstanceNum
] =
546 getPassNameAndInstanceNum(StopBeforeOpt
);
547 auto [StopAfter
, StopAfterInstanceNum
] =
548 getPassNameAndInstanceNum(StopAfterOpt
);
550 if (!StartBefore
.empty() && !StartAfter
.empty())
551 return make_error
<StringError
>(
552 Twine(StartBeforeOptName
) + " and " + StartAfterOptName
+ " specified!",
553 std::make_error_code(std::errc::invalid_argument
));
554 if (!StopBefore
.empty() && !StopAfter
.empty())
555 return make_error
<StringError
>(
556 Twine(StopBeforeOptName
) + " and " + StopAfterOptName
+ " specified!",
557 std::make_error_code(std::errc::invalid_argument
));
559 StartStopInfo Result
;
560 Result
.StartPass
= StartBefore
.empty() ? StartAfter
: StartBefore
;
561 Result
.StopPass
= StopBefore
.empty() ? StopAfter
: StopBefore
;
562 Result
.StartInstanceNum
=
563 StartBefore
.empty() ? StartAfterInstanceNum
: StartBeforeInstanceNum
;
564 Result
.StopInstanceNum
=
565 StopBefore
.empty() ? StopAfterInstanceNum
: StopBeforeInstanceNum
;
566 Result
.StartAfter
= !StartAfter
.empty();
567 Result
.StopAfter
= !StopAfter
.empty();
568 Result
.StartInstanceNum
+= Result
.StartInstanceNum
== 0;
569 Result
.StopInstanceNum
+= Result
.StopInstanceNum
== 0;
573 // Out of line constructor provides default values for pass options and
574 // registers all common codegen passes.
575 TargetPassConfig::TargetPassConfig(LLVMTargetMachine
&TM
, PassManagerBase
&pm
)
576 : ImmutablePass(ID
), PM(&pm
), TM(&TM
) {
577 Impl
= new PassConfigImpl();
579 // Register all target independent codegen passes to activate their PassIDs,
580 // including this pass itself.
581 initializeCodeGen(*PassRegistry::getPassRegistry());
583 // Also register alias analysis passes required by codegen passes.
584 initializeBasicAAWrapperPassPass(*PassRegistry::getPassRegistry());
585 initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry());
587 if (EnableIPRA
.getNumOccurrences())
588 TM
.Options
.EnableIPRA
= EnableIPRA
;
590 // If not explicitly specified, use target default.
591 TM
.Options
.EnableIPRA
|= TM
.useIPRA();
594 if (TM
.Options
.EnableIPRA
)
595 setRequiresCodeGenSCCOrder();
597 if (EnableGlobalISelAbort
.getNumOccurrences())
598 TM
.Options
.GlobalISelAbort
= EnableGlobalISelAbort
;
600 setStartStopPasses();
603 CodeGenOptLevel
TargetPassConfig::getOptLevel() const {
604 return TM
->getOptLevel();
607 /// Insert InsertedPassID pass after TargetPassID.
608 void TargetPassConfig::insertPass(AnalysisID TargetPassID
,
609 IdentifyingPassPtr InsertedPassID
) {
610 assert(((!InsertedPassID
.isInstance() &&
611 TargetPassID
!= InsertedPassID
.getID()) ||
612 (InsertedPassID
.isInstance() &&
613 TargetPassID
!= InsertedPassID
.getInstance()->getPassID())) &&
614 "Insert a pass after itself!");
615 Impl
->InsertedPasses
.emplace_back(TargetPassID
, InsertedPassID
);
618 /// createPassConfig - Create a pass configuration object to be used by
619 /// addPassToEmitX methods for generating a pipeline of CodeGen passes.
621 /// Targets may override this to extend TargetPassConfig.
622 TargetPassConfig
*LLVMTargetMachine::createPassConfig(PassManagerBase
&PM
) {
623 return new TargetPassConfig(*this, PM
);
626 TargetPassConfig::TargetPassConfig()
627 : ImmutablePass(ID
) {
628 report_fatal_error("Trying to construct TargetPassConfig without a target "
629 "machine. Scheduling a CodeGen pass without a target "
633 bool TargetPassConfig::willCompleteCodeGenPipeline() {
634 return StopBeforeOpt
.empty() && StopAfterOpt
.empty();
637 bool TargetPassConfig::hasLimitedCodeGenPipeline() {
638 return !StartBeforeOpt
.empty() || !StartAfterOpt
.empty() ||
639 !willCompleteCodeGenPipeline();
642 std::string
TargetPassConfig::getLimitedCodeGenPipelineReason() {
643 if (!hasLimitedCodeGenPipeline())
644 return std::string();
646 static cl::opt
<std::string
> *PassNames
[] = {&StartAfterOpt
, &StartBeforeOpt
,
647 &StopAfterOpt
, &StopBeforeOpt
};
648 static const char *OptNames
[] = {StartAfterOptName
, StartBeforeOptName
,
649 StopAfterOptName
, StopBeforeOptName
};
651 for (int Idx
= 0; Idx
< 4; ++Idx
)
652 if (!PassNames
[Idx
]->empty()) {
656 Res
+= OptNames
[Idx
];
661 // Helper to verify the analysis is really immutable.
662 void TargetPassConfig::setOpt(bool &Opt
, bool Val
) {
663 assert(!Initialized
&& "PassConfig is immutable");
667 void TargetPassConfig::substitutePass(AnalysisID StandardID
,
668 IdentifyingPassPtr TargetID
) {
669 Impl
->TargetPasses
[StandardID
] = TargetID
;
672 IdentifyingPassPtr
TargetPassConfig::getPassSubstitution(AnalysisID ID
) const {
673 DenseMap
<AnalysisID
, IdentifyingPassPtr
>::const_iterator
674 I
= Impl
->TargetPasses
.find(ID
);
675 if (I
== Impl
->TargetPasses
.end())
680 bool TargetPassConfig::isPassSubstitutedOrOverridden(AnalysisID ID
) const {
681 IdentifyingPassPtr TargetID
= getPassSubstitution(ID
);
682 IdentifyingPassPtr FinalPtr
= overridePass(ID
, TargetID
);
683 return !FinalPtr
.isValid() || FinalPtr
.isInstance() ||
684 FinalPtr
.getID() != ID
;
687 /// Add a pass to the PassManager if that pass is supposed to be run. If the
688 /// Started/Stopped flags indicate either that the compilation should start at
689 /// a later pass or that it should stop after an earlier pass, then do not add
690 /// the pass. Finally, compare the current pass against the StartAfter
691 /// and StopAfter options and change the Started/Stopped flags accordingly.
692 void TargetPassConfig::addPass(Pass
*P
) {
693 assert(!Initialized
&& "PassConfig is immutable");
695 // Cache the Pass ID here in case the pass manager finds this pass is
696 // redundant with ones already scheduled / available, and deletes it.
697 // Fundamentally, once we add the pass to the manager, we no longer own it
698 // and shouldn't reference it.
699 AnalysisID PassID
= P
->getPassID();
701 if (StartBefore
== PassID
&& StartBeforeCount
++ == StartBeforeInstanceNum
)
703 if (StopBefore
== PassID
&& StopBeforeCount
++ == StopBeforeInstanceNum
)
705 if (Started
&& !Stopped
) {
706 if (AddingMachinePasses
) {
707 // Construct banner message before PM->add() as that may delete the pass.
709 std::string("After ") + std::string(P
->getPassName());
710 addMachinePrePasses();
712 addMachinePostPasses(Banner
);
717 // Add the passes after the pass P if there is any.
718 for (const auto &IP
: Impl
->InsertedPasses
)
719 if (IP
.TargetPassID
== PassID
)
720 addPass(IP
.getInsertedPass());
725 if (StopAfter
== PassID
&& StopAfterCount
++ == StopAfterInstanceNum
)
728 if (StartAfter
== PassID
&& StartAfterCount
++ == StartAfterInstanceNum
)
730 if (Stopped
&& !Started
)
731 report_fatal_error("Cannot stop compilation after pass that is not run");
734 /// Add a CodeGen pass at this point in the pipeline after checking for target
735 /// and command line overrides.
737 /// addPass cannot return a pointer to the pass instance because is internal the
738 /// PassManager and the instance we create here may already be freed.
739 AnalysisID
TargetPassConfig::addPass(AnalysisID PassID
) {
740 IdentifyingPassPtr TargetID
= getPassSubstitution(PassID
);
741 IdentifyingPassPtr FinalPtr
= overridePass(PassID
, TargetID
);
742 if (!FinalPtr
.isValid())
746 if (FinalPtr
.isInstance())
747 P
= FinalPtr
.getInstance();
749 P
= Pass::createPass(FinalPtr
.getID());
751 llvm_unreachable("Pass ID not registered");
753 AnalysisID FinalID
= P
->getPassID();
754 addPass(P
); // Ends the lifetime of P.
759 void TargetPassConfig::printAndVerify(const std::string
&Banner
) {
760 addPrintPass(Banner
);
761 addVerifyPass(Banner
);
764 void TargetPassConfig::addPrintPass(const std::string
&Banner
) {
766 PM
->add(createMachineFunctionPrinterPass(dbgs(), Banner
));
769 void TargetPassConfig::addVerifyPass(const std::string
&Banner
) {
770 bool Verify
= VerifyMachineCode
== cl::BOU_TRUE
;
771 #ifdef EXPENSIVE_CHECKS
772 if (VerifyMachineCode
== cl::BOU_UNSET
)
773 Verify
= TM
->isMachineVerifierClean();
776 PM
->add(createMachineVerifierPass(Banner
));
779 void TargetPassConfig::addDebugifyPass() {
780 PM
->add(createDebugifyMachineModulePass());
783 void TargetPassConfig::addStripDebugPass() {
784 PM
->add(createStripDebugMachineModulePass(/*OnlyDebugified=*/true));
787 void TargetPassConfig::addCheckDebugPass() {
788 PM
->add(createCheckDebugMachineModulePass());
791 void TargetPassConfig::addMachinePrePasses(bool AllowDebugify
) {
792 if (AllowDebugify
&& DebugifyIsSafe
&&
793 (DebugifyAndStripAll
== cl::BOU_TRUE
||
794 DebugifyCheckAndStripAll
== cl::BOU_TRUE
))
798 void TargetPassConfig::addMachinePostPasses(const std::string
&Banner
) {
799 if (DebugifyIsSafe
) {
800 if (DebugifyCheckAndStripAll
== cl::BOU_TRUE
) {
803 } else if (DebugifyAndStripAll
== cl::BOU_TRUE
)
806 addVerifyPass(Banner
);
809 /// Add common target configurable passes that perform LLVM IR to IR transforms
810 /// following machine independent optimization.
811 void TargetPassConfig::addIRPasses() {
812 // Before running any passes, run the verifier to determine if the input
813 // coming from the front-end and/or optimizer is valid.
815 addPass(createVerifierPass());
817 if (getOptLevel() != CodeGenOptLevel::None
) {
818 // Basic AliasAnalysis support.
819 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
820 // BasicAliasAnalysis wins if they disagree. This is intended to help
821 // support "obvious" type-punning idioms.
822 addPass(createTypeBasedAAWrapperPass());
823 addPass(createScopedNoAliasAAWrapperPass());
824 addPass(createBasicAAWrapperPass());
826 // Run loop strength reduction before anything else.
828 addPass(createCanonicalizeFreezeInLoopsPass());
829 addPass(createLoopStrengthReducePass());
831 addPass(createPrintFunctionPass(dbgs(),
832 "\n\n*** Code after LSR ***\n"));
835 // The MergeICmpsPass tries to create memcmp calls by grouping sequences of
836 // loads and compares. ExpandMemCmpPass then tries to expand those calls
837 // into optimally-sized loads and compares. The transforms are enabled by a
838 // target lowering hook.
839 if (!DisableMergeICmps
)
840 addPass(createMergeICmpsLegacyPass());
841 addPass(createExpandMemCmpLegacyPass());
844 // Run GC lowering passes for builtin collectors
845 // TODO: add a pass insertion point here
846 addPass(&GCLoweringID
);
847 addPass(&ShadowStackGCLoweringID
);
848 addPass(createLowerConstantIntrinsicsPass());
850 // For MachO, lower @llvm.global_dtors into @llvm.global_ctors with
851 // __cxa_atexit() calls to avoid emitting the deprecated __mod_term_func.
852 if (TM
->getTargetTriple().isOSBinFormatMachO() &&
853 !DisableAtExitBasedGlobalDtorLowering
)
854 addPass(createLowerGlobalDtorsLegacyPass());
856 // Make sure that no unreachable blocks are instruction selected.
857 addPass(createUnreachableBlockEliminationPass());
859 // Prepare expensive constants for SelectionDAG.
860 if (getOptLevel() != CodeGenOptLevel::None
&& !DisableConstantHoisting
)
861 addPass(createConstantHoistingPass());
863 if (getOptLevel() != CodeGenOptLevel::None
&& !DisableReplaceWithVecLib
)
864 addPass(createReplaceWithVeclibLegacyPass());
866 if (getOptLevel() != CodeGenOptLevel::None
&& !DisablePartialLibcallInlining
)
867 addPass(createPartiallyInlineLibCallsPass());
869 // Expand vector predication intrinsics into standard IR instructions.
870 // This pass has to run before ScalarizeMaskedMemIntrin and ExpandReduction
871 // passes since it emits those kinds of intrinsics.
872 addPass(createExpandVectorPredicationPass());
874 // Instrument function entry after all inlining.
875 addPass(createPostInlineEntryExitInstrumenterPass());
877 // Add scalarization of target's unsupported masked memory intrinsics pass.
878 // the unsupported intrinsic will be replaced with a chain of basic blocks,
879 // that stores/loads element one-by-one if the appropriate mask bit is set.
880 addPass(createScalarizeMaskedMemIntrinLegacyPass());
882 // Expand reduction intrinsics into shuffle sequences if the target wants to.
883 // Allow disabling it for testing purposes.
884 if (!DisableExpandReductions
)
885 addPass(createExpandReductionsPass());
887 if (getOptLevel() != CodeGenOptLevel::None
)
888 addPass(createTLSVariableHoistPass());
890 // Convert conditional moves to conditional jumps when profitable.
891 if (getOptLevel() != CodeGenOptLevel::None
&& !DisableSelectOptimize
)
892 addPass(createSelectOptimizePass());
895 /// Turn exception handling constructs into something the code generators can
897 void TargetPassConfig::addPassesToHandleExceptions() {
898 const MCAsmInfo
*MCAI
= TM
->getMCAsmInfo();
899 assert(MCAI
&& "No MCAsmInfo");
900 switch (MCAI
->getExceptionHandlingType()) {
901 case ExceptionHandling::SjLj
:
902 // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both
903 // Dwarf EH prepare needs to be run after SjLj prepare. Otherwise,
904 // catch info can get misplaced when a selector ends up more than one block
905 // removed from the parent invoke(s). This could happen when a landing
906 // pad is shared by multiple invokes and is also a target of a normal
907 // edge from elsewhere.
908 addPass(createSjLjEHPreparePass(TM
));
910 case ExceptionHandling::DwarfCFI
:
911 case ExceptionHandling::ARM
:
912 case ExceptionHandling::AIX
:
913 case ExceptionHandling::ZOS
:
914 addPass(createDwarfEHPass(getOptLevel()));
916 case ExceptionHandling::WinEH
:
917 // We support using both GCC-style and MSVC-style exceptions on Windows, so
918 // add both preparation passes. Each pass will only actually run if it
919 // recognizes the personality function.
920 addPass(createWinEHPass());
921 addPass(createDwarfEHPass(getOptLevel()));
923 case ExceptionHandling::Wasm
:
924 // Wasm EH uses Windows EH instructions, but it does not need to demote PHIs
925 // on catchpads and cleanuppads because it does not outline them into
926 // funclets. Catchswitch blocks are not lowered in SelectionDAG, so we
927 // should remove PHIs there.
928 addPass(createWinEHPass(/*DemoteCatchSwitchPHIOnly=*/true));
929 addPass(createWasmEHPass());
931 case ExceptionHandling::None
:
932 addPass(createLowerInvokePass());
934 // The lower invoke pass may create unreachable code. Remove it.
935 addPass(createUnreachableBlockEliminationPass());
940 /// Add pass to prepare the LLVM IR for code generation. This should be done
941 /// before exception handling preparation passes.
942 void TargetPassConfig::addCodeGenPrepare() {
943 if (getOptLevel() != CodeGenOptLevel::None
&& !DisableCGP
)
944 addPass(createCodeGenPrepareLegacyPass());
947 /// Add common passes that perform LLVM IR to IR transforms in preparation for
948 /// instruction selection.
949 void TargetPassConfig::addISelPrepare() {
952 // Force codegen to run according to the callgraph.
953 if (requiresCodeGenSCCOrder())
954 addPass(new DummyCGSCCPass
);
956 addPass(createCallBrPass());
958 // Add both the safe stack and the stack protection passes: each of them will
959 // only protect functions that have corresponding attributes.
960 addPass(createSafeStackPass());
961 addPass(createStackProtectorPass());
964 addPass(createPrintFunctionPass(
965 dbgs(), "\n\n*** Final LLVM Code input to ISel ***\n"));
967 // All passes which modify the LLVM IR are now complete; run the verifier
968 // to ensure that the IR is valid.
970 addPass(createVerifierPass());
973 bool TargetPassConfig::addCoreISelPasses() {
974 // Enable FastISel with -fast-isel, but allow that to be overridden.
975 TM
->setO0WantsFastISel(EnableFastISelOption
!= cl::BOU_FALSE
);
977 // Determine an instruction selector.
978 enum class SelectorType
{ SelectionDAG
, FastISel
, GlobalISel
};
979 SelectorType Selector
;
981 if (EnableFastISelOption
== cl::BOU_TRUE
)
982 Selector
= SelectorType::FastISel
;
983 else if (EnableGlobalISelOption
== cl::BOU_TRUE
||
984 (TM
->Options
.EnableGlobalISel
&&
985 EnableGlobalISelOption
!= cl::BOU_FALSE
))
986 Selector
= SelectorType::GlobalISel
;
987 else if (TM
->getOptLevel() == CodeGenOptLevel::None
&&
988 TM
->getO0WantsFastISel())
989 Selector
= SelectorType::FastISel
;
991 Selector
= SelectorType::SelectionDAG
;
993 // Set consistently TM->Options.EnableFastISel and EnableGlobalISel.
994 if (Selector
== SelectorType::FastISel
) {
995 TM
->setFastISel(true);
996 TM
->setGlobalISel(false);
997 } else if (Selector
== SelectorType::GlobalISel
) {
998 TM
->setFastISel(false);
999 TM
->setGlobalISel(true);
1002 // FIXME: Injecting into the DAGISel pipeline seems to cause issues with
1003 // analyses needing to be re-run. This can result in being unable to
1004 // schedule passes (particularly with 'Function Alias Analysis
1005 // Results'). It's not entirely clear why but AFAICT this seems to be
1006 // due to one FunctionPassManager not being able to use analyses from a
1007 // previous one. As we're injecting a ModulePass we break the usual
1008 // pass manager into two. GlobalISel with the fallback path disabled
1009 // and -run-pass seem to be unaffected. The majority of GlobalISel
1010 // testing uses -run-pass so this probably isn't too bad.
1011 SaveAndRestore
SavedDebugifyIsSafe(DebugifyIsSafe
);
1012 if (Selector
!= SelectorType::GlobalISel
|| !isGlobalISelAbortEnabled())
1013 DebugifyIsSafe
= false;
1015 // Add instruction selector passes.
1016 if (Selector
== SelectorType::GlobalISel
) {
1017 SaveAndRestore
SavedAddingMachinePasses(AddingMachinePasses
, true);
1018 if (addIRTranslator())
1021 addPreLegalizeMachineIR();
1023 if (addLegalizeMachineIR())
1026 // Before running the register bank selector, ask the target if it
1027 // wants to run some passes.
1028 addPreRegBankSelect();
1030 if (addRegBankSelect())
1033 addPreGlobalInstructionSelect();
1035 if (addGlobalInstructionSelect())
1038 // Pass to reset the MachineFunction if the ISel failed.
1039 addPass(createResetMachineFunctionPass(
1040 reportDiagnosticWhenGlobalISelFallback(), isGlobalISelAbortEnabled()));
1042 // Provide a fallback path when we do not want to abort on
1043 // not-yet-supported input.
1044 if (!isGlobalISelAbortEnabled() && addInstSelector())
1047 } else if (addInstSelector())
1050 // Expand pseudo-instructions emitted by ISel. Don't run the verifier before
1052 addPass(&FinalizeISelID
);
1054 // Print the instruction selected machine code...
1055 printAndVerify("After Instruction Selection");
1060 bool TargetPassConfig::addISelPasses() {
1061 if (TM
->useEmulatedTLS())
1062 addPass(createLowerEmuTLSPass());
1064 PM
->add(createTargetTransformInfoWrapperPass(TM
->getTargetIRAnalysis()));
1065 addPass(createPreISelIntrinsicLoweringPass());
1066 addPass(createExpandLargeDivRemPass());
1067 addPass(createExpandLargeFpConvertPass());
1069 addCodeGenPrepare();
1070 addPassesToHandleExceptions();
1073 return addCoreISelPasses();
1076 /// -regalloc=... command line option.
1077 static FunctionPass
*useDefaultRegisterAllocator() { return nullptr; }
1078 static cl::opt
<RegisterRegAlloc::FunctionPassCtor
, false,
1079 RegisterPassParser
<RegisterRegAlloc
>>
1080 RegAlloc("regalloc", cl::Hidden
, cl::init(&useDefaultRegisterAllocator
),
1081 cl::desc("Register allocator to use"));
1083 /// Add the complete set of target-independent postISel code generator passes.
1085 /// This can be read as the standard order of major LLVM CodeGen stages. Stages
1086 /// with nontrivial configuration or multiple passes are broken out below in
1087 /// add%Stage routines.
1089 /// Any TargetPassConfig::addXX routine may be overriden by the Target. The
1090 /// addPre/Post methods with empty header implementations allow injecting
1091 /// target-specific fixups just before or after major stages. Additionally,
1092 /// targets have the flexibility to change pass order within a stage by
1093 /// overriding default implementation of add%Stage routines below. Each
1094 /// technique has maintainability tradeoffs because alternate pass orders are
1095 /// not well supported. addPre/Post works better if the target pass is easily
1096 /// tied to a common pass. But if it has subtle dependencies on multiple passes,
1097 /// the target should override the stage instead.
1099 /// TODO: We could use a single addPre/Post(ID) hook to allow pass injection
1100 /// before/after any target-independent pass. But it's currently overkill.
1101 void TargetPassConfig::addMachinePasses() {
1102 AddingMachinePasses
= true;
1104 // Add passes that optimize machine instructions in SSA form.
1105 if (getOptLevel() != CodeGenOptLevel::None
) {
1106 addMachineSSAOptimization();
1108 // If the target requests it, assign local variables to stack slots relative
1109 // to one another and simplify frame index references where possible.
1110 addPass(&LocalStackSlotAllocationID
);
1113 if (TM
->Options
.EnableIPRA
)
1114 addPass(createRegUsageInfoPropPass());
1116 // Run pre-ra passes.
1119 // Debugifying the register allocator passes seems to provoke some
1120 // non-determinism that affects CodeGen and there doesn't seem to be a point
1121 // where it becomes safe again so stop debugifying here.
1122 DebugifyIsSafe
= false;
1124 // Add a FSDiscriminator pass right before RA, so that we could get
1125 // more precise SampleFDO profile for RA.
1126 if (EnableFSDiscriminator
) {
1127 addPass(createMIRAddFSDiscriminatorsPass(
1128 sampleprof::FSDiscriminatorPass::Pass1
));
1129 const std::string ProfileFile
= getFSProfileFile(TM
);
1130 if (!ProfileFile
.empty() && !DisableRAFSProfileLoader
)
1131 addPass(createMIRProfileLoaderPass(ProfileFile
, getFSRemappingFile(TM
),
1132 sampleprof::FSDiscriminatorPass::Pass1
,
1136 // Run register allocation and passes that are tightly coupled with it,
1137 // including phi elimination and scheduling.
1138 if (getOptimizeRegAlloc())
1139 addOptimizedRegAlloc();
1143 // Run post-ra passes.
1146 addPass(&RemoveRedundantDebugValuesID
);
1148 addPass(&FixupStatepointCallerSavedID
);
1150 // Insert prolog/epilog code. Eliminate abstract frame index references...
1151 if (getOptLevel() != CodeGenOptLevel::None
) {
1152 addPass(&PostRAMachineSinkingID
);
1153 addPass(&ShrinkWrapID
);
1156 // Prolog/Epilog inserter needs a TargetMachine to instantiate. But only
1157 // do so if it hasn't been disabled, substituted, or overridden.
1158 if (!isPassSubstitutedOrOverridden(&PrologEpilogCodeInserterID
))
1159 addPass(createPrologEpilogInserterPass());
1161 /// Add passes that optimize machine instructions after register allocation.
1162 if (getOptLevel() != CodeGenOptLevel::None
)
1163 addMachineLateOptimization();
1165 // Expand pseudo instructions before second scheduling pass.
1166 addPass(&ExpandPostRAPseudosID
);
1168 // Run pre-sched2 passes.
1171 if (EnableImplicitNullChecks
)
1172 addPass(&ImplicitNullChecksID
);
1174 // Second pass scheduler.
1175 // Let Target optionally insert this pass by itself at some other
1177 if (getOptLevel() != CodeGenOptLevel::None
&&
1178 !TM
->targetSchedulesPostRAScheduling()) {
1180 addPass(&PostMachineSchedulerID
);
1182 addPass(&PostRASchedulerID
);
1188 // Basic block placement.
1189 if (getOptLevel() != CodeGenOptLevel::None
)
1190 addBlockPlacement();
1192 // Insert before XRay Instrumentation.
1193 addPass(&FEntryInserterID
);
1195 addPass(&XRayInstrumentationID
);
1196 addPass(&PatchableFunctionID
);
1200 if (TM
->Options
.EnableIPRA
)
1201 // Collect register usage information and produce a register mask of
1202 // clobbered registers, to be used to optimize call sites.
1203 addPass(createRegUsageInfoCollector());
1205 // FIXME: Some backends are incompatible with running the verifier after
1206 // addPreEmitPass. Maybe only pass "false" here for those targets?
1207 addPass(&FuncletLayoutID
);
1209 addPass(&StackMapLivenessID
);
1210 addPass(&LiveDebugValuesID
);
1211 addPass(&MachineSanitizerBinaryMetadataID
);
1213 if (TM
->Options
.EnableMachineOutliner
&&
1214 getOptLevel() != CodeGenOptLevel::None
&&
1215 EnableMachineOutliner
!= RunOutliner::NeverOutline
) {
1216 bool RunOnAllFunctions
=
1217 (EnableMachineOutliner
== RunOutliner::AlwaysOutline
);
1219 RunOnAllFunctions
|| TM
->Options
.SupportsDefaultOutlining
;
1221 addPass(createMachineOutlinerPass(RunOnAllFunctions
));
1225 addPass(llvm::createGCEmptyBasicBlocksPass());
1227 if (EnableFSDiscriminator
)
1228 addPass(createMIRAddFSDiscriminatorsPass(
1229 sampleprof::FSDiscriminatorPass::PassLast
));
1231 bool NeedsBBSections
=
1232 TM
->getBBSectionsType() != llvm::BasicBlockSection::None
;
1233 // Machine function splitter uses the basic block sections feature. Both
1234 // cannot be enabled at the same time. We do not apply machine function
1235 // splitter if -basic-block-sections is requested.
1236 if (!NeedsBBSections
&& (TM
->Options
.EnableMachineFunctionSplitter
||
1237 EnableMachineFunctionSplitter
)) {
1238 const std::string ProfileFile
= getFSProfileFile(TM
);
1239 if (!ProfileFile
.empty()) {
1240 if (EnableFSDiscriminator
) {
1241 addPass(createMIRProfileLoaderPass(
1242 ProfileFile
, getFSRemappingFile(TM
),
1243 sampleprof::FSDiscriminatorPass::PassLast
, nullptr));
1245 // Sample profile is given, but FSDiscriminator is not
1246 // enabled, this may result in performance regression.
1247 WithColor::warning()
1248 << "Using AutoFDO without FSDiscriminator for MFS may regress "
1252 addPass(createMachineFunctionSplitterPass());
1254 // We run the BasicBlockSections pass if either we need BB sections or BB
1255 // address map (or both).
1256 if (NeedsBBSections
|| TM
->Options
.BBAddrMap
) {
1257 if (TM
->getBBSectionsType() == llvm::BasicBlockSection::List
) {
1258 addPass(llvm::createBasicBlockSectionsProfileReaderWrapperPass(
1259 TM
->getBBSectionsFuncListBuf()));
1260 addPass(llvm::createBasicBlockPathCloningPass());
1262 addPass(llvm::createBasicBlockSectionsPass());
1265 addPostBBSections();
1267 if (!DisableCFIFixup
&& TM
->Options
.EnableCFIFixup
)
1268 addPass(createCFIFixup());
1270 PM
->add(createStackFrameLayoutAnalysisPass());
1272 // Add passes that directly emit MI after all other MI passes.
1275 AddingMachinePasses
= false;
1278 /// Add passes that optimize machine instructions in SSA form.
1279 void TargetPassConfig::addMachineSSAOptimization() {
1280 // Pre-ra tail duplication.
1281 addPass(&EarlyTailDuplicateID
);
1283 // Optimize PHIs before DCE: removing dead PHI cycles may make more
1284 // instructions dead.
1285 addPass(&OptimizePHIsID
);
1287 // This pass merges large allocas. StackSlotColoring is a different pass
1288 // which merges spill slots.
1289 addPass(&StackColoringID
);
1291 // If the target requests it, assign local variables to stack slots relative
1292 // to one another and simplify frame index references where possible.
1293 addPass(&LocalStackSlotAllocationID
);
1295 // With optimization, dead code should already be eliminated. However
1296 // there is one known exception: lowered code for arguments that are only
1297 // used by tail calls, where the tail calls reuse the incoming stack
1298 // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll).
1299 addPass(&DeadMachineInstructionElimID
);
1301 // Allow targets to insert passes that improve instruction level parallelism,
1302 // like if-conversion. Such passes will typically need dominator trees and
1303 // loop info, just like LICM and CSE below.
1306 addPass(&EarlyMachineLICMID
);
1307 addPass(&MachineCSEID
);
1309 addPass(&MachineSinkingID
);
1311 addPass(&PeepholeOptimizerID
);
1312 // Clean-up the dead code that may have been generated by peephole
1314 addPass(&DeadMachineInstructionElimID
);
1317 //===---------------------------------------------------------------------===//
1318 /// Register Allocation Pass Configuration
1319 //===---------------------------------------------------------------------===//
1321 bool TargetPassConfig::getOptimizeRegAlloc() const {
1322 switch (OptimizeRegAlloc
) {
1324 return getOptLevel() != CodeGenOptLevel::None
;
1325 case cl::BOU_TRUE
: return true;
1326 case cl::BOU_FALSE
: return false;
1328 llvm_unreachable("Invalid optimize-regalloc state");
1331 /// A dummy default pass factory indicates whether the register allocator is
1332 /// overridden on the command line.
1333 static llvm::once_flag InitializeDefaultRegisterAllocatorFlag
;
1335 static RegisterRegAlloc
1336 defaultRegAlloc("default",
1337 "pick register allocator based on -O option",
1338 useDefaultRegisterAllocator
);
1340 static void initializeDefaultRegisterAllocatorOnce() {
1341 if (!RegisterRegAlloc::getDefault())
1342 RegisterRegAlloc::setDefault(RegAlloc
);
1345 /// Instantiate the default register allocator pass for this target for either
1346 /// the optimized or unoptimized allocation path. This will be added to the pass
1347 /// manager by addFastRegAlloc in the unoptimized case or addOptimizedRegAlloc
1348 /// in the optimized case.
1350 /// A target that uses the standard regalloc pass order for fast or optimized
1351 /// allocation may still override this for per-target regalloc
1352 /// selection. But -regalloc=... always takes precedence.
1353 FunctionPass
*TargetPassConfig::createTargetRegisterAllocator(bool Optimized
) {
1355 return createGreedyRegisterAllocator();
1357 return createFastRegisterAllocator();
1360 /// Find and instantiate the register allocation pass requested by this target
1361 /// at the current optimization level. Different register allocators are
1362 /// defined as separate passes because they may require different analysis.
1364 /// This helper ensures that the regalloc= option is always available,
1365 /// even for targets that override the default allocator.
1367 /// FIXME: When MachinePassRegistry register pass IDs instead of function ptrs,
1368 /// this can be folded into addPass.
1369 FunctionPass
*TargetPassConfig::createRegAllocPass(bool Optimized
) {
1370 // Initialize the global default.
1371 llvm::call_once(InitializeDefaultRegisterAllocatorFlag
,
1372 initializeDefaultRegisterAllocatorOnce
);
1374 RegisterRegAlloc::FunctionPassCtor Ctor
= RegisterRegAlloc::getDefault();
1375 if (Ctor
!= useDefaultRegisterAllocator
)
1378 // With no -regalloc= override, ask the target for a regalloc pass.
1379 return createTargetRegisterAllocator(Optimized
);
1382 bool TargetPassConfig::isCustomizedRegAlloc() {
1384 (RegisterRegAlloc::FunctionPassCtor
)&useDefaultRegisterAllocator
;
1387 bool TargetPassConfig::addRegAssignAndRewriteFast() {
1388 if (RegAlloc
!= (RegisterRegAlloc::FunctionPassCtor
)&useDefaultRegisterAllocator
&&
1389 RegAlloc
!= (RegisterRegAlloc::FunctionPassCtor
)&createFastRegisterAllocator
)
1390 report_fatal_error("Must use fast (default) register allocator for unoptimized regalloc.");
1392 addPass(createRegAllocPass(false));
1394 // Allow targets to change the register assignments after
1395 // fast register allocation.
1396 addPostFastRegAllocRewrite();
1400 bool TargetPassConfig::addRegAssignAndRewriteOptimized() {
1401 // Add the selected register allocation pass.
1402 addPass(createRegAllocPass(true));
1404 // Allow targets to change the register assignments before rewriting.
1407 // Finally rewrite virtual registers.
1408 addPass(&VirtRegRewriterID
);
1410 // Regalloc scoring for ML-driven eviction - noop except when learning a new
1412 addPass(createRegAllocScoringPass());
1416 /// Return true if the default global register allocator is in use and
1417 /// has not be overriden on the command line with '-regalloc=...'
1418 bool TargetPassConfig::usingDefaultRegAlloc() const {
1419 return RegAlloc
.getNumOccurrences() == 0;
1422 /// Add the minimum set of target-independent passes that are required for
1423 /// register allocation. No coalescing or scheduling.
1424 void TargetPassConfig::addFastRegAlloc() {
1425 addPass(&PHIEliminationID
);
1426 addPass(&TwoAddressInstructionPassID
);
1428 addRegAssignAndRewriteFast();
1431 /// Add standard target-independent passes that are tightly coupled with
1432 /// optimized register allocation, including coalescing, machine instruction
1433 /// scheduling, and register allocation itself.
1434 void TargetPassConfig::addOptimizedRegAlloc() {
1435 addPass(&DetectDeadLanesID
);
1437 addPass(&InitUndefID
);
1439 addPass(&ProcessImplicitDefsID
);
1441 // LiveVariables currently requires pure SSA form.
1443 // FIXME: Once TwoAddressInstruction pass no longer uses kill flags,
1444 // LiveVariables can be removed completely, and LiveIntervals can be directly
1445 // computed. (We still either need to regenerate kill flags after regalloc, or
1446 // preferably fix the scavenger to not depend on them).
1447 // FIXME: UnreachableMachineBlockElim is a dependant pass of LiveVariables.
1448 // When LiveVariables is removed this has to be removed/moved either.
1449 // Explicit addition of UnreachableMachineBlockElim allows stopping before or
1450 // after it with -stop-before/-stop-after.
1451 addPass(&UnreachableMachineBlockElimID
);
1452 addPass(&LiveVariablesID
);
1454 // Edge splitting is smarter with machine loop info.
1455 addPass(&MachineLoopInfoID
);
1456 addPass(&PHIEliminationID
);
1458 // Eventually, we want to run LiveIntervals before PHI elimination.
1459 if (EarlyLiveIntervals
)
1460 addPass(&LiveIntervalsID
);
1462 addPass(&TwoAddressInstructionPassID
);
1463 addPass(&RegisterCoalescerID
);
1465 // The machine scheduler may accidentally create disconnected components
1466 // when moving subregister definitions around, avoid this by splitting them to
1467 // separate vregs before. Splitting can also improve reg. allocation quality.
1468 addPass(&RenameIndependentSubregsID
);
1470 // PreRA instruction scheduling.
1471 addPass(&MachineSchedulerID
);
1473 if (addRegAssignAndRewriteOptimized()) {
1474 // Perform stack slot coloring and post-ra machine LICM.
1475 addPass(&StackSlotColoringID
);
1477 // Allow targets to expand pseudo instructions depending on the choice of
1478 // registers before MachineCopyPropagation.
1481 // Copy propagate to forward register uses and try to eliminate COPYs that
1482 // were not coalesced.
1483 addPass(&MachineCopyPropagationID
);
1485 // Run post-ra machine LICM to hoist reloads / remats.
1487 // FIXME: can this move into MachineLateOptimization?
1488 addPass(&MachineLICMID
);
1492 //===---------------------------------------------------------------------===//
1493 /// Post RegAlloc Pass Configuration
1494 //===---------------------------------------------------------------------===//
1496 /// Add passes that optimize machine instructions after register allocation.
1497 void TargetPassConfig::addMachineLateOptimization() {
1498 // Cleanup of redundant immediate/address loads.
1499 addPass(&MachineLateInstrsCleanupID
);
1501 // Branch folding must be run after regalloc and prolog/epilog insertion.
1502 addPass(&BranchFolderPassID
);
1504 // Tail duplication.
1505 // Note that duplicating tail just increases code size and degrades
1506 // performance for targets that require Structured Control Flow.
1507 // In addition it can also make CFG irreducible. Thus we disable it.
1508 if (!TM
->requiresStructuredCFG())
1509 addPass(&TailDuplicateID
);
1511 // Copy propagation.
1512 addPass(&MachineCopyPropagationID
);
1515 /// Add standard GC passes.
1516 bool TargetPassConfig::addGCPasses() {
1517 addPass(&GCMachineCodeAnalysisID
);
1521 /// Add standard basic block placement passes.
1522 void TargetPassConfig::addBlockPlacement() {
1523 if (EnableFSDiscriminator
) {
1524 addPass(createMIRAddFSDiscriminatorsPass(
1525 sampleprof::FSDiscriminatorPass::Pass2
));
1526 const std::string ProfileFile
= getFSProfileFile(TM
);
1527 if (!ProfileFile
.empty() && !DisableLayoutFSProfileLoader
)
1528 addPass(createMIRProfileLoaderPass(ProfileFile
, getFSRemappingFile(TM
),
1529 sampleprof::FSDiscriminatorPass::Pass2
,
1532 if (addPass(&MachineBlockPlacementID
)) {
1533 // Run a separate pass to collect block placement statistics.
1534 if (EnableBlockPlacementStats
)
1535 addPass(&MachineBlockPlacementStatsID
);
1539 //===---------------------------------------------------------------------===//
1540 /// GlobalISel Configuration
1541 //===---------------------------------------------------------------------===//
1542 bool TargetPassConfig::isGlobalISelAbortEnabled() const {
1543 return TM
->Options
.GlobalISelAbort
== GlobalISelAbortMode::Enable
;
1546 bool TargetPassConfig::reportDiagnosticWhenGlobalISelFallback() const {
1547 return TM
->Options
.GlobalISelAbort
== GlobalISelAbortMode::DisableWithDiag
;
1550 bool TargetPassConfig::isGISelCSEEnabled() const {
1554 std::unique_ptr
<CSEConfigBase
> TargetPassConfig::getCSEConfig() const {
1555 return std::make_unique
<CSEConfigBase
>();