blob: d58bff56576d724c8acfb884945fb5e64e524f33 [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"
George Burgess IV278199f2016-04-12 01:05:35 +000020#include "llvm/ADT/Optional.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Attributes.h"
Amaury Sechet17b67cd2016-07-21 04:25:06 +000022#include "AttributeSetNode.h"
Matthias Braundaa812d2016-01-30 01:14:01 +000023#include "llvm/Support/DataTypes.h"
Matthias Braund520d4e2016-01-29 22:30:30 +000024#include <climits>
Matthias Braun9c981052016-01-29 22:35:29 +000025#include <string>
Bill Wendlinge38b8042012-09-26 21:07:29 +000026
27namespace llvm {
28
Bill Wendling0cd0f7f2012-12-29 12:29:38 +000029class Constant;
Bill Wendling6ad6c3b2012-12-19 23:55:43 +000030class LLVMContext;
31
Bill Wendling66e978f2012-12-20 21:28:43 +000032//===----------------------------------------------------------------------===//
33/// \class
Benjamin Kramer741146b2013-07-11 12:13:16 +000034/// \brief This class represents a single, uniqued attribute. That attribute
35/// could be a single enum, a tuple, or a string.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000036class AttributeImpl : public FoldingSetNode {
Benjamin Kramer741146b2013-07-11 12:13:16 +000037 unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
38
39 // AttributesImpl is uniqued, these should not be publicly available.
Aaron Ballmanf9a18972015-02-15 22:54:22 +000040 void operator=(const AttributeImpl &) = delete;
41 AttributeImpl(const AttributeImpl &) = delete;
Benjamin Kramer741146b2013-07-11 12:13:16 +000042
Bill Wendling3f12ac22013-02-05 22:37:24 +000043protected:
44 enum AttrEntryKind {
45 EnumAttrEntry,
Hal Finkele15442c2014-07-18 06:51:55 +000046 IntAttrEntry,
Bill Wendling3f12ac22013-02-05 22:37:24 +000047 StringAttrEntry
48 };
Benjamin Kramer741146b2013-07-11 12:13:16 +000049
50 AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
51
Bill Wendling3f12ac22013-02-05 22:37:24 +000052public:
Alexey Samsonov49109a22013-11-18 09:31:53 +000053 virtual ~AttributeImpl();
54
Benjamin Kramer741146b2013-07-11 12:13:16 +000055 bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
Hal Finkele15442c2014-07-18 06:51:55 +000056 bool isIntAttribute() const { return KindID == IntAttrEntry; }
Benjamin Kramer741146b2013-07-11 12:13:16 +000057 bool isStringAttribute() const { return KindID == StringAttrEntry; }
Bill Wendling3f12ac22013-02-05 22:37:24 +000058
Bill Wendling9ac69f92013-01-04 20:54:35 +000059 bool hasAttribute(Attribute::AttrKind A) const;
Bill Wendling3f12ac22013-02-05 22:37:24 +000060 bool hasAttribute(StringRef Kind) const;
Bill Wendling73ea2de2012-10-08 21:47:17 +000061
Bill Wendling3f12ac22013-02-05 22:37:24 +000062 Attribute::AttrKind getKindAsEnum() const;
63 uint64_t getValueAsInt() const;
Bill Wendlingc3c714b2013-01-29 20:37:10 +000064
Bill Wendling3f12ac22013-02-05 22:37:24 +000065 StringRef getKindAsString() const;
66 StringRef getValueAsString() const;
Bill Wendlingb1d12612012-12-30 01:38:39 +000067
Bill Wendling9c2eba92013-01-31 20:59:05 +000068 /// \brief Used when sorting the attributes.
Bill Wendlingd2e493b2013-01-24 00:06:56 +000069 bool operator<(const AttributeImpl &AI) const;
70
Bill Wendlinge38b8042012-09-26 21:07:29 +000071 void Profile(FoldingSetNodeID &ID) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +000072 if (isEnumAttribute())
73 Profile(ID, getKindAsEnum(), 0);
Hal Finkele15442c2014-07-18 06:51:55 +000074 else if (isIntAttribute())
Bill Wendling3f12ac22013-02-05 22:37:24 +000075 Profile(ID, getKindAsEnum(), getValueAsInt());
76 else
77 Profile(ID, getKindAsString(), getValueAsString());
Bill Wendlinge38b8042012-09-26 21:07:29 +000078 }
Bill Wendling3f12ac22013-02-05 22:37:24 +000079 static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
80 uint64_t Val) {
81 ID.AddInteger(Kind);
82 if (Val) ID.AddInteger(Val);
83 }
84 static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
85 ID.AddString(Kind);
Bill Wendling8a0e0842013-02-28 21:17:03 +000086 if (!Values.empty()) ID.AddString(Values);
Bill Wendlingd509a662013-01-29 00:34:06 +000087 }
88
Bill Wendling3f9fcd42013-02-01 00:48:14 +000089 // FIXME: Remove this!
Bill Wendlingd509a662013-01-29 00:34:06 +000090 static uint64_t getAttrMask(Attribute::AttrKind Val);
Bill Wendlinge38b8042012-09-26 21:07:29 +000091};
92
Bill Wendling66e978f2012-12-20 21:28:43 +000093//===----------------------------------------------------------------------===//
94/// \class
Benjamin Kramer741146b2013-07-11 12:13:16 +000095/// \brief A set of classes that contain the value of the
96/// attribute object. There are three main categories: enum attribute entries,
97/// represented by Attribute::AttrKind; alignment attribute entries; and string
98/// attribute enties, which are for target-dependent attributes.
99
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000100class EnumAttributeImpl : public AttributeImpl {
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000101 virtual void anchor();
Benjamin Kramer741146b2013-07-11 12:13:16 +0000102 Attribute::AttrKind Kind;
103
104protected:
105 EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
106 : AttributeImpl(ID), Kind(Kind) {}
107
108public:
109 EnumAttributeImpl(Attribute::AttrKind Kind)
110 : AttributeImpl(EnumAttrEntry), Kind(Kind) {}
111
112 Attribute::AttrKind getEnumKind() const { return Kind; }
113};
114
Hal Finkele15442c2014-07-18 06:51:55 +0000115class IntAttributeImpl : public EnumAttributeImpl {
Craig Topperf398d7c2014-03-05 06:35:38 +0000116 void anchor() override;
Hal Finkele15442c2014-07-18 06:51:55 +0000117 uint64_t Val;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000118
119public:
Hal Finkele15442c2014-07-18 06:51:55 +0000120 IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
121 : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000122 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
123 Kind == Attribute::Dereferenceable ||
George Burgess IV278199f2016-04-12 01:05:35 +0000124 Kind == Attribute::DereferenceableOrNull ||
125 Kind == Attribute::AllocSize) &&
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000126 "Wrong kind for int attribute!");
Benjamin Kramer741146b2013-07-11 12:13:16 +0000127 }
128
Hal Finkele15442c2014-07-18 06:51:55 +0000129 uint64_t getValue() const { return Val; }
Benjamin Kramer741146b2013-07-11 12:13:16 +0000130};
131
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000132class StringAttributeImpl : public AttributeImpl {
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000133 virtual void anchor();
Benjamin Kramer741146b2013-07-11 12:13:16 +0000134 std::string Kind;
135 std::string Val;
136
137public:
138 StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
139 : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
140
141 StringRef getStringKind() const { return Kind; }
142 StringRef getStringValue() const { return Val; }
143};
144
NAKAMURA Takumi51fe1192015-08-06 09:49:17 +0000145typedef std::pair<unsigned, AttributeSetNode *> IndexAttrPair;
146
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000147//===----------------------------------------------------------------------===//
148/// \class
149/// \brief This class represents a set of attributes that apply to the function,
150/// return type, and parameters.
James Y Knightaa365b22015-08-05 22:57:34 +0000151class AttributeSetImpl final
152 : public FoldingSetNode,
153 private TrailingObjects<AttributeSetImpl, IndexAttrPair> {
154 friend class AttributeSet;
155 friend TrailingObjects;
James Y Knight8096d342015-06-17 01:21:20 +0000156
157private:
158 LLVMContext &Context;
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000159 unsigned NumSlots; ///< Number of entries in this set.
Matthias Braun33282812016-01-29 22:25:19 +0000160 /// Bitset with a bit for each available attribute Attribute::AttrKind.
161 uint64_t AvailableFunctionAttrs;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000162
James Y Knightaa365b22015-08-05 22:57:34 +0000163 // Helper fn for TrailingObjects class.
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000164 size_t numTrailingObjects(OverloadToken<IndexAttrPair>) { return NumSlots; }
James Y Knightaa365b22015-08-05 22:57:34 +0000165
Benjamin Kramer741146b2013-07-11 12:13:16 +0000166 /// \brief Return a pointer to the IndexAttrPair for the specified slot.
167 const IndexAttrPair *getNode(unsigned Slot) const {
James Y Knightaa365b22015-08-05 22:57:34 +0000168 return getTrailingObjects<IndexAttrPair>() + Slot;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000169 }
Bill Wendling39a4c802013-01-24 01:01:34 +0000170
Bill Wendling698e84f2012-12-30 10:32:01 +0000171 // AttributesSet is uniqued, these should not be publicly available.
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000172 void operator=(const AttributeSetImpl &) = delete;
173 AttributeSetImpl(const AttributeSetImpl &) = delete;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000174public:
Bill Wendlingec454542013-01-28 21:55:20 +0000175 AttributeSetImpl(LLVMContext &C,
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000176 ArrayRef<std::pair<unsigned, AttributeSetNode *> > Slots)
177 : Context(C), NumSlots(Slots.size()), AvailableFunctionAttrs(0) {
Reid Klecknerb0461542016-01-29 22:40:22 +0000178 static_assert(Attribute::EndAttrKinds <=
179 sizeof(AvailableFunctionAttrs) * CHAR_BIT,
180 "Too many attributes");
James Y Knight8096d342015-06-17 01:21:20 +0000181
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000182#ifndef NDEBUG
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000183 if (Slots.size() >= 2) {
184 for (const std::pair<unsigned, AttributeSetNode *> *i = Slots.begin() + 1,
185 *e = Slots.end();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000186 i != e; ++i) {
187 assert((i-1)->first <= i->first && "Attribute set not ordered!");
188 }
189 }
190#endif
Benjamin Kramer741146b2013-07-11 12:13:16 +0000191 // There's memory after the node where we can store the entries in.
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000192 std::copy(Slots.begin(), Slots.end(), getTrailingObjects<IndexAttrPair>());
Matthias Braun33282812016-01-29 22:25:19 +0000193
194 // Initialize AvailableFunctionAttrs summary bitset.
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000195 if (NumSlots > 0) {
Matthias Braun33282812016-01-29 22:25:19 +0000196 static_assert(AttributeSet::FunctionIndex == ~0u,
197 "FunctionIndex should be biggest possible index");
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000198 const std::pair<unsigned, AttributeSetNode *> &Last = Slots.back();
Matthias Braun33282812016-01-29 22:25:19 +0000199 if (Last.first == AttributeSet::FunctionIndex) {
200 const AttributeSetNode *Node = Last.second;
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000201 for (Attribute I : *Node) {
202 if (!I.isStringAttribute())
203 AvailableFunctionAttrs |= ((uint64_t)1) << I.getKindAsEnum();
Matthias Braun33282812016-01-29 22:25:19 +0000204 }
205 }
206 }
Benjamin Kramer741146b2013-07-11 12:13:16 +0000207 }
Bill Wendling9ac69f92013-01-04 20:54:35 +0000208
Richard Smitha64e1ad2016-02-09 02:09:16 +0000209 void operator delete(void *p) { ::operator delete(p); }
Richard Smith1b65c322016-02-09 01:03:42 +0000210
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000211 /// \brief Get the context that created this AttributeSetImpl.
Bill Wendling9ac69f92013-01-04 20:54:35 +0000212 LLVMContext &getContext() { return Context; }
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000213
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000214 /// \brief Return the number of slots used in this attribute list. This is
215 /// the number of arguments that have an attribute set on them (including the
216 /// function itself).
217 unsigned getNumSlots() const { return NumSlots; }
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000218
219 /// \brief Get the index of the given "slot" in the AttrNodes list. This index
220 /// is the index of the return, parameter, or function object that the
221 /// attributes are applied to, not the index into the AttrNodes list where the
222 /// attributes reside.
Rafael Espindoladd275302013-04-30 16:53:38 +0000223 unsigned getSlotIndex(unsigned Slot) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000224 return getNode(Slot)->first;
Bill Wendling9eb689c2013-01-28 00:21:34 +0000225 }
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000226
227 /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
228 /// \p Slot is an index into the AttrNodes list, not the index of the return /
229 /// parameter/ function which the attributes apply to.
Bill Wendling57625a42013-01-25 23:09:36 +0000230 AttributeSet getSlotAttributes(unsigned Slot) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000231 return AttributeSet::get(Context, *getNode(Slot));
Bill Wendling57625a42013-01-25 23:09:36 +0000232 }
Bill Wendlingf86efb92012-11-20 05:09:20 +0000233
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000234 /// \brief Retrieve the attribute set node for the given "slot" in the
235 /// AttrNode list.
236 AttributeSetNode *getSlotNode(unsigned Slot) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000237 return getNode(Slot)->second;
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000238 }
239
Matthias Braun33282812016-01-29 22:25:19 +0000240 /// \brief Return true if the AttributeSetNode for 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
Benjamin Kramer741146b2013-07-11 12:13:16 +0000246 typedef AttributeSetNode::iterator iterator;
247 iterator begin(unsigned Slot) const { return getSlotNode(Slot)->begin(); }
248 iterator end(unsigned Slot) const { return getSlotNode(Slot)->end(); }
Bill Wendling9eb689c2013-01-28 00:21:34 +0000249
Bill Wendlingf86efb92012-11-20 05:09:20 +0000250 void Profile(FoldingSetNodeID &ID) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000251 Profile(ID, makeArrayRef(getNode(0), getNumSlots()));
Bill Wendlingf86efb92012-11-20 05:09:20 +0000252 }
Bill Wendling9ac69f92013-01-04 20:54:35 +0000253 static void Profile(FoldingSetNodeID &ID,
Bill Wendlingeeebb132013-01-28 22:33:39 +0000254 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
Bill Wendling39a4c802013-01-24 01:01:34 +0000255 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
256 ID.AddInteger(Nodes[i].first);
257 ID.AddPointer(Nodes[i].second);
258 }
259 }
Bill Wendling1f786a72013-01-27 23:41:29 +0000260
261 // FIXME: This atrocity is temporary.
Rafael Espindoladd275302013-04-30 16:53:38 +0000262 uint64_t Raw(unsigned Index) const;
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000263
264 void dump() const;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000265};
266
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000267} // end llvm namespace
Bill Wendlinge38b8042012-09-26 21:07:29 +0000268
269#endif