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