blob: 450de1e52a1a2fea07f3f7d75fc652c2a9b4c54e [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"
Mikhail Glushenkov61fc6c82009-01-16 07:02:28 +000019#include "llvm/Support/RegistryParser.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
Chris Lattner4db98aa2007-12-06 01:34:04 +000041/// createInternal - Create an return a new JIT compiler if there is one
42/// available for the current target. Otherwise, return null.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043///
Chris Lattner4db98aa2007-12-06 01:34:04 +000044ExecutionEngine *JIT::createJIT(ModuleProvider *MP, std::string *ErrorStr,
Bill Wendling5ed22ac2009-04-29 23:29:43 +000045 JITMemoryManager *JMM,
Jeffrey Yasskin892956a2009-07-08 21:59:57 +000046 CodeGenOpt::Level OptLevel,
47 bool AllocateGVsWithCode) {
Daniel Dunbar6e570c82009-07-16 02:23:53 +000048 const Target *TheTarget = 0;
49 if (MArch.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 std::string Error;
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000051 TheTarget = TargetRegistry::getClosestTargetForJIT(Error);
52 if (TheTarget == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 if (ErrorStr)
54 *ErrorStr = Error;
55 return 0;
56 }
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000057 } else {
Daniel Dunbar6e570c82009-07-16 02:23:53 +000058 for (TargetRegistry::iterator it = TargetRegistry::begin(),
59 ie = TargetRegistry::end(); it != ie; ++it) {
60 if (MArch == it->getName()) {
61 TheTarget = &*it;
62 break;
63 }
64 }
65
66 if (TheTarget == 0) {
67 if (ErrorStr)
68 *ErrorStr = std::string("invalid target '" + MArch + "'.\n");
69 return 0;
70 }
71
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000072 if (TheTarget->getJITMatchQuality() == 0) {
73 cerr << "WARNING: This target JIT is not designed for the host you are"
74 << " running. If bad things happen, please choose a different "
75 << "-march switch.\n";
76 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077 }
78
79 // Package up features to be passed to target/subtarget
80 std::string FeaturesStr;
Dan Gohman775977b2008-07-07 17:52:43 +000081 if (!MCPU.empty() || !MAttrs.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 SubtargetFeatures Features;
83 Features.setCPU(MCPU);
84 for (unsigned i = 0; i != MAttrs.size(); ++i)
85 Features.AddFeature(MAttrs[i]);
86 FeaturesStr = Features.getString();
87 }
88
89 // Allocate a target...
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000090 TargetMachine *Target =
91 TheTarget->createTargetMachine(*MP->getModule(), FeaturesStr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 assert(Target && "Could not allocate target machine!");
93
94 // If the target supports JIT code generation, return a new JIT now.
95 if (TargetJITInfo *TJ = Target->getJITInfo())
Jeffrey Yasskin892956a2009-07-08 21:59:57 +000096 return new JIT(MP, *Target, *TJ, JMM, OptLevel, AllocateGVsWithCode);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097
98 if (ErrorStr)
99 *ErrorStr = "target does not support JIT code generation";
100 return 0;
101}