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