xref: /freebsd/contrib/llvm-project/llvm/include/llvm/PassSupport.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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/Compiler.h"
31 #include "llvm/Support/Error.h"
32 #include "llvm/Support/Threading.h"
33 #include <functional>
34 
35 namespace llvm {
36 
37 class Pass;
38 
39 #define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)              \
40   static void initialize##passName##PassOnce(PassRegistry &Registry) {
41 
42 #define INITIALIZE_PASS_DEPENDENCY(depName) initialize##depName##Pass(Registry);
43 
44 #define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)                \
45   PassInfo *PI = new PassInfo(                                                 \
46       name, arg, &passName::ID,                                                \
47       PassInfo::NormalCtor_t(callDefaultCtor<passName>), cfg, analysis);       \
48   Registry.registerPass(*PI, true);                                            \
49   }                                                                            \
50   static llvm::once_flag Initialize##passName##PassFlag;                       \
51   void llvm::initialize##passName##Pass(PassRegistry &Registry) {              \
52     llvm::call_once(Initialize##passName##PassFlag,                            \
53                     initialize##passName##PassOnce, std::ref(Registry));       \
54   }
55 
56 #define INITIALIZE_PASS(passName, arg, name, cfg, analysis)                    \
57   INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)                    \
58   INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
59 
60 #define INITIALIZE_PASS_WITH_OPTIONS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \
61   INITIALIZE_PASS_BEGIN(PassName, Arg, Name, Cfg, Analysis)                    \
62   PassName::registerOptions();
63 
64 #define INITIALIZE_PASS_WITH_OPTIONS(PassName, Arg, Name, Cfg, Analysis)       \
65   INITIALIZE_PASS_WITH_OPTIONS_BEGIN(PassName, Arg, Name, Cfg, Analysis)       \
66   INITIALIZE_PASS_END(PassName, Arg, Name, Cfg, Analysis)
67 
callDefaultCtor()68 template <class PassName> Pass *callDefaultCtor() {
69   if constexpr (std::is_default_constructible_v<PassName>)
70     return new PassName();
71   else
72     // Some codegen passes should only be testable via
73     // `llc -{start|stop}-{before|after}=<passname>`, not via `opt -<passname>`.
74     report_fatal_error("target-specific codegen-only pass");
75 }
76 
77 //===---------------------------------------------------------------------------
78 /// RegisterPass<t> template - This template class is used to notify the system
79 /// that a Pass is available for use, and registers it into the internal
80 /// database maintained by the PassManager.  Unless this template is used, opt,
81 /// for example will not be able to see the pass and attempts to create the pass
82 /// will fail. This template is used in the follow manner (at global scope, in
83 /// your .cpp file):
84 ///
85 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
86 ///
87 /// This statement will cause your pass to be created by calling the default
88 /// constructor exposed by the pass.
89 template <typename passName> struct RegisterPass : public PassInfo {
90   // Register Pass using default constructor...
91   RegisterPass(StringRef PassArg, StringRef Name, bool CFGOnly = false,
92                bool is_analysis = false)
93       : PassInfo(Name, PassArg, &passName::ID,
94                  PassInfo::NormalCtor_t(callDefaultCtor<passName>), CFGOnly,
95                  is_analysis) {
96     PassRegistry::getPassRegistry()->registerPass(*this);
97   }
98 };
99 
100 //===---------------------------------------------------------------------------
101 /// PassRegistrationListener class - This class is meant to be derived from by
102 /// clients that are interested in which passes get registered and unregistered
103 /// at runtime (which can be because of the RegisterPass constructors being run
104 /// as the program starts up, or may be because a shared object just got
105 /// loaded).
106 struct PassRegistrationListener {
107   PassRegistrationListener() = default;
108   virtual ~PassRegistrationListener() = default;
109 
110   /// Callback functions - These functions are invoked whenever a pass is loaded
111   /// or removed from the current executable.
passRegisteredPassRegistrationListener112   virtual void passRegistered(const PassInfo *) {}
113 
114   /// enumeratePasses - Iterate over the registered passes, calling the
115   /// passEnumerate callback on each PassInfo object.
116   LLVM_ABI void enumeratePasses();
117 
118   /// passEnumerate - Callback function invoked when someone calls
119   /// enumeratePasses on this PassRegistrationListener object.
passEnumeratePassRegistrationListener120   virtual void passEnumerate(const PassInfo *) {}
121 };
122 
123 } // end namespace llvm
124 
125 #endif // LLVM_PASSSUPPORT_H
126