1//===- Attributes.cpp - Implement AttributesList --------------------------===//
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 implements the Attribute, AttributeImpl, AttrBuilder,
11// AttributeListImpl, and AttributeList classes.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/IR/Attributes.h"
16#include "AttributeImpl.h"
17#include "LLVMContextImpl.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/FoldingSet.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/StringExtras.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/ADT/StringSwitch.h"
25#include "llvm/Config/llvm-config.h"
26#include "llvm/IR/AttributeMask.h"
27#include "llvm/IR/ConstantRange.h"
28#include "llvm/IR/ConstantRangeList.h"
29#include "llvm/IR/Function.h"
30#include "llvm/IR/LLVMContext.h"
31#include "llvm/IR/Operator.h"
32#include "llvm/IR/Type.h"
33#include "llvm/Support/Compiler.h"
34#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/ModRef.h"
36#include "llvm/Support/raw_ostream.h"
37#include <algorithm>
38#include <cassert>
39#include <cstddef>
40#include <cstdint>
41#include <limits>
42#include <optional>
43#include <string>
44#include <tuple>
45#include <utility>
46
47using namespace llvm;
48
49//===----------------------------------------------------------------------===//
50// Attribute Construction Methods
51//===----------------------------------------------------------------------===//
52
53// allocsize has two integer arguments, but because they're both 32 bits, we can
54// pack them into one 64-bit value, at the cost of making said value
55// nonsensical.
56//
57// In order to do this, we need to reserve one value of the second (optional)
58// allocsize argument to signify "not present."
59static const unsigned AllocSizeNumElemsNotPresent = -1;
60
61static uint64_t packAllocSizeArgs(unsigned ElemSizeArg,
62 const std::optional<unsigned> &NumElemsArg) {
63 assert((!NumElemsArg || *NumElemsArg != AllocSizeNumElemsNotPresent) &&
64 "Attempting to pack a reserved value");
65
66 return uint64_t(ElemSizeArg) << 32 |
67 NumElemsArg.value_or(u: AllocSizeNumElemsNotPresent);
68}
69
70static std::pair<unsigned, std::optional<unsigned>>
71unpackAllocSizeArgs(uint64_t Num) {
72 unsigned NumElems = Num & std::numeric_limits<unsigned>::max();
73 unsigned ElemSizeArg = Num >> 32;
74
75 std::optional<unsigned> NumElemsArg;
76 if (NumElems != AllocSizeNumElemsNotPresent)
77 NumElemsArg = NumElems;
78 return std::make_pair(x&: ElemSizeArg, y&: NumElemsArg);
79}
80
81static uint64_t packVScaleRangeArgs(unsigned MinValue,
82 std::optional<unsigned> MaxValue) {
83 return uint64_t(MinValue) << 32 | MaxValue.value_or(u: 0);
84}
85
86static std::pair<unsigned, std::optional<unsigned>>
87unpackVScaleRangeArgs(uint64_t Value) {
88 unsigned MaxValue = Value & std::numeric_limits<unsigned>::max();
89 unsigned MinValue = Value >> 32;
90
91 return std::make_pair(x&: MinValue,
92 y: MaxValue > 0 ? MaxValue : std::optional<unsigned>());
93}
94
95Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
96 uint64_t Val) {
97 bool IsIntAttr = Attribute::isIntAttrKind(Kind);
98 assert((IsIntAttr || Attribute::isEnumAttrKind(Kind)) &&
99 "Not an enum or int attribute");
100
101 LLVMContextImpl *pImpl = Context.pImpl;
102 if (!IsIntAttr) {
103 assert(Val == 0 && "Value must be zero for enum attributes");
104 EnumAttributeImpl *&PA = pImpl->EnumAttrs[Kind - Attribute::FirstEnumAttr];
105 if (!PA)
106 PA = new (pImpl->Alloc) EnumAttributeImpl(Kind);
107 return Attribute(PA);
108 }
109
110 FoldingSetInsertToken Token;
111 IntAttributeImpl *PA = pImpl->IntAttrs.lookup(Key: {Kind, Val}, Token);
112 if (!PA) {
113 // If we didn't find any existing attributes of the same shape then create a
114 // new one and insert it.
115 PA = new (pImpl->Alloc) IntAttributeImpl(Kind, Val);
116 pImpl->IntAttrs.insert(N: PA, Token);
117 }
118
119 // Return the Attribute that we found or created.
120 return Attribute(PA);
121}
122
123Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
124 LLVMContextImpl *pImpl = Context.pImpl;
125 FoldingSetInsertToken Token;
126 StringAttributeImpl *PA = pImpl->StringAttrs.lookup(Key: {Kind, Val}, Token);
127 if (!PA) {
128 // If we didn't find any existing attributes of the same shape then create a
129 // new one and insert it.
130 void *Mem =
131 pImpl->Alloc.Allocate(Size: StringAttributeImpl::totalSizeToAlloc(Kind, Val),
132 Alignment: alignof(StringAttributeImpl));
133 PA = new (Mem) StringAttributeImpl(Kind, Val);
134 pImpl->StringAttrs.insert(N: PA, Token);
135 }
136
137 // Return the Attribute that we found or created.
138 return Attribute(PA);
139}
140
141Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
142 Type *Ty) {
143 assert(Attribute::isTypeAttrKind(Kind) && "Not a type attribute");
144 LLVMContextImpl *pImpl = Context.pImpl;
145 FoldingSetInsertToken Token;
146 TypeAttributeImpl *PA = pImpl->TypeAttrs.lookup(Key: {Kind, Ty}, Token);
147 if (!PA) {
148 // If we didn't find any existing attributes of the same shape then create a
149 // new one and insert it.
150 PA = new (pImpl->Alloc) TypeAttributeImpl(Kind, Ty);
151 pImpl->TypeAttrs.insert(N: PA, Token);
152 }
153
154 // Return the Attribute that we found or created.
155 return Attribute(PA);
156}
157
158Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
159 const ConstantRange &CR) {
160 assert(Attribute::isConstantRangeAttrKind(Kind) &&
161 "Not a ConstantRange attribute");
162 assert(!CR.isFullSet() && "ConstantRange attribute must not be full");
163 LLVMContextImpl *pImpl = Context.pImpl;
164 FoldingSetNodeID ID;
165 ID.AddInteger(I: Kind);
166 CR.getLower().Profile(id&: ID);
167 CR.getUpper().Profile(id&: ID);
168
169 FoldingSetInsertToken Token;
170 AttributeImpl *PA = pImpl->AttrsSet.lookup(ID, Token);
171
172 if (!PA) {
173 // If we didn't find any existing attributes of the same shape then create a
174 // new one and insert it.
175 PA = new (pImpl->ConstantRangeAttributeAlloc.Allocate())
176 ConstantRangeAttributeImpl(Kind, CR);
177 pImpl->AttrsSet.insert(N: PA, Token);
178 }
179
180 // Return the Attribute that we found or created.
181 return Attribute(PA);
182}
183
184Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
185 ArrayRef<ConstantRange> Val) {
186 assert(Attribute::isConstantRangeListAttrKind(Kind) &&
187 "Not a ConstantRangeList attribute");
188 LLVMContextImpl *pImpl = Context.pImpl;
189 FoldingSetNodeID ID;
190 ID.AddInteger(I: Kind);
191 ID.AddInteger(I: Val.size());
192 for (auto &CR : Val) {
193 CR.getLower().Profile(id&: ID);
194 CR.getUpper().Profile(id&: ID);
195 }
196
197 FoldingSetInsertToken Token;
198 AttributeImpl *PA = pImpl->AttrsSet.lookup(ID, Token);
199
200 if (!PA) {
201 // If we didn't find any existing attributes of the same shape then create a
202 // new one and insert it.
203 // ConstantRangeListAttributeImpl is a dynamically sized class and cannot
204 // use SpecificBumpPtrAllocator. Instead, we use normal Alloc for
205 // allocation and record the allocated pointer in
206 // `ConstantRangeListAttributes`. LLVMContext destructor will call the
207 // destructor of the allocated pointer explicitly.
208 void *Mem = pImpl->Alloc.Allocate(
209 Size: ConstantRangeListAttributeImpl::totalSizeToAlloc(Val),
210 Alignment: alignof(ConstantRangeListAttributeImpl));
211 PA = new (Mem) ConstantRangeListAttributeImpl(Kind, Val);
212 pImpl->AttrsSet.insert(N: PA, Token);
213 pImpl->ConstantRangeListAttributes.push_back(
214 x: reinterpret_cast<ConstantRangeListAttributeImpl *>(PA));
215 }
216
217 // Return the Attribute that we found or created.
218 return Attribute(PA);
219}
220
221Attribute Attribute::getWithAlignment(LLVMContext &Context, Align A) {
222 assert(A <= llvm::Value::MaximumAlignment && "Alignment too large.");
223 return get(Context, Kind: Alignment, Val: A.value());
224}
225
226Attribute Attribute::getWithStackAlignment(LLVMContext &Context, Align A) {
227 assert(A <= 0x100 && "Alignment too large.");
228 return get(Context, Kind: StackAlignment, Val: A.value());
229}
230
231Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context,
232 uint64_t Bytes) {
233 assert(Bytes && "Bytes must be non-zero.");
234 return get(Context, Kind: Dereferenceable, Val: Bytes);
235}
236
237Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context,
238 uint64_t Bytes) {
239 assert(Bytes && "Bytes must be non-zero.");
240 return get(Context, Kind: DereferenceableOrNull, Val: Bytes);
241}
242
243Attribute Attribute::getWithByValType(LLVMContext &Context, Type *Ty) {
244 return get(Context, Kind: ByVal, Ty);
245}
246
247Attribute Attribute::getWithStructRetType(LLVMContext &Context, Type *Ty) {
248 return get(Context, Kind: StructRet, Ty);
249}
250
251Attribute Attribute::getWithByRefType(LLVMContext &Context, Type *Ty) {
252 return get(Context, Kind: ByRef, Ty);
253}
254
255Attribute Attribute::getWithPreallocatedType(LLVMContext &Context, Type *Ty) {
256 return get(Context, Kind: Preallocated, Ty);
257}
258
259Attribute Attribute::getWithInAllocaType(LLVMContext &Context, Type *Ty) {
260 return get(Context, Kind: InAlloca, Ty);
261}
262
263Attribute Attribute::getWithUWTableKind(LLVMContext &Context,
264 UWTableKind Kind) {
265 return get(Context, Kind: UWTable, Val: uint64_t(Kind));
266}
267
268Attribute Attribute::getWithMemoryEffects(LLVMContext &Context,
269 MemoryEffects ME) {
270 return get(Context, Kind: Memory, Val: ME.toIntValue());
271}
272
273Attribute Attribute::getWithNoFPClass(LLVMContext &Context,
274 FPClassTest ClassMask) {
275 return get(Context, Kind: NoFPClass, Val: ClassMask);
276}
277
278Attribute Attribute::getWithDeadOnReturnInfo(LLVMContext &Context,
279 DeadOnReturnInfo DI) {
280 return get(Context, Kind: DeadOnReturn, Val: DI.toIntValue());
281}
282
283Attribute Attribute::getWithCaptureInfo(LLVMContext &Context, CaptureInfo CI) {
284 return get(Context, Kind: Captures, Val: CI.toIntValue());
285}
286
287Attribute
288Attribute::getWithAllocSizeArgs(LLVMContext &Context, unsigned ElemSizeArg,
289 const std::optional<unsigned> &NumElemsArg) {
290 assert(!(ElemSizeArg == 0 && NumElemsArg == 0) &&
291 "Invalid allocsize arguments -- given allocsize(0, 0)");
292 return get(Context, Kind: AllocSize, Val: packAllocSizeArgs(ElemSizeArg, NumElemsArg));
293}
294
295Attribute Attribute::getWithAllocKind(LLVMContext &Context, AllocFnKind Kind) {
296 return get(Context, Kind: AllocKind, Val: static_cast<uint64_t>(Kind));
297}
298
299Attribute Attribute::getWithVScaleRangeArgs(LLVMContext &Context,
300 unsigned MinValue,
301 unsigned MaxValue) {
302 return get(Context, Kind: VScaleRange, Val: packVScaleRangeArgs(MinValue, MaxValue));
303}
304
305Attribute::AttrKind Attribute::getAttrKindFromName(StringRef AttrName) {
306 return StringSwitch<Attribute::AttrKind>(AttrName)
307#define GET_ATTR_NAMES
308#define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) \
309 .Case(#DISPLAY_NAME, Attribute::ENUM_NAME)
310#include "llvm/IR/Attributes.inc"
311 .Default(Value: Attribute::None);
312}
313
314StringRef Attribute::getNameFromAttrKind(Attribute::AttrKind AttrKind) {
315 switch (AttrKind) {
316#define GET_ATTR_NAMES
317#define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) \
318 case Attribute::ENUM_NAME: \
319 return #DISPLAY_NAME;
320#include "llvm/IR/Attributes.inc"
321 case Attribute::None:
322 return "none";
323 default:
324 llvm_unreachable("invalid Kind");
325 }
326}
327
328//===----------------------------------------------------------------------===//
329// Attribute Accessor Methods
330//===----------------------------------------------------------------------===//
331
332bool Attribute::isEnumAttribute() const {
333 return pImpl && pImpl->isEnumAttribute();
334}
335
336bool Attribute::isIntAttribute() const {
337 return pImpl && pImpl->isIntAttribute();
338}
339
340bool Attribute::isStringAttribute() const {
341 return pImpl && pImpl->isStringAttribute();
342}
343
344bool Attribute::isTypeAttribute() const {
345 return pImpl && pImpl->isTypeAttribute();
346}
347
348bool Attribute::isConstantRangeAttribute() const {
349 return pImpl && pImpl->isConstantRangeAttribute();
350}
351
352bool Attribute::isConstantRangeListAttribute() const {
353 return pImpl && pImpl->isConstantRangeListAttribute();
354}
355
356Attribute::AttrKind Attribute::getKindAsEnum() const {
357 if (!pImpl) return None;
358 assert(hasKindAsEnum() &&
359 "Invalid attribute type to get the kind as an enum!");
360 return pImpl->getKindAsEnum();
361}
362
363uint64_t Attribute::getValueAsInt() const {
364 if (!pImpl) return 0;
365 assert(isIntAttribute() &&
366 "Expected the attribute to be an integer attribute!");
367 return pImpl->getValueAsInt();
368}
369
370bool Attribute::getValueAsBool() const {
371 if (!pImpl) return false;
372 assert(isStringAttribute() &&
373 "Expected the attribute to be a string attribute!");
374 return pImpl->getValueAsBool();
375}
376
377StringRef Attribute::getKindAsString() const {
378 if (!pImpl) return {};
379 assert(isStringAttribute() &&
380 "Invalid attribute type to get the kind as a string!");
381 return pImpl->getKindAsString();
382}
383
384StringRef Attribute::getValueAsString() const {
385 if (!pImpl) return {};
386 assert(isStringAttribute() &&
387 "Invalid attribute type to get the value as a string!");
388 return pImpl->getValueAsString();
389}
390
391Type *Attribute::getValueAsType() const {
392 if (!pImpl) return {};
393 assert(isTypeAttribute() &&
394 "Invalid attribute type to get the value as a type!");
395 return pImpl->getValueAsType();
396}
397
398const ConstantRange &Attribute::getValueAsConstantRange() const {
399 assert(isConstantRangeAttribute() &&
400 "Invalid attribute type to get the value as a ConstantRange!");
401 return pImpl->getValueAsConstantRange();
402}
403
404ArrayRef<ConstantRange> Attribute::getValueAsConstantRangeList() const {
405 assert(isConstantRangeListAttribute() &&
406 "Invalid attribute type to get the value as a ConstantRangeList!");
407 return pImpl->getValueAsConstantRangeList();
408}
409
410bool Attribute::hasAttribute(AttrKind Kind) const {
411 return (pImpl && pImpl->hasAttribute(A: Kind)) || (!pImpl && Kind == None);
412}
413
414bool Attribute::hasAttribute(StringRef Kind) const {
415 if (!isStringAttribute()) return false;
416 return pImpl && pImpl->hasAttribute(Kind);
417}
418
419MaybeAlign Attribute::getAlignment() const {
420 assert(hasAttribute(Attribute::Alignment) &&
421 "Trying to get alignment from non-alignment attribute!");
422 return MaybeAlign(pImpl->getValueAsInt());
423}
424
425MaybeAlign Attribute::getStackAlignment() const {
426 assert(hasAttribute(Attribute::StackAlignment) &&
427 "Trying to get alignment from non-alignment attribute!");
428 return MaybeAlign(pImpl->getValueAsInt());
429}
430
431uint64_t Attribute::getDereferenceableBytes() const {
432 assert(hasAttribute(Attribute::Dereferenceable) &&
433 "Trying to get dereferenceable bytes from "
434 "non-dereferenceable attribute!");
435 return pImpl->getValueAsInt();
436}
437
438DeadOnReturnInfo Attribute::getDeadOnReturnInfo() const {
439 assert(hasAttribute(Attribute::DeadOnReturn) &&
440 "Trying to get dead_on_return bytes from"
441 "a parameter without such an attribute!");
442 return DeadOnReturnInfo::createFromIntValue(Data: pImpl->getValueAsInt());
443}
444
445uint64_t Attribute::getDereferenceableOrNullBytes() const {
446 assert(hasAttribute(Attribute::DereferenceableOrNull) &&
447 "Trying to get dereferenceable bytes from "
448 "non-dereferenceable attribute!");
449 return pImpl->getValueAsInt();
450}
451
452std::pair<unsigned, std::optional<unsigned>>
453Attribute::getAllocSizeArgs() const {
454 assert(hasAttribute(Attribute::AllocSize) &&
455 "Trying to get allocsize args from non-allocsize attribute");
456 return unpackAllocSizeArgs(Num: pImpl->getValueAsInt());
457}
458
459unsigned Attribute::getVScaleRangeMin() const {
460 assert(hasAttribute(Attribute::VScaleRange) &&
461 "Trying to get vscale args from non-vscale attribute");
462 return unpackVScaleRangeArgs(Value: pImpl->getValueAsInt()).first;
463}
464
465std::optional<unsigned> Attribute::getVScaleRangeMax() const {
466 assert(hasAttribute(Attribute::VScaleRange) &&
467 "Trying to get vscale args from non-vscale attribute");
468 return unpackVScaleRangeArgs(Value: pImpl->getValueAsInt()).second;
469}
470
471UWTableKind Attribute::getUWTableKind() const {
472 assert(hasAttribute(Attribute::UWTable) &&
473 "Trying to get unwind table kind from non-uwtable attribute");
474 return UWTableKind(pImpl->getValueAsInt());
475}
476
477AllocFnKind Attribute::getAllocKind() const {
478 assert(hasAttribute(Attribute::AllocKind) &&
479 "Trying to get allockind value from non-allockind attribute");
480 return AllocFnKind(pImpl->getValueAsInt());
481}
482
483MemoryEffects Attribute::getMemoryEffects() const {
484 assert(hasAttribute(Attribute::Memory) &&
485 "Can only call getMemoryEffects() on memory attribute");
486 return MemoryEffects::createFromIntValue(Data: pImpl->getValueAsInt());
487}
488
489CaptureInfo Attribute::getCaptureInfo() const {
490 assert(hasAttribute(Attribute::Captures) &&
491 "Can only call getCaptureInfo() on captures attribute");
492 return CaptureInfo::createFromIntValue(Data: pImpl->getValueAsInt());
493}
494
495DenormalFPEnv Attribute::getDenormalFPEnv() const {
496 return DenormalFPEnv::createFromIntValue(Data: pImpl->getValueAsInt());
497}
498
499FPClassTest Attribute::getNoFPClass() const {
500 assert(hasAttribute(Attribute::NoFPClass) &&
501 "Can only call getNoFPClass() on nofpclass attribute");
502 return static_cast<FPClassTest>(pImpl->getValueAsInt());
503}
504
505const ConstantRange &Attribute::getRange() const {
506 assert(hasAttribute(Attribute::Range) &&
507 "Trying to get range args from non-range attribute");
508 return pImpl->getValueAsConstantRange();
509}
510
511ArrayRef<ConstantRange> Attribute::getInitializes() const {
512 assert(hasAttribute(Attribute::Initializes) &&
513 "Trying to get initializes attr from non-ConstantRangeList attribute");
514 return pImpl->getValueAsConstantRangeList();
515}
516
517static const char *getModRefStr(ModRefInfo MR) {
518 switch (MR) {
519 case ModRefInfo::NoModRef:
520 return "none";
521 case ModRefInfo::Ref:
522 return "read";
523 case ModRefInfo::Mod:
524 return "write";
525 case ModRefInfo::ModRef:
526 return "readwrite";
527 }
528 llvm_unreachable("Invalid ModRefInfo");
529}
530
531std::string Attribute::getAsString(bool InAttrGrp) const {
532 if (!pImpl) return {};
533
534 if (isEnumAttribute())
535 return getNameFromAttrKind(AttrKind: getKindAsEnum()).str();
536
537 if (isTypeAttribute()) {
538 std::string Result = getNameFromAttrKind(AttrKind: getKindAsEnum()).str();
539 Result += '(';
540 raw_string_ostream OS(Result);
541 getValueAsType()->print(O&: OS, IsForDebug: false, NoDetails: true);
542 Result += ')';
543 return Result;
544 }
545
546 // FIXME: These should be output like this:
547 //
548 // align=4
549 // alignstack=8
550 //
551 if (hasAttribute(Kind: Attribute::Alignment))
552 return (InAttrGrp ? "align=" + Twine(getValueAsInt())
553 : "align " + Twine(getValueAsInt()))
554 .str();
555
556 auto AttrWithBytesToString = [&](const char *Name) {
557 return (InAttrGrp ? Name + ("=" + Twine(getValueAsInt()))
558 : Name + ("(" + Twine(getValueAsInt())) + ")")
559 .str();
560 };
561
562 if (hasAttribute(Kind: Attribute::StackAlignment))
563 return AttrWithBytesToString("alignstack");
564
565 if (hasAttribute(Kind: Attribute::Dereferenceable))
566 return AttrWithBytesToString("dereferenceable");
567
568 if (hasAttribute(Kind: Attribute::DereferenceableOrNull))
569 return AttrWithBytesToString("dereferenceable_or_null");
570
571 if (hasAttribute(Kind: Attribute::DeadOnReturn)) {
572 uint64_t DeadBytes = getValueAsInt();
573 if (DeadBytes == std::numeric_limits<uint64_t>::max())
574 return "dead_on_return";
575 return AttrWithBytesToString("dead_on_return");
576 }
577
578 if (hasAttribute(Kind: Attribute::AllocSize)) {
579 unsigned ElemSize;
580 std::optional<unsigned> NumElems;
581 std::tie(args&: ElemSize, args&: NumElems) = getAllocSizeArgs();
582
583 return (NumElems
584 ? "allocsize(" + Twine(ElemSize) + "," + Twine(*NumElems) + ")"
585 : "allocsize(" + Twine(ElemSize) + ")")
586 .str();
587 }
588
589 if (hasAttribute(Kind: Attribute::VScaleRange)) {
590 unsigned MinValue = getVScaleRangeMin();
591 std::optional<unsigned> MaxValue = getVScaleRangeMax();
592 return ("vscale_range(" + Twine(MinValue) + "," +
593 Twine(MaxValue.value_or(u: 0)) + ")")
594 .str();
595 }
596
597 if (hasAttribute(Kind: Attribute::UWTable)) {
598 UWTableKind Kind = getUWTableKind();
599 assert(Kind != UWTableKind::None && "uwtable attribute should not be none");
600 return Kind == UWTableKind::Default ? "uwtable" : "uwtable(sync)";
601 }
602
603 if (hasAttribute(Kind: Attribute::AllocKind)) {
604 AllocFnKind Kind = getAllocKind();
605 SmallVector<StringRef> parts;
606 if ((Kind & AllocFnKind::Alloc) != AllocFnKind::Unknown)
607 parts.push_back(Elt: "alloc");
608 if ((Kind & AllocFnKind::Realloc) != AllocFnKind::Unknown)
609 parts.push_back(Elt: "realloc");
610 if ((Kind & AllocFnKind::Free) != AllocFnKind::Unknown)
611 parts.push_back(Elt: "free");
612 if ((Kind & AllocFnKind::Uninitialized) != AllocFnKind::Unknown)
613 parts.push_back(Elt: "uninitialized");
614 if ((Kind & AllocFnKind::Zeroed) != AllocFnKind::Unknown)
615 parts.push_back(Elt: "zeroed");
616 if ((Kind & AllocFnKind::Aligned) != AllocFnKind::Unknown)
617 parts.push_back(Elt: "aligned");
618 return ("allockind(\"" +
619 Twine(llvm::join(Begin: parts.begin(), End: parts.end(), Separator: ",")) + "\")")
620 .str();
621 }
622
623 if (hasAttribute(Kind: Attribute::Memory)) {
624 std::string Result;
625 raw_string_ostream OS(Result);
626 bool First = true;
627 OS << "memory(";
628
629 MemoryEffects ME = getMemoryEffects();
630
631 // Print access kind for "other" as the default access kind. This way it
632 // will apply to any new location kinds that get split out of "other".
633 ModRefInfo OtherMR = ME.getModRef(Loc: IRMemLocation::Other);
634 if (OtherMR != ModRefInfo::NoModRef || ME.getModRef() == OtherMR) {
635 First = false;
636 OS << getModRefStr(MR: OtherMR);
637 }
638
639 bool TargetPrintedForAll = false;
640 for (auto Loc : MemoryEffects::locations()) {
641 ModRefInfo MR = ME.getModRef(Loc);
642 if (MR == OtherMR)
643 continue;
644
645 if (!First && !TargetPrintedForAll)
646 OS << ", ";
647 First = false;
648
649 // isTargetMemLocSameForAll is fine for target location < 3
650 // If more targets are added it should do something like:
651 // memory(target_mem:read, target_mem3:none, target_mem5:write).
652 if (ME.isTargetMemLoc(Loc) && ME.isTargetMemLocSameForAll()) {
653 if (!TargetPrintedForAll) {
654 OS << "target_mem: ";
655 OS << getModRefStr(MR);
656 TargetPrintedForAll = true;
657 }
658 // Only works when target memories are last to be listed in Location.
659 continue;
660 }
661
662 switch (Loc) {
663 case IRMemLocation::ArgMem:
664 OS << "argmem: ";
665 break;
666 case IRMemLocation::InaccessibleMem:
667 OS << "inaccessiblemem: ";
668 break;
669 case IRMemLocation::ErrnoMem:
670 OS << "errnomem: ";
671 break;
672 case IRMemLocation::Other:
673 llvm_unreachable("This is represented as the default access kind");
674 case IRMemLocation::TargetMem0:
675 OS << "target_mem0: ";
676 break;
677 case IRMemLocation::TargetMem1:
678 OS << "target_mem1: ";
679 break;
680 }
681 OS << getModRefStr(MR);
682 }
683 OS << ")";
684 return Result;
685 }
686
687 if (hasAttribute(Kind: Attribute::Captures)) {
688 std::string Result;
689 raw_string_ostream(Result) << getCaptureInfo();
690 return Result;
691 }
692
693 if (hasAttribute(Kind: Attribute::DenormalFPEnv)) {
694 std::string Result = "denormal_fpenv(";
695 raw_string_ostream OS(Result);
696
697 struct DenormalFPEnv FPEnv = getDenormalFPEnv();
698 FPEnv.print(OS, /*OmitIfSame=*/true);
699
700 OS << ')';
701 return Result;
702 }
703
704 if (hasAttribute(Kind: Attribute::NoFPClass)) {
705 std::string Result = "nofpclass";
706 raw_string_ostream(Result) << getNoFPClass();
707 return Result;
708 }
709
710 if (hasAttribute(Kind: Attribute::Range)) {
711 std::string Result;
712 raw_string_ostream OS(Result);
713 const ConstantRange &CR = getValueAsConstantRange();
714 OS << "range(";
715 OS << "i" << CR.getBitWidth() << " ";
716 OS << CR.getLower() << ", " << CR.getUpper();
717 OS << ")";
718 return Result;
719 }
720
721 if (hasAttribute(Kind: Attribute::Initializes)) {
722 std::string Result;
723 raw_string_ostream OS(Result);
724 ConstantRangeList CRL = getInitializes();
725 OS << "initializes(";
726 CRL.print(OS);
727 OS << ")";
728 return Result;
729 }
730
731 // Convert target-dependent attributes to strings of the form:
732 //
733 // "kind"
734 // "kind" = "value"
735 //
736 if (isStringAttribute()) {
737 std::string Result;
738 {
739 raw_string_ostream OS(Result);
740 OS << '"' << getKindAsString() << '"';
741
742 // Since some attribute strings contain special characters that cannot be
743 // printable, those have to be escaped to make the attribute value
744 // printable as is. e.g. "\01__gnu_mcount_nc"
745 const auto &AttrVal = pImpl->getValueAsString();
746 if (!AttrVal.empty()) {
747 OS << "=\"";
748 printEscapedString(Name: AttrVal, Out&: OS);
749 OS << "\"";
750 }
751 }
752 return Result;
753 }
754
755 llvm_unreachable("Unknown attribute");
756}
757
758bool Attribute::hasParentContext(LLVMContext &C) const {
759 assert(isValid() && "invalid Attribute doesn't refer to any context");
760 LLVMContextImpl *pI = C.pImpl;
761 FoldingSetInsertToken Token;
762 if (pImpl->isEnumAttribute())
763 return pI->EnumAttrs[pImpl->getKindAsEnum() - FirstEnumAttr] == pImpl;
764 if (pImpl->isIntAttribute())
765 return pI->IntAttrs.lookup(Key: {pImpl->getKindAsEnum(), pImpl->getValueAsInt()},
766 Token) == pImpl;
767 if (pImpl->isStringAttribute())
768 return pI->StringAttrs.lookup(
769 Key: {pImpl->getKindAsString(), pImpl->getValueAsString()}, Token) ==
770 pImpl;
771 if (pImpl->isTypeAttribute())
772 return pI->TypeAttrs.lookup(
773 Key: {pImpl->getKindAsEnum(), pImpl->getValueAsType()}, Token) ==
774 pImpl;
775 FoldingSetNodeID ID;
776 pImpl->Profile(ID);
777 return pI->AttrsSet.lookup(ID, Token) == pImpl;
778}
779
780int Attribute::cmpKind(Attribute A) const {
781 if (!pImpl && !A.pImpl)
782 return 0;
783 if (!pImpl)
784 return 1;
785 if (!A.pImpl)
786 return -1;
787 return pImpl->cmp(AI: *A.pImpl, /*KindOnly=*/true);
788}
789
790bool Attribute::operator<(Attribute A) const {
791 if (!pImpl && !A.pImpl) return false;
792 if (!pImpl) return true;
793 if (!A.pImpl) return false;
794 return *pImpl < *A.pImpl;
795}
796
797enum AttributeProperty {
798 FnAttr = (1 << 0),
799 ParamAttr = (1 << 1),
800 RetAttr = (1 << 2),
801 IntersectPreserve = (0 << 3),
802 IntersectAnd = (1 << 3),
803 IntersectMin = (2 << 3),
804 IntersectCustom = (3 << 3),
805 IntersectPropertyMask = (3 << 3),
806 ABIAttr = (1 << 5),
807};
808
809#define GET_ATTR_PROP_TABLE
810#include "llvm/IR/Attributes.inc"
811
812static unsigned getAttributeProperties(Attribute::AttrKind Kind) {
813 unsigned Index = Kind - 1;
814 assert(Index < std::size(AttrPropTable) && "Invalid attribute kind");
815 return AttrPropTable[Index];
816}
817
818static bool hasAttributeProperty(Attribute::AttrKind Kind,
819 AttributeProperty Prop) {
820 return getAttributeProperties(Kind) & Prop;
821}
822
823bool Attribute::canUseAsFnAttr(AttrKind Kind) {
824 return hasAttributeProperty(Kind, Prop: AttributeProperty::FnAttr);
825}
826
827bool Attribute::canUseAsParamAttr(AttrKind Kind) {
828 return hasAttributeProperty(Kind, Prop: AttributeProperty::ParamAttr);
829}
830
831bool Attribute::canUseAsRetAttr(AttrKind Kind) {
832 return hasAttributeProperty(Kind, Prop: AttributeProperty::RetAttr);
833}
834
835bool Attribute::isABIAttr(AttrKind Kind) {
836 return hasAttributeProperty(Kind, Prop: AttributeProperty::ABIAttr);
837}
838
839static bool hasIntersectProperty(Attribute::AttrKind Kind,
840 AttributeProperty Prop) {
841 assert((Prop == AttributeProperty::IntersectPreserve ||
842 Prop == AttributeProperty::IntersectAnd ||
843 Prop == AttributeProperty::IntersectMin ||
844 Prop == AttributeProperty::IntersectCustom) &&
845 "Unknown intersect property");
846 return (getAttributeProperties(Kind) &
847 AttributeProperty::IntersectPropertyMask) == Prop;
848}
849
850bool Attribute::intersectMustPreserve(AttrKind Kind) {
851 return hasIntersectProperty(Kind, Prop: AttributeProperty::IntersectPreserve);
852}
853bool Attribute::intersectWithAnd(AttrKind Kind) {
854 return hasIntersectProperty(Kind, Prop: AttributeProperty::IntersectAnd);
855}
856bool Attribute::intersectWithMin(AttrKind Kind) {
857 return hasIntersectProperty(Kind, Prop: AttributeProperty::IntersectMin);
858}
859bool Attribute::intersectWithCustom(AttrKind Kind) {
860 return hasIntersectProperty(Kind, Prop: AttributeProperty::IntersectCustom);
861}
862
863//===----------------------------------------------------------------------===//
864// AttributeImpl Definition
865//===----------------------------------------------------------------------===//
866
867bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
868 if (isStringAttribute()) return false;
869 return getKindAsEnum() == A;
870}
871
872bool AttributeImpl::hasAttribute(StringRef Kind) const {
873 if (!isStringAttribute()) return false;
874 return getKindAsString() == Kind;
875}
876
877Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
878 assert(isEnumAttribute() || isIntAttribute() || isTypeAttribute() ||
879 isConstantRangeAttribute() || isConstantRangeListAttribute());
880 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
881}
882
883uint64_t AttributeImpl::getValueAsInt() const {
884 assert(isIntAttribute());
885 return static_cast<const IntAttributeImpl *>(this)->getValue();
886}
887
888bool AttributeImpl::getValueAsBool() const {
889 assert(getValueAsString().empty() || getValueAsString() == "false" || getValueAsString() == "true");
890 return getValueAsString() == "true";
891}
892
893StringRef AttributeImpl::getKindAsString() const {
894 assert(isStringAttribute());
895 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
896}
897
898StringRef AttributeImpl::getValueAsString() const {
899 assert(isStringAttribute());
900 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
901}
902
903Type *AttributeImpl::getValueAsType() const {
904 assert(isTypeAttribute());
905 return static_cast<const TypeAttributeImpl *>(this)->getTypeValue();
906}
907
908const ConstantRange &AttributeImpl::getValueAsConstantRange() const {
909 assert(isConstantRangeAttribute());
910 return static_cast<const ConstantRangeAttributeImpl *>(this)
911 ->getConstantRangeValue();
912}
913
914ArrayRef<ConstantRange> AttributeImpl::getValueAsConstantRangeList() const {
915 assert(isConstantRangeListAttribute());
916 return static_cast<const ConstantRangeListAttributeImpl *>(this)
917 ->getConstantRangeListValue();
918}
919
920int AttributeImpl::cmp(const AttributeImpl &AI, bool KindOnly) const {
921 if (this == &AI)
922 return 0;
923
924 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
925 // relative to their enum value) and then strings.
926 if (!isStringAttribute()) {
927 if (AI.isStringAttribute())
928 return -1;
929
930 if (getKindAsEnum() != AI.getKindAsEnum())
931 return getKindAsEnum() < AI.getKindAsEnum() ? -1 : 1;
932 else if (KindOnly)
933 return 0;
934
935 assert(!AI.isEnumAttribute() && "Non-unique attribute");
936 assert(!AI.isTypeAttribute() && "Comparison of types would be unstable");
937 assert(!AI.isConstantRangeAttribute() && "Unclear how to compare ranges");
938 assert(!AI.isConstantRangeListAttribute() &&
939 "Unclear how to compare range list");
940 // TODO: Is this actually needed?
941 assert(AI.isIntAttribute() && "Only possibility left");
942 if (getValueAsInt() < AI.getValueAsInt())
943 return -1;
944 return getValueAsInt() == AI.getValueAsInt() ? 0 : 1;
945 }
946 if (!AI.isStringAttribute())
947 return 1;
948 if (KindOnly)
949 return getKindAsString().compare(RHS: AI.getKindAsString());
950 if (getKindAsString() == AI.getKindAsString())
951 return getValueAsString().compare(RHS: AI.getValueAsString());
952 return getKindAsString().compare(RHS: AI.getKindAsString());
953}
954
955bool AttributeImpl::operator<(const AttributeImpl &AI) const {
956 return cmp(AI, /*KindOnly=*/false) < 0;
957}
958
959//===----------------------------------------------------------------------===//
960// AttributeSet Definition
961//===----------------------------------------------------------------------===//
962
963AttributeSet::AttributeSet(AttributeSetNode *ASN) : SetNode(ASN) {}
964
965AttributeSetNode *AttributeSet::getNode() const {
966 return static_cast<AttributeSetNode *>(SetNode);
967}
968
969AttributeSet AttributeSet::get(LLVMContext &C, const AttrBuilder &B) {
970 return AttributeSet(AttributeSetNode::get(C, B));
971}
972
973AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<Attribute> Attrs) {
974 return AttributeSet(AttributeSetNode::get(C, Attrs));
975}
976
977AttributeSet AttributeSet::addAttribute(LLVMContext &C,
978 Attribute::AttrKind Kind) const {
979 if (hasAttribute(Kind)) return *this;
980 AttrBuilder B(C);
981 B.addAttribute(Val: Kind);
982 return addAttributes(C, AS: AttributeSet::get(C, B));
983}
984
985AttributeSet AttributeSet::addAttribute(LLVMContext &C, StringRef Kind,
986 StringRef Value) const {
987 AttrBuilder B(C);
988 B.addAttribute(A: Kind, V: Value);
989 return addAttributes(C, AS: AttributeSet::get(C, B));
990}
991
992AttributeSet AttributeSet::addAttributes(LLVMContext &C,
993 const AttributeSet AS) const {
994 if (!hasAttributes())
995 return AS;
996
997 if (!AS.hasAttributes())
998 return *this;
999
1000 AttrBuilder B(C, *this);
1001 B.merge(B: AttrBuilder(C, AS));
1002 return get(C, B);
1003}
1004
1005AttributeSet AttributeSet::addAttributes(LLVMContext &C,
1006 const AttrBuilder &B) const {
1007 if (!hasAttributes())
1008 return get(C, B);
1009
1010 if (!B.hasAttributes())
1011 return *this;
1012
1013 AttrBuilder Merged(C, *this);
1014 Merged.merge(B);
1015 return get(C, B: Merged);
1016}
1017
1018AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
1019 Attribute::AttrKind Kind) const {
1020 if (!hasAttribute(Kind)) return *this;
1021 AttrBuilder B(C, *this);
1022 B.removeAttribute(Val: Kind);
1023 return get(C, B);
1024}
1025
1026AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
1027 StringRef Kind) const {
1028 if (!hasAttribute(Kind)) return *this;
1029 AttrBuilder B(C, *this);
1030 B.removeAttribute(A: Kind);
1031 return get(C, B);
1032}
1033
1034AttributeSet AttributeSet::removeAttributes(LLVMContext &C,
1035 const AttributeMask &Attrs) const {
1036 // If there is nothing to remove, directly return the original set.
1037 if (none_of(Range: *this, P: [&](Attribute A) { return Attrs.contains(A); }))
1038 return *this;
1039
1040 AttrBuilder B(C, *this);
1041 B.remove(AM: Attrs);
1042 return get(C, B);
1043}
1044
1045std::optional<AttributeSet>
1046AttributeSet::intersectWith(LLVMContext &C, AttributeSet Other) const {
1047 if (*this == Other)
1048 return *this;
1049
1050 AttrBuilder Intersected(C);
1051 // Iterate over both attr sets at once.
1052 auto ItBegin0 = begin();
1053 auto ItEnd0 = end();
1054 auto ItBegin1 = Other.begin();
1055 auto ItEnd1 = Other.end();
1056
1057 while (ItBegin0 != ItEnd0 || ItBegin1 != ItEnd1) {
1058 // Loop through all attributes in both this and Other in sorted order. If
1059 // the attribute is only present in one of the sets, it will be set in
1060 // Attr0. If it is present in both sets both Attr0 and Attr1 will be set.
1061 Attribute Attr0, Attr1;
1062 if (ItBegin1 == ItEnd1)
1063 Attr0 = *ItBegin0++;
1064 else if (ItBegin0 == ItEnd0)
1065 Attr0 = *ItBegin1++;
1066 else {
1067 int Cmp = ItBegin0->cmpKind(A: *ItBegin1);
1068 if (Cmp == 0) {
1069 Attr0 = *ItBegin0++;
1070 Attr1 = *ItBegin1++;
1071 } else if (Cmp < 0)
1072 Attr0 = *ItBegin0++;
1073 else
1074 Attr0 = *ItBegin1++;
1075 }
1076 assert(Attr0.isValid() && "Iteration should always yield a valid attr");
1077
1078 auto IntersectEq = [&]() {
1079 if (!Attr1.isValid())
1080 return false;
1081 if (Attr0 != Attr1)
1082 return false;
1083 Intersected.addAttribute(A: Attr0);
1084 return true;
1085 };
1086
1087 // Non-enum assume we must preserve. Handle early so we can unconditionally
1088 // use Kind below.
1089 if (!Attr0.hasKindAsEnum()) {
1090 if (!IntersectEq())
1091 return std::nullopt;
1092 continue;
1093 }
1094
1095 Attribute::AttrKind Kind = Attr0.getKindAsEnum();
1096 // If we don't have both attributes, then fail if the attribute is
1097 // must-preserve or drop it otherwise.
1098 if (!Attr1.isValid()) {
1099 if (Attribute::intersectMustPreserve(Kind))
1100 return std::nullopt;
1101 continue;
1102 }
1103
1104 // We have both attributes so apply the intersection rule.
1105 assert(Attr1.hasKindAsEnum() && Kind == Attr1.getKindAsEnum() &&
1106 "Iterator picked up two different attributes in the same iteration");
1107
1108 // Attribute we can intersect with "and"
1109 if (Attribute::intersectWithAnd(Kind)) {
1110 assert(Attribute::isEnumAttrKind(Kind) &&
1111 "Invalid attr type of intersectAnd");
1112 Intersected.addAttribute(Val: Kind);
1113 continue;
1114 }
1115
1116 // Attribute we can intersect with "min"
1117 if (Attribute::intersectWithMin(Kind)) {
1118 assert(Attribute::isIntAttrKind(Kind) &&
1119 "Invalid attr type of intersectMin");
1120 uint64_t NewVal = std::min(a: Attr0.getValueAsInt(), b: Attr1.getValueAsInt());
1121 Intersected.addRawIntAttr(Kind, Value: NewVal);
1122 continue;
1123 }
1124 // Attribute we can intersect but need a custom rule for.
1125 if (Attribute::intersectWithCustom(Kind)) {
1126 switch (Kind) {
1127 case Attribute::Alignment:
1128 // If `byval` is present, alignment become must-preserve. This is
1129 // handled below if we have `byval`.
1130 Intersected.addAlignmentAttr(
1131 Align: std::min(a: Attr0.getAlignment().valueOrOne(),
1132 b: Attr1.getAlignment().valueOrOne()));
1133 break;
1134 case Attribute::Memory:
1135 Intersected.addMemoryAttr(ME: Attr0.getMemoryEffects() |
1136 Attr1.getMemoryEffects());
1137 break;
1138 case Attribute::Captures:
1139 Intersected.addCapturesAttr(CI: Attr0.getCaptureInfo() |
1140 Attr1.getCaptureInfo());
1141 break;
1142 case Attribute::NoFPClass:
1143 Intersected.addNoFPClassAttr(NoFPClassMask: Attr0.getNoFPClass() &
1144 Attr1.getNoFPClass());
1145 break;
1146 case Attribute::Range: {
1147 ConstantRange Range0 = Attr0.getRange();
1148 ConstantRange Range1 = Attr1.getRange();
1149 ConstantRange NewRange = Range0.unionWith(CR: Range1);
1150 if (!NewRange.isFullSet())
1151 Intersected.addRangeAttr(CR: NewRange);
1152 } break;
1153 default:
1154 llvm_unreachable("Unknown attribute with custom intersection rule");
1155 }
1156 continue;
1157 }
1158
1159 // Attributes with no intersection rule. Only intersect if they are equal.
1160 // Otherwise fail.
1161 if (!IntersectEq())
1162 return std::nullopt;
1163
1164 // Special handling of `byval`. `byval` essentially turns align attr into
1165 // must-preserve
1166 if (Kind == Attribute::ByVal &&
1167 getAttribute(Kind: Attribute::Alignment) !=
1168 Other.getAttribute(Kind: Attribute::Alignment))
1169 return std::nullopt;
1170 }
1171
1172 return get(C, B: Intersected);
1173}
1174
1175unsigned AttributeSet::getNumAttributes() const {
1176 return SetNode ? getNode()->getNumAttributes() : 0;
1177}
1178
1179
1180bool AttributeSet::hasAttribute(StringRef Kind) const {
1181 return SetNode ? getNode()->hasAttribute(Kind) : false;
1182}
1183
1184Attribute AttributeSet::getAttribute(Attribute::AttrKind Kind) const {
1185 return SetNode ? getNode()->getAttribute(Kind) : Attribute();
1186}
1187
1188Attribute AttributeSet::getAttribute(StringRef Kind) const {
1189 return SetNode ? getNode()->getAttribute(Kind) : Attribute();
1190}
1191
1192MaybeAlign AttributeSet::getAlignment() const {
1193 return SetNode ? getNode()->getAlignment() : std::nullopt;
1194}
1195
1196MaybeAlign AttributeSet::getStackAlignment() const {
1197 return SetNode ? getNode()->getStackAlignment() : std::nullopt;
1198}
1199
1200uint64_t AttributeSet::getDereferenceableBytes() const {
1201 return SetNode ? getNode()->getDereferenceableBytes() : 0;
1202}
1203
1204DeadOnReturnInfo AttributeSet::getDeadOnReturnInfo() const {
1205 return SetNode ? getNode()->getDeadOnReturnInfo() : DeadOnReturnInfo(0);
1206}
1207
1208uint64_t AttributeSet::getDereferenceableOrNullBytes() const {
1209 return SetNode ? getNode()->getDereferenceableOrNullBytes() : 0;
1210}
1211
1212Type *AttributeSet::getByRefType() const {
1213 return SetNode ? getNode()->getAttributeType(Kind: Attribute::ByRef) : nullptr;
1214}
1215
1216Type *AttributeSet::getByValType() const {
1217 return SetNode ? getNode()->getAttributeType(Kind: Attribute::ByVal) : nullptr;
1218}
1219
1220Type *AttributeSet::getStructRetType() const {
1221 return SetNode ? getNode()->getAttributeType(Kind: Attribute::StructRet) : nullptr;
1222}
1223
1224Type *AttributeSet::getPreallocatedType() const {
1225 return SetNode ? getNode()->getAttributeType(Kind: Attribute::Preallocated)
1226 : nullptr;
1227}
1228
1229Type *AttributeSet::getInAllocaType() const {
1230 return SetNode ? getNode()->getAttributeType(Kind: Attribute::InAlloca) : nullptr;
1231}
1232
1233Type *AttributeSet::getElementType() const {
1234 return SetNode ? getNode()->getAttributeType(Kind: Attribute::ElementType)
1235 : nullptr;
1236}
1237
1238std::optional<std::pair<unsigned, std::optional<unsigned>>>
1239AttributeSet::getAllocSizeArgs() const {
1240 if (SetNode)
1241 return getNode()->getAllocSizeArgs();
1242 return std::nullopt;
1243}
1244
1245unsigned AttributeSet::getVScaleRangeMin() const {
1246 return SetNode ? getNode()->getVScaleRangeMin() : 1;
1247}
1248
1249std::optional<unsigned> AttributeSet::getVScaleRangeMax() const {
1250 return SetNode ? getNode()->getVScaleRangeMax() : std::nullopt;
1251}
1252
1253UWTableKind AttributeSet::getUWTableKind() const {
1254 return SetNode ? getNode()->getUWTableKind() : UWTableKind::None;
1255}
1256
1257AllocFnKind AttributeSet::getAllocKind() const {
1258 return SetNode ? getNode()->getAllocKind() : AllocFnKind::Unknown;
1259}
1260
1261MemoryEffects AttributeSet::getMemoryEffects() const {
1262 return SetNode ? getNode()->getMemoryEffects() : MemoryEffects::unknown();
1263}
1264
1265CaptureInfo AttributeSet::getCaptureInfo() const {
1266 return SetNode ? getNode()->getCaptureInfo() : CaptureInfo::all();
1267}
1268
1269FPClassTest AttributeSet::getNoFPClass() const {
1270 return SetNode ? getNode()->getNoFPClass() : fcNone;
1271}
1272
1273std::string AttributeSet::getAsString(bool InAttrGrp) const {
1274 return SetNode ? getNode()->getAsString(InAttrGrp) : "";
1275}
1276
1277bool AttributeSet::hasParentContext(LLVMContext &C) const {
1278 assert(hasAttributes() && "empty AttributeSet doesn't refer to any context");
1279 FoldingSetInsertToken Token;
1280 return C.pImpl->AttrsSetNodes.lookup(Key: getNode()->getKey(), Token) == SetNode;
1281}
1282
1283AttributeSet::iterator AttributeSet::begin() const {
1284 return SetNode ? getNode()->begin() : nullptr;
1285}
1286
1287AttributeSet::iterator AttributeSet::end() const {
1288 return SetNode ? getNode()->end() : nullptr;
1289}
1290
1291#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1292LLVM_DUMP_METHOD void AttributeSet::dump() const {
1293 dbgs() << "AS =\n";
1294 dbgs() << " { ";
1295 dbgs() << getAsString(true) << " }\n";
1296}
1297#endif
1298
1299//===----------------------------------------------------------------------===//
1300// AttributeSetNode Definition
1301//===----------------------------------------------------------------------===//
1302
1303AttributeSetNode::AttributeSetNode(ArrayRef<Attribute> Attrs)
1304 : NumAttrs(Attrs.size()) {
1305 // There's memory after the node where we can store the entries in.
1306 llvm::copy(Range&: Attrs, Out: getTrailingObjects());
1307
1308 for (const auto &I : *this) {
1309 if (I.isStringAttribute())
1310 StringAttrs.insert(KV: { I.getKindAsString(), I });
1311 else
1312 AvailableAttrs.addAttribute(Kind: I.getKindAsEnum());
1313 }
1314}
1315
1316AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
1317 ArrayRef<Attribute> Attrs) {
1318 SmallVector<Attribute, 8> SortedAttrs(Attrs);
1319 llvm::sort(C&: SortedAttrs);
1320 return getSorted(C, SortedAttrs);
1321}
1322
1323/// Attribute comparator that only compares attribute keys. Enum attributes are
1324/// sorted before string attributes.
1325struct AttributeComparator {
1326 bool operator()(Attribute A0, Attribute A1) const {
1327 bool A0IsString = A0.isStringAttribute();
1328 bool A1IsString = A1.isStringAttribute();
1329 if (A0IsString) {
1330 if (A1IsString)
1331 return A0.getKindAsString() < A1.getKindAsString();
1332 else
1333 return false;
1334 }
1335 if (A1IsString)
1336 return true;
1337 return A0.getKindAsEnum() < A1.getKindAsEnum();
1338 }
1339 bool operator()(Attribute A0, Attribute::AttrKind Kind) const {
1340 if (A0.isStringAttribute())
1341 return false;
1342 return A0.getKindAsEnum() < Kind;
1343 }
1344 bool operator()(Attribute A0, StringRef Kind) const {
1345 if (A0.isStringAttribute())
1346 return A0.getKindAsString() < Kind;
1347 return true;
1348 }
1349};
1350
1351#ifndef NDEBUG
1352/// Return true if \p Attrs is known sorted with no duplicate entries.
1353static bool isSortedAndUnique(ArrayRef<Attribute> Attrs) {
1354 return adjacent_find(Attrs, [](Attribute A0, Attribute A1) {
1355 return !AttributeComparator()(A0, A1);
1356 }) == Attrs.end();
1357}
1358#endif
1359
1360AttributeSetNode *AttributeSetNode::getSorted(LLVMContext &C,
1361 ArrayRef<Attribute> SortedAttrs) {
1362 if (SortedAttrs.empty())
1363 return nullptr;
1364
1365 assert(isSortedAndUnique(SortedAttrs) &&
1366 "expected sorted and unique attributes!");
1367
1368 FoldingSetInsertToken Token;
1369 AttributeSetNode *PA = C.pImpl->AttrsSetNodes.lookup(Key: SortedAttrs, Token);
1370
1371 // If we didn't find any existing attributes of the same shape then create a
1372 // new one and insert it.
1373 if (!PA) {
1374 // Coallocate entries after the AttributeSetNode itself.
1375 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(Counts: SortedAttrs.size()));
1376 PA = new (Mem) AttributeSetNode(SortedAttrs);
1377 C.pImpl->AttrsSetNodes.insert(N: PA, Token);
1378 }
1379
1380 // Return the AttributeSetNode that we found or created.
1381 return PA;
1382}
1383
1384AttributeSetNode *AttributeSetNode::get(LLVMContext &C, const AttrBuilder &B) {
1385 return getSorted(C, SortedAttrs: B.attrs());
1386}
1387
1388bool AttributeSetNode::hasAttribute(StringRef Kind) const {
1389 return StringAttrs.count(Val: Kind);
1390}
1391
1392std::optional<Attribute>
1393AttributeSetNode::findEnumAttribute(Attribute::AttrKind Kind) const {
1394 // Do a quick presence check.
1395 if (!hasAttribute(Kind))
1396 return std::nullopt;
1397
1398 // Attributes in a set are sorted by enum value, followed by string
1399 // attributes. Binary search the one we want.
1400 const Attribute *I =
1401 std::lower_bound(first: begin(), last: end() - StringAttrs.size(), val: Kind,
1402 comp: [](Attribute A, Attribute::AttrKind Kind) {
1403 return A.getKindAsEnum() < Kind;
1404 });
1405 assert(I != end() && I->hasAttribute(Kind) && "Presence check failed?");
1406 return *I;
1407}
1408
1409Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
1410 if (auto A = findEnumAttribute(Kind))
1411 return *A;
1412 return {};
1413}
1414
1415Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
1416 return StringAttrs.lookup(Val: Kind);
1417}
1418
1419MaybeAlign AttributeSetNode::getAlignment() const {
1420 if (auto A = findEnumAttribute(Kind: Attribute::Alignment))
1421 return A->getAlignment();
1422 return std::nullopt;
1423}
1424
1425MaybeAlign AttributeSetNode::getStackAlignment() const {
1426 if (auto A = findEnumAttribute(Kind: Attribute::StackAlignment))
1427 return A->getStackAlignment();
1428 return std::nullopt;
1429}
1430
1431Type *AttributeSetNode::getAttributeType(Attribute::AttrKind Kind) const {
1432 if (auto A = findEnumAttribute(Kind))
1433 return A->getValueAsType();
1434 return nullptr;
1435}
1436
1437uint64_t AttributeSetNode::getDereferenceableBytes() const {
1438 if (auto A = findEnumAttribute(Kind: Attribute::Dereferenceable))
1439 return A->getDereferenceableBytes();
1440 return 0;
1441}
1442
1443DeadOnReturnInfo AttributeSetNode::getDeadOnReturnInfo() const {
1444 if (auto A = findEnumAttribute(Kind: Attribute::DeadOnReturn))
1445 return A->getDeadOnReturnInfo();
1446 return 0;
1447}
1448
1449uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
1450 if (auto A = findEnumAttribute(Kind: Attribute::DereferenceableOrNull))
1451 return A->getDereferenceableOrNullBytes();
1452 return 0;
1453}
1454
1455std::optional<std::pair<unsigned, std::optional<unsigned>>>
1456AttributeSetNode::getAllocSizeArgs() const {
1457 if (auto A = findEnumAttribute(Kind: Attribute::AllocSize))
1458 return A->getAllocSizeArgs();
1459 return std::nullopt;
1460}
1461
1462unsigned AttributeSetNode::getVScaleRangeMin() const {
1463 if (auto A = findEnumAttribute(Kind: Attribute::VScaleRange))
1464 return A->getVScaleRangeMin();
1465 return 1;
1466}
1467
1468std::optional<unsigned> AttributeSetNode::getVScaleRangeMax() const {
1469 if (auto A = findEnumAttribute(Kind: Attribute::VScaleRange))
1470 return A->getVScaleRangeMax();
1471 return std::nullopt;
1472}
1473
1474UWTableKind AttributeSetNode::getUWTableKind() const {
1475 if (auto A = findEnumAttribute(Kind: Attribute::UWTable))
1476 return A->getUWTableKind();
1477 return UWTableKind::None;
1478}
1479
1480AllocFnKind AttributeSetNode::getAllocKind() const {
1481 if (auto A = findEnumAttribute(Kind: Attribute::AllocKind))
1482 return A->getAllocKind();
1483 return AllocFnKind::Unknown;
1484}
1485
1486MemoryEffects AttributeSetNode::getMemoryEffects() const {
1487 if (auto A = findEnumAttribute(Kind: Attribute::Memory))
1488 return A->getMemoryEffects();
1489 return MemoryEffects::unknown();
1490}
1491
1492CaptureInfo AttributeSetNode::getCaptureInfo() const {
1493 if (auto A = findEnumAttribute(Kind: Attribute::Captures))
1494 return A->getCaptureInfo();
1495 return CaptureInfo::all();
1496}
1497
1498FPClassTest AttributeSetNode::getNoFPClass() const {
1499 if (auto A = findEnumAttribute(Kind: Attribute::NoFPClass))
1500 return A->getNoFPClass();
1501 return fcNone;
1502}
1503
1504std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
1505 std::string Str;
1506 for (iterator I = begin(), E = end(); I != E; ++I) {
1507 if (I != begin())
1508 Str += ' ';
1509 Str += I->getAsString(InAttrGrp);
1510 }
1511 return Str;
1512}
1513
1514//===----------------------------------------------------------------------===//
1515// AttributeListImpl Definition
1516//===----------------------------------------------------------------------===//
1517
1518/// Map from AttributeList index to the internal array index. Adding one happens
1519/// to work, because -1 wraps around to 0.
1520static unsigned attrIdxToArrayIdx(unsigned Index) {
1521 return Index + 1;
1522}
1523
1524AttributeListImpl::AttributeListImpl(ArrayRef<AttributeSet> Sets)
1525 : NumAttrSets(Sets.size()) {
1526 assert(!Sets.empty() && "pointless AttributeListImpl");
1527
1528 // There's memory after the node where we can store the entries in.
1529 llvm::copy(Range&: Sets, Out: getTrailingObjects());
1530
1531 // Initialize AvailableFunctionAttrs and AvailableSomewhereAttrs
1532 // summary bitsets.
1533 for (const auto &I : Sets[attrIdxToArrayIdx(Index: AttributeList::FunctionIndex)])
1534 if (!I.isStringAttribute())
1535 AvailableFunctionAttrs.addAttribute(Kind: I.getKindAsEnum());
1536
1537 for (const auto &Set : Sets)
1538 for (const auto &I : Set)
1539 if (!I.isStringAttribute())
1540 AvailableSomewhereAttrs.addAttribute(Kind: I.getKindAsEnum());
1541}
1542
1543bool AttributeListImpl::hasAttrSomewhere(Attribute::AttrKind Kind,
1544 unsigned *Index) const {
1545 if (!AvailableSomewhereAttrs.hasAttribute(Kind))
1546 return false;
1547
1548 if (Index) {
1549 for (unsigned I = 0, E = NumAttrSets; I != E; ++I) {
1550 if (begin()[I].hasAttribute(Kind)) {
1551 *Index = I - 1;
1552 break;
1553 }
1554 }
1555 }
1556
1557 return true;
1558}
1559
1560
1561#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1562LLVM_DUMP_METHOD void AttributeListImpl::dump() const {
1563 AttributeList(const_cast<AttributeListImpl *>(this)).dump();
1564}
1565#endif
1566
1567//===----------------------------------------------------------------------===//
1568// AttributeList Construction and Mutation Methods
1569//===----------------------------------------------------------------------===//
1570
1571AttributeList::AttributeList(AttributeListImpl *LI) : pImpl(LI) {}
1572
1573AttributeListImpl *AttributeList::getListImpl() const {
1574 return static_cast<AttributeListImpl *>(pImpl);
1575}
1576
1577AttributeList AttributeList::getImpl(LLVMContext &C,
1578 ArrayRef<AttributeSet> AttrSets) {
1579 assert(!AttrSets.empty() && "pointless AttributeListImpl");
1580
1581 LLVMContextImpl *pImpl = C.pImpl;
1582 FoldingSetInsertToken Token;
1583 AttributeListImpl *PA = pImpl->AttrsLists.lookup(Key: AttrSets, Token);
1584
1585 // If we didn't find any existing attributes of the same shape then
1586 // create a new one and insert it.
1587 if (!PA) {
1588 // Coallocate entries after the AttributeListImpl itself.
1589 void *Mem = pImpl->Alloc.Allocate(
1590 Size: AttributeListImpl::totalSizeToAlloc<AttributeSet>(Counts: AttrSets.size()),
1591 Alignment: alignof(AttributeListImpl));
1592 PA = new (Mem) AttributeListImpl(AttrSets);
1593 pImpl->AttrsLists.insert(N: PA, Token);
1594 }
1595
1596 // Return the AttributesList that we found or created.
1597 return AttributeList(PA);
1598}
1599
1600AttributeList
1601AttributeList::get(LLVMContext &C,
1602 ArrayRef<std::pair<unsigned, Attribute>> Attrs) {
1603 // If there are no attributes then return a null AttributesList pointer.
1604 if (Attrs.empty())
1605 return {};
1606
1607 assert(llvm::is_sorted(Attrs, llvm::less_first()) &&
1608 "Misordered Attributes list!");
1609 assert(llvm::all_of(Attrs,
1610 [](const std::pair<unsigned, Attribute> &Pair) {
1611 return Pair.second.isValid();
1612 }) &&
1613 "Pointless attribute!");
1614
1615 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
1616 // list.
1617 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairVec;
1618 for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(),
1619 E = Attrs.end(); I != E; ) {
1620 unsigned Index = I->first;
1621 SmallVector<Attribute, 4> AttrVec;
1622 while (I != E && I->first == Index) {
1623 AttrVec.push_back(Elt: I->second);
1624 ++I;
1625 }
1626
1627 AttrPairVec.emplace_back(Args&: Index, Args: AttributeSet::get(C, Attrs: AttrVec));
1628 }
1629
1630 return get(C, Attrs: AttrPairVec);
1631}
1632
1633AttributeList
1634AttributeList::get(LLVMContext &C,
1635 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) {
1636 // If there are no attributes then return a null AttributesList pointer.
1637 if (Attrs.empty())
1638 return {};
1639
1640 assert(llvm::is_sorted(Attrs, llvm::less_first()) &&
1641 "Misordered Attributes list!");
1642 assert(llvm::none_of(Attrs,
1643 [](const std::pair<unsigned, AttributeSet> &Pair) {
1644 return !Pair.second.hasAttributes();
1645 }) &&
1646 "Pointless attribute!");
1647
1648 unsigned MaxIndex = Attrs.back().first;
1649 // If the MaxIndex is FunctionIndex and there are other indices in front
1650 // of it, we need to use the largest of those to get the right size.
1651 if (MaxIndex == FunctionIndex && Attrs.size() > 1)
1652 MaxIndex = Attrs[Attrs.size() - 2].first;
1653
1654 SmallVector<AttributeSet, 4> AttrVec(attrIdxToArrayIdx(Index: MaxIndex) + 1);
1655 for (const auto &Pair : Attrs)
1656 AttrVec[attrIdxToArrayIdx(Index: Pair.first)] = Pair.second;
1657
1658 return getImpl(C, AttrSets: AttrVec);
1659}
1660
1661AttributeList AttributeList::get(LLVMContext &C, AttributeSet FnAttrs,
1662 AttributeSet RetAttrs,
1663 ArrayRef<AttributeSet> ArgAttrs) {
1664 // Scan from the end to find the last argument with attributes. Most
1665 // arguments don't have attributes, so it's nice if we can have fewer unique
1666 // AttributeListImpls by dropping empty attribute sets at the end of the list.
1667 unsigned NumSets = 0;
1668 for (size_t I = ArgAttrs.size(); I != 0; --I) {
1669 if (ArgAttrs[I - 1].hasAttributes()) {
1670 NumSets = I + 2;
1671 break;
1672 }
1673 }
1674 if (NumSets == 0) {
1675 // Check function and return attributes if we didn't have argument
1676 // attributes.
1677 if (RetAttrs.hasAttributes())
1678 NumSets = 2;
1679 else if (FnAttrs.hasAttributes())
1680 NumSets = 1;
1681 }
1682
1683 // If all attribute sets were empty, we can use the empty attribute list.
1684 if (NumSets == 0)
1685 return {};
1686
1687 SmallVector<AttributeSet, 8> AttrSets;
1688 AttrSets.reserve(N: NumSets);
1689 // If we have any attributes, we always have function attributes.
1690 AttrSets.push_back(Elt: FnAttrs);
1691 if (NumSets > 1)
1692 AttrSets.push_back(Elt: RetAttrs);
1693 if (NumSets > 2) {
1694 // Drop the empty argument attribute sets at the end.
1695 ArgAttrs = ArgAttrs.take_front(N: NumSets - 2);
1696 llvm::append_range(C&: AttrSets, R&: ArgAttrs);
1697 }
1698
1699 return getImpl(C, AttrSets);
1700}
1701
1702AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
1703 AttributeSet Attrs) {
1704 if (!Attrs.hasAttributes())
1705 return {};
1706 Index = attrIdxToArrayIdx(Index);
1707 SmallVector<AttributeSet, 8> AttrSets(Index + 1);
1708 AttrSets[Index] = Attrs;
1709 return getImpl(C, AttrSets);
1710}
1711
1712AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
1713 const AttrBuilder &B) {
1714 return get(C, Index, Attrs: AttributeSet::get(C, B));
1715}
1716
1717AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
1718 ArrayRef<Attribute::AttrKind> Kinds) {
1719 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
1720 for (const auto K : Kinds)
1721 Attrs.emplace_back(Args&: Index, Args: Attribute::get(Context&: C, Kind: K));
1722 return get(C, Attrs);
1723}
1724
1725AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
1726 ArrayRef<Attribute::AttrKind> Kinds,
1727 ArrayRef<uint64_t> Values) {
1728 assert(Kinds.size() == Values.size() && "Mismatched attribute values.");
1729 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
1730 auto VI = Values.begin();
1731 for (const auto K : Kinds)
1732 Attrs.emplace_back(Args&: Index, Args: Attribute::get(Context&: C, Kind: K, Val: *VI++));
1733 return get(C, Attrs);
1734}
1735
1736AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
1737 ArrayRef<StringRef> Kinds) {
1738 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
1739 for (const auto &K : Kinds)
1740 Attrs.emplace_back(Args&: Index, Args: Attribute::get(Context&: C, Kind: K));
1741 return get(C, Attrs);
1742}
1743
1744AttributeList AttributeList::get(LLVMContext &C,
1745 ArrayRef<AttributeList> Attrs) {
1746 if (Attrs.empty())
1747 return {};
1748 if (Attrs.size() == 1)
1749 return Attrs[0];
1750
1751 unsigned MaxSize = 0;
1752 for (const auto &List : Attrs)
1753 MaxSize = std::max(a: MaxSize, b: List.getNumAttrSets());
1754
1755 // If every list was empty, there is no point in merging the lists.
1756 if (MaxSize == 0)
1757 return {};
1758
1759 SmallVector<AttributeSet, 8> NewAttrSets(MaxSize);
1760 for (unsigned I = 0; I < MaxSize; ++I) {
1761 AttrBuilder CurBuilder(C);
1762 for (const auto &List : Attrs)
1763 CurBuilder.merge(B: AttrBuilder(C, List.getAttributes(Index: I - 1)));
1764 NewAttrSets[I] = AttributeSet::get(C, B: CurBuilder);
1765 }
1766
1767 return getImpl(C, AttrSets: NewAttrSets);
1768}
1769
1770AttributeList
1771AttributeList::addAttributeAtIndex(LLVMContext &C, unsigned Index,
1772 Attribute::AttrKind Kind) const {
1773 AttributeSet Attrs = getAttributes(Index);
1774 if (Attrs.hasAttribute(Kind))
1775 return *this;
1776 // TODO: Insert at correct position and avoid sort.
1777 SmallVector<Attribute, 8> NewAttrs(Attrs.begin(), Attrs.end());
1778 NewAttrs.push_back(Elt: Attribute::get(Context&: C, Kind));
1779 return setAttributesAtIndex(C, Index, Attrs: AttributeSet::get(C, Attrs: NewAttrs));
1780}
1781
1782AttributeList AttributeList::addAttributeAtIndex(LLVMContext &C, unsigned Index,
1783 StringRef Kind,
1784 StringRef Value) const {
1785 AttrBuilder B(C);
1786 B.addAttribute(A: Kind, V: Value);
1787 return addAttributesAtIndex(C, Index, B);
1788}
1789
1790AttributeList AttributeList::addAttributeAtIndex(LLVMContext &C, unsigned Index,
1791 Attribute A) const {
1792 AttrBuilder B(C);
1793 B.addAttribute(A);
1794 return addAttributesAtIndex(C, Index, B);
1795}
1796
1797AttributeList AttributeList::setAttributesAtIndex(LLVMContext &C,
1798 unsigned Index,
1799 AttributeSet Attrs) const {
1800 Index = attrIdxToArrayIdx(Index);
1801 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1802 if (Index >= AttrSets.size())
1803 AttrSets.resize(N: Index + 1);
1804 AttrSets[Index] = Attrs;
1805
1806 // Remove trailing empty attribute sets.
1807 while (!AttrSets.empty() && !AttrSets.back().hasAttributes())
1808 AttrSets.pop_back();
1809 if (AttrSets.empty())
1810 return {};
1811 return AttributeList::getImpl(C, AttrSets);
1812}
1813
1814AttributeList AttributeList::addAttributesAtIndex(LLVMContext &C,
1815 unsigned Index,
1816 const AttrBuilder &B) const {
1817 if (!B.hasAttributes())
1818 return *this;
1819
1820 if (!pImpl)
1821 return AttributeList::get(C, Attrs: {{Index, AttributeSet::get(C, B)}});
1822
1823 AttrBuilder Merged(C, getAttributes(Index));
1824 Merged.merge(B);
1825 return setAttributesAtIndex(C, Index, Attrs: AttributeSet::get(C, B: Merged));
1826}
1827
1828AttributeList
1829AttributeList::addSortedAttributesAtIndex(LLVMContext &C, unsigned Index,
1830 ArrayRef<Attribute> Attrs) const {
1831 if (Attrs.empty())
1832 return *this;
1833 return setAttributesAtIndex(
1834 C, Index, Attrs: getAttributes(Index).addSortedAttributes(C, Attrs));
1835}
1836
1837AttributeList AttributeList::addParamAttribute(LLVMContext &C,
1838 ArrayRef<unsigned> ArgNos,
1839 Attribute A) const {
1840 assert(llvm::is_sorted(ArgNos));
1841
1842 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1843 unsigned MaxIndex = attrIdxToArrayIdx(Index: ArgNos.back() + FirstArgIndex);
1844 if (MaxIndex >= AttrSets.size())
1845 AttrSets.resize(N: MaxIndex + 1);
1846
1847 for (unsigned ArgNo : ArgNos) {
1848 unsigned Index = attrIdxToArrayIdx(Index: ArgNo + FirstArgIndex);
1849 AttrBuilder B(C, AttrSets[Index]);
1850 B.addAttribute(A);
1851 AttrSets[Index] = AttributeSet::get(C, B);
1852 }
1853
1854 return getImpl(C, AttrSets);
1855}
1856
1857AttributeList
1858AttributeList::removeAttributeAtIndex(LLVMContext &C, unsigned Index,
1859 Attribute::AttrKind Kind) const {
1860 AttributeSet Attrs = getAttributes(Index);
1861 AttributeSet NewAttrs = Attrs.removeAttribute(C, Kind);
1862 if (Attrs == NewAttrs)
1863 return *this;
1864 return setAttributesAtIndex(C, Index, Attrs: NewAttrs);
1865}
1866
1867AttributeList AttributeList::removeAttributeAtIndex(LLVMContext &C,
1868 unsigned Index,
1869 StringRef Kind) const {
1870 AttributeSet Attrs = getAttributes(Index);
1871 AttributeSet NewAttrs = Attrs.removeAttribute(C, Kind);
1872 if (Attrs == NewAttrs)
1873 return *this;
1874 return setAttributesAtIndex(C, Index, Attrs: NewAttrs);
1875}
1876
1877AttributeList AttributeList::removeAttributesAtIndex(
1878 LLVMContext &C, unsigned Index, const AttributeMask &AttrsToRemove) const {
1879 AttributeSet Attrs = getAttributes(Index);
1880 AttributeSet NewAttrs = Attrs.removeAttributes(C, Attrs: AttrsToRemove);
1881 // If nothing was removed, return the original list.
1882 if (Attrs == NewAttrs)
1883 return *this;
1884 return setAttributesAtIndex(C, Index, Attrs: NewAttrs);
1885}
1886
1887AttributeList
1888AttributeList::removeAttributesAtIndex(LLVMContext &C,
1889 unsigned WithoutIndex) const {
1890 if (!pImpl)
1891 return {};
1892 if (attrIdxToArrayIdx(Index: WithoutIndex) >= getNumAttrSets())
1893 return *this;
1894 return setAttributesAtIndex(C, Index: WithoutIndex, Attrs: AttributeSet());
1895}
1896
1897AttributeList AttributeList::addDereferenceableRetAttr(LLVMContext &C,
1898 uint64_t Bytes) const {
1899 AttrBuilder B(C);
1900 B.addDereferenceableAttr(Bytes);
1901 return addRetAttributes(C, B);
1902}
1903
1904AttributeList AttributeList::addDereferenceableParamAttr(LLVMContext &C,
1905 unsigned Index,
1906 uint64_t Bytes) const {
1907 AttrBuilder B(C);
1908 B.addDereferenceableAttr(Bytes);
1909 return addParamAttributes(C, ArgNo: Index, B);
1910}
1911
1912AttributeList
1913AttributeList::addDereferenceableOrNullParamAttr(LLVMContext &C, unsigned Index,
1914 uint64_t Bytes) const {
1915 AttrBuilder B(C);
1916 B.addDereferenceableOrNullAttr(Bytes);
1917 return addParamAttributes(C, ArgNo: Index, B);
1918}
1919
1920AttributeList AttributeList::addRangeRetAttr(LLVMContext &C,
1921 const ConstantRange &CR) const {
1922 AttrBuilder B(C);
1923 B.addRangeAttr(CR);
1924 return addRetAttributes(C, B);
1925}
1926
1927AttributeList AttributeList::addAllocSizeParamAttr(
1928 LLVMContext &C, unsigned Index, unsigned ElemSizeArg,
1929 const std::optional<unsigned> &NumElemsArg) const {
1930 AttrBuilder B(C);
1931 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
1932 return addParamAttributes(C, ArgNo: Index, B);
1933}
1934
1935std::optional<AttributeList>
1936AttributeList::intersectWith(LLVMContext &C, AttributeList Other) const {
1937 // Trivial case, the two lists are equal.
1938 if (*this == Other)
1939 return *this;
1940
1941 SmallVector<std::pair<unsigned, AttributeSet>> IntersectedAttrs;
1942 auto IndexIt =
1943 index_iterator(std::max(a: getNumAttrSets(), b: Other.getNumAttrSets()));
1944 for (unsigned Idx : IndexIt) {
1945 auto IntersectedAS =
1946 getAttributes(Index: Idx).intersectWith(C, Other: Other.getAttributes(Index: Idx));
1947 // If any index fails to intersect, fail.
1948 if (!IntersectedAS)
1949 return std::nullopt;
1950 if (!IntersectedAS->hasAttributes())
1951 continue;
1952 IntersectedAttrs.push_back(Elt: std::make_pair(x&: Idx, y&: *IntersectedAS));
1953 }
1954
1955 llvm::sort(C&: IntersectedAttrs, Comp: llvm::less_first());
1956 return AttributeList::get(C, Attrs: IntersectedAttrs);
1957}
1958
1959//===----------------------------------------------------------------------===//
1960// AttributeList Accessor Methods
1961//===----------------------------------------------------------------------===//
1962
1963AttributeSet AttributeList::getParamAttrs(unsigned ArgNo) const {
1964 return getAttributes(Index: ArgNo + FirstArgIndex);
1965}
1966
1967AttributeSet AttributeList::getRetAttrs() const {
1968 return getAttributes(Index: ReturnIndex);
1969}
1970
1971AttributeSet AttributeList::getFnAttrs() const {
1972 return getAttributes(Index: FunctionIndex);
1973}
1974
1975bool AttributeList::hasAttributeAtIndex(unsigned Index,
1976 Attribute::AttrKind Kind) const {
1977 return getAttributes(Index).hasAttribute(Kind);
1978}
1979
1980bool AttributeList::hasAttributeAtIndex(unsigned Index, StringRef Kind) const {
1981 return getAttributes(Index).hasAttribute(Kind);
1982}
1983
1984bool AttributeList::hasAttributesAtIndex(unsigned Index) const {
1985 return getAttributes(Index).hasAttributes();
1986}
1987
1988
1989bool AttributeList::hasFnAttr(StringRef Kind) const {
1990 return hasAttributeAtIndex(Index: AttributeList::FunctionIndex, Kind);
1991}
1992
1993bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr,
1994 unsigned *Index) const {
1995 return pImpl && getListImpl()->hasAttrSomewhere(Kind: Attr, Index);
1996}
1997
1998Attribute AttributeList::getAttributeAtIndex(unsigned Index,
1999 Attribute::AttrKind Kind) const {
2000 return getAttributes(Index).getAttribute(Kind);
2001}
2002
2003Attribute AttributeList::getAttributeAtIndex(unsigned Index,
2004 StringRef Kind) const {
2005 return getAttributes(Index).getAttribute(Kind);
2006}
2007
2008MaybeAlign AttributeList::getRetAlignment() const {
2009 return getAttributes(Index: ReturnIndex).getAlignment();
2010}
2011
2012MaybeAlign AttributeList::getParamAlignment(unsigned ArgNo) const {
2013 return getAttributes(Index: ArgNo + FirstArgIndex).getAlignment();
2014}
2015
2016MaybeAlign AttributeList::getParamStackAlignment(unsigned ArgNo) const {
2017 return getAttributes(Index: ArgNo + FirstArgIndex).getStackAlignment();
2018}
2019
2020Type *AttributeList::getParamByValType(unsigned Index) const {
2021 return getAttributes(Index: Index+FirstArgIndex).getByValType();
2022}
2023
2024Type *AttributeList::getParamStructRetType(unsigned Index) const {
2025 return getAttributes(Index: Index + FirstArgIndex).getStructRetType();
2026}
2027
2028Type *AttributeList::getParamByRefType(unsigned Index) const {
2029 return getAttributes(Index: Index + FirstArgIndex).getByRefType();
2030}
2031
2032Type *AttributeList::getParamPreallocatedType(unsigned Index) const {
2033 return getAttributes(Index: Index + FirstArgIndex).getPreallocatedType();
2034}
2035
2036Type *AttributeList::getParamInAllocaType(unsigned Index) const {
2037 return getAttributes(Index: Index + FirstArgIndex).getInAllocaType();
2038}
2039
2040Type *AttributeList::getParamElementType(unsigned Index) const {
2041 return getAttributes(Index: Index + FirstArgIndex).getElementType();
2042}
2043
2044MaybeAlign AttributeList::getFnStackAlignment() const {
2045 return getFnAttrs().getStackAlignment();
2046}
2047
2048MaybeAlign AttributeList::getRetStackAlignment() const {
2049 return getRetAttrs().getStackAlignment();
2050}
2051
2052uint64_t AttributeList::getRetDereferenceableBytes() const {
2053 return getRetAttrs().getDereferenceableBytes();
2054}
2055
2056uint64_t AttributeList::getParamDereferenceableBytes(unsigned Index) const {
2057 return getParamAttrs(ArgNo: Index).getDereferenceableBytes();
2058}
2059
2060uint64_t AttributeList::getRetDereferenceableOrNullBytes() const {
2061 return getRetAttrs().getDereferenceableOrNullBytes();
2062}
2063
2064DeadOnReturnInfo AttributeList::getDeadOnReturnInfo(unsigned Index) const {
2065 return getParamAttrs(ArgNo: Index).getDeadOnReturnInfo();
2066}
2067
2068uint64_t
2069AttributeList::getParamDereferenceableOrNullBytes(unsigned Index) const {
2070 return getParamAttrs(ArgNo: Index).getDereferenceableOrNullBytes();
2071}
2072
2073std::optional<ConstantRange>
2074AttributeList::getParamRange(unsigned ArgNo) const {
2075 auto RangeAttr = getParamAttrs(ArgNo).getAttribute(Kind: Attribute::Range);
2076 if (RangeAttr.isValid())
2077 return RangeAttr.getRange();
2078 return std::nullopt;
2079}
2080
2081FPClassTest AttributeList::getRetNoFPClass() const {
2082 return getRetAttrs().getNoFPClass();
2083}
2084
2085FPClassTest AttributeList::getParamNoFPClass(unsigned Index) const {
2086 return getParamAttrs(ArgNo: Index).getNoFPClass();
2087}
2088
2089UWTableKind AttributeList::getUWTableKind() const {
2090 return getFnAttrs().getUWTableKind();
2091}
2092
2093AllocFnKind AttributeList::getAllocKind() const {
2094 return getFnAttrs().getAllocKind();
2095}
2096
2097MemoryEffects AttributeList::getMemoryEffects() const {
2098 return getFnAttrs().getMemoryEffects();
2099}
2100
2101std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const {
2102 return getAttributes(Index).getAsString(InAttrGrp);
2103}
2104
2105AttributeSet AttributeList::getAttributes(unsigned Index) const {
2106 Index = attrIdxToArrayIdx(Index);
2107 if (!pImpl || Index >= getNumAttrSets())
2108 return {};
2109 return getListImpl()->begin()[Index];
2110}
2111
2112bool AttributeList::hasParentContext(LLVMContext &C) const {
2113 assert(!isEmpty() && "an empty attribute list has no parent context");
2114 FoldingSetInsertToken Token;
2115 return C.pImpl->AttrsLists.lookup(Key: getListImpl()->getKey(), Token) == pImpl;
2116}
2117
2118AttributeList::iterator AttributeList::begin() const {
2119 return pImpl ? getListImpl()->begin() : nullptr;
2120}
2121
2122AttributeList::iterator AttributeList::end() const {
2123 return pImpl ? getListImpl()->end() : nullptr;
2124}
2125
2126//===----------------------------------------------------------------------===//
2127// AttributeList Introspection Methods
2128//===----------------------------------------------------------------------===//
2129
2130unsigned AttributeList::getNumAttrSets() const {
2131 return pImpl ? getListImpl()->NumAttrSets : 0;
2132}
2133
2134void AttributeList::print(raw_ostream &O) const {
2135 O << "AttributeList[\n";
2136
2137 for (unsigned i : indexes()) {
2138 if (!getAttributes(Index: i).hasAttributes())
2139 continue;
2140 O << " { ";
2141 switch (i) {
2142 case AttrIndex::ReturnIndex:
2143 O << "return";
2144 break;
2145 case AttrIndex::FunctionIndex:
2146 O << "function";
2147 break;
2148 default:
2149 O << "arg(" << i - AttrIndex::FirstArgIndex << ")";
2150 }
2151 O << " => " << getAsString(Index: i) << " }\n";
2152 }
2153
2154 O << "]\n";
2155}
2156
2157#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2158LLVM_DUMP_METHOD void AttributeList::dump() const { print(dbgs()); }
2159#endif
2160
2161//===----------------------------------------------------------------------===//
2162// AttrBuilder Method Implementations
2163//===----------------------------------------------------------------------===//
2164
2165AttrBuilder::AttrBuilder(LLVMContext &Ctx, AttributeSet AS) : Ctx(Ctx) {
2166 append_range(C&: Attrs, R&: AS);
2167 assert(is_sorted(Attrs) && "AttributeSet should be sorted");
2168}
2169
2170void AttrBuilder::clear() { Attrs.clear(); }
2171
2172template <typename K>
2173static void addAttributeImpl(SmallVectorImpl<Attribute> &Attrs, K Kind,
2174 Attribute Attr) {
2175 auto It = lower_bound(Attrs, Kind, AttributeComparator());
2176 if (It != Attrs.end() && It->hasAttribute(Kind))
2177 std::swap(*It, Attr);
2178 else
2179 Attrs.insert(It, Attr);
2180}
2181
2182AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
2183 if (Attr.isStringAttribute())
2184 addAttributeImpl(Attrs, Kind: Attr.getKindAsString(), Attr);
2185 else
2186 addAttributeImpl(Attrs, Kind: Attr.getKindAsEnum(), Attr);
2187 return *this;
2188}
2189
2190AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Kind) {
2191 addAttributeImpl(Attrs, Kind, Attr: Attribute::get(Context&: Ctx, Kind));
2192 return *this;
2193}
2194
2195AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
2196 addAttributeImpl(Attrs, Kind: A, Attr: Attribute::get(Context&: Ctx, Kind: A, Val: V));
2197 return *this;
2198}
2199
2200AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
2201 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
2202 auto It = lower_bound(Range&: Attrs, Value&: Val, C: AttributeComparator());
2203 if (It != Attrs.end() && It->hasAttribute(Kind: Val))
2204 Attrs.erase(CI: It);
2205 return *this;
2206}
2207
2208AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
2209 auto It = lower_bound(Range&: Attrs, Value&: A, C: AttributeComparator());
2210 if (It != Attrs.end() && It->hasAttribute(Kind: A))
2211 Attrs.erase(CI: It);
2212 return *this;
2213}
2214
2215std::optional<uint64_t>
2216AttrBuilder::getRawIntAttr(Attribute::AttrKind Kind) const {
2217 assert(Attribute::isIntAttrKind(Kind) && "Not an int attribute");
2218 Attribute A = getAttribute(Kind);
2219 if (A.isValid())
2220 return A.getValueAsInt();
2221 return std::nullopt;
2222}
2223
2224AttrBuilder &AttrBuilder::addRawIntAttr(Attribute::AttrKind Kind,
2225 uint64_t Value) {
2226 return addAttribute(Attr: Attribute::get(Context&: Ctx, Kind, Val: Value));
2227}
2228
2229std::optional<std::pair<unsigned, std::optional<unsigned>>>
2230AttrBuilder::getAllocSizeArgs() const {
2231 Attribute A = getAttribute(Kind: Attribute::AllocSize);
2232 if (A.isValid())
2233 return A.getAllocSizeArgs();
2234 return std::nullopt;
2235}
2236
2237AttrBuilder &AttrBuilder::addAlignmentAttr(MaybeAlign Align) {
2238 if (!Align)
2239 return *this;
2240
2241 assert(*Align <= llvm::Value::MaximumAlignment && "Alignment too large.");
2242 return addRawIntAttr(Kind: Attribute::Alignment, Value: Align->value());
2243}
2244
2245AttrBuilder &AttrBuilder::addStackAlignmentAttr(MaybeAlign Align) {
2246 // Default alignment, allow the target to define how to align it.
2247 if (!Align)
2248 return *this;
2249
2250 assert(*Align <= 0x100 && "Alignment too large.");
2251 return addRawIntAttr(Kind: Attribute::StackAlignment, Value: Align->value());
2252}
2253
2254AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
2255 if (Bytes == 0) return *this;
2256
2257 return addRawIntAttr(Kind: Attribute::Dereferenceable, Value: Bytes);
2258}
2259
2260AttrBuilder &AttrBuilder::addDeadOnReturnAttr(DeadOnReturnInfo Info) {
2261 if (Info.isZeroSized())
2262 return *this;
2263
2264 return addRawIntAttr(Kind: Attribute::DeadOnReturn, Value: Info.toIntValue());
2265}
2266
2267AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
2268 if (Bytes == 0)
2269 return *this;
2270
2271 return addRawIntAttr(Kind: Attribute::DereferenceableOrNull, Value: Bytes);
2272}
2273
2274AttrBuilder &
2275AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
2276 const std::optional<unsigned> &NumElems) {
2277 return addAllocSizeAttrFromRawRepr(RawAllocSizeRepr: packAllocSizeArgs(ElemSizeArg: ElemSize, NumElemsArg: NumElems));
2278}
2279
2280AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
2281 // (0, 0) is our "not present" value, so we need to check for it here.
2282 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
2283 return addRawIntAttr(Kind: Attribute::AllocSize, Value: RawArgs);
2284}
2285
2286AttrBuilder &AttrBuilder::addVScaleRangeAttr(unsigned MinValue,
2287 std::optional<unsigned> MaxValue) {
2288 return addVScaleRangeAttrFromRawRepr(RawVScaleRangeRepr: packVScaleRangeArgs(MinValue, MaxValue));
2289}
2290
2291AttrBuilder &AttrBuilder::addVScaleRangeAttrFromRawRepr(uint64_t RawArgs) {
2292 // (0, 0) is not present hence ignore this case
2293 if (RawArgs == 0)
2294 return *this;
2295
2296 return addRawIntAttr(Kind: Attribute::VScaleRange, Value: RawArgs);
2297}
2298
2299AttrBuilder &AttrBuilder::addUWTableAttr(UWTableKind Kind) {
2300 if (Kind == UWTableKind::None)
2301 return *this;
2302 return addRawIntAttr(Kind: Attribute::UWTable, Value: uint64_t(Kind));
2303}
2304
2305AttrBuilder &AttrBuilder::addMemoryAttr(MemoryEffects ME) {
2306 return addRawIntAttr(Kind: Attribute::Memory, Value: ME.toIntValue());
2307}
2308
2309AttrBuilder &AttrBuilder::addCapturesAttr(CaptureInfo CI) {
2310 return addRawIntAttr(Kind: Attribute::Captures, Value: CI.toIntValue());
2311}
2312
2313AttrBuilder &AttrBuilder::addDenormalFPEnvAttr(DenormalFPEnv FPEnv) {
2314 return addRawIntAttr(Kind: Attribute::DenormalFPEnv, Value: FPEnv.toIntValue());
2315}
2316
2317AttrBuilder &AttrBuilder::addNoFPClassAttr(FPClassTest Mask) {
2318 if (Mask == fcNone)
2319 return *this;
2320
2321 return addRawIntAttr(Kind: Attribute::NoFPClass, Value: Mask);
2322}
2323
2324AttrBuilder &AttrBuilder::addAllocKindAttr(AllocFnKind Kind) {
2325 return addRawIntAttr(Kind: Attribute::AllocKind, Value: static_cast<uint64_t>(Kind));
2326}
2327
2328Type *AttrBuilder::getTypeAttr(Attribute::AttrKind Kind) const {
2329 assert(Attribute::isTypeAttrKind(Kind) && "Not a type attribute");
2330 Attribute A = getAttribute(Kind);
2331 return A.isValid() ? A.getValueAsType() : nullptr;
2332}
2333
2334AttrBuilder &AttrBuilder::addTypeAttr(Attribute::AttrKind Kind, Type *Ty) {
2335 return addAttribute(Attr: Attribute::get(Context&: Ctx, Kind, Ty));
2336}
2337
2338AttrBuilder &AttrBuilder::addByValAttr(Type *Ty) {
2339 return addTypeAttr(Kind: Attribute::ByVal, Ty);
2340}
2341
2342AttrBuilder &AttrBuilder::addStructRetAttr(Type *Ty) {
2343 return addTypeAttr(Kind: Attribute::StructRet, Ty);
2344}
2345
2346AttrBuilder &AttrBuilder::addByRefAttr(Type *Ty) {
2347 return addTypeAttr(Kind: Attribute::ByRef, Ty);
2348}
2349
2350AttrBuilder &AttrBuilder::addPreallocatedAttr(Type *Ty) {
2351 return addTypeAttr(Kind: Attribute::Preallocated, Ty);
2352}
2353
2354AttrBuilder &AttrBuilder::addInAllocaAttr(Type *Ty) {
2355 return addTypeAttr(Kind: Attribute::InAlloca, Ty);
2356}
2357
2358AttrBuilder &AttrBuilder::addConstantRangeAttr(Attribute::AttrKind Kind,
2359 const ConstantRange &CR) {
2360 if (CR.isFullSet())
2361 return *this;
2362
2363 return addAttribute(Attr: Attribute::get(Context&: Ctx, Kind, CR));
2364}
2365
2366AttrBuilder &AttrBuilder::addRangeAttr(const ConstantRange &CR) {
2367 return addConstantRangeAttr(Kind: Attribute::Range, CR);
2368}
2369
2370AttrBuilder &
2371AttrBuilder::addConstantRangeListAttr(Attribute::AttrKind Kind,
2372 ArrayRef<ConstantRange> Val) {
2373 return addAttribute(Attr: Attribute::get(Context&: Ctx, Kind, Val));
2374}
2375
2376AttrBuilder &AttrBuilder::addInitializesAttr(const ConstantRangeList &CRL) {
2377 return addConstantRangeListAttr(Kind: Attribute::Initializes, Val: CRL.rangesRef());
2378}
2379
2380AttrBuilder &AttrBuilder::addFromEquivalentMetadata(const Instruction &I) {
2381 if (I.hasMetadata(KindID: LLVMContext::MD_nonnull))
2382 addAttribute(Kind: Attribute::NonNull);
2383
2384 if (I.hasMetadata(KindID: LLVMContext::MD_noundef))
2385 addAttribute(Kind: Attribute::NoUndef);
2386
2387 if (const MDNode *Align = I.getMetadata(KindID: LLVMContext::MD_align)) {
2388 ConstantInt *CI = mdconst::extract<ConstantInt>(MD: Align->getOperand(I: 0));
2389 addAlignmentAttr(Align: CI->getZExtValue());
2390 }
2391
2392 if (const MDNode *Dereferenceable =
2393 I.getMetadata(KindID: LLVMContext::MD_dereferenceable)) {
2394 ConstantInt *CI =
2395 mdconst::extract<ConstantInt>(MD: Dereferenceable->getOperand(I: 0));
2396 addDereferenceableAttr(Bytes: CI->getZExtValue());
2397 }
2398
2399 if (const MDNode *DereferenceableOrNull =
2400 I.getMetadata(KindID: LLVMContext::MD_dereferenceable_or_null)) {
2401 ConstantInt *CI =
2402 mdconst::extract<ConstantInt>(MD: DereferenceableOrNull->getOperand(I: 0));
2403 addDereferenceableAttr(Bytes: CI->getZExtValue());
2404 }
2405
2406 if (const MDNode *Range = I.getMetadata(KindID: LLVMContext::MD_range))
2407 addRangeAttr(CR: getConstantRangeFromMetadata(RangeMD: *Range));
2408
2409 if (const MDNode *NoFPClass = I.getMetadata(KindID: LLVMContext::MD_nofpclass)) {
2410 ConstantInt *CI = mdconst::extract<ConstantInt>(MD: NoFPClass->getOperand(I: 0));
2411 addNoFPClassAttr(Mask: static_cast<FPClassTest>(CI->getZExtValue()));
2412 }
2413
2414 return *this;
2415}
2416
2417/// Merge \p B into \p A, both sorted by kind, into \p Merged. An attribute
2418/// in \p B replaces the attribute of the same kind in \p A.
2419static void mergeSortedAttrs(ArrayRef<Attribute> A, ArrayRef<Attribute> B,
2420 SmallVectorImpl<Attribute> &Merged) {
2421 Merged.reserve(N: A.size() + B.size());
2422 AttributeComparator Less;
2423 auto *I = A.begin(), *E = A.end();
2424
2425 for (Attribute BAttr : B) {
2426 while (I != E && Less(*I, BAttr))
2427 Merged.push_back(Elt: *I++);
2428
2429 if (I != E && !Less(BAttr, *I))
2430 ++I;
2431
2432 Merged.push_back(Elt: BAttr);
2433 }
2434
2435 Merged.append(in_start: I, in_end: E);
2436}
2437
2438AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
2439 if (B.Attrs.empty())
2440 return *this;
2441
2442 if (Attrs.empty()) {
2443 Attrs = B.Attrs;
2444 return *this;
2445 }
2446
2447 // Both lists are already sorted by kind.
2448 SmallVector<Attribute, 8> Merged;
2449 mergeSortedAttrs(A: Attrs, B: B.Attrs, Merged);
2450 Attrs = std::move(Merged);
2451 return *this;
2452}
2453
2454AttributeSet
2455AttributeSet::addSortedAttributes(LLVMContext &C,
2456 ArrayRef<Attribute> Attrs) const {
2457 if (Attrs.empty())
2458 return *this;
2459
2460 assert(isSortedAndUnique(Attrs) &&
2461 "Expected sorted attributes with distinct kinds!");
2462
2463 SmallVector<Attribute, 32> Merged;
2464 mergeSortedAttrs(A: ArrayRef(begin(), end()), B: Attrs, Merged);
2465 return AttributeSet(AttributeSetNode::getSorted(C, SortedAttrs: Merged));
2466}
2467
2468AttrBuilder &AttrBuilder::remove(const AttributeMask &AM) {
2469 erase_if(C&: Attrs, P: [&](Attribute A) { return AM.contains(A); });
2470 return *this;
2471}
2472
2473bool AttrBuilder::overlaps(const AttributeMask &AM) const {
2474 return any_of(Range: Attrs, P: [&](Attribute A) { return AM.contains(A); });
2475}
2476
2477Attribute AttrBuilder::getAttribute(Attribute::AttrKind A) const {
2478 assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
2479 auto It = lower_bound(Range: Attrs, Value&: A, C: AttributeComparator());
2480 if (It != Attrs.end() && It->hasAttribute(Kind: A))
2481 return *It;
2482 return {};
2483}
2484
2485Attribute AttrBuilder::getAttribute(StringRef A) const {
2486 auto It = lower_bound(Range: Attrs, Value&: A, C: AttributeComparator());
2487 if (It != Attrs.end() && It->hasAttribute(Kind: A))
2488 return *It;
2489 return {};
2490}
2491
2492std::optional<ConstantRange> AttrBuilder::getRange() const {
2493 const Attribute RangeAttr = getAttribute(A: Attribute::Range);
2494 if (RangeAttr.isValid())
2495 return RangeAttr.getRange();
2496 return std::nullopt;
2497}
2498
2499bool AttrBuilder::contains(Attribute::AttrKind A) const {
2500 return getAttribute(A).isValid();
2501}
2502
2503bool AttrBuilder::contains(StringRef A) const {
2504 return getAttribute(A).isValid();
2505}
2506
2507bool AttrBuilder::operator==(const AttrBuilder &B) const {
2508 return Attrs == B.Attrs;
2509}
2510
2511//===----------------------------------------------------------------------===//
2512// AttributeFuncs Function Defintions
2513//===----------------------------------------------------------------------===//
2514
2515/// Returns true if this is a type legal for the 'nofpclass' attribute. This
2516/// follows the same type rules as FPMathOperator.
2517bool AttributeFuncs::isNoFPClassCompatibleType(Type *Ty) {
2518 return FPMathOperator::isSupportedFloatingPointType(Ty);
2519}
2520
2521/// Which attributes cannot be applied to a type.
2522AttributeMask AttributeFuncs::typeIncompatible(Type *Ty, AttributeSet AS,
2523 AttributeSafetyKind ASK) {
2524 AttributeMask Incompatible;
2525
2526 if (!Ty->isIntegerTy()) {
2527 // Attributes that only apply to integers.
2528 if (ASK & ASK_SAFE_TO_DROP)
2529 Incompatible.addAttribute(Val: Attribute::AllocAlign);
2530 }
2531
2532 if (!Ty->isIntegerTy() && !Ty->isByteTy()) {
2533 // Attributes that only apply to integers and bytes.
2534 if (ASK & ASK_UNSAFE_TO_DROP)
2535 Incompatible.addAttribute(Val: Attribute::SExt).addAttribute(Val: Attribute::ZExt);
2536 }
2537
2538 if (!Ty->isIntOrIntVectorTy()) {
2539 // Attributes that only apply to integers or vector of integers.
2540 if (ASK & ASK_SAFE_TO_DROP)
2541 Incompatible.addAttribute(Val: Attribute::Range);
2542 } else {
2543 Attribute RangeAttr = AS.getAttribute(Kind: Attribute::Range);
2544 if (RangeAttr.isValid() &&
2545 RangeAttr.getRange().getBitWidth() != Ty->getScalarSizeInBits())
2546 Incompatible.addAttribute(Val: Attribute::Range);
2547 }
2548
2549 if (!Ty->isPointerTy()) {
2550 // Attributes that only apply to pointers.
2551 if (ASK & ASK_SAFE_TO_DROP)
2552 Incompatible.addAttribute(Val: Attribute::NoAlias)
2553 .addAttribute(Val: Attribute::NonNull)
2554 .addAttribute(Val: Attribute::ReadNone)
2555 .addAttribute(Val: Attribute::ReadOnly)
2556 .addAttribute(Val: Attribute::Dereferenceable)
2557 .addAttribute(Val: Attribute::DereferenceableOrNull)
2558 .addAttribute(Val: Attribute::Writable)
2559 .addAttribute(Val: Attribute::DeadOnUnwind)
2560 .addAttribute(Val: Attribute::Initializes)
2561 .addAttribute(Val: Attribute::Captures)
2562 .addAttribute(Val: Attribute::DeadOnReturn)
2563 .addAttribute(Val: Attribute::NoFree)
2564 .addAttribute(Val: Attribute::NoFreeObj);
2565 if (ASK & ASK_UNSAFE_TO_DROP)
2566 Incompatible.addAttribute(Val: Attribute::Nest)
2567 .addAttribute(Val: Attribute::SwiftError)
2568 .addAttribute(Val: Attribute::Preallocated)
2569 .addAttribute(Val: Attribute::InAlloca)
2570 .addAttribute(Val: Attribute::ByVal)
2571 .addAttribute(Val: Attribute::StructRet)
2572 .addAttribute(Val: Attribute::ByRef)
2573 .addAttribute(Val: Attribute::ElementType)
2574 .addAttribute(Val: Attribute::AllocatedPointer);
2575 }
2576
2577 // Attributes that only apply to pointers or vectors of pointers.
2578 if (!Ty->isPtrOrPtrVectorTy()) {
2579 if (ASK & ASK_SAFE_TO_DROP)
2580 Incompatible.addAttribute(Val: Attribute::Alignment);
2581 }
2582
2583 if (ASK & ASK_SAFE_TO_DROP) {
2584 if (!isNoFPClassCompatibleType(Ty))
2585 Incompatible.addAttribute(Val: Attribute::NoFPClass);
2586 }
2587
2588 // Some attributes can apply to all "values" but there are no `void` values.
2589 if (Ty->isVoidTy()) {
2590 if (ASK & ASK_SAFE_TO_DROP)
2591 Incompatible.addAttribute(Val: Attribute::NoUndef);
2592 }
2593
2594 return Incompatible;
2595}
2596
2597AttributeMask AttributeFuncs::getUBImplyingAttributes() {
2598 AttributeMask AM;
2599 AM.addAttribute(Val: Attribute::NoUndef);
2600 AM.addAttribute(Val: Attribute::Dereferenceable);
2601 AM.addAttribute(Val: Attribute::DereferenceableOrNull);
2602 return AM;
2603}
2604
2605AttributeMask AttributeFuncs::getPoisonGeneratingAttributes() {
2606 AttributeMask AM;
2607 AM.addAttribute(Val: Attribute::Range);
2608 AM.addAttribute(Val: Attribute::Alignment);
2609 AM.addAttribute(Val: Attribute::NonNull);
2610 AM.addAttribute(Val: Attribute::NoFPClass);
2611 return AM;
2612}
2613
2614/// Callees with dynamic denormal modes are compatible with any caller mode.
2615static bool denormModeCompatible(DenormalMode CallerMode,
2616 DenormalMode CalleeMode) {
2617 if (CallerMode == CalleeMode || CalleeMode == DenormalMode::getDynamic())
2618 return true;
2619
2620 // If they don't exactly match, it's OK if the mismatched component is
2621 // dynamic.
2622 if (CalleeMode.Input == CallerMode.Input &&
2623 CalleeMode.Output == DenormalMode::Dynamic)
2624 return true;
2625
2626 if (CalleeMode.Output == CallerMode.Output &&
2627 CalleeMode.Input == DenormalMode::Dynamic)
2628 return true;
2629 return false;
2630}
2631
2632static DenormalFPEnv getDenormalFPEnv(AttributeSet FnAttrs) {
2633 Attribute Attr = FnAttrs.getAttribute(Kind: Attribute::DenormalFPEnv);
2634 return Attr.isValid() ? Attr.getDenormalFPEnv() : DenormalFPEnv::getDefault();
2635}
2636
2637static bool checkDenormMode(AttributeSet CallerAttrs,
2638 AttributeSet CalleeAttrs) {
2639 DenormalFPEnv CallerEnv = getDenormalFPEnv(FnAttrs: CallerAttrs);
2640 DenormalFPEnv CalleeEnv = getDenormalFPEnv(FnAttrs: CalleeAttrs);
2641
2642 if (denormModeCompatible(CallerMode: CallerEnv.DefaultMode, CalleeMode: CalleeEnv.DefaultMode)) {
2643 DenormalMode CallerModeF32 = CallerEnv.F32Mode;
2644 DenormalMode CalleeModeF32 = CalleeEnv.F32Mode;
2645 if (CallerModeF32 == DenormalMode::getInvalid())
2646 CallerModeF32 = CallerEnv.DefaultMode;
2647 if (CalleeModeF32 == DenormalMode::getInvalid())
2648 CalleeModeF32 = CalleeEnv.DefaultMode;
2649 return denormModeCompatible(CallerMode: CallerModeF32, CalleeMode: CalleeModeF32);
2650 }
2651
2652 return false;
2653}
2654
2655static bool checkStrictFP(AttributeSet CallerAttrs, AttributeSet CalleeAttrs) {
2656 // Do not inline strictfp function into non-strictfp one. It would require
2657 // conversion of all FP operations in host function to constrained intrinsics.
2658 return !CalleeAttrs.hasAttribute(Kind: Attribute::StrictFP) ||
2659 CallerAttrs.hasAttribute(Kind: Attribute::StrictFP);
2660}
2661
2662template <typename AttrClass>
2663static bool isEqual(AttributeSet CallerAttrs, AttributeSet CalleeAttrs) {
2664 return CallerAttrs.getAttribute(AttrClass::getKind()) ==
2665 CalleeAttrs.getAttribute(AttrClass::getKind());
2666}
2667
2668static bool isEqual(AttributeSet CallerAttrs, AttributeSet CalleeAttrs,
2669 StringRef AttrName) {
2670 return CallerAttrs.getAttribute(Kind: AttrName) ==
2671 CalleeAttrs.getAttribute(Kind: AttrName);
2672}
2673
2674/// Compute the logical AND of the attributes of the caller and the
2675/// callee.
2676///
2677/// This function sets the caller's attribute to false if the callee's attribute
2678/// is false.
2679template<typename AttrClass>
2680static void setAND(Function &Caller, const Function &Callee) {
2681 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
2682 !AttrClass::isSet(Callee, AttrClass::getKind()))
2683 AttrClass::set(Caller, AttrClass::getKind(), false);
2684}
2685
2686/// Compute the logical OR of the attributes of the caller and the
2687/// callee.
2688///
2689/// This function sets the caller's attribute to true if the callee's attribute
2690/// is true.
2691template<typename AttrClass>
2692static void setOR(Function &Caller, const Function &Callee) {
2693 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
2694 AttrClass::isSet(Callee, AttrClass::getKind()))
2695 AttrClass::set(Caller, AttrClass::getKind(), true);
2696}
2697
2698/// If the inlined function had a higher stack protection level than the
2699/// calling function, then bump up the caller's stack protection level.
2700static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
2701 // If the calling function has *no* stack protection level (e.g. it was built
2702 // with Clang's -fno-stack-protector or no_stack_protector attribute), don't
2703 // change it as that could change the program's semantics.
2704 if (!Caller.hasStackProtectorFnAttr())
2705 return;
2706
2707 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
2708 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
2709 // clutter to the IR.
2710 AttributeMask OldSSPAttr;
2711 OldSSPAttr.addAttribute(Val: Attribute::StackProtect)
2712 .addAttribute(Val: Attribute::StackProtectStrong)
2713 .addAttribute(Val: Attribute::StackProtectReq);
2714
2715 if (Callee.hasFnAttribute(Kind: Attribute::StackProtectReq)) {
2716 Caller.removeFnAttrs(Attrs: OldSSPAttr);
2717 Caller.addFnAttr(Kind: Attribute::StackProtectReq);
2718 } else if (Callee.hasFnAttribute(Kind: Attribute::StackProtectStrong) &&
2719 !Caller.hasFnAttribute(Kind: Attribute::StackProtectReq)) {
2720 Caller.removeFnAttrs(Attrs: OldSSPAttr);
2721 Caller.addFnAttr(Kind: Attribute::StackProtectStrong);
2722 } else if (Callee.hasFnAttribute(Kind: Attribute::StackProtect) &&
2723 !Caller.hasFnAttribute(Kind: Attribute::StackProtectReq) &&
2724 !Caller.hasFnAttribute(Kind: Attribute::StackProtectStrong))
2725 Caller.addFnAttr(Kind: Attribute::StackProtect);
2726}
2727
2728/// If the inlined function required stack probes, then ensure that
2729/// the calling function has those too.
2730static void adjustCallerStackProbes(Function &Caller, const Function &Callee) {
2731 if (!Caller.hasFnAttribute(Kind: "probe-stack") &&
2732 Callee.hasFnAttribute(Kind: "probe-stack")) {
2733 Caller.addFnAttr(Attr: Callee.getFnAttribute(Kind: "probe-stack"));
2734 }
2735}
2736
2737/// If the inlined function defines the size of guard region
2738/// on the stack, then ensure that the calling function defines a guard region
2739/// that is no larger.
2740static void
2741adjustCallerStackProbeSize(Function &Caller, const Function &Callee) {
2742 Attribute CalleeAttr = Callee.getFnAttribute(Kind: "stack-probe-size");
2743 if (CalleeAttr.isValid()) {
2744 Attribute CallerAttr = Caller.getFnAttribute(Kind: "stack-probe-size");
2745 if (CallerAttr.isValid()) {
2746 uint64_t CallerStackProbeSize, CalleeStackProbeSize;
2747 CallerAttr.getValueAsString().getAsInteger(Radix: 0, Result&: CallerStackProbeSize);
2748 CalleeAttr.getValueAsString().getAsInteger(Radix: 0, Result&: CalleeStackProbeSize);
2749
2750 if (CallerStackProbeSize > CalleeStackProbeSize) {
2751 Caller.addFnAttr(Attr: CalleeAttr);
2752 }
2753 } else {
2754 Caller.addFnAttr(Attr: CalleeAttr);
2755 }
2756 }
2757}
2758
2759/// If the inlined function defines a min legal vector width, then ensure
2760/// the calling function has the same or larger min legal vector width. If the
2761/// caller has the attribute, but the callee doesn't, we need to remove the
2762/// attribute from the caller since we can't make any guarantees about the
2763/// caller's requirements.
2764/// This function is called after the inlining decision has been made so we have
2765/// to merge the attribute this way. Heuristics that would use
2766/// min-legal-vector-width to determine inline compatibility would need to be
2767/// handled as part of inline cost analysis.
2768static void
2769adjustMinLegalVectorWidth(Function &Caller, const Function &Callee) {
2770 Attribute CallerAttr = Caller.getFnAttribute(Kind: "min-legal-vector-width");
2771 if (CallerAttr.isValid()) {
2772 Attribute CalleeAttr = Callee.getFnAttribute(Kind: "min-legal-vector-width");
2773 if (CalleeAttr.isValid()) {
2774 uint64_t CallerVectorWidth, CalleeVectorWidth;
2775 CallerAttr.getValueAsString().getAsInteger(Radix: 0, Result&: CallerVectorWidth);
2776 CalleeAttr.getValueAsString().getAsInteger(Radix: 0, Result&: CalleeVectorWidth);
2777 if (CallerVectorWidth < CalleeVectorWidth)
2778 Caller.addFnAttr(Attr: CalleeAttr);
2779 } else {
2780 // If the callee doesn't have the attribute then we don't know anything
2781 // and must drop the attribute from the caller.
2782 Caller.removeFnAttr(Kind: "min-legal-vector-width");
2783 }
2784 }
2785}
2786
2787/// If the inlined function has null_pointer_is_valid attribute,
2788/// set this attribute in the caller post inlining.
2789static void
2790adjustNullPointerValidAttr(Function &Caller, const Function &Callee) {
2791 if (Callee.nullPointerIsDefined() && !Caller.nullPointerIsDefined()) {
2792 Caller.addFnAttr(Kind: Attribute::NullPointerIsValid);
2793 }
2794}
2795
2796struct EnumAttr {
2797 static bool isSet(const Function &Fn,
2798 Attribute::AttrKind Kind) {
2799 return Fn.hasFnAttribute(Kind);
2800 }
2801
2802 static void set(Function &Fn,
2803 Attribute::AttrKind Kind, bool Val) {
2804 if (Val)
2805 Fn.addFnAttr(Kind);
2806 else
2807 Fn.removeFnAttr(Kind);
2808 }
2809};
2810
2811struct StrBoolAttr {
2812 static bool isSet(const Function &Fn,
2813 StringRef Kind) {
2814 auto A = Fn.getFnAttribute(Kind);
2815 return A.getValueAsString() == "true";
2816 }
2817
2818 static void set(Function &Fn,
2819 StringRef Kind, bool Val) {
2820 Fn.addFnAttr(Kind, Val: Val ? "true" : "false");
2821 }
2822};
2823
2824#define GET_ATTR_NAMES
2825#define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) \
2826 struct ENUM_NAME##Attr : EnumAttr { \
2827 static enum Attribute::AttrKind getKind() { \
2828 return llvm::Attribute::ENUM_NAME; \
2829 } \
2830 };
2831#define ATTRIBUTE_STRBOOL(ENUM_NAME, DISPLAY_NAME) \
2832 struct ENUM_NAME##Attr : StrBoolAttr { \
2833 static StringRef getKind() { return #DISPLAY_NAME; } \
2834 };
2835#include "llvm/IR/Attributes.inc"
2836
2837#define GET_ATTR_COMPAT_FUNC
2838#include "llvm/IR/Attributes.inc"
2839
2840bool AttributeFuncs::areInlineCompatible(const Function &Caller,
2841 const Function &Callee) {
2842 return hasCompatibleFnAttrs(Caller, Callee);
2843}
2844
2845bool AttributeFuncs::isStrictFPInlineCompatible(const Function &Caller,
2846 const Function &Callee) {
2847 return checkStrictFP(CallerAttrs: Caller.getAttributes().getFnAttrs(),
2848 CalleeAttrs: Callee.getAttributes().getFnAttrs());
2849}
2850
2851bool AttributeFuncs::areOutlineCompatible(const Function &A,
2852 const Function &B) {
2853 return hasCompatibleFnAttrs(Caller: A, Callee: B);
2854}
2855
2856void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
2857 const Function &Callee) {
2858 mergeFnAttrs(Caller, Callee);
2859}
2860
2861void AttributeFuncs::mergeAttributesForOutlining(Function &Base,
2862 const Function &ToMerge) {
2863
2864 // We merge functions so that they meet the most general case.
2865 // For example, if the NoNansFPMathAttr is set in one function, but not in
2866 // the other, in the merged function we can say that the NoNansFPMathAttr
2867 // is not set.
2868 // However if we have the SpeculativeLoadHardeningAttr set true in one
2869 // function, but not the other, we make sure that the function retains
2870 // that aspect in the merged function.
2871 mergeFnAttrs(Caller&: Base, Callee: ToMerge);
2872}
2873
2874void AttributeFuncs::updateMinLegalVectorWidthAttr(Function &Fn,
2875 uint64_t Width) {
2876 Attribute Attr = Fn.getFnAttribute(Kind: "min-legal-vector-width");
2877 if (Attr.isValid()) {
2878 uint64_t OldWidth;
2879 Attr.getValueAsString().getAsInteger(Radix: 0, Result&: OldWidth);
2880 if (Width > OldWidth)
2881 Fn.addFnAttr(Kind: "min-legal-vector-width", Val: llvm::utostr(X: Width));
2882 }
2883}
2884