add a new PassManagerBuilder class to replace StandardPasses.h
[llvm/stm8.git] / include / llvm / Support / PassManagerBuilder.h
blobc684f096111831a75869dba22bca563a87b34c90
1 //===-- llvm/Support/PasaMangerBuilder.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++.
13 // These are implemented as inline functions so that we do not have to worry
14 // about link issues.
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_SUPPORT_PASSMANAGERBUILDER_H
19 #define LLVM_SUPPORT_PASSMANAGERBUILDER_H
21 #include "llvm/PassManager.h"
22 #include "llvm/DefaultPasses.h"
23 #include "llvm/Analysis/Passes.h"
24 #include "llvm/Analysis/Verifier.h"
25 #include "llvm/Target/TargetLibraryInfo.h"
26 #include "llvm/Transforms/Scalar.h"
27 #include "llvm/Transforms/IPO.h"
29 namespace llvm {
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 /// OptimizerBuilder Builder;
36 /// Builder.setOptimizationLevel(2);
37 /// Builder.populateFunctionPassManager(FPM);
38 /// Builder.populateModulePassManager(MPM);
39 ///
40 class PassManagerBuilder {
41 unsigned OptLevel; // 0 = -O0, 1 = -O1, 2 = -O2, 3 = -O3
42 unsigned SizeLevel; // 0 = none, 1 = -Os, 2 = -Oz
43 TargetLibraryInfo *TLI;
44 Pass *InlinerPass;
46 bool DisableSimplifyLibCalls;
47 bool DisableUnitAtATime;
48 bool DisableUnrollLoops;
49 public:
50 PassManagerBuilder() {
51 OptLevel = 2;
52 SizeLevel = 0;
53 TLI = 0;
54 InlinerPass = 0;
55 DisableSimplifyLibCalls = false;
56 DisableUnitAtATime = false;
57 DisableUnrollLoops = false;
60 ~PassManagerBuilder() {
61 delete TLI;
62 delete InlinerPass;
65 /// setOptimizationLevel - Specify the basic optimization level -O0 ... -O3.
66 void setOptimizationLevel(unsigned L) { OptLevel = L; }
68 /// setSizeLevel - Specify the size optimization level: none, -Os, -Oz.
69 void setSizeLevel(unsigned L) { SizeLevel = L; }
71 /// setLibraryInfo - Set information about the runtime library for the
72 /// optimizer. If this is specified, it is added to both the function and
73 /// per-module pass pipeline.
74 void setLibraryInfo(TargetLibraryInfo *LI) { TLI = LI; }
76 /// setInliner - Specify the inliner to use. If this is specified, it is
77 /// added to the per-module passes.
78 void setInliner(Pass *P) { InlinerPass = P; }
81 void disableSimplifyLibCalls() { DisableSimplifyLibCalls = true; }
82 void disableUnitAtATime() { DisableUnitAtATime = true; }
83 void disableUnrollLoops() { DisableUnrollLoops = true; }
85 private:
86 void addInitialAliasAnalysisPasses(PassManagerBase &PM) {
87 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
88 // BasicAliasAnalysis wins if they disagree. This is intended to help
89 // support "obvious" type-punning idioms.
90 PM.add(createTypeBasedAliasAnalysisPass());
91 PM.add(createBasicAliasAnalysisPass());
93 public:
95 /// populateFunctionPassManager - This fills in the function pass manager,
96 /// which is expected to be run on each function immediately as it is
97 /// generated. The idea is to reduce the size of the IR in memory.
98 void populateFunctionPassManager(FunctionPassManager &FPM) {
99 if (OptLevel == 0) return;
101 // Add TLI if we have some.
102 if (TLI) FPM.add(new TargetLibraryInfo(*TLI));
104 addInitialAliasAnalysisPasses(FPM);
106 FPM.add(createCFGSimplificationPass());
107 FPM.add(createScalarReplAggregatesPass());
108 FPM.add(createEarlyCSEPass());
111 /// populateModulePassManager - This sets up the primary pass manager.
112 void populateModulePassManager(PassManagerBase &MPM) {
113 // If all optimizations are disabled, just run the always-inline pass.
114 if (OptLevel == 0) {
115 if (InlinerPass) {
116 MPM.add(InlinerPass);
117 InlinerPass = 0;
119 return;
122 // Add TLI if we have some.
123 if (TLI) MPM.add(new TargetLibraryInfo(*TLI));
125 addInitialAliasAnalysisPasses(MPM);
127 if (!DisableUnitAtATime) {
128 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
130 MPM.add(createIPSCCPPass()); // IP SCCP
131 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
133 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
134 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
137 // Start of CallGraph SCC passes.
138 if (!DisableUnitAtATime)
139 MPM.add(createPruneEHPass()); // Remove dead EH info
140 if (InlinerPass) {
141 MPM.add(InlinerPass);
142 InlinerPass = 0;
144 if (!DisableUnitAtATime)
145 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
146 if (OptLevel > 2)
147 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
149 // Start of function pass.
150 // Break up aggregate allocas, using SSAUpdater.
151 MPM.add(createScalarReplAggregatesPass(-1, false));
152 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
153 if (!DisableSimplifyLibCalls)
154 MPM.add(createSimplifyLibCallsPass()); // Library Call Optimizations
155 MPM.add(createJumpThreadingPass()); // Thread jumps.
156 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
157 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
158 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
160 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
161 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
162 MPM.add(createReassociatePass()); // Reassociate expressions
163 MPM.add(createLoopRotatePass()); // Rotate Loop
164 MPM.add(createLICMPass()); // Hoist loop invariants
165 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
166 MPM.add(createInstructionCombiningPass());
167 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
168 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
169 MPM.add(createLoopDeletionPass()); // Delete dead loops
170 if (!DisableUnrollLoops)
171 MPM.add(createLoopUnrollPass()); // Unroll small loops
172 if (OptLevel > 1)
173 MPM.add(createGVNPass()); // Remove redundancies
174 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
175 MPM.add(createSCCPPass()); // Constant prop with SCCP
177 // Run instcombine after redundancy elimination to exploit opportunities
178 // opened up by them.
179 MPM.add(createInstructionCombiningPass());
180 MPM.add(createJumpThreadingPass()); // Thread jumps
181 MPM.add(createCorrelatedValuePropagationPass());
182 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
183 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
184 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
185 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
187 if (!DisableUnitAtATime) {
188 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
189 MPM.add(createDeadTypeEliminationPass()); // Eliminate dead types
191 // GlobalOpt already deletes dead functions and globals, at -O3 try a
192 // late pass of GlobalDCE. It is capable of deleting dead cycles.
193 if (OptLevel > 2)
194 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
196 if (OptLevel > 1)
197 MPM.add(createConstantMergePass()); // Merge dup global constants
203 } // end namespace llvm
204 #endif