LLVM 20.0.0git
PassSupport.h
Go to the documentation of this file.
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"
32#include <functional>
33
34namespace llvm {
35
36class 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
79template <
80 class PassName,
81 std::enable_if_t<std::is_default_constructible<PassName>{}, bool> = true>
83 return new PassName();
84}
85
86template <
87 class PassName,
88 std::enable_if_t<!std::is_default_constructible<PassName>{}, bool> = true>
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.
107template <typename passName> struct RegisterPass : public PassInfo {
108 // Register Pass using default constructor...
110 bool is_analysis = false)
111 : PassInfo(Name, PassArg, &passName::ID,
113 is_analysis) {
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).
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
aarch64 AArch64 CCMP Pass
std::string Name
static cl::opt< bool > CFGOnly("dot-mcfg-only", cl::init(false), cl::Hidden, cl::desc("Print only the CFG without blocks body"))
static const char PassName[]
PassInfo class - An instance of this class exists for every pass known by the system,...
Definition: PassInfo.h:30
Pass *(*)() NormalCtor_t
Definition: PassInfo.h:32
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
void registerPass(const PassInfo &PI, bool ShouldFree=false)
registerPass - Register a pass (by means of its PassInfo) with the registry.
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:94
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Pass * callDefaultCtor()
Definition: PassSupport.h:82
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
PassRegistrationListener class - This class is meant to be derived from by clients that are intereste...
Definition: PassSupport.h:124
virtual void passRegistered(const PassInfo *)
Callback functions - These functions are invoked whenever a pass is loaded or removed from the curren...
Definition: PassSupport.h:130
virtual void passEnumerate(const PassInfo *)
passEnumerate - Callback function invoked when someone calls enumeratePasses on this PassRegistration...
Definition: PassSupport.h:138
void enumeratePasses()
enumeratePasses - Iterate over the registered passes, calling the passEnumerate callback on each Pass...
Definition: Pass.cpp:213
virtual ~PassRegistrationListener()=default
RegisterPass<t> template - This template class is used to notify the system that a Pass is available ...
Definition: PassSupport.h:107
RegisterPass(StringRef PassArg, StringRef Name, bool CFGOnly=false, bool is_analysis=false)
Definition: PassSupport.h:109