Use the new predicate support that Evan Cheng added to remove some code
[oota-llvm.git] / lib / Target / TargetMachineRegistry.cpp
1 //===-- 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"
18 #include <algorithm>
19 using namespace llvm;
20
21 /// List - This is the main list of all of the registered target machines.
22 const TargetMachineRegistry::Entry *TargetMachineRegistry::List = 0;
23
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,
29                        TargetMachine *(*CF)(const Module &, IntrinsicLowering*,
30                                             const std::string &),
31                            unsigned (*MMF)(const Module &M), unsigned (*JMF)())
32   : Name(N), ShortDesc(SD), CtorFn(CF), ModuleMatchQualityFn(MMF),
33     JITMatchQualityFn(JMF), Next(List) {
34   List = this;
35   for (TargetRegistrationListener *L = Listeners; L; L = L->getNext())
36     L->targetRegistered(this);
37 }
38
39 TargetRegistrationListener::TargetRegistrationListener() {
40   Next = Listeners;
41   if (Next) Next->Prev = &Next;
42   Prev = &Listeners;
43   Listeners = this;
44 }
45
46 TargetRegistrationListener::~TargetRegistrationListener() {
47   *Prev = Next;
48 }
49
50 /// getClosestStaticTargetForModule - Given an LLVM module, pick the best target
51 /// that is compatible with the module.  If no close target can be found, this
52 /// returns null and sets the Error string to a reason.
53 const TargetMachineRegistry::Entry *
54 TargetMachineRegistry::getClosestStaticTargetForModule(const Module &M,
55                                                        std::string &Error) {
56   std::vector<std::pair<unsigned, const Entry *> > UsableTargets;
57   for (const Entry *E = getList(); E; E = E->getNext())
58     if (unsigned Qual = E->ModuleMatchQualityFn(M))
59       UsableTargets.push_back(std::make_pair(Qual, E));
60
61   if (UsableTargets.empty()) {
62     Error = "No available targets are compatible with this module";
63     return 0;
64   } else if (UsableTargets.size() == 1)
65     return UsableTargets.back().second;
66
67   // Otherwise, take the best target, but make sure we don't have to equally
68   // good best targets.
69   std::sort(UsableTargets.begin(), UsableTargets.end());
70   if (UsableTargets.back().first ==UsableTargets[UsableTargets.size()-2].first){
71     Error = "Cannot choose between targets \"" +
72       std::string(UsableTargets.back().second->Name) + "\" and \"" +
73       std::string(UsableTargets[UsableTargets.size()-2].second->Name) + "\"";
74     return 0;
75   }
76   return UsableTargets.back().second;
77 }
78
79 /// getClosestTargetForJIT - Given an LLVM module, pick the best target that
80 /// is compatible with the current host and the specified module.  If no
81 /// close target can be found, this returns null and sets the Error string
82 /// to a reason.
83 const TargetMachineRegistry::Entry *
84 TargetMachineRegistry::getClosestTargetForJIT(std::string &Error) {
85   std::vector<std::pair<unsigned, const Entry *> > UsableTargets;
86   for (const Entry *E = getList(); E; E = E->getNext())
87     if (unsigned Qual = E->JITMatchQualityFn())
88       UsableTargets.push_back(std::make_pair(Qual, E));
89
90   if (UsableTargets.empty()) {
91     Error = "No JIT is available for this host";
92     return 0;
93   } else if (UsableTargets.size() == 1)
94     return UsableTargets.back().second;
95
96   // Otherwise, take the best target.  If there is a tie, just pick one.
97   unsigned MaxQual = UsableTargets.front().first;
98   const Entry *MaxQualTarget = UsableTargets.front().second;
99
100   for (unsigned i = 1, e = UsableTargets.size(); i != e; ++i)
101     if (UsableTargets[i].first > MaxQual) {
102       MaxQual = UsableTargets[i].first;
103       MaxQualTarget = UsableTargets[i].second;
104     }
105
106   return MaxQualTarget;
107 }
108