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