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 14 #include "llvm/Support/AArch64TargetParser.h" 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 27 unsigned AArch64::getDefaultFPU(StringRef CPU, AArch64::ArchKind AK) { 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" 35 .Default(ARM::FK_INVALID); 36 } 37 38 uint64_t AArch64::getDefaultExtensions(StringRef CPU, AArch64::ArchKind AK) { 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" 48 .Default(AArch64::AEK_INVALID); 49 } 50 51 AArch64::ArchKind AArch64::getCPUArchKind(StringRef CPU) { 52 if (CPU == "generic") 53 return ArchKind::ARMV8A; 54 55 return StringSwitch<AArch64::ArchKind>(CPU) 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 62 bool AArch64::getExtensionFeatures(uint64_t Extensions, 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 108 return true; 109 } 110 111 bool AArch64::getArchFeatures(AArch64::ArchKind AK, 112 std::vector<StringRef> &Features) { 113 if (AK == ArchKind::ARMV8_1A) 114 Features.push_back("+v8.1a"); 115 if (AK == ArchKind::ARMV8_2A) 116 Features.push_back("+v8.2a"); 117 if (AK == ArchKind::ARMV8_3A) 118 Features.push_back("+v8.3a"); 119 if (AK == ArchKind::ARMV8_4A) 120 Features.push_back("+v8.4a"); 121 if (AK == ArchKind::ARMV8_5A) 122 Features.push_back("+v8.5a"); 123 if (AK == AArch64::ArchKind::ARMV8_6A) 124 Features.push_back("+v8.6a"); 125 if (AK == AArch64::ArchKind::ARMV8_7A) 126 Features.push_back("+v8.7a"); 127 if(AK == AArch64::ArchKind::ARMV8R) 128 Features.push_back("+v8r"); 129 130 return AK != ArchKind::INVALID; 131 } 132 133 StringRef AArch64::getArchName(AArch64::ArchKind AK) { 134 return AArch64ARCHNames[static_cast<unsigned>(AK)].getName(); 135 } 136 137 StringRef AArch64::getCPUAttr(AArch64::ArchKind AK) { 138 return AArch64ARCHNames[static_cast<unsigned>(AK)].getCPUAttr(); 139 } 140 141 StringRef AArch64::getSubArch(AArch64::ArchKind AK) { 142 return AArch64ARCHNames[static_cast<unsigned>(AK)].getSubArch(); 143 } 144 145 unsigned AArch64::getArchAttr(AArch64::ArchKind AK) { 146 return AArch64ARCHNames[static_cast<unsigned>(AK)].ArchAttr; 147 } 148 149 StringRef AArch64::getArchExtName(unsigned ArchExtKind) { 150 for (const auto &AE : AArch64ARCHExtNames) 151 if (ArchExtKind == AE.ID) 152 return AE.getName(); 153 return StringRef(); 154 } 155 156 StringRef AArch64::getArchExtFeature(StringRef ArchExt) { 157 if (ArchExt.startswith("no")) { 158 StringRef ArchExtBase(ArchExt.substr(2)); 159 for (const auto &AE : AArch64ARCHExtNames) { 160 if (AE.NegFeature && ArchExtBase == AE.getName()) 161 return StringRef(AE.NegFeature); 162 } 163 } 164 165 for (const auto &AE : AArch64ARCHExtNames) 166 if (AE.Feature && ArchExt == AE.getName()) 167 return StringRef(AE.Feature); 168 return StringRef(); 169 } 170 171 StringRef AArch64::getDefaultCPU(StringRef Arch) { 172 ArchKind AK = parseArch(Arch); 173 if (AK == ArchKind::INVALID) 174 return StringRef(); 175 176 // Look for multiple AKs to find the default for pair AK+Name. 177 for (const auto &CPU : AArch64CPUNames) 178 if (CPU.ArchID == AK && CPU.Default) 179 return CPU.getName(); 180 181 // If we can't find a default then target the architecture instead 182 return "generic"; 183 } 184 185 void AArch64::fillValidCPUArchList(SmallVectorImpl<StringRef> &Values) { 186 for (const auto &Arch : AArch64CPUNames) { 187 if (Arch.ArchID != ArchKind::INVALID) 188 Values.push_back(Arch.getName()); 189 } 190 } 191 192 bool AArch64::isX18ReservedByDefault(const Triple &TT) { 193 return TT.isAndroid() || TT.isOSDarwin() || TT.isOSFuchsia() || 194 TT.isOSWindows(); 195 } 196 197 // Allows partial match, ex. "v8a" matches "armv8a". 198 AArch64::ArchKind AArch64::parseArch(StringRef Arch) { 199 Arch = ARM::getCanonicalArchName(Arch); 200 if (checkArchVersion(Arch) < 8) 201 return ArchKind::INVALID; 202 203 StringRef Syn = ARM::getArchSynonym(Arch); 204 for (const auto &A : AArch64ARCHNames) { 205 if (A.getName().endswith(Syn)) 206 return A.ID; 207 } 208 return ArchKind::INVALID; 209 } 210 211 AArch64::ArchExtKind AArch64::parseArchExt(StringRef ArchExt) { 212 for (const auto &A : AArch64ARCHExtNames) { 213 if (ArchExt == A.getName()) 214 return static_cast<ArchExtKind>(A.ID); 215 } 216 return AArch64::AEK_INVALID; 217 } 218 219 AArch64::ArchKind AArch64::parseCPUArch(StringRef CPU) { 220 for (const auto &C : AArch64CPUNames) { 221 if (CPU == C.getName()) 222 return C.ArchID; 223 } 224 return ArchKind::INVALID; 225 } 226 227 // Parse a branch protection specification, which has the form 228 // standard | none | [bti,pac-ret[+b-key,+leaf]*] 229 // Returns true on success, with individual elements of the specification 230 // returned in `PBP`. Returns false in error, with `Err` containing 231 // an erroneous part of the spec. 232 bool AArch64::parseBranchProtection(StringRef Spec, ParsedBranchProtection &PBP, 233 StringRef &Err) { 234 PBP = {"none", "a_key", false}; 235 if (Spec == "none") 236 return true; // defaults are ok 237 238 if (Spec == "standard") { 239 PBP.Scope = "non-leaf"; 240 PBP.BranchTargetEnforcement = true; 241 return true; 242 } 243 244 SmallVector<StringRef, 4> Opts; 245 Spec.split(Opts, "+"); 246 for (int I = 0, E = Opts.size(); I != E; ++I) { 247 StringRef Opt = Opts[I].trim(); 248 if (Opt == "bti") { 249 PBP.BranchTargetEnforcement = true; 250 continue; 251 } 252 if (Opt == "pac-ret") { 253 PBP.Scope = "non-leaf"; 254 for (; I + 1 != E; ++I) { 255 StringRef PACOpt = Opts[I + 1].trim(); 256 if (PACOpt == "leaf") 257 PBP.Scope = "all"; 258 else if (PACOpt == "b-key") 259 PBP.Key = "b_key"; 260 else 261 break; 262 } 263 continue; 264 } 265 if (Opt == "") 266 Err = "<empty>"; 267 else 268 Err = Opt; 269 return false; 270 } 271 272 return true; 273 } 274