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