1 //===--- TargetInfo.h - Expose information about the target -----*- 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 /// \file 10 /// Defines the clang::TargetInfo interface. 11 /// 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_CLANG_BASIC_TARGETINFO_H 15 #define LLVM_CLANG_BASIC_TARGETINFO_H 16 17 #include "clang/Basic/AddressSpaces.h" 18 #include "clang/Basic/BitmaskEnum.h" 19 #include "clang/Basic/CodeGenOptions.h" 20 #include "clang/Basic/LLVM.h" 21 #include "clang/Basic/LangOptions.h" 22 #include "clang/Basic/Specifiers.h" 23 #include "clang/Basic/TargetCXXABI.h" 24 #include "clang/Basic/TargetOptions.h" 25 #include "llvm/ADT/APFloat.h" 26 #include "llvm/ADT/APInt.h" 27 #include "llvm/ADT/ArrayRef.h" 28 #include "llvm/ADT/IntrusiveRefCntPtr.h" 29 #include "llvm/ADT/SmallSet.h" 30 #include "llvm/ADT/StringMap.h" 31 #include "llvm/ADT/StringRef.h" 32 #include "llvm/ADT/StringSet.h" 33 #include "llvm/Frontend/OpenMP/OMPGridValues.h" 34 #include "llvm/IR/DerivedTypes.h" 35 #include "llvm/Support/DataTypes.h" 36 #include "llvm/Support/Error.h" 37 #include "llvm/Support/VersionTuple.h" 38 #include "llvm/TargetParser/Triple.h" 39 #include <cassert> 40 #include <optional> 41 #include <string> 42 #include <vector> 43 44 namespace llvm { 45 struct fltSemantics; 46 } 47 48 namespace clang { 49 class DiagnosticsEngine; 50 class LangOptions; 51 class CodeGenOptions; 52 class MacroBuilder; 53 54 /// Contains information gathered from parsing the contents of TargetAttr. 55 struct ParsedTargetAttr { 56 std::vector<std::string> Features; 57 StringRef CPU; 58 StringRef Tune; 59 StringRef BranchProtection; 60 StringRef Duplicate; 61 bool operator ==(const ParsedTargetAttr &Other) const { 62 return Duplicate == Other.Duplicate && CPU == Other.CPU && 63 Tune == Other.Tune && BranchProtection == Other.BranchProtection && 64 Features == Other.Features; 65 } 66 }; 67 68 namespace Builtin { struct Info; } 69 70 enum class FloatModeKind { 71 NoFloat = 0, 72 Half = 1 << 0, 73 Float = 1 << 1, 74 Double = 1 << 2, 75 LongDouble = 1 << 3, 76 Float128 = 1 << 4, 77 Ibm128 = 1 << 5, 78 LLVM_MARK_AS_BITMASK_ENUM(Ibm128) 79 }; 80 81 /// Fields controlling how types are laid out in memory; these may need to 82 /// be copied for targets like AMDGPU that base their ABIs on an auxiliary 83 /// CPU target. 84 struct TransferrableTargetInfo { 85 unsigned char PointerWidth, PointerAlign; 86 unsigned char BoolWidth, BoolAlign; 87 unsigned char IntWidth, IntAlign; 88 unsigned char HalfWidth, HalfAlign; 89 unsigned char BFloat16Width, BFloat16Align; 90 unsigned char FloatWidth, FloatAlign; 91 unsigned char DoubleWidth, DoubleAlign; 92 unsigned char LongDoubleWidth, LongDoubleAlign, Float128Align, Ibm128Align; 93 unsigned char LargeArrayMinWidth, LargeArrayAlign; 94 unsigned char LongWidth, LongAlign; 95 unsigned char LongLongWidth, LongLongAlign; 96 unsigned char Int128Align; 97 98 // Fixed point bit widths 99 unsigned char ShortAccumWidth, ShortAccumAlign; 100 unsigned char AccumWidth, AccumAlign; 101 unsigned char LongAccumWidth, LongAccumAlign; 102 unsigned char ShortFractWidth, ShortFractAlign; 103 unsigned char FractWidth, FractAlign; 104 unsigned char LongFractWidth, LongFractAlign; 105 106 // If true, unsigned fixed point types have the same number of fractional bits 107 // as their signed counterparts, forcing the unsigned types to have one extra 108 // bit of padding. Otherwise, unsigned fixed point types have 109 // one more fractional bit than its corresponding signed type. This is false 110 // by default. 111 bool PaddingOnUnsignedFixedPoint; 112 113 // Fixed point integral and fractional bit sizes 114 // Saturated types share the same integral/fractional bits as their 115 // corresponding unsaturated types. 116 // For simplicity, the fractional bits in a _Fract type will be one less the 117 // width of that _Fract type. This leaves all signed _Fract types having no 118 // padding and unsigned _Fract types will only have 1 bit of padding after the 119 // sign if PaddingOnUnsignedFixedPoint is set. 120 unsigned char ShortAccumScale; 121 unsigned char AccumScale; 122 unsigned char LongAccumScale; 123 124 unsigned char DefaultAlignForAttributeAligned; 125 unsigned char MinGlobalAlign; 126 127 unsigned short SuitableAlign; 128 unsigned short NewAlign; 129 unsigned MaxVectorAlign; 130 unsigned MaxTLSAlign; 131 132 const llvm::fltSemantics *HalfFormat, *BFloat16Format, *FloatFormat, 133 *DoubleFormat, *LongDoubleFormat, *Float128Format, *Ibm128Format; 134 135 ///===---- Target Data Type Query Methods -------------------------------===// 136 enum IntType { 137 NoInt = 0, 138 SignedChar, 139 UnsignedChar, 140 SignedShort, 141 UnsignedShort, 142 SignedInt, 143 UnsignedInt, 144 SignedLong, 145 UnsignedLong, 146 SignedLongLong, 147 UnsignedLongLong 148 }; 149 150 protected: 151 IntType SizeType, IntMaxType, PtrDiffType, IntPtrType, WCharType, WIntType, 152 Char16Type, Char32Type, Int64Type, Int16Type, SigAtomicType, 153 ProcessIDType; 154 155 /// Whether Objective-C's built-in boolean type should be signed char. 156 /// 157 /// Otherwise, when this flag is not set, the normal built-in boolean type is 158 /// used. 159 unsigned UseSignedCharForObjCBool : 1; 160 161 /// Control whether the alignment of bit-field types is respected when laying 162 /// out structures. If true, then the alignment of the bit-field type will be 163 /// used to (a) impact the alignment of the containing structure, and (b) 164 /// ensure that the individual bit-field will not straddle an alignment 165 /// boundary. 166 unsigned UseBitFieldTypeAlignment : 1; 167 168 /// Whether zero length bitfields (e.g., int : 0;) force alignment of 169 /// the next bitfield. 170 /// 171 /// If the alignment of the zero length bitfield is greater than the member 172 /// that follows it, `bar', `bar' will be aligned as the type of the 173 /// zero-length bitfield. 174 unsigned UseZeroLengthBitfieldAlignment : 1; 175 176 /// Whether zero length bitfield alignment is respected if they are the 177 /// leading members. 178 unsigned UseLeadingZeroLengthBitfield : 1; 179 180 /// Whether explicit bit field alignment attributes are honored. 181 unsigned UseExplicitBitFieldAlignment : 1; 182 183 /// If non-zero, specifies a fixed alignment value for bitfields that follow 184 /// zero length bitfield, regardless of the zero length bitfield type. 185 unsigned ZeroLengthBitfieldBoundary; 186 187 /// If non-zero, specifies a maximum alignment to truncate alignment 188 /// specified in the aligned attribute of a static variable to this value. 189 unsigned MaxAlignedAttribute; 190 }; 191 192 /// OpenCL type kinds. 193 enum OpenCLTypeKind : uint8_t { 194 OCLTK_Default, 195 OCLTK_ClkEvent, 196 OCLTK_Event, 197 OCLTK_Image, 198 OCLTK_Pipe, 199 OCLTK_Queue, 200 OCLTK_ReserveID, 201 OCLTK_Sampler, 202 }; 203 204 /// Exposes information about the current target. 205 /// 206 class TargetInfo : public TransferrableTargetInfo, 207 public RefCountedBase<TargetInfo> { 208 std::shared_ptr<TargetOptions> TargetOpts; 209 llvm::Triple Triple; 210 protected: 211 // Target values set by the ctor of the actual target implementation. Default 212 // values are specified by the TargetInfo constructor. 213 bool BigEndian; 214 bool TLSSupported; 215 bool VLASupported; 216 bool NoAsmVariants; // True if {|} are normal characters. 217 bool HasLegalHalfType; // True if the backend supports operations on the half 218 // LLVM IR type. 219 bool HalfArgsAndReturns; 220 bool HasFloat128; 221 bool HasFloat16; 222 bool HasBFloat16; 223 bool HasFullBFloat16; // True if the backend supports native bfloat16 224 // arithmetic. Used to determine excess precision 225 // support in the frontend. 226 bool HasIbm128; 227 bool HasLongDouble; 228 bool HasFPReturn; 229 bool HasStrictFP; 230 231 unsigned char MaxAtomicPromoteWidth, MaxAtomicInlineWidth; 232 std::string DataLayoutString; 233 const char *UserLabelPrefix; 234 const char *MCountName; 235 unsigned char RegParmMax, SSERegParmMax; 236 TargetCXXABI TheCXXABI; 237 const LangASMap *AddrSpaceMap; 238 239 mutable StringRef PlatformName; 240 mutable VersionTuple PlatformMinVersion; 241 242 unsigned HasAlignMac68kSupport : 1; 243 unsigned RealTypeUsesObjCFPRetMask : llvm::BitWidth<FloatModeKind>; 244 unsigned ComplexLongDoubleUsesFP2Ret : 1; 245 246 unsigned HasBuiltinMSVaList : 1; 247 248 unsigned IsRenderScriptTarget : 1; 249 250 unsigned HasAArch64SVETypes : 1; 251 252 unsigned HasRISCVVTypes : 1; 253 254 unsigned AllowAMDGPUUnsafeFPAtomics : 1; 255 256 unsigned ARMCDECoprocMask : 8; 257 258 unsigned MaxOpenCLWorkGroupSize; 259 260 std::optional<unsigned> MaxBitIntWidth; 261 262 std::optional<llvm::Triple> DarwinTargetVariantTriple; 263 264 // TargetInfo Constructor. Default initializes all fields. 265 TargetInfo(const llvm::Triple &T); 266 267 // UserLabelPrefix must match DL's getGlobalPrefix() when interpreted 268 // as a DataLayout object. 269 void resetDataLayout(StringRef DL, const char *UserLabelPrefix = ""); 270 271 // Target features that are read-only and should not be disabled/enabled 272 // by command line options. Such features are for emitting predefined 273 // macros or checking availability of builtin functions and can be omitted 274 // in function attributes in IR. 275 llvm::StringSet<> ReadOnlyFeatures; 276 277 public: 278 /// Construct a target for the given options. 279 /// 280 /// \param Opts - The options to use to initialize the target. The target may 281 /// modify the options to canonicalize the target feature information to match 282 /// what the backend expects. 283 static TargetInfo * 284 CreateTargetInfo(DiagnosticsEngine &Diags, 285 const std::shared_ptr<TargetOptions> &Opts); 286 287 virtual ~TargetInfo(); 288 289 /// Retrieve the target options. 290 TargetOptions &getTargetOpts() const { 291 assert(TargetOpts && "Missing target options"); 292 return *TargetOpts; 293 } 294 295 /// The different kinds of __builtin_va_list types defined by 296 /// the target implementation. 297 enum BuiltinVaListKind { 298 /// typedef char* __builtin_va_list; 299 CharPtrBuiltinVaList = 0, 300 301 /// typedef void* __builtin_va_list; 302 VoidPtrBuiltinVaList, 303 304 /// __builtin_va_list as defined by the AArch64 ABI 305 /// http://infocenter.arm.com/help/topic/com.arm.doc.ihi0055a/IHI0055A_aapcs64.pdf 306 AArch64ABIBuiltinVaList, 307 308 /// __builtin_va_list as defined by the PNaCl ABI: 309 /// http://www.chromium.org/nativeclient/pnacl/bitcode-abi#TOC-Machine-Types 310 PNaClABIBuiltinVaList, 311 312 /// __builtin_va_list as defined by the Power ABI: 313 /// https://www.power.org 314 /// /resources/downloads/Power-Arch-32-bit-ABI-supp-1.0-Embedded.pdf 315 PowerABIBuiltinVaList, 316 317 /// __builtin_va_list as defined by the x86-64 ABI: 318 /// http://refspecs.linuxbase.org/elf/x86_64-abi-0.21.pdf 319 X86_64ABIBuiltinVaList, 320 321 /// __builtin_va_list as defined by ARM AAPCS ABI 322 /// http://infocenter.arm.com 323 // /help/topic/com.arm.doc.ihi0042d/IHI0042D_aapcs.pdf 324 AAPCSABIBuiltinVaList, 325 326 // typedef struct __va_list_tag 327 // { 328 // long __gpr; 329 // long __fpr; 330 // void *__overflow_arg_area; 331 // void *__reg_save_area; 332 // } va_list[1]; 333 SystemZBuiltinVaList, 334 335 // typedef struct __va_list_tag { 336 // void *__current_saved_reg_area_pointer; 337 // void *__saved_reg_area_end_pointer; 338 // void *__overflow_area_pointer; 339 //} va_list; 340 HexagonBuiltinVaList 341 }; 342 343 protected: 344 /// Specify if mangling based on address space map should be used or 345 /// not for language specific address spaces 346 bool UseAddrSpaceMapMangling; 347 348 public: 349 IntType getSizeType() const { return SizeType; } 350 IntType getSignedSizeType() const { 351 switch (SizeType) { 352 case UnsignedShort: 353 return SignedShort; 354 case UnsignedInt: 355 return SignedInt; 356 case UnsignedLong: 357 return SignedLong; 358 case UnsignedLongLong: 359 return SignedLongLong; 360 default: 361 llvm_unreachable("Invalid SizeType"); 362 } 363 } 364 IntType getIntMaxType() const { return IntMaxType; } 365 IntType getUIntMaxType() const { 366 return getCorrespondingUnsignedType(IntMaxType); 367 } 368 IntType getPtrDiffType(LangAS AddrSpace) const { 369 return AddrSpace == LangAS::Default ? PtrDiffType 370 : getPtrDiffTypeV(AddrSpace); 371 } 372 IntType getUnsignedPtrDiffType(LangAS AddrSpace) const { 373 return getCorrespondingUnsignedType(getPtrDiffType(AddrSpace)); 374 } 375 IntType getIntPtrType() const { return IntPtrType; } 376 IntType getUIntPtrType() const { 377 return getCorrespondingUnsignedType(IntPtrType); 378 } 379 IntType getWCharType() const { return WCharType; } 380 IntType getWIntType() const { return WIntType; } 381 IntType getChar16Type() const { return Char16Type; } 382 IntType getChar32Type() const { return Char32Type; } 383 IntType getInt64Type() const { return Int64Type; } 384 IntType getUInt64Type() const { 385 return getCorrespondingUnsignedType(Int64Type); 386 } 387 IntType getInt16Type() const { return Int16Type; } 388 IntType getUInt16Type() const { 389 return getCorrespondingUnsignedType(Int16Type); 390 } 391 IntType getSigAtomicType() const { return SigAtomicType; } 392 IntType getProcessIDType() const { return ProcessIDType; } 393 394 static IntType getCorrespondingUnsignedType(IntType T) { 395 switch (T) { 396 case SignedChar: 397 return UnsignedChar; 398 case SignedShort: 399 return UnsignedShort; 400 case SignedInt: 401 return UnsignedInt; 402 case SignedLong: 403 return UnsignedLong; 404 case SignedLongLong: 405 return UnsignedLongLong; 406 default: 407 llvm_unreachable("Unexpected signed integer type"); 408 } 409 } 410 411 /// In the event this target uses the same number of fractional bits for its 412 /// unsigned types as it does with its signed counterparts, there will be 413 /// exactly one bit of padding. 414 /// Return true if unsigned fixed point types have padding for this target. 415 bool doUnsignedFixedPointTypesHavePadding() const { 416 return PaddingOnUnsignedFixedPoint; 417 } 418 419 /// Return the width (in bits) of the specified integer type enum. 420 /// 421 /// For example, SignedInt -> getIntWidth(). 422 unsigned getTypeWidth(IntType T) const; 423 424 /// Return integer type with specified width. 425 virtual IntType getIntTypeByWidth(unsigned BitWidth, bool IsSigned) const; 426 427 /// Return the smallest integer type with at least the specified width. 428 virtual IntType getLeastIntTypeByWidth(unsigned BitWidth, 429 bool IsSigned) const; 430 431 /// Return floating point type with specified width. On PPC, there are 432 /// three possible types for 128-bit floating point: "PPC double-double", 433 /// IEEE 754R quad precision, and "long double" (which under the covers 434 /// is represented as one of those two). At this time, there is no support 435 /// for an explicit "PPC double-double" type (i.e. __ibm128) so we only 436 /// need to differentiate between "long double" and IEEE quad precision. 437 FloatModeKind getRealTypeByWidth(unsigned BitWidth, 438 FloatModeKind ExplicitType) const; 439 440 /// Return the alignment (in bits) of the specified integer type enum. 441 /// 442 /// For example, SignedInt -> getIntAlign(). 443 unsigned getTypeAlign(IntType T) const; 444 445 /// Returns true if the type is signed; false otherwise. 446 static bool isTypeSigned(IntType T); 447 448 /// Return the width of pointers on this target, for the 449 /// specified address space. 450 uint64_t getPointerWidth(LangAS AddrSpace) const { 451 return AddrSpace == LangAS::Default ? PointerWidth 452 : getPointerWidthV(AddrSpace); 453 } 454 uint64_t getPointerAlign(LangAS AddrSpace) const { 455 return AddrSpace == LangAS::Default ? PointerAlign 456 : getPointerAlignV(AddrSpace); 457 } 458 459 /// Return the maximum width of pointers on this target. 460 virtual uint64_t getMaxPointerWidth() const { 461 return PointerWidth; 462 } 463 464 /// Get integer value for null pointer. 465 /// \param AddrSpace address space of pointee in source language. 466 virtual uint64_t getNullPointerValue(LangAS AddrSpace) const { return 0; } 467 468 /// Return the size of '_Bool' and C++ 'bool' for this target, in bits. 469 unsigned getBoolWidth() const { return BoolWidth; } 470 471 /// Return the alignment of '_Bool' and C++ 'bool' for this target. 472 unsigned getBoolAlign() const { return BoolAlign; } 473 474 unsigned getCharWidth() const { return 8; } // FIXME 475 unsigned getCharAlign() const { return 8; } // FIXME 476 477 /// Return the size of 'signed short' and 'unsigned short' for this 478 /// target, in bits. 479 unsigned getShortWidth() const { return 16; } // FIXME 480 481 /// Return the alignment of 'signed short' and 'unsigned short' for 482 /// this target. 483 unsigned getShortAlign() const { return 16; } // FIXME 484 485 /// getIntWidth/Align - Return the size of 'signed int' and 'unsigned int' for 486 /// this target, in bits. 487 unsigned getIntWidth() const { return IntWidth; } 488 unsigned getIntAlign() const { return IntAlign; } 489 490 /// getLongWidth/Align - Return the size of 'signed long' and 'unsigned long' 491 /// for this target, in bits. 492 unsigned getLongWidth() const { return LongWidth; } 493 unsigned getLongAlign() const { return LongAlign; } 494 495 /// getLongLongWidth/Align - Return the size of 'signed long long' and 496 /// 'unsigned long long' for this target, in bits. 497 unsigned getLongLongWidth() const { return LongLongWidth; } 498 unsigned getLongLongAlign() const { return LongLongAlign; } 499 500 /// getInt128Align() - Returns the alignment of Int128. 501 unsigned getInt128Align() const { return Int128Align; } 502 503 /// getShortAccumWidth/Align - Return the size of 'signed short _Accum' and 504 /// 'unsigned short _Accum' for this target, in bits. 505 unsigned getShortAccumWidth() const { return ShortAccumWidth; } 506 unsigned getShortAccumAlign() const { return ShortAccumAlign; } 507 508 /// getAccumWidth/Align - Return the size of 'signed _Accum' and 509 /// 'unsigned _Accum' for this target, in bits. 510 unsigned getAccumWidth() const { return AccumWidth; } 511 unsigned getAccumAlign() const { return AccumAlign; } 512 513 /// getLongAccumWidth/Align - Return the size of 'signed long _Accum' and 514 /// 'unsigned long _Accum' for this target, in bits. 515 unsigned getLongAccumWidth() const { return LongAccumWidth; } 516 unsigned getLongAccumAlign() const { return LongAccumAlign; } 517 518 /// getShortFractWidth/Align - Return the size of 'signed short _Fract' and 519 /// 'unsigned short _Fract' for this target, in bits. 520 unsigned getShortFractWidth() const { return ShortFractWidth; } 521 unsigned getShortFractAlign() const { return ShortFractAlign; } 522 523 /// getFractWidth/Align - Return the size of 'signed _Fract' and 524 /// 'unsigned _Fract' for this target, in bits. 525 unsigned getFractWidth() const { return FractWidth; } 526 unsigned getFractAlign() const { return FractAlign; } 527 528 /// getLongFractWidth/Align - Return the size of 'signed long _Fract' and 529 /// 'unsigned long _Fract' for this target, in bits. 530 unsigned getLongFractWidth() const { return LongFractWidth; } 531 unsigned getLongFractAlign() const { return LongFractAlign; } 532 533 /// getShortAccumScale/IBits - Return the number of fractional/integral bits 534 /// in a 'signed short _Accum' type. 535 unsigned getShortAccumScale() const { return ShortAccumScale; } 536 unsigned getShortAccumIBits() const { 537 return ShortAccumWidth - ShortAccumScale - 1; 538 } 539 540 /// getAccumScale/IBits - Return the number of fractional/integral bits 541 /// in a 'signed _Accum' type. 542 unsigned getAccumScale() const { return AccumScale; } 543 unsigned getAccumIBits() const { return AccumWidth - AccumScale - 1; } 544 545 /// getLongAccumScale/IBits - Return the number of fractional/integral bits 546 /// in a 'signed long _Accum' type. 547 unsigned getLongAccumScale() const { return LongAccumScale; } 548 unsigned getLongAccumIBits() const { 549 return LongAccumWidth - LongAccumScale - 1; 550 } 551 552 /// getUnsignedShortAccumScale/IBits - Return the number of 553 /// fractional/integral bits in a 'unsigned short _Accum' type. 554 unsigned getUnsignedShortAccumScale() const { 555 return PaddingOnUnsignedFixedPoint ? ShortAccumScale : ShortAccumScale + 1; 556 } 557 unsigned getUnsignedShortAccumIBits() const { 558 return PaddingOnUnsignedFixedPoint 559 ? getShortAccumIBits() 560 : ShortAccumWidth - getUnsignedShortAccumScale(); 561 } 562 563 /// getUnsignedAccumScale/IBits - Return the number of fractional/integral 564 /// bits in a 'unsigned _Accum' type. 565 unsigned getUnsignedAccumScale() const { 566 return PaddingOnUnsignedFixedPoint ? AccumScale : AccumScale + 1; 567 } 568 unsigned getUnsignedAccumIBits() const { 569 return PaddingOnUnsignedFixedPoint ? getAccumIBits() 570 : AccumWidth - getUnsignedAccumScale(); 571 } 572 573 /// getUnsignedLongAccumScale/IBits - Return the number of fractional/integral 574 /// bits in a 'unsigned long _Accum' type. 575 unsigned getUnsignedLongAccumScale() const { 576 return PaddingOnUnsignedFixedPoint ? LongAccumScale : LongAccumScale + 1; 577 } 578 unsigned getUnsignedLongAccumIBits() const { 579 return PaddingOnUnsignedFixedPoint 580 ? getLongAccumIBits() 581 : LongAccumWidth - getUnsignedLongAccumScale(); 582 } 583 584 /// getShortFractScale - Return the number of fractional bits 585 /// in a 'signed short _Fract' type. 586 unsigned getShortFractScale() const { return ShortFractWidth - 1; } 587 588 /// getFractScale - Return the number of fractional bits 589 /// in a 'signed _Fract' type. 590 unsigned getFractScale() const { return FractWidth - 1; } 591 592 /// getLongFractScale - Return the number of fractional bits 593 /// in a 'signed long _Fract' type. 594 unsigned getLongFractScale() const { return LongFractWidth - 1; } 595 596 /// getUnsignedShortFractScale - Return the number of fractional bits 597 /// in a 'unsigned short _Fract' type. 598 unsigned getUnsignedShortFractScale() const { 599 return PaddingOnUnsignedFixedPoint ? getShortFractScale() 600 : getShortFractScale() + 1; 601 } 602 603 /// getUnsignedFractScale - Return the number of fractional bits 604 /// in a 'unsigned _Fract' type. 605 unsigned getUnsignedFractScale() const { 606 return PaddingOnUnsignedFixedPoint ? getFractScale() : getFractScale() + 1; 607 } 608 609 /// getUnsignedLongFractScale - Return the number of fractional bits 610 /// in a 'unsigned long _Fract' type. 611 unsigned getUnsignedLongFractScale() const { 612 return PaddingOnUnsignedFixedPoint ? getLongFractScale() 613 : getLongFractScale() + 1; 614 } 615 616 /// Determine whether the __int128 type is supported on this target. 617 virtual bool hasInt128Type() const { 618 return (getPointerWidth(LangAS::Default) >= 64) || 619 getTargetOpts().ForceEnableInt128; 620 } // FIXME 621 622 /// Determine whether the _BitInt type is supported on this target. This 623 /// limitation is put into place for ABI reasons. 624 /// FIXME: _BitInt is a required type in C23, so there's not much utility in 625 /// asking whether the target supported it or not; I think this should be 626 /// removed once backends have been alerted to the type and have had the 627 /// chance to do implementation work if needed. 628 virtual bool hasBitIntType() const { 629 return false; 630 } 631 632 // Different targets may support a different maximum width for the _BitInt 633 // type, depending on what operations are supported. 634 virtual size_t getMaxBitIntWidth() const { 635 // Consider -fexperimental-max-bitint-width= first. 636 if (MaxBitIntWidth) 637 return std::min<size_t>(*MaxBitIntWidth, llvm::IntegerType::MAX_INT_BITS); 638 639 // FIXME: this value should be llvm::IntegerType::MAX_INT_BITS, which is 640 // maximum bit width that LLVM claims its IR can support. However, most 641 // backends currently have a bug where they only support float to int 642 // conversion (and vice versa) on types that are <= 128 bits and crash 643 // otherwise. We're setting the max supported value to 128 to be 644 // conservative. 645 return 128; 646 } 647 648 /// Determine whether _Float16 is supported on this target. 649 virtual bool hasLegalHalfType() const { return HasLegalHalfType; } 650 651 /// Whether half args and returns are supported. 652 virtual bool allowHalfArgsAndReturns() const { return HalfArgsAndReturns; } 653 654 /// Determine whether the __float128 type is supported on this target. 655 virtual bool hasFloat128Type() const { return HasFloat128; } 656 657 /// Determine whether the _Float16 type is supported on this target. 658 virtual bool hasFloat16Type() const { return HasFloat16; } 659 660 /// Determine whether the _BFloat16 type is supported on this target. 661 virtual bool hasBFloat16Type() const { 662 return HasBFloat16 || HasFullBFloat16; 663 } 664 665 /// Determine whether the BFloat type is fully supported on this target, i.e 666 /// arithemtic operations. 667 virtual bool hasFullBFloat16Type() const { return HasFullBFloat16; } 668 669 /// Determine whether the __ibm128 type is supported on this target. 670 virtual bool hasIbm128Type() const { return HasIbm128; } 671 672 /// Determine whether the long double type is supported on this target. 673 virtual bool hasLongDoubleType() const { return HasLongDouble; } 674 675 /// Determine whether return of a floating point value is supported 676 /// on this target. 677 virtual bool hasFPReturn() const { return HasFPReturn; } 678 679 /// Determine whether constrained floating point is supported on this target. 680 virtual bool hasStrictFP() const { return HasStrictFP; } 681 682 /// Return the alignment that is the largest alignment ever used for any 683 /// scalar/SIMD data type on the target machine you are compiling for 684 /// (including types with an extended alignment requirement). 685 unsigned getSuitableAlign() const { return SuitableAlign; } 686 687 /// Return the default alignment for __attribute__((aligned)) on 688 /// this target, to be used if no alignment value is specified. 689 unsigned getDefaultAlignForAttributeAligned() const { 690 return DefaultAlignForAttributeAligned; 691 } 692 693 /// getMinGlobalAlign - Return the minimum alignment of a global variable, 694 /// unless its alignment is explicitly reduced via attributes. 695 virtual unsigned getMinGlobalAlign (uint64_t) const { 696 return MinGlobalAlign; 697 } 698 699 /// Return the largest alignment for which a suitably-sized allocation with 700 /// '::operator new(size_t)' is guaranteed to produce a correctly-aligned 701 /// pointer. 702 unsigned getNewAlign() const { 703 return NewAlign ? NewAlign : std::max(LongDoubleAlign, LongLongAlign); 704 } 705 706 /// getWCharWidth/Align - Return the size of 'wchar_t' for this target, in 707 /// bits. 708 unsigned getWCharWidth() const { return getTypeWidth(WCharType); } 709 unsigned getWCharAlign() const { return getTypeAlign(WCharType); } 710 711 /// getChar16Width/Align - Return the size of 'char16_t' for this target, in 712 /// bits. 713 unsigned getChar16Width() const { return getTypeWidth(Char16Type); } 714 unsigned getChar16Align() const { return getTypeAlign(Char16Type); } 715 716 /// getChar32Width/Align - Return the size of 'char32_t' for this target, in 717 /// bits. 718 unsigned getChar32Width() const { return getTypeWidth(Char32Type); } 719 unsigned getChar32Align() const { return getTypeAlign(Char32Type); } 720 721 /// getHalfWidth/Align/Format - Return the size/align/format of 'half'. 722 unsigned getHalfWidth() const { return HalfWidth; } 723 unsigned getHalfAlign() const { return HalfAlign; } 724 const llvm::fltSemantics &getHalfFormat() const { return *HalfFormat; } 725 726 /// getFloatWidth/Align/Format - Return the size/align/format of 'float'. 727 unsigned getFloatWidth() const { return FloatWidth; } 728 unsigned getFloatAlign() const { return FloatAlign; } 729 const llvm::fltSemantics &getFloatFormat() const { return *FloatFormat; } 730 731 /// getBFloat16Width/Align/Format - Return the size/align/format of '__bf16'. 732 unsigned getBFloat16Width() const { return BFloat16Width; } 733 unsigned getBFloat16Align() const { return BFloat16Align; } 734 const llvm::fltSemantics &getBFloat16Format() const { return *BFloat16Format; } 735 736 /// getDoubleWidth/Align/Format - Return the size/align/format of 'double'. 737 unsigned getDoubleWidth() const { return DoubleWidth; } 738 unsigned getDoubleAlign() const { return DoubleAlign; } 739 const llvm::fltSemantics &getDoubleFormat() const { return *DoubleFormat; } 740 741 /// getLongDoubleWidth/Align/Format - Return the size/align/format of 'long 742 /// double'. 743 unsigned getLongDoubleWidth() const { return LongDoubleWidth; } 744 unsigned getLongDoubleAlign() const { return LongDoubleAlign; } 745 const llvm::fltSemantics &getLongDoubleFormat() const { 746 return *LongDoubleFormat; 747 } 748 749 /// getFloat128Width/Align/Format - Return the size/align/format of 750 /// '__float128'. 751 unsigned getFloat128Width() const { return 128; } 752 unsigned getFloat128Align() const { return Float128Align; } 753 const llvm::fltSemantics &getFloat128Format() const { 754 return *Float128Format; 755 } 756 757 /// getIbm128Width/Align/Format - Return the size/align/format of 758 /// '__ibm128'. 759 unsigned getIbm128Width() const { return 128; } 760 unsigned getIbm128Align() const { return Ibm128Align; } 761 const llvm::fltSemantics &getIbm128Format() const { return *Ibm128Format; } 762 763 /// Return the mangled code of long double. 764 virtual const char *getLongDoubleMangling() const { return "e"; } 765 766 /// Return the mangled code of __float128. 767 virtual const char *getFloat128Mangling() const { return "g"; } 768 769 /// Return the mangled code of __ibm128. 770 virtual const char *getIbm128Mangling() const { 771 llvm_unreachable("ibm128 not implemented on this target"); 772 } 773 774 /// Return the mangled code of bfloat. 775 virtual const char *getBFloat16Mangling() const { return "DF16b"; } 776 777 /// Return the value for the C99 FLT_EVAL_METHOD macro. 778 virtual LangOptions::FPEvalMethodKind getFPEvalMethod() const { 779 return LangOptions::FPEvalMethodKind::FEM_Source; 780 } 781 782 virtual bool supportSourceEvalMethod() const { return true; } 783 784 // getLargeArrayMinWidth/Align - Return the minimum array size that is 785 // 'large' and its alignment. 786 unsigned getLargeArrayMinWidth() const { return LargeArrayMinWidth; } 787 unsigned getLargeArrayAlign() const { return LargeArrayAlign; } 788 789 /// Return the maximum width lock-free atomic operation which will 790 /// ever be supported for the given target 791 unsigned getMaxAtomicPromoteWidth() const { return MaxAtomicPromoteWidth; } 792 /// Return the maximum width lock-free atomic operation which can be 793 /// inlined given the supported features of the given target. 794 unsigned getMaxAtomicInlineWidth() const { return MaxAtomicInlineWidth; } 795 /// Set the maximum inline or promote width lock-free atomic operation 796 /// for the given target. 797 virtual void setMaxAtomicWidth() {} 798 /// Returns true if the given target supports lock-free atomic 799 /// operations at the specified width and alignment. 800 virtual bool hasBuiltinAtomic(uint64_t AtomicSizeInBits, 801 uint64_t AlignmentInBits) const { 802 return AtomicSizeInBits <= AlignmentInBits && 803 AtomicSizeInBits <= getMaxAtomicInlineWidth() && 804 (AtomicSizeInBits <= getCharWidth() || 805 llvm::isPowerOf2_64(AtomicSizeInBits / getCharWidth())); 806 } 807 808 /// Return the maximum vector alignment supported for the given target. 809 unsigned getMaxVectorAlign() const { return MaxVectorAlign; } 810 811 unsigned getMaxOpenCLWorkGroupSize() const { return MaxOpenCLWorkGroupSize; } 812 813 /// Return the alignment (in bits) of the thrown exception object. This is 814 /// only meaningful for targets that allocate C++ exceptions in a system 815 /// runtime, such as those using the Itanium C++ ABI. 816 virtual unsigned getExnObjectAlignment() const { 817 // Itanium says that an _Unwind_Exception has to be "double-word" 818 // aligned (and thus the end of it is also so-aligned), meaning 16 819 // bytes. Of course, that was written for the actual Itanium, 820 // which is a 64-bit platform. Classically, the ABI doesn't really 821 // specify the alignment on other platforms, but in practice 822 // libUnwind declares the struct with __attribute__((aligned)), so 823 // we assume that alignment here. (It's generally 16 bytes, but 824 // some targets overwrite it.) 825 return getDefaultAlignForAttributeAligned(); 826 } 827 828 /// Return the size of intmax_t and uintmax_t for this target, in bits. 829 unsigned getIntMaxTWidth() const { 830 return getTypeWidth(IntMaxType); 831 } 832 833 // Return the size of unwind_word for this target. 834 virtual unsigned getUnwindWordWidth() const { 835 return getPointerWidth(LangAS::Default); 836 } 837 838 /// Return the "preferred" register width on this target. 839 virtual unsigned getRegisterWidth() const { 840 // Currently we assume the register width on the target matches the pointer 841 // width, we can introduce a new variable for this if/when some target wants 842 // it. 843 return PointerWidth; 844 } 845 846 /// \brief Returns the default value of the __USER_LABEL_PREFIX__ macro, 847 /// which is the prefix given to user symbols by default. 848 /// 849 /// On most platforms this is "", but it is "_" on some. 850 const char *getUserLabelPrefix() const { return UserLabelPrefix; } 851 852 /// Returns the name of the mcount instrumentation function. 853 const char *getMCountName() const { 854 return MCountName; 855 } 856 857 /// Check if the Objective-C built-in boolean type should be signed 858 /// char. 859 /// 860 /// Otherwise, if this returns false, the normal built-in boolean type 861 /// should also be used for Objective-C. 862 bool useSignedCharForObjCBool() const { 863 return UseSignedCharForObjCBool; 864 } 865 void noSignedCharForObjCBool() { 866 UseSignedCharForObjCBool = false; 867 } 868 869 /// Check whether the alignment of bit-field types is respected 870 /// when laying out structures. 871 bool useBitFieldTypeAlignment() const { 872 return UseBitFieldTypeAlignment; 873 } 874 875 /// Check whether zero length bitfields should force alignment of 876 /// the next member. 877 bool useZeroLengthBitfieldAlignment() const { 878 return UseZeroLengthBitfieldAlignment; 879 } 880 881 /// Check whether zero length bitfield alignment is respected if they are 882 /// leading members. 883 bool useLeadingZeroLengthBitfield() const { 884 return UseLeadingZeroLengthBitfield; 885 } 886 887 /// Get the fixed alignment value in bits for a member that follows 888 /// a zero length bitfield. 889 unsigned getZeroLengthBitfieldBoundary() const { 890 return ZeroLengthBitfieldBoundary; 891 } 892 893 /// Get the maximum alignment in bits for a static variable with 894 /// aligned attribute. 895 unsigned getMaxAlignedAttribute() const { return MaxAlignedAttribute; } 896 897 /// Check whether explicit bitfield alignment attributes should be 898 // honored, as in "__attribute__((aligned(2))) int b : 1;". 899 bool useExplicitBitFieldAlignment() const { 900 return UseExplicitBitFieldAlignment; 901 } 902 903 /// Check whether this target support '\#pragma options align=mac68k'. 904 bool hasAlignMac68kSupport() const { 905 return HasAlignMac68kSupport; 906 } 907 908 /// Return the user string for the specified integer type enum. 909 /// 910 /// For example, SignedShort -> "short". 911 static const char *getTypeName(IntType T); 912 913 /// Return the constant suffix for the specified integer type enum. 914 /// 915 /// For example, SignedLong -> "L". 916 const char *getTypeConstantSuffix(IntType T) const; 917 918 /// Return the printf format modifier for the specified 919 /// integer type enum. 920 /// 921 /// For example, SignedLong -> "l". 922 static const char *getTypeFormatModifier(IntType T); 923 924 /// Check whether the given real type should use the "fpret" flavor of 925 /// Objective-C message passing on this target. 926 bool useObjCFPRetForRealType(FloatModeKind T) const { 927 return (int)((FloatModeKind)RealTypeUsesObjCFPRetMask & T); 928 } 929 930 /// Check whether _Complex long double should use the "fp2ret" flavor 931 /// of Objective-C message passing on this target. 932 bool useObjCFP2RetForComplexLongDouble() const { 933 return ComplexLongDoubleUsesFP2Ret; 934 } 935 936 /// Check whether llvm intrinsics such as llvm.convert.to.fp16 should be used 937 /// to convert to and from __fp16. 938 /// FIXME: This function should be removed once all targets stop using the 939 /// conversion intrinsics. 940 virtual bool useFP16ConversionIntrinsics() const { 941 return true; 942 } 943 944 /// Specify if mangling based on address space map should be used or 945 /// not for language specific address spaces 946 bool useAddressSpaceMapMangling() const { 947 return UseAddrSpaceMapMangling; 948 } 949 950 ///===---- Other target property query methods --------------------------===// 951 952 /// Appends the target-specific \#define values for this 953 /// target set to the specified buffer. 954 virtual void getTargetDefines(const LangOptions &Opts, 955 MacroBuilder &Builder) const = 0; 956 957 958 /// Return information about target-specific builtins for 959 /// the current primary target, and info about which builtins are non-portable 960 /// across the current set of primary and secondary targets. 961 virtual ArrayRef<Builtin::Info> getTargetBuiltins() const = 0; 962 963 /// Returns target-specific min and max values VScale_Range. 964 virtual std::optional<std::pair<unsigned, unsigned>> 965 getVScaleRange(const LangOptions &LangOpts) const { 966 return std::nullopt; 967 } 968 /// The __builtin_clz* and __builtin_ctz* built-in 969 /// functions are specified to have undefined results for zero inputs, but 970 /// on targets that support these operations in a way that provides 971 /// well-defined results for zero without loss of performance, it is a good 972 /// idea to avoid optimizing based on that undef behavior. 973 virtual bool isCLZForZeroUndef() const { return true; } 974 975 /// Returns the kind of __builtin_va_list type that should be used 976 /// with this target. 977 virtual BuiltinVaListKind getBuiltinVaListKind() const = 0; 978 979 /// Returns whether or not type \c __builtin_ms_va_list type is 980 /// available on this target. 981 bool hasBuiltinMSVaList() const { return HasBuiltinMSVaList; } 982 983 /// Returns true for RenderScript. 984 bool isRenderScriptTarget() const { return IsRenderScriptTarget; } 985 986 /// Returns whether or not the AArch64 SVE built-in types are 987 /// available on this target. 988 bool hasAArch64SVETypes() const { return HasAArch64SVETypes; } 989 990 /// Returns whether or not the RISC-V V built-in types are 991 /// available on this target. 992 bool hasRISCVVTypes() const { return HasRISCVVTypes; } 993 994 /// Returns whether or not the AMDGPU unsafe floating point atomics are 995 /// allowed. 996 bool allowAMDGPUUnsafeFPAtomics() const { return AllowAMDGPUUnsafeFPAtomics; } 997 998 /// For ARM targets returns a mask defining which coprocessors are configured 999 /// as Custom Datapath. 1000 uint32_t getARMCDECoprocMask() const { return ARMCDECoprocMask; } 1001 1002 /// Returns whether the passed in string is a valid clobber in an 1003 /// inline asm statement. 1004 /// 1005 /// This is used by Sema. 1006 bool isValidClobber(StringRef Name) const; 1007 1008 /// Returns whether the passed in string is a valid register name 1009 /// according to GCC. 1010 /// 1011 /// This is used by Sema for inline asm statements. 1012 virtual bool isValidGCCRegisterName(StringRef Name) const; 1013 1014 /// Returns the "normalized" GCC register name. 1015 /// 1016 /// ReturnCannonical true will return the register name without any additions 1017 /// such as "{}" or "%" in it's canonical form, for example: 1018 /// ReturnCanonical = true and Name = "rax", will return "ax". 1019 StringRef getNormalizedGCCRegisterName(StringRef Name, 1020 bool ReturnCanonical = false) const; 1021 1022 virtual bool isSPRegName(StringRef) const { return false; } 1023 1024 /// Extracts a register from the passed constraint (if it is a 1025 /// single-register constraint) and the asm label expression related to a 1026 /// variable in the input or output list of an inline asm statement. 1027 /// 1028 /// This function is used by Sema in order to diagnose conflicts between 1029 /// the clobber list and the input/output lists. 1030 virtual StringRef getConstraintRegister(StringRef Constraint, 1031 StringRef Expression) const { 1032 return ""; 1033 } 1034 1035 struct ConstraintInfo { 1036 enum { 1037 CI_None = 0x00, 1038 CI_AllowsMemory = 0x01, 1039 CI_AllowsRegister = 0x02, 1040 CI_ReadWrite = 0x04, // "+r" output constraint (read and write). 1041 CI_HasMatchingInput = 0x08, // This output operand has a matching input. 1042 CI_ImmediateConstant = 0x10, // This operand must be an immediate constant 1043 CI_EarlyClobber = 0x20, // "&" output constraint (early clobber). 1044 }; 1045 unsigned Flags; 1046 int TiedOperand; 1047 struct { 1048 int Min; 1049 int Max; 1050 bool isConstrained; 1051 } ImmRange; 1052 llvm::SmallSet<int, 4> ImmSet; 1053 1054 std::string ConstraintStr; // constraint: "=rm" 1055 std::string Name; // Operand name: [foo] with no []'s. 1056 public: 1057 ConstraintInfo(StringRef ConstraintStr, StringRef Name) 1058 : Flags(0), TiedOperand(-1), ConstraintStr(ConstraintStr.str()), 1059 Name(Name.str()) { 1060 ImmRange.Min = ImmRange.Max = 0; 1061 ImmRange.isConstrained = false; 1062 } 1063 1064 const std::string &getConstraintStr() const { return ConstraintStr; } 1065 const std::string &getName() const { return Name; } 1066 bool isReadWrite() const { return (Flags & CI_ReadWrite) != 0; } 1067 bool earlyClobber() { return (Flags & CI_EarlyClobber) != 0; } 1068 bool allowsRegister() const { return (Flags & CI_AllowsRegister) != 0; } 1069 bool allowsMemory() const { return (Flags & CI_AllowsMemory) != 0; } 1070 1071 /// Return true if this output operand has a matching 1072 /// (tied) input operand. 1073 bool hasMatchingInput() const { return (Flags & CI_HasMatchingInput) != 0; } 1074 1075 /// Return true if this input operand is a matching 1076 /// constraint that ties it to an output operand. 1077 /// 1078 /// If this returns true then getTiedOperand will indicate which output 1079 /// operand this is tied to. 1080 bool hasTiedOperand() const { return TiedOperand != -1; } 1081 unsigned getTiedOperand() const { 1082 assert(hasTiedOperand() && "Has no tied operand!"); 1083 return (unsigned)TiedOperand; 1084 } 1085 1086 bool requiresImmediateConstant() const { 1087 return (Flags & CI_ImmediateConstant) != 0; 1088 } 1089 bool isValidAsmImmediate(const llvm::APInt &Value) const { 1090 if (!ImmSet.empty()) 1091 return Value.isSignedIntN(32) && ImmSet.contains(Value.getZExtValue()); 1092 return !ImmRange.isConstrained || 1093 (Value.sge(ImmRange.Min) && Value.sle(ImmRange.Max)); 1094 } 1095 1096 void setIsReadWrite() { Flags |= CI_ReadWrite; } 1097 void setEarlyClobber() { Flags |= CI_EarlyClobber; } 1098 void setAllowsMemory() { Flags |= CI_AllowsMemory; } 1099 void setAllowsRegister() { Flags |= CI_AllowsRegister; } 1100 void setHasMatchingInput() { Flags |= CI_HasMatchingInput; } 1101 void setRequiresImmediate(int Min, int Max) { 1102 Flags |= CI_ImmediateConstant; 1103 ImmRange.Min = Min; 1104 ImmRange.Max = Max; 1105 ImmRange.isConstrained = true; 1106 } 1107 void setRequiresImmediate(llvm::ArrayRef<int> Exacts) { 1108 Flags |= CI_ImmediateConstant; 1109 for (int Exact : Exacts) 1110 ImmSet.insert(Exact); 1111 } 1112 void setRequiresImmediate(int Exact) { 1113 Flags |= CI_ImmediateConstant; 1114 ImmSet.insert(Exact); 1115 } 1116 void setRequiresImmediate() { 1117 Flags |= CI_ImmediateConstant; 1118 } 1119 1120 /// Indicate that this is an input operand that is tied to 1121 /// the specified output operand. 1122 /// 1123 /// Copy over the various constraint information from the output. 1124 void setTiedOperand(unsigned N, ConstraintInfo &Output) { 1125 Output.setHasMatchingInput(); 1126 Flags = Output.Flags; 1127 TiedOperand = N; 1128 // Don't copy Name or constraint string. 1129 } 1130 }; 1131 1132 /// Validate register name used for global register variables. 1133 /// 1134 /// This function returns true if the register passed in RegName can be used 1135 /// for global register variables on this target. In addition, it returns 1136 /// true in HasSizeMismatch if the size of the register doesn't match the 1137 /// variable size passed in RegSize. 1138 virtual bool validateGlobalRegisterVariable(StringRef RegName, 1139 unsigned RegSize, 1140 bool &HasSizeMismatch) const { 1141 HasSizeMismatch = false; 1142 return true; 1143 } 1144 1145 // validateOutputConstraint, validateInputConstraint - Checks that 1146 // a constraint is valid and provides information about it. 1147 // FIXME: These should return a real error instead of just true/false. 1148 bool validateOutputConstraint(ConstraintInfo &Info) const; 1149 bool validateInputConstraint(MutableArrayRef<ConstraintInfo> OutputConstraints, 1150 ConstraintInfo &info) const; 1151 1152 virtual bool validateOutputSize(const llvm::StringMap<bool> &FeatureMap, 1153 StringRef /*Constraint*/, 1154 unsigned /*Size*/) const { 1155 return true; 1156 } 1157 1158 virtual bool validateInputSize(const llvm::StringMap<bool> &FeatureMap, 1159 StringRef /*Constraint*/, 1160 unsigned /*Size*/) const { 1161 return true; 1162 } 1163 virtual bool 1164 validateConstraintModifier(StringRef /*Constraint*/, 1165 char /*Modifier*/, 1166 unsigned /*Size*/, 1167 std::string &/*SuggestedModifier*/) const { 1168 return true; 1169 } 1170 virtual bool 1171 validateAsmConstraint(const char *&Name, 1172 TargetInfo::ConstraintInfo &info) const = 0; 1173 1174 bool resolveSymbolicName(const char *&Name, 1175 ArrayRef<ConstraintInfo> OutputConstraints, 1176 unsigned &Index) const; 1177 1178 // Constraint parm will be left pointing at the last character of 1179 // the constraint. In practice, it won't be changed unless the 1180 // constraint is longer than one character. 1181 virtual std::string convertConstraint(const char *&Constraint) const { 1182 // 'p' defaults to 'r', but can be overridden by targets. 1183 if (*Constraint == 'p') 1184 return std::string("r"); 1185 return std::string(1, *Constraint); 1186 } 1187 1188 /// Replace some escaped characters with another string based on 1189 /// target-specific rules 1190 virtual std::optional<std::string> handleAsmEscapedChar(char C) const { 1191 return std::nullopt; 1192 } 1193 1194 /// Returns a string of target-specific clobbers, in LLVM format. 1195 virtual std::string_view getClobbers() const = 0; 1196 1197 /// Returns true if NaN encoding is IEEE 754-2008. 1198 /// Only MIPS allows a different encoding. 1199 virtual bool isNan2008() const { 1200 return true; 1201 } 1202 1203 /// Returns the target triple of the primary target. 1204 const llvm::Triple &getTriple() const { 1205 return Triple; 1206 } 1207 1208 /// Returns the target ID if supported. 1209 virtual std::optional<std::string> getTargetID() const { 1210 return std::nullopt; 1211 } 1212 1213 const char *getDataLayoutString() const { 1214 assert(!DataLayoutString.empty() && "Uninitialized DataLayout!"); 1215 return DataLayoutString.c_str(); 1216 } 1217 1218 struct GCCRegAlias { 1219 const char * const Aliases[5]; 1220 const char * const Register; 1221 }; 1222 1223 struct AddlRegName { 1224 const char * const Names[5]; 1225 const unsigned RegNum; 1226 }; 1227 1228 /// Does this target support "protected" visibility? 1229 /// 1230 /// Any target which dynamic libraries will naturally support 1231 /// something like "default" (meaning that the symbol is visible 1232 /// outside this shared object) and "hidden" (meaning that it isn't) 1233 /// visibilities, but "protected" is really an ELF-specific concept 1234 /// with weird semantics designed around the convenience of dynamic 1235 /// linker implementations. Which is not to suggest that there's 1236 /// consistent target-independent semantics for "default" visibility 1237 /// either; the entire thing is pretty badly mangled. 1238 virtual bool hasProtectedVisibility() const { return true; } 1239 1240 /// Does this target aim for semantic compatibility with 1241 /// Microsoft C++ code using dllimport/export attributes? 1242 virtual bool shouldDLLImportComdatSymbols() const { 1243 return getTriple().isWindowsMSVCEnvironment() || 1244 getTriple().isWindowsItaniumEnvironment() || getTriple().isPS(); 1245 } 1246 1247 // Does this target have PS4 specific dllimport/export handling? 1248 virtual bool hasPS4DLLImportExport() const { 1249 return getTriple().isPS() || 1250 // Windows Itanium support allows for testing the SCEI flavour of 1251 // dllimport/export handling on a Windows system. 1252 (getTriple().isWindowsItaniumEnvironment() && 1253 getTriple().getVendor() == llvm::Triple::SCEI); 1254 } 1255 1256 /// Set forced language options. 1257 /// 1258 /// Apply changes to the target information with respect to certain 1259 /// language options which change the target configuration and adjust 1260 /// the language based on the target options where applicable. 1261 virtual void adjust(DiagnosticsEngine &Diags, LangOptions &Opts); 1262 1263 /// Adjust target options based on codegen options. 1264 virtual void adjustTargetOptions(const CodeGenOptions &CGOpts, 1265 TargetOptions &TargetOpts) const {} 1266 1267 /// Initialize the map with the default set of target features for the 1268 /// CPU this should include all legal feature strings on the target. 1269 /// 1270 /// \return False on error (invalid features). 1271 virtual bool initFeatureMap(llvm::StringMap<bool> &Features, 1272 DiagnosticsEngine &Diags, StringRef CPU, 1273 const std::vector<std::string> &FeatureVec) const; 1274 1275 /// Get the ABI currently in use. 1276 virtual StringRef getABI() const { return StringRef(); } 1277 1278 /// Get the C++ ABI currently in use. 1279 TargetCXXABI getCXXABI() const { 1280 return TheCXXABI; 1281 } 1282 1283 /// Target the specified CPU. 1284 /// 1285 /// \return False on error (invalid CPU name). 1286 virtual bool setCPU(const std::string &Name) { 1287 return false; 1288 } 1289 1290 /// Fill a SmallVectorImpl with the valid values to setCPU. 1291 virtual void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const {} 1292 1293 /// Fill a SmallVectorImpl with the valid values for tuning CPU. 1294 virtual void fillValidTuneCPUList(SmallVectorImpl<StringRef> &Values) const { 1295 fillValidCPUList(Values); 1296 } 1297 1298 /// brief Determine whether this TargetInfo supports the given CPU name. 1299 virtual bool isValidCPUName(StringRef Name) const { 1300 return true; 1301 } 1302 1303 /// brief Determine whether this TargetInfo supports the given CPU name for 1304 // tuning. 1305 virtual bool isValidTuneCPUName(StringRef Name) const { 1306 return isValidCPUName(Name); 1307 } 1308 1309 virtual ParsedTargetAttr parseTargetAttr(StringRef Str) const; 1310 1311 /// brief Determine whether this TargetInfo supports tune in target attribute. 1312 virtual bool supportsTargetAttributeTune() const { 1313 return false; 1314 } 1315 1316 /// Use the specified ABI. 1317 /// 1318 /// \return False on error (invalid ABI name). 1319 virtual bool setABI(const std::string &Name) { 1320 return false; 1321 } 1322 1323 /// Use the specified unit for FP math. 1324 /// 1325 /// \return False on error (invalid unit name). 1326 virtual bool setFPMath(StringRef Name) { 1327 return false; 1328 } 1329 1330 /// Check if target has a given feature enabled 1331 virtual bool hasFeatureEnabled(const llvm::StringMap<bool> &Features, 1332 StringRef Name) const { 1333 return Features.lookup(Name); 1334 } 1335 1336 /// Enable or disable a specific target feature; 1337 /// the feature name must be valid. 1338 virtual void setFeatureEnabled(llvm::StringMap<bool> &Features, 1339 StringRef Name, 1340 bool Enabled) const { 1341 Features[Name] = Enabled; 1342 } 1343 1344 /// Determine whether this TargetInfo supports the given feature. 1345 virtual bool isValidFeatureName(StringRef Feature) const { 1346 return true; 1347 } 1348 1349 /// Returns true if feature has an impact on target code 1350 /// generation. 1351 virtual bool doesFeatureAffectCodeGen(StringRef Feature) const { 1352 return true; 1353 } 1354 1355 /// For given feature return dependent ones. 1356 virtual StringRef getFeatureDependencies(StringRef Feature) const { 1357 return StringRef(); 1358 } 1359 1360 struct BranchProtectionInfo { 1361 LangOptions::SignReturnAddressScopeKind SignReturnAddr = 1362 LangOptions::SignReturnAddressScopeKind::None; 1363 LangOptions::SignReturnAddressKeyKind SignKey = 1364 LangOptions::SignReturnAddressKeyKind::AKey; 1365 bool BranchTargetEnforcement = false; 1366 }; 1367 1368 /// Determine if the Architecture in this TargetInfo supports branch 1369 /// protection 1370 virtual bool isBranchProtectionSupportedArch(StringRef Arch) const { 1371 return false; 1372 } 1373 1374 /// Determine if this TargetInfo supports the given branch protection 1375 /// specification 1376 virtual bool validateBranchProtection(StringRef Spec, StringRef Arch, 1377 BranchProtectionInfo &BPI, 1378 StringRef &Err) const { 1379 Err = ""; 1380 return false; 1381 } 1382 1383 /// Perform initialization based on the user configured 1384 /// set of features (e.g., +sse4). 1385 /// 1386 /// The list is guaranteed to have at most one entry per feature. 1387 /// 1388 /// The target may modify the features list, to change which options are 1389 /// passed onwards to the backend. 1390 /// FIXME: This part should be fixed so that we can change handleTargetFeatures 1391 /// to merely a TargetInfo initialization routine. 1392 /// 1393 /// \return False on error. 1394 virtual bool handleTargetFeatures(std::vector<std::string> &Features, 1395 DiagnosticsEngine &Diags) { 1396 return true; 1397 } 1398 1399 /// Determine whether the given target has the given feature. 1400 virtual bool hasFeature(StringRef Feature) const { 1401 return false; 1402 } 1403 1404 /// Determine whether the given target feature is read only. 1405 bool isReadOnlyFeature(StringRef Feature) const { 1406 return ReadOnlyFeatures.count(Feature); 1407 } 1408 1409 /// Identify whether this target supports multiversioning of functions, 1410 /// which requires support for cpu_supports and cpu_is functionality. 1411 bool supportsMultiVersioning() const { 1412 return getTriple().isX86() || getTriple().isAArch64(); 1413 } 1414 1415 /// Identify whether this target supports IFuncs. 1416 bool supportsIFunc() const { 1417 return getTriple().isOSBinFormatELF() && 1418 ((getTriple().isOSLinux() && !getTriple().isMusl()) || 1419 getTriple().isOSFreeBSD()); 1420 } 1421 1422 // Validate the contents of the __builtin_cpu_supports(const char*) 1423 // argument. 1424 virtual bool validateCpuSupports(StringRef Name) const { return false; } 1425 1426 // Return the target-specific priority for features/cpus/vendors so 1427 // that they can be properly sorted for checking. 1428 virtual unsigned multiVersionSortPriority(StringRef Name) const { 1429 return 0; 1430 } 1431 1432 // Return the target-specific cost for feature 1433 // that taken into account in priority sorting. 1434 virtual unsigned multiVersionFeatureCost() const { return 0; } 1435 1436 // Validate the contents of the __builtin_cpu_is(const char*) 1437 // argument. 1438 virtual bool validateCpuIs(StringRef Name) const { return false; } 1439 1440 // Validate a cpu_dispatch/cpu_specific CPU option, which is a different list 1441 // from cpu_is, since it checks via features rather than CPUs directly. 1442 virtual bool validateCPUSpecificCPUDispatch(StringRef Name) const { 1443 return false; 1444 } 1445 1446 // Get the character to be added for mangling purposes for cpu_specific. 1447 virtual char CPUSpecificManglingCharacter(StringRef Name) const { 1448 llvm_unreachable( 1449 "cpu_specific Multiversioning not implemented on this target"); 1450 } 1451 1452 // Get the value for the 'tune-cpu' flag for a cpu_specific variant with the 1453 // programmer-specified 'Name'. 1454 virtual StringRef getCPUSpecificTuneName(StringRef Name) const { 1455 llvm_unreachable( 1456 "cpu_specific Multiversioning not implemented on this target"); 1457 } 1458 1459 // Get a list of the features that make up the CPU option for 1460 // cpu_specific/cpu_dispatch so that it can be passed to llvm as optimization 1461 // options. 1462 virtual void getCPUSpecificCPUDispatchFeatures( 1463 StringRef Name, llvm::SmallVectorImpl<StringRef> &Features) const { 1464 llvm_unreachable( 1465 "cpu_specific Multiversioning not implemented on this target"); 1466 } 1467 1468 // Get the cache line size of a given cpu. This method switches over 1469 // the given cpu and returns "std::nullopt" if the CPU is not found. 1470 virtual std::optional<unsigned> getCPUCacheLineSize() const { 1471 return std::nullopt; 1472 } 1473 1474 // Returns maximal number of args passed in registers. 1475 unsigned getRegParmMax() const { 1476 assert(RegParmMax < 7 && "RegParmMax value is larger than AST can handle"); 1477 return RegParmMax; 1478 } 1479 1480 /// Whether the target supports thread-local storage. 1481 bool isTLSSupported() const { 1482 return TLSSupported; 1483 } 1484 1485 /// Return the maximum alignment (in bits) of a TLS variable 1486 /// 1487 /// Gets the maximum alignment (in bits) of a TLS variable on this target. 1488 /// Returns zero if there is no such constraint. 1489 unsigned getMaxTLSAlign() const { return MaxTLSAlign; } 1490 1491 /// Whether target supports variable-length arrays. 1492 bool isVLASupported() const { return VLASupported; } 1493 1494 /// Whether the target supports SEH __try. 1495 bool isSEHTrySupported() const { 1496 return getTriple().isOSWindows() && 1497 (getTriple().isX86() || 1498 getTriple().getArch() == llvm::Triple::aarch64); 1499 } 1500 1501 /// Return true if {|} are normal characters in the asm string. 1502 /// 1503 /// If this returns false (the default), then {abc|xyz} is syntax 1504 /// that says that when compiling for asm variant #0, "abc" should be 1505 /// generated, but when compiling for asm variant #1, "xyz" should be 1506 /// generated. 1507 bool hasNoAsmVariants() const { 1508 return NoAsmVariants; 1509 } 1510 1511 /// Return the register number that __builtin_eh_return_regno would 1512 /// return with the specified argument. 1513 /// This corresponds with TargetLowering's getExceptionPointerRegister 1514 /// and getExceptionSelectorRegister in the backend. 1515 virtual int getEHDataRegisterNumber(unsigned RegNo) const { 1516 return -1; 1517 } 1518 1519 /// Return the section to use for C++ static initialization functions. 1520 virtual const char *getStaticInitSectionSpecifier() const { 1521 return nullptr; 1522 } 1523 1524 const LangASMap &getAddressSpaceMap() const { return *AddrSpaceMap; } 1525 unsigned getTargetAddressSpace(LangAS AS) const { 1526 if (isTargetAddressSpace(AS)) 1527 return toTargetAddressSpace(AS); 1528 return getAddressSpaceMap()[(unsigned)AS]; 1529 } 1530 1531 /// Map from the address space field in builtin description strings to the 1532 /// language address space. 1533 virtual LangAS getOpenCLBuiltinAddressSpace(unsigned AS) const { 1534 return getLangASFromTargetAS(AS); 1535 } 1536 1537 /// Map from the address space field in builtin description strings to the 1538 /// language address space. 1539 virtual LangAS getCUDABuiltinAddressSpace(unsigned AS) const { 1540 return getLangASFromTargetAS(AS); 1541 } 1542 1543 /// Return an AST address space which can be used opportunistically 1544 /// for constant global memory. It must be possible to convert pointers into 1545 /// this address space to LangAS::Default. If no such address space exists, 1546 /// this may return std::nullopt, and such optimizations will be disabled. 1547 virtual std::optional<LangAS> getConstantAddressSpace() const { 1548 return LangAS::Default; 1549 } 1550 1551 // access target-specific GPU grid values that must be consistent between 1552 // host RTL (plugin), deviceRTL and clang. 1553 virtual const llvm::omp::GV &getGridValue() const { 1554 llvm_unreachable("getGridValue not implemented on this target"); 1555 } 1556 1557 /// Retrieve the name of the platform as it is used in the 1558 /// availability attribute. 1559 StringRef getPlatformName() const { return PlatformName; } 1560 1561 /// Retrieve the minimum desired version of the platform, to 1562 /// which the program should be compiled. 1563 VersionTuple getPlatformMinVersion() const { return PlatformMinVersion; } 1564 1565 bool isBigEndian() const { return BigEndian; } 1566 bool isLittleEndian() const { return !BigEndian; } 1567 1568 /// Whether the option -fextend-arguments={32,64} is supported on the target. 1569 virtual bool supportsExtendIntArgs() const { return false; } 1570 1571 /// Controls if __arithmetic_fence is supported in the targeted backend. 1572 virtual bool checkArithmeticFenceSupported() const { return false; } 1573 1574 /// Gets the default calling convention for the given target and 1575 /// declaration context. 1576 virtual CallingConv getDefaultCallingConv() const { 1577 // Not all targets will specify an explicit calling convention that we can 1578 // express. This will always do the right thing, even though it's not 1579 // an explicit calling convention. 1580 return CC_C; 1581 } 1582 1583 enum CallingConvCheckResult { 1584 CCCR_OK, 1585 CCCR_Warning, 1586 CCCR_Ignore, 1587 CCCR_Error, 1588 }; 1589 1590 /// Determines whether a given calling convention is valid for the 1591 /// target. A calling convention can either be accepted, produce a warning 1592 /// and be substituted with the default calling convention, or (someday) 1593 /// produce an error (such as using thiscall on a non-instance function). 1594 virtual CallingConvCheckResult checkCallingConvention(CallingConv CC) const { 1595 switch (CC) { 1596 default: 1597 return CCCR_Warning; 1598 case CC_C: 1599 return CCCR_OK; 1600 } 1601 } 1602 1603 enum CallingConvKind { 1604 CCK_Default, 1605 CCK_ClangABI4OrPS4, 1606 CCK_MicrosoftWin64 1607 }; 1608 1609 virtual CallingConvKind getCallingConvKind(bool ClangABICompat4) const; 1610 1611 /// Controls whether explicitly defaulted (`= default`) special member 1612 /// functions disqualify something from being POD-for-the-purposes-of-layout. 1613 /// Historically, Clang didn't consider these acceptable for POD, but GCC 1614 /// does. So in newer Clang ABIs they are acceptable for POD to be compatible 1615 /// with GCC/Itanium ABI, and remains disqualifying for targets that need 1616 /// Clang backwards compatibility rather than GCC/Itanium ABI compatibility. 1617 virtual bool areDefaultedSMFStillPOD(const LangOptions&) const; 1618 1619 /// Controls if __builtin_longjmp / __builtin_setjmp can be lowered to 1620 /// llvm.eh.sjlj.longjmp / llvm.eh.sjlj.setjmp. 1621 virtual bool hasSjLjLowering() const { 1622 return false; 1623 } 1624 1625 /// Check if the target supports CFProtection branch. 1626 virtual bool 1627 checkCFProtectionBranchSupported(DiagnosticsEngine &Diags) const; 1628 1629 /// Check if the target supports CFProtection return. 1630 virtual bool 1631 checkCFProtectionReturnSupported(DiagnosticsEngine &Diags) const; 1632 1633 /// Whether target allows to overalign ABI-specified preferred alignment 1634 virtual bool allowsLargerPreferedTypeAlignment() const { return true; } 1635 1636 /// Whether target defaults to the `power` alignment rules of AIX. 1637 virtual bool defaultsToAIXPowerAlignment() const { return false; } 1638 1639 /// Set supported OpenCL extensions and optional core features. 1640 virtual void setSupportedOpenCLOpts() {} 1641 1642 virtual void supportAllOpenCLOpts(bool V = true) { 1643 #define OPENCLEXTNAME(Ext) \ 1644 setFeatureEnabled(getTargetOpts().OpenCLFeaturesMap, #Ext, V); 1645 #include "clang/Basic/OpenCLExtensions.def" 1646 } 1647 1648 /// Set supported OpenCL extensions as written on command line 1649 virtual void setCommandLineOpenCLOpts() { 1650 for (const auto &Ext : getTargetOpts().OpenCLExtensionsAsWritten) { 1651 bool IsPrefixed = (Ext[0] == '+' || Ext[0] == '-'); 1652 std::string Name = IsPrefixed ? Ext.substr(1) : Ext; 1653 bool V = IsPrefixed ? Ext[0] == '+' : true; 1654 1655 if (Name == "all") { 1656 supportAllOpenCLOpts(V); 1657 continue; 1658 } 1659 1660 getTargetOpts().OpenCLFeaturesMap[Name] = V; 1661 } 1662 } 1663 1664 /// Get supported OpenCL extensions and optional core features. 1665 llvm::StringMap<bool> &getSupportedOpenCLOpts() { 1666 return getTargetOpts().OpenCLFeaturesMap; 1667 } 1668 1669 /// Get const supported OpenCL extensions and optional core features. 1670 const llvm::StringMap<bool> &getSupportedOpenCLOpts() const { 1671 return getTargetOpts().OpenCLFeaturesMap; 1672 } 1673 1674 /// Get address space for OpenCL type. 1675 virtual LangAS getOpenCLTypeAddrSpace(OpenCLTypeKind TK) const; 1676 1677 /// \returns Target specific vtbl ptr address space. 1678 virtual unsigned getVtblPtrAddressSpace() const { 1679 return 0; 1680 } 1681 1682 /// \returns If a target requires an address within a target specific address 1683 /// space \p AddressSpace to be converted in order to be used, then return the 1684 /// corresponding target specific DWARF address space. 1685 /// 1686 /// \returns Otherwise return std::nullopt and no conversion will be emitted 1687 /// in the DWARF. 1688 virtual std::optional<unsigned> getDWARFAddressSpace(unsigned AddressSpace) 1689 const { 1690 return std::nullopt; 1691 } 1692 1693 /// \returns The version of the SDK which was used during the compilation if 1694 /// one was specified, or an empty version otherwise. 1695 const llvm::VersionTuple &getSDKVersion() const { 1696 return getTargetOpts().SDKVersion; 1697 } 1698 1699 /// Check the target is valid after it is fully initialized. 1700 virtual bool validateTarget(DiagnosticsEngine &Diags) const { 1701 return true; 1702 } 1703 1704 /// Check that OpenCL target has valid options setting based on OpenCL 1705 /// version. 1706 virtual bool validateOpenCLTarget(const LangOptions &Opts, 1707 DiagnosticsEngine &Diags) const; 1708 1709 virtual void setAuxTarget(const TargetInfo *Aux) {} 1710 1711 /// Whether target allows debuginfo types for decl only variables/functions. 1712 virtual bool allowDebugInfoForExternalRef() const { return false; } 1713 1714 /// Returns the darwin target variant triple, the variant of the deployment 1715 /// target for which the code is being compiled. 1716 const llvm::Triple *getDarwinTargetVariantTriple() const { 1717 return DarwinTargetVariantTriple ? &*DarwinTargetVariantTriple : nullptr; 1718 } 1719 1720 /// Returns the version of the darwin target variant SDK which was used during 1721 /// the compilation if one was specified, or an empty version otherwise. 1722 const std::optional<VersionTuple> getDarwinTargetVariantSDKVersion() const { 1723 return !getTargetOpts().DarwinTargetVariantSDKVersion.empty() 1724 ? getTargetOpts().DarwinTargetVariantSDKVersion 1725 : std::optional<VersionTuple>(); 1726 } 1727 1728 /// Whether to support HIP image/texture API's. 1729 virtual bool hasHIPImageSupport() const { return true; } 1730 1731 protected: 1732 /// Copy type and layout related info. 1733 void copyAuxTarget(const TargetInfo *Aux); 1734 virtual uint64_t getPointerWidthV(LangAS AddrSpace) const { 1735 return PointerWidth; 1736 } 1737 virtual uint64_t getPointerAlignV(LangAS AddrSpace) const { 1738 return PointerAlign; 1739 } 1740 virtual enum IntType getPtrDiffTypeV(LangAS AddrSpace) const { 1741 return PtrDiffType; 1742 } 1743 virtual ArrayRef<const char *> getGCCRegNames() const = 0; 1744 virtual ArrayRef<GCCRegAlias> getGCCRegAliases() const = 0; 1745 virtual ArrayRef<AddlRegName> getGCCAddlRegNames() const { 1746 return std::nullopt; 1747 } 1748 1749 private: 1750 // Assert the values for the fractional and integral bits for each fixed point 1751 // type follow the restrictions given in clause 6.2.6.3 of N1169. 1752 void CheckFixedPointBits() const; 1753 }; 1754 1755 } // end namespace clang 1756 1757 #endif 1758