1 //===--- TargetInfo.cpp - Information about Target machine ----------------===// 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 // This file implements the TargetInfo and TargetInfoImpl interfaces. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/Basic/TargetInfo.h" 14 #include "clang/Basic/AddressSpaces.h" 15 #include "clang/Basic/CharInfo.h" 16 #include "clang/Basic/Diagnostic.h" 17 #include "clang/Basic/DiagnosticFrontend.h" 18 #include "clang/Basic/LangOptions.h" 19 #include "llvm/ADT/APFloat.h" 20 #include "llvm/ADT/STLExtras.h" 21 #include "llvm/Support/ErrorHandling.h" 22 #include "llvm/Support/TargetParser.h" 23 #include <cstdlib> 24 using namespace clang; 25 26 static const LangASMap DefaultAddrSpaceMap = {0}; 27 // The fake address space map must have a distinct entry for each 28 // language-specific address space. 29 static const LangASMap FakeAddrSpaceMap = { 30 0, // Default 31 1, // opencl_global 32 3, // opencl_local 33 2, // opencl_constant 34 0, // opencl_private 35 4, // opencl_generic 36 5, // opencl_global_device 37 6, // opencl_global_host 38 7, // cuda_device 39 8, // cuda_constant 40 9, // cuda_shared 41 1, // sycl_global 42 5, // sycl_global_device 43 6, // sycl_global_host 44 3, // sycl_local 45 0, // sycl_private 46 10, // ptr32_sptr 47 11, // ptr32_uptr 48 12, // ptr64 49 13, // hlsl_groupshared 50 }; 51 52 // TargetInfo Constructor. 53 TargetInfo::TargetInfo(const llvm::Triple &T) : Triple(T) { 54 // Set defaults. Defaults are set for a 32-bit RISC platform, like PPC or 55 // SPARC. These should be overridden by concrete targets as needed. 56 BigEndian = !T.isLittleEndian(); 57 TLSSupported = true; 58 VLASupported = true; 59 NoAsmVariants = false; 60 HasLegalHalfType = false; 61 HalfArgsAndReturns = false; 62 HasFloat128 = false; 63 HasIbm128 = false; 64 HasFloat16 = false; 65 HasBFloat16 = false; 66 HasLongDouble = true; 67 HasFPReturn = true; 68 HasStrictFP = false; 69 PointerWidth = PointerAlign = 32; 70 BoolWidth = BoolAlign = 8; 71 IntWidth = IntAlign = 32; 72 LongWidth = LongAlign = 32; 73 LongLongWidth = LongLongAlign = 64; 74 Int128Align = 128; 75 76 // Fixed point default bit widths 77 ShortAccumWidth = ShortAccumAlign = 16; 78 AccumWidth = AccumAlign = 32; 79 LongAccumWidth = LongAccumAlign = 64; 80 ShortFractWidth = ShortFractAlign = 8; 81 FractWidth = FractAlign = 16; 82 LongFractWidth = LongFractAlign = 32; 83 84 // Fixed point default integral and fractional bit sizes 85 // We give the _Accum 1 fewer fractional bits than their corresponding _Fract 86 // types by default to have the same number of fractional bits between _Accum 87 // and _Fract types. 88 PaddingOnUnsignedFixedPoint = false; 89 ShortAccumScale = 7; 90 AccumScale = 15; 91 LongAccumScale = 31; 92 93 SuitableAlign = 64; 94 DefaultAlignForAttributeAligned = 128; 95 MinGlobalAlign = 0; 96 // From the glibc documentation, on GNU systems, malloc guarantees 16-byte 97 // alignment on 64-bit systems and 8-byte alignment on 32-bit systems. See 98 // https://www.gnu.org/software/libc/manual/html_node/Malloc-Examples.html. 99 // This alignment guarantee also applies to Windows and Android. On Darwin 100 // and OpenBSD, the alignment is 16 bytes on both 64-bit and 32-bit systems. 101 if (T.isGNUEnvironment() || T.isWindowsMSVCEnvironment() || T.isAndroid()) 102 NewAlign = Triple.isArch64Bit() ? 128 : Triple.isArch32Bit() ? 64 : 0; 103 else if (T.isOSDarwin() || T.isOSOpenBSD()) 104 NewAlign = 128; 105 else 106 NewAlign = 0; // Infer from basic type alignment. 107 HalfWidth = 16; 108 HalfAlign = 16; 109 FloatWidth = 32; 110 FloatAlign = 32; 111 DoubleWidth = 64; 112 DoubleAlign = 64; 113 LongDoubleWidth = 64; 114 LongDoubleAlign = 64; 115 Float128Align = 128; 116 Ibm128Align = 128; 117 LargeArrayMinWidth = 0; 118 LargeArrayAlign = 0; 119 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0; 120 MaxVectorAlign = 0; 121 MaxTLSAlign = 0; 122 SimdDefaultAlign = 0; 123 SizeType = UnsignedLong; 124 PtrDiffType = SignedLong; 125 IntMaxType = SignedLongLong; 126 IntPtrType = SignedLong; 127 WCharType = SignedInt; 128 WIntType = SignedInt; 129 Char16Type = UnsignedShort; 130 Char32Type = UnsignedInt; 131 Int64Type = SignedLongLong; 132 Int16Type = SignedShort; 133 SigAtomicType = SignedInt; 134 ProcessIDType = SignedInt; 135 UseSignedCharForObjCBool = true; 136 UseBitFieldTypeAlignment = true; 137 UseZeroLengthBitfieldAlignment = false; 138 UseLeadingZeroLengthBitfield = true; 139 UseExplicitBitFieldAlignment = true; 140 ZeroLengthBitfieldBoundary = 0; 141 MaxAlignedAttribute = 0; 142 HalfFormat = &llvm::APFloat::IEEEhalf(); 143 FloatFormat = &llvm::APFloat::IEEEsingle(); 144 DoubleFormat = &llvm::APFloat::IEEEdouble(); 145 LongDoubleFormat = &llvm::APFloat::IEEEdouble(); 146 Float128Format = &llvm::APFloat::IEEEquad(); 147 Ibm128Format = &llvm::APFloat::PPCDoubleDouble(); 148 MCountName = "mcount"; 149 UserLabelPrefix = "_"; 150 RegParmMax = 0; 151 SSERegParmMax = 0; 152 HasAlignMac68kSupport = false; 153 HasBuiltinMSVaList = false; 154 IsRenderScriptTarget = false; 155 HasAArch64SVETypes = false; 156 HasRISCVVTypes = false; 157 AllowAMDGPUUnsafeFPAtomics = false; 158 ARMCDECoprocMask = 0; 159 160 // Default to no types using fpret. 161 RealTypeUsesObjCFPRetMask = 0; 162 163 // Default to not using fp2ret for __Complex long double 164 ComplexLongDoubleUsesFP2Ret = false; 165 166 // Set the C++ ABI based on the triple. 167 TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment() 168 ? TargetCXXABI::Microsoft 169 : TargetCXXABI::GenericItanium); 170 171 // Default to an empty address space map. 172 AddrSpaceMap = &DefaultAddrSpaceMap; 173 UseAddrSpaceMapMangling = false; 174 175 // Default to an unknown platform name. 176 PlatformName = "unknown"; 177 PlatformMinVersion = VersionTuple(); 178 179 MaxOpenCLWorkGroupSize = 1024; 180 181 MaxBitIntWidth.reset(); 182 } 183 184 // Out of line virtual dtor for TargetInfo. 185 TargetInfo::~TargetInfo() {} 186 187 void TargetInfo::resetDataLayout(StringRef DL, const char *ULP) { 188 DataLayoutString = DL.str(); 189 UserLabelPrefix = ULP; 190 } 191 192 bool 193 TargetInfo::checkCFProtectionBranchSupported(DiagnosticsEngine &Diags) const { 194 Diags.Report(diag::err_opt_not_valid_on_target) << "cf-protection=branch"; 195 return false; 196 } 197 198 bool 199 TargetInfo::checkCFProtectionReturnSupported(DiagnosticsEngine &Diags) const { 200 Diags.Report(diag::err_opt_not_valid_on_target) << "cf-protection=return"; 201 return false; 202 } 203 204 /// getTypeName - Return the user string for the specified integer type enum. 205 /// For example, SignedShort -> "short". 206 const char *TargetInfo::getTypeName(IntType T) { 207 switch (T) { 208 default: llvm_unreachable("not an integer!"); 209 case SignedChar: return "signed char"; 210 case UnsignedChar: return "unsigned char"; 211 case SignedShort: return "short"; 212 case UnsignedShort: return "unsigned short"; 213 case SignedInt: return "int"; 214 case UnsignedInt: return "unsigned int"; 215 case SignedLong: return "long int"; 216 case UnsignedLong: return "long unsigned int"; 217 case SignedLongLong: return "long long int"; 218 case UnsignedLongLong: return "long long unsigned int"; 219 } 220 } 221 222 /// getTypeConstantSuffix - Return the constant suffix for the specified 223 /// integer type enum. For example, SignedLong -> "L". 224 const char *TargetInfo::getTypeConstantSuffix(IntType T) const { 225 switch (T) { 226 default: llvm_unreachable("not an integer!"); 227 case SignedChar: 228 case SignedShort: 229 case SignedInt: return ""; 230 case SignedLong: return "L"; 231 case SignedLongLong: return "LL"; 232 case UnsignedChar: 233 if (getCharWidth() < getIntWidth()) 234 return ""; 235 [[fallthrough]]; 236 case UnsignedShort: 237 if (getShortWidth() < getIntWidth()) 238 return ""; 239 [[fallthrough]]; 240 case UnsignedInt: return "U"; 241 case UnsignedLong: return "UL"; 242 case UnsignedLongLong: return "ULL"; 243 } 244 } 245 246 /// getTypeFormatModifier - Return the printf format modifier for the 247 /// specified integer type enum. For example, SignedLong -> "l". 248 249 const char *TargetInfo::getTypeFormatModifier(IntType T) { 250 switch (T) { 251 default: llvm_unreachable("not an integer!"); 252 case SignedChar: 253 case UnsignedChar: return "hh"; 254 case SignedShort: 255 case UnsignedShort: return "h"; 256 case SignedInt: 257 case UnsignedInt: return ""; 258 case SignedLong: 259 case UnsignedLong: return "l"; 260 case SignedLongLong: 261 case UnsignedLongLong: return "ll"; 262 } 263 } 264 265 /// getTypeWidth - Return the width (in bits) of the specified integer type 266 /// enum. For example, SignedInt -> getIntWidth(). 267 unsigned TargetInfo::getTypeWidth(IntType T) const { 268 switch (T) { 269 default: llvm_unreachable("not an integer!"); 270 case SignedChar: 271 case UnsignedChar: return getCharWidth(); 272 case SignedShort: 273 case UnsignedShort: return getShortWidth(); 274 case SignedInt: 275 case UnsignedInt: return getIntWidth(); 276 case SignedLong: 277 case UnsignedLong: return getLongWidth(); 278 case SignedLongLong: 279 case UnsignedLongLong: return getLongLongWidth(); 280 }; 281 } 282 283 TargetInfo::IntType TargetInfo::getIntTypeByWidth( 284 unsigned BitWidth, bool IsSigned) const { 285 if (getCharWidth() == BitWidth) 286 return IsSigned ? SignedChar : UnsignedChar; 287 if (getShortWidth() == BitWidth) 288 return IsSigned ? SignedShort : UnsignedShort; 289 if (getIntWidth() == BitWidth) 290 return IsSigned ? SignedInt : UnsignedInt; 291 if (getLongWidth() == BitWidth) 292 return IsSigned ? SignedLong : UnsignedLong; 293 if (getLongLongWidth() == BitWidth) 294 return IsSigned ? SignedLongLong : UnsignedLongLong; 295 return NoInt; 296 } 297 298 TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth, 299 bool IsSigned) const { 300 if (getCharWidth() >= BitWidth) 301 return IsSigned ? SignedChar : UnsignedChar; 302 if (getShortWidth() >= BitWidth) 303 return IsSigned ? SignedShort : UnsignedShort; 304 if (getIntWidth() >= BitWidth) 305 return IsSigned ? SignedInt : UnsignedInt; 306 if (getLongWidth() >= BitWidth) 307 return IsSigned ? SignedLong : UnsignedLong; 308 if (getLongLongWidth() >= BitWidth) 309 return IsSigned ? SignedLongLong : UnsignedLongLong; 310 return NoInt; 311 } 312 313 FloatModeKind TargetInfo::getRealTypeByWidth(unsigned BitWidth, 314 FloatModeKind ExplicitType) const { 315 if (getHalfWidth() == BitWidth) 316 return FloatModeKind::Half; 317 if (getFloatWidth() == BitWidth) 318 return FloatModeKind::Float; 319 if (getDoubleWidth() == BitWidth) 320 return FloatModeKind::Double; 321 322 switch (BitWidth) { 323 case 96: 324 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended()) 325 return FloatModeKind::LongDouble; 326 break; 327 case 128: 328 // The caller explicitly asked for an IEEE compliant type but we still 329 // have to check if the target supports it. 330 if (ExplicitType == FloatModeKind::Float128) 331 return hasFloat128Type() ? FloatModeKind::Float128 332 : FloatModeKind::NoFloat; 333 if (ExplicitType == FloatModeKind::Ibm128) 334 return hasIbm128Type() ? FloatModeKind::Ibm128 335 : FloatModeKind::NoFloat; 336 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble() || 337 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad()) 338 return FloatModeKind::LongDouble; 339 if (hasFloat128Type()) 340 return FloatModeKind::Float128; 341 break; 342 } 343 344 return FloatModeKind::NoFloat; 345 } 346 347 /// getTypeAlign - Return the alignment (in bits) of the specified integer type 348 /// enum. For example, SignedInt -> getIntAlign(). 349 unsigned TargetInfo::getTypeAlign(IntType T) const { 350 switch (T) { 351 default: llvm_unreachable("not an integer!"); 352 case SignedChar: 353 case UnsignedChar: return getCharAlign(); 354 case SignedShort: 355 case UnsignedShort: return getShortAlign(); 356 case SignedInt: 357 case UnsignedInt: return getIntAlign(); 358 case SignedLong: 359 case UnsignedLong: return getLongAlign(); 360 case SignedLongLong: 361 case UnsignedLongLong: return getLongLongAlign(); 362 }; 363 } 364 365 /// isTypeSigned - Return whether an integer types is signed. Returns true if 366 /// the type is signed; false otherwise. 367 bool TargetInfo::isTypeSigned(IntType T) { 368 switch (T) { 369 default: llvm_unreachable("not an integer!"); 370 case SignedChar: 371 case SignedShort: 372 case SignedInt: 373 case SignedLong: 374 case SignedLongLong: 375 return true; 376 case UnsignedChar: 377 case UnsignedShort: 378 case UnsignedInt: 379 case UnsignedLong: 380 case UnsignedLongLong: 381 return false; 382 }; 383 } 384 385 /// adjust - Set forced language options. 386 /// Apply changes to the target information with respect to certain 387 /// language options which change the target configuration and adjust 388 /// the language based on the target options where applicable. 389 void TargetInfo::adjust(DiagnosticsEngine &Diags, LangOptions &Opts) { 390 if (Opts.NoBitFieldTypeAlign) 391 UseBitFieldTypeAlignment = false; 392 393 switch (Opts.WCharSize) { 394 default: llvm_unreachable("invalid wchar_t width"); 395 case 0: break; 396 case 1: WCharType = Opts.WCharIsSigned ? SignedChar : UnsignedChar; break; 397 case 2: WCharType = Opts.WCharIsSigned ? SignedShort : UnsignedShort; break; 398 case 4: WCharType = Opts.WCharIsSigned ? SignedInt : UnsignedInt; break; 399 } 400 401 if (Opts.AlignDouble) { 402 DoubleAlign = LongLongAlign = 64; 403 LongDoubleAlign = 64; 404 } 405 406 if (Opts.OpenCL) { 407 // OpenCL C requires specific widths for types, irrespective of 408 // what these normally are for the target. 409 // We also define long long and long double here, although the 410 // OpenCL standard only mentions these as "reserved". 411 IntWidth = IntAlign = 32; 412 LongWidth = LongAlign = 64; 413 LongLongWidth = LongLongAlign = 128; 414 HalfWidth = HalfAlign = 16; 415 FloatWidth = FloatAlign = 32; 416 417 // Embedded 32-bit targets (OpenCL EP) might have double C type 418 // defined as float. Let's not override this as it might lead 419 // to generating illegal code that uses 64bit doubles. 420 if (DoubleWidth != FloatWidth) { 421 DoubleWidth = DoubleAlign = 64; 422 DoubleFormat = &llvm::APFloat::IEEEdouble(); 423 } 424 LongDoubleWidth = LongDoubleAlign = 128; 425 426 unsigned MaxPointerWidth = getMaxPointerWidth(); 427 assert(MaxPointerWidth == 32 || MaxPointerWidth == 64); 428 bool Is32BitArch = MaxPointerWidth == 32; 429 SizeType = Is32BitArch ? UnsignedInt : UnsignedLong; 430 PtrDiffType = Is32BitArch ? SignedInt : SignedLong; 431 IntPtrType = Is32BitArch ? SignedInt : SignedLong; 432 433 IntMaxType = SignedLongLong; 434 Int64Type = SignedLong; 435 436 HalfFormat = &llvm::APFloat::IEEEhalf(); 437 FloatFormat = &llvm::APFloat::IEEEsingle(); 438 LongDoubleFormat = &llvm::APFloat::IEEEquad(); 439 440 // OpenCL C v3.0 s6.7.5 - The generic address space requires support for 441 // OpenCL C 2.0 or OpenCL C 3.0 with the __opencl_c_generic_address_space 442 // feature 443 // OpenCL C v3.0 s6.2.1 - OpenCL pipes require support of OpenCL C 2.0 444 // or later and __opencl_c_pipes feature 445 // FIXME: These language options are also defined in setLangDefaults() 446 // for OpenCL C 2.0 but with no access to target capabilities. Target 447 // should be immutable once created and thus these language options need 448 // to be defined only once. 449 if (Opts.getOpenCLCompatibleVersion() == 300) { 450 const auto &OpenCLFeaturesMap = getSupportedOpenCLOpts(); 451 Opts.OpenCLGenericAddressSpace = hasFeatureEnabled( 452 OpenCLFeaturesMap, "__opencl_c_generic_address_space"); 453 Opts.OpenCLPipes = 454 hasFeatureEnabled(OpenCLFeaturesMap, "__opencl_c_pipes"); 455 Opts.Blocks = 456 hasFeatureEnabled(OpenCLFeaturesMap, "__opencl_c_device_enqueue"); 457 } 458 } 459 460 if (Opts.DoubleSize) { 461 if (Opts.DoubleSize == 32) { 462 DoubleWidth = 32; 463 LongDoubleWidth = 32; 464 DoubleFormat = &llvm::APFloat::IEEEsingle(); 465 LongDoubleFormat = &llvm::APFloat::IEEEsingle(); 466 } else if (Opts.DoubleSize == 64) { 467 DoubleWidth = 64; 468 LongDoubleWidth = 64; 469 DoubleFormat = &llvm::APFloat::IEEEdouble(); 470 LongDoubleFormat = &llvm::APFloat::IEEEdouble(); 471 } 472 } 473 474 if (Opts.LongDoubleSize) { 475 if (Opts.LongDoubleSize == DoubleWidth) { 476 LongDoubleWidth = DoubleWidth; 477 LongDoubleAlign = DoubleAlign; 478 LongDoubleFormat = DoubleFormat; 479 } else if (Opts.LongDoubleSize == 128) { 480 LongDoubleWidth = LongDoubleAlign = 128; 481 LongDoubleFormat = &llvm::APFloat::IEEEquad(); 482 } else if (Opts.LongDoubleSize == 80) { 483 LongDoubleFormat = &llvm::APFloat::x87DoubleExtended(); 484 if (getTriple().isWindowsMSVCEnvironment()) { 485 LongDoubleWidth = 128; 486 LongDoubleAlign = 128; 487 } else { // Linux 488 if (getTriple().getArch() == llvm::Triple::x86) { 489 LongDoubleWidth = 96; 490 LongDoubleAlign = 32; 491 } else { 492 LongDoubleWidth = 128; 493 LongDoubleAlign = 128; 494 } 495 } 496 } 497 } 498 499 if (Opts.NewAlignOverride) 500 NewAlign = Opts.NewAlignOverride * getCharWidth(); 501 502 // Each unsigned fixed point type has the same number of fractional bits as 503 // its corresponding signed type. 504 PaddingOnUnsignedFixedPoint |= Opts.PaddingOnUnsignedFixedPoint; 505 CheckFixedPointBits(); 506 507 if (Opts.ProtectParens && !checkArithmeticFenceSupported()) { 508 Diags.Report(diag::err_opt_not_valid_on_target) << "-fprotect-parens"; 509 Opts.ProtectParens = false; 510 } 511 512 if (Opts.MaxBitIntWidth) 513 MaxBitIntWidth = static_cast<unsigned>(Opts.MaxBitIntWidth); 514 515 if (Opts.FakeAddressSpaceMap) 516 AddrSpaceMap = &FakeAddrSpaceMap; 517 } 518 519 bool TargetInfo::initFeatureMap( 520 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU, 521 const std::vector<std::string> &FeatureVec) const { 522 for (const auto &F : FeatureVec) { 523 StringRef Name = F; 524 if (Name.empty()) 525 continue; 526 // Apply the feature via the target. 527 if (Name[0] != '+' && Name[0] != '-') 528 Diags.Report(diag::warn_fe_backend_invalid_feature_flag) << Name; 529 else 530 setFeatureEnabled(Features, Name.substr(1), Name[0] == '+'); 531 } 532 return true; 533 } 534 535 ParsedTargetAttr TargetInfo::parseTargetAttr(StringRef Features) const { 536 ParsedTargetAttr Ret; 537 if (Features == "default") 538 return Ret; 539 SmallVector<StringRef, 1> AttrFeatures; 540 Features.split(AttrFeatures, ","); 541 542 // Grab the various features and prepend a "+" to turn on the feature to 543 // the backend and add them to our existing set of features. 544 for (auto &Feature : AttrFeatures) { 545 // Go ahead and trim whitespace rather than either erroring or 546 // accepting it weirdly. 547 Feature = Feature.trim(); 548 549 // TODO: Support the fpmath option. It will require checking 550 // overall feature validity for the function with the rest of the 551 // attributes on the function. 552 if (Feature.startswith("fpmath=")) 553 continue; 554 555 if (Feature.startswith("branch-protection=")) { 556 Ret.BranchProtection = Feature.split('=').second.trim(); 557 continue; 558 } 559 560 // While we're here iterating check for a different target cpu. 561 if (Feature.startswith("arch=")) { 562 if (!Ret.CPU.empty()) 563 Ret.Duplicate = "arch="; 564 else 565 Ret.CPU = Feature.split("=").second.trim(); 566 } else if (Feature.startswith("tune=")) { 567 if (!Ret.Tune.empty()) 568 Ret.Duplicate = "tune="; 569 else 570 Ret.Tune = Feature.split("=").second.trim(); 571 } else if (Feature.startswith("no-")) 572 Ret.Features.push_back("-" + Feature.split("-").second.str()); 573 else 574 Ret.Features.push_back("+" + Feature.str()); 575 } 576 return Ret; 577 } 578 579 TargetInfo::CallingConvKind 580 TargetInfo::getCallingConvKind(bool ClangABICompat4) const { 581 if (getCXXABI() != TargetCXXABI::Microsoft && 582 (ClangABICompat4 || getTriple().isPS4())) 583 return CCK_ClangABI4OrPS4; 584 return CCK_Default; 585 } 586 587 bool TargetInfo::areDefaultedSMFStillPOD(const LangOptions &LangOpts) const { 588 return LangOpts.getClangABICompat() > LangOptions::ClangABI::Ver15; 589 } 590 591 LangAS TargetInfo::getOpenCLTypeAddrSpace(OpenCLTypeKind TK) const { 592 switch (TK) { 593 case OCLTK_Image: 594 case OCLTK_Pipe: 595 return LangAS::opencl_global; 596 597 case OCLTK_Sampler: 598 return LangAS::opencl_constant; 599 600 default: 601 return LangAS::Default; 602 } 603 } 604 605 //===----------------------------------------------------------------------===// 606 607 608 static StringRef removeGCCRegisterPrefix(StringRef Name) { 609 if (Name[0] == '%' || Name[0] == '#') 610 Name = Name.substr(1); 611 612 return Name; 613 } 614 615 /// isValidClobber - Returns whether the passed in string is 616 /// a valid clobber in an inline asm statement. This is used by 617 /// Sema. 618 bool TargetInfo::isValidClobber(StringRef Name) const { 619 return (isValidGCCRegisterName(Name) || Name == "memory" || Name == "cc" || 620 Name == "unwind"); 621 } 622 623 /// isValidGCCRegisterName - Returns whether the passed in string 624 /// is a valid register name according to GCC. This is used by Sema for 625 /// inline asm statements. 626 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const { 627 if (Name.empty()) 628 return false; 629 630 // Get rid of any register prefix. 631 Name = removeGCCRegisterPrefix(Name); 632 if (Name.empty()) 633 return false; 634 635 ArrayRef<const char *> Names = getGCCRegNames(); 636 637 // If we have a number it maps to an entry in the register name array. 638 if (isDigit(Name[0])) { 639 unsigned n; 640 if (!Name.getAsInteger(0, n)) 641 return n < Names.size(); 642 } 643 644 // Check register names. 645 if (llvm::is_contained(Names, Name)) 646 return true; 647 648 // Check any additional names that we have. 649 for (const AddlRegName &ARN : getGCCAddlRegNames()) 650 for (const char *AN : ARN.Names) { 651 if (!AN) 652 break; 653 // Make sure the register that the additional name is for is within 654 // the bounds of the register names from above. 655 if (AN == Name && ARN.RegNum < Names.size()) 656 return true; 657 } 658 659 // Now check aliases. 660 for (const GCCRegAlias &GRA : getGCCRegAliases()) 661 for (const char *A : GRA.Aliases) { 662 if (!A) 663 break; 664 if (A == Name) 665 return true; 666 } 667 668 return false; 669 } 670 671 StringRef TargetInfo::getNormalizedGCCRegisterName(StringRef Name, 672 bool ReturnCanonical) const { 673 assert(isValidGCCRegisterName(Name) && "Invalid register passed in"); 674 675 // Get rid of any register prefix. 676 Name = removeGCCRegisterPrefix(Name); 677 678 ArrayRef<const char *> Names = getGCCRegNames(); 679 680 // First, check if we have a number. 681 if (isDigit(Name[0])) { 682 unsigned n; 683 if (!Name.getAsInteger(0, n)) { 684 assert(n < Names.size() && "Out of bounds register number!"); 685 return Names[n]; 686 } 687 } 688 689 // Check any additional names that we have. 690 for (const AddlRegName &ARN : getGCCAddlRegNames()) 691 for (const char *AN : ARN.Names) { 692 if (!AN) 693 break; 694 // Make sure the register that the additional name is for is within 695 // the bounds of the register names from above. 696 if (AN == Name && ARN.RegNum < Names.size()) 697 return ReturnCanonical ? Names[ARN.RegNum] : Name; 698 } 699 700 // Now check aliases. 701 for (const GCCRegAlias &RA : getGCCRegAliases()) 702 for (const char *A : RA.Aliases) { 703 if (!A) 704 break; 705 if (A == Name) 706 return RA.Register; 707 } 708 709 return Name; 710 } 711 712 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const { 713 const char *Name = Info.getConstraintStr().c_str(); 714 // An output constraint must start with '=' or '+' 715 if (*Name != '=' && *Name != '+') 716 return false; 717 718 if (*Name == '+') 719 Info.setIsReadWrite(); 720 721 Name++; 722 while (*Name) { 723 switch (*Name) { 724 default: 725 if (!validateAsmConstraint(Name, Info)) { 726 // FIXME: We temporarily return false 727 // so we can add more constraints as we hit it. 728 // Eventually, an unknown constraint should just be treated as 'g'. 729 return false; 730 } 731 break; 732 case '&': // early clobber. 733 Info.setEarlyClobber(); 734 break; 735 case '%': // commutative. 736 // FIXME: Check that there is a another register after this one. 737 break; 738 case 'r': // general register. 739 Info.setAllowsRegister(); 740 break; 741 case 'm': // memory operand. 742 case 'o': // offsetable memory operand. 743 case 'V': // non-offsetable memory operand. 744 case '<': // autodecrement memory operand. 745 case '>': // autoincrement memory operand. 746 Info.setAllowsMemory(); 747 break; 748 case 'g': // general register, memory operand or immediate integer. 749 case 'X': // any operand. 750 Info.setAllowsRegister(); 751 Info.setAllowsMemory(); 752 break; 753 case ',': // multiple alternative constraint. Pass it. 754 // Handle additional optional '=' or '+' modifiers. 755 if (Name[1] == '=' || Name[1] == '+') 756 Name++; 757 break; 758 case '#': // Ignore as constraint. 759 while (Name[1] && Name[1] != ',') 760 Name++; 761 break; 762 case '?': // Disparage slightly code. 763 case '!': // Disparage severely. 764 case '*': // Ignore for choosing register preferences. 765 case 'i': // Ignore i,n,E,F as output constraints (match from the other 766 // chars) 767 case 'n': 768 case 'E': 769 case 'F': 770 break; // Pass them. 771 } 772 773 Name++; 774 } 775 776 // Early clobber with a read-write constraint which doesn't permit registers 777 // is invalid. 778 if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister()) 779 return false; 780 781 // If a constraint allows neither memory nor register operands it contains 782 // only modifiers. Reject it. 783 return Info.allowsMemory() || Info.allowsRegister(); 784 } 785 786 bool TargetInfo::resolveSymbolicName(const char *&Name, 787 ArrayRef<ConstraintInfo> OutputConstraints, 788 unsigned &Index) const { 789 assert(*Name == '[' && "Symbolic name did not start with '['"); 790 Name++; 791 const char *Start = Name; 792 while (*Name && *Name != ']') 793 Name++; 794 795 if (!*Name) { 796 // Missing ']' 797 return false; 798 } 799 800 std::string SymbolicName(Start, Name - Start); 801 802 for (Index = 0; Index != OutputConstraints.size(); ++Index) 803 if (SymbolicName == OutputConstraints[Index].getName()) 804 return true; 805 806 return false; 807 } 808 809 bool TargetInfo::validateInputConstraint( 810 MutableArrayRef<ConstraintInfo> OutputConstraints, 811 ConstraintInfo &Info) const { 812 const char *Name = Info.ConstraintStr.c_str(); 813 814 if (!*Name) 815 return false; 816 817 while (*Name) { 818 switch (*Name) { 819 default: 820 // Check if we have a matching constraint 821 if (*Name >= '0' && *Name <= '9') { 822 const char *DigitStart = Name; 823 while (Name[1] >= '0' && Name[1] <= '9') 824 Name++; 825 const char *DigitEnd = Name; 826 unsigned i; 827 if (StringRef(DigitStart, DigitEnd - DigitStart + 1) 828 .getAsInteger(10, i)) 829 return false; 830 831 // Check if matching constraint is out of bounds. 832 if (i >= OutputConstraints.size()) return false; 833 834 // A number must refer to an output only operand. 835 if (OutputConstraints[i].isReadWrite()) 836 return false; 837 838 // If the constraint is already tied, it must be tied to the 839 // same operand referenced to by the number. 840 if (Info.hasTiedOperand() && Info.getTiedOperand() != i) 841 return false; 842 843 // The constraint should have the same info as the respective 844 // output constraint. 845 Info.setTiedOperand(i, OutputConstraints[i]); 846 } else if (!validateAsmConstraint(Name, Info)) { 847 // FIXME: This error return is in place temporarily so we can 848 // add more constraints as we hit it. Eventually, an unknown 849 // constraint should just be treated as 'g'. 850 return false; 851 } 852 break; 853 case '[': { 854 unsigned Index = 0; 855 if (!resolveSymbolicName(Name, OutputConstraints, Index)) 856 return false; 857 858 // If the constraint is already tied, it must be tied to the 859 // same operand referenced to by the number. 860 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index) 861 return false; 862 863 // A number must refer to an output only operand. 864 if (OutputConstraints[Index].isReadWrite()) 865 return false; 866 867 Info.setTiedOperand(Index, OutputConstraints[Index]); 868 break; 869 } 870 case '%': // commutative 871 // FIXME: Fail if % is used with the last operand. 872 break; 873 case 'i': // immediate integer. 874 break; 875 case 'n': // immediate integer with a known value. 876 Info.setRequiresImmediate(); 877 break; 878 case 'I': // Various constant constraints with target-specific meanings. 879 case 'J': 880 case 'K': 881 case 'L': 882 case 'M': 883 case 'N': 884 case 'O': 885 case 'P': 886 if (!validateAsmConstraint(Name, Info)) 887 return false; 888 break; 889 case 'r': // general register. 890 Info.setAllowsRegister(); 891 break; 892 case 'm': // memory operand. 893 case 'o': // offsettable memory operand. 894 case 'V': // non-offsettable memory operand. 895 case '<': // autodecrement memory operand. 896 case '>': // autoincrement memory operand. 897 Info.setAllowsMemory(); 898 break; 899 case 'g': // general register, memory operand or immediate integer. 900 case 'X': // any operand. 901 Info.setAllowsRegister(); 902 Info.setAllowsMemory(); 903 break; 904 case 'E': // immediate floating point. 905 case 'F': // immediate floating point. 906 case 'p': // address operand. 907 break; 908 case ',': // multiple alternative constraint. Ignore comma. 909 break; 910 case '#': // Ignore as constraint. 911 while (Name[1] && Name[1] != ',') 912 Name++; 913 break; 914 case '?': // Disparage slightly code. 915 case '!': // Disparage severely. 916 case '*': // Ignore for choosing register preferences. 917 break; // Pass them. 918 } 919 920 Name++; 921 } 922 923 return true; 924 } 925 926 void TargetInfo::CheckFixedPointBits() const { 927 // Check that the number of fractional and integral bits (and maybe sign) can 928 // fit into the bits given for a fixed point type. 929 assert(ShortAccumScale + getShortAccumIBits() + 1 <= ShortAccumWidth); 930 assert(AccumScale + getAccumIBits() + 1 <= AccumWidth); 931 assert(LongAccumScale + getLongAccumIBits() + 1 <= LongAccumWidth); 932 assert(getUnsignedShortAccumScale() + getUnsignedShortAccumIBits() <= 933 ShortAccumWidth); 934 assert(getUnsignedAccumScale() + getUnsignedAccumIBits() <= AccumWidth); 935 assert(getUnsignedLongAccumScale() + getUnsignedLongAccumIBits() <= 936 LongAccumWidth); 937 938 assert(getShortFractScale() + 1 <= ShortFractWidth); 939 assert(getFractScale() + 1 <= FractWidth); 940 assert(getLongFractScale() + 1 <= LongFractWidth); 941 assert(getUnsignedShortFractScale() <= ShortFractWidth); 942 assert(getUnsignedFractScale() <= FractWidth); 943 assert(getUnsignedLongFractScale() <= LongFractWidth); 944 945 // Each unsigned fract type has either the same number of fractional bits 946 // as, or one more fractional bit than, its corresponding signed fract type. 947 assert(getShortFractScale() == getUnsignedShortFractScale() || 948 getShortFractScale() == getUnsignedShortFractScale() - 1); 949 assert(getFractScale() == getUnsignedFractScale() || 950 getFractScale() == getUnsignedFractScale() - 1); 951 assert(getLongFractScale() == getUnsignedLongFractScale() || 952 getLongFractScale() == getUnsignedLongFractScale() - 1); 953 954 // When arranged in order of increasing rank (see 6.3.1.3a), the number of 955 // fractional bits is nondecreasing for each of the following sets of 956 // fixed-point types: 957 // - signed fract types 958 // - unsigned fract types 959 // - signed accum types 960 // - unsigned accum types. 961 assert(getLongFractScale() >= getFractScale() && 962 getFractScale() >= getShortFractScale()); 963 assert(getUnsignedLongFractScale() >= getUnsignedFractScale() && 964 getUnsignedFractScale() >= getUnsignedShortFractScale()); 965 assert(LongAccumScale >= AccumScale && AccumScale >= ShortAccumScale); 966 assert(getUnsignedLongAccumScale() >= getUnsignedAccumScale() && 967 getUnsignedAccumScale() >= getUnsignedShortAccumScale()); 968 969 // When arranged in order of increasing rank (see 6.3.1.3a), the number of 970 // integral bits is nondecreasing for each of the following sets of 971 // fixed-point types: 972 // - signed accum types 973 // - unsigned accum types 974 assert(getLongAccumIBits() >= getAccumIBits() && 975 getAccumIBits() >= getShortAccumIBits()); 976 assert(getUnsignedLongAccumIBits() >= getUnsignedAccumIBits() && 977 getUnsignedAccumIBits() >= getUnsignedShortAccumIBits()); 978 979 // Each signed accum type has at least as many integral bits as its 980 // corresponding unsigned accum type. 981 assert(getShortAccumIBits() >= getUnsignedShortAccumIBits()); 982 assert(getAccumIBits() >= getUnsignedAccumIBits()); 983 assert(getLongAccumIBits() >= getUnsignedLongAccumIBits()); 984 } 985 986 void TargetInfo::copyAuxTarget(const TargetInfo *Aux) { 987 auto *Target = static_cast<TransferrableTargetInfo*>(this); 988 auto *Src = static_cast<const TransferrableTargetInfo*>(Aux); 989 *Target = *Src; 990 } 991