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::StructRet))
257 if (hasAttribute(Attribute::SanitizeThread))
258 return "sanitize_thread";
259 if (hasAttribute(Attribute::SanitizeMemory))
260 return "sanitize_memory";
261 if (hasAttribute(Attribute::UWTable))
263 if (hasAttribute(Attribute::ZExt))
265 if (hasAttribute(Attribute::Cold))
268 // FIXME: These should be output like this:
273 if (hasAttribute(Attribute::Alignment)) {
276 Result += (InAttrGrp) ? "=" : " ";
277 Result += utostr(getValueAsInt());
281 auto AttrWithBytesToString = [&](const char *Name) {
286 Result += utostr(getValueAsInt());
289 Result += utostr(getValueAsInt());
295 if (hasAttribute(Attribute::StackAlignment))
296 return AttrWithBytesToString("alignstack");
298 if (hasAttribute(Attribute::Dereferenceable))
299 return AttrWithBytesToString("dereferenceable");
301 if (hasAttribute(Attribute::DereferenceableOrNull))
302 return AttrWithBytesToString("dereferenceable_or_null");
304 // Convert target-dependent attributes to strings of the form:
309 if (isStringAttribute()) {
311 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
313 StringRef Val = pImpl->getValueAsString();
314 if (Val.empty()) return Result;
316 Result += ("=\"" + Val + Twine('"')).str();
320 llvm_unreachable("Unknown attribute");
323 bool Attribute::operator<(Attribute A) const {
324 if (!pImpl && !A.pImpl) return false;
325 if (!pImpl) return true;
326 if (!A.pImpl) return false;
327 return *pImpl < *A.pImpl;
330 //===----------------------------------------------------------------------===//
331 // AttributeImpl Definition
332 //===----------------------------------------------------------------------===//
334 // Pin the vtables to this file.
335 AttributeImpl::~AttributeImpl() {}
336 void EnumAttributeImpl::anchor() {}
337 void IntAttributeImpl::anchor() {}
338 void StringAttributeImpl::anchor() {}
340 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
341 if (isStringAttribute()) return false;
342 return getKindAsEnum() == A;
345 bool AttributeImpl::hasAttribute(StringRef Kind) const {
346 if (!isStringAttribute()) return false;
347 return getKindAsString() == Kind;
350 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
351 assert(isEnumAttribute() || isIntAttribute());
352 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
355 uint64_t AttributeImpl::getValueAsInt() const {
356 assert(isIntAttribute());
357 return static_cast<const IntAttributeImpl *>(this)->getValue();
360 StringRef AttributeImpl::getKindAsString() const {
361 assert(isStringAttribute());
362 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
365 StringRef AttributeImpl::getValueAsString() const {
366 assert(isStringAttribute());
367 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
370 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
371 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
372 // relative to their enum value) and then strings.
373 if (isEnumAttribute()) {
374 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
375 if (AI.isIntAttribute()) return true;
376 if (AI.isStringAttribute()) return true;
379 if (isIntAttribute()) {
380 if (AI.isEnumAttribute()) return false;
381 if (AI.isIntAttribute()) return getValueAsInt() < AI.getValueAsInt();
382 if (AI.isStringAttribute()) return true;
385 if (AI.isEnumAttribute()) return false;
386 if (AI.isIntAttribute()) return false;
387 if (getKindAsString() == AI.getKindAsString())
388 return getValueAsString() < AI.getValueAsString();
389 return getKindAsString() < AI.getKindAsString();
392 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
393 // FIXME: Remove this.
395 case Attribute::EndAttrKinds:
396 llvm_unreachable("Synthetic enumerators which should never get here");
398 case Attribute::None: return 0;
399 case Attribute::ZExt: return 1 << 0;
400 case Attribute::SExt: return 1 << 1;
401 case Attribute::NoReturn: return 1 << 2;
402 case Attribute::InReg: return 1 << 3;
403 case Attribute::StructRet: return 1 << 4;
404 case Attribute::NoUnwind: return 1 << 5;
405 case Attribute::NoAlias: return 1 << 6;
406 case Attribute::ByVal: return 1 << 7;
407 case Attribute::Nest: return 1 << 8;
408 case Attribute::ReadNone: return 1 << 9;
409 case Attribute::ReadOnly: return 1 << 10;
410 case Attribute::NoInline: return 1 << 11;
411 case Attribute::AlwaysInline: return 1 << 12;
412 case Attribute::OptimizeForSize: return 1 << 13;
413 case Attribute::StackProtect: return 1 << 14;
414 case Attribute::StackProtectReq: return 1 << 15;
415 case Attribute::Alignment: return 31 << 16;
416 case Attribute::NoCapture: return 1 << 21;
417 case Attribute::NoRedZone: return 1 << 22;
418 case Attribute::NoImplicitFloat: return 1 << 23;
419 case Attribute::Naked: return 1 << 24;
420 case Attribute::InlineHint: return 1 << 25;
421 case Attribute::StackAlignment: return 7 << 26;
422 case Attribute::ReturnsTwice: return 1 << 29;
423 case Attribute::UWTable: return 1 << 30;
424 case Attribute::NonLazyBind: return 1U << 31;
425 case Attribute::SanitizeAddress: return 1ULL << 32;
426 case Attribute::MinSize: return 1ULL << 33;
427 case Attribute::NoDuplicate: return 1ULL << 34;
428 case Attribute::StackProtectStrong: return 1ULL << 35;
429 case Attribute::SanitizeThread: return 1ULL << 36;
430 case Attribute::SanitizeMemory: return 1ULL << 37;
431 case Attribute::NoBuiltin: return 1ULL << 38;
432 case Attribute::Returned: return 1ULL << 39;
433 case Attribute::Cold: return 1ULL << 40;
434 case Attribute::Builtin: return 1ULL << 41;
435 case Attribute::OptimizeNone: return 1ULL << 42;
436 case Attribute::InAlloca: return 1ULL << 43;
437 case Attribute::NonNull: return 1ULL << 44;
438 case Attribute::JumpTable: return 1ULL << 45;
439 case Attribute::Convergent: return 1ULL << 46;
440 case Attribute::Dereferenceable:
441 llvm_unreachable("dereferenceable attribute not supported in raw format");
443 case Attribute::DereferenceableOrNull:
444 llvm_unreachable("dereferenceable_or_null attribute not supported in raw "
448 llvm_unreachable("Unsupported attribute type");
451 //===----------------------------------------------------------------------===//
452 // AttributeSetNode Definition
453 //===----------------------------------------------------------------------===//
455 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
456 ArrayRef<Attribute> Attrs) {
460 // Otherwise, build a key to look up the existing attributes.
461 LLVMContextImpl *pImpl = C.pImpl;
464 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
465 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
467 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
468 E = SortedAttrs.end(); I != E; ++I)
472 AttributeSetNode *PA =
473 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
475 // If we didn't find any existing attributes of the same shape then create a
476 // new one and insert it.
478 // Coallocate entries after the AttributeSetNode itself.
479 void *Mem = ::operator new(sizeof(AttributeSetNode) +
480 sizeof(Attribute) * SortedAttrs.size());
481 PA = new (Mem) AttributeSetNode(SortedAttrs);
482 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
485 // Return the AttributesListNode that we found or created.
489 bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
490 for (iterator I = begin(), E = end(); I != E; ++I)
491 if (I->hasAttribute(Kind))
496 bool AttributeSetNode::hasAttribute(StringRef Kind) const {
497 for (iterator I = begin(), E = end(); I != E; ++I)
498 if (I->hasAttribute(Kind))
503 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
504 for (iterator I = begin(), E = end(); I != E; ++I)
505 if (I->hasAttribute(Kind))
510 Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
511 for (iterator I = begin(), E = end(); I != E; ++I)
512 if (I->hasAttribute(Kind))
517 unsigned AttributeSetNode::getAlignment() const {
518 for (iterator I = begin(), E = end(); I != E; ++I)
519 if (I->hasAttribute(Attribute::Alignment))
520 return I->getAlignment();
524 unsigned AttributeSetNode::getStackAlignment() const {
525 for (iterator I = begin(), E = end(); I != E; ++I)
526 if (I->hasAttribute(Attribute::StackAlignment))
527 return I->getStackAlignment();
531 uint64_t AttributeSetNode::getDereferenceableBytes() const {
532 for (iterator I = begin(), E = end(); I != E; ++I)
533 if (I->hasAttribute(Attribute::Dereferenceable))
534 return I->getDereferenceableBytes();
538 uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
539 for (iterator I = begin(), E = end(); I != E; ++I)
540 if (I->hasAttribute(Attribute::DereferenceableOrNull))
541 return I->getDereferenceableOrNullBytes();
545 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
547 for (iterator I = begin(), E = end(); I != E; ++I) {
550 Str += I->getAsString(InAttrGrp);
555 //===----------------------------------------------------------------------===//
556 // AttributeSetImpl Definition
557 //===----------------------------------------------------------------------===//
559 uint64_t AttributeSetImpl::Raw(unsigned Index) const {
560 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
561 if (getSlotIndex(I) != Index) continue;
562 const AttributeSetNode *ASN = getSlotNode(I);
565 for (AttributeSetNode::iterator II = ASN->begin(),
566 IE = ASN->end(); II != IE; ++II) {
567 Attribute Attr = *II;
569 // This cannot handle string attributes.
570 if (Attr.isStringAttribute()) continue;
572 Attribute::AttrKind Kind = Attr.getKindAsEnum();
574 if (Kind == Attribute::Alignment)
575 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
576 else if (Kind == Attribute::StackAlignment)
577 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
578 else if (Kind == Attribute::Dereferenceable)
579 llvm_unreachable("dereferenceable not supported in bit mask");
581 Mask |= AttributeImpl::getAttrMask(Kind);
590 void AttributeSetImpl::dump() const {
591 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
594 //===----------------------------------------------------------------------===//
595 // AttributeSet Construction and Mutation Methods
596 //===----------------------------------------------------------------------===//
599 AttributeSet::getImpl(LLVMContext &C,
600 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
601 LLVMContextImpl *pImpl = C.pImpl;
603 AttributeSetImpl::Profile(ID, Attrs);
606 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
608 // If we didn't find any existing attributes of the same shape then
609 // create a new one and insert it.
611 // Coallocate entries after the AttributeSetImpl itself.
612 void *Mem = ::operator new(sizeof(AttributeSetImpl) +
613 sizeof(std::pair<unsigned, AttributeSetNode *>) *
615 PA = new (Mem) AttributeSetImpl(C, Attrs);
616 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
619 // Return the AttributesList that we found or created.
620 return AttributeSet(PA);
623 AttributeSet AttributeSet::get(LLVMContext &C,
624 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
625 // If there are no attributes then return a null AttributesList pointer.
627 return AttributeSet();
630 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
631 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
632 "Misordered Attributes list!");
633 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
634 "Pointless attribute!");
638 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
640 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
641 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
642 E = Attrs.end(); I != E; ) {
643 unsigned Index = I->first;
644 SmallVector<Attribute, 4> AttrVec;
645 while (I != E && I->first == Index) {
646 AttrVec.push_back(I->second);
650 AttrPairVec.push_back(std::make_pair(Index,
651 AttributeSetNode::get(C, AttrVec)));
654 return getImpl(C, AttrPairVec);
657 AttributeSet AttributeSet::get(LLVMContext &C,
658 ArrayRef<std::pair<unsigned,
659 AttributeSetNode*> > Attrs) {
660 // If there are no attributes then return a null AttributesList pointer.
662 return AttributeSet();
664 return getImpl(C, Attrs);
667 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
668 const AttrBuilder &B) {
669 if (!B.hasAttributes())
670 return AttributeSet();
672 // Add target-independent attributes.
673 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
674 for (Attribute::AttrKind Kind = Attribute::None;
675 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
676 if (!B.contains(Kind))
679 if (Kind == Attribute::Alignment)
680 Attrs.push_back(std::make_pair(Index, Attribute::
681 getWithAlignment(C, B.getAlignment())));
682 else if (Kind == Attribute::StackAlignment)
683 Attrs.push_back(std::make_pair(Index, Attribute::
684 getWithStackAlignment(C, B.getStackAlignment())));
685 else if (Kind == Attribute::Dereferenceable)
686 Attrs.push_back(std::make_pair(Index,
687 Attribute::getWithDereferenceableBytes(C,
688 B.getDereferenceableBytes())));
689 else if (Kind == Attribute::DereferenceableOrNull)
691 std::make_pair(Index, Attribute::getWithDereferenceableOrNullBytes(
692 C, B.getDereferenceableOrNullBytes())));
694 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
697 // Add target-dependent (string) attributes.
698 for (const AttrBuilder::td_type &TDA : B.td_attrs())
700 std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
702 return get(C, Attrs);
705 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
706 ArrayRef<Attribute::AttrKind> Kind) {
707 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
708 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
709 E = Kind.end(); I != E; ++I)
710 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
711 return get(C, Attrs);
714 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
715 if (Attrs.empty()) return AttributeSet();
716 if (Attrs.size() == 1) return Attrs[0];
718 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
719 AttributeSetImpl *A0 = Attrs[0].pImpl;
721 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
722 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
723 // ordered by index. Because we know that each list in Attrs is ordered by
724 // index we only need to merge each successive list in rather than doing a
726 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
727 AttributeSetImpl *AS = Attrs[I].pImpl;
729 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
730 ANVI = AttrNodeVec.begin(), ANVE;
731 for (const AttributeSetImpl::IndexAttrPair
732 *AI = AS->getNode(0),
733 *AE = AS->getNode(AS->getNumAttributes());
735 ANVE = AttrNodeVec.end();
736 while (ANVI != ANVE && ANVI->first <= AI->first)
738 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
742 return getImpl(C, AttrNodeVec);
745 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
746 Attribute::AttrKind Attr) const {
747 if (hasAttribute(Index, Attr)) return *this;
748 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
751 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
752 StringRef Kind) const {
754 B.addAttribute(Kind);
755 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
758 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
759 StringRef Kind, StringRef Value) const {
761 B.addAttribute(Kind, Value);
762 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
765 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
766 AttributeSet Attrs) const {
767 if (!pImpl) return Attrs;
768 if (!Attrs.pImpl) return *this;
771 // FIXME it is not obvious how this should work for alignment. For now, say
772 // we can't change a known alignment.
773 unsigned OldAlign = getParamAlignment(Index);
774 unsigned NewAlign = Attrs.getParamAlignment(Index);
775 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
776 "Attempt to change alignment!");
779 // Add the attribute slots before the one we're trying to add.
780 SmallVector<AttributeSet, 4> AttrSet;
781 uint64_t NumAttrs = pImpl->getNumAttributes();
783 uint64_t LastIndex = 0;
784 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
785 if (getSlotIndex(I) >= Index) {
786 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
790 AttrSet.push_back(getSlotAttributes(I));
793 // Now add the attribute into the correct slot. There may already be an
794 // AttributeSet there.
795 AttrBuilder B(AS, Index);
797 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
798 if (Attrs.getSlotIndex(I) == Index) {
799 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
800 IE = Attrs.pImpl->end(I); II != IE; ++II)
805 AttrSet.push_back(AttributeSet::get(C, Index, B));
807 // Add the remaining attribute slots.
808 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
809 AttrSet.push_back(getSlotAttributes(I));
811 return get(C, AttrSet);
814 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
815 Attribute::AttrKind Attr) const {
816 if (!hasAttribute(Index, Attr)) return *this;
817 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
820 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
821 AttributeSet Attrs) const {
822 if (!pImpl) return AttributeSet();
823 if (!Attrs.pImpl) return *this;
825 // FIXME it is not obvious how this should work for alignment.
826 // For now, say we can't pass in alignment, which no current use does.
827 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
828 "Attempt to change alignment!");
830 // Add the attribute slots before the one we're trying to add.
831 SmallVector<AttributeSet, 4> AttrSet;
832 uint64_t NumAttrs = pImpl->getNumAttributes();
834 uint64_t LastIndex = 0;
835 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
836 if (getSlotIndex(I) >= Index) {
837 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
841 AttrSet.push_back(getSlotAttributes(I));
844 // Now remove the attribute from the correct slot. There may already be an
845 // AttributeSet there.
846 AttrBuilder B(AS, Index);
848 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
849 if (Attrs.getSlotIndex(I) == Index) {
850 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
854 AttrSet.push_back(AttributeSet::get(C, Index, B));
856 // Add the remaining attribute slots.
857 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
858 AttrSet.push_back(getSlotAttributes(I));
860 return get(C, AttrSet);
863 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
864 const AttrBuilder &Attrs) const {
865 if (!pImpl) return AttributeSet();
867 // FIXME it is not obvious how this should work for alignment.
868 // For now, say we can't pass in alignment, which no current use does.
869 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
871 // Add the attribute slots before the one we're trying to add.
872 SmallVector<AttributeSet, 4> AttrSet;
873 uint64_t NumAttrs = pImpl->getNumAttributes();
875 uint64_t LastIndex = 0;
876 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
877 if (getSlotIndex(I) >= Index) {
878 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
882 AttrSet.push_back(getSlotAttributes(I));
885 // Now remove the attribute from the correct slot. There may already be an
886 // AttributeSet there.
887 AttrBuilder B(AS, Index);
890 AttrSet.push_back(AttributeSet::get(C, Index, B));
892 // Add the remaining attribute slots.
893 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
894 AttrSet.push_back(getSlotAttributes(I));
896 return get(C, AttrSet);
899 AttributeSet AttributeSet::addDereferenceableAttr(LLVMContext &C, unsigned Index,
900 uint64_t Bytes) const {
902 B.addDereferenceableAttr(Bytes);
903 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
906 AttributeSet AttributeSet::addDereferenceableOrNullAttr(LLVMContext &C,
908 uint64_t Bytes) const {
910 B.addDereferenceableOrNullAttr(Bytes);
911 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
914 //===----------------------------------------------------------------------===//
915 // AttributeSet Accessor Methods
916 //===----------------------------------------------------------------------===//
918 LLVMContext &AttributeSet::getContext() const {
919 return pImpl->getContext();
922 AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
923 return pImpl && hasAttributes(Index) ?
924 AttributeSet::get(pImpl->getContext(),
925 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
926 std::make_pair(Index, getAttributes(Index)))) :
930 AttributeSet AttributeSet::getRetAttributes() const {
931 return pImpl && hasAttributes(ReturnIndex) ?
932 AttributeSet::get(pImpl->getContext(),
933 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
934 std::make_pair(ReturnIndex,
935 getAttributes(ReturnIndex)))) :
939 AttributeSet AttributeSet::getFnAttributes() const {
940 return pImpl && hasAttributes(FunctionIndex) ?
941 AttributeSet::get(pImpl->getContext(),
942 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
943 std::make_pair(FunctionIndex,
944 getAttributes(FunctionIndex)))) :
948 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
949 AttributeSetNode *ASN = getAttributes(Index);
950 return ASN ? ASN->hasAttribute(Kind) : false;
953 bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
954 AttributeSetNode *ASN = getAttributes(Index);
955 return ASN ? ASN->hasAttribute(Kind) : false;
958 bool AttributeSet::hasAttributes(unsigned Index) const {
959 AttributeSetNode *ASN = getAttributes(Index);
960 return ASN ? ASN->hasAttributes() : false;
963 /// \brief Return true if the specified attribute is set for at least one
964 /// parameter or for the return value.
965 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
966 if (!pImpl) return false;
968 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
969 for (AttributeSetImpl::iterator II = pImpl->begin(I),
970 IE = pImpl->end(I); II != IE; ++II)
971 if (II->hasAttribute(Attr))
977 Attribute AttributeSet::getAttribute(unsigned Index,
978 Attribute::AttrKind Kind) const {
979 AttributeSetNode *ASN = getAttributes(Index);
980 return ASN ? ASN->getAttribute(Kind) : Attribute();
983 Attribute AttributeSet::getAttribute(unsigned Index,
984 StringRef Kind) const {
985 AttributeSetNode *ASN = getAttributes(Index);
986 return ASN ? ASN->getAttribute(Kind) : Attribute();
989 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
990 AttributeSetNode *ASN = getAttributes(Index);
991 return ASN ? ASN->getAlignment() : 0;
994 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
995 AttributeSetNode *ASN = getAttributes(Index);
996 return ASN ? ASN->getStackAlignment() : 0;
999 uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
1000 AttributeSetNode *ASN = getAttributes(Index);
1001 return ASN ? ASN->getDereferenceableBytes() : 0;
1004 uint64_t AttributeSet::getDereferenceableOrNullBytes(unsigned Index) const {
1005 AttributeSetNode *ASN = getAttributes(Index);
1006 return ASN ? ASN->getDereferenceableOrNullBytes() : 0;
1009 std::string AttributeSet::getAsString(unsigned Index,
1010 bool InAttrGrp) const {
1011 AttributeSetNode *ASN = getAttributes(Index);
1012 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
1015 /// \brief The attributes for the specified index are returned.
1016 AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
1017 if (!pImpl) return nullptr;
1019 // Loop through to find the attribute node we want.
1020 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
1021 if (pImpl->getSlotIndex(I) == Index)
1022 return pImpl->getSlotNode(I);
1027 AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
1029 return ArrayRef<Attribute>().begin();
1030 return pImpl->begin(Slot);
1033 AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
1035 return ArrayRef<Attribute>().end();
1036 return pImpl->end(Slot);
1039 //===----------------------------------------------------------------------===//
1040 // AttributeSet Introspection Methods
1041 //===----------------------------------------------------------------------===//
1043 /// \brief Return the number of slots used in this attribute list. This is the
1044 /// number of arguments that have an attribute set on them (including the
1045 /// function itself).
1046 unsigned AttributeSet::getNumSlots() const {
1047 return pImpl ? pImpl->getNumAttributes() : 0;
1050 unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
1051 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1052 "Slot # out of range!");
1053 return pImpl->getSlotIndex(Slot);
1056 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
1057 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1058 "Slot # out of range!");
1059 return pImpl->getSlotAttributes(Slot);
1062 uint64_t AttributeSet::Raw(unsigned Index) const {
1063 // FIXME: Remove this.
1064 return pImpl ? pImpl->Raw(Index) : 0;
1067 void AttributeSet::dump() const {
1070 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1071 uint64_t Index = getSlotIndex(i);
1077 dbgs() << " => " << getAsString(Index) << " }\n";
1083 //===----------------------------------------------------------------------===//
1084 // AttrBuilder Method Implementations
1085 //===----------------------------------------------------------------------===//
1087 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
1088 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
1089 DerefOrNullBytes(0) {
1090 AttributeSetImpl *pImpl = AS.pImpl;
1093 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
1094 if (pImpl->getSlotIndex(I) != Index) continue;
1096 for (AttributeSetImpl::iterator II = pImpl->begin(I),
1097 IE = pImpl->end(I); II != IE; ++II)
1104 void AttrBuilder::clear() {
1106 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
1109 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
1110 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1111 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
1112 Val != Attribute::Dereferenceable &&
1113 "Adding integer attribute without adding a value!");
1118 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
1119 if (Attr.isStringAttribute()) {
1120 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1124 Attribute::AttrKind Kind = Attr.getKindAsEnum();
1127 if (Kind == Attribute::Alignment)
1128 Alignment = Attr.getAlignment();
1129 else if (Kind == Attribute::StackAlignment)
1130 StackAlignment = Attr.getStackAlignment();
1131 else if (Kind == Attribute::Dereferenceable)
1132 DerefBytes = Attr.getDereferenceableBytes();
1133 else if (Kind == Attribute::DereferenceableOrNull)
1134 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
1138 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1139 TargetDepAttrs[A] = V;
1143 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
1144 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1147 if (Val == Attribute::Alignment)
1149 else if (Val == Attribute::StackAlignment)
1151 else if (Val == Attribute::Dereferenceable)
1153 else if (Val == Attribute::DereferenceableOrNull)
1154 DerefOrNullBytes = 0;
1159 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
1160 unsigned Slot = ~0U;
1161 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1162 if (A.getSlotIndex(I) == Index) {
1167 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
1169 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
1170 Attribute Attr = *I;
1171 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1172 Attribute::AttrKind Kind = I->getKindAsEnum();
1173 Attrs[Kind] = false;
1175 if (Kind == Attribute::Alignment)
1177 else if (Kind == Attribute::StackAlignment)
1179 else if (Kind == Attribute::Dereferenceable)
1181 else if (Kind == Attribute::DereferenceableOrNull)
1182 DerefOrNullBytes = 0;
1184 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1185 std::map<std::string, std::string>::iterator
1186 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1187 if (Iter != TargetDepAttrs.end())
1188 TargetDepAttrs.erase(Iter);
1195 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1196 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1197 if (I != TargetDepAttrs.end())
1198 TargetDepAttrs.erase(I);
1202 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
1203 if (Align == 0) return *this;
1205 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1206 assert(Align <= 0x40000000 && "Alignment too large.");
1208 Attrs[Attribute::Alignment] = true;
1213 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1214 // Default alignment, allow the target to define how to align it.
1215 if (Align == 0) return *this;
1217 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1218 assert(Align <= 0x100 && "Alignment too large.");
1220 Attrs[Attribute::StackAlignment] = true;
1221 StackAlignment = Align;
1225 AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1226 if (Bytes == 0) return *this;
1228 Attrs[Attribute::Dereferenceable] = true;
1233 AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1237 Attrs[Attribute::DereferenceableOrNull] = true;
1238 DerefOrNullBytes = Bytes;
1242 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1243 // FIXME: What if both have alignments, but they don't match?!
1245 Alignment = B.Alignment;
1247 if (!StackAlignment)
1248 StackAlignment = B.StackAlignment;
1251 DerefBytes = B.DerefBytes;
1253 if (!DerefOrNullBytes)
1254 DerefOrNullBytes = B.DerefOrNullBytes;
1258 for (auto I : B.td_attrs())
1259 TargetDepAttrs[I.first] = I.second;
1264 AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1265 // FIXME: What if both have alignments, but they don't match?!
1269 if (B.StackAlignment)
1275 if (B.DerefOrNullBytes)
1276 DerefOrNullBytes = 0;
1280 for (auto I : B.td_attrs())
1281 TargetDepAttrs.erase(I.first);
1286 bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1287 // First check if any of the target independent attributes overlap.
1288 if ((Attrs & B.Attrs).any())
1291 // Then check if any target dependent ones do.
1292 for (auto I : td_attrs())
1293 if (B.contains(I.first))
1299 bool AttrBuilder::contains(StringRef A) const {
1300 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1303 bool AttrBuilder::hasAttributes() const {
1304 return !Attrs.none() || !TargetDepAttrs.empty();
1307 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
1308 unsigned Slot = ~0U;
1309 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1310 if (A.getSlotIndex(I) == Index) {
1315 assert(Slot != ~0U && "Couldn't find the index!");
1317 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
1319 Attribute Attr = *I;
1320 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1321 if (Attrs[I->getKindAsEnum()])
1324 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1325 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1332 bool AttrBuilder::hasAlignmentAttr() const {
1333 return Alignment != 0;
1336 bool AttrBuilder::operator==(const AttrBuilder &B) {
1337 if (Attrs != B.Attrs)
1340 for (td_const_iterator I = TargetDepAttrs.begin(),
1341 E = TargetDepAttrs.end(); I != E; ++I)
1342 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1345 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1346 DerefBytes == B.DerefBytes;
1349 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
1350 // FIXME: Remove this in 4.0.
1351 if (!Val) return *this;
1353 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1354 I = Attribute::AttrKind(I + 1)) {
1355 if (I == Attribute::Dereferenceable ||
1356 I == Attribute::DereferenceableOrNull)
1358 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1361 if (I == Attribute::Alignment)
1362 Alignment = 1ULL << ((A >> 16) - 1);
1363 else if (I == Attribute::StackAlignment)
1364 StackAlignment = 1ULL << ((A >> 26)-1);
1371 //===----------------------------------------------------------------------===//
1372 // AttributeFuncs Function Defintions
1373 //===----------------------------------------------------------------------===//
1375 /// \brief Which attributes cannot be applied to a type.
1376 AttrBuilder AttributeFuncs::typeIncompatible(const Type *Ty) {
1377 AttrBuilder Incompatible;
1379 if (!Ty->isIntegerTy())
1380 // Attribute that only apply to integers.
1381 Incompatible.addAttribute(Attribute::SExt)
1382 .addAttribute(Attribute::ZExt);
1384 if (!Ty->isPointerTy())
1385 // Attribute that only apply to pointers.
1386 Incompatible.addAttribute(Attribute::ByVal)
1387 .addAttribute(Attribute::Nest)
1388 .addAttribute(Attribute::NoAlias)
1389 .addAttribute(Attribute::NoCapture)
1390 .addAttribute(Attribute::NonNull)
1391 .addDereferenceableAttr(1) // the int here is ignored
1392 .addDereferenceableOrNullAttr(1) // the int here is ignored
1393 .addAttribute(Attribute::ReadNone)
1394 .addAttribute(Attribute::ReadOnly)
1395 .addAttribute(Attribute::StructRet)
1396 .addAttribute(Attribute::InAlloca);
1398 return Incompatible;