ef01fa350531a90b8a304aa0de08108d22f11aa7
[oota-llvm.git] / include / llvm / Transforms / IPO / PassManagerBuilder.h
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 <vector>
19
20 namespace llvm {
21 class Pass;
22 class TargetLibraryInfoImpl;
23 class TargetMachine;
24
25 // The old pass manager infrastructure is hidden in a legacy namespace now.
26 namespace legacy {
27 class FunctionPassManager;
28 class PassManagerBase;
29 }
30
31 /// PassManagerBuilder - This class is used to set up a standard optimization
32 /// sequence for languages like C and C++, allowing some APIs to customize the
33 /// pass sequence in various ways. A simple example of using it would be:
34 ///
35 ///  PassManagerBuilder Builder;
36 ///  Builder.OptLevel = 2;
37 ///  Builder.populateFunctionPassManager(FPM);
38 ///  Builder.populateModulePassManager(MPM);
39 ///
40 /// In addition to setting up the basic passes, PassManagerBuilder allows
41 /// frontends to vend a plugin API, where plugins are allowed to add extensions
42 /// to the default pass manager.  They do this by specifying where in the pass
43 /// pipeline they want to be added, along with a callback function that adds
44 /// the pass(es).  For example, a plugin that wanted to add a loop optimization
45 /// could do something like this:
46 ///
47 /// static void addMyLoopPass(const PMBuilder &Builder, PassManagerBase &PM) {
48 ///   if (Builder.getOptLevel() > 2 && Builder.getOptSizeLevel() == 0)
49 ///     PM.add(createMyAwesomePass());
50 /// }
51 ///   ...
52 ///   Builder.addExtension(PassManagerBuilder::EP_LoopOptimizerEnd,
53 ///                        addMyLoopPass);
54 ///   ...
55 class PassManagerBuilder {
56 public:
57   /// Extensions are passed the builder itself (so they can see how it is
58   /// configured) as well as the pass manager to add stuff to.
59   typedef void (*ExtensionFn)(const PassManagerBuilder &Builder,
60                               legacy::PassManagerBase &PM);
61   enum ExtensionPointTy {
62     /// EP_EarlyAsPossible - This extension point allows adding passes before
63     /// any other transformations, allowing them to see the code as it is coming
64     /// out of the frontend.
65     EP_EarlyAsPossible,
66
67     /// EP_ModuleOptimizerEarly - This extension point allows adding passes
68     /// just before the main module-level optimization passes.
69     EP_ModuleOptimizerEarly,
70
71     /// EP_LoopOptimizerEnd - This extension point allows adding loop passes to
72     /// the end of the loop optimizer.
73     EP_LoopOptimizerEnd,
74
75     /// EP_ScalarOptimizerLate - This extension point allows adding optimization
76     /// passes after most of the main optimizations, but before the last
77     /// cleanup-ish optimizations.
78     EP_ScalarOptimizerLate,
79
80     /// EP_OptimizerLast -- This extension point allows adding passes that
81     /// run after everything else.
82     EP_OptimizerLast,
83
84     /// EP_VectorizerStart - This extension point allows adding optimization
85     /// passes before the vectorizer and other highly target specific
86     /// optimization passes are executed.
87     EP_VectorizerStart,
88
89     /// EP_EnabledOnOptLevel0 - This extension point allows adding passes that
90     /// should not be disabled by O0 optimization level. The passes will be
91     /// inserted after the inlining pass.
92     EP_EnabledOnOptLevel0,
93
94     /// EP_Peephole - This extension point allows adding passes that perform
95     /// peephole optimizations similar to the instruction combiner. These passes
96     /// will be inserted after each instance of the instruction combiner pass.
97     EP_Peephole,
98   };
99
100   /// The Optimization Level - Specify the basic optimization level.
101   ///    0 = -O0, 1 = -O1, 2 = -O2, 3 = -O3
102   unsigned OptLevel;
103
104   /// SizeLevel - How much we're optimizing for size.
105   ///    0 = none, 1 = -Os, 2 = -Oz
106   unsigned SizeLevel;
107
108   /// LibraryInfo - Specifies information about the runtime library for the
109   /// optimizer.  If this is non-null, it is added to both the function and
110   /// per-module pass pipeline.
111   TargetLibraryInfoImpl *LibraryInfo;
112
113   /// Inliner - Specifies the inliner to use.  If this is non-null, it is
114   /// added to the per-module passes.
115   Pass *Inliner;
116
117   bool DisableTailCalls;
118   bool DisableUnitAtATime;
119   bool DisableUnrollLoops;
120   bool BBVectorize;
121   bool SLPVectorize;
122   bool LoopVectorize;
123   bool RerollLoops;
124   bool LoadCombine;
125   bool DisableGVNLoadPRE;
126   bool VerifyInput;
127   bool VerifyOutput;
128   bool MergeFunctions;
129   bool PrepareForLTO;
130
131 private:
132   /// ExtensionList - This is list of all of the extensions that are registered.
133   std::vector<std::pair<ExtensionPointTy, ExtensionFn> > Extensions;
134
135 public:
136   PassManagerBuilder();
137   ~PassManagerBuilder();
138   /// Adds an extension that will be used by all PassManagerBuilder instances.
139   /// This is intended to be used by plugins, to register a set of
140   /// optimisations to run automatically.
141   static void addGlobalExtension(ExtensionPointTy Ty, ExtensionFn Fn);
142   void addExtension(ExtensionPointTy Ty, ExtensionFn Fn);
143
144 private:
145   void addExtensionsToPM(ExtensionPointTy ETy,
146                          legacy::PassManagerBase &PM) const;
147   void addInitialAliasAnalysisPasses(legacy::PassManagerBase &PM) const;
148   void addLTOOptimizationPasses(legacy::PassManagerBase &PM);
149   void addLateLTOOptimizationPasses(legacy::PassManagerBase &PM);
150
151 public:
152   /// populateFunctionPassManager - This fills in the function pass manager,
153   /// which is expected to be run on each function immediately as it is
154   /// generated.  The idea is to reduce the size of the IR in memory.
155   void populateFunctionPassManager(legacy::FunctionPassManager &FPM);
156
157   /// populateModulePassManager - This sets up the primary pass manager.
158   void populateModulePassManager(legacy::PassManagerBase &MPM);
159   void populateLTOPassManager(legacy::PassManagerBase &PM);
160 };
161
162 /// Registers a function for adding a standard set of passes.  This should be
163 /// used by optimizer plugins to allow all front ends to transparently use
164 /// them.  Create a static instance of this class in your plugin, providing a
165 /// private function that the PassManagerBuilder can use to add your passes.
166 struct RegisterStandardPasses {
167   RegisterStandardPasses(PassManagerBuilder::ExtensionPointTy Ty,
168                          PassManagerBuilder::ExtensionFn Fn) {
169     PassManagerBuilder::addGlobalExtension(Ty, Fn);
170   }
171 };
172
173 } // end namespace llvm
174 #endif