Home | History | Annotate | Download | only in CodeGen
      1 //===- llvm/CodeGen/MachinePassRegistry.h -----------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file contains the mechanics for machine function pass registries.  A
     11 // function pass registry (MachinePassRegistry) is auto filled by the static
     12 // constructors of MachinePassRegistryNode.  Further there is a command line
     13 // parser (RegisterPassParser) which listens to each registry for additions
     14 // and deletions, so that the appropriate command option is updated.
     15 //
     16 //===----------------------------------------------------------------------===//
     17 
     18 #ifndef LLVM_CODEGEN_MACHINEPASSREGISTRY_H
     19 #define LLVM_CODEGEN_MACHINEPASSREGISTRY_H
     20 
     21 #include "llvm/ADT/StringRef.h"
     22 #include "llvm/CodeGen/Passes.h"
     23 #include "llvm/Support/CommandLine.h"
     24 
     25 namespace llvm {
     26 
     27 using MachinePassCtor = void *(*)();
     28 
     29 //===----------------------------------------------------------------------===//
     30 ///
     31 /// MachinePassRegistryListener - Listener to adds and removals of nodes in
     32 /// registration list.
     33 ///
     34 //===----------------------------------------------------------------------===//
     35 class MachinePassRegistryListener {
     36   virtual void anchor();
     37 
     38 public:
     39   MachinePassRegistryListener() = default;
     40   virtual ~MachinePassRegistryListener() = default;
     41 
     42   virtual void NotifyAdd(StringRef N, MachinePassCtor C, StringRef D) = 0;
     43   virtual void NotifyRemove(StringRef N) = 0;
     44 };
     45 
     46 //===----------------------------------------------------------------------===//
     47 ///
     48 /// MachinePassRegistryNode - Machine pass node stored in registration list.
     49 ///
     50 //===----------------------------------------------------------------------===//
     51 class MachinePassRegistryNode {
     52 private:
     53   MachinePassRegistryNode *Next = nullptr; // Next function pass in list.
     54   StringRef Name;                       // Name of function pass.
     55   StringRef Description;                // Description string.
     56   MachinePassCtor Ctor;                 // Function pass creator.
     57 
     58 public:
     59   MachinePassRegistryNode(const char *N, const char *D, MachinePassCtor C)
     60       : Name(N), Description(D), Ctor(C) {}
     61 
     62   // Accessors
     63   MachinePassRegistryNode *getNext()      const { return Next; }
     64   MachinePassRegistryNode **getNextAddress()    { return &Next; }
     65   StringRef getName()                   const { return Name; }
     66   StringRef getDescription()            const { return Description; }
     67   MachinePassCtor getCtor()               const { return Ctor; }
     68   void setNext(MachinePassRegistryNode *N)      { Next = N; }
     69 };
     70 
     71 //===----------------------------------------------------------------------===//
     72 ///
     73 /// MachinePassRegistry - Track the registration of machine passes.
     74 ///
     75 //===----------------------------------------------------------------------===//
     76 class MachinePassRegistry {
     77 private:
     78   MachinePassRegistryNode *List;        // List of registry nodes.
     79   MachinePassCtor Default;              // Default function pass creator.
     80   MachinePassRegistryListener *Listener; // Listener for list adds are removes.
     81 
     82 public:
     83   // NO CONSTRUCTOR - we don't want static constructor ordering to mess
     84   // with the registry.
     85 
     86   // Accessors.
     87   //
     88   MachinePassRegistryNode *getList()                    { return List; }
     89   MachinePassCtor getDefault()                          { return Default; }
     90   void setDefault(MachinePassCtor C)                    { Default = C; }
     91   void setDefault(StringRef Name);
     92   void setListener(MachinePassRegistryListener *L)      { Listener = L; }
     93 
     94   /// Add - Adds a function pass to the registration list.
     95   ///
     96   void Add(MachinePassRegistryNode *Node);
     97 
     98   /// Remove - Removes a function pass from the registration list.
     99   ///
    100   void Remove(MachinePassRegistryNode *Node);
    101 };
    102 
    103 //===----------------------------------------------------------------------===//
    104 ///
    105 /// RegisterPassParser class - Handle the addition of new machine passes.
    106 ///
    107 //===----------------------------------------------------------------------===//
    108 template<class RegistryClass>
    109 class RegisterPassParser : public MachinePassRegistryListener,
    110                    public cl::parser<typename RegistryClass::FunctionPassCtor> {
    111 public:
    112   RegisterPassParser(cl::Option &O)
    113       : cl::parser<typename RegistryClass::FunctionPassCtor>(O) {}
    114   ~RegisterPassParser() override { RegistryClass::setListener(nullptr); }
    115 
    116   void initialize() {
    117     cl::parser<typename RegistryClass::FunctionPassCtor>::initialize();
    118 
    119     // Add existing passes to option.
    120     for (RegistryClass *Node = RegistryClass::getList();
    121          Node; Node = Node->getNext()) {
    122       this->addLiteralOption(Node->getName(),
    123                       (typename RegistryClass::FunctionPassCtor)Node->getCtor(),
    124                              Node->getDescription());
    125     }
    126 
    127     // Make sure we listen for list changes.
    128     RegistryClass::setListener(this);
    129   }
    130 
    131   // Implement the MachinePassRegistryListener callbacks.
    132   void NotifyAdd(StringRef N, MachinePassCtor C, StringRef D) override {
    133     this->addLiteralOption(N, (typename RegistryClass::FunctionPassCtor)C, D);
    134   }
    135   void NotifyRemove(StringRef N) override {
    136     this->removeLiteralOption(N);
    137   }
    138 };
    139 
    140 } // end namespace llvm
    141 
    142 #endif // LLVM_CODEGEN_MACHINEPASSREGISTRY_H
    143