1 //===-- llvm/Attributes.h - Container for Attributes ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file contains the simple types necessary to represent the
12 /// attributes associated with functions and their calls.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_IR_ATTRIBUTES_H
17 #define LLVM_IR_ATTRIBUTES_H
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/Optional.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/PointerLikeTypeTraits.h"
24 #include "llvm-c/Types.h"
34 class AttributeSetImpl;
35 class AttributeSetNode;
37 template<typename T> struct DenseMapInfo;
42 //===----------------------------------------------------------------------===//
44 /// \brief Functions, function parameters, and return types can have attributes
45 /// to indicate how they should be treated by optimizations and code
46 /// generation. This class represents one of those attributes. It's light-weight
47 /// and should be passed around by-value.
50 /// This enumeration lists the attributes that can be associated with
51 /// parameters, function results, or the function itself.
53 /// Note: The `uwtable' attribute is about the ABI or the user mandating an
54 /// entry in the unwind table. The `nounwind' attribute is about an exception
55 /// passing by the function.
57 /// In a theoretical system that uses tables for profiling and SjLj for
58 /// exceptions, they would be fully independent. In a normal system that uses
59 /// tables for both, the semantics are:
61 /// nil = Needs an entry because an exception might pass by.
62 /// nounwind = No need for an entry
63 /// uwtable = Needs an entry because the ABI says so and because
64 /// an exception might pass by.
65 /// uwtable + nounwind = Needs an entry because the ABI says so.
68 // IR-Level Attributes
69 None, ///< No attributes have been set
71 #include "llvm/IR/Attributes.inc"
72 EndAttrKinds ///< Sentinal value useful for loops
77 Attribute(AttributeImpl *A) : pImpl(A) {}
80 Attribute() : pImpl(nullptr) {}
82 //===--------------------------------------------------------------------===//
83 // Attribute Construction
84 //===--------------------------------------------------------------------===//
86 /// \brief Return a uniquified Attribute object.
87 static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
88 static Attribute get(LLVMContext &Context, StringRef Kind,
89 StringRef Val = StringRef());
91 /// \brief Return a uniquified Attribute object that has the specific
93 static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
94 static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
95 static Attribute getWithDereferenceableBytes(LLVMContext &Context,
97 static Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context,
99 static Attribute getWithAllocSizeArgs(LLVMContext &Context,
100 unsigned ElemSizeArg,
101 const Optional<unsigned> &NumElemsArg);
103 //===--------------------------------------------------------------------===//
104 // Attribute Accessors
105 //===--------------------------------------------------------------------===//
107 /// \brief Return true if the attribute is an Attribute::AttrKind type.
108 bool isEnumAttribute() const;
110 /// \brief Return true if the attribute is an integer attribute.
111 bool isIntAttribute() const;
113 /// \brief Return true if the attribute is a string (target-dependent)
115 bool isStringAttribute() const;
117 /// \brief Return true if the attribute is present.
118 bool hasAttribute(AttrKind Val) const;
120 /// \brief Return true if the target-dependent attribute is present.
121 bool hasAttribute(StringRef Val) const;
123 /// \brief Return the attribute's kind as an enum (Attribute::AttrKind). This
124 /// requires the attribute to be an enum or integer attribute.
125 Attribute::AttrKind getKindAsEnum() const;
127 /// \brief Return the attribute's value as an integer. This requires that the
128 /// attribute be an integer attribute.
129 uint64_t getValueAsInt() const;
131 /// \brief Return the attribute's kind as a string. This requires the
132 /// attribute to be a string attribute.
133 StringRef getKindAsString() const;
135 /// \brief Return the attribute's value as a string. This requires the
136 /// attribute to be a string attribute.
137 StringRef getValueAsString() const;
139 /// \brief Returns the alignment field of an attribute as a byte alignment
141 unsigned getAlignment() const;
143 /// \brief Returns the stack alignment field of an attribute as a byte
145 unsigned getStackAlignment() const;
147 /// \brief Returns the number of dereferenceable bytes from the
148 /// dereferenceable attribute.
149 uint64_t getDereferenceableBytes() const;
151 /// \brief Returns the number of dereferenceable_or_null bytes from the
152 /// dereferenceable_or_null attribute.
153 uint64_t getDereferenceableOrNullBytes() const;
155 /// Returns the argument numbers for the allocsize attribute (or pair(0, 0)
157 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
159 /// \brief The Attribute is converted to a string of equivalent mnemonic. This
160 /// is, presumably, for writing out the mnemonics for the assembly writer.
161 std::string getAsString(bool InAttrGrp = false) const;
163 /// \brief Equality and non-equality operators.
164 bool operator==(Attribute A) const { return pImpl == A.pImpl; }
165 bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
167 /// \brief Less-than operator. Useful for sorting the attributes list.
168 bool operator<(Attribute A) const;
170 void Profile(FoldingSetNodeID &ID) const {
171 ID.AddPointer(pImpl);
174 /// \brief Return a raw pointer that uniquely identifies this attribute.
175 void *getRawPointer() const {
179 /// \brief Get an attribute from a raw pointer created by getRawPointer.
180 static Attribute fromRawPointer(void *RawPtr) {
181 return Attribute(reinterpret_cast<AttributeImpl*>(RawPtr));
185 // Specialized opaque value conversions.
186 inline LLVMAttributeRef wrap(Attribute Attr) {
187 return reinterpret_cast<LLVMAttributeRef>(Attr.getRawPointer());
190 // Specialized opaque value conversions.
191 inline Attribute unwrap(LLVMAttributeRef Attr) {
192 return Attribute::fromRawPointer(Attr);
195 //===----------------------------------------------------------------------===//
197 /// \brief This class holds the attributes for a function, its return value, and
198 /// its parameters. You access the attributes for each of them via an index into
199 /// the AttributeSet object. The function attributes are at index
200 /// `AttributeSet::FunctionIndex', the return value is at index
201 /// `AttributeSet::ReturnIndex', and the attributes for the parameters start at
205 enum AttrIndex : unsigned {
211 friend class AttrBuilder;
212 friend class AttributeSetImpl;
213 template <typename Ty> friend struct DenseMapInfo;
215 /// \brief The attributes that we are managing. This can be null to represent
216 /// the empty attributes list.
217 AttributeSetImpl *pImpl;
219 /// \brief The attributes for the specified index are returned.
220 AttributeSetNode *getAttributes(unsigned Index) const;
222 /// \brief Create an AttributeSet with the specified parameters in it.
223 static AttributeSet get(LLVMContext &C,
224 ArrayRef<std::pair<unsigned, Attribute> > Attrs);
225 static AttributeSet get(LLVMContext &C,
226 ArrayRef<std::pair<unsigned,
227 AttributeSetNode*> > Attrs);
229 static AttributeSet getImpl(LLVMContext &C,
230 ArrayRef<std::pair<unsigned,
231 AttributeSetNode*> > Attrs);
233 explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {}
236 AttributeSet() : pImpl(nullptr) {}
238 //===--------------------------------------------------------------------===//
239 // AttributeSet Construction and Mutation
240 //===--------------------------------------------------------------------===//
242 /// \brief Return an AttributeSet with the specified parameters in it.
243 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs);
244 static AttributeSet get(LLVMContext &C, unsigned Index,
245 ArrayRef<Attribute::AttrKind> Kinds);
246 static AttributeSet get(LLVMContext &C, unsigned Index,
247 ArrayRef<StringRef> Kind);
248 static AttributeSet get(LLVMContext &C, unsigned Index, const AttrBuilder &B);
250 /// \brief Add an attribute to the attribute set at the given index. Because
251 /// attribute sets are immutable, this returns a new set.
252 AttributeSet addAttribute(LLVMContext &C, unsigned Index,
253 Attribute::AttrKind Kind) const;
255 /// \brief Add an attribute to the attribute set at the given index. Because
256 /// attribute sets are immutable, this returns a new set.
257 AttributeSet addAttribute(LLVMContext &C, unsigned Index, StringRef Kind,
258 StringRef Value = StringRef()) const;
260 /// Add an attribute to the attribute set at the given indices. Because
261 /// attribute sets are immutable, this returns a new set.
262 AttributeSet addAttribute(LLVMContext &C, ArrayRef<unsigned> Indices,
265 /// \brief Add attributes to the attribute set at the given index. Because
266 /// attribute sets are immutable, this returns a new set.
267 AttributeSet addAttributes(LLVMContext &C, unsigned Index,
268 AttributeSet Attrs) const;
270 /// \brief Remove the specified attribute at the specified index from this
271 /// attribute list. Because attribute lists are immutable, this returns the
273 AttributeSet removeAttribute(LLVMContext &C, unsigned Index,
274 Attribute::AttrKind Kind) const;
276 /// \brief Remove the specified attribute at the specified index from this
277 /// attribute list. Because attribute lists are immutable, this returns the
279 AttributeSet removeAttribute(LLVMContext &C, unsigned Index,
280 StringRef Kind) const;
282 /// \brief Remove the specified attributes at the specified index from this
283 /// attribute list. Because attribute lists are immutable, this returns the
285 AttributeSet removeAttributes(LLVMContext &C, unsigned Index,
286 AttributeSet Attrs) const;
288 /// \brief Remove the specified attributes at the specified index from this
289 /// attribute list. Because attribute lists are immutable, this returns the
291 AttributeSet removeAttributes(LLVMContext &C, unsigned Index,
292 const AttrBuilder &Attrs) const;
294 /// \brief Add the dereferenceable attribute to the attribute set at the given
295 /// index. Because attribute sets are immutable, this returns a new set.
296 AttributeSet addDereferenceableAttr(LLVMContext &C, unsigned Index,
297 uint64_t Bytes) const;
299 /// \brief Add the dereferenceable_or_null attribute to the attribute set at
300 /// the given index. Because attribute sets are immutable, this returns a new
302 AttributeSet addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
303 uint64_t Bytes) const;
305 /// Add the allocsize attribute to the attribute set at the given index.
306 /// Because attribute sets are immutable, this returns a new set.
307 AttributeSet addAllocSizeAttr(LLVMContext &C, unsigned Index,
308 unsigned ElemSizeArg,
309 const Optional<unsigned> &NumElemsArg);
311 //===--------------------------------------------------------------------===//
312 // AttributeSet Accessors
313 //===--------------------------------------------------------------------===//
315 /// \brief Retrieve the LLVM context.
316 LLVMContext &getContext() const;
318 /// \brief The attributes for the specified index are returned.
319 AttributeSet getParamAttributes(unsigned Index) const;
321 /// \brief The attributes for the ret value are returned.
322 AttributeSet getRetAttributes() const;
324 /// \brief The function attributes are returned.
325 AttributeSet getFnAttributes() const;
327 /// \brief Return true if the attribute exists at the given index.
328 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
330 /// \brief Return true if the attribute exists at the given index.
331 bool hasAttribute(unsigned Index, StringRef Kind) const;
333 /// \brief Return true if attribute exists at the given index.
334 bool hasAttributes(unsigned Index) const;
336 /// \brief Equivalent to hasAttribute(AttributeSet::FunctionIndex, Kind) but
338 bool hasFnAttribute(Attribute::AttrKind Kind) const;
340 /// \brief Return true if the specified attribute is set for at least one
341 /// parameter or for the return value. If Index is not nullptr, the index
342 /// of a parameter with the specified attribute is provided.
343 bool hasAttrSomewhere(Attribute::AttrKind Kind,
344 unsigned *Index = nullptr) const;
346 /// \brief Return the attribute object that exists at the given index.
347 Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
349 /// \brief Return the attribute object that exists at the given index.
350 Attribute getAttribute(unsigned Index, StringRef Kind) const;
352 /// \brief Return the alignment for the specified function parameter.
353 unsigned getParamAlignment(unsigned Index) const;
355 /// \brief Get the stack alignment.
356 unsigned getStackAlignment(unsigned Index) const;
358 /// \brief Get the number of dereferenceable bytes (or zero if unknown).
359 uint64_t getDereferenceableBytes(unsigned Index) const;
361 /// \brief Get the number of dereferenceable_or_null bytes (or zero if
363 uint64_t getDereferenceableOrNullBytes(unsigned Index) const;
365 /// Get the allocsize argument numbers (or pair(0, 0) if unknown).
366 std::pair<unsigned, Optional<unsigned>>
367 getAllocSizeArgs(unsigned Index) const;
369 /// \brief Return the attributes at the index as a string.
370 std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
372 typedef ArrayRef<Attribute>::iterator iterator;
374 iterator begin(unsigned Slot) const;
375 iterator end(unsigned Slot) const;
377 /// operator==/!= - Provide equality predicates.
378 bool operator==(const AttributeSet &RHS) const {
379 return pImpl == RHS.pImpl;
381 bool operator!=(const AttributeSet &RHS) const {
382 return pImpl != RHS.pImpl;
385 //===--------------------------------------------------------------------===//
386 // AttributeSet Introspection
387 //===--------------------------------------------------------------------===//
389 // FIXME: Remove this.
390 uint64_t Raw(unsigned Index) const;
392 /// \brief Return a raw pointer that uniquely identifies this attribute list.
393 void *getRawPointer() const {
397 /// \brief Return true if there are no attributes.
398 bool isEmpty() const {
399 return getNumSlots() == 0;
402 /// \brief Return the number of slots used in this attribute list. This is
403 /// the number of arguments that have an attribute set on them (including the
404 /// function itself).
405 unsigned getNumSlots() const;
407 /// \brief Return the index for the given slot.
408 unsigned getSlotIndex(unsigned Slot) const;
410 /// \brief Return the attributes at the given slot.
411 AttributeSet getSlotAttributes(unsigned Slot) const;
416 //===----------------------------------------------------------------------===//
418 /// \brief Provide DenseMapInfo for AttributeSet.
419 template<> struct DenseMapInfo<AttributeSet> {
420 static inline AttributeSet getEmptyKey() {
421 uintptr_t Val = static_cast<uintptr_t>(-1);
422 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
423 return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
425 static inline AttributeSet getTombstoneKey() {
426 uintptr_t Val = static_cast<uintptr_t>(-2);
427 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
428 return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
430 static unsigned getHashValue(AttributeSet AS) {
431 return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
432 (unsigned((uintptr_t)AS.pImpl) >> 9);
434 static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
437 //===----------------------------------------------------------------------===//
439 /// \brief This class is used in conjunction with the Attribute::get method to
440 /// create an Attribute object. The object itself is uniquified. The Builder's
441 /// value, however, is not. So this can be used as a quick way to test for
442 /// equality, presence of attributes, etc.
444 std::bitset<Attribute::EndAttrKinds> Attrs;
445 std::map<std::string, std::string> TargetDepAttrs;
447 uint64_t StackAlignment;
449 uint64_t DerefOrNullBytes;
450 uint64_t AllocSizeArgs;
454 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
455 DerefOrNullBytes(0), AllocSizeArgs(0) {}
456 explicit AttrBuilder(uint64_t Val)
457 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
458 DerefOrNullBytes(0), AllocSizeArgs(0) {
461 AttrBuilder(const Attribute &A)
462 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
463 DerefOrNullBytes(0), AllocSizeArgs(0) {
466 AttrBuilder(AttributeSet AS, unsigned Idx);
470 /// \brief Add an attribute to the builder.
471 AttrBuilder &addAttribute(Attribute::AttrKind Val);
473 /// \brief Add the Attribute object to the builder.
474 AttrBuilder &addAttribute(Attribute A);
476 /// \brief Add the target-dependent attribute to the builder.
477 AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
479 /// \brief Remove an attribute from the builder.
480 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
482 /// \brief Remove the attributes from the builder.
483 AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index);
485 /// \brief Remove the target-dependent attribute to the builder.
486 AttrBuilder &removeAttribute(StringRef A);
488 /// \brief Add the attributes from the builder.
489 AttrBuilder &merge(const AttrBuilder &B);
491 /// \brief Remove the attributes from the builder.
492 AttrBuilder &remove(const AttrBuilder &B);
494 /// \brief Return true if the builder has any attribute that's in the
495 /// specified builder.
496 bool overlaps(const AttrBuilder &B) const;
498 /// \brief Return true if the builder has the specified attribute.
499 bool contains(Attribute::AttrKind A) const {
500 assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
504 /// \brief Return true if the builder has the specified target-dependent
506 bool contains(StringRef A) const;
508 /// \brief Return true if the builder has IR-level attributes.
509 bool hasAttributes() const;
511 /// \brief Return true if the builder has any attribute that's in the
512 /// specified attribute.
513 bool hasAttributes(AttributeSet A, uint64_t Index) const;
515 /// \brief Return true if the builder has an alignment attribute.
516 bool hasAlignmentAttr() const;
518 /// \brief Retrieve the alignment attribute, if it exists.
519 uint64_t getAlignment() const { return Alignment; }
521 /// \brief Retrieve the stack alignment attribute, if it exists.
522 uint64_t getStackAlignment() const { return StackAlignment; }
524 /// \brief Retrieve the number of dereferenceable bytes, if the
525 /// dereferenceable attribute exists (zero is returned otherwise).
526 uint64_t getDereferenceableBytes() const { return DerefBytes; }
528 /// \brief Retrieve the number of dereferenceable_or_null bytes, if the
529 /// dereferenceable_or_null attribute exists (zero is returned otherwise).
530 uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; }
532 /// Retrieve the allocsize args, if the allocsize attribute exists. If it
533 /// doesn't exist, pair(0, 0) is returned.
534 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
536 /// \brief This turns an int alignment (which must be a power of 2) into the
537 /// form used internally in Attribute.
538 AttrBuilder &addAlignmentAttr(unsigned Align);
540 /// \brief This turns an int stack alignment (which must be a power of 2) into
541 /// the form used internally in Attribute.
542 AttrBuilder &addStackAlignmentAttr(unsigned Align);
544 /// \brief This turns the number of dereferenceable bytes into the form used
545 /// internally in Attribute.
546 AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
548 /// \brief This turns the number of dereferenceable_or_null bytes into the
549 /// form used internally in Attribute.
550 AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
552 /// This turns one (or two) ints into the form used internally in Attribute.
553 AttrBuilder &addAllocSizeAttr(unsigned ElemSizeArg,
554 const Optional<unsigned> &NumElemsArg);
556 /// Add an allocsize attribute, using the representation returned by
557 /// Attribute.getIntValue().
558 AttrBuilder &addAllocSizeAttrFromRawRepr(uint64_t RawAllocSizeRepr);
560 /// \brief Return true if the builder contains no target-independent
562 bool empty() const { return Attrs.none(); }
564 // Iterators for target-dependent attributes.
565 typedef std::pair<std::string, std::string> td_type;
566 typedef std::map<std::string, std::string>::iterator td_iterator;
567 typedef std::map<std::string, std::string>::const_iterator td_const_iterator;
568 typedef llvm::iterator_range<td_iterator> td_range;
569 typedef llvm::iterator_range<td_const_iterator> td_const_range;
571 td_iterator td_begin() { return TargetDepAttrs.begin(); }
572 td_iterator td_end() { return TargetDepAttrs.end(); }
574 td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
575 td_const_iterator td_end() const { return TargetDepAttrs.end(); }
577 td_range td_attrs() { return td_range(td_begin(), td_end()); }
578 td_const_range td_attrs() const {
579 return td_const_range(td_begin(), td_end());
582 bool td_empty() const { return TargetDepAttrs.empty(); }
584 bool operator==(const AttrBuilder &B);
585 bool operator!=(const AttrBuilder &B) {
586 return !(*this == B);
589 // FIXME: Remove this in 4.0.
591 /// \brief Add the raw value to the internal representation.
592 AttrBuilder &addRawValue(uint64_t Val);
595 namespace AttributeFuncs {
597 /// \brief Which attributes cannot be applied to a type.
598 AttrBuilder typeIncompatible(Type *Ty);
600 /// \returns Return true if the two functions have compatible target-independent
601 /// attributes for inlining purposes.
602 bool areInlineCompatible(const Function &Caller, const Function &Callee);
604 /// \brief Merge caller's and callee's attributes.
605 void mergeAttributesForInlining(Function &Caller, const Function &Callee);
607 } // end AttributeFuncs namespace
609 } // end llvm namespace