Add createStandardLTOPasses to StandardPasses.h, and move lto and llvm-ld over.
[oota-llvm.git] / include / llvm / Support / StandardPasses.h
1 //===-- llvm/Support/StandardPasses.h - Standard pass lists -----*- C++ -*-===//
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 utility functions for creating a "standard" set of
11 // optimization passes, so that compilers and tools which use optimization
12 // passes use the same set of standard passes.
13 //
14 // These are implemented as inline functions so that we do not have to worry
15 // about link issues.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_SUPPORT_STANDARDPASSES_H
20 #define LLVM_SUPPORT_STANDARDPASSES_H
21
22 #include "llvm/PassManager.h"
23 #include "llvm/Analysis/Passes.h"
24 #include "llvm/Analysis/Verifier.h"
25 #include "llvm/Transforms/Scalar.h"
26 #include "llvm/Transforms/IPO.h"
27
28 namespace llvm {
29   /// createStandardFunctionPasses - Add the standard list of function passes to
30   /// the provided pass manager.
31   ///
32   /// \arg OptimizationLevel - The optimization level, corresponding to -O0,
33   /// -O1, etc.
34   static inline void createStandardFunctionPasses(FunctionPassManager *PM,
35                                                   unsigned OptimizationLevel);
36
37   /// createStandardModulePasses - Add the standard list of module passes to the
38   /// provided pass manager.
39   ///
40   /// \arg OptimizationLevel - The optimization level, corresponding to -O0,
41   /// -O1, etc.
42   /// \arg OptimizeSize - Whether the transformations should optimize for size.
43   /// \arg UnitAtATime - Allow passes which may make global module changes.
44   /// \arg UnrollLoops - Allow loop unrolling.
45   /// \arg SimplifyLibCalls - Allow library calls to be simplified.
46   /// \arg HaveExceptions - Whether the module may have code using exceptions.
47   /// \arg InliningPass - The inlining pass to use, if any, or null. This will
48   /// always be added, even at -O0.a
49   static inline void createStandardModulePasses(PassManager *PM,
50                                                 unsigned OptimizationLevel,
51                                                 bool OptimizeSize,
52                                                 bool UnitAtATime,
53                                                 bool UnrollLoops,
54                                                 bool SimplifyLibCalls,
55                                                 bool HaveExceptions,
56                                                 Pass *InliningPass);
57
58   /// createStandardLTOPasses - Add the standard list of module passes suitable
59   /// for link time optimization.
60   ///
61   /// Internalize - Run the internalize pass.
62   /// RunInliner - Use a function inlining pass.
63   /// RunSecondGlobalOpt - Run the global optimizer pass twice.
64   /// VerifyEach - Run the verifier after each pass.
65   //
66   // FIXME: RunSecondGlobalOpt should go away once we resolve which of LTO or
67   // llvm-ld is better.
68   static inline void createStandardLTOPasses(PassManager *PM,
69                                              bool Internalize,
70                                              bool RunInliner,
71                                              bool RunSecondGlobalOpt,
72                                              bool VerifyEach);
73
74   // Implementations
75
76   static inline void createStandardFunctionPasses(FunctionPassManager *PM,
77                                                   unsigned OptimizationLevel) {
78     if (OptimizationLevel > 0) {
79       PM->add(createCFGSimplificationPass());
80       if (OptimizationLevel == 1)
81         PM->add(createPromoteMemoryToRegisterPass());
82       else
83         PM->add(createScalarReplAggregatesPass());
84       PM->add(createInstructionCombiningPass());
85     }
86   }
87
88   static inline void createStandardModulePasses(PassManager *PM,
89                                                 unsigned OptimizationLevel,
90                                                 bool OptimizeSize,
91                                                 bool UnitAtATime,
92                                                 bool UnrollLoops,
93                                                 bool SimplifyLibCalls,
94                                                 bool HaveExceptions,
95                                                 Pass *InliningPass) {
96     if (OptimizationLevel == 0) {
97       if (InliningPass)
98         PM->add(InliningPass);
99     } else {
100       if (UnitAtATime)
101         PM->add(createRaiseAllocationsPass());    // call %malloc -> malloc inst
102       PM->add(createCFGSimplificationPass());     // Clean up disgusting code
103        // Kill useless allocas
104       PM->add(createPromoteMemoryToRegisterPass());
105       if (UnitAtATime) {
106         PM->add(createGlobalOptimizerPass());     // Optimize out global vars
107         PM->add(createGlobalDCEPass());           // Remove unused fns and globs
108         // IP Constant Propagation
109         PM->add(createIPConstantPropagationPass());
110         PM->add(createDeadArgEliminationPass());  // Dead argument elimination
111       }
112       PM->add(createInstructionCombiningPass());  // Clean up after IPCP & DAE
113       PM->add(createCFGSimplificationPass());     // Clean up after IPCP & DAE
114       if (UnitAtATime) {
115         if (HaveExceptions)
116           PM->add(createPruneEHPass());           // Remove dead EH info
117         PM->add(createFunctionAttrsPass());       // Set readonly/readnone attrs
118       }
119       if (InliningPass)
120         PM->add(InliningPass);
121       if (OptimizationLevel > 2)
122         PM->add(createArgumentPromotionPass());   // Scalarize uninlined fn args
123       if (SimplifyLibCalls)
124         PM->add(createSimplifyLibCallsPass());    // Library Call Optimizations
125       PM->add(createInstructionCombiningPass());  // Cleanup for scalarrepl.
126       PM->add(createJumpThreadingPass());         // Thread jumps.
127       PM->add(createCFGSimplificationPass());     // Merge & remove BBs
128       PM->add(createScalarReplAggregatesPass());  // Break up aggregate allocas
129       PM->add(createInstructionCombiningPass());  // Combine silly seq's
130       PM->add(createCondPropagationPass());       // Propagate conditionals
131       PM->add(createTailCallEliminationPass());   // Eliminate tail calls
132       PM->add(createCFGSimplificationPass());     // Merge & remove BBs
133       PM->add(createReassociatePass());           // Reassociate expressions
134       PM->add(createLoopRotatePass());            // Rotate Loop
135       PM->add(createLICMPass());                  // Hoist loop invariants
136       PM->add(createLoopUnswitchPass(OptimizeSize));
137       PM->add(createLoopIndexSplitPass());        // Split loop index
138       PM->add(createInstructionCombiningPass());  
139       PM->add(createIndVarSimplifyPass());        // Canonicalize indvars
140       PM->add(createLoopDeletionPass());          // Delete dead loops
141       if (UnrollLoops)
142         PM->add(createLoopUnrollPass());          // Unroll small loops
143       PM->add(createInstructionCombiningPass());  // Clean up after the unroller
144       PM->add(createGVNPass());                   // Remove redundancies
145       PM->add(createMemCpyOptPass());             // Remove memcpy / form memset
146       PM->add(createSCCPPass());                  // Constant prop with SCCP
147     
148       // Run instcombine after redundancy elimination to exploit opportunities
149       // opened up by them.
150       PM->add(createInstructionCombiningPass());
151       PM->add(createCondPropagationPass());       // Propagate conditionals
152       PM->add(createDeadStoreEliminationPass());  // Delete dead stores
153       PM->add(createAggressiveDCEPass());         // Delete dead instructions
154       PM->add(createCFGSimplificationPass());     // Merge & remove BBs
155
156       if (UnitAtATime) {
157         PM->add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
158         PM->add(createDeadTypeEliminationPass()); // Eliminate dead types
159       }
160
161       if (OptimizationLevel > 1 && UnitAtATime)
162         PM->add(createConstantMergePass());       // Merge dup global constants
163     }
164   }
165
166   static inline void addOnePass(PassManager *PM, Pass *P, bool AndVerify) {
167     PM->add(P);
168
169     if (AndVerify)
170       PM->add(createVerifierPass());
171   }
172
173   static inline void createStandardLTOPasses(PassManager *PM,
174                                              bool Internalize,
175                                              bool RunInliner,
176                                              bool RunSecondGlobalOpt,
177                                              bool VerifyEach) {
178     // Now that composite has been compiled, scan through the module, looking
179     // for a main function.  If main is defined, mark all other functions
180     // internal.
181     if (Internalize)
182       addOnePass(PM, createInternalizePass(true), VerifyEach);
183
184     // Propagate constants at call sites into the functions they call.  This
185     // opens opportunities for globalopt (and inlining) by substituting function
186     // pointers passed as arguments to direct uses of functions.  
187     addOnePass(PM, createIPSCCPPass(), VerifyEach);
188
189     // Now that we internalized some globals, see if we can hack on them!
190     addOnePass(PM, createGlobalOptimizerPass(), VerifyEach);
191     
192     // Linking modules together can lead to duplicated global constants, only
193     // keep one copy of each constant...
194     addOnePass(PM, createConstantMergePass(), VerifyEach);
195     
196     // Remove unused arguments from functions...
197     addOnePass(PM, createDeadArgEliminationPass(), VerifyEach);
198
199     // Reduce the code after globalopt and ipsccp.  Both can open up significant
200     // simplification opportunities, and both can propagate functions through
201     // function pointers.  When this happens, we often have to resolve varargs
202     // calls, etc, so let instcombine do this.
203     addOnePass(PM, createInstructionCombiningPass(), VerifyEach);
204
205     // Inline small functions
206     if (RunInliner)
207       addOnePass(PM, createFunctionInliningPass(), VerifyEach);
208
209     addOnePass(PM, createPruneEHPass(), VerifyEach);   // Remove dead EH info.
210     // Optimize globals again.
211     if (RunSecondGlobalOpt)
212       addOnePass(PM, createGlobalOptimizerPass(), VerifyEach);
213     addOnePass(PM, createGlobalDCEPass(), VerifyEach); // Remove dead functions.
214
215     // If we didn't decide to inline a function, check to see if we can
216     // transform it to pass arguments by value instead of by reference.
217     addOnePass(PM, createArgumentPromotionPass(), VerifyEach);
218
219     // The IPO passes may leave cruft around.  Clean up after them.
220     addOnePass(PM, createInstructionCombiningPass(), VerifyEach);
221     addOnePass(PM, createJumpThreadingPass(), VerifyEach);
222     // Break up allocas
223     addOnePass(PM, createScalarReplAggregatesPass(), VerifyEach);
224
225     // Run a few AA driven optimizations here and now, to cleanup the code.
226     addOnePass(PM, createFunctionAttrsPass(), VerifyEach); // Add nocapture.
227     addOnePass(PM, createGlobalsModRefPass(), VerifyEach); // IP alias analysis.
228
229     addOnePass(PM, createLICMPass(), VerifyEach);      // Hoist loop invariants.
230     addOnePass(PM, createGVNPass(), VerifyEach);       // Remove redundancies.
231     addOnePass(PM, createMemCpyOptPass(), VerifyEach); // Remove dead memcpys.
232     // Nuke dead stores.
233     addOnePass(PM, createDeadStoreEliminationPass(), VerifyEach);
234
235     // Cleanup and simplify the code after the scalar optimizations.
236     addOnePass(PM, createInstructionCombiningPass(), VerifyEach);
237
238     addOnePass(PM, createJumpThreadingPass(), VerifyEach);
239     // Cleanup jump threading.
240     addOnePass(PM, createPromoteMemoryToRegisterPass(), VerifyEach);
241     
242     // Delete basic blocks, which optimization passes may have killed...
243     addOnePass(PM, createCFGSimplificationPass(), VerifyEach);
244
245     // Now that we have optimized the program, discard unreachable functions.
246     addOnePass(PM, createGlobalDCEPass(), VerifyEach);
247   }
248 }
249
250 #endif