• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 namespace llvm {
24 class ModuleSummaryIndex;
25 class Pass;
26 class TargetLibraryInfoImpl;
27 class TargetMachine;
28 
29 // The old pass manager infrastructure is hidden in a legacy namespace now.
30 namespace legacy {
31 class FunctionPassManager;
32 class 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 ///   ...
59 class PassManagerBuilder {
60 public:
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 
105   /// The Optimization Level - Specify the basic optimization level.
106   ///    0 = -O0, 1 = -O1, 2 = -O2, 3 = -O3
107   unsigned OptLevel;
108 
109   /// SizeLevel - How much we're optimizing for size.
110   ///    0 = none, 1 = -Os, 2 = -Oz
111   unsigned SizeLevel;
112 
113   /// LibraryInfo - Specifies information about the runtime library for the
114   /// optimizer.  If this is non-null, it is added to both the function and
115   /// per-module pass pipeline.
116   TargetLibraryInfoImpl *LibraryInfo;
117 
118   /// Inliner - Specifies the inliner to use.  If this is non-null, it is
119   /// added to the per-module passes.
120   Pass *Inliner;
121 
122   /// The module summary index to use for function importing.
123   const ModuleSummaryIndex *ModuleSummary;
124 
125   bool DisableTailCalls;
126   bool DisableUnitAtATime;
127   bool DisableUnrollLoops;
128   bool BBVectorize;
129   bool SLPVectorize;
130   bool LoopVectorize;
131   bool RerollLoops;
132   bool LoadCombine;
133   bool DisableGVNLoadPRE;
134   bool VerifyInput;
135   bool VerifyOutput;
136   bool MergeFunctions;
137   bool PrepareForLTO;
138   bool PrepareForThinLTO;
139   bool PerformThinLTO;
140 
141   /// Profile data file name that the instrumentation will be written to.
142   std::string PGOInstrGen;
143   /// Path of the profile data file.
144   std::string PGOInstrUse;
145 
146 private:
147   /// ExtensionList - This is list of all of the extensions that are registered.
148   std::vector<std::pair<ExtensionPointTy, ExtensionFn>> Extensions;
149 
150 public:
151   PassManagerBuilder();
152   ~PassManagerBuilder();
153   /// Adds an extension that will be used by all PassManagerBuilder instances.
154   /// This is intended to be used by plugins, to register a set of
155   /// optimisations to run automatically.
156   static void addGlobalExtension(ExtensionPointTy Ty, ExtensionFn Fn);
157   void addExtension(ExtensionPointTy Ty, ExtensionFn Fn);
158 
159 private:
160   void addExtensionsToPM(ExtensionPointTy ETy,
161                          legacy::PassManagerBase &PM) const;
162   void addInitialAliasAnalysisPasses(legacy::PassManagerBase &PM) const;
163   void addLTOOptimizationPasses(legacy::PassManagerBase &PM);
164   void addLateLTOOptimizationPasses(legacy::PassManagerBase &PM);
165   void addPGOInstrPasses(legacy::PassManagerBase &MPM);
166   void addFunctionSimplificationPasses(legacy::PassManagerBase &MPM);
167   void addInstructionCombiningPass(legacy::PassManagerBase &MPM) const;
168 
169 public:
170   /// populateFunctionPassManager - This fills in the function pass manager,
171   /// which is expected to be run on each function immediately as it is
172   /// generated.  The idea is to reduce the size of the IR in memory.
173   void populateFunctionPassManager(legacy::FunctionPassManager &FPM);
174 
175   /// populateModulePassManager - This sets up the primary pass manager.
176   void populateModulePassManager(legacy::PassManagerBase &MPM);
177   void populateLTOPassManager(legacy::PassManagerBase &PM);
178   void populateThinLTOPassManager(legacy::PassManagerBase &PM);
179 };
180 
181 /// Registers a function for adding a standard set of passes.  This should be
182 /// used by optimizer plugins to allow all front ends to transparently use
183 /// them.  Create a static instance of this class in your plugin, providing a
184 /// private function that the PassManagerBuilder can use to add your passes.
185 struct RegisterStandardPasses {
RegisterStandardPassesRegisterStandardPasses186   RegisterStandardPasses(PassManagerBuilder::ExtensionPointTy Ty,
187                          PassManagerBuilder::ExtensionFn Fn) {
188     PassManagerBuilder::addGlobalExtension(Ty, std::move(Fn));
189   }
190 };
191 
192 } // end namespace llvm
193 #endif
194