Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- TargetSelect.cpp - Target Chooser Code ----------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Daniel Dunbar | 6e570c8 | 2009-07-16 02:23:53 +0000 | [diff] [blame] | 10 | // 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. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "JIT.h" |
| 17 | #include "llvm/Module.h" |
| 18 | #include "llvm/ModuleProvider.h" |
Daniel Dunbar | 4baa5fe | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Triple.h" |
Daniel Dunbar | cd86207 | 2009-07-16 04:01:35 +0000 | [diff] [blame] | 20 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | 4baa5fe | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | #include "llvm/System/Host.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 23 | #include "llvm/Target/SubtargetFeature.h" |
| 24 | #include "llvm/Target/TargetMachine.h" |
Daniel Dunbar | 6e570c8 | 2009-07-16 02:23:53 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetRegistry.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Daniel Dunbar | 6e570c8 | 2009-07-16 02:23:53 +0000 | [diff] [blame] | 28 | static cl::opt<std::string> |
| 29 | MArch("march", cl::desc("Architecture to generate assembly for (see --version)")); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 30 | |
| 31 | static cl::opt<std::string> |
Mikhail Glushenkov | 26e9aed | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 32 | MCPU("mcpu", |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 33 | cl::desc("Target a specific cpu type (-mcpu=help for details)"), |
| 34 | cl::value_desc("cpu-name"), |
| 35 | cl::init("")); |
| 36 | |
| 37 | static cl::list<std::string> |
Mikhail Glushenkov | 26e9aed | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 38 | MAttrs("mattr", |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 39 | cl::CommaSeparated, |
| 40 | cl::desc("Target specific attributes (-mattr=help for details)"), |
| 41 | cl::value_desc("a1,+a2,-a3,...")); |
| 42 | |
Reid Kleckner | fa3585b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 43 | /// selectTarget - Pick a target either via -march or by guessing the native |
| 44 | /// arch. Add any CPU features specified via -mcpu or -mattr. |
| 45 | TargetMachine *JIT::selectTarget(ModuleProvider *MP, std::string *ErrorStr) { |
Daniel Dunbar | 4baa5fe | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 46 | Triple TheTriple(sys::getHostTriple()); |
Daniel Dunbar | 6e570c8 | 2009-07-16 02:23:53 +0000 | [diff] [blame] | 47 | |
Daniel Dunbar | 4baa5fe | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 48 | // 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 = |
Daniel Dunbar | 9aa0827 | 2009-08-03 04:20:57 +0000 | [diff] [blame] | 54 | TargetRegistry::lookupTarget(TheTriple.getTriple(), Error); |
Daniel Dunbar | 4baa5fe | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 55 | if (TheTarget == 0) { |
| 56 | if (ErrorStr) |
| 57 | *ErrorStr = Error; |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | if (!TheTarget->hasJIT()) { |
| 62 | errs() << "WARNING: This target JIT is not designed for the host you are" |
Daniel Dunbar | fe5939f | 2009-07-15 20:24:03 +0000 | [diff] [blame] | 63 | << " running. If bad things happen, please choose a different " |
| 64 | << "-march switch.\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // Package up features to be passed to target/subtarget |
| 68 | std::string FeaturesStr; |
Dan Gohman | 775977b | 2008-07-07 17:52:43 +0000 | [diff] [blame] | 69 | if (!MCPU.empty() || !MAttrs.empty()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 70 | SubtargetFeatures Features; |
| 71 | Features.setCPU(MCPU); |
| 72 | for (unsigned i = 0; i != MAttrs.size(); ++i) |
| 73 | Features.AddFeature(MAttrs[i]); |
| 74 | FeaturesStr = Features.getString(); |
| 75 | } |
| 76 | |
| 77 | // Allocate a target... |
Daniel Dunbar | fe5939f | 2009-07-15 20:24:03 +0000 | [diff] [blame] | 78 | TargetMachine *Target = |
Daniel Dunbar | 4baa5fe | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 79 | TheTarget->createTargetMachine(*MP->getModule(), TheTriple.getTriple(), |
| 80 | FeaturesStr); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 81 | assert(Target && "Could not allocate target machine!"); |
Reid Kleckner | fa3585b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 82 | return Target; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 83 | } |