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/PassManager.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Target/TargetLibraryInfo.h"
25 #include "llvm/Transforms/IPO.h"
26 #include "llvm/Transforms/Scalar.h"
27 #include "llvm/Transforms/Vectorize.h"
32 RunLoopVectorization("vectorize-loops",
33 cl::desc("Run the Loop vectorization passes"));
36 RunBBVectorization("vectorize", cl::desc("Run the BB vectorization passes"));
39 UseGVNAfterVectorization("use-gvn-after-vectorization",
40 cl::init(false), cl::Hidden,
41 cl::desc("Run GVN instead of Early CSE after vectorization passes"));
43 static cl::opt<bool> UseNewSROA("use-new-sroa",
44 cl::init(true), cl::Hidden,
45 cl::desc("Enable the new, experimental SROA pass"));
47 PassManagerBuilder::PassManagerBuilder() {
52 DisableSimplifyLibCalls = false;
53 DisableUnitAtATime = false;
54 DisableUnrollLoops = false;
55 Vectorize = RunBBVectorization;
56 LoopVectorize = RunLoopVectorization;
59 PassManagerBuilder::~PassManagerBuilder() {
64 /// Set of global extensions, automatically added as part of the standard set.
65 static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
66 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
68 void PassManagerBuilder::addGlobalExtension(
69 PassManagerBuilder::ExtensionPointTy Ty,
70 PassManagerBuilder::ExtensionFn Fn) {
71 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
74 void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
75 Extensions.push_back(std::make_pair(Ty, Fn));
78 void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
79 PassManagerBase &PM) const {
80 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
81 if ((*GlobalExtensions)[i].first == ETy)
82 (*GlobalExtensions)[i].second(*this, PM);
83 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
84 if (Extensions[i].first == ETy)
85 Extensions[i].second(*this, PM);
89 PassManagerBuilder::addInitialAliasAnalysisPasses(PassManagerBase &PM) const {
90 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
91 // BasicAliasAnalysis wins if they disagree. This is intended to help
92 // support "obvious" type-punning idioms.
93 PM.add(createTypeBasedAliasAnalysisPass());
94 PM.add(createBasicAliasAnalysisPass());
97 void PassManagerBuilder::populateFunctionPassManager(FunctionPassManager &FPM) {
98 addExtensionsToPM(EP_EarlyAsPossible, FPM);
100 // Add LibraryInfo if we have some.
101 if (LibraryInfo) FPM.add(new TargetLibraryInfo(*LibraryInfo));
103 if (OptLevel == 0) return;
105 addInitialAliasAnalysisPasses(FPM);
107 FPM.add(createCFGSimplificationPass());
109 FPM.add(createSROAPass());
111 FPM.add(createScalarReplAggregatesPass());
112 FPM.add(createEarlyCSEPass());
113 FPM.add(createLowerExpectIntrinsicPass());
116 void PassManagerBuilder::populateModulePassManager(PassManagerBase &MPM) {
117 // If all optimizations are disabled, just run the always-inline pass.
124 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
125 // pass manager, but we don't want to add extensions into that pass manager.
126 // To prevent this we must insert a no-op module pass to reset the pass
127 // manager to get the same behavior as EP_OptimizerLast in non-O0 builds.
128 if (!GlobalExtensions->empty() || !Extensions.empty())
129 MPM.add(createBarrierNoopPass());
131 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
135 // Add LibraryInfo if we have some.
136 if (LibraryInfo) MPM.add(new TargetLibraryInfo(*LibraryInfo));
138 addInitialAliasAnalysisPasses(MPM);
140 if (!DisableUnitAtATime) {
141 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
143 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
145 MPM.add(createIPSCCPPass()); // IP SCCP
146 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
148 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
149 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
152 // Start of CallGraph SCC passes.
153 if (!DisableUnitAtATime)
154 MPM.add(createPruneEHPass()); // Remove dead EH info
159 if (!DisableUnitAtATime)
160 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
162 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
164 // Start of function pass.
165 // Break up aggregate allocas, using SSAUpdater.
167 MPM.add(createSROAPass(/*RequiresDomTree*/ false));
169 MPM.add(createScalarReplAggregatesPass(-1, false));
170 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
171 if (!DisableSimplifyLibCalls)
172 MPM.add(createSimplifyLibCallsPass()); // Library Call Optimizations
173 MPM.add(createJumpThreadingPass()); // Thread jumps.
174 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
175 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
176 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
178 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
179 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
180 MPM.add(createReassociatePass()); // Reassociate expressions
181 MPM.add(createLoopRotatePass()); // Rotate Loop
182 MPM.add(createLICMPass()); // Hoist loop invariants
183 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
184 MPM.add(createInstructionCombiningPass());
185 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
186 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
187 MPM.add(createLoopDeletionPass()); // Delete dead loops
189 if (LoopVectorize && OptLevel > 2)
190 MPM.add(createLoopVectorizePass());
192 if (!DisableUnrollLoops)
193 MPM.add(createLoopUnrollPass()); // Unroll small loops
194 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
197 MPM.add(createGVNPass()); // Remove redundancies
198 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
199 MPM.add(createSCCPPass()); // Constant prop with SCCP
201 // Run instcombine after redundancy elimination to exploit opportunities
202 // opened up by them.
203 MPM.add(createInstructionCombiningPass());
204 MPM.add(createJumpThreadingPass()); // Thread jumps
205 MPM.add(createCorrelatedValuePropagationPass());
206 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
208 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
211 MPM.add(createBBVectorizePass());
212 MPM.add(createInstructionCombiningPass());
213 if (OptLevel > 1 && UseGVNAfterVectorization)
214 MPM.add(createGVNPass()); // Remove redundancies
216 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
219 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
220 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
221 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
223 if (!DisableUnitAtATime) {
224 // FIXME: We shouldn't bother with this anymore.
225 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
227 // GlobalOpt already deletes dead functions and globals, at -O2 try a
228 // late pass of GlobalDCE. It is capable of deleting dead cycles.
230 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
231 MPM.add(createConstantMergePass()); // Merge dup global constants
234 addExtensionsToPM(EP_OptimizerLast, MPM);
237 void PassManagerBuilder::populateLTOPassManager(PassManagerBase &PM,
240 bool DisableGVNLoadPRE) {
241 // Provide AliasAnalysis services for optimizations.
242 addInitialAliasAnalysisPasses(PM);
244 // Now that composite has been compiled, scan through the module, looking
245 // for a main function. If main is defined, mark all other functions
248 std::vector<const char*> E;
250 PM.add(createInternalizePass(E));
253 // Propagate constants at call sites into the functions they call. This
254 // opens opportunities for globalopt (and inlining) by substituting function
255 // pointers passed as arguments to direct uses of functions.
256 PM.add(createIPSCCPPass());
258 // Now that we internalized some globals, see if we can hack on them!
259 PM.add(createGlobalOptimizerPass());
261 // Linking modules together can lead to duplicated global constants, only
262 // keep one copy of each constant.
263 PM.add(createConstantMergePass());
265 // Remove unused arguments from functions.
266 PM.add(createDeadArgEliminationPass());
268 // Reduce the code after globalopt and ipsccp. Both can open up significant
269 // simplification opportunities, and both can propagate functions through
270 // function pointers. When this happens, we often have to resolve varargs
271 // calls, etc, so let instcombine do this.
272 PM.add(createInstructionCombiningPass());
274 // Inline small functions
276 PM.add(createFunctionInliningPass());
278 PM.add(createPruneEHPass()); // Remove dead EH info.
280 // Optimize globals again if we ran the inliner.
282 PM.add(createGlobalOptimizerPass());
283 PM.add(createGlobalDCEPass()); // Remove dead functions.
285 // If we didn't decide to inline a function, check to see if we can
286 // transform it to pass arguments by value instead of by reference.
287 PM.add(createArgumentPromotionPass());
289 // The IPO passes may leave cruft around. Clean up after them.
290 PM.add(createInstructionCombiningPass());
291 PM.add(createJumpThreadingPass());
294 PM.add(createSROAPass());
296 PM.add(createScalarReplAggregatesPass());
298 // Run a few AA driven optimizations here and now, to cleanup the code.
299 PM.add(createFunctionAttrsPass()); // Add nocapture.
300 PM.add(createGlobalsModRefPass()); // IP alias analysis.
302 PM.add(createLICMPass()); // Hoist loop invariants.
303 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
304 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
306 PM.add(createDeadStoreEliminationPass());
308 // Cleanup and simplify the code after the scalar optimizations.
309 PM.add(createInstructionCombiningPass());
311 PM.add(createJumpThreadingPass());
313 // Delete basic blocks, which optimization passes may have killed.
314 PM.add(createCFGSimplificationPass());
316 // Now that we have optimized the program, discard unreachable functions.
317 PM.add(createGlobalDCEPass());
320 LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
321 PassManagerBuilder *PMB = new PassManagerBuilder();
325 void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
326 PassManagerBuilder *Builder = unwrap(PMB);
331 LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
333 PassManagerBuilder *Builder = unwrap(PMB);
334 Builder->OptLevel = OptLevel;
338 LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
339 unsigned SizeLevel) {
340 PassManagerBuilder *Builder = unwrap(PMB);
341 Builder->SizeLevel = SizeLevel;
345 LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
347 PassManagerBuilder *Builder = unwrap(PMB);
348 Builder->DisableUnitAtATime = Value;
352 LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
354 PassManagerBuilder *Builder = unwrap(PMB);
355 Builder->DisableUnrollLoops = Value;
359 LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
361 PassManagerBuilder *Builder = unwrap(PMB);
362 Builder->DisableSimplifyLibCalls = Value;
366 LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
367 unsigned Threshold) {
368 PassManagerBuilder *Builder = unwrap(PMB);
369 Builder->Inliner = createFunctionInliningPass(Threshold);
373 LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
374 LLVMPassManagerRef PM) {
375 PassManagerBuilder *Builder = unwrap(PMB);
376 FunctionPassManager *FPM = unwrap<FunctionPassManager>(PM);
377 Builder->populateFunctionPassManager(*FPM);
381 LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
382 LLVMPassManagerRef PM) {
383 PassManagerBuilder *Builder = unwrap(PMB);
384 PassManagerBase *MPM = unwrap(PM);
385 Builder->populateModulePassManager(*MPM);
388 void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
389 LLVMPassManagerRef PM,
392 PassManagerBuilder *Builder = unwrap(PMB);
393 PassManagerBase *LPM = unwrap(PM);
394 Builder->populateLTOPassManager(*LPM, Internalize, RunInliner);