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