blob: 06cc09e1cfc79a1d2bbe196dbc9441c8932d7b21 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- llvm/Attributes.h - Container for Attributes -------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9/// \file
Andrew Scullcdfcccc2018-10-05 20:58:37 +010010/// This file contains the simple types necessary to represent the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010011/// attributes associated with functions and their calls.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_IR_ATTRIBUTES_H
16#define LLVM_IR_ATTRIBUTES_H
17
18#include "llvm-c/Types.h"
19#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/FoldingSet.h"
21#include "llvm/ADT/Optional.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/ADT/iterator_range.h"
Andrew Scullcdfcccc2018-10-05 20:58:37 +010024#include "llvm/Config/llvm-config.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010025#include "llvm/Support/PointerLikeTypeTraits.h"
26#include <bitset>
27#include <cassert>
28#include <cstdint>
29#include <map>
30#include <string>
31#include <utility>
32
33namespace llvm {
34
35class AttrBuilder;
36class AttributeImpl;
37class AttributeListImpl;
38class AttributeSetNode;
39template<typename T> struct DenseMapInfo;
40class Function;
41class LLVMContext;
42class Type;
43
44//===----------------------------------------------------------------------===//
45/// \class
Andrew Scullcdfcccc2018-10-05 20:58:37 +010046/// Functions, function parameters, and return types can have attributes
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010047/// to indicate how they should be treated by optimizations and code
48/// generation. This class represents one of those attributes. It's light-weight
49/// and should be passed around by-value.
50class Attribute {
51public:
52 /// This enumeration lists the attributes that can be associated with
53 /// parameters, function results, or the function itself.
54 ///
55 /// Note: The `uwtable' attribute is about the ABI or the user mandating an
56 /// entry in the unwind table. The `nounwind' attribute is about an exception
57 /// passing by the function.
58 ///
59 /// In a theoretical system that uses tables for profiling and SjLj for
60 /// exceptions, they would be fully independent. In a normal system that uses
61 /// tables for both, the semantics are:
62 ///
63 /// nil = Needs an entry because an exception might pass by.
64 /// nounwind = No need for an entry
65 /// uwtable = Needs an entry because the ABI says so and because
66 /// an exception might pass by.
67 /// uwtable + nounwind = Needs an entry because the ABI says so.
68
69 enum AttrKind {
70 // IR-Level Attributes
71 None, ///< No attributes have been set
72 #define GET_ATTR_ENUM
Andrew Scullcdfcccc2018-10-05 20:58:37 +010073 #include "llvm/IR/Attributes.inc"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010074 EndAttrKinds ///< Sentinal value useful for loops
75 };
76
77private:
78 AttributeImpl *pImpl = nullptr;
79
80 Attribute(AttributeImpl *A) : pImpl(A) {}
81
82public:
83 Attribute() = default;
84
85 //===--------------------------------------------------------------------===//
86 // Attribute Construction
87 //===--------------------------------------------------------------------===//
88
Andrew Scullcdfcccc2018-10-05 20:58:37 +010089 /// Return a uniquified Attribute object.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010090 static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
91 static Attribute get(LLVMContext &Context, StringRef Kind,
92 StringRef Val = StringRef());
Andrew Walbran3d2c1972020-04-07 12:24:26 +010093 static Attribute get(LLVMContext &Context, AttrKind Kind, Type *Ty);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010094
Andrew Scullcdfcccc2018-10-05 20:58:37 +010095 /// Return a uniquified Attribute object that has the specific
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010096 /// alignment set.
97 static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
98 static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
99 static Attribute getWithDereferenceableBytes(LLVMContext &Context,
100 uint64_t Bytes);
101 static Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context,
102 uint64_t Bytes);
103 static Attribute getWithAllocSizeArgs(LLVMContext &Context,
104 unsigned ElemSizeArg,
105 const Optional<unsigned> &NumElemsArg);
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100106 static Attribute getWithByValType(LLVMContext &Context, Type *Ty);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100107
108 //===--------------------------------------------------------------------===//
109 // Attribute Accessors
110 //===--------------------------------------------------------------------===//
111
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100112 /// Return true if the attribute is an Attribute::AttrKind type.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100113 bool isEnumAttribute() const;
114
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100115 /// Return true if the attribute is an integer attribute.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100116 bool isIntAttribute() const;
117
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100118 /// Return true if the attribute is a string (target-dependent)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100119 /// attribute.
120 bool isStringAttribute() const;
121
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100122 /// Return true if the attribute is a type attribute.
123 bool isTypeAttribute() const;
124
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100125 /// Return true if the attribute is present.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100126 bool hasAttribute(AttrKind Val) const;
127
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100128 /// Return true if the target-dependent attribute is present.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100129 bool hasAttribute(StringRef Val) const;
130
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100131 /// Return the attribute's kind as an enum (Attribute::AttrKind). This
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100132 /// requires the attribute to be an enum or integer attribute.
133 Attribute::AttrKind getKindAsEnum() const;
134
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100135 /// Return the attribute's value as an integer. This requires that the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100136 /// attribute be an integer attribute.
137 uint64_t getValueAsInt() const;
138
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100139 /// Return the attribute's kind as a string. This requires the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100140 /// attribute to be a string attribute.
141 StringRef getKindAsString() const;
142
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100143 /// Return the attribute's value as a string. This requires the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100144 /// attribute to be a string attribute.
145 StringRef getValueAsString() const;
146
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100147 /// Return the attribute's value as a Type. This requires the attribute to be
148 /// a type attribute.
149 Type *getValueAsType() const;
150
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100151 /// Returns the alignment field of an attribute as a byte alignment
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100152 /// value.
153 unsigned getAlignment() const;
154
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100155 /// Returns the stack alignment field of an attribute as a byte
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100156 /// alignment value.
157 unsigned getStackAlignment() const;
158
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100159 /// Returns the number of dereferenceable bytes from the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100160 /// dereferenceable attribute.
161 uint64_t getDereferenceableBytes() const;
162
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100163 /// Returns the number of dereferenceable_or_null bytes from the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100164 /// dereferenceable_or_null attribute.
165 uint64_t getDereferenceableOrNullBytes() const;
166
167 /// Returns the argument numbers for the allocsize attribute (or pair(0, 0)
168 /// if not known).
169 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
170
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100171 /// The Attribute is converted to a string of equivalent mnemonic. This
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100172 /// is, presumably, for writing out the mnemonics for the assembly writer.
173 std::string getAsString(bool InAttrGrp = false) const;
174
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100175 /// Equality and non-equality operators.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100176 bool operator==(Attribute A) const { return pImpl == A.pImpl; }
177 bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
178
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100179 /// Less-than operator. Useful for sorting the attributes list.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100180 bool operator<(Attribute A) const;
181
182 void Profile(FoldingSetNodeID &ID) const {
183 ID.AddPointer(pImpl);
184 }
185
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100186 /// Return a raw pointer that uniquely identifies this attribute.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100187 void *getRawPointer() const {
188 return pImpl;
189 }
190
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100191 /// Get an attribute from a raw pointer created by getRawPointer.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100192 static Attribute fromRawPointer(void *RawPtr) {
193 return Attribute(reinterpret_cast<AttributeImpl*>(RawPtr));
194 }
195};
196
197// Specialized opaque value conversions.
198inline LLVMAttributeRef wrap(Attribute Attr) {
199 return reinterpret_cast<LLVMAttributeRef>(Attr.getRawPointer());
200}
201
202// Specialized opaque value conversions.
203inline Attribute unwrap(LLVMAttributeRef Attr) {
204 return Attribute::fromRawPointer(Attr);
205}
206
207//===----------------------------------------------------------------------===//
208/// \class
209/// This class holds the attributes for a particular argument, parameter,
210/// function, or return value. It is an immutable value type that is cheap to
211/// copy. Adding and removing enum attributes is intended to be fast, but adding
212/// and removing string or integer attributes involves a FoldingSet lookup.
213class AttributeSet {
214 friend AttributeListImpl;
215 template <typename Ty> friend struct DenseMapInfo;
216
217 // TODO: Extract AvailableAttrs from AttributeSetNode and store them here.
218 // This will allow an efficient implementation of addAttribute and
219 // removeAttribute for enum attrs.
220
221 /// Private implementation pointer.
222 AttributeSetNode *SetNode = nullptr;
223
224private:
225 explicit AttributeSet(AttributeSetNode *ASN) : SetNode(ASN) {}
226
227public:
228 /// AttributeSet is a trivially copyable value type.
229 AttributeSet() = default;
230 AttributeSet(const AttributeSet &) = default;
231 ~AttributeSet() = default;
232
233 static AttributeSet get(LLVMContext &C, const AttrBuilder &B);
234 static AttributeSet get(LLVMContext &C, ArrayRef<Attribute> Attrs);
235
236 bool operator==(const AttributeSet &O) const { return SetNode == O.SetNode; }
237 bool operator!=(const AttributeSet &O) const { return !(*this == O); }
238
239 /// Add an argument attribute. Returns a new set because attribute sets are
240 /// immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100241 LLVM_NODISCARD AttributeSet addAttribute(LLVMContext &C,
242 Attribute::AttrKind Kind) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100243
244 /// Add a target-dependent attribute. Returns a new set because attribute sets
245 /// are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100246 LLVM_NODISCARD AttributeSet addAttribute(LLVMContext &C, StringRef Kind,
247 StringRef Value = StringRef()) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100248
249 /// Add attributes to the attribute set. Returns a new set because attribute
250 /// sets are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100251 LLVM_NODISCARD AttributeSet addAttributes(LLVMContext &C,
252 AttributeSet AS) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100253
254 /// Remove the specified attribute from this set. Returns a new set because
255 /// attribute sets are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100256 LLVM_NODISCARD AttributeSet removeAttribute(LLVMContext &C,
257 Attribute::AttrKind Kind) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100258
259 /// Remove the specified attribute from this set. Returns a new set because
260 /// attribute sets are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100261 LLVM_NODISCARD AttributeSet removeAttribute(LLVMContext &C,
262 StringRef Kind) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100263
264 /// Remove the specified attributes from this set. Returns a new set because
265 /// attribute sets are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100266 LLVM_NODISCARD AttributeSet
267 removeAttributes(LLVMContext &C, const AttrBuilder &AttrsToRemove) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100268
269 /// Return the number of attributes in this set.
270 unsigned getNumAttributes() const;
271
272 /// Return true if attributes exists in this set.
273 bool hasAttributes() const { return SetNode != nullptr; }
274
275 /// Return true if the attribute exists in this set.
276 bool hasAttribute(Attribute::AttrKind Kind) const;
277
278 /// Return true if the attribute exists in this set.
279 bool hasAttribute(StringRef Kind) const;
280
281 /// Return the attribute object.
282 Attribute getAttribute(Attribute::AttrKind Kind) const;
283
284 /// Return the target-dependent attribute object.
285 Attribute getAttribute(StringRef Kind) const;
286
287 unsigned getAlignment() const;
288 unsigned getStackAlignment() const;
289 uint64_t getDereferenceableBytes() const;
290 uint64_t getDereferenceableOrNullBytes() const;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100291 Type *getByValType() const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100292 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
293 std::string getAsString(bool InAttrGrp = false) const;
294
295 using iterator = const Attribute *;
296
297 iterator begin() const;
298 iterator end() const;
299#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
300 void dump() const;
301#endif
302};
303
304//===----------------------------------------------------------------------===//
305/// \class
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100306/// Provide DenseMapInfo for AttributeSet.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100307template <> struct DenseMapInfo<AttributeSet> {
308 static AttributeSet getEmptyKey() {
309 auto Val = static_cast<uintptr_t>(-1);
310 Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
311 return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
312 }
313
314 static AttributeSet getTombstoneKey() {
315 auto Val = static_cast<uintptr_t>(-2);
316 Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
317 return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
318 }
319
320 static unsigned getHashValue(AttributeSet AS) {
321 return (unsigned((uintptr_t)AS.SetNode) >> 4) ^
322 (unsigned((uintptr_t)AS.SetNode) >> 9);
323 }
324
325 static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
326};
327
328//===----------------------------------------------------------------------===//
329/// \class
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100330/// This class holds the attributes for a function, its return value, and
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100331/// its parameters. You access the attributes for each of them via an index into
332/// the AttributeList object. The function attributes are at index
333/// `AttributeList::FunctionIndex', the return value is at index
334/// `AttributeList::ReturnIndex', and the attributes for the parameters start at
335/// index `AttributeList::FirstArgIndex'.
336class AttributeList {
337public:
338 enum AttrIndex : unsigned {
339 ReturnIndex = 0U,
340 FunctionIndex = ~0U,
341 FirstArgIndex = 1,
342 };
343
344private:
345 friend class AttrBuilder;
346 friend class AttributeListImpl;
347 friend class AttributeSet;
348 friend class AttributeSetNode;
349 template <typename Ty> friend struct DenseMapInfo;
350
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100351 /// The attributes that we are managing. This can be null to represent
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100352 /// the empty attributes list.
353 AttributeListImpl *pImpl = nullptr;
354
355public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100356 /// Create an AttributeList with the specified parameters in it.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100357 static AttributeList get(LLVMContext &C,
358 ArrayRef<std::pair<unsigned, Attribute>> Attrs);
359 static AttributeList get(LLVMContext &C,
360 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs);
361
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100362 /// Create an AttributeList from attribute sets for a function, its
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100363 /// return value, and all of its arguments.
364 static AttributeList get(LLVMContext &C, AttributeSet FnAttrs,
365 AttributeSet RetAttrs,
366 ArrayRef<AttributeSet> ArgAttrs);
367
368private:
369 explicit AttributeList(AttributeListImpl *LI) : pImpl(LI) {}
370
371 static AttributeList getImpl(LLVMContext &C, ArrayRef<AttributeSet> AttrSets);
372
373public:
374 AttributeList() = default;
375
376 //===--------------------------------------------------------------------===//
377 // AttributeList Construction and Mutation
378 //===--------------------------------------------------------------------===//
379
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100380 /// Return an AttributeList with the specified parameters in it.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100381 static AttributeList get(LLVMContext &C, ArrayRef<AttributeList> Attrs);
382 static AttributeList get(LLVMContext &C, unsigned Index,
383 ArrayRef<Attribute::AttrKind> Kinds);
384 static AttributeList get(LLVMContext &C, unsigned Index,
385 ArrayRef<StringRef> Kind);
386 static AttributeList get(LLVMContext &C, unsigned Index,
387 const AttrBuilder &B);
388
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100389 /// Add an attribute to the attribute set at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100390 /// Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100391 LLVM_NODISCARD AttributeList addAttribute(LLVMContext &C, unsigned Index,
392 Attribute::AttrKind Kind) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100393
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100394 /// Add an attribute to the attribute set at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100395 /// Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100396 LLVM_NODISCARD AttributeList
397 addAttribute(LLVMContext &C, unsigned Index, StringRef Kind,
398 StringRef Value = StringRef()) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100399
400 /// Add an attribute to the attribute set at the given index.
401 /// Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100402 LLVM_NODISCARD AttributeList addAttribute(LLVMContext &C, unsigned Index,
403 Attribute A) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100404
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100405 /// Add attributes to the attribute set at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100406 /// Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100407 LLVM_NODISCARD AttributeList addAttributes(LLVMContext &C, unsigned Index,
408 const AttrBuilder &B) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100409
410 /// Add an argument attribute to the list. Returns a new list because
411 /// attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100412 LLVM_NODISCARD AttributeList addParamAttribute(
413 LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100414 return addAttribute(C, ArgNo + FirstArgIndex, Kind);
415 }
416
417 /// Add an argument attribute to the list. Returns a new list because
418 /// attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100419 LLVM_NODISCARD AttributeList
420 addParamAttribute(LLVMContext &C, unsigned ArgNo, StringRef Kind,
421 StringRef Value = StringRef()) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100422 return addAttribute(C, ArgNo + FirstArgIndex, Kind, Value);
423 }
424
425 /// Add an attribute to the attribute list at the given arg indices. Returns a
426 /// new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100427 LLVM_NODISCARD AttributeList addParamAttribute(LLVMContext &C,
428 ArrayRef<unsigned> ArgNos,
429 Attribute A) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100430
431 /// Add an argument attribute to the list. Returns a new list because
432 /// attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100433 LLVM_NODISCARD AttributeList addParamAttributes(LLVMContext &C,
434 unsigned ArgNo,
435 const AttrBuilder &B) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100436 return addAttributes(C, ArgNo + FirstArgIndex, B);
437 }
438
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100439 /// Remove the specified attribute at the specified index from this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100440 /// attribute list. Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100441 LLVM_NODISCARD AttributeList removeAttribute(LLVMContext &C, unsigned Index,
442 Attribute::AttrKind Kind) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100443
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100444 /// Remove the specified attribute at the specified index from this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100445 /// attribute list. Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100446 LLVM_NODISCARD AttributeList removeAttribute(LLVMContext &C, unsigned Index,
447 StringRef Kind) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100448
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100449 /// Remove the specified attributes at the specified index from this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100450 /// attribute list. Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100451 LLVM_NODISCARD AttributeList removeAttributes(
452 LLVMContext &C, unsigned Index, const AttrBuilder &AttrsToRemove) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100453
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100454 /// Remove all attributes at the specified index from this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100455 /// attribute list. Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100456 LLVM_NODISCARD AttributeList removeAttributes(LLVMContext &C,
457 unsigned Index) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100458
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100459 /// Remove the specified attribute at the specified arg index from this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100460 /// attribute list. Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100461 LLVM_NODISCARD AttributeList removeParamAttribute(
462 LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100463 return removeAttribute(C, ArgNo + FirstArgIndex, Kind);
464 }
465
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100466 /// Remove the specified attribute at the specified arg index from this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100467 /// attribute list. Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100468 LLVM_NODISCARD AttributeList removeParamAttribute(LLVMContext &C,
469 unsigned ArgNo,
470 StringRef Kind) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100471 return removeAttribute(C, ArgNo + FirstArgIndex, Kind);
472 }
473
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100474 /// Remove the specified attribute at the specified arg index from this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100475 /// attribute list. Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100476 LLVM_NODISCARD AttributeList removeParamAttributes(
477 LLVMContext &C, unsigned ArgNo, const AttrBuilder &AttrsToRemove) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100478 return removeAttributes(C, ArgNo + FirstArgIndex, AttrsToRemove);
479 }
480
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100481 /// Remove all attributes at the specified arg index from this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100482 /// attribute list. Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100483 LLVM_NODISCARD AttributeList removeParamAttributes(LLVMContext &C,
484 unsigned ArgNo) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100485 return removeAttributes(C, ArgNo + FirstArgIndex);
486 }
487
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100488 /// \brief Add the dereferenceable attribute to the attribute set at the given
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100489 /// index. Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100490 LLVM_NODISCARD AttributeList addDereferenceableAttr(LLVMContext &C,
491 unsigned Index,
492 uint64_t Bytes) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100493
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100494 /// \brief Add the dereferenceable attribute to the attribute set at the given
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100495 /// arg index. Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100496 LLVM_NODISCARD AttributeList addDereferenceableParamAttr(
497 LLVMContext &C, unsigned ArgNo, uint64_t Bytes) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100498 return addDereferenceableAttr(C, ArgNo + FirstArgIndex, Bytes);
499 }
500
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100501 /// Add the dereferenceable_or_null attribute to the attribute set at
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100502 /// the given index. Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100503 LLVM_NODISCARD AttributeList addDereferenceableOrNullAttr(
504 LLVMContext &C, unsigned Index, uint64_t Bytes) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100505
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100506 /// Add the dereferenceable_or_null attribute to the attribute set at
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100507 /// the given arg index. Returns a new list because attribute lists are
508 /// immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100509 LLVM_NODISCARD AttributeList addDereferenceableOrNullParamAttr(
510 LLVMContext &C, unsigned ArgNo, uint64_t Bytes) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100511 return addDereferenceableOrNullAttr(C, ArgNo + FirstArgIndex, Bytes);
512 }
513
514 /// Add the allocsize attribute to the attribute set at the given index.
515 /// Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100516 LLVM_NODISCARD AttributeList
517 addAllocSizeAttr(LLVMContext &C, unsigned Index, unsigned ElemSizeArg,
518 const Optional<unsigned> &NumElemsArg);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100519
520 /// Add the allocsize attribute to the attribute set at the given arg index.
521 /// Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100522 LLVM_NODISCARD AttributeList
523 addAllocSizeParamAttr(LLVMContext &C, unsigned ArgNo, unsigned ElemSizeArg,
524 const Optional<unsigned> &NumElemsArg) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100525 return addAllocSizeAttr(C, ArgNo + FirstArgIndex, ElemSizeArg, NumElemsArg);
526 }
527
528 //===--------------------------------------------------------------------===//
529 // AttributeList Accessors
530 //===--------------------------------------------------------------------===//
531
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100532 /// Retrieve the LLVM context.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100533 LLVMContext &getContext() const;
534
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100535 /// The attributes for the specified index are returned.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100536 AttributeSet getAttributes(unsigned Index) const;
537
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100538 /// The attributes for the argument or parameter at the given index are
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100539 /// returned.
540 AttributeSet getParamAttributes(unsigned ArgNo) const;
541
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100542 /// The attributes for the ret value are returned.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100543 AttributeSet getRetAttributes() const;
544
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100545 /// The function attributes are returned.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100546 AttributeSet getFnAttributes() const;
547
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100548 /// Return true if the attribute exists at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100549 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
550
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100551 /// Return true if the attribute exists at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100552 bool hasAttribute(unsigned Index, StringRef Kind) const;
553
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100554 /// Return true if attribute exists at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100555 bool hasAttributes(unsigned Index) const;
556
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100557 /// Return true if the attribute exists for the given argument
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100558 bool hasParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
559 return hasAttribute(ArgNo + FirstArgIndex, Kind);
560 }
561
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100562 /// Return true if the attribute exists for the given argument
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100563 bool hasParamAttr(unsigned ArgNo, StringRef Kind) const {
564 return hasAttribute(ArgNo + FirstArgIndex, Kind);
565 }
566
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100567 /// Return true if attributes exists for the given argument
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100568 bool hasParamAttrs(unsigned ArgNo) const {
569 return hasAttributes(ArgNo + FirstArgIndex);
570 }
571
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100572 /// Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100573 /// may be faster.
574 bool hasFnAttribute(Attribute::AttrKind Kind) const;
575
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100576 /// Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100577 /// may be faster.
578 bool hasFnAttribute(StringRef Kind) const;
579
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100580 /// Equivalent to hasAttribute(ArgNo + FirstArgIndex, Kind).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100581 bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const;
582
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100583 /// Return true if the specified attribute is set for at least one
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100584 /// parameter or for the return value. If Index is not nullptr, the index
585 /// of a parameter with the specified attribute is provided.
586 bool hasAttrSomewhere(Attribute::AttrKind Kind,
587 unsigned *Index = nullptr) const;
588
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100589 /// Return the attribute object that exists at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100590 Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
591
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100592 /// Return the attribute object that exists at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100593 Attribute getAttribute(unsigned Index, StringRef Kind) const;
594
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100595 /// Return the attribute object that exists at the arg index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100596 Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
597 return getAttribute(ArgNo + FirstArgIndex, Kind);
598 }
599
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100600 /// Return the attribute object that exists at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100601 Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const {
602 return getAttribute(ArgNo + FirstArgIndex, Kind);
603 }
604
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100605 /// Return the alignment of the return value.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100606 unsigned getRetAlignment() const;
607
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100608 /// Return the alignment for the specified function parameter.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100609 unsigned getParamAlignment(unsigned ArgNo) const;
610
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100611 /// Return the byval type for the specified function parameter.
612 Type *getParamByValType(unsigned ArgNo) const;
613
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100614 /// Get the stack alignment.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100615 unsigned getStackAlignment(unsigned Index) const;
616
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100617 /// Get the number of dereferenceable bytes (or zero if unknown).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100618 uint64_t getDereferenceableBytes(unsigned Index) const;
619
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100620 /// Get the number of dereferenceable bytes (or zero if unknown) of an
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100621 /// arg.
622 uint64_t getParamDereferenceableBytes(unsigned ArgNo) const {
623 return getDereferenceableBytes(ArgNo + FirstArgIndex);
624 }
625
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100626 /// Get the number of dereferenceable_or_null bytes (or zero if
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100627 /// unknown).
628 uint64_t getDereferenceableOrNullBytes(unsigned Index) const;
629
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100630 /// Get the number of dereferenceable_or_null bytes (or zero if
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100631 /// unknown) of an arg.
632 uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo) const {
633 return getDereferenceableOrNullBytes(ArgNo + FirstArgIndex);
634 }
635
636 /// Get the allocsize argument numbers (or pair(0, 0) if unknown).
637 std::pair<unsigned, Optional<unsigned>>
638 getAllocSizeArgs(unsigned Index) const;
639
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100640 /// Return the attributes at the index as a string.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100641 std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
642
643 //===--------------------------------------------------------------------===//
644 // AttributeList Introspection
645 //===--------------------------------------------------------------------===//
646
647 using iterator = const AttributeSet *;
648
649 iterator begin() const;
650 iterator end() const;
651
652 unsigned getNumAttrSets() const;
653
654 /// Use these to iterate over the valid attribute indices.
655 unsigned index_begin() const { return AttributeList::FunctionIndex; }
656 unsigned index_end() const { return getNumAttrSets() - 1; }
657
658 /// operator==/!= - Provide equality predicates.
659 bool operator==(const AttributeList &RHS) const { return pImpl == RHS.pImpl; }
660 bool operator!=(const AttributeList &RHS) const { return pImpl != RHS.pImpl; }
661
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100662 /// Return a raw pointer that uniquely identifies this attribute list.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100663 void *getRawPointer() const {
664 return pImpl;
665 }
666
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100667 /// Return true if there are no attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100668 bool isEmpty() const { return pImpl == nullptr; }
669
670 void dump() const;
671};
672
673//===----------------------------------------------------------------------===//
674/// \class
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100675/// Provide DenseMapInfo for AttributeList.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100676template <> struct DenseMapInfo<AttributeList> {
677 static AttributeList getEmptyKey() {
678 auto Val = static_cast<uintptr_t>(-1);
679 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
680 return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
681 }
682
683 static AttributeList getTombstoneKey() {
684 auto Val = static_cast<uintptr_t>(-2);
685 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
686 return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
687 }
688
689 static unsigned getHashValue(AttributeList AS) {
690 return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
691 (unsigned((uintptr_t)AS.pImpl) >> 9);
692 }
693
694 static bool isEqual(AttributeList LHS, AttributeList RHS) {
695 return LHS == RHS;
696 }
697};
698
699//===----------------------------------------------------------------------===//
700/// \class
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100701/// This class is used in conjunction with the Attribute::get method to
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100702/// create an Attribute object. The object itself is uniquified. The Builder's
703/// value, however, is not. So this can be used as a quick way to test for
704/// equality, presence of attributes, etc.
705class AttrBuilder {
706 std::bitset<Attribute::EndAttrKinds> Attrs;
707 std::map<std::string, std::string> TargetDepAttrs;
708 uint64_t Alignment = 0;
709 uint64_t StackAlignment = 0;
710 uint64_t DerefBytes = 0;
711 uint64_t DerefOrNullBytes = 0;
712 uint64_t AllocSizeArgs = 0;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100713 Type *ByValType = nullptr;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100714
715public:
716 AttrBuilder() = default;
717
718 AttrBuilder(const Attribute &A) {
719 addAttribute(A);
720 }
721
722 AttrBuilder(AttributeList AS, unsigned Idx);
723 AttrBuilder(AttributeSet AS);
724
725 void clear();
726
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100727 /// Add an attribute to the builder.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100728 AttrBuilder &addAttribute(Attribute::AttrKind Val);
729
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100730 /// Add the Attribute object to the builder.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100731 AttrBuilder &addAttribute(Attribute A);
732
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100733 /// Add the target-dependent attribute to the builder.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100734 AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
735
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100736 /// Remove an attribute from the builder.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100737 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
738
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100739 /// Remove the attributes from the builder.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100740 AttrBuilder &removeAttributes(AttributeList A, uint64_t WithoutIndex);
741
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100742 /// Remove the target-dependent attribute to the builder.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100743 AttrBuilder &removeAttribute(StringRef A);
744
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100745 /// Add the attributes from the builder.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100746 AttrBuilder &merge(const AttrBuilder &B);
747
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100748 /// Remove the attributes from the builder.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100749 AttrBuilder &remove(const AttrBuilder &B);
750
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100751 /// Return true if the builder has any attribute that's in the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100752 /// specified builder.
753 bool overlaps(const AttrBuilder &B) const;
754
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100755 /// Return true if the builder has the specified attribute.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100756 bool contains(Attribute::AttrKind A) const {
757 assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
758 return Attrs[A];
759 }
760
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100761 /// Return true if the builder has the specified target-dependent
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100762 /// attribute.
763 bool contains(StringRef A) const;
764
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100765 /// Return true if the builder has IR-level attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100766 bool hasAttributes() const;
767
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100768 /// Return true if the builder has any attribute that's in the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100769 /// specified attribute.
770 bool hasAttributes(AttributeList A, uint64_t Index) const;
771
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100772 /// Return true if the builder has an alignment attribute.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100773 bool hasAlignmentAttr() const;
774
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100775 /// Retrieve the alignment attribute, if it exists.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100776 uint64_t getAlignment() const { return Alignment; }
777
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100778 /// Retrieve the stack alignment attribute, if it exists.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100779 uint64_t getStackAlignment() const { return StackAlignment; }
780
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100781 /// Retrieve the number of dereferenceable bytes, if the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100782 /// dereferenceable attribute exists (zero is returned otherwise).
783 uint64_t getDereferenceableBytes() const { return DerefBytes; }
784
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100785 /// Retrieve the number of dereferenceable_or_null bytes, if the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100786 /// dereferenceable_or_null attribute exists (zero is returned otherwise).
787 uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; }
788
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100789 /// Retrieve the byval type.
790 Type *getByValType() const { return ByValType; }
791
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100792 /// Retrieve the allocsize args, if the allocsize attribute exists. If it
793 /// doesn't exist, pair(0, 0) is returned.
794 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
795
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100796 /// This turns an int alignment (which must be a power of 2) into the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100797 /// form used internally in Attribute.
798 AttrBuilder &addAlignmentAttr(unsigned Align);
799
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100800 /// This turns an int stack alignment (which must be a power of 2) into
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100801 /// the form used internally in Attribute.
802 AttrBuilder &addStackAlignmentAttr(unsigned Align);
803
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100804 /// This turns the number of dereferenceable bytes into the form used
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100805 /// internally in Attribute.
806 AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
807
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100808 /// This turns the number of dereferenceable_or_null bytes into the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100809 /// form used internally in Attribute.
810 AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
811
812 /// This turns one (or two) ints into the form used internally in Attribute.
813 AttrBuilder &addAllocSizeAttr(unsigned ElemSizeArg,
814 const Optional<unsigned> &NumElemsArg);
815
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100816 /// This turns a byval type into the form used internally in Attribute.
817 AttrBuilder &addByValAttr(Type *Ty);
818
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100819 /// Add an allocsize attribute, using the representation returned by
820 /// Attribute.getIntValue().
821 AttrBuilder &addAllocSizeAttrFromRawRepr(uint64_t RawAllocSizeRepr);
822
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100823 /// Return true if the builder contains no target-independent
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100824 /// attributes.
825 bool empty() const { return Attrs.none(); }
826
827 // Iterators for target-dependent attributes.
828 using td_type = std::pair<std::string, std::string>;
829 using td_iterator = std::map<std::string, std::string>::iterator;
830 using td_const_iterator = std::map<std::string, std::string>::const_iterator;
831 using td_range = iterator_range<td_iterator>;
832 using td_const_range = iterator_range<td_const_iterator>;
833
834 td_iterator td_begin() { return TargetDepAttrs.begin(); }
835 td_iterator td_end() { return TargetDepAttrs.end(); }
836
837 td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
838 td_const_iterator td_end() const { return TargetDepAttrs.end(); }
839
840 td_range td_attrs() { return td_range(td_begin(), td_end()); }
841
842 td_const_range td_attrs() const {
843 return td_const_range(td_begin(), td_end());
844 }
845
846 bool td_empty() const { return TargetDepAttrs.empty(); }
847
848 bool operator==(const AttrBuilder &B);
849 bool operator!=(const AttrBuilder &B) {
850 return !(*this == B);
851 }
852};
853
854namespace AttributeFuncs {
855
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100856/// Which attributes cannot be applied to a type.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100857AttrBuilder typeIncompatible(Type *Ty);
858
859/// \returns Return true if the two functions have compatible target-independent
860/// attributes for inlining purposes.
861bool areInlineCompatible(const Function &Caller, const Function &Callee);
862
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100863/// Merge caller's and callee's attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100864void mergeAttributesForInlining(Function &Caller, const Function &Callee);
865
866} // end namespace AttributeFuncs
867
868} // end namespace llvm
869
870#endif // LLVM_IR_ATTRIBUTES_H