1 //===--- AArch64.cpp - AArch64 (not ARM) 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 "AArch64.h"
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"
16
17 using namespace clang::driver;
18 using namespace clang::driver::tools;
19 using namespace clang;
20 using namespace llvm::opt;
21
22 /// \returns true if the given triple can determine the default CPU type even
23 /// if -arch is not specified.
isCPUDeterminedByTriple(const llvm::Triple & Triple)24 static bool isCPUDeterminedByTriple(const llvm::Triple &Triple) {
25 return Triple.isOSDarwin();
26 }
27
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.
getAArch64TargetCPU(const ArgList & Args,const llvm::Triple & Triple,Arg * & A)31 std::string aarch64::getAArch64TargetCPU(const ArgList &Args,
32 const llvm::Triple &Triple, Arg *&A) {
33 std::string CPU;
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();
38 }
39
40 // Handle CPU name is 'native'.
41 if (CPU == "native")
42 return std::string(llvm::sys::getHostCPUName());
43
44 // arm64e requires v8.3a and only runs on apple-a12 and later CPUs.
45 if (Triple.isArm64e())
46 return "apple-a12";
47
48 if (CPU.size())
49 return CPU;
50
51 if (Triple.isTargetMachineMac() &&
52 Triple.getArch() == llvm::Triple::aarch64) {
53 // Apple Silicon macs default to A12 CPUs.
54 return "apple-a12";
55 }
56
57 // Make sure we pick the appropriate Apple CPU if -arch is used or when
58 // targetting a Darwin OS.
59 if (Args.getLastArg(options::OPT_arch) || Triple.isOSDarwin())
60 return Triple.getArch() == llvm::Triple::aarch64_32 ? "apple-s4"
61 : "apple-a7";
62
63 return "generic";
64 }
65
66 // Decode AArch64 features from string like +[no]featureA+[no]featureB+...
DecodeAArch64Features(const Driver & D,StringRef text,std::vector<StringRef> & Features,llvm::AArch64::ArchKind ArchKind)67 static bool DecodeAArch64Features(const Driver &D, StringRef text,
68 std::vector<StringRef> &Features,
69 llvm::AArch64::ArchKind ArchKind) {
70 SmallVector<StringRef, 8> Split;
71 text.split(Split, StringRef("+"), -1, false);
72
73 for (StringRef Feature : Split) {
74 StringRef FeatureName = llvm::AArch64::getArchExtFeature(Feature);
75 if (!FeatureName.empty())
76 Features.push_back(FeatureName);
77 else if (Feature == "neon" || Feature == "noneon")
78 D.Diag(clang::diag::err_drv_no_neon_modifier);
79 else
80 return false;
81
82 // +sve implies +f32mm if the base architecture is v8.6A or v8.7A
83 // it isn't the case in general that sve implies both f64mm and f32mm
84 if ((ArchKind == llvm::AArch64::ArchKind::ARMV8_6A ||
85 ArchKind == llvm::AArch64::ArchKind::ARMV8_7A) && Feature == "sve")
86 Features.push_back("+f32mm");
87 }
88 return true;
89 }
90
91 // Check if the CPU name and feature modifiers in -mcpu are legal. If yes,
92 // decode CPU and feature.
DecodeAArch64Mcpu(const Driver & D,StringRef Mcpu,StringRef & CPU,std::vector<StringRef> & Features)93 static bool DecodeAArch64Mcpu(const Driver &D, StringRef Mcpu, StringRef &CPU,
94 std::vector<StringRef> &Features) {
95 std::pair<StringRef, StringRef> Split = Mcpu.split("+");
96 CPU = Split.first;
97 llvm::AArch64::ArchKind ArchKind = llvm::AArch64::ArchKind::ARMV8A;
98
99 if (CPU == "native")
100 CPU = llvm::sys::getHostCPUName();
101
102 if (CPU == "generic") {
103 Features.push_back("+neon");
104 } else {
105 ArchKind = llvm::AArch64::parseCPUArch(CPU);
106 if (!llvm::AArch64::getArchFeatures(ArchKind, Features))
107 return false;
108
109 uint64_t Extension = llvm::AArch64::getDefaultExtensions(CPU, ArchKind);
110 if (!llvm::AArch64::getExtensionFeatures(Extension, Features))
111 return false;
112 }
113
114 if (Split.second.size() &&
115 !DecodeAArch64Features(D, Split.second, Features, ArchKind))
116 return false;
117
118 return true;
119 }
120
121 static bool
getAArch64ArchFeaturesFromMarch(const Driver & D,StringRef March,const ArgList & Args,std::vector<StringRef> & Features)122 getAArch64ArchFeaturesFromMarch(const Driver &D, StringRef March,
123 const ArgList &Args,
124 std::vector<StringRef> &Features) {
125 std::string MarchLowerCase = March.lower();
126 std::pair<StringRef, StringRef> Split = StringRef(MarchLowerCase).split("+");
127
128 llvm::AArch64::ArchKind ArchKind = llvm::AArch64::parseArch(Split.first);
129 if (ArchKind == llvm::AArch64::ArchKind::INVALID ||
130 !llvm::AArch64::getArchFeatures(ArchKind, Features) ||
131 (Split.second.size() &&
132 !DecodeAArch64Features(D, Split.second, Features, ArchKind)))
133 return false;
134
135 return true;
136 }
137
138 static bool
getAArch64ArchFeaturesFromMcpu(const Driver & D,StringRef Mcpu,const ArgList & Args,std::vector<StringRef> & Features)139 getAArch64ArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu,
140 const ArgList &Args,
141 std::vector<StringRef> &Features) {
142 StringRef CPU;
143 std::string McpuLowerCase = Mcpu.lower();
144 if (!DecodeAArch64Mcpu(D, McpuLowerCase, CPU, Features))
145 return false;
146
147 return true;
148 }
149
150 static bool
getAArch64MicroArchFeaturesFromMtune(const Driver & D,StringRef Mtune,const ArgList & Args,std::vector<StringRef> & Features)151 getAArch64MicroArchFeaturesFromMtune(const Driver &D, StringRef Mtune,
152 const ArgList &Args,
153 std::vector<StringRef> &Features) {
154 std::string MtuneLowerCase = Mtune.lower();
155 // Check CPU name is valid
156 std::vector<StringRef> MtuneFeatures;
157 StringRef Tune;
158 if (!DecodeAArch64Mcpu(D, MtuneLowerCase, Tune, MtuneFeatures))
159 return false;
160
161 // Handle CPU name is 'native'.
162 if (MtuneLowerCase == "native")
163 MtuneLowerCase = std::string(llvm::sys::getHostCPUName());
164 if (MtuneLowerCase == "cyclone" ||
165 StringRef(MtuneLowerCase).startswith("apple")) {
166 Features.push_back("+zcm");
167 Features.push_back("+zcz");
168 }
169 return true;
170 }
171
172 static bool
getAArch64MicroArchFeaturesFromMcpu(const Driver & D,StringRef Mcpu,const ArgList & Args,std::vector<StringRef> & Features)173 getAArch64MicroArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu,
174 const ArgList &Args,
175 std::vector<StringRef> &Features) {
176 StringRef CPU;
177 std::vector<StringRef> DecodedFeature;
178 std::string McpuLowerCase = Mcpu.lower();
179 if (!DecodeAArch64Mcpu(D, McpuLowerCase, CPU, DecodedFeature))
180 return false;
181
182 return getAArch64MicroArchFeaturesFromMtune(D, CPU, Args, Features);
183 }
184
getAArch64TargetFeatures(const Driver & D,const llvm::Triple & Triple,const ArgList & Args,std::vector<StringRef> & Features)185 void aarch64::getAArch64TargetFeatures(const Driver &D,
186 const llvm::Triple &Triple,
187 const ArgList &Args,
188 std::vector<StringRef> &Features) {
189 Arg *A;
190 bool success = true;
191 // Enable NEON by default.
192 Features.push_back("+neon");
193 if ((A = Args.getLastArg(options::OPT_march_EQ)))
194 success = getAArch64ArchFeaturesFromMarch(D, A->getValue(), Args, Features);
195 else if ((A = Args.getLastArg(options::OPT_mcpu_EQ)))
196 success = getAArch64ArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
197 else if (Args.hasArg(options::OPT_arch) || isCPUDeterminedByTriple(Triple))
198 success = getAArch64ArchFeaturesFromMcpu(
199 D, getAArch64TargetCPU(Args, Triple, A), Args, Features);
200
201 if (success && (A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)))
202 success =
203 getAArch64MicroArchFeaturesFromMtune(D, A->getValue(), Args, Features);
204 else if (success && (A = Args.getLastArg(options::OPT_mcpu_EQ)))
205 success =
206 getAArch64MicroArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
207 else if (success &&
208 (Args.hasArg(options::OPT_arch) || isCPUDeterminedByTriple(Triple)))
209 success = getAArch64MicroArchFeaturesFromMcpu(
210 D, getAArch64TargetCPU(Args, Triple, A), Args, Features);
211
212 if (!success)
213 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
214
215 if (Args.getLastArg(options::OPT_mgeneral_regs_only)) {
216 Features.push_back("-fp-armv8");
217 Features.push_back("-crypto");
218 Features.push_back("-neon");
219 }
220
221 if (Arg *A = Args.getLastArg(options::OPT_mtp_mode_EQ)) {
222 StringRef Mtp = A->getValue();
223 if (Mtp == "el3")
224 Features.push_back("+tpidr-el3");
225 else if (Mtp == "el2")
226 Features.push_back("+tpidr-el2");
227 else if (Mtp == "el1")
228 Features.push_back("+tpidr-el1");
229 else if (Mtp != "el0")
230 D.Diag(diag::err_drv_invalid_mtp) << A->getAsString(Args);
231 }
232
233 // Enable/disable straight line speculation hardening.
234 if (Arg *A = Args.getLastArg(options::OPT_mharden_sls_EQ)) {
235 StringRef Scope = A->getValue();
236 bool EnableRetBr = false;
237 bool EnableBlr = false;
238 if (Scope != "none" && Scope != "all") {
239 SmallVector<StringRef, 4> Opts;
240 Scope.split(Opts, ",");
241 for (auto Opt : Opts) {
242 Opt = Opt.trim();
243 if (Opt == "retbr") {
244 EnableRetBr = true;
245 continue;
246 }
247 if (Opt == "blr") {
248 EnableBlr = true;
249 continue;
250 }
251 D.Diag(diag::err_invalid_sls_hardening)
252 << Scope << A->getAsString(Args);
253 break;
254 }
255 } else if (Scope == "all") {
256 EnableRetBr = true;
257 EnableBlr = true;
258 }
259
260 if (EnableRetBr)
261 Features.push_back("+harden-sls-retbr");
262 if (EnableBlr)
263 Features.push_back("+harden-sls-blr");
264 }
265
266 // En/disable crc
267 if (Arg *A = Args.getLastArg(options::OPT_mcrc, options::OPT_mnocrc)) {
268 if (A->getOption().matches(options::OPT_mcrc))
269 Features.push_back("+crc");
270 else
271 Features.push_back("-crc");
272 }
273
274 // Handle (arch-dependent) fp16fml/fullfp16 relationship.
275 // FIXME: this fp16fml option handling will be reimplemented after the
276 // TargetParser rewrite.
277 const auto ItRNoFullFP16 = std::find(Features.rbegin(), Features.rend(), "-fullfp16");
278 const auto ItRFP16FML = std::find(Features.rbegin(), Features.rend(), "+fp16fml");
279 if (llvm::is_contained(Features, "+v8.4a")) {
280 const auto ItRFullFP16 = std::find(Features.rbegin(), Features.rend(), "+fullfp16");
281 if (ItRFullFP16 < ItRNoFullFP16 && ItRFullFP16 < ItRFP16FML) {
282 // Only entangled feature that can be to the right of this +fullfp16 is -fp16fml.
283 // Only append the +fp16fml if there is no -fp16fml after the +fullfp16.
284 if (std::find(Features.rbegin(), ItRFullFP16, "-fp16fml") == ItRFullFP16)
285 Features.push_back("+fp16fml");
286 }
287 else
288 goto fp16_fml_fallthrough;
289 } else {
290 fp16_fml_fallthrough:
291 // In both of these cases, putting the 'other' feature on the end of the vector will
292 // result in the same effect as placing it immediately after the current feature.
293 if (ItRNoFullFP16 < ItRFP16FML)
294 Features.push_back("-fp16fml");
295 else if (ItRNoFullFP16 > ItRFP16FML)
296 Features.push_back("+fullfp16");
297 }
298
299 // FIXME: this needs reimplementation too after the TargetParser rewrite
300 //
301 // Context sensitive meaning of Crypto:
302 // 1) For Arch >= ARMv8.4a: crypto = sm4 + sha3 + sha2 + aes
303 // 2) For Arch <= ARMv8.3a: crypto = sha2 + aes
304 const auto ItBegin = Features.begin();
305 const auto ItEnd = Features.end();
306 const auto ItRBegin = Features.rbegin();
307 const auto ItREnd = Features.rend();
308 const auto ItRCrypto = std::find(ItRBegin, ItREnd, "+crypto");
309 const auto ItRNoCrypto = std::find(ItRBegin, ItREnd, "-crypto");
310 const auto HasCrypto = ItRCrypto != ItREnd;
311 const auto HasNoCrypto = ItRNoCrypto != ItREnd;
312 const ptrdiff_t PosCrypto = ItRCrypto - ItRBegin;
313 const ptrdiff_t PosNoCrypto = ItRNoCrypto - ItRBegin;
314
315 bool NoCrypto = false;
316 if (HasCrypto && HasNoCrypto) {
317 if (PosNoCrypto < PosCrypto)
318 NoCrypto = true;
319 }
320
321 if (std::find(ItBegin, ItEnd, "+v8.4a") != ItEnd) {
322 if (HasCrypto && !NoCrypto) {
323 // Check if we have NOT disabled an algorithm with something like:
324 // +crypto, -algorithm
325 // And if "-algorithm" does not occur, we enable that crypto algorithm.
326 const bool HasSM4 = (std::find(ItBegin, ItEnd, "-sm4") == ItEnd);
327 const bool HasSHA3 = (std::find(ItBegin, ItEnd, "-sha3") == ItEnd);
328 const bool HasSHA2 = (std::find(ItBegin, ItEnd, "-sha2") == ItEnd);
329 const bool HasAES = (std::find(ItBegin, ItEnd, "-aes") == ItEnd);
330 if (HasSM4)
331 Features.push_back("+sm4");
332 if (HasSHA3)
333 Features.push_back("+sha3");
334 if (HasSHA2)
335 Features.push_back("+sha2");
336 if (HasAES)
337 Features.push_back("+aes");
338 } else if (HasNoCrypto) {
339 // Check if we have NOT enabled a crypto algorithm with something like:
340 // -crypto, +algorithm
341 // And if "+algorithm" does not occur, we disable that crypto algorithm.
342 const bool HasSM4 = (std::find(ItBegin, ItEnd, "+sm4") != ItEnd);
343 const bool HasSHA3 = (std::find(ItBegin, ItEnd, "+sha3") != ItEnd);
344 const bool HasSHA2 = (std::find(ItBegin, ItEnd, "+sha2") != ItEnd);
345 const bool HasAES = (std::find(ItBegin, ItEnd, "+aes") != ItEnd);
346 if (!HasSM4)
347 Features.push_back("-sm4");
348 if (!HasSHA3)
349 Features.push_back("-sha3");
350 if (!HasSHA2)
351 Features.push_back("-sha2");
352 if (!HasAES)
353 Features.push_back("-aes");
354 }
355 } else {
356 if (HasCrypto && !NoCrypto) {
357 const bool HasSHA2 = (std::find(ItBegin, ItEnd, "-sha2") == ItEnd);
358 const bool HasAES = (std::find(ItBegin, ItEnd, "-aes") == ItEnd);
359 if (HasSHA2)
360 Features.push_back("+sha2");
361 if (HasAES)
362 Features.push_back("+aes");
363 } else if (HasNoCrypto) {
364 const bool HasSHA2 = (std::find(ItBegin, ItEnd, "+sha2") != ItEnd);
365 const bool HasAES = (std::find(ItBegin, ItEnd, "+aes") != ItEnd);
366 const bool HasV82a = (std::find(ItBegin, ItEnd, "+v8.2a") != ItEnd);
367 const bool HasV83a = (std::find(ItBegin, ItEnd, "+v8.3a") != ItEnd);
368 const bool HasV84a = (std::find(ItBegin, ItEnd, "+v8.4a") != ItEnd);
369 if (!HasSHA2)
370 Features.push_back("-sha2");
371 if (!HasAES)
372 Features.push_back("-aes");
373 if (HasV82a || HasV83a || HasV84a) {
374 Features.push_back("-sm4");
375 Features.push_back("-sha3");
376 }
377 }
378 }
379
380 auto V8_6Pos = llvm::find(Features, "+v8.6a");
381 if (V8_6Pos != std::end(Features))
382 V8_6Pos = Features.insert(std::next(V8_6Pos), {"+i8mm", "+bf16"});
383
384 if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access,
385 options::OPT_munaligned_access)) {
386 if (A->getOption().matches(options::OPT_mno_unaligned_access))
387 Features.push_back("+strict-align");
388 } else if (Triple.isOSOpenBSD())
389 Features.push_back("+strict-align");
390
391 if (Args.hasArg(options::OPT_ffixed_x1))
392 Features.push_back("+reserve-x1");
393
394 if (Args.hasArg(options::OPT_ffixed_x2))
395 Features.push_back("+reserve-x2");
396
397 if (Args.hasArg(options::OPT_ffixed_x3))
398 Features.push_back("+reserve-x3");
399
400 if (Args.hasArg(options::OPT_ffixed_x4))
401 Features.push_back("+reserve-x4");
402
403 if (Args.hasArg(options::OPT_ffixed_x5))
404 Features.push_back("+reserve-x5");
405
406 if (Args.hasArg(options::OPT_ffixed_x6))
407 Features.push_back("+reserve-x6");
408
409 if (Args.hasArg(options::OPT_ffixed_x7))
410 Features.push_back("+reserve-x7");
411
412 if (Args.hasArg(options::OPT_ffixed_x9))
413 Features.push_back("+reserve-x9");
414
415 if (Args.hasArg(options::OPT_ffixed_x10))
416 Features.push_back("+reserve-x10");
417
418 if (Args.hasArg(options::OPT_ffixed_x11))
419 Features.push_back("+reserve-x11");
420
421 if (Args.hasArg(options::OPT_ffixed_x12))
422 Features.push_back("+reserve-x12");
423
424 if (Args.hasArg(options::OPT_ffixed_x13))
425 Features.push_back("+reserve-x13");
426
427 if (Args.hasArg(options::OPT_ffixed_x14))
428 Features.push_back("+reserve-x14");
429
430 if (Args.hasArg(options::OPT_ffixed_x15))
431 Features.push_back("+reserve-x15");
432
433 if (Args.hasArg(options::OPT_ffixed_x18))
434 Features.push_back("+reserve-x18");
435
436 if (Args.hasArg(options::OPT_ffixed_x20))
437 Features.push_back("+reserve-x20");
438
439 if (Args.hasArg(options::OPT_ffixed_x21))
440 Features.push_back("+reserve-x21");
441
442 if (Args.hasArg(options::OPT_ffixed_x22))
443 Features.push_back("+reserve-x22");
444
445 if (Args.hasArg(options::OPT_ffixed_x23))
446 Features.push_back("+reserve-x23");
447
448 if (Args.hasArg(options::OPT_ffixed_x24))
449 Features.push_back("+reserve-x24");
450
451 if (Args.hasArg(options::OPT_ffixed_x25))
452 Features.push_back("+reserve-x25");
453
454 if (Args.hasArg(options::OPT_ffixed_x26))
455 Features.push_back("+reserve-x26");
456
457 if (Args.hasArg(options::OPT_ffixed_x27))
458 Features.push_back("+reserve-x27");
459
460 if (Args.hasArg(options::OPT_ffixed_x28))
461 Features.push_back("+reserve-x28");
462
463 if (Args.hasArg(options::OPT_ffixed_x30))
464 Features.push_back("+reserve-x30");
465
466 if (Args.hasArg(options::OPT_fcall_saved_x8))
467 Features.push_back("+call-saved-x8");
468
469 if (Args.hasArg(options::OPT_fcall_saved_x9))
470 Features.push_back("+call-saved-x9");
471
472 if (Args.hasArg(options::OPT_fcall_saved_x10))
473 Features.push_back("+call-saved-x10");
474
475 if (Args.hasArg(options::OPT_fcall_saved_x11))
476 Features.push_back("+call-saved-x11");
477
478 if (Args.hasArg(options::OPT_fcall_saved_x12))
479 Features.push_back("+call-saved-x12");
480
481 if (Args.hasArg(options::OPT_fcall_saved_x13))
482 Features.push_back("+call-saved-x13");
483
484 if (Args.hasArg(options::OPT_fcall_saved_x14))
485 Features.push_back("+call-saved-x14");
486
487 if (Args.hasArg(options::OPT_fcall_saved_x15))
488 Features.push_back("+call-saved-x15");
489
490 if (Args.hasArg(options::OPT_fcall_saved_x18))
491 Features.push_back("+call-saved-x18");
492
493 if (Args.hasArg(options::OPT_mno_neg_immediates))
494 Features.push_back("+no-neg-immediates");
495 }
496