Chris Lattner | bf57337 | 2004-07-11 02:44:26 +0000 | [diff] [blame] | 1 | //===-- TargetMachineRegistry.cpp - Target Auto Registration Impl ---------===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | bf57337 | 2004-07-11 02:44:26 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | bf57337 | 2004-07-11 02:44:26 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file exposes the RegisterTarget class, which TargetMachine |
| 11 | // implementations should use to register themselves with the system. This file |
| 12 | // also exposes the TargetMachineRegistry class, which allows tools to inspect |
| 13 | // all of registered targets. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "llvm/Target/TargetMachineRegistry.h" |
Chris Lattner | d7099bc | 2004-07-11 04:00:19 +0000 | [diff] [blame] | 18 | #include <algorithm> |
Chris Lattner | bf57337 | 2004-07-11 02:44:26 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
Chris Lattner | 7c38def | 2004-07-11 06:03:21 +0000 | [diff] [blame] | 21 | /// List - This is the main list of all of the registered target machines. |
Chris Lattner | bf57337 | 2004-07-11 02:44:26 +0000 | [diff] [blame] | 22 | const TargetMachineRegistry::Entry *TargetMachineRegistry::List = 0; |
| 23 | |
Chris Lattner | 7c38def | 2004-07-11 06:03:21 +0000 | [diff] [blame] | 24 | /// Listeners - All of the listeners registered to get notified when new targets |
| 25 | /// are loaded. |
| 26 | static TargetRegistrationListener *Listeners = 0; |
| 27 | |
| 28 | TargetMachineRegistry::Entry::Entry(const char *N, const char *SD, |
Chris Lattner | ef98691 | 2006-03-23 05:28:02 +0000 | [diff] [blame] | 29 | TargetMachine *(*CF)(const Module &,const std::string &), |
Chris Lattner | 7c38def | 2004-07-11 06:03:21 +0000 | [diff] [blame] | 30 | unsigned (*MMF)(const Module &M), unsigned (*JMF)()) |
| 31 | : Name(N), ShortDesc(SD), CtorFn(CF), ModuleMatchQualityFn(MMF), |
| 32 | JITMatchQualityFn(JMF), Next(List) { |
| 33 | List = this; |
| 34 | for (TargetRegistrationListener *L = Listeners; L; L = L->getNext()) |
| 35 | L->targetRegistered(this); |
| 36 | } |
| 37 | |
| 38 | TargetRegistrationListener::TargetRegistrationListener() { |
| 39 | Next = Listeners; |
| 40 | if (Next) Next->Prev = &Next; |
| 41 | Prev = &Listeners; |
| 42 | Listeners = this; |
| 43 | } |
| 44 | |
| 45 | TargetRegistrationListener::~TargetRegistrationListener() { |
| 46 | *Prev = Next; |
| 47 | } |
| 48 | |
Chris Lattner | d7099bc | 2004-07-11 04:00:19 +0000 | [diff] [blame] | 49 | /// getClosestStaticTargetForModule - Given an LLVM module, pick the best target |
| 50 | /// that is compatible with the module. If no close target can be found, this |
| 51 | /// returns null and sets the Error string to a reason. |
| 52 | const TargetMachineRegistry::Entry * |
| 53 | TargetMachineRegistry::getClosestStaticTargetForModule(const Module &M, |
| 54 | std::string &Error) { |
| 55 | std::vector<std::pair<unsigned, const Entry *> > UsableTargets; |
| 56 | for (const Entry *E = getList(); E; E = E->getNext()) |
| 57 | if (unsigned Qual = E->ModuleMatchQualityFn(M)) |
| 58 | UsableTargets.push_back(std::make_pair(Qual, E)); |
| 59 | |
| 60 | if (UsableTargets.empty()) { |
| 61 | Error = "No available targets are compatible with this module"; |
| 62 | return 0; |
| 63 | } else if (UsableTargets.size() == 1) |
| 64 | return UsableTargets.back().second; |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 65 | |
Chris Lattner | d7099bc | 2004-07-11 04:00:19 +0000 | [diff] [blame] | 66 | // Otherwise, take the best target, but make sure we don't have to equally |
| 67 | // good best targets. |
| 68 | std::sort(UsableTargets.begin(), UsableTargets.end()); |
| 69 | if (UsableTargets.back().first ==UsableTargets[UsableTargets.size()-2].first){ |
| 70 | Error = "Cannot choose between targets \"" + |
| 71 | std::string(UsableTargets.back().second->Name) + "\" and \"" + |
| 72 | std::string(UsableTargets[UsableTargets.size()-2].second->Name) + "\""; |
| 73 | return 0; |
| 74 | } |
| 75 | return UsableTargets.back().second; |
| 76 | } |
| 77 | |
| 78 | /// getClosestTargetForJIT - Given an LLVM module, pick the best target that |
| 79 | /// is compatible with the current host and the specified module. If no |
| 80 | /// close target can be found, this returns null and sets the Error string |
| 81 | /// to a reason. |
| 82 | const TargetMachineRegistry::Entry * |
| 83 | TargetMachineRegistry::getClosestTargetForJIT(std::string &Error) { |
| 84 | std::vector<std::pair<unsigned, const Entry *> > UsableTargets; |
| 85 | for (const Entry *E = getList(); E; E = E->getNext()) |
| 86 | if (unsigned Qual = E->JITMatchQualityFn()) |
| 87 | UsableTargets.push_back(std::make_pair(Qual, E)); |
| 88 | |
| 89 | if (UsableTargets.empty()) { |
| 90 | Error = "No JIT is available for this host"; |
| 91 | return 0; |
| 92 | } else if (UsableTargets.size() == 1) |
| 93 | return UsableTargets.back().second; |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 94 | |
Chris Lattner | d7099bc | 2004-07-11 04:00:19 +0000 | [diff] [blame] | 95 | // Otherwise, take the best target. If there is a tie, just pick one. |
| 96 | unsigned MaxQual = UsableTargets.front().first; |
| 97 | const Entry *MaxQualTarget = UsableTargets.front().second; |
| 98 | |
| 99 | for (unsigned i = 1, e = UsableTargets.size(); i != e; ++i) |
| 100 | if (UsableTargets[i].first > MaxQual) { |
| 101 | MaxQual = UsableTargets[i].first; |
| 102 | MaxQualTarget = UsableTargets[i].second; |
| 103 | } |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 104 | |
Chris Lattner | d7099bc | 2004-07-11 04:00:19 +0000 | [diff] [blame] | 105 | return MaxQualTarget; |
| 106 | } |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 107 | |