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", cl::Hidden,
33 cl::desc("Run the Loop vectorization passes"));
36 LateVectorization("late-vectorize", cl::init(true), cl::Hidden,
37 cl::desc("Run the vectorization pasess late in the pass "
38 "pipeline (after the inliner)"));
41 RunSLPVectorization("vectorize-slp", cl::Hidden,
42 cl::desc("Run the SLP vectorization passes"));
45 RunBBVectorization("vectorize-slp-aggressive", cl::Hidden,
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"));
58 RunLoopRerolling("reroll-loops", cl::Hidden,
59 cl::desc("Run the loop rerolling pass"));
61 PassManagerBuilder::PassManagerBuilder() {
66 DisableUnitAtATime = false;
67 DisableUnrollLoops = false;
68 BBVectorize = RunBBVectorization;
69 SLPVectorize = RunSLPVectorization;
70 LoopVectorize = RunLoopVectorization;
71 LateVectorize = LateVectorization;
74 PassManagerBuilder::~PassManagerBuilder() {
79 /// Set of global extensions, automatically added as part of the standard set.
80 static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
81 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
83 void PassManagerBuilder::addGlobalExtension(
84 PassManagerBuilder::ExtensionPointTy Ty,
85 PassManagerBuilder::ExtensionFn Fn) {
86 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
89 void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
90 Extensions.push_back(std::make_pair(Ty, Fn));
93 void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
94 PassManagerBase &PM) const {
95 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
96 if ((*GlobalExtensions)[i].first == ETy)
97 (*GlobalExtensions)[i].second(*this, PM);
98 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
99 if (Extensions[i].first == ETy)
100 Extensions[i].second(*this, PM);
104 PassManagerBuilder::addInitialAliasAnalysisPasses(PassManagerBase &PM) const {
105 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
106 // BasicAliasAnalysis wins if they disagree. This is intended to help
107 // support "obvious" type-punning idioms.
108 PM.add(createTypeBasedAliasAnalysisPass());
109 PM.add(createBasicAliasAnalysisPass());
112 void PassManagerBuilder::populateFunctionPassManager(FunctionPassManager &FPM) {
113 addExtensionsToPM(EP_EarlyAsPossible, FPM);
115 // Add LibraryInfo if we have some.
116 if (LibraryInfo) FPM.add(new TargetLibraryInfo(*LibraryInfo));
118 if (OptLevel == 0) return;
120 addInitialAliasAnalysisPasses(FPM);
122 FPM.add(createCFGSimplificationPass());
124 FPM.add(createSROAPass());
126 FPM.add(createScalarReplAggregatesPass());
127 FPM.add(createEarlyCSEPass());
128 FPM.add(createLowerExpectIntrinsicPass());
131 void PassManagerBuilder::populateModulePassManager(PassManagerBase &MPM) {
132 // If all optimizations are disabled, just run the always-inline pass.
139 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
140 // pass manager, but we don't want to add extensions into that pass manager.
141 // To prevent this we must insert a no-op module pass to reset the pass
142 // manager to get the same behavior as EP_OptimizerLast in non-O0 builds.
143 if (!GlobalExtensions->empty() || !Extensions.empty())
144 MPM.add(createBarrierNoopPass());
146 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
150 // Add LibraryInfo if we have some.
151 if (LibraryInfo) MPM.add(new TargetLibraryInfo(*LibraryInfo));
153 addInitialAliasAnalysisPasses(MPM);
155 if (!DisableUnitAtATime) {
156 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
158 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
160 MPM.add(createIPSCCPPass()); // IP SCCP
161 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
163 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
164 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
167 // Start of CallGraph SCC passes.
168 if (!DisableUnitAtATime)
169 MPM.add(createPruneEHPass()); // Remove dead EH info
174 if (!DisableUnitAtATime)
175 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
177 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
179 // Start of function pass.
180 // Break up aggregate allocas, using SSAUpdater.
182 MPM.add(createSROAPass(/*RequiresDomTree*/ false));
184 MPM.add(createScalarReplAggregatesPass(-1, false));
185 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
186 MPM.add(createJumpThreadingPass()); // Thread jumps.
187 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
188 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
189 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
191 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
192 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
193 MPM.add(createReassociatePass()); // Reassociate expressions
194 MPM.add(createLoopRotatePass()); // Rotate Loop
195 MPM.add(createLICMPass()); // Hoist loop invariants
196 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
197 MPM.add(createInstructionCombiningPass());
198 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
199 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
200 MPM.add(createLoopDeletionPass()); // Delete dead loops
202 if (!LateVectorize && LoopVectorize)
203 MPM.add(createLoopVectorizePass(DisableUnrollLoops));
205 if (!DisableUnrollLoops)
206 MPM.add(createLoopUnrollPass()); // Unroll small loops
207 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
210 MPM.add(createGVNPass()); // Remove redundancies
211 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
212 MPM.add(createSCCPPass()); // Constant prop with SCCP
214 // Run instcombine after redundancy elimination to exploit opportunities
215 // opened up by them.
216 MPM.add(createInstructionCombiningPass());
217 MPM.add(createJumpThreadingPass()); // Thread jumps
218 MPM.add(createCorrelatedValuePropagationPass());
219 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
221 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
223 if (RunLoopRerolling)
224 MPM.add(createLoopRerollPass());
226 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
229 MPM.add(createBBVectorizePass());
230 MPM.add(createInstructionCombiningPass());
231 if (OptLevel > 1 && UseGVNAfterVectorization)
232 MPM.add(createGVNPass()); // Remove redundancies
234 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
236 // BBVectorize may have significantly shortened a loop body; unroll again.
237 if (!DisableUnrollLoops)
238 MPM.add(createLoopUnrollPass());
241 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
242 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
243 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
245 // As an experimental mode, run any vectorization passes in a separate
246 // pipeline from the CGSCC pass manager that runs iteratively with the
248 if (LateVectorize && LoopVectorize) {
249 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
250 // pass manager that we are specifically trying to avoid. To prevent this
251 // we must insert a no-op module pass to reset the pass manager.
252 MPM.add(createBarrierNoopPass());
254 // Add the various vectorization passes and relevant cleanup passes for
255 // them since we are no longer in the middle of the main scalar pipeline.
256 MPM.add(createLoopVectorizePass(DisableUnrollLoops));
257 MPM.add(createInstructionCombiningPass());
258 MPM.add(createCFGSimplificationPass());
261 if (!DisableUnitAtATime) {
262 // FIXME: We shouldn't bother with this anymore.
263 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
265 // GlobalOpt already deletes dead functions and globals, at -O2 try a
266 // late pass of GlobalDCE. It is capable of deleting dead cycles.
268 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
269 MPM.add(createConstantMergePass()); // Merge dup global constants
272 addExtensionsToPM(EP_OptimizerLast, MPM);
275 void PassManagerBuilder::populateLTOPassManager(PassManagerBase &PM,
278 bool DisableGVNLoadPRE) {
279 // Provide AliasAnalysis services for optimizations.
280 addInitialAliasAnalysisPasses(PM);
282 // Now that composite has been compiled, scan through the module, looking
283 // for a main function. If main is defined, mark all other functions
286 PM.add(createInternalizePass("main"));
288 // Propagate constants at call sites into the functions they call. This
289 // opens opportunities for globalopt (and inlining) by substituting function
290 // pointers passed as arguments to direct uses of functions.
291 PM.add(createIPSCCPPass());
293 // Now that we internalized some globals, see if we can hack on them!
294 PM.add(createGlobalOptimizerPass());
296 // Linking modules together can lead to duplicated global constants, only
297 // keep one copy of each constant.
298 PM.add(createConstantMergePass());
300 // Remove unused arguments from functions.
301 PM.add(createDeadArgEliminationPass());
303 // Reduce the code after globalopt and ipsccp. Both can open up significant
304 // simplification opportunities, and both can propagate functions through
305 // function pointers. When this happens, we often have to resolve varargs
306 // calls, etc, so let instcombine do this.
307 PM.add(createInstructionCombiningPass());
309 // Inline small functions
311 PM.add(createFunctionInliningPass());
313 PM.add(createPruneEHPass()); // Remove dead EH info.
315 // Optimize globals again if we ran the inliner.
317 PM.add(createGlobalOptimizerPass());
318 PM.add(createGlobalDCEPass()); // Remove dead functions.
320 // If we didn't decide to inline a function, check to see if we can
321 // transform it to pass arguments by value instead of by reference.
322 PM.add(createArgumentPromotionPass());
324 // The IPO passes may leave cruft around. Clean up after them.
325 PM.add(createInstructionCombiningPass());
326 PM.add(createJumpThreadingPass());
330 PM.add(createSROAPass());
332 PM.add(createScalarReplAggregatesPass());
334 // Run a few AA driven optimizations here and now, to cleanup the code.
335 PM.add(createFunctionAttrsPass()); // Add nocapture.
336 PM.add(createGlobalsModRefPass()); // IP alias analysis.
338 PM.add(createLICMPass()); // Hoist loop invariants.
339 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
340 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
343 PM.add(createDeadStoreEliminationPass());
345 // Cleanup and simplify the code after the scalar optimizations.
346 PM.add(createInstructionCombiningPass());
348 PM.add(createJumpThreadingPass());
350 // Delete basic blocks, which optimization passes may have killed.
351 PM.add(createCFGSimplificationPass());
353 // Now that we have optimized the program, discard unreachable functions.
354 PM.add(createGlobalDCEPass());
357 inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
358 return reinterpret_cast<PassManagerBuilder*>(P);
361 inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
362 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
365 LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
366 PassManagerBuilder *PMB = new PassManagerBuilder();
370 void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
371 PassManagerBuilder *Builder = unwrap(PMB);
376 LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
378 PassManagerBuilder *Builder = unwrap(PMB);
379 Builder->OptLevel = OptLevel;
383 LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
384 unsigned SizeLevel) {
385 PassManagerBuilder *Builder = unwrap(PMB);
386 Builder->SizeLevel = SizeLevel;
390 LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
392 PassManagerBuilder *Builder = unwrap(PMB);
393 Builder->DisableUnitAtATime = Value;
397 LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
399 PassManagerBuilder *Builder = unwrap(PMB);
400 Builder->DisableUnrollLoops = Value;
404 LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
406 // NOTE: The simplify-libcalls pass has been removed.
410 LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
411 unsigned Threshold) {
412 PassManagerBuilder *Builder = unwrap(PMB);
413 Builder->Inliner = createFunctionInliningPass(Threshold);
417 LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
418 LLVMPassManagerRef PM) {
419 PassManagerBuilder *Builder = unwrap(PMB);
420 FunctionPassManager *FPM = unwrap<FunctionPassManager>(PM);
421 Builder->populateFunctionPassManager(*FPM);
425 LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
426 LLVMPassManagerRef PM) {
427 PassManagerBuilder *Builder = unwrap(PMB);
428 PassManagerBase *MPM = unwrap(PM);
429 Builder->populateModulePassManager(*MPM);
432 void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
433 LLVMPassManagerRef PM,
434 LLVMBool Internalize,
435 LLVMBool RunInliner) {
436 PassManagerBuilder *Builder = unwrap(PMB);
437 PassManagerBase *LPM = unwrap(PM);
438 Builder->populateLTOPassManager(*LPM, Internalize != 0, RunInliner != 0);