blob: c30f88c572ef12702edab0eeaaaf4fc0a8c2b550 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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
Chris Lattnerd5e1d9d2004-07-11 04:02:06 +000023static cl::opt<const TargetMachineRegistry::Entry*, false, TargetNameParser>
24MArch("march", cl::desc("Architecture to generate assembly for:"));
Chris Lattner4d326fa2003-12-20 01:46:27 +000025
Jim Laskeyb1e11802005-09-01 21:38:21 +000026static cl::opt<std::string>
27MCPU("mcpu",
Chris Lattner667eeca2005-10-23 22:39:01 +000028 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Jim Laskeyb1e11802005-09-01 21:38:21 +000029 cl::value_desc("cpu-name"),
30 cl::init(""));
31
32static cl::list<std::string>
33MAttrs("mattr",
34 cl::CommaSeparated,
Chris Lattner667eeca2005-10-23 22:39:01 +000035 cl::desc("Target specific attributes (-mattr=help for details)"),
36 cl::value_desc("a1,+a2,-a3,..."));
Jim Laskeyb1e11802005-09-01 21:38:21 +000037
Chris Lattner4d326fa2003-12-20 01:46:27 +000038/// create - Create an return a new JIT compiler if there is one available
39/// for the current target. Otherwise, return null.
40///
Chris Lattner726c1ef2006-03-23 05:22:51 +000041ExecutionEngine *JIT::create(ModuleProvider *MP) {
Chris Lattnerd5e1d9d2004-07-11 04:02:06 +000042 if (MArch == 0) {
43 std::string Error;
44 MArch = TargetMachineRegistry::getClosestTargetForJIT(Error);
45 if (MArch == 0) return 0;
46 } else if (MArch->JITMatchQualityFn() == 0) {
Bill Wendling832171c2006-12-07 20:04:42 +000047 cerr << "WARNING: This target JIT is not designed for the host you are"
48 << " running. If bad things happen, please choose a different "
49 << "-march switch.\n";
Chris Lattner4d326fa2003-12-20 01:46:27 +000050 }
Chris Lattner4d326fa2003-12-20 01:46:27 +000051
Jim Laskeyb1e11802005-09-01 21:38:21 +000052 // Package up features to be passed to target/subtarget
53 std::string FeaturesStr;
54 if (MCPU.size() || MAttrs.size()) {
55 SubtargetFeatures Features;
56 Features.setCPU(MCPU);
57 for (unsigned i = 0; i != MAttrs.size(); ++i)
58 Features.AddFeature(MAttrs[i]);
59 FeaturesStr = Features.getString();
60 }
61
Chris Lattner4d326fa2003-12-20 01:46:27 +000062 // Allocate a target...
Chris Lattneref986912006-03-23 05:28:02 +000063 TargetMachine *Target = MArch->CtorFn(*MP->getModule(), FeaturesStr);
Chris Lattner4d326fa2003-12-20 01:46:27 +000064 assert(Target && "Could not allocate target machine!");
65
66 // If the target supports JIT code generation, return a new JIT now.
67 if (TargetJITInfo *TJ = Target->getJITInfo())
68 return new JIT(MP, *Target, *TJ);
69 return 0;
70}