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 AlignAttributeImpl(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 //===----------------------------------------------------------------------===//
92 // Attribute Accessor Methods
93 //===----------------------------------------------------------------------===//
95 bool Attribute::isEnumAttribute() const {
96 return pImpl && pImpl->isEnumAttribute();
99 bool Attribute::isAlignAttribute() const {
100 return pImpl && pImpl->isAlignAttribute();
103 bool Attribute::isStringAttribute() const {
104 return pImpl && pImpl->isStringAttribute();
107 Attribute::AttrKind Attribute::getKindAsEnum() const {
108 if (!pImpl) return None;
109 assert((isEnumAttribute() || isAlignAttribute()) &&
110 "Invalid attribute type to get the kind as an enum!");
111 return pImpl ? pImpl->getKindAsEnum() : None;
114 uint64_t Attribute::getValueAsInt() const {
115 if (!pImpl) return 0;
116 assert(isAlignAttribute() &&
117 "Expected the attribute to be an alignment attribute!");
118 return pImpl ? pImpl->getValueAsInt() : 0;
121 StringRef Attribute::getKindAsString() const {
122 if (!pImpl) return StringRef();
123 assert(isStringAttribute() &&
124 "Invalid attribute type to get the kind as a string!");
125 return pImpl ? pImpl->getKindAsString() : StringRef();
128 StringRef Attribute::getValueAsString() const {
129 if (!pImpl) return StringRef();
130 assert(isStringAttribute() &&
131 "Invalid attribute type to get the value as a string!");
132 return pImpl ? pImpl->getValueAsString() : StringRef();
135 bool Attribute::hasAttribute(AttrKind Kind) const {
136 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
139 bool Attribute::hasAttribute(StringRef Kind) const {
140 if (!isStringAttribute()) return false;
141 return pImpl && pImpl->hasAttribute(Kind);
144 /// This returns the alignment field of an attribute as a byte alignment value.
145 unsigned Attribute::getAlignment() const {
146 assert(hasAttribute(Attribute::Alignment) &&
147 "Trying to get alignment from non-alignment attribute!");
148 return pImpl->getValueAsInt();
151 /// This returns the stack alignment field of an attribute as a byte alignment
153 unsigned Attribute::getStackAlignment() const {
154 assert(hasAttribute(Attribute::StackAlignment) &&
155 "Trying to get alignment from non-alignment attribute!");
156 return pImpl->getValueAsInt();
159 std::string Attribute::getAsString(bool InAttrGrp) const {
160 if (!pImpl) return "";
162 if (hasAttribute(Attribute::SanitizeAddress))
163 return "sanitize_address";
164 if (hasAttribute(Attribute::AlwaysInline))
165 return "alwaysinline";
166 if (hasAttribute(Attribute::Builtin))
168 if (hasAttribute(Attribute::ByVal))
170 if (hasAttribute(Attribute::InAlloca))
172 if (hasAttribute(Attribute::InlineHint))
174 if (hasAttribute(Attribute::InReg))
176 if (hasAttribute(Attribute::MinSize))
178 if (hasAttribute(Attribute::Naked))
180 if (hasAttribute(Attribute::Nest))
182 if (hasAttribute(Attribute::NoAlias))
184 if (hasAttribute(Attribute::NoBuiltin))
186 if (hasAttribute(Attribute::NoCapture))
188 if (hasAttribute(Attribute::NoDuplicate))
189 return "noduplicate";
190 if (hasAttribute(Attribute::NoImplicitFloat))
191 return "noimplicitfloat";
192 if (hasAttribute(Attribute::NoInline))
194 if (hasAttribute(Attribute::NonLazyBind))
195 return "nonlazybind";
196 if (hasAttribute(Attribute::NonNull))
198 if (hasAttribute(Attribute::NoRedZone))
200 if (hasAttribute(Attribute::NoReturn))
202 if (hasAttribute(Attribute::NoUnwind))
204 if (hasAttribute(Attribute::OptimizeNone))
206 if (hasAttribute(Attribute::OptimizeForSize))
208 if (hasAttribute(Attribute::ReadNone))
210 if (hasAttribute(Attribute::ReadOnly))
212 if (hasAttribute(Attribute::Returned))
214 if (hasAttribute(Attribute::ReturnsTwice))
215 return "returns_twice";
216 if (hasAttribute(Attribute::SExt))
218 if (hasAttribute(Attribute::StackProtect))
220 if (hasAttribute(Attribute::StackProtectReq))
222 if (hasAttribute(Attribute::StackProtectStrong))
224 if (hasAttribute(Attribute::StructRet))
226 if (hasAttribute(Attribute::SanitizeThread))
227 return "sanitize_thread";
228 if (hasAttribute(Attribute::SanitizeMemory))
229 return "sanitize_memory";
230 if (hasAttribute(Attribute::UWTable))
232 if (hasAttribute(Attribute::ZExt))
234 if (hasAttribute(Attribute::Cold))
237 // FIXME: These should be output like this:
242 if (hasAttribute(Attribute::Alignment)) {
245 Result += (InAttrGrp) ? "=" : " ";
246 Result += utostr(getValueAsInt());
250 if (hasAttribute(Attribute::StackAlignment)) {
252 Result += "alignstack";
255 Result += utostr(getValueAsInt());
258 Result += utostr(getValueAsInt());
264 // Convert target-dependent attributes to strings of the form:
269 if (isStringAttribute()) {
271 Result += '\"' + getKindAsString().str() + '"';
273 StringRef Val = pImpl->getValueAsString();
274 if (Val.empty()) return Result;
276 Result += "=\"" + Val.str() + '"';
280 llvm_unreachable("Unknown attribute");
283 bool Attribute::operator<(Attribute A) const {
284 if (!pImpl && !A.pImpl) return false;
285 if (!pImpl) return true;
286 if (!A.pImpl) return false;
287 return *pImpl < *A.pImpl;
290 //===----------------------------------------------------------------------===//
291 // AttributeImpl Definition
292 //===----------------------------------------------------------------------===//
294 // Pin the vtabels to this file.
295 AttributeImpl::~AttributeImpl() {}
296 void EnumAttributeImpl::anchor() {}
297 void AlignAttributeImpl::anchor() {}
298 void StringAttributeImpl::anchor() {}
300 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
301 if (isStringAttribute()) return false;
302 return getKindAsEnum() == A;
305 bool AttributeImpl::hasAttribute(StringRef Kind) const {
306 if (!isStringAttribute()) return false;
307 return getKindAsString() == Kind;
310 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
311 assert(isEnumAttribute() || isAlignAttribute());
312 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
315 uint64_t AttributeImpl::getValueAsInt() const {
316 assert(isAlignAttribute());
317 return static_cast<const AlignAttributeImpl *>(this)->getAlignment();
320 StringRef AttributeImpl::getKindAsString() const {
321 assert(isStringAttribute());
322 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
325 StringRef AttributeImpl::getValueAsString() const {
326 assert(isStringAttribute());
327 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
330 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
331 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
332 // relative to their enum value) and then strings.
333 if (isEnumAttribute()) {
334 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
335 if (AI.isAlignAttribute()) return true;
336 if (AI.isStringAttribute()) return true;
339 if (isAlignAttribute()) {
340 if (AI.isEnumAttribute()) return false;
341 if (AI.isAlignAttribute()) return getValueAsInt() < AI.getValueAsInt();
342 if (AI.isStringAttribute()) return true;
345 if (AI.isEnumAttribute()) return false;
346 if (AI.isAlignAttribute()) return false;
347 if (getKindAsString() == AI.getKindAsString())
348 return getValueAsString() < AI.getValueAsString();
349 return getKindAsString() < AI.getKindAsString();
352 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
353 // FIXME: Remove this.
355 case Attribute::EndAttrKinds:
356 llvm_unreachable("Synthetic enumerators which should never get here");
358 case Attribute::None: return 0;
359 case Attribute::ZExt: return 1 << 0;
360 case Attribute::SExt: return 1 << 1;
361 case Attribute::NoReturn: return 1 << 2;
362 case Attribute::InReg: return 1 << 3;
363 case Attribute::StructRet: return 1 << 4;
364 case Attribute::NoUnwind: return 1 << 5;
365 case Attribute::NoAlias: return 1 << 6;
366 case Attribute::ByVal: return 1 << 7;
367 case Attribute::Nest: return 1 << 8;
368 case Attribute::ReadNone: return 1 << 9;
369 case Attribute::ReadOnly: return 1 << 10;
370 case Attribute::NoInline: return 1 << 11;
371 case Attribute::AlwaysInline: return 1 << 12;
372 case Attribute::OptimizeForSize: return 1 << 13;
373 case Attribute::StackProtect: return 1 << 14;
374 case Attribute::StackProtectReq: return 1 << 15;
375 case Attribute::Alignment: return 31 << 16;
376 case Attribute::NoCapture: return 1 << 21;
377 case Attribute::NoRedZone: return 1 << 22;
378 case Attribute::NoImplicitFloat: return 1 << 23;
379 case Attribute::Naked: return 1 << 24;
380 case Attribute::InlineHint: return 1 << 25;
381 case Attribute::StackAlignment: return 7 << 26;
382 case Attribute::ReturnsTwice: return 1 << 29;
383 case Attribute::UWTable: return 1 << 30;
384 case Attribute::NonLazyBind: return 1U << 31;
385 case Attribute::SanitizeAddress: return 1ULL << 32;
386 case Attribute::MinSize: return 1ULL << 33;
387 case Attribute::NoDuplicate: return 1ULL << 34;
388 case Attribute::StackProtectStrong: return 1ULL << 35;
389 case Attribute::SanitizeThread: return 1ULL << 36;
390 case Attribute::SanitizeMemory: return 1ULL << 37;
391 case Attribute::NoBuiltin: return 1ULL << 38;
392 case Attribute::Returned: return 1ULL << 39;
393 case Attribute::Cold: return 1ULL << 40;
394 case Attribute::Builtin: return 1ULL << 41;
395 case Attribute::OptimizeNone: return 1ULL << 42;
396 case Attribute::InAlloca: return 1ULL << 43;
397 case Attribute::NonNull: return 1ULL << 44;
399 llvm_unreachable("Unsupported attribute type");
402 //===----------------------------------------------------------------------===//
403 // AttributeSetNode Definition
404 //===----------------------------------------------------------------------===//
406 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
407 ArrayRef<Attribute> Attrs) {
411 // Otherwise, build a key to look up the existing attributes.
412 LLVMContextImpl *pImpl = C.pImpl;
415 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
416 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
418 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
419 E = SortedAttrs.end(); I != E; ++I)
423 AttributeSetNode *PA =
424 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
426 // If we didn't find any existing attributes of the same shape then create a
427 // new one and insert it.
429 // Coallocate entries after the AttributeSetNode itself.
430 void *Mem = ::operator new(sizeof(AttributeSetNode) +
431 sizeof(Attribute) * SortedAttrs.size());
432 PA = new (Mem) AttributeSetNode(SortedAttrs);
433 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
436 // Return the AttributesListNode that we found or created.
440 bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
441 for (iterator I = begin(), E = end(); I != E; ++I)
442 if (I->hasAttribute(Kind))
447 bool AttributeSetNode::hasAttribute(StringRef Kind) const {
448 for (iterator I = begin(), E = end(); I != E; ++I)
449 if (I->hasAttribute(Kind))
454 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
455 for (iterator I = begin(), E = end(); I != E; ++I)
456 if (I->hasAttribute(Kind))
461 Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
462 for (iterator I = begin(), E = end(); I != E; ++I)
463 if (I->hasAttribute(Kind))
468 unsigned AttributeSetNode::getAlignment() const {
469 for (iterator I = begin(), E = end(); I != E; ++I)
470 if (I->hasAttribute(Attribute::Alignment))
471 return I->getAlignment();
475 unsigned AttributeSetNode::getStackAlignment() const {
476 for (iterator I = begin(), E = end(); I != E; ++I)
477 if (I->hasAttribute(Attribute::StackAlignment))
478 return I->getStackAlignment();
482 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
484 for (iterator I = begin(), E = end(); I != E; ++I) {
487 Str += I->getAsString(InAttrGrp);
492 //===----------------------------------------------------------------------===//
493 // AttributeSetImpl Definition
494 //===----------------------------------------------------------------------===//
496 uint64_t AttributeSetImpl::Raw(unsigned Index) const {
497 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
498 if (getSlotIndex(I) != Index) continue;
499 const AttributeSetNode *ASN = getSlotNode(I);
502 for (AttributeSetNode::iterator II = ASN->begin(),
503 IE = ASN->end(); II != IE; ++II) {
504 Attribute Attr = *II;
506 // This cannot handle string attributes.
507 if (Attr.isStringAttribute()) continue;
509 Attribute::AttrKind Kind = Attr.getKindAsEnum();
511 if (Kind == Attribute::Alignment)
512 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
513 else if (Kind == Attribute::StackAlignment)
514 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
516 Mask |= AttributeImpl::getAttrMask(Kind);
525 void AttributeSetImpl::dump() const {
526 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
529 //===----------------------------------------------------------------------===//
530 // AttributeSet Construction and Mutation Methods
531 //===----------------------------------------------------------------------===//
534 AttributeSet::getImpl(LLVMContext &C,
535 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
536 LLVMContextImpl *pImpl = C.pImpl;
538 AttributeSetImpl::Profile(ID, Attrs);
541 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
543 // If we didn't find any existing attributes of the same shape then
544 // create a new one and insert it.
546 // Coallocate entries after the AttributeSetImpl itself.
547 void *Mem = ::operator new(sizeof(AttributeSetImpl) +
548 sizeof(std::pair<unsigned, AttributeSetNode *>) *
550 PA = new (Mem) AttributeSetImpl(C, Attrs);
551 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
554 // Return the AttributesList that we found or created.
555 return AttributeSet(PA);
558 AttributeSet AttributeSet::get(LLVMContext &C,
559 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
560 // If there are no attributes then return a null AttributesList pointer.
562 return AttributeSet();
565 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
566 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
567 "Misordered Attributes list!");
568 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
569 "Pointless attribute!");
573 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
575 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
576 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
577 E = Attrs.end(); I != E; ) {
578 unsigned Index = I->first;
579 SmallVector<Attribute, 4> AttrVec;
580 while (I != E && I->first == Index) {
581 AttrVec.push_back(I->second);
585 AttrPairVec.push_back(std::make_pair(Index,
586 AttributeSetNode::get(C, AttrVec)));
589 return getImpl(C, AttrPairVec);
592 AttributeSet AttributeSet::get(LLVMContext &C,
593 ArrayRef<std::pair<unsigned,
594 AttributeSetNode*> > Attrs) {
595 // If there are no attributes then return a null AttributesList pointer.
597 return AttributeSet();
599 return getImpl(C, Attrs);
602 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
603 const AttrBuilder &B) {
604 if (!B.hasAttributes())
605 return AttributeSet();
607 // Add target-independent attributes.
608 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
609 for (Attribute::AttrKind Kind = Attribute::None;
610 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
611 if (!B.contains(Kind))
614 if (Kind == Attribute::Alignment)
615 Attrs.push_back(std::make_pair(Index, Attribute::
616 getWithAlignment(C, B.getAlignment())));
617 else if (Kind == Attribute::StackAlignment)
618 Attrs.push_back(std::make_pair(Index, Attribute::
619 getWithStackAlignment(C, B.getStackAlignment())));
621 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
624 // Add target-dependent (string) attributes.
625 for (const AttrBuilder::td_type &TDA : B.td_attrs())
627 std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
629 return get(C, Attrs);
632 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
633 ArrayRef<Attribute::AttrKind> Kind) {
634 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
635 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
636 E = Kind.end(); I != E; ++I)
637 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
638 return get(C, Attrs);
641 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
642 if (Attrs.empty()) return AttributeSet();
643 if (Attrs.size() == 1) return Attrs[0];
645 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
646 AttributeSetImpl *A0 = Attrs[0].pImpl;
648 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
649 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
650 // ordered by index. Because we know that each list in Attrs is ordered by
651 // index we only need to merge each successive list in rather than doing a
653 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
654 AttributeSetImpl *AS = Attrs[I].pImpl;
656 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
657 ANVI = AttrNodeVec.begin(), ANVE;
658 for (const AttributeSetImpl::IndexAttrPair
659 *AI = AS->getNode(0),
660 *AE = AS->getNode(AS->getNumAttributes());
662 ANVE = AttrNodeVec.end();
663 while (ANVI != ANVE && ANVI->first <= AI->first)
665 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
669 return getImpl(C, AttrNodeVec);
672 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
673 Attribute::AttrKind Attr) const {
674 if (hasAttribute(Index, Attr)) return *this;
675 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
678 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
679 StringRef Kind) const {
681 B.addAttribute(Kind);
682 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
685 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
686 StringRef Kind, StringRef Value) const {
688 B.addAttribute(Kind, Value);
689 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
692 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
693 AttributeSet Attrs) const {
694 if (!pImpl) return Attrs;
695 if (!Attrs.pImpl) return *this;
698 // FIXME it is not obvious how this should work for alignment. For now, say
699 // we can't change a known alignment.
700 unsigned OldAlign = getParamAlignment(Index);
701 unsigned NewAlign = Attrs.getParamAlignment(Index);
702 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
703 "Attempt to change alignment!");
706 // Add the attribute slots before the one we're trying to add.
707 SmallVector<AttributeSet, 4> AttrSet;
708 uint64_t NumAttrs = pImpl->getNumAttributes();
710 uint64_t LastIndex = 0;
711 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
712 if (getSlotIndex(I) >= Index) {
713 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
717 AttrSet.push_back(getSlotAttributes(I));
720 // Now add the attribute into the correct slot. There may already be an
721 // AttributeSet there.
722 AttrBuilder B(AS, Index);
724 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
725 if (Attrs.getSlotIndex(I) == Index) {
726 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
727 IE = Attrs.pImpl->end(I); II != IE; ++II)
732 AttrSet.push_back(AttributeSet::get(C, Index, B));
734 // Add the remaining attribute slots.
735 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
736 AttrSet.push_back(getSlotAttributes(I));
738 return get(C, AttrSet);
741 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
742 Attribute::AttrKind Attr) const {
743 if (!hasAttribute(Index, Attr)) return *this;
744 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
747 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
748 AttributeSet Attrs) const {
749 if (!pImpl) return AttributeSet();
750 if (!Attrs.pImpl) return *this;
753 // FIXME it is not obvious how this should work for alignment.
754 // For now, say we can't pass in alignment, which no current use does.
755 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
756 "Attempt to change alignment!");
759 // Add the attribute slots before the one we're trying to add.
760 SmallVector<AttributeSet, 4> AttrSet;
761 uint64_t NumAttrs = pImpl->getNumAttributes();
763 uint64_t LastIndex = 0;
764 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
765 if (getSlotIndex(I) >= Index) {
766 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
770 AttrSet.push_back(getSlotAttributes(I));
773 // Now remove the attribute from the correct slot. There may already be an
774 // AttributeSet there.
775 AttrBuilder B(AS, Index);
777 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
778 if (Attrs.getSlotIndex(I) == Index) {
779 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
783 AttrSet.push_back(AttributeSet::get(C, Index, B));
785 // Add the remaining attribute slots.
786 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
787 AttrSet.push_back(getSlotAttributes(I));
789 return get(C, AttrSet);
792 //===----------------------------------------------------------------------===//
793 // AttributeSet Accessor Methods
794 //===----------------------------------------------------------------------===//
796 LLVMContext &AttributeSet::getContext() const {
797 return pImpl->getContext();
800 AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
801 return pImpl && hasAttributes(Index) ?
802 AttributeSet::get(pImpl->getContext(),
803 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
804 std::make_pair(Index, getAttributes(Index)))) :
808 AttributeSet AttributeSet::getRetAttributes() const {
809 return pImpl && hasAttributes(ReturnIndex) ?
810 AttributeSet::get(pImpl->getContext(),
811 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
812 std::make_pair(ReturnIndex,
813 getAttributes(ReturnIndex)))) :
817 AttributeSet AttributeSet::getFnAttributes() const {
818 return pImpl && hasAttributes(FunctionIndex) ?
819 AttributeSet::get(pImpl->getContext(),
820 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
821 std::make_pair(FunctionIndex,
822 getAttributes(FunctionIndex)))) :
826 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
827 AttributeSetNode *ASN = getAttributes(Index);
828 return ASN ? ASN->hasAttribute(Kind) : false;
831 bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
832 AttributeSetNode *ASN = getAttributes(Index);
833 return ASN ? ASN->hasAttribute(Kind) : false;
836 bool AttributeSet::hasAttributes(unsigned Index) const {
837 AttributeSetNode *ASN = getAttributes(Index);
838 return ASN ? ASN->hasAttributes() : false;
841 /// \brief Return true if the specified attribute is set for at least one
842 /// parameter or for the return value.
843 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
844 if (!pImpl) return false;
846 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
847 for (AttributeSetImpl::iterator II = pImpl->begin(I),
848 IE = pImpl->end(I); II != IE; ++II)
849 if (II->hasAttribute(Attr))
855 Attribute AttributeSet::getAttribute(unsigned Index,
856 Attribute::AttrKind Kind) const {
857 AttributeSetNode *ASN = getAttributes(Index);
858 return ASN ? ASN->getAttribute(Kind) : Attribute();
861 Attribute AttributeSet::getAttribute(unsigned Index,
862 StringRef Kind) const {
863 AttributeSetNode *ASN = getAttributes(Index);
864 return ASN ? ASN->getAttribute(Kind) : Attribute();
867 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
868 AttributeSetNode *ASN = getAttributes(Index);
869 return ASN ? ASN->getAlignment() : 0;
872 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
873 AttributeSetNode *ASN = getAttributes(Index);
874 return ASN ? ASN->getStackAlignment() : 0;
877 std::string AttributeSet::getAsString(unsigned Index,
878 bool InAttrGrp) const {
879 AttributeSetNode *ASN = getAttributes(Index);
880 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
883 /// \brief The attributes for the specified index are returned.
884 AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
885 if (!pImpl) return nullptr;
887 // Loop through to find the attribute node we want.
888 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
889 if (pImpl->getSlotIndex(I) == Index)
890 return pImpl->getSlotNode(I);
895 AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
897 return ArrayRef<Attribute>().begin();
898 return pImpl->begin(Slot);
901 AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
903 return ArrayRef<Attribute>().end();
904 return pImpl->end(Slot);
907 //===----------------------------------------------------------------------===//
908 // AttributeSet Introspection Methods
909 //===----------------------------------------------------------------------===//
911 /// \brief Return the number of slots used in this attribute list. This is the
912 /// number of arguments that have an attribute set on them (including the
913 /// function itself).
914 unsigned AttributeSet::getNumSlots() const {
915 return pImpl ? pImpl->getNumAttributes() : 0;
918 unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
919 assert(pImpl && Slot < pImpl->getNumAttributes() &&
920 "Slot # out of range!");
921 return pImpl->getSlotIndex(Slot);
924 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
925 assert(pImpl && Slot < pImpl->getNumAttributes() &&
926 "Slot # out of range!");
927 return pImpl->getSlotAttributes(Slot);
930 uint64_t AttributeSet::Raw(unsigned Index) const {
931 // FIXME: Remove this.
932 return pImpl ? pImpl->Raw(Index) : 0;
935 void AttributeSet::dump() const {
938 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
939 uint64_t Index = getSlotIndex(i);
945 dbgs() << " => " << getAsString(Index) << " }\n";
951 //===----------------------------------------------------------------------===//
952 // AttrBuilder Method Implementations
953 //===----------------------------------------------------------------------===//
955 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
956 : Attrs(0), Alignment(0), StackAlignment(0) {
957 AttributeSetImpl *pImpl = AS.pImpl;
960 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
961 if (pImpl->getSlotIndex(I) != Index) continue;
963 for (AttributeSetImpl::iterator II = pImpl->begin(I),
964 IE = pImpl->end(I); II != IE; ++II)
971 void AttrBuilder::clear() {
973 Alignment = StackAlignment = 0;
976 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
977 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
978 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
979 "Adding alignment attribute without adding alignment value!");
984 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
985 if (Attr.isStringAttribute()) {
986 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
990 Attribute::AttrKind Kind = Attr.getKindAsEnum();
993 if (Kind == Attribute::Alignment)
994 Alignment = Attr.getAlignment();
995 else if (Kind == Attribute::StackAlignment)
996 StackAlignment = Attr.getStackAlignment();
1000 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1001 TargetDepAttrs[A] = V;
1005 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
1006 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1009 if (Val == Attribute::Alignment)
1011 else if (Val == Attribute::StackAlignment)
1017 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
1018 unsigned Slot = ~0U;
1019 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1020 if (A.getSlotIndex(I) == Index) {
1025 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
1027 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
1028 Attribute Attr = *I;
1029 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1030 Attribute::AttrKind Kind = I->getKindAsEnum();
1031 Attrs[Kind] = false;
1033 if (Kind == Attribute::Alignment)
1035 else if (Kind == Attribute::StackAlignment)
1038 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1039 std::map<std::string, std::string>::iterator
1040 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1041 if (Iter != TargetDepAttrs.end())
1042 TargetDepAttrs.erase(Iter);
1049 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1050 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1051 if (I != TargetDepAttrs.end())
1052 TargetDepAttrs.erase(I);
1056 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
1057 if (Align == 0) return *this;
1059 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1060 assert(Align <= 0x40000000 && "Alignment too large.");
1062 Attrs[Attribute::Alignment] = true;
1067 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1068 // Default alignment, allow the target to define how to align it.
1069 if (Align == 0) return *this;
1071 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1072 assert(Align <= 0x100 && "Alignment too large.");
1074 Attrs[Attribute::StackAlignment] = true;
1075 StackAlignment = Align;
1079 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1080 // FIXME: What if both have alignments, but they don't match?!
1082 Alignment = B.Alignment;
1084 if (!StackAlignment)
1085 StackAlignment = B.StackAlignment;
1089 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1090 E = B.TargetDepAttrs.end(); I != E; ++I)
1091 TargetDepAttrs[I->first] = I->second;
1096 bool AttrBuilder::contains(StringRef A) const {
1097 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1100 bool AttrBuilder::hasAttributes() const {
1101 return !Attrs.none() || !TargetDepAttrs.empty();
1104 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
1105 unsigned Slot = ~0U;
1106 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1107 if (A.getSlotIndex(I) == Index) {
1112 assert(Slot != ~0U && "Couldn't find the index!");
1114 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
1116 Attribute Attr = *I;
1117 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1118 if (Attrs[I->getKindAsEnum()])
1121 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1122 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1129 bool AttrBuilder::hasAlignmentAttr() const {
1130 return Alignment != 0;
1133 bool AttrBuilder::operator==(const AttrBuilder &B) {
1134 if (Attrs != B.Attrs)
1137 for (td_const_iterator I = TargetDepAttrs.begin(),
1138 E = TargetDepAttrs.end(); I != E; ++I)
1139 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1142 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
1145 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
1146 // FIXME: Remove this in 4.0.
1147 if (!Val) return *this;
1149 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1150 I = Attribute::AttrKind(I + 1)) {
1151 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1154 if (I == Attribute::Alignment)
1155 Alignment = 1ULL << ((A >> 16) - 1);
1156 else if (I == Attribute::StackAlignment)
1157 StackAlignment = 1ULL << ((A >> 26)-1);
1164 //===----------------------------------------------------------------------===//
1165 // AttributeFuncs Function Defintions
1166 //===----------------------------------------------------------------------===//
1168 /// \brief Which attributes cannot be applied to a type.
1169 AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
1170 AttrBuilder Incompatible;
1172 if (!Ty->isIntegerTy())
1173 // Attribute that only apply to integers.
1174 Incompatible.addAttribute(Attribute::SExt)
1175 .addAttribute(Attribute::ZExt);
1177 if (!Ty->isPointerTy())
1178 // Attribute that only apply to pointers.
1179 Incompatible.addAttribute(Attribute::ByVal)
1180 .addAttribute(Attribute::Nest)
1181 .addAttribute(Attribute::NoAlias)
1182 .addAttribute(Attribute::NoCapture)
1183 .addAttribute(Attribute::NonNull)
1184 .addAttribute(Attribute::ReadNone)
1185 .addAttribute(Attribute::ReadOnly)
1186 .addAttribute(Attribute::StructRet)
1187 .addAttribute(Attribute::InAlloca);
1189 return AttributeSet::get(Ty->getContext(), Index, Incompatible);