blob: 0d9c6cfa090876777eca11253f78fce0c8e24815 [file] [log] [blame]
Dylan Noblesmith00dd3d62011-05-06 22:20:09 +00001//===-- TargetSelect.cpp - Target Chooser Code ----------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Dylan Noblesmith00dd3d62011-05-06 22:20:09 +00006//
7//===----------------------------------------------------------------------===//
8//
Dylan Noblesmith7f262462011-12-12 04:20:36 +00009// This just asks the TargetRegistry for the appropriate target to use, and
10// allows the user to specify a specific one on the commandline with -march=x,
11// -mcpu=y, and -mattr=a,-b,+c. Clients should initialize targets prior to
12// calling selectTarget().
Dylan Noblesmith00dd3d62011-05-06 22:20:09 +000013//
14//===----------------------------------------------------------------------===//
15
Dylan Noblesmith00dd3d62011-05-06 22:20:09 +000016#include "llvm/ADT/Triple.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000017#include "llvm/ExecutionEngine/ExecutionEngine.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Module.h"
Evan Cheng8264e272011-06-29 01:14:12 +000019#include "llvm/MC/SubtargetFeature.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000020#include "llvm/Support/Host.h"
21#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/Target/TargetMachine.h"
Dylan Noblesmith7f262462011-12-12 04:20:36 +000023
Dylan Noblesmith00dd3d62011-05-06 22:20:09 +000024using namespace llvm;
25
Owen Andersonadd6f1d2012-03-23 17:40:56 +000026TargetMachine *EngineBuilder::selectTarget() {
Andrew Kaylorfeb805f2012-10-02 18:38:34 +000027 Triple TT;
28
29 // MCJIT can generate code for remote targets, but the old JIT and Interpreter
30 // must use the host architecture.
Eric Christopher79cc1e32014-09-02 22:28:02 +000031 if (WhichEngine != EngineKind::Interpreter && M)
Andrew Kaylorfeb805f2012-10-02 18:38:34 +000032 TT.setTriple(M->getTargetTriple());
Peter Collingbournea51c6ed2013-01-16 17:27:22 +000033
Owen Andersonadd6f1d2012-03-23 17:40:56 +000034 return selectTarget(TT, MArch, MCPU, MAttrs);
35}
36
Dylan Noblesmith00dd3d62011-05-06 22:20:09 +000037/// selectTarget - Pick a target either via -march or by guessing the native
38/// arch. Add any CPU features specified via -mcpu or -mattr.
Dylan Noblesmith7f262462011-12-12 04:20:36 +000039TargetMachine *EngineBuilder::selectTarget(const Triple &TargetTriple,
Dylan Noblesmith1756faa2011-05-13 21:36:16 +000040 StringRef MArch,
41 StringRef MCPU,
Owen Andersonadd6f1d2012-03-23 17:40:56 +000042 const SmallVectorImpl<std::string>& MAttrs) {
Dylan Noblesmith7f262462011-12-12 04:20:36 +000043 Triple TheTriple(TargetTriple);
Dylan Noblesmith00dd3d62011-05-06 22:20:09 +000044 if (TheTriple.getTriple().empty())
Peter Collingbournea51c6ed2013-01-16 17:27:22 +000045 TheTriple.setTriple(sys::getProcessTriple());
Dylan Noblesmith00dd3d62011-05-06 22:20:09 +000046
47 // Adjust the triple to match what the user requested.
Craig Topper2617dcc2014-04-15 06:32:26 +000048 const Target *TheTarget = nullptr;
Dylan Noblesmith00dd3d62011-05-06 22:20:09 +000049 if (!MArch.empty()) {
David Majnemer42531262016-08-12 03:55:06 +000050 auto I = find_if(TargetRegistry::targets(),
51 [&](const Target &T) { return MArch == T.getName(); });
Dylan Noblesmith00dd3d62011-05-06 22:20:09 +000052
David Blaikie46c561c2015-05-11 22:20:48 +000053 if (I == TargetRegistry::targets().end()) {
Jim Grosbach1ace8b62012-05-10 00:31:50 +000054 if (ErrorStr)
55 *ErrorStr = "No available targets are compatible with this -march, "
56 "see -version for the available targets.\n";
Craig Topper2617dcc2014-04-15 06:32:26 +000057 return nullptr;
Dylan Noblesmith00dd3d62011-05-06 22:20:09 +000058 }
59
David Blaikie46c561c2015-05-11 22:20:48 +000060 TheTarget = &*I;
61
Dylan Noblesmith00dd3d62011-05-06 22:20:09 +000062 // Adjust the triple to match (if known), otherwise stick with the
Dylan Noblesmith7f262462011-12-12 04:20:36 +000063 // requested/host triple.
Dylan Noblesmith00dd3d62011-05-06 22:20:09 +000064 Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
65 if (Type != Triple::UnknownArch)
66 TheTriple.setArch(Type);
67 } else {
68 std::string Error;
69 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
Craig Topper2617dcc2014-04-15 06:32:26 +000070 if (!TheTarget) {
Dylan Noblesmith00dd3d62011-05-06 22:20:09 +000071 if (ErrorStr)
72 *ErrorStr = Error;
Craig Topper2617dcc2014-04-15 06:32:26 +000073 return nullptr;
Dylan Noblesmith00dd3d62011-05-06 22:20:09 +000074 }
75 }
76
Dylan Noblesmith00dd3d62011-05-06 22:20:09 +000077 // Package up features to be passed to target/subtarget
78 std::string FeaturesStr;
Evan Chengfe6e4052011-06-30 01:53:36 +000079 if (!MAttrs.empty()) {
Dylan Noblesmith00dd3d62011-05-06 22:20:09 +000080 SubtargetFeatures Features;
Dylan Noblesmith00dd3d62011-05-06 22:20:09 +000081 for (unsigned i = 0; i != MAttrs.size(); ++i)
82 Features.AddFeature(MAttrs[i]);
83 FeaturesStr = Features.getString();
84 }
85
JF Bastien18db1f22013-06-14 02:49:43 +000086 // FIXME: non-iOS ARM FastISel is broken with MCJIT.
Eric Christopher79cc1e32014-09-02 22:28:02 +000087 if (TheTriple.getArch() == Triple::arm &&
Cameron Esfahani943908b2013-08-29 20:23:14 +000088 !TheTriple.isiOS() &&
JF Bastien18db1f22013-06-14 02:49:43 +000089 OptLevel == CodeGenOpt::None) {
90 OptLevel = CodeGenOpt::Less;
91 }
92
Dylan Noblesmith00dd3d62011-05-06 22:20:09 +000093 // Allocate a target...
Rafael Espindola79e238a2017-08-03 02:16:21 +000094 TargetMachine *Target =
95 TheTarget->createTargetMachine(TheTriple.getTriple(), MCPU, FeaturesStr,
96 Options, RelocModel, CMModel, OptLevel,
Lang Hames8eec91e2017-10-20 00:53:16 +000097 /*JIT*/ true);
98 Target->Options.EmulatedTLS = EmulatedTLS;
Chih-Hung Hsieh9f9e4682018-02-28 17:48:55 +000099 Target->Options.ExplicitEmulatedTLS = true;
100
Dylan Noblesmith00dd3d62011-05-06 22:20:09 +0000101 assert(Target && "Could not allocate target machine!");
102 return Target;
103}