1193323Sed//===- llvm/PassSupport.h - Pass Support code -------------------*- C++ -*-===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file defines stuff that is used to define and "use" Passes. This file 11193323Sed// is automatically #included by Pass.h, so: 12193323Sed// 13193323Sed// NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY 14193323Sed// 15193323Sed// Instead, #include Pass.h. 16193323Sed// 17193323Sed// This file defines Pass registration code and classes used for it. 18193323Sed// 19193323Sed//===----------------------------------------------------------------------===// 20193323Sed 21249423Sdim#ifndef LLVM_PASSSUPPORT_H 22249423Sdim#define LLVM_PASSSUPPORT_H 23193323Sed 24198396Srdivacky#include "Pass.h" 25249423Sdim#include "llvm/InitializePasses.h" 26212904Sdim#include "llvm/PassRegistry.h" 27218893Sdim#include "llvm/Support/Atomic.h" 28234353Sdim#include "llvm/Support/Valgrind.h" 29218893Sdim#include <vector> 30193323Sed 31193323Sednamespace llvm { 32193323Sed 33193323Sed//===--------------------------------------------------------------------------- 34193323Sed/// PassInfo class - An instance of this class exists for every pass known by 35193323Sed/// the system, and can be obtained from a live Pass by calling its 36193323Sed/// getPassInfo() method. These objects are set up by the RegisterPass<> 37193323Sed/// template, defined below. 38193323Sed/// 39193323Sedclass PassInfo { 40193323Sedpublic: 41193323Sed typedef Pass* (*NormalCtor_t)(); 42193323Sed 43193323Sedprivate: 44193323Sed const char *const PassName; // Nice name for Pass 45193323Sed const char *const PassArgument; // Command Line argument to run this pass 46212904Sdim const void *PassID; 47193323Sed const bool IsCFGOnlyPass; // Pass only looks at the CFG. 48193323Sed const bool IsAnalysis; // True if an analysis pass. 49193323Sed const bool IsAnalysisGroup; // True if an analysis group. 50193323Sed std::vector<const PassInfo*> ItfImpl;// Interfaces implemented by this pass 51193323Sed 52193323Sed NormalCtor_t NormalCtor; 53193323Sed 54193323Sedpublic: 55193323Sed /// PassInfo ctor - Do not call this directly, this should only be invoked 56193323Sed /// through RegisterPass. 57212904Sdim PassInfo(const char *name, const char *arg, const void *pi, 58212904Sdim NormalCtor_t normal, bool isCFGOnly, bool is_analysis) 59193323Sed : PassName(name), PassArgument(arg), PassID(pi), 60193323Sed IsCFGOnlyPass(isCFGOnly), 61218893Sdim IsAnalysis(is_analysis), IsAnalysisGroup(false), NormalCtor(normal) { } 62193323Sed /// PassInfo ctor - Do not call this directly, this should only be invoked 63193323Sed /// through RegisterPass. This version is for use by analysis groups; it 64193323Sed /// does not auto-register the pass. 65212904Sdim PassInfo(const char *name, const void *pi) 66193323Sed : PassName(name), PassArgument(""), PassID(pi), 67193323Sed IsCFGOnlyPass(false), 68218893Sdim IsAnalysis(false), IsAnalysisGroup(true), NormalCtor(0) { } 69193323Sed 70193323Sed /// getPassName - Return the friendly name for the pass, never returns null 71193323Sed /// 72193323Sed const char *getPassName() const { return PassName; } 73193323Sed 74193323Sed /// getPassArgument - Return the command line option that may be passed to 75193323Sed /// 'opt' that will cause this pass to be run. This will return null if there 76193323Sed /// is no argument. 77193323Sed /// 78193323Sed const char *getPassArgument() const { return PassArgument; } 79193323Sed 80193323Sed /// getTypeInfo - Return the id object for the pass... 81193323Sed /// TODO : Rename 82212904Sdim const void *getTypeInfo() const { return PassID; } 83193323Sed 84202878Srdivacky /// Return true if this PassID implements the specified ID pointer. 85212904Sdim bool isPassID(const void *IDPtr) const { 86212904Sdim return PassID == IDPtr; 87202878Srdivacky } 88202878Srdivacky 89193323Sed /// isAnalysisGroup - Return true if this is an analysis group, not a normal 90193323Sed /// pass. 91193323Sed /// 92193323Sed bool isAnalysisGroup() const { return IsAnalysisGroup; } 93193323Sed bool isAnalysis() const { return IsAnalysis; } 94193323Sed 95193323Sed /// isCFGOnlyPass - return true if this pass only looks at the CFG for the 96193323Sed /// function. 97193323Sed bool isCFGOnlyPass() const { return IsCFGOnlyPass; } 98193323Sed 99193323Sed /// getNormalCtor - Return a pointer to a function, that when called, creates 100193323Sed /// an instance of the pass and returns it. This pointer may be null if there 101193323Sed /// is no default constructor for the pass. 102193323Sed /// 103193323Sed NormalCtor_t getNormalCtor() const { 104193323Sed return NormalCtor; 105193323Sed } 106193323Sed void setNormalCtor(NormalCtor_t Ctor) { 107193323Sed NormalCtor = Ctor; 108193323Sed } 109193323Sed 110193323Sed /// createPass() - Use this method to create an instance of this pass. 111210299Sed Pass *createPass() const; 112193323Sed 113193323Sed /// addInterfaceImplemented - This method is called when this pass is 114193323Sed /// registered as a member of an analysis group with the RegisterAnalysisGroup 115193323Sed /// template. 116193323Sed /// 117193323Sed void addInterfaceImplemented(const PassInfo *ItfPI) { 118193323Sed ItfImpl.push_back(ItfPI); 119193323Sed } 120193323Sed 121193323Sed /// getInterfacesImplemented - Return a list of all of the analysis group 122193323Sed /// interfaces implemented by this pass. 123193323Sed /// 124193323Sed const std::vector<const PassInfo*> &getInterfacesImplemented() const { 125193323Sed return ItfImpl; 126193323Sed } 127193323Sed 128193323Sedprivate: 129243830Sdim void operator=(const PassInfo &) LLVM_DELETED_FUNCTION; 130243830Sdim PassInfo(const PassInfo &) LLVM_DELETED_FUNCTION; 131193323Sed}; 132193323Sed 133218893Sdim#define CALL_ONCE_INITIALIZATION(function) \ 134218893Sdim static volatile sys::cas_flag initialized = 0; \ 135218893Sdim sys::cas_flag old_val = sys::CompareAndSwap(&initialized, 1, 0); \ 136218893Sdim if (old_val == 0) { \ 137218893Sdim function(Registry); \ 138218893Sdim sys::MemoryFence(); \ 139234353Sdim TsanIgnoreWritesBegin(); \ 140234353Sdim TsanHappensBefore(&initialized); \ 141218893Sdim initialized = 2; \ 142234353Sdim TsanIgnoreWritesEnd(); \ 143218893Sdim } else { \ 144218893Sdim sys::cas_flag tmp = initialized; \ 145218893Sdim sys::MemoryFence(); \ 146218893Sdim while (tmp != 2) { \ 147218893Sdim tmp = initialized; \ 148218893Sdim sys::MemoryFence(); \ 149218893Sdim } \ 150234353Sdim } \ 151234353Sdim TsanHappensAfter(&initialized); 152218893Sdim 153212904Sdim#define INITIALIZE_PASS(passName, arg, name, cfg, analysis) \ 154218893Sdim static void* initialize##passName##PassOnce(PassRegistry &Registry) { \ 155218893Sdim PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \ 156218893Sdim PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \ 157218893Sdim Registry.registerPass(*PI, true); \ 158218893Sdim return PI; \ 159218893Sdim } \ 160218893Sdim void llvm::initialize##passName##Pass(PassRegistry &Registry) { \ 161218893Sdim CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \ 162218893Sdim } 163193323Sed 164218893Sdim#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \ 165218893Sdim static void* initialize##passName##PassOnce(PassRegistry &Registry) { 166218893Sdim 167218893Sdim#define INITIALIZE_PASS_DEPENDENCY(depName) \ 168218893Sdim initialize##depName##Pass(Registry); 169218893Sdim#define INITIALIZE_AG_DEPENDENCY(depName) \ 170218893Sdim initialize##depName##AnalysisGroup(Registry); 171218893Sdim 172218893Sdim#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis) \ 173218893Sdim PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \ 174218893Sdim PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \ 175218893Sdim Registry.registerPass(*PI, true); \ 176218893Sdim return PI; \ 177218893Sdim } \ 178218893Sdim void llvm::initialize##passName##Pass(PassRegistry &Registry) { \ 179218893Sdim CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \ 180218893Sdim } 181218893Sdim 182193323Sedtemplate<typename PassName> 183193323SedPass *callDefaultCtor() { return new PassName(); } 184193323Sed 185193323Sed//===--------------------------------------------------------------------------- 186193323Sed/// RegisterPass<t> template - This template class is used to notify the system 187193323Sed/// that a Pass is available for use, and registers it into the internal 188193323Sed/// database maintained by the PassManager. Unless this template is used, opt, 189193323Sed/// for example will not be able to see the pass and attempts to create the pass 190193323Sed/// will fail. This template is used in the follow manner (at global scope, in 191193323Sed/// your .cpp file): 192193323Sed/// 193193323Sed/// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name"); 194193323Sed/// 195193323Sed/// This statement will cause your pass to be created by calling the default 196193323Sed/// constructor exposed by the pass. If you have a different constructor that 197193323Sed/// must be called, create a global constructor function (which takes the 198193323Sed/// arguments you need and returns a Pass*) and register your pass like this: 199193323Sed/// 200193323Sed/// static RegisterPass<PassClassName> tmp("passopt", "My Name"); 201193323Sed/// 202193323Sedtemplate<typename passName> 203193323Sedstruct RegisterPass : public PassInfo { 204193323Sed 205193323Sed // Register Pass using default constructor... 206193323Sed RegisterPass(const char *PassArg, const char *Name, bool CFGOnly = false, 207193323Sed bool is_analysis = false) 208212904Sdim : PassInfo(Name, PassArg, &passName::ID, 209193323Sed PassInfo::NormalCtor_t(callDefaultCtor<passName>), 210193323Sed CFGOnly, is_analysis) { 211218893Sdim PassRegistry::getPassRegistry()->registerPass(*this); 212193323Sed } 213193323Sed}; 214193323Sed 215193323Sed 216193323Sed/// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_. 217193323Sed/// Analysis groups are used to define an interface (which need not derive from 218193323Sed/// Pass) that is required by passes to do their job. Analysis Groups differ 219193323Sed/// from normal analyses because any available implementation of the group will 220193323Sed/// be used if it is available. 221193323Sed/// 222193323Sed/// If no analysis implementing the interface is available, a default 223193323Sed/// implementation is created and added. A pass registers itself as the default 224193323Sed/// implementation by specifying 'true' as the second template argument of this 225193323Sed/// class. 226193323Sed/// 227193323Sed/// In addition to registering itself as an analysis group member, a pass must 228193323Sed/// register itself normally as well. Passes may be members of multiple groups 229193323Sed/// and may still be "required" specifically by name. 230193323Sed/// 231193323Sed/// The actual interface may also be registered as well (by not specifying the 232193323Sed/// second template argument). The interface should be registered to associate 233193323Sed/// a nice name with the interface. 234193323Sed/// 235193323Sedclass RegisterAGBase : public PassInfo { 236218893Sdimpublic: 237198090Srdivacky RegisterAGBase(const char *Name, 238212904Sdim const void *InterfaceID, 239212904Sdim const void *PassID = 0, 240198090Srdivacky bool isDefault = false); 241193323Sed}; 242193323Sed 243193323Sedtemplate<typename Interface, bool Default = false> 244193323Sedstruct RegisterAnalysisGroup : public RegisterAGBase { 245193323Sed explicit RegisterAnalysisGroup(PassInfo &RPB) 246193323Sed : RegisterAGBase(RPB.getPassName(), 247212904Sdim &Interface::ID, RPB.getTypeInfo(), 248193323Sed Default) { 249193323Sed } 250193323Sed 251193323Sed explicit RegisterAnalysisGroup(const char *Name) 252212904Sdim : RegisterAGBase(Name, &Interface::ID) { 253193323Sed } 254193323Sed}; 255193323Sed 256218893Sdim#define INITIALIZE_ANALYSIS_GROUP(agName, name, defaultPass) \ 257218893Sdim static void* initialize##agName##AnalysisGroupOnce(PassRegistry &Registry) { \ 258218893Sdim initialize##defaultPass##Pass(Registry); \ 259218893Sdim PassInfo *AI = new PassInfo(name, & agName :: ID); \ 260218893Sdim Registry.registerAnalysisGroup(& agName ::ID, 0, *AI, false, true); \ 261218893Sdim return AI; \ 262218893Sdim } \ 263218893Sdim void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) { \ 264218893Sdim CALL_ONCE_INITIALIZATION(initialize##agName##AnalysisGroupOnce) \ 265218893Sdim } 266218893Sdim 267218893Sdim 268212904Sdim#define INITIALIZE_AG_PASS(passName, agName, arg, name, cfg, analysis, def) \ 269218893Sdim static void* initialize##passName##PassOnce(PassRegistry &Registry) { \ 270218893Sdim if (!def) initialize##agName##AnalysisGroup(Registry); \ 271218893Sdim PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \ 272218893Sdim PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \ 273218893Sdim Registry.registerPass(*PI, true); \ 274218893Sdim \ 275218893Sdim PassInfo *AI = new PassInfo(name, & agName :: ID); \ 276218893Sdim Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \ 277218893Sdim *AI, def, true); \ 278218893Sdim return AI; \ 279218893Sdim } \ 280218893Sdim void llvm::initialize##passName##Pass(PassRegistry &Registry) { \ 281218893Sdim CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \ 282218893Sdim } 283193323Sed 284218893Sdim 285218893Sdim#define INITIALIZE_AG_PASS_BEGIN(passName, agName, arg, n, cfg, analysis, def) \ 286218893Sdim static void* initialize##passName##PassOnce(PassRegistry &Registry) { \ 287218893Sdim if (!def) initialize##agName##AnalysisGroup(Registry); 288218893Sdim 289218893Sdim#define INITIALIZE_AG_PASS_END(passName, agName, arg, n, cfg, analysis, def) \ 290218893Sdim PassInfo *PI = new PassInfo(n, arg, & passName ::ID, \ 291218893Sdim PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \ 292218893Sdim Registry.registerPass(*PI, true); \ 293218893Sdim \ 294218893Sdim PassInfo *AI = new PassInfo(n, & agName :: ID); \ 295218893Sdim Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \ 296218893Sdim *AI, def, true); \ 297218893Sdim return AI; \ 298218893Sdim } \ 299218893Sdim void llvm::initialize##passName##Pass(PassRegistry &Registry) { \ 300218893Sdim CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \ 301218893Sdim } 302218893Sdim 303193323Sed//===--------------------------------------------------------------------------- 304193323Sed/// PassRegistrationListener class - This class is meant to be derived from by 305193323Sed/// clients that are interested in which passes get registered and unregistered 306193323Sed/// at runtime (which can be because of the RegisterPass constructors being run 307193323Sed/// as the program starts up, or may be because a shared object just got 308249423Sdim/// loaded). Deriving from the PassRegistrationListener class automatically 309193323Sed/// registers your object to receive callbacks indicating when passes are loaded 310193323Sed/// and removed. 311193323Sed/// 312193323Sedstruct PassRegistrationListener { 313193323Sed 314193323Sed /// PassRegistrationListener ctor - Add the current object to the list of 315193323Sed /// PassRegistrationListeners... 316193323Sed PassRegistrationListener(); 317193323Sed 318193323Sed /// dtor - Remove object from list of listeners... 319193323Sed /// 320193323Sed virtual ~PassRegistrationListener(); 321193323Sed 322193323Sed /// Callback functions - These functions are invoked whenever a pass is loaded 323193323Sed /// or removed from the current executable. 324193323Sed /// 325193323Sed virtual void passRegistered(const PassInfo *) {} 326193323Sed 327193323Sed /// enumeratePasses - Iterate over the registered passes, calling the 328193323Sed /// passEnumerate callback on each PassInfo object. 329193323Sed /// 330193323Sed void enumeratePasses(); 331193323Sed 332193323Sed /// passEnumerate - Callback function invoked when someone calls 333193323Sed /// enumeratePasses on this PassRegistrationListener object. 334193323Sed /// 335193323Sed virtual void passEnumerate(const PassInfo *) {} 336193323Sed}; 337193323Sed 338193323Sed 339193323Sed} // End llvm namespace 340193323Sed 341193323Sed#endif 342