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