blob: 98819c19b83e6f843fc9ee14135eefe7265ccd55 [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,
Evan Cheng502f20b2008-08-08 08:11:34 +000045 JITMemoryManager *JMM, bool Fast) {
Gordon Henriksen4b2b9402007-10-17 21:28:48 +000046 const TargetMachineRegistry::entry *TheArch = MArch;
Chris Lattner108ec4b2007-04-20 22:57:20 +000047 if (TheArch == 0) {
Chris Lattnerd5e1d9d2004-07-11 04:02:06 +000048 std::string Error;
Chris Lattner108ec4b2007-04-20 22:57:20 +000049 TheArch = TargetMachineRegistry::getClosestTargetForJIT(Error);
50 if (TheArch == 0) {
Reid Spencerd4c0e622007-03-03 18:19:18 +000051 if (ErrorStr)
52 *ErrorStr = Error;
53 return 0;
54 }
Chris Lattner108ec4b2007-04-20 22:57:20 +000055 } else if (TheArch->JITMatchQualityFn() == 0) {
Bill Wendling832171c2006-12-07 20:04:42 +000056 cerr << "WARNING: This target JIT is not designed for the host you are"
57 << " running. If bad things happen, please choose a different "
58 << "-march switch.\n";
Chris Lattner4d326fa2003-12-20 01:46:27 +000059 }
Chris Lattner4d326fa2003-12-20 01:46:27 +000060
Jim Laskeyb1e11802005-09-01 21:38:21 +000061 // Package up features to be passed to target/subtarget
62 std::string FeaturesStr;
Dan Gohmaneccfb6a2008-07-07 17:52:43 +000063 if (!MCPU.empty() || !MAttrs.empty()) {
Jim Laskeyb1e11802005-09-01 21:38:21 +000064 SubtargetFeatures Features;
65 Features.setCPU(MCPU);
66 for (unsigned i = 0; i != MAttrs.size(); ++i)
67 Features.AddFeature(MAttrs[i]);
68 FeaturesStr = Features.getString();
69 }
70
Chris Lattner4d326fa2003-12-20 01:46:27 +000071 // Allocate a target...
Chris Lattner108ec4b2007-04-20 22:57:20 +000072 TargetMachine *Target = TheArch->CtorFn(*MP->getModule(), FeaturesStr);
Chris Lattner4d326fa2003-12-20 01:46:27 +000073 assert(Target && "Could not allocate target machine!");
74
75 // If the target supports JIT code generation, return a new JIT now.
76 if (TargetJITInfo *TJ = Target->getJITInfo())
Evan Cheng502f20b2008-08-08 08:11:34 +000077 return new JIT(MP, *Target, *TJ, JMM, Fast);
Reid Spencerd4c0e622007-03-03 18:19:18 +000078
79 if (ErrorStr)
80 *ErrorStr = "target does not support JIT code generation";
Chris Lattner4d326fa2003-12-20 01:46:27 +000081 return 0;
82}