1226584Sdim// llvm/Transforms/IPO/PassManagerBuilder.h - Build Standard Pass -*- C++ -*-=//
2226584Sdim//
3226584Sdim//                     The LLVM Compiler Infrastructure
4226584Sdim//
5226584Sdim// This file is distributed under the University of Illinois Open Source
6226584Sdim// License. See LICENSE.TXT for details.
7226584Sdim//
8226584Sdim//===----------------------------------------------------------------------===//
9226584Sdim//
10226584Sdim// This file defines the PassManagerBuilder class, which is used to set up a
11226584Sdim// "standard" optimization sequence suitable for languages like C and C++.
12226584Sdim//
13226584Sdim//===----------------------------------------------------------------------===//
14226584Sdim
15249423Sdim#ifndef LLVM_TRANSFORMS_IPO_PASSMANAGERBUILDER_H
16249423Sdim#define LLVM_TRANSFORMS_IPO_PASSMANAGERBUILDER_H
17226584Sdim
18226584Sdim#include <vector>
19226584Sdim
20226584Sdimnamespace llvm {
21263508Sdimclass TargetLibraryInfo;
22263508Sdimclass Pass;
23226584Sdim
24263508Sdim// The old pass manager infrastructure is hidden in a legacy namespace now.
25263508Sdimnamespace legacy {
26263508Sdimclass PassManagerBase;
27263508Sdimclass FunctionPassManager;
28263508Sdim}
29263508Sdimusing legacy::PassManagerBase;
30263508Sdimusing legacy::FunctionPassManager;
31263508Sdim
32226584Sdim/// PassManagerBuilder - This class is used to set up a standard optimization
33226584Sdim/// sequence for languages like C and C++, allowing some APIs to customize the
34226584Sdim/// pass sequence in various ways. A simple example of using it would be:
35226584Sdim///
36226584Sdim///  PassManagerBuilder Builder;
37226584Sdim///  Builder.OptLevel = 2;
38226584Sdim///  Builder.populateFunctionPassManager(FPM);
39226584Sdim///  Builder.populateModulePassManager(MPM);
40226584Sdim///
41226584Sdim/// In addition to setting up the basic passes, PassManagerBuilder allows
42226584Sdim/// frontends to vend a plugin API, where plugins are allowed to add extensions
43226584Sdim/// to the default pass manager.  They do this by specifying where in the pass
44226584Sdim/// pipeline they want to be added, along with a callback function that adds
45226584Sdim/// the pass(es).  For example, a plugin that wanted to add a loop optimization
46226584Sdim/// could do something like this:
47226584Sdim///
48226584Sdim/// static void addMyLoopPass(const PMBuilder &Builder, PassManagerBase &PM) {
49226584Sdim///   if (Builder.getOptLevel() > 2 && Builder.getOptSizeLevel() == 0)
50226584Sdim///     PM.add(createMyAwesomePass());
51226584Sdim/// }
52226584Sdim///   ...
53226584Sdim///   Builder.addExtension(PassManagerBuilder::EP_LoopOptimizerEnd,
54226584Sdim///                        addMyLoopPass);
55226584Sdim///   ...
56226584Sdimclass PassManagerBuilder {
57226584Sdimpublic:
58226584Sdim
59226584Sdim  /// Extensions are passed the builder itself (so they can see how it is
60226584Sdim  /// configured) as well as the pass manager to add stuff to.
61226584Sdim  typedef void (*ExtensionFn)(const PassManagerBuilder &Builder,
62226584Sdim                              PassManagerBase &PM);
63226584Sdim  enum ExtensionPointTy {
64226584Sdim    /// EP_EarlyAsPossible - This extension point allows adding passes before
65226584Sdim    /// any other transformations, allowing them to see the code as it is coming
66226584Sdim    /// out of the frontend.
67226584Sdim    EP_EarlyAsPossible,
68226584Sdim
69234353Sdim    /// EP_ModuleOptimizerEarly - This extension point allows adding passes
70234353Sdim    /// just before the main module-level optimization passes.
71234353Sdim    EP_ModuleOptimizerEarly,
72234353Sdim
73226584Sdim    /// EP_LoopOptimizerEnd - This extension point allows adding loop passes to
74226584Sdim    /// the end of the loop optimizer.
75226584Sdim    EP_LoopOptimizerEnd,
76226584Sdim
77226584Sdim    /// EP_ScalarOptimizerLate - This extension point allows adding optimization
78226584Sdim    /// passes after most of the main optimizations, but before the last
79226584Sdim    /// cleanup-ish optimizations.
80234353Sdim    EP_ScalarOptimizerLate,
81234353Sdim
82234353Sdim    /// EP_OptimizerLast -- This extension point allows adding passes that
83234353Sdim    /// run after everything else.
84234353Sdim    EP_OptimizerLast,
85234353Sdim
86234353Sdim    /// EP_EnabledOnOptLevel0 - This extension point allows adding passes that
87234353Sdim    /// should not be disabled by O0 optimization level. The passes will be
88234353Sdim    /// inserted after the inlining pass.
89234353Sdim    EP_EnabledOnOptLevel0
90226584Sdim  };
91226584Sdim
92226584Sdim  /// The Optimization Level - Specify the basic optimization level.
93226584Sdim  ///    0 = -O0, 1 = -O1, 2 = -O2, 3 = -O3
94226584Sdim  unsigned OptLevel;
95226584Sdim
96226584Sdim  /// SizeLevel - How much we're optimizing for size.
97226584Sdim  ///    0 = none, 1 = -Os, 2 = -Oz
98226584Sdim  unsigned SizeLevel;
99226584Sdim
100226584Sdim  /// LibraryInfo - Specifies information about the runtime library for the
101226584Sdim  /// optimizer.  If this is non-null, it is added to both the function and
102226584Sdim  /// per-module pass pipeline.
103226584Sdim  TargetLibraryInfo *LibraryInfo;
104226584Sdim
105226584Sdim  /// Inliner - Specifies the inliner to use.  If this is non-null, it is
106226584Sdim  /// added to the per-module passes.
107226584Sdim  Pass *Inliner;
108226584Sdim
109226584Sdim  bool DisableUnitAtATime;
110226584Sdim  bool DisableUnrollLoops;
111251662Sdim  bool BBVectorize;
112251662Sdim  bool SLPVectorize;
113243830Sdim  bool LoopVectorize;
114263508Sdim  bool LateVectorize;
115263508Sdim  bool RerollLoops;
116226584Sdim
117226584Sdimprivate:
118226584Sdim  /// ExtensionList - This is list of all of the extensions that are registered.
119226584Sdim  std::vector<std::pair<ExtensionPointTy, ExtensionFn> > Extensions;
120226584Sdim
121226584Sdimpublic:
122226584Sdim  PassManagerBuilder();
123226584Sdim  ~PassManagerBuilder();
124226584Sdim  /// Adds an extension that will be used by all PassManagerBuilder instances.
125226584Sdim  /// This is intended to be used by plugins, to register a set of
126226584Sdim  /// optimisations to run automatically.
127226584Sdim  static void addGlobalExtension(ExtensionPointTy Ty, ExtensionFn Fn);
128226584Sdim  void addExtension(ExtensionPointTy Ty, ExtensionFn Fn);
129226584Sdim
130226584Sdimprivate:
131226584Sdim  void addExtensionsToPM(ExtensionPointTy ETy, PassManagerBase &PM) const;
132226584Sdim  void addInitialAliasAnalysisPasses(PassManagerBase &PM) const;
133226584Sdimpublic:
134226584Sdim
135226584Sdim  /// populateFunctionPassManager - This fills in the function pass manager,
136226584Sdim  /// which is expected to be run on each function immediately as it is
137226584Sdim  /// generated.  The idea is to reduce the size of the IR in memory.
138226584Sdim  void populateFunctionPassManager(FunctionPassManager &FPM);
139226584Sdim
140226584Sdim  /// populateModulePassManager - This sets up the primary pass manager.
141226584Sdim  void populateModulePassManager(PassManagerBase &MPM);
142226584Sdim  void populateLTOPassManager(PassManagerBase &PM, bool Internalize,
143234353Sdim                              bool RunInliner, bool DisableGVNLoadPRE = false);
144226584Sdim};
145234353Sdim
146226584Sdim/// Registers a function for adding a standard set of passes.  This should be
147226584Sdim/// used by optimizer plugins to allow all front ends to transparently use
148226584Sdim/// them.  Create a static instance of this class in your plugin, providing a
149226584Sdim/// private function that the PassManagerBuilder can use to add your passes.
150226584Sdimstruct RegisterStandardPasses {
151226584Sdim  RegisterStandardPasses(PassManagerBuilder::ExtensionPointTy Ty,
152226584Sdim                         PassManagerBuilder::ExtensionFn Fn) {
153226584Sdim    PassManagerBuilder::addGlobalExtension(Ty, Fn);
154226584Sdim  }
155226584Sdim};
156234353Sdim
157226584Sdim} // end namespace llvm
158226584Sdim#endif
159