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" |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Triple.h" |
Daniel Dunbar | 1da8be2 | 2009-07-16 04:01:35 +0000 | [diff] [blame] | 19 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | #include "llvm/System/Host.h" |
Jim Laskey | b1e1180 | 2005-09-01 21:38:21 +0000 | [diff] [blame] | 22 | #include "llvm/Target/SubtargetFeature.h" |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetMachine.h" |
Daniel Dunbar | 1d92921 | 2009-07-16 02:23:53 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetRegistry.h" |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 27 | /// selectTarget - Pick a target either via -march or by guessing the native |
| 28 | /// arch. Add any CPU features specified via -mcpu or -mattr. |
Jeffrey Yasskin | 4688261 | 2010-02-05 16:19:36 +0000 | [diff] [blame^] | 29 | TargetMachine *JIT::selectTarget(Module *Mod, |
| 30 | StringRef MArch, |
| 31 | StringRef MCPU, |
| 32 | const SmallVectorImpl<std::string>& MAttrs, |
| 33 | std::string *ErrorStr) { |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 34 | Triple TheTriple(Mod->getTargetTriple()); |
Evan Cheng | 94f07d8 | 2009-09-02 00:19:03 +0000 | [diff] [blame] | 35 | if (TheTriple.getTriple().empty()) |
| 36 | TheTriple.setTriple(sys::getHostTriple()); |
Daniel Dunbar | 1d92921 | 2009-07-16 02:23:53 +0000 | [diff] [blame] | 37 | |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 38 | // Adjust the triple to match what the user requested. |
Evan Cheng | 94f07d8 | 2009-09-02 00:19:03 +0000 | [diff] [blame] | 39 | const Target *TheTarget = 0; |
| 40 | if (!MArch.empty()) { |
| 41 | for (TargetRegistry::iterator it = TargetRegistry::begin(), |
| 42 | ie = TargetRegistry::end(); it != ie; ++it) { |
| 43 | if (MArch == it->getName()) { |
| 44 | TheTarget = &*it; |
| 45 | break; |
| 46 | } |
| 47 | } |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 48 | |
Evan Cheng | 94f07d8 | 2009-09-02 00:19:03 +0000 | [diff] [blame] | 49 | if (!TheTarget) { |
Daniel Dunbar | 0247656 | 2009-09-08 23:32:35 +0000 | [diff] [blame] | 50 | *ErrorStr = "No available targets are compatible with this -march, " |
| 51 | "see -version for the available targets.\n"; |
Evan Cheng | 94f07d8 | 2009-09-02 00:19:03 +0000 | [diff] [blame] | 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | // Adjust the triple to match (if known), otherwise stick with the |
| 56 | // module/host triple. |
| 57 | Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch); |
| 58 | if (Type != Triple::UnknownArch) |
| 59 | TheTriple.setArch(Type); |
| 60 | } else { |
| 61 | std::string Error; |
| 62 | TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error); |
| 63 | if (TheTarget == 0) { |
| 64 | if (ErrorStr) |
| 65 | *ErrorStr = Error; |
| 66 | return 0; |
| 67 | } |
Daniel Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | if (!TheTarget->hasJIT()) { |
| 71 | 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] | 72 | << " running. If bad things happen, please choose a different " |
| 73 | << "-march switch.\n"; |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 74 | } |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 75 | |
Jim Laskey | b1e1180 | 2005-09-01 21:38:21 +0000 | [diff] [blame] | 76 | // Package up features to be passed to target/subtarget |
| 77 | std::string FeaturesStr; |
Dan Gohman | eccfb6a | 2008-07-07 17:52:43 +0000 | [diff] [blame] | 78 | if (!MCPU.empty() || !MAttrs.empty()) { |
Jim Laskey | b1e1180 | 2005-09-01 21:38:21 +0000 | [diff] [blame] | 79 | SubtargetFeatures Features; |
| 80 | Features.setCPU(MCPU); |
| 81 | for (unsigned i = 0; i != MAttrs.size(); ++i) |
| 82 | Features.AddFeature(MAttrs[i]); |
| 83 | FeaturesStr = Features.getString(); |
| 84 | } |
| 85 | |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 86 | // Allocate a target... |
Daniel Dunbar | 51b198a | 2009-07-15 20:24:03 +0000 | [diff] [blame] | 87 | TargetMachine *Target = |
Daniel Dunbar | 4b3d572 | 2009-08-04 04:08:40 +0000 | [diff] [blame] | 88 | TheTarget->createTargetMachine(TheTriple.getTriple(), FeaturesStr); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 89 | assert(Target && "Could not allocate target machine!"); |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 90 | return Target; |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 91 | } |