blob: 62c76bbc5bf14ed2c93e71a7b1ccf1b2880adad1 [file] [log] [blame]
Chris Lattnerbf573372004-07-11 02:44:26 +00001//===-- TargetMachineRegistry.cpp - Target Auto Registration Impl ---------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Chris Lattnerbf573372004-07-11 02:44:26 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
Chris Lattnerbf573372004-07-11 02:44:26 +00008//===----------------------------------------------------------------------===//
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
Gordon Henriksen4b2b9402007-10-17 21:28:48 +000021template<> Registry<TargetMachine>::node *Registry<TargetMachine>::Head = 0;
22template<> Registry<TargetMachine>::node *Registry<TargetMachine>::Tail = 0;
23template<> Registry<TargetMachine>::listener *Registry<TargetMachine>::
24ListenerHead = 0;
25template<> Registry<TargetMachine>::listener *Registry<TargetMachine>::
26ListenerTail = 0;
Chris Lattner7c38def2004-07-11 06:03:21 +000027
Chris Lattnerd7099bc2004-07-11 04:00:19 +000028/// getClosestStaticTargetForModule - Given an LLVM module, pick the best target
29/// that is compatible with the module. If no close target can be found, this
30/// returns null and sets the Error string to a reason.
Gordon Henriksen4b2b9402007-10-17 21:28:48 +000031const TargetMachineRegistry::entry *
Chris Lattnerd7099bc2004-07-11 04:00:19 +000032TargetMachineRegistry::getClosestStaticTargetForModule(const Module &M,
33 std::string &Error) {
Gordon Henriksen4b2b9402007-10-17 21:28:48 +000034 std::vector<std::pair<unsigned, const entry *> > UsableTargets;
Gordon Henriksen38ffcc92007-10-18 11:53:05 +000035 for (Registry<TargetMachine>::iterator I = begin(), E = end(); I != E; ++I)
Gordon Henriksen4b2b9402007-10-17 21:28:48 +000036 if (unsigned Qual = I->ModuleMatchQualityFn(M))
37 UsableTargets.push_back(std::make_pair(Qual, &*I));
Chris Lattnerd7099bc2004-07-11 04:00:19 +000038
39 if (UsableTargets.empty()) {
40 Error = "No available targets are compatible with this module";
41 return 0;
42 } else if (UsableTargets.size() == 1)
43 return UsableTargets.back().second;
Misha Brukmanf976c852005-04-21 22:55:34 +000044
Chris Lattnerd7099bc2004-07-11 04:00:19 +000045 // Otherwise, take the best target, but make sure we don't have to equally
46 // good best targets.
47 std::sort(UsableTargets.begin(), UsableTargets.end());
48 if (UsableTargets.back().first ==UsableTargets[UsableTargets.size()-2].first){
49 Error = "Cannot choose between targets \"" +
50 std::string(UsableTargets.back().second->Name) + "\" and \"" +
51 std::string(UsableTargets[UsableTargets.size()-2].second->Name) + "\"";
52 return 0;
53 }
54 return UsableTargets.back().second;
55}
56
Dan Gohmanfb71d382007-07-30 14:58:59 +000057/// getClosestTargetForJIT - Pick the best target that is compatible with
58/// the current host. If no close target can be found, this returns null
59/// and sets the Error string to a reason.
Gordon Henriksen4b2b9402007-10-17 21:28:48 +000060const TargetMachineRegistry::entry *
Chris Lattnerd7099bc2004-07-11 04:00:19 +000061TargetMachineRegistry::getClosestTargetForJIT(std::string &Error) {
Gordon Henriksen4b2b9402007-10-17 21:28:48 +000062 std::vector<std::pair<unsigned, const entry *> > UsableTargets;
Gordon Henriksen38ffcc92007-10-18 11:53:05 +000063 for (Registry<TargetMachine>::iterator I = begin(), E = end(); I != E; ++I)
Gordon Henriksen4b2b9402007-10-17 21:28:48 +000064 if (unsigned Qual = I->JITMatchQualityFn())
65 UsableTargets.push_back(std::make_pair(Qual, &*I));
Chris Lattnerd7099bc2004-07-11 04:00:19 +000066
67 if (UsableTargets.empty()) {
68 Error = "No JIT is available for this host";
69 return 0;
70 } else if (UsableTargets.size() == 1)
71 return UsableTargets.back().second;
Misha Brukmanf976c852005-04-21 22:55:34 +000072
Chris Lattnerd7099bc2004-07-11 04:00:19 +000073 // Otherwise, take the best target. If there is a tie, just pick one.
74 unsigned MaxQual = UsableTargets.front().first;
Gordon Henriksen4b2b9402007-10-17 21:28:48 +000075 const entry *MaxQualTarget = UsableTargets.front().second;
Chris Lattnerd7099bc2004-07-11 04:00:19 +000076
77 for (unsigned i = 1, e = UsableTargets.size(); i != e; ++i)
78 if (UsableTargets[i].first > MaxQual) {
79 MaxQual = UsableTargets[i].first;
80 MaxQualTarget = UsableTargets[i].second;
81 }
Misha Brukmanf976c852005-04-21 22:55:34 +000082
Chris Lattnerd7099bc2004-07-11 04:00:19 +000083 return MaxQualTarget;
84}
Misha Brukmanf976c852005-04-21 22:55:34 +000085