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"
18 #include "llvm-c/Transforms/PassManagerBuilder.h"
20 #include "llvm/PassManager.h"
21 #include "llvm/DefaultPasses.h"
22 #include "llvm/PassManager.h"
23 #include "llvm/Analysis/Passes.h"
24 #include "llvm/Analysis/Verifier.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Target/TargetLibraryInfo.h"
27 #include "llvm/Transforms/Scalar.h"
28 #include "llvm/Transforms/Vectorize.h"
29 #include "llvm/Transforms/IPO.h"
30 #include "llvm/ADT/SmallVector.h"
31 #include "llvm/Support/ManagedStatic.h"
36 RunLoopVectorization("vectorize-loops",
37 cl::desc("Run the Loop vectorization passes"));
40 RunBBVectorization("vectorize", cl::desc("Run the BB vectorization passes"));
43 UseGVNAfterVectorization("use-gvn-after-vectorization",
44 cl::init(false), cl::Hidden,
45 cl::desc("Run GVN instead of Early CSE after vectorization passes"));
47 static cl::opt<bool> UseNewSROA("use-new-sroa",
48 cl::init(true), cl::Hidden,
49 cl::desc("Enable the new, experimental SROA pass"));
51 PassManagerBuilder::PassManagerBuilder() {
56 DisableSimplifyLibCalls = false;
57 DisableUnitAtATime = false;
58 DisableUnrollLoops = false;
59 Vectorize = RunBBVectorization;
60 LoopVectorize = RunLoopVectorization;
63 PassManagerBuilder::~PassManagerBuilder() {
68 /// Set of global extensions, automatically added as part of the standard set.
69 static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
70 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
72 void PassManagerBuilder::addGlobalExtension(
73 PassManagerBuilder::ExtensionPointTy Ty,
74 PassManagerBuilder::ExtensionFn Fn) {
75 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
78 void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
79 Extensions.push_back(std::make_pair(Ty, Fn));
82 void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
83 PassManagerBase &PM) const {
84 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
85 if ((*GlobalExtensions)[i].first == ETy)
86 (*GlobalExtensions)[i].second(*this, PM);
87 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
88 if (Extensions[i].first == ETy)
89 Extensions[i].second(*this, PM);
93 PassManagerBuilder::addInitialAliasAnalysisPasses(PassManagerBase &PM) const {
94 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
95 // BasicAliasAnalysis wins if they disagree. This is intended to help
96 // support "obvious" type-punning idioms.
97 PM.add(createTypeBasedAliasAnalysisPass());
98 PM.add(createBasicAliasAnalysisPass());
101 void PassManagerBuilder::populateFunctionPassManager(FunctionPassManager &FPM) {
102 addExtensionsToPM(EP_EarlyAsPossible, FPM);
104 // Add LibraryInfo if we have some.
105 if (LibraryInfo) FPM.add(new TargetLibraryInfo(*LibraryInfo));
107 if (OptLevel == 0) return;
109 addInitialAliasAnalysisPasses(FPM);
111 FPM.add(createCFGSimplificationPass());
113 FPM.add(createSROAPass());
115 FPM.add(createScalarReplAggregatesPass());
116 FPM.add(createEarlyCSEPass());
117 FPM.add(createLowerExpectIntrinsicPass());
120 void PassManagerBuilder::populateModulePassManager(PassManagerBase &MPM) {
121 // If all optimizations are disabled, just run the always-inline pass.
128 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
129 // pass manager, but we don't want to add extensions into that pass manager.
130 // To prevent this we must insert a no-op module pass to reset the pass
131 // manager to get the same behavior as EP_OptimizerLast in non-O0 builds.
132 if (!GlobalExtensions->empty() || !Extensions.empty())
133 MPM.add(createBarrierNoopPass());
135 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
139 // Add LibraryInfo if we have some.
140 if (LibraryInfo) MPM.add(new TargetLibraryInfo(*LibraryInfo));
142 addInitialAliasAnalysisPasses(MPM);
144 if (!DisableUnitAtATime) {
145 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
147 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
149 MPM.add(createIPSCCPPass()); // IP SCCP
150 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
152 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
153 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
156 // Start of CallGraph SCC passes.
157 if (!DisableUnitAtATime)
158 MPM.add(createPruneEHPass()); // Remove dead EH info
163 if (!DisableUnitAtATime)
164 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
166 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
168 // Start of function pass.
169 // Break up aggregate allocas, using SSAUpdater.
171 MPM.add(createSROAPass(/*RequiresDomTree*/ false));
173 MPM.add(createScalarReplAggregatesPass(-1, false));
174 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
175 if (!DisableSimplifyLibCalls)
176 MPM.add(createSimplifyLibCallsPass()); // Library Call Optimizations
177 MPM.add(createJumpThreadingPass()); // Thread jumps.
178 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
179 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
180 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
182 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
183 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
184 MPM.add(createReassociatePass()); // Reassociate expressions
185 MPM.add(createLoopRotatePass()); // Rotate Loop
186 MPM.add(createLICMPass()); // Hoist loop invariants
187 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
188 MPM.add(createInstructionCombiningPass());
189 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
190 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
191 MPM.add(createLoopDeletionPass()); // Delete dead loops
194 MPM.add(createLoopVectorizePass());
195 MPM.add(createLICMPass());
198 if (!DisableUnrollLoops)
199 MPM.add(createLoopUnrollPass()); // Unroll small loops
200 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
203 MPM.add(createGVNPass()); // Remove redundancies
204 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
205 MPM.add(createSCCPPass()); // Constant prop with SCCP
207 // Run instcombine after redundancy elimination to exploit opportunities
208 // opened up by them.
209 MPM.add(createInstructionCombiningPass());
210 MPM.add(createJumpThreadingPass()); // Thread jumps
211 MPM.add(createCorrelatedValuePropagationPass());
212 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
214 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
217 MPM.add(createBBVectorizePass());
218 MPM.add(createInstructionCombiningPass());
219 if (OptLevel > 1 && UseGVNAfterVectorization)
220 MPM.add(createGVNPass()); // Remove redundancies
222 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
225 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
226 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
227 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
229 if (!DisableUnitAtATime) {
230 // FIXME: We shouldn't bother with this anymore.
231 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
233 // GlobalOpt already deletes dead functions and globals, at -O2 try a
234 // late pass of GlobalDCE. It is capable of deleting dead cycles.
236 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
237 MPM.add(createConstantMergePass()); // Merge dup global constants
240 addExtensionsToPM(EP_OptimizerLast, MPM);
243 void PassManagerBuilder::populateLTOPassManager(PassManagerBase &PM,
246 bool DisableGVNLoadPRE) {
247 // Provide AliasAnalysis services for optimizations.
248 addInitialAliasAnalysisPasses(PM);
250 // Now that composite has been compiled, scan through the module, looking
251 // for a main function. If main is defined, mark all other functions
254 std::vector<const char*> E;
256 PM.add(createInternalizePass(E));
259 // Propagate constants at call sites into the functions they call. This
260 // opens opportunities for globalopt (and inlining) by substituting function
261 // pointers passed as arguments to direct uses of functions.
262 PM.add(createIPSCCPPass());
264 // Now that we internalized some globals, see if we can hack on them!
265 PM.add(createGlobalOptimizerPass());
267 // Linking modules together can lead to duplicated global constants, only
268 // keep one copy of each constant.
269 PM.add(createConstantMergePass());
271 // Remove unused arguments from functions.
272 PM.add(createDeadArgEliminationPass());
274 // Reduce the code after globalopt and ipsccp. Both can open up significant
275 // simplification opportunities, and both can propagate functions through
276 // function pointers. When this happens, we often have to resolve varargs
277 // calls, etc, so let instcombine do this.
278 PM.add(createInstructionCombiningPass());
280 // Inline small functions
282 PM.add(createFunctionInliningPass());
284 PM.add(createPruneEHPass()); // Remove dead EH info.
286 // Optimize globals again if we ran the inliner.
288 PM.add(createGlobalOptimizerPass());
289 PM.add(createGlobalDCEPass()); // Remove dead functions.
291 // If we didn't decide to inline a function, check to see if we can
292 // transform it to pass arguments by value instead of by reference.
293 PM.add(createArgumentPromotionPass());
295 // The IPO passes may leave cruft around. Clean up after them.
296 PM.add(createInstructionCombiningPass());
297 PM.add(createJumpThreadingPass());
300 PM.add(createSROAPass());
302 PM.add(createScalarReplAggregatesPass());
304 // Run a few AA driven optimizations here and now, to cleanup the code.
305 PM.add(createFunctionAttrsPass()); // Add nocapture.
306 PM.add(createGlobalsModRefPass()); // IP alias analysis.
308 PM.add(createLICMPass()); // Hoist loop invariants.
309 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
310 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
312 PM.add(createDeadStoreEliminationPass());
314 // Cleanup and simplify the code after the scalar optimizations.
315 PM.add(createInstructionCombiningPass());
317 PM.add(createJumpThreadingPass());
319 // Delete basic blocks, which optimization passes may have killed.
320 PM.add(createCFGSimplificationPass());
322 // Now that we have optimized the program, discard unreachable functions.
323 PM.add(createGlobalDCEPass());
326 LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
327 PassManagerBuilder *PMB = new PassManagerBuilder();
331 void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
332 PassManagerBuilder *Builder = unwrap(PMB);
337 LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
339 PassManagerBuilder *Builder = unwrap(PMB);
340 Builder->OptLevel = OptLevel;
344 LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
345 unsigned SizeLevel) {
346 PassManagerBuilder *Builder = unwrap(PMB);
347 Builder->SizeLevel = SizeLevel;
351 LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
353 PassManagerBuilder *Builder = unwrap(PMB);
354 Builder->DisableUnitAtATime = Value;
358 LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
360 PassManagerBuilder *Builder = unwrap(PMB);
361 Builder->DisableUnrollLoops = Value;
365 LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
367 PassManagerBuilder *Builder = unwrap(PMB);
368 Builder->DisableSimplifyLibCalls = Value;
372 LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
373 unsigned Threshold) {
374 PassManagerBuilder *Builder = unwrap(PMB);
375 Builder->Inliner = createFunctionInliningPass(Threshold);
379 LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
380 LLVMPassManagerRef PM) {
381 PassManagerBuilder *Builder = unwrap(PMB);
382 FunctionPassManager *FPM = unwrap<FunctionPassManager>(PM);
383 Builder->populateFunctionPassManager(*FPM);
387 LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
388 LLVMPassManagerRef PM) {
389 PassManagerBuilder *Builder = unwrap(PMB);
390 PassManagerBase *MPM = unwrap(PM);
391 Builder->populateModulePassManager(*MPM);
394 void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
395 LLVMPassManagerRef PM,
398 PassManagerBuilder *Builder = unwrap(PMB);
399 PassManagerBase *LPM = unwrap(PM);
400 Builder->populateLTOPassManager(*LPM, Internalize, RunInliner);