Move methods in PassManagerBuilder offline.
[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/PassManager.h"
19 #include "llvm/DefaultPasses.h"
20 #include "llvm/PassManager.h"
21 #include "llvm/Analysis/Passes.h"
22 #include "llvm/Analysis/Verifier.h"
23 #include "llvm/Target/TargetLibraryInfo.h"
24 #include "llvm/Transforms/Scalar.h"
25 #include "llvm/Transforms/IPO.h"
26
27 using namespace llvm;
28
29 PassManagerBuilder::PassManagerBuilder() {
30     OptLevel = 2;
31     SizeLevel = 0;
32     LibraryInfo = 0;
33     Inliner = 0;
34     DisableSimplifyLibCalls = false;
35     DisableUnitAtATime = false;
36     DisableUnrollLoops = false;
37 }
38
39 PassManagerBuilder::~PassManagerBuilder() {
40   delete LibraryInfo;
41   delete Inliner;
42 }
43
44 void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
45   Extensions.push_back(std::make_pair(Ty, Fn));
46 }
47
48 void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
49                                            PassManagerBase &PM) const {
50   for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
51     if (Extensions[i].first == ETy)
52       Extensions[i].second(*this, PM);
53 }
54
55 void
56 PassManagerBuilder::addInitialAliasAnalysisPasses(PassManagerBase &PM) const {
57   // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
58   // BasicAliasAnalysis wins if they disagree. This is intended to help
59   // support "obvious" type-punning idioms.
60   PM.add(createTypeBasedAliasAnalysisPass());
61   PM.add(createBasicAliasAnalysisPass());
62 }
63
64 void PassManagerBuilder::populateFunctionPassManager(FunctionPassManager &FPM) {
65   addExtensionsToPM(EP_EarlyAsPossible, FPM);
66
67   // Add LibraryInfo if we have some.
68   if (LibraryInfo) FPM.add(new TargetLibraryInfo(*LibraryInfo));
69
70   if (OptLevel == 0) return;
71
72   addInitialAliasAnalysisPasses(FPM);
73
74   FPM.add(createCFGSimplificationPass());
75   FPM.add(createScalarReplAggregatesPass());
76   FPM.add(createEarlyCSEPass());
77   FPM.add(createLowerExpectIntrinsicPass());
78 }
79
80 void PassManagerBuilder::populateModulePassManager(PassManagerBase &MPM) {
81   // If all optimizations are disabled, just run the always-inline pass.
82   if (OptLevel == 0) {
83     if (Inliner) {
84       MPM.add(Inliner);
85       Inliner = 0;
86     }
87     return;
88   }
89
90   // Add LibraryInfo if we have some.
91   if (LibraryInfo) MPM.add(new TargetLibraryInfo(*LibraryInfo));
92
93   addInitialAliasAnalysisPasses(MPM);
94
95   if (!DisableUnitAtATime) {
96     MPM.add(createGlobalOptimizerPass());     // Optimize out global vars
97
98     MPM.add(createIPSCCPPass());              // IP SCCP
99     MPM.add(createDeadArgEliminationPass());  // Dead argument elimination
100
101     MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
102     MPM.add(createCFGSimplificationPass());   // Clean up after IPCP & DAE
103   }
104
105   // Start of CallGraph SCC passes.
106   if (!DisableUnitAtATime)
107     MPM.add(createPruneEHPass());             // Remove dead EH info
108   if (Inliner) {
109     MPM.add(Inliner);
110     Inliner = 0;
111   }
112   if (!DisableUnitAtATime)
113     MPM.add(createFunctionAttrsPass());       // Set readonly/readnone attrs
114   if (OptLevel > 2)
115     MPM.add(createArgumentPromotionPass());   // Scalarize uninlined fn args
116
117   // Start of function pass.
118   // Break up aggregate allocas, using SSAUpdater.
119   MPM.add(createScalarReplAggregatesPass(-1, false));
120   MPM.add(createEarlyCSEPass());              // Catch trivial redundancies
121   if (!DisableSimplifyLibCalls)
122     MPM.add(createSimplifyLibCallsPass());    // Library Call Optimizations
123   MPM.add(createJumpThreadingPass());         // Thread jumps.
124   MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
125   MPM.add(createCFGSimplificationPass());     // Merge & remove BBs
126   MPM.add(createInstructionCombiningPass());  // Combine silly seq's
127
128   MPM.add(createTailCallEliminationPass());   // Eliminate tail calls
129   MPM.add(createCFGSimplificationPass());     // Merge & remove BBs
130   MPM.add(createReassociatePass());           // Reassociate expressions
131   MPM.add(createLoopRotatePass());            // Rotate Loop
132   MPM.add(createLICMPass());                  // Hoist loop invariants
133   MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
134   MPM.add(createInstructionCombiningPass());
135   MPM.add(createIndVarSimplifyPass());        // Canonicalize indvars
136   MPM.add(createLoopIdiomPass());             // Recognize idioms like memset.
137   MPM.add(createLoopDeletionPass());          // Delete dead loops
138   if (!DisableUnrollLoops)
139     MPM.add(createLoopUnrollPass());          // Unroll small loops
140   addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
141
142   if (OptLevel > 1)
143     MPM.add(createGVNPass());                 // Remove redundancies
144   MPM.add(createMemCpyOptPass());             // Remove memcpy / form memset
145   MPM.add(createSCCPPass());                  // Constant prop with SCCP
146
147   // Run instcombine after redundancy elimination to exploit opportunities
148   // opened up by them.
149   MPM.add(createInstructionCombiningPass());
150   MPM.add(createJumpThreadingPass());         // Thread jumps
151   MPM.add(createCorrelatedValuePropagationPass());
152   MPM.add(createDeadStoreEliminationPass());  // Delete dead stores
153
154   addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
155
156   MPM.add(createAggressiveDCEPass());         // Delete dead instructions
157   MPM.add(createCFGSimplificationPass());     // Merge & remove BBs
158   MPM.add(createInstructionCombiningPass());  // Clean up after everything.
159
160   if (!DisableUnitAtATime) {
161     // FIXME: We shouldn't bother with this anymore.
162     MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
163
164     // GlobalOpt already deletes dead functions and globals, at -O3 try a
165     // late pass of GlobalDCE.  It is capable of deleting dead cycles.
166     if (OptLevel > 2)
167       MPM.add(createGlobalDCEPass());         // Remove dead fns and globals.
168
169     if (OptLevel > 1)
170       MPM.add(createConstantMergePass());     // Merge dup global constants
171   }
172 }
173
174 void PassManagerBuilder::populateLTOPassManager(PassManagerBase &PM,
175                                                 bool Internalize,
176                                                 bool RunInliner) {
177   // Provide AliasAnalysis services for optimizations.
178   addInitialAliasAnalysisPasses(PM);
179
180   // Now that composite has been compiled, scan through the module, looking
181   // for a main function.  If main is defined, mark all other functions
182   // internal.
183   if (Internalize)
184     PM.add(createInternalizePass(true));
185
186   // Propagate constants at call sites into the functions they call.  This
187   // opens opportunities for globalopt (and inlining) by substituting function
188   // pointers passed as arguments to direct uses of functions.
189   PM.add(createIPSCCPPass());
190
191   // Now that we internalized some globals, see if we can hack on them!
192   PM.add(createGlobalOptimizerPass());
193
194   // Linking modules together can lead to duplicated global constants, only
195   // keep one copy of each constant.
196   PM.add(createConstantMergePass());
197
198   // Remove unused arguments from functions.
199   PM.add(createDeadArgEliminationPass());
200
201   // Reduce the code after globalopt and ipsccp.  Both can open up significant
202   // simplification opportunities, and both can propagate functions through
203   // function pointers.  When this happens, we often have to resolve varargs
204   // calls, etc, so let instcombine do this.
205   PM.add(createInstructionCombiningPass());
206
207   // Inline small functions
208   if (RunInliner)
209     PM.add(createFunctionInliningPass());
210
211   PM.add(createPruneEHPass());   // Remove dead EH info.
212
213   // Optimize globals again if we ran the inliner.
214   if (RunInliner)
215     PM.add(createGlobalOptimizerPass());
216   PM.add(createGlobalDCEPass()); // Remove dead functions.
217
218   // If we didn't decide to inline a function, check to see if we can
219   // transform it to pass arguments by value instead of by reference.
220   PM.add(createArgumentPromotionPass());
221
222   // The IPO passes may leave cruft around.  Clean up after them.
223   PM.add(createInstructionCombiningPass());
224   PM.add(createJumpThreadingPass());
225   // Break up allocas
226   PM.add(createScalarReplAggregatesPass());
227
228   // Run a few AA driven optimizations here and now, to cleanup the code.
229   PM.add(createFunctionAttrsPass()); // Add nocapture.
230   PM.add(createGlobalsModRefPass()); // IP alias analysis.
231
232   PM.add(createLICMPass());      // Hoist loop invariants.
233   PM.add(createGVNPass());       // Remove redundancies.
234   PM.add(createMemCpyOptPass()); // Remove dead memcpys.
235   // Nuke dead stores.
236   PM.add(createDeadStoreEliminationPass());
237
238   // Cleanup and simplify the code after the scalar optimizations.
239   PM.add(createInstructionCombiningPass());
240
241   PM.add(createJumpThreadingPass());
242
243   // Delete basic blocks, which optimization passes may have killed.
244   PM.add(createCFGSimplificationPass());
245
246   // Now that we have optimized the program, discard unreachable functions.
247   PM.add(createGlobalDCEPass());
248 }