]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/IR/AttributeImpl.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r301441, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / IR / AttributeImpl.h
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/ArrayRef.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/IR/Attributes.h"
23 #include "llvm/Support/TrailingObjects.h"
24 #include <algorithm>
25 #include <cassert>
26 #include <climits>
27 #include <cstddef>
28 #include <cstdint>
29 #include <string>
30 #include <utility>
31
32 namespace llvm {
33
34 class LLVMContext;
35
36 //===----------------------------------------------------------------------===//
37 /// \class
38 /// \brief This class represents a single, uniqued attribute. That attribute
39 /// could be a single enum, a tuple, or a string.
40 class AttributeImpl : public FoldingSetNode {
41   unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
42
43 protected:
44   enum AttrEntryKind {
45     EnumAttrEntry,
46     IntAttrEntry,
47     StringAttrEntry
48   };
49
50   AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
51
52 public:
53   // AttributesImpl is uniqued, these should not be available.
54   AttributeImpl(const AttributeImpl &) = delete;
55   AttributeImpl &operator=(const AttributeImpl &) = delete;
56
57   virtual ~AttributeImpl();
58
59   bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
60   bool isIntAttribute() const { return KindID == IntAttrEntry; }
61   bool isStringAttribute() const { return KindID == StringAttrEntry; }
62
63   bool hasAttribute(Attribute::AttrKind A) const;
64   bool hasAttribute(StringRef Kind) const;
65
66   Attribute::AttrKind getKindAsEnum() const;
67   uint64_t getValueAsInt() const;
68
69   StringRef getKindAsString() const;
70   StringRef getValueAsString() const;
71
72   /// \brief Used when sorting the attributes.
73   bool operator<(const AttributeImpl &AI) const;
74
75   void Profile(FoldingSetNodeID &ID) const {
76     if (isEnumAttribute())
77       Profile(ID, getKindAsEnum(), 0);
78     else if (isIntAttribute())
79       Profile(ID, getKindAsEnum(), getValueAsInt());
80     else
81       Profile(ID, getKindAsString(), getValueAsString());
82   }
83   static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
84                       uint64_t Val) {
85     ID.AddInteger(Kind);
86     if (Val) ID.AddInteger(Val);
87   }
88   static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
89     ID.AddString(Kind);
90     if (!Values.empty()) ID.AddString(Values);
91   }
92 };
93
94 //===----------------------------------------------------------------------===//
95 /// \class
96 /// \brief A set of classes that contain the value of the
97 /// attribute object. There are three main categories: enum attribute entries,
98 /// represented by Attribute::AttrKind; alignment attribute entries; and string
99 /// attribute enties, which are for target-dependent attributes.
100
101 class EnumAttributeImpl : public AttributeImpl {
102   virtual void anchor();
103   Attribute::AttrKind Kind;
104
105 protected:
106   EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
107       : AttributeImpl(ID), Kind(Kind) {}
108
109 public:
110   EnumAttributeImpl(Attribute::AttrKind Kind)
111       : AttributeImpl(EnumAttrEntry), Kind(Kind) {}
112
113   Attribute::AttrKind getEnumKind() const { return Kind; }
114 };
115
116 class IntAttributeImpl : public EnumAttributeImpl {
117   void anchor() override;
118   uint64_t Val;
119
120 public:
121   IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
122       : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
123     assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
124             Kind == Attribute::Dereferenceable ||
125             Kind == Attribute::DereferenceableOrNull ||
126             Kind == Attribute::AllocSize) &&
127            "Wrong kind for int attribute!");
128   }
129
130   uint64_t getValue() const { return Val; }
131 };
132
133 class StringAttributeImpl : public AttributeImpl {
134   virtual void anchor();
135   std::string Kind;
136   std::string Val;
137
138 public:
139   StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
140       : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
141
142   StringRef getStringKind() const { return Kind; }
143   StringRef getStringValue() const { return Val; }
144 };
145
146 //===----------------------------------------------------------------------===//
147 /// \class
148 /// \brief This class represents a group of attributes that apply to one
149 /// element: function, return type, or parameter.
150 class AttributeSetNode final
151     : public FoldingSetNode,
152       private TrailingObjects<AttributeSetNode, Attribute> {
153   friend TrailingObjects;
154
155   /// Bitset with a bit for each available attribute Attribute::AttrKind.
156   uint64_t AvailableAttrs;
157   unsigned NumAttrs; ///< Number of attributes in this node.
158
159   AttributeSetNode(ArrayRef<Attribute> Attrs);
160
161 public:
162   // AttributesSetNode is uniqued, these should not be available.
163   AttributeSetNode(const AttributeSetNode &) = delete;
164   AttributeSetNode &operator=(const AttributeSetNode &) = delete;
165
166   void operator delete(void *p) { ::operator delete(p); }
167
168   static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B);
169
170   static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
171
172   /// \brief Return the number of attributes this AttributeList contains.
173   unsigned getNumAttributes() const { return NumAttrs; }
174
175   bool hasAttribute(Attribute::AttrKind Kind) const {
176     return AvailableAttrs & ((uint64_t)1) << Kind;
177   }
178   bool hasAttribute(StringRef Kind) const;
179   bool hasAttributes() const { return NumAttrs != 0; }
180
181   Attribute getAttribute(Attribute::AttrKind Kind) const;
182   Attribute getAttribute(StringRef Kind) const;
183
184   unsigned getAlignment() const;
185   unsigned getStackAlignment() const;
186   uint64_t getDereferenceableBytes() const;
187   uint64_t getDereferenceableOrNullBytes() const;
188   std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
189   std::string getAsString(bool InAttrGrp) const;
190
191   typedef const Attribute *iterator;
192   iterator begin() const { return getTrailingObjects<Attribute>(); }
193   iterator end() const { return begin() + NumAttrs; }
194
195   void Profile(FoldingSetNodeID &ID) const {
196     Profile(ID, makeArrayRef(begin(), end()));
197   }
198   static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
199     for (const auto &Attr : AttrList)
200       Attr.Profile(ID);
201   }
202 };
203
204 typedef std::pair<unsigned, AttributeSet> IndexAttrPair;
205
206 //===----------------------------------------------------------------------===//
207 /// \class
208 /// \brief This class represents a set of attributes that apply to the function,
209 /// return type, and parameters.
210 class AttributeListImpl final
211     : public FoldingSetNode,
212       private TrailingObjects<AttributeListImpl, IndexAttrPair> {
213   friend class AttributeList;
214   friend TrailingObjects;
215
216 private:
217   LLVMContext &Context;
218   unsigned NumSlots; ///< Number of entries in this set.
219   /// Bitset with a bit for each available attribute Attribute::AttrKind.
220   uint64_t AvailableFunctionAttrs;
221
222   // Helper fn for TrailingObjects class.
223   size_t numTrailingObjects(OverloadToken<IndexAttrPair>) { return NumSlots; }
224
225   /// \brief Return a pointer to the IndexAttrPair for the specified slot.
226   const IndexAttrPair *getSlotPair(unsigned Slot) const {
227     return getTrailingObjects<IndexAttrPair>() + Slot;
228   }
229
230 public:
231   AttributeListImpl(LLVMContext &C,
232                     ArrayRef<std::pair<unsigned, AttributeSet>> Slots);
233
234   // AttributesSetImpt is uniqued, these should not be available.
235   AttributeListImpl(const AttributeListImpl &) = delete;
236   AttributeListImpl &operator=(const AttributeListImpl &) = delete;
237
238   void operator delete(void *p) { ::operator delete(p); }
239
240   /// \brief Get the context that created this AttributeListImpl.
241   LLVMContext &getContext() { return Context; }
242
243   /// \brief Return the number of slots used in this attribute list. This is
244   /// the number of arguments that have an attribute set on them (including the
245   /// function itself).
246   unsigned getNumSlots() const { return NumSlots; }
247
248   /// \brief Get the index of the given "slot" in the AttrNodes list. This index
249   /// is the index of the return, parameter, or function object that the
250   /// attributes are applied to, not the index into the AttrNodes list where the
251   /// attributes reside.
252   unsigned getSlotIndex(unsigned Slot) const {
253     return getSlotPair(Slot)->first;
254   }
255
256   /// \brief Retrieve the attribute set node for the given "slot" in the
257   /// AttrNode list.
258   AttributeSet getSlotAttributes(unsigned Slot) const {
259     return getSlotPair(Slot)->second;
260   }
261
262   /// \brief Return true if the AttributeSet or the FunctionIndex has an
263   /// enum attribute of the given kind.
264   bool hasFnAttribute(Attribute::AttrKind Kind) const {
265     return AvailableFunctionAttrs & ((uint64_t)1) << Kind;
266   }
267
268   typedef AttributeSet::iterator iterator;
269   iterator begin(unsigned Slot) const {
270     return getSlotAttributes(Slot).begin();
271   }
272   iterator end(unsigned Slot) const { return getSlotAttributes(Slot).end(); }
273
274   void Profile(FoldingSetNodeID &ID) const;
275   static void Profile(FoldingSetNodeID &ID,
276                       ArrayRef<std::pair<unsigned, AttributeSet>> Nodes);
277
278   void dump() const;
279 };
280
281 } // end namespace llvm
282
283 #endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H