blob: 8d92ab01c3db8de97765cb28c98be77e4ebc2961 [file] [log] [blame]
Chris Lattner4d326fa2003-12-20 01:46:27 +00001//===-- TargetSelect.cpp - Target Chooser Code ----------------------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Chris Lattner4d326fa2003-12-20 01:46:27 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
Chris Lattner4d326fa2003-12-20 01:46:27 +00008//===----------------------------------------------------------------------===//
9//
Daniel Dunbar1d929212009-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.
Chris Lattner4d326fa2003-12-20 01:46:27 +000013//
14//===----------------------------------------------------------------------===//
15
16#include "JIT.h"
17#include "llvm/Module.h"
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +000018#include "llvm/ADT/Triple.h"
Daniel Dunbar1da8be22009-07-16 04:01:35 +000019#include "llvm/Support/CommandLine.h"
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +000020#include "llvm/Support/raw_ostream.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000021#include "llvm/Support/Host.h"
Jim Laskeyb1e11802005-09-01 21:38:21 +000022#include "llvm/Target/SubtargetFeature.h"
Chris Lattner4d326fa2003-12-20 01:46:27 +000023#include "llvm/Target/TargetMachine.h"
Daniel Dunbar1d929212009-07-16 02:23:53 +000024#include "llvm/Target/TargetRegistry.h"
Chris Lattner4d326fa2003-12-20 01:46:27 +000025using namespace llvm;
26
Reid Kleckner4b1511b2009-07-18 00:42:18 +000027/// selectTarget - Pick a target either via -march or by guessing the native
28/// arch. Add any CPU features specified via -mcpu or -mattr.
Jeffrey Yasskin46882612010-02-05 16:19:36 +000029TargetMachine *JIT::selectTarget(Module *Mod,
30 StringRef MArch,
31 StringRef MCPU,
32 const SmallVectorImpl<std::string>& MAttrs,
33 std::string *ErrorStr) {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000034 Triple TheTriple(Mod->getTargetTriple());
Evan Cheng94f07d82009-09-02 00:19:03 +000035 if (TheTriple.getTriple().empty())
36 TheTriple.setTriple(sys::getHostTriple());
Daniel Dunbar1d929212009-07-16 02:23:53 +000037
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +000038 // Adjust the triple to match what the user requested.
Evan Cheng94f07d82009-09-02 00:19:03 +000039 const Target *TheTarget = 0;
40 if (!MArch.empty()) {
41 for (TargetRegistry::iterator it = TargetRegistry::begin(),
42 ie = TargetRegistry::end(); it != ie; ++it) {
43 if (MArch == it->getName()) {
44 TheTarget = &*it;
45 break;
46 }
47 }
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +000048
Evan Cheng94f07d82009-09-02 00:19:03 +000049 if (!TheTarget) {
Daniel Dunbar02476562009-09-08 23:32:35 +000050 *ErrorStr = "No available targets are compatible with this -march, "
51 "see -version for the available targets.\n";
Evan Cheng94f07d82009-09-02 00:19:03 +000052 return 0;
53 }
54
55 // Adjust the triple to match (if known), otherwise stick with the
56 // module/host triple.
57 Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
58 if (Type != Triple::UnknownArch)
59 TheTriple.setArch(Type);
60 } else {
61 std::string Error;
62 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
63 if (TheTarget == 0) {
64 if (ErrorStr)
65 *ErrorStr = Error;
66 return 0;
67 }
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +000068 }
69
70 if (!TheTarget->hasJIT()) {
71 errs() << "WARNING: This target JIT is not designed for the host you are"
Daniel Dunbar51b198a2009-07-15 20:24:03 +000072 << " running. If bad things happen, please choose a different "
73 << "-march switch.\n";
Chris Lattner4d326fa2003-12-20 01:46:27 +000074 }
Chris Lattner4d326fa2003-12-20 01:46:27 +000075
Jim Laskeyb1e11802005-09-01 21:38:21 +000076 // Package up features to be passed to target/subtarget
77 std::string FeaturesStr;
Dan Gohmaneccfb6a2008-07-07 17:52:43 +000078 if (!MCPU.empty() || !MAttrs.empty()) {
Jim Laskeyb1e11802005-09-01 21:38:21 +000079 SubtargetFeatures Features;
80 Features.setCPU(MCPU);
81 for (unsigned i = 0; i != MAttrs.size(); ++i)
82 Features.AddFeature(MAttrs[i]);
83 FeaturesStr = Features.getString();
84 }
85
Chris Lattner4d326fa2003-12-20 01:46:27 +000086 // Allocate a target...
Jim Grosbach8bf0ecd2011-03-22 15:21:58 +000087 TargetMachine *Target =
Daniel Dunbar4b3d5722009-08-04 04:08:40 +000088 TheTarget->createTargetMachine(TheTriple.getTriple(), FeaturesStr);
Chris Lattner4d326fa2003-12-20 01:46:27 +000089 assert(Target && "Could not allocate target machine!");
Reid Kleckner4b1511b2009-07-18 00:42:18 +000090 return Target;
Chris Lattner4d326fa2003-12-20 01:46:27 +000091}