LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

TargetMachineRegistry.cpp

Go to the documentation of this file.
00001 //===-- TargetMachineRegistry.cpp - Target Auto Registration Impl ---------===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file exposes the RegisterTarget class, which TargetMachine
00011 // implementations should use to register themselves with the system.  This file
00012 // also exposes the TargetMachineRegistry class, which allows tools to inspect
00013 // all of registered targets.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #include "llvm/Target/TargetMachineRegistry.h"
00018 #include <algorithm>
00019 using namespace llvm;
00020 
00021 /// List - This is the main list of all of the registered target machines.
00022 const TargetMachineRegistry::Entry *TargetMachineRegistry::List = 0;
00023 
00024 /// Listeners - All of the listeners registered to get notified when new targets
00025 /// are loaded.
00026 static TargetRegistrationListener *Listeners = 0;
00027 
00028 TargetMachineRegistry::Entry::Entry(const char *N, const char *SD,
00029                        TargetMachine *(*CF)(const Module &, IntrinsicLowering*),
00030                            unsigned (*MMF)(const Module &M), unsigned (*JMF)())
00031   : Name(N), ShortDesc(SD), CtorFn(CF), ModuleMatchQualityFn(MMF),
00032     JITMatchQualityFn(JMF), Next(List) {
00033   List = this;
00034   for (TargetRegistrationListener *L = Listeners; L; L = L->getNext())
00035     L->targetRegistered(this);
00036 }
00037 
00038 TargetRegistrationListener::TargetRegistrationListener() {
00039   Next = Listeners;
00040   if (Next) Next->Prev = &Next;
00041   Prev = &Listeners;
00042   Listeners = this;
00043 }
00044 
00045 TargetRegistrationListener::~TargetRegistrationListener() {
00046   *Prev = Next;
00047 }
00048 
00049 /// getClosestStaticTargetForModule - Given an LLVM module, pick the best target
00050 /// that is compatible with the module.  If no close target can be found, this
00051 /// returns null and sets the Error string to a reason.
00052 const TargetMachineRegistry::Entry *
00053 TargetMachineRegistry::getClosestStaticTargetForModule(const Module &M,
00054                                                        std::string &Error) {
00055   std::vector<std::pair<unsigned, const Entry *> > UsableTargets;
00056   for (const Entry *E = getList(); E; E = E->getNext())
00057     if (unsigned Qual = E->ModuleMatchQualityFn(M))
00058       UsableTargets.push_back(std::make_pair(Qual, E));
00059 
00060   if (UsableTargets.empty()) {
00061     Error = "No available targets are compatible with this module";
00062     return 0;
00063   } else if (UsableTargets.size() == 1)
00064     return UsableTargets.back().second;
00065   
00066   // Otherwise, take the best target, but make sure we don't have to equally
00067   // good best targets.
00068   std::sort(UsableTargets.begin(), UsableTargets.end());
00069   if (UsableTargets.back().first ==UsableTargets[UsableTargets.size()-2].first){
00070     Error = "Cannot choose between targets \"" +
00071       std::string(UsableTargets.back().second->Name) + "\" and \"" +
00072       std::string(UsableTargets[UsableTargets.size()-2].second->Name) + "\"";
00073     return 0;
00074   }
00075   return UsableTargets.back().second;
00076 }
00077 
00078 /// getClosestTargetForJIT - Given an LLVM module, pick the best target that
00079 /// is compatible with the current host and the specified module.  If no
00080 /// close target can be found, this returns null and sets the Error string
00081 /// to a reason.
00082 const TargetMachineRegistry::Entry *
00083 TargetMachineRegistry::getClosestTargetForJIT(std::string &Error) {
00084   std::vector<std::pair<unsigned, const Entry *> > UsableTargets;
00085   for (const Entry *E = getList(); E; E = E->getNext())
00086     if (unsigned Qual = E->JITMatchQualityFn())
00087       UsableTargets.push_back(std::make_pair(Qual, E));
00088 
00089   if (UsableTargets.empty()) {
00090     Error = "No JIT is available for this host";
00091     return 0;
00092   } else if (UsableTargets.size() == 1)
00093     return UsableTargets.back().second;
00094   
00095   // Otherwise, take the best target.  If there is a tie, just pick one.
00096   unsigned MaxQual = UsableTargets.front().first;
00097   const Entry *MaxQualTarget = UsableTargets.front().second;
00098 
00099   for (unsigned i = 1, e = UsableTargets.size(); i != e; ++i)
00100     if (UsableTargets[i].first > MaxQual) {
00101       MaxQual = UsableTargets[i].first;
00102       MaxQualTarget = UsableTargets[i].second;
00103     }
00104   
00105   return MaxQualTarget;
00106 }
00107