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