Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 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" |
Daniel Dunbar | d6fd377 | 2009-07-25 10:09:50 +0000 | [diff] [blame^] | 11 | #include "llvm/System/Host.h" |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 12 | #include <cassert> |
| 13 | using namespace llvm; |
| 14 | |
Daniel Dunbar | 73b3ec4 | 2009-07-15 07:09:29 +0000 | [diff] [blame] | 15 | // Clients are responsible for avoid race conditions in registration. |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 16 | static Target *FirstTarget = 0; |
| 17 | |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 18 | TargetRegistry::iterator TargetRegistry::begin() { |
| 19 | return iterator(FirstTarget); |
| 20 | } |
| 21 | |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 22 | const Target * |
| 23 | TargetRegistry::getClosestStaticTargetForTriple(const std::string &TT, |
| 24 | std::string &Error) { |
Daniel Dunbar | 7df0c07 | 2009-07-17 15:50:49 +0000 | [diff] [blame] | 25 | // Provide special warning when no targets are initialized. |
| 26 | if (begin() == end()) { |
| 27 | Error = "Unable to find target for this triple (no targets are registered)"; |
| 28 | return 0; |
| 29 | } |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 30 | const Target *Best = 0, *EquallyBest = 0; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 31 | unsigned BestQuality = 0; |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 32 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 33 | if (unsigned Qual = it->TripleMatchQualityFn(TT)) { |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 34 | if (!Best || Qual > BestQuality) { |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 35 | Best = &*it; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 36 | EquallyBest = 0; |
| 37 | BestQuality = Qual; |
| 38 | } else if (Qual == BestQuality) |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 39 | EquallyBest = &*it; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
| 43 | if (!Best) { |
Daniel Dunbar | 7df0c07 | 2009-07-17 15:50:49 +0000 | [diff] [blame] | 44 | Error = "No available targets are compatible with this triple"; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | // Otherwise, take the best target, but make sure we don't have two equally |
| 49 | // good best targets. |
| 50 | if (EquallyBest) { |
| 51 | Error = std::string("Cannot choose between targets \"") + |
| 52 | Best->Name + "\" and \"" + EquallyBest->Name + "\""; |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | return Best; |
| 57 | } |
| 58 | |
| 59 | const Target * |
| 60 | TargetRegistry::getClosestStaticTargetForModule(const Module &M, |
| 61 | std::string &Error) { |
Daniel Dunbar | 7df0c07 | 2009-07-17 15:50:49 +0000 | [diff] [blame] | 62 | // Provide special warning when no targets are initialized. |
| 63 | if (begin() == end()) { |
| 64 | Error = "Unable to find target for this module (no targets are registered)"; |
| 65 | return 0; |
| 66 | } |
| 67 | |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 68 | const Target *Best = 0, *EquallyBest = 0; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 69 | unsigned BestQuality = 0; |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 70 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 71 | if (unsigned Qual = it->ModuleMatchQualityFn(M)) { |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 72 | if (!Best || Qual > BestQuality) { |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 73 | Best = &*it; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 74 | EquallyBest = 0; |
| 75 | BestQuality = Qual; |
| 76 | } else if (Qual == BestQuality) |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 77 | EquallyBest = &*it; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Daniel Dunbar | d6fd377 | 2009-07-25 10:09:50 +0000 | [diff] [blame^] | 81 | // FIXME: This is a hack to ignore super weak matches like msil, etc. and look |
| 82 | // by host instead. They will be found again via the triple. |
| 83 | if (Best && BestQuality == 1) |
| 84 | Best = EquallyBest = 0; |
| 85 | |
| 86 | // If that failed, try looking up the host triple. |
| 87 | if (!Best) |
| 88 | Best = getClosestStaticTargetForTriple(sys::getHostTriple(), Error); |
| 89 | |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 90 | if (!Best) { |
| 91 | Error = "No available targets are compatible with this module"; |
Daniel Dunbar | d6fd377 | 2009-07-25 10:09:50 +0000 | [diff] [blame^] | 92 | return Best; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // Otherwise, take the best target, but make sure we don't have two equally |
| 96 | // good best targets. |
| 97 | if (EquallyBest) { |
| 98 | Error = std::string("Cannot choose between targets \"") + |
| 99 | Best->Name + "\" and \"" + EquallyBest->Name + "\""; |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | return Best; |
| 104 | } |
| 105 | |
| 106 | const Target * |
| 107 | TargetRegistry::getClosestTargetForJIT(std::string &Error) { |
Daniel Dunbar | d6fd377 | 2009-07-25 10:09:50 +0000 | [diff] [blame^] | 108 | std::string Triple = sys::getHostTriple(); |
| 109 | |
Daniel Dunbar | 7df0c07 | 2009-07-17 15:50:49 +0000 | [diff] [blame] | 110 | // Provide special warning when no targets are initialized. |
| 111 | if (begin() == end()) { |
| 112 | Error = "No JIT is available for this host (no targets are registered)"; |
| 113 | return 0; |
| 114 | } |
| 115 | |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 116 | const Target *Best = 0, *EquallyBest = 0; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 117 | unsigned BestQuality = 0; |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 118 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
Daniel Dunbar | d6fd377 | 2009-07-25 10:09:50 +0000 | [diff] [blame^] | 119 | if (!it->hasJIT()) |
| 120 | continue; |
| 121 | |
| 122 | if (unsigned Qual = it->TripleMatchQualityFn(Triple)) { |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 123 | if (!Best || Qual > BestQuality) { |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 124 | Best = &*it; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 125 | EquallyBest = 0; |
| 126 | BestQuality = Qual; |
| 127 | } else if (Qual == BestQuality) |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 128 | EquallyBest = &*it; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | if (!Best) { |
| 133 | Error = "No JIT is available for this host"; |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | // Return the best, ignoring ties. |
| 138 | return Best; |
| 139 | } |
| 140 | |
| 141 | void TargetRegistry::RegisterTarget(Target &T, |
| 142 | const char *Name, |
| 143 | const char *ShortDesc, |
| 144 | Target::TripleMatchQualityFnTy TQualityFn, |
| 145 | Target::ModuleMatchQualityFnTy MQualityFn, |
Daniel Dunbar | d6fd377 | 2009-07-25 10:09:50 +0000 | [diff] [blame^] | 146 | bool HasJIT) { |
| 147 | assert(Name && ShortDesc && TQualityFn && MQualityFn && |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 148 | "Missing required target information!"); |
Daniel Dunbar | 51b198a | 2009-07-15 20:24:03 +0000 | [diff] [blame] | 149 | |
| 150 | // Check if this target has already been initialized, we allow this as a |
| 151 | // convenience to some clients. |
| 152 | if (T.Name) |
| 153 | return; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 154 | |
| 155 | // Add to the list of targets. |
| 156 | T.Next = FirstTarget; |
Daniel Dunbar | f23d493 | 2009-07-15 07:37:49 +0000 | [diff] [blame] | 157 | FirstTarget = &T; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 158 | |
| 159 | T.Name = Name; |
| 160 | T.ShortDesc = ShortDesc; |
| 161 | T.TripleMatchQualityFn = TQualityFn; |
| 162 | T.ModuleMatchQualityFn = MQualityFn; |
Daniel Dunbar | d6fd377 | 2009-07-25 10:09:50 +0000 | [diff] [blame^] | 163 | T.HasJIT = HasJIT; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 164 | } |
| 165 | |