blob: baaf1092e0416868975e8233f9390a24b71cf40c [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>
Matthias Braund520d4e2016-01-29 22:30:30 +000023#include <climits>
Bill Wendlinge38b8042012-09-26 21:07:29 +000024
25namespace llvm {
26
Bill Wendling0cd0f7f2012-12-29 12:29:38 +000027class Constant;
Bill Wendling6ad6c3b2012-12-19 23:55:43 +000028class LLVMContext;
29
Bill Wendling66e978f2012-12-20 21:28:43 +000030//===----------------------------------------------------------------------===//
31/// \class
Benjamin Kramer741146b2013-07-11 12:13:16 +000032/// \brief This class represents a single, uniqued attribute. That attribute
33/// could be a single enum, a tuple, or a string.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000034class AttributeImpl : public FoldingSetNode {
Benjamin Kramer741146b2013-07-11 12:13:16 +000035 unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
36
37 // AttributesImpl is uniqued, these should not be publicly available.
Aaron Ballmanf9a18972015-02-15 22:54:22 +000038 void operator=(const AttributeImpl &) = delete;
39 AttributeImpl(const AttributeImpl &) = delete;
Benjamin Kramer741146b2013-07-11 12:13:16 +000040
Bill Wendling3f12ac22013-02-05 22:37:24 +000041protected:
42 enum AttrEntryKind {
43 EnumAttrEntry,
Hal Finkele15442c2014-07-18 06:51:55 +000044 IntAttrEntry,
Bill Wendling3f12ac22013-02-05 22:37:24 +000045 StringAttrEntry
46 };
Benjamin Kramer741146b2013-07-11 12:13:16 +000047
48 AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
49
Bill Wendling3f12ac22013-02-05 22:37:24 +000050public:
Alexey Samsonov49109a22013-11-18 09:31:53 +000051 virtual ~AttributeImpl();
52
Benjamin Kramer741146b2013-07-11 12:13:16 +000053 bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
Hal Finkele15442c2014-07-18 06:51:55 +000054 bool isIntAttribute() const { return KindID == IntAttrEntry; }
Benjamin Kramer741146b2013-07-11 12:13:16 +000055 bool isStringAttribute() const { return KindID == StringAttrEntry; }
Bill Wendling3f12ac22013-02-05 22:37:24 +000056
Bill Wendling9ac69f92013-01-04 20:54:35 +000057 bool hasAttribute(Attribute::AttrKind A) const;
Bill Wendling3f12ac22013-02-05 22:37:24 +000058 bool hasAttribute(StringRef Kind) const;
Bill Wendling73ea2de2012-10-08 21:47:17 +000059
Bill Wendling3f12ac22013-02-05 22:37:24 +000060 Attribute::AttrKind getKindAsEnum() const;
61 uint64_t getValueAsInt() const;
Bill Wendlingc3c714b2013-01-29 20:37:10 +000062
Bill Wendling3f12ac22013-02-05 22:37:24 +000063 StringRef getKindAsString() const;
64 StringRef getValueAsString() const;
Bill Wendlingb1d12612012-12-30 01:38:39 +000065
Bill Wendling9c2eba92013-01-31 20:59:05 +000066 /// \brief Used when sorting the attributes.
Bill Wendlingd2e493b2013-01-24 00:06:56 +000067 bool operator<(const AttributeImpl &AI) const;
68
Bill Wendlinge38b8042012-09-26 21:07:29 +000069 void Profile(FoldingSetNodeID &ID) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +000070 if (isEnumAttribute())
71 Profile(ID, getKindAsEnum(), 0);
Hal Finkele15442c2014-07-18 06:51:55 +000072 else if (isIntAttribute())
Bill Wendling3f12ac22013-02-05 22:37:24 +000073 Profile(ID, getKindAsEnum(), getValueAsInt());
74 else
75 Profile(ID, getKindAsString(), getValueAsString());
Bill Wendlinge38b8042012-09-26 21:07:29 +000076 }
Bill Wendling3f12ac22013-02-05 22:37:24 +000077 static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
78 uint64_t Val) {
79 ID.AddInteger(Kind);
80 if (Val) ID.AddInteger(Val);
81 }
82 static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
83 ID.AddString(Kind);
Bill Wendling8a0e0842013-02-28 21:17:03 +000084 if (!Values.empty()) ID.AddString(Values);
Bill Wendlingd509a662013-01-29 00:34:06 +000085 }
86
Bill Wendling3f9fcd42013-02-01 00:48:14 +000087 // FIXME: Remove this!
Bill Wendlingd509a662013-01-29 00:34:06 +000088 static uint64_t getAttrMask(Attribute::AttrKind Val);
Bill Wendlinge38b8042012-09-26 21:07:29 +000089};
90
Bill Wendling66e978f2012-12-20 21:28:43 +000091//===----------------------------------------------------------------------===//
92/// \class
Benjamin Kramer741146b2013-07-11 12:13:16 +000093/// \brief A set of classes that contain the value of the
94/// attribute object. There are three main categories: enum attribute entries,
95/// represented by Attribute::AttrKind; alignment attribute entries; and string
96/// attribute enties, which are for target-dependent attributes.
97
Benjamin Kramer079b96e2013-09-11 18:05:11 +000098class EnumAttributeImpl : public AttributeImpl {
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000099 virtual void anchor();
Benjamin Kramer741146b2013-07-11 12:13:16 +0000100 Attribute::AttrKind Kind;
101
102protected:
103 EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
104 : AttributeImpl(ID), Kind(Kind) {}
105
106public:
107 EnumAttributeImpl(Attribute::AttrKind Kind)
108 : AttributeImpl(EnumAttrEntry), Kind(Kind) {}
109
110 Attribute::AttrKind getEnumKind() const { return Kind; }
111};
112
Hal Finkele15442c2014-07-18 06:51:55 +0000113class IntAttributeImpl : public EnumAttributeImpl {
Craig Topperf398d7c2014-03-05 06:35:38 +0000114 void anchor() override;
Hal Finkele15442c2014-07-18 06:51:55 +0000115 uint64_t Val;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000116
117public:
Hal Finkele15442c2014-07-18 06:51:55 +0000118 IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
119 : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000120 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
121 Kind == Attribute::Dereferenceable ||
122 Kind == Attribute::DereferenceableOrNull) &&
123 "Wrong kind for int attribute!");
Benjamin Kramer741146b2013-07-11 12:13:16 +0000124 }
125
Hal Finkele15442c2014-07-18 06:51:55 +0000126 uint64_t getValue() const { return Val; }
Benjamin Kramer741146b2013-07-11 12:13:16 +0000127};
128
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000129class StringAttributeImpl : public AttributeImpl {
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000130 virtual void anchor();
Benjamin Kramer741146b2013-07-11 12:13:16 +0000131 std::string Kind;
132 std::string Val;
133
134public:
135 StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
136 : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
137
138 StringRef getStringKind() const { return Kind; }
139 StringRef getStringValue() const { return Val; }
140};
141
142//===----------------------------------------------------------------------===//
143/// \class
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000144/// \brief This class represents a group of attributes that apply to one
145/// element: function, return type, or parameter.
James Y Knightaa365b22015-08-05 22:57:34 +0000146class AttributeSetNode final
147 : public FoldingSetNode,
148 private TrailingObjects<AttributeSetNode, Attribute> {
149 friend TrailingObjects;
150
Benjamin Kramer741146b2013-07-11 12:13:16 +0000151 unsigned NumAttrs; ///< Number of attributes in this node.
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000152 /// Bitset with a bit for each available attribute Attribute::AttrKind.
153 uint64_t AvailableAttrs;
154 static_assert(Attribute::EndAttrKinds <= sizeof(AvailableAttrs)*CHAR_BIT,
155 "Too many attributes for AvailableAttrs");
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000156
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000157 AttributeSetNode(ArrayRef<Attribute> Attrs)
158 : NumAttrs(Attrs.size()), AvailableAttrs(0) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000159 // There's memory after the node where we can store the entries in.
James Y Knightaa365b22015-08-05 22:57:34 +0000160 std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<Attribute>());
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000161
162 for (iterator I = begin(), E = end(); I != E; ++I) {
163 if (!I->isStringAttribute()) {
164 AvailableAttrs |= ((uint64_t)1) << I->getKindAsEnum();
165 }
166 }
Benjamin Kramer741146b2013-07-11 12:13:16 +0000167 }
Bill Wendling97b4f702013-01-27 21:38:03 +0000168
169 // AttributesSetNode is uniqued, these should not be publicly available.
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000170 void operator=(const AttributeSetNode &) = delete;
171 AttributeSetNode(const AttributeSetNode &) = delete;
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000172public:
173 static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
174
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000175 bool hasAttribute(Attribute::AttrKind Kind) const {
176 return AvailableAttrs & ((uint64_t)1) << Kind;
177 }
Bill Wendlingbce7b972013-02-13 08:42:21 +0000178 bool hasAttribute(StringRef Kind) const;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000179 bool hasAttributes() const { return NumAttrs != 0; }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000180
Bill Wendlingbce7b972013-02-13 08:42:21 +0000181 Attribute getAttribute(Attribute::AttrKind Kind) const;
182 Attribute getAttribute(StringRef Kind) const;
183
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000184 unsigned getAlignment() const;
185 unsigned getStackAlignment() const;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000186 uint64_t getDereferenceableBytes() const;
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000187 uint64_t getDereferenceableOrNullBytes() const;
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000188 std::string getAsString(bool InAttrGrp) const;
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000189
Benjamin Kramer741146b2013-07-11 12:13:16 +0000190 typedef const Attribute *iterator;
James Y Knightaa365b22015-08-05 22:57:34 +0000191 iterator begin() const { return getTrailingObjects<Attribute>(); }
Benjamin Kramer741146b2013-07-11 12:13:16 +0000192 iterator end() const { return begin() + NumAttrs; }
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000193
194 void Profile(FoldingSetNodeID &ID) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000195 Profile(ID, makeArrayRef(begin(), end()));
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000196 }
197 static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
198 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
199 AttrList[I].Profile(ID);
200 }
201};
202
NAKAMURA Takumi51fe1192015-08-06 09:49:17 +0000203typedef std::pair<unsigned, AttributeSetNode *> IndexAttrPair;
204
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000205//===----------------------------------------------------------------------===//
206/// \class
207/// \brief This class represents a set of attributes that apply to the function,
208/// return type, and parameters.
James Y Knightaa365b22015-08-05 22:57:34 +0000209class AttributeSetImpl final
210 : public FoldingSetNode,
211 private TrailingObjects<AttributeSetImpl, IndexAttrPair> {
212 friend class AttributeSet;
213 friend TrailingObjects;
James Y Knight8096d342015-06-17 01:21:20 +0000214
215private:
216 LLVMContext &Context;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000217 unsigned NumAttrs; ///< Number of entries in this set.
Matthias Braun33282812016-01-29 22:25:19 +0000218 /// Bitset with a bit for each available attribute Attribute::AttrKind.
219 uint64_t AvailableFunctionAttrs;
220 static_assert(Attribute::EndAttrKinds
221 <= sizeof(AvailableFunctionAttrs)*CHAR_BIT,
222 "Too many attributes");
Benjamin Kramer741146b2013-07-11 12:13:16 +0000223
James Y Knightaa365b22015-08-05 22:57:34 +0000224 // Helper fn for TrailingObjects class.
225 size_t numTrailingObjects(OverloadToken<IndexAttrPair>) { return NumAttrs; }
226
Benjamin Kramer741146b2013-07-11 12:13:16 +0000227 /// \brief Return a pointer to the IndexAttrPair for the specified slot.
228 const IndexAttrPair *getNode(unsigned Slot) const {
James Y Knightaa365b22015-08-05 22:57:34 +0000229 return getTrailingObjects<IndexAttrPair>() + Slot;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000230 }
Bill Wendling39a4c802013-01-24 01:01:34 +0000231
Bill Wendling698e84f2012-12-30 10:32:01 +0000232 // AttributesSet is uniqued, these should not be publicly available.
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000233 void operator=(const AttributeSetImpl &) = delete;
234 AttributeSetImpl(const AttributeSetImpl &) = delete;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000235public:
Bill Wendlingec454542013-01-28 21:55:20 +0000236 AttributeSetImpl(LLVMContext &C,
Benjamin Kramer741146b2013-07-11 12:13:16 +0000237 ArrayRef<std::pair<unsigned, AttributeSetNode *> > Attrs)
Matthias Braun33282812016-01-29 22:25:19 +0000238 : Context(C), NumAttrs(Attrs.size()), AvailableFunctionAttrs(0) {
James Y Knight8096d342015-06-17 01:21:20 +0000239
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000240#ifndef NDEBUG
241 if (Attrs.size() >= 2) {
242 for (const std::pair<unsigned, AttributeSetNode *> *i = Attrs.begin() + 1,
243 *e = Attrs.end();
244 i != e; ++i) {
245 assert((i-1)->first <= i->first && "Attribute set not ordered!");
246 }
247 }
248#endif
Benjamin Kramer741146b2013-07-11 12:13:16 +0000249 // There's memory after the node where we can store the entries in.
James Y Knightaa365b22015-08-05 22:57:34 +0000250 std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<IndexAttrPair>());
Matthias Braun33282812016-01-29 22:25:19 +0000251
252 // Initialize AvailableFunctionAttrs summary bitset.
253 if (NumAttrs > 0) {
254 static_assert(AttributeSet::FunctionIndex == ~0u,
255 "FunctionIndex should be biggest possible index");
256 const std::pair<unsigned, AttributeSetNode *> &Last = Attrs.back();
257 if (Last.first == AttributeSet::FunctionIndex) {
258 const AttributeSetNode *Node = Last.second;
259 for (AttributeSetNode::iterator I = Node->begin(), E = Node->end();
260 I != E; ++I) {
261 if (!I->isStringAttribute())
262 AvailableFunctionAttrs |= ((uint64_t)1) << I->getKindAsEnum();
263 }
264 }
265 }
Benjamin Kramer741146b2013-07-11 12:13:16 +0000266 }
Bill Wendling9ac69f92013-01-04 20:54:35 +0000267
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000268 /// \brief Get the context that created this AttributeSetImpl.
Bill Wendling9ac69f92013-01-04 20:54:35 +0000269 LLVMContext &getContext() { return Context; }
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000270
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000271 /// \brief Return the number of attributes this AttributeSet contains.
Benjamin Kramer741146b2013-07-11 12:13:16 +0000272 unsigned getNumAttributes() const { return NumAttrs; }
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000273
274 /// \brief Get the index of the given "slot" in the AttrNodes list. This index
275 /// is the index of the return, parameter, or function object that the
276 /// attributes are applied to, not the index into the AttrNodes list where the
277 /// attributes reside.
Rafael Espindoladd275302013-04-30 16:53:38 +0000278 unsigned getSlotIndex(unsigned Slot) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000279 return getNode(Slot)->first;
Bill Wendling9eb689c2013-01-28 00:21:34 +0000280 }
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000281
282 /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
283 /// \p Slot is an index into the AttrNodes list, not the index of the return /
284 /// parameter/ function which the attributes apply to.
Bill Wendling57625a42013-01-25 23:09:36 +0000285 AttributeSet getSlotAttributes(unsigned Slot) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000286 return AttributeSet::get(Context, *getNode(Slot));
Bill Wendling57625a42013-01-25 23:09:36 +0000287 }
Bill Wendlingf86efb92012-11-20 05:09:20 +0000288
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000289 /// \brief Retrieve the attribute set node for the given "slot" in the
290 /// AttrNode list.
291 AttributeSetNode *getSlotNode(unsigned Slot) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000292 return getNode(Slot)->second;
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000293 }
294
Matthias Braun33282812016-01-29 22:25:19 +0000295 /// \brief Return true if the AttributeSetNode for the FunctionIndex has an
296 /// enum attribute of the given kind.
297 bool hasFnAttribute(Attribute::AttrKind Kind) const {
298 return AvailableFunctionAttrs & ((uint64_t)1) << Kind;
299 }
300
Benjamin Kramer741146b2013-07-11 12:13:16 +0000301 typedef AttributeSetNode::iterator iterator;
302 iterator begin(unsigned Slot) const { return getSlotNode(Slot)->begin(); }
303 iterator end(unsigned Slot) const { return getSlotNode(Slot)->end(); }
Bill Wendling9eb689c2013-01-28 00:21:34 +0000304
Bill Wendlingf86efb92012-11-20 05:09:20 +0000305 void Profile(FoldingSetNodeID &ID) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000306 Profile(ID, makeArrayRef(getNode(0), getNumAttributes()));
Bill Wendlingf86efb92012-11-20 05:09:20 +0000307 }
Bill Wendling9ac69f92013-01-04 20:54:35 +0000308 static void Profile(FoldingSetNodeID &ID,
Bill Wendlingeeebb132013-01-28 22:33:39 +0000309 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
Bill Wendling39a4c802013-01-24 01:01:34 +0000310 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
311 ID.AddInteger(Nodes[i].first);
312 ID.AddPointer(Nodes[i].second);
313 }
314 }
Bill Wendling1f786a72013-01-27 23:41:29 +0000315
316 // FIXME: This atrocity is temporary.
Rafael Espindoladd275302013-04-30 16:53:38 +0000317 uint64_t Raw(unsigned Index) const;
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000318
319 void dump() const;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000320};
321
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000322} // end llvm namespace
Bill Wendlinge38b8042012-09-26 21:07:29 +0000323
324#endif