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;
40 //===----------------------------------------------------------------------===//
42 /// \brief Functions, function parameters, and return types can have attributes
43 /// to indicate how they should be treated by optimizations and code
44 /// generation. This class represents one of those attributes. It's light-weight
45 /// and should be passed around by-value.
48 /// This enumeration lists the attributes that can be associated with
49 /// parameters, function results, or the function itself.
51 /// Note: The `uwtable' attribute is about the ABI or the user mandating an
52 /// entry in the unwind table. The `nounwind' attribute is about an exception
53 /// passing by the function.
55 /// In a theoretical system that uses tables for profiling and SjLj for
56 /// exceptions, they would be fully independent. In a normal system that uses
57 /// tables for both, the semantics are:
59 /// nil = Needs an entry because an exception might pass by.
60 /// nounwind = No need for an entry
61 /// uwtable = Needs an entry because the ABI says so and because
62 /// an exception might pass by.
63 /// uwtable + nounwind = Needs an entry because the ABI says so.
66 // IR-Level Attributes
67 None, ///< No attributes have been set
69 #include "llvm/IR/Attributes.inc"
70 EndAttrKinds ///< Sentinal value useful for loops
75 Attribute(AttributeImpl *A) : pImpl(A) {}
78 Attribute() : pImpl(nullptr) {}
80 //===--------------------------------------------------------------------===//
81 // Attribute Construction
82 //===--------------------------------------------------------------------===//
84 /// \brief Return a uniquified Attribute object.
85 static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
86 static Attribute get(LLVMContext &Context, StringRef Kind,
87 StringRef Val = StringRef());
89 /// \brief Return a uniquified Attribute object that has the specific
91 static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
92 static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
93 static Attribute getWithDereferenceableBytes(LLVMContext &Context,
95 static Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context,
98 //===--------------------------------------------------------------------===//
99 // Attribute Accessors
100 //===--------------------------------------------------------------------===//
102 /// \brief Return true if the attribute is an Attribute::AttrKind type.
103 bool isEnumAttribute() const;
105 /// \brief Return true if the attribute is an integer attribute.
106 bool isIntAttribute() const;
108 /// \brief Return true if the attribute is a string (target-dependent)
110 bool isStringAttribute() const;
112 /// \brief Return true if the attribute is present.
113 bool hasAttribute(AttrKind Val) const;
115 /// \brief Return true if the target-dependent attribute is present.
116 bool hasAttribute(StringRef Val) const;
118 /// \brief Return the attribute's kind as an enum (Attribute::AttrKind). This
119 /// requires the attribute to be an enum or alignment attribute.
120 Attribute::AttrKind getKindAsEnum() const;
122 /// \brief Return the attribute's value as an integer. This requires that the
123 /// attribute be an alignment attribute.
124 uint64_t getValueAsInt() const;
126 /// \brief Return the attribute's kind as a string. This requires the
127 /// attribute to be a string attribute.
128 StringRef getKindAsString() const;
130 /// \brief Return the attribute's value as a string. This requires the
131 /// attribute to be a string attribute.
132 StringRef getValueAsString() const;
134 /// \brief Returns the alignment field of an attribute as a byte alignment
136 unsigned getAlignment() const;
138 /// \brief Returns the stack alignment field of an attribute as a byte
140 unsigned getStackAlignment() const;
142 /// \brief Returns the number of dereferenceable bytes from the
143 /// dereferenceable attribute (or zero if unknown).
144 uint64_t getDereferenceableBytes() const;
146 /// \brief Returns the number of dereferenceable_or_null bytes from the
147 /// dereferenceable_or_null attribute (or zero if unknown).
148 uint64_t getDereferenceableOrNullBytes() const;
150 /// \brief The Attribute is converted to a string of equivalent mnemonic. This
151 /// is, presumably, for writing out the mnemonics for the assembly writer.
152 std::string getAsString(bool InAttrGrp = false) const;
154 /// \brief Equality and non-equality operators.
155 bool operator==(Attribute A) const { return pImpl == A.pImpl; }
156 bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
158 /// \brief Less-than operator. Useful for sorting the attributes list.
159 bool operator<(Attribute A) const;
161 void Profile(FoldingSetNodeID &ID) const {
162 ID.AddPointer(pImpl);
166 //===----------------------------------------------------------------------===//
168 /// \brief This class holds the attributes for a function, its return value, and
169 /// its parameters. You access the attributes for each of them via an index into
170 /// the AttributeSet object. The function attributes are at index
171 /// `AttributeSet::FunctionIndex', the return value is at index
172 /// `AttributeSet::ReturnIndex', and the attributes for the parameters start at
176 enum AttrIndex : unsigned {
182 friend class AttrBuilder;
183 friend class AttributeSetImpl;
184 template <typename Ty> friend struct DenseMapInfo;
186 /// \brief The attributes that we are managing. This can be null to represent
187 /// the empty attributes list.
188 AttributeSetImpl *pImpl;
190 /// \brief The attributes for the specified index are returned.
191 AttributeSetNode *getAttributes(unsigned Index) const;
193 /// \brief Create an AttributeSet with the specified parameters in it.
194 static AttributeSet get(LLVMContext &C,
195 ArrayRef<std::pair<unsigned, Attribute> > Attrs);
196 static AttributeSet get(LLVMContext &C,
197 ArrayRef<std::pair<unsigned,
198 AttributeSetNode*> > Attrs);
200 static AttributeSet getImpl(LLVMContext &C,
201 ArrayRef<std::pair<unsigned,
202 AttributeSetNode*> > Attrs);
204 explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {}
207 AttributeSet() : pImpl(nullptr) {}
209 //===--------------------------------------------------------------------===//
210 // AttributeSet Construction and Mutation
211 //===--------------------------------------------------------------------===//
213 /// \brief Return an AttributeSet with the specified parameters in it.
214 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs);
215 static AttributeSet get(LLVMContext &C, unsigned Index,
216 ArrayRef<Attribute::AttrKind> Kind);
217 static AttributeSet get(LLVMContext &C, unsigned Index, const AttrBuilder &B);
219 /// \brief Add an attribute to the attribute set at the given index. Because
220 /// attribute sets are immutable, this returns a new set.
221 AttributeSet addAttribute(LLVMContext &C, unsigned Index,
222 Attribute::AttrKind Attr) const;
224 /// \brief Add an attribute to the attribute set at the given index. Because
225 /// attribute sets are immutable, this returns a new set.
226 AttributeSet addAttribute(LLVMContext &C, unsigned Index,
227 StringRef Kind) const;
228 AttributeSet addAttribute(LLVMContext &C, unsigned Index,
229 StringRef Kind, StringRef Value) const;
231 /// \brief Add attributes to the attribute set at the given index. Because
232 /// attribute sets are immutable, this returns a new set.
233 AttributeSet addAttributes(LLVMContext &C, unsigned Index,
234 AttributeSet Attrs) const;
236 /// \brief Remove the specified attribute at the specified index from this
237 /// attribute list. Because attribute lists are immutable, this returns the
239 AttributeSet removeAttribute(LLVMContext &C, unsigned Index,
240 Attribute::AttrKind Attr) const;
242 /// \brief Remove the specified attributes at the specified index from this
243 /// attribute list. Because attribute lists are immutable, this returns the
245 AttributeSet removeAttributes(LLVMContext &C, unsigned Index,
246 AttributeSet Attrs) const;
248 /// \brief Remove the specified attributes at the specified index from this
249 /// attribute list. Because attribute lists are immutable, this returns the
251 AttributeSet removeAttributes(LLVMContext &C, unsigned Index,
252 const AttrBuilder &Attrs) const;
254 /// \brief Add the dereferenceable attribute to the attribute set at the given
255 /// index. Because attribute sets are immutable, this returns a new set.
256 AttributeSet addDereferenceableAttr(LLVMContext &C, unsigned Index,
257 uint64_t Bytes) const;
259 /// \brief Add the dereferenceable_or_null attribute to the attribute set at
260 /// the given index. Because attribute sets are immutable, this returns a new
262 AttributeSet addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
263 uint64_t Bytes) const;
265 //===--------------------------------------------------------------------===//
266 // AttributeSet Accessors
267 //===--------------------------------------------------------------------===//
269 /// \brief Retrieve the LLVM context.
270 LLVMContext &getContext() const;
272 /// \brief The attributes for the specified index are returned.
273 AttributeSet getParamAttributes(unsigned Index) const;
275 /// \brief The attributes for the ret value are returned.
276 AttributeSet getRetAttributes() const;
278 /// \brief The function attributes are returned.
279 AttributeSet getFnAttributes() const;
281 /// \brief Return true if the attribute exists at the given index.
282 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
284 /// \brief Return true if the attribute exists at the given index.
285 bool hasAttribute(unsigned Index, StringRef Kind) const;
287 /// \brief Return true if attribute exists at the given index.
288 bool hasAttributes(unsigned Index) const;
290 /// \brief Return true if the specified attribute is set for at least one
291 /// parameter or for the return value.
292 bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
294 /// \brief Return the attribute object that exists at the given index.
295 Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
297 /// \brief Return the attribute object that exists at the given index.
298 Attribute getAttribute(unsigned Index, StringRef Kind) const;
300 /// \brief Return the alignment for the specified function parameter.
301 unsigned getParamAlignment(unsigned Index) const;
303 /// \brief Get the stack alignment.
304 unsigned getStackAlignment(unsigned Index) const;
306 /// \brief Get the number of dereferenceable bytes (or zero if unknown).
307 uint64_t getDereferenceableBytes(unsigned Index) const;
309 /// \brief Get the number of dereferenceable_or_null bytes (or zero if
311 uint64_t getDereferenceableOrNullBytes(unsigned Index) const;
313 /// \brief Return the attributes at the index as a string.
314 std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
316 typedef ArrayRef<Attribute>::iterator iterator;
318 iterator begin(unsigned Slot) const;
319 iterator end(unsigned Slot) const;
321 /// operator==/!= - Provide equality predicates.
322 bool operator==(const AttributeSet &RHS) const {
323 return pImpl == RHS.pImpl;
325 bool operator!=(const AttributeSet &RHS) const {
326 return pImpl != RHS.pImpl;
329 //===--------------------------------------------------------------------===//
330 // AttributeSet Introspection
331 //===--------------------------------------------------------------------===//
333 // FIXME: Remove this.
334 uint64_t Raw(unsigned Index) const;
336 /// \brief Return a raw pointer that uniquely identifies this attribute list.
337 void *getRawPointer() const {
341 /// \brief Return true if there are no attributes.
342 bool isEmpty() const {
343 return getNumSlots() == 0;
346 /// \brief Return the number of slots used in this attribute list. This is
347 /// the number of arguments that have an attribute set on them (including the
348 /// function itself).
349 unsigned getNumSlots() const;
351 /// \brief Return the index for the given slot.
352 unsigned getSlotIndex(unsigned Slot) const;
354 /// \brief Return the attributes at the given slot.
355 AttributeSet getSlotAttributes(unsigned Slot) const;
360 //===----------------------------------------------------------------------===//
362 /// \brief Provide DenseMapInfo for AttributeSet.
363 template<> struct DenseMapInfo<AttributeSet> {
364 static inline AttributeSet getEmptyKey() {
365 uintptr_t Val = static_cast<uintptr_t>(-1);
366 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
367 return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
369 static inline AttributeSet getTombstoneKey() {
370 uintptr_t Val = static_cast<uintptr_t>(-2);
371 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
372 return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
374 static unsigned getHashValue(AttributeSet AS) {
375 return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
376 (unsigned((uintptr_t)AS.pImpl) >> 9);
378 static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
381 //===----------------------------------------------------------------------===//
383 /// \brief This class is used in conjunction with the Attribute::get method to
384 /// create an Attribute object. The object itself is uniquified. The Builder's
385 /// value, however, is not. So this can be used as a quick way to test for
386 /// equality, presence of attributes, etc.
388 std::bitset<Attribute::EndAttrKinds> Attrs;
389 std::map<std::string, std::string> TargetDepAttrs;
391 uint64_t StackAlignment;
393 uint64_t DerefOrNullBytes;
397 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
398 DerefOrNullBytes(0) {}
399 explicit AttrBuilder(uint64_t Val)
400 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
401 DerefOrNullBytes(0) {
404 AttrBuilder(const Attribute &A)
405 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
406 DerefOrNullBytes(0) {
409 AttrBuilder(AttributeSet AS, unsigned Idx);
413 /// \brief Add an attribute to the builder.
414 AttrBuilder &addAttribute(Attribute::AttrKind Val);
416 /// \brief Add the Attribute object to the builder.
417 AttrBuilder &addAttribute(Attribute A);
419 /// \brief Add the target-dependent attribute to the builder.
420 AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
422 /// \brief Remove an attribute from the builder.
423 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
425 /// \brief Remove the attributes from the builder.
426 AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index);
428 /// \brief Remove the target-dependent attribute to the builder.
429 AttrBuilder &removeAttribute(StringRef A);
431 /// \brief Add the attributes from the builder.
432 AttrBuilder &merge(const AttrBuilder &B);
434 /// \brief Remove the attributes from the builder.
435 AttrBuilder &remove(const AttrBuilder &B);
437 /// \brief Return true if the builder has any attribute that's in the
438 /// specified builder.
439 bool overlaps(const AttrBuilder &B) const;
441 /// \brief Return true if the builder has the specified attribute.
442 bool contains(Attribute::AttrKind A) const {
443 assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
447 /// \brief Return true if the builder has the specified target-dependent
449 bool contains(StringRef A) const;
451 /// \brief Return true if the builder has IR-level attributes.
452 bool hasAttributes() const;
454 /// \brief Return true if the builder has any attribute that's in the
455 /// specified attribute.
456 bool hasAttributes(AttributeSet A, uint64_t Index) const;
458 /// \brief Return true if the builder has an alignment attribute.
459 bool hasAlignmentAttr() const;
461 /// \brief Retrieve the alignment attribute, if it exists.
462 uint64_t getAlignment() const { return Alignment; }
464 /// \brief Retrieve the stack alignment attribute, if it exists.
465 uint64_t getStackAlignment() const { return StackAlignment; }
467 /// \brief Retrieve the number of dereferenceable bytes, if the
468 /// dereferenceable attribute exists (zero is returned otherwise).
469 uint64_t getDereferenceableBytes() const { return DerefBytes; }
471 /// \brief Retrieve the number of dereferenceable_or_null bytes, if the
472 /// dereferenceable_or_null attribute exists (zero is returned otherwise).
473 uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; }
475 /// \brief This turns an int alignment (which must be a power of 2) into the
476 /// form used internally in Attribute.
477 AttrBuilder &addAlignmentAttr(unsigned Align);
479 /// \brief This turns an int stack alignment (which must be a power of 2) into
480 /// the form used internally in Attribute.
481 AttrBuilder &addStackAlignmentAttr(unsigned Align);
483 /// \brief This turns the number of dereferenceable bytes into the form used
484 /// internally in Attribute.
485 AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
487 /// \brief This turns the number of dereferenceable_or_null bytes into the
488 /// form used internally in Attribute.
489 AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
491 /// \brief Return true if the builder contains no target-independent
493 bool empty() const { return Attrs.none(); }
495 // Iterators for target-dependent attributes.
496 typedef std::pair<std::string, std::string> td_type;
497 typedef std::map<std::string, std::string>::iterator td_iterator;
498 typedef std::map<std::string, std::string>::const_iterator td_const_iterator;
499 typedef llvm::iterator_range<td_iterator> td_range;
500 typedef llvm::iterator_range<td_const_iterator> td_const_range;
502 td_iterator td_begin() { return TargetDepAttrs.begin(); }
503 td_iterator td_end() { return TargetDepAttrs.end(); }
505 td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
506 td_const_iterator td_end() const { return TargetDepAttrs.end(); }
508 td_range td_attrs() { return td_range(td_begin(), td_end()); }
509 td_const_range td_attrs() const {
510 return td_const_range(td_begin(), td_end());
513 bool td_empty() const { return TargetDepAttrs.empty(); }
515 bool operator==(const AttrBuilder &B);
516 bool operator!=(const AttrBuilder &B) {
517 return !(*this == B);
520 // FIXME: Remove this in 4.0.
522 /// \brief Add the raw value to the internal representation.
523 AttrBuilder &addRawValue(uint64_t Val);
526 namespace AttributeFuncs {
528 /// \brief Which attributes cannot be applied to a type.
529 AttrBuilder typeIncompatible(Type *Ty);
531 /// \returns Return true if the two functions have compatible target-independent
532 /// attributes for inlining purposes.
533 bool areInlineCompatible(const Function &Caller, const Function &Callee);
535 /// \brief Merge caller's and callee's attributes.
536 void mergeAttributesForInlining(Function &Caller, const Function &Callee);
538 } // end AttributeFuncs namespace
540 } // end llvm namespace