blob: 93ad81f85f30bd749491d44763e153362b5fe9de [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//
10// This just asks the TargetMachineRegistry for the appropriate JIT to use, and
11// allows the user to specify a specific one on the commandline with -march=x.
12//
13//===----------------------------------------------------------------------===//
14
15#include "JIT.h"
16#include "llvm/Module.h"
17#include "llvm/ModuleProvider.h"
Mikhail Glushenkov61fc6c82009-01-16 07:02:28 +000018#include "llvm/Support/RegistryParser.h"
Dan Gohmand408d392008-05-23 20:40:06 +000019#include "llvm/Support/Streams.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/Target/SubtargetFeature.h"
21#include "llvm/Target/TargetMachine.h"
22#include "llvm/Target/TargetMachineRegistry.h"
23using namespace llvm;
24
Gordon Henriksen99e34ab2007-10-17 21:28:48 +000025static cl::opt<const TargetMachineRegistry::entry*, false,
Mikhail Glushenkov61fc6c82009-01-16 07:02:28 +000026 RegistryParser<TargetMachine> >
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027MArch("march", cl::desc("Architecture to generate assembly for:"));
28
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 Dunbarfe5939f2009-07-15 20:24:03 +000048 const Target *TheTarget;
49 if (MArch == 0) {
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 {
58 TheTarget = &MArch->TheTarget;
59 if (TheTarget->getJITMatchQuality() == 0) {
60 cerr << "WARNING: This target JIT is not designed for the host you are"
61 << " running. If bad things happen, please choose a different "
62 << "-march switch.\n";
63 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 }
65
66 // Package up features to be passed to target/subtarget
67 std::string FeaturesStr;
Dan Gohman775977b2008-07-07 17:52:43 +000068 if (!MCPU.empty() || !MAttrs.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 SubtargetFeatures Features;
70 Features.setCPU(MCPU);
71 for (unsigned i = 0; i != MAttrs.size(); ++i)
72 Features.AddFeature(MAttrs[i]);
73 FeaturesStr = Features.getString();
74 }
75
76 // Allocate a target...
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000077 TargetMachine *Target =
78 TheTarget->createTargetMachine(*MP->getModule(), FeaturesStr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 assert(Target && "Could not allocate target machine!");
80
81 // If the target supports JIT code generation, return a new JIT now.
82 if (TargetJITInfo *TJ = Target->getJITInfo())
Jeffrey Yasskin892956a2009-07-08 21:59:57 +000083 return new JIT(MP, *Target, *TJ, JMM, OptLevel, AllocateGVsWithCode);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084
85 if (ErrorStr)
86 *ErrorStr = "target does not support JIT code generation";
87 return 0;
88}