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 | // |
| 10 | // 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. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "JIT.h" |
| 16 | #include "llvm/Module.h" |
| 17 | #include "llvm/ModuleProvider.h" |
Mikhail Glushenkov | 61fc6c8 | 2009-01-16 07:02:28 +0000 | [diff] [blame] | 18 | #include "llvm/Support/RegistryParser.h" |
Dan Gohman | d408d39 | 2008-05-23 20:40:06 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Streams.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 20 | #include "llvm/Target/SubtargetFeature.h" |
| 21 | #include "llvm/Target/TargetMachine.h" |
| 22 | #include "llvm/Target/TargetMachineRegistry.h" |
| 23 | using namespace llvm; |
| 24 | |
Gordon Henriksen | 99e34ab | 2007-10-17 21:28:48 +0000 | [diff] [blame] | 25 | static cl::opt<const TargetMachineRegistry::entry*, false, |
Mikhail Glushenkov | 61fc6c8 | 2009-01-16 07:02:28 +0000 | [diff] [blame] | 26 | RegistryParser<TargetMachine> > |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 27 | MArch("march", cl::desc("Architecture to generate assembly for:")); |
| 28 | |
| 29 | static cl::opt<std::string> |
Mikhail Glushenkov | 26e9aed | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 30 | MCPU("mcpu", |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 31 | cl::desc("Target a specific cpu type (-mcpu=help for details)"), |
| 32 | cl::value_desc("cpu-name"), |
| 33 | cl::init("")); |
| 34 | |
| 35 | static cl::list<std::string> |
Mikhail Glushenkov | 26e9aed | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 36 | MAttrs("mattr", |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 37 | cl::CommaSeparated, |
| 38 | cl::desc("Target specific attributes (-mattr=help for details)"), |
| 39 | cl::value_desc("a1,+a2,-a3,...")); |
| 40 | |
Chris Lattner | 4db98aa | 2007-12-06 01:34:04 +0000 | [diff] [blame] | 41 | /// createInternal - Create an return a new JIT compiler if there is one |
| 42 | /// available for the current target. Otherwise, return null. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 43 | /// |
Chris Lattner | 4db98aa | 2007-12-06 01:34:04 +0000 | [diff] [blame] | 44 | ExecutionEngine *JIT::createJIT(ModuleProvider *MP, std::string *ErrorStr, |
Bill Wendling | 277d727 | 2009-04-29 00:32:19 +0000 | [diff] [blame] | 45 | JITMemoryManager *JMM, unsigned OptLevel) { |
Gordon Henriksen | 99e34ab | 2007-10-17 21:28:48 +0000 | [diff] [blame] | 46 | const TargetMachineRegistry::entry *TheArch = MArch; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 47 | if (TheArch == 0) { |
| 48 | std::string Error; |
| 49 | TheArch = TargetMachineRegistry::getClosestTargetForJIT(Error); |
| 50 | if (TheArch == 0) { |
| 51 | if (ErrorStr) |
| 52 | *ErrorStr = Error; |
| 53 | return 0; |
| 54 | } |
| 55 | } else if (TheArch->JITMatchQualityFn() == 0) { |
| 56 | cerr << "WARNING: This target JIT is not designed for the host you are" |
| 57 | << " running. If bad things happen, please choose a different " |
| 58 | << "-march switch.\n"; |
| 59 | } |
| 60 | |
| 61 | // Package up features to be passed to target/subtarget |
| 62 | std::string FeaturesStr; |
Dan Gohman | 775977b | 2008-07-07 17:52:43 +0000 | [diff] [blame] | 63 | if (!MCPU.empty() || !MAttrs.empty()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 64 | SubtargetFeatures Features; |
| 65 | Features.setCPU(MCPU); |
| 66 | for (unsigned i = 0; i != MAttrs.size(); ++i) |
| 67 | Features.AddFeature(MAttrs[i]); |
| 68 | FeaturesStr = Features.getString(); |
| 69 | } |
| 70 | |
| 71 | // Allocate a target... |
| 72 | TargetMachine *Target = TheArch->CtorFn(*MP->getModule(), FeaturesStr); |
| 73 | assert(Target && "Could not allocate target machine!"); |
| 74 | |
| 75 | // If the target supports JIT code generation, return a new JIT now. |
| 76 | if (TargetJITInfo *TJ = Target->getJITInfo()) |
Bill Wendling | 277d727 | 2009-04-29 00:32:19 +0000 | [diff] [blame] | 77 | return new JIT(MP, *Target, *TJ, JMM, OptLevel); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 78 | |
| 79 | if (ErrorStr) |
| 80 | *ErrorStr = "target does not support JIT code generation"; |
| 81 | return 0; |
| 82 | } |