1 //===- 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"
17 #include "llvm-c/Transforms/PassManagerBuilder.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/Analysis/Passes.h"
20 #include "llvm/IR/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"
28
29 using namespace llvm;
30
31 static cl::opt<bool>
32 RunLoopVectorization("vectorize-loops", cl::Hidden,
33 cl::desc("Run the Loop vectorization passes"));
34
35 static cl::opt<bool>
36 RunSLPVectorization("vectorize-slp", cl::Hidden,
37 cl::desc("Run the SLP vectorization passes"));
38
39 static cl::opt<bool>
40 RunBBVectorization("vectorize-slp-aggressive", cl::Hidden,
41 cl::desc("Run the BB vectorization passes"));
42
43 static cl::opt<bool>
44 UseGVNAfterVectorization("use-gvn-after-vectorization",
45 cl::init(false), cl::Hidden,
46 cl::desc("Run GVN instead of Early CSE after vectorization passes"));
47
48 static cl::opt<bool> UseNewSROA("use-new-sroa",
49 cl::init(true), cl::Hidden,
50 cl::desc("Enable the new, experimental SROA pass"));
51
52 static cl::opt<bool>
53 RunLoopRerolling("reroll-loops", cl::Hidden,
54 cl::desc("Run the loop rerolling pass"));
55
56 static cl::opt<bool> RunLoadCombine("combine-loads", cl::init(false),
57 cl::Hidden,
58 cl::desc("Run the load combining pass"));
59
PassManagerBuilder()60 PassManagerBuilder::PassManagerBuilder() {
61 OptLevel = 2;
62 SizeLevel = 0;
63 LibraryInfo = nullptr;
64 Inliner = nullptr;
65 DisableTailCalls = false;
66 DisableUnitAtATime = false;
67 DisableUnrollLoops = false;
68 BBVectorize = RunBBVectorization;
69 SLPVectorize = RunSLPVectorization;
70 LoopVectorize = RunLoopVectorization;
71 RerollLoops = RunLoopRerolling;
72 LoadCombine = RunLoadCombine;
73 }
74
~PassManagerBuilder()75 PassManagerBuilder::~PassManagerBuilder() {
76 delete LibraryInfo;
77 delete Inliner;
78 }
79
80 /// Set of global extensions, automatically added as part of the standard set.
81 static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
82 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
83
addGlobalExtension(PassManagerBuilder::ExtensionPointTy Ty,PassManagerBuilder::ExtensionFn Fn)84 void PassManagerBuilder::addGlobalExtension(
85 PassManagerBuilder::ExtensionPointTy Ty,
86 PassManagerBuilder::ExtensionFn Fn) {
87 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
88 }
89
addExtension(ExtensionPointTy Ty,ExtensionFn Fn)90 void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
91 Extensions.push_back(std::make_pair(Ty, Fn));
92 }
93
addExtensionsToPM(ExtensionPointTy ETy,PassManagerBase & PM) const94 void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
95 PassManagerBase &PM) const {
96 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
97 if ((*GlobalExtensions)[i].first == ETy)
98 (*GlobalExtensions)[i].second(*this, PM);
99 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
100 if (Extensions[i].first == ETy)
101 Extensions[i].second(*this, PM);
102 }
103
104 void
addInitialAliasAnalysisPasses(PassManagerBase & PM) const105 PassManagerBuilder::addInitialAliasAnalysisPasses(PassManagerBase &PM) const {
106 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
107 // BasicAliasAnalysis wins if they disagree. This is intended to help
108 // support "obvious" type-punning idioms.
109 PM.add(createTypeBasedAliasAnalysisPass());
110 PM.add(createBasicAliasAnalysisPass());
111 }
112
populateFunctionPassManager(FunctionPassManager & FPM)113 void PassManagerBuilder::populateFunctionPassManager(FunctionPassManager &FPM) {
114 addExtensionsToPM(EP_EarlyAsPossible, FPM);
115
116 // Add LibraryInfo if we have some.
117 if (LibraryInfo) FPM.add(new TargetLibraryInfo(*LibraryInfo));
118
119 if (OptLevel == 0) return;
120
121 addInitialAliasAnalysisPasses(FPM);
122
123 FPM.add(createCFGSimplificationPass());
124 if (UseNewSROA)
125 FPM.add(createSROAPass());
126 else
127 FPM.add(createScalarReplAggregatesPass());
128 FPM.add(createEarlyCSEPass());
129 FPM.add(createLowerExpectIntrinsicPass());
130 }
131
populateModulePassManager(PassManagerBase & MPM)132 void PassManagerBuilder::populateModulePassManager(PassManagerBase &MPM) {
133 // If all optimizations are disabled, just run the always-inline pass.
134 if (OptLevel == 0) {
135 if (Inliner) {
136 MPM.add(Inliner);
137 Inliner = nullptr;
138 }
139
140 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
141 // pass manager, but we don't want to add extensions into that pass manager.
142 // To prevent this we must insert a no-op module pass to reset the pass
143 // manager to get the same behavior as EP_OptimizerLast in non-O0 builds.
144 if (!GlobalExtensions->empty() || !Extensions.empty())
145 MPM.add(createBarrierNoopPass());
146
147 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
148 return;
149 }
150
151 // Add LibraryInfo if we have some.
152 if (LibraryInfo) MPM.add(new TargetLibraryInfo(*LibraryInfo));
153
154 addInitialAliasAnalysisPasses(MPM);
155
156 if (!DisableUnitAtATime) {
157 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
158
159 MPM.add(createIPSCCPPass()); // IP SCCP
160 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
161
162 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
163
164 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
165 addExtensionsToPM(EP_Peephole, MPM);
166 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
167 }
168
169 // Start of CallGraph SCC passes.
170 if (!DisableUnitAtATime)
171 MPM.add(createPruneEHPass()); // Remove dead EH info
172 if (Inliner) {
173 MPM.add(Inliner);
174 Inliner = nullptr;
175 }
176 if (!DisableUnitAtATime)
177 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
178 if (OptLevel > 2)
179 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
180
181 // Start of function pass.
182 // Break up aggregate allocas, using SSAUpdater.
183 if (UseNewSROA)
184 MPM.add(createSROAPass(/*RequiresDomTree*/ false));
185 else
186 MPM.add(createScalarReplAggregatesPass(-1, false));
187 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
188 MPM.add(createJumpThreadingPass()); // Thread jumps.
189 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
190 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
191 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
192 addExtensionsToPM(EP_Peephole, MPM);
193
194 if (!DisableTailCalls)
195 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
196 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
197 MPM.add(createReassociatePass()); // Reassociate expressions
198 MPM.add(createLoopRotatePass()); // Rotate Loop
199 MPM.add(createLICMPass()); // Hoist loop invariants
200 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
201 MPM.add(createInstructionCombiningPass());
202 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
203 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
204 MPM.add(createLoopDeletionPass()); // Delete dead loops
205
206 if (!DisableUnrollLoops)
207 MPM.add(createSimpleLoopUnrollPass()); // Unroll small loops
208 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
209
210 if (OptLevel > 1)
211 MPM.add(createGVNPass()); // Remove redundancies
212 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
213 MPM.add(createSCCPPass()); // Constant prop with SCCP
214
215 // Run instcombine after redundancy elimination to exploit opportunities
216 // opened up by them.
217 MPM.add(createInstructionCombiningPass());
218 addExtensionsToPM(EP_Peephole, MPM);
219 MPM.add(createJumpThreadingPass()); // Thread jumps
220 MPM.add(createCorrelatedValuePropagationPass());
221 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
222
223 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
224
225 if (RerollLoops)
226 MPM.add(createLoopRerollPass());
227 if (SLPVectorize)
228 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
229
230 if (BBVectorize) {
231 MPM.add(createBBVectorizePass());
232 MPM.add(createInstructionCombiningPass());
233 addExtensionsToPM(EP_Peephole, MPM);
234 if (OptLevel > 1 && UseGVNAfterVectorization)
235 MPM.add(createGVNPass()); // Remove redundancies
236 else
237 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
238
239 // BBVectorize may have significantly shortened a loop body; unroll again.
240 if (!DisableUnrollLoops)
241 MPM.add(createLoopUnrollPass());
242 }
243
244 if (LoadCombine)
245 MPM.add(createLoadCombinePass());
246
247 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
248 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
249 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
250 addExtensionsToPM(EP_Peephole, MPM);
251
252 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
253 // pass manager that we are specifically trying to avoid. To prevent this
254 // we must insert a no-op module pass to reset the pass manager.
255 MPM.add(createBarrierNoopPass());
256 MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
257 // FIXME: Because of #pragma vectorize enable, the passes below are always
258 // inserted in the pipeline, even when the vectorizer doesn't run (ex. when
259 // on -O1 and no #pragma is found). Would be good to have these two passes
260 // as function calls, so that we can only pass them when the vectorizer
261 // changed the code.
262 MPM.add(createInstructionCombiningPass());
263 addExtensionsToPM(EP_Peephole, MPM);
264 MPM.add(createCFGSimplificationPass());
265
266 if (!DisableUnrollLoops)
267 MPM.add(createLoopUnrollPass()); // Unroll small loops
268
269 if (!DisableUnitAtATime) {
270 // FIXME: We shouldn't bother with this anymore.
271 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
272
273 // GlobalOpt already deletes dead functions and globals, at -O2 try a
274 // late pass of GlobalDCE. It is capable of deleting dead cycles.
275 if (OptLevel > 1) {
276 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
277 MPM.add(createConstantMergePass()); // Merge dup global constants
278 }
279 }
280 addExtensionsToPM(EP_OptimizerLast, MPM);
281 }
282
populateLTOPassManager(PassManagerBase & PM,bool Internalize,bool RunInliner,bool DisableGVNLoadPRE)283 void PassManagerBuilder::populateLTOPassManager(PassManagerBase &PM,
284 bool Internalize,
285 bool RunInliner,
286 bool DisableGVNLoadPRE) {
287 // Provide AliasAnalysis services for optimizations.
288 addInitialAliasAnalysisPasses(PM);
289
290 // Now that composite has been compiled, scan through the module, looking
291 // for a main function. If main is defined, mark all other functions
292 // internal.
293 if (Internalize)
294 PM.add(createInternalizePass("main"));
295
296 // Propagate constants at call sites into the functions they call. This
297 // opens opportunities for globalopt (and inlining) by substituting function
298 // pointers passed as arguments to direct uses of functions.
299 PM.add(createIPSCCPPass());
300
301 // Now that we internalized some globals, see if we can hack on them!
302 PM.add(createGlobalOptimizerPass());
303
304 // Linking modules together can lead to duplicated global constants, only
305 // keep one copy of each constant.
306 PM.add(createConstantMergePass());
307
308 // Remove unused arguments from functions.
309 PM.add(createDeadArgEliminationPass());
310
311 // Reduce the code after globalopt and ipsccp. Both can open up significant
312 // simplification opportunities, and both can propagate functions through
313 // function pointers. When this happens, we often have to resolve varargs
314 // calls, etc, so let instcombine do this.
315 PM.add(createInstructionCombiningPass());
316 addExtensionsToPM(EP_Peephole, PM);
317
318 // Inline small functions
319 if (RunInliner)
320 PM.add(createFunctionInliningPass());
321
322 PM.add(createPruneEHPass()); // Remove dead EH info.
323
324 // Optimize globals again if we ran the inliner.
325 if (RunInliner)
326 PM.add(createGlobalOptimizerPass());
327 PM.add(createGlobalDCEPass()); // Remove dead functions.
328
329 // If we didn't decide to inline a function, check to see if we can
330 // transform it to pass arguments by value instead of by reference.
331 PM.add(createArgumentPromotionPass());
332
333 // The IPO passes may leave cruft around. Clean up after them.
334 PM.add(createInstructionCombiningPass());
335 addExtensionsToPM(EP_Peephole, PM);
336 PM.add(createJumpThreadingPass());
337
338 // Break up allocas
339 if (UseNewSROA)
340 PM.add(createSROAPass());
341 else
342 PM.add(createScalarReplAggregatesPass());
343
344 // Run a few AA driven optimizations here and now, to cleanup the code.
345 PM.add(createFunctionAttrsPass()); // Add nocapture.
346 PM.add(createGlobalsModRefPass()); // IP alias analysis.
347
348 PM.add(createLICMPass()); // Hoist loop invariants.
349 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
350 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
351
352 // Nuke dead stores.
353 PM.add(createDeadStoreEliminationPass());
354
355 // More loops are countable; try to optimize them.
356 PM.add(createIndVarSimplifyPass());
357 PM.add(createLoopDeletionPass());
358 PM.add(createLoopVectorizePass(true, true));
359
360 // More scalar chains could be vectorized due to more alias information
361 PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
362
363 if (LoadCombine)
364 PM.add(createLoadCombinePass());
365
366 // Cleanup and simplify the code after the scalar optimizations.
367 PM.add(createInstructionCombiningPass());
368 addExtensionsToPM(EP_Peephole, PM);
369
370 PM.add(createJumpThreadingPass());
371
372 // Delete basic blocks, which optimization passes may have killed.
373 PM.add(createCFGSimplificationPass());
374
375 // Now that we have optimized the program, discard unreachable functions.
376 PM.add(createGlobalDCEPass());
377 }
378
unwrap(LLVMPassManagerBuilderRef P)379 inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
380 return reinterpret_cast<PassManagerBuilder*>(P);
381 }
382
wrap(PassManagerBuilder * P)383 inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
384 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
385 }
386
LLVMPassManagerBuilderCreate()387 LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
388 PassManagerBuilder *PMB = new PassManagerBuilder();
389 return wrap(PMB);
390 }
391
LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB)392 void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
393 PassManagerBuilder *Builder = unwrap(PMB);
394 delete Builder;
395 }
396
397 void
LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,unsigned OptLevel)398 LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
399 unsigned OptLevel) {
400 PassManagerBuilder *Builder = unwrap(PMB);
401 Builder->OptLevel = OptLevel;
402 }
403
404 void
LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,unsigned SizeLevel)405 LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
406 unsigned SizeLevel) {
407 PassManagerBuilder *Builder = unwrap(PMB);
408 Builder->SizeLevel = SizeLevel;
409 }
410
411 void
LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,LLVMBool Value)412 LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
413 LLVMBool Value) {
414 PassManagerBuilder *Builder = unwrap(PMB);
415 Builder->DisableUnitAtATime = Value;
416 }
417
418 void
LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,LLVMBool Value)419 LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
420 LLVMBool Value) {
421 PassManagerBuilder *Builder = unwrap(PMB);
422 Builder->DisableUnrollLoops = Value;
423 }
424
425 void
LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,LLVMBool Value)426 LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
427 LLVMBool Value) {
428 // NOTE: The simplify-libcalls pass has been removed.
429 }
430
431 void
LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,unsigned Threshold)432 LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
433 unsigned Threshold) {
434 PassManagerBuilder *Builder = unwrap(PMB);
435 Builder->Inliner = createFunctionInliningPass(Threshold);
436 }
437
438 void
LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,LLVMPassManagerRef PM)439 LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
440 LLVMPassManagerRef PM) {
441 PassManagerBuilder *Builder = unwrap(PMB);
442 FunctionPassManager *FPM = unwrap<FunctionPassManager>(PM);
443 Builder->populateFunctionPassManager(*FPM);
444 }
445
446 void
LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,LLVMPassManagerRef PM)447 LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
448 LLVMPassManagerRef PM) {
449 PassManagerBuilder *Builder = unwrap(PMB);
450 PassManagerBase *MPM = unwrap(PM);
451 Builder->populateModulePassManager(*MPM);
452 }
453
LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,LLVMPassManagerRef PM,LLVMBool Internalize,LLVMBool RunInliner)454 void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
455 LLVMPassManagerRef PM,
456 LLVMBool Internalize,
457 LLVMBool RunInliner) {
458 PassManagerBuilder *Builder = unwrap(PMB);
459 PassManagerBase *LPM = unwrap(PM);
460 Builder->populateLTOPassManager(*LPM, Internalize != 0, RunInliner != 0);
461 }
462