1 //===- llvm/Attributes.h - Container for Attributes -------------*- C++ -*-===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// This file contains the simple types necessary to represent the
11 /// 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/BitmaskEnum.h"
21 #include "llvm/ADT/Optional.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/Config/llvm-config.h"
25 #include "llvm/Support/Alignment.h"
26 #include "llvm/Support/CodeGen.h"
27 #include "llvm/Support/PointerLikeTypeTraits.h"
28 #include <bitset>
29 #include <cassert>
30 #include <cstdint>
31 #include <set>
32 #include <string>
33 #include <utility>
34
35 namespace llvm {
36
37 class AttrBuilder;
38 class AttributeMask;
39 class AttributeImpl;
40 class AttributeListImpl;
41 class AttributeSetNode;
42 class FoldingSetNodeID;
43 class Function;
44 class LLVMContext;
45 class Type;
46
47 enum class AllocFnKind : uint64_t {
48 Unknown = 0,
49 Alloc = 1 << 0, // Allocator function returns a new allocation
50 Realloc = 1 << 1, // Allocator function resizes the `allocptr` argument
51 Free = 1 << 2, // Allocator function frees the `allocptr` argument
52 Uninitialized = 1 << 3, // Allocator function returns uninitialized memory
53 Zeroed = 1 << 4, // Allocator function returns zeroed memory
54 Aligned = 1 << 5, // Allocator function aligns allocations per the
55 // `allocalign` argument
56 LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ Aligned)
57 };
58
59 //===----------------------------------------------------------------------===//
60 /// \class
61 /// Functions, function parameters, and return types can have attributes
62 /// to indicate how they should be treated by optimizations and code
63 /// generation. This class represents one of those attributes. It's light-weight
64 /// and should be passed around by-value.
65 class Attribute {
66 public:
67 /// This enumeration lists the attributes that can be associated with
68 /// parameters, function results, or the function itself.
69 ///
70 /// Note: The `uwtable' attribute is about the ABI or the user mandating an
71 /// entry in the unwind table. The `nounwind' attribute is about an exception
72 /// passing by the function.
73 ///
74 /// In a theoretical system that uses tables for profiling and SjLj for
75 /// exceptions, they would be fully independent. In a normal system that uses
76 /// tables for both, the semantics are:
77 ///
78 /// nil = Needs an entry because an exception might pass by.
79 /// nounwind = No need for an entry
80 /// uwtable = Needs an entry because the ABI says so and because
81 /// an exception might pass by.
82 /// uwtable + nounwind = Needs an entry because the ABI says so.
83
84 enum AttrKind {
85 // IR-Level Attributes
86 None, ///< No attributes have been set
87 #define GET_ATTR_ENUM
88 #include "llvm/IR/Attributes.inc"
89 EndAttrKinds, ///< Sentinal value useful for loops
90 EmptyKey, ///< Use as Empty key for DenseMap of AttrKind
91 TombstoneKey, ///< Use as Tombstone key for DenseMap of AttrKind
92 };
93
94 static const unsigned NumIntAttrKinds = LastIntAttr - FirstIntAttr + 1;
95 static const unsigned NumTypeAttrKinds = LastTypeAttr - FirstTypeAttr + 1;
96
isEnumAttrKind(AttrKind Kind)97 static bool isEnumAttrKind(AttrKind Kind) {
98 return Kind >= FirstEnumAttr && Kind <= LastEnumAttr;
99 }
isIntAttrKind(AttrKind Kind)100 static bool isIntAttrKind(AttrKind Kind) {
101 return Kind >= FirstIntAttr && Kind <= LastIntAttr;
102 }
isTypeAttrKind(AttrKind Kind)103 static bool isTypeAttrKind(AttrKind Kind) {
104 return Kind >= FirstTypeAttr && Kind <= LastTypeAttr;
105 }
106
107 static bool canUseAsFnAttr(AttrKind Kind);
108 static bool canUseAsParamAttr(AttrKind Kind);
109 static bool canUseAsRetAttr(AttrKind Kind);
110
111 private:
112 AttributeImpl *pImpl = nullptr;
113
Attribute(AttributeImpl * A)114 Attribute(AttributeImpl *A) : pImpl(A) {}
115
116 public:
117 Attribute() = default;
118
119 //===--------------------------------------------------------------------===//
120 // Attribute Construction
121 //===--------------------------------------------------------------------===//
122
123 /// Return a uniquified Attribute object.
124 static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
125 static Attribute get(LLVMContext &Context, StringRef Kind,
126 StringRef Val = StringRef());
127 static Attribute get(LLVMContext &Context, AttrKind Kind, Type *Ty);
128
129 /// Return a uniquified Attribute object that has the specific
130 /// alignment set.
131 static Attribute getWithAlignment(LLVMContext &Context, Align Alignment);
132 static Attribute getWithStackAlignment(LLVMContext &Context, Align Alignment);
133 static Attribute getWithDereferenceableBytes(LLVMContext &Context,
134 uint64_t Bytes);
135 static Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context,
136 uint64_t Bytes);
137 static Attribute getWithAllocSizeArgs(LLVMContext &Context,
138 unsigned ElemSizeArg,
139 const Optional<unsigned> &NumElemsArg);
140 static Attribute getWithVScaleRangeArgs(LLVMContext &Context,
141 unsigned MinValue, unsigned MaxValue);
142 static Attribute getWithByValType(LLVMContext &Context, Type *Ty);
143 static Attribute getWithStructRetType(LLVMContext &Context, Type *Ty);
144 static Attribute getWithByRefType(LLVMContext &Context, Type *Ty);
145 static Attribute getWithPreallocatedType(LLVMContext &Context, Type *Ty);
146 static Attribute getWithInAllocaType(LLVMContext &Context, Type *Ty);
147 static Attribute getWithUWTableKind(LLVMContext &Context, UWTableKind Kind);
148
149 /// For a typed attribute, return the equivalent attribute with the type
150 /// changed to \p ReplacementTy.
getWithNewType(LLVMContext & Context,Type * ReplacementTy)151 Attribute getWithNewType(LLVMContext &Context, Type *ReplacementTy) {
152 assert(isTypeAttribute() && "this requires a typed attribute");
153 return get(Context, getKindAsEnum(), ReplacementTy);
154 }
155
156 static Attribute::AttrKind getAttrKindFromName(StringRef AttrName);
157
158 static StringRef getNameFromAttrKind(Attribute::AttrKind AttrKind);
159
160 /// Return true if the provided string matches the IR name of an attribute.
161 /// example: "noalias" return true but not "NoAlias"
162 static bool isExistingAttribute(StringRef Name);
163
164 //===--------------------------------------------------------------------===//
165 // Attribute Accessors
166 //===--------------------------------------------------------------------===//
167
168 /// Return true if the attribute is an Attribute::AttrKind type.
169 bool isEnumAttribute() const;
170
171 /// Return true if the attribute is an integer attribute.
172 bool isIntAttribute() const;
173
174 /// Return true if the attribute is a string (target-dependent)
175 /// attribute.
176 bool isStringAttribute() const;
177
178 /// Return true if the attribute is a type attribute.
179 bool isTypeAttribute() const;
180
181 /// Return true if the attribute is any kind of attribute.
isValid()182 bool isValid() const { return pImpl; }
183
184 /// Return true if the attribute is present.
185 bool hasAttribute(AttrKind Val) const;
186
187 /// Return true if the target-dependent attribute is present.
188 bool hasAttribute(StringRef Val) const;
189
190 /// Return the attribute's kind as an enum (Attribute::AttrKind). This
191 /// requires the attribute to be an enum, integer, or type attribute.
192 Attribute::AttrKind getKindAsEnum() const;
193
194 /// Return the attribute's value as an integer. This requires that the
195 /// attribute be an integer attribute.
196 uint64_t getValueAsInt() const;
197
198 /// Return the attribute's value as a boolean. This requires that the
199 /// attribute be a string attribute.
200 bool getValueAsBool() const;
201
202 /// Return the attribute's kind as a string. This requires the
203 /// attribute to be a string attribute.
204 StringRef getKindAsString() const;
205
206 /// Return the attribute's value as a string. This requires the
207 /// attribute to be a string attribute.
208 StringRef getValueAsString() const;
209
210 /// Return the attribute's value as a Type. This requires the attribute to be
211 /// a type attribute.
212 Type *getValueAsType() const;
213
214 /// Returns the alignment field of an attribute as a byte alignment
215 /// value.
216 MaybeAlign getAlignment() const;
217
218 /// Returns the stack alignment field of an attribute as a byte
219 /// alignment value.
220 MaybeAlign getStackAlignment() const;
221
222 /// Returns the number of dereferenceable bytes from the
223 /// dereferenceable attribute.
224 uint64_t getDereferenceableBytes() const;
225
226 /// Returns the number of dereferenceable_or_null bytes from the
227 /// dereferenceable_or_null attribute.
228 uint64_t getDereferenceableOrNullBytes() const;
229
230 /// Returns the argument numbers for the allocsize attribute (or pair(0, 0)
231 /// if not known).
232 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
233
234 /// Returns the minimum value for the vscale_range attribute.
235 unsigned getVScaleRangeMin() const;
236
237 /// Returns the maximum value for the vscale_range attribute or None when
238 /// unknown.
239 Optional<unsigned> getVScaleRangeMax() const;
240
241 // Returns the unwind table kind.
242 UWTableKind getUWTableKind() const;
243
244 // Returns the allocator function kind.
245 AllocFnKind getAllocKind() const;
246
247 /// The Attribute is converted to a string of equivalent mnemonic. This
248 /// is, presumably, for writing out the mnemonics for the assembly writer.
249 std::string getAsString(bool InAttrGrp = false) const;
250
251 /// Return true if this attribute belongs to the LLVMContext.
252 bool hasParentContext(LLVMContext &C) const;
253
254 /// Equality and non-equality operators.
255 bool operator==(Attribute A) const { return pImpl == A.pImpl; }
256 bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
257
258 /// Less-than operator. Useful for sorting the attributes list.
259 bool operator<(Attribute A) const;
260
261 void Profile(FoldingSetNodeID &ID) const;
262
263 /// Return a raw pointer that uniquely identifies this attribute.
getRawPointer()264 void *getRawPointer() const {
265 return pImpl;
266 }
267
268 /// Get an attribute from a raw pointer created by getRawPointer.
fromRawPointer(void * RawPtr)269 static Attribute fromRawPointer(void *RawPtr) {
270 return Attribute(reinterpret_cast<AttributeImpl*>(RawPtr));
271 }
272 };
273
274 // Specialized opaque value conversions.
wrap(Attribute Attr)275 inline LLVMAttributeRef wrap(Attribute Attr) {
276 return reinterpret_cast<LLVMAttributeRef>(Attr.getRawPointer());
277 }
278
279 // Specialized opaque value conversions.
unwrap(LLVMAttributeRef Attr)280 inline Attribute unwrap(LLVMAttributeRef Attr) {
281 return Attribute::fromRawPointer(Attr);
282 }
283
284 //===----------------------------------------------------------------------===//
285 /// \class
286 /// This class holds the attributes for a particular argument, parameter,
287 /// function, or return value. It is an immutable value type that is cheap to
288 /// copy. Adding and removing enum attributes is intended to be fast, but adding
289 /// and removing string or integer attributes involves a FoldingSet lookup.
290 class AttributeSet {
291 friend AttributeListImpl;
292 template <typename Ty, typename Enable> friend struct DenseMapInfo;
293
294 // TODO: Extract AvailableAttrs from AttributeSetNode and store them here.
295 // This will allow an efficient implementation of addAttribute and
296 // removeAttribute for enum attrs.
297
298 /// Private implementation pointer.
299 AttributeSetNode *SetNode = nullptr;
300
301 private:
AttributeSet(AttributeSetNode * ASN)302 explicit AttributeSet(AttributeSetNode *ASN) : SetNode(ASN) {}
303
304 public:
305 /// AttributeSet is a trivially copyable value type.
306 AttributeSet() = default;
307 AttributeSet(const AttributeSet &) = default;
308 ~AttributeSet() = default;
309
310 static AttributeSet get(LLVMContext &C, const AttrBuilder &B);
311 static AttributeSet get(LLVMContext &C, ArrayRef<Attribute> Attrs);
312
313 bool operator==(const AttributeSet &O) const { return SetNode == O.SetNode; }
314 bool operator!=(const AttributeSet &O) const { return !(*this == O); }
315
316 /// Add an argument attribute. Returns a new set because attribute sets are
317 /// immutable.
318 LLVM_NODISCARD AttributeSet addAttribute(LLVMContext &C,
319 Attribute::AttrKind Kind) const;
320
321 /// Add a target-dependent attribute. Returns a new set because attribute sets
322 /// are immutable.
323 LLVM_NODISCARD AttributeSet addAttribute(LLVMContext &C, StringRef Kind,
324 StringRef Value = StringRef()) const;
325
326 /// Add attributes to the attribute set. Returns a new set because attribute
327 /// sets are immutable.
328 LLVM_NODISCARD AttributeSet addAttributes(LLVMContext &C,
329 AttributeSet AS) const;
330
331 /// Remove the specified attribute from this set. Returns a new set because
332 /// attribute sets are immutable.
333 LLVM_NODISCARD AttributeSet removeAttribute(LLVMContext &C,
334 Attribute::AttrKind Kind) const;
335
336 /// Remove the specified attribute from this set. Returns a new set because
337 /// attribute sets are immutable.
338 LLVM_NODISCARD AttributeSet removeAttribute(LLVMContext &C,
339 StringRef Kind) const;
340
341 /// Remove the specified attributes from this set. Returns a new set because
342 /// attribute sets are immutable.
343 LLVM_NODISCARD AttributeSet
344 removeAttributes(LLVMContext &C, const AttributeMask &AttrsToRemove) const;
345
346 /// Return the number of attributes in this set.
347 unsigned getNumAttributes() const;
348
349 /// Return true if attributes exists in this set.
hasAttributes()350 bool hasAttributes() const { return SetNode != nullptr; }
351
352 /// Return true if the attribute exists in this set.
353 bool hasAttribute(Attribute::AttrKind Kind) const;
354
355 /// Return true if the attribute exists in this set.
356 bool hasAttribute(StringRef Kind) const;
357
358 /// Return the attribute object.
359 Attribute getAttribute(Attribute::AttrKind Kind) const;
360
361 /// Return the target-dependent attribute object.
362 Attribute getAttribute(StringRef Kind) const;
363
364 MaybeAlign getAlignment() const;
365 MaybeAlign getStackAlignment() const;
366 uint64_t getDereferenceableBytes() const;
367 uint64_t getDereferenceableOrNullBytes() const;
368 Type *getByValType() const;
369 Type *getStructRetType() const;
370 Type *getByRefType() const;
371 Type *getPreallocatedType() const;
372 Type *getInAllocaType() const;
373 Type *getElementType() const;
374 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
375 unsigned getVScaleRangeMin() const;
376 Optional<unsigned> getVScaleRangeMax() const;
377 UWTableKind getUWTableKind() const;
378 AllocFnKind getAllocKind() const;
379 std::string getAsString(bool InAttrGrp = false) const;
380
381 /// Return true if this attribute set belongs to the LLVMContext.
382 bool hasParentContext(LLVMContext &C) const;
383
384 using iterator = const Attribute *;
385
386 iterator begin() const;
387 iterator end() const;
388 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
389 void dump() const;
390 #endif
391 };
392
393 //===----------------------------------------------------------------------===//
394 /// \class
395 /// Provide DenseMapInfo for AttributeSet.
396 template <> struct DenseMapInfo<AttributeSet, void> {
397 static AttributeSet getEmptyKey() {
398 auto Val = static_cast<uintptr_t>(-1);
399 Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
400 return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
401 }
402
403 static AttributeSet getTombstoneKey() {
404 auto Val = static_cast<uintptr_t>(-2);
405 Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
406 return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
407 }
408
409 static unsigned getHashValue(AttributeSet AS) {
410 return (unsigned((uintptr_t)AS.SetNode) >> 4) ^
411 (unsigned((uintptr_t)AS.SetNode) >> 9);
412 }
413
414 static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
415 };
416
417 //===----------------------------------------------------------------------===//
418 /// \class
419 /// This class holds the attributes for a function, its return value, and
420 /// its parameters. You access the attributes for each of them via an index into
421 /// the AttributeList object. The function attributes are at index
422 /// `AttributeList::FunctionIndex', the return value is at index
423 /// `AttributeList::ReturnIndex', and the attributes for the parameters start at
424 /// index `AttributeList::FirstArgIndex'.
425 class AttributeList {
426 public:
427 enum AttrIndex : unsigned {
428 ReturnIndex = 0U,
429 FunctionIndex = ~0U,
430 FirstArgIndex = 1,
431 };
432
433 private:
434 friend class AttrBuilder;
435 friend class AttributeListImpl;
436 friend class AttributeSet;
437 friend class AttributeSetNode;
438 template <typename Ty, typename Enable> friend struct DenseMapInfo;
439
440 /// The attributes that we are managing. This can be null to represent
441 /// the empty attributes list.
442 AttributeListImpl *pImpl = nullptr;
443
444 public:
445 /// Create an AttributeList with the specified parameters in it.
446 static AttributeList get(LLVMContext &C,
447 ArrayRef<std::pair<unsigned, Attribute>> Attrs);
448 static AttributeList get(LLVMContext &C,
449 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs);
450
451 /// Create an AttributeList from attribute sets for a function, its
452 /// return value, and all of its arguments.
453 static AttributeList get(LLVMContext &C, AttributeSet FnAttrs,
454 AttributeSet RetAttrs,
455 ArrayRef<AttributeSet> ArgAttrs);
456
457 private:
458 explicit AttributeList(AttributeListImpl *LI) : pImpl(LI) {}
459
460 static AttributeList getImpl(LLVMContext &C, ArrayRef<AttributeSet> AttrSets);
461
462 AttributeList setAttributesAtIndex(LLVMContext &C, unsigned Index,
463 AttributeSet Attrs) const;
464
465 public:
466 AttributeList() = default;
467
468 //===--------------------------------------------------------------------===//
469 // AttributeList Construction and Mutation
470 //===--------------------------------------------------------------------===//
471
472 /// Return an AttributeList with the specified parameters in it.
473 static AttributeList get(LLVMContext &C, ArrayRef<AttributeList> Attrs);
474 static AttributeList get(LLVMContext &C, unsigned Index,
475 ArrayRef<Attribute::AttrKind> Kinds);
476 static AttributeList get(LLVMContext &C, unsigned Index,
477 ArrayRef<Attribute::AttrKind> Kinds,
478 ArrayRef<uint64_t> Values);
479 static AttributeList get(LLVMContext &C, unsigned Index,
480 ArrayRef<StringRef> Kind);
481 static AttributeList get(LLVMContext &C, unsigned Index,
482 AttributeSet Attrs);
483 static AttributeList get(LLVMContext &C, unsigned Index,
484 const AttrBuilder &B);
485
486 // TODO: remove non-AtIndex versions of these methods.
487 /// Add an attribute to the attribute set at the given index.
488 /// Returns a new list because attribute lists are immutable.
489 LLVM_NODISCARD AttributeList addAttributeAtIndex(
490 LLVMContext &C, unsigned Index, Attribute::AttrKind Kind) const;
491
492 /// Add an attribute to the attribute set at the given index.
493 /// Returns a new list because attribute lists are immutable.
494 LLVM_NODISCARD AttributeList
495 addAttributeAtIndex(LLVMContext &C, unsigned Index, StringRef Kind,
496 StringRef Value = StringRef()) const;
497
498 /// Add an attribute to the attribute set at the given index.
499 /// Returns a new list because attribute lists are immutable.
500 LLVM_NODISCARD AttributeList addAttributeAtIndex(LLVMContext &C,
501 unsigned Index,
502 Attribute A) const;
503
504 /// Add attributes to the attribute set at the given index.
505 /// Returns a new list because attribute lists are immutable.
506 LLVM_NODISCARD AttributeList addAttributesAtIndex(LLVMContext &C,
507 unsigned Index,
508 const AttrBuilder &B) const;
509
510 /// Add a function attribute to the list. Returns a new list because
511 /// attribute lists are immutable.
512 LLVM_NODISCARD AttributeList addFnAttribute(LLVMContext &C,
513 Attribute::AttrKind Kind) const {
514 return addAttributeAtIndex(C, FunctionIndex, Kind);
515 }
516
517 /// Add a function attribute to the list. Returns a new list because
518 /// attribute lists are immutable.
519 LLVM_NODISCARD AttributeList addFnAttribute(LLVMContext &C,
520 Attribute Attr) const {
521 return addAttributeAtIndex(C, FunctionIndex, Attr);
522 }
523
524 /// Add a function attribute to the list. Returns a new list because
525 /// attribute lists are immutable.
526 LLVM_NODISCARD AttributeList addFnAttribute(
527 LLVMContext &C, StringRef Kind, StringRef Value = StringRef()) const {
528 return addAttributeAtIndex(C, FunctionIndex, Kind, Value);
529 }
530
531 /// Add function attribute to the list. Returns a new list because
532 /// attribute lists are immutable.
533 LLVM_NODISCARD AttributeList addFnAttributes(LLVMContext &C,
534 const AttrBuilder &B) const {
535 return addAttributesAtIndex(C, FunctionIndex, B);
536 }
537
538 /// Add a return value attribute to the list. Returns a new list because
539 /// attribute lists are immutable.
540 LLVM_NODISCARD AttributeList addRetAttribute(LLVMContext &C,
541 Attribute::AttrKind Kind) const {
542 return addAttributeAtIndex(C, ReturnIndex, Kind);
543 }
544
545 /// Add a return value attribute to the list. Returns a new list because
546 /// attribute lists are immutable.
547 LLVM_NODISCARD AttributeList addRetAttribute(LLVMContext &C,
548 Attribute Attr) const {
549 return addAttributeAtIndex(C, ReturnIndex, Attr);
550 }
551
552 /// Add a return value attribute to the list. Returns a new list because
553 /// attribute lists are immutable.
554 LLVM_NODISCARD AttributeList addRetAttributes(LLVMContext &C,
555 const AttrBuilder &B) const {
556 return addAttributesAtIndex(C, ReturnIndex, B);
557 }
558
559 /// Add an argument attribute to the list. Returns a new list because
560 /// attribute lists are immutable.
561 LLVM_NODISCARD AttributeList addParamAttribute(
562 LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const {
563 return addAttributeAtIndex(C, ArgNo + FirstArgIndex, Kind);
564 }
565
566 /// Add an argument attribute to the list. Returns a new list because
567 /// attribute lists are immutable.
568 LLVM_NODISCARD AttributeList
569 addParamAttribute(LLVMContext &C, unsigned ArgNo, StringRef Kind,
570 StringRef Value = StringRef()) const {
571 return addAttributeAtIndex(C, ArgNo + FirstArgIndex, Kind, Value);
572 }
573
574 /// Add an attribute to the attribute list at the given arg indices. Returns a
575 /// new list because attribute lists are immutable.
576 LLVM_NODISCARD AttributeList addParamAttribute(LLVMContext &C,
577 ArrayRef<unsigned> ArgNos,
578 Attribute A) const;
579
580 /// Add an argument attribute to the list. Returns a new list because
581 /// attribute lists are immutable.
582 LLVM_NODISCARD AttributeList addParamAttributes(LLVMContext &C,
583 unsigned ArgNo,
584 const AttrBuilder &B) const {
585 return addAttributesAtIndex(C, ArgNo + FirstArgIndex, B);
586 }
587
588 /// Remove the specified attribute at the specified index from this
589 /// attribute list. Returns a new list because attribute lists are immutable.
590 LLVM_NODISCARD AttributeList removeAttributeAtIndex(
591 LLVMContext &C, unsigned Index, Attribute::AttrKind Kind) const;
592
593 /// Remove the specified attribute at the specified index from this
594 /// attribute list. Returns a new list because attribute lists are immutable.
595 LLVM_NODISCARD AttributeList removeAttributeAtIndex(LLVMContext &C,
596 unsigned Index,
597 StringRef Kind) const;
598 LLVM_NODISCARD AttributeList removeAttribute(LLVMContext &C, unsigned Index,
599 StringRef Kind) const {
600 return removeAttributeAtIndex(C, Index, Kind);
601 }
602
603 /// Remove the specified attributes at the specified index from this
604 /// attribute list. Returns a new list because attribute lists are immutable.
605 LLVM_NODISCARD AttributeList removeAttributesAtIndex(
606 LLVMContext &C, unsigned Index, const AttributeMask &AttrsToRemove) const;
607
608 /// Remove all attributes at the specified index from this
609 /// attribute list. Returns a new list because attribute lists are immutable.
610 LLVM_NODISCARD AttributeList removeAttributesAtIndex(LLVMContext &C,
611 unsigned Index) const;
612
613 /// Remove the specified attribute at the function index from this
614 /// attribute list. Returns a new list because attribute lists are immutable.
615 LLVM_NODISCARD AttributeList
616 removeFnAttribute(LLVMContext &C, Attribute::AttrKind Kind) const {
617 return removeAttributeAtIndex(C, FunctionIndex, Kind);
618 }
619
620 /// Remove the specified attribute at the function index from this
621 /// attribute list. Returns a new list because attribute lists are immutable.
622 LLVM_NODISCARD AttributeList removeFnAttribute(LLVMContext &C,
623 StringRef Kind) const {
624 return removeAttributeAtIndex(C, FunctionIndex, Kind);
625 }
626
627 /// Remove the specified attribute at the function index from this
628 /// attribute list. Returns a new list because attribute lists are immutable.
629 LLVM_NODISCARD AttributeList
630 removeFnAttributes(LLVMContext &C, const AttributeMask &AttrsToRemove) const {
631 return removeAttributesAtIndex(C, FunctionIndex, AttrsToRemove);
632 }
633
634 /// Remove the attributes at the function index from this
635 /// attribute list. Returns a new list because attribute lists are immutable.
636 LLVM_NODISCARD AttributeList removeFnAttributes(LLVMContext &C) const {
637 return removeAttributesAtIndex(C, FunctionIndex);
638 }
639
640 /// Remove the specified attribute at the return value index from this
641 /// attribute list. Returns a new list because attribute lists are immutable.
642 LLVM_NODISCARD AttributeList
643 removeRetAttribute(LLVMContext &C, Attribute::AttrKind Kind) const {
644 return removeAttributeAtIndex(C, ReturnIndex, Kind);
645 }
646
647 /// Remove the specified attribute at the return value index from this
648 /// attribute list. Returns a new list because attribute lists are immutable.
649 LLVM_NODISCARD AttributeList removeRetAttribute(LLVMContext &C,
650 StringRef Kind) const {
651 return removeAttributeAtIndex(C, ReturnIndex, Kind);
652 }
653
654 /// Remove the specified attribute at the return value index from this
655 /// attribute list. Returns a new list because attribute lists are immutable.
656 LLVM_NODISCARD AttributeList removeRetAttributes(
657 LLVMContext &C, const AttributeMask &AttrsToRemove) const {
658 return removeAttributesAtIndex(C, ReturnIndex, AttrsToRemove);
659 }
660
661 /// Remove the specified attribute at the specified arg index from this
662 /// attribute list. Returns a new list because attribute lists are immutable.
663 LLVM_NODISCARD AttributeList removeParamAttribute(
664 LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const {
665 return removeAttributeAtIndex(C, ArgNo + FirstArgIndex, Kind);
666 }
667
668 /// Remove the specified attribute at the specified arg index from this
669 /// attribute list. Returns a new list because attribute lists are immutable.
670 LLVM_NODISCARD AttributeList removeParamAttribute(LLVMContext &C,
671 unsigned ArgNo,
672 StringRef Kind) const {
673 return removeAttributeAtIndex(C, ArgNo + FirstArgIndex, Kind);
674 }
675
676 /// Remove the specified attribute at the specified arg index from this
677 /// attribute list. Returns a new list because attribute lists are immutable.
678 LLVM_NODISCARD AttributeList
679 removeParamAttributes(LLVMContext &C, unsigned ArgNo,
680 const AttributeMask &AttrsToRemove) const {
681 return removeAttributesAtIndex(C, ArgNo + FirstArgIndex, AttrsToRemove);
682 }
683
684 /// Remove all attributes at the specified arg index from this
685 /// attribute list. Returns a new list because attribute lists are immutable.
686 LLVM_NODISCARD AttributeList removeParamAttributes(LLVMContext &C,
687 unsigned ArgNo) const {
688 return removeAttributesAtIndex(C, ArgNo + FirstArgIndex);
689 }
690
691 /// Replace the type contained by attribute \p AttrKind at index \p ArgNo wih
692 /// \p ReplacementTy, preserving all other attributes.
693 LLVM_NODISCARD AttributeList replaceAttributeTypeAtIndex(
694 LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind,
695 Type *ReplacementTy) const {
696 Attribute Attr = getAttributeAtIndex(ArgNo, Kind);
697 auto Attrs = removeAttributeAtIndex(C, ArgNo, Kind);
698 return Attrs.addAttributeAtIndex(C, ArgNo,
699 Attr.getWithNewType(C, ReplacementTy));
700 }
701
702 /// \brief Add the dereferenceable attribute to the attribute set at the given
703 /// index. Returns a new list because attribute lists are immutable.
704 LLVM_NODISCARD AttributeList addDereferenceableRetAttr(LLVMContext &C,
705 uint64_t Bytes) const;
706
707 /// \brief Add the dereferenceable attribute to the attribute set at the given
708 /// arg index. Returns a new list because attribute lists are immutable.
709 LLVM_NODISCARD AttributeList addDereferenceableParamAttr(
710 LLVMContext &C, unsigned ArgNo, uint64_t Bytes) const;
711
712 /// Add the dereferenceable_or_null attribute to the attribute set at
713 /// the given arg index. Returns a new list because attribute lists are
714 /// immutable.
715 LLVM_NODISCARD AttributeList addDereferenceableOrNullParamAttr(
716 LLVMContext &C, unsigned ArgNo, uint64_t Bytes) const;
717
718 /// Add the allocsize attribute to the attribute set at the given arg index.
719 /// Returns a new list because attribute lists are immutable.
720 LLVM_NODISCARD AttributeList
721 addAllocSizeParamAttr(LLVMContext &C, unsigned ArgNo, unsigned ElemSizeArg,
722 const Optional<unsigned> &NumElemsArg);
723
724 //===--------------------------------------------------------------------===//
725 // AttributeList Accessors
726 //===--------------------------------------------------------------------===//
727
728 /// The attributes for the specified index are returned.
729 AttributeSet getAttributes(unsigned Index) const;
730
731 /// The attributes for the argument or parameter at the given index are
732 /// returned.
733 AttributeSet getParamAttrs(unsigned ArgNo) const;
734
735 /// The attributes for the ret value are returned.
736 AttributeSet getRetAttrs() const;
737
738 /// The function attributes are returned.
739 AttributeSet getFnAttrs() const;
740
741 /// Return true if the attribute exists at the given index.
742 bool hasAttributeAtIndex(unsigned Index, Attribute::AttrKind Kind) const;
743
744 /// Return true if the attribute exists at the given index.
745 bool hasAttributeAtIndex(unsigned Index, StringRef Kind) const;
746
747 /// Return true if attribute exists at the given index.
748 bool hasAttributesAtIndex(unsigned Index) const;
749
750 /// Return true if the attribute exists for the given argument
751 bool hasParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
752 return hasAttributeAtIndex(ArgNo + FirstArgIndex, Kind);
753 }
754
755 /// Return true if the attribute exists for the given argument
756 bool hasParamAttr(unsigned ArgNo, StringRef Kind) const {
757 return hasAttributeAtIndex(ArgNo + FirstArgIndex, Kind);
758 }
759
760 /// Return true if attributes exists for the given argument
761 bool hasParamAttrs(unsigned ArgNo) const {
762 return hasAttributesAtIndex(ArgNo + FirstArgIndex);
763 }
764
765 /// Return true if the attribute exists for the return value.
766 bool hasRetAttr(Attribute::AttrKind Kind) const {
767 return hasAttributeAtIndex(ReturnIndex, Kind);
768 }
769
770 /// Return true if the attribute exists for the return value.
771 bool hasRetAttr(StringRef Kind) const {
772 return hasAttributeAtIndex(ReturnIndex, Kind);
773 }
774
775 /// Return true if attributes exist for the return value.
776 bool hasRetAttrs() const { return hasAttributesAtIndex(ReturnIndex); }
777
778 /// Return true if the attribute exists for the function.
779 bool hasFnAttr(Attribute::AttrKind Kind) const;
780
781 /// Return true if the attribute exists for the function.
782 bool hasFnAttr(StringRef Kind) const;
783
784 /// Return true the attributes exist for the function.
785 bool hasFnAttrs() const { return hasAttributesAtIndex(FunctionIndex); }
786
787 /// Return true if the specified attribute is set for at least one
788 /// parameter or for the return value. If Index is not nullptr, the index
789 /// of a parameter with the specified attribute is provided.
790 bool hasAttrSomewhere(Attribute::AttrKind Kind,
791 unsigned *Index = nullptr) const;
792
793 /// Return the attribute object that exists at the given index.
794 Attribute getAttributeAtIndex(unsigned Index, Attribute::AttrKind Kind) const;
795
796 /// Return the attribute object that exists at the given index.
797 Attribute getAttributeAtIndex(unsigned Index, StringRef Kind) const;
798
799 /// Return the attribute object that exists at the arg index.
800 Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
801 return getAttributeAtIndex(ArgNo + FirstArgIndex, Kind);
802 }
803
804 /// Return the attribute object that exists at the given index.
805 Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const {
806 return getAttributeAtIndex(ArgNo + FirstArgIndex, Kind);
807 }
808
809 /// Return the attribute object that exists for the function.
810 Attribute getFnAttr(Attribute::AttrKind Kind) const {
811 return getAttributeAtIndex(FunctionIndex, Kind);
812 }
813
814 /// Return the attribute object that exists for the function.
815 Attribute getFnAttr(StringRef Kind) const {
816 return getAttributeAtIndex(FunctionIndex, Kind);
817 }
818
819 /// Return the alignment of the return value.
820 MaybeAlign getRetAlignment() const;
821
822 /// Return the alignment for the specified function parameter.
823 MaybeAlign getParamAlignment(unsigned ArgNo) const;
824
825 /// Return the stack alignment for the specified function parameter.
826 MaybeAlign getParamStackAlignment(unsigned ArgNo) const;
827
828 /// Return the byval type for the specified function parameter.
829 Type *getParamByValType(unsigned ArgNo) const;
830
831 /// Return the sret type for the specified function parameter.
832 Type *getParamStructRetType(unsigned ArgNo) const;
833
834 /// Return the byref type for the specified function parameter.
835 Type *getParamByRefType(unsigned ArgNo) const;
836
837 /// Return the preallocated type for the specified function parameter.
838 Type *getParamPreallocatedType(unsigned ArgNo) const;
839
840 /// Return the inalloca type for the specified function parameter.
841 Type *getParamInAllocaType(unsigned ArgNo) const;
842
843 /// Return the elementtype type for the specified function parameter.
844 Type *getParamElementType(unsigned ArgNo) const;
845
846 /// Get the stack alignment of the function.
847 MaybeAlign getFnStackAlignment() const;
848
849 /// Get the stack alignment of the return value.
850 MaybeAlign getRetStackAlignment() const;
851
852 /// Get the number of dereferenceable bytes (or zero if unknown) of the return
853 /// value.
854 uint64_t getRetDereferenceableBytes() const;
855
856 /// Get the number of dereferenceable bytes (or zero if unknown) of an arg.
857 uint64_t getParamDereferenceableBytes(unsigned Index) const;
858
859 /// Get the number of dereferenceable_or_null bytes (or zero if unknown) of
860 /// the return value.
861 uint64_t getRetDereferenceableOrNullBytes() const;
862
863 /// Get the number of dereferenceable_or_null bytes (or zero if unknown) of an
864 /// arg.
865 uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo) const;
866
867 /// Get the unwind table kind requested for the function.
868 UWTableKind getUWTableKind() const;
869
870 AllocFnKind getAllocKind() const;
871
872 /// Return the attributes at the index as a string.
873 std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
874
875 /// Return true if this attribute list belongs to the LLVMContext.
876 bool hasParentContext(LLVMContext &C) const;
877
878 //===--------------------------------------------------------------------===//
879 // AttributeList Introspection
880 //===--------------------------------------------------------------------===//
881
882 using iterator = const AttributeSet *;
883
884 iterator begin() const;
885 iterator end() const;
886
887 unsigned getNumAttrSets() const;
888
889 // Implementation of indexes(). Produces iterators that wrap an index. Mostly
890 // to hide the awkwardness of unsigned wrapping when iterating over valid
891 // indexes.
892 struct index_iterator {
893 unsigned NumAttrSets;
894 index_iterator(int NumAttrSets) : NumAttrSets(NumAttrSets) {}
895 struct int_wrapper {
896 int_wrapper(unsigned i) : i(i) {}
897 unsigned i;
898 unsigned operator*() { return i; }
899 bool operator!=(const int_wrapper &Other) { return i != Other.i; }
900 int_wrapper &operator++() {
901 // This is expected to undergo unsigned wrapping since FunctionIndex is
902 // ~0 and that's where we start.
903 ++i;
904 return *this;
905 }
906 };
907
908 int_wrapper begin() { return int_wrapper(AttributeList::FunctionIndex); }
909
910 int_wrapper end() { return int_wrapper(NumAttrSets - 1); }
911 };
912
913 /// Use this to iterate over the valid attribute indexes.
914 index_iterator indexes() const { return index_iterator(getNumAttrSets()); }
915
916 /// operator==/!= - Provide equality predicates.
917 bool operator==(const AttributeList &RHS) const { return pImpl == RHS.pImpl; }
918 bool operator!=(const AttributeList &RHS) const { return pImpl != RHS.pImpl; }
919
920 /// Return a raw pointer that uniquely identifies this attribute list.
921 void *getRawPointer() const {
922 return pImpl;
923 }
924
925 /// Return true if there are no attributes.
926 bool isEmpty() const { return pImpl == nullptr; }
927
928 void print(raw_ostream &O) const;
929
930 void dump() const;
931 };
932
933 //===----------------------------------------------------------------------===//
934 /// \class
935 /// Provide DenseMapInfo for AttributeList.
936 template <> struct DenseMapInfo<AttributeList, void> {
937 static AttributeList getEmptyKey() {
938 auto Val = static_cast<uintptr_t>(-1);
939 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
940 return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
941 }
942
943 static AttributeList getTombstoneKey() {
944 auto Val = static_cast<uintptr_t>(-2);
945 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
946 return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
947 }
948
949 static unsigned getHashValue(AttributeList AS) {
950 return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
951 (unsigned((uintptr_t)AS.pImpl) >> 9);
952 }
953
954 static bool isEqual(AttributeList LHS, AttributeList RHS) {
955 return LHS == RHS;
956 }
957 };
958
959 //===----------------------------------------------------------------------===//
960 /// \class
961 /// This class stores enough information to efficiently remove some attributes
962 /// from an existing AttrBuilder, AttributeSet or AttributeList.
963 class AttributeMask {
964 std::bitset<Attribute::EndAttrKinds> Attrs;
965 std::set<SmallString<32>, std::less<>> TargetDepAttrs;
966
967 public:
968 AttributeMask() = default;
969 AttributeMask(const AttributeMask &) = delete;
970 AttributeMask(AttributeMask &&) = default;
971
972 AttributeMask(AttributeSet AS) {
973 for (Attribute A : AS)
974 addAttribute(A);
975 }
976
977 /// Add an attribute to the mask.
978 AttributeMask &addAttribute(Attribute::AttrKind Val) {
979 assert((unsigned)Val < Attribute::EndAttrKinds &&
980 "Attribute out of range!");
981 Attrs[Val] = true;
982 return *this;
983 }
984
985 /// Add the Attribute object to the builder.
986 AttributeMask &addAttribute(Attribute A) {
987 if (A.isStringAttribute())
988 addAttribute(A.getKindAsString());
989 else
990 addAttribute(A.getKindAsEnum());
991 return *this;
992 }
993
994 /// Add the target-dependent attribute to the builder.
995 AttributeMask &addAttribute(StringRef A) {
996 TargetDepAttrs.insert(A);
997 return *this;
998 }
999
1000 /// Return true if the builder has the specified attribute.
1001 bool contains(Attribute::AttrKind A) const {
1002 assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
1003 return Attrs[A];
1004 }
1005
1006 /// Return true if the builder has the specified target-dependent
1007 /// attribute.
1008 bool contains(StringRef A) const { return TargetDepAttrs.count(A); }
1009
1010 /// Return true if the mask contains the specified attribute.
1011 bool contains(Attribute A) const {
1012 if (A.isStringAttribute())
1013 return contains(A.getKindAsString());
1014 return contains(A.getKindAsEnum());
1015 }
1016 };
1017
1018 //===----------------------------------------------------------------------===//
1019 /// \class
1020 /// This class is used in conjunction with the Attribute::get method to
1021 /// create an Attribute object. The object itself is uniquified. The Builder's
1022 /// value, however, is not. So this can be used as a quick way to test for
1023 /// equality, presence of attributes, etc.
1024 class AttrBuilder {
1025 LLVMContext &Ctx;
1026 SmallVector<Attribute, 8> Attrs;
1027
1028 public:
1029 AttrBuilder(LLVMContext &Ctx) : Ctx(Ctx) {}
1030 AttrBuilder(const AttrBuilder &) = delete;
1031 AttrBuilder(AttrBuilder &&) = default;
1032
1033 AttrBuilder(LLVMContext &Ctx, const Attribute &A) : Ctx(Ctx) {
1034 addAttribute(A);
1035 }
1036
1037 AttrBuilder(LLVMContext &Ctx, AttributeSet AS);
1038
1039 void clear();
1040
1041 /// Add an attribute to the builder.
1042 AttrBuilder &addAttribute(Attribute::AttrKind Val);
1043
1044 /// Add the Attribute object to the builder.
1045 AttrBuilder &addAttribute(Attribute A);
1046
1047 /// Add the target-dependent attribute to the builder.
1048 AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
1049
1050 /// Remove an attribute from the builder.
1051 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
1052
1053 /// Remove the target-dependent attribute from the builder.
1054 AttrBuilder &removeAttribute(StringRef A);
1055
1056 /// Remove the target-dependent attribute from the builder.
1057 AttrBuilder &removeAttribute(Attribute A) {
1058 if (A.isStringAttribute())
1059 return removeAttribute(A.getKindAsString());
1060 else
1061 return removeAttribute(A.getKindAsEnum());
1062 }
1063
1064 /// Add the attributes from the builder. Attributes in the passed builder
1065 /// overwrite attributes in this builder if they have the same key.
1066 AttrBuilder &merge(const AttrBuilder &B);
1067
1068 /// Remove the attributes from the builder.
1069 AttrBuilder &remove(const AttributeMask &AM);
1070
1071 /// Return true if the builder has any attribute that's in the
1072 /// specified builder.
1073 bool overlaps(const AttributeMask &AM) const;
1074
1075 /// Return true if the builder has the specified attribute.
1076 bool contains(Attribute::AttrKind A) const;
1077
1078 /// Return true if the builder has the specified target-dependent
1079 /// attribute.
1080 bool contains(StringRef A) const;
1081
1082 /// Return true if the builder has IR-level attributes.
1083 bool hasAttributes() const { return !Attrs.empty(); }
1084
1085 /// Return true if the builder has an alignment attribute.
1086 bool hasAlignmentAttr() const;
1087
1088 /// Return Attribute with the given Kind. The returned attribute will be
1089 /// invalid if the Kind is not present in the builder.
1090 Attribute getAttribute(Attribute::AttrKind Kind) const;
1091
1092 /// Return Attribute with the given Kind. The returned attribute will be
1093 /// invalid if the Kind is not present in the builder.
1094 Attribute getAttribute(StringRef Kind) const;
1095
1096 /// Return raw (possibly packed/encoded) value of integer attribute or 0 if
1097 /// not set.
1098 uint64_t getRawIntAttr(Attribute::AttrKind Kind) const;
1099
1100 /// Retrieve the alignment attribute, if it exists.
1101 MaybeAlign getAlignment() const {
1102 return MaybeAlign(getRawIntAttr(Attribute::Alignment));
1103 }
1104
1105 /// Retrieve the stack alignment attribute, if it exists.
1106 MaybeAlign getStackAlignment() const {
1107 return MaybeAlign(getRawIntAttr(Attribute::StackAlignment));
1108 }
1109
1110 /// Retrieve the number of dereferenceable bytes, if the
1111 /// dereferenceable attribute exists (zero is returned otherwise).
1112 uint64_t getDereferenceableBytes() const {
1113 return getRawIntAttr(Attribute::Dereferenceable);
1114 }
1115
1116 /// Retrieve the number of dereferenceable_or_null bytes, if the
1117 /// dereferenceable_or_null attribute exists (zero is returned otherwise).
1118 uint64_t getDereferenceableOrNullBytes() const {
1119 return getRawIntAttr(Attribute::DereferenceableOrNull);
1120 }
1121
1122 /// Retrieve type for the given type attribute.
1123 Type *getTypeAttr(Attribute::AttrKind Kind) const;
1124
1125 /// Retrieve the byval type.
1126 Type *getByValType() const { return getTypeAttr(Attribute::ByVal); }
1127
1128 /// Retrieve the sret type.
1129 Type *getStructRetType() const { return getTypeAttr(Attribute::StructRet); }
1130
1131 /// Retrieve the byref type.
1132 Type *getByRefType() const { return getTypeAttr(Attribute::ByRef); }
1133
1134 /// Retrieve the preallocated type.
1135 Type *getPreallocatedType() const {
1136 return getTypeAttr(Attribute::Preallocated);
1137 }
1138
1139 /// Retrieve the inalloca type.
1140 Type *getInAllocaType() const { return getTypeAttr(Attribute::InAlloca); }
1141
1142 /// Retrieve the allocsize args, if the allocsize attribute exists. If it
1143 /// doesn't exist, pair(0, 0) is returned.
1144 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
1145
1146 /// Retrieve the minimum value of 'vscale_range'.
1147 unsigned getVScaleRangeMin() const;
1148
1149 /// Retrieve the maximum value of 'vscale_range' or None when unknown.
1150 Optional<unsigned> getVScaleRangeMax() const;
1151
1152 /// Add integer attribute with raw value (packed/encoded if necessary).
1153 AttrBuilder &addRawIntAttr(Attribute::AttrKind Kind, uint64_t Value);
1154
1155 /// This turns an alignment into the form used internally in Attribute.
1156 /// This call has no effect if Align is not set.
1157 AttrBuilder &addAlignmentAttr(MaybeAlign Align);
1158
1159 /// This turns an int alignment (which must be a power of 2) into the
1160 /// form used internally in Attribute.
1161 /// This call has no effect if Align is 0.
1162 /// Deprecated, use the version using a MaybeAlign.
1163 inline AttrBuilder &addAlignmentAttr(unsigned Align) {
1164 return addAlignmentAttr(MaybeAlign(Align));
1165 }
1166
1167 /// This turns a stack alignment into the form used internally in Attribute.
1168 /// This call has no effect if Align is not set.
1169 AttrBuilder &addStackAlignmentAttr(MaybeAlign Align);
1170
1171 /// This turns an int stack alignment (which must be a power of 2) into
1172 /// the form used internally in Attribute.
1173 /// This call has no effect if Align is 0.
1174 /// Deprecated, use the version using a MaybeAlign.
1175 inline AttrBuilder &addStackAlignmentAttr(unsigned Align) {
1176 return addStackAlignmentAttr(MaybeAlign(Align));
1177 }
1178
1179 /// This turns the number of dereferenceable bytes into the form used
1180 /// internally in Attribute.
1181 AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
1182
1183 /// This turns the number of dereferenceable_or_null bytes into the
1184 /// form used internally in Attribute.
1185 AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
1186
1187 /// This turns one (or two) ints into the form used internally in Attribute.
1188 AttrBuilder &addAllocSizeAttr(unsigned ElemSizeArg,
1189 const Optional<unsigned> &NumElemsArg);
1190
1191 /// This turns two ints into the form used internally in Attribute.
1192 AttrBuilder &addVScaleRangeAttr(unsigned MinValue,
1193 Optional<unsigned> MaxValue);
1194
1195 /// Add a type attribute with the given type.
1196 AttrBuilder &addTypeAttr(Attribute::AttrKind Kind, Type *Ty);
1197
1198 /// This turns a byval type into the form used internally in Attribute.
1199 AttrBuilder &addByValAttr(Type *Ty);
1200
1201 /// This turns a sret type into the form used internally in Attribute.
1202 AttrBuilder &addStructRetAttr(Type *Ty);
1203
1204 /// This turns a byref type into the form used internally in Attribute.
1205 AttrBuilder &addByRefAttr(Type *Ty);
1206
1207 /// This turns a preallocated type into the form used internally in Attribute.
1208 AttrBuilder &addPreallocatedAttr(Type *Ty);
1209
1210 /// This turns an inalloca type into the form used internally in Attribute.
1211 AttrBuilder &addInAllocaAttr(Type *Ty);
1212
1213 /// Add an allocsize attribute, using the representation returned by
1214 /// Attribute.getIntValue().
1215 AttrBuilder &addAllocSizeAttrFromRawRepr(uint64_t RawAllocSizeRepr);
1216
1217 /// Add a vscale_range attribute, using the representation returned by
1218 /// Attribute.getIntValue().
1219 AttrBuilder &addVScaleRangeAttrFromRawRepr(uint64_t RawVScaleRangeRepr);
1220
1221 /// This turns the unwind table kind into the form used internally in
1222 /// Attribute.
1223 AttrBuilder &addUWTableAttr(UWTableKind Kind);
1224
1225 // This turns the allocator kind into the form used internally in Attribute.
1226 AttrBuilder &addAllocKindAttr(AllocFnKind Kind);
1227
1228 ArrayRef<Attribute> attrs() const { return Attrs; }
1229
1230 bool operator==(const AttrBuilder &B) const;
1231 bool operator!=(const AttrBuilder &B) const { return !(*this == B); }
1232 };
1233
1234 namespace AttributeFuncs {
1235
1236 enum AttributeSafetyKind : uint8_t {
1237 ASK_SAFE_TO_DROP = 1,
1238 ASK_UNSAFE_TO_DROP = 2,
1239 ASK_ALL = ASK_SAFE_TO_DROP | ASK_UNSAFE_TO_DROP,
1240 };
1241
1242 /// Which attributes cannot be applied to a type. The argument \p ASK indicates,
1243 /// if only attributes that are known to be safely droppable are contained in
1244 /// the mask; only attributes that might be unsafe to drop (e.g., ABI-related
1245 /// attributes) are in the mask; or both.
1246 AttributeMask typeIncompatible(Type *Ty, AttributeSafetyKind ASK = ASK_ALL);
1247
1248 /// Get param/return attributes which imply immediate undefined behavior if an
1249 /// invalid value is passed. For example, this includes noundef (where undef
1250 /// implies UB), but not nonnull (where null implies poison). It also does not
1251 /// include attributes like nocapture, which constrain the function
1252 /// implementation rather than the passed value.
1253 AttributeMask getUBImplyingAttributes();
1254
1255 /// \returns Return true if the two functions have compatible target-independent
1256 /// attributes for inlining purposes.
1257 bool areInlineCompatible(const Function &Caller, const Function &Callee);
1258
1259
1260 /// Checks if there are any incompatible function attributes between
1261 /// \p A and \p B.
1262 ///
1263 /// \param [in] A - The first function to be compared with.
1264 /// \param [in] B - The second function to be compared with.
1265 /// \returns true if the functions have compatible attributes.
1266 bool areOutlineCompatible(const Function &A, const Function &B);
1267
1268 /// Merge caller's and callee's attributes.
1269 void mergeAttributesForInlining(Function &Caller, const Function &Callee);
1270
1271 /// Merges the functions attributes from \p ToMerge into function \p Base.
1272 ///
1273 /// \param [in,out] Base - The function being merged into.
1274 /// \param [in] ToMerge - The function to merge attributes from.
1275 void mergeAttributesForOutlining(Function &Base, const Function &ToMerge);
1276
1277 /// Update min-legal-vector-width if it is in Attribute and less than Width.
1278 void updateMinLegalVectorWidthAttr(Function &Fn, uint64_t Width);
1279
1280 } // end namespace AttributeFuncs
1281
1282 } // end namespace llvm
1283
1284 #endif // LLVM_IR_ATTRIBUTES_H
1285