LLVM API Documentation

StandardPasses.h

Go to the documentation of this file.
00001 //===-- llvm/Support/StandardPasses.h - Standard pass lists -----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines utility functions for creating a "standard" set of
00011 // optimization passes, so that compilers and tools which use optimization
00012 // passes use the same set of standard passes.
00013 //
00014 // These are implemented as inline functions so that we do not have to worry
00015 // about link issues.
00016 //
00017 //===----------------------------------------------------------------------===//
00018 
00019 #ifndef LLVM_SUPPORT_STANDARDPASSES_H
00020 #define LLVM_SUPPORT_STANDARDPASSES_H
00021 
00022 #include "llvm/PassManager.h"
00023 #include "llvm/Analysis/Passes.h"
00024 #include "llvm/Analysis/Verifier.h"
00025 #include "llvm/Transforms/Scalar.h"
00026 #include "llvm/Transforms/IPO.h"
00027 
00028 namespace llvm {
00029   /// createStandardFunctionPasses - Add the standard list of function passes to
00030   /// the provided pass manager.
00031   ///
00032   /// \arg OptimizationLevel - The optimization level, corresponding to -O0,
00033   /// -O1, etc.
00034   static inline void createStandardFunctionPasses(FunctionPassManager *PM,
00035                                                   unsigned OptimizationLevel);
00036 
00037   /// createStandardModulePasses - Add the standard list of module passes to the
00038   /// provided pass manager.
00039   ///
00040   /// \arg OptimizationLevel - The optimization level, corresponding to -O0,
00041   /// -O1, etc.
00042   /// \arg OptimizeSize - Whether the transformations should optimize for size.
00043   /// \arg UnitAtATime - Allow passes which may make global module changes.
00044   /// \arg UnrollLoops - Allow loop unrolling.
00045   /// \arg SimplifyLibCalls - Allow library calls to be simplified.
00046   /// \arg HaveExceptions - Whether the module may have code using exceptions.
00047   /// \arg InliningPass - The inlining pass to use, if any, or null. This will
00048   /// always be added, even at -O0.a
00049   static inline void createStandardModulePasses(PassManager *PM,
00050                                                 unsigned OptimizationLevel,
00051                                                 bool OptimizeSize,
00052                                                 bool UnitAtATime,
00053                                                 bool UnrollLoops,
00054                                                 bool SimplifyLibCalls,
00055                                                 bool HaveExceptions,
00056                                                 Pass *InliningPass);
00057 
00058   /// createStandardLTOPasses - Add the standard list of module passes suitable
00059   /// for link time optimization.
00060   ///
00061   /// Internalize - Run the internalize pass.
00062   /// RunInliner - Use a function inlining pass.
00063   /// VerifyEach - Run the verifier after each pass.
00064   static inline void createStandardLTOPasses(PassManager *PM,
00065                                              bool Internalize,
00066                                              bool RunInliner,
00067                                              bool VerifyEach);
00068 
00069   // Implementations
00070 
00071   static inline void createStandardFunctionPasses(FunctionPassManager *PM,
00072                                                   unsigned OptimizationLevel) {
00073     if (OptimizationLevel > 0) {
00074       PM->add(createCFGSimplificationPass());
00075       if (OptimizationLevel == 1)
00076         PM->add(createPromoteMemoryToRegisterPass());
00077       else
00078         PM->add(createScalarReplAggregatesPass());
00079       PM->add(createInstructionCombiningPass());
00080     }
00081   }
00082 
00083   static inline void createStandardModulePasses(PassManager *PM,
00084                                                 unsigned OptimizationLevel,
00085                                                 bool OptimizeSize,
00086                                                 bool UnitAtATime,
00087                                                 bool UnrollLoops,
00088                                                 bool SimplifyLibCalls,
00089                                                 bool HaveExceptions,
00090                                                 Pass *InliningPass) {
00091     if (OptimizationLevel == 0) {
00092       if (InliningPass)
00093         PM->add(InliningPass);
00094     } else {
00095       if (UnitAtATime)
00096         PM->add(createRaiseAllocationsPass());    // call %malloc -> malloc inst
00097       PM->add(createCFGSimplificationPass());     // Clean up disgusting code
00098        // Kill useless allocas
00099       PM->add(createPromoteMemoryToRegisterPass());
00100       if (UnitAtATime) {
00101         PM->add(createGlobalOptimizerPass());     // Optimize out global vars
00102         PM->add(createGlobalDCEPass());           // Remove unused fns and globs
00103         // IP Constant Propagation
00104         PM->add(createIPConstantPropagationPass());
00105         PM->add(createDeadArgEliminationPass());  // Dead argument elimination
00106       }
00107       PM->add(createInstructionCombiningPass());  // Clean up after IPCP & DAE
00108       PM->add(createCFGSimplificationPass());     // Clean up after IPCP & DAE
00109       if (UnitAtATime) {
00110         if (HaveExceptions)
00111           PM->add(createPruneEHPass());           // Remove dead EH info
00112         PM->add(createFunctionAttrsPass());       // Set readonly/readnone attrs
00113       }
00114       if (InliningPass)
00115         PM->add(InliningPass);
00116       if (OptimizationLevel > 2)
00117         PM->add(createArgumentPromotionPass());   // Scalarize uninlined fn args
00118       if (SimplifyLibCalls)
00119         PM->add(createSimplifyLibCallsPass());    // Library Call Optimizations
00120       PM->add(createInstructionCombiningPass());  // Cleanup for scalarrepl.
00121       PM->add(createJumpThreadingPass());         // Thread jumps.
00122       PM->add(createCFGSimplificationPass());     // Merge & remove BBs
00123       PM->add(createScalarReplAggregatesPass());  // Break up aggregate allocas
00124       PM->add(createInstructionCombiningPass());  // Combine silly seq's
00125       PM->add(createCondPropagationPass());       // Propagate conditionals
00126       PM->add(createTailCallEliminationPass());   // Eliminate tail calls
00127       PM->add(createCFGSimplificationPass());     // Merge & remove BBs
00128       PM->add(createReassociatePass());           // Reassociate expressions
00129       PM->add(createLoopRotatePass());            // Rotate Loop
00130       PM->add(createLICMPass());                  // Hoist loop invariants
00131       PM->add(createLoopUnswitchPass(OptimizeSize));
00132       PM->add(createInstructionCombiningPass());  
00133       PM->add(createIndVarSimplifyPass());        // Canonicalize indvars
00134       PM->add(createLoopDeletionPass());          // Delete dead loops
00135       if (UnrollLoops)
00136         PM->add(createLoopUnrollPass());          // Unroll small loops
00137       PM->add(createInstructionCombiningPass());  // Clean up after the unroller
00138       PM->add(createGVNPass());                   // Remove redundancies
00139       PM->add(createMemCpyOptPass());             // Remove memcpy / form memset
00140       PM->add(createSCCPPass());                  // Constant prop with SCCP
00141     
00142       // Run instcombine after redundancy elimination to exploit opportunities
00143       // opened up by them.
00144       PM->add(createInstructionCombiningPass());
00145       PM->add(createCondPropagationPass());       // Propagate conditionals
00146       PM->add(createDeadStoreEliminationPass());  // Delete dead stores
00147       PM->add(createAggressiveDCEPass());         // Delete dead instructions
00148       PM->add(createCFGSimplificationPass());     // Merge & remove BBs
00149 
00150       if (UnitAtATime) {
00151         PM->add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
00152         PM->add(createDeadTypeEliminationPass()); // Eliminate dead types
00153       }
00154 
00155       if (OptimizationLevel > 1 && UnitAtATime)
00156         PM->add(createConstantMergePass());       // Merge dup global constants
00157     }
00158   }
00159 
00160   static inline void addOnePass(PassManager *PM, Pass *P, bool AndVerify) {
00161     PM->add(P);
00162 
00163     if (AndVerify)
00164       PM->add(createVerifierPass());
00165   }
00166 
00167   static inline void createStandardLTOPasses(PassManager *PM,
00168                                              bool Internalize,
00169                                              bool RunInliner,
00170                                              bool VerifyEach) {
00171     // Now that composite has been compiled, scan through the module, looking
00172     // for a main function.  If main is defined, mark all other functions
00173     // internal.
00174     if (Internalize)
00175       addOnePass(PM, createInternalizePass(true), VerifyEach);
00176 
00177     // Propagate constants at call sites into the functions they call.  This
00178     // opens opportunities for globalopt (and inlining) by substituting function
00179     // pointers passed as arguments to direct uses of functions.  
00180     addOnePass(PM, createIPSCCPPass(), VerifyEach);
00181 
00182     // Now that we internalized some globals, see if we can hack on them!
00183     addOnePass(PM, createGlobalOptimizerPass(), VerifyEach);
00184     
00185     // Linking modules together can lead to duplicated global constants, only
00186     // keep one copy of each constant...
00187     addOnePass(PM, createConstantMergePass(), VerifyEach);
00188     
00189     // Remove unused arguments from functions...
00190     addOnePass(PM, createDeadArgEliminationPass(), VerifyEach);
00191 
00192     // Reduce the code after globalopt and ipsccp.  Both can open up significant
00193     // simplification opportunities, and both can propagate functions through
00194     // function pointers.  When this happens, we often have to resolve varargs
00195     // calls, etc, so let instcombine do this.
00196     addOnePass(PM, createInstructionCombiningPass(), VerifyEach);
00197 
00198     // Inline small functions
00199     if (RunInliner)
00200       addOnePass(PM, createFunctionInliningPass(), VerifyEach);
00201 
00202     addOnePass(PM, createPruneEHPass(), VerifyEach);   // Remove dead EH info.
00203     // Optimize globals again if we ran the inliner.
00204     if (RunInliner)
00205       addOnePass(PM, createGlobalOptimizerPass(), VerifyEach);
00206     addOnePass(PM, createGlobalDCEPass(), VerifyEach); // Remove dead functions.
00207 
00208     // If we didn't decide to inline a function, check to see if we can
00209     // transform it to pass arguments by value instead of by reference.
00210     addOnePass(PM, createArgumentPromotionPass(), VerifyEach);
00211 
00212     // The IPO passes may leave cruft around.  Clean up after them.
00213     addOnePass(PM, createInstructionCombiningPass(), VerifyEach);
00214     addOnePass(PM, createJumpThreadingPass(), VerifyEach);
00215     // Break up allocas
00216     addOnePass(PM, createScalarReplAggregatesPass(), VerifyEach);
00217 
00218     // Run a few AA driven optimizations here and now, to cleanup the code.
00219     addOnePass(PM, createFunctionAttrsPass(), VerifyEach); // Add nocapture.
00220     addOnePass(PM, createGlobalsModRefPass(), VerifyEach); // IP alias analysis.
00221 
00222     addOnePass(PM, createLICMPass(), VerifyEach);      // Hoist loop invariants.
00223     addOnePass(PM, createGVNPass(), VerifyEach);       // Remove redundancies.
00224     addOnePass(PM, createMemCpyOptPass(), VerifyEach); // Remove dead memcpys.
00225     // Nuke dead stores.
00226     addOnePass(PM, createDeadStoreEliminationPass(), VerifyEach);
00227 
00228     // Cleanup and simplify the code after the scalar optimizations.
00229     addOnePass(PM, createInstructionCombiningPass(), VerifyEach);
00230 
00231     addOnePass(PM, createJumpThreadingPass(), VerifyEach);
00232     // Cleanup jump threading.
00233     addOnePass(PM, createPromoteMemoryToRegisterPass(), VerifyEach);
00234     
00235     // Delete basic blocks, which optimization passes may have killed...
00236     addOnePass(PM, createCFGSimplificationPass(), VerifyEach);
00237 
00238     // Now that we have optimized the program, discard unreachable functions.
00239     addOnePass(PM, createGlobalDCEPass(), VerifyEach);
00240   }
00241 }
00242 
00243 #endif