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
67 Alignment, ///< Alignment of parameter (5 bits)
68 ///< stored as log2 of alignment with +1 bias
69 ///< 0 means unaligned (different from align(1))
70 AlwaysInline, ///< inline=always
71 Builtin, ///< Callee is recognized as a builtin, despite
72 ///< nobuiltin attribute on its declaration.
73 ByVal, ///< Pass structure by value
74 InAlloca, ///< Pass structure in an alloca
75 Cold, ///< Marks function as being in a cold path.
76 InlineHint, ///< Source said inlining was desirable
77 InReg, ///< Force argument to be passed in register
78 MinSize, ///< Function must be optimized for size first
79 Naked, ///< Naked function
80 Nest, ///< Nested function static chain
81 NoAlias, ///< Considered to not alias after call
82 NoBuiltin, ///< Callee isn't recognized as a builtin
83 NoCapture, ///< Function creates no aliases of pointer
84 NoDuplicate, ///< Call cannot be duplicated
85 NoImplicitFloat, ///< Disable implicit floating point insts
86 NoInline, ///< inline=never
87 NonLazyBind, ///< Function is called early and/or
88 ///< often, so lazy binding isn't worthwhile
89 NonNull, ///< Pointer is known to be not null
90 NoRedZone, ///< Disable redzone
91 NoReturn, ///< Mark the function as not returning
92 NoUnwind, ///< Function doesn't unwind stack
93 OptimizeForSize, ///< opt_size
94 OptimizeNone, ///< Function must not be optimized.
95 ReadNone, ///< Function does not access memory
96 ReadOnly, ///< Function only reads from memory
97 Returned, ///< Return value is always equal to this argument
98 ReturnsTwice, ///< Function can return twice
99 SExt, ///< Sign extended before/after call
100 StackAlignment, ///< Alignment of stack for function (3 bits)
101 ///< stored as log2 of alignment with +1 bias 0
102 ///< means unaligned (different from
104 StackProtect, ///< Stack protection.
105 StackProtectReq, ///< Stack protection required.
106 StackProtectStrong, ///< Strong Stack protection.
107 StructRet, ///< Hidden pointer to structure to return
108 SanitizeAddress, ///< AddressSanitizer is on.
109 SanitizeThread, ///< ThreadSanitizer is on.
110 SanitizeMemory, ///< MemorySanitizer is on.
111 UWTable, ///< Function must be in a unwind table
112 ZExt, ///< Zero extended before/after call
114 EndAttrKinds ///< Sentinal value useful for loops
117 AttributeImpl *pImpl;
118 Attribute(AttributeImpl *A) : pImpl(A) {}
120 Attribute() : pImpl(nullptr) {}
122 //===--------------------------------------------------------------------===//
123 // Attribute Construction
124 //===--------------------------------------------------------------------===//
126 /// \brief Return a uniquified Attribute object.
127 static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
128 static Attribute get(LLVMContext &Context, StringRef Kind,
129 StringRef Val = StringRef());
131 /// \brief Return a uniquified Attribute object that has the specific
133 static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
134 static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
136 //===--------------------------------------------------------------------===//
137 // Attribute Accessors
138 //===--------------------------------------------------------------------===//
140 /// \brief Return true if the attribute is an Attribute::AttrKind type.
141 bool isEnumAttribute() const;
143 /// \brief Return true if the attribute is an alignment attribute.
144 bool isAlignAttribute() const;
146 /// \brief Return true if the attribute is a string (target-dependent)
148 bool isStringAttribute() const;
150 /// \brief Return true if the attribute is present.
151 bool hasAttribute(AttrKind Val) const;
153 /// \brief Return true if the target-dependent attribute is present.
154 bool hasAttribute(StringRef Val) const;
156 /// \brief Return the attribute's kind as an enum (Attribute::AttrKind). This
157 /// requires the attribute to be an enum or alignment attribute.
158 Attribute::AttrKind getKindAsEnum() const;
160 /// \brief Return the attribute's value as an integer. This requires that the
161 /// attribute be an alignment attribute.
162 uint64_t getValueAsInt() const;
164 /// \brief Return the attribute's kind as a string. This requires the
165 /// attribute to be a string attribute.
166 StringRef getKindAsString() const;
168 /// \brief Return the attribute's value as a string. This requires the
169 /// attribute to be a string attribute.
170 StringRef getValueAsString() const;
172 /// \brief Returns the alignment field of an attribute as a byte alignment
174 unsigned getAlignment() const;
176 /// \brief Returns the stack alignment field of an attribute as a byte
178 unsigned getStackAlignment() const;
180 /// \brief The Attribute is converted to a string of equivalent mnemonic. This
181 /// is, presumably, for writing out the mnemonics for the assembly writer.
182 std::string getAsString(bool InAttrGrp = false) const;
184 /// \brief Equality and non-equality operators.
185 bool operator==(Attribute A) const { return pImpl == A.pImpl; }
186 bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
188 /// \brief Less-than operator. Useful for sorting the attributes list.
189 bool operator<(Attribute A) const;
191 void Profile(FoldingSetNodeID &ID) const {
192 ID.AddPointer(pImpl);
196 //===----------------------------------------------------------------------===//
198 /// \brief This class holds the attributes for a function, its return value, and
199 /// its parameters. You access the attributes for each of them via an index into
200 /// the AttributeSet object. The function attributes are at index
201 /// `AttributeSet::FunctionIndex', the return value is at index
202 /// `AttributeSet::ReturnIndex', and the attributes for the parameters start at
206 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);
234 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> Kind);
246 static AttributeSet get(LLVMContext &C, unsigned Index, const AttrBuilder &B);
248 /// \brief Add an attribute to the attribute set at the given index. Since
249 /// attribute sets are immutable, this returns a new set.
250 AttributeSet addAttribute(LLVMContext &C, unsigned Index,
251 Attribute::AttrKind Attr) const;
253 /// \brief Add an attribute to the attribute set at the given index. Since
254 /// attribute sets are immutable, this returns a new set.
255 AttributeSet addAttribute(LLVMContext &C, unsigned Index,
256 StringRef Kind) const;
257 AttributeSet addAttribute(LLVMContext &C, unsigned Index,
258 StringRef Kind, StringRef Value) const;
260 /// \brief Add attributes to the attribute set at the given index. Since
261 /// attribute sets are immutable, this returns a new set.
262 AttributeSet addAttributes(LLVMContext &C, unsigned Index,
263 AttributeSet Attrs) const;
265 /// \brief Remove the specified attribute at the specified index from this
266 /// attribute list. Since attribute lists are immutable, this returns the new
268 AttributeSet removeAttribute(LLVMContext &C, unsigned Index,
269 Attribute::AttrKind Attr) const;
271 /// \brief Remove the specified attributes at the specified index from this
272 /// attribute list. Since attribute lists are immutable, this returns the new
274 AttributeSet removeAttributes(LLVMContext &C, unsigned Index,
275 AttributeSet Attrs) const;
277 //===--------------------------------------------------------------------===//
278 // AttributeSet Accessors
279 //===--------------------------------------------------------------------===//
281 /// \brief Retrieve the LLVM context.
282 LLVMContext &getContext() const;
284 /// \brief The attributes for the specified index are returned.
285 AttributeSet getParamAttributes(unsigned Index) const;
287 /// \brief The attributes for the ret value are returned.
288 AttributeSet getRetAttributes() const;
290 /// \brief The function attributes are returned.
291 AttributeSet getFnAttributes() const;
293 /// \brief Return true if the attribute exists at the given index.
294 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
296 /// \brief Return true if the attribute exists at the given index.
297 bool hasAttribute(unsigned Index, StringRef Kind) const;
299 /// \brief Return true if attribute exists at the given index.
300 bool hasAttributes(unsigned Index) const;
302 /// \brief Return true if the specified attribute is set for at least one
303 /// parameter or for the return value.
304 bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
306 /// \brief Return the attribute object that exists at the given index.
307 Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
309 /// \brief Return the attribute object that exists at the given index.
310 Attribute getAttribute(unsigned Index, StringRef Kind) const;
312 /// \brief Return the alignment for the specified function parameter.
313 unsigned getParamAlignment(unsigned Index) const;
315 /// \brief Get the stack alignment.
316 unsigned getStackAlignment(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 AttrBuilder() : Attrs(0), Alignment(0), StackAlignment(0) {}
399 explicit AttrBuilder(uint64_t Val)
400 : Attrs(0), Alignment(0), StackAlignment(0) {
403 AttrBuilder(const Attribute &A) : Attrs(0), Alignment(0), StackAlignment(0) {
406 AttrBuilder(AttributeSet AS, unsigned Idx);
410 /// \brief Add an attribute to the builder.
411 AttrBuilder &addAttribute(Attribute::AttrKind Val);
413 /// \brief Add the Attribute object to the builder.
414 AttrBuilder &addAttribute(Attribute A);
416 /// \brief Add the target-dependent attribute to the builder.
417 AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
419 /// \brief Remove an attribute from the builder.
420 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
422 /// \brief Remove the attributes from the builder.
423 AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index);
425 /// \brief Remove the target-dependent attribute to the builder.
426 AttrBuilder &removeAttribute(StringRef A);
428 /// \brief Add the attributes from the builder.
429 AttrBuilder &merge(const AttrBuilder &B);
431 /// \brief Return true if the builder has the specified attribute.
432 bool contains(Attribute::AttrKind A) const {
433 assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
437 /// \brief Return true if the builder has the specified target-dependent
439 bool contains(StringRef A) const;
441 /// \brief Return true if the builder has IR-level attributes.
442 bool hasAttributes() const;
444 /// \brief Return true if the builder has any attribute that's in the
445 /// specified attribute.
446 bool hasAttributes(AttributeSet A, uint64_t Index) const;
448 /// \brief Return true if the builder has an alignment attribute.
449 bool hasAlignmentAttr() const;
451 /// \brief Retrieve the alignment attribute, if it exists.
452 uint64_t getAlignment() const { return Alignment; }
454 /// \brief Retrieve the stack alignment attribute, if it exists.
455 uint64_t getStackAlignment() const { return StackAlignment; }
457 /// \brief This turns an int alignment (which must be a power of 2) into the
458 /// form used internally in Attribute.
459 AttrBuilder &addAlignmentAttr(unsigned Align);
461 /// \brief This turns an int stack alignment (which must be a power of 2) into
462 /// the form used internally in Attribute.
463 AttrBuilder &addStackAlignmentAttr(unsigned Align);
465 /// \brief Return true if the builder contains no target-independent
467 bool empty() const { return Attrs.none(); }
469 // Iterators for target-dependent attributes.
470 typedef std::pair<std::string, std::string> td_type;
471 typedef std::map<std::string, std::string>::iterator td_iterator;
472 typedef std::map<std::string, std::string>::const_iterator td_const_iterator;
473 typedef llvm::iterator_range<td_iterator> td_range;
474 typedef llvm::iterator_range<td_const_iterator> td_const_range;
476 td_iterator td_begin() { return TargetDepAttrs.begin(); }
477 td_iterator td_end() { return TargetDepAttrs.end(); }
479 td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
480 td_const_iterator td_end() const { return TargetDepAttrs.end(); }
482 td_range td_attrs() { return td_range(td_begin(), td_end()); }
483 td_const_range td_attrs() const {
484 return td_const_range(td_begin(), td_end());
487 bool td_empty() const { return TargetDepAttrs.empty(); }
489 bool operator==(const AttrBuilder &B);
490 bool operator!=(const AttrBuilder &B) {
491 return !(*this == B);
494 // FIXME: Remove this in 4.0.
496 /// \brief Add the raw value to the internal representation.
497 AttrBuilder &addRawValue(uint64_t Val);
500 namespace AttributeFuncs {
502 /// \brief Which attributes cannot be applied to a type.
503 AttributeSet typeIncompatible(Type *Ty, uint64_t Index);
505 } // end AttributeFuncs namespace
507 } // end llvm namespace