clang 23.0.0git
X86.cpp
Go to the documentation of this file.
1//===--- X86.cpp - X86 Helpers for Tools ------------------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "X86.h"
10#include "clang/Driver/Driver.h"
12#include "llvm/ADT/STLExtras.h"
13#include "llvm/ADT/StringExtras.h"
14#include "llvm/ADT/StringMap.h"
15#include "llvm/Option/ArgList.h"
16#include "llvm/TargetParser/Host.h"
17#include "llvm/TargetParser/X86TargetParser.h"
18
19using namespace clang::driver;
20using namespace clang::driver::tools;
21using namespace clang;
22using namespace llvm::opt;
23
24std::string x86::getX86TargetCPU(const Driver &D, const ArgList &Args,
25 const llvm::Triple &Triple) {
26 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
27 StringRef CPU = A->getValue();
28 if (CPU != "native")
29 return std::string(CPU);
30
31 // FIXME: Reject attempts to use -march=native unless the target matches
32 // the host.
33 CPU = llvm::sys::getHostCPUName();
34 if (!CPU.empty() && CPU != "generic")
35 return std::string(CPU);
36 }
37
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"},
44 {"AVX2", "haswell"},
45 {"AVX512F", "knl"},
46 {"AVX512", "skylake-avx512"},
47 {"AVX10.1", "sapphirerapids"},
48 {"AVX10.2", "sapphirerapids"},
49 });
50 if (Triple.getArch() == llvm::Triple::x86) {
51 // 32-bit-only /arch: flags.
52 ArchMap.insert({
53 {"IA32", "i386"},
54 {"SSE", "pentium3"},
55 {"SSE2", "pentium4"},
56 });
57 }
58 StringRef CPU = ArchMap.lookup(A->getValue());
59 if (CPU.empty()) {
60 std::vector<StringRef> ValidArchs{ArchMap.keys().begin(),
61 ArchMap.keys().end()};
62 sort(ValidArchs);
63 D.Diag(diag::warn_drv_invalid_arch_name_with_suggestion)
64 << A->getValue() << (Triple.getArch() == llvm::Triple::x86)
65 << join(ValidArchs, ", ");
66 }
67 return std::string(CPU);
68 }
69
70 // Select the default CPU if none was given (or detection failed).
71
72 if (!Triple.isX86())
73 return ""; // This routine is only handling x86 targets.
74
75 bool Is64Bit = Triple.getArch() == llvm::Triple::x86_64;
76
77 // FIXME: Need target hooks.
78 if (Triple.isOSDarwin()) {
79 if (Triple.getArchName() == "x86_64h")
80 return "core-avx2";
81 // macosx10.12 drops support for all pre-Penryn Macs.
82 // Simulators can still run on 10.11 though, like Xcode.
83 if (Triple.isMacOSX() && !Triple.isOSVersionLT(10, 12))
84 return "penryn";
85
86 if (Triple.isDriverKit())
87 return "nehalem";
88
89 // The oldest x86_64 Macs have core2/Merom; the oldest x86 Macs have Yonah.
90 return Is64Bit ? "core2" : "yonah";
91 }
92
93 // Set up default CPU name for PS4/PS5 compilers.
94 if (Triple.isPS4())
95 return "btver2";
96 if (Triple.isPS5())
97 return "znver2";
98
99 // On Android use targets compatible with gcc
100 if (Triple.isAndroid())
101 return Is64Bit ? "x86-64" : "i686";
102
103 // Everything else goes to x86-64 in 64-bit mode.
104 if (Is64Bit)
105 return "x86-64";
106
107 switch (Triple.getOS()) {
108 case llvm::Triple::NetBSD:
109 return "i486";
110 case llvm::Triple::Haiku:
111 case llvm::Triple::OpenBSD:
112 return "i586";
113 case llvm::Triple::FreeBSD:
114 return "i686";
115 default:
116 // Fallback to p4.
117 return "pentium4";
118 }
119}
120
121void x86::getX86TargetFeatures(const Driver &D, const llvm::Triple &Triple,
122 const ArgList &Args,
123 std::vector<StringRef> &Features) {
124 // Claim and report unsupported -mabi=. Note: we don't support "sysv_abi" or
125 // "ms_abi" as default function attributes.
126 if (const Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
127 StringRef DefaultAbi =
128 (Triple.isOSWindows() || Triple.isUEFI()) ? "ms" : "sysv";
129 if (A->getValue() != DefaultAbi)
130 D.Diag(diag::err_drv_unsupported_opt_for_target)
131 << A->getSpelling() << Triple.getTriple();
132 }
133
134 // If -march=native, autodetect the feature list.
135 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
136 if (StringRef(A->getValue()) == "native") {
137 for (auto &F : llvm::sys::getHostCPUFeatures())
138 Features.push_back(
139 Args.MakeArgString((F.second ? "+" : "-") + F.first()));
140 }
141 }
142
143 if (Triple.getArchName() == "x86_64h") {
144 // x86_64h implies quite a few of the more modern subtarget features
145 // for Haswell class CPUs, but not all of them. Opt-out of a few.
146 Features.push_back("-rdrnd");
147 Features.push_back("-aes");
148 Features.push_back("-pclmul");
149 Features.push_back("-rtm");
150 Features.push_back("-fsgsbase");
151 }
152
153 const llvm::Triple::ArchType ArchType = Triple.getArch();
154
155 // Add features to be compatible with gcc for Android.
156 if (Triple.isAndroid()) {
157 if (ArchType == llvm::Triple::x86_64) {
158 Features.push_back("+sse4.2");
159 Features.push_back("+popcnt");
160 Features.push_back("+cx16");
161 } else
162 Features.push_back("+ssse3");
163 }
164
165 // Translate the high level `-mretpoline` flag to the specific target feature
166 // flags. We also detect if the user asked for retpoline external thunks but
167 // failed to ask for retpolines themselves (through any of the different
168 // flags). This is a bit hacky but keeps existing usages working. We should
169 // consider deprecating this and instead warn if the user requests external
170 // retpoline thunks and *doesn't* request some form of retpolines.
171 auto SpectreOpt = options::ID::OPT_INVALID;
172 if (Args.hasArgNoClaim(options::OPT_mretpoline, options::OPT_mno_retpoline,
173 options::OPT_mspeculative_load_hardening,
174 options::OPT_mno_speculative_load_hardening)) {
175 if (Args.hasFlag(options::OPT_mretpoline, options::OPT_mno_retpoline,
176 false)) {
177 Features.push_back("+retpoline-indirect-calls");
178 Features.push_back("+retpoline-indirect-branches");
179 SpectreOpt = options::OPT_mretpoline;
180 } else if (Args.hasFlag(options::OPT_mspeculative_load_hardening,
181 options::OPT_mno_speculative_load_hardening,
182 false)) {
183 // On x86, speculative load hardening relies on at least using retpolines
184 // for indirect calls.
185 Features.push_back("+retpoline-indirect-calls");
186 SpectreOpt = options::OPT_mspeculative_load_hardening;
187 }
188 } else if (Args.hasFlag(options::OPT_mretpoline_external_thunk,
189 options::OPT_mno_retpoline_external_thunk, false)) {
190 // FIXME: Add a warning about failing to specify `-mretpoline` and
191 // eventually switch to an error here.
192 Features.push_back("+retpoline-indirect-calls");
193 Features.push_back("+retpoline-indirect-branches");
194 SpectreOpt = options::OPT_mretpoline_external_thunk;
195 }
196
197 auto LVIOpt = options::ID::OPT_INVALID;
198 if (Args.hasFlag(options::OPT_mlvi_hardening, options::OPT_mno_lvi_hardening,
199 false)) {
200 Features.push_back("+lvi-load-hardening");
201 Features.push_back("+lvi-cfi"); // load hardening implies CFI protection
202 LVIOpt = options::OPT_mlvi_hardening;
203 } else if (Args.hasFlag(options::OPT_mlvi_cfi, options::OPT_mno_lvi_cfi,
204 false)) {
205 Features.push_back("+lvi-cfi");
206 LVIOpt = options::OPT_mlvi_cfi;
207 }
208
209 if (Args.hasFlag(options::OPT_m_seses, options::OPT_mno_seses, false)) {
210 if (LVIOpt == options::OPT_mlvi_hardening)
211 D.Diag(diag::err_drv_argument_not_allowed_with)
212 << D.getOpts().getOptionName(options::OPT_mlvi_hardening)
213 << D.getOpts().getOptionName(options::OPT_m_seses);
214
215 if (SpectreOpt != options::ID::OPT_INVALID)
216 D.Diag(diag::err_drv_argument_not_allowed_with)
217 << D.getOpts().getOptionName(SpectreOpt)
218 << D.getOpts().getOptionName(options::OPT_m_seses);
219
220 Features.push_back("+seses");
221 if (!Args.hasArg(options::OPT_mno_lvi_cfi)) {
222 Features.push_back("+lvi-cfi");
223 LVIOpt = options::OPT_mlvi_cfi;
224 }
225 }
226
227 if (SpectreOpt != options::ID::OPT_INVALID &&
228 LVIOpt != options::ID::OPT_INVALID) {
229 D.Diag(diag::err_drv_argument_not_allowed_with)
230 << D.getOpts().getOptionName(SpectreOpt)
231 << D.getOpts().getOptionName(LVIOpt);
232 }
233
234 enum class EGPRFeature { Unknown, Disabled, Enabled };
235 EGPRFeature EGPROpt = EGPRFeature::Unknown;
236 // Now add any that the user explicitly requested on the command line,
237 // which may override the defaults.
238 for (const Arg *A : Args.filtered(options::OPT_m_x86_Features_Group,
239 options::OPT_mgeneral_regs_only)) {
240 StringRef Name = A->getOption().getName();
241 A->claim();
242
243 // Skip over "-m".
244 assert(Name.starts_with("m") && "Invalid feature name.");
245 Name = Name.substr(1);
246
247 // Replace -mgeneral-regs-only with -x87, -mmx, -sse
248 if (A->getOption().getID() == options::OPT_mgeneral_regs_only) {
249 Features.insert(Features.end(), {"-x87", "-mmx", "-sse"});
250 continue;
251 }
252
253 bool IsNegative = Name.starts_with("no-");
254
255 bool Not64Bit = ArchType != llvm::Triple::x86_64;
256 if (Not64Bit && Name == "uintr")
257 D.Diag(diag::err_drv_unsupported_opt_for_target)
258 << A->getSpelling() << Triple.getTriple();
259
260 if (IsNegative)
261 Name = Name.substr(3);
262
263 if (A->getOption().matches(options::OPT_mapxf) ||
264 A->getOption().matches(options::OPT_mno_apxf)) {
265 if (IsNegative) {
266 EGPROpt = EGPRFeature::Disabled;
267 } else {
268 EGPROpt = EGPRFeature::Enabled;
269 if (Not64Bit)
270 D.Diag(diag::err_drv_unsupported_opt_for_target)
271 << StringRef("-mapxf") << Triple.getTriple();
272 }
273 continue;
274 }
275
276 if (A->getOption().matches(options::OPT_mapx_features_EQ) ||
277 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("-mapx-features=") << Triple.getTriple();
281
282 for (StringRef Value : A->getValues()) {
283 if (Value != "egpr" && Value != "push2pop2" && Value != "ppx" &&
284 Value != "ndd" && Value != "ccmp" && Value != "nf" &&
285 Value != "cf" && Value != "zu" && Value != "jmpabs")
286 D.Diag(clang::diag::err_drv_unsupported_option_argument)
287 << A->getSpelling() << Value;
288
289 if (Value == "egpr") {
290 EGPROpt = IsNegative ? EGPRFeature::Disabled : EGPRFeature::Enabled;
291 }
292
293 Features.push_back(
294 Args.MakeArgString((IsNegative ? "-" : "+") + Value));
295 }
296 continue;
297 }
298
299 if (Name == "egpr") {
300 EGPROpt = IsNegative ? EGPRFeature::Disabled : EGPRFeature::Enabled;
301 }
302 Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
303 }
304
305 // Enable/disable straight line speculation hardening.
306 if (Arg *A = Args.getLastArg(options::OPT_mharden_sls_EQ)) {
307 StringRef Scope = A->getValue();
308 if (Scope == "all") {
309 Features.push_back("+harden-sls-ijmp");
310 Features.push_back("+harden-sls-ret");
311 } else if (Scope == "return") {
312 Features.push_back("+harden-sls-ret");
313 } else if (Scope == "indirect-jmp") {
314 Features.push_back("+harden-sls-ijmp");
315 } else if (Scope != "none") {
316 D.Diag(diag::err_drv_unsupported_option_argument)
317 << A->getSpelling() << Scope;
318 }
319 }
320
321 // -mno-gather, -mno-scatter support
322 if (Args.hasArg(options::OPT_mno_gather))
323 Features.push_back("+prefer-no-gather");
324 if (Args.hasArg(options::OPT_mno_scatter))
325 Features.push_back("+prefer-no-scatter");
326 if (Args.hasArg(options::OPT_mapx_inline_asm_use_gpr32))
327 Features.push_back("+inline-asm-use-gpr32");
328
329 // Warn for removed 3dnow support
330 if (const Arg *A =
331 Args.getLastArg(options::OPT_m3dnowa, options::OPT_mno_3dnowa,
332 options::OPT_mno_3dnow)) {
333 if (A->getOption().matches(options::OPT_m3dnowa))
334 D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
335 }
336 if (const Arg *A =
337 Args.getLastArg(options::OPT_m3dnow, options::OPT_mno_3dnow)) {
338 if (A->getOption().matches(options::OPT_m3dnow))
339 D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
340 }
341
342 // Handle features corresponding to "-ffixed-X" options
343 if (Args.hasArg(options::OPT_ffixed_edi)) {
344 if (ArchType != llvm::Triple::x86)
345 D.Diag(diag::err_drv_unsupported_opt_for_target)
346 << "-ffixed-edi" << Triple.getTriple();
347 else
348 Features.push_back("+reserve-edi");
349 }
350#define RESERVE_REG(REG) \
351 if (Args.hasArg(options::OPT_ffixed_##REG)) \
352 Features.push_back("+reserve-" #REG);
353 RESERVE_REG(r8)
354 RESERVE_REG(r9)
355 RESERVE_REG(r10)
356 RESERVE_REG(r11)
357 RESERVE_REG(r12)
358 RESERVE_REG(r13)
359 RESERVE_REG(r14)
360 RESERVE_REG(r15)
361#undef RESERVE_REG
362
363 bool NeedDetectEGPR = Args.hasArg(
364 options::OPT_ffixed_r16, options::OPT_ffixed_r17, options::OPT_ffixed_r18,
365 options::OPT_ffixed_r19, options::OPT_ffixed_r20, options::OPT_ffixed_r21,
366 options::OPT_ffixed_r22, options::OPT_ffixed_r23, options::OPT_ffixed_r24,
367 options::OPT_ffixed_r25, options::OPT_ffixed_r26, options::OPT_ffixed_r27,
368 options::OPT_ffixed_r28, options::OPT_ffixed_r29, options::OPT_ffixed_r30,
369 options::OPT_ffixed_r31);
370 if (NeedDetectEGPR && EGPROpt == EGPRFeature::Unknown &&
371 ArchType == llvm::Triple::x86_64) {
372 SmallVector<StringRef, 16> CPUFeatures;
373 llvm::X86::getFeaturesForCPU(getX86TargetCPU(D, Args, Triple), CPUFeatures);
374 EGPROpt = llvm::is_contained(CPUFeatures, "+egpr") ? EGPRFeature::Enabled
375 : EGPRFeature::Disabled;
376 }
377#define RESERVE_EGPR(REG) \
378 if (Args.hasArg(options::OPT_ffixed_##REG)) { \
379 if (EGPROpt != EGPRFeature::Enabled) \
380 D.Diag(diag::err_drv_unsupported_opt_for_target) \
381 << "-ffixed-" #REG << Triple.getTriple(); \
382 else \
383 Features.push_back("+reserve-" #REG); \
384 }
385 RESERVE_EGPR(r16)
386 RESERVE_EGPR(r17)
387 RESERVE_EGPR(r18)
388 RESERVE_EGPR(r19)
389 RESERVE_EGPR(r20)
390 RESERVE_EGPR(r21)
391 RESERVE_EGPR(r22)
392 RESERVE_EGPR(r23)
393 RESERVE_EGPR(r24)
394 RESERVE_EGPR(r25)
395 RESERVE_EGPR(r26)
396 RESERVE_EGPR(r27)
397 RESERVE_EGPR(r28)
398 RESERVE_EGPR(r29)
399 RESERVE_EGPR(r30)
400 RESERVE_EGPR(r31)
401#undef RESERVE_EGPR
402}
#define RESERVE_REG(REG)
#define RESERVE_EGPR(REG)
Scope - A scope is a transient data structure that is used while parsing the program.
Definition Scope.h:41
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition Driver.h:94
DiagnosticBuilder Diag(unsigned DiagID) const
Definition Driver.h:158
const llvm::opt::OptTable & getOpts() const
Definition Driver.h:406
std::string getX86TargetCPU(const Driver &D, const llvm::opt::ArgList &Args, const llvm::Triple &Triple)
void getX86TargetFeatures(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args, std::vector< llvm::StringRef > &Features)
llvm::ImmutableSet< T > join(llvm::ImmutableSet< T > A, llvm::ImmutableSet< T > B, typename llvm::ImmutableSet< T >::Factory &F)
Computes the union of two ImmutableSets.
Definition Utils.h:39
@ Disabled
When the validation is disabled. For example, for a precompiled header.
Definition ModuleFile.h:141
The JSON file list parser is used to communicate input to InstallAPI.