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 return values.
57 const Attributes ReturnOnly = NoReturn | NoUnwind | ReadNone | ReadOnly;
59 /// @brief Parameter attributes that do not apply to vararg call arguments.
60 const Attributes VarArgsIncompatible = StructRet;
62 /// @brief Attributes that are mutually incompatible.
63 const Attributes MutuallyIncompatible[3] = {
64 ByVal | InReg | Nest | StructRet,
69 /// @brief Which attributes cannot be applied to a type.
70 Attributes typeIncompatible(const Type *Ty);
72 /// This turns an int alignment (a power of 2, normally) into the
73 /// form used internally in Attributes.
74 inline Attributes constructAlignmentFromInt(unsigned i) {
78 /// The set of Attributes set in Attributes is converted to a
79 /// string of equivalent mnemonics. This is, presumably, for writing out
80 /// the mnemonics for the assembly writer.
81 /// @brief Convert attribute bits to text
82 std::string getAsString(Attributes Attrs);
83 } // end namespace Attribute
86 } // end namespace Attribute
88 /// This is just a pair of values to associate a set of attributes
90 struct AttributeWithIndex {
91 Attributes Attrs; ///< The attributes that are set, or'd together.
92 unsigned Index; ///< Index of the parameter for which the attributes apply.
93 ///< Index 0 is used for return value attributes.
94 ///< Index ~0U is used for function attributes.
96 static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
104 //===----------------------------------------------------------------------===//
105 // AttrListPtr Smart Pointer
106 //===----------------------------------------------------------------------===//
108 class AttributeListImpl;
110 /// AttrListPtr - This class manages the ref count for the opaque
111 /// AttributeListImpl object and provides accessors for it.
113 /// AttrList - The attributes that we are managing. This can be null
114 /// to represent the empty attributes list.
115 AttributeListImpl *AttrList;
117 AttrListPtr() : AttrList(0) {}
118 AttrListPtr(const AttrListPtr &P);
119 const AttrListPtr &operator=(const AttrListPtr &RHS);
122 //===--------------------------------------------------------------------===//
123 // Attribute List Construction and Mutation
124 //===--------------------------------------------------------------------===//
126 /// get - Return a Attributes list with the specified parameter in it.
127 static AttrListPtr get(const AttributeWithIndex *Attr, unsigned NumAttrs);
129 /// get - Return a Attribute list with the parameters specified by the
130 /// consecutive random access iterator range.
131 template <typename Iter>
132 static AttrListPtr get(const Iter &I, const Iter &E) {
133 if (I == E) return AttrListPtr(); // Empty list.
134 return get(&*I, static_cast<unsigned>(E-I));
137 /// addAttr - Add the specified attribute at the specified index to this
138 /// attribute list. Since attribute lists are immutable, this
139 /// returns the new list.
140 AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
142 /// removeAttr - Remove the specified attribute at the specified index from
143 /// this attribute list. Since attribute lists are immutable, this
144 /// returns the new list.
145 AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
147 //===--------------------------------------------------------------------===//
148 // Attribute List Accessors
149 //===--------------------------------------------------------------------===//
150 /// getParamAttributes - The attributes for the specified index are
152 Attributes getParamAttributes(unsigned Idx) const {
153 assert (Idx && Idx != ~0U && "Invalid parameter index!");
154 return getAttributes(Idx);
157 /// getRetAttributes - The attributes for the ret value are
159 Attributes getRetAttributes() const {
160 return getAttributes(0);
163 /// getFnAttributes - The function attributes are returned.
164 Attributes getFnAttributes() const {
165 return getAttributes(~0);
168 /// paramHasAttr - Return true if the specified parameter index has the
169 /// specified attribute set.
170 bool paramHasAttr(unsigned Idx, Attributes Attr) const {
171 return getAttributes(Idx) & Attr;
174 /// getParamAlignment - Return the alignment for the specified function
176 unsigned getParamAlignment(unsigned Idx) const {
177 return (getAttributes(Idx) & Attribute::Alignment) >> 16;
180 /// hasAttrSomewhere - Return true if the specified attribute is set for at
181 /// least one parameter or for the return value.
182 bool hasAttrSomewhere(Attributes Attr) const;
184 /// operator==/!= - Provide equality predicates.
185 bool operator==(const AttrListPtr &RHS) const { return AttrList == RHS.AttrList; }
186 bool operator!=(const AttrListPtr &RHS) const { return AttrList != RHS.AttrList; }
190 //===--------------------------------------------------------------------===//
191 // Attribute List Introspection
192 //===--------------------------------------------------------------------===//
194 /// getRawPointer - Return a raw pointer that uniquely identifies this
196 void *getRawPointer() const {
200 // Attributes are stored as a dense set of slots, where there is one
201 // slot for each argument that has an attribute. This allows walking over the
202 // dense set instead of walking the sparse list of attributes.
204 /// isEmpty - Return true if there are no attributes.
206 bool isEmpty() const {
207 return AttrList == 0;
210 /// getNumSlots - Return the number of slots used in this attribute list.
211 /// This is the number of arguments that have an attribute set on them
212 /// (including the function itself).
213 unsigned getNumSlots() const;
215 /// getSlot - Return the AttributeWithIndex at the specified slot. This
216 /// holds a index number plus a set of attributes.
217 const AttributeWithIndex &getSlot(unsigned Slot) const;
220 explicit AttrListPtr(AttributeListImpl *L);
222 /// getAttributes - The attributes for the specified index are
223 /// returned. Attributes for the result are denoted with Idx = 0.
224 Attributes getAttributes(unsigned Idx) const;
228 } // End llvm namespace