Line data Source code
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"
24 : #include "llvm/ADT/iterator_range.h"
25 : #include "llvm/Config/llvm-config.h"
26 : #include "llvm/Support/PointerLikeTypeTraits.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);
92 : static Attribute get(LLVMContext &Context, StringRef Kind,
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);
99 : static Attribute getWithDereferenceableBytes(LLVMContext &Context,
100 : uint64_t Bytes);
101 : static Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context,
102 : uint64_t Bytes);
103 : static Attribute getWithAllocSizeArgs(LLVMContext &Context,
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.
129 : Attribute::AttrKind getKindAsEnum() const;
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 2008752 : 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 0 : void Profile(FoldingSetNodeID &ID) const {
175 198139209 : ID.AddPointer(pImpl);
176 0 : }
177 :
178 : /// Return a raw pointer that uniquely identifies this attribute.
179 0 : void *getRawPointer() const {
180 0 : 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.
190 : inline LLVMAttributeRef wrap(Attribute Attr) {
191 : return reinterpret_cast<LLVMAttributeRef>(Attr.getRawPointer());
192 : }
193 :
194 : // Specialized opaque value conversions.
195 : inline Attribute unwrap(LLVMAttributeRef Attr) {
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.
205 : class AttributeSet {
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 405072 : AttributeSet() = default;
222 : AttributeSet(const AttributeSet &) = default;
223 : ~AttributeSet() = default;
224 :
225 : static AttributeSet get(LLVMContext &C, const AttrBuilder &B);
226 : static AttributeSet get(LLVMContext &C, ArrayRef<Attribute> Attrs);
227 :
228 535680 : 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 2983813 : 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> {
295 : static AttributeSet getEmptyKey() {
296 : auto Val = static_cast<uintptr_t>(-1);
297 : Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
298 : return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
299 : }
300 :
301 : static AttributeSet getTombstoneKey() {
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 565337 : return (unsigned((uintptr_t)AS.SetNode) >> 4) ^
309 565337 : (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'.
323 : class AttributeList {
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,
345 : ArrayRef<std::pair<unsigned, Attribute>> Attrs);
346 : static AttributeList get(LLVMContext &C,
347 : ArrayRef<std::pair<unsigned, AttributeSet>> Attrs);
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 :
358 : static AttributeList getImpl(LLVMContext &C, ArrayRef<AttributeSet> AttrSets);
359 :
360 : public:
361 5188567 : AttributeList() = default;
362 :
363 : //===--------------------------------------------------------------------===//
364 : // AttributeList Construction and Mutation
365 : //===--------------------------------------------------------------------===//
366 :
367 : /// Return an AttributeList with the specified parameters in it.
368 : static AttributeList get(LLVMContext &C, ArrayRef<AttributeList> Attrs);
369 : static AttributeList get(LLVMContext &C, unsigned Index,
370 : ArrayRef<Attribute::AttrKind> Kinds);
371 : static AttributeList get(LLVMContext &C, unsigned Index,
372 : ArrayRef<StringRef> Kind);
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.
397 : AttributeList addParamAttribute(LLVMContext &C, unsigned ArgNo,
398 : Attribute::AttrKind Kind) const {
399 41711 : 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.
404 : AttributeList addParamAttribute(LLVMContext &C, unsigned ArgNo,
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.
417 : AttributeList addParamAttributes(LLVMContext &C, unsigned ArgNo,
418 : const AttrBuilder &B) const {
419 13 : 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.
443 : AttributeList removeParamAttribute(LLVMContext &C, unsigned ArgNo,
444 : Attribute::AttrKind Kind) const {
445 372901 : 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.
450 : AttributeList removeParamAttribute(LLVMContext &C, unsigned ArgNo,
451 : StringRef Kind) const {
452 0 : 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.
457 : AttributeList removeParamAttributes(LLVMContext &C, unsigned ArgNo,
458 : const AttrBuilder &AttrsToRemove) const {
459 1 : 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.
475 : AttributeList addDereferenceableParamAttr(LLVMContext &C, unsigned ArgNo,
476 : uint64_t Bytes) const {
477 0 : 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.
488 : AttributeList addDereferenceableOrNullParamAttr(LLVMContext &C,
489 : unsigned ArgNo,
490 : uint64_t Bytes) const {
491 0 : 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.
502 : AttributeList addAllocSizeParamAttr(LLVMContext &C, unsigned ArgNo,
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 10 : 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 1 : 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 1942248 : 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 94024 : 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 0 : unsigned index_begin() const { return AttributeList::FunctionIndex; }
633 10029111 : unsigned index_end() const { return getNumAttrSets() - 1; }
634 :
635 : /// operator==/!= - Provide equality predicates.
636 33773 : bool operator==(const AttributeList &RHS) const { return pImpl == RHS.pImpl; }
637 0 : 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 1 : 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> {
654 : static AttributeList getEmptyKey() {
655 : auto Val = static_cast<uintptr_t>(-1);
656 : Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
657 : return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
658 : }
659 :
660 : static AttributeList getTombstoneKey() {
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 31449 : return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
668 31449 : (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 7278629 : 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 21132511 : AttrBuilder() = default;
693 :
694 4 : AttrBuilder(const Attribute &A) {
695 4 : addAttribute(A);
696 : }
697 :
698 : AttrBuilder(AttributeList AS, unsigned Idx);
699 : AttrBuilder(AttributeSet AS);
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.
732 : bool contains(Attribute::AttrKind A) const {
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 0 : uint64_t getAlignment() const { return Alignment; }
753 :
754 : /// Retrieve the stack alignment attribute, if it exists.
755 0 : 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 0 : 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 0 : 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;
801 : using td_range = iterator_range<td_iterator>;
802 : using td_const_range = iterator_range<td_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 :
812 : td_const_range td_attrs() const {
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.
827 : AttrBuilder typeIncompatible(Type *Ty);
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
|