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 ? pImpl->getKindAsEnum() : None;
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 ? pImpl->getValueAsInt() : 0;
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 ? pImpl->getKindAsString() : StringRef();
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 ? pImpl->getValueAsString() : StringRef();
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::Builtin))
195 if (hasAttribute(Attribute::ByVal))
197 if (hasAttribute(Attribute::Convergent))
199 if (hasAttribute(Attribute::InAlloca))
201 if (hasAttribute(Attribute::InlineHint))
203 if (hasAttribute(Attribute::InReg))
205 if (hasAttribute(Attribute::JumpTable))
207 if (hasAttribute(Attribute::MinSize))
209 if (hasAttribute(Attribute::Naked))
211 if (hasAttribute(Attribute::Nest))
213 if (hasAttribute(Attribute::NoAlias))
215 if (hasAttribute(Attribute::NoBuiltin))
217 if (hasAttribute(Attribute::NoCapture))
219 if (hasAttribute(Attribute::NoDuplicate))
220 return "noduplicate";
221 if (hasAttribute(Attribute::NoImplicitFloat))
222 return "noimplicitfloat";
223 if (hasAttribute(Attribute::NoInline))
225 if (hasAttribute(Attribute::NonLazyBind))
226 return "nonlazybind";
227 if (hasAttribute(Attribute::NonNull))
229 if (hasAttribute(Attribute::NoRedZone))
231 if (hasAttribute(Attribute::NoReturn))
233 if (hasAttribute(Attribute::NoUnwind))
235 if (hasAttribute(Attribute::OptimizeNone))
237 if (hasAttribute(Attribute::OptimizeForSize))
239 if (hasAttribute(Attribute::ReadNone))
241 if (hasAttribute(Attribute::ReadOnly))
243 if (hasAttribute(Attribute::Returned))
245 if (hasAttribute(Attribute::ReturnsTwice))
246 return "returns_twice";
247 if (hasAttribute(Attribute::SExt))
249 if (hasAttribute(Attribute::StackProtect))
251 if (hasAttribute(Attribute::StackProtectReq))
253 if (hasAttribute(Attribute::StackProtectStrong))
255 if (hasAttribute(Attribute::SafeStack))
257 if (hasAttribute(Attribute::StructRet))
259 if (hasAttribute(Attribute::SanitizeThread))
260 return "sanitize_thread";
261 if (hasAttribute(Attribute::SanitizeMemory))
262 return "sanitize_memory";
263 if (hasAttribute(Attribute::UWTable))
265 if (hasAttribute(Attribute::ZExt))
267 if (hasAttribute(Attribute::Cold))
270 // FIXME: These should be output like this:
275 if (hasAttribute(Attribute::Alignment)) {
278 Result += (InAttrGrp) ? "=" : " ";
279 Result += utostr(getValueAsInt());
283 auto AttrWithBytesToString = [&](const char *Name) {
288 Result += utostr(getValueAsInt());
291 Result += utostr(getValueAsInt());
297 if (hasAttribute(Attribute::StackAlignment))
298 return AttrWithBytesToString("alignstack");
300 if (hasAttribute(Attribute::Dereferenceable))
301 return AttrWithBytesToString("dereferenceable");
303 if (hasAttribute(Attribute::DereferenceableOrNull))
304 return AttrWithBytesToString("dereferenceable_or_null");
306 // Convert target-dependent attributes to strings of the form:
311 if (isStringAttribute()) {
313 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
315 StringRef Val = pImpl->getValueAsString();
316 if (Val.empty()) return Result;
318 Result += ("=\"" + Val + Twine('"')).str();
322 llvm_unreachable("Unknown attribute");
325 bool Attribute::operator<(Attribute A) const {
326 if (!pImpl && !A.pImpl) return false;
327 if (!pImpl) return true;
328 if (!A.pImpl) return false;
329 return *pImpl < *A.pImpl;
332 //===----------------------------------------------------------------------===//
333 // AttributeImpl Definition
334 //===----------------------------------------------------------------------===//
336 // Pin the vtables to this file.
337 AttributeImpl::~AttributeImpl() {}
338 void EnumAttributeImpl::anchor() {}
339 void IntAttributeImpl::anchor() {}
340 void StringAttributeImpl::anchor() {}
342 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
343 if (isStringAttribute()) return false;
344 return getKindAsEnum() == A;
347 bool AttributeImpl::hasAttribute(StringRef Kind) const {
348 if (!isStringAttribute()) return false;
349 return getKindAsString() == Kind;
352 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
353 assert(isEnumAttribute() || isIntAttribute());
354 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
357 uint64_t AttributeImpl::getValueAsInt() const {
358 assert(isIntAttribute());
359 return static_cast<const IntAttributeImpl *>(this)->getValue();
362 StringRef AttributeImpl::getKindAsString() const {
363 assert(isStringAttribute());
364 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
367 StringRef AttributeImpl::getValueAsString() const {
368 assert(isStringAttribute());
369 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
372 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
373 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
374 // relative to their enum value) and then strings.
375 if (isEnumAttribute()) {
376 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
377 if (AI.isIntAttribute()) return true;
378 if (AI.isStringAttribute()) return true;
381 if (isIntAttribute()) {
382 if (AI.isEnumAttribute()) return false;
383 if (AI.isIntAttribute()) return getValueAsInt() < AI.getValueAsInt();
384 if (AI.isStringAttribute()) return true;
387 if (AI.isEnumAttribute()) return false;
388 if (AI.isIntAttribute()) return false;
389 if (getKindAsString() == AI.getKindAsString())
390 return getValueAsString() < AI.getValueAsString();
391 return getKindAsString() < AI.getKindAsString();
394 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
395 // FIXME: Remove this.
397 case Attribute::EndAttrKinds:
398 llvm_unreachable("Synthetic enumerators which should never get here");
400 case Attribute::None: return 0;
401 case Attribute::ZExt: return 1 << 0;
402 case Attribute::SExt: return 1 << 1;
403 case Attribute::NoReturn: return 1 << 2;
404 case Attribute::InReg: return 1 << 3;
405 case Attribute::StructRet: return 1 << 4;
406 case Attribute::NoUnwind: return 1 << 5;
407 case Attribute::NoAlias: return 1 << 6;
408 case Attribute::ByVal: return 1 << 7;
409 case Attribute::Nest: return 1 << 8;
410 case Attribute::ReadNone: return 1 << 9;
411 case Attribute::ReadOnly: return 1 << 10;
412 case Attribute::NoInline: return 1 << 11;
413 case Attribute::AlwaysInline: return 1 << 12;
414 case Attribute::OptimizeForSize: return 1 << 13;
415 case Attribute::StackProtect: return 1 << 14;
416 case Attribute::StackProtectReq: return 1 << 15;
417 case Attribute::Alignment: return 31 << 16;
418 case Attribute::NoCapture: return 1 << 21;
419 case Attribute::NoRedZone: return 1 << 22;
420 case Attribute::NoImplicitFloat: return 1 << 23;
421 case Attribute::Naked: return 1 << 24;
422 case Attribute::InlineHint: return 1 << 25;
423 case Attribute::StackAlignment: return 7 << 26;
424 case Attribute::ReturnsTwice: return 1 << 29;
425 case Attribute::UWTable: return 1 << 30;
426 case Attribute::NonLazyBind: return 1U << 31;
427 case Attribute::SanitizeAddress: return 1ULL << 32;
428 case Attribute::MinSize: return 1ULL << 33;
429 case Attribute::NoDuplicate: return 1ULL << 34;
430 case Attribute::StackProtectStrong: return 1ULL << 35;
431 case Attribute::SanitizeThread: return 1ULL << 36;
432 case Attribute::SanitizeMemory: return 1ULL << 37;
433 case Attribute::NoBuiltin: return 1ULL << 38;
434 case Attribute::Returned: return 1ULL << 39;
435 case Attribute::Cold: return 1ULL << 40;
436 case Attribute::Builtin: return 1ULL << 41;
437 case Attribute::OptimizeNone: return 1ULL << 42;
438 case Attribute::InAlloca: return 1ULL << 43;
439 case Attribute::NonNull: return 1ULL << 44;
440 case Attribute::JumpTable: return 1ULL << 45;
441 case Attribute::Convergent: return 1ULL << 46;
442 case Attribute::SafeStack: return 1ULL << 47;
443 case Attribute::Dereferenceable:
444 llvm_unreachable("dereferenceable attribute not supported in raw format");
446 case Attribute::DereferenceableOrNull:
447 llvm_unreachable("dereferenceable_or_null attribute not supported in raw "
451 llvm_unreachable("Unsupported attribute type");
454 //===----------------------------------------------------------------------===//
455 // AttributeSetNode Definition
456 //===----------------------------------------------------------------------===//
458 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
459 ArrayRef<Attribute> Attrs) {
463 // Otherwise, build a key to look up the existing attributes.
464 LLVMContextImpl *pImpl = C.pImpl;
467 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
468 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
470 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
471 E = SortedAttrs.end(); I != E; ++I)
475 AttributeSetNode *PA =
476 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
478 // If we didn't find any existing attributes of the same shape then create a
479 // new one and insert it.
481 // Coallocate entries after the AttributeSetNode itself.
482 void *Mem = ::operator new(sizeof(AttributeSetNode) +
483 sizeof(Attribute) * SortedAttrs.size());
484 PA = new (Mem) AttributeSetNode(SortedAttrs);
485 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
488 // Return the AttributesListNode that we found or created.
492 bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
493 for (iterator I = begin(), E = end(); I != E; ++I)
494 if (I->hasAttribute(Kind))
499 bool AttributeSetNode::hasAttribute(StringRef Kind) const {
500 for (iterator I = begin(), E = end(); I != E; ++I)
501 if (I->hasAttribute(Kind))
506 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
507 for (iterator I = begin(), E = end(); I != E; ++I)
508 if (I->hasAttribute(Kind))
513 Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
514 for (iterator I = begin(), E = end(); I != E; ++I)
515 if (I->hasAttribute(Kind))
520 unsigned AttributeSetNode::getAlignment() const {
521 for (iterator I = begin(), E = end(); I != E; ++I)
522 if (I->hasAttribute(Attribute::Alignment))
523 return I->getAlignment();
527 unsigned AttributeSetNode::getStackAlignment() const {
528 for (iterator I = begin(), E = end(); I != E; ++I)
529 if (I->hasAttribute(Attribute::StackAlignment))
530 return I->getStackAlignment();
534 uint64_t AttributeSetNode::getDereferenceableBytes() const {
535 for (iterator I = begin(), E = end(); I != E; ++I)
536 if (I->hasAttribute(Attribute::Dereferenceable))
537 return I->getDereferenceableBytes();
541 uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
542 for (iterator I = begin(), E = end(); I != E; ++I)
543 if (I->hasAttribute(Attribute::DereferenceableOrNull))
544 return I->getDereferenceableOrNullBytes();
548 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
550 for (iterator I = begin(), E = end(); I != E; ++I) {
553 Str += I->getAsString(InAttrGrp);
558 //===----------------------------------------------------------------------===//
559 // AttributeSetImpl Definition
560 //===----------------------------------------------------------------------===//
562 uint64_t AttributeSetImpl::Raw(unsigned Index) const {
563 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
564 if (getSlotIndex(I) != Index) continue;
565 const AttributeSetNode *ASN = getSlotNode(I);
568 for (AttributeSetNode::iterator II = ASN->begin(),
569 IE = ASN->end(); II != IE; ++II) {
570 Attribute Attr = *II;
572 // This cannot handle string attributes.
573 if (Attr.isStringAttribute()) continue;
575 Attribute::AttrKind Kind = Attr.getKindAsEnum();
577 if (Kind == Attribute::Alignment)
578 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
579 else if (Kind == Attribute::StackAlignment)
580 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
581 else if (Kind == Attribute::Dereferenceable)
582 llvm_unreachable("dereferenceable not supported in bit mask");
584 Mask |= AttributeImpl::getAttrMask(Kind);
593 void AttributeSetImpl::dump() const {
594 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
597 //===----------------------------------------------------------------------===//
598 // AttributeSet Construction and Mutation Methods
599 //===----------------------------------------------------------------------===//
602 AttributeSet::getImpl(LLVMContext &C,
603 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
604 LLVMContextImpl *pImpl = C.pImpl;
606 AttributeSetImpl::Profile(ID, Attrs);
609 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
611 // If we didn't find any existing attributes of the same shape then
612 // create a new one and insert it.
614 // Coallocate entries after the AttributeSetImpl itself.
615 void *Mem = ::operator new(sizeof(AttributeSetImpl) +
616 sizeof(std::pair<unsigned, AttributeSetNode *>) *
618 PA = new (Mem) AttributeSetImpl(C, Attrs);
619 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
622 // Return the AttributesList that we found or created.
623 return AttributeSet(PA);
626 AttributeSet AttributeSet::get(LLVMContext &C,
627 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
628 // If there are no attributes then return a null AttributesList pointer.
630 return AttributeSet();
633 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
634 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
635 "Misordered Attributes list!");
636 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
637 "Pointless attribute!");
641 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
643 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
644 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
645 E = Attrs.end(); I != E; ) {
646 unsigned Index = I->first;
647 SmallVector<Attribute, 4> AttrVec;
648 while (I != E && I->first == Index) {
649 AttrVec.push_back(I->second);
653 AttrPairVec.push_back(std::make_pair(Index,
654 AttributeSetNode::get(C, AttrVec)));
657 return getImpl(C, AttrPairVec);
660 AttributeSet AttributeSet::get(LLVMContext &C,
661 ArrayRef<std::pair<unsigned,
662 AttributeSetNode*> > Attrs) {
663 // If there are no attributes then return a null AttributesList pointer.
665 return AttributeSet();
667 return getImpl(C, Attrs);
670 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
671 const AttrBuilder &B) {
672 if (!B.hasAttributes())
673 return AttributeSet();
675 // Add target-independent attributes.
676 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
677 for (Attribute::AttrKind Kind = Attribute::None;
678 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
679 if (!B.contains(Kind))
682 if (Kind == Attribute::Alignment)
683 Attrs.push_back(std::make_pair(Index, Attribute::
684 getWithAlignment(C, B.getAlignment())));
685 else if (Kind == Attribute::StackAlignment)
686 Attrs.push_back(std::make_pair(Index, Attribute::
687 getWithStackAlignment(C, B.getStackAlignment())));
688 else if (Kind == Attribute::Dereferenceable)
689 Attrs.push_back(std::make_pair(Index,
690 Attribute::getWithDereferenceableBytes(C,
691 B.getDereferenceableBytes())));
692 else if (Kind == Attribute::DereferenceableOrNull)
694 std::make_pair(Index, Attribute::getWithDereferenceableOrNullBytes(
695 C, B.getDereferenceableOrNullBytes())));
697 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
700 // Add target-dependent (string) attributes.
701 for (const AttrBuilder::td_type &TDA : B.td_attrs())
703 std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
705 return get(C, Attrs);
708 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
709 ArrayRef<Attribute::AttrKind> Kind) {
710 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
711 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
712 E = Kind.end(); I != E; ++I)
713 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
714 return get(C, Attrs);
717 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
718 if (Attrs.empty()) return AttributeSet();
719 if (Attrs.size() == 1) return Attrs[0];
721 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
722 AttributeSetImpl *A0 = Attrs[0].pImpl;
724 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
725 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
726 // ordered by index. Because we know that each list in Attrs is ordered by
727 // index we only need to merge each successive list in rather than doing a
729 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
730 AttributeSetImpl *AS = Attrs[I].pImpl;
732 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
733 ANVI = AttrNodeVec.begin(), ANVE;
734 for (const AttributeSetImpl::IndexAttrPair
735 *AI = AS->getNode(0),
736 *AE = AS->getNode(AS->getNumAttributes());
738 ANVE = AttrNodeVec.end();
739 while (ANVI != ANVE && ANVI->first <= AI->first)
741 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
745 return getImpl(C, AttrNodeVec);
748 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
749 Attribute::AttrKind Attr) const {
750 if (hasAttribute(Index, Attr)) return *this;
751 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
754 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
755 StringRef Kind) const {
757 B.addAttribute(Kind);
758 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
761 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
762 StringRef Kind, StringRef Value) const {
764 B.addAttribute(Kind, Value);
765 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
768 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
769 AttributeSet Attrs) const {
770 if (!pImpl) return Attrs;
771 if (!Attrs.pImpl) return *this;
774 // FIXME it is not obvious how this should work for alignment. For now, say
775 // we can't change a known alignment.
776 unsigned OldAlign = getParamAlignment(Index);
777 unsigned NewAlign = Attrs.getParamAlignment(Index);
778 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
779 "Attempt to change alignment!");
782 // Add the attribute slots before the one we're trying to add.
783 SmallVector<AttributeSet, 4> AttrSet;
784 uint64_t NumAttrs = pImpl->getNumAttributes();
786 uint64_t LastIndex = 0;
787 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
788 if (getSlotIndex(I) >= Index) {
789 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
793 AttrSet.push_back(getSlotAttributes(I));
796 // Now add the attribute into the correct slot. There may already be an
797 // AttributeSet there.
798 AttrBuilder B(AS, Index);
800 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
801 if (Attrs.getSlotIndex(I) == Index) {
802 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
803 IE = Attrs.pImpl->end(I); II != IE; ++II)
808 AttrSet.push_back(AttributeSet::get(C, Index, B));
810 // Add the remaining attribute slots.
811 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
812 AttrSet.push_back(getSlotAttributes(I));
814 return get(C, AttrSet);
817 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
818 Attribute::AttrKind Attr) const {
819 if (!hasAttribute(Index, Attr)) return *this;
820 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
823 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
824 AttributeSet Attrs) const {
825 if (!pImpl) return AttributeSet();
826 if (!Attrs.pImpl) return *this;
828 // FIXME it is not obvious how this should work for alignment.
829 // For now, say we can't pass in alignment, which no current use does.
830 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
831 "Attempt to change alignment!");
833 // Add the attribute slots before the one we're trying to add.
834 SmallVector<AttributeSet, 4> AttrSet;
835 uint64_t NumAttrs = pImpl->getNumAttributes();
837 uint64_t LastIndex = 0;
838 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
839 if (getSlotIndex(I) >= Index) {
840 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
844 AttrSet.push_back(getSlotAttributes(I));
847 // Now remove the attribute from the correct slot. There may already be an
848 // AttributeSet there.
849 AttrBuilder B(AS, Index);
851 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
852 if (Attrs.getSlotIndex(I) == Index) {
853 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
857 AttrSet.push_back(AttributeSet::get(C, Index, B));
859 // Add the remaining attribute slots.
860 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
861 AttrSet.push_back(getSlotAttributes(I));
863 return get(C, AttrSet);
866 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
867 const AttrBuilder &Attrs) const {
868 if (!pImpl) return AttributeSet();
870 // FIXME it is not obvious how this should work for alignment.
871 // For now, say we can't pass in alignment, which no current use does.
872 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
874 // Add the attribute slots before the one we're trying to add.
875 SmallVector<AttributeSet, 4> AttrSet;
876 uint64_t NumAttrs = pImpl->getNumAttributes();
878 uint64_t LastIndex = 0;
879 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
880 if (getSlotIndex(I) >= Index) {
881 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
885 AttrSet.push_back(getSlotAttributes(I));
888 // Now remove the attribute from the correct slot. There may already be an
889 // AttributeSet there.
890 AttrBuilder B(AS, Index);
893 AttrSet.push_back(AttributeSet::get(C, Index, B));
895 // Add the remaining attribute slots.
896 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
897 AttrSet.push_back(getSlotAttributes(I));
899 return get(C, AttrSet);
902 AttributeSet AttributeSet::addDereferenceableAttr(LLVMContext &C, unsigned Index,
903 uint64_t Bytes) const {
905 B.addDereferenceableAttr(Bytes);
906 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
909 AttributeSet AttributeSet::addDereferenceableOrNullAttr(LLVMContext &C,
911 uint64_t Bytes) const {
913 B.addDereferenceableOrNullAttr(Bytes);
914 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
917 //===----------------------------------------------------------------------===//
918 // AttributeSet Accessor Methods
919 //===----------------------------------------------------------------------===//
921 LLVMContext &AttributeSet::getContext() const {
922 return pImpl->getContext();
925 AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
926 return pImpl && hasAttributes(Index) ?
927 AttributeSet::get(pImpl->getContext(),
928 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
929 std::make_pair(Index, getAttributes(Index)))) :
933 AttributeSet AttributeSet::getRetAttributes() const {
934 return pImpl && hasAttributes(ReturnIndex) ?
935 AttributeSet::get(pImpl->getContext(),
936 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
937 std::make_pair(ReturnIndex,
938 getAttributes(ReturnIndex)))) :
942 AttributeSet AttributeSet::getFnAttributes() const {
943 return pImpl && hasAttributes(FunctionIndex) ?
944 AttributeSet::get(pImpl->getContext(),
945 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
946 std::make_pair(FunctionIndex,
947 getAttributes(FunctionIndex)))) :
951 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
952 AttributeSetNode *ASN = getAttributes(Index);
953 return ASN ? ASN->hasAttribute(Kind) : false;
956 bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
957 AttributeSetNode *ASN = getAttributes(Index);
958 return ASN ? ASN->hasAttribute(Kind) : false;
961 bool AttributeSet::hasAttributes(unsigned Index) const {
962 AttributeSetNode *ASN = getAttributes(Index);
963 return ASN ? ASN->hasAttributes() : false;
966 /// \brief Return true if the specified attribute is set for at least one
967 /// parameter or for the return value.
968 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
969 if (!pImpl) return false;
971 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
972 for (AttributeSetImpl::iterator II = pImpl->begin(I),
973 IE = pImpl->end(I); II != IE; ++II)
974 if (II->hasAttribute(Attr))
980 Attribute AttributeSet::getAttribute(unsigned Index,
981 Attribute::AttrKind Kind) const {
982 AttributeSetNode *ASN = getAttributes(Index);
983 return ASN ? ASN->getAttribute(Kind) : Attribute();
986 Attribute AttributeSet::getAttribute(unsigned Index,
987 StringRef Kind) const {
988 AttributeSetNode *ASN = getAttributes(Index);
989 return ASN ? ASN->getAttribute(Kind) : Attribute();
992 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
993 AttributeSetNode *ASN = getAttributes(Index);
994 return ASN ? ASN->getAlignment() : 0;
997 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
998 AttributeSetNode *ASN = getAttributes(Index);
999 return ASN ? ASN->getStackAlignment() : 0;
1002 uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
1003 AttributeSetNode *ASN = getAttributes(Index);
1004 return ASN ? ASN->getDereferenceableBytes() : 0;
1007 uint64_t AttributeSet::getDereferenceableOrNullBytes(unsigned Index) const {
1008 AttributeSetNode *ASN = getAttributes(Index);
1009 return ASN ? ASN->getDereferenceableOrNullBytes() : 0;
1012 std::string AttributeSet::getAsString(unsigned Index,
1013 bool InAttrGrp) const {
1014 AttributeSetNode *ASN = getAttributes(Index);
1015 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
1018 /// \brief The attributes for the specified index are returned.
1019 AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
1020 if (!pImpl) return nullptr;
1022 // Loop through to find the attribute node we want.
1023 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
1024 if (pImpl->getSlotIndex(I) == Index)
1025 return pImpl->getSlotNode(I);
1030 AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
1032 return ArrayRef<Attribute>().begin();
1033 return pImpl->begin(Slot);
1036 AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
1038 return ArrayRef<Attribute>().end();
1039 return pImpl->end(Slot);
1042 //===----------------------------------------------------------------------===//
1043 // AttributeSet Introspection Methods
1044 //===----------------------------------------------------------------------===//
1046 /// \brief Return the number of slots used in this attribute list. This is the
1047 /// number of arguments that have an attribute set on them (including the
1048 /// function itself).
1049 unsigned AttributeSet::getNumSlots() const {
1050 return pImpl ? pImpl->getNumAttributes() : 0;
1053 unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
1054 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1055 "Slot # out of range!");
1056 return pImpl->getSlotIndex(Slot);
1059 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
1060 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1061 "Slot # out of range!");
1062 return pImpl->getSlotAttributes(Slot);
1065 uint64_t AttributeSet::Raw(unsigned Index) const {
1066 // FIXME: Remove this.
1067 return pImpl ? pImpl->Raw(Index) : 0;
1070 void AttributeSet::dump() const {
1073 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1074 uint64_t Index = getSlotIndex(i);
1080 dbgs() << " => " << getAsString(Index) << " }\n";
1086 //===----------------------------------------------------------------------===//
1087 // AttrBuilder Method Implementations
1088 //===----------------------------------------------------------------------===//
1090 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
1091 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
1092 DerefOrNullBytes(0) {
1093 AttributeSetImpl *pImpl = AS.pImpl;
1096 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
1097 if (pImpl->getSlotIndex(I) != Index) continue;
1099 for (AttributeSetImpl::iterator II = pImpl->begin(I),
1100 IE = pImpl->end(I); II != IE; ++II)
1107 void AttrBuilder::clear() {
1109 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
1112 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
1113 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1114 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
1115 Val != Attribute::Dereferenceable &&
1116 "Adding integer attribute without adding a value!");
1121 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
1122 if (Attr.isStringAttribute()) {
1123 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1127 Attribute::AttrKind Kind = Attr.getKindAsEnum();
1130 if (Kind == Attribute::Alignment)
1131 Alignment = Attr.getAlignment();
1132 else if (Kind == Attribute::StackAlignment)
1133 StackAlignment = Attr.getStackAlignment();
1134 else if (Kind == Attribute::Dereferenceable)
1135 DerefBytes = Attr.getDereferenceableBytes();
1136 else if (Kind == Attribute::DereferenceableOrNull)
1137 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
1141 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1142 TargetDepAttrs[A] = V;
1146 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
1147 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1150 if (Val == Attribute::Alignment)
1152 else if (Val == Attribute::StackAlignment)
1154 else if (Val == Attribute::Dereferenceable)
1156 else if (Val == Attribute::DereferenceableOrNull)
1157 DerefOrNullBytes = 0;
1162 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
1163 unsigned Slot = ~0U;
1164 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1165 if (A.getSlotIndex(I) == Index) {
1170 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
1172 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
1173 Attribute Attr = *I;
1174 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1175 Attribute::AttrKind Kind = I->getKindAsEnum();
1176 Attrs[Kind] = false;
1178 if (Kind == Attribute::Alignment)
1180 else if (Kind == Attribute::StackAlignment)
1182 else if (Kind == Attribute::Dereferenceable)
1184 else if (Kind == Attribute::DereferenceableOrNull)
1185 DerefOrNullBytes = 0;
1187 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1188 std::map<std::string, std::string>::iterator
1189 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1190 if (Iter != TargetDepAttrs.end())
1191 TargetDepAttrs.erase(Iter);
1198 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1199 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1200 if (I != TargetDepAttrs.end())
1201 TargetDepAttrs.erase(I);
1205 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
1206 if (Align == 0) return *this;
1208 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1209 assert(Align <= 0x40000000 && "Alignment too large.");
1211 Attrs[Attribute::Alignment] = true;
1216 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1217 // Default alignment, allow the target to define how to align it.
1218 if (Align == 0) return *this;
1220 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1221 assert(Align <= 0x100 && "Alignment too large.");
1223 Attrs[Attribute::StackAlignment] = true;
1224 StackAlignment = Align;
1228 AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1229 if (Bytes == 0) return *this;
1231 Attrs[Attribute::Dereferenceable] = true;
1236 AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1240 Attrs[Attribute::DereferenceableOrNull] = true;
1241 DerefOrNullBytes = Bytes;
1245 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1246 // FIXME: What if both have alignments, but they don't match?!
1248 Alignment = B.Alignment;
1250 if (!StackAlignment)
1251 StackAlignment = B.StackAlignment;
1254 DerefBytes = B.DerefBytes;
1256 if (!DerefOrNullBytes)
1257 DerefOrNullBytes = B.DerefOrNullBytes;
1261 for (auto I : B.td_attrs())
1262 TargetDepAttrs[I.first] = I.second;
1267 AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1268 // FIXME: What if both have alignments, but they don't match?!
1272 if (B.StackAlignment)
1278 if (B.DerefOrNullBytes)
1279 DerefOrNullBytes = 0;
1283 for (auto I : B.td_attrs())
1284 TargetDepAttrs.erase(I.first);
1289 bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1290 // First check if any of the target independent attributes overlap.
1291 if ((Attrs & B.Attrs).any())
1294 // Then check if any target dependent ones do.
1295 for (auto I : td_attrs())
1296 if (B.contains(I.first))
1302 bool AttrBuilder::contains(StringRef A) const {
1303 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1306 bool AttrBuilder::hasAttributes() const {
1307 return !Attrs.none() || !TargetDepAttrs.empty();
1310 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
1311 unsigned Slot = ~0U;
1312 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1313 if (A.getSlotIndex(I) == Index) {
1318 assert(Slot != ~0U && "Couldn't find the index!");
1320 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
1322 Attribute Attr = *I;
1323 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1324 if (Attrs[I->getKindAsEnum()])
1327 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1328 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1335 bool AttrBuilder::hasAlignmentAttr() const {
1336 return Alignment != 0;
1339 bool AttrBuilder::operator==(const AttrBuilder &B) {
1340 if (Attrs != B.Attrs)
1343 for (td_const_iterator I = TargetDepAttrs.begin(),
1344 E = TargetDepAttrs.end(); I != E; ++I)
1345 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1348 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1349 DerefBytes == B.DerefBytes;
1352 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
1353 // FIXME: Remove this in 4.0.
1354 if (!Val) return *this;
1356 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1357 I = Attribute::AttrKind(I + 1)) {
1358 if (I == Attribute::Dereferenceable ||
1359 I == Attribute::DereferenceableOrNull)
1361 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1364 if (I == Attribute::Alignment)
1365 Alignment = 1ULL << ((A >> 16) - 1);
1366 else if (I == Attribute::StackAlignment)
1367 StackAlignment = 1ULL << ((A >> 26)-1);
1374 //===----------------------------------------------------------------------===//
1375 // AttributeFuncs Function Defintions
1376 //===----------------------------------------------------------------------===//
1378 /// \brief Which attributes cannot be applied to a type.
1379 AttrBuilder AttributeFuncs::typeIncompatible(const Type *Ty) {
1380 AttrBuilder Incompatible;
1382 if (!Ty->isIntegerTy())
1383 // Attribute that only apply to integers.
1384 Incompatible.addAttribute(Attribute::SExt)
1385 .addAttribute(Attribute::ZExt);
1387 if (!Ty->isPointerTy())
1388 // Attribute that only apply to pointers.
1389 Incompatible.addAttribute(Attribute::ByVal)
1390 .addAttribute(Attribute::Nest)
1391 .addAttribute(Attribute::NoAlias)
1392 .addAttribute(Attribute::NoCapture)
1393 .addAttribute(Attribute::NonNull)
1394 .addDereferenceableAttr(1) // the int here is ignored
1395 .addDereferenceableOrNullAttr(1) // the int here is ignored
1396 .addAttribute(Attribute::ReadNone)
1397 .addAttribute(Attribute::ReadOnly)
1398 .addAttribute(Attribute::StructRet)
1399 .addAttribute(Attribute::InAlloca);
1401 return Incompatible;