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