blob: 0448dc17409e9e063ccbc34d39b96864fa46b601 [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"
Bill Wendlingf2955aa2013-01-29 03:20:31 +000021#include <string>
Bill Wendlinge38b8042012-09-26 21:07:29 +000022
23namespace llvm {
24
Bill Wendling0cd0f7f2012-12-29 12:29:38 +000025class Constant;
Bill Wendling6ad6c3b2012-12-19 23:55:43 +000026class LLVMContext;
27
Bill Wendling66e978f2012-12-20 21:28:43 +000028//===----------------------------------------------------------------------===//
29/// \class
Benjamin Kramer741146b2013-07-11 12:13:16 +000030/// \brief This class represents a single, uniqued attribute. That attribute
31/// could be a single enum, a tuple, or a string.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000032class AttributeImpl : public FoldingSetNode {
Benjamin Kramer741146b2013-07-11 12:13:16 +000033 unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
34
35 // AttributesImpl is uniqued, these should not be publicly available.
36 void operator=(const AttributeImpl &) LLVM_DELETED_FUNCTION;
37 AttributeImpl(const AttributeImpl &) LLVM_DELETED_FUNCTION;
38
Bill Wendling3f12ac22013-02-05 22:37:24 +000039protected:
40 enum AttrEntryKind {
41 EnumAttrEntry,
Hal Finkele15442c2014-07-18 06:51:55 +000042 IntAttrEntry,
Bill Wendling3f12ac22013-02-05 22:37:24 +000043 StringAttrEntry
44 };
Benjamin Kramer741146b2013-07-11 12:13:16 +000045
46 AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
47
Bill Wendling3f12ac22013-02-05 22:37:24 +000048public:
Alexey Samsonov49109a22013-11-18 09:31:53 +000049 virtual ~AttributeImpl();
50
Benjamin Kramer741146b2013-07-11 12:13:16 +000051 bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
Hal Finkele15442c2014-07-18 06:51:55 +000052 bool isIntAttribute() const { return KindID == IntAttrEntry; }
Benjamin Kramer741146b2013-07-11 12:13:16 +000053 bool isStringAttribute() const { return KindID == StringAttrEntry; }
Bill Wendling3f12ac22013-02-05 22:37:24 +000054
Bill Wendling9ac69f92013-01-04 20:54:35 +000055 bool hasAttribute(Attribute::AttrKind A) const;
Bill Wendling3f12ac22013-02-05 22:37:24 +000056 bool hasAttribute(StringRef Kind) const;
Bill Wendling73ea2de2012-10-08 21:47:17 +000057
Bill Wendling3f12ac22013-02-05 22:37:24 +000058 Attribute::AttrKind getKindAsEnum() const;
59 uint64_t getValueAsInt() const;
Bill Wendlingc3c714b2013-01-29 20:37:10 +000060
Bill Wendling3f12ac22013-02-05 22:37:24 +000061 StringRef getKindAsString() const;
62 StringRef getValueAsString() const;
Bill Wendlingb1d12612012-12-30 01:38:39 +000063
Bill Wendling9c2eba92013-01-31 20:59:05 +000064 /// \brief Used when sorting the attributes.
Bill Wendlingd2e493b2013-01-24 00:06:56 +000065 bool operator<(const AttributeImpl &AI) const;
66
Bill Wendlinge38b8042012-09-26 21:07:29 +000067 void Profile(FoldingSetNodeID &ID) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +000068 if (isEnumAttribute())
69 Profile(ID, getKindAsEnum(), 0);
Hal Finkele15442c2014-07-18 06:51:55 +000070 else if (isIntAttribute())
Bill Wendling3f12ac22013-02-05 22:37:24 +000071 Profile(ID, getKindAsEnum(), getValueAsInt());
72 else
73 Profile(ID, getKindAsString(), getValueAsString());
Bill Wendlinge38b8042012-09-26 21:07:29 +000074 }
Bill Wendling3f12ac22013-02-05 22:37:24 +000075 static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
76 uint64_t Val) {
77 ID.AddInteger(Kind);
78 if (Val) ID.AddInteger(Val);
79 }
80 static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
81 ID.AddString(Kind);
Bill Wendling8a0e0842013-02-28 21:17:03 +000082 if (!Values.empty()) ID.AddString(Values);
Bill Wendlingd509a662013-01-29 00:34:06 +000083 }
84
Bill Wendling3f9fcd42013-02-01 00:48:14 +000085 // FIXME: Remove this!
Bill Wendlingd509a662013-01-29 00:34:06 +000086 static uint64_t getAttrMask(Attribute::AttrKind Val);
Bill Wendlinge38b8042012-09-26 21:07:29 +000087};
88
Bill Wendling66e978f2012-12-20 21:28:43 +000089//===----------------------------------------------------------------------===//
90/// \class
Benjamin Kramer741146b2013-07-11 12:13:16 +000091/// \brief A set of classes that contain the value of the
92/// attribute object. There are three main categories: enum attribute entries,
93/// represented by Attribute::AttrKind; alignment attribute entries; and string
94/// attribute enties, which are for target-dependent attributes.
95
Benjamin Kramer079b96e2013-09-11 18:05:11 +000096class EnumAttributeImpl : public AttributeImpl {
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000097 virtual void anchor();
Benjamin Kramer741146b2013-07-11 12:13:16 +000098 Attribute::AttrKind Kind;
99
100protected:
101 EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
102 : AttributeImpl(ID), Kind(Kind) {}
103
104public:
105 EnumAttributeImpl(Attribute::AttrKind Kind)
106 : AttributeImpl(EnumAttrEntry), Kind(Kind) {}
107
108 Attribute::AttrKind getEnumKind() const { return Kind; }
109};
110
Hal Finkele15442c2014-07-18 06:51:55 +0000111class IntAttributeImpl : public EnumAttributeImpl {
Craig Topperf398d7c2014-03-05 06:35:38 +0000112 void anchor() override;
Hal Finkele15442c2014-07-18 06:51:55 +0000113 uint64_t Val;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000114
115public:
Hal Finkele15442c2014-07-18 06:51:55 +0000116 IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
117 : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000118 assert(
Hal Finkelb0407ba2014-07-18 15:51:28 +0000119 (Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
120 Kind == Attribute::Dereferenceable) &&
Hal Finkele15442c2014-07-18 06:51:55 +0000121 "Wrong kind for int attribute!");
Benjamin Kramer741146b2013-07-11 12:13:16 +0000122 }
123
Hal Finkele15442c2014-07-18 06:51:55 +0000124 uint64_t getValue() const { return Val; }
Benjamin Kramer741146b2013-07-11 12:13:16 +0000125};
126
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000127class StringAttributeImpl : public AttributeImpl {
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000128 virtual void anchor();
Benjamin Kramer741146b2013-07-11 12:13:16 +0000129 std::string Kind;
130 std::string Val;
131
132public:
133 StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
134 : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
135
136 StringRef getStringKind() const { return Kind; }
137 StringRef getStringValue() const { return Val; }
138};
139
140//===----------------------------------------------------------------------===//
141/// \class
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000142/// \brief This class represents a group of attributes that apply to one
143/// element: function, return type, or parameter.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000144class AttributeSetNode : public FoldingSetNode {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000145 unsigned NumAttrs; ///< Number of attributes in this node.
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000146
Benjamin Kramer741146b2013-07-11 12:13:16 +0000147 AttributeSetNode(ArrayRef<Attribute> Attrs) : NumAttrs(Attrs.size()) {
148 // There's memory after the node where we can store the entries in.
149 std::copy(Attrs.begin(), Attrs.end(),
150 reinterpret_cast<Attribute *>(this + 1));
151 }
Bill Wendling97b4f702013-01-27 21:38:03 +0000152
153 // AttributesSetNode is uniqued, these should not be publicly available.
154 void operator=(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
155 AttributeSetNode(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000156public:
157 static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
158
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000159 bool hasAttribute(Attribute::AttrKind Kind) const;
Bill Wendlingbce7b972013-02-13 08:42:21 +0000160 bool hasAttribute(StringRef Kind) const;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000161 bool hasAttributes() const { return NumAttrs != 0; }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000162
Bill Wendlingbce7b972013-02-13 08:42:21 +0000163 Attribute getAttribute(Attribute::AttrKind Kind) const;
164 Attribute getAttribute(StringRef Kind) const;
165
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000166 unsigned getAlignment() const;
167 unsigned getStackAlignment() const;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000168 uint64_t getDereferenceableBytes() const;
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000169 std::string getAsString(bool InAttrGrp) const;
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000170
Benjamin Kramer741146b2013-07-11 12:13:16 +0000171 typedef const Attribute *iterator;
172 iterator begin() const { return reinterpret_cast<iterator>(this + 1); }
173 iterator end() const { return begin() + NumAttrs; }
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000174
175 void Profile(FoldingSetNodeID &ID) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000176 Profile(ID, makeArrayRef(begin(), end()));
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000177 }
178 static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
179 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
180 AttrList[I].Profile(ID);
181 }
182};
183
184//===----------------------------------------------------------------------===//
185/// \class
186/// \brief This class represents a set of attributes that apply to the function,
187/// return type, and parameters.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000188class AttributeSetImpl : public FoldingSetNode {
Bill Wendling57625a42013-01-25 23:09:36 +0000189 friend class AttributeSet;
190
Bill Wendling9ac69f92013-01-04 20:54:35 +0000191 LLVMContext &Context;
Bill Wendling9ac69f92013-01-04 20:54:35 +0000192
Bill Wendlingeeebb132013-01-28 22:33:39 +0000193 typedef std::pair<unsigned, AttributeSetNode*> IndexAttrPair;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000194 unsigned NumAttrs; ///< Number of entries in this set.
195
196 /// \brief Return a pointer to the IndexAttrPair for the specified slot.
197 const IndexAttrPair *getNode(unsigned Slot) const {
198 return reinterpret_cast<const IndexAttrPair *>(this + 1) + Slot;
199 }
Bill Wendling39a4c802013-01-24 01:01:34 +0000200
Bill Wendling698e84f2012-12-30 10:32:01 +0000201 // AttributesSet is uniqued, these should not be publicly available.
Bill Wendling6848e382012-12-19 22:42:22 +0000202 void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
203 AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000204public:
Bill Wendlingec454542013-01-28 21:55:20 +0000205 AttributeSetImpl(LLVMContext &C,
Benjamin Kramer741146b2013-07-11 12:13:16 +0000206 ArrayRef<std::pair<unsigned, AttributeSetNode *> > Attrs)
207 : Context(C), NumAttrs(Attrs.size()) {
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000208#ifndef NDEBUG
209 if (Attrs.size() >= 2) {
210 for (const std::pair<unsigned, AttributeSetNode *> *i = Attrs.begin() + 1,
211 *e = Attrs.end();
212 i != e; ++i) {
213 assert((i-1)->first <= i->first && "Attribute set not ordered!");
214 }
215 }
216#endif
Benjamin Kramer741146b2013-07-11 12:13:16 +0000217 // There's memory after the node where we can store the entries in.
218 std::copy(Attrs.begin(), Attrs.end(),
219 reinterpret_cast<IndexAttrPair *>(this + 1));
220 }
Bill Wendling9ac69f92013-01-04 20:54:35 +0000221
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000222 /// \brief Get the context that created this AttributeSetImpl.
Bill Wendling9ac69f92013-01-04 20:54:35 +0000223 LLVMContext &getContext() { return Context; }
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000224
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000225 /// \brief Return the number of attributes this AttributeSet contains.
Benjamin Kramer741146b2013-07-11 12:13:16 +0000226 unsigned getNumAttributes() const { return NumAttrs; }
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000227
228 /// \brief Get the index of the given "slot" in the AttrNodes list. This index
229 /// is the index of the return, parameter, or function object that the
230 /// attributes are applied to, not the index into the AttrNodes list where the
231 /// attributes reside.
Rafael Espindoladd275302013-04-30 16:53:38 +0000232 unsigned getSlotIndex(unsigned Slot) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000233 return getNode(Slot)->first;
Bill Wendling9eb689c2013-01-28 00:21:34 +0000234 }
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000235
236 /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
237 /// \p Slot is an index into the AttrNodes list, not the index of the return /
238 /// parameter/ function which the attributes apply to.
Bill Wendling57625a42013-01-25 23:09:36 +0000239 AttributeSet getSlotAttributes(unsigned Slot) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000240 return AttributeSet::get(Context, *getNode(Slot));
Bill Wendling57625a42013-01-25 23:09:36 +0000241 }
Bill Wendlingf86efb92012-11-20 05:09:20 +0000242
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000243 /// \brief Retrieve the attribute set node for the given "slot" in the
244 /// AttrNode list.
245 AttributeSetNode *getSlotNode(unsigned Slot) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000246 return getNode(Slot)->second;
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000247 }
248
Benjamin Kramer741146b2013-07-11 12:13:16 +0000249 typedef AttributeSetNode::iterator iterator;
250 iterator begin(unsigned Slot) const { return getSlotNode(Slot)->begin(); }
251 iterator end(unsigned Slot) const { return getSlotNode(Slot)->end(); }
Bill Wendling9eb689c2013-01-28 00:21:34 +0000252
Bill Wendlingf86efb92012-11-20 05:09:20 +0000253 void Profile(FoldingSetNodeID &ID) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000254 Profile(ID, makeArrayRef(getNode(0), getNumAttributes()));
Bill Wendlingf86efb92012-11-20 05:09:20 +0000255 }
Bill Wendling9ac69f92013-01-04 20:54:35 +0000256 static void Profile(FoldingSetNodeID &ID,
Bill Wendlingeeebb132013-01-28 22:33:39 +0000257 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
Bill Wendling39a4c802013-01-24 01:01:34 +0000258 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
259 ID.AddInteger(Nodes[i].first);
260 ID.AddPointer(Nodes[i].second);
261 }
262 }
Bill Wendling1f786a72013-01-27 23:41:29 +0000263
264 // FIXME: This atrocity is temporary.
Rafael Espindoladd275302013-04-30 16:53:38 +0000265 uint64_t Raw(unsigned Index) const;
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000266
267 void dump() const;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000268};
269
Bill Wendlinge38b8042012-09-26 21:07:29 +0000270} // end llvm namespace
271
272#endif