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/DenseSet.h"
21 #include "llvm/ADT/FoldingSet.h"
30 class AttributeSetImpl;
31 class AttributeSetNode;
36 //===----------------------------------------------------------------------===//
38 /// \brief Functions, function parameters, and return types can have attributes
39 /// to indicate how they should be treated by optimizations and code
40 /// generation. This class represents one of those attributes. It's light-weight
41 /// and should be passed around by-value.
44 /// This enumeration lists the attributes that can be associated with
45 /// parameters, function results, or the function itself.
47 /// Note: The `uwtable' attribute is about the ABI or the user mandating an
48 /// entry in the unwind table. The `nounwind' attribute is about an exception
49 /// passing by the function.
51 /// In a theoretical system that uses tables for profiling and SjLj for
52 /// exceptions, they would be fully independent. In a normal system that uses
53 /// tables for both, the semantics are:
55 /// nil = Needs an entry because an exception might pass by.
56 /// nounwind = No need for an entry
57 /// uwtable = Needs an entry because the ABI says so and because
58 /// an exception might pass by.
59 /// uwtable + nounwind = Needs an entry because the ABI says so.
62 // IR-Level Attributes
63 None, ///< No attributes have been set
64 AddressSafety, ///< Address safety checking is on.
65 Alignment, ///< Alignment of parameter (5 bits)
66 ///< stored as log2 of alignment with +1 bias
67 ///< 0 means unaligned (different from align(1))
68 AlwaysInline, ///< inline=always
69 ByVal, ///< Pass structure by value
70 InlineHint, ///< Source said inlining was desirable
71 InReg, ///< Force argument to be passed in register
72 MinSize, ///< Function must be optimized for size first
73 Naked, ///< Naked function
74 Nest, ///< Nested function static chain
75 NoAlias, ///< Considered to not alias after call
76 NoCapture, ///< Function creates no aliases of pointer
77 NoDuplicate, ///< Call cannot be duplicated
78 NoImplicitFloat, ///< Disable implicit floating point insts
79 NoInline, ///< inline=never
80 NonLazyBind, ///< Function is called early and/or
81 ///< often, so lazy binding isn't worthwhile
82 NoRedZone, ///< Disable redzone
83 NoReturn, ///< Mark the function as not returning
84 NoUnwind, ///< Function doesn't unwind stack
85 OptimizeForSize, ///< opt_size
86 ReadNone, ///< Function does not access memory
87 ReadOnly, ///< Function only reads from memory
88 ReturnsTwice, ///< Function can return twice
89 SExt, ///< Sign extended before/after call
90 StackAlignment, ///< Alignment of stack for function (3 bits)
91 ///< stored as log2 of alignment with +1 bias 0
92 ///< means unaligned (different from
94 StackProtect, ///< Stack protection.
95 StackProtectReq, ///< Stack protection required.
96 StackProtectStrong, ///< Strong Stack protection.
97 StructRet, ///< Hidden pointer to structure to return
98 UWTable, ///< Function must be in a unwind table
99 ZExt, ///< Zero extended before/after call
101 EndAttrKinds, ///< Sentinal value useful for loops
103 AttrKindEmptyKey, ///< Empty key value for DenseMapInfo
104 AttrKindTombstoneKey ///< Tombstone key value for DenseMapInfo
107 AttributeImpl *pImpl;
108 Attribute(AttributeImpl *A) : pImpl(A) {}
110 Attribute() : pImpl(0) {}
112 //===--------------------------------------------------------------------===//
113 // Attribute Construction
114 //===--------------------------------------------------------------------===//
116 /// \brief Return a uniquified Attribute object.
117 static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
118 static Attribute get(LLVMContext &Context, StringRef Kind,
119 StringRef Val = StringRef());
121 /// \brief Return a uniquified Attribute object that has the specific
123 static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
124 static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
126 //===--------------------------------------------------------------------===//
127 // Attribute Accessors
128 //===--------------------------------------------------------------------===//
130 /// \brief Return true if the attribute is an Attribute::AttrKind type.
131 bool isEnumAttribute() const;
133 /// \brief Return true if the attribute is an alignment attribute.
134 bool isAlignAttribute() const;
136 /// \brief Return true if the attribute is a string (target-dependent)
138 bool isStringAttribute() const;
140 /// \brief Return true if the attribute is present.
141 bool hasAttribute(AttrKind Val) const;
143 /// \brief Return true if the target-dependent attribute is present.
144 bool hasAttribute(StringRef Val) const;
146 /// \brief Return the attribute's kind as an enum (Attribute::AttrKind). This
147 /// requires the attribute to be an enum or alignment attribute.
148 Attribute::AttrKind getKindAsEnum() const;
150 /// \brief Return the attribute's value as an integer. This requires that the
151 /// attribute be an alignment attribute.
152 uint64_t getValueAsInt() const;
154 /// \brief Return the attribute's kind as a string. This requires the
155 /// attribute to be a string attribute.
156 StringRef getKindAsString() const;
158 /// \brief Return the attribute's value as a string. This requires the
159 /// attribute to be a string attribute.
160 StringRef getValueAsString() const;
162 /// \brief Returns the alignment field of an attribute as a byte alignment
164 unsigned getAlignment() const;
166 /// \brief Returns the stack alignment field of an attribute as a byte
168 unsigned getStackAlignment() const;
170 /// \brief The Attribute is converted to a string of equivalent mnemonic. This
171 /// is, presumably, for writing out the mnemonics for the assembly writer.
172 std::string getAsString() const;
174 /// \brief Equality and non-equality operators.
175 bool operator==(Attribute A) const { return pImpl == A.pImpl; }
176 bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
178 /// \brief Less-than operator. Useful for sorting the attributes list.
179 bool operator<(Attribute A) const;
181 void Profile(FoldingSetNodeID &ID) const {
182 ID.AddPointer(pImpl);
186 //===----------------------------------------------------------------------===//
188 /// \brief This class holds the attributes for a function, its return value, and
189 /// its parameters. You access the attributes for each of them via an index into
190 /// the AttributeSet object. The function attributes are at index
191 /// `AttributeSet::FunctionIndex', the return value is at index
192 /// `AttributeSet::ReturnIndex', and the attributes for the parameters start at
201 friend class AttrBuilder;
202 friend class AttributeSetImpl;
204 /// \brief The attributes that we are managing. This can be null to represent
205 /// the empty attributes list.
206 AttributeSetImpl *pImpl;
208 /// \brief The attributes for the specified index are returned.
209 AttributeSetNode *getAttributes(unsigned Idx) const;
211 /// \brief Create an AttributeSet with the specified parameters in it.
212 static AttributeSet get(LLVMContext &C,
213 ArrayRef<std::pair<unsigned, Attribute> > Attrs);
214 static AttributeSet get(LLVMContext &C,
215 ArrayRef<std::pair<unsigned,
216 AttributeSetNode*> > Attrs);
218 static AttributeSet getImpl(LLVMContext &C,
219 ArrayRef<std::pair<unsigned,
220 AttributeSetNode*> > Attrs);
223 explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {}
225 AttributeSet() : pImpl(0) {}
226 AttributeSet(const AttributeSet &P) : pImpl(P.pImpl) {}
227 const AttributeSet &operator=(const AttributeSet &RHS) {
232 //===--------------------------------------------------------------------===//
233 // AttributeSet Construction and Mutation
234 //===--------------------------------------------------------------------===//
236 /// \brief Return an AttributeSet with the specified parameters in it.
237 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs);
238 static AttributeSet get(LLVMContext &C, unsigned Idx,
239 ArrayRef<Attribute::AttrKind> Kind);
240 static AttributeSet get(LLVMContext &C, unsigned Idx, AttrBuilder &B);
242 /// \brief Add an attribute to the attribute set at the given index. Since
243 /// attribute sets are immutable, this returns a new set.
244 AttributeSet addAttribute(LLVMContext &C, unsigned Idx,
245 Attribute::AttrKind Attr) const;
247 /// \brief Add attributes to the attribute set at the given index. Since
248 /// attribute sets are immutable, this returns a new set.
249 AttributeSet addAttributes(LLVMContext &C, unsigned Idx,
250 AttributeSet Attrs) const;
252 /// \brief Remove the specified attribute at the specified index from this
253 /// attribute list. Since attribute lists are immutable, this returns the new
255 AttributeSet removeAttribute(LLVMContext &C, unsigned Idx,
256 Attribute::AttrKind Attr) const;
258 /// \brief Remove the specified attributes at the specified index from this
259 /// attribute list. Since attribute lists are immutable, this returns the new
261 AttributeSet removeAttributes(LLVMContext &C, unsigned Idx,
262 AttributeSet Attrs) const;
264 //===--------------------------------------------------------------------===//
265 // AttributeSet Accessors
266 //===--------------------------------------------------------------------===//
268 /// \brief The attributes for the specified index are returned.
269 AttributeSet getParamAttributes(unsigned Idx) const;
271 /// \brief The attributes for the ret value are returned.
272 AttributeSet getRetAttributes() const;
274 /// \brief The function attributes are returned.
275 AttributeSet getFnAttributes() const;
277 /// \brief Return true if the attribute exists at the given index.
278 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
280 /// \brief Return true if attribute exists at the given index.
281 bool hasAttributes(unsigned Index) const;
283 /// \brief Return true if the specified attribute is set for at least one
284 /// parameter or for the return value.
285 bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
287 /// \brief Return the alignment for the specified function parameter.
288 unsigned getParamAlignment(unsigned Idx) const;
290 /// \brief Get the stack alignment.
291 unsigned getStackAlignment(unsigned Index) const;
293 /// \brief Return the attributes at the index as a string.
294 std::string getAsString(unsigned Index) const;
296 typedef ArrayRef<Attribute>::iterator iterator;
298 iterator begin(unsigned Idx) const;
299 iterator end(unsigned Idx) const;
301 /// operator==/!= - Provide equality predicates.
302 bool operator==(const AttributeSet &RHS) const {
303 return pImpl == RHS.pImpl;
305 bool operator!=(const AttributeSet &RHS) const {
306 return pImpl != RHS.pImpl;
309 //===--------------------------------------------------------------------===//
310 // AttributeSet Introspection
311 //===--------------------------------------------------------------------===//
313 // FIXME: Remove this.
314 uint64_t Raw(unsigned Index) const;
316 /// \brief Return a raw pointer that uniquely identifies this attribute list.
317 void *getRawPointer() const {
321 /// \brief Return true if there are no attributes.
322 bool isEmpty() const {
323 return getNumSlots() == 0;
326 /// \brief Return the number of slots used in this attribute list. This is
327 /// the number of arguments that have an attribute set on them (including the
328 /// function itself).
329 unsigned getNumSlots() const;
331 /// \brief Return the index for the given slot.
332 uint64_t getSlotIndex(unsigned Slot) const;
334 /// \brief Return the attributes at the given slot.
335 AttributeSet getSlotAttributes(unsigned Slot) const;
340 //===----------------------------------------------------------------------===//
342 /// \brief Provide DenseMapInfo for Attribute::AttrKinds. This is used by
344 template<> struct DenseMapInfo<Attribute::AttrKind> {
345 static inline Attribute::AttrKind getEmptyKey() {
346 return Attribute::AttrKindEmptyKey;
348 static inline Attribute::AttrKind getTombstoneKey() {
349 return Attribute::AttrKindTombstoneKey;
351 static unsigned getHashValue(const Attribute::AttrKind &Val) {
354 static bool isEqual(const Attribute::AttrKind &LHS,
355 const Attribute::AttrKind &RHS) {
360 //===----------------------------------------------------------------------===//
362 /// \brief This class is used in conjunction with the Attribute::get method to
363 /// create an Attribute object. The object itself is uniquified. The Builder's
364 /// value, however, is not. So this can be used as a quick way to test for
365 /// equality, presence of attributes, etc.
367 DenseSet<Attribute::AttrKind> Attrs;
368 std::map<std::string, std::string> TargetDepAttrs;
370 uint64_t StackAlignment;
372 AttrBuilder() : Alignment(0), StackAlignment(0) {}
373 explicit AttrBuilder(uint64_t Val) : Alignment(0), StackAlignment(0) {
376 AttrBuilder(const Attribute &A) : Alignment(0), StackAlignment(0) {
379 AttrBuilder(AttributeSet AS, unsigned Idx);
380 AttrBuilder(const AttrBuilder &B)
382 TargetDepAttrs(B.TargetDepAttrs.begin(), B.TargetDepAttrs.end()),
383 Alignment(B.Alignment), StackAlignment(B.StackAlignment) {}
387 /// \brief Add an attribute to the builder.
388 AttrBuilder &addAttribute(Attribute::AttrKind Val);
390 /// \brief Add the Attribute object to the builder.
391 AttrBuilder &addAttribute(Attribute A);
393 /// \brief Add the target-dependent attribute to the builder.
394 AttrBuilder &addAttribute(StringRef A, StringRef V);
396 /// \brief Remove an attribute from the builder.
397 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
399 /// \brief Remove the attributes from the builder.
400 AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index);
402 /// \brief Remove the target-dependent attribute to the builder.
403 AttrBuilder &removeAttribute(StringRef A);
405 /// \brief Add the attributes from the builder.
406 AttrBuilder &merge(const AttrBuilder &B);
408 /// \brief Return true if the builder has the specified attribute.
409 bool contains(Attribute::AttrKind A) const;
411 /// \brief Return true if the builder has the specified target-dependent
413 bool contains(StringRef A) const;
415 /// \brief Return true if the builder has IR-level attributes.
416 bool hasAttributes() const;
418 /// \brief Return true if the builder has any attribute that's in the
419 /// specified attribute.
420 bool hasAttributes(AttributeSet A, uint64_t Index) const;
422 /// \brief Return true if the builder has an alignment attribute.
423 bool hasAlignmentAttr() const;
425 /// \brief Retrieve the alignment attribute, if it exists.
426 uint64_t getAlignment() const { return Alignment; }
428 /// \brief Retrieve the stack alignment attribute, if it exists.
429 uint64_t getStackAlignment() const { return StackAlignment; }
431 /// \brief This turns an int alignment (which must be a power of 2) into the
432 /// form used internally in Attribute.
433 AttrBuilder &addAlignmentAttr(unsigned Align);
435 /// \brief This turns an int stack alignment (which must be a power of 2) into
436 /// the form used internally in Attribute.
437 AttrBuilder &addStackAlignmentAttr(unsigned Align);
439 // Iterators for target-independent attributes.
440 typedef DenseSet<Attribute::AttrKind>::iterator iterator;
441 typedef DenseSet<Attribute::AttrKind>::const_iterator const_iterator;
443 iterator begin() { return Attrs.begin(); }
444 iterator end() { return Attrs.end(); }
446 const_iterator begin() const { return Attrs.begin(); }
447 const_iterator end() const { return Attrs.end(); }
449 // Iterators for target-dependent attributes.
450 typedef std::pair<std::string, std::string> td_type;
451 typedef std::map<std::string, std::string>::iterator td_iterator;
452 typedef std::map<std::string, std::string>::const_iterator td_const_iterator;
454 td_iterator td_begin() { return TargetDepAttrs.begin(); }
455 td_iterator td_end() { return TargetDepAttrs.end(); }
457 td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
458 td_const_iterator td_end() const { return TargetDepAttrs.end(); }
460 /// \brief Remove attributes that are used on functions only.
461 void removeFunctionOnlyAttrs() {
462 removeAttribute(Attribute::NoReturn)
463 .removeAttribute(Attribute::NoUnwind)
464 .removeAttribute(Attribute::ReadNone)
465 .removeAttribute(Attribute::ReadOnly)
466 .removeAttribute(Attribute::NoInline)
467 .removeAttribute(Attribute::AlwaysInline)
468 .removeAttribute(Attribute::OptimizeForSize)
469 .removeAttribute(Attribute::StackProtect)
470 .removeAttribute(Attribute::StackProtectReq)
471 .removeAttribute(Attribute::StackProtectStrong)
472 .removeAttribute(Attribute::NoRedZone)
473 .removeAttribute(Attribute::NoImplicitFloat)
474 .removeAttribute(Attribute::Naked)
475 .removeAttribute(Attribute::InlineHint)
476 .removeAttribute(Attribute::StackAlignment)
477 .removeAttribute(Attribute::UWTable)
478 .removeAttribute(Attribute::NonLazyBind)
479 .removeAttribute(Attribute::ReturnsTwice)
480 .removeAttribute(Attribute::AddressSafety)
481 .removeAttribute(Attribute::MinSize)
482 .removeAttribute(Attribute::NoDuplicate);
485 bool operator==(const AttrBuilder &B);
486 bool operator!=(const AttrBuilder &B) {
487 return !(*this == B);
490 // FIXME: Remove this in 4.0.
492 /// \brief Add the raw value to the internal representation.
493 AttrBuilder &addRawValue(uint64_t Val);
496 namespace AttributeFuncs {
498 /// \brief Which attributes cannot be applied to a type.
499 AttributeSet typeIncompatible(Type *Ty, uint64_t Index);
501 } // end AttributeFuncs namespace
503 } // end llvm namespace