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