blob: 0fb0b41999e489155e2d751b0b502702f98ebc9a [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"
Jim Laskeyb1e11802005-09-01 21:38:21 +000018#include "llvm/Target/SubtargetFeature.h"
Chris Lattner4d326fa2003-12-20 01:46:27 +000019#include "llvm/Target/TargetMachine.h"
Chris Lattnerd5e1d9d2004-07-11 04:02:06 +000020#include "llvm/Target/TargetMachineRegistry.h"
Chris Lattner4d326fa2003-12-20 01:46:27 +000021using namespace llvm;
22
Gordon Henriksen4b2b9402007-10-17 21:28:48 +000023static cl::opt<const TargetMachineRegistry::entry*, false,
24 TargetMachineRegistry::Parser>
Chris Lattnerd5e1d9d2004-07-11 04:02:06 +000025MArch("march", cl::desc("Architecture to generate assembly for:"));
Chris Lattner4d326fa2003-12-20 01:46:27 +000026
Jim Laskeyb1e11802005-09-01 21:38:21 +000027static cl::opt<std::string>
28MCPU("mcpu",
Chris Lattner667eeca2005-10-23 22:39:01 +000029 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Jim Laskeyb1e11802005-09-01 21:38:21 +000030 cl::value_desc("cpu-name"),
31 cl::init(""));
32
33static cl::list<std::string>
34MAttrs("mattr",
35 cl::CommaSeparated,
Chris Lattner667eeca2005-10-23 22:39:01 +000036 cl::desc("Target specific attributes (-mattr=help for details)"),
37 cl::value_desc("a1,+a2,-a3,..."));
Jim Laskeyb1e11802005-09-01 21:38:21 +000038
Chris Lattner34c94332007-12-06 01:34:04 +000039/// createInternal - Create an return a new JIT compiler if there is one
40/// available for the current target. Otherwise, return null.
Chris Lattner4d326fa2003-12-20 01:46:27 +000041///
Chris Lattner34c94332007-12-06 01:34:04 +000042ExecutionEngine *JIT::createJIT(ModuleProvider *MP, std::string *ErrorStr,
43 JITMemoryManager *JMM) {
Gordon Henriksen4b2b9402007-10-17 21:28:48 +000044 const TargetMachineRegistry::entry *TheArch = MArch;
Chris Lattner108ec4b2007-04-20 22:57:20 +000045 if (TheArch == 0) {
Chris Lattnerd5e1d9d2004-07-11 04:02:06 +000046 std::string Error;
Chris Lattner108ec4b2007-04-20 22:57:20 +000047 TheArch = TargetMachineRegistry::getClosestTargetForJIT(Error);
48 if (TheArch == 0) {
Reid Spencerd4c0e622007-03-03 18:19:18 +000049 if (ErrorStr)
50 *ErrorStr = Error;
51 return 0;
52 }
Chris Lattner108ec4b2007-04-20 22:57:20 +000053 } else if (TheArch->JITMatchQualityFn() == 0) {
Bill Wendling832171c2006-12-07 20:04:42 +000054 cerr << "WARNING: This target JIT is not designed for the host you are"
55 << " running. If bad things happen, please choose a different "
56 << "-march switch.\n";
Chris Lattner4d326fa2003-12-20 01:46:27 +000057 }
Chris Lattner4d326fa2003-12-20 01:46:27 +000058
Jim Laskeyb1e11802005-09-01 21:38:21 +000059 // Package up features to be passed to target/subtarget
60 std::string FeaturesStr;
61 if (MCPU.size() || MAttrs.size()) {
62 SubtargetFeatures Features;
63 Features.setCPU(MCPU);
64 for (unsigned i = 0; i != MAttrs.size(); ++i)
65 Features.AddFeature(MAttrs[i]);
66 FeaturesStr = Features.getString();
67 }
68
Chris Lattner4d326fa2003-12-20 01:46:27 +000069 // Allocate a target...
Chris Lattner108ec4b2007-04-20 22:57:20 +000070 TargetMachine *Target = TheArch->CtorFn(*MP->getModule(), FeaturesStr);
Chris Lattner4d326fa2003-12-20 01:46:27 +000071 assert(Target && "Could not allocate target machine!");
72
73 // If the target supports JIT code generation, return a new JIT now.
74 if (TargetJITInfo *TJ = Target->getJITInfo())
Chris Lattner34c94332007-12-06 01:34:04 +000075 return new JIT(MP, *Target, *TJ, JMM);
Reid Spencerd4c0e622007-03-03 18:19:18 +000076
77 if (ErrorStr)
78 *ErrorStr = "target does not support JIT code generation";
Chris Lattner4d326fa2003-12-20 01:46:27 +000079 return 0;
80}