blob: b21cfac6e7ed77ad83855f00f3a2b74567894a0c [file] [log] [blame]
David L. Jonesecc6de32017-02-24 00:28:01 +00001//===--- AArch64.cpp - AArch64 (not ARM) Helpers for Tools ------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
David L. Jonesecc6de32017-02-24 00:28:01 +00006//
7//===----------------------------------------------------------------------===//
8
David L. Jonesf561aba2017-03-08 01:02:16 +00009#include "AArch64.h"
David L. Jonesecc6de32017-02-24 00:28:01 +000010#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"
Reid Kleckner90c64a32019-10-19 00:48:11 +000015#include "llvm/Support/Host.h"
David L. Jonesecc6de32017-02-24 00:28:01 +000016
17using namespace clang::driver;
18using namespace clang::driver::tools;
19using namespace clang;
20using namespace llvm::opt;
21
Alex Lorenz9b20a992018-12-17 19:30:46 +000022/// \returns true if the given triple can determine the default CPU type even
23/// if -arch is not specified.
24static bool isCPUDeterminedByTriple(const llvm::Triple &Triple) {
25 return Triple.isOSDarwin();
26}
27
David L. Jonesecc6de32017-02-24 00:28:01 +000028/// getAArch64TargetCPU - Get the (LLVM) name of the AArch64 cpu we are
Peter Smith820e46f2017-10-24 09:51:55 +000029/// targeting. Set \p A to the Arg corresponding to the -mcpu argument if it is
30/// provided, or to nullptr otherwise.
Alex Lorenz9b20a992018-12-17 19:30:46 +000031std::string aarch64::getAArch64TargetCPU(const ArgList &Args,
32 const llvm::Triple &Triple, Arg *&A) {
David L. Jonesecc6de32017-02-24 00:28:01 +000033 std::string CPU;
Peter Smith820e46f2017-10-24 09:51:55 +000034 // If we have -mcpu, use that.
35 if ((A = Args.getLastArg(options::OPT_mcpu_EQ))) {
David L. Jonesecc6de32017-02-24 00:28:01 +000036 StringRef Mcpu = A->getValue();
37 CPU = Mcpu.split("+").first.lower();
38 }
39
40 // Handle CPU name is 'native'.
41 if (CPU == "native")
Benjamin Krameradcd0262020-01-28 20:23:46 +010042 return std::string(llvm::sys::getHostCPUName());
David L. Jonesecc6de32017-02-24 00:28:01 +000043 else if (CPU.size())
44 return CPU;
45
Tim Northover903e5c32019-11-15 12:39:56 +000046 // Make sure we pick the appropriate Apple CPU if -arch is used or when
47 // targetting a Darwin OS.
Alex Lorenz9b20a992018-12-17 19:30:46 +000048 if (Args.getLastArg(options::OPT_arch) || Triple.isOSDarwin())
Tim Northover903e5c32019-11-15 12:39:56 +000049 return Triple.getArch() == llvm::Triple::aarch64_32 ? "apple-s4"
50 : "apple-a7";
David L. Jonesecc6de32017-02-24 00:28:01 +000051
52 return "generic";
53}
54
55// Decode AArch64 features from string like +[no]featureA+[no]featureB+...
56static bool DecodeAArch64Features(const Driver &D, StringRef text,
57 std::vector<StringRef> &Features) {
58 SmallVector<StringRef, 8> Split;
59 text.split(Split, StringRef("+"), -1, false);
60
61 for (StringRef Feature : Split) {
62 StringRef FeatureName = llvm::AArch64::getArchExtFeature(Feature);
63 if (!FeatureName.empty())
64 Features.push_back(FeatureName);
65 else if (Feature == "neon" || Feature == "noneon")
66 D.Diag(clang::diag::err_drv_no_neon_modifier);
67 else
68 return false;
69 }
70 return true;
71}
72
73// Check if the CPU name and feature modifiers in -mcpu are legal. If yes,
74// decode CPU and feature.
75static bool DecodeAArch64Mcpu(const Driver &D, StringRef Mcpu, StringRef &CPU,
76 std::vector<StringRef> &Features) {
77 std::pair<StringRef, StringRef> Split = Mcpu.split("+");
78 CPU = Split.first;
79
Florian Hahn4327b3e2018-07-06 10:49:59 +000080 if (CPU == "native")
81 CPU = llvm::sys::getHostCPUName();
82
David L. Jonesecc6de32017-02-24 00:28:01 +000083 if (CPU == "generic") {
84 Features.push_back("+neon");
85 } else {
Florian Hahnef5bbd62017-07-27 16:28:39 +000086 llvm::AArch64::ArchKind ArchKind = llvm::AArch64::parseCPUArch(CPU);
David L. Jonesecc6de32017-02-24 00:28:01 +000087 if (!llvm::AArch64::getArchFeatures(ArchKind, Features))
88 return false;
89
90 unsigned Extension = llvm::AArch64::getDefaultExtensions(CPU, ArchKind);
91 if (!llvm::AArch64::getExtensionFeatures(Extension, Features))
92 return false;
93 }
94
95 if (Split.second.size() && !DecodeAArch64Features(D, Split.second, Features))
96 return false;
97
98 return true;
99}
100
101static bool
102getAArch64ArchFeaturesFromMarch(const Driver &D, StringRef March,
103 const ArgList &Args,
104 std::vector<StringRef> &Features) {
105 std::string MarchLowerCase = March.lower();
106 std::pair<StringRef, StringRef> Split = StringRef(MarchLowerCase).split("+");
107
Florian Hahnef5bbd62017-07-27 16:28:39 +0000108 llvm::AArch64::ArchKind ArchKind = llvm::AArch64::parseArch(Split.first);
109 if (ArchKind == llvm::AArch64::ArchKind::INVALID ||
David L. Jonesecc6de32017-02-24 00:28:01 +0000110 !llvm::AArch64::getArchFeatures(ArchKind, Features) ||
111 (Split.second.size() && !DecodeAArch64Features(D, Split.second, Features)))
112 return false;
113
114 return true;
115}
116
117static bool
118getAArch64ArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu,
119 const ArgList &Args,
120 std::vector<StringRef> &Features) {
121 StringRef CPU;
122 std::string McpuLowerCase = Mcpu.lower();
123 if (!DecodeAArch64Mcpu(D, McpuLowerCase, CPU, Features))
124 return false;
125
126 return true;
127}
128
129static bool
130getAArch64MicroArchFeaturesFromMtune(const Driver &D, StringRef Mtune,
131 const ArgList &Args,
132 std::vector<StringRef> &Features) {
133 std::string MtuneLowerCase = Mtune.lower();
Peter Smith820e46f2017-10-24 09:51:55 +0000134 // Check CPU name is valid
135 std::vector<StringRef> MtuneFeatures;
136 StringRef Tune;
137 if (!DecodeAArch64Mcpu(D, MtuneLowerCase, Tune, MtuneFeatures))
138 return false;
139
David L. Jonesecc6de32017-02-24 00:28:01 +0000140 // Handle CPU name is 'native'.
141 if (MtuneLowerCase == "native")
Benjamin Krameradcd0262020-01-28 20:23:46 +0100142 MtuneLowerCase = std::string(llvm::sys::getHostCPUName());
Benjamin Kramere8f13f42020-03-31 20:56:24 +0200143 if (MtuneLowerCase == "cyclone" ||
144 StringRef(MtuneLowerCase).startswith("apple")) {
David L. Jonesecc6de32017-02-24 00:28:01 +0000145 Features.push_back("+zcm");
146 Features.push_back("+zcz");
147 }
148 return true;
149}
150
151static bool
152getAArch64MicroArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu,
153 const ArgList &Args,
154 std::vector<StringRef> &Features) {
155 StringRef CPU;
156 std::vector<StringRef> DecodedFeature;
157 std::string McpuLowerCase = Mcpu.lower();
158 if (!DecodeAArch64Mcpu(D, McpuLowerCase, CPU, DecodedFeature))
159 return false;
160
161 return getAArch64MicroArchFeaturesFromMtune(D, CPU, Args, Features);
162}
163
Alex Lorenz9b20a992018-12-17 19:30:46 +0000164void aarch64::getAArch64TargetFeatures(const Driver &D,
165 const llvm::Triple &Triple,
166 const ArgList &Args,
David L. Jonesecc6de32017-02-24 00:28:01 +0000167 std::vector<StringRef> &Features) {
168 Arg *A;
169 bool success = true;
170 // Enable NEON by default.
171 Features.push_back("+neon");
172 if ((A = Args.getLastArg(options::OPT_march_EQ)))
173 success = getAArch64ArchFeaturesFromMarch(D, A->getValue(), Args, Features);
174 else if ((A = Args.getLastArg(options::OPT_mcpu_EQ)))
175 success = getAArch64ArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
Alex Lorenz9b20a992018-12-17 19:30:46 +0000176 else if (Args.hasArg(options::OPT_arch) || isCPUDeterminedByTriple(Triple))
177 success = getAArch64ArchFeaturesFromMcpu(
178 D, getAArch64TargetCPU(Args, Triple, A), Args, Features);
David L. Jonesecc6de32017-02-24 00:28:01 +0000179
180 if (success && (A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)))
181 success =
182 getAArch64MicroArchFeaturesFromMtune(D, A->getValue(), Args, Features);
183 else if (success && (A = Args.getLastArg(options::OPT_mcpu_EQ)))
184 success =
185 getAArch64MicroArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
Alex Lorenz9b20a992018-12-17 19:30:46 +0000186 else if (success &&
187 (Args.hasArg(options::OPT_arch) || isCPUDeterminedByTriple(Triple)))
David L. Jonesecc6de32017-02-24 00:28:01 +0000188 success = getAArch64MicroArchFeaturesFromMcpu(
Alex Lorenz9b20a992018-12-17 19:30:46 +0000189 D, getAArch64TargetCPU(Args, Triple, A), Args, Features);
David L. Jonesecc6de32017-02-24 00:28:01 +0000190
191 if (!success)
192 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
193
194 if (Args.getLastArg(options::OPT_mgeneral_regs_only)) {
195 Features.push_back("-fp-armv8");
196 Features.push_back("-crypto");
197 Features.push_back("-neon");
198 }
199
Oliver Stannardd83a5592019-03-29 13:32:41 +0000200 if (Arg *A = Args.getLastArg(options::OPT_mtp_mode_EQ)) {
201 StringRef Mtp = A->getValue();
202 if (Mtp == "el3")
203 Features.push_back("+tpidr-el3");
204 else if (Mtp == "el2")
205 Features.push_back("+tpidr-el2");
206 else if (Mtp == "el1")
207 Features.push_back("+tpidr-el1");
208 else if (Mtp != "el0")
209 D.Diag(diag::err_drv_invalid_mtp) << A->getAsString(Args);
210 }
211
David L. Jonesecc6de32017-02-24 00:28:01 +0000212 // En/disable crc
213 if (Arg *A = Args.getLastArg(options::OPT_mcrc, options::OPT_mnocrc)) {
214 if (A->getOption().matches(options::OPT_mcrc))
215 Features.push_back("+crc");
216 else
217 Features.push_back("-crc");
218 }
219
Sjoerd Meijerc0176562018-09-24 07:55:20 +0000220 // Handle (arch-dependent) fp16fml/fullfp16 relationship.
221 // FIXME: this fp16fml option handling will be reimplemented after the
222 // TargetParser rewrite.
223 const auto ItRNoFullFP16 = std::find(Features.rbegin(), Features.rend(), "-fullfp16");
224 const auto ItRFP16FML = std::find(Features.rbegin(), Features.rend(), "+fp16fml");
Fangrui Song9ac13a12019-02-10 05:54:57 +0000225 if (llvm::is_contained(Features, "+v8.4a")) {
Sjoerd Meijerc0176562018-09-24 07:55:20 +0000226 const auto ItRFullFP16 = std::find(Features.rbegin(), Features.rend(), "+fullfp16");
227 if (ItRFullFP16 < ItRNoFullFP16 && ItRFullFP16 < ItRFP16FML) {
228 // Only entangled feature that can be to the right of this +fullfp16 is -fp16fml.
229 // Only append the +fp16fml if there is no -fp16fml after the +fullfp16.
230 if (std::find(Features.rbegin(), ItRFullFP16, "-fp16fml") == ItRFullFP16)
231 Features.push_back("+fp16fml");
232 }
233 else
234 goto fp16_fml_fallthrough;
Fangrui Song9ac13a12019-02-10 05:54:57 +0000235 } else {
Sjoerd Meijerc0176562018-09-24 07:55:20 +0000236fp16_fml_fallthrough:
237 // In both of these cases, putting the 'other' feature on the end of the vector will
238 // result in the same effect as placing it immediately after the current feature.
239 if (ItRNoFullFP16 < ItRFP16FML)
240 Features.push_back("-fp16fml");
241 else if (ItRNoFullFP16 > ItRFP16FML)
242 Features.push_back("+fullfp16");
243 }
244
Sjoerd Meijerd60540a2018-10-04 07:38:53 +0000245 // FIXME: this needs reimplementation too after the TargetParser rewrite
246 //
247 // Context sensitive meaning of Crypto:
248 // 1) For Arch >= ARMv8.4a: crypto = sm4 + sha3 + sha2 + aes
249 // 2) For Arch <= ARMv8.3a: crypto = sha2 + aes
250 const auto ItBegin = Features.begin();
251 const auto ItEnd = Features.end();
252 const auto ItRBegin = Features.rbegin();
253 const auto ItREnd = Features.rend();
254 const auto ItRCrypto = std::find(ItRBegin, ItREnd, "+crypto");
255 const auto ItRNoCrypto = std::find(ItRBegin, ItREnd, "-crypto");
256 const auto HasCrypto = ItRCrypto != ItREnd;
257 const auto HasNoCrypto = ItRNoCrypto != ItREnd;
258 const ptrdiff_t PosCrypto = ItRCrypto - ItRBegin;
259 const ptrdiff_t PosNoCrypto = ItRNoCrypto - ItRBegin;
260
261 bool NoCrypto = false;
262 if (HasCrypto && HasNoCrypto) {
263 if (PosNoCrypto < PosCrypto)
264 NoCrypto = true;
265 }
266
267 if (std::find(ItBegin, ItEnd, "+v8.4a") != ItEnd) {
268 if (HasCrypto && !NoCrypto) {
269 // Check if we have NOT disabled an algorithm with something like:
270 // +crypto, -algorithm
271 // And if "-algorithm" does not occur, we enable that crypto algorithm.
272 const bool HasSM4 = (std::find(ItBegin, ItEnd, "-sm4") == ItEnd);
273 const bool HasSHA3 = (std::find(ItBegin, ItEnd, "-sha3") == ItEnd);
274 const bool HasSHA2 = (std::find(ItBegin, ItEnd, "-sha2") == ItEnd);
275 const bool HasAES = (std::find(ItBegin, ItEnd, "-aes") == ItEnd);
276 if (HasSM4)
277 Features.push_back("+sm4");
278 if (HasSHA3)
279 Features.push_back("+sha3");
280 if (HasSHA2)
281 Features.push_back("+sha2");
282 if (HasAES)
283 Features.push_back("+aes");
284 } else if (HasNoCrypto) {
285 // Check if we have NOT enabled a crypto algorithm with something like:
286 // -crypto, +algorithm
287 // And if "+algorithm" does not occur, we disable that crypto algorithm.
288 const bool HasSM4 = (std::find(ItBegin, ItEnd, "+sm4") != ItEnd);
289 const bool HasSHA3 = (std::find(ItBegin, ItEnd, "+sha3") != ItEnd);
290 const bool HasSHA2 = (std::find(ItBegin, ItEnd, "+sha2") != ItEnd);
291 const bool HasAES = (std::find(ItBegin, ItEnd, "+aes") != ItEnd);
292 if (!HasSM4)
293 Features.push_back("-sm4");
294 if (!HasSHA3)
295 Features.push_back("-sha3");
296 if (!HasSHA2)
297 Features.push_back("-sha2");
298 if (!HasAES)
299 Features.push_back("-aes");
300 }
301 } else {
302 if (HasCrypto && !NoCrypto) {
303 const bool HasSHA2 = (std::find(ItBegin, ItEnd, "-sha2") == ItEnd);
304 const bool HasAES = (std::find(ItBegin, ItEnd, "-aes") == ItEnd);
305 if (HasSHA2)
306 Features.push_back("+sha2");
307 if (HasAES)
308 Features.push_back("+aes");
309 } else if (HasNoCrypto) {
310 const bool HasSHA2 = (std::find(ItBegin, ItEnd, "+sha2") != ItEnd);
311 const bool HasAES = (std::find(ItBegin, ItEnd, "+aes") != ItEnd);
312 const bool HasV82a = (std::find(ItBegin, ItEnd, "+v8.2a") != ItEnd);
313 const bool HasV83a = (std::find(ItBegin, ItEnd, "+v8.3a") != ItEnd);
314 const bool HasV84a = (std::find(ItBegin, ItEnd, "+v8.4a") != ItEnd);
315 if (!HasSHA2)
316 Features.push_back("-sha2");
317 if (!HasAES)
318 Features.push_back("-aes");
319 if (HasV82a || HasV83a || HasV84a) {
320 Features.push_back("-sm4");
321 Features.push_back("-sha3");
322 }
323 }
324 }
325
David L. Jonesecc6de32017-02-24 00:28:01 +0000326 if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access,
327 options::OPT_munaligned_access))
328 if (A->getOption().matches(options::OPT_mno_unaligned_access))
329 Features.push_back("+strict-align");
330
Tri Vo6e8abbc2018-09-12 23:45:04 +0000331 if (Args.hasArg(options::OPT_ffixed_x1))
332 Features.push_back("+reserve-x1");
333
334 if (Args.hasArg(options::OPT_ffixed_x2))
335 Features.push_back("+reserve-x2");
336
337 if (Args.hasArg(options::OPT_ffixed_x3))
338 Features.push_back("+reserve-x3");
339
340 if (Args.hasArg(options::OPT_ffixed_x4))
341 Features.push_back("+reserve-x4");
342
343 if (Args.hasArg(options::OPT_ffixed_x5))
344 Features.push_back("+reserve-x5");
345
346 if (Args.hasArg(options::OPT_ffixed_x6))
347 Features.push_back("+reserve-x6");
348
349 if (Args.hasArg(options::OPT_ffixed_x7))
350 Features.push_back("+reserve-x7");
351
Petr Hosekfcbec022019-02-13 17:28:47 +0000352 if (Args.hasArg(options::OPT_ffixed_x9))
353 Features.push_back("+reserve-x9");
354
355 if (Args.hasArg(options::OPT_ffixed_x10))
356 Features.push_back("+reserve-x10");
357
358 if (Args.hasArg(options::OPT_ffixed_x11))
359 Features.push_back("+reserve-x11");
360
361 if (Args.hasArg(options::OPT_ffixed_x12))
362 Features.push_back("+reserve-x12");
363
364 if (Args.hasArg(options::OPT_ffixed_x13))
365 Features.push_back("+reserve-x13");
366
367 if (Args.hasArg(options::OPT_ffixed_x14))
368 Features.push_back("+reserve-x14");
369
370 if (Args.hasArg(options::OPT_ffixed_x15))
371 Features.push_back("+reserve-x15");
372
David L. Jonesecc6de32017-02-24 00:28:01 +0000373 if (Args.hasArg(options::OPT_ffixed_x18))
374 Features.push_back("+reserve-x18");
Sanne Wouda784004e2017-03-27 15:34:52 +0000375
Petr Hosek72509082018-06-12 20:00:50 +0000376 if (Args.hasArg(options::OPT_ffixed_x20))
377 Features.push_back("+reserve-x20");
378
Petr Hosekfcbec022019-02-13 17:28:47 +0000379 if (Args.hasArg(options::OPT_ffixed_x21))
380 Features.push_back("+reserve-x21");
381
382 if (Args.hasArg(options::OPT_ffixed_x22))
383 Features.push_back("+reserve-x22");
384
385 if (Args.hasArg(options::OPT_ffixed_x23))
386 Features.push_back("+reserve-x23");
387
388 if (Args.hasArg(options::OPT_ffixed_x24))
389 Features.push_back("+reserve-x24");
390
Petr Hosek7a290df2019-02-13 18:01:23 +0000391 if (Args.hasArg(options::OPT_ffixed_x25))
392 Features.push_back("+reserve-x25");
393
Petr Hosekfcbec022019-02-13 17:28:47 +0000394 if (Args.hasArg(options::OPT_ffixed_x26))
395 Features.push_back("+reserve-x26");
396
397 if (Args.hasArg(options::OPT_ffixed_x27))
398 Features.push_back("+reserve-x27");
399
400 if (Args.hasArg(options::OPT_ffixed_x28))
401 Features.push_back("+reserve-x28");
402
Tri Vo28e7e602018-09-25 16:48:40 +0000403 if (Args.hasArg(options::OPT_fcall_saved_x8))
404 Features.push_back("+call-saved-x8");
405
406 if (Args.hasArg(options::OPT_fcall_saved_x9))
407 Features.push_back("+call-saved-x9");
408
409 if (Args.hasArg(options::OPT_fcall_saved_x10))
410 Features.push_back("+call-saved-x10");
411
412 if (Args.hasArg(options::OPT_fcall_saved_x11))
413 Features.push_back("+call-saved-x11");
414
415 if (Args.hasArg(options::OPT_fcall_saved_x12))
416 Features.push_back("+call-saved-x12");
417
418 if (Args.hasArg(options::OPT_fcall_saved_x13))
419 Features.push_back("+call-saved-x13");
420
421 if (Args.hasArg(options::OPT_fcall_saved_x14))
422 Features.push_back("+call-saved-x14");
423
424 if (Args.hasArg(options::OPT_fcall_saved_x15))
425 Features.push_back("+call-saved-x15");
426
427 if (Args.hasArg(options::OPT_fcall_saved_x18))
428 Features.push_back("+call-saved-x18");
429
Sanne Wouda784004e2017-03-27 15:34:52 +0000430 if (Args.hasArg(options::OPT_mno_neg_immediates))
431 Features.push_back("+no-neg-immediates");
David L. Jonesecc6de32017-02-24 00:28:01 +0000432}