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::InAlloca))
199 if (hasAttribute(Attribute::InlineHint))
201 if (hasAttribute(Attribute::InReg))
203 if (hasAttribute(Attribute::JumpTable))
205 if (hasAttribute(Attribute::MinSize))
207 if (hasAttribute(Attribute::Naked))
209 if (hasAttribute(Attribute::Nest))
211 if (hasAttribute(Attribute::NoAlias))
213 if (hasAttribute(Attribute::NoBuiltin))
215 if (hasAttribute(Attribute::NoCapture))
217 if (hasAttribute(Attribute::NoDuplicate))
218 return "noduplicate";
219 if (hasAttribute(Attribute::NoImplicitFloat))
220 return "noimplicitfloat";
221 if (hasAttribute(Attribute::NoInline))
223 if (hasAttribute(Attribute::NonLazyBind))
224 return "nonlazybind";
225 if (hasAttribute(Attribute::NonNull))
227 if (hasAttribute(Attribute::NoRedZone))
229 if (hasAttribute(Attribute::NoReturn))
231 if (hasAttribute(Attribute::NoUnwind))
233 if (hasAttribute(Attribute::OptimizeNone))
235 if (hasAttribute(Attribute::OptimizeForSize))
237 if (hasAttribute(Attribute::ReadNone))
239 if (hasAttribute(Attribute::ReadOnly))
241 if (hasAttribute(Attribute::Returned))
243 if (hasAttribute(Attribute::ReturnsTwice))
244 return "returns_twice";
245 if (hasAttribute(Attribute::SExt))
247 if (hasAttribute(Attribute::StackProtect))
249 if (hasAttribute(Attribute::StackProtectReq))
251 if (hasAttribute(Attribute::StackProtectStrong))
253 if (hasAttribute(Attribute::StructRet))
255 if (hasAttribute(Attribute::SanitizeThread))
256 return "sanitize_thread";
257 if (hasAttribute(Attribute::SanitizeMemory))
258 return "sanitize_memory";
259 if (hasAttribute(Attribute::UWTable))
261 if (hasAttribute(Attribute::ZExt))
263 if (hasAttribute(Attribute::Cold))
266 // FIXME: These should be output like this:
271 if (hasAttribute(Attribute::Alignment)) {
274 Result += (InAttrGrp) ? "=" : " ";
275 Result += utostr(getValueAsInt());
279 auto AttrWithBytesToString = [&](const char *Name) {
284 Result += utostr(getValueAsInt());
287 Result += utostr(getValueAsInt());
293 if (hasAttribute(Attribute::StackAlignment))
294 return AttrWithBytesToString("alignstack");
296 if (hasAttribute(Attribute::Dereferenceable))
297 return AttrWithBytesToString("dereferenceable");
299 if (hasAttribute(Attribute::DereferenceableOrNull))
300 return AttrWithBytesToString("dereferenceable_or_null");
302 // Convert target-dependent attributes to strings of the form:
307 if (isStringAttribute()) {
309 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
311 StringRef Val = pImpl->getValueAsString();
312 if (Val.empty()) return Result;
314 Result += ("=\"" + Val + Twine('"')).str();
318 llvm_unreachable("Unknown attribute");
321 bool Attribute::operator<(Attribute A) const {
322 if (!pImpl && !A.pImpl) return false;
323 if (!pImpl) return true;
324 if (!A.pImpl) return false;
325 return *pImpl < *A.pImpl;
328 //===----------------------------------------------------------------------===//
329 // AttributeImpl Definition
330 //===----------------------------------------------------------------------===//
332 // Pin the vtables to this file.
333 AttributeImpl::~AttributeImpl() {}
334 void EnumAttributeImpl::anchor() {}
335 void IntAttributeImpl::anchor() {}
336 void StringAttributeImpl::anchor() {}
338 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
339 if (isStringAttribute()) return false;
340 return getKindAsEnum() == A;
343 bool AttributeImpl::hasAttribute(StringRef Kind) const {
344 if (!isStringAttribute()) return false;
345 return getKindAsString() == Kind;
348 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
349 assert(isEnumAttribute() || isIntAttribute());
350 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
353 uint64_t AttributeImpl::getValueAsInt() const {
354 assert(isIntAttribute());
355 return static_cast<const IntAttributeImpl *>(this)->getValue();
358 StringRef AttributeImpl::getKindAsString() const {
359 assert(isStringAttribute());
360 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
363 StringRef AttributeImpl::getValueAsString() const {
364 assert(isStringAttribute());
365 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
368 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
369 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
370 // relative to their enum value) and then strings.
371 if (isEnumAttribute()) {
372 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
373 if (AI.isIntAttribute()) return true;
374 if (AI.isStringAttribute()) return true;
377 if (isIntAttribute()) {
378 if (AI.isEnumAttribute()) return false;
379 if (AI.isIntAttribute()) return getValueAsInt() < AI.getValueAsInt();
380 if (AI.isStringAttribute()) return true;
383 if (AI.isEnumAttribute()) return false;
384 if (AI.isIntAttribute()) return false;
385 if (getKindAsString() == AI.getKindAsString())
386 return getValueAsString() < AI.getValueAsString();
387 return getKindAsString() < AI.getKindAsString();
390 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
391 // FIXME: Remove this.
393 case Attribute::EndAttrKinds:
394 llvm_unreachable("Synthetic enumerators which should never get here");
396 case Attribute::None: return 0;
397 case Attribute::ZExt: return 1 << 0;
398 case Attribute::SExt: return 1 << 1;
399 case Attribute::NoReturn: return 1 << 2;
400 case Attribute::InReg: return 1 << 3;
401 case Attribute::StructRet: return 1 << 4;
402 case Attribute::NoUnwind: return 1 << 5;
403 case Attribute::NoAlias: return 1 << 6;
404 case Attribute::ByVal: return 1 << 7;
405 case Attribute::Nest: return 1 << 8;
406 case Attribute::ReadNone: return 1 << 9;
407 case Attribute::ReadOnly: return 1 << 10;
408 case Attribute::NoInline: return 1 << 11;
409 case Attribute::AlwaysInline: return 1 << 12;
410 case Attribute::OptimizeForSize: return 1 << 13;
411 case Attribute::StackProtect: return 1 << 14;
412 case Attribute::StackProtectReq: return 1 << 15;
413 case Attribute::Alignment: return 31 << 16;
414 case Attribute::NoCapture: return 1 << 21;
415 case Attribute::NoRedZone: return 1 << 22;
416 case Attribute::NoImplicitFloat: return 1 << 23;
417 case Attribute::Naked: return 1 << 24;
418 case Attribute::InlineHint: return 1 << 25;
419 case Attribute::StackAlignment: return 7 << 26;
420 case Attribute::ReturnsTwice: return 1 << 29;
421 case Attribute::UWTable: return 1 << 30;
422 case Attribute::NonLazyBind: return 1U << 31;
423 case Attribute::SanitizeAddress: return 1ULL << 32;
424 case Attribute::MinSize: return 1ULL << 33;
425 case Attribute::NoDuplicate: return 1ULL << 34;
426 case Attribute::StackProtectStrong: return 1ULL << 35;
427 case Attribute::SanitizeThread: return 1ULL << 36;
428 case Attribute::SanitizeMemory: return 1ULL << 37;
429 case Attribute::NoBuiltin: return 1ULL << 38;
430 case Attribute::Returned: return 1ULL << 39;
431 case Attribute::Cold: return 1ULL << 40;
432 case Attribute::Builtin: return 1ULL << 41;
433 case Attribute::OptimizeNone: return 1ULL << 42;
434 case Attribute::InAlloca: return 1ULL << 43;
435 case Attribute::NonNull: return 1ULL << 44;
436 case Attribute::JumpTable: return 1ULL << 45;
437 case Attribute::Dereferenceable:
438 llvm_unreachable("dereferenceable attribute not supported in raw format");
440 case Attribute::DereferenceableOrNull:
441 llvm_unreachable("dereferenceable_or_null attribute not supported in raw "
445 llvm_unreachable("Unsupported attribute type");
448 //===----------------------------------------------------------------------===//
449 // AttributeSetNode Definition
450 //===----------------------------------------------------------------------===//
452 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
453 ArrayRef<Attribute> Attrs) {
457 // Otherwise, build a key to look up the existing attributes.
458 LLVMContextImpl *pImpl = C.pImpl;
461 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
462 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
464 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
465 E = SortedAttrs.end(); I != E; ++I)
469 AttributeSetNode *PA =
470 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
472 // If we didn't find any existing attributes of the same shape then create a
473 // new one and insert it.
475 // Coallocate entries after the AttributeSetNode itself.
476 void *Mem = ::operator new(sizeof(AttributeSetNode) +
477 sizeof(Attribute) * SortedAttrs.size());
478 PA = new (Mem) AttributeSetNode(SortedAttrs);
479 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
482 // Return the AttributesListNode that we found or created.
486 bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
487 for (iterator I = begin(), E = end(); I != E; ++I)
488 if (I->hasAttribute(Kind))
493 bool AttributeSetNode::hasAttribute(StringRef Kind) const {
494 for (iterator I = begin(), E = end(); I != E; ++I)
495 if (I->hasAttribute(Kind))
500 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
501 for (iterator I = begin(), E = end(); I != E; ++I)
502 if (I->hasAttribute(Kind))
507 Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
508 for (iterator I = begin(), E = end(); I != E; ++I)
509 if (I->hasAttribute(Kind))
514 unsigned AttributeSetNode::getAlignment() const {
515 for (iterator I = begin(), E = end(); I != E; ++I)
516 if (I->hasAttribute(Attribute::Alignment))
517 return I->getAlignment();
521 unsigned AttributeSetNode::getStackAlignment() const {
522 for (iterator I = begin(), E = end(); I != E; ++I)
523 if (I->hasAttribute(Attribute::StackAlignment))
524 return I->getStackAlignment();
528 uint64_t AttributeSetNode::getDereferenceableBytes() const {
529 for (iterator I = begin(), E = end(); I != E; ++I)
530 if (I->hasAttribute(Attribute::Dereferenceable))
531 return I->getDereferenceableBytes();
535 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
537 for (iterator I = begin(), E = end(); I != E; ++I) {
540 Str += I->getAsString(InAttrGrp);
545 //===----------------------------------------------------------------------===//
546 // AttributeSetImpl Definition
547 //===----------------------------------------------------------------------===//
549 uint64_t AttributeSetImpl::Raw(unsigned Index) const {
550 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
551 if (getSlotIndex(I) != Index) continue;
552 const AttributeSetNode *ASN = getSlotNode(I);
555 for (AttributeSetNode::iterator II = ASN->begin(),
556 IE = ASN->end(); II != IE; ++II) {
557 Attribute Attr = *II;
559 // This cannot handle string attributes.
560 if (Attr.isStringAttribute()) continue;
562 Attribute::AttrKind Kind = Attr.getKindAsEnum();
564 if (Kind == Attribute::Alignment)
565 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
566 else if (Kind == Attribute::StackAlignment)
567 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
568 else if (Kind == Attribute::Dereferenceable)
569 llvm_unreachable("dereferenceable not supported in bit mask");
571 Mask |= AttributeImpl::getAttrMask(Kind);
580 void AttributeSetImpl::dump() const {
581 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
584 //===----------------------------------------------------------------------===//
585 // AttributeSet Construction and Mutation Methods
586 //===----------------------------------------------------------------------===//
589 AttributeSet::getImpl(LLVMContext &C,
590 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
591 LLVMContextImpl *pImpl = C.pImpl;
593 AttributeSetImpl::Profile(ID, Attrs);
596 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
598 // If we didn't find any existing attributes of the same shape then
599 // create a new one and insert it.
601 // Coallocate entries after the AttributeSetImpl itself.
602 void *Mem = ::operator new(sizeof(AttributeSetImpl) +
603 sizeof(std::pair<unsigned, AttributeSetNode *>) *
605 PA = new (Mem) AttributeSetImpl(C, Attrs);
606 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
609 // Return the AttributesList that we found or created.
610 return AttributeSet(PA);
613 AttributeSet AttributeSet::get(LLVMContext &C,
614 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
615 // If there are no attributes then return a null AttributesList pointer.
617 return AttributeSet();
620 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
621 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
622 "Misordered Attributes list!");
623 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
624 "Pointless attribute!");
628 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
630 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
631 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
632 E = Attrs.end(); I != E; ) {
633 unsigned Index = I->first;
634 SmallVector<Attribute, 4> AttrVec;
635 while (I != E && I->first == Index) {
636 AttrVec.push_back(I->second);
640 AttrPairVec.push_back(std::make_pair(Index,
641 AttributeSetNode::get(C, AttrVec)));
644 return getImpl(C, AttrPairVec);
647 AttributeSet AttributeSet::get(LLVMContext &C,
648 ArrayRef<std::pair<unsigned,
649 AttributeSetNode*> > Attrs) {
650 // If there are no attributes then return a null AttributesList pointer.
652 return AttributeSet();
654 return getImpl(C, Attrs);
657 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
658 const AttrBuilder &B) {
659 if (!B.hasAttributes())
660 return AttributeSet();
662 // Add target-independent attributes.
663 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
664 for (Attribute::AttrKind Kind = Attribute::None;
665 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
666 if (!B.contains(Kind))
669 if (Kind == Attribute::Alignment)
670 Attrs.push_back(std::make_pair(Index, Attribute::
671 getWithAlignment(C, B.getAlignment())));
672 else if (Kind == Attribute::StackAlignment)
673 Attrs.push_back(std::make_pair(Index, Attribute::
674 getWithStackAlignment(C, B.getStackAlignment())));
675 else if (Kind == Attribute::Dereferenceable)
676 Attrs.push_back(std::make_pair(Index,
677 Attribute::getWithDereferenceableBytes(C,
678 B.getDereferenceableBytes())));
679 else if (Kind == Attribute::DereferenceableOrNull)
681 std::make_pair(Index, Attribute::getWithDereferenceableOrNullBytes(
682 C, B.getDereferenceableOrNullBytes())));
684 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
687 // Add target-dependent (string) attributes.
688 for (const AttrBuilder::td_type &TDA : B.td_attrs())
690 std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
692 return get(C, Attrs);
695 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
696 ArrayRef<Attribute::AttrKind> Kind) {
697 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
698 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
699 E = Kind.end(); I != E; ++I)
700 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
701 return get(C, Attrs);
704 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
705 if (Attrs.empty()) return AttributeSet();
706 if (Attrs.size() == 1) return Attrs[0];
708 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
709 AttributeSetImpl *A0 = Attrs[0].pImpl;
711 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
712 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
713 // ordered by index. Because we know that each list in Attrs is ordered by
714 // index we only need to merge each successive list in rather than doing a
716 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
717 AttributeSetImpl *AS = Attrs[I].pImpl;
719 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
720 ANVI = AttrNodeVec.begin(), ANVE;
721 for (const AttributeSetImpl::IndexAttrPair
722 *AI = AS->getNode(0),
723 *AE = AS->getNode(AS->getNumAttributes());
725 ANVE = AttrNodeVec.end();
726 while (ANVI != ANVE && ANVI->first <= AI->first)
728 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
732 return getImpl(C, AttrNodeVec);
735 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
736 Attribute::AttrKind Attr) const {
737 if (hasAttribute(Index, Attr)) return *this;
738 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
741 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
742 StringRef Kind) const {
744 B.addAttribute(Kind);
745 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
748 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
749 StringRef Kind, StringRef Value) const {
751 B.addAttribute(Kind, Value);
752 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
755 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
756 AttributeSet Attrs) const {
757 if (!pImpl) return Attrs;
758 if (!Attrs.pImpl) return *this;
761 // FIXME it is not obvious how this should work for alignment. For now, say
762 // we can't change a known alignment.
763 unsigned OldAlign = getParamAlignment(Index);
764 unsigned NewAlign = Attrs.getParamAlignment(Index);
765 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
766 "Attempt to change alignment!");
769 // Add the attribute slots before the one we're trying to add.
770 SmallVector<AttributeSet, 4> AttrSet;
771 uint64_t NumAttrs = pImpl->getNumAttributes();
773 uint64_t LastIndex = 0;
774 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
775 if (getSlotIndex(I) >= Index) {
776 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
780 AttrSet.push_back(getSlotAttributes(I));
783 // Now add the attribute into the correct slot. There may already be an
784 // AttributeSet there.
785 AttrBuilder B(AS, Index);
787 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
788 if (Attrs.getSlotIndex(I) == Index) {
789 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
790 IE = Attrs.pImpl->end(I); II != IE; ++II)
795 AttrSet.push_back(AttributeSet::get(C, Index, B));
797 // Add the remaining attribute slots.
798 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
799 AttrSet.push_back(getSlotAttributes(I));
801 return get(C, AttrSet);
804 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
805 Attribute::AttrKind Attr) const {
806 if (!hasAttribute(Index, Attr)) return *this;
807 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
810 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
811 AttributeSet Attrs) const {
812 if (!pImpl) return AttributeSet();
813 if (!Attrs.pImpl) return *this;
816 // FIXME it is not obvious how this should work for alignment.
817 // For now, say we can't pass in alignment, which no current use does.
818 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
819 "Attempt to change alignment!");
822 // Add the attribute slots before the one we're trying to add.
823 SmallVector<AttributeSet, 4> AttrSet;
824 uint64_t NumAttrs = pImpl->getNumAttributes();
826 uint64_t LastIndex = 0;
827 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
828 if (getSlotIndex(I) >= Index) {
829 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
833 AttrSet.push_back(getSlotAttributes(I));
836 // Now remove the attribute from the correct slot. There may already be an
837 // AttributeSet there.
838 AttrBuilder B(AS, Index);
840 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
841 if (Attrs.getSlotIndex(I) == Index) {
842 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
846 AttrSet.push_back(AttributeSet::get(C, Index, B));
848 // Add the remaining attribute slots.
849 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
850 AttrSet.push_back(getSlotAttributes(I));
852 return get(C, AttrSet);
855 AttributeSet AttributeSet::addDereferenceableAttr(LLVMContext &C, unsigned Index,
856 uint64_t Bytes) const {
858 B.addDereferenceableAttr(Bytes);
859 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
862 AttributeSet AttributeSet::addDereferenceableOrNullAttr(LLVMContext &C,
864 uint64_t Bytes) const {
866 B.addDereferenceableOrNullAttr(Bytes);
867 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
870 //===----------------------------------------------------------------------===//
871 // AttributeSet Accessor Methods
872 //===----------------------------------------------------------------------===//
874 LLVMContext &AttributeSet::getContext() const {
875 return pImpl->getContext();
878 AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
879 return pImpl && hasAttributes(Index) ?
880 AttributeSet::get(pImpl->getContext(),
881 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
882 std::make_pair(Index, getAttributes(Index)))) :
886 AttributeSet AttributeSet::getRetAttributes() const {
887 return pImpl && hasAttributes(ReturnIndex) ?
888 AttributeSet::get(pImpl->getContext(),
889 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
890 std::make_pair(ReturnIndex,
891 getAttributes(ReturnIndex)))) :
895 AttributeSet AttributeSet::getFnAttributes() const {
896 return pImpl && hasAttributes(FunctionIndex) ?
897 AttributeSet::get(pImpl->getContext(),
898 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
899 std::make_pair(FunctionIndex,
900 getAttributes(FunctionIndex)))) :
904 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
905 AttributeSetNode *ASN = getAttributes(Index);
906 return ASN ? ASN->hasAttribute(Kind) : false;
909 bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
910 AttributeSetNode *ASN = getAttributes(Index);
911 return ASN ? ASN->hasAttribute(Kind) : false;
914 bool AttributeSet::hasAttributes(unsigned Index) const {
915 AttributeSetNode *ASN = getAttributes(Index);
916 return ASN ? ASN->hasAttributes() : false;
919 /// \brief Return true if the specified attribute is set for at least one
920 /// parameter or for the return value.
921 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
922 if (!pImpl) return false;
924 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
925 for (AttributeSetImpl::iterator II = pImpl->begin(I),
926 IE = pImpl->end(I); II != IE; ++II)
927 if (II->hasAttribute(Attr))
933 Attribute AttributeSet::getAttribute(unsigned Index,
934 Attribute::AttrKind Kind) const {
935 AttributeSetNode *ASN = getAttributes(Index);
936 return ASN ? ASN->getAttribute(Kind) : Attribute();
939 Attribute AttributeSet::getAttribute(unsigned Index,
940 StringRef Kind) const {
941 AttributeSetNode *ASN = getAttributes(Index);
942 return ASN ? ASN->getAttribute(Kind) : Attribute();
945 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
946 AttributeSetNode *ASN = getAttributes(Index);
947 return ASN ? ASN->getAlignment() : 0;
950 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
951 AttributeSetNode *ASN = getAttributes(Index);
952 return ASN ? ASN->getStackAlignment() : 0;
955 uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
956 AttributeSetNode *ASN = getAttributes(Index);
957 return ASN ? ASN->getDereferenceableBytes() : 0;
960 std::string AttributeSet::getAsString(unsigned Index,
961 bool InAttrGrp) const {
962 AttributeSetNode *ASN = getAttributes(Index);
963 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
966 /// \brief The attributes for the specified index are returned.
967 AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
968 if (!pImpl) return nullptr;
970 // Loop through to find the attribute node we want.
971 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
972 if (pImpl->getSlotIndex(I) == Index)
973 return pImpl->getSlotNode(I);
978 AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
980 return ArrayRef<Attribute>().begin();
981 return pImpl->begin(Slot);
984 AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
986 return ArrayRef<Attribute>().end();
987 return pImpl->end(Slot);
990 //===----------------------------------------------------------------------===//
991 // AttributeSet Introspection Methods
992 //===----------------------------------------------------------------------===//
994 /// \brief Return the number of slots used in this attribute list. This is the
995 /// number of arguments that have an attribute set on them (including the
996 /// function itself).
997 unsigned AttributeSet::getNumSlots() const {
998 return pImpl ? pImpl->getNumAttributes() : 0;
1001 unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
1002 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1003 "Slot # out of range!");
1004 return pImpl->getSlotIndex(Slot);
1007 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
1008 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1009 "Slot # out of range!");
1010 return pImpl->getSlotAttributes(Slot);
1013 uint64_t AttributeSet::Raw(unsigned Index) const {
1014 // FIXME: Remove this.
1015 return pImpl ? pImpl->Raw(Index) : 0;
1018 void AttributeSet::dump() const {
1021 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1022 uint64_t Index = getSlotIndex(i);
1028 dbgs() << " => " << getAsString(Index) << " }\n";
1034 //===----------------------------------------------------------------------===//
1035 // AttrBuilder Method Implementations
1036 //===----------------------------------------------------------------------===//
1038 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
1039 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
1040 DerefOrNullBytes(0) {
1041 AttributeSetImpl *pImpl = AS.pImpl;
1044 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
1045 if (pImpl->getSlotIndex(I) != Index) continue;
1047 for (AttributeSetImpl::iterator II = pImpl->begin(I),
1048 IE = pImpl->end(I); II != IE; ++II)
1055 void AttrBuilder::clear() {
1057 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
1060 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
1061 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1062 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
1063 Val != Attribute::Dereferenceable &&
1064 "Adding integer attribute without adding a value!");
1069 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
1070 if (Attr.isStringAttribute()) {
1071 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1075 Attribute::AttrKind Kind = Attr.getKindAsEnum();
1078 if (Kind == Attribute::Alignment)
1079 Alignment = Attr.getAlignment();
1080 else if (Kind == Attribute::StackAlignment)
1081 StackAlignment = Attr.getStackAlignment();
1082 else if (Kind == Attribute::Dereferenceable)
1083 DerefBytes = Attr.getDereferenceableBytes();
1084 else if (Kind == Attribute::DereferenceableOrNull)
1085 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
1089 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1090 TargetDepAttrs[A] = V;
1094 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
1095 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1098 if (Val == Attribute::Alignment)
1100 else if (Val == Attribute::StackAlignment)
1102 else if (Val == Attribute::Dereferenceable)
1104 else if (Val == Attribute::DereferenceableOrNull)
1105 DerefOrNullBytes = 0;
1110 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
1111 unsigned Slot = ~0U;
1112 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1113 if (A.getSlotIndex(I) == Index) {
1118 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
1120 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
1121 Attribute Attr = *I;
1122 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1123 Attribute::AttrKind Kind = I->getKindAsEnum();
1124 Attrs[Kind] = false;
1126 if (Kind == Attribute::Alignment)
1128 else if (Kind == Attribute::StackAlignment)
1130 else if (Kind == Attribute::Dereferenceable)
1132 else if (Kind == Attribute::DereferenceableOrNull)
1133 DerefOrNullBytes = 0;
1135 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1136 std::map<std::string, std::string>::iterator
1137 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1138 if (Iter != TargetDepAttrs.end())
1139 TargetDepAttrs.erase(Iter);
1146 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1147 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1148 if (I != TargetDepAttrs.end())
1149 TargetDepAttrs.erase(I);
1153 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
1154 if (Align == 0) return *this;
1156 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1157 assert(Align <= 0x40000000 && "Alignment too large.");
1159 Attrs[Attribute::Alignment] = true;
1164 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1165 // Default alignment, allow the target to define how to align it.
1166 if (Align == 0) return *this;
1168 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1169 assert(Align <= 0x100 && "Alignment too large.");
1171 Attrs[Attribute::StackAlignment] = true;
1172 StackAlignment = Align;
1176 AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1177 if (Bytes == 0) return *this;
1179 Attrs[Attribute::Dereferenceable] = true;
1184 AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1188 Attrs[Attribute::DereferenceableOrNull] = true;
1189 DerefOrNullBytes = Bytes;
1193 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1194 // FIXME: What if both have alignments, but they don't match?!
1196 Alignment = B.Alignment;
1198 if (!StackAlignment)
1199 StackAlignment = B.StackAlignment;
1202 DerefBytes = B.DerefBytes;
1206 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1207 E = B.TargetDepAttrs.end(); I != E; ++I)
1208 TargetDepAttrs[I->first] = I->second;
1213 bool AttrBuilder::contains(StringRef A) const {
1214 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1217 bool AttrBuilder::hasAttributes() const {
1218 return !Attrs.none() || !TargetDepAttrs.empty();
1221 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
1222 unsigned Slot = ~0U;
1223 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1224 if (A.getSlotIndex(I) == Index) {
1229 assert(Slot != ~0U && "Couldn't find the index!");
1231 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
1233 Attribute Attr = *I;
1234 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1235 if (Attrs[I->getKindAsEnum()])
1238 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1239 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1246 bool AttrBuilder::hasAlignmentAttr() const {
1247 return Alignment != 0;
1250 bool AttrBuilder::operator==(const AttrBuilder &B) {
1251 if (Attrs != B.Attrs)
1254 for (td_const_iterator I = TargetDepAttrs.begin(),
1255 E = TargetDepAttrs.end(); I != E; ++I)
1256 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1259 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1260 DerefBytes == B.DerefBytes;
1263 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
1264 // FIXME: Remove this in 4.0.
1265 if (!Val) return *this;
1267 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1268 I = Attribute::AttrKind(I + 1)) {
1269 if (I == Attribute::Dereferenceable ||
1270 I == Attribute::DereferenceableOrNull)
1272 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1275 if (I == Attribute::Alignment)
1276 Alignment = 1ULL << ((A >> 16) - 1);
1277 else if (I == Attribute::StackAlignment)
1278 StackAlignment = 1ULL << ((A >> 26)-1);
1285 //===----------------------------------------------------------------------===//
1286 // AttributeFuncs Function Defintions
1287 //===----------------------------------------------------------------------===//
1289 /// \brief Which attributes cannot be applied to a type.
1290 AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
1291 AttrBuilder Incompatible;
1293 if (!Ty->isIntegerTy())
1294 // Attribute that only apply to integers.
1295 Incompatible.addAttribute(Attribute::SExt)
1296 .addAttribute(Attribute::ZExt);
1298 if (!Ty->isPointerTy())
1299 // Attribute that only apply to pointers.
1300 Incompatible.addAttribute(Attribute::ByVal)
1301 .addAttribute(Attribute::Nest)
1302 .addAttribute(Attribute::NoAlias)
1303 .addAttribute(Attribute::NoCapture)
1304 .addAttribute(Attribute::NonNull)
1305 .addDereferenceableAttr(1) // the int here is ignored
1306 .addDereferenceableOrNullAttr(1) // the int here is ignored
1307 .addAttribute(Attribute::ReadNone)
1308 .addAttribute(Attribute::ReadOnly)
1309 .addAttribute(Attribute::StructRet)
1310 .addAttribute(Attribute::InAlloca);
1312 return AttributeSet::get(Ty->getContext(), Index, Incompatible);