Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / lib / Support / ARMTargetParser.cpp
blob7b8ec2f339065775ab31b5345bfa88a2cb6acbce
1 //===-- ARMTargetParser - Parser for ARM target features --------*- 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 // This file implements a target parser to recognise ARM hardware features
10 // such as FPU/CPU/ARCH/extensions and specific support such as HWDIV.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/ARMTargetParser.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include <cctype>
18 using namespace llvm;
20 static StringRef getHWDivSynonym(StringRef HWDiv) {
21 return StringSwitch<StringRef>(HWDiv)
22 .Case("thumb,arm", "arm,thumb")
23 .Default(HWDiv);
26 // Allows partial match, ex. "v7a" matches "armv7a".
27 ARM::ArchKind ARM::parseArch(StringRef Arch) {
28 Arch = getCanonicalArchName(Arch);
29 StringRef Syn = getArchSynonym(Arch);
30 for (const auto A : ARCHNames) {
31 if (A.getName().endswith(Syn))
32 return A.ID;
34 return ArchKind::INVALID;
37 // Version number (ex. v7 = 7).
38 unsigned ARM::parseArchVersion(StringRef Arch) {
39 Arch = getCanonicalArchName(Arch);
40 switch (parseArch(Arch)) {
41 case ArchKind::ARMV2:
42 case ArchKind::ARMV2A:
43 return 2;
44 case ArchKind::ARMV3:
45 case ArchKind::ARMV3M:
46 return 3;
47 case ArchKind::ARMV4:
48 case ArchKind::ARMV4T:
49 return 4;
50 case ArchKind::ARMV5T:
51 case ArchKind::ARMV5TE:
52 case ArchKind::IWMMXT:
53 case ArchKind::IWMMXT2:
54 case ArchKind::XSCALE:
55 case ArchKind::ARMV5TEJ:
56 return 5;
57 case ArchKind::ARMV6:
58 case ArchKind::ARMV6K:
59 case ArchKind::ARMV6T2:
60 case ArchKind::ARMV6KZ:
61 case ArchKind::ARMV6M:
62 return 6;
63 case ArchKind::ARMV7A:
64 case ArchKind::ARMV7VE:
65 case ArchKind::ARMV7R:
66 case ArchKind::ARMV7M:
67 case ArchKind::ARMV7S:
68 case ArchKind::ARMV7EM:
69 case ArchKind::ARMV7K:
70 return 7;
71 case ArchKind::ARMV8A:
72 case ArchKind::ARMV8_1A:
73 case ArchKind::ARMV8_2A:
74 case ArchKind::ARMV8_3A:
75 case ArchKind::ARMV8_4A:
76 case ArchKind::ARMV8_5A:
77 case ArchKind::ARMV8R:
78 case ArchKind::ARMV8MBaseline:
79 case ArchKind::ARMV8MMainline:
80 return 8;
81 case ArchKind::INVALID:
82 return 0;
84 llvm_unreachable("Unhandled architecture");
87 // Profile A/R/M
88 ARM::ProfileKind ARM::parseArchProfile(StringRef Arch) {
89 Arch = getCanonicalArchName(Arch);
90 switch (parseArch(Arch)) {
91 case ArchKind::ARMV6M:
92 case ArchKind::ARMV7M:
93 case ArchKind::ARMV7EM:
94 case ArchKind::ARMV8MMainline:
95 case ArchKind::ARMV8MBaseline:
96 return ProfileKind::M;
97 case ArchKind::ARMV7R:
98 case ArchKind::ARMV8R:
99 return ProfileKind::R;
100 case ArchKind::ARMV7A:
101 case ArchKind::ARMV7VE:
102 case ArchKind::ARMV7K:
103 case ArchKind::ARMV8A:
104 case ArchKind::ARMV8_1A:
105 case ArchKind::ARMV8_2A:
106 case ArchKind::ARMV8_3A:
107 case ArchKind::ARMV8_4A:
108 case ArchKind::ARMV8_5A:
109 return ProfileKind::A;
110 case ArchKind::ARMV2:
111 case ArchKind::ARMV2A:
112 case ArchKind::ARMV3:
113 case ArchKind::ARMV3M:
114 case ArchKind::ARMV4:
115 case ArchKind::ARMV4T:
116 case ArchKind::ARMV5T:
117 case ArchKind::ARMV5TE:
118 case ArchKind::ARMV5TEJ:
119 case ArchKind::ARMV6:
120 case ArchKind::ARMV6K:
121 case ArchKind::ARMV6T2:
122 case ArchKind::ARMV6KZ:
123 case ArchKind::ARMV7S:
124 case ArchKind::IWMMXT:
125 case ArchKind::IWMMXT2:
126 case ArchKind::XSCALE:
127 case ArchKind::INVALID:
128 return ProfileKind::INVALID;
130 llvm_unreachable("Unhandled architecture");
133 StringRef ARM::getArchSynonym(StringRef Arch) {
134 return StringSwitch<StringRef>(Arch)
135 .Case("v5", "v5t")
136 .Case("v5e", "v5te")
137 .Case("v6j", "v6")
138 .Case("v6hl", "v6k")
139 .Cases("v6m", "v6sm", "v6s-m", "v6-m")
140 .Cases("v6z", "v6zk", "v6kz")
141 .Cases("v7", "v7a", "v7hl", "v7l", "v7-a")
142 .Case("v7r", "v7-r")
143 .Case("v7m", "v7-m")
144 .Case("v7em", "v7e-m")
145 .Cases("v8", "v8a", "v8l", "aarch64", "arm64", "v8-a")
146 .Case("v8.1a", "v8.1-a")
147 .Case("v8.2a", "v8.2-a")
148 .Case("v8.3a", "v8.3-a")
149 .Case("v8.4a", "v8.4-a")
150 .Case("v8.5a", "v8.5-a")
151 .Case("v8r", "v8-r")
152 .Case("v8m.base", "v8-m.base")
153 .Case("v8m.main", "v8-m.main")
154 .Default(Arch);
157 bool ARM::getFPUFeatures(unsigned FPUKind, std::vector<StringRef> &Features) {
159 if (FPUKind >= FK_LAST || FPUKind == FK_INVALID)
160 return false;
162 // fp-only-sp and d16 subtarget features are independent of each other, so we
163 // must enable/disable both.
164 switch (FPUNames[FPUKind].Restriction) {
165 case FPURestriction::SP_D16:
166 Features.push_back("+fp-only-sp");
167 Features.push_back("+d16");
168 break;
169 case FPURestriction::D16:
170 Features.push_back("-fp-only-sp");
171 Features.push_back("+d16");
172 break;
173 case FPURestriction::None:
174 Features.push_back("-fp-only-sp");
175 Features.push_back("-d16");
176 break;
179 // FPU version subtarget features are inclusive of lower-numbered ones, so
180 // enable the one corresponding to this version and disable all that are
181 // higher. We also have to make sure to disable fp16 when vfp4 is disabled,
182 // as +vfp4 implies +fp16 but -vfp4 does not imply -fp16.
183 switch (FPUNames[FPUKind].FPUVer) {
184 case FPUVersion::VFPV5:
185 Features.push_back("+fp-armv8");
186 break;
187 case FPUVersion::VFPV4:
188 Features.push_back("+vfp4");
189 Features.push_back("-fp-armv8");
190 break;
191 case FPUVersion::VFPV3_FP16:
192 Features.push_back("+vfp3");
193 Features.push_back("+fp16");
194 Features.push_back("-vfp4");
195 Features.push_back("-fp-armv8");
196 break;
197 case FPUVersion::VFPV3:
198 Features.push_back("+vfp3");
199 Features.push_back("-fp16");
200 Features.push_back("-vfp4");
201 Features.push_back("-fp-armv8");
202 break;
203 case FPUVersion::VFPV2:
204 Features.push_back("+vfp2");
205 Features.push_back("-vfp3");
206 Features.push_back("-fp16");
207 Features.push_back("-vfp4");
208 Features.push_back("-fp-armv8");
209 break;
210 case FPUVersion::NONE:
211 Features.push_back("-vfp2");
212 Features.push_back("-vfp3");
213 Features.push_back("-fp16");
214 Features.push_back("-vfp4");
215 Features.push_back("-fp-armv8");
216 break;
219 // crypto includes neon, so we handle this similarly to FPU version.
220 switch (FPUNames[FPUKind].NeonSupport) {
221 case NeonSupportLevel::Crypto:
222 Features.push_back("+neon");
223 Features.push_back("+crypto");
224 break;
225 case NeonSupportLevel::Neon:
226 Features.push_back("+neon");
227 Features.push_back("-crypto");
228 break;
229 case NeonSupportLevel::None:
230 Features.push_back("-neon");
231 Features.push_back("-crypto");
232 break;
235 return true;
238 // Little/Big endian
239 ARM::EndianKind ARM::parseArchEndian(StringRef Arch) {
240 if (Arch.startswith("armeb") || Arch.startswith("thumbeb") ||
241 Arch.startswith("aarch64_be"))
242 return EndianKind::BIG;
244 if (Arch.startswith("arm") || Arch.startswith("thumb")) {
245 if (Arch.endswith("eb"))
246 return EndianKind::BIG;
247 else
248 return EndianKind::LITTLE;
251 if (Arch.startswith("aarch64"))
252 return EndianKind::LITTLE;
254 return EndianKind::INVALID;
257 // ARM, Thumb, AArch64
258 ARM::ISAKind ARM::parseArchISA(StringRef Arch) {
259 return StringSwitch<ISAKind>(Arch)
260 .StartsWith("aarch64", ISAKind::AARCH64)
261 .StartsWith("arm64", ISAKind::AARCH64)
262 .StartsWith("thumb", ISAKind::THUMB)
263 .StartsWith("arm", ISAKind::ARM)
264 .Default(ISAKind::INVALID);
267 unsigned ARM::parseFPU(StringRef FPU) {
268 StringRef Syn = getFPUSynonym(FPU);
269 for (const auto F : FPUNames) {
270 if (Syn == F.getName())
271 return F.ID;
273 return FK_INVALID;
276 ARM::NeonSupportLevel ARM::getFPUNeonSupportLevel(unsigned FPUKind) {
277 if (FPUKind >= FK_LAST)
278 return NeonSupportLevel::None;
279 return FPUNames[FPUKind].NeonSupport;
282 // MArch is expected to be of the form (arm|thumb)?(eb)?(v.+)?(eb)?, but
283 // (iwmmxt|xscale)(eb)? is also permitted. If the former, return
284 // "v.+", if the latter, return unmodified string, minus 'eb'.
285 // If invalid, return empty string.
286 StringRef ARM::getCanonicalArchName(StringRef Arch) {
287 size_t offset = StringRef::npos;
288 StringRef A = Arch;
289 StringRef Error = "";
291 // Begins with "arm" / "thumb", move past it.
292 if (A.startswith("arm64"))
293 offset = 5;
294 else if (A.startswith("arm"))
295 offset = 3;
296 else if (A.startswith("thumb"))
297 offset = 5;
298 else if (A.startswith("aarch64")) {
299 offset = 7;
300 // AArch64 uses "_be", not "eb" suffix.
301 if (A.find("eb") != StringRef::npos)
302 return Error;
303 if (A.substr(offset, 3) == "_be")
304 offset += 3;
307 // Ex. "armebv7", move past the "eb".
308 if (offset != StringRef::npos && A.substr(offset, 2) == "eb")
309 offset += 2;
310 // Or, if it ends with eb ("armv7eb"), chop it off.
311 else if (A.endswith("eb"))
312 A = A.substr(0, A.size() - 2);
313 // Trim the head
314 if (offset != StringRef::npos)
315 A = A.substr(offset);
317 // Empty string means offset reached the end, which means it's valid.
318 if (A.empty())
319 return Arch;
321 // Only match non-marketing names
322 if (offset != StringRef::npos) {
323 // Must start with 'vN'.
324 if (A.size() >= 2 && (A[0] != 'v' || !std::isdigit(A[1])))
325 return Error;
326 // Can't have an extra 'eb'.
327 if (A.find("eb") != StringRef::npos)
328 return Error;
331 // Arch will either be a 'v' name (v7a) or a marketing name (xscale).
332 return A;
335 StringRef ARM::getFPUSynonym(StringRef FPU) {
336 return StringSwitch<StringRef>(FPU)
337 .Cases("fpa", "fpe2", "fpe3", "maverick", "invalid") // Unsupported
338 .Case("vfp2", "vfpv2")
339 .Case("vfp3", "vfpv3")
340 .Case("vfp4", "vfpv4")
341 .Case("vfp3-d16", "vfpv3-d16")
342 .Case("vfp4-d16", "vfpv4-d16")
343 .Cases("fp4-sp-d16", "vfpv4-sp-d16", "fpv4-sp-d16")
344 .Cases("fp4-dp-d16", "fpv4-dp-d16", "vfpv4-d16")
345 .Case("fp5-sp-d16", "fpv5-sp-d16")
346 .Cases("fp5-dp-d16", "fpv5-dp-d16", "fpv5-d16")
347 // FIXME: Clang uses it, but it's bogus, since neon defaults to vfpv3.
348 .Case("neon-vfpv3", "neon")
349 .Default(FPU);
352 StringRef ARM::getFPUName(unsigned FPUKind) {
353 if (FPUKind >= FK_LAST)
354 return StringRef();
355 return FPUNames[FPUKind].getName();
358 ARM::FPUVersion ARM::getFPUVersion(unsigned FPUKind) {
359 if (FPUKind >= FK_LAST)
360 return FPUVersion::NONE;
361 return FPUNames[FPUKind].FPUVer;
364 ARM::FPURestriction ARM::getFPURestriction(unsigned FPUKind) {
365 if (FPUKind >= FK_LAST)
366 return FPURestriction::None;
367 return FPUNames[FPUKind].Restriction;
370 unsigned ARM::getDefaultFPU(StringRef CPU, ARM::ArchKind AK) {
371 if (CPU == "generic")
372 return ARM::ARCHNames[static_cast<unsigned>(AK)].DefaultFPU;
374 return StringSwitch<unsigned>(CPU)
375 #define ARM_CPU_NAME(NAME, ID, DEFAULT_FPU, IS_DEFAULT, DEFAULT_EXT) \
376 .Case(NAME, DEFAULT_FPU)
377 #include "llvm/Support/ARMTargetParser.def"
378 .Default(ARM::FK_INVALID);
381 unsigned ARM::getDefaultExtensions(StringRef CPU, ARM::ArchKind AK) {
382 if (CPU == "generic")
383 return ARM::ARCHNames[static_cast<unsigned>(AK)].ArchBaseExtensions;
385 return StringSwitch<unsigned>(CPU)
386 #define ARM_CPU_NAME(NAME, ID, DEFAULT_FPU, IS_DEFAULT, DEFAULT_EXT) \
387 .Case(NAME, \
388 ARCHNames[static_cast<unsigned>(ArchKind::ID)].ArchBaseExtensions | \
389 DEFAULT_EXT)
390 #include "llvm/Support/ARMTargetParser.def"
391 .Default(ARM::AEK_INVALID);
394 bool ARM::getHWDivFeatures(unsigned HWDivKind,
395 std::vector<StringRef> &Features) {
397 if (HWDivKind == AEK_INVALID)
398 return false;
400 if (HWDivKind & AEK_HWDIVARM)
401 Features.push_back("+hwdiv-arm");
402 else
403 Features.push_back("-hwdiv-arm");
405 if (HWDivKind & AEK_HWDIVTHUMB)
406 Features.push_back("+hwdiv");
407 else
408 Features.push_back("-hwdiv");
410 return true;
413 bool ARM::getExtensionFeatures(unsigned Extensions,
414 std::vector<StringRef> &Features) {
416 if (Extensions == AEK_INVALID)
417 return false;
419 if (Extensions & AEK_CRC)
420 Features.push_back("+crc");
421 else
422 Features.push_back("-crc");
424 if (Extensions & AEK_DSP)
425 Features.push_back("+dsp");
426 else
427 Features.push_back("-dsp");
429 if (Extensions & AEK_FP16FML)
430 Features.push_back("+fp16fml");
431 else
432 Features.push_back("-fp16fml");
434 if (Extensions & AEK_RAS)
435 Features.push_back("+ras");
436 else
437 Features.push_back("-ras");
439 if (Extensions & AEK_DOTPROD)
440 Features.push_back("+dotprod");
441 else
442 Features.push_back("-dotprod");
444 return getHWDivFeatures(Extensions, Features);
447 StringRef ARM::getArchName(ARM::ArchKind AK) {
448 return ARCHNames[static_cast<unsigned>(AK)].getName();
451 StringRef ARM::getCPUAttr(ARM::ArchKind AK) {
452 return ARCHNames[static_cast<unsigned>(AK)].getCPUAttr();
455 StringRef ARM::getSubArch(ARM::ArchKind AK) {
456 return ARCHNames[static_cast<unsigned>(AK)].getSubArch();
459 unsigned ARM::getArchAttr(ARM::ArchKind AK) {
460 return ARCHNames[static_cast<unsigned>(AK)].ArchAttr;
463 StringRef ARM::getArchExtName(unsigned ArchExtKind) {
464 for (const auto AE : ARCHExtNames) {
465 if (ArchExtKind == AE.ID)
466 return AE.getName();
468 return StringRef();
471 StringRef ARM::getArchExtFeature(StringRef ArchExt) {
472 if (ArchExt.startswith("no")) {
473 StringRef ArchExtBase(ArchExt.substr(2));
474 for (const auto AE : ARCHExtNames) {
475 if (AE.NegFeature && ArchExtBase == AE.getName())
476 return StringRef(AE.NegFeature);
479 for (const auto AE : ARCHExtNames) {
480 if (AE.Feature && ArchExt == AE.getName())
481 return StringRef(AE.Feature);
484 return StringRef();
487 StringRef ARM::getHWDivName(unsigned HWDivKind) {
488 for (const auto D : HWDivNames) {
489 if (HWDivKind == D.ID)
490 return D.getName();
492 return StringRef();
495 StringRef ARM::getDefaultCPU(StringRef Arch) {
496 ArchKind AK = parseArch(Arch);
497 if (AK == ArchKind::INVALID)
498 return StringRef();
500 // Look for multiple AKs to find the default for pair AK+Name.
501 for (const auto CPU : CPUNames) {
502 if (CPU.ArchID == AK && CPU.Default)
503 return CPU.getName();
506 // If we can't find a default then target the architecture instead
507 return "generic";
510 unsigned ARM::parseHWDiv(StringRef HWDiv) {
511 StringRef Syn = getHWDivSynonym(HWDiv);
512 for (const auto D : HWDivNames) {
513 if (Syn == D.getName())
514 return D.ID;
516 return AEK_INVALID;
519 unsigned ARM::parseArchExt(StringRef ArchExt) {
520 for (const auto A : ARCHExtNames) {
521 if (ArchExt == A.getName())
522 return A.ID;
524 return AEK_INVALID;
527 ARM::ArchKind ARM::parseCPUArch(StringRef CPU) {
528 for (const auto C : CPUNames) {
529 if (CPU == C.getName())
530 return C.ArchID;
532 return ArchKind::INVALID;
535 void ARM::fillValidCPUArchList(SmallVectorImpl<StringRef> &Values) {
536 for (const CpuNames<ArchKind> &Arch : CPUNames) {
537 if (Arch.ArchID != ArchKind::INVALID)
538 Values.push_back(Arch.getName());
542 StringRef ARM::computeDefaultTargetABI(const Triple &TT, StringRef CPU) {
543 StringRef ArchName =
544 CPU.empty() ? TT.getArchName() : getArchName(parseCPUArch(CPU));
546 if (TT.isOSBinFormatMachO()) {
547 if (TT.getEnvironment() == Triple::EABI ||
548 TT.getOS() == Triple::UnknownOS ||
549 parseArchProfile(ArchName) == ProfileKind::M)
550 return "aapcs";
551 if (TT.isWatchABI())
552 return "aapcs16";
553 return "apcs-gnu";
554 } else if (TT.isOSWindows())
555 // FIXME: this is invalid for WindowsCE.
556 return "aapcs";
558 // Select the default based on the platform.
559 switch (TT.getEnvironment()) {
560 case Triple::Android:
561 case Triple::GNUEABI:
562 case Triple::GNUEABIHF:
563 case Triple::MuslEABI:
564 case Triple::MuslEABIHF:
565 return "aapcs-linux";
566 case Triple::EABIHF:
567 case Triple::EABI:
568 return "aapcs";
569 default:
570 if (TT.isOSNetBSD())
571 return "apcs-gnu";
572 if (TT.isOSOpenBSD())
573 return "aapcs-linux";
574 return "aapcs";