blob: b3446e6109227872d1f3c601ab9b39a9b47e9e63 [file] [log] [blame]
Daniel Dunbarbb061292009-07-15 04:24:58 +00001//===--- 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 <cassert>
12using namespace llvm;
13
Daniel Dunbar73b3ec42009-07-15 07:09:29 +000014// Clients are responsible for avoid race conditions in registration.
Daniel Dunbarbb061292009-07-15 04:24:58 +000015static Target *FirstTarget = 0;
16
Daniel Dunbar603bea32009-07-16 02:06:09 +000017TargetRegistry::iterator TargetRegistry::begin() {
18 return iterator(FirstTarget);
19}
20
Daniel Dunbarbb061292009-07-15 04:24:58 +000021const Target *
22TargetRegistry::getClosestStaticTargetForTriple(const std::string &TT,
23 std::string &Error) {
Daniel Dunbar7df0c072009-07-17 15:50:49 +000024 // Provide special warning when no targets are initialized.
25 if (begin() == end()) {
26 Error = "Unable to find target for this triple (no targets are registered)";
27 return 0;
28 }
Daniel Dunbar603bea32009-07-16 02:06:09 +000029 const Target *Best = 0, *EquallyBest = 0;
Daniel Dunbarbb061292009-07-15 04:24:58 +000030 unsigned BestQuality = 0;
Daniel Dunbar603bea32009-07-16 02:06:09 +000031 for (iterator it = begin(), ie = end(); it != ie; ++it) {
32 if (unsigned Qual = it->TripleMatchQualityFn(TT)) {
Daniel Dunbarbb061292009-07-15 04:24:58 +000033 if (!Best || Qual > BestQuality) {
Daniel Dunbar603bea32009-07-16 02:06:09 +000034 Best = &*it;
Daniel Dunbarbb061292009-07-15 04:24:58 +000035 EquallyBest = 0;
36 BestQuality = Qual;
37 } else if (Qual == BestQuality)
Daniel Dunbar603bea32009-07-16 02:06:09 +000038 EquallyBest = &*it;
Daniel Dunbarbb061292009-07-15 04:24:58 +000039 }
40 }
41
42 if (!Best) {
Daniel Dunbar7df0c072009-07-17 15:50:49 +000043 Error = "No available targets are compatible with this triple";
Daniel Dunbarbb061292009-07-15 04:24:58 +000044 return 0;
45 }
46
47 // Otherwise, take the best target, but make sure we don't have two equally
48 // good best targets.
49 if (EquallyBest) {
50 Error = std::string("Cannot choose between targets \"") +
51 Best->Name + "\" and \"" + EquallyBest->Name + "\"";
52 return 0;
53 }
54
55 return Best;
56}
57
58const Target *
59TargetRegistry::getClosestStaticTargetForModule(const Module &M,
60 std::string &Error) {
Daniel Dunbar7df0c072009-07-17 15:50:49 +000061 // Provide special warning when no targets are initialized.
62 if (begin() == end()) {
63 Error = "Unable to find target for this module (no targets are registered)";
64 return 0;
65 }
66
Daniel Dunbar603bea32009-07-16 02:06:09 +000067 const Target *Best = 0, *EquallyBest = 0;
Daniel Dunbarbb061292009-07-15 04:24:58 +000068 unsigned BestQuality = 0;
Daniel Dunbar603bea32009-07-16 02:06:09 +000069 for (iterator it = begin(), ie = end(); it != ie; ++it) {
70 if (unsigned Qual = it->ModuleMatchQualityFn(M)) {
Daniel Dunbarbb061292009-07-15 04:24:58 +000071 if (!Best || Qual > BestQuality) {
Daniel Dunbar603bea32009-07-16 02:06:09 +000072 Best = &*it;
Daniel Dunbarbb061292009-07-15 04:24:58 +000073 EquallyBest = 0;
74 BestQuality = Qual;
75 } else if (Qual == BestQuality)
Daniel Dunbar603bea32009-07-16 02:06:09 +000076 EquallyBest = &*it;
Daniel Dunbarbb061292009-07-15 04:24:58 +000077 }
78 }
79
80 if (!Best) {
81 Error = "No available targets are compatible with this module";
82 return 0;
83 }
84
85 // Otherwise, take the best target, but make sure we don't have two equally
86 // good best targets.
87 if (EquallyBest) {
88 Error = std::string("Cannot choose between targets \"") +
89 Best->Name + "\" and \"" + EquallyBest->Name + "\"";
90 return 0;
91 }
92
93 return Best;
94}
95
96const Target *
97TargetRegistry::getClosestTargetForJIT(std::string &Error) {
Daniel Dunbar7df0c072009-07-17 15:50:49 +000098 // Provide special warning when no targets are initialized.
99 if (begin() == end()) {
100 Error = "No JIT is available for this host (no targets are registered)";
101 return 0;
102 }
103
Daniel Dunbar603bea32009-07-16 02:06:09 +0000104 const Target *Best = 0, *EquallyBest = 0;
Daniel Dunbarbb061292009-07-15 04:24:58 +0000105 unsigned BestQuality = 0;
Daniel Dunbar603bea32009-07-16 02:06:09 +0000106 for (iterator it = begin(), ie = end(); it != ie; ++it) {
107 if (unsigned Qual = it->JITMatchQualityFn()) {
Daniel Dunbarbb061292009-07-15 04:24:58 +0000108 if (!Best || Qual > BestQuality) {
Daniel Dunbar603bea32009-07-16 02:06:09 +0000109 Best = &*it;
Daniel Dunbarbb061292009-07-15 04:24:58 +0000110 EquallyBest = 0;
111 BestQuality = Qual;
112 } else if (Qual == BestQuality)
Daniel Dunbar603bea32009-07-16 02:06:09 +0000113 EquallyBest = &*it;
Daniel Dunbarbb061292009-07-15 04:24:58 +0000114 }
115 }
116
117 if (!Best) {
118 Error = "No JIT is available for this host";
119 return 0;
120 }
121
122 // Return the best, ignoring ties.
123 return Best;
124}
125
126void TargetRegistry::RegisterTarget(Target &T,
127 const char *Name,
128 const char *ShortDesc,
129 Target::TripleMatchQualityFnTy TQualityFn,
130 Target::ModuleMatchQualityFnTy MQualityFn,
131 Target::JITMatchQualityFnTy JITQualityFn) {
Daniel Dunbarbb061292009-07-15 04:24:58 +0000132 assert(Name && ShortDesc && TQualityFn && MQualityFn && JITQualityFn &&
133 "Missing required target information!");
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000134
135 // Check if this target has already been initialized, we allow this as a
136 // convenience to some clients.
137 if (T.Name)
138 return;
Daniel Dunbarbb061292009-07-15 04:24:58 +0000139
140 // Add to the list of targets.
141 T.Next = FirstTarget;
Daniel Dunbarf23d4932009-07-15 07:37:49 +0000142 FirstTarget = &T;
Daniel Dunbarbb061292009-07-15 04:24:58 +0000143
144 T.Name = Name;
145 T.ShortDesc = ShortDesc;
146 T.TripleMatchQualityFn = TQualityFn;
147 T.ModuleMatchQualityFn = MQualityFn;
148 T.JITMatchQualityFn = JITQualityFn;
149}
150