1// llvm/Transforms/IPO/PassManagerBuilder.h - Build Standard Pass -*- C++ -*-=//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PassManagerBuilder class, which is used to set up a
11// "standard" optimization sequence suitable for languages like C and C++.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_TRANSFORMS_IPO_PASSMANAGERBUILDER_H
16#define LLVM_TRANSFORMS_IPO_PASSMANAGERBUILDER_H
17
18#include <functional>
19#include <memory>
20#include <string>
21#include <vector>
22
23namespace llvm {
24class ModuleSummaryIndex;
25class Pass;
26class TargetLibraryInfoImpl;
27class TargetMachine;
28
29// The old pass manager infrastructure is hidden in a legacy namespace now.
30namespace legacy {
31class FunctionPassManager;
32class PassManagerBase;
33}
34
35/// PassManagerBuilder - This class is used to set up a standard optimization
36/// sequence for languages like C and C++, allowing some APIs to customize the
37/// pass sequence in various ways. A simple example of using it would be:
38///
39/// PassManagerBuilder Builder;
40/// Builder.OptLevel = 2;
41/// Builder.populateFunctionPassManager(FPM);
42/// Builder.populateModulePassManager(MPM);
43///
44/// In addition to setting up the basic passes, PassManagerBuilder allows
45/// frontends to vend a plugin API, where plugins are allowed to add extensions
46/// to the default pass manager. They do this by specifying where in the pass
47/// pipeline they want to be added, along with a callback function that adds
48/// the pass(es). For example, a plugin that wanted to add a loop optimization
49/// could do something like this:
50///
51/// static void addMyLoopPass(const PMBuilder &Builder, PassManagerBase &PM) {
52/// if (Builder.getOptLevel() > 2 && Builder.getOptSizeLevel() == 0)
53/// PM.add(createMyAwesomePass());
54/// }
55/// ...
56/// Builder.addExtension(PassManagerBuilder::EP_LoopOptimizerEnd,
57/// addMyLoopPass);
58/// ...
59class PassManagerBuilder {
60public:
61 /// Extensions are passed the builder itself (so they can see how it is
62 /// configured) as well as the pass manager to add stuff to.
63 typedef std::function<void(const PassManagerBuilder &Builder,
64 legacy::PassManagerBase &PM)>
65 ExtensionFn;
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
118 /// The Optimization Level - Specify the basic optimization level.
119 /// 0 = -O0, 1 = -O1, 2 = -O2, 3 = -O3
120 unsigned OptLevel;
121
122 /// SizeLevel - How much we're optimizing for size.
123 /// 0 = none, 1 = -Os, 2 = -Oz
124 unsigned SizeLevel;
125
126 /// LibraryInfo - Specifies information about the runtime library for the
127 /// optimizer. If this is non-null, it is added to both the function and
128 /// per-module pass pipeline.
129 TargetLibraryInfoImpl *LibraryInfo;
130
131 /// Inliner - Specifies the inliner to use. If this is non-null, it is
132 /// added to the per-module passes.
133 Pass *Inliner;
134
135 /// The module summary index to use for exporting information from the
136 /// regular LTO phase, for example for the CFI and devirtualization type
137 /// tests.
138 ModuleSummaryIndex *ExportSummary = nullptr;
139
140 /// The module summary index to use for importing information to the
141 /// thin LTO backends, for example for the CFI and devirtualization type
142 /// tests.
143 const ModuleSummaryIndex *ImportSummary = nullptr;
144
145 bool DisableTailCalls;
146 bool DisableUnitAtATime;
147 bool DisableUnrollLoops;
148 bool SLPVectorize;
149 bool LoopVectorize;
150 bool RerollLoops;
151 bool NewGVN;
152 bool DisableGVNLoadPRE;
153 bool VerifyInput;
154 bool VerifyOutput;
155 bool MergeFunctions;
156 bool PrepareForLTO;
157 bool PrepareForThinLTO;
158 bool PerformThinLTO;
159 bool DivergentTarget;
160
161 /// Enable profile instrumentation pass.
162 bool EnablePGOInstrGen;
163 /// Profile data file name that the instrumentation will be written to.
164 std::string PGOInstrGen;
165 /// Path of the profile data file.
166 std::string PGOInstrUse;
167 /// Path of the sample Profile data file.
168 std::string PGOSampleUse;
169
170private:
171 /// ExtensionList - This is list of all of the extensions that are registered.
172 std::vector<std::pair<ExtensionPointTy, ExtensionFn>> Extensions;
173
174public:
175 PassManagerBuilder();
176 ~PassManagerBuilder();
177 /// Adds an extension that will be used by all PassManagerBuilder instances.
178 /// This is intended to be used by plugins, to register a set of
179 /// optimisations to run automatically.
180 static void addGlobalExtension(ExtensionPointTy Ty, ExtensionFn Fn);
181 void addExtension(ExtensionPointTy Ty, ExtensionFn Fn);
182
183private:
184 void addExtensionsToPM(ExtensionPointTy ETy,
185 legacy::PassManagerBase &PM) const;
186 void addInitialAliasAnalysisPasses(legacy::PassManagerBase &PM) const;
187 void addLTOOptimizationPasses(legacy::PassManagerBase &PM);
188 void addLateLTOOptimizationPasses(legacy::PassManagerBase &PM);
189 void addPGOInstrPasses(legacy::PassManagerBase &MPM);
190 void addFunctionSimplificationPasses(legacy::PassManagerBase &MPM);
191 void addInstructionCombiningPass(legacy::PassManagerBase &MPM) const;
192
193public:
194 /// populateFunctionPassManager - This fills in the function pass manager,
195 /// which is expected to be run on each function immediately as it is
196 /// generated. The idea is to reduce the size of the IR in memory.
197 void populateFunctionPassManager(legacy::FunctionPassManager &FPM);
198
199 /// populateModulePassManager - This sets up the primary pass manager.
200 void populateModulePassManager(legacy::PassManagerBase &MPM);
201 void populateLTOPassManager(legacy::PassManagerBase &PM);
202 void populateThinLTOPassManager(legacy::PassManagerBase &PM);
203};
204
205/// Registers a function for adding a standard set of passes. This should be
206/// used by optimizer plugins to allow all front ends to transparently use
207/// them. Create a static instance of this class in your plugin, providing a
208/// private function that the PassManagerBuilder can use to add your passes.
209struct RegisterStandardPasses {
210 RegisterStandardPasses(PassManagerBuilder::ExtensionPointTy Ty,
211 PassManagerBuilder::ExtensionFn Fn) {
212 PassManagerBuilder::addGlobalExtension(Ty, std::move(Fn));
213 }
214};
215
216} // end namespace llvm
217#endif
218