LLVM  10.0.0svn
AttributeImpl.h
Go to the documentation of this file.
1 //===- AttributeImpl.h - Attribute Internals --------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file defines various helper methods and classes used by
11 /// LLVMContextImpl for creating and managing attributes.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_LIB_IR_ATTRIBUTEIMPL_H
16 #define LLVM_LIB_IR_ATTRIBUTEIMPL_H
17 
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/FoldingSet.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/IR/Attributes.h"
23 #include <cassert>
24 #include <cstddef>
25 #include <cstdint>
26 #include <string>
27 #include <utility>
28 
29 namespace llvm {
30 
31 class LLVMContext;
32 class Type;
33 
34 //===----------------------------------------------------------------------===//
35 /// \class
36 /// This class represents a single, uniqued attribute. That attribute
37 /// could be a single enum, a tuple, or a string.
38 class AttributeImpl : public FoldingSetNode {
39  unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
40 
41 protected:
47  };
48 
49  AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
50 
51 public:
52  // AttributesImpl is uniqued, these should not be available.
53  AttributeImpl(const AttributeImpl &) = delete;
54  AttributeImpl &operator=(const AttributeImpl &) = delete;
55 
56  virtual ~AttributeImpl();
57 
58  bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
59  bool isIntAttribute() const { return KindID == IntAttrEntry; }
60  bool isStringAttribute() const { return KindID == StringAttrEntry; }
61  bool isTypeAttribute() const { return KindID == TypeAttrEntry; }
62 
63  bool hasAttribute(Attribute::AttrKind A) const;
64  bool hasAttribute(StringRef Kind) const;
65 
67  uint64_t getValueAsInt() const;
68 
69  StringRef getKindAsString() const;
71 
72  Type *getValueAsType() const;
73 
74  /// Used when sorting the attributes.
75  bool operator<(const AttributeImpl &AI) const;
76 
77  void Profile(FoldingSetNodeID &ID) const {
78  if (isEnumAttribute())
79  Profile(ID, getKindAsEnum(), static_cast<uint64_t>(0));
80  else if (isIntAttribute())
82  else if (isStringAttribute())
84  else
86  }
87 
89  uint64_t Val) {
90  ID.AddInteger(Kind);
91  if (Val) ID.AddInteger(Val);
92  }
93 
94  static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
95  ID.AddString(Kind);
96  if (!Values.empty()) ID.AddString(Values);
97  }
98 
100  Type *Ty) {
101  ID.AddInteger(Kind);
102  ID.AddPointer(Ty);
103  }
104 };
105 
106 //===----------------------------------------------------------------------===//
107 /// \class
108 /// A set of classes that contain the value of the
109 /// attribute object. There are three main categories: enum attribute entries,
110 /// represented by Attribute::AttrKind; alignment attribute entries; and string
111 /// attribute enties, which are for target-dependent attributes.
112 
114  virtual void anchor();
115 
117 
118 protected:
120  : AttributeImpl(ID), Kind(Kind) {}
121 
122 public:
124  : AttributeImpl(EnumAttrEntry), Kind(Kind) {}
125 
127 };
128 
130  uint64_t Val;
131 
132  void anchor() override;
133 
134 public:
136  : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
137  assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
138  Kind == Attribute::Dereferenceable ||
139  Kind == Attribute::DereferenceableOrNull ||
140  Kind == Attribute::AllocSize) &&
141  "Wrong kind for int attribute!");
142  }
143 
144  uint64_t getValue() const { return Val; }
145 };
146 
148  virtual void anchor();
149 
150  std::string Kind;
151  std::string Val;
152 
153 public:
155  : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
156 
157  StringRef getStringKind() const { return Kind; }
158  StringRef getStringValue() const { return Val; }
159 };
160 
162  virtual void anchor();
163 
164  Type *Ty;
165 
166 public:
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
176 /// element: function, return type, or parameter.
177 class AttributeSetNode final
178  : public FoldingSetNode,
179  private TrailingObjects<AttributeSetNode, Attribute> {
180  friend TrailingObjects;
181 
182  unsigned NumAttrs; ///< Number of attributes in this node.
183  /// Bitset with a bit for each available attribute Attribute::AttrKind.
184  uint8_t AvailableAttrs[12] = {};
185 
187 
188 public:
189  // AttributesSetNode is uniqued, these should not be available.
190  AttributeSetNode(const AttributeSetNode &) = delete;
191  AttributeSetNode &operator=(const AttributeSetNode &) = delete;
192 
193  void operator delete(void *p) { ::operator delete(p); }
194 
195  static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B);
196 
198 
199  /// Return the number of attributes this AttributeList contains.
200  unsigned getNumAttributes() const { return NumAttrs; }
201 
203  return AvailableAttrs[Kind / 8] & ((uint64_t)1) << (Kind % 8);
204  }
205  bool hasAttribute(StringRef Kind) const;
206  bool hasAttributes() const { return NumAttrs != 0; }
207 
209  Attribute getAttribute(StringRef Kind) const;
210 
211  unsigned getAlignment() const;
212  unsigned getStackAlignment() const;
213  uint64_t getDereferenceableBytes() const;
214  uint64_t getDereferenceableOrNullBytes() const;
215  std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
216  std::string getAsString(bool InAttrGrp) const;
217  Type *getByValType() const;
218 
219  using iterator = const Attribute *;
220 
221  iterator begin() const { return getTrailingObjects<Attribute>(); }
222  iterator end() const { return begin() + NumAttrs; }
223 
224  void Profile(FoldingSetNodeID &ID) const {
225  Profile(ID, makeArrayRef(begin(), end()));
226  }
227 
228  static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
229  for (const auto &Attr : AttrList)
230  Attr.Profile(ID);
231  }
232 };
233 
234 using IndexAttrPair = std::pair<unsigned, AttributeSet>;
235 
236 //===----------------------------------------------------------------------===//
237 /// \class
238 /// This class represents a set of attributes that apply to the function,
239 /// return type, and parameters.
240 class AttributeListImpl final
241  : public FoldingSetNode,
242  private TrailingObjects<AttributeListImpl, AttributeSet> {
243  friend class AttributeList;
244  friend TrailingObjects;
245 
246 private:
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] = {};
251 
252  // Helper fn for TrailingObjects class.
253  size_t numTrailingObjects(OverloadToken<AttributeSet>) { return NumAttrSets; }
254 
255 public:
257 
258  // AttributesSetImpt is uniqued, these should not be available.
259  AttributeListImpl(const AttributeListImpl &) = delete;
260  AttributeListImpl &operator=(const AttributeListImpl &) = delete;
261 
262  void operator delete(void *p) { ::operator delete(p); }
263 
264  /// Get the context that created this AttributeListImpl.
266 
267  /// Return true if the AttributeSet or the FunctionIndex has an
268  /// enum attribute of the given kind.
270  return AvailableFunctionAttrs[Kind / 8] & ((uint64_t)1) << (Kind % 8);
271  }
272 
273  using iterator = const AttributeSet *;
274 
275  iterator begin() const { return getTrailingObjects<AttributeSet>(); }
276  iterator end() const { return begin() + NumAttrSets; }
277 
278  void Profile(FoldingSetNodeID &ID) const;
279  static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeSet> Nodes);
280 
281  void dump() const;
282 };
283 
284 } // end namespace llvm
285 
286 #endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H
uint64_t CallInst * C
void AddPointer(const void *Ptr)
Add* - Add various data types to Bit data.
Definition: FoldingSet.cpp:51
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:233
bool hasAttribute(Attribute::AttrKind A) const
Definition: Attributes.cpp:512
LLVMContext & Context
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:224
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void Profile(FoldingSetNodeID &ID) const
Definition: AttributeImpl.h:77
iterator begin() const
StringAttributeImpl(StringRef Kind, StringRef Val=StringRef())
static void Profile(FoldingSetNodeID &ID, ArrayRef< Attribute > AttrList)
static uint32_t getAlignment(const MCSectionCOFF &Sec)
Type * getValueAsType() const
Definition: Attributes.cpp:542
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:450
unsigned getNumAttributes() const
Return the number of attributes this AttributeList contains.
bool isIntAttribute() const
Definition: AttributeImpl.h:59
This file contains the simple types necessary to represent the attributes associated with functions a...
void AddInteger(signed I)
Definition: FoldingSet.cpp:60
void Profile(FoldingSetNodeID &ID) const
uint64_t getValue() const
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:140
EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
EnumAttributeImpl(Attribute::AttrKind Kind)
iterator end() const
StringRef getStringValue() const
StringRef getStringKind() const
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
std::pair< unsigned, AttributeSet > IndexAttrPair
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
Type * getTypeValue() const
static xmlAttrPtr getAttribute(xmlNodePtr Node, const unsigned char *AttributeName)
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:305
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
bool isTypeAttribute() const
Definition: AttributeImpl.h:61
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
bool isStringAttribute() const
Definition: AttributeImpl.h:60
See the file comment for details on the usage of the TrailingObjects type.
Attribute::AttrKind getKindAsEnum() const
Definition: Attributes.cpp:522
iterator end() const
bool operator<(const AttributeImpl &AI) const
Used when sorting the attributes.
Definition: Attributes.cpp:547
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the AttributeSet or the FunctionIndex has an enum attribute of the given kind...
StringRef getKindAsString() const
Definition: Attributes.cpp:532
static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, Type *Ty)
Definition: AttributeImpl.h:99
bool isEnumAttribute() const
Definition: AttributeImpl.h:58
AttributeImpl(AttrEntryKind KindID)
Definition: AttributeImpl.h:49
bool hasAttribute(Attribute::AttrKind Kind) const
Attribute::AttrKind getEnumKind() const
static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, uint64_t Val)
Definition: AttributeImpl.h:88
bool hasAttributes() const
Node - This class is used to maintain the singly linked bucket list in a folding set.
Definition: FoldingSet.h:135
LLVMContext & getContext()
Get the context that created this AttributeListImpl.
void AddString(StringRef String)
Definition: FoldingSet.cpp:86
uint64_t getValueAsInt() const
Definition: Attributes.cpp:527
StringRef getValueAsString() const
Definition: Attributes.cpp:537
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
virtual ~AttributeImpl()
This header defines support for implementing classes that have some trailing object (or arrays of obj...
iterator begin() const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
TypeAttributeImpl(Attribute::AttrKind Kind, Type *Ty)
static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values)
Definition: AttributeImpl.h:94
AttributeImpl & operator=(const AttributeImpl &)=delete
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results...
Definition: Attributes.h:70