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 NoRedZone, ///< Disable redzone
90 NoReturn, ///< Mark the function as not returning
91 NoUnwind, ///< Function doesn't unwind stack
92 OptimizeForSize, ///< opt_size
93 OptimizeNone, ///< Function must not be optimized.
94 ReadNone, ///< Function does not access memory
95 ReadOnly, ///< Function only reads from memory
96 Returned, ///< Return value is always equal to this argument
97 ReturnsTwice, ///< Function can return twice
98 SExt, ///< Sign extended before/after call
99 StackAlignment, ///< Alignment of stack for function (3 bits)
100 ///< stored as log2 of alignment with +1 bias 0
101 ///< means unaligned (different from
103 StackProtect, ///< Stack protection.
104 StackProtectReq, ///< Stack protection required.
105 StackProtectStrong, ///< Strong Stack protection.
106 StructRet, ///< Hidden pointer to structure to return
107 SanitizeAddress, ///< AddressSanitizer is on.
108 SanitizeThread, ///< ThreadSanitizer is on.
109 SanitizeMemory, ///< MemorySanitizer is on.
110 UWTable, ///< Function must be in a unwind table
111 ZExt, ///< Zero extended before/after call
113 EndAttrKinds ///< Sentinal value useful for loops
116 AttributeImpl *pImpl;
117 Attribute(AttributeImpl *A) : pImpl(A) {}
119 Attribute() : pImpl(nullptr) {}
121 //===--------------------------------------------------------------------===//
122 // Attribute Construction
123 //===--------------------------------------------------------------------===//
125 /// \brief Return a uniquified Attribute object.
126 static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
127 static Attribute get(LLVMContext &Context, StringRef Kind,
128 StringRef Val = StringRef());
130 /// \brief Return a uniquified Attribute object that has the specific
132 static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
133 static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
135 //===--------------------------------------------------------------------===//
136 // Attribute Accessors
137 //===--------------------------------------------------------------------===//
139 /// \brief Return true if the attribute is an Attribute::AttrKind type.
140 bool isEnumAttribute() const;
142 /// \brief Return true if the attribute is an alignment attribute.
143 bool isAlignAttribute() const;
145 /// \brief Return true if the attribute is a string (target-dependent)
147 bool isStringAttribute() const;
149 /// \brief Return true if the attribute is present.
150 bool hasAttribute(AttrKind Val) const;
152 /// \brief Return true if the target-dependent attribute is present.
153 bool hasAttribute(StringRef Val) const;
155 /// \brief Return the attribute's kind as an enum (Attribute::AttrKind). This
156 /// requires the attribute to be an enum or alignment attribute.
157 Attribute::AttrKind getKindAsEnum() const;
159 /// \brief Return the attribute's value as an integer. This requires that the
160 /// attribute be an alignment attribute.
161 uint64_t getValueAsInt() const;
163 /// \brief Return the attribute's kind as a string. This requires the
164 /// attribute to be a string attribute.
165 StringRef getKindAsString() const;
167 /// \brief Return the attribute's value as a string. This requires the
168 /// attribute to be a string attribute.
169 StringRef getValueAsString() const;
171 /// \brief Returns the alignment field of an attribute as a byte alignment
173 unsigned getAlignment() const;
175 /// \brief Returns the stack alignment field of an attribute as a byte
177 unsigned getStackAlignment() const;
179 /// \brief The Attribute is converted to a string of equivalent mnemonic. This
180 /// is, presumably, for writing out the mnemonics for the assembly writer.
181 std::string getAsString(bool InAttrGrp = false) const;
183 /// \brief Equality and non-equality operators.
184 bool operator==(Attribute A) const { return pImpl == A.pImpl; }
185 bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
187 /// \brief Less-than operator. Useful for sorting the attributes list.
188 bool operator<(Attribute A) const;
190 void Profile(FoldingSetNodeID &ID) const {
191 ID.AddPointer(pImpl);
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 {
210 friend class AttrBuilder;
211 friend class AttributeSetImpl;
212 template <typename Ty> friend struct DenseMapInfo;
214 /// \brief The attributes that we are managing. This can be null to represent
215 /// the empty attributes list.
216 AttributeSetImpl *pImpl;
218 /// \brief The attributes for the specified index are returned.
219 AttributeSetNode *getAttributes(unsigned Index) const;
221 /// \brief Create an AttributeSet with the specified parameters in it.
222 static AttributeSet get(LLVMContext &C,
223 ArrayRef<std::pair<unsigned, Attribute> > Attrs);
224 static AttributeSet get(LLVMContext &C,
225 ArrayRef<std::pair<unsigned,
226 AttributeSetNode*> > Attrs);
228 static AttributeSet getImpl(LLVMContext &C,
229 ArrayRef<std::pair<unsigned,
230 AttributeSetNode*> > Attrs);
233 explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {}
235 AttributeSet() : pImpl(nullptr) {}
237 //===--------------------------------------------------------------------===//
238 // AttributeSet Construction and Mutation
239 //===--------------------------------------------------------------------===//
241 /// \brief Return an AttributeSet with the specified parameters in it.
242 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs);
243 static AttributeSet get(LLVMContext &C, unsigned Index,
244 ArrayRef<Attribute::AttrKind> Kind);
245 static AttributeSet get(LLVMContext &C, unsigned Index, const AttrBuilder &B);
247 /// \brief Add an attribute to the attribute set at the given index. Since
248 /// attribute sets are immutable, this returns a new set.
249 AttributeSet addAttribute(LLVMContext &C, unsigned Index,
250 Attribute::AttrKind Attr) const;
252 /// \brief Add an attribute to the attribute set at the given index. Since
253 /// attribute sets are immutable, this returns a new set.
254 AttributeSet addAttribute(LLVMContext &C, unsigned Index,
255 StringRef Kind) const;
256 AttributeSet addAttribute(LLVMContext &C, unsigned Index,
257 StringRef Kind, StringRef Value) const;
259 /// \brief Add attributes to the attribute set at the given index. Since
260 /// attribute sets are immutable, this returns a new set.
261 AttributeSet addAttributes(LLVMContext &C, unsigned Index,
262 AttributeSet Attrs) const;
264 /// \brief Remove the specified attribute at the specified index from this
265 /// attribute list. Since attribute lists are immutable, this returns the new
267 AttributeSet removeAttribute(LLVMContext &C, unsigned Index,
268 Attribute::AttrKind Attr) const;
270 /// \brief Remove the specified attributes at the specified index from this
271 /// attribute list. Since attribute lists are immutable, this returns the new
273 AttributeSet removeAttributes(LLVMContext &C, unsigned Index,
274 AttributeSet Attrs) const;
276 //===--------------------------------------------------------------------===//
277 // AttributeSet Accessors
278 //===--------------------------------------------------------------------===//
280 /// \brief Retrieve the LLVM context.
281 LLVMContext &getContext() const;
283 /// \brief The attributes for the specified index are returned.
284 AttributeSet getParamAttributes(unsigned Index) const;
286 /// \brief The attributes for the ret value are returned.
287 AttributeSet getRetAttributes() const;
289 /// \brief The function attributes are returned.
290 AttributeSet getFnAttributes() const;
292 /// \brief Return true if the attribute exists at the given index.
293 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
295 /// \brief Return true if the attribute exists at the given index.
296 bool hasAttribute(unsigned Index, StringRef Kind) const;
298 /// \brief Return true if attribute exists at the given index.
299 bool hasAttributes(unsigned Index) const;
301 /// \brief Return true if the specified attribute is set for at least one
302 /// parameter or for the return value.
303 bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
305 /// \brief Return the attribute object that exists at the given index.
306 Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
308 /// \brief Return the attribute object that exists at the given index.
309 Attribute getAttribute(unsigned Index, StringRef Kind) const;
311 /// \brief Return the alignment for the specified function parameter.
312 unsigned getParamAlignment(unsigned Index) const;
314 /// \brief Get the stack alignment.
315 unsigned getStackAlignment(unsigned Index) const;
317 /// \brief Return the attributes at the index as a string.
318 std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
320 typedef ArrayRef<Attribute>::iterator iterator;
322 iterator begin(unsigned Slot) const;
323 iterator end(unsigned Slot) const;
325 /// operator==/!= - Provide equality predicates.
326 bool operator==(const AttributeSet &RHS) const {
327 return pImpl == RHS.pImpl;
329 bool operator!=(const AttributeSet &RHS) const {
330 return pImpl != RHS.pImpl;
333 //===--------------------------------------------------------------------===//
334 // AttributeSet Introspection
335 //===--------------------------------------------------------------------===//
337 // FIXME: Remove this.
338 uint64_t Raw(unsigned Index) const;
340 /// \brief Return a raw pointer that uniquely identifies this attribute list.
341 void *getRawPointer() const {
345 /// \brief Return true if there are no attributes.
346 bool isEmpty() const {
347 return getNumSlots() == 0;
350 /// \brief Return the number of slots used in this attribute list. This is
351 /// the number of arguments that have an attribute set on them (including the
352 /// function itself).
353 unsigned getNumSlots() const;
355 /// \brief Return the index for the given slot.
356 unsigned getSlotIndex(unsigned Slot) const;
358 /// \brief Return the attributes at the given slot.
359 AttributeSet getSlotAttributes(unsigned Slot) const;
364 //===----------------------------------------------------------------------===//
366 /// \brief Provide DenseMapInfo for AttributeSet.
367 template<> struct DenseMapInfo<AttributeSet> {
368 static inline AttributeSet getEmptyKey() {
369 uintptr_t Val = static_cast<uintptr_t>(-1);
370 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
371 return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
373 static inline AttributeSet getTombstoneKey() {
374 uintptr_t Val = static_cast<uintptr_t>(-2);
375 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
376 return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
378 static unsigned getHashValue(AttributeSet AS) {
379 return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
380 (unsigned((uintptr_t)AS.pImpl) >> 9);
382 static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
385 //===----------------------------------------------------------------------===//
387 /// \brief This class is used in conjunction with the Attribute::get method to
388 /// create an Attribute object. The object itself is uniquified. The Builder's
389 /// value, however, is not. So this can be used as a quick way to test for
390 /// equality, presence of attributes, etc.
392 std::bitset<Attribute::EndAttrKinds> Attrs;
393 std::map<std::string, std::string> TargetDepAttrs;
395 uint64_t StackAlignment;
397 AttrBuilder() : Attrs(0), Alignment(0), StackAlignment(0) {}
398 explicit AttrBuilder(uint64_t Val)
399 : Attrs(0), Alignment(0), StackAlignment(0) {
402 AttrBuilder(const Attribute &A) : Attrs(0), Alignment(0), StackAlignment(0) {
405 AttrBuilder(AttributeSet AS, unsigned Idx);
409 /// \brief Add an attribute to the builder.
410 AttrBuilder &addAttribute(Attribute::AttrKind Val);
412 /// \brief Add the Attribute object to the builder.
413 AttrBuilder &addAttribute(Attribute A);
415 /// \brief Add the target-dependent attribute to the builder.
416 AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
418 /// \brief Remove an attribute from the builder.
419 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
421 /// \brief Remove the attributes from the builder.
422 AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index);
424 /// \brief Remove the target-dependent attribute to the builder.
425 AttrBuilder &removeAttribute(StringRef A);
427 /// \brief Add the attributes from the builder.
428 AttrBuilder &merge(const AttrBuilder &B);
430 /// \brief Return true if the builder has the specified attribute.
431 bool contains(Attribute::AttrKind A) const {
432 assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
436 /// \brief Return true if the builder has the specified target-dependent
438 bool contains(StringRef A) const;
440 /// \brief Return true if the builder has IR-level attributes.
441 bool hasAttributes() const;
443 /// \brief Return true if the builder has any attribute that's in the
444 /// specified attribute.
445 bool hasAttributes(AttributeSet A, uint64_t Index) const;
447 /// \brief Return true if the builder has an alignment attribute.
448 bool hasAlignmentAttr() const;
450 /// \brief Retrieve the alignment attribute, if it exists.
451 uint64_t getAlignment() const { return Alignment; }
453 /// \brief Retrieve the stack alignment attribute, if it exists.
454 uint64_t getStackAlignment() const { return StackAlignment; }
456 /// \brief This turns an int alignment (which must be a power of 2) into the
457 /// form used internally in Attribute.
458 AttrBuilder &addAlignmentAttr(unsigned Align);
460 /// \brief This turns an int stack alignment (which must be a power of 2) into
461 /// the form used internally in Attribute.
462 AttrBuilder &addStackAlignmentAttr(unsigned Align);
464 /// \brief Return true if the builder contains no target-independent
466 bool empty() const { return Attrs.none(); }
468 // Iterators for target-dependent attributes.
469 typedef std::pair<std::string, std::string> td_type;
470 typedef std::map<std::string, std::string>::iterator td_iterator;
471 typedef std::map<std::string, std::string>::const_iterator td_const_iterator;
472 typedef llvm::iterator_range<td_iterator> td_range;
473 typedef llvm::iterator_range<td_const_iterator> td_const_range;
475 td_iterator td_begin() { return TargetDepAttrs.begin(); }
476 td_iterator td_end() { return TargetDepAttrs.end(); }
478 td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
479 td_const_iterator td_end() const { return TargetDepAttrs.end(); }
481 td_range td_attrs() { return td_range(td_begin(), td_end()); }
482 td_const_range td_attrs() const {
483 return td_const_range(td_begin(), td_end());
486 bool td_empty() const { return TargetDepAttrs.empty(); }
488 bool operator==(const AttrBuilder &B);
489 bool operator!=(const AttrBuilder &B) {
490 return !(*this == B);
493 // FIXME: Remove this in 4.0.
495 /// \brief Add the raw value to the internal representation.
496 AttrBuilder &addRawValue(uint64_t Val);
499 namespace AttributeFuncs {
501 /// \brief Which attributes cannot be applied to a type.
502 AttributeSet typeIncompatible(Type *Ty, uint64_t Index);
504 } // end AttributeFuncs namespace
506 } // end llvm namespace