blob: 8d7f4018e846c70b0e49d45746e824b57254e106 [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());
93
Andrew Scullcdfcccc2018-10-05 20:58:37 +010094 /// Return a uniquified Attribute object that has the specific
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010095 /// alignment set.
96 static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
97 static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
98 static Attribute getWithDereferenceableBytes(LLVMContext &Context,
99 uint64_t Bytes);
100 static Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context,
101 uint64_t Bytes);
102 static Attribute getWithAllocSizeArgs(LLVMContext &Context,
103 unsigned ElemSizeArg,
104 const Optional<unsigned> &NumElemsArg);
105
106 //===--------------------------------------------------------------------===//
107 // Attribute Accessors
108 //===--------------------------------------------------------------------===//
109
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100110 /// Return true if the attribute is an Attribute::AttrKind type.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100111 bool isEnumAttribute() const;
112
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100113 /// Return true if the attribute is an integer attribute.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100114 bool isIntAttribute() const;
115
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100116 /// Return true if the attribute is a string (target-dependent)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100117 /// attribute.
118 bool isStringAttribute() const;
119
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100120 /// Return true if the attribute is present.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100121 bool hasAttribute(AttrKind Val) const;
122
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100123 /// Return true if the target-dependent attribute is present.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100124 bool hasAttribute(StringRef Val) const;
125
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100126 /// Return the attribute's kind as an enum (Attribute::AttrKind). This
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100127 /// requires the attribute to be an enum or integer attribute.
128 Attribute::AttrKind getKindAsEnum() const;
129
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100130 /// Return the attribute's value as an integer. This requires that the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100131 /// attribute be an integer attribute.
132 uint64_t getValueAsInt() const;
133
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100134 /// Return the attribute's kind as a string. This requires the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100135 /// attribute to be a string attribute.
136 StringRef getKindAsString() const;
137
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100138 /// Return the attribute's value as a string. This requires the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100139 /// attribute to be a string attribute.
140 StringRef getValueAsString() const;
141
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100142 /// Returns the alignment field of an attribute as a byte alignment
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100143 /// value.
144 unsigned getAlignment() const;
145
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100146 /// Returns the stack alignment field of an attribute as a byte
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100147 /// alignment value.
148 unsigned getStackAlignment() const;
149
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100150 /// Returns the number of dereferenceable bytes from the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100151 /// dereferenceable attribute.
152 uint64_t getDereferenceableBytes() const;
153
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100154 /// Returns the number of dereferenceable_or_null bytes from the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100155 /// dereferenceable_or_null attribute.
156 uint64_t getDereferenceableOrNullBytes() const;
157
158 /// Returns the argument numbers for the allocsize attribute (or pair(0, 0)
159 /// if not known).
160 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
161
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100162 /// The Attribute is converted to a string of equivalent mnemonic. This
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100163 /// is, presumably, for writing out the mnemonics for the assembly writer.
164 std::string getAsString(bool InAttrGrp = false) const;
165
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100166 /// Equality and non-equality operators.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100167 bool operator==(Attribute A) const { return pImpl == A.pImpl; }
168 bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
169
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100170 /// Less-than operator. Useful for sorting the attributes list.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100171 bool operator<(Attribute A) const;
172
173 void Profile(FoldingSetNodeID &ID) const {
174 ID.AddPointer(pImpl);
175 }
176
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100177 /// Return a raw pointer that uniquely identifies this attribute.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100178 void *getRawPointer() const {
179 return pImpl;
180 }
181
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100182 /// Get an attribute from a raw pointer created by getRawPointer.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100183 static Attribute fromRawPointer(void *RawPtr) {
184 return Attribute(reinterpret_cast<AttributeImpl*>(RawPtr));
185 }
186};
187
188// Specialized opaque value conversions.
189inline LLVMAttributeRef wrap(Attribute Attr) {
190 return reinterpret_cast<LLVMAttributeRef>(Attr.getRawPointer());
191}
192
193// Specialized opaque value conversions.
194inline Attribute unwrap(LLVMAttributeRef Attr) {
195 return Attribute::fromRawPointer(Attr);
196}
197
198//===----------------------------------------------------------------------===//
199/// \class
200/// This class holds the attributes for a particular argument, parameter,
201/// function, or return value. It is an immutable value type that is cheap to
202/// copy. Adding and removing enum attributes is intended to be fast, but adding
203/// and removing string or integer attributes involves a FoldingSet lookup.
204class AttributeSet {
205 friend AttributeListImpl;
206 template <typename Ty> friend struct DenseMapInfo;
207
208 // TODO: Extract AvailableAttrs from AttributeSetNode and store them here.
209 // This will allow an efficient implementation of addAttribute and
210 // removeAttribute for enum attrs.
211
212 /// Private implementation pointer.
213 AttributeSetNode *SetNode = nullptr;
214
215private:
216 explicit AttributeSet(AttributeSetNode *ASN) : SetNode(ASN) {}
217
218public:
219 /// AttributeSet is a trivially copyable value type.
220 AttributeSet() = default;
221 AttributeSet(const AttributeSet &) = default;
222 ~AttributeSet() = default;
223
224 static AttributeSet get(LLVMContext &C, const AttrBuilder &B);
225 static AttributeSet get(LLVMContext &C, ArrayRef<Attribute> Attrs);
226
227 bool operator==(const AttributeSet &O) const { return SetNode == O.SetNode; }
228 bool operator!=(const AttributeSet &O) const { return !(*this == O); }
229
230 /// Add an argument attribute. Returns a new set because attribute sets are
231 /// immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100232 LLVM_NODISCARD AttributeSet addAttribute(LLVMContext &C,
233 Attribute::AttrKind Kind) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100234
235 /// Add a target-dependent attribute. Returns a new set because attribute sets
236 /// are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100237 LLVM_NODISCARD AttributeSet addAttribute(LLVMContext &C, StringRef Kind,
238 StringRef Value = StringRef()) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100239
240 /// Add attributes to the attribute set. Returns a new set because attribute
241 /// sets are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100242 LLVM_NODISCARD AttributeSet addAttributes(LLVMContext &C,
243 AttributeSet AS) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100244
245 /// Remove the specified attribute from this set. Returns a new set because
246 /// attribute sets are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100247 LLVM_NODISCARD AttributeSet removeAttribute(LLVMContext &C,
248 Attribute::AttrKind Kind) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100249
250 /// Remove the specified attribute from this set. Returns a new set because
251 /// attribute sets are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100252 LLVM_NODISCARD AttributeSet removeAttribute(LLVMContext &C,
253 StringRef Kind) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100254
255 /// Remove the specified attributes from this set. Returns a new set because
256 /// attribute sets are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100257 LLVM_NODISCARD AttributeSet
258 removeAttributes(LLVMContext &C, const AttrBuilder &AttrsToRemove) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100259
260 /// Return the number of attributes in this set.
261 unsigned getNumAttributes() const;
262
263 /// Return true if attributes exists in this set.
264 bool hasAttributes() const { return SetNode != nullptr; }
265
266 /// Return true if the attribute exists in this set.
267 bool hasAttribute(Attribute::AttrKind Kind) const;
268
269 /// Return true if the attribute exists in this set.
270 bool hasAttribute(StringRef Kind) const;
271
272 /// Return the attribute object.
273 Attribute getAttribute(Attribute::AttrKind Kind) const;
274
275 /// Return the target-dependent attribute object.
276 Attribute getAttribute(StringRef Kind) const;
277
278 unsigned getAlignment() const;
279 unsigned getStackAlignment() const;
280 uint64_t getDereferenceableBytes() const;
281 uint64_t getDereferenceableOrNullBytes() const;
282 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
283 std::string getAsString(bool InAttrGrp = false) const;
284
285 using iterator = const Attribute *;
286
287 iterator begin() const;
288 iterator end() const;
289#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
290 void dump() const;
291#endif
292};
293
294//===----------------------------------------------------------------------===//
295/// \class
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100296/// Provide DenseMapInfo for AttributeSet.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100297template <> struct DenseMapInfo<AttributeSet> {
298 static AttributeSet getEmptyKey() {
299 auto Val = static_cast<uintptr_t>(-1);
300 Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
301 return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
302 }
303
304 static AttributeSet getTombstoneKey() {
305 auto Val = static_cast<uintptr_t>(-2);
306 Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
307 return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
308 }
309
310 static unsigned getHashValue(AttributeSet AS) {
311 return (unsigned((uintptr_t)AS.SetNode) >> 4) ^
312 (unsigned((uintptr_t)AS.SetNode) >> 9);
313 }
314
315 static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
316};
317
318//===----------------------------------------------------------------------===//
319/// \class
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100320/// This class holds the attributes for a function, its return value, and
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100321/// its parameters. You access the attributes for each of them via an index into
322/// the AttributeList object. The function attributes are at index
323/// `AttributeList::FunctionIndex', the return value is at index
324/// `AttributeList::ReturnIndex', and the attributes for the parameters start at
325/// index `AttributeList::FirstArgIndex'.
326class AttributeList {
327public:
328 enum AttrIndex : unsigned {
329 ReturnIndex = 0U,
330 FunctionIndex = ~0U,
331 FirstArgIndex = 1,
332 };
333
334private:
335 friend class AttrBuilder;
336 friend class AttributeListImpl;
337 friend class AttributeSet;
338 friend class AttributeSetNode;
339 template <typename Ty> friend struct DenseMapInfo;
340
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100341 /// The attributes that we are managing. This can be null to represent
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100342 /// the empty attributes list.
343 AttributeListImpl *pImpl = nullptr;
344
345public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100346 /// Create an AttributeList with the specified parameters in it.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100347 static AttributeList get(LLVMContext &C,
348 ArrayRef<std::pair<unsigned, Attribute>> Attrs);
349 static AttributeList get(LLVMContext &C,
350 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs);
351
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100352 /// Create an AttributeList from attribute sets for a function, its
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100353 /// return value, and all of its arguments.
354 static AttributeList get(LLVMContext &C, AttributeSet FnAttrs,
355 AttributeSet RetAttrs,
356 ArrayRef<AttributeSet> ArgAttrs);
357
358private:
359 explicit AttributeList(AttributeListImpl *LI) : pImpl(LI) {}
360
361 static AttributeList getImpl(LLVMContext &C, ArrayRef<AttributeSet> AttrSets);
362
363public:
364 AttributeList() = default;
365
366 //===--------------------------------------------------------------------===//
367 // AttributeList Construction and Mutation
368 //===--------------------------------------------------------------------===//
369
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100370 /// Return an AttributeList with the specified parameters in it.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100371 static AttributeList get(LLVMContext &C, ArrayRef<AttributeList> Attrs);
372 static AttributeList get(LLVMContext &C, unsigned Index,
373 ArrayRef<Attribute::AttrKind> Kinds);
374 static AttributeList get(LLVMContext &C, unsigned Index,
375 ArrayRef<StringRef> Kind);
376 static AttributeList get(LLVMContext &C, unsigned Index,
377 const AttrBuilder &B);
378
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100379 /// Add an attribute to the attribute set at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100380 /// Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100381 LLVM_NODISCARD AttributeList addAttribute(LLVMContext &C, unsigned Index,
382 Attribute::AttrKind Kind) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100383
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100384 /// Add an attribute to the attribute set at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100385 /// Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100386 LLVM_NODISCARD AttributeList
387 addAttribute(LLVMContext &C, unsigned Index, StringRef Kind,
388 StringRef Value = StringRef()) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100389
390 /// Add an attribute to the attribute set at the given index.
391 /// Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100392 LLVM_NODISCARD AttributeList addAttribute(LLVMContext &C, unsigned Index,
393 Attribute A) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100394
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100395 /// Add attributes to the attribute set at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100396 /// Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100397 LLVM_NODISCARD AttributeList addAttributes(LLVMContext &C, unsigned Index,
398 const AttrBuilder &B) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100399
400 /// Add an argument attribute to the list. Returns a new list because
401 /// attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100402 LLVM_NODISCARD AttributeList addParamAttribute(
403 LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100404 return addAttribute(C, ArgNo + FirstArgIndex, Kind);
405 }
406
407 /// Add an argument attribute to the list. Returns a new list because
408 /// attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100409 LLVM_NODISCARD AttributeList
410 addParamAttribute(LLVMContext &C, unsigned ArgNo, StringRef Kind,
411 StringRef Value = StringRef()) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100412 return addAttribute(C, ArgNo + FirstArgIndex, Kind, Value);
413 }
414
415 /// Add an attribute to the attribute list at the given arg indices. Returns a
416 /// new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100417 LLVM_NODISCARD AttributeList addParamAttribute(LLVMContext &C,
418 ArrayRef<unsigned> ArgNos,
419 Attribute A) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100420
421 /// Add an argument attribute to the list. Returns a new list because
422 /// attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100423 LLVM_NODISCARD AttributeList addParamAttributes(LLVMContext &C,
424 unsigned ArgNo,
425 const AttrBuilder &B) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100426 return addAttributes(C, ArgNo + FirstArgIndex, B);
427 }
428
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100429 /// Remove the specified attribute at the specified index from this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100430 /// attribute list. Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100431 LLVM_NODISCARD AttributeList removeAttribute(LLVMContext &C, unsigned Index,
432 Attribute::AttrKind Kind) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100433
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100434 /// Remove the specified attribute at the specified index from this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100435 /// attribute list. Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100436 LLVM_NODISCARD AttributeList removeAttribute(LLVMContext &C, unsigned Index,
437 StringRef Kind) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100438
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100439 /// Remove the specified attributes 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 removeAttributes(
442 LLVMContext &C, unsigned Index, const AttrBuilder &AttrsToRemove) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100443
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100444 /// Remove all attributes 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 removeAttributes(LLVMContext &C,
447 unsigned Index) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100448
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100449 /// Remove the specified attribute at the specified arg 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 removeParamAttribute(
452 LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100453 return removeAttribute(C, ArgNo + FirstArgIndex, Kind);
454 }
455
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100456 /// Remove the specified attribute at the specified arg index from this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100457 /// attribute list. Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100458 LLVM_NODISCARD AttributeList removeParamAttribute(LLVMContext &C,
459 unsigned ArgNo,
460 StringRef Kind) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100461 return removeAttribute(C, ArgNo + FirstArgIndex, Kind);
462 }
463
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100464 /// Remove the specified attribute at the specified arg index from this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100465 /// attribute list. Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100466 LLVM_NODISCARD AttributeList removeParamAttributes(
467 LLVMContext &C, unsigned ArgNo, const AttrBuilder &AttrsToRemove) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100468 return removeAttributes(C, ArgNo + FirstArgIndex, AttrsToRemove);
469 }
470
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100471 /// Remove all attributes at the specified arg index from this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100472 /// attribute list. Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100473 LLVM_NODISCARD AttributeList removeParamAttributes(LLVMContext &C,
474 unsigned ArgNo) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100475 return removeAttributes(C, ArgNo + FirstArgIndex);
476 }
477
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100478 /// \brief Add the dereferenceable attribute to the attribute set at the given
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100479 /// index. Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100480 LLVM_NODISCARD AttributeList addDereferenceableAttr(LLVMContext &C,
481 unsigned Index,
482 uint64_t Bytes) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100483
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100484 /// \brief Add the dereferenceable attribute to the attribute set at the given
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100485 /// arg index. Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100486 LLVM_NODISCARD AttributeList addDereferenceableParamAttr(
487 LLVMContext &C, unsigned ArgNo, uint64_t Bytes) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100488 return addDereferenceableAttr(C, ArgNo + FirstArgIndex, Bytes);
489 }
490
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100491 /// Add the dereferenceable_or_null attribute to the attribute set at
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100492 /// the given index. Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100493 LLVM_NODISCARD AttributeList addDereferenceableOrNullAttr(
494 LLVMContext &C, unsigned Index, uint64_t Bytes) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100495
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100496 /// Add the dereferenceable_or_null attribute to the attribute set at
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100497 /// the given arg index. Returns a new list because attribute lists are
498 /// immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100499 LLVM_NODISCARD AttributeList addDereferenceableOrNullParamAttr(
500 LLVMContext &C, unsigned ArgNo, uint64_t Bytes) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100501 return addDereferenceableOrNullAttr(C, ArgNo + FirstArgIndex, Bytes);
502 }
503
504 /// Add the allocsize attribute to the attribute set at the given index.
505 /// Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100506 LLVM_NODISCARD AttributeList
507 addAllocSizeAttr(LLVMContext &C, unsigned Index, unsigned ElemSizeArg,
508 const Optional<unsigned> &NumElemsArg);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100509
510 /// Add the allocsize attribute to the attribute set at the given arg index.
511 /// Returns a new list because attribute lists are immutable.
Andrew Walbran16937d02019-10-22 13:54:20 +0100512 LLVM_NODISCARD AttributeList
513 addAllocSizeParamAttr(LLVMContext &C, unsigned ArgNo, unsigned ElemSizeArg,
514 const Optional<unsigned> &NumElemsArg) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100515 return addAllocSizeAttr(C, ArgNo + FirstArgIndex, ElemSizeArg, NumElemsArg);
516 }
517
518 //===--------------------------------------------------------------------===//
519 // AttributeList Accessors
520 //===--------------------------------------------------------------------===//
521
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100522 /// Retrieve the LLVM context.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100523 LLVMContext &getContext() const;
524
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100525 /// The attributes for the specified index are returned.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100526 AttributeSet getAttributes(unsigned Index) const;
527
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100528 /// The attributes for the argument or parameter at the given index are
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100529 /// returned.
530 AttributeSet getParamAttributes(unsigned ArgNo) const;
531
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100532 /// The attributes for the ret value are returned.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100533 AttributeSet getRetAttributes() const;
534
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100535 /// The function attributes are returned.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100536 AttributeSet getFnAttributes() const;
537
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100538 /// Return true if the attribute exists at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100539 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
540
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100541 /// Return true if the attribute exists at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100542 bool hasAttribute(unsigned Index, StringRef Kind) const;
543
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100544 /// Return true if attribute exists at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100545 bool hasAttributes(unsigned Index) const;
546
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100547 /// Return true if the attribute exists for the given argument
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100548 bool hasParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
549 return hasAttribute(ArgNo + FirstArgIndex, Kind);
550 }
551
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100552 /// Return true if the attribute exists for the given argument
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100553 bool hasParamAttr(unsigned ArgNo, StringRef Kind) const {
554 return hasAttribute(ArgNo + FirstArgIndex, Kind);
555 }
556
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100557 /// Return true if attributes exists for the given argument
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100558 bool hasParamAttrs(unsigned ArgNo) const {
559 return hasAttributes(ArgNo + FirstArgIndex);
560 }
561
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100562 /// Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100563 /// may be faster.
564 bool hasFnAttribute(Attribute::AttrKind Kind) const;
565
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100566 /// Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100567 /// may be faster.
568 bool hasFnAttribute(StringRef Kind) const;
569
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100570 /// Equivalent to hasAttribute(ArgNo + FirstArgIndex, Kind).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100571 bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const;
572
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100573 /// Return true if the specified attribute is set for at least one
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100574 /// parameter or for the return value. If Index is not nullptr, the index
575 /// of a parameter with the specified attribute is provided.
576 bool hasAttrSomewhere(Attribute::AttrKind Kind,
577 unsigned *Index = nullptr) const;
578
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100579 /// Return the attribute object that exists at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100580 Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
581
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100582 /// Return the attribute object that exists at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100583 Attribute getAttribute(unsigned Index, StringRef Kind) const;
584
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100585 /// Return the attribute object that exists at the arg index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100586 Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
587 return getAttribute(ArgNo + FirstArgIndex, Kind);
588 }
589
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100590 /// Return the attribute object that exists at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100591 Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const {
592 return getAttribute(ArgNo + FirstArgIndex, Kind);
593 }
594
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100595 /// Return the alignment of the return value.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100596 unsigned getRetAlignment() const;
597
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100598 /// Return the alignment for the specified function parameter.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100599 unsigned getParamAlignment(unsigned ArgNo) const;
600
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100601 /// Get the stack alignment.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100602 unsigned getStackAlignment(unsigned Index) const;
603
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100604 /// Get the number of dereferenceable bytes (or zero if unknown).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100605 uint64_t getDereferenceableBytes(unsigned Index) const;
606
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100607 /// Get the number of dereferenceable bytes (or zero if unknown) of an
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100608 /// arg.
609 uint64_t getParamDereferenceableBytes(unsigned ArgNo) const {
610 return getDereferenceableBytes(ArgNo + FirstArgIndex);
611 }
612
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100613 /// Get the number of dereferenceable_or_null bytes (or zero if
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100614 /// unknown).
615 uint64_t getDereferenceableOrNullBytes(unsigned Index) const;
616
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100617 /// Get the number of dereferenceable_or_null bytes (or zero if
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100618 /// unknown) of an arg.
619 uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo) const {
620 return getDereferenceableOrNullBytes(ArgNo + FirstArgIndex);
621 }
622
623 /// Get the allocsize argument numbers (or pair(0, 0) if unknown).
624 std::pair<unsigned, Optional<unsigned>>
625 getAllocSizeArgs(unsigned Index) const;
626
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100627 /// Return the attributes at the index as a string.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100628 std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
629
630 //===--------------------------------------------------------------------===//
631 // AttributeList Introspection
632 //===--------------------------------------------------------------------===//
633
634 using iterator = const AttributeSet *;
635
636 iterator begin() const;
637 iterator end() const;
638
639 unsigned getNumAttrSets() const;
640
641 /// Use these to iterate over the valid attribute indices.
642 unsigned index_begin() const { return AttributeList::FunctionIndex; }
643 unsigned index_end() const { return getNumAttrSets() - 1; }
644
645 /// operator==/!= - Provide equality predicates.
646 bool operator==(const AttributeList &RHS) const { return pImpl == RHS.pImpl; }
647 bool operator!=(const AttributeList &RHS) const { return pImpl != RHS.pImpl; }
648
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100649 /// Return a raw pointer that uniquely identifies this attribute list.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100650 void *getRawPointer() const {
651 return pImpl;
652 }
653
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100654 /// Return true if there are no attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100655 bool isEmpty() const { return pImpl == nullptr; }
656
657 void dump() const;
658};
659
660//===----------------------------------------------------------------------===//
661/// \class
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100662/// Provide DenseMapInfo for AttributeList.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100663template <> struct DenseMapInfo<AttributeList> {
664 static AttributeList getEmptyKey() {
665 auto Val = static_cast<uintptr_t>(-1);
666 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
667 return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
668 }
669
670 static AttributeList getTombstoneKey() {
671 auto Val = static_cast<uintptr_t>(-2);
672 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
673 return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
674 }
675
676 static unsigned getHashValue(AttributeList AS) {
677 return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
678 (unsigned((uintptr_t)AS.pImpl) >> 9);
679 }
680
681 static bool isEqual(AttributeList LHS, AttributeList RHS) {
682 return LHS == RHS;
683 }
684};
685
686//===----------------------------------------------------------------------===//
687/// \class
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100688/// This class is used in conjunction with the Attribute::get method to
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100689/// create an Attribute object. The object itself is uniquified. The Builder's
690/// value, however, is not. So this can be used as a quick way to test for
691/// equality, presence of attributes, etc.
692class AttrBuilder {
693 std::bitset<Attribute::EndAttrKinds> Attrs;
694 std::map<std::string, std::string> TargetDepAttrs;
695 uint64_t Alignment = 0;
696 uint64_t StackAlignment = 0;
697 uint64_t DerefBytes = 0;
698 uint64_t DerefOrNullBytes = 0;
699 uint64_t AllocSizeArgs = 0;
700
701public:
702 AttrBuilder() = default;
703
704 AttrBuilder(const Attribute &A) {
705 addAttribute(A);
706 }
707
708 AttrBuilder(AttributeList AS, unsigned Idx);
709 AttrBuilder(AttributeSet AS);
710
711 void clear();
712
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100713 /// Add an attribute to the builder.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100714 AttrBuilder &addAttribute(Attribute::AttrKind Val);
715
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100716 /// Add the Attribute object to the builder.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100717 AttrBuilder &addAttribute(Attribute A);
718
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100719 /// Add the target-dependent attribute to the builder.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100720 AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
721
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100722 /// Remove an attribute from the builder.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100723 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
724
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100725 /// Remove the attributes from the builder.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100726 AttrBuilder &removeAttributes(AttributeList A, uint64_t WithoutIndex);
727
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100728 /// Remove the target-dependent attribute to the builder.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100729 AttrBuilder &removeAttribute(StringRef A);
730
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100731 /// Add the attributes from the builder.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100732 AttrBuilder &merge(const AttrBuilder &B);
733
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100734 /// Remove the attributes from the builder.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100735 AttrBuilder &remove(const AttrBuilder &B);
736
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100737 /// Return true if the builder has any attribute that's in the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100738 /// specified builder.
739 bool overlaps(const AttrBuilder &B) const;
740
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100741 /// Return true if the builder has the specified attribute.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100742 bool contains(Attribute::AttrKind A) const {
743 assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
744 return Attrs[A];
745 }
746
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100747 /// Return true if the builder has the specified target-dependent
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100748 /// attribute.
749 bool contains(StringRef A) const;
750
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100751 /// Return true if the builder has IR-level attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100752 bool hasAttributes() const;
753
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100754 /// Return true if the builder has any attribute that's in the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100755 /// specified attribute.
756 bool hasAttributes(AttributeList A, uint64_t Index) const;
757
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100758 /// Return true if the builder has an alignment attribute.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100759 bool hasAlignmentAttr() const;
760
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100761 /// Retrieve the alignment attribute, if it exists.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100762 uint64_t getAlignment() const { return Alignment; }
763
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100764 /// Retrieve the stack alignment attribute, if it exists.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100765 uint64_t getStackAlignment() const { return StackAlignment; }
766
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100767 /// Retrieve the number of dereferenceable bytes, if the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100768 /// dereferenceable attribute exists (zero is returned otherwise).
769 uint64_t getDereferenceableBytes() const { return DerefBytes; }
770
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100771 /// Retrieve the number of dereferenceable_or_null bytes, if the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100772 /// dereferenceable_or_null attribute exists (zero is returned otherwise).
773 uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; }
774
775 /// Retrieve the allocsize args, if the allocsize attribute exists. If it
776 /// doesn't exist, pair(0, 0) is returned.
777 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
778
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100779 /// This turns an int alignment (which must be a power of 2) into the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100780 /// form used internally in Attribute.
781 AttrBuilder &addAlignmentAttr(unsigned Align);
782
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100783 /// This turns an int stack alignment (which must be a power of 2) into
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100784 /// the form used internally in Attribute.
785 AttrBuilder &addStackAlignmentAttr(unsigned Align);
786
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100787 /// This turns the number of dereferenceable bytes into the form used
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100788 /// internally in Attribute.
789 AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
790
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100791 /// This turns the number of dereferenceable_or_null bytes into the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100792 /// form used internally in Attribute.
793 AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
794
795 /// This turns one (or two) ints into the form used internally in Attribute.
796 AttrBuilder &addAllocSizeAttr(unsigned ElemSizeArg,
797 const Optional<unsigned> &NumElemsArg);
798
799 /// Add an allocsize attribute, using the representation returned by
800 /// Attribute.getIntValue().
801 AttrBuilder &addAllocSizeAttrFromRawRepr(uint64_t RawAllocSizeRepr);
802
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100803 /// Return true if the builder contains no target-independent
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100804 /// attributes.
805 bool empty() const { return Attrs.none(); }
806
807 // Iterators for target-dependent attributes.
808 using td_type = std::pair<std::string, std::string>;
809 using td_iterator = std::map<std::string, std::string>::iterator;
810 using td_const_iterator = std::map<std::string, std::string>::const_iterator;
811 using td_range = iterator_range<td_iterator>;
812 using td_const_range = iterator_range<td_const_iterator>;
813
814 td_iterator td_begin() { return TargetDepAttrs.begin(); }
815 td_iterator td_end() { return TargetDepAttrs.end(); }
816
817 td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
818 td_const_iterator td_end() const { return TargetDepAttrs.end(); }
819
820 td_range td_attrs() { return td_range(td_begin(), td_end()); }
821
822 td_const_range td_attrs() const {
823 return td_const_range(td_begin(), td_end());
824 }
825
826 bool td_empty() const { return TargetDepAttrs.empty(); }
827
828 bool operator==(const AttrBuilder &B);
829 bool operator!=(const AttrBuilder &B) {
830 return !(*this == B);
831 }
832};
833
834namespace AttributeFuncs {
835
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100836/// Which attributes cannot be applied to a type.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100837AttrBuilder typeIncompatible(Type *Ty);
838
839/// \returns Return true if the two functions have compatible target-independent
840/// attributes for inlining purposes.
841bool areInlineCompatible(const Function &Caller, const Function &Callee);
842
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100843/// Merge caller's and callee's attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100844void mergeAttributesForInlining(Function &Caller, const Function &Callee);
845
846} // end namespace AttributeFuncs
847
848} // end namespace llvm
849
850#endif // LLVM_IR_ATTRIBUTES_H