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