01b3be8cb60f5df0abea154053b9cd2b5960a3c4
[oota-llvm.git] / include / llvm / 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 // This file contains the simple types necessary to represent the
11 // attributes associated with functions and their calls.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_ATTRIBUTES_H
16 #define LLVM_ATTRIBUTES_H
17
18 #include "llvm/Support/MathExtras.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include <cassert>
21 #include <string>
22
23 namespace llvm {
24
25 class Type;
26
27 namespace Attribute {
28
29 /// We use this proxy POD type to allow constructing Attributes constants using
30 /// initializer lists. Do not use this class directly.
31 struct AttrConst {
32   uint64_t v;
33   AttrConst operator | (const AttrConst Attrs) const {
34     AttrConst Res = {v | Attrs.v};
35     return Res;
36   }
37   AttrConst operator ~ () const {
38     AttrConst Res = {~v};
39     return Res;
40   }
41 };
42
43 /// Function parameters and results can have attributes to indicate how they
44 /// should be treated by optimizations and code generation. This enumeration
45 /// lists the attributes that can be associated with parameters, function
46 /// results or the function itself.
47 /// @brief Function attributes.
48
49 /// We declare AttrConst objects that will be used throughout the code and also
50 /// raw uint64_t objects with _i suffix to be used below for other constant
51 /// declarations. This is done to avoid static CTORs and at the same time to
52 /// keep type-safety of Attributes.
53 #define DECLARE_LLVM_ATTRIBUTE(name, value) \
54   const uint64_t name##_i = value; \
55   const AttrConst name = {value};
56
57 DECLARE_LLVM_ATTRIBUTE(None,0)    ///< No attributes have been set
58 DECLARE_LLVM_ATTRIBUTE(ZExt,1<<0) ///< Zero extended before/after call
59 DECLARE_LLVM_ATTRIBUTE(SExt,1<<1) ///< Sign extended before/after call
60 DECLARE_LLVM_ATTRIBUTE(NoReturn,1<<2) ///< Mark the function as not returning
61 DECLARE_LLVM_ATTRIBUTE(InReg,1<<3) ///< Force argument to be passed in register
62 DECLARE_LLVM_ATTRIBUTE(StructRet,1<<4) ///< Hidden pointer to structure to return
63 DECLARE_LLVM_ATTRIBUTE(NoUnwind,1<<5) ///< Function doesn't unwind stack
64 DECLARE_LLVM_ATTRIBUTE(NoAlias,1<<6) ///< Considered to not alias after call
65 DECLARE_LLVM_ATTRIBUTE(ByVal,1<<7) ///< Pass structure by value
66 DECLARE_LLVM_ATTRIBUTE(Nest,1<<8) ///< Nested function static chain
67 DECLARE_LLVM_ATTRIBUTE(ReadNone,1<<9) ///< Function does not access memory
68 DECLARE_LLVM_ATTRIBUTE(ReadOnly,1<<10) ///< Function only reads from memory
69 DECLARE_LLVM_ATTRIBUTE(NoInline,1<<11) ///< inline=never
70 DECLARE_LLVM_ATTRIBUTE(AlwaysInline,1<<12) ///< inline=always
71 DECLARE_LLVM_ATTRIBUTE(OptimizeForSize,1<<13) ///< opt_size
72 DECLARE_LLVM_ATTRIBUTE(StackProtect,1<<14) ///< Stack protection.
73 DECLARE_LLVM_ATTRIBUTE(StackProtectReq,1<<15) ///< Stack protection required.
74 DECLARE_LLVM_ATTRIBUTE(Alignment,31<<16) ///< Alignment of parameter (5 bits)
75                                      // stored as log2 of alignment with +1 bias
76                                      // 0 means unaligned different from align 1
77 DECLARE_LLVM_ATTRIBUTE(NoCapture,1<<21) ///< Function creates no aliases of pointer
78 DECLARE_LLVM_ATTRIBUTE(NoRedZone,1<<22) /// disable redzone
79 DECLARE_LLVM_ATTRIBUTE(NoImplicitFloat,1<<23) /// disable implicit floating point
80                                            /// instructions.
81 DECLARE_LLVM_ATTRIBUTE(Naked,1<<24) ///< Naked function
82 DECLARE_LLVM_ATTRIBUTE(InlineHint,1<<25) ///< source said inlining was
83                                            ///desirable
84 DECLARE_LLVM_ATTRIBUTE(StackAlignment,7<<26) ///< Alignment of stack for
85                                            ///function (3 bits) stored as log2
86                                            ///of alignment with +1 bias
87                                            ///0 means unaligned (different from
88                                            ///alignstack= {1))
89 DECLARE_LLVM_ATTRIBUTE(ReturnsTwice,1<<29) ///< Function can return twice
90 DECLARE_LLVM_ATTRIBUTE(UWTable,1<<30) ///< Function must be in a unwind
91                                            ///table
92 DECLARE_LLVM_ATTRIBUTE(NonLazyBind,1U<<31) ///< Function is called early and/or
93                                             /// often, so lazy binding isn't
94                                             /// worthwhile.
95 DECLARE_LLVM_ATTRIBUTE(AddressSafety,1ULL<<32) ///< Address safety checking is on.
96
97 #undef DECLARE_LLVM_ATTRIBUTE
98
99 }  // namespace Attribute
100
101 /// Attributes - A bitset of attributes.
102 class Attributes {
103   // Currently, we need less than 64 bits.
104   uint64_t Bits;
105 public:
106   Attributes() : Bits(0) { }
107   explicit Attributes(uint64_t Val) : Bits(Val) { }
108   /*implicit*/ Attributes(Attribute::AttrConst Val) : Bits(Val.v) { }
109
110   // Attribute query methods.
111   // FIXME: StackAlignment & Alignment attributes have no predicate methods.
112   bool hasAttributes() const {
113     return Bits != 0;
114   }
115   bool hasAttributes(const Attributes &A) const {
116     return Bits & A.Bits;
117   }
118
119   bool hasZExtAttr() const {
120     return Bits & Attribute::ZExt_i;
121   }
122   bool hasSExtAttr() const {
123     return Bits & Attribute::SExt_i;
124   }
125   bool hasNoReturnAttr() const {
126     return Bits & Attribute::NoReturn_i;
127   }
128   bool hasInRegAttr() const {
129     return Bits & Attribute::InReg_i;
130   }
131   bool hasStructRetAttr() const {
132     return Bits & Attribute::StructRet_i;
133   }
134   bool hasNoUnwindAttr() const {
135     return Bits & Attribute::NoUnwind_i;
136   }
137   bool hasNoAliasAttr() const {
138     return Bits & Attribute::NoAlias_i;
139   }
140   bool hasByValAttr() const {
141     return Bits & Attribute::ByVal_i;
142   }
143   bool hasNestAttr() const {
144     return Bits & Attribute::Nest_i;
145   }
146   bool hasReadNoneAttr() const {
147     return Bits & Attribute::ReadNone_i;
148   }
149   bool hasReadOnlyAttr() const {
150     return Bits & Attribute::ReadOnly_i;
151   }
152   bool hasNoInlineAttr() const {
153     return Bits & Attribute::NoInline_i;
154   }
155   bool hasAlwaysInlineAttr() const {
156     return Bits & Attribute::AlwaysInline_i;
157   }
158   bool hasOptimizeForSizeAttr() const {
159     return Bits & Attribute::OptimizeForSize_i;
160   }
161   bool hasStackProtectAttr() const {
162     return Bits & Attribute::StackProtect_i;
163   }
164   bool hasStackProtectReqAttr() const {
165     return Bits & Attribute::StackProtectReq_i;
166   }
167   bool hasAlignmentAttr() const {
168     return Bits & Attribute::Alignment_i;
169   }
170   bool hasNoCaptureAttr() const {
171     return Bits & Attribute::NoCapture_i;
172   }
173   bool hasNoRedZoneAttr() const {
174     return Bits & Attribute::NoRedZone_i;
175   }
176   bool hasNoImplicitFloatAttr() const {
177     return Bits & Attribute::NoImplicitFloat_i;
178   }
179   bool hasNakedAttr() const {
180     return Bits & Attribute::Naked_i;
181   }
182   bool hasInlineHintAttr() const {
183     return Bits & Attribute::InlineHint_i;
184   }
185   bool hasReturnsTwiceAttr() const {
186     return Bits & Attribute::ReturnsTwice_i;
187   }
188   bool hasStackAlignmentAttr() const {
189     return Bits & Attribute::StackAlignment_i;
190   }
191   bool hasUWTableAttr() const {
192     return Bits & Attribute::UWTable_i;
193   }
194   bool hasNonLazyBindAttr() const {
195     return Bits & Attribute::NonLazyBind_i;
196   }
197   bool hasAddressSafetyAttr() const {
198     return Bits & Attribute::AddressSafety_i;
199   }
200
201   uint64_t getRawAlignment() const {
202     return Bits & Attribute::Alignment_i;
203   }
204   uint64_t getRawStackAlignment() const {
205     return Bits & Attribute::StackAlignment_i;
206   }
207
208   // This is a "safe bool() operator".
209   operator const void *() const { return Bits ? this : 0; }
210   bool isEmptyOrSingleton() const { return (Bits & (Bits - 1)) == 0; }
211   bool operator == (const Attributes &Attrs) const {
212     return Bits == Attrs.Bits;
213   }
214   bool operator != (const Attributes &Attrs) const {
215     return Bits != Attrs.Bits;
216   }
217   Attributes operator | (const Attributes &Attrs) const {
218     return Attributes(Bits | Attrs.Bits);
219   }
220   Attributes operator & (const Attributes &Attrs) const {
221     return Attributes(Bits & Attrs.Bits);
222   }
223   Attributes operator ^ (const Attributes &Attrs) const {
224     return Attributes(Bits ^ Attrs.Bits);
225   }
226   Attributes &operator |= (const Attributes &Attrs) {
227     Bits |= Attrs.Bits;
228     return *this;
229   }
230   Attributes &operator &= (const Attributes &Attrs) {
231     Bits &= Attrs.Bits;
232     return *this;
233   }
234   Attributes operator ~ () const { return Attributes(~Bits); }
235   uint64_t Raw() const { return Bits; }
236
237   /// The set of Attributes set in Attributes is converted to a string of
238   /// equivalent mnemonics. This is, presumably, for writing out the mnemonics
239   /// for the assembly writer.
240   /// @brief Convert attribute bits to text
241   std::string getAsString() const;
242 };
243
244 namespace Attribute {
245
246 /// Note that uwtable is about the ABI or the user mandating an entry in the
247 /// unwind table. The nounwind attribute is about an exception passing by the
248 /// function.
249 /// In a theoretical system that uses tables for profiling and sjlj for
250 /// exceptions, they would be fully independent. In a normal system that
251 /// uses tables for both, the semantics are:
252 /// nil                = Needs an entry because an exception might pass by.
253 /// nounwind           = No need for an entry
254 /// uwtable            = Needs an entry because the ABI says so and because
255 ///                      an exception might pass by.
256 /// uwtable + nounwind = Needs an entry because the ABI says so.
257
258 /// @brief Attributes that only apply to function parameters.
259 const AttrConst ParameterOnly = {ByVal_i | Nest_i |
260     StructRet_i | NoCapture_i};
261
262 /// @brief Attributes that may be applied to the function itself.  These cannot
263 /// be used on return values or function parameters.
264 const AttrConst FunctionOnly = {NoReturn_i | NoUnwind_i | ReadNone_i |
265   ReadOnly_i | NoInline_i | AlwaysInline_i | OptimizeForSize_i |
266   StackProtect_i | StackProtectReq_i | NoRedZone_i | NoImplicitFloat_i |
267   Naked_i | InlineHint_i | StackAlignment_i |
268   UWTable_i | NonLazyBind_i | ReturnsTwice_i | AddressSafety_i};
269
270 /// @brief Parameter attributes that do not apply to vararg call arguments.
271 const AttrConst VarArgsIncompatible = {StructRet_i};
272
273 /// @brief Attributes that are mutually incompatible.
274 const AttrConst MutuallyIncompatible[5] = {
275   {ByVal_i | Nest_i | StructRet_i},
276   {ByVal_i | Nest_i | InReg_i },
277   {ZExt_i  | SExt_i},
278   {ReadNone_i | ReadOnly_i},
279   {NoInline_i | AlwaysInline_i}
280 };
281
282 /// @brief Which attributes cannot be applied to a type.
283 Attributes typeIncompatible(Type *Ty);
284
285 /// This turns an int alignment (a power of 2, normally) into the
286 /// form used internally in Attributes.
287 inline Attributes constructAlignmentFromInt(unsigned i) {
288   // Default alignment, allow the target to define how to align it.
289   if (i == 0)
290     return None;
291
292   assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
293   assert(i <= 0x40000000 && "Alignment too large.");
294   return Attributes((Log2_32(i)+1) << 16);
295 }
296
297 /// This returns the alignment field of an attribute as a byte alignment value.
298 inline unsigned getAlignmentFromAttrs(Attributes A) {
299   if (!A.hasAlignmentAttr())
300     return 0;
301
302   return 1U << ((A.getRawAlignment() >> 16) - 1);
303 }
304
305 /// This turns an int stack alignment (which must be a power of 2) into
306 /// the form used internally in Attributes.
307 inline Attributes constructStackAlignmentFromInt(unsigned i) {
308   // Default alignment, allow the target to define how to align it.
309   if (i == 0)
310     return None;
311
312   assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
313   assert(i <= 0x100 && "Alignment too large.");
314   return Attributes((Log2_32(i)+1) << 26);
315 }
316
317 /// This returns the stack alignment field of an attribute as a byte alignment
318 /// value.
319 inline unsigned getStackAlignmentFromAttrs(Attributes A) {
320   if (!A.hasStackAlignmentAttr())
321     return 0;
322
323   return 1U << ((A.getRawStackAlignment() >> 26) - 1);
324 }
325
326 /// This returns an integer containing an encoding of all the
327 /// LLVM attributes found in the given attribute bitset.  Any
328 /// change to this encoding is a breaking change to bitcode
329 /// compatibility.
330 inline uint64_t encodeLLVMAttributesForBitcode(Attributes Attrs) {
331   // FIXME: It doesn't make sense to store the alignment information as an
332   // expanded out value, we should store it as a log2 value.  However, we can't
333   // just change that here without breaking bitcode compatibility.  If this ever
334   // becomes a problem in practice, we should introduce new tag numbers in the
335   // bitcode file and have those tags use a more efficiently encoded alignment
336   // field.
337
338   // Store the alignment in the bitcode as a 16-bit raw value instead of a
339   // 5-bit log2 encoded value. Shift the bits above the alignment up by
340   // 11 bits.
341
342   uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
343   if (Attrs.hasAlignmentAttr())
344     EncodedAttrs |= (1ull << 16) <<
345       ((Attrs.getRawAlignment() - 1) >> 16);
346   EncodedAttrs |= (Attrs.Raw() & (0xfffull << 21)) << 11;
347
348   return EncodedAttrs;
349 }
350
351 /// This returns an attribute bitset containing the LLVM attributes
352 /// that have been decoded from the given integer.  This function
353 /// must stay in sync with 'encodeLLVMAttributesForBitcode'.
354 inline Attributes decodeLLVMAttributesForBitcode(uint64_t EncodedAttrs) {
355   // The alignment is stored as a 16-bit raw value from bits 31--16.
356   // We shift the bits above 31 down by 11 bits.
357
358   unsigned Alignment = (EncodedAttrs & (0xffffull << 16)) >> 16;
359   assert((!Alignment || isPowerOf2_32(Alignment)) &&
360          "Alignment must be a power of two.");
361
362   Attributes Attrs(EncodedAttrs & 0xffff);
363   if (Alignment)
364     Attrs |= Attribute::constructAlignmentFromInt(Alignment);
365   Attrs |= Attributes((EncodedAttrs & (0xfffull << 32)) >> 11);
366
367   return Attrs;
368 }
369
370 } // end namespace Attribute
371
372 /// This is just a pair of values to associate a set of attributes
373 /// with an index.
374 struct AttributeWithIndex {
375   Attributes Attrs; ///< The attributes that are set, or'd together.
376   unsigned Index; ///< Index of the parameter for which the attributes apply.
377                   ///< Index 0 is used for return value attributes.
378                   ///< Index ~0U is used for function attributes.
379
380   static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
381     AttributeWithIndex P;
382     P.Index = Idx;
383     P.Attrs = Attrs;
384     return P;
385   }
386 };
387
388 //===----------------------------------------------------------------------===//
389 // AttrListPtr Smart Pointer
390 //===----------------------------------------------------------------------===//
391
392 class AttributeListImpl;
393
394 /// AttrListPtr - This class manages the ref count for the opaque
395 /// AttributeListImpl object and provides accessors for it.
396 class AttrListPtr {
397   /// AttrList - The attributes that we are managing.  This can be null
398   /// to represent the empty attributes list.
399   AttributeListImpl *AttrList;
400 public:
401   AttrListPtr() : AttrList(0) {}
402   AttrListPtr(const AttrListPtr &P);
403   const AttrListPtr &operator=(const AttrListPtr &RHS);
404   ~AttrListPtr();
405
406   //===--------------------------------------------------------------------===//
407   // Attribute List Construction and Mutation
408   //===--------------------------------------------------------------------===//
409
410   /// get - Return a Attributes list with the specified parameters in it.
411   static AttrListPtr get(ArrayRef<AttributeWithIndex> Attrs);
412
413   /// addAttr - Add the specified attribute at the specified index to this
414   /// attribute list.  Since attribute lists are immutable, this
415   /// returns the new list.
416   AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
417
418   /// removeAttr - Remove the specified attribute at the specified index from
419   /// this attribute list.  Since attribute lists are immutable, this
420   /// returns the new list.
421   AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
422
423   //===--------------------------------------------------------------------===//
424   // Attribute List Accessors
425   //===--------------------------------------------------------------------===//
426   /// getParamAttributes - The attributes for the specified index are
427   /// returned.
428   Attributes getParamAttributes(unsigned Idx) const {
429     assert (Idx && Idx != ~0U && "Invalid parameter index!");
430     return getAttributes(Idx);
431   }
432
433   /// getRetAttributes - The attributes for the ret value are
434   /// returned.
435   Attributes getRetAttributes() const {
436     return getAttributes(0);
437   }
438
439   /// getFnAttributes - The function attributes are returned.
440   Attributes getFnAttributes() const {
441     return getAttributes(~0U);
442   }
443
444   /// paramHasAttr - Return true if the specified parameter index has the
445   /// specified attribute set.
446   bool paramHasAttr(unsigned Idx, Attributes Attr) const {
447     return getAttributes(Idx).hasAttributes(Attr);
448   }
449
450   /// getParamAlignment - Return the alignment for the specified function
451   /// parameter.
452   unsigned getParamAlignment(unsigned Idx) const {
453     return Attribute::getAlignmentFromAttrs(getAttributes(Idx));
454   }
455
456   /// hasAttrSomewhere - Return true if the specified attribute is set for at
457   /// least one parameter or for the return value.
458   bool hasAttrSomewhere(Attributes Attr) const;
459
460   /// operator==/!= - Provide equality predicates.
461   bool operator==(const AttrListPtr &RHS) const
462   { return AttrList == RHS.AttrList; }
463   bool operator!=(const AttrListPtr &RHS) const
464   { return AttrList != RHS.AttrList; }
465
466   void dump() const;
467
468   //===--------------------------------------------------------------------===//
469   // Attribute List Introspection
470   //===--------------------------------------------------------------------===//
471
472   /// getRawPointer - Return a raw pointer that uniquely identifies this
473   /// attribute list.
474   void *getRawPointer() const {
475     return AttrList;
476   }
477
478   // Attributes are stored as a dense set of slots, where there is one
479   // slot for each argument that has an attribute.  This allows walking over the
480   // dense set instead of walking the sparse list of attributes.
481
482   /// isEmpty - Return true if there are no attributes.
483   ///
484   bool isEmpty() const {
485     return AttrList == 0;
486   }
487
488   /// getNumSlots - Return the number of slots used in this attribute list.
489   /// This is the number of arguments that have an attribute set on them
490   /// (including the function itself).
491   unsigned getNumSlots() const;
492
493   /// getSlot - Return the AttributeWithIndex at the specified slot.  This
494   /// holds a index number plus a set of attributes.
495   const AttributeWithIndex &getSlot(unsigned Slot) const;
496
497 private:
498   explicit AttrListPtr(AttributeListImpl *L);
499
500   /// getAttributes - The attributes for the specified index are
501   /// returned.  Attributes for the result are denoted with Idx = 0.
502   Attributes getAttributes(unsigned Idx) const;
503
504 };
505
506 } // End llvm namespace
507
508 #endif