Add 'nonnull', a new parameter and return attribute which indicates that the pointer...
[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     NonNull,               ///< Pointer is known to be not null
90     NoRedZone,             ///< Disable redzone
91     NoReturn,              ///< Mark the function as not returning
92     NoUnwind,              ///< Function doesn't unwind stack
93     OptimizeForSize,       ///< opt_size
94     OptimizeNone,          ///< Function must not be optimized.
95     ReadNone,              ///< Function does not access memory
96     ReadOnly,              ///< Function only reads from memory
97     Returned,              ///< Return value is always equal to this argument
98     ReturnsTwice,          ///< Function can return twice
99     SExt,                  ///< Sign extended before/after call
100     StackAlignment,        ///< Alignment of stack for function (3 bits)
101                            ///< stored as log2 of alignment with +1 bias 0
102                            ///< means unaligned (different from
103                            ///< alignstack=(1))
104     StackProtect,          ///< Stack protection.
105     StackProtectReq,       ///< Stack protection required.
106     StackProtectStrong,    ///< Strong Stack protection.
107     StructRet,             ///< Hidden pointer to structure to return
108     SanitizeAddress,       ///< AddressSanitizer is on.
109     SanitizeThread,        ///< ThreadSanitizer is on.
110     SanitizeMemory,        ///< MemorySanitizer is on.
111     UWTable,               ///< Function must be in a unwind table
112     ZExt,                  ///< Zero extended before/after call
113
114     EndAttrKinds           ///< Sentinal value useful for loops
115   };
116 private:
117   AttributeImpl *pImpl;
118   Attribute(AttributeImpl *A) : pImpl(A) {}
119 public:
120   Attribute() : pImpl(nullptr) {}
121
122   //===--------------------------------------------------------------------===//
123   // Attribute Construction
124   //===--------------------------------------------------------------------===//
125
126   /// \brief Return a uniquified Attribute object.
127   static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
128   static Attribute get(LLVMContext &Context, StringRef Kind,
129                        StringRef Val = StringRef());
130
131   /// \brief Return a uniquified Attribute object that has the specific
132   /// alignment set.
133   static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
134   static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
135
136   //===--------------------------------------------------------------------===//
137   // Attribute Accessors
138   //===--------------------------------------------------------------------===//
139
140   /// \brief Return true if the attribute is an Attribute::AttrKind type.
141   bool isEnumAttribute() const;
142
143   /// \brief Return true if the attribute is an alignment attribute.
144   bool isAlignAttribute() const;
145
146   /// \brief Return true if the attribute is a string (target-dependent)
147   /// attribute.
148   bool isStringAttribute() const;
149
150   /// \brief Return true if the attribute is present.
151   bool hasAttribute(AttrKind Val) const;
152
153   /// \brief Return true if the target-dependent attribute is present.
154   bool hasAttribute(StringRef Val) const;
155
156   /// \brief Return the attribute's kind as an enum (Attribute::AttrKind). This
157   /// requires the attribute to be an enum or alignment attribute.
158   Attribute::AttrKind getKindAsEnum() const;
159
160   /// \brief Return the attribute's value as an integer. This requires that the
161   /// attribute be an alignment attribute.
162   uint64_t getValueAsInt() const;
163
164   /// \brief Return the attribute's kind as a string. This requires the
165   /// attribute to be a string attribute.
166   StringRef getKindAsString() const;
167
168   /// \brief Return the attribute's value as a string. This requires the
169   /// attribute to be a string attribute.
170   StringRef getValueAsString() const;
171
172   /// \brief Returns the alignment field of an attribute as a byte alignment
173   /// value.
174   unsigned getAlignment() const;
175
176   /// \brief Returns the stack alignment field of an attribute as a byte
177   /// alignment value.
178   unsigned getStackAlignment() const;
179
180   /// \brief The Attribute is converted to a string of equivalent mnemonic. This
181   /// is, presumably, for writing out the mnemonics for the assembly writer.
182   std::string getAsString(bool InAttrGrp = false) const;
183
184   /// \brief Equality and non-equality operators.
185   bool operator==(Attribute A) const { return pImpl == A.pImpl; }
186   bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
187
188   /// \brief Less-than operator. Useful for sorting the attributes list.
189   bool operator<(Attribute A) const;
190
191   void Profile(FoldingSetNodeID &ID) const {
192     ID.AddPointer(pImpl);
193   }
194 };
195
196 //===----------------------------------------------------------------------===//
197 /// \class
198 /// \brief This class holds the attributes for a function, its return value, and
199 /// its parameters. You access the attributes for each of them via an index into
200 /// the AttributeSet object. The function attributes are at index
201 /// `AttributeSet::FunctionIndex', the return value is at index
202 /// `AttributeSet::ReturnIndex', and the attributes for the parameters start at
203 /// index `1'.
204 class AttributeSet {
205 public:
206   enum AttrIndex : unsigned {
207     ReturnIndex = 0U,
208     FunctionIndex = ~0U
209   };
210 private:
211   friend class AttrBuilder;
212   friend class AttributeSetImpl;
213   template <typename Ty> friend struct DenseMapInfo;
214
215   /// \brief The attributes that we are managing. This can be null to represent
216   /// the empty attributes list.
217   AttributeSetImpl *pImpl;
218
219   /// \brief The attributes for the specified index are returned.
220   AttributeSetNode *getAttributes(unsigned Index) const;
221
222   /// \brief Create an AttributeSet with the specified parameters in it.
223   static AttributeSet get(LLVMContext &C,
224                           ArrayRef<std::pair<unsigned, Attribute> > Attrs);
225   static AttributeSet get(LLVMContext &C,
226                           ArrayRef<std::pair<unsigned,
227                                              AttributeSetNode*> > Attrs);
228
229   static AttributeSet getImpl(LLVMContext &C,
230                               ArrayRef<std::pair<unsigned,
231                                                  AttributeSetNode*> > Attrs);
232
233
234   explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {}
235 public:
236   AttributeSet() : pImpl(nullptr) {}
237
238   //===--------------------------------------------------------------------===//
239   // AttributeSet Construction and Mutation
240   //===--------------------------------------------------------------------===//
241
242   /// \brief Return an AttributeSet with the specified parameters in it.
243   static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs);
244   static AttributeSet get(LLVMContext &C, unsigned Index,
245                           ArrayRef<Attribute::AttrKind> Kind);
246   static AttributeSet get(LLVMContext &C, unsigned Index, const AttrBuilder &B);
247
248   /// \brief Add an attribute to the attribute set at the given index. Since
249   /// attribute sets are immutable, this returns a new set.
250   AttributeSet addAttribute(LLVMContext &C, unsigned Index,
251                             Attribute::AttrKind Attr) const;
252
253   /// \brief Add an attribute to the attribute set at the given index. Since
254   /// attribute sets are immutable, this returns a new set.
255   AttributeSet addAttribute(LLVMContext &C, unsigned Index,
256                             StringRef Kind) const;
257   AttributeSet addAttribute(LLVMContext &C, unsigned Index,
258                             StringRef Kind, StringRef Value) const;
259
260   /// \brief Add attributes to the attribute set at the given index. Since
261   /// attribute sets are immutable, this returns a new set.
262   AttributeSet addAttributes(LLVMContext &C, unsigned Index,
263                              AttributeSet Attrs) const;
264
265   /// \brief Remove the specified attribute at the specified index from this
266   /// attribute list. Since attribute lists are immutable, this returns the new
267   /// list.
268   AttributeSet removeAttribute(LLVMContext &C, unsigned Index, 
269                                Attribute::AttrKind Attr) const;
270
271   /// \brief Remove the specified attributes at the specified index from this
272   /// attribute list. Since attribute lists are immutable, this returns the new
273   /// list.
274   AttributeSet removeAttributes(LLVMContext &C, unsigned Index, 
275                                 AttributeSet Attrs) const;
276
277   //===--------------------------------------------------------------------===//
278   // AttributeSet Accessors
279   //===--------------------------------------------------------------------===//
280
281   /// \brief Retrieve the LLVM context.
282   LLVMContext &getContext() const;
283
284   /// \brief The attributes for the specified index are returned.
285   AttributeSet getParamAttributes(unsigned Index) 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 true if the attribute exists at the given index.
294   bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
295
296   /// \brief Return true if the attribute exists at the given index.
297   bool hasAttribute(unsigned Index, StringRef Kind) const;
298
299   /// \brief Return true if attribute exists at the given index.
300   bool hasAttributes(unsigned Index) const;
301
302   /// \brief Return true if the specified attribute is set for at least one
303   /// parameter or for the return value.
304   bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
305
306   /// \brief Return the attribute object that exists at the given index.
307   Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
308
309   /// \brief Return the attribute object that exists at the given index.
310   Attribute getAttribute(unsigned Index, StringRef Kind) const;
311
312   /// \brief Return the alignment for the specified function parameter.
313   unsigned getParamAlignment(unsigned Index) const;
314
315   /// \brief Get the stack alignment.
316   unsigned getStackAlignment(unsigned Index) const;
317
318   /// \brief Return the attributes at the index as a string.
319   std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
320
321   typedef ArrayRef<Attribute>::iterator iterator;
322
323   iterator begin(unsigned Slot) const;
324   iterator end(unsigned Slot) const;
325
326   /// operator==/!= - Provide equality predicates.
327   bool operator==(const AttributeSet &RHS) const {
328     return pImpl == RHS.pImpl;
329   }
330   bool operator!=(const AttributeSet &RHS) const {
331     return pImpl != RHS.pImpl;
332   }
333
334   //===--------------------------------------------------------------------===//
335   // AttributeSet Introspection
336   //===--------------------------------------------------------------------===//
337
338   // FIXME: Remove this.
339   uint64_t Raw(unsigned Index) const;
340
341   /// \brief Return a raw pointer that uniquely identifies this attribute list.
342   void *getRawPointer() const {
343     return pImpl;
344   }
345
346   /// \brief Return true if there are no attributes.
347   bool isEmpty() const {
348     return getNumSlots() == 0;
349   }
350
351   /// \brief Return the number of slots used in this attribute list.  This is
352   /// the number of arguments that have an attribute set on them (including the
353   /// function itself).
354   unsigned getNumSlots() const;
355
356   /// \brief Return the index for the given slot.
357   unsigned getSlotIndex(unsigned Slot) const;
358
359   /// \brief Return the attributes at the given slot.
360   AttributeSet getSlotAttributes(unsigned Slot) const;
361
362   void dump() const;
363 };
364
365 //===----------------------------------------------------------------------===//
366 /// \class
367 /// \brief Provide DenseMapInfo for AttributeSet.
368 template<> struct DenseMapInfo<AttributeSet> {
369   static inline AttributeSet getEmptyKey() {
370     uintptr_t Val = static_cast<uintptr_t>(-1);
371     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
372     return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
373   }
374   static inline AttributeSet getTombstoneKey() {
375     uintptr_t Val = static_cast<uintptr_t>(-2);
376     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
377     return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
378   }
379   static unsigned getHashValue(AttributeSet AS) {
380     return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
381            (unsigned((uintptr_t)AS.pImpl) >> 9);
382   }
383   static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
384 };
385
386 //===----------------------------------------------------------------------===//
387 /// \class
388 /// \brief This class is used in conjunction with the Attribute::get method to
389 /// create an Attribute object. The object itself is uniquified. The Builder's
390 /// value, however, is not. So this can be used as a quick way to test for
391 /// equality, presence of attributes, etc.
392 class AttrBuilder {
393   std::bitset<Attribute::EndAttrKinds> Attrs;
394   std::map<std::string, std::string> TargetDepAttrs;
395   uint64_t Alignment;
396   uint64_t StackAlignment;
397 public:
398   AttrBuilder() : Attrs(0), Alignment(0), StackAlignment(0) {}
399   explicit AttrBuilder(uint64_t Val)
400     : Attrs(0), Alignment(0), StackAlignment(0) {
401     addRawValue(Val);
402   }
403   AttrBuilder(const Attribute &A) : Attrs(0), Alignment(0), StackAlignment(0) {
404     addAttribute(A);
405   }
406   AttrBuilder(AttributeSet AS, unsigned Idx);
407
408   void clear();
409
410   /// \brief Add an attribute to the builder.
411   AttrBuilder &addAttribute(Attribute::AttrKind Val);
412
413   /// \brief Add the Attribute object to the builder.
414   AttrBuilder &addAttribute(Attribute A);
415
416   /// \brief Add the target-dependent attribute to the builder.
417   AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
418
419   /// \brief Remove an attribute from the builder.
420   AttrBuilder &removeAttribute(Attribute::AttrKind Val);
421
422   /// \brief Remove the attributes from the builder.
423   AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index);
424
425   /// \brief Remove the target-dependent attribute to the builder.
426   AttrBuilder &removeAttribute(StringRef A);
427
428   /// \brief Add the attributes from the builder.
429   AttrBuilder &merge(const AttrBuilder &B);
430
431   /// \brief Return true if the builder has the specified attribute.
432   bool contains(Attribute::AttrKind A) const {
433     assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
434     return Attrs[A];
435   }
436
437   /// \brief Return true if the builder has the specified target-dependent
438   /// attribute.
439   bool contains(StringRef A) const;
440
441   /// \brief Return true if the builder has IR-level attributes.
442   bool hasAttributes() const;
443
444   /// \brief Return true if the builder has any attribute that's in the
445   /// specified attribute.
446   bool hasAttributes(AttributeSet A, uint64_t Index) const;
447
448   /// \brief Return true if the builder has an alignment attribute.
449   bool hasAlignmentAttr() const;
450
451   /// \brief Retrieve the alignment attribute, if it exists.
452   uint64_t getAlignment() const { return Alignment; }
453
454   /// \brief Retrieve the stack alignment attribute, if it exists.
455   uint64_t getStackAlignment() const { return StackAlignment; }
456
457   /// \brief This turns an int alignment (which must be a power of 2) into the
458   /// form used internally in Attribute.
459   AttrBuilder &addAlignmentAttr(unsigned Align);
460
461   /// \brief This turns an int stack alignment (which must be a power of 2) into
462   /// the form used internally in Attribute.
463   AttrBuilder &addStackAlignmentAttr(unsigned Align);
464
465   /// \brief Return true if the builder contains no target-independent
466   /// attributes.
467   bool empty() const { return Attrs.none(); }
468
469   // Iterators for target-dependent attributes.
470   typedef std::pair<std::string, std::string>                td_type;
471   typedef std::map<std::string, std::string>::iterator       td_iterator;
472   typedef std::map<std::string, std::string>::const_iterator td_const_iterator;
473   typedef llvm::iterator_range<td_iterator>                  td_range;
474   typedef llvm::iterator_range<td_const_iterator>            td_const_range;
475
476   td_iterator td_begin()             { return TargetDepAttrs.begin(); }
477   td_iterator td_end()               { return TargetDepAttrs.end(); }
478
479   td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
480   td_const_iterator td_end() const   { return TargetDepAttrs.end(); }
481
482   td_range td_attrs() { return td_range(td_begin(), td_end()); }
483   td_const_range td_attrs() const {
484     return td_const_range(td_begin(), td_end());
485   }
486
487   bool td_empty() const              { return TargetDepAttrs.empty(); }
488
489   bool operator==(const AttrBuilder &B);
490   bool operator!=(const AttrBuilder &B) {
491     return !(*this == B);
492   }
493
494   // FIXME: Remove this in 4.0.
495
496   /// \brief Add the raw value to the internal representation.
497   AttrBuilder &addRawValue(uint64_t Val);
498 };
499
500 namespace AttributeFuncs {
501
502 /// \brief Which attributes cannot be applied to a type.
503 AttributeSet typeIncompatible(Type *Ty, uint64_t Index);
504
505 } // end AttributeFuncs namespace
506
507 } // end llvm namespace
508
509 #endif