1 //===- llvm/Attributes.h - Container for Attributes -------------*- 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 /// This file contains the simple types necessary to represent the 11 /// attributes associated with functions and their calls. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_IR_ATTRIBUTES_H 16 #define LLVM_IR_ATTRIBUTES_H 17 18 #include "llvm-c/Types.h" 19 #include "llvm/ADT/ArrayRef.h" 20 #include "llvm/ADT/BitmaskEnum.h" 21 #include "llvm/ADT/StringRef.h" 22 #include "llvm/Config/llvm-config.h" 23 #include "llvm/Support/Alignment.h" 24 #include "llvm/Support/CodeGen.h" 25 #include "llvm/Support/ModRef.h" 26 #include "llvm/Support/PointerLikeTypeTraits.h" 27 #include <cassert> 28 #include <cstdint> 29 #include <optional> 30 #include <string> 31 #include <utility> 32 33 namespace llvm { 34 35 class AttrBuilder; 36 class AttributeMask; 37 class AttributeImpl; 38 class AttributeListImpl; 39 class AttributeSetNode; 40 class ConstantRange; 41 class ConstantRangeList; 42 class FoldingSetNodeID; 43 class Function; 44 class LLVMContext; 45 class Type; 46 class raw_ostream; 47 enum FPClassTest : unsigned; 48 49 enum class AllocFnKind : uint64_t { 50 Unknown = 0, 51 Alloc = 1 << 0, // Allocator function returns a new allocation 52 Realloc = 1 << 1, // Allocator function resizes the `allocptr` argument 53 Free = 1 << 2, // Allocator function frees the `allocptr` argument 54 Uninitialized = 1 << 3, // Allocator function returns uninitialized memory 55 Zeroed = 1 << 4, // Allocator function returns zeroed memory 56 Aligned = 1 << 5, // Allocator function aligns allocations per the 57 // `allocalign` argument 58 LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ Aligned) 59 }; 60 61 //===----------------------------------------------------------------------===// 62 /// \class 63 /// Functions, function parameters, and return types can have attributes 64 /// to indicate how they should be treated by optimizations and code 65 /// generation. This class represents one of those attributes. It's light-weight 66 /// and should be passed around by-value. 67 class Attribute { 68 public: 69 /// This enumeration lists the attributes that can be associated with 70 /// parameters, function results, or the function itself. 71 /// 72 /// Note: The `uwtable' attribute is about the ABI or the user mandating an 73 /// entry in the unwind table. The `nounwind' attribute is about an exception 74 /// passing by the function. 75 /// 76 /// In a theoretical system that uses tables for profiling and SjLj for 77 /// exceptions, they would be fully independent. In a normal system that uses 78 /// tables for both, the semantics are: 79 /// 80 /// nil = Needs an entry because an exception might pass by. 81 /// nounwind = No need for an entry 82 /// uwtable = Needs an entry because the ABI says so and because 83 /// an exception might pass by. 84 /// uwtable + nounwind = Needs an entry because the ABI says so. 85 86 enum AttrKind { 87 // IR-Level Attributes 88 None, ///< No attributes have been set 89 #define GET_ATTR_ENUM 90 #include "llvm/IR/Attributes.inc" 91 EndAttrKinds, ///< Sentinel value useful for loops 92 EmptyKey, ///< Use as Empty key for DenseMap of AttrKind 93 TombstoneKey, ///< Use as Tombstone key for DenseMap of AttrKind 94 }; 95 96 static const unsigned NumIntAttrKinds = LastIntAttr - FirstIntAttr + 1; 97 static const unsigned NumTypeAttrKinds = LastTypeAttr - FirstTypeAttr + 1; 98 99 static bool isEnumAttrKind(AttrKind Kind) { 100 return Kind >= FirstEnumAttr && Kind <= LastEnumAttr; 101 } 102 static bool isIntAttrKind(AttrKind Kind) { 103 return Kind >= FirstIntAttr && Kind <= LastIntAttr; 104 } 105 static bool isTypeAttrKind(AttrKind Kind) { 106 return Kind >= FirstTypeAttr && Kind <= LastTypeAttr; 107 } 108 static bool isConstantRangeAttrKind(AttrKind Kind) { 109 return Kind >= FirstConstantRangeAttr && Kind <= LastConstantRangeAttr; 110 } 111 static bool isConstantRangeListAttrKind(AttrKind Kind) { 112 return Kind >= FirstConstantRangeListAttr && 113 Kind <= LastConstantRangeListAttr; 114 } 115 116 static bool canUseAsFnAttr(AttrKind Kind); 117 static bool canUseAsParamAttr(AttrKind Kind); 118 static bool canUseAsRetAttr(AttrKind Kind); 119 120 static bool intersectMustPreserve(AttrKind Kind); 121 static bool intersectWithAnd(AttrKind Kind); 122 static bool intersectWithMin(AttrKind Kind); 123 static bool intersectWithCustom(AttrKind Kind); 124 125 private: 126 AttributeImpl *pImpl = nullptr; 127 128 Attribute(AttributeImpl *A) : pImpl(A) {} 129 130 public: 131 Attribute() = default; 132 133 //===--------------------------------------------------------------------===// 134 // Attribute Construction 135 //===--------------------------------------------------------------------===// 136 137 /// Return a uniquified Attribute object. 138 static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0); 139 static Attribute get(LLVMContext &Context, StringRef Kind, 140 StringRef Val = StringRef()); 141 static Attribute get(LLVMContext &Context, AttrKind Kind, Type *Ty); 142 static Attribute get(LLVMContext &Context, AttrKind Kind, 143 const ConstantRange &CR); 144 static Attribute get(LLVMContext &Context, AttrKind Kind, 145 ArrayRef<ConstantRange> Val); 146 147 /// Return a uniquified Attribute object that has the specific 148 /// alignment set. 149 static Attribute getWithAlignment(LLVMContext &Context, Align Alignment); 150 static Attribute getWithStackAlignment(LLVMContext &Context, Align Alignment); 151 static Attribute getWithDereferenceableBytes(LLVMContext &Context, 152 uint64_t Bytes); 153 static Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context, 154 uint64_t Bytes); 155 static Attribute getWithAllocSizeArgs( 156 LLVMContext &Context, unsigned ElemSizeArg, 157 const std::optional<unsigned> &NumElemsArg); 158 static Attribute getWithVScaleRangeArgs(LLVMContext &Context, 159 unsigned MinValue, unsigned MaxValue); 160 static Attribute getWithByValType(LLVMContext &Context, Type *Ty); 161 static Attribute getWithStructRetType(LLVMContext &Context, Type *Ty); 162 static Attribute getWithByRefType(LLVMContext &Context, Type *Ty); 163 static Attribute getWithPreallocatedType(LLVMContext &Context, Type *Ty); 164 static Attribute getWithInAllocaType(LLVMContext &Context, Type *Ty); 165 static Attribute getWithUWTableKind(LLVMContext &Context, UWTableKind Kind); 166 static Attribute getWithMemoryEffects(LLVMContext &Context, MemoryEffects ME); 167 static Attribute getWithNoFPClass(LLVMContext &Context, FPClassTest Mask); 168 static Attribute getWithCaptureInfo(LLVMContext &Context, CaptureInfo CI); 169 170 /// For a typed attribute, return the equivalent attribute with the type 171 /// changed to \p ReplacementTy. 172 Attribute getWithNewType(LLVMContext &Context, Type *ReplacementTy) { 173 assert(isTypeAttribute() && "this requires a typed attribute"); 174 return get(Context, getKindAsEnum(), ReplacementTy); 175 } 176 177 static Attribute::AttrKind getAttrKindFromName(StringRef AttrName); 178 179 static StringRef getNameFromAttrKind(Attribute::AttrKind AttrKind); 180 181 /// Return true if the provided string matches the IR name of an attribute. 182 /// example: "noalias" return true but not "NoAlias" 183 static bool isExistingAttribute(StringRef Name); 184 185 //===--------------------------------------------------------------------===// 186 // Attribute Accessors 187 //===--------------------------------------------------------------------===// 188 189 /// Return true if the attribute is an Attribute::AttrKind type. 190 bool isEnumAttribute() const; 191 192 /// Return true if the attribute is an integer attribute. 193 bool isIntAttribute() const; 194 195 /// Return true if the attribute is a string (target-dependent) 196 /// attribute. 197 bool isStringAttribute() const; 198 199 /// Return true if the attribute is a type attribute. 200 bool isTypeAttribute() const; 201 202 /// Return true if the attribute is a ConstantRange attribute. 203 bool isConstantRangeAttribute() const; 204 205 /// Return true if the attribute is a ConstantRangeList attribute. 206 bool isConstantRangeListAttribute() const; 207 208 /// Return true if the attribute is any kind of attribute. 209 bool isValid() const { return pImpl; } 210 211 /// Return true if the attribute is present. 212 bool hasAttribute(AttrKind Val) const; 213 214 /// Return true if the target-dependent attribute is present. 215 bool hasAttribute(StringRef Val) const; 216 217 /// Returns true if the attribute's kind can be represented as an enum (Enum, 218 /// Integer, Type, ConstantRange, or ConstantRangeList attribute). 219 bool hasKindAsEnum() const { return !isStringAttribute(); } 220 221 /// Return the attribute's kind as an enum (Attribute::AttrKind). This 222 /// requires the attribute be representable as an enum (see: `hasKindAsEnum`). 223 Attribute::AttrKind getKindAsEnum() const; 224 225 /// Return the attribute's value as an integer. This requires that the 226 /// attribute be an integer attribute. 227 uint64_t getValueAsInt() const; 228 229 /// Return the attribute's value as a boolean. This requires that the 230 /// attribute be a string attribute. 231 bool getValueAsBool() const; 232 233 /// Return the attribute's kind as a string. This requires the 234 /// attribute to be a string attribute. 235 StringRef getKindAsString() const; 236 237 /// Return the attribute's value as a string. This requires the 238 /// attribute to be a string attribute. 239 StringRef getValueAsString() const; 240 241 /// Return the attribute's value as a Type. This requires the attribute to be 242 /// a type attribute. 243 Type *getValueAsType() const; 244 245 /// Return the attribute's value as a ConstantRange. This requires the 246 /// attribute to be a ConstantRange attribute. 247 const ConstantRange &getValueAsConstantRange() const; 248 249 /// Return the attribute's value as a ConstantRange array. This requires the 250 /// attribute to be a ConstantRangeList attribute. 251 ArrayRef<ConstantRange> getValueAsConstantRangeList() const; 252 253 /// Returns the alignment field of an attribute as a byte alignment 254 /// value. 255 MaybeAlign getAlignment() const; 256 257 /// Returns the stack alignment field of an attribute as a byte 258 /// alignment value. 259 MaybeAlign getStackAlignment() const; 260 261 /// Returns the number of dereferenceable bytes from the 262 /// dereferenceable attribute. 263 uint64_t getDereferenceableBytes() const; 264 265 /// Returns the number of dereferenceable_or_null bytes from the 266 /// dereferenceable_or_null attribute. 267 uint64_t getDereferenceableOrNullBytes() const; 268 269 /// Returns the argument numbers for the allocsize attribute. 270 std::pair<unsigned, std::optional<unsigned>> getAllocSizeArgs() const; 271 272 /// Returns the minimum value for the vscale_range attribute. 273 unsigned getVScaleRangeMin() const; 274 275 /// Returns the maximum value for the vscale_range attribute or std::nullopt 276 /// when unknown. 277 std::optional<unsigned> getVScaleRangeMax() const; 278 279 // Returns the unwind table kind. 280 UWTableKind getUWTableKind() const; 281 282 // Returns the allocator function kind. 283 AllocFnKind getAllocKind() const; 284 285 /// Returns memory effects. 286 MemoryEffects getMemoryEffects() const; 287 288 /// Returns information from captures attribute. 289 CaptureInfo getCaptureInfo() const; 290 291 /// Return the FPClassTest for nofpclass 292 FPClassTest getNoFPClass() const; 293 294 /// Returns the value of the range attribute. 295 const ConstantRange &getRange() const; 296 297 /// Returns the value of the initializes attribute. 298 ArrayRef<ConstantRange> getInitializes() const; 299 300 /// The Attribute is converted to a string of equivalent mnemonic. This 301 /// is, presumably, for writing out the mnemonics for the assembly writer. 302 std::string getAsString(bool InAttrGrp = false) const; 303 304 /// Return true if this attribute belongs to the LLVMContext. 305 bool hasParentContext(LLVMContext &C) const; 306 307 /// Equality and non-equality operators. 308 bool operator==(Attribute A) const { return pImpl == A.pImpl; } 309 bool operator!=(Attribute A) const { return pImpl != A.pImpl; } 310 311 /// Used to sort attribute by kind. 312 int cmpKind(Attribute A) const; 313 314 /// Less-than operator. Useful for sorting the attributes list. 315 bool operator<(Attribute A) const; 316 317 void Profile(FoldingSetNodeID &ID) const; 318 319 /// Return a raw pointer that uniquely identifies this attribute. 320 void *getRawPointer() const { 321 return pImpl; 322 } 323 324 /// Get an attribute from a raw pointer created by getRawPointer. 325 static Attribute fromRawPointer(void *RawPtr) { 326 return Attribute(reinterpret_cast<AttributeImpl*>(RawPtr)); 327 } 328 }; 329 330 // Specialized opaque value conversions. 331 inline LLVMAttributeRef wrap(Attribute Attr) { 332 return reinterpret_cast<LLVMAttributeRef>(Attr.getRawPointer()); 333 } 334 335 // Specialized opaque value conversions. 336 inline Attribute unwrap(LLVMAttributeRef Attr) { 337 return Attribute::fromRawPointer(Attr); 338 } 339 340 //===----------------------------------------------------------------------===// 341 /// \class 342 /// This class holds the attributes for a particular argument, parameter, 343 /// function, or return value. It is an immutable value type that is cheap to 344 /// copy. Adding and removing enum attributes is intended to be fast, but adding 345 /// and removing string or integer attributes involves a FoldingSet lookup. 346 class AttributeSet { 347 friend AttributeListImpl; 348 template <typename Ty, typename Enable> friend struct DenseMapInfo; 349 350 // TODO: Extract AvailableAttrs from AttributeSetNode and store them here. 351 // This will allow an efficient implementation of addAttribute and 352 // removeAttribute for enum attrs. 353 354 /// Private implementation pointer. 355 AttributeSetNode *SetNode = nullptr; 356 357 private: 358 explicit AttributeSet(AttributeSetNode *ASN) : SetNode(ASN) {} 359 360 public: 361 /// AttributeSet is a trivially copyable value type. 362 AttributeSet() = default; 363 AttributeSet(const AttributeSet &) = default; 364 ~AttributeSet() = default; 365 366 static AttributeSet get(LLVMContext &C, const AttrBuilder &B); 367 static AttributeSet get(LLVMContext &C, ArrayRef<Attribute> Attrs); 368 369 bool operator==(const AttributeSet &O) const { return SetNode == O.SetNode; } 370 bool operator!=(const AttributeSet &O) const { return !(*this == O); } 371 372 /// Add an argument attribute. Returns a new set because attribute sets are 373 /// immutable. 374 [[nodiscard]] AttributeSet addAttribute(LLVMContext &C, 375 Attribute::AttrKind Kind) const; 376 377 /// Add a target-dependent attribute. Returns a new set because attribute sets 378 /// are immutable. 379 [[nodiscard]] AttributeSet addAttribute(LLVMContext &C, StringRef Kind, 380 StringRef Value = StringRef()) const; 381 382 /// Add attributes to the attribute set. Returns a new set because attribute 383 /// sets are immutable. 384 [[nodiscard]] AttributeSet addAttributes(LLVMContext &C, 385 AttributeSet AS) const; 386 387 /// Remove the specified attribute from this set. Returns a new set because 388 /// attribute sets are immutable. 389 [[nodiscard]] AttributeSet removeAttribute(LLVMContext &C, 390 Attribute::AttrKind Kind) const; 391 392 /// Remove the specified attribute from this set. Returns a new set because 393 /// attribute sets are immutable. 394 [[nodiscard]] AttributeSet removeAttribute(LLVMContext &C, 395 StringRef Kind) const; 396 397 /// Remove the specified attributes from this set. Returns a new set because 398 /// attribute sets are immutable. 399 [[nodiscard]] AttributeSet 400 removeAttributes(LLVMContext &C, const AttributeMask &AttrsToRemove) const; 401 402 /// Try to intersect this AttributeSet with Other. Returns std::nullopt if 403 /// the two lists are inherently incompatible (imply different behavior, not 404 /// just analysis). 405 [[nodiscard]] std::optional<AttributeSet> 406 intersectWith(LLVMContext &C, AttributeSet Other) const; 407 408 /// Return the number of attributes in this set. 409 unsigned getNumAttributes() const; 410 411 /// Return true if attributes exists in this set. 412 bool hasAttributes() const { return SetNode != nullptr; } 413 414 /// Return true if the attribute exists in this set. 415 bool hasAttribute(Attribute::AttrKind Kind) const; 416 417 /// Return true if the attribute exists in this set. 418 bool hasAttribute(StringRef Kind) const; 419 420 /// Return the attribute object. 421 Attribute getAttribute(Attribute::AttrKind Kind) const; 422 423 /// Return the target-dependent attribute object. 424 Attribute getAttribute(StringRef Kind) const; 425 426 MaybeAlign getAlignment() const; 427 MaybeAlign getStackAlignment() const; 428 uint64_t getDereferenceableBytes() const; 429 uint64_t getDereferenceableOrNullBytes() const; 430 Type *getByValType() const; 431 Type *getStructRetType() const; 432 Type *getByRefType() const; 433 Type *getPreallocatedType() const; 434 Type *getInAllocaType() const; 435 Type *getElementType() const; 436 std::optional<std::pair<unsigned, std::optional<unsigned>>> getAllocSizeArgs() 437 const; 438 unsigned getVScaleRangeMin() const; 439 std::optional<unsigned> getVScaleRangeMax() const; 440 UWTableKind getUWTableKind() const; 441 AllocFnKind getAllocKind() const; 442 MemoryEffects getMemoryEffects() const; 443 CaptureInfo getCaptureInfo() const; 444 FPClassTest getNoFPClass() const; 445 std::string getAsString(bool InAttrGrp = false) const; 446 447 /// Return true if this attribute set belongs to the LLVMContext. 448 bool hasParentContext(LLVMContext &C) const; 449 450 using iterator = const Attribute *; 451 452 iterator begin() const; 453 iterator end() const; 454 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 455 void dump() const; 456 #endif 457 }; 458 459 //===----------------------------------------------------------------------===// 460 /// \class 461 /// Provide DenseMapInfo for AttributeSet. 462 template <> struct DenseMapInfo<AttributeSet, void> { 463 static AttributeSet getEmptyKey() { 464 auto Val = static_cast<uintptr_t>(-1); 465 Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable; 466 return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val)); 467 } 468 469 static AttributeSet getTombstoneKey() { 470 auto Val = static_cast<uintptr_t>(-2); 471 Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable; 472 return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val)); 473 } 474 475 static unsigned getHashValue(AttributeSet AS) { 476 return (unsigned((uintptr_t)AS.SetNode) >> 4) ^ 477 (unsigned((uintptr_t)AS.SetNode) >> 9); 478 } 479 480 static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; } 481 }; 482 483 //===----------------------------------------------------------------------===// 484 /// \class 485 /// This class holds the attributes for a function, its return value, and 486 /// its parameters. You access the attributes for each of them via an index into 487 /// the AttributeList object. The function attributes are at index 488 /// `AttributeList::FunctionIndex', the return value is at index 489 /// `AttributeList::ReturnIndex', and the attributes for the parameters start at 490 /// index `AttributeList::FirstArgIndex'. 491 class AttributeList { 492 public: 493 enum AttrIndex : unsigned { 494 ReturnIndex = 0U, 495 FunctionIndex = ~0U, 496 FirstArgIndex = 1, 497 }; 498 499 private: 500 friend class AttrBuilder; 501 friend class AttributeListImpl; 502 friend class AttributeSet; 503 friend class AttributeSetNode; 504 template <typename Ty, typename Enable> friend struct DenseMapInfo; 505 506 /// The attributes that we are managing. This can be null to represent 507 /// the empty attributes list. 508 AttributeListImpl *pImpl = nullptr; 509 510 public: 511 /// Create an AttributeList with the specified parameters in it. 512 static AttributeList get(LLVMContext &C, 513 ArrayRef<std::pair<unsigned, Attribute>> Attrs); 514 static AttributeList get(LLVMContext &C, 515 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs); 516 517 /// Create an AttributeList from attribute sets for a function, its 518 /// return value, and all of its arguments. 519 static AttributeList get(LLVMContext &C, AttributeSet FnAttrs, 520 AttributeSet RetAttrs, 521 ArrayRef<AttributeSet> ArgAttrs); 522 523 private: 524 explicit AttributeList(AttributeListImpl *LI) : pImpl(LI) {} 525 526 static AttributeList getImpl(LLVMContext &C, ArrayRef<AttributeSet> AttrSets); 527 528 AttributeList setAttributesAtIndex(LLVMContext &C, unsigned Index, 529 AttributeSet Attrs) const; 530 531 public: 532 AttributeList() = default; 533 534 //===--------------------------------------------------------------------===// 535 // AttributeList Construction and Mutation 536 //===--------------------------------------------------------------------===// 537 538 /// Return an AttributeList with the specified parameters in it. 539 static AttributeList get(LLVMContext &C, ArrayRef<AttributeList> Attrs); 540 static AttributeList get(LLVMContext &C, unsigned Index, 541 ArrayRef<Attribute::AttrKind> Kinds); 542 static AttributeList get(LLVMContext &C, unsigned Index, 543 ArrayRef<Attribute::AttrKind> Kinds, 544 ArrayRef<uint64_t> Values); 545 static AttributeList get(LLVMContext &C, unsigned Index, 546 ArrayRef<StringRef> Kind); 547 static AttributeList get(LLVMContext &C, unsigned Index, 548 AttributeSet Attrs); 549 static AttributeList get(LLVMContext &C, unsigned Index, 550 const AttrBuilder &B); 551 552 // TODO: remove non-AtIndex versions of these methods. 553 /// Add an attribute to the attribute set at the given index. 554 /// Returns a new list because attribute lists are immutable. 555 [[nodiscard]] AttributeList 556 addAttributeAtIndex(LLVMContext &C, unsigned Index, 557 Attribute::AttrKind Kind) const; 558 559 /// Add an attribute to the attribute set at the given index. 560 /// Returns a new list because attribute lists are immutable. 561 [[nodiscard]] AttributeList 562 addAttributeAtIndex(LLVMContext &C, unsigned Index, StringRef Kind, 563 StringRef Value = StringRef()) const; 564 565 /// Add an attribute to the attribute set at the given index. 566 /// Returns a new list because attribute lists are immutable. 567 [[nodiscard]] AttributeList 568 addAttributeAtIndex(LLVMContext &C, unsigned Index, Attribute A) const; 569 570 /// Add attributes to the attribute set at the given index. 571 /// Returns a new list because attribute lists are immutable. 572 [[nodiscard]] AttributeList addAttributesAtIndex(LLVMContext &C, 573 unsigned Index, 574 const AttrBuilder &B) const; 575 576 /// Add a function attribute to the list. Returns a new list because 577 /// attribute lists are immutable. 578 [[nodiscard]] AttributeList addFnAttribute(LLVMContext &C, 579 Attribute::AttrKind Kind) const { 580 return addAttributeAtIndex(C, FunctionIndex, Kind); 581 } 582 583 /// Add a function attribute to the list. Returns a new list because 584 /// attribute lists are immutable. 585 [[nodiscard]] AttributeList addFnAttribute(LLVMContext &C, 586 Attribute Attr) const { 587 return addAttributeAtIndex(C, FunctionIndex, Attr); 588 } 589 590 /// Add a function attribute to the list. Returns a new list because 591 /// attribute lists are immutable. 592 [[nodiscard]] AttributeList 593 addFnAttribute(LLVMContext &C, StringRef Kind, 594 StringRef Value = StringRef()) const { 595 return addAttributeAtIndex(C, FunctionIndex, Kind, Value); 596 } 597 598 /// Add function attribute to the list. Returns a new list because 599 /// attribute lists are immutable. 600 [[nodiscard]] AttributeList addFnAttributes(LLVMContext &C, 601 const AttrBuilder &B) const { 602 return addAttributesAtIndex(C, FunctionIndex, B); 603 } 604 605 /// Add a return value attribute to the list. Returns a new list because 606 /// attribute lists are immutable. 607 [[nodiscard]] AttributeList addRetAttribute(LLVMContext &C, 608 Attribute::AttrKind Kind) const { 609 return addAttributeAtIndex(C, ReturnIndex, Kind); 610 } 611 612 /// Add a return value attribute to the list. Returns a new list because 613 /// attribute lists are immutable. 614 [[nodiscard]] AttributeList addRetAttribute(LLVMContext &C, 615 Attribute Attr) const { 616 return addAttributeAtIndex(C, ReturnIndex, Attr); 617 } 618 619 /// Add a return value attribute to the list. Returns a new list because 620 /// attribute lists are immutable. 621 [[nodiscard]] AttributeList addRetAttributes(LLVMContext &C, 622 const AttrBuilder &B) const { 623 return addAttributesAtIndex(C, ReturnIndex, B); 624 } 625 626 /// Add an argument attribute to the list. Returns a new list because 627 /// attribute lists are immutable. 628 [[nodiscard]] AttributeList 629 addParamAttribute(LLVMContext &C, unsigned ArgNo, 630 Attribute::AttrKind Kind) const { 631 return addAttributeAtIndex(C, ArgNo + FirstArgIndex, Kind); 632 } 633 634 /// Add an argument attribute to the list. Returns a new list because 635 /// attribute lists are immutable. 636 [[nodiscard]] AttributeList 637 addParamAttribute(LLVMContext &C, unsigned ArgNo, StringRef Kind, 638 StringRef Value = StringRef()) const { 639 return addAttributeAtIndex(C, ArgNo + FirstArgIndex, Kind, Value); 640 } 641 642 /// Add an attribute to the attribute list at the given arg indices. Returns a 643 /// new list because attribute lists are immutable. 644 [[nodiscard]] AttributeList addParamAttribute(LLVMContext &C, 645 ArrayRef<unsigned> ArgNos, 646 Attribute A) const; 647 648 /// Add an argument attribute to the list. Returns a new list because 649 /// attribute lists are immutable. 650 [[nodiscard]] AttributeList addParamAttributes(LLVMContext &C, unsigned ArgNo, 651 const AttrBuilder &B) const { 652 return addAttributesAtIndex(C, ArgNo + FirstArgIndex, B); 653 } 654 655 /// Remove the specified attribute at the specified index from this 656 /// attribute list. Returns a new list because attribute lists are immutable. 657 [[nodiscard]] AttributeList 658 removeAttributeAtIndex(LLVMContext &C, unsigned Index, 659 Attribute::AttrKind Kind) const; 660 661 /// Remove the specified attribute at the specified index from this 662 /// attribute list. Returns a new list because attribute lists are immutable. 663 [[nodiscard]] AttributeList 664 removeAttributeAtIndex(LLVMContext &C, unsigned Index, StringRef Kind) const; 665 [[nodiscard]] AttributeList removeAttribute(LLVMContext &C, unsigned Index, 666 StringRef Kind) const { 667 return removeAttributeAtIndex(C, Index, Kind); 668 } 669 670 /// Remove the specified attributes at the specified index from this 671 /// attribute list. Returns a new list because attribute lists are immutable. 672 [[nodiscard]] AttributeList 673 removeAttributesAtIndex(LLVMContext &C, unsigned Index, 674 const AttributeMask &AttrsToRemove) const; 675 676 /// Remove all attributes at the specified index from this 677 /// attribute list. Returns a new list because attribute lists are immutable. 678 [[nodiscard]] AttributeList removeAttributesAtIndex(LLVMContext &C, 679 unsigned Index) const; 680 681 /// Remove the specified attribute at the function index from this 682 /// attribute list. Returns a new list because attribute lists are immutable. 683 [[nodiscard]] AttributeList 684 removeFnAttribute(LLVMContext &C, Attribute::AttrKind Kind) const { 685 return removeAttributeAtIndex(C, FunctionIndex, Kind); 686 } 687 688 /// Remove the specified attribute at the function index from this 689 /// attribute list. Returns a new list because attribute lists are immutable. 690 [[nodiscard]] AttributeList removeFnAttribute(LLVMContext &C, 691 StringRef Kind) const { 692 return removeAttributeAtIndex(C, FunctionIndex, Kind); 693 } 694 695 /// Remove the specified attribute at the function index from this 696 /// attribute list. Returns a new list because attribute lists are immutable. 697 [[nodiscard]] AttributeList 698 removeFnAttributes(LLVMContext &C, const AttributeMask &AttrsToRemove) const { 699 return removeAttributesAtIndex(C, FunctionIndex, AttrsToRemove); 700 } 701 702 /// Remove the attributes at the function index from this 703 /// attribute list. Returns a new list because attribute lists are immutable. 704 [[nodiscard]] AttributeList removeFnAttributes(LLVMContext &C) const { 705 return removeAttributesAtIndex(C, FunctionIndex); 706 } 707 708 /// Remove the specified attribute at the return value index from this 709 /// attribute list. Returns a new list because attribute lists are immutable. 710 [[nodiscard]] AttributeList 711 removeRetAttribute(LLVMContext &C, Attribute::AttrKind Kind) const { 712 return removeAttributeAtIndex(C, ReturnIndex, Kind); 713 } 714 715 /// Remove the specified attribute at the return value index from this 716 /// attribute list. Returns a new list because attribute lists are immutable. 717 [[nodiscard]] AttributeList removeRetAttribute(LLVMContext &C, 718 StringRef Kind) const { 719 return removeAttributeAtIndex(C, ReturnIndex, Kind); 720 } 721 722 /// Remove the specified attribute at the return value index from this 723 /// attribute list. Returns a new list because attribute lists are immutable. 724 [[nodiscard]] AttributeList 725 removeRetAttributes(LLVMContext &C, 726 const AttributeMask &AttrsToRemove) const { 727 return removeAttributesAtIndex(C, ReturnIndex, AttrsToRemove); 728 } 729 730 /// Remove the specified attribute at the specified arg index from this 731 /// attribute list. Returns a new list because attribute lists are immutable. 732 [[nodiscard]] AttributeList 733 removeParamAttribute(LLVMContext &C, unsigned ArgNo, 734 Attribute::AttrKind Kind) const { 735 return removeAttributeAtIndex(C, ArgNo + FirstArgIndex, Kind); 736 } 737 738 /// Remove the specified attribute at the specified arg index from this 739 /// attribute list. Returns a new list because attribute lists are immutable. 740 [[nodiscard]] AttributeList 741 removeParamAttribute(LLVMContext &C, unsigned ArgNo, StringRef Kind) const { 742 return removeAttributeAtIndex(C, ArgNo + FirstArgIndex, Kind); 743 } 744 745 /// Remove the specified attribute at the specified arg index from this 746 /// attribute list. Returns a new list because attribute lists are immutable. 747 [[nodiscard]] AttributeList 748 removeParamAttributes(LLVMContext &C, unsigned ArgNo, 749 const AttributeMask &AttrsToRemove) const { 750 return removeAttributesAtIndex(C, ArgNo + FirstArgIndex, AttrsToRemove); 751 } 752 753 /// Remove all attributes at the specified arg index from this 754 /// attribute list. Returns a new list because attribute lists are immutable. 755 [[nodiscard]] AttributeList removeParamAttributes(LLVMContext &C, 756 unsigned ArgNo) const { 757 return removeAttributesAtIndex(C, ArgNo + FirstArgIndex); 758 } 759 760 /// Replace the type contained by attribute \p AttrKind at index \p ArgNo wih 761 /// \p ReplacementTy, preserving all other attributes. 762 [[nodiscard]] AttributeList 763 replaceAttributeTypeAtIndex(LLVMContext &C, unsigned ArgNo, 764 Attribute::AttrKind Kind, 765 Type *ReplacementTy) const { 766 Attribute Attr = getAttributeAtIndex(ArgNo, Kind); 767 auto Attrs = removeAttributeAtIndex(C, ArgNo, Kind); 768 return Attrs.addAttributeAtIndex(C, ArgNo, 769 Attr.getWithNewType(C, ReplacementTy)); 770 } 771 772 /// \brief Add the dereferenceable attribute to the attribute set at the given 773 /// index. Returns a new list because attribute lists are immutable. 774 [[nodiscard]] AttributeList addDereferenceableRetAttr(LLVMContext &C, 775 uint64_t Bytes) const; 776 777 /// \brief Add the dereferenceable attribute to the attribute set at the given 778 /// arg index. Returns a new list because attribute lists are immutable. 779 [[nodiscard]] AttributeList addDereferenceableParamAttr(LLVMContext &C, 780 unsigned ArgNo, 781 uint64_t Bytes) const; 782 783 /// Add the dereferenceable_or_null attribute to the attribute set at 784 /// the given arg index. Returns a new list because attribute lists are 785 /// immutable. 786 [[nodiscard]] AttributeList 787 addDereferenceableOrNullParamAttr(LLVMContext &C, unsigned ArgNo, 788 uint64_t Bytes) const; 789 790 /// Add the range attribute to the attribute set at the return value index. 791 /// Returns a new list because attribute lists are immutable. 792 [[nodiscard]] AttributeList addRangeRetAttr(LLVMContext &C, 793 const ConstantRange &CR) const; 794 795 /// Add the allocsize attribute to the attribute set at the given arg index. 796 /// Returns a new list because attribute lists are immutable. 797 [[nodiscard]] AttributeList 798 addAllocSizeParamAttr(LLVMContext &C, unsigned ArgNo, unsigned ElemSizeArg, 799 const std::optional<unsigned> &NumElemsArg) const; 800 801 /// Try to intersect this AttributeList with Other. Returns std::nullopt if 802 /// the two lists are inherently incompatible (imply different behavior, not 803 /// just analysis). 804 [[nodiscard]] std::optional<AttributeList> 805 intersectWith(LLVMContext &C, AttributeList Other) const; 806 807 //===--------------------------------------------------------------------===// 808 // AttributeList Accessors 809 //===--------------------------------------------------------------------===// 810 811 /// The attributes for the specified index are returned. 812 AttributeSet getAttributes(unsigned Index) const; 813 814 /// The attributes for the argument or parameter at the given index are 815 /// returned. 816 AttributeSet getParamAttrs(unsigned ArgNo) const; 817 818 /// The attributes for the ret value are returned. 819 AttributeSet getRetAttrs() const; 820 821 /// The function attributes are returned. 822 AttributeSet getFnAttrs() const; 823 824 /// Return true if the attribute exists at the given index. 825 bool hasAttributeAtIndex(unsigned Index, Attribute::AttrKind Kind) const; 826 827 /// Return true if the attribute exists at the given index. 828 bool hasAttributeAtIndex(unsigned Index, StringRef Kind) const; 829 830 /// Return true if attribute exists at the given index. 831 bool hasAttributesAtIndex(unsigned Index) const; 832 833 /// Return true if the attribute exists for the given argument 834 bool hasParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const { 835 return hasAttributeAtIndex(ArgNo + FirstArgIndex, Kind); 836 } 837 838 /// Return true if the attribute exists for the given argument 839 bool hasParamAttr(unsigned ArgNo, StringRef Kind) const { 840 return hasAttributeAtIndex(ArgNo + FirstArgIndex, Kind); 841 } 842 843 /// Return true if attributes exists for the given argument 844 bool hasParamAttrs(unsigned ArgNo) const { 845 return hasAttributesAtIndex(ArgNo + FirstArgIndex); 846 } 847 848 /// Return true if the attribute exists for the return value. 849 bool hasRetAttr(Attribute::AttrKind Kind) const { 850 return hasAttributeAtIndex(ReturnIndex, Kind); 851 } 852 853 /// Return true if the attribute exists for the return value. 854 bool hasRetAttr(StringRef Kind) const { 855 return hasAttributeAtIndex(ReturnIndex, Kind); 856 } 857 858 /// Return true if attributes exist for the return value. 859 bool hasRetAttrs() const { return hasAttributesAtIndex(ReturnIndex); } 860 861 /// Return true if the attribute exists for the function. 862 bool hasFnAttr(Attribute::AttrKind Kind) const; 863 864 /// Return true if the attribute exists for the function. 865 bool hasFnAttr(StringRef Kind) const; 866 867 /// Return true the attributes exist for the function. 868 bool hasFnAttrs() const { return hasAttributesAtIndex(FunctionIndex); } 869 870 /// Return true if the specified attribute is set for at least one 871 /// parameter or for the return value. If Index is not nullptr, the index 872 /// of a parameter with the specified attribute is provided. 873 bool hasAttrSomewhere(Attribute::AttrKind Kind, 874 unsigned *Index = nullptr) const; 875 876 /// Return the attribute object that exists at the given index. 877 Attribute getAttributeAtIndex(unsigned Index, Attribute::AttrKind Kind) const; 878 879 /// Return the attribute object that exists at the given index. 880 Attribute getAttributeAtIndex(unsigned Index, StringRef Kind) const; 881 882 /// Return the attribute object that exists at the arg index. 883 Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const { 884 return getAttributeAtIndex(ArgNo + FirstArgIndex, Kind); 885 } 886 887 /// Return the attribute object that exists at the given index. 888 Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const { 889 return getAttributeAtIndex(ArgNo + FirstArgIndex, Kind); 890 } 891 892 /// Return the attribute object that exists for the function. 893 Attribute getFnAttr(Attribute::AttrKind Kind) const { 894 return getAttributeAtIndex(FunctionIndex, Kind); 895 } 896 897 /// Return the attribute object that exists for the function. 898 Attribute getFnAttr(StringRef Kind) const { 899 return getAttributeAtIndex(FunctionIndex, Kind); 900 } 901 902 /// Return the attribute for the given attribute kind for the return value. 903 Attribute getRetAttr(Attribute::AttrKind Kind) const { 904 return getAttributeAtIndex(ReturnIndex, Kind); 905 } 906 907 /// Return the alignment of the return value. 908 MaybeAlign getRetAlignment() const; 909 910 /// Return the alignment for the specified function parameter. 911 MaybeAlign getParamAlignment(unsigned ArgNo) const; 912 913 /// Return the stack alignment for the specified function parameter. 914 MaybeAlign getParamStackAlignment(unsigned ArgNo) const; 915 916 /// Return the byval type for the specified function parameter. 917 Type *getParamByValType(unsigned ArgNo) const; 918 919 /// Return the sret type for the specified function parameter. 920 Type *getParamStructRetType(unsigned ArgNo) const; 921 922 /// Return the byref type for the specified function parameter. 923 Type *getParamByRefType(unsigned ArgNo) const; 924 925 /// Return the preallocated type for the specified function parameter. 926 Type *getParamPreallocatedType(unsigned ArgNo) const; 927 928 /// Return the inalloca type for the specified function parameter. 929 Type *getParamInAllocaType(unsigned ArgNo) const; 930 931 /// Return the elementtype type for the specified function parameter. 932 Type *getParamElementType(unsigned ArgNo) const; 933 934 /// Get the stack alignment of the function. 935 MaybeAlign getFnStackAlignment() const; 936 937 /// Get the stack alignment of the return value. 938 MaybeAlign getRetStackAlignment() const; 939 940 /// Get the number of dereferenceable bytes (or zero if unknown) of the return 941 /// value. 942 uint64_t getRetDereferenceableBytes() const; 943 944 /// Get the number of dereferenceable bytes (or zero if unknown) of an arg. 945 uint64_t getParamDereferenceableBytes(unsigned Index) const; 946 947 /// Get the number of dereferenceable_or_null bytes (or zero if unknown) of 948 /// the return value. 949 uint64_t getRetDereferenceableOrNullBytes() const; 950 951 /// Get the number of dereferenceable_or_null bytes (or zero if unknown) of an 952 /// arg. 953 uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo) const; 954 955 /// Get range (or std::nullopt if unknown) of an arg. 956 std::optional<ConstantRange> getParamRange(unsigned ArgNo) const; 957 958 /// Get the disallowed floating-point classes of the return value. 959 FPClassTest getRetNoFPClass() const; 960 961 /// Get the disallowed floating-point classes of the argument value. 962 FPClassTest getParamNoFPClass(unsigned ArgNo) const; 963 964 /// Get the unwind table kind requested for the function. 965 UWTableKind getUWTableKind() const; 966 967 AllocFnKind getAllocKind() const; 968 969 /// Returns memory effects of the function. 970 MemoryEffects getMemoryEffects() const; 971 972 /// Return the attributes at the index as a string. 973 std::string getAsString(unsigned Index, bool InAttrGrp = false) const; 974 975 /// Return true if this attribute list belongs to the LLVMContext. 976 bool hasParentContext(LLVMContext &C) const; 977 978 //===--------------------------------------------------------------------===// 979 // AttributeList Introspection 980 //===--------------------------------------------------------------------===// 981 982 using iterator = const AttributeSet *; 983 984 iterator begin() const; 985 iterator end() const; 986 987 unsigned getNumAttrSets() const; 988 989 // Implementation of indexes(). Produces iterators that wrap an index. Mostly 990 // to hide the awkwardness of unsigned wrapping when iterating over valid 991 // indexes. 992 struct index_iterator { 993 unsigned NumAttrSets; 994 index_iterator(int NumAttrSets) : NumAttrSets(NumAttrSets) {} 995 struct int_wrapper { 996 int_wrapper(unsigned i) : i(i) {} 997 unsigned i; 998 unsigned operator*() { return i; } 999 bool operator!=(const int_wrapper &Other) { return i != Other.i; } 1000 int_wrapper &operator++() { 1001 // This is expected to undergo unsigned wrapping since FunctionIndex is 1002 // ~0 and that's where we start. 1003 ++i; 1004 return *this; 1005 } 1006 }; 1007 1008 int_wrapper begin() { return int_wrapper(AttributeList::FunctionIndex); } 1009 1010 int_wrapper end() { return int_wrapper(NumAttrSets - 1); } 1011 }; 1012 1013 /// Use this to iterate over the valid attribute indexes. 1014 index_iterator indexes() const { return index_iterator(getNumAttrSets()); } 1015 1016 /// operator==/!= - Provide equality predicates. 1017 bool operator==(const AttributeList &RHS) const { return pImpl == RHS.pImpl; } 1018 bool operator!=(const AttributeList &RHS) const { return pImpl != RHS.pImpl; } 1019 1020 /// Return a raw pointer that uniquely identifies this attribute list. 1021 void *getRawPointer() const { 1022 return pImpl; 1023 } 1024 1025 /// Return true if there are no attributes. 1026 bool isEmpty() const { return pImpl == nullptr; } 1027 1028 void print(raw_ostream &O) const; 1029 1030 void dump() const; 1031 }; 1032 1033 //===----------------------------------------------------------------------===// 1034 /// \class 1035 /// Provide DenseMapInfo for AttributeList. 1036 template <> struct DenseMapInfo<AttributeList, void> { 1037 static AttributeList getEmptyKey() { 1038 auto Val = static_cast<uintptr_t>(-1); 1039 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable; 1040 return AttributeList(reinterpret_cast<AttributeListImpl *>(Val)); 1041 } 1042 1043 static AttributeList getTombstoneKey() { 1044 auto Val = static_cast<uintptr_t>(-2); 1045 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable; 1046 return AttributeList(reinterpret_cast<AttributeListImpl *>(Val)); 1047 } 1048 1049 static unsigned getHashValue(AttributeList AS) { 1050 return (unsigned((uintptr_t)AS.pImpl) >> 4) ^ 1051 (unsigned((uintptr_t)AS.pImpl) >> 9); 1052 } 1053 1054 static bool isEqual(AttributeList LHS, AttributeList RHS) { 1055 return LHS == RHS; 1056 } 1057 }; 1058 1059 //===----------------------------------------------------------------------===// 1060 /// \class 1061 /// This class is used in conjunction with the Attribute::get method to 1062 /// create an Attribute object. The object itself is uniquified. The Builder's 1063 /// value, however, is not. So this can be used as a quick way to test for 1064 /// equality, presence of attributes, etc. 1065 class AttrBuilder { 1066 LLVMContext &Ctx; 1067 SmallVector<Attribute, 8> Attrs; 1068 1069 public: 1070 AttrBuilder(LLVMContext &Ctx) : Ctx(Ctx) {} 1071 AttrBuilder(const AttrBuilder &) = delete; 1072 AttrBuilder(AttrBuilder &&) = default; 1073 1074 AttrBuilder(LLVMContext &Ctx, const Attribute &A) : Ctx(Ctx) { 1075 addAttribute(A); 1076 } 1077 1078 AttrBuilder(LLVMContext &Ctx, AttributeSet AS); 1079 1080 void clear(); 1081 1082 /// Add an attribute to the builder. 1083 AttrBuilder &addAttribute(Attribute::AttrKind Val); 1084 1085 /// Add the Attribute object to the builder. 1086 AttrBuilder &addAttribute(Attribute A); 1087 1088 /// Add the target-dependent attribute to the builder. 1089 AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef()); 1090 1091 /// Remove an attribute from the builder. 1092 AttrBuilder &removeAttribute(Attribute::AttrKind Val); 1093 1094 /// Remove the target-dependent attribute from the builder. 1095 AttrBuilder &removeAttribute(StringRef A); 1096 1097 /// Remove the target-dependent attribute from the builder. 1098 AttrBuilder &removeAttribute(Attribute A) { 1099 if (A.isStringAttribute()) 1100 return removeAttribute(A.getKindAsString()); 1101 else 1102 return removeAttribute(A.getKindAsEnum()); 1103 } 1104 1105 /// Add the attributes from the builder. Attributes in the passed builder 1106 /// overwrite attributes in this builder if they have the same key. 1107 AttrBuilder &merge(const AttrBuilder &B); 1108 1109 /// Remove the attributes from the builder. 1110 AttrBuilder &remove(const AttributeMask &AM); 1111 1112 /// Return true if the builder has any attribute that's in the 1113 /// specified builder. 1114 bool overlaps(const AttributeMask &AM) const; 1115 1116 /// Return true if the builder has the specified attribute. 1117 bool contains(Attribute::AttrKind A) const; 1118 1119 /// Return true if the builder has the specified target-dependent 1120 /// attribute. 1121 bool contains(StringRef A) const; 1122 1123 /// Return true if the builder has IR-level attributes. 1124 bool hasAttributes() const { return !Attrs.empty(); } 1125 1126 /// Return Attribute with the given Kind. The returned attribute will be 1127 /// invalid if the Kind is not present in the builder. 1128 Attribute getAttribute(Attribute::AttrKind Kind) const; 1129 1130 /// Return Attribute with the given Kind. The returned attribute will be 1131 /// invalid if the Kind is not present in the builder. 1132 Attribute getAttribute(StringRef Kind) const; 1133 1134 /// Retrieve the range if the attribute exists (std::nullopt is returned 1135 /// otherwise). 1136 std::optional<ConstantRange> getRange() const; 1137 1138 /// Return raw (possibly packed/encoded) value of integer attribute or 1139 /// std::nullopt if not set. 1140 std::optional<uint64_t> getRawIntAttr(Attribute::AttrKind Kind) const; 1141 1142 /// Retrieve the alignment attribute, if it exists. 1143 MaybeAlign getAlignment() const { 1144 return MaybeAlign(getRawIntAttr(Attribute::Alignment).value_or(0)); 1145 } 1146 1147 /// Retrieve the stack alignment attribute, if it exists. 1148 MaybeAlign getStackAlignment() const { 1149 return MaybeAlign(getRawIntAttr(Attribute::StackAlignment).value_or(0)); 1150 } 1151 1152 /// Retrieve the number of dereferenceable bytes, if the 1153 /// dereferenceable attribute exists (zero is returned otherwise). 1154 uint64_t getDereferenceableBytes() const { 1155 return getRawIntAttr(Attribute::Dereferenceable).value_or(0); 1156 } 1157 1158 /// Retrieve the number of dereferenceable_or_null bytes, if the 1159 /// dereferenceable_or_null attribute exists (zero is returned otherwise). 1160 uint64_t getDereferenceableOrNullBytes() const { 1161 return getRawIntAttr(Attribute::DereferenceableOrNull).value_or(0); 1162 } 1163 1164 /// Retrieve type for the given type attribute. 1165 Type *getTypeAttr(Attribute::AttrKind Kind) const; 1166 1167 /// Retrieve the byval type. 1168 Type *getByValType() const { return getTypeAttr(Attribute::ByVal); } 1169 1170 /// Retrieve the sret type. 1171 Type *getStructRetType() const { return getTypeAttr(Attribute::StructRet); } 1172 1173 /// Retrieve the byref type. 1174 Type *getByRefType() const { return getTypeAttr(Attribute::ByRef); } 1175 1176 /// Retrieve the preallocated type. 1177 Type *getPreallocatedType() const { 1178 return getTypeAttr(Attribute::Preallocated); 1179 } 1180 1181 /// Retrieve the inalloca type. 1182 Type *getInAllocaType() const { return getTypeAttr(Attribute::InAlloca); } 1183 1184 /// Retrieve the allocsize args, or std::nullopt if the attribute does not 1185 /// exist. 1186 std::optional<std::pair<unsigned, std::optional<unsigned>>> getAllocSizeArgs() 1187 const; 1188 1189 /// Add integer attribute with raw value (packed/encoded if necessary). 1190 AttrBuilder &addRawIntAttr(Attribute::AttrKind Kind, uint64_t Value); 1191 1192 /// This turns an alignment into the form used internally in Attribute. 1193 /// This call has no effect if Align is not set. 1194 AttrBuilder &addAlignmentAttr(MaybeAlign Align); 1195 1196 /// This turns an int alignment (which must be a power of 2) into the 1197 /// form used internally in Attribute. 1198 /// This call has no effect if Align is 0. 1199 /// Deprecated, use the version using a MaybeAlign. 1200 inline AttrBuilder &addAlignmentAttr(unsigned Align) { 1201 return addAlignmentAttr(MaybeAlign(Align)); 1202 } 1203 1204 /// This turns a stack alignment into the form used internally in Attribute. 1205 /// This call has no effect if Align is not set. 1206 AttrBuilder &addStackAlignmentAttr(MaybeAlign Align); 1207 1208 /// This turns an int stack alignment (which must be a power of 2) into 1209 /// the form used internally in Attribute. 1210 /// This call has no effect if Align is 0. 1211 /// Deprecated, use the version using a MaybeAlign. 1212 inline AttrBuilder &addStackAlignmentAttr(unsigned Align) { 1213 return addStackAlignmentAttr(MaybeAlign(Align)); 1214 } 1215 1216 /// This turns the number of dereferenceable bytes into the form used 1217 /// internally in Attribute. 1218 AttrBuilder &addDereferenceableAttr(uint64_t Bytes); 1219 1220 /// This turns the number of dereferenceable_or_null bytes into the 1221 /// form used internally in Attribute. 1222 AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes); 1223 1224 /// This turns one (or two) ints into the form used internally in Attribute. 1225 AttrBuilder &addAllocSizeAttr(unsigned ElemSizeArg, 1226 const std::optional<unsigned> &NumElemsArg); 1227 1228 /// This turns two ints into the form used internally in Attribute. 1229 AttrBuilder &addVScaleRangeAttr(unsigned MinValue, 1230 std::optional<unsigned> MaxValue); 1231 1232 /// Add a type attribute with the given type. 1233 AttrBuilder &addTypeAttr(Attribute::AttrKind Kind, Type *Ty); 1234 1235 /// This turns a byval type into the form used internally in Attribute. 1236 AttrBuilder &addByValAttr(Type *Ty); 1237 1238 /// This turns a sret type into the form used internally in Attribute. 1239 AttrBuilder &addStructRetAttr(Type *Ty); 1240 1241 /// This turns a byref type into the form used internally in Attribute. 1242 AttrBuilder &addByRefAttr(Type *Ty); 1243 1244 /// This turns a preallocated type into the form used internally in Attribute. 1245 AttrBuilder &addPreallocatedAttr(Type *Ty); 1246 1247 /// This turns an inalloca type into the form used internally in Attribute. 1248 AttrBuilder &addInAllocaAttr(Type *Ty); 1249 1250 /// Add an allocsize attribute, using the representation returned by 1251 /// Attribute.getIntValue(). 1252 AttrBuilder &addAllocSizeAttrFromRawRepr(uint64_t RawAllocSizeRepr); 1253 1254 /// Add a vscale_range attribute, using the representation returned by 1255 /// Attribute.getIntValue(). 1256 AttrBuilder &addVScaleRangeAttrFromRawRepr(uint64_t RawVScaleRangeRepr); 1257 1258 /// This turns the unwind table kind into the form used internally in 1259 /// Attribute. 1260 AttrBuilder &addUWTableAttr(UWTableKind Kind); 1261 1262 // This turns the allocator kind into the form used internally in Attribute. 1263 AttrBuilder &addAllocKindAttr(AllocFnKind Kind); 1264 1265 /// Add memory effect attribute. 1266 AttrBuilder &addMemoryAttr(MemoryEffects ME); 1267 1268 /// Add captures attribute. 1269 AttrBuilder &addCapturesAttr(CaptureInfo CI); 1270 1271 // Add nofpclass attribute 1272 AttrBuilder &addNoFPClassAttr(FPClassTest NoFPClassMask); 1273 1274 /// Add a ConstantRange attribute with the given range. 1275 AttrBuilder &addConstantRangeAttr(Attribute::AttrKind Kind, 1276 const ConstantRange &CR); 1277 1278 /// Add range attribute. 1279 AttrBuilder &addRangeAttr(const ConstantRange &CR); 1280 1281 /// Add a ConstantRangeList attribute with the given ranges. 1282 AttrBuilder &addConstantRangeListAttr(Attribute::AttrKind Kind, 1283 ArrayRef<ConstantRange> Val); 1284 1285 /// Add initializes attribute. 1286 AttrBuilder &addInitializesAttr(const ConstantRangeList &CRL); 1287 1288 ArrayRef<Attribute> attrs() const { return Attrs; } 1289 1290 bool operator==(const AttrBuilder &B) const; 1291 bool operator!=(const AttrBuilder &B) const { return !(*this == B); } 1292 }; 1293 1294 namespace AttributeFuncs { 1295 1296 enum AttributeSafetyKind : uint8_t { 1297 ASK_SAFE_TO_DROP = 1, 1298 ASK_UNSAFE_TO_DROP = 2, 1299 ASK_ALL = ASK_SAFE_TO_DROP | ASK_UNSAFE_TO_DROP, 1300 }; 1301 1302 /// Returns true if this is a type legal for the 'nofpclass' attribute. This 1303 /// follows the same type rules as FPMathOperator. 1304 bool isNoFPClassCompatibleType(Type *Ty); 1305 1306 /// Which attributes cannot be applied to a type. The argument \p AS 1307 /// is used as a hint for the attributes whose compatibility is being 1308 /// checked against \p Ty. This does not mean the return will be a 1309 /// subset of \p AS, just that attributes that have specific dynamic 1310 /// type compatibilities (i.e `range`) will be checked against what is 1311 /// contained in \p AS. The argument \p ASK indicates, if only 1312 /// attributes that are known to be safely droppable are contained in 1313 /// the mask; only attributes that might be unsafe to drop (e.g., 1314 /// ABI-related attributes) are in the mask; or both. 1315 AttributeMask typeIncompatible(Type *Ty, AttributeSet AS, 1316 AttributeSafetyKind ASK = ASK_ALL); 1317 1318 /// Get param/return attributes which imply immediate undefined behavior if an 1319 /// invalid value is passed. For example, this includes noundef (where undef 1320 /// implies UB), but not nonnull (where null implies poison). It also does not 1321 /// include attributes like nocapture, which constrain the function 1322 /// implementation rather than the passed value. 1323 AttributeMask getUBImplyingAttributes(); 1324 1325 /// \returns Return true if the two functions have compatible target-independent 1326 /// attributes for inlining purposes. 1327 bool areInlineCompatible(const Function &Caller, const Function &Callee); 1328 1329 1330 /// Checks if there are any incompatible function attributes between 1331 /// \p A and \p B. 1332 /// 1333 /// \param [in] A - The first function to be compared with. 1334 /// \param [in] B - The second function to be compared with. 1335 /// \returns true if the functions have compatible attributes. 1336 bool areOutlineCompatible(const Function &A, const Function &B); 1337 1338 /// Merge caller's and callee's attributes. 1339 void mergeAttributesForInlining(Function &Caller, const Function &Callee); 1340 1341 /// Merges the functions attributes from \p ToMerge into function \p Base. 1342 /// 1343 /// \param [in,out] Base - The function being merged into. 1344 /// \param [in] ToMerge - The function to merge attributes from. 1345 void mergeAttributesForOutlining(Function &Base, const Function &ToMerge); 1346 1347 /// Update min-legal-vector-width if it is in Attribute and less than Width. 1348 void updateMinLegalVectorWidthAttr(Function &Fn, uint64_t Width); 1349 1350 } // end namespace AttributeFuncs 1351 1352 } // end namespace llvm 1353 1354 #endif // LLVM_IR_ATTRIBUTES_H 1355