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