1 //===-- Attribute.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 //===----------------------------------------------------------------------===//
10 // This file implements the Attribute, AttributeImpl, AttrBuilder,
11 // AttributeSetImpl, and AttributeSet classes.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/IR/Attributes.h"
16 #include "AttributeImpl.h"
17 #include "LLVMContextImpl.h"
18 #include "llvm/ADT/FoldingSet.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/IR/Type.h"
21 #include "llvm/Support/Atomic.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Support/Mutex.h"
25 #include "llvm/Support/raw_ostream.h"
29 //===----------------------------------------------------------------------===//
30 // Attribute Implementation
31 //===----------------------------------------------------------------------===//
33 Attribute Attribute::get(LLVMContext &Context, ArrayRef<AttrKind> Kinds) {
35 for (ArrayRef<AttrKind>::iterator I = Kinds.begin(), E = Kinds.end();
38 return Attribute::get(Context, B);
41 Attribute Attribute::get(LLVMContext &Context, AttrBuilder &B) {
42 // If there are no attributes, return an empty Attribute class.
43 if (!B.hasAttributes())
46 // Otherwise, build a key to look up the existing attributes.
47 LLVMContextImpl *pImpl = Context.pImpl;
49 ID.AddInteger(B.Raw());
52 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
55 // If we didn't find any existing attributes of the same shape then create a
56 // new one and insert it.
57 PA = new AttributeImpl(Context, B.Raw());
58 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
61 // Return the AttributesList that we found or created.
65 Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
67 return get(Context, B.addAlignmentAttr(Align));
70 Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
73 return get(Context, B.addStackAlignmentAttr(Align));
76 bool Attribute::hasAttribute(AttrKind Val) const {
77 return pImpl && pImpl->hasAttribute(Val);
80 bool Attribute::hasAttributes() const {
81 return pImpl && pImpl->hasAttributes();
84 /// This returns the alignment field of an attribute as a byte alignment value.
85 unsigned Attribute::getAlignment() const {
86 if (!hasAttribute(Attribute::Alignment))
88 return pImpl->getAlignment();
91 /// This returns the stack alignment field of an attribute as a byte alignment
93 unsigned Attribute::getStackAlignment() const {
94 if (!hasAttribute(Attribute::StackAlignment))
96 return pImpl->getStackAlignment();
99 bool Attribute::operator==(AttrKind K) const {
100 return pImpl && *pImpl == K;
102 bool Attribute::operator!=(AttrKind K) const {
103 return !(*this == K);
106 bool Attribute::operator<(Attribute A) const {
107 if (!pImpl && !A.pImpl) return false;
108 if (!pImpl) return true;
109 if (!A.pImpl) return false;
110 return *pImpl < *A.pImpl;
113 uint64_t Attribute::Raw() const {
114 return pImpl ? pImpl->Raw() : 0;
117 std::string Attribute::getAsString() const {
119 if (hasAttribute(Attribute::ZExt))
120 Result += "zeroext ";
121 if (hasAttribute(Attribute::SExt))
122 Result += "signext ";
123 if (hasAttribute(Attribute::NoReturn))
124 Result += "noreturn ";
125 if (hasAttribute(Attribute::NoUnwind))
126 Result += "nounwind ";
127 if (hasAttribute(Attribute::UWTable))
128 Result += "uwtable ";
129 if (hasAttribute(Attribute::ReturnsTwice))
130 Result += "returns_twice ";
131 if (hasAttribute(Attribute::InReg))
133 if (hasAttribute(Attribute::NoAlias))
134 Result += "noalias ";
135 if (hasAttribute(Attribute::NoCapture))
136 Result += "nocapture ";
137 if (hasAttribute(Attribute::StructRet))
139 if (hasAttribute(Attribute::ByVal))
141 if (hasAttribute(Attribute::Nest))
143 if (hasAttribute(Attribute::ReadNone))
144 Result += "readnone ";
145 if (hasAttribute(Attribute::ReadOnly))
146 Result += "readonly ";
147 if (hasAttribute(Attribute::OptimizeForSize))
148 Result += "optsize ";
149 if (hasAttribute(Attribute::NoInline))
150 Result += "noinline ";
151 if (hasAttribute(Attribute::InlineHint))
152 Result += "inlinehint ";
153 if (hasAttribute(Attribute::AlwaysInline))
154 Result += "alwaysinline ";
155 if (hasAttribute(Attribute::StackProtect))
157 if (hasAttribute(Attribute::StackProtectReq))
159 if (hasAttribute(Attribute::StackProtectStrong))
160 Result += "sspstrong ";
161 if (hasAttribute(Attribute::NoRedZone))
162 Result += "noredzone ";
163 if (hasAttribute(Attribute::NoImplicitFloat))
164 Result += "noimplicitfloat ";
165 if (hasAttribute(Attribute::Naked))
167 if (hasAttribute(Attribute::NonLazyBind))
168 Result += "nonlazybind ";
169 if (hasAttribute(Attribute::AddressSafety))
170 Result += "address_safety ";
171 if (hasAttribute(Attribute::MinSize))
172 Result += "minsize ";
173 if (hasAttribute(Attribute::StackAlignment)) {
174 Result += "alignstack(";
175 Result += utostr(getStackAlignment());
178 if (hasAttribute(Attribute::Alignment)) {
180 Result += utostr(getAlignment());
183 if (hasAttribute(Attribute::NoDuplicate))
184 Result += "noduplicate ";
185 // Trim the trailing space.
186 assert(!Result.empty() && "Unknown attribute!");
187 Result.erase(Result.end()-1);
191 //===----------------------------------------------------------------------===//
192 // AttrBuilder Method Implementations
193 //===----------------------------------------------------------------------===//
195 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
196 : Alignment(0), StackAlignment(0) {
197 AttributeSetImpl *pImpl = AS.pImpl;
202 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
203 if (pImpl->getSlotIndex(I) != Idx) continue;
205 for (AttributeSetNode::const_iterator II = pImpl->begin(I),
206 IE = pImpl->end(I); II != IE; ++II)
207 B.addAttributes(*II);
212 if (!B.hasAttributes()) return;
214 uint64_t Mask = B.Raw();
216 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
217 I = Attribute::AttrKind(I + 1)) {
218 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
221 if (I == Attribute::Alignment)
222 Alignment = 1ULL << ((A >> 16) - 1);
223 else if (I == Attribute::StackAlignment)
224 StackAlignment = 1ULL << ((A >> 26)-1);
229 void AttrBuilder::clear() {
231 Alignment = StackAlignment = 0;
234 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
239 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
241 if (Val == Attribute::Alignment)
243 else if (Val == Attribute::StackAlignment)
249 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
250 if (Align == 0) return *this;
252 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
253 assert(Align <= 0x40000000 && "Alignment too large.");
255 Attrs.insert(Attribute::Alignment);
260 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
261 // Default alignment, allow the target to define how to align it.
262 if (Align == 0) return *this;
264 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
265 assert(Align <= 0x100 && "Alignment too large.");
267 Attrs.insert(Attribute::StackAlignment);
268 StackAlignment = Align;
272 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
273 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
274 I = Attribute::AttrKind(I + 1)) {
275 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
278 if (I == Attribute::Alignment)
279 Alignment = 1ULL << ((A >> 16) - 1);
280 else if (I == Attribute::StackAlignment)
281 StackAlignment = 1ULL << ((A >> 26)-1);
288 AttrBuilder &AttrBuilder::addAttributes(const Attribute &Attr) {
289 uint64_t Mask = Attr.Raw();
291 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
292 I = Attribute::AttrKind(I + 1))
293 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
296 if (Attr.getAlignment())
297 Alignment = Attr.getAlignment();
298 if (Attr.getStackAlignment())
299 StackAlignment = Attr.getStackAlignment();
303 AttrBuilder &AttrBuilder::removeAttributes(const Attribute &A){
304 uint64_t Mask = A.Raw();
306 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
307 I = Attribute::AttrKind(I + 1)) {
308 if (Mask & AttributeImpl::getAttrMask(I)) {
311 if (I == Attribute::Alignment)
313 else if (I == Attribute::StackAlignment)
321 bool AttrBuilder::contains(Attribute::AttrKind A) const {
322 return Attrs.count(A);
325 bool AttrBuilder::hasAttributes() const {
326 return !Attrs.empty();
329 bool AttrBuilder::hasAttributes(const Attribute &A) const {
330 return Raw() & A.Raw();
333 bool AttrBuilder::hasAlignmentAttr() const {
334 return Alignment != 0;
337 uint64_t AttrBuilder::Raw() const {
340 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
341 E = Attrs.end(); I != E; ++I) {
342 Attribute::AttrKind Kind = *I;
344 if (Kind == Attribute::Alignment)
345 Mask |= (Log2_32(Alignment) + 1) << 16;
346 else if (Kind == Attribute::StackAlignment)
347 Mask |= (Log2_32(StackAlignment) + 1) << 26;
349 Mask |= AttributeImpl::getAttrMask(Kind);
355 bool AttrBuilder::operator==(const AttrBuilder &B) {
356 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
357 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
361 //===----------------------------------------------------------------------===//
362 // AttributeImpl Definition
363 //===----------------------------------------------------------------------===//
365 AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data)
367 Data = ConstantInt::get(Type::getInt64Ty(C), data);
369 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data)
371 Data = ConstantInt::get(Type::getInt64Ty(C), data);
373 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
374 ArrayRef<Constant*> values)
376 Data = ConstantInt::get(Type::getInt64Ty(C), data);
377 Vals.reserve(values.size());
378 Vals.append(values.begin(), values.end());
380 AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data)
382 Data = ConstantDataArray::getString(C, data);
385 bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
386 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
387 return CI->getZExtValue() == Kind;
390 bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
391 return !(*this == Kind);
394 bool AttributeImpl::operator==(StringRef Kind) const {
395 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
397 return CDA->getAsString() == Kind;
401 bool AttributeImpl::operator!=(StringRef Kind) const {
402 return !(*this == Kind);
405 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
406 if (!Data && !AI.Data) return false;
407 if (!Data && AI.Data) return true;
408 if (Data && !AI.Data) return false;
410 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Data);
411 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Data);
413 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Data);
414 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Data);
416 if (ThisCI && ThatCI)
417 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
419 if (ThisCI && ThatCDA)
422 if (ThisCDA && ThatCI)
425 return ThisCDA->getAsString() < ThatCDA->getAsString();
428 uint64_t AttributeImpl::Raw() const {
429 // FIXME: Remove this.
430 return cast<ConstantInt>(Data)->getZExtValue();
433 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
435 case Attribute::EndAttrKinds:
436 case Attribute::AttrKindEmptyKey:
437 case Attribute::AttrKindTombstoneKey:
438 llvm_unreachable("Synthetic enumerators which should never get here");
440 case Attribute::None: return 0;
441 case Attribute::ZExt: return 1 << 0;
442 case Attribute::SExt: return 1 << 1;
443 case Attribute::NoReturn: return 1 << 2;
444 case Attribute::InReg: return 1 << 3;
445 case Attribute::StructRet: return 1 << 4;
446 case Attribute::NoUnwind: return 1 << 5;
447 case Attribute::NoAlias: return 1 << 6;
448 case Attribute::ByVal: return 1 << 7;
449 case Attribute::Nest: return 1 << 8;
450 case Attribute::ReadNone: return 1 << 9;
451 case Attribute::ReadOnly: return 1 << 10;
452 case Attribute::NoInline: return 1 << 11;
453 case Attribute::AlwaysInline: return 1 << 12;
454 case Attribute::OptimizeForSize: return 1 << 13;
455 case Attribute::StackProtect: return 1 << 14;
456 case Attribute::StackProtectReq: return 1 << 15;
457 case Attribute::Alignment: return 31 << 16;
458 case Attribute::NoCapture: return 1 << 21;
459 case Attribute::NoRedZone: return 1 << 22;
460 case Attribute::NoImplicitFloat: return 1 << 23;
461 case Attribute::Naked: return 1 << 24;
462 case Attribute::InlineHint: return 1 << 25;
463 case Attribute::StackAlignment: return 7 << 26;
464 case Attribute::ReturnsTwice: return 1 << 29;
465 case Attribute::UWTable: return 1 << 30;
466 case Attribute::NonLazyBind: return 1U << 31;
467 case Attribute::AddressSafety: return 1ULL << 32;
468 case Attribute::MinSize: return 1ULL << 33;
469 case Attribute::NoDuplicate: return 1ULL << 34;
470 case Attribute::StackProtectStrong: return 1ULL << 35;
472 llvm_unreachable("Unsupported attribute type");
475 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
476 return (Raw() & getAttrMask(A)) != 0;
479 bool AttributeImpl::hasAttributes() const {
483 uint64_t AttributeImpl::getAlignment() const {
484 uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment);
485 return 1ULL << ((Mask >> 16) - 1);
488 uint64_t AttributeImpl::getStackAlignment() const {
489 uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment);
490 return 1ULL << ((Mask >> 26) - 1);
493 void AttributeImpl::Profile(FoldingSetNodeID &ID, Constant *Data,
494 ArrayRef<Constant*> Vals) {
495 ID.AddInteger(cast<ConstantInt>(Data)->getZExtValue());
497 // FIXME: Not yet supported.
498 for (ArrayRef<Constant*>::iterator I = Vals.begin(), E = Vals.end();
504 //===----------------------------------------------------------------------===//
505 // AttributeSetNode Definition
506 //===----------------------------------------------------------------------===//
508 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
509 ArrayRef<Attribute> Attrs) {
513 // Otherwise, build a key to look up the existing attributes.
514 LLVMContextImpl *pImpl = C.pImpl;
517 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
518 std::sort(SortedAttrs.begin(), SortedAttrs.end());
520 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
521 E = SortedAttrs.end(); I != E; ++I)
525 AttributeSetNode *PA =
526 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
528 // If we didn't find any existing attributes of the same shape then create a
529 // new one and insert it.
531 PA = new AttributeSetNode(SortedAttrs);
532 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
535 // Return the AttributesListNode that we found or created.
539 //===----------------------------------------------------------------------===//
540 // AttributeSetImpl Definition
541 //===----------------------------------------------------------------------===//
544 AttributeSetImpl(LLVMContext &C,
545 ArrayRef<AttributeWithIndex> attrs)
546 : Context(C), AttrList(attrs.begin(), attrs.end()) {
547 for (unsigned I = 0, E = attrs.size(); I != E; ++I) {
548 const AttributeWithIndex &AWI = attrs[I];
549 uint64_t Mask = AWI.Attrs.Raw();
550 SmallVector<Attribute, 8> Attrs;
552 for (Attribute::AttrKind II = Attribute::None;
553 II != Attribute::EndAttrKinds; II = Attribute::AttrKind(II + 1)) {
554 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(II))) {
557 if (II == Attribute::Alignment)
558 B.addAlignmentAttr(1ULL << ((A >> 16) - 1));
559 else if (II == Attribute::StackAlignment)
560 B.addStackAlignmentAttr(1ULL << ((A >> 26) - 1));
564 Attrs.push_back(Attribute::get(C, B));
568 AttrNodes.push_back(std::make_pair(AWI.Index,
569 AttributeSetNode::get(C, Attrs)));
572 assert(AttrNodes.size() == AttrList.size() &&
573 "Number of attributes is different between lists!");
575 for (unsigned I = 0, E = AttrNodes.size(); I != E; ++I)
576 assert((I == 0 || AttrNodes[I - 1].first < AttrNodes[I].first) &&
577 "Attributes not in ascending order!");
581 uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
582 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
583 if (getSlotIndex(I) != Index) continue;
584 const AttributeSetNode *ASN = AttrNodes[I].second;
587 for (AttributeSetNode::const_iterator II = ASN->begin(),
588 IE = ASN->end(); II != IE; ++II)
589 B.addAttributes(*II);
591 assert(B.Raw() == AttrList[I].Attrs.Raw() &&
592 "Attributes aren't the same!");
599 //===----------------------------------------------------------------------===//
600 // AttributeSet Method Implementations
601 //===----------------------------------------------------------------------===//
603 AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
605 return pImpl && hasAttributes(Idx) ?
606 AttributeSet::get(pImpl->getContext(),
607 AttributeWithIndex::get(Idx, getAttributes(Idx))) :
611 AttributeSet AttributeSet::getRetAttributes() const {
613 return pImpl && hasAttributes(ReturnIndex) ?
614 AttributeSet::get(pImpl->getContext(),
615 AttributeWithIndex::get(ReturnIndex,
616 getAttributes(ReturnIndex))) :
620 AttributeSet AttributeSet::getFnAttributes() const {
622 return pImpl && hasAttributes(FunctionIndex) ?
623 AttributeSet::get(pImpl->getContext(),
624 AttributeWithIndex::get(FunctionIndex,
625 getAttributes(FunctionIndex))) :
629 AttributeSet AttributeSet::get(LLVMContext &C,
630 ArrayRef<AttributeWithIndex> Attrs) {
631 // If there are no attributes then return a null AttributesList pointer.
633 return AttributeSet();
636 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
637 assert(Attrs[i].Attrs.hasAttributes() &&
638 "Pointless attribute!");
639 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
640 "Misordered AttributesList!");
644 // Otherwise, build a key to look up the existing attributes.
645 LLVMContextImpl *pImpl = C.pImpl;
647 AttributeSetImpl::Profile(ID, Attrs);
650 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
652 // If we didn't find any existing attributes of the same shape then
653 // create a new one and insert it.
655 PA = new AttributeSetImpl(C, Attrs);
656 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
659 // Return the AttributesList that we found or created.
660 return AttributeSet(PA);
663 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
664 // FIXME: This should be implemented as a loop that creates the
665 // AttributeWithIndexes that then are used to create the AttributeSet.
666 if (!B.hasAttributes())
667 return AttributeSet();
668 return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, B)));
671 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
672 ArrayRef<Attribute::AttrKind> Kind) {
673 // FIXME: This is temporary. Ultimately, the AttributeWithIndex will be
674 // replaced by an object that holds multiple Attribute::AttrKinds.
676 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
677 E = Kind.end(); I != E; ++I)
679 return get(C, Idx, B);
682 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
683 SmallVector<AttributeWithIndex, 8> AttrList;
684 for (ArrayRef<AttributeSet>::iterator I = Attrs.begin(), E = Attrs.end();
686 AttributeSet AS = *I;
687 if (!AS.pImpl) continue;
688 AttrList.append(AS.pImpl->AttrList.begin(), AS.pImpl->AttrList.end());
691 return get(C, AttrList);
694 /// \brief Return the number of slots used in this attribute list. This is the
695 /// number of arguments that have an attribute set on them (including the
696 /// function itself).
697 unsigned AttributeSet::getNumSlots() const {
698 return pImpl ? pImpl->getNumAttributes() : 0;
701 uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
702 assert(pImpl && Slot < pImpl->getNumAttributes() &&
703 "Slot # out of range!");
704 return pImpl->getSlotIndex(Slot);
707 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
708 assert(pImpl && Slot < pImpl->getNumAttributes() &&
709 "Slot # out of range!");
710 return pImpl->getSlotAttributes(Slot);
713 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
714 return getAttributes(Index).hasAttribute(Kind);
717 bool AttributeSet::hasAttributes(unsigned Index) const {
718 return getAttributes(Index).hasAttributes();
721 std::string AttributeSet::getAsString(unsigned Index) const {
722 return getAttributes(Index).getAsString();
725 unsigned AttributeSet::getParamAlignment(unsigned Idx) const {
726 return getAttributes(Idx).getAlignment();
729 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
730 return getAttributes(Index).getStackAlignment();
733 uint64_t AttributeSet::Raw(unsigned Index) const {
734 // FIXME: Remove this.
735 return pImpl ? pImpl->Raw(Index) : 0;
738 /// getAttributes - The attributes for the specified index are returned.
739 Attribute AttributeSet::getAttributes(unsigned Idx) const {
740 if (pImpl == 0) return Attribute();
742 ArrayRef<AttributeWithIndex> Attrs = pImpl->getAttributes();
743 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
744 if (Attrs[i].Index == Idx)
745 return Attrs[i].Attrs;
750 /// hasAttrSomewhere - Return true if the specified attribute is set for at
751 /// least one parameter or for the return value.
752 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
753 if (pImpl == 0) return false;
755 ArrayRef<AttributeWithIndex> Attrs = pImpl->getAttributes();
756 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
757 if (Attrs[i].Attrs.hasAttribute(Attr))
763 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
764 Attribute::AttrKind Attr) const {
765 return addAttr(C, Idx, Attribute::get(C, Attr));
768 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
769 AttributeSet Attrs) const {
770 return addAttr(C, Idx, Attrs.getAttributes(Idx));
773 AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
774 Attribute Attrs) const {
775 Attribute OldAttrs = getAttributes(Idx);
777 // FIXME it is not obvious how this should work for alignment.
778 // For now, say we can't change a known alignment.
779 unsigned OldAlign = OldAttrs.getAlignment();
780 unsigned NewAlign = Attrs.getAlignment();
781 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
782 "Attempt to change alignment!");
785 AttrBuilder NewAttrs =
786 AttrBuilder(OldAttrs).addAttributes(Attrs);
787 if (NewAttrs == AttrBuilder(OldAttrs))
790 SmallVector<AttributeWithIndex, 8> NewAttrList;
792 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
794 ArrayRef<AttributeWithIndex> OldAttrList = pImpl->getAttributes();
795 unsigned i = 0, e = OldAttrList.size();
796 // Copy attributes for arguments before this one.
797 for (; i != e && OldAttrList[i].Index < Idx; ++i)
798 NewAttrList.push_back(OldAttrList[i]);
800 // If there are attributes already at this index, merge them in.
801 if (i != e && OldAttrList[i].Index == Idx) {
803 Attribute::get(C, AttrBuilder(Attrs).
804 addAttributes(OldAttrList[i].Attrs));
808 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
810 // Copy attributes for arguments after this one.
811 NewAttrList.insert(NewAttrList.end(),
812 OldAttrList.begin()+i, OldAttrList.end());
815 return get(C, NewAttrList);
818 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
819 Attribute::AttrKind Attr) const {
820 return removeAttr(C, Idx, Attribute::get(C, Attr));
823 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
824 AttributeSet Attrs) const {
825 return removeAttr(C, Idx, Attrs.getAttributes(Idx));
828 AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
829 Attribute Attrs) const {
831 // FIXME it is not obvious how this should work for alignment.
832 // For now, say we can't pass in alignment, which no current use does.
833 assert(!Attrs.hasAttribute(Attribute::Alignment) &&
834 "Attempt to exclude alignment!");
836 if (pImpl == 0) return AttributeSet();
838 Attribute OldAttrs = getAttributes(Idx);
839 AttrBuilder NewAttrs =
840 AttrBuilder(OldAttrs).removeAttributes(Attrs);
841 if (NewAttrs == AttrBuilder(OldAttrs))
844 SmallVector<AttributeWithIndex, 8> NewAttrList;
845 ArrayRef<AttributeWithIndex> OldAttrList = pImpl->getAttributes();
846 unsigned i = 0, e = OldAttrList.size();
848 // Copy attributes for arguments before this one.
849 for (; i != e && OldAttrList[i].Index < Idx; ++i)
850 NewAttrList.push_back(OldAttrList[i]);
852 // If there are attributes already at this index, merge them in.
853 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
854 Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
855 removeAttributes(Attrs));
857 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
858 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
860 // Copy attributes for arguments after this one.
861 NewAttrList.insert(NewAttrList.end(),
862 OldAttrList.begin()+i, OldAttrList.end());
864 return get(C, NewAttrList);
867 void AttributeSet::dump() const {
869 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
870 uint64_t Index = getSlotIndex(i);
876 dbgs() << " => " << getAsString(Index) << " }\n";
882 //===----------------------------------------------------------------------===//
883 // AttributeFuncs Function Defintions
884 //===----------------------------------------------------------------------===//
886 Attribute AttributeFuncs::typeIncompatible(Type *Ty) {
887 AttrBuilder Incompatible;
889 if (!Ty->isIntegerTy())
890 // Attribute that only apply to integers.
891 Incompatible.addAttribute(Attribute::SExt)
892 .addAttribute(Attribute::ZExt);
894 if (!Ty->isPointerTy())
895 // Attribute that only apply to pointers.
896 Incompatible.addAttribute(Attribute::ByVal)
897 .addAttribute(Attribute::Nest)
898 .addAttribute(Attribute::NoAlias)
899 .addAttribute(Attribute::NoCapture)
900 .addAttribute(Attribute::StructRet);
902 return Attribute::get(Ty->getContext(), Incompatible);
905 /// encodeLLVMAttributesForBitcode - This returns an integer containing an
906 /// encoding of all the LLVM attributes found in the given attribute bitset.
907 /// Any change to this encoding is a breaking change to bitcode compatibility.
908 uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
910 // FIXME: It doesn't make sense to store the alignment information as an
911 // expanded out value, we should store it as a log2 value. However, we can't
912 // just change that here without breaking bitcode compatibility. If this ever
913 // becomes a problem in practice, we should introduce new tag numbers in the
914 // bitcode file and have those tags use a more efficiently encoded alignment
917 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
918 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
919 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
920 if (Attrs.hasAttribute(Index, Attribute::Alignment))
921 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
922 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
926 /// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
927 /// the LLVM attributes that have been decoded from the given integer. This
928 /// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
929 Attribute AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
930 uint64_t EncodedAttrs){
931 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
932 // the bits above 31 down by 11 bits.
933 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
934 assert((!Alignment || isPowerOf2_32(Alignment)) &&
935 "Alignment must be a power of two.");
937 AttrBuilder B(EncodedAttrs & 0xffff);
939 B.addAlignmentAttr(Alignment);
940 B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
941 return Attribute::get(C, B);