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