Bill Wendling | 4607f4b | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 1 | //===-- AttributeImpl.h - Attribute Internals -------------------*- C++ -*-===// |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Bill Wendling | 66e978f | 2012-12-20 21:28:43 +0000 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
| 11 | /// \brief This file defines various helper methods and classes used by |
| 12 | /// LLVMContextImpl for creating and managing attributes. |
| 13 | /// |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 16 | #ifndef LLVM_LIB_IR_ATTRIBUTEIMPL_H |
| 17 | #define LLVM_LIB_IR_ATTRIBUTEIMPL_H |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 18 | |
| 19 | #include "llvm/ADT/FoldingSet.h" |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame^] | 20 | #include "llvm/ADT/Optional.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Attributes.h" |
Matthias Braun | daa812d | 2016-01-30 01:14:01 +0000 | [diff] [blame] | 22 | #include "llvm/Support/DataTypes.h" |
James Y Knight | aa365b2 | 2015-08-05 22:57:34 +0000 | [diff] [blame] | 23 | #include "llvm/Support/TrailingObjects.h" |
Matthias Braun | d520d4e | 2016-01-29 22:30:30 +0000 | [diff] [blame] | 24 | #include <climits> |
Matthias Braun | 9c98105 | 2016-01-29 22:35:29 +0000 | [diff] [blame] | 25 | #include <string> |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 26 | |
| 27 | namespace llvm { |
| 28 | |
Bill Wendling | 0cd0f7f | 2012-12-29 12:29:38 +0000 | [diff] [blame] | 29 | class Constant; |
Bill Wendling | 6ad6c3b | 2012-12-19 23:55:43 +0000 | [diff] [blame] | 30 | class LLVMContext; |
| 31 | |
Bill Wendling | 66e978f | 2012-12-20 21:28:43 +0000 | [diff] [blame] | 32 | //===----------------------------------------------------------------------===// |
| 33 | /// \class |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 34 | /// \brief This class represents a single, uniqued attribute. That attribute |
| 35 | /// could be a single enum, a tuple, or a string. |
Benjamin Kramer | 079b96e | 2013-09-11 18:05:11 +0000 | [diff] [blame] | 36 | class AttributeImpl : public FoldingSetNode { |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 37 | unsigned char KindID; ///< Holds the AttrEntryKind of the attribute |
| 38 | |
| 39 | // AttributesImpl is uniqued, these should not be publicly available. |
Aaron Ballman | f9a1897 | 2015-02-15 22:54:22 +0000 | [diff] [blame] | 40 | void operator=(const AttributeImpl &) = delete; |
| 41 | AttributeImpl(const AttributeImpl &) = delete; |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 42 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 43 | protected: |
| 44 | enum AttrEntryKind { |
| 45 | EnumAttrEntry, |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 46 | IntAttrEntry, |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 47 | StringAttrEntry |
| 48 | }; |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 49 | |
| 50 | AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {} |
| 51 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 52 | public: |
Alexey Samsonov | 49109a2 | 2013-11-18 09:31:53 +0000 | [diff] [blame] | 53 | virtual ~AttributeImpl(); |
| 54 | |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 55 | bool isEnumAttribute() const { return KindID == EnumAttrEntry; } |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 56 | bool isIntAttribute() const { return KindID == IntAttrEntry; } |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 57 | bool isStringAttribute() const { return KindID == StringAttrEntry; } |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 58 | |
Bill Wendling | 9ac69f9 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 59 | bool hasAttribute(Attribute::AttrKind A) const; |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 60 | bool hasAttribute(StringRef Kind) const; |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 61 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 62 | Attribute::AttrKind getKindAsEnum() const; |
| 63 | uint64_t getValueAsInt() const; |
Bill Wendling | c3c714b | 2013-01-29 20:37:10 +0000 | [diff] [blame] | 64 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 65 | StringRef getKindAsString() const; |
| 66 | StringRef getValueAsString() const; |
Bill Wendling | b1d1261 | 2012-12-30 01:38:39 +0000 | [diff] [blame] | 67 | |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 68 | /// \brief Used when sorting the attributes. |
Bill Wendling | d2e493b | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 69 | bool operator<(const AttributeImpl &AI) const; |
| 70 | |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 71 | void Profile(FoldingSetNodeID &ID) const { |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 72 | if (isEnumAttribute()) |
| 73 | Profile(ID, getKindAsEnum(), 0); |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 74 | else if (isIntAttribute()) |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 75 | Profile(ID, getKindAsEnum(), getValueAsInt()); |
| 76 | else |
| 77 | Profile(ID, getKindAsString(), getValueAsString()); |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 78 | } |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 79 | static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, |
| 80 | uint64_t Val) { |
| 81 | ID.AddInteger(Kind); |
| 82 | if (Val) ID.AddInteger(Val); |
| 83 | } |
| 84 | static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) { |
| 85 | ID.AddString(Kind); |
Bill Wendling | 8a0e084 | 2013-02-28 21:17:03 +0000 | [diff] [blame] | 86 | if (!Values.empty()) ID.AddString(Values); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Bill Wendling | 3f9fcd4 | 2013-02-01 00:48:14 +0000 | [diff] [blame] | 89 | // FIXME: Remove this! |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 90 | static uint64_t getAttrMask(Attribute::AttrKind Val); |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
Bill Wendling | 66e978f | 2012-12-20 21:28:43 +0000 | [diff] [blame] | 93 | //===----------------------------------------------------------------------===// |
| 94 | /// \class |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 95 | /// \brief A set of classes that contain the value of the |
| 96 | /// attribute object. There are three main categories: enum attribute entries, |
| 97 | /// represented by Attribute::AttrKind; alignment attribute entries; and string |
| 98 | /// attribute enties, which are for target-dependent attributes. |
| 99 | |
Benjamin Kramer | 079b96e | 2013-09-11 18:05:11 +0000 | [diff] [blame] | 100 | class EnumAttributeImpl : public AttributeImpl { |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 101 | virtual void anchor(); |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 102 | Attribute::AttrKind Kind; |
| 103 | |
| 104 | protected: |
| 105 | EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind) |
| 106 | : AttributeImpl(ID), Kind(Kind) {} |
| 107 | |
| 108 | public: |
| 109 | EnumAttributeImpl(Attribute::AttrKind Kind) |
| 110 | : AttributeImpl(EnumAttrEntry), Kind(Kind) {} |
| 111 | |
| 112 | Attribute::AttrKind getEnumKind() const { return Kind; } |
| 113 | }; |
| 114 | |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 115 | class IntAttributeImpl : public EnumAttributeImpl { |
Craig Topper | f398d7c | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 116 | void anchor() override; |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 117 | uint64_t Val; |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 118 | |
| 119 | public: |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 120 | IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val) |
| 121 | : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) { |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 122 | assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment || |
| 123 | Kind == Attribute::Dereferenceable || |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame^] | 124 | Kind == Attribute::DereferenceableOrNull || |
| 125 | Kind == Attribute::AllocSize) && |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 126 | "Wrong kind for int attribute!"); |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 129 | uint64_t getValue() const { return Val; } |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
Benjamin Kramer | 079b96e | 2013-09-11 18:05:11 +0000 | [diff] [blame] | 132 | class StringAttributeImpl : public AttributeImpl { |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 133 | virtual void anchor(); |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 134 | std::string Kind; |
| 135 | std::string Val; |
| 136 | |
| 137 | public: |
| 138 | StringAttributeImpl(StringRef Kind, StringRef Val = StringRef()) |
| 139 | : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {} |
| 140 | |
| 141 | StringRef getStringKind() const { return Kind; } |
| 142 | StringRef getStringValue() const { return Val; } |
| 143 | }; |
| 144 | |
| 145 | //===----------------------------------------------------------------------===// |
| 146 | /// \class |
Bill Wendling | d2e493b | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 147 | /// \brief This class represents a group of attributes that apply to one |
| 148 | /// element: function, return type, or parameter. |
James Y Knight | aa365b2 | 2015-08-05 22:57:34 +0000 | [diff] [blame] | 149 | class AttributeSetNode final |
| 150 | : public FoldingSetNode, |
| 151 | private TrailingObjects<AttributeSetNode, Attribute> { |
| 152 | friend TrailingObjects; |
| 153 | |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 154 | unsigned NumAttrs; ///< Number of attributes in this node. |
Matthias Braun | 31eeb76f | 2016-01-29 22:25:13 +0000 | [diff] [blame] | 155 | /// Bitset with a bit for each available attribute Attribute::AttrKind. |
| 156 | uint64_t AvailableAttrs; |
Bill Wendling | d2e493b | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 157 | |
Matthias Braun | 31eeb76f | 2016-01-29 22:25:13 +0000 | [diff] [blame] | 158 | AttributeSetNode(ArrayRef<Attribute> Attrs) |
| 159 | : NumAttrs(Attrs.size()), AvailableAttrs(0) { |
Reid Kleckner | b046154 | 2016-01-29 22:40:22 +0000 | [diff] [blame] | 160 | static_assert(Attribute::EndAttrKinds <= sizeof(AvailableAttrs) * CHAR_BIT, |
| 161 | "Too many attributes for AvailableAttrs"); |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 162 | // There's memory after the node where we can store the entries in. |
James Y Knight | aa365b2 | 2015-08-05 22:57:34 +0000 | [diff] [blame] | 163 | std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<Attribute>()); |
Matthias Braun | 31eeb76f | 2016-01-29 22:25:13 +0000 | [diff] [blame] | 164 | |
| 165 | for (iterator I = begin(), E = end(); I != E; ++I) { |
| 166 | if (!I->isStringAttribute()) { |
| 167 | AvailableAttrs |= ((uint64_t)1) << I->getKindAsEnum(); |
| 168 | } |
| 169 | } |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 170 | } |
Bill Wendling | 97b4f70 | 2013-01-27 21:38:03 +0000 | [diff] [blame] | 171 | |
| 172 | // AttributesSetNode is uniqued, these should not be publicly available. |
Aaron Ballman | f9a1897 | 2015-02-15 22:54:22 +0000 | [diff] [blame] | 173 | void operator=(const AttributeSetNode &) = delete; |
| 174 | AttributeSetNode(const AttributeSetNode &) = delete; |
Bill Wendling | d2e493b | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 175 | public: |
Richard Smith | a64e1ad | 2016-02-09 02:09:16 +0000 | [diff] [blame] | 176 | void operator delete(void *p) { ::operator delete(p); } |
Richard Smith | 1b65c32 | 2016-02-09 01:03:42 +0000 | [diff] [blame] | 177 | |
Bill Wendling | d2e493b | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 178 | static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs); |
| 179 | |
Matthias Braun | 31eeb76f | 2016-01-29 22:25:13 +0000 | [diff] [blame] | 180 | bool hasAttribute(Attribute::AttrKind Kind) const { |
| 181 | return AvailableAttrs & ((uint64_t)1) << Kind; |
| 182 | } |
Bill Wendling | bce7b97 | 2013-02-13 08:42:21 +0000 | [diff] [blame] | 183 | bool hasAttribute(StringRef Kind) const; |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 184 | bool hasAttributes() const { return NumAttrs != 0; } |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 185 | |
Bill Wendling | bce7b97 | 2013-02-13 08:42:21 +0000 | [diff] [blame] | 186 | Attribute getAttribute(Attribute::AttrKind Kind) const; |
| 187 | Attribute getAttribute(StringRef Kind) const; |
| 188 | |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 189 | unsigned getAlignment() const; |
| 190 | unsigned getStackAlignment() const; |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 191 | uint64_t getDereferenceableBytes() const; |
Sanjoy Das | 06cf33f | 2015-05-06 17:41:54 +0000 | [diff] [blame] | 192 | uint64_t getDereferenceableOrNullBytes() const; |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame^] | 193 | std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const; |
Rafael Espindola | cbf5a7a | 2013-05-01 13:07:03 +0000 | [diff] [blame] | 194 | std::string getAsString(bool InAttrGrp) const; |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 195 | |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 196 | typedef const Attribute *iterator; |
James Y Knight | aa365b2 | 2015-08-05 22:57:34 +0000 | [diff] [blame] | 197 | iterator begin() const { return getTrailingObjects<Attribute>(); } |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 198 | iterator end() const { return begin() + NumAttrs; } |
Bill Wendling | d2e493b | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 199 | |
| 200 | void Profile(FoldingSetNodeID &ID) const { |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 201 | Profile(ID, makeArrayRef(begin(), end())); |
Bill Wendling | d2e493b | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 202 | } |
| 203 | static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) { |
| 204 | for (unsigned I = 0, E = AttrList.size(); I != E; ++I) |
| 205 | AttrList[I].Profile(ID); |
| 206 | } |
| 207 | }; |
| 208 | |
NAKAMURA Takumi | 51fe119 | 2015-08-06 09:49:17 +0000 | [diff] [blame] | 209 | typedef std::pair<unsigned, AttributeSetNode *> IndexAttrPair; |
| 210 | |
Bill Wendling | d2e493b | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 211 | //===----------------------------------------------------------------------===// |
| 212 | /// \class |
| 213 | /// \brief This class represents a set of attributes that apply to the function, |
| 214 | /// return type, and parameters. |
James Y Knight | aa365b2 | 2015-08-05 22:57:34 +0000 | [diff] [blame] | 215 | class AttributeSetImpl final |
| 216 | : public FoldingSetNode, |
| 217 | private TrailingObjects<AttributeSetImpl, IndexAttrPair> { |
| 218 | friend class AttributeSet; |
| 219 | friend TrailingObjects; |
James Y Knight | 8096d34 | 2015-06-17 01:21:20 +0000 | [diff] [blame] | 220 | |
| 221 | private: |
| 222 | LLVMContext &Context; |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 223 | unsigned NumAttrs; ///< Number of entries in this set. |
Matthias Braun | 3328281 | 2016-01-29 22:25:19 +0000 | [diff] [blame] | 224 | /// Bitset with a bit for each available attribute Attribute::AttrKind. |
| 225 | uint64_t AvailableFunctionAttrs; |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 226 | |
James Y Knight | aa365b2 | 2015-08-05 22:57:34 +0000 | [diff] [blame] | 227 | // Helper fn for TrailingObjects class. |
| 228 | size_t numTrailingObjects(OverloadToken<IndexAttrPair>) { return NumAttrs; } |
| 229 | |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 230 | /// \brief Return a pointer to the IndexAttrPair for the specified slot. |
| 231 | const IndexAttrPair *getNode(unsigned Slot) const { |
James Y Knight | aa365b2 | 2015-08-05 22:57:34 +0000 | [diff] [blame] | 232 | return getTrailingObjects<IndexAttrPair>() + Slot; |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 233 | } |
Bill Wendling | 39a4c80 | 2013-01-24 01:01:34 +0000 | [diff] [blame] | 234 | |
Bill Wendling | 698e84f | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 235 | // AttributesSet is uniqued, these should not be publicly available. |
Aaron Ballman | f9a1897 | 2015-02-15 22:54:22 +0000 | [diff] [blame] | 236 | void operator=(const AttributeSetImpl &) = delete; |
| 237 | AttributeSetImpl(const AttributeSetImpl &) = delete; |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 238 | public: |
Bill Wendling | ec45454 | 2013-01-28 21:55:20 +0000 | [diff] [blame] | 239 | AttributeSetImpl(LLVMContext &C, |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 240 | ArrayRef<std::pair<unsigned, AttributeSetNode *> > Attrs) |
Matthias Braun | 3328281 | 2016-01-29 22:25:19 +0000 | [diff] [blame] | 241 | : Context(C), NumAttrs(Attrs.size()), AvailableFunctionAttrs(0) { |
Reid Kleckner | b046154 | 2016-01-29 22:40:22 +0000 | [diff] [blame] | 242 | static_assert(Attribute::EndAttrKinds <= |
| 243 | sizeof(AvailableFunctionAttrs) * CHAR_BIT, |
| 244 | "Too many attributes"); |
James Y Knight | 8096d34 | 2015-06-17 01:21:20 +0000 | [diff] [blame] | 245 | |
Peter Collingbourne | bd6c745 | 2013-08-02 22:29:40 +0000 | [diff] [blame] | 246 | #ifndef NDEBUG |
| 247 | if (Attrs.size() >= 2) { |
| 248 | for (const std::pair<unsigned, AttributeSetNode *> *i = Attrs.begin() + 1, |
| 249 | *e = Attrs.end(); |
| 250 | i != e; ++i) { |
| 251 | assert((i-1)->first <= i->first && "Attribute set not ordered!"); |
| 252 | } |
| 253 | } |
| 254 | #endif |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 255 | // There's memory after the node where we can store the entries in. |
James Y Knight | aa365b2 | 2015-08-05 22:57:34 +0000 | [diff] [blame] | 256 | std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<IndexAttrPair>()); |
Matthias Braun | 3328281 | 2016-01-29 22:25:19 +0000 | [diff] [blame] | 257 | |
| 258 | // Initialize AvailableFunctionAttrs summary bitset. |
| 259 | if (NumAttrs > 0) { |
| 260 | static_assert(AttributeSet::FunctionIndex == ~0u, |
| 261 | "FunctionIndex should be biggest possible index"); |
| 262 | const std::pair<unsigned, AttributeSetNode *> &Last = Attrs.back(); |
| 263 | if (Last.first == AttributeSet::FunctionIndex) { |
| 264 | const AttributeSetNode *Node = Last.second; |
| 265 | for (AttributeSetNode::iterator I = Node->begin(), E = Node->end(); |
| 266 | I != E; ++I) { |
| 267 | if (!I->isStringAttribute()) |
| 268 | AvailableFunctionAttrs |= ((uint64_t)1) << I->getKindAsEnum(); |
| 269 | } |
| 270 | } |
| 271 | } |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 272 | } |
Bill Wendling | 9ac69f9 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 273 | |
Richard Smith | a64e1ad | 2016-02-09 02:09:16 +0000 | [diff] [blame] | 274 | void operator delete(void *p) { ::operator delete(p); } |
Richard Smith | 1b65c32 | 2016-02-09 01:03:42 +0000 | [diff] [blame] | 275 | |
Bill Wendling | 5c8b2df | 2013-01-27 21:32:11 +0000 | [diff] [blame] | 276 | /// \brief Get the context that created this AttributeSetImpl. |
Bill Wendling | 9ac69f9 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 277 | LLVMContext &getContext() { return Context; } |
Bill Wendling | 5c8b2df | 2013-01-27 21:32:11 +0000 | [diff] [blame] | 278 | |
Bill Wendling | 5c8b2df | 2013-01-27 21:32:11 +0000 | [diff] [blame] | 279 | /// \brief Return the number of attributes this AttributeSet contains. |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 280 | unsigned getNumAttributes() const { return NumAttrs; } |
Bill Wendling | 5c8b2df | 2013-01-27 21:32:11 +0000 | [diff] [blame] | 281 | |
| 282 | /// \brief Get the index of the given "slot" in the AttrNodes list. This index |
| 283 | /// is the index of the return, parameter, or function object that the |
| 284 | /// attributes are applied to, not the index into the AttrNodes list where the |
| 285 | /// attributes reside. |
Rafael Espindola | dd27530 | 2013-04-30 16:53:38 +0000 | [diff] [blame] | 286 | unsigned getSlotIndex(unsigned Slot) const { |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 287 | return getNode(Slot)->first; |
Bill Wendling | 9eb689c | 2013-01-28 00:21:34 +0000 | [diff] [blame] | 288 | } |
Bill Wendling | 5c8b2df | 2013-01-27 21:32:11 +0000 | [diff] [blame] | 289 | |
| 290 | /// \brief Retrieve the attributes for the given "slot" in the AttrNode list. |
| 291 | /// \p Slot is an index into the AttrNodes list, not the index of the return / |
| 292 | /// parameter/ function which the attributes apply to. |
Bill Wendling | 57625a4 | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 293 | AttributeSet getSlotAttributes(unsigned Slot) const { |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 294 | return AttributeSet::get(Context, *getNode(Slot)); |
Bill Wendling | 57625a4 | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 295 | } |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 296 | |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 297 | /// \brief Retrieve the attribute set node for the given "slot" in the |
| 298 | /// AttrNode list. |
| 299 | AttributeSetNode *getSlotNode(unsigned Slot) const { |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 300 | return getNode(Slot)->second; |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Matthias Braun | 3328281 | 2016-01-29 22:25:19 +0000 | [diff] [blame] | 303 | /// \brief Return true if the AttributeSetNode for the FunctionIndex has an |
| 304 | /// enum attribute of the given kind. |
| 305 | bool hasFnAttribute(Attribute::AttrKind Kind) const { |
| 306 | return AvailableFunctionAttrs & ((uint64_t)1) << Kind; |
| 307 | } |
| 308 | |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 309 | typedef AttributeSetNode::iterator iterator; |
| 310 | iterator begin(unsigned Slot) const { return getSlotNode(Slot)->begin(); } |
| 311 | iterator end(unsigned Slot) const { return getSlotNode(Slot)->end(); } |
Bill Wendling | 9eb689c | 2013-01-28 00:21:34 +0000 | [diff] [blame] | 312 | |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 313 | void Profile(FoldingSetNodeID &ID) const { |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 314 | Profile(ID, makeArrayRef(getNode(0), getNumAttributes())); |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 315 | } |
Bill Wendling | 9ac69f9 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 316 | static void Profile(FoldingSetNodeID &ID, |
Bill Wendling | eeebb13 | 2013-01-28 22:33:39 +0000 | [diff] [blame] | 317 | ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) { |
Bill Wendling | 39a4c80 | 2013-01-24 01:01:34 +0000 | [diff] [blame] | 318 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { |
| 319 | ID.AddInteger(Nodes[i].first); |
| 320 | ID.AddPointer(Nodes[i].second); |
| 321 | } |
| 322 | } |
Bill Wendling | 1f786a7 | 2013-01-27 23:41:29 +0000 | [diff] [blame] | 323 | |
| 324 | // FIXME: This atrocity is temporary. |
Rafael Espindola | dd27530 | 2013-04-30 16:53:38 +0000 | [diff] [blame] | 325 | uint64_t Raw(unsigned Index) const; |
Peter Collingbourne | abca2ec | 2013-08-02 22:34:30 +0000 | [diff] [blame] | 326 | |
| 327 | void dump() const; |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 328 | }; |
| 329 | |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 330 | } // end llvm namespace |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 331 | |
| 332 | #endif |