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/Support/Compiler.h"
22 #include "llvm/Support/PointerLikeTypeTraits.h"
32 class AttributeSetImpl;
33 class AttributeSetNode;
35 template<typename T> struct DenseMapInfo;
39 //===----------------------------------------------------------------------===//
41 /// \brief Functions, function parameters, and return types can have attributes
42 /// to indicate how they should be treated by optimizations and code
43 /// generation. This class represents one of those attributes. It's light-weight
44 /// and should be passed around by-value.
47 /// This enumeration lists the attributes that can be associated with
48 /// parameters, function results, or the function itself.
50 /// Note: The `uwtable' attribute is about the ABI or the user mandating an
51 /// entry in the unwind table. The `nounwind' attribute is about an exception
52 /// passing by the function.
54 /// In a theoretical system that uses tables for profiling and SjLj for
55 /// exceptions, they would be fully independent. In a normal system that uses
56 /// tables for both, the semantics are:
58 /// nil = Needs an entry because an exception might pass by.
59 /// nounwind = No need for an entry
60 /// uwtable = Needs an entry because the ABI says so and because
61 /// an exception might pass by.
62 /// uwtable + nounwind = Needs an entry because the ABI says so.
65 // IR-Level Attributes
66 None, ///< No attributes have been set
68 #include "llvm/IR/Attributes.inc"
69 EndAttrKinds ///< Sentinal value useful for loops
74 Attribute(AttributeImpl *A) : pImpl(A) {}
77 Attribute() : pImpl(nullptr) {}
79 //===--------------------------------------------------------------------===//
80 // Attribute Construction
81 //===--------------------------------------------------------------------===//
83 /// \brief Return a uniquified Attribute object.
84 static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
85 static Attribute get(LLVMContext &Context, StringRef Kind,
86 StringRef Val = StringRef());
88 /// \brief Return a uniquified Attribute object that has the specific
90 static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
91 static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
92 static Attribute getWithDereferenceableBytes(LLVMContext &Context,
94 static Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context,
97 //===--------------------------------------------------------------------===//
98 // Attribute Accessors
99 //===--------------------------------------------------------------------===//
101 /// \brief Return true if the attribute is an Attribute::AttrKind type.
102 bool isEnumAttribute() const;
104 /// \brief Return true if the attribute is an integer attribute.
105 bool isIntAttribute() const;
107 /// \brief Return true if the attribute is a string (target-dependent)
109 bool isStringAttribute() const;
111 /// \brief Return true if the attribute is present.
112 bool hasAttribute(AttrKind Val) const;
114 /// \brief Return true if the target-dependent attribute is present.
115 bool hasAttribute(StringRef Val) const;
117 /// \brief Return the attribute's kind as an enum (Attribute::AttrKind). This
118 /// requires the attribute to be an enum or alignment attribute.
119 Attribute::AttrKind getKindAsEnum() const;
121 /// \brief Return the attribute's value as an integer. This requires that the
122 /// attribute be an alignment attribute.
123 uint64_t getValueAsInt() const;
125 /// \brief Return the attribute's kind as a string. This requires the
126 /// attribute to be a string attribute.
127 StringRef getKindAsString() const;
129 /// \brief Return the attribute's value as a string. This requires the
130 /// attribute to be a string attribute.
131 StringRef getValueAsString() const;
133 /// \brief Returns the alignment field of an attribute as a byte alignment
135 unsigned getAlignment() const;
137 /// \brief Returns the stack alignment field of an attribute as a byte
139 unsigned getStackAlignment() const;
141 /// \brief Returns the number of dereferenceable bytes from the
142 /// dereferenceable attribute (or zero if unknown).
143 uint64_t getDereferenceableBytes() const;
145 /// \brief Returns the number of dereferenceable_or_null bytes from the
146 /// dereferenceable_or_null attribute (or zero if unknown).
147 uint64_t getDereferenceableOrNullBytes() const;
149 /// \brief The Attribute is converted to a string of equivalent mnemonic. This
150 /// is, presumably, for writing out the mnemonics for the assembly writer.
151 std::string getAsString(bool InAttrGrp = false) const;
153 /// \brief Equality and non-equality operators.
154 bool operator==(Attribute A) const { return pImpl == A.pImpl; }
155 bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
157 /// \brief Less-than operator. Useful for sorting the attributes list.
158 bool operator<(Attribute A) const;
160 void Profile(FoldingSetNodeID &ID) const {
161 ID.AddPointer(pImpl);
165 //===----------------------------------------------------------------------===//
167 /// \brief This class holds the attributes for a function, its return value, and
168 /// its parameters. You access the attributes for each of them via an index into
169 /// the AttributeSet object. The function attributes are at index
170 /// `AttributeSet::FunctionIndex', the return value is at index
171 /// `AttributeSet::ReturnIndex', and the attributes for the parameters start at
175 enum AttrIndex : unsigned {
181 friend class AttrBuilder;
182 friend class AttributeSetImpl;
183 template <typename Ty> friend struct DenseMapInfo;
185 /// \brief The attributes that we are managing. This can be null to represent
186 /// the empty attributes list.
187 AttributeSetImpl *pImpl;
189 /// \brief The attributes for the specified index are returned.
190 AttributeSetNode *getAttributes(unsigned Index) const;
192 /// \brief Create an AttributeSet with the specified parameters in it.
193 static AttributeSet get(LLVMContext &C,
194 ArrayRef<std::pair<unsigned, Attribute> > Attrs);
195 static AttributeSet get(LLVMContext &C,
196 ArrayRef<std::pair<unsigned,
197 AttributeSetNode*> > Attrs);
199 static AttributeSet getImpl(LLVMContext &C,
200 ArrayRef<std::pair<unsigned,
201 AttributeSetNode*> > Attrs);
203 explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {}
206 AttributeSet() : pImpl(nullptr) {}
208 //===--------------------------------------------------------------------===//
209 // AttributeSet Construction and Mutation
210 //===--------------------------------------------------------------------===//
212 /// \brief Return an AttributeSet with the specified parameters in it.
213 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs);
214 static AttributeSet get(LLVMContext &C, unsigned Index,
215 ArrayRef<Attribute::AttrKind> Kind);
216 static AttributeSet get(LLVMContext &C, unsigned Index, const AttrBuilder &B);
218 /// \brief Add an attribute to the attribute set at the given index. Because
219 /// attribute sets are immutable, this returns a new set.
220 AttributeSet addAttribute(LLVMContext &C, unsigned Index,
221 Attribute::AttrKind Attr) const;
223 /// \brief Add an attribute to the attribute set at the given index. Because
224 /// attribute sets are immutable, this returns a new set.
225 AttributeSet addAttribute(LLVMContext &C, unsigned Index,
226 StringRef Kind) const;
227 AttributeSet addAttribute(LLVMContext &C, unsigned Index,
228 StringRef Kind, StringRef Value) const;
230 /// \brief Add attributes to the attribute set at the given index. Because
231 /// attribute sets are immutable, this returns a new set.
232 AttributeSet addAttributes(LLVMContext &C, unsigned Index,
233 AttributeSet Attrs) const;
235 /// \brief Remove the specified attribute at the specified index from this
236 /// attribute list. Because attribute lists are immutable, this returns the
238 AttributeSet removeAttribute(LLVMContext &C, unsigned Index,
239 Attribute::AttrKind Attr) const;
241 /// \brief Remove the specified attributes at the specified index from this
242 /// attribute list. Because attribute lists are immutable, this returns the
244 AttributeSet removeAttributes(LLVMContext &C, unsigned Index,
245 AttributeSet Attrs) const;
247 /// \brief Remove the specified attributes at the specified index from this
248 /// attribute list. Because attribute lists are immutable, this returns the
250 AttributeSet removeAttributes(LLVMContext &C, unsigned Index,
251 const AttrBuilder &Attrs) const;
253 /// \brief Add the dereferenceable attribute to the attribute set at the given
254 /// index. Because attribute sets are immutable, this returns a new set.
255 AttributeSet addDereferenceableAttr(LLVMContext &C, unsigned Index,
256 uint64_t Bytes) const;
258 /// \brief Add the dereferenceable_or_null attribute to the attribute set at
259 /// the given index. Because attribute sets are immutable, this returns a new
261 AttributeSet addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
262 uint64_t Bytes) const;
264 //===--------------------------------------------------------------------===//
265 // AttributeSet Accessors
266 //===--------------------------------------------------------------------===//
268 /// \brief Retrieve the LLVM context.
269 LLVMContext &getContext() const;
271 /// \brief The attributes for the specified index are returned.
272 AttributeSet getParamAttributes(unsigned Index) const;
274 /// \brief The attributes for the ret value are returned.
275 AttributeSet getRetAttributes() const;
277 /// \brief The function attributes are returned.
278 AttributeSet getFnAttributes() const;
280 /// \brief Return true if the attribute exists at the given index.
281 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
283 /// \brief Return true if the attribute exists at the given index.
284 bool hasAttribute(unsigned Index, StringRef Kind) const;
286 /// \brief Return true if attribute exists at the given index.
287 bool hasAttributes(unsigned Index) const;
289 /// \brief Return true if the specified attribute is set for at least one
290 /// parameter or for the return value.
291 bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
293 /// \brief Return the attribute object that exists at the given index.
294 Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
296 /// \brief Return the attribute object that exists at the given index.
297 Attribute getAttribute(unsigned Index, StringRef Kind) const;
299 /// \brief Return the alignment for the specified function parameter.
300 unsigned getParamAlignment(unsigned Index) const;
302 /// \brief Get the stack alignment.
303 unsigned getStackAlignment(unsigned Index) const;
305 /// \brief Get the number of dereferenceable bytes (or zero if unknown).
306 uint64_t getDereferenceableBytes(unsigned Index) const;
308 /// \brief Get the number of dereferenceable_or_null bytes (or zero if
310 uint64_t getDereferenceableOrNullBytes(unsigned Index) const;
312 /// \brief Return the attributes at the index as a string.
313 std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
315 typedef ArrayRef<Attribute>::iterator iterator;
317 iterator begin(unsigned Slot) const;
318 iterator end(unsigned Slot) const;
320 /// operator==/!= - Provide equality predicates.
321 bool operator==(const AttributeSet &RHS) const {
322 return pImpl == RHS.pImpl;
324 bool operator!=(const AttributeSet &RHS) const {
325 return pImpl != RHS.pImpl;
328 //===--------------------------------------------------------------------===//
329 // AttributeSet Introspection
330 //===--------------------------------------------------------------------===//
332 // FIXME: Remove this.
333 uint64_t Raw(unsigned Index) const;
335 /// \brief Return a raw pointer that uniquely identifies this attribute list.
336 void *getRawPointer() const {
340 /// \brief Return true if there are no attributes.
341 bool isEmpty() const {
342 return getNumSlots() == 0;
345 /// \brief Return the number of slots used in this attribute list. This is
346 /// the number of arguments that have an attribute set on them (including the
347 /// function itself).
348 unsigned getNumSlots() const;
350 /// \brief Return the index for the given slot.
351 unsigned getSlotIndex(unsigned Slot) const;
353 /// \brief Return the attributes at the given slot.
354 AttributeSet getSlotAttributes(unsigned Slot) const;
359 //===----------------------------------------------------------------------===//
361 /// \brief Provide DenseMapInfo for AttributeSet.
362 template<> struct DenseMapInfo<AttributeSet> {
363 static inline AttributeSet getEmptyKey() {
364 uintptr_t Val = static_cast<uintptr_t>(-1);
365 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
366 return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
368 static inline AttributeSet getTombstoneKey() {
369 uintptr_t Val = static_cast<uintptr_t>(-2);
370 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
371 return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
373 static unsigned getHashValue(AttributeSet AS) {
374 return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
375 (unsigned((uintptr_t)AS.pImpl) >> 9);
377 static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
380 //===----------------------------------------------------------------------===//
382 /// \brief This class is used in conjunction with the Attribute::get method to
383 /// create an Attribute object. The object itself is uniquified. The Builder's
384 /// value, however, is not. So this can be used as a quick way to test for
385 /// equality, presence of attributes, etc.
387 std::bitset<Attribute::EndAttrKinds> Attrs;
388 std::map<std::string, std::string> TargetDepAttrs;
390 uint64_t StackAlignment;
392 uint64_t DerefOrNullBytes;
396 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
397 DerefOrNullBytes(0) {}
398 explicit AttrBuilder(uint64_t Val)
399 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
400 DerefOrNullBytes(0) {
403 AttrBuilder(const Attribute &A)
404 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
405 DerefOrNullBytes(0) {
408 AttrBuilder(AttributeSet AS, unsigned Idx);
412 /// \brief Add an attribute to the builder.
413 AttrBuilder &addAttribute(Attribute::AttrKind Val);
415 /// \brief Add the Attribute object to the builder.
416 AttrBuilder &addAttribute(Attribute A);
418 /// \brief Add the target-dependent attribute to the builder.
419 AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
421 /// \brief Remove an attribute from the builder.
422 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
424 /// \brief Remove the attributes from the builder.
425 AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index);
427 /// \brief Remove the target-dependent attribute to the builder.
428 AttrBuilder &removeAttribute(StringRef A);
430 /// \brief Add the attributes from the builder.
431 AttrBuilder &merge(const AttrBuilder &B);
433 /// \brief Remove the attributes from the builder.
434 AttrBuilder &remove(const AttrBuilder &B);
436 /// \brief Return true if the builder has any attribute that's in the
437 /// specified builder.
438 bool overlaps(const AttrBuilder &B) const;
440 /// \brief Return true if the builder has the specified attribute.
441 bool contains(Attribute::AttrKind A) const {
442 assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
446 /// \brief Return true if the builder has the specified target-dependent
448 bool contains(StringRef A) const;
450 /// \brief Return true if the builder has IR-level attributes.
451 bool hasAttributes() const;
453 /// \brief Return true if the builder has any attribute that's in the
454 /// specified attribute.
455 bool hasAttributes(AttributeSet A, uint64_t Index) const;
457 /// \brief Return true if the builder has an alignment attribute.
458 bool hasAlignmentAttr() const;
460 /// \brief Retrieve the alignment attribute, if it exists.
461 uint64_t getAlignment() const { return Alignment; }
463 /// \brief Retrieve the stack alignment attribute, if it exists.
464 uint64_t getStackAlignment() const { return StackAlignment; }
466 /// \brief Retrieve the number of dereferenceable bytes, if the
467 /// dereferenceable attribute exists (zero is returned otherwise).
468 uint64_t getDereferenceableBytes() const { return DerefBytes; }
470 /// \brief Retrieve the number of dereferenceable_or_null bytes, if the
471 /// dereferenceable_or_null attribute exists (zero is returned otherwise).
472 uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; }
474 /// \brief This turns an int alignment (which must be a power of 2) into the
475 /// form used internally in Attribute.
476 AttrBuilder &addAlignmentAttr(unsigned Align);
478 /// \brief This turns an int stack alignment (which must be a power of 2) into
479 /// the form used internally in Attribute.
480 AttrBuilder &addStackAlignmentAttr(unsigned Align);
482 /// \brief This turns the number of dereferenceable bytes into the form used
483 /// internally in Attribute.
484 AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
486 /// \brief This turns the number of dereferenceable_or_null bytes into the
487 /// form used internally in Attribute.
488 AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
490 /// \brief Return true if the builder contains no target-independent
492 bool empty() const { return Attrs.none(); }
494 // Iterators for target-dependent attributes.
495 typedef std::pair<std::string, std::string> td_type;
496 typedef std::map<std::string, std::string>::iterator td_iterator;
497 typedef std::map<std::string, std::string>::const_iterator td_const_iterator;
498 typedef llvm::iterator_range<td_iterator> td_range;
499 typedef llvm::iterator_range<td_const_iterator> td_const_range;
501 td_iterator td_begin() { return TargetDepAttrs.begin(); }
502 td_iterator td_end() { return TargetDepAttrs.end(); }
504 td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
505 td_const_iterator td_end() const { return TargetDepAttrs.end(); }
507 td_range td_attrs() { return td_range(td_begin(), td_end()); }
508 td_const_range td_attrs() const {
509 return td_const_range(td_begin(), td_end());
512 bool td_empty() const { return TargetDepAttrs.empty(); }
514 bool operator==(const AttrBuilder &B);
515 bool operator!=(const AttrBuilder &B) {
516 return !(*this == B);
519 // FIXME: Remove this in 4.0.
521 /// \brief Add the raw value to the internal representation.
522 AttrBuilder &addRawValue(uint64_t Val);
525 namespace AttributeFuncs {
527 /// \brief Which attributes cannot be applied to a type.
528 AttrBuilder typeIncompatible(Type *Ty);
530 } // end AttributeFuncs namespace
532 } // end llvm namespace