blob: ecbfc82932b35862d89c54bc0c0e6c0b5a718eaa [file] [log] [blame]
Chris Lattnerbf573372004-07-11 02:44:26 +00001//===-- TargetMachineRegistry.cpp - Target Auto Registration Impl ---------===//
2//
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.
7//
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 Lattnerd7099bc2004-07-11 04:00:19 +000018#include <algorithm>
Chris Lattnerbf573372004-07-11 02:44:26 +000019using namespace llvm;
20
21const TargetMachineRegistry::Entry *TargetMachineRegistry::List = 0;
22
Chris Lattnerd7099bc2004-07-11 04:00:19 +000023/// getClosestStaticTargetForModule - Given an LLVM module, pick the best target
24/// that is compatible with the module. If no close target can be found, this
25/// returns null and sets the Error string to a reason.
26const TargetMachineRegistry::Entry *
27TargetMachineRegistry::getClosestStaticTargetForModule(const Module &M,
28 std::string &Error) {
29 std::vector<std::pair<unsigned, const Entry *> > UsableTargets;
30 for (const Entry *E = getList(); E; E = E->getNext())
31 if (unsigned Qual = E->ModuleMatchQualityFn(M))
32 UsableTargets.push_back(std::make_pair(Qual, E));
33
34 if (UsableTargets.empty()) {
35 Error = "No available targets are compatible with this module";
36 return 0;
37 } else if (UsableTargets.size() == 1)
38 return UsableTargets.back().second;
39
40 // Otherwise, take the best target, but make sure we don't have to equally
41 // good best targets.
42 std::sort(UsableTargets.begin(), UsableTargets.end());
43 if (UsableTargets.back().first ==UsableTargets[UsableTargets.size()-2].first){
44 Error = "Cannot choose between targets \"" +
45 std::string(UsableTargets.back().second->Name) + "\" and \"" +
46 std::string(UsableTargets[UsableTargets.size()-2].second->Name) + "\"";
47 return 0;
48 }
49 return UsableTargets.back().second;
50}
51
52/// getClosestTargetForJIT - Given an LLVM module, pick the best target that
53/// is compatible with the current host and the specified module. If no
54/// close target can be found, this returns null and sets the Error string
55/// to a reason.
56const TargetMachineRegistry::Entry *
57TargetMachineRegistry::getClosestTargetForJIT(std::string &Error) {
58 std::vector<std::pair<unsigned, const Entry *> > UsableTargets;
59 for (const Entry *E = getList(); E; E = E->getNext())
60 if (unsigned Qual = E->JITMatchQualityFn())
61 UsableTargets.push_back(std::make_pair(Qual, E));
62
63 if (UsableTargets.empty()) {
64 Error = "No JIT is available for this host";
65 return 0;
66 } else if (UsableTargets.size() == 1)
67 return UsableTargets.back().second;
68
69 // Otherwise, take the best target. If there is a tie, just pick one.
70 unsigned MaxQual = UsableTargets.front().first;
71 const Entry *MaxQualTarget = UsableTargets.front().second;
72
73 for (unsigned i = 1, e = UsableTargets.size(); i != e; ++i)
74 if (UsableTargets[i].first > MaxQual) {
75 MaxQual = UsableTargets[i].first;
76 MaxQualTarget = UsableTargets[i].second;
77 }
78
79 return MaxQualTarget;
80}
81