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
24 /// Attributes - A bitset of attributes.
25 typedef unsigned Attributes;
29 /// Function parameters and results can have attributes to indicate how they
30 /// should be treated by optimizations and code generation. This enumeration
31 /// lists the attributes that can be associated with parameters, function
32 /// results or the function itself.
33 /// @brief Function attributes.
35 const Attributes None = 0; ///< No attributes have been set
36 const Attributes ZExt = 1<<0; ///< Zero extended before/after call
37 const Attributes SExt = 1<<1; ///< Sign extended before/after call
38 const Attributes NoReturn = 1<<2; ///< Mark the function as not returning
39 const Attributes InReg = 1<<3; ///< Force argument to be passed in register
40 const Attributes StructRet = 1<<4; ///< Hidden pointer to structure to return
41 const Attributes NoUnwind = 1<<5; ///< Function doesn't unwind stack
42 const Attributes NoAlias = 1<<6; ///< Considered to not alias after call
43 const Attributes ByVal = 1<<7; ///< Pass structure by value
44 const Attributes Nest = 1<<8; ///< Nested function static chain
45 const Attributes ReadNone = 1<<9; ///< Function does not access memory
46 const Attributes ReadOnly = 1<<10; ///< Function only reads from memory
47 const Attributes NoInline = 1<<11; // inline=never
48 const Attributes AlwaysInline = 1<<12; // inline=always
49 const Attributes OptimizeForSize = 1<<13; // opt_size
50 const Attributes Alignment = 0xffff<<16; ///< Alignment of parameter (16 bits)
51 // 0 = unknown, else in clear (not log)
53 /// @brief Attributes that only apply to function parameters.
54 const Attributes ParameterOnly = ByVal | Nest | StructRet;
56 /// @brief Attributes that only apply to function.
57 const Attributes FunctionOnly = NoReturn | NoUnwind | ReadNone | ReadOnly |
58 NoInline | AlwaysInline | OptimizeForSize;
60 /// @brief Parameter attributes that do not apply to vararg call arguments.
61 const Attributes VarArgsIncompatible = StructRet;
63 /// @brief Attributes that are mutually incompatible.
64 const Attributes MutuallyIncompatible[4] = {
65 ByVal | InReg | Nest | StructRet,
68 NoInline | AlwaysInline
71 /// @brief Which attributes cannot be applied to a type.
72 Attributes typeIncompatible(const Type *Ty);
74 /// This turns an int alignment (a power of 2, normally) into the
75 /// form used internally in Attributes.
76 inline Attributes constructAlignmentFromInt(unsigned i) {
80 /// The set of Attributes set in Attributes is converted to a
81 /// string of equivalent mnemonics. This is, presumably, for writing out
82 /// the mnemonics for the assembly writer.
83 /// @brief Convert attribute bits to text
84 std::string getAsString(Attributes Attrs);
85 } // end namespace Attribute
87 /// This is just a pair of values to associate a set of attributes
89 struct AttributeWithIndex {
90 Attributes Attrs; ///< The attributes that are set, or'd together.
91 unsigned Index; ///< Index of the parameter for which the attributes apply.
92 ///< Index 0 is used for return value attributes.
93 ///< Index ~0U is used for function attributes.
95 static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
103 //===----------------------------------------------------------------------===//
104 // AttrListPtr Smart Pointer
105 //===----------------------------------------------------------------------===//
107 class AttributeListImpl;
109 /// AttrListPtr - This class manages the ref count for the opaque
110 /// AttributeListImpl object and provides accessors for it.
112 /// AttrList - The attributes that we are managing. This can be null
113 /// to represent the empty attributes list.
114 AttributeListImpl *AttrList;
116 AttrListPtr() : AttrList(0) {}
117 AttrListPtr(const AttrListPtr &P);
118 const AttrListPtr &operator=(const AttrListPtr &RHS);
121 //===--------------------------------------------------------------------===//
122 // Attribute List Construction and Mutation
123 //===--------------------------------------------------------------------===//
125 /// get - Return a Attributes list with the specified parameter in it.
126 static AttrListPtr get(const AttributeWithIndex *Attr, unsigned NumAttrs);
128 /// get - Return a Attribute list with the parameters specified by the
129 /// consecutive random access iterator range.
130 template <typename Iter>
131 static AttrListPtr get(const Iter &I, const Iter &E) {
132 if (I == E) return AttrListPtr(); // Empty list.
133 return get(&*I, static_cast<unsigned>(E-I));
136 /// addAttr - Add the specified attribute at the specified index to this
137 /// attribute list. Since attribute lists are immutable, this
138 /// returns the new list.
139 AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
141 /// removeAttr - Remove the specified attribute at the specified index from
142 /// this attribute list. Since attribute lists are immutable, this
143 /// returns the new list.
144 AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
146 //===--------------------------------------------------------------------===//
147 // Attribute List Accessors
148 //===--------------------------------------------------------------------===//
149 /// getParamAttributes - The attributes for the specified index are
151 Attributes getParamAttributes(unsigned Idx) const {
152 assert (Idx && Idx != ~0U && "Invalid parameter index!");
153 return getAttributes(Idx);
156 /// getRetAttributes - The attributes for the ret value are
158 Attributes getRetAttributes() const {
159 return getAttributes(0);
162 /// getFnAttributes - The function attributes are returned.
163 Attributes getFnAttributes() const {
164 return getAttributes(~0);
167 /// paramHasAttr - Return true if the specified parameter index has the
168 /// specified attribute set.
169 bool paramHasAttr(unsigned Idx, Attributes Attr) const {
170 return getAttributes(Idx) & Attr;
173 /// getParamAlignment - Return the alignment for the specified function
175 unsigned getParamAlignment(unsigned Idx) const {
176 return (getAttributes(Idx) & Attribute::Alignment) >> 16;
179 /// hasAttrSomewhere - Return true if the specified attribute is set for at
180 /// least one parameter or for the return value.
181 bool hasAttrSomewhere(Attributes Attr) const;
183 /// operator==/!= - Provide equality predicates.
184 bool operator==(const AttrListPtr &RHS) const { return AttrList == RHS.AttrList; }
185 bool operator!=(const AttrListPtr &RHS) const { return AttrList != RHS.AttrList; }
189 //===--------------------------------------------------------------------===//
190 // Attribute List Introspection
191 //===--------------------------------------------------------------------===//
193 /// getRawPointer - Return a raw pointer that uniquely identifies this
195 void *getRawPointer() const {
199 // Attributes are stored as a dense set of slots, where there is one
200 // slot for each argument that has an attribute. This allows walking over the
201 // dense set instead of walking the sparse list of attributes.
203 /// isEmpty - Return true if there are no attributes.
205 bool isEmpty() const {
206 return AttrList == 0;
209 /// getNumSlots - Return the number of slots used in this attribute list.
210 /// This is the number of arguments that have an attribute set on them
211 /// (including the function itself).
212 unsigned getNumSlots() const;
214 /// getSlot - Return the AttributeWithIndex at the specified slot. This
215 /// holds a index number plus a set of attributes.
216 const AttributeWithIndex &getSlot(unsigned Slot) const;
219 explicit AttrListPtr(AttributeListImpl *L);
221 /// getAttributes - The attributes for the specified index are
222 /// returned. Attributes for the result are denoted with Idx = 0.
223 Attributes getAttributes(unsigned Idx) const;
227 } // End llvm namespace