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