1 // llvm/Transforms/IPO/PassManagerBuilder.h - Build Standard Pass -*- C++ -*-=//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the PassManagerBuilder class, which is used to set up a
10 // "standard" optimization sequence suitable for languages like C and C++.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_TRANSFORMS_IPO_PASSMANAGERBUILDER_H
15 #define LLVM_TRANSFORMS_IPO_PASSMANAGERBUILDER_H
16 
17 #include "llvm-c/Transforms/PassManagerBuilder.h"
18 #include <functional>
19 #include <string>
20 #include <vector>
21 
22 namespace llvm {
23 class ModuleSummaryIndex;
24 class Pass;
25 class TargetLibraryInfoImpl;
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 to the builder itself (so they can see how it is
60   /// configured) as well as the pass manager to add stuff to.
61   typedef std::function<void(const PassManagerBuilder &Builder,
62                              legacy::PassManagerBase &PM)>
63       ExtensionFn;
64   typedef int GlobalExtensionID;
65 
66   /// The Optimization Level - Specify the basic optimization level.
67   ///    0 = -O0, 1 = -O1, 2 = -O2, 3 = -O3
68   unsigned OptLevel;
69 
70   /// SizeLevel - How much we're optimizing for size.
71   ///    0 = none, 1 = -Os, 2 = -Oz
72   unsigned SizeLevel;
73 
74   /// LibraryInfo - Specifies information about the runtime library for the
75   /// optimizer.  If this is non-null, it is added to both the function and
76   /// per-module pass pipeline.
77   TargetLibraryInfoImpl *LibraryInfo;
78 
79   /// Inliner - Specifies the inliner to use.  If this is non-null, it is
80   /// added to the per-module passes.
81   Pass *Inliner;
82 
83   /// The module summary index to use for exporting information from the
84   /// regular LTO phase, for example for the CFI and devirtualization type
85   /// tests.
86   ModuleSummaryIndex *ExportSummary = nullptr;
87 
88   /// The module summary index to use for importing information to the
89   /// thin LTO backends, for example for the CFI and devirtualization type
90   /// tests.
91   const ModuleSummaryIndex *ImportSummary = nullptr;
92 
93   bool DisableUnrollLoops;
94   bool CallGraphProfile;
95   bool SLPVectorize;
96   bool LoopVectorize;
97   bool LoopsInterleaved;
98   bool DisableGVNLoadPRE;
99   bool ForgetAllSCEVInLoopUnroll;
100   bool VerifyInput;
101   bool VerifyOutput;
102   bool MergeFunctions;
103   bool DivergentTarget;
104   unsigned LicmMssaOptCap;
105   unsigned LicmMssaNoAccForPromotionCap;
106 
107 public:
108   PassManagerBuilder();
109   ~PassManagerBuilder();
110 
111 private:
112   void addInitialAliasAnalysisPasses(legacy::PassManagerBase &PM) const;
113   void addFunctionSimplificationPasses(legacy::PassManagerBase &MPM);
114   void addVectorPasses(legacy::PassManagerBase &PM, bool IsFullLTO);
115 
116 public:
117   /// populateFunctionPassManager - This fills in the function pass manager,
118   /// which is expected to be run on each function immediately as it is
119   /// generated.  The idea is to reduce the size of the IR in memory.
120   void populateFunctionPassManager(legacy::FunctionPassManager &FPM);
121 
122   /// populateModulePassManager - This sets up the primary pass manager.
123   void populateModulePassManager(legacy::PassManagerBase &MPM);
124 };
125 
unwrap(LLVMPassManagerBuilderRef P)126 inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
127     return reinterpret_cast<PassManagerBuilder*>(P);
128 }
129 
wrap(PassManagerBuilder * P)130 inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
131   return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
132 }
133 
134 } // end namespace llvm
135 #endif
136