LLVM  14.0.0git
AArch64TargetParser.cpp
Go to the documentation of this file.
1 //===-- AArch64TargetParser - Parser for AArch64 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 AArch64 hardware features
10 // such as FPU/CPU/ARCH and extension names.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/ADT/Triple.h"
17 #include <cctype>
18 
19 using namespace llvm;
20 
21 static unsigned checkArchVersion(llvm::StringRef Arch) {
22  if (Arch.size() >= 2 && Arch[0] == 'v' && std::isdigit(Arch[1]))
23  return (Arch[1] - 48);
24  return 0;
25 }
26 
28  if (CPU == "generic")
29  return AArch64ARCHNames[static_cast<unsigned>(AK)].DefaultFPU;
30 
31  return StringSwitch<unsigned>(CPU)
32 #define AARCH64_CPU_NAME(NAME, ID, DEFAULT_FPU, IS_DEFAULT, DEFAULT_EXT) \
33  .Case(NAME, ARM::DEFAULT_FPU)
34 #include "../../include/llvm/Support/AArch64TargetParser.def"
36 }
37 
39  if (CPU == "generic")
40  return AArch64ARCHNames[static_cast<unsigned>(AK)].ArchBaseExtensions;
41 
42  return StringSwitch<uint64_t>(CPU)
43 #define AARCH64_CPU_NAME(NAME, ID, DEFAULT_FPU, IS_DEFAULT, DEFAULT_EXT) \
44  .Case(NAME, AArch64ARCHNames[static_cast<unsigned>(ArchKind::ID)] \
45  .ArchBaseExtensions | \
46  DEFAULT_EXT)
47 #include "../../include/llvm/Support/AArch64TargetParser.def"
49 }
50 
52  if (CPU == "generic")
53  return ArchKind::ARMV8A;
54 
56 #define AARCH64_CPU_NAME(NAME, ID, DEFAULT_FPU, IS_DEFAULT, DEFAULT_EXT) \
57  .Case(NAME, ArchKind::ID)
58 #include "../../include/llvm/Support/AArch64TargetParser.def"
59  .Default(ArchKind::INVALID);
60 }
61 
63  std::vector<StringRef> &Features) {
64  if (Extensions == AArch64::AEK_INVALID)
65  return false;
66 
67  if (Extensions & AEK_FP)
68  Features.push_back("+fp-armv8");
69  if (Extensions & AEK_SIMD)
70  Features.push_back("+neon");
71  if (Extensions & AEK_CRC)
72  Features.push_back("+crc");
73  if (Extensions & AEK_CRYPTO)
74  Features.push_back("+crypto");
75  if (Extensions & AEK_DOTPROD)
76  Features.push_back("+dotprod");
77  if (Extensions & AEK_FP16FML)
78  Features.push_back("+fp16fml");
79  if (Extensions & AEK_FP16)
80  Features.push_back("+fullfp16");
81  if (Extensions & AEK_PROFILE)
82  Features.push_back("+spe");
83  if (Extensions & AEK_RAS)
84  Features.push_back("+ras");
85  if (Extensions & AEK_LSE)
86  Features.push_back("+lse");
87  if (Extensions & AEK_RDM)
88  Features.push_back("+rdm");
89  if (Extensions & AEK_SVE)
90  Features.push_back("+sve");
91  if (Extensions & AEK_SVE2)
92  Features.push_back("+sve2");
93  if (Extensions & AEK_SVE2AES)
94  Features.push_back("+sve2-aes");
95  if (Extensions & AEK_SVE2SM4)
96  Features.push_back("+sve2-sm4");
97  if (Extensions & AEK_SVE2SHA3)
98  Features.push_back("+sve2-sha3");
99  if (Extensions & AEK_SVE2BITPERM)
100  Features.push_back("+sve2-bitperm");
101  if (Extensions & AEK_RCPC)
102  Features.push_back("+rcpc");
103  if (Extensions & AEK_BRBE)
104  Features.push_back("+brbe");
105  if (Extensions & AEK_PAUTH)
106  Features.push_back("+pauth");
107  if (Extensions & AEK_FLAGM)
108  Features.push_back("+flagm");
109  if (Extensions & AArch64::AEK_SME)
110  Features.push_back("+sme");
111  if (Extensions & AArch64::AEK_SMEF64)
112  Features.push_back("+sme-f64");
113  if (Extensions & AArch64::AEK_SMEI64)
114  Features.push_back("+sme-i64");
115 
116  return true;
117 }
118 
120  std::vector<StringRef> &Features) {
121  if (AK == ArchKind::ARMV8_1A)
122  Features.push_back("+v8.1a");
123  if (AK == ArchKind::ARMV8_2A)
124  Features.push_back("+v8.2a");
125  if (AK == ArchKind::ARMV8_3A)
126  Features.push_back("+v8.3a");
127  if (AK == ArchKind::ARMV8_4A)
128  Features.push_back("+v8.4a");
129  if (AK == ArchKind::ARMV8_5A)
130  Features.push_back("+v8.5a");
131  if (AK == AArch64::ArchKind::ARMV8_6A)
132  Features.push_back("+v8.6a");
133  if (AK == AArch64::ArchKind::ARMV8_7A)
134  Features.push_back("+v8.7a");
135  if(AK == AArch64::ArchKind::ARMV8R)
136  Features.push_back("+v8r");
137 
138  return AK != ArchKind::INVALID;
139 }
140 
142  return AArch64ARCHNames[static_cast<unsigned>(AK)].getName();
143 }
144 
146  return AArch64ARCHNames[static_cast<unsigned>(AK)].getCPUAttr();
147 }
148 
150  return AArch64ARCHNames[static_cast<unsigned>(AK)].getSubArch();
151 }
152 
154  return AArch64ARCHNames[static_cast<unsigned>(AK)].ArchAttr;
155 }
156 
158  for (const auto &AE : AArch64ARCHExtNames)
159  if (ArchExtKind == AE.ID)
160  return AE.getName();
161  return StringRef();
162 }
163 
165  if (ArchExt.startswith("no")) {
166  StringRef ArchExtBase(ArchExt.substr(2));
167  for (const auto &AE : AArch64ARCHExtNames) {
168  if (AE.NegFeature && ArchExtBase == AE.getName())
169  return StringRef(AE.NegFeature);
170  }
171  }
172 
173  for (const auto &AE : AArch64ARCHExtNames)
174  if (AE.Feature && ArchExt == AE.getName())
175  return StringRef(AE.Feature);
176  return StringRef();
177 }
178 
180  ArchKind AK = parseArch(Arch);
181  if (AK == ArchKind::INVALID)
182  return StringRef();
183 
184  // Look for multiple AKs to find the default for pair AK+Name.
185  for (const auto &CPU : AArch64CPUNames)
186  if (CPU.ArchID == AK && CPU.Default)
187  return CPU.getName();
188 
189  // If we can't find a default then target the architecture instead
190  return "generic";
191 }
192 
194  for (const auto &Arch : AArch64CPUNames) {
195  if (Arch.ArchID != ArchKind::INVALID)
196  Values.push_back(Arch.getName());
197  }
198 }
199 
201  return TT.isAndroid() || TT.isOSDarwin() || TT.isOSFuchsia() ||
202  TT.isOSWindows();
203 }
204 
205 // Allows partial match, ex. "v8a" matches "armv8a".
207  Arch = ARM::getCanonicalArchName(Arch);
208  if (checkArchVersion(Arch) < 8)
209  return ArchKind::INVALID;
210 
211  StringRef Syn = ARM::getArchSynonym(Arch);
212  for (const auto &A : AArch64ARCHNames) {
213  if (A.getName().endswith(Syn))
214  return A.ID;
215  }
216  return ArchKind::INVALID;
217 }
218 
220  for (const auto &A : AArch64ARCHExtNames) {
221  if (ArchExt == A.getName())
222  return static_cast<ArchExtKind>(A.ID);
223  }
224  return AArch64::AEK_INVALID;
225 }
226 
228  for (const auto &C : AArch64CPUNames) {
229  if (CPU == C.getName())
230  return C.ArchID;
231  }
232  return ArchKind::INVALID;
233 }
234 
235 // Parse a branch protection specification, which has the form
236 // standard | none | [bti,pac-ret[+b-key,+leaf]*]
237 // Returns true on success, with individual elements of the specification
238 // returned in `PBP`. Returns false in error, with `Err` containing
239 // an erroneous part of the spec.
241  StringRef &Err) {
242  PBP = {"none", "a_key", false};
243  if (Spec == "none")
244  return true; // defaults are ok
245 
246  if (Spec == "standard") {
247  PBP.Scope = "non-leaf";
248  PBP.BranchTargetEnforcement = true;
249  return true;
250  }
251 
253  Spec.split(Opts, "+");
254  for (int I = 0, E = Opts.size(); I != E; ++I) {
255  StringRef Opt = Opts[I].trim();
256  if (Opt == "bti") {
257  PBP.BranchTargetEnforcement = true;
258  continue;
259  }
260  if (Opt == "pac-ret") {
261  PBP.Scope = "non-leaf";
262  for (; I + 1 != E; ++I) {
263  StringRef PACOpt = Opts[I + 1].trim();
264  if (PACOpt == "leaf")
265  PBP.Scope = "all";
266  else if (PACOpt == "b-key")
267  PBP.Key = "b_key";
268  else
269  break;
270  }
271  continue;
272  }
273  if (Opt == "")
274  Err = "<empty>";
275  else
276  Err = Opt;
277  return false;
278  }
279 
280  return true;
281 }
llvm::AArch64::getArchFeatures
bool getArchFeatures(ArchKind AK, std::vector< StringRef > &Features)
Definition: AArch64TargetParser.cpp:119
llvm::StringRef::startswith
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:286
getName
static StringRef getName(Value *V)
Definition: ProvenanceAnalysisEvaluator.cpp:42
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::AArch64::ArchExtKind
ArchExtKind
Definition: AArch64TargetParser.h:30
llvm::AArch64::AEK_RCPC
@ AEK_RCPC
Definition: AArch64TargetParser.h:43
llvm::AArch64::AEK_SVE2
@ AEK_SVE2
Definition: AArch64TargetParser.h:55
llvm::ARM::getCanonicalArchName
StringRef getCanonicalArchName(StringRef Arch)
Definition: ARMTargetParser.cpp:279
AArch64TargetParser.h
llvm::AArch64::AEK_SIMD
@ AEK_SIMD
Definition: AArch64TargetParser.h:36
llvm::AArch64::AEK_SMEF64
@ AEK_SMEF64
Definition: AArch64TargetParser.h:70
llvm::AArch64::AEK_CRYPTO
@ AEK_CRYPTO
Definition: AArch64TargetParser.h:34
llvm::StringSwitch::Default
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:181
llvm::AArch64::getExtensionFeatures
bool getExtensionFeatures(uint64_t Extensions, std::vector< StringRef > &Features)
Definition: AArch64TargetParser.cpp:62
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
llvm::ARM::getArchSynonym
StringRef getArchSynonym(StringRef Arch)
Definition: ARMTargetParser.cpp:140
llvm::AArch64::getCPUAttr
StringRef getCPUAttr(ArchKind AK)
Definition: AArch64TargetParser.cpp:145
llvm::AArch64::AEK_FP16
@ AEK_FP16
Definition: AArch64TargetParser.h:37
llvm::AArch64::parseArchExt
ArchExtKind parseArchExt(StringRef ArchExt)
Definition: AArch64TargetParser.cpp:219
llvm::AArch64::AEK_SVE2SM4
@ AEK_SVE2SM4
Definition: AArch64TargetParser.h:57
llvm::AArch64::ParsedBranchProtection::Key
StringRef Key
Definition: AArch64TargetParser.h:142
llvm::AArch64::ParsedBranchProtection
Definition: AArch64TargetParser.h:140
llvm::AArch64::getArchExtName
StringRef getArchExtName(unsigned ArchExtKind)
Definition: AArch64TargetParser.cpp:157
llvm::AArch64::getDefaultExtensions
uint64_t getDefaultExtensions(StringRef CPU, ArchKind AK)
Definition: AArch64TargetParser.cpp:38
llvm::AArch64::getDefaultCPU
StringRef getDefaultCPU(StringRef Arch)
Definition: AArch64TargetParser.cpp:179
llvm::AArch64::getArchAttr
unsigned getArchAttr(ArchKind AK)
Definition: AArch64TargetParser.cpp:153
llvm::AArch64::AEK_DOTPROD
@ AEK_DOTPROD
Definition: AArch64TargetParser.h:42
llvm::AArch64::getArchExtFeature
StringRef getArchExtFeature(StringRef ArchExt)
Definition: AArch64TargetParser.cpp:164
llvm::StringRef::substr
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:611
llvm::AArch64::AArch64CPUNames
const ARM::CpuNames< ArchKind > AArch64CPUNames[]
Definition: AArch64TargetParser.h:101
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::AArch64::AEK_INVALID
@ AEK_INVALID
Definition: AArch64TargetParser.h:31
llvm::StringRef::split
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:749
llvm::AArch64::AEK_SVE2AES
@ AEK_SVE2AES
Definition: AArch64TargetParser.h:56
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::AArch64::AEK_SMEI64
@ AEK_SMEI64
Definition: AArch64TargetParser.h:71
llvm::AArch64::ParsedBranchProtection::Scope
StringRef Scope
Definition: AArch64TargetParser.h:141
llvm::AArch64::AEK_SVE2SHA3
@ AEK_SVE2SHA3
Definition: AArch64TargetParser.h:58
llvm::AArch64::AEK_LSE
@ AEK_LSE
Definition: AArch64TargetParser.h:40
llvm::AArch64::ParsedBranchProtection::BranchTargetEnforcement
bool BranchTargetEnforcement
Definition: AArch64TargetParser.h:143
llvm::AArch64::AEK_SME
@ AEK_SME
Definition: AArch64TargetParser.h:69
llvm::AArch64::isX18ReservedByDefault
bool isX18ReservedByDefault(const Triple &TT)
Definition: AArch64TargetParser.cpp:200
llvm::AArch64::AEK_SVE
@ AEK_SVE
Definition: AArch64TargetParser.h:41
llvm::AArch64::getDefaultFPU
unsigned getDefaultFPU(StringRef CPU, ArchKind AK)
Definition: AArch64TargetParser.cpp:27
llvm::AArch64::parseBranchProtection
bool parseBranchProtection(StringRef Spec, ParsedBranchProtection &PBP, StringRef &Err)
Definition: AArch64TargetParser.cpp:240
checkArchVersion
static unsigned checkArchVersion(llvm::StringRef Arch)
Definition: AArch64TargetParser.cpp:21
parseArch
static Triple::ArchType parseArch(StringRef ArchName)
Definition: Triple.cpp:403
uint64_t
llvm::AArch64::AEK_CRC
@ AEK_CRC
Definition: AArch64TargetParser.h:33
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::AArch64::getArchName
StringRef getArchName(ArchKind AK)
Definition: AArch64TargetParser.cpp:141
llvm::AArch64::AEK_RDM
@ AEK_RDM
Definition: AArch64TargetParser.h:44
llvm::AArch64::AArch64ARCHNames
const ARM::ArchNames< ArchKind > AArch64ARCHNames[]
Definition: AArch64TargetParser.h:79
llvm::AArch64::fillValidCPUArchList
void fillValidCPUArchList(SmallVectorImpl< StringRef > &Values)
Definition: AArch64TargetParser.cpp:193
llvm::AArch64::parseCPUArch
ArchKind parseCPUArch(StringRef CPU)
Definition: AArch64TargetParser.cpp:227
llvm::AArch64::AEK_BRBE
@ AEK_BRBE
Definition: AArch64TargetParser.h:66
Triple.h
llvm::AArch64::AEK_RAS
@ AEK_RAS
Definition: AArch64TargetParser.h:39
llvm::AArch64::parseArch
ArchKind parseArch(StringRef Arch)
Definition: AArch64TargetParser.cpp:206
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::AArch64::AArch64ARCHExtNames
const ARM::ExtName AArch64ARCHExtNames[]
Definition: AArch64TargetParser.h:95
llvm::AArch64::AEK_PAUTH
@ AEK_PAUTH
Definition: AArch64TargetParser.h:67
llvm::AArch64::AEK_PROFILE
@ AEK_PROFILE
Definition: AArch64TargetParser.h:38
llvm::RISCV::FK_INVALID
@ FK_INVALID
Definition: TargetParser.h:158
llvm::AArch64::AEK_SVE2BITPERM
@ AEK_SVE2BITPERM
Definition: AArch64TargetParser.h:59
llvm::AArch64::getSubArch
StringRef getSubArch(ArchKind AK)
Definition: AArch64TargetParser.cpp:149
StringSwitch.h
llvm::AArch64::AEK_FLAGM
@ AEK_FLAGM
Definition: AArch64TargetParser.h:68
llvm::AArch64::getCPUArchKind
ArchKind getCPUArchKind(StringRef CPU)
Definition: AArch64TargetParser.cpp:51
llvm::AArch64::AEK_FP
@ AEK_FP
Definition: AArch64TargetParser.h:35
llvm::AArch64::ArchKind
ArchKind
Definition: AArch64TargetParser.h:74
llvm::SmallVectorImpl< StringRef >
llvm::StringSwitch
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
llvm::AArch64::AEK_FP16FML
@ AEK_FP16FML
Definition: AArch64TargetParser.h:49
llvm::StringRef::size
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:157