blob: d0d27101aa867f353dcf552ee2fba1049e274860 [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
Amaury Sechet17b67cd2016-07-21 04:25:06 +000019#include "AttributeSetNode.h"
Eugene Zelenko9408c612016-12-07 22:06:02 +000020#include "llvm/ADT/ArrayRef.h"
21#include "llvm/ADT/FoldingSet.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/IR/Attributes.h"
24#include "llvm/Support/TrailingObjects.h"
25#include <algorithm>
26#include <cassert>
Matthias Braund520d4e2016-01-29 22:30:30 +000027#include <climits>
Eugene Zelenko9408c612016-12-07 22:06:02 +000028#include <cstddef>
29#include <cstdint>
Matthias Braun9c981052016-01-29 22:35:29 +000030#include <string>
Eugene Zelenko9408c612016-12-07 22:06:02 +000031#include <utility>
Bill Wendlinge38b8042012-09-26 21:07:29 +000032
33namespace llvm {
34
Bill Wendling6ad6c3b2012-12-19 23:55:43 +000035class LLVMContext;
36
Bill Wendling66e978f2012-12-20 21:28:43 +000037//===----------------------------------------------------------------------===//
38/// \class
Benjamin Kramer741146b2013-07-11 12:13:16 +000039/// \brief This class represents a single, uniqued attribute. That attribute
40/// could be a single enum, a tuple, or a string.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000041class AttributeImpl : public FoldingSetNode {
Benjamin Kramer741146b2013-07-11 12:13:16 +000042 unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
43
Bill Wendling3f12ac22013-02-05 22:37:24 +000044protected:
45 enum AttrEntryKind {
46 EnumAttrEntry,
Hal Finkele15442c2014-07-18 06:51:55 +000047 IntAttrEntry,
Bill Wendling3f12ac22013-02-05 22:37:24 +000048 StringAttrEntry
49 };
Benjamin Kramer741146b2013-07-11 12:13:16 +000050
51 AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
52
Bill Wendling3f12ac22013-02-05 22:37:24 +000053public:
Eugene Zelenko9408c612016-12-07 22:06:02 +000054 // AttributesImpl is uniqued, these should not be available.
55 AttributeImpl(const AttributeImpl &) = delete;
56 AttributeImpl &operator=(const AttributeImpl &) = delete;
57
Alexey Samsonov49109a22013-11-18 09:31:53 +000058 virtual ~AttributeImpl();
59
Benjamin Kramer741146b2013-07-11 12:13:16 +000060 bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
Hal Finkele15442c2014-07-18 06:51:55 +000061 bool isIntAttribute() const { return KindID == IntAttrEntry; }
Benjamin Kramer741146b2013-07-11 12:13:16 +000062 bool isStringAttribute() const { return KindID == StringAttrEntry; }
Bill Wendling3f12ac22013-02-05 22:37:24 +000063
Bill Wendling9ac69f92013-01-04 20:54:35 +000064 bool hasAttribute(Attribute::AttrKind A) const;
Bill Wendling3f12ac22013-02-05 22:37:24 +000065 bool hasAttribute(StringRef Kind) const;
Bill Wendling73ea2de2012-10-08 21:47:17 +000066
Bill Wendling3f12ac22013-02-05 22:37:24 +000067 Attribute::AttrKind getKindAsEnum() const;
68 uint64_t getValueAsInt() const;
Bill Wendlingc3c714b2013-01-29 20:37:10 +000069
Bill Wendling3f12ac22013-02-05 22:37:24 +000070 StringRef getKindAsString() const;
71 StringRef getValueAsString() const;
Bill Wendlingb1d12612012-12-30 01:38:39 +000072
Bill Wendling9c2eba92013-01-31 20:59:05 +000073 /// \brief Used when sorting the attributes.
Bill Wendlingd2e493b2013-01-24 00:06:56 +000074 bool operator<(const AttributeImpl &AI) const;
75
Bill Wendlinge38b8042012-09-26 21:07:29 +000076 void Profile(FoldingSetNodeID &ID) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +000077 if (isEnumAttribute())
78 Profile(ID, getKindAsEnum(), 0);
Hal Finkele15442c2014-07-18 06:51:55 +000079 else if (isIntAttribute())
Bill Wendling3f12ac22013-02-05 22:37:24 +000080 Profile(ID, getKindAsEnum(), getValueAsInt());
81 else
82 Profile(ID, getKindAsString(), getValueAsString());
Bill Wendlinge38b8042012-09-26 21:07:29 +000083 }
Bill Wendling3f12ac22013-02-05 22:37:24 +000084 static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
85 uint64_t Val) {
86 ID.AddInteger(Kind);
87 if (Val) ID.AddInteger(Val);
88 }
89 static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
90 ID.AddString(Kind);
Bill Wendling8a0e0842013-02-28 21:17:03 +000091 if (!Values.empty()) ID.AddString(Values);
Bill Wendlingd509a662013-01-29 00:34:06 +000092 }
Bill Wendlinge38b8042012-09-26 21:07:29 +000093};
94
Bill Wendling66e978f2012-12-20 21:28:43 +000095//===----------------------------------------------------------------------===//
96/// \class
Benjamin Kramer741146b2013-07-11 12:13:16 +000097/// \brief A set of classes that contain the value of the
98/// attribute object. There are three main categories: enum attribute entries,
99/// represented by Attribute::AttrKind; alignment attribute entries; and string
100/// attribute enties, which are for target-dependent attributes.
101
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000102class EnumAttributeImpl : public AttributeImpl {
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000103 virtual void anchor();
Benjamin Kramer741146b2013-07-11 12:13:16 +0000104 Attribute::AttrKind Kind;
105
106protected:
107 EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
108 : AttributeImpl(ID), Kind(Kind) {}
109
110public:
111 EnumAttributeImpl(Attribute::AttrKind Kind)
112 : AttributeImpl(EnumAttrEntry), Kind(Kind) {}
113
114 Attribute::AttrKind getEnumKind() const { return Kind; }
115};
116
Hal Finkele15442c2014-07-18 06:51:55 +0000117class IntAttributeImpl : public EnumAttributeImpl {
Craig Topperf398d7c2014-03-05 06:35:38 +0000118 void anchor() override;
Hal Finkele15442c2014-07-18 06:51:55 +0000119 uint64_t Val;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000120
121public:
Hal Finkele15442c2014-07-18 06:51:55 +0000122 IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
123 : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000124 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
125 Kind == Attribute::Dereferenceable ||
George Burgess IV278199f2016-04-12 01:05:35 +0000126 Kind == Attribute::DereferenceableOrNull ||
127 Kind == Attribute::AllocSize) &&
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000128 "Wrong kind for int attribute!");
Benjamin Kramer741146b2013-07-11 12:13:16 +0000129 }
130
Hal Finkele15442c2014-07-18 06:51:55 +0000131 uint64_t getValue() const { return Val; }
Benjamin Kramer741146b2013-07-11 12:13:16 +0000132};
133
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000134class StringAttributeImpl : public AttributeImpl {
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000135 virtual void anchor();
Benjamin Kramer741146b2013-07-11 12:13:16 +0000136 std::string Kind;
137 std::string Val;
138
139public:
140 StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
141 : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
142
143 StringRef getStringKind() const { return Kind; }
144 StringRef getStringValue() const { return Val; }
145};
146
NAKAMURA Takumi51fe1192015-08-06 09:49:17 +0000147typedef std::pair<unsigned, AttributeSetNode *> IndexAttrPair;
148
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000149//===----------------------------------------------------------------------===//
150/// \class
151/// \brief This class represents a set of attributes that apply to the function,
152/// return type, and parameters.
James Y Knightaa365b22015-08-05 22:57:34 +0000153class AttributeSetImpl final
154 : public FoldingSetNode,
155 private TrailingObjects<AttributeSetImpl, IndexAttrPair> {
156 friend class AttributeSet;
157 friend TrailingObjects;
James Y Knight8096d342015-06-17 01:21:20 +0000158
159private:
160 LLVMContext &Context;
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000161 unsigned NumSlots; ///< Number of entries in this set.
Matthias Braun33282812016-01-29 22:25:19 +0000162 /// Bitset with a bit for each available attribute Attribute::AttrKind.
163 uint64_t AvailableFunctionAttrs;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000164
James Y Knightaa365b22015-08-05 22:57:34 +0000165 // Helper fn for TrailingObjects class.
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000166 size_t numTrailingObjects(OverloadToken<IndexAttrPair>) { return NumSlots; }
James Y Knightaa365b22015-08-05 22:57:34 +0000167
Benjamin Kramer741146b2013-07-11 12:13:16 +0000168 /// \brief Return a pointer to the IndexAttrPair for the specified slot.
169 const IndexAttrPair *getNode(unsigned Slot) const {
James Y Knightaa365b22015-08-05 22:57:34 +0000170 return getTrailingObjects<IndexAttrPair>() + Slot;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000171 }
Bill Wendling39a4c802013-01-24 01:01:34 +0000172
Bill Wendlingf86efb92012-11-20 05:09:20 +0000173public:
Bill Wendlingec454542013-01-28 21:55:20 +0000174 AttributeSetImpl(LLVMContext &C,
Eugene Zelenko9408c612016-12-07 22:06:02 +0000175 ArrayRef<std::pair<unsigned, AttributeSetNode *>> Slots)
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000176 : Context(C), NumSlots(Slots.size()), AvailableFunctionAttrs(0) {
Reid Klecknerb0461542016-01-29 22:40:22 +0000177 static_assert(Attribute::EndAttrKinds <=
178 sizeof(AvailableFunctionAttrs) * CHAR_BIT,
179 "Too many attributes");
James Y Knight8096d342015-06-17 01:21:20 +0000180
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000181#ifndef NDEBUG
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000182 if (Slots.size() >= 2) {
183 for (const std::pair<unsigned, AttributeSetNode *> *i = Slots.begin() + 1,
184 *e = Slots.end();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000185 i != e; ++i) {
186 assert((i-1)->first <= i->first && "Attribute set not ordered!");
187 }
188 }
189#endif
Benjamin Kramer741146b2013-07-11 12:13:16 +0000190 // There's memory after the node where we can store the entries in.
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000191 std::copy(Slots.begin(), Slots.end(), getTrailingObjects<IndexAttrPair>());
Matthias Braun33282812016-01-29 22:25:19 +0000192
193 // Initialize AvailableFunctionAttrs summary bitset.
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000194 if (NumSlots > 0) {
Matthias Braun33282812016-01-29 22:25:19 +0000195 static_assert(AttributeSet::FunctionIndex == ~0u,
196 "FunctionIndex should be biggest possible index");
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000197 const std::pair<unsigned, AttributeSetNode *> &Last = Slots.back();
Matthias Braun33282812016-01-29 22:25:19 +0000198 if (Last.first == AttributeSet::FunctionIndex) {
199 const AttributeSetNode *Node = Last.second;
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000200 for (Attribute I : *Node) {
201 if (!I.isStringAttribute())
202 AvailableFunctionAttrs |= ((uint64_t)1) << I.getKindAsEnum();
Matthias Braun33282812016-01-29 22:25:19 +0000203 }
204 }
205 }
Benjamin Kramer741146b2013-07-11 12:13:16 +0000206 }
Bill Wendling9ac69f92013-01-04 20:54:35 +0000207
Eugene Zelenko9408c612016-12-07 22:06:02 +0000208 // AttributesSetImpt is uniqued, these should not be available.
209 AttributeSetImpl(const AttributeSetImpl &) = delete;
210 AttributeSetImpl &operator=(const AttributeSetImpl &) = delete;
211
Richard Smitha64e1ad2016-02-09 02:09:16 +0000212 void operator delete(void *p) { ::operator delete(p); }
Richard Smith1b65c322016-02-09 01:03:42 +0000213
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000214 /// \brief Get the context that created this AttributeSetImpl.
Bill Wendling9ac69f92013-01-04 20:54:35 +0000215 LLVMContext &getContext() { return Context; }
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000216
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000217 /// \brief Return the number of slots used in this attribute list. This is
218 /// the number of arguments that have an attribute set on them (including the
219 /// function itself).
220 unsigned getNumSlots() const { return NumSlots; }
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000221
222 /// \brief Get the index of the given "slot" in the AttrNodes list. This index
223 /// is the index of the return, parameter, or function object that the
224 /// attributes are applied to, not the index into the AttrNodes list where the
225 /// attributes reside.
Rafael Espindoladd275302013-04-30 16:53:38 +0000226 unsigned getSlotIndex(unsigned Slot) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000227 return getNode(Slot)->first;
Bill Wendling9eb689c2013-01-28 00:21:34 +0000228 }
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000229
230 /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
231 /// \p Slot is an index into the AttrNodes list, not the index of the return /
232 /// parameter/ function which the attributes apply to.
Bill Wendling57625a42013-01-25 23:09:36 +0000233 AttributeSet getSlotAttributes(unsigned Slot) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000234 return AttributeSet::get(Context, *getNode(Slot));
Bill Wendling57625a42013-01-25 23:09:36 +0000235 }
Bill Wendlingf86efb92012-11-20 05:09:20 +0000236
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000237 /// \brief Retrieve the attribute set node for the given "slot" in the
238 /// AttrNode list.
239 AttributeSetNode *getSlotNode(unsigned Slot) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000240 return getNode(Slot)->second;
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000241 }
242
Matthias Braun33282812016-01-29 22:25:19 +0000243 /// \brief Return true if the AttributeSetNode for the FunctionIndex has an
244 /// enum attribute of the given kind.
245 bool hasFnAttribute(Attribute::AttrKind Kind) const {
246 return AvailableFunctionAttrs & ((uint64_t)1) << Kind;
247 }
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 {
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000254 Profile(ID, makeArrayRef(getNode(0), getNumSlots()));
Bill Wendlingf86efb92012-11-20 05:09:20 +0000255 }
Bill Wendling9ac69f92013-01-04 20:54:35 +0000256 static void Profile(FoldingSetNodeID &ID,
Eugene Zelenko9408c612016-12-07 22:06:02 +0000257 ArrayRef<std::pair<unsigned, AttributeSetNode*>> Nodes) {
258 for (const auto &Node : Nodes) {
259 ID.AddInteger(Node.first);
260 ID.AddPointer(Node.second);
Bill Wendling39a4c802013-01-24 01:01:34 +0000261 }
262 }
Bill Wendling1f786a72013-01-27 23:41:29 +0000263
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000264 void dump() const;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000265};
266
Eugene Zelenko9408c612016-12-07 22:06:02 +0000267} // end namespace llvm
Bill Wendlinge38b8042012-09-26 21:07:29 +0000268
Eugene Zelenko9408c612016-12-07 22:06:02 +0000269#endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H