Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 1 | //===-- TargetSelect.cpp - Target Chooser Code ----------------------------===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Daniel Dunbar | 1d92921 | 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. |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "JIT.h" |
| 17 | #include "llvm/Module.h" |
| 18 | #include "llvm/ModuleProvider.h" |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame^] | 19 | #include "llvm/ADT/Triple.h" |
Daniel Dunbar | 1da8be2 | 2009-07-16 04:01:35 +0000 | [diff] [blame] | 20 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame^] | 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | #include "llvm/System/Host.h" |
Jim Laskey | b1e1180 | 2005-09-01 21:38:21 +0000 | [diff] [blame] | 23 | #include "llvm/Target/SubtargetFeature.h" |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetMachine.h" |
Daniel Dunbar | 1d92921 | 2009-07-16 02:23:53 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetRegistry.h" |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Daniel Dunbar | 1d92921 | 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)")); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 30 | |
Jim Laskey | b1e1180 | 2005-09-01 21:38:21 +0000 | [diff] [blame] | 31 | static cl::opt<std::string> |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 32 | MCPU("mcpu", |
Chris Lattner | 667eeca | 2005-10-23 22:39:01 +0000 | [diff] [blame] | 33 | cl::desc("Target a specific cpu type (-mcpu=help for details)"), |
Jim Laskey | b1e1180 | 2005-09-01 21:38:21 +0000 | [diff] [blame] | 34 | cl::value_desc("cpu-name"), |
| 35 | cl::init("")); |
| 36 | |
| 37 | static cl::list<std::string> |
Mikhail Glushenkov | 5c1799b | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 38 | MAttrs("mattr", |
Jim Laskey | b1e1180 | 2005-09-01 21:38:21 +0000 | [diff] [blame] | 39 | cl::CommaSeparated, |
Chris Lattner | 667eeca | 2005-10-23 22:39:01 +0000 | [diff] [blame] | 40 | cl::desc("Target specific attributes (-mattr=help for details)"), |
| 41 | cl::value_desc("a1,+a2,-a3,...")); |
Jim Laskey | b1e1180 | 2005-09-01 21:38:21 +0000 | [diff] [blame] | 42 | |
Reid Kleckner | 4b1511b | 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 | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame^] | 46 | Triple TheTriple(sys::getHostTriple()); |
Daniel Dunbar | 1d92921 | 2009-07-16 02:23:53 +0000 | [diff] [blame] | 47 | |
Daniel Dunbar | 3c2d4bf | 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 = |
| 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 Dunbar | 51b198a | 2009-07-15 20:24:03 +0000 | [diff] [blame] | 66 | << " running. If bad things happen, please choose a different " |
| 67 | << "-march switch.\n"; |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 68 | } |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 69 | |
Jim Laskey | b1e1180 | 2005-09-01 21:38:21 +0000 | [diff] [blame] | 70 | // Package up features to be passed to target/subtarget |
| 71 | std::string FeaturesStr; |
Dan Gohman | eccfb6a | 2008-07-07 17:52:43 +0000 | [diff] [blame] | 72 | if (!MCPU.empty() || !MAttrs.empty()) { |
Jim Laskey | b1e1180 | 2005-09-01 21:38:21 +0000 | [diff] [blame] | 73 | 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 Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 80 | // Allocate a target... |
Daniel Dunbar | 51b198a | 2009-07-15 20:24:03 +0000 | [diff] [blame] | 81 | TargetMachine *Target = |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame^] | 82 | TheTarget->createTargetMachine(*MP->getModule(), TheTriple.getTriple(), |
| 83 | FeaturesStr); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 84 | assert(Target && "Could not allocate target machine!"); |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 85 | return Target; |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 86 | } |