blob: 8bed33bb7d42c9dc7bc6d758d5200145b8927884 [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>
Daniel Dunbar4b349f22009-09-08 23:32:35 +000029MArch("march",
30 cl::desc("Architecture to generate assembly for (see --version)"));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031
32static cl::opt<std::string>
Mikhail Glushenkov26e9aed2009-01-16 06:53:46 +000033MCPU("mcpu",
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
35 cl::value_desc("cpu-name"),
36 cl::init(""));
37
38static cl::list<std::string>
Mikhail Glushenkov26e9aed2009-01-16 06:53:46 +000039MAttrs("mattr",
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040 cl::CommaSeparated,
41 cl::desc("Target specific attributes (-mattr=help for details)"),
42 cl::value_desc("a1,+a2,-a3,..."));
43
Reid Klecknerfa3585b2009-07-18 00:42:18 +000044/// selectTarget - Pick a target either via -march or by guessing the native
45/// arch. Add any CPU features specified via -mcpu or -mattr.
46TargetMachine *JIT::selectTarget(ModuleProvider *MP, std::string *ErrorStr) {
Evan Cheng5f0d0812009-09-02 00:19:03 +000047 Module &Mod = *MP->getModule();
48
49 Triple TheTriple(Mod.getTargetTriple());
50 if (TheTriple.getTriple().empty())
51 TheTriple.setTriple(sys::getHostTriple());
Daniel Dunbar6e570c82009-07-16 02:23:53 +000052
Daniel Dunbar4baa5fe2009-08-03 04:03:51 +000053 // Adjust the triple to match what the user requested.
Evan Cheng5f0d0812009-09-02 00:19:03 +000054 const Target *TheTarget = 0;
55 if (!MArch.empty()) {
56 for (TargetRegistry::iterator it = TargetRegistry::begin(),
57 ie = TargetRegistry::end(); it != ie; ++it) {
58 if (MArch == it->getName()) {
59 TheTarget = &*it;
60 break;
61 }
62 }
Daniel Dunbar4baa5fe2009-08-03 04:03:51 +000063
Evan Cheng5f0d0812009-09-02 00:19:03 +000064 if (!TheTarget) {
Daniel Dunbar4b349f22009-09-08 23:32:35 +000065 *ErrorStr = "No available targets are compatible with this -march, "
66 "see -version for the available targets.\n";
Evan Cheng5f0d0812009-09-02 00:19:03 +000067 return 0;
68 }
69
70 // Adjust the triple to match (if known), otherwise stick with the
71 // module/host triple.
72 Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
73 if (Type != Triple::UnknownArch)
74 TheTriple.setArch(Type);
75 } else {
76 std::string Error;
77 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
78 if (TheTarget == 0) {
79 if (ErrorStr)
80 *ErrorStr = Error;
81 return 0;
82 }
Daniel Dunbar4baa5fe2009-08-03 04:03:51 +000083 }
84
85 if (!TheTarget->hasJIT()) {
86 errs() << "WARNING: This target JIT is not designed for the host you are"
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000087 << " running. If bad things happen, please choose a different "
88 << "-march switch.\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 }
90
91 // Package up features to be passed to target/subtarget
92 std::string FeaturesStr;
Dan Gohman775977b2008-07-07 17:52:43 +000093 if (!MCPU.empty() || !MAttrs.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 SubtargetFeatures Features;
95 Features.setCPU(MCPU);
96 for (unsigned i = 0; i != MAttrs.size(); ++i)
97 Features.AddFeature(MAttrs[i]);
98 FeaturesStr = Features.getString();
99 }
100
101 // Allocate a target...
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000102 TargetMachine *Target =
Daniel Dunbar09a25f32009-08-04 04:08:40 +0000103 TheTarget->createTargetMachine(TheTriple.getTriple(), FeaturesStr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 assert(Target && "Could not allocate target machine!");
Reid Klecknerfa3585b2009-07-18 00:42:18 +0000105 return Target;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106}