1 //===- llvm/PassSupport.h - Pass Support code -------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines stuff that is used to define and "use" Passes. This file 10 // is automatically #included by Pass.h, so: 11 // 12 // NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY 13 // 14 // Instead, #include Pass.h. 15 // 16 // This file defines Pass registration code and classes used for it. 17 // 18 //===----------------------------------------------------------------------===// 19 20 #if !defined(LLVM_PASS_H) || defined(LLVM_PASSSUPPORT_H) 21 #error "Do not include <PassSupport.h>; include <Pass.h> instead" 22 #endif 23 24 #ifndef LLVM_PASSSUPPORT_H 25 #define LLVM_PASSSUPPORT_H 26 27 #include "llvm/ADT/StringRef.h" 28 #include "llvm/PassInfo.h" 29 #include "llvm/PassRegistry.h" 30 #include "llvm/Support/Error.h" 31 #include "llvm/Support/Threading.h" 32 #include <functional> 33 34 namespace llvm { 35 36 class Pass; 37 38 #define INITIALIZE_PASS(passName, arg, name, cfg, analysis) \ 39 static void *initialize##passName##PassOnce(PassRegistry &Registry) { \ 40 PassInfo *PI = new PassInfo( \ 41 name, arg, &passName::ID, \ 42 PassInfo::NormalCtor_t(callDefaultCtor<passName>), cfg, analysis); \ 43 Registry.registerPass(*PI, true); \ 44 return PI; \ 45 } \ 46 static llvm::once_flag Initialize##passName##PassFlag; \ 47 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \ 48 llvm::call_once(Initialize##passName##PassFlag, \ 49 initialize##passName##PassOnce, std::ref(Registry)); \ 50 } 51 52 #define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \ 53 static void *initialize##passName##PassOnce(PassRegistry &Registry) { 54 55 #define INITIALIZE_PASS_DEPENDENCY(depName) initialize##depName##Pass(Registry); 56 57 #define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis) \ 58 PassInfo *PI = new PassInfo( \ 59 name, arg, &passName::ID, \ 60 PassInfo::NormalCtor_t(callDefaultCtor<passName>), cfg, analysis); \ 61 Registry.registerPass(*PI, true); \ 62 return PI; \ 63 } \ 64 static llvm::once_flag Initialize##passName##PassFlag; \ 65 void llvm::initialize##passName##Pass(PassRegistry &Registry) { \ 66 llvm::call_once(Initialize##passName##PassFlag, \ 67 initialize##passName##PassOnce, std::ref(Registry)); \ 68 } 69 70 #define INITIALIZE_PASS_WITH_OPTIONS(PassName, Arg, Name, Cfg, Analysis) \ 71 INITIALIZE_PASS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \ 72 PassName::registerOptions(); \ 73 INITIALIZE_PASS_END(PassName, Arg, Name, Cfg, Analysis) 74 75 #define INITIALIZE_PASS_WITH_OPTIONS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \ 76 INITIALIZE_PASS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \ 77 PassName::registerOptions(); 78 79 template < 80 class PassName, 81 std::enable_if_t<std::is_default_constructible<PassName>{}, bool> = true> 82 Pass *callDefaultCtor() { 83 return new PassName(); 84 } 85 86 template < 87 class PassName, 88 std::enable_if_t<!std::is_default_constructible<PassName>{}, bool> = true> 89 Pass *callDefaultCtor() { 90 // Some codegen passes should only be testable via 91 // `llc -{start|stop}-{before|after}=<passname>`, not via `opt -<passname>`. 92 report_fatal_error("target-specific codegen-only pass"); 93 } 94 95 //===--------------------------------------------------------------------------- 96 /// RegisterPass<t> template - This template class is used to notify the system 97 /// that a Pass is available for use, and registers it into the internal 98 /// database maintained by the PassManager. Unless this template is used, opt, 99 /// for example will not be able to see the pass and attempts to create the pass 100 /// will fail. This template is used in the follow manner (at global scope, in 101 /// your .cpp file): 102 /// 103 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name"); 104 /// 105 /// This statement will cause your pass to be created by calling the default 106 /// constructor exposed by the pass. 107 template <typename passName> struct RegisterPass : public PassInfo { 108 // Register Pass using default constructor... 109 RegisterPass(StringRef PassArg, StringRef Name, bool CFGOnly = false, 110 bool is_analysis = false) 111 : PassInfo(Name, PassArg, &passName::ID, 112 PassInfo::NormalCtor_t(callDefaultCtor<passName>), CFGOnly, 113 is_analysis) { 114 PassRegistry::getPassRegistry()->registerPass(*this); 115 } 116 }; 117 118 //===--------------------------------------------------------------------------- 119 /// PassRegistrationListener class - This class is meant to be derived from by 120 /// clients that are interested in which passes get registered and unregistered 121 /// at runtime (which can be because of the RegisterPass constructors being run 122 /// as the program starts up, or may be because a shared object just got 123 /// loaded). 124 struct PassRegistrationListener { 125 PassRegistrationListener() = default; 126 virtual ~PassRegistrationListener() = default; 127 128 /// Callback functions - These functions are invoked whenever a pass is loaded 129 /// or removed from the current executable. 130 virtual void passRegistered(const PassInfo *) {} 131 132 /// enumeratePasses - Iterate over the registered passes, calling the 133 /// passEnumerate callback on each PassInfo object. 134 void enumeratePasses(); 135 136 /// passEnumerate - Callback function invoked when someone calls 137 /// enumeratePasses on this PassRegistrationListener object. 138 virtual void passEnumerate(const PassInfo *) {} 139 }; 140 141 } // end namespace llvm 142 143 #endif // LLVM_PASSSUPPORT_H 144