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"
11#include "clang/Options/Options.h"
12#include "llvm/ADT/StringExtras.h"
13#include "llvm/ADT/StringMap.h"
14#include "llvm/Option/ArgList.h"
15#include "llvm/TargetParser/Host.h"
16
17using namespace clang::driver;
18using namespace clang::driver::tools;
19using namespace clang;
20using namespace llvm::opt;
21
22std::string x86::getX86TargetCPU(const Driver &D, const ArgList &Args,
23 const llvm::Triple &Triple) {
24 if (const Arg *A = Args.getLastArg(Ids: options::OPT_march_EQ)) {
25 StringRef CPU = A->getValue();
26 if (CPU != "native")
27 return std::string(CPU);
28
29 // FIXME: Reject attempts to use -march=native unless the target matches
30 // the host.
31 CPU = llvm::sys::getHostCPUName();
32 if (!CPU.empty() && CPU != "generic")
33 return std::string(CPU);
34 }
35
36 if (const Arg *A = Args.getLastArg(Ids: options::OPT__SLASH_arch)) {
37 // Mapping built by looking at lib/Basic's X86TargetInfo::initFeatureMap().
38 // The keys are case-sensitive; this matches link.exe.
39 // 32-bit and 64-bit /arch: flags.
40 llvm::StringMap<StringRef> ArchMap({
41 {"AVX", "sandybridge"},
42 {"AVX2", "haswell"},
43 {"AVX512F", "knl"},
44 {"AVX512", "skylake-avx512"},
45 {"AVX10.1", "sapphirerapids"},
46 {"AVX10.2", "diamondrapids"},
47 });
48 if (Triple.getArch() == llvm::Triple::x86) {
49 // 32-bit-only /arch: flags.
50 ArchMap.insert(List: {
51 {"IA32", "i386"},
52 {"SSE", "pentium3"},
53 {"SSE2", "pentium4"},
54 });
55 }
56 StringRef CPU = ArchMap.lookup(Key: A->getValue());
57 if (CPU.empty()) {
58 std::vector<StringRef> ValidArchs{ArchMap.keys().begin(),
59 ArchMap.keys().end()};
60 sort(C&: ValidArchs);
61 D.Diag(DiagID: diag::warn_drv_invalid_arch_name_with_suggestion)
62 << A->getValue() << (Triple.getArch() == llvm::Triple::x86)
63 << join(R&: ValidArchs, Separator: ", ");
64 }
65 return std::string(CPU);
66 }
67
68 // Select the default CPU if none was given (or detection failed).
69
70 if (!Triple.isX86())
71 return ""; // This routine is only handling x86 targets.
72
73 bool Is64Bit = Triple.getArch() == llvm::Triple::x86_64;
74
75 // FIXME: Need target hooks.
76 if (Triple.isOSDarwin()) {
77 if (Triple.getArchName() == "x86_64h")
78 return "core-avx2";
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(Major: 10, Minor: 12))
82 return "penryn";
83
84 if (Triple.isDriverKit())
85 return "nehalem";
86
87 // The oldest x86_64 Macs have core2/Merom; the oldest x86 Macs have Yonah.
88 return Is64Bit ? "core2" : "yonah";
89 }
90
91 // Set up default CPU name for PS4/PS5 compilers.
92 if (Triple.isPS4())
93 return "btver2";
94 if (Triple.isPS5())
95 return "znver2";
96
97 // On Android use targets compatible with gcc
98 if (Triple.isAndroid())
99 return Is64Bit ? "x86-64" : "i686";
100
101 // Everything else goes to x86-64 in 64-bit mode.
102 if (Is64Bit)
103 return "x86-64";
104
105 switch (Triple.getOS()) {
106 case llvm::Triple::NetBSD:
107 return "i486";
108 case llvm::Triple::Haiku:
109 case llvm::Triple::OpenBSD:
110 return "i586";
111 case llvm::Triple::FreeBSD:
112 return "i686";
113 default:
114 // Fallback to p4.
115 return "pentium4";
116 }
117}
118
119void x86::getX86TargetFeatures(const Driver &D, const llvm::Triple &Triple,
120 const ArgList &Args,
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(Ids: options::OPT_mabi_EQ)) {
125 StringRef DefaultAbi =
126 (Triple.isOSWindows() || Triple.isUEFI()) ? "ms" : "sysv";
127 if (A->getValue() != DefaultAbi)
128 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
129 << A->getSpelling() << Triple.getTriple();
130 }
131
132 // If -march=native, autodetect the feature list.
133 if (const Arg *A = Args.getLastArg(Ids: options::OPT_march_EQ)) {
134 if (StringRef(A->getValue()) == "native") {
135 for (auto &F : llvm::sys::getHostCPUFeatures())
136 Features.push_back(
137 x: Args.MakeArgString(Str: (F.second ? "+" : "-") + F.first()));
138 }
139 }
140
141 if (Triple.getArchName() == "x86_64h") {
142 // x86_64h implies quite a few of the more modern subtarget features
143 // for Haswell class CPUs, but not all of them. Opt-out of a few.
144 Features.push_back(x: "-rdrnd");
145 Features.push_back(x: "-aes");
146 Features.push_back(x: "-pclmul");
147 Features.push_back(x: "-rtm");
148 Features.push_back(x: "-fsgsbase");
149 }
150
151 const llvm::Triple::ArchType ArchType = Triple.getArch();
152 // Add features to be compatible with gcc for Android.
153 if (Triple.isAndroid()) {
154 if (ArchType == llvm::Triple::x86_64) {
155 Features.push_back(x: "+sse4.2");
156 Features.push_back(x: "+popcnt");
157 Features.push_back(x: "+cx16");
158 } else
159 Features.push_back(x: "+ssse3");
160 }
161
162 // Translate the high level `-mretpoline` flag to the specific target feature
163 // flags. We also detect if the user asked for retpoline external thunks but
164 // failed to ask for retpolines themselves (through any of the different
165 // flags). This is a bit hacky but keeps existing usages working. We should
166 // consider deprecating this and instead warn if the user requests external
167 // retpoline thunks and *doesn't* request some form of retpolines.
168 auto SpectreOpt = options::ID::OPT_INVALID;
169 if (Args.hasArgNoClaim(Ids: options::OPT_mretpoline, Ids: options::OPT_mno_retpoline,
170 Ids: options::OPT_mspeculative_load_hardening,
171 Ids: options::OPT_mno_speculative_load_hardening)) {
172 if (Args.hasFlag(Pos: options::OPT_mretpoline, Neg: options::OPT_mno_retpoline,
173 Default: false)) {
174 Features.push_back(x: "+retpoline-indirect-calls");
175 Features.push_back(x: "+retpoline-indirect-branches");
176 SpectreOpt = options::OPT_mretpoline;
177 } else if (Args.hasFlag(Pos: options::OPT_mspeculative_load_hardening,
178 Neg: options::OPT_mno_speculative_load_hardening,
179 Default: false)) {
180 // On x86, speculative load hardening relies on at least using retpolines
181 // for indirect calls.
182 Features.push_back(x: "+retpoline-indirect-calls");
183 SpectreOpt = options::OPT_mspeculative_load_hardening;
184 }
185 } else if (Args.hasFlag(Pos: options::OPT_mretpoline_external_thunk,
186 Neg: options::OPT_mno_retpoline_external_thunk, Default: false)) {
187 // FIXME: Add a warning about failing to specify `-mretpoline` and
188 // eventually switch to an error here.
189 Features.push_back(x: "+retpoline-indirect-calls");
190 Features.push_back(x: "+retpoline-indirect-branches");
191 SpectreOpt = options::OPT_mretpoline_external_thunk;
192 }
193
194 auto LVIOpt = options::ID::OPT_INVALID;
195 if (Args.hasFlag(Pos: options::OPT_mlvi_hardening, Neg: options::OPT_mno_lvi_hardening,
196 Default: false)) {
197 Features.push_back(x: "+lvi-load-hardening");
198 Features.push_back(x: "+lvi-cfi"); // load hardening implies CFI protection
199 LVIOpt = options::OPT_mlvi_hardening;
200 } else if (Args.hasFlag(Pos: options::OPT_mlvi_cfi, Neg: options::OPT_mno_lvi_cfi,
201 Default: false)) {
202 Features.push_back(x: "+lvi-cfi");
203 LVIOpt = options::OPT_mlvi_cfi;
204 }
205
206 if (Args.hasFlag(Pos: options::OPT_m_seses, Neg: options::OPT_mno_seses, Default: false)) {
207 if (LVIOpt == options::OPT_mlvi_hardening)
208 D.Diag(DiagID: diag::err_drv_argument_not_allowed_with)
209 << D.getOpts().getOptionName(id: options::OPT_mlvi_hardening)
210 << D.getOpts().getOptionName(id: options::OPT_m_seses);
211
212 if (SpectreOpt != options::ID::OPT_INVALID)
213 D.Diag(DiagID: diag::err_drv_argument_not_allowed_with)
214 << D.getOpts().getOptionName(id: SpectreOpt)
215 << D.getOpts().getOptionName(id: options::OPT_m_seses);
216
217 Features.push_back(x: "+seses");
218 if (!Args.hasArg(Ids: options::OPT_mno_lvi_cfi)) {
219 Features.push_back(x: "+lvi-cfi");
220 LVIOpt = options::OPT_mlvi_cfi;
221 }
222 }
223
224 if (SpectreOpt != options::ID::OPT_INVALID &&
225 LVIOpt != options::ID::OPT_INVALID) {
226 D.Diag(DiagID: diag::err_drv_argument_not_allowed_with)
227 << D.getOpts().getOptionName(id: SpectreOpt)
228 << D.getOpts().getOptionName(id: LVIOpt);
229 }
230
231 // Now add any that the user explicitly requested on the command line,
232 // which may override the defaults.
233 for (const Arg *A : Args.filtered(Ids: options::OPT_m_x86_Features_Group,
234 Ids: options::OPT_mgeneral_regs_only)) {
235 StringRef Name = A->getOption().getName();
236 A->claim();
237
238 // Skip over "-m".
239 assert(Name.starts_with("m") && "Invalid feature name.");
240 Name = Name.substr(Start: 1);
241
242 // Replace -mgeneral-regs-only with -x87, -mmx, -sse
243 if (A->getOption().getID() == options::OPT_mgeneral_regs_only) {
244 Features.insert(position: Features.end(), l: {"-x87", "-mmx", "-sse"});
245 continue;
246 }
247
248 bool IsNegative = Name.starts_with(Prefix: "no-");
249
250 bool Not64Bit = ArchType != llvm::Triple::x86_64;
251 if (Not64Bit && Name == "uintr")
252 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
253 << A->getSpelling() << Triple.getTriple();
254
255 if (IsNegative)
256 Name = Name.substr(Start: 3);
257 if (A->getOption().matches(ID: options::OPT_mapxf) ||
258 A->getOption().matches(ID: options::OPT_mno_apxf) ||
259 A->getOption().matches(ID: options::OPT_mapx_features_EQ) ||
260 A->getOption().matches(ID: options::OPT_mno_apx_features_EQ)) {
261
262 if (Name == "apxf") {
263 if (IsNegative) {
264 Features.insert(position: Features.end(),
265 l: {"-egpr", "-ndd", "-ccmp", "-nf", "-zu"});
266 if (!Triple.isOSWindows())
267 Features.insert(position: Features.end(), l: {"-push2pop2", "-ppx"});
268 } else {
269 Features.insert(position: Features.end(),
270 l: {"+egpr", "+ndd", "+ccmp", "+nf", "+zu"});
271 if (!Triple.isOSWindows())
272 Features.insert(position: Features.end(), l: {"+push2pop2", "+ppx"});
273
274 if (Not64Bit)
275 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
276 << StringRef("-mapxf") << Triple.getTriple();
277 }
278 continue;
279 }
280
281 if (Not64Bit && !IsNegative)
282 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
283 << StringRef("-mapx-features=") << Triple.getTriple();
284
285 for (StringRef Value : A->getValues()) {
286 if (Value != "egpr" && Value != "push2pop2" && Value != "ppx" &&
287 Value != "ndd" && Value != "ccmp" && Value != "nf" &&
288 Value != "cf" && Value != "zu")
289 D.Diag(DiagID: clang::diag::err_drv_unsupported_option_argument)
290 << A->getSpelling() << Value;
291
292 Features.push_back(
293 x: Args.MakeArgString(Str: (IsNegative ? "-" : "+") + Value));
294 }
295 continue;
296 }
297 Features.push_back(x: Args.MakeArgString(Str: (IsNegative ? "-" : "+") + Name));
298 }
299
300 // Enable/disable straight line speculation hardening.
301 if (Arg *A = Args.getLastArg(Ids: options::OPT_mharden_sls_EQ)) {
302 StringRef Scope = A->getValue();
303 if (Scope == "all") {
304 Features.push_back(x: "+harden-sls-ijmp");
305 Features.push_back(x: "+harden-sls-ret");
306 } else if (Scope == "return") {
307 Features.push_back(x: "+harden-sls-ret");
308 } else if (Scope == "indirect-jmp") {
309 Features.push_back(x: "+harden-sls-ijmp");
310 } else if (Scope != "none") {
311 D.Diag(DiagID: diag::err_drv_unsupported_option_argument)
312 << A->getSpelling() << Scope;
313 }
314 }
315
316 // -mno-gather, -mno-scatter support
317 if (Args.hasArg(Ids: options::OPT_mno_gather))
318 Features.push_back(x: "+prefer-no-gather");
319 if (Args.hasArg(Ids: options::OPT_mno_scatter))
320 Features.push_back(x: "+prefer-no-scatter");
321 if (Args.hasArg(Ids: options::OPT_mapx_inline_asm_use_gpr32))
322 Features.push_back(x: "+inline-asm-use-gpr32");
323
324 // Warn for removed 3dnow support
325 if (const Arg *A =
326 Args.getLastArg(Ids: options::OPT_m3dnowa, Ids: options::OPT_mno_3dnowa,
327 Ids: options::OPT_mno_3dnow)) {
328 if (A->getOption().matches(ID: options::OPT_m3dnowa))
329 D.Diag(DiagID: diag::warn_drv_clang_unsupported) << A->getAsString(Args);
330 }
331 if (const Arg *A =
332 Args.getLastArg(Ids: options::OPT_m3dnow, Ids: options::OPT_mno_3dnow)) {
333 if (A->getOption().matches(ID: options::OPT_m3dnow))
334 D.Diag(DiagID: diag::warn_drv_clang_unsupported) << A->getAsString(Args);
335 }
336}
337