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", cl::desc("Run the Loop vectorization passes"));
39 RunBBVectorization("vectorize", cl::desc("Run the BB vectorization passes"));
42 UseGVNAfterVectorization("use-gvn-after-vectorization",
43 cl::init(false), cl::Hidden,
44 cl::desc("Run GVN instead of Early CSE after vectorization passes"));
46 static cl::opt<bool> UseNewSROA("use-new-sroa",
47 cl::init(true), cl::Hidden,
48 cl::desc("Enable the new, experimental SROA pass"));
50 PassManagerBuilder::PassManagerBuilder() {
55 DisableSimplifyLibCalls = false;
56 DisableUnitAtATime = false;
57 DisableUnrollLoops = false;
58 BBVectorize = RunBBVectorization;
59 LoopVectorize = RunLoopVectorization;
62 PassManagerBuilder::~PassManagerBuilder() {
67 /// Set of global extensions, automatically added as part of the standard set.
68 static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
69 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
71 void PassManagerBuilder::addGlobalExtension(
72 PassManagerBuilder::ExtensionPointTy Ty,
73 PassManagerBuilder::ExtensionFn Fn) {
74 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
77 void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
78 Extensions.push_back(std::make_pair(Ty, Fn));
81 void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
82 PassManagerBase &PM) const {
83 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
84 if ((*GlobalExtensions)[i].first == ETy)
85 (*GlobalExtensions)[i].second(*this, PM);
86 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
87 if (Extensions[i].first == ETy)
88 Extensions[i].second(*this, PM);
92 PassManagerBuilder::addInitialAliasAnalysisPasses(PassManagerBase &PM) const {
93 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
94 // BasicAliasAnalysis wins if they disagree. This is intended to help
95 // support "obvious" type-punning idioms.
96 PM.add(createTypeBasedAliasAnalysisPass());
97 PM.add(createBasicAliasAnalysisPass());
100 void PassManagerBuilder::populateFunctionPassManager(FunctionPassManager &FPM) {
101 addExtensionsToPM(EP_EarlyAsPossible, FPM);
103 // Add LibraryInfo if we have some.
104 if (LibraryInfo) FPM.add(new TargetLibraryInfo(*LibraryInfo));
106 if (OptLevel == 0) return;
108 addInitialAliasAnalysisPasses(FPM);
110 FPM.add(createCFGSimplificationPass());
112 FPM.add(createSROAPass());
114 FPM.add(createScalarReplAggregatesPass());
115 FPM.add(createEarlyCSEPass());
116 FPM.add(createLowerExpectIntrinsicPass());
119 void PassManagerBuilder::populateModulePassManager(PassManagerBase &MPM) {
120 // If all optimizations are disabled, just run the always-inline pass.
127 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
128 // pass manager, but we don't want to add extensions into that pass manager.
129 // To prevent this we must insert a no-op module pass to reset the pass
130 // manager to get the same behavior as EP_OptimizerLast in non-O0 builds.
131 if (!GlobalExtensions->empty() || !Extensions.empty())
132 MPM.add(createBarrierNoopPass());
134 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
138 // Add LibraryInfo if we have some.
139 if (LibraryInfo) MPM.add(new TargetLibraryInfo(*LibraryInfo));
141 addInitialAliasAnalysisPasses(MPM);
143 if (!DisableUnitAtATime) {
144 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
146 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
148 MPM.add(createIPSCCPPass()); // IP SCCP
149 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
151 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
152 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
155 // Start of CallGraph SCC passes.
156 if (!DisableUnitAtATime)
157 MPM.add(createPruneEHPass()); // Remove dead EH info
162 if (!DisableUnitAtATime)
163 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
165 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
167 // Start of function pass.
168 // Break up aggregate allocas, using SSAUpdater.
170 MPM.add(createSROAPass(/*RequiresDomTree*/ false));
172 MPM.add(createScalarReplAggregatesPass(-1, false));
173 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
174 if (!DisableSimplifyLibCalls)
175 MPM.add(createSimplifyLibCallsPass()); // Library Call Optimizations
176 MPM.add(createJumpThreadingPass()); // Thread jumps.
177 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
178 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
179 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
181 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
182 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
183 MPM.add(createReassociatePass()); // Reassociate expressions
184 MPM.add(createLoopRotatePass()); // Rotate Loop
185 MPM.add(createLICMPass()); // Hoist loop invariants
186 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
187 MPM.add(createInstructionCombiningPass());
188 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
189 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
190 MPM.add(createLoopDeletionPass()); // Delete dead loops
193 MPM.add(createLoopVectorizePass());
194 MPM.add(createLICMPass());
197 if (!DisableUnrollLoops)
198 MPM.add(createLoopUnrollPass()); // Unroll small loops
199 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
202 MPM.add(createGVNPass()); // Remove redundancies
203 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
204 MPM.add(createSCCPPass()); // Constant prop with SCCP
206 // Run instcombine after redundancy elimination to exploit opportunities
207 // opened up by them.
208 MPM.add(createInstructionCombiningPass());
209 MPM.add(createJumpThreadingPass()); // Thread jumps
210 MPM.add(createCorrelatedValuePropagationPass());
211 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
213 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
216 MPM.add(createBBVectorizePass());
217 MPM.add(createInstructionCombiningPass());
218 if (OptLevel > 1 && UseGVNAfterVectorization)
219 MPM.add(createGVNPass()); // Remove redundancies
221 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
224 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
225 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
226 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
228 if (!DisableUnitAtATime) {
229 // FIXME: We shouldn't bother with this anymore.
230 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
232 // GlobalOpt already deletes dead functions and globals, at -O2 try a
233 // late pass of GlobalDCE. It is capable of deleting dead cycles.
235 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
236 MPM.add(createConstantMergePass()); // Merge dup global constants
239 addExtensionsToPM(EP_OptimizerLast, MPM);
242 void PassManagerBuilder::populateLTOPassManager(PassManagerBase &PM,
245 bool DisableGVNLoadPRE) {
246 // Provide AliasAnalysis services for optimizations.
247 addInitialAliasAnalysisPasses(PM);
249 // Now that composite has been compiled, scan through the module, looking
250 // for a main function. If main is defined, mark all other functions
253 std::vector<const char*> E;
255 PM.add(createInternalizePass(E));
258 // Propagate constants at call sites into the functions they call. This
259 // opens opportunities for globalopt (and inlining) by substituting function
260 // pointers passed as arguments to direct uses of functions.
261 PM.add(createIPSCCPPass());
263 // Now that we internalized some globals, see if we can hack on them!
264 PM.add(createGlobalOptimizerPass());
266 // Linking modules together can lead to duplicated global constants, only
267 // keep one copy of each constant.
268 PM.add(createConstantMergePass());
270 // Remove unused arguments from functions.
271 PM.add(createDeadArgEliminationPass());
273 // Reduce the code after globalopt and ipsccp. Both can open up significant
274 // simplification opportunities, and both can propagate functions through
275 // function pointers. When this happens, we often have to resolve varargs
276 // calls, etc, so let instcombine do this.
277 PM.add(createInstructionCombiningPass());
279 // Inline small functions
281 PM.add(createFunctionInliningPass());
283 PM.add(createPruneEHPass()); // Remove dead EH info.
285 // Optimize globals again if we ran the inliner.
287 PM.add(createGlobalOptimizerPass());
288 PM.add(createGlobalDCEPass()); // Remove dead functions.
290 // If we didn't decide to inline a function, check to see if we can
291 // transform it to pass arguments by value instead of by reference.
292 PM.add(createArgumentPromotionPass());
294 // The IPO passes may leave cruft around. Clean up after them.
295 PM.add(createInstructionCombiningPass());
296 PM.add(createJumpThreadingPass());
299 PM.add(createSROAPass());
301 PM.add(createScalarReplAggregatesPass());
303 // Run a few AA driven optimizations here and now, to cleanup the code.
304 PM.add(createFunctionAttrsPass()); // Add nocapture.
305 PM.add(createGlobalsModRefPass()); // IP alias analysis.
307 PM.add(createLICMPass()); // Hoist loop invariants.
308 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
309 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
311 PM.add(createDeadStoreEliminationPass());
313 // Cleanup and simplify the code after the scalar optimizations.
314 PM.add(createInstructionCombiningPass());
316 PM.add(createJumpThreadingPass());
318 // Delete basic blocks, which optimization passes may have killed.
319 PM.add(createCFGSimplificationPass());
321 // Now that we have optimized the program, discard unreachable functions.
322 PM.add(createGlobalDCEPass());
325 LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate(void) {
326 PassManagerBuilder *PMB = new PassManagerBuilder();
330 void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
331 PassManagerBuilder *Builder = unwrap(PMB);
336 LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
338 PassManagerBuilder *Builder = unwrap(PMB);
339 Builder->OptLevel = OptLevel;
343 LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
344 unsigned SizeLevel) {
345 PassManagerBuilder *Builder = unwrap(PMB);
346 Builder->SizeLevel = SizeLevel;
350 LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
352 PassManagerBuilder *Builder = unwrap(PMB);
353 Builder->DisableUnitAtATime = Value;
357 LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
359 PassManagerBuilder *Builder = unwrap(PMB);
360 Builder->DisableUnrollLoops = Value;
364 LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
366 PassManagerBuilder *Builder = unwrap(PMB);
367 Builder->DisableSimplifyLibCalls = Value;
371 LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
372 unsigned Threshold) {
373 PassManagerBuilder *Builder = unwrap(PMB);
374 Builder->Inliner = createFunctionInliningPass(Threshold);
378 LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
379 LLVMPassManagerRef PM) {
380 PassManagerBuilder *Builder = unwrap(PMB);
381 FunctionPassManager *FPM = unwrap<FunctionPassManager>(PM);
382 Builder->populateFunctionPassManager(*FPM);
386 LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
387 LLVMPassManagerRef PM) {
388 PassManagerBuilder *Builder = unwrap(PMB);
389 PassManagerBase *MPM = unwrap(PM);
390 Builder->populateModulePassManager(*MPM);
393 void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
394 LLVMPassManagerRef PM,
397 PassManagerBuilder *Builder = unwrap(PMB);
398 PassManagerBase *LPM = unwrap(PM);
399 Builder->populateLTOPassManager(*LPM, Internalize, RunInliner);