blob: ca7ae5cbb294581c5da35fbc61460dbe4c9d73a9 [file] [log] [blame]
Bill Wendling4607f4b2012-12-20 01:36:59 +00001//===-- AttributeImpl.h - Attribute Internals -------------------*- C++ -*-===//
Bill Wendlinge38b8042012-09-26 21:07:29 +00002//
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//===----------------------------------------------------------------------===//
Bill Wendling66e978f2012-12-20 21:28:43 +00009///
10/// \file
11/// \brief This file defines various helper methods and classes used by
12/// LLVMContextImpl for creating and managing attributes.
13///
Bill Wendlinge38b8042012-09-26 21:07:29 +000014//===----------------------------------------------------------------------===//
15
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000016#ifndef LLVM_LIB_IR_ATTRIBUTEIMPL_H
17#define LLVM_LIB_IR_ATTRIBUTEIMPL_H
Bill Wendlinge38b8042012-09-26 21:07:29 +000018
19#include "llvm/ADT/FoldingSet.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Attributes.h"
Matthias Braundaa812d2016-01-30 01:14:01 +000021#include "llvm/Support/DataTypes.h"
James Y Knightaa365b22015-08-05 22:57:34 +000022#include "llvm/Support/TrailingObjects.h"
Matthias Braund520d4e2016-01-29 22:30:30 +000023#include <climits>
Matthias Braun9c981052016-01-29 22:35:29 +000024#include <string>
Bill Wendlinge38b8042012-09-26 21:07:29 +000025
26namespace llvm {
27
Bill Wendling0cd0f7f2012-12-29 12:29:38 +000028class Constant;
Bill Wendling6ad6c3b2012-12-19 23:55:43 +000029class LLVMContext;
30
Bill Wendling66e978f2012-12-20 21:28:43 +000031//===----------------------------------------------------------------------===//
32/// \class
Benjamin Kramer741146b2013-07-11 12:13:16 +000033/// \brief This class represents a single, uniqued attribute. That attribute
34/// could be a single enum, a tuple, or a string.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000035class AttributeImpl : public FoldingSetNode {
Benjamin Kramer741146b2013-07-11 12:13:16 +000036 unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
37
38 // AttributesImpl is uniqued, these should not be publicly available.
Aaron Ballmanf9a18972015-02-15 22:54:22 +000039 void operator=(const AttributeImpl &) = delete;
40 AttributeImpl(const AttributeImpl &) = delete;
Benjamin Kramer741146b2013-07-11 12:13:16 +000041
Bill Wendling3f12ac22013-02-05 22:37:24 +000042protected:
43 enum AttrEntryKind {
44 EnumAttrEntry,
Hal Finkele15442c2014-07-18 06:51:55 +000045 IntAttrEntry,
Bill Wendling3f12ac22013-02-05 22:37:24 +000046 StringAttrEntry
47 };
Benjamin Kramer741146b2013-07-11 12:13:16 +000048
49 AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
50
Bill Wendling3f12ac22013-02-05 22:37:24 +000051public:
Alexey Samsonov49109a22013-11-18 09:31:53 +000052 virtual ~AttributeImpl();
53
Benjamin Kramer741146b2013-07-11 12:13:16 +000054 bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
Hal Finkele15442c2014-07-18 06:51:55 +000055 bool isIntAttribute() const { return KindID == IntAttrEntry; }
Benjamin Kramer741146b2013-07-11 12:13:16 +000056 bool isStringAttribute() const { return KindID == StringAttrEntry; }
Bill Wendling3f12ac22013-02-05 22:37:24 +000057
Bill Wendling9ac69f92013-01-04 20:54:35 +000058 bool hasAttribute(Attribute::AttrKind A) const;
Bill Wendling3f12ac22013-02-05 22:37:24 +000059 bool hasAttribute(StringRef Kind) const;
Bill Wendling73ea2de2012-10-08 21:47:17 +000060
Bill Wendling3f12ac22013-02-05 22:37:24 +000061 Attribute::AttrKind getKindAsEnum() const;
62 uint64_t getValueAsInt() const;
Bill Wendlingc3c714b2013-01-29 20:37:10 +000063
Bill Wendling3f12ac22013-02-05 22:37:24 +000064 StringRef getKindAsString() const;
65 StringRef getValueAsString() const;
Bill Wendlingb1d12612012-12-30 01:38:39 +000066
Bill Wendling9c2eba92013-01-31 20:59:05 +000067 /// \brief Used when sorting the attributes.
Bill Wendlingd2e493b2013-01-24 00:06:56 +000068 bool operator<(const AttributeImpl &AI) const;
69
Bill Wendlinge38b8042012-09-26 21:07:29 +000070 void Profile(FoldingSetNodeID &ID) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +000071 if (isEnumAttribute())
72 Profile(ID, getKindAsEnum(), 0);
Hal Finkele15442c2014-07-18 06:51:55 +000073 else if (isIntAttribute())
Bill Wendling3f12ac22013-02-05 22:37:24 +000074 Profile(ID, getKindAsEnum(), getValueAsInt());
75 else
76 Profile(ID, getKindAsString(), getValueAsString());
Bill Wendlinge38b8042012-09-26 21:07:29 +000077 }
Bill Wendling3f12ac22013-02-05 22:37:24 +000078 static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
79 uint64_t Val) {
80 ID.AddInteger(Kind);
81 if (Val) ID.AddInteger(Val);
82 }
83 static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
84 ID.AddString(Kind);
Bill Wendling8a0e0842013-02-28 21:17:03 +000085 if (!Values.empty()) ID.AddString(Values);
Bill Wendlingd509a662013-01-29 00:34:06 +000086 }
87
Bill Wendling3f9fcd42013-02-01 00:48:14 +000088 // FIXME: Remove this!
Bill Wendlingd509a662013-01-29 00:34:06 +000089 static uint64_t getAttrMask(Attribute::AttrKind Val);
Bill Wendlinge38b8042012-09-26 21:07:29 +000090};
91
Bill Wendling66e978f2012-12-20 21:28:43 +000092//===----------------------------------------------------------------------===//
93/// \class
Benjamin Kramer741146b2013-07-11 12:13:16 +000094/// \brief A set of classes that contain the value of the
95/// attribute object. There are three main categories: enum attribute entries,
96/// represented by Attribute::AttrKind; alignment attribute entries; and string
97/// attribute enties, which are for target-dependent attributes.
98
Benjamin Kramer079b96e2013-09-11 18:05:11 +000099class EnumAttributeImpl : public AttributeImpl {
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000100 virtual void anchor();
Benjamin Kramer741146b2013-07-11 12:13:16 +0000101 Attribute::AttrKind Kind;
102
103protected:
104 EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
105 : AttributeImpl(ID), Kind(Kind) {}
106
107public:
108 EnumAttributeImpl(Attribute::AttrKind Kind)
109 : AttributeImpl(EnumAttrEntry), Kind(Kind) {}
110
111 Attribute::AttrKind getEnumKind() const { return Kind; }
112};
113
Hal Finkele15442c2014-07-18 06:51:55 +0000114class IntAttributeImpl : public EnumAttributeImpl {
Craig Topperf398d7c2014-03-05 06:35:38 +0000115 void anchor() override;
Hal Finkele15442c2014-07-18 06:51:55 +0000116 uint64_t Val;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000117
118public:
Hal Finkele15442c2014-07-18 06:51:55 +0000119 IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
120 : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000121 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
122 Kind == Attribute::Dereferenceable ||
123 Kind == Attribute::DereferenceableOrNull) &&
124 "Wrong kind for int attribute!");
Benjamin Kramer741146b2013-07-11 12:13:16 +0000125 }
126
Hal Finkele15442c2014-07-18 06:51:55 +0000127 uint64_t getValue() const { return Val; }
Benjamin Kramer741146b2013-07-11 12:13:16 +0000128};
129
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000130class StringAttributeImpl : public AttributeImpl {
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000131 virtual void anchor();
Benjamin Kramer741146b2013-07-11 12:13:16 +0000132 std::string Kind;
133 std::string Val;
134
135public:
136 StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
137 : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
138
139 StringRef getStringKind() const { return Kind; }
140 StringRef getStringValue() const { return Val; }
141};
142
143//===----------------------------------------------------------------------===//
144/// \class
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000145/// \brief This class represents a group of attributes that apply to one
146/// element: function, return type, or parameter.
James Y Knightaa365b22015-08-05 22:57:34 +0000147class AttributeSetNode final
148 : public FoldingSetNode,
149 private TrailingObjects<AttributeSetNode, Attribute> {
150 friend TrailingObjects;
151
Benjamin Kramer741146b2013-07-11 12:13:16 +0000152 unsigned NumAttrs; ///< Number of attributes in this node.
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000153 /// Bitset with a bit for each available attribute Attribute::AttrKind.
154 uint64_t AvailableAttrs;
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000155
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000156 AttributeSetNode(ArrayRef<Attribute> Attrs)
157 : NumAttrs(Attrs.size()), AvailableAttrs(0) {
Reid Klecknerb0461542016-01-29 22:40:22 +0000158 static_assert(Attribute::EndAttrKinds <= sizeof(AvailableAttrs) * CHAR_BIT,
159 "Too many attributes for AvailableAttrs");
Benjamin Kramer741146b2013-07-11 12:13:16 +0000160 // There's memory after the node where we can store the entries in.
James Y Knightaa365b22015-08-05 22:57:34 +0000161 std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<Attribute>());
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000162
163 for (iterator I = begin(), E = end(); I != E; ++I) {
164 if (!I->isStringAttribute()) {
165 AvailableAttrs |= ((uint64_t)1) << I->getKindAsEnum();
166 }
167 }
Benjamin Kramer741146b2013-07-11 12:13:16 +0000168 }
Bill Wendling97b4f702013-01-27 21:38:03 +0000169
170 // AttributesSetNode is uniqued, these should not be publicly available.
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000171 void operator=(const AttributeSetNode &) = delete;
172 AttributeSetNode(const AttributeSetNode &) = delete;
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000173public:
174 static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
175
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000176 bool hasAttribute(Attribute::AttrKind Kind) const {
177 return AvailableAttrs & ((uint64_t)1) << Kind;
178 }
Bill Wendlingbce7b972013-02-13 08:42:21 +0000179 bool hasAttribute(StringRef Kind) const;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000180 bool hasAttributes() const { return NumAttrs != 0; }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000181
Bill Wendlingbce7b972013-02-13 08:42:21 +0000182 Attribute getAttribute(Attribute::AttrKind Kind) const;
183 Attribute getAttribute(StringRef Kind) const;
184
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000185 unsigned getAlignment() const;
186 unsigned getStackAlignment() const;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000187 uint64_t getDereferenceableBytes() const;
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000188 uint64_t getDereferenceableOrNullBytes() const;
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000189 std::string getAsString(bool InAttrGrp) const;
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000190
Benjamin Kramer741146b2013-07-11 12:13:16 +0000191 typedef const Attribute *iterator;
James Y Knightaa365b22015-08-05 22:57:34 +0000192 iterator begin() const { return getTrailingObjects<Attribute>(); }
Benjamin Kramer741146b2013-07-11 12:13:16 +0000193 iterator end() const { return begin() + NumAttrs; }
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000194
195 void Profile(FoldingSetNodeID &ID) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000196 Profile(ID, makeArrayRef(begin(), end()));
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000197 }
198 static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
199 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
200 AttrList[I].Profile(ID);
201 }
202};
203
NAKAMURA Takumi51fe1192015-08-06 09:49:17 +0000204typedef std::pair<unsigned, AttributeSetNode *> IndexAttrPair;
205
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000206//===----------------------------------------------------------------------===//
207/// \class
208/// \brief This class represents a set of attributes that apply to the function,
209/// return type, and parameters.
James Y Knightaa365b22015-08-05 22:57:34 +0000210class AttributeSetImpl final
211 : public FoldingSetNode,
212 private TrailingObjects<AttributeSetImpl, IndexAttrPair> {
213 friend class AttributeSet;
214 friend TrailingObjects;
James Y Knight8096d342015-06-17 01:21:20 +0000215
216private:
217 LLVMContext &Context;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000218 unsigned NumAttrs; ///< Number of entries in this set.
Matthias Braun33282812016-01-29 22:25:19 +0000219 /// Bitset with a bit for each available attribute Attribute::AttrKind.
220 uint64_t AvailableFunctionAttrs;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000221
James Y Knightaa365b22015-08-05 22:57:34 +0000222 // Helper fn for TrailingObjects class.
223 size_t numTrailingObjects(OverloadToken<IndexAttrPair>) { return NumAttrs; }
224
Benjamin Kramer741146b2013-07-11 12:13:16 +0000225 /// \brief Return a pointer to the IndexAttrPair for the specified slot.
226 const IndexAttrPair *getNode(unsigned Slot) const {
James Y Knightaa365b22015-08-05 22:57:34 +0000227 return getTrailingObjects<IndexAttrPair>() + Slot;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000228 }
Bill Wendling39a4c802013-01-24 01:01:34 +0000229
Bill Wendling698e84f2012-12-30 10:32:01 +0000230 // AttributesSet is uniqued, these should not be publicly available.
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000231 void operator=(const AttributeSetImpl &) = delete;
232 AttributeSetImpl(const AttributeSetImpl &) = delete;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000233public:
Bill Wendlingec454542013-01-28 21:55:20 +0000234 AttributeSetImpl(LLVMContext &C,
Benjamin Kramer741146b2013-07-11 12:13:16 +0000235 ArrayRef<std::pair<unsigned, AttributeSetNode *> > Attrs)
Matthias Braun33282812016-01-29 22:25:19 +0000236 : Context(C), NumAttrs(Attrs.size()), AvailableFunctionAttrs(0) {
Reid Klecknerb0461542016-01-29 22:40:22 +0000237 static_assert(Attribute::EndAttrKinds <=
238 sizeof(AvailableFunctionAttrs) * CHAR_BIT,
239 "Too many attributes");
James Y Knight8096d342015-06-17 01:21:20 +0000240
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000241#ifndef NDEBUG
242 if (Attrs.size() >= 2) {
243 for (const std::pair<unsigned, AttributeSetNode *> *i = Attrs.begin() + 1,
244 *e = Attrs.end();
245 i != e; ++i) {
246 assert((i-1)->first <= i->first && "Attribute set not ordered!");
247 }
248 }
249#endif
Benjamin Kramer741146b2013-07-11 12:13:16 +0000250 // There's memory after the node where we can store the entries in.
James Y Knightaa365b22015-08-05 22:57:34 +0000251 std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<IndexAttrPair>());
Matthias Braun33282812016-01-29 22:25:19 +0000252
253 // Initialize AvailableFunctionAttrs summary bitset.
254 if (NumAttrs > 0) {
255 static_assert(AttributeSet::FunctionIndex == ~0u,
256 "FunctionIndex should be biggest possible index");
257 const std::pair<unsigned, AttributeSetNode *> &Last = Attrs.back();
258 if (Last.first == AttributeSet::FunctionIndex) {
259 const AttributeSetNode *Node = Last.second;
260 for (AttributeSetNode::iterator I = Node->begin(), E = Node->end();
261 I != E; ++I) {
262 if (!I->isStringAttribute())
263 AvailableFunctionAttrs |= ((uint64_t)1) << I->getKindAsEnum();
264 }
265 }
266 }
Benjamin Kramer741146b2013-07-11 12:13:16 +0000267 }
Bill Wendling9ac69f92013-01-04 20:54:35 +0000268
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000269 /// \brief Get the context that created this AttributeSetImpl.
Bill Wendling9ac69f92013-01-04 20:54:35 +0000270 LLVMContext &getContext() { return Context; }
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000271
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000272 /// \brief Return the number of attributes this AttributeSet contains.
Benjamin Kramer741146b2013-07-11 12:13:16 +0000273 unsigned getNumAttributes() const { return NumAttrs; }
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000274
275 /// \brief Get the index of the given "slot" in the AttrNodes list. This index
276 /// is the index of the return, parameter, or function object that the
277 /// attributes are applied to, not the index into the AttrNodes list where the
278 /// attributes reside.
Rafael Espindoladd275302013-04-30 16:53:38 +0000279 unsigned getSlotIndex(unsigned Slot) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000280 return getNode(Slot)->first;
Bill Wendling9eb689c2013-01-28 00:21:34 +0000281 }
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000282
283 /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
284 /// \p Slot is an index into the AttrNodes list, not the index of the return /
285 /// parameter/ function which the attributes apply to.
Bill Wendling57625a42013-01-25 23:09:36 +0000286 AttributeSet getSlotAttributes(unsigned Slot) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000287 return AttributeSet::get(Context, *getNode(Slot));
Bill Wendling57625a42013-01-25 23:09:36 +0000288 }
Bill Wendlingf86efb92012-11-20 05:09:20 +0000289
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000290 /// \brief Retrieve the attribute set node for the given "slot" in the
291 /// AttrNode list.
292 AttributeSetNode *getSlotNode(unsigned Slot) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000293 return getNode(Slot)->second;
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000294 }
295
Matthias Braun33282812016-01-29 22:25:19 +0000296 /// \brief Return true if the AttributeSetNode for the FunctionIndex has an
297 /// enum attribute of the given kind.
298 bool hasFnAttribute(Attribute::AttrKind Kind) const {
299 return AvailableFunctionAttrs & ((uint64_t)1) << Kind;
300 }
301
Benjamin Kramer741146b2013-07-11 12:13:16 +0000302 typedef AttributeSetNode::iterator iterator;
303 iterator begin(unsigned Slot) const { return getSlotNode(Slot)->begin(); }
304 iterator end(unsigned Slot) const { return getSlotNode(Slot)->end(); }
Bill Wendling9eb689c2013-01-28 00:21:34 +0000305
Bill Wendlingf86efb92012-11-20 05:09:20 +0000306 void Profile(FoldingSetNodeID &ID) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000307 Profile(ID, makeArrayRef(getNode(0), getNumAttributes()));
Bill Wendlingf86efb92012-11-20 05:09:20 +0000308 }
Bill Wendling9ac69f92013-01-04 20:54:35 +0000309 static void Profile(FoldingSetNodeID &ID,
Bill Wendlingeeebb132013-01-28 22:33:39 +0000310 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
Bill Wendling39a4c802013-01-24 01:01:34 +0000311 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
312 ID.AddInteger(Nodes[i].first);
313 ID.AddPointer(Nodes[i].second);
314 }
315 }
Bill Wendling1f786a72013-01-27 23:41:29 +0000316
317 // FIXME: This atrocity is temporary.
Rafael Espindoladd275302013-04-30 16:53:38 +0000318 uint64_t Raw(unsigned Index) const;
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000319
320 void dump() const;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000321};
322
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000323} // end llvm namespace
Bill Wendlinge38b8042012-09-26 21:07:29 +0000324
325#endif