Fix PR4845: r77946 completely broke x86_64 Darwin (or any situation where the
[oota-llvm.git] / lib / ExecutionEngine / JIT / TargetSelect.cpp
1 //===-- TargetSelect.cpp - Target Chooser Code ----------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "JIT.h"
17 #include "llvm/Module.h"
18 #include "llvm/ModuleProvider.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/System/Host.h"
23 #include "llvm/Target/SubtargetFeature.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Target/TargetRegistry.h"
26 using namespace llvm;
27
28 static cl::opt<std::string>
29 MArch("march", cl::desc("Architecture to generate assembly for (see --version)"));
30
31 static cl::opt<std::string>
32 MCPU("mcpu",
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>
38 MAttrs("mattr",
39   cl::CommaSeparated,
40   cl::desc("Target specific attributes (-mattr=help for details)"),
41   cl::value_desc("a1,+a2,-a3,..."));
42
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) {
46   Module &Mod = *MP->getModule();
47
48   Triple TheTriple(Mod.getTargetTriple());
49   if (TheTriple.getTriple().empty())
50     TheTriple.setTriple(sys::getHostTriple());
51
52   // Adjust the triple to match what the user requested.
53   const Target *TheTarget = 0;
54   if (!MArch.empty()) {
55     for (TargetRegistry::iterator it = TargetRegistry::begin(),
56            ie = TargetRegistry::end(); it != ie; ++it) {
57       if (MArch == it->getName()) {
58         TheTarget = &*it;
59         break;
60       }
61     }
62
63     if (!TheTarget) {
64       errs() << "JIT: error: invalid target '" << MArch << "'.\n";
65       return 0;
66     }
67
68     // Adjust the triple to match (if known), otherwise stick with the
69     // module/host triple.
70     Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
71     if (Type != Triple::UnknownArch)
72       TheTriple.setArch(Type);
73   } else {
74     std::string Error;
75     TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
76     if (TheTarget == 0) {
77       if (ErrorStr)
78         *ErrorStr = Error;
79       return 0;
80     }
81   }
82
83   if (!TheTarget->hasJIT()) {
84     errs() << "WARNING: This target JIT is not designed for the host you are"
85            << " running.  If bad things happen, please choose a different "
86            << "-march switch.\n";
87   }
88
89   // Package up features to be passed to target/subtarget
90   std::string FeaturesStr;
91   if (!MCPU.empty() || !MAttrs.empty()) {
92     SubtargetFeatures Features;
93     Features.setCPU(MCPU);
94     for (unsigned i = 0; i != MAttrs.size(); ++i)
95       Features.AddFeature(MAttrs[i]);
96     FeaturesStr = Features.getString();
97   }
98
99   // Allocate a target...
100   TargetMachine *Target = 
101     TheTarget->createTargetMachine(TheTriple.getTriple(), FeaturesStr);
102   assert(Target && "Could not allocate target machine!");
103   return Target;
104 }