2003-12-20 02:46:27 +01:00
|
|
|
//===-- TargetSelect.cpp - Target Chooser Code ----------------------------===//
|
2005-04-22 00:55:34 +02:00
|
|
|
//
|
2003-12-20 02:46:27 +01:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 00:55:34 +02:00
|
|
|
//
|
2003-12-20 02:46:27 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2009-07-16 04:23:53 +02: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 02:46:27 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "JIT.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/ModuleProvider.h"
|
2009-07-16 06:01:35 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2008-05-23 22:40:06 +02:00
|
|
|
#include "llvm/Support/Streams.h"
|
2005-09-01 23:38:21 +02:00
|
|
|
#include "llvm/Target/SubtargetFeature.h"
|
2003-12-20 02:46:27 +01:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2009-07-16 04:23:53 +02:00
|
|
|
#include "llvm/Target/TargetRegistry.h"
|
2003-12-20 02:46:27 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-07-16 04:23:53 +02:00
|
|
|
static cl::opt<std::string>
|
|
|
|
MArch("march", cl::desc("Architecture to generate assembly for (see --version)"));
|
2003-12-20 02:46:27 +01:00
|
|
|
|
2005-09-01 23:38:21 +02:00
|
|
|
static cl::opt<std::string>
|
2009-01-16 07:53:46 +01:00
|
|
|
MCPU("mcpu",
|
2005-10-24 00:39:01 +02:00
|
|
|
cl::desc("Target a specific cpu type (-mcpu=help for details)"),
|
2005-09-01 23:38:21 +02:00
|
|
|
cl::value_desc("cpu-name"),
|
|
|
|
cl::init(""));
|
|
|
|
|
|
|
|
static cl::list<std::string>
|
2009-01-16 07:53:46 +01:00
|
|
|
MAttrs("mattr",
|
2005-09-01 23:38:21 +02:00
|
|
|
cl::CommaSeparated,
|
2005-10-24 00:39:01 +02:00
|
|
|
cl::desc("Target specific attributes (-mattr=help for details)"),
|
|
|
|
cl::value_desc("a1,+a2,-a3,..."));
|
2005-09-01 23:38:21 +02:00
|
|
|
|
2009-07-18 02:42:18 +02:00
|
|
|
/// selectTarget - Pick a target either via -march or by guessing the native
|
|
|
|
/// arch. Add any CPU features specified via -mcpu or -mattr.
|
|
|
|
TargetMachine *JIT::selectTarget(ModuleProvider *MP, std::string *ErrorStr) {
|
2009-07-16 04:23:53 +02:00
|
|
|
const Target *TheTarget = 0;
|
|
|
|
if (MArch.empty()) {
|
2004-07-11 06:02:06 +02:00
|
|
|
std::string Error;
|
2009-07-15 22:24:03 +02:00
|
|
|
TheTarget = TargetRegistry::getClosestTargetForJIT(Error);
|
|
|
|
if (TheTarget == 0) {
|
2007-03-03 19:19:18 +01:00
|
|
|
if (ErrorStr)
|
|
|
|
*ErrorStr = Error;
|
|
|
|
return 0;
|
|
|
|
}
|
2009-07-15 22:24:03 +02:00
|
|
|
} else {
|
2009-07-16 04:23:53 +02:00
|
|
|
for (TargetRegistry::iterator it = TargetRegistry::begin(),
|
|
|
|
ie = TargetRegistry::end(); it != ie; ++it) {
|
|
|
|
if (MArch == it->getName()) {
|
|
|
|
TheTarget = &*it;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TheTarget == 0) {
|
|
|
|
if (ErrorStr)
|
|
|
|
*ErrorStr = std::string("invalid target '" + MArch + "'.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-15 22:24:03 +02:00
|
|
|
if (TheTarget->getJITMatchQuality() == 0) {
|
|
|
|
cerr << "WARNING: This target JIT is not designed for the host you are"
|
|
|
|
<< " running. If bad things happen, please choose a different "
|
|
|
|
<< "-march switch.\n";
|
|
|
|
}
|
2003-12-20 02:46:27 +01:00
|
|
|
}
|
|
|
|
|
2005-09-01 23:38:21 +02:00
|
|
|
// Package up features to be passed to target/subtarget
|
|
|
|
std::string FeaturesStr;
|
2008-07-07 19:52:43 +02:00
|
|
|
if (!MCPU.empty() || !MAttrs.empty()) {
|
2005-09-01 23:38:21 +02:00
|
|
|
SubtargetFeatures Features;
|
|
|
|
Features.setCPU(MCPU);
|
|
|
|
for (unsigned i = 0; i != MAttrs.size(); ++i)
|
|
|
|
Features.AddFeature(MAttrs[i]);
|
|
|
|
FeaturesStr = Features.getString();
|
|
|
|
}
|
|
|
|
|
2003-12-20 02:46:27 +01:00
|
|
|
// Allocate a target...
|
2009-07-15 22:24:03 +02:00
|
|
|
TargetMachine *Target =
|
|
|
|
TheTarget->createTargetMachine(*MP->getModule(), FeaturesStr);
|
2003-12-20 02:46:27 +01:00
|
|
|
assert(Target && "Could not allocate target machine!");
|
2009-07-18 02:42:18 +02:00
|
|
|
return Target;
|
2003-12-20 02:46:27 +01:00
|
|
|
}
|