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 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // 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 | d7099bc | 2004-07-11 04:00:19 +0000 | [diff] [blame] | 21 | /// getClosestStaticTargetForModule - Given an LLVM module, pick the best target |
| 22 | /// that is compatible with the module. If no close target can be found, this |
| 23 | /// returns null and sets the Error string to a reason. |
Gordon Henriksen | 4b2b940 | 2007-10-17 21:28:48 +0000 | [diff] [blame] | 24 | const TargetMachineRegistry::entry * |
Chris Lattner | d7099bc | 2004-07-11 04:00:19 +0000 | [diff] [blame] | 25 | TargetMachineRegistry::getClosestStaticTargetForModule(const Module &M, |
| 26 | std::string &Error) { |
Gordon Henriksen | 4b2b940 | 2007-10-17 21:28:48 +0000 | [diff] [blame] | 27 | std::vector<std::pair<unsigned, const entry *> > UsableTargets; |
Gordon Henriksen | 38ffcc9 | 2007-10-18 11:53:05 +0000 | [diff] [blame] | 28 | for (Registry<TargetMachine>::iterator I = begin(), E = end(); I != E; ++I) |
Gordon Henriksen | 4b2b940 | 2007-10-17 21:28:48 +0000 | [diff] [blame] | 29 | if (unsigned Qual = I->ModuleMatchQualityFn(M)) |
| 30 | UsableTargets.push_back(std::make_pair(Qual, &*I)); |
Chris Lattner | d7099bc | 2004-07-11 04:00:19 +0000 | [diff] [blame] | 31 | |
| 32 | if (UsableTargets.empty()) { |
| 33 | Error = "No available targets are compatible with this module"; |
| 34 | return 0; |
| 35 | } else if (UsableTargets.size() == 1) |
| 36 | return UsableTargets.back().second; |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 37 | |
Dan Gohman | 62bdec0 | 2008-07-22 00:52:04 +0000 | [diff] [blame] | 38 | // Otherwise, take the best target, but make sure we don't have two equally |
Chris Lattner | d7099bc | 2004-07-11 04:00:19 +0000 | [diff] [blame] | 39 | // good best targets. |
| 40 | std::sort(UsableTargets.begin(), UsableTargets.end()); |
| 41 | if (UsableTargets.back().first ==UsableTargets[UsableTargets.size()-2].first){ |
| 42 | Error = "Cannot choose between targets \"" + |
| 43 | std::string(UsableTargets.back().second->Name) + "\" and \"" + |
| 44 | std::string(UsableTargets[UsableTargets.size()-2].second->Name) + "\""; |
| 45 | return 0; |
| 46 | } |
| 47 | return UsableTargets.back().second; |
| 48 | } |
| 49 | |
Dan Gohman | fb71d38 | 2007-07-30 14:58:59 +0000 | [diff] [blame] | 50 | /// getClosestTargetForJIT - Pick the best target that is compatible with |
| 51 | /// the current host. If no close target can be found, this returns null |
| 52 | /// and sets the Error string to a reason. |
Gordon Henriksen | 4b2b940 | 2007-10-17 21:28:48 +0000 | [diff] [blame] | 53 | const TargetMachineRegistry::entry * |
Chris Lattner | d7099bc | 2004-07-11 04:00:19 +0000 | [diff] [blame] | 54 | TargetMachineRegistry::getClosestTargetForJIT(std::string &Error) { |
Gordon Henriksen | 4b2b940 | 2007-10-17 21:28:48 +0000 | [diff] [blame] | 55 | std::vector<std::pair<unsigned, const entry *> > UsableTargets; |
Gordon Henriksen | 38ffcc9 | 2007-10-18 11:53:05 +0000 | [diff] [blame] | 56 | for (Registry<TargetMachine>::iterator I = begin(), E = end(); I != E; ++I) |
Gordon Henriksen | 4b2b940 | 2007-10-17 21:28:48 +0000 | [diff] [blame] | 57 | if (unsigned Qual = I->JITMatchQualityFn()) |
| 58 | UsableTargets.push_back(std::make_pair(Qual, &*I)); |
Chris Lattner | d7099bc | 2004-07-11 04:00:19 +0000 | [diff] [blame] | 59 | |
| 60 | if (UsableTargets.empty()) { |
| 61 | Error = "No JIT is available for this host"; |
| 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. If there is a tie, just pick one. |
| 67 | unsigned MaxQual = UsableTargets.front().first; |
Gordon Henriksen | 4b2b940 | 2007-10-17 21:28:48 +0000 | [diff] [blame] | 68 | const entry *MaxQualTarget = UsableTargets.front().second; |
Chris Lattner | d7099bc | 2004-07-11 04:00:19 +0000 | [diff] [blame] | 69 | |
| 70 | for (unsigned i = 1, e = UsableTargets.size(); i != e; ++i) |
| 71 | if (UsableTargets[i].first > MaxQual) { |
| 72 | MaxQual = UsableTargets[i].first; |
| 73 | MaxQualTarget = UsableTargets[i].second; |
| 74 | } |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 75 | |
Chris Lattner | d7099bc | 2004-07-11 04:00:19 +0000 | [diff] [blame] | 76 | return MaxQualTarget; |
| 77 | } |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 78 | |