blob: 5aaaaf3c396bdc812ba2040fb5bde7d01a57ccdb [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- llvm/Attributes.h - Container for Attributes -------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
Andrew Scullcdfcccc2018-10-05 20:58:37 +010011/// This file contains the simple types necessary to represent the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010012/// attributes associated with functions and their calls.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_IR_ATTRIBUTES_H
17#define LLVM_IR_ATTRIBUTES_H
18
19#include "llvm-c/Types.h"
20#include "llvm/ADT/ArrayRef.h"
21#include "llvm/ADT/FoldingSet.h"
22#include "llvm/ADT/Optional.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/ADT/iterator_range.h"
Andrew Scullcdfcccc2018-10-05 20:58:37 +010025#include "llvm/Config/llvm-config.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010026#include "llvm/Support/PointerLikeTypeTraits.h"
27#include <bitset>
28#include <cassert>
29#include <cstdint>
30#include <map>
31#include <string>
32#include <utility>
33
34namespace llvm {
35
36class AttrBuilder;
37class AttributeImpl;
38class AttributeListImpl;
39class AttributeSetNode;
40template<typename T> struct DenseMapInfo;
41class Function;
42class LLVMContext;
43class Type;
44
45//===----------------------------------------------------------------------===//
46/// \class
Andrew Scullcdfcccc2018-10-05 20:58:37 +010047/// Functions, function parameters, and return types can have attributes
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010048/// to indicate how they should be treated by optimizations and code
49/// generation. This class represents one of those attributes. It's light-weight
50/// and should be passed around by-value.
51class Attribute {
52public:
53 /// This enumeration lists the attributes that can be associated with
54 /// parameters, function results, or the function itself.
55 ///
56 /// Note: The `uwtable' attribute is about the ABI or the user mandating an
57 /// entry in the unwind table. The `nounwind' attribute is about an exception
58 /// passing by the function.
59 ///
60 /// In a theoretical system that uses tables for profiling and SjLj for
61 /// exceptions, they would be fully independent. In a normal system that uses
62 /// tables for both, the semantics are:
63 ///
64 /// nil = Needs an entry because an exception might pass by.
65 /// nounwind = No need for an entry
66 /// uwtable = Needs an entry because the ABI says so and because
67 /// an exception might pass by.
68 /// uwtable + nounwind = Needs an entry because the ABI says so.
69
70 enum AttrKind {
71 // IR-Level Attributes
72 None, ///< No attributes have been set
73 #define GET_ATTR_ENUM
Andrew Scullcdfcccc2018-10-05 20:58:37 +010074 #include "llvm/IR/Attributes.inc"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010075 EndAttrKinds ///< Sentinal value useful for loops
76 };
77
78private:
79 AttributeImpl *pImpl = nullptr;
80
81 Attribute(AttributeImpl *A) : pImpl(A) {}
82
83public:
84 Attribute() = default;
85
86 //===--------------------------------------------------------------------===//
87 // Attribute Construction
88 //===--------------------------------------------------------------------===//
89
Andrew Scullcdfcccc2018-10-05 20:58:37 +010090 /// Return a uniquified Attribute object.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010091 static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
92 static Attribute get(LLVMContext &Context, StringRef Kind,
93 StringRef Val = StringRef());
94
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);
106
107 //===--------------------------------------------------------------------===//
108 // Attribute Accessors
109 //===--------------------------------------------------------------------===//
110
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100111 /// Return true if the attribute is an Attribute::AttrKind type.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100112 bool isEnumAttribute() const;
113
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100114 /// Return true if the attribute is an integer attribute.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100115 bool isIntAttribute() const;
116
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100117 /// Return true if the attribute is a string (target-dependent)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100118 /// attribute.
119 bool isStringAttribute() const;
120
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100121 /// Return true if the attribute is present.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100122 bool hasAttribute(AttrKind Val) const;
123
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100124 /// Return true if the target-dependent attribute is present.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100125 bool hasAttribute(StringRef Val) const;
126
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100127 /// Return the attribute's kind as an enum (Attribute::AttrKind). This
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100128 /// requires the attribute to be an enum or integer attribute.
129 Attribute::AttrKind getKindAsEnum() const;
130
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100131 /// Return the attribute's value as an integer. This requires that the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100132 /// attribute be an integer attribute.
133 uint64_t getValueAsInt() const;
134
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100135 /// Return the attribute's kind as a string. This requires the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100136 /// attribute to be a string attribute.
137 StringRef getKindAsString() const;
138
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100139 /// Return the attribute's value as a string. This requires the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100140 /// attribute to be a string attribute.
141 StringRef getValueAsString() const;
142
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100143 /// Returns the alignment field of an attribute as a byte alignment
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100144 /// value.
145 unsigned getAlignment() const;
146
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100147 /// Returns the stack alignment field of an attribute as a byte
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100148 /// alignment value.
149 unsigned getStackAlignment() const;
150
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100151 /// Returns the number of dereferenceable bytes from the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100152 /// dereferenceable attribute.
153 uint64_t getDereferenceableBytes() const;
154
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100155 /// Returns the number of dereferenceable_or_null bytes from the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100156 /// dereferenceable_or_null attribute.
157 uint64_t getDereferenceableOrNullBytes() const;
158
159 /// Returns the argument numbers for the allocsize attribute (or pair(0, 0)
160 /// if not known).
161 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
162
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100163 /// The Attribute is converted to a string of equivalent mnemonic. This
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100164 /// is, presumably, for writing out the mnemonics for the assembly writer.
165 std::string getAsString(bool InAttrGrp = false) const;
166
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100167 /// Equality and non-equality operators.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100168 bool operator==(Attribute A) const { return pImpl == A.pImpl; }
169 bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
170
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100171 /// Less-than operator. Useful for sorting the attributes list.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100172 bool operator<(Attribute A) const;
173
174 void Profile(FoldingSetNodeID &ID) const {
175 ID.AddPointer(pImpl);
176 }
177
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100178 /// Return a raw pointer that uniquely identifies this attribute.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100179 void *getRawPointer() const {
180 return pImpl;
181 }
182
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100183 /// Get an attribute from a raw pointer created by getRawPointer.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100184 static Attribute fromRawPointer(void *RawPtr) {
185 return Attribute(reinterpret_cast<AttributeImpl*>(RawPtr));
186 }
187};
188
189// Specialized opaque value conversions.
190inline LLVMAttributeRef wrap(Attribute Attr) {
191 return reinterpret_cast<LLVMAttributeRef>(Attr.getRawPointer());
192}
193
194// Specialized opaque value conversions.
195inline Attribute unwrap(LLVMAttributeRef Attr) {
196 return Attribute::fromRawPointer(Attr);
197}
198
199//===----------------------------------------------------------------------===//
200/// \class
201/// This class holds the attributes for a particular argument, parameter,
202/// function, or return value. It is an immutable value type that is cheap to
203/// copy. Adding and removing enum attributes is intended to be fast, but adding
204/// and removing string or integer attributes involves a FoldingSet lookup.
205class AttributeSet {
206 friend AttributeListImpl;
207 template <typename Ty> friend struct DenseMapInfo;
208
209 // TODO: Extract AvailableAttrs from AttributeSetNode and store them here.
210 // This will allow an efficient implementation of addAttribute and
211 // removeAttribute for enum attrs.
212
213 /// Private implementation pointer.
214 AttributeSetNode *SetNode = nullptr;
215
216private:
217 explicit AttributeSet(AttributeSetNode *ASN) : SetNode(ASN) {}
218
219public:
220 /// AttributeSet is a trivially copyable value type.
221 AttributeSet() = default;
222 AttributeSet(const AttributeSet &) = default;
223 ~AttributeSet() = default;
224
225 static AttributeSet get(LLVMContext &C, const AttrBuilder &B);
226 static AttributeSet get(LLVMContext &C, ArrayRef<Attribute> Attrs);
227
228 bool operator==(const AttributeSet &O) const { return SetNode == O.SetNode; }
229 bool operator!=(const AttributeSet &O) const { return !(*this == O); }
230
231 /// Add an argument attribute. Returns a new set because attribute sets are
232 /// immutable.
233 AttributeSet addAttribute(LLVMContext &C, Attribute::AttrKind Kind) const;
234
235 /// Add a target-dependent attribute. Returns a new set because attribute sets
236 /// are immutable.
237 AttributeSet addAttribute(LLVMContext &C, StringRef Kind,
238 StringRef Value = StringRef()) const;
239
240 /// Add attributes to the attribute set. Returns a new set because attribute
241 /// sets are immutable.
242 AttributeSet addAttributes(LLVMContext &C, AttributeSet AS) const;
243
244 /// Remove the specified attribute from this set. Returns a new set because
245 /// attribute sets are immutable.
246 AttributeSet removeAttribute(LLVMContext &C, Attribute::AttrKind Kind) const;
247
248 /// Remove the specified attribute from this set. Returns a new set because
249 /// attribute sets are immutable.
250 AttributeSet removeAttribute(LLVMContext &C, StringRef Kind) const;
251
252 /// Remove the specified attributes from this set. Returns a new set because
253 /// attribute sets are immutable.
254 AttributeSet removeAttributes(LLVMContext &C,
255 const AttrBuilder &AttrsToRemove) const;
256
257 /// Return the number of attributes in this set.
258 unsigned getNumAttributes() const;
259
260 /// Return true if attributes exists in this set.
261 bool hasAttributes() const { return SetNode != nullptr; }
262
263 /// Return true if the attribute exists in this set.
264 bool hasAttribute(Attribute::AttrKind Kind) const;
265
266 /// Return true if the attribute exists in this set.
267 bool hasAttribute(StringRef Kind) const;
268
269 /// Return the attribute object.
270 Attribute getAttribute(Attribute::AttrKind Kind) const;
271
272 /// Return the target-dependent attribute object.
273 Attribute getAttribute(StringRef Kind) const;
274
275 unsigned getAlignment() const;
276 unsigned getStackAlignment() const;
277 uint64_t getDereferenceableBytes() const;
278 uint64_t getDereferenceableOrNullBytes() const;
279 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
280 std::string getAsString(bool InAttrGrp = false) const;
281
282 using iterator = const Attribute *;
283
284 iterator begin() const;
285 iterator end() const;
286#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
287 void dump() const;
288#endif
289};
290
291//===----------------------------------------------------------------------===//
292/// \class
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100293/// Provide DenseMapInfo for AttributeSet.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100294template <> struct DenseMapInfo<AttributeSet> {
295 static AttributeSet getEmptyKey() {
296 auto Val = static_cast<uintptr_t>(-1);
297 Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
298 return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
299 }
300
301 static AttributeSet getTombstoneKey() {
302 auto Val = static_cast<uintptr_t>(-2);
303 Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
304 return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
305 }
306
307 static unsigned getHashValue(AttributeSet AS) {
308 return (unsigned((uintptr_t)AS.SetNode) >> 4) ^
309 (unsigned((uintptr_t)AS.SetNode) >> 9);
310 }
311
312 static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
313};
314
315//===----------------------------------------------------------------------===//
316/// \class
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100317/// This class holds the attributes for a function, its return value, and
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100318/// its parameters. You access the attributes for each of them via an index into
319/// the AttributeList object. The function attributes are at index
320/// `AttributeList::FunctionIndex', the return value is at index
321/// `AttributeList::ReturnIndex', and the attributes for the parameters start at
322/// index `AttributeList::FirstArgIndex'.
323class AttributeList {
324public:
325 enum AttrIndex : unsigned {
326 ReturnIndex = 0U,
327 FunctionIndex = ~0U,
328 FirstArgIndex = 1,
329 };
330
331private:
332 friend class AttrBuilder;
333 friend class AttributeListImpl;
334 friend class AttributeSet;
335 friend class AttributeSetNode;
336 template <typename Ty> friend struct DenseMapInfo;
337
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100338 /// The attributes that we are managing. This can be null to represent
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100339 /// the empty attributes list.
340 AttributeListImpl *pImpl = nullptr;
341
342public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100343 /// Create an AttributeList with the specified parameters in it.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100344 static AttributeList get(LLVMContext &C,
345 ArrayRef<std::pair<unsigned, Attribute>> Attrs);
346 static AttributeList get(LLVMContext &C,
347 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs);
348
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100349 /// Create an AttributeList from attribute sets for a function, its
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100350 /// return value, and all of its arguments.
351 static AttributeList get(LLVMContext &C, AttributeSet FnAttrs,
352 AttributeSet RetAttrs,
353 ArrayRef<AttributeSet> ArgAttrs);
354
355private:
356 explicit AttributeList(AttributeListImpl *LI) : pImpl(LI) {}
357
358 static AttributeList getImpl(LLVMContext &C, ArrayRef<AttributeSet> AttrSets);
359
360public:
361 AttributeList() = default;
362
363 //===--------------------------------------------------------------------===//
364 // AttributeList Construction and Mutation
365 //===--------------------------------------------------------------------===//
366
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100367 /// Return an AttributeList with the specified parameters in it.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100368 static AttributeList get(LLVMContext &C, ArrayRef<AttributeList> Attrs);
369 static AttributeList get(LLVMContext &C, unsigned Index,
370 ArrayRef<Attribute::AttrKind> Kinds);
371 static AttributeList get(LLVMContext &C, unsigned Index,
372 ArrayRef<StringRef> Kind);
373 static AttributeList get(LLVMContext &C, unsigned Index,
374 const AttrBuilder &B);
375
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100376 /// Add an attribute to the attribute set at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100377 /// Returns a new list because attribute lists are immutable.
378 AttributeList addAttribute(LLVMContext &C, unsigned Index,
379 Attribute::AttrKind Kind) const;
380
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100381 /// Add an attribute to the attribute set at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100382 /// Returns a new list because attribute lists are immutable.
383 AttributeList addAttribute(LLVMContext &C, unsigned Index, StringRef Kind,
384 StringRef Value = StringRef()) const;
385
386 /// Add an attribute to the attribute set at the given index.
387 /// Returns a new list because attribute lists are immutable.
388 AttributeList addAttribute(LLVMContext &C, unsigned Index, Attribute A) const;
389
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100390 /// Add attributes to the attribute set at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100391 /// Returns a new list because attribute lists are immutable.
392 AttributeList addAttributes(LLVMContext &C, unsigned Index,
393 const AttrBuilder &B) const;
394
395 /// Add an argument attribute to the list. Returns a new list because
396 /// attribute lists are immutable.
397 AttributeList addParamAttribute(LLVMContext &C, unsigned ArgNo,
398 Attribute::AttrKind Kind) const {
399 return addAttribute(C, ArgNo + FirstArgIndex, Kind);
400 }
401
402 /// Add an argument attribute to the list. Returns a new list because
403 /// attribute lists are immutable.
404 AttributeList addParamAttribute(LLVMContext &C, unsigned ArgNo,
405 StringRef Kind,
406 StringRef Value = StringRef()) const {
407 return addAttribute(C, ArgNo + FirstArgIndex, Kind, Value);
408 }
409
410 /// Add an attribute to the attribute list at the given arg indices. Returns a
411 /// new list because attribute lists are immutable.
412 AttributeList addParamAttribute(LLVMContext &C, ArrayRef<unsigned> ArgNos,
413 Attribute A) const;
414
415 /// Add an argument attribute to the list. Returns a new list because
416 /// attribute lists are immutable.
417 AttributeList addParamAttributes(LLVMContext &C, unsigned ArgNo,
418 const AttrBuilder &B) const {
419 return addAttributes(C, ArgNo + FirstArgIndex, B);
420 }
421
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100422 /// Remove the specified attribute at the specified index from this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100423 /// attribute list. Returns a new list because attribute lists are immutable.
424 AttributeList removeAttribute(LLVMContext &C, unsigned Index,
425 Attribute::AttrKind Kind) const;
426
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100427 /// Remove the specified attribute at the specified index from this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100428 /// attribute list. Returns a new list because attribute lists are immutable.
429 AttributeList removeAttribute(LLVMContext &C, unsigned Index,
430 StringRef Kind) const;
431
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100432 /// Remove the specified attributes at the specified index from this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100433 /// attribute list. Returns a new list because attribute lists are immutable.
434 AttributeList removeAttributes(LLVMContext &C, unsigned Index,
435 const AttrBuilder &AttrsToRemove) const;
436
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100437 /// Remove all attributes at the specified index from this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100438 /// attribute list. Returns a new list because attribute lists are immutable.
439 AttributeList removeAttributes(LLVMContext &C, unsigned Index) const;
440
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100441 /// Remove the specified attribute at the specified arg index from this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100442 /// attribute list. Returns a new list because attribute lists are immutable.
443 AttributeList removeParamAttribute(LLVMContext &C, unsigned ArgNo,
444 Attribute::AttrKind Kind) const {
445 return removeAttribute(C, ArgNo + FirstArgIndex, Kind);
446 }
447
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100448 /// Remove the specified attribute at the specified arg index from this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100449 /// attribute list. Returns a new list because attribute lists are immutable.
450 AttributeList removeParamAttribute(LLVMContext &C, unsigned ArgNo,
451 StringRef Kind) const {
452 return removeAttribute(C, ArgNo + FirstArgIndex, Kind);
453 }
454
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100455 /// Remove the specified attribute at the specified arg index from this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100456 /// attribute list. Returns a new list because attribute lists are immutable.
457 AttributeList removeParamAttributes(LLVMContext &C, unsigned ArgNo,
458 const AttrBuilder &AttrsToRemove) const {
459 return removeAttributes(C, ArgNo + FirstArgIndex, AttrsToRemove);
460 }
461
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100462 /// Remove all attributes at the specified arg index from this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100463 /// attribute list. Returns a new list because attribute lists are immutable.
464 AttributeList removeParamAttributes(LLVMContext &C, unsigned ArgNo) const {
465 return removeAttributes(C, ArgNo + FirstArgIndex);
466 }
467
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100468 /// \brief Add the dereferenceable attribute to the attribute set at the given
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100469 /// index. Returns a new list because attribute lists are immutable.
470 AttributeList addDereferenceableAttr(LLVMContext &C, unsigned Index,
471 uint64_t Bytes) const;
472
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100473 /// \brief Add the dereferenceable attribute to the attribute set at the given
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100474 /// arg index. Returns a new list because attribute lists are immutable.
475 AttributeList addDereferenceableParamAttr(LLVMContext &C, unsigned ArgNo,
476 uint64_t Bytes) const {
477 return addDereferenceableAttr(C, ArgNo + FirstArgIndex, Bytes);
478 }
479
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100480 /// Add the dereferenceable_or_null attribute to the attribute set at
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100481 /// the given index. Returns a new list because attribute lists are immutable.
482 AttributeList addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
483 uint64_t Bytes) const;
484
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100485 /// Add the dereferenceable_or_null attribute to the attribute set at
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100486 /// the given arg index. Returns a new list because attribute lists are
487 /// immutable.
488 AttributeList addDereferenceableOrNullParamAttr(LLVMContext &C,
489 unsigned ArgNo,
490 uint64_t Bytes) const {
491 return addDereferenceableOrNullAttr(C, ArgNo + FirstArgIndex, Bytes);
492 }
493
494 /// Add the allocsize attribute to the attribute set at the given index.
495 /// Returns a new list because attribute lists are immutable.
496 AttributeList addAllocSizeAttr(LLVMContext &C, unsigned Index,
497 unsigned ElemSizeArg,
498 const Optional<unsigned> &NumElemsArg);
499
500 /// Add the allocsize attribute to the attribute set at the given arg index.
501 /// Returns a new list because attribute lists are immutable.
502 AttributeList addAllocSizeParamAttr(LLVMContext &C, unsigned ArgNo,
503 unsigned ElemSizeArg,
504 const Optional<unsigned> &NumElemsArg) {
505 return addAllocSizeAttr(C, ArgNo + FirstArgIndex, ElemSizeArg, NumElemsArg);
506 }
507
508 //===--------------------------------------------------------------------===//
509 // AttributeList Accessors
510 //===--------------------------------------------------------------------===//
511
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100512 /// Retrieve the LLVM context.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100513 LLVMContext &getContext() const;
514
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100515 /// The attributes for the specified index are returned.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100516 AttributeSet getAttributes(unsigned Index) const;
517
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100518 /// The attributes for the argument or parameter at the given index are
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100519 /// returned.
520 AttributeSet getParamAttributes(unsigned ArgNo) const;
521
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100522 /// The attributes for the ret value are returned.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100523 AttributeSet getRetAttributes() const;
524
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100525 /// The function attributes are returned.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100526 AttributeSet getFnAttributes() const;
527
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100528 /// Return true if the attribute exists at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100529 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
530
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100531 /// Return true if the attribute exists at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100532 bool hasAttribute(unsigned Index, StringRef Kind) const;
533
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100534 /// Return true if attribute exists at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100535 bool hasAttributes(unsigned Index) const;
536
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100537 /// Return true if the attribute exists for the given argument
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100538 bool hasParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
539 return hasAttribute(ArgNo + FirstArgIndex, Kind);
540 }
541
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100542 /// Return true if the attribute exists for the given argument
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100543 bool hasParamAttr(unsigned ArgNo, StringRef Kind) const {
544 return hasAttribute(ArgNo + FirstArgIndex, Kind);
545 }
546
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100547 /// Return true if attributes exists for the given argument
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100548 bool hasParamAttrs(unsigned ArgNo) const {
549 return hasAttributes(ArgNo + FirstArgIndex);
550 }
551
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100552 /// Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100553 /// may be faster.
554 bool hasFnAttribute(Attribute::AttrKind Kind) const;
555
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100556 /// Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100557 /// may be faster.
558 bool hasFnAttribute(StringRef Kind) const;
559
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100560 /// Equivalent to hasAttribute(ArgNo + FirstArgIndex, Kind).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100561 bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const;
562
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100563 /// Return true if the specified attribute is set for at least one
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100564 /// parameter or for the return value. If Index is not nullptr, the index
565 /// of a parameter with the specified attribute is provided.
566 bool hasAttrSomewhere(Attribute::AttrKind Kind,
567 unsigned *Index = nullptr) const;
568
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100569 /// Return the attribute object that exists at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100570 Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
571
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100572 /// Return the attribute object that exists at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100573 Attribute getAttribute(unsigned Index, StringRef Kind) const;
574
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100575 /// Return the attribute object that exists at the arg index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100576 Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
577 return getAttribute(ArgNo + FirstArgIndex, Kind);
578 }
579
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100580 /// Return the attribute object that exists at the given index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100581 Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const {
582 return getAttribute(ArgNo + FirstArgIndex, Kind);
583 }
584
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100585 /// Return the alignment of the return value.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100586 unsigned getRetAlignment() const;
587
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100588 /// Return the alignment for the specified function parameter.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100589 unsigned getParamAlignment(unsigned ArgNo) const;
590
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100591 /// Get the stack alignment.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100592 unsigned getStackAlignment(unsigned Index) const;
593
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100594 /// Get the number of dereferenceable bytes (or zero if unknown).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100595 uint64_t getDereferenceableBytes(unsigned Index) const;
596
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100597 /// Get the number of dereferenceable bytes (or zero if unknown) of an
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100598 /// arg.
599 uint64_t getParamDereferenceableBytes(unsigned ArgNo) const {
600 return getDereferenceableBytes(ArgNo + FirstArgIndex);
601 }
602
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100603 /// Get the number of dereferenceable_or_null bytes (or zero if
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100604 /// unknown).
605 uint64_t getDereferenceableOrNullBytes(unsigned Index) const;
606
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100607 /// Get the number of dereferenceable_or_null bytes (or zero if
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100608 /// unknown) of an arg.
609 uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo) const {
610 return getDereferenceableOrNullBytes(ArgNo + FirstArgIndex);
611 }
612
613 /// Get the allocsize argument numbers (or pair(0, 0) if unknown).
614 std::pair<unsigned, Optional<unsigned>>
615 getAllocSizeArgs(unsigned Index) const;
616
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100617 /// Return the attributes at the index as a string.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100618 std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
619
620 //===--------------------------------------------------------------------===//
621 // AttributeList Introspection
622 //===--------------------------------------------------------------------===//
623
624 using iterator = const AttributeSet *;
625
626 iterator begin() const;
627 iterator end() const;
628
629 unsigned getNumAttrSets() const;
630
631 /// Use these to iterate over the valid attribute indices.
632 unsigned index_begin() const { return AttributeList::FunctionIndex; }
633 unsigned index_end() const { return getNumAttrSets() - 1; }
634
635 /// operator==/!= - Provide equality predicates.
636 bool operator==(const AttributeList &RHS) const { return pImpl == RHS.pImpl; }
637 bool operator!=(const AttributeList &RHS) const { return pImpl != RHS.pImpl; }
638
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100639 /// Return a raw pointer that uniquely identifies this attribute list.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100640 void *getRawPointer() const {
641 return pImpl;
642 }
643
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100644 /// Return true if there are no attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100645 bool isEmpty() const { return pImpl == nullptr; }
646
647 void dump() const;
648};
649
650//===----------------------------------------------------------------------===//
651/// \class
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100652/// Provide DenseMapInfo for AttributeList.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100653template <> struct DenseMapInfo<AttributeList> {
654 static AttributeList getEmptyKey() {
655 auto Val = static_cast<uintptr_t>(-1);
656 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
657 return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
658 }
659
660 static AttributeList getTombstoneKey() {
661 auto Val = static_cast<uintptr_t>(-2);
662 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
663 return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
664 }
665
666 static unsigned getHashValue(AttributeList AS) {
667 return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
668 (unsigned((uintptr_t)AS.pImpl) >> 9);
669 }
670
671 static bool isEqual(AttributeList LHS, AttributeList RHS) {
672 return LHS == RHS;
673 }
674};
675
676//===----------------------------------------------------------------------===//
677/// \class
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100678/// This class is used in conjunction with the Attribute::get method to
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100679/// create an Attribute object. The object itself is uniquified. The Builder's
680/// value, however, is not. So this can be used as a quick way to test for
681/// equality, presence of attributes, etc.
682class AttrBuilder {
683 std::bitset<Attribute::EndAttrKinds> Attrs;
684 std::map<std::string, std::string> TargetDepAttrs;
685 uint64_t Alignment = 0;
686 uint64_t StackAlignment = 0;
687 uint64_t DerefBytes = 0;
688 uint64_t DerefOrNullBytes = 0;
689 uint64_t AllocSizeArgs = 0;
690
691public:
692 AttrBuilder() = default;
693
694 AttrBuilder(const Attribute &A) {
695 addAttribute(A);
696 }
697
698 AttrBuilder(AttributeList AS, unsigned Idx);
699 AttrBuilder(AttributeSet AS);
700
701 void clear();
702
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100703 /// Add an attribute to the builder.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100704 AttrBuilder &addAttribute(Attribute::AttrKind Val);
705
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100706 /// Add the Attribute object to the builder.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100707 AttrBuilder &addAttribute(Attribute A);
708
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100709 /// Add the target-dependent attribute to the builder.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100710 AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
711
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100712 /// Remove an attribute from the builder.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100713 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
714
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100715 /// Remove the attributes from the builder.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100716 AttrBuilder &removeAttributes(AttributeList A, uint64_t WithoutIndex);
717
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100718 /// Remove the target-dependent attribute to the builder.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100719 AttrBuilder &removeAttribute(StringRef A);
720
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100721 /// Add the attributes from the builder.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100722 AttrBuilder &merge(const AttrBuilder &B);
723
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100724 /// Remove the attributes from the builder.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100725 AttrBuilder &remove(const AttrBuilder &B);
726
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100727 /// Return true if the builder has any attribute that's in the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100728 /// specified builder.
729 bool overlaps(const AttrBuilder &B) const;
730
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100731 /// Return true if the builder has the specified attribute.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100732 bool contains(Attribute::AttrKind A) const {
733 assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
734 return Attrs[A];
735 }
736
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100737 /// Return true if the builder has the specified target-dependent
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100738 /// attribute.
739 bool contains(StringRef A) const;
740
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100741 /// Return true if the builder has IR-level attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100742 bool hasAttributes() const;
743
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100744 /// Return true if the builder has any attribute that's in the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100745 /// specified attribute.
746 bool hasAttributes(AttributeList A, uint64_t Index) const;
747
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100748 /// Return true if the builder has an alignment attribute.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100749 bool hasAlignmentAttr() const;
750
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100751 /// Retrieve the alignment attribute, if it exists.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100752 uint64_t getAlignment() const { return Alignment; }
753
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100754 /// Retrieve the stack alignment attribute, if it exists.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100755 uint64_t getStackAlignment() const { return StackAlignment; }
756
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100757 /// Retrieve the number of dereferenceable bytes, if the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100758 /// dereferenceable attribute exists (zero is returned otherwise).
759 uint64_t getDereferenceableBytes() const { return DerefBytes; }
760
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100761 /// Retrieve the number of dereferenceable_or_null bytes, if the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100762 /// dereferenceable_or_null attribute exists (zero is returned otherwise).
763 uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; }
764
765 /// Retrieve the allocsize args, if the allocsize attribute exists. If it
766 /// doesn't exist, pair(0, 0) is returned.
767 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
768
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100769 /// This turns an int alignment (which must be a power of 2) into the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100770 /// form used internally in Attribute.
771 AttrBuilder &addAlignmentAttr(unsigned Align);
772
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100773 /// This turns an int stack alignment (which must be a power of 2) into
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100774 /// the form used internally in Attribute.
775 AttrBuilder &addStackAlignmentAttr(unsigned Align);
776
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100777 /// This turns the number of dereferenceable bytes into the form used
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100778 /// internally in Attribute.
779 AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
780
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100781 /// This turns the number of dereferenceable_or_null bytes into the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100782 /// form used internally in Attribute.
783 AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
784
785 /// This turns one (or two) ints into the form used internally in Attribute.
786 AttrBuilder &addAllocSizeAttr(unsigned ElemSizeArg,
787 const Optional<unsigned> &NumElemsArg);
788
789 /// Add an allocsize attribute, using the representation returned by
790 /// Attribute.getIntValue().
791 AttrBuilder &addAllocSizeAttrFromRawRepr(uint64_t RawAllocSizeRepr);
792
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100793 /// Return true if the builder contains no target-independent
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100794 /// attributes.
795 bool empty() const { return Attrs.none(); }
796
797 // Iterators for target-dependent attributes.
798 using td_type = std::pair<std::string, std::string>;
799 using td_iterator = std::map<std::string, std::string>::iterator;
800 using td_const_iterator = std::map<std::string, std::string>::const_iterator;
801 using td_range = iterator_range<td_iterator>;
802 using td_const_range = iterator_range<td_const_iterator>;
803
804 td_iterator td_begin() { return TargetDepAttrs.begin(); }
805 td_iterator td_end() { return TargetDepAttrs.end(); }
806
807 td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
808 td_const_iterator td_end() const { return TargetDepAttrs.end(); }
809
810 td_range td_attrs() { return td_range(td_begin(), td_end()); }
811
812 td_const_range td_attrs() const {
813 return td_const_range(td_begin(), td_end());
814 }
815
816 bool td_empty() const { return TargetDepAttrs.empty(); }
817
818 bool operator==(const AttrBuilder &B);
819 bool operator!=(const AttrBuilder &B) {
820 return !(*this == B);
821 }
822};
823
824namespace AttributeFuncs {
825
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100826/// Which attributes cannot be applied to a type.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100827AttrBuilder typeIncompatible(Type *Ty);
828
829/// \returns Return true if the two functions have compatible target-independent
830/// attributes for inlining purposes.
831bool areInlineCompatible(const Function &Caller, const Function &Callee);
832
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100833/// Merge caller's and callee's attributes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100834void mergeAttributesForInlining(Function &Caller, const Function &Callee);
835
836} // end namespace AttributeFuncs
837
838} // end namespace llvm
839
840#endif // LLVM_IR_ATTRIBUTES_H