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