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"
17 #include "llvm-c/Transforms/PassManagerBuilder.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/Analysis/Passes.h"
20 #include "llvm/Analysis/Verifier.h"
21 #include "llvm/DefaultPasses.h"
22 #include "llvm/PassManager.h"
23 #include "llvm/PassManager.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/ManagedStatic.h"
26 #include "llvm/Target/TargetLibraryInfo.h"
27 #include "llvm/Transforms/IPO.h"
28 #include "llvm/Transforms/Scalar.h"
29 #include "llvm/Transforms/Vectorize.h"
34 RunLoopVectorization("vectorize-loops",
35 cl::desc("Run the Loop vectorization passes"));
38 RunBBVectorization("vectorize", cl::desc("Run the BB vectorization passes"));
41 UseGVNAfterVectorization("use-gvn-after-vectorization",
42 cl::init(false), cl::Hidden,
43 cl::desc("Run GVN instead of Early CSE after vectorization passes"));
45 static cl::opt<bool> UseNewSROA("use-new-sroa",
46 cl::init(true), cl::Hidden,
47 cl::desc("Enable the new, experimental SROA pass"));
49 PassManagerBuilder::PassManagerBuilder() {
54 DisableSimplifyLibCalls = false;
55 DisableUnitAtATime = false;
56 DisableUnrollLoops = false;
57 Vectorize = RunBBVectorization;
58 LoopVectorize = RunLoopVectorization;
61 PassManagerBuilder::~PassManagerBuilder() {
66 /// Set of global extensions, automatically added as part of the standard set.
67 static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
68 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
70 void PassManagerBuilder::addGlobalExtension(
71 PassManagerBuilder::ExtensionPointTy Ty,
72 PassManagerBuilder::ExtensionFn Fn) {
73 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
76 void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
77 Extensions.push_back(std::make_pair(Ty, Fn));
80 void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
81 PassManagerBase &PM) const {
82 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
83 if ((*GlobalExtensions)[i].first == ETy)
84 (*GlobalExtensions)[i].second(*this, PM);
85 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
86 if (Extensions[i].first == ETy)
87 Extensions[i].second(*this, PM);
91 PassManagerBuilder::addInitialAliasAnalysisPasses(PassManagerBase &PM) const {
92 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
93 // BasicAliasAnalysis wins if they disagree. This is intended to help
94 // support "obvious" type-punning idioms.
95 PM.add(createTypeBasedAliasAnalysisPass());
96 PM.add(createBasicAliasAnalysisPass());
99 void PassManagerBuilder::populateFunctionPassManager(FunctionPassManager &FPM) {
100 addExtensionsToPM(EP_EarlyAsPossible, FPM);
102 // Add LibraryInfo if we have some.
103 if (LibraryInfo) FPM.add(new TargetLibraryInfo(*LibraryInfo));
105 if (OptLevel == 0) return;
107 addInitialAliasAnalysisPasses(FPM);
109 FPM.add(createCFGSimplificationPass());
111 FPM.add(createSROAPass());
113 FPM.add(createScalarReplAggregatesPass());
114 FPM.add(createEarlyCSEPass());
115 FPM.add(createLowerExpectIntrinsicPass());
118 void PassManagerBuilder::populateModulePassManager(PassManagerBase &MPM) {
119 // If all optimizations are disabled, just run the always-inline pass.
126 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
127 // pass manager, but we don't want to add extensions into that pass manager.
128 // To prevent this we must insert a no-op module pass to reset the pass
129 // manager to get the same behavior as EP_OptimizerLast in non-O0 builds.
130 if (!GlobalExtensions->empty() || !Extensions.empty())
131 MPM.add(createBarrierNoopPass());
133 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
137 // Add LibraryInfo if we have some.
138 if (LibraryInfo) MPM.add(new TargetLibraryInfo(*LibraryInfo));
140 addInitialAliasAnalysisPasses(MPM);
142 if (!DisableUnitAtATime) {
143 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
145 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
147 MPM.add(createIPSCCPPass()); // IP SCCP
148 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
150 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
151 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
154 // Start of CallGraph SCC passes.
155 if (!DisableUnitAtATime)
156 MPM.add(createPruneEHPass()); // Remove dead EH info
161 if (!DisableUnitAtATime)
162 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
164 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
166 // Start of function pass.
167 // Break up aggregate allocas, using SSAUpdater.
169 MPM.add(createSROAPass(/*RequiresDomTree*/ false));
171 MPM.add(createScalarReplAggregatesPass(-1, false));
172 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
173 if (!DisableSimplifyLibCalls)
174 MPM.add(createSimplifyLibCallsPass()); // Library Call Optimizations
175 MPM.add(createJumpThreadingPass()); // Thread jumps.
176 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
177 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
178 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
180 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
181 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
182 MPM.add(createReassociatePass()); // Reassociate expressions
183 MPM.add(createLoopRotatePass()); // Rotate Loop
184 MPM.add(createLICMPass()); // Hoist loop invariants
185 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
186 MPM.add(createInstructionCombiningPass());
187 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
188 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
189 MPM.add(createLoopDeletionPass()); // Delete dead loops
191 if (LoopVectorize && OptLevel > 1)
192 MPM.add(createLoopVectorizePass());
194 if (!DisableUnrollLoops)
195 MPM.add(createLoopUnrollPass()); // Unroll small loops
196 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
199 MPM.add(createGVNPass()); // Remove redundancies
200 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
201 MPM.add(createSCCPPass()); // Constant prop with SCCP
203 // Run instcombine after redundancy elimination to exploit opportunities
204 // opened up by them.
205 MPM.add(createInstructionCombiningPass());
206 MPM.add(createJumpThreadingPass()); // Thread jumps
207 MPM.add(createCorrelatedValuePropagationPass());
208 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
210 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
213 MPM.add(createBBVectorizePass());
214 MPM.add(createInstructionCombiningPass());
215 if (OptLevel > 1 && UseGVNAfterVectorization)
216 MPM.add(createGVNPass()); // Remove redundancies
218 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
221 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
222 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
223 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
225 if (!DisableUnitAtATime) {
226 // FIXME: We shouldn't bother with this anymore.
227 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
229 // GlobalOpt already deletes dead functions and globals, at -O2 try a
230 // late pass of GlobalDCE. It is capable of deleting dead cycles.
232 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
233 MPM.add(createConstantMergePass()); // Merge dup global constants
236 addExtensionsToPM(EP_OptimizerLast, MPM);
239 void PassManagerBuilder::populateLTOPassManager(PassManagerBase &PM,
242 bool DisableGVNLoadPRE) {
243 // Provide AliasAnalysis services for optimizations.
244 addInitialAliasAnalysisPasses(PM);
246 // Now that composite has been compiled, scan through the module, looking
247 // for a main function. If main is defined, mark all other functions
250 std::vector<const char*> E;
252 PM.add(createInternalizePass(E));
255 // Propagate constants at call sites into the functions they call. This
256 // opens opportunities for globalopt (and inlining) by substituting function
257 // pointers passed as arguments to direct uses of functions.
258 PM.add(createIPSCCPPass());
260 // Now that we internalized some globals, see if we can hack on them!
261 PM.add(createGlobalOptimizerPass());
263 // Linking modules together can lead to duplicated global constants, only
264 // keep one copy of each constant.
265 PM.add(createConstantMergePass());
267 // Remove unused arguments from functions.
268 PM.add(createDeadArgEliminationPass());
270 // Reduce the code after globalopt and ipsccp. Both can open up significant
271 // simplification opportunities, and both can propagate functions through
272 // function pointers. When this happens, we often have to resolve varargs
273 // calls, etc, so let instcombine do this.
274 PM.add(createInstructionCombiningPass());
276 // Inline small functions
278 PM.add(createFunctionInliningPass());
280 PM.add(createPruneEHPass()); // Remove dead EH info.
282 // Optimize globals again if we ran the inliner.
284 PM.add(createGlobalOptimizerPass());
285 PM.add(createGlobalDCEPass()); // Remove dead functions.
287 // If we didn't decide to inline a function, check to see if we can
288 // transform it to pass arguments by value instead of by reference.
289 PM.add(createArgumentPromotionPass());
291 // The IPO passes may leave cruft around. Clean up after them.
292 PM.add(createInstructionCombiningPass());
293 PM.add(createJumpThreadingPass());
296 PM.add(createSROAPass());
298 PM.add(createScalarReplAggregatesPass());
300 // Run a few AA driven optimizations here and now, to cleanup the code.
301 PM.add(createFunctionAttrsPass()); // Add nocapture.
302 PM.add(createGlobalsModRefPass()); // IP alias analysis.
304 PM.add(createLICMPass()); // Hoist loop invariants.
305 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
306 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
308 PM.add(createDeadStoreEliminationPass());
310 // Cleanup and simplify the code after the scalar optimizations.
311 PM.add(createInstructionCombiningPass());
313 PM.add(createJumpThreadingPass());
315 // Delete basic blocks, which optimization passes may have killed.
316 PM.add(createCFGSimplificationPass());
318 // Now that we have optimized the program, discard unreachable functions.
319 PM.add(createGlobalDCEPass());
322 LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
323 PassManagerBuilder *PMB = new PassManagerBuilder();
327 void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
328 PassManagerBuilder *Builder = unwrap(PMB);
333 LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
335 PassManagerBuilder *Builder = unwrap(PMB);
336 Builder->OptLevel = OptLevel;
340 LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
341 unsigned SizeLevel) {
342 PassManagerBuilder *Builder = unwrap(PMB);
343 Builder->SizeLevel = SizeLevel;
347 LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
349 PassManagerBuilder *Builder = unwrap(PMB);
350 Builder->DisableUnitAtATime = Value;
354 LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
356 PassManagerBuilder *Builder = unwrap(PMB);
357 Builder->DisableUnrollLoops = Value;
361 LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
363 PassManagerBuilder *Builder = unwrap(PMB);
364 Builder->DisableSimplifyLibCalls = Value;
368 LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
369 unsigned Threshold) {
370 PassManagerBuilder *Builder = unwrap(PMB);
371 Builder->Inliner = createFunctionInliningPass(Threshold);
375 LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
376 LLVMPassManagerRef PM) {
377 PassManagerBuilder *Builder = unwrap(PMB);
378 FunctionPassManager *FPM = unwrap<FunctionPassManager>(PM);
379 Builder->populateFunctionPassManager(*FPM);
383 LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
384 LLVMPassManagerRef PM) {
385 PassManagerBuilder *Builder = unwrap(PMB);
386 PassManagerBase *MPM = unwrap(PM);
387 Builder->populateModulePassManager(*MPM);
390 void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
391 LLVMPassManagerRef PM,
394 PassManagerBuilder *Builder = unwrap(PMB);
395 PassManagerBase *LPM = unwrap(PM);
396 Builder->populateLTOPassManager(*LPM, Internalize, RunInliner);