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