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 friend class AttributeSetNode;
214 template <typename Ty> friend struct DenseMapInfo;
216 /// \brief The attributes that we are managing. This can be null to represent
217 /// the empty attributes list.
218 AttributeSetImpl *pImpl;
220 /// \brief The attributes for the specified index are returned.
221 AttributeSetNode *getAttributes(unsigned Index) const;
223 /// \brief Create an AttributeSet with the specified parameters in it.
224 static AttributeSet get(LLVMContext &C,
225 ArrayRef<std::pair<unsigned, Attribute> > Attrs);
226 static AttributeSet get(LLVMContext &C,
227 ArrayRef<std::pair<unsigned,
228 AttributeSetNode*> > Attrs);
230 static AttributeSet getImpl(LLVMContext &C,
231 ArrayRef<std::pair<unsigned,
232 AttributeSetNode*> > Attrs);
234 explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {}
237 AttributeSet() : pImpl(nullptr) {}
239 //===--------------------------------------------------------------------===//
240 // AttributeSet Construction and Mutation
241 //===--------------------------------------------------------------------===//
243 /// \brief Return an AttributeSet with the specified parameters in it.
244 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs);
245 static AttributeSet get(LLVMContext &C, unsigned Index,
246 ArrayRef<Attribute::AttrKind> Kinds);
247 static AttributeSet get(LLVMContext &C, unsigned Index,
248 ArrayRef<StringRef> Kind);
249 static AttributeSet get(LLVMContext &C, unsigned Index, const AttrBuilder &B);
251 /// \brief Add an attribute to the attribute set at the given index. Because
252 /// attribute sets are immutable, this returns a new set.
253 AttributeSet addAttribute(LLVMContext &C, unsigned Index,
254 Attribute::AttrKind Kind) const;
256 /// \brief Add an attribute to the attribute set at the given index. Because
257 /// attribute sets are immutable, this returns a new set.
258 AttributeSet addAttribute(LLVMContext &C, unsigned Index, StringRef Kind,
259 StringRef Value = StringRef()) const;
261 /// Add an attribute to the attribute set at the given indices. Because
262 /// attribute sets are immutable, this returns a new set.
263 AttributeSet addAttribute(LLVMContext &C, ArrayRef<unsigned> Indices,
266 /// \brief Add attributes to the attribute set at the given index. Because
267 /// attribute sets are immutable, this returns a new set.
268 AttributeSet addAttributes(LLVMContext &C, unsigned Index,
269 AttributeSet Attrs) const;
271 /// \brief Remove the specified attribute at the specified index from this
272 /// attribute list. Because attribute lists are immutable, this returns the
274 AttributeSet removeAttribute(LLVMContext &C, unsigned Index,
275 Attribute::AttrKind Kind) const;
277 /// \brief Remove the specified attribute at the specified index from this
278 /// attribute list. Because attribute lists are immutable, this returns the
280 AttributeSet removeAttribute(LLVMContext &C, unsigned Index,
281 StringRef Kind) const;
283 /// \brief Remove the specified attributes at the specified index from this
284 /// attribute list. Because attribute lists are immutable, this returns the
286 AttributeSet removeAttributes(LLVMContext &C, unsigned Index,
287 AttributeSet Attrs) const;
289 /// \brief Remove the specified attributes at the specified index from this
290 /// attribute list. Because attribute lists are immutable, this returns the
292 AttributeSet removeAttributes(LLVMContext &C, unsigned Index,
293 const AttrBuilder &Attrs) const;
295 /// \brief Add the dereferenceable attribute to the attribute set at the given
296 /// index. Because attribute sets are immutable, this returns a new set.
297 AttributeSet addDereferenceableAttr(LLVMContext &C, unsigned Index,
298 uint64_t Bytes) const;
300 /// \brief Add the dereferenceable_or_null attribute to the attribute set at
301 /// the given index. Because attribute sets are immutable, this returns a new
303 AttributeSet addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
304 uint64_t Bytes) const;
306 /// Add the allocsize attribute to the attribute set at the given index.
307 /// Because attribute sets are immutable, this returns a new set.
308 AttributeSet addAllocSizeAttr(LLVMContext &C, unsigned Index,
309 unsigned ElemSizeArg,
310 const Optional<unsigned> &NumElemsArg);
312 //===--------------------------------------------------------------------===//
313 // AttributeSet Accessors
314 //===--------------------------------------------------------------------===//
316 /// \brief Retrieve the LLVM context.
317 LLVMContext &getContext() const;
319 /// \brief The attributes for the specified index are returned.
320 AttributeSet getParamAttributes(unsigned Index) const;
322 /// \brief The attributes for the ret value are returned.
323 AttributeSet getRetAttributes() const;
325 /// \brief The function attributes are returned.
326 AttributeSet getFnAttributes() const;
328 /// \brief Return true if the attribute exists at the given index.
329 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
331 /// \brief Return true if the attribute exists at the given index.
332 bool hasAttribute(unsigned Index, StringRef Kind) const;
334 /// \brief Return true if attribute exists at the given index.
335 bool hasAttributes(unsigned Index) const;
337 /// \brief Equivalent to hasAttribute(AttributeSet::FunctionIndex, Kind) but
339 bool hasFnAttribute(Attribute::AttrKind Kind) const;
341 /// \brief Return true if the specified attribute is set for at least one
342 /// parameter or for the return value. If Index is not nullptr, the index
343 /// of a parameter with the specified attribute is provided.
344 bool hasAttrSomewhere(Attribute::AttrKind Kind,
345 unsigned *Index = nullptr) const;
347 /// \brief Return the attribute object that exists at the given index.
348 Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
350 /// \brief Return the attribute object that exists at the given index.
351 Attribute getAttribute(unsigned Index, StringRef Kind) const;
353 /// \brief Return the alignment for the specified function parameter.
354 unsigned getParamAlignment(unsigned Index) const;
356 /// \brief Get the stack alignment.
357 unsigned getStackAlignment(unsigned Index) const;
359 /// \brief Get the number of dereferenceable bytes (or zero if unknown).
360 uint64_t getDereferenceableBytes(unsigned Index) const;
362 /// \brief Get the number of dereferenceable_or_null bytes (or zero if
364 uint64_t getDereferenceableOrNullBytes(unsigned Index) const;
366 /// Get the allocsize argument numbers (or pair(0, 0) if unknown).
367 std::pair<unsigned, Optional<unsigned>>
368 getAllocSizeArgs(unsigned Index) const;
370 /// \brief Return the attributes at the index as a string.
371 std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
373 typedef ArrayRef<Attribute>::iterator iterator;
375 iterator begin(unsigned Slot) const;
376 iterator end(unsigned Slot) const;
378 /// operator==/!= - Provide equality predicates.
379 bool operator==(const AttributeSet &RHS) const {
380 return pImpl == RHS.pImpl;
382 bool operator!=(const AttributeSet &RHS) const {
383 return pImpl != RHS.pImpl;
386 //===--------------------------------------------------------------------===//
387 // AttributeSet Introspection
388 //===--------------------------------------------------------------------===//
390 // FIXME: Remove this.
391 uint64_t Raw(unsigned Index) const;
393 /// \brief Return a raw pointer that uniquely identifies this attribute list.
394 void *getRawPointer() const {
398 /// \brief Return true if there are no attributes.
399 bool isEmpty() const {
400 return getNumSlots() == 0;
403 /// \brief Return the number of slots used in this attribute list. This is
404 /// the number of arguments that have an attribute set on them (including the
405 /// function itself).
406 unsigned getNumSlots() const;
408 /// \brief Return the index for the given slot.
409 unsigned getSlotIndex(unsigned Slot) const;
411 /// \brief Return the attributes at the given slot.
412 AttributeSet getSlotAttributes(unsigned Slot) const;
417 //===----------------------------------------------------------------------===//
419 /// \brief Provide DenseMapInfo for AttributeSet.
420 template<> struct DenseMapInfo<AttributeSet> {
421 static inline AttributeSet getEmptyKey() {
422 uintptr_t Val = static_cast<uintptr_t>(-1);
423 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
424 return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
426 static inline AttributeSet getTombstoneKey() {
427 uintptr_t Val = static_cast<uintptr_t>(-2);
428 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
429 return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
431 static unsigned getHashValue(AttributeSet AS) {
432 return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
433 (unsigned((uintptr_t)AS.pImpl) >> 9);
435 static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
438 //===----------------------------------------------------------------------===//
440 /// \brief This class is used in conjunction with the Attribute::get method to
441 /// create an Attribute object. The object itself is uniquified. The Builder's
442 /// value, however, is not. So this can be used as a quick way to test for
443 /// equality, presence of attributes, etc.
445 std::bitset<Attribute::EndAttrKinds> Attrs;
446 std::map<std::string, std::string> TargetDepAttrs;
448 uint64_t StackAlignment;
450 uint64_t DerefOrNullBytes;
451 uint64_t AllocSizeArgs;
455 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
456 DerefOrNullBytes(0), AllocSizeArgs(0) {}
457 explicit AttrBuilder(uint64_t Val)
458 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
459 DerefOrNullBytes(0), AllocSizeArgs(0) {
462 AttrBuilder(const Attribute &A)
463 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
464 DerefOrNullBytes(0), AllocSizeArgs(0) {
467 AttrBuilder(AttributeSet AS, unsigned Idx);
471 /// \brief Add an attribute to the builder.
472 AttrBuilder &addAttribute(Attribute::AttrKind Val);
474 /// \brief Add the Attribute object to the builder.
475 AttrBuilder &addAttribute(Attribute A);
477 /// \brief Add the target-dependent attribute to the builder.
478 AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
480 /// \brief Remove an attribute from the builder.
481 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
483 /// \brief Remove the attributes from the builder.
484 AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index);
486 /// \brief Remove the target-dependent attribute to the builder.
487 AttrBuilder &removeAttribute(StringRef A);
489 /// \brief Add the attributes from the builder.
490 AttrBuilder &merge(const AttrBuilder &B);
492 /// \brief Remove the attributes from the builder.
493 AttrBuilder &remove(const AttrBuilder &B);
495 /// \brief Return true if the builder has any attribute that's in the
496 /// specified builder.
497 bool overlaps(const AttrBuilder &B) const;
499 /// \brief Return true if the builder has the specified attribute.
500 bool contains(Attribute::AttrKind A) const {
501 assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
505 /// \brief Return true if the builder has the specified target-dependent
507 bool contains(StringRef A) const;
509 /// \brief Return true if the builder has IR-level attributes.
510 bool hasAttributes() const;
512 /// \brief Return true if the builder has any attribute that's in the
513 /// specified attribute.
514 bool hasAttributes(AttributeSet A, uint64_t Index) const;
516 /// \brief Return true if the builder has an alignment attribute.
517 bool hasAlignmentAttr() const;
519 /// \brief Retrieve the alignment attribute, if it exists.
520 uint64_t getAlignment() const { return Alignment; }
522 /// \brief Retrieve the stack alignment attribute, if it exists.
523 uint64_t getStackAlignment() const { return StackAlignment; }
525 /// \brief Retrieve the number of dereferenceable bytes, if the
526 /// dereferenceable attribute exists (zero is returned otherwise).
527 uint64_t getDereferenceableBytes() const { return DerefBytes; }
529 /// \brief Retrieve the number of dereferenceable_or_null bytes, if the
530 /// dereferenceable_or_null attribute exists (zero is returned otherwise).
531 uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; }
533 /// Retrieve the allocsize args, if the allocsize attribute exists. If it
534 /// doesn't exist, pair(0, 0) is returned.
535 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
537 /// \brief This turns an int alignment (which must be a power of 2) into the
538 /// form used internally in Attribute.
539 AttrBuilder &addAlignmentAttr(unsigned Align);
541 /// \brief This turns an int stack alignment (which must be a power of 2) into
542 /// the form used internally in Attribute.
543 AttrBuilder &addStackAlignmentAttr(unsigned Align);
545 /// \brief This turns the number of dereferenceable bytes into the form used
546 /// internally in Attribute.
547 AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
549 /// \brief This turns the number of dereferenceable_or_null bytes into the
550 /// form used internally in Attribute.
551 AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
553 /// This turns one (or two) ints into the form used internally in Attribute.
554 AttrBuilder &addAllocSizeAttr(unsigned ElemSizeArg,
555 const Optional<unsigned> &NumElemsArg);
557 /// Add an allocsize attribute, using the representation returned by
558 /// Attribute.getIntValue().
559 AttrBuilder &addAllocSizeAttrFromRawRepr(uint64_t RawAllocSizeRepr);
561 /// \brief Return true if the builder contains no target-independent
563 bool empty() const { return Attrs.none(); }
565 // Iterators for target-dependent attributes.
566 typedef std::pair<std::string, std::string> td_type;
567 typedef std::map<std::string, std::string>::iterator td_iterator;
568 typedef std::map<std::string, std::string>::const_iterator td_const_iterator;
569 typedef llvm::iterator_range<td_iterator> td_range;
570 typedef llvm::iterator_range<td_const_iterator> td_const_range;
572 td_iterator td_begin() { return TargetDepAttrs.begin(); }
573 td_iterator td_end() { return TargetDepAttrs.end(); }
575 td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
576 td_const_iterator td_end() const { return TargetDepAttrs.end(); }
578 td_range td_attrs() { return td_range(td_begin(), td_end()); }
579 td_const_range td_attrs() const {
580 return td_const_range(td_begin(), td_end());
583 bool td_empty() const { return TargetDepAttrs.empty(); }
585 bool operator==(const AttrBuilder &B);
586 bool operator!=(const AttrBuilder &B) {
587 return !(*this == B);
590 // FIXME: Remove this in 4.0.
592 /// \brief Add the raw value to the internal representation.
593 AttrBuilder &addRawValue(uint64_t Val);
596 namespace AttributeFuncs {
598 /// \brief Which attributes cannot be applied to a type.
599 AttrBuilder typeIncompatible(Type *Ty);
601 /// \returns Return true if the two functions have compatible target-independent
602 /// attributes for inlining purposes.
603 bool areInlineCompatible(const Function &Caller, const Function &Callee);
605 /// \brief Merge caller's and callee's attributes.
606 void mergeAttributesForInlining(Function &Caller, const Function &Callee);
608 } // end AttributeFuncs namespace
610 } // end llvm namespace