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.
144 uint64_t getDereferenceableBytes() const;
146 /// \brief Returns the number of dereferenceable_or_null bytes from the
147 /// dereferenceable_or_null attribute.
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 /// Add an attribute to the attribute set at the given indices. Because
232 /// attribute sets are immutable, this returns a new set.
233 AttributeSet addAttribute(LLVMContext &C, ArrayRef<unsigned> Indices,
236 /// \brief Add attributes to the attribute set at the given index. Because
237 /// attribute sets are immutable, this returns a new set.
238 AttributeSet addAttributes(LLVMContext &C, unsigned Index,
239 AttributeSet Attrs) const;
241 /// \brief Remove the specified attribute at the specified index from this
242 /// attribute list. Because attribute lists are immutable, this returns the
244 AttributeSet removeAttribute(LLVMContext &C, unsigned Index,
245 Attribute::AttrKind Attr) 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 AttributeSet Attrs) const;
253 /// \brief Remove the specified attributes at the specified index from this
254 /// attribute list. Because attribute lists are immutable, this returns the
256 AttributeSet removeAttributes(LLVMContext &C, unsigned Index,
257 const AttrBuilder &Attrs) const;
259 /// \brief Add the dereferenceable attribute to the attribute set at the given
260 /// index. Because attribute sets are immutable, this returns a new set.
261 AttributeSet addDereferenceableAttr(LLVMContext &C, unsigned Index,
262 uint64_t Bytes) const;
264 /// \brief Add the dereferenceable_or_null attribute to the attribute set at
265 /// the given index. Because attribute sets are immutable, this returns a new
267 AttributeSet addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
268 uint64_t Bytes) const;
270 //===--------------------------------------------------------------------===//
271 // AttributeSet Accessors
272 //===--------------------------------------------------------------------===//
274 /// \brief Retrieve the LLVM context.
275 LLVMContext &getContext() const;
277 /// \brief The attributes for the specified index are returned.
278 AttributeSet getParamAttributes(unsigned Index) const;
280 /// \brief The attributes for the ret value are returned.
281 AttributeSet getRetAttributes() const;
283 /// \brief The function attributes are returned.
284 AttributeSet getFnAttributes() const;
286 /// \brief Return true if the attribute exists at the given index.
287 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
289 /// \brief Return true if the attribute exists at the given index.
290 bool hasAttribute(unsigned Index, StringRef Kind) const;
292 /// \brief Return true if attribute exists at the given index.
293 bool hasAttributes(unsigned Index) const;
295 /// \brief Return true if the specified attribute is set for at least one
296 /// parameter or for the return value.
297 bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
299 /// \brief Return the attribute object that exists at the given index.
300 Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
302 /// \brief Return the attribute object that exists at the given index.
303 Attribute getAttribute(unsigned Index, StringRef Kind) const;
305 /// \brief Return the alignment for the specified function parameter.
306 unsigned getParamAlignment(unsigned Index) const;
308 /// \brief Get the stack alignment.
309 unsigned getStackAlignment(unsigned Index) const;
311 /// \brief Get the number of dereferenceable bytes (or zero if unknown).
312 uint64_t getDereferenceableBytes(unsigned Index) const;
314 /// \brief Get the number of dereferenceable_or_null bytes (or zero if
316 uint64_t getDereferenceableOrNullBytes(unsigned Index) const;
318 /// \brief Return the attributes at the index as a string.
319 std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
321 typedef ArrayRef<Attribute>::iterator iterator;
323 iterator begin(unsigned Slot) const;
324 iterator end(unsigned Slot) const;
326 /// operator==/!= - Provide equality predicates.
327 bool operator==(const AttributeSet &RHS) const {
328 return pImpl == RHS.pImpl;
330 bool operator!=(const AttributeSet &RHS) const {
331 return pImpl != RHS.pImpl;
334 //===--------------------------------------------------------------------===//
335 // AttributeSet Introspection
336 //===--------------------------------------------------------------------===//
338 // FIXME: Remove this.
339 uint64_t Raw(unsigned Index) const;
341 /// \brief Return a raw pointer that uniquely identifies this attribute list.
342 void *getRawPointer() const {
346 /// \brief Return true if there are no attributes.
347 bool isEmpty() const {
348 return getNumSlots() == 0;
351 /// \brief Return the number of slots used in this attribute list. This is
352 /// the number of arguments that have an attribute set on them (including the
353 /// function itself).
354 unsigned getNumSlots() const;
356 /// \brief Return the index for the given slot.
357 unsigned getSlotIndex(unsigned Slot) const;
359 /// \brief Return the attributes at the given slot.
360 AttributeSet getSlotAttributes(unsigned Slot) const;
365 //===----------------------------------------------------------------------===//
367 /// \brief Provide DenseMapInfo for AttributeSet.
368 template<> struct DenseMapInfo<AttributeSet> {
369 static inline AttributeSet getEmptyKey() {
370 uintptr_t Val = static_cast<uintptr_t>(-1);
371 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
372 return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
374 static inline AttributeSet getTombstoneKey() {
375 uintptr_t Val = static_cast<uintptr_t>(-2);
376 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
377 return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
379 static unsigned getHashValue(AttributeSet AS) {
380 return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
381 (unsigned((uintptr_t)AS.pImpl) >> 9);
383 static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
386 //===----------------------------------------------------------------------===//
388 /// \brief This class is used in conjunction with the Attribute::get method to
389 /// create an Attribute object. The object itself is uniquified. The Builder's
390 /// value, however, is not. So this can be used as a quick way to test for
391 /// equality, presence of attributes, etc.
393 std::bitset<Attribute::EndAttrKinds> Attrs;
394 std::map<std::string, std::string> TargetDepAttrs;
396 uint64_t StackAlignment;
398 uint64_t DerefOrNullBytes;
402 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
403 DerefOrNullBytes(0) {}
404 explicit AttrBuilder(uint64_t Val)
405 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
406 DerefOrNullBytes(0) {
409 AttrBuilder(const Attribute &A)
410 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
411 DerefOrNullBytes(0) {
414 AttrBuilder(AttributeSet AS, unsigned Idx);
418 /// \brief Add an attribute to the builder.
419 AttrBuilder &addAttribute(Attribute::AttrKind Val);
421 /// \brief Add the Attribute object to the builder.
422 AttrBuilder &addAttribute(Attribute A);
424 /// \brief Add the target-dependent attribute to the builder.
425 AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
427 /// \brief Remove an attribute from the builder.
428 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
430 /// \brief Remove the attributes from the builder.
431 AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index);
433 /// \brief Remove the target-dependent attribute to the builder.
434 AttrBuilder &removeAttribute(StringRef A);
436 /// \brief Add the attributes from the builder.
437 AttrBuilder &merge(const AttrBuilder &B);
439 /// \brief Remove the attributes from the builder.
440 AttrBuilder &remove(const AttrBuilder &B);
442 /// \brief Return true if the builder has any attribute that's in the
443 /// specified builder.
444 bool overlaps(const AttrBuilder &B) const;
446 /// \brief Return true if the builder has the specified attribute.
447 bool contains(Attribute::AttrKind A) const {
448 assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
452 /// \brief Return true if the builder has the specified target-dependent
454 bool contains(StringRef A) const;
456 /// \brief Return true if the builder has IR-level attributes.
457 bool hasAttributes() const;
459 /// \brief Return true if the builder has any attribute that's in the
460 /// specified attribute.
461 bool hasAttributes(AttributeSet A, uint64_t Index) const;
463 /// \brief Return true if the builder has an alignment attribute.
464 bool hasAlignmentAttr() const;
466 /// \brief Retrieve the alignment attribute, if it exists.
467 uint64_t getAlignment() const { return Alignment; }
469 /// \brief Retrieve the stack alignment attribute, if it exists.
470 uint64_t getStackAlignment() const { return StackAlignment; }
472 /// \brief Retrieve the number of dereferenceable bytes, if the
473 /// dereferenceable attribute exists (zero is returned otherwise).
474 uint64_t getDereferenceableBytes() const { return DerefBytes; }
476 /// \brief Retrieve the number of dereferenceable_or_null bytes, if the
477 /// dereferenceable_or_null attribute exists (zero is returned otherwise).
478 uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; }
480 /// \brief This turns an int alignment (which must be a power of 2) into the
481 /// form used internally in Attribute.
482 AttrBuilder &addAlignmentAttr(unsigned Align);
484 /// \brief This turns an int stack alignment (which must be a power of 2) into
485 /// the form used internally in Attribute.
486 AttrBuilder &addStackAlignmentAttr(unsigned Align);
488 /// \brief This turns the number of dereferenceable bytes into the form used
489 /// internally in Attribute.
490 AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
492 /// \brief This turns the number of dereferenceable_or_null bytes into the
493 /// form used internally in Attribute.
494 AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
496 /// \brief Return true if the builder contains no target-independent
498 bool empty() const { return Attrs.none(); }
500 // Iterators for target-dependent attributes.
501 typedef std::pair<std::string, std::string> td_type;
502 typedef std::map<std::string, std::string>::iterator td_iterator;
503 typedef std::map<std::string, std::string>::const_iterator td_const_iterator;
504 typedef llvm::iterator_range<td_iterator> td_range;
505 typedef llvm::iterator_range<td_const_iterator> td_const_range;
507 td_iterator td_begin() { return TargetDepAttrs.begin(); }
508 td_iterator td_end() { return TargetDepAttrs.end(); }
510 td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
511 td_const_iterator td_end() const { return TargetDepAttrs.end(); }
513 td_range td_attrs() { return td_range(td_begin(), td_end()); }
514 td_const_range td_attrs() const {
515 return td_const_range(td_begin(), td_end());
518 bool td_empty() const { return TargetDepAttrs.empty(); }
520 bool operator==(const AttrBuilder &B);
521 bool operator!=(const AttrBuilder &B) {
522 return !(*this == B);
525 // FIXME: Remove this in 4.0.
527 /// \brief Add the raw value to the internal representation.
528 AttrBuilder &addRawValue(uint64_t Val);
531 namespace AttributeFuncs {
533 /// \brief Which attributes cannot be applied to a type.
534 AttrBuilder typeIncompatible(Type *Ty);
536 /// \returns Return true if the two functions have compatible target-independent
537 /// attributes for inlining purposes.
538 bool areInlineCompatible(const Function &Caller, const Function &Callee);
540 /// \brief Merge caller's and callee's attributes.
541 void mergeAttributesForInlining(Function &Caller, const Function &Callee);
543 } // end AttributeFuncs namespace
545 } // end llvm namespace