LLVM API Documentation

Transforms/IPO/PassManagerBuilder.h
Go to the documentation of this file.
00001 // llvm/Transforms/IPO/PassManagerBuilder.h - Build Standard Pass -*- 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 the PassManagerBuilder class, which is used to set up a
00011 // "standard" optimization sequence suitable for languages like C and C++.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_TRANSFORMS_IPO_PASSMANAGERBUILDER_H
00016 #define LLVM_TRANSFORMS_IPO_PASSMANAGERBUILDER_H
00017 
00018 #include <vector>
00019 
00020 namespace llvm {
00021 class Pass;
00022 class TargetLibraryInfo;
00023 class TargetMachine;
00024 
00025 // The old pass manager infrastructure is hidden in a legacy namespace now.
00026 namespace legacy {
00027 class FunctionPassManager;
00028 class PassManagerBase;
00029 }
00030 using legacy::FunctionPassManager;
00031 using legacy::PassManagerBase;
00032 
00033 /// PassManagerBuilder - This class is used to set up a standard optimization
00034 /// sequence for languages like C and C++, allowing some APIs to customize the
00035 /// pass sequence in various ways. A simple example of using it would be:
00036 ///
00037 ///  PassManagerBuilder Builder;
00038 ///  Builder.OptLevel = 2;
00039 ///  Builder.populateFunctionPassManager(FPM);
00040 ///  Builder.populateModulePassManager(MPM);
00041 ///
00042 /// In addition to setting up the basic passes, PassManagerBuilder allows
00043 /// frontends to vend a plugin API, where plugins are allowed to add extensions
00044 /// to the default pass manager.  They do this by specifying where in the pass
00045 /// pipeline they want to be added, along with a callback function that adds
00046 /// the pass(es).  For example, a plugin that wanted to add a loop optimization
00047 /// could do something like this:
00048 ///
00049 /// static void addMyLoopPass(const PMBuilder &Builder, PassManagerBase &PM) {
00050 ///   if (Builder.getOptLevel() > 2 && Builder.getOptSizeLevel() == 0)
00051 ///     PM.add(createMyAwesomePass());
00052 /// }
00053 ///   ...
00054 ///   Builder.addExtension(PassManagerBuilder::EP_LoopOptimizerEnd,
00055 ///                        addMyLoopPass);
00056 ///   ...
00057 class PassManagerBuilder {
00058 public:
00059   /// Extensions are passed the builder itself (so they can see how it is
00060   /// configured) as well as the pass manager to add stuff to.
00061   typedef void (*ExtensionFn)(const PassManagerBuilder &Builder,
00062                               PassManagerBase &PM);
00063   enum ExtensionPointTy {
00064     /// EP_EarlyAsPossible - This extension point allows adding passes before
00065     /// any other transformations, allowing them to see the code as it is coming
00066     /// out of the frontend.
00067     EP_EarlyAsPossible,
00068 
00069     /// EP_ModuleOptimizerEarly - This extension point allows adding passes
00070     /// just before the main module-level optimization passes.
00071     EP_ModuleOptimizerEarly,
00072 
00073     /// EP_LoopOptimizerEnd - This extension point allows adding loop passes to
00074     /// the end of the loop optimizer.
00075     EP_LoopOptimizerEnd,
00076 
00077     /// EP_ScalarOptimizerLate - This extension point allows adding optimization
00078     /// passes after most of the main optimizations, but before the last
00079     /// cleanup-ish optimizations.
00080     EP_ScalarOptimizerLate,
00081 
00082     /// EP_OptimizerLast -- This extension point allows adding passes that
00083     /// run after everything else.
00084     EP_OptimizerLast,
00085 
00086     /// EP_EnabledOnOptLevel0 - This extension point allows adding passes that
00087     /// should not be disabled by O0 optimization level. The passes will be
00088     /// inserted after the inlining pass.
00089     EP_EnabledOnOptLevel0,
00090 
00091     /// EP_Peephole - This extension point allows adding passes that perform
00092     /// peephole optimizations similar to the instruction combiner. These passes
00093     /// will be inserted after each instance of the instruction combiner pass.
00094     EP_Peephole,
00095   };
00096 
00097   /// The Optimization Level - Specify the basic optimization level.
00098   ///    0 = -O0, 1 = -O1, 2 = -O2, 3 = -O3
00099   unsigned OptLevel;
00100 
00101   /// SizeLevel - How much we're optimizing for size.
00102   ///    0 = none, 1 = -Os, 2 = -Oz
00103   unsigned SizeLevel;
00104 
00105   /// LibraryInfo - Specifies information about the runtime library for the
00106   /// optimizer.  If this is non-null, it is added to both the function and
00107   /// per-module pass pipeline.
00108   TargetLibraryInfo *LibraryInfo;
00109 
00110   /// Inliner - Specifies the inliner to use.  If this is non-null, it is
00111   /// added to the per-module passes.
00112   Pass *Inliner;
00113 
00114   bool DisableTailCalls;
00115   bool DisableUnitAtATime;
00116   bool DisableUnrollLoops;
00117   bool BBVectorize;
00118   bool SLPVectorize;
00119   bool LoopVectorize;
00120   bool RerollLoops;
00121   bool LoadCombine;
00122   bool DisableGVNLoadPRE;
00123   bool VerifyInput;
00124   bool VerifyOutput;
00125   bool StripDebug;
00126   bool MergeFunctions;
00127 
00128 private:
00129   /// ExtensionList - This is list of all of the extensions that are registered.
00130   std::vector<std::pair<ExtensionPointTy, ExtensionFn> > Extensions;
00131 
00132 public:
00133   PassManagerBuilder();
00134   ~PassManagerBuilder();
00135   /// Adds an extension that will be used by all PassManagerBuilder instances.
00136   /// This is intended to be used by plugins, to register a set of
00137   /// optimisations to run automatically.
00138   static void addGlobalExtension(ExtensionPointTy Ty, ExtensionFn Fn);
00139   void addExtension(ExtensionPointTy Ty, ExtensionFn Fn);
00140 
00141 private:
00142   void addExtensionsToPM(ExtensionPointTy ETy, PassManagerBase &PM) const;
00143   void addInitialAliasAnalysisPasses(PassManagerBase &PM) const;
00144   void addLTOOptimizationPasses(PassManagerBase &PM);
00145 
00146 public:
00147   /// populateFunctionPassManager - This fills in the function pass manager,
00148   /// which is expected to be run on each function immediately as it is
00149   /// generated.  The idea is to reduce the size of the IR in memory.
00150   void populateFunctionPassManager(FunctionPassManager &FPM);
00151 
00152   /// populateModulePassManager - This sets up the primary pass manager.
00153   void populateModulePassManager(PassManagerBase &MPM);
00154   void populateLTOPassManager(PassManagerBase &PM, TargetMachine *TM = nullptr);
00155 };
00156 
00157 /// Registers a function for adding a standard set of passes.  This should be
00158 /// used by optimizer plugins to allow all front ends to transparently use
00159 /// them.  Create a static instance of this class in your plugin, providing a
00160 /// private function that the PassManagerBuilder can use to add your passes.
00161 struct RegisterStandardPasses {
00162   RegisterStandardPasses(PassManagerBuilder::ExtensionPointTy Ty,
00163                          PassManagerBuilder::ExtensionFn Fn) {
00164     PassManagerBuilder::addGlobalExtension(Ty, Fn);
00165   }
00166 };
00167 
00168 } // end namespace llvm
00169 #endif