1 //===--- AArch64.cpp - AArch64 (not ARM) Helpers for Tools ------*- 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 #include "AArch64.h" 10 #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" 15 #include "llvm/Support/Host.h" 16 17 using namespace clang::driver; 18 using namespace clang::driver::tools; 19 using namespace clang; 20 using namespace llvm::opt; 21 22 /// \returns true if the given triple can determine the default CPU type even 23 /// if -arch is not specified. 24 static bool isCPUDeterminedByTriple(const llvm::Triple &Triple) { 25 return Triple.isOSDarwin(); 26 } 27 28 /// getAArch64TargetCPU - Get the (LLVM) name of the AArch64 cpu we are 29 /// targeting. Set \p A to the Arg corresponding to the -mcpu argument if it is 30 /// provided, or to nullptr otherwise. 31 std::string aarch64::getAArch64TargetCPU(const ArgList &Args, 32 const llvm::Triple &Triple, Arg *&A) { 33 std::string CPU; 34 // If we have -mcpu, use that. 35 if ((A = Args.getLastArg(options::OPT_mcpu_EQ))) { 36 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 46 // 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()) 49 return "cyclone"; 50 51 return "generic"; 52 } 53 54 // Decode AArch64 features from string like +[no]featureA+[no]featureB+... 55 static 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. 74 static 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 79 if (CPU == "native") 80 CPU = llvm::sys::getHostCPUName(); 81 82 if (CPU == "generic") { 83 Features.push_back("+neon"); 84 } else { 85 llvm::AArch64::ArchKind ArchKind = llvm::AArch64::parseCPUArch(CPU); 86 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 100 static bool 101 getAArch64ArchFeaturesFromMarch(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 107 llvm::AArch64::ArchKind ArchKind = llvm::AArch64::parseArch(Split.first); 108 if (ArchKind == llvm::AArch64::ArchKind::INVALID || 109 !llvm::AArch64::getArchFeatures(ArchKind, Features) || 110 (Split.second.size() && !DecodeAArch64Features(D, Split.second, Features))) 111 return false; 112 113 return true; 114 } 115 116 static bool 117 getAArch64ArchFeaturesFromMcpu(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 128 static bool 129 getAArch64MicroArchFeaturesFromMtune(const Driver &D, StringRef Mtune, 130 const ArgList &Args, 131 std::vector<StringRef> &Features) { 132 std::string MtuneLowerCase = Mtune.lower(); 133 // Check CPU name is valid 134 std::vector<StringRef> MtuneFeatures; 135 StringRef Tune; 136 if (!DecodeAArch64Mcpu(D, MtuneLowerCase, Tune, MtuneFeatures)) 137 return false; 138 139 // 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 149 static bool 150 getAArch64MicroArchFeaturesFromMcpu(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 162 void aarch64::getAArch64TargetFeatures(const Driver &D, 163 const llvm::Triple &Triple, 164 const ArgList &Args, 165 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); 174 else if (Args.hasArg(options::OPT_arch) || isCPUDeterminedByTriple(Triple)) 175 success = getAArch64ArchFeaturesFromMcpu( 176 D, getAArch64TargetCPU(Args, Triple, A), Args, Features); 177 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); 184 else if (success && 185 (Args.hasArg(options::OPT_arch) || isCPUDeterminedByTriple(Triple))) 186 success = getAArch64MicroArchFeaturesFromMcpu( 187 D, getAArch64TargetCPU(Args, Triple, A), Args, Features); 188 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 198 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 210 // 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 218 // 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"); 223 if (llvm::is_contained(Features, "+v8.4a")) { 224 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; 233 } else { 234 fp16_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 243 // 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 324 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 329 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 350 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 371 if (Args.hasArg(options::OPT_ffixed_x18)) 372 Features.push_back("+reserve-x18"); 373 374 if (Args.hasArg(options::OPT_ffixed_x20)) 375 Features.push_back("+reserve-x20"); 376 377 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 389 if (Args.hasArg(options::OPT_ffixed_x25)) 390 Features.push_back("+reserve-x25"); 391 392 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 401 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 428 if (Args.hasArg(options::OPT_mno_neg_immediates)) 429 Features.push_back("+no-neg-immediates"); 430 } 431