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 LateVectorization("late-vectorize", cl::init(false), cl::Hidden,
37 cl::desc("Run the vectorization pasess late in the pass "
38 "pipeline (after the inliner)"));
41 RunSLPVectorization("vectorize-slp",
42 cl::desc("Run the SLP vectorization passes"));
45 RunBBVectorization("vectorize-slp-aggressive",
46 cl::desc("Run the BB vectorization passes"));
49 UseGVNAfterVectorization("use-gvn-after-vectorization",
50 cl::init(false), cl::Hidden,
51 cl::desc("Run GVN instead of Early CSE after vectorization passes"));
53 static cl::opt<bool> UseNewSROA("use-new-sroa",
54 cl::init(true), cl::Hidden,
55 cl::desc("Enable the new, experimental SROA pass"));
57 PassManagerBuilder::PassManagerBuilder() {
62 DisableUnitAtATime = false;
63 DisableUnrollLoops = false;
64 BBVectorize = RunBBVectorization;
65 SLPVectorize = RunSLPVectorization;
66 LoopVectorize = RunLoopVectorization;
67 LateVectorize = LateVectorization;
70 PassManagerBuilder::~PassManagerBuilder() {
75 /// Set of global extensions, automatically added as part of the standard set.
76 static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
77 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
79 void PassManagerBuilder::addGlobalExtension(
80 PassManagerBuilder::ExtensionPointTy Ty,
81 PassManagerBuilder::ExtensionFn Fn) {
82 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
85 void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
86 Extensions.push_back(std::make_pair(Ty, Fn));
89 void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
90 PassManagerBase &PM) const {
91 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
92 if ((*GlobalExtensions)[i].first == ETy)
93 (*GlobalExtensions)[i].second(*this, PM);
94 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
95 if (Extensions[i].first == ETy)
96 Extensions[i].second(*this, PM);
100 PassManagerBuilder::addInitialAliasAnalysisPasses(PassManagerBase &PM) const {
101 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
102 // BasicAliasAnalysis wins if they disagree. This is intended to help
103 // support "obvious" type-punning idioms.
104 PM.add(createTypeBasedAliasAnalysisPass());
105 PM.add(createBasicAliasAnalysisPass());
108 void PassManagerBuilder::populateFunctionPassManager(FunctionPassManager &FPM) {
109 addExtensionsToPM(EP_EarlyAsPossible, FPM);
111 // Add LibraryInfo if we have some.
112 if (LibraryInfo) FPM.add(new TargetLibraryInfo(*LibraryInfo));
114 if (OptLevel == 0) return;
116 addInitialAliasAnalysisPasses(FPM);
118 FPM.add(createCFGSimplificationPass());
120 FPM.add(createSROAPass());
122 FPM.add(createScalarReplAggregatesPass());
123 FPM.add(createEarlyCSEPass());
124 FPM.add(createLowerExpectIntrinsicPass());
127 void PassManagerBuilder::populateModulePassManager(PassManagerBase &MPM) {
128 // If all optimizations are disabled, just run the always-inline pass.
135 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
136 // pass manager, but we don't want to add extensions into that pass manager.
137 // To prevent this we must insert a no-op module pass to reset the pass
138 // manager to get the same behavior as EP_OptimizerLast in non-O0 builds.
139 if (!GlobalExtensions->empty() || !Extensions.empty())
140 MPM.add(createBarrierNoopPass());
142 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
146 // Add LibraryInfo if we have some.
147 if (LibraryInfo) MPM.add(new TargetLibraryInfo(*LibraryInfo));
149 addInitialAliasAnalysisPasses(MPM);
151 if (!DisableUnitAtATime) {
152 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
154 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
156 MPM.add(createIPSCCPPass()); // IP SCCP
157 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
159 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
160 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
163 // Start of CallGraph SCC passes.
164 if (!DisableUnitAtATime)
165 MPM.add(createPruneEHPass()); // Remove dead EH info
170 if (!DisableUnitAtATime)
171 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
173 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
175 // Start of function pass.
176 // Break up aggregate allocas, using SSAUpdater.
178 MPM.add(createSROAPass(/*RequiresDomTree*/ false));
180 MPM.add(createScalarReplAggregatesPass(-1, false));
181 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
182 MPM.add(createJumpThreadingPass()); // Thread jumps.
183 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
184 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
185 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
187 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
188 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
189 MPM.add(createReassociatePass()); // Reassociate expressions
190 MPM.add(createLoopRotatePass()); // Rotate Loop
191 MPM.add(createLICMPass()); // Hoist loop invariants
192 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
193 MPM.add(createInstructionCombiningPass());
194 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
195 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
196 MPM.add(createLoopDeletionPass()); // Delete dead loops
198 if (!LateVectorize && LoopVectorize && OptLevel > 1 && SizeLevel < 2)
199 MPM.add(createLoopVectorizePass());
201 if (!DisableUnrollLoops)
202 MPM.add(createLoopUnrollPass()); // Unroll small loops
203 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
206 MPM.add(createGVNPass()); // Remove redundancies
207 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
208 MPM.add(createSCCPPass()); // Constant prop with SCCP
210 // Run instcombine after redundancy elimination to exploit opportunities
211 // opened up by them.
212 MPM.add(createInstructionCombiningPass());
213 MPM.add(createJumpThreadingPass()); // Thread jumps
214 MPM.add(createCorrelatedValuePropagationPass());
215 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
217 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
219 if (!LateVectorize) {
221 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
224 MPM.add(createBBVectorizePass());
225 MPM.add(createInstructionCombiningPass());
226 if (OptLevel > 1 && UseGVNAfterVectorization)
227 MPM.add(createGVNPass()); // Remove redundancies
229 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
231 // BBVectorize may have significantly shortened a loop body; unroll again.
232 if (!DisableUnrollLoops)
233 MPM.add(createLoopUnrollPass());
237 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
238 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
239 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
241 // As an experimental mode, run any vectorization passes in a separate
242 // pipeline from the CGSCC pass manager that runs iteratively with the
245 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
246 // pass manager that we are specifically trying to avoid. To prevent this
247 // we must insert a no-op module pass to reset the pass manager.
248 MPM.add(createBarrierNoopPass());
250 // Add the various vectorization passes and relevant cleanup passes for
251 // them since we are no longer in the middle of the main scalar pipeline.
252 if (LoopVectorize && OptLevel > 1 && SizeLevel < 2) {
253 MPM.add(createLoopVectorizePass());
255 if (!DisableUnrollLoops)
256 MPM.add(createLoopUnrollPass()); // Unroll small loops
258 // FIXME: Is this necessary/useful? Should we also do SimplifyCFG?
259 MPM.add(createInstructionCombiningPass());
263 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
265 // FIXME: Is this necessary/useful? Should we also do SimplifyCFG?
266 MPM.add(createInstructionCombiningPass());
270 MPM.add(createBBVectorizePass());
271 MPM.add(createInstructionCombiningPass());
272 if (OptLevel > 1 && UseGVNAfterVectorization)
273 MPM.add(createGVNPass()); // Remove redundancies
275 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
277 // BBVectorize may have significantly shortened a loop body; unroll again.
278 if (!DisableUnrollLoops)
279 MPM.add(createLoopUnrollPass());
283 if (!DisableUnitAtATime) {
284 // FIXME: We shouldn't bother with this anymore.
285 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
287 // GlobalOpt already deletes dead functions and globals, at -O2 try a
288 // late pass of GlobalDCE. It is capable of deleting dead cycles.
290 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
291 MPM.add(createConstantMergePass()); // Merge dup global constants
294 addExtensionsToPM(EP_OptimizerLast, MPM);
297 void PassManagerBuilder::populateLTOPassManager(PassManagerBase &PM,
300 bool DisableGVNLoadPRE) {
301 // Provide AliasAnalysis services for optimizations.
302 addInitialAliasAnalysisPasses(PM);
304 // Now that composite has been compiled, scan through the module, looking
305 // for a main function. If main is defined, mark all other functions
308 std::vector<const char*> E;
310 PM.add(createInternalizePass(E));
313 // Propagate constants at call sites into the functions they call. This
314 // opens opportunities for globalopt (and inlining) by substituting function
315 // pointers passed as arguments to direct uses of functions.
316 PM.add(createIPSCCPPass());
318 // Now that we internalized some globals, see if we can hack on them!
319 PM.add(createGlobalOptimizerPass());
321 // Linking modules together can lead to duplicated global constants, only
322 // keep one copy of each constant.
323 PM.add(createConstantMergePass());
325 // Remove unused arguments from functions.
326 PM.add(createDeadArgEliminationPass());
328 // Reduce the code after globalopt and ipsccp. Both can open up significant
329 // simplification opportunities, and both can propagate functions through
330 // function pointers. When this happens, we often have to resolve varargs
331 // calls, etc, so let instcombine do this.
332 PM.add(createInstructionCombiningPass());
334 // Inline small functions
336 PM.add(createFunctionInliningPass());
338 PM.add(createPruneEHPass()); // Remove dead EH info.
340 // Optimize globals again if we ran the inliner.
342 PM.add(createGlobalOptimizerPass());
343 PM.add(createGlobalDCEPass()); // Remove dead functions.
345 // If we didn't decide to inline a function, check to see if we can
346 // transform it to pass arguments by value instead of by reference.
347 PM.add(createArgumentPromotionPass());
349 // The IPO passes may leave cruft around. Clean up after them.
350 PM.add(createInstructionCombiningPass());
351 PM.add(createJumpThreadingPass());
354 PM.add(createSROAPass());
356 PM.add(createScalarReplAggregatesPass());
358 // Run a few AA driven optimizations here and now, to cleanup the code.
359 PM.add(createFunctionAttrsPass()); // Add nocapture.
360 PM.add(createGlobalsModRefPass()); // IP alias analysis.
362 PM.add(createLICMPass()); // Hoist loop invariants.
363 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
364 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
366 PM.add(createDeadStoreEliminationPass());
368 // Cleanup and simplify the code after the scalar optimizations.
369 PM.add(createInstructionCombiningPass());
371 PM.add(createJumpThreadingPass());
373 // Delete basic blocks, which optimization passes may have killed.
374 PM.add(createCFGSimplificationPass());
376 // Now that we have optimized the program, discard unreachable functions.
377 PM.add(createGlobalDCEPass());
380 inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
381 return reinterpret_cast<PassManagerBuilder*>(P);
384 inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
385 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
388 LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
389 PassManagerBuilder *PMB = new PassManagerBuilder();
393 void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
394 PassManagerBuilder *Builder = unwrap(PMB);
399 LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
401 PassManagerBuilder *Builder = unwrap(PMB);
402 Builder->OptLevel = OptLevel;
406 LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
407 unsigned SizeLevel) {
408 PassManagerBuilder *Builder = unwrap(PMB);
409 Builder->SizeLevel = SizeLevel;
413 LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
415 PassManagerBuilder *Builder = unwrap(PMB);
416 Builder->DisableUnitAtATime = Value;
420 LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
422 PassManagerBuilder *Builder = unwrap(PMB);
423 Builder->DisableUnrollLoops = Value;
427 LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
429 // NOTE: The simplify-libcalls pass has been removed.
433 LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
434 unsigned Threshold) {
435 PassManagerBuilder *Builder = unwrap(PMB);
436 Builder->Inliner = createFunctionInliningPass(Threshold);
440 LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
441 LLVMPassManagerRef PM) {
442 PassManagerBuilder *Builder = unwrap(PMB);
443 FunctionPassManager *FPM = unwrap<FunctionPassManager>(PM);
444 Builder->populateFunctionPassManager(*FPM);
448 LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
449 LLVMPassManagerRef PM) {
450 PassManagerBuilder *Builder = unwrap(PMB);
451 PassManagerBase *MPM = unwrap(PM);
452 Builder->populateModulePassManager(*MPM);
455 void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
456 LLVMPassManagerRef PM,
457 LLVMBool Internalize,
458 LLVMBool RunInliner) {
459 PassManagerBuilder *Builder = unwrap(PMB);
460 PassManagerBase *LPM = unwrap(PM);
461 Builder->populateLTOPassManager(*LPM, Internalize != 0, RunInliner != 0);