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