remove an extraneous mem2reg pass early in the pipe. Since
[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   /// VerifyEach - Run the verifier after each pass.
64   static inline void createStandardLTOPasses(PassManager *PM,
65                                              bool Internalize,
66                                              bool RunInliner,
67                                              bool VerifyEach);
68
69   // Implementations
70
71   static inline void createStandardFunctionPasses(FunctionPassManager *PM,
72                                                   unsigned OptimizationLevel) {
73     if (OptimizationLevel > 0) {
74       PM->add(createCFGSimplificationPass());
75       if (OptimizationLevel == 1)
76         PM->add(createPromoteMemoryToRegisterPass());
77       else
78         PM->add(createScalarReplAggregatesPass());
79       PM->add(createInstructionCombiningPass());
80     }
81   }
82
83   /// createStandardModulePasses - Add the standard module passes.  This is
84   /// expected to be run after the standard function passes.
85   static inline void createStandardModulePasses(PassManager *PM,
86                                                 unsigned OptimizationLevel,
87                                                 bool OptimizeSize,
88                                                 bool UnitAtATime,
89                                                 bool UnrollLoops,
90                                                 bool SimplifyLibCalls,
91                                                 bool HaveExceptions,
92                                                 Pass *InliningPass) {
93     if (OptimizationLevel == 0) {
94       if (InliningPass)
95         PM->add(InliningPass);
96       return;
97     }
98     
99     if (UnitAtATime)
100       PM->add(createRaiseAllocationsPass());    // call %malloc -> malloc inst
101     PM->add(createCFGSimplificationPass());     // Clean up disgusting code
102     if (UnitAtATime) {
103       PM->add(createGlobalOptimizerPass());     // Optimize out global vars
104       PM->add(createGlobalDCEPass());           // Remove unused fns and globs
105       // IP Constant Propagation
106       PM->add(createIPConstantPropagationPass());
107       PM->add(createDeadArgEliminationPass());  // Dead argument elimination
108     }
109     PM->add(createInstructionCombiningPass());  // Clean up after IPCP & DAE
110     PM->add(createCFGSimplificationPass());     // Clean up after IPCP & DAE
111     if (UnitAtATime) {
112       if (HaveExceptions)
113         PM->add(createPruneEHPass());           // Remove dead EH info
114       PM->add(createFunctionAttrsPass());       // Set readonly/readnone attrs
115     }
116     if (InliningPass)
117       PM->add(InliningPass);
118     if (OptimizationLevel > 2)
119       PM->add(createArgumentPromotionPass());   // Scalarize uninlined fn args
120     if (SimplifyLibCalls)
121       PM->add(createSimplifyLibCallsPass());    // Library Call Optimizations
122     PM->add(createInstructionCombiningPass());  // Cleanup for scalarrepl.
123     PM->add(createJumpThreadingPass());         // Thread jumps.
124     PM->add(createCFGSimplificationPass());     // Merge & remove BBs
125     PM->add(createScalarReplAggregatesPass());  // Break up aggregate allocas
126     PM->add(createInstructionCombiningPass());  // Combine silly seq's
127     PM->add(createCondPropagationPass());       // Propagate conditionals
128     PM->add(createTailCallEliminationPass());   // Eliminate tail calls
129     PM->add(createCFGSimplificationPass());     // Merge & remove BBs
130     PM->add(createReassociatePass());           // Reassociate expressions
131     PM->add(createLoopRotatePass());            // Rotate Loop
132     PM->add(createLICMPass());                  // Hoist loop invariants
133     PM->add(createLoopUnswitchPass(OptimizeSize));
134     PM->add(createInstructionCombiningPass());  
135     PM->add(createIndVarSimplifyPass());        // Canonicalize indvars
136     PM->add(createLoopDeletionPass());          // Delete dead loops
137     if (UnrollLoops)
138       PM->add(createLoopUnrollPass());          // Unroll small loops
139     PM->add(createInstructionCombiningPass());  // Clean up after the unroller
140     PM->add(createGVNPass());                   // Remove redundancies
141     PM->add(createMemCpyOptPass());             // Remove memcpy / form memset
142     PM->add(createSCCPPass());                  // Constant prop with SCCP
143   
144     // Run instcombine after redundancy elimination to exploit opportunities
145     // opened up by them.
146     PM->add(createInstructionCombiningPass());
147     PM->add(createCondPropagationPass());       // Propagate conditionals
148     PM->add(createDeadStoreEliminationPass());  // Delete dead stores
149     PM->add(createAggressiveDCEPass());         // Delete dead instructions
150     PM->add(createCFGSimplificationPass());     // Merge & remove BBs
151
152     if (UnitAtATime) {
153       PM->add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
154       PM->add(createDeadTypeEliminationPass()); // Eliminate dead types
155     }
156
157     if (OptimizationLevel > 1 && UnitAtATime)
158       PM->add(createConstantMergePass());       // Merge dup global constants
159   }
160
161   static inline void addOnePass(PassManager *PM, Pass *P, bool AndVerify) {
162     PM->add(P);
163
164     if (AndVerify)
165       PM->add(createVerifierPass());
166   }
167
168   static inline void createStandardLTOPasses(PassManager *PM,
169                                              bool Internalize,
170                                              bool RunInliner,
171                                              bool VerifyEach) {
172     // Now that composite has been compiled, scan through the module, looking
173     // for a main function.  If main is defined, mark all other functions
174     // internal.
175     if (Internalize)
176       addOnePass(PM, createInternalizePass(true), VerifyEach);
177
178     // Propagate constants at call sites into the functions they call.  This
179     // opens opportunities for globalopt (and inlining) by substituting function
180     // pointers passed as arguments to direct uses of functions.  
181     addOnePass(PM, createIPSCCPPass(), VerifyEach);
182
183     // Now that we internalized some globals, see if we can hack on them!
184     addOnePass(PM, createGlobalOptimizerPass(), VerifyEach);
185     
186     // Linking modules together can lead to duplicated global constants, only
187     // keep one copy of each constant...
188     addOnePass(PM, createConstantMergePass(), VerifyEach);
189     
190     // Remove unused arguments from functions...
191     addOnePass(PM, createDeadArgEliminationPass(), VerifyEach);
192
193     // Reduce the code after globalopt and ipsccp.  Both can open up significant
194     // simplification opportunities, and both can propagate functions through
195     // function pointers.  When this happens, we often have to resolve varargs
196     // calls, etc, so let instcombine do this.
197     addOnePass(PM, createInstructionCombiningPass(), VerifyEach);
198
199     // Inline small functions
200     if (RunInliner)
201       addOnePass(PM, createFunctionInliningPass(), VerifyEach);
202
203     addOnePass(PM, createPruneEHPass(), VerifyEach);   // Remove dead EH info.
204     // Optimize globals again if we ran the inliner.
205     if (RunInliner)
206       addOnePass(PM, createGlobalOptimizerPass(), VerifyEach);
207     addOnePass(PM, createGlobalDCEPass(), VerifyEach); // Remove dead functions.
208
209     // If we didn't decide to inline a function, check to see if we can
210     // transform it to pass arguments by value instead of by reference.
211     addOnePass(PM, createArgumentPromotionPass(), VerifyEach);
212
213     // The IPO passes may leave cruft around.  Clean up after them.
214     addOnePass(PM, createInstructionCombiningPass(), VerifyEach);
215     addOnePass(PM, createJumpThreadingPass(), VerifyEach);
216     // Break up allocas
217     addOnePass(PM, createScalarReplAggregatesPass(), VerifyEach);
218
219     // Run a few AA driven optimizations here and now, to cleanup the code.
220     addOnePass(PM, createFunctionAttrsPass(), VerifyEach); // Add nocapture.
221     addOnePass(PM, createGlobalsModRefPass(), VerifyEach); // IP alias analysis.
222
223     addOnePass(PM, createLICMPass(), VerifyEach);      // Hoist loop invariants.
224     addOnePass(PM, createGVNPass(), VerifyEach);       // Remove redundancies.
225     addOnePass(PM, createMemCpyOptPass(), VerifyEach); // Remove dead memcpys.
226     // Nuke dead stores.
227     addOnePass(PM, createDeadStoreEliminationPass(), VerifyEach);
228
229     // Cleanup and simplify the code after the scalar optimizations.
230     addOnePass(PM, createInstructionCombiningPass(), VerifyEach);
231
232     addOnePass(PM, createJumpThreadingPass(), VerifyEach);
233     // Cleanup jump threading.
234     addOnePass(PM, createPromoteMemoryToRegisterPass(), VerifyEach);
235     
236     // Delete basic blocks, which optimization passes may have killed...
237     addOnePass(PM, createCFGSimplificationPass(), VerifyEach);
238
239     // Now that we have optimized the program, discard unreachable functions.
240     addOnePass(PM, createGlobalDCEPass(), VerifyEach);
241   }
242 }
243
244 #endif