blob: 8ebcb04a565d1b0db7a1aa5a8d64c985810ec25b [file] [log] [blame]
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001//===- AttributeImpl.h - Attribute Internals --------------------*- C++ -*-===//
Bill Wendlinge38b8042012-09-26 21:07:29 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Bill Wendlinge38b8042012-09-26 21:07:29 +00006//
7//===----------------------------------------------------------------------===//
Bill Wendling66e978f2012-12-20 21:28:43 +00008///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// This file defines various helper methods and classes used by
Bill Wendling66e978f2012-12-20 21:28:43 +000011/// LLVMContextImpl for creating and managing attributes.
12///
Bill Wendlinge38b8042012-09-26 21:07:29 +000013//===----------------------------------------------------------------------===//
14
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000015#ifndef LLVM_LIB_IR_ATTRIBUTEIMPL_H
16#define LLVM_LIB_IR_ATTRIBUTEIMPL_H
Bill Wendlinge38b8042012-09-26 21:07:29 +000017
Eugene Zelenko9408c612016-12-07 22:06:02 +000018#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/FoldingSet.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/IR/Attributes.h"
22#include "llvm/Support/TrailingObjects.h"
Eugene Zelenko9408c612016-12-07 22:06:02 +000023#include <cassert>
Eugene Zelenko9408c612016-12-07 22:06:02 +000024#include <cstddef>
25#include <cstdint>
Matthias Braun9c981052016-01-29 22:35:29 +000026#include <string>
Eugene Zelenko9408c612016-12-07 22:06:02 +000027#include <utility>
Bill Wendlinge38b8042012-09-26 21:07:29 +000028
29namespace llvm {
30
Bill Wendling6ad6c3b2012-12-19 23:55:43 +000031class LLVMContext;
32
Bill Wendling66e978f2012-12-20 21:28:43 +000033//===----------------------------------------------------------------------===//
34/// \class
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000035/// This class represents a single, uniqued attribute. That attribute
Benjamin Kramer741146b2013-07-11 12:13:16 +000036/// could be a single enum, a tuple, or a string.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000037class AttributeImpl : public FoldingSetNode {
Benjamin Kramer741146b2013-07-11 12:13:16 +000038 unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
39
Bill Wendling3f12ac22013-02-05 22:37:24 +000040protected:
41 enum AttrEntryKind {
42 EnumAttrEntry,
Hal Finkele15442c2014-07-18 06:51:55 +000043 IntAttrEntry,
Bill Wendling3f12ac22013-02-05 22:37:24 +000044 StringAttrEntry
45 };
Benjamin Kramer741146b2013-07-11 12:13:16 +000046
47 AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
48
Bill Wendling3f12ac22013-02-05 22:37:24 +000049public:
Eugene Zelenko9408c612016-12-07 22:06:02 +000050 // AttributesImpl is uniqued, these should not be available.
51 AttributeImpl(const AttributeImpl &) = delete;
52 AttributeImpl &operator=(const AttributeImpl &) = delete;
53
Alexey Samsonov49109a22013-11-18 09:31:53 +000054 virtual ~AttributeImpl();
55
Benjamin Kramer741146b2013-07-11 12:13:16 +000056 bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
Hal Finkele15442c2014-07-18 06:51:55 +000057 bool isIntAttribute() const { return KindID == IntAttrEntry; }
Benjamin Kramer741146b2013-07-11 12:13:16 +000058 bool isStringAttribute() const { return KindID == StringAttrEntry; }
Bill Wendling3f12ac22013-02-05 22:37:24 +000059
Bill Wendling9ac69f92013-01-04 20:54:35 +000060 bool hasAttribute(Attribute::AttrKind A) const;
Bill Wendling3f12ac22013-02-05 22:37:24 +000061 bool hasAttribute(StringRef Kind) const;
Bill Wendling73ea2de2012-10-08 21:47:17 +000062
Bill Wendling3f12ac22013-02-05 22:37:24 +000063 Attribute::AttrKind getKindAsEnum() const;
64 uint64_t getValueAsInt() const;
Bill Wendlingc3c714b2013-01-29 20:37:10 +000065
Bill Wendling3f12ac22013-02-05 22:37:24 +000066 StringRef getKindAsString() const;
67 StringRef getValueAsString() const;
Bill Wendlingb1d12612012-12-30 01:38:39 +000068
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000069 /// Used when sorting the attributes.
Bill Wendlingd2e493b2013-01-24 00:06:56 +000070 bool operator<(const AttributeImpl &AI) const;
71
Bill Wendlinge38b8042012-09-26 21:07:29 +000072 void Profile(FoldingSetNodeID &ID) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +000073 if (isEnumAttribute())
74 Profile(ID, getKindAsEnum(), 0);
Hal Finkele15442c2014-07-18 06:51:55 +000075 else if (isIntAttribute())
Bill Wendling3f12ac22013-02-05 22:37:24 +000076 Profile(ID, getKindAsEnum(), getValueAsInt());
77 else
78 Profile(ID, getKindAsString(), getValueAsString());
Bill Wendlinge38b8042012-09-26 21:07:29 +000079 }
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000080
Bill Wendling3f12ac22013-02-05 22:37:24 +000081 static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
82 uint64_t Val) {
83 ID.AddInteger(Kind);
84 if (Val) ID.AddInteger(Val);
85 }
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000086
Bill Wendling3f12ac22013-02-05 22:37:24 +000087 static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
88 ID.AddString(Kind);
Bill Wendling8a0e0842013-02-28 21:17:03 +000089 if (!Values.empty()) ID.AddString(Values);
Bill Wendlingd509a662013-01-29 00:34:06 +000090 }
Bill Wendlinge38b8042012-09-26 21:07:29 +000091};
92
Bill Wendling66e978f2012-12-20 21:28:43 +000093//===----------------------------------------------------------------------===//
94/// \class
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000095/// A set of classes that contain the value of the
Benjamin Kramer741146b2013-07-11 12:13:16 +000096/// 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();
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000102
Benjamin Kramer741146b2013-07-11 12:13:16 +0000103 Attribute::AttrKind Kind;
104
105protected:
106 EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
107 : AttributeImpl(ID), Kind(Kind) {}
108
109public:
110 EnumAttributeImpl(Attribute::AttrKind Kind)
111 : AttributeImpl(EnumAttrEntry), Kind(Kind) {}
112
113 Attribute::AttrKind getEnumKind() const { return Kind; }
114};
115
Hal Finkele15442c2014-07-18 06:51:55 +0000116class IntAttributeImpl : public EnumAttributeImpl {
Hal Finkele15442c2014-07-18 06:51:55 +0000117 uint64_t Val;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000118
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000119 void anchor() override;
120
Benjamin Kramer741146b2013-07-11 12:13:16 +0000121public:
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();
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000136
Benjamin Kramer741146b2013-07-11 12:13:16 +0000137 std::string Kind;
138 std::string Val;
139
140public:
141 StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
142 : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
143
144 StringRef getStringKind() const { return Kind; }
145 StringRef getStringValue() const { return Val; }
146};
147
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000148//===----------------------------------------------------------------------===//
149/// \class
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000150/// This class represents a group of attributes that apply to one
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000151/// element: function, return type, or parameter.
152class AttributeSetNode final
153 : public FoldingSetNode,
154 private TrailingObjects<AttributeSetNode, Attribute> {
155 friend TrailingObjects;
156
157 /// Bitset with a bit for each available attribute Attribute::AttrKind.
158 uint64_t AvailableAttrs;
159 unsigned NumAttrs; ///< Number of attributes in this node.
160
161 AttributeSetNode(ArrayRef<Attribute> Attrs);
162
163public:
164 // AttributesSetNode is uniqued, these should not be available.
165 AttributeSetNode(const AttributeSetNode &) = delete;
166 AttributeSetNode &operator=(const AttributeSetNode &) = delete;
167
168 void operator delete(void *p) { ::operator delete(p); }
169
170 static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B);
171
172 static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
173
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000174 /// Return the number of attributes this AttributeList contains.
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000175 unsigned getNumAttributes() const { return NumAttrs; }
176
177 bool hasAttribute(Attribute::AttrKind Kind) const {
178 return AvailableAttrs & ((uint64_t)1) << Kind;
179 }
180 bool hasAttribute(StringRef Kind) const;
181 bool hasAttributes() const { return NumAttrs != 0; }
182
183 Attribute getAttribute(Attribute::AttrKind Kind) const;
184 Attribute getAttribute(StringRef Kind) const;
185
186 unsigned getAlignment() const;
187 unsigned getStackAlignment() const;
188 uint64_t getDereferenceableBytes() const;
189 uint64_t getDereferenceableOrNullBytes() const;
190 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
191 std::string getAsString(bool InAttrGrp) const;
192
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000193 using iterator = const Attribute *;
194
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000195 iterator begin() const { return getTrailingObjects<Attribute>(); }
196 iterator end() const { return begin() + NumAttrs; }
197
198 void Profile(FoldingSetNodeID &ID) const {
199 Profile(ID, makeArrayRef(begin(), end()));
200 }
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000201
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000202 static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
203 for (const auto &Attr : AttrList)
204 Attr.Profile(ID);
205 }
206};
207
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000208using IndexAttrPair = std::pair<unsigned, AttributeSet>;
NAKAMURA Takumi51fe1192015-08-06 09:49:17 +0000209
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000210//===----------------------------------------------------------------------===//
211/// \class
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000212/// This class represents a set of attributes that apply to the function,
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000213/// return type, and parameters.
Reid Klecknerb5180542017-03-21 16:57:19 +0000214class AttributeListImpl final
James Y Knightaa365b22015-08-05 22:57:34 +0000215 : public FoldingSetNode,
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000216 private TrailingObjects<AttributeListImpl, AttributeSet> {
Reid Klecknerb5180542017-03-21 16:57:19 +0000217 friend class AttributeList;
James Y Knightaa365b22015-08-05 22:57:34 +0000218 friend TrailingObjects;
James Y Knight8096d342015-06-17 01:21:20 +0000219
220private:
Matthias Braun33282812016-01-29 22:25:19 +0000221 /// Bitset with a bit for each available attribute Attribute::AttrKind.
222 uint64_t AvailableFunctionAttrs;
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000223 LLVMContext &Context;
224 unsigned NumAttrSets; ///< Number of entries in this set.
Benjamin Kramer741146b2013-07-11 12:13:16 +0000225
James Y Knightaa365b22015-08-05 22:57:34 +0000226 // Helper fn for TrailingObjects class.
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000227 size_t numTrailingObjects(OverloadToken<AttributeSet>) { return NumAttrSets; }
Bill Wendling39a4c802013-01-24 01:01:34 +0000228
Bill Wendlingf86efb92012-11-20 05:09:20 +0000229public:
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000230 AttributeListImpl(LLVMContext &C, ArrayRef<AttributeSet> Sets);
Bill Wendling9ac69f92013-01-04 20:54:35 +0000231
Eugene Zelenko9408c612016-12-07 22:06:02 +0000232 // AttributesSetImpt is uniqued, these should not be available.
Reid Klecknerb5180542017-03-21 16:57:19 +0000233 AttributeListImpl(const AttributeListImpl &) = delete;
234 AttributeListImpl &operator=(const AttributeListImpl &) = delete;
Eugene Zelenko9408c612016-12-07 22:06:02 +0000235
Richard Smitha64e1ad2016-02-09 02:09:16 +0000236 void operator delete(void *p) { ::operator delete(p); }
Richard Smith1b65c322016-02-09 01:03:42 +0000237
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000238 /// Get the context that created this AttributeListImpl.
Bill Wendling9ac69f92013-01-04 20:54:35 +0000239 LLVMContext &getContext() { return Context; }
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000240
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000241 /// Return true if the AttributeSet or the FunctionIndex has an
Matthias Braun33282812016-01-29 22:25:19 +0000242 /// enum attribute of the given kind.
243 bool hasFnAttribute(Attribute::AttrKind Kind) const {
244 return AvailableFunctionAttrs & ((uint64_t)1) << Kind;
245 }
246
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000247 using iterator = const AttributeSet *;
248
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000249 iterator begin() const { return getTrailingObjects<AttributeSet>(); }
250 iterator end() const { return begin() + NumAttrSets; }
Bill Wendling9eb689c2013-01-28 00:21:34 +0000251
Reid Klecknera82be602017-04-11 00:16:00 +0000252 void Profile(FoldingSetNodeID &ID) const;
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000253 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeSet> Nodes);
Bill Wendling1f786a72013-01-27 23:41:29 +0000254
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000255 void dump() const;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000256};
257
Eugene Zelenko9408c612016-12-07 22:06:02 +0000258} // end namespace llvm
Bill Wendlinge38b8042012-09-26 21:07:29 +0000259
Eugene Zelenko9408c612016-12-07 22:06:02 +0000260#endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H