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