PassManagerBuilder.h revision 243830
1226584Sdim// llvm/Transforms/IPO/PassManagerBuilder.h - Build Standard Pass -*- C++ -*-=// 2226584Sdim// 3226584Sdim// The LLVM Compiler Infrastructure 4226584Sdim// 5226584Sdim// This file is distributed under the University of Illinois Open Source 6226584Sdim// License. See LICENSE.TXT for details. 7226584Sdim// 8226584Sdim//===----------------------------------------------------------------------===// 9226584Sdim// 10226584Sdim// This file defines the PassManagerBuilder class, which is used to set up a 11226584Sdim// "standard" optimization sequence suitable for languages like C and C++. 12226584Sdim// 13226584Sdim//===----------------------------------------------------------------------===// 14226584Sdim 15226584Sdim#ifndef LLVM_SUPPORT_PASSMANAGERBUILDER_H 16226584Sdim#define LLVM_SUPPORT_PASSMANAGERBUILDER_H 17226584Sdim 18226584Sdim#include <vector> 19226584Sdim 20226584Sdimnamespace llvm { 21226584Sdim class TargetLibraryInfo; 22226584Sdim class PassManagerBase; 23226584Sdim class Pass; 24226584Sdim class FunctionPassManager; 25226584Sdim 26226584Sdim/// PassManagerBuilder - This class is used to set up a standard optimization 27226584Sdim/// sequence for languages like C and C++, allowing some APIs to customize the 28226584Sdim/// pass sequence in various ways. A simple example of using it would be: 29226584Sdim/// 30226584Sdim/// PassManagerBuilder Builder; 31226584Sdim/// Builder.OptLevel = 2; 32226584Sdim/// Builder.populateFunctionPassManager(FPM); 33226584Sdim/// Builder.populateModulePassManager(MPM); 34226584Sdim/// 35226584Sdim/// In addition to setting up the basic passes, PassManagerBuilder allows 36226584Sdim/// frontends to vend a plugin API, where plugins are allowed to add extensions 37226584Sdim/// to the default pass manager. They do this by specifying where in the pass 38226584Sdim/// pipeline they want to be added, along with a callback function that adds 39226584Sdim/// the pass(es). For example, a plugin that wanted to add a loop optimization 40226584Sdim/// could do something like this: 41226584Sdim/// 42226584Sdim/// static void addMyLoopPass(const PMBuilder &Builder, PassManagerBase &PM) { 43226584Sdim/// if (Builder.getOptLevel() > 2 && Builder.getOptSizeLevel() == 0) 44226584Sdim/// PM.add(createMyAwesomePass()); 45226584Sdim/// } 46226584Sdim/// ... 47226584Sdim/// Builder.addExtension(PassManagerBuilder::EP_LoopOptimizerEnd, 48226584Sdim/// addMyLoopPass); 49226584Sdim/// ... 50226584Sdimclass PassManagerBuilder { 51226584Sdimpublic: 52226584Sdim 53226584Sdim /// Extensions are passed the builder itself (so they can see how it is 54226584Sdim /// configured) as well as the pass manager to add stuff to. 55226584Sdim typedef void (*ExtensionFn)(const PassManagerBuilder &Builder, 56226584Sdim PassManagerBase &PM); 57226584Sdim enum ExtensionPointTy { 58226584Sdim /// EP_EarlyAsPossible - This extension point allows adding passes before 59226584Sdim /// any other transformations, allowing them to see the code as it is coming 60226584Sdim /// out of the frontend. 61226584Sdim EP_EarlyAsPossible, 62226584Sdim 63234353Sdim /// EP_ModuleOptimizerEarly - This extension point allows adding passes 64234353Sdim /// just before the main module-level optimization passes. 65234353Sdim EP_ModuleOptimizerEarly, 66234353Sdim 67226584Sdim /// EP_LoopOptimizerEnd - This extension point allows adding loop passes to 68226584Sdim /// the end of the loop optimizer. 69226584Sdim EP_LoopOptimizerEnd, 70226584Sdim 71226584Sdim /// EP_ScalarOptimizerLate - This extension point allows adding optimization 72226584Sdim /// passes after most of the main optimizations, but before the last 73226584Sdim /// cleanup-ish optimizations. 74234353Sdim EP_ScalarOptimizerLate, 75234353Sdim 76234353Sdim /// EP_OptimizerLast -- This extension point allows adding passes that 77234353Sdim /// run after everything else. 78234353Sdim EP_OptimizerLast, 79234353Sdim 80234353Sdim /// EP_EnabledOnOptLevel0 - This extension point allows adding passes that 81234353Sdim /// should not be disabled by O0 optimization level. The passes will be 82234353Sdim /// inserted after the inlining pass. 83234353Sdim EP_EnabledOnOptLevel0 84226584Sdim }; 85226584Sdim 86226584Sdim /// The Optimization Level - Specify the basic optimization level. 87226584Sdim /// 0 = -O0, 1 = -O1, 2 = -O2, 3 = -O3 88226584Sdim unsigned OptLevel; 89226584Sdim 90226584Sdim /// SizeLevel - How much we're optimizing for size. 91226584Sdim /// 0 = none, 1 = -Os, 2 = -Oz 92226584Sdim unsigned SizeLevel; 93226584Sdim 94226584Sdim /// LibraryInfo - Specifies information about the runtime library for the 95226584Sdim /// optimizer. If this is non-null, it is added to both the function and 96226584Sdim /// per-module pass pipeline. 97226584Sdim TargetLibraryInfo *LibraryInfo; 98226584Sdim 99226584Sdim /// Inliner - Specifies the inliner to use. If this is non-null, it is 100226584Sdim /// added to the per-module passes. 101226584Sdim Pass *Inliner; 102226584Sdim 103226584Sdim bool DisableSimplifyLibCalls; 104226584Sdim bool DisableUnitAtATime; 105226584Sdim bool DisableUnrollLoops; 106234353Sdim bool Vectorize; 107243830Sdim bool LoopVectorize; 108226584Sdim 109226584Sdimprivate: 110226584Sdim /// ExtensionList - This is list of all of the extensions that are registered. 111226584Sdim std::vector<std::pair<ExtensionPointTy, ExtensionFn> > Extensions; 112226584Sdim 113226584Sdimpublic: 114226584Sdim PassManagerBuilder(); 115226584Sdim ~PassManagerBuilder(); 116226584Sdim /// Adds an extension that will be used by all PassManagerBuilder instances. 117226584Sdim /// This is intended to be used by plugins, to register a set of 118226584Sdim /// optimisations to run automatically. 119226584Sdim static void addGlobalExtension(ExtensionPointTy Ty, ExtensionFn Fn); 120226584Sdim void addExtension(ExtensionPointTy Ty, ExtensionFn Fn); 121226584Sdim 122226584Sdimprivate: 123226584Sdim void addExtensionsToPM(ExtensionPointTy ETy, PassManagerBase &PM) const; 124226584Sdim void addInitialAliasAnalysisPasses(PassManagerBase &PM) const; 125226584Sdimpublic: 126226584Sdim 127226584Sdim /// populateFunctionPassManager - This fills in the function pass manager, 128226584Sdim /// which is expected to be run on each function immediately as it is 129226584Sdim /// generated. The idea is to reduce the size of the IR in memory. 130226584Sdim void populateFunctionPassManager(FunctionPassManager &FPM); 131226584Sdim 132226584Sdim /// populateModulePassManager - This sets up the primary pass manager. 133226584Sdim void populateModulePassManager(PassManagerBase &MPM); 134226584Sdim void populateLTOPassManager(PassManagerBase &PM, bool Internalize, 135234353Sdim bool RunInliner, bool DisableGVNLoadPRE = false); 136226584Sdim}; 137234353Sdim 138226584Sdim/// Registers a function for adding a standard set of passes. This should be 139226584Sdim/// used by optimizer plugins to allow all front ends to transparently use 140226584Sdim/// them. Create a static instance of this class in your plugin, providing a 141226584Sdim/// private function that the PassManagerBuilder can use to add your passes. 142226584Sdimstruct RegisterStandardPasses { 143226584Sdim RegisterStandardPasses(PassManagerBuilder::ExtensionPointTy Ty, 144226584Sdim PassManagerBuilder::ExtensionFn Fn) { 145226584Sdim PassManagerBuilder::addGlobalExtension(Ty, Fn); 146226584Sdim } 147226584Sdim}; 148234353Sdim 149226584Sdim} // end namespace llvm 150226584Sdim#endif 151