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