1 //===-- Attributes.cpp - Implement AttributesList -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 // \brief This file implements the Attribute, AttributeImpl, AttrBuilder,
12 // AttributeSetImpl, and AttributeSet classes.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/IR/Attributes.h"
17 #include "llvm/IR/Function.h"
18 #include "AttributeImpl.h"
19 #include "LLVMContextImpl.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/IR/Type.h"
23 #include "llvm/Support/Atomic.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ManagedStatic.h"
26 #include "llvm/Support/Mutex.h"
27 #include "llvm/Support/raw_ostream.h"
31 //===----------------------------------------------------------------------===//
32 // Attribute Construction Methods
33 //===----------------------------------------------------------------------===//
35 // allocsize has two integer arguments, but because they're both 32 bits, we can
36 // pack them into one 64-bit value, at the cost of making said value
39 // In order to do this, we need to reserve one value of the second (optional)
40 // allocsize argument to signify "not present."
41 LLVM_CONSTEXPR static unsigned AllocSizeNumElemsNotPresent = -1;
43 static uint64_t packAllocSizeArgs(unsigned ElemSizeArg,
44 const Optional<unsigned> &NumElemsArg) {
45 assert((!NumElemsArg.hasValue() ||
46 *NumElemsArg != AllocSizeNumElemsNotPresent) &&
47 "Attempting to pack a reserved value");
49 return uint64_t(ElemSizeArg) << 32 |
50 NumElemsArg.getValueOr(AllocSizeNumElemsNotPresent);
53 static std::pair<unsigned, Optional<unsigned>>
54 unpackAllocSizeArgs(uint64_t Num) {
55 unsigned NumElems = Num & std::numeric_limits<unsigned>::max();
56 unsigned ElemSizeArg = Num >> 32;
58 Optional<unsigned> NumElemsArg;
59 if (NumElems != AllocSizeNumElemsNotPresent)
60 NumElemsArg = NumElems;
61 return std::make_pair(ElemSizeArg, NumElemsArg);
64 Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
66 LLVMContextImpl *pImpl = Context.pImpl;
69 if (Val) ID.AddInteger(Val);
72 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
75 // If we didn't find any existing attributes of the same shape then create a
76 // new one and insert it.
78 PA = new EnumAttributeImpl(Kind);
80 PA = new IntAttributeImpl(Kind, Val);
81 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
84 // Return the Attribute that we found or created.
88 Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
89 LLVMContextImpl *pImpl = Context.pImpl;
92 if (!Val.empty()) ID.AddString(Val);
95 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
98 // If we didn't find any existing attributes of the same shape then create a
99 // new one and insert it.
100 PA = new StringAttributeImpl(Kind, Val);
101 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
104 // Return the Attribute that we found or created.
105 return Attribute(PA);
108 Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
109 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
110 assert(Align <= 0x40000000 && "Alignment too large.");
111 return get(Context, Alignment, Align);
114 Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
116 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
117 assert(Align <= 0x100 && "Alignment too large.");
118 return get(Context, StackAlignment, Align);
121 Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context,
123 assert(Bytes && "Bytes must be non-zero.");
124 return get(Context, Dereferenceable, Bytes);
127 Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context,
129 assert(Bytes && "Bytes must be non-zero.");
130 return get(Context, DereferenceableOrNull, Bytes);
134 Attribute::getWithAllocSizeArgs(LLVMContext &Context, unsigned ElemSizeArg,
135 const Optional<unsigned> &NumElemsArg) {
136 assert(!(ElemSizeArg == 0 && NumElemsArg && *NumElemsArg == 0) &&
137 "Invalid allocsize arguments -- given allocsize(0, 0)");
138 return get(Context, AllocSize, packAllocSizeArgs(ElemSizeArg, NumElemsArg));
141 //===----------------------------------------------------------------------===//
142 // Attribute Accessor Methods
143 //===----------------------------------------------------------------------===//
145 bool Attribute::isEnumAttribute() const {
146 return pImpl && pImpl->isEnumAttribute();
149 bool Attribute::isIntAttribute() const {
150 return pImpl && pImpl->isIntAttribute();
153 bool Attribute::isStringAttribute() const {
154 return pImpl && pImpl->isStringAttribute();
157 Attribute::AttrKind Attribute::getKindAsEnum() const {
158 if (!pImpl) return None;
159 assert((isEnumAttribute() || isIntAttribute()) &&
160 "Invalid attribute type to get the kind as an enum!");
161 return pImpl->getKindAsEnum();
164 uint64_t Attribute::getValueAsInt() const {
165 if (!pImpl) return 0;
166 assert(isIntAttribute() &&
167 "Expected the attribute to be an integer attribute!");
168 return pImpl->getValueAsInt();
171 StringRef Attribute::getKindAsString() const {
172 if (!pImpl) return StringRef();
173 assert(isStringAttribute() &&
174 "Invalid attribute type to get the kind as a string!");
175 return pImpl->getKindAsString();
178 StringRef Attribute::getValueAsString() const {
179 if (!pImpl) return StringRef();
180 assert(isStringAttribute() &&
181 "Invalid attribute type to get the value as a string!");
182 return pImpl->getValueAsString();
185 bool Attribute::hasAttribute(AttrKind Kind) const {
186 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
189 bool Attribute::hasAttribute(StringRef Kind) const {
190 if (!isStringAttribute()) return false;
191 return pImpl && pImpl->hasAttribute(Kind);
194 unsigned Attribute::getAlignment() const {
195 assert(hasAttribute(Attribute::Alignment) &&
196 "Trying to get alignment from non-alignment attribute!");
197 return pImpl->getValueAsInt();
200 unsigned Attribute::getStackAlignment() const {
201 assert(hasAttribute(Attribute::StackAlignment) &&
202 "Trying to get alignment from non-alignment attribute!");
203 return pImpl->getValueAsInt();
206 uint64_t Attribute::getDereferenceableBytes() const {
207 assert(hasAttribute(Attribute::Dereferenceable) &&
208 "Trying to get dereferenceable bytes from "
209 "non-dereferenceable attribute!");
210 return pImpl->getValueAsInt();
213 uint64_t Attribute::getDereferenceableOrNullBytes() const {
214 assert(hasAttribute(Attribute::DereferenceableOrNull) &&
215 "Trying to get dereferenceable bytes from "
216 "non-dereferenceable attribute!");
217 return pImpl->getValueAsInt();
220 std::pair<unsigned, Optional<unsigned>> Attribute::getAllocSizeArgs() const {
221 assert(hasAttribute(Attribute::AllocSize) &&
222 "Trying to get allocsize args from non-allocsize attribute");
223 return unpackAllocSizeArgs(pImpl->getValueAsInt());
226 std::string Attribute::getAsString(bool InAttrGrp) const {
227 if (!pImpl) return "";
229 if (hasAttribute(Attribute::SanitizeAddress))
230 return "sanitize_address";
231 if (hasAttribute(Attribute::AlwaysInline))
232 return "alwaysinline";
233 if (hasAttribute(Attribute::ArgMemOnly))
235 if (hasAttribute(Attribute::Builtin))
237 if (hasAttribute(Attribute::ByVal))
239 if (hasAttribute(Attribute::Convergent))
241 if (hasAttribute(Attribute::SwiftError))
243 if (hasAttribute(Attribute::SwiftSelf))
245 if (hasAttribute(Attribute::InaccessibleMemOnly))
246 return "inaccessiblememonly";
247 if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly))
248 return "inaccessiblemem_or_argmemonly";
249 if (hasAttribute(Attribute::InAlloca))
251 if (hasAttribute(Attribute::InlineHint))
253 if (hasAttribute(Attribute::InReg))
255 if (hasAttribute(Attribute::JumpTable))
257 if (hasAttribute(Attribute::MinSize))
259 if (hasAttribute(Attribute::Naked))
261 if (hasAttribute(Attribute::Nest))
263 if (hasAttribute(Attribute::NoAlias))
265 if (hasAttribute(Attribute::NoBuiltin))
267 if (hasAttribute(Attribute::NoCapture))
269 if (hasAttribute(Attribute::NoDuplicate))
270 return "noduplicate";
271 if (hasAttribute(Attribute::NoImplicitFloat))
272 return "noimplicitfloat";
273 if (hasAttribute(Attribute::NoInline))
275 if (hasAttribute(Attribute::NonLazyBind))
276 return "nonlazybind";
277 if (hasAttribute(Attribute::NonNull))
279 if (hasAttribute(Attribute::NoRedZone))
281 if (hasAttribute(Attribute::NoReturn))
283 if (hasAttribute(Attribute::NoRecurse))
285 if (hasAttribute(Attribute::NoUnwind))
287 if (hasAttribute(Attribute::OptimizeNone))
289 if (hasAttribute(Attribute::OptimizeForSize))
291 if (hasAttribute(Attribute::ReadNone))
293 if (hasAttribute(Attribute::ReadOnly))
295 if (hasAttribute(Attribute::WriteOnly))
297 if (hasAttribute(Attribute::Returned))
299 if (hasAttribute(Attribute::ReturnsTwice))
300 return "returns_twice";
301 if (hasAttribute(Attribute::SExt))
303 if (hasAttribute(Attribute::StackProtect))
305 if (hasAttribute(Attribute::StackProtectReq))
307 if (hasAttribute(Attribute::StackProtectStrong))
309 if (hasAttribute(Attribute::SafeStack))
311 if (hasAttribute(Attribute::StructRet))
313 if (hasAttribute(Attribute::SanitizeThread))
314 return "sanitize_thread";
315 if (hasAttribute(Attribute::SanitizeMemory))
316 return "sanitize_memory";
317 if (hasAttribute(Attribute::UWTable))
319 if (hasAttribute(Attribute::ZExt))
321 if (hasAttribute(Attribute::Cold))
324 // FIXME: These should be output like this:
329 if (hasAttribute(Attribute::Alignment)) {
332 Result += (InAttrGrp) ? "=" : " ";
333 Result += utostr(getValueAsInt());
337 auto AttrWithBytesToString = [&](const char *Name) {
342 Result += utostr(getValueAsInt());
345 Result += utostr(getValueAsInt());
351 if (hasAttribute(Attribute::StackAlignment))
352 return AttrWithBytesToString("alignstack");
354 if (hasAttribute(Attribute::Dereferenceable))
355 return AttrWithBytesToString("dereferenceable");
357 if (hasAttribute(Attribute::DereferenceableOrNull))
358 return AttrWithBytesToString("dereferenceable_or_null");
360 if (hasAttribute(Attribute::AllocSize)) {
362 Optional<unsigned> NumElems;
363 std::tie(ElemSize, NumElems) = getAllocSizeArgs();
365 std::string Result = "allocsize(";
366 Result += utostr(ElemSize);
367 if (NumElems.hasValue()) {
369 Result += utostr(*NumElems);
375 // Convert target-dependent attributes to strings of the form:
380 if (isStringAttribute()) {
382 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
384 StringRef Val = pImpl->getValueAsString();
385 if (Val.empty()) return Result;
387 Result += ("=\"" + Val + Twine('"')).str();
391 llvm_unreachable("Unknown attribute");
394 bool Attribute::operator<(Attribute A) const {
395 if (!pImpl && !A.pImpl) return false;
396 if (!pImpl) return true;
397 if (!A.pImpl) return false;
398 return *pImpl < *A.pImpl;
401 //===----------------------------------------------------------------------===//
402 // AttributeImpl Definition
403 //===----------------------------------------------------------------------===//
405 // Pin the vtables to this file.
406 AttributeImpl::~AttributeImpl() {}
407 void EnumAttributeImpl::anchor() {}
408 void IntAttributeImpl::anchor() {}
409 void StringAttributeImpl::anchor() {}
411 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
412 if (isStringAttribute()) return false;
413 return getKindAsEnum() == A;
416 bool AttributeImpl::hasAttribute(StringRef Kind) const {
417 if (!isStringAttribute()) return false;
418 return getKindAsString() == Kind;
421 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
422 assert(isEnumAttribute() || isIntAttribute());
423 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
426 uint64_t AttributeImpl::getValueAsInt() const {
427 assert(isIntAttribute());
428 return static_cast<const IntAttributeImpl *>(this)->getValue();
431 StringRef AttributeImpl::getKindAsString() const {
432 assert(isStringAttribute());
433 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
436 StringRef AttributeImpl::getValueAsString() const {
437 assert(isStringAttribute());
438 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
441 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
442 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
443 // relative to their enum value) and then strings.
444 if (isEnumAttribute()) {
445 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
446 if (AI.isIntAttribute()) return true;
447 if (AI.isStringAttribute()) return true;
450 if (isIntAttribute()) {
451 if (AI.isEnumAttribute()) return false;
452 if (AI.isIntAttribute()) {
453 if (getKindAsEnum() == AI.getKindAsEnum())
454 return getValueAsInt() < AI.getValueAsInt();
455 return getKindAsEnum() < AI.getKindAsEnum();
457 if (AI.isStringAttribute()) return true;
460 if (AI.isEnumAttribute()) return false;
461 if (AI.isIntAttribute()) return false;
462 if (getKindAsString() == AI.getKindAsString())
463 return getValueAsString() < AI.getValueAsString();
464 return getKindAsString() < AI.getKindAsString();
467 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
468 // FIXME: Remove this.
470 case Attribute::EndAttrKinds:
471 llvm_unreachable("Synthetic enumerators which should never get here");
473 case Attribute::None: return 0;
474 case Attribute::ZExt: return 1 << 0;
475 case Attribute::SExt: return 1 << 1;
476 case Attribute::NoReturn: return 1 << 2;
477 case Attribute::InReg: return 1 << 3;
478 case Attribute::StructRet: return 1 << 4;
479 case Attribute::NoUnwind: return 1 << 5;
480 case Attribute::NoAlias: return 1 << 6;
481 case Attribute::ByVal: return 1 << 7;
482 case Attribute::Nest: return 1 << 8;
483 case Attribute::ReadNone: return 1 << 9;
484 case Attribute::ReadOnly: return 1 << 10;
485 case Attribute::NoInline: return 1 << 11;
486 case Attribute::AlwaysInline: return 1 << 12;
487 case Attribute::OptimizeForSize: return 1 << 13;
488 case Attribute::StackProtect: return 1 << 14;
489 case Attribute::StackProtectReq: return 1 << 15;
490 case Attribute::Alignment: return 31 << 16;
491 case Attribute::NoCapture: return 1 << 21;
492 case Attribute::NoRedZone: return 1 << 22;
493 case Attribute::NoImplicitFloat: return 1 << 23;
494 case Attribute::Naked: return 1 << 24;
495 case Attribute::InlineHint: return 1 << 25;
496 case Attribute::StackAlignment: return 7 << 26;
497 case Attribute::ReturnsTwice: return 1 << 29;
498 case Attribute::UWTable: return 1 << 30;
499 case Attribute::NonLazyBind: return 1U << 31;
500 case Attribute::SanitizeAddress: return 1ULL << 32;
501 case Attribute::MinSize: return 1ULL << 33;
502 case Attribute::NoDuplicate: return 1ULL << 34;
503 case Attribute::StackProtectStrong: return 1ULL << 35;
504 case Attribute::SanitizeThread: return 1ULL << 36;
505 case Attribute::SanitizeMemory: return 1ULL << 37;
506 case Attribute::NoBuiltin: return 1ULL << 38;
507 case Attribute::Returned: return 1ULL << 39;
508 case Attribute::Cold: return 1ULL << 40;
509 case Attribute::Builtin: return 1ULL << 41;
510 case Attribute::OptimizeNone: return 1ULL << 42;
511 case Attribute::InAlloca: return 1ULL << 43;
512 case Attribute::NonNull: return 1ULL << 44;
513 case Attribute::JumpTable: return 1ULL << 45;
514 case Attribute::Convergent: return 1ULL << 46;
515 case Attribute::SafeStack: return 1ULL << 47;
516 case Attribute::NoRecurse: return 1ULL << 48;
517 case Attribute::InaccessibleMemOnly: return 1ULL << 49;
518 case Attribute::InaccessibleMemOrArgMemOnly: return 1ULL << 50;
519 case Attribute::SwiftSelf: return 1ULL << 51;
520 case Attribute::SwiftError: return 1ULL << 52;
521 case Attribute::WriteOnly: return 1ULL << 53;
522 case Attribute::Dereferenceable:
523 llvm_unreachable("dereferenceable attribute not supported in raw format");
525 case Attribute::DereferenceableOrNull:
526 llvm_unreachable("dereferenceable_or_null attribute not supported in raw "
529 case Attribute::ArgMemOnly:
530 llvm_unreachable("argmemonly attribute not supported in raw format");
532 case Attribute::AllocSize:
533 llvm_unreachable("allocsize not supported in raw format");
536 llvm_unreachable("Unsupported attribute type");
539 //===----------------------------------------------------------------------===//
540 // AttributeSetNode Definition
541 //===----------------------------------------------------------------------===//
543 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
544 ArrayRef<Attribute> Attrs) {
548 // Otherwise, build a key to look up the existing attributes.
549 LLVMContextImpl *pImpl = C.pImpl;
552 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
553 std::sort(SortedAttrs.begin(), SortedAttrs.end());
555 for (Attribute Attr : SortedAttrs)
559 AttributeSetNode *PA =
560 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
562 // If we didn't find any existing attributes of the same shape then create a
563 // new one and insert it.
565 // Coallocate entries after the AttributeSetNode itself.
566 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
567 PA = new (Mem) AttributeSetNode(SortedAttrs);
568 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
571 // Return the AttributesListNode that we found or created.
575 bool AttributeSetNode::hasAttribute(StringRef Kind) const {
576 for (Attribute I : *this)
577 if (I.hasAttribute(Kind))
582 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
583 if (hasAttribute(Kind)) {
584 for (Attribute I : *this)
585 if (I.hasAttribute(Kind))
591 Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
592 for (Attribute I : *this)
593 if (I.hasAttribute(Kind))
598 unsigned AttributeSetNode::getAlignment() const {
599 for (Attribute I : *this)
600 if (I.hasAttribute(Attribute::Alignment))
601 return I.getAlignment();
605 unsigned AttributeSetNode::getStackAlignment() const {
606 for (Attribute I : *this)
607 if (I.hasAttribute(Attribute::StackAlignment))
608 return I.getStackAlignment();
612 uint64_t AttributeSetNode::getDereferenceableBytes() const {
613 for (Attribute I : *this)
614 if (I.hasAttribute(Attribute::Dereferenceable))
615 return I.getDereferenceableBytes();
619 uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
620 for (Attribute I : *this)
621 if (I.hasAttribute(Attribute::DereferenceableOrNull))
622 return I.getDereferenceableOrNullBytes();
626 std::pair<unsigned, Optional<unsigned>>
627 AttributeSetNode::getAllocSizeArgs() const {
628 for (Attribute I : *this)
629 if (I.hasAttribute(Attribute::AllocSize))
630 return I.getAllocSizeArgs();
631 return std::make_pair(0, 0);
634 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
636 for (iterator I = begin(), E = end(); I != E; ++I) {
639 Str += I->getAsString(InAttrGrp);
644 //===----------------------------------------------------------------------===//
645 // AttributeSetImpl Definition
646 //===----------------------------------------------------------------------===//
648 uint64_t AttributeSetImpl::Raw(unsigned Index) const {
649 for (unsigned I = 0, E = getNumSlots(); I != E; ++I) {
650 if (getSlotIndex(I) != Index) continue;
651 const AttributeSetNode *ASN = getSlotNode(I);
654 for (AttributeSetNode::iterator II = ASN->begin(),
655 IE = ASN->end(); II != IE; ++II) {
656 Attribute Attr = *II;
658 // This cannot handle string attributes.
659 if (Attr.isStringAttribute()) continue;
661 Attribute::AttrKind Kind = Attr.getKindAsEnum();
663 if (Kind == Attribute::Alignment)
664 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
665 else if (Kind == Attribute::StackAlignment)
666 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
667 else if (Kind == Attribute::Dereferenceable)
668 llvm_unreachable("dereferenceable not supported in bit mask");
669 else if (Kind == Attribute::AllocSize)
670 llvm_unreachable("allocsize not supported in bit mask");
672 Mask |= AttributeImpl::getAttrMask(Kind);
681 LLVM_DUMP_METHOD void AttributeSetImpl::dump() const {
682 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
685 //===----------------------------------------------------------------------===//
686 // AttributeSet Construction and Mutation Methods
687 //===----------------------------------------------------------------------===//
690 AttributeSet::getImpl(LLVMContext &C,
691 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
692 LLVMContextImpl *pImpl = C.pImpl;
694 AttributeSetImpl::Profile(ID, Attrs);
697 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
699 // If we didn't find any existing attributes of the same shape then
700 // create a new one and insert it.
702 // Coallocate entries after the AttributeSetImpl itself.
703 void *Mem = ::operator new(
704 AttributeSetImpl::totalSizeToAlloc<IndexAttrPair>(Attrs.size()));
705 PA = new (Mem) AttributeSetImpl(C, Attrs);
706 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
709 // Return the AttributesList that we found or created.
710 return AttributeSet(PA);
713 AttributeSet AttributeSet::get(LLVMContext &C,
714 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
715 // If there are no attributes then return a null AttributesList pointer.
717 return AttributeSet();
719 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
720 [](const std::pair<unsigned, Attribute> &LHS,
721 const std::pair<unsigned, Attribute> &RHS) {
722 return LHS.first < RHS.first;
723 }) && "Misordered Attributes list!");
724 assert(std::none_of(Attrs.begin(), Attrs.end(),
725 [](const std::pair<unsigned, Attribute> &Pair) {
726 return Pair.second.hasAttribute(Attribute::None);
727 }) && "Pointless attribute!");
729 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
731 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
732 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
733 E = Attrs.end(); I != E; ) {
734 unsigned Index = I->first;
735 SmallVector<Attribute, 4> AttrVec;
736 while (I != E && I->first == Index) {
737 AttrVec.push_back(I->second);
741 AttrPairVec.push_back(std::make_pair(Index,
742 AttributeSetNode::get(C, AttrVec)));
745 return getImpl(C, AttrPairVec);
748 AttributeSet AttributeSet::get(LLVMContext &C,
749 ArrayRef<std::pair<unsigned,
750 AttributeSetNode*> > Attrs) {
751 // If there are no attributes then return a null AttributesList pointer.
753 return AttributeSet();
755 return getImpl(C, Attrs);
758 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
759 const AttrBuilder &B) {
760 if (!B.hasAttributes())
761 return AttributeSet();
763 // Add target-independent attributes.
764 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
765 for (Attribute::AttrKind Kind = Attribute::None;
766 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
767 if (!B.contains(Kind))
772 case Attribute::Alignment:
773 Attr = Attribute::getWithAlignment(C, B.getAlignment());
775 case Attribute::StackAlignment:
776 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
778 case Attribute::Dereferenceable:
779 Attr = Attribute::getWithDereferenceableBytes(
780 C, B.getDereferenceableBytes());
782 case Attribute::DereferenceableOrNull:
783 Attr = Attribute::getWithDereferenceableOrNullBytes(
784 C, B.getDereferenceableOrNullBytes());
786 case Attribute::AllocSize: {
787 auto A = B.getAllocSizeArgs();
788 Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second);
792 Attr = Attribute::get(C, Kind);
794 Attrs.push_back(std::make_pair(Index, Attr));
797 // Add target-dependent (string) attributes.
798 for (const auto &TDA : B.td_attrs())
800 std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
802 return get(C, Attrs);
805 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
806 ArrayRef<Attribute::AttrKind> Kinds) {
807 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
808 for (Attribute::AttrKind K : Kinds)
809 Attrs.push_back(std::make_pair(Index, Attribute::get(C, K)));
810 return get(C, Attrs);
813 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
814 ArrayRef<StringRef> Kinds) {
815 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
816 for (StringRef K : Kinds)
817 Attrs.push_back(std::make_pair(Index, Attribute::get(C, K)));
818 return get(C, Attrs);
821 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
822 if (Attrs.empty()) return AttributeSet();
823 if (Attrs.size() == 1) return Attrs[0];
825 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
826 AttributeSetImpl *A0 = Attrs[0].pImpl;
828 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumSlots()));
829 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
830 // ordered by index. Because we know that each list in Attrs is ordered by
831 // index we only need to merge each successive list in rather than doing a
833 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
834 AttributeSetImpl *AS = Attrs[I].pImpl;
836 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
837 ANVI = AttrNodeVec.begin(), ANVE;
838 for (const IndexAttrPair *AI = AS->getNode(0),
839 *AE = AS->getNode(AS->getNumSlots());
841 ANVE = AttrNodeVec.end();
842 while (ANVI != ANVE && ANVI->first <= AI->first)
844 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
848 return getImpl(C, AttrNodeVec);
851 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
852 Attribute::AttrKind Kind) const {
853 if (hasAttribute(Index, Kind)) return *this;
854 return addAttributes(C, Index, AttributeSet::get(C, Index, Kind));
857 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
858 StringRef Kind, StringRef Value) const {
860 B.addAttribute(Kind, Value);
861 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
864 AttributeSet AttributeSet::addAttribute(LLVMContext &C,
865 ArrayRef<unsigned> Indices,
867 unsigned I = 0, E = pImpl ? pImpl->getNumSlots() : 0;
868 auto IdxI = Indices.begin(), IdxE = Indices.end();
869 SmallVector<AttributeSet, 4> AttrSet;
871 while (I != E && IdxI != IdxE) {
872 if (getSlotIndex(I) < *IdxI)
873 AttrSet.emplace_back(getSlotAttributes(I++));
874 else if (getSlotIndex(I) > *IdxI)
875 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
877 AttrBuilder B(getSlotAttributes(I), *IdxI);
879 AttrSet.emplace_back(AttributeSet::get(C, *IdxI, B));
886 AttrSet.emplace_back(getSlotAttributes(I++));
889 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
891 return get(C, AttrSet);
894 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
895 AttributeSet Attrs) const {
896 if (!pImpl) return Attrs;
897 if (!Attrs.pImpl) return *this;
900 // FIXME it is not obvious how this should work for alignment. For now, say
901 // we can't change a known alignment.
902 unsigned OldAlign = getParamAlignment(Index);
903 unsigned NewAlign = Attrs.getParamAlignment(Index);
904 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
905 "Attempt to change alignment!");
908 // Add the attribute slots before the one we're trying to add.
909 SmallVector<AttributeSet, 4> AttrSet;
910 uint64_t NumAttrs = pImpl->getNumSlots();
912 uint64_t LastIndex = 0;
913 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
914 if (getSlotIndex(I) >= Index) {
915 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
919 AttrSet.push_back(getSlotAttributes(I));
922 // Now add the attribute into the correct slot. There may already be an
923 // AttributeSet there.
924 AttrBuilder B(AS, Index);
926 for (unsigned I = 0, E = Attrs.pImpl->getNumSlots(); I != E; ++I)
927 if (Attrs.getSlotIndex(I) == Index) {
928 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
929 IE = Attrs.pImpl->end(I); II != IE; ++II)
934 AttrSet.push_back(AttributeSet::get(C, Index, B));
936 // Add the remaining attribute slots.
937 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
938 AttrSet.push_back(getSlotAttributes(I));
940 return get(C, AttrSet);
943 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
944 Attribute::AttrKind Kind) const {
945 if (!hasAttribute(Index, Kind)) return *this;
946 return removeAttributes(C, Index, AttributeSet::get(C, Index, Kind));
949 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
950 StringRef Kind) const {
951 if (!hasAttribute(Index, Kind)) return *this;
952 return removeAttributes(C, Index, AttributeSet::get(C, Index, Kind));
955 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
956 AttributeSet Attrs) const {
957 if (!pImpl) return AttributeSet();
958 if (!Attrs.pImpl) return *this;
960 // FIXME it is not obvious how this should work for alignment.
961 // For now, say we can't pass in alignment, which no current use does.
962 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
963 "Attempt to change alignment!");
965 // Add the attribute slots before the one we're trying to add.
966 SmallVector<AttributeSet, 4> AttrSet;
967 uint64_t NumAttrs = pImpl->getNumSlots();
969 uint64_t LastIndex = 0;
970 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
971 if (getSlotIndex(I) >= Index) {
972 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
976 AttrSet.push_back(getSlotAttributes(I));
979 // Now remove the attribute from the correct slot. There may already be an
980 // AttributeSet there.
981 AttrBuilder B(AS, Index);
983 for (unsigned I = 0, E = Attrs.pImpl->getNumSlots(); I != E; ++I)
984 if (Attrs.getSlotIndex(I) == Index) {
985 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
989 AttrSet.push_back(AttributeSet::get(C, Index, B));
991 // Add the remaining attribute slots.
992 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
993 AttrSet.push_back(getSlotAttributes(I));
995 return get(C, AttrSet);
998 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
999 const AttrBuilder &Attrs) const {
1000 if (!pImpl) return AttributeSet();
1002 // FIXME it is not obvious how this should work for alignment.
1003 // For now, say we can't pass in alignment, which no current use does.
1004 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
1006 // Add the attribute slots before the one we're trying to add.
1007 SmallVector<AttributeSet, 4> AttrSet;
1008 uint64_t NumAttrs = pImpl->getNumSlots();
1010 uint64_t LastIndex = 0;
1011 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
1012 if (getSlotIndex(I) >= Index) {
1013 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
1017 AttrSet.push_back(getSlotAttributes(I));
1020 // Now remove the attribute from the correct slot. There may already be an
1021 // AttributeSet there.
1022 AttrBuilder B(AS, Index);
1025 AttrSet.push_back(AttributeSet::get(C, Index, B));
1027 // Add the remaining attribute slots.
1028 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
1029 AttrSet.push_back(getSlotAttributes(I));
1031 return get(C, AttrSet);
1034 AttributeSet AttributeSet::addDereferenceableAttr(LLVMContext &C, unsigned Index,
1035 uint64_t Bytes) const {
1036 llvm::AttrBuilder B;
1037 B.addDereferenceableAttr(Bytes);
1038 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
1041 AttributeSet AttributeSet::addDereferenceableOrNullAttr(LLVMContext &C,
1043 uint64_t Bytes) const {
1044 llvm::AttrBuilder B;
1045 B.addDereferenceableOrNullAttr(Bytes);
1046 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
1050 AttributeSet::addAllocSizeAttr(LLVMContext &C, unsigned Index,
1051 unsigned ElemSizeArg,
1052 const Optional<unsigned> &NumElemsArg) {
1053 llvm::AttrBuilder B;
1054 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
1055 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
1058 //===----------------------------------------------------------------------===//
1059 // AttributeSet Accessor Methods
1060 //===----------------------------------------------------------------------===//
1062 LLVMContext &AttributeSet::getContext() const {
1063 return pImpl->getContext();
1066 AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
1067 return pImpl && hasAttributes(Index) ?
1068 AttributeSet::get(pImpl->getContext(),
1069 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
1070 std::make_pair(Index, getAttributes(Index)))) :
1074 AttributeSet AttributeSet::getRetAttributes() const {
1075 return pImpl && hasAttributes(ReturnIndex) ?
1076 AttributeSet::get(pImpl->getContext(),
1077 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
1078 std::make_pair(ReturnIndex,
1079 getAttributes(ReturnIndex)))) :
1083 AttributeSet AttributeSet::getFnAttributes() const {
1084 return pImpl && hasAttributes(FunctionIndex) ?
1085 AttributeSet::get(pImpl->getContext(),
1086 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
1087 std::make_pair(FunctionIndex,
1088 getAttributes(FunctionIndex)))) :
1092 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
1093 AttributeSetNode *ASN = getAttributes(Index);
1094 return ASN && ASN->hasAttribute(Kind);
1097 bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
1098 AttributeSetNode *ASN = getAttributes(Index);
1099 return ASN && ASN->hasAttribute(Kind);
1102 bool AttributeSet::hasAttributes(unsigned Index) const {
1103 AttributeSetNode *ASN = getAttributes(Index);
1104 return ASN && ASN->hasAttributes();
1107 bool AttributeSet::hasFnAttribute(Attribute::AttrKind Kind) const {
1108 return pImpl && pImpl->hasFnAttribute(Kind);
1111 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr,
1112 unsigned *Index) const {
1113 if (!pImpl) return false;
1115 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I)
1116 for (AttributeSetImpl::iterator II = pImpl->begin(I),
1117 IE = pImpl->end(I); II != IE; ++II)
1118 if (II->hasAttribute(Attr)) {
1119 if (Index) *Index = pImpl->getSlotIndex(I);
1126 Attribute AttributeSet::getAttribute(unsigned Index,
1127 Attribute::AttrKind Kind) const {
1128 AttributeSetNode *ASN = getAttributes(Index);
1129 return ASN ? ASN->getAttribute(Kind) : Attribute();
1132 Attribute AttributeSet::getAttribute(unsigned Index,
1133 StringRef Kind) const {
1134 AttributeSetNode *ASN = getAttributes(Index);
1135 return ASN ? ASN->getAttribute(Kind) : Attribute();
1138 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
1139 AttributeSetNode *ASN = getAttributes(Index);
1140 return ASN ? ASN->getAlignment() : 0;
1143 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
1144 AttributeSetNode *ASN = getAttributes(Index);
1145 return ASN ? ASN->getStackAlignment() : 0;
1148 uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
1149 AttributeSetNode *ASN = getAttributes(Index);
1150 return ASN ? ASN->getDereferenceableBytes() : 0;
1153 uint64_t AttributeSet::getDereferenceableOrNullBytes(unsigned Index) const {
1154 AttributeSetNode *ASN = getAttributes(Index);
1155 return ASN ? ASN->getDereferenceableOrNullBytes() : 0;
1158 std::pair<unsigned, Optional<unsigned>>
1159 AttributeSet::getAllocSizeArgs(unsigned Index) const {
1160 AttributeSetNode *ASN = getAttributes(Index);
1161 return ASN ? ASN->getAllocSizeArgs() : std::make_pair(0, 0);
1164 std::string AttributeSet::getAsString(unsigned Index, bool InAttrGrp) const {
1165 AttributeSetNode *ASN = getAttributes(Index);
1166 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
1169 AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
1170 if (!pImpl) return nullptr;
1172 // Loop through to find the attribute node we want.
1173 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I)
1174 if (pImpl->getSlotIndex(I) == Index)
1175 return pImpl->getSlotNode(I);
1180 AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
1182 return ArrayRef<Attribute>().begin();
1183 return pImpl->begin(Slot);
1186 AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
1188 return ArrayRef<Attribute>().end();
1189 return pImpl->end(Slot);
1192 //===----------------------------------------------------------------------===//
1193 // AttributeSet Introspection Methods
1194 //===----------------------------------------------------------------------===//
1196 unsigned AttributeSet::getNumSlots() const {
1197 return pImpl ? pImpl->getNumSlots() : 0;
1200 unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
1201 assert(pImpl && Slot < pImpl->getNumSlots() &&
1202 "Slot # out of range!");
1203 return pImpl->getSlotIndex(Slot);
1206 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
1207 assert(pImpl && Slot < pImpl->getNumSlots() &&
1208 "Slot # out of range!");
1209 return pImpl->getSlotAttributes(Slot);
1212 uint64_t AttributeSet::Raw(unsigned Index) const {
1213 // FIXME: Remove this.
1214 return pImpl ? pImpl->Raw(Index) : 0;
1217 LLVM_DUMP_METHOD void AttributeSet::dump() const {
1220 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1221 uint64_t Index = getSlotIndex(i);
1227 dbgs() << " => " << getAsString(Index) << " }\n";
1233 //===----------------------------------------------------------------------===//
1234 // AttrBuilder Method Implementations
1235 //===----------------------------------------------------------------------===//
1237 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
1238 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
1239 DerefOrNullBytes(0), AllocSizeArgs(0) {
1240 AttributeSetImpl *pImpl = AS.pImpl;
1243 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I) {
1244 if (pImpl->getSlotIndex(I) != Index) continue;
1246 for (AttributeSetImpl::iterator II = pImpl->begin(I),
1247 IE = pImpl->end(I); II != IE; ++II)
1254 void AttrBuilder::clear() {
1256 TargetDepAttrs.clear();
1257 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
1261 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
1262 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1263 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
1264 Val != Attribute::Dereferenceable && Val != Attribute::AllocSize &&
1265 "Adding integer attribute without adding a value!");
1270 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
1271 if (Attr.isStringAttribute()) {
1272 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1276 Attribute::AttrKind Kind = Attr.getKindAsEnum();
1279 if (Kind == Attribute::Alignment)
1280 Alignment = Attr.getAlignment();
1281 else if (Kind == Attribute::StackAlignment)
1282 StackAlignment = Attr.getStackAlignment();
1283 else if (Kind == Attribute::Dereferenceable)
1284 DerefBytes = Attr.getDereferenceableBytes();
1285 else if (Kind == Attribute::DereferenceableOrNull)
1286 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
1287 else if (Kind == Attribute::AllocSize)
1288 AllocSizeArgs = Attr.getValueAsInt();
1292 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1293 TargetDepAttrs[A] = V;
1297 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
1298 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1301 if (Val == Attribute::Alignment)
1303 else if (Val == Attribute::StackAlignment)
1305 else if (Val == Attribute::Dereferenceable)
1307 else if (Val == Attribute::DereferenceableOrNull)
1308 DerefOrNullBytes = 0;
1309 else if (Val == Attribute::AllocSize)
1315 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
1316 unsigned Slot = ~0U;
1317 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1318 if (A.getSlotIndex(I) == Index) {
1323 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
1325 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
1326 Attribute Attr = *I;
1327 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1328 removeAttribute(Attr.getKindAsEnum());
1330 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1331 removeAttribute(Attr.getKindAsString());
1338 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1339 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1340 if (I != TargetDepAttrs.end())
1341 TargetDepAttrs.erase(I);
1345 std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1346 return unpackAllocSizeArgs(AllocSizeArgs);
1349 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
1350 if (Align == 0) return *this;
1352 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1353 assert(Align <= 0x40000000 && "Alignment too large.");
1355 Attrs[Attribute::Alignment] = true;
1360 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1361 // Default alignment, allow the target to define how to align it.
1362 if (Align == 0) return *this;
1364 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1365 assert(Align <= 0x100 && "Alignment too large.");
1367 Attrs[Attribute::StackAlignment] = true;
1368 StackAlignment = Align;
1372 AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1373 if (Bytes == 0) return *this;
1375 Attrs[Attribute::Dereferenceable] = true;
1380 AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1384 Attrs[Attribute::DereferenceableOrNull] = true;
1385 DerefOrNullBytes = Bytes;
1389 AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
1390 const Optional<unsigned> &NumElems) {
1391 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
1394 AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
1395 // (0, 0) is our "not present" value, so we need to check for it here.
1396 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
1398 Attrs[Attribute::AllocSize] = true;
1399 // Reuse existing machinery to store this as a single 64-bit integer so we can
1400 // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
1401 AllocSizeArgs = RawArgs;
1405 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1406 // FIXME: What if both have alignments, but they don't match?!
1408 Alignment = B.Alignment;
1410 if (!StackAlignment)
1411 StackAlignment = B.StackAlignment;
1414 DerefBytes = B.DerefBytes;
1416 if (!DerefOrNullBytes)
1417 DerefOrNullBytes = B.DerefOrNullBytes;
1420 AllocSizeArgs = B.AllocSizeArgs;
1424 for (auto I : B.td_attrs())
1425 TargetDepAttrs[I.first] = I.second;
1430 AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1431 // FIXME: What if both have alignments, but they don't match?!
1435 if (B.StackAlignment)
1441 if (B.DerefOrNullBytes)
1442 DerefOrNullBytes = 0;
1444 if (B.AllocSizeArgs)
1449 for (auto I : B.td_attrs())
1450 TargetDepAttrs.erase(I.first);
1455 bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1456 // First check if any of the target independent attributes overlap.
1457 if ((Attrs & B.Attrs).any())
1460 // Then check if any target dependent ones do.
1461 for (auto I : td_attrs())
1462 if (B.contains(I.first))
1468 bool AttrBuilder::contains(StringRef A) const {
1469 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1472 bool AttrBuilder::hasAttributes() const {
1473 return !Attrs.none() || !TargetDepAttrs.empty();
1476 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
1477 unsigned Slot = ~0U;
1478 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1479 if (A.getSlotIndex(I) == Index) {
1484 assert(Slot != ~0U && "Couldn't find the index!");
1486 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
1487 Attribute Attr = *I;
1488 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1489 if (Attrs[I->getKindAsEnum()])
1492 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1493 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1500 bool AttrBuilder::hasAlignmentAttr() const {
1501 return Alignment != 0;
1504 bool AttrBuilder::operator==(const AttrBuilder &B) {
1505 if (Attrs != B.Attrs)
1508 for (td_const_iterator I = TargetDepAttrs.begin(),
1509 E = TargetDepAttrs.end(); I != E; ++I)
1510 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1513 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1514 DerefBytes == B.DerefBytes;
1517 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
1518 // FIXME: Remove this in 4.0.
1519 if (!Val) return *this;
1521 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1522 I = Attribute::AttrKind(I + 1)) {
1523 if (I == Attribute::Dereferenceable ||
1524 I == Attribute::DereferenceableOrNull ||
1525 I == Attribute::ArgMemOnly ||
1526 I == Attribute::AllocSize)
1528 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1531 if (I == Attribute::Alignment)
1532 Alignment = 1ULL << ((A >> 16) - 1);
1533 else if (I == Attribute::StackAlignment)
1534 StackAlignment = 1ULL << ((A >> 26)-1);
1541 //===----------------------------------------------------------------------===//
1542 // AttributeFuncs Function Defintions
1543 //===----------------------------------------------------------------------===//
1545 /// \brief Which attributes cannot be applied to a type.
1546 AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
1547 AttrBuilder Incompatible;
1549 if (!Ty->isIntegerTy())
1550 // Attribute that only apply to integers.
1551 Incompatible.addAttribute(Attribute::SExt)
1552 .addAttribute(Attribute::ZExt);
1554 if (!Ty->isPointerTy())
1555 // Attribute that only apply to pointers.
1556 Incompatible.addAttribute(Attribute::ByVal)
1557 .addAttribute(Attribute::Nest)
1558 .addAttribute(Attribute::NoAlias)
1559 .addAttribute(Attribute::NoCapture)
1560 .addAttribute(Attribute::NonNull)
1561 .addDereferenceableAttr(1) // the int here is ignored
1562 .addDereferenceableOrNullAttr(1) // the int here is ignored
1563 .addAttribute(Attribute::ReadNone)
1564 .addAttribute(Attribute::ReadOnly)
1565 .addAttribute(Attribute::StructRet)
1566 .addAttribute(Attribute::InAlloca);
1568 return Incompatible;
1571 template<typename AttrClass>
1572 static bool isEqual(const Function &Caller, const Function &Callee) {
1573 return Caller.getFnAttribute(AttrClass::getKind()) ==
1574 Callee.getFnAttribute(AttrClass::getKind());
1577 /// \brief Compute the logical AND of the attributes of the caller and the
1580 /// This function sets the caller's attribute to false if the callee's attribute
1582 template<typename AttrClass>
1583 static void setAND(Function &Caller, const Function &Callee) {
1584 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1585 !AttrClass::isSet(Callee, AttrClass::getKind()))
1586 AttrClass::set(Caller, AttrClass::getKind(), false);
1589 /// \brief Compute the logical OR of the attributes of the caller and the
1592 /// This function sets the caller's attribute to true if the callee's attribute
1594 template<typename AttrClass>
1595 static void setOR(Function &Caller, const Function &Callee) {
1596 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1597 AttrClass::isSet(Callee, AttrClass::getKind()))
1598 AttrClass::set(Caller, AttrClass::getKind(), true);
1601 /// \brief If the inlined function had a higher stack protection level than the
1602 /// calling function, then bump up the caller's stack protection level.
1603 static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1604 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1605 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1606 // clutter to the IR.
1608 B.addAttribute(Attribute::StackProtect)
1609 .addAttribute(Attribute::StackProtectStrong)
1610 .addAttribute(Attribute::StackProtectReq);
1611 AttributeSet OldSSPAttr = AttributeSet::get(Caller.getContext(),
1612 AttributeSet::FunctionIndex,
1615 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
1616 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
1617 Caller.addFnAttr(Attribute::StackProtectReq);
1618 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
1619 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
1620 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
1621 Caller.addFnAttr(Attribute::StackProtectStrong);
1622 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
1623 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1624 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1625 Caller.addFnAttr(Attribute::StackProtect);
1628 #define GET_ATTR_COMPAT_FUNC
1629 #include "AttributesCompatFunc.inc"
1631 bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1632 const Function &Callee) {
1633 return hasCompatibleFnAttrs(Caller, Callee);
1637 void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1638 const Function &Callee) {
1639 mergeFnAttrs(Caller, Callee);