1 //===--- X86.cpp - X86 Helpers for Tools ------------------------*- C++ -*-===//
3 // 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
7 //===----------------------------------------------------------------------===//
10 #include "ToolChains/CommonArgs.h"
11 #include "clang/Driver/Driver.h"
12 #include "clang/Driver/DriverDiagnostic.h"
13 #include "clang/Driver/Options.h"
14 #include "llvm/ADT/StringExtras.h"
15 #include "llvm/ADT/StringMap.h"
16 #include "llvm/Option/ArgList.h"
17 #include "llvm/Support/Host.h"
19 using namespace clang::driver
;
20 using namespace clang::driver::tools
;
21 using namespace clang
;
22 using namespace llvm::opt
;
24 std::string
x86::getX86TargetCPU(const Driver
&D
, const ArgList
&Args
,
25 const llvm::Triple
&Triple
) {
26 if (const Arg
*A
= Args
.getLastArg(clang::driver::options::OPT_march_EQ
)) {
27 StringRef CPU
= A
->getValue();
29 return std::string(CPU
);
31 // FIXME: Reject attempts to use -march=native unless the target matches
34 // FIXME: We should also incorporate the detected target features for use
36 CPU
= llvm::sys::getHostCPUName();
37 if (!CPU
.empty() && CPU
!= "generic")
38 return std::string(CPU
);
41 if (const Arg
*A
= Args
.getLastArg(options::OPT__SLASH_arch
)) {
42 // Mapping built by looking at lib/Basic's X86TargetInfo::initFeatureMap().
43 // The keys are case-sensitive; this matches link.exe.
44 // 32-bit and 64-bit /arch: flags.
45 llvm::StringMap
<StringRef
> ArchMap({
46 {"AVX", "sandybridge"},
49 {"AVX512", "skylake-avx512"},
51 if (Triple
.getArch() == llvm::Triple::x86
) {
52 // 32-bit-only /arch: flags.
59 StringRef CPU
= ArchMap
.lookup(A
->getValue());
61 std::vector
<StringRef
> ValidArchs
{ArchMap
.keys().begin(),
62 ArchMap
.keys().end()};
64 D
.Diag(diag::warn_drv_invalid_arch_name_with_suggestion
)
65 << A
->getValue() << (Triple
.getArch() == llvm::Triple::x86
)
66 << join(ValidArchs
, ", ");
68 return std::string(CPU
);
71 // Select the default CPU if none was given (or detection failed).
74 return ""; // This routine is only handling x86 targets.
76 bool Is64Bit
= Triple
.getArch() == llvm::Triple::x86_64
;
78 // FIXME: Need target hooks.
79 if (Triple
.isOSDarwin()) {
80 if (Triple
.getArchName() == "x86_64h")
82 // macosx10.12 drops support for all pre-Penryn Macs.
83 // Simulators can still run on 10.11 though, like Xcode.
84 if (Triple
.isMacOSX() && !Triple
.isOSVersionLT(10, 12))
87 if (Triple
.isDriverKit())
90 // The oldest x86_64 Macs have core2/Merom; the oldest x86 Macs have Yonah.
91 return Is64Bit
? "core2" : "yonah";
94 // Set up default CPU name for PS4/PS5 compilers.
100 // On Android use targets compatible with gcc
101 if (Triple
.isAndroid())
102 return Is64Bit
? "x86-64" : "i686";
104 // Everything else goes to x86-64 in 64-bit mode.
108 switch (Triple
.getOS()) {
109 case llvm::Triple::NetBSD
:
111 case llvm::Triple::Haiku
:
112 case llvm::Triple::OpenBSD
:
114 case llvm::Triple::FreeBSD
:
122 void x86::getX86TargetFeatures(const Driver
&D
, const llvm::Triple
&Triple
,
124 std::vector
<StringRef
> &Features
) {
125 // If -march=native, autodetect the feature list.
126 if (const Arg
*A
= Args
.getLastArg(clang::driver::options::OPT_march_EQ
)) {
127 if (StringRef(A
->getValue()) == "native") {
128 llvm::StringMap
<bool> HostFeatures
;
129 if (llvm::sys::getHostCPUFeatures(HostFeatures
))
130 for (auto &F
: HostFeatures
)
132 Args
.MakeArgString((F
.second
? "+" : "-") + F
.first()));
136 if (Triple
.getArchName() == "x86_64h") {
137 // x86_64h implies quite a few of the more modern subtarget features
138 // for Haswell class CPUs, but not all of them. Opt-out of a few.
139 Features
.push_back("-rdrnd");
140 Features
.push_back("-aes");
141 Features
.push_back("-pclmul");
142 Features
.push_back("-rtm");
143 Features
.push_back("-fsgsbase");
146 const llvm::Triple::ArchType ArchType
= Triple
.getArch();
147 // Add features to be compatible with gcc for Android.
148 if (Triple
.isAndroid()) {
149 if (ArchType
== llvm::Triple::x86_64
) {
150 Features
.push_back("+sse4.2");
151 Features
.push_back("+popcnt");
152 Features
.push_back("+cx16");
154 Features
.push_back("+ssse3");
157 // Translate the high level `-mretpoline` flag to the specific target feature
158 // flags. We also detect if the user asked for retpoline external thunks but
159 // failed to ask for retpolines themselves (through any of the different
160 // flags). This is a bit hacky but keeps existing usages working. We should
161 // consider deprecating this and instead warn if the user requests external
162 // retpoline thunks and *doesn't* request some form of retpolines.
163 auto SpectreOpt
= clang::driver::options::ID::OPT_INVALID
;
164 if (Args
.hasArgNoClaim(options::OPT_mretpoline
, options::OPT_mno_retpoline
,
165 options::OPT_mspeculative_load_hardening
,
166 options::OPT_mno_speculative_load_hardening
)) {
167 if (Args
.hasFlag(options::OPT_mretpoline
, options::OPT_mno_retpoline
,
169 Features
.push_back("+retpoline-indirect-calls");
170 Features
.push_back("+retpoline-indirect-branches");
171 SpectreOpt
= options::OPT_mretpoline
;
172 } else if (Args
.hasFlag(options::OPT_mspeculative_load_hardening
,
173 options::OPT_mno_speculative_load_hardening
,
175 // On x86, speculative load hardening relies on at least using retpolines
176 // for indirect calls.
177 Features
.push_back("+retpoline-indirect-calls");
178 SpectreOpt
= options::OPT_mspeculative_load_hardening
;
180 } else if (Args
.hasFlag(options::OPT_mretpoline_external_thunk
,
181 options::OPT_mno_retpoline_external_thunk
, false)) {
182 // FIXME: Add a warning about failing to specify `-mretpoline` and
183 // eventually switch to an error here.
184 Features
.push_back("+retpoline-indirect-calls");
185 Features
.push_back("+retpoline-indirect-branches");
186 SpectreOpt
= options::OPT_mretpoline_external_thunk
;
189 auto LVIOpt
= clang::driver::options::ID::OPT_INVALID
;
190 if (Args
.hasFlag(options::OPT_mlvi_hardening
, options::OPT_mno_lvi_hardening
,
192 Features
.push_back("+lvi-load-hardening");
193 Features
.push_back("+lvi-cfi"); // load hardening implies CFI protection
194 LVIOpt
= options::OPT_mlvi_hardening
;
195 } else if (Args
.hasFlag(options::OPT_mlvi_cfi
, options::OPT_mno_lvi_cfi
,
197 Features
.push_back("+lvi-cfi");
198 LVIOpt
= options::OPT_mlvi_cfi
;
201 if (Args
.hasFlag(options::OPT_m_seses
, options::OPT_mno_seses
, false)) {
202 if (LVIOpt
== options::OPT_mlvi_hardening
)
203 D
.Diag(diag::err_drv_argument_not_allowed_with
)
204 << D
.getOpts().getOptionName(options::OPT_mlvi_hardening
)
205 << D
.getOpts().getOptionName(options::OPT_m_seses
);
207 if (SpectreOpt
!= clang::driver::options::ID::OPT_INVALID
)
208 D
.Diag(diag::err_drv_argument_not_allowed_with
)
209 << D
.getOpts().getOptionName(SpectreOpt
)
210 << D
.getOpts().getOptionName(options::OPT_m_seses
);
212 Features
.push_back("+seses");
213 if (!Args
.hasArg(options::OPT_mno_lvi_cfi
)) {
214 Features
.push_back("+lvi-cfi");
215 LVIOpt
= options::OPT_mlvi_cfi
;
219 if (SpectreOpt
!= clang::driver::options::ID::OPT_INVALID
&&
220 LVIOpt
!= clang::driver::options::ID::OPT_INVALID
) {
221 D
.Diag(diag::err_drv_argument_not_allowed_with
)
222 << D
.getOpts().getOptionName(SpectreOpt
)
223 << D
.getOpts().getOptionName(LVIOpt
);
226 // Now add any that the user explicitly requested on the command line,
227 // which may override the defaults.
228 for (const Arg
*A
: Args
.filtered(options::OPT_m_x86_Features_Group
,
229 options::OPT_mgeneral_regs_only
)) {
230 StringRef Name
= A
->getOption().getName();
234 assert(Name
.startswith("m") && "Invalid feature name.");
235 Name
= Name
.substr(1);
237 // Replace -mgeneral-regs-only with -x87, -mmx, -sse
238 if (A
->getOption().getID() == options::OPT_mgeneral_regs_only
) {
239 Features
.insert(Features
.end(), {"-x87", "-mmx", "-sse"});
243 bool IsNegative
= Name
.startswith("no-");
245 Name
= Name
.substr(3);
246 Features
.push_back(Args
.MakeArgString((IsNegative
? "-" : "+") + Name
));
249 // Enable/disable straight line speculation hardening.
250 if (Arg
*A
= Args
.getLastArg(options::OPT_mharden_sls_EQ
)) {
251 StringRef Scope
= A
->getValue();
252 if (Scope
== "all") {
253 Features
.push_back("+harden-sls-ijmp");
254 Features
.push_back("+harden-sls-ret");
255 } else if (Scope
== "return") {
256 Features
.push_back("+harden-sls-ret");
257 } else if (Scope
== "indirect-jmp") {
258 Features
.push_back("+harden-sls-ijmp");
259 } else if (Scope
!= "none") {
260 D
.Diag(diag::err_drv_unsupported_option_argument
)
261 << A
->getOption().getName() << Scope
;