Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame^] | 1 | //===- llvm/Attributes.h - Container for Attributes -------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | /// \file |
| 11 | /// \brief This file contains the simple types necessary to represent the |
| 12 | /// attributes associated with functions and their calls. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #ifndef LLVM_IR_ATTRIBUTES_H |
| 17 | #define LLVM_IR_ATTRIBUTES_H |
| 18 | |
| 19 | #include "llvm-c/Types.h" |
| 20 | #include "llvm/ADT/ArrayRef.h" |
| 21 | #include "llvm/ADT/FoldingSet.h" |
| 22 | #include "llvm/ADT/Optional.h" |
| 23 | #include "llvm/ADT/StringRef.h" |
| 24 | #include "llvm/ADT/iterator_range.h" |
| 25 | #include "llvm/Support/PointerLikeTypeTraits.h" |
| 26 | #include <bitset> |
| 27 | #include <cassert> |
| 28 | #include <cstdint> |
| 29 | #include <map> |
| 30 | #include <string> |
| 31 | #include <utility> |
| 32 | |
| 33 | namespace llvm { |
| 34 | |
| 35 | class AttrBuilder; |
| 36 | class AttributeImpl; |
| 37 | class AttributeListImpl; |
| 38 | class AttributeSetNode; |
| 39 | template<typename T> struct DenseMapInfo; |
| 40 | class Function; |
| 41 | class LLVMContext; |
| 42 | class Type; |
| 43 | |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | /// \class |
| 46 | /// \brief Functions, function parameters, and return types can have attributes |
| 47 | /// to indicate how they should be treated by optimizations and code |
| 48 | /// generation. This class represents one of those attributes. It's light-weight |
| 49 | /// and should be passed around by-value. |
| 50 | class Attribute { |
| 51 | public: |
| 52 | /// This enumeration lists the attributes that can be associated with |
| 53 | /// parameters, function results, or the function itself. |
| 54 | /// |
| 55 | /// Note: The `uwtable' attribute is about the ABI or the user mandating an |
| 56 | /// entry in the unwind table. The `nounwind' attribute is about an exception |
| 57 | /// passing by the function. |
| 58 | /// |
| 59 | /// In a theoretical system that uses tables for profiling and SjLj for |
| 60 | /// exceptions, they would be fully independent. In a normal system that uses |
| 61 | /// tables for both, the semantics are: |
| 62 | /// |
| 63 | /// nil = Needs an entry because an exception might pass by. |
| 64 | /// nounwind = No need for an entry |
| 65 | /// uwtable = Needs an entry because the ABI says so and because |
| 66 | /// an exception might pass by. |
| 67 | /// uwtable + nounwind = Needs an entry because the ABI says so. |
| 68 | |
| 69 | enum AttrKind { |
| 70 | // IR-Level Attributes |
| 71 | None, ///< No attributes have been set |
| 72 | #define GET_ATTR_ENUM |
| 73 | #include "llvm/IR/Attributes.gen" |
| 74 | EndAttrKinds ///< Sentinal value useful for loops |
| 75 | }; |
| 76 | |
| 77 | private: |
| 78 | AttributeImpl *pImpl = nullptr; |
| 79 | |
| 80 | Attribute(AttributeImpl *A) : pImpl(A) {} |
| 81 | |
| 82 | public: |
| 83 | Attribute() = default; |
| 84 | |
| 85 | //===--------------------------------------------------------------------===// |
| 86 | // Attribute Construction |
| 87 | //===--------------------------------------------------------------------===// |
| 88 | |
| 89 | /// \brief Return a uniquified Attribute object. |
| 90 | static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0); |
| 91 | static Attribute get(LLVMContext &Context, StringRef Kind, |
| 92 | StringRef Val = StringRef()); |
| 93 | |
| 94 | /// \brief Return a uniquified Attribute object that has the specific |
| 95 | /// alignment set. |
| 96 | static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align); |
| 97 | static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align); |
| 98 | static Attribute getWithDereferenceableBytes(LLVMContext &Context, |
| 99 | uint64_t Bytes); |
| 100 | static Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context, |
| 101 | uint64_t Bytes); |
| 102 | static Attribute getWithAllocSizeArgs(LLVMContext &Context, |
| 103 | unsigned ElemSizeArg, |
| 104 | const Optional<unsigned> &NumElemsArg); |
| 105 | |
| 106 | //===--------------------------------------------------------------------===// |
| 107 | // Attribute Accessors |
| 108 | //===--------------------------------------------------------------------===// |
| 109 | |
| 110 | /// \brief Return true if the attribute is an Attribute::AttrKind type. |
| 111 | bool isEnumAttribute() const; |
| 112 | |
| 113 | /// \brief Return true if the attribute is an integer attribute. |
| 114 | bool isIntAttribute() const; |
| 115 | |
| 116 | /// \brief Return true if the attribute is a string (target-dependent) |
| 117 | /// attribute. |
| 118 | bool isStringAttribute() const; |
| 119 | |
| 120 | /// \brief Return true if the attribute is present. |
| 121 | bool hasAttribute(AttrKind Val) const; |
| 122 | |
| 123 | /// \brief Return true if the target-dependent attribute is present. |
| 124 | bool hasAttribute(StringRef Val) const; |
| 125 | |
| 126 | /// \brief Return the attribute's kind as an enum (Attribute::AttrKind). This |
| 127 | /// requires the attribute to be an enum or integer attribute. |
| 128 | Attribute::AttrKind getKindAsEnum() const; |
| 129 | |
| 130 | /// \brief Return the attribute's value as an integer. This requires that the |
| 131 | /// attribute be an integer attribute. |
| 132 | uint64_t getValueAsInt() const; |
| 133 | |
| 134 | /// \brief Return the attribute's kind as a string. This requires the |
| 135 | /// attribute to be a string attribute. |
| 136 | StringRef getKindAsString() const; |
| 137 | |
| 138 | /// \brief Return the attribute's value as a string. This requires the |
| 139 | /// attribute to be a string attribute. |
| 140 | StringRef getValueAsString() const; |
| 141 | |
| 142 | /// \brief Returns the alignment field of an attribute as a byte alignment |
| 143 | /// value. |
| 144 | unsigned getAlignment() const; |
| 145 | |
| 146 | /// \brief Returns the stack alignment field of an attribute as a byte |
| 147 | /// alignment value. |
| 148 | unsigned getStackAlignment() const; |
| 149 | |
| 150 | /// \brief Returns the number of dereferenceable bytes from the |
| 151 | /// dereferenceable attribute. |
| 152 | uint64_t getDereferenceableBytes() const; |
| 153 | |
| 154 | /// \brief Returns the number of dereferenceable_or_null bytes from the |
| 155 | /// dereferenceable_or_null attribute. |
| 156 | uint64_t getDereferenceableOrNullBytes() const; |
| 157 | |
| 158 | /// Returns the argument numbers for the allocsize attribute (or pair(0, 0) |
| 159 | /// if not known). |
| 160 | std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const; |
| 161 | |
| 162 | /// \brief The Attribute is converted to a string of equivalent mnemonic. This |
| 163 | /// is, presumably, for writing out the mnemonics for the assembly writer. |
| 164 | std::string getAsString(bool InAttrGrp = false) const; |
| 165 | |
| 166 | /// \brief Equality and non-equality operators. |
| 167 | bool operator==(Attribute A) const { return pImpl == A.pImpl; } |
| 168 | bool operator!=(Attribute A) const { return pImpl != A.pImpl; } |
| 169 | |
| 170 | /// \brief Less-than operator. Useful for sorting the attributes list. |
| 171 | bool operator<(Attribute A) const; |
| 172 | |
| 173 | void Profile(FoldingSetNodeID &ID) const { |
| 174 | ID.AddPointer(pImpl); |
| 175 | } |
| 176 | |
| 177 | /// \brief Return a raw pointer that uniquely identifies this attribute. |
| 178 | void *getRawPointer() const { |
| 179 | return pImpl; |
| 180 | } |
| 181 | |
| 182 | /// \brief Get an attribute from a raw pointer created by getRawPointer. |
| 183 | static Attribute fromRawPointer(void *RawPtr) { |
| 184 | return Attribute(reinterpret_cast<AttributeImpl*>(RawPtr)); |
| 185 | } |
| 186 | }; |
| 187 | |
| 188 | // Specialized opaque value conversions. |
| 189 | inline LLVMAttributeRef wrap(Attribute Attr) { |
| 190 | return reinterpret_cast<LLVMAttributeRef>(Attr.getRawPointer()); |
| 191 | } |
| 192 | |
| 193 | // Specialized opaque value conversions. |
| 194 | inline Attribute unwrap(LLVMAttributeRef Attr) { |
| 195 | return Attribute::fromRawPointer(Attr); |
| 196 | } |
| 197 | |
| 198 | //===----------------------------------------------------------------------===// |
| 199 | /// \class |
| 200 | /// This class holds the attributes for a particular argument, parameter, |
| 201 | /// function, or return value. It is an immutable value type that is cheap to |
| 202 | /// copy. Adding and removing enum attributes is intended to be fast, but adding |
| 203 | /// and removing string or integer attributes involves a FoldingSet lookup. |
| 204 | class AttributeSet { |
| 205 | friend AttributeListImpl; |
| 206 | template <typename Ty> friend struct DenseMapInfo; |
| 207 | |
| 208 | // TODO: Extract AvailableAttrs from AttributeSetNode and store them here. |
| 209 | // This will allow an efficient implementation of addAttribute and |
| 210 | // removeAttribute for enum attrs. |
| 211 | |
| 212 | /// Private implementation pointer. |
| 213 | AttributeSetNode *SetNode = nullptr; |
| 214 | |
| 215 | private: |
| 216 | explicit AttributeSet(AttributeSetNode *ASN) : SetNode(ASN) {} |
| 217 | |
| 218 | public: |
| 219 | /// AttributeSet is a trivially copyable value type. |
| 220 | AttributeSet() = default; |
| 221 | AttributeSet(const AttributeSet &) = default; |
| 222 | ~AttributeSet() = default; |
| 223 | |
| 224 | static AttributeSet get(LLVMContext &C, const AttrBuilder &B); |
| 225 | static AttributeSet get(LLVMContext &C, ArrayRef<Attribute> Attrs); |
| 226 | |
| 227 | bool operator==(const AttributeSet &O) const { return SetNode == O.SetNode; } |
| 228 | bool operator!=(const AttributeSet &O) const { return !(*this == O); } |
| 229 | |
| 230 | /// Add an argument attribute. Returns a new set because attribute sets are |
| 231 | /// immutable. |
| 232 | AttributeSet addAttribute(LLVMContext &C, Attribute::AttrKind Kind) const; |
| 233 | |
| 234 | /// Add a target-dependent attribute. Returns a new set because attribute sets |
| 235 | /// are immutable. |
| 236 | AttributeSet addAttribute(LLVMContext &C, StringRef Kind, |
| 237 | StringRef Value = StringRef()) const; |
| 238 | |
| 239 | /// Add attributes to the attribute set. Returns a new set because attribute |
| 240 | /// sets are immutable. |
| 241 | AttributeSet addAttributes(LLVMContext &C, AttributeSet AS) const; |
| 242 | |
| 243 | /// Remove the specified attribute from this set. Returns a new set because |
| 244 | /// attribute sets are immutable. |
| 245 | AttributeSet removeAttribute(LLVMContext &C, Attribute::AttrKind Kind) const; |
| 246 | |
| 247 | /// Remove the specified attribute from this set. Returns a new set because |
| 248 | /// attribute sets are immutable. |
| 249 | AttributeSet removeAttribute(LLVMContext &C, StringRef Kind) const; |
| 250 | |
| 251 | /// Remove the specified attributes from this set. Returns a new set because |
| 252 | /// attribute sets are immutable. |
| 253 | AttributeSet removeAttributes(LLVMContext &C, |
| 254 | const AttrBuilder &AttrsToRemove) const; |
| 255 | |
| 256 | /// Return the number of attributes in this set. |
| 257 | unsigned getNumAttributes() const; |
| 258 | |
| 259 | /// Return true if attributes exists in this set. |
| 260 | bool hasAttributes() const { return SetNode != nullptr; } |
| 261 | |
| 262 | /// Return true if the attribute exists in this set. |
| 263 | bool hasAttribute(Attribute::AttrKind Kind) const; |
| 264 | |
| 265 | /// Return true if the attribute exists in this set. |
| 266 | bool hasAttribute(StringRef Kind) const; |
| 267 | |
| 268 | /// Return the attribute object. |
| 269 | Attribute getAttribute(Attribute::AttrKind Kind) const; |
| 270 | |
| 271 | /// Return the target-dependent attribute object. |
| 272 | Attribute getAttribute(StringRef Kind) const; |
| 273 | |
| 274 | unsigned getAlignment() const; |
| 275 | unsigned getStackAlignment() const; |
| 276 | uint64_t getDereferenceableBytes() const; |
| 277 | uint64_t getDereferenceableOrNullBytes() const; |
| 278 | std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const; |
| 279 | std::string getAsString(bool InAttrGrp = false) const; |
| 280 | |
| 281 | using iterator = const Attribute *; |
| 282 | |
| 283 | iterator begin() const; |
| 284 | iterator end() const; |
| 285 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 286 | void dump() const; |
| 287 | #endif |
| 288 | }; |
| 289 | |
| 290 | //===----------------------------------------------------------------------===// |
| 291 | /// \class |
| 292 | /// \brief Provide DenseMapInfo for AttributeSet. |
| 293 | template <> struct DenseMapInfo<AttributeSet> { |
| 294 | static AttributeSet getEmptyKey() { |
| 295 | auto Val = static_cast<uintptr_t>(-1); |
| 296 | Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable; |
| 297 | return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val)); |
| 298 | } |
| 299 | |
| 300 | static AttributeSet getTombstoneKey() { |
| 301 | auto Val = static_cast<uintptr_t>(-2); |
| 302 | Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable; |
| 303 | return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val)); |
| 304 | } |
| 305 | |
| 306 | static unsigned getHashValue(AttributeSet AS) { |
| 307 | return (unsigned((uintptr_t)AS.SetNode) >> 4) ^ |
| 308 | (unsigned((uintptr_t)AS.SetNode) >> 9); |
| 309 | } |
| 310 | |
| 311 | static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; } |
| 312 | }; |
| 313 | |
| 314 | //===----------------------------------------------------------------------===// |
| 315 | /// \class |
| 316 | /// \brief This class holds the attributes for a function, its return value, and |
| 317 | /// its parameters. You access the attributes for each of them via an index into |
| 318 | /// the AttributeList object. The function attributes are at index |
| 319 | /// `AttributeList::FunctionIndex', the return value is at index |
| 320 | /// `AttributeList::ReturnIndex', and the attributes for the parameters start at |
| 321 | /// index `AttributeList::FirstArgIndex'. |
| 322 | class AttributeList { |
| 323 | public: |
| 324 | enum AttrIndex : unsigned { |
| 325 | ReturnIndex = 0U, |
| 326 | FunctionIndex = ~0U, |
| 327 | FirstArgIndex = 1, |
| 328 | }; |
| 329 | |
| 330 | private: |
| 331 | friend class AttrBuilder; |
| 332 | friend class AttributeListImpl; |
| 333 | friend class AttributeSet; |
| 334 | friend class AttributeSetNode; |
| 335 | template <typename Ty> friend struct DenseMapInfo; |
| 336 | |
| 337 | /// \brief The attributes that we are managing. This can be null to represent |
| 338 | /// the empty attributes list. |
| 339 | AttributeListImpl *pImpl = nullptr; |
| 340 | |
| 341 | public: |
| 342 | /// \brief Create an AttributeList with the specified parameters in it. |
| 343 | static AttributeList get(LLVMContext &C, |
| 344 | ArrayRef<std::pair<unsigned, Attribute>> Attrs); |
| 345 | static AttributeList get(LLVMContext &C, |
| 346 | ArrayRef<std::pair<unsigned, AttributeSet>> Attrs); |
| 347 | |
| 348 | /// \brief Create an AttributeList from attribute sets for a function, its |
| 349 | /// return value, and all of its arguments. |
| 350 | static AttributeList get(LLVMContext &C, AttributeSet FnAttrs, |
| 351 | AttributeSet RetAttrs, |
| 352 | ArrayRef<AttributeSet> ArgAttrs); |
| 353 | |
| 354 | private: |
| 355 | explicit AttributeList(AttributeListImpl *LI) : pImpl(LI) {} |
| 356 | |
| 357 | static AttributeList getImpl(LLVMContext &C, ArrayRef<AttributeSet> AttrSets); |
| 358 | |
| 359 | public: |
| 360 | AttributeList() = default; |
| 361 | |
| 362 | //===--------------------------------------------------------------------===// |
| 363 | // AttributeList Construction and Mutation |
| 364 | //===--------------------------------------------------------------------===// |
| 365 | |
| 366 | /// \brief Return an AttributeList with the specified parameters in it. |
| 367 | static AttributeList get(LLVMContext &C, ArrayRef<AttributeList> Attrs); |
| 368 | static AttributeList get(LLVMContext &C, unsigned Index, |
| 369 | ArrayRef<Attribute::AttrKind> Kinds); |
| 370 | static AttributeList get(LLVMContext &C, unsigned Index, |
| 371 | ArrayRef<StringRef> Kind); |
| 372 | static AttributeList get(LLVMContext &C, unsigned Index, |
| 373 | const AttrBuilder &B); |
| 374 | |
| 375 | /// \brief Add an attribute to the attribute set at the given index. |
| 376 | /// Returns a new list because attribute lists are immutable. |
| 377 | AttributeList addAttribute(LLVMContext &C, unsigned Index, |
| 378 | Attribute::AttrKind Kind) const; |
| 379 | |
| 380 | /// \brief Add an attribute to the attribute set at the given index. |
| 381 | /// Returns a new list because attribute lists are immutable. |
| 382 | AttributeList addAttribute(LLVMContext &C, unsigned Index, StringRef Kind, |
| 383 | StringRef Value = StringRef()) const; |
| 384 | |
| 385 | /// Add an attribute to the attribute set at the given index. |
| 386 | /// Returns a new list because attribute lists are immutable. |
| 387 | AttributeList addAttribute(LLVMContext &C, unsigned Index, Attribute A) const; |
| 388 | |
| 389 | /// \brief Add attributes to the attribute set at the given index. |
| 390 | /// Returns a new list because attribute lists are immutable. |
| 391 | AttributeList addAttributes(LLVMContext &C, unsigned Index, |
| 392 | const AttrBuilder &B) const; |
| 393 | |
| 394 | /// Add an argument attribute to the list. Returns a new list because |
| 395 | /// attribute lists are immutable. |
| 396 | AttributeList addParamAttribute(LLVMContext &C, unsigned ArgNo, |
| 397 | Attribute::AttrKind Kind) const { |
| 398 | return addAttribute(C, ArgNo + FirstArgIndex, Kind); |
| 399 | } |
| 400 | |
| 401 | /// Add an argument attribute to the list. Returns a new list because |
| 402 | /// attribute lists are immutable. |
| 403 | AttributeList addParamAttribute(LLVMContext &C, unsigned ArgNo, |
| 404 | StringRef Kind, |
| 405 | StringRef Value = StringRef()) const { |
| 406 | return addAttribute(C, ArgNo + FirstArgIndex, Kind, Value); |
| 407 | } |
| 408 | |
| 409 | /// Add an attribute to the attribute list at the given arg indices. Returns a |
| 410 | /// new list because attribute lists are immutable. |
| 411 | AttributeList addParamAttribute(LLVMContext &C, ArrayRef<unsigned> ArgNos, |
| 412 | Attribute A) const; |
| 413 | |
| 414 | /// Add an argument attribute to the list. Returns a new list because |
| 415 | /// attribute lists are immutable. |
| 416 | AttributeList addParamAttributes(LLVMContext &C, unsigned ArgNo, |
| 417 | const AttrBuilder &B) const { |
| 418 | return addAttributes(C, ArgNo + FirstArgIndex, B); |
| 419 | } |
| 420 | |
| 421 | /// \brief Remove the specified attribute at the specified index from this |
| 422 | /// attribute list. Returns a new list because attribute lists are immutable. |
| 423 | AttributeList removeAttribute(LLVMContext &C, unsigned Index, |
| 424 | Attribute::AttrKind Kind) const; |
| 425 | |
| 426 | /// \brief Remove the specified attribute at the specified index from this |
| 427 | /// attribute list. Returns a new list because attribute lists are immutable. |
| 428 | AttributeList removeAttribute(LLVMContext &C, unsigned Index, |
| 429 | StringRef Kind) const; |
| 430 | |
| 431 | /// \brief Remove the specified attributes at the specified index from this |
| 432 | /// attribute list. Returns a new list because attribute lists are immutable. |
| 433 | AttributeList removeAttributes(LLVMContext &C, unsigned Index, |
| 434 | const AttrBuilder &AttrsToRemove) const; |
| 435 | |
| 436 | /// \brief Remove all attributes at the specified index from this |
| 437 | /// attribute list. Returns a new list because attribute lists are immutable. |
| 438 | AttributeList removeAttributes(LLVMContext &C, unsigned Index) const; |
| 439 | |
| 440 | /// \brief Remove the specified attribute at the specified arg index from this |
| 441 | /// attribute list. Returns a new list because attribute lists are immutable. |
| 442 | AttributeList removeParamAttribute(LLVMContext &C, unsigned ArgNo, |
| 443 | Attribute::AttrKind Kind) const { |
| 444 | return removeAttribute(C, ArgNo + FirstArgIndex, Kind); |
| 445 | } |
| 446 | |
| 447 | /// \brief Remove the specified attribute at the specified arg index from this |
| 448 | /// attribute list. Returns a new list because attribute lists are immutable. |
| 449 | AttributeList removeParamAttribute(LLVMContext &C, unsigned ArgNo, |
| 450 | StringRef Kind) const { |
| 451 | return removeAttribute(C, ArgNo + FirstArgIndex, Kind); |
| 452 | } |
| 453 | |
| 454 | /// \brief Remove the specified attribute at the specified arg index from this |
| 455 | /// attribute list. Returns a new list because attribute lists are immutable. |
| 456 | AttributeList removeParamAttributes(LLVMContext &C, unsigned ArgNo, |
| 457 | const AttrBuilder &AttrsToRemove) const { |
| 458 | return removeAttributes(C, ArgNo + FirstArgIndex, AttrsToRemove); |
| 459 | } |
| 460 | |
| 461 | /// \brief Remove all attributes at the specified arg index from this |
| 462 | /// attribute list. Returns a new list because attribute lists are immutable. |
| 463 | AttributeList removeParamAttributes(LLVMContext &C, unsigned ArgNo) const { |
| 464 | return removeAttributes(C, ArgNo + FirstArgIndex); |
| 465 | } |
| 466 | |
| 467 | /// \Brief Add the dereferenceable attribute to the attribute set at the given |
| 468 | /// index. Returns a new list because attribute lists are immutable. |
| 469 | AttributeList addDereferenceableAttr(LLVMContext &C, unsigned Index, |
| 470 | uint64_t Bytes) const; |
| 471 | |
| 472 | /// \Brief Add the dereferenceable attribute to the attribute set at the given |
| 473 | /// arg index. Returns a new list because attribute lists are immutable. |
| 474 | AttributeList addDereferenceableParamAttr(LLVMContext &C, unsigned ArgNo, |
| 475 | uint64_t Bytes) const { |
| 476 | return addDereferenceableAttr(C, ArgNo + FirstArgIndex, Bytes); |
| 477 | } |
| 478 | |
| 479 | /// \brief Add the dereferenceable_or_null attribute to the attribute set at |
| 480 | /// the given index. Returns a new list because attribute lists are immutable. |
| 481 | AttributeList addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index, |
| 482 | uint64_t Bytes) const; |
| 483 | |
| 484 | /// \brief Add the dereferenceable_or_null attribute to the attribute set at |
| 485 | /// the given arg index. Returns a new list because attribute lists are |
| 486 | /// immutable. |
| 487 | AttributeList addDereferenceableOrNullParamAttr(LLVMContext &C, |
| 488 | unsigned ArgNo, |
| 489 | uint64_t Bytes) const { |
| 490 | return addDereferenceableOrNullAttr(C, ArgNo + FirstArgIndex, Bytes); |
| 491 | } |
| 492 | |
| 493 | /// Add the allocsize attribute to the attribute set at the given index. |
| 494 | /// Returns a new list because attribute lists are immutable. |
| 495 | AttributeList addAllocSizeAttr(LLVMContext &C, unsigned Index, |
| 496 | unsigned ElemSizeArg, |
| 497 | const Optional<unsigned> &NumElemsArg); |
| 498 | |
| 499 | /// Add the allocsize attribute to the attribute set at the given arg index. |
| 500 | /// Returns a new list because attribute lists are immutable. |
| 501 | AttributeList addAllocSizeParamAttr(LLVMContext &C, unsigned ArgNo, |
| 502 | unsigned ElemSizeArg, |
| 503 | const Optional<unsigned> &NumElemsArg) { |
| 504 | return addAllocSizeAttr(C, ArgNo + FirstArgIndex, ElemSizeArg, NumElemsArg); |
| 505 | } |
| 506 | |
| 507 | //===--------------------------------------------------------------------===// |
| 508 | // AttributeList Accessors |
| 509 | //===--------------------------------------------------------------------===// |
| 510 | |
| 511 | /// \brief Retrieve the LLVM context. |
| 512 | LLVMContext &getContext() const; |
| 513 | |
| 514 | /// \brief The attributes for the specified index are returned. |
| 515 | AttributeSet getAttributes(unsigned Index) const; |
| 516 | |
| 517 | /// \brief The attributes for the argument or parameter at the given index are |
| 518 | /// returned. |
| 519 | AttributeSet getParamAttributes(unsigned ArgNo) const; |
| 520 | |
| 521 | /// \brief The attributes for the ret value are returned. |
| 522 | AttributeSet getRetAttributes() const; |
| 523 | |
| 524 | /// \brief The function attributes are returned. |
| 525 | AttributeSet getFnAttributes() const; |
| 526 | |
| 527 | /// \brief Return true if the attribute exists at the given index. |
| 528 | bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const; |
| 529 | |
| 530 | /// \brief Return true if the attribute exists at the given index. |
| 531 | bool hasAttribute(unsigned Index, StringRef Kind) const; |
| 532 | |
| 533 | /// \brief Return true if attribute exists at the given index. |
| 534 | bool hasAttributes(unsigned Index) const; |
| 535 | |
| 536 | /// \brief Return true if the attribute exists for the given argument |
| 537 | bool hasParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const { |
| 538 | return hasAttribute(ArgNo + FirstArgIndex, Kind); |
| 539 | } |
| 540 | |
| 541 | /// \brief Return true if the attribute exists for the given argument |
| 542 | bool hasParamAttr(unsigned ArgNo, StringRef Kind) const { |
| 543 | return hasAttribute(ArgNo + FirstArgIndex, Kind); |
| 544 | } |
| 545 | |
| 546 | /// \brief Return true if attributes exists for the given argument |
| 547 | bool hasParamAttrs(unsigned ArgNo) const { |
| 548 | return hasAttributes(ArgNo + FirstArgIndex); |
| 549 | } |
| 550 | |
| 551 | /// \brief Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but |
| 552 | /// may be faster. |
| 553 | bool hasFnAttribute(Attribute::AttrKind Kind) const; |
| 554 | |
| 555 | /// \brief Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but |
| 556 | /// may be faster. |
| 557 | bool hasFnAttribute(StringRef Kind) const; |
| 558 | |
| 559 | /// \brief Equivalent to hasAttribute(ArgNo + FirstArgIndex, Kind). |
| 560 | bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const; |
| 561 | |
| 562 | /// \brief Return true if the specified attribute is set for at least one |
| 563 | /// parameter or for the return value. If Index is not nullptr, the index |
| 564 | /// of a parameter with the specified attribute is provided. |
| 565 | bool hasAttrSomewhere(Attribute::AttrKind Kind, |
| 566 | unsigned *Index = nullptr) const; |
| 567 | |
| 568 | /// \brief Return the attribute object that exists at the given index. |
| 569 | Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const; |
| 570 | |
| 571 | /// \brief Return the attribute object that exists at the given index. |
| 572 | Attribute getAttribute(unsigned Index, StringRef Kind) const; |
| 573 | |
| 574 | /// \brief Return the attribute object that exists at the arg index. |
| 575 | Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const { |
| 576 | return getAttribute(ArgNo + FirstArgIndex, Kind); |
| 577 | } |
| 578 | |
| 579 | /// \brief Return the attribute object that exists at the given index. |
| 580 | Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const { |
| 581 | return getAttribute(ArgNo + FirstArgIndex, Kind); |
| 582 | } |
| 583 | |
| 584 | /// \brief Return the alignment of the return value. |
| 585 | unsigned getRetAlignment() const; |
| 586 | |
| 587 | /// \brief Return the alignment for the specified function parameter. |
| 588 | unsigned getParamAlignment(unsigned ArgNo) const; |
| 589 | |
| 590 | /// \brief Get the stack alignment. |
| 591 | unsigned getStackAlignment(unsigned Index) const; |
| 592 | |
| 593 | /// \brief Get the number of dereferenceable bytes (or zero if unknown). |
| 594 | uint64_t getDereferenceableBytes(unsigned Index) const; |
| 595 | |
| 596 | /// \brief Get the number of dereferenceable bytes (or zero if unknown) of an |
| 597 | /// arg. |
| 598 | uint64_t getParamDereferenceableBytes(unsigned ArgNo) const { |
| 599 | return getDereferenceableBytes(ArgNo + FirstArgIndex); |
| 600 | } |
| 601 | |
| 602 | /// \brief Get the number of dereferenceable_or_null bytes (or zero if |
| 603 | /// unknown). |
| 604 | uint64_t getDereferenceableOrNullBytes(unsigned Index) const; |
| 605 | |
| 606 | /// \brief Get the number of dereferenceable_or_null bytes (or zero if |
| 607 | /// unknown) of an arg. |
| 608 | uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo) const { |
| 609 | return getDereferenceableOrNullBytes(ArgNo + FirstArgIndex); |
| 610 | } |
| 611 | |
| 612 | /// Get the allocsize argument numbers (or pair(0, 0) if unknown). |
| 613 | std::pair<unsigned, Optional<unsigned>> |
| 614 | getAllocSizeArgs(unsigned Index) const; |
| 615 | |
| 616 | /// \brief Return the attributes at the index as a string. |
| 617 | std::string getAsString(unsigned Index, bool InAttrGrp = false) const; |
| 618 | |
| 619 | //===--------------------------------------------------------------------===// |
| 620 | // AttributeList Introspection |
| 621 | //===--------------------------------------------------------------------===// |
| 622 | |
| 623 | using iterator = const AttributeSet *; |
| 624 | |
| 625 | iterator begin() const; |
| 626 | iterator end() const; |
| 627 | |
| 628 | unsigned getNumAttrSets() const; |
| 629 | |
| 630 | /// Use these to iterate over the valid attribute indices. |
| 631 | unsigned index_begin() const { return AttributeList::FunctionIndex; } |
| 632 | unsigned index_end() const { return getNumAttrSets() - 1; } |
| 633 | |
| 634 | /// operator==/!= - Provide equality predicates. |
| 635 | bool operator==(const AttributeList &RHS) const { return pImpl == RHS.pImpl; } |
| 636 | bool operator!=(const AttributeList &RHS) const { return pImpl != RHS.pImpl; } |
| 637 | |
| 638 | /// \brief Return a raw pointer that uniquely identifies this attribute list. |
| 639 | void *getRawPointer() const { |
| 640 | return pImpl; |
| 641 | } |
| 642 | |
| 643 | /// \brief Return true if there are no attributes. |
| 644 | bool isEmpty() const { return pImpl == nullptr; } |
| 645 | |
| 646 | void dump() const; |
| 647 | }; |
| 648 | |
| 649 | //===----------------------------------------------------------------------===// |
| 650 | /// \class |
| 651 | /// \brief Provide DenseMapInfo for AttributeList. |
| 652 | template <> struct DenseMapInfo<AttributeList> { |
| 653 | static AttributeList getEmptyKey() { |
| 654 | auto Val = static_cast<uintptr_t>(-1); |
| 655 | Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable; |
| 656 | return AttributeList(reinterpret_cast<AttributeListImpl *>(Val)); |
| 657 | } |
| 658 | |
| 659 | static AttributeList getTombstoneKey() { |
| 660 | auto Val = static_cast<uintptr_t>(-2); |
| 661 | Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable; |
| 662 | return AttributeList(reinterpret_cast<AttributeListImpl *>(Val)); |
| 663 | } |
| 664 | |
| 665 | static unsigned getHashValue(AttributeList AS) { |
| 666 | return (unsigned((uintptr_t)AS.pImpl) >> 4) ^ |
| 667 | (unsigned((uintptr_t)AS.pImpl) >> 9); |
| 668 | } |
| 669 | |
| 670 | static bool isEqual(AttributeList LHS, AttributeList RHS) { |
| 671 | return LHS == RHS; |
| 672 | } |
| 673 | }; |
| 674 | |
| 675 | //===----------------------------------------------------------------------===// |
| 676 | /// \class |
| 677 | /// \brief This class is used in conjunction with the Attribute::get method to |
| 678 | /// create an Attribute object. The object itself is uniquified. The Builder's |
| 679 | /// value, however, is not. So this can be used as a quick way to test for |
| 680 | /// equality, presence of attributes, etc. |
| 681 | class AttrBuilder { |
| 682 | std::bitset<Attribute::EndAttrKinds> Attrs; |
| 683 | std::map<std::string, std::string> TargetDepAttrs; |
| 684 | uint64_t Alignment = 0; |
| 685 | uint64_t StackAlignment = 0; |
| 686 | uint64_t DerefBytes = 0; |
| 687 | uint64_t DerefOrNullBytes = 0; |
| 688 | uint64_t AllocSizeArgs = 0; |
| 689 | |
| 690 | public: |
| 691 | AttrBuilder() = default; |
| 692 | |
| 693 | AttrBuilder(const Attribute &A) { |
| 694 | addAttribute(A); |
| 695 | } |
| 696 | |
| 697 | AttrBuilder(AttributeList AS, unsigned Idx); |
| 698 | AttrBuilder(AttributeSet AS); |
| 699 | |
| 700 | void clear(); |
| 701 | |
| 702 | /// \brief Add an attribute to the builder. |
| 703 | AttrBuilder &addAttribute(Attribute::AttrKind Val); |
| 704 | |
| 705 | /// \brief Add the Attribute object to the builder. |
| 706 | AttrBuilder &addAttribute(Attribute A); |
| 707 | |
| 708 | /// \brief Add the target-dependent attribute to the builder. |
| 709 | AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef()); |
| 710 | |
| 711 | /// \brief Remove an attribute from the builder. |
| 712 | AttrBuilder &removeAttribute(Attribute::AttrKind Val); |
| 713 | |
| 714 | /// \brief Remove the attributes from the builder. |
| 715 | AttrBuilder &removeAttributes(AttributeList A, uint64_t WithoutIndex); |
| 716 | |
| 717 | /// \brief Remove the target-dependent attribute to the builder. |
| 718 | AttrBuilder &removeAttribute(StringRef A); |
| 719 | |
| 720 | /// \brief Add the attributes from the builder. |
| 721 | AttrBuilder &merge(const AttrBuilder &B); |
| 722 | |
| 723 | /// \brief Remove the attributes from the builder. |
| 724 | AttrBuilder &remove(const AttrBuilder &B); |
| 725 | |
| 726 | /// \brief Return true if the builder has any attribute that's in the |
| 727 | /// specified builder. |
| 728 | bool overlaps(const AttrBuilder &B) const; |
| 729 | |
| 730 | /// \brief Return true if the builder has the specified attribute. |
| 731 | bool contains(Attribute::AttrKind A) const { |
| 732 | assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!"); |
| 733 | return Attrs[A]; |
| 734 | } |
| 735 | |
| 736 | /// \brief Return true if the builder has the specified target-dependent |
| 737 | /// attribute. |
| 738 | bool contains(StringRef A) const; |
| 739 | |
| 740 | /// \brief Return true if the builder has IR-level attributes. |
| 741 | bool hasAttributes() const; |
| 742 | |
| 743 | /// \brief Return true if the builder has any attribute that's in the |
| 744 | /// specified attribute. |
| 745 | bool hasAttributes(AttributeList A, uint64_t Index) const; |
| 746 | |
| 747 | /// \brief Return true if the builder has an alignment attribute. |
| 748 | bool hasAlignmentAttr() const; |
| 749 | |
| 750 | /// \brief Retrieve the alignment attribute, if it exists. |
| 751 | uint64_t getAlignment() const { return Alignment; } |
| 752 | |
| 753 | /// \brief Retrieve the stack alignment attribute, if it exists. |
| 754 | uint64_t getStackAlignment() const { return StackAlignment; } |
| 755 | |
| 756 | /// \brief Retrieve the number of dereferenceable bytes, if the |
| 757 | /// dereferenceable attribute exists (zero is returned otherwise). |
| 758 | uint64_t getDereferenceableBytes() const { return DerefBytes; } |
| 759 | |
| 760 | /// \brief Retrieve the number of dereferenceable_or_null bytes, if the |
| 761 | /// dereferenceable_or_null attribute exists (zero is returned otherwise). |
| 762 | uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; } |
| 763 | |
| 764 | /// Retrieve the allocsize args, if the allocsize attribute exists. If it |
| 765 | /// doesn't exist, pair(0, 0) is returned. |
| 766 | std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const; |
| 767 | |
| 768 | /// \brief This turns an int alignment (which must be a power of 2) into the |
| 769 | /// form used internally in Attribute. |
| 770 | AttrBuilder &addAlignmentAttr(unsigned Align); |
| 771 | |
| 772 | /// \brief This turns an int stack alignment (which must be a power of 2) into |
| 773 | /// the form used internally in Attribute. |
| 774 | AttrBuilder &addStackAlignmentAttr(unsigned Align); |
| 775 | |
| 776 | /// \brief This turns the number of dereferenceable bytes into the form used |
| 777 | /// internally in Attribute. |
| 778 | AttrBuilder &addDereferenceableAttr(uint64_t Bytes); |
| 779 | |
| 780 | /// \brief This turns the number of dereferenceable_or_null bytes into the |
| 781 | /// form used internally in Attribute. |
| 782 | AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes); |
| 783 | |
| 784 | /// This turns one (or two) ints into the form used internally in Attribute. |
| 785 | AttrBuilder &addAllocSizeAttr(unsigned ElemSizeArg, |
| 786 | const Optional<unsigned> &NumElemsArg); |
| 787 | |
| 788 | /// Add an allocsize attribute, using the representation returned by |
| 789 | /// Attribute.getIntValue(). |
| 790 | AttrBuilder &addAllocSizeAttrFromRawRepr(uint64_t RawAllocSizeRepr); |
| 791 | |
| 792 | /// \brief Return true if the builder contains no target-independent |
| 793 | /// attributes. |
| 794 | bool empty() const { return Attrs.none(); } |
| 795 | |
| 796 | // Iterators for target-dependent attributes. |
| 797 | using td_type = std::pair<std::string, std::string>; |
| 798 | using td_iterator = std::map<std::string, std::string>::iterator; |
| 799 | using td_const_iterator = std::map<std::string, std::string>::const_iterator; |
| 800 | using td_range = iterator_range<td_iterator>; |
| 801 | using td_const_range = iterator_range<td_const_iterator>; |
| 802 | |
| 803 | td_iterator td_begin() { return TargetDepAttrs.begin(); } |
| 804 | td_iterator td_end() { return TargetDepAttrs.end(); } |
| 805 | |
| 806 | td_const_iterator td_begin() const { return TargetDepAttrs.begin(); } |
| 807 | td_const_iterator td_end() const { return TargetDepAttrs.end(); } |
| 808 | |
| 809 | td_range td_attrs() { return td_range(td_begin(), td_end()); } |
| 810 | |
| 811 | td_const_range td_attrs() const { |
| 812 | return td_const_range(td_begin(), td_end()); |
| 813 | } |
| 814 | |
| 815 | bool td_empty() const { return TargetDepAttrs.empty(); } |
| 816 | |
| 817 | bool operator==(const AttrBuilder &B); |
| 818 | bool operator!=(const AttrBuilder &B) { |
| 819 | return !(*this == B); |
| 820 | } |
| 821 | }; |
| 822 | |
| 823 | namespace AttributeFuncs { |
| 824 | |
| 825 | /// \brief Which attributes cannot be applied to a type. |
| 826 | AttrBuilder typeIncompatible(Type *Ty); |
| 827 | |
| 828 | /// \returns Return true if the two functions have compatible target-independent |
| 829 | /// attributes for inlining purposes. |
| 830 | bool areInlineCompatible(const Function &Caller, const Function &Callee); |
| 831 | |
| 832 | /// \brief Merge caller's and callee's attributes. |
| 833 | void mergeAttributesForInlining(Function &Caller, const Function &Callee); |
| 834 | |
| 835 | } // end namespace AttributeFuncs |
| 836 | |
| 837 | } // end namespace llvm |
| 838 | |
| 839 | #endif // LLVM_IR_ATTRIBUTES_H |