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