comparison lib/IR/AttributeImpl.h @ 121:803732b1fca8

LLVM 5.0
author kono
date Fri, 27 Oct 2017 17:07:41 +0900
parents 1172e4bd9c6f
children c2174574ed3a
comparison
equal deleted inserted replaced
120:1172e4bd9c6f 121:803732b1fca8
1 //===-- AttributeImpl.h - Attribute Internals -------------------*- C++ -*-===// 1 //===- AttributeImpl.h - Attribute Internals --------------------*- C++ -*-===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // The LLVM Compiler Infrastructure
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
14 //===----------------------------------------------------------------------===// 14 //===----------------------------------------------------------------------===//
15 15
16 #ifndef LLVM_LIB_IR_ATTRIBUTEIMPL_H 16 #ifndef LLVM_LIB_IR_ATTRIBUTEIMPL_H
17 #define LLVM_LIB_IR_ATTRIBUTEIMPL_H 17 #define LLVM_LIB_IR_ATTRIBUTEIMPL_H
18 18
19 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/FoldingSet.h" 20 #include "llvm/ADT/FoldingSet.h"
20 #include "llvm/ADT/Optional.h" 21 #include "llvm/ADT/StringRef.h"
21 #include "llvm/IR/Attributes.h" 22 #include "llvm/IR/Attributes.h"
22 #include "AttributeSetNode.h" 23 #include "llvm/Support/TrailingObjects.h"
23 #include "llvm/Support/DataTypes.h" 24 #include <cassert>
24 #include <climits> 25 #include <cstddef>
26 #include <cstdint>
25 #include <string> 27 #include <string>
28 #include <utility>
26 29
27 namespace llvm { 30 namespace llvm {
28 31
29 class Constant;
30 class LLVMContext; 32 class LLVMContext;
31 33
32 //===----------------------------------------------------------------------===// 34 //===----------------------------------------------------------------------===//
33 /// \class 35 /// \class
34 /// \brief This class represents a single, uniqued attribute. That attribute 36 /// \brief This class represents a single, uniqued attribute. That attribute
35 /// could be a single enum, a tuple, or a string. 37 /// could be a single enum, a tuple, or a string.
36 class AttributeImpl : public FoldingSetNode { 38 class AttributeImpl : public FoldingSetNode {
37 unsigned char KindID; ///< Holds the AttrEntryKind of the attribute 39 unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
38
39 // AttributesImpl is uniqued, these should not be publicly available.
40 void operator=(const AttributeImpl &) = delete;
41 AttributeImpl(const AttributeImpl &) = delete;
42 40
43 protected: 41 protected:
44 enum AttrEntryKind { 42 enum AttrEntryKind {
45 EnumAttrEntry, 43 EnumAttrEntry,
46 IntAttrEntry, 44 IntAttrEntry,
48 }; 46 };
49 47
50 AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {} 48 AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
51 49
52 public: 50 public:
51 // AttributesImpl is uniqued, these should not be available.
52 AttributeImpl(const AttributeImpl &) = delete;
53 AttributeImpl &operator=(const AttributeImpl &) = delete;
54
53 virtual ~AttributeImpl(); 55 virtual ~AttributeImpl();
54 56
55 bool isEnumAttribute() const { return KindID == EnumAttrEntry; } 57 bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
56 bool isIntAttribute() const { return KindID == IntAttrEntry; } 58 bool isIntAttribute() const { return KindID == IntAttrEntry; }
57 bool isStringAttribute() const { return KindID == StringAttrEntry; } 59 bool isStringAttribute() const { return KindID == StringAttrEntry; }
74 else if (isIntAttribute()) 76 else if (isIntAttribute())
75 Profile(ID, getKindAsEnum(), getValueAsInt()); 77 Profile(ID, getKindAsEnum(), getValueAsInt());
76 else 78 else
77 Profile(ID, getKindAsString(), getValueAsString()); 79 Profile(ID, getKindAsString(), getValueAsString());
78 } 80 }
81
79 static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, 82 static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
80 uint64_t Val) { 83 uint64_t Val) {
81 ID.AddInteger(Kind); 84 ID.AddInteger(Kind);
82 if (Val) ID.AddInteger(Val); 85 if (Val) ID.AddInteger(Val);
83 } 86 }
87
84 static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) { 88 static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
85 ID.AddString(Kind); 89 ID.AddString(Kind);
86 if (!Values.empty()) ID.AddString(Values); 90 if (!Values.empty()) ID.AddString(Values);
87 } 91 }
88 }; 92 };
94 /// represented by Attribute::AttrKind; alignment attribute entries; and string 98 /// represented by Attribute::AttrKind; alignment attribute entries; and string
95 /// attribute enties, which are for target-dependent attributes. 99 /// attribute enties, which are for target-dependent attributes.
96 100
97 class EnumAttributeImpl : public AttributeImpl { 101 class EnumAttributeImpl : public AttributeImpl {
98 virtual void anchor(); 102 virtual void anchor();
103
99 Attribute::AttrKind Kind; 104 Attribute::AttrKind Kind;
100 105
101 protected: 106 protected:
102 EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind) 107 EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
103 : AttributeImpl(ID), Kind(Kind) {} 108 : AttributeImpl(ID), Kind(Kind) {}
108 113
109 Attribute::AttrKind getEnumKind() const { return Kind; } 114 Attribute::AttrKind getEnumKind() const { return Kind; }
110 }; 115 };
111 116
112 class IntAttributeImpl : public EnumAttributeImpl { 117 class IntAttributeImpl : public EnumAttributeImpl {
118 uint64_t Val;
119
113 void anchor() override; 120 void anchor() override;
114 uint64_t Val;
115 121
116 public: 122 public:
117 IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val) 123 IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
118 : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) { 124 : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
119 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment || 125 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
126 uint64_t getValue() const { return Val; } 132 uint64_t getValue() const { return Val; }
127 }; 133 };
128 134
129 class StringAttributeImpl : public AttributeImpl { 135 class StringAttributeImpl : public AttributeImpl {
130 virtual void anchor(); 136 virtual void anchor();
137
131 std::string Kind; 138 std::string Kind;
132 std::string Val; 139 std::string Val;
133 140
134 public: 141 public:
135 StringAttributeImpl(StringRef Kind, StringRef Val = StringRef()) 142 StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
137 144
138 StringRef getStringKind() const { return Kind; } 145 StringRef getStringKind() const { return Kind; }
139 StringRef getStringValue() const { return Val; } 146 StringRef getStringValue() const { return Val; }
140 }; 147 };
141 148
142 typedef std::pair<unsigned, AttributeSetNode *> IndexAttrPair; 149 //===----------------------------------------------------------------------===//
150 /// \class
151 /// \brief This class represents a group of attributes that apply to one
152 /// element: function, return type, or parameter.
153 class AttributeSetNode final
154 : public FoldingSetNode,
155 private TrailingObjects<AttributeSetNode, Attribute> {
156 friend TrailingObjects;
157
158 /// Bitset with a bit for each available attribute Attribute::AttrKind.
159 uint64_t AvailableAttrs;
160 unsigned NumAttrs; ///< Number of attributes in this node.
161
162 AttributeSetNode(ArrayRef<Attribute> Attrs);
163
164 public:
165 // AttributesSetNode is uniqued, these should not be available.
166 AttributeSetNode(const AttributeSetNode &) = delete;
167 AttributeSetNode &operator=(const AttributeSetNode &) = delete;
168
169 void operator delete(void *p) { ::operator delete(p); }
170
171 static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B);
172
173 static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
174
175 /// \brief Return the number of attributes this AttributeList contains.
176 unsigned getNumAttributes() const { return NumAttrs; }
177
178 bool hasAttribute(Attribute::AttrKind Kind) const {
179 return AvailableAttrs & ((uint64_t)1) << Kind;
180 }
181 bool hasAttribute(StringRef Kind) const;
182 bool hasAttributes() const { return NumAttrs != 0; }
183
184 Attribute getAttribute(Attribute::AttrKind Kind) const;
185 Attribute getAttribute(StringRef Kind) const;
186
187 unsigned getAlignment() const;
188 unsigned getStackAlignment() const;
189 uint64_t getDereferenceableBytes() const;
190 uint64_t getDereferenceableOrNullBytes() const;
191 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
192 std::string getAsString(bool InAttrGrp) const;
193
194 using iterator = const Attribute *;
195
196 iterator begin() const { return getTrailingObjects<Attribute>(); }
197 iterator end() const { return begin() + NumAttrs; }
198
199 void Profile(FoldingSetNodeID &ID) const {
200 Profile(ID, makeArrayRef(begin(), end()));
201 }
202
203 static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
204 for (const auto &Attr : AttrList)
205 Attr.Profile(ID);
206 }
207 };
208
209 using IndexAttrPair = std::pair<unsigned, AttributeSet>;
143 210
144 //===----------------------------------------------------------------------===// 211 //===----------------------------------------------------------------------===//
145 /// \class 212 /// \class
146 /// \brief This class represents a set of attributes that apply to the function, 213 /// \brief This class represents a set of attributes that apply to the function,
147 /// return type, and parameters. 214 /// return type, and parameters.
148 class AttributeSetImpl final 215 class AttributeListImpl final
149 : public FoldingSetNode, 216 : public FoldingSetNode,
150 private TrailingObjects<AttributeSetImpl, IndexAttrPair> { 217 private TrailingObjects<AttributeListImpl, AttributeSet> {
151 friend class AttributeSet; 218 friend class AttributeList;
152 friend TrailingObjects; 219 friend TrailingObjects;
153 220
154 private: 221 private:
155 LLVMContext &Context;
156 unsigned NumSlots; ///< Number of entries in this set.
157 /// Bitset with a bit for each available attribute Attribute::AttrKind. 222 /// Bitset with a bit for each available attribute Attribute::AttrKind.
158 uint64_t AvailableFunctionAttrs; 223 uint64_t AvailableFunctionAttrs;
224 LLVMContext &Context;
225 unsigned NumAttrSets; ///< Number of entries in this set.
159 226
160 // Helper fn for TrailingObjects class. 227 // Helper fn for TrailingObjects class.
161 size_t numTrailingObjects(OverloadToken<IndexAttrPair>) { return NumSlots; } 228 size_t numTrailingObjects(OverloadToken<AttributeSet>) { return NumAttrSets; }
162 229
163 /// \brief Return a pointer to the IndexAttrPair for the specified slot. 230 public:
164 const IndexAttrPair *getNode(unsigned Slot) const { 231 AttributeListImpl(LLVMContext &C, ArrayRef<AttributeSet> Sets);
165 return getTrailingObjects<IndexAttrPair>() + Slot; 232
166 } 233 // AttributesSetImpt is uniqued, these should not be available.
167 234 AttributeListImpl(const AttributeListImpl &) = delete;
168 // AttributesSet is uniqued, these should not be publicly available. 235 AttributeListImpl &operator=(const AttributeListImpl &) = delete;
169 void operator=(const AttributeSetImpl &) = delete;
170 AttributeSetImpl(const AttributeSetImpl &) = delete;
171 public:
172 AttributeSetImpl(LLVMContext &C,
173 ArrayRef<std::pair<unsigned, AttributeSetNode *> > Slots)
174 : Context(C), NumSlots(Slots.size()), AvailableFunctionAttrs(0) {
175 static_assert(Attribute::EndAttrKinds <=
176 sizeof(AvailableFunctionAttrs) * CHAR_BIT,
177 "Too many attributes");
178
179 #ifndef NDEBUG
180 if (Slots.size() >= 2) {
181 for (const std::pair<unsigned, AttributeSetNode *> *i = Slots.begin() + 1,
182 *e = Slots.end();
183 i != e; ++i) {
184 assert((i-1)->first <= i->first && "Attribute set not ordered!");
185 }
186 }
187 #endif
188 // There's memory after the node where we can store the entries in.
189 std::copy(Slots.begin(), Slots.end(), getTrailingObjects<IndexAttrPair>());
190
191 // Initialize AvailableFunctionAttrs summary bitset.
192 if (NumSlots > 0) {
193 static_assert(AttributeSet::FunctionIndex == ~0u,
194 "FunctionIndex should be biggest possible index");
195 const std::pair<unsigned, AttributeSetNode *> &Last = Slots.back();
196 if (Last.first == AttributeSet::FunctionIndex) {
197 const AttributeSetNode *Node = Last.second;
198 for (Attribute I : *Node) {
199 if (!I.isStringAttribute())
200 AvailableFunctionAttrs |= ((uint64_t)1) << I.getKindAsEnum();
201 }
202 }
203 }
204 }
205 236
206 void operator delete(void *p) { ::operator delete(p); } 237 void operator delete(void *p) { ::operator delete(p); }
207 238
208 /// \brief Get the context that created this AttributeSetImpl. 239 /// \brief Get the context that created this AttributeListImpl.
209 LLVMContext &getContext() { return Context; } 240 LLVMContext &getContext() { return Context; }
210 241
211 /// \brief Return the number of slots used in this attribute list. This is 242 /// \brief Return true if the AttributeSet or the FunctionIndex has an
212 /// the number of arguments that have an attribute set on them (including the
213 /// function itself).
214 unsigned getNumSlots() const { return NumSlots; }
215
216 /// \brief Get the index of the given "slot" in the AttrNodes list. This index
217 /// is the index of the return, parameter, or function object that the
218 /// attributes are applied to, not the index into the AttrNodes list where the
219 /// attributes reside.
220 unsigned getSlotIndex(unsigned Slot) const {
221 return getNode(Slot)->first;
222 }
223
224 /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
225 /// \p Slot is an index into the AttrNodes list, not the index of the return /
226 /// parameter/ function which the attributes apply to.
227 AttributeSet getSlotAttributes(unsigned Slot) const {
228 return AttributeSet::get(Context, *getNode(Slot));
229 }
230
231 /// \brief Retrieve the attribute set node for the given "slot" in the
232 /// AttrNode list.
233 AttributeSetNode *getSlotNode(unsigned Slot) const {
234 return getNode(Slot)->second;
235 }
236
237 /// \brief Return true if the AttributeSetNode for the FunctionIndex has an
238 /// enum attribute of the given kind. 243 /// enum attribute of the given kind.
239 bool hasFnAttribute(Attribute::AttrKind Kind) const { 244 bool hasFnAttribute(Attribute::AttrKind Kind) const {
240 return AvailableFunctionAttrs & ((uint64_t)1) << Kind; 245 return AvailableFunctionAttrs & ((uint64_t)1) << Kind;
241 } 246 }
242 247
243 typedef AttributeSetNode::iterator iterator; 248 using iterator = const AttributeSet *;
244 iterator begin(unsigned Slot) const { return getSlotNode(Slot)->begin(); } 249
245 iterator end(unsigned Slot) const { return getSlotNode(Slot)->end(); } 250 iterator begin() const { return getTrailingObjects<AttributeSet>(); }
246 251 iterator end() const { return begin() + NumAttrSets; }
247 void Profile(FoldingSetNodeID &ID) const { 252
248 Profile(ID, makeArrayRef(getNode(0), getNumSlots())); 253 void Profile(FoldingSetNodeID &ID) const;
249 } 254 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeSet> Nodes);
250 static void Profile(FoldingSetNodeID &ID,
251 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
252 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
253 ID.AddInteger(Nodes[i].first);
254 ID.AddPointer(Nodes[i].second);
255 }
256 }
257 255
258 void dump() const; 256 void dump() const;
259 }; 257 };
260 258
261 } // end llvm namespace 259 } // end namespace llvm
262 260
263 #endif 261 #endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H