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