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