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 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().
15 //===----------------------------------------------------------------------===//
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"
27 TargetMachine
*EngineBuilder::selectTarget() {
30 // MCJIT can generate code for remote targets, but the old JIT and Interpreter
31 // must use the host architecture.
32 if (WhichEngine
!= EngineKind::Interpreter
&& M
)
33 TT
.setTriple(M
->getTargetTriple());
35 return selectTarget(TT
, MArch
, MCPU
, MAttrs
);
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 TargetMachine
*EngineBuilder::selectTarget(const Triple
&TargetTriple
,
43 const SmallVectorImpl
<std::string
>& MAttrs
) {
44 Triple
TheTriple(TargetTriple
);
45 if (TheTriple
.getTriple().empty())
46 TheTriple
.setTriple(sys::getProcessTriple());
48 // Adjust the triple to match what the user requested.
49 const Target
*TheTarget
= nullptr;
51 auto I
= find_if(TargetRegistry::targets(),
52 [&](const Target
&T
) { return MArch
== T
.getName(); });
54 if (I
== TargetRegistry::targets().end()) {
56 *ErrorStr
= "No available targets are compatible with this -march, "
57 "see -version for the available targets.\n";
63 // Adjust the triple to match (if known), otherwise stick with the
64 // requested/host triple.
65 Triple::ArchType Type
= Triple::getArchTypeForLLVMName(MArch
);
66 if (Type
!= Triple::UnknownArch
)
67 TheTriple
.setArch(Type
);
70 TheTarget
= TargetRegistry::lookupTarget(TheTriple
.getTriple(), Error
);
78 // Package up features to be passed to target/subtarget
79 std::string FeaturesStr
;
80 if (!MAttrs
.empty()) {
81 SubtargetFeatures Features
;
82 for (unsigned i
= 0; i
!= MAttrs
.size(); ++i
)
83 Features
.AddFeature(MAttrs
[i
]);
84 FeaturesStr
= Features
.getString();
87 // FIXME: non-iOS ARM FastISel is broken with MCJIT.
88 if (TheTriple
.getArch() == Triple::arm
&&
90 OptLevel
== CodeGenOpt::None
) {
91 OptLevel
= CodeGenOpt::Less
;
94 // Allocate a target...
95 TargetMachine
*Target
=
96 TheTarget
->createTargetMachine(TheTriple
.getTriple(), MCPU
, FeaturesStr
,
97 Options
, RelocModel
, CMModel
, OptLevel
,
99 Target
->Options
.EmulatedTLS
= EmulatedTLS
;
100 Target
->Options
.ExplicitEmulatedTLS
= true;
102 assert(Target
&& "Could not allocate target machine!");