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.
26 typedef unsigned Attributes;
30 /// Function parameters and results can have attributes to indicate how they
31 /// should be treated by optimizations and code generation. This enumeration
32 /// lists the attributes that can be associated with parameters, function
33 /// results or the function itself.
34 /// @brief Function attributes.
36 const Attributes None = 0; ///< No attributes have been set
37 const Attributes ZExt = 1<<0; ///< Zero extended before/after call
38 const Attributes SExt = 1<<1; ///< Sign extended before/after call
39 const Attributes NoReturn = 1<<2; ///< Mark the function as not returning
40 const Attributes InReg = 1<<3; ///< Force argument to be passed in register
41 const Attributes StructRet = 1<<4; ///< Hidden pointer to structure to return
42 const Attributes NoUnwind = 1<<5; ///< Function doesn't unwind stack
43 const Attributes NoAlias = 1<<6; ///< Considered to not alias after call
44 const Attributes ByVal = 1<<7; ///< Pass structure by value
45 const Attributes Nest = 1<<8; ///< Nested function static chain
46 const Attributes ReadNone = 1<<9; ///< Function does not access memory
47 const Attributes ReadOnly = 1<<10; ///< Function only reads from memory
48 const Attributes NoInline = 1<<11; ///< inline=never
49 const Attributes AlwaysInline = 1<<12; ///< inline=always
50 const Attributes OptimizeForSize = 1<<13; ///< opt_size
51 const Attributes StackProtect = 1<<14; ///< Stack protection.
52 const Attributes StackProtectReq = 1<<15; ///< Stack protection required.
53 const Attributes Alignment = 31<<16; ///< Alignment of parameter (5 bits)
54 // stored as log2 of alignment with +1 bias
55 // 0 means unaligned different from align 1
56 const Attributes NoCapture = 1<<21; ///< Function creates no aliases of pointer
57 const Attributes NoRedZone = 1<<22; /// disable redzone
58 const Attributes NoImplicitFloat = 1<<23; /// disable implicit floating point
60 const Attributes Naked = 1<<24; ///< Naked function
61 const Attributes InlineHint = 1<<25; ///< source said inlining was
63 const Attributes StackAlignment = 7<<26; ///< Alignment of stack for
64 ///function (3 bits) stored as log2
65 ///of alignment with +1 bias
66 ///0 means unaligned (different from
68 const Attributes ReturnsTwice = 1<<29; ///< Function can return twice
69 const Attributes UWTable = 1<<30; ///< Function must be in a unwind
71 const Attributes NonLazyBind = 1U<<31; ///< Function is called early and/or
72 /// often, so lazy binding isn't
75 /// Note that uwtable is about the ABI or the user mandating an entry in the
76 /// unwind table. The nounwind attribute is about an exception passing by the
78 /// In a theoretical system that uses tables for profiling and sjlj for
79 /// exceptions, they would be fully independent. In a normal system that
80 /// uses tables for both, the semantics are:
81 /// nil = Needs an entry because an exception might pass by.
82 /// nounwind = No need for an entry
83 /// uwtable = Needs an entry because the ABI says so and because
84 /// an exception might pass by.
85 /// uwtable + nounwind = Needs an entry because the ABI says so.
87 /// @brief Attributes that only apply to function parameters.
88 const Attributes ParameterOnly = ByVal | Nest | StructRet | NoCapture;
90 /// @brief Attributes that may be applied to the function itself. These cannot
91 /// be used on return values or function parameters.
92 const Attributes FunctionOnly = NoReturn | NoUnwind | ReadNone | ReadOnly |
93 NoInline | AlwaysInline | OptimizeForSize | StackProtect | StackProtectReq |
94 NoRedZone | NoImplicitFloat | Naked | InlineHint | StackAlignment |
95 UWTable | NonLazyBind | ReturnsTwice;
97 /// @brief Parameter attributes that do not apply to vararg call arguments.
98 const Attributes VarArgsIncompatible = StructRet;
100 /// @brief Attributes that are mutually incompatible.
101 const Attributes MutuallyIncompatible[4] = {
102 ByVal | InReg | Nest | StructRet,
105 NoInline | AlwaysInline
108 /// @brief Which attributes cannot be applied to a type.
109 Attributes typeIncompatible(Type *Ty);
111 /// This turns an int alignment (a power of 2, normally) into the
112 /// form used internally in Attributes.
113 inline Attributes constructAlignmentFromInt(unsigned i) {
114 // Default alignment, allow the target to define how to align it.
118 assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
119 assert(i <= 0x40000000 && "Alignment too large.");
120 return (Log2_32(i)+1) << 16;
123 /// This returns the alignment field of an attribute as a byte alignment value.
124 inline unsigned getAlignmentFromAttrs(Attributes A) {
125 Attributes Align = A & Attribute::Alignment;
129 return 1U << ((Align >> 16) - 1);
132 /// This turns an int stack alignment (which must be a power of 2) into
133 /// the form used internally in Attributes.
134 inline Attributes constructStackAlignmentFromInt(unsigned i) {
135 // Default alignment, allow the target to define how to align it.
139 assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
140 assert(i <= 0x100 && "Alignment too large.");
141 return (Log2_32(i)+1) << 26;
144 /// This returns the stack alignment field of an attribute as a byte alignment
146 inline unsigned getStackAlignmentFromAttrs(Attributes A) {
147 Attributes StackAlign = A & Attribute::StackAlignment;
151 return 1U << ((StackAlign >> 26) - 1);
155 /// The set of Attributes set in Attributes is converted to a
156 /// string of equivalent mnemonics. This is, presumably, for writing out
157 /// the mnemonics for the assembly writer.
158 /// @brief Convert attribute bits to text
159 std::string getAsString(Attributes Attrs);
160 } // end namespace Attribute
162 /// This is just a pair of values to associate a set of attributes
164 struct AttributeWithIndex {
165 Attributes Attrs; ///< The attributes that are set, or'd together.
166 unsigned Index; ///< Index of the parameter for which the attributes apply.
167 ///< Index 0 is used for return value attributes.
168 ///< Index ~0U is used for function attributes.
170 static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
171 AttributeWithIndex P;
178 //===----------------------------------------------------------------------===//
179 // AttrListPtr Smart Pointer
180 //===----------------------------------------------------------------------===//
182 class AttributeListImpl;
184 /// AttrListPtr - This class manages the ref count for the opaque
185 /// AttributeListImpl object and provides accessors for it.
187 /// AttrList - The attributes that we are managing. This can be null
188 /// to represent the empty attributes list.
189 AttributeListImpl *AttrList;
191 AttrListPtr() : AttrList(0) {}
192 AttrListPtr(const AttrListPtr &P);
193 const AttrListPtr &operator=(const AttrListPtr &RHS);
196 //===--------------------------------------------------------------------===//
197 // Attribute List Construction and Mutation
198 //===--------------------------------------------------------------------===//
200 /// get - Return a Attributes list with the specified parameter in it.
201 static AttrListPtr get(const AttributeWithIndex *Attr, unsigned NumAttrs);
203 /// get - Return a Attribute list with the parameters specified by the
204 /// consecutive random access iterator range.
205 template <typename Iter>
206 static AttrListPtr get(const Iter &I, const Iter &E) {
207 if (I == E) return AttrListPtr(); // Empty list.
208 return get(&*I, static_cast<unsigned>(E-I));
211 /// addAttr - Add the specified attribute at the specified index to this
212 /// attribute list. Since attribute lists are immutable, this
213 /// returns the new list.
214 AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
216 /// removeAttr - Remove the specified attribute at the specified index from
217 /// this attribute list. Since attribute lists are immutable, this
218 /// returns the new list.
219 AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
221 //===--------------------------------------------------------------------===//
222 // Attribute List Accessors
223 //===--------------------------------------------------------------------===//
224 /// getParamAttributes - The attributes for the specified index are
226 Attributes getParamAttributes(unsigned Idx) const {
227 assert (Idx && Idx != ~0U && "Invalid parameter index!");
228 return getAttributes(Idx);
231 /// getRetAttributes - The attributes for the ret value are
233 Attributes getRetAttributes() const {
234 return getAttributes(0);
237 /// getFnAttributes - The function attributes are returned.
238 Attributes getFnAttributes() const {
239 return getAttributes(~0U);
242 /// paramHasAttr - Return true if the specified parameter index has the
243 /// specified attribute set.
244 bool paramHasAttr(unsigned Idx, Attributes Attr) const {
245 return (getAttributes(Idx) & Attr) != 0;
248 /// getParamAlignment - Return the alignment for the specified function
250 unsigned getParamAlignment(unsigned Idx) const {
251 return Attribute::getAlignmentFromAttrs(getAttributes(Idx));
254 /// hasAttrSomewhere - Return true if the specified attribute is set for at
255 /// least one parameter or for the return value.
256 bool hasAttrSomewhere(Attributes Attr) const;
258 /// operator==/!= - Provide equality predicates.
259 bool operator==(const AttrListPtr &RHS) const
260 { return AttrList == RHS.AttrList; }
261 bool operator!=(const AttrListPtr &RHS) const
262 { return AttrList != RHS.AttrList; }
266 //===--------------------------------------------------------------------===//
267 // Attribute List Introspection
268 //===--------------------------------------------------------------------===//
270 /// getRawPointer - Return a raw pointer that uniquely identifies this
272 void *getRawPointer() const {
276 // Attributes are stored as a dense set of slots, where there is one
277 // slot for each argument that has an attribute. This allows walking over the
278 // dense set instead of walking the sparse list of attributes.
280 /// isEmpty - Return true if there are no attributes.
282 bool isEmpty() const {
283 return AttrList == 0;
286 /// getNumSlots - Return the number of slots used in this attribute list.
287 /// This is the number of arguments that have an attribute set on them
288 /// (including the function itself).
289 unsigned getNumSlots() const;
291 /// getSlot - Return the AttributeWithIndex at the specified slot. This
292 /// holds a index number plus a set of attributes.
293 const AttributeWithIndex &getSlot(unsigned Slot) const;
296 explicit AttrListPtr(AttributeListImpl *L);
298 /// getAttributes - The attributes for the specified index are
299 /// returned. Attributes for the result are denoted with Idx = 0.
300 Attributes getAttributes(unsigned Idx) const;
304 } // End llvm namespace