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