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