LLVM  10.0.0svn
PassBuilder.cpp
Go to the documentation of this file.
1 //===- Parsing, selection, and construction of pass pipelines -------------===//
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 /// \file
9 ///
10 /// This file provides the implementation of the PassBuilder based on our
11 /// static pass registry as well as related functionality. It also provides
12 /// helpers to aid in analyzing, debugging, and testing passes and pass
13 /// pipelines.
14 ///
15 //===----------------------------------------------------------------------===//
16 
18 #include "llvm/ADT/StringSwitch.h"
30 #include "llvm/Analysis/DDG.h"
35 #include "llvm/Analysis/IVUsers.h"
40 #include "llvm/Analysis/LoopInfo.h"
59 #include "llvm/IR/Dominators.h"
61 #include "llvm/IR/PassManager.h"
63 #include "llvm/IR/Verifier.h"
64 #include "llvm/Support/Debug.h"
66 #include "llvm/Support/Regex.h"
182 
183 using namespace llvm;
184 
185 static cl::opt<unsigned> MaxDevirtIterations("pm-max-devirt-iterations",
187 static cl::opt<bool>
188  RunPartialInlining("enable-npm-partial-inlining", cl::init(false),
190  cl::desc("Run Partial inlinining pass"));
191 
192 static cl::opt<bool>
193  RunNewGVN("enable-npm-newgvn", cl::init(false),
195  cl::desc("Run NewGVN instead of GVN"));
196 
198  "enable-npm-gvn-hoist", cl::init(false), cl::Hidden,
199  cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
200 
202  "enable-npm-gvn-sink", cl::init(false), cl::Hidden,
203  cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
204 
206  "enable-npm-unroll-and-jam", cl::init(false), cl::Hidden,
207  cl::desc("Enable the Unroll and Jam pass for the new PM (default = off)"));
208 
210  "enable-npm-synthetic-counts", cl::init(false), cl::Hidden, cl::ZeroOrMore,
211  cl::desc("Run synthetic function entry count generation "
212  "pass"));
213 
214 static const Regex DefaultAliasRegex(
215  "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$");
216 
217 // This option is used in simplifying testing SampleFDO optimizations for
218 // profile loading.
219 static cl::opt<bool>
220  EnableCHR("enable-chr-npm", cl::init(true), cl::Hidden,
221  cl::desc("Enable control height reduction optimization (CHR)"));
222 
227  LoopUnrolling = true;
231 }
232 
235 
237 
239  switch (Level) {
240  case PassBuilder::O0:
241  case PassBuilder::O1:
242  case PassBuilder::O2:
243  case PassBuilder::O3:
244  return false;
245 
246  case PassBuilder::Os:
247  case PassBuilder::Oz:
248  return true;
249  }
250  llvm_unreachable("Invalid optimization level!");
251 }
252 
253 namespace {
254 
255 /// No-op module pass which does nothing.
256 struct NoOpModulePass {
258  return PreservedAnalyses::all();
259  }
260  static StringRef name() { return "NoOpModulePass"; }
261 };
262 
263 /// No-op module analysis.
264 class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
266  static AnalysisKey Key;
267 
268 public:
269  struct Result {};
270  Result run(Module &, ModuleAnalysisManager &) { return Result(); }
271  static StringRef name() { return "NoOpModuleAnalysis"; }
272 };
273 
274 /// No-op CGSCC pass which does nothing.
275 struct NoOpCGSCCPass {
278  return PreservedAnalyses::all();
279  }
280  static StringRef name() { return "NoOpCGSCCPass"; }
281 };
282 
283 /// No-op CGSCC analysis.
284 class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
286  static AnalysisKey Key;
287 
288 public:
289  struct Result {};
291  return Result();
292  }
293  static StringRef name() { return "NoOpCGSCCAnalysis"; }
294 };
295 
296 /// No-op function pass which does nothing.
297 struct NoOpFunctionPass {
299  return PreservedAnalyses::all();
300  }
301  static StringRef name() { return "NoOpFunctionPass"; }
302 };
303 
304 /// No-op function analysis.
305 class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
307  static AnalysisKey Key;
308 
309 public:
310  struct Result {};
311  Result run(Function &, FunctionAnalysisManager &) { return Result(); }
312  static StringRef name() { return "NoOpFunctionAnalysis"; }
313 };
314 
315 /// No-op loop pass which does nothing.
316 struct NoOpLoopPass {
319  return PreservedAnalyses::all();
320  }
321  static StringRef name() { return "NoOpLoopPass"; }
322 };
323 
324 /// No-op loop analysis.
325 class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
327  static AnalysisKey Key;
328 
329 public:
330  struct Result {};
332  return Result();
333  }
334  static StringRef name() { return "NoOpLoopAnalysis"; }
335 };
336 
341 
342 } // End anonymous namespace.
343 
344 void PassBuilder::invokePeepholeEPCallbacks(
346  for (auto &C : PeepholeEPCallbacks)
347  C(FPM, Level);
348 }
349 
351 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
352  MAM.registerPass([&] { return CREATE_PASS; });
353 #include "PassRegistry.def"
354 
355  for (auto &C : ModuleAnalysisRegistrationCallbacks)
356  C(MAM);
357 }
358 
360 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
361  CGAM.registerPass([&] { return CREATE_PASS; });
362 #include "PassRegistry.def"
363 
364  for (auto &C : CGSCCAnalysisRegistrationCallbacks)
365  C(CGAM);
366 }
367 
369 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
370  FAM.registerPass([&] { return CREATE_PASS; });
371 #include "PassRegistry.def"
372 
373  for (auto &C : FunctionAnalysisRegistrationCallbacks)
374  C(FAM);
375 }
376 
378 #define LOOP_ANALYSIS(NAME, CREATE_PASS) \
379  LAM.registerPass([&] { return CREATE_PASS; });
380 #include "PassRegistry.def"
381 
382  for (auto &C : LoopAnalysisRegistrationCallbacks)
383  C(LAM);
384 }
385 
389  bool DebugLogging) {
390  assert(Level != O0 && "Must request optimizations!");
391  FunctionPassManager FPM(DebugLogging);
392 
393  // Form SSA out of local memory accesses after breaking apart aggregates into
394  // scalars.
395  FPM.addPass(SROA());
396 
397  // Catch trivial redundancies
398  FPM.addPass(EarlyCSEPass(true /* Enable mem-ssa. */));
399 
400  // Hoisting of scalars and load expressions.
401  if (EnableGVNHoist)
402  FPM.addPass(GVNHoistPass());
403 
404  // Global value numbering based sinking.
405  if (EnableGVNSink) {
406  FPM.addPass(GVNSinkPass());
407  FPM.addPass(SimplifyCFGPass());
408  }
409 
410  // Speculative execution if the target has divergent branches; otherwise nop.
412 
413  // Optimize based on known information about branches, and cleanup afterward.
414  FPM.addPass(JumpThreadingPass());
416  FPM.addPass(SimplifyCFGPass());
417  if (Level == O3)
419  FPM.addPass(InstCombinePass());
420 
421  if (!isOptimizingForSize(Level))
423 
424  invokePeepholeEPCallbacks(FPM, Level);
425 
426  // For PGO use pipeline, try to optimize memory intrinsics such as memcpy
427  // using the size value profile. Don't perform this when optimizing for size.
428  if (PGOOpt && PGOOpt->Action == PGOOptions::IRUse &&
429  !isOptimizingForSize(Level))
430  FPM.addPass(PGOMemOPSizeOpt());
431 
432  FPM.addPass(TailCallElimPass());
433  FPM.addPass(SimplifyCFGPass());
434 
435  // Form canonically associated expression trees, and simplify the trees using
436  // basic mathematical properties. For example, this will form (nearly)
437  // minimal multiplication trees.
438  FPM.addPass(ReassociatePass());
439 
440  // Add the primary loop simplification pipeline.
441  // FIXME: Currently this is split into two loop pass pipelines because we run
442  // some function passes in between them. These can and should be removed
443  // and/or replaced by scheduling the loop pass equivalents in the correct
444  // positions. But those equivalent passes aren't powerful enough yet.
445  // Specifically, `SimplifyCFGPass` and `InstCombinePass` are currently still
446  // used. We have `LoopSimplifyCFGPass` which isn't yet powerful enough yet to
447  // fully replace `SimplifyCFGPass`, and the closest to the other we have is
448  // `LoopInstSimplify`.
449  LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
450 
451  // Simplify the loop body. We do this initially to clean up after other loop
452  // passes run, either when iterating on a loop or on inner loops with
453  // implications on the outer loop.
454  LPM1.addPass(LoopInstSimplifyPass());
455  LPM1.addPass(LoopSimplifyCFGPass());
456 
457  // Rotate Loop - disable header duplication at -Oz
458  LPM1.addPass(LoopRotatePass(Level != Oz));
459  LPM1.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap));
460  LPM1.addPass(SimpleLoopUnswitchPass());
461  LPM2.addPass(IndVarSimplifyPass());
463 
464  for (auto &C : LateLoopOptimizationsEPCallbacks)
465  C(LPM2, Level);
466 
467  LPM2.addPass(LoopDeletionPass());
468  // Do not enable unrolling in PreLinkThinLTO phase during sample PGO
469  // because it changes IR to makes profile annotation in back compile
470  // inaccurate.
471  if ((Phase != ThinLTOPhase::PreLink || !PGOOpt ||
472  PGOOpt->Action != PGOOptions::SampleUse) &&
473  PTO.LoopUnrolling)
474  LPM2.addPass(LoopFullUnrollPass(Level, /*OnlyWhenForced=*/false,
475  PTO.ForgetAllSCEVInLoopUnroll));
476 
477  for (auto &C : LoopOptimizerEndEPCallbacks)
478  C(LPM2, Level);
479 
480  // We provide the opt remark emitter pass for LICM to use. We only need to do
481  // this once as it is immutable.
484  std::move(LPM1), EnableMSSALoopDependency, DebugLogging));
485  FPM.addPass(SimplifyCFGPass());
486  FPM.addPass(InstCombinePass());
487  // The loop passes in LPM2 (IndVarSimplifyPass, LoopIdiomRecognizePass,
488  // LoopDeletionPass and LoopFullUnrollPass) do not preserve MemorySSA.
489  // *All* loop passes must preserve it, in order to be able to use it.
491  std::move(LPM2), /*UseMemorySSA=*/false, DebugLogging));
492 
493  // Eliminate redundancies.
494  if (Level != O1) {
495  // These passes add substantial compile time so skip them at O1.
497  if (RunNewGVN)
498  FPM.addPass(NewGVNPass());
499  else
500  FPM.addPass(GVN());
501  }
502 
503  // Specially optimize memory movement as it doesn't look like dataflow in SSA.
504  FPM.addPass(MemCpyOptPass());
505 
506  // Sparse conditional constant propagation.
507  // FIXME: It isn't clear why we do this *after* loop passes rather than
508  // before...
509  FPM.addPass(SCCPPass());
510 
511  // Delete dead bit computations (instcombine runs after to fold away the dead
512  // computations, and then ADCE will run later to exploit any new DCE
513  // opportunities that creates).
514  FPM.addPass(BDCEPass());
515 
516  // Run instcombine after redundancy and dead bit elimination to exploit
517  // opportunities opened up by them.
518  FPM.addPass(InstCombinePass());
519  invokePeepholeEPCallbacks(FPM, Level);
520 
521  // Re-consider control flow based optimizations after redundancy elimination,
522  // redo DCE, etc.
523  FPM.addPass(JumpThreadingPass());
525  FPM.addPass(DSEPass());
527  LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap),
528  EnableMSSALoopDependency, DebugLogging));
529 
530  for (auto &C : ScalarOptimizerLateEPCallbacks)
531  C(FPM, Level);
532 
533  // Finally, do an expensive DCE pass to catch all the dead code exposed by
534  // the simplifications and basic cleanup after all the simplifications.
535  FPM.addPass(ADCEPass());
536  FPM.addPass(SimplifyCFGPass());
537  FPM.addPass(InstCombinePass());
538  invokePeepholeEPCallbacks(FPM, Level);
539 
540  if (EnableCHR && Level == O3 && PGOOpt &&
541  (PGOOpt->Action == PGOOptions::IRUse ||
542  PGOOpt->Action == PGOOptions::SampleUse))
544 
545  return FPM;
546 }
547 
548 void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
550  bool RunProfileGen, bool IsCS,
551  std::string ProfileFile,
552  std::string ProfileRemappingFile) {
553  assert(Level != O0 && "Not expecting O0 here!");
554  // Generally running simplification passes and the inliner with an high
555  // threshold results in smaller executables, but there may be cases where
556  // the size grows, so let's be conservative here and skip this simplification
557  // at -Os/Oz. We will not do this inline for context sensistive PGO (when
558  // IsCS is true).
559  if (!isOptimizingForSize(Level) && !IsCS) {
560  InlineParams IP;
561 
562  // In the old pass manager, this is a cl::opt. Should still this be one?
563  IP.DefaultThreshold = 75;
564 
565  // FIXME: The hint threshold has the same value used by the regular inliner.
566  // This should probably be lowered after performance testing.
567  // FIXME: this comment is cargo culted from the old pass manager, revisit).
568  IP.HintThreshold = 325;
569 
570  CGSCCPassManager CGPipeline(DebugLogging);
571 
572  CGPipeline.addPass(InlinerPass(IP));
573 
575  FPM.addPass(SROA());
576  FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies.
577  FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
578  FPM.addPass(InstCombinePass()); // Combine silly sequences.
579  invokePeepholeEPCallbacks(FPM, Level);
580 
581  CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
582 
583  MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
584 
585  // Delete anything that is now dead to make sure that we don't instrument
586  // dead code. Instrumentation can end up keeping dead code around and
587  // dramatically increase code size.
588  MPM.addPass(GlobalDCEPass());
589  }
590 
591  if (!RunProfileGen) {
592  assert(!ProfileFile.empty() && "Profile use expecting a profile file!");
593  MPM.addPass(PGOInstrumentationUse(ProfileFile, ProfileRemappingFile, IsCS));
594  // Cache ProfileSummaryAnalysis once to avoid the potential need to insert
595  // RequireAnalysisPass for PSI before subsequent non-module passes.
597  return;
598  }
599 
600  // Perform PGO instrumentation.
601  MPM.addPass(PGOInstrumentationGen(IsCS));
602 
605  LoopRotatePass(), EnableMSSALoopDependency, DebugLogging));
606  MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
607 
608  // Add the profile lowering pass.
609  InstrProfOptions Options;
610  if (!ProfileFile.empty())
611  Options.InstrProfileOutput = ProfileFile;
612  // Do counter promotion at Level greater than O0.
613  Options.DoCounterPromotion = true;
614  Options.UseBFIInPromotion = IsCS;
615  MPM.addPass(InstrProfiling(Options, IsCS));
616 }
617 
619  bool DebugLogging, bool RunProfileGen,
620  bool IsCS, std::string ProfileFile,
621  std::string ProfileRemappingFile) {
622  if (!RunProfileGen) {
623  assert(!ProfileFile.empty() && "Profile use expecting a profile file!");
624  MPM.addPass(PGOInstrumentationUse(ProfileFile, ProfileRemappingFile, IsCS));
625  // Cache ProfileSummaryAnalysis once to avoid the potential need to insert
626  // RequireAnalysisPass for PSI before subsequent non-module passes.
628  return;
629  }
630 
631  // Perform PGO instrumentation.
632  MPM.addPass(PGOInstrumentationGen(IsCS));
633  // Add the profile lowering pass.
634  InstrProfOptions Options;
635  if (!ProfileFile.empty())
636  Options.InstrProfileOutput = ProfileFile;
637  // Do not do counter promotion at O0.
638  Options.DoCounterPromotion = false;
639  Options.UseBFIInPromotion = IsCS;
640  MPM.addPass(InstrProfiling(Options, IsCS));
641 }
642 
643 static InlineParams
645  auto O3 = PassBuilder::O3;
646  unsigned OptLevel = Level > O3 ? 2 : Level;
647  unsigned SizeLevel = Level > O3 ? Level - O3 : 0;
648  return getInlineParams(OptLevel, SizeLevel);
649 }
650 
654  bool DebugLogging) {
655  ModulePassManager MPM(DebugLogging);
656 
657  bool HasSampleProfile = PGOOpt && (PGOOpt->Action == PGOOptions::SampleUse);
658 
659  // In ThinLTO mode, when flattened profile is used, all the available
660  // profile information will be annotated in PreLink phase so there is
661  // no need to load the profile again in PostLink.
662  bool LoadSampleProfile =
663  HasSampleProfile &&
664  !(FlattenedProfileUsed && Phase == ThinLTOPhase::PostLink);
665 
666  // During the ThinLTO backend phase we perform early indirect call promotion
667  // here, before globalopt. Otherwise imported available_externally functions
668  // look unreferenced and are removed. If we are going to load the sample
669  // profile then defer until later.
670  // TODO: See if we can move later and consolidate with the location where
671  // we perform ICP when we are loading a sample profile.
672  // TODO: We pass HasSampleProfile (whether there was a sample profile file
673  // passed to the compile) to the SamplePGO flag of ICP. This is used to
674  // determine whether the new direct calls are annotated with prof metadata.
675  // Ideally this should be determined from whether the IR is annotated with
676  // sample profile, and not whether the a sample profile was provided on the
677  // command line. E.g. for flattened profiles where we will not be reloading
678  // the sample profile in the ThinLTO backend, we ideally shouldn't have to
679  // provide the sample profile file.
680  if (Phase == ThinLTOPhase::PostLink && !LoadSampleProfile)
681  MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */, HasSampleProfile));
682 
683  // Do basic inference of function attributes from known properties of system
684  // libraries and other oracles.
686 
687  // Create an early function pass manager to cleanup the output of the
688  // frontend.
689  FunctionPassManager EarlyFPM(DebugLogging);
690  EarlyFPM.addPass(SimplifyCFGPass());
691  EarlyFPM.addPass(SROA());
692  EarlyFPM.addPass(EarlyCSEPass());
693  EarlyFPM.addPass(LowerExpectIntrinsicPass());
694  if (Level == O3)
695  EarlyFPM.addPass(CallSiteSplittingPass());
696 
697  // In SamplePGO ThinLTO backend, we need instcombine before profile annotation
698  // to convert bitcast to direct calls so that they can be inlined during the
699  // profile annotation prepration step.
700  // More details about SamplePGO design can be found in:
701  // https://research.google.com/pubs/pub45290.html
702  // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured.
703  if (LoadSampleProfile)
704  EarlyFPM.addPass(InstCombinePass());
705  MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
706 
707  if (LoadSampleProfile) {
708  // Annotate sample profile right after early FPM to ensure freshness of
709  // the debug info.
710  MPM.addPass(SampleProfileLoaderPass(PGOOpt->ProfileFile,
711  PGOOpt->ProfileRemappingFile,
712  Phase == ThinLTOPhase::PreLink));
713  // Cache ProfileSummaryAnalysis once to avoid the potential need to insert
714  // RequireAnalysisPass for PSI before subsequent non-module passes.
716  // Do not invoke ICP in the ThinLTOPrelink phase as it makes it hard
717  // for the profile annotation to be accurate in the ThinLTO backend.
718  if (Phase != ThinLTOPhase::PreLink)
719  // We perform early indirect call promotion here, before globalopt.
720  // This is important for the ThinLTO backend phase because otherwise
721  // imported available_externally functions look unreferenced and are
722  // removed.
723  MPM.addPass(PGOIndirectCallPromotion(Phase == ThinLTOPhase::PostLink,
724  true /* SamplePGO */));
725  }
726 
727  // Interprocedural constant propagation now that basic cleanup has occurred
728  // and prior to optimizing globals.
729  // FIXME: This position in the pipeline hasn't been carefully considered in
730  // years, it should be re-analyzed.
731  MPM.addPass(IPSCCPPass());
732 
733  // Attach metadata to indirect call sites indicating the set of functions
734  // they may target at run-time. This should follow IPSCCP.
736 
737  // Optimize globals to try and fold them into constants.
738  MPM.addPass(GlobalOptPass());
739 
740  // Promote any localized globals to SSA registers.
741  // FIXME: Should this instead by a run of SROA?
742  // FIXME: We should probably run instcombine and simplify-cfg afterward to
743  // delete control flows that are dead once globals have been folded to
744  // constants.
746 
747  // Remove any dead arguments exposed by cleanups and constand folding
748  // globals.
750 
751  // Create a small function pass pipeline to cleanup after all the global
752  // optimizations.
753  FunctionPassManager GlobalCleanupPM(DebugLogging);
754  GlobalCleanupPM.addPass(InstCombinePass());
755  invokePeepholeEPCallbacks(GlobalCleanupPM, Level);
756 
757  GlobalCleanupPM.addPass(SimplifyCFGPass());
758  MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
759 
760  // Add all the requested passes for instrumentation PGO, if requested.
761  if (PGOOpt && Phase != ThinLTOPhase::PostLink &&
762  (PGOOpt->Action == PGOOptions::IRInstr ||
763  PGOOpt->Action == PGOOptions::IRUse)) {
764  addPGOInstrPasses(MPM, DebugLogging, Level,
765  /* RunProfileGen */ PGOOpt->Action == PGOOptions::IRInstr,
766  /* IsCS */ false, PGOOpt->ProfileFile,
767  PGOOpt->ProfileRemappingFile);
768  MPM.addPass(PGOIndirectCallPromotion(false, false));
769  }
770  if (PGOOpt && Phase != ThinLTOPhase::PostLink &&
771  PGOOpt->CSAction == PGOOptions::CSIRInstr)
772  MPM.addPass(PGOInstrumentationGenCreateVar(PGOOpt->CSProfileGenFile));
773 
774  // Synthesize function entry counts for non-PGO compilation.
775  if (EnableSyntheticCounts && !PGOOpt)
777 
778  // Require the GlobalsAA analysis for the module so we can query it within
779  // the CGSCC pipeline.
781 
782  // Require the ProfileSummaryAnalysis for the module so we can query it within
783  // the inliner pass.
785 
786  // Now begin the main postorder CGSCC pipeline.
787  // FIXME: The current CGSCC pipeline has its origins in the legacy pass
788  // manager and trying to emulate its precise behavior. Much of this doesn't
789  // make a lot of sense and we should revisit the core CGSCC structure.
790  CGSCCPassManager MainCGPipeline(DebugLogging);
791 
792  // Note: historically, the PruneEH pass was run first to deduce nounwind and
793  // generally clean up exception handling overhead. It isn't clear this is
794  // valuable as the inliner doesn't currently care whether it is inlining an
795  // invoke or a call.
796 
797  // Run the inliner first. The theory is that we are walking bottom-up and so
798  // the callees have already been fully optimized, and we want to inline them
799  // into the callers so that our optimizations can reflect that.
800  // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO
801  // because it makes profile annotation in the backend inaccurate.
803  if (Phase == ThinLTOPhase::PreLink && PGOOpt &&
804  PGOOpt->Action == PGOOptions::SampleUse)
805  IP.HotCallSiteThreshold = 0;
806  MainCGPipeline.addPass(InlinerPass(IP));
807 
808  // Now deduce any function attributes based in the current code.
809  MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
810 
811  // When at O3 add argument promotion to the pass pipeline.
812  // FIXME: It isn't at all clear why this should be limited to O3.
813  if (Level == O3)
814  MainCGPipeline.addPass(ArgumentPromotionPass());
815 
816  // Lastly, add the core function simplification pipeline nested inside the
817  // CGSCC walk.
819  buildFunctionSimplificationPipeline(Level, Phase, DebugLogging)));
820 
821  for (auto &C : CGSCCOptimizerLateEPCallbacks)
822  C(MainCGPipeline, Level);
823 
824  // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
825  // to detect when we devirtualize indirect calls and iterate the SCC passes
826  // in that case to try and catch knock-on inlining or function attrs
827  // opportunities. Then we add it to the module pipeline by walking the SCCs
828  // in postorder (or bottom-up).
829  MPM.addPass(
831  std::move(MainCGPipeline), MaxDevirtIterations)));
832 
833  return MPM;
834 }
835 
837  OptimizationLevel Level, bool DebugLogging, bool LTOPreLink) {
838  ModulePassManager MPM(DebugLogging);
839 
840  // Optimize globals now that the module is fully simplified.
841  MPM.addPass(GlobalOptPass());
842  MPM.addPass(GlobalDCEPass());
843 
844  // Run partial inlining pass to partially inline functions that have
845  // large bodies.
846  if (RunPartialInlining)
848 
849  // Remove avail extern fns and globals definitions since we aren't compiling
850  // an object file for later LTO. For LTO we want to preserve these so they
851  // are eligible for inlining at link-time. Note if they are unreferenced they
852  // will be removed by GlobalDCE later, so this only impacts referenced
853  // available externally globals. Eventually they will be suppressed during
854  // codegen, but eliminating here enables more opportunity for GlobalDCE as it
855  // may make globals referenced by available external functions dead and saves
856  // running remaining passes on the eliminated functions. These should be
857  // preserved during prelinking for link-time inlining decisions.
858  if (!LTOPreLink)
860 
861  if (EnableOrderFileInstrumentation)
863 
864  // Do RPO function attribute inference across the module to forward-propagate
865  // attributes where applicable.
866  // FIXME: Is this really an optimization rather than a canonicalization?
868 
869  // Do a post inline PGO instrumentation and use pass. This is a context
870  // sensitive PGO pass. We don't want to do this in LTOPreLink phrase as
871  // cross-module inline has not been done yet. The context sensitive
872  // instrumentation is after all the inlines are done.
873  if (!LTOPreLink && PGOOpt) {
874  if (PGOOpt->CSAction == PGOOptions::CSIRInstr)
875  addPGOInstrPasses(MPM, DebugLogging, Level, /* RunProfileGen */ true,
876  /* IsCS */ true, PGOOpt->CSProfileGenFile,
877  PGOOpt->ProfileRemappingFile);
878  else if (PGOOpt->CSAction == PGOOptions::CSIRUse)
879  addPGOInstrPasses(MPM, DebugLogging, Level, /* RunProfileGen */ false,
880  /* IsCS */ true, PGOOpt->ProfileFile,
881  PGOOpt->ProfileRemappingFile);
882  }
883 
884  // Re-require GloblasAA here prior to function passes. This is particularly
885  // useful as the above will have inlined, DCE'ed, and function-attr
886  // propagated everything. We should at this point have a reasonably minimal
887  // and richly annotated call graph. By computing aliasing and mod/ref
888  // information for all local globals here, the late loop passes and notably
889  // the vectorizer will be able to use them to help recognize vectorizable
890  // memory operations.
892 
893  FunctionPassManager OptimizePM(DebugLogging);
894  OptimizePM.addPass(Float2IntPass());
895  OptimizePM.addPass(LowerConstantIntrinsicsPass());
896 
897  // FIXME: We need to run some loop optimizations to re-rotate loops after
898  // simplify-cfg and others undo their rotation.
899 
900  // Optimize the loop execution. These passes operate on entire loop nests
901  // rather than on each loop in an inside-out manner, and so they are actually
902  // function passes.
903 
904  for (auto &C : VectorizerStartEPCallbacks)
905  C(OptimizePM, Level);
906 
907  // First rotate loops that may have been un-rotated by prior passes.
909  LoopRotatePass(), EnableMSSALoopDependency, DebugLogging));
910 
911  // Distribute loops to allow partial vectorization. I.e. isolate dependences
912  // into separate loop that would otherwise inhibit vectorization. This is
913  // currently only performed for loops marked with the metadata
914  // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
915  OptimizePM.addPass(LoopDistributePass());
916 
917  // Now run the core loop vectorizer.
918  OptimizePM.addPass(LoopVectorizePass(
919  LoopVectorizeOptions(!PTO.LoopInterleaving, !PTO.LoopVectorization)));
920 
921  // Eliminate loads by forwarding stores from the previous iteration to loads
922  // of the current iteration.
923  OptimizePM.addPass(LoopLoadEliminationPass());
924 
925  // Cleanup after the loop optimization passes.
926  OptimizePM.addPass(InstCombinePass());
927 
928  // Now that we've formed fast to execute loop structures, we do further
929  // optimizations. These are run afterward as they might block doing complex
930  // analyses and transforms such as what are needed for loop vectorization.
931 
932  // Cleanup after loop vectorization, etc. Simplification passes like CVP and
933  // GVN, loop transforms, and others have already run, so it's now better to
934  // convert to more optimized IR using more aggressive simplify CFG options.
935  // The extra sinking transform can create larger basic blocks, so do this
936  // before SLP vectorization.
938  forwardSwitchCondToPhi(true).
939  convertSwitchToLookupTable(true).
940  needCanonicalLoops(false).
941  sinkCommonInsts(true)));
942 
943  // Optimize parallel scalar instruction chains into SIMD instructions.
944  if (PTO.SLPVectorization)
945  OptimizePM.addPass(SLPVectorizerPass());
946 
947  OptimizePM.addPass(InstCombinePass());
948 
949  // Unroll small loops to hide loop backedge latency and saturate any parallel
950  // execution resources of an out-of-order processor. We also then need to
951  // clean up redundancies and loop invariant code.
952  // FIXME: It would be really good to use a loop-integrated instruction
953  // combiner for cleanup here so that the unrolling and LICM can be pipelined
954  // across the loop nests.
955  // We do UnrollAndJam in a separate LPM to ensure it happens before unroll
956  if (EnableUnrollAndJam && PTO.LoopUnrolling) {
957  OptimizePM.addPass(
959  }
960  OptimizePM.addPass(LoopUnrollPass(
961  LoopUnrollOptions(Level, /*OnlyWhenForced=*/!PTO.LoopUnrolling,
962  PTO.ForgetAllSCEVInLoopUnroll)));
964  OptimizePM.addPass(InstCombinePass());
967  LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap),
968  EnableMSSALoopDependency, DebugLogging));
969 
970  // Now that we've vectorized and unrolled loops, we may have more refined
971  // alignment information, try to re-derive it here.
973 
974  // Split out cold code. Splitting is done late to avoid hiding context from
975  // other optimizations and inadvertently regressing performance. The tradeoff
976  // is that this has a higher code size cost than splitting early.
977  if (EnableHotColdSplit && !LTOPreLink)
979 
980  // LoopSink pass sinks instructions hoisted by LICM, which serves as a
981  // canonicalization pass that enables other optimizations. As a result,
982  // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
983  // result too early.
984  OptimizePM.addPass(LoopSinkPass());
985 
986  // And finally clean up LCSSA form before generating code.
987  OptimizePM.addPass(InstSimplifyPass());
988 
989  // This hoists/decomposes div/rem ops. It should run after other sink/hoist
990  // passes to avoid re-sinking, but before SimplifyCFG because it can allow
991  // flattening of blocks.
992  OptimizePM.addPass(DivRemPairsPass());
993 
994  // LoopSink (and other loop passes since the last simplifyCFG) might have
995  // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
996  OptimizePM.addPass(SimplifyCFGPass());
997 
998  // Optimize PHIs by speculating around them when profitable. Note that this
999  // pass needs to be run after any PRE or similar pass as it is essentially
1000  // inserting redundancies into the program. This even includes SimplifyCFG.
1001  OptimizePM.addPass(SpeculateAroundPHIsPass());
1002 
1003  for (auto &C : OptimizerLastEPCallbacks)
1004  C(OptimizePM, Level);
1005 
1006  // Add the core optimizing pipeline.
1007  MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
1008 
1009  MPM.addPass(CGProfilePass());
1010 
1011  // Now we need to do some global optimization transforms.
1012  // FIXME: It would seem like these should come first in the optimization
1013  // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
1014  // ordering here.
1015  MPM.addPass(GlobalDCEPass());
1016  MPM.addPass(ConstantMergePass());
1017 
1018  return MPM;
1019 }
1020 
1023  bool DebugLogging, bool LTOPreLink) {
1024  assert(Level != O0 && "Must request optimizations for the default pipeline!");
1025 
1026  ModulePassManager MPM(DebugLogging);
1027 
1028  // Force any function attributes we want the rest of the pipeline to observe.
1030 
1031  // Apply module pipeline start EP callback.
1032  for (auto &C : PipelineStartEPCallbacks)
1033  C(MPM);
1034 
1035  if (PGOOpt && PGOOpt->SamplePGOSupport)
1037 
1038  // Add the core simplification pipeline.
1039  MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::None,
1040  DebugLogging));
1041 
1042  // Now add the optimization pipeline.
1043  MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging, LTOPreLink));
1044 
1045  return MPM;
1046 }
1047 
1050  bool DebugLogging) {
1051  assert(Level != O0 && "Must request optimizations for the default pipeline!");
1052 
1053  ModulePassManager MPM(DebugLogging);
1054 
1055  // Force any function attributes we want the rest of the pipeline to observe.
1057 
1058  if (PGOOpt && PGOOpt->SamplePGOSupport)
1060 
1061  // Apply module pipeline start EP callback.
1062  for (auto &C : PipelineStartEPCallbacks)
1063  C(MPM);
1064 
1065  // If we are planning to perform ThinLTO later, we don't bloat the code with
1066  // unrolling/vectorization/... now. Just simplify the module as much as we
1067  // can.
1068  MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PreLink,
1069  DebugLogging));
1070 
1071  // Run partial inlining pass to partially inline functions that have
1072  // large bodies.
1073  // FIXME: It isn't clear whether this is really the right place to run this
1074  // in ThinLTO. Because there is another canonicalization and simplification
1075  // phase that will run after the thin link, running this here ends up with
1076  // less information than will be available later and it may grow functions in
1077  // ways that aren't beneficial.
1078  if (RunPartialInlining)
1079  MPM.addPass(PartialInlinerPass());
1080 
1081  // Reduce the size of the IR as much as possible.
1082  MPM.addPass(GlobalOptPass());
1083 
1084  return MPM;
1085 }
1086 
1088  OptimizationLevel Level, bool DebugLogging,
1089  const ModuleSummaryIndex *ImportSummary) {
1090  ModulePassManager MPM(DebugLogging);
1091 
1092  if (ImportSummary) {
1093  // These passes import type identifier resolutions for whole-program
1094  // devirtualization and CFI. They must run early because other passes may
1095  // disturb the specific instruction patterns that these passes look for,
1096  // creating dependencies on resolutions that may not appear in the summary.
1097  //
1098  // For example, GVN may transform the pattern assume(type.test) appearing in
1099  // two basic blocks into assume(phi(type.test, type.test)), which would
1100  // transform a dependency on a WPD resolution into a dependency on a type
1101  // identifier resolution for CFI.
1102  //
1103  // Also, WPD has access to more precise information than ICP and can
1104  // devirtualize more effectively, so it should operate on the IR first.
1105  //
1106  // The WPD and LowerTypeTest passes need to run at -O0 to lower type
1107  // metadata and intrinsics.
1108  MPM.addPass(WholeProgramDevirtPass(nullptr, ImportSummary));
1109  MPM.addPass(LowerTypeTestsPass(nullptr, ImportSummary));
1110  }
1111 
1112  if (Level == O0)
1113  return MPM;
1114 
1115  // Force any function attributes we want the rest of the pipeline to observe.
1117 
1118  // Add the core simplification pipeline.
1119  MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PostLink,
1120  DebugLogging));
1121 
1122  // Now add the optimization pipeline.
1123  MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
1124 
1125  return MPM;
1126 }
1127 
1130  bool DebugLogging) {
1131  assert(Level != O0 && "Must request optimizations for the default pipeline!");
1132  // FIXME: We should use a customized pre-link pipeline!
1133  return buildPerModuleDefaultPipeline(Level, DebugLogging,
1134  /* LTOPreLink */true);
1135 }
1136 
1139  ModuleSummaryIndex *ExportSummary) {
1140  ModulePassManager MPM(DebugLogging);
1141 
1142  if (Level == O0) {
1143  // The WPD and LowerTypeTest passes need to run at -O0 to lower type
1144  // metadata and intrinsics.
1145  MPM.addPass(WholeProgramDevirtPass(ExportSummary, nullptr));
1146  MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr));
1147  return MPM;
1148  }
1149 
1150  if (PGOOpt && PGOOpt->Action == PGOOptions::SampleUse) {
1151  // Load sample profile before running the LTO optimization pipeline.
1152  MPM.addPass(SampleProfileLoaderPass(PGOOpt->ProfileFile,
1153  PGOOpt->ProfileRemappingFile,
1154  false /* ThinLTOPhase::PreLink */));
1155  // Cache ProfileSummaryAnalysis once to avoid the potential need to insert
1156  // RequireAnalysisPass for PSI before subsequent non-module passes.
1158  }
1159 
1160  // Remove unused virtual tables to improve the quality of code generated by
1161  // whole-program devirtualization and bitset lowering.
1162  MPM.addPass(GlobalDCEPass());
1163 
1164  // Force any function attributes we want the rest of the pipeline to observe.
1166 
1167  // Do basic inference of function attributes from known properties of system
1168  // libraries and other oracles.
1170 
1171  if (Level > 1) {
1172  FunctionPassManager EarlyFPM(DebugLogging);
1173  EarlyFPM.addPass(CallSiteSplittingPass());
1174  MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
1175 
1176  // Indirect call promotion. This should promote all the targets that are
1177  // left by the earlier promotion pass that promotes intra-module targets.
1178  // This two-step promotion is to save the compile time. For LTO, it should
1179  // produce the same result as if we only do promotion here.
1181  true /* InLTO */, PGOOpt && PGOOpt->Action == PGOOptions::SampleUse));
1182  // Propagate constants at call sites into the functions they call. This
1183  // opens opportunities for globalopt (and inlining) by substituting function
1184  // pointers passed as arguments to direct uses of functions.
1185  MPM.addPass(IPSCCPPass());
1186 
1187  // Attach metadata to indirect call sites indicating the set of functions
1188  // they may target at run-time. This should follow IPSCCP.
1190  }
1191 
1192  // Now deduce any function attributes based in the current code.
1195 
1196  // Do RPO function attribute inference across the module to forward-propagate
1197  // attributes where applicable.
1198  // FIXME: Is this really an optimization rather than a canonicalization?
1200 
1201  // Use in-range annotations on GEP indices to split globals where beneficial.
1202  MPM.addPass(GlobalSplitPass());
1203 
1204  // Run whole program optimization of virtual call when the list of callees
1205  // is fixed.
1206  MPM.addPass(WholeProgramDevirtPass(ExportSummary, nullptr));
1207 
1208  // Stop here at -O1.
1209  if (Level == 1) {
1210  // The LowerTypeTestsPass needs to run to lower type metadata and the
1211  // type.test intrinsics. The pass does nothing if CFI is disabled.
1212  MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr));
1213  return MPM;
1214  }
1215 
1216  // Optimize globals to try and fold them into constants.
1217  MPM.addPass(GlobalOptPass());
1218 
1219  // Promote any localized globals to SSA registers.
1221 
1222  // Linking modules together can lead to duplicate global constant, only
1223  // keep one copy of each constant.
1224  MPM.addPass(ConstantMergePass());
1225 
1226  // Remove unused arguments from functions.
1228 
1229  // Reduce the code after globalopt and ipsccp. Both can open up significant
1230  // simplification opportunities, and both can propagate functions through
1231  // function pointers. When this happens, we often have to resolve varargs
1232  // calls, etc, so let instcombine do this.
1233  FunctionPassManager PeepholeFPM(DebugLogging);
1234  if (Level == O3)
1235  PeepholeFPM.addPass(AggressiveInstCombinePass());
1236  PeepholeFPM.addPass(InstCombinePass());
1237  invokePeepholeEPCallbacks(PeepholeFPM, Level);
1238 
1239  MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM)));
1240 
1241  // Note: historically, the PruneEH pass was run first to deduce nounwind and
1242  // generally clean up exception handling overhead. It isn't clear this is
1243  // valuable as the inliner doesn't currently care whether it is inlining an
1244  // invoke or a call.
1245  // Run the inliner now.
1248 
1249  // Optimize globals again after we ran the inliner.
1250  MPM.addPass(GlobalOptPass());
1251 
1252  // Garbage collect dead functions.
1253  // FIXME: Add ArgumentPromotion pass after once it's ported.
1254  MPM.addPass(GlobalDCEPass());
1255 
1256  FunctionPassManager FPM(DebugLogging);
1257  // The IPO Passes may leave cruft around. Clean up after them.
1258  FPM.addPass(InstCombinePass());
1259  invokePeepholeEPCallbacks(FPM, Level);
1260 
1261  FPM.addPass(JumpThreadingPass());
1262 
1263  // Do a post inline PGO instrumentation and use pass. This is a context
1264  // sensitive PGO pass.
1265  if (PGOOpt) {
1266  if (PGOOpt->CSAction == PGOOptions::CSIRInstr)
1267  addPGOInstrPasses(MPM, DebugLogging, Level, /* RunProfileGen */ true,
1268  /* IsCS */ true, PGOOpt->CSProfileGenFile,
1269  PGOOpt->ProfileRemappingFile);
1270  else if (PGOOpt->CSAction == PGOOptions::CSIRUse)
1271  addPGOInstrPasses(MPM, DebugLogging, Level, /* RunProfileGen */ false,
1272  /* IsCS */ true, PGOOpt->ProfileFile,
1273  PGOOpt->ProfileRemappingFile);
1274  }
1275 
1276  // Break up allocas
1277  FPM.addPass(SROA());
1278 
1279  // LTO provides additional opportunities for tailcall elimination due to
1280  // link-time inlining, and visibility of nocapture attribute.
1281  FPM.addPass(TailCallElimPass());
1282 
1283  // Run a few AA driver optimizations here and now to cleanup the code.
1284  MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1285 
1288  // FIXME: here we run IP alias analysis in the legacy PM.
1289 
1290  FunctionPassManager MainFPM;
1291 
1292  // FIXME: once we fix LoopPass Manager, add LICM here.
1293  // FIXME: once we provide support for enabling MLSM, add it here.
1294  if (RunNewGVN)
1295  MainFPM.addPass(NewGVNPass());
1296  else
1297  MainFPM.addPass(GVN());
1298 
1299  // Remove dead memcpy()'s.
1300  MainFPM.addPass(MemCpyOptPass());
1301 
1302  // Nuke dead stores.
1303  MainFPM.addPass(DSEPass());
1304 
1305  // FIXME: at this point, we run a bunch of loop passes:
1306  // indVarSimplify, loopDeletion, loopInterchange, loopUnroll,
1307  // loopVectorize. Enable them once the remaining issue with LPM
1308  // are sorted out.
1309 
1310  MainFPM.addPass(InstCombinePass());
1311  MainFPM.addPass(SimplifyCFGPass());
1312  MainFPM.addPass(SCCPPass());
1313  MainFPM.addPass(InstCombinePass());
1314  MainFPM.addPass(BDCEPass());
1315 
1316  // FIXME: We may want to run SLPVectorizer here.
1317  // After vectorization, assume intrinsics may tell us more
1318  // about pointer alignments.
1319 #if 0
1320  MainFPM.add(AlignmentFromAssumptionsPass());
1321 #endif
1322 
1323  // FIXME: Conditionally run LoadCombine here, after it's ported
1324  // (in case we still have this pass, given its questionable usefulness).
1325 
1326  MainFPM.addPass(InstCombinePass());
1327  invokePeepholeEPCallbacks(MainFPM, Level);
1328  MainFPM.addPass(JumpThreadingPass());
1329  MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
1330 
1331  // Create a function that performs CFI checks for cross-DSO calls with
1332  // targets in the current module.
1333  MPM.addPass(CrossDSOCFIPass());
1334 
1335  // Lower type metadata and the type.test intrinsic. This pass supports
1336  // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
1337  // to be run at link time if CFI is enabled. This pass does nothing if
1338  // CFI is disabled.
1339  MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr));
1340 
1341  // Enable splitting late in the FullLTO post-link pipeline. This is done in
1342  // the same stage in the old pass manager (\ref addLateLTOOptimizationPasses).
1343  if (EnableHotColdSplit)
1345 
1346  // Add late LTO optimization passes.
1347  // Delete basic blocks, which optimization passes may have killed.
1349 
1350  // Drop bodies of available eternally objects to improve GlobalDCE.
1352 
1353  // Now that we have optimized the program, discard unreachable functions.
1354  MPM.addPass(GlobalDCEPass());
1355 
1356  // FIXME: Maybe enable MergeFuncs conditionally after it's ported.
1357  return MPM;
1358 }
1359 
1361  AAManager AA;
1362 
1363  // The order in which these are registered determines their priority when
1364  // being queried.
1365 
1366  // First we register the basic alias analysis that provides the majority of
1367  // per-function local AA logic. This is a stateless, on-demand local set of
1368  // AA techniques.
1370 
1371  // Next we query fast, specialized alias analyses that wrap IR-embedded
1372  // information about aliasing.
1375 
1376  // Add support for querying global aliasing information when available.
1377  // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
1378  // analysis, all that the `AAManager` can do is query for any *cached*
1379  // results from `GlobalsAA` through a readonly proxy.
1381 
1382  return AA;
1383 }
1384 
1386  if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
1387  return None;
1388  int Count;
1389  if (Name.getAsInteger(0, Count) || Count <= 0)
1390  return None;
1391  return Count;
1392 }
1393 
1395  if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
1396  return None;
1397  int Count;
1398  if (Name.getAsInteger(0, Count) || Count <= 0)
1399  return None;
1400  return Count;
1401 }
1402 
1404  if (!Name.consume_front(PassName))
1405  return false;
1406  // normal pass name w/o parameters == default parameters
1407  if (Name.empty())
1408  return true;
1409  return Name.startswith("<") && Name.endswith(">");
1410 }
1411 
1412 namespace {
1413 
1414 /// This performs customized parsing of pass name with parameters.
1415 ///
1416 /// We do not need parametrization of passes in textual pipeline very often,
1417 /// yet on a rare occasion ability to specify parameters right there can be
1418 /// useful.
1419 ///
1420 /// \p Name - parameterized specification of a pass from a textual pipeline
1421 /// is a string in a form of :
1422 /// PassName '<' parameter-list '>'
1423 ///
1424 /// Parameter list is being parsed by the parser callable argument, \p Parser,
1425 /// It takes a string-ref of parameters and returns either StringError or a
1426 /// parameter list in a form of a custom parameters type, all wrapped into
1427 /// Expected<> template class.
1428 ///
1429 template <typename ParametersParseCallableT>
1430 auto parsePassParameters(ParametersParseCallableT &&Parser, StringRef Name,
1431  StringRef PassName) -> decltype(Parser(StringRef{})) {
1432  using ParametersT = typename decltype(Parser(StringRef{}))::value_type;
1433 
1434  StringRef Params = Name;
1435  if (!Params.consume_front(PassName)) {
1436  assert(false &&
1437  "unable to strip pass name from parametrized pass specification");
1438  }
1439  if (Params.empty())
1440  return ParametersT{};
1441  if (!Params.consume_front("<") || !Params.consume_back(">")) {
1442  assert(false && "invalid format for parametrized pass name");
1443  }
1444 
1445  Expected<ParametersT> Result = Parser(Params);
1446  assert((Result || Result.template errorIsA<StringError>()) &&
1447  "Pass parameter parser can only return StringErrors.");
1448  return std::move(Result);
1449 }
1450 
1451 /// Parser of parameters for LoopUnroll pass.
1452 Expected<LoopUnrollOptions> parseLoopUnrollOptions(StringRef Params) {
1453  LoopUnrollOptions UnrollOpts;
1454  while (!Params.empty()) {
1455  StringRef ParamName;
1456  std::tie(ParamName, Params) = Params.split(';');
1457  int OptLevel = StringSwitch<int>(ParamName)
1458  .Case("O0", 0)
1459  .Case("O1", 1)
1460  .Case("O2", 2)
1461  .Case("O3", 3)
1462  .Default(-1);
1463  if (OptLevel >= 0) {
1464  UnrollOpts.setOptLevel(OptLevel);
1465  continue;
1466  }
1467  if (ParamName.consume_front("full-unroll-max=")) {
1468  int Count;
1469  if (ParamName.getAsInteger(0, Count))
1470  return make_error<StringError>(
1471  formatv("invalid LoopUnrollPass parameter '{0}' ", ParamName).str(),
1473  UnrollOpts.setFullUnrollMaxCount(Count);
1474  continue;
1475  }
1476 
1477  bool Enable = !ParamName.consume_front("no-");
1478  if (ParamName == "partial") {
1479  UnrollOpts.setPartial(Enable);
1480  } else if (ParamName == "peeling") {
1481  UnrollOpts.setPeeling(Enable);
1482  } else if (ParamName == "profile-peeling") {
1483  UnrollOpts.setProfileBasedPeeling(Enable);
1484  } else if (ParamName == "runtime") {
1485  UnrollOpts.setRuntime(Enable);
1486  } else if (ParamName == "upperbound") {
1487  UnrollOpts.setUpperBound(Enable);
1488  } else {
1489  return make_error<StringError>(
1490  formatv("invalid LoopUnrollPass parameter '{0}' ", ParamName).str(),
1492  }
1493  }
1494  return UnrollOpts;
1495 }
1496 
1497 Expected<MemorySanitizerOptions> parseMSanPassOptions(StringRef Params) {
1498  MemorySanitizerOptions Result;
1499  while (!Params.empty()) {
1500  StringRef ParamName;
1501  std::tie(ParamName, Params) = Params.split(';');
1502 
1503  if (ParamName == "recover") {
1504  Result.Recover = true;
1505  } else if (ParamName == "kernel") {
1506  Result.Kernel = true;
1507  } else if (ParamName.consume_front("track-origins=")) {
1508  if (ParamName.getAsInteger(0, Result.TrackOrigins))
1509  return make_error<StringError>(
1510  formatv("invalid argument to MemorySanitizer pass track-origins "
1511  "parameter: '{0}' ",
1512  ParamName)
1513  .str(),
1515  } else {
1516  return make_error<StringError>(
1517  formatv("invalid MemorySanitizer pass parameter '{0}' ", ParamName)
1518  .str(),
1520  }
1521  }
1522  return Result;
1523 }
1524 
1525 /// Parser of parameters for SimplifyCFG pass.
1526 Expected<SimplifyCFGOptions> parseSimplifyCFGOptions(StringRef Params) {
1527  SimplifyCFGOptions Result;
1528  while (!Params.empty()) {
1529  StringRef ParamName;
1530  std::tie(ParamName, Params) = Params.split(';');
1531 
1532  bool Enable = !ParamName.consume_front("no-");
1533  if (ParamName == "forward-switch-cond") {
1534  Result.forwardSwitchCondToPhi(Enable);
1535  } else if (ParamName == "switch-to-lookup") {
1536  Result.convertSwitchToLookupTable(Enable);
1537  } else if (ParamName == "keep-loops") {
1538  Result.needCanonicalLoops(Enable);
1539  } else if (ParamName == "sink-common-insts") {
1540  Result.sinkCommonInsts(Enable);
1541  } else if (Enable && ParamName.consume_front("bonus-inst-threshold=")) {
1542  APInt BonusInstThreshold;
1543  if (ParamName.getAsInteger(0, BonusInstThreshold))
1544  return make_error<StringError>(
1545  formatv("invalid argument to SimplifyCFG pass bonus-threshold "
1546  "parameter: '{0}' ",
1547  ParamName).str(),
1549  Result.bonusInstThreshold(BonusInstThreshold.getSExtValue());
1550  } else {
1551  return make_error<StringError>(
1552  formatv("invalid SimplifyCFG pass parameter '{0}' ", ParamName).str(),
1554  }
1555  }
1556  return Result;
1557 }
1558 
1559 /// Parser of parameters for LoopVectorize pass.
1560 Expected<LoopVectorizeOptions> parseLoopVectorizeOptions(StringRef Params) {
1561  LoopVectorizeOptions Opts;
1562  while (!Params.empty()) {
1563  StringRef ParamName;
1564  std::tie(ParamName, Params) = Params.split(';');
1565 
1566  bool Enable = !ParamName.consume_front("no-");
1567  if (ParamName == "interleave-forced-only") {
1568  Opts.setInterleaveOnlyWhenForced(Enable);
1569  } else if (ParamName == "vectorize-forced-only") {
1570  Opts.setVectorizeOnlyWhenForced(Enable);
1571  } else {
1572  return make_error<StringError>(
1573  formatv("invalid LoopVectorize parameter '{0}' ", ParamName).str(),
1575  }
1576  }
1577  return Opts;
1578 }
1579 
1580 Expected<bool> parseLoopUnswitchOptions(StringRef Params) {
1581  bool Result = false;
1582  while (!Params.empty()) {
1583  StringRef ParamName;
1584  std::tie(ParamName, Params) = Params.split(';');
1585 
1586  bool Enable = !ParamName.consume_front("no-");
1587  if (ParamName == "nontrivial") {
1588  Result = Enable;
1589  } else {
1590  return make_error<StringError>(
1591  formatv("invalid LoopUnswitch pass parameter '{0}' ", ParamName)
1592  .str(),
1594  }
1595  }
1596  return Result;
1597 }
1598 
1599 Expected<bool> parseMergedLoadStoreMotionOptions(StringRef Params) {
1600  bool Result = false;
1601  while (!Params.empty()) {
1602  StringRef ParamName;
1603  std::tie(ParamName, Params) = Params.split(';');
1604 
1605  bool Enable = !ParamName.consume_front("no-");
1606  if (ParamName == "split-footer-bb") {
1607  Result = Enable;
1608  } else {
1609  return make_error<StringError>(
1610  formatv("invalid MergedLoadStoreMotion pass parameter '{0}' ",
1611  ParamName)
1612  .str(),
1614  }
1615  }
1616  return Result;
1617 }
1618 } // namespace
1619 
1620 /// Tests whether a pass name starts with a valid prefix for a default pipeline
1621 /// alias.
1623  return Name.startswith("default") || Name.startswith("thinlto") ||
1624  Name.startswith("lto");
1625 }
1626 
1627 /// Tests whether registered callbacks will accept a given pass name.
1628 ///
1629 /// When parsing a pipeline text, the type of the outermost pipeline may be
1630 /// omitted, in which case the type is automatically determined from the first
1631 /// pass name in the text. This may be a name that is handled through one of the
1632 /// callbacks. We check this through the oridinary parsing callbacks by setting
1633 /// up a dummy PassManager in order to not force the client to also handle this
1634 /// type of query.
1635 template <typename PassManagerT, typename CallbacksT>
1636 static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) {
1637  if (!Callbacks.empty()) {
1638  PassManagerT DummyPM;
1639  for (auto &CB : Callbacks)
1640  if (CB(Name, DummyPM, {}))
1641  return true;
1642  }
1643  return false;
1644 }
1645 
1646 template <typename CallbacksT>
1647 static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) {
1648  // Manually handle aliases for pre-configured pipeline fragments.
1650  return DefaultAliasRegex.match(Name);
1651 
1652  // Explicitly handle pass manager names.
1653  if (Name == "module")
1654  return true;
1655  if (Name == "cgscc")
1656  return true;
1657  if (Name == "function")
1658  return true;
1659 
1660  // Explicitly handle custom-parsed pass names.
1661  if (parseRepeatPassName(Name))
1662  return true;
1663 
1664 #define MODULE_PASS(NAME, CREATE_PASS) \
1665  if (Name == NAME) \
1666  return true;
1667 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1668  if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1669  return true;
1670 #include "PassRegistry.def"
1671 
1672  return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks);
1673 }
1674 
1675 template <typename CallbacksT>
1676 static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) {
1677  // Explicitly handle pass manager names.
1678  if (Name == "cgscc")
1679  return true;
1680  if (Name == "function")
1681  return true;
1682 
1683  // Explicitly handle custom-parsed pass names.
1684  if (parseRepeatPassName(Name))
1685  return true;
1686  if (parseDevirtPassName(Name))
1687  return true;
1688 
1689 #define CGSCC_PASS(NAME, CREATE_PASS) \
1690  if (Name == NAME) \
1691  return true;
1692 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1693  if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1694  return true;
1695 #include "PassRegistry.def"
1696 
1697  return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks);
1698 }
1699 
1700 template <typename CallbacksT>
1701 static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) {
1702  // Explicitly handle pass manager names.
1703  if (Name == "function")
1704  return true;
1705  if (Name == "loop" || Name == "loop-mssa")
1706  return true;
1707 
1708  // Explicitly handle custom-parsed pass names.
1709  if (parseRepeatPassName(Name))
1710  return true;
1711 
1712 #define FUNCTION_PASS(NAME, CREATE_PASS) \
1713  if (Name == NAME) \
1714  return true;
1715 #define FUNCTION_PASS_WITH_PARAMS(NAME, CREATE_PASS, PARSER) \
1716  if (checkParametrizedPassName(Name, NAME)) \
1717  return true;
1718 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1719  if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1720  return true;
1721 #include "PassRegistry.def"
1722 
1723  return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks);
1724 }
1725 
1726 template <typename CallbacksT>
1727 static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks) {
1728  // Explicitly handle pass manager names.
1729  if (Name == "loop" || Name == "loop-mssa")
1730  return true;
1731 
1732  // Explicitly handle custom-parsed pass names.
1733  if (parseRepeatPassName(Name))
1734  return true;
1735 
1736 #define LOOP_PASS(NAME, CREATE_PASS) \
1737  if (Name == NAME) \
1738  return true;
1739 #define LOOP_PASS_WITH_PARAMS(NAME, CREATE_PASS, PARSER) \
1740  if (checkParametrizedPassName(Name, NAME)) \
1741  return true;
1742 #define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1743  if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1744  return true;
1745 #include "PassRegistry.def"
1746 
1747  return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
1748 }
1749 
1751 PassBuilder::parsePipelineText(StringRef Text) {
1752  std::vector<PipelineElement> ResultPipeline;
1753 
1754  SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
1755  &ResultPipeline};
1756  for (;;) {
1757  std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1758  size_t Pos = Text.find_first_of(",()");
1759  Pipeline.push_back({Text.substr(0, Pos), {}});
1760 
1761  // If we have a single terminating name, we're done.
1762  if (Pos == Text.npos)
1763  break;
1764 
1765  char Sep = Text[Pos];
1766  Text = Text.substr(Pos + 1);
1767  if (Sep == ',')
1768  // Just a name ending in a comma, continue.
1769  continue;
1770 
1771  if (Sep == '(') {
1772  // Push the inner pipeline onto the stack to continue processing.
1773  PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1774  continue;
1775  }
1776 
1777  assert(Sep == ')' && "Bogus separator!");
1778  // When handling the close parenthesis, we greedily consume them to avoid
1779  // empty strings in the pipeline.
1780  do {
1781  // If we try to pop the outer pipeline we have unbalanced parentheses.
1782  if (PipelineStack.size() == 1)
1783  return None;
1784 
1785  PipelineStack.pop_back();
1786  } while (Text.consume_front(")"));
1787 
1788  // Check if we've finished parsing.
1789  if (Text.empty())
1790  break;
1791 
1792  // Otherwise, the end of an inner pipeline always has to be followed by
1793  // a comma, and then we can continue.
1794  if (!Text.consume_front(","))
1795  return None;
1796  }
1797 
1798  if (PipelineStack.size() > 1)
1799  // Unbalanced paretheses.
1800  return None;
1801 
1802  assert(PipelineStack.back() == &ResultPipeline &&
1803  "Wrong pipeline at the bottom of the stack!");
1804  return {std::move(ResultPipeline)};
1805 }
1806 
1807 Error PassBuilder::parseModulePass(ModulePassManager &MPM,
1808  const PipelineElement &E,
1809  bool VerifyEachPass, bool DebugLogging) {
1810  auto &Name = E.Name;
1811  auto &InnerPipeline = E.InnerPipeline;
1812 
1813  // First handle complex passes like the pass managers which carry pipelines.
1814  if (!InnerPipeline.empty()) {
1815  if (Name == "module") {
1816  ModulePassManager NestedMPM(DebugLogging);
1817  if (auto Err = parseModulePassPipeline(NestedMPM, InnerPipeline,
1818  VerifyEachPass, DebugLogging))
1819  return Err;
1820  MPM.addPass(std::move(NestedMPM));
1821  return Error::success();
1822  }
1823  if (Name == "cgscc") {
1824  CGSCCPassManager CGPM(DebugLogging);
1825  if (auto Err = parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1826  DebugLogging))
1827  return Err;
1828  MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
1829  return Error::success();
1830  }
1831  if (Name == "function") {
1832  FunctionPassManager FPM(DebugLogging);
1833  if (auto Err = parseFunctionPassPipeline(FPM, InnerPipeline,
1834  VerifyEachPass, DebugLogging))
1835  return Err;
1836  MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1837  return Error::success();
1838  }
1839  if (auto Count = parseRepeatPassName(Name)) {
1840  ModulePassManager NestedMPM(DebugLogging);
1841  if (auto Err = parseModulePassPipeline(NestedMPM, InnerPipeline,
1842  VerifyEachPass, DebugLogging))
1843  return Err;
1844  MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
1845  return Error::success();
1846  }
1847 
1848  for (auto &C : ModulePipelineParsingCallbacks)
1849  if (C(Name, MPM, InnerPipeline))
1850  return Error::success();
1851 
1852  // Normal passes can't have pipelines.
1853  return make_error<StringError>(
1854  formatv("invalid use of '{0}' pass as module pipeline", Name).str(),
1856  ;
1857  }
1858 
1859  // Manually handle aliases for pre-configured pipeline fragments.
1861  SmallVector<StringRef, 3> Matches;
1862  if (!DefaultAliasRegex.match(Name, &Matches))
1863  return make_error<StringError>(
1864  formatv("unknown default pipeline alias '{0}'", Name).str(),
1866 
1867  assert(Matches.size() == 3 && "Must capture two matched strings!");
1868 
1869  OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
1870  .Case("O0", O0)
1871  .Case("O1", O1)
1872  .Case("O2", O2)
1873  .Case("O3", O3)
1874  .Case("Os", Os)
1875  .Case("Oz", Oz);
1876  if (L == O0) {
1877  // Add instrumentation PGO passes -- at O0 we can still do PGO.
1878  if (PGOOpt && Matches[1] != "thinlto" &&
1879  (PGOOpt->Action == PGOOptions::IRInstr ||
1880  PGOOpt->Action == PGOOptions::IRUse))
1881  addPGOInstrPassesForO0(
1882  MPM, DebugLogging,
1883  /* RunProfileGen */ (PGOOpt->Action == PGOOptions::IRInstr),
1884  /* IsCS */ false, PGOOpt->ProfileFile,
1885  PGOOpt->ProfileRemappingFile);
1886  // Do nothing else at all!
1887  return Error::success();
1888  }
1889 
1890  if (Matches[1] == "default") {
1891  MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
1892  } else if (Matches[1] == "thinlto-pre-link") {
1893  MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L, DebugLogging));
1894  } else if (Matches[1] == "thinlto") {
1895  MPM.addPass(buildThinLTODefaultPipeline(L, DebugLogging, nullptr));
1896  } else if (Matches[1] == "lto-pre-link") {
1897  MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
1898  } else {
1899  assert(Matches[1] == "lto" && "Not one of the matched options!");
1900  MPM.addPass(buildLTODefaultPipeline(L, DebugLogging, nullptr));
1901  }
1902  return Error::success();
1903  }
1904 
1905  // Finally expand the basic registered passes from the .inc file.
1906 #define MODULE_PASS(NAME, CREATE_PASS) \
1907  if (Name == NAME) { \
1908  MPM.addPass(CREATE_PASS); \
1909  return Error::success(); \
1910  }
1911 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1912  if (Name == "require<" NAME ">") { \
1913  MPM.addPass( \
1914  RequireAnalysisPass< \
1915  std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
1916  return Error::success(); \
1917  } \
1918  if (Name == "invalidate<" NAME ">") { \
1919  MPM.addPass(InvalidateAnalysisPass< \
1920  std::remove_reference<decltype(CREATE_PASS)>::type>()); \
1921  return Error::success(); \
1922  }
1923 #include "PassRegistry.def"
1924 
1925  for (auto &C : ModulePipelineParsingCallbacks)
1926  if (C(Name, MPM, InnerPipeline))
1927  return Error::success();
1928  return make_error<StringError>(
1929  formatv("unknown module pass '{0}'", Name).str(),
1931 }
1932 
1933 Error PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1934  const PipelineElement &E, bool VerifyEachPass,
1935  bool DebugLogging) {
1936  auto &Name = E.Name;
1937  auto &InnerPipeline = E.InnerPipeline;
1938 
1939  // First handle complex passes like the pass managers which carry pipelines.
1940  if (!InnerPipeline.empty()) {
1941  if (Name == "cgscc") {
1942  CGSCCPassManager NestedCGPM(DebugLogging);
1943  if (auto Err = parseCGSCCPassPipeline(NestedCGPM, InnerPipeline,
1944  VerifyEachPass, DebugLogging))
1945  return Err;
1946  // Add the nested pass manager with the appropriate adaptor.
1947  CGPM.addPass(std::move(NestedCGPM));
1948  return Error::success();
1949  }
1950  if (Name == "function") {
1951  FunctionPassManager FPM(DebugLogging);
1952  if (auto Err = parseFunctionPassPipeline(FPM, InnerPipeline,
1953  VerifyEachPass, DebugLogging))
1954  return Err;
1955  // Add the nested pass manager with the appropriate adaptor.
1956  CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
1957  return Error::success();
1958  }
1959  if (auto Count = parseRepeatPassName(Name)) {
1960  CGSCCPassManager NestedCGPM(DebugLogging);
1961  if (auto Err = parseCGSCCPassPipeline(NestedCGPM, InnerPipeline,
1962  VerifyEachPass, DebugLogging))
1963  return Err;
1964  CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
1965  return Error::success();
1966  }
1967  if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1968  CGSCCPassManager NestedCGPM(DebugLogging);
1969  if (auto Err = parseCGSCCPassPipeline(NestedCGPM, InnerPipeline,
1970  VerifyEachPass, DebugLogging))
1971  return Err;
1972  CGPM.addPass(
1973  createDevirtSCCRepeatedPass(std::move(NestedCGPM), *MaxRepetitions));
1974  return Error::success();
1975  }
1976 
1977  for (auto &C : CGSCCPipelineParsingCallbacks)
1978  if (C(Name, CGPM, InnerPipeline))
1979  return Error::success();
1980 
1981  // Normal passes can't have pipelines.
1982  return make_error<StringError>(
1983  formatv("invalid use of '{0}' pass as cgscc pipeline", Name).str(),
1985  }
1986 
1987 // Now expand the basic registered passes from the .inc file.
1988 #define CGSCC_PASS(NAME, CREATE_PASS) \
1989  if (Name == NAME) { \
1990  CGPM.addPass(CREATE_PASS); \
1991  return Error::success(); \
1992  }
1993 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1994  if (Name == "require<" NAME ">") { \
1995  CGPM.addPass(RequireAnalysisPass< \
1996  std::remove_reference<decltype(CREATE_PASS)>::type, \
1997  LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1998  CGSCCUpdateResult &>()); \
1999  return Error::success(); \
2000  } \
2001  if (Name == "invalidate<" NAME ">") { \
2002  CGPM.addPass(InvalidateAnalysisPass< \
2003  std::remove_reference<decltype(CREATE_PASS)>::type>()); \
2004  return Error::success(); \
2005  }
2006 #include "PassRegistry.def"
2007 
2008  for (auto &C : CGSCCPipelineParsingCallbacks)
2009  if (C(Name, CGPM, InnerPipeline))
2010  return Error::success();
2011  return make_error<StringError>(
2012  formatv("unknown cgscc pass '{0}'", Name).str(),
2014 }
2015 
2016 Error PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
2017  const PipelineElement &E,
2018  bool VerifyEachPass, bool DebugLogging) {
2019  auto &Name = E.Name;
2020  auto &InnerPipeline = E.InnerPipeline;
2021 
2022  // First handle complex passes like the pass managers which carry pipelines.
2023  if (!InnerPipeline.empty()) {
2024  if (Name == "function") {
2025  FunctionPassManager NestedFPM(DebugLogging);
2026  if (auto Err = parseFunctionPassPipeline(NestedFPM, InnerPipeline,
2027  VerifyEachPass, DebugLogging))
2028  return Err;
2029  // Add the nested pass manager with the appropriate adaptor.
2030  FPM.addPass(std::move(NestedFPM));
2031  return Error::success();
2032  }
2033  if (Name == "loop" || Name == "loop-mssa") {
2034  LoopPassManager LPM(DebugLogging);
2035  if (auto Err = parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
2036  DebugLogging))
2037  return Err;
2038  // Add the nested pass manager with the appropriate adaptor.
2039  bool UseMemorySSA = (Name == "loop-mssa");
2040  FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM), UseMemorySSA,
2041  DebugLogging));
2042  return Error::success();
2043  }
2044  if (auto Count = parseRepeatPassName(Name)) {
2045  FunctionPassManager NestedFPM(DebugLogging);
2046  if (auto Err = parseFunctionPassPipeline(NestedFPM, InnerPipeline,
2047  VerifyEachPass, DebugLogging))
2048  return Err;
2049  FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
2050  return Error::success();
2051  }
2052 
2053  for (auto &C : FunctionPipelineParsingCallbacks)
2054  if (C(Name, FPM, InnerPipeline))
2055  return Error::success();
2056 
2057  // Normal passes can't have pipelines.
2058  return make_error<StringError>(
2059  formatv("invalid use of '{0}' pass as function pipeline", Name).str(),
2061  }
2062 
2063 // Now expand the basic registered passes from the .inc file.
2064 #define FUNCTION_PASS(NAME, CREATE_PASS) \
2065  if (Name == NAME) { \
2066  FPM.addPass(CREATE_PASS); \
2067  return Error::success(); \
2068  }
2069 #define FUNCTION_PASS_WITH_PARAMS(NAME, CREATE_PASS, PARSER) \
2070  if (checkParametrizedPassName(Name, NAME)) { \
2071  auto Params = parsePassParameters(PARSER, Name, NAME); \
2072  if (!Params) \
2073  return Params.takeError(); \
2074  FPM.addPass(CREATE_PASS(Params.get())); \
2075  return Error::success(); \
2076  }
2077 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
2078  if (Name == "require<" NAME ">") { \
2079  FPM.addPass( \
2080  RequireAnalysisPass< \
2081  std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
2082  return Error::success(); \
2083  } \
2084  if (Name == "invalidate<" NAME ">") { \
2085  FPM.addPass(InvalidateAnalysisPass< \
2086  std::remove_reference<decltype(CREATE_PASS)>::type>()); \
2087  return Error::success(); \
2088  }
2089 #include "PassRegistry.def"
2090 
2091  for (auto &C : FunctionPipelineParsingCallbacks)
2092  if (C(Name, FPM, InnerPipeline))
2093  return Error::success();
2094  return make_error<StringError>(
2095  formatv("unknown function pass '{0}'", Name).str(),
2097 }
2098 
2099 Error PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
2100  bool VerifyEachPass, bool DebugLogging) {
2101  StringRef Name = E.Name;
2102  auto &InnerPipeline = E.InnerPipeline;
2103 
2104  // First handle complex passes like the pass managers which carry pipelines.
2105  if (!InnerPipeline.empty()) {
2106  if (Name == "loop") {
2107  LoopPassManager NestedLPM(DebugLogging);
2108  if (auto Err = parseLoopPassPipeline(NestedLPM, InnerPipeline,
2109  VerifyEachPass, DebugLogging))
2110  return Err;
2111  // Add the nested pass manager with the appropriate adaptor.
2112  LPM.addPass(std::move(NestedLPM));
2113  return Error::success();
2114  }
2115  if (auto Count = parseRepeatPassName(Name)) {
2116  LoopPassManager NestedLPM(DebugLogging);
2117  if (auto Err = parseLoopPassPipeline(NestedLPM, InnerPipeline,
2118  VerifyEachPass, DebugLogging))
2119  return Err;
2120  LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
2121  return Error::success();
2122  }
2123 
2124  for (auto &C : LoopPipelineParsingCallbacks)
2125  if (C(Name, LPM, InnerPipeline))
2126  return Error::success();
2127 
2128  // Normal passes can't have pipelines.
2129  return make_error<StringError>(
2130  formatv("invalid use of '{0}' pass as loop pipeline", Name).str(),
2132  }
2133 
2134 // Now expand the basic registered passes from the .inc file.
2135 #define LOOP_PASS(NAME, CREATE_PASS) \
2136  if (Name == NAME) { \
2137  LPM.addPass(CREATE_PASS); \
2138  return Error::success(); \
2139  }
2140 #define LOOP_PASS_WITH_PARAMS(NAME, CREATE_PASS, PARSER) \
2141  if (checkParametrizedPassName(Name, NAME)) { \
2142  auto Params = parsePassParameters(PARSER, Name, NAME); \
2143  if (!Params) \
2144  return Params.takeError(); \
2145  LPM.addPass(CREATE_PASS(Params.get())); \
2146  return Error::success(); \
2147  }
2148 #define LOOP_ANALYSIS(NAME, CREATE_PASS) \
2149  if (Name == "require<" NAME ">") { \
2150  LPM.addPass(RequireAnalysisPass< \
2151  std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
2152  LoopAnalysisManager, LoopStandardAnalysisResults &, \
2153  LPMUpdater &>()); \
2154  return Error::success(); \
2155  } \
2156  if (Name == "invalidate<" NAME ">") { \
2157  LPM.addPass(InvalidateAnalysisPass< \
2158  std::remove_reference<decltype(CREATE_PASS)>::type>()); \
2159  return Error::success(); \
2160  }
2161 #include "PassRegistry.def"
2162 
2163  for (auto &C : LoopPipelineParsingCallbacks)
2164  if (C(Name, LPM, InnerPipeline))
2165  return Error::success();
2166  return make_error<StringError>(formatv("unknown loop pass '{0}'", Name).str(),
2168 }
2169 
2170 bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
2171 #define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
2172  if (Name == NAME) { \
2173  AA.registerModuleAnalysis< \
2174  std::remove_reference<decltype(CREATE_PASS)>::type>(); \
2175  return true; \
2176  }
2177 #define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
2178  if (Name == NAME) { \
2179  AA.registerFunctionAnalysis< \
2180  std::remove_reference<decltype(CREATE_PASS)>::type>(); \
2181  return true; \
2182  }
2183 #include "PassRegistry.def"
2184 
2185  for (auto &C : AAParsingCallbacks)
2186  if (C(Name, AA))
2187  return true;
2188  return false;
2189 }
2190 
2191 Error PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
2192  ArrayRef<PipelineElement> Pipeline,
2193  bool VerifyEachPass,
2194  bool DebugLogging) {
2195  for (const auto &Element : Pipeline) {
2196  if (auto Err = parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
2197  return Err;
2198  // FIXME: No verifier support for Loop passes!
2199  }
2200  return Error::success();
2201 }
2202 
2203 Error PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
2204  ArrayRef<PipelineElement> Pipeline,
2205  bool VerifyEachPass,
2206  bool DebugLogging) {
2207  for (const auto &Element : Pipeline) {
2208  if (auto Err =
2209  parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
2210  return Err;
2211  if (VerifyEachPass)
2212  FPM.addPass(VerifierPass());
2213  }
2214  return Error::success();
2215 }
2216 
2217 Error PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
2218  ArrayRef<PipelineElement> Pipeline,
2219  bool VerifyEachPass,
2220  bool DebugLogging) {
2221  for (const auto &Element : Pipeline) {
2222  if (auto Err = parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
2223  return Err;
2224  // FIXME: No verifier support for CGSCC passes!
2225  }
2226  return Error::success();
2227 }
2228 
2231  CGSCCAnalysisManager &CGAM,
2232  ModuleAnalysisManager &MAM) {
2233  MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
2234  MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
2235  CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
2236  FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
2237  FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
2238  FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
2239  LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
2240 }
2241 
2242 Error PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
2243  ArrayRef<PipelineElement> Pipeline,
2244  bool VerifyEachPass,
2245  bool DebugLogging) {
2246  for (const auto &Element : Pipeline) {
2247  if (auto Err = parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
2248  return Err;
2249  if (VerifyEachPass)
2250  MPM.addPass(VerifierPass());
2251  }
2252  return Error::success();
2253 }
2254 
2255 // Primary pass pipeline description parsing routine for a \c ModulePassManager
2256 // FIXME: Should this routine accept a TargetMachine or require the caller to
2257 // pre-populate the analysis managers with target-specific stuff?
2259  StringRef PipelineText,
2260  bool VerifyEachPass, bool DebugLogging) {
2261  auto Pipeline = parsePipelineText(PipelineText);
2262  if (!Pipeline || Pipeline->empty())
2263  return make_error<StringError>(
2264  formatv("invalid pipeline '{0}'", PipelineText).str(),
2266 
2267  // If the first name isn't at the module layer, wrap the pipeline up
2268  // automatically.
2269  StringRef FirstName = Pipeline->front().Name;
2270 
2271  if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) {
2272  if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) {
2273  Pipeline = {{"cgscc", std::move(*Pipeline)}};
2274  } else if (isFunctionPassName(FirstName,
2275  FunctionPipelineParsingCallbacks)) {
2276  Pipeline = {{"function", std::move(*Pipeline)}};
2277  } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks)) {
2278  Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
2279  } else {
2280  for (auto &C : TopLevelPipelineParsingCallbacks)
2281  if (C(MPM, *Pipeline, VerifyEachPass, DebugLogging))
2282  return Error::success();
2283 
2284  // Unknown pass or pipeline name!
2285  auto &InnerPipeline = Pipeline->front().InnerPipeline;
2286  return make_error<StringError>(
2287  formatv("unknown {0} name '{1}'",
2288  (InnerPipeline.empty() ? "pass" : "pipeline"), FirstName)
2289  .str(),
2291  }
2292  }
2293 
2294  if (auto Err =
2295  parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging))
2296  return Err;
2297  return Error::success();
2298 }
2299 
2300 // Primary pass pipeline description parsing routine for a \c CGSCCPassManager
2302  StringRef PipelineText,
2303  bool VerifyEachPass, bool DebugLogging) {
2304  auto Pipeline = parsePipelineText(PipelineText);
2305  if (!Pipeline || Pipeline->empty())
2306  return make_error<StringError>(
2307  formatv("invalid pipeline '{0}'", PipelineText).str(),
2309 
2310  StringRef FirstName = Pipeline->front().Name;
2311  if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks))
2312  return make_error<StringError>(
2313  formatv("unknown cgscc pass '{0}' in pipeline '{1}'", FirstName,
2314  PipelineText)
2315  .str(),
2317 
2318  if (auto Err =
2319  parseCGSCCPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging))
2320  return Err;
2321  return Error::success();
2322 }
2323 
2324 // Primary pass pipeline description parsing routine for a \c
2325 // FunctionPassManager
2327  StringRef PipelineText,
2328  bool VerifyEachPass, bool DebugLogging) {
2329  auto Pipeline = parsePipelineText(PipelineText);
2330  if (!Pipeline || Pipeline->empty())
2331  return make_error<StringError>(
2332  formatv("invalid pipeline '{0}'", PipelineText).str(),
2334 
2335  StringRef FirstName = Pipeline->front().Name;
2336  if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks))
2337  return make_error<StringError>(
2338  formatv("unknown function pass '{0}' in pipeline '{1}'", FirstName,
2339  PipelineText)
2340  .str(),
2342 
2343  if (auto Err = parseFunctionPassPipeline(FPM, *Pipeline, VerifyEachPass,
2344  DebugLogging))
2345  return Err;
2346  return Error::success();
2347 }
2348 
2349 // Primary pass pipeline description parsing routine for a \c LoopPassManager
2351  StringRef PipelineText,
2352  bool VerifyEachPass, bool DebugLogging) {
2353  auto Pipeline = parsePipelineText(PipelineText);
2354  if (!Pipeline || Pipeline->empty())
2355  return make_error<StringError>(
2356  formatv("invalid pipeline '{0}'", PipelineText).str(),
2358 
2359  if (auto Err =
2360  parseLoopPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging))
2361  return Err;
2362 
2363  return Error::success();
2364 }
2365 
2367  // If the pipeline just consists of the word 'default' just replace the AA
2368  // manager with our default one.
2369  if (PipelineText == "default") {
2370  AA = buildDefaultAAPipeline();
2371  return Error::success();
2372  }
2373 
2374  while (!PipelineText.empty()) {
2375  StringRef Name;
2376  std::tie(Name, PipelineText) = PipelineText.split(',');
2377  if (!parseAAPassName(AA, Name))
2378  return make_error<StringError>(
2379  formatv("unknown alias analysis name '{0}'", Name).str(),
2381  }
2382 
2383  return Error::success();
2384 }
Super simple passes to force specific function attrs from the commandline into the IR for debugging p...
Defines passes for running instruction simplification across chunks of IR.
This file provides the interface for the pass responsible for both simplifying and canonicalizing the...
const T & front() const
front - Get the first element.
Definition: ArrayRef.h:151
Analysis pass providing a never-invalidated alias analysis result.
A set of parameters used to control various transforms performed by the LoopUnroll pass...
uint64_t CallInst * C
Computes function attributes in post-order over the call graph.
Definition: FunctionAttrs.h:50
ModulePassManager buildPerModuleDefaultPipeline(OptimizationLevel Level, bool DebugLogging=false, bool LTOPreLink=false)
Build a per-module default optimization pipeline.
A simple loop rotation transformation.
Definition: LoopRotation.h:23
A simple and fast domtree-based CSE pass.
Definition: EarlyCSE.h:30
The profile annotation (profile-instr-use) pass for IR based PGO.
Thresholds to tune inline cost analysis.
Definition: InlineCost.h:153
bool LoopVectorization
Tuning option to enable/disable loop vectorization.
Definition: PassBuilder.h:82
Interfaces for registering analysis passes, producing common pass manager configurations, and parsing of pass pipelines.
AAManager buildDefaultAAPipeline()
Build the default AAManager with the default alias analysis pipeline registered.
Eliminate dead arguments (and return values) from functions.
LLVM_NODISCARD bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:281
This is the interface for LLVM&#39;s inclusion-based alias analysis implemented with CFL graph reachabili...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static Optional< int > parseRepeatPassName(StringRef Name)
This header provides classes for managing a pipeline of passes over loops in LLVM IR...
cl::opt< bool > ForgetSCEVInLoopUnroll
This is the interface for a simple mod/ref and alias analysis over globals.
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
Error parseAAPipeline(AAManager &AA, StringRef PipelineText)
Parse a textual alias analysis pipeline into the provided AA manager.
This is the interface to build a ModuleSummaryIndex for a module.
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:270
Uses an "inverted" value numbering to decide the similarity of expressions and sinks similar expressi...
Definition: GVN.h:308
InnerAnalysisManagerProxy< LoopAnalysisManager, Function > LoopAnalysisManagerFunctionProxy
A proxy from a LoopAnalysisManager to a Function.
Performs basic CFG simplifications to assist other loop passes.
This file provides the interface for LLVM&#39;s Scalar Replacement of Aggregates pass.
This file implements a simple N^2 alias analysis accuracy evaluator.
InnerAnalysisManagerProxy< CGSCCAnalysisManager, Module > CGSCCAnalysisManagerModuleProxy
A proxy from a CGSCCAnalysisManager to a Module.
Implements a lazy call graph analysis and related passes for the new pass manager.
RepeatedPass< PassT > createRepeatedPass(int Count, PassT P)
Definition: PassManager.h:1444
This file implements the Loop Fusion pass.
void registerModuleAnalyses(ModuleAnalysisManager &MAM)
Registers all available module analysis passes.
bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition: StringRef.h:663
This is the interface for a metadata-based scoped no-alias analysis.
SimplifyCFGOptions & bonusInstThreshold(int I)
Definition: Local.h:84
SimplifyCFGOptions & sinkCommonInsts(bool B)
Definition: Local.h:100
void registerModuleAnalysis()
Register a specific AA result.
LoopUnrollOptions & setFullUnrollMaxCount(unsigned O)
The instrumentation pass for recording function order.
ModulePassManager buildModuleOptimizationPipeline(OptimizationLevel Level, bool DebugLogging=false, bool LTOPreLink=false)
Construct the core LLVM module optimization pipeline.
LoopUnrollOptions & setUpperBound(bool UpperBound)
Enables or disables the use of trip count upper bound in loop unrolling.
ModuleToPostOrderCGSCCPassAdaptor< CGSCCPassT > createModuleToPostOrderCGSCCPassAdaptor(CGSCCPassT Pass)
A function to deduce a function pass type and wrap it in the templated adaptor.
CGSCCToFunctionPassAdaptor< FunctionPassT > createCGSCCToFunctionPassAdaptor(FunctionPassT Pass)
A function to deduce a function pass type and wrap it in the templated adaptor.
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
Pass to remove unused function declarations.
OptimizationLevel
LLVM-provided high-level optimization levels.
Definition: PassBuilder.h:146
ModulePassManager buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level, bool DebugLogging=false)
Build a pre-link, ThinLTO-targeting default optimization pipeline to a pass manager.
This file provides the interface for LLVM&#39;s PGO Instrumentation lowering pass.
OuterAnalysisManagerProxy< FunctionAnalysisManager, Loop, LoopStandardAnalysisResults & > FunctionAnalysisManagerLoopProxy
A proxy from a FunctionAnalysisManager to a Loop.
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
F(f)
Loop unroll pass that only does full loop unrolling.
Loop unroll pass that will support both full and partial unrolling.
Pass to perform split of global variables.
Definition: GlobalSplit.h:26
This pass transforms loops that contain branches or switches on loop- invariant conditions to have mu...
A DCE pass that assumes instructions are dead until proven otherwise.
Definition: ADCE.h:31
static cl::opt< bool > EnableSyntheticCounts("enable-npm-synthetic-counts", cl::init(false), cl::Hidden, cl::ZeroOrMore, cl::desc("Run synthetic function entry count generation " "pass"))
A utility pass template to force an analysis result to be available.
Definition: PassManager.h:1356
static cl::opt< bool > EnableGVNHoist("enable-npm-gvn-hoist", cl::init(false), cl::Hidden, cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"))
Reassociate commutative expressions.
Definition: Reassociate.h:71
Support structure for SCC passes to communicate updates the call graph back to the CGSCC pass manager...
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:67
cl::opt< bool > EnableHotColdSplit
Run instruction simplification across each instruction in the function.
Pass to forward loads in a loop around the backedge to subsequent iterations.
SimplifyCFGOptions & forwardSwitchCondToPhi(bool B)
Definition: Local.h:88
cl::opt< bool > EnableLoopInterleaving
A pass that transforms external global definitions into declarations.
This pass handles simple speculating of instructions around PHIs when doing so is profitable for a pa...
A simple and fast domtree-based GVN pass to hoist common expressions from sibling branches...
Definition: GVN.h:301
This is the interface for a SCEV-based alias analysis.
ModulePassManager buildModuleSimplificationPipeline(OptimizationLevel Level, ThinLTOPhase Phase, bool DebugLogging=false)
Construct the core LLVM module canonicalization and simplification pipeline.
A pass to do RPO deduction and propagation of function attributes.
Definition: FunctionAttrs.h:69
Optional< int > HintThreshold
Threshold to use for callees with inline hint.
Definition: InlineCost.h:158
Similar to O2 but tries to optimize for small code size instead of fast execution without triggering ...
Definition: PassBuilder.h:216
Instrumentation based profiling lowering pass.
bool registerPass(PassBuilderT &&PassBuilder)
Register an analysis pass with the manager.
Definition: PassManager.h:829
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:181
static cl::opt< bool > EnableGVNSink("enable-npm-gvn-sink", cl::init(false), cl::Hidden, cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"))
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
Definition: PassManager.h:1128
Hoist/decompose integer division and remainder instructions to enable CFG improvements and better cod...
Definition: DivRemPairs.h:23
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:592
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:140
OuterAnalysisManagerProxy< CGSCCAnalysisManager, Function > CGSCCAnalysisManagerFunctionProxy
A proxy from a CGSCCAnalysisManager to a Function.
void registerLoopAnalyses(LoopAnalysisManager &LAM)
Registers all available loop analysis passes.
The instrumentation (profile-instr-gen) pass for IR based PGO.
Key
PAL metadata keys.
A very specialized mode that will optimize for code size at any and all costs.
Definition: PassBuilder.h:225
This file provides interfaces used to build and manipulate a call graph, which is a very useful tool ...
The sample profiler data loader pass.
Definition: SampleProfile.h:25
This file provides the interface for IR based instrumentation passes ( (profile-gen, and profile-use).
bool match(StringRef String, SmallVectorImpl< StringRef > *Matches=nullptr, std::string *Error=nullptr) const
matches - Match the regex against a given String.
Definition: Regex.cpp:82
int64_t getSExtValue() const
Get sign extended value.
Definition: APInt.h:1583
static bool isModulePassName(StringRef Name, CallbacksT &Callbacks)
falkor hwpf fix Falkor HW Prefetch Fix Late Phase
This file provides the interface for a simple, fast CSE pass.
This file provides the interface for the GCOV style profiler pass.
Performs Loop Invariant Code Motion Pass.
Definition: LICM.h:45
The core GVN pass object.
Definition: GVN.h:68
void crossRegisterProxies(LoopAnalysisManager &LAM, FunctionAnalysisManager &FAM, CGSCCAnalysisManager &CGAM, ModuleAnalysisManager &MAM)
Cross register the analysis managers through their proxies.
Pass to remove unused function declarations.
Definition: GlobalDCE.h:29
A lazily constructed view of the call graph of a module.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
A pass that does profile-guided sinking of instructions into loops.
Definition: LoopSink.h:33
void registerFunctionAnalysis()
Register a specific AA result.
cl::opt< bool > EnableOrderFileInstrumentation
This class implements a trivial dead store elimination.
SimplifyCFGOptions & convertSwitchToLookupTable(bool B)
Definition: Local.h:92
Class to hold module path string table and global value map, and encapsulate methods for operating on...
Argument promotion pass.
OuterAnalysisManagerProxy< ModuleAnalysisManager, LazyCallGraph::SCC, LazyCallGraph & > ModuleAnalysisManagerCGSCCProxy
A proxy from a ModuleAnalysisManager to an SCC.
LoopUnrollOptions & setPartial(bool Partial)
Enables or disables partial unrolling.
Pass to perform interprocedural constant propagation.
Definition: SCCP.h:30
A pass which infers function attributes from the names and signatures of function declarations in a m...
cl::opt< bool > FlattenedProfileUsed
LoopVectorizeOptions & setInterleaveOnlyWhenForced(bool Value)
static cl::opt< unsigned > MaxDevirtIterations("pm-max-devirt-iterations", cl::ReallyHidden, cl::init(4))
cl::opt< bool > RunSLPVectorization
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
This is the interface for LLVM&#39;s unification-based alias analysis implemented with CFL graph reachabi...
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:432
static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level)
static bool checkParametrizedPassName(StringRef Name, StringRef PassName)
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
This is the interface for a metadata-based TBAA.
ModuleToFunctionPassAdaptor< FunctionPassT > createModuleToFunctionPassAdaptor(FunctionPassT Pass)
A function to deduce a function pass type and wrap it in the templated adaptor.
Definition: PassManager.h:1340
void addPGOInstrPassesForO0(ModulePassManager &MPM, bool DebugLogging, bool RunProfileGen, bool IsCS, std::string ProfileFile, std::string ProfileRemappingFile)
Add PGOInstrumenation passes for O0 only.
This file provides the interface for LLVM&#39;s Global Value Numbering pass which eliminates fully redund...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file provides the primary interface to the instcombine pass.
Disable as many optimizations as possible.
Definition: PassBuilder.h:150
FunctionPassManager buildFunctionSimplificationPipeline(OptimizationLevel Level, ThinLTOPhase Phase, bool DebugLogging=false)
Construct the core LLVM function canonicalization and simplification pipeline.
A manager for alias analyses.
ModulePassManager buildLTOPreLinkDefaultPipeline(OptimizationLevel Level, bool DebugLogging=false)
Build a pre-link, LTO-targeting default optimization pipeline to a pass manager.
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:390
static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks)
bool ForgetAllSCEVInLoopUnroll
Tuning option to forget all SCEV loops in LoopUnroll.
Definition: PassBuilder.h:93
The inliner pass for the new pass manager.
Definition: Inliner.h:94
cl::opt< unsigned > SetLicmMssaOptCap
void registerFunctionAnalyses(FunctionAnalysisManager &FAM)
Registers all available function analysis passes.
Analysis pass providing a never-invalidated alias analysis result.
This file provides the interface for LLVM&#39;s Global Value Numbering pass.
ThinLTOPhase
ThinLTO phase.
Definition: PassBuilder.h:133
Optimize for fast execution as much as possible.
Definition: PassBuilder.h:204
static cl::opt< bool > EnableCHR("enable-chr-npm", cl::init(true), cl::Hidden, cl::desc("Enable control height reduction optimization (CHR)"))
DevirtSCCRepeatedPass< PassT > createDevirtSCCRepeatedPass(PassT Pass, int MaxIterations)
A function to deduce a function pass type and wrap it in the templated adaptor.
A simple loop rotation transformation.
LoopUnrollOptions & setProfileBasedPeeling(int O)
static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks)
Tests whether registered callbacks will accept a given pass name.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:160
std::string InstrProfileOutput
size_t size() const
Definition: SmallVector.h:52
Create a verifier pass.
Definition: Verifier.h:136
ModulePassManager buildThinLTODefaultPipeline(OptimizationLevel Level, bool DebugLogging, const ModuleSummaryIndex *ImportSummary)
Build an ThinLTO default optimization pipeline to a pass manager.
static cl::opt< bool > EnableUnrollAndJam("enable-npm-unroll-and-jam", cl::init(false), cl::Hidden, cl::desc("Enable the Unroll and Jam pass for the new PM (default = off)"))
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:492
This file provides the interface for LLVM&#39;s Loop Data Prefetching Pass.
Provides passes to inlining "always_inline" functions.
This file provides the primary interface to the aggressive instcombine pass.
LoopUnrollOptions & setOptLevel(int O)
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
This header defines the LoopLoadEliminationPass object.
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
bool LoopInterleaving
Tuning option to set loop interleaving on/off.
Definition: PassBuilder.h:78
LoopVectorizeOptions & setVectorizeOnlyWhenForced(bool Value)
InlineParams getInlineParams()
Generate the parameters to tune the inline cost analysis based only on the commandline options...
static Optional< int > parseDevirtPassName(StringRef Name)
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
Optimize for fast execution as much as possible without triggering significant incremental compile ti...
Definition: PassBuilder.h:188
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:710
This pass performs &#39;jump threading&#39;, which looks at blocks that have multiple predecessors and multip...
Definition: JumpThreading.h:77
A pass to simplify and canonicalize the CFG of a function.
Definition: SimplifyCFG.h:29
static const Regex DefaultAliasRegex("^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$")
SimplifyCFGOptions & needCanonicalLoops(bool B)
Definition: Local.h:96
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
Error parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText, bool VerifyEachPass=true, bool DebugLogging=false)
Parse a textual pass pipeline description into a ModulePassManager.
static InlineParams getInlineParamsFromOptLevel(PassBuilder::OptimizationLevel Level)
PipelineTuningOptions()
Constructor sets pipeline tuning defaults based on cl::opts.
The instrumentation (profile-instr-gen) pass for IR based PGO.
This pass converts vector operations into scalar operations, in order to expose optimization opportun...
Class for arbitrary precision integers.
Definition: APInt.h:69
FunctionToLoopPassAdaptor< LoopPassT > createFunctionToLoopPassAdaptor(LoopPassT Pass, bool UseMemorySSA=false, bool DebugLogging=false)
A function to deduce a loop pass type and wrap it in the templated adaptor.
unsigned LicmMssaOptCap
Tuning option to cap the number of calls to retrive clobbering accesses in MemorySSA, in LICM.
Definition: PassBuilder.h:97
A pass that merges duplicate global constants into a single constant.
Definition: ConstantMerge.h:29
Analysis pass providing a never-invalidated alias analysis result.
bool LoopUnrolling
Tuning option to enable/disable loop unrolling. Its default value is true.
Definition: PassBuilder.h:89
Options for the frontend instrumentation based profiling pass.
This pass performs merges of loads and stores on both sides of a.
The indirect function call promotion pass.
Interfaces for passes which infer implicit function attributes from the name and signature of functio...
Analysis pass providing a never-invalidated alias analysis result.
LoopUnrollOptions & setRuntime(bool Runtime)
Enables or disables unrolling of loops with runtime trip count.
cl::opt< unsigned > SetLicmMssaNoAccForPromotionCap
static const size_t npos
Definition: StringRef.h:50
cl::opt< bool > EnableMSSALoopDependency
Enables memory ssa as a dependency for loop passes.
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:509
unsigned LicmMssaNoAccForPromotionCap
Tuning option to disable promotion to scalars in LICM with MemorySSA, if the number of access is too ...
Definition: PassBuilder.h:101
Pass which forces specific function attributes into the IR, primarily as a debugging tool...
LLVM_NODISCARD size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
Definition: StringRef.h:394
The header file for the LowerExpectIntrinsic pass as used by the new pass manager.
Pass to outline cold regions.
Provides passes for computing function attributes based on interprocedural analyses.
This file defines the interface for the loop cache analysis.
OuterAnalysisManagerProxy< ModuleAnalysisManager, Function > ModuleAnalysisManagerFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
Definition: PassManager.h:1257
bool consume_back(StringRef Suffix)
Returns true if this StringRef has the given suffix and removes that suffix.
Definition: StringRef.h:673
This header provides classes for managing passes over SCCs of the call graph.
This file defines passes to print out IR in various granularities.
void registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM)
Registers all available CGSCC analysis passes.
cl::opt< bool > EnableLoopVectorization
ModulePassManager buildLTODefaultPipeline(OptimizationLevel Level, bool DebugLogging, ModuleSummaryIndex *ExportSummary)
Build an LTO default optimization pipeline to a pass manager.
static bool startsWithDefaultPipelineAliasPrefix(StringRef Name)
Tests whether a pass name starts with a valid prefix for a default pipeline alias.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file provides the interface for LLVM&#39;s Call Graph Profile pass.
Optimize quickly without destroying debuggability.
Definition: PassBuilder.h:170
The profile size based optimization pass for memory intrinsics.
See the comments on JumpThreadingPass.
An SCC of the call graph.
static const char * name
Performs Loop Inst Simplify Pass.
The LoopVectorize Pass.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks)
An optimization pass providing Scalar Replacement of Aggregates.
Definition: SROA.h:64
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
This is the interface for LLVM&#39;s primary stateless and local alias analysis.
A container for analyses that lazily runs them and caches their results.
static cl::opt< bool > RunPartialInlining("enable-npm-partial-inlining", cl::init(false), cl::Hidden, cl::ZeroOrMore, cl::desc("Run Partial inlinining pass"))
A set of parameters used to control the transforms in the SimplifyCFG pass.
Definition: Local.h:63
void addPass(PassT Pass)
Definition: PassManager.h:549
This pass exposes codegen information to IR-level passes.
This pass performs function-level constant propagation and merging.
Definition: SCCP.h:36
This header defines various interfaces for pass management in LLVM.
LoopUnrollOptions & setPeeling(bool Peeling)
Enables or disables loop peeling.
static cl::opt< bool > RunNewGVN("enable-npm-newgvn", cl::init(false), cl::Hidden, cl::ZeroOrMore, cl::desc("Run NewGVN instead of GVN"))
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:71
The header file for the LowerConstantIntrinsics pass as used by the new pass manager.
bool SLPVectorization
Tuning option to enable/disable slp loop vectorization.
Definition: PassBuilder.h:86
This file provides the interface for the sampled PGO loader pass.
Optional< int > HotCallSiteThreshold
Threshold to use when the callsite is considered hot.
Definition: InlineCost.h:170
int DefaultThreshold
The default threshold to start with for a callee.
Definition: InlineCost.h:155
Optimize globals that never have their address taken.
Definition: GlobalOpt.h:25
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:143
Performs Loop Idiom Recognize Pass.
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:77
static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks)