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