xref: /llvm-project/llvm/lib/CodeGen/TargetPassConfig.cpp (revision a0275705bb5aa938119c3e7c8bc957a823450b17)
1 //===- TargetPassConfig.cpp - Target independent code generation passes ---===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines interfaces to access the target independent code
10 // generation passes provided by the LLVM backend.
11 //
12 //===---------------------------------------------------------------------===//
13 
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/CFLAndersAliasAnalysis.h"
20 #include "llvm/Analysis/CFLSteensAliasAnalysis.h"
21 #include "llvm/Analysis/CallGraphSCCPass.h"
22 #include "llvm/Analysis/ScopedNoAliasAA.h"
23 #include "llvm/Analysis/TargetTransformInfo.h"
24 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
25 #include "llvm/CodeGen/CSEConfigBase.h"
26 #include "llvm/CodeGen/MachineFunctionPass.h"
27 #include "llvm/CodeGen/MachinePassRegistry.h"
28 #include "llvm/CodeGen/Passes.h"
29 #include "llvm/CodeGen/RegAllocRegistry.h"
30 #include "llvm/IR/IRPrintingPasses.h"
31 #include "llvm/IR/LegacyPassManager.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/ErrorHandling.h"
42 #include "llvm/Support/SaveAndRestore.h"
43 #include "llvm/Support/Threading.h"
44 #include "llvm/Target/TargetMachine.h"
45 #include "llvm/Transforms/Scalar.h"
46 #include "llvm/Transforms/Utils.h"
47 #include "llvm/Transforms/Utils/SymbolRewriter.h"
48 #include <cassert>
49 #include <string>
50 
51 using namespace llvm;
52 
53 static cl::opt<bool>
54     EnableIPRA("enable-ipra", cl::init(false), cl::Hidden,
55                cl::desc("Enable interprocedural register allocation "
56                         "to reduce load/store at procedure calls."));
57 static cl::opt<bool> DisablePostRASched("disable-post-ra", cl::Hidden,
58     cl::desc("Disable Post Regalloc Scheduler"));
59 static cl::opt<bool> DisableBranchFold("disable-branch-fold", cl::Hidden,
60     cl::desc("Disable branch folding"));
61 static cl::opt<bool> DisableTailDuplicate("disable-tail-duplicate", cl::Hidden,
62     cl::desc("Disable tail duplication"));
63 static cl::opt<bool> DisableEarlyTailDup("disable-early-taildup", cl::Hidden,
64     cl::desc("Disable pre-register allocation tail duplication"));
65 static cl::opt<bool> DisableBlockPlacement("disable-block-placement",
66     cl::Hidden, cl::desc("Disable probability-driven block placement"));
67 static cl::opt<bool> EnableBlockPlacementStats("enable-block-placement-stats",
68     cl::Hidden, cl::desc("Collect probability-driven block placement stats"));
69 static cl::opt<bool> DisableSSC("disable-ssc", cl::Hidden,
70     cl::desc("Disable Stack Slot Coloring"));
71 static cl::opt<bool> DisableMachineDCE("disable-machine-dce", cl::Hidden,
72     cl::desc("Disable Machine Dead Code Elimination"));
73 static cl::opt<bool> DisableEarlyIfConversion("disable-early-ifcvt", cl::Hidden,
74     cl::desc("Disable Early If-conversion"));
75 static cl::opt<bool> DisableMachineLICM("disable-machine-licm", cl::Hidden,
76     cl::desc("Disable Machine LICM"));
77 static cl::opt<bool> DisableMachineCSE("disable-machine-cse", cl::Hidden,
78     cl::desc("Disable Machine Common Subexpression Elimination"));
79 static cl::opt<cl::boolOrDefault> OptimizeRegAlloc(
80     "optimize-regalloc", cl::Hidden,
81     cl::desc("Enable optimized register allocation compilation path."));
82 static cl::opt<bool> DisablePostRAMachineLICM("disable-postra-machine-licm",
83     cl::Hidden,
84     cl::desc("Disable Machine LICM"));
85 static cl::opt<bool> DisableMachineSink("disable-machine-sink", cl::Hidden,
86     cl::desc("Disable Machine Sinking"));
87 static cl::opt<bool> DisablePostRAMachineSink("disable-postra-machine-sink",
88     cl::Hidden,
89     cl::desc("Disable PostRA Machine Sinking"));
90 static cl::opt<bool> DisableLSR("disable-lsr", cl::Hidden,
91     cl::desc("Disable Loop Strength Reduction Pass"));
92 static cl::opt<bool> DisableConstantHoisting("disable-constant-hoisting",
93     cl::Hidden, cl::desc("Disable ConstantHoisting"));
94 static cl::opt<bool> DisableCGP("disable-cgp", cl::Hidden,
95     cl::desc("Disable Codegen Prepare"));
96 static cl::opt<bool> DisableCopyProp("disable-copyprop", cl::Hidden,
97     cl::desc("Disable Copy Propagation pass"));
98 static cl::opt<bool> DisablePartialLibcallInlining("disable-partial-libcall-inlining",
99     cl::Hidden, cl::desc("Disable Partial Libcall Inlining"));
100 static cl::opt<bool> EnableImplicitNullChecks(
101     "enable-implicit-null-checks",
102     cl::desc("Fold null checks into faulting memory operations"),
103     cl::init(false), cl::Hidden);
104 static cl::opt<bool> DisableMergeICmps("disable-mergeicmps",
105     cl::desc("Disable MergeICmps Pass"),
106     cl::init(false), cl::Hidden);
107 static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
108     cl::desc("Print LLVM IR produced by the loop-reduce pass"));
109 static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
110     cl::desc("Print LLVM IR input to isel pass"));
111 static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
112     cl::desc("Dump garbage collector data"));
113 static cl::opt<cl::boolOrDefault>
114     VerifyMachineCode("verify-machineinstrs", cl::Hidden,
115                       cl::desc("Verify generated machine code"),
116                       cl::ZeroOrMore);
117 enum RunOutliner { AlwaysOutline, NeverOutline, TargetDefault };
118 // Enable or disable the MachineOutliner.
119 static cl::opt<RunOutliner> EnableMachineOutliner(
120     "enable-machine-outliner", cl::desc("Enable the machine outliner"),
121     cl::Hidden, cl::ValueOptional, cl::init(TargetDefault),
122     cl::values(clEnumValN(AlwaysOutline, "always",
123                           "Run on all functions guaranteed to be beneficial"),
124                clEnumValN(NeverOutline, "never", "Disable all outlining"),
125                // Sentinel value for unspecified option.
126                clEnumValN(AlwaysOutline, "", "")));
127 // Enable or disable FastISel. Both options are needed, because
128 // FastISel is enabled by default with -fast, and we wish to be
129 // able to enable or disable fast-isel independently from -O0.
130 static cl::opt<cl::boolOrDefault>
131 EnableFastISelOption("fast-isel", cl::Hidden,
132   cl::desc("Enable the \"fast\" instruction selector"));
133 
134 static cl::opt<cl::boolOrDefault> EnableGlobalISelOption(
135     "global-isel", cl::Hidden,
136     cl::desc("Enable the \"global\" instruction selector"));
137 
138 static cl::opt<std::string> PrintMachineInstrs(
139     "print-machineinstrs", cl::ValueOptional, cl::desc("Print machine instrs"),
140     cl::value_desc("pass-name"), cl::init("option-unspecified"), cl::Hidden);
141 
142 static cl::opt<GlobalISelAbortMode> EnableGlobalISelAbort(
143     "global-isel-abort", cl::Hidden,
144     cl::desc("Enable abort calls when \"global\" instruction selection "
145              "fails to lower/select an instruction"),
146     cl::values(
147         clEnumValN(GlobalISelAbortMode::Disable, "0", "Disable the abort"),
148         clEnumValN(GlobalISelAbortMode::Enable, "1", "Enable the abort"),
149         clEnumValN(GlobalISelAbortMode::DisableWithDiag, "2",
150                    "Disable the abort but emit a diagnostic on failure")));
151 
152 // Temporary option to allow experimenting with MachineScheduler as a post-RA
153 // scheduler. Targets can "properly" enable this with
154 // substitutePass(&PostRASchedulerID, &PostMachineSchedulerID).
155 // Targets can return true in targetSchedulesPostRAScheduling() and
156 // insert a PostRA scheduling pass wherever it wants.
157 static cl::opt<bool> MISchedPostRA(
158     "misched-postra", cl::Hidden,
159     cl::desc(
160         "Run MachineScheduler post regalloc (independent of preRA sched)"));
161 
162 // Experimental option to run live interval analysis early.
163 static cl::opt<bool> EarlyLiveIntervals("early-live-intervals", cl::Hidden,
164     cl::desc("Run live interval analysis earlier in the pipeline"));
165 
166 // Experimental option to use CFL-AA in codegen
167 enum class CFLAAType { None, Steensgaard, Andersen, Both };
168 static cl::opt<CFLAAType> UseCFLAA(
169     "use-cfl-aa-in-codegen", cl::init(CFLAAType::None), cl::Hidden,
170     cl::desc("Enable the new, experimental CFL alias analysis in CodeGen"),
171     cl::values(clEnumValN(CFLAAType::None, "none", "Disable CFL-AA"),
172                clEnumValN(CFLAAType::Steensgaard, "steens",
173                           "Enable unification-based CFL-AA"),
174                clEnumValN(CFLAAType::Andersen, "anders",
175                           "Enable inclusion-based CFL-AA"),
176                clEnumValN(CFLAAType::Both, "both",
177                           "Enable both variants of CFL-AA")));
178 
179 /// Option names for limiting the codegen pipeline.
180 /// Those are used in error reporting and we didn't want
181 /// to duplicate their names all over the place.
182 static const char StartAfterOptName[] = "start-after";
183 static const char StartBeforeOptName[] = "start-before";
184 static const char StopAfterOptName[] = "stop-after";
185 static const char StopBeforeOptName[] = "stop-before";
186 
187 static cl::opt<std::string>
188     StartAfterOpt(StringRef(StartAfterOptName),
189                   cl::desc("Resume compilation after a specific pass"),
190                   cl::value_desc("pass-name"), cl::init(""), cl::Hidden);
191 
192 static cl::opt<std::string>
193     StartBeforeOpt(StringRef(StartBeforeOptName),
194                    cl::desc("Resume compilation before a specific pass"),
195                    cl::value_desc("pass-name"), cl::init(""), cl::Hidden);
196 
197 static cl::opt<std::string>
198     StopAfterOpt(StringRef(StopAfterOptName),
199                  cl::desc("Stop compilation after a specific pass"),
200                  cl::value_desc("pass-name"), cl::init(""), cl::Hidden);
201 
202 static cl::opt<std::string>
203     StopBeforeOpt(StringRef(StopBeforeOptName),
204                   cl::desc("Stop compilation before a specific pass"),
205                   cl::value_desc("pass-name"), cl::init(""), cl::Hidden);
206 
207 extern cl::opt<bool> UseRegistersForDeoptValues;
208 
209 /// Allow standard passes to be disabled by command line options. This supports
210 /// simple binary flags that either suppress the pass or do nothing.
211 /// i.e. -disable-mypass=false has no effect.
212 /// These should be converted to boolOrDefault in order to use applyOverride.
213 static IdentifyingPassPtr applyDisable(IdentifyingPassPtr PassID,
214                                        bool Override) {
215   if (Override)
216     return IdentifyingPassPtr();
217   return PassID;
218 }
219 
220 /// Allow standard passes to be disabled by the command line, regardless of who
221 /// is adding the pass.
222 ///
223 /// StandardID is the pass identified in the standard pass pipeline and provided
224 /// to addPass(). It may be a target-specific ID in the case that the target
225 /// directly adds its own pass, but in that case we harmlessly fall through.
226 ///
227 /// TargetID is the pass that the target has configured to override StandardID.
228 ///
229 /// StandardID may be a pseudo ID. In that case TargetID is the name of the real
230 /// pass to run. This allows multiple options to control a single pass depending
231 /// on where in the pipeline that pass is added.
232 static IdentifyingPassPtr overridePass(AnalysisID StandardID,
233                                        IdentifyingPassPtr TargetID) {
234   if (StandardID == &PostRASchedulerID)
235     return applyDisable(TargetID, DisablePostRASched);
236 
237   if (StandardID == &BranchFolderPassID)
238     return applyDisable(TargetID, DisableBranchFold);
239 
240   if (StandardID == &TailDuplicateID)
241     return applyDisable(TargetID, DisableTailDuplicate);
242 
243   if (StandardID == &EarlyTailDuplicateID)
244     return applyDisable(TargetID, DisableEarlyTailDup);
245 
246   if (StandardID == &MachineBlockPlacementID)
247     return applyDisable(TargetID, DisableBlockPlacement);
248 
249   if (StandardID == &StackSlotColoringID)
250     return applyDisable(TargetID, DisableSSC);
251 
252   if (StandardID == &DeadMachineInstructionElimID)
253     return applyDisable(TargetID, DisableMachineDCE);
254 
255   if (StandardID == &EarlyIfConverterID)
256     return applyDisable(TargetID, DisableEarlyIfConversion);
257 
258   if (StandardID == &EarlyMachineLICMID)
259     return applyDisable(TargetID, DisableMachineLICM);
260 
261   if (StandardID == &MachineCSEID)
262     return applyDisable(TargetID, DisableMachineCSE);
263 
264   if (StandardID == &MachineLICMID)
265     return applyDisable(TargetID, DisablePostRAMachineLICM);
266 
267   if (StandardID == &MachineSinkingID)
268     return applyDisable(TargetID, DisableMachineSink);
269 
270   if (StandardID == &PostRAMachineSinkingID)
271     return applyDisable(TargetID, DisablePostRAMachineSink);
272 
273   if (StandardID == &MachineCopyPropagationID)
274     return applyDisable(TargetID, DisableCopyProp);
275 
276   return TargetID;
277 }
278 
279 //===---------------------------------------------------------------------===//
280 /// TargetPassConfig
281 //===---------------------------------------------------------------------===//
282 
283 INITIALIZE_PASS(TargetPassConfig, "targetpassconfig",
284                 "Target Pass Configuration", false, false)
285 char TargetPassConfig::ID = 0;
286 
287 namespace {
288 
289 struct InsertedPass {
290   AnalysisID TargetPassID;
291   IdentifyingPassPtr InsertedPassID;
292   bool VerifyAfter;
293   bool PrintAfter;
294 
295   InsertedPass(AnalysisID TargetPassID, IdentifyingPassPtr InsertedPassID,
296                bool VerifyAfter, bool PrintAfter)
297       : TargetPassID(TargetPassID), InsertedPassID(InsertedPassID),
298         VerifyAfter(VerifyAfter), PrintAfter(PrintAfter) {}
299 
300   Pass *getInsertedPass() const {
301     assert(InsertedPassID.isValid() && "Illegal Pass ID!");
302     if (InsertedPassID.isInstance())
303       return InsertedPassID.getInstance();
304     Pass *NP = Pass::createPass(InsertedPassID.getID());
305     assert(NP && "Pass ID not registered");
306     return NP;
307   }
308 };
309 
310 } // end anonymous namespace
311 
312 namespace llvm {
313 
314 class PassConfigImpl {
315 public:
316   // List of passes explicitly substituted by this target. Normally this is
317   // empty, but it is a convenient way to suppress or replace specific passes
318   // that are part of a standard pass pipeline without overridding the entire
319   // pipeline. This mechanism allows target options to inherit a standard pass's
320   // user interface. For example, a target may disable a standard pass by
321   // default by substituting a pass ID of zero, and the user may still enable
322   // that standard pass with an explicit command line option.
323   DenseMap<AnalysisID,IdentifyingPassPtr> TargetPasses;
324 
325   /// Store the pairs of <AnalysisID, AnalysisID> of which the second pass
326   /// is inserted after each instance of the first one.
327   SmallVector<InsertedPass, 4> InsertedPasses;
328 };
329 
330 } // end namespace llvm
331 
332 // Out of line virtual method.
333 TargetPassConfig::~TargetPassConfig() {
334   delete Impl;
335 }
336 
337 static const PassInfo *getPassInfo(StringRef PassName) {
338   if (PassName.empty())
339     return nullptr;
340 
341   const PassRegistry &PR = *PassRegistry::getPassRegistry();
342   const PassInfo *PI = PR.getPassInfo(PassName);
343   if (!PI)
344     report_fatal_error(Twine('\"') + Twine(PassName) +
345                        Twine("\" pass is not registered."));
346   return PI;
347 }
348 
349 static AnalysisID getPassIDFromName(StringRef PassName) {
350   const PassInfo *PI = getPassInfo(PassName);
351   return PI ? PI->getTypeInfo() : nullptr;
352 }
353 
354 static std::pair<StringRef, unsigned>
355 getPassNameAndInstanceNum(StringRef PassName) {
356   StringRef Name, InstanceNumStr;
357   std::tie(Name, InstanceNumStr) = PassName.split(',');
358 
359   unsigned InstanceNum = 0;
360   if (!InstanceNumStr.empty() && InstanceNumStr.getAsInteger(10, InstanceNum))
361     report_fatal_error("invalid pass instance specifier " + PassName);
362 
363   return std::make_pair(Name, InstanceNum);
364 }
365 
366 void TargetPassConfig::setStartStopPasses() {
367   StringRef StartBeforeName;
368   std::tie(StartBeforeName, StartBeforeInstanceNum) =
369     getPassNameAndInstanceNum(StartBeforeOpt);
370 
371   StringRef StartAfterName;
372   std::tie(StartAfterName, StartAfterInstanceNum) =
373     getPassNameAndInstanceNum(StartAfterOpt);
374 
375   StringRef StopBeforeName;
376   std::tie(StopBeforeName, StopBeforeInstanceNum)
377     = getPassNameAndInstanceNum(StopBeforeOpt);
378 
379   StringRef StopAfterName;
380   std::tie(StopAfterName, StopAfterInstanceNum)
381     = getPassNameAndInstanceNum(StopAfterOpt);
382 
383   StartBefore = getPassIDFromName(StartBeforeName);
384   StartAfter = getPassIDFromName(StartAfterName);
385   StopBefore = getPassIDFromName(StopBeforeName);
386   StopAfter = getPassIDFromName(StopAfterName);
387   if (StartBefore && StartAfter)
388     report_fatal_error(Twine(StartBeforeOptName) + Twine(" and ") +
389                        Twine(StartAfterOptName) + Twine(" specified!"));
390   if (StopBefore && StopAfter)
391     report_fatal_error(Twine(StopBeforeOptName) + Twine(" and ") +
392                        Twine(StopAfterOptName) + Twine(" specified!"));
393   Started = (StartAfter == nullptr) && (StartBefore == nullptr);
394 }
395 
396 // Out of line constructor provides default values for pass options and
397 // registers all common codegen passes.
398 TargetPassConfig::TargetPassConfig(LLVMTargetMachine &TM, PassManagerBase &pm)
399     : ImmutablePass(ID), PM(&pm), TM(&TM) {
400   Impl = new PassConfigImpl();
401 
402   // Register all target independent codegen passes to activate their PassIDs,
403   // including this pass itself.
404   initializeCodeGen(*PassRegistry::getPassRegistry());
405 
406   // Also register alias analysis passes required by codegen passes.
407   initializeBasicAAWrapperPassPass(*PassRegistry::getPassRegistry());
408   initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry());
409 
410   if (StringRef(PrintMachineInstrs.getValue()).equals(""))
411     TM.Options.PrintMachineCode = true;
412 
413   if (EnableIPRA.getNumOccurrences())
414     TM.Options.EnableIPRA = EnableIPRA;
415   else {
416     // If not explicitly specified, use target default.
417     TM.Options.EnableIPRA |= TM.useIPRA();
418   }
419 
420   if (TM.Options.EnableIPRA)
421     setRequiresCodeGenSCCOrder();
422 
423   if (EnableGlobalISelAbort.getNumOccurrences())
424     TM.Options.GlobalISelAbort = EnableGlobalISelAbort;
425 
426   setStartStopPasses();
427 }
428 
429 CodeGenOpt::Level TargetPassConfig::getOptLevel() const {
430   return TM->getOptLevel();
431 }
432 
433 /// Insert InsertedPassID pass after TargetPassID.
434 void TargetPassConfig::insertPass(AnalysisID TargetPassID,
435                                   IdentifyingPassPtr InsertedPassID,
436                                   bool VerifyAfter, bool PrintAfter) {
437   assert(((!InsertedPassID.isInstance() &&
438            TargetPassID != InsertedPassID.getID()) ||
439           (InsertedPassID.isInstance() &&
440            TargetPassID != InsertedPassID.getInstance()->getPassID())) &&
441          "Insert a pass after itself!");
442   Impl->InsertedPasses.emplace_back(TargetPassID, InsertedPassID, VerifyAfter,
443                                     PrintAfter);
444 }
445 
446 /// createPassConfig - Create a pass configuration object to be used by
447 /// addPassToEmitX methods for generating a pipeline of CodeGen passes.
448 ///
449 /// Targets may override this to extend TargetPassConfig.
450 TargetPassConfig *LLVMTargetMachine::createPassConfig(PassManagerBase &PM) {
451   return new TargetPassConfig(*this, PM);
452 }
453 
454 TargetPassConfig::TargetPassConfig()
455   : ImmutablePass(ID) {
456   report_fatal_error("Trying to construct TargetPassConfig without a target "
457                      "machine. Scheduling a CodeGen pass without a target "
458                      "triple set?");
459 }
460 
461 bool TargetPassConfig::willCompleteCodeGenPipeline() {
462   return StopBeforeOpt.empty() && StopAfterOpt.empty();
463 }
464 
465 bool TargetPassConfig::hasLimitedCodeGenPipeline() {
466   return !StartBeforeOpt.empty() || !StartAfterOpt.empty() ||
467          !willCompleteCodeGenPipeline();
468 }
469 
470 std::string
471 TargetPassConfig::getLimitedCodeGenPipelineReason(const char *Separator) const {
472   if (!hasLimitedCodeGenPipeline())
473     return std::string();
474   std::string Res;
475   static cl::opt<std::string> *PassNames[] = {&StartAfterOpt, &StartBeforeOpt,
476                                               &StopAfterOpt, &StopBeforeOpt};
477   static const char *OptNames[] = {StartAfterOptName, StartBeforeOptName,
478                                    StopAfterOptName, StopBeforeOptName};
479   bool IsFirst = true;
480   for (int Idx = 0; Idx < 4; ++Idx)
481     if (!PassNames[Idx]->empty()) {
482       if (!IsFirst)
483         Res += Separator;
484       IsFirst = false;
485       Res += OptNames[Idx];
486     }
487   return Res;
488 }
489 
490 // Helper to verify the analysis is really immutable.
491 void TargetPassConfig::setOpt(bool &Opt, bool Val) {
492   assert(!Initialized && "PassConfig is immutable");
493   Opt = Val;
494 }
495 
496 void TargetPassConfig::substitutePass(AnalysisID StandardID,
497                                       IdentifyingPassPtr TargetID) {
498   Impl->TargetPasses[StandardID] = TargetID;
499 }
500 
501 IdentifyingPassPtr TargetPassConfig::getPassSubstitution(AnalysisID ID) const {
502   DenseMap<AnalysisID, IdentifyingPassPtr>::const_iterator
503     I = Impl->TargetPasses.find(ID);
504   if (I == Impl->TargetPasses.end())
505     return ID;
506   return I->second;
507 }
508 
509 bool TargetPassConfig::isPassSubstitutedOrOverridden(AnalysisID ID) const {
510   IdentifyingPassPtr TargetID = getPassSubstitution(ID);
511   IdentifyingPassPtr FinalPtr = overridePass(ID, TargetID);
512   return !FinalPtr.isValid() || FinalPtr.isInstance() ||
513       FinalPtr.getID() != ID;
514 }
515 
516 /// Add a pass to the PassManager if that pass is supposed to be run.  If the
517 /// Started/Stopped flags indicate either that the compilation should start at
518 /// a later pass or that it should stop after an earlier pass, then do not add
519 /// the pass.  Finally, compare the current pass against the StartAfter
520 /// and StopAfter options and change the Started/Stopped flags accordingly.
521 void TargetPassConfig::addPass(Pass *P, bool verifyAfter, bool printAfter) {
522   assert(!Initialized && "PassConfig is immutable");
523 
524   // Cache the Pass ID here in case the pass manager finds this pass is
525   // redundant with ones already scheduled / available, and deletes it.
526   // Fundamentally, once we add the pass to the manager, we no longer own it
527   // and shouldn't reference it.
528   AnalysisID PassID = P->getPassID();
529 
530   if (StartBefore == PassID && StartBeforeCount++ == StartBeforeInstanceNum)
531     Started = true;
532   if (StopBefore == PassID && StopBeforeCount++ == StopBeforeInstanceNum)
533     Stopped = true;
534   if (Started && !Stopped) {
535     std::string Banner;
536     // Construct banner message before PM->add() as that may delete the pass.
537     if (AddingMachinePasses && (printAfter || verifyAfter))
538       Banner = std::string("After ") + std::string(P->getPassName());
539     PM->add(P);
540     if (AddingMachinePasses) {
541       if (printAfter)
542         addPrintPass(Banner);
543       if (verifyAfter)
544         addVerifyPass(Banner);
545     }
546 
547     // Add the passes after the pass P if there is any.
548     for (auto IP : Impl->InsertedPasses) {
549       if (IP.TargetPassID == PassID)
550         addPass(IP.getInsertedPass(), IP.VerifyAfter, IP.PrintAfter);
551     }
552   } else {
553     delete P;
554   }
555 
556   if (StopAfter == PassID && StopAfterCount++ == StopAfterInstanceNum)
557     Stopped = true;
558 
559   if (StartAfter == PassID && StartAfterCount++ == StartAfterInstanceNum)
560     Started = true;
561   if (Stopped && !Started)
562     report_fatal_error("Cannot stop compilation after pass that is not run");
563 }
564 
565 /// Add a CodeGen pass at this point in the pipeline after checking for target
566 /// and command line overrides.
567 ///
568 /// addPass cannot return a pointer to the pass instance because is internal the
569 /// PassManager and the instance we create here may already be freed.
570 AnalysisID TargetPassConfig::addPass(AnalysisID PassID, bool verifyAfter,
571                                      bool printAfter) {
572   IdentifyingPassPtr TargetID = getPassSubstitution(PassID);
573   IdentifyingPassPtr FinalPtr = overridePass(PassID, TargetID);
574   if (!FinalPtr.isValid())
575     return nullptr;
576 
577   Pass *P;
578   if (FinalPtr.isInstance())
579     P = FinalPtr.getInstance();
580   else {
581     P = Pass::createPass(FinalPtr.getID());
582     if (!P)
583       llvm_unreachable("Pass ID not registered");
584   }
585   AnalysisID FinalID = P->getPassID();
586   addPass(P, verifyAfter, printAfter); // Ends the lifetime of P.
587 
588   return FinalID;
589 }
590 
591 void TargetPassConfig::printAndVerify(const std::string &Banner) {
592   addPrintPass(Banner);
593   addVerifyPass(Banner);
594 }
595 
596 void TargetPassConfig::addPrintPass(const std::string &Banner) {
597   if (TM->shouldPrintMachineCode())
598     PM->add(createMachineFunctionPrinterPass(dbgs(), Banner));
599 }
600 
601 void TargetPassConfig::addVerifyPass(const std::string &Banner) {
602   bool Verify = VerifyMachineCode == cl::BOU_TRUE;
603 #ifdef EXPENSIVE_CHECKS
604   if (VerifyMachineCode == cl::BOU_UNSET)
605     Verify = TM->isMachineVerifierClean();
606 #endif
607   if (Verify)
608     PM->add(createMachineVerifierPass(Banner));
609 }
610 
611 /// Add common target configurable passes that perform LLVM IR to IR transforms
612 /// following machine independent optimization.
613 void TargetPassConfig::addIRPasses() {
614   switch (UseCFLAA) {
615   case CFLAAType::Steensgaard:
616     addPass(createCFLSteensAAWrapperPass());
617     break;
618   case CFLAAType::Andersen:
619     addPass(createCFLAndersAAWrapperPass());
620     break;
621   case CFLAAType::Both:
622     addPass(createCFLAndersAAWrapperPass());
623     addPass(createCFLSteensAAWrapperPass());
624     break;
625   default:
626     break;
627   }
628 
629   // Basic AliasAnalysis support.
630   // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
631   // BasicAliasAnalysis wins if they disagree. This is intended to help
632   // support "obvious" type-punning idioms.
633   addPass(createTypeBasedAAWrapperPass());
634   addPass(createScopedNoAliasAAWrapperPass());
635   addPass(createBasicAAWrapperPass());
636 
637   // Before running any passes, run the verifier to determine if the input
638   // coming from the front-end and/or optimizer is valid.
639   if (!DisableVerify)
640     addPass(createVerifierPass());
641 
642   // Run loop strength reduction before anything else.
643   if (getOptLevel() != CodeGenOpt::None && !DisableLSR) {
644     addPass(createLoopStrengthReducePass());
645     if (PrintLSR)
646       addPass(createPrintFunctionPass(dbgs(), "\n\n*** Code after LSR ***\n"));
647   }
648 
649   if (getOptLevel() != CodeGenOpt::None) {
650     // The MergeICmpsPass tries to create memcmp calls by grouping sequences of
651     // loads and compares. ExpandMemCmpPass then tries to expand those calls
652     // into optimally-sized loads and compares. The transforms are enabled by a
653     // target lowering hook.
654     if (!DisableMergeICmps)
655       addPass(createMergeICmpsLegacyPass());
656     addPass(createExpandMemCmpPass());
657   }
658 
659   // Run GC lowering passes for builtin collectors
660   // TODO: add a pass insertion point here
661   addPass(createGCLoweringPass());
662   addPass(createShadowStackGCLoweringPass());
663   addPass(createLowerConstantIntrinsicsPass());
664 
665   // Make sure that no unreachable blocks are instruction selected.
666   addPass(createUnreachableBlockEliminationPass());
667 
668   // Prepare expensive constants for SelectionDAG.
669   if (getOptLevel() != CodeGenOpt::None && !DisableConstantHoisting)
670     addPass(createConstantHoistingPass());
671 
672   if (getOptLevel() != CodeGenOpt::None && !DisablePartialLibcallInlining)
673     addPass(createPartiallyInlineLibCallsPass());
674 
675   // Instrument function entry and exit, e.g. with calls to mcount().
676   addPass(createPostInlineEntryExitInstrumenterPass());
677 
678   // Add scalarization of target's unsupported masked memory intrinsics pass.
679   // the unsupported intrinsic will be replaced with a chain of basic blocks,
680   // that stores/loads element one-by-one if the appropriate mask bit is set.
681   addPass(createScalarizeMaskedMemIntrinPass());
682 
683   // Expand reduction intrinsics into shuffle sequences if the target wants to.
684   addPass(createExpandReductionsPass());
685 }
686 
687 /// Turn exception handling constructs into something the code generators can
688 /// handle.
689 void TargetPassConfig::addPassesToHandleExceptions() {
690   const MCAsmInfo *MCAI = TM->getMCAsmInfo();
691   assert(MCAI && "No MCAsmInfo");
692   switch (MCAI->getExceptionHandlingType()) {
693   case ExceptionHandling::SjLj:
694     // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both
695     // Dwarf EH prepare needs to be run after SjLj prepare. Otherwise,
696     // catch info can get misplaced when a selector ends up more than one block
697     // removed from the parent invoke(s). This could happen when a landing
698     // pad is shared by multiple invokes and is also a target of a normal
699     // edge from elsewhere.
700     addPass(createSjLjEHPreparePass(TM));
701     LLVM_FALLTHROUGH;
702   case ExceptionHandling::DwarfCFI:
703   case ExceptionHandling::ARM:
704     addPass(createDwarfEHPass());
705     break;
706   case ExceptionHandling::WinEH:
707     // We support using both GCC-style and MSVC-style exceptions on Windows, so
708     // add both preparation passes. Each pass will only actually run if it
709     // recognizes the personality function.
710     addPass(createWinEHPass());
711     addPass(createDwarfEHPass());
712     break;
713   case ExceptionHandling::Wasm:
714     // Wasm EH uses Windows EH instructions, but it does not need to demote PHIs
715     // on catchpads and cleanuppads because it does not outline them into
716     // funclets. Catchswitch blocks are not lowered in SelectionDAG, so we
717     // should remove PHIs there.
718     addPass(createWinEHPass(/*DemoteCatchSwitchPHIOnly=*/false));
719     addPass(createWasmEHPass());
720     break;
721   case ExceptionHandling::None:
722     addPass(createLowerInvokePass());
723 
724     // The lower invoke pass may create unreachable code. Remove it.
725     addPass(createUnreachableBlockEliminationPass());
726     break;
727   }
728 }
729 
730 /// Add pass to prepare the LLVM IR for code generation. This should be done
731 /// before exception handling preparation passes.
732 void TargetPassConfig::addCodeGenPrepare() {
733   if (getOptLevel() != CodeGenOpt::None && !DisableCGP)
734     addPass(createCodeGenPreparePass());
735   addPass(createRewriteSymbolsPass());
736 }
737 
738 /// Add common passes that perform LLVM IR to IR transforms in preparation for
739 /// instruction selection.
740 void TargetPassConfig::addISelPrepare() {
741   addPreISel();
742 
743   // Force codegen to run according to the callgraph.
744   if (requiresCodeGenSCCOrder())
745     addPass(new DummyCGSCCPass);
746 
747   // Add both the safe stack and the stack protection passes: each of them will
748   // only protect functions that have corresponding attributes.
749   addPass(createSafeStackPass());
750   addPass(createStackProtectorPass());
751 
752   if (PrintISelInput)
753     addPass(createPrintFunctionPass(
754         dbgs(), "\n\n*** Final LLVM Code input to ISel ***\n"));
755 
756   // All passes which modify the LLVM IR are now complete; run the verifier
757   // to ensure that the IR is valid.
758   if (!DisableVerify)
759     addPass(createVerifierPass());
760 }
761 
762 bool TargetPassConfig::addCoreISelPasses() {
763   // Enable FastISel with -fast-isel, but allow that to be overridden.
764   TM->setO0WantsFastISel(EnableFastISelOption != cl::BOU_FALSE);
765 
766   // Determine an instruction selector.
767   enum class SelectorType { SelectionDAG, FastISel, GlobalISel };
768   SelectorType Selector;
769 
770   if (EnableFastISelOption == cl::BOU_TRUE)
771     Selector = SelectorType::FastISel;
772   else if (EnableGlobalISelOption == cl::BOU_TRUE ||
773            (TM->Options.EnableGlobalISel &&
774             EnableGlobalISelOption != cl::BOU_FALSE))
775     Selector = SelectorType::GlobalISel;
776   else if (TM->getOptLevel() == CodeGenOpt::None && TM->getO0WantsFastISel())
777     Selector = SelectorType::FastISel;
778   else
779     Selector = SelectorType::SelectionDAG;
780 
781   // Set consistently TM->Options.EnableFastISel and EnableGlobalISel.
782   if (Selector == SelectorType::FastISel) {
783     TM->setFastISel(true);
784     TM->setGlobalISel(false);
785   } else if (Selector == SelectorType::GlobalISel) {
786     TM->setFastISel(false);
787     TM->setGlobalISel(true);
788   }
789 
790   // Add instruction selector passes.
791   if (Selector == SelectorType::GlobalISel) {
792     SaveAndRestore<bool> SavedAddingMachinePasses(AddingMachinePasses, true);
793     if (addIRTranslator())
794       return true;
795 
796     addPreLegalizeMachineIR();
797 
798     if (addLegalizeMachineIR())
799       return true;
800 
801     // Before running the register bank selector, ask the target if it
802     // wants to run some passes.
803     addPreRegBankSelect();
804 
805     if (addRegBankSelect())
806       return true;
807 
808     addPreGlobalInstructionSelect();
809 
810     if (addGlobalInstructionSelect())
811       return true;
812 
813     // Pass to reset the MachineFunction if the ISel failed.
814     addPass(createResetMachineFunctionPass(
815         reportDiagnosticWhenGlobalISelFallback(), isGlobalISelAbortEnabled()));
816 
817     // Provide a fallback path when we do not want to abort on
818     // not-yet-supported input.
819     if (!isGlobalISelAbortEnabled() && addInstSelector())
820       return true;
821 
822   } else if (addInstSelector())
823     return true;
824 
825   // Expand pseudo-instructions emitted by ISel. Don't run the verifier before
826   // FinalizeISel.
827   addPass(&FinalizeISelID);
828 
829   // Print the instruction selected machine code...
830   printAndVerify("After Instruction Selection");
831 
832   return false;
833 }
834 
835 bool TargetPassConfig::addISelPasses() {
836   if (TM->useEmulatedTLS())
837     addPass(createLowerEmuTLSPass());
838 
839   addPass(createPreISelIntrinsicLoweringPass());
840   addPass(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
841   addIRPasses();
842   addCodeGenPrepare();
843   addPassesToHandleExceptions();
844   addISelPrepare();
845 
846   return addCoreISelPasses();
847 }
848 
849 /// -regalloc=... command line option.
850 static FunctionPass *useDefaultRegisterAllocator() { return nullptr; }
851 static cl::opt<RegisterRegAlloc::FunctionPassCtor, false,
852                RegisterPassParser<RegisterRegAlloc>>
853     RegAlloc("regalloc", cl::Hidden, cl::init(&useDefaultRegisterAllocator),
854              cl::desc("Register allocator to use"));
855 
856 /// Add the complete set of target-independent postISel code generator passes.
857 ///
858 /// This can be read as the standard order of major LLVM CodeGen stages. Stages
859 /// with nontrivial configuration or multiple passes are broken out below in
860 /// add%Stage routines.
861 ///
862 /// Any TargetPassConfig::addXX routine may be overriden by the Target. The
863 /// addPre/Post methods with empty header implementations allow injecting
864 /// target-specific fixups just before or after major stages. Additionally,
865 /// targets have the flexibility to change pass order within a stage by
866 /// overriding default implementation of add%Stage routines below. Each
867 /// technique has maintainability tradeoffs because alternate pass orders are
868 /// not well supported. addPre/Post works better if the target pass is easily
869 /// tied to a common pass. But if it has subtle dependencies on multiple passes,
870 /// the target should override the stage instead.
871 ///
872 /// TODO: We could use a single addPre/Post(ID) hook to allow pass injection
873 /// before/after any target-independent pass. But it's currently overkill.
874 void TargetPassConfig::addMachinePasses() {
875   AddingMachinePasses = true;
876 
877   // Insert a machine instr printer pass after the specified pass.
878   StringRef PrintMachineInstrsPassName = PrintMachineInstrs.getValue();
879   if (!PrintMachineInstrsPassName.equals("") &&
880       !PrintMachineInstrsPassName.equals("option-unspecified")) {
881     if (const PassInfo *TPI = getPassInfo(PrintMachineInstrsPassName)) {
882       const PassRegistry *PR = PassRegistry::getPassRegistry();
883       const PassInfo *IPI = PR->getPassInfo(StringRef("machineinstr-printer"));
884       assert(IPI && "failed to get \"machineinstr-printer\" PassInfo!");
885       const char *TID = (const char *)(TPI->getTypeInfo());
886       const char *IID = (const char *)(IPI->getTypeInfo());
887       insertPass(TID, IID);
888     }
889   }
890 
891   // Add passes that optimize machine instructions in SSA form.
892   if (getOptLevel() != CodeGenOpt::None) {
893     addMachineSSAOptimization();
894   } else {
895     // If the target requests it, assign local variables to stack slots relative
896     // to one another and simplify frame index references where possible.
897     addPass(&LocalStackSlotAllocationID, false);
898   }
899 
900   if (TM->Options.EnableIPRA)
901     addPass(createRegUsageInfoPropPass());
902 
903   // Run pre-ra passes.
904   addPreRegAlloc();
905 
906   // Run register allocation and passes that are tightly coupled with it,
907   // including phi elimination and scheduling.
908   if (getOptimizeRegAlloc())
909     addOptimizedRegAlloc();
910   else
911     addFastRegAlloc();
912 
913   // Run post-ra passes.
914   addPostRegAlloc();
915 
916   if (UseRegistersForDeoptValues)
917     addPass(&FixupStatepointCallerSavedID);
918 
919   // Insert prolog/epilog code.  Eliminate abstract frame index references...
920   if (getOptLevel() != CodeGenOpt::None) {
921     addPass(&PostRAMachineSinkingID);
922     addPass(&ShrinkWrapID);
923   }
924 
925   // Prolog/Epilog inserter needs a TargetMachine to instantiate. But only
926   // do so if it hasn't been disabled, substituted, or overridden.
927   if (!isPassSubstitutedOrOverridden(&PrologEpilogCodeInserterID))
928       addPass(createPrologEpilogInserterPass());
929 
930   /// Add passes that optimize machine instructions after register allocation.
931   if (getOptLevel() != CodeGenOpt::None)
932     addMachineLateOptimization();
933 
934   // Expand pseudo instructions before second scheduling pass.
935   addPass(&ExpandPostRAPseudosID);
936 
937   // Run pre-sched2 passes.
938   addPreSched2();
939 
940   if (EnableImplicitNullChecks)
941     addPass(&ImplicitNullChecksID);
942 
943   // Second pass scheduler.
944   // Let Target optionally insert this pass by itself at some other
945   // point.
946   if (getOptLevel() != CodeGenOpt::None &&
947       !TM->targetSchedulesPostRAScheduling()) {
948     if (MISchedPostRA)
949       addPass(&PostMachineSchedulerID);
950     else
951       addPass(&PostRASchedulerID);
952   }
953 
954   // GC
955   if (addGCPasses()) {
956     if (PrintGCInfo)
957       addPass(createGCInfoPrinter(dbgs()), false, false);
958   }
959 
960   // Basic block placement.
961   if (getOptLevel() != CodeGenOpt::None)
962     addBlockPlacement();
963 
964   // Insert before XRay Instrumentation.
965   addPass(&FEntryInserterID, false);
966 
967   addPass(&XRayInstrumentationID, false);
968   addPass(&PatchableFunctionID, false);
969 
970   addPreEmitPass();
971 
972   if (TM->Options.EnableIPRA)
973     // Collect register usage information and produce a register mask of
974     // clobbered registers, to be used to optimize call sites.
975     addPass(createRegUsageInfoCollector());
976 
977   addPass(&FuncletLayoutID, false);
978 
979   addPass(&StackMapLivenessID, false);
980   addPass(&LiveDebugValuesID, false);
981 
982   if (TM->Options.EnableMachineOutliner && getOptLevel() != CodeGenOpt::None &&
983       EnableMachineOutliner != NeverOutline) {
984     bool RunOnAllFunctions = (EnableMachineOutliner == AlwaysOutline);
985     bool AddOutliner = RunOnAllFunctions ||
986                        TM->Options.SupportsDefaultOutlining;
987     if (AddOutliner)
988       addPass(createMachineOutlinerPass(RunOnAllFunctions));
989   }
990 
991   if (TM->getBBSectionsType() != llvm::BasicBlockSection::None)
992     addPass(llvm::createBBSectionsPreparePass(TM->getBBSectionsFuncListBuf()));
993 
994   // Add passes that directly emit MI after all other MI passes.
995   addPreEmitPass2();
996 
997   AddingMachinePasses = false;
998 }
999 
1000 /// Add passes that optimize machine instructions in SSA form.
1001 void TargetPassConfig::addMachineSSAOptimization() {
1002   // Pre-ra tail duplication.
1003   addPass(&EarlyTailDuplicateID);
1004 
1005   // Optimize PHIs before DCE: removing dead PHI cycles may make more
1006   // instructions dead.
1007   addPass(&OptimizePHIsID, false);
1008 
1009   // This pass merges large allocas. StackSlotColoring is a different pass
1010   // which merges spill slots.
1011   addPass(&StackColoringID, false);
1012 
1013   // If the target requests it, assign local variables to stack slots relative
1014   // to one another and simplify frame index references where possible.
1015   addPass(&LocalStackSlotAllocationID, false);
1016 
1017   // With optimization, dead code should already be eliminated. However
1018   // there is one known exception: lowered code for arguments that are only
1019   // used by tail calls, where the tail calls reuse the incoming stack
1020   // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll).
1021   addPass(&DeadMachineInstructionElimID);
1022 
1023   // Allow targets to insert passes that improve instruction level parallelism,
1024   // like if-conversion. Such passes will typically need dominator trees and
1025   // loop info, just like LICM and CSE below.
1026   addILPOpts();
1027 
1028   addPass(&EarlyMachineLICMID, false);
1029   addPass(&MachineCSEID, false);
1030 
1031   addPass(&MachineSinkingID);
1032 
1033   addPass(&PeepholeOptimizerID);
1034   // Clean-up the dead code that may have been generated by peephole
1035   // rewriting.
1036   addPass(&DeadMachineInstructionElimID);
1037 }
1038 
1039 //===---------------------------------------------------------------------===//
1040 /// Register Allocation Pass Configuration
1041 //===---------------------------------------------------------------------===//
1042 
1043 bool TargetPassConfig::getOptimizeRegAlloc() const {
1044   switch (OptimizeRegAlloc) {
1045   case cl::BOU_UNSET: return getOptLevel() != CodeGenOpt::None;
1046   case cl::BOU_TRUE:  return true;
1047   case cl::BOU_FALSE: return false;
1048   }
1049   llvm_unreachable("Invalid optimize-regalloc state");
1050 }
1051 
1052 /// A dummy default pass factory indicates whether the register allocator is
1053 /// overridden on the command line.
1054 static llvm::once_flag InitializeDefaultRegisterAllocatorFlag;
1055 
1056 static RegisterRegAlloc
1057 defaultRegAlloc("default",
1058                 "pick register allocator based on -O option",
1059                 useDefaultRegisterAllocator);
1060 
1061 static void initializeDefaultRegisterAllocatorOnce() {
1062   if (!RegisterRegAlloc::getDefault())
1063     RegisterRegAlloc::setDefault(RegAlloc);
1064 }
1065 
1066 /// Instantiate the default register allocator pass for this target for either
1067 /// the optimized or unoptimized allocation path. This will be added to the pass
1068 /// manager by addFastRegAlloc in the unoptimized case or addOptimizedRegAlloc
1069 /// in the optimized case.
1070 ///
1071 /// A target that uses the standard regalloc pass order for fast or optimized
1072 /// allocation may still override this for per-target regalloc
1073 /// selection. But -regalloc=... always takes precedence.
1074 FunctionPass *TargetPassConfig::createTargetRegisterAllocator(bool Optimized) {
1075   if (Optimized)
1076     return createGreedyRegisterAllocator();
1077   else
1078     return createFastRegisterAllocator();
1079 }
1080 
1081 /// Find and instantiate the register allocation pass requested by this target
1082 /// at the current optimization level.  Different register allocators are
1083 /// defined as separate passes because they may require different analysis.
1084 ///
1085 /// This helper ensures that the regalloc= option is always available,
1086 /// even for targets that override the default allocator.
1087 ///
1088 /// FIXME: When MachinePassRegistry register pass IDs instead of function ptrs,
1089 /// this can be folded into addPass.
1090 FunctionPass *TargetPassConfig::createRegAllocPass(bool Optimized) {
1091   // Initialize the global default.
1092   llvm::call_once(InitializeDefaultRegisterAllocatorFlag,
1093                   initializeDefaultRegisterAllocatorOnce);
1094 
1095   RegisterRegAlloc::FunctionPassCtor Ctor = RegisterRegAlloc::getDefault();
1096   if (Ctor != useDefaultRegisterAllocator)
1097     return Ctor();
1098 
1099   // With no -regalloc= override, ask the target for a regalloc pass.
1100   return createTargetRegisterAllocator(Optimized);
1101 }
1102 
1103 bool TargetPassConfig::addRegAssignmentFast() {
1104   if (RegAlloc != &useDefaultRegisterAllocator &&
1105       RegAlloc != &createFastRegisterAllocator)
1106     report_fatal_error("Must use fast (default) register allocator for unoptimized regalloc.");
1107 
1108   addPass(createRegAllocPass(false));
1109   return true;
1110 }
1111 
1112 bool TargetPassConfig::addRegAssignmentOptimized() {
1113   // Add the selected register allocation pass.
1114   addPass(createRegAllocPass(true));
1115 
1116   // Allow targets to change the register assignments before rewriting.
1117   addPreRewrite();
1118 
1119   // Finally rewrite virtual registers.
1120   addPass(&VirtRegRewriterID);
1121   // Perform stack slot coloring and post-ra machine LICM.
1122   //
1123   // FIXME: Re-enable coloring with register when it's capable of adding
1124   // kill markers.
1125   addPass(&StackSlotColoringID);
1126 
1127   return true;
1128 }
1129 
1130 /// Return true if the default global register allocator is in use and
1131 /// has not be overriden on the command line with '-regalloc=...'
1132 bool TargetPassConfig::usingDefaultRegAlloc() const {
1133   return RegAlloc.getNumOccurrences() == 0;
1134 }
1135 
1136 /// Add the minimum set of target-independent passes that are required for
1137 /// register allocation. No coalescing or scheduling.
1138 void TargetPassConfig::addFastRegAlloc() {
1139   addPass(&PHIEliminationID, false);
1140   addPass(&TwoAddressInstructionPassID, false);
1141 
1142   addRegAssignmentFast();
1143 }
1144 
1145 /// Add standard target-independent passes that are tightly coupled with
1146 /// optimized register allocation, including coalescing, machine instruction
1147 /// scheduling, and register allocation itself.
1148 void TargetPassConfig::addOptimizedRegAlloc() {
1149   addPass(&DetectDeadLanesID, false);
1150 
1151   addPass(&ProcessImplicitDefsID, false);
1152 
1153   // LiveVariables currently requires pure SSA form.
1154   //
1155   // FIXME: Once TwoAddressInstruction pass no longer uses kill flags,
1156   // LiveVariables can be removed completely, and LiveIntervals can be directly
1157   // computed. (We still either need to regenerate kill flags after regalloc, or
1158   // preferably fix the scavenger to not depend on them).
1159   addPass(&LiveVariablesID, false);
1160 
1161   // Edge splitting is smarter with machine loop info.
1162   addPass(&MachineLoopInfoID, false);
1163   addPass(&PHIEliminationID, false);
1164 
1165   // Eventually, we want to run LiveIntervals before PHI elimination.
1166   if (EarlyLiveIntervals)
1167     addPass(&LiveIntervalsID, false);
1168 
1169   addPass(&TwoAddressInstructionPassID, false);
1170   addPass(&RegisterCoalescerID);
1171 
1172   // The machine scheduler may accidentally create disconnected components
1173   // when moving subregister definitions around, avoid this by splitting them to
1174   // separate vregs before. Splitting can also improve reg. allocation quality.
1175   addPass(&RenameIndependentSubregsID);
1176 
1177   // PreRA instruction scheduling.
1178   addPass(&MachineSchedulerID);
1179 
1180   if (addRegAssignmentOptimized()) {
1181     // Allow targets to expand pseudo instructions depending on the choice of
1182     // registers before MachineCopyPropagation.
1183     addPostRewrite();
1184 
1185     // Copy propagate to forward register uses and try to eliminate COPYs that
1186     // were not coalesced.
1187     addPass(&MachineCopyPropagationID);
1188 
1189     // Run post-ra machine LICM to hoist reloads / remats.
1190     //
1191     // FIXME: can this move into MachineLateOptimization?
1192     addPass(&MachineLICMID);
1193   }
1194 }
1195 
1196 //===---------------------------------------------------------------------===//
1197 /// Post RegAlloc Pass Configuration
1198 //===---------------------------------------------------------------------===//
1199 
1200 /// Add passes that optimize machine instructions after register allocation.
1201 void TargetPassConfig::addMachineLateOptimization() {
1202   // Branch folding must be run after regalloc and prolog/epilog insertion.
1203   addPass(&BranchFolderPassID);
1204 
1205   // Tail duplication.
1206   // Note that duplicating tail just increases code size and degrades
1207   // performance for targets that require Structured Control Flow.
1208   // In addition it can also make CFG irreducible. Thus we disable it.
1209   if (!TM->requiresStructuredCFG())
1210     addPass(&TailDuplicateID);
1211 
1212   // Copy propagation.
1213   addPass(&MachineCopyPropagationID);
1214 }
1215 
1216 /// Add standard GC passes.
1217 bool TargetPassConfig::addGCPasses() {
1218   addPass(&GCMachineCodeAnalysisID, false);
1219   return true;
1220 }
1221 
1222 /// Add standard basic block placement passes.
1223 void TargetPassConfig::addBlockPlacement() {
1224   if (addPass(&MachineBlockPlacementID)) {
1225     // Run a separate pass to collect block placement statistics.
1226     if (EnableBlockPlacementStats)
1227       addPass(&MachineBlockPlacementStatsID);
1228   }
1229 }
1230 
1231 //===---------------------------------------------------------------------===//
1232 /// GlobalISel Configuration
1233 //===---------------------------------------------------------------------===//
1234 bool TargetPassConfig::isGlobalISelAbortEnabled() const {
1235   return TM->Options.GlobalISelAbort == GlobalISelAbortMode::Enable;
1236 }
1237 
1238 bool TargetPassConfig::reportDiagnosticWhenGlobalISelFallback() const {
1239   return TM->Options.GlobalISelAbort == GlobalISelAbortMode::DisableWithDiag;
1240 }
1241 
1242 bool TargetPassConfig::isGISelCSEEnabled() const {
1243   return true;
1244 }
1245 
1246 std::unique_ptr<CSEConfigBase> TargetPassConfig::getCSEConfig() const {
1247   return std::make_unique<CSEConfigBase>();
1248 }
1249