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/Wrap.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/ManagedStatic.h"
25 #include "llvm/Target/TargetLibraryInfo.h"
26 #include "llvm/Transforms/IPO.h"
27 #include "llvm/Transforms/Scalar.h"
28 #include "llvm/Transforms/Vectorize.h"
33 RunLoopVectorization("vectorize-loops",
34 cl::desc("Run the Loop vectorization passes"));
37 RunSLPVectorization("vectorize-slp",
38 cl::desc("Run the SLP vectorization passes"));
41 RunBBVectorization("vectorize-slp-aggressive",
42 cl::desc("Run the BB vectorization passes"));
45 UseGVNAfterVectorization("use-gvn-after-vectorization",
46 cl::init(false), cl::Hidden,
47 cl::desc("Run GVN instead of Early CSE after vectorization passes"));
49 static cl::opt<bool> UseNewSROA("use-new-sroa",
50 cl::init(true), cl::Hidden,
51 cl::desc("Enable the new, experimental SROA pass"));
53 PassManagerBuilder::PassManagerBuilder() {
58 DisableSimplifyLibCalls = false;
59 DisableUnitAtATime = false;
60 DisableUnrollLoops = false;
61 BBVectorize = RunBBVectorization;
62 SLPVectorize = RunSLPVectorization;
63 LoopVectorize = RunLoopVectorization;
66 PassManagerBuilder::~PassManagerBuilder() {
71 /// Set of global extensions, automatically added as part of the standard set.
72 static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
73 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
75 void PassManagerBuilder::addGlobalExtension(
76 PassManagerBuilder::ExtensionPointTy Ty,
77 PassManagerBuilder::ExtensionFn Fn) {
78 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
81 void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
82 Extensions.push_back(std::make_pair(Ty, Fn));
85 void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
86 PassManagerBase &PM) const {
87 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
88 if ((*GlobalExtensions)[i].first == ETy)
89 (*GlobalExtensions)[i].second(*this, PM);
90 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
91 if (Extensions[i].first == ETy)
92 Extensions[i].second(*this, PM);
96 PassManagerBuilder::addInitialAliasAnalysisPasses(PassManagerBase &PM) const {
97 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
98 // BasicAliasAnalysis wins if they disagree. This is intended to help
99 // support "obvious" type-punning idioms.
100 PM.add(createTypeBasedAliasAnalysisPass());
101 PM.add(createBasicAliasAnalysisPass());
104 void PassManagerBuilder::populateFunctionPassManager(FunctionPassManager &FPM) {
105 addExtensionsToPM(EP_EarlyAsPossible, FPM);
107 // Add LibraryInfo if we have some.
108 if (LibraryInfo) FPM.add(new TargetLibraryInfo(*LibraryInfo));
110 if (OptLevel == 0) return;
112 addInitialAliasAnalysisPasses(FPM);
114 FPM.add(createCFGSimplificationPass());
116 FPM.add(createSROAPass());
118 FPM.add(createScalarReplAggregatesPass());
119 FPM.add(createEarlyCSEPass());
120 FPM.add(createLowerExpectIntrinsicPass());
123 void PassManagerBuilder::populateModulePassManager(PassManagerBase &MPM) {
124 // If all optimizations are disabled, just run the always-inline pass.
131 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
132 // pass manager, but we don't want to add extensions into that pass manager.
133 // To prevent this we must insert a no-op module pass to reset the pass
134 // manager to get the same behavior as EP_OptimizerLast in non-O0 builds.
135 if (!GlobalExtensions->empty() || !Extensions.empty())
136 MPM.add(createBarrierNoopPass());
138 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
142 // Add LibraryInfo if we have some.
143 if (LibraryInfo) MPM.add(new TargetLibraryInfo(*LibraryInfo));
145 addInitialAliasAnalysisPasses(MPM);
147 if (!DisableUnitAtATime) {
148 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
150 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
152 MPM.add(createIPSCCPPass()); // IP SCCP
153 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
155 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
156 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
159 // Start of CallGraph SCC passes.
160 if (!DisableUnitAtATime)
161 MPM.add(createPruneEHPass()); // Remove dead EH info
166 if (!DisableUnitAtATime)
167 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
169 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
171 // Start of function pass.
172 // Break up aggregate allocas, using SSAUpdater.
174 MPM.add(createSROAPass(/*RequiresDomTree*/ false));
176 MPM.add(createScalarReplAggregatesPass(-1, false));
177 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
178 if (!DisableSimplifyLibCalls)
179 MPM.add(createSimplifyLibCallsPass()); // Library Call Optimizations
180 MPM.add(createJumpThreadingPass()); // Thread jumps.
181 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
182 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
183 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
185 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
186 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
187 MPM.add(createReassociatePass()); // Reassociate expressions
188 MPM.add(createLoopRotatePass()); // Rotate Loop
189 MPM.add(createLICMPass()); // Hoist loop invariants
190 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
191 MPM.add(createInstructionCombiningPass());
192 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
193 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
194 MPM.add(createLoopDeletionPass()); // Delete dead loops
196 if (LoopVectorize && OptLevel > 2)
197 MPM.add(createLoopVectorizePass());
199 if (!DisableUnrollLoops)
200 MPM.add(createLoopUnrollPass()); // Unroll small loops
201 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
204 MPM.add(createGVNPass()); // Remove redundancies
205 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
206 MPM.add(createSCCPPass()); // Constant prop with SCCP
208 // Run instcombine after redundancy elimination to exploit opportunities
209 // opened up by them.
210 MPM.add(createInstructionCombiningPass());
211 MPM.add(createJumpThreadingPass()); // Thread jumps
212 MPM.add(createCorrelatedValuePropagationPass());
213 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
215 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
218 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
221 MPM.add(createBBVectorizePass());
222 MPM.add(createInstructionCombiningPass());
223 if (OptLevel > 1 && UseGVNAfterVectorization)
224 MPM.add(createGVNPass()); // Remove redundancies
226 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
228 // BBVectorize may have significantly shortened a loop body; unroll again.
229 if (!DisableUnrollLoops)
230 MPM.add(createLoopUnrollPass());
233 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
234 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
235 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
237 if (!DisableUnitAtATime) {
238 // FIXME: We shouldn't bother with this anymore.
239 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
241 // GlobalOpt already deletes dead functions and globals, at -O2 try a
242 // late pass of GlobalDCE. It is capable of deleting dead cycles.
244 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
245 MPM.add(createConstantMergePass()); // Merge dup global constants
248 addExtensionsToPM(EP_OptimizerLast, MPM);
251 void PassManagerBuilder::populateLTOPassManager(PassManagerBase &PM,
254 bool DisableGVNLoadPRE) {
255 // Provide AliasAnalysis services for optimizations.
256 addInitialAliasAnalysisPasses(PM);
258 // Now that composite has been compiled, scan through the module, looking
259 // for a main function. If main is defined, mark all other functions
262 std::vector<const char*> E;
264 PM.add(createInternalizePass(E));
267 // Propagate constants at call sites into the functions they call. This
268 // opens opportunities for globalopt (and inlining) by substituting function
269 // pointers passed as arguments to direct uses of functions.
270 PM.add(createIPSCCPPass());
272 // Now that we internalized some globals, see if we can hack on them!
273 PM.add(createGlobalOptimizerPass());
275 // Linking modules together can lead to duplicated global constants, only
276 // keep one copy of each constant.
277 PM.add(createConstantMergePass());
279 // Remove unused arguments from functions.
280 PM.add(createDeadArgEliminationPass());
282 // Reduce the code after globalopt and ipsccp. Both can open up significant
283 // simplification opportunities, and both can propagate functions through
284 // function pointers. When this happens, we often have to resolve varargs
285 // calls, etc, so let instcombine do this.
286 PM.add(createInstructionCombiningPass());
288 // Inline small functions
290 PM.add(createFunctionInliningPass());
292 PM.add(createPruneEHPass()); // Remove dead EH info.
294 // Optimize globals again if we ran the inliner.
296 PM.add(createGlobalOptimizerPass());
297 PM.add(createGlobalDCEPass()); // Remove dead functions.
299 // If we didn't decide to inline a function, check to see if we can
300 // transform it to pass arguments by value instead of by reference.
301 PM.add(createArgumentPromotionPass());
303 // The IPO passes may leave cruft around. Clean up after them.
304 PM.add(createInstructionCombiningPass());
305 PM.add(createJumpThreadingPass());
308 PM.add(createSROAPass());
310 PM.add(createScalarReplAggregatesPass());
312 // Run a few AA driven optimizations here and now, to cleanup the code.
313 PM.add(createFunctionAttrsPass()); // Add nocapture.
314 PM.add(createGlobalsModRefPass()); // IP alias analysis.
316 PM.add(createLICMPass()); // Hoist loop invariants.
317 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
318 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
320 PM.add(createDeadStoreEliminationPass());
322 // Cleanup and simplify the code after the scalar optimizations.
323 PM.add(createInstructionCombiningPass());
325 PM.add(createJumpThreadingPass());
327 // Delete basic blocks, which optimization passes may have killed.
328 PM.add(createCFGSimplificationPass());
330 // Now that we have optimized the program, discard unreachable functions.
331 PM.add(createGlobalDCEPass());
334 inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
335 return reinterpret_cast<PassManagerBuilder*>(P);
338 inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
339 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
342 LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
343 PassManagerBuilder *PMB = new PassManagerBuilder();
347 void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
348 PassManagerBuilder *Builder = unwrap(PMB);
353 LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
355 PassManagerBuilder *Builder = unwrap(PMB);
356 Builder->OptLevel = OptLevel;
360 LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
361 unsigned SizeLevel) {
362 PassManagerBuilder *Builder = unwrap(PMB);
363 Builder->SizeLevel = SizeLevel;
367 LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
369 PassManagerBuilder *Builder = unwrap(PMB);
370 Builder->DisableUnitAtATime = Value;
374 LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
376 PassManagerBuilder *Builder = unwrap(PMB);
377 Builder->DisableUnrollLoops = Value;
381 LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
383 PassManagerBuilder *Builder = unwrap(PMB);
384 Builder->DisableSimplifyLibCalls = Value;
388 LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
389 unsigned Threshold) {
390 PassManagerBuilder *Builder = unwrap(PMB);
391 Builder->Inliner = createFunctionInliningPass(Threshold);
395 LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
396 LLVMPassManagerRef PM) {
397 PassManagerBuilder *Builder = unwrap(PMB);
398 FunctionPassManager *FPM = unwrap<FunctionPassManager>(PM);
399 Builder->populateFunctionPassManager(*FPM);
403 LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
404 LLVMPassManagerRef PM) {
405 PassManagerBuilder *Builder = unwrap(PMB);
406 PassManagerBase *MPM = unwrap(PM);
407 Builder->populateModulePassManager(*MPM);
410 void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
411 LLVMPassManagerRef PM,
412 LLVMBool Internalize,
413 LLVMBool RunInliner) {
414 PassManagerBuilder *Builder = unwrap(PMB);
415 PassManagerBase *LPM = unwrap(PM);
416 Builder->populateLTOPassManager(*LPM, Internalize != 0, RunInliner != 0);