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::InAlloca))
203 if (hasAttribute(Attribute::InlineHint))
205 if (hasAttribute(Attribute::InReg))
207 if (hasAttribute(Attribute::JumpTable))
209 if (hasAttribute(Attribute::MinSize))
211 if (hasAttribute(Attribute::Naked))
213 if (hasAttribute(Attribute::Nest))
215 if (hasAttribute(Attribute::NoAlias))
217 if (hasAttribute(Attribute::NoBuiltin))
219 if (hasAttribute(Attribute::NoCapture))
221 if (hasAttribute(Attribute::NoDuplicate))
222 return "noduplicate";
223 if (hasAttribute(Attribute::NoImplicitFloat))
224 return "noimplicitfloat";
225 if (hasAttribute(Attribute::NoInline))
227 if (hasAttribute(Attribute::NonLazyBind))
228 return "nonlazybind";
229 if (hasAttribute(Attribute::NonNull))
231 if (hasAttribute(Attribute::NoRedZone))
233 if (hasAttribute(Attribute::NoReturn))
235 if (hasAttribute(Attribute::NoRecurse))
237 if (hasAttribute(Attribute::NoUnwind))
239 if (hasAttribute(Attribute::OptimizeNone))
241 if (hasAttribute(Attribute::OptimizeForSize))
243 if (hasAttribute(Attribute::ReadNone))
245 if (hasAttribute(Attribute::ReadOnly))
247 if (hasAttribute(Attribute::Returned))
249 if (hasAttribute(Attribute::ReturnsTwice))
250 return "returns_twice";
251 if (hasAttribute(Attribute::SExt))
253 if (hasAttribute(Attribute::StackProtect))
255 if (hasAttribute(Attribute::StackProtectReq))
257 if (hasAttribute(Attribute::StackProtectStrong))
259 if (hasAttribute(Attribute::SafeStack))
261 if (hasAttribute(Attribute::StructRet))
263 if (hasAttribute(Attribute::SanitizeThread))
264 return "sanitize_thread";
265 if (hasAttribute(Attribute::SanitizeMemory))
266 return "sanitize_memory";
267 if (hasAttribute(Attribute::UWTable))
269 if (hasAttribute(Attribute::ZExt))
271 if (hasAttribute(Attribute::Cold))
274 // FIXME: These should be output like this:
279 if (hasAttribute(Attribute::Alignment)) {
282 Result += (InAttrGrp) ? "=" : " ";
283 Result += utostr(getValueAsInt());
287 auto AttrWithBytesToString = [&](const char *Name) {
292 Result += utostr(getValueAsInt());
295 Result += utostr(getValueAsInt());
301 if (hasAttribute(Attribute::StackAlignment))
302 return AttrWithBytesToString("alignstack");
304 if (hasAttribute(Attribute::Dereferenceable))
305 return AttrWithBytesToString("dereferenceable");
307 if (hasAttribute(Attribute::DereferenceableOrNull))
308 return AttrWithBytesToString("dereferenceable_or_null");
310 // Convert target-dependent attributes to strings of the form:
315 if (isStringAttribute()) {
317 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
319 StringRef Val = pImpl->getValueAsString();
320 if (Val.empty()) return Result;
322 Result += ("=\"" + Val + Twine('"')).str();
326 llvm_unreachable("Unknown attribute");
329 bool Attribute::operator<(Attribute A) const {
330 if (!pImpl && !A.pImpl) return false;
331 if (!pImpl) return true;
332 if (!A.pImpl) return false;
333 return *pImpl < *A.pImpl;
336 //===----------------------------------------------------------------------===//
337 // AttributeImpl Definition
338 //===----------------------------------------------------------------------===//
340 // Pin the vtables to this file.
341 AttributeImpl::~AttributeImpl() {}
342 void EnumAttributeImpl::anchor() {}
343 void IntAttributeImpl::anchor() {}
344 void StringAttributeImpl::anchor() {}
346 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
347 if (isStringAttribute()) return false;
348 return getKindAsEnum() == A;
351 bool AttributeImpl::hasAttribute(StringRef Kind) const {
352 if (!isStringAttribute()) return false;
353 return getKindAsString() == Kind;
356 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
357 assert(isEnumAttribute() || isIntAttribute());
358 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
361 uint64_t AttributeImpl::getValueAsInt() const {
362 assert(isIntAttribute());
363 return static_cast<const IntAttributeImpl *>(this)->getValue();
366 StringRef AttributeImpl::getKindAsString() const {
367 assert(isStringAttribute());
368 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
371 StringRef AttributeImpl::getValueAsString() const {
372 assert(isStringAttribute());
373 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
376 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
377 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
378 // relative to their enum value) and then strings.
379 if (isEnumAttribute()) {
380 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
381 if (AI.isIntAttribute()) return true;
382 if (AI.isStringAttribute()) return true;
385 if (isIntAttribute()) {
386 if (AI.isEnumAttribute()) return false;
387 if (AI.isIntAttribute()) return getValueAsInt() < AI.getValueAsInt();
388 if (AI.isStringAttribute()) return true;
391 if (AI.isEnumAttribute()) return false;
392 if (AI.isIntAttribute()) return false;
393 if (getKindAsString() == AI.getKindAsString())
394 return getValueAsString() < AI.getValueAsString();
395 return getKindAsString() < AI.getKindAsString();
398 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
399 // FIXME: Remove this.
401 case Attribute::EndAttrKinds:
402 llvm_unreachable("Synthetic enumerators which should never get here");
404 case Attribute::None: return 0;
405 case Attribute::ZExt: return 1 << 0;
406 case Attribute::SExt: return 1 << 1;
407 case Attribute::NoReturn: return 1 << 2;
408 case Attribute::InReg: return 1 << 3;
409 case Attribute::StructRet: return 1 << 4;
410 case Attribute::NoUnwind: return 1 << 5;
411 case Attribute::NoAlias: return 1 << 6;
412 case Attribute::ByVal: return 1 << 7;
413 case Attribute::Nest: return 1 << 8;
414 case Attribute::ReadNone: return 1 << 9;
415 case Attribute::ReadOnly: return 1 << 10;
416 case Attribute::NoInline: return 1 << 11;
417 case Attribute::AlwaysInline: return 1 << 12;
418 case Attribute::OptimizeForSize: return 1 << 13;
419 case Attribute::StackProtect: return 1 << 14;
420 case Attribute::StackProtectReq: return 1 << 15;
421 case Attribute::Alignment: return 31 << 16;
422 case Attribute::NoCapture: return 1 << 21;
423 case Attribute::NoRedZone: return 1 << 22;
424 case Attribute::NoImplicitFloat: return 1 << 23;
425 case Attribute::Naked: return 1 << 24;
426 case Attribute::InlineHint: return 1 << 25;
427 case Attribute::StackAlignment: return 7 << 26;
428 case Attribute::ReturnsTwice: return 1 << 29;
429 case Attribute::UWTable: return 1 << 30;
430 case Attribute::NonLazyBind: return 1U << 31;
431 case Attribute::SanitizeAddress: return 1ULL << 32;
432 case Attribute::MinSize: return 1ULL << 33;
433 case Attribute::NoDuplicate: return 1ULL << 34;
434 case Attribute::StackProtectStrong: return 1ULL << 35;
435 case Attribute::SanitizeThread: return 1ULL << 36;
436 case Attribute::SanitizeMemory: return 1ULL << 37;
437 case Attribute::NoBuiltin: return 1ULL << 38;
438 case Attribute::Returned: return 1ULL << 39;
439 case Attribute::Cold: return 1ULL << 40;
440 case Attribute::Builtin: return 1ULL << 41;
441 case Attribute::OptimizeNone: return 1ULL << 42;
442 case Attribute::InAlloca: return 1ULL << 43;
443 case Attribute::NonNull: return 1ULL << 44;
444 case Attribute::JumpTable: return 1ULL << 45;
445 case Attribute::Convergent: return 1ULL << 46;
446 case Attribute::SafeStack: return 1ULL << 47;
447 case Attribute::NoRecurse: return 1ULL << 48;
448 case Attribute::Dereferenceable:
449 llvm_unreachable("dereferenceable attribute not supported in raw format");
451 case Attribute::DereferenceableOrNull:
452 llvm_unreachable("dereferenceable_or_null attribute not supported in raw "
455 case Attribute::ArgMemOnly:
456 llvm_unreachable("argmemonly attribute not supported in raw format");
459 llvm_unreachable("Unsupported attribute type");
462 //===----------------------------------------------------------------------===//
463 // AttributeSetNode Definition
464 //===----------------------------------------------------------------------===//
466 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
467 ArrayRef<Attribute> Attrs) {
471 // Otherwise, build a key to look up the existing attributes.
472 LLVMContextImpl *pImpl = C.pImpl;
475 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
476 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
478 for (Attribute Attr : SortedAttrs)
482 AttributeSetNode *PA =
483 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
485 // If we didn't find any existing attributes of the same shape then create a
486 // new one and insert it.
488 // Coallocate entries after the AttributeSetNode itself.
489 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
490 PA = new (Mem) AttributeSetNode(SortedAttrs);
491 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
494 // Return the AttributesListNode that we found or created.
498 bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
499 for (iterator I = begin(), E = end(); I != E; ++I)
500 if (I->hasAttribute(Kind))
505 bool AttributeSetNode::hasAttribute(StringRef Kind) const {
506 for (iterator I = begin(), E = end(); I != E; ++I)
507 if (I->hasAttribute(Kind))
512 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
513 for (iterator I = begin(), E = end(); I != E; ++I)
514 if (I->hasAttribute(Kind))
519 Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
520 for (iterator I = begin(), E = end(); I != E; ++I)
521 if (I->hasAttribute(Kind))
526 unsigned AttributeSetNode::getAlignment() const {
527 for (iterator I = begin(), E = end(); I != E; ++I)
528 if (I->hasAttribute(Attribute::Alignment))
529 return I->getAlignment();
533 unsigned AttributeSetNode::getStackAlignment() const {
534 for (iterator I = begin(), E = end(); I != E; ++I)
535 if (I->hasAttribute(Attribute::StackAlignment))
536 return I->getStackAlignment();
540 uint64_t AttributeSetNode::getDereferenceableBytes() const {
541 for (iterator I = begin(), E = end(); I != E; ++I)
542 if (I->hasAttribute(Attribute::Dereferenceable))
543 return I->getDereferenceableBytes();
547 uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
548 for (iterator I = begin(), E = end(); I != E; ++I)
549 if (I->hasAttribute(Attribute::DereferenceableOrNull))
550 return I->getDereferenceableOrNullBytes();
554 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
556 for (iterator I = begin(), E = end(); I != E; ++I) {
559 Str += I->getAsString(InAttrGrp);
564 //===----------------------------------------------------------------------===//
565 // AttributeSetImpl Definition
566 //===----------------------------------------------------------------------===//
568 uint64_t AttributeSetImpl::Raw(unsigned Index) const {
569 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
570 if (getSlotIndex(I) != Index) continue;
571 const AttributeSetNode *ASN = getSlotNode(I);
574 for (AttributeSetNode::iterator II = ASN->begin(),
575 IE = ASN->end(); II != IE; ++II) {
576 Attribute Attr = *II;
578 // This cannot handle string attributes.
579 if (Attr.isStringAttribute()) continue;
581 Attribute::AttrKind Kind = Attr.getKindAsEnum();
583 if (Kind == Attribute::Alignment)
584 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
585 else if (Kind == Attribute::StackAlignment)
586 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
587 else if (Kind == Attribute::Dereferenceable)
588 llvm_unreachable("dereferenceable not supported in bit mask");
590 Mask |= AttributeImpl::getAttrMask(Kind);
599 void AttributeSetImpl::dump() const {
600 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
603 //===----------------------------------------------------------------------===//
604 // AttributeSet Construction and Mutation Methods
605 //===----------------------------------------------------------------------===//
608 AttributeSet::getImpl(LLVMContext &C,
609 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
610 LLVMContextImpl *pImpl = C.pImpl;
612 AttributeSetImpl::Profile(ID, Attrs);
615 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
617 // If we didn't find any existing attributes of the same shape then
618 // create a new one and insert it.
620 // Coallocate entries after the AttributeSetImpl itself.
621 void *Mem = ::operator new(
622 AttributeSetImpl::totalSizeToAlloc<IndexAttrPair>(Attrs.size()));
623 PA = new (Mem) AttributeSetImpl(C, Attrs);
624 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
627 // Return the AttributesList that we found or created.
628 return AttributeSet(PA);
631 AttributeSet AttributeSet::get(LLVMContext &C,
632 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
633 // If there are no attributes then return a null AttributesList pointer.
635 return AttributeSet();
638 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
639 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
640 "Misordered Attributes list!");
641 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
642 "Pointless attribute!");
646 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
648 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
649 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
650 E = Attrs.end(); I != E; ) {
651 unsigned Index = I->first;
652 SmallVector<Attribute, 4> AttrVec;
653 while (I != E && I->first == Index) {
654 AttrVec.push_back(I->second);
658 AttrPairVec.push_back(std::make_pair(Index,
659 AttributeSetNode::get(C, AttrVec)));
662 return getImpl(C, AttrPairVec);
665 AttributeSet AttributeSet::get(LLVMContext &C,
666 ArrayRef<std::pair<unsigned,
667 AttributeSetNode*> > Attrs) {
668 // If there are no attributes then return a null AttributesList pointer.
670 return AttributeSet();
672 return getImpl(C, Attrs);
675 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
676 const AttrBuilder &B) {
677 if (!B.hasAttributes())
678 return AttributeSet();
680 // Add target-independent attributes.
681 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
682 for (Attribute::AttrKind Kind = Attribute::None;
683 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
684 if (!B.contains(Kind))
689 case Attribute::Alignment:
690 Attr = Attribute::getWithAlignment(C, B.getAlignment());
692 case Attribute::StackAlignment:
693 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
695 case Attribute::Dereferenceable:
696 Attr = Attribute::getWithDereferenceableBytes(
697 C, B.getDereferenceableBytes());
699 case Attribute::DereferenceableOrNull:
700 Attr = Attribute::getWithDereferenceableOrNullBytes(
701 C, B.getDereferenceableOrNullBytes());
704 Attr = Attribute::get(C, Kind);
706 Attrs.push_back(std::make_pair(Index, Attr));
709 // Add target-dependent (string) attributes.
710 for (const AttrBuilder::td_type &TDA : B.td_attrs())
712 std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
714 return get(C, Attrs);
717 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
718 ArrayRef<Attribute::AttrKind> Kind) {
719 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
720 for (Attribute::AttrKind K : Kind)
721 Attrs.push_back(std::make_pair(Index, Attribute::get(C, K)));
722 return get(C, Attrs);
725 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
726 if (Attrs.empty()) return AttributeSet();
727 if (Attrs.size() == 1) return Attrs[0];
729 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
730 AttributeSetImpl *A0 = Attrs[0].pImpl;
732 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
733 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
734 // ordered by index. Because we know that each list in Attrs is ordered by
735 // index we only need to merge each successive list in rather than doing a
737 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
738 AttributeSetImpl *AS = Attrs[I].pImpl;
740 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
741 ANVI = AttrNodeVec.begin(), ANVE;
742 for (const IndexAttrPair *AI = AS->getNode(0),
743 *AE = AS->getNode(AS->getNumAttributes());
745 ANVE = AttrNodeVec.end();
746 while (ANVI != ANVE && ANVI->first <= AI->first)
748 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
752 return getImpl(C, AttrNodeVec);
755 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
756 Attribute::AttrKind Attr) const {
757 if (hasAttribute(Index, Attr)) return *this;
758 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
761 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
762 StringRef Kind) const {
764 B.addAttribute(Kind);
765 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
768 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
769 StringRef Kind, StringRef Value) const {
771 B.addAttribute(Kind, Value);
772 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
775 AttributeSet AttributeSet::addAttribute(LLVMContext &C,
776 ArrayRef<unsigned> Indices,
778 unsigned I = 0, E = pImpl ? pImpl->getNumAttributes() : 0;
779 auto IdxI = Indices.begin(), IdxE = Indices.end();
780 SmallVector<AttributeSet, 4> AttrSet;
782 while (I != E && IdxI != IdxE) {
783 if (getSlotIndex(I) < *IdxI)
784 AttrSet.emplace_back(getSlotAttributes(I++));
785 else if (getSlotIndex(I) > *IdxI)
786 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
788 AttrBuilder B(getSlotAttributes(I), *IdxI);
790 AttrSet.emplace_back(AttributeSet::get(C, *IdxI, B));
797 AttrSet.emplace_back(getSlotAttributes(I++));
800 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
802 return get(C, AttrSet);
805 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
806 AttributeSet Attrs) const {
807 if (!pImpl) return Attrs;
808 if (!Attrs.pImpl) return *this;
811 // FIXME it is not obvious how this should work for alignment. For now, say
812 // we can't change a known alignment.
813 unsigned OldAlign = getParamAlignment(Index);
814 unsigned NewAlign = Attrs.getParamAlignment(Index);
815 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
816 "Attempt to change alignment!");
819 // Add the attribute slots before the one we're trying to add.
820 SmallVector<AttributeSet, 4> AttrSet;
821 uint64_t NumAttrs = pImpl->getNumAttributes();
823 uint64_t LastIndex = 0;
824 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
825 if (getSlotIndex(I) >= Index) {
826 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
830 AttrSet.push_back(getSlotAttributes(I));
833 // Now add the attribute into the correct slot. There may already be an
834 // AttributeSet there.
835 AttrBuilder B(AS, Index);
837 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
838 if (Attrs.getSlotIndex(I) == Index) {
839 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
840 IE = Attrs.pImpl->end(I); II != IE; ++II)
845 AttrSet.push_back(AttributeSet::get(C, Index, B));
847 // Add the remaining attribute slots.
848 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
849 AttrSet.push_back(getSlotAttributes(I));
851 return get(C, AttrSet);
854 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
855 Attribute::AttrKind Attr) const {
856 if (!hasAttribute(Index, Attr)) return *this;
857 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
860 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
861 AttributeSet Attrs) const {
862 if (!pImpl) return AttributeSet();
863 if (!Attrs.pImpl) return *this;
865 // FIXME it is not obvious how this should work for alignment.
866 // For now, say we can't pass in alignment, which no current use does.
867 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
868 "Attempt to change alignment!");
870 // Add the attribute slots before the one we're trying to add.
871 SmallVector<AttributeSet, 4> AttrSet;
872 uint64_t NumAttrs = pImpl->getNumAttributes();
874 uint64_t LastIndex = 0;
875 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
876 if (getSlotIndex(I) >= Index) {
877 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
881 AttrSet.push_back(getSlotAttributes(I));
884 // Now remove the attribute from the correct slot. There may already be an
885 // AttributeSet there.
886 AttrBuilder B(AS, Index);
888 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
889 if (Attrs.getSlotIndex(I) == Index) {
890 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
894 AttrSet.push_back(AttributeSet::get(C, Index, B));
896 // Add the remaining attribute slots.
897 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
898 AttrSet.push_back(getSlotAttributes(I));
900 return get(C, AttrSet);
903 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
904 const AttrBuilder &Attrs) const {
905 if (!pImpl) return AttributeSet();
907 // FIXME it is not obvious how this should work for alignment.
908 // For now, say we can't pass in alignment, which no current use does.
909 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
911 // Add the attribute slots before the one we're trying to add.
912 SmallVector<AttributeSet, 4> AttrSet;
913 uint64_t NumAttrs = pImpl->getNumAttributes();
915 uint64_t LastIndex = 0;
916 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
917 if (getSlotIndex(I) >= Index) {
918 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
922 AttrSet.push_back(getSlotAttributes(I));
925 // Now remove the attribute from the correct slot. There may already be an
926 // AttributeSet there.
927 AttrBuilder B(AS, Index);
930 AttrSet.push_back(AttributeSet::get(C, Index, B));
932 // Add the remaining attribute slots.
933 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
934 AttrSet.push_back(getSlotAttributes(I));
936 return get(C, AttrSet);
939 AttributeSet AttributeSet::addDereferenceableAttr(LLVMContext &C, unsigned Index,
940 uint64_t Bytes) const {
942 B.addDereferenceableAttr(Bytes);
943 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
946 AttributeSet AttributeSet::addDereferenceableOrNullAttr(LLVMContext &C,
948 uint64_t Bytes) const {
950 B.addDereferenceableOrNullAttr(Bytes);
951 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
954 //===----------------------------------------------------------------------===//
955 // AttributeSet Accessor Methods
956 //===----------------------------------------------------------------------===//
958 LLVMContext &AttributeSet::getContext() const {
959 return pImpl->getContext();
962 AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
963 return pImpl && hasAttributes(Index) ?
964 AttributeSet::get(pImpl->getContext(),
965 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
966 std::make_pair(Index, getAttributes(Index)))) :
970 AttributeSet AttributeSet::getRetAttributes() const {
971 return pImpl && hasAttributes(ReturnIndex) ?
972 AttributeSet::get(pImpl->getContext(),
973 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
974 std::make_pair(ReturnIndex,
975 getAttributes(ReturnIndex)))) :
979 AttributeSet AttributeSet::getFnAttributes() const {
980 return pImpl && hasAttributes(FunctionIndex) ?
981 AttributeSet::get(pImpl->getContext(),
982 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
983 std::make_pair(FunctionIndex,
984 getAttributes(FunctionIndex)))) :
988 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
989 AttributeSetNode *ASN = getAttributes(Index);
990 return ASN && ASN->hasAttribute(Kind);
993 bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
994 AttributeSetNode *ASN = getAttributes(Index);
995 return ASN && ASN->hasAttribute(Kind);
998 bool AttributeSet::hasAttributes(unsigned Index) const {
999 AttributeSetNode *ASN = getAttributes(Index);
1000 return ASN && ASN->hasAttributes();
1003 /// \brief Return true if the specified attribute is set for at least one
1004 /// parameter or for the return value.
1005 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
1006 if (!pImpl) return false;
1008 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
1009 for (AttributeSetImpl::iterator II = pImpl->begin(I),
1010 IE = pImpl->end(I); II != IE; ++II)
1011 if (II->hasAttribute(Attr))
1017 Attribute AttributeSet::getAttribute(unsigned Index,
1018 Attribute::AttrKind Kind) const {
1019 AttributeSetNode *ASN = getAttributes(Index);
1020 return ASN ? ASN->getAttribute(Kind) : Attribute();
1023 Attribute AttributeSet::getAttribute(unsigned Index,
1024 StringRef Kind) const {
1025 AttributeSetNode *ASN = getAttributes(Index);
1026 return ASN ? ASN->getAttribute(Kind) : Attribute();
1029 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
1030 AttributeSetNode *ASN = getAttributes(Index);
1031 return ASN ? ASN->getAlignment() : 0;
1034 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
1035 AttributeSetNode *ASN = getAttributes(Index);
1036 return ASN ? ASN->getStackAlignment() : 0;
1039 uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
1040 AttributeSetNode *ASN = getAttributes(Index);
1041 return ASN ? ASN->getDereferenceableBytes() : 0;
1044 uint64_t AttributeSet::getDereferenceableOrNullBytes(unsigned Index) const {
1045 AttributeSetNode *ASN = getAttributes(Index);
1046 return ASN ? ASN->getDereferenceableOrNullBytes() : 0;
1049 std::string AttributeSet::getAsString(unsigned Index,
1050 bool InAttrGrp) const {
1051 AttributeSetNode *ASN = getAttributes(Index);
1052 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
1055 /// \brief The attributes for the specified index are returned.
1056 AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
1057 if (!pImpl) return nullptr;
1059 // Loop through to find the attribute node we want.
1060 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
1061 if (pImpl->getSlotIndex(I) == Index)
1062 return pImpl->getSlotNode(I);
1067 AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
1069 return ArrayRef<Attribute>().begin();
1070 return pImpl->begin(Slot);
1073 AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
1075 return ArrayRef<Attribute>().end();
1076 return pImpl->end(Slot);
1079 //===----------------------------------------------------------------------===//
1080 // AttributeSet Introspection Methods
1081 //===----------------------------------------------------------------------===//
1083 /// \brief Return the number of slots used in this attribute list. This is the
1084 /// number of arguments that have an attribute set on them (including the
1085 /// function itself).
1086 unsigned AttributeSet::getNumSlots() const {
1087 return pImpl ? pImpl->getNumAttributes() : 0;
1090 unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
1091 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1092 "Slot # out of range!");
1093 return pImpl->getSlotIndex(Slot);
1096 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
1097 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1098 "Slot # out of range!");
1099 return pImpl->getSlotAttributes(Slot);
1102 uint64_t AttributeSet::Raw(unsigned Index) const {
1103 // FIXME: Remove this.
1104 return pImpl ? pImpl->Raw(Index) : 0;
1107 void AttributeSet::dump() const {
1110 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1111 uint64_t Index = getSlotIndex(i);
1117 dbgs() << " => " << getAsString(Index) << " }\n";
1123 //===----------------------------------------------------------------------===//
1124 // AttrBuilder Method Implementations
1125 //===----------------------------------------------------------------------===//
1127 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
1128 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
1129 DerefOrNullBytes(0) {
1130 AttributeSetImpl *pImpl = AS.pImpl;
1133 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
1134 if (pImpl->getSlotIndex(I) != Index) continue;
1136 for (AttributeSetImpl::iterator II = pImpl->begin(I),
1137 IE = pImpl->end(I); II != IE; ++II)
1144 void AttrBuilder::clear() {
1146 TargetDepAttrs.clear();
1147 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
1150 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
1151 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1152 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
1153 Val != Attribute::Dereferenceable &&
1154 "Adding integer attribute without adding a value!");
1159 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
1160 if (Attr.isStringAttribute()) {
1161 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1165 Attribute::AttrKind Kind = Attr.getKindAsEnum();
1168 if (Kind == Attribute::Alignment)
1169 Alignment = Attr.getAlignment();
1170 else if (Kind == Attribute::StackAlignment)
1171 StackAlignment = Attr.getStackAlignment();
1172 else if (Kind == Attribute::Dereferenceable)
1173 DerefBytes = Attr.getDereferenceableBytes();
1174 else if (Kind == Attribute::DereferenceableOrNull)
1175 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
1179 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1180 TargetDepAttrs[A] = V;
1184 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
1185 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1188 if (Val == Attribute::Alignment)
1190 else if (Val == Attribute::StackAlignment)
1192 else if (Val == Attribute::Dereferenceable)
1194 else if (Val == Attribute::DereferenceableOrNull)
1195 DerefOrNullBytes = 0;
1200 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
1201 unsigned Slot = ~0U;
1202 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1203 if (A.getSlotIndex(I) == Index) {
1208 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
1210 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
1211 Attribute Attr = *I;
1212 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1213 removeAttribute(Attr.getKindAsEnum());
1215 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1216 removeAttribute(Attr.getKindAsString());
1223 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1224 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1225 if (I != TargetDepAttrs.end())
1226 TargetDepAttrs.erase(I);
1230 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
1231 if (Align == 0) return *this;
1233 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1234 assert(Align <= 0x40000000 && "Alignment too large.");
1236 Attrs[Attribute::Alignment] = true;
1241 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1242 // Default alignment, allow the target to define how to align it.
1243 if (Align == 0) return *this;
1245 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1246 assert(Align <= 0x100 && "Alignment too large.");
1248 Attrs[Attribute::StackAlignment] = true;
1249 StackAlignment = Align;
1253 AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1254 if (Bytes == 0) return *this;
1256 Attrs[Attribute::Dereferenceable] = true;
1261 AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1265 Attrs[Attribute::DereferenceableOrNull] = true;
1266 DerefOrNullBytes = Bytes;
1270 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1271 // FIXME: What if both have alignments, but they don't match?!
1273 Alignment = B.Alignment;
1275 if (!StackAlignment)
1276 StackAlignment = B.StackAlignment;
1279 DerefBytes = B.DerefBytes;
1281 if (!DerefOrNullBytes)
1282 DerefOrNullBytes = B.DerefOrNullBytes;
1286 for (auto I : B.td_attrs())
1287 TargetDepAttrs[I.first] = I.second;
1292 AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1293 // FIXME: What if both have alignments, but they don't match?!
1297 if (B.StackAlignment)
1303 if (B.DerefOrNullBytes)
1304 DerefOrNullBytes = 0;
1308 for (auto I : B.td_attrs())
1309 TargetDepAttrs.erase(I.first);
1314 bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1315 // First check if any of the target independent attributes overlap.
1316 if ((Attrs & B.Attrs).any())
1319 // Then check if any target dependent ones do.
1320 for (auto I : td_attrs())
1321 if (B.contains(I.first))
1327 bool AttrBuilder::contains(StringRef A) const {
1328 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1331 bool AttrBuilder::hasAttributes() const {
1332 return !Attrs.none() || !TargetDepAttrs.empty();
1335 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
1336 unsigned Slot = ~0U;
1337 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1338 if (A.getSlotIndex(I) == Index) {
1343 assert(Slot != ~0U && "Couldn't find the index!");
1345 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
1346 Attribute Attr = *I;
1347 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1348 if (Attrs[I->getKindAsEnum()])
1351 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1352 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1359 bool AttrBuilder::hasAlignmentAttr() const {
1360 return Alignment != 0;
1363 bool AttrBuilder::operator==(const AttrBuilder &B) {
1364 if (Attrs != B.Attrs)
1367 for (td_const_iterator I = TargetDepAttrs.begin(),
1368 E = TargetDepAttrs.end(); I != E; ++I)
1369 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1372 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1373 DerefBytes == B.DerefBytes;
1376 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
1377 // FIXME: Remove this in 4.0.
1378 if (!Val) return *this;
1380 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1381 I = Attribute::AttrKind(I + 1)) {
1382 if (I == Attribute::Dereferenceable ||
1383 I == Attribute::DereferenceableOrNull ||
1384 I == Attribute::ArgMemOnly)
1386 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1389 if (I == Attribute::Alignment)
1390 Alignment = 1ULL << ((A >> 16) - 1);
1391 else if (I == Attribute::StackAlignment)
1392 StackAlignment = 1ULL << ((A >> 26)-1);
1399 //===----------------------------------------------------------------------===//
1400 // AttributeFuncs Function Defintions
1401 //===----------------------------------------------------------------------===//
1403 /// \brief Which attributes cannot be applied to a type.
1404 AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
1405 AttrBuilder Incompatible;
1407 if (!Ty->isIntegerTy())
1408 // Attribute that only apply to integers.
1409 Incompatible.addAttribute(Attribute::SExt)
1410 .addAttribute(Attribute::ZExt);
1412 if (!Ty->isPointerTy())
1413 // Attribute that only apply to pointers.
1414 Incompatible.addAttribute(Attribute::ByVal)
1415 .addAttribute(Attribute::Nest)
1416 .addAttribute(Attribute::NoAlias)
1417 .addAttribute(Attribute::NoCapture)
1418 .addAttribute(Attribute::NonNull)
1419 .addDereferenceableAttr(1) // the int here is ignored
1420 .addDereferenceableOrNullAttr(1) // the int here is ignored
1421 .addAttribute(Attribute::ReadNone)
1422 .addAttribute(Attribute::ReadOnly)
1423 .addAttribute(Attribute::StructRet)
1424 .addAttribute(Attribute::InAlloca);
1426 return Incompatible;