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