Have AttributeSet::getRetAttributes() return an AttributeSet instead of Attribute.
[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_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/Support/MathExtras.h"
22 #include <cassert>
23 #include <string>
24
25 namespace llvm {
26
27 class AttrBuilder;
28 class AttributeImpl;
29 class Constant;
30 class LLVMContext;
31 class Type;
32
33 //===----------------------------------------------------------------------===//
34 /// \class
35 /// \brief Functions, function parameters, and return types can have attributes
36 /// to indicate how they should be treated by optimizations and code
37 /// generation. This class represents one of those attributes. It's light-weight
38 /// and should be passed around by-value.
39 class Attribute {
40 public:
41   /// This enumeration lists the attributes that can be associated with
42   /// parameters, function results or the function itself.
43   ///
44   /// Note: uwtable is about the ABI or the user mandating an entry in the
45   /// unwind table. The nounwind attribute is about an exception passing by the
46   /// function.
47   ///
48   /// In a theoretical system that uses tables for profiling and sjlj for
49   /// exceptions, they would be fully independent. In a normal system that uses
50   /// tables for both, the semantics are:
51   ///
52   /// nil                = Needs an entry because an exception might pass by.
53   /// nounwind           = No need for an entry
54   /// uwtable            = Needs an entry because the ABI says so and because
55   ///                      an exception might pass by.
56   /// uwtable + nounwind = Needs an entry because the ABI says so.
57
58   enum AttrKind {
59     // IR-Level Attributes
60     None,                  ///< No attributes have been set
61     AddressSafety,         ///< Address safety checking is on.
62     Alignment,             ///< Alignment of parameter (5 bits)
63                            ///< stored as log2 of alignment with +1 bias
64                            ///< 0 means unaligned (different from align(1))
65     AlwaysInline,          ///< inline=always
66     ByVal,                 ///< Pass structure by value
67     InlineHint,            ///< Source said inlining was desirable
68     InReg,                 ///< Force argument to be passed in register
69     MinSize,               ///< Function must be optimized for size first
70     Naked,                 ///< Naked function
71     Nest,                  ///< Nested function static chain
72     NoAlias,               ///< Considered to not alias after call
73     NoCapture,             ///< Function creates no aliases of pointer
74     NoDuplicate,           ///< Call cannot be duplicated
75     NoImplicitFloat,       ///< Disable implicit floating point insts
76     NoInline,              ///< inline=never
77     NonLazyBind,           ///< Function is called early and/or
78                            ///< often, so lazy binding isn't worthwhile
79     NoRedZone,             ///< Disable redzone
80     NoReturn,              ///< Mark the function as not returning
81     NoUnwind,              ///< Function doesn't unwind stack
82     OptimizeForSize,       ///< opt_size
83     ReadNone,              ///< Function does not access memory
84     ReadOnly,              ///< Function only reads from memory
85     ReturnsTwice,          ///< Function can return twice
86     SExt,                  ///< Sign extended before/after call
87     StackAlignment,        ///< Alignment of stack for function (3 bits)
88                            ///< stored as log2 of alignment with +1 bias 0
89                            ///< means unaligned (different from
90                            ///< alignstack=(1))
91     StackProtect,          ///< Stack protection.
92     StackProtectReq,       ///< Stack protection required.
93     StructRet,             ///< Hidden pointer to structure to return
94     UWTable,               ///< Function must be in a unwind table
95     ZExt,                  ///< Zero extended before/after call
96
97     EndAttrKinds,          ///< Sentinal value useful for loops
98
99     AttrKindEmptyKey,      ///< Empty key value for DenseMapInfo
100     AttrKindTombstoneKey   ///< Tombstone key value for DenseMapInfo
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 Raw() 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::AttrKindEmptyKey;
173   }
174   static inline Attribute::AttrKind getTombstoneKey() {
175     return Attribute::AttrKindTombstoneKey;
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 // AttributeSet Smart Pointer
188 //===----------------------------------------------------------------------===//
189
190 class AttrBuilder;
191 class AttributeSetImpl;
192 struct AttributeWithIndex;
193
194 //===----------------------------------------------------------------------===//
195 /// \class
196 /// \brief This class manages the ref count for the opaque AttributeSetImpl
197 /// object and provides accessors for it.
198 class AttributeSet {
199 public:
200   enum AttrIndex {
201     ReturnIndex = 0U,
202     FunctionIndex = ~0U
203   };
204 private:
205   friend class AttrBuilder;
206
207   /// \brief The attributes that we are managing.  This can be null to represent
208   /// the empty attributes list.
209   AttributeSetImpl *AttrList;
210
211   /// \brief The attributes for the specified index are returned.  Attributes
212   /// for the result are denoted with Idx = 0.
213   Attribute getAttributes(unsigned Idx) const;
214
215   explicit AttributeSet(AttributeSetImpl *LI) : AttrList(LI) {}
216 public:
217   AttributeSet() : AttrList(0) {}
218   AttributeSet(const AttributeSet &P) : AttrList(P.AttrList) {}
219   const AttributeSet &operator=(const AttributeSet &RHS);
220
221   //===--------------------------------------------------------------------===//
222   // Attribute List Construction and Mutation
223   //===--------------------------------------------------------------------===//
224
225   /// \brief Return an AttributeSet with the specified parameters in it.
226   static AttributeSet get(LLVMContext &C, ArrayRef<AttributeWithIndex> Attrs);
227   static AttributeSet get(LLVMContext &C, unsigned Idx, AttrBuilder &B);
228
229   /// \brief Add the specified attribute at the specified index to this
230   /// attribute list.  Since attribute lists are immutable, this returns the new
231   /// list.
232   AttributeSet addAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const;
233
234   /// \brief Add return attributes to this attribute set. Since attribute sets
235   /// are immutable, this returns a new set.
236   AttributeSet addRetAttributes(LLVMContext &C, AttributeSet Attrs) const;
237
238   /// \brief Add function attributes to this attribute set. Since attribute sets
239   /// are immutable, this returns a new set.
240   AttributeSet addFnAttributes(LLVMContext &C, AttributeSet Attrs) const;
241
242   /// \brief Remove the specified attribute at the specified index from this
243   /// attribute list.  Since attribute lists are immutable, this returns the new
244   /// list.
245   AttributeSet removeAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const;
246
247   //===--------------------------------------------------------------------===//
248   // Attribute List Accessors
249   //===--------------------------------------------------------------------===//
250
251   /// \brief The attributes for the specified index are returned.
252   Attribute getParamAttributes(unsigned Idx) const {
253     return getAttributes(Idx);
254   }
255
256   /// \brief The attributes for the ret value are returned.
257   AttributeSet getRetAttributes() const;
258
259   /// \brief The function attributes are returned.
260   AttributeSet getFnAttributes() const;
261
262   /// \brief Return the alignment for the specified function parameter.
263   unsigned getParamAlignment(unsigned Idx) const;
264
265   /// \brief Return true if the attribute exists at the given index.
266   bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
267
268   /// \brief Return true if attribute exists at the given index.
269   bool hasAttributes(unsigned Index) const;
270
271   /// \brief Get the stack alignment.
272   unsigned getStackAlignment(unsigned Index) const;
273
274   /// \brief Return the attributes at the index as a string.
275   std::string getAsString(unsigned Index) const;
276
277   uint64_t Raw(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   /// operator==/!= - Provide equality predicates.
284   bool operator==(const AttributeSet &RHS) const {
285     return AttrList == RHS.AttrList;
286   }
287   bool operator!=(const AttributeSet &RHS) const {
288     return AttrList != RHS.AttrList;
289   }
290
291   //===--------------------------------------------------------------------===//
292   // Attribute List Introspection
293   //===--------------------------------------------------------------------===//
294
295   /// \brief Return a raw pointer that uniquely identifies this attribute list.
296   void *getRawPointer() const {
297     return AttrList;
298   }
299
300   // Attributes are stored as a dense set of slots, where there is one slot for
301   // each argument that has an attribute.  This allows walking over the dense
302   // set instead of walking the sparse list of attributes.
303
304   /// \brief Return true if there are no attributes.
305   bool isEmpty() const {
306     return AttrList == 0;
307   }
308
309   /// \brief Return the number of slots used in this attribute list.  This is
310   /// the number of arguments that have an attribute set on them (including the
311   /// function itself).
312   unsigned getNumSlots() const;
313
314   /// \brief Return the AttributeWithIndex at the specified slot.  This holds a
315   /// index number plus a set of attributes.
316   const AttributeWithIndex &getSlot(unsigned Slot) const;
317
318   void dump() const;
319 };
320
321 //===----------------------------------------------------------------------===//
322 /// \class
323 /// \brief This is just a pair of values to associate a set of attributes with
324 /// an index.
325 struct AttributeWithIndex {
326   Attribute Attrs;  ///< The attributes that are set, or'd together.
327   Constant *Val;    ///< Value attached to attribute, e.g. alignment.
328   unsigned Index;   ///< Index of the parameter for which the attributes apply.
329                     ///< Index 0 is used for return value attributes.
330                     ///< Index ~0U is used for function attributes.
331
332   // FIXME: These methods all need to be revised. The first one is temporary.
333   static AttributeWithIndex get(LLVMContext &C, unsigned Idx, AttributeSet AS);
334   static AttributeWithIndex get(LLVMContext &C, unsigned Idx,
335                                 ArrayRef<Attribute::AttrKind> Attrs) {
336     return get(Idx, Attribute::get(C, Attrs));
337   }
338   static AttributeWithIndex get(unsigned Idx, Attribute Attrs) {
339     AttributeWithIndex P;
340     P.Index = Idx;
341     P.Attrs = Attrs;
342     P.Val = 0;
343     return P;
344   }
345   static AttributeWithIndex get(unsigned Idx, Attribute Attrs, Constant *Val) {
346     AttributeWithIndex P;
347     P.Index = Idx;
348     P.Attrs = Attrs;
349     P.Val = Val;
350     return P;
351   }
352 };
353
354 //===----------------------------------------------------------------------===//
355 /// \class
356 /// \brief This class is used in conjunction with the Attribute::get method to
357 /// create an Attribute object. The object itself is uniquified. The Builder's
358 /// value, however, is not. So this can be used as a quick way to test for
359 /// equality, presence of attributes, etc.
360 class AttrBuilder {
361   DenseSet<Attribute::AttrKind> Attrs;
362   uint64_t Alignment;
363   uint64_t StackAlignment;
364 public:
365   AttrBuilder() : Alignment(0), StackAlignment(0) {}
366   explicit AttrBuilder(uint64_t B) : Alignment(0), StackAlignment(0) {
367     addRawValue(B);
368   }
369   AttrBuilder(const Attribute &A) : Alignment(0), StackAlignment(0) {
370     addAttributes(A);
371   }
372   AttrBuilder(AttributeSet AS, unsigned Idx);
373
374   void clear();
375
376   /// \brief Add an attribute to the builder.
377   AttrBuilder &addAttribute(Attribute::AttrKind Val);
378
379   /// \brief Remove an attribute from the builder.
380   AttrBuilder &removeAttribute(Attribute::AttrKind Val);
381
382   /// \brief Add the attributes from A to the builder.
383   AttrBuilder &addAttributes(const Attribute &A);
384
385   /// \brief Remove the attributes from A from the builder.
386   AttrBuilder &removeAttributes(const Attribute &A);
387
388   /// \brief Return true if the builder has the specified attribute.
389   bool contains(Attribute::AttrKind A) const;
390
391   /// \brief Return true if the builder has IR-level attributes.
392   bool hasAttributes() const;
393
394   /// \brief Return true if the builder has any attribute that's in the
395   /// specified attribute.
396   bool hasAttributes(const Attribute &A) const;
397
398   /// \brief Return true if the builder has an alignment attribute.
399   bool hasAlignmentAttr() const;
400
401   /// \brief Retrieve the alignment attribute, if it exists.
402   uint64_t getAlignment() const { return Alignment; }
403
404   /// \brief Retrieve the stack alignment attribute, if it exists.
405   uint64_t getStackAlignment() const { return StackAlignment; }
406
407   /// \brief This turns an int alignment (which must be a power of 2) into the
408   /// form used internally in Attribute.
409   AttrBuilder &addAlignmentAttr(unsigned Align);
410
411   /// \brief This turns an int stack alignment (which must be a power of 2) into
412   /// the form used internally in Attribute.
413   AttrBuilder &addStackAlignmentAttr(unsigned Align);
414
415   typedef DenseSet<Attribute::AttrKind>::iterator       iterator;
416   typedef DenseSet<Attribute::AttrKind>::const_iterator const_iterator;
417
418   iterator begin() { return Attrs.begin(); }
419   iterator end()   { return Attrs.end(); }
420
421   const_iterator begin() const { return Attrs.begin(); }
422   const_iterator end() const   { return Attrs.end(); }
423
424   /// \brief Add the raw value to the internal representation.
425   /// 
426   /// N.B. This should be used ONLY for decoding LLVM bitcode!
427   AttrBuilder &addRawValue(uint64_t Val);
428
429   /// \brief Remove attributes that are used on functions only.
430   void removeFunctionOnlyAttrs() {
431     removeAttribute(Attribute::NoReturn)
432       .removeAttribute(Attribute::NoUnwind)
433       .removeAttribute(Attribute::ReadNone)
434       .removeAttribute(Attribute::ReadOnly)
435       .removeAttribute(Attribute::NoInline)
436       .removeAttribute(Attribute::AlwaysInline)
437       .removeAttribute(Attribute::OptimizeForSize)
438       .removeAttribute(Attribute::StackProtect)
439       .removeAttribute(Attribute::StackProtectReq)
440       .removeAttribute(Attribute::NoRedZone)
441       .removeAttribute(Attribute::NoImplicitFloat)
442       .removeAttribute(Attribute::Naked)
443       .removeAttribute(Attribute::InlineHint)
444       .removeAttribute(Attribute::StackAlignment)
445       .removeAttribute(Attribute::UWTable)
446       .removeAttribute(Attribute::NonLazyBind)
447       .removeAttribute(Attribute::ReturnsTwice)
448       .removeAttribute(Attribute::AddressSafety)
449       .removeAttribute(Attribute::MinSize)
450       .removeAttribute(Attribute::NoDuplicate);
451   }
452
453   uint64_t Raw() const;
454
455   bool operator==(const AttrBuilder &B);
456   bool operator!=(const AttrBuilder &B) {
457     return !(*this == B);
458   }
459 };
460
461 } // end llvm namespace
462
463 #endif