blob: c30a698822bc2da5b7744ba03dac7ce42ab7b7fd [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- TargetSelect.cpp - Target Chooser Code ----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
Daniel Dunbar6e570c82009-07-16 02:23:53 +000010// This just asks the TargetRegistry for the appropriate JIT to use, and allows
11// the user to specify a specific one on the commandline with -march=x. Clients
12// should initialize targets prior to calling createJIT.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000013//
14//===----------------------------------------------------------------------===//
15
16#include "JIT.h"
17#include "llvm/Module.h"
18#include "llvm/ModuleProvider.h"
Daniel Dunbar4baa5fe2009-08-03 04:03:51 +000019#include "llvm/ADT/Triple.h"
Daniel Dunbarcd862072009-07-16 04:01:35 +000020#include "llvm/Support/CommandLine.h"
Daniel Dunbar4baa5fe2009-08-03 04:03:51 +000021#include "llvm/Support/raw_ostream.h"
22#include "llvm/System/Host.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/Target/SubtargetFeature.h"
24#include "llvm/Target/TargetMachine.h"
Daniel Dunbar6e570c82009-07-16 02:23:53 +000025#include "llvm/Target/TargetRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026using namespace llvm;
27
Daniel Dunbar6e570c82009-07-16 02:23:53 +000028static cl::opt<std::string>
29MArch("march", cl::desc("Architecture to generate assembly for (see --version)"));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030
31static cl::opt<std::string>
Mikhail Glushenkov26e9aed2009-01-16 06:53:46 +000032MCPU("mcpu",
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
34 cl::value_desc("cpu-name"),
35 cl::init(""));
36
37static cl::list<std::string>
Mikhail Glushenkov26e9aed2009-01-16 06:53:46 +000038MAttrs("mattr",
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039 cl::CommaSeparated,
40 cl::desc("Target specific attributes (-mattr=help for details)"),
41 cl::value_desc("a1,+a2,-a3,..."));
42
Reid Klecknerfa3585b2009-07-18 00:42:18 +000043/// selectTarget - Pick a target either via -march or by guessing the native
44/// arch. Add any CPU features specified via -mcpu or -mattr.
45TargetMachine *JIT::selectTarget(ModuleProvider *MP, std::string *ErrorStr) {
Daniel Dunbar4baa5fe2009-08-03 04:03:51 +000046 Triple TheTriple(sys::getHostTriple());
Daniel Dunbar6e570c82009-07-16 02:23:53 +000047
Daniel Dunbar4baa5fe2009-08-03 04:03:51 +000048 // Adjust the triple to match what the user requested.
49 if (!MArch.empty())
50 TheTriple.setArch(Triple::getArchTypeForLLVMName(MArch));
51
52 std::string Error;
53 const Target *TheTarget =
Daniel Dunbar9aa08272009-08-03 04:20:57 +000054 TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
Daniel Dunbar4baa5fe2009-08-03 04:03:51 +000055 if (TheTarget == 0) {
56 if (ErrorStr)
57 *ErrorStr = Error;
58 return 0;
59 }
60
61 if (!TheTarget->hasJIT()) {
62 errs() << "WARNING: This target JIT is not designed for the host you are"
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000063 << " running. If bad things happen, please choose a different "
64 << "-march switch.\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 }
66
67 // Package up features to be passed to target/subtarget
68 std::string FeaturesStr;
Dan Gohman775977b2008-07-07 17:52:43 +000069 if (!MCPU.empty() || !MAttrs.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 SubtargetFeatures Features;
71 Features.setCPU(MCPU);
72 for (unsigned i = 0; i != MAttrs.size(); ++i)
73 Features.AddFeature(MAttrs[i]);
74 FeaturesStr = Features.getString();
75 }
76
77 // Allocate a target...
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000078 TargetMachine *Target =
Daniel Dunbar4baa5fe2009-08-03 04:03:51 +000079 TheTarget->createTargetMachine(*MP->getModule(), TheTriple.getTriple(),
80 FeaturesStr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 assert(Target && "Could not allocate target machine!");
Reid Klecknerfa3585b2009-07-18 00:42:18 +000082 return Target;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083}