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