LLVM  9.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 
33 //===----------------------------------------------------------------------===//
34 /// \class
35 /// This class represents a single, uniqued attribute. That attribute
36 /// could be a single enum, a tuple, or a string.
37 class AttributeImpl : public FoldingSetNode {
38  unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
39 
40 protected:
45  };
46 
47  AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
48 
49 public:
50  // AttributesImpl is uniqued, these should not be available.
51  AttributeImpl(const AttributeImpl &) = delete;
52  AttributeImpl &operator=(const AttributeImpl &) = delete;
53 
54  virtual ~AttributeImpl();
55 
56  bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
57  bool isIntAttribute() const { return KindID == IntAttrEntry; }
58  bool isStringAttribute() const { return KindID == StringAttrEntry; }
59 
60  bool hasAttribute(Attribute::AttrKind A) const;
61  bool hasAttribute(StringRef Kind) const;
62 
64  uint64_t getValueAsInt() const;
65 
66  StringRef getKindAsString() const;
68 
69  /// Used when sorting the attributes.
70  bool operator<(const AttributeImpl &AI) const;
71 
72  void Profile(FoldingSetNodeID &ID) const {
73  if (isEnumAttribute())
74  Profile(ID, getKindAsEnum(), 0);
75  else if (isIntAttribute())
77  else
79  }
80 
82  uint64_t Val) {
83  ID.AddInteger(Kind);
84  if (Val) ID.AddInteger(Val);
85  }
86 
87  static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
88  ID.AddString(Kind);
89  if (!Values.empty()) ID.AddString(Values);
90  }
91 };
92 
93 //===----------------------------------------------------------------------===//
94 /// \class
95 /// A set of classes that contain the value of the
96 /// attribute object. There are three main categories: enum attribute entries,
97 /// represented by Attribute::AttrKind; alignment attribute entries; and string
98 /// attribute enties, which are for target-dependent attributes.
99 
101  virtual void anchor();
102 
104 
105 protected:
107  : AttributeImpl(ID), Kind(Kind) {}
108 
109 public:
111  : AttributeImpl(EnumAttrEntry), Kind(Kind) {}
112 
114 };
115 
117  uint64_t Val;
118 
119  void anchor() override;
120 
121 public:
123  : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
124  assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
125  Kind == Attribute::Dereferenceable ||
126  Kind == Attribute::DereferenceableOrNull ||
127  Kind == Attribute::AllocSize) &&
128  "Wrong kind for int attribute!");
129  }
130 
131  uint64_t getValue() const { return Val; }
132 };
133 
135  virtual void anchor();
136 
137  std::string Kind;
138  std::string Val;
139 
140 public:
142  : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
143 
144  StringRef getStringKind() const { return Kind; }
145  StringRef getStringValue() const { return Val; }
146 };
147 
148 //===----------------------------------------------------------------------===//
149 /// \class
150 /// This class represents a group of attributes that apply to one
151 /// element: function, return type, or parameter.
152 class AttributeSetNode final
153  : public FoldingSetNode,
154  private TrailingObjects<AttributeSetNode, Attribute> {
155  friend TrailingObjects;
156 
157  /// Bitset with a bit for each available attribute Attribute::AttrKind.
158  uint64_t AvailableAttrs;
159  unsigned NumAttrs; ///< Number of attributes in this node.
160 
162 
163 public:
164  // AttributesSetNode is uniqued, these should not be available.
165  AttributeSetNode(const AttributeSetNode &) = delete;
166  AttributeSetNode &operator=(const AttributeSetNode &) = delete;
167 
168  void operator delete(void *p) { ::operator delete(p); }
169 
170  static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B);
171 
173 
174  /// Return the number of attributes this AttributeList contains.
175  unsigned getNumAttributes() const { return NumAttrs; }
176 
178  return AvailableAttrs & ((uint64_t)1) << Kind;
179  }
180  bool hasAttribute(StringRef Kind) const;
181  bool hasAttributes() const { return NumAttrs != 0; }
182 
184  Attribute getAttribute(StringRef Kind) const;
185 
186  unsigned getAlignment() const;
187  unsigned getStackAlignment() const;
188  uint64_t getDereferenceableBytes() const;
189  uint64_t getDereferenceableOrNullBytes() const;
190  std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
191  std::string getAsString(bool InAttrGrp) const;
192 
193  using iterator = const Attribute *;
194 
195  iterator begin() const { return getTrailingObjects<Attribute>(); }
196  iterator end() const { return begin() + NumAttrs; }
197 
198  void Profile(FoldingSetNodeID &ID) const {
199  Profile(ID, makeArrayRef(begin(), end()));
200  }
201 
202  static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
203  for (const auto &Attr : AttrList)
204  Attr.Profile(ID);
205  }
206 };
207 
208 using IndexAttrPair = std::pair<unsigned, AttributeSet>;
209 
210 //===----------------------------------------------------------------------===//
211 /// \class
212 /// This class represents a set of attributes that apply to the function,
213 /// return type, and parameters.
214 class AttributeListImpl final
215  : public FoldingSetNode,
216  private TrailingObjects<AttributeListImpl, AttributeSet> {
217  friend class AttributeList;
218  friend TrailingObjects;
219 
220 private:
221  /// Bitset with a bit for each available attribute Attribute::AttrKind.
222  uint64_t AvailableFunctionAttrs;
224  unsigned NumAttrSets; ///< Number of entries in this set.
225 
226  // Helper fn for TrailingObjects class.
227  size_t numTrailingObjects(OverloadToken<AttributeSet>) { return NumAttrSets; }
228 
229 public:
231 
232  // AttributesSetImpt is uniqued, these should not be available.
233  AttributeListImpl(const AttributeListImpl &) = delete;
234  AttributeListImpl &operator=(const AttributeListImpl &) = delete;
235 
236  void operator delete(void *p) { ::operator delete(p); }
237 
238  /// Get the context that created this AttributeListImpl.
240 
241  /// Return true if the AttributeSet or the FunctionIndex has an
242  /// enum attribute of the given kind.
244  return AvailableFunctionAttrs & ((uint64_t)1) << Kind;
245  }
246 
247  using iterator = const AttributeSet *;
248 
249  iterator begin() const { return getTrailingObjects<AttributeSet>(); }
250  iterator end() const { return begin() + NumAttrSets; }
251 
252  void Profile(FoldingSetNodeID &ID) const;
253  static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeSet> Nodes);
254 
255  void dump() const;
256 };
257 
258 } // end namespace llvm
259 
260 #endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H
uint64_t CallInst * C
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:233
bool hasAttribute(Attribute::AttrKind A) const
Definition: Attributes.cpp:454
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:72
iterator begin() const
StringAttributeImpl(StringRef Kind, StringRef Val=StringRef())
static void Profile(FoldingSetNodeID &ID, ArrayRef< Attribute > AttrList)
static uint32_t getAlignment(const MCSectionCOFF &Sec)
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:57
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:126
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.
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)
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:58
See the file comment for details on the usage of the TrailingObjects type.
Attribute::AttrKind getKindAsEnum() const
Definition: Attributes.cpp:464
iterator end() const
bool operator<(const AttributeImpl &AI) const
Used when sorting the attributes.
Definition: Attributes.cpp:484
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:474
bool isEnumAttribute() const
Definition: AttributeImpl.h:56
AttributeImpl(AttrEntryKind KindID)
Definition: AttributeImpl.h:47
bool hasAttribute(Attribute::AttrKind Kind) const
Attribute::AttrKind getEnumKind() const
static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, uint64_t Val)
Definition: AttributeImpl.h:81
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:469
StringRef getValueAsString() const
Definition: Attributes.cpp:479
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
static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values)
Definition: AttributeImpl.h:87
AttributeImpl & operator=(const AttributeImpl &)=delete
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results...
Definition: Attributes.h:69