Attributes.h revision 89e124b6bea174978a8c9b8472731cf0dbf18511
1//===-- llvm/Attributes.h - Container for Attributes ------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the simple types necessary to represent the
11// attributes associated with functions and their calls.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_ATTRIBUTES_H
16#define LLVM_ATTRIBUTES_H
17
18#include "llvm/Support/MathExtras.h"
19#include <cassert>
20#include <string>
21
22namespace llvm {
23class Type;
24
25/// Attributes - A bitset of attributes.
26typedef unsigned Attributes;
27
28namespace Attribute {
29
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.
35
36const Attributes None      = 0;     ///< No attributes have been set
37const Attributes ZExt      = 1<<0;  ///< Zero extended before/after call
38const Attributes SExt      = 1<<1;  ///< Sign extended before/after call
39const Attributes NoReturn  = 1<<2;  ///< Mark the function as not returning
40const Attributes InReg     = 1<<3;  ///< Force argument to be passed in register
41const Attributes StructRet = 1<<4;  ///< Hidden pointer to structure to return
42const Attributes NoUnwind  = 1<<5;  ///< Function doesn't unwind stack
43const Attributes NoAlias   = 1<<6;  ///< Considered to not alias after call
44const Attributes ByVal     = 1<<7;  ///< Pass structure by value
45const Attributes Nest      = 1<<8;  ///< Nested function static chain
46const Attributes ReadNone  = 1<<9;  ///< Function does not access memory
47const Attributes ReadOnly  = 1<<10; ///< Function only reads from memory
48const Attributes NoInline        = 1<<11; ///< inline=never
49const Attributes AlwaysInline    = 1<<12; ///< inline=always
50const Attributes OptimizeForSize = 1<<13; ///< opt_size
51const Attributes StackProtect    = 1<<14; ///< Stack protection.
52const Attributes StackProtectReq = 1<<15; ///< Stack protection required.
53const 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
56const Attributes NoCapture = 1<<21; ///< Function creates no aliases of pointer
57const Attributes NoRedZone = 1<<22; /// disable redzone
58const Attributes NoImplicitFloat = 1<<23; /// disable implicit floating point
59                                          /// instructions.
60const Attributes Naked           = 1<<24; ///< Naked function
61const Attributes InlineHint      = 1<<25; ///< source said inlining was
62                                          ///desirable
63const 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
67                                          ///alignstack(1))
68
69/// @brief Attributes that only apply to function parameters.
70const Attributes ParameterOnly = ByVal | Nest | StructRet | NoCapture;
71
72/// @brief Attributes that may be applied to the function itself.  These cannot
73/// be used on return values or function parameters.
74const Attributes FunctionOnly = NoReturn | NoUnwind | ReadNone | ReadOnly |
75  NoInline | AlwaysInline | OptimizeForSize | StackProtect | StackProtectReq |
76  NoRedZone | NoImplicitFloat | Naked | InlineHint | StackAlignment;
77
78/// @brief Parameter attributes that do not apply to vararg call arguments.
79const Attributes VarArgsIncompatible = StructRet;
80
81/// @brief Attributes that are mutually incompatible.
82const Attributes MutuallyIncompatible[4] = {
83  ByVal | InReg | Nest | StructRet,
84  ZExt  | SExt,
85  ReadNone | ReadOnly,
86  NoInline | AlwaysInline
87};
88
89/// @brief Which attributes cannot be applied to a type.
90Attributes typeIncompatible(const Type *Ty);
91
92/// This turns an int alignment (a power of 2, normally) into the
93/// form used internally in Attributes.
94inline Attributes constructAlignmentFromInt(unsigned i) {
95  // Default alignment, allow the target to define how to align it.
96  if (i == 0)
97    return 0;
98
99  assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
100  assert(i <= 0x40000000 && "Alignment too large.");
101  return (Log2_32(i)+1) << 16;
102}
103
104/// This returns the alignment field of an attribute as a byte alignment value.
105inline unsigned getAlignmentFromAttrs(Attributes A) {
106  Attributes Align = A & Attribute::Alignment;
107  if (Align == 0)
108    return 0;
109
110  return 1U << ((Align >> 16) - 1);
111}
112
113/// This turns an int stack alignment (which must be a power of 2) into
114/// the form used internally in Attributes.
115inline Attributes constructStackAlignmentFromInt(unsigned i) {
116  // Default alignment, allow the target to define how to align it.
117  if (i == 0)
118    return 0;
119
120  assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
121  assert(i <= 0x100 && "Alignment too large.");
122  return (Log2_32(i)+1) << 26;
123}
124
125/// This returns the stack alignment field of an attribute as a byte alignment
126/// value.
127inline unsigned getStackAlignmentFromAttrs(Attributes A) {
128  Attributes StackAlign = A & Attribute::StackAlignment;
129  if (StackAlign == 0)
130    return 0;
131
132  return 1U << ((StackAlign >> 26) - 1);
133}
134
135
136/// The set of Attributes set in Attributes is converted to a
137/// string of equivalent mnemonics. This is, presumably, for writing out
138/// the mnemonics for the assembly writer.
139/// @brief Convert attribute bits to text
140std::string getAsString(Attributes Attrs);
141} // end namespace Attribute
142
143/// This is just a pair of values to associate a set of attributes
144/// with an index.
145struct AttributeWithIndex {
146  Attributes Attrs; ///< The attributes that are set, or'd together.
147  unsigned Index; ///< Index of the parameter for which the attributes apply.
148                  ///< Index 0 is used for return value attributes.
149                  ///< Index ~0U is used for function attributes.
150
151  static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
152    AttributeWithIndex P;
153    P.Index = Idx;
154    P.Attrs = Attrs;
155    return P;
156  }
157};
158
159//===----------------------------------------------------------------------===//
160// AttrListPtr Smart Pointer
161//===----------------------------------------------------------------------===//
162
163class AttributeListImpl;
164
165/// AttrListPtr - This class manages the ref count for the opaque
166/// AttributeListImpl object and provides accessors for it.
167class AttrListPtr {
168  /// AttrList - The attributes that we are managing.  This can be null
169  /// to represent the empty attributes list.
170  AttributeListImpl *AttrList;
171public:
172  AttrListPtr() : AttrList(0) {}
173  AttrListPtr(const AttrListPtr &P);
174  const AttrListPtr &operator=(const AttrListPtr &RHS);
175  ~AttrListPtr();
176
177  //===--------------------------------------------------------------------===//
178  // Attribute List Construction and Mutation
179  //===--------------------------------------------------------------------===//
180
181  /// get - Return a Attributes list with the specified parameter in it.
182  static AttrListPtr get(const AttributeWithIndex *Attr, unsigned NumAttrs);
183
184  /// get - Return a Attribute list with the parameters specified by the
185  /// consecutive random access iterator range.
186  template <typename Iter>
187  static AttrListPtr get(const Iter &I, const Iter &E) {
188    if (I == E) return AttrListPtr();  // Empty list.
189    return get(&*I, static_cast<unsigned>(E-I));
190  }
191
192  /// addAttr - Add the specified attribute at the specified index to this
193  /// attribute list.  Since attribute lists are immutable, this
194  /// returns the new list.
195  AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
196
197  /// removeAttr - Remove the specified attribute at the specified index from
198  /// this attribute list.  Since attribute lists are immutable, this
199  /// returns the new list.
200  AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
201
202  //===--------------------------------------------------------------------===//
203  // Attribute List Accessors
204  //===--------------------------------------------------------------------===//
205  /// getParamAttributes - The attributes for the specified index are
206  /// returned.
207  Attributes getParamAttributes(unsigned Idx) const {
208    assert (Idx && Idx != ~0U && "Invalid parameter index!");
209    return getAttributes(Idx);
210  }
211
212  /// getRetAttributes - The attributes for the ret value are
213  /// returned.
214  Attributes getRetAttributes() const {
215    return getAttributes(0);
216  }
217
218  /// getFnAttributes - The function attributes are returned.
219  Attributes getFnAttributes() const {
220    return getAttributes(~0U);
221  }
222
223  /// paramHasAttr - Return true if the specified parameter index has the
224  /// specified attribute set.
225  bool paramHasAttr(unsigned Idx, Attributes Attr) const {
226    return getAttributes(Idx) & Attr;
227  }
228
229  /// getParamAlignment - Return the alignment for the specified function
230  /// parameter.
231  unsigned getParamAlignment(unsigned Idx) const {
232    return Attribute::getAlignmentFromAttrs(getAttributes(Idx));
233  }
234
235  /// hasAttrSomewhere - Return true if the specified attribute is set for at
236  /// least one parameter or for the return value.
237  bool hasAttrSomewhere(Attributes Attr) const;
238
239  /// operator==/!= - Provide equality predicates.
240  bool operator==(const AttrListPtr &RHS) const
241  { return AttrList == RHS.AttrList; }
242  bool operator!=(const AttrListPtr &RHS) const
243  { return AttrList != RHS.AttrList; }
244
245  void dump() const;
246
247  //===--------------------------------------------------------------------===//
248  // Attribute List Introspection
249  //===--------------------------------------------------------------------===//
250
251  /// getRawPointer - Return a raw pointer that uniquely identifies this
252  /// attribute list.
253  void *getRawPointer() const {
254    return AttrList;
255  }
256
257  // Attributes are stored as a dense set of slots, where there is one
258  // slot for each argument that has an attribute.  This allows walking over the
259  // dense set instead of walking the sparse list of attributes.
260
261  /// isEmpty - Return true if there are no attributes.
262  ///
263  bool isEmpty() const {
264    return AttrList == 0;
265  }
266
267  /// getNumSlots - Return the number of slots used in this attribute list.
268  /// This is the number of arguments that have an attribute set on them
269  /// (including the function itself).
270  unsigned getNumSlots() const;
271
272  /// getSlot - Return the AttributeWithIndex at the specified slot.  This
273  /// holds a index number plus a set of attributes.
274  const AttributeWithIndex &getSlot(unsigned Slot) const;
275
276private:
277  explicit AttrListPtr(AttributeListImpl *L);
278
279  /// getAttributes - The attributes for the specified index are
280  /// returned.  Attributes for the result are denoted with Idx = 0.
281  Attributes getAttributes(unsigned Idx) const;
282
283};
284
285} // End llvm namespace
286
287#endif
288