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