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 <memory>
19 #include <vector>
20 
21 namespace llvm {
22 class FunctionInfoIndex;
23 class Pass;
24 class TargetLibraryInfoImpl;
25 class TargetMachine;
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 the builder itself (so they can see how it is
60   /// configured) as well as the pass manager to add stuff to.
61   typedef void (*ExtensionFn)(const PassManagerBuilder &Builder,
62                               legacy::PassManagerBase &PM);
63   enum ExtensionPointTy {
64     /// EP_EarlyAsPossible - This extension point allows adding passes before
65     /// any other transformations, allowing them to see the code as it is coming
66     /// out of the frontend.
67     EP_EarlyAsPossible,
68 
69     /// EP_ModuleOptimizerEarly - This extension point allows adding passes
70     /// just before the main module-level optimization passes.
71     EP_ModuleOptimizerEarly,
72 
73     /// EP_LoopOptimizerEnd - This extension point allows adding loop passes to
74     /// the end of the loop optimizer.
75     EP_LoopOptimizerEnd,
76 
77     /// EP_ScalarOptimizerLate - This extension point allows adding optimization
78     /// passes after most of the main optimizations, but before the last
79     /// cleanup-ish optimizations.
80     EP_ScalarOptimizerLate,
81 
82     /// EP_OptimizerLast -- This extension point allows adding passes that
83     /// run after everything else.
84     EP_OptimizerLast,
85 
86     /// EP_VectorizerStart - This extension point allows adding optimization
87     /// passes before the vectorizer and other highly target specific
88     /// optimization passes are executed.
89     EP_VectorizerStart,
90 
91     /// EP_EnabledOnOptLevel0 - This extension point allows adding passes that
92     /// should not be disabled by O0 optimization level. The passes will be
93     /// inserted after the inlining pass.
94     EP_EnabledOnOptLevel0,
95 
96     /// EP_Peephole - This extension point allows adding passes that perform
97     /// peephole optimizations similar to the instruction combiner. These passes
98     /// will be inserted after each instance of the instruction combiner pass.
99     EP_Peephole,
100   };
101 
102   /// The Optimization Level - Specify the basic optimization level.
103   ///    0 = -O0, 1 = -O1, 2 = -O2, 3 = -O3
104   unsigned OptLevel;
105 
106   /// SizeLevel - How much we're optimizing for size.
107   ///    0 = none, 1 = -Os, 2 = -Oz
108   unsigned SizeLevel;
109 
110   /// LibraryInfo - Specifies information about the runtime library for the
111   /// optimizer.  If this is non-null, it is added to both the function and
112   /// per-module pass pipeline.
113   TargetLibraryInfoImpl *LibraryInfo;
114 
115   /// Inliner - Specifies the inliner to use.  If this is non-null, it is
116   /// added to the per-module passes.
117   Pass *Inliner;
118 
119   /// The function summary index to use for function importing.
120   const FunctionInfoIndex *FunctionIndex;
121 
122   bool DisableTailCalls;
123   bool DisableUnitAtATime;
124   bool DisableUnrollLoops;
125   bool BBVectorize;
126   bool SLPVectorize;
127   bool LoopVectorize;
128   bool RerollLoops;
129   bool LoadCombine;
130   bool DisableGVNLoadPRE;
131   bool VerifyInput;
132   bool VerifyOutput;
133   bool MergeFunctions;
134   bool PrepareForLTO;
135 
136 private:
137   /// ExtensionList - This is list of all of the extensions that are registered.
138   std::vector<std::pair<ExtensionPointTy, ExtensionFn> > Extensions;
139 
140 public:
141   PassManagerBuilder();
142   ~PassManagerBuilder();
143   /// Adds an extension that will be used by all PassManagerBuilder instances.
144   /// This is intended to be used by plugins, to register a set of
145   /// optimisations to run automatically.
146   static void addGlobalExtension(ExtensionPointTy Ty, ExtensionFn Fn);
147   void addExtension(ExtensionPointTy Ty, ExtensionFn Fn);
148 
149 private:
150   void addExtensionsToPM(ExtensionPointTy ETy,
151                          legacy::PassManagerBase &PM) const;
152   void addInitialAliasAnalysisPasses(legacy::PassManagerBase &PM) const;
153   void addLTOOptimizationPasses(legacy::PassManagerBase &PM);
154   void addLateLTOOptimizationPasses(legacy::PassManagerBase &PM);
155 
156 public:
157   /// populateFunctionPassManager - This fills in the function pass manager,
158   /// which is expected to be run on each function immediately as it is
159   /// generated.  The idea is to reduce the size of the IR in memory.
160   void populateFunctionPassManager(legacy::FunctionPassManager &FPM);
161 
162   /// populateModulePassManager - This sets up the primary pass manager.
163   void populateModulePassManager(legacy::PassManagerBase &MPM);
164   void populateLTOPassManager(legacy::PassManagerBase &PM);
165 };
166 
167 /// Registers a function for adding a standard set of passes.  This should be
168 /// used by optimizer plugins to allow all front ends to transparently use
169 /// them.  Create a static instance of this class in your plugin, providing a
170 /// private function that the PassManagerBuilder can use to add your passes.
171 struct RegisterStandardPasses {
RegisterStandardPassesRegisterStandardPasses172   RegisterStandardPasses(PassManagerBuilder::ExtensionPointTy Ty,
173                          PassManagerBuilder::ExtensionFn Fn) {
174     PassManagerBuilder::addGlobalExtension(Ty, Fn);
175   }
176 };
177 
178 } // end namespace llvm
179 #endif
180