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