Daniel Dunbar | 351304b | 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" |
| 11 | #include <cassert> |
| 12 | using namespace llvm; |
| 13 | |
Daniel Dunbar | 988af81 | 2009-07-15 07:09:29 +0000 | [diff] [blame] | 14 | // Clients are responsible for avoid race conditions in registration. |
Daniel Dunbar | 351304b | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 15 | static Target *FirstTarget = 0; |
| 16 | |
Daniel Dunbar | 9b3edb6 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 17 | TargetRegistry::iterator TargetRegistry::begin() { |
| 18 | return iterator(FirstTarget); |
| 19 | } |
| 20 | |
Daniel Dunbar | 351304b | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 21 | const Target * |
| 22 | TargetRegistry::getClosestStaticTargetForTriple(const std::string &TT, |
| 23 | std::string &Error) { |
Daniel Dunbar | 766e20f | 2009-07-17 15:50:49 +0000 | [diff] [blame] | 24 | // 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 Dunbar | 9b3edb6 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 29 | const Target *Best = 0, *EquallyBest = 0; |
Daniel Dunbar | 351304b | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 30 | unsigned BestQuality = 0; |
Daniel Dunbar | 9b3edb6 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 31 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 32 | if (unsigned Qual = it->TripleMatchQualityFn(TT)) { |
Daniel Dunbar | 351304b | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 33 | if (!Best || Qual > BestQuality) { |
Daniel Dunbar | 9b3edb6 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 34 | Best = &*it; |
Daniel Dunbar | 351304b | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 35 | EquallyBest = 0; |
| 36 | BestQuality = Qual; |
| 37 | } else if (Qual == BestQuality) |
Daniel Dunbar | 9b3edb6 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 38 | EquallyBest = &*it; |
Daniel Dunbar | 351304b | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
| 42 | if (!Best) { |
Daniel Dunbar | 766e20f | 2009-07-17 15:50:49 +0000 | [diff] [blame] | 43 | Error = "No available targets are compatible with this triple"; |
Daniel Dunbar | 351304b | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 44 | 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 | |
| 58 | const Target * |
| 59 | TargetRegistry::getClosestStaticTargetForModule(const Module &M, |
| 60 | std::string &Error) { |
Daniel Dunbar | 766e20f | 2009-07-17 15:50:49 +0000 | [diff] [blame] | 61 | // 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 Dunbar | 9b3edb6 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 67 | const Target *Best = 0, *EquallyBest = 0; |
Daniel Dunbar | 351304b | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 68 | unsigned BestQuality = 0; |
Daniel Dunbar | 9b3edb6 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 69 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 70 | if (unsigned Qual = it->ModuleMatchQualityFn(M)) { |
Daniel Dunbar | 351304b | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 71 | if (!Best || Qual > BestQuality) { |
Daniel Dunbar | 9b3edb6 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 72 | Best = &*it; |
Daniel Dunbar | 351304b | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 73 | EquallyBest = 0; |
| 74 | BestQuality = Qual; |
| 75 | } else if (Qual == BestQuality) |
Daniel Dunbar | 9b3edb6 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 76 | EquallyBest = &*it; |
Daniel Dunbar | 351304b | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 77 | } |
| 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 | |
| 96 | const Target * |
| 97 | TargetRegistry::getClosestTargetForJIT(std::string &Error) { |
Daniel Dunbar | 766e20f | 2009-07-17 15:50:49 +0000 | [diff] [blame] | 98 | // 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 Dunbar | 9b3edb6 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 104 | const Target *Best = 0, *EquallyBest = 0; |
Daniel Dunbar | 351304b | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 105 | unsigned BestQuality = 0; |
Daniel Dunbar | 9b3edb6 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 106 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 107 | if (unsigned Qual = it->JITMatchQualityFn()) { |
Daniel Dunbar | 351304b | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 108 | if (!Best || Qual > BestQuality) { |
Daniel Dunbar | 9b3edb6 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 109 | Best = &*it; |
Daniel Dunbar | 351304b | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 110 | EquallyBest = 0; |
| 111 | BestQuality = Qual; |
| 112 | } else if (Qual == BestQuality) |
Daniel Dunbar | 9b3edb6 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 113 | EquallyBest = &*it; |
Daniel Dunbar | 351304b | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 114 | } |
| 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 | |
| 126 | void 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 Dunbar | 351304b | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 132 | assert(Name && ShortDesc && TQualityFn && MQualityFn && JITQualityFn && |
| 133 | "Missing required target information!"); |
Daniel Dunbar | fe5939f | 2009-07-15 20:24:03 +0000 | [diff] [blame] | 134 | |
| 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 Dunbar | 351304b | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 139 | |
| 140 | // Add to the list of targets. |
| 141 | T.Next = FirstTarget; |
Daniel Dunbar | 0a346f4 | 2009-07-15 07:37:49 +0000 | [diff] [blame] | 142 | FirstTarget = &T; |
Daniel Dunbar | 351304b | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 143 | |
| 144 | T.Name = Name; |
| 145 | T.ShortDesc = ShortDesc; |
| 146 | T.TripleMatchQualityFn = TQualityFn; |
| 147 | T.ModuleMatchQualityFn = MQualityFn; |
| 148 | T.JITMatchQualityFn = JITQualityFn; |
| 149 | } |
| 150 | |