1 //===--- ARM.cpp - ARM (not AArch64) 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 "ARM.h" 10 #include "clang/Driver/Driver.h" 11 #include "clang/Driver/DriverDiagnostic.h" 12 #include "clang/Driver/Options.h" 13 #include "llvm/ADT/StringSwitch.h" 14 #include "llvm/Option/ArgList.h" 15 #include "llvm/Support/TargetParser.h" 16 #include "llvm/Support/Host.h" 17 18 using namespace clang::driver; 19 using namespace clang::driver::tools; 20 using namespace clang; 21 using namespace llvm::opt; 22 23 // Get SubArch (vN). 24 int arm::getARMSubArchVersionNumber(const llvm::Triple &Triple) { 25 llvm::StringRef Arch = Triple.getArchName(); 26 return llvm::ARM::parseArchVersion(Arch); 27 } 28 29 // True if M-profile. 30 bool arm::isARMMProfile(const llvm::Triple &Triple) { 31 llvm::StringRef Arch = Triple.getArchName(); 32 return llvm::ARM::parseArchProfile(Arch) == llvm::ARM::ProfileKind::M; 33 } 34 35 // True if A-profile. 36 bool arm::isARMAProfile(const llvm::Triple &Triple) { 37 llvm::StringRef Arch = Triple.getArchName(); 38 return llvm::ARM::parseArchProfile(Arch) == llvm::ARM::ProfileKind::A; 39 } 40 41 // Get Arch/CPU from args. 42 void arm::getARMArchCPUFromArgs(const ArgList &Args, llvm::StringRef &Arch, 43 llvm::StringRef &CPU, bool FromAs) { 44 if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mcpu_EQ)) 45 CPU = A->getValue(); 46 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) 47 Arch = A->getValue(); 48 if (!FromAs) 49 return; 50 51 for (const Arg *A : 52 Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) { 53 // Use getValues because -Wa can have multiple arguments 54 // e.g. -Wa,-mcpu=foo,-mcpu=bar 55 for (StringRef Value : A->getValues()) { 56 if (Value.startswith("-mcpu=")) 57 CPU = Value.substr(6); 58 if (Value.startswith("-march=")) 59 Arch = Value.substr(7); 60 } 61 } 62 } 63 64 // Handle -mhwdiv=. 65 // FIXME: Use ARMTargetParser. 66 static void getARMHWDivFeatures(const Driver &D, const Arg *A, 67 const ArgList &Args, StringRef HWDiv, 68 std::vector<StringRef> &Features) { 69 uint64_t HWDivID = llvm::ARM::parseHWDiv(HWDiv); 70 if (!llvm::ARM::getHWDivFeatures(HWDivID, Features)) 71 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args); 72 } 73 74 // Handle -mfpu=. 75 static unsigned getARMFPUFeatures(const Driver &D, const Arg *A, 76 const ArgList &Args, StringRef FPU, 77 std::vector<StringRef> &Features) { 78 unsigned FPUID = llvm::ARM::parseFPU(FPU); 79 if (!llvm::ARM::getFPUFeatures(FPUID, Features)) 80 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args); 81 return FPUID; 82 } 83 84 // Decode ARM features from string like +[no]featureA+[no]featureB+... 85 static bool DecodeARMFeatures(const Driver &D, StringRef text, StringRef CPU, 86 llvm::ARM::ArchKind ArchKind, 87 std::vector<StringRef> &Features, 88 unsigned &ArgFPUID) { 89 SmallVector<StringRef, 8> Split; 90 text.split(Split, StringRef("+"), -1, false); 91 92 for (StringRef Feature : Split) { 93 if (!appendArchExtFeatures(CPU, ArchKind, Feature, Features, ArgFPUID)) 94 return false; 95 } 96 return true; 97 } 98 99 static void DecodeARMFeaturesFromCPU(const Driver &D, StringRef CPU, 100 std::vector<StringRef> &Features) { 101 CPU = CPU.split("+").first; 102 if (CPU != "generic") { 103 llvm::ARM::ArchKind ArchKind = llvm::ARM::parseCPUArch(CPU); 104 uint64_t Extension = llvm::ARM::getDefaultExtensions(CPU, ArchKind); 105 llvm::ARM::getExtensionFeatures(Extension, Features); 106 } 107 } 108 109 // Check if -march is valid by checking if it can be canonicalised and parsed. 110 // getARMArch is used here instead of just checking the -march value in order 111 // to handle -march=native correctly. 112 static void checkARMArchName(const Driver &D, const Arg *A, const ArgList &Args, 113 llvm::StringRef ArchName, llvm::StringRef CPUName, 114 std::vector<StringRef> &Features, 115 const llvm::Triple &Triple, unsigned &ArgFPUID) { 116 std::pair<StringRef, StringRef> Split = ArchName.split("+"); 117 118 std::string MArch = arm::getARMArch(ArchName, Triple); 119 llvm::ARM::ArchKind ArchKind = llvm::ARM::parseArch(MArch); 120 if (ArchKind == llvm::ARM::ArchKind::INVALID || 121 (Split.second.size() && !DecodeARMFeatures(D, Split.second, CPUName, 122 ArchKind, Features, ArgFPUID))) 123 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args); 124 } 125 126 // Check -mcpu=. Needs ArchName to handle -mcpu=generic. 127 static void checkARMCPUName(const Driver &D, const Arg *A, const ArgList &Args, 128 llvm::StringRef CPUName, llvm::StringRef ArchName, 129 std::vector<StringRef> &Features, 130 const llvm::Triple &Triple, unsigned &ArgFPUID) { 131 std::pair<StringRef, StringRef> Split = CPUName.split("+"); 132 133 std::string CPU = arm::getARMTargetCPU(CPUName, ArchName, Triple); 134 llvm::ARM::ArchKind ArchKind = 135 arm::getLLVMArchKindForARM(CPU, ArchName, Triple); 136 if (ArchKind == llvm::ARM::ArchKind::INVALID || 137 (Split.second.size() && 138 !DecodeARMFeatures(D, Split.second, CPU, ArchKind, Features, ArgFPUID))) 139 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args); 140 } 141 142 bool arm::useAAPCSForMachO(const llvm::Triple &T) { 143 // The backend is hardwired to assume AAPCS for M-class processors, ensure 144 // the frontend matches that. 145 return T.getEnvironment() == llvm::Triple::EABI || 146 T.getEnvironment() == llvm::Triple::EABIHF || 147 T.getOS() == llvm::Triple::UnknownOS || isARMMProfile(T); 148 } 149 150 // We follow GCC and support when the backend has support for the MRC/MCR 151 // instructions that are used to set the hard thread pointer ("CP15 C13 152 // Thread id"). 153 bool arm::isHardTPSupported(const llvm::Triple &Triple) { 154 int Ver = getARMSubArchVersionNumber(Triple); 155 llvm::ARM::ArchKind AK = llvm::ARM::parseArch(Triple.getArchName()); 156 return Triple.isARM() || AK == llvm::ARM::ArchKind::ARMV6T2 || 157 (Ver >= 7 && AK != llvm::ARM::ArchKind::ARMV8MBaseline); 158 } 159 160 // Select mode for reading thread pointer (-mtp=soft/cp15). 161 arm::ReadTPMode arm::getReadTPMode(const Driver &D, const ArgList &Args, 162 const llvm::Triple &Triple, bool ForAS) { 163 if (Arg *A = Args.getLastArg(options::OPT_mtp_mode_EQ)) { 164 arm::ReadTPMode ThreadPointer = 165 llvm::StringSwitch<arm::ReadTPMode>(A->getValue()) 166 .Case("cp15", ReadTPMode::Cp15) 167 .Case("soft", ReadTPMode::Soft) 168 .Default(ReadTPMode::Invalid); 169 if (ThreadPointer == ReadTPMode::Cp15 && !isHardTPSupported(Triple) && 170 !ForAS) { 171 D.Diag(diag::err_target_unsupported_tp_hard) << Triple.getArchName(); 172 return ReadTPMode::Invalid; 173 } 174 if (ThreadPointer != ReadTPMode::Invalid) 175 return ThreadPointer; 176 if (StringRef(A->getValue()).empty()) 177 D.Diag(diag::err_drv_missing_arg_mtp) << A->getAsString(Args); 178 else 179 D.Diag(diag::err_drv_invalid_mtp) << A->getAsString(Args); 180 return ReadTPMode::Invalid; 181 } 182 return ReadTPMode::Soft; 183 } 184 185 void arm::setArchNameInTriple(const Driver &D, const ArgList &Args, 186 types::ID InputType, llvm::Triple &Triple) { 187 StringRef MCPU, MArch; 188 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) 189 MCPU = A->getValue(); 190 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) 191 MArch = A->getValue(); 192 193 std::string CPU = Triple.isOSBinFormatMachO() 194 ? tools::arm::getARMCPUForMArch(MArch, Triple).str() 195 : tools::arm::getARMTargetCPU(MCPU, MArch, Triple); 196 StringRef Suffix = tools::arm::getLLVMArchSuffixForARM(CPU, MArch, Triple); 197 198 bool IsBigEndian = Triple.getArch() == llvm::Triple::armeb || 199 Triple.getArch() == llvm::Triple::thumbeb; 200 // Handle pseudo-target flags '-mlittle-endian'/'-EL' and 201 // '-mbig-endian'/'-EB'. 202 if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian, 203 options::OPT_mbig_endian)) { 204 IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian); 205 } 206 std::string ArchName = IsBigEndian ? "armeb" : "arm"; 207 208 // FIXME: Thumb should just be another -target-feaure, not in the triple. 209 bool IsMProfile = 210 llvm::ARM::parseArchProfile(Suffix) == llvm::ARM::ProfileKind::M; 211 bool ThumbDefault = IsMProfile || 212 // Thumb2 is the default for V7 on Darwin. 213 (llvm::ARM::parseArchVersion(Suffix) == 7 && 214 Triple.isOSBinFormatMachO()) || 215 // FIXME: this is invalid for WindowsCE 216 Triple.isOSWindows(); 217 218 // Check if ARM ISA was explicitly selected (using -mno-thumb or -marm) for 219 // M-Class CPUs/architecture variants, which is not supported. 220 bool ARMModeRequested = 221 !Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault); 222 if (IsMProfile && ARMModeRequested) { 223 if (MCPU.size()) 224 D.Diag(diag::err_cpu_unsupported_isa) << CPU << "ARM"; 225 else 226 D.Diag(diag::err_arch_unsupported_isa) 227 << tools::arm::getARMArch(MArch, Triple) << "ARM"; 228 } 229 230 // Check to see if an explicit choice to use thumb has been made via 231 // -mthumb. For assembler files we must check for -mthumb in the options 232 // passed to the assembler via -Wa or -Xassembler. 233 bool IsThumb = false; 234 if (InputType != types::TY_PP_Asm) 235 IsThumb = 236 Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault); 237 else { 238 // Ideally we would check for these flags in 239 // CollectArgsForIntegratedAssembler but we can't change the ArchName at 240 // that point. 241 llvm::StringRef WaMArch, WaMCPU; 242 for (const auto *A : 243 Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) { 244 for (StringRef Value : A->getValues()) { 245 // There is no assembler equivalent of -mno-thumb, -marm, or -mno-arm. 246 if (Value == "-mthumb") 247 IsThumb = true; 248 else if (Value.startswith("-march=")) 249 WaMArch = Value.substr(7); 250 else if (Value.startswith("-mcpu=")) 251 WaMCPU = Value.substr(6); 252 } 253 } 254 255 if (WaMCPU.size() || WaMArch.size()) { 256 // The way this works means that we prefer -Wa,-mcpu's architecture 257 // over -Wa,-march. Which matches the compiler behaviour. 258 Suffix = tools::arm::getLLVMArchSuffixForARM(WaMCPU, WaMArch, Triple); 259 } 260 } 261 262 // Assembly files should start in ARM mode, unless arch is M-profile, or 263 // -mthumb has been passed explicitly to the assembler. Windows is always 264 // thumb. 265 if (IsThumb || IsMProfile || Triple.isOSWindows()) { 266 if (IsBigEndian) 267 ArchName = "thumbeb"; 268 else 269 ArchName = "thumb"; 270 } 271 Triple.setArchName(ArchName + Suffix.str()); 272 } 273 274 void arm::setFloatABIInTriple(const Driver &D, const ArgList &Args, 275 llvm::Triple &Triple) { 276 bool isHardFloat = 277 (arm::getARMFloatABI(D, Triple, Args) == arm::FloatABI::Hard); 278 279 switch (Triple.getEnvironment()) { 280 case llvm::Triple::GNUEABI: 281 case llvm::Triple::GNUEABIHF: 282 Triple.setEnvironment(isHardFloat ? llvm::Triple::GNUEABIHF 283 : llvm::Triple::GNUEABI); 284 break; 285 case llvm::Triple::EABI: 286 case llvm::Triple::EABIHF: 287 Triple.setEnvironment(isHardFloat ? llvm::Triple::EABIHF 288 : llvm::Triple::EABI); 289 break; 290 case llvm::Triple::MuslEABI: 291 case llvm::Triple::MuslEABIHF: 292 Triple.setEnvironment(isHardFloat ? llvm::Triple::MuslEABIHF 293 : llvm::Triple::MuslEABI); 294 break; 295 default: { 296 arm::FloatABI DefaultABI = arm::getDefaultFloatABI(Triple); 297 if (DefaultABI != arm::FloatABI::Invalid && 298 isHardFloat != (DefaultABI == arm::FloatABI::Hard)) { 299 Arg *ABIArg = 300 Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float, 301 options::OPT_mfloat_abi_EQ); 302 assert(ABIArg && "Non-default float abi expected to be from arg"); 303 D.Diag(diag::err_drv_unsupported_opt_for_target) 304 << ABIArg->getAsString(Args) << Triple.getTriple(); 305 } 306 break; 307 } 308 } 309 } 310 311 arm::FloatABI arm::getARMFloatABI(const ToolChain &TC, const ArgList &Args) { 312 return arm::getARMFloatABI(TC.getDriver(), TC.getEffectiveTriple(), Args); 313 } 314 315 arm::FloatABI arm::getDefaultFloatABI(const llvm::Triple &Triple) { 316 auto SubArch = getARMSubArchVersionNumber(Triple); 317 switch (Triple.getOS()) { 318 case llvm::Triple::Darwin: 319 case llvm::Triple::MacOSX: 320 case llvm::Triple::IOS: 321 case llvm::Triple::TvOS: 322 // Darwin defaults to "softfp" for v6 and v7. 323 if (Triple.isWatchABI()) 324 return FloatABI::Hard; 325 else 326 return (SubArch == 6 || SubArch == 7) ? FloatABI::SoftFP : FloatABI::Soft; 327 328 case llvm::Triple::WatchOS: 329 return FloatABI::Hard; 330 331 // FIXME: this is invalid for WindowsCE 332 case llvm::Triple::Win32: 333 // It is incorrect to select hard float ABI on MachO platforms if the ABI is 334 // "apcs-gnu". 335 if (Triple.isOSBinFormatMachO() && !useAAPCSForMachO(Triple)) 336 return FloatABI::Soft; 337 return FloatABI::Hard; 338 339 case llvm::Triple::NetBSD: 340 switch (Triple.getEnvironment()) { 341 case llvm::Triple::EABIHF: 342 case llvm::Triple::GNUEABIHF: 343 return FloatABI::Hard; 344 default: 345 return FloatABI::Soft; 346 } 347 break; 348 349 case llvm::Triple::FreeBSD: 350 switch (Triple.getEnvironment()) { 351 case llvm::Triple::GNUEABIHF: 352 return FloatABI::Hard; 353 default: 354 // FreeBSD defaults to soft float 355 return FloatABI::Soft; 356 } 357 break; 358 359 case llvm::Triple::OpenBSD: 360 return FloatABI::SoftFP; 361 362 default: 363 switch (Triple.getEnvironment()) { 364 case llvm::Triple::GNUEABIHF: 365 case llvm::Triple::MuslEABIHF: 366 case llvm::Triple::EABIHF: 367 return FloatABI::Hard; 368 case llvm::Triple::GNUEABI: 369 case llvm::Triple::MuslEABI: 370 case llvm::Triple::EABI: 371 // EABI is always AAPCS, and if it was not marked 'hard', it's softfp 372 return FloatABI::SoftFP; 373 case llvm::Triple::Android: 374 return (SubArch >= 7) ? FloatABI::SoftFP : FloatABI::Soft; 375 default: 376 return FloatABI::Invalid; 377 } 378 } 379 return FloatABI::Invalid; 380 } 381 382 // Select the float ABI as determined by -msoft-float, -mhard-float, and 383 // -mfloat-abi=. 384 arm::FloatABI arm::getARMFloatABI(const Driver &D, const llvm::Triple &Triple, 385 const ArgList &Args) { 386 arm::FloatABI ABI = FloatABI::Invalid; 387 if (Arg *A = 388 Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float, 389 options::OPT_mfloat_abi_EQ)) { 390 if (A->getOption().matches(options::OPT_msoft_float)) { 391 ABI = FloatABI::Soft; 392 } else if (A->getOption().matches(options::OPT_mhard_float)) { 393 ABI = FloatABI::Hard; 394 } else { 395 ABI = llvm::StringSwitch<arm::FloatABI>(A->getValue()) 396 .Case("soft", FloatABI::Soft) 397 .Case("softfp", FloatABI::SoftFP) 398 .Case("hard", FloatABI::Hard) 399 .Default(FloatABI::Invalid); 400 if (ABI == FloatABI::Invalid && !StringRef(A->getValue()).empty()) { 401 D.Diag(diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args); 402 ABI = FloatABI::Soft; 403 } 404 } 405 } 406 407 // If unspecified, choose the default based on the platform. 408 if (ABI == FloatABI::Invalid) 409 ABI = arm::getDefaultFloatABI(Triple); 410 411 if (ABI == FloatABI::Invalid) { 412 // Assume "soft", but warn the user we are guessing. 413 if (Triple.isOSBinFormatMachO() && 414 Triple.getSubArch() == llvm::Triple::ARMSubArch_v7em) 415 ABI = FloatABI::Hard; 416 else 417 ABI = FloatABI::Soft; 418 419 if (Triple.getOS() != llvm::Triple::UnknownOS || 420 !Triple.isOSBinFormatMachO()) 421 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft"; 422 } 423 424 assert(ABI != FloatABI::Invalid && "must select an ABI"); 425 return ABI; 426 } 427 428 static bool hasIntegerMVE(const std::vector<StringRef> &F) { 429 auto MVE = llvm::find(llvm::reverse(F), "+mve"); 430 auto NoMVE = llvm::find(llvm::reverse(F), "-mve"); 431 return MVE != F.rend() && 432 (NoMVE == F.rend() || std::distance(MVE, NoMVE) > 0); 433 } 434 435 void arm::getARMTargetFeatures(const Driver &D, const llvm::Triple &Triple, 436 const ArgList &Args, ArgStringList &CmdArgs, 437 std::vector<StringRef> &Features, bool ForAS) { 438 bool KernelOrKext = 439 Args.hasArg(options::OPT_mkernel, options::OPT_fapple_kext); 440 arm::FloatABI ABI = arm::getARMFloatABI(D, Triple, Args); 441 llvm::Optional<std::pair<const Arg *, StringRef>> WaCPU, WaFPU, WaHDiv, 442 WaArch; 443 444 // This vector will accumulate features from the architecture 445 // extension suffixes on -mcpu and -march (e.g. the 'bar' in 446 // -mcpu=foo+bar). We want to apply those after the features derived 447 // from the FPU, in case -mfpu generates a negative feature which 448 // the +bar is supposed to override. 449 std::vector<StringRef> ExtensionFeatures; 450 451 if (!ForAS) { 452 // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these 453 // yet (it uses the -mfloat-abi and -msoft-float options), and it is 454 // stripped out by the ARM target. We should probably pass this a new 455 // -target-option, which is handled by the -cc1/-cc1as invocation. 456 // 457 // FIXME2: For consistency, it would be ideal if we set up the target 458 // machine state the same when using the frontend or the assembler. We don't 459 // currently do that for the assembler, we pass the options directly to the 460 // backend and never even instantiate the frontend TargetInfo. If we did, 461 // and used its handleTargetFeatures hook, then we could ensure the 462 // assembler and the frontend behave the same. 463 464 // Use software floating point operations? 465 if (ABI == arm::FloatABI::Soft) 466 Features.push_back("+soft-float"); 467 468 // Use software floating point argument passing? 469 if (ABI != arm::FloatABI::Hard) 470 Features.push_back("+soft-float-abi"); 471 } else { 472 // Here, we make sure that -Wa,-mfpu/cpu/arch/hwdiv will be passed down 473 // to the assembler correctly. 474 for (const Arg *A : 475 Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) { 476 // We use getValues here because you can have many options per -Wa 477 // We will keep the last one we find for each of these 478 for (StringRef Value : A->getValues()) { 479 if (Value.startswith("-mfpu=")) { 480 WaFPU = std::make_pair(A, Value.substr(6)); 481 } else if (Value.startswith("-mcpu=")) { 482 WaCPU = std::make_pair(A, Value.substr(6)); 483 } else if (Value.startswith("-mhwdiv=")) { 484 WaHDiv = std::make_pair(A, Value.substr(8)); 485 } else if (Value.startswith("-march=")) { 486 WaArch = std::make_pair(A, Value.substr(7)); 487 } 488 } 489 } 490 } 491 492 if (getReadTPMode(D, Args, Triple, ForAS) == ReadTPMode::Cp15) 493 Features.push_back("+read-tp-hard"); 494 495 const Arg *ArchArg = Args.getLastArg(options::OPT_march_EQ); 496 const Arg *CPUArg = Args.getLastArg(options::OPT_mcpu_EQ); 497 StringRef ArchName; 498 StringRef CPUName; 499 unsigned ArchArgFPUID = llvm::ARM::FK_INVALID; 500 unsigned CPUArgFPUID = llvm::ARM::FK_INVALID; 501 502 // Check -mcpu. ClangAs gives preference to -Wa,-mcpu=. 503 if (WaCPU) { 504 if (CPUArg) 505 D.Diag(clang::diag::warn_drv_unused_argument) 506 << CPUArg->getAsString(Args); 507 CPUName = WaCPU->second; 508 CPUArg = WaCPU->first; 509 } else if (CPUArg) 510 CPUName = CPUArg->getValue(); 511 512 // Check -march. ClangAs gives preference to -Wa,-march=. 513 if (WaArch) { 514 if (ArchArg) 515 D.Diag(clang::diag::warn_drv_unused_argument) 516 << ArchArg->getAsString(Args); 517 ArchName = WaArch->second; 518 // This will set any features after the base architecture. 519 checkARMArchName(D, WaArch->first, Args, ArchName, CPUName, 520 ExtensionFeatures, Triple, ArchArgFPUID); 521 // The base architecture was handled in ToolChain::ComputeLLVMTriple because 522 // triple is read only by this point. 523 } else if (ArchArg) { 524 ArchName = ArchArg->getValue(); 525 checkARMArchName(D, ArchArg, Args, ArchName, CPUName, ExtensionFeatures, 526 Triple, ArchArgFPUID); 527 } 528 529 // Add CPU features for generic CPUs 530 if (CPUName == "native") { 531 llvm::StringMap<bool> HostFeatures; 532 if (llvm::sys::getHostCPUFeatures(HostFeatures)) 533 for (auto &F : HostFeatures) 534 Features.push_back( 535 Args.MakeArgString((F.second ? "+" : "-") + F.first())); 536 } else if (!CPUName.empty()) { 537 // This sets the default features for the specified CPU. We certainly don't 538 // want to override the features that have been explicitly specified on the 539 // command line. Therefore, process them directly instead of appending them 540 // at the end later. 541 DecodeARMFeaturesFromCPU(D, CPUName, Features); 542 } 543 544 if (CPUArg) 545 checkARMCPUName(D, CPUArg, Args, CPUName, ArchName, ExtensionFeatures, 546 Triple, CPUArgFPUID); 547 // Honor -mfpu=. ClangAs gives preference to -Wa,-mfpu=. 548 unsigned FPUID = llvm::ARM::FK_INVALID; 549 const Arg *FPUArg = Args.getLastArg(options::OPT_mfpu_EQ); 550 if (WaFPU) { 551 if (FPUArg) 552 D.Diag(clang::diag::warn_drv_unused_argument) 553 << FPUArg->getAsString(Args); 554 (void)getARMFPUFeatures(D, WaFPU->first, Args, WaFPU->second, Features); 555 } else if (FPUArg) { 556 FPUID = getARMFPUFeatures(D, FPUArg, Args, FPUArg->getValue(), Features); 557 } else if (Triple.isAndroid() && getARMSubArchVersionNumber(Triple) >= 7) { 558 const char *AndroidFPU = "neon"; 559 FPUID = llvm::ARM::parseFPU(AndroidFPU); 560 if (!llvm::ARM::getFPUFeatures(FPUID, Features)) 561 D.Diag(clang::diag::err_drv_clang_unsupported) 562 << std::string("-mfpu=") + AndroidFPU; 563 } else { 564 if (!ForAS) { 565 std::string CPU = arm::getARMTargetCPU(CPUName, ArchName, Triple); 566 llvm::ARM::ArchKind ArchKind = 567 arm::getLLVMArchKindForARM(CPU, ArchName, Triple); 568 FPUID = llvm::ARM::getDefaultFPU(CPU, ArchKind); 569 (void)llvm::ARM::getFPUFeatures(FPUID, Features); 570 } 571 } 572 573 // Now we've finished accumulating features from arch, cpu and fpu, 574 // we can append the ones for architecture extensions that we 575 // collected separately. 576 Features.insert(std::end(Features), 577 std::begin(ExtensionFeatures), std::end(ExtensionFeatures)); 578 579 // Honor -mhwdiv=. ClangAs gives preference to -Wa,-mhwdiv=. 580 const Arg *HDivArg = Args.getLastArg(options::OPT_mhwdiv_EQ); 581 if (WaHDiv) { 582 if (HDivArg) 583 D.Diag(clang::diag::warn_drv_unused_argument) 584 << HDivArg->getAsString(Args); 585 getARMHWDivFeatures(D, WaHDiv->first, Args, WaHDiv->second, Features); 586 } else if (HDivArg) 587 getARMHWDivFeatures(D, HDivArg, Args, HDivArg->getValue(), Features); 588 589 // Handle (arch-dependent) fp16fml/fullfp16 relationship. 590 // Must happen before any features are disabled due to soft-float. 591 // FIXME: this fp16fml option handling will be reimplemented after the 592 // TargetParser rewrite. 593 const auto ItRNoFullFP16 = std::find(Features.rbegin(), Features.rend(), "-fullfp16"); 594 const auto ItRFP16FML = std::find(Features.rbegin(), Features.rend(), "+fp16fml"); 595 if (Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v8_4a) { 596 const auto ItRFullFP16 = std::find(Features.rbegin(), Features.rend(), "+fullfp16"); 597 if (ItRFullFP16 < ItRNoFullFP16 && ItRFullFP16 < ItRFP16FML) { 598 // Only entangled feature that can be to the right of this +fullfp16 is -fp16fml. 599 // Only append the +fp16fml if there is no -fp16fml after the +fullfp16. 600 if (std::find(Features.rbegin(), ItRFullFP16, "-fp16fml") == ItRFullFP16) 601 Features.push_back("+fp16fml"); 602 } 603 else 604 goto fp16_fml_fallthrough; 605 } 606 else { 607 fp16_fml_fallthrough: 608 // In both of these cases, putting the 'other' feature on the end of the vector will 609 // result in the same effect as placing it immediately after the current feature. 610 if (ItRNoFullFP16 < ItRFP16FML) 611 Features.push_back("-fp16fml"); 612 else if (ItRNoFullFP16 > ItRFP16FML) 613 Features.push_back("+fullfp16"); 614 } 615 616 // Setting -msoft-float/-mfloat-abi=soft, -mfpu=none, or adding +nofp to 617 // -march/-mcpu effectively disables the FPU (GCC ignores the -mfpu options in 618 // this case). Note that the ABI can also be set implicitly by the target 619 // selected. 620 if (ABI == arm::FloatABI::Soft) { 621 llvm::ARM::getFPUFeatures(llvm::ARM::FK_NONE, Features); 622 623 // Disable all features relating to hardware FP, not already disabled by the 624 // above call. 625 Features.insert(Features.end(), {"-dotprod", "-fp16fml", "-bf16", "-mve", 626 "-mve.fp", "-fpregs"}); 627 } else if (FPUID == llvm::ARM::FK_NONE || 628 ArchArgFPUID == llvm::ARM::FK_NONE || 629 CPUArgFPUID == llvm::ARM::FK_NONE) { 630 // -mfpu=none, -march=armvX+nofp or -mcpu=X+nofp is *very* similar to 631 // -mfloat-abi=soft, only that it should not disable MVE-I. They disable the 632 // FPU, but not the FPU registers, thus MVE-I, which depends only on the 633 // latter, is still supported. 634 Features.insert(Features.end(), 635 {"-dotprod", "-fp16fml", "-bf16", "-mve.fp"}); 636 if (!hasIntegerMVE(Features)) 637 Features.emplace_back("-fpregs"); 638 } 639 640 // En/disable crc code generation. 641 if (Arg *A = Args.getLastArg(options::OPT_mcrc, options::OPT_mnocrc)) { 642 if (A->getOption().matches(options::OPT_mcrc)) 643 Features.push_back("+crc"); 644 else 645 Features.push_back("-crc"); 646 } 647 648 // For Arch >= ARMv8.0 && A or R profile: crypto = sha2 + aes 649 // Rather than replace within the feature vector, determine whether each 650 // algorithm is enabled and append this to the end of the vector. 651 // The algorithms can be controlled by their specific feature or the crypto 652 // feature, so their status can be determined by the last occurance of 653 // either in the vector. This allows one to supercede the other. 654 // e.g. +crypto+noaes in -march/-mcpu should enable sha2, but not aes 655 // FIXME: this needs reimplementation after the TargetParser rewrite 656 bool HasSHA2 = false; 657 bool HasAES = false; 658 const auto ItCrypto = 659 llvm::find_if(llvm::reverse(Features), [](const StringRef F) { 660 return F.contains("crypto"); 661 }); 662 const auto ItSHA2 = 663 llvm::find_if(llvm::reverse(Features), [](const StringRef F) { 664 return F.contains("crypto") || F.contains("sha2"); 665 }); 666 const auto ItAES = 667 llvm::find_if(llvm::reverse(Features), [](const StringRef F) { 668 return F.contains("crypto") || F.contains("aes"); 669 }); 670 const bool FoundSHA2 = ItSHA2 != Features.rend(); 671 const bool FoundAES = ItAES != Features.rend(); 672 if (FoundSHA2) 673 HasSHA2 = ItSHA2->take_front() == "+"; 674 if (FoundAES) 675 HasAES = ItAES->take_front() == "+"; 676 if (ItCrypto != Features.rend()) { 677 if (HasSHA2 && HasAES) 678 Features.push_back("+crypto"); 679 else 680 Features.push_back("-crypto"); 681 if (HasSHA2) 682 Features.push_back("+sha2"); 683 else 684 Features.push_back("-sha2"); 685 if (HasAES) 686 Features.push_back("+aes"); 687 else 688 Features.push_back("-aes"); 689 } 690 691 if (HasSHA2 || HasAES) { 692 StringRef ArchSuffix = arm::getLLVMArchSuffixForARM( 693 arm::getARMTargetCPU(CPUName, ArchName, Triple), ArchName, Triple); 694 llvm::ARM::ProfileKind ArchProfile = 695 llvm::ARM::parseArchProfile(ArchSuffix); 696 if (!((llvm::ARM::parseArchVersion(ArchSuffix) >= 8) && 697 (ArchProfile == llvm::ARM::ProfileKind::A || 698 ArchProfile == llvm::ARM::ProfileKind::R))) { 699 if (HasSHA2) 700 D.Diag(clang::diag::warn_target_unsupported_extension) 701 << "sha2" 702 << llvm::ARM::getArchName(llvm::ARM::parseArch(ArchSuffix)); 703 if (HasAES) 704 D.Diag(clang::diag::warn_target_unsupported_extension) 705 << "aes" 706 << llvm::ARM::getArchName(llvm::ARM::parseArch(ArchSuffix)); 707 // With -fno-integrated-as -mfpu=crypto-neon-fp-armv8 some assemblers such 708 // as the GNU assembler will permit the use of crypto instructions as the 709 // fpu will override the architecture. We keep the crypto feature in this 710 // case to preserve compatibility. In all other cases we remove the crypto 711 // feature. 712 if (!Args.hasArg(options::OPT_fno_integrated_as)) { 713 Features.push_back("-sha2"); 714 Features.push_back("-aes"); 715 } 716 } 717 } 718 719 // CMSE: Check for target 8M (for -mcmse to be applicable) is performed later. 720 if (Args.getLastArg(options::OPT_mcmse)) 721 Features.push_back("+8msecext"); 722 723 if (Arg *A = Args.getLastArg(options::OPT_mfix_cmse_cve_2021_35465, 724 options::OPT_mno_fix_cmse_cve_2021_35465)) { 725 if (!Args.getLastArg(options::OPT_mcmse)) 726 D.Diag(diag::err_opt_not_valid_without_opt) 727 << A->getOption().getName() << "-mcmse"; 728 729 if (A->getOption().matches(options::OPT_mfix_cmse_cve_2021_35465)) 730 Features.push_back("+fix-cmse-cve-2021-35465"); 731 else 732 Features.push_back("-fix-cmse-cve-2021-35465"); 733 } 734 735 // Look for the last occurrence of -mlong-calls or -mno-long-calls. If 736 // neither options are specified, see if we are compiling for kernel/kext and 737 // decide whether to pass "+long-calls" based on the OS and its version. 738 if (Arg *A = Args.getLastArg(options::OPT_mlong_calls, 739 options::OPT_mno_long_calls)) { 740 if (A->getOption().matches(options::OPT_mlong_calls)) 741 Features.push_back("+long-calls"); 742 } else if (KernelOrKext && (!Triple.isiOS() || Triple.isOSVersionLT(6)) && 743 !Triple.isWatchOS()) { 744 Features.push_back("+long-calls"); 745 } 746 747 // Generate execute-only output (no data access to code sections). 748 // This only makes sense for the compiler, not for the assembler. 749 if (!ForAS) { 750 // Supported only on ARMv6T2 and ARMv7 and above. 751 // Cannot be combined with -mno-movt or -mlong-calls 752 if (Arg *A = Args.getLastArg(options::OPT_mexecute_only, options::OPT_mno_execute_only)) { 753 if (A->getOption().matches(options::OPT_mexecute_only)) { 754 if (getARMSubArchVersionNumber(Triple) < 7 && 755 llvm::ARM::parseArch(Triple.getArchName()) != llvm::ARM::ArchKind::ARMV6T2) 756 D.Diag(diag::err_target_unsupported_execute_only) << Triple.getArchName(); 757 else if (Arg *B = Args.getLastArg(options::OPT_mno_movt)) 758 D.Diag(diag::err_opt_not_valid_with_opt) << A->getAsString(Args) << B->getAsString(Args); 759 // Long calls create constant pool entries and have not yet been fixed up 760 // to play nicely with execute-only. Hence, they cannot be used in 761 // execute-only code for now 762 else if (Arg *B = Args.getLastArg(options::OPT_mlong_calls, options::OPT_mno_long_calls)) { 763 if (B->getOption().matches(options::OPT_mlong_calls)) 764 D.Diag(diag::err_opt_not_valid_with_opt) << A->getAsString(Args) << B->getAsString(Args); 765 } 766 Features.push_back("+execute-only"); 767 } 768 } 769 } 770 771 // Kernel code has more strict alignment requirements. 772 if (KernelOrKext) 773 Features.push_back("+strict-align"); 774 else if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access, 775 options::OPT_munaligned_access)) { 776 if (A->getOption().matches(options::OPT_munaligned_access)) { 777 // No v6M core supports unaligned memory access (v6M ARM ARM A3.2). 778 if (Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v6m) 779 D.Diag(diag::err_target_unsupported_unaligned) << "v6m"; 780 // v8M Baseline follows on from v6M, so doesn't support unaligned memory 781 // access either. 782 else if (Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v8m_baseline) 783 D.Diag(diag::err_target_unsupported_unaligned) << "v8m.base"; 784 } else 785 Features.push_back("+strict-align"); 786 } else { 787 // Assume pre-ARMv6 doesn't support unaligned accesses. 788 // 789 // ARMv6 may or may not support unaligned accesses depending on the 790 // SCTLR.U bit, which is architecture-specific. We assume ARMv6 791 // Darwin and NetBSD targets support unaligned accesses, and others don't. 792 // 793 // ARMv7 always has SCTLR.U set to 1, but it has a new SCTLR.A bit 794 // which raises an alignment fault on unaligned accesses. Linux 795 // defaults this bit to 0 and handles it as a system-wide (not 796 // per-process) setting. It is therefore safe to assume that ARMv7+ 797 // Linux targets support unaligned accesses. The same goes for NaCl 798 // and Windows. 799 // 800 // The above behavior is consistent with GCC. 801 int VersionNum = getARMSubArchVersionNumber(Triple); 802 if (Triple.isOSDarwin() || Triple.isOSNetBSD()) { 803 if (VersionNum < 6 || 804 Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v6m) 805 Features.push_back("+strict-align"); 806 } else if (Triple.isOSLinux() || Triple.isOSNaCl() || 807 Triple.isOSWindows()) { 808 if (VersionNum < 7) 809 Features.push_back("+strict-align"); 810 } else 811 Features.push_back("+strict-align"); 812 } 813 814 // llvm does not support reserving registers in general. There is support 815 // for reserving r9 on ARM though (defined as a platform-specific register 816 // in ARM EABI). 817 if (Args.hasArg(options::OPT_ffixed_r9)) 818 Features.push_back("+reserve-r9"); 819 820 // The kext linker doesn't know how to deal with movw/movt. 821 if (KernelOrKext || Args.hasArg(options::OPT_mno_movt)) 822 Features.push_back("+no-movt"); 823 824 if (Args.hasArg(options::OPT_mno_neg_immediates)) 825 Features.push_back("+no-neg-immediates"); 826 827 // Enable/disable straight line speculation hardening. 828 if (Arg *A = Args.getLastArg(options::OPT_mharden_sls_EQ)) { 829 StringRef Scope = A->getValue(); 830 bool EnableRetBr = false; 831 bool EnableBlr = false; 832 bool DisableComdat = false; 833 if (Scope != "none") { 834 SmallVector<StringRef, 4> Opts; 835 Scope.split(Opts, ","); 836 for (auto Opt : Opts) { 837 Opt = Opt.trim(); 838 if (Opt == "all") { 839 EnableBlr = true; 840 EnableRetBr = true; 841 continue; 842 } 843 if (Opt == "retbr") { 844 EnableRetBr = true; 845 continue; 846 } 847 if (Opt == "blr") { 848 EnableBlr = true; 849 continue; 850 } 851 if (Opt == "comdat") { 852 DisableComdat = false; 853 continue; 854 } 855 if (Opt == "nocomdat") { 856 DisableComdat = true; 857 continue; 858 } 859 D.Diag(diag::err_invalid_sls_hardening) 860 << Scope << A->getAsString(Args); 861 break; 862 } 863 } 864 865 if (EnableRetBr || EnableBlr) 866 if (!(isARMAProfile(Triple) && getARMSubArchVersionNumber(Triple) >= 7)) 867 D.Diag(diag::err_sls_hardening_arm_not_supported) 868 << Scope << A->getAsString(Args); 869 870 if (EnableRetBr) 871 Features.push_back("+harden-sls-retbr"); 872 if (EnableBlr) 873 Features.push_back("+harden-sls-blr"); 874 if (DisableComdat) { 875 Features.push_back("+harden-sls-nocomdat"); 876 } 877 } 878 879 if (Args.getLastArg(options::OPT_mno_bti_at_return_twice)) 880 Features.push_back("+no-bti-at-return-twice"); 881 } 882 883 std::string arm::getARMArch(StringRef Arch, const llvm::Triple &Triple) { 884 std::string MArch; 885 if (!Arch.empty()) 886 MArch = std::string(Arch); 887 else 888 MArch = std::string(Triple.getArchName()); 889 MArch = StringRef(MArch).split("+").first.lower(); 890 891 // Handle -march=native. 892 if (MArch == "native") { 893 std::string CPU = std::string(llvm::sys::getHostCPUName()); 894 if (CPU != "generic") { 895 // Translate the native cpu into the architecture suffix for that CPU. 896 StringRef Suffix = arm::getLLVMArchSuffixForARM(CPU, MArch, Triple); 897 // If there is no valid architecture suffix for this CPU we don't know how 898 // to handle it, so return no architecture. 899 if (Suffix.empty()) 900 MArch = ""; 901 else 902 MArch = std::string("arm") + Suffix.str(); 903 } 904 } 905 906 return MArch; 907 } 908 909 /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting. 910 StringRef arm::getARMCPUForMArch(StringRef Arch, const llvm::Triple &Triple) { 911 std::string MArch = getARMArch(Arch, Triple); 912 // getARMCPUForArch defaults to the triple if MArch is empty, but empty MArch 913 // here means an -march=native that we can't handle, so instead return no CPU. 914 if (MArch.empty()) 915 return StringRef(); 916 917 // We need to return an empty string here on invalid MArch values as the 918 // various places that call this function can't cope with a null result. 919 return Triple.getARMCPUForArch(MArch); 920 } 921 922 /// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting. 923 std::string arm::getARMTargetCPU(StringRef CPU, StringRef Arch, 924 const llvm::Triple &Triple) { 925 // FIXME: Warn on inconsistent use of -mcpu and -march. 926 // If we have -mcpu=, use that. 927 if (!CPU.empty()) { 928 std::string MCPU = StringRef(CPU).split("+").first.lower(); 929 // Handle -mcpu=native. 930 if (MCPU == "native") 931 return std::string(llvm::sys::getHostCPUName()); 932 else 933 return MCPU; 934 } 935 936 return std::string(getARMCPUForMArch(Arch, Triple)); 937 } 938 939 /// getLLVMArchSuffixForARM - Get the LLVM ArchKind value to use for a 940 /// particular CPU (or Arch, if CPU is generic). This is needed to 941 /// pass to functions like llvm::ARM::getDefaultFPU which need an 942 /// ArchKind as well as a CPU name. 943 llvm::ARM::ArchKind arm::getLLVMArchKindForARM(StringRef CPU, StringRef Arch, 944 const llvm::Triple &Triple) { 945 llvm::ARM::ArchKind ArchKind; 946 if (CPU == "generic" || CPU.empty()) { 947 std::string ARMArch = tools::arm::getARMArch(Arch, Triple); 948 ArchKind = llvm::ARM::parseArch(ARMArch); 949 if (ArchKind == llvm::ARM::ArchKind::INVALID) 950 // In case of generic Arch, i.e. "arm", 951 // extract arch from default cpu of the Triple 952 ArchKind = llvm::ARM::parseCPUArch(Triple.getARMCPUForArch(ARMArch)); 953 } else { 954 // FIXME: horrible hack to get around the fact that Cortex-A7 is only an 955 // armv7k triple if it's actually been specified via "-arch armv7k". 956 ArchKind = (Arch == "armv7k" || Arch == "thumbv7k") 957 ? llvm::ARM::ArchKind::ARMV7K 958 : llvm::ARM::parseCPUArch(CPU); 959 } 960 return ArchKind; 961 } 962 963 /// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular 964 /// CPU (or Arch, if CPU is generic). 965 // FIXME: This is redundant with -mcpu, why does LLVM use this. 966 StringRef arm::getLLVMArchSuffixForARM(StringRef CPU, StringRef Arch, 967 const llvm::Triple &Triple) { 968 llvm::ARM::ArchKind ArchKind = getLLVMArchKindForARM(CPU, Arch, Triple); 969 if (ArchKind == llvm::ARM::ArchKind::INVALID) 970 return ""; 971 return llvm::ARM::getSubArch(ArchKind); 972 } 973 974 void arm::appendBE8LinkFlag(const ArgList &Args, ArgStringList &CmdArgs, 975 const llvm::Triple &Triple) { 976 if (Args.hasArg(options::OPT_r)) 977 return; 978 979 // ARMv7 (and later) and ARMv6-M do not support BE-32, so instruct the linker 980 // to generate BE-8 executables. 981 if (arm::getARMSubArchVersionNumber(Triple) >= 7 || arm::isARMMProfile(Triple)) 982 CmdArgs.push_back("--be8"); 983 } 984