OSDN Git Service

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