LLVM 22.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"
31#include "llvm/Support/Error.h"
33#include <functional>
34
35namespace llvm {
36
37class 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
68template <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.
89template <typename passName> struct RegisterPass : public PassInfo {
90 // Register Pass using default constructor...
92 bool is_analysis = false)
93 : PassInfo(Name, PassArg, &passName::ID,
95 is_analysis) {
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).
108 virtual ~PassRegistrationListener() = default;
109
110 /// Callback functions - These functions are invoked whenever a pass is loaded
111 /// or removed from the current executable.
112 virtual void passRegistered(const PassInfo *) {}
113
114 /// enumeratePasses - Iterate over the registered passes, calling the
115 /// passEnumerate callback on each PassInfo object.
117
118 /// passEnumerate - Callback function invoked when someone calls
119 /// enumeratePasses on this PassRegistrationListener object.
120 virtual void passEnumerate(const PassInfo *) {}
121};
122
123} // end namespace llvm
124
125#endif // LLVM_PASSSUPPORT_H
#define LLVM_ABI
Definition: Compiler.h:213
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"))
Shrink Wrap Pass
Definition: ShrinkWrap.cpp:301
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 LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
LLVM_ABI 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:99
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Pass * callDefaultCtor()
Definition: PassSupport.h:68
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition: Error.cpp:167
PassRegistrationListener class - This class is meant to be derived from by clients that are intereste...
Definition: PassSupport.h:106
virtual void passRegistered(const PassInfo *)
Callback functions - These functions are invoked whenever a pass is loaded or removed from the curren...
Definition: PassSupport.h:112
virtual void passEnumerate(const PassInfo *)
passEnumerate - Callback function invoked when someone calls enumeratePasses on this PassRegistration...
Definition: PassSupport.h:120
LLVM_ABI void enumeratePasses()
enumeratePasses - Iterate over the registered passes, calling the passEnumerate callback on each Pass...
Definition: Pass.cpp:227
virtual ~PassRegistrationListener()=default
RegisterPass<t> template - This template class is used to notify the system that a Pass is available ...
Definition: PassSupport.h:89
RegisterPass(StringRef PassArg, StringRef Name, bool CFGOnly=false, bool is_analysis=false)
Definition: PassSupport.h:91