Remove unused header
[oota-llvm.git] / lib / Support / TargetRegistry.cpp
1 //===--- TargetRegistry.cpp - Target registration -------------------------===//
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 #include "llvm/Target/TargetRegistry.h"
11 #include "llvm/System/Host.h"
12 #include <cassert>
13 using namespace llvm;
14
15 // Clients are responsible for avoid race conditions in registration.
16 static Target *FirstTarget = 0;
17
18 TargetRegistry::iterator TargetRegistry::begin() {
19   return iterator(FirstTarget);
20 }
21
22 const Target *TargetRegistry::lookupTarget(const std::string &TT,
23                                            bool FallbackToHost,
24                                            bool RequireJIT,
25                                            std::string &Error) {
26   // Provide special warning when no targets are initialized.
27   if (begin() == end()) {
28     Error = "Unable to find target for this triple (no targets are registered)";
29     return 0;
30   }
31   const Target *Best = 0, *EquallyBest = 0;
32   unsigned BestQuality = 0;
33   for (iterator it = begin(), ie = end(); it != ie; ++it) {
34     if (RequireJIT && !it->hasJIT())
35       continue;
36
37     if (unsigned Qual = it->TripleMatchQualityFn(TT)) {
38       if (!Best || Qual > BestQuality) {
39         Best = &*it;
40         EquallyBest = 0;
41         BestQuality = Qual;
42       } else if (Qual == BestQuality)
43         EquallyBest = &*it;
44     }
45   }
46
47   // FIXME: Hack. If we only have an extremely weak match and the client
48   // requested to fall back to the host, then ignore it and try again.
49   if (BestQuality == 1 && FallbackToHost)
50     Best = 0;
51
52   // Fallback to the host triple if we didn't find anything.
53   if (!Best && FallbackToHost)
54     return lookupTarget(sys::getHostTriple(), false, RequireJIT, Error);
55
56   if (!Best) {
57     Error = "No available targets are compatible with this triple";
58     return 0;
59   }
60
61   // Otherwise, take the best target, but make sure we don't have two equally
62   // good best targets.
63   if (EquallyBest) {
64     Error = std::string("Cannot choose between targets \"") +
65       Best->Name  + "\" and \"" + EquallyBest->Name + "\"";
66     return 0;
67   }
68
69   return Best;
70 }
71
72 void TargetRegistry::RegisterTarget(Target &T,
73                                     const char *Name,
74                                     const char *ShortDesc,
75                                     Target::TripleMatchQualityFnTy TQualityFn,
76                                     bool HasJIT) {
77   assert(Name && ShortDesc && TQualityFn &&
78          "Missing required target information!");
79
80   // Check if this target has already been initialized, we allow this as a
81   // convenience to some clients.
82   if (T.Name)
83     return;
84          
85   // Add to the list of targets.
86   T.Next = FirstTarget;
87   FirstTarget = &T;
88
89   T.Name = Name;
90   T.ShortDesc = ShortDesc;
91   T.TripleMatchQualityFn = TQualityFn;
92   T.HasJIT = HasJIT;
93 }
94