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/IR/DataLayout.h"
21 #include "llvm/IR/Verifier.h"
22 #include "llvm/PassManager.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/ManagedStatic.h"
25 #include "llvm/Target/TargetLibraryInfo.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include "llvm/Target/TargetSubtargetInfo.h"
28 #include "llvm/Transforms/IPO.h"
29 #include "llvm/Transforms/Scalar.h"
30 #include "llvm/Transforms/Vectorize.h"
35 RunLoopVectorization("vectorize-loops", cl::Hidden,
36 cl::desc("Run the Loop vectorization passes"));
39 RunSLPVectorization("vectorize-slp", cl::Hidden,
40 cl::desc("Run the SLP vectorization passes"));
43 RunBBVectorization("vectorize-slp-aggressive", cl::Hidden,
44 cl::desc("Run the BB vectorization passes"));
47 UseGVNAfterVectorization("use-gvn-after-vectorization",
48 cl::init(false), cl::Hidden,
49 cl::desc("Run GVN instead of Early CSE after vectorization passes"));
51 static cl::opt<bool> UseNewSROA("use-new-sroa",
52 cl::init(true), cl::Hidden,
53 cl::desc("Enable the new, experimental SROA pass"));
56 RunLoopRerolling("reroll-loops", cl::Hidden,
57 cl::desc("Run the loop rerolling pass"));
59 static cl::opt<bool> RunLoadCombine("combine-loads", cl::init(false),
61 cl::desc("Run the load combining pass"));
64 RunSLPAfterLoopVectorization("run-slp-after-loop-vectorization",
65 cl::init(true), cl::Hidden,
66 cl::desc("Run the SLP vectorizer (and BB vectorizer) after the Loop "
67 "vectorizer instead of before"));
69 static cl::opt<bool> UseCFLAA("use-cfl-aa",
70 cl::init(false), cl::Hidden,
71 cl::desc("Enable the new, experimental CFL alias analysis"));
74 EnableMLSM("mlsm", cl::init(true), cl::Hidden,
75 cl::desc("Enable motion of merged load and store"));
77 PassManagerBuilder::PassManagerBuilder() {
80 LibraryInfo = nullptr;
82 DisableTailCalls = false;
83 DisableUnitAtATime = false;
84 DisableUnrollLoops = false;
85 BBVectorize = RunBBVectorization;
86 SLPVectorize = RunSLPVectorization;
87 LoopVectorize = RunLoopVectorization;
88 RerollLoops = RunLoopRerolling;
89 LoadCombine = RunLoadCombine;
90 DisableGVNLoadPRE = false;
94 MergeFunctions = false;
97 PassManagerBuilder::~PassManagerBuilder() {
102 /// Set of global extensions, automatically added as part of the standard set.
103 static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
104 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
106 void PassManagerBuilder::addGlobalExtension(
107 PassManagerBuilder::ExtensionPointTy Ty,
108 PassManagerBuilder::ExtensionFn Fn) {
109 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
112 void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
113 Extensions.push_back(std::make_pair(Ty, Fn));
116 void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
117 PassManagerBase &PM) const {
118 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
119 if ((*GlobalExtensions)[i].first == ETy)
120 (*GlobalExtensions)[i].second(*this, PM);
121 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
122 if (Extensions[i].first == ETy)
123 Extensions[i].second(*this, PM);
127 PassManagerBuilder::addInitialAliasAnalysisPasses(PassManagerBase &PM) const {
128 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
129 // BasicAliasAnalysis wins if they disagree. This is intended to help
130 // support "obvious" type-punning idioms.
132 PM.add(createCFLAliasAnalysisPass());
133 PM.add(createTypeBasedAliasAnalysisPass());
134 PM.add(createScopedNoAliasAAPass());
135 PM.add(createBasicAliasAnalysisPass());
138 void PassManagerBuilder::populateFunctionPassManager(FunctionPassManager &FPM) {
139 addExtensionsToPM(EP_EarlyAsPossible, FPM);
141 // Add LibraryInfo if we have some.
142 if (LibraryInfo) FPM.add(new TargetLibraryInfo(*LibraryInfo));
144 if (OptLevel == 0) return;
146 addInitialAliasAnalysisPasses(FPM);
148 FPM.add(createCFGSimplificationPass());
150 FPM.add(createSROAPass());
152 FPM.add(createScalarReplAggregatesPass());
153 FPM.add(createEarlyCSEPass());
154 FPM.add(createLowerExpectIntrinsicPass());
157 void PassManagerBuilder::populateModulePassManager(PassManagerBase &MPM) {
158 // If all optimizations are disabled, just run the always-inline pass.
165 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
166 // pass manager, but we don't want to add extensions into that pass manager.
167 // To prevent this we must insert a no-op module pass to reset the pass
168 // manager to get the same behavior as EP_OptimizerLast in non-O0 builds.
169 if (!GlobalExtensions->empty() || !Extensions.empty())
170 MPM.add(createBarrierNoopPass());
172 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
176 // Add LibraryInfo if we have some.
177 if (LibraryInfo) MPM.add(new TargetLibraryInfo(*LibraryInfo));
179 addInitialAliasAnalysisPasses(MPM);
181 if (!DisableUnitAtATime) {
182 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
184 MPM.add(createIPSCCPPass()); // IP SCCP
185 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
187 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
189 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
190 addExtensionsToPM(EP_Peephole, MPM);
191 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
194 // Start of CallGraph SCC passes.
195 if (!DisableUnitAtATime)
196 MPM.add(createPruneEHPass()); // Remove dead EH info
201 if (!DisableUnitAtATime)
202 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
204 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
206 // Start of function pass.
207 // Break up aggregate allocas, using SSAUpdater.
209 MPM.add(createSROAPass(/*RequiresDomTree*/ false));
211 MPM.add(createScalarReplAggregatesPass(-1, false));
212 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
213 MPM.add(createJumpThreadingPass()); // Thread jumps.
214 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
215 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
216 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
217 addExtensionsToPM(EP_Peephole, MPM);
219 if (!DisableTailCalls)
220 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
221 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
222 MPM.add(createReassociatePass()); // Reassociate expressions
223 MPM.add(createLoopRotatePass()); // Rotate Loop
224 MPM.add(createLICMPass()); // Hoist loop invariants
225 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
226 MPM.add(createInstructionCombiningPass());
227 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
228 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
229 MPM.add(createLoopDeletionPass()); // Delete dead loops
231 if (!DisableUnrollLoops)
232 MPM.add(createSimpleLoopUnrollPass()); // Unroll small loops
233 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
237 MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds
238 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
240 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
241 MPM.add(createSCCPPass()); // Constant prop with SCCP
243 // Run instcombine after redundancy elimination to exploit opportunities
244 // opened up by them.
245 MPM.add(createInstructionCombiningPass());
246 addExtensionsToPM(EP_Peephole, MPM);
247 MPM.add(createJumpThreadingPass()); // Thread jumps
248 MPM.add(createCorrelatedValuePropagationPass());
249 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
251 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
254 MPM.add(createLoopRerollPass());
255 if (!RunSLPAfterLoopVectorization) {
257 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
260 MPM.add(createBBVectorizePass());
261 MPM.add(createInstructionCombiningPass());
262 addExtensionsToPM(EP_Peephole, MPM);
263 if (OptLevel > 1 && UseGVNAfterVectorization)
264 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
266 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
268 // BBVectorize may have significantly shortened a loop body; unroll again.
269 if (!DisableUnrollLoops)
270 MPM.add(createLoopUnrollPass());
275 MPM.add(createLoadCombinePass());
277 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
278 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
279 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
280 addExtensionsToPM(EP_Peephole, MPM);
282 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
283 // pass manager that we are specifically trying to avoid. To prevent this
284 // we must insert a no-op module pass to reset the pass manager.
285 MPM.add(createBarrierNoopPass());
286 MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
287 // FIXME: Because of #pragma vectorize enable, the passes below are always
288 // inserted in the pipeline, even when the vectorizer doesn't run (ex. when
289 // on -O1 and no #pragma is found). Would be good to have these two passes
290 // as function calls, so that we can only pass them when the vectorizer
292 MPM.add(createInstructionCombiningPass());
294 if (RunSLPAfterLoopVectorization) {
296 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
299 MPM.add(createBBVectorizePass());
300 MPM.add(createInstructionCombiningPass());
301 addExtensionsToPM(EP_Peephole, MPM);
302 if (OptLevel > 1 && UseGVNAfterVectorization)
303 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
305 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
307 // BBVectorize may have significantly shortened a loop body; unroll again.
308 if (!DisableUnrollLoops)
309 MPM.add(createLoopUnrollPass());
313 addExtensionsToPM(EP_Peephole, MPM);
314 MPM.add(createCFGSimplificationPass());
316 if (!DisableUnrollLoops)
317 MPM.add(createLoopUnrollPass()); // Unroll small loops
319 // After vectorization and unrolling, assume intrinsics may tell us more
320 // about pointer alignments.
321 MPM.add(createAlignmentFromAssumptionsPass());
323 if (!DisableUnitAtATime) {
324 // FIXME: We shouldn't bother with this anymore.
325 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
327 // GlobalOpt already deletes dead functions and globals, at -O2 try a
328 // late pass of GlobalDCE. It is capable of deleting dead cycles.
330 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
331 MPM.add(createConstantMergePass()); // Merge dup global constants
336 MPM.add(createMergeFunctionsPass());
338 addExtensionsToPM(EP_OptimizerLast, MPM);
341 void PassManagerBuilder::addLTOOptimizationPasses(PassManagerBase &PM) {
342 // Provide AliasAnalysis services for optimizations.
343 addInitialAliasAnalysisPasses(PM);
345 // Propagate constants at call sites into the functions they call. This
346 // opens opportunities for globalopt (and inlining) by substituting function
347 // pointers passed as arguments to direct uses of functions.
348 PM.add(createIPSCCPPass());
350 // Now that we internalized some globals, see if we can hack on them!
351 PM.add(createGlobalOptimizerPass());
353 // Linking modules together can lead to duplicated global constants, only
354 // keep one copy of each constant.
355 PM.add(createConstantMergePass());
357 // Remove unused arguments from functions.
358 PM.add(createDeadArgEliminationPass());
360 // Reduce the code after globalopt and ipsccp. Both can open up significant
361 // simplification opportunities, and both can propagate functions through
362 // function pointers. When this happens, we often have to resolve varargs
363 // calls, etc, so let instcombine do this.
364 PM.add(createInstructionCombiningPass());
365 addExtensionsToPM(EP_Peephole, PM);
367 // Inline small functions
368 bool RunInliner = Inliner;
374 PM.add(createPruneEHPass()); // Remove dead EH info.
376 // Optimize globals again if we ran the inliner.
378 PM.add(createGlobalOptimizerPass());
379 PM.add(createGlobalDCEPass()); // Remove dead functions.
381 // If we didn't decide to inline a function, check to see if we can
382 // transform it to pass arguments by value instead of by reference.
383 PM.add(createArgumentPromotionPass());
385 // The IPO passes may leave cruft around. Clean up after them.
386 PM.add(createInstructionCombiningPass());
387 addExtensionsToPM(EP_Peephole, PM);
388 PM.add(createJumpThreadingPass());
392 PM.add(createSROAPass());
394 PM.add(createScalarReplAggregatesPass());
396 // Run a few AA driven optimizations here and now, to cleanup the code.
397 PM.add(createFunctionAttrsPass()); // Add nocapture.
398 PM.add(createGlobalsModRefPass()); // IP alias analysis.
400 PM.add(createLICMPass()); // Hoist loop invariants.
402 PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds.
403 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
404 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
407 PM.add(createDeadStoreEliminationPass());
409 // More loops are countable; try to optimize them.
410 PM.add(createIndVarSimplifyPass());
411 PM.add(createLoopDeletionPass());
412 PM.add(createLoopVectorizePass(true, true));
414 // More scalar chains could be vectorized due to more alias information
415 PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
417 // After vectorization, assume intrinsics may tell us more about pointer
419 PM.add(createAlignmentFromAssumptionsPass());
422 PM.add(createLoadCombinePass());
424 // Cleanup and simplify the code after the scalar optimizations.
425 PM.add(createInstructionCombiningPass());
426 addExtensionsToPM(EP_Peephole, PM);
428 PM.add(createJumpThreadingPass());
430 // Delete basic blocks, which optimization passes may have killed.
431 PM.add(createCFGSimplificationPass());
433 // Now that we have optimized the program, discard unreachable functions.
434 PM.add(createGlobalDCEPass());
436 // FIXME: this is profitable (for compiler time) to do at -O0 too, but
437 // currently it damages debug info.
439 PM.add(createMergeFunctionsPass());
442 void PassManagerBuilder::populateLTOPassManager(PassManagerBase &PM,
445 PM.add(new DataLayoutPass());
446 TM->addAnalysisPasses(PM);
450 PM.add(new TargetLibraryInfo(*LibraryInfo));
453 PM.add(createVerifierPass());
456 PM.add(createStripSymbolsPass(true));
459 PM.add(createDebugInfoVerifierPass());
462 addLTOOptimizationPasses(PM);
465 PM.add(createVerifierPass());
466 PM.add(createDebugInfoVerifierPass());
470 inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
471 return reinterpret_cast<PassManagerBuilder*>(P);
474 inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
475 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
478 LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
479 PassManagerBuilder *PMB = new PassManagerBuilder();
483 void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
484 PassManagerBuilder *Builder = unwrap(PMB);
489 LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
491 PassManagerBuilder *Builder = unwrap(PMB);
492 Builder->OptLevel = OptLevel;
496 LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
497 unsigned SizeLevel) {
498 PassManagerBuilder *Builder = unwrap(PMB);
499 Builder->SizeLevel = SizeLevel;
503 LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
505 PassManagerBuilder *Builder = unwrap(PMB);
506 Builder->DisableUnitAtATime = Value;
510 LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
512 PassManagerBuilder *Builder = unwrap(PMB);
513 Builder->DisableUnrollLoops = Value;
517 LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
519 // NOTE: The simplify-libcalls pass has been removed.
523 LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
524 unsigned Threshold) {
525 PassManagerBuilder *Builder = unwrap(PMB);
526 Builder->Inliner = createFunctionInliningPass(Threshold);
530 LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
531 LLVMPassManagerRef PM) {
532 PassManagerBuilder *Builder = unwrap(PMB);
533 FunctionPassManager *FPM = unwrap<FunctionPassManager>(PM);
534 Builder->populateFunctionPassManager(*FPM);
538 LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
539 LLVMPassManagerRef PM) {
540 PassManagerBuilder *Builder = unwrap(PMB);
541 PassManagerBase *MPM = unwrap(PM);
542 Builder->populateModulePassManager(*MPM);
545 void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
546 LLVMPassManagerRef PM,
547 LLVMBool Internalize,
548 LLVMBool RunInliner) {
549 PassManagerBuilder *Builder = unwrap(PMB);
550 PassManagerBase *LPM = unwrap(PM);
552 // A small backwards compatibility hack. populateLTOPassManager used to take
553 // an RunInliner option.
554 if (RunInliner && !Builder->Inliner)
555 Builder->Inliner = createFunctionInliningPass();
557 Builder->populateLTOPassManager(*LPM);