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