LLVM API Documentation
00001 //===-- llvm/CodeGen/MachinePassRegistry.h ----------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains the mechanics for machine function pass registries. A 00011 // function pass registry (MachinePassRegistry) is auto filled by the static 00012 // constructors of MachinePassRegistryNode. Further there is a command line 00013 // parser (RegisterPassParser) which listens to each registry for additions 00014 // and deletions, so that the appropriate command option is updated. 00015 // 00016 //===----------------------------------------------------------------------===// 00017 00018 #ifndef LLVM_CODEGEN_MACHINEPASSREGISTRY_H 00019 #define LLVM_CODEGEN_MACHINEPASSREGISTRY_H 00020 00021 #include "llvm/CodeGen/Passes.h" 00022 #include "llvm/Support/CommandLine.h" 00023 00024 namespace llvm { 00025 00026 typedef void *(*MachinePassCtor)(); 00027 00028 00029 //===----------------------------------------------------------------------===// 00030 /// 00031 /// MachinePassRegistryListener - Listener to adds and removals of nodes in 00032 /// registration list. 00033 /// 00034 //===----------------------------------------------------------------------===// 00035 class MachinePassRegistryListener { 00036 virtual void anchor(); 00037 public: 00038 MachinePassRegistryListener() {} 00039 virtual ~MachinePassRegistryListener() {} 00040 virtual void NotifyAdd(const char *N, MachinePassCtor C, const char *D) = 0; 00041 virtual void NotifyRemove(const char *N) = 0; 00042 }; 00043 00044 00045 //===----------------------------------------------------------------------===// 00046 /// 00047 /// MachinePassRegistryNode - Machine pass node stored in registration list. 00048 /// 00049 //===----------------------------------------------------------------------===// 00050 class MachinePassRegistryNode { 00051 00052 private: 00053 00054 MachinePassRegistryNode *Next; // Next function pass in list. 00055 const char *Name; // Name of function pass. 00056 const char *Description; // Description string. 00057 MachinePassCtor Ctor; // Function pass creator. 00058 00059 public: 00060 00061 MachinePassRegistryNode(const char *N, const char *D, MachinePassCtor C) 00062 : Next(nullptr) 00063 , Name(N) 00064 , Description(D) 00065 , Ctor(C) 00066 {} 00067 00068 // Accessors 00069 MachinePassRegistryNode *getNext() const { return Next; } 00070 MachinePassRegistryNode **getNextAddress() { return &Next; } 00071 const char *getName() const { return Name; } 00072 const char *getDescription() const { return Description; } 00073 MachinePassCtor getCtor() const { return Ctor; } 00074 void setNext(MachinePassRegistryNode *N) { Next = N; } 00075 00076 }; 00077 00078 00079 //===----------------------------------------------------------------------===// 00080 /// 00081 /// MachinePassRegistry - Track the registration of machine passes. 00082 /// 00083 //===----------------------------------------------------------------------===// 00084 class MachinePassRegistry { 00085 00086 private: 00087 00088 MachinePassRegistryNode *List; // List of registry nodes. 00089 MachinePassCtor Default; // Default function pass creator. 00090 MachinePassRegistryListener* Listener;// Listener for list adds are removes. 00091 00092 public: 00093 00094 // NO CONSTRUCTOR - we don't want static constructor ordering to mess 00095 // with the registry. 00096 00097 // Accessors. 00098 // 00099 MachinePassRegistryNode *getList() { return List; } 00100 MachinePassCtor getDefault() { return Default; } 00101 void setDefault(MachinePassCtor C) { Default = C; } 00102 void setDefault(StringRef Name); 00103 void setListener(MachinePassRegistryListener *L) { Listener = L; } 00104 00105 /// Add - Adds a function pass to the registration list. 00106 /// 00107 void Add(MachinePassRegistryNode *Node); 00108 00109 /// Remove - Removes a function pass from the registration list. 00110 /// 00111 void Remove(MachinePassRegistryNode *Node); 00112 00113 }; 00114 00115 00116 //===----------------------------------------------------------------------===// 00117 /// 00118 /// RegisterPassParser class - Handle the addition of new machine passes. 00119 /// 00120 //===----------------------------------------------------------------------===// 00121 template<class RegistryClass> 00122 class RegisterPassParser : public MachinePassRegistryListener, 00123 public cl::parser<typename RegistryClass::FunctionPassCtor> { 00124 public: 00125 RegisterPassParser() {} 00126 ~RegisterPassParser() { RegistryClass::setListener(nullptr); } 00127 00128 void initialize(cl::Option &O) { 00129 cl::parser<typename RegistryClass::FunctionPassCtor>::initialize(O); 00130 00131 // Add existing passes to option. 00132 for (RegistryClass *Node = RegistryClass::getList(); 00133 Node; Node = Node->getNext()) { 00134 this->addLiteralOption(Node->getName(), 00135 (typename RegistryClass::FunctionPassCtor)Node->getCtor(), 00136 Node->getDescription()); 00137 } 00138 00139 // Make sure we listen for list changes. 00140 RegistryClass::setListener(this); 00141 } 00142 00143 // Implement the MachinePassRegistryListener callbacks. 00144 // 00145 void NotifyAdd(const char *N, MachinePassCtor C, const char *D) override { 00146 this->addLiteralOption(N, (typename RegistryClass::FunctionPassCtor)C, D); 00147 } 00148 void NotifyRemove(const char *N) override { 00149 this->removeLiteralOption(N); 00150 } 00151 }; 00152 00153 00154 } // end namespace llvm 00155 00156 #endif