blob: 8ed7704d5a080caa3c1721fcd3dac865643a1db0 [file] [log] [blame]
Rafael Espindolac684e832011-08-02 21:50:27 +00001//===- PassManagerBuilder.cpp - Build Standard Pass -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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++.
12//
13//===----------------------------------------------------------------------===//
14
15
16#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Rafael Espindola69cb2162011-08-09 22:17:34 +000017#include "llvm-c/Transforms/PassManagerBuilder.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "llvm/ADT/SmallVector.h"
Rafael Espindolac684e832011-08-02 21:50:27 +000019#include "llvm/Analysis/Passes.h"
Hal Finkelde5e5ec2012-02-01 03:51:43 +000020#include "llvm/Analysis/Verifier.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000021#include "llvm/PassManager.h"
Hal Finkelde5e5ec2012-02-01 03:51:43 +000022#include "llvm/Support/CommandLine.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/Support/ManagedStatic.h"
Rafael Espindolac684e832011-08-02 21:50:27 +000024#include "llvm/Target/TargetLibraryInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000025#include "llvm/Transforms/IPO.h"
Rafael Espindolac684e832011-08-02 21:50:27 +000026#include "llvm/Transforms/Scalar.h"
Hal Finkelde5e5ec2012-02-01 03:51:43 +000027#include "llvm/Transforms/Vectorize.h"
Rafael Espindolac684e832011-08-02 21:50:27 +000028
29using namespace llvm;
30
Hal Finkelde5e5ec2012-02-01 03:51:43 +000031static cl::opt<bool>
Andrew Trick76c25dc2013-03-06 19:04:56 +000032RunLoopVectorization("vectorize-loops",
Nadav Rotem9210cc32012-10-30 18:37:43 +000033 cl::desc("Run the Loop vectorization passes"));
Nadav Rotemd233b782012-10-29 16:36:25 +000034
Nadav Rotem9342b9c2013-06-06 22:35:47 +000035// 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.
37static cl::opt<bool>
38VectorizeO2("vectorize-o2",
39 cl::desc("Enable vectorization on all O levels"));
40
Nadav Rotemd233b782012-10-29 16:36:25 +000041static cl::opt<bool>
Nadav Rotem1129a832013-04-15 05:39:58 +000042RunSLPVectorization("vectorize-slp",
43 cl::desc("Run the SLP vectorization passes"));
44
45static cl::opt<bool>
46RunBBVectorization("vectorize-slp-aggressive",
47 cl::desc("Run the BB vectorization passes"));
Hal Finkelde5e5ec2012-02-01 03:51:43 +000048
Hal Finkel064551e2012-04-13 17:15:33 +000049static cl::opt<bool>
50UseGVNAfterVectorization("use-gvn-after-vectorization",
51 cl::init(false), cl::Hidden,
52 cl::desc("Run GVN instead of Early CSE after vectorization passes"));
53
Chandler Carruth713aa942012-09-14 09:22:59 +000054static cl::opt<bool> UseNewSROA("use-new-sroa",
Chandler Carruth1fe4fae2012-10-02 04:24:01 +000055 cl::init(true), cl::Hidden,
Chandler Carruth713aa942012-09-14 09:22:59 +000056 cl::desc("Enable the new, experimental SROA pass"));
57
Rafael Espindolac684e832011-08-02 21:50:27 +000058PassManagerBuilder::PassManagerBuilder() {
59 OptLevel = 2;
60 SizeLevel = 0;
61 LibraryInfo = 0;
62 Inliner = 0;
63 DisableSimplifyLibCalls = false;
64 DisableUnitAtATime = false;
65 DisableUnrollLoops = false;
Nadav Rotem1129a832013-04-15 05:39:58 +000066 BBVectorize = RunBBVectorization;
Nadav Rotem88498382013-04-15 04:54:42 +000067 SLPVectorize = RunSLPVectorization;
Nadav Rotemd233b782012-10-29 16:36:25 +000068 LoopVectorize = RunLoopVectorization;
Rafael Espindolac684e832011-08-02 21:50:27 +000069}
70
71PassManagerBuilder::~PassManagerBuilder() {
72 delete LibraryInfo;
73 delete Inliner;
74}
75
David Chisnall7a817ea2011-08-16 13:58:41 +000076/// Set of global extensions, automatically added as part of the standard set.
77static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
78 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
79
80void PassManagerBuilder::addGlobalExtension(
81 PassManagerBuilder::ExtensionPointTy Ty,
82 PassManagerBuilder::ExtensionFn Fn) {
83 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
84}
85
Rafael Espindolac684e832011-08-02 21:50:27 +000086void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
87 Extensions.push_back(std::make_pair(Ty, Fn));
88}
89
90void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
91 PassManagerBase &PM) const {
David Chisnall7a817ea2011-08-16 13:58:41 +000092 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
93 if ((*GlobalExtensions)[i].first == ETy)
94 (*GlobalExtensions)[i].second(*this, PM);
Rafael Espindolac684e832011-08-02 21:50:27 +000095 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
96 if (Extensions[i].first == ETy)
97 Extensions[i].second(*this, PM);
98}
99
100void
101PassManagerBuilder::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());
107}
108
109void PassManagerBuilder::populateFunctionPassManager(FunctionPassManager &FPM) {
110 addExtensionsToPM(EP_EarlyAsPossible, FPM);
111
112 // Add LibraryInfo if we have some.
113 if (LibraryInfo) FPM.add(new TargetLibraryInfo(*LibraryInfo));
114
115 if (OptLevel == 0) return;
116
117 addInitialAliasAnalysisPasses(FPM);
118
119 FPM.add(createCFGSimplificationPass());
Chandler Carruth713aa942012-09-14 09:22:59 +0000120 if (UseNewSROA)
121 FPM.add(createSROAPass());
122 else
123 FPM.add(createScalarReplAggregatesPass());
Rafael Espindolac684e832011-08-02 21:50:27 +0000124 FPM.add(createEarlyCSEPass());
125 FPM.add(createLowerExpectIntrinsicPass());
126}
127
128void PassManagerBuilder::populateModulePassManager(PassManagerBase &MPM) {
129 // If all optimizations are disabled, just run the always-inline pass.
130 if (OptLevel == 0) {
131 if (Inliner) {
132 MPM.add(Inliner);
133 Inliner = 0;
134 }
Chandler Carruth63a1eb62012-10-18 08:05:46 +0000135
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());
142
Kostya Serebryanyaf65a8c2011-11-30 22:19:26 +0000143 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
Rafael Espindolac684e832011-08-02 21:50:27 +0000144 return;
145 }
146
147 // Add LibraryInfo if we have some.
148 if (LibraryInfo) MPM.add(new TargetLibraryInfo(*LibraryInfo));
149
150 addInitialAliasAnalysisPasses(MPM);
151
152 if (!DisableUnitAtATime) {
Dan Gohman7d4c87e2012-01-17 20:51:32 +0000153 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
154
Rafael Espindolac684e832011-08-02 21:50:27 +0000155 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
156
157 MPM.add(createIPSCCPPass()); // IP SCCP
158 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
159
160 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
161 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
162 }
163
164 // Start of CallGraph SCC passes.
165 if (!DisableUnitAtATime)
166 MPM.add(createPruneEHPass()); // Remove dead EH info
167 if (Inliner) {
168 MPM.add(Inliner);
169 Inliner = 0;
170 }
171 if (!DisableUnitAtATime)
172 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
173 if (OptLevel > 2)
174 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
175
176 // Start of function pass.
177 // Break up aggregate allocas, using SSAUpdater.
Chandler Carruth1c8db502012-09-15 11:43:14 +0000178 if (UseNewSROA)
179 MPM.add(createSROAPass(/*RequiresDomTree*/ false));
180 else
181 MPM.add(createScalarReplAggregatesPass(-1, false));
Rafael Espindolac684e832011-08-02 21:50:27 +0000182 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
189
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
Nadav Rotemd15c0c72012-10-17 18:25:06 +0000200
Nadav Rotem9342b9c2013-06-06 22:35:47 +0000201 if (LoopVectorize && (OptLevel > 2 || VectorizeO2))
Nadav Rotemae3b6522012-12-12 19:29:45 +0000202 MPM.add(createLoopVectorizePass());
Nadav Rotemd15c0c72012-10-17 18:25:06 +0000203
Rafael Espindolac684e832011-08-02 21:50:27 +0000204 if (!DisableUnrollLoops)
205 MPM.add(createLoopUnrollPass()); // Unroll small loops
206 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
207
208 if (OptLevel > 1)
209 MPM.add(createGVNPass()); // Remove redundancies
210 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
211 MPM.add(createSCCPPass()); // Constant prop with SCCP
212
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
219
220 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
221
Nadav Roteme9a44112013-04-15 22:00:26 +0000222 if (SLPVectorize)
223 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Nadav Rotem1129a832013-04-15 05:39:58 +0000224
225 if (BBVectorize) {
Hal Finkelde5e5ec2012-02-01 03:51:43 +0000226 MPM.add(createBBVectorizePass());
227 MPM.add(createInstructionCombiningPass());
Hal Finkel064551e2012-04-13 17:15:33 +0000228 if (OptLevel > 1 && UseGVNAfterVectorization)
229 MPM.add(createGVNPass()); // Remove redundancies
230 else
231 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Hal Finkelc0b3d4c2013-01-29 00:22:49 +0000232
233 // BBVectorize may have significantly shortened a loop body; unroll again.
234 if (!DisableUnrollLoops)
235 MPM.add(createLoopUnrollPass());
Hal Finkelde5e5ec2012-02-01 03:51:43 +0000236 }
237
Rafael Espindolac684e832011-08-02 21:50:27 +0000238 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
239 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
240 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
241
242 if (!DisableUnitAtATime) {
243 // FIXME: We shouldn't bother with this anymore.
244 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
245
Evan Chengf5fdc142012-09-28 21:23:26 +0000246 // GlobalOpt already deletes dead functions and globals, at -O2 try a
Rafael Espindolac684e832011-08-02 21:50:27 +0000247 // late pass of GlobalDCE. It is capable of deleting dead cycles.
Evan Chengf5fdc142012-09-28 21:23:26 +0000248 if (OptLevel > 1) {
Rafael Espindolac684e832011-08-02 21:50:27 +0000249 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
Rafael Espindolac684e832011-08-02 21:50:27 +0000250 MPM.add(createConstantMergePass()); // Merge dup global constants
Evan Chengf5fdc142012-09-28 21:23:26 +0000251 }
Rafael Espindolac684e832011-08-02 21:50:27 +0000252 }
Kostya Serebryany1db39492012-03-23 23:22:59 +0000253 addExtensionsToPM(EP_OptimizerLast, MPM);
Rafael Espindolac684e832011-08-02 21:50:27 +0000254}
255
256void PassManagerBuilder::populateLTOPassManager(PassManagerBase &PM,
257 bool Internalize,
Bill Wendling3197b442012-04-02 22:16:50 +0000258 bool RunInliner,
259 bool DisableGVNLoadPRE) {
Rafael Espindolac684e832011-08-02 21:50:27 +0000260 // Provide AliasAnalysis services for optimizations.
261 addInitialAliasAnalysisPasses(PM);
262
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
265 // internal.
Rafael Espindolae5551ed2012-10-26 18:47:48 +0000266 if (Internalize) {
267 std::vector<const char*> E;
268 E.push_back("main");
269 PM.add(createInternalizePass(E));
270 }
Rafael Espindolac684e832011-08-02 21:50:27 +0000271
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());
276
277 // Now that we internalized some globals, see if we can hack on them!
278 PM.add(createGlobalOptimizerPass());
279
280 // Linking modules together can lead to duplicated global constants, only
281 // keep one copy of each constant.
282 PM.add(createConstantMergePass());
283
284 // Remove unused arguments from functions.
285 PM.add(createDeadArgEliminationPass());
286
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());
292
293 // Inline small functions
294 if (RunInliner)
295 PM.add(createFunctionInliningPass());
296
297 PM.add(createPruneEHPass()); // Remove dead EH info.
298
299 // Optimize globals again if we ran the inliner.
300 if (RunInliner)
301 PM.add(createGlobalOptimizerPass());
302 PM.add(createGlobalDCEPass()); // Remove dead functions.
303
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());
307
308 // The IPO passes may leave cruft around. Clean up after them.
309 PM.add(createInstructionCombiningPass());
310 PM.add(createJumpThreadingPass());
311 // Break up allocas
Chandler Carruth713aa942012-09-14 09:22:59 +0000312 if (UseNewSROA)
313 PM.add(createSROAPass());
314 else
315 PM.add(createScalarReplAggregatesPass());
Rafael Espindolac684e832011-08-02 21:50:27 +0000316
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.
320
Bill Wendling3197b442012-04-02 22:16:50 +0000321 PM.add(createLICMPass()); // Hoist loop invariants.
322 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
323 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
Rafael Espindolac684e832011-08-02 21:50:27 +0000324 // Nuke dead stores.
325 PM.add(createDeadStoreEliminationPass());
326
327 // Cleanup and simplify the code after the scalar optimizations.
328 PM.add(createInstructionCombiningPass());
329
330 PM.add(createJumpThreadingPass());
331
332 // Delete basic blocks, which optimization passes may have killed.
333 PM.add(createCFGSimplificationPass());
334
335 // Now that we have optimized the program, discard unreachable functions.
336 PM.add(createGlobalDCEPass());
337}
Rafael Espindola69cb2162011-08-09 22:17:34 +0000338
Eric Christopher3e397312013-04-22 22:47:22 +0000339inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
340 return reinterpret_cast<PassManagerBuilder*>(P);
341}
342
343inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
344 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
345}
346
Dmitri Gribenko79c07d22012-11-15 16:51:49 +0000347LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
Rafael Espindola69cb2162011-08-09 22:17:34 +0000348 PassManagerBuilder *PMB = new PassManagerBuilder();
349 return wrap(PMB);
350}
351
352void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
353 PassManagerBuilder *Builder = unwrap(PMB);
354 delete Builder;
355}
356
357void
358LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
359 unsigned OptLevel) {
360 PassManagerBuilder *Builder = unwrap(PMB);
361 Builder->OptLevel = OptLevel;
362}
363
364void
365LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
366 unsigned SizeLevel) {
367 PassManagerBuilder *Builder = unwrap(PMB);
368 Builder->SizeLevel = SizeLevel;
369}
370
371void
372LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
373 LLVMBool Value) {
374 PassManagerBuilder *Builder = unwrap(PMB);
375 Builder->DisableUnitAtATime = Value;
376}
377
378void
379LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
380 LLVMBool Value) {
381 PassManagerBuilder *Builder = unwrap(PMB);
382 Builder->DisableUnrollLoops = Value;
383}
384
385void
386LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
387 LLVMBool Value) {
388 PassManagerBuilder *Builder = unwrap(PMB);
389 Builder->DisableSimplifyLibCalls = Value;
390}
391
392void
393LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
394 unsigned Threshold) {
395 PassManagerBuilder *Builder = unwrap(PMB);
396 Builder->Inliner = createFunctionInliningPass(Threshold);
397}
398
399void
400LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
401 LLVMPassManagerRef PM) {
402 PassManagerBuilder *Builder = unwrap(PMB);
403 FunctionPassManager *FPM = unwrap<FunctionPassManager>(PM);
404 Builder->populateFunctionPassManager(*FPM);
405}
406
407void
408LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
409 LLVMPassManagerRef PM) {
410 PassManagerBuilder *Builder = unwrap(PMB);
411 PassManagerBase *MPM = unwrap(PM);
412 Builder->populateModulePassManager(*MPM);
413}
414
415void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
416 LLVMPassManagerRef PM,
Nick Lewycky0ebc0842013-03-10 21:58:22 +0000417 LLVMBool Internalize,
418 LLVMBool RunInliner) {
Rafael Espindola69cb2162011-08-09 22:17:34 +0000419 PassManagerBuilder *Builder = unwrap(PMB);
420 PassManagerBase *LPM = unwrap(PM);
Nick Lewycky0ebc0842013-03-10 21:58:22 +0000421 Builder->populateLTOPassManager(*LPM, Internalize != 0, RunInliner != 0);
Rafael Espindola69cb2162011-08-09 22:17:34 +0000422}