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::JumpTable))
178 if (hasAttribute(Attribute::MinSize))
180 if (hasAttribute(Attribute::Naked))
182 if (hasAttribute(Attribute::Nest))
184 if (hasAttribute(Attribute::NoAlias))
186 if (hasAttribute(Attribute::NoBuiltin))
188 if (hasAttribute(Attribute::NoCapture))
190 if (hasAttribute(Attribute::NoDuplicate))
191 return "noduplicate";
192 if (hasAttribute(Attribute::NoImplicitFloat))
193 return "noimplicitfloat";
194 if (hasAttribute(Attribute::NoInline))
196 if (hasAttribute(Attribute::NonLazyBind))
197 return "nonlazybind";
198 if (hasAttribute(Attribute::NonNull))
200 if (hasAttribute(Attribute::NoRedZone))
202 if (hasAttribute(Attribute::NoReturn))
204 if (hasAttribute(Attribute::NoUnwind))
206 if (hasAttribute(Attribute::OptimizeNone))
208 if (hasAttribute(Attribute::OptimizeForSize))
210 if (hasAttribute(Attribute::ReadNone))
212 if (hasAttribute(Attribute::ReadOnly))
214 if (hasAttribute(Attribute::Returned))
216 if (hasAttribute(Attribute::ReturnsTwice))
217 return "returns_twice";
218 if (hasAttribute(Attribute::SExt))
220 if (hasAttribute(Attribute::StackProtect))
222 if (hasAttribute(Attribute::StackProtectReq))
224 if (hasAttribute(Attribute::StackProtectStrong))
226 if (hasAttribute(Attribute::StructRet))
228 if (hasAttribute(Attribute::SanitizeThread))
229 return "sanitize_thread";
230 if (hasAttribute(Attribute::SanitizeMemory))
231 return "sanitize_memory";
232 if (hasAttribute(Attribute::UWTable))
234 if (hasAttribute(Attribute::ZExt))
236 if (hasAttribute(Attribute::Cold))
239 // FIXME: These should be output like this:
244 if (hasAttribute(Attribute::Alignment)) {
247 Result += (InAttrGrp) ? "=" : " ";
248 Result += utostr(getValueAsInt());
252 if (hasAttribute(Attribute::StackAlignment)) {
254 Result += "alignstack";
257 Result += utostr(getValueAsInt());
260 Result += utostr(getValueAsInt());
266 // Convert target-dependent attributes to strings of the form:
271 if (isStringAttribute()) {
273 Result += '\"' + getKindAsString().str() + '"';
275 StringRef Val = pImpl->getValueAsString();
276 if (Val.empty()) return Result;
278 Result += "=\"" + Val.str() + '"';
282 llvm_unreachable("Unknown attribute");
285 bool Attribute::operator<(Attribute A) const {
286 if (!pImpl && !A.pImpl) return false;
287 if (!pImpl) return true;
288 if (!A.pImpl) return false;
289 return *pImpl < *A.pImpl;
292 //===----------------------------------------------------------------------===//
293 // AttributeImpl Definition
294 //===----------------------------------------------------------------------===//
296 // Pin the vtabels to this file.
297 AttributeImpl::~AttributeImpl() {}
298 void EnumAttributeImpl::anchor() {}
299 void AlignAttributeImpl::anchor() {}
300 void StringAttributeImpl::anchor() {}
302 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
303 if (isStringAttribute()) return false;
304 return getKindAsEnum() == A;
307 bool AttributeImpl::hasAttribute(StringRef Kind) const {
308 if (!isStringAttribute()) return false;
309 return getKindAsString() == Kind;
312 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
313 assert(isEnumAttribute() || isAlignAttribute());
314 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
317 uint64_t AttributeImpl::getValueAsInt() const {
318 assert(isAlignAttribute());
319 return static_cast<const AlignAttributeImpl *>(this)->getAlignment();
322 StringRef AttributeImpl::getKindAsString() const {
323 assert(isStringAttribute());
324 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
327 StringRef AttributeImpl::getValueAsString() const {
328 assert(isStringAttribute());
329 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
332 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
333 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
334 // relative to their enum value) and then strings.
335 if (isEnumAttribute()) {
336 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
337 if (AI.isAlignAttribute()) return true;
338 if (AI.isStringAttribute()) return true;
341 if (isAlignAttribute()) {
342 if (AI.isEnumAttribute()) return false;
343 if (AI.isAlignAttribute()) return getValueAsInt() < AI.getValueAsInt();
344 if (AI.isStringAttribute()) return true;
347 if (AI.isEnumAttribute()) return false;
348 if (AI.isAlignAttribute()) return false;
349 if (getKindAsString() == AI.getKindAsString())
350 return getValueAsString() < AI.getValueAsString();
351 return getKindAsString() < AI.getKindAsString();
354 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
355 // FIXME: Remove this.
357 case Attribute::EndAttrKinds:
358 llvm_unreachable("Synthetic enumerators which should never get here");
360 case Attribute::None: return 0;
361 case Attribute::ZExt: return 1 << 0;
362 case Attribute::SExt: return 1 << 1;
363 case Attribute::NoReturn: return 1 << 2;
364 case Attribute::InReg: return 1 << 3;
365 case Attribute::StructRet: return 1 << 4;
366 case Attribute::NoUnwind: return 1 << 5;
367 case Attribute::NoAlias: return 1 << 6;
368 case Attribute::ByVal: return 1 << 7;
369 case Attribute::Nest: return 1 << 8;
370 case Attribute::ReadNone: return 1 << 9;
371 case Attribute::ReadOnly: return 1 << 10;
372 case Attribute::NoInline: return 1 << 11;
373 case Attribute::AlwaysInline: return 1 << 12;
374 case Attribute::OptimizeForSize: return 1 << 13;
375 case Attribute::StackProtect: return 1 << 14;
376 case Attribute::StackProtectReq: return 1 << 15;
377 case Attribute::Alignment: return 31 << 16;
378 case Attribute::NoCapture: return 1 << 21;
379 case Attribute::NoRedZone: return 1 << 22;
380 case Attribute::NoImplicitFloat: return 1 << 23;
381 case Attribute::Naked: return 1 << 24;
382 case Attribute::InlineHint: return 1 << 25;
383 case Attribute::StackAlignment: return 7 << 26;
384 case Attribute::ReturnsTwice: return 1 << 29;
385 case Attribute::UWTable: return 1 << 30;
386 case Attribute::NonLazyBind: return 1U << 31;
387 case Attribute::SanitizeAddress: return 1ULL << 32;
388 case Attribute::MinSize: return 1ULL << 33;
389 case Attribute::NoDuplicate: return 1ULL << 34;
390 case Attribute::StackProtectStrong: return 1ULL << 35;
391 case Attribute::SanitizeThread: return 1ULL << 36;
392 case Attribute::SanitizeMemory: return 1ULL << 37;
393 case Attribute::NoBuiltin: return 1ULL << 38;
394 case Attribute::Returned: return 1ULL << 39;
395 case Attribute::Cold: return 1ULL << 40;
396 case Attribute::Builtin: return 1ULL << 41;
397 case Attribute::OptimizeNone: return 1ULL << 42;
398 case Attribute::InAlloca: return 1ULL << 43;
399 case Attribute::NonNull: return 1ULL << 44;
400 case Attribute::JumpTable: return 1ULL << 45;
402 llvm_unreachable("Unsupported attribute type");
405 //===----------------------------------------------------------------------===//
406 // AttributeSetNode Definition
407 //===----------------------------------------------------------------------===//
409 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
410 ArrayRef<Attribute> Attrs) {
414 // Otherwise, build a key to look up the existing attributes.
415 LLVMContextImpl *pImpl = C.pImpl;
418 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
419 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
421 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
422 E = SortedAttrs.end(); I != E; ++I)
426 AttributeSetNode *PA =
427 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
429 // If we didn't find any existing attributes of the same shape then create a
430 // new one and insert it.
432 // Coallocate entries after the AttributeSetNode itself.
433 void *Mem = ::operator new(sizeof(AttributeSetNode) +
434 sizeof(Attribute) * SortedAttrs.size());
435 PA = new (Mem) AttributeSetNode(SortedAttrs);
436 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
439 // Return the AttributesListNode that we found or created.
443 bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
444 for (iterator I = begin(), E = end(); I != E; ++I)
445 if (I->hasAttribute(Kind))
450 bool AttributeSetNode::hasAttribute(StringRef Kind) const {
451 for (iterator I = begin(), E = end(); I != E; ++I)
452 if (I->hasAttribute(Kind))
457 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
458 for (iterator I = begin(), E = end(); I != E; ++I)
459 if (I->hasAttribute(Kind))
464 Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
465 for (iterator I = begin(), E = end(); I != E; ++I)
466 if (I->hasAttribute(Kind))
471 unsigned AttributeSetNode::getAlignment() const {
472 for (iterator I = begin(), E = end(); I != E; ++I)
473 if (I->hasAttribute(Attribute::Alignment))
474 return I->getAlignment();
478 unsigned AttributeSetNode::getStackAlignment() const {
479 for (iterator I = begin(), E = end(); I != E; ++I)
480 if (I->hasAttribute(Attribute::StackAlignment))
481 return I->getStackAlignment();
485 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
487 for (iterator I = begin(), E = end(); I != E; ++I) {
490 Str += I->getAsString(InAttrGrp);
495 //===----------------------------------------------------------------------===//
496 // AttributeSetImpl Definition
497 //===----------------------------------------------------------------------===//
499 uint64_t AttributeSetImpl::Raw(unsigned Index) const {
500 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
501 if (getSlotIndex(I) != Index) continue;
502 const AttributeSetNode *ASN = getSlotNode(I);
505 for (AttributeSetNode::iterator II = ASN->begin(),
506 IE = ASN->end(); II != IE; ++II) {
507 Attribute Attr = *II;
509 // This cannot handle string attributes.
510 if (Attr.isStringAttribute()) continue;
512 Attribute::AttrKind Kind = Attr.getKindAsEnum();
514 if (Kind == Attribute::Alignment)
515 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
516 else if (Kind == Attribute::StackAlignment)
517 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
519 Mask |= AttributeImpl::getAttrMask(Kind);
528 void AttributeSetImpl::dump() const {
529 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
532 //===----------------------------------------------------------------------===//
533 // AttributeSet Construction and Mutation Methods
534 //===----------------------------------------------------------------------===//
537 AttributeSet::getImpl(LLVMContext &C,
538 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
539 LLVMContextImpl *pImpl = C.pImpl;
541 AttributeSetImpl::Profile(ID, Attrs);
544 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
546 // If we didn't find any existing attributes of the same shape then
547 // create a new one and insert it.
549 // Coallocate entries after the AttributeSetImpl itself.
550 void *Mem = ::operator new(sizeof(AttributeSetImpl) +
551 sizeof(std::pair<unsigned, AttributeSetNode *>) *
553 PA = new (Mem) AttributeSetImpl(C, Attrs);
554 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
557 // Return the AttributesList that we found or created.
558 return AttributeSet(PA);
561 AttributeSet AttributeSet::get(LLVMContext &C,
562 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
563 // If there are no attributes then return a null AttributesList pointer.
565 return AttributeSet();
568 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
569 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
570 "Misordered Attributes list!");
571 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
572 "Pointless attribute!");
576 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
578 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
579 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
580 E = Attrs.end(); I != E; ) {
581 unsigned Index = I->first;
582 SmallVector<Attribute, 4> AttrVec;
583 while (I != E && I->first == Index) {
584 AttrVec.push_back(I->second);
588 AttrPairVec.push_back(std::make_pair(Index,
589 AttributeSetNode::get(C, AttrVec)));
592 return getImpl(C, AttrPairVec);
595 AttributeSet AttributeSet::get(LLVMContext &C,
596 ArrayRef<std::pair<unsigned,
597 AttributeSetNode*> > Attrs) {
598 // If there are no attributes then return a null AttributesList pointer.
600 return AttributeSet();
602 return getImpl(C, Attrs);
605 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
606 const AttrBuilder &B) {
607 if (!B.hasAttributes())
608 return AttributeSet();
610 // Add target-independent attributes.
611 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
612 for (Attribute::AttrKind Kind = Attribute::None;
613 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
614 if (!B.contains(Kind))
617 if (Kind == Attribute::Alignment)
618 Attrs.push_back(std::make_pair(Index, Attribute::
619 getWithAlignment(C, B.getAlignment())));
620 else if (Kind == Attribute::StackAlignment)
621 Attrs.push_back(std::make_pair(Index, Attribute::
622 getWithStackAlignment(C, B.getStackAlignment())));
624 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
627 // Add target-dependent (string) attributes.
628 for (const AttrBuilder::td_type &TDA : B.td_attrs())
630 std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
632 return get(C, Attrs);
635 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
636 ArrayRef<Attribute::AttrKind> Kind) {
637 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
638 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
639 E = Kind.end(); I != E; ++I)
640 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
641 return get(C, Attrs);
644 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
645 if (Attrs.empty()) return AttributeSet();
646 if (Attrs.size() == 1) return Attrs[0];
648 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
649 AttributeSetImpl *A0 = Attrs[0].pImpl;
651 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
652 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
653 // ordered by index. Because we know that each list in Attrs is ordered by
654 // index we only need to merge each successive list in rather than doing a
656 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
657 AttributeSetImpl *AS = Attrs[I].pImpl;
659 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
660 ANVI = AttrNodeVec.begin(), ANVE;
661 for (const AttributeSetImpl::IndexAttrPair
662 *AI = AS->getNode(0),
663 *AE = AS->getNode(AS->getNumAttributes());
665 ANVE = AttrNodeVec.end();
666 while (ANVI != ANVE && ANVI->first <= AI->first)
668 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
672 return getImpl(C, AttrNodeVec);
675 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
676 Attribute::AttrKind Attr) const {
677 if (hasAttribute(Index, Attr)) return *this;
678 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
681 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
682 StringRef Kind) const {
684 B.addAttribute(Kind);
685 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
688 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
689 StringRef Kind, StringRef Value) const {
691 B.addAttribute(Kind, Value);
692 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
695 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
696 AttributeSet Attrs) const {
697 if (!pImpl) return Attrs;
698 if (!Attrs.pImpl) return *this;
701 // FIXME it is not obvious how this should work for alignment. For now, say
702 // we can't change a known alignment.
703 unsigned OldAlign = getParamAlignment(Index);
704 unsigned NewAlign = Attrs.getParamAlignment(Index);
705 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
706 "Attempt to change alignment!");
709 // Add the attribute slots before the one we're trying to add.
710 SmallVector<AttributeSet, 4> AttrSet;
711 uint64_t NumAttrs = pImpl->getNumAttributes();
713 uint64_t LastIndex = 0;
714 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
715 if (getSlotIndex(I) >= Index) {
716 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
720 AttrSet.push_back(getSlotAttributes(I));
723 // Now add the attribute into the correct slot. There may already be an
724 // AttributeSet there.
725 AttrBuilder B(AS, Index);
727 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
728 if (Attrs.getSlotIndex(I) == Index) {
729 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
730 IE = Attrs.pImpl->end(I); II != IE; ++II)
735 AttrSet.push_back(AttributeSet::get(C, Index, B));
737 // Add the remaining attribute slots.
738 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
739 AttrSet.push_back(getSlotAttributes(I));
741 return get(C, AttrSet);
744 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
745 Attribute::AttrKind Attr) const {
746 if (!hasAttribute(Index, Attr)) return *this;
747 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
750 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
751 AttributeSet Attrs) const {
752 if (!pImpl) return AttributeSet();
753 if (!Attrs.pImpl) return *this;
756 // FIXME it is not obvious how this should work for alignment.
757 // For now, say we can't pass in alignment, which no current use does.
758 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
759 "Attempt to change alignment!");
762 // Add the attribute slots before the one we're trying to add.
763 SmallVector<AttributeSet, 4> AttrSet;
764 uint64_t NumAttrs = pImpl->getNumAttributes();
766 uint64_t LastIndex = 0;
767 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
768 if (getSlotIndex(I) >= Index) {
769 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
773 AttrSet.push_back(getSlotAttributes(I));
776 // Now remove the attribute from the correct slot. There may already be an
777 // AttributeSet there.
778 AttrBuilder B(AS, Index);
780 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
781 if (Attrs.getSlotIndex(I) == Index) {
782 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
786 AttrSet.push_back(AttributeSet::get(C, Index, B));
788 // Add the remaining attribute slots.
789 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
790 AttrSet.push_back(getSlotAttributes(I));
792 return get(C, AttrSet);
795 //===----------------------------------------------------------------------===//
796 // AttributeSet Accessor Methods
797 //===----------------------------------------------------------------------===//
799 LLVMContext &AttributeSet::getContext() const {
800 return pImpl->getContext();
803 AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
804 return pImpl && hasAttributes(Index) ?
805 AttributeSet::get(pImpl->getContext(),
806 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
807 std::make_pair(Index, getAttributes(Index)))) :
811 AttributeSet AttributeSet::getRetAttributes() const {
812 return pImpl && hasAttributes(ReturnIndex) ?
813 AttributeSet::get(pImpl->getContext(),
814 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
815 std::make_pair(ReturnIndex,
816 getAttributes(ReturnIndex)))) :
820 AttributeSet AttributeSet::getFnAttributes() const {
821 return pImpl && hasAttributes(FunctionIndex) ?
822 AttributeSet::get(pImpl->getContext(),
823 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
824 std::make_pair(FunctionIndex,
825 getAttributes(FunctionIndex)))) :
829 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
830 AttributeSetNode *ASN = getAttributes(Index);
831 return ASN ? ASN->hasAttribute(Kind) : false;
834 bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
835 AttributeSetNode *ASN = getAttributes(Index);
836 return ASN ? ASN->hasAttribute(Kind) : false;
839 bool AttributeSet::hasAttributes(unsigned Index) const {
840 AttributeSetNode *ASN = getAttributes(Index);
841 return ASN ? ASN->hasAttributes() : false;
844 /// \brief Return true if the specified attribute is set for at least one
845 /// parameter or for the return value.
846 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
847 if (!pImpl) return false;
849 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
850 for (AttributeSetImpl::iterator II = pImpl->begin(I),
851 IE = pImpl->end(I); II != IE; ++II)
852 if (II->hasAttribute(Attr))
858 Attribute AttributeSet::getAttribute(unsigned Index,
859 Attribute::AttrKind Kind) const {
860 AttributeSetNode *ASN = getAttributes(Index);
861 return ASN ? ASN->getAttribute(Kind) : Attribute();
864 Attribute AttributeSet::getAttribute(unsigned Index,
865 StringRef Kind) const {
866 AttributeSetNode *ASN = getAttributes(Index);
867 return ASN ? ASN->getAttribute(Kind) : Attribute();
870 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
871 AttributeSetNode *ASN = getAttributes(Index);
872 return ASN ? ASN->getAlignment() : 0;
875 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
876 AttributeSetNode *ASN = getAttributes(Index);
877 return ASN ? ASN->getStackAlignment() : 0;
880 std::string AttributeSet::getAsString(unsigned Index,
881 bool InAttrGrp) const {
882 AttributeSetNode *ASN = getAttributes(Index);
883 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
886 /// \brief The attributes for the specified index are returned.
887 AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
888 if (!pImpl) return nullptr;
890 // Loop through to find the attribute node we want.
891 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
892 if (pImpl->getSlotIndex(I) == Index)
893 return pImpl->getSlotNode(I);
898 AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
900 return ArrayRef<Attribute>().begin();
901 return pImpl->begin(Slot);
904 AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
906 return ArrayRef<Attribute>().end();
907 return pImpl->end(Slot);
910 //===----------------------------------------------------------------------===//
911 // AttributeSet Introspection Methods
912 //===----------------------------------------------------------------------===//
914 /// \brief Return the number of slots used in this attribute list. This is the
915 /// number of arguments that have an attribute set on them (including the
916 /// function itself).
917 unsigned AttributeSet::getNumSlots() const {
918 return pImpl ? pImpl->getNumAttributes() : 0;
921 unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
922 assert(pImpl && Slot < pImpl->getNumAttributes() &&
923 "Slot # out of range!");
924 return pImpl->getSlotIndex(Slot);
927 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
928 assert(pImpl && Slot < pImpl->getNumAttributes() &&
929 "Slot # out of range!");
930 return pImpl->getSlotAttributes(Slot);
933 uint64_t AttributeSet::Raw(unsigned Index) const {
934 // FIXME: Remove this.
935 return pImpl ? pImpl->Raw(Index) : 0;
938 void AttributeSet::dump() const {
941 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
942 uint64_t Index = getSlotIndex(i);
948 dbgs() << " => " << getAsString(Index) << " }\n";
954 //===----------------------------------------------------------------------===//
955 // AttrBuilder Method Implementations
956 //===----------------------------------------------------------------------===//
958 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
959 : Attrs(0), Alignment(0), StackAlignment(0) {
960 AttributeSetImpl *pImpl = AS.pImpl;
963 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
964 if (pImpl->getSlotIndex(I) != Index) continue;
966 for (AttributeSetImpl::iterator II = pImpl->begin(I),
967 IE = pImpl->end(I); II != IE; ++II)
974 void AttrBuilder::clear() {
976 Alignment = StackAlignment = 0;
979 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
980 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
981 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
982 "Adding alignment attribute without adding alignment value!");
987 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
988 if (Attr.isStringAttribute()) {
989 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
993 Attribute::AttrKind Kind = Attr.getKindAsEnum();
996 if (Kind == Attribute::Alignment)
997 Alignment = Attr.getAlignment();
998 else if (Kind == Attribute::StackAlignment)
999 StackAlignment = Attr.getStackAlignment();
1003 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1004 TargetDepAttrs[A] = V;
1008 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
1009 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1012 if (Val == Attribute::Alignment)
1014 else if (Val == Attribute::StackAlignment)
1020 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
1021 unsigned Slot = ~0U;
1022 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1023 if (A.getSlotIndex(I) == Index) {
1028 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
1030 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
1031 Attribute Attr = *I;
1032 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1033 Attribute::AttrKind Kind = I->getKindAsEnum();
1034 Attrs[Kind] = false;
1036 if (Kind == Attribute::Alignment)
1038 else if (Kind == Attribute::StackAlignment)
1041 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1042 std::map<std::string, std::string>::iterator
1043 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1044 if (Iter != TargetDepAttrs.end())
1045 TargetDepAttrs.erase(Iter);
1052 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1053 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1054 if (I != TargetDepAttrs.end())
1055 TargetDepAttrs.erase(I);
1059 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
1060 if (Align == 0) return *this;
1062 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1063 assert(Align <= 0x40000000 && "Alignment too large.");
1065 Attrs[Attribute::Alignment] = true;
1070 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1071 // Default alignment, allow the target to define how to align it.
1072 if (Align == 0) return *this;
1074 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1075 assert(Align <= 0x100 && "Alignment too large.");
1077 Attrs[Attribute::StackAlignment] = true;
1078 StackAlignment = Align;
1082 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1083 // FIXME: What if both have alignments, but they don't match?!
1085 Alignment = B.Alignment;
1087 if (!StackAlignment)
1088 StackAlignment = B.StackAlignment;
1092 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1093 E = B.TargetDepAttrs.end(); I != E; ++I)
1094 TargetDepAttrs[I->first] = I->second;
1099 bool AttrBuilder::contains(StringRef A) const {
1100 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1103 bool AttrBuilder::hasAttributes() const {
1104 return !Attrs.none() || !TargetDepAttrs.empty();
1107 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
1108 unsigned Slot = ~0U;
1109 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1110 if (A.getSlotIndex(I) == Index) {
1115 assert(Slot != ~0U && "Couldn't find the index!");
1117 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
1119 Attribute Attr = *I;
1120 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1121 if (Attrs[I->getKindAsEnum()])
1124 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1125 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1132 bool AttrBuilder::hasAlignmentAttr() const {
1133 return Alignment != 0;
1136 bool AttrBuilder::operator==(const AttrBuilder &B) {
1137 if (Attrs != B.Attrs)
1140 for (td_const_iterator I = TargetDepAttrs.begin(),
1141 E = TargetDepAttrs.end(); I != E; ++I)
1142 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1145 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
1148 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
1149 // FIXME: Remove this in 4.0.
1150 if (!Val) return *this;
1152 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1153 I = Attribute::AttrKind(I + 1)) {
1154 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1157 if (I == Attribute::Alignment)
1158 Alignment = 1ULL << ((A >> 16) - 1);
1159 else if (I == Attribute::StackAlignment)
1160 StackAlignment = 1ULL << ((A >> 26)-1);
1167 //===----------------------------------------------------------------------===//
1168 // AttributeFuncs Function Defintions
1169 //===----------------------------------------------------------------------===//
1171 /// \brief Which attributes cannot be applied to a type.
1172 AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
1173 AttrBuilder Incompatible;
1175 if (!Ty->isIntegerTy())
1176 // Attribute that only apply to integers.
1177 Incompatible.addAttribute(Attribute::SExt)
1178 .addAttribute(Attribute::ZExt);
1180 if (!Ty->isPointerTy())
1181 // Attribute that only apply to pointers.
1182 Incompatible.addAttribute(Attribute::ByVal)
1183 .addAttribute(Attribute::Nest)
1184 .addAttribute(Attribute::NoAlias)
1185 .addAttribute(Attribute::NoCapture)
1186 .addAttribute(Attribute::NonNull)
1187 .addAttribute(Attribute::ReadNone)
1188 .addAttribute(Attribute::ReadOnly)
1189 .addAttribute(Attribute::StructRet)
1190 .addAttribute(Attribute::InAlloca);
1192 return AttributeSet::get(Ty->getContext(), Index, Incompatible);