blob: bb0c072e47814b7a825a7ee57b1cf9afacff40c0 [file] [log] [blame]
Eugene Zelenko46795222017-05-15 21:57:41 +00001//===- AttributeImpl.h - Attribute Internals --------------------*- C++ -*-===//
Bill Wendling2c79ecb2012-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 Wendling27107f62012-12-20 21:28:43 +00009///
10/// \file
Adrian Prantl26b584c2018-05-01 15:54:18 +000011/// This file defines various helper methods and classes used by
Bill Wendling27107f62012-12-20 21:28:43 +000012/// LLVMContextImpl for creating and managing attributes.
13///
Bill Wendling2c79ecb2012-09-26 21:07:29 +000014//===----------------------------------------------------------------------===//
15
Benjamin Kramer00e08fc2014-08-13 16:26:38 +000016#ifndef LLVM_LIB_IR_ATTRIBUTEIMPL_H
17#define LLVM_LIB_IR_ATTRIBUTEIMPL_H
Bill Wendling2c79ecb2012-09-26 21:07:29 +000018
Eugene Zelenko553d8c82016-12-07 22:06:02 +000019#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/FoldingSet.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/IR/Attributes.h"
23#include "llvm/Support/TrailingObjects.h"
Eugene Zelenko553d8c82016-12-07 22:06:02 +000024#include <cassert>
Eugene Zelenko553d8c82016-12-07 22:06:02 +000025#include <cstddef>
26#include <cstdint>
Matthias Braun7448ec52016-01-29 22:35:29 +000027#include <string>
Eugene Zelenko553d8c82016-12-07 22:06:02 +000028#include <utility>
Bill Wendling2c79ecb2012-09-26 21:07:29 +000029
30namespace llvm {
31
Bill Wendling5f93e2b2012-12-19 23:55:43 +000032class LLVMContext;
33
Bill Wendling27107f62012-12-20 21:28:43 +000034//===----------------------------------------------------------------------===//
35/// \class
Adrian Prantl26b584c2018-05-01 15:54:18 +000036/// This class represents a single, uniqued attribute. That attribute
Benjamin Kramere22cde02013-07-11 12:13:16 +000037/// could be a single enum, a tuple, or a string.
Benjamin Kramer55c06ae2013-09-11 18:05:11 +000038class AttributeImpl : public FoldingSetNode {
Benjamin Kramere22cde02013-07-11 12:13:16 +000039 unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
40
Bill Wendling8c74ecf2013-02-05 22:37:24 +000041protected:
42 enum AttrEntryKind {
43 EnumAttrEntry,
Hal Finkeld0261682014-07-18 06:51:55 +000044 IntAttrEntry,
Bill Wendling8c74ecf2013-02-05 22:37:24 +000045 StringAttrEntry
46 };
Benjamin Kramere22cde02013-07-11 12:13:16 +000047
48 AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
49
Bill Wendling8c74ecf2013-02-05 22:37:24 +000050public:
Eugene Zelenko553d8c82016-12-07 22:06:02 +000051 // AttributesImpl is uniqued, these should not be available.
52 AttributeImpl(const AttributeImpl &) = delete;
53 AttributeImpl &operator=(const AttributeImpl &) = delete;
54
Alexey Samsonovb21ab432013-11-18 09:31:53 +000055 virtual ~AttributeImpl();
56
Benjamin Kramere22cde02013-07-11 12:13:16 +000057 bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
Hal Finkeld0261682014-07-18 06:51:55 +000058 bool isIntAttribute() const { return KindID == IntAttrEntry; }
Benjamin Kramere22cde02013-07-11 12:13:16 +000059 bool isStringAttribute() const { return KindID == StringAttrEntry; }
Bill Wendling8c74ecf2013-02-05 22:37:24 +000060
Bill Wendling60507d52013-01-04 20:54:35 +000061 bool hasAttribute(Attribute::AttrKind A) const;
Bill Wendling8c74ecf2013-02-05 22:37:24 +000062 bool hasAttribute(StringRef Kind) const;
Bill Wendling8e635db2012-10-08 21:47:17 +000063
Bill Wendling8c74ecf2013-02-05 22:37:24 +000064 Attribute::AttrKind getKindAsEnum() const;
65 uint64_t getValueAsInt() const;
Bill Wendling9f175f82013-01-29 20:37:10 +000066
Bill Wendling8c74ecf2013-02-05 22:37:24 +000067 StringRef getKindAsString() const;
68 StringRef getValueAsString() const;
Bill Wendling529ec712012-12-30 01:38:39 +000069
Adrian Prantl26b584c2018-05-01 15:54:18 +000070 /// Used when sorting the attributes.
Bill Wendling3467e302013-01-24 00:06:56 +000071 bool operator<(const AttributeImpl &AI) const;
72
Bill Wendling2c79ecb2012-09-26 21:07:29 +000073 void Profile(FoldingSetNodeID &ID) const {
Bill Wendling8c74ecf2013-02-05 22:37:24 +000074 if (isEnumAttribute())
75 Profile(ID, getKindAsEnum(), 0);
Hal Finkeld0261682014-07-18 06:51:55 +000076 else if (isIntAttribute())
Bill Wendling8c74ecf2013-02-05 22:37:24 +000077 Profile(ID, getKindAsEnum(), getValueAsInt());
78 else
79 Profile(ID, getKindAsString(), getValueAsString());
Bill Wendling2c79ecb2012-09-26 21:07:29 +000080 }
Eugene Zelenko46795222017-05-15 21:57:41 +000081
Bill Wendling8c74ecf2013-02-05 22:37:24 +000082 static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
83 uint64_t Val) {
84 ID.AddInteger(Kind);
85 if (Val) ID.AddInteger(Val);
86 }
Eugene Zelenko46795222017-05-15 21:57:41 +000087
Bill Wendling8c74ecf2013-02-05 22:37:24 +000088 static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
89 ID.AddString(Kind);
Bill Wendling40c81fe2013-02-28 21:17:03 +000090 if (!Values.empty()) ID.AddString(Values);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +000091 }
Bill Wendling2c79ecb2012-09-26 21:07:29 +000092};
93
Bill Wendling27107f62012-12-20 21:28:43 +000094//===----------------------------------------------------------------------===//
95/// \class
Adrian Prantl26b584c2018-05-01 15:54:18 +000096/// A set of classes that contain the value of the
Benjamin Kramere22cde02013-07-11 12:13:16 +000097/// attribute object. There are three main categories: enum attribute entries,
98/// represented by Attribute::AttrKind; alignment attribute entries; and string
99/// attribute enties, which are for target-dependent attributes.
100
Benjamin Kramer55c06ae2013-09-11 18:05:11 +0000101class EnumAttributeImpl : public AttributeImpl {
Juergen Ributzka35436252013-11-19 00:57:56 +0000102 virtual void anchor();
Eugene Zelenkof1934002017-06-19 22:05:08 +0000103
Benjamin Kramere22cde02013-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 Finkeld0261682014-07-18 06:51:55 +0000117class IntAttributeImpl : public EnumAttributeImpl {
Hal Finkeld0261682014-07-18 06:51:55 +0000118 uint64_t Val;
Benjamin Kramere22cde02013-07-11 12:13:16 +0000119
Eugene Zelenko46795222017-05-15 21:57:41 +0000120 void anchor() override;
121
Benjamin Kramere22cde02013-07-11 12:13:16 +0000122public:
Hal Finkeld0261682014-07-18 06:51:55 +0000123 IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
124 : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
Sanjoy Das5ff59072015-04-16 20:29:50 +0000125 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
126 Kind == Attribute::Dereferenceable ||
George Burgess IV274105b2016-04-12 01:05:35 +0000127 Kind == Attribute::DereferenceableOrNull ||
128 Kind == Attribute::AllocSize) &&
Sanjoy Das5ff59072015-04-16 20:29:50 +0000129 "Wrong kind for int attribute!");
Benjamin Kramere22cde02013-07-11 12:13:16 +0000130 }
131
Hal Finkeld0261682014-07-18 06:51:55 +0000132 uint64_t getValue() const { return Val; }
Benjamin Kramere22cde02013-07-11 12:13:16 +0000133};
134
Benjamin Kramer55c06ae2013-09-11 18:05:11 +0000135class StringAttributeImpl : public AttributeImpl {
Juergen Ributzka35436252013-11-19 00:57:56 +0000136 virtual void anchor();
Eugene Zelenkof1934002017-06-19 22:05:08 +0000137
Benjamin Kramere22cde02013-07-11 12:13:16 +0000138 std::string Kind;
139 std::string Val;
140
141public:
142 StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
143 : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
144
145 StringRef getStringKind() const { return Kind; }
146 StringRef getStringValue() const { return Val; }
147};
148
Reid Kleckner06090402017-04-12 00:38:00 +0000149//===----------------------------------------------------------------------===//
150/// \class
Adrian Prantl26b584c2018-05-01 15:54:18 +0000151/// This class represents a group of attributes that apply to one
Reid Kleckner06090402017-04-12 00:38:00 +0000152/// element: function, return type, or parameter.
153class AttributeSetNode final
154 : public FoldingSetNode,
155 private TrailingObjects<AttributeSetNode, Attribute> {
156 friend TrailingObjects;
157
158 /// Bitset with a bit for each available attribute Attribute::AttrKind.
159 uint64_t AvailableAttrs;
160 unsigned NumAttrs; ///< Number of attributes in this node.
161
162 AttributeSetNode(ArrayRef<Attribute> Attrs);
163
164public:
165 // AttributesSetNode is uniqued, these should not be available.
166 AttributeSetNode(const AttributeSetNode &) = delete;
167 AttributeSetNode &operator=(const AttributeSetNode &) = delete;
168
169 void operator delete(void *p) { ::operator delete(p); }
170
171 static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B);
172
173 static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
174
Adrian Prantl26b584c2018-05-01 15:54:18 +0000175 /// Return the number of attributes this AttributeList contains.
Reid Kleckner06090402017-04-12 00:38:00 +0000176 unsigned getNumAttributes() const { return NumAttrs; }
177
178 bool hasAttribute(Attribute::AttrKind Kind) const {
179 return AvailableAttrs & ((uint64_t)1) << Kind;
180 }
181 bool hasAttribute(StringRef Kind) const;
182 bool hasAttributes() const { return NumAttrs != 0; }
183
184 Attribute getAttribute(Attribute::AttrKind Kind) const;
185 Attribute getAttribute(StringRef Kind) const;
186
187 unsigned getAlignment() const;
188 unsigned getStackAlignment() const;
189 uint64_t getDereferenceableBytes() const;
190 uint64_t getDereferenceableOrNullBytes() const;
191 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
192 std::string getAsString(bool InAttrGrp) const;
193
Eugene Zelenko46795222017-05-15 21:57:41 +0000194 using iterator = const Attribute *;
195
Reid Kleckner06090402017-04-12 00:38:00 +0000196 iterator begin() const { return getTrailingObjects<Attribute>(); }
197 iterator end() const { return begin() + NumAttrs; }
198
199 void Profile(FoldingSetNodeID &ID) const {
200 Profile(ID, makeArrayRef(begin(), end()));
201 }
Eugene Zelenko46795222017-05-15 21:57:41 +0000202
Reid Kleckner06090402017-04-12 00:38:00 +0000203 static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
204 for (const auto &Attr : AttrList)
205 Attr.Profile(ID);
206 }
207};
208
Eugene Zelenko46795222017-05-15 21:57:41 +0000209using IndexAttrPair = std::pair<unsigned, AttributeSet>;
NAKAMURA Takumi47e91a8102015-08-06 09:49:17 +0000210
Bill Wendling3467e302013-01-24 00:06:56 +0000211//===----------------------------------------------------------------------===//
212/// \class
Adrian Prantl26b584c2018-05-01 15:54:18 +0000213/// This class represents a set of attributes that apply to the function,
Bill Wendling3467e302013-01-24 00:06:56 +0000214/// return type, and parameters.
Reid Kleckner67077702017-03-21 16:57:19 +0000215class AttributeListImpl final
James Y Knight1cf6cc72015-08-05 22:57:34 +0000216 : public FoldingSetNode,
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000217 private TrailingObjects<AttributeListImpl, AttributeSet> {
Reid Kleckner67077702017-03-21 16:57:19 +0000218 friend class AttributeList;
James Y Knight1cf6cc72015-08-05 22:57:34 +0000219 friend TrailingObjects;
James Y Knight4a80b4b2015-06-17 01:21:20 +0000220
221private:
Matthias Braun1a0e2912016-01-29 22:25:19 +0000222 /// Bitset with a bit for each available attribute Attribute::AttrKind.
223 uint64_t AvailableFunctionAttrs;
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000224 LLVMContext &Context;
225 unsigned NumAttrSets; ///< Number of entries in this set.
Benjamin Kramere22cde02013-07-11 12:13:16 +0000226
James Y Knight1cf6cc72015-08-05 22:57:34 +0000227 // Helper fn for TrailingObjects class.
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000228 size_t numTrailingObjects(OverloadToken<AttributeSet>) { return NumAttrSets; }
Bill Wendlingbb085932013-01-24 01:01:34 +0000229
Bill Wendling0976e002012-11-20 05:09:20 +0000230public:
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000231 AttributeListImpl(LLVMContext &C, ArrayRef<AttributeSet> Sets);
Bill Wendling60507d52013-01-04 20:54:35 +0000232
Eugene Zelenko553d8c82016-12-07 22:06:02 +0000233 // AttributesSetImpt is uniqued, these should not be available.
Reid Kleckner67077702017-03-21 16:57:19 +0000234 AttributeListImpl(const AttributeListImpl &) = delete;
235 AttributeListImpl &operator=(const AttributeListImpl &) = delete;
Eugene Zelenko553d8c82016-12-07 22:06:02 +0000236
Richard Smith749e6022016-02-09 02:09:16 +0000237 void operator delete(void *p) { ::operator delete(p); }
Richard Smithbca133d2016-02-09 01:03:42 +0000238
Adrian Prantl26b584c2018-05-01 15:54:18 +0000239 /// Get the context that created this AttributeListImpl.
Bill Wendling60507d52013-01-04 20:54:35 +0000240 LLVMContext &getContext() { return Context; }
Bill Wendling893eac12013-01-27 21:32:11 +0000241
Adrian Prantl26b584c2018-05-01 15:54:18 +0000242 /// Return true if the AttributeSet or the FunctionIndex has an
Matthias Braun1a0e2912016-01-29 22:25:19 +0000243 /// enum attribute of the given kind.
244 bool hasFnAttribute(Attribute::AttrKind Kind) const {
245 return AvailableFunctionAttrs & ((uint64_t)1) << Kind;
246 }
247
Eugene Zelenkof1934002017-06-19 22:05:08 +0000248 using iterator = const AttributeSet *;
249
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000250 iterator begin() const { return getTrailingObjects<AttributeSet>(); }
251 iterator end() const { return begin() + NumAttrSets; }
Bill Wendling73bc4522013-01-28 00:21:34 +0000252
Reid Klecknera8ca3012017-04-11 00:16:00 +0000253 void Profile(FoldingSetNodeID &ID) const;
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000254 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeSet> Nodes);
Bill Wendlingd05204a2013-01-27 23:41:29 +0000255
Peter Collingbourne40bacac2013-08-02 22:34:30 +0000256 void dump() const;
Bill Wendling0976e002012-11-20 05:09:20 +0000257};
258
Eugene Zelenko553d8c82016-12-07 22:06:02 +0000259} // end namespace llvm
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000260
Eugene Zelenko553d8c82016-12-07 22:06:02 +0000261#endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H