Attributes.h revision d6de30ceea69ee9ce6dded4bb3ae5881861cb090
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.
60
61/// @brief Attributes that only apply to function parameters.
62const Attributes ParameterOnly = ByVal | Nest | StructRet | NoCapture;
63
64/// @brief Attributes that may be applied to the function itself.  These cannot
65/// be used on return values or function parameters.
66const Attributes FunctionOnly = NoReturn | NoUnwind | ReadNone | ReadOnly |
67  NoInline | AlwaysInline | OptimizeForSize | StackProtect | StackProtectReq |
68  NoRedZone | NoImplicitFloat;
69
70/// @brief Parameter attributes that do not apply to vararg call arguments.
71const Attributes VarArgsIncompatible = StructRet;
72
73/// @brief Attributes that are mutually incompatible.
74const Attributes MutuallyIncompatible[4] = {
75  ByVal | InReg | Nest | StructRet,
76  ZExt  | SExt,
77  ReadNone | ReadOnly,
78  NoInline | AlwaysInline
79};
80
81/// @brief Which attributes cannot be applied to a type.
82Attributes typeIncompatible(const Type *Ty);
83
84/// This turns an int alignment (a power of 2, normally) into the
85/// form used internally in Attributes.
86inline Attributes constructAlignmentFromInt(unsigned i) {
87  // Default alignment, allow the target to define how to align it.
88  if (i == 0)
89    return 0;
90
91  assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
92  assert(i <= 0x40000000 && "Alignment too large.");
93  return (Log2_32(i)+1) << 16;
94}
95
96/// This returns the alignment field of an attribute as a byte alignment value.
97inline unsigned getAlignmentFromAttrs(Attributes A) {
98  Attributes Align = A & Attribute::Alignment;
99  if (Align == 0)
100    return 0;
101
102  return 1U << ((Align >> 16) - 1);
103}
104
105
106/// The set of Attributes set in Attributes is converted to a
107/// string of equivalent mnemonics. This is, presumably, for writing out
108/// the mnemonics for the assembly writer.
109/// @brief Convert attribute bits to text
110std::string getAsString(Attributes Attrs);
111} // end namespace Attribute
112
113/// This is just a pair of values to associate a set of attributes
114/// with an index.
115struct AttributeWithIndex {
116  Attributes Attrs; ///< The attributes that are set, or'd together.
117  unsigned Index; ///< Index of the parameter for which the attributes apply.
118                  ///< Index 0 is used for return value attributes.
119                  ///< Index ~0U is used for function attributes.
120
121  static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
122    AttributeWithIndex P;
123    P.Index = Idx;
124    P.Attrs = Attrs;
125    return P;
126  }
127};
128
129//===----------------------------------------------------------------------===//
130// AttrListPtr Smart Pointer
131//===----------------------------------------------------------------------===//
132
133class AttributeListImpl;
134
135/// AttrListPtr - This class manages the ref count for the opaque
136/// AttributeListImpl object and provides accessors for it.
137class AttrListPtr {
138  /// AttrList - The attributes that we are managing.  This can be null
139  /// to represent the empty attributes list.
140  AttributeListImpl *AttrList;
141public:
142  AttrListPtr() : AttrList(0) {}
143  AttrListPtr(const AttrListPtr &P);
144  const AttrListPtr &operator=(const AttrListPtr &RHS);
145  ~AttrListPtr();
146
147  //===--------------------------------------------------------------------===//
148  // Attribute List Construction and Mutation
149  //===--------------------------------------------------------------------===//
150
151  /// get - Return a Attributes list with the specified parameter in it.
152  static AttrListPtr get(const AttributeWithIndex *Attr, unsigned NumAttrs);
153
154  /// get - Return a Attribute list with the parameters specified by the
155  /// consecutive random access iterator range.
156  template <typename Iter>
157  static AttrListPtr get(const Iter &I, const Iter &E) {
158    if (I == E) return AttrListPtr();  // Empty list.
159    return get(&*I, static_cast<unsigned>(E-I));
160  }
161
162  /// addAttr - Add the specified attribute at the specified index to this
163  /// attribute list.  Since attribute lists are immutable, this
164  /// returns the new list.
165  AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
166
167  /// removeAttr - Remove the specified attribute at the specified index from
168  /// this attribute list.  Since attribute lists are immutable, this
169  /// returns the new list.
170  AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
171
172  //===--------------------------------------------------------------------===//
173  // Attribute List Accessors
174  //===--------------------------------------------------------------------===//
175  /// getParamAttributes - The attributes for the specified index are
176  /// returned.
177  Attributes getParamAttributes(unsigned Idx) const {
178    assert (Idx && Idx != ~0U && "Invalid parameter index!");
179    return getAttributes(Idx);
180  }
181
182  /// getRetAttributes - The attributes for the ret value are
183  /// returned.
184  Attributes getRetAttributes() const {
185    return getAttributes(0);
186  }
187
188  /// getFnAttributes - The function attributes are returned.
189  Attributes getFnAttributes() const {
190    return getAttributes(~0U);
191  }
192
193  /// paramHasAttr - Return true if the specified parameter index has the
194  /// specified attribute set.
195  bool paramHasAttr(unsigned Idx, Attributes Attr) const {
196    return getAttributes(Idx) & Attr;
197  }
198
199  /// getParamAlignment - Return the alignment for the specified function
200  /// parameter.
201  unsigned getParamAlignment(unsigned Idx) const {
202    return Attribute::getAlignmentFromAttrs(getAttributes(Idx));
203  }
204
205  /// hasAttrSomewhere - Return true if the specified attribute is set for at
206  /// least one parameter or for the return value.
207  bool hasAttrSomewhere(Attributes Attr) const;
208
209  /// operator==/!= - Provide equality predicates.
210  bool operator==(const AttrListPtr &RHS) const { return AttrList == RHS.AttrList; }
211  bool operator!=(const AttrListPtr &RHS) const { return AttrList != RHS.AttrList; }
212
213  void dump() const;
214
215  //===--------------------------------------------------------------------===//
216  // Attribute List Introspection
217  //===--------------------------------------------------------------------===//
218
219  /// getRawPointer - Return a raw pointer that uniquely identifies this
220  /// attribute list.
221  void *getRawPointer() const {
222    return AttrList;
223  }
224
225  // Attributes are stored as a dense set of slots, where there is one
226  // slot for each argument that has an attribute.  This allows walking over the
227  // dense set instead of walking the sparse list of attributes.
228
229  /// isEmpty - Return true if there are no attributes.
230  ///
231  bool isEmpty() const {
232    return AttrList == 0;
233  }
234
235  /// getNumSlots - Return the number of slots used in this attribute list.
236  /// This is the number of arguments that have an attribute set on them
237  /// (including the function itself).
238  unsigned getNumSlots() const;
239
240  /// getSlot - Return the AttributeWithIndex at the specified slot.  This
241  /// holds a index number plus a set of attributes.
242  const AttributeWithIndex &getSlot(unsigned Slot) const;
243
244private:
245  explicit AttrListPtr(AttributeListImpl *L);
246
247  /// getAttributes - The attributes for the specified index are
248  /// returned.  Attributes for the result are denoted with Idx = 0.
249  Attributes getAttributes(unsigned Idx) const;
250
251};
252
253} // End llvm namespace
254
255#endif
256