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 | |
Eugene Zelenko | 9408c61 | 2016-12-07 22:06:02 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/ArrayRef.h" |
| 20 | #include "llvm/ADT/FoldingSet.h" |
| 21 | #include "llvm/ADT/StringRef.h" |
Reid Kleckner | eb9dd5b | 2017-04-10 23:31:05 +0000 | [diff] [blame] | 22 | #include "llvm/IR/AttributeSetNode.h" |
Eugene Zelenko | 9408c61 | 2016-12-07 22:06:02 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Attributes.h" |
| 24 | #include "llvm/Support/TrailingObjects.h" |
| 25 | #include <algorithm> |
| 26 | #include <cassert> |
Matthias Braun | d520d4e | 2016-01-29 22:30:30 +0000 | [diff] [blame] | 27 | #include <climits> |
Eugene Zelenko | 9408c61 | 2016-12-07 22:06:02 +0000 | [diff] [blame] | 28 | #include <cstddef> |
| 29 | #include <cstdint> |
Matthias Braun | 9c98105 | 2016-01-29 22:35:29 +0000 | [diff] [blame] | 30 | #include <string> |
Eugene Zelenko | 9408c61 | 2016-12-07 22:06:02 +0000 | [diff] [blame] | 31 | #include <utility> |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 32 | |
| 33 | namespace llvm { |
| 34 | |
Bill Wendling | 6ad6c3b | 2012-12-19 23:55:43 +0000 | [diff] [blame] | 35 | class LLVMContext; |
| 36 | |
Bill Wendling | 66e978f | 2012-12-20 21:28:43 +0000 | [diff] [blame] | 37 | //===----------------------------------------------------------------------===// |
| 38 | /// \class |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 39 | /// \brief This class represents a single, uniqued attribute. That attribute |
| 40 | /// could be a single enum, a tuple, or a string. |
Benjamin Kramer | 079b96e | 2013-09-11 18:05:11 +0000 | [diff] [blame] | 41 | class AttributeImpl : public FoldingSetNode { |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 42 | unsigned char KindID; ///< Holds the AttrEntryKind of the attribute |
| 43 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 44 | protected: |
| 45 | enum AttrEntryKind { |
| 46 | EnumAttrEntry, |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 47 | IntAttrEntry, |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 48 | StringAttrEntry |
| 49 | }; |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 50 | |
| 51 | AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {} |
| 52 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 53 | public: |
Eugene Zelenko | 9408c61 | 2016-12-07 22:06:02 +0000 | [diff] [blame] | 54 | // AttributesImpl is uniqued, these should not be available. |
| 55 | AttributeImpl(const AttributeImpl &) = delete; |
| 56 | AttributeImpl &operator=(const AttributeImpl &) = delete; |
| 57 | |
Alexey Samsonov | 49109a2 | 2013-11-18 09:31:53 +0000 | [diff] [blame] | 58 | virtual ~AttributeImpl(); |
| 59 | |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 60 | bool isEnumAttribute() const { return KindID == EnumAttrEntry; } |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 61 | bool isIntAttribute() const { return KindID == IntAttrEntry; } |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 62 | bool isStringAttribute() const { return KindID == StringAttrEntry; } |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 63 | |
Bill Wendling | 9ac69f9 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 64 | bool hasAttribute(Attribute::AttrKind A) const; |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 65 | bool hasAttribute(StringRef Kind) const; |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 66 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 67 | Attribute::AttrKind getKindAsEnum() const; |
| 68 | uint64_t getValueAsInt() const; |
Bill Wendling | c3c714b | 2013-01-29 20:37:10 +0000 | [diff] [blame] | 69 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 70 | StringRef getKindAsString() const; |
| 71 | StringRef getValueAsString() const; |
Bill Wendling | b1d1261 | 2012-12-30 01:38:39 +0000 | [diff] [blame] | 72 | |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 73 | /// \brief Used when sorting the attributes. |
Bill Wendling | d2e493b | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 74 | bool operator<(const AttributeImpl &AI) const; |
| 75 | |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 76 | void Profile(FoldingSetNodeID &ID) const { |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 77 | if (isEnumAttribute()) |
| 78 | Profile(ID, getKindAsEnum(), 0); |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 79 | else if (isIntAttribute()) |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 80 | Profile(ID, getKindAsEnum(), getValueAsInt()); |
| 81 | else |
| 82 | Profile(ID, getKindAsString(), getValueAsString()); |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 83 | } |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 84 | static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, |
| 85 | uint64_t Val) { |
| 86 | ID.AddInteger(Kind); |
| 87 | if (Val) ID.AddInteger(Val); |
| 88 | } |
| 89 | static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) { |
| 90 | ID.AddString(Kind); |
Bill Wendling | 8a0e084 | 2013-02-28 21:17:03 +0000 | [diff] [blame] | 91 | if (!Values.empty()) ID.AddString(Values); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 92 | } |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
Bill Wendling | 66e978f | 2012-12-20 21:28:43 +0000 | [diff] [blame] | 95 | //===----------------------------------------------------------------------===// |
| 96 | /// \class |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 97 | /// \brief A set of classes that contain the value of the |
| 98 | /// attribute object. There are three main categories: enum attribute entries, |
| 99 | /// represented by Attribute::AttrKind; alignment attribute entries; and string |
| 100 | /// attribute enties, which are for target-dependent attributes. |
| 101 | |
Benjamin Kramer | 079b96e | 2013-09-11 18:05:11 +0000 | [diff] [blame] | 102 | class EnumAttributeImpl : public AttributeImpl { |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 103 | virtual void anchor(); |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 104 | Attribute::AttrKind Kind; |
| 105 | |
| 106 | protected: |
| 107 | EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind) |
| 108 | : AttributeImpl(ID), Kind(Kind) {} |
| 109 | |
| 110 | public: |
| 111 | EnumAttributeImpl(Attribute::AttrKind Kind) |
| 112 | : AttributeImpl(EnumAttrEntry), Kind(Kind) {} |
| 113 | |
| 114 | Attribute::AttrKind getEnumKind() const { return Kind; } |
| 115 | }; |
| 116 | |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 117 | class IntAttributeImpl : public EnumAttributeImpl { |
Craig Topper | f398d7c | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 118 | void anchor() override; |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 119 | uint64_t Val; |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 120 | |
| 121 | public: |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 122 | IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val) |
| 123 | : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) { |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 124 | assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment || |
| 125 | Kind == Attribute::Dereferenceable || |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 126 | Kind == Attribute::DereferenceableOrNull || |
| 127 | Kind == Attribute::AllocSize) && |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 128 | "Wrong kind for int attribute!"); |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 131 | uint64_t getValue() const { return Val; } |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
Benjamin Kramer | 079b96e | 2013-09-11 18:05:11 +0000 | [diff] [blame] | 134 | class StringAttributeImpl : public AttributeImpl { |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 135 | virtual void anchor(); |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 136 | std::string Kind; |
| 137 | std::string Val; |
| 138 | |
| 139 | public: |
| 140 | StringAttributeImpl(StringRef Kind, StringRef Val = StringRef()) |
| 141 | : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {} |
| 142 | |
| 143 | StringRef getStringKind() const { return Kind; } |
| 144 | StringRef getStringValue() const { return Val; } |
| 145 | }; |
| 146 | |
NAKAMURA Takumi | 51fe119 | 2015-08-06 09:49:17 +0000 | [diff] [blame] | 147 | typedef std::pair<unsigned, AttributeSetNode *> IndexAttrPair; |
| 148 | |
Bill Wendling | d2e493b | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 149 | //===----------------------------------------------------------------------===// |
| 150 | /// \class |
| 151 | /// \brief This class represents a set of attributes that apply to the function, |
| 152 | /// return type, and parameters. |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 153 | class AttributeListImpl final |
James Y Knight | aa365b2 | 2015-08-05 22:57:34 +0000 | [diff] [blame] | 154 | : public FoldingSetNode, |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 155 | private TrailingObjects<AttributeListImpl, IndexAttrPair> { |
| 156 | friend class AttributeList; |
James Y Knight | aa365b2 | 2015-08-05 22:57:34 +0000 | [diff] [blame] | 157 | friend TrailingObjects; |
James Y Knight | 8096d34 | 2015-06-17 01:21:20 +0000 | [diff] [blame] | 158 | |
| 159 | private: |
| 160 | LLVMContext &Context; |
Amaury Sechet | 24c84fd | 2016-06-14 22:04:16 +0000 | [diff] [blame] | 161 | unsigned NumSlots; ///< Number of entries in this set. |
Matthias Braun | 3328281 | 2016-01-29 22:25:19 +0000 | [diff] [blame] | 162 | /// Bitset with a bit for each available attribute Attribute::AttrKind. |
| 163 | uint64_t AvailableFunctionAttrs; |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 164 | |
James Y Knight | aa365b2 | 2015-08-05 22:57:34 +0000 | [diff] [blame] | 165 | // Helper fn for TrailingObjects class. |
Amaury Sechet | 24c84fd | 2016-06-14 22:04:16 +0000 | [diff] [blame] | 166 | size_t numTrailingObjects(OverloadToken<IndexAttrPair>) { return NumSlots; } |
James Y Knight | aa365b2 | 2015-08-05 22:57:34 +0000 | [diff] [blame] | 167 | |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 168 | /// \brief Return a pointer to the IndexAttrPair for the specified slot. |
| 169 | const IndexAttrPair *getNode(unsigned Slot) const { |
James Y Knight | aa365b2 | 2015-08-05 22:57:34 +0000 | [diff] [blame] | 170 | return getTrailingObjects<IndexAttrPair>() + Slot; |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 171 | } |
Bill Wendling | 39a4c80 | 2013-01-24 01:01:34 +0000 | [diff] [blame] | 172 | |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 173 | public: |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 174 | AttributeListImpl(LLVMContext &C, |
Reid Kleckner | a82be60 | 2017-04-11 00:16:00 +0000 | [diff] [blame^] | 175 | ArrayRef<std::pair<unsigned, AttributeSetNode *>> Slots); |
Bill Wendling | 9ac69f9 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 176 | |
Eugene Zelenko | 9408c61 | 2016-12-07 22:06:02 +0000 | [diff] [blame] | 177 | // AttributesSetImpt is uniqued, these should not be available. |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 178 | AttributeListImpl(const AttributeListImpl &) = delete; |
| 179 | AttributeListImpl &operator=(const AttributeListImpl &) = delete; |
Eugene Zelenko | 9408c61 | 2016-12-07 22:06:02 +0000 | [diff] [blame] | 180 | |
Richard Smith | a64e1ad | 2016-02-09 02:09:16 +0000 | [diff] [blame] | 181 | void operator delete(void *p) { ::operator delete(p); } |
Richard Smith | 1b65c32 | 2016-02-09 01:03:42 +0000 | [diff] [blame] | 182 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 183 | /// \brief Get the context that created this AttributeListImpl. |
Bill Wendling | 9ac69f9 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 184 | LLVMContext &getContext() { return Context; } |
Bill Wendling | 5c8b2df | 2013-01-27 21:32:11 +0000 | [diff] [blame] | 185 | |
Amaury Sechet | 24c84fd | 2016-06-14 22:04:16 +0000 | [diff] [blame] | 186 | /// \brief Return the number of slots used in this attribute list. This is |
| 187 | /// the number of arguments that have an attribute set on them (including the |
| 188 | /// function itself). |
| 189 | unsigned getNumSlots() const { return NumSlots; } |
Bill Wendling | 5c8b2df | 2013-01-27 21:32:11 +0000 | [diff] [blame] | 190 | |
| 191 | /// \brief Get the index of the given "slot" in the AttrNodes list. This index |
| 192 | /// is the index of the return, parameter, or function object that the |
| 193 | /// attributes are applied to, not the index into the AttrNodes list where the |
| 194 | /// attributes reside. |
Rafael Espindola | dd27530 | 2013-04-30 16:53:38 +0000 | [diff] [blame] | 195 | unsigned getSlotIndex(unsigned Slot) const { |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 196 | return getNode(Slot)->first; |
Bill Wendling | 9eb689c | 2013-01-28 00:21:34 +0000 | [diff] [blame] | 197 | } |
Bill Wendling | 5c8b2df | 2013-01-27 21:32:11 +0000 | [diff] [blame] | 198 | |
| 199 | /// \brief Retrieve the attributes for the given "slot" in the AttrNode list. |
| 200 | /// \p Slot is an index into the AttrNodes list, not the index of the return / |
| 201 | /// parameter/ function which the attributes apply to. |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 202 | AttributeList getSlotAttributes(unsigned Slot) const { |
| 203 | return AttributeList::get(Context, *getNode(Slot)); |
Bill Wendling | 57625a4 | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 204 | } |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 205 | |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 206 | /// \brief Retrieve the attribute set node for the given "slot" in the |
| 207 | /// AttrNode list. |
| 208 | AttributeSetNode *getSlotNode(unsigned Slot) const { |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 209 | return getNode(Slot)->second; |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Matthias Braun | 3328281 | 2016-01-29 22:25:19 +0000 | [diff] [blame] | 212 | /// \brief Return true if the AttributeSetNode for the FunctionIndex has an |
| 213 | /// enum attribute of the given kind. |
| 214 | bool hasFnAttribute(Attribute::AttrKind Kind) const { |
| 215 | return AvailableFunctionAttrs & ((uint64_t)1) << Kind; |
| 216 | } |
| 217 | |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 218 | typedef AttributeSetNode::iterator iterator; |
| 219 | iterator begin(unsigned Slot) const { return getSlotNode(Slot)->begin(); } |
| 220 | iterator end(unsigned Slot) const { return getSlotNode(Slot)->end(); } |
Bill Wendling | 9eb689c | 2013-01-28 00:21:34 +0000 | [diff] [blame] | 221 | |
Reid Kleckner | a82be60 | 2017-04-11 00:16:00 +0000 | [diff] [blame^] | 222 | void Profile(FoldingSetNodeID &ID) const; |
Bill Wendling | 9ac69f9 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 223 | static void Profile(FoldingSetNodeID &ID, |
Reid Kleckner | a82be60 | 2017-04-11 00:16:00 +0000 | [diff] [blame^] | 224 | ArrayRef<std::pair<unsigned, AttributeSetNode*>> Nodes); |
Bill Wendling | 1f786a7 | 2013-01-27 23:41:29 +0000 | [diff] [blame] | 225 | |
Peter Collingbourne | abca2ec | 2013-08-02 22:34:30 +0000 | [diff] [blame] | 226 | void dump() const; |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 227 | }; |
| 228 | |
Eugene Zelenko | 9408c61 | 2016-12-07 22:06:02 +0000 | [diff] [blame] | 229 | } // end namespace llvm |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 230 | |
Eugene Zelenko | 9408c61 | 2016-12-07 22:06:02 +0000 | [diff] [blame] | 231 | #endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H |