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) {
35 LLVMContextImpl *pImpl = Context.pImpl;
40 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
43 // If we didn't find any existing attributes of the same shape then create a
44 // new one and insert it.
45 PA = new EnumAttributeImpl(Kind);
46 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
49 // Return the Attribute that we found or created.
53 Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
55 LLVMContextImpl *pImpl = Context.pImpl;
61 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
64 // If we didn't find any existing attributes of the same shape then create a
65 // new one and insert it.
66 PA = new IntAttributeImpl(Kind, Val);
67 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
70 // Return the Attribute that we found or created.
74 Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
75 LLVMContextImpl *pImpl = Context.pImpl;
78 if (!Val.empty()) ID.AddString(Val);
81 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
84 // If we didn't find any existing attributes of the same shape then create a
85 // new one and insert it.
86 PA = new StringAttributeImpl(Kind, Val);
87 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
90 // Return the Attribute that we found or created.
94 Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
95 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
96 assert(Align <= 0x40000000 && "Alignment too large.");
97 return get(Context, Alignment, Align);
100 Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
102 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
103 assert(Align <= 0x100 && "Alignment too large.");
104 return get(Context, StackAlignment, Align);
107 Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context,
109 assert(Bytes && "Bytes must be non-zero.");
110 return get(Context, Dereferenceable, Bytes);
113 //===----------------------------------------------------------------------===//
114 // Attribute Accessor Methods
115 //===----------------------------------------------------------------------===//
117 bool Attribute::isEnumAttribute() const {
118 return pImpl && pImpl->isEnumAttribute();
121 bool Attribute::isIntAttribute() const {
122 return pImpl && pImpl->isIntAttribute();
125 bool Attribute::isStringAttribute() const {
126 return pImpl && pImpl->isStringAttribute();
129 Attribute::AttrKind Attribute::getKindAsEnum() const {
130 if (!pImpl) return None;
131 assert((isEnumAttribute() || isIntAttribute()) &&
132 "Invalid attribute type to get the kind as an enum!");
133 return pImpl ? pImpl->getKindAsEnum() : None;
136 uint64_t Attribute::getValueAsInt() const {
137 if (!pImpl) return 0;
138 assert(isIntAttribute() &&
139 "Expected the attribute to be an integer attribute!");
140 return pImpl ? pImpl->getValueAsInt() : 0;
143 StringRef Attribute::getKindAsString() const {
144 if (!pImpl) return StringRef();
145 assert(isStringAttribute() &&
146 "Invalid attribute type to get the kind as a string!");
147 return pImpl ? pImpl->getKindAsString() : StringRef();
150 StringRef Attribute::getValueAsString() const {
151 if (!pImpl) return StringRef();
152 assert(isStringAttribute() &&
153 "Invalid attribute type to get the value as a string!");
154 return pImpl ? pImpl->getValueAsString() : StringRef();
157 bool Attribute::hasAttribute(AttrKind Kind) const {
158 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
161 bool Attribute::hasAttribute(StringRef Kind) const {
162 if (!isStringAttribute()) return false;
163 return pImpl && pImpl->hasAttribute(Kind);
166 /// This returns the alignment field of an attribute as a byte alignment value.
167 unsigned Attribute::getAlignment() const {
168 assert(hasAttribute(Attribute::Alignment) &&
169 "Trying to get alignment from non-alignment attribute!");
170 return pImpl->getValueAsInt();
173 /// This returns the stack alignment field of an attribute as a byte alignment
175 unsigned Attribute::getStackAlignment() const {
176 assert(hasAttribute(Attribute::StackAlignment) &&
177 "Trying to get alignment from non-alignment attribute!");
178 return pImpl->getValueAsInt();
181 /// This returns the number of dereferenceable bytes.
182 uint64_t Attribute::getDereferenceableBytes() const {
183 assert(hasAttribute(Attribute::Dereferenceable) &&
184 "Trying to get dereferenceable bytes from "
185 "non-dereferenceable attribute!");
186 return pImpl->getValueAsInt();
189 std::string Attribute::getAsString(bool InAttrGrp) const {
190 if (!pImpl) return "";
192 if (hasAttribute(Attribute::SanitizeAddress))
193 return "sanitize_address";
194 if (hasAttribute(Attribute::AlwaysInline))
195 return "alwaysinline";
196 if (hasAttribute(Attribute::Builtin))
198 if (hasAttribute(Attribute::ByVal))
200 if (hasAttribute(Attribute::InAlloca))
202 if (hasAttribute(Attribute::InlineHint))
204 if (hasAttribute(Attribute::InReg))
206 if (hasAttribute(Attribute::JumpTable))
208 if (hasAttribute(Attribute::MinSize))
210 if (hasAttribute(Attribute::Naked))
212 if (hasAttribute(Attribute::Nest))
214 if (hasAttribute(Attribute::NoAlias))
216 if (hasAttribute(Attribute::NoBuiltin))
218 if (hasAttribute(Attribute::NoCapture))
220 if (hasAttribute(Attribute::NoDuplicate))
221 return "noduplicate";
222 if (hasAttribute(Attribute::NoImplicitFloat))
223 return "noimplicitfloat";
224 if (hasAttribute(Attribute::NoInline))
226 if (hasAttribute(Attribute::NonLazyBind))
227 return "nonlazybind";
228 if (hasAttribute(Attribute::NonNull))
230 if (hasAttribute(Attribute::NoRedZone))
232 if (hasAttribute(Attribute::NoReturn))
234 if (hasAttribute(Attribute::NoUnwind))
236 if (hasAttribute(Attribute::OptimizeNone))
238 if (hasAttribute(Attribute::OptimizeForSize))
240 if (hasAttribute(Attribute::ReadNone))
242 if (hasAttribute(Attribute::ReadOnly))
244 if (hasAttribute(Attribute::Returned))
246 if (hasAttribute(Attribute::ReturnsTwice))
247 return "returns_twice";
248 if (hasAttribute(Attribute::SExt))
250 if (hasAttribute(Attribute::StackProtect))
252 if (hasAttribute(Attribute::StackProtectReq))
254 if (hasAttribute(Attribute::StackProtectStrong))
256 if (hasAttribute(Attribute::StructRet))
258 if (hasAttribute(Attribute::SanitizeThread))
259 return "sanitize_thread";
260 if (hasAttribute(Attribute::SanitizeMemory))
261 return "sanitize_memory";
262 if (hasAttribute(Attribute::UWTable))
264 if (hasAttribute(Attribute::ZExt))
266 if (hasAttribute(Attribute::Cold))
269 // FIXME: These should be output like this:
274 if (hasAttribute(Attribute::Alignment)) {
277 Result += (InAttrGrp) ? "=" : " ";
278 Result += utostr(getValueAsInt());
282 if (hasAttribute(Attribute::StackAlignment)) {
284 Result += "alignstack";
287 Result += utostr(getValueAsInt());
290 Result += utostr(getValueAsInt());
296 if (hasAttribute(Attribute::Dereferenceable)) {
298 Result += "dereferenceable";
301 Result += utostr(getValueAsInt());
304 Result += utostr(getValueAsInt());
310 // Convert target-dependent attributes to strings of the form:
315 if (isStringAttribute()) {
317 Result += '\"' + getKindAsString().str() + '"';
319 StringRef Val = pImpl->getValueAsString();
320 if (Val.empty()) return Result;
322 Result += "=\"" + Val.str() + '"';
326 llvm_unreachable("Unknown attribute");
329 bool Attribute::operator<(Attribute A) const {
330 if (!pImpl && !A.pImpl) return false;
331 if (!pImpl) return true;
332 if (!A.pImpl) return false;
333 return *pImpl < *A.pImpl;
336 //===----------------------------------------------------------------------===//
337 // AttributeImpl Definition
338 //===----------------------------------------------------------------------===//
340 // Pin the vtables to this file.
341 AttributeImpl::~AttributeImpl() {}
342 void EnumAttributeImpl::anchor() {}
343 void IntAttributeImpl::anchor() {}
344 void StringAttributeImpl::anchor() {}
346 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
347 if (isStringAttribute()) return false;
348 return getKindAsEnum() == A;
351 bool AttributeImpl::hasAttribute(StringRef Kind) const {
352 if (!isStringAttribute()) return false;
353 return getKindAsString() == Kind;
356 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
357 assert(isEnumAttribute() || isIntAttribute());
358 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
361 uint64_t AttributeImpl::getValueAsInt() const {
362 assert(isIntAttribute());
363 return static_cast<const IntAttributeImpl *>(this)->getValue();
366 StringRef AttributeImpl::getKindAsString() const {
367 assert(isStringAttribute());
368 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
371 StringRef AttributeImpl::getValueAsString() const {
372 assert(isStringAttribute());
373 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
376 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
377 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
378 // relative to their enum value) and then strings.
379 if (isEnumAttribute()) {
380 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
381 if (AI.isIntAttribute()) return true;
382 if (AI.isStringAttribute()) return true;
385 if (isIntAttribute()) {
386 if (AI.isEnumAttribute()) return false;
387 if (AI.isIntAttribute()) return getValueAsInt() < AI.getValueAsInt();
388 if (AI.isStringAttribute()) return true;
391 if (AI.isEnumAttribute()) return false;
392 if (AI.isIntAttribute()) return false;
393 if (getKindAsString() == AI.getKindAsString())
394 return getValueAsString() < AI.getValueAsString();
395 return getKindAsString() < AI.getKindAsString();
398 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
399 // FIXME: Remove this.
401 case Attribute::EndAttrKinds:
402 llvm_unreachable("Synthetic enumerators which should never get here");
404 case Attribute::None: return 0;
405 case Attribute::ZExt: return 1 << 0;
406 case Attribute::SExt: return 1 << 1;
407 case Attribute::NoReturn: return 1 << 2;
408 case Attribute::InReg: return 1 << 3;
409 case Attribute::StructRet: return 1 << 4;
410 case Attribute::NoUnwind: return 1 << 5;
411 case Attribute::NoAlias: return 1 << 6;
412 case Attribute::ByVal: return 1 << 7;
413 case Attribute::Nest: return 1 << 8;
414 case Attribute::ReadNone: return 1 << 9;
415 case Attribute::ReadOnly: return 1 << 10;
416 case Attribute::NoInline: return 1 << 11;
417 case Attribute::AlwaysInline: return 1 << 12;
418 case Attribute::OptimizeForSize: return 1 << 13;
419 case Attribute::StackProtect: return 1 << 14;
420 case Attribute::StackProtectReq: return 1 << 15;
421 case Attribute::Alignment: return 31 << 16;
422 case Attribute::NoCapture: return 1 << 21;
423 case Attribute::NoRedZone: return 1 << 22;
424 case Attribute::NoImplicitFloat: return 1 << 23;
425 case Attribute::Naked: return 1 << 24;
426 case Attribute::InlineHint: return 1 << 25;
427 case Attribute::StackAlignment: return 7 << 26;
428 case Attribute::ReturnsTwice: return 1 << 29;
429 case Attribute::UWTable: return 1 << 30;
430 case Attribute::NonLazyBind: return 1U << 31;
431 case Attribute::SanitizeAddress: return 1ULL << 32;
432 case Attribute::MinSize: return 1ULL << 33;
433 case Attribute::NoDuplicate: return 1ULL << 34;
434 case Attribute::StackProtectStrong: return 1ULL << 35;
435 case Attribute::SanitizeThread: return 1ULL << 36;
436 case Attribute::SanitizeMemory: return 1ULL << 37;
437 case Attribute::NoBuiltin: return 1ULL << 38;
438 case Attribute::Returned: return 1ULL << 39;
439 case Attribute::Cold: return 1ULL << 40;
440 case Attribute::Builtin: return 1ULL << 41;
441 case Attribute::OptimizeNone: return 1ULL << 42;
442 case Attribute::InAlloca: return 1ULL << 43;
443 case Attribute::NonNull: return 1ULL << 44;
444 case Attribute::JumpTable: return 1ULL << 45;
445 case Attribute::Dereferenceable:
446 llvm_unreachable("dereferenceable attribute not supported in raw format");
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 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
540 for (iterator I = begin(), E = end(); I != E; ++I) {
543 Str += I->getAsString(InAttrGrp);
548 //===----------------------------------------------------------------------===//
549 // AttributeSetImpl Definition
550 //===----------------------------------------------------------------------===//
552 uint64_t AttributeSetImpl::Raw(unsigned Index) const {
553 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
554 if (getSlotIndex(I) != Index) continue;
555 const AttributeSetNode *ASN = getSlotNode(I);
558 for (AttributeSetNode::iterator II = ASN->begin(),
559 IE = ASN->end(); II != IE; ++II) {
560 Attribute Attr = *II;
562 // This cannot handle string attributes.
563 if (Attr.isStringAttribute()) continue;
565 Attribute::AttrKind Kind = Attr.getKindAsEnum();
567 if (Kind == Attribute::Alignment)
568 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
569 else if (Kind == Attribute::StackAlignment)
570 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
571 else if (Kind == Attribute::Dereferenceable)
572 llvm_unreachable("dereferenceable not supported in bit mask");
574 Mask |= AttributeImpl::getAttrMask(Kind);
583 void AttributeSetImpl::dump() const {
584 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
587 //===----------------------------------------------------------------------===//
588 // AttributeSet Construction and Mutation Methods
589 //===----------------------------------------------------------------------===//
592 AttributeSet::getImpl(LLVMContext &C,
593 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
594 LLVMContextImpl *pImpl = C.pImpl;
596 AttributeSetImpl::Profile(ID, Attrs);
599 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
601 // If we didn't find any existing attributes of the same shape then
602 // create a new one and insert it.
604 // Coallocate entries after the AttributeSetImpl itself.
605 void *Mem = ::operator new(sizeof(AttributeSetImpl) +
606 sizeof(std::pair<unsigned, AttributeSetNode *>) *
608 PA = new (Mem) AttributeSetImpl(C, Attrs);
609 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
612 // Return the AttributesList that we found or created.
613 return AttributeSet(PA);
616 AttributeSet AttributeSet::get(LLVMContext &C,
617 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
618 // If there are no attributes then return a null AttributesList pointer.
620 return AttributeSet();
623 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
624 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
625 "Misordered Attributes list!");
626 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
627 "Pointless attribute!");
631 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
633 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
634 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
635 E = Attrs.end(); I != E; ) {
636 unsigned Index = I->first;
637 SmallVector<Attribute, 4> AttrVec;
638 while (I != E && I->first == Index) {
639 AttrVec.push_back(I->second);
643 AttrPairVec.push_back(std::make_pair(Index,
644 AttributeSetNode::get(C, AttrVec)));
647 return getImpl(C, AttrPairVec);
650 AttributeSet AttributeSet::get(LLVMContext &C,
651 ArrayRef<std::pair<unsigned,
652 AttributeSetNode*> > Attrs) {
653 // If there are no attributes then return a null AttributesList pointer.
655 return AttributeSet();
657 return getImpl(C, Attrs);
660 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
661 const AttrBuilder &B) {
662 if (!B.hasAttributes())
663 return AttributeSet();
665 // Add target-independent attributes.
666 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
667 for (Attribute::AttrKind Kind = Attribute::None;
668 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
669 if (!B.contains(Kind))
672 if (Kind == Attribute::Alignment)
673 Attrs.push_back(std::make_pair(Index, Attribute::
674 getWithAlignment(C, B.getAlignment())));
675 else if (Kind == Attribute::StackAlignment)
676 Attrs.push_back(std::make_pair(Index, Attribute::
677 getWithStackAlignment(C, B.getStackAlignment())));
678 else if (Kind == Attribute::Dereferenceable)
679 Attrs.push_back(std::make_pair(Index,
680 Attribute::getWithDereferenceableBytes(C,
681 B.getDereferenceableBytes())));
683 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
686 // Add target-dependent (string) attributes.
687 for (const AttrBuilder::td_type &TDA : B.td_attrs())
689 std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
691 return get(C, Attrs);
694 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
695 ArrayRef<Attribute::AttrKind> Kind) {
696 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
697 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
698 E = Kind.end(); I != E; ++I)
699 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
700 return get(C, Attrs);
703 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
704 if (Attrs.empty()) return AttributeSet();
705 if (Attrs.size() == 1) return Attrs[0];
707 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
708 AttributeSetImpl *A0 = Attrs[0].pImpl;
710 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
711 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
712 // ordered by index. Because we know that each list in Attrs is ordered by
713 // index we only need to merge each successive list in rather than doing a
715 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
716 AttributeSetImpl *AS = Attrs[I].pImpl;
718 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
719 ANVI = AttrNodeVec.begin(), ANVE;
720 for (const AttributeSetImpl::IndexAttrPair
721 *AI = AS->getNode(0),
722 *AE = AS->getNode(AS->getNumAttributes());
724 ANVE = AttrNodeVec.end();
725 while (ANVI != ANVE && ANVI->first <= AI->first)
727 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
731 return getImpl(C, AttrNodeVec);
734 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
735 Attribute::AttrKind Attr) const {
736 if (hasAttribute(Index, Attr)) return *this;
737 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
740 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
741 StringRef Kind) const {
743 B.addAttribute(Kind);
744 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
747 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
748 StringRef Kind, StringRef Value) const {
750 B.addAttribute(Kind, Value);
751 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
754 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
755 AttributeSet Attrs) const {
756 if (!pImpl) return Attrs;
757 if (!Attrs.pImpl) return *this;
760 // FIXME it is not obvious how this should work for alignment. For now, say
761 // we can't change a known alignment.
762 unsigned OldAlign = getParamAlignment(Index);
763 unsigned NewAlign = Attrs.getParamAlignment(Index);
764 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
765 "Attempt to change alignment!");
768 // Add the attribute slots before the one we're trying to add.
769 SmallVector<AttributeSet, 4> AttrSet;
770 uint64_t NumAttrs = pImpl->getNumAttributes();
772 uint64_t LastIndex = 0;
773 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
774 if (getSlotIndex(I) >= Index) {
775 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
779 AttrSet.push_back(getSlotAttributes(I));
782 // Now add the attribute into the correct slot. There may already be an
783 // AttributeSet there.
784 AttrBuilder B(AS, Index);
786 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
787 if (Attrs.getSlotIndex(I) == Index) {
788 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
789 IE = Attrs.pImpl->end(I); II != IE; ++II)
794 AttrSet.push_back(AttributeSet::get(C, Index, B));
796 // Add the remaining attribute slots.
797 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
798 AttrSet.push_back(getSlotAttributes(I));
800 return get(C, AttrSet);
803 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
804 Attribute::AttrKind Attr) const {
805 if (!hasAttribute(Index, Attr)) return *this;
806 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
809 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
810 AttributeSet Attrs) const {
811 if (!pImpl) return AttributeSet();
812 if (!Attrs.pImpl) return *this;
815 // FIXME it is not obvious how this should work for alignment.
816 // For now, say we can't pass in alignment, which no current use does.
817 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
818 "Attempt to change alignment!");
821 // Add the attribute slots before the one we're trying to add.
822 SmallVector<AttributeSet, 4> AttrSet;
823 uint64_t NumAttrs = pImpl->getNumAttributes();
825 uint64_t LastIndex = 0;
826 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
827 if (getSlotIndex(I) >= Index) {
828 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
832 AttrSet.push_back(getSlotAttributes(I));
835 // Now remove the attribute from the correct slot. There may already be an
836 // AttributeSet there.
837 AttrBuilder B(AS, Index);
839 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
840 if (Attrs.getSlotIndex(I) == Index) {
841 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
845 AttrSet.push_back(AttributeSet::get(C, Index, B));
847 // Add the remaining attribute slots.
848 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
849 AttrSet.push_back(getSlotAttributes(I));
851 return get(C, AttrSet);
854 //===----------------------------------------------------------------------===//
855 // AttributeSet Accessor Methods
856 //===----------------------------------------------------------------------===//
858 LLVMContext &AttributeSet::getContext() const {
859 return pImpl->getContext();
862 AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
863 return pImpl && hasAttributes(Index) ?
864 AttributeSet::get(pImpl->getContext(),
865 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
866 std::make_pair(Index, getAttributes(Index)))) :
870 AttributeSet AttributeSet::getRetAttributes() const {
871 return pImpl && hasAttributes(ReturnIndex) ?
872 AttributeSet::get(pImpl->getContext(),
873 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
874 std::make_pair(ReturnIndex,
875 getAttributes(ReturnIndex)))) :
879 AttributeSet AttributeSet::getFnAttributes() const {
880 return pImpl && hasAttributes(FunctionIndex) ?
881 AttributeSet::get(pImpl->getContext(),
882 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
883 std::make_pair(FunctionIndex,
884 getAttributes(FunctionIndex)))) :
888 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
889 AttributeSetNode *ASN = getAttributes(Index);
890 return ASN ? ASN->hasAttribute(Kind) : false;
893 bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
894 AttributeSetNode *ASN = getAttributes(Index);
895 return ASN ? ASN->hasAttribute(Kind) : false;
898 bool AttributeSet::hasAttributes(unsigned Index) const {
899 AttributeSetNode *ASN = getAttributes(Index);
900 return ASN ? ASN->hasAttributes() : false;
903 /// \brief Return true if the specified attribute is set for at least one
904 /// parameter or for the return value.
905 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
906 if (!pImpl) return false;
908 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
909 for (AttributeSetImpl::iterator II = pImpl->begin(I),
910 IE = pImpl->end(I); II != IE; ++II)
911 if (II->hasAttribute(Attr))
917 Attribute AttributeSet::getAttribute(unsigned Index,
918 Attribute::AttrKind Kind) const {
919 AttributeSetNode *ASN = getAttributes(Index);
920 return ASN ? ASN->getAttribute(Kind) : Attribute();
923 Attribute AttributeSet::getAttribute(unsigned Index,
924 StringRef Kind) const {
925 AttributeSetNode *ASN = getAttributes(Index);
926 return ASN ? ASN->getAttribute(Kind) : Attribute();
929 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
930 AttributeSetNode *ASN = getAttributes(Index);
931 return ASN ? ASN->getAlignment() : 0;
934 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
935 AttributeSetNode *ASN = getAttributes(Index);
936 return ASN ? ASN->getStackAlignment() : 0;
939 uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
940 AttributeSetNode *ASN = getAttributes(Index);
941 return ASN ? ASN->getDereferenceableBytes() : 0;
944 std::string AttributeSet::getAsString(unsigned Index,
945 bool InAttrGrp) const {
946 AttributeSetNode *ASN = getAttributes(Index);
947 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
950 /// \brief The attributes for the specified index are returned.
951 AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
952 if (!pImpl) return nullptr;
954 // Loop through to find the attribute node we want.
955 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
956 if (pImpl->getSlotIndex(I) == Index)
957 return pImpl->getSlotNode(I);
962 AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
964 return ArrayRef<Attribute>().begin();
965 return pImpl->begin(Slot);
968 AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
970 return ArrayRef<Attribute>().end();
971 return pImpl->end(Slot);
974 //===----------------------------------------------------------------------===//
975 // AttributeSet Introspection Methods
976 //===----------------------------------------------------------------------===//
978 /// \brief Return the number of slots used in this attribute list. This is the
979 /// number of arguments that have an attribute set on them (including the
980 /// function itself).
981 unsigned AttributeSet::getNumSlots() const {
982 return pImpl ? pImpl->getNumAttributes() : 0;
985 unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
986 assert(pImpl && Slot < pImpl->getNumAttributes() &&
987 "Slot # out of range!");
988 return pImpl->getSlotIndex(Slot);
991 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
992 assert(pImpl && Slot < pImpl->getNumAttributes() &&
993 "Slot # out of range!");
994 return pImpl->getSlotAttributes(Slot);
997 uint64_t AttributeSet::Raw(unsigned Index) const {
998 // FIXME: Remove this.
999 return pImpl ? pImpl->Raw(Index) : 0;
1002 void AttributeSet::dump() const {
1005 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1006 uint64_t Index = getSlotIndex(i);
1012 dbgs() << " => " << getAsString(Index) << " }\n";
1018 //===----------------------------------------------------------------------===//
1019 // AttrBuilder Method Implementations
1020 //===----------------------------------------------------------------------===//
1022 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
1023 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0) {
1024 AttributeSetImpl *pImpl = AS.pImpl;
1027 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
1028 if (pImpl->getSlotIndex(I) != Index) continue;
1030 for (AttributeSetImpl::iterator II = pImpl->begin(I),
1031 IE = pImpl->end(I); II != IE; ++II)
1038 void AttrBuilder::clear() {
1040 Alignment = StackAlignment = DerefBytes = 0;
1043 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
1044 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1045 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
1046 Val != Attribute::Dereferenceable &&
1047 "Adding integer attribute without adding a value!");
1052 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
1053 if (Attr.isStringAttribute()) {
1054 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1058 Attribute::AttrKind Kind = Attr.getKindAsEnum();
1061 if (Kind == Attribute::Alignment)
1062 Alignment = Attr.getAlignment();
1063 else if (Kind == Attribute::StackAlignment)
1064 StackAlignment = Attr.getStackAlignment();
1065 else if (Kind == Attribute::Dereferenceable)
1066 DerefBytes = Attr.getDereferenceableBytes();
1070 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1071 TargetDepAttrs[A] = V;
1075 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
1076 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1079 if (Val == Attribute::Alignment)
1081 else if (Val == Attribute::StackAlignment)
1083 else if (Val == Attribute::Dereferenceable)
1089 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
1090 unsigned Slot = ~0U;
1091 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1092 if (A.getSlotIndex(I) == Index) {
1097 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
1099 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
1100 Attribute Attr = *I;
1101 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1102 Attribute::AttrKind Kind = I->getKindAsEnum();
1103 Attrs[Kind] = false;
1105 if (Kind == Attribute::Alignment)
1107 else if (Kind == Attribute::StackAlignment)
1109 else if (Kind == Attribute::Dereferenceable)
1112 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1113 std::map<std::string, std::string>::iterator
1114 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1115 if (Iter != TargetDepAttrs.end())
1116 TargetDepAttrs.erase(Iter);
1123 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1124 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1125 if (I != TargetDepAttrs.end())
1126 TargetDepAttrs.erase(I);
1130 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
1131 if (Align == 0) return *this;
1133 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1134 assert(Align <= 0x40000000 && "Alignment too large.");
1136 Attrs[Attribute::Alignment] = true;
1141 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1142 // Default alignment, allow the target to define how to align it.
1143 if (Align == 0) return *this;
1145 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1146 assert(Align <= 0x100 && "Alignment too large.");
1148 Attrs[Attribute::StackAlignment] = true;
1149 StackAlignment = Align;
1153 AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1154 if (Bytes == 0) return *this;
1156 Attrs[Attribute::Dereferenceable] = true;
1161 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1162 // FIXME: What if both have alignments, but they don't match?!
1164 Alignment = B.Alignment;
1166 if (!StackAlignment)
1167 StackAlignment = B.StackAlignment;
1170 DerefBytes = B.DerefBytes;
1174 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1175 E = B.TargetDepAttrs.end(); I != E; ++I)
1176 TargetDepAttrs[I->first] = I->second;
1181 bool AttrBuilder::contains(StringRef A) const {
1182 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1185 bool AttrBuilder::hasAttributes() const {
1186 return !Attrs.none() || !TargetDepAttrs.empty();
1189 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
1190 unsigned Slot = ~0U;
1191 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1192 if (A.getSlotIndex(I) == Index) {
1197 assert(Slot != ~0U && "Couldn't find the index!");
1199 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
1201 Attribute Attr = *I;
1202 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1203 if (Attrs[I->getKindAsEnum()])
1206 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1207 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1214 bool AttrBuilder::hasAlignmentAttr() const {
1215 return Alignment != 0;
1218 bool AttrBuilder::operator==(const AttrBuilder &B) {
1219 if (Attrs != B.Attrs)
1222 for (td_const_iterator I = TargetDepAttrs.begin(),
1223 E = TargetDepAttrs.end(); I != E; ++I)
1224 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1227 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1228 DerefBytes == B.DerefBytes;
1231 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
1232 // FIXME: Remove this in 4.0.
1233 if (!Val) return *this;
1235 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1236 I = Attribute::AttrKind(I + 1)) {
1237 if (I == Attribute::Dereferenceable)
1239 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1242 if (I == Attribute::Alignment)
1243 Alignment = 1ULL << ((A >> 16) - 1);
1244 else if (I == Attribute::StackAlignment)
1245 StackAlignment = 1ULL << ((A >> 26)-1);
1252 //===----------------------------------------------------------------------===//
1253 // AttributeFuncs Function Defintions
1254 //===----------------------------------------------------------------------===//
1256 /// \brief Which attributes cannot be applied to a type.
1257 AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
1258 AttrBuilder Incompatible;
1260 if (!Ty->isIntegerTy())
1261 // Attribute that only apply to integers.
1262 Incompatible.addAttribute(Attribute::SExt)
1263 .addAttribute(Attribute::ZExt);
1265 if (!Ty->isPointerTy())
1266 // Attribute that only apply to pointers.
1267 Incompatible.addAttribute(Attribute::ByVal)
1268 .addAttribute(Attribute::Nest)
1269 .addAttribute(Attribute::NoAlias)
1270 .addAttribute(Attribute::NoCapture)
1271 .addAttribute(Attribute::NonNull)
1272 .addDereferenceableAttr(1) // the int here is ignored
1273 .addAttribute(Attribute::ReadNone)
1274 .addAttribute(Attribute::ReadOnly)
1275 .addAttribute(Attribute::StructRet)
1276 .addAttribute(Attribute::InAlloca);
1278 return AttributeSet::get(Ty->getContext(), Index, Incompatible);