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 <functional>
     19 #include <memory>
     20 #include <string>
     21 #include <vector>
     22 
     23 namespace llvm {
     24 class ModuleSummaryIndex;
     25 class Pass;
     26 class TargetLibraryInfoImpl;
     27 class TargetMachine;
     28 
     29 // The old pass manager infrastructure is hidden in a legacy namespace now.
     30 namespace legacy {
     31 class FunctionPassManager;
     32 class PassManagerBase;
     33 }
     34 
     35 /// PassManagerBuilder - This class is used to set up a standard optimization
     36 /// sequence for languages like C and C++, allowing some APIs to customize the
     37 /// pass sequence in various ways. A simple example of using it would be:
     38 ///
     39 ///  PassManagerBuilder Builder;
     40 ///  Builder.OptLevel = 2;
     41 ///  Builder.populateFunctionPassManager(FPM);
     42 ///  Builder.populateModulePassManager(MPM);
     43 ///
     44 /// In addition to setting up the basic passes, PassManagerBuilder allows
     45 /// frontends to vend a plugin API, where plugins are allowed to add extensions
     46 /// to the default pass manager.  They do this by specifying where in the pass
     47 /// pipeline they want to be added, along with a callback function that adds
     48 /// the pass(es).  For example, a plugin that wanted to add a loop optimization
     49 /// could do something like this:
     50 ///
     51 /// static void addMyLoopPass(const PMBuilder &Builder, PassManagerBase &PM) {
     52 ///   if (Builder.getOptLevel() > 2 && Builder.getOptSizeLevel() == 0)
     53 ///     PM.add(createMyAwesomePass());
     54 /// }
     55 ///   ...
     56 ///   Builder.addExtension(PassManagerBuilder::EP_LoopOptimizerEnd,
     57 ///                        addMyLoopPass);
     58 ///   ...
     59 class PassManagerBuilder {
     60 public:
     61   /// Extensions are passed the builder itself (so they can see how it is
     62   /// configured) as well as the pass manager to add stuff to.
     63   typedef std::function<void(const PassManagerBuilder &Builder,
     64                              legacy::PassManagerBase &PM)>
     65       ExtensionFn;
     66   enum ExtensionPointTy {
     67     /// EP_EarlyAsPossible - This extension point allows adding passes before
     68     /// any other transformations, allowing them to see the code as it is coming
     69     /// out of the frontend.
     70     EP_EarlyAsPossible,
     71 
     72     /// EP_ModuleOptimizerEarly - This extension point allows adding passes
     73     /// just before the main module-level optimization passes.
     74     EP_ModuleOptimizerEarly,
     75 
     76     /// EP_LoopOptimizerEnd - This extension point allows adding loop passes to
     77     /// the end of the loop optimizer.
     78     EP_LoopOptimizerEnd,
     79 
     80     /// EP_ScalarOptimizerLate - This extension point allows adding optimization
     81     /// passes after most of the main optimizations, but before the last
     82     /// cleanup-ish optimizations.
     83     EP_ScalarOptimizerLate,
     84 
     85     /// EP_OptimizerLast -- This extension point allows adding passes that
     86     /// run after everything else.
     87     EP_OptimizerLast,
     88 
     89     /// EP_VectorizerStart - This extension point allows adding optimization
     90     /// passes before the vectorizer and other highly target specific
     91     /// optimization passes are executed.
     92     EP_VectorizerStart,
     93 
     94     /// EP_EnabledOnOptLevel0 - This extension point allows adding passes that
     95     /// should not be disabled by O0 optimization level. The passes will be
     96     /// inserted after the inlining pass.
     97     EP_EnabledOnOptLevel0,
     98 
     99     /// EP_Peephole - This extension point allows adding passes that perform
    100     /// peephole optimizations similar to the instruction combiner. These passes
    101     /// will be inserted after each instance of the instruction combiner pass.
    102     EP_Peephole,
    103 
    104     /// EP_LateLoopOptimizations - This extension point allows adding late loop
    105     /// canonicalization and simplification passes. This is the last point in
    106     /// the loop optimization pipeline before loop deletion. Each pass added
    107     /// here must be an instance of LoopPass.
    108     /// This is the place to add passes that can remove loops, such as target-
    109     /// specific loop idiom recognition.
    110     EP_LateLoopOptimizations,
    111 
    112     /// EP_CGSCCOptimizerLate - This extension point allows adding CallGraphSCC
    113     /// passes at the end of the main CallGraphSCC passes and before any
    114     /// function simplification passes run by CGPassManager.
    115     EP_CGSCCOptimizerLate,
    116   };
    117 
    118   /// The Optimization Level - Specify the basic optimization level.
    119   ///    0 = -O0, 1 = -O1, 2 = -O2, 3 = -O3
    120   unsigned OptLevel;
    121 
    122   /// SizeLevel - How much we're optimizing for size.
    123   ///    0 = none, 1 = -Os, 2 = -Oz
    124   unsigned SizeLevel;
    125 
    126   /// LibraryInfo - Specifies information about the runtime library for the
    127   /// optimizer.  If this is non-null, it is added to both the function and
    128   /// per-module pass pipeline.
    129   TargetLibraryInfoImpl *LibraryInfo;
    130 
    131   /// Inliner - Specifies the inliner to use.  If this is non-null, it is
    132   /// added to the per-module passes.
    133   Pass *Inliner;
    134 
    135   /// The module summary index to use for exporting information from the
    136   /// regular LTO phase, for example for the CFI and devirtualization type
    137   /// tests.
    138   ModuleSummaryIndex *ExportSummary = nullptr;
    139 
    140   /// The module summary index to use for importing information to the
    141   /// thin LTO backends, for example for the CFI and devirtualization type
    142   /// tests.
    143   const ModuleSummaryIndex *ImportSummary = nullptr;
    144 
    145   bool DisableTailCalls;
    146   bool DisableUnitAtATime;
    147   bool DisableUnrollLoops;
    148   bool BBVectorize;
    149   bool SLPVectorize;
    150   bool LoopVectorize;
    151   bool RerollLoops;
    152   bool LoadCombine;
    153   bool NewGVN;
    154   bool DisableGVNLoadPRE;
    155   bool VerifyInput;
    156   bool VerifyOutput;
    157   bool MergeFunctions;
    158   bool PrepareForLTO;
    159   bool PrepareForThinLTO;
    160   bool PerformThinLTO;
    161   bool DivergentTarget;
    162 
    163   /// Enable profile instrumentation pass.
    164   bool EnablePGOInstrGen;
    165   /// Profile data file name that the instrumentation will be written to.
    166   std::string PGOInstrGen;
    167   /// Path of the profile data file.
    168   std::string PGOInstrUse;
    169   /// Path of the sample Profile data file.
    170   std::string PGOSampleUse;
    171 
    172 private:
    173   /// ExtensionList - This is list of all of the extensions that are registered.
    174   std::vector<std::pair<ExtensionPointTy, ExtensionFn>> Extensions;
    175 
    176 public:
    177   PassManagerBuilder();
    178   ~PassManagerBuilder();
    179   /// Adds an extension that will be used by all PassManagerBuilder instances.
    180   /// This is intended to be used by plugins, to register a set of
    181   /// optimisations to run automatically.
    182   static void addGlobalExtension(ExtensionPointTy Ty, ExtensionFn Fn);
    183   void addExtension(ExtensionPointTy Ty, ExtensionFn Fn);
    184 
    185 private:
    186   void addExtensionsToPM(ExtensionPointTy ETy,
    187                          legacy::PassManagerBase &PM) const;
    188   void addInitialAliasAnalysisPasses(legacy::PassManagerBase &PM) const;
    189   void addLTOOptimizationPasses(legacy::PassManagerBase &PM);
    190   void addLateLTOOptimizationPasses(legacy::PassManagerBase &PM);
    191   void addPGOInstrPasses(legacy::PassManagerBase &MPM);
    192   void addFunctionSimplificationPasses(legacy::PassManagerBase &MPM);
    193   void addInstructionCombiningPass(legacy::PassManagerBase &MPM) const;
    194 
    195 public:
    196   /// populateFunctionPassManager - This fills in the function pass manager,
    197   /// which is expected to be run on each function immediately as it is
    198   /// generated.  The idea is to reduce the size of the IR in memory.
    199   void populateFunctionPassManager(legacy::FunctionPassManager &FPM);
    200 
    201   /// populateModulePassManager - This sets up the primary pass manager.
    202   void populateModulePassManager(legacy::PassManagerBase &MPM);
    203   void populateLTOPassManager(legacy::PassManagerBase &PM);
    204   void populateThinLTOPassManager(legacy::PassManagerBase &PM);
    205 };
    206 
    207 /// Registers a function for adding a standard set of passes.  This should be
    208 /// used by optimizer plugins to allow all front ends to transparently use
    209 /// them.  Create a static instance of this class in your plugin, providing a
    210 /// private function that the PassManagerBuilder can use to add your passes.
    211 struct RegisterStandardPasses {
    212   RegisterStandardPasses(PassManagerBuilder::ExtensionPointTy Ty,
    213                          PassManagerBuilder::ExtensionFn Fn) {
    214     PassManagerBuilder::addGlobalExtension(Ty, std::move(Fn));
    215   }
    216 };
    217 
    218 } // end namespace llvm
    219 #endif
    220