Reorder some functions and add comments. 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   //===--------------------------------------------------------------------===//
111   // Attribute Construction
112   //===--------------------------------------------------------------------===//
113
114   /// \brief Return a uniquified Attribute object.
115   static Attribute get(LLVMContext &Context, AttrKind Kind);
116   static Attribute get(LLVMContext &Context, AttrBuilder &B);
117
118   /// \brief Return a uniquified Attribute object that has the specific
119   /// alignment set.
120   static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
121   static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
122
123   //===--------------------------------------------------------------------===//
124   // Attribute Accessors
125   //===--------------------------------------------------------------------===//
126
127   /// \brief Return true if the attribute is present.
128   bool hasAttribute(AttrKind Val) const;
129
130   /// \brief Return true if attributes exist
131   bool hasAttributes() const;
132
133   /// \brief Returns the alignment field of an attribute as a byte alignment
134   /// value.
135   unsigned getAlignment() const;
136
137   /// \brief Returns the stack alignment field of an attribute as a byte
138   /// alignment value.
139   unsigned getStackAlignment() const;
140
141   /// \brief The Attribute is converted to a string of equivalent mnemonic. This
142   /// is, presumably, for writing out the mnemonics for the assembly writer.
143   std::string getAsString() const;
144
145   /// \brief Equality and non-equality query methods.
146   bool operator==(AttrKind K) const;
147   bool operator!=(AttrKind K) const;
148
149   bool operator==(Attribute A) const { return pImpl == A.pImpl; }
150   bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
151
152   /// \brief Less-than operator. Useful for sorting the attributes list.
153   bool operator<(Attribute A) const;
154
155   void Profile(FoldingSetNodeID &ID) const {
156     ID.AddPointer(pImpl);
157   }
158
159   // FIXME: Remove this.
160   uint64_t Raw() const;
161 };
162
163 //===----------------------------------------------------------------------===//
164 // AttributeSet Smart Pointer
165 //===----------------------------------------------------------------------===//
166
167 class AttrBuilder;
168 class AttributeSetImpl;
169 class AttributeSetNode;
170
171 //===----------------------------------------------------------------------===//
172 /// \class
173 /// \brief This class manages the ref count for the opaque AttributeSetImpl
174 /// object and provides accessors for it.
175 class AttributeSet {
176 public:
177   enum AttrIndex {
178     ReturnIndex = 0U,
179     FunctionIndex = ~0U
180   };
181 private:
182   friend class AttrBuilder;
183   friend class AttributeSetImpl;
184
185   /// \brief The attributes that we are managing.  This can be null to represent
186   /// the empty attributes list.
187   AttributeSetImpl *pImpl;
188
189   /// \brief The attributes for the specified index are returned.  Attributes
190   /// for the result are denoted with Idx = 0.
191   Attribute getAttributes(unsigned Idx) const;
192
193   /// \brief Create an AttributeSet with the specified parameters in it.
194   static AttributeSet get(LLVMContext &C,
195                           ArrayRef<std::pair<unsigned, Attribute> > Attrs);
196   static AttributeSet get(LLVMContext &C,
197                           ArrayRef<std::pair<unsigned,
198                                              AttributeSetNode*> > Attrs);
199
200   static AttributeSet getImpl(LLVMContext &C,
201                               ArrayRef<std::pair<unsigned,
202                                                  AttributeSetNode*> > Attrs);
203
204
205   explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {}
206 public:
207   AttributeSet() : pImpl(0) {}
208   AttributeSet(const AttributeSet &P) : pImpl(P.pImpl) {}
209   const AttributeSet &operator=(const AttributeSet &RHS) {
210     pImpl = RHS.pImpl;
211     return *this;
212   }
213
214   //===--------------------------------------------------------------------===//
215   // AttributeSet Construction and Mutation
216   //===--------------------------------------------------------------------===//
217
218   /// \brief Return an AttributeSet with the specified parameters in it.
219   static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs);
220   static AttributeSet get(LLVMContext &C, unsigned Idx,
221                           ArrayRef<Attribute::AttrKind> Kind);
222   static AttributeSet get(LLVMContext &C, unsigned Idx, AttrBuilder &B);
223
224   /// \brief Add an attribute to the attribute set at the given index. Since
225   /// attribute sets are immutable, this returns a new set.
226   AttributeSet addAttribute(LLVMContext &C, unsigned Idx,
227                             Attribute::AttrKind Attr) const;
228
229   /// \brief Add attributes to the attribute set at the given index. Since
230   /// attribute sets are immutable, this returns a new set.
231   AttributeSet addAttributes(LLVMContext &C, unsigned Idx,
232                              AttributeSet Attrs) const;
233
234   /// \brief Add return attributes to this attribute set. Since attribute sets
235   /// are immutable, this returns a new set.
236   AttributeSet addRetAttributes(LLVMContext &C, AttributeSet Attrs) const {
237     return addAttributes(C, ReturnIndex, Attrs);
238   }
239
240   /// \brief Add function attributes to this attribute set. Since attribute sets
241   /// are immutable, this returns a new set.
242   AttributeSet addFnAttributes(LLVMContext &C, AttributeSet Attrs) const {
243     return addAttributes(C, FunctionIndex, Attrs);
244   }
245
246   /// \brief Remove the specified attribute at the specified index from this
247   /// attribute list. Since attribute lists are immutable, this returns the new
248   /// list.
249   AttributeSet removeAttribute(LLVMContext &C, unsigned Idx, 
250                                Attribute::AttrKind Attr) const;
251
252   /// \brief Remove the specified attributes at the specified index from this
253   /// attribute list. Since attribute lists are immutable, this returns the new
254   /// list.
255   AttributeSet removeAttributes(LLVMContext &C, unsigned Idx, 
256                                 AttributeSet Attrs) const;
257
258   //===--------------------------------------------------------------------===//
259   // AttributeSet Accessors
260   //===--------------------------------------------------------------------===//
261
262   /// \brief The attributes for the specified index are returned.
263   AttributeSet getParamAttributes(unsigned Idx) const;
264
265   /// \brief The attributes for the ret value are returned.
266   AttributeSet getRetAttributes() const;
267
268   /// \brief The function attributes are returned.
269   AttributeSet getFnAttributes() const;
270
271   /// \brief Return true if the attribute exists at the given index.
272   bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
273
274   /// \brief Return true if attribute exists at the given index.
275   bool hasAttributes(unsigned Index) const;
276
277   /// \brief Return true if the specified attribute is set for at least one
278   /// parameter or for the return value.
279   bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
280
281   /// \brief Return the alignment for the specified function parameter.
282   unsigned getParamAlignment(unsigned Idx) const;
283
284   /// \brief Get the stack alignment.
285   unsigned getStackAlignment(unsigned Index) const;
286
287   /// \brief Return the attributes at the index as a string.
288   std::string getAsString(unsigned Index) const;
289
290   /// operator==/!= - Provide equality predicates.
291   bool operator==(const AttributeSet &RHS) const {
292     return pImpl == RHS.pImpl;
293   }
294   bool operator!=(const AttributeSet &RHS) const {
295     return pImpl != RHS.pImpl;
296   }
297
298   //===--------------------------------------------------------------------===//
299   // AttributeSet Introspection
300   //===--------------------------------------------------------------------===//
301
302   // FIXME: Remove this.
303   uint64_t Raw(unsigned Index) const;
304
305   /// \brief Return a raw pointer that uniquely identifies this attribute list.
306   void *getRawPointer() const {
307     return pImpl;
308   }
309
310   /// \brief Return true if there are no attributes.
311   bool isEmpty() const {
312     return pImpl == 0;
313   }
314
315   /// \brief Return the number of slots used in this attribute list.  This is
316   /// the number of arguments that have an attribute set on them (including the
317   /// function itself).
318   unsigned getNumSlots() const;
319
320   /// \brief Return the index for the given slot.
321   uint64_t getSlotIndex(unsigned Slot) const;
322
323   /// \brief Return the attributes at the given slot.
324   AttributeSet getSlotAttributes(unsigned Slot) const;
325
326   void dump() const;
327 };
328
329 //===----------------------------------------------------------------------===//
330 /// \class
331 /// \brief Provide DenseMapInfo for Attribute::AttrKinds. This is used by
332 /// AttrBuilder.
333 template<> struct DenseMapInfo<Attribute::AttrKind> {
334   static inline Attribute::AttrKind getEmptyKey() {
335     return Attribute::AttrKindEmptyKey;
336   }
337   static inline Attribute::AttrKind getTombstoneKey() {
338     return Attribute::AttrKindTombstoneKey;
339   }
340   static unsigned getHashValue(const Attribute::AttrKind &Val) {
341     return Val * 37U;
342   }
343   static bool isEqual(const Attribute::AttrKind &LHS,
344                       const Attribute::AttrKind &RHS) {
345     return LHS == RHS;
346   }
347 };
348
349 //===----------------------------------------------------------------------===//
350 /// \class
351 /// \brief This class is used in conjunction with the Attribute::get method to
352 /// create an Attribute object. The object itself is uniquified. The Builder's
353 /// value, however, is not. So this can be used as a quick way to test for
354 /// equality, presence of attributes, etc.
355 class AttrBuilder {
356   DenseSet<Attribute::AttrKind> Attrs;
357   uint64_t Alignment;
358   uint64_t StackAlignment;
359 public:
360   AttrBuilder() : Alignment(0), StackAlignment(0) {}
361   explicit AttrBuilder(uint64_t B) : Alignment(0), StackAlignment(0) {
362     addRawValue(B);
363   }
364   AttrBuilder(const Attribute &A) : Alignment(0), StackAlignment(0) {
365     addAttributes(A);
366   }
367   AttrBuilder(AttributeSet AS, unsigned Idx);
368
369   void clear();
370
371   /// \brief Add an attribute to the builder.
372   AttrBuilder &addAttribute(Attribute::AttrKind Val);
373
374   /// \brief Remove an attribute from the builder.
375   AttrBuilder &removeAttribute(Attribute::AttrKind Val);
376
377   /// \brief Add the attributes to the builder.
378   AttrBuilder &addAttributes(Attribute A);
379
380   /// \brief Remove the attributes from the builder.
381   AttrBuilder &removeAttributes(Attribute A);
382
383   /// \brief Add the attributes to the builder.
384   AttrBuilder &addAttributes(AttributeSet A);
385
386   /// \brief Return true if the builder has the specified attribute.
387   bool contains(Attribute::AttrKind A) const;
388
389   /// \brief Return true if the builder has IR-level attributes.
390   bool hasAttributes() const;
391
392   /// \brief Return true if the builder has any attribute that's in the
393   /// specified attribute.
394   bool hasAttributes(const Attribute &A) const;
395
396   /// \brief Return true if the builder has an alignment attribute.
397   bool hasAlignmentAttr() const;
398
399   /// \brief Retrieve the alignment attribute, if it exists.
400   uint64_t getAlignment() const { return Alignment; }
401
402   /// \brief Retrieve the stack alignment attribute, if it exists.
403   uint64_t getStackAlignment() const { return StackAlignment; }
404
405   /// \brief This turns an int alignment (which must be a power of 2) into the
406   /// form used internally in Attribute.
407   AttrBuilder &addAlignmentAttr(unsigned Align);
408
409   /// \brief This turns an int stack alignment (which must be a power of 2) into
410   /// the form used internally in Attribute.
411   AttrBuilder &addStackAlignmentAttr(unsigned Align);
412
413   typedef DenseSet<Attribute::AttrKind>::iterator       iterator;
414   typedef DenseSet<Attribute::AttrKind>::const_iterator const_iterator;
415
416   iterator begin()             { return Attrs.begin(); }
417   iterator end()               { return Attrs.end(); }
418   const_iterator begin() const { return Attrs.begin(); }
419   const_iterator end() const   { return Attrs.end(); }
420
421   /// \brief Remove attributes that are used on functions only.
422   void removeFunctionOnlyAttrs() {
423     removeAttribute(Attribute::NoReturn)
424       .removeAttribute(Attribute::NoUnwind)
425       .removeAttribute(Attribute::ReadNone)
426       .removeAttribute(Attribute::ReadOnly)
427       .removeAttribute(Attribute::NoInline)
428       .removeAttribute(Attribute::AlwaysInline)
429       .removeAttribute(Attribute::OptimizeForSize)
430       .removeAttribute(Attribute::StackProtect)
431       .removeAttribute(Attribute::StackProtectReq)
432       .removeAttribute(Attribute::StackProtectStrong)
433       .removeAttribute(Attribute::NoRedZone)
434       .removeAttribute(Attribute::NoImplicitFloat)
435       .removeAttribute(Attribute::Naked)
436       .removeAttribute(Attribute::InlineHint)
437       .removeAttribute(Attribute::StackAlignment)
438       .removeAttribute(Attribute::UWTable)
439       .removeAttribute(Attribute::NonLazyBind)
440       .removeAttribute(Attribute::ReturnsTwice)
441       .removeAttribute(Attribute::AddressSafety)
442       .removeAttribute(Attribute::MinSize)
443       .removeAttribute(Attribute::NoDuplicate);
444   }
445
446   bool operator==(const AttrBuilder &B);
447   bool operator!=(const AttrBuilder &B) {
448     return !(*this == B);
449   }
450
451   // FIXME: Remove these.
452
453   /// \brief Add the raw value to the internal representation.
454   /// 
455   /// N.B. This should be used ONLY for decoding LLVM bitcode!
456   AttrBuilder &addRawValue(uint64_t Val);
457
458   uint64_t Raw() const;
459 };
460
461 namespace AttributeFuncs {
462
463 /// \brief Which attributes cannot be applied to a type.
464 Attribute typeIncompatible(Type *Ty);
465
466 /// \brief This returns an integer containing an encoding of all the LLVM
467 /// attributes found in the given attribute bitset.  Any change to this encoding
468 /// is a breaking change to bitcode compatibility.
469 uint64_t encodeLLVMAttributesForBitcode(AttributeSet Attrs, unsigned Index);
470
471 /// \brief This returns an attribute bitset containing the LLVM attributes that
472 /// have been decoded from the given integer.  This function must stay in sync
473 /// with 'encodeLLVMAttributesForBitcode'.
474 Attribute decodeLLVMAttributesForBitcode(LLVMContext &C,
475                                          uint64_t EncodedAttrs);
476
477 } // end AttributeFuncs namespace
478
479 } // end llvm namespace
480
481 #endif