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::NoRedZone))
198 if (hasAttribute(Attribute::NoReturn))
200 if (hasAttribute(Attribute::NoUnwind))
202 if (hasAttribute(Attribute::OptimizeNone))
204 if (hasAttribute(Attribute::OptimizeForSize))
206 if (hasAttribute(Attribute::ReadNone))
208 if (hasAttribute(Attribute::ReadOnly))
210 if (hasAttribute(Attribute::Returned))
212 if (hasAttribute(Attribute::ReturnsTwice))
213 return "returns_twice";
214 if (hasAttribute(Attribute::SExt))
216 if (hasAttribute(Attribute::StackProtect))
218 if (hasAttribute(Attribute::StackProtectReq))
220 if (hasAttribute(Attribute::StackProtectStrong))
222 if (hasAttribute(Attribute::StructRet))
224 if (hasAttribute(Attribute::SanitizeThread))
225 return "sanitize_thread";
226 if (hasAttribute(Attribute::SanitizeMemory))
227 return "sanitize_memory";
228 if (hasAttribute(Attribute::UWTable))
230 if (hasAttribute(Attribute::ZExt))
232 if (hasAttribute(Attribute::Cold))
235 // FIXME: These should be output like this:
240 if (hasAttribute(Attribute::Alignment)) {
243 Result += (InAttrGrp) ? "=" : " ";
244 Result += utostr(getValueAsInt());
248 if (hasAttribute(Attribute::StackAlignment)) {
250 Result += "alignstack";
253 Result += utostr(getValueAsInt());
256 Result += utostr(getValueAsInt());
262 // Convert target-dependent attributes to strings of the form:
267 if (isStringAttribute()) {
269 Result += '\"' + getKindAsString().str() + '"';
271 StringRef Val = pImpl->getValueAsString();
272 if (Val.empty()) return Result;
274 Result += "=\"" + Val.str() + '"';
278 llvm_unreachable("Unknown attribute");
281 bool Attribute::operator<(Attribute A) const {
282 if (!pImpl && !A.pImpl) return false;
283 if (!pImpl) return true;
284 if (!A.pImpl) return false;
285 return *pImpl < *A.pImpl;
288 //===----------------------------------------------------------------------===//
289 // AttributeImpl Definition
290 //===----------------------------------------------------------------------===//
292 // Pin the vtabels to this file.
293 AttributeImpl::~AttributeImpl() {}
294 void EnumAttributeImpl::anchor() {}
295 void AlignAttributeImpl::anchor() {}
296 void StringAttributeImpl::anchor() {}
298 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
299 if (isStringAttribute()) return false;
300 return getKindAsEnum() == A;
303 bool AttributeImpl::hasAttribute(StringRef Kind) const {
304 if (!isStringAttribute()) return false;
305 return getKindAsString() == Kind;
308 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
309 assert(isEnumAttribute() || isAlignAttribute());
310 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
313 uint64_t AttributeImpl::getValueAsInt() const {
314 assert(isAlignAttribute());
315 return static_cast<const AlignAttributeImpl *>(this)->getAlignment();
318 StringRef AttributeImpl::getKindAsString() const {
319 assert(isStringAttribute());
320 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
323 StringRef AttributeImpl::getValueAsString() const {
324 assert(isStringAttribute());
325 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
328 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
329 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
330 // relative to their enum value) and then strings.
331 if (isEnumAttribute()) {
332 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
333 if (AI.isAlignAttribute()) return true;
334 if (AI.isStringAttribute()) return true;
337 if (isAlignAttribute()) {
338 if (AI.isEnumAttribute()) return false;
339 if (AI.isAlignAttribute()) return getValueAsInt() < AI.getValueAsInt();
340 if (AI.isStringAttribute()) return true;
343 if (AI.isEnumAttribute()) return false;
344 if (AI.isAlignAttribute()) return false;
345 if (getKindAsString() == AI.getKindAsString())
346 return getValueAsString() < AI.getValueAsString();
347 return getKindAsString() < AI.getKindAsString();
350 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
351 // FIXME: Remove this.
353 case Attribute::EndAttrKinds:
354 llvm_unreachable("Synthetic enumerators which should never get here");
356 case Attribute::None: return 0;
357 case Attribute::ZExt: return 1 << 0;
358 case Attribute::SExt: return 1 << 1;
359 case Attribute::NoReturn: return 1 << 2;
360 case Attribute::InReg: return 1 << 3;
361 case Attribute::StructRet: return 1 << 4;
362 case Attribute::NoUnwind: return 1 << 5;
363 case Attribute::NoAlias: return 1 << 6;
364 case Attribute::ByVal: return 1 << 7;
365 case Attribute::Nest: return 1 << 8;
366 case Attribute::ReadNone: return 1 << 9;
367 case Attribute::ReadOnly: return 1 << 10;
368 case Attribute::NoInline: return 1 << 11;
369 case Attribute::AlwaysInline: return 1 << 12;
370 case Attribute::OptimizeForSize: return 1 << 13;
371 case Attribute::StackProtect: return 1 << 14;
372 case Attribute::StackProtectReq: return 1 << 15;
373 case Attribute::Alignment: return 31 << 16;
374 case Attribute::NoCapture: return 1 << 21;
375 case Attribute::NoRedZone: return 1 << 22;
376 case Attribute::NoImplicitFloat: return 1 << 23;
377 case Attribute::Naked: return 1 << 24;
378 case Attribute::InlineHint: return 1 << 25;
379 case Attribute::StackAlignment: return 7 << 26;
380 case Attribute::ReturnsTwice: return 1 << 29;
381 case Attribute::UWTable: return 1 << 30;
382 case Attribute::NonLazyBind: return 1U << 31;
383 case Attribute::SanitizeAddress: return 1ULL << 32;
384 case Attribute::MinSize: return 1ULL << 33;
385 case Attribute::NoDuplicate: return 1ULL << 34;
386 case Attribute::StackProtectStrong: return 1ULL << 35;
387 case Attribute::SanitizeThread: return 1ULL << 36;
388 case Attribute::SanitizeMemory: return 1ULL << 37;
389 case Attribute::NoBuiltin: return 1ULL << 38;
390 case Attribute::Returned: return 1ULL << 39;
391 case Attribute::Cold: return 1ULL << 40;
392 case Attribute::Builtin: return 1ULL << 41;
393 case Attribute::OptimizeNone: return 1ULL << 42;
394 case Attribute::InAlloca: return 1ULL << 43;
396 llvm_unreachable("Unsupported attribute type");
399 //===----------------------------------------------------------------------===//
400 // AttributeSetNode Definition
401 //===----------------------------------------------------------------------===//
403 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
404 ArrayRef<Attribute> Attrs) {
408 // Otherwise, build a key to look up the existing attributes.
409 LLVMContextImpl *pImpl = C.pImpl;
412 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
413 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
415 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
416 E = SortedAttrs.end(); I != E; ++I)
420 AttributeSetNode *PA =
421 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
423 // If we didn't find any existing attributes of the same shape then create a
424 // new one and insert it.
426 // Coallocate entries after the AttributeSetNode itself.
427 void *Mem = ::operator new(sizeof(AttributeSetNode) +
428 sizeof(Attribute) * SortedAttrs.size());
429 PA = new (Mem) AttributeSetNode(SortedAttrs);
430 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
433 // Return the AttributesListNode that we found or created.
437 bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
438 for (iterator I = begin(), E = end(); I != E; ++I)
439 if (I->hasAttribute(Kind))
444 bool AttributeSetNode::hasAttribute(StringRef Kind) const {
445 for (iterator I = begin(), E = end(); I != E; ++I)
446 if (I->hasAttribute(Kind))
451 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
452 for (iterator I = begin(), E = end(); I != E; ++I)
453 if (I->hasAttribute(Kind))
458 Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
459 for (iterator I = begin(), E = end(); I != E; ++I)
460 if (I->hasAttribute(Kind))
465 unsigned AttributeSetNode::getAlignment() const {
466 for (iterator I = begin(), E = end(); I != E; ++I)
467 if (I->hasAttribute(Attribute::Alignment))
468 return I->getAlignment();
472 unsigned AttributeSetNode::getStackAlignment() const {
473 for (iterator I = begin(), E = end(); I != E; ++I)
474 if (I->hasAttribute(Attribute::StackAlignment))
475 return I->getStackAlignment();
479 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
481 for (iterator I = begin(), E = end(); I != E; ++I) {
484 Str += I->getAsString(InAttrGrp);
489 //===----------------------------------------------------------------------===//
490 // AttributeSetImpl Definition
491 //===----------------------------------------------------------------------===//
493 uint64_t AttributeSetImpl::Raw(unsigned Index) const {
494 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
495 if (getSlotIndex(I) != Index) continue;
496 const AttributeSetNode *ASN = getSlotNode(I);
499 for (AttributeSetNode::iterator II = ASN->begin(),
500 IE = ASN->end(); II != IE; ++II) {
501 Attribute Attr = *II;
503 // This cannot handle string attributes.
504 if (Attr.isStringAttribute()) continue;
506 Attribute::AttrKind Kind = Attr.getKindAsEnum();
508 if (Kind == Attribute::Alignment)
509 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
510 else if (Kind == Attribute::StackAlignment)
511 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
513 Mask |= AttributeImpl::getAttrMask(Kind);
522 void AttributeSetImpl::dump() const {
523 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
526 //===----------------------------------------------------------------------===//
527 // AttributeSet Construction and Mutation Methods
528 //===----------------------------------------------------------------------===//
531 AttributeSet::getImpl(LLVMContext &C,
532 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
533 LLVMContextImpl *pImpl = C.pImpl;
535 AttributeSetImpl::Profile(ID, Attrs);
538 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
540 // If we didn't find any existing attributes of the same shape then
541 // create a new one and insert it.
543 // Coallocate entries after the AttributeSetImpl itself.
544 void *Mem = ::operator new(sizeof(AttributeSetImpl) +
545 sizeof(std::pair<unsigned, AttributeSetNode *>) *
547 PA = new (Mem) AttributeSetImpl(C, Attrs);
548 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
551 // Return the AttributesList that we found or created.
552 return AttributeSet(PA);
555 AttributeSet AttributeSet::get(LLVMContext &C,
556 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
557 // If there are no attributes then return a null AttributesList pointer.
559 return AttributeSet();
562 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
563 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
564 "Misordered Attributes list!");
565 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
566 "Pointless attribute!");
570 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
572 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
573 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
574 E = Attrs.end(); I != E; ) {
575 unsigned Index = I->first;
576 SmallVector<Attribute, 4> AttrVec;
577 while (I != E && I->first == Index) {
578 AttrVec.push_back(I->second);
582 AttrPairVec.push_back(std::make_pair(Index,
583 AttributeSetNode::get(C, AttrVec)));
586 return getImpl(C, AttrPairVec);
589 AttributeSet AttributeSet::get(LLVMContext &C,
590 ArrayRef<std::pair<unsigned,
591 AttributeSetNode*> > Attrs) {
592 // If there are no attributes then return a null AttributesList pointer.
594 return AttributeSet();
596 return getImpl(C, Attrs);
599 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
600 const AttrBuilder &B) {
601 if (!B.hasAttributes())
602 return AttributeSet();
604 // Add target-independent attributes.
605 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
606 for (Attribute::AttrKind Kind = Attribute::None;
607 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
608 if (!B.contains(Kind))
611 if (Kind == Attribute::Alignment)
612 Attrs.push_back(std::make_pair(Index, Attribute::
613 getWithAlignment(C, B.getAlignment())));
614 else if (Kind == Attribute::StackAlignment)
615 Attrs.push_back(std::make_pair(Index, Attribute::
616 getWithStackAlignment(C, B.getStackAlignment())));
618 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
621 // Add target-dependent (string) attributes.
622 for (const AttrBuilder::td_type &TDA : B.td_attrs())
624 std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
626 return get(C, Attrs);
629 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
630 ArrayRef<Attribute::AttrKind> Kind) {
631 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
632 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
633 E = Kind.end(); I != E; ++I)
634 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
635 return get(C, Attrs);
638 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
639 if (Attrs.empty()) return AttributeSet();
640 if (Attrs.size() == 1) return Attrs[0];
642 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
643 AttributeSetImpl *A0 = Attrs[0].pImpl;
645 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
646 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
647 // ordered by index. Because we know that each list in Attrs is ordered by
648 // index we only need to merge each successive list in rather than doing a
650 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
651 AttributeSetImpl *AS = Attrs[I].pImpl;
653 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
654 ANVI = AttrNodeVec.begin(), ANVE;
655 for (const AttributeSetImpl::IndexAttrPair
656 *AI = AS->getNode(0),
657 *AE = AS->getNode(AS->getNumAttributes());
659 ANVE = AttrNodeVec.end();
660 while (ANVI != ANVE && ANVI->first <= AI->first)
662 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
666 return getImpl(C, AttrNodeVec);
669 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
670 Attribute::AttrKind Attr) const {
671 if (hasAttribute(Index, Attr)) return *this;
672 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
675 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
676 StringRef Kind) const {
678 B.addAttribute(Kind);
679 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
682 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
683 StringRef Kind, StringRef Value) const {
685 B.addAttribute(Kind, Value);
686 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
689 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
690 AttributeSet Attrs) const {
691 if (!pImpl) return Attrs;
692 if (!Attrs.pImpl) return *this;
695 // FIXME it is not obvious how this should work for alignment. For now, say
696 // we can't change a known alignment.
697 unsigned OldAlign = getParamAlignment(Index);
698 unsigned NewAlign = Attrs.getParamAlignment(Index);
699 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
700 "Attempt to change alignment!");
703 // Add the attribute slots before the one we're trying to add.
704 SmallVector<AttributeSet, 4> AttrSet;
705 uint64_t NumAttrs = pImpl->getNumAttributes();
707 uint64_t LastIndex = 0;
708 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
709 if (getSlotIndex(I) >= Index) {
710 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
714 AttrSet.push_back(getSlotAttributes(I));
717 // Now add the attribute into the correct slot. There may already be an
718 // AttributeSet there.
719 AttrBuilder B(AS, Index);
721 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
722 if (Attrs.getSlotIndex(I) == Index) {
723 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
724 IE = Attrs.pImpl->end(I); II != IE; ++II)
729 AttrSet.push_back(AttributeSet::get(C, Index, B));
731 // Add the remaining attribute slots.
732 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
733 AttrSet.push_back(getSlotAttributes(I));
735 return get(C, AttrSet);
738 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
739 Attribute::AttrKind Attr) const {
740 if (!hasAttribute(Index, Attr)) return *this;
741 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
744 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
745 AttributeSet Attrs) const {
746 if (!pImpl) return AttributeSet();
747 if (!Attrs.pImpl) return *this;
750 // FIXME it is not obvious how this should work for alignment.
751 // For now, say we can't pass in alignment, which no current use does.
752 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
753 "Attempt to change alignment!");
756 // Add the attribute slots before the one we're trying to add.
757 SmallVector<AttributeSet, 4> AttrSet;
758 uint64_t NumAttrs = pImpl->getNumAttributes();
760 uint64_t LastIndex = 0;
761 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
762 if (getSlotIndex(I) >= Index) {
763 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
767 AttrSet.push_back(getSlotAttributes(I));
770 // Now remove the attribute from the correct slot. There may already be an
771 // AttributeSet there.
772 AttrBuilder B(AS, Index);
774 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
775 if (Attrs.getSlotIndex(I) == Index) {
776 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
780 AttrSet.push_back(AttributeSet::get(C, Index, B));
782 // Add the remaining attribute slots.
783 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
784 AttrSet.push_back(getSlotAttributes(I));
786 return get(C, AttrSet);
789 //===----------------------------------------------------------------------===//
790 // AttributeSet Accessor Methods
791 //===----------------------------------------------------------------------===//
793 LLVMContext &AttributeSet::getContext() const {
794 return pImpl->getContext();
797 AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
798 return pImpl && hasAttributes(Index) ?
799 AttributeSet::get(pImpl->getContext(),
800 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
801 std::make_pair(Index, getAttributes(Index)))) :
805 AttributeSet AttributeSet::getRetAttributes() const {
806 return pImpl && hasAttributes(ReturnIndex) ?
807 AttributeSet::get(pImpl->getContext(),
808 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
809 std::make_pair(ReturnIndex,
810 getAttributes(ReturnIndex)))) :
814 AttributeSet AttributeSet::getFnAttributes() const {
815 return pImpl && hasAttributes(FunctionIndex) ?
816 AttributeSet::get(pImpl->getContext(),
817 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
818 std::make_pair(FunctionIndex,
819 getAttributes(FunctionIndex)))) :
823 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
824 AttributeSetNode *ASN = getAttributes(Index);
825 return ASN ? ASN->hasAttribute(Kind) : false;
828 bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
829 AttributeSetNode *ASN = getAttributes(Index);
830 return ASN ? ASN->hasAttribute(Kind) : false;
833 bool AttributeSet::hasAttributes(unsigned Index) const {
834 AttributeSetNode *ASN = getAttributes(Index);
835 return ASN ? ASN->hasAttributes() : false;
838 /// \brief Return true if the specified attribute is set for at least one
839 /// parameter or for the return value.
840 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
841 if (!pImpl) return false;
843 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
844 for (AttributeSetImpl::iterator II = pImpl->begin(I),
845 IE = pImpl->end(I); II != IE; ++II)
846 if (II->hasAttribute(Attr))
852 Attribute AttributeSet::getAttribute(unsigned Index,
853 Attribute::AttrKind Kind) const {
854 AttributeSetNode *ASN = getAttributes(Index);
855 return ASN ? ASN->getAttribute(Kind) : Attribute();
858 Attribute AttributeSet::getAttribute(unsigned Index,
859 StringRef Kind) const {
860 AttributeSetNode *ASN = getAttributes(Index);
861 return ASN ? ASN->getAttribute(Kind) : Attribute();
864 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
865 AttributeSetNode *ASN = getAttributes(Index);
866 return ASN ? ASN->getAlignment() : 0;
869 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
870 AttributeSetNode *ASN = getAttributes(Index);
871 return ASN ? ASN->getStackAlignment() : 0;
874 std::string AttributeSet::getAsString(unsigned Index,
875 bool InAttrGrp) const {
876 AttributeSetNode *ASN = getAttributes(Index);
877 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
880 /// \brief The attributes for the specified index are returned.
881 AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
882 if (!pImpl) return nullptr;
884 // Loop through to find the attribute node we want.
885 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
886 if (pImpl->getSlotIndex(I) == Index)
887 return pImpl->getSlotNode(I);
892 AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
894 return ArrayRef<Attribute>().begin();
895 return pImpl->begin(Slot);
898 AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
900 return ArrayRef<Attribute>().end();
901 return pImpl->end(Slot);
904 //===----------------------------------------------------------------------===//
905 // AttributeSet Introspection Methods
906 //===----------------------------------------------------------------------===//
908 /// \brief Return the number of slots used in this attribute list. This is the
909 /// number of arguments that have an attribute set on them (including the
910 /// function itself).
911 unsigned AttributeSet::getNumSlots() const {
912 return pImpl ? pImpl->getNumAttributes() : 0;
915 unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
916 assert(pImpl && Slot < pImpl->getNumAttributes() &&
917 "Slot # out of range!");
918 return pImpl->getSlotIndex(Slot);
921 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
922 assert(pImpl && Slot < pImpl->getNumAttributes() &&
923 "Slot # out of range!");
924 return pImpl->getSlotAttributes(Slot);
927 uint64_t AttributeSet::Raw(unsigned Index) const {
928 // FIXME: Remove this.
929 return pImpl ? pImpl->Raw(Index) : 0;
932 void AttributeSet::dump() const {
935 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
936 uint64_t Index = getSlotIndex(i);
942 dbgs() << " => " << getAsString(Index) << " }\n";
948 //===----------------------------------------------------------------------===//
949 // AttrBuilder Method Implementations
950 //===----------------------------------------------------------------------===//
952 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
953 : Attrs(0), Alignment(0), StackAlignment(0) {
954 AttributeSetImpl *pImpl = AS.pImpl;
957 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
958 if (pImpl->getSlotIndex(I) != Index) continue;
960 for (AttributeSetImpl::iterator II = pImpl->begin(I),
961 IE = pImpl->end(I); II != IE; ++II)
968 void AttrBuilder::clear() {
970 Alignment = StackAlignment = 0;
973 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
974 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
975 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
976 "Adding alignment attribute without adding alignment value!");
981 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
982 if (Attr.isStringAttribute()) {
983 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
987 Attribute::AttrKind Kind = Attr.getKindAsEnum();
990 if (Kind == Attribute::Alignment)
991 Alignment = Attr.getAlignment();
992 else if (Kind == Attribute::StackAlignment)
993 StackAlignment = Attr.getStackAlignment();
997 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
998 TargetDepAttrs[A] = V;
1002 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
1003 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1006 if (Val == Attribute::Alignment)
1008 else if (Val == Attribute::StackAlignment)
1014 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
1015 unsigned Slot = ~0U;
1016 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1017 if (A.getSlotIndex(I) == Index) {
1022 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
1024 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
1025 Attribute Attr = *I;
1026 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1027 Attribute::AttrKind Kind = I->getKindAsEnum();
1028 Attrs[Kind] = false;
1030 if (Kind == Attribute::Alignment)
1032 else if (Kind == Attribute::StackAlignment)
1035 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1036 std::map<std::string, std::string>::iterator
1037 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1038 if (Iter != TargetDepAttrs.end())
1039 TargetDepAttrs.erase(Iter);
1046 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1047 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1048 if (I != TargetDepAttrs.end())
1049 TargetDepAttrs.erase(I);
1053 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
1054 if (Align == 0) return *this;
1056 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1057 assert(Align <= 0x40000000 && "Alignment too large.");
1059 Attrs[Attribute::Alignment] = true;
1064 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1065 // Default alignment, allow the target to define how to align it.
1066 if (Align == 0) return *this;
1068 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1069 assert(Align <= 0x100 && "Alignment too large.");
1071 Attrs[Attribute::StackAlignment] = true;
1072 StackAlignment = Align;
1076 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1077 // FIXME: What if both have alignments, but they don't match?!
1079 Alignment = B.Alignment;
1081 if (!StackAlignment)
1082 StackAlignment = B.StackAlignment;
1086 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1087 E = B.TargetDepAttrs.end(); I != E; ++I)
1088 TargetDepAttrs[I->first] = I->second;
1093 bool AttrBuilder::contains(StringRef A) const {
1094 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1097 bool AttrBuilder::hasAttributes() const {
1098 return !Attrs.none() || !TargetDepAttrs.empty();
1101 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
1102 unsigned Slot = ~0U;
1103 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1104 if (A.getSlotIndex(I) == Index) {
1109 assert(Slot != ~0U && "Couldn't find the index!");
1111 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
1113 Attribute Attr = *I;
1114 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1115 if (Attrs[I->getKindAsEnum()])
1118 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1119 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1126 bool AttrBuilder::hasAlignmentAttr() const {
1127 return Alignment != 0;
1130 bool AttrBuilder::operator==(const AttrBuilder &B) {
1131 if (Attrs != B.Attrs)
1134 for (td_const_iterator I = TargetDepAttrs.begin(),
1135 E = TargetDepAttrs.end(); I != E; ++I)
1136 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1139 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
1142 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
1143 // FIXME: Remove this in 4.0.
1144 if (!Val) return *this;
1146 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1147 I = Attribute::AttrKind(I + 1)) {
1148 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1151 if (I == Attribute::Alignment)
1152 Alignment = 1ULL << ((A >> 16) - 1);
1153 else if (I == Attribute::StackAlignment)
1154 StackAlignment = 1ULL << ((A >> 26)-1);
1161 //===----------------------------------------------------------------------===//
1162 // AttributeFuncs Function Defintions
1163 //===----------------------------------------------------------------------===//
1165 /// \brief Which attributes cannot be applied to a type.
1166 AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
1167 AttrBuilder Incompatible;
1169 if (!Ty->isIntegerTy())
1170 // Attribute that only apply to integers.
1171 Incompatible.addAttribute(Attribute::SExt)
1172 .addAttribute(Attribute::ZExt);
1174 if (!Ty->isPointerTy())
1175 // Attribute that only apply to pointers.
1176 Incompatible.addAttribute(Attribute::ByVal)
1177 .addAttribute(Attribute::Nest)
1178 .addAttribute(Attribute::NoAlias)
1179 .addAttribute(Attribute::NoCapture)
1180 .addAttribute(Attribute::ReadNone)
1181 .addAttribute(Attribute::ReadOnly)
1182 .addAttribute(Attribute::StructRet)
1183 .addAttribute(Attribute::InAlloca);
1185 return AttributeSet::get(Ty->getContext(), Index, Incompatible);