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