1 //===- PassManagerBuilder.cpp - Build Standard Pass -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
16 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
18 #include "llvm/PassManager.h"
19 #include "llvm/DefaultPasses.h"
20 #include "llvm/PassManager.h"
21 #include "llvm/Analysis/Passes.h"
22 #include "llvm/Analysis/Verifier.h"
23 #include "llvm/Target/TargetLibraryInfo.h"
24 #include "llvm/Transforms/Scalar.h"
25 #include "llvm/Transforms/IPO.h"
29 PassManagerBuilder::PassManagerBuilder() {
34 DisableSimplifyLibCalls = false;
35 DisableUnitAtATime = false;
36 DisableUnrollLoops = false;
39 PassManagerBuilder::~PassManagerBuilder() {
44 void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
45 Extensions.push_back(std::make_pair(Ty, Fn));
48 void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
49 PassManagerBase &PM) const {
50 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
51 if (Extensions[i].first == ETy)
52 Extensions[i].second(*this, PM);
56 PassManagerBuilder::addInitialAliasAnalysisPasses(PassManagerBase &PM) const {
57 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
58 // BasicAliasAnalysis wins if they disagree. This is intended to help
59 // support "obvious" type-punning idioms.
60 PM.add(createTypeBasedAliasAnalysisPass());
61 PM.add(createBasicAliasAnalysisPass());
64 void PassManagerBuilder::populateFunctionPassManager(FunctionPassManager &FPM) {
65 addExtensionsToPM(EP_EarlyAsPossible, FPM);
67 // Add LibraryInfo if we have some.
68 if (LibraryInfo) FPM.add(new TargetLibraryInfo(*LibraryInfo));
70 if (OptLevel == 0) return;
72 addInitialAliasAnalysisPasses(FPM);
74 FPM.add(createCFGSimplificationPass());
75 FPM.add(createScalarReplAggregatesPass());
76 FPM.add(createEarlyCSEPass());
77 FPM.add(createLowerExpectIntrinsicPass());
80 void PassManagerBuilder::populateModulePassManager(PassManagerBase &MPM) {
81 // If all optimizations are disabled, just run the always-inline pass.
90 // Add LibraryInfo if we have some.
91 if (LibraryInfo) MPM.add(new TargetLibraryInfo(*LibraryInfo));
93 addInitialAliasAnalysisPasses(MPM);
95 if (!DisableUnitAtATime) {
96 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
98 MPM.add(createIPSCCPPass()); // IP SCCP
99 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
101 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
102 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
105 // Start of CallGraph SCC passes.
106 if (!DisableUnitAtATime)
107 MPM.add(createPruneEHPass()); // Remove dead EH info
112 if (!DisableUnitAtATime)
113 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
115 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
117 // Start of function pass.
118 // Break up aggregate allocas, using SSAUpdater.
119 MPM.add(createScalarReplAggregatesPass(-1, false));
120 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
121 if (!DisableSimplifyLibCalls)
122 MPM.add(createSimplifyLibCallsPass()); // Library Call Optimizations
123 MPM.add(createJumpThreadingPass()); // Thread jumps.
124 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
125 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
126 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
128 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
129 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
130 MPM.add(createReassociatePass()); // Reassociate expressions
131 MPM.add(createLoopRotatePass()); // Rotate Loop
132 MPM.add(createLICMPass()); // Hoist loop invariants
133 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
134 MPM.add(createInstructionCombiningPass());
135 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
136 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
137 MPM.add(createLoopDeletionPass()); // Delete dead loops
138 if (!DisableUnrollLoops)
139 MPM.add(createLoopUnrollPass()); // Unroll small loops
140 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
143 MPM.add(createGVNPass()); // Remove redundancies
144 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
145 MPM.add(createSCCPPass()); // Constant prop with SCCP
147 // Run instcombine after redundancy elimination to exploit opportunities
148 // opened up by them.
149 MPM.add(createInstructionCombiningPass());
150 MPM.add(createJumpThreadingPass()); // Thread jumps
151 MPM.add(createCorrelatedValuePropagationPass());
152 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
154 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
156 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
157 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
158 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
160 if (!DisableUnitAtATime) {
161 // FIXME: We shouldn't bother with this anymore.
162 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
164 // GlobalOpt already deletes dead functions and globals, at -O3 try a
165 // late pass of GlobalDCE. It is capable of deleting dead cycles.
167 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
170 MPM.add(createConstantMergePass()); // Merge dup global constants
174 void PassManagerBuilder::populateLTOPassManager(PassManagerBase &PM,
177 // Provide AliasAnalysis services for optimizations.
178 addInitialAliasAnalysisPasses(PM);
180 // Now that composite has been compiled, scan through the module, looking
181 // for a main function. If main is defined, mark all other functions
184 PM.add(createInternalizePass(true));
186 // Propagate constants at call sites into the functions they call. This
187 // opens opportunities for globalopt (and inlining) by substituting function
188 // pointers passed as arguments to direct uses of functions.
189 PM.add(createIPSCCPPass());
191 // Now that we internalized some globals, see if we can hack on them!
192 PM.add(createGlobalOptimizerPass());
194 // Linking modules together can lead to duplicated global constants, only
195 // keep one copy of each constant.
196 PM.add(createConstantMergePass());
198 // Remove unused arguments from functions.
199 PM.add(createDeadArgEliminationPass());
201 // Reduce the code after globalopt and ipsccp. Both can open up significant
202 // simplification opportunities, and both can propagate functions through
203 // function pointers. When this happens, we often have to resolve varargs
204 // calls, etc, so let instcombine do this.
205 PM.add(createInstructionCombiningPass());
207 // Inline small functions
209 PM.add(createFunctionInliningPass());
211 PM.add(createPruneEHPass()); // Remove dead EH info.
213 // Optimize globals again if we ran the inliner.
215 PM.add(createGlobalOptimizerPass());
216 PM.add(createGlobalDCEPass()); // Remove dead functions.
218 // If we didn't decide to inline a function, check to see if we can
219 // transform it to pass arguments by value instead of by reference.
220 PM.add(createArgumentPromotionPass());
222 // The IPO passes may leave cruft around. Clean up after them.
223 PM.add(createInstructionCombiningPass());
224 PM.add(createJumpThreadingPass());
226 PM.add(createScalarReplAggregatesPass());
228 // Run a few AA driven optimizations here and now, to cleanup the code.
229 PM.add(createFunctionAttrsPass()); // Add nocapture.
230 PM.add(createGlobalsModRefPass()); // IP alias analysis.
232 PM.add(createLICMPass()); // Hoist loop invariants.
233 PM.add(createGVNPass()); // Remove redundancies.
234 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
236 PM.add(createDeadStoreEliminationPass());
238 // Cleanup and simplify the code after the scalar optimizations.
239 PM.add(createInstructionCombiningPass());
241 PM.add(createJumpThreadingPass());
243 // Delete basic blocks, which optimization passes may have killed.
244 PM.add(createCFGSimplificationPass());
246 // Now that we have optimized the program, discard unreachable functions.
247 PM.add(createGlobalDCEPass());