1 // llvm/Transforms/IPO/PassManagerBuilder.h - Build Standard Pass -*- C++ -*-=//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the PassManagerBuilder class, which is used to set up a
10 // "standard" optimization sequence suitable for languages like C and C++.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TRANSFORMS_IPO_PASSMANAGERBUILDER_H
15 #define LLVM_TRANSFORMS_IPO_PASSMANAGERBUILDER_H
23 class ModuleSummaryIndex
;
25 class TargetLibraryInfoImpl
;
28 // The old pass manager infrastructure is hidden in a legacy namespace now.
30 class FunctionPassManager
;
31 class PassManagerBase
;
34 /// PassManagerBuilder - This class is used to set up a standard optimization
35 /// sequence for languages like C and C++, allowing some APIs to customize the
36 /// pass sequence in various ways. A simple example of using it would be:
38 /// PassManagerBuilder Builder;
39 /// Builder.OptLevel = 2;
40 /// Builder.populateFunctionPassManager(FPM);
41 /// Builder.populateModulePassManager(MPM);
43 /// In addition to setting up the basic passes, PassManagerBuilder allows
44 /// frontends to vend a plugin API, where plugins are allowed to add extensions
45 /// to the default pass manager. They do this by specifying where in the pass
46 /// pipeline they want to be added, along with a callback function that adds
47 /// the pass(es). For example, a plugin that wanted to add a loop optimization
48 /// could do something like this:
50 /// static void addMyLoopPass(const PMBuilder &Builder, PassManagerBase &PM) {
51 /// if (Builder.getOptLevel() > 2 && Builder.getOptSizeLevel() == 0)
52 /// PM.add(createMyAwesomePass());
55 /// Builder.addExtension(PassManagerBuilder::EP_LoopOptimizerEnd,
58 class PassManagerBuilder
{
60 /// Extensions are passed to the builder itself (so they can see how it is
61 /// configured) as well as the pass manager to add stuff to.
62 typedef std::function
<void(const PassManagerBuilder
&Builder
,
63 legacy::PassManagerBase
&PM
)>
65 enum ExtensionPointTy
{
66 /// EP_EarlyAsPossible - This extension point allows adding passes before
67 /// any other transformations, allowing them to see the code as it is coming
68 /// out of the frontend.
71 /// EP_ModuleOptimizerEarly - This extension point allows adding passes
72 /// just before the main module-level optimization passes.
73 EP_ModuleOptimizerEarly
,
75 /// EP_LoopOptimizerEnd - This extension point allows adding loop passes to
76 /// the end of the loop optimizer.
79 /// EP_ScalarOptimizerLate - This extension point allows adding optimization
80 /// passes after most of the main optimizations, but before the last
81 /// cleanup-ish optimizations.
82 EP_ScalarOptimizerLate
,
84 /// EP_OptimizerLast -- This extension point allows adding passes that
85 /// run after everything else.
88 /// EP_VectorizerStart - This extension point allows adding optimization
89 /// passes before the vectorizer and other highly target specific
90 /// optimization passes are executed.
93 /// EP_EnabledOnOptLevel0 - This extension point allows adding passes that
94 /// should not be disabled by O0 optimization level. The passes will be
95 /// inserted after the inlining pass.
96 EP_EnabledOnOptLevel0
,
98 /// EP_Peephole - This extension point allows adding passes that perform
99 /// peephole optimizations similar to the instruction combiner. These passes
100 /// will be inserted after each instance of the instruction combiner pass.
103 /// EP_LateLoopOptimizations - This extension point allows adding late loop
104 /// canonicalization and simplification passes. This is the last point in
105 /// the loop optimization pipeline before loop deletion. Each pass added
106 /// here must be an instance of LoopPass.
107 /// This is the place to add passes that can remove loops, such as target-
108 /// specific loop idiom recognition.
109 EP_LateLoopOptimizations
,
111 /// EP_CGSCCOptimizerLate - This extension point allows adding CallGraphSCC
112 /// passes at the end of the main CallGraphSCC passes and before any
113 /// function simplification passes run by CGPassManager.
114 EP_CGSCCOptimizerLate
,
116 /// EP_FullLinkTimeOptimizationEarly - This extensions point allow adding
118 /// run at Link Time, before Full Link Time Optimization.
119 EP_FullLinkTimeOptimizationEarly
,
121 /// EP_FullLinkTimeOptimizationLast - This extensions point allow adding
123 /// run at Link Time, after Full Link Time Optimization.
124 EP_FullLinkTimeOptimizationLast
,
127 /// The Optimization Level - Specify the basic optimization level.
128 /// 0 = -O0, 1 = -O1, 2 = -O2, 3 = -O3
131 /// SizeLevel - How much we're optimizing for size.
132 /// 0 = none, 1 = -Os, 2 = -Oz
135 /// LibraryInfo - Specifies information about the runtime library for the
136 /// optimizer. If this is non-null, it is added to both the function and
137 /// per-module pass pipeline.
138 TargetLibraryInfoImpl
*LibraryInfo
;
140 /// Inliner - Specifies the inliner to use. If this is non-null, it is
141 /// added to the per-module passes.
144 /// The module summary index to use for exporting information from the
145 /// regular LTO phase, for example for the CFI and devirtualization type
147 ModuleSummaryIndex
*ExportSummary
= nullptr;
149 /// The module summary index to use for importing information to the
150 /// thin LTO backends, for example for the CFI and devirtualization type
152 const ModuleSummaryIndex
*ImportSummary
= nullptr;
154 bool DisableTailCalls
;
155 bool DisableUnrollLoops
;
158 bool LoopsInterleaved
;
161 bool DisableGVNLoadPRE
;
162 bool ForgetAllSCEVInLoopUnroll
;
167 bool PrepareForThinLTO
;
169 bool DivergentTarget
;
170 unsigned LicmMssaOptCap
;
171 unsigned LicmMssaNoAccForPromotionCap
;
173 /// Enable profile instrumentation pass.
174 bool EnablePGOInstrGen
;
175 /// Enable profile context sensitive instrumentation pass.
176 bool EnablePGOCSInstrGen
;
177 /// Enable profile context sensitive profile use pass.
178 bool EnablePGOCSInstrUse
;
179 /// Profile data file name that the instrumentation will be written to.
180 std::string PGOInstrGen
;
181 /// Path of the profile data file.
182 std::string PGOInstrUse
;
183 /// Path of the sample Profile data file.
184 std::string PGOSampleUse
;
187 /// ExtensionList - This is list of all of the extensions that are registered.
188 std::vector
<std::pair
<ExtensionPointTy
, ExtensionFn
>> Extensions
;
191 PassManagerBuilder();
192 ~PassManagerBuilder();
193 /// Adds an extension that will be used by all PassManagerBuilder instances.
194 /// This is intended to be used by plugins, to register a set of
195 /// optimisations to run automatically.
196 static void addGlobalExtension(ExtensionPointTy Ty
, ExtensionFn Fn
);
197 void addExtension(ExtensionPointTy Ty
, ExtensionFn Fn
);
200 void addExtensionsToPM(ExtensionPointTy ETy
,
201 legacy::PassManagerBase
&PM
) const;
202 void addInitialAliasAnalysisPasses(legacy::PassManagerBase
&PM
) const;
203 void addLTOOptimizationPasses(legacy::PassManagerBase
&PM
);
204 void addLateLTOOptimizationPasses(legacy::PassManagerBase
&PM
);
205 void addPGOInstrPasses(legacy::PassManagerBase
&MPM
, bool IsCS
);
206 void addFunctionSimplificationPasses(legacy::PassManagerBase
&MPM
);
207 void addInstructionCombiningPass(legacy::PassManagerBase
&MPM
) const;
210 /// populateFunctionPassManager - This fills in the function pass manager,
211 /// which is expected to be run on each function immediately as it is
212 /// generated. The idea is to reduce the size of the IR in memory.
213 void populateFunctionPassManager(legacy::FunctionPassManager
&FPM
);
215 /// populateModulePassManager - This sets up the primary pass manager.
216 void populateModulePassManager(legacy::PassManagerBase
&MPM
);
217 void populateLTOPassManager(legacy::PassManagerBase
&PM
);
218 void populateThinLTOPassManager(legacy::PassManagerBase
&PM
);
221 /// Registers a function for adding a standard set of passes. This should be
222 /// used by optimizer plugins to allow all front ends to transparently use
223 /// them. Create a static instance of this class in your plugin, providing a
224 /// private function that the PassManagerBuilder can use to add your passes.
225 struct RegisterStandardPasses
{
226 RegisterStandardPasses(PassManagerBuilder::ExtensionPointTy Ty
,
227 PassManagerBuilder::ExtensionFn Fn
) {
228 PassManagerBuilder::addGlobalExtension(Ty
, std::move(Fn
));
232 } // end namespace llvm