Attributes.h revision f9271ea159b97e2febedcf095c3c4122cb24d077
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/// \file
11/// \brief This file contains the simple types necessary to represent the
12/// attributes associated with functions and their calls.
13///
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_IR_ATTRIBUTES_H
17#define LLVM_IR_ATTRIBUTES_H
18
19#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/DenseSet.h"
21#include "llvm/ADT/FoldingSet.h"
22#include <cassert>
23#include <string>
24
25namespace llvm {
26
27class AttrBuilder;
28class AttributeImpl;
29class AttributeSetImpl;
30class AttributeSetNode;
31class Constant;
32class LLVMContext;
33class Type;
34
35//===----------------------------------------------------------------------===//
36/// \class
37/// \brief Functions, function parameters, and return types can have attributes
38/// to indicate how they should be treated by optimizations and code
39/// generation. This class represents one of those attributes. It's light-weight
40/// and should be passed around by-value.
41class Attribute {
42public:
43  /// This enumeration lists the attributes that can be associated with
44  /// parameters, function results, or the function itself.
45  ///
46  /// Note: The `uwtable' attribute is about the ABI or the user mandating an
47  /// entry in the unwind table. The `nounwind' attribute is about an exception
48  /// passing by the function.
49  ///
50  /// In a theoretical system that uses tables for profiling and SjLj for
51  /// exceptions, they would be fully independent. In a normal system that uses
52  /// tables for both, the semantics are:
53  ///
54  /// nil                = Needs an entry because an exception might pass by.
55  /// nounwind           = No need for an entry
56  /// uwtable            = Needs an entry because the ABI says so and because
57  ///                      an exception might pass by.
58  /// uwtable + nounwind = Needs an entry because the ABI says so.
59
60  enum AttrKind {
61    // IR-Level Attributes
62    None,                  ///< No attributes have been set
63    AddressSafety,         ///< Address safety checking is on.
64    Alignment,             ///< Alignment of parameter (5 bits)
65                           ///< stored as log2 of alignment with +1 bias
66                           ///< 0 means unaligned (different from align(1))
67    AlwaysInline,          ///< inline=always
68    ByVal,                 ///< Pass structure by value
69    InlineHint,            ///< Source said inlining was desirable
70    InReg,                 ///< Force argument to be passed in register
71    MinSize,               ///< Function must be optimized for size first
72    Naked,                 ///< Naked function
73    Nest,                  ///< Nested function static chain
74    NoAlias,               ///< Considered to not alias after call
75    NoCapture,             ///< Function creates no aliases of pointer
76    NoDuplicate,           ///< Call cannot be duplicated
77    NoImplicitFloat,       ///< Disable implicit floating point insts
78    NoInline,              ///< inline=never
79    NonLazyBind,           ///< Function is called early and/or
80                           ///< often, so lazy binding isn't worthwhile
81    NoRedZone,             ///< Disable redzone
82    NoReturn,              ///< Mark the function as not returning
83    NoUnwind,              ///< Function doesn't unwind stack
84    OptimizeForSize,       ///< opt_size
85    ReadNone,              ///< Function does not access memory
86    ReadOnly,              ///< Function only reads from memory
87    ReturnsTwice,          ///< Function can return twice
88    SExt,                  ///< Sign extended before/after call
89    StackAlignment,        ///< Alignment of stack for function (3 bits)
90                           ///< stored as log2 of alignment with +1 bias 0
91                           ///< means unaligned (different from
92                           ///< alignstack=(1))
93    StackProtect,          ///< Stack protection.
94    StackProtectReq,       ///< Stack protection required.
95    StackProtectStrong,    ///< Strong Stack protection.
96    StructRet,             ///< Hidden pointer to structure to return
97    UWTable,               ///< Function must be in a unwind table
98    ZExt,                  ///< Zero extended before/after call
99
100    EndAttrKinds,          ///< Sentinal value useful for loops
101
102    AttrKindEmptyKey,      ///< Empty key value for DenseMapInfo
103    AttrKindTombstoneKey   ///< Tombstone key value for DenseMapInfo
104  };
105private:
106  AttributeImpl *pImpl;
107  Attribute(AttributeImpl *A) : pImpl(A) {}
108public:
109  Attribute() : pImpl(0) {}
110
111  //===--------------------------------------------------------------------===//
112  // Attribute Construction
113  //===--------------------------------------------------------------------===//
114
115  /// \brief Return a uniquified Attribute object.
116  static Attribute get(LLVMContext &Context, AttrKind Kind, Constant *Val = 0);
117  static Attribute get(LLVMContext &Context, Constant *Kind, Constant *Val = 0);
118
119  /// \brief Return a uniquified Attribute object that has the specific
120  /// alignment set.
121  static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
122  static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
123
124  //===--------------------------------------------------------------------===//
125  // Attribute Accessors
126  //===--------------------------------------------------------------------===//
127
128  /// \brief Return true if the attribute is present.
129  bool hasAttribute(AttrKind Val) const;
130
131  /// \brief Return the kind of this attribute: enum or string.
132  Constant *getAttributeKind() const;
133
134  /// \brief Return the values (if present) of the attribute. This may be a
135  /// ConstantVector to represent a list of values associated with the
136  /// attribute.
137  Constant *getAttributeValues() const;
138
139  /// \brief Returns the alignment field of an attribute as a byte alignment
140  /// value.
141  unsigned getAlignment() const;
142
143  /// \brief Returns the stack alignment field of an attribute as a byte
144  /// alignment value.
145  unsigned getStackAlignment() const;
146
147  /// \brief The Attribute is converted to a string of equivalent mnemonic. This
148  /// is, presumably, for writing out the mnemonics for the assembly writer.
149  std::string getAsString() const;
150
151  /// \brief Equality and non-equality query methods.
152  bool operator==(AttrKind K) const;
153  bool operator!=(AttrKind K) const;
154
155  bool operator==(Attribute A) const { return pImpl == A.pImpl; }
156  bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
157
158  /// \brief Less-than operator. Useful for sorting the attributes list.
159  bool operator<(Attribute A) const;
160
161  void Profile(FoldingSetNodeID &ID) const {
162    ID.AddPointer(pImpl);
163  }
164};
165
166//===----------------------------------------------------------------------===//
167/// \class
168/// \brief This class manages the ref count for the opaque AttributeSetImpl
169/// object and provides accessors for it.
170class AttributeSet {
171public:
172  enum AttrIndex {
173    ReturnIndex = 0U,
174    FunctionIndex = ~0U
175  };
176private:
177  friend class AttrBuilder;
178  friend class AttributeSetImpl;
179
180  /// \brief The attributes that we are managing. This can be null to represent
181  /// the empty attributes list.
182  AttributeSetImpl *pImpl;
183
184  /// \brief The attributes for the specified index are returned.
185  AttributeSetNode *getAttributes(unsigned Idx) const;
186
187  /// \brief Create an AttributeSet with the specified parameters in it.
188  static AttributeSet get(LLVMContext &C,
189                          ArrayRef<std::pair<unsigned, Attribute> > Attrs);
190  static AttributeSet get(LLVMContext &C,
191                          ArrayRef<std::pair<unsigned,
192                                             AttributeSetNode*> > Attrs);
193
194  static AttributeSet getImpl(LLVMContext &C,
195                              ArrayRef<std::pair<unsigned,
196                                                 AttributeSetNode*> > Attrs);
197
198
199  explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {}
200public:
201  AttributeSet() : pImpl(0) {}
202  AttributeSet(const AttributeSet &P) : pImpl(P.pImpl) {}
203  const AttributeSet &operator=(const AttributeSet &RHS) {
204    pImpl = RHS.pImpl;
205    return *this;
206  }
207
208  //===--------------------------------------------------------------------===//
209  // AttributeSet Construction and Mutation
210  //===--------------------------------------------------------------------===//
211
212  /// \brief Return an AttributeSet with the specified parameters in it.
213  static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs);
214  static AttributeSet get(LLVMContext &C, unsigned Idx,
215                          ArrayRef<Attribute::AttrKind> Kind);
216  static AttributeSet get(LLVMContext &C, unsigned Idx, AttrBuilder &B);
217
218  /// \brief Add an attribute to the attribute set at the given index. Since
219  /// attribute sets are immutable, this returns a new set.
220  AttributeSet addAttribute(LLVMContext &C, unsigned Idx,
221                            Attribute::AttrKind Attr) const;
222
223  /// \brief Add attributes to the attribute set at the given index. Since
224  /// attribute sets are immutable, this returns a new set.
225  AttributeSet addAttributes(LLVMContext &C, unsigned Idx,
226                             AttributeSet Attrs) const;
227
228  /// \brief Remove the specified attribute at the specified index from this
229  /// attribute list. Since attribute lists are immutable, this returns the new
230  /// list.
231  AttributeSet removeAttribute(LLVMContext &C, unsigned Idx,
232                               Attribute::AttrKind Attr) const;
233
234  /// \brief Remove the specified attributes at the specified index from this
235  /// attribute list. Since attribute lists are immutable, this returns the new
236  /// list.
237  AttributeSet removeAttributes(LLVMContext &C, unsigned Idx,
238                                AttributeSet Attrs) const;
239
240  //===--------------------------------------------------------------------===//
241  // AttributeSet Accessors
242  //===--------------------------------------------------------------------===//
243
244  /// \brief The attributes for the specified index are returned.
245  AttributeSet getParamAttributes(unsigned Idx) const;
246
247  /// \brief The attributes for the ret value are returned.
248  AttributeSet getRetAttributes() const;
249
250  /// \brief The function attributes are returned.
251  AttributeSet getFnAttributes() const;
252
253  /// \brief Return true if the attribute exists at the given index.
254  bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
255
256  /// \brief Return true if attribute exists at the given index.
257  bool hasAttributes(unsigned Index) const;
258
259  /// \brief Return true if the specified attribute is set for at least one
260  /// parameter or for the return value.
261  bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
262
263  /// \brief Return the alignment for the specified function parameter.
264  unsigned getParamAlignment(unsigned Idx) const;
265
266  /// \brief Get the stack alignment.
267  unsigned getStackAlignment(unsigned Index) const;
268
269  /// \brief Return the attributes at the index as a string.
270  std::string getAsString(unsigned Index) const;
271
272  typedef ArrayRef<Attribute>::iterator iterator;
273
274  iterator begin(unsigned Idx) const;
275  iterator end(unsigned Idx) const;
276
277  /// operator==/!= - Provide equality predicates.
278  bool operator==(const AttributeSet &RHS) const {
279    return pImpl == RHS.pImpl;
280  }
281  bool operator!=(const AttributeSet &RHS) const {
282    return pImpl != RHS.pImpl;
283  }
284
285  //===--------------------------------------------------------------------===//
286  // AttributeSet Introspection
287  //===--------------------------------------------------------------------===//
288
289  // FIXME: Remove this.
290  uint64_t Raw(unsigned Index) const;
291
292  /// \brief Return a raw pointer that uniquely identifies this attribute list.
293  void *getRawPointer() const {
294    return pImpl;
295  }
296
297  /// \brief Return true if there are no attributes.
298  bool isEmpty() const {
299    return getNumSlots() == 0;
300  }
301
302  /// \brief Return the number of slots used in this attribute list.  This is
303  /// the number of arguments that have an attribute set on them (including the
304  /// function itself).
305  unsigned getNumSlots() const;
306
307  /// \brief Return the index for the given slot.
308  uint64_t getSlotIndex(unsigned Slot) const;
309
310  /// \brief Return the attributes at the given slot.
311  AttributeSet getSlotAttributes(unsigned Slot) const;
312
313  void dump() const;
314};
315
316//===----------------------------------------------------------------------===//
317/// \class
318/// \brief Provide DenseMapInfo for Attribute::AttrKinds. This is used by
319/// AttrBuilder.
320template<> struct DenseMapInfo<Attribute::AttrKind> {
321  static inline Attribute::AttrKind getEmptyKey() {
322    return Attribute::AttrKindEmptyKey;
323  }
324  static inline Attribute::AttrKind getTombstoneKey() {
325    return Attribute::AttrKindTombstoneKey;
326  }
327  static unsigned getHashValue(const Attribute::AttrKind &Val) {
328    return Val * 37U;
329  }
330  static bool isEqual(const Attribute::AttrKind &LHS,
331                      const Attribute::AttrKind &RHS) {
332    return LHS == RHS;
333  }
334};
335
336//===----------------------------------------------------------------------===//
337/// \class
338/// \brief This class is used in conjunction with the Attribute::get method to
339/// create an Attribute object. The object itself is uniquified. The Builder's
340/// value, however, is not. So this can be used as a quick way to test for
341/// equality, presence of attributes, etc.
342class AttrBuilder {
343  DenseSet<Attribute::AttrKind> Attrs;
344  uint64_t Alignment;
345  uint64_t StackAlignment;
346public:
347  AttrBuilder() : Alignment(0), StackAlignment(0) {}
348  explicit AttrBuilder(uint64_t Val) : Alignment(0), StackAlignment(0) {
349    addRawValue(Val);
350  }
351  AttrBuilder(const Attribute &A) : Alignment(0), StackAlignment(0) {
352    addAttribute(A);
353  }
354  AttrBuilder(AttributeSet AS, unsigned Idx);
355
356  void clear();
357
358  /// \brief Add an attribute to the builder.
359  AttrBuilder &addAttribute(Attribute::AttrKind Val);
360
361  /// \brief Add the Attribute object to the builder.
362  AttrBuilder &addAttribute(Attribute A);
363
364  /// \brief Remove an attribute from the builder.
365  AttrBuilder &removeAttribute(Attribute::AttrKind Val);
366
367  /// \brief Remove the attributes from the builder.
368  AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index);
369
370  /// \brief Return true if the builder has the specified attribute.
371  bool contains(Attribute::AttrKind A) const;
372
373  /// \brief Return true if the builder has IR-level attributes.
374  bool hasAttributes() const;
375
376  /// \brief Return true if the builder has any attribute that's in the
377  /// specified attribute.
378  bool hasAttributes(AttributeSet A, uint64_t Index) const;
379
380  /// \brief Return true if the builder has an alignment attribute.
381  bool hasAlignmentAttr() const;
382
383  /// \brief Retrieve the alignment attribute, if it exists.
384  uint64_t getAlignment() const { return Alignment; }
385
386  /// \brief Retrieve the stack alignment attribute, if it exists.
387  uint64_t getStackAlignment() const { return StackAlignment; }
388
389  /// \brief This turns an int alignment (which must be a power of 2) into the
390  /// form used internally in Attribute.
391  AttrBuilder &addAlignmentAttr(unsigned Align);
392
393  /// \brief This turns an int stack alignment (which must be a power of 2) into
394  /// the form used internally in Attribute.
395  AttrBuilder &addStackAlignmentAttr(unsigned Align);
396
397  typedef DenseSet<Attribute::AttrKind>::iterator       iterator;
398  typedef DenseSet<Attribute::AttrKind>::const_iterator const_iterator;
399
400  iterator begin()             { return Attrs.begin(); }
401  iterator end()               { return Attrs.end(); }
402
403  const_iterator begin() const { return Attrs.begin(); }
404  const_iterator end() const   { return Attrs.end(); }
405
406  /// \brief Remove attributes that are used on functions only.
407  void removeFunctionOnlyAttrs() {
408    removeAttribute(Attribute::NoReturn)
409      .removeAttribute(Attribute::NoUnwind)
410      .removeAttribute(Attribute::ReadNone)
411      .removeAttribute(Attribute::ReadOnly)
412      .removeAttribute(Attribute::NoInline)
413      .removeAttribute(Attribute::AlwaysInline)
414      .removeAttribute(Attribute::OptimizeForSize)
415      .removeAttribute(Attribute::StackProtect)
416      .removeAttribute(Attribute::StackProtectReq)
417      .removeAttribute(Attribute::StackProtectStrong)
418      .removeAttribute(Attribute::NoRedZone)
419      .removeAttribute(Attribute::NoImplicitFloat)
420      .removeAttribute(Attribute::Naked)
421      .removeAttribute(Attribute::InlineHint)
422      .removeAttribute(Attribute::StackAlignment)
423      .removeAttribute(Attribute::UWTable)
424      .removeAttribute(Attribute::NonLazyBind)
425      .removeAttribute(Attribute::ReturnsTwice)
426      .removeAttribute(Attribute::AddressSafety)
427      .removeAttribute(Attribute::MinSize)
428      .removeAttribute(Attribute::NoDuplicate);
429  }
430
431  bool operator==(const AttrBuilder &B);
432  bool operator!=(const AttrBuilder &B) {
433    return !(*this == B);
434  }
435
436  // FIXME: Remove this in 4.0.
437
438  /// \brief Add the raw value to the internal representation.
439  AttrBuilder &addRawValue(uint64_t Val);
440};
441
442namespace AttributeFuncs {
443
444/// \brief Which attributes cannot be applied to a type.
445AttributeSet typeIncompatible(Type *Ty, uint64_t Index);
446
447} // end AttributeFuncs namespace
448
449} // end llvm namespace
450
451#endif
452