blob: 55ff44121ddc4d8acb9ff52d634fe2c20d8b4fc9 [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//
Daniel Dunbar1d929212009-07-16 02:23:53 +000010// This just asks the TargetRegistry for the appropriate JIT to use, and allows
11// the user to specify a specific one on the commandline with -march=x. Clients
12// should initialize targets prior to calling createJIT.
Chris Lattner4d326fa2003-12-20 01:46:27 +000013//
14//===----------------------------------------------------------------------===//
15
16#include "JIT.h"
17#include "llvm/Module.h"
18#include "llvm/ModuleProvider.h"
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +000019#include "llvm/ADT/Triple.h"
Daniel Dunbar1da8be22009-07-16 04:01:35 +000020#include "llvm/Support/CommandLine.h"
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +000021#include "llvm/Support/raw_ostream.h"
22#include "llvm/System/Host.h"
Jim Laskeyb1e11802005-09-01 21:38:21 +000023#include "llvm/Target/SubtargetFeature.h"
Chris Lattner4d326fa2003-12-20 01:46:27 +000024#include "llvm/Target/TargetMachine.h"
Daniel Dunbar1d929212009-07-16 02:23:53 +000025#include "llvm/Target/TargetRegistry.h"
Chris Lattner4d326fa2003-12-20 01:46:27 +000026using namespace llvm;
27
Daniel Dunbar1d929212009-07-16 02:23:53 +000028static cl::opt<std::string>
29MArch("march", cl::desc("Architecture to generate assembly for (see --version)"));
Chris Lattner4d326fa2003-12-20 01:46:27 +000030
Jim Laskeyb1e11802005-09-01 21:38:21 +000031static cl::opt<std::string>
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000032MCPU("mcpu",
Chris Lattner667eeca2005-10-23 22:39:01 +000033 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
Jim Laskeyb1e11802005-09-01 21:38:21 +000034 cl::value_desc("cpu-name"),
35 cl::init(""));
36
37static cl::list<std::string>
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000038MAttrs("mattr",
Jim Laskeyb1e11802005-09-01 21:38:21 +000039 cl::CommaSeparated,
Chris Lattner667eeca2005-10-23 22:39:01 +000040 cl::desc("Target specific attributes (-mattr=help for details)"),
41 cl::value_desc("a1,+a2,-a3,..."));
Jim Laskeyb1e11802005-09-01 21:38:21 +000042
Reid Kleckner4b1511b2009-07-18 00:42:18 +000043/// selectTarget - Pick a target either via -march or by guessing the native
44/// arch. Add any CPU features specified via -mcpu or -mattr.
45TargetMachine *JIT::selectTarget(ModuleProvider *MP, std::string *ErrorStr) {
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +000046 Triple TheTriple(sys::getHostTriple());
Daniel Dunbar1d929212009-07-16 02:23:53 +000047
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +000048 // Adjust the triple to match what the user requested.
49 if (!MArch.empty())
50 TheTriple.setArch(Triple::getArchTypeForLLVMName(MArch));
51
52 std::string Error;
53 const Target *TheTarget =
54 TargetRegistry::lookupTarget(TheTriple.getTriple(),
55 /*FallbackToHost=*/false,
56 /*RequireJIT=*/false,
57 Error);
58 if (TheTarget == 0) {
59 if (ErrorStr)
60 *ErrorStr = Error;
61 return 0;
62 }
63
64 if (!TheTarget->hasJIT()) {
65 errs() << "WARNING: This target JIT is not designed for the host you are"
Daniel Dunbar51b198a2009-07-15 20:24:03 +000066 << " running. If bad things happen, please choose a different "
67 << "-march switch.\n";
Chris Lattner4d326fa2003-12-20 01:46:27 +000068 }
Chris Lattner4d326fa2003-12-20 01:46:27 +000069
Jim Laskeyb1e11802005-09-01 21:38:21 +000070 // Package up features to be passed to target/subtarget
71 std::string FeaturesStr;
Dan Gohmaneccfb6a2008-07-07 17:52:43 +000072 if (!MCPU.empty() || !MAttrs.empty()) {
Jim Laskeyb1e11802005-09-01 21:38:21 +000073 SubtargetFeatures Features;
74 Features.setCPU(MCPU);
75 for (unsigned i = 0; i != MAttrs.size(); ++i)
76 Features.AddFeature(MAttrs[i]);
77 FeaturesStr = Features.getString();
78 }
79
Chris Lattner4d326fa2003-12-20 01:46:27 +000080 // Allocate a target...
Daniel Dunbar51b198a2009-07-15 20:24:03 +000081 TargetMachine *Target =
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +000082 TheTarget->createTargetMachine(*MP->getModule(), TheTriple.getTriple(),
83 FeaturesStr);
Chris Lattner4d326fa2003-12-20 01:46:27 +000084 assert(Target && "Could not allocate target machine!");
Reid Kleckner4b1511b2009-07-18 00:42:18 +000085 return Target;
Chris Lattner4d326fa2003-12-20 01:46:27 +000086}