blob: 84b745b3f9a399495ba17e46e4cf1ad03bbbda64 [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 Dunbarcd862072009-07-16 04:01:35 +000019#include "llvm/Support/CommandLine.h"
Dan Gohmand408d392008-05-23 20:40:06 +000020#include "llvm/Support/Streams.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/Target/SubtargetFeature.h"
22#include "llvm/Target/TargetMachine.h"
Daniel Dunbar6e570c82009-07-16 02:23:53 +000023#include "llvm/Target/TargetRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024using namespace llvm;
25
Daniel Dunbar6e570c82009-07-16 02:23:53 +000026static cl::opt<std::string>
27MArch("march", cl::desc("Architecture to generate assembly for (see --version)"));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028
29static cl::opt<std::string>
Mikhail Glushenkov26e9aed2009-01-16 06:53:46 +000030MCPU("mcpu",
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
32 cl::value_desc("cpu-name"),
33 cl::init(""));
34
35static cl::list<std::string>
Mikhail Glushenkov26e9aed2009-01-16 06:53:46 +000036MAttrs("mattr",
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037 cl::CommaSeparated,
38 cl::desc("Target specific attributes (-mattr=help for details)"),
39 cl::value_desc("a1,+a2,-a3,..."));
40
Reid Klecknerfa3585b2009-07-18 00:42:18 +000041/// selectTarget - Pick a target either via -march or by guessing the native
42/// arch. Add any CPU features specified via -mcpu or -mattr.
43TargetMachine *JIT::selectTarget(ModuleProvider *MP, std::string *ErrorStr) {
Daniel Dunbar6e570c82009-07-16 02:23:53 +000044 const Target *TheTarget = 0;
45 if (MArch.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 std::string Error;
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000047 TheTarget = TargetRegistry::getClosestTargetForJIT(Error);
48 if (TheTarget == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 if (ErrorStr)
50 *ErrorStr = Error;
51 return 0;
52 }
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000053 } else {
Daniel Dunbar6e570c82009-07-16 02:23:53 +000054 for (TargetRegistry::iterator it = TargetRegistry::begin(),
55 ie = TargetRegistry::end(); it != ie; ++it) {
56 if (MArch == it->getName()) {
57 TheTarget = &*it;
58 break;
59 }
60 }
61
62 if (TheTarget == 0) {
63 if (ErrorStr)
64 *ErrorStr = std::string("invalid target '" + MArch + "'.\n");
65 return 0;
66 }
67
Daniel Dunbar123157a2009-07-25 10:09:50 +000068 if (!TheTarget->hasJIT()) {
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000069 cerr << "WARNING: This target JIT is not designed for the host you are"
70 << " running. If bad things happen, please choose a different "
71 << "-march switch.\n";
72 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 }
74
75 // Package up features to be passed to target/subtarget
76 std::string FeaturesStr;
Dan Gohman775977b2008-07-07 17:52:43 +000077 if (!MCPU.empty() || !MAttrs.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 SubtargetFeatures Features;
79 Features.setCPU(MCPU);
80 for (unsigned i = 0; i != MAttrs.size(); ++i)
81 Features.AddFeature(MAttrs[i]);
82 FeaturesStr = Features.getString();
83 }
84
85 // Allocate a target...
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000086 TargetMachine *Target =
87 TheTarget->createTargetMachine(*MP->getModule(), FeaturesStr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 assert(Target && "Could not allocate target machine!");
Reid Klecknerfa3585b2009-07-18 00:42:18 +000089 return Target;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090}