blob: 0f208193075b8f9b4bd8a5e2526189c5505b809d [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//
Chris Lattnerbeff74f2004-07-11 08:24:02 +000010// 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.
Chris Lattner4d326fa2003-12-20 01:46:27 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "JIT.h"
16#include "llvm/Module.h"
17#include "llvm/ModuleProvider.h"
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000018#include "llvm/Support/RegistryParser.h"
Dan Gohmanee335e32008-05-23 20:40:06 +000019#include "llvm/Support/Streams.h"
Jim Laskeyb1e11802005-09-01 21:38:21 +000020#include "llvm/Target/SubtargetFeature.h"
Chris Lattner4d326fa2003-12-20 01:46:27 +000021#include "llvm/Target/TargetMachine.h"
Chris Lattnerd5e1d9d2004-07-11 04:02:06 +000022#include "llvm/Target/TargetMachineRegistry.h"
Chris Lattner4d326fa2003-12-20 01:46:27 +000023using namespace llvm;
24
Gordon Henriksen4b2b9402007-10-17 21:28:48 +000025static cl::opt<const TargetMachineRegistry::entry*, false,
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000026 RegistryParser<TargetMachine> >
Chris Lattnerd5e1d9d2004-07-11 04:02:06 +000027MArch("march", cl::desc("Architecture to generate assembly for:"));
Chris Lattner4d326fa2003-12-20 01:46:27 +000028
Jim Laskeyb1e11802005-09-01 21:38:21 +000029static cl::opt<std::string>
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000030MCPU("mcpu",
Chris Lattner667eeca2005-10-23 22:39:01 +000031 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Jim Laskeyb1e11802005-09-01 21:38:21 +000032 cl::value_desc("cpu-name"),
33 cl::init(""));
34
35static cl::list<std::string>
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000036MAttrs("mattr",
Jim Laskeyb1e11802005-09-01 21:38:21 +000037 cl::CommaSeparated,
Chris Lattner667eeca2005-10-23 22:39:01 +000038 cl::desc("Target specific attributes (-mattr=help for details)"),
39 cl::value_desc("a1,+a2,-a3,..."));
Jim Laskeyb1e11802005-09-01 21:38:21 +000040
Chris Lattner34c94332007-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.
Chris Lattner4d326fa2003-12-20 01:46:27 +000043///
Chris Lattner34c94332007-12-06 01:34:04 +000044ExecutionEngine *JIT::createJIT(ModuleProvider *MP, std::string *ErrorStr,
Bill Wendling98a366d2009-04-29 23:29:43 +000045 JITMemoryManager *JMM,
46 CodeGenOpt::Level OptLevel) {
Gordon Henriksen4b2b9402007-10-17 21:28:48 +000047 const TargetMachineRegistry::entry *TheArch = MArch;
Chris Lattner108ec4b2007-04-20 22:57:20 +000048 if (TheArch == 0) {
Chris Lattnerd5e1d9d2004-07-11 04:02:06 +000049 std::string Error;
Chris Lattner108ec4b2007-04-20 22:57:20 +000050 TheArch = TargetMachineRegistry::getClosestTargetForJIT(Error);
51 if (TheArch == 0) {
Reid Spencerd4c0e622007-03-03 18:19:18 +000052 if (ErrorStr)
53 *ErrorStr = Error;
54 return 0;
55 }
Chris Lattner108ec4b2007-04-20 22:57:20 +000056 } else if (TheArch->JITMatchQualityFn() == 0) {
Bill Wendling832171c2006-12-07 20:04:42 +000057 cerr << "WARNING: This target JIT is not designed for the host you are"
58 << " running. If bad things happen, please choose a different "
59 << "-march switch.\n";
Chris Lattner4d326fa2003-12-20 01:46:27 +000060 }
Chris Lattner4d326fa2003-12-20 01:46:27 +000061
Jim Laskeyb1e11802005-09-01 21:38:21 +000062 // Package up features to be passed to target/subtarget
63 std::string FeaturesStr;
Dan Gohmaneccfb6a2008-07-07 17:52:43 +000064 if (!MCPU.empty() || !MAttrs.empty()) {
Jim Laskeyb1e11802005-09-01 21:38:21 +000065 SubtargetFeatures Features;
66 Features.setCPU(MCPU);
67 for (unsigned i = 0; i != MAttrs.size(); ++i)
68 Features.AddFeature(MAttrs[i]);
69 FeaturesStr = Features.getString();
70 }
71
Chris Lattner4d326fa2003-12-20 01:46:27 +000072 // Allocate a target...
Chris Lattner108ec4b2007-04-20 22:57:20 +000073 TargetMachine *Target = TheArch->CtorFn(*MP->getModule(), FeaturesStr);
Chris Lattner4d326fa2003-12-20 01:46:27 +000074 assert(Target && "Could not allocate target machine!");
75
76 // If the target supports JIT code generation, return a new JIT now.
77 if (TargetJITInfo *TJ = Target->getJITInfo())
Bill Wendling5e5cb792009-04-29 00:32:19 +000078 return new JIT(MP, *Target, *TJ, JMM, OptLevel);
Reid Spencerd4c0e622007-03-03 18:19:18 +000079
80 if (ErrorStr)
81 *ErrorStr = "target does not support JIT code generation";
Chris Lattner4d326fa2003-12-20 01:46:27 +000082 return 0;
83}