[DFAJumpThreading] Remove incoming StartBlock from all phis when unfolding select...
[llvm-project.git] / clang / lib / Driver / ToolChains / Arch / X86.cpp
blob848c26ddb43e4ae15eacec08b0f65d702d6ad106
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 //===----------------------------------------------------------------------===//
9 #include "X86.h"
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();
28 if (CPU != "native")
29 return std::string(CPU);
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);
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 });
48 if (Triple.getArch() == llvm::Triple::x86) {
49 // 32-bit-only /arch: flags.
50 ArchMap.insert({
51 {"IA32", "i386"},
52 {"SSE", "pentium3"},
53 {"SSE2", "pentium4"},
54 });
56 StringRef CPU = ArchMap.lookup(A->getValue());
57 if (CPU.empty()) {
58 std::vector<StringRef> ValidArchs{ArchMap.keys().begin(),
59 ArchMap.keys().end()};
60 sort(ValidArchs);
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).
70 if (!Triple.isX86())
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")
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(10, 12))
82 return "penryn";
84 if (Triple.isDriverKit())
85 return "nehalem";
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.
92 if (Triple.isPS4())
93 return "btver2";
94 if (Triple.isPS5())
95 return "znver2";
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.
102 if (Is64Bit)
103 return "x86-64";
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";
119 void 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(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 llvm::StringMap<bool> HostFeatures;
135 if (llvm::sys::getHostCPUFeatures(HostFeatures))
136 for (auto &F : HostFeatures)
137 Features.push_back(
138 Args.MakeArgString((F.second ? "+" : "-") + F.first()));
142 if (Triple.getArchName() == "x86_64h") {
143 // x86_64h implies quite a few of the more modern subtarget features
144 // for Haswell class CPUs, but not all of them. Opt-out of a few.
145 Features.push_back("-rdrnd");
146 Features.push_back("-aes");
147 Features.push_back("-pclmul");
148 Features.push_back("-rtm");
149 Features.push_back("-fsgsbase");
152 const llvm::Triple::ArchType ArchType = Triple.getArch();
153 // Add features to be compatible with gcc for Android.
154 if (Triple.isAndroid()) {
155 if (ArchType == llvm::Triple::x86_64) {
156 Features.push_back("+sse4.2");
157 Features.push_back("+popcnt");
158 Features.push_back("+cx16");
159 } else
160 Features.push_back("+ssse3");
163 // Translate the high level `-mretpoline` flag to the specific target feature
164 // flags. We also detect if the user asked for retpoline external thunks but
165 // failed to ask for retpolines themselves (through any of the different
166 // flags). This is a bit hacky but keeps existing usages working. We should
167 // consider deprecating this and instead warn if the user requests external
168 // retpoline thunks and *doesn't* request some form of retpolines.
169 auto SpectreOpt = clang::driver::options::ID::OPT_INVALID;
170 if (Args.hasArgNoClaim(options::OPT_mretpoline, options::OPT_mno_retpoline,
171 options::OPT_mspeculative_load_hardening,
172 options::OPT_mno_speculative_load_hardening)) {
173 if (Args.hasFlag(options::OPT_mretpoline, options::OPT_mno_retpoline,
174 false)) {
175 Features.push_back("+retpoline-indirect-calls");
176 Features.push_back("+retpoline-indirect-branches");
177 SpectreOpt = options::OPT_mretpoline;
178 } else if (Args.hasFlag(options::OPT_mspeculative_load_hardening,
179 options::OPT_mno_speculative_load_hardening,
180 false)) {
181 // On x86, speculative load hardening relies on at least using retpolines
182 // for indirect calls.
183 Features.push_back("+retpoline-indirect-calls");
184 SpectreOpt = options::OPT_mspeculative_load_hardening;
186 } else if (Args.hasFlag(options::OPT_mretpoline_external_thunk,
187 options::OPT_mno_retpoline_external_thunk, false)) {
188 // FIXME: Add a warning about failing to specify `-mretpoline` and
189 // eventually switch to an error here.
190 Features.push_back("+retpoline-indirect-calls");
191 Features.push_back("+retpoline-indirect-branches");
192 SpectreOpt = options::OPT_mretpoline_external_thunk;
195 auto LVIOpt = clang::driver::options::ID::OPT_INVALID;
196 if (Args.hasFlag(options::OPT_mlvi_hardening, options::OPT_mno_lvi_hardening,
197 false)) {
198 Features.push_back("+lvi-load-hardening");
199 Features.push_back("+lvi-cfi"); // load hardening implies CFI protection
200 LVIOpt = options::OPT_mlvi_hardening;
201 } else if (Args.hasFlag(options::OPT_mlvi_cfi, options::OPT_mno_lvi_cfi,
202 false)) {
203 Features.push_back("+lvi-cfi");
204 LVIOpt = options::OPT_mlvi_cfi;
207 if (Args.hasFlag(options::OPT_m_seses, options::OPT_mno_seses, false)) {
208 if (LVIOpt == options::OPT_mlvi_hardening)
209 D.Diag(diag::err_drv_argument_not_allowed_with)
210 << D.getOpts().getOptionName(options::OPT_mlvi_hardening)
211 << D.getOpts().getOptionName(options::OPT_m_seses);
213 if (SpectreOpt != clang::driver::options::ID::OPT_INVALID)
214 D.Diag(diag::err_drv_argument_not_allowed_with)
215 << D.getOpts().getOptionName(SpectreOpt)
216 << D.getOpts().getOptionName(options::OPT_m_seses);
218 Features.push_back("+seses");
219 if (!Args.hasArg(options::OPT_mno_lvi_cfi)) {
220 Features.push_back("+lvi-cfi");
221 LVIOpt = options::OPT_mlvi_cfi;
225 if (SpectreOpt != clang::driver::options::ID::OPT_INVALID &&
226 LVIOpt != clang::driver::options::ID::OPT_INVALID) {
227 D.Diag(diag::err_drv_argument_not_allowed_with)
228 << D.getOpts().getOptionName(SpectreOpt)
229 << D.getOpts().getOptionName(LVIOpt);
232 bool HasAVX10 = false;
233 for (const Arg *A : Args.filtered(options::OPT_m_x86_AVX10_Features_Group)) {
234 StringRef Name = A->getOption().getName();
235 A->claim();
237 // Skip over "-m".
238 assert(Name.startswith("m") && "Invalid feature name.");
239 Name = Name.substr(1);
241 bool IsNegative = Name.startswith("no-");
242 if (IsNegative)
243 Name = Name.substr(3);
245 #ifndef NDEBUG
246 assert(Name.startswith("avx10.") && "Invalid AVX10 feature name.");
247 StringRef Version, Width;
248 std::tie(Version, Width) = Name.substr(6).split('-');
249 assert(Version == "1" && "Invalid AVX10 feature name.");
250 assert((Width == "256" || Width == "512") && "Invalid AVX10 feature name.");
251 #endif
253 Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
254 HasAVX10 = true;
257 // Now add any that the user explicitly requested on the command line,
258 // which may override the defaults.
259 for (const Arg *A : Args.filtered(options::OPT_m_x86_Features_Group,
260 options::OPT_mgeneral_regs_only)) {
261 StringRef Name = A->getOption().getName();
262 A->claim();
264 // Skip over "-m".
265 assert(Name.startswith("m") && "Invalid feature name.");
266 Name = Name.substr(1);
268 // Replace -mgeneral-regs-only with -x87, -mmx, -sse
269 if (A->getOption().getID() == options::OPT_mgeneral_regs_only) {
270 Features.insert(Features.end(), {"-x87", "-mmx", "-sse"});
271 continue;
274 StringRef AVX512Name = Name;
275 bool IsNegative = Name.startswith("no-");
276 if (IsNegative)
277 Name = Name.substr(3);
278 if (HasAVX10 && (Name.startswith("avx512") || Name == "evex512")) {
279 D.Diag(diag::warn_drv_unused_argument) << AVX512Name;
280 continue;
282 Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
285 // Enable/disable straight line speculation hardening.
286 if (Arg *A = Args.getLastArg(options::OPT_mharden_sls_EQ)) {
287 StringRef Scope = A->getValue();
288 if (Scope == "all") {
289 Features.push_back("+harden-sls-ijmp");
290 Features.push_back("+harden-sls-ret");
291 } else if (Scope == "return") {
292 Features.push_back("+harden-sls-ret");
293 } else if (Scope == "indirect-jmp") {
294 Features.push_back("+harden-sls-ijmp");
295 } else if (Scope != "none") {
296 D.Diag(diag::err_drv_unsupported_option_argument)
297 << A->getSpelling() << Scope;
301 // -mno-gather, -mno-scatter support
302 if (Args.hasArg(options::OPT_mno_gather))
303 Features.push_back("+prefer-no-gather");
304 if (Args.hasArg(options::OPT_mno_scatter))
305 Features.push_back("+prefer-no-scatter");