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.
55 const Attributes NoCapture = 1<<21; ///< Function creates no aliases of pointer
57 /// @brief Attributes that only apply to function parameters.
58 const Attributes ParameterOnly = ByVal | Nest | StructRet | NoCapture;
60 /// @brief Attributes that only apply to function.
61 const Attributes FunctionOnly = NoReturn | NoUnwind | ReadNone | ReadOnly |
62 NoInline | AlwaysInline | OptimizeForSize | StackProtect | StackProtectReq;
64 /// @brief Parameter attributes that do not apply to vararg call arguments.
65 const Attributes VarArgsIncompatible = StructRet;
67 /// @brief Attributes that are mutually incompatible.
68 const Attributes MutuallyIncompatible[4] = {
69 ByVal | InReg | Nest | StructRet,
72 NoInline | AlwaysInline
75 /// @brief Which attributes cannot be applied to a type.
76 Attributes typeIncompatible(const Type *Ty);
78 /// This turns an int alignment (a power of 2, normally) into the
79 /// form used internally in Attributes.
80 inline Attributes constructAlignmentFromInt(unsigned i) {
81 assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
82 return Log2_32(i) << 16;
85 /// The set of Attributes set in Attributes is converted to a
86 /// string of equivalent mnemonics. This is, presumably, for writing out
87 /// the mnemonics for the assembly writer.
88 /// @brief Convert attribute bits to text
89 std::string getAsString(Attributes Attrs);
90 } // end namespace Attribute
92 /// This is just a pair of values to associate a set of attributes
94 struct AttributeWithIndex {
95 Attributes Attrs; ///< The attributes that are set, or'd together.
96 unsigned Index; ///< Index of the parameter for which the attributes apply.
97 ///< Index 0 is used for return value attributes.
98 ///< Index ~0U is used for function attributes.
100 static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
101 AttributeWithIndex P;
108 //===----------------------------------------------------------------------===//
109 // AttrListPtr Smart Pointer
110 //===----------------------------------------------------------------------===//
112 class AttributeListImpl;
114 /// AttrListPtr - This class manages the ref count for the opaque
115 /// AttributeListImpl object and provides accessors for it.
117 /// AttrList - The attributes that we are managing. This can be null
118 /// to represent the empty attributes list.
119 AttributeListImpl *AttrList;
121 AttrListPtr() : AttrList(0) {}
122 AttrListPtr(const AttrListPtr &P);
123 const AttrListPtr &operator=(const AttrListPtr &RHS);
126 //===--------------------------------------------------------------------===//
127 // Attribute List Construction and Mutation
128 //===--------------------------------------------------------------------===//
130 /// get - Return a Attributes list with the specified parameter in it.
131 static AttrListPtr get(const AttributeWithIndex *Attr, unsigned NumAttrs);
133 /// get - Return a Attribute list with the parameters specified by the
134 /// consecutive random access iterator range.
135 template <typename Iter>
136 static AttrListPtr get(const Iter &I, const Iter &E) {
137 if (I == E) return AttrListPtr(); // Empty list.
138 return get(&*I, static_cast<unsigned>(E-I));
141 /// addAttr - Add the specified attribute at the specified index to this
142 /// attribute list. Since attribute lists are immutable, this
143 /// returns the new list.
144 AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
146 /// removeAttr - Remove the specified attribute at the specified index from
147 /// this attribute list. Since attribute lists are immutable, this
148 /// returns the new list.
149 AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
151 //===--------------------------------------------------------------------===//
152 // Attribute List Accessors
153 //===--------------------------------------------------------------------===//
154 /// getParamAttributes - The attributes for the specified index are
156 Attributes getParamAttributes(unsigned Idx) const {
157 assert (Idx && Idx != ~0U && "Invalid parameter index!");
158 return getAttributes(Idx);
161 /// getRetAttributes - The attributes for the ret value are
163 Attributes getRetAttributes() const {
164 return getAttributes(0);
167 /// getFnAttributes - The function attributes are returned.
168 Attributes getFnAttributes() const {
169 return getAttributes(~0);
172 /// paramHasAttr - Return true if the specified parameter index has the
173 /// specified attribute set.
174 bool paramHasAttr(unsigned Idx, Attributes Attr) const {
175 return getAttributes(Idx) & Attr;
178 /// getParamAlignment - Return the alignment for the specified function
180 unsigned getParamAlignment(unsigned Idx) const {
181 return (getAttributes(Idx) & Attribute::Alignment) >> 16;
184 /// hasAttrSomewhere - Return true if the specified attribute is set for at
185 /// least one parameter or for the return value.
186 bool hasAttrSomewhere(Attributes Attr) const;
188 /// operator==/!= - Provide equality predicates.
189 bool operator==(const AttrListPtr &RHS) const { return AttrList == RHS.AttrList; }
190 bool operator!=(const AttrListPtr &RHS) const { return AttrList != RHS.AttrList; }
194 //===--------------------------------------------------------------------===//
195 // Attribute List Introspection
196 //===--------------------------------------------------------------------===//
198 /// getRawPointer - Return a raw pointer that uniquely identifies this
200 void *getRawPointer() const {
204 // Attributes are stored as a dense set of slots, where there is one
205 // slot for each argument that has an attribute. This allows walking over the
206 // dense set instead of walking the sparse list of attributes.
208 /// isEmpty - Return true if there are no attributes.
210 bool isEmpty() const {
211 return AttrList == 0;
214 /// getNumSlots - Return the number of slots used in this attribute list.
215 /// This is the number of arguments that have an attribute set on them
216 /// (including the function itself).
217 unsigned getNumSlots() const;
219 /// getSlot - Return the AttributeWithIndex at the specified slot. This
220 /// holds a index number plus a set of attributes.
221 const AttributeWithIndex &getSlot(unsigned Slot) const;
224 explicit AttrListPtr(AttributeListImpl *L);
226 /// getAttributes - The attributes for the specified index are
227 /// returned. Attributes for the result are denoted with Idx = 0.
228 Attributes getAttributes(unsigned Idx) const;
232 } // End llvm namespace