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