blob: 9c7b61f6792366549c42e46242676947571d4ab0 [file] [log] [blame]
Eugene Zelenkod761e2c2017-05-15 21:57:41 +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
Eugene Zelenko9408c612016-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 Zelenko9408c612016-12-07 22:06:02 +000024#include <cassert>
Eugene Zelenko9408c612016-12-07 22:06:02 +000025#include <cstddef>
26#include <cstdint>
Matthias Braun9c981052016-01-29 22:35:29 +000027#include <string>
Eugene Zelenko9408c612016-12-07 22:06:02 +000028#include <utility>
Bill Wendlinge38b8042012-09-26 21:07:29 +000029
30namespace llvm {
31
Bill Wendling6ad6c3b2012-12-19 23:55:43 +000032class LLVMContext;
33
Bill Wendling66e978f2012-12-20 21:28:43 +000034//===----------------------------------------------------------------------===//
35/// \class
Benjamin Kramer741146b2013-07-11 12:13:16 +000036/// \brief This class represents a single, uniqued attribute. That attribute
37/// could be a single enum, a tuple, or a string.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000038class AttributeImpl : public FoldingSetNode {
Benjamin Kramer741146b2013-07-11 12:13:16 +000039 unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
40
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:
Eugene Zelenko9408c612016-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 Samsonov49109a22013-11-18 09:31:53 +000055 virtual ~AttributeImpl();
56
Benjamin Kramer741146b2013-07-11 12:13:16 +000057 bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
Hal Finkele15442c2014-07-18 06:51:55 +000058 bool isIntAttribute() const { return KindID == IntAttrEntry; }
Benjamin Kramer741146b2013-07-11 12:13:16 +000059 bool isStringAttribute() const { return KindID == StringAttrEntry; }
Bill Wendling3f12ac22013-02-05 22:37:24 +000060
Bill Wendling9ac69f92013-01-04 20:54:35 +000061 bool hasAttribute(Attribute::AttrKind A) const;
Bill Wendling3f12ac22013-02-05 22:37:24 +000062 bool hasAttribute(StringRef Kind) const;
Bill Wendling73ea2de2012-10-08 21:47:17 +000063
Bill Wendling3f12ac22013-02-05 22:37:24 +000064 Attribute::AttrKind getKindAsEnum() const;
65 uint64_t getValueAsInt() const;
Bill Wendlingc3c714b2013-01-29 20:37:10 +000066
Bill Wendling3f12ac22013-02-05 22:37:24 +000067 StringRef getKindAsString() const;
68 StringRef getValueAsString() const;
Bill Wendlingb1d12612012-12-30 01:38:39 +000069
Bill Wendling9c2eba92013-01-31 20:59:05 +000070 /// \brief Used when sorting the attributes.
Bill Wendlingd2e493b2013-01-24 00:06:56 +000071 bool operator<(const AttributeImpl &AI) const;
72
Bill Wendlinge38b8042012-09-26 21:07:29 +000073 void Profile(FoldingSetNodeID &ID) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +000074 if (isEnumAttribute())
75 Profile(ID, getKindAsEnum(), 0);
Hal Finkele15442c2014-07-18 06:51:55 +000076 else if (isIntAttribute())
Bill Wendling3f12ac22013-02-05 22:37:24 +000077 Profile(ID, getKindAsEnum(), getValueAsInt());
78 else
79 Profile(ID, getKindAsString(), getValueAsString());
Bill Wendlinge38b8042012-09-26 21:07:29 +000080 }
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000081
Bill Wendling3f12ac22013-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 Zelenkod761e2c2017-05-15 21:57:41 +000087
Bill Wendling3f12ac22013-02-05 22:37:24 +000088 static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
89 ID.AddString(Kind);
Bill Wendling8a0e0842013-02-28 21:17:03 +000090 if (!Values.empty()) ID.AddString(Values);
Bill Wendlingd509a662013-01-29 00:34:06 +000091 }
Bill Wendlinge38b8042012-09-26 21:07:29 +000092};
93
Bill Wendling66e978f2012-12-20 21:28:43 +000094//===----------------------------------------------------------------------===//
95/// \class
Benjamin Kramer741146b2013-07-11 12:13:16 +000096/// \brief A set of classes that contain the value of the
97/// 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 Kramer079b96e2013-09-11 18:05:11 +0000101class EnumAttributeImpl : public AttributeImpl {
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000102 virtual void anchor();
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000103
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 {
Hal Finkele15442c2014-07-18 06:51:55 +0000118 uint64_t Val;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000119
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000120 void anchor() override;
121
Benjamin Kramer741146b2013-07-11 12:13:16 +0000122public:
Hal Finkele15442c2014-07-18 06:51:55 +0000123 IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
124 : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000125 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
126 Kind == Attribute::Dereferenceable ||
George Burgess IV278199f2016-04-12 01:05:35 +0000127 Kind == Attribute::DereferenceableOrNull ||
128 Kind == Attribute::AllocSize) &&
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000129 "Wrong kind for int attribute!");
Benjamin Kramer741146b2013-07-11 12:13:16 +0000130 }
131
Hal Finkele15442c2014-07-18 06:51:55 +0000132 uint64_t getValue() const { return Val; }
Benjamin Kramer741146b2013-07-11 12:13:16 +0000133};
134
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000135class StringAttributeImpl : public AttributeImpl {
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000136 virtual void anchor();
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000137
Benjamin Kramer741146b2013-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 Klecknerc2cb5602017-04-12 00:38:00 +0000149//===----------------------------------------------------------------------===//
150/// \class
151/// \brief This class represents a group of attributes that apply to one
152/// 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
175 /// \brief Return the number of attributes this AttributeList contains.
176 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 Zelenkod761e2c2017-05-15 21:57:41 +0000194 using iterator = const Attribute *;
195
Reid Klecknerc2cb5602017-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 Zelenkod761e2c2017-05-15 21:57:41 +0000202
Reid Klecknerc2cb5602017-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 Zelenkod761e2c2017-05-15 21:57:41 +0000209using IndexAttrPair = std::pair<unsigned, AttributeSet>;
NAKAMURA Takumi51fe1192015-08-06 09:49:17 +0000210
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000211//===----------------------------------------------------------------------===//
212/// \class
213/// \brief This class represents a set of attributes that apply to the function,
214/// return type, and parameters.
Reid Klecknerb5180542017-03-21 16:57:19 +0000215class AttributeListImpl final
James Y Knightaa365b22015-08-05 22:57:34 +0000216 : public FoldingSetNode,
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000217 private TrailingObjects<AttributeListImpl, AttributeSet> {
Reid Klecknerb5180542017-03-21 16:57:19 +0000218 friend class AttributeList;
James Y Knightaa365b22015-08-05 22:57:34 +0000219 friend TrailingObjects;
James Y Knight8096d342015-06-17 01:21:20 +0000220
221private:
Matthias Braun33282812016-01-29 22:25:19 +0000222 /// Bitset with a bit for each available attribute Attribute::AttrKind.
223 uint64_t AvailableFunctionAttrs;
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000224 LLVMContext &Context;
225 unsigned NumAttrSets; ///< Number of entries in this set.
Benjamin Kramer741146b2013-07-11 12:13:16 +0000226
James Y Knightaa365b22015-08-05 22:57:34 +0000227 // Helper fn for TrailingObjects class.
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000228 size_t numTrailingObjects(OverloadToken<AttributeSet>) { return NumAttrSets; }
Bill Wendling39a4c802013-01-24 01:01:34 +0000229
Bill Wendlingf86efb92012-11-20 05:09:20 +0000230public:
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000231 AttributeListImpl(LLVMContext &C, ArrayRef<AttributeSet> Sets);
Bill Wendling9ac69f92013-01-04 20:54:35 +0000232
Eugene Zelenko9408c612016-12-07 22:06:02 +0000233 // AttributesSetImpt is uniqued, these should not be available.
Reid Klecknerb5180542017-03-21 16:57:19 +0000234 AttributeListImpl(const AttributeListImpl &) = delete;
235 AttributeListImpl &operator=(const AttributeListImpl &) = delete;
Eugene Zelenko9408c612016-12-07 22:06:02 +0000236
Richard Smitha64e1ad2016-02-09 02:09:16 +0000237 void operator delete(void *p) { ::operator delete(p); }
Richard Smith1b65c322016-02-09 01:03:42 +0000238
Reid Klecknerb5180542017-03-21 16:57:19 +0000239 /// \brief Get the context that created this AttributeListImpl.
Bill Wendling9ac69f92013-01-04 20:54:35 +0000240 LLVMContext &getContext() { return Context; }
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000241
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000242 /// \brief Return true if the AttributeSet or the FunctionIndex has an
Matthias Braun33282812016-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 Zelenkode6cce22017-06-19 22:05:08 +0000248 using iterator = const AttributeSet *;
249
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000250 iterator begin() const { return getTrailingObjects<AttributeSet>(); }
251 iterator end() const { return begin() + NumAttrSets; }
Bill Wendling9eb689c2013-01-28 00:21:34 +0000252
Reid Klecknera82be602017-04-11 00:16:00 +0000253 void Profile(FoldingSetNodeID &ID) const;
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000254 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeSet> Nodes);
Bill Wendling1f786a72013-01-27 23:41:29 +0000255
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000256 void dump() const;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000257};
258
Eugene Zelenko9408c612016-12-07 22:06:02 +0000259} // end namespace llvm
Bill Wendlinge38b8042012-09-26 21:07:29 +0000260
Eugene Zelenko9408c612016-12-07 22:06:02 +0000261#endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H