1 //===-- llvm/Support/StandardPasses.h - Standard pass lists -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 // These are implemented as inline functions so that we do not have to worry
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_SUPPORT_STANDARDPASSES_H
20 #define LLVM_SUPPORT_STANDARDPASSES_H
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"
29 /// createStandardFunctionPasses - Add the standard list of function passes to
30 /// the provided pass manager.
32 /// \arg OptimizationLevel - The optimization level, corresponding to -O0,
34 static inline void createStandardFunctionPasses(PassManagerBase *PM,
35 unsigned OptimizationLevel);
37 /// createStandardModulePasses - Add the standard list of module passes to the
38 /// provided pass manager.
40 /// \arg OptimizationLevel - The optimization level, corresponding to -O0,
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(PassManagerBase *PM,
50 unsigned OptimizationLevel,
54 bool SimplifyLibCalls,
58 /// createStandardLTOPasses - Add the standard list of module passes suitable
59 /// for link time optimization.
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(PassManagerBase *PM,
71 static inline void createStandardAliasAnalysisPasses(PassManagerBase *PM) {
72 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
73 // BasicAliasAnalysis wins if they disagree. This is intended to help
74 // support "obvious" type-punning idioms.
75 PM->add(createTypeBasedAliasAnalysisPass());
76 PM->add(createBasicAliasAnalysisPass());
79 static inline void createStandardFunctionPasses(PassManagerBase *PM,
80 unsigned OptimizationLevel) {
81 if (OptimizationLevel > 0) {
82 createStandardAliasAnalysisPasses(PM);
83 PM->add(createCFGSimplificationPass());
84 PM->add(createScalarReplAggregatesPass());
85 PM->add(createEarlyCSEPass());
89 /// createStandardModulePasses - Add the standard module passes. This is
90 /// expected to be run after the standard function passes.
91 static inline void createStandardModulePasses(PassManagerBase *PM,
92 unsigned OptimizationLevel,
96 bool OptimizeBuiltins,
99 createStandardAliasAnalysisPasses(PM);
101 if (OptimizationLevel == 0) {
103 PM->add(InliningPass);
108 PM->add(createGlobalOptimizerPass()); // Optimize out global vars
110 PM->add(createIPSCCPPass()); // IP SCCP
111 PM->add(createDeadArgEliminationPass()); // Dead argument elimination
113 PM->add(createInstructionCombiningPass()); // Clean up after IPCP & DAE
114 PM->add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
116 // Start of CallGraph SCC passes.
117 if (UnitAtATime && HaveExceptions)
118 PM->add(createPruneEHPass()); // Remove dead EH info
120 PM->add(InliningPass);
122 PM->add(createFunctionAttrsPass()); // Set readonly/readnone attrs
123 if (OptimizationLevel > 2)
124 PM->add(createArgumentPromotionPass()); // Scalarize uninlined fn args
126 // Start of function pass.
127 // Break up aggregate allocas, using SSAUpdater.
128 PM->add(createScalarReplAggregatesPass(-1, false));
129 PM->add(createEarlyCSEPass()); // Catch trivial redundancies
130 if (OptimizeBuiltins)
131 PM->add(createSimplifyLibCallsPass()); // Library Call Optimizations
132 PM->add(createJumpThreadingPass()); // Thread jumps.
133 PM->add(createCorrelatedValuePropagationPass()); // Propagate conditionals
134 PM->add(createCFGSimplificationPass()); // Merge & remove BBs
135 PM->add(createInstructionCombiningPass()); // Combine silly seq's
137 PM->add(createTailCallEliminationPass()); // Eliminate tail calls
138 PM->add(createCFGSimplificationPass()); // Merge & remove BBs
139 PM->add(createReassociatePass()); // Reassociate expressions
140 PM->add(createLoopRotatePass()); // Rotate Loop
141 PM->add(createLICMPass()); // Hoist loop invariants
142 PM->add(createLoopUnswitchPass(OptimizeSize || OptimizationLevel < 3));
143 PM->add(createInstructionCombiningPass());
144 PM->add(createIndVarSimplifyPass()); // Canonicalize indvars
145 if (OptimizeBuiltins)
146 PM->add(createLoopIdiomPass()); // Recognize idioms like memset.
147 PM->add(createLoopDeletionPass()); // Delete dead loops
149 PM->add(createLoopUnrollPass()); // Unroll small loops
150 PM->add(createInstructionCombiningPass()); // Clean up after the unroller
151 if (OptimizationLevel > 1)
152 PM->add(createGVNPass()); // Remove redundancies
153 PM->add(createMemCpyOptPass()); // Remove memcpy / form memset
154 PM->add(createSCCPPass()); // Constant prop with SCCP
156 // Run instcombine after redundancy elimination to exploit opportunities
157 // opened up by them.
158 PM->add(createInstructionCombiningPass());
159 PM->add(createJumpThreadingPass()); // Thread jumps
160 PM->add(createCorrelatedValuePropagationPass());
161 PM->add(createDeadStoreEliminationPass()); // Delete dead stores
162 PM->add(createAggressiveDCEPass()); // Delete dead instructions
163 PM->add(createCFGSimplificationPass()); // Merge & remove BBs
166 PM->add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
167 PM->add(createDeadTypeEliminationPass()); // Eliminate dead types
169 // GlobalOpt already deletes dead functions and globals, at -O3 try a
170 // late pass of GlobalDCE. It is capable of deleting dead cycles.
171 if (OptimizationLevel > 2)
172 PM->add(createGlobalDCEPass()); // Remove dead fns and globals.
174 if (OptimizationLevel > 1)
175 PM->add(createConstantMergePass()); // Merge dup global constants
179 static inline void addOnePass(PassManagerBase *PM, Pass *P, bool AndVerify) {
183 PM->add(createVerifierPass());
186 static inline void createStandardLTOPasses(PassManagerBase *PM,
190 // Provide AliasAnalysis services for optimizations.
191 createStandardAliasAnalysisPasses(PM);
193 // Now that composite has been compiled, scan through the module, looking
194 // for a main function. If main is defined, mark all other functions
197 addOnePass(PM, createInternalizePass(true), VerifyEach);
199 // Propagate constants at call sites into the functions they call. This
200 // opens opportunities for globalopt (and inlining) by substituting function
201 // pointers passed as arguments to direct uses of functions.
202 addOnePass(PM, createIPSCCPPass(), VerifyEach);
204 // Now that we internalized some globals, see if we can hack on them!
205 addOnePass(PM, createGlobalOptimizerPass(), VerifyEach);
207 // Linking modules together can lead to duplicated global constants, only
208 // keep one copy of each constant...
209 addOnePass(PM, createConstantMergePass(), VerifyEach);
211 // Remove unused arguments from functions...
212 addOnePass(PM, createDeadArgEliminationPass(), VerifyEach);
214 // Reduce the code after globalopt and ipsccp. Both can open up significant
215 // simplification opportunities, and both can propagate functions through
216 // function pointers. When this happens, we often have to resolve varargs
217 // calls, etc, so let instcombine do this.
218 addOnePass(PM, createInstructionCombiningPass(), VerifyEach);
220 // Inline small functions
222 addOnePass(PM, createFunctionInliningPass(), VerifyEach);
224 addOnePass(PM, createPruneEHPass(), VerifyEach); // Remove dead EH info.
225 // Optimize globals again if we ran the inliner.
227 addOnePass(PM, createGlobalOptimizerPass(), VerifyEach);
228 addOnePass(PM, createGlobalDCEPass(), VerifyEach); // Remove dead functions.
230 // If we didn't decide to inline a function, check to see if we can
231 // transform it to pass arguments by value instead of by reference.
232 addOnePass(PM, createArgumentPromotionPass(), VerifyEach);
234 // The IPO passes may leave cruft around. Clean up after them.
235 addOnePass(PM, createInstructionCombiningPass(), VerifyEach);
236 addOnePass(PM, createJumpThreadingPass(), VerifyEach);
238 addOnePass(PM, createScalarReplAggregatesPass(), VerifyEach);
240 // Run a few AA driven optimizations here and now, to cleanup the code.
241 addOnePass(PM, createFunctionAttrsPass(), VerifyEach); // Add nocapture.
242 addOnePass(PM, createGlobalsModRefPass(), VerifyEach); // IP alias analysis.
244 addOnePass(PM, createLICMPass(), VerifyEach); // Hoist loop invariants.
245 addOnePass(PM, createGVNPass(), VerifyEach); // Remove redundancies.
246 addOnePass(PM, createMemCpyOptPass(), VerifyEach); // Remove dead memcpys.
248 addOnePass(PM, createDeadStoreEliminationPass(), VerifyEach);
250 // Cleanup and simplify the code after the scalar optimizations.
251 addOnePass(PM, createInstructionCombiningPass(), VerifyEach);
253 addOnePass(PM, createJumpThreadingPass(), VerifyEach);
255 // Delete basic blocks, which optimization passes may have killed.
256 addOnePass(PM, createCFGSimplificationPass(), VerifyEach);
258 // Now that we have optimized the program, discard unreachable functions.
259 addOnePass(PM, createGlobalDCEPass(), VerifyEach);