106f32e7eSjoerg //===- Parsing, selection, and construction of pass pipelines --*- C++ -*--===//
206f32e7eSjoerg //
306f32e7eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406f32e7eSjoerg // See https://llvm.org/LICENSE.txt for license information.
506f32e7eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606f32e7eSjoerg //
706f32e7eSjoerg //===----------------------------------------------------------------------===//
806f32e7eSjoerg /// \file
906f32e7eSjoerg ///
1006f32e7eSjoerg /// Interfaces for registering analysis passes, producing common pass manager
1106f32e7eSjoerg /// configurations, and parsing of pass pipelines.
1206f32e7eSjoerg ///
1306f32e7eSjoerg //===----------------------------------------------------------------------===//
1406f32e7eSjoerg 
1506f32e7eSjoerg #ifndef LLVM_PASSES_PASSBUILDER_H
1606f32e7eSjoerg #define LLVM_PASSES_PASSBUILDER_H
1706f32e7eSjoerg 
1806f32e7eSjoerg #include "llvm/ADT/Optional.h"
1906f32e7eSjoerg #include "llvm/Analysis/CGSCCPassManager.h"
2006f32e7eSjoerg #include "llvm/IR/PassManager.h"
2106f32e7eSjoerg #include "llvm/Support/Error.h"
22*da58b97aSjoerg #include "llvm/Support/raw_ostream.h"
23*da58b97aSjoerg #include "llvm/Transforms/IPO/Inliner.h"
2406f32e7eSjoerg #include "llvm/Transforms/Instrumentation.h"
2506f32e7eSjoerg #include "llvm/Transforms/Scalar/LoopPassManager.h"
2606f32e7eSjoerg #include <vector>
2706f32e7eSjoerg 
2806f32e7eSjoerg namespace llvm {
2906f32e7eSjoerg class StringRef;
3006f32e7eSjoerg class AAManager;
3106f32e7eSjoerg class TargetMachine;
3206f32e7eSjoerg class ModuleSummaryIndex;
3306f32e7eSjoerg 
3406f32e7eSjoerg /// A struct capturing PGO tunables.
3506f32e7eSjoerg struct PGOOptions {
3606f32e7eSjoerg   enum PGOAction { NoAction, IRInstr, IRUse, SampleUse };
3706f32e7eSjoerg   enum CSPGOAction { NoCSAction, CSIRInstr, CSIRUse };
3806f32e7eSjoerg   PGOOptions(std::string ProfileFile = "", std::string CSProfileGenFile = "",
3906f32e7eSjoerg              std::string ProfileRemappingFile = "", PGOAction Action = NoAction,
40*da58b97aSjoerg              CSPGOAction CSAction = NoCSAction,
41*da58b97aSjoerg              bool DebugInfoForProfiling = false,
42*da58b97aSjoerg              bool PseudoProbeForProfiling = false)
ProfileFilePGOOptions4306f32e7eSjoerg       : ProfileFile(ProfileFile), CSProfileGenFile(CSProfileGenFile),
4406f32e7eSjoerg         ProfileRemappingFile(ProfileRemappingFile), Action(Action),
45*da58b97aSjoerg         CSAction(CSAction), DebugInfoForProfiling(DebugInfoForProfiling ||
46*da58b97aSjoerg                                                   (Action == SampleUse &&
47*da58b97aSjoerg                                                    !PseudoProbeForProfiling)),
48*da58b97aSjoerg         PseudoProbeForProfiling(PseudoProbeForProfiling) {
4906f32e7eSjoerg     // Note, we do allow ProfileFile.empty() for Action=IRUse LTO can
5006f32e7eSjoerg     // callback with IRUse action without ProfileFile.
5106f32e7eSjoerg 
5206f32e7eSjoerg     // If there is a CSAction, PGOAction cannot be IRInstr or SampleUse.
5306f32e7eSjoerg     assert(this->CSAction == NoCSAction ||
5406f32e7eSjoerg            (this->Action != IRInstr && this->Action != SampleUse));
5506f32e7eSjoerg 
5606f32e7eSjoerg     // For CSIRInstr, CSProfileGenFile also needs to be nonempty.
5706f32e7eSjoerg     assert(this->CSAction != CSIRInstr || !this->CSProfileGenFile.empty());
5806f32e7eSjoerg 
5906f32e7eSjoerg     // If CSAction is CSIRUse, PGOAction needs to be IRUse as they share
6006f32e7eSjoerg     // a profile.
6106f32e7eSjoerg     assert(this->CSAction != CSIRUse || this->Action == IRUse);
6206f32e7eSjoerg 
63*da58b97aSjoerg     // If neither Action nor CSAction, DebugInfoForProfiling or
64*da58b97aSjoerg     // PseudoProbeForProfiling needs to be true.
6506f32e7eSjoerg     assert(this->Action != NoAction || this->CSAction != NoCSAction ||
66*da58b97aSjoerg            this->DebugInfoForProfiling || this->PseudoProbeForProfiling);
67*da58b97aSjoerg 
68*da58b97aSjoerg     // Pseudo probe emission does not work with -fdebug-info-for-profiling since
69*da58b97aSjoerg     // they both use the discriminator field of debug lines but for different
70*da58b97aSjoerg     // purposes.
71*da58b97aSjoerg     if (this->DebugInfoForProfiling && this->PseudoProbeForProfiling) {
72*da58b97aSjoerg       report_fatal_error(
73*da58b97aSjoerg           "Pseudo probes cannot be used with -debug-info-for-profiling", false);
74*da58b97aSjoerg     }
7506f32e7eSjoerg   }
7606f32e7eSjoerg   std::string ProfileFile;
7706f32e7eSjoerg   std::string CSProfileGenFile;
7806f32e7eSjoerg   std::string ProfileRemappingFile;
7906f32e7eSjoerg   PGOAction Action;
8006f32e7eSjoerg   CSPGOAction CSAction;
81*da58b97aSjoerg   bool DebugInfoForProfiling;
82*da58b97aSjoerg   bool PseudoProbeForProfiling;
8306f32e7eSjoerg };
8406f32e7eSjoerg 
8506f32e7eSjoerg /// Tunable parameters for passes in the default pipelines.
8606f32e7eSjoerg class PipelineTuningOptions {
8706f32e7eSjoerg public:
8806f32e7eSjoerg   /// Constructor sets pipeline tuning defaults based on cl::opts. Each option
8906f32e7eSjoerg   /// can be set in the PassBuilder when using a LLVM as a library.
9006f32e7eSjoerg   PipelineTuningOptions();
9106f32e7eSjoerg 
92*da58b97aSjoerg   /// Tuning option to set loop interleaving on/off, set based on opt level.
9306f32e7eSjoerg   bool LoopInterleaving;
9406f32e7eSjoerg 
95*da58b97aSjoerg   /// Tuning option to enable/disable loop vectorization, set based on opt
96*da58b97aSjoerg   /// level.
9706f32e7eSjoerg   bool LoopVectorization;
9806f32e7eSjoerg 
99*da58b97aSjoerg   /// Tuning option to enable/disable slp loop vectorization, set based on opt
100*da58b97aSjoerg   /// level.
10106f32e7eSjoerg   bool SLPVectorization;
10206f32e7eSjoerg 
10306f32e7eSjoerg   /// Tuning option to enable/disable loop unrolling. Its default value is true.
10406f32e7eSjoerg   bool LoopUnrolling;
10506f32e7eSjoerg 
10606f32e7eSjoerg   /// Tuning option to forget all SCEV loops in LoopUnroll. Its default value
10706f32e7eSjoerg   /// is that of the flag: `-forget-scev-loop-unroll`.
10806f32e7eSjoerg   bool ForgetAllSCEVInLoopUnroll;
10906f32e7eSjoerg 
110*da58b97aSjoerg   /// Tuning option to enable/disable coroutine intrinsic lowering. Its default
111*da58b97aSjoerg   /// value is false. Frontends such as Clang may enable this conditionally. For
112*da58b97aSjoerg   /// example, Clang enables this option if the flags `-std=c++2a` or above, or
113*da58b97aSjoerg   /// `-fcoroutines-ts`, have been specified.
114*da58b97aSjoerg   bool Coroutines;
115*da58b97aSjoerg 
11606f32e7eSjoerg   /// Tuning option to cap the number of calls to retrive clobbering accesses in
11706f32e7eSjoerg   /// MemorySSA, in LICM.
11806f32e7eSjoerg   unsigned LicmMssaOptCap;
11906f32e7eSjoerg 
12006f32e7eSjoerg   /// Tuning option to disable promotion to scalars in LICM with MemorySSA, if
12106f32e7eSjoerg   /// the number of access is too large.
12206f32e7eSjoerg   unsigned LicmMssaNoAccForPromotionCap;
123*da58b97aSjoerg 
124*da58b97aSjoerg   /// Tuning option to enable/disable call graph profile. Its default value is
125*da58b97aSjoerg   /// that of the flag: `-enable-npm-call-graph-profile`.
126*da58b97aSjoerg   bool CallGraphProfile;
127*da58b97aSjoerg 
128*da58b97aSjoerg   /// Tuning option to enable/disable function merging. Its default value is
129*da58b97aSjoerg   /// false.
130*da58b97aSjoerg   bool MergeFunctions;
13106f32e7eSjoerg };
13206f32e7eSjoerg 
13306f32e7eSjoerg /// This class provides access to building LLVM's passes.
13406f32e7eSjoerg ///
13506f32e7eSjoerg /// Its members provide the baseline state available to passes during their
13606f32e7eSjoerg /// construction. The \c PassRegistry.def file specifies how to construct all
13706f32e7eSjoerg /// of the built-in passes, and those may reference these members during
13806f32e7eSjoerg /// construction.
13906f32e7eSjoerg class PassBuilder {
14006f32e7eSjoerg   TargetMachine *TM;
14106f32e7eSjoerg   PipelineTuningOptions PTO;
14206f32e7eSjoerg   Optional<PGOOptions> PGOOpt;
14306f32e7eSjoerg   PassInstrumentationCallbacks *PIC;
14406f32e7eSjoerg 
14506f32e7eSjoerg public:
14606f32e7eSjoerg   /// A struct to capture parsed pass pipeline names.
14706f32e7eSjoerg   ///
14806f32e7eSjoerg   /// A pipeline is defined as a series of names, each of which may in itself
14906f32e7eSjoerg   /// recursively contain a nested pipeline. A name is either the name of a pass
15006f32e7eSjoerg   /// (e.g. "instcombine") or the name of a pipeline type (e.g. "cgscc"). If the
15106f32e7eSjoerg   /// name is the name of a pass, the InnerPipeline is empty, since passes
15206f32e7eSjoerg   /// cannot contain inner pipelines. See parsePassPipeline() for a more
15306f32e7eSjoerg   /// detailed description of the textual pipeline format.
15406f32e7eSjoerg   struct PipelineElement {
15506f32e7eSjoerg     StringRef Name;
15606f32e7eSjoerg     std::vector<PipelineElement> InnerPipeline;
15706f32e7eSjoerg   };
15806f32e7eSjoerg 
15906f32e7eSjoerg   /// LLVM-provided high-level optimization levels.
16006f32e7eSjoerg   ///
16106f32e7eSjoerg   /// This enumerates the LLVM-provided high-level optimization levels. Each
16206f32e7eSjoerg   /// level has a specific goal and rationale.
163*da58b97aSjoerg   class OptimizationLevel final {
164*da58b97aSjoerg     unsigned SpeedLevel = 2;
165*da58b97aSjoerg     unsigned SizeLevel = 0;
OptimizationLevel(unsigned SpeedLevel,unsigned SizeLevel)166*da58b97aSjoerg     OptimizationLevel(unsigned SpeedLevel, unsigned SizeLevel)
167*da58b97aSjoerg         : SpeedLevel(SpeedLevel), SizeLevel(SizeLevel) {
168*da58b97aSjoerg       // Check that only valid combinations are passed.
169*da58b97aSjoerg       assert(SpeedLevel <= 3 &&
170*da58b97aSjoerg              "Optimization level for speed should be 0, 1, 2, or 3");
171*da58b97aSjoerg       assert(SizeLevel <= 2 &&
172*da58b97aSjoerg              "Optimization level for size should be 0, 1, or 2");
173*da58b97aSjoerg       assert((SizeLevel == 0 || SpeedLevel == 2) &&
174*da58b97aSjoerg              "Optimize for size should be encoded with speedup level == 2");
175*da58b97aSjoerg     }
176*da58b97aSjoerg 
177*da58b97aSjoerg   public:
178*da58b97aSjoerg     OptimizationLevel() = default;
17906f32e7eSjoerg     /// Disable as many optimizations as possible. This doesn't completely
18006f32e7eSjoerg     /// disable the optimizer in all cases, for example always_inline functions
18106f32e7eSjoerg     /// can be required to be inlined for correctness.
182*da58b97aSjoerg     static const OptimizationLevel O0;
18306f32e7eSjoerg 
18406f32e7eSjoerg     /// Optimize quickly without destroying debuggability.
18506f32e7eSjoerg     ///
18606f32e7eSjoerg     /// This level is tuned to produce a result from the optimizer as quickly
18706f32e7eSjoerg     /// as possible and to avoid destroying debuggability. This tends to result
18806f32e7eSjoerg     /// in a very good development mode where the compiled code will be
18906f32e7eSjoerg     /// immediately executed as part of testing. As a consequence, where
19006f32e7eSjoerg     /// possible, we would like to produce efficient-to-execute code, but not
19106f32e7eSjoerg     /// if it significantly slows down compilation or would prevent even basic
19206f32e7eSjoerg     /// debugging of the resulting binary.
19306f32e7eSjoerg     ///
19406f32e7eSjoerg     /// As an example, complex loop transformations such as versioning,
195*da58b97aSjoerg     /// vectorization, or fusion don't make sense here due to the degree to
196*da58b97aSjoerg     /// which the executed code differs from the source code, and the compile
197*da58b97aSjoerg     /// time cost.
198*da58b97aSjoerg     static const OptimizationLevel O1;
19906f32e7eSjoerg     /// Optimize for fast execution as much as possible without triggering
20006f32e7eSjoerg     /// significant incremental compile time or code size growth.
20106f32e7eSjoerg     ///
20206f32e7eSjoerg     /// The key idea is that optimizations at this level should "pay for
20306f32e7eSjoerg     /// themselves". So if an optimization increases compile time by 5% or
20406f32e7eSjoerg     /// increases code size by 5% for a particular benchmark, that benchmark
20506f32e7eSjoerg     /// should also be one which sees a 5% runtime improvement. If the compile
20606f32e7eSjoerg     /// time or code size penalties happen on average across a diverse range of
20706f32e7eSjoerg     /// LLVM users' benchmarks, then the improvements should as well.
20806f32e7eSjoerg     ///
20906f32e7eSjoerg     /// And no matter what, the compile time needs to not grow superlinearly
21006f32e7eSjoerg     /// with the size of input to LLVM so that users can control the runtime of
21106f32e7eSjoerg     /// the optimizer in this mode.
21206f32e7eSjoerg     ///
21306f32e7eSjoerg     /// This is expected to be a good default optimization level for the vast
21406f32e7eSjoerg     /// majority of users.
215*da58b97aSjoerg     static const OptimizationLevel O2;
21606f32e7eSjoerg     /// Optimize for fast execution as much as possible.
21706f32e7eSjoerg     ///
21806f32e7eSjoerg     /// This mode is significantly more aggressive in trading off compile time
21906f32e7eSjoerg     /// and code size to get execution time improvements. The core idea is that
22006f32e7eSjoerg     /// this mode should include any optimization that helps execution time on
22106f32e7eSjoerg     /// balance across a diverse collection of benchmarks, even if it increases
22206f32e7eSjoerg     /// code size or compile time for some benchmarks without corresponding
22306f32e7eSjoerg     /// improvements to execution time.
22406f32e7eSjoerg     ///
22506f32e7eSjoerg     /// Despite being willing to trade more compile time off to get improved
22606f32e7eSjoerg     /// execution time, this mode still tries to avoid superlinear growth in
22706f32e7eSjoerg     /// order to make even significantly slower compile times at least scale
22806f32e7eSjoerg     /// reasonably. This does not preclude very substantial constant factor
22906f32e7eSjoerg     /// costs though.
230*da58b97aSjoerg     static const OptimizationLevel O3;
23106f32e7eSjoerg     /// Similar to \c O2 but tries to optimize for small code size instead of
23206f32e7eSjoerg     /// fast execution without triggering significant incremental execution
23306f32e7eSjoerg     /// time slowdowns.
23406f32e7eSjoerg     ///
23506f32e7eSjoerg     /// The logic here is exactly the same as \c O2, but with code size and
23606f32e7eSjoerg     /// execution time metrics swapped.
23706f32e7eSjoerg     ///
23806f32e7eSjoerg     /// A consequence of the different core goal is that this should in general
23906f32e7eSjoerg     /// produce substantially smaller executables that still run in
24006f32e7eSjoerg     /// a reasonable amount of time.
241*da58b97aSjoerg     static const OptimizationLevel Os;
24206f32e7eSjoerg     /// A very specialized mode that will optimize for code size at any and all
24306f32e7eSjoerg     /// costs.
24406f32e7eSjoerg     ///
24506f32e7eSjoerg     /// This is useful primarily when there are absolute size limitations and
24606f32e7eSjoerg     /// any effort taken to reduce the size is worth it regardless of the
24706f32e7eSjoerg     /// execution time impact. You should expect this level to produce rather
24806f32e7eSjoerg     /// slow, but very small, code.
249*da58b97aSjoerg     static const OptimizationLevel Oz;
250*da58b97aSjoerg 
isOptimizingForSpeed()251*da58b97aSjoerg     bool isOptimizingForSpeed() const {
252*da58b97aSjoerg       return SizeLevel == 0 && SpeedLevel > 0;
253*da58b97aSjoerg     }
254*da58b97aSjoerg 
isOptimizingForSize()255*da58b97aSjoerg     bool isOptimizingForSize() const { return SizeLevel > 0; }
256*da58b97aSjoerg 
257*da58b97aSjoerg     bool operator==(const OptimizationLevel &Other) const {
258*da58b97aSjoerg       return SizeLevel == Other.SizeLevel && SpeedLevel == Other.SpeedLevel;
259*da58b97aSjoerg     }
260*da58b97aSjoerg     bool operator!=(const OptimizationLevel &Other) const {
261*da58b97aSjoerg       return SizeLevel != Other.SizeLevel || SpeedLevel != Other.SpeedLevel;
262*da58b97aSjoerg     }
263*da58b97aSjoerg 
getSpeedupLevel()264*da58b97aSjoerg     unsigned getSpeedupLevel() const { return SpeedLevel; }
265*da58b97aSjoerg 
getSizeLevel()266*da58b97aSjoerg     unsigned getSizeLevel() const { return SizeLevel; }
26706f32e7eSjoerg   };
26806f32e7eSjoerg 
26906f32e7eSjoerg   explicit PassBuilder(TargetMachine *TM = nullptr,
27006f32e7eSjoerg                        PipelineTuningOptions PTO = PipelineTuningOptions(),
27106f32e7eSjoerg                        Optional<PGOOptions> PGOOpt = None,
272*da58b97aSjoerg                        PassInstrumentationCallbacks *PIC = nullptr);
27306f32e7eSjoerg 
27406f32e7eSjoerg   /// Cross register the analysis managers through their proxies.
27506f32e7eSjoerg   ///
27606f32e7eSjoerg   /// This is an interface that can be used to cross register each
27706f32e7eSjoerg   /// AnalysisManager with all the others analysis managers.
27806f32e7eSjoerg   void crossRegisterProxies(LoopAnalysisManager &LAM,
27906f32e7eSjoerg                             FunctionAnalysisManager &FAM,
28006f32e7eSjoerg                             CGSCCAnalysisManager &CGAM,
28106f32e7eSjoerg                             ModuleAnalysisManager &MAM);
28206f32e7eSjoerg 
28306f32e7eSjoerg   /// Registers all available module analysis passes.
28406f32e7eSjoerg   ///
28506f32e7eSjoerg   /// This is an interface that can be used to populate a \c
28606f32e7eSjoerg   /// ModuleAnalysisManager with all registered module analyses. Callers can
28706f32e7eSjoerg   /// still manually register any additional analyses. Callers can also
28806f32e7eSjoerg   /// pre-register analyses and this will not override those.
28906f32e7eSjoerg   void registerModuleAnalyses(ModuleAnalysisManager &MAM);
29006f32e7eSjoerg 
29106f32e7eSjoerg   /// Registers all available CGSCC analysis passes.
29206f32e7eSjoerg   ///
29306f32e7eSjoerg   /// This is an interface that can be used to populate a \c CGSCCAnalysisManager
29406f32e7eSjoerg   /// with all registered CGSCC analyses. Callers can still manually register any
29506f32e7eSjoerg   /// additional analyses. Callers can also pre-register analyses and this will
29606f32e7eSjoerg   /// not override those.
29706f32e7eSjoerg   void registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM);
29806f32e7eSjoerg 
29906f32e7eSjoerg   /// Registers all available function analysis passes.
30006f32e7eSjoerg   ///
30106f32e7eSjoerg   /// This is an interface that can be used to populate a \c
30206f32e7eSjoerg   /// FunctionAnalysisManager with all registered function analyses. Callers can
30306f32e7eSjoerg   /// still manually register any additional analyses. Callers can also
30406f32e7eSjoerg   /// pre-register analyses and this will not override those.
30506f32e7eSjoerg   void registerFunctionAnalyses(FunctionAnalysisManager &FAM);
30606f32e7eSjoerg 
30706f32e7eSjoerg   /// Registers all available loop analysis passes.
30806f32e7eSjoerg   ///
30906f32e7eSjoerg   /// This is an interface that can be used to populate a \c LoopAnalysisManager
31006f32e7eSjoerg   /// with all registered loop analyses. Callers can still manually register any
31106f32e7eSjoerg   /// additional analyses.
31206f32e7eSjoerg   void registerLoopAnalyses(LoopAnalysisManager &LAM);
31306f32e7eSjoerg 
31406f32e7eSjoerg   /// Construct the core LLVM function canonicalization and simplification
31506f32e7eSjoerg   /// pipeline.
31606f32e7eSjoerg   ///
31706f32e7eSjoerg   /// This is a long pipeline and uses most of the per-function optimization
31806f32e7eSjoerg   /// passes in LLVM to canonicalize and simplify the IR. It is suitable to run
31906f32e7eSjoerg   /// repeatedly over the IR and is not expected to destroy important
32006f32e7eSjoerg   /// information about the semantics of the IR.
32106f32e7eSjoerg   ///
32206f32e7eSjoerg   /// Note that \p Level cannot be `O0` here. The pipelines produced are
32306f32e7eSjoerg   /// only intended for use when attempting to optimize code. If frontends
32406f32e7eSjoerg   /// require some transformations for semantic reasons, they should explicitly
32506f32e7eSjoerg   /// build them.
32606f32e7eSjoerg   ///
32706f32e7eSjoerg   /// \p Phase indicates the current ThinLTO phase.
32806f32e7eSjoerg   FunctionPassManager
32906f32e7eSjoerg   buildFunctionSimplificationPipeline(OptimizationLevel Level,
330*da58b97aSjoerg                                       ThinOrFullLTOPhase Phase);
33106f32e7eSjoerg 
33206f32e7eSjoerg   /// Construct the core LLVM module canonicalization and simplification
33306f32e7eSjoerg   /// pipeline.
33406f32e7eSjoerg   ///
33506f32e7eSjoerg   /// This pipeline focuses on canonicalizing and simplifying the entire module
33606f32e7eSjoerg   /// of IR. Much like the function simplification pipeline above, it is
33706f32e7eSjoerg   /// suitable to run repeatedly over the IR and is not expected to destroy
33806f32e7eSjoerg   /// important information. It does, however, perform inlining and other
33906f32e7eSjoerg   /// heuristic based simplifications that are not strictly reversible.
34006f32e7eSjoerg   ///
34106f32e7eSjoerg   /// Note that \p Level cannot be `O0` here. The pipelines produced are
34206f32e7eSjoerg   /// only intended for use when attempting to optimize code. If frontends
34306f32e7eSjoerg   /// require some transformations for semantic reasons, they should explicitly
34406f32e7eSjoerg   /// build them.
34506f32e7eSjoerg   ///
34606f32e7eSjoerg   /// \p Phase indicates the current ThinLTO phase.
347*da58b97aSjoerg   ModulePassManager buildModuleSimplificationPipeline(OptimizationLevel Level,
348*da58b97aSjoerg                                                       ThinOrFullLTOPhase Phase);
349*da58b97aSjoerg 
350*da58b97aSjoerg   /// Construct the module pipeline that performs inlining as well as
351*da58b97aSjoerg   /// the inlining-driven cleanups.
352*da58b97aSjoerg   ModuleInlinerWrapperPass buildInlinerPipeline(OptimizationLevel Level,
353*da58b97aSjoerg                                                 ThinOrFullLTOPhase Phase);
35406f32e7eSjoerg 
35506f32e7eSjoerg   /// Construct the core LLVM module optimization pipeline.
35606f32e7eSjoerg   ///
35706f32e7eSjoerg   /// This pipeline focuses on optimizing the execution speed of the IR. It
35806f32e7eSjoerg   /// uses cost modeling and thresholds to balance code growth against runtime
35906f32e7eSjoerg   /// improvements. It includes vectorization and other information destroying
36006f32e7eSjoerg   /// transformations. It also cannot generally be run repeatedly on a module
36106f32e7eSjoerg   /// without potentially seriously regressing either runtime performance of
36206f32e7eSjoerg   /// the code or serious code size growth.
36306f32e7eSjoerg   ///
36406f32e7eSjoerg   /// Note that \p Level cannot be `O0` here. The pipelines produced are
36506f32e7eSjoerg   /// only intended for use when attempting to optimize code. If frontends
36606f32e7eSjoerg   /// require some transformations for semantic reasons, they should explicitly
36706f32e7eSjoerg   /// build them.
36806f32e7eSjoerg   ModulePassManager buildModuleOptimizationPipeline(OptimizationLevel Level,
36906f32e7eSjoerg                                                     bool LTOPreLink = false);
37006f32e7eSjoerg 
37106f32e7eSjoerg   /// Build a per-module default optimization pipeline.
37206f32e7eSjoerg   ///
37306f32e7eSjoerg   /// This provides a good default optimization pipeline for per-module
37406f32e7eSjoerg   /// optimization and code generation without any link-time optimization. It
37506f32e7eSjoerg   /// typically correspond to frontend "-O[123]" options for optimization
37606f32e7eSjoerg   /// levels \c O1, \c O2 and \c O3 resp.
37706f32e7eSjoerg   ///
37806f32e7eSjoerg   /// Note that \p Level cannot be `O0` here. The pipelines produced are
37906f32e7eSjoerg   /// only intended for use when attempting to optimize code. If frontends
38006f32e7eSjoerg   /// require some transformations for semantic reasons, they should explicitly
38106f32e7eSjoerg   /// build them.
38206f32e7eSjoerg   ModulePassManager buildPerModuleDefaultPipeline(OptimizationLevel Level,
38306f32e7eSjoerg                                                   bool LTOPreLink = false);
38406f32e7eSjoerg 
38506f32e7eSjoerg   /// Build a pre-link, ThinLTO-targeting default optimization pipeline to
38606f32e7eSjoerg   /// a pass manager.
38706f32e7eSjoerg   ///
38806f32e7eSjoerg   /// This adds the pre-link optimizations tuned to prepare a module for
38906f32e7eSjoerg   /// a ThinLTO run. It works to minimize the IR which needs to be analyzed
39006f32e7eSjoerg   /// without making irreversible decisions which could be made better during
39106f32e7eSjoerg   /// the LTO run.
39206f32e7eSjoerg   ///
39306f32e7eSjoerg   /// Note that \p Level cannot be `O0` here. The pipelines produced are
39406f32e7eSjoerg   /// only intended for use when attempting to optimize code. If frontends
39506f32e7eSjoerg   /// require some transformations for semantic reasons, they should explicitly
39606f32e7eSjoerg   /// build them.
397*da58b97aSjoerg   ModulePassManager buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level);
39806f32e7eSjoerg 
39906f32e7eSjoerg   /// Build an ThinLTO default optimization pipeline to a pass manager.
40006f32e7eSjoerg   ///
40106f32e7eSjoerg   /// This provides a good default optimization pipeline for link-time
40206f32e7eSjoerg   /// optimization and code generation. It is particularly tuned to fit well
40306f32e7eSjoerg   /// when IR coming into the LTO phase was first run through \c
40406f32e7eSjoerg   /// addPreLinkLTODefaultPipeline, and the two coordinate closely.
40506f32e7eSjoerg   ///
40606f32e7eSjoerg   /// Note that \p Level cannot be `O0` here. The pipelines produced are
40706f32e7eSjoerg   /// only intended for use when attempting to optimize code. If frontends
40806f32e7eSjoerg   /// require some transformations for semantic reasons, they should explicitly
40906f32e7eSjoerg   /// build them.
41006f32e7eSjoerg   ModulePassManager
411*da58b97aSjoerg   buildThinLTODefaultPipeline(OptimizationLevel Level,
41206f32e7eSjoerg                               const ModuleSummaryIndex *ImportSummary);
41306f32e7eSjoerg 
41406f32e7eSjoerg   /// Build a pre-link, LTO-targeting default optimization pipeline to a pass
41506f32e7eSjoerg   /// manager.
41606f32e7eSjoerg   ///
41706f32e7eSjoerg   /// This adds the pre-link optimizations tuned to work well with a later LTO
41806f32e7eSjoerg   /// run. It works to minimize the IR which needs to be analyzed without
41906f32e7eSjoerg   /// making irreversible decisions which could be made better during the LTO
42006f32e7eSjoerg   /// run.
42106f32e7eSjoerg   ///
42206f32e7eSjoerg   /// Note that \p Level cannot be `O0` here. The pipelines produced are
42306f32e7eSjoerg   /// only intended for use when attempting to optimize code. If frontends
42406f32e7eSjoerg   /// require some transformations for semantic reasons, they should explicitly
42506f32e7eSjoerg   /// build them.
426*da58b97aSjoerg   ModulePassManager buildLTOPreLinkDefaultPipeline(OptimizationLevel Level);
42706f32e7eSjoerg 
42806f32e7eSjoerg   /// Build an LTO default optimization pipeline to a pass manager.
42906f32e7eSjoerg   ///
43006f32e7eSjoerg   /// This provides a good default optimization pipeline for link-time
43106f32e7eSjoerg   /// optimization and code generation. It is particularly tuned to fit well
43206f32e7eSjoerg   /// when IR coming into the LTO phase was first run through \c
43306f32e7eSjoerg   /// addPreLinkLTODefaultPipeline, and the two coordinate closely.
43406f32e7eSjoerg   ///
43506f32e7eSjoerg   /// Note that \p Level cannot be `O0` here. The pipelines produced are
43606f32e7eSjoerg   /// only intended for use when attempting to optimize code. If frontends
43706f32e7eSjoerg   /// require some transformations for semantic reasons, they should explicitly
43806f32e7eSjoerg   /// build them.
43906f32e7eSjoerg   ModulePassManager buildLTODefaultPipeline(OptimizationLevel Level,
44006f32e7eSjoerg                                             ModuleSummaryIndex *ExportSummary);
44106f32e7eSjoerg 
442*da58b97aSjoerg   /// Build an O0 pipeline with the minimal semantically required passes.
443*da58b97aSjoerg   ///
444*da58b97aSjoerg   /// This should only be used for non-LTO and LTO pre-link pipelines.
445*da58b97aSjoerg   ModulePassManager buildO0DefaultPipeline(OptimizationLevel Level,
446*da58b97aSjoerg                                            bool LTOPreLink = false);
447*da58b97aSjoerg 
44806f32e7eSjoerg   /// Build the default `AAManager` with the default alias analysis pipeline
44906f32e7eSjoerg   /// registered.
450*da58b97aSjoerg   ///
451*da58b97aSjoerg   /// This also adds target-specific alias analyses registered via
452*da58b97aSjoerg   /// TargetMachine::registerDefaultAliasAnalyses().
45306f32e7eSjoerg   AAManager buildDefaultAAPipeline();
45406f32e7eSjoerg 
45506f32e7eSjoerg   /// Parse a textual pass pipeline description into a \c
45606f32e7eSjoerg   /// ModulePassManager.
45706f32e7eSjoerg   ///
45806f32e7eSjoerg   /// The format of the textual pass pipeline description looks something like:
45906f32e7eSjoerg   ///
46006f32e7eSjoerg   ///   module(function(instcombine,sroa),dce,cgscc(inliner,function(...)),...)
46106f32e7eSjoerg   ///
46206f32e7eSjoerg   /// Pass managers have ()s describing the nest structure of passes. All passes
46306f32e7eSjoerg   /// are comma separated. As a special shortcut, if the very first pass is not
46406f32e7eSjoerg   /// a module pass (as a module pass manager is), this will automatically form
46506f32e7eSjoerg   /// the shortest stack of pass managers that allow inserting that first pass.
46606f32e7eSjoerg   /// So, assuming function passes 'fpassN', CGSCC passes 'cgpassN', and loop
46706f32e7eSjoerg   /// passes 'lpassN', all of these are valid:
46806f32e7eSjoerg   ///
46906f32e7eSjoerg   ///   fpass1,fpass2,fpass3
47006f32e7eSjoerg   ///   cgpass1,cgpass2,cgpass3
47106f32e7eSjoerg   ///   lpass1,lpass2,lpass3
47206f32e7eSjoerg   ///
47306f32e7eSjoerg   /// And they are equivalent to the following (resp.):
47406f32e7eSjoerg   ///
47506f32e7eSjoerg   ///   module(function(fpass1,fpass2,fpass3))
47606f32e7eSjoerg   ///   module(cgscc(cgpass1,cgpass2,cgpass3))
47706f32e7eSjoerg   ///   module(function(loop(lpass1,lpass2,lpass3)))
47806f32e7eSjoerg   ///
47906f32e7eSjoerg   /// This shortcut is especially useful for debugging and testing small pass
480*da58b97aSjoerg   /// combinations.
481*da58b97aSjoerg   ///
482*da58b97aSjoerg   /// The sequence of passes aren't necessarily the exact same kind of pass.
483*da58b97aSjoerg   /// You can mix different levels implicitly if adaptor passes are defined to
484*da58b97aSjoerg   /// make them work. For example,
485*da58b97aSjoerg   ///
486*da58b97aSjoerg   ///   mpass1,fpass1,fpass2,mpass2,lpass1
487*da58b97aSjoerg   ///
488*da58b97aSjoerg   /// This pipeline uses only one pass manager: the top-level module manager.
489*da58b97aSjoerg   /// fpass1,fpass2 and lpass1 are added into the the top-level module manager
490*da58b97aSjoerg   /// using only adaptor passes. No nested function/loop pass managers are
491*da58b97aSjoerg   /// added. The purpose is to allow easy pass testing when the user
492*da58b97aSjoerg   /// specifically want the pass to run under a adaptor directly. This is
493*da58b97aSjoerg   /// preferred when a pipeline is largely of one type, but one or just a few
494*da58b97aSjoerg   /// passes are of different types(See PassBuilder.cpp for examples).
495*da58b97aSjoerg   Error parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText);
49606f32e7eSjoerg 
49706f32e7eSjoerg   /// {{@ Parse a textual pass pipeline description into a specific PassManager
49806f32e7eSjoerg   ///
49906f32e7eSjoerg   /// Automatic deduction of an appropriate pass manager stack is not supported.
50006f32e7eSjoerg   /// For example, to insert a loop pass 'lpass' into a FunctionPassManager,
50106f32e7eSjoerg   /// this is the valid pipeline text:
50206f32e7eSjoerg   ///
50306f32e7eSjoerg   ///   function(lpass)
504*da58b97aSjoerg   Error parsePassPipeline(CGSCCPassManager &CGPM, StringRef PipelineText);
505*da58b97aSjoerg   Error parsePassPipeline(FunctionPassManager &FPM, StringRef PipelineText);
506*da58b97aSjoerg   Error parsePassPipeline(LoopPassManager &LPM, StringRef PipelineText);
50706f32e7eSjoerg   /// @}}
50806f32e7eSjoerg 
50906f32e7eSjoerg   /// Parse a textual alias analysis pipeline into the provided AA manager.
51006f32e7eSjoerg   ///
51106f32e7eSjoerg   /// The format of the textual AA pipeline is a comma separated list of AA
51206f32e7eSjoerg   /// pass names:
51306f32e7eSjoerg   ///
51406f32e7eSjoerg   ///   basic-aa,globals-aa,...
51506f32e7eSjoerg   ///
51606f32e7eSjoerg   /// The AA manager is set up such that the provided alias analyses are tried
51706f32e7eSjoerg   /// in the order specified. See the \c AAManaager documentation for details
51806f32e7eSjoerg   /// about the logic used. This routine just provides the textual mapping
51906f32e7eSjoerg   /// between AA names and the analyses to register with the manager.
52006f32e7eSjoerg   ///
52106f32e7eSjoerg   /// Returns false if the text cannot be parsed cleanly. The specific state of
52206f32e7eSjoerg   /// the \p AA manager is unspecified if such an error is encountered and this
52306f32e7eSjoerg   /// returns false.
52406f32e7eSjoerg   Error parseAAPipeline(AAManager &AA, StringRef PipelineText);
52506f32e7eSjoerg 
526*da58b97aSjoerg   /// Returns true if the pass name is the name of an alias analysis pass.
527*da58b97aSjoerg   bool isAAPassName(StringRef PassName);
528*da58b97aSjoerg 
529*da58b97aSjoerg   /// Returns true if the pass name is the name of a (non-alias) analysis pass.
530*da58b97aSjoerg   bool isAnalysisPassName(StringRef PassName);
531*da58b97aSjoerg 
532*da58b97aSjoerg   /// Print pass names.
533*da58b97aSjoerg   void printPassNames(raw_ostream &OS);
534*da58b97aSjoerg 
53506f32e7eSjoerg   /// Register a callback for a default optimizer pipeline extension
53606f32e7eSjoerg   /// point
53706f32e7eSjoerg   ///
53806f32e7eSjoerg   /// This extension point allows adding passes that perform peephole
53906f32e7eSjoerg   /// optimizations similar to the instruction combiner. These passes will be
54006f32e7eSjoerg   /// inserted after each instance of the instruction combiner pass.
registerPeepholeEPCallback(const std::function<void (FunctionPassManager &,OptimizationLevel)> & C)54106f32e7eSjoerg   void registerPeepholeEPCallback(
54206f32e7eSjoerg       const std::function<void(FunctionPassManager &, OptimizationLevel)> &C) {
54306f32e7eSjoerg     PeepholeEPCallbacks.push_back(C);
54406f32e7eSjoerg   }
54506f32e7eSjoerg 
54606f32e7eSjoerg   /// Register a callback for a default optimizer pipeline extension
54706f32e7eSjoerg   /// point
54806f32e7eSjoerg   ///
54906f32e7eSjoerg   /// This extension point allows adding late loop canonicalization and
55006f32e7eSjoerg   /// simplification passes. This is the last point in the loop optimization
55106f32e7eSjoerg   /// pipeline before loop deletion. Each pass added
55206f32e7eSjoerg   /// here must be an instance of LoopPass.
55306f32e7eSjoerg   /// This is the place to add passes that can remove loops, such as target-
55406f32e7eSjoerg   /// specific loop idiom recognition.
registerLateLoopOptimizationsEPCallback(const std::function<void (LoopPassManager &,OptimizationLevel)> & C)55506f32e7eSjoerg   void registerLateLoopOptimizationsEPCallback(
55606f32e7eSjoerg       const std::function<void(LoopPassManager &, OptimizationLevel)> &C) {
55706f32e7eSjoerg     LateLoopOptimizationsEPCallbacks.push_back(C);
55806f32e7eSjoerg   }
55906f32e7eSjoerg 
56006f32e7eSjoerg   /// Register a callback for a default optimizer pipeline extension
56106f32e7eSjoerg   /// point
56206f32e7eSjoerg   ///
56306f32e7eSjoerg   /// This extension point allows adding loop passes to the end of the loop
56406f32e7eSjoerg   /// optimizer.
registerLoopOptimizerEndEPCallback(const std::function<void (LoopPassManager &,OptimizationLevel)> & C)56506f32e7eSjoerg   void registerLoopOptimizerEndEPCallback(
56606f32e7eSjoerg       const std::function<void(LoopPassManager &, OptimizationLevel)> &C) {
56706f32e7eSjoerg     LoopOptimizerEndEPCallbacks.push_back(C);
56806f32e7eSjoerg   }
56906f32e7eSjoerg 
57006f32e7eSjoerg   /// Register a callback for a default optimizer pipeline extension
57106f32e7eSjoerg   /// point
57206f32e7eSjoerg   ///
57306f32e7eSjoerg   /// This extension point allows adding optimization passes after most of the
57406f32e7eSjoerg   /// main optimizations, but before the last cleanup-ish optimizations.
registerScalarOptimizerLateEPCallback(const std::function<void (FunctionPassManager &,OptimizationLevel)> & C)57506f32e7eSjoerg   void registerScalarOptimizerLateEPCallback(
57606f32e7eSjoerg       const std::function<void(FunctionPassManager &, OptimizationLevel)> &C) {
57706f32e7eSjoerg     ScalarOptimizerLateEPCallbacks.push_back(C);
57806f32e7eSjoerg   }
57906f32e7eSjoerg 
58006f32e7eSjoerg   /// Register a callback for a default optimizer pipeline extension
58106f32e7eSjoerg   /// point
58206f32e7eSjoerg   ///
58306f32e7eSjoerg   /// This extension point allows adding CallGraphSCC passes at the end of the
58406f32e7eSjoerg   /// main CallGraphSCC passes and before any function simplification passes run
58506f32e7eSjoerg   /// by CGPassManager.
registerCGSCCOptimizerLateEPCallback(const std::function<void (CGSCCPassManager &,OptimizationLevel)> & C)58606f32e7eSjoerg   void registerCGSCCOptimizerLateEPCallback(
58706f32e7eSjoerg       const std::function<void(CGSCCPassManager &, OptimizationLevel)> &C) {
58806f32e7eSjoerg     CGSCCOptimizerLateEPCallbacks.push_back(C);
58906f32e7eSjoerg   }
59006f32e7eSjoerg 
59106f32e7eSjoerg   /// Register a callback for a default optimizer pipeline extension
59206f32e7eSjoerg   /// point
59306f32e7eSjoerg   ///
59406f32e7eSjoerg   /// This extension point allows adding optimization passes before the
59506f32e7eSjoerg   /// vectorizer and other highly target specific optimization passes are
59606f32e7eSjoerg   /// executed.
registerVectorizerStartEPCallback(const std::function<void (FunctionPassManager &,OptimizationLevel)> & C)59706f32e7eSjoerg   void registerVectorizerStartEPCallback(
59806f32e7eSjoerg       const std::function<void(FunctionPassManager &, OptimizationLevel)> &C) {
59906f32e7eSjoerg     VectorizerStartEPCallbacks.push_back(C);
60006f32e7eSjoerg   }
60106f32e7eSjoerg 
60206f32e7eSjoerg   /// Register a callback for a default optimizer pipeline extension point.
60306f32e7eSjoerg   ///
60406f32e7eSjoerg   /// This extension point allows adding optimization once at the start of the
60506f32e7eSjoerg   /// pipeline. This does not apply to 'backend' compiles (LTO and ThinLTO
60606f32e7eSjoerg   /// link-time pipelines).
registerPipelineStartEPCallback(const std::function<void (ModulePassManager &,OptimizationLevel)> & C)60706f32e7eSjoerg   void registerPipelineStartEPCallback(
608*da58b97aSjoerg       const std::function<void(ModulePassManager &, OptimizationLevel)> &C) {
60906f32e7eSjoerg     PipelineStartEPCallbacks.push_back(C);
61006f32e7eSjoerg   }
61106f32e7eSjoerg 
612*da58b97aSjoerg   /// Register a callback for a default optimizer pipeline extension point.
613*da58b97aSjoerg   ///
614*da58b97aSjoerg   /// This extension point allows adding optimization right after passes that do
615*da58b97aSjoerg   /// basic simplification of the input IR.
registerPipelineEarlySimplificationEPCallback(const std::function<void (ModulePassManager &,OptimizationLevel)> & C)616*da58b97aSjoerg   void registerPipelineEarlySimplificationEPCallback(
617*da58b97aSjoerg       const std::function<void(ModulePassManager &, OptimizationLevel)> &C) {
618*da58b97aSjoerg     PipelineEarlySimplificationEPCallbacks.push_back(C);
619*da58b97aSjoerg   }
620*da58b97aSjoerg 
62106f32e7eSjoerg   /// Register a callback for a default optimizer pipeline extension point
62206f32e7eSjoerg   ///
62306f32e7eSjoerg   /// This extension point allows adding optimizations at the very end of the
624*da58b97aSjoerg   /// function optimization pipeline.
registerOptimizerLastEPCallback(const std::function<void (ModulePassManager &,OptimizationLevel)> & C)62506f32e7eSjoerg   void registerOptimizerLastEPCallback(
626*da58b97aSjoerg       const std::function<void(ModulePassManager &, OptimizationLevel)> &C) {
62706f32e7eSjoerg     OptimizerLastEPCallbacks.push_back(C);
62806f32e7eSjoerg   }
62906f32e7eSjoerg 
63006f32e7eSjoerg   /// Register a callback for parsing an AliasAnalysis Name to populate
63106f32e7eSjoerg   /// the given AAManager \p AA
registerParseAACallback(const std::function<bool (StringRef Name,AAManager & AA)> & C)63206f32e7eSjoerg   void registerParseAACallback(
63306f32e7eSjoerg       const std::function<bool(StringRef Name, AAManager &AA)> &C) {
63406f32e7eSjoerg     AAParsingCallbacks.push_back(C);
63506f32e7eSjoerg   }
63606f32e7eSjoerg 
63706f32e7eSjoerg   /// {{@ Register callbacks for analysis registration with this PassBuilder
63806f32e7eSjoerg   /// instance.
63906f32e7eSjoerg   /// Callees register their analyses with the given AnalysisManager objects.
registerAnalysisRegistrationCallback(const std::function<void (CGSCCAnalysisManager &)> & C)64006f32e7eSjoerg   void registerAnalysisRegistrationCallback(
64106f32e7eSjoerg       const std::function<void(CGSCCAnalysisManager &)> &C) {
64206f32e7eSjoerg     CGSCCAnalysisRegistrationCallbacks.push_back(C);
64306f32e7eSjoerg   }
registerAnalysisRegistrationCallback(const std::function<void (FunctionAnalysisManager &)> & C)64406f32e7eSjoerg   void registerAnalysisRegistrationCallback(
64506f32e7eSjoerg       const std::function<void(FunctionAnalysisManager &)> &C) {
64606f32e7eSjoerg     FunctionAnalysisRegistrationCallbacks.push_back(C);
64706f32e7eSjoerg   }
registerAnalysisRegistrationCallback(const std::function<void (LoopAnalysisManager &)> & C)64806f32e7eSjoerg   void registerAnalysisRegistrationCallback(
64906f32e7eSjoerg       const std::function<void(LoopAnalysisManager &)> &C) {
65006f32e7eSjoerg     LoopAnalysisRegistrationCallbacks.push_back(C);
65106f32e7eSjoerg   }
registerAnalysisRegistrationCallback(const std::function<void (ModuleAnalysisManager &)> & C)65206f32e7eSjoerg   void registerAnalysisRegistrationCallback(
65306f32e7eSjoerg       const std::function<void(ModuleAnalysisManager &)> &C) {
65406f32e7eSjoerg     ModuleAnalysisRegistrationCallbacks.push_back(C);
65506f32e7eSjoerg   }
65606f32e7eSjoerg   /// @}}
65706f32e7eSjoerg 
65806f32e7eSjoerg   /// {{@ Register pipeline parsing callbacks with this pass builder instance.
65906f32e7eSjoerg   /// Using these callbacks, callers can parse both a single pass name, as well
66006f32e7eSjoerg   /// as entire sub-pipelines, and populate the PassManager instance
66106f32e7eSjoerg   /// accordingly.
registerPipelineParsingCallback(const std::function<bool (StringRef Name,CGSCCPassManager &,ArrayRef<PipelineElement>)> & C)66206f32e7eSjoerg   void registerPipelineParsingCallback(
66306f32e7eSjoerg       const std::function<bool(StringRef Name, CGSCCPassManager &,
66406f32e7eSjoerg                                ArrayRef<PipelineElement>)> &C) {
66506f32e7eSjoerg     CGSCCPipelineParsingCallbacks.push_back(C);
66606f32e7eSjoerg   }
registerPipelineParsingCallback(const std::function<bool (StringRef Name,FunctionPassManager &,ArrayRef<PipelineElement>)> & C)66706f32e7eSjoerg   void registerPipelineParsingCallback(
66806f32e7eSjoerg       const std::function<bool(StringRef Name, FunctionPassManager &,
66906f32e7eSjoerg                                ArrayRef<PipelineElement>)> &C) {
67006f32e7eSjoerg     FunctionPipelineParsingCallbacks.push_back(C);
67106f32e7eSjoerg   }
registerPipelineParsingCallback(const std::function<bool (StringRef Name,LoopPassManager &,ArrayRef<PipelineElement>)> & C)67206f32e7eSjoerg   void registerPipelineParsingCallback(
67306f32e7eSjoerg       const std::function<bool(StringRef Name, LoopPassManager &,
67406f32e7eSjoerg                                ArrayRef<PipelineElement>)> &C) {
67506f32e7eSjoerg     LoopPipelineParsingCallbacks.push_back(C);
67606f32e7eSjoerg   }
registerPipelineParsingCallback(const std::function<bool (StringRef Name,ModulePassManager &,ArrayRef<PipelineElement>)> & C)67706f32e7eSjoerg   void registerPipelineParsingCallback(
67806f32e7eSjoerg       const std::function<bool(StringRef Name, ModulePassManager &,
67906f32e7eSjoerg                                ArrayRef<PipelineElement>)> &C) {
68006f32e7eSjoerg     ModulePipelineParsingCallbacks.push_back(C);
68106f32e7eSjoerg   }
68206f32e7eSjoerg   /// @}}
68306f32e7eSjoerg 
68406f32e7eSjoerg   /// Register a callback for a top-level pipeline entry.
68506f32e7eSjoerg   ///
68606f32e7eSjoerg   /// If the PassManager type is not given at the top level of the pipeline
68706f32e7eSjoerg   /// text, this Callback should be used to determine the appropriate stack of
68806f32e7eSjoerg   /// PassManagers and populate the passed ModulePassManager.
68906f32e7eSjoerg   void registerParseTopLevelPipelineCallback(
690*da58b97aSjoerg       const std::function<bool(ModulePassManager &, ArrayRef<PipelineElement>)>
691*da58b97aSjoerg           &C);
69206f32e7eSjoerg 
69306f32e7eSjoerg   /// Add PGOInstrumenation passes for O0 only.
694*da58b97aSjoerg   void addPGOInstrPassesForO0(ModulePassManager &MPM, bool RunProfileGen,
695*da58b97aSjoerg                               bool IsCS, std::string ProfileFile,
69606f32e7eSjoerg                               std::string ProfileRemappingFile);
69706f32e7eSjoerg 
698*da58b97aSjoerg   /// Returns PIC. External libraries can use this to register pass
699*da58b97aSjoerg   /// instrumentation callbacks.
getPassInstrumentationCallbacks()700*da58b97aSjoerg   PassInstrumentationCallbacks *getPassInstrumentationCallbacks() const {
701*da58b97aSjoerg     return PIC;
702*da58b97aSjoerg   }
703*da58b97aSjoerg 
70406f32e7eSjoerg private:
705*da58b97aSjoerg   // O1 pass pipeline
706*da58b97aSjoerg   FunctionPassManager
707*da58b97aSjoerg   buildO1FunctionSimplificationPipeline(OptimizationLevel Level,
708*da58b97aSjoerg                                         ThinOrFullLTOPhase Phase);
709*da58b97aSjoerg 
710*da58b97aSjoerg   void addRequiredLTOPreLinkPasses(ModulePassManager &MPM);
711*da58b97aSjoerg 
712*da58b97aSjoerg   void addVectorPasses(OptimizationLevel Level, FunctionPassManager &FPM,
713*da58b97aSjoerg                        bool IsLTO);
714*da58b97aSjoerg 
71506f32e7eSjoerg   static Optional<std::vector<PipelineElement>>
71606f32e7eSjoerg   parsePipelineText(StringRef Text);
71706f32e7eSjoerg 
718*da58b97aSjoerg   Error parseModulePass(ModulePassManager &MPM, const PipelineElement &E);
719*da58b97aSjoerg   Error parseCGSCCPass(CGSCCPassManager &CGPM, const PipelineElement &E);
720*da58b97aSjoerg   Error parseFunctionPass(FunctionPassManager &FPM, const PipelineElement &E);
721*da58b97aSjoerg   Error parseLoopPass(LoopPassManager &LPM, const PipelineElement &E);
72206f32e7eSjoerg   bool parseAAPassName(AAManager &AA, StringRef Name);
72306f32e7eSjoerg 
72406f32e7eSjoerg   Error parseLoopPassPipeline(LoopPassManager &LPM,
725*da58b97aSjoerg                               ArrayRef<PipelineElement> Pipeline);
72606f32e7eSjoerg   Error parseFunctionPassPipeline(FunctionPassManager &FPM,
727*da58b97aSjoerg                                   ArrayRef<PipelineElement> Pipeline);
72806f32e7eSjoerg   Error parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
729*da58b97aSjoerg                                ArrayRef<PipelineElement> Pipeline);
73006f32e7eSjoerg   Error parseModulePassPipeline(ModulePassManager &MPM,
731*da58b97aSjoerg                                 ArrayRef<PipelineElement> Pipeline);
73206f32e7eSjoerg 
733*da58b97aSjoerg   void addPGOInstrPasses(ModulePassManager &MPM, OptimizationLevel Level,
734*da58b97aSjoerg                          bool RunProfileGen, bool IsCS, std::string ProfileFile,
73506f32e7eSjoerg                          std::string ProfileRemappingFile);
73606f32e7eSjoerg   void invokePeepholeEPCallbacks(FunctionPassManager &, OptimizationLevel);
73706f32e7eSjoerg 
73806f32e7eSjoerg   // Extension Point callbacks
73906f32e7eSjoerg   SmallVector<std::function<void(FunctionPassManager &, OptimizationLevel)>, 2>
74006f32e7eSjoerg       PeepholeEPCallbacks;
74106f32e7eSjoerg   SmallVector<std::function<void(LoopPassManager &, OptimizationLevel)>, 2>
74206f32e7eSjoerg       LateLoopOptimizationsEPCallbacks;
74306f32e7eSjoerg   SmallVector<std::function<void(LoopPassManager &, OptimizationLevel)>, 2>
74406f32e7eSjoerg       LoopOptimizerEndEPCallbacks;
74506f32e7eSjoerg   SmallVector<std::function<void(FunctionPassManager &, OptimizationLevel)>, 2>
74606f32e7eSjoerg       ScalarOptimizerLateEPCallbacks;
74706f32e7eSjoerg   SmallVector<std::function<void(CGSCCPassManager &, OptimizationLevel)>, 2>
74806f32e7eSjoerg       CGSCCOptimizerLateEPCallbacks;
74906f32e7eSjoerg   SmallVector<std::function<void(FunctionPassManager &, OptimizationLevel)>, 2>
75006f32e7eSjoerg       VectorizerStartEPCallbacks;
751*da58b97aSjoerg   SmallVector<std::function<void(ModulePassManager &, OptimizationLevel)>, 2>
75206f32e7eSjoerg       OptimizerLastEPCallbacks;
75306f32e7eSjoerg   // Module callbacks
754*da58b97aSjoerg   SmallVector<std::function<void(ModulePassManager &, OptimizationLevel)>, 2>
75506f32e7eSjoerg       PipelineStartEPCallbacks;
756*da58b97aSjoerg   SmallVector<std::function<void(ModulePassManager &, OptimizationLevel)>, 2>
757*da58b97aSjoerg       PipelineEarlySimplificationEPCallbacks;
758*da58b97aSjoerg 
75906f32e7eSjoerg   SmallVector<std::function<void(ModuleAnalysisManager &)>, 2>
76006f32e7eSjoerg       ModuleAnalysisRegistrationCallbacks;
76106f32e7eSjoerg   SmallVector<std::function<bool(StringRef, ModulePassManager &,
76206f32e7eSjoerg                                  ArrayRef<PipelineElement>)>,
76306f32e7eSjoerg               2>
76406f32e7eSjoerg       ModulePipelineParsingCallbacks;
765*da58b97aSjoerg   SmallVector<
766*da58b97aSjoerg       std::function<bool(ModulePassManager &, ArrayRef<PipelineElement>)>, 2>
76706f32e7eSjoerg       TopLevelPipelineParsingCallbacks;
76806f32e7eSjoerg   // CGSCC callbacks
76906f32e7eSjoerg   SmallVector<std::function<void(CGSCCAnalysisManager &)>, 2>
77006f32e7eSjoerg       CGSCCAnalysisRegistrationCallbacks;
77106f32e7eSjoerg   SmallVector<std::function<bool(StringRef, CGSCCPassManager &,
77206f32e7eSjoerg                                  ArrayRef<PipelineElement>)>,
77306f32e7eSjoerg               2>
77406f32e7eSjoerg       CGSCCPipelineParsingCallbacks;
77506f32e7eSjoerg   // Function callbacks
77606f32e7eSjoerg   SmallVector<std::function<void(FunctionAnalysisManager &)>, 2>
77706f32e7eSjoerg       FunctionAnalysisRegistrationCallbacks;
77806f32e7eSjoerg   SmallVector<std::function<bool(StringRef, FunctionPassManager &,
77906f32e7eSjoerg                                  ArrayRef<PipelineElement>)>,
78006f32e7eSjoerg               2>
78106f32e7eSjoerg       FunctionPipelineParsingCallbacks;
78206f32e7eSjoerg   // Loop callbacks
78306f32e7eSjoerg   SmallVector<std::function<void(LoopAnalysisManager &)>, 2>
78406f32e7eSjoerg       LoopAnalysisRegistrationCallbacks;
78506f32e7eSjoerg   SmallVector<std::function<bool(StringRef, LoopPassManager &,
78606f32e7eSjoerg                                  ArrayRef<PipelineElement>)>,
78706f32e7eSjoerg               2>
78806f32e7eSjoerg       LoopPipelineParsingCallbacks;
78906f32e7eSjoerg   // AA callbacks
79006f32e7eSjoerg   SmallVector<std::function<bool(StringRef Name, AAManager &AA)>, 2>
79106f32e7eSjoerg       AAParsingCallbacks;
79206f32e7eSjoerg };
79306f32e7eSjoerg 
79406f32e7eSjoerg /// This utility template takes care of adding require<> and invalidate<>
79506f32e7eSjoerg /// passes for an analysis to a given \c PassManager. It is intended to be used
79606f32e7eSjoerg /// during parsing of a pass pipeline when parsing a single PipelineName.
79706f32e7eSjoerg /// When registering a new function analysis FancyAnalysis with the pass
79806f32e7eSjoerg /// pipeline name "fancy-analysis", a matching ParsePipelineCallback could look
79906f32e7eSjoerg /// like this:
80006f32e7eSjoerg ///
80106f32e7eSjoerg /// static bool parseFunctionPipeline(StringRef Name, FunctionPassManager &FPM,
80206f32e7eSjoerg ///                                   ArrayRef<PipelineElement> P) {
80306f32e7eSjoerg ///   if (parseAnalysisUtilityPasses<FancyAnalysis>("fancy-analysis", Name,
80406f32e7eSjoerg ///                                                 FPM))
80506f32e7eSjoerg ///     return true;
80606f32e7eSjoerg ///   return false;
80706f32e7eSjoerg /// }
80806f32e7eSjoerg template <typename AnalysisT, typename IRUnitT, typename AnalysisManagerT,
80906f32e7eSjoerg           typename... ExtraArgTs>
parseAnalysisUtilityPasses(StringRef AnalysisName,StringRef PipelineName,PassManager<IRUnitT,AnalysisManagerT,ExtraArgTs...> & PM)81006f32e7eSjoerg bool parseAnalysisUtilityPasses(
81106f32e7eSjoerg     StringRef AnalysisName, StringRef PipelineName,
81206f32e7eSjoerg     PassManager<IRUnitT, AnalysisManagerT, ExtraArgTs...> &PM) {
81306f32e7eSjoerg   if (!PipelineName.endswith(">"))
81406f32e7eSjoerg     return false;
81506f32e7eSjoerg   // See if this is an invalidate<> pass name
81606f32e7eSjoerg   if (PipelineName.startswith("invalidate<")) {
81706f32e7eSjoerg     PipelineName = PipelineName.substr(11, PipelineName.size() - 12);
81806f32e7eSjoerg     if (PipelineName != AnalysisName)
81906f32e7eSjoerg       return false;
82006f32e7eSjoerg     PM.addPass(InvalidateAnalysisPass<AnalysisT>());
82106f32e7eSjoerg     return true;
82206f32e7eSjoerg   }
82306f32e7eSjoerg 
82406f32e7eSjoerg   // See if this is a require<> pass name
82506f32e7eSjoerg   if (PipelineName.startswith("require<")) {
82606f32e7eSjoerg     PipelineName = PipelineName.substr(8, PipelineName.size() - 9);
82706f32e7eSjoerg     if (PipelineName != AnalysisName)
82806f32e7eSjoerg       return false;
82906f32e7eSjoerg     PM.addPass(RequireAnalysisPass<AnalysisT, IRUnitT, AnalysisManagerT,
83006f32e7eSjoerg                                    ExtraArgTs...>());
83106f32e7eSjoerg     return true;
83206f32e7eSjoerg   }
83306f32e7eSjoerg 
83406f32e7eSjoerg   return false;
83506f32e7eSjoerg }
83606f32e7eSjoerg }
83706f32e7eSjoerg 
83806f32e7eSjoerg #endif
839