1 // llvm/Transforms/IPO/PassManagerBuilder.h - Build Standard Pass -*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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++.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TRANSFORMS_IPO_PASSMANAGERBUILDER_H
16 #define LLVM_TRANSFORMS_IPO_PASSMANAGERBUILDER_H
24 class ModuleSummaryIndex;
26 class TargetLibraryInfoImpl;
29 // The old pass manager infrastructure is hidden in a legacy namespace now.
31 class FunctionPassManager;
32 class PassManagerBase;
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:
39 /// PassManagerBuilder Builder;
40 /// Builder.OptLevel = 2;
41 /// Builder.populateFunctionPassManager(FPM);
42 /// Builder.populateModulePassManager(MPM);
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:
51 /// static void addMyLoopPass(const PMBuilder &Builder, PassManagerBase &PM) {
52 /// if (Builder.getOptLevel() > 2 && Builder.getOptSizeLevel() == 0)
53 /// PM.add(createMyAwesomePass());
56 /// Builder.addExtension(PassManagerBuilder::EP_LoopOptimizerEnd,
59 class PassManagerBuilder {
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)>
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.
72 /// EP_ModuleOptimizerEarly - This extension point allows adding passes
73 /// just before the main module-level optimization passes.
74 EP_ModuleOptimizerEarly,
76 /// EP_LoopOptimizerEnd - This extension point allows adding loop passes to
77 /// the end of the loop optimizer.
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,
85 /// EP_OptimizerLast -- This extension point allows adding passes that
86 /// run after everything else.
89 /// EP_VectorizerStart - This extension point allows adding optimization
90 /// passes before the vectorizer and other highly target specific
91 /// optimization passes are executed.
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,
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.
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,
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,
118 /// The Optimization Level - Specify the basic optimization level.
119 /// 0 = -O0, 1 = -O1, 2 = -O2, 3 = -O3
122 /// SizeLevel - How much we're optimizing for size.
123 /// 0 = none, 1 = -Os, 2 = -Oz
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;
131 /// Inliner - Specifies the inliner to use. If this is non-null, it is
132 /// added to the per-module passes.
135 /// The module summary index to use for exporting information from the
136 /// regular LTO phase, for example for the CFI and devirtualization type
138 ModuleSummaryIndex *ExportSummary = nullptr;
140 /// The module summary index to use for importing information to the
141 /// thin LTO backends, for example for the CFI and devirtualization type
143 const ModuleSummaryIndex *ImportSummary = nullptr;
145 bool DisableTailCalls;
146 bool DisableUnitAtATime;
147 bool DisableUnrollLoops;
152 bool DisableGVNLoadPRE;
157 bool PrepareForThinLTO;
159 bool DivergentTarget;
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;
171 /// ExtensionList - This is list of all of the extensions that are registered.
172 std::vector<std::pair<ExtensionPointTy, ExtensionFn>> Extensions;
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);
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;
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);
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);
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.
209 struct RegisterStandardPasses {
210 RegisterStandardPasses(PassManagerBuilder::ExtensionPointTy Ty,
211 PassManagerBuilder::ExtensionFn Fn) {
212 PassManagerBuilder::addGlobalExtension(Ty, std::move(Fn));
216 } // end namespace llvm