blob: d5456580a670300eedda4184ed6ea18aec256047 [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"
James Y Knightaa365b22015-08-05 22:57:34 +000021#include "llvm/Support/TrailingObjects.h"
Bill Wendlingf2955aa2013-01-29 03:20:31 +000022#include <string>
Bill Wendlinge38b8042012-09-26 21:07:29 +000023
24namespace llvm {
25
Bill Wendling0cd0f7f2012-12-29 12:29:38 +000026class Constant;
Bill Wendling6ad6c3b2012-12-19 23:55:43 +000027class LLVMContext;
28
Bill Wendling66e978f2012-12-20 21:28:43 +000029//===----------------------------------------------------------------------===//
30/// \class
Benjamin Kramer741146b2013-07-11 12:13:16 +000031/// \brief This class represents a single, uniqued attribute. That attribute
32/// could be a single enum, a tuple, or a string.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000033class AttributeImpl : public FoldingSetNode {
Benjamin Kramer741146b2013-07-11 12:13:16 +000034 unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
35
36 // AttributesImpl is uniqued, these should not be publicly available.
Aaron Ballmanf9a18972015-02-15 22:54:22 +000037 void operator=(const AttributeImpl &) = delete;
38 AttributeImpl(const AttributeImpl &) = delete;
Benjamin Kramer741146b2013-07-11 12:13:16 +000039
Bill Wendling3f12ac22013-02-05 22:37:24 +000040protected:
41 enum AttrEntryKind {
42 EnumAttrEntry,
Hal Finkele15442c2014-07-18 06:51:55 +000043 IntAttrEntry,
Bill Wendling3f12ac22013-02-05 22:37:24 +000044 StringAttrEntry
45 };
Benjamin Kramer741146b2013-07-11 12:13:16 +000046
47 AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
48
Bill Wendling3f12ac22013-02-05 22:37:24 +000049public:
Alexey Samsonov49109a22013-11-18 09:31:53 +000050 virtual ~AttributeImpl();
51
Benjamin Kramer741146b2013-07-11 12:13:16 +000052 bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
Hal Finkele15442c2014-07-18 06:51:55 +000053 bool isIntAttribute() const { return KindID == IntAttrEntry; }
Benjamin Kramer741146b2013-07-11 12:13:16 +000054 bool isStringAttribute() const { return KindID == StringAttrEntry; }
Bill Wendling3f12ac22013-02-05 22:37:24 +000055
Bill Wendling9ac69f92013-01-04 20:54:35 +000056 bool hasAttribute(Attribute::AttrKind A) const;
Bill Wendling3f12ac22013-02-05 22:37:24 +000057 bool hasAttribute(StringRef Kind) const;
Bill Wendling73ea2de2012-10-08 21:47:17 +000058
Bill Wendling3f12ac22013-02-05 22:37:24 +000059 Attribute::AttrKind getKindAsEnum() const;
60 uint64_t getValueAsInt() const;
Bill Wendlingc3c714b2013-01-29 20:37:10 +000061
Bill Wendling3f12ac22013-02-05 22:37:24 +000062 StringRef getKindAsString() const;
63 StringRef getValueAsString() const;
Bill Wendlingb1d12612012-12-30 01:38:39 +000064
Bill Wendling9c2eba92013-01-31 20:59:05 +000065 /// \brief Used when sorting the attributes.
Bill Wendlingd2e493b2013-01-24 00:06:56 +000066 bool operator<(const AttributeImpl &AI) const;
67
Bill Wendlinge38b8042012-09-26 21:07:29 +000068 void Profile(FoldingSetNodeID &ID) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +000069 if (isEnumAttribute())
70 Profile(ID, getKindAsEnum(), 0);
Hal Finkele15442c2014-07-18 06:51:55 +000071 else if (isIntAttribute())
Bill Wendling3f12ac22013-02-05 22:37:24 +000072 Profile(ID, getKindAsEnum(), getValueAsInt());
73 else
74 Profile(ID, getKindAsString(), getValueAsString());
Bill Wendlinge38b8042012-09-26 21:07:29 +000075 }
Bill Wendling3f12ac22013-02-05 22:37:24 +000076 static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
77 uint64_t Val) {
78 ID.AddInteger(Kind);
79 if (Val) ID.AddInteger(Val);
80 }
81 static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
82 ID.AddString(Kind);
Bill Wendling8a0e0842013-02-28 21:17:03 +000083 if (!Values.empty()) ID.AddString(Values);
Bill Wendlingd509a662013-01-29 00:34:06 +000084 }
85
Bill Wendling3f9fcd42013-02-01 00:48:14 +000086 // FIXME: Remove this!
Bill Wendlingd509a662013-01-29 00:34:06 +000087 static uint64_t getAttrMask(Attribute::AttrKind Val);
Bill Wendlinge38b8042012-09-26 21:07:29 +000088};
89
Bill Wendling66e978f2012-12-20 21:28:43 +000090//===----------------------------------------------------------------------===//
91/// \class
Benjamin Kramer741146b2013-07-11 12:13:16 +000092/// \brief A set of classes that contain the value of the
93/// attribute object. There are three main categories: enum attribute entries,
94/// represented by Attribute::AttrKind; alignment attribute entries; and string
95/// attribute enties, which are for target-dependent attributes.
96
Benjamin Kramer079b96e2013-09-11 18:05:11 +000097class EnumAttributeImpl : public AttributeImpl {
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000098 virtual void anchor();
Benjamin Kramer741146b2013-07-11 12:13:16 +000099 Attribute::AttrKind Kind;
100
101protected:
102 EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
103 : AttributeImpl(ID), Kind(Kind) {}
104
105public:
106 EnumAttributeImpl(Attribute::AttrKind Kind)
107 : AttributeImpl(EnumAttrEntry), Kind(Kind) {}
108
109 Attribute::AttrKind getEnumKind() const { return Kind; }
110};
111
Hal Finkele15442c2014-07-18 06:51:55 +0000112class IntAttributeImpl : public EnumAttributeImpl {
Craig Topperf398d7c2014-03-05 06:35:38 +0000113 void anchor() override;
Hal Finkele15442c2014-07-18 06:51:55 +0000114 uint64_t Val;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000115
116public:
Hal Finkele15442c2014-07-18 06:51:55 +0000117 IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
118 : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000119 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
120 Kind == Attribute::Dereferenceable ||
121 Kind == Attribute::DereferenceableOrNull) &&
122 "Wrong kind for int attribute!");
Benjamin Kramer741146b2013-07-11 12:13:16 +0000123 }
124
Hal Finkele15442c2014-07-18 06:51:55 +0000125 uint64_t getValue() const { return Val; }
Benjamin Kramer741146b2013-07-11 12:13:16 +0000126};
127
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000128class StringAttributeImpl : public AttributeImpl {
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000129 virtual void anchor();
Benjamin Kramer741146b2013-07-11 12:13:16 +0000130 std::string Kind;
131 std::string Val;
132
133public:
134 StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
135 : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
136
137 StringRef getStringKind() const { return Kind; }
138 StringRef getStringValue() const { return Val; }
139};
140
141//===----------------------------------------------------------------------===//
142/// \class
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000143/// \brief This class represents a group of attributes that apply to one
144/// element: function, return type, or parameter.
James Y Knightaa365b22015-08-05 22:57:34 +0000145class AttributeSetNode final
146 : public FoldingSetNode,
147 private TrailingObjects<AttributeSetNode, Attribute> {
148 friend TrailingObjects;
149
Benjamin Kramer741146b2013-07-11 12:13:16 +0000150 unsigned NumAttrs; ///< Number of attributes in this node.
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000151
Benjamin Kramer741146b2013-07-11 12:13:16 +0000152 AttributeSetNode(ArrayRef<Attribute> Attrs) : NumAttrs(Attrs.size()) {
153 // There's memory after the node where we can store the entries in.
James Y Knightaa365b22015-08-05 22:57:34 +0000154 std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<Attribute>());
Benjamin Kramer741146b2013-07-11 12:13:16 +0000155 }
Bill Wendling97b4f702013-01-27 21:38:03 +0000156
157 // AttributesSetNode is uniqued, these should not be publicly available.
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000158 void operator=(const AttributeSetNode &) = delete;
159 AttributeSetNode(const AttributeSetNode &) = delete;
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000160public:
161 static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
162
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000163 bool hasAttribute(Attribute::AttrKind Kind) const;
Bill Wendlingbce7b972013-02-13 08:42:21 +0000164 bool hasAttribute(StringRef Kind) const;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000165 bool hasAttributes() const { return NumAttrs != 0; }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000166
Bill Wendlingbce7b972013-02-13 08:42:21 +0000167 Attribute getAttribute(Attribute::AttrKind Kind) const;
168 Attribute getAttribute(StringRef Kind) const;
169
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000170 unsigned getAlignment() const;
171 unsigned getStackAlignment() const;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000172 uint64_t getDereferenceableBytes() const;
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000173 uint64_t getDereferenceableOrNullBytes() const;
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000174 std::string getAsString(bool InAttrGrp) const;
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000175
Benjamin Kramer741146b2013-07-11 12:13:16 +0000176 typedef const Attribute *iterator;
James Y Knightaa365b22015-08-05 22:57:34 +0000177 iterator begin() const { return getTrailingObjects<Attribute>(); }
Benjamin Kramer741146b2013-07-11 12:13:16 +0000178 iterator end() const { return begin() + NumAttrs; }
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000179
180 void Profile(FoldingSetNodeID &ID) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000181 Profile(ID, makeArrayRef(begin(), end()));
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000182 }
183 static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
184 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
185 AttrList[I].Profile(ID);
186 }
187};
188
189//===----------------------------------------------------------------------===//
190/// \class
191/// \brief This class represents a set of attributes that apply to the function,
192/// return type, and parameters.
James Y Knightaa365b22015-08-05 22:57:34 +0000193typedef std::pair<unsigned, AttributeSetNode *> IndexAttrPair;
Bill Wendling57625a42013-01-25 23:09:36 +0000194
James Y Knightaa365b22015-08-05 22:57:34 +0000195class AttributeSetImpl final
196 : public FoldingSetNode,
197 private TrailingObjects<AttributeSetImpl, IndexAttrPair> {
198 friend class AttributeSet;
199 friend TrailingObjects;
James Y Knight8096d342015-06-17 01:21:20 +0000200
201private:
202 LLVMContext &Context;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000203 unsigned NumAttrs; ///< Number of entries in this set.
204
James Y Knightaa365b22015-08-05 22:57:34 +0000205 // Helper fn for TrailingObjects class.
206 size_t numTrailingObjects(OverloadToken<IndexAttrPair>) { return NumAttrs; }
207
Benjamin Kramer741146b2013-07-11 12:13:16 +0000208 /// \brief Return a pointer to the IndexAttrPair for the specified slot.
209 const IndexAttrPair *getNode(unsigned Slot) const {
James Y Knightaa365b22015-08-05 22:57:34 +0000210 return getTrailingObjects<IndexAttrPair>() + Slot;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000211 }
Bill Wendling39a4c802013-01-24 01:01:34 +0000212
Bill Wendling698e84f2012-12-30 10:32:01 +0000213 // AttributesSet is uniqued, these should not be publicly available.
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000214 void operator=(const AttributeSetImpl &) = delete;
215 AttributeSetImpl(const AttributeSetImpl &) = delete;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000216public:
Bill Wendlingec454542013-01-28 21:55:20 +0000217 AttributeSetImpl(LLVMContext &C,
Benjamin Kramer741146b2013-07-11 12:13:16 +0000218 ArrayRef<std::pair<unsigned, AttributeSetNode *> > Attrs)
219 : Context(C), NumAttrs(Attrs.size()) {
James Y Knight8096d342015-06-17 01:21:20 +0000220
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000221#ifndef NDEBUG
222 if (Attrs.size() >= 2) {
223 for (const std::pair<unsigned, AttributeSetNode *> *i = Attrs.begin() + 1,
224 *e = Attrs.end();
225 i != e; ++i) {
226 assert((i-1)->first <= i->first && "Attribute set not ordered!");
227 }
228 }
229#endif
Benjamin Kramer741146b2013-07-11 12:13:16 +0000230 // There's memory after the node where we can store the entries in.
James Y Knightaa365b22015-08-05 22:57:34 +0000231 std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<IndexAttrPair>());
Benjamin Kramer741146b2013-07-11 12:13:16 +0000232 }
Bill Wendling9ac69f92013-01-04 20:54:35 +0000233
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000234 /// \brief Get the context that created this AttributeSetImpl.
Bill Wendling9ac69f92013-01-04 20:54:35 +0000235 LLVMContext &getContext() { return Context; }
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000236
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000237 /// \brief Return the number of attributes this AttributeSet contains.
Benjamin Kramer741146b2013-07-11 12:13:16 +0000238 unsigned getNumAttributes() const { return NumAttrs; }
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000239
240 /// \brief Get the index of the given "slot" in the AttrNodes list. This index
241 /// is the index of the return, parameter, or function object that the
242 /// attributes are applied to, not the index into the AttrNodes list where the
243 /// attributes reside.
Rafael Espindoladd275302013-04-30 16:53:38 +0000244 unsigned getSlotIndex(unsigned Slot) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000245 return getNode(Slot)->first;
Bill Wendling9eb689c2013-01-28 00:21:34 +0000246 }
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000247
248 /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
249 /// \p Slot is an index into the AttrNodes list, not the index of the return /
250 /// parameter/ function which the attributes apply to.
Bill Wendling57625a42013-01-25 23:09:36 +0000251 AttributeSet getSlotAttributes(unsigned Slot) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000252 return AttributeSet::get(Context, *getNode(Slot));
Bill Wendling57625a42013-01-25 23:09:36 +0000253 }
Bill Wendlingf86efb92012-11-20 05:09:20 +0000254
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000255 /// \brief Retrieve the attribute set node for the given "slot" in the
256 /// AttrNode list.
257 AttributeSetNode *getSlotNode(unsigned Slot) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000258 return getNode(Slot)->second;
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000259 }
260
Benjamin Kramer741146b2013-07-11 12:13:16 +0000261 typedef AttributeSetNode::iterator iterator;
262 iterator begin(unsigned Slot) const { return getSlotNode(Slot)->begin(); }
263 iterator end(unsigned Slot) const { return getSlotNode(Slot)->end(); }
Bill Wendling9eb689c2013-01-28 00:21:34 +0000264
Bill Wendlingf86efb92012-11-20 05:09:20 +0000265 void Profile(FoldingSetNodeID &ID) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000266 Profile(ID, makeArrayRef(getNode(0), getNumAttributes()));
Bill Wendlingf86efb92012-11-20 05:09:20 +0000267 }
Bill Wendling9ac69f92013-01-04 20:54:35 +0000268 static void Profile(FoldingSetNodeID &ID,
Bill Wendlingeeebb132013-01-28 22:33:39 +0000269 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
Bill Wendling39a4c802013-01-24 01:01:34 +0000270 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
271 ID.AddInteger(Nodes[i].first);
272 ID.AddPointer(Nodes[i].second);
273 }
274 }
Bill Wendling1f786a72013-01-27 23:41:29 +0000275
276 // FIXME: This atrocity is temporary.
Rafael Espindoladd275302013-04-30 16:53:38 +0000277 uint64_t Raw(unsigned Index) const;
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000278
279 void dump() const;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000280};
281
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000282} // end llvm namespace
Bill Wendlinge38b8042012-09-26 21:07:29 +0000283
284#endif