Mercurial > hg > CbC > CbC_llvm
comparison lib/IR/AttributeImpl.h @ 148:63bd29f05246
merged
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 14 Aug 2019 19:46:37 +0900 |
parents | c2174574ed3a |
children |
comparison
equal
deleted
inserted
replaced
146:3fc4d5c3e21e | 148:63bd29f05246 |
---|---|
1 //===- AttributeImpl.h - Attribute Internals --------------------*- C++ -*-===// | 1 //===- AttributeImpl.h - Attribute Internals --------------------*- C++ -*-===// |
2 // | 2 // |
3 // The LLVM Compiler Infrastructure | 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 // | 4 // See https://llvm.org/LICENSE.txt for license information. |
5 // This file is distributed under the University of Illinois Open Source | 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 // License. See LICENSE.TXT for details. | |
7 // | 6 // |
8 //===----------------------------------------------------------------------===// | 7 //===----------------------------------------------------------------------===// |
9 /// | 8 /// |
10 /// \file | 9 /// \file |
11 /// \brief This file defines various helper methods and classes used by | 10 /// This file defines various helper methods and classes used by |
12 /// LLVMContextImpl for creating and managing attributes. | 11 /// LLVMContextImpl for creating and managing attributes. |
13 /// | 12 /// |
14 //===----------------------------------------------------------------------===// | 13 //===----------------------------------------------------------------------===// |
15 | 14 |
16 #ifndef LLVM_LIB_IR_ATTRIBUTEIMPL_H | 15 #ifndef LLVM_LIB_IR_ATTRIBUTEIMPL_H |
28 #include <utility> | 27 #include <utility> |
29 | 28 |
30 namespace llvm { | 29 namespace llvm { |
31 | 30 |
32 class LLVMContext; | 31 class LLVMContext; |
33 | 32 class Type; |
34 //===----------------------------------------------------------------------===// | 33 |
35 /// \class | 34 //===----------------------------------------------------------------------===// |
36 /// \brief This class represents a single, uniqued attribute. That attribute | 35 /// \class |
36 /// This class represents a single, uniqued attribute. That attribute | |
37 /// could be a single enum, a tuple, or a string. | 37 /// could be a single enum, a tuple, or a string. |
38 class AttributeImpl : public FoldingSetNode { | 38 class AttributeImpl : public FoldingSetNode { |
39 unsigned char KindID; ///< Holds the AttrEntryKind of the attribute | 39 unsigned char KindID; ///< Holds the AttrEntryKind of the attribute |
40 | 40 |
41 protected: | 41 protected: |
42 enum AttrEntryKind { | 42 enum AttrEntryKind { |
43 EnumAttrEntry, | 43 EnumAttrEntry, |
44 IntAttrEntry, | 44 IntAttrEntry, |
45 StringAttrEntry | 45 StringAttrEntry, |
46 TypeAttrEntry, | |
46 }; | 47 }; |
47 | 48 |
48 AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {} | 49 AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {} |
49 | 50 |
50 public: | 51 public: |
55 virtual ~AttributeImpl(); | 56 virtual ~AttributeImpl(); |
56 | 57 |
57 bool isEnumAttribute() const { return KindID == EnumAttrEntry; } | 58 bool isEnumAttribute() const { return KindID == EnumAttrEntry; } |
58 bool isIntAttribute() const { return KindID == IntAttrEntry; } | 59 bool isIntAttribute() const { return KindID == IntAttrEntry; } |
59 bool isStringAttribute() const { return KindID == StringAttrEntry; } | 60 bool isStringAttribute() const { return KindID == StringAttrEntry; } |
61 bool isTypeAttribute() const { return KindID == TypeAttrEntry; } | |
60 | 62 |
61 bool hasAttribute(Attribute::AttrKind A) const; | 63 bool hasAttribute(Attribute::AttrKind A) const; |
62 bool hasAttribute(StringRef Kind) const; | 64 bool hasAttribute(StringRef Kind) const; |
63 | 65 |
64 Attribute::AttrKind getKindAsEnum() const; | 66 Attribute::AttrKind getKindAsEnum() const; |
65 uint64_t getValueAsInt() const; | 67 uint64_t getValueAsInt() const; |
66 | 68 |
67 StringRef getKindAsString() const; | 69 StringRef getKindAsString() const; |
68 StringRef getValueAsString() const; | 70 StringRef getValueAsString() const; |
69 | 71 |
70 /// \brief Used when sorting the attributes. | 72 Type *getValueAsType() const; |
73 | |
74 /// Used when sorting the attributes. | |
71 bool operator<(const AttributeImpl &AI) const; | 75 bool operator<(const AttributeImpl &AI) const; |
72 | 76 |
73 void Profile(FoldingSetNodeID &ID) const { | 77 void Profile(FoldingSetNodeID &ID) const { |
74 if (isEnumAttribute()) | 78 if (isEnumAttribute()) |
75 Profile(ID, getKindAsEnum(), 0); | 79 Profile(ID, getKindAsEnum(), static_cast<uint64_t>(0)); |
76 else if (isIntAttribute()) | 80 else if (isIntAttribute()) |
77 Profile(ID, getKindAsEnum(), getValueAsInt()); | 81 Profile(ID, getKindAsEnum(), getValueAsInt()); |
82 else if (isStringAttribute()) | |
83 Profile(ID, getKindAsString(), getValueAsString()); | |
78 else | 84 else |
79 Profile(ID, getKindAsString(), getValueAsString()); | 85 Profile(ID, getKindAsEnum(), getValueAsType()); |
80 } | 86 } |
81 | 87 |
82 static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, | 88 static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, |
83 uint64_t Val) { | 89 uint64_t Val) { |
84 ID.AddInteger(Kind); | 90 ID.AddInteger(Kind); |
87 | 93 |
88 static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) { | 94 static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) { |
89 ID.AddString(Kind); | 95 ID.AddString(Kind); |
90 if (!Values.empty()) ID.AddString(Values); | 96 if (!Values.empty()) ID.AddString(Values); |
91 } | 97 } |
92 }; | 98 |
93 | 99 static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, |
94 //===----------------------------------------------------------------------===// | 100 Type *Ty) { |
95 /// \class | 101 ID.AddInteger(Kind); |
96 /// \brief A set of classes that contain the value of the | 102 ID.AddPointer(Ty); |
103 } | |
104 }; | |
105 | |
106 //===----------------------------------------------------------------------===// | |
107 /// \class | |
108 /// A set of classes that contain the value of the | |
97 /// attribute object. There are three main categories: enum attribute entries, | 109 /// attribute object. There are three main categories: enum attribute entries, |
98 /// represented by Attribute::AttrKind; alignment attribute entries; and string | 110 /// represented by Attribute::AttrKind; alignment attribute entries; and string |
99 /// attribute enties, which are for target-dependent attributes. | 111 /// attribute enties, which are for target-dependent attributes. |
100 | 112 |
101 class EnumAttributeImpl : public AttributeImpl { | 113 class EnumAttributeImpl : public AttributeImpl { |
144 | 156 |
145 StringRef getStringKind() const { return Kind; } | 157 StringRef getStringKind() const { return Kind; } |
146 StringRef getStringValue() const { return Val; } | 158 StringRef getStringValue() const { return Val; } |
147 }; | 159 }; |
148 | 160 |
149 //===----------------------------------------------------------------------===// | 161 class TypeAttributeImpl : public EnumAttributeImpl { |
150 /// \class | 162 virtual void anchor(); |
151 /// \brief This class represents a group of attributes that apply to one | 163 |
164 Type *Ty; | |
165 | |
166 public: | |
167 TypeAttributeImpl(Attribute::AttrKind Kind, Type *Ty) | |
168 : EnumAttributeImpl(TypeAttrEntry, Kind), Ty(Ty) {} | |
169 | |
170 Type *getTypeValue() const { return Ty; } | |
171 }; | |
172 | |
173 //===----------------------------------------------------------------------===// | |
174 /// \class | |
175 /// This class represents a group of attributes that apply to one | |
152 /// element: function, return type, or parameter. | 176 /// element: function, return type, or parameter. |
153 class AttributeSetNode final | 177 class AttributeSetNode final |
154 : public FoldingSetNode, | 178 : public FoldingSetNode, |
155 private TrailingObjects<AttributeSetNode, Attribute> { | 179 private TrailingObjects<AttributeSetNode, Attribute> { |
156 friend TrailingObjects; | 180 friend TrailingObjects; |
157 | 181 |
182 unsigned NumAttrs; ///< Number of attributes in this node. | |
158 /// Bitset with a bit for each available attribute Attribute::AttrKind. | 183 /// Bitset with a bit for each available attribute Attribute::AttrKind. |
159 uint64_t AvailableAttrs; | 184 uint8_t AvailableAttrs[12] = {}; |
160 unsigned NumAttrs; ///< Number of attributes in this node. | |
161 | 185 |
162 AttributeSetNode(ArrayRef<Attribute> Attrs); | 186 AttributeSetNode(ArrayRef<Attribute> Attrs); |
163 | 187 |
164 public: | 188 public: |
165 // AttributesSetNode is uniqued, these should not be available. | 189 // AttributesSetNode is uniqued, these should not be available. |
170 | 194 |
171 static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B); | 195 static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B); |
172 | 196 |
173 static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs); | 197 static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs); |
174 | 198 |
175 /// \brief Return the number of attributes this AttributeList contains. | 199 /// Return the number of attributes this AttributeList contains. |
176 unsigned getNumAttributes() const { return NumAttrs; } | 200 unsigned getNumAttributes() const { return NumAttrs; } |
177 | 201 |
178 bool hasAttribute(Attribute::AttrKind Kind) const { | 202 bool hasAttribute(Attribute::AttrKind Kind) const { |
179 return AvailableAttrs & ((uint64_t)1) << Kind; | 203 return AvailableAttrs[Kind / 8] & ((uint64_t)1) << (Kind % 8); |
180 } | 204 } |
181 bool hasAttribute(StringRef Kind) const; | 205 bool hasAttribute(StringRef Kind) const; |
182 bool hasAttributes() const { return NumAttrs != 0; } | 206 bool hasAttributes() const { return NumAttrs != 0; } |
183 | 207 |
184 Attribute getAttribute(Attribute::AttrKind Kind) const; | 208 Attribute getAttribute(Attribute::AttrKind Kind) const; |
188 unsigned getStackAlignment() const; | 212 unsigned getStackAlignment() const; |
189 uint64_t getDereferenceableBytes() const; | 213 uint64_t getDereferenceableBytes() const; |
190 uint64_t getDereferenceableOrNullBytes() const; | 214 uint64_t getDereferenceableOrNullBytes() const; |
191 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const; | 215 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const; |
192 std::string getAsString(bool InAttrGrp) const; | 216 std::string getAsString(bool InAttrGrp) const; |
217 Type *getByValType() const; | |
193 | 218 |
194 using iterator = const Attribute *; | 219 using iterator = const Attribute *; |
195 | 220 |
196 iterator begin() const { return getTrailingObjects<Attribute>(); } | 221 iterator begin() const { return getTrailingObjects<Attribute>(); } |
197 iterator end() const { return begin() + NumAttrs; } | 222 iterator end() const { return begin() + NumAttrs; } |
208 | 233 |
209 using IndexAttrPair = std::pair<unsigned, AttributeSet>; | 234 using IndexAttrPair = std::pair<unsigned, AttributeSet>; |
210 | 235 |
211 //===----------------------------------------------------------------------===// | 236 //===----------------------------------------------------------------------===// |
212 /// \class | 237 /// \class |
213 /// \brief This class represents a set of attributes that apply to the function, | 238 /// This class represents a set of attributes that apply to the function, |
214 /// return type, and parameters. | 239 /// return type, and parameters. |
215 class AttributeListImpl final | 240 class AttributeListImpl final |
216 : public FoldingSetNode, | 241 : public FoldingSetNode, |
217 private TrailingObjects<AttributeListImpl, AttributeSet> { | 242 private TrailingObjects<AttributeListImpl, AttributeSet> { |
218 friend class AttributeList; | 243 friend class AttributeList; |
219 friend TrailingObjects; | 244 friend TrailingObjects; |
220 | 245 |
221 private: | 246 private: |
222 /// Bitset with a bit for each available attribute Attribute::AttrKind. | |
223 uint64_t AvailableFunctionAttrs; | |
224 LLVMContext &Context; | 247 LLVMContext &Context; |
225 unsigned NumAttrSets; ///< Number of entries in this set. | 248 unsigned NumAttrSets; ///< Number of entries in this set. |
249 /// Bitset with a bit for each available attribute Attribute::AttrKind. | |
250 uint8_t AvailableFunctionAttrs[12] = {}; | |
226 | 251 |
227 // Helper fn for TrailingObjects class. | 252 // Helper fn for TrailingObjects class. |
228 size_t numTrailingObjects(OverloadToken<AttributeSet>) { return NumAttrSets; } | 253 size_t numTrailingObjects(OverloadToken<AttributeSet>) { return NumAttrSets; } |
229 | 254 |
230 public: | 255 public: |
234 AttributeListImpl(const AttributeListImpl &) = delete; | 259 AttributeListImpl(const AttributeListImpl &) = delete; |
235 AttributeListImpl &operator=(const AttributeListImpl &) = delete; | 260 AttributeListImpl &operator=(const AttributeListImpl &) = delete; |
236 | 261 |
237 void operator delete(void *p) { ::operator delete(p); } | 262 void operator delete(void *p) { ::operator delete(p); } |
238 | 263 |
239 /// \brief Get the context that created this AttributeListImpl. | 264 /// Get the context that created this AttributeListImpl. |
240 LLVMContext &getContext() { return Context; } | 265 LLVMContext &getContext() { return Context; } |
241 | 266 |
242 /// \brief Return true if the AttributeSet or the FunctionIndex has an | 267 /// Return true if the AttributeSet or the FunctionIndex has an |
243 /// enum attribute of the given kind. | 268 /// enum attribute of the given kind. |
244 bool hasFnAttribute(Attribute::AttrKind Kind) const { | 269 bool hasFnAttribute(Attribute::AttrKind Kind) const { |
245 return AvailableFunctionAttrs & ((uint64_t)1) << Kind; | 270 return AvailableFunctionAttrs[Kind / 8] & ((uint64_t)1) << (Kind % 8); |
246 } | 271 } |
247 | 272 |
248 using iterator = const AttributeSet *; | 273 using iterator = const AttributeSet *; |
249 | 274 |
250 iterator begin() const { return getTrailingObjects<AttributeSet>(); } | 275 iterator begin() const { return getTrailingObjects<AttributeSet>(); } |