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