LLVM API Documentation

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 is distributed under the University of Illinois Open Source
00006 // 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 /// getClosestStaticTargetForModule - Given an LLVM module, pick the best target
00022 /// that is compatible with the module.  If no close target can be found, this
00023 /// returns null and sets the Error string to a reason.
00024 const TargetMachineRegistry::entry *
00025 TargetMachineRegistry::getClosestStaticTargetForModule(const Module &M,
00026                                                        std::string &Error) {
00027   std::vector<std::pair<unsigned, const entry *> > UsableTargets;
00028   for (Registry<TargetMachine>::iterator I = begin(), E = end(); I != E; ++I)
00029     if (unsigned Qual = I->ModuleMatchQualityFn(M))
00030       UsableTargets.push_back(std::make_pair(Qual, &*I));
00031 
00032   if (UsableTargets.empty()) {
00033     Error = "No available targets are compatible with this module";
00034     return 0;
00035   } else if (UsableTargets.size() == 1)
00036     return UsableTargets.back().second;
00037 
00038   // Otherwise, take the best target, but make sure we don't have two equally
00039   // good best targets.
00040   std::sort(UsableTargets.begin(), UsableTargets.end());
00041   if (UsableTargets.back().first ==UsableTargets[UsableTargets.size()-2].first){
00042     Error = "Cannot choose between targets \"" +
00043       std::string(UsableTargets.back().second->Name) + "\" and \"" +
00044       std::string(UsableTargets[UsableTargets.size()-2].second->Name) + "\"";
00045     return 0;
00046   }
00047   return UsableTargets.back().second;
00048 }
00049 
00050 /// getClosestTargetForJIT - Pick the best target that is compatible with
00051 /// the current host.  If no close target can be found, this returns null
00052 /// and sets the Error string to a reason.
00053 const TargetMachineRegistry::entry *
00054 TargetMachineRegistry::getClosestTargetForJIT(std::string &Error) {
00055   std::vector<std::pair<unsigned, const entry *> > UsableTargets;
00056   for (Registry<TargetMachine>::iterator I = begin(), E = end(); I != E; ++I)
00057     if (unsigned Qual = I->JITMatchQualityFn())
00058       UsableTargets.push_back(std::make_pair(Qual, &*I));
00059 
00060   if (UsableTargets.empty()) {
00061     Error = "No JIT is available for this host";
00062     return 0;
00063   } else if (UsableTargets.size() == 1)
00064     return UsableTargets.back().second;
00065 
00066   // Otherwise, take the best target.  If there is a tie, just pick one.
00067   unsigned MaxQual = UsableTargets.front().first;
00068   const entry *MaxQualTarget = UsableTargets.front().second;
00069 
00070   for (unsigned i = 1, e = UsableTargets.size(); i != e; ++i)
00071     if (UsableTargets[i].first > MaxQual) {
00072       MaxQual = UsableTargets[i].first;
00073       MaxQualTarget = UsableTargets[i].second;
00074     }
00075 
00076   return MaxQualTarget;
00077 }
00078 



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.