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