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