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