]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/IR/AttributeImpl.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r304149, 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 <cassert>
25 #include <cstddef>
26 #include <cstdint>
27 #include <string>
28 #include <utility>
29
30 namespace llvm {
31
32 class LLVMContext;
33
34 //===----------------------------------------------------------------------===//
35 /// \class
36 /// \brief 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:
42   enum AttrEntryKind {
43     EnumAttrEntry,
44     IntAttrEntry,
45     StringAttrEntry
46   };
47
48   AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
49
50 public:
51   // AttributesImpl is uniqued, these should not be available.
52   AttributeImpl(const AttributeImpl &) = delete;
53   AttributeImpl &operator=(const AttributeImpl &) = delete;
54
55   virtual ~AttributeImpl();
56
57   bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
58   bool isIntAttribute() const { return KindID == IntAttrEntry; }
59   bool isStringAttribute() const { return KindID == StringAttrEntry; }
60
61   bool hasAttribute(Attribute::AttrKind A) const;
62   bool hasAttribute(StringRef Kind) const;
63
64   Attribute::AttrKind getKindAsEnum() const;
65   uint64_t getValueAsInt() const;
66
67   StringRef getKindAsString() const;
68   StringRef getValueAsString() const;
69
70   /// \brief Used when sorting the attributes.
71   bool operator<(const AttributeImpl &AI) const;
72
73   void Profile(FoldingSetNodeID &ID) const {
74     if (isEnumAttribute())
75       Profile(ID, getKindAsEnum(), 0);
76     else if (isIntAttribute())
77       Profile(ID, getKindAsEnum(), getValueAsInt());
78     else
79       Profile(ID, getKindAsString(), getValueAsString());
80   }
81
82   static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
83                       uint64_t Val) {
84     ID.AddInteger(Kind);
85     if (Val) ID.AddInteger(Val);
86   }
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   uint64_t Val;
118
119   void anchor() override;
120
121 public:
122   IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
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
134 class StringAttributeImpl : public AttributeImpl {
135   virtual void anchor();
136   std::string Kind;
137   std::string Val;
138
139 public:
140   StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
141       : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
142
143   StringRef getStringKind() const { return Kind; }
144   StringRef getStringValue() const { return Val; }
145 };
146
147 //===----------------------------------------------------------------------===//
148 /// \class
149 /// \brief This class represents a group of attributes that apply to one
150 /// element: function, return type, or parameter.
151 class AttributeSetNode final
152     : public FoldingSetNode,
153       private TrailingObjects<AttributeSetNode, Attribute> {
154   friend TrailingObjects;
155
156   /// Bitset with a bit for each available attribute Attribute::AttrKind.
157   uint64_t AvailableAttrs;
158   unsigned NumAttrs; ///< Number of attributes in this node.
159
160   AttributeSetNode(ArrayRef<Attribute> Attrs);
161
162 public:
163   // AttributesSetNode is uniqued, these should not be available.
164   AttributeSetNode(const AttributeSetNode &) = delete;
165   AttributeSetNode &operator=(const AttributeSetNode &) = delete;
166
167   void operator delete(void *p) { ::operator delete(p); }
168
169   static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B);
170
171   static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
172
173   /// \brief Return the number of attributes this AttributeList contains.
174   unsigned getNumAttributes() const { return NumAttrs; }
175
176   bool hasAttribute(Attribute::AttrKind Kind) const {
177     return AvailableAttrs & ((uint64_t)1) << Kind;
178   }
179   bool hasAttribute(StringRef Kind) const;
180   bool hasAttributes() const { return NumAttrs != 0; }
181
182   Attribute getAttribute(Attribute::AttrKind Kind) const;
183   Attribute getAttribute(StringRef Kind) const;
184
185   unsigned getAlignment() const;
186   unsigned getStackAlignment() const;
187   uint64_t getDereferenceableBytes() const;
188   uint64_t getDereferenceableOrNullBytes() const;
189   std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
190   std::string getAsString(bool InAttrGrp) const;
191
192   using iterator = const Attribute *;
193
194   iterator begin() const { return getTrailingObjects<Attribute>(); }
195   iterator end() const { return begin() + NumAttrs; }
196
197   void Profile(FoldingSetNodeID &ID) const {
198     Profile(ID, makeArrayRef(begin(), end()));
199   }
200
201   static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
202     for (const auto &Attr : AttrList)
203       Attr.Profile(ID);
204   }
205 };
206
207 using IndexAttrPair = std::pair<unsigned, AttributeSet>;
208
209 //===----------------------------------------------------------------------===//
210 /// \class
211 /// \brief This class represents a set of attributes that apply to the function,
212 /// return type, and parameters.
213 class AttributeListImpl final
214     : public FoldingSetNode,
215       private TrailingObjects<AttributeListImpl, AttributeSet> {
216   friend class AttributeList;
217   friend TrailingObjects;
218
219 private:
220   /// Bitset with a bit for each available attribute Attribute::AttrKind.
221   uint64_t AvailableFunctionAttrs;
222   LLVMContext &Context;
223   unsigned NumAttrSets; ///< Number of entries in this set.
224
225   // Helper fn for TrailingObjects class.
226   size_t numTrailingObjects(OverloadToken<AttributeSet>) { return NumAttrSets; }
227
228 public:
229   AttributeListImpl(LLVMContext &C, ArrayRef<AttributeSet> Sets);
230
231   // AttributesSetImpt is uniqued, these should not be available.
232   AttributeListImpl(const AttributeListImpl &) = delete;
233   AttributeListImpl &operator=(const AttributeListImpl &) = delete;
234
235   void operator delete(void *p) { ::operator delete(p); }
236
237   /// \brief Get the context that created this AttributeListImpl.
238   LLVMContext &getContext() { return Context; }
239
240   /// \brief Return true if the AttributeSet or the FunctionIndex has an
241   /// enum attribute of the given kind.
242   bool hasFnAttribute(Attribute::AttrKind Kind) const {
243     return AvailableFunctionAttrs & ((uint64_t)1) << Kind;
244   }
245
246   typedef const AttributeSet *iterator;
247   iterator begin() const { return getTrailingObjects<AttributeSet>(); }
248   iterator end() const { return begin() + NumAttrSets; }
249
250   void Profile(FoldingSetNodeID &ID) const;
251   static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeSet> Nodes);
252
253   void dump() const;
254 };
255
256 } // end namespace llvm
257
258 #endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H