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 // Get Arch/CPU from args. 36 void arm::getARMArchCPUFromArgs(const ArgList &Args, llvm::StringRef &Arch, 37 llvm::StringRef &CPU, bool FromAs) { 38 if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mcpu_EQ)) 39 CPU = A->getValue(); 40 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) 41 Arch = A->getValue(); 42 if (!FromAs) 43 return; 44 45 for (const Arg *A : 46 Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) { 47 StringRef Value = A->getValue(); 48 if (Value.startswith("-mcpu=")) 49 CPU = Value.substr(6); 50 if (Value.startswith("-march=")) 51 Arch = Value.substr(7); 52 } 53 } 54 55 // Handle -mhwdiv=. 56 // FIXME: Use ARMTargetParser. 57 static void getARMHWDivFeatures(const Driver &D, const Arg *A, 58 const ArgList &Args, StringRef HWDiv, 59 std::vector<StringRef> &Features) { 60 uint64_t HWDivID = llvm::ARM::parseHWDiv(HWDiv); 61 if (!llvm::ARM::getHWDivFeatures(HWDivID, Features)) 62 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args); 63 } 64 65 // Handle -mfpu=. 66 static unsigned getARMFPUFeatures(const Driver &D, const Arg *A, 67 const ArgList &Args, StringRef FPU, 68 std::vector<StringRef> &Features) { 69 unsigned FPUID = llvm::ARM::parseFPU(FPU); 70 if (!llvm::ARM::getFPUFeatures(FPUID, Features)) 71 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args); 72 return FPUID; 73 } 74 75 // Decode ARM features from string like +[no]featureA+[no]featureB+... 76 static bool DecodeARMFeatures(const Driver &D, StringRef text, StringRef CPU, 77 llvm::ARM::ArchKind ArchKind, 78 std::vector<StringRef> &Features, 79 unsigned &ArgFPUID) { 80 SmallVector<StringRef, 8> Split; 81 text.split(Split, StringRef("+"), -1, false); 82 83 for (StringRef Feature : Split) { 84 if (!appendArchExtFeatures(CPU, ArchKind, Feature, Features, ArgFPUID)) 85 return false; 86 } 87 return true; 88 } 89 90 static void DecodeARMFeaturesFromCPU(const Driver &D, StringRef CPU, 91 std::vector<StringRef> &Features) { 92 CPU = CPU.split("+").first; 93 if (CPU != "generic") { 94 llvm::ARM::ArchKind ArchKind = llvm::ARM::parseCPUArch(CPU); 95 uint64_t Extension = llvm::ARM::getDefaultExtensions(CPU, ArchKind); 96 llvm::ARM::getExtensionFeatures(Extension, Features); 97 } 98 } 99 100 // Check if -march is valid by checking if it can be canonicalised and parsed. 101 // getARMArch is used here instead of just checking the -march value in order 102 // to handle -march=native correctly. 103 static void checkARMArchName(const Driver &D, const Arg *A, const ArgList &Args, 104 llvm::StringRef ArchName, llvm::StringRef CPUName, 105 std::vector<StringRef> &Features, 106 const llvm::Triple &Triple, unsigned &ArgFPUID) { 107 std::pair<StringRef, StringRef> Split = ArchName.split("+"); 108 109 std::string MArch = arm::getARMArch(ArchName, Triple); 110 llvm::ARM::ArchKind ArchKind = llvm::ARM::parseArch(MArch); 111 if (ArchKind == llvm::ARM::ArchKind::INVALID || 112 (Split.second.size() && !DecodeARMFeatures(D, Split.second, CPUName, 113 ArchKind, Features, ArgFPUID))) 114 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args); 115 } 116 117 // Check -mcpu=. Needs ArchName to handle -mcpu=generic. 118 static void checkARMCPUName(const Driver &D, const Arg *A, const ArgList &Args, 119 llvm::StringRef CPUName, llvm::StringRef ArchName, 120 std::vector<StringRef> &Features, 121 const llvm::Triple &Triple, unsigned &ArgFPUID) { 122 std::pair<StringRef, StringRef> Split = CPUName.split("+"); 123 124 std::string CPU = arm::getARMTargetCPU(CPUName, ArchName, Triple); 125 llvm::ARM::ArchKind ArchKind = 126 arm::getLLVMArchKindForARM(CPU, ArchName, Triple); 127 if (ArchKind == llvm::ARM::ArchKind::INVALID || 128 (Split.second.size() && 129 !DecodeARMFeatures(D, Split.second, CPU, ArchKind, Features, ArgFPUID))) 130 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args); 131 } 132 133 bool arm::useAAPCSForMachO(const llvm::Triple &T) { 134 // The backend is hardwired to assume AAPCS for M-class processors, ensure 135 // the frontend matches that. 136 return T.getEnvironment() == llvm::Triple::EABI || 137 T.getOS() == llvm::Triple::UnknownOS || isARMMProfile(T); 138 } 139 140 // Select mode for reading thread pointer (-mtp=soft/cp15). 141 arm::ReadTPMode arm::getReadTPMode(const Driver &D, const ArgList &Args) { 142 if (Arg *A = Args.getLastArg(options::OPT_mtp_mode_EQ)) { 143 arm::ReadTPMode ThreadPointer = 144 llvm::StringSwitch<arm::ReadTPMode>(A->getValue()) 145 .Case("cp15", ReadTPMode::Cp15) 146 .Case("soft", ReadTPMode::Soft) 147 .Default(ReadTPMode::Invalid); 148 if (ThreadPointer != ReadTPMode::Invalid) 149 return ThreadPointer; 150 if (StringRef(A->getValue()).empty()) 151 D.Diag(diag::err_drv_missing_arg_mtp) << A->getAsString(Args); 152 else 153 D.Diag(diag::err_drv_invalid_mtp) << A->getAsString(Args); 154 return ReadTPMode::Invalid; 155 } 156 return ReadTPMode::Soft; 157 } 158 159 arm::FloatABI arm::getARMFloatABI(const ToolChain &TC, const ArgList &Args) { 160 return arm::getARMFloatABI(TC.getDriver(), TC.getEffectiveTriple(), Args); 161 } 162 163 // Select the float ABI as determined by -msoft-float, -mhard-float, and 164 // -mfloat-abi=. 165 arm::FloatABI arm::getARMFloatABI(const Driver &D, const llvm::Triple &Triple, 166 const ArgList &Args) { 167 auto SubArch = getARMSubArchVersionNumber(Triple); 168 arm::FloatABI ABI = FloatABI::Invalid; 169 if (Arg *A = 170 Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float, 171 options::OPT_mfloat_abi_EQ)) { 172 if (A->getOption().matches(options::OPT_msoft_float)) { 173 ABI = FloatABI::Soft; 174 } else if (A->getOption().matches(options::OPT_mhard_float)) { 175 ABI = FloatABI::Hard; 176 } else { 177 ABI = llvm::StringSwitch<arm::FloatABI>(A->getValue()) 178 .Case("soft", FloatABI::Soft) 179 .Case("softfp", FloatABI::SoftFP) 180 .Case("hard", FloatABI::Hard) 181 .Default(FloatABI::Invalid); 182 if (ABI == FloatABI::Invalid && !StringRef(A->getValue()).empty()) { 183 D.Diag(diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args); 184 ABI = FloatABI::Soft; 185 } 186 } 187 188 // It is incorrect to select hard float ABI on MachO platforms if the ABI is 189 // "apcs-gnu". 190 if (Triple.isOSBinFormatMachO() && !useAAPCSForMachO(Triple) && 191 ABI == FloatABI::Hard) { 192 D.Diag(diag::err_drv_unsupported_opt_for_target) << A->getAsString(Args) 193 << Triple.getArchName(); 194 } 195 } 196 197 // If unspecified, choose the default based on the platform. 198 if (ABI == FloatABI::Invalid) { 199 switch (Triple.getOS()) { 200 case llvm::Triple::Darwin: 201 case llvm::Triple::MacOSX: 202 case llvm::Triple::IOS: 203 case llvm::Triple::TvOS: { 204 // Darwin defaults to "softfp" for v6 and v7. 205 ABI = (SubArch == 6 || SubArch == 7) ? FloatABI::SoftFP : FloatABI::Soft; 206 ABI = Triple.isWatchABI() ? FloatABI::Hard : ABI; 207 break; 208 } 209 case llvm::Triple::WatchOS: 210 ABI = FloatABI::Hard; 211 break; 212 213 // FIXME: this is invalid for WindowsCE 214 case llvm::Triple::Win32: 215 ABI = FloatABI::Hard; 216 break; 217 218 case llvm::Triple::NetBSD: 219 switch (Triple.getEnvironment()) { 220 case llvm::Triple::EABIHF: 221 case llvm::Triple::GNUEABIHF: 222 ABI = FloatABI::Hard; 223 break; 224 default: 225 ABI = FloatABI::Soft; 226 break; 227 } 228 break; 229 230 case llvm::Triple::FreeBSD: 231 switch (Triple.getEnvironment()) { 232 case llvm::Triple::GNUEABIHF: 233 ABI = FloatABI::Hard; 234 break; 235 default: 236 // FreeBSD defaults to soft float 237 ABI = FloatABI::Soft; 238 break; 239 } 240 break; 241 242 case llvm::Triple::OpenBSD: 243 ABI = FloatABI::SoftFP; 244 break; 245 246 default: 247 switch (Triple.getEnvironment()) { 248 case llvm::Triple::GNUEABIHF: 249 case llvm::Triple::MuslEABIHF: 250 case llvm::Triple::EABIHF: 251 ABI = FloatABI::Hard; 252 break; 253 case llvm::Triple::GNUEABI: 254 case llvm::Triple::MuslEABI: 255 case llvm::Triple::EABI: 256 // EABI is always AAPCS, and if it was not marked 'hard', it's softfp 257 ABI = FloatABI::SoftFP; 258 break; 259 case llvm::Triple::Android: 260 ABI = (SubArch >= 7) ? FloatABI::SoftFP : FloatABI::Soft; 261 break; 262 default: 263 // Assume "soft", but warn the user we are guessing. 264 if (Triple.isOSBinFormatMachO() && 265 Triple.getSubArch() == llvm::Triple::ARMSubArch_v7em) 266 ABI = FloatABI::Hard; 267 else 268 ABI = FloatABI::Soft; 269 270 if (Triple.getOS() != llvm::Triple::UnknownOS || 271 !Triple.isOSBinFormatMachO()) 272 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft"; 273 break; 274 } 275 } 276 } 277 278 assert(ABI != FloatABI::Invalid && "must select an ABI"); 279 return ABI; 280 } 281 282 static bool hasIntegerMVE(const std::vector<StringRef> &F) { 283 auto MVE = llvm::find(llvm::reverse(F), "+mve"); 284 auto NoMVE = llvm::find(llvm::reverse(F), "-mve"); 285 return MVE != F.rend() && 286 (NoMVE == F.rend() || std::distance(MVE, NoMVE) > 0); 287 } 288 289 void arm::getARMTargetFeatures(const Driver &D, const llvm::Triple &Triple, 290 const ArgList &Args, ArgStringList &CmdArgs, 291 std::vector<StringRef> &Features, bool ForAS) { 292 bool KernelOrKext = 293 Args.hasArg(options::OPT_mkernel, options::OPT_fapple_kext); 294 arm::FloatABI ABI = arm::getARMFloatABI(D, Triple, Args); 295 arm::ReadTPMode ThreadPointer = arm::getReadTPMode(D, Args); 296 const Arg *WaCPU = nullptr, *WaFPU = nullptr; 297 const Arg *WaHDiv = nullptr, *WaArch = nullptr; 298 299 // This vector will accumulate features from the architecture 300 // extension suffixes on -mcpu and -march (e.g. the 'bar' in 301 // -mcpu=foo+bar). We want to apply those after the features derived 302 // from the FPU, in case -mfpu generates a negative feature which 303 // the +bar is supposed to override. 304 std::vector<StringRef> ExtensionFeatures; 305 306 if (!ForAS) { 307 // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these 308 // yet (it uses the -mfloat-abi and -msoft-float options), and it is 309 // stripped out by the ARM target. We should probably pass this a new 310 // -target-option, which is handled by the -cc1/-cc1as invocation. 311 // 312 // FIXME2: For consistency, it would be ideal if we set up the target 313 // machine state the same when using the frontend or the assembler. We don't 314 // currently do that for the assembler, we pass the options directly to the 315 // backend and never even instantiate the frontend TargetInfo. If we did, 316 // and used its handleTargetFeatures hook, then we could ensure the 317 // assembler and the frontend behave the same. 318 319 // Use software floating point operations? 320 if (ABI == arm::FloatABI::Soft) 321 Features.push_back("+soft-float"); 322 323 // Use software floating point argument passing? 324 if (ABI != arm::FloatABI::Hard) 325 Features.push_back("+soft-float-abi"); 326 } else { 327 // Here, we make sure that -Wa,-mfpu/cpu/arch/hwdiv will be passed down 328 // to the assembler correctly. 329 for (const Arg *A : 330 Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) { 331 StringRef Value = A->getValue(); 332 if (Value.startswith("-mfpu=")) { 333 WaFPU = A; 334 } else if (Value.startswith("-mcpu=")) { 335 WaCPU = A; 336 } else if (Value.startswith("-mhwdiv=")) { 337 WaHDiv = A; 338 } else if (Value.startswith("-march=")) { 339 WaArch = A; 340 } 341 } 342 } 343 344 if (ThreadPointer == arm::ReadTPMode::Cp15) 345 Features.push_back("+read-tp-hard"); 346 347 const Arg *ArchArg = Args.getLastArg(options::OPT_march_EQ); 348 const Arg *CPUArg = Args.getLastArg(options::OPT_mcpu_EQ); 349 StringRef ArchName; 350 StringRef CPUName; 351 unsigned ArchArgFPUID = llvm::ARM::FK_INVALID; 352 unsigned CPUArgFPUID = llvm::ARM::FK_INVALID; 353 354 // Check -mcpu. ClangAs gives preference to -Wa,-mcpu=. 355 if (WaCPU) { 356 if (CPUArg) 357 D.Diag(clang::diag::warn_drv_unused_argument) 358 << CPUArg->getAsString(Args); 359 CPUName = StringRef(WaCPU->getValue()).substr(6); 360 CPUArg = WaCPU; 361 } else if (CPUArg) 362 CPUName = CPUArg->getValue(); 363 364 // Check -march. ClangAs gives preference to -Wa,-march=. 365 if (WaArch) { 366 if (ArchArg) 367 D.Diag(clang::diag::warn_drv_unused_argument) 368 << ArchArg->getAsString(Args); 369 ArchName = StringRef(WaArch->getValue()).substr(7); 370 checkARMArchName(D, WaArch, Args, ArchName, CPUName, ExtensionFeatures, 371 Triple, ArchArgFPUID); 372 // FIXME: Set Arch. 373 D.Diag(clang::diag::warn_drv_unused_argument) << WaArch->getAsString(Args); 374 } else if (ArchArg) { 375 ArchName = ArchArg->getValue(); 376 checkARMArchName(D, ArchArg, Args, ArchName, CPUName, ExtensionFeatures, 377 Triple, ArchArgFPUID); 378 } 379 380 // Add CPU features for generic CPUs 381 if (CPUName == "native") { 382 llvm::StringMap<bool> HostFeatures; 383 if (llvm::sys::getHostCPUFeatures(HostFeatures)) 384 for (auto &F : HostFeatures) 385 Features.push_back( 386 Args.MakeArgString((F.second ? "+" : "-") + F.first())); 387 } else if (!CPUName.empty()) { 388 // This sets the default features for the specified CPU. We certainly don't 389 // want to override the features that have been explicitly specified on the 390 // command line. Therefore, process them directly instead of appending them 391 // at the end later. 392 DecodeARMFeaturesFromCPU(D, CPUName, Features); 393 } 394 395 if (CPUArg) 396 checkARMCPUName(D, CPUArg, Args, CPUName, ArchName, ExtensionFeatures, 397 Triple, CPUArgFPUID); 398 // Honor -mfpu=. ClangAs gives preference to -Wa,-mfpu=. 399 unsigned FPUID = llvm::ARM::FK_INVALID; 400 const Arg *FPUArg = Args.getLastArg(options::OPT_mfpu_EQ); 401 if (WaFPU) { 402 if (FPUArg) 403 D.Diag(clang::diag::warn_drv_unused_argument) 404 << FPUArg->getAsString(Args); 405 (void)getARMFPUFeatures(D, WaFPU, Args, StringRef(WaFPU->getValue()).substr(6), 406 Features); 407 } else if (FPUArg) { 408 FPUID = getARMFPUFeatures(D, FPUArg, Args, FPUArg->getValue(), Features); 409 } else if (Triple.isAndroid() && getARMSubArchVersionNumber(Triple) >= 7) { 410 const char *AndroidFPU = "neon"; 411 FPUID = llvm::ARM::parseFPU(AndroidFPU); 412 if (!llvm::ARM::getFPUFeatures(FPUID, Features)) 413 D.Diag(clang::diag::err_drv_clang_unsupported) 414 << std::string("-mfpu=") + AndroidFPU; 415 } 416 417 // Now we've finished accumulating features from arch, cpu and fpu, 418 // we can append the ones for architecture extensions that we 419 // collected separately. 420 Features.insert(std::end(Features), 421 std::begin(ExtensionFeatures), std::end(ExtensionFeatures)); 422 423 // Honor -mhwdiv=. ClangAs gives preference to -Wa,-mhwdiv=. 424 const Arg *HDivArg = Args.getLastArg(options::OPT_mhwdiv_EQ); 425 if (WaHDiv) { 426 if (HDivArg) 427 D.Diag(clang::diag::warn_drv_unused_argument) 428 << HDivArg->getAsString(Args); 429 getARMHWDivFeatures(D, WaHDiv, Args, 430 StringRef(WaHDiv->getValue()).substr(8), Features); 431 } else if (HDivArg) 432 getARMHWDivFeatures(D, HDivArg, Args, HDivArg->getValue(), Features); 433 434 // Handle (arch-dependent) fp16fml/fullfp16 relationship. 435 // Must happen before any features are disabled due to soft-float. 436 // FIXME: this fp16fml option handling will be reimplemented after the 437 // TargetParser rewrite. 438 const auto ItRNoFullFP16 = std::find(Features.rbegin(), Features.rend(), "-fullfp16"); 439 const auto ItRFP16FML = std::find(Features.rbegin(), Features.rend(), "+fp16fml"); 440 if (Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v8_4a) { 441 const auto ItRFullFP16 = std::find(Features.rbegin(), Features.rend(), "+fullfp16"); 442 if (ItRFullFP16 < ItRNoFullFP16 && ItRFullFP16 < ItRFP16FML) { 443 // Only entangled feature that can be to the right of this +fullfp16 is -fp16fml. 444 // Only append the +fp16fml if there is no -fp16fml after the +fullfp16. 445 if (std::find(Features.rbegin(), ItRFullFP16, "-fp16fml") == ItRFullFP16) 446 Features.push_back("+fp16fml"); 447 } 448 else 449 goto fp16_fml_fallthrough; 450 } 451 else { 452 fp16_fml_fallthrough: 453 // In both of these cases, putting the 'other' feature on the end of the vector will 454 // result in the same effect as placing it immediately after the current feature. 455 if (ItRNoFullFP16 < ItRFP16FML) 456 Features.push_back("-fp16fml"); 457 else if (ItRNoFullFP16 > ItRFP16FML) 458 Features.push_back("+fullfp16"); 459 } 460 461 // Setting -msoft-float/-mfloat-abi=soft, -mfpu=none, or adding +nofp to 462 // -march/-mcpu effectively disables the FPU (GCC ignores the -mfpu options in 463 // this case). Note that the ABI can also be set implicitly by the target 464 // selected. 465 if (ABI == arm::FloatABI::Soft) { 466 llvm::ARM::getFPUFeatures(llvm::ARM::FK_NONE, Features); 467 468 // Disable all features relating to hardware FP, not already disabled by the 469 // above call. 470 Features.insert(Features.end(), {"-dotprod", "-fp16fml", "-bf16", "-mve", 471 "-mve.fp", "-fpregs"}); 472 } else if (FPUID == llvm::ARM::FK_NONE || 473 ArchArgFPUID == llvm::ARM::FK_NONE || 474 CPUArgFPUID == llvm::ARM::FK_NONE) { 475 // -mfpu=none, -march=armvX+nofp or -mcpu=X+nofp is *very* similar to 476 // -mfloat-abi=soft, only that it should not disable MVE-I. They disable the 477 // FPU, but not the FPU registers, thus MVE-I, which depends only on the 478 // latter, is still supported. 479 Features.insert(Features.end(), 480 {"-dotprod", "-fp16fml", "-bf16", "-mve.fp"}); 481 if (!hasIntegerMVE(Features)) 482 Features.emplace_back("-fpregs"); 483 } 484 485 // En/disable crc code generation. 486 if (Arg *A = Args.getLastArg(options::OPT_mcrc, options::OPT_mnocrc)) { 487 if (A->getOption().matches(options::OPT_mcrc)) 488 Features.push_back("+crc"); 489 else 490 Features.push_back("-crc"); 491 } 492 493 // For Arch >= ARMv8.0 && A profile: crypto = sha2 + aes 494 // FIXME: this needs reimplementation after the TargetParser rewrite 495 auto CryptoIt = llvm::find_if(llvm::reverse(Features), [](const StringRef F) { 496 return F.contains("crypto"); 497 }); 498 if (CryptoIt != Features.rend()) { 499 if (CryptoIt->take_front() == "+") { 500 StringRef ArchSuffix = arm::getLLVMArchSuffixForARM( 501 arm::getARMTargetCPU(CPUName, ArchName, Triple), ArchName, Triple); 502 if (llvm::ARM::parseArchVersion(ArchSuffix) >= 8 && 503 llvm::ARM::parseArchProfile(ArchSuffix) == 504 llvm::ARM::ProfileKind::A) { 505 if (ArchName.find_lower("+nosha2") == StringRef::npos && 506 CPUName.find_lower("+nosha2") == StringRef::npos) 507 Features.push_back("+sha2"); 508 if (ArchName.find_lower("+noaes") == StringRef::npos && 509 CPUName.find_lower("+noaes") == StringRef::npos) 510 Features.push_back("+aes"); 511 } else { 512 D.Diag(clang::diag::warn_target_unsupported_extension) 513 << "crypto" 514 << llvm::ARM::getArchName(llvm::ARM::parseArch(ArchSuffix)); 515 // With -fno-integrated-as -mfpu=crypto-neon-fp-armv8 some assemblers such as the GNU assembler 516 // will permit the use of crypto instructions as the fpu will override the architecture. 517 // We keep the crypto feature in this case to preserve compatibility. 518 // In all other cases we remove the crypto feature. 519 if (!Args.hasArg(options::OPT_fno_integrated_as)) 520 Features.push_back("-crypto"); 521 } 522 } 523 } 524 525 // CMSE: Check for target 8M (for -mcmse to be applicable) is performed later. 526 if (Args.getLastArg(options::OPT_mcmse)) 527 Features.push_back("+8msecext"); 528 529 // Look for the last occurrence of -mlong-calls or -mno-long-calls. If 530 // neither options are specified, see if we are compiling for kernel/kext and 531 // decide whether to pass "+long-calls" based on the OS and its version. 532 if (Arg *A = Args.getLastArg(options::OPT_mlong_calls, 533 options::OPT_mno_long_calls)) { 534 if (A->getOption().matches(options::OPT_mlong_calls)) 535 Features.push_back("+long-calls"); 536 } else if (KernelOrKext && (!Triple.isiOS() || Triple.isOSVersionLT(6)) && 537 !Triple.isWatchOS()) { 538 Features.push_back("+long-calls"); 539 } 540 541 // Generate execute-only output (no data access to code sections). 542 // This only makes sense for the compiler, not for the assembler. 543 if (!ForAS) { 544 // Supported only on ARMv6T2 and ARMv7 and above. 545 // Cannot be combined with -mno-movt or -mlong-calls 546 if (Arg *A = Args.getLastArg(options::OPT_mexecute_only, options::OPT_mno_execute_only)) { 547 if (A->getOption().matches(options::OPT_mexecute_only)) { 548 if (getARMSubArchVersionNumber(Triple) < 7 && 549 llvm::ARM::parseArch(Triple.getArchName()) != llvm::ARM::ArchKind::ARMV6T2) 550 D.Diag(diag::err_target_unsupported_execute_only) << Triple.getArchName(); 551 else if (Arg *B = Args.getLastArg(options::OPT_mno_movt)) 552 D.Diag(diag::err_opt_not_valid_with_opt) << A->getAsString(Args) << B->getAsString(Args); 553 // Long calls create constant pool entries and have not yet been fixed up 554 // to play nicely with execute-only. Hence, they cannot be used in 555 // execute-only code for now 556 else if (Arg *B = Args.getLastArg(options::OPT_mlong_calls, options::OPT_mno_long_calls)) { 557 if (B->getOption().matches(options::OPT_mlong_calls)) 558 D.Diag(diag::err_opt_not_valid_with_opt) << A->getAsString(Args) << B->getAsString(Args); 559 } 560 Features.push_back("+execute-only"); 561 } 562 } 563 } 564 565 // Kernel code has more strict alignment requirements. 566 if (KernelOrKext) 567 Features.push_back("+strict-align"); 568 else if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access, 569 options::OPT_munaligned_access)) { 570 if (A->getOption().matches(options::OPT_munaligned_access)) { 571 // No v6M core supports unaligned memory access (v6M ARM ARM A3.2). 572 if (Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v6m) 573 D.Diag(diag::err_target_unsupported_unaligned) << "v6m"; 574 // v8M Baseline follows on from v6M, so doesn't support unaligned memory 575 // access either. 576 else if (Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v8m_baseline) 577 D.Diag(diag::err_target_unsupported_unaligned) << "v8m.base"; 578 } else 579 Features.push_back("+strict-align"); 580 } else { 581 // Assume pre-ARMv6 doesn't support unaligned accesses. 582 // 583 // ARMv6 may or may not support unaligned accesses depending on the 584 // SCTLR.U bit, which is architecture-specific. We assume ARMv6 585 // Darwin and NetBSD targets support unaligned accesses, and others don't. 586 // 587 // ARMv7 always has SCTLR.U set to 1, but it has a new SCTLR.A bit 588 // which raises an alignment fault on unaligned accesses. Linux 589 // defaults this bit to 0 and handles it as a system-wide (not 590 // per-process) setting. It is therefore safe to assume that ARMv7+ 591 // Linux targets support unaligned accesses. The same goes for NaCl. 592 // 593 // The above behavior is consistent with GCC. 594 int VersionNum = getARMSubArchVersionNumber(Triple); 595 if (Triple.isOSDarwin() || Triple.isOSNetBSD()) { 596 if (VersionNum < 6 || 597 Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v6m) 598 Features.push_back("+strict-align"); 599 } else if (Triple.isOSLinux() || Triple.isOSNaCl()) { 600 if (VersionNum < 7) 601 Features.push_back("+strict-align"); 602 } else 603 Features.push_back("+strict-align"); 604 } 605 606 // llvm does not support reserving registers in general. There is support 607 // for reserving r9 on ARM though (defined as a platform-specific register 608 // in ARM EABI). 609 if (Args.hasArg(options::OPT_ffixed_r9)) 610 Features.push_back("+reserve-r9"); 611 612 // The kext linker doesn't know how to deal with movw/movt. 613 if (KernelOrKext || Args.hasArg(options::OPT_mno_movt)) 614 Features.push_back("+no-movt"); 615 616 if (Args.hasArg(options::OPT_mno_neg_immediates)) 617 Features.push_back("+no-neg-immediates"); 618 } 619 620 const std::string arm::getARMArch(StringRef Arch, const llvm::Triple &Triple) { 621 std::string MArch; 622 if (!Arch.empty()) 623 MArch = std::string(Arch); 624 else 625 MArch = std::string(Triple.getArchName()); 626 MArch = StringRef(MArch).split("+").first.lower(); 627 628 // Handle -march=native. 629 if (MArch == "native") { 630 std::string CPU = std::string(llvm::sys::getHostCPUName()); 631 if (CPU != "generic") { 632 // Translate the native cpu into the architecture suffix for that CPU. 633 StringRef Suffix = arm::getLLVMArchSuffixForARM(CPU, MArch, Triple); 634 // If there is no valid architecture suffix for this CPU we don't know how 635 // to handle it, so return no architecture. 636 if (Suffix.empty()) 637 MArch = ""; 638 else 639 MArch = std::string("arm") + Suffix.str(); 640 } 641 } 642 643 return MArch; 644 } 645 646 /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting. 647 StringRef arm::getARMCPUForMArch(StringRef Arch, const llvm::Triple &Triple) { 648 std::string MArch = getARMArch(Arch, Triple); 649 // getARMCPUForArch defaults to the triple if MArch is empty, but empty MArch 650 // here means an -march=native that we can't handle, so instead return no CPU. 651 if (MArch.empty()) 652 return StringRef(); 653 654 // We need to return an empty string here on invalid MArch values as the 655 // various places that call this function can't cope with a null result. 656 return Triple.getARMCPUForArch(MArch); 657 } 658 659 /// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting. 660 std::string arm::getARMTargetCPU(StringRef CPU, StringRef Arch, 661 const llvm::Triple &Triple) { 662 // FIXME: Warn on inconsistent use of -mcpu and -march. 663 // If we have -mcpu=, use that. 664 if (!CPU.empty()) { 665 std::string MCPU = StringRef(CPU).split("+").first.lower(); 666 // Handle -mcpu=native. 667 if (MCPU == "native") 668 return std::string(llvm::sys::getHostCPUName()); 669 else 670 return MCPU; 671 } 672 673 return std::string(getARMCPUForMArch(Arch, Triple)); 674 } 675 676 /// getLLVMArchSuffixForARM - Get the LLVM ArchKind value to use for a 677 /// particular CPU (or Arch, if CPU is generic). This is needed to 678 /// pass to functions like llvm::ARM::getDefaultFPU which need an 679 /// ArchKind as well as a CPU name. 680 llvm::ARM::ArchKind arm::getLLVMArchKindForARM(StringRef CPU, StringRef Arch, 681 const llvm::Triple &Triple) { 682 llvm::ARM::ArchKind ArchKind; 683 if (CPU == "generic" || CPU.empty()) { 684 std::string ARMArch = tools::arm::getARMArch(Arch, Triple); 685 ArchKind = llvm::ARM::parseArch(ARMArch); 686 if (ArchKind == llvm::ARM::ArchKind::INVALID) 687 // In case of generic Arch, i.e. "arm", 688 // extract arch from default cpu of the Triple 689 ArchKind = llvm::ARM::parseCPUArch(Triple.getARMCPUForArch(ARMArch)); 690 } else { 691 // FIXME: horrible hack to get around the fact that Cortex-A7 is only an 692 // armv7k triple if it's actually been specified via "-arch armv7k". 693 ArchKind = (Arch == "armv7k" || Arch == "thumbv7k") 694 ? llvm::ARM::ArchKind::ARMV7K 695 : llvm::ARM::parseCPUArch(CPU); 696 } 697 return ArchKind; 698 } 699 700 /// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular 701 /// CPU (or Arch, if CPU is generic). 702 // FIXME: This is redundant with -mcpu, why does LLVM use this. 703 StringRef arm::getLLVMArchSuffixForARM(StringRef CPU, StringRef Arch, 704 const llvm::Triple &Triple) { 705 llvm::ARM::ArchKind ArchKind = getLLVMArchKindForARM(CPU, Arch, Triple); 706 if (ArchKind == llvm::ARM::ArchKind::INVALID) 707 return ""; 708 return llvm::ARM::getSubArch(ArchKind); 709 } 710 711 void arm::appendBE8LinkFlag(const ArgList &Args, ArgStringList &CmdArgs, 712 const llvm::Triple &Triple) { 713 if (Args.hasArg(options::OPT_r)) 714 return; 715 716 // ARMv7 (and later) and ARMv6-M do not support BE-32, so instruct the linker 717 // to generate BE-8 executables. 718 if (arm::getARMSubArchVersionNumber(Triple) >= 7 || arm::isARMMProfile(Triple)) 719 CmdArgs.push_back("--be8"); 720 } 721