blob: 24dd013639846309bf38755afcf13d407ca3bc0a [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,
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000046 CodeGenOpt::Level OptLevel,
47 bool AllocateGVsWithCode) {
Stuart Hastings2286f8d2009-07-15 17:27:11 +000048 const TargetMachineRegistry::entry *TheArch = MArch;
49 if (TheArch == 0) {
Chris Lattnerd5e1d9d2004-07-11 04:02:06 +000050 std::string Error;
Stuart Hastings2286f8d2009-07-15 17:27:11 +000051 TheArch = TargetMachineRegistry::getClosestTargetForJIT(Error);
52 if (TheArch == 0) {
Reid Spencerd4c0e622007-03-03 18:19:18 +000053 if (ErrorStr)
54 *ErrorStr = Error;
55 return 0;
56 }
Stuart Hastings2286f8d2009-07-15 17:27:11 +000057 } else if (TheArch->JITMatchQualityFn() == 0) {
Bill Wendling832171c2006-12-07 20:04:42 +000058 cerr << "WARNING: This target JIT is not designed for the host you are"
59 << " running. If bad things happen, please choose a different "
60 << "-march switch.\n";
Chris Lattner4d326fa2003-12-20 01:46:27 +000061 }
Chris Lattner4d326fa2003-12-20 01:46:27 +000062
Jim Laskeyb1e11802005-09-01 21:38:21 +000063 // Package up features to be passed to target/subtarget
64 std::string FeaturesStr;
Dan Gohmaneccfb6a2008-07-07 17:52:43 +000065 if (!MCPU.empty() || !MAttrs.empty()) {
Jim Laskeyb1e11802005-09-01 21:38:21 +000066 SubtargetFeatures Features;
67 Features.setCPU(MCPU);
68 for (unsigned i = 0; i != MAttrs.size(); ++i)
69 Features.AddFeature(MAttrs[i]);
70 FeaturesStr = Features.getString();
71 }
72
Chris Lattner4d326fa2003-12-20 01:46:27 +000073 // Allocate a target...
Stuart Hastings2286f8d2009-07-15 17:27:11 +000074 TargetMachine *Target = TheArch->CtorFn(*MP->getModule(), FeaturesStr);
Chris Lattner4d326fa2003-12-20 01:46:27 +000075 assert(Target && "Could not allocate target machine!");
76
77 // If the target supports JIT code generation, return a new JIT now.
78 if (TargetJITInfo *TJ = Target->getJITInfo())
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000079 return new JIT(MP, *Target, *TJ, JMM, OptLevel, AllocateGVsWithCode);
Reid Spencerd4c0e622007-03-03 18:19:18 +000080
81 if (ErrorStr)
82 *ErrorStr = "target does not support JIT code generation";
Chris Lattner4d326fa2003-12-20 01:46:27 +000083 return 0;
84}