Index: include/llvm/Target/TargetMachineRegistry.h
===================================================================
--- include/llvm/Target/TargetMachineRegistry.h	(revision 41642)
+++ include/llvm/Target/TargetMachineRegistry.h	(working copy)
@@ -17,52 +17,49 @@
 #ifndef LLVM_TARGET_TARGETMACHINEREGISTRY_H
 #define LLVM_TARGET_TARGETMACHINEREGISTRY_H
 
-#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/Registry.h"
 
 namespace llvm {
   class Module;
   class TargetMachine;
+  
+  struct TargetMachineRegistryEntry {
+    const char *Name;
+    const char *ShortDesc;
+    TargetMachine *(*CtorFn)(const Module &, const std::string &);
+    unsigned (*ModuleMatchQualityFn)(const Module &M);
+    unsigned (*JITMatchQualityFn)();
+    
+  public:
+    TargetMachineRegistryEntry(const char *N, const char *SD,
+                      TargetMachine *(*CF)(const Module &, const std::string &),
+                               unsigned (*MMF)(const Module &M),
+                               unsigned (*JMF)())
+      : Name(N), ShortDesc(SD), CtorFn(CF), ModuleMatchQualityFn(MMF),
+        JITMatchQualityFn(JMF) {}
+  };
+  
+  template<>
+  class RegistryTraits<TargetMachine> {
+  public:
+    typedef TargetMachineRegistryEntry entry;
+    
+    static const char *nameof(const entry &Entry) { return Entry.Name; }
+    static const char *descof(const entry &Entry) { return Entry.ShortDesc; }
+  };
 
-  struct TargetMachineRegistry {
-    struct Entry;
-
-    /// TargetMachineRegistry::getList - This static method returns the list of
-    /// target machines that are registered with the system.
-    static const Entry *getList() { return List; }
-
+  struct TargetMachineRegistry : Registry<TargetMachine> {
     /// getClosestStaticTargetForModule - Given an LLVM module, pick the best
     /// target that is compatible with the module.  If no close target can be
     /// found, this returns null and sets the Error string to a reason.
-    static const Entry *getClosestStaticTargetForModule(const Module &M,
+    static const entry *getClosestStaticTargetForModule(const Module &M,
                                                         std::string &Error);
 
     /// getClosestTargetForJIT - Pick the best target that is compatible with
     /// the current host.  If no close target can be found, this returns null
     /// and sets the Error string to a reason.
-    static const Entry *getClosestTargetForJIT(std::string &Error);
+    static const entry *getClosestTargetForJIT(std::string &Error);
 
-
-    /// Entry - One instance of this struct is created for each target that is
-    /// registered.
-    struct Entry {
-      const char *Name;
-      const char *ShortDesc;
-      TargetMachine *(*CtorFn)(const Module &, const std::string &);
-      unsigned (*ModuleMatchQualityFn)(const Module &M);
-      unsigned (*JITMatchQualityFn)();
-
-      const Entry *getNext() const { return Next; }
-
-    protected:
-      Entry(const char *N, const char *SD,
-            TargetMachine *(*CF)(const Module &, const std::string &),
-            unsigned (*MMF)(const Module &M), unsigned (*JMF)());
-    private:
-      const Entry *Next;  // Next entry in the linked list.
-    };
-
-  private:
-    static const Entry *List;
   };
 
   //===--------------------------------------------------------------------===//
@@ -77,51 +74,23 @@
   ///   flavour.
   
   template<class TargetMachineImpl>
-  struct RegisterTarget : public TargetMachineRegistry::Entry {
-    RegisterTarget(const char *Name, const char *ShortDesc) :
-      TargetMachineRegistry::Entry(Name, ShortDesc, &Allocator,
-                                   &TargetMachineImpl::getModuleMatchQuality,
-                                   &TargetMachineImpl::getJITMatchQuality) {
-    }
+  struct RegisterTarget {
+    RegisterTarget(const char *Name, const char *ShortDesc)
+      : Entry(Name, ShortDesc, &Allocator,
+              &TargetMachineImpl::getModuleMatchQuality,
+              &TargetMachineImpl::getJITMatchQuality),
+        Node(Entry)
+    {}
+
   private:
+    TargetMachineRegistry::entry Entry;
+    TargetMachineRegistry::node Node;
+    
     static TargetMachine *Allocator(const Module &M, const std::string &FS) {
       return new TargetMachineImpl(M, FS);
     }
   };
 
-  /// TargetRegistrationListener - This class allows code to listen for targets
-  /// that are dynamically registered, and be notified of it when they are.
-  class TargetRegistrationListener {
-    TargetRegistrationListener **Prev, *Next;
-  public:
-    TargetRegistrationListener();
-    virtual ~TargetRegistrationListener();
-
-    TargetRegistrationListener *getNext() const { return Next; }
-
-    virtual void targetRegistered(const TargetMachineRegistry::Entry *E) = 0;
-  };
-
-
-  //===--------------------------------------------------------------------===//
-  /// TargetNameParser - This option can be used to provide a command line
-  /// option to choose among the various registered targets (commonly -march).
-  class TargetNameParser : public TargetRegistrationListener,
-    public cl::parser<const TargetMachineRegistry::Entry*> {
-  public:
-    void initialize(cl::Option &O) {
-      for (const TargetMachineRegistry::Entry *E =
-             TargetMachineRegistry::getList(); E; E = E->getNext())
-        Values.push_back(std::make_pair(E->Name,
-                                        std::make_pair(E, E->ShortDesc)));
-      cl::parser<const TargetMachineRegistry::Entry*>::initialize(O);
-    }
-
-    virtual void targetRegistered(const TargetMachineRegistry::Entry *E) {
-      Values.push_back(std::make_pair(E->Name,
-                                      std::make_pair(E, E->ShortDesc)));
-    }
-  };
 }
 
 #endif
Index: lib/Target/TargetMachineRegistry.cpp
===================================================================
--- lib/Target/TargetMachineRegistry.cpp	(revision 41642)
+++ lib/Target/TargetMachineRegistry.cpp	(working copy)
@@ -18,44 +18,23 @@
 #include <algorithm>
 using namespace llvm;
 
-/// List - This is the main list of all of the registered target machines.
-const TargetMachineRegistry::Entry *TargetMachineRegistry::List = 0;
+template<> Registry<TargetMachine>::node *Registry<TargetMachine>::Head = 0;
+template<> Registry<TargetMachine>::node *Registry<TargetMachine>::Tail = 0;
+template<> Registry<TargetMachine>::listener *Registry<TargetMachine>::
+ListenerHead = 0;
+template<> Registry<TargetMachine>::listener *Registry<TargetMachine>::
+ListenerTail = 0;
 
-/// Listeners - All of the listeners registered to get notified when new targets
-/// are loaded.
-static TargetRegistrationListener *Listeners = 0;
-
-TargetMachineRegistry::Entry::Entry(const char *N, const char *SD,
-                       TargetMachine *(*CF)(const Module &,const std::string &),
-                           unsigned (*MMF)(const Module &M), unsigned (*JMF)())
-  : Name(N), ShortDesc(SD), CtorFn(CF), ModuleMatchQualityFn(MMF),
-    JITMatchQualityFn(JMF), Next(List) {
-  List = this;
-  for (TargetRegistrationListener *L = Listeners; L; L = L->getNext())
-    L->targetRegistered(this);
-}
-
-TargetRegistrationListener::TargetRegistrationListener() {
-  Next = Listeners;
-  if (Next) Next->Prev = &Next;
-  Prev = &Listeners;
-  Listeners = this;
-}
-
-TargetRegistrationListener::~TargetRegistrationListener() {
-  *Prev = Next;
-}
-
 /// getClosestStaticTargetForModule - Given an LLVM module, pick the best target
 /// that is compatible with the module.  If no close target can be found, this
 /// returns null and sets the Error string to a reason.
-const TargetMachineRegistry::Entry *
+const TargetMachineRegistry::entry *
 TargetMachineRegistry::getClosestStaticTargetForModule(const Module &M,
                                                        std::string &Error) {
-  std::vector<std::pair<unsigned, const Entry *> > UsableTargets;
-  for (const Entry *E = getList(); E; E = E->getNext())
-    if (unsigned Qual = E->ModuleMatchQualityFn(M))
-      UsableTargets.push_back(std::make_pair(Qual, E));
+  std::vector<std::pair<unsigned, const entry *> > UsableTargets;
+  for (iterator I = begin(), E = end(); I != E; ++I)
+    if (unsigned Qual = I->ModuleMatchQualityFn(M))
+      UsableTargets.push_back(std::make_pair(Qual, &*I));
 
   if (UsableTargets.empty()) {
     Error = "No available targets are compatible with this module";
@@ -78,12 +57,12 @@
 /// getClosestTargetForJIT - Pick the best target that is compatible with
 /// the current host.  If no close target can be found, this returns null
 /// and sets the Error string to a reason.
-const TargetMachineRegistry::Entry *
+const TargetMachineRegistry::entry *
 TargetMachineRegistry::getClosestTargetForJIT(std::string &Error) {
-  std::vector<std::pair<unsigned, const Entry *> > UsableTargets;
-  for (const Entry *E = getList(); E; E = E->getNext())
-    if (unsigned Qual = E->JITMatchQualityFn())
-      UsableTargets.push_back(std::make_pair(Qual, E));
+  std::vector<std::pair<unsigned, const entry *> > UsableTargets;
+  for (iterator I = begin(), E = end(); I != E; ++I)
+    if (unsigned Qual = I->JITMatchQualityFn())
+      UsableTargets.push_back(std::make_pair(Qual, &*I));
 
   if (UsableTargets.empty()) {
     Error = "No JIT is available for this host";
@@ -93,7 +72,7 @@
 
   // Otherwise, take the best target.  If there is a tie, just pick one.
   unsigned MaxQual = UsableTargets.front().first;
-  const Entry *MaxQualTarget = UsableTargets.front().second;
+  const entry *MaxQualTarget = UsableTargets.front().second;
 
   for (unsigned i = 1, e = UsableTargets.size(); i != e; ++i)
     if (UsableTargets[i].first > MaxQual) {
Index: lib/Target/CBackend/CBackend.cpp
===================================================================
--- lib/Target/CBackend/CBackend.cpp	(revision 41642)
+++ lib/Target/CBackend/CBackend.cpp	(working copy)
@@ -2665,7 +2665,7 @@
   //Grab the translation table from TargetAsmInfo if it exists
   if (!TAsm) {
     std::string E;
-    const TargetMachineRegistry::Entry* Match = 
+    const TargetMachineRegistry::entry* Match = 
       TargetMachineRegistry::getClosestStaticTargetForModule(*TheModule, E);
     if (Match) {
       //Per platform Target Machines don't exist, so create it
Index: lib/ExecutionEngine/JIT/TargetSelect.cpp
===================================================================
--- lib/ExecutionEngine/JIT/TargetSelect.cpp	(revision 41642)
+++ lib/ExecutionEngine/JIT/TargetSelect.cpp	(working copy)
@@ -20,7 +20,8 @@
 #include "llvm/Target/TargetMachineRegistry.h"
 using namespace llvm;
 
-static cl::opt<const TargetMachineRegistry::Entry*, false, TargetNameParser>
+static cl::opt<const TargetMachineRegistry::entry*, false,
+               TargetMachineRegistry::Parser>
 MArch("march", cl::desc("Architecture to generate assembly for:"));
 
 static cl::opt<std::string>
@@ -39,7 +40,7 @@
 /// for the current target.  Otherwise, return null.
 ///
 ExecutionEngine *JIT::create(ModuleProvider *MP, std::string *ErrorStr) {
-  const TargetMachineRegistry::Entry *TheArch = MArch;
+  const TargetMachineRegistry::entry *TheArch = MArch;
   if (TheArch == 0) {
     std::string Error;
     TheArch = TargetMachineRegistry::getClosestTargetForJIT(Error);
Index: tools/lto/lto.cpp
===================================================================
--- tools/lto/lto.cpp	(revision 41642)
+++ tools/lto/lto.cpp	(working copy)
@@ -217,7 +217,7 @@
     return;
 
   std::string Err;
-  const TargetMachineRegistry::Entry* March = 
+  const TargetMachineRegistry::entry* March = 
     TargetMachineRegistry::getClosestStaticTargetForModule(*M, Err);
   
   if (March == 0)
Index: tools/llc/llc.cpp
===================================================================
--- tools/llc/llc.cpp	(revision 41642)
+++ tools/llc/llc.cpp	(working copy)
@@ -57,7 +57,8 @@
 static cl::opt<std::string>
 TargetTriple("mtriple", cl::desc("Override target triple for module"));
 
-static cl::opt<const TargetMachineRegistry::Entry*, false, TargetNameParser>
+static cl::opt<const TargetMachineRegistry::entry*, false,
+               TargetMachineRegistry::Parser>
 MArch("march", cl::desc("Architecture to generate code for:"));
 
 static cl::opt<std::string>
