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   if (CPU.size())
45     return CPU;
46 
47   if (Triple.isTargetMachineMac() &&
48       Triple.getArch() == llvm::Triple::aarch64) {
49     // Apple Silicon macs default to M1 CPUs.
50     return "apple-m1";
51   }
52 
53   // arm64e requires v8.3a and only runs on apple-a12 and later CPUs.
54   if (Triple.isArm64e())
55     return "apple-a12";
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,bool ForAS)185 void aarch64::getAArch64TargetFeatures(const Driver &D,
186                                        const llvm::Triple &Triple,
187                                        const ArgList &Args,
188                                        std::vector<StringRef> &Features,
189                                        bool ForAS) {
190   Arg *A;
191   bool success = true;
192   // Enable NEON by default.
193   Features.push_back("+neon");
194   llvm::StringRef WaMArch = "";
195   if (ForAS)
196     for (const auto *A :
197          Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler))
198       for (StringRef Value : A->getValues())
199         if (Value.startswith("-march="))
200           WaMArch = Value.substr(7);
201   // Call getAArch64ArchFeaturesFromMarch only if "-Wa,-march=" or
202   // "-Xassembler -march" is detected. Otherwise it may return false
203   // and causes Clang to error out.
204   if (WaMArch.size())
205     success = getAArch64ArchFeaturesFromMarch(D, WaMArch, Args, Features);
206   else if ((A = Args.getLastArg(options::OPT_march_EQ)))
207     success = getAArch64ArchFeaturesFromMarch(D, A->getValue(), Args, Features);
208   else if ((A = Args.getLastArg(options::OPT_mcpu_EQ)))
209     success = getAArch64ArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
210   else if (Args.hasArg(options::OPT_arch) || isCPUDeterminedByTriple(Triple))
211     success = getAArch64ArchFeaturesFromMcpu(
212         D, getAArch64TargetCPU(Args, Triple, A), Args, Features);
213 
214   if (success && (A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)))
215     success =
216         getAArch64MicroArchFeaturesFromMtune(D, A->getValue(), Args, Features);
217   else if (success && (A = Args.getLastArg(options::OPT_mcpu_EQ)))
218     success =
219         getAArch64MicroArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
220   else if (success &&
221            (Args.hasArg(options::OPT_arch) || isCPUDeterminedByTriple(Triple)))
222     success = getAArch64MicroArchFeaturesFromMcpu(
223         D, getAArch64TargetCPU(Args, Triple, A), Args, Features);
224 
225   if (!success)
226     D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
227 
228   if (Args.getLastArg(options::OPT_mgeneral_regs_only)) {
229     Features.push_back("-fp-armv8");
230     Features.push_back("-crypto");
231     Features.push_back("-neon");
232   }
233 
234   if (Arg *A = Args.getLastArg(options::OPT_mtp_mode_EQ)) {
235     StringRef Mtp = A->getValue();
236     if (Mtp == "el3")
237       Features.push_back("+tpidr-el3");
238     else if (Mtp == "el2")
239       Features.push_back("+tpidr-el2");
240     else if (Mtp == "el1")
241       Features.push_back("+tpidr-el1");
242     else if (Mtp != "el0")
243       D.Diag(diag::err_drv_invalid_mtp) << A->getAsString(Args);
244   }
245 
246   // Enable/disable straight line speculation hardening.
247   if (Arg *A = Args.getLastArg(options::OPT_mharden_sls_EQ)) {
248     StringRef Scope = A->getValue();
249     bool EnableRetBr = false;
250     bool EnableBlr = false;
251     bool DisableComdat = false;
252     if (Scope != "none") {
253       SmallVector<StringRef, 4> Opts;
254       Scope.split(Opts, ",");
255       for (auto Opt : Opts) {
256         Opt = Opt.trim();
257         if (Opt == "all") {
258           EnableBlr = true;
259           EnableRetBr = true;
260           continue;
261         }
262         if (Opt == "retbr") {
263           EnableRetBr = true;
264           continue;
265         }
266         if (Opt == "blr") {
267           EnableBlr = true;
268           continue;
269         }
270         if (Opt == "comdat") {
271           DisableComdat = false;
272           continue;
273         }
274         if (Opt == "nocomdat") {
275           DisableComdat = true;
276           continue;
277         }
278         D.Diag(diag::err_invalid_sls_hardening)
279             << Scope << A->getAsString(Args);
280         break;
281       }
282     }
283 
284     if (EnableRetBr)
285       Features.push_back("+harden-sls-retbr");
286     if (EnableBlr)
287       Features.push_back("+harden-sls-blr");
288     if (DisableComdat) {
289       Features.push_back("+harden-sls-nocomdat");
290     }
291   }
292 
293   // En/disable crc
294   if (Arg *A = Args.getLastArg(options::OPT_mcrc, options::OPT_mnocrc)) {
295     if (A->getOption().matches(options::OPT_mcrc))
296       Features.push_back("+crc");
297     else
298       Features.push_back("-crc");
299   }
300 
301   // Handle (arch-dependent) fp16fml/fullfp16 relationship.
302   // FIXME: this fp16fml option handling will be reimplemented after the
303   // TargetParser rewrite.
304   const auto ItRNoFullFP16 = std::find(Features.rbegin(), Features.rend(), "-fullfp16");
305   const auto ItRFP16FML = std::find(Features.rbegin(), Features.rend(), "+fp16fml");
306   if (llvm::is_contained(Features, "+v8.4a")) {
307     const auto ItRFullFP16  = std::find(Features.rbegin(), Features.rend(), "+fullfp16");
308     if (ItRFullFP16 < ItRNoFullFP16 && ItRFullFP16 < ItRFP16FML) {
309       // Only entangled feature that can be to the right of this +fullfp16 is -fp16fml.
310       // Only append the +fp16fml if there is no -fp16fml after the +fullfp16.
311       if (std::find(Features.rbegin(), ItRFullFP16, "-fp16fml") == ItRFullFP16)
312         Features.push_back("+fp16fml");
313     }
314     else
315       goto fp16_fml_fallthrough;
316   } else {
317 fp16_fml_fallthrough:
318     // In both of these cases, putting the 'other' feature on the end of the vector will
319     // result in the same effect as placing it immediately after the current feature.
320     if (ItRNoFullFP16 < ItRFP16FML)
321       Features.push_back("-fp16fml");
322     else if (ItRNoFullFP16 > ItRFP16FML)
323       Features.push_back("+fullfp16");
324   }
325 
326   // FIXME: this needs reimplementation too after the TargetParser rewrite
327   //
328   // Context sensitive meaning of Crypto:
329   // 1) For Arch >= ARMv8.4a:  crypto = sm4 + sha3 + sha2 + aes
330   // 2) For Arch <= ARMv8.3a:  crypto = sha2 + aes
331   const auto ItBegin = Features.begin();
332   const auto ItEnd = Features.end();
333   const auto ItRBegin = Features.rbegin();
334   const auto ItREnd = Features.rend();
335   const auto ItRCrypto = std::find(ItRBegin, ItREnd, "+crypto");
336   const auto ItRNoCrypto = std::find(ItRBegin, ItREnd, "-crypto");
337   const auto HasCrypto  = ItRCrypto != ItREnd;
338   const auto HasNoCrypto = ItRNoCrypto != ItREnd;
339   const ptrdiff_t PosCrypto = ItRCrypto - ItRBegin;
340   const ptrdiff_t PosNoCrypto = ItRNoCrypto - ItRBegin;
341 
342   bool NoCrypto = false;
343   if (HasCrypto && HasNoCrypto) {
344     if (PosNoCrypto < PosCrypto)
345       NoCrypto = true;
346   }
347 
348   if (std::find(ItBegin, ItEnd, "+v8.4a") != ItEnd) {
349     if (HasCrypto && !NoCrypto) {
350       // Check if we have NOT disabled an algorithm with something like:
351       //   +crypto, -algorithm
352       // And if "-algorithm" does not occur, we enable that crypto algorithm.
353       const bool HasSM4  = (std::find(ItBegin, ItEnd, "-sm4") == ItEnd);
354       const bool HasSHA3 = (std::find(ItBegin, ItEnd, "-sha3") == ItEnd);
355       const bool HasSHA2 = (std::find(ItBegin, ItEnd, "-sha2") == ItEnd);
356       const bool HasAES  = (std::find(ItBegin, ItEnd, "-aes") == ItEnd);
357       if (HasSM4)
358         Features.push_back("+sm4");
359       if (HasSHA3)
360         Features.push_back("+sha3");
361       if (HasSHA2)
362         Features.push_back("+sha2");
363       if (HasAES)
364         Features.push_back("+aes");
365     } else if (HasNoCrypto) {
366       // Check if we have NOT enabled a crypto algorithm with something like:
367       //   -crypto, +algorithm
368       // And if "+algorithm" does not occur, we disable that crypto algorithm.
369       const bool HasSM4  = (std::find(ItBegin, ItEnd, "+sm4") != ItEnd);
370       const bool HasSHA3 = (std::find(ItBegin, ItEnd, "+sha3") != ItEnd);
371       const bool HasSHA2 = (std::find(ItBegin, ItEnd, "+sha2") != ItEnd);
372       const bool HasAES  = (std::find(ItBegin, ItEnd, "+aes") != ItEnd);
373       if (!HasSM4)
374         Features.push_back("-sm4");
375       if (!HasSHA3)
376         Features.push_back("-sha3");
377       if (!HasSHA2)
378         Features.push_back("-sha2");
379       if (!HasAES)
380         Features.push_back("-aes");
381     }
382   } else {
383     if (HasCrypto && !NoCrypto) {
384       const bool HasSHA2 = (std::find(ItBegin, ItEnd, "-sha2") == ItEnd);
385       const bool HasAES = (std::find(ItBegin, ItEnd, "-aes") == ItEnd);
386       if (HasSHA2)
387         Features.push_back("+sha2");
388       if (HasAES)
389         Features.push_back("+aes");
390     } else if (HasNoCrypto) {
391       const bool HasSHA2 = (std::find(ItBegin, ItEnd, "+sha2") != ItEnd);
392       const bool HasAES  = (std::find(ItBegin, ItEnd, "+aes") != ItEnd);
393       const bool HasV82a = (std::find(ItBegin, ItEnd, "+v8.2a") != ItEnd);
394       const bool HasV83a = (std::find(ItBegin, ItEnd, "+v8.3a") != ItEnd);
395       const bool HasV84a = (std::find(ItBegin, ItEnd, "+v8.4a") != ItEnd);
396       if (!HasSHA2)
397         Features.push_back("-sha2");
398       if (!HasAES)
399         Features.push_back("-aes");
400       if (HasV82a || HasV83a || HasV84a) {
401         Features.push_back("-sm4");
402         Features.push_back("-sha3");
403       }
404     }
405   }
406 
407   auto V8_6Pos = llvm::find(Features, "+v8.6a");
408   if (V8_6Pos != std::end(Features))
409     V8_6Pos = Features.insert(std::next(V8_6Pos), {"+i8mm", "+bf16"});
410 
411   if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access,
412                                options::OPT_munaligned_access)) {
413     if (A->getOption().matches(options::OPT_mno_unaligned_access))
414       Features.push_back("+strict-align");
415   } else if (Triple.isOSOpenBSD())
416     Features.push_back("+strict-align");
417 
418   if (Args.hasArg(options::OPT_ffixed_x1))
419     Features.push_back("+reserve-x1");
420 
421   if (Args.hasArg(options::OPT_ffixed_x2))
422     Features.push_back("+reserve-x2");
423 
424   if (Args.hasArg(options::OPT_ffixed_x3))
425     Features.push_back("+reserve-x3");
426 
427   if (Args.hasArg(options::OPT_ffixed_x4))
428     Features.push_back("+reserve-x4");
429 
430   if (Args.hasArg(options::OPT_ffixed_x5))
431     Features.push_back("+reserve-x5");
432 
433   if (Args.hasArg(options::OPT_ffixed_x6))
434     Features.push_back("+reserve-x6");
435 
436   if (Args.hasArg(options::OPT_ffixed_x7))
437     Features.push_back("+reserve-x7");
438 
439   if (Args.hasArg(options::OPT_ffixed_x9))
440     Features.push_back("+reserve-x9");
441 
442   if (Args.hasArg(options::OPT_ffixed_x10))
443     Features.push_back("+reserve-x10");
444 
445   if (Args.hasArg(options::OPT_ffixed_x11))
446     Features.push_back("+reserve-x11");
447 
448   if (Args.hasArg(options::OPT_ffixed_x12))
449     Features.push_back("+reserve-x12");
450 
451   if (Args.hasArg(options::OPT_ffixed_x13))
452     Features.push_back("+reserve-x13");
453 
454   if (Args.hasArg(options::OPT_ffixed_x14))
455     Features.push_back("+reserve-x14");
456 
457   if (Args.hasArg(options::OPT_ffixed_x15))
458     Features.push_back("+reserve-x15");
459 
460   if (Args.hasArg(options::OPT_ffixed_x18))
461     Features.push_back("+reserve-x18");
462 
463   if (Args.hasArg(options::OPT_ffixed_x20))
464     Features.push_back("+reserve-x20");
465 
466   if (Args.hasArg(options::OPT_ffixed_x21))
467     Features.push_back("+reserve-x21");
468 
469   if (Args.hasArg(options::OPT_ffixed_x22))
470     Features.push_back("+reserve-x22");
471 
472   if (Args.hasArg(options::OPT_ffixed_x23))
473     Features.push_back("+reserve-x23");
474 
475   if (Args.hasArg(options::OPT_ffixed_x24))
476     Features.push_back("+reserve-x24");
477 
478   if (Args.hasArg(options::OPT_ffixed_x25))
479     Features.push_back("+reserve-x25");
480 
481   if (Args.hasArg(options::OPT_ffixed_x26))
482     Features.push_back("+reserve-x26");
483 
484   if (Args.hasArg(options::OPT_ffixed_x27))
485     Features.push_back("+reserve-x27");
486 
487   if (Args.hasArg(options::OPT_ffixed_x28))
488     Features.push_back("+reserve-x28");
489 
490   if (Args.hasArg(options::OPT_ffixed_x30))
491     Features.push_back("+reserve-x30");
492 
493   if (Args.hasArg(options::OPT_fcall_saved_x8))
494     Features.push_back("+call-saved-x8");
495 
496   if (Args.hasArg(options::OPT_fcall_saved_x9))
497     Features.push_back("+call-saved-x9");
498 
499   if (Args.hasArg(options::OPT_fcall_saved_x10))
500     Features.push_back("+call-saved-x10");
501 
502   if (Args.hasArg(options::OPT_fcall_saved_x11))
503     Features.push_back("+call-saved-x11");
504 
505   if (Args.hasArg(options::OPT_fcall_saved_x12))
506     Features.push_back("+call-saved-x12");
507 
508   if (Args.hasArg(options::OPT_fcall_saved_x13))
509     Features.push_back("+call-saved-x13");
510 
511   if (Args.hasArg(options::OPT_fcall_saved_x14))
512     Features.push_back("+call-saved-x14");
513 
514   if (Args.hasArg(options::OPT_fcall_saved_x15))
515     Features.push_back("+call-saved-x15");
516 
517   if (Args.hasArg(options::OPT_fcall_saved_x18))
518     Features.push_back("+call-saved-x18");
519 
520   if (Args.hasArg(options::OPT_mno_neg_immediates))
521     Features.push_back("+no-neg-immediates");
522 }
523