Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:46

0001 //===- llvm/PassSupport.h - Pass Support code -------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines stuff that is used to define and "use" Passes.  This file
0010 // is automatically #included by Pass.h, so:
0011 //
0012 //           NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
0013 //
0014 // Instead, #include Pass.h.
0015 //
0016 // This file defines Pass registration code and classes used for it.
0017 //
0018 //===----------------------------------------------------------------------===//
0019 
0020 #if !defined(LLVM_PASS_H) || defined(LLVM_PASSSUPPORT_H)
0021 #error "Do not include <PassSupport.h>; include <Pass.h> instead"
0022 #endif
0023 
0024 #ifndef LLVM_PASSSUPPORT_H
0025 #define LLVM_PASSSUPPORT_H
0026 
0027 #include "llvm/ADT/StringRef.h"
0028 #include "llvm/PassInfo.h"
0029 #include "llvm/PassRegistry.h"
0030 #include "llvm/Support/Error.h"
0031 #include "llvm/Support/Threading.h"
0032 #include <functional>
0033 
0034 namespace llvm {
0035 
0036 class Pass;
0037 
0038 #define INITIALIZE_PASS(passName, arg, name, cfg, analysis)                    \
0039   static void *initialize##passName##PassOnce(PassRegistry &Registry) {        \
0040     PassInfo *PI = new PassInfo(                                               \
0041         name, arg, &passName::ID,                                              \
0042         PassInfo::NormalCtor_t(callDefaultCtor<passName>), cfg, analysis);     \
0043     Registry.registerPass(*PI, true);                                          \
0044     return PI;                                                                 \
0045   }                                                                            \
0046   static llvm::once_flag Initialize##passName##PassFlag;                       \
0047   void llvm::initialize##passName##Pass(PassRegistry &Registry) {              \
0048     llvm::call_once(Initialize##passName##PassFlag,                            \
0049                     initialize##passName##PassOnce, std::ref(Registry));       \
0050   }
0051 
0052 #define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)              \
0053   static void *initialize##passName##PassOnce(PassRegistry &Registry) {
0054 
0055 #define INITIALIZE_PASS_DEPENDENCY(depName) initialize##depName##Pass(Registry);
0056 
0057 #define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)                \
0058   PassInfo *PI = new PassInfo(                                                 \
0059       name, arg, &passName::ID,                                                \
0060       PassInfo::NormalCtor_t(callDefaultCtor<passName>), cfg, analysis);       \
0061   Registry.registerPass(*PI, true);                                            \
0062   return PI;                                                                   \
0063   }                                                                            \
0064   static llvm::once_flag Initialize##passName##PassFlag;                       \
0065   void llvm::initialize##passName##Pass(PassRegistry &Registry) {              \
0066     llvm::call_once(Initialize##passName##PassFlag,                            \
0067                     initialize##passName##PassOnce, std::ref(Registry));       \
0068   }
0069 
0070 #define INITIALIZE_PASS_WITH_OPTIONS(PassName, Arg, Name, Cfg, Analysis)       \
0071   INITIALIZE_PASS_BEGIN(PassName, Arg, Name, Cfg, Analysis)                    \
0072   PassName::registerOptions();                                                 \
0073   INITIALIZE_PASS_END(PassName, Arg, Name, Cfg, Analysis)
0074 
0075 #define INITIALIZE_PASS_WITH_OPTIONS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \
0076   INITIALIZE_PASS_BEGIN(PassName, Arg, Name, Cfg, Analysis)                    \
0077   PassName::registerOptions();
0078 
0079 template <
0080     class PassName,
0081     std::enable_if_t<std::is_default_constructible<PassName>{}, bool> = true>
0082 Pass *callDefaultCtor() {
0083   return new PassName();
0084 }
0085 
0086 template <
0087     class PassName,
0088     std::enable_if_t<!std::is_default_constructible<PassName>{}, bool> = true>
0089 Pass *callDefaultCtor() {
0090   // Some codegen passes should only be testable via
0091   // `llc -{start|stop}-{before|after}=<passname>`, not via `opt -<passname>`.
0092   report_fatal_error("target-specific codegen-only pass");
0093 }
0094 
0095 //===---------------------------------------------------------------------------
0096 /// RegisterPass<t> template - This template class is used to notify the system
0097 /// that a Pass is available for use, and registers it into the internal
0098 /// database maintained by the PassManager.  Unless this template is used, opt,
0099 /// for example will not be able to see the pass and attempts to create the pass
0100 /// will fail. This template is used in the follow manner (at global scope, in
0101 /// your .cpp file):
0102 ///
0103 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
0104 ///
0105 /// This statement will cause your pass to be created by calling the default
0106 /// constructor exposed by the pass.
0107 template <typename passName> struct RegisterPass : public PassInfo {
0108   // Register Pass using default constructor...
0109   RegisterPass(StringRef PassArg, StringRef Name, bool CFGOnly = false,
0110                bool is_analysis = false)
0111       : PassInfo(Name, PassArg, &passName::ID,
0112                  PassInfo::NormalCtor_t(callDefaultCtor<passName>), CFGOnly,
0113                  is_analysis) {
0114     PassRegistry::getPassRegistry()->registerPass(*this);
0115   }
0116 };
0117 
0118 //===---------------------------------------------------------------------------
0119 /// PassRegistrationListener class - This class is meant to be derived from by
0120 /// clients that are interested in which passes get registered and unregistered
0121 /// at runtime (which can be because of the RegisterPass constructors being run
0122 /// as the program starts up, or may be because a shared object just got
0123 /// loaded).
0124 struct PassRegistrationListener {
0125   PassRegistrationListener() = default;
0126   virtual ~PassRegistrationListener() = default;
0127 
0128   /// Callback functions - These functions are invoked whenever a pass is loaded
0129   /// or removed from the current executable.
0130   virtual void passRegistered(const PassInfo *) {}
0131 
0132   /// enumeratePasses - Iterate over the registered passes, calling the
0133   /// passEnumerate callback on each PassInfo object.
0134   void enumeratePasses();
0135 
0136   /// passEnumerate - Callback function invoked when someone calls
0137   /// enumeratePasses on this PassRegistrationListener object.
0138   virtual void passEnumerate(const PassInfo *) {}
0139 };
0140 
0141 } // end namespace llvm
0142 
0143 #endif // LLVM_PASSSUPPORT_H