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