2003-12-20 01:46:27 +00:00
|
|
|
//===-- TargetSelect.cpp - Target Chooser Code ----------------------------===//
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2003-12-20 01:46:27 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2003-12-20 01:46:27 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2009-07-16 02:23:53 +00:00
|
|
|
// This just asks the TargetRegistry for the appropriate JIT to use, and allows
|
|
|
|
// the user to specify a specific one on the commandline with -march=x. Clients
|
|
|
|
// should initialize targets prior to calling createJIT.
|
2003-12-20 01:46:27 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "JIT.h"
|
|
|
|
#include "llvm/Module.h"
|
2009-08-03 04:03:51 +00:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2009-07-16 04:01:35 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2009-08-03 04:03:51 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/System/Host.h"
|
2005-09-01 21:38:21 +00:00
|
|
|
#include "llvm/Target/SubtargetFeature.h"
|
2003-12-20 01:46:27 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2009-07-16 02:23:53 +00:00
|
|
|
#include "llvm/Target/TargetRegistry.h"
|
2003-12-20 01:46:27 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-07-18 00:42:18 +00:00
|
|
|
/// selectTarget - Pick a target either via -march or by guessing the native
|
|
|
|
/// arch. Add any CPU features specified via -mcpu or -mattr.
|
2010-02-05 16:19:36 +00:00
|
|
|
TargetMachine *JIT::selectTarget(Module *Mod,
|
|
|
|
StringRef MArch,
|
|
|
|
StringRef MCPU,
|
|
|
|
const SmallVectorImpl<std::string>& MAttrs,
|
|
|
|
std::string *ErrorStr) {
|
2010-01-27 20:34:15 +00:00
|
|
|
Triple TheTriple(Mod->getTargetTriple());
|
2009-09-02 00:19:03 +00:00
|
|
|
if (TheTriple.getTriple().empty())
|
|
|
|
TheTriple.setTriple(sys::getHostTriple());
|
2009-07-16 02:23:53 +00:00
|
|
|
|
2009-08-03 04:03:51 +00:00
|
|
|
// Adjust the triple to match what the user requested.
|
2009-09-02 00:19:03 +00:00
|
|
|
const Target *TheTarget = 0;
|
|
|
|
if (!MArch.empty()) {
|
|
|
|
for (TargetRegistry::iterator it = TargetRegistry::begin(),
|
|
|
|
ie = TargetRegistry::end(); it != ie; ++it) {
|
|
|
|
if (MArch == it->getName()) {
|
|
|
|
TheTarget = &*it;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!TheTarget) {
|
2009-09-08 23:32:35 +00:00
|
|
|
*ErrorStr = "No available targets are compatible with this -march, "
|
|
|
|
"see -version for the available targets.\n";
|
2009-09-02 00:19:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2009-08-03 04:03:51 +00:00
|
|
|
|
2009-09-02 00:19:03 +00:00
|
|
|
// Adjust the triple to match (if known), otherwise stick with the
|
|
|
|
// module/host triple.
|
|
|
|
Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
|
|
|
|
if (Type != Triple::UnknownArch)
|
|
|
|
TheTriple.setArch(Type);
|
|
|
|
} else {
|
|
|
|
std::string Error;
|
|
|
|
TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
|
|
|
|
if (TheTarget == 0) {
|
|
|
|
if (ErrorStr)
|
|
|
|
*ErrorStr = Error;
|
|
|
|
return 0;
|
|
|
|
}
|
2009-08-03 04:03:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!TheTarget->hasJIT()) {
|
|
|
|
errs() << "WARNING: This target JIT is not designed for the host you are"
|
2009-07-15 20:24:03 +00:00
|
|
|
<< " running. If bad things happen, please choose a different "
|
|
|
|
<< "-march switch.\n";
|
2003-12-20 01:46:27 +00:00
|
|
|
}
|
|
|
|
|
2005-09-01 21:38:21 +00:00
|
|
|
// Package up features to be passed to target/subtarget
|
|
|
|
std::string FeaturesStr;
|
2008-07-07 17:52:43 +00:00
|
|
|
if (!MCPU.empty() || !MAttrs.empty()) {
|
2005-09-01 21:38:21 +00:00
|
|
|
SubtargetFeatures Features;
|
|
|
|
Features.setCPU(MCPU);
|
|
|
|
for (unsigned i = 0; i != MAttrs.size(); ++i)
|
|
|
|
Features.AddFeature(MAttrs[i]);
|
|
|
|
FeaturesStr = Features.getString();
|
|
|
|
}
|
|
|
|
|
2003-12-20 01:46:27 +00:00
|
|
|
// Allocate a target...
|
2009-07-15 20:24:03 +00:00
|
|
|
TargetMachine *Target =
|
2009-08-04 04:08:40 +00:00
|
|
|
TheTarget->createTargetMachine(TheTriple.getTriple(), FeaturesStr);
|
2003-12-20 01:46:27 +00:00
|
|
|
assert(Target && "Could not allocate target machine!");
|
2009-07-18 00:42:18 +00:00
|
|
|
return Target;
|
2003-12-20 01:46:27 +00:00
|
|
|
}
|