LLVM  3.7.0
AttributeImpl.h
Go to the documentation of this file.
1 //===-- AttributeImpl.h - Attribute Internals -------------------*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief This file defines various helper methods and classes used by
12 /// LLVMContextImpl for creating and managing attributes.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_LIB_IR_ATTRIBUTEIMPL_H
17 #define LLVM_LIB_IR_ATTRIBUTEIMPL_H
18 
19 #include "llvm/ADT/FoldingSet.h"
20 #include "llvm/IR/Attributes.h"
21 #include <string>
22 
23 namespace llvm {
24 
25 class Constant;
26 class LLVMContext;
27 
28 //===----------------------------------------------------------------------===//
29 /// \class
30 /// \brief This class represents a single, uniqued attribute. That attribute
31 /// could be a single enum, a tuple, or a string.
32 class AttributeImpl : public FoldingSetNode {
33  unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
34 
35  // AttributesImpl is uniqued, these should not be publicly available.
36  void operator=(const AttributeImpl &) = delete;
37  AttributeImpl(const AttributeImpl &) = delete;
38 
39 protected:
44  };
45 
46  AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
47 
48 public:
49  virtual ~AttributeImpl();
50 
51  bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
52  bool isIntAttribute() const { return KindID == IntAttrEntry; }
53  bool isStringAttribute() const { return KindID == StringAttrEntry; }
54 
56  bool hasAttribute(StringRef Kind) const;
57 
59  uint64_t getValueAsInt() const;
60 
61  StringRef getKindAsString() const;
63 
64  /// \brief Used when sorting the attributes.
65  bool operator<(const AttributeImpl &AI) const;
66 
67  void Profile(FoldingSetNodeID &ID) const {
68  if (isEnumAttribute())
69  Profile(ID, getKindAsEnum(), 0);
70  else if (isIntAttribute())
72  else
74  }
76  uint64_t Val) {
77  ID.AddInteger(Kind);
78  if (Val) ID.AddInteger(Val);
79  }
81  ID.AddString(Kind);
82  if (!Values.empty()) ID.AddString(Values);
83  }
84 
85  // FIXME: Remove this!
86  static uint64_t getAttrMask(Attribute::AttrKind Val);
87 };
88 
89 //===----------------------------------------------------------------------===//
90 /// \class
91 /// \brief A set of classes that contain the value of the
92 /// attribute object. There are three main categories: enum attribute entries,
93 /// represented by Attribute::AttrKind; alignment attribute entries; and string
94 /// attribute enties, which are for target-dependent attributes.
95 
97  virtual void anchor();
99 
100 protected:
102  : AttributeImpl(ID), Kind(Kind) {}
103 
104 public:
106  : AttributeImpl(EnumAttrEntry), Kind(Kind) {}
107 
108  Attribute::AttrKind getEnumKind() const { return Kind; }
109 };
110 
112  void anchor() override;
113  uint64_t Val;
114 
115 public:
117  : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
118  assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
119  Kind == Attribute::Dereferenceable ||
121  "Wrong kind for int attribute!");
122  }
123 
124  uint64_t getValue() const { return Val; }
125 };
126 
128  virtual void anchor();
129  std::string Kind;
130  std::string Val;
131 
132 public:
134  : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
135 
136  StringRef getStringKind() const { return Kind; }
137  StringRef getStringValue() const { return Val; }
138 };
139 
140 //===----------------------------------------------------------------------===//
141 /// \class
142 /// \brief This class represents a group of attributes that apply to one
143 /// element: function, return type, or parameter.
145  unsigned NumAttrs; ///< Number of attributes in this node.
146 
147  AttributeSetNode(ArrayRef<Attribute> Attrs) : NumAttrs(Attrs.size()) {
148  // There's memory after the node where we can store the entries in.
149  std::copy(Attrs.begin(), Attrs.end(),
150  reinterpret_cast<Attribute *>(this + 1));
151  }
152 
153  // AttributesSetNode is uniqued, these should not be publicly available.
154  void operator=(const AttributeSetNode &) = delete;
155  AttributeSetNode(const AttributeSetNode &) = delete;
156 public:
157  static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
158 
160  bool hasAttribute(StringRef Kind) const;
161  bool hasAttributes() const { return NumAttrs != 0; }
162 
164  Attribute getAttribute(StringRef Kind) const;
165 
166  unsigned getAlignment() const;
167  unsigned getStackAlignment() const;
168  uint64_t getDereferenceableBytes() const;
169  uint64_t getDereferenceableOrNullBytes() const;
170  std::string getAsString(bool InAttrGrp) const;
171 
172  typedef const Attribute *iterator;
173  iterator begin() const { return reinterpret_cast<iterator>(this + 1); }
174  iterator end() const { return begin() + NumAttrs; }
175 
176  void Profile(FoldingSetNodeID &ID) const {
177  Profile(ID, makeArrayRef(begin(), end()));
178  }
179  static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
180  for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
181  AttrList[I].Profile(ID);
182  }
183 };
184 static_assert(
185  AlignOf<AttributeSetNode>::Alignment >= AlignOf<Attribute>::Alignment,
186  "Alignment is insufficient for objects appended to AttributeSetNode");
187 
188 //===----------------------------------------------------------------------===//
189 /// \class
190 /// \brief This class represents a set of attributes that apply to the function,
191 /// return type, and parameters.
193  friend class AttributeSet;
194 
195 public:
196  typedef std::pair<unsigned, AttributeSetNode*> IndexAttrPair;
197 
198 private:
199  LLVMContext &Context;
200  unsigned NumAttrs; ///< Number of entries in this set.
201 
202  /// \brief Return a pointer to the IndexAttrPair for the specified slot.
203  const IndexAttrPair *getNode(unsigned Slot) const {
204  return reinterpret_cast<const IndexAttrPair *>(this + 1) + Slot;
205  }
206 
207  // AttributesSet is uniqued, these should not be publicly available.
208  void operator=(const AttributeSetImpl &) = delete;
209  AttributeSetImpl(const AttributeSetImpl &) = delete;
210 public:
212  ArrayRef<std::pair<unsigned, AttributeSetNode *> > Attrs)
213  : Context(C), NumAttrs(Attrs.size()) {
214 
215 #ifndef NDEBUG
216  if (Attrs.size() >= 2) {
217  for (const std::pair<unsigned, AttributeSetNode *> *i = Attrs.begin() + 1,
218  *e = Attrs.end();
219  i != e; ++i) {
220  assert((i-1)->first <= i->first && "Attribute set not ordered!");
221  }
222  }
223 #endif
224  // There's memory after the node where we can store the entries in.
225  std::copy(Attrs.begin(), Attrs.end(),
226  reinterpret_cast<IndexAttrPair *>(this + 1));
227  }
228 
229  /// \brief Get the context that created this AttributeSetImpl.
230  LLVMContext &getContext() { return Context; }
231 
232  /// \brief Return the number of attributes this AttributeSet contains.
233  unsigned getNumAttributes() const { return NumAttrs; }
234 
235  /// \brief Get the index of the given "slot" in the AttrNodes list. This index
236  /// is the index of the return, parameter, or function object that the
237  /// attributes are applied to, not the index into the AttrNodes list where the
238  /// attributes reside.
239  unsigned getSlotIndex(unsigned Slot) const {
240  return getNode(Slot)->first;
241  }
242 
243  /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
244  /// \p Slot is an index into the AttrNodes list, not the index of the return /
245  /// parameter/ function which the attributes apply to.
247  return AttributeSet::get(Context, *getNode(Slot));
248  }
249 
250  /// \brief Retrieve the attribute set node for the given "slot" in the
251  /// AttrNode list.
252  AttributeSetNode *getSlotNode(unsigned Slot) const {
253  return getNode(Slot)->second;
254  }
255 
257  iterator begin(unsigned Slot) const { return getSlotNode(Slot)->begin(); }
258  iterator end(unsigned Slot) const { return getSlotNode(Slot)->end(); }
259 
260  void Profile(FoldingSetNodeID &ID) const {
261  Profile(ID, makeArrayRef(getNode(0), getNumAttributes()));
262  }
264  ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
265  for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
266  ID.AddInteger(Nodes[i].first);
267  ID.AddPointer(Nodes[i].second);
268  }
269  }
270 
271  // FIXME: This atrocity is temporary.
272  uint64_t Raw(unsigned Index) const;
273 
274  void dump() const;
275 };
276 static_assert(
277  AlignOf<AttributeSetImpl>::Alignment >=
278  AlignOf<AttributeSetImpl::IndexAttrPair>::Alignment,
279  "Alignment is insufficient for objects appended to AttributeSetImpl");
280 
281 } // end llvm namespace
282 
283 #endif
void AddPointer(const void *Ptr)
Add* - Add various data types to Bit data.
Definition: FoldingSet.cpp:52
uint64_t getDereferenceableBytes() const
Definition: Attributes.cpp:539
std::string getAsString(bool InAttrGrp) const
Definition: Attributes.cpp:553
Alignment of stack for function (3 bits) stored as log2 of alignment with +1 bias 0 means unaligned (...
Definition: Attributes.h:106
iterator begin() const
iterator end() const
Definition: ArrayRef.h:123
std::pair< unsigned, AttributeSetNode * > IndexAttrPair
AttributeSet getSlotAttributes(unsigned Slot) const
Retrieve the attributes for the given "slot" in the AttrNode list.
StringRef getStringValue() const
iterator end() const
StringAttributeImpl(StringRef Kind, StringRef Val=StringRef())
void Profile(FoldingSetNodeID &ID) const
static void Profile(FoldingSetNodeID &ID, ArrayRef< Attribute > AttrList)
bool hasAttributes() const
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:308
This file contains the simple types necessary to represent the attributes associated with functions a...
Attribute::AttrKind getKindAsEnum() const
Definition: Attributes.cpp:354
void AddInteger(signed I)
Definition: FoldingSet.cpp:60
EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
bool operator<(const AttributeImpl &AI) const
Used when sorting the attributes.
Definition: Attributes.cpp:374
EnumAttributeImpl(Attribute::AttrKind Kind)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
iterator end(unsigned Slot) const
AttributeSetNode::iterator iterator
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:134
StringRef getKindAsString() const
Definition: Attributes.cpp:364
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:297
bool hasAttribute(Attribute::AttrKind Kind) const
Definition: Attributes.cpp:497
StringRef getStringKind() const
iterator begin(unsigned Slot) const
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:41
bool isStringAttribute() const
Definition: AttributeImpl.h:53
IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
unsigned getSlotIndex(unsigned Slot) const
Get the index of the given "slot" in the AttrNodes list.
unsigned getAlignment() const
Definition: Attributes.cpp:525
uint64_t Raw(unsigned Index) const
Definition: Attributes.cpp:567
iterator begin() const
Definition: ArrayRef.h:122
StringRef getValueAsString() const
Definition: Attributes.cpp:369
bool isIntAttribute() const
Definition: AttributeImpl.h:52
bool hasAttribute(Attribute::AttrKind A) const
Definition: Attributes.cpp:344
uint64_t getValueAsInt() const
Definition: Attributes.cpp:359
Attribute::AttrKind getEnumKind() const
static uint64_t getAttrMask(Attribute::AttrKind Val)
Definition: Attributes.cpp:396
AttributeSetNode * getSlotNode(unsigned Slot) const
Retrieve the attribute set node for the given "slot" in the AttrNode list.
const Attribute * iterator
Node - This class is used to maintain the singly linked bucket list in a folding set.
Definition: FoldingSet.h:134
Alignment of parameter (5 bits) stored as log2 of alignment with +1 bias 0 means unaligned (different...
Definition: Attributes.h:67
LLVMContext & getContext()
Get the context that created this AttributeSetImpl.
unsigned getStackAlignment() const
Definition: Attributes.cpp:532
uint64_t getValue() const
AttributeImpl(AttrEntryKind KindID)
Definition: AttributeImpl.h:46
static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, uint64_t Val)
Definition: AttributeImpl.h:75
#define I(x, y, z)
Definition: MD5.cpp:54
bool isEnumAttribute() const
Definition: AttributeImpl.h:51
void size_t size
void AddString(StringRef String)
Definition: FoldingSet.cpp:87
uint64_t getDereferenceableOrNullBytes() const
Definition: Attributes.cpp:546
const ARM::ArchExtKind Kind
static void Profile(FoldingSetNodeID &ID, ArrayRef< std::pair< unsigned, AttributeSetNode * > > Nodes)
unsigned getNumAttributes() const
Return the number of attributes this AttributeSet contains.
Pointer is known to be dereferenceable.
Definition: Attributes.h:92
void Profile(FoldingSetNodeID &ID) const
AttributeSetImpl(LLVMContext &C, ArrayRef< std::pair< unsigned, AttributeSetNode * > > Attrs)
void Profile(FoldingSetNodeID &ID) const
Definition: AttributeImpl.h:67
virtual ~AttributeImpl()
Definition: Attributes.cpp:339
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
Attribute getAttribute(Attribute::AttrKind Kind) const
Definition: Attributes.cpp:511
static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values)
Definition: AttributeImpl.h:80
Pointer is either null or dereferenceable.
Definition: Attributes.h:93
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results...
Definition: Attributes.h:64
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110