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