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