Line data Source code
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 : /// 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 : /// This class represents a single, uniqued attribute. That attribute
37 : /// could be a single enum, a tuple, or a string.
38 255065 : 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 883210 : 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 0 : bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
58 0 : bool isIntAttribute() const { return KindID == IntAttrEntry; }
59 0 : 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 : /// Used when sorting the attributes.
71 : bool operator<(const AttributeImpl &AI) const;
72 :
73 117784627 : void Profile(FoldingSetNodeID &ID) const {
74 117784627 : if (isEnumAttribute())
75 24898478 : Profile(ID, getKindAsEnum(), 0);
76 92886149 : else if (isIntAttribute())
77 16819375 : Profile(ID, getKindAsEnum(), getValueAsInt());
78 : else
79 152133548 : Profile(ID, getKindAsString(), getValueAsString());
80 117784627 : }
81 :
82 : static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
83 : uint64_t Val) {
84 41717853 : ID.AddInteger(Kind);
85 16819375 : if (Val) ID.AddInteger(Val);
86 : }
87 :
88 : static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
89 76066774 : ID.AddString(Kind);
90 76066774 : if (!Values.empty()) ID.AddString(Values);
91 : }
92 : };
93 :
94 : //===----------------------------------------------------------------------===//
95 : /// \class
96 : /// 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 :
104 : Attribute::AttrKind Kind;
105 :
106 : protected:
107 : EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
108 72642 : : AttributeImpl(ID), Kind(Kind) {}
109 :
110 : public:
111 : EnumAttributeImpl(Attribute::AttrKind Kind)
112 346736 : : AttributeImpl(EnumAttrEntry), Kind(Kind) {}
113 :
114 0 : Attribute::AttrKind getEnumKind() const { return Kind; }
115 : };
116 :
117 : class IntAttributeImpl : public EnumAttributeImpl {
118 : uint64_t Val;
119 :
120 : void anchor() override;
121 :
122 : public:
123 : IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
124 72642 : : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
125 : assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
126 : Kind == Attribute::Dereferenceable ||
127 : Kind == Attribute::DereferenceableOrNull ||
128 : Kind == Attribute::AllocSize) &&
129 : "Wrong kind for int attribute!");
130 : }
131 :
132 0 : uint64_t getValue() const { return Val; }
133 : };
134 :
135 : class StringAttributeImpl : public AttributeImpl {
136 : virtual void anchor();
137 :
138 : std::string Kind;
139 : std::string Val;
140 :
141 : public:
142 231916 : StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
143 463832 : : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
144 :
145 : StringRef getStringKind() const { return Kind; }
146 : StringRef getStringValue() const { return Val; }
147 : };
148 :
149 : //===----------------------------------------------------------------------===//
150 : /// \class
151 : /// This class represents a group of attributes that apply to one
152 : /// element: function, return type, or parameter.
153 : class AttributeSetNode final
154 : : public FoldingSetNode,
155 : private TrailingObjects<AttributeSetNode, Attribute> {
156 : friend TrailingObjects;
157 :
158 : /// Bitset with a bit for each available attribute Attribute::AttrKind.
159 : uint64_t AvailableAttrs;
160 : unsigned NumAttrs; ///< Number of attributes in this node.
161 :
162 : AttributeSetNode(ArrayRef<Attribute> Attrs);
163 :
164 : public:
165 : // AttributesSetNode is uniqued, these should not be available.
166 : AttributeSetNode(const AttributeSetNode &) = delete;
167 : AttributeSetNode &operator=(const AttributeSetNode &) = delete;
168 :
169 174532 : void operator delete(void *p) { ::operator delete(p); }
170 :
171 : static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B);
172 :
173 : static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
174 :
175 : /// Return the number of attributes this AttributeList contains.
176 0 : unsigned getNumAttributes() const { return NumAttrs; }
177 :
178 0 : bool hasAttribute(Attribute::AttrKind Kind) const {
179 327145591 : return AvailableAttrs & ((uint64_t)1) << Kind;
180 : }
181 : bool hasAttribute(StringRef Kind) const;
182 : bool hasAttributes() const { return NumAttrs != 0; }
183 :
184 : Attribute getAttribute(Attribute::AttrKind Kind) const;
185 : Attribute getAttribute(StringRef Kind) const;
186 :
187 : unsigned getAlignment() const;
188 : unsigned getStackAlignment() const;
189 : uint64_t getDereferenceableBytes() const;
190 : uint64_t getDereferenceableOrNullBytes() const;
191 : std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
192 : std::string getAsString(bool InAttrGrp) const;
193 :
194 : using iterator = const Attribute *;
195 :
196 : iterator begin() const { return getTrailingObjects<Attribute>(); }
197 235716931 : iterator end() const { return begin() + NumAttrs; }
198 :
199 14032544 : void Profile(FoldingSetNodeID &ID) const {
200 : Profile(ID, makeArrayRef(begin(), end()));
201 14032544 : }
202 :
203 : static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
204 125714350 : for (const auto &Attr : AttrList)
205 111681806 : Attr.Profile(ID);
206 : }
207 : };
208 :
209 : using IndexAttrPair = std::pair<unsigned, AttributeSet>;
210 :
211 : //===----------------------------------------------------------------------===//
212 : /// \class
213 : /// This class represents a set of attributes that apply to the function,
214 : /// return type, and parameters.
215 : class AttributeListImpl final
216 : : public FoldingSetNode,
217 : private TrailingObjects<AttributeListImpl, AttributeSet> {
218 : friend class AttributeList;
219 : friend TrailingObjects;
220 :
221 : private:
222 : /// Bitset with a bit for each available attribute Attribute::AttrKind.
223 : uint64_t AvailableFunctionAttrs;
224 : LLVMContext &Context;
225 : unsigned NumAttrSets; ///< Number of entries in this set.
226 :
227 : // Helper fn for TrailingObjects class.
228 : size_t numTrailingObjects(OverloadToken<AttributeSet>) { return NumAttrSets; }
229 :
230 : public:
231 : AttributeListImpl(LLVMContext &C, ArrayRef<AttributeSet> Sets);
232 :
233 : // AttributesSetImpt is uniqued, these should not be available.
234 : AttributeListImpl(const AttributeListImpl &) = delete;
235 : AttributeListImpl &operator=(const AttributeListImpl &) = delete;
236 :
237 235510 : void operator delete(void *p) { ::operator delete(p); }
238 :
239 : /// Get the context that created this AttributeListImpl.
240 0 : LLVMContext &getContext() { return Context; }
241 :
242 : /// Return true if the AttributeSet or the FunctionIndex has an
243 : /// enum attribute of the given kind.
244 0 : bool hasFnAttribute(Attribute::AttrKind Kind) const {
245 239223557 : return AvailableFunctionAttrs & ((uint64_t)1) << Kind;
246 : }
247 :
248 : using iterator = const AttributeSet *;
249 :
250 : iterator begin() const { return getTrailingObjects<AttributeSet>(); }
251 19591754 : iterator end() const { return begin() + NumAttrSets; }
252 :
253 : void Profile(FoldingSetNodeID &ID) const;
254 : static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeSet> Nodes);
255 :
256 : void dump() const;
257 : };
258 :
259 : } // end namespace llvm
260 :
261 : #endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H
|