LLVM  8.0.0svn
Attributes.h
Go to the documentation of this file.
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 /// 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-c/Types.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/FoldingSet.h"
22 #include "llvm/ADT/Optional.h"
23 #include "llvm/ADT/StringRef.h"
25 #include "llvm/Config/llvm-config.h"
27 #include <bitset>
28 #include <cassert>
29 #include <cstdint>
30 #include <map>
31 #include <string>
32 #include <utility>
33 
34 namespace llvm {
35 
36 class AttrBuilder;
37 class AttributeImpl;
38 class AttributeListImpl;
39 class AttributeSetNode;
40 template<typename T> struct DenseMapInfo;
41 class Function;
42 class LLVMContext;
43 class Type;
44 
45 //===----------------------------------------------------------------------===//
46 /// \class
47 /// Functions, function parameters, and return types can have attributes
48 /// to indicate how they should be treated by optimizations and code
49 /// generation. This class represents one of those attributes. It's light-weight
50 /// and should be passed around by-value.
51 class Attribute {
52 public:
53  /// This enumeration lists the attributes that can be associated with
54  /// parameters, function results, or the function itself.
55  ///
56  /// Note: The `uwtable' attribute is about the ABI or the user mandating an
57  /// entry in the unwind table. The `nounwind' attribute is about an exception
58  /// passing by the function.
59  ///
60  /// In a theoretical system that uses tables for profiling and SjLj for
61  /// exceptions, they would be fully independent. In a normal system that uses
62  /// tables for both, the semantics are:
63  ///
64  /// nil = Needs an entry because an exception might pass by.
65  /// nounwind = No need for an entry
66  /// uwtable = Needs an entry because the ABI says so and because
67  /// an exception might pass by.
68  /// uwtable + nounwind = Needs an entry because the ABI says so.
69 
70  enum AttrKind {
71  // IR-Level Attributes
72  None, ///< No attributes have been set
73  #define GET_ATTR_ENUM
74  #include "llvm/IR/Attributes.inc"
75  EndAttrKinds ///< Sentinal value useful for loops
76  };
77 
78 private:
79  AttributeImpl *pImpl = nullptr;
80 
81  Attribute(AttributeImpl *A) : pImpl(A) {}
82 
83 public:
84  Attribute() = default;
85 
86  //===--------------------------------------------------------------------===//
87  // Attribute Construction
88  //===--------------------------------------------------------------------===//
89 
90  /// Return a uniquified Attribute object.
91  static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
93  StringRef Val = StringRef());
94 
95  /// Return a uniquified Attribute object that has the specific
96  /// alignment set.
97  static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
98  static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
100  uint64_t Bytes);
102  uint64_t Bytes);
104  unsigned ElemSizeArg,
105  const Optional<unsigned> &NumElemsArg);
106 
107  //===--------------------------------------------------------------------===//
108  // Attribute Accessors
109  //===--------------------------------------------------------------------===//
110 
111  /// Return true if the attribute is an Attribute::AttrKind type.
112  bool isEnumAttribute() const;
113 
114  /// Return true if the attribute is an integer attribute.
115  bool isIntAttribute() const;
116 
117  /// Return true if the attribute is a string (target-dependent)
118  /// attribute.
119  bool isStringAttribute() const;
120 
121  /// Return true if the attribute is present.
122  bool hasAttribute(AttrKind Val) const;
123 
124  /// Return true if the target-dependent attribute is present.
125  bool hasAttribute(StringRef Val) const;
126 
127  /// Return the attribute's kind as an enum (Attribute::AttrKind). This
128  /// requires the attribute to be an enum or integer attribute.
130 
131  /// Return the attribute's value as an integer. This requires that the
132  /// attribute be an integer attribute.
133  uint64_t getValueAsInt() const;
134 
135  /// Return the attribute's kind as a string. This requires the
136  /// attribute to be a string attribute.
137  StringRef getKindAsString() const;
138 
139  /// Return the attribute's value as a string. This requires the
140  /// attribute to be a string attribute.
141  StringRef getValueAsString() const;
142 
143  /// Returns the alignment field of an attribute as a byte alignment
144  /// value.
145  unsigned getAlignment() const;
146 
147  /// Returns the stack alignment field of an attribute as a byte
148  /// alignment value.
149  unsigned getStackAlignment() const;
150 
151  /// Returns the number of dereferenceable bytes from the
152  /// dereferenceable attribute.
153  uint64_t getDereferenceableBytes() const;
154 
155  /// Returns the number of dereferenceable_or_null bytes from the
156  /// dereferenceable_or_null attribute.
157  uint64_t getDereferenceableOrNullBytes() const;
158 
159  /// Returns the argument numbers for the allocsize attribute (or pair(0, 0)
160  /// if not known).
161  std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
162 
163  /// The Attribute is converted to a string of equivalent mnemonic. This
164  /// is, presumably, for writing out the mnemonics for the assembly writer.
165  std::string getAsString(bool InAttrGrp = false) const;
166 
167  /// Equality and non-equality operators.
168  bool operator==(Attribute A) const { return pImpl == A.pImpl; }
169  bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
170 
171  /// Less-than operator. Useful for sorting the attributes list.
172  bool operator<(Attribute A) const;
173 
174  void Profile(FoldingSetNodeID &ID) const {
175  ID.AddPointer(pImpl);
176  }
177 
178  /// Return a raw pointer that uniquely identifies this attribute.
179  void *getRawPointer() const {
180  return pImpl;
181  }
182 
183  /// Get an attribute from a raw pointer created by getRawPointer.
184  static Attribute fromRawPointer(void *RawPtr) {
185  return Attribute(reinterpret_cast<AttributeImpl*>(RawPtr));
186  }
187 };
188 
189 // Specialized opaque value conversions.
191  return reinterpret_cast<LLVMAttributeRef>(Attr.getRawPointer());
192 }
193 
194 // Specialized opaque value conversions.
196  return Attribute::fromRawPointer(Attr);
197 }
198 
199 //===----------------------------------------------------------------------===//
200 /// \class
201 /// This class holds the attributes for a particular argument, parameter,
202 /// function, or return value. It is an immutable value type that is cheap to
203 /// copy. Adding and removing enum attributes is intended to be fast, but adding
204 /// and removing string or integer attributes involves a FoldingSet lookup.
206  friend AttributeListImpl;
207  template <typename Ty> friend struct DenseMapInfo;
208 
209  // TODO: Extract AvailableAttrs from AttributeSetNode and store them here.
210  // This will allow an efficient implementation of addAttribute and
211  // removeAttribute for enum attrs.
212 
213  /// Private implementation pointer.
214  AttributeSetNode *SetNode = nullptr;
215 
216 private:
217  explicit AttributeSet(AttributeSetNode *ASN) : SetNode(ASN) {}
218 
219 public:
220  /// AttributeSet is a trivially copyable value type.
221  AttributeSet() = default;
222  AttributeSet(const AttributeSet &) = default;
223  ~AttributeSet() = default;
224 
225  static AttributeSet get(LLVMContext &C, const AttrBuilder &B);
227 
228  bool operator==(const AttributeSet &O) const { return SetNode == O.SetNode; }
229  bool operator!=(const AttributeSet &O) const { return !(*this == O); }
230 
231  /// Add an argument attribute. Returns a new set because attribute sets are
232  /// immutable.
233  AttributeSet addAttribute(LLVMContext &C, Attribute::AttrKind Kind) const;
234 
235  /// Add a target-dependent attribute. Returns a new set because attribute sets
236  /// are immutable.
237  AttributeSet addAttribute(LLVMContext &C, StringRef Kind,
238  StringRef Value = StringRef()) const;
239 
240  /// Add attributes to the attribute set. Returns a new set because attribute
241  /// sets are immutable.
242  AttributeSet addAttributes(LLVMContext &C, AttributeSet AS) const;
243 
244  /// Remove the specified attribute from this set. Returns a new set because
245  /// attribute sets are immutable.
246  AttributeSet removeAttribute(LLVMContext &C, Attribute::AttrKind Kind) const;
247 
248  /// Remove the specified attribute from this set. Returns a new set because
249  /// attribute sets are immutable.
250  AttributeSet removeAttribute(LLVMContext &C, StringRef Kind) const;
251 
252  /// Remove the specified attributes from this set. Returns a new set because
253  /// attribute sets are immutable.
254  AttributeSet removeAttributes(LLVMContext &C,
255  const AttrBuilder &AttrsToRemove) const;
256 
257  /// Return the number of attributes in this set.
258  unsigned getNumAttributes() const;
259 
260  /// Return true if attributes exists in this set.
261  bool hasAttributes() const { return SetNode != nullptr; }
262 
263  /// Return true if the attribute exists in this set.
264  bool hasAttribute(Attribute::AttrKind Kind) const;
265 
266  /// Return true if the attribute exists in this set.
267  bool hasAttribute(StringRef Kind) const;
268 
269  /// Return the attribute object.
270  Attribute getAttribute(Attribute::AttrKind Kind) const;
271 
272  /// Return the target-dependent attribute object.
273  Attribute getAttribute(StringRef Kind) const;
274 
275  unsigned getAlignment() const;
276  unsigned getStackAlignment() const;
277  uint64_t getDereferenceableBytes() const;
278  uint64_t getDereferenceableOrNullBytes() const;
279  std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
280  std::string getAsString(bool InAttrGrp = false) const;
281 
282  using iterator = const Attribute *;
283 
284  iterator begin() const;
285  iterator end() const;
286 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
287  void dump() const;
288 #endif
289 };
290 
291 //===----------------------------------------------------------------------===//
292 /// \class
293 /// Provide DenseMapInfo for AttributeSet.
294 template <> struct DenseMapInfo<AttributeSet> {
296  auto Val = static_cast<uintptr_t>(-1);
297  Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
298  return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
299  }
300 
302  auto Val = static_cast<uintptr_t>(-2);
303  Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
304  return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
305  }
306 
307  static unsigned getHashValue(AttributeSet AS) {
308  return (unsigned((uintptr_t)AS.SetNode) >> 4) ^
309  (unsigned((uintptr_t)AS.SetNode) >> 9);
310  }
311 
312  static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
313 };
314 
315 //===----------------------------------------------------------------------===//
316 /// \class
317 /// This class holds the attributes for a function, its return value, and
318 /// its parameters. You access the attributes for each of them via an index into
319 /// the AttributeList object. The function attributes are at index
320 /// `AttributeList::FunctionIndex', the return value is at index
321 /// `AttributeList::ReturnIndex', and the attributes for the parameters start at
322 /// index `AttributeList::FirstArgIndex'.
324 public:
325  enum AttrIndex : unsigned {
326  ReturnIndex = 0U,
327  FunctionIndex = ~0U,
328  FirstArgIndex = 1,
329  };
330 
331 private:
332  friend class AttrBuilder;
333  friend class AttributeListImpl;
334  friend class AttributeSet;
335  friend class AttributeSetNode;
336  template <typename Ty> friend struct DenseMapInfo;
337 
338  /// The attributes that we are managing. This can be null to represent
339  /// the empty attributes list.
340  AttributeListImpl *pImpl = nullptr;
341 
342 public:
343  /// Create an AttributeList with the specified parameters in it.
344  static AttributeList get(LLVMContext &C,
346  static AttributeList get(LLVMContext &C,
348 
349  /// Create an AttributeList from attribute sets for a function, its
350  /// return value, and all of its arguments.
351  static AttributeList get(LLVMContext &C, AttributeSet FnAttrs,
352  AttributeSet RetAttrs,
353  ArrayRef<AttributeSet> ArgAttrs);
354 
355 private:
356  explicit AttributeList(AttributeListImpl *LI) : pImpl(LI) {}
357 
359 
360 public:
361  AttributeList() = default;
362 
363  //===--------------------------------------------------------------------===//
364  // AttributeList Construction and Mutation
365  //===--------------------------------------------------------------------===//
366 
367  /// Return an AttributeList with the specified parameters in it.
369  static AttributeList get(LLVMContext &C, unsigned Index,
371  static AttributeList get(LLVMContext &C, unsigned Index,
373  static AttributeList get(LLVMContext &C, unsigned Index,
374  const AttrBuilder &B);
375 
376  /// Add an attribute to the attribute set at the given index.
377  /// Returns a new list because attribute lists are immutable.
378  AttributeList addAttribute(LLVMContext &C, unsigned Index,
379  Attribute::AttrKind Kind) const;
380 
381  /// Add an attribute to the attribute set at the given index.
382  /// Returns a new list because attribute lists are immutable.
383  AttributeList addAttribute(LLVMContext &C, unsigned Index, StringRef Kind,
384  StringRef Value = StringRef()) const;
385 
386  /// Add an attribute to the attribute set at the given index.
387  /// Returns a new list because attribute lists are immutable.
388  AttributeList addAttribute(LLVMContext &C, unsigned Index, Attribute A) const;
389 
390  /// Add attributes to the attribute set at the given index.
391  /// Returns a new list because attribute lists are immutable.
392  AttributeList addAttributes(LLVMContext &C, unsigned Index,
393  const AttrBuilder &B) const;
394 
395  /// Add an argument attribute to the list. Returns a new list because
396  /// attribute lists are immutable.
398  Attribute::AttrKind Kind) const {
399  return addAttribute(C, ArgNo + FirstArgIndex, Kind);
400  }
401 
402  /// Add an argument attribute to the list. Returns a new list because
403  /// attribute lists are immutable.
405  StringRef Kind,
406  StringRef Value = StringRef()) const {
407  return addAttribute(C, ArgNo + FirstArgIndex, Kind, Value);
408  }
409 
410  /// Add an attribute to the attribute list at the given arg indices. Returns a
411  /// new list because attribute lists are immutable.
412  AttributeList addParamAttribute(LLVMContext &C, ArrayRef<unsigned> ArgNos,
413  Attribute A) const;
414 
415  /// Add an argument attribute to the list. Returns a new list because
416  /// attribute lists are immutable.
418  const AttrBuilder &B) const {
419  return addAttributes(C, ArgNo + FirstArgIndex, B);
420  }
421 
422  /// Remove the specified attribute at the specified index from this
423  /// attribute list. Returns a new list because attribute lists are immutable.
424  AttributeList removeAttribute(LLVMContext &C, unsigned Index,
425  Attribute::AttrKind Kind) const;
426 
427  /// Remove the specified attribute at the specified index from this
428  /// attribute list. Returns a new list because attribute lists are immutable.
429  AttributeList removeAttribute(LLVMContext &C, unsigned Index,
430  StringRef Kind) const;
431 
432  /// Remove the specified attributes at the specified index from this
433  /// attribute list. Returns a new list because attribute lists are immutable.
434  AttributeList removeAttributes(LLVMContext &C, unsigned Index,
435  const AttrBuilder &AttrsToRemove) const;
436 
437  /// Remove all attributes at the specified index from this
438  /// attribute list. Returns a new list because attribute lists are immutable.
439  AttributeList removeAttributes(LLVMContext &C, unsigned Index) const;
440 
441  /// Remove the specified attribute at the specified arg index from this
442  /// attribute list. Returns a new list because attribute lists are immutable.
444  Attribute::AttrKind Kind) const {
445  return removeAttribute(C, ArgNo + FirstArgIndex, Kind);
446  }
447 
448  /// Remove the specified attribute at the specified arg index from this
449  /// attribute list. Returns a new list because attribute lists are immutable.
451  StringRef Kind) const {
452  return removeAttribute(C, ArgNo + FirstArgIndex, Kind);
453  }
454 
455  /// Remove the specified attribute at the specified arg index from this
456  /// attribute list. Returns a new list because attribute lists are immutable.
458  const AttrBuilder &AttrsToRemove) const {
459  return removeAttributes(C, ArgNo + FirstArgIndex, AttrsToRemove);
460  }
461 
462  /// Remove all attributes at the specified arg index from this
463  /// attribute list. Returns a new list because attribute lists are immutable.
464  AttributeList removeParamAttributes(LLVMContext &C, unsigned ArgNo) const {
465  return removeAttributes(C, ArgNo + FirstArgIndex);
466  }
467 
468  /// \brief Add the dereferenceable attribute to the attribute set at the given
469  /// index. Returns a new list because attribute lists are immutable.
470  AttributeList addDereferenceableAttr(LLVMContext &C, unsigned Index,
471  uint64_t Bytes) const;
472 
473  /// \brief Add the dereferenceable attribute to the attribute set at the given
474  /// arg index. Returns a new list because attribute lists are immutable.
476  uint64_t Bytes) const {
477  return addDereferenceableAttr(C, ArgNo + FirstArgIndex, Bytes);
478  }
479 
480  /// Add the dereferenceable_or_null attribute to the attribute set at
481  /// the given index. Returns a new list because attribute lists are immutable.
482  AttributeList addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
483  uint64_t Bytes) const;
484 
485  /// Add the dereferenceable_or_null attribute to the attribute set at
486  /// the given arg index. Returns a new list because attribute lists are
487  /// immutable.
489  unsigned ArgNo,
490  uint64_t Bytes) const {
491  return addDereferenceableOrNullAttr(C, ArgNo + FirstArgIndex, Bytes);
492  }
493 
494  /// Add the allocsize attribute to the attribute set at the given index.
495  /// Returns a new list because attribute lists are immutable.
496  AttributeList addAllocSizeAttr(LLVMContext &C, unsigned Index,
497  unsigned ElemSizeArg,
498  const Optional<unsigned> &NumElemsArg);
499 
500  /// Add the allocsize attribute to the attribute set at the given arg index.
501  /// Returns a new list because attribute lists are immutable.
503  unsigned ElemSizeArg,
504  const Optional<unsigned> &NumElemsArg) {
505  return addAllocSizeAttr(C, ArgNo + FirstArgIndex, ElemSizeArg, NumElemsArg);
506  }
507 
508  //===--------------------------------------------------------------------===//
509  // AttributeList Accessors
510  //===--------------------------------------------------------------------===//
511 
512  /// Retrieve the LLVM context.
513  LLVMContext &getContext() const;
514 
515  /// The attributes for the specified index are returned.
516  AttributeSet getAttributes(unsigned Index) const;
517 
518  /// The attributes for the argument or parameter at the given index are
519  /// returned.
520  AttributeSet getParamAttributes(unsigned ArgNo) const;
521 
522  /// The attributes for the ret value are returned.
523  AttributeSet getRetAttributes() const;
524 
525  /// The function attributes are returned.
526  AttributeSet getFnAttributes() const;
527 
528  /// Return true if the attribute exists at the given index.
529  bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
530 
531  /// Return true if the attribute exists at the given index.
532  bool hasAttribute(unsigned Index, StringRef Kind) const;
533 
534  /// Return true if attribute exists at the given index.
535  bool hasAttributes(unsigned Index) const;
536 
537  /// Return true if the attribute exists for the given argument
538  bool hasParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
539  return hasAttribute(ArgNo + FirstArgIndex, Kind);
540  }
541 
542  /// Return true if the attribute exists for the given argument
543  bool hasParamAttr(unsigned ArgNo, StringRef Kind) const {
544  return hasAttribute(ArgNo + FirstArgIndex, Kind);
545  }
546 
547  /// Return true if attributes exists for the given argument
548  bool hasParamAttrs(unsigned ArgNo) const {
549  return hasAttributes(ArgNo + FirstArgIndex);
550  }
551 
552  /// Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
553  /// may be faster.
554  bool hasFnAttribute(Attribute::AttrKind Kind) const;
555 
556  /// Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
557  /// may be faster.
558  bool hasFnAttribute(StringRef Kind) const;
559 
560  /// Equivalent to hasAttribute(ArgNo + FirstArgIndex, Kind).
561  bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const;
562 
563  /// Return true if the specified attribute is set for at least one
564  /// parameter or for the return value. If Index is not nullptr, the index
565  /// of a parameter with the specified attribute is provided.
566  bool hasAttrSomewhere(Attribute::AttrKind Kind,
567  unsigned *Index = nullptr) const;
568 
569  /// Return the attribute object that exists at the given index.
570  Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
571 
572  /// Return the attribute object that exists at the given index.
573  Attribute getAttribute(unsigned Index, StringRef Kind) const;
574 
575  /// Return the attribute object that exists at the arg index.
576  Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
577  return getAttribute(ArgNo + FirstArgIndex, Kind);
578  }
579 
580  /// Return the attribute object that exists at the given index.
581  Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const {
582  return getAttribute(ArgNo + FirstArgIndex, Kind);
583  }
584 
585  /// Return the alignment of the return value.
586  unsigned getRetAlignment() const;
587 
588  /// Return the alignment for the specified function parameter.
589  unsigned getParamAlignment(unsigned ArgNo) const;
590 
591  /// Get the stack alignment.
592  unsigned getStackAlignment(unsigned Index) const;
593 
594  /// Get the number of dereferenceable bytes (or zero if unknown).
595  uint64_t getDereferenceableBytes(unsigned Index) const;
596 
597  /// Get the number of dereferenceable bytes (or zero if unknown) of an
598  /// arg.
599  uint64_t getParamDereferenceableBytes(unsigned ArgNo) const {
600  return getDereferenceableBytes(ArgNo + FirstArgIndex);
601  }
602 
603  /// Get the number of dereferenceable_or_null bytes (or zero if
604  /// unknown).
605  uint64_t getDereferenceableOrNullBytes(unsigned Index) const;
606 
607  /// Get the number of dereferenceable_or_null bytes (or zero if
608  /// unknown) of an arg.
609  uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo) const {
610  return getDereferenceableOrNullBytes(ArgNo + FirstArgIndex);
611  }
612 
613  /// Get the allocsize argument numbers (or pair(0, 0) if unknown).
614  std::pair<unsigned, Optional<unsigned>>
615  getAllocSizeArgs(unsigned Index) const;
616 
617  /// Return the attributes at the index as a string.
618  std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
619 
620  //===--------------------------------------------------------------------===//
621  // AttributeList Introspection
622  //===--------------------------------------------------------------------===//
623 
624  using iterator = const AttributeSet *;
625 
626  iterator begin() const;
627  iterator end() const;
628 
629  unsigned getNumAttrSets() const;
630 
631  /// Use these to iterate over the valid attribute indices.
632  unsigned index_begin() const { return AttributeList::FunctionIndex; }
633  unsigned index_end() const { return getNumAttrSets() - 1; }
634 
635  /// operator==/!= - Provide equality predicates.
636  bool operator==(const AttributeList &RHS) const { return pImpl == RHS.pImpl; }
637  bool operator!=(const AttributeList &RHS) const { return pImpl != RHS.pImpl; }
638 
639  /// Return a raw pointer that uniquely identifies this attribute list.
640  void *getRawPointer() const {
641  return pImpl;
642  }
643 
644  /// Return true if there are no attributes.
645  bool isEmpty() const { return pImpl == nullptr; }
646 
647  void dump() const;
648 };
649 
650 //===----------------------------------------------------------------------===//
651 /// \class
652 /// Provide DenseMapInfo for AttributeList.
653 template <> struct DenseMapInfo<AttributeList> {
655  auto Val = static_cast<uintptr_t>(-1);
656  Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
657  return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
658  }
659 
661  auto Val = static_cast<uintptr_t>(-2);
662  Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
663  return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
664  }
665 
666  static unsigned getHashValue(AttributeList AS) {
667  return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
668  (unsigned((uintptr_t)AS.pImpl) >> 9);
669  }
670 
671  static bool isEqual(AttributeList LHS, AttributeList RHS) {
672  return LHS == RHS;
673  }
674 };
675 
676 //===----------------------------------------------------------------------===//
677 /// \class
678 /// This class is used in conjunction with the Attribute::get method to
679 /// create an Attribute object. The object itself is uniquified. The Builder's
680 /// value, however, is not. So this can be used as a quick way to test for
681 /// equality, presence of attributes, etc.
682 class AttrBuilder {
683  std::bitset<Attribute::EndAttrKinds> Attrs;
684  std::map<std::string, std::string> TargetDepAttrs;
685  uint64_t Alignment = 0;
686  uint64_t StackAlignment = 0;
687  uint64_t DerefBytes = 0;
688  uint64_t DerefOrNullBytes = 0;
689  uint64_t AllocSizeArgs = 0;
690 
691 public:
692  AttrBuilder() = default;
693 
694  AttrBuilder(const Attribute &A) {
695  addAttribute(A);
696  }
697 
698  AttrBuilder(AttributeList AS, unsigned Idx);
700 
701  void clear();
702 
703  /// Add an attribute to the builder.
704  AttrBuilder &addAttribute(Attribute::AttrKind Val);
705 
706  /// Add the Attribute object to the builder.
707  AttrBuilder &addAttribute(Attribute A);
708 
709  /// Add the target-dependent attribute to the builder.
710  AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
711 
712  /// Remove an attribute from the builder.
713  AttrBuilder &removeAttribute(Attribute::AttrKind Val);
714 
715  /// Remove the attributes from the builder.
716  AttrBuilder &removeAttributes(AttributeList A, uint64_t WithoutIndex);
717 
718  /// Remove the target-dependent attribute to the builder.
719  AttrBuilder &removeAttribute(StringRef A);
720 
721  /// Add the attributes from the builder.
722  AttrBuilder &merge(const AttrBuilder &B);
723 
724  /// Remove the attributes from the builder.
725  AttrBuilder &remove(const AttrBuilder &B);
726 
727  /// Return true if the builder has any attribute that's in the
728  /// specified builder.
729  bool overlaps(const AttrBuilder &B) const;
730 
731  /// Return true if the builder has the specified attribute.
733  assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
734  return Attrs[A];
735  }
736 
737  /// Return true if the builder has the specified target-dependent
738  /// attribute.
739  bool contains(StringRef A) const;
740 
741  /// Return true if the builder has IR-level attributes.
742  bool hasAttributes() const;
743 
744  /// Return true if the builder has any attribute that's in the
745  /// specified attribute.
746  bool hasAttributes(AttributeList A, uint64_t Index) const;
747 
748  /// Return true if the builder has an alignment attribute.
749  bool hasAlignmentAttr() const;
750 
751  /// Retrieve the alignment attribute, if it exists.
752  uint64_t getAlignment() const { return Alignment; }
753 
754  /// Retrieve the stack alignment attribute, if it exists.
755  uint64_t getStackAlignment() const { return StackAlignment; }
756 
757  /// Retrieve the number of dereferenceable bytes, if the
758  /// dereferenceable attribute exists (zero is returned otherwise).
759  uint64_t getDereferenceableBytes() const { return DerefBytes; }
760 
761  /// Retrieve the number of dereferenceable_or_null bytes, if the
762  /// dereferenceable_or_null attribute exists (zero is returned otherwise).
763  uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; }
764 
765  /// Retrieve the allocsize args, if the allocsize attribute exists. If it
766  /// doesn't exist, pair(0, 0) is returned.
767  std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
768 
769  /// This turns an int alignment (which must be a power of 2) into the
770  /// form used internally in Attribute.
771  AttrBuilder &addAlignmentAttr(unsigned Align);
772 
773  /// This turns an int stack alignment (which must be a power of 2) into
774  /// the form used internally in Attribute.
775  AttrBuilder &addStackAlignmentAttr(unsigned Align);
776 
777  /// This turns the number of dereferenceable bytes into the form used
778  /// internally in Attribute.
779  AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
780 
781  /// This turns the number of dereferenceable_or_null bytes into the
782  /// form used internally in Attribute.
783  AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
784 
785  /// This turns one (or two) ints into the form used internally in Attribute.
786  AttrBuilder &addAllocSizeAttr(unsigned ElemSizeArg,
787  const Optional<unsigned> &NumElemsArg);
788 
789  /// Add an allocsize attribute, using the representation returned by
790  /// Attribute.getIntValue().
791  AttrBuilder &addAllocSizeAttrFromRawRepr(uint64_t RawAllocSizeRepr);
792 
793  /// Return true if the builder contains no target-independent
794  /// attributes.
795  bool empty() const { return Attrs.none(); }
796 
797  // Iterators for target-dependent attributes.
798  using td_type = std::pair<std::string, std::string>;
799  using td_iterator = std::map<std::string, std::string>::iterator;
800  using td_const_iterator = std::map<std::string, std::string>::const_iterator;
803 
804  td_iterator td_begin() { return TargetDepAttrs.begin(); }
805  td_iterator td_end() { return TargetDepAttrs.end(); }
806 
807  td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
808  td_const_iterator td_end() const { return TargetDepAttrs.end(); }
809 
810  td_range td_attrs() { return td_range(td_begin(), td_end()); }
811 
813  return td_const_range(td_begin(), td_end());
814  }
815 
816  bool td_empty() const { return TargetDepAttrs.empty(); }
817 
818  bool operator==(const AttrBuilder &B);
819  bool operator!=(const AttrBuilder &B) {
820  return !(*this == B);
821  }
822 };
823 
824 namespace AttributeFuncs {
825 
826 /// Which attributes cannot be applied to a type.
828 
829 /// \returns Return true if the two functions have compatible target-independent
830 /// attributes for inlining purposes.
831 bool areInlineCompatible(const Function &Caller, const Function &Callee);
832 
833 /// Merge caller's and callee's attributes.
834 void mergeAttributesForInlining(Function &Caller, const Function &Callee);
835 
836 } // end namespace AttributeFuncs
837 
838 } // end namespace llvm
839 
840 #endif // LLVM_IR_ATTRIBUTES_H
AttributeList addParamAttribute(LLVMContext &C, unsigned ArgNo, StringRef Kind, StringRef Value=StringRef()) const
Add an argument attribute to the list.
Definition: Attributes.h:404
uint64_t CallInst * C
void AddPointer(const void *Ptr)
Add* - Add various data types to Bit data.
Definition: FoldingSet.cpp:52
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:259
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Return the attribute object that exists at the arg index.
Definition: Attributes.h:576
static AttributeSet getEmptyKey()
Definition: Attributes.h:295
StringRef getKindAsString() const
Return the attribute&#39;s kind as a string.
Definition: Attributes.cpp:188
LLVMContext & Context
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:250
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
uint64_t getDereferenceableOrNullBytes() const
Returns the number of dereferenceable_or_null bytes from the dereferenceable_or_null attribute...
Definition: Attributes.cpp:230
unsigned getStackAlignment() const
Returns the stack alignment field of an attribute as a byte alignment value.
Definition: Attributes.cpp:217
static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align)
Return a uniquified Attribute object that has the specific alignment set.
Definition: Attributes.cpp:125
static Attribute getWithDereferenceableBytes(LLVMContext &Context, uint64_t Bytes)
Definition: Attributes.cpp:138
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
static unsigned getHashValue(AttributeSet AS)
Definition: Attributes.h:307
td_iterator td_end()
Definition: Attributes.h:805
bool operator!=(const AttributeList &RHS) const
Definition: Attributes.h:637
td_const_iterator td_end() const
Definition: Attributes.h:808
unsigned index_end() const
Definition: Attributes.h:633
AttributeList addAllocSizeParamAttr(LLVMContext &C, unsigned ArgNo, unsigned ElemSizeArg, const Optional< unsigned > &NumElemsArg)
Add the allocsize attribute to the attribute set at the given arg index.
Definition: Attributes.h:502
static unsigned getHashValue(AttributeList AS)
Definition: Attributes.h:666
return AArch64::GPR64RegClass contains(Reg)
static AttributeList getTombstoneKey()
Definition: Attributes.h:660
bool areInlineCompatible(const Function &Caller, const Function &Callee)
AttributeList removeParamAttribute(LLVMContext &C, unsigned ArgNo, StringRef Kind) const
Remove the specified attribute at the specified arg index from this attribute list.
Definition: Attributes.h:450
bool operator<(Attribute A) const
Less-than operator. Useful for sorting the attributes list.
Definition: Attributes.cpp:431
AttributeList addDereferenceableParamAttr(LLVMContext &C, unsigned ArgNo, uint64_t Bytes) const
Add the dereferenceable attribute to the attribute set at the given arg index.
Definition: Attributes.h:475
Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const
Return the attribute object that exists at the given index.
Definition: Attributes.h:581
uint64_t getDereferenceableOrNullBytes() const
Retrieve the number of dereferenceable_or_null bytes, if the dereferenceable_or_null attribute exists...
Definition: Attributes.h:763
AttributeList addDereferenceableOrNullParamAttr(LLVMContext &C, unsigned ArgNo, uint64_t Bytes) const
Add the dereferenceable_or_null attribute to the attribute set at the given arg index.
Definition: Attributes.h:488
uint64_t getValueAsInt() const
Return the attribute&#39;s value as an integer.
Definition: Attributes.cpp:181
bool isStringAttribute() const
Return true if the attribute is a string (target-dependent) attribute.
Definition: Attributes.cpp:170
Attribute unwrap(LLVMAttributeRef Attr)
Definition: Attributes.h:195
AttributeList addParamAttribute(LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const
Add an argument attribute to the list.
Definition: Attributes.h:397
static Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context, uint64_t Bytes)
Definition: Attributes.cpp:144
No attributes have been set.
Definition: Attributes.h:72
bool td_empty() const
Definition: Attributes.h:816
bool operator!=(const AttributeSet &O) const
Definition: Attributes.h:229
AttributeList getAttributes(LLVMContext &C, ID id)
Return the attributes for an intrinsic.
uint64_t getStackAlignment() const
Retrieve the stack alignment attribute, if it exists.
Definition: Attributes.h:755
static AttributeSet getTombstoneKey()
Definition: Attributes.h:301
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
std::pair< unsigned, Optional< unsigned > > getAllocSizeArgs() const
Returns the argument numbers for the allocsize attribute (or pair(0, 0) if not known).
Definition: Attributes.cpp:237
td_const_range td_attrs() const
Definition: Attributes.h:812
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
struct LLVMOpaqueAttributeRef * LLVMAttributeRef
Used to represent an attributes.
Definition: Types.h:130
uint64_t getDereferenceableBytes() const
Returns the number of dereferenceable bytes from the dereferenceable attribute.
Definition: Attributes.cpp:223
amdgpu Simplify well known AMD library false Value * Callee
static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align)
Definition: Attributes.cpp:131
bool operator==(const AttributeSet &O) const
Definition: Attributes.h:228
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:306
void * getRawPointer() const
Return a raw pointer that uniquely identifies this attribute.
Definition: Attributes.h:179
void Profile(FoldingSetNodeID &ID) const
Definition: Attributes.h:174
bool hasAttribute(AttrKind Val) const
Return true if the attribute is present.
Definition: Attributes.cpp:202
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
std::map< std::string, std::string >::iterator td_iterator
Definition: Attributes.h:799
std::map< std::string, std::string >::const_iterator td_const_iterator
Definition: Attributes.h:800
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
uint64_t getAlignment() const
Retrieve the alignment attribute, if it exists.
Definition: Attributes.h:752
bool operator!=(const AttrBuilder &B)
Definition: Attributes.h:819
Attribute::AttrKind getKindAsEnum() const
Return the attribute&#39;s kind as an enum (Attribute::AttrKind).
Definition: Attributes.cpp:174
td_iterator td_begin()
Definition: Attributes.h:804
bool isEnumAttribute() const
Return true if the attribute is an Attribute::AttrKind type.
Definition: Attributes.cpp:162
bool operator==(Attribute A) const
Equality and non-equality operators.
Definition: Attributes.h:168
Sentinal value useful for loops.
Definition: Attributes.h:75
bool hasParamAttr(unsigned ArgNo, StringRef Kind) const
Return true if the attribute exists for the given argument.
Definition: Attributes.h:543
bool hasParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Return true if the attribute exists for the given argument.
Definition: Attributes.h:538
const AMDGPUAS & AS
uint64_t getDereferenceableBytes() const
Retrieve the number of dereferenceable bytes, if the dereferenceable attribute exists (zero is return...
Definition: Attributes.h:759
static AttributeList getEmptyKey()
Definition: Attributes.h:654
bool contains(Attribute::AttrKind A) const
Return true if the builder has the specified attribute.
Definition: Attributes.h:732
void * getRawPointer() const
Return a raw pointer that uniquely identifies this attribute list.
Definition: Attributes.h:640
AttributeList addParamAttributes(LLVMContext &C, unsigned ArgNo, const AttrBuilder &B) const
Add an argument attribute to the list.
Definition: Attributes.h:417
bool operator==(const AttributeList &RHS) const
operator==/!= - Provide equality predicates.
Definition: Attributes.h:636
AttrBuilder(const Attribute &A)
Definition: Attributes.h:694
void mergeAttributesForInlining(Function &Caller, const Function &Callee)
Merge caller&#39;s and callee&#39;s attributes.
static Attribute fromRawPointer(void *RawPtr)
Get an attribute from a raw pointer created by getRawPointer.
Definition: Attributes.h:184
A range adaptor for a pair of iterators.
AttributeList removeParamAttributes(LLVMContext &C, unsigned ArgNo, const AttrBuilder &AttrsToRemove) const
Remove the specified attribute at the specified arg index from this attribute list.
Definition: Attributes.h:457
uint64_t getParamDereferenceableBytes(unsigned ArgNo) const
Get the number of dereferenceable bytes (or zero if unknown) of an arg.
Definition: Attributes.h:599
static void clear(coro::Shape &Shape)
Definition: Coroutines.cpp:211
bool isIntAttribute() const
Return true if the attribute is an integer attribute.
Definition: Attributes.cpp:166
unsigned getAlignment() const
Returns the alignment field of an attribute as a byte alignment value.
Definition: Attributes.cpp:211
LLVMAttributeRef wrap(Attribute Attr)
Definition: Attributes.h:190
uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo) const
Get the number of dereferenceable_or_null bytes (or zero if unknown) of an arg.
Definition: Attributes.h:609
static bool isEqual(AttributeList LHS, AttributeList RHS)
Definition: Attributes.h:671
bool empty() const
Return true if the builder contains no target-independent attributes.
Definition: Attributes.h:795
StringRef getValueAsString() const
Return the attribute&#39;s value as a string.
Definition: Attributes.cpp:195
AttributeList removeParamAttributes(LLVMContext &C, unsigned ArgNo) const
Remove all attributes at the specified arg index from this attribute list.
Definition: Attributes.h:464
static Attribute getWithAllocSizeArgs(LLVMContext &Context, unsigned ElemSizeArg, const Optional< unsigned > &NumElemsArg)
Definition: Attributes.cpp:151
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
Attribute()=default
td_range td_attrs()
Definition: Attributes.h:810
std::string getAsString(bool InAttrGrp=false) const
The Attribute is converted to a string of equivalent mnemonic.
Definition: Attributes.cpp:243
const unsigned Kind
std::pair< std::string, std::string > td_type
Definition: Attributes.h:798
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool hasAttributes() const
Return true if attributes exists in this set.
Definition: Attributes.h:261
LLVM Value Representation.
Definition: Value.h:73
bool operator!=(Attribute A) const
Definition: Attributes.h:169
AttrBuilder typeIncompatible(Type *Ty)
Which attributes cannot be applied to a type.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
AttributeList removeParamAttribute(LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const
Remove the specified attribute at the specified arg index from this attribute list.
Definition: Attributes.h:443
static LazyValueInfoImpl & getImpl(void *&PImpl, AssumptionCache *AC, const DataLayout *DL, DominatorTree *DT=nullptr)
This lazily constructs the LazyValueInfoImpl.
static bool isEqual(AttributeSet LHS, AttributeSet RHS)
Definition: Attributes.h:312
bool isEmpty() const
Return true if there are no attributes.
Definition: Attributes.h:645
bool hasParamAttrs(unsigned ArgNo) const
Return true if attributes exists for the given argument.
Definition: Attributes.h:548
td_const_iterator td_begin() const
Definition: Attributes.h:807
unsigned index_begin() const
Use these to iterate over the valid attribute indices.
Definition: Attributes.h:632
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results...
Definition: Attributes.h:70