X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FTarget%2FTargetMachineRegistry.cpp;h=c1a4777c63140304bbc2b412e877d967697e5b2f;hb=b3379fbc604cc9e11b8b7fcd1ecb09a40a886697;hp=f5286efaab3a6642c47bba59665e6a9b803fb28f;hpb=bf5733737fb8afbc0389fd2507c53a3c85b72ebd;p=oota-llvm.git diff --git a/lib/Target/TargetMachineRegistry.cpp b/lib/Target/TargetMachineRegistry.cpp index f5286efaab3..c1a4777c631 100644 --- a/lib/Target/TargetMachineRegistry.cpp +++ b/lib/Target/TargetMachineRegistry.cpp @@ -1,10 +1,10 @@ //===-- TargetMachineRegistry.cpp - Target Auto Registration Impl ---------===// -// +// // The LLVM Compiler Infrastructure // -// This file was developed by the LLVM research group and is distributed under -// the University of Illinois Open Source License. See LICENSE.TXT for details. -// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// //===----------------------------------------------------------------------===// // // This file exposes the RegisterTarget class, which TargetMachine @@ -15,7 +15,64 @@ //===----------------------------------------------------------------------===// #include "llvm/Target/TargetMachineRegistry.h" +#include using namespace llvm; -const TargetMachineRegistry::Entry *TargetMachineRegistry::List = 0; +/// getClosestStaticTargetForModule - Given an LLVM module, pick the best target +/// that is compatible with the module. If no close target can be found, this +/// returns null and sets the Error string to a reason. +const TargetMachineRegistry::entry * +TargetMachineRegistry::getClosestStaticTargetForModule(const Module &M, + std::string &Error) { + std::vector > UsableTargets; + for (Registry::iterator I = begin(), E = end(); I != E; ++I) + if (unsigned Qual = I->ModuleMatchQualityFn(M)) + UsableTargets.push_back(std::make_pair(Qual, &*I)); + + if (UsableTargets.empty()) { + Error = "No available targets are compatible with this module"; + return 0; + } else if (UsableTargets.size() == 1) + return UsableTargets.back().second; + + // Otherwise, take the best target, but make sure we don't have two equally + // good best targets. + std::sort(UsableTargets.begin(), UsableTargets.end()); + if (UsableTargets.back().first ==UsableTargets[UsableTargets.size()-2].first){ + Error = "Cannot choose between targets \"" + + std::string(UsableTargets.back().second->Name) + "\" and \"" + + std::string(UsableTargets[UsableTargets.size()-2].second->Name) + "\""; + return 0; + } + return UsableTargets.back().second; +} + +/// getClosestTargetForJIT - Pick the best target that is compatible with +/// the current host. If no close target can be found, this returns null +/// and sets the Error string to a reason. +const TargetMachineRegistry::entry * +TargetMachineRegistry::getClosestTargetForJIT(std::string &Error) { + std::vector > UsableTargets; + for (Registry::iterator I = begin(), E = end(); I != E; ++I) + if (unsigned Qual = I->JITMatchQualityFn()) + UsableTargets.push_back(std::make_pair(Qual, &*I)); + + if (UsableTargets.empty()) { + Error = "No JIT is available for this host"; + return 0; + } else if (UsableTargets.size() == 1) + return UsableTargets.back().second; + + // Otherwise, take the best target. If there is a tie, just pick one. + unsigned MaxQual = UsableTargets.front().first; + const entry *MaxQualTarget = UsableTargets.front().second; + + for (unsigned i = 1, e = UsableTargets.size(); i != e; ++i) + if (UsableTargets[i].first > MaxQual) { + MaxQual = UsableTargets[i].first; + MaxQualTarget = UsableTargets[i].second; + } + + return MaxQualTarget; +}