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 //===----------------------------------------------------------------------===//
10 // This file contains the simple types necessary to represent the
11 // attributes associated with functions and their calls.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ATTRIBUTES_H
16 #define LLVM_ATTRIBUTES_H
18 #include "llvm/Support/MathExtras.h"
25 /// Attributes - A bitset of attributes.
28 Attributes() : Bits(0) { }
29 explicit Attributes(uint64_t Val) : Bits(Val) { }
30 Attributes(const Attributes &Attrs) : Bits(Attrs.Bits) { }
31 // This is a "safe bool() operator".
32 operator const void *() const { return Bits ? this : 0; }
33 bool isEmptyOrSingleton() const { return (Bits & (Bits - 1)) == 0; }
34 Attributes &operator = (const Attributes &Attrs) {
38 bool operator == (const Attributes &Attrs) const {
39 return Bits == Attrs.Bits;
41 bool operator != (const Attributes &Attrs) const {
42 return Bits != Attrs.Bits;
44 Attributes operator | (const Attributes &Attrs) const {
45 return Attributes(Bits | Attrs.Bits);
47 Attributes operator & (const Attributes &Attrs) const {
48 return Attributes(Bits & Attrs.Bits);
50 Attributes operator ^ (const Attributes &Attrs) const {
51 return Attributes(Bits ^ Attrs.Bits);
53 Attributes &operator |= (const Attributes &Attrs) {
57 Attributes &operator &= (const Attributes &Attrs) {
61 Attributes operator ~ () const { return Attributes(~Bits); }
62 uint64_t Raw() const { return Bits; }
64 // Currently, we need less than 64 bits.
70 /// Function parameters and results can have attributes to indicate how they
71 /// should be treated by optimizations and code generation. This enumeration
72 /// lists the attributes that can be associated with parameters, function
73 /// results or the function itself.
74 /// @brief Function attributes.
76 const Attributes None (0); ///< No attributes have been set
77 const Attributes ZExt (1<<0); ///< Zero extended before/after call
78 const Attributes SExt (1<<1); ///< Sign extended before/after call
79 const Attributes NoReturn (1<<2); ///< Mark the function as not returning
80 const Attributes InReg (1<<3); ///< Force argument to be passed in register
81 const Attributes StructRet (1<<4); ///< Hidden pointer to structure to return
82 const Attributes NoUnwind (1<<5); ///< Function doesn't unwind stack
83 const Attributes NoAlias (1<<6); ///< Considered to not alias after call
84 const Attributes ByVal (1<<7); ///< Pass structure by value
85 const Attributes Nest (1<<8); ///< Nested function static chain
86 const Attributes ReadNone (1<<9); ///< Function does not access memory
87 const Attributes ReadOnly (1<<10); ///< Function only reads from memory
88 const Attributes NoInline (1<<11); ///< inline=never
89 const Attributes AlwaysInline (1<<12); ///< inline=always
90 const Attributes OptimizeForSize (1<<13); ///< opt_size
91 const Attributes StackProtect (1<<14); ///< Stack protection.
92 const Attributes StackProtectReq (1<<15); ///< Stack protection required.
93 const Attributes Alignment (31<<16); ///< Alignment of parameter (5 bits)
94 // stored as log2 of alignment with +1 bias
95 // 0 means unaligned different from align 1
96 const Attributes NoCapture (1<<21); ///< Function creates no aliases of pointer
97 const Attributes NoRedZone (1<<22); /// disable redzone
98 const Attributes NoImplicitFloat (1<<23); /// disable implicit floating point
100 const Attributes Naked (1<<24); ///< Naked function
101 const Attributes InlineHint (1<<25); ///< source said inlining was
103 const Attributes StackAlignment (7<<26); ///< Alignment of stack for
104 ///function (3 bits) stored as log2
105 ///of alignment with +1 bias
106 ///0 means unaligned (different from
108 const Attributes ReturnsTwice (1<<29); ///< Function can return twice
109 const Attributes UWTable (1<<30); ///< Function must be in a unwind
111 const Attributes NonLazyBind (1U<<31); ///< Function is called early and/or
112 /// often, so lazy binding isn't
114 const Attributes AddressSafety(1ULL<<32); ///< Address safety checking is on.
116 /// Note that uwtable is about the ABI or the user mandating an entry in the
117 /// unwind table. The nounwind attribute is about an exception passing by the
119 /// In a theoretical system that uses tables for profiling and sjlj for
120 /// exceptions, they would be fully independent. In a normal system that
121 /// uses tables for both, the semantics are:
122 /// nil = Needs an entry because an exception might pass by.
123 /// nounwind = No need for an entry
124 /// uwtable = Needs an entry because the ABI says so and because
125 /// an exception might pass by.
126 /// uwtable + nounwind = Needs an entry because the ABI says so.
128 /// @brief Attributes that only apply to function parameters.
129 const Attributes ParameterOnly = ByVal | Nest | StructRet | NoCapture;
131 /// @brief Attributes that may be applied to the function itself. These cannot
132 /// be used on return values or function parameters.
133 const Attributes FunctionOnly = NoReturn | NoUnwind | ReadNone | ReadOnly |
134 NoInline | AlwaysInline | OptimizeForSize | StackProtect | StackProtectReq |
135 NoRedZone | NoImplicitFloat | Naked | InlineHint | StackAlignment |
136 UWTable | NonLazyBind | ReturnsTwice | AddressSafety;
138 /// @brief Parameter attributes that do not apply to vararg call arguments.
139 const Attributes VarArgsIncompatible = StructRet;
141 /// @brief Attributes that are mutually incompatible.
142 const Attributes MutuallyIncompatible[4] = {
143 ByVal | InReg | Nest | StructRet,
146 NoInline | AlwaysInline
149 /// @brief Which attributes cannot be applied to a type.
150 Attributes typeIncompatible(Type *Ty);
152 /// This turns an int alignment (a power of 2, normally) into the
153 /// form used internally in Attributes.
154 inline Attributes constructAlignmentFromInt(unsigned i) {
155 // Default alignment, allow the target to define how to align it.
159 assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
160 assert(i <= 0x40000000 && "Alignment too large.");
161 return Attributes((Log2_32(i)+1) << 16);
164 /// This returns the alignment field of an attribute as a byte alignment value.
165 inline unsigned getAlignmentFromAttrs(Attributes A) {
166 Attributes Align = A & Attribute::Alignment;
170 return 1U << ((Align.Raw() >> 16) - 1);
173 /// This turns an int stack alignment (which must be a power of 2) into
174 /// the form used internally in Attributes.
175 inline Attributes constructStackAlignmentFromInt(unsigned i) {
176 // Default alignment, allow the target to define how to align it.
180 assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
181 assert(i <= 0x100 && "Alignment too large.");
182 return Attributes((Log2_32(i)+1) << 26);
185 /// This returns the stack alignment field of an attribute as a byte alignment
187 inline unsigned getStackAlignmentFromAttrs(Attributes A) {
188 Attributes StackAlign = A & Attribute::StackAlignment;
192 return 1U << ((StackAlign.Raw() >> 26) - 1);
196 /// The set of Attributes set in Attributes is converted to a
197 /// string of equivalent mnemonics. This is, presumably, for writing out
198 /// the mnemonics for the assembly writer.
199 /// @brief Convert attribute bits to text
200 std::string getAsString(Attributes Attrs);
201 } // end namespace Attribute
203 /// This is just a pair of values to associate a set of attributes
205 struct AttributeWithIndex {
206 Attributes Attrs; ///< The attributes that are set, or'd together.
207 unsigned Index; ///< Index of the parameter for which the attributes apply.
208 ///< Index 0 is used for return value attributes.
209 ///< Index ~0U is used for function attributes.
211 static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
212 AttributeWithIndex P;
219 //===----------------------------------------------------------------------===//
220 // AttrListPtr Smart Pointer
221 //===----------------------------------------------------------------------===//
223 class AttributeListImpl;
225 /// AttrListPtr - This class manages the ref count for the opaque
226 /// AttributeListImpl object and provides accessors for it.
228 /// AttrList - The attributes that we are managing. This can be null
229 /// to represent the empty attributes list.
230 AttributeListImpl *AttrList;
232 AttrListPtr() : AttrList(0) {}
233 AttrListPtr(const AttrListPtr &P);
234 const AttrListPtr &operator=(const AttrListPtr &RHS);
237 //===--------------------------------------------------------------------===//
238 // Attribute List Construction and Mutation
239 //===--------------------------------------------------------------------===//
241 /// get - Return a Attributes list with the specified parameter in it.
242 static AttrListPtr get(const AttributeWithIndex *Attr, unsigned NumAttrs);
244 /// get - Return a Attribute list with the parameters specified by the
245 /// consecutive random access iterator range.
246 template <typename Iter>
247 static AttrListPtr get(const Iter &I, const Iter &E) {
248 if (I == E) return AttrListPtr(); // Empty list.
249 return get(&*I, static_cast<unsigned>(E-I));
252 /// addAttr - Add the specified attribute at the specified index to this
253 /// attribute list. Since attribute lists are immutable, this
254 /// returns the new list.
255 AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
257 /// removeAttr - Remove the specified attribute at the specified index from
258 /// this attribute list. Since attribute lists are immutable, this
259 /// returns the new list.
260 AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
262 //===--------------------------------------------------------------------===//
263 // Attribute List Accessors
264 //===--------------------------------------------------------------------===//
265 /// getParamAttributes - The attributes for the specified index are
267 Attributes getParamAttributes(unsigned Idx) const {
268 assert (Idx && Idx != ~0U && "Invalid parameter index!");
269 return getAttributes(Idx);
272 /// getRetAttributes - The attributes for the ret value are
274 Attributes getRetAttributes() const {
275 return getAttributes(0);
278 /// getFnAttributes - The function attributes are returned.
279 Attributes getFnAttributes() const {
280 return getAttributes(~0U);
283 /// paramHasAttr - Return true if the specified parameter index has the
284 /// specified attribute set.
285 bool paramHasAttr(unsigned Idx, Attributes Attr) const {
286 return getAttributes(Idx) & Attr;
289 /// getParamAlignment - Return the alignment for the specified function
291 unsigned getParamAlignment(unsigned Idx) const {
292 return Attribute::getAlignmentFromAttrs(getAttributes(Idx));
295 /// hasAttrSomewhere - Return true if the specified attribute is set for at
296 /// least one parameter or for the return value.
297 bool hasAttrSomewhere(Attributes Attr) const;
299 /// operator==/!= - Provide equality predicates.
300 bool operator==(const AttrListPtr &RHS) const
301 { return AttrList == RHS.AttrList; }
302 bool operator!=(const AttrListPtr &RHS) const
303 { return AttrList != RHS.AttrList; }
307 //===--------------------------------------------------------------------===//
308 // Attribute List Introspection
309 //===--------------------------------------------------------------------===//
311 /// getRawPointer - Return a raw pointer that uniquely identifies this
313 void *getRawPointer() const {
317 // Attributes are stored as a dense set of slots, where there is one
318 // slot for each argument that has an attribute. This allows walking over the
319 // dense set instead of walking the sparse list of attributes.
321 /// isEmpty - Return true if there are no attributes.
323 bool isEmpty() const {
324 return AttrList == 0;
327 /// getNumSlots - Return the number of slots used in this attribute list.
328 /// This is the number of arguments that have an attribute set on them
329 /// (including the function itself).
330 unsigned getNumSlots() const;
332 /// getSlot - Return the AttributeWithIndex at the specified slot. This
333 /// holds a index number plus a set of attributes.
334 const AttributeWithIndex &getSlot(unsigned Slot) const;
337 explicit AttrListPtr(AttributeListImpl *L);
339 /// getAttributes - The attributes for the specified index are
340 /// returned. Attributes for the result are denoted with Idx = 0.
341 Attributes getAttributes(unsigned Idx) const;
345 } // End llvm namespace