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::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::NoUnwind))
237 if (hasAttribute(Attribute::OptimizeNone))
239 if (hasAttribute(Attribute::OptimizeForSize))
241 if (hasAttribute(Attribute::ReadNone))
243 if (hasAttribute(Attribute::ReadOnly))
245 if (hasAttribute(Attribute::Returned))
247 if (hasAttribute(Attribute::ReturnsTwice))
248 return "returns_twice";
249 if (hasAttribute(Attribute::SExt))
251 if (hasAttribute(Attribute::StackProtect))
253 if (hasAttribute(Attribute::StackProtectReq))
255 if (hasAttribute(Attribute::StackProtectStrong))
257 if (hasAttribute(Attribute::SafeStack))
259 if (hasAttribute(Attribute::StructRet))
261 if (hasAttribute(Attribute::SanitizeThread))
262 return "sanitize_thread";
263 if (hasAttribute(Attribute::SanitizeMemory))
264 return "sanitize_memory";
265 if (hasAttribute(Attribute::UWTable))
267 if (hasAttribute(Attribute::ZExt))
269 if (hasAttribute(Attribute::Cold))
272 // FIXME: These should be output like this:
277 if (hasAttribute(Attribute::Alignment)) {
280 Result += (InAttrGrp) ? "=" : " ";
281 Result += utostr(getValueAsInt());
285 auto AttrWithBytesToString = [&](const char *Name) {
290 Result += utostr(getValueAsInt());
293 Result += utostr(getValueAsInt());
299 if (hasAttribute(Attribute::StackAlignment))
300 return AttrWithBytesToString("alignstack");
302 if (hasAttribute(Attribute::Dereferenceable))
303 return AttrWithBytesToString("dereferenceable");
305 if (hasAttribute(Attribute::DereferenceableOrNull))
306 return AttrWithBytesToString("dereferenceable_or_null");
308 // Convert target-dependent attributes to strings of the form:
313 if (isStringAttribute()) {
315 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
317 StringRef Val = pImpl->getValueAsString();
318 if (Val.empty()) return Result;
320 Result += ("=\"" + Val + Twine('"')).str();
324 llvm_unreachable("Unknown attribute");
327 bool Attribute::operator<(Attribute A) const {
328 if (!pImpl && !A.pImpl) return false;
329 if (!pImpl) return true;
330 if (!A.pImpl) return false;
331 return *pImpl < *A.pImpl;
334 //===----------------------------------------------------------------------===//
335 // AttributeImpl Definition
336 //===----------------------------------------------------------------------===//
338 // Pin the vtables to this file.
339 AttributeImpl::~AttributeImpl() {}
340 void EnumAttributeImpl::anchor() {}
341 void IntAttributeImpl::anchor() {}
342 void StringAttributeImpl::anchor() {}
344 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
345 if (isStringAttribute()) return false;
346 return getKindAsEnum() == A;
349 bool AttributeImpl::hasAttribute(StringRef Kind) const {
350 if (!isStringAttribute()) return false;
351 return getKindAsString() == Kind;
354 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
355 assert(isEnumAttribute() || isIntAttribute());
356 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
359 uint64_t AttributeImpl::getValueAsInt() const {
360 assert(isIntAttribute());
361 return static_cast<const IntAttributeImpl *>(this)->getValue();
364 StringRef AttributeImpl::getKindAsString() const {
365 assert(isStringAttribute());
366 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
369 StringRef AttributeImpl::getValueAsString() const {
370 assert(isStringAttribute());
371 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
374 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
375 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
376 // relative to their enum value) and then strings.
377 if (isEnumAttribute()) {
378 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
379 if (AI.isIntAttribute()) return true;
380 if (AI.isStringAttribute()) return true;
383 if (isIntAttribute()) {
384 if (AI.isEnumAttribute()) return false;
385 if (AI.isIntAttribute()) return getValueAsInt() < AI.getValueAsInt();
386 if (AI.isStringAttribute()) return true;
389 if (AI.isEnumAttribute()) return false;
390 if (AI.isIntAttribute()) return false;
391 if (getKindAsString() == AI.getKindAsString())
392 return getValueAsString() < AI.getValueAsString();
393 return getKindAsString() < AI.getKindAsString();
396 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
397 // FIXME: Remove this.
399 case Attribute::EndAttrKinds:
400 llvm_unreachable("Synthetic enumerators which should never get here");
402 case Attribute::None: return 0;
403 case Attribute::ZExt: return 1 << 0;
404 case Attribute::SExt: return 1 << 1;
405 case Attribute::NoReturn: return 1 << 2;
406 case Attribute::InReg: return 1 << 3;
407 case Attribute::StructRet: return 1 << 4;
408 case Attribute::NoUnwind: return 1 << 5;
409 case Attribute::NoAlias: return 1 << 6;
410 case Attribute::ByVal: return 1 << 7;
411 case Attribute::Nest: return 1 << 8;
412 case Attribute::ReadNone: return 1 << 9;
413 case Attribute::ReadOnly: return 1 << 10;
414 case Attribute::NoInline: return 1 << 11;
415 case Attribute::AlwaysInline: return 1 << 12;
416 case Attribute::OptimizeForSize: return 1 << 13;
417 case Attribute::StackProtect: return 1 << 14;
418 case Attribute::StackProtectReq: return 1 << 15;
419 case Attribute::Alignment: return 31 << 16;
420 case Attribute::NoCapture: return 1 << 21;
421 case Attribute::NoRedZone: return 1 << 22;
422 case Attribute::NoImplicitFloat: return 1 << 23;
423 case Attribute::Naked: return 1 << 24;
424 case Attribute::InlineHint: return 1 << 25;
425 case Attribute::StackAlignment: return 7 << 26;
426 case Attribute::ReturnsTwice: return 1 << 29;
427 case Attribute::UWTable: return 1 << 30;
428 case Attribute::NonLazyBind: return 1U << 31;
429 case Attribute::SanitizeAddress: return 1ULL << 32;
430 case Attribute::MinSize: return 1ULL << 33;
431 case Attribute::NoDuplicate: return 1ULL << 34;
432 case Attribute::StackProtectStrong: return 1ULL << 35;
433 case Attribute::SanitizeThread: return 1ULL << 36;
434 case Attribute::SanitizeMemory: return 1ULL << 37;
435 case Attribute::NoBuiltin: return 1ULL << 38;
436 case Attribute::Returned: return 1ULL << 39;
437 case Attribute::Cold: return 1ULL << 40;
438 case Attribute::Builtin: return 1ULL << 41;
439 case Attribute::OptimizeNone: return 1ULL << 42;
440 case Attribute::InAlloca: return 1ULL << 43;
441 case Attribute::NonNull: return 1ULL << 44;
442 case Attribute::JumpTable: return 1ULL << 45;
443 case Attribute::Convergent: return 1ULL << 46;
444 case Attribute::SafeStack: return 1ULL << 47;
445 case Attribute::Dereferenceable:
446 llvm_unreachable("dereferenceable attribute not supported in raw format");
448 case Attribute::DereferenceableOrNull:
449 llvm_unreachable("dereferenceable_or_null attribute not supported in raw "
452 case Attribute::ArgMemOnly:
453 llvm_unreachable("argmemonly attribute not supported in raw format");
456 llvm_unreachable("Unsupported attribute type");
459 //===----------------------------------------------------------------------===//
460 // AttributeSetNode Definition
461 //===----------------------------------------------------------------------===//
463 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
464 ArrayRef<Attribute> Attrs) {
468 // Otherwise, build a key to look up the existing attributes.
469 LLVMContextImpl *pImpl = C.pImpl;
472 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
473 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
475 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
476 E = SortedAttrs.end(); I != E; ++I)
480 AttributeSetNode *PA =
481 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
483 // If we didn't find any existing attributes of the same shape then create a
484 // new one and insert it.
486 // Coallocate entries after the AttributeSetNode itself.
487 void *Mem = ::operator new(sizeof(AttributeSetNode) +
488 sizeof(Attribute) * SortedAttrs.size());
489 PA = new (Mem) AttributeSetNode(SortedAttrs);
490 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
493 // Return the AttributesListNode that we found or created.
497 bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
498 for (iterator I = begin(), E = end(); I != E; ++I)
499 if (I->hasAttribute(Kind))
504 bool AttributeSetNode::hasAttribute(StringRef Kind) const {
505 for (iterator I = begin(), E = end(); I != E; ++I)
506 if (I->hasAttribute(Kind))
511 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
512 for (iterator I = begin(), E = end(); I != E; ++I)
513 if (I->hasAttribute(Kind))
518 Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
519 for (iterator I = begin(), E = end(); I != E; ++I)
520 if (I->hasAttribute(Kind))
525 unsigned AttributeSetNode::getAlignment() const {
526 for (iterator I = begin(), E = end(); I != E; ++I)
527 if (I->hasAttribute(Attribute::Alignment))
528 return I->getAlignment();
532 unsigned AttributeSetNode::getStackAlignment() const {
533 for (iterator I = begin(), E = end(); I != E; ++I)
534 if (I->hasAttribute(Attribute::StackAlignment))
535 return I->getStackAlignment();
539 uint64_t AttributeSetNode::getDereferenceableBytes() const {
540 for (iterator I = begin(), E = end(); I != E; ++I)
541 if (I->hasAttribute(Attribute::Dereferenceable))
542 return I->getDereferenceableBytes();
546 uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
547 for (iterator I = begin(), E = end(); I != E; ++I)
548 if (I->hasAttribute(Attribute::DereferenceableOrNull))
549 return I->getDereferenceableOrNullBytes();
553 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
555 for (iterator I = begin(), E = end(); I != E; ++I) {
558 Str += I->getAsString(InAttrGrp);
563 //===----------------------------------------------------------------------===//
564 // AttributeSetImpl Definition
565 //===----------------------------------------------------------------------===//
567 uint64_t AttributeSetImpl::Raw(unsigned Index) const {
568 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
569 if (getSlotIndex(I) != Index) continue;
570 const AttributeSetNode *ASN = getSlotNode(I);
573 for (AttributeSetNode::iterator II = ASN->begin(),
574 IE = ASN->end(); II != IE; ++II) {
575 Attribute Attr = *II;
577 // This cannot handle string attributes.
578 if (Attr.isStringAttribute()) continue;
580 Attribute::AttrKind Kind = Attr.getKindAsEnum();
582 if (Kind == Attribute::Alignment)
583 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
584 else if (Kind == Attribute::StackAlignment)
585 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
586 else if (Kind == Attribute::Dereferenceable)
587 llvm_unreachable("dereferenceable not supported in bit mask");
589 Mask |= AttributeImpl::getAttrMask(Kind);
598 void AttributeSetImpl::dump() const {
599 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
602 //===----------------------------------------------------------------------===//
603 // AttributeSet Construction and Mutation Methods
604 //===----------------------------------------------------------------------===//
607 AttributeSet::getImpl(LLVMContext &C,
608 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
609 LLVMContextImpl *pImpl = C.pImpl;
611 AttributeSetImpl::Profile(ID, Attrs);
614 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
616 // If we didn't find any existing attributes of the same shape then
617 // create a new one and insert it.
619 // Coallocate entries after the AttributeSetImpl itself.
620 void *Mem = ::operator new(sizeof(AttributeSetImpl) +
621 sizeof(std::pair<unsigned, AttributeSetNode *>) *
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))
687 if (Kind == Attribute::Alignment)
688 Attrs.push_back(std::make_pair(Index, Attribute::
689 getWithAlignment(C, B.getAlignment())));
690 else if (Kind == Attribute::StackAlignment)
691 Attrs.push_back(std::make_pair(Index, Attribute::
692 getWithStackAlignment(C, B.getStackAlignment())));
693 else if (Kind == Attribute::Dereferenceable)
694 Attrs.push_back(std::make_pair(Index,
695 Attribute::getWithDereferenceableBytes(C,
696 B.getDereferenceableBytes())));
697 else if (Kind == Attribute::DereferenceableOrNull)
699 std::make_pair(Index, Attribute::getWithDereferenceableOrNullBytes(
700 C, B.getDereferenceableOrNullBytes())));
702 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
705 // Add target-dependent (string) attributes.
706 for (const AttrBuilder::td_type &TDA : B.td_attrs())
708 std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
710 return get(C, Attrs);
713 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
714 ArrayRef<Attribute::AttrKind> Kind) {
715 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
716 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
717 E = Kind.end(); I != E; ++I)
718 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
719 return get(C, Attrs);
722 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
723 if (Attrs.empty()) return AttributeSet();
724 if (Attrs.size() == 1) return Attrs[0];
726 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
727 AttributeSetImpl *A0 = Attrs[0].pImpl;
729 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
730 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
731 // ordered by index. Because we know that each list in Attrs is ordered by
732 // index we only need to merge each successive list in rather than doing a
734 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
735 AttributeSetImpl *AS = Attrs[I].pImpl;
737 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
738 ANVI = AttrNodeVec.begin(), ANVE;
739 for (const AttributeSetImpl::IndexAttrPair
740 *AI = AS->getNode(0),
741 *AE = AS->getNode(AS->getNumAttributes());
743 ANVE = AttrNodeVec.end();
744 while (ANVI != ANVE && ANVI->first <= AI->first)
746 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
750 return getImpl(C, AttrNodeVec);
753 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
754 Attribute::AttrKind Attr) const {
755 if (hasAttribute(Index, Attr)) return *this;
756 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
759 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
760 StringRef Kind) const {
762 B.addAttribute(Kind);
763 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
766 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
767 StringRef Kind, StringRef Value) const {
769 B.addAttribute(Kind, Value);
770 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
773 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
774 AttributeSet Attrs) const {
775 if (!pImpl) return Attrs;
776 if (!Attrs.pImpl) return *this;
779 // FIXME it is not obvious how this should work for alignment. For now, say
780 // we can't change a known alignment.
781 unsigned OldAlign = getParamAlignment(Index);
782 unsigned NewAlign = Attrs.getParamAlignment(Index);
783 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
784 "Attempt to change alignment!");
787 // Add the attribute slots before the one we're trying to add.
788 SmallVector<AttributeSet, 4> AttrSet;
789 uint64_t NumAttrs = pImpl->getNumAttributes();
791 uint64_t LastIndex = 0;
792 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
793 if (getSlotIndex(I) >= Index) {
794 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
798 AttrSet.push_back(getSlotAttributes(I));
801 // Now add the attribute into the correct slot. There may already be an
802 // AttributeSet there.
803 AttrBuilder B(AS, Index);
805 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
806 if (Attrs.getSlotIndex(I) == Index) {
807 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
808 IE = Attrs.pImpl->end(I); II != IE; ++II)
813 AttrSet.push_back(AttributeSet::get(C, Index, B));
815 // Add the remaining attribute slots.
816 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
817 AttrSet.push_back(getSlotAttributes(I));
819 return get(C, AttrSet);
822 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
823 Attribute::AttrKind Attr) const {
824 if (!hasAttribute(Index, Attr)) return *this;
825 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
828 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
829 AttributeSet Attrs) const {
830 if (!pImpl) return AttributeSet();
831 if (!Attrs.pImpl) return *this;
833 // FIXME it is not obvious how this should work for alignment.
834 // For now, say we can't pass in alignment, which no current use does.
835 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
836 "Attempt to change alignment!");
838 // Add the attribute slots before the one we're trying to add.
839 SmallVector<AttributeSet, 4> AttrSet;
840 uint64_t NumAttrs = pImpl->getNumAttributes();
842 uint64_t LastIndex = 0;
843 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
844 if (getSlotIndex(I) >= Index) {
845 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
849 AttrSet.push_back(getSlotAttributes(I));
852 // Now remove the attribute from the correct slot. There may already be an
853 // AttributeSet there.
854 AttrBuilder B(AS, Index);
856 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
857 if (Attrs.getSlotIndex(I) == Index) {
858 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
862 AttrSet.push_back(AttributeSet::get(C, Index, B));
864 // Add the remaining attribute slots.
865 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
866 AttrSet.push_back(getSlotAttributes(I));
868 return get(C, AttrSet);
871 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
872 const AttrBuilder &Attrs) const {
873 if (!pImpl) return AttributeSet();
875 // FIXME it is not obvious how this should work for alignment.
876 // For now, say we can't pass in alignment, which no current use does.
877 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
879 // Add the attribute slots before the one we're trying to add.
880 SmallVector<AttributeSet, 4> AttrSet;
881 uint64_t NumAttrs = pImpl->getNumAttributes();
883 uint64_t LastIndex = 0;
884 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
885 if (getSlotIndex(I) >= Index) {
886 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
890 AttrSet.push_back(getSlotAttributes(I));
893 // Now remove the attribute from the correct slot. There may already be an
894 // AttributeSet there.
895 AttrBuilder B(AS, Index);
898 AttrSet.push_back(AttributeSet::get(C, Index, B));
900 // Add the remaining attribute slots.
901 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
902 AttrSet.push_back(getSlotAttributes(I));
904 return get(C, AttrSet);
907 AttributeSet AttributeSet::addDereferenceableAttr(LLVMContext &C, unsigned Index,
908 uint64_t Bytes) const {
910 B.addDereferenceableAttr(Bytes);
911 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
914 AttributeSet AttributeSet::addDereferenceableOrNullAttr(LLVMContext &C,
916 uint64_t Bytes) const {
918 B.addDereferenceableOrNullAttr(Bytes);
919 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
922 //===----------------------------------------------------------------------===//
923 // AttributeSet Accessor Methods
924 //===----------------------------------------------------------------------===//
926 LLVMContext &AttributeSet::getContext() const {
927 return pImpl->getContext();
930 AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
931 return pImpl && hasAttributes(Index) ?
932 AttributeSet::get(pImpl->getContext(),
933 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
934 std::make_pair(Index, getAttributes(Index)))) :
938 AttributeSet AttributeSet::getRetAttributes() const {
939 return pImpl && hasAttributes(ReturnIndex) ?
940 AttributeSet::get(pImpl->getContext(),
941 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
942 std::make_pair(ReturnIndex,
943 getAttributes(ReturnIndex)))) :
947 AttributeSet AttributeSet::getFnAttributes() const {
948 return pImpl && hasAttributes(FunctionIndex) ?
949 AttributeSet::get(pImpl->getContext(),
950 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
951 std::make_pair(FunctionIndex,
952 getAttributes(FunctionIndex)))) :
956 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
957 AttributeSetNode *ASN = getAttributes(Index);
958 return ASN ? ASN->hasAttribute(Kind) : false;
961 bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
962 AttributeSetNode *ASN = getAttributes(Index);
963 return ASN ? ASN->hasAttribute(Kind) : false;
966 bool AttributeSet::hasAttributes(unsigned Index) const {
967 AttributeSetNode *ASN = getAttributes(Index);
968 return ASN ? ASN->hasAttributes() : false;
971 /// \brief Return true if the specified attribute is set for at least one
972 /// parameter or for the return value.
973 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
974 if (!pImpl) return false;
976 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
977 for (AttributeSetImpl::iterator II = pImpl->begin(I),
978 IE = pImpl->end(I); II != IE; ++II)
979 if (II->hasAttribute(Attr))
985 Attribute AttributeSet::getAttribute(unsigned Index,
986 Attribute::AttrKind Kind) const {
987 AttributeSetNode *ASN = getAttributes(Index);
988 return ASN ? ASN->getAttribute(Kind) : Attribute();
991 Attribute AttributeSet::getAttribute(unsigned Index,
992 StringRef Kind) const {
993 AttributeSetNode *ASN = getAttributes(Index);
994 return ASN ? ASN->getAttribute(Kind) : Attribute();
997 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
998 AttributeSetNode *ASN = getAttributes(Index);
999 return ASN ? ASN->getAlignment() : 0;
1002 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
1003 AttributeSetNode *ASN = getAttributes(Index);
1004 return ASN ? ASN->getStackAlignment() : 0;
1007 uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
1008 AttributeSetNode *ASN = getAttributes(Index);
1009 return ASN ? ASN->getDereferenceableBytes() : 0;
1012 uint64_t AttributeSet::getDereferenceableOrNullBytes(unsigned Index) const {
1013 AttributeSetNode *ASN = getAttributes(Index);
1014 return ASN ? ASN->getDereferenceableOrNullBytes() : 0;
1017 std::string AttributeSet::getAsString(unsigned Index,
1018 bool InAttrGrp) const {
1019 AttributeSetNode *ASN = getAttributes(Index);
1020 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
1023 /// \brief The attributes for the specified index are returned.
1024 AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
1025 if (!pImpl) return nullptr;
1027 // Loop through to find the attribute node we want.
1028 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
1029 if (pImpl->getSlotIndex(I) == Index)
1030 return pImpl->getSlotNode(I);
1035 AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
1037 return ArrayRef<Attribute>().begin();
1038 return pImpl->begin(Slot);
1041 AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
1043 return ArrayRef<Attribute>().end();
1044 return pImpl->end(Slot);
1047 //===----------------------------------------------------------------------===//
1048 // AttributeSet Introspection Methods
1049 //===----------------------------------------------------------------------===//
1051 /// \brief Return the number of slots used in this attribute list. This is the
1052 /// number of arguments that have an attribute set on them (including the
1053 /// function itself).
1054 unsigned AttributeSet::getNumSlots() const {
1055 return pImpl ? pImpl->getNumAttributes() : 0;
1058 unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
1059 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1060 "Slot # out of range!");
1061 return pImpl->getSlotIndex(Slot);
1064 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
1065 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1066 "Slot # out of range!");
1067 return pImpl->getSlotAttributes(Slot);
1070 uint64_t AttributeSet::Raw(unsigned Index) const {
1071 // FIXME: Remove this.
1072 return pImpl ? pImpl->Raw(Index) : 0;
1075 void AttributeSet::dump() const {
1078 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1079 uint64_t Index = getSlotIndex(i);
1085 dbgs() << " => " << getAsString(Index) << " }\n";
1091 //===----------------------------------------------------------------------===//
1092 // AttrBuilder Method Implementations
1093 //===----------------------------------------------------------------------===//
1095 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
1096 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
1097 DerefOrNullBytes(0) {
1098 AttributeSetImpl *pImpl = AS.pImpl;
1101 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
1102 if (pImpl->getSlotIndex(I) != Index) continue;
1104 for (AttributeSetImpl::iterator II = pImpl->begin(I),
1105 IE = pImpl->end(I); II != IE; ++II)
1112 void AttrBuilder::clear() {
1114 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
1117 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
1118 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1119 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
1120 Val != Attribute::Dereferenceable &&
1121 "Adding integer attribute without adding a value!");
1126 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
1127 if (Attr.isStringAttribute()) {
1128 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1132 Attribute::AttrKind Kind = Attr.getKindAsEnum();
1135 if (Kind == Attribute::Alignment)
1136 Alignment = Attr.getAlignment();
1137 else if (Kind == Attribute::StackAlignment)
1138 StackAlignment = Attr.getStackAlignment();
1139 else if (Kind == Attribute::Dereferenceable)
1140 DerefBytes = Attr.getDereferenceableBytes();
1141 else if (Kind == Attribute::DereferenceableOrNull)
1142 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
1146 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1147 TargetDepAttrs[A] = V;
1151 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
1152 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1155 if (Val == Attribute::Alignment)
1157 else if (Val == Attribute::StackAlignment)
1159 else if (Val == Attribute::Dereferenceable)
1161 else if (Val == Attribute::DereferenceableOrNull)
1162 DerefOrNullBytes = 0;
1167 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
1168 unsigned Slot = ~0U;
1169 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1170 if (A.getSlotIndex(I) == Index) {
1175 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
1177 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
1178 Attribute Attr = *I;
1179 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1180 Attribute::AttrKind Kind = I->getKindAsEnum();
1181 Attrs[Kind] = false;
1183 if (Kind == Attribute::Alignment)
1185 else if (Kind == Attribute::StackAlignment)
1187 else if (Kind == Attribute::Dereferenceable)
1189 else if (Kind == Attribute::DereferenceableOrNull)
1190 DerefOrNullBytes = 0;
1192 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1193 std::map<std::string, std::string>::iterator
1194 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1195 if (Iter != TargetDepAttrs.end())
1196 TargetDepAttrs.erase(Iter);
1203 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1204 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1205 if (I != TargetDepAttrs.end())
1206 TargetDepAttrs.erase(I);
1210 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
1211 if (Align == 0) return *this;
1213 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1214 assert(Align <= 0x40000000 && "Alignment too large.");
1216 Attrs[Attribute::Alignment] = true;
1221 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1222 // Default alignment, allow the target to define how to align it.
1223 if (Align == 0) return *this;
1225 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1226 assert(Align <= 0x100 && "Alignment too large.");
1228 Attrs[Attribute::StackAlignment] = true;
1229 StackAlignment = Align;
1233 AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1234 if (Bytes == 0) return *this;
1236 Attrs[Attribute::Dereferenceable] = true;
1241 AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1245 Attrs[Attribute::DereferenceableOrNull] = true;
1246 DerefOrNullBytes = Bytes;
1250 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1251 // FIXME: What if both have alignments, but they don't match?!
1253 Alignment = B.Alignment;
1255 if (!StackAlignment)
1256 StackAlignment = B.StackAlignment;
1259 DerefBytes = B.DerefBytes;
1261 if (!DerefOrNullBytes)
1262 DerefOrNullBytes = B.DerefOrNullBytes;
1266 for (auto I : B.td_attrs())
1267 TargetDepAttrs[I.first] = I.second;
1272 AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1273 // FIXME: What if both have alignments, but they don't match?!
1277 if (B.StackAlignment)
1283 if (B.DerefOrNullBytes)
1284 DerefOrNullBytes = 0;
1288 for (auto I : B.td_attrs())
1289 TargetDepAttrs.erase(I.first);
1294 bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1295 // First check if any of the target independent attributes overlap.
1296 if ((Attrs & B.Attrs).any())
1299 // Then check if any target dependent ones do.
1300 for (auto I : td_attrs())
1301 if (B.contains(I.first))
1307 bool AttrBuilder::contains(StringRef A) const {
1308 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1311 bool AttrBuilder::hasAttributes() const {
1312 return !Attrs.none() || !TargetDepAttrs.empty();
1315 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
1316 unsigned Slot = ~0U;
1317 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1318 if (A.getSlotIndex(I) == Index) {
1323 assert(Slot != ~0U && "Couldn't find the index!");
1325 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
1327 Attribute Attr = *I;
1328 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1329 if (Attrs[I->getKindAsEnum()])
1332 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1333 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1340 bool AttrBuilder::hasAlignmentAttr() const {
1341 return Alignment != 0;
1344 bool AttrBuilder::operator==(const AttrBuilder &B) {
1345 if (Attrs != B.Attrs)
1348 for (td_const_iterator I = TargetDepAttrs.begin(),
1349 E = TargetDepAttrs.end(); I != E; ++I)
1350 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1353 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1354 DerefBytes == B.DerefBytes;
1357 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
1358 // FIXME: Remove this in 4.0.
1359 if (!Val) return *this;
1361 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1362 I = Attribute::AttrKind(I + 1)) {
1363 if (I == Attribute::Dereferenceable ||
1364 I == Attribute::DereferenceableOrNull ||
1365 I == Attribute::ArgMemOnly)
1367 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1370 if (I == Attribute::Alignment)
1371 Alignment = 1ULL << ((A >> 16) - 1);
1372 else if (I == Attribute::StackAlignment)
1373 StackAlignment = 1ULL << ((A >> 26)-1);
1380 //===----------------------------------------------------------------------===//
1381 // AttributeFuncs Function Defintions
1382 //===----------------------------------------------------------------------===//
1384 /// \brief Which attributes cannot be applied to a type.
1385 AttrBuilder AttributeFuncs::typeIncompatible(const Type *Ty) {
1386 AttrBuilder Incompatible;
1388 if (!Ty->isIntegerTy())
1389 // Attribute that only apply to integers.
1390 Incompatible.addAttribute(Attribute::SExt)
1391 .addAttribute(Attribute::ZExt);
1393 if (!Ty->isPointerTy())
1394 // Attribute that only apply to pointers.
1395 Incompatible.addAttribute(Attribute::ByVal)
1396 .addAttribute(Attribute::Nest)
1397 .addAttribute(Attribute::NoAlias)
1398 .addAttribute(Attribute::NoCapture)
1399 .addAttribute(Attribute::NonNull)
1400 .addDereferenceableAttr(1) // the int here is ignored
1401 .addDereferenceableOrNullAttr(1) // the int here is ignored
1402 .addAttribute(Attribute::ReadNone)
1403 .addAttribute(Attribute::ReadOnly)
1404 .addAttribute(Attribute::StructRet)
1405 .addAttribute(Attribute::InAlloca);
1407 return Incompatible;