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