blob: b462ee73695177f07d8589966b141f4b478367c7 [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"
Daniel Dunbar4baa5fe2009-08-03 04:03:51 +000018#include "llvm/ADT/Triple.h"
Daniel Dunbarcd862072009-07-16 04:01:35 +000019#include "llvm/Support/CommandLine.h"
Daniel Dunbar4baa5fe2009-08-03 04:03:51 +000020#include "llvm/Support/raw_ostream.h"
21#include "llvm/System/Host.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/Target/SubtargetFeature.h"
23#include "llvm/Target/TargetMachine.h"
Daniel Dunbar6e570c82009-07-16 02:23:53 +000024#include "llvm/Target/TargetRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025using namespace llvm;
26
Daniel Dunbar6e570c82009-07-16 02:23:53 +000027static cl::opt<std::string>
Daniel Dunbar4b349f22009-09-08 23:32:35 +000028MArch("march",
29 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.
Jeffrey Yasskin62de4e72010-01-27 20:34:15 +000045TargetMachine *JIT::selectTarget(Module *Mod, std::string *ErrorStr) {
46 Triple TheTriple(Mod->getTargetTriple());
Evan Cheng5f0d0812009-09-02 00:19:03 +000047 if (TheTriple.getTriple().empty())
48 TheTriple.setTriple(sys::getHostTriple());
Daniel Dunbar6e570c82009-07-16 02:23:53 +000049
Daniel Dunbar4baa5fe2009-08-03 04:03:51 +000050 // Adjust the triple to match what the user requested.
Evan Cheng5f0d0812009-09-02 00:19:03 +000051 const Target *TheTarget = 0;
52 if (!MArch.empty()) {
53 for (TargetRegistry::iterator it = TargetRegistry::begin(),
54 ie = TargetRegistry::end(); it != ie; ++it) {
55 if (MArch == it->getName()) {
56 TheTarget = &*it;
57 break;
58 }
59 }
Daniel Dunbar4baa5fe2009-08-03 04:03:51 +000060
Evan Cheng5f0d0812009-09-02 00:19:03 +000061 if (!TheTarget) {
Daniel Dunbar4b349f22009-09-08 23:32:35 +000062 *ErrorStr = "No available targets are compatible with this -march, "
63 "see -version for the available targets.\n";
Evan Cheng5f0d0812009-09-02 00:19:03 +000064 return 0;
65 }
66
67 // Adjust the triple to match (if known), otherwise stick with the
68 // module/host triple.
69 Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
70 if (Type != Triple::UnknownArch)
71 TheTriple.setArch(Type);
72 } else {
73 std::string Error;
74 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
75 if (TheTarget == 0) {
76 if (ErrorStr)
77 *ErrorStr = Error;
78 return 0;
79 }
Daniel Dunbar4baa5fe2009-08-03 04:03:51 +000080 }
81
82 if (!TheTarget->hasJIT()) {
83 errs() << "WARNING: This target JIT is not designed for the host you are"
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000084 << " running. If bad things happen, please choose a different "
85 << "-march switch.\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086 }
87
88 // Package up features to be passed to target/subtarget
89 std::string FeaturesStr;
Dan Gohman775977b2008-07-07 17:52:43 +000090 if (!MCPU.empty() || !MAttrs.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 SubtargetFeatures Features;
92 Features.setCPU(MCPU);
93 for (unsigned i = 0; i != MAttrs.size(); ++i)
94 Features.AddFeature(MAttrs[i]);
95 FeaturesStr = Features.getString();
96 }
97
98 // Allocate a target...
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000099 TargetMachine *Target =
Daniel Dunbar09a25f32009-08-04 04:08:40 +0000100 TheTarget->createTargetMachine(TheTriple.getTriple(), FeaturesStr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 assert(Target && "Could not allocate target machine!");
Reid Klecknerfa3585b2009-07-18 00:42:18 +0000102 return Target;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103}