Kill ModuleProvider and ghost linkage by inverting the relationship between
[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/ADT/Triple.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/System/Host.h"
22 #include "llvm/Target/SubtargetFeature.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetRegistry.h"
25 using namespace llvm;
26
27 static cl::opt<std::string>
28 MArch("march",
29       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(Module *Mod, std::string *ErrorStr) {
46   Triple TheTriple(Mod->getTargetTriple());
47   if (TheTriple.getTriple().empty())
48     TheTriple.setTriple(sys::getHostTriple());
49
50   // Adjust the triple to match what the user requested.
51   const Target *TheTarget = 0;
52   if (!MArch.empty()) {
53     for (TargetRegistry::iterator it = TargetRegistry::begin(),
54            ie = TargetRegistry::end(); it != ie; ++it) {
55       if (MArch == it->getName()) {
56         TheTarget = &*it;
57         break;
58       }
59     }
60
61     if (!TheTarget) {
62       *ErrorStr = "No available targets are compatible with this -march, "
63         "see -version for the available targets.\n";
64       return 0;
65     }
66
67     // Adjust the triple to match (if known), otherwise stick with the
68     // module/host triple.
69     Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
70     if (Type != Triple::UnknownArch)
71       TheTriple.setArch(Type);
72   } else {
73     std::string Error;
74     TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
75     if (TheTarget == 0) {
76       if (ErrorStr)
77         *ErrorStr = Error;
78       return 0;
79     }
80   }
81
82   if (!TheTarget->hasJIT()) {
83     errs() << "WARNING: This target JIT is not designed for the host you are"
84            << " running.  If bad things happen, please choose a different "
85            << "-march switch.\n";
86   }
87
88   // Package up features to be passed to target/subtarget
89   std::string FeaturesStr;
90   if (!MCPU.empty() || !MAttrs.empty()) {
91     SubtargetFeatures Features;
92     Features.setCPU(MCPU);
93     for (unsigned i = 0; i != MAttrs.size(); ++i)
94       Features.AddFeature(MAttrs[i]);
95     FeaturesStr = Features.getString();
96   }
97
98   // Allocate a target...
99   TargetMachine *Target = 
100     TheTarget->createTargetMachine(TheTriple.getTriple(), FeaturesStr);
101   assert(Target && "Could not allocate target machine!");
102   return Target;
103 }