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