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"));
35 // This is a helper flag that we use for testing the profitability of
36 // vectorization on -O2 and -Os. It should go away once we make a decision.
38 VectorizeO2("vectorize-o2",
39 cl::desc("Enable vectorization on all O levels"));
42 RunSLPVectorization("vectorize-slp",
43 cl::desc("Run the SLP vectorization passes"));
46 RunBBVectorization("vectorize-slp-aggressive",
47 cl::desc("Run the BB vectorization passes"));
50 UseGVNAfterVectorization("use-gvn-after-vectorization",
51 cl::init(false), cl::Hidden,
52 cl::desc("Run GVN instead of Early CSE after vectorization passes"));
54 static cl::opt<bool> UseNewSROA("use-new-sroa",
55 cl::init(true), cl::Hidden,
56 cl::desc("Enable the new, experimental SROA pass"));
58 PassManagerBuilder::PassManagerBuilder() {
63 DisableSimplifyLibCalls = false;
64 DisableUnitAtATime = false;
65 DisableUnrollLoops = false;
66 BBVectorize = RunBBVectorization;
67 SLPVectorize = RunSLPVectorization;
68 LoopVectorize = RunLoopVectorization;
71 PassManagerBuilder::~PassManagerBuilder() {
76 /// Set of global extensions, automatically added as part of the standard set.
77 static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
78 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
80 void PassManagerBuilder::addGlobalExtension(
81 PassManagerBuilder::ExtensionPointTy Ty,
82 PassManagerBuilder::ExtensionFn Fn) {
83 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
86 void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
87 Extensions.push_back(std::make_pair(Ty, Fn));
90 void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
91 PassManagerBase &PM) const {
92 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
93 if ((*GlobalExtensions)[i].first == ETy)
94 (*GlobalExtensions)[i].second(*this, PM);
95 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
96 if (Extensions[i].first == ETy)
97 Extensions[i].second(*this, PM);
101 PassManagerBuilder::addInitialAliasAnalysisPasses(PassManagerBase &PM) const {
102 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
103 // BasicAliasAnalysis wins if they disagree. This is intended to help
104 // support "obvious" type-punning idioms.
105 PM.add(createTypeBasedAliasAnalysisPass());
106 PM.add(createBasicAliasAnalysisPass());
109 void PassManagerBuilder::populateFunctionPassManager(FunctionPassManager &FPM) {
110 addExtensionsToPM(EP_EarlyAsPossible, FPM);
112 // Add LibraryInfo if we have some.
113 if (LibraryInfo) FPM.add(new TargetLibraryInfo(*LibraryInfo));
115 if (OptLevel == 0) return;
117 addInitialAliasAnalysisPasses(FPM);
119 FPM.add(createCFGSimplificationPass());
121 FPM.add(createSROAPass());
123 FPM.add(createScalarReplAggregatesPass());
124 FPM.add(createEarlyCSEPass());
125 FPM.add(createLowerExpectIntrinsicPass());
128 void PassManagerBuilder::populateModulePassManager(PassManagerBase &MPM) {
129 // If all optimizations are disabled, just run the always-inline pass.
136 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
137 // pass manager, but we don't want to add extensions into that pass manager.
138 // To prevent this we must insert a no-op module pass to reset the pass
139 // manager to get the same behavior as EP_OptimizerLast in non-O0 builds.
140 if (!GlobalExtensions->empty() || !Extensions.empty())
141 MPM.add(createBarrierNoopPass());
143 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
147 // Add LibraryInfo if we have some.
148 if (LibraryInfo) MPM.add(new TargetLibraryInfo(*LibraryInfo));
150 addInitialAliasAnalysisPasses(MPM);
152 if (!DisableUnitAtATime) {
153 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
155 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
157 MPM.add(createIPSCCPPass()); // IP SCCP
158 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
160 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
161 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
164 // Start of CallGraph SCC passes.
165 if (!DisableUnitAtATime)
166 MPM.add(createPruneEHPass()); // Remove dead EH info
171 if (!DisableUnitAtATime)
172 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
174 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
176 // Start of function pass.
177 // Break up aggregate allocas, using SSAUpdater.
179 MPM.add(createSROAPass(/*RequiresDomTree*/ false));
181 MPM.add(createScalarReplAggregatesPass(-1, false));
182 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
183 if (!DisableSimplifyLibCalls)
184 MPM.add(createSimplifyLibCallsPass()); // Library Call Optimizations
185 MPM.add(createJumpThreadingPass()); // Thread jumps.
186 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
187 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
188 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
190 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
191 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
192 MPM.add(createReassociatePass()); // Reassociate expressions
193 MPM.add(createLoopRotatePass()); // Rotate Loop
194 MPM.add(createLICMPass()); // Hoist loop invariants
195 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
196 MPM.add(createInstructionCombiningPass());
197 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
198 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
199 MPM.add(createLoopDeletionPass()); // Delete dead loops
201 if (LoopVectorize && (OptLevel > 2 || VectorizeO2))
202 MPM.add(createLoopVectorizePass());
204 if (!DisableUnrollLoops)
205 MPM.add(createLoopUnrollPass()); // Unroll small loops
206 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
209 MPM.add(createGVNPass()); // Remove redundancies
210 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
211 MPM.add(createSCCPPass()); // Constant prop with SCCP
213 // Run instcombine after redundancy elimination to exploit opportunities
214 // opened up by them.
215 MPM.add(createInstructionCombiningPass());
216 MPM.add(createJumpThreadingPass()); // Thread jumps
217 MPM.add(createCorrelatedValuePropagationPass());
218 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
220 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
223 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
226 MPM.add(createBBVectorizePass());
227 MPM.add(createInstructionCombiningPass());
228 if (OptLevel > 1 && UseGVNAfterVectorization)
229 MPM.add(createGVNPass()); // Remove redundancies
231 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
233 // BBVectorize may have significantly shortened a loop body; unroll again.
234 if (!DisableUnrollLoops)
235 MPM.add(createLoopUnrollPass());
238 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
239 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
240 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
242 if (!DisableUnitAtATime) {
243 // FIXME: We shouldn't bother with this anymore.
244 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
246 // GlobalOpt already deletes dead functions and globals, at -O2 try a
247 // late pass of GlobalDCE. It is capable of deleting dead cycles.
249 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
250 MPM.add(createConstantMergePass()); // Merge dup global constants
253 addExtensionsToPM(EP_OptimizerLast, MPM);
256 void PassManagerBuilder::populateLTOPassManager(PassManagerBase &PM,
259 bool DisableGVNLoadPRE) {
260 // Provide AliasAnalysis services for optimizations.
261 addInitialAliasAnalysisPasses(PM);
263 // Now that composite has been compiled, scan through the module, looking
264 // for a main function. If main is defined, mark all other functions
267 std::vector<const char*> E;
269 PM.add(createInternalizePass(E));
272 // Propagate constants at call sites into the functions they call. This
273 // opens opportunities for globalopt (and inlining) by substituting function
274 // pointers passed as arguments to direct uses of functions.
275 PM.add(createIPSCCPPass());
277 // Now that we internalized some globals, see if we can hack on them!
278 PM.add(createGlobalOptimizerPass());
280 // Linking modules together can lead to duplicated global constants, only
281 // keep one copy of each constant.
282 PM.add(createConstantMergePass());
284 // Remove unused arguments from functions.
285 PM.add(createDeadArgEliminationPass());
287 // Reduce the code after globalopt and ipsccp. Both can open up significant
288 // simplification opportunities, and both can propagate functions through
289 // function pointers. When this happens, we often have to resolve varargs
290 // calls, etc, so let instcombine do this.
291 PM.add(createInstructionCombiningPass());
293 // Inline small functions
295 PM.add(createFunctionInliningPass());
297 PM.add(createPruneEHPass()); // Remove dead EH info.
299 // Optimize globals again if we ran the inliner.
301 PM.add(createGlobalOptimizerPass());
302 PM.add(createGlobalDCEPass()); // Remove dead functions.
304 // If we didn't decide to inline a function, check to see if we can
305 // transform it to pass arguments by value instead of by reference.
306 PM.add(createArgumentPromotionPass());
308 // The IPO passes may leave cruft around. Clean up after them.
309 PM.add(createInstructionCombiningPass());
310 PM.add(createJumpThreadingPass());
313 PM.add(createSROAPass());
315 PM.add(createScalarReplAggregatesPass());
317 // Run a few AA driven optimizations here and now, to cleanup the code.
318 PM.add(createFunctionAttrsPass()); // Add nocapture.
319 PM.add(createGlobalsModRefPass()); // IP alias analysis.
321 PM.add(createLICMPass()); // Hoist loop invariants.
322 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
323 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
325 PM.add(createDeadStoreEliminationPass());
327 // Cleanup and simplify the code after the scalar optimizations.
328 PM.add(createInstructionCombiningPass());
330 PM.add(createJumpThreadingPass());
332 // Delete basic blocks, which optimization passes may have killed.
333 PM.add(createCFGSimplificationPass());
335 // Now that we have optimized the program, discard unreachable functions.
336 PM.add(createGlobalDCEPass());
339 inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
340 return reinterpret_cast<PassManagerBuilder*>(P);
343 inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
344 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
347 LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
348 PassManagerBuilder *PMB = new PassManagerBuilder();
352 void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
353 PassManagerBuilder *Builder = unwrap(PMB);
358 LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
360 PassManagerBuilder *Builder = unwrap(PMB);
361 Builder->OptLevel = OptLevel;
365 LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
366 unsigned SizeLevel) {
367 PassManagerBuilder *Builder = unwrap(PMB);
368 Builder->SizeLevel = SizeLevel;
372 LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
374 PassManagerBuilder *Builder = unwrap(PMB);
375 Builder->DisableUnitAtATime = Value;
379 LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
381 PassManagerBuilder *Builder = unwrap(PMB);
382 Builder->DisableUnrollLoops = Value;
386 LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
388 PassManagerBuilder *Builder = unwrap(PMB);
389 Builder->DisableSimplifyLibCalls = Value;
393 LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
394 unsigned Threshold) {
395 PassManagerBuilder *Builder = unwrap(PMB);
396 Builder->Inliner = createFunctionInliningPass(Threshold);
400 LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
401 LLVMPassManagerRef PM) {
402 PassManagerBuilder *Builder = unwrap(PMB);
403 FunctionPassManager *FPM = unwrap<FunctionPassManager>(PM);
404 Builder->populateFunctionPassManager(*FPM);
408 LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
409 LLVMPassManagerRef PM) {
410 PassManagerBuilder *Builder = unwrap(PMB);
411 PassManagerBase *MPM = unwrap(PM);
412 Builder->populateModulePassManager(*MPM);
415 void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
416 LLVMPassManagerRef PM,
417 LLVMBool Internalize,
418 LLVMBool RunInliner) {
419 PassManagerBuilder *Builder = unwrap(PMB);
420 PassManagerBase *LPM = unwrap(PM);
421 Builder->populateLTOPassManager(*LPM, Internalize != 0, RunInliner != 0);