Home | History | Annotate | Download | only in IPO
      1 // llvm/Transforms/IPO/PassManagerBuilder.h - Build Standard Pass -*- 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 the PassManagerBuilder class, which is used to set up a
     11 // "standard" optimization sequence suitable for languages like C and C++.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_TRANSFORMS_IPO_PASSMANAGERBUILDER_H
     16 #define LLVM_TRANSFORMS_IPO_PASSMANAGERBUILDER_H
     17 
     18 #include <vector>
     19 
     20 namespace llvm {
     21   class TargetLibraryInfo;
     22   class PassManagerBase;
     23   class Pass;
     24   class FunctionPassManager;
     25 
     26 /// PassManagerBuilder - This class is used to set up a standard optimization
     27 /// sequence for languages like C and C++, allowing some APIs to customize the
     28 /// pass sequence in various ways. A simple example of using it would be:
     29 ///
     30 ///  PassManagerBuilder Builder;
     31 ///  Builder.OptLevel = 2;
     32 ///  Builder.populateFunctionPassManager(FPM);
     33 ///  Builder.populateModulePassManager(MPM);
     34 ///
     35 /// In addition to setting up the basic passes, PassManagerBuilder allows
     36 /// frontends to vend a plugin API, where plugins are allowed to add extensions
     37 /// to the default pass manager.  They do this by specifying where in the pass
     38 /// pipeline they want to be added, along with a callback function that adds
     39 /// the pass(es).  For example, a plugin that wanted to add a loop optimization
     40 /// could do something like this:
     41 ///
     42 /// static void addMyLoopPass(const PMBuilder &Builder, PassManagerBase &PM) {
     43 ///   if (Builder.getOptLevel() > 2 && Builder.getOptSizeLevel() == 0)
     44 ///     PM.add(createMyAwesomePass());
     45 /// }
     46 ///   ...
     47 ///   Builder.addExtension(PassManagerBuilder::EP_LoopOptimizerEnd,
     48 ///                        addMyLoopPass);
     49 ///   ...
     50 class PassManagerBuilder {
     51 public:
     52 
     53   /// Extensions are passed the builder itself (so they can see how it is
     54   /// configured) as well as the pass manager to add stuff to.
     55   typedef void (*ExtensionFn)(const PassManagerBuilder &Builder,
     56                               PassManagerBase &PM);
     57   enum ExtensionPointTy {
     58     /// EP_EarlyAsPossible - This extension point allows adding passes before
     59     /// any other transformations, allowing them to see the code as it is coming
     60     /// out of the frontend.
     61     EP_EarlyAsPossible,
     62 
     63     /// EP_ModuleOptimizerEarly - This extension point allows adding passes
     64     /// just before the main module-level optimization passes.
     65     EP_ModuleOptimizerEarly,
     66 
     67     /// EP_LoopOptimizerEnd - This extension point allows adding loop passes to
     68     /// the end of the loop optimizer.
     69     EP_LoopOptimizerEnd,
     70 
     71     /// EP_ScalarOptimizerLate - This extension point allows adding optimization
     72     /// passes after most of the main optimizations, but before the last
     73     /// cleanup-ish optimizations.
     74     EP_ScalarOptimizerLate,
     75 
     76     /// EP_OptimizerLast -- This extension point allows adding passes that
     77     /// run after everything else.
     78     EP_OptimizerLast,
     79 
     80     /// EP_EnabledOnOptLevel0 - This extension point allows adding passes that
     81     /// should not be disabled by O0 optimization level. The passes will be
     82     /// inserted after the inlining pass.
     83     EP_EnabledOnOptLevel0
     84   };
     85 
     86   /// The Optimization Level - Specify the basic optimization level.
     87   ///    0 = -O0, 1 = -O1, 2 = -O2, 3 = -O3
     88   unsigned OptLevel;
     89 
     90   /// SizeLevel - How much we're optimizing for size.
     91   ///    0 = none, 1 = -Os, 2 = -Oz
     92   unsigned SizeLevel;
     93 
     94   /// LibraryInfo - Specifies information about the runtime library for the
     95   /// optimizer.  If this is non-null, it is added to both the function and
     96   /// per-module pass pipeline.
     97   TargetLibraryInfo *LibraryInfo;
     98 
     99   /// Inliner - Specifies the inliner to use.  If this is non-null, it is
    100   /// added to the per-module passes.
    101   Pass *Inliner;
    102 
    103   bool DisableUnitAtATime;
    104   bool DisableUnrollLoops;
    105   bool BBVectorize;
    106   bool SLPVectorize;
    107   bool LoopVectorize;
    108   bool LateVectorize;
    109 
    110 private:
    111   /// ExtensionList - This is list of all of the extensions that are registered.
    112   std::vector<std::pair<ExtensionPointTy, ExtensionFn> > Extensions;
    113 
    114 public:
    115   PassManagerBuilder();
    116   ~PassManagerBuilder();
    117   /// Adds an extension that will be used by all PassManagerBuilder instances.
    118   /// This is intended to be used by plugins, to register a set of
    119   /// optimisations to run automatically.
    120   static void addGlobalExtension(ExtensionPointTy Ty, ExtensionFn Fn);
    121   void addExtension(ExtensionPointTy Ty, ExtensionFn Fn);
    122 
    123 private:
    124   void addExtensionsToPM(ExtensionPointTy ETy, PassManagerBase &PM) const;
    125   void addInitialAliasAnalysisPasses(PassManagerBase &PM) const;
    126 public:
    127 
    128   /// populateFunctionPassManager - This fills in the function pass manager,
    129   /// which is expected to be run on each function immediately as it is
    130   /// generated.  The idea is to reduce the size of the IR in memory.
    131   void populateFunctionPassManager(FunctionPassManager &FPM);
    132 
    133   /// populateModulePassManager - This sets up the primary pass manager.
    134   void populateModulePassManager(PassManagerBase &MPM);
    135   void populateLTOPassManager(PassManagerBase &PM, bool Internalize,
    136                               bool RunInliner, bool DisableGVNLoadPRE = false);
    137 };
    138 
    139 /// Registers a function for adding a standard set of passes.  This should be
    140 /// used by optimizer plugins to allow all front ends to transparently use
    141 /// them.  Create a static instance of this class in your plugin, providing a
    142 /// private function that the PassManagerBuilder can use to add your passes.
    143 struct RegisterStandardPasses {
    144   RegisterStandardPasses(PassManagerBuilder::ExtensionPointTy Ty,
    145                          PassManagerBuilder::ExtensionFn Fn) {
    146     PassManagerBuilder::addGlobalExtension(Ty, Fn);
    147   }
    148 };
    149 
    150 } // end namespace llvm
    151 #endif
    152