Revert 75762, 75763, 75766..75769, 75772..75775, 75778, 75780, 75782 to repair broken LLVM-GCC build.
Will revert 75770 in the llvm-gcc trunk.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@75799 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Target/ARM/ARM.h b/lib/Target/ARM/ARM.h
index 1b5b828..f6ae680 100644
--- a/lib/Target/ARM/ARM.h
+++ b/lib/Target/ARM/ARM.h
@@ -94,7 +94,7 @@
 
 FunctionPass *createARMISelDag(ARMBaseTargetMachine &TM);
 FunctionPass *createARMCodePrinterPass(formatted_raw_ostream &O,
-                                       TargetMachine &TM,
+                                       ARMBaseTargetMachine &TM,
                                        bool Verbose);
 FunctionPass *createARMCodeEmitterPass(ARMBaseTargetMachine &TM,
                                        MachineCodeEmitter &MCE);
diff --git a/lib/Target/ARM/ARMTargetMachine.cpp b/lib/Target/ARM/ARMTargetMachine.cpp
index ad80020..932659d 100644
--- a/lib/Target/ARM/ARMTargetMachine.cpp
+++ b/lib/Target/ARM/ARMTargetMachine.cpp
@@ -36,11 +36,8 @@
 int ARMTargetMachineModule = 0;
 
 // Register the target.
-extern Target TheARMTarget;
-static RegisterTarget<ARMTargetMachine>   X(TheARMTarget, "arm",   "ARM");
-
-extern Target TheThumbTarget;
-static RegisterTarget<ThumbTargetMachine> Y(TheThumbTarget, "thumb", "Thumb");
+static RegisterTarget<ARMTargetMachine>   X("arm",   "ARM");
+static RegisterTarget<ThumbTargetMachine> Y("thumb", "Thumb");
 
 // Force static initialization.
 extern "C" void LLVMInitializeARMTarget() { }
@@ -48,32 +45,57 @@
 // No assembler printer by default
 ARMBaseTargetMachine::AsmPrinterCtorFn ARMBaseTargetMachine::AsmPrinterCtor = 0;
 
+/// ThumbTargetMachine - Create an Thumb architecture model.
+///
+unsigned ThumbTargetMachine::getJITMatchQuality() {
+#if defined(__thumb__)
+  return 10;
+#endif
+  return 0;
+}
+
+unsigned ThumbTargetMachine::getModuleMatchQuality(const Module &M) {
+  std::string TT = M.getTargetTriple();
+  // Match thumb-foo-bar, as well as things like thumbv5blah-*
+  if (TT.size() >= 6 &&
+      (TT.substr(0, 6) == "thumb-" || TT.substr(0, 6) == "thumbv"))
+    return 20;
+
+  // If the target triple is something non-thumb, we don't match.
+  if (!TT.empty()) return 0;
+
+  if (M.getEndianness()  == Module::LittleEndian &&
+      M.getPointerSize() == Module::Pointer32)
+    return 10;                                   // Weak match
+  else if (M.getEndianness() != Module::AnyEndianness ||
+           M.getPointerSize() != Module::AnyPointerSize)
+    return 0;                                    // Match for some other target
+
+  return getJITMatchQuality()/2;
+}
+
 /// TargetMachine ctor - Create an ARM architecture model.
 ///
-ARMBaseTargetMachine::ARMBaseTargetMachine(const Target &T,
-                                           const Module &M,
+ARMBaseTargetMachine::ARMBaseTargetMachine(const Module &M,
                                            const std::string &FS,
                                            bool isThumb)
-  : LLVMTargetMachine(T),
-    Subtarget(M, FS, isThumb),
+  : Subtarget(M, FS, isThumb),
     FrameInfo(Subtarget),
     JITInfo(),
     InstrItins(Subtarget.getInstrItineraryData()) {
   DefRelocModel = getRelocationModel();
 }
 
-ARMTargetMachine::ARMTargetMachine(const Target &T, const Module &M, 
-                                   const std::string &FS)
-  : ARMBaseTargetMachine(T, M, FS, false), InstrInfo(Subtarget),
+ARMTargetMachine::ARMTargetMachine(const Module &M, const std::string &FS)
+  : ARMBaseTargetMachine(M, FS, false), InstrInfo(Subtarget),
     DataLayout(Subtarget.isAPCS_ABI() ?
                std::string("e-p:32:32-f64:32:32-i64:32:32") :
                std::string("e-p:32:32-f64:64:64-i64:64:64")),
     TLInfo(*this) {
 }
 
-ThumbTargetMachine::ThumbTargetMachine(const Target &T, const Module &M, 
-                                       const std::string &FS)
-  : ARMBaseTargetMachine(T, M, FS, true),
+ThumbTargetMachine::ThumbTargetMachine(const Module &M, const std::string &FS)
+  : ARMBaseTargetMachine(M, FS, true),
     DataLayout(Subtarget.isAPCS_ABI() ?
                std::string("e-p:32:32-f64:32:32-i64:32:32-"
                            "i16:16:32-i8:8:32-i1:8:32-a:0:32") :
@@ -87,6 +109,32 @@
     InstrInfo = new Thumb1InstrInfo(Subtarget);
 }
 
+unsigned ARMTargetMachine::getJITMatchQuality() {
+#if defined(__arm__)
+  return 10;
+#endif
+  return 0;
+}
+
+unsigned ARMTargetMachine::getModuleMatchQuality(const Module &M) {
+  std::string TT = M.getTargetTriple();
+  // Match arm-foo-bar, as well as things like armv5blah-*
+  if (TT.size() >= 4 &&
+      (TT.substr(0, 4) == "arm-" || TT.substr(0, 4) == "armv"))
+    return 20;
+  // If the target triple is something non-arm, we don't match.
+  if (!TT.empty()) return 0;
+
+  if (M.getEndianness()  == Module::LittleEndian &&
+      M.getPointerSize() == Module::Pointer32)
+    return 10;                                   // Weak match
+  else if (M.getEndianness() != Module::AnyEndianness ||
+           M.getPointerSize() != Module::AnyPointerSize)
+    return 0;                                    // Match for some other target
+
+  return getJITMatchQuality()/2;
+}
+
 
 const TargetAsmInfo *ARMBaseTargetMachine::createTargetAsmInfo() const {
   switch (Subtarget.TargetType) {
diff --git a/lib/Target/ARM/ARMTargetMachine.h b/lib/Target/ARM/ARMTargetMachine.h
index 3fe259a..56b18ae 100644
--- a/lib/Target/ARM/ARMTargetMachine.h
+++ b/lib/Target/ARM/ARMTargetMachine.h
@@ -42,13 +42,12 @@
   // To avoid having target depend on the asmprinter stuff libraries, asmprinter
   // set this functions to ctor pointer at startup time if they are linked in.
   typedef FunctionPass *(*AsmPrinterCtorFn)(formatted_raw_ostream &o,
-                                            TargetMachine &tm,
+                                            ARMBaseTargetMachine &tm,
                                             bool verbose);
   static AsmPrinterCtorFn AsmPrinterCtor;
 
 public:
-  ARMBaseTargetMachine(const Target &T, const Module &M, const std::string &FS, 
-                       bool isThumb);
+  ARMBaseTargetMachine(const Module &M, const std::string &FS, bool isThumb);
 
   virtual const ARMFrameInfo     *getFrameInfo() const { return &FrameInfo; }
   virtual       ARMJITInfo       *getJITInfo()         { return &JITInfo; }
@@ -61,6 +60,9 @@
     AsmPrinterCtor = F;
   }
 
+  static unsigned getModuleMatchQuality(const Module &M);
+  static unsigned getJITMatchQuality();
+
   virtual const TargetAsmInfo *createTargetAsmInfo() const;
 
   // Pass Pipeline Configuration
@@ -97,7 +99,7 @@
   const TargetData    DataLayout;       // Calculates type size & alignment
   ARMTargetLowering   TLInfo;
 public:
-  ARMTargetMachine(const Target &T, const Module &M, const std::string &FS);
+  ARMTargetMachine(const Module &M, const std::string &FS);
 
   virtual const ARMRegisterInfo  *getRegisterInfo() const {
     return &InstrInfo.getRegisterInfo();
@@ -123,7 +125,7 @@
   const TargetData    DataLayout;   // Calculates type size & alignment
   ARMTargetLowering   TLInfo;
 public:
-  ThumbTargetMachine(const Target &T, const Module &M, const std::string &FS);
+  ThumbTargetMachine(const Module &M, const std::string &FS);
 
   /// returns either Thumb1RegisterInfo of Thumb2RegisterInfo
   virtual const ARMBaseRegisterInfo *getRegisterInfo() const {
diff --git a/lib/Target/ARM/AsmPrinter/ARMAsmPrinter.cpp b/lib/Target/ARM/AsmPrinter/ARMAsmPrinter.cpp
index 098f5d3..de6adbd 100644
--- a/lib/Target/ARM/AsmPrinter/ARMAsmPrinter.cpp
+++ b/lib/Target/ARM/AsmPrinter/ARMAsmPrinter.cpp
@@ -31,7 +31,6 @@
 #include "llvm/Target/TargetData.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Target/TargetOptions.h"
-#include "llvm/Target/TargetRegistry.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringSet.h"
@@ -1288,7 +1287,7 @@
 /// regardless of whether the function is in SSA form.
 ///
 FunctionPass *llvm::createARMCodePrinterPass(formatted_raw_ostream &o,
-                                             TargetMachine &tm,
+                                             ARMBaseTargetMachine &tm,
                                              bool verbose) {
   return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
 }
@@ -1302,8 +1301,4 @@
 }
 
 // Force static initialization.
-extern "C" void LLVMInitializeARMAsmPrinter() { 
-  extern Target TheARMTarget, TheThumbTarget;
-  TargetRegistry::RegisterAsmPrinter(TheARMTarget, createARMCodePrinterPass);
-  TargetRegistry::RegisterAsmPrinter(TheThumbTarget, createARMCodePrinterPass);
-}
+extern "C" void LLVMInitializeARMAsmPrinter() { }
diff --git a/lib/Target/Alpha/AlphaTargetMachine.cpp b/lib/Target/Alpha/AlphaTargetMachine.cpp
index a7a8162..625d0ce 100644
--- a/lib/Target/Alpha/AlphaTargetMachine.cpp
+++ b/lib/Target/Alpha/AlphaTargetMachine.cpp
@@ -22,9 +22,7 @@
 using namespace llvm;
 
 // Register the targets
-extern Target TheAlphaTarget;
-static RegisterTarget<AlphaTargetMachine> X(TheAlphaTarget, "alpha", 
-                                            "Alpha [experimental]");
+static RegisterTarget<AlphaTargetMachine> X("alpha", "Alpha [experimental]");
 
 // No assembler printer by default
 AlphaTargetMachine::AsmPrinterCtorFn AlphaTargetMachine::AsmPrinterCtor = 0;
@@ -36,10 +34,35 @@
   return new AlphaTargetAsmInfo(*this);
 }
 
-AlphaTargetMachine::AlphaTargetMachine(const Target &T, const Module &M, 
-                                       const std::string &FS)
-  : LLVMTargetMachine(T),
-    DataLayout("e-f128:128:128"),
+unsigned AlphaTargetMachine::getModuleMatchQuality(const Module &M) {
+  // We strongly match "alpha*".
+  std::string TT = M.getTargetTriple();
+  if (TT.size() >= 5 && TT[0] == 'a' && TT[1] == 'l' && TT[2] == 'p' &&
+      TT[3] == 'h' && TT[4] == 'a')
+    return 20;
+  // If the target triple is something non-alpha, we don't match.
+  if (!TT.empty()) return 0;
+
+  if (M.getEndianness()  == Module::LittleEndian &&
+      M.getPointerSize() == Module::Pointer64)
+    return 10;                                   // Weak match
+  else if (M.getEndianness() != Module::AnyEndianness ||
+           M.getPointerSize() != Module::AnyPointerSize)
+    return 0;                                    // Match for some other target
+
+  return getJITMatchQuality()/2;
+}
+
+unsigned AlphaTargetMachine::getJITMatchQuality() {
+#ifdef __alpha
+  return 10;
+#else
+  return 0;
+#endif
+}
+
+AlphaTargetMachine::AlphaTargetMachine(const Module &M, const std::string &FS)
+  : DataLayout("e-f128:128:128"),
     FrameInfo(TargetFrameInfo::StackGrowsDown, 16, 0),
     JITInfo(*this),
     Subtarget(M, FS),
diff --git a/lib/Target/Alpha/AlphaTargetMachine.h b/lib/Target/Alpha/AlphaTargetMachine.h
index 6125ce5..7546892 100644
--- a/lib/Target/Alpha/AlphaTargetMachine.h
+++ b/lib/Target/Alpha/AlphaTargetMachine.h
@@ -45,7 +45,7 @@
   static AsmPrinterCtorFn AsmPrinterCtor;
 
 public:
-  AlphaTargetMachine(const Target &T, const Module &M, const std::string &FS);
+  AlphaTargetMachine(const Module &M, const std::string &FS);
 
   virtual const AlphaInstrInfo *getInstrInfo() const { return &InstrInfo; }
   virtual const TargetFrameInfo  *getFrameInfo() const { return &FrameInfo; }
@@ -61,6 +61,9 @@
     return &JITInfo;
   }
 
+  static unsigned getJITMatchQuality();
+  static unsigned getModuleMatchQuality(const Module &M);
+
   // Pass Pipeline Configuration
   virtual bool addInstSelector(PassManagerBase &PM, CodeGenOpt::Level OptLevel);
   virtual bool addPreEmitPass(PassManagerBase &PM, CodeGenOpt::Level OptLevel);
diff --git a/lib/Target/Alpha/AsmPrinter/AlphaAsmPrinter.cpp b/lib/Target/Alpha/AsmPrinter/AlphaAsmPrinter.cpp
index ea810df..21622db 100644
--- a/lib/Target/Alpha/AsmPrinter/AlphaAsmPrinter.cpp
+++ b/lib/Target/Alpha/AsmPrinter/AlphaAsmPrinter.cpp
@@ -24,7 +24,6 @@
 #include "llvm/CodeGen/DwarfWriter.h"
 #include "llvm/Target/TargetAsmInfo.h"
 #include "llvm/Target/TargetMachine.h"
-#include "llvm/Target/TargetRegistry.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/Mangler.h"
@@ -293,6 +292,9 @@
   return false;
 }
 
+// Force static initialization.
+extern "C" void LLVMInitializeAlphaAsmPrinter() { }
+
 namespace {
   static struct Register {
     Register() {
@@ -300,10 +302,3 @@
     }
   } Registrator;
 }
-
-// Force static initialization.
-extern "C" void LLVMInitializeAlphaAsmPrinter() { 
-  extern Target TheAlphaTarget;
-  TargetRegistry::RegisterAsmPrinter(TheAlphaTarget,
-                                     createAlphaCodePrinterPass);
-}
diff --git a/lib/Target/CBackend/CBackend.cpp b/lib/Target/CBackend/CBackend.cpp
index b1361e8..caf91c8 100644
--- a/lib/Target/CBackend/CBackend.cpp
+++ b/lib/Target/CBackend/CBackend.cpp
@@ -30,10 +30,9 @@
 #include "llvm/CodeGen/Passes.h"
 #include "llvm/CodeGen/IntrinsicLowering.h"
 #include "llvm/Transforms/Scalar.h"
+#include "llvm/Target/TargetMachineRegistry.h"
 #include "llvm/Target/TargetAsmInfo.h"
 #include "llvm/Target/TargetData.h"
-#include "llvm/Target/TargetMachineRegistry.h"
-#include "llvm/Target/TargetRegistry.h"
 #include "llvm/Support/CallSite.h"
 #include "llvm/Support/CFG.h"
 #include "llvm/Support/ErrorHandling.h"
@@ -59,8 +58,7 @@
 int CBackendTargetMachineModule = 0;
 
 // Register the target.
-extern Target TheCBackendTarget;
-static RegisterTarget<CTargetMachine> X(TheCBackendTarget, "c", "C backend");
+static RegisterTarget<CTargetMachine> X("c", "C backend");
 
 // Force static initialization.
 extern "C" void LLVMInitializeCBackendTarget() { }
@@ -3188,27 +3186,27 @@
 
   const char *const *table = 0;
   
-  // Grab the translation table from TargetAsmInfo if it exists.
+  //Grab the translation table from TargetAsmInfo if it exists
   if (!TAsm) {
     std::string E;
-    const Target *Match =
-      TargetRegistry::getClosestStaticTargetForModule(*TheModule, E);
+    const TargetMachineRegistry::entry* Match = 
+      TargetMachineRegistry::getClosestStaticTargetForModule(*TheModule, E);
     if (Match) {
-      // Per platform Target Machines don't exist, so create it;
-      // this must be done only once.
-      const TargetMachine* TM = Match->createTargetMachine(*TheModule, "");
+      //Per platform Target Machines don't exist, so create it
+      // this must be done only once
+      const TargetMachine* TM = Match->CtorFn(*TheModule, "");
       TAsm = TM->getTargetAsmInfo();
     }
   }
   if (TAsm)
     table = TAsm->getAsmCBE();
 
-  // Search the translation table if it exists.
+  //Search the translation table if it exists
   for (int i = 0; table && table[i]; i += 2)
     if (c.Codes[0] == table[i])
       return table[i+1];
 
-  // Default is identity.
+  //default is identity
   return c.Codes[0];
 }
 
diff --git a/lib/Target/CBackend/CTargetMachine.h b/lib/Target/CBackend/CTargetMachine.h
index b1d9e07..64b373b 100644
--- a/lib/Target/CBackend/CTargetMachine.h
+++ b/lib/Target/CBackend/CTargetMachine.h
@@ -22,14 +22,18 @@
 struct CTargetMachine : public TargetMachine {
   const TargetData DataLayout;       // Calculates type size & alignment
 
-  CTargetMachine(const Target &T, const Module &M, const std::string &FS)
-    : TargetMachine(T), DataLayout(&M) {}
+  CTargetMachine(const Module &M, const std::string &FS)
+    : DataLayout(&M) {}
 
   virtual bool WantsWholeFile() const { return true; }
   virtual bool addPassesToEmitWholeFile(PassManager &PM,
                                         formatted_raw_ostream &Out,
                                         CodeGenFileType FileType,
                                         CodeGenOpt::Level OptLevel);
+
+  // This class always works, but must be requested explicitly on 
+  // llc command line.
+  static unsigned getModuleMatchQuality(const Module &M) { return 0; }
   
   virtual const TargetData *getTargetData() const { return &DataLayout; }
 };
diff --git a/lib/Target/CellSPU/AsmPrinter/SPUAsmPrinter.cpp b/lib/Target/CellSPU/AsmPrinter/SPUAsmPrinter.cpp
index 0c36457..f0d8a36 100644
--- a/lib/Target/CellSPU/AsmPrinter/SPUAsmPrinter.cpp
+++ b/lib/Target/CellSPU/AsmPrinter/SPUAsmPrinter.cpp
@@ -37,7 +37,6 @@
 #include "llvm/Target/TargetRegisterInfo.h"
 #include "llvm/Target/TargetInstrInfo.h"
 #include "llvm/Target/TargetOptions.h"
-#include "llvm/Target/TargetRegistry.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/ADT/StringExtras.h"
 #include <set>
@@ -290,7 +289,7 @@
   class VISIBILITY_HIDDEN LinuxAsmPrinter : public SPUAsmPrinter {
     DwarfWriter *DW;
   public:
-    explicit LinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
+    explicit LinuxAsmPrinter(formatted_raw_ostream &O, SPUTargetMachine &TM,
                              const TargetAsmInfo *T, bool V)
       : SPUAsmPrinter(O, TM, T, V), DW(0) {}
 
@@ -600,11 +599,14 @@
 /// that the Linux SPU assembler can deal with.
 ///
 FunctionPass *llvm::createSPUAsmPrinterPass(formatted_raw_ostream &o,
-                                            TargetMachine &tm,
+                                            SPUTargetMachine &tm,
                                             bool verbose) {
   return new LinuxAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
 }
 
+// Force static initialization.
+extern "C" void LLVMInitializeCellSPUAsmPrinter() { }
+
 namespace {
   static struct Register {
     Register() {
@@ -612,9 +614,3 @@
     }
   } Registrator;
 }
-
-// Force static initialization.
-extern "C" void LLVMInitializeCellSPUAsmPrinter() { 
-  extern Target TheCellSPUTarget;
-  TargetRegistry::RegisterAsmPrinter(TheCellSPUTarget, createSPUAsmPrinterPass);
-}
diff --git a/lib/Target/CellSPU/SPU.h b/lib/Target/CellSPU/SPU.h
index f76fc82..0262348 100644
--- a/lib/Target/CellSPU/SPU.h
+++ b/lib/Target/CellSPU/SPU.h
@@ -25,7 +25,7 @@
 
   FunctionPass *createSPUISelDag(SPUTargetMachine &TM);
   FunctionPass *createSPUAsmPrinterPass(formatted_raw_ostream &o,
-                                        TargetMachine &tm,
+                                        SPUTargetMachine &tm,
                                         bool verbose);
 
   /*--== Utility functions/predicates/etc used all over the place: --==*/
diff --git a/lib/Target/CellSPU/SPUTargetMachine.cpp b/lib/Target/CellSPU/SPUTargetMachine.cpp
index 3a659d8..5c794c9 100644
--- a/lib/Target/CellSPU/SPUTargetMachine.cpp
+++ b/lib/Target/CellSPU/SPUTargetMachine.cpp
@@ -23,11 +23,10 @@
 
 using namespace llvm;
 
-extern Target TheCellSPUTarget;
 namespace {
   // Register the targets
   RegisterTarget<SPUTargetMachine>
-  CELLSPU(TheCellSPUTarget, "cellspu", "STI CBEA Cell SPU [experimental]");
+  CELLSPU("cellspu", "STI CBEA Cell SPU [experimental]");
 }
 
 // No assembler printer by default
@@ -48,10 +47,22 @@
   return new SPULinuxTargetAsmInfo(*this);
 }
 
-SPUTargetMachine::SPUTargetMachine(const Target &T, const Module &M, 
-                                   const std::string &FS)
-  : LLVMTargetMachine(T),
-    Subtarget(*this, M, FS),
+unsigned
+SPUTargetMachine::getModuleMatchQuality(const Module &M)
+{
+  // We strongly match "spu-*" or "cellspu-*".
+  std::string TT = M.getTargetTriple();
+  if ((TT.size() == 3 && std::string(TT.begin(), TT.begin()+3) == "spu")
+      || (TT.size() == 7 && std::string(TT.begin(), TT.begin()+7) == "cellspu")
+      || (TT.size() >= 4 && std::string(TT.begin(), TT.begin()+4) == "spu-")
+      || (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "cellspu-"))
+    return 20;
+  
+  return 0;                     // No match at all...
+}
+
+SPUTargetMachine::SPUTargetMachine(const Module &M, const std::string &FS)
+  : Subtarget(*this, M, FS),
     DataLayout(Subtarget.getTargetDataString()),
     InstrInfo(*this),
     FrameInfo(*this),
diff --git a/lib/Target/CellSPU/SPUTargetMachine.h b/lib/Target/CellSPU/SPUTargetMachine.h
index 18f525d..ce8e550 100644
--- a/lib/Target/CellSPU/SPUTargetMachine.h
+++ b/lib/Target/CellSPU/SPUTargetMachine.h
@@ -42,12 +42,12 @@
   // To avoid having target depend on the asmprinter stuff libraries, asmprinter
   // set this functions to ctor pointer at startup time if they are linked in.
   typedef FunctionPass *(*AsmPrinterCtorFn)(formatted_raw_ostream &o,
-                                            TargetMachine &tm,
+                                            SPUTargetMachine &tm,
                                             bool verbose);
   static AsmPrinterCtorFn AsmPrinterCtor;
 
 public:
-  SPUTargetMachine(const Target &T, const Module &M, const std::string &FS);
+  SPUTargetMachine(const Module &M, const std::string &FS);
 
   /// Return the subtarget implementation object
   virtual const SPUSubtarget     *getSubtargetImpl() const {
@@ -66,6 +66,12 @@
   virtual       TargetJITInfo    *getJITInfo() {
     return NULL;
   }
+  
+  //! Module match function
+  /*!
+    Module matching function called by TargetMachineRegistry().
+   */
+  static unsigned getModuleMatchQuality(const Module &M);
 
   virtual       SPUTargetLowering *getTargetLowering() const { 
    return const_cast<SPUTargetLowering*>(&TLInfo); 
diff --git a/lib/Target/CppBackend/CPPBackend.cpp b/lib/Target/CppBackend/CPPBackend.cpp
index 7b2995f..b2d72dd 100644
--- a/lib/Target/CppBackend/CPPBackend.cpp
+++ b/lib/Target/CppBackend/CPPBackend.cpp
@@ -81,8 +81,7 @@
 int CppBackendTargetMachineModule = 0;
 
 // Register the target.
-extern Target TheCppBackendTarget;
-static RegisterTarget<CPPTargetMachine> X(TheCppBackendTarget, "cpp", "C++ backend");
+static RegisterTarget<CPPTargetMachine> X("cpp", "C++ backend");
 
 // Force static initialization.
 extern "C" void LLVMInitializeCppBackendTarget() { }
diff --git a/lib/Target/CppBackend/CPPTargetMachine.h b/lib/Target/CppBackend/CPPTargetMachine.h
index 4d6d5fe..84a07ea 100644
--- a/lib/Target/CppBackend/CPPTargetMachine.h
+++ b/lib/Target/CppBackend/CPPTargetMachine.h
@@ -24,8 +24,8 @@
 struct CPPTargetMachine : public TargetMachine {
   const TargetData DataLayout;       // Calculates type size & alignment
 
-  CPPTargetMachine(const Target &T, const Module &M, const std::string &FS)
-    : TargetMachine(T), DataLayout(&M) {}
+  CPPTargetMachine(const Module &M, const std::string &FS)
+    : DataLayout(&M) {}
 
   virtual bool WantsWholeFile() const { return true; }
   virtual bool addPassesToEmitWholeFile(PassManager &PM,
@@ -33,6 +33,9 @@
                                         CodeGenFileType FileType,
                                         CodeGenOpt::Level OptLevel);
 
+  // This class always works, but shouldn't be the default in most cases.
+  static unsigned getModuleMatchQuality(const Module &M) { return 1; }
+
   virtual const TargetData *getTargetData() const { return &DataLayout; }
 };
 
diff --git a/lib/Target/IA64/AsmPrinter/IA64AsmPrinter.cpp b/lib/Target/IA64/AsmPrinter/IA64AsmPrinter.cpp
index e5eedad..b8aa37e 100644
--- a/lib/Target/IA64/AsmPrinter/IA64AsmPrinter.cpp
+++ b/lib/Target/IA64/AsmPrinter/IA64AsmPrinter.cpp
@@ -26,7 +26,6 @@
 #include "llvm/CodeGen/DwarfWriter.h"
 #include "llvm/CodeGen/MachineFunctionPass.h"
 #include "llvm/Target/TargetAsmInfo.h"
-#include "llvm/Target/TargetRegistry.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/FormattedStream.h"
 #include "llvm/Support/Mangler.h"
@@ -370,7 +369,7 @@
 /// the given target machine description.
 ///
 FunctionPass *llvm::createIA64CodePrinterPass(formatted_raw_ostream &o,
-                                              TargetMachine &tm,
+                                              IA64TargetMachine &tm,
                                               bool verbose) {
   return new IA64AsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
 }
@@ -385,7 +384,4 @@
 
 
 // Force static initialization.
-extern "C" void LLVMInitializeIA64AsmPrinter() { 
-  extern Target TheIA64Target;
-  TargetRegistry::RegisterAsmPrinter(TheIA64Target, createIA64CodePrinterPass);
-}
+extern "C" void LLVMInitializeIA64AsmPrinter() { }
diff --git a/lib/Target/IA64/IA64.h b/lib/Target/IA64/IA64.h
index e45aeda..afcbdaf 100644
--- a/lib/Target/IA64/IA64.h
+++ b/lib/Target/IA64/IA64.h
@@ -38,7 +38,7 @@
 /// regardless of whether the function is in SSA form.
 ///
 FunctionPass *createIA64CodePrinterPass(formatted_raw_ostream &o,
-                                        TargetMachine &tm,
+                                        IA64TargetMachine &tm,
                                         bool verbose);
 
 } // End llvm namespace
diff --git a/lib/Target/IA64/IA64TargetMachine.cpp b/lib/Target/IA64/IA64TargetMachine.cpp
index 5355075..00fdd5e 100644
--- a/lib/Target/IA64/IA64TargetMachine.cpp
+++ b/lib/Target/IA64/IA64TargetMachine.cpp
@@ -20,8 +20,7 @@
 using namespace llvm;
 
 // Register the target
-extern Target TheIA64Target;
-static RegisterTarget<IA64TargetMachine> X(TheIA64Target, "ia64",
+static RegisterTarget<IA64TargetMachine> X("ia64",
                                            "IA-64 (Itanium) [experimental]");
 
 // No assembler printer by default
@@ -34,12 +33,36 @@
   return new IA64TargetAsmInfo(*this);
 }
 
+unsigned IA64TargetMachine::getModuleMatchQuality(const Module &M) {
+  // we match [iI][aA]*64
+  bool seenIA64=false;
+  std::string TT = M.getTargetTriple();
+
+  if (TT.size() >= 4) {
+    if( (TT[0]=='i' || TT[0]=='I') &&
+        (TT[1]=='a' || TT[1]=='A') ) {
+      for(unsigned int i=2; i<(TT.size()-1); i++)
+        if(TT[i]=='6' && TT[i+1]=='4')
+          seenIA64=true;
+    }
+
+    if (seenIA64)
+      return 20; // strong match
+  }
+  // If the target triple is something non-ia64, we don't match.
+  if (!TT.empty()) return 0;
+
+#if defined(__ia64__) || defined(__IA64__)
+  return 5;
+#else
+  return 0;
+#endif
+}
+
 /// IA64TargetMachine ctor - Create an LP64 architecture model
 ///
-IA64TargetMachine::IA64TargetMachine(const Target &T, const Module &M, 
-                                     const std::string &FS)
-  : LLVMTargetMachine(T),
-    DataLayout("e-f80:128:128"),
+IA64TargetMachine::IA64TargetMachine(const Module &M, const std::string &FS)
+  : DataLayout("e-f80:128:128"),
     FrameInfo(TargetFrameInfo::StackGrowsDown, 16, 0),
     TLInfo(*this) { // FIXME? check this stuff
 }
diff --git a/lib/Target/IA64/IA64TargetMachine.h b/lib/Target/IA64/IA64TargetMachine.h
index e82bf59..8340052 100644
--- a/lib/Target/IA64/IA64TargetMachine.h
+++ b/lib/Target/IA64/IA64TargetMachine.h
@@ -37,12 +37,12 @@
   // To avoid having target depend on the asmprinter stuff libraries, asmprinter
   // set this functions to ctor pointer at startup time if they are linked in.
   typedef FunctionPass *(*AsmPrinterCtorFn)(formatted_raw_ostream &o,
-                                            TargetMachine &tm,
+                                            IA64TargetMachine &tm,
                                             bool verbose);
   static AsmPrinterCtorFn AsmPrinterCtor;
 
 public:
-  IA64TargetMachine(const Target &T, const Module &M, const std::string &FS);
+  IA64TargetMachine(const Module &M, const std::string &FS);
 
   virtual const IA64InstrInfo      *getInstrInfo() const { return &InstrInfo; }
   virtual const TargetFrameInfo    *getFrameInfo() const { return &FrameInfo; }
@@ -55,6 +55,8 @@
   }
   virtual const TargetData       *getTargetData() const { return &DataLayout; }
 
+  static unsigned getModuleMatchQuality(const Module &M);
+
   // Pass Pipeline Configuration
   virtual bool addInstSelector(PassManagerBase &PM, CodeGenOpt::Level OptLevel);
   virtual bool addPreEmitPass(PassManagerBase &PM, CodeGenOpt::Level OptLevel);
diff --git a/lib/Target/MSIL/MSILWriter.cpp b/lib/Target/MSIL/MSILWriter.cpp
index bedf403..2ae1d4f 100644
--- a/lib/Target/MSIL/MSILWriter.cpp
+++ b/lib/Target/MSIL/MSILWriter.cpp
@@ -31,8 +31,8 @@
   struct VISIBILITY_HIDDEN MSILTarget : public TargetMachine {
     const TargetData DataLayout;       // Calculates type size & alignment
 
-    MSILTarget(const Target &T, const Module &M, const std::string &FS)
-      : TargetMachine(T), DataLayout(&M) {}
+    MSILTarget(const Module &M, const std::string &FS)
+      : DataLayout(&M) {}
 
     virtual bool WantsWholeFile() const { return true; }
     virtual bool addPassesToEmitWholeFile(PassManager &PM,
@@ -55,8 +55,7 @@
 extern "C" int MSILTargetMachineModule;
 int MSILTargetMachineModule = 0;
 
-extern Target TheMSILTarget;
-static RegisterTarget<MSILTarget> X(TheMSILTarget, "msil", "MSIL backend");
+static RegisterTarget<MSILTarget> X("msil", "MSIL backend");
 
 // Force static initialization.
 extern "C" void LLVMInitializeMSILTarget() { }
diff --git a/lib/Target/MSP430/MSP430TargetMachine.cpp b/lib/Target/MSP430/MSP430TargetMachine.cpp
index bf259e2..924c7e8 100644
--- a/lib/Target/MSP430/MSP430TargetMachine.cpp
+++ b/lib/Target/MSP430/MSP430TargetMachine.cpp
@@ -32,17 +32,14 @@
 
 
 // Register the targets
-extern Target TheMSP430Target;
 static RegisterTarget<MSP430TargetMachine>
-X(TheMSP430Target, "msp430", "MSP430 [experimental]");
+X("msp430", "MSP430 [experimental]");
 
 // Force static initialization.
 extern "C" void LLVMInitializeMSP430Target() { }
 
-MSP430TargetMachine::MSP430TargetMachine(const Target &T,
-                                         const Module &M,
+MSP430TargetMachine::MSP430TargetMachine(const Module &M,
                                          const std::string &FS) :
-  LLVMTargetMachine(T),
   Subtarget(*this, M, FS),
   // FIXME: Check TargetData string.
   DataLayout("e-p:16:8:8-i8:8:8-i16:8:8-i32:8:8"),
@@ -69,3 +66,14 @@
   return false;
 }
 
+unsigned MSP430TargetMachine::getModuleMatchQuality(const Module &M) {
+  std::string TT = M.getTargetTriple();
+
+  // We strongly match msp430
+  if (TT.size() >= 6 && TT[0] == 'm' && TT[1] == 's' && TT[2] == 'p' &&
+      TT[3] == '4' &&  TT[4] == '3' && TT[5] == '0')
+    return 20;
+
+  return 0;
+}
+
diff --git a/lib/Target/MSP430/MSP430TargetMachine.h b/lib/Target/MSP430/MSP430TargetMachine.h
index dfe827b..0a5c64e 100644
--- a/lib/Target/MSP430/MSP430TargetMachine.h
+++ b/lib/Target/MSP430/MSP430TargetMachine.h
@@ -41,7 +41,7 @@
   virtual const TargetAsmInfo *createTargetAsmInfo() const;
 
 public:
-  MSP430TargetMachine(const Target &T, const Module &M, const std::string &FS);
+  MSP430TargetMachine(const Module &M, const std::string &FS);
 
   virtual const TargetFrameInfo *getFrameInfo() const { return &FrameInfo; }
   virtual const MSP430InstrInfo *getInstrInfo() const  { return &InstrInfo; }
@@ -60,6 +60,7 @@
   virtual bool addAssemblyEmitter(PassManagerBase &PM,
                                   CodeGenOpt::Level OptLevel, bool Verbose,
                                   formatted_raw_ostream &Out);
+  static unsigned getModuleMatchQuality(const Module &M);
 }; // MSP430TargetMachine.
 
 } // end namespace llvm
diff --git a/lib/Target/Mips/AsmPrinter/MipsAsmPrinter.cpp b/lib/Target/Mips/AsmPrinter/MipsAsmPrinter.cpp
index 501dc6a..719c2586 100644
--- a/lib/Target/Mips/AsmPrinter/MipsAsmPrinter.cpp
+++ b/lib/Target/Mips/AsmPrinter/MipsAsmPrinter.cpp
@@ -33,7 +33,6 @@
 #include "llvm/Target/TargetData.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Target/TargetOptions.h"
-#include "llvm/Target/TargetRegistry.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/Mangler.h"
 #include "llvm/ADT/Statistic.h"
@@ -52,7 +51,7 @@
   class VISIBILITY_HIDDEN MipsAsmPrinter : public AsmPrinter {
     const MipsSubtarget *Subtarget;
   public:
-    explicit MipsAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM, 
+    explicit MipsAsmPrinter(formatted_raw_ostream &O, MipsTargetMachine &TM, 
                             const TargetAsmInfo *T, bool V)
       : AsmPrinter(O, TM, T, V) {
       Subtarget = &TM.getSubtarget<MipsSubtarget>();
@@ -93,7 +92,7 @@
 /// using the given target machine description.  This should work
 /// regardless of whether the function is in SSA form.
 FunctionPass *llvm::createMipsCodePrinterPass(formatted_raw_ostream &o,
-                                              TargetMachine &tm,
+                                              MipsTargetMachine &tm,
                                               bool verbose) {
   return new MipsAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
 }
@@ -585,11 +584,4 @@
 }
 
 // Force static initialization.
-extern "C" void LLVMInitializeMipsAsmPrinter() { 
-  extern Target TheMipsTarget;
-  TargetRegistry::RegisterAsmPrinter(TheMipsTarget, createMipsCodePrinterPass);
-
-  extern Target TheMipselTarget;
-  TargetRegistry::RegisterAsmPrinter(TheMipselTarget, 
-                                     createMipsCodePrinterPass);
-}
+extern "C" void LLVMInitializeMipsAsmPrinter() { }
diff --git a/lib/Target/Mips/Mips.h b/lib/Target/Mips/Mips.h
index 1ff34fb..c1d6fdd 100644
--- a/lib/Target/Mips/Mips.h
+++ b/lib/Target/Mips/Mips.h
@@ -26,7 +26,7 @@
   FunctionPass *createMipsISelDag(MipsTargetMachine &TM);
   FunctionPass *createMipsDelaySlotFillerPass(MipsTargetMachine &TM);
   FunctionPass *createMipsCodePrinterPass(formatted_raw_ostream &OS, 
-                                          TargetMachine &TM,
+                                          MipsTargetMachine &TM,
                                           bool Verbose);
 } // end namespace llvm;
 
diff --git a/lib/Target/Mips/MipsTargetMachine.cpp b/lib/Target/Mips/MipsTargetMachine.cpp
index 3c2693b..1e6add4 100644
--- a/lib/Target/Mips/MipsTargetMachine.cpp
+++ b/lib/Target/Mips/MipsTargetMachine.cpp
@@ -28,12 +28,8 @@
 int MipsTargetMachineModule = 0;
 
 // Register the target.
-extern Target TheMipsTarget;
-static RegisterTarget<MipsTargetMachine>    X(TheMipsTarget, "mips", "Mips");
-
-extern Target TheMipselTarget;
-static RegisterTarget<MipselTargetMachine>  Y(TheMipselTarget, "mipsel", 
-                                              "Mipsel");
+static RegisterTarget<MipsTargetMachine>    X("mips", "Mips");
+static RegisterTarget<MipselTargetMachine>  Y("mipsel", "Mipsel");
 
 MipsTargetMachine::AsmPrinterCtorFn MipsTargetMachine::AsmPrinterCtor = 0;
 
@@ -55,9 +51,7 @@
 // an easier handling.
 // Using CodeModel::Large enables different CALL behavior.
 MipsTargetMachine::
-MipsTargetMachine(const Target &T, const Module &M, const std::string &FS, 
-                  bool isLittle=false):
-  LLVMTargetMachine(T),
+MipsTargetMachine(const Module &M, const std::string &FS, bool isLittle=false):
   Subtarget(*this, M, FS, isLittle), 
   DataLayout(isLittle ? std::string("e-p:32:32:32-i8:8:32-i16:16:32") :
                         std::string("E-p:32:32:32-i8:8:32-i16:16:32")), 
@@ -76,8 +70,43 @@
 }
 
 MipselTargetMachine::
-MipselTargetMachine(const Target &T, const Module &M, const std::string &FS) :
-  MipsTargetMachine(T, M, FS, true) {}
+MipselTargetMachine(const Module &M, const std::string &FS) :
+  MipsTargetMachine(M, FS, true) {}
+
+// return 0 and must specify -march to gen MIPS code.
+unsigned MipsTargetMachine::
+getModuleMatchQuality(const Module &M) 
+{
+  // We strongly match "mips*-*".
+  std::string TT = M.getTargetTriple();
+  if (TT.size() >= 5 && std::string(TT.begin(), TT.begin()+5) == "mips-")
+    return 20;
+  
+  if (TT.size() >= 13 && std::string(TT.begin(), 
+      TT.begin()+13) == "mipsallegrex-")
+    return 20;
+
+  return 0;
+}
+
+// return 0 and must specify -march to gen MIPSEL code.
+unsigned MipselTargetMachine::
+getModuleMatchQuality(const Module &M) 
+{
+  // We strongly match "mips*el-*".
+  std::string TT = M.getTargetTriple();
+  if (TT.size() >= 7 && std::string(TT.begin(), TT.begin()+7) == "mipsel-")
+    return 20;
+
+  if (TT.size() >= 15 && std::string(TT.begin(), 
+      TT.begin()+15) == "mipsallegrexel-")
+    return 20;
+
+  if (TT.size() == 3 && std::string(TT.begin(), TT.begin()+3) == "psp")
+    return 20;
+  
+  return 0;
+}
 
 // Install an instruction selector pass using 
 // the ISelDag to gen Mips code.
diff --git a/lib/Target/Mips/MipsTargetMachine.h b/lib/Target/Mips/MipsTargetMachine.h
index 6e2562a..7dd8d2b 100644
--- a/lib/Target/Mips/MipsTargetMachine.h
+++ b/lib/Target/Mips/MipsTargetMachine.h
@@ -38,13 +38,12 @@
     // asmprinter set this functions to ctor pointer at startup time if they are
     // linked in.
     typedef FunctionPass *(*AsmPrinterCtorFn)(formatted_raw_ostream &o,
-                                              TargetMachine &tm,
+                                              MipsTargetMachine &tm,
                                               bool verbose);
     static AsmPrinterCtorFn AsmPrinterCtor;
     
   public:
-    MipsTargetMachine(const Target &T, const Module &M, const std::string &FS, 
-                      bool isLittle);
+    MipsTargetMachine(const Module &M, const std::string &FS, bool isLittle);
 
     static void registerAsmPrinter(AsmPrinterCtorFn F) {
       AsmPrinterCtor = F;
@@ -67,6 +66,8 @@
       return const_cast<MipsTargetLowering*>(&TLInfo); 
     }
 
+    static unsigned getModuleMatchQuality(const Module &M);
+
     // Pass Pipeline Configuration
     virtual bool addInstSelector(PassManagerBase &PM,
                                  CodeGenOpt::Level OptLevel);
@@ -81,7 +82,7 @@
 ///
 class MipselTargetMachine : public MipsTargetMachine {
 public:
-  MipselTargetMachine(const Target &T, const Module &M, const std::string &FS);
+  MipselTargetMachine(const Module &M, const std::string &FS);
 
   static unsigned getModuleMatchQuality(const Module &M);
 };
diff --git a/lib/Target/PIC16/PIC16TargetMachine.cpp b/lib/Target/PIC16/PIC16TargetMachine.cpp
index 45a61fa..50deab1 100644
--- a/lib/Target/PIC16/PIC16TargetMachine.cpp
+++ b/lib/Target/PIC16/PIC16TargetMachine.cpp
@@ -32,31 +32,26 @@
 
 
 // Register the targets
-extern Target ThePIC16Target;
 static RegisterTarget<PIC16TargetMachine> 
-X(ThePIC16Target, "pic16", "PIC16 14-bit [experimental].");
-
-extern Target TheCooperTarget;
+X("pic16", "PIC16 14-bit [experimental].");
 static RegisterTarget<CooperTargetMachine> 
-Y(TheCooperTarget, "cooper", "PIC16 Cooper [experimental].");
+Y("cooper", "PIC16 Cooper [experimental].");
 
 // Force static initialization.
 extern "C" void LLVMInitializePIC16Target() { }
 
 // PIC16TargetMachine - Traditional PIC16 Machine.
-PIC16TargetMachine::PIC16TargetMachine(const Target &T, const Module &M, 
-                                       const std::string &FS, bool Cooper)
-: LLVMTargetMachine(T),
-  Subtarget(M, FS, Cooper),
+PIC16TargetMachine::PIC16TargetMachine(const Module &M, const std::string &FS,
+                                       bool Cooper)
+: Subtarget(M, FS, Cooper),
   DataLayout("e-p:16:8:8-i8:8:8-i16:8:8-i32:8:8"), 
   InstrInfo(*this), TLInfo(*this),
   FrameInfo(TargetFrameInfo::StackGrowsUp, 8, 0) { }
 
 // CooperTargetMachine - Uses the same PIC16TargetMachine, but makes IsCooper
 // as true.
-CooperTargetMachine::CooperTargetMachine(const Target &T, const Module &M, 
-                                         const std::string &FS)
-  : PIC16TargetMachine(T, M, FS, true) {}
+CooperTargetMachine::CooperTargetMachine(const Module &M, const std::string &FS)
+  : PIC16TargetMachine(M, FS, true) {}
 
 
 const TargetAsmInfo *PIC16TargetMachine::createTargetAsmInfo() const {
diff --git a/lib/Target/PIC16/PIC16TargetMachine.h b/lib/Target/PIC16/PIC16TargetMachine.h
index f14d8fd..a0080df 100644
--- a/lib/Target/PIC16/PIC16TargetMachine.h
+++ b/lib/Target/PIC16/PIC16TargetMachine.h
@@ -41,7 +41,7 @@
   virtual const TargetAsmInfo *createTargetAsmInfo() const;
 
 public:
-  PIC16TargetMachine(const Target &T, const Module &M, const std::string &FS, 
+  PIC16TargetMachine(const Module &M, const std::string &FS, 
                      bool Cooper = false);
 
   virtual const TargetFrameInfo *getFrameInfo() const { return &FrameInfo; }
@@ -68,7 +68,7 @@
 /// CooperTargetMachine
 class CooperTargetMachine : public PIC16TargetMachine {
 public:
-  CooperTargetMachine(const Target &T, const Module &M, const std::string &FS);
+  CooperTargetMachine(const Module &M, const std::string &FS);
 }; // CooperTargetMachine.
 
 } // end namespace llvm
diff --git a/lib/Target/PowerPC/AsmPrinter/PPCAsmPrinter.cpp b/lib/Target/PowerPC/AsmPrinter/PPCAsmPrinter.cpp
index f568bc1..7489e49 100644
--- a/lib/Target/PowerPC/AsmPrinter/PPCAsmPrinter.cpp
+++ b/lib/Target/PowerPC/AsmPrinter/PPCAsmPrinter.cpp
@@ -43,7 +43,6 @@
 #include "llvm/Target/TargetRegisterInfo.h"
 #include "llvm/Target/TargetInstrInfo.h"
 #include "llvm/Target/TargetOptions.h"
-#include "llvm/Target/TargetRegistry.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringSet.h"
@@ -318,7 +317,7 @@
   /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
   class VISIBILITY_HIDDEN PPCLinuxAsmPrinter : public PPCAsmPrinter {
   public:
-    explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
+    explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, PPCTargetMachine &TM,
                                 const TargetAsmInfo *T, bool V)
       : PPCAsmPrinter(O, TM, T, V){}
 
@@ -344,7 +343,7 @@
   class VISIBILITY_HIDDEN PPCDarwinAsmPrinter : public PPCAsmPrinter {
     formatted_raw_ostream &OS;
   public:
-    explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
+    explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, PPCTargetMachine &TM,
                                  const TargetAsmInfo *T, bool V)
       : PPCAsmPrinter(O, TM, T, V), OS(O) {}
 
@@ -1081,7 +1080,7 @@
 /// Darwin assembler can deal with.
 ///
 FunctionPass *llvm::createPPCAsmPrinterPass(formatted_raw_ostream &o,
-                                            TargetMachine &tm,
+                                            PPCTargetMachine &tm,
                                             bool verbose) {
   const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
 
@@ -1104,10 +1103,4 @@
 int PowerPCAsmPrinterForceLink = 0;
 
 // Force static initialization.
-extern "C" void LLVMInitializePowerPCAsmPrinter() { 
-  extern Target ThePPC32Target;
-  TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
-
-  extern Target ThePPC64Target;
-  TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
-}
+extern "C" void LLVMInitializePowerPCAsmPrinter() { }
diff --git a/lib/Target/PowerPC/PPC.h b/lib/Target/PowerPC/PPC.h
index 3aa313c..89dc1c1 100644
--- a/lib/Target/PowerPC/PPC.h
+++ b/lib/Target/PowerPC/PPC.h
@@ -30,7 +30,7 @@
 FunctionPass *createPPCBranchSelectionPass();
 FunctionPass *createPPCISelDag(PPCTargetMachine &TM);
 FunctionPass *createPPCAsmPrinterPass(formatted_raw_ostream &OS,
-                                      TargetMachine &TM,
+                                      PPCTargetMachine &TM,
                                       bool Verbose);
 FunctionPass *createPPCCodeEmitterPass(PPCTargetMachine &TM,
                                        MachineCodeEmitter &MCE);
diff --git a/lib/Target/PowerPC/PPCTargetMachine.cpp b/lib/Target/PowerPC/PPCTargetMachine.cpp
index 7e401c4..c261f87 100644
--- a/lib/Target/PowerPC/PPCTargetMachine.cpp
+++ b/lib/Target/PowerPC/PPCTargetMachine.cpp
@@ -30,13 +30,10 @@
 int PowerPCTargetMachineModule = 0;
 
 // Register the targets
-extern Target ThePPC32Target;
 static RegisterTarget<PPC32TargetMachine>
-X(ThePPC32Target, "ppc32", "PowerPC 32");
-
-extern Target ThePPC64Target;
+X("ppc32", "PowerPC 32");
 static RegisterTarget<PPC64TargetMachine>
-Y(ThePPC64Target, "ppc64", "PowerPC 64");
+Y("ppc64", "PowerPC 64");
 
 // Force static initialization.
 extern "C" void LLVMInitializePowerPCTarget() { }
@@ -51,10 +48,60 @@
     return new PPCLinuxTargetAsmInfo(*this);
 }
 
-PPCTargetMachine::PPCTargetMachine(const Target&T, const Module &M, 
-                                   const std::string &FS, bool is64Bit)
-  : LLVMTargetMachine(T),
-    Subtarget(*this, M, FS, is64Bit),
+unsigned PPC32TargetMachine::getJITMatchQuality() {
+#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
+  if (sizeof(void*) == 4)
+    return 10;
+#endif
+  return 0;
+}
+unsigned PPC64TargetMachine::getJITMatchQuality() {
+#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
+  if (sizeof(void*) == 8)
+    return 10;
+#endif
+  return 0;
+}
+
+unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) {
+  // We strongly match "powerpc-*".
+  std::string TT = M.getTargetTriple();
+  if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-")
+    return 20;
+  
+  // If the target triple is something non-powerpc, we don't match.
+  if (!TT.empty()) return 0;
+  
+  if (M.getEndianness()  == Module::BigEndian &&
+      M.getPointerSize() == Module::Pointer32)
+    return 10;                                   // Weak match
+  else if (M.getEndianness() != Module::AnyEndianness ||
+           M.getPointerSize() != Module::AnyPointerSize)
+    return 0;                                    // Match for some other target
+  
+  return getJITMatchQuality()/2;
+}
+
+unsigned PPC64TargetMachine::getModuleMatchQuality(const Module &M) {
+  // We strongly match "powerpc64-*".
+  std::string TT = M.getTargetTriple();
+  if (TT.size() >= 10 && std::string(TT.begin(), TT.begin()+10) == "powerpc64-")
+    return 20;
+  
+  if (M.getEndianness()  == Module::BigEndian &&
+      M.getPointerSize() == Module::Pointer64)
+    return 10;                                   // Weak match
+  else if (M.getEndianness() != Module::AnyEndianness ||
+           M.getPointerSize() != Module::AnyPointerSize)
+    return 0;                                    // Match for some other target
+  
+  return getJITMatchQuality()/2;
+}
+
+
+PPCTargetMachine::PPCTargetMachine(const Module &M, const std::string &FS,
+                                   bool is64Bit)
+  : Subtarget(*this, M, FS, is64Bit),
     DataLayout(Subtarget.getTargetDataString()), InstrInfo(*this),
     FrameInfo(*this, is64Bit), JITInfo(*this, is64Bit), TLInfo(*this),
     InstrItins(Subtarget.getInstrItineraryData()), MachOWriterInfo(*this) {
@@ -71,15 +118,13 @@
 /// groups, which typically degrades performance.
 bool PPCTargetMachine::getEnableTailMergeDefault() const { return false; }
 
-PPC32TargetMachine::PPC32TargetMachine(const Target &T, const Module &M, 
-                                       const std::string &FS) 
-  : PPCTargetMachine(T, M, FS, false) {
+PPC32TargetMachine::PPC32TargetMachine(const Module &M, const std::string &FS) 
+  : PPCTargetMachine(M, FS, false) {
 }
 
 
-PPC64TargetMachine::PPC64TargetMachine(const Target &T, const Module &M, 
-                                       const std::string &FS)
-  : PPCTargetMachine(T, M, FS, true) {
+PPC64TargetMachine::PPC64TargetMachine(const Module &M, const std::string &FS)
+  : PPCTargetMachine(M, FS, true) {
 }
 
 
diff --git a/lib/Target/PowerPC/PPCTargetMachine.h b/lib/Target/PowerPC/PPCTargetMachine.h
index 794123a..e6f6102 100644
--- a/lib/Target/PowerPC/PPCTargetMachine.h
+++ b/lib/Target/PowerPC/PPCTargetMachine.h
@@ -45,13 +45,12 @@
   // To avoid having target depend on the asmprinter stuff libraries, asmprinter
   // set this functions to ctor pointer at startup time if they are linked in.
   typedef FunctionPass *(*AsmPrinterCtorFn)(formatted_raw_ostream &o,
-                                            TargetMachine &m, 
+                                            PPCTargetMachine &tm, 
                                             bool verbose);
   static AsmPrinterCtorFn AsmPrinterCtor;
 
 public:
-  PPCTargetMachine(const Target &T, const Module &M, const std::string &FS, 
-                   bool is64Bit);
+  PPCTargetMachine(const Module &M, const std::string &FS, bool is64Bit);
 
   virtual const PPCInstrInfo     *getInstrInfo() const { return &InstrInfo; }
   virtual const PPCFrameInfo     *getFrameInfo() const { return &FrameInfo; }
@@ -104,14 +103,20 @@
 ///
 class PPC32TargetMachine : public PPCTargetMachine {
 public:
-  PPC32TargetMachine(const Target &T, const Module &M, const std::string &FS);
+  PPC32TargetMachine(const Module &M, const std::string &FS);
+  
+  static unsigned getJITMatchQuality();
+  static unsigned getModuleMatchQuality(const Module &M);
 };
 
 /// PPC64TargetMachine - PowerPC 64-bit target machine.
 ///
 class PPC64TargetMachine : public PPCTargetMachine {
 public:
-  PPC64TargetMachine(const Target &T, const Module &M, const std::string &FS);
+  PPC64TargetMachine(const Module &M, const std::string &FS);
+  
+  static unsigned getJITMatchQuality();
+  static unsigned getModuleMatchQuality(const Module &M);
 };
 
 } // end namespace llvm
diff --git a/lib/Target/Sparc/AsmPrinter/SparcAsmPrinter.cpp b/lib/Target/Sparc/AsmPrinter/SparcAsmPrinter.cpp
index 03735b0..c580285 100644
--- a/lib/Target/Sparc/AsmPrinter/SparcAsmPrinter.cpp
+++ b/lib/Target/Sparc/AsmPrinter/SparcAsmPrinter.cpp
@@ -26,7 +26,6 @@
 #include "llvm/CodeGen/MachineConstantPool.h"
 #include "llvm/CodeGen/MachineInstr.h"
 #include "llvm/Target/TargetAsmInfo.h"
-#include "llvm/Target/TargetRegistry.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/FormattedStream.h"
 #include "llvm/Support/Mangler.h"
@@ -360,8 +359,4 @@
 }
 
 // Force static initialization.
-extern "C" void LLVMInitializeSparcAsmPrinter() { 
-  extern Target TheSparcTarget;
-  TargetRegistry::RegisterAsmPrinter(TheSparcTarget, 
-                                     createSparcCodePrinterPass);
-}
+extern "C" void LLVMInitializeSparcAsmPrinter() { }
diff --git a/lib/Target/Sparc/SparcTargetMachine.cpp b/lib/Target/Sparc/SparcTargetMachine.cpp
index 2550363..66d5497 100644
--- a/lib/Target/Sparc/SparcTargetMachine.cpp
+++ b/lib/Target/Sparc/SparcTargetMachine.cpp
@@ -19,8 +19,7 @@
 using namespace llvm;
 
 // Register the target.
-extern Target TheSparcTarget;
-static RegisterTarget<SparcTargetMachine> X(TheSparcTarget, "sparc", "SPARC");
+static RegisterTarget<SparcTargetMachine> X("sparc", "SPARC");
 
 // No assembler printer by default
 SparcTargetMachine::AsmPrinterCtorFn SparcTargetMachine::AsmPrinterCtor = 0;
@@ -36,14 +35,38 @@
 
 /// SparcTargetMachine ctor - Create an ILP32 architecture model
 ///
-SparcTargetMachine::SparcTargetMachine(const Target &T, const Module &M, 
-                                       const std::string &FS)
-  : LLVMTargetMachine(T),
-    DataLayout("E-p:32:32-f128:128:128"),
+SparcTargetMachine::SparcTargetMachine(const Module &M, const std::string &FS)
+  : DataLayout("E-p:32:32-f128:128:128"),
     Subtarget(M, FS), TLInfo(*this), InstrInfo(Subtarget),
     FrameInfo(TargetFrameInfo::StackGrowsDown, 8, 0) {
 }
 
+unsigned SparcTargetMachine::getModuleMatchQuality(const Module &M) {
+  std::string TT = M.getTargetTriple();
+  if (TT.size() >= 6 && std::string(TT.begin(), TT.begin()+6) == "sparc-")
+    return 20;
+  
+  // If the target triple is something non-sparc, we don't match.
+  if (!TT.empty()) return 0;
+
+  if (M.getEndianness()  == Module::BigEndian &&
+      M.getPointerSize() == Module::Pointer32)
+#ifdef __sparc__
+    return 20;   // BE/32 ==> Prefer sparc on sparc
+#else
+    return 5;    // BE/32 ==> Prefer ppc elsewhere
+#endif
+  else if (M.getEndianness() != Module::AnyEndianness ||
+           M.getPointerSize() != Module::AnyPointerSize)
+    return 0;                                    // Match for some other target
+
+#if defined(__sparc__)
+  return 10;
+#else
+  return 0;
+#endif
+}
+
 bool SparcTargetMachine::addInstSelector(PassManagerBase &PM,
                                          CodeGenOpt::Level OptLevel) {
   PM.add(createSparcISelDag(*this));
diff --git a/lib/Target/Sparc/SparcTargetMachine.h b/lib/Target/Sparc/SparcTargetMachine.h
index ca34b96..5da4a78 100644
--- a/lib/Target/Sparc/SparcTargetMachine.h
+++ b/lib/Target/Sparc/SparcTargetMachine.h
@@ -43,7 +43,7 @@
   static AsmPrinterCtorFn AsmPrinterCtor;
   
 public:
-  SparcTargetMachine(const Target &T, const Module &M, const std::string &FS);
+  SparcTargetMachine(const Module &M, const std::string &FS);
 
   virtual const SparcInstrInfo *getInstrInfo() const { return &InstrInfo; }
   virtual const TargetFrameInfo  *getFrameInfo() const { return &FrameInfo; }
@@ -55,6 +55,7 @@
     return const_cast<SparcTargetLowering*>(&TLInfo);
   }
   virtual const TargetData       *getTargetData() const { return &DataLayout; }
+  static unsigned getModuleMatchQuality(const Module &M);
 
   // Pass Pipeline Configuration
   virtual bool addInstSelector(PassManagerBase &PM, CodeGenOpt::Level OptLevel);
diff --git a/lib/Target/TargetMachine.cpp b/lib/Target/TargetMachine.cpp
index fb95c52..c487cb8 100644
--- a/lib/Target/TargetMachine.cpp
+++ b/lib/Target/TargetMachine.cpp
@@ -176,8 +176,8 @@
 // TargetMachine Class
 //
 
-TargetMachine::TargetMachine(const Target &T) 
-  : TheTarget(T), AsmInfo(0) {
+TargetMachine::TargetMachine() 
+  : AsmInfo(0) {
   // Typically it will be subtargets that will adjust FloatABIType from Default
   // to Soft or Hard.
   if (UseSoftFloat)
diff --git a/lib/Target/X86/AsmPrinter/X86ATTAsmPrinter.h b/lib/Target/X86/AsmPrinter/X86ATTAsmPrinter.h
index b8a25a6..9721fdc 100644
--- a/lib/Target/X86/AsmPrinter/X86ATTAsmPrinter.h
+++ b/lib/Target/X86/AsmPrinter/X86ATTAsmPrinter.h
@@ -37,7 +37,7 @@
   MCContext *Context;
   MCStreamer *Streamer;
  public:
-  explicit X86ATTAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
+  explicit X86ATTAsmPrinter(formatted_raw_ostream &O, X86TargetMachine &TM,
                             const TargetAsmInfo *T, bool V)
     : AsmPrinter(O, TM, T, V) {
     Subtarget = &TM.getSubtarget<X86Subtarget>();
diff --git a/lib/Target/X86/AsmPrinter/X86AsmPrinter.cpp b/lib/Target/X86/AsmPrinter/X86AsmPrinter.cpp
index 3fad77e..40b24f3 100644
--- a/lib/Target/X86/AsmPrinter/X86AsmPrinter.cpp
+++ b/lib/Target/X86/AsmPrinter/X86AsmPrinter.cpp
@@ -17,7 +17,6 @@
 #include "X86ATTAsmPrinter.h"
 #include "X86IntelAsmPrinter.h"
 #include "X86Subtarget.h"
-#include "llvm/Target/TargetRegistry.h"
 using namespace llvm;
 
 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
@@ -25,7 +24,7 @@
 /// machine description.
 ///
 FunctionPass *llvm::createX86CodePrinterPass(formatted_raw_ostream &o,
-                                             TargetMachine &tm,
+                                             X86TargetMachine &tm,
                                              bool verbose) {
   const X86Subtarget *Subtarget = &tm.getSubtarget<X86Subtarget>();
 
@@ -46,10 +45,4 @@
 int X86AsmPrinterForceLink = 0;
 
 // Force static initialization.
-extern "C" void LLVMInitializeX86AsmPrinter() { 
-  extern Target TheX86_32Target;
-  TargetRegistry::RegisterAsmPrinter(TheX86_32Target, createX86CodePrinterPass);
-
-  extern Target TheX86_64Target;
-  TargetRegistry::RegisterAsmPrinter(TheX86_64Target, createX86CodePrinterPass);
-}
+extern "C" void LLVMInitializeX86AsmPrinter() { }
diff --git a/lib/Target/X86/AsmPrinter/X86IntelAsmPrinter.h b/lib/Target/X86/AsmPrinter/X86IntelAsmPrinter.h
index 603de3e..8872869 100644
--- a/lib/Target/X86/AsmPrinter/X86IntelAsmPrinter.h
+++ b/lib/Target/X86/AsmPrinter/X86IntelAsmPrinter.h
@@ -25,7 +25,7 @@
 namespace llvm {
 
 struct VISIBILITY_HIDDEN X86IntelAsmPrinter : public AsmPrinter {
-  explicit X86IntelAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
+  explicit X86IntelAsmPrinter(formatted_raw_ostream &O, X86TargetMachine &TM,
                               const TargetAsmInfo *T, bool V)
     : AsmPrinter(O, TM, T, V) {}
 
diff --git a/lib/Target/X86/X86.h b/lib/Target/X86/X86.h
index 457e18a..ead4ce1 100644
--- a/lib/Target/X86/X86.h
+++ b/lib/Target/X86/X86.h
@@ -47,7 +47,7 @@
 /// using the given target machine description.
 ///
 FunctionPass *createX86CodePrinterPass(formatted_raw_ostream &o,
-                                       TargetMachine &tm,
+                                       X86TargetMachine &tm,
                                        bool Verbose);
 
 /// createX86CodeEmitterPass - Return a pass that emits the collected X86 code
diff --git a/lib/Target/X86/X86TargetMachine.cpp b/lib/Target/X86/X86TargetMachine.cpp
index c640847..40dc18d 100644
--- a/lib/Target/X86/X86TargetMachine.cpp
+++ b/lib/Target/X86/X86TargetMachine.cpp
@@ -31,18 +31,13 @@
 int X86TargetMachineModule = 0;
 
 // Register the target.
-extern Target TheX86_32Target;
 static RegisterTarget<X86_32TargetMachine>
-X(TheX86_32Target, "x86",    "32-bit X86: Pentium-Pro and above");
-
-extern Target TheX86_64Target;
+X("x86",    "32-bit X86: Pentium-Pro and above");
 static RegisterTarget<X86_64TargetMachine>
-Y(TheX86_64Target, "x86-64", "64-bit X86: EM64T and AMD64");
+Y("x86-64", "64-bit X86: EM64T and AMD64");
 
 // Force static initialization.
-extern "C" void LLVMInitializeX86Target() { 
-  
-}
+extern "C" void LLVMInitializeX86Target() { }
 
 // No assembler printer by default
 X86TargetMachine::AsmPrinterCtorFn X86TargetMachine::AsmPrinterCtor = 0;
@@ -66,23 +61,78 @@
     }
 }
 
-X86_32TargetMachine::X86_32TargetMachine(const Target &T, const Module &M, 
-                                         const std::string &FS)
-  : X86TargetMachine(T, M, FS, false) {
+unsigned X86_32TargetMachine::getJITMatchQuality() {
+#if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
+  return 10;
+#endif
+  return 0;
+}
+
+unsigned X86_64TargetMachine::getJITMatchQuality() {
+#if defined(__x86_64__) || defined(_M_AMD64)
+  return 10;
+#endif
+  return 0;
+}
+
+unsigned X86_32TargetMachine::getModuleMatchQuality(const Module &M) {
+  // We strongly match "i[3-9]86-*".
+  std::string TT = M.getTargetTriple();
+  if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
+      TT[4] == '-' && TT[1] - '3' < 6)
+    return 20;
+  // If the target triple is something non-X86, we don't match.
+  if (!TT.empty()) return 0;
+
+  if (M.getEndianness()  == Module::LittleEndian &&
+      M.getPointerSize() == Module::Pointer32)
+    return 10;                                   // Weak match
+  else if (M.getEndianness() != Module::AnyEndianness ||
+           M.getPointerSize() != Module::AnyPointerSize)
+    return 0;                                    // Match for some other target
+
+  return getJITMatchQuality()/2;
+}
+
+unsigned X86_64TargetMachine::getModuleMatchQuality(const Module &M) {
+  // We strongly match "x86_64-*".
+  std::string TT = M.getTargetTriple();
+  if (TT.size() >= 7 && TT[0] == 'x' && TT[1] == '8' && TT[2] == '6' &&
+      TT[3] == '_' && TT[4] == '6' && TT[5] == '4' && TT[6] == '-')
+    return 20;
+
+  // We strongly match "amd64-*".
+  if (TT.size() >= 6 && TT[0] == 'a' && TT[1] == 'm' && TT[2] == 'd' &&
+      TT[3] == '6' && TT[4] == '4' && TT[5] == '-')
+    return 20;
+  
+  // If the target triple is something non-X86-64, we don't match.
+  if (!TT.empty()) return 0;
+
+  if (M.getEndianness()  == Module::LittleEndian &&
+      M.getPointerSize() == Module::Pointer64)
+    return 10;                                   // Weak match
+  else if (M.getEndianness() != Module::AnyEndianness ||
+           M.getPointerSize() != Module::AnyPointerSize)
+    return 0;                                    // Match for some other target
+
+  return getJITMatchQuality()/2;
+}
+
+X86_32TargetMachine::X86_32TargetMachine(const Module &M, const std::string &FS)
+  : X86TargetMachine(M, FS, false) {
 }
 
 
-X86_64TargetMachine::X86_64TargetMachine(const Target &T, const Module &M, 
-                                         const std::string &FS)
-  : X86TargetMachine(T, M, FS, true) {
+X86_64TargetMachine::X86_64TargetMachine(const Module &M, const std::string &FS)
+  : X86TargetMachine(M, FS, true) {
 }
 
 /// X86TargetMachine ctor - Create an X86 target.
 ///
-X86TargetMachine::X86TargetMachine(const Target &T, const Module &M, 
-                                   const std::string &FS, bool is64Bit)
-  : LLVMTargetMachine(T), 
-    Subtarget(M, FS, is64Bit),
+X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS,
+                                   bool is64Bit)
+  : Subtarget(M, FS, is64Bit),
     DataLayout(Subtarget.getDataLayout()),
     FrameInfo(TargetFrameInfo::StackGrowsDown,
               Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
diff --git a/lib/Target/X86/X86TargetMachine.h b/lib/Target/X86/X86TargetMachine.h
index da6811b..ea7a453 100644
--- a/lib/Target/X86/X86TargetMachine.h
+++ b/lib/Target/X86/X86TargetMachine.h
@@ -44,13 +44,12 @@
   // To avoid having target depend on the asmprinter stuff libraries, asmprinter
   // set this functions to ctor pointer at startup time if they are linked in.
   typedef FunctionPass *(*AsmPrinterCtorFn)(formatted_raw_ostream &o,
-                                            TargetMachine &tm,
+                                            X86TargetMachine &tm,
                                             bool verbose);
   static AsmPrinterCtorFn AsmPrinterCtor;
 
 public:
-  X86TargetMachine(const Target &T, const Module &M, const std::string &FS, 
-                   bool is64Bit);
+  X86TargetMachine(const Module &M, const std::string &FS, bool is64Bit);
 
   virtual const X86InstrInfo     *getInstrInfo() const { return &InstrInfo; }
   virtual const TargetFrameInfo  *getFrameInfo() const { return &FrameInfo; }
@@ -67,6 +66,9 @@
     return Subtarget.isTargetELF() ? &ELFWriterInfo : 0;
   }
 
+  static unsigned getModuleMatchQuality(const Module &M);
+  static unsigned getJITMatchQuality();
+
   static void registerAsmPrinter(AsmPrinterCtorFn F) {
     AsmPrinterCtor = F;
   }
@@ -99,7 +101,7 @@
 ///
 class X86_32TargetMachine : public X86TargetMachine {
 public:
-  X86_32TargetMachine(const Target &T, const Module &M, const std::string &FS);
+  X86_32TargetMachine(const Module &M, const std::string &FS);
   
   static unsigned getJITMatchQuality();
   static unsigned getModuleMatchQuality(const Module &M);
@@ -109,7 +111,7 @@
 ///
 class X86_64TargetMachine : public X86TargetMachine {
 public:
-  X86_64TargetMachine(const Target &T, const Module &M, const std::string &FS);
+  X86_64TargetMachine(const Module &M, const std::string &FS);
   
   static unsigned getJITMatchQuality();
   static unsigned getModuleMatchQuality(const Module &M);
diff --git a/lib/Target/XCore/XCoreTargetMachine.cpp b/lib/Target/XCore/XCoreTargetMachine.cpp
index 7fc8694..42b9875 100644
--- a/lib/Target/XCore/XCoreTargetMachine.cpp
+++ b/lib/Target/XCore/XCoreTargetMachine.cpp
@@ -26,10 +26,9 @@
 extern "C" int XCoreTargetMachineModule;
 int XCoreTargetMachineModule = 0;
 
-extern Target TheXCoreTarget;
 namespace {
   // Register the target.
-  RegisterTarget<XCoreTargetMachine> X(TheXCoreTarget, "xcore", "XCore");
+  RegisterTarget<XCoreTargetMachine> X("xcore", "XCore");
 }
 
 // Force static initialization.
@@ -41,10 +40,8 @@
 
 /// XCoreTargetMachine ctor - Create an ILP32 architecture model
 ///
-XCoreTargetMachine::XCoreTargetMachine(const Target &T, const Module &M, 
-                                       const std::string &FS)
-  : LLVMTargetMachine(T),
-    Subtarget(*this, M, FS),
+XCoreTargetMachine::XCoreTargetMachine(const Module &M, const std::string &FS)
+  : Subtarget(*this, M, FS),
     DataLayout("e-p:32:32:32-a0:0:32-f32:32:32-f64:32:32-i1:8:32-i8:8:32-"
                "i16:16:32-i32:32:32-i64:32:32"),
     InstrInfo(),
@@ -52,6 +49,15 @@
     TLInfo(*this) {
 }
 
+unsigned XCoreTargetMachine::getModuleMatchQuality(const Module &M) {
+  std::string TT = M.getTargetTriple();
+  if (TT.size() >= 6 && std::string(TT.begin(), TT.begin()+6) == "xcore-")
+    return 20;
+  
+  // Otherwise we don't match.
+  return 0;
+}
+
 bool XCoreTargetMachine::addInstSelector(PassManagerBase &PM,
                                          CodeGenOpt::Level OptLevel) {
   PM.add(createXCoreISelDag(*this));
diff --git a/lib/Target/XCore/XCoreTargetMachine.h b/lib/Target/XCore/XCoreTargetMachine.h
index e88c81b..081cf7a 100644
--- a/lib/Target/XCore/XCoreTargetMachine.h
+++ b/lib/Target/XCore/XCoreTargetMachine.h
@@ -36,7 +36,7 @@
   virtual const TargetAsmInfo *createTargetAsmInfo() const;
 
 public:
-  XCoreTargetMachine(const Target &T, const Module &M, const std::string &FS);
+  XCoreTargetMachine(const Module &M, const std::string &FS);
 
   virtual const XCoreInstrInfo *getInstrInfo() const { return &InstrInfo; }
   virtual const XCoreFrameInfo *getFrameInfo() const { return &FrameInfo; }
@@ -49,6 +49,7 @@
     return &InstrInfo.getRegisterInfo();
   }
   virtual const TargetData       *getTargetData() const { return &DataLayout; }
+  static unsigned getModuleMatchQuality(const Module &M);
 
   // Pass Pipeline Configuration
   virtual bool addInstSelector(PassManagerBase &PM, CodeGenOpt::Level OptLevel);