1 //===-- TargetSelect.cpp - Target Chooser Code ----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
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"
28 static cl::opt
<std::string
>
29 MArch("march", cl::desc("Architecture to generate assembly for (see --version)"));
31 static cl::opt
<std::string
>
33 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
34 cl::value_desc("cpu-name"),
37 static cl::list
<std::string
>
40 cl::desc("Target specific attributes (-mattr=help for details)"),
41 cl::value_desc("a1,+a2,-a3,..."));
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 Triple
TheTriple(sys::getHostTriple());
48 // Adjust the triple to match what the user requested.
50 TheTriple
.setArch(Triple::getArchTypeForLLVMName(MArch
));
53 const Target
*TheTarget
=
54 TargetRegistry::lookupTarget(TheTriple
.getTriple(), Error
);
61 if (!TheTarget
->hasJIT()) {
62 errs() << "WARNING: This target JIT is not designed for the host you are"
63 << " running. If bad things happen, please choose a different "
64 << "-march switch.\n";
67 // Package up features to be passed to target/subtarget
68 std::string FeaturesStr
;
69 if (!MCPU
.empty() || !MAttrs
.empty()) {
70 SubtargetFeatures Features
;
71 Features
.setCPU(MCPU
);
72 for (unsigned i
= 0; i
!= MAttrs
.size(); ++i
)
73 Features
.AddFeature(MAttrs
[i
]);
74 FeaturesStr
= Features
.getString();
77 // Allocate a target...
78 TargetMachine
*Target
=
79 TheTarget
->createTargetMachine(TheTriple
.getTriple(), FeaturesStr
);
80 assert(Target
&& "Could not allocate target machine!");