1 // llvm/Transforms/IPO/PassManagerBuilder.h - Build Standard Pass -*- C++ -*-=//
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 //
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++.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_TRANSFORMS_IPO_PASSMANAGERBUILDER_H
15 #define LLVM_TRANSFORMS_IPO_PASSMANAGERBUILDER_H
16
17 #include "llvm-c/Transforms/PassManagerBuilder.h"
18 #include <functional>
19 #include <string>
20 #include <vector>
21
22 namespace llvm {
23 class ModuleSummaryIndex;
24 class Pass;
25 class TargetLibraryInfoImpl;
26
27 // The old pass manager infrastructure is hidden in a legacy namespace now.
28 namespace legacy {
29 class FunctionPassManager;
30 class PassManagerBase;
31 }
32
33 /// PassManagerBuilder - This class is used to set up a standard optimization
34 /// sequence for languages like C and C++, allowing some APIs to customize the
35 /// pass sequence in various ways. A simple example of using it would be:
36 ///
37 /// PassManagerBuilder Builder;
38 /// Builder.OptLevel = 2;
39 /// Builder.populateFunctionPassManager(FPM);
40 /// Builder.populateModulePassManager(MPM);
41 ///
42 /// In addition to setting up the basic passes, PassManagerBuilder allows
43 /// frontends to vend a plugin API, where plugins are allowed to add extensions
44 /// to the default pass manager. They do this by specifying where in the pass
45 /// pipeline they want to be added, along with a callback function that adds
46 /// the pass(es). For example, a plugin that wanted to add a loop optimization
47 /// could do something like this:
48 ///
49 /// static void addMyLoopPass(const PMBuilder &Builder, PassManagerBase &PM) {
50 /// if (Builder.getOptLevel() > 2 && Builder.getOptSizeLevel() == 0)
51 /// PM.add(createMyAwesomePass());
52 /// }
53 /// ...
54 /// Builder.addExtension(PassManagerBuilder::EP_LoopOptimizerEnd,
55 /// addMyLoopPass);
56 /// ...
57 class PassManagerBuilder {
58 public:
59 /// Extensions are passed to the builder itself (so they can see how it is
60 /// configured) as well as the pass manager to add stuff to.
61 typedef std::function<void(const PassManagerBuilder &Builder,
62 legacy::PassManagerBase &PM)>
63 ExtensionFn;
64 typedef int GlobalExtensionID;
65
66 enum ExtensionPointTy {
67 /// EP_EarlyAsPossible - This extension point allows adding passes before
68 /// any other transformations, allowing them to see the code as it is coming
69 /// out of the frontend.
70 EP_EarlyAsPossible,
71
72 /// EP_ModuleOptimizerEarly - This extension point allows adding passes
73 /// just before the main module-level optimization passes.
74 EP_ModuleOptimizerEarly,
75
76 /// EP_LoopOptimizerEnd - This extension point allows adding loop passes to
77 /// the end of the loop optimizer.
78 EP_LoopOptimizerEnd,
79
80 /// EP_ScalarOptimizerLate - This extension point allows adding optimization
81 /// passes after most of the main optimizations, but before the last
82 /// cleanup-ish optimizations.
83 EP_ScalarOptimizerLate,
84
85 /// EP_OptimizerLast -- This extension point allows adding passes that
86 /// run after everything else.
87 EP_OptimizerLast,
88
89 /// EP_VectorizerStart - This extension point allows adding optimization
90 /// passes before the vectorizer and other highly target specific
91 /// optimization passes are executed.
92 EP_VectorizerStart,
93
94 /// EP_EnabledOnOptLevel0 - This extension point allows adding passes that
95 /// should not be disabled by O0 optimization level. The passes will be
96 /// inserted after the inlining pass.
97 EP_EnabledOnOptLevel0,
98
99 /// EP_Peephole - This extension point allows adding passes that perform
100 /// peephole optimizations similar to the instruction combiner. These passes
101 /// will be inserted after each instance of the instruction combiner pass.
102 EP_Peephole,
103
104 /// EP_LateLoopOptimizations - This extension point allows adding late loop
105 /// canonicalization and simplification passes. This is the last point in
106 /// the loop optimization pipeline before loop deletion. Each pass added
107 /// here must be an instance of LoopPass.
108 /// This is the place to add passes that can remove loops, such as target-
109 /// specific loop idiom recognition.
110 EP_LateLoopOptimizations,
111
112 /// EP_CGSCCOptimizerLate - This extension point allows adding CallGraphSCC
113 /// passes at the end of the main CallGraphSCC passes and before any
114 /// function simplification passes run by CGPassManager.
115 EP_CGSCCOptimizerLate,
116
117 /// EP_FullLinkTimeOptimizationEarly - This extensions point allow adding
118 /// passes that
119 /// run at Link Time, before Full Link Time Optimization.
120 EP_FullLinkTimeOptimizationEarly,
121
122 /// EP_FullLinkTimeOptimizationLast - This extensions point allow adding
123 /// passes that
124 /// run at Link Time, after Full Link Time Optimization.
125 EP_FullLinkTimeOptimizationLast,
126 };
127
128 /// The Optimization Level - Specify the basic optimization level.
129 /// 0 = -O0, 1 = -O1, 2 = -O2, 3 = -O3
130 unsigned OptLevel;
131
132 /// SizeLevel - How much we're optimizing for size.
133 /// 0 = none, 1 = -Os, 2 = -Oz
134 unsigned SizeLevel;
135
136 /// LibraryInfo - Specifies information about the runtime library for the
137 /// optimizer. If this is non-null, it is added to both the function and
138 /// per-module pass pipeline.
139 TargetLibraryInfoImpl *LibraryInfo;
140
141 /// Inliner - Specifies the inliner to use. If this is non-null, it is
142 /// added to the per-module passes.
143 Pass *Inliner;
144
145 /// The module summary index to use for exporting information from the
146 /// regular LTO phase, for example for the CFI and devirtualization type
147 /// tests.
148 ModuleSummaryIndex *ExportSummary = nullptr;
149
150 /// The module summary index to use for importing information to the
151 /// thin LTO backends, for example for the CFI and devirtualization type
152 /// tests.
153 const ModuleSummaryIndex *ImportSummary = nullptr;
154
155 bool DisableUnrollLoops;
156 bool CallGraphProfile;
157 bool SLPVectorize;
158 bool LoopVectorize;
159 bool LoopsInterleaved;
160 bool RerollLoops;
161 bool NewGVN;
162 bool DisableGVNLoadPRE;
163 bool ForgetAllSCEVInLoopUnroll;
164 bool VerifyInput;
165 bool VerifyOutput;
166 bool MergeFunctions;
167 bool DivergentTarget;
168 unsigned LicmMssaOptCap;
169 unsigned LicmMssaNoAccForPromotionCap;
170
171 private:
172 /// ExtensionList - This is list of all of the extensions that are registered.
173 std::vector<std::pair<ExtensionPointTy, ExtensionFn>> Extensions;
174
175 public:
176 PassManagerBuilder();
177 ~PassManagerBuilder();
178 /// Adds an extension that will be used by all PassManagerBuilder instances.
179 /// This is intended to be used by plugins, to register a set of
180 /// optimisations to run automatically.
181 ///
182 /// \returns A global extension identifier that can be used to remove the
183 /// extension.
184 static GlobalExtensionID addGlobalExtension(ExtensionPointTy Ty,
185 ExtensionFn Fn);
186 /// Removes an extension that was previously added using addGlobalExtension.
187 /// This is also intended to be used by plugins, to remove any extension that
188 /// was previously registered before being unloaded.
189 ///
190 /// \param ExtensionID Identifier of the extension to be removed.
191 static void removeGlobalExtension(GlobalExtensionID ExtensionID);
192 void addExtension(ExtensionPointTy Ty, ExtensionFn Fn);
193
194 private:
195 void addExtensionsToPM(ExtensionPointTy ETy,
196 legacy::PassManagerBase &PM) const;
197 void addInitialAliasAnalysisPasses(legacy::PassManagerBase &PM) const;
198 void addFunctionSimplificationPasses(legacy::PassManagerBase &MPM);
199 void addVectorPasses(legacy::PassManagerBase &PM, bool IsFullLTO);
200
201 public:
202 /// populateFunctionPassManager - This fills in the function pass manager,
203 /// which is expected to be run on each function immediately as it is
204 /// generated. The idea is to reduce the size of the IR in memory.
205 void populateFunctionPassManager(legacy::FunctionPassManager &FPM);
206
207 /// populateModulePassManager - This sets up the primary pass manager.
208 void populateModulePassManager(legacy::PassManagerBase &MPM);
209 };
210
211 /// Registers a function for adding a standard set of passes. This should be
212 /// used by optimizer plugins to allow all front ends to transparently use
213 /// them. Create a static instance of this class in your plugin, providing a
214 /// private function that the PassManagerBuilder can use to add your passes.
215 class RegisterStandardPasses {
216 PassManagerBuilder::GlobalExtensionID ExtensionID;
217
218 public:
RegisterStandardPasses(PassManagerBuilder::ExtensionPointTy Ty,PassManagerBuilder::ExtensionFn Fn)219 RegisterStandardPasses(PassManagerBuilder::ExtensionPointTy Ty,
220 PassManagerBuilder::ExtensionFn Fn) {
221 ExtensionID = PassManagerBuilder::addGlobalExtension(Ty, std::move(Fn));
222 }
223
~RegisterStandardPasses()224 ~RegisterStandardPasses() {
225 // If the collection holding the global extensions is destroyed after the
226 // plugin is unloaded, the extension has to be removed here. Indeed, the
227 // destructor of the ExtensionFn may reference code in the plugin.
228 PassManagerBuilder::removeGlobalExtension(ExtensionID);
229 }
230 };
231
unwrap(LLVMPassManagerBuilderRef P)232 inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
233 return reinterpret_cast<PassManagerBuilder*>(P);
234 }
235
wrap(PassManagerBuilder * P)236 inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
237 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
238 }
239
240 } // end namespace llvm
241 #endif
242