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

source code of llvm/include/llvm/PassSupport.h