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