blob: ef9c8f549e687668bf2a0509fc4ca3cfe6340be1 [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"
21#include <iostream>
Chris Lattner4d326fa2003-12-20 01:46:27 +000022using namespace llvm;
23
Chris Lattnerd5e1d9d2004-07-11 04:02:06 +000024static cl::opt<const TargetMachineRegistry::Entry*, false, TargetNameParser>
25MArch("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 Lattner4d326fa2003-12-20 01:46:27 +000039/// create - Create an return a new JIT compiler if there is one available
40/// for the current target. Otherwise, return null.
41///
Chris Lattner726c1ef2006-03-23 05:22:51 +000042ExecutionEngine *JIT::create(ModuleProvider *MP) {
Chris Lattnerd5e1d9d2004-07-11 04:02:06 +000043 if (MArch == 0) {
44 std::string Error;
45 MArch = TargetMachineRegistry::getClosestTargetForJIT(Error);
46 if (MArch == 0) return 0;
47 } else if (MArch->JITMatchQualityFn() == 0) {
48 std::cerr << "WARNING: This target JIT is not designed for the host you are"
49 << " running. If bad things happen, please choose a different "
50 << "-march switch.\n";
Chris Lattner4d326fa2003-12-20 01:46:27 +000051 }
Chris Lattner4d326fa2003-12-20 01:46:27 +000052
Jim Laskeyb1e11802005-09-01 21:38:21 +000053 // Package up features to be passed to target/subtarget
54 std::string FeaturesStr;
55 if (MCPU.size() || MAttrs.size()) {
56 SubtargetFeatures Features;
57 Features.setCPU(MCPU);
58 for (unsigned i = 0; i != MAttrs.size(); ++i)
59 Features.AddFeature(MAttrs[i]);
60 FeaturesStr = Features.getString();
61 }
62
Chris Lattner4d326fa2003-12-20 01:46:27 +000063 // Allocate a target...
Chris Lattneref986912006-03-23 05:28:02 +000064 TargetMachine *Target = MArch->CtorFn(*MP->getModule(), FeaturesStr);
Chris Lattner4d326fa2003-12-20 01:46:27 +000065 assert(Target && "Could not allocate target machine!");
66
67 // If the target supports JIT code generation, return a new JIT now.
68 if (TargetJITInfo *TJ = Target->getJITInfo())
69 return new JIT(MP, *Target, *TJ);
70 return 0;
71}