Fix bugs section.
[llvm-complete.git] / lib / Target / TargetMachineRegistry.cpp
blob62c76bbc5bf14ed2c93e71a7b1ccf1b2880adad1
1 //===-- TargetMachineRegistry.cpp - Target Auto Registration Impl ---------===//
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 // 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.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Target/TargetMachineRegistry.h"
18 #include <algorithm>
19 using namespace llvm;
21 template<> Registry<TargetMachine>::node *Registry<TargetMachine>::Head = 0;
22 template<> Registry<TargetMachine>::node *Registry<TargetMachine>::Tail = 0;
23 template<> Registry<TargetMachine>::listener *Registry<TargetMachine>::
24 ListenerHead = 0;
25 template<> Registry<TargetMachine>::listener *Registry<TargetMachine>::
26 ListenerTail = 0;
28 /// 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.
31 const TargetMachineRegistry::entry *
32 TargetMachineRegistry::getClosestStaticTargetForModule(const Module &M,
33 std::string &Error) {
34 std::vector<std::pair<unsigned, const entry *> > UsableTargets;
35 for (Registry<TargetMachine>::iterator I = begin(), E = end(); I != E; ++I)
36 if (unsigned Qual = I->ModuleMatchQualityFn(M))
37 UsableTargets.push_back(std::make_pair(Qual, &*I));
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;
45 // 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;
54 return UsableTargets.back().second;
57 /// 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.
60 const TargetMachineRegistry::entry *
61 TargetMachineRegistry::getClosestTargetForJIT(std::string &Error) {
62 std::vector<std::pair<unsigned, const entry *> > UsableTargets;
63 for (Registry<TargetMachine>::iterator I = begin(), E = end(); I != E; ++I)
64 if (unsigned Qual = I->JITMatchQualityFn())
65 UsableTargets.push_back(std::make_pair(Qual, &*I));
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;
73 // Otherwise, take the best target. If there is a tie, just pick one.
74 unsigned MaxQual = UsableTargets.front().first;
75 const entry *MaxQualTarget = UsableTargets.front().second;
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;
83 return MaxQualTarget;