Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:31

0001 //===- llvm/CodeGen/MachinePassRegistry.h -----------------------*- 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 contains the mechanics for machine function pass registries.  A
0010 // function pass registry (MachinePassRegistry) is auto filled by the static
0011 // constructors of MachinePassRegistryNode.  Further there is a command line
0012 // parser (RegisterPassParser) which listens to each registry for additions
0013 // and deletions, so that the appropriate command option is updated.
0014 //
0015 //===----------------------------------------------------------------------===//
0016 
0017 #ifndef LLVM_CODEGEN_MACHINEPASSREGISTRY_H
0018 #define LLVM_CODEGEN_MACHINEPASSREGISTRY_H
0019 
0020 #include "llvm/ADT/StringRef.h"
0021 #include "llvm/CodeGen/Passes.h"
0022 #include "llvm/Support/CommandLine.h"
0023 
0024 namespace llvm {
0025 
0026 //===----------------------------------------------------------------------===//
0027 ///
0028 /// MachinePassRegistryListener - Listener to adds and removals of nodes in
0029 /// registration list.
0030 ///
0031 //===----------------------------------------------------------------------===//
0032 template <class PassCtorTy> class MachinePassRegistryListener {
0033   virtual void anchor() {}
0034 
0035 public:
0036   MachinePassRegistryListener() = default;
0037   virtual ~MachinePassRegistryListener() = default;
0038 
0039   virtual void NotifyAdd(StringRef N, PassCtorTy C, StringRef D) = 0;
0040   virtual void NotifyRemove(StringRef N) = 0;
0041 };
0042 
0043 //===----------------------------------------------------------------------===//
0044 ///
0045 /// MachinePassRegistryNode - Machine pass node stored in registration list.
0046 ///
0047 //===----------------------------------------------------------------------===//
0048 template <typename PassCtorTy> class MachinePassRegistryNode {
0049 private:
0050   MachinePassRegistryNode *Next = nullptr; // Next function pass in list.
0051   StringRef Name;                       // Name of function pass.
0052   StringRef Description;                // Description string.
0053   PassCtorTy Ctor;                      // Pass creator.
0054 
0055 public:
0056   MachinePassRegistryNode(const char *N, const char *D, PassCtorTy C)
0057       : Name(N), Description(D), Ctor(C) {}
0058 
0059   // Accessors
0060   MachinePassRegistryNode *getNext()      const { return Next; }
0061   MachinePassRegistryNode **getNextAddress()    { return &Next; }
0062   StringRef getName()                   const { return Name; }
0063   StringRef getDescription()            const { return Description; }
0064   PassCtorTy getCtor() const { return Ctor; }
0065   void setNext(MachinePassRegistryNode *N)      { Next = N; }
0066 };
0067 
0068 //===----------------------------------------------------------------------===//
0069 ///
0070 /// MachinePassRegistry - Track the registration of machine passes.
0071 ///
0072 //===----------------------------------------------------------------------===//
0073 template <typename PassCtorTy> class MachinePassRegistry {
0074 private:
0075   MachinePassRegistryNode<PassCtorTy> *List; // List of registry nodes.
0076   PassCtorTy Default;                        // Default function pass creator.
0077   MachinePassRegistryListener<PassCtorTy>
0078       *Listener; // Listener for list adds are removes.
0079 
0080 public:
0081   // NO CONSTRUCTOR - we don't want static constructor ordering to mess
0082   // with the registry.
0083 
0084   // Accessors.
0085   //
0086   MachinePassRegistryNode<PassCtorTy> *getList() { return List; }
0087   PassCtorTy getDefault() { return Default; }
0088   void setDefault(PassCtorTy C) { Default = C; }
0089   /// setDefault - Set the default constructor by name.
0090   void setDefault(StringRef Name) {
0091     PassCtorTy Ctor = nullptr;
0092     for (MachinePassRegistryNode<PassCtorTy> *R = getList(); R;
0093          R = R->getNext()) {
0094       if (R->getName() == Name) {
0095         Ctor = R->getCtor();
0096         break;
0097       }
0098     }
0099     assert(Ctor && "Unregistered pass name");
0100     setDefault(Ctor);
0101   }
0102   void setListener(MachinePassRegistryListener<PassCtorTy> *L) { Listener = L; }
0103 
0104   /// Add - Adds a function pass to the registration list.
0105   ///
0106   void Add(MachinePassRegistryNode<PassCtorTy> *Node) {
0107     Node->setNext(List);
0108     List = Node;
0109     if (Listener)
0110       Listener->NotifyAdd(Node->getName(), Node->getCtor(),
0111                           Node->getDescription());
0112   }
0113 
0114   /// Remove - Removes a function pass from the registration list.
0115   ///
0116   void Remove(MachinePassRegistryNode<PassCtorTy> *Node) {
0117     for (MachinePassRegistryNode<PassCtorTy> **I = &List; *I;
0118          I = (*I)->getNextAddress()) {
0119       if (*I == Node) {
0120         if (Listener)
0121           Listener->NotifyRemove(Node->getName());
0122         *I = (*I)->getNext();
0123         break;
0124       }
0125     }
0126   }
0127 };
0128 
0129 //===----------------------------------------------------------------------===//
0130 ///
0131 /// RegisterPassParser class - Handle the addition of new machine passes.
0132 ///
0133 //===----------------------------------------------------------------------===//
0134 template <class RegistryClass>
0135 class RegisterPassParser
0136     : public MachinePassRegistryListener<
0137           typename RegistryClass::FunctionPassCtor>,
0138       public cl::parser<typename RegistryClass::FunctionPassCtor> {
0139 public:
0140   RegisterPassParser(cl::Option &O)
0141       : cl::parser<typename RegistryClass::FunctionPassCtor>(O) {}
0142   ~RegisterPassParser() override { RegistryClass::setListener(nullptr); }
0143 
0144   void initialize() {
0145     cl::parser<typename RegistryClass::FunctionPassCtor>::initialize();
0146 
0147     // Add existing passes to option.
0148     for (RegistryClass *Node = RegistryClass::getList();
0149          Node; Node = Node->getNext()) {
0150       this->addLiteralOption(Node->getName(),
0151                       (typename RegistryClass::FunctionPassCtor)Node->getCtor(),
0152                              Node->getDescription());
0153     }
0154 
0155     // Make sure we listen for list changes.
0156     RegistryClass::setListener(this);
0157   }
0158 
0159   // Implement the MachinePassRegistryListener callbacks.
0160   void NotifyAdd(StringRef N, typename RegistryClass::FunctionPassCtor C,
0161                  StringRef D) override {
0162     this->addLiteralOption(N, C, D);
0163   }
0164   void NotifyRemove(StringRef N) override {
0165     this->removeLiteralOption(N);
0166   }
0167 };
0168 
0169 } // end namespace llvm
0170 
0171 #endif // LLVM_CODEGEN_MACHINEPASSREGISTRY_H