1 //===--- AArch64.cpp - AArch64 (not ARM) 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 "clang/Driver/Driver.h"
11 #include "clang/Driver/DriverDiagnostic.h"
12 #include "clang/Driver/Options.h"
13 #include "llvm/Option/ArgList.h"
14 #include "llvm/Support/TargetParser.h"
15 #include "llvm/Support/Host.h"
17 using namespace clang::driver;
18 using namespace clang::driver::tools;
19 using namespace clang;
20 using namespace llvm::opt;
22 /// \returns true if the given triple can determine the default CPU type even
23 /// if -arch is not specified.
24 static bool isCPUDeterminedByTriple(const llvm::Triple &Triple) {
25 return Triple.isOSDarwin();
28 /// getAArch64TargetCPU - Get the (LLVM) name of the AArch64 cpu we are
29 /// targeting. Set \p A to the Arg corresponding to the -mcpu argument if it is
30 /// provided, or to nullptr otherwise.
31 std::string aarch64::getAArch64TargetCPU(const ArgList &Args,
32 const llvm::Triple &Triple, Arg *&A) {
34 // If we have -mcpu, use that.
35 if ((A = Args.getLastArg(options::OPT_mcpu_EQ))) {
36 StringRef Mcpu = A->getValue();
37 CPU = Mcpu.split("+").first.lower();
40 // Handle CPU name is 'native'.
42 return std::string(llvm::sys::getHostCPUName());
46 // Make sure we pick the appropriate Apple CPU if -arch is used or when
47 // targetting a Darwin OS.
48 if (Args.getLastArg(options::OPT_arch) || Triple.isOSDarwin())
49 return Triple.getArch() == llvm::Triple::aarch64_32 ? "apple-s4"
55 // Decode AArch64 features from string like +[no]featureA+[no]featureB+...
56 static bool DecodeAArch64Features(const Driver &D, StringRef text,
57 std::vector<StringRef> &Features,
58 llvm::AArch64::ArchKind ArchKind) {
59 SmallVector<StringRef, 8> Split;
60 text.split(Split, StringRef("+"), -1, false);
62 for (StringRef Feature : Split) {
63 StringRef FeatureName = llvm::AArch64::getArchExtFeature(Feature);
64 if (!FeatureName.empty())
65 Features.push_back(FeatureName);
66 else if (Feature == "neon" || Feature == "noneon")
67 D.Diag(clang::diag::err_drv_no_neon_modifier);
71 // +sve implies +f32mm if the base architecture is v8.6A
72 // it isn't the case in general that sve implies both f64mm and f32mm
73 if ((ArchKind == llvm::AArch64::ArchKind::ARMV8_6A) && Feature == "sve")
74 Features.push_back("+f32mm");
79 // Check if the CPU name and feature modifiers in -mcpu are legal. If yes,
80 // decode CPU and feature.
81 static bool DecodeAArch64Mcpu(const Driver &D, StringRef Mcpu, StringRef &CPU,
82 std::vector<StringRef> &Features) {
83 std::pair<StringRef, StringRef> Split = Mcpu.split("+");
85 llvm::AArch64::ArchKind ArchKind = llvm::AArch64::ArchKind::ARMV8A;
88 CPU = llvm::sys::getHostCPUName();
90 if (CPU == "generic") {
91 Features.push_back("+neon");
93 ArchKind = llvm::AArch64::parseCPUArch(CPU);
94 if (!llvm::AArch64::getArchFeatures(ArchKind, Features))
97 unsigned Extension = llvm::AArch64::getDefaultExtensions(CPU, ArchKind);
98 if (!llvm::AArch64::getExtensionFeatures(Extension, Features))
102 if (Split.second.size() &&
103 !DecodeAArch64Features(D, Split.second, Features, ArchKind))
110 getAArch64ArchFeaturesFromMarch(const Driver &D, StringRef March,
112 std::vector<StringRef> &Features) {
113 std::string MarchLowerCase = March.lower();
114 std::pair<StringRef, StringRef> Split = StringRef(MarchLowerCase).split("+");
116 llvm::AArch64::ArchKind ArchKind = llvm::AArch64::parseArch(Split.first);
117 if (ArchKind == llvm::AArch64::ArchKind::INVALID ||
118 !llvm::AArch64::getArchFeatures(ArchKind, Features) ||
119 (Split.second.size() &&
120 !DecodeAArch64Features(D, Split.second, Features, ArchKind)))
127 getAArch64ArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu,
129 std::vector<StringRef> &Features) {
131 std::string McpuLowerCase = Mcpu.lower();
132 if (!DecodeAArch64Mcpu(D, McpuLowerCase, CPU, Features))
139 getAArch64MicroArchFeaturesFromMtune(const Driver &D, StringRef Mtune,
141 std::vector<StringRef> &Features) {
142 std::string MtuneLowerCase = Mtune.lower();
143 // Check CPU name is valid
144 std::vector<StringRef> MtuneFeatures;
146 if (!DecodeAArch64Mcpu(D, MtuneLowerCase, Tune, MtuneFeatures))
149 // Handle CPU name is 'native'.
150 if (MtuneLowerCase == "native")
151 MtuneLowerCase = std::string(llvm::sys::getHostCPUName());
152 if (MtuneLowerCase == "cyclone" ||
153 StringRef(MtuneLowerCase).startswith("apple")) {
154 Features.push_back("+zcm");
155 Features.push_back("+zcz");
161 getAArch64MicroArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu,
163 std::vector<StringRef> &Features) {
165 std::vector<StringRef> DecodedFeature;
166 std::string McpuLowerCase = Mcpu.lower();
167 if (!DecodeAArch64Mcpu(D, McpuLowerCase, CPU, DecodedFeature))
170 return getAArch64MicroArchFeaturesFromMtune(D, CPU, Args, Features);
173 void aarch64::getAArch64TargetFeatures(const Driver &D,
174 const llvm::Triple &Triple,
176 std::vector<StringRef> &Features) {
179 // Enable NEON by default.
180 Features.push_back("+neon");
181 if ((A = Args.getLastArg(options::OPT_march_EQ)))
182 success = getAArch64ArchFeaturesFromMarch(D, A->getValue(), Args, Features);
183 else if ((A = Args.getLastArg(options::OPT_mcpu_EQ)))
184 success = getAArch64ArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
185 else if (Args.hasArg(options::OPT_arch) || isCPUDeterminedByTriple(Triple))
186 success = getAArch64ArchFeaturesFromMcpu(
187 D, getAArch64TargetCPU(Args, Triple, A), Args, Features);
189 if (success && (A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)))
191 getAArch64MicroArchFeaturesFromMtune(D, A->getValue(), Args, Features);
192 else if (success && (A = Args.getLastArg(options::OPT_mcpu_EQ)))
194 getAArch64MicroArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
196 (Args.hasArg(options::OPT_arch) || isCPUDeterminedByTriple(Triple)))
197 success = getAArch64MicroArchFeaturesFromMcpu(
198 D, getAArch64TargetCPU(Args, Triple, A), Args, Features);
201 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
203 if (Args.getLastArg(options::OPT_mgeneral_regs_only)) {
204 Features.push_back("-fp-armv8");
205 Features.push_back("-crypto");
206 Features.push_back("-neon");
209 if (Arg *A = Args.getLastArg(options::OPT_mtp_mode_EQ)) {
210 StringRef Mtp = A->getValue();
212 Features.push_back("+tpidr-el3");
213 else if (Mtp == "el2")
214 Features.push_back("+tpidr-el2");
215 else if (Mtp == "el1")
216 Features.push_back("+tpidr-el1");
217 else if (Mtp != "el0")
218 D.Diag(diag::err_drv_invalid_mtp) << A->getAsString(Args);
221 // Enable/disable straight line speculation hardening.
222 if (Arg *A = Args.getLastArg(options::OPT_mharden_sls_EQ)) {
223 StringRef Scope = A->getValue();
224 bool EnableRetBr = false;
225 bool EnableBlr = false;
226 if (Scope != "none" && Scope != "all") {
227 SmallVector<StringRef, 4> Opts;
228 Scope.split(Opts, ",");
229 for (auto Opt : Opts) {
231 if (Opt == "retbr") {
239 D.Diag(diag::err_invalid_sls_hardening)
240 << Scope << A->getAsString(Args);
243 } else if (Scope == "all") {
249 Features.push_back("+harden-sls-retbr");
251 Features.push_back("+harden-sls-blr");
255 if (Arg *A = Args.getLastArg(options::OPT_mcrc, options::OPT_mnocrc)) {
256 if (A->getOption().matches(options::OPT_mcrc))
257 Features.push_back("+crc");
259 Features.push_back("-crc");
262 // Handle (arch-dependent) fp16fml/fullfp16 relationship.
263 // FIXME: this fp16fml option handling will be reimplemented after the
264 // TargetParser rewrite.
265 const auto ItRNoFullFP16 = std::find(Features.rbegin(), Features.rend(), "-fullfp16");
266 const auto ItRFP16FML = std::find(Features.rbegin(), Features.rend(), "+fp16fml");
267 if (llvm::is_contained(Features, "+v8.4a")) {
268 const auto ItRFullFP16 = std::find(Features.rbegin(), Features.rend(), "+fullfp16");
269 if (ItRFullFP16 < ItRNoFullFP16 && ItRFullFP16 < ItRFP16FML) {
270 // Only entangled feature that can be to the right of this +fullfp16 is -fp16fml.
271 // Only append the +fp16fml if there is no -fp16fml after the +fullfp16.
272 if (std::find(Features.rbegin(), ItRFullFP16, "-fp16fml") == ItRFullFP16)
273 Features.push_back("+fp16fml");
276 goto fp16_fml_fallthrough;
278 fp16_fml_fallthrough:
279 // In both of these cases, putting the 'other' feature on the end of the vector will
280 // result in the same effect as placing it immediately after the current feature.
281 if (ItRNoFullFP16 < ItRFP16FML)
282 Features.push_back("-fp16fml");
283 else if (ItRNoFullFP16 > ItRFP16FML)
284 Features.push_back("+fullfp16");
287 // FIXME: this needs reimplementation too after the TargetParser rewrite
289 // Context sensitive meaning of Crypto:
290 // 1) For Arch >= ARMv8.4a: crypto = sm4 + sha3 + sha2 + aes
291 // 2) For Arch <= ARMv8.3a: crypto = sha2 + aes
292 const auto ItBegin = Features.begin();
293 const auto ItEnd = Features.end();
294 const auto ItRBegin = Features.rbegin();
295 const auto ItREnd = Features.rend();
296 const auto ItRCrypto = std::find(ItRBegin, ItREnd, "+crypto");
297 const auto ItRNoCrypto = std::find(ItRBegin, ItREnd, "-crypto");
298 const auto HasCrypto = ItRCrypto != ItREnd;
299 const auto HasNoCrypto = ItRNoCrypto != ItREnd;
300 const ptrdiff_t PosCrypto = ItRCrypto - ItRBegin;
301 const ptrdiff_t PosNoCrypto = ItRNoCrypto - ItRBegin;
303 bool NoCrypto = false;
304 if (HasCrypto && HasNoCrypto) {
305 if (PosNoCrypto < PosCrypto)
309 if (std::find(ItBegin, ItEnd, "+v8.4a") != ItEnd) {
310 if (HasCrypto && !NoCrypto) {
311 // Check if we have NOT disabled an algorithm with something like:
312 // +crypto, -algorithm
313 // And if "-algorithm" does not occur, we enable that crypto algorithm.
314 const bool HasSM4 = (std::find(ItBegin, ItEnd, "-sm4") == ItEnd);
315 const bool HasSHA3 = (std::find(ItBegin, ItEnd, "-sha3") == ItEnd);
316 const bool HasSHA2 = (std::find(ItBegin, ItEnd, "-sha2") == ItEnd);
317 const bool HasAES = (std::find(ItBegin, ItEnd, "-aes") == ItEnd);
319 Features.push_back("+sm4");
321 Features.push_back("+sha3");
323 Features.push_back("+sha2");
325 Features.push_back("+aes");
326 } else if (HasNoCrypto) {
327 // Check if we have NOT enabled a crypto algorithm with something like:
328 // -crypto, +algorithm
329 // And if "+algorithm" does not occur, we disable that crypto algorithm.
330 const bool HasSM4 = (std::find(ItBegin, ItEnd, "+sm4") != ItEnd);
331 const bool HasSHA3 = (std::find(ItBegin, ItEnd, "+sha3") != ItEnd);
332 const bool HasSHA2 = (std::find(ItBegin, ItEnd, "+sha2") != ItEnd);
333 const bool HasAES = (std::find(ItBegin, ItEnd, "+aes") != ItEnd);
335 Features.push_back("-sm4");
337 Features.push_back("-sha3");
339 Features.push_back("-sha2");
341 Features.push_back("-aes");
344 if (HasCrypto && !NoCrypto) {
345 const bool HasSHA2 = (std::find(ItBegin, ItEnd, "-sha2") == ItEnd);
346 const bool HasAES = (std::find(ItBegin, ItEnd, "-aes") == ItEnd);
348 Features.push_back("+sha2");
350 Features.push_back("+aes");
351 } else if (HasNoCrypto) {
352 const bool HasSHA2 = (std::find(ItBegin, ItEnd, "+sha2") != ItEnd);
353 const bool HasAES = (std::find(ItBegin, ItEnd, "+aes") != ItEnd);
354 const bool HasV82a = (std::find(ItBegin, ItEnd, "+v8.2a") != ItEnd);
355 const bool HasV83a = (std::find(ItBegin, ItEnd, "+v8.3a") != ItEnd);
356 const bool HasV84a = (std::find(ItBegin, ItEnd, "+v8.4a") != ItEnd);
358 Features.push_back("-sha2");
360 Features.push_back("-aes");
361 if (HasV82a || HasV83a || HasV84a) {
362 Features.push_back("-sm4");
363 Features.push_back("-sha3");
368 auto V8_6Pos = llvm::find(Features, "+v8.6a");
369 if (V8_6Pos != std::end(Features))
370 V8_6Pos = Features.insert(std::next(V8_6Pos), {"+i8mm", "+bf16"});
372 if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access,
373 options::OPT_munaligned_access)) {
374 if (A->getOption().matches(options::OPT_mno_unaligned_access))
375 Features.push_back("+strict-align");
376 } else if (Triple.isOSOpenBSD())
377 Features.push_back("+strict-align");
379 if (Args.hasArg(options::OPT_ffixed_x1))
380 Features.push_back("+reserve-x1");
382 if (Args.hasArg(options::OPT_ffixed_x2))
383 Features.push_back("+reserve-x2");
385 if (Args.hasArg(options::OPT_ffixed_x3))
386 Features.push_back("+reserve-x3");
388 if (Args.hasArg(options::OPT_ffixed_x4))
389 Features.push_back("+reserve-x4");
391 if (Args.hasArg(options::OPT_ffixed_x5))
392 Features.push_back("+reserve-x5");
394 if (Args.hasArg(options::OPT_ffixed_x6))
395 Features.push_back("+reserve-x6");
397 if (Args.hasArg(options::OPT_ffixed_x7))
398 Features.push_back("+reserve-x7");
400 if (Args.hasArg(options::OPT_ffixed_x9))
401 Features.push_back("+reserve-x9");
403 if (Args.hasArg(options::OPT_ffixed_x10))
404 Features.push_back("+reserve-x10");
406 if (Args.hasArg(options::OPT_ffixed_x11))
407 Features.push_back("+reserve-x11");
409 if (Args.hasArg(options::OPT_ffixed_x12))
410 Features.push_back("+reserve-x12");
412 if (Args.hasArg(options::OPT_ffixed_x13))
413 Features.push_back("+reserve-x13");
415 if (Args.hasArg(options::OPT_ffixed_x14))
416 Features.push_back("+reserve-x14");
418 if (Args.hasArg(options::OPT_ffixed_x15))
419 Features.push_back("+reserve-x15");
421 if (Args.hasArg(options::OPT_ffixed_x18))
422 Features.push_back("+reserve-x18");
424 if (Args.hasArg(options::OPT_ffixed_x20))
425 Features.push_back("+reserve-x20");
427 if (Args.hasArg(options::OPT_ffixed_x21))
428 Features.push_back("+reserve-x21");
430 if (Args.hasArg(options::OPT_ffixed_x22))
431 Features.push_back("+reserve-x22");
433 if (Args.hasArg(options::OPT_ffixed_x23))
434 Features.push_back("+reserve-x23");
436 if (Args.hasArg(options::OPT_ffixed_x24))
437 Features.push_back("+reserve-x24");
439 if (Args.hasArg(options::OPT_ffixed_x25))
440 Features.push_back("+reserve-x25");
442 if (Args.hasArg(options::OPT_ffixed_x26))
443 Features.push_back("+reserve-x26");
445 if (Args.hasArg(options::OPT_ffixed_x27))
446 Features.push_back("+reserve-x27");
448 if (Args.hasArg(options::OPT_ffixed_x28))
449 Features.push_back("+reserve-x28");
451 if (Args.hasArg(options::OPT_ffixed_x30))
452 Features.push_back("+reserve-x30");
454 if (Args.hasArg(options::OPT_fcall_saved_x8))
455 Features.push_back("+call-saved-x8");
457 if (Args.hasArg(options::OPT_fcall_saved_x9))
458 Features.push_back("+call-saved-x9");
460 if (Args.hasArg(options::OPT_fcall_saved_x10))
461 Features.push_back("+call-saved-x10");
463 if (Args.hasArg(options::OPT_fcall_saved_x11))
464 Features.push_back("+call-saved-x11");
466 if (Args.hasArg(options::OPT_fcall_saved_x12))
467 Features.push_back("+call-saved-x12");
469 if (Args.hasArg(options::OPT_fcall_saved_x13))
470 Features.push_back("+call-saved-x13");
472 if (Args.hasArg(options::OPT_fcall_saved_x14))
473 Features.push_back("+call-saved-x14");
475 if (Args.hasArg(options::OPT_fcall_saved_x15))
476 Features.push_back("+call-saved-x15");
478 if (Args.hasArg(options::OPT_fcall_saved_x18))
479 Features.push_back("+call-saved-x18");
481 if (Args.hasArg(options::OPT_mno_neg_immediates))
482 Features.push_back("+no-neg-immediates");