Improve JIT error message for users crazy enough to use -march with JIT, and
[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",
30       cl::desc("Architecture to generate assembly for (see --version)"));
31
32 static cl::opt<std::string>
33 MCPU("mcpu",
34   cl::desc("Target a specific cpu type (-mcpu=help for details)"),
35   cl::value_desc("cpu-name"),
36   cl::init(""));
37
38 static cl::list<std::string>
39 MAttrs("mattr",
40   cl::CommaSeparated,
41   cl::desc("Target specific attributes (-mattr=help for details)"),
42   cl::value_desc("a1,+a2,-a3,..."));
43
44 /// selectTarget - Pick a target either via -march or by guessing the native
45 /// arch.  Add any CPU features specified via -mcpu or -mattr.
46 TargetMachine *JIT::selectTarget(ModuleProvider *MP, std::string *ErrorStr) {
47   Module &Mod = *MP->getModule();
48
49   Triple TheTriple(Mod.getTargetTriple());
50   if (TheTriple.getTriple().empty())
51     TheTriple.setTriple(sys::getHostTriple());
52
53   // Adjust the triple to match what the user requested.
54   const Target *TheTarget = 0;
55   if (!MArch.empty()) {
56     for (TargetRegistry::iterator it = TargetRegistry::begin(),
57            ie = TargetRegistry::end(); it != ie; ++it) {
58       if (MArch == it->getName()) {
59         TheTarget = &*it;
60         break;
61       }
62     }
63
64     if (!TheTarget) {
65       *ErrorStr = "No available targets are compatible with this -march, "
66         "see -version for the available targets.\n";
67       return 0;
68     }
69
70     // Adjust the triple to match (if known), otherwise stick with the
71     // module/host triple.
72     Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
73     if (Type != Triple::UnknownArch)
74       TheTriple.setArch(Type);
75   } else {
76     std::string Error;
77     TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
78     if (TheTarget == 0) {
79       if (ErrorStr)
80         *ErrorStr = Error;
81       return 0;
82     }
83   }
84
85   if (!TheTarget->hasJIT()) {
86     errs() << "WARNING: This target JIT is not designed for the host you are"
87            << " running.  If bad things happen, please choose a different "
88            << "-march switch.\n";
89   }
90
91   // Package up features to be passed to target/subtarget
92   std::string FeaturesStr;
93   if (!MCPU.empty() || !MAttrs.empty()) {
94     SubtargetFeatures Features;
95     Features.setCPU(MCPU);
96     for (unsigned i = 0; i != MAttrs.size(); ++i)
97       Features.AddFeature(MAttrs[i]);
98     FeaturesStr = Features.getString();
99   }
100
101   // Allocate a target...
102   TargetMachine *Target = 
103     TheTarget->createTargetMachine(TheTriple.getTriple(), FeaturesStr);
104   assert(Target && "Could not allocate target machine!");
105   return Target;
106 }