Disable the new SROA pass to get the tree back in working order. We don't yet
[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(false), 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   if (UseNewSROA)
158     MPM.add(createSROAPass(/*RequiresDomTree*/ false));
159   else
160     MPM.add(createScalarReplAggregatesPass(-1, false));
161   MPM.add(createEarlyCSEPass());              // Catch trivial redundancies
162   if (!DisableSimplifyLibCalls)
163     MPM.add(createSimplifyLibCallsPass());    // Library Call Optimizations
164   MPM.add(createJumpThreadingPass());         // Thread jumps.
165   MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
166   MPM.add(createCFGSimplificationPass());     // Merge & remove BBs
167   MPM.add(createInstructionCombiningPass());  // Combine silly seq's
168
169   MPM.add(createTailCallEliminationPass());   // Eliminate tail calls
170   MPM.add(createCFGSimplificationPass());     // Merge & remove BBs
171   MPM.add(createReassociatePass());           // Reassociate expressions
172   MPM.add(createLoopRotatePass());            // Rotate Loop
173   MPM.add(createLICMPass());                  // Hoist loop invariants
174   MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
175   MPM.add(createInstructionCombiningPass());
176   MPM.add(createIndVarSimplifyPass());        // Canonicalize indvars
177   MPM.add(createLoopIdiomPass());             // Recognize idioms like memset.
178   MPM.add(createLoopDeletionPass());          // Delete dead loops
179   if (!DisableUnrollLoops)
180     MPM.add(createLoopUnrollPass());          // Unroll small loops
181   addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
182
183   if (OptLevel > 1)
184     MPM.add(createGVNPass());                 // Remove redundancies
185   MPM.add(createMemCpyOptPass());             // Remove memcpy / form memset
186   MPM.add(createSCCPPass());                  // Constant prop with SCCP
187
188   // Run instcombine after redundancy elimination to exploit opportunities
189   // opened up by them.
190   MPM.add(createInstructionCombiningPass());
191   MPM.add(createJumpThreadingPass());         // Thread jumps
192   MPM.add(createCorrelatedValuePropagationPass());
193   MPM.add(createDeadStoreEliminationPass());  // Delete dead stores
194
195   addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
196
197   if (Vectorize) {
198     MPM.add(createBBVectorizePass());
199     MPM.add(createInstructionCombiningPass());
200     if (OptLevel > 1 && UseGVNAfterVectorization)
201       MPM.add(createGVNPass());                   // Remove redundancies
202     else
203       MPM.add(createEarlyCSEPass());              // Catch trivial redundancies
204   }
205
206   MPM.add(createAggressiveDCEPass());         // Delete dead instructions
207   MPM.add(createCFGSimplificationPass());     // Merge & remove BBs
208   MPM.add(createInstructionCombiningPass());  // Clean up after everything.
209
210   if (!DisableUnitAtATime) {
211     // FIXME: We shouldn't bother with this anymore.
212     MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
213
214     // GlobalOpt already deletes dead functions and globals, at -O3 try a
215     // late pass of GlobalDCE.  It is capable of deleting dead cycles.
216     if (OptLevel > 2)
217       MPM.add(createGlobalDCEPass());         // Remove dead fns and globals.
218
219     if (OptLevel > 1)
220       MPM.add(createConstantMergePass());     // Merge dup global constants
221   }
222   addExtensionsToPM(EP_OptimizerLast, MPM);
223 }
224
225 void PassManagerBuilder::populateLTOPassManager(PassManagerBase &PM,
226                                                 bool Internalize,
227                                                 bool RunInliner,
228                                                 bool DisableGVNLoadPRE) {
229   // Provide AliasAnalysis services for optimizations.
230   addInitialAliasAnalysisPasses(PM);
231
232   // Now that composite has been compiled, scan through the module, looking
233   // for a main function.  If main is defined, mark all other functions
234   // internal.
235   if (Internalize)
236     PM.add(createInternalizePass(true));
237
238   // Propagate constants at call sites into the functions they call.  This
239   // opens opportunities for globalopt (and inlining) by substituting function
240   // pointers passed as arguments to direct uses of functions.
241   PM.add(createIPSCCPPass());
242
243   // Now that we internalized some globals, see if we can hack on them!
244   PM.add(createGlobalOptimizerPass());
245
246   // Linking modules together can lead to duplicated global constants, only
247   // keep one copy of each constant.
248   PM.add(createConstantMergePass());
249
250   // Remove unused arguments from functions.
251   PM.add(createDeadArgEliminationPass());
252
253   // Reduce the code after globalopt and ipsccp.  Both can open up significant
254   // simplification opportunities, and both can propagate functions through
255   // function pointers.  When this happens, we often have to resolve varargs
256   // calls, etc, so let instcombine do this.
257   PM.add(createInstructionCombiningPass());
258
259   // Inline small functions
260   if (RunInliner)
261     PM.add(createFunctionInliningPass());
262
263   PM.add(createPruneEHPass());   // Remove dead EH info.
264
265   // Optimize globals again if we ran the inliner.
266   if (RunInliner)
267     PM.add(createGlobalOptimizerPass());
268   PM.add(createGlobalDCEPass()); // Remove dead functions.
269
270   // If we didn't decide to inline a function, check to see if we can
271   // transform it to pass arguments by value instead of by reference.
272   PM.add(createArgumentPromotionPass());
273
274   // The IPO passes may leave cruft around.  Clean up after them.
275   PM.add(createInstructionCombiningPass());
276   PM.add(createJumpThreadingPass());
277   // Break up allocas
278   if (UseNewSROA)
279     PM.add(createSROAPass());
280   else
281     PM.add(createScalarReplAggregatesPass());
282
283   // Run a few AA driven optimizations here and now, to cleanup the code.
284   PM.add(createFunctionAttrsPass()); // Add nocapture.
285   PM.add(createGlobalsModRefPass()); // IP alias analysis.
286
287   PM.add(createLICMPass());                 // Hoist loop invariants.
288   PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
289   PM.add(createMemCpyOptPass());            // Remove dead memcpys.
290   // Nuke dead stores.
291   PM.add(createDeadStoreEliminationPass());
292
293   // Cleanup and simplify the code after the scalar optimizations.
294   PM.add(createInstructionCombiningPass());
295
296   PM.add(createJumpThreadingPass());
297
298   // Delete basic blocks, which optimization passes may have killed.
299   PM.add(createCFGSimplificationPass());
300
301   // Now that we have optimized the program, discard unreachable functions.
302   PM.add(createGlobalDCEPass());
303 }
304
305 LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate(void) {
306   PassManagerBuilder *PMB = new PassManagerBuilder();
307   return wrap(PMB);
308 }
309
310 void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
311   PassManagerBuilder *Builder = unwrap(PMB);
312   delete Builder;
313 }
314
315 void
316 LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
317                                   unsigned OptLevel) {
318   PassManagerBuilder *Builder = unwrap(PMB);
319   Builder->OptLevel = OptLevel;
320 }
321
322 void
323 LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
324                                    unsigned SizeLevel) {
325   PassManagerBuilder *Builder = unwrap(PMB);
326   Builder->SizeLevel = SizeLevel;
327 }
328
329 void
330 LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
331                                             LLVMBool Value) {
332   PassManagerBuilder *Builder = unwrap(PMB);
333   Builder->DisableUnitAtATime = Value;
334 }
335
336 void
337 LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
338                                             LLVMBool Value) {
339   PassManagerBuilder *Builder = unwrap(PMB);
340   Builder->DisableUnrollLoops = Value;
341 }
342
343 void
344 LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
345                                                  LLVMBool Value) {
346   PassManagerBuilder *Builder = unwrap(PMB);
347   Builder->DisableSimplifyLibCalls = Value;
348 }
349
350 void
351 LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
352                                               unsigned Threshold) {
353   PassManagerBuilder *Builder = unwrap(PMB);
354   Builder->Inliner = createFunctionInliningPass(Threshold);
355 }
356
357 void
358 LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
359                                                   LLVMPassManagerRef PM) {
360   PassManagerBuilder *Builder = unwrap(PMB);
361   FunctionPassManager *FPM = unwrap<FunctionPassManager>(PM);
362   Builder->populateFunctionPassManager(*FPM);
363 }
364
365 void
366 LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
367                                                 LLVMPassManagerRef PM) {
368   PassManagerBuilder *Builder = unwrap(PMB);
369   PassManagerBase *MPM = unwrap(PM);
370   Builder->populateModulePassManager(*MPM);
371 }
372
373 void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
374                                                   LLVMPassManagerRef PM,
375                                                   bool Internalize,
376                                                   bool RunInliner) {
377   PassManagerBuilder *Builder = unwrap(PMB);
378   PassManagerBase *LPM = unwrap(PM);
379   Builder->populateLTOPassManager(*LPM, Internalize, RunInliner);
380 }