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/CodeGen/Passes.h"
     22 #include "llvm/Support/CommandLine.h"
     23 
     24 namespace llvm {
     25 
     26 typedef void *(*MachinePassCtor)();
     27 
     28 
     29 //===----------------------------------------------------------------------===//
     30 ///
     31 /// MachinePassRegistryListener - Listener to adds and removals of nodes in
     32 /// registration list.
     33 ///
     34 //===----------------------------------------------------------------------===//
     35 class MachinePassRegistryListener {
     36 public:
     37   MachinePassRegistryListener() {}
     38   virtual ~MachinePassRegistryListener() {}
     39   virtual void NotifyAdd(const char *N, MachinePassCtor C, const char *D) = 0;
     40   virtual void NotifyRemove(const char *N) = 0;
     41 };
     42 
     43 
     44 //===----------------------------------------------------------------------===//
     45 ///
     46 /// MachinePassRegistryNode - Machine pass node stored in registration list.
     47 ///
     48 //===----------------------------------------------------------------------===//
     49 class MachinePassRegistryNode {
     50 
     51 private:
     52 
     53   MachinePassRegistryNode *Next;        // Next function pass in list.
     54   const char *Name;                     // Name of function pass.
     55   const char *Description;              // Description string.
     56   MachinePassCtor Ctor;                 // Function pass creator.
     57 
     58 public:
     59 
     60   MachinePassRegistryNode(const char *N, const char *D, MachinePassCtor C)
     61   : Next(NULL)
     62   , Name(N)
     63   , Description(D)
     64   , Ctor(C)
     65   {}
     66 
     67   // Accessors
     68   MachinePassRegistryNode *getNext()      const { return Next; }
     69   MachinePassRegistryNode **getNextAddress()    { return &Next; }
     70   const char *getName()                   const { return Name; }
     71   const char *getDescription()            const { return Description; }
     72   MachinePassCtor getCtor()               const { return Ctor; }
     73   void setNext(MachinePassRegistryNode *N)      { Next = N; }
     74 
     75 };
     76 
     77 
     78 //===----------------------------------------------------------------------===//
     79 ///
     80 /// MachinePassRegistry - Track the registration of machine passes.
     81 ///
     82 //===----------------------------------------------------------------------===//
     83 class MachinePassRegistry {
     84 
     85 private:
     86 
     87   MachinePassRegistryNode *List;        // List of registry nodes.
     88   MachinePassCtor Default;              // Default function pass creator.
     89   MachinePassRegistryListener* Listener;// Listener for list adds are removes.
     90 
     91 public:
     92 
     93   // NO CONSTRUCTOR - we don't want static constructor ordering to mess
     94   // with the registry.
     95 
     96   // Accessors.
     97   //
     98   MachinePassRegistryNode *getList()                    { return List; }
     99   MachinePassCtor getDefault()                          { return Default; }
    100   void setDefault(MachinePassCtor C)                    { Default = C; }
    101   void setListener(MachinePassRegistryListener *L)      { Listener = L; }
    102 
    103   /// Add - Adds a function pass to the registration list.
    104   ///
    105   void Add(MachinePassRegistryNode *Node);
    106 
    107   /// Remove - Removes a function pass from the registration list.
    108   ///
    109   void Remove(MachinePassRegistryNode *Node);
    110 
    111 };
    112 
    113 
    114 //===----------------------------------------------------------------------===//
    115 ///
    116 /// RegisterPassParser class - Handle the addition of new machine passes.
    117 ///
    118 //===----------------------------------------------------------------------===//
    119 template<class RegistryClass>
    120 class RegisterPassParser : public MachinePassRegistryListener,
    121                    public cl::parser<typename RegistryClass::FunctionPassCtor> {
    122 public:
    123   RegisterPassParser() {}
    124   ~RegisterPassParser() { RegistryClass::setListener(NULL); }
    125 
    126   void initialize(cl::Option &O) {
    127     cl::parser<typename RegistryClass::FunctionPassCtor>::initialize(O);
    128 
    129     // Add existing passes to option.
    130     for (RegistryClass *Node = RegistryClass::getList();
    131          Node; Node = Node->getNext()) {
    132       this->addLiteralOption(Node->getName(),
    133                       (typename RegistryClass::FunctionPassCtor)Node->getCtor(),
    134                              Node->getDescription());
    135     }
    136 
    137     // Make sure we listen for list changes.
    138     RegistryClass::setListener(this);
    139   }
    140 
    141   // Implement the MachinePassRegistryListener callbacks.
    142   //
    143   virtual void NotifyAdd(const char *N,
    144                          MachinePassCtor C,
    145                          const char *D) {
    146     this->addLiteralOption(N, (typename RegistryClass::FunctionPassCtor)C, D);
    147   }
    148   virtual void NotifyRemove(const char *N) {
    149     this->removeLiteralOption(N);
    150   }
    151 };
    152 
    153 
    154 } // end namespace llvm
    155 
    156 #endif
    157