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/TargetParser/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
33 CPU
= llvm::sys::getHostCPUName();
34 if (!CPU
.empty() && CPU
!= "generic")
35 return std::string(CPU
);
38 if (const Arg
*A
= Args
.getLastArg(options::OPT__SLASH_arch
)) {
39 // Mapping built by looking at lib/Basic's X86TargetInfo::initFeatureMap().
40 // The keys are case-sensitive; this matches link.exe.
41 // 32-bit and 64-bit /arch: flags.
42 llvm::StringMap
<StringRef
> ArchMap({
43 {"AVX", "sandybridge"},
46 {"AVX512", "skylake-avx512"},
48 if (Triple
.getArch() == llvm::Triple::x86
) {
49 // 32-bit-only /arch: flags.
56 StringRef CPU
= ArchMap
.lookup(A
->getValue());
58 std::vector
<StringRef
> ValidArchs
{ArchMap
.keys().begin(),
59 ArchMap
.keys().end()};
61 D
.Diag(diag::warn_drv_invalid_arch_name_with_suggestion
)
62 << A
->getValue() << (Triple
.getArch() == llvm::Triple::x86
)
63 << join(ValidArchs
, ", ");
65 return std::string(CPU
);
68 // Select the default CPU if none was given (or detection failed).
71 return ""; // This routine is only handling x86 targets.
73 bool Is64Bit
= Triple
.getArch() == llvm::Triple::x86_64
;
75 // FIXME: Need target hooks.
76 if (Triple
.isOSDarwin()) {
77 if (Triple
.getArchName() == "x86_64h")
79 // macosx10.12 drops support for all pre-Penryn Macs.
80 // Simulators can still run on 10.11 though, like Xcode.
81 if (Triple
.isMacOSX() && !Triple
.isOSVersionLT(10, 12))
84 if (Triple
.isDriverKit())
87 // The oldest x86_64 Macs have core2/Merom; the oldest x86 Macs have Yonah.
88 return Is64Bit
? "core2" : "yonah";
91 // Set up default CPU name for PS4/PS5 compilers.
97 // On Android use targets compatible with gcc
98 if (Triple
.isAndroid())
99 return Is64Bit
? "x86-64" : "i686";
101 // Everything else goes to x86-64 in 64-bit mode.
105 switch (Triple
.getOS()) {
106 case llvm::Triple::NetBSD
:
108 case llvm::Triple::Haiku
:
109 case llvm::Triple::OpenBSD
:
111 case llvm::Triple::FreeBSD
:
119 void x86::getX86TargetFeatures(const Driver
&D
, const llvm::Triple
&Triple
,
121 std::vector
<StringRef
> &Features
) {
122 // Claim and report unsupported -mabi=. Note: we don't support "sysv_abi" or
123 // "ms_abi" as default function attributes.
124 if (const Arg
*A
= Args
.getLastArg(clang::driver::options::OPT_mabi_EQ
)) {
125 StringRef DefaultAbi
= Triple
.isOSWindows() ? "ms" : "sysv";
126 if (A
->getValue() != DefaultAbi
)
127 D
.Diag(diag::err_drv_unsupported_opt_for_target
)
128 << A
->getSpelling() << Triple
.getTriple();
131 // If -march=native, autodetect the feature list.
132 if (const Arg
*A
= Args
.getLastArg(clang::driver::options::OPT_march_EQ
)) {
133 if (StringRef(A
->getValue()) == "native") {
134 for (auto &F
: llvm::sys::getHostCPUFeatures())
136 Args
.MakeArgString((F
.second
? "+" : "-") + F
.first()));
140 if (Triple
.getArchName() == "x86_64h") {
141 // x86_64h implies quite a few of the more modern subtarget features
142 // for Haswell class CPUs, but not all of them. Opt-out of a few.
143 Features
.push_back("-rdrnd");
144 Features
.push_back("-aes");
145 Features
.push_back("-pclmul");
146 Features
.push_back("-rtm");
147 Features
.push_back("-fsgsbase");
150 const llvm::Triple::ArchType ArchType
= Triple
.getArch();
151 // Add features to be compatible with gcc for Android.
152 if (Triple
.isAndroid()) {
153 if (ArchType
== llvm::Triple::x86_64
) {
154 Features
.push_back("+sse4.2");
155 Features
.push_back("+popcnt");
156 Features
.push_back("+cx16");
158 Features
.push_back("+ssse3");
161 // Translate the high level `-mretpoline` flag to the specific target feature
162 // flags. We also detect if the user asked for retpoline external thunks but
163 // failed to ask for retpolines themselves (through any of the different
164 // flags). This is a bit hacky but keeps existing usages working. We should
165 // consider deprecating this and instead warn if the user requests external
166 // retpoline thunks and *doesn't* request some form of retpolines.
167 auto SpectreOpt
= clang::driver::options::ID::OPT_INVALID
;
168 if (Args
.hasArgNoClaim(options::OPT_mretpoline
, options::OPT_mno_retpoline
,
169 options::OPT_mspeculative_load_hardening
,
170 options::OPT_mno_speculative_load_hardening
)) {
171 if (Args
.hasFlag(options::OPT_mretpoline
, options::OPT_mno_retpoline
,
173 Features
.push_back("+retpoline-indirect-calls");
174 Features
.push_back("+retpoline-indirect-branches");
175 SpectreOpt
= options::OPT_mretpoline
;
176 } else if (Args
.hasFlag(options::OPT_mspeculative_load_hardening
,
177 options::OPT_mno_speculative_load_hardening
,
179 // On x86, speculative load hardening relies on at least using retpolines
180 // for indirect calls.
181 Features
.push_back("+retpoline-indirect-calls");
182 SpectreOpt
= options::OPT_mspeculative_load_hardening
;
184 } else if (Args
.hasFlag(options::OPT_mretpoline_external_thunk
,
185 options::OPT_mno_retpoline_external_thunk
, false)) {
186 // FIXME: Add a warning about failing to specify `-mretpoline` and
187 // eventually switch to an error here.
188 Features
.push_back("+retpoline-indirect-calls");
189 Features
.push_back("+retpoline-indirect-branches");
190 SpectreOpt
= options::OPT_mretpoline_external_thunk
;
193 auto LVIOpt
= clang::driver::options::ID::OPT_INVALID
;
194 if (Args
.hasFlag(options::OPT_mlvi_hardening
, options::OPT_mno_lvi_hardening
,
196 Features
.push_back("+lvi-load-hardening");
197 Features
.push_back("+lvi-cfi"); // load hardening implies CFI protection
198 LVIOpt
= options::OPT_mlvi_hardening
;
199 } else if (Args
.hasFlag(options::OPT_mlvi_cfi
, options::OPT_mno_lvi_cfi
,
201 Features
.push_back("+lvi-cfi");
202 LVIOpt
= options::OPT_mlvi_cfi
;
205 if (Args
.hasFlag(options::OPT_m_seses
, options::OPT_mno_seses
, false)) {
206 if (LVIOpt
== options::OPT_mlvi_hardening
)
207 D
.Diag(diag::err_drv_argument_not_allowed_with
)
208 << D
.getOpts().getOptionName(options::OPT_mlvi_hardening
)
209 << D
.getOpts().getOptionName(options::OPT_m_seses
);
211 if (SpectreOpt
!= clang::driver::options::ID::OPT_INVALID
)
212 D
.Diag(diag::err_drv_argument_not_allowed_with
)
213 << D
.getOpts().getOptionName(SpectreOpt
)
214 << D
.getOpts().getOptionName(options::OPT_m_seses
);
216 Features
.push_back("+seses");
217 if (!Args
.hasArg(options::OPT_mno_lvi_cfi
)) {
218 Features
.push_back("+lvi-cfi");
219 LVIOpt
= options::OPT_mlvi_cfi
;
223 if (SpectreOpt
!= clang::driver::options::ID::OPT_INVALID
&&
224 LVIOpt
!= clang::driver::options::ID::OPT_INVALID
) {
225 D
.Diag(diag::err_drv_argument_not_allowed_with
)
226 << D
.getOpts().getOptionName(SpectreOpt
)
227 << D
.getOpts().getOptionName(LVIOpt
);
230 for (const Arg
*A
: Args
.filtered(options::OPT_m_x86_AVX10_Features_Group
)) {
231 StringRef Name
= A
->getOption().getName();
235 assert(Name
.starts_with("m") && "Invalid feature name.");
236 Name
= Name
.substr(1);
238 bool IsNegative
= Name
.consume_front("no-");
241 assert(Name
.starts_with("avx10.") && "Invalid AVX10 feature name.");
242 StringRef Version
, Width
;
243 std::tie(Version
, Width
) = Name
.substr(6).split('-');
244 assert((Version
== "1" || Version
== "2") && "Invalid AVX10 feature name.");
245 assert((Width
== "256" || Width
== "512") && "Invalid AVX10 feature name.");
248 Features
.push_back(Args
.MakeArgString((IsNegative
? "-" : "+") + Name
));
251 // Now add any that the user explicitly requested on the command line,
252 // which may override the defaults.
253 for (const Arg
*A
: Args
.filtered(options::OPT_m_x86_Features_Group
,
254 options::OPT_mgeneral_regs_only
)) {
255 StringRef Name
= A
->getOption().getName();
259 assert(Name
.starts_with("m") && "Invalid feature name.");
260 Name
= Name
.substr(1);
262 // Replace -mgeneral-regs-only with -x87, -mmx, -sse
263 if (A
->getOption().getID() == options::OPT_mgeneral_regs_only
) {
264 Features
.insert(Features
.end(), {"-x87", "-mmx", "-sse"});
268 bool IsNegative
= Name
.starts_with("no-");
270 bool Not64Bit
= ArchType
!= llvm::Triple::x86_64
;
271 if (Not64Bit
&& Name
== "uintr")
272 D
.Diag(diag::err_drv_unsupported_opt_for_target
)
273 << A
->getSpelling() << Triple
.getTriple();
275 if (A
->getOption().matches(options::OPT_mapx_features_EQ
) ||
276 A
->getOption().matches(options::OPT_mno_apx_features_EQ
)) {
278 if (Not64Bit
&& !IsNegative
)
279 D
.Diag(diag::err_drv_unsupported_opt_for_target
)
280 << StringRef(A
->getSpelling().str() + "|-mapxf")
281 << Triple
.getTriple();
283 for (StringRef Value
: A
->getValues()) {
284 if (Value
!= "egpr" && Value
!= "push2pop2" && Value
!= "ppx" &&
285 Value
!= "ndd" && Value
!= "ccmp" && Value
!= "nf" &&
286 Value
!= "cf" && Value
!= "zu")
287 D
.Diag(clang::diag::err_drv_unsupported_option_argument
)
288 << A
->getSpelling() << Value
;
291 Args
.MakeArgString((IsNegative
? "-" : "+") + Value
));
296 Name
= Name
.substr(3);
297 Features
.push_back(Args
.MakeArgString((IsNegative
? "-" : "+") + Name
));
300 // Enable/disable straight line speculation hardening.
301 if (Arg
*A
= Args
.getLastArg(options::OPT_mharden_sls_EQ
)) {
302 StringRef Scope
= A
->getValue();
303 if (Scope
== "all") {
304 Features
.push_back("+harden-sls-ijmp");
305 Features
.push_back("+harden-sls-ret");
306 } else if (Scope
== "return") {
307 Features
.push_back("+harden-sls-ret");
308 } else if (Scope
== "indirect-jmp") {
309 Features
.push_back("+harden-sls-ijmp");
310 } else if (Scope
!= "none") {
311 D
.Diag(diag::err_drv_unsupported_option_argument
)
312 << A
->getSpelling() << Scope
;
316 // -mno-gather, -mno-scatter support
317 if (Args
.hasArg(options::OPT_mno_gather
))
318 Features
.push_back("+prefer-no-gather");
319 if (Args
.hasArg(options::OPT_mno_scatter
))
320 Features
.push_back("+prefer-no-scatter");
321 if (Args
.hasArg(options::OPT_mapx_inline_asm_use_gpr32
))
322 Features
.push_back("+inline-asm-use-gpr32");
324 // Warn for removed 3dnow support
326 Args
.getLastArg(options::OPT_m3dnowa
, options::OPT_mno_3dnowa
,
327 options::OPT_mno_3dnow
)) {
328 if (A
->getOption().matches(options::OPT_m3dnowa
))
329 D
.Diag(diag::warn_drv_clang_unsupported
) << A
->getAsString(Args
);
332 Args
.getLastArg(options::OPT_m3dnow
, options::OPT_mno_3dnow
)) {
333 if (A
->getOption().matches(options::OPT_m3dnow
))
334 D
.Diag(diag::warn_drv_clang_unsupported
) << A
->getAsString(Args
);