Provide a way to specify inliner's attribute compatibility and merging
[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/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 Function;
37 class LLVMContext;
38 class Type;
39
40 //===----------------------------------------------------------------------===//
41 /// \class
42 /// \brief Functions, function parameters, and return types can have attributes
43 /// to indicate how they should be treated by optimizations and code
44 /// generation. This class represents one of those attributes. It's light-weight
45 /// and should be passed around by-value.
46 class Attribute {
47 public:
48   /// This enumeration lists the attributes that can be associated with
49   /// parameters, function results, or the function itself.
50   ///
51   /// Note: The `uwtable' attribute is about the ABI or the user mandating an
52   /// entry in the unwind table. The `nounwind' attribute is about an exception
53   /// passing by the function.
54   ///
55   /// In a theoretical system that uses tables for profiling and SjLj for
56   /// exceptions, they would be fully independent. In a normal system that uses
57   /// tables for both, the semantics are:
58   ///
59   /// nil                = Needs an entry because an exception might pass by.
60   /// nounwind           = No need for an entry
61   /// uwtable            = Needs an entry because the ABI says so and because
62   ///                      an exception might pass by.
63   /// uwtable + nounwind = Needs an entry because the ABI says so.
64
65   enum AttrKind {
66     // IR-Level Attributes
67     None,                  ///< No attributes have been set
68     #define GET_ATTR_ENUM
69     #include "llvm/IR/Attributes.inc"
70     EndAttrKinds           ///< Sentinal value useful for loops
71   };
72
73 private:
74   AttributeImpl *pImpl;
75   Attribute(AttributeImpl *A) : pImpl(A) {}
76
77 public:
78   Attribute() : pImpl(nullptr) {}
79
80   //===--------------------------------------------------------------------===//
81   // Attribute Construction
82   //===--------------------------------------------------------------------===//
83
84   /// \brief Return a uniquified Attribute object.
85   static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
86   static Attribute get(LLVMContext &Context, StringRef Kind,
87                        StringRef Val = StringRef());
88
89   /// \brief Return a uniquified Attribute object that has the specific
90   /// alignment set.
91   static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
92   static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
93   static Attribute getWithDereferenceableBytes(LLVMContext &Context,
94                                               uint64_t Bytes);
95   static Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context,
96                                                      uint64_t Bytes);
97
98   //===--------------------------------------------------------------------===//
99   // Attribute Accessors
100   //===--------------------------------------------------------------------===//
101
102   /// \brief Return true if the attribute is an Attribute::AttrKind type.
103   bool isEnumAttribute() const;
104
105   /// \brief Return true if the attribute is an integer attribute.
106   bool isIntAttribute() const;
107
108   /// \brief Return true if the attribute is a string (target-dependent)
109   /// attribute.
110   bool isStringAttribute() const;
111
112   /// \brief Return true if the attribute is present.
113   bool hasAttribute(AttrKind Val) const;
114
115   /// \brief Return true if the target-dependent attribute is present.
116   bool hasAttribute(StringRef Val) const;
117
118   /// \brief Return the attribute's kind as an enum (Attribute::AttrKind). This
119   /// requires the attribute to be an enum or alignment attribute.
120   Attribute::AttrKind getKindAsEnum() const;
121
122   /// \brief Return the attribute's value as an integer. This requires that the
123   /// attribute be an alignment attribute.
124   uint64_t getValueAsInt() const;
125
126   /// \brief Return the attribute's kind as a string. This requires the
127   /// attribute to be a string attribute.
128   StringRef getKindAsString() const;
129
130   /// \brief Return the attribute's value as a string. This requires the
131   /// attribute to be a string attribute.
132   StringRef getValueAsString() const;
133
134   /// \brief Returns the alignment field of an attribute as a byte alignment
135   /// value.
136   unsigned getAlignment() const;
137
138   /// \brief Returns the stack alignment field of an attribute as a byte
139   /// alignment value.
140   unsigned getStackAlignment() const;
141
142   /// \brief Returns the number of dereferenceable bytes from the
143   /// dereferenceable attribute (or zero if unknown).
144   uint64_t getDereferenceableBytes() const;
145
146   /// \brief Returns the number of dereferenceable_or_null bytes from the
147   /// dereferenceable_or_null attribute (or zero if unknown).
148   uint64_t getDereferenceableOrNullBytes() const;
149
150   /// \brief The Attribute is converted to a string of equivalent mnemonic. This
151   /// is, presumably, for writing out the mnemonics for the assembly writer.
152   std::string getAsString(bool InAttrGrp = false) const;
153
154   /// \brief Equality and non-equality operators.
155   bool operator==(Attribute A) const { return pImpl == A.pImpl; }
156   bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
157
158   /// \brief Less-than operator. Useful for sorting the attributes list.
159   bool operator<(Attribute A) const;
160
161   void Profile(FoldingSetNodeID &ID) const {
162     ID.AddPointer(pImpl);
163   }
164 };
165
166 //===----------------------------------------------------------------------===//
167 /// \class
168 /// \brief This class holds the attributes for a function, its return value, and
169 /// its parameters. You access the attributes for each of them via an index into
170 /// the AttributeSet object. The function attributes are at index
171 /// `AttributeSet::FunctionIndex', the return value is at index
172 /// `AttributeSet::ReturnIndex', and the attributes for the parameters start at
173 /// index `1'.
174 class AttributeSet {
175 public:
176   enum AttrIndex : unsigned {
177     ReturnIndex = 0U,
178     FunctionIndex = ~0U
179   };
180
181 private:
182   friend class AttrBuilder;
183   friend class AttributeSetImpl;
184   template <typename Ty> friend struct DenseMapInfo;
185
186   /// \brief The attributes that we are managing. This can be null to represent
187   /// the empty attributes list.
188   AttributeSetImpl *pImpl;
189
190   /// \brief The attributes for the specified index are returned.
191   AttributeSetNode *getAttributes(unsigned Index) 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   explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {}
205
206 public:
207   AttributeSet() : pImpl(nullptr) {}
208
209   //===--------------------------------------------------------------------===//
210   // AttributeSet Construction and Mutation
211   //===--------------------------------------------------------------------===//
212
213   /// \brief Return an AttributeSet with the specified parameters in it.
214   static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs);
215   static AttributeSet get(LLVMContext &C, unsigned Index,
216                           ArrayRef<Attribute::AttrKind> Kind);
217   static AttributeSet get(LLVMContext &C, unsigned Index, const AttrBuilder &B);
218
219   /// \brief Add an attribute to the attribute set at the given index. Because
220   /// attribute sets are immutable, this returns a new set.
221   AttributeSet addAttribute(LLVMContext &C, unsigned Index,
222                             Attribute::AttrKind Attr) const;
223
224   /// \brief Add an attribute to the attribute set at the given index. Because
225   /// attribute sets are immutable, this returns a new set.
226   AttributeSet addAttribute(LLVMContext &C, unsigned Index,
227                             StringRef Kind) const;
228   AttributeSet addAttribute(LLVMContext &C, unsigned Index,
229                             StringRef Kind, StringRef Value) const;
230
231   /// \brief Add attributes to the attribute set at the given index. Because
232   /// attribute sets are immutable, this returns a new set.
233   AttributeSet addAttributes(LLVMContext &C, unsigned Index,
234                              AttributeSet Attrs) const;
235
236   /// \brief Remove the specified attribute at the specified index from this
237   /// attribute list. Because attribute lists are immutable, this returns the
238   /// new list.
239   AttributeSet removeAttribute(LLVMContext &C, unsigned Index,
240                                Attribute::AttrKind Attr) const;
241
242   /// \brief Remove the specified attributes at the specified index from this
243   /// attribute list. Because attribute lists are immutable, this returns the
244   /// new list.
245   AttributeSet removeAttributes(LLVMContext &C, unsigned Index,
246                                 AttributeSet Attrs) const;
247
248   /// \brief Remove the specified attributes at the specified index from this
249   /// attribute list. Because attribute lists are immutable, this returns the
250   /// new list.
251   AttributeSet removeAttributes(LLVMContext &C, unsigned Index,
252                                 const AttrBuilder &Attrs) const;
253
254   /// \brief Add the dereferenceable attribute to the attribute set at the given
255   /// index. Because attribute sets are immutable, this returns a new set.
256   AttributeSet addDereferenceableAttr(LLVMContext &C, unsigned Index,
257                                       uint64_t Bytes) const;
258
259   /// \brief Add the dereferenceable_or_null attribute to the attribute set at
260   /// the given index. Because attribute sets are immutable, this returns a new
261   /// set.
262   AttributeSet addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
263                                             uint64_t Bytes) const;
264
265   //===--------------------------------------------------------------------===//
266   // AttributeSet Accessors
267   //===--------------------------------------------------------------------===//
268
269   /// \brief Retrieve the LLVM context.
270   LLVMContext &getContext() const;
271
272   /// \brief The attributes for the specified index are returned.
273   AttributeSet getParamAttributes(unsigned Index) const;
274
275   /// \brief The attributes for the ret value are returned.
276   AttributeSet getRetAttributes() const;
277
278   /// \brief The function attributes are returned.
279   AttributeSet getFnAttributes() const;
280
281   /// \brief Return true if the attribute exists at the given index.
282   bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
283
284   /// \brief Return true if the attribute exists at the given index.
285   bool hasAttribute(unsigned Index, StringRef Kind) const;
286
287   /// \brief Return true if attribute exists at the given index.
288   bool hasAttributes(unsigned Index) const;
289
290   /// \brief Return true if the specified attribute is set for at least one
291   /// parameter or for the return value.
292   bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
293
294   /// \brief Return the attribute object that exists at the given index.
295   Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
296
297   /// \brief Return the attribute object that exists at the given index.
298   Attribute getAttribute(unsigned Index, StringRef Kind) const;
299
300   /// \brief Return the alignment for the specified function parameter.
301   unsigned getParamAlignment(unsigned Index) const;
302
303   /// \brief Get the stack alignment.
304   unsigned getStackAlignment(unsigned Index) const;
305
306   /// \brief Get the number of dereferenceable bytes (or zero if unknown).
307   uint64_t getDereferenceableBytes(unsigned Index) const;
308
309   /// \brief Get the number of dereferenceable_or_null bytes (or zero if
310   /// unknown).
311   uint64_t getDereferenceableOrNullBytes(unsigned Index) const;
312
313   /// \brief Return the attributes at the index as a string.
314   std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
315
316   typedef ArrayRef<Attribute>::iterator iterator;
317
318   iterator begin(unsigned Slot) const;
319   iterator end(unsigned Slot) const;
320
321   /// operator==/!= - Provide equality predicates.
322   bool operator==(const AttributeSet &RHS) const {
323     return pImpl == RHS.pImpl;
324   }
325   bool operator!=(const AttributeSet &RHS) const {
326     return pImpl != RHS.pImpl;
327   }
328
329   //===--------------------------------------------------------------------===//
330   // AttributeSet Introspection
331   //===--------------------------------------------------------------------===//
332
333   // FIXME: Remove this.
334   uint64_t Raw(unsigned Index) const;
335
336   /// \brief Return a raw pointer that uniquely identifies this attribute list.
337   void *getRawPointer() const {
338     return pImpl;
339   }
340
341   /// \brief Return true if there are no attributes.
342   bool isEmpty() const {
343     return getNumSlots() == 0;
344   }
345
346   /// \brief Return the number of slots used in this attribute list.  This is
347   /// the number of arguments that have an attribute set on them (including the
348   /// function itself).
349   unsigned getNumSlots() const;
350
351   /// \brief Return the index for the given slot.
352   unsigned getSlotIndex(unsigned Slot) const;
353
354   /// \brief Return the attributes at the given slot.
355   AttributeSet getSlotAttributes(unsigned Slot) const;
356
357   void dump() const;
358 };
359
360 //===----------------------------------------------------------------------===//
361 /// \class
362 /// \brief Provide DenseMapInfo for AttributeSet.
363 template<> struct DenseMapInfo<AttributeSet> {
364   static inline AttributeSet getEmptyKey() {
365     uintptr_t Val = static_cast<uintptr_t>(-1);
366     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
367     return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
368   }
369   static inline AttributeSet getTombstoneKey() {
370     uintptr_t Val = static_cast<uintptr_t>(-2);
371     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
372     return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
373   }
374   static unsigned getHashValue(AttributeSet AS) {
375     return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
376            (unsigned((uintptr_t)AS.pImpl) >> 9);
377   }
378   static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
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   std::bitset<Attribute::EndAttrKinds> Attrs;
389   std::map<std::string, std::string> TargetDepAttrs;
390   uint64_t Alignment;
391   uint64_t StackAlignment;
392   uint64_t DerefBytes;
393   uint64_t DerefOrNullBytes;
394
395 public:
396   AttrBuilder()
397       : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
398         DerefOrNullBytes(0) {}
399   explicit AttrBuilder(uint64_t Val)
400       : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
401         DerefOrNullBytes(0) {
402     addRawValue(Val);
403   }
404   AttrBuilder(const Attribute &A)
405       : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
406         DerefOrNullBytes(0) {
407     addAttribute(A);
408   }
409   AttrBuilder(AttributeSet AS, unsigned Idx);
410
411   void clear();
412
413   /// \brief Add an attribute to the builder.
414   AttrBuilder &addAttribute(Attribute::AttrKind Val);
415
416   /// \brief Add the Attribute object to the builder.
417   AttrBuilder &addAttribute(Attribute A);
418
419   /// \brief Add the target-dependent attribute to the builder.
420   AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
421
422   /// \brief Remove an attribute from the builder.
423   AttrBuilder &removeAttribute(Attribute::AttrKind Val);
424
425   /// \brief Remove the attributes from the builder.
426   AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index);
427
428   /// \brief Remove the target-dependent attribute to the builder.
429   AttrBuilder &removeAttribute(StringRef A);
430
431   /// \brief Add the attributes from the builder.
432   AttrBuilder &merge(const AttrBuilder &B);
433
434   /// \brief Remove the attributes from the builder.
435   AttrBuilder &remove(const AttrBuilder &B);
436
437   /// \brief Return true if the builder has any attribute that's in the
438   /// specified builder.
439   bool overlaps(const AttrBuilder &B) const;
440
441   /// \brief Return true if the builder has the specified attribute.
442   bool contains(Attribute::AttrKind A) const {
443     assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
444     return Attrs[A];
445   }
446
447   /// \brief Return true if the builder has the specified target-dependent
448   /// attribute.
449   bool contains(StringRef A) const;
450
451   /// \brief Return true if the builder has IR-level attributes.
452   bool hasAttributes() const;
453
454   /// \brief Return true if the builder has any attribute that's in the
455   /// specified attribute.
456   bool hasAttributes(AttributeSet A, uint64_t Index) const;
457
458   /// \brief Return true if the builder has an alignment attribute.
459   bool hasAlignmentAttr() const;
460
461   /// \brief Retrieve the alignment attribute, if it exists.
462   uint64_t getAlignment() const { return Alignment; }
463
464   /// \brief Retrieve the stack alignment attribute, if it exists.
465   uint64_t getStackAlignment() const { return StackAlignment; }
466
467   /// \brief Retrieve the number of dereferenceable bytes, if the
468   /// dereferenceable attribute exists (zero is returned otherwise).
469   uint64_t getDereferenceableBytes() const { return DerefBytes; }
470
471   /// \brief Retrieve the number of dereferenceable_or_null bytes, if the
472   /// dereferenceable_or_null attribute exists (zero is returned otherwise).
473   uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; }
474
475   /// \brief This turns an int alignment (which must be a power of 2) into the
476   /// form used internally in Attribute.
477   AttrBuilder &addAlignmentAttr(unsigned Align);
478
479   /// \brief This turns an int stack alignment (which must be a power of 2) into
480   /// the form used internally in Attribute.
481   AttrBuilder &addStackAlignmentAttr(unsigned Align);
482
483   /// \brief This turns the number of dereferenceable bytes into the form used
484   /// internally in Attribute.
485   AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
486
487   /// \brief This turns the number of dereferenceable_or_null bytes into the
488   /// form used internally in Attribute.
489   AttrBuilder &addDereferenceableOrNullAttr(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 AttrBuilder typeIncompatible(Type *Ty);
530
531 /// \returns Return true if the two functions have compatible target-independent
532 /// attributes for inlining purposes.
533 bool areInlineCompatible(const Function &Caller, const Function &Callee);
534
535 /// \brief Merge caller's and callee's attributes.
536 void mergeAttributesForInlining(Function &Caller, const Function &Callee);
537
538 } // end AttributeFuncs namespace
539
540 } // end llvm namespace
541
542 #endif