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 "AttributeImpl.h"
18 #include "LLVMContextImpl.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/IR/Type.h"
22 #include "llvm/Support/Atomic.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ManagedStatic.h"
25 #include "llvm/Support/Mutex.h"
26 #include "llvm/Support/raw_ostream.h"
30 //===----------------------------------------------------------------------===//
31 // Attribute Construction Methods
32 //===----------------------------------------------------------------------===//
34 Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
36 LLVMContextImpl *pImpl = Context.pImpl;
39 if (Val) ID.AddInteger(Val);
42 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
45 // If we didn't find any existing attributes of the same shape then create a
46 // new one and insert it.
48 PA = new EnumAttributeImpl(Kind);
50 PA = new IntAttributeImpl(Kind, Val);
51 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
54 // Return the Attribute that we found or created.
58 Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
59 LLVMContextImpl *pImpl = Context.pImpl;
62 if (!Val.empty()) ID.AddString(Val);
65 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
68 // If we didn't find any existing attributes of the same shape then create a
69 // new one and insert it.
70 PA = new StringAttributeImpl(Kind, Val);
71 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
74 // Return the Attribute that we found or created.
78 Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
79 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
80 assert(Align <= 0x40000000 && "Alignment too large.");
81 return get(Context, Alignment, Align);
84 Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
86 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
87 assert(Align <= 0x100 && "Alignment too large.");
88 return get(Context, StackAlignment, Align);
91 Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context,
93 assert(Bytes && "Bytes must be non-zero.");
94 return get(Context, Dereferenceable, Bytes);
97 Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context,
99 assert(Bytes && "Bytes must be non-zero.");
100 return get(Context, DereferenceableOrNull, Bytes);
103 //===----------------------------------------------------------------------===//
104 // Attribute Accessor Methods
105 //===----------------------------------------------------------------------===//
107 bool Attribute::isEnumAttribute() const {
108 return pImpl && pImpl->isEnumAttribute();
111 bool Attribute::isIntAttribute() const {
112 return pImpl && pImpl->isIntAttribute();
115 bool Attribute::isStringAttribute() const {
116 return pImpl && pImpl->isStringAttribute();
119 Attribute::AttrKind Attribute::getKindAsEnum() const {
120 if (!pImpl) return None;
121 assert((isEnumAttribute() || isIntAttribute()) &&
122 "Invalid attribute type to get the kind as an enum!");
123 return pImpl->getKindAsEnum();
126 uint64_t Attribute::getValueAsInt() const {
127 if (!pImpl) return 0;
128 assert(isIntAttribute() &&
129 "Expected the attribute to be an integer attribute!");
130 return pImpl->getValueAsInt();
133 StringRef Attribute::getKindAsString() const {
134 if (!pImpl) return StringRef();
135 assert(isStringAttribute() &&
136 "Invalid attribute type to get the kind as a string!");
137 return pImpl->getKindAsString();
140 StringRef Attribute::getValueAsString() const {
141 if (!pImpl) return StringRef();
142 assert(isStringAttribute() &&
143 "Invalid attribute type to get the value as a string!");
144 return pImpl->getValueAsString();
147 bool Attribute::hasAttribute(AttrKind Kind) const {
148 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
151 bool Attribute::hasAttribute(StringRef Kind) const {
152 if (!isStringAttribute()) return false;
153 return pImpl && pImpl->hasAttribute(Kind);
156 /// This returns the alignment field of an attribute as a byte alignment value.
157 unsigned Attribute::getAlignment() const {
158 assert(hasAttribute(Attribute::Alignment) &&
159 "Trying to get alignment from non-alignment attribute!");
160 return pImpl->getValueAsInt();
163 /// This returns the stack alignment field of an attribute as a byte alignment
165 unsigned Attribute::getStackAlignment() const {
166 assert(hasAttribute(Attribute::StackAlignment) &&
167 "Trying to get alignment from non-alignment attribute!");
168 return pImpl->getValueAsInt();
171 /// This returns the number of dereferenceable bytes.
172 uint64_t Attribute::getDereferenceableBytes() const {
173 assert(hasAttribute(Attribute::Dereferenceable) &&
174 "Trying to get dereferenceable bytes from "
175 "non-dereferenceable attribute!");
176 return pImpl->getValueAsInt();
179 uint64_t Attribute::getDereferenceableOrNullBytes() const {
180 assert(hasAttribute(Attribute::DereferenceableOrNull) &&
181 "Trying to get dereferenceable bytes from "
182 "non-dereferenceable attribute!");
183 return pImpl->getValueAsInt();
186 std::string Attribute::getAsString(bool InAttrGrp) const {
187 if (!pImpl) return "";
189 if (hasAttribute(Attribute::SanitizeAddress))
190 return "sanitize_address";
191 if (hasAttribute(Attribute::AlwaysInline))
192 return "alwaysinline";
193 if (hasAttribute(Attribute::ArgMemOnly))
195 if (hasAttribute(Attribute::Builtin))
197 if (hasAttribute(Attribute::ByVal))
199 if (hasAttribute(Attribute::Convergent))
201 if (hasAttribute(Attribute::InaccessibleMemOnly))
202 return "inaccessiblememonly";
203 if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly))
204 return "inaccessiblemem_or_argmemonly";
205 if (hasAttribute(Attribute::InAlloca))
207 if (hasAttribute(Attribute::InlineHint))
209 if (hasAttribute(Attribute::InReg))
211 if (hasAttribute(Attribute::JumpTable))
213 if (hasAttribute(Attribute::MinSize))
215 if (hasAttribute(Attribute::Naked))
217 if (hasAttribute(Attribute::Nest))
219 if (hasAttribute(Attribute::NoAlias))
221 if (hasAttribute(Attribute::NoBuiltin))
223 if (hasAttribute(Attribute::NoCapture))
225 if (hasAttribute(Attribute::NoDuplicate))
226 return "noduplicate";
227 if (hasAttribute(Attribute::NoImplicitFloat))
228 return "noimplicitfloat";
229 if (hasAttribute(Attribute::NoInline))
231 if (hasAttribute(Attribute::NonLazyBind))
232 return "nonlazybind";
233 if (hasAttribute(Attribute::NonNull))
235 if (hasAttribute(Attribute::NoRedZone))
237 if (hasAttribute(Attribute::NoReturn))
239 if (hasAttribute(Attribute::NoRecurse))
241 if (hasAttribute(Attribute::NoUnwind))
243 if (hasAttribute(Attribute::OptimizeNone))
245 if (hasAttribute(Attribute::OptimizeForSize))
247 if (hasAttribute(Attribute::ReadNone))
249 if (hasAttribute(Attribute::ReadOnly))
251 if (hasAttribute(Attribute::Returned))
253 if (hasAttribute(Attribute::ReturnsTwice))
254 return "returns_twice";
255 if (hasAttribute(Attribute::SExt))
257 if (hasAttribute(Attribute::StackProtect))
259 if (hasAttribute(Attribute::StackProtectReq))
261 if (hasAttribute(Attribute::StackProtectStrong))
263 if (hasAttribute(Attribute::SafeStack))
265 if (hasAttribute(Attribute::StructRet))
267 if (hasAttribute(Attribute::SanitizeThread))
268 return "sanitize_thread";
269 if (hasAttribute(Attribute::SanitizeMemory))
270 return "sanitize_memory";
271 if (hasAttribute(Attribute::UWTable))
273 if (hasAttribute(Attribute::ZExt))
275 if (hasAttribute(Attribute::Cold))
278 // FIXME: These should be output like this:
283 if (hasAttribute(Attribute::Alignment)) {
286 Result += (InAttrGrp) ? "=" : " ";
287 Result += utostr(getValueAsInt());
291 auto AttrWithBytesToString = [&](const char *Name) {
296 Result += utostr(getValueAsInt());
299 Result += utostr(getValueAsInt());
305 if (hasAttribute(Attribute::StackAlignment))
306 return AttrWithBytesToString("alignstack");
308 if (hasAttribute(Attribute::Dereferenceable))
309 return AttrWithBytesToString("dereferenceable");
311 if (hasAttribute(Attribute::DereferenceableOrNull))
312 return AttrWithBytesToString("dereferenceable_or_null");
314 // Convert target-dependent attributes to strings of the form:
319 if (isStringAttribute()) {
321 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
323 StringRef Val = pImpl->getValueAsString();
324 if (Val.empty()) return Result;
326 Result += ("=\"" + Val + Twine('"')).str();
330 llvm_unreachable("Unknown attribute");
333 bool Attribute::operator<(Attribute A) const {
334 if (!pImpl && !A.pImpl) return false;
335 if (!pImpl) return true;
336 if (!A.pImpl) return false;
337 return *pImpl < *A.pImpl;
340 //===----------------------------------------------------------------------===//
341 // AttributeImpl Definition
342 //===----------------------------------------------------------------------===//
344 // Pin the vtables to this file.
345 AttributeImpl::~AttributeImpl() {}
346 void EnumAttributeImpl::anchor() {}
347 void IntAttributeImpl::anchor() {}
348 void StringAttributeImpl::anchor() {}
350 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
351 if (isStringAttribute()) return false;
352 return getKindAsEnum() == A;
355 bool AttributeImpl::hasAttribute(StringRef Kind) const {
356 if (!isStringAttribute()) return false;
357 return getKindAsString() == Kind;
360 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
361 assert(isEnumAttribute() || isIntAttribute());
362 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
365 uint64_t AttributeImpl::getValueAsInt() const {
366 assert(isIntAttribute());
367 return static_cast<const IntAttributeImpl *>(this)->getValue();
370 StringRef AttributeImpl::getKindAsString() const {
371 assert(isStringAttribute());
372 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
375 StringRef AttributeImpl::getValueAsString() const {
376 assert(isStringAttribute());
377 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
380 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
381 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
382 // relative to their enum value) and then strings.
383 if (isEnumAttribute()) {
384 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
385 if (AI.isIntAttribute()) return true;
386 if (AI.isStringAttribute()) return true;
389 if (isIntAttribute()) {
390 if (AI.isEnumAttribute()) return false;
391 if (AI.isIntAttribute()) return getValueAsInt() < AI.getValueAsInt();
392 if (AI.isStringAttribute()) return true;
395 if (AI.isEnumAttribute()) return false;
396 if (AI.isIntAttribute()) return false;
397 if (getKindAsString() == AI.getKindAsString())
398 return getValueAsString() < AI.getValueAsString();
399 return getKindAsString() < AI.getKindAsString();
402 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
403 // FIXME: Remove this.
405 case Attribute::EndAttrKinds:
406 llvm_unreachable("Synthetic enumerators which should never get here");
408 case Attribute::None: return 0;
409 case Attribute::ZExt: return 1 << 0;
410 case Attribute::SExt: return 1 << 1;
411 case Attribute::NoReturn: return 1 << 2;
412 case Attribute::InReg: return 1 << 3;
413 case Attribute::StructRet: return 1 << 4;
414 case Attribute::NoUnwind: return 1 << 5;
415 case Attribute::NoAlias: return 1 << 6;
416 case Attribute::ByVal: return 1 << 7;
417 case Attribute::Nest: return 1 << 8;
418 case Attribute::ReadNone: return 1 << 9;
419 case Attribute::ReadOnly: return 1 << 10;
420 case Attribute::NoInline: return 1 << 11;
421 case Attribute::AlwaysInline: return 1 << 12;
422 case Attribute::OptimizeForSize: return 1 << 13;
423 case Attribute::StackProtect: return 1 << 14;
424 case Attribute::StackProtectReq: return 1 << 15;
425 case Attribute::Alignment: return 31 << 16;
426 case Attribute::NoCapture: return 1 << 21;
427 case Attribute::NoRedZone: return 1 << 22;
428 case Attribute::NoImplicitFloat: return 1 << 23;
429 case Attribute::Naked: return 1 << 24;
430 case Attribute::InlineHint: return 1 << 25;
431 case Attribute::StackAlignment: return 7 << 26;
432 case Attribute::ReturnsTwice: return 1 << 29;
433 case Attribute::UWTable: return 1 << 30;
434 case Attribute::NonLazyBind: return 1U << 31;
435 case Attribute::SanitizeAddress: return 1ULL << 32;
436 case Attribute::MinSize: return 1ULL << 33;
437 case Attribute::NoDuplicate: return 1ULL << 34;
438 case Attribute::StackProtectStrong: return 1ULL << 35;
439 case Attribute::SanitizeThread: return 1ULL << 36;
440 case Attribute::SanitizeMemory: return 1ULL << 37;
441 case Attribute::NoBuiltin: return 1ULL << 38;
442 case Attribute::Returned: return 1ULL << 39;
443 case Attribute::Cold: return 1ULL << 40;
444 case Attribute::Builtin: return 1ULL << 41;
445 case Attribute::OptimizeNone: return 1ULL << 42;
446 case Attribute::InAlloca: return 1ULL << 43;
447 case Attribute::NonNull: return 1ULL << 44;
448 case Attribute::JumpTable: return 1ULL << 45;
449 case Attribute::Convergent: return 1ULL << 46;
450 case Attribute::SafeStack: return 1ULL << 47;
451 case Attribute::NoRecurse: return 1ULL << 48;
452 case Attribute::InaccessibleMemOnly: return 1ULL << 49;
453 case Attribute::InaccessibleMemOrArgMemOnly: return 1ULL << 50;
454 case Attribute::Dereferenceable:
455 llvm_unreachable("dereferenceable attribute not supported in raw format");
457 case Attribute::DereferenceableOrNull:
458 llvm_unreachable("dereferenceable_or_null attribute not supported in raw "
461 case Attribute::ArgMemOnly:
462 llvm_unreachable("argmemonly attribute not supported in raw format");
465 llvm_unreachable("Unsupported attribute type");
468 //===----------------------------------------------------------------------===//
469 // AttributeSetNode Definition
470 //===----------------------------------------------------------------------===//
472 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
473 ArrayRef<Attribute> Attrs) {
477 // Otherwise, build a key to look up the existing attributes.
478 LLVMContextImpl *pImpl = C.pImpl;
481 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
482 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
484 for (Attribute Attr : SortedAttrs)
488 AttributeSetNode *PA =
489 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
491 // If we didn't find any existing attributes of the same shape then create a
492 // new one and insert it.
494 // Coallocate entries after the AttributeSetNode itself.
495 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
496 PA = new (Mem) AttributeSetNode(SortedAttrs);
497 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
500 // Return the AttributesListNode that we found or created.
504 bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
505 for (iterator I = begin(), E = end(); I != E; ++I)
506 if (I->hasAttribute(Kind))
511 bool AttributeSetNode::hasAttribute(StringRef Kind) const {
512 for (iterator I = begin(), E = end(); I != E; ++I)
513 if (I->hasAttribute(Kind))
518 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
519 for (iterator I = begin(), E = end(); I != E; ++I)
520 if (I->hasAttribute(Kind))
525 Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
526 for (iterator I = begin(), E = end(); I != E; ++I)
527 if (I->hasAttribute(Kind))
532 unsigned AttributeSetNode::getAlignment() const {
533 for (iterator I = begin(), E = end(); I != E; ++I)
534 if (I->hasAttribute(Attribute::Alignment))
535 return I->getAlignment();
539 unsigned AttributeSetNode::getStackAlignment() const {
540 for (iterator I = begin(), E = end(); I != E; ++I)
541 if (I->hasAttribute(Attribute::StackAlignment))
542 return I->getStackAlignment();
546 uint64_t AttributeSetNode::getDereferenceableBytes() const {
547 for (iterator I = begin(), E = end(); I != E; ++I)
548 if (I->hasAttribute(Attribute::Dereferenceable))
549 return I->getDereferenceableBytes();
553 uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
554 for (iterator I = begin(), E = end(); I != E; ++I)
555 if (I->hasAttribute(Attribute::DereferenceableOrNull))
556 return I->getDereferenceableOrNullBytes();
560 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
562 for (iterator I = begin(), E = end(); I != E; ++I) {
565 Str += I->getAsString(InAttrGrp);
570 //===----------------------------------------------------------------------===//
571 // AttributeSetImpl Definition
572 //===----------------------------------------------------------------------===//
574 uint64_t AttributeSetImpl::Raw(unsigned Index) const {
575 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
576 if (getSlotIndex(I) != Index) continue;
577 const AttributeSetNode *ASN = getSlotNode(I);
580 for (AttributeSetNode::iterator II = ASN->begin(),
581 IE = ASN->end(); II != IE; ++II) {
582 Attribute Attr = *II;
584 // This cannot handle string attributes.
585 if (Attr.isStringAttribute()) continue;
587 Attribute::AttrKind Kind = Attr.getKindAsEnum();
589 if (Kind == Attribute::Alignment)
590 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
591 else if (Kind == Attribute::StackAlignment)
592 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
593 else if (Kind == Attribute::Dereferenceable)
594 llvm_unreachable("dereferenceable not supported in bit mask");
596 Mask |= AttributeImpl::getAttrMask(Kind);
605 void AttributeSetImpl::dump() const {
606 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
609 //===----------------------------------------------------------------------===//
610 // AttributeSet Construction and Mutation Methods
611 //===----------------------------------------------------------------------===//
614 AttributeSet::getImpl(LLVMContext &C,
615 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
616 LLVMContextImpl *pImpl = C.pImpl;
618 AttributeSetImpl::Profile(ID, Attrs);
621 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
623 // If we didn't find any existing attributes of the same shape then
624 // create a new one and insert it.
626 // Coallocate entries after the AttributeSetImpl itself.
627 void *Mem = ::operator new(
628 AttributeSetImpl::totalSizeToAlloc<IndexAttrPair>(Attrs.size()));
629 PA = new (Mem) AttributeSetImpl(C, Attrs);
630 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
633 // Return the AttributesList that we found or created.
634 return AttributeSet(PA);
637 AttributeSet AttributeSet::get(LLVMContext &C,
638 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
639 // If there are no attributes then return a null AttributesList pointer.
641 return AttributeSet();
644 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
645 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
646 "Misordered Attributes list!");
647 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
648 "Pointless attribute!");
652 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
654 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
655 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
656 E = Attrs.end(); I != E; ) {
657 unsigned Index = I->first;
658 SmallVector<Attribute, 4> AttrVec;
659 while (I != E && I->first == Index) {
660 AttrVec.push_back(I->second);
664 AttrPairVec.push_back(std::make_pair(Index,
665 AttributeSetNode::get(C, AttrVec)));
668 return getImpl(C, AttrPairVec);
671 AttributeSet AttributeSet::get(LLVMContext &C,
672 ArrayRef<std::pair<unsigned,
673 AttributeSetNode*> > Attrs) {
674 // If there are no attributes then return a null AttributesList pointer.
676 return AttributeSet();
678 return getImpl(C, Attrs);
681 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
682 const AttrBuilder &B) {
683 if (!B.hasAttributes())
684 return AttributeSet();
686 // Add target-independent attributes.
687 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
688 for (Attribute::AttrKind Kind = Attribute::None;
689 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
690 if (!B.contains(Kind))
695 case Attribute::Alignment:
696 Attr = Attribute::getWithAlignment(C, B.getAlignment());
698 case Attribute::StackAlignment:
699 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
701 case Attribute::Dereferenceable:
702 Attr = Attribute::getWithDereferenceableBytes(
703 C, B.getDereferenceableBytes());
705 case Attribute::DereferenceableOrNull:
706 Attr = Attribute::getWithDereferenceableOrNullBytes(
707 C, B.getDereferenceableOrNullBytes());
710 Attr = Attribute::get(C, Kind);
712 Attrs.push_back(std::make_pair(Index, Attr));
715 // Add target-dependent (string) attributes.
716 for (const AttrBuilder::td_type &TDA : B.td_attrs())
718 std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
720 return get(C, Attrs);
723 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
724 ArrayRef<Attribute::AttrKind> Kind) {
725 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
726 for (Attribute::AttrKind K : Kind)
727 Attrs.push_back(std::make_pair(Index, Attribute::get(C, K)));
728 return get(C, Attrs);
731 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
732 if (Attrs.empty()) return AttributeSet();
733 if (Attrs.size() == 1) return Attrs[0];
735 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
736 AttributeSetImpl *A0 = Attrs[0].pImpl;
738 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
739 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
740 // ordered by index. Because we know that each list in Attrs is ordered by
741 // index we only need to merge each successive list in rather than doing a
743 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
744 AttributeSetImpl *AS = Attrs[I].pImpl;
746 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
747 ANVI = AttrNodeVec.begin(), ANVE;
748 for (const IndexAttrPair *AI = AS->getNode(0),
749 *AE = AS->getNode(AS->getNumAttributes());
751 ANVE = AttrNodeVec.end();
752 while (ANVI != ANVE && ANVI->first <= AI->first)
754 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
758 return getImpl(C, AttrNodeVec);
761 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
762 Attribute::AttrKind Attr) const {
763 if (hasAttribute(Index, Attr)) return *this;
764 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
767 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
768 StringRef Kind) const {
770 B.addAttribute(Kind);
771 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
774 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
775 StringRef Kind, StringRef Value) const {
777 B.addAttribute(Kind, Value);
778 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
781 AttributeSet AttributeSet::addAttribute(LLVMContext &C,
782 ArrayRef<unsigned> Indices,
784 unsigned I = 0, E = pImpl ? pImpl->getNumAttributes() : 0;
785 auto IdxI = Indices.begin(), IdxE = Indices.end();
786 SmallVector<AttributeSet, 4> AttrSet;
788 while (I != E && IdxI != IdxE) {
789 if (getSlotIndex(I) < *IdxI)
790 AttrSet.emplace_back(getSlotAttributes(I++));
791 else if (getSlotIndex(I) > *IdxI)
792 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
794 AttrBuilder B(getSlotAttributes(I), *IdxI);
796 AttrSet.emplace_back(AttributeSet::get(C, *IdxI, B));
803 AttrSet.emplace_back(getSlotAttributes(I++));
806 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
808 return get(C, AttrSet);
811 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
812 AttributeSet Attrs) const {
813 if (!pImpl) return Attrs;
814 if (!Attrs.pImpl) return *this;
817 // FIXME it is not obvious how this should work for alignment. For now, say
818 // we can't change a known alignment.
819 unsigned OldAlign = getParamAlignment(Index);
820 unsigned NewAlign = Attrs.getParamAlignment(Index);
821 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
822 "Attempt to change alignment!");
825 // Add the attribute slots before the one we're trying to add.
826 SmallVector<AttributeSet, 4> AttrSet;
827 uint64_t NumAttrs = pImpl->getNumAttributes();
829 uint64_t LastIndex = 0;
830 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
831 if (getSlotIndex(I) >= Index) {
832 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
836 AttrSet.push_back(getSlotAttributes(I));
839 // Now add the attribute into the correct slot. There may already be an
840 // AttributeSet there.
841 AttrBuilder B(AS, Index);
843 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
844 if (Attrs.getSlotIndex(I) == Index) {
845 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
846 IE = Attrs.pImpl->end(I); II != IE; ++II)
851 AttrSet.push_back(AttributeSet::get(C, Index, B));
853 // Add the remaining attribute slots.
854 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
855 AttrSet.push_back(getSlotAttributes(I));
857 return get(C, AttrSet);
860 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
861 Attribute::AttrKind Attr) const {
862 if (!hasAttribute(Index, Attr)) return *this;
863 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
866 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
867 AttributeSet Attrs) const {
868 if (!pImpl) return AttributeSet();
869 if (!Attrs.pImpl) return *this;
871 // FIXME it is not obvious how this should work for alignment.
872 // For now, say we can't pass in alignment, which no current use does.
873 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
874 "Attempt to change alignment!");
876 // Add the attribute slots before the one we're trying to add.
877 SmallVector<AttributeSet, 4> AttrSet;
878 uint64_t NumAttrs = pImpl->getNumAttributes();
880 uint64_t LastIndex = 0;
881 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
882 if (getSlotIndex(I) >= Index) {
883 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
887 AttrSet.push_back(getSlotAttributes(I));
890 // Now remove the attribute from the correct slot. There may already be an
891 // AttributeSet there.
892 AttrBuilder B(AS, Index);
894 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
895 if (Attrs.getSlotIndex(I) == Index) {
896 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
900 AttrSet.push_back(AttributeSet::get(C, Index, B));
902 // Add the remaining attribute slots.
903 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
904 AttrSet.push_back(getSlotAttributes(I));
906 return get(C, AttrSet);
909 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
910 const AttrBuilder &Attrs) const {
911 if (!pImpl) return AttributeSet();
913 // FIXME it is not obvious how this should work for alignment.
914 // For now, say we can't pass in alignment, which no current use does.
915 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
917 // Add the attribute slots before the one we're trying to add.
918 SmallVector<AttributeSet, 4> AttrSet;
919 uint64_t NumAttrs = pImpl->getNumAttributes();
921 uint64_t LastIndex = 0;
922 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
923 if (getSlotIndex(I) >= Index) {
924 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
928 AttrSet.push_back(getSlotAttributes(I));
931 // Now remove the attribute from the correct slot. There may already be an
932 // AttributeSet there.
933 AttrBuilder B(AS, Index);
936 AttrSet.push_back(AttributeSet::get(C, Index, B));
938 // Add the remaining attribute slots.
939 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
940 AttrSet.push_back(getSlotAttributes(I));
942 return get(C, AttrSet);
945 AttributeSet AttributeSet::addDereferenceableAttr(LLVMContext &C, unsigned Index,
946 uint64_t Bytes) const {
948 B.addDereferenceableAttr(Bytes);
949 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
952 AttributeSet AttributeSet::addDereferenceableOrNullAttr(LLVMContext &C,
954 uint64_t Bytes) const {
956 B.addDereferenceableOrNullAttr(Bytes);
957 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
960 //===----------------------------------------------------------------------===//
961 // AttributeSet Accessor Methods
962 //===----------------------------------------------------------------------===//
964 LLVMContext &AttributeSet::getContext() const {
965 return pImpl->getContext();
968 AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
969 return pImpl && hasAttributes(Index) ?
970 AttributeSet::get(pImpl->getContext(),
971 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
972 std::make_pair(Index, getAttributes(Index)))) :
976 AttributeSet AttributeSet::getRetAttributes() const {
977 return pImpl && hasAttributes(ReturnIndex) ?
978 AttributeSet::get(pImpl->getContext(),
979 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
980 std::make_pair(ReturnIndex,
981 getAttributes(ReturnIndex)))) :
985 AttributeSet AttributeSet::getFnAttributes() const {
986 return pImpl && hasAttributes(FunctionIndex) ?
987 AttributeSet::get(pImpl->getContext(),
988 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
989 std::make_pair(FunctionIndex,
990 getAttributes(FunctionIndex)))) :
994 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
995 AttributeSetNode *ASN = getAttributes(Index);
996 return ASN && ASN->hasAttribute(Kind);
999 bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
1000 AttributeSetNode *ASN = getAttributes(Index);
1001 return ASN && ASN->hasAttribute(Kind);
1004 bool AttributeSet::hasAttributes(unsigned Index) const {
1005 AttributeSetNode *ASN = getAttributes(Index);
1006 return ASN && ASN->hasAttributes();
1009 /// \brief Return true if the specified attribute is set for at least one
1010 /// parameter or for the return value.
1011 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
1012 if (!pImpl) return false;
1014 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
1015 for (AttributeSetImpl::iterator II = pImpl->begin(I),
1016 IE = pImpl->end(I); II != IE; ++II)
1017 if (II->hasAttribute(Attr))
1023 Attribute AttributeSet::getAttribute(unsigned Index,
1024 Attribute::AttrKind Kind) const {
1025 AttributeSetNode *ASN = getAttributes(Index);
1026 return ASN ? ASN->getAttribute(Kind) : Attribute();
1029 Attribute AttributeSet::getAttribute(unsigned Index,
1030 StringRef Kind) const {
1031 AttributeSetNode *ASN = getAttributes(Index);
1032 return ASN ? ASN->getAttribute(Kind) : Attribute();
1035 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
1036 AttributeSetNode *ASN = getAttributes(Index);
1037 return ASN ? ASN->getAlignment() : 0;
1040 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
1041 AttributeSetNode *ASN = getAttributes(Index);
1042 return ASN ? ASN->getStackAlignment() : 0;
1045 uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
1046 AttributeSetNode *ASN = getAttributes(Index);
1047 return ASN ? ASN->getDereferenceableBytes() : 0;
1050 uint64_t AttributeSet::getDereferenceableOrNullBytes(unsigned Index) const {
1051 AttributeSetNode *ASN = getAttributes(Index);
1052 return ASN ? ASN->getDereferenceableOrNullBytes() : 0;
1055 std::string AttributeSet::getAsString(unsigned Index,
1056 bool InAttrGrp) const {
1057 AttributeSetNode *ASN = getAttributes(Index);
1058 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
1061 /// \brief The attributes for the specified index are returned.
1062 AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
1063 if (!pImpl) return nullptr;
1065 // Loop through to find the attribute node we want.
1066 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
1067 if (pImpl->getSlotIndex(I) == Index)
1068 return pImpl->getSlotNode(I);
1073 AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
1075 return ArrayRef<Attribute>().begin();
1076 return pImpl->begin(Slot);
1079 AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
1081 return ArrayRef<Attribute>().end();
1082 return pImpl->end(Slot);
1085 //===----------------------------------------------------------------------===//
1086 // AttributeSet Introspection Methods
1087 //===----------------------------------------------------------------------===//
1089 /// \brief Return the number of slots used in this attribute list. This is the
1090 /// number of arguments that have an attribute set on them (including the
1091 /// function itself).
1092 unsigned AttributeSet::getNumSlots() const {
1093 return pImpl ? pImpl->getNumAttributes() : 0;
1096 unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
1097 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1098 "Slot # out of range!");
1099 return pImpl->getSlotIndex(Slot);
1102 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
1103 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1104 "Slot # out of range!");
1105 return pImpl->getSlotAttributes(Slot);
1108 uint64_t AttributeSet::Raw(unsigned Index) const {
1109 // FIXME: Remove this.
1110 return pImpl ? pImpl->Raw(Index) : 0;
1113 void AttributeSet::dump() const {
1116 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1117 uint64_t Index = getSlotIndex(i);
1123 dbgs() << " => " << getAsString(Index) << " }\n";
1129 //===----------------------------------------------------------------------===//
1130 // AttrBuilder Method Implementations
1131 //===----------------------------------------------------------------------===//
1133 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
1134 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
1135 DerefOrNullBytes(0) {
1136 AttributeSetImpl *pImpl = AS.pImpl;
1139 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
1140 if (pImpl->getSlotIndex(I) != Index) continue;
1142 for (AttributeSetImpl::iterator II = pImpl->begin(I),
1143 IE = pImpl->end(I); II != IE; ++II)
1150 void AttrBuilder::clear() {
1152 TargetDepAttrs.clear();
1153 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
1156 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
1157 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1158 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
1159 Val != Attribute::Dereferenceable &&
1160 "Adding integer attribute without adding a value!");
1165 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
1166 if (Attr.isStringAttribute()) {
1167 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1171 Attribute::AttrKind Kind = Attr.getKindAsEnum();
1174 if (Kind == Attribute::Alignment)
1175 Alignment = Attr.getAlignment();
1176 else if (Kind == Attribute::StackAlignment)
1177 StackAlignment = Attr.getStackAlignment();
1178 else if (Kind == Attribute::Dereferenceable)
1179 DerefBytes = Attr.getDereferenceableBytes();
1180 else if (Kind == Attribute::DereferenceableOrNull)
1181 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
1185 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1186 TargetDepAttrs[A] = V;
1190 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
1191 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1194 if (Val == Attribute::Alignment)
1196 else if (Val == Attribute::StackAlignment)
1198 else if (Val == Attribute::Dereferenceable)
1200 else if (Val == Attribute::DereferenceableOrNull)
1201 DerefOrNullBytes = 0;
1206 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
1207 unsigned Slot = ~0U;
1208 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1209 if (A.getSlotIndex(I) == Index) {
1214 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
1216 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
1217 Attribute Attr = *I;
1218 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1219 removeAttribute(Attr.getKindAsEnum());
1221 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1222 removeAttribute(Attr.getKindAsString());
1229 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1230 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1231 if (I != TargetDepAttrs.end())
1232 TargetDepAttrs.erase(I);
1236 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
1237 if (Align == 0) return *this;
1239 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1240 assert(Align <= 0x40000000 && "Alignment too large.");
1242 Attrs[Attribute::Alignment] = true;
1247 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1248 // Default alignment, allow the target to define how to align it.
1249 if (Align == 0) return *this;
1251 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1252 assert(Align <= 0x100 && "Alignment too large.");
1254 Attrs[Attribute::StackAlignment] = true;
1255 StackAlignment = Align;
1259 AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1260 if (Bytes == 0) return *this;
1262 Attrs[Attribute::Dereferenceable] = true;
1267 AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1271 Attrs[Attribute::DereferenceableOrNull] = true;
1272 DerefOrNullBytes = Bytes;
1276 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1277 // FIXME: What if both have alignments, but they don't match?!
1279 Alignment = B.Alignment;
1281 if (!StackAlignment)
1282 StackAlignment = B.StackAlignment;
1285 DerefBytes = B.DerefBytes;
1287 if (!DerefOrNullBytes)
1288 DerefOrNullBytes = B.DerefOrNullBytes;
1292 for (auto I : B.td_attrs())
1293 TargetDepAttrs[I.first] = I.second;
1298 AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1299 // FIXME: What if both have alignments, but they don't match?!
1303 if (B.StackAlignment)
1309 if (B.DerefOrNullBytes)
1310 DerefOrNullBytes = 0;
1314 for (auto I : B.td_attrs())
1315 TargetDepAttrs.erase(I.first);
1320 bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1321 // First check if any of the target independent attributes overlap.
1322 if ((Attrs & B.Attrs).any())
1325 // Then check if any target dependent ones do.
1326 for (auto I : td_attrs())
1327 if (B.contains(I.first))
1333 bool AttrBuilder::contains(StringRef A) const {
1334 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1337 bool AttrBuilder::hasAttributes() const {
1338 return !Attrs.none() || !TargetDepAttrs.empty();
1341 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
1342 unsigned Slot = ~0U;
1343 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1344 if (A.getSlotIndex(I) == Index) {
1349 assert(Slot != ~0U && "Couldn't find the index!");
1351 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
1352 Attribute Attr = *I;
1353 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1354 if (Attrs[I->getKindAsEnum()])
1357 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1358 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1365 bool AttrBuilder::hasAlignmentAttr() const {
1366 return Alignment != 0;
1369 bool AttrBuilder::operator==(const AttrBuilder &B) {
1370 if (Attrs != B.Attrs)
1373 for (td_const_iterator I = TargetDepAttrs.begin(),
1374 E = TargetDepAttrs.end(); I != E; ++I)
1375 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1378 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1379 DerefBytes == B.DerefBytes;
1382 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
1383 // FIXME: Remove this in 4.0.
1384 if (!Val) return *this;
1386 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1387 I = Attribute::AttrKind(I + 1)) {
1388 if (I == Attribute::Dereferenceable ||
1389 I == Attribute::DereferenceableOrNull ||
1390 I == Attribute::ArgMemOnly)
1392 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1395 if (I == Attribute::Alignment)
1396 Alignment = 1ULL << ((A >> 16) - 1);
1397 else if (I == Attribute::StackAlignment)
1398 StackAlignment = 1ULL << ((A >> 26)-1);
1405 //===----------------------------------------------------------------------===//
1406 // AttributeFuncs Function Defintions
1407 //===----------------------------------------------------------------------===//
1409 /// \brief Which attributes cannot be applied to a type.
1410 AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
1411 AttrBuilder Incompatible;
1413 if (!Ty->isIntegerTy())
1414 // Attribute that only apply to integers.
1415 Incompatible.addAttribute(Attribute::SExt)
1416 .addAttribute(Attribute::ZExt);
1418 if (!Ty->isPointerTy())
1419 // Attribute that only apply to pointers.
1420 Incompatible.addAttribute(Attribute::ByVal)
1421 .addAttribute(Attribute::Nest)
1422 .addAttribute(Attribute::NoAlias)
1423 .addAttribute(Attribute::NoCapture)
1424 .addAttribute(Attribute::NonNull)
1425 .addDereferenceableAttr(1) // the int here is ignored
1426 .addDereferenceableOrNullAttr(1) // the int here is ignored
1427 .addAttribute(Attribute::ReadNone)
1428 .addAttribute(Attribute::ReadOnly)
1429 .addAttribute(Attribute::StructRet)
1430 .addAttribute(Attribute::InAlloca);
1432 return Incompatible;