Line data Source code
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 target to use, and
11 : // allows the user to specify a specific one on the commandline with -march=x,
12 : // -mcpu=y, and -mattr=a,-b,+c. Clients should initialize targets prior to
13 : // calling selectTarget().
14 : //
15 : //===----------------------------------------------------------------------===//
16 :
17 : #include "llvm/ADT/Triple.h"
18 : #include "llvm/ExecutionEngine/ExecutionEngine.h"
19 : #include "llvm/IR/Module.h"
20 : #include "llvm/MC/SubtargetFeature.h"
21 : #include "llvm/Support/Host.h"
22 : #include "llvm/Support/TargetRegistry.h"
23 : #include "llvm/Target/TargetMachine.h"
24 :
25 : using namespace llvm;
26 :
27 237 : TargetMachine *EngineBuilder::selectTarget() {
28 : Triple TT;
29 :
30 : // MCJIT can generate code for remote targets, but the old JIT and Interpreter
31 : // must use the host architecture.
32 237 : if (WhichEngine != EngineKind::Interpreter && M)
33 203 : TT.setTriple(M->getTargetTriple());
34 :
35 474 : return selectTarget(TT, MArch, MCPU, MAttrs);
36 : }
37 :
38 : /// selectTarget - Pick a target either via -march or by guessing the native
39 : /// arch. Add any CPU features specified via -mcpu or -mattr.
40 241 : TargetMachine *EngineBuilder::selectTarget(const Triple &TargetTriple,
41 : StringRef MArch,
42 : StringRef MCPU,
43 : const SmallVectorImpl<std::string>& MAttrs) {
44 241 : Triple TheTriple(TargetTriple);
45 241 : if (TheTriple.getTriple().empty())
46 402 : TheTriple.setTriple(sys::getProcessTriple());
47 :
48 : // Adjust the triple to match what the user requested.
49 : const Target *TheTarget = nullptr;
50 241 : if (!MArch.empty()) {
51 0 : auto I = find_if(TargetRegistry::targets(),
52 : [&](const Target &T) { return MArch == T.getName(); });
53 :
54 0 : if (I == TargetRegistry::targets().end()) {
55 0 : if (ErrorStr)
56 : *ErrorStr = "No available targets are compatible with this -march, "
57 : "see -version for the available targets.\n";
58 : return nullptr;
59 : }
60 :
61 : TheTarget = &*I;
62 :
63 : // Adjust the triple to match (if known), otherwise stick with the
64 : // requested/host triple.
65 0 : Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
66 0 : if (Type != Triple::UnknownArch)
67 0 : TheTriple.setArch(Type);
68 : } else {
69 : std::string Error;
70 241 : TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
71 241 : if (!TheTarget) {
72 5 : if (ErrorStr)
73 : *ErrorStr = Error;
74 : return nullptr;
75 : }
76 : }
77 :
78 : // Package up features to be passed to target/subtarget
79 : std::string FeaturesStr;
80 236 : if (!MAttrs.empty()) {
81 0 : SubtargetFeatures Features;
82 0 : for (unsigned i = 0; i != MAttrs.size(); ++i)
83 0 : Features.AddFeature(MAttrs[i]);
84 0 : FeaturesStr = Features.getString();
85 : }
86 :
87 : // FIXME: non-iOS ARM FastISel is broken with MCJIT.
88 236 : if (TheTriple.getArch() == Triple::arm &&
89 236 : !TheTriple.isiOS() &&
90 0 : OptLevel == CodeGenOpt::None) {
91 0 : OptLevel = CodeGenOpt::Less;
92 : }
93 :
94 : // Allocate a target...
95 : TargetMachine *Target =
96 236 : TheTarget->createTargetMachine(TheTriple.getTriple(), MCPU, FeaturesStr,
97 236 : Options, RelocModel, CMModel, OptLevel,
98 : /*JIT*/ true);
99 236 : Target->Options.EmulatedTLS = EmulatedTLS;
100 236 : Target->Options.ExplicitEmulatedTLS = true;
101 :
102 : assert(Target && "Could not allocate target machine!");
103 : return Target;
104 : }
|