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> Vals) {
35 for (ArrayRef<AttrKind>::iterator I = Vals.begin(), E = Vals.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 bool Attribute::hasAttribute(AttrKind Val) const {
66 return pImpl && pImpl->hasAttribute(Val);
69 bool Attribute::hasAttributes() const {
70 return pImpl && pImpl->hasAttributes();
73 /// This returns the alignment field of an attribute as a byte alignment value.
74 unsigned Attribute::getAlignment() const {
75 if (!hasAttribute(Attribute::Alignment))
77 return pImpl->getAlignment();
80 /// This returns the stack alignment field of an attribute as a byte alignment
82 unsigned Attribute::getStackAlignment() const {
83 if (!hasAttribute(Attribute::StackAlignment))
85 return pImpl->getStackAlignment();
88 bool Attribute::operator==(AttrKind K) const {
89 return pImpl && *pImpl == K;
91 bool Attribute::operator!=(AttrKind K) const {
95 bool Attribute::operator<(Attribute A) const {
96 if (!pImpl && !A.pImpl) return false;
97 if (!pImpl) return true;
98 if (!A.pImpl) return false;
99 return *pImpl < *A.pImpl;
103 void Attribute::Profile(FoldingSetNodeID &ID) const {
104 ID.AddPointer(pImpl);
107 uint64_t Attribute::Raw() const {
108 return pImpl ? pImpl->Raw() : 0;
111 Attribute Attribute::typeIncompatible(Type *Ty) {
112 AttrBuilder Incompatible;
114 if (!Ty->isIntegerTy())
115 // Attribute that only apply to integers.
116 Incompatible.addAttribute(Attribute::SExt)
117 .addAttribute(Attribute::ZExt);
119 if (!Ty->isPointerTy())
120 // Attribute that only apply to pointers.
121 Incompatible.addAttribute(Attribute::ByVal)
122 .addAttribute(Attribute::Nest)
123 .addAttribute(Attribute::NoAlias)
124 .addAttribute(Attribute::NoCapture)
125 .addAttribute(Attribute::StructRet);
127 return Attribute::get(Ty->getContext(), Incompatible);
130 /// encodeLLVMAttributesForBitcode - This returns an integer containing an
131 /// encoding of all the LLVM attributes found in the given attribute bitset.
132 /// Any change to this encoding is a breaking change to bitcode compatibility.
133 uint64_t Attribute::encodeLLVMAttributesForBitcode(Attribute Attrs) {
134 // FIXME: It doesn't make sense to store the alignment information as an
135 // expanded out value, we should store it as a log2 value. However, we can't
136 // just change that here without breaking bitcode compatibility. If this ever
137 // becomes a problem in practice, we should introduce new tag numbers in the
138 // bitcode file and have those tags use a more efficiently encoded alignment
141 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
142 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
143 uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
144 if (Attrs.hasAttribute(Attribute::Alignment))
145 EncodedAttrs |= Attrs.getAlignment() << 16;
146 EncodedAttrs |= (Attrs.Raw() & (0xffffULL << 21)) << 11;
150 /// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
151 /// the LLVM attributes that have been decoded from the given integer. This
152 /// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
153 Attribute Attribute::decodeLLVMAttributesForBitcode(LLVMContext &C,
154 uint64_t EncodedAttrs) {
155 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
156 // the bits above 31 down by 11 bits.
157 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
158 assert((!Alignment || isPowerOf2_32(Alignment)) &&
159 "Alignment must be a power of two.");
161 AttrBuilder B(EncodedAttrs & 0xffff);
163 B.addAlignmentAttr(Alignment);
164 B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
165 return Attribute::get(C, B);
168 std::string Attribute::getAsString() const {
170 if (hasAttribute(Attribute::ZExt))
171 Result += "zeroext ";
172 if (hasAttribute(Attribute::SExt))
173 Result += "signext ";
174 if (hasAttribute(Attribute::NoReturn))
175 Result += "noreturn ";
176 if (hasAttribute(Attribute::NoUnwind))
177 Result += "nounwind ";
178 if (hasAttribute(Attribute::UWTable))
179 Result += "uwtable ";
180 if (hasAttribute(Attribute::ReturnsTwice))
181 Result += "returns_twice ";
182 if (hasAttribute(Attribute::InReg))
184 if (hasAttribute(Attribute::NoAlias))
185 Result += "noalias ";
186 if (hasAttribute(Attribute::NoCapture))
187 Result += "nocapture ";
188 if (hasAttribute(Attribute::StructRet))
190 if (hasAttribute(Attribute::ByVal))
192 if (hasAttribute(Attribute::Nest))
194 if (hasAttribute(Attribute::ReadNone))
195 Result += "readnone ";
196 if (hasAttribute(Attribute::ReadOnly))
197 Result += "readonly ";
198 if (hasAttribute(Attribute::OptimizeForSize))
199 Result += "optsize ";
200 if (hasAttribute(Attribute::NoInline))
201 Result += "noinline ";
202 if (hasAttribute(Attribute::InlineHint))
203 Result += "inlinehint ";
204 if (hasAttribute(Attribute::AlwaysInline))
205 Result += "alwaysinline ";
206 if (hasAttribute(Attribute::StackProtect))
208 if (hasAttribute(Attribute::StackProtectReq))
210 if (hasAttribute(Attribute::StackProtectStrong))
211 Result += "sspstrong ";
212 if (hasAttribute(Attribute::NoRedZone))
213 Result += "noredzone ";
214 if (hasAttribute(Attribute::NoImplicitFloat))
215 Result += "noimplicitfloat ";
216 if (hasAttribute(Attribute::Naked))
218 if (hasAttribute(Attribute::NonLazyBind))
219 Result += "nonlazybind ";
220 if (hasAttribute(Attribute::AddressSafety))
221 Result += "address_safety ";
222 if (hasAttribute(Attribute::MinSize))
223 Result += "minsize ";
224 if (hasAttribute(Attribute::StackAlignment)) {
225 Result += "alignstack(";
226 Result += utostr(getStackAlignment());
229 if (hasAttribute(Attribute::Alignment)) {
231 Result += utostr(getAlignment());
234 if (hasAttribute(Attribute::NoDuplicate))
235 Result += "noduplicate ";
236 // Trim the trailing space.
237 assert(!Result.empty() && "Unknown attribute!");
238 Result.erase(Result.end()-1);
242 //===----------------------------------------------------------------------===//
243 // AttrBuilder Method Implementations
244 //===----------------------------------------------------------------------===//
246 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
247 : Alignment(0), StackAlignment(0) {
248 AttributeSetImpl *pImpl = AS.AttrList;
251 ArrayRef<AttributeWithIndex> AttrList = pImpl->getAttributes();
252 const AttributeWithIndex *AWI = 0;
253 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
254 if (AttrList[I].Index == Idx) {
261 uint64_t Mask = AWI->Attrs.Raw();
263 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
264 I = Attribute::AttrKind(I + 1)) {
265 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
268 if (I == Attribute::Alignment)
269 Alignment = 1ULL << ((A >> 16) - 1);
270 else if (I == Attribute::StackAlignment)
271 StackAlignment = 1ULL << ((A >> 26)-1);
276 void AttrBuilder::clear() {
278 Alignment = StackAlignment = 0;
281 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
286 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
288 if (Val == Attribute::Alignment)
290 else if (Val == Attribute::StackAlignment)
296 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
297 if (Align == 0) return *this;
299 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
300 assert(Align <= 0x40000000 && "Alignment too large.");
302 Attrs.insert(Attribute::Alignment);
307 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
308 // Default alignment, allow the target to define how to align it.
309 if (Align == 0) return *this;
311 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
312 assert(Align <= 0x100 && "Alignment too large.");
314 Attrs.insert(Attribute::StackAlignment);
315 StackAlignment = Align;
319 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
320 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
321 I = Attribute::AttrKind(I + 1)) {
322 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
325 if (I == Attribute::Alignment)
326 Alignment = 1ULL << ((A >> 16) - 1);
327 else if (I == Attribute::StackAlignment)
328 StackAlignment = 1ULL << ((A >> 26)-1);
335 AttrBuilder &AttrBuilder::addAttributes(const Attribute &Attr) {
336 uint64_t Mask = Attr.Raw();
338 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
339 I = Attribute::AttrKind(I + 1))
340 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
343 if (Attr.getAlignment())
344 Alignment = Attr.getAlignment();
345 if (Attr.getStackAlignment())
346 StackAlignment = Attr.getStackAlignment();
350 AttrBuilder &AttrBuilder::removeAttributes(const Attribute &A){
351 uint64_t Mask = A.Raw();
353 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
354 I = Attribute::AttrKind(I + 1)) {
355 if (Mask & AttributeImpl::getAttrMask(I)) {
358 if (I == Attribute::Alignment)
360 else if (I == Attribute::StackAlignment)
368 bool AttrBuilder::contains(Attribute::AttrKind A) const {
369 return Attrs.count(A);
372 bool AttrBuilder::hasAttributes() const {
373 return !Attrs.empty();
376 bool AttrBuilder::hasAttributes(const Attribute &A) const {
377 return Raw() & A.Raw();
380 bool AttrBuilder::hasAlignmentAttr() const {
381 return Alignment != 0;
384 uint64_t AttrBuilder::Raw() const {
387 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
388 E = Attrs.end(); I != E; ++I) {
389 Attribute::AttrKind Kind = *I;
391 if (Kind == Attribute::Alignment)
392 Mask |= (Log2_32(Alignment) + 1) << 16;
393 else if (Kind == Attribute::StackAlignment)
394 Mask |= (Log2_32(StackAlignment) + 1) << 26;
396 Mask |= AttributeImpl::getAttrMask(Kind);
402 bool AttrBuilder::operator==(const AttrBuilder &B) {
403 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
404 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
408 //===----------------------------------------------------------------------===//
409 // AttributeImpl Definition
410 //===----------------------------------------------------------------------===//
412 AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data)
414 Data = ConstantInt::get(Type::getInt64Ty(C), data);
416 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data)
418 Data = ConstantInt::get(Type::getInt64Ty(C), data);
420 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
421 ArrayRef<Constant*> values)
423 Data = ConstantInt::get(Type::getInt64Ty(C), data);
424 Vals.reserve(values.size());
425 Vals.append(values.begin(), values.end());
427 AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data)
429 Data = ConstantDataArray::getString(C, data);
432 bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
433 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
434 return CI->getZExtValue() == Kind;
437 bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
438 return !(*this == Kind);
441 bool AttributeImpl::operator==(StringRef Kind) const {
442 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
444 return CDA->getAsString() == Kind;
448 bool AttributeImpl::operator!=(StringRef Kind) const {
449 return !(*this == Kind);
452 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
453 if (!Data && !AI.Data) return false;
454 if (!Data && AI.Data) return true;
455 if (Data && !AI.Data) return false;
457 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Data);
458 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Data);
460 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Data);
461 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Data);
463 if (ThisCI && ThatCI)
464 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
466 if (ThisCI && ThatCDA)
469 if (ThisCDA && ThatCI)
472 return ThisCDA->getAsString() < ThatCDA->getAsString();
475 uint64_t AttributeImpl::Raw() const {
476 // FIXME: Remove this.
477 return cast<ConstantInt>(Data)->getZExtValue();
480 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
482 case Attribute::EndAttrKinds:
483 case Attribute::AttrKindEmptyKey:
484 case Attribute::AttrKindTombstoneKey:
485 llvm_unreachable("Synthetic enumerators which should never get here");
487 case Attribute::None: return 0;
488 case Attribute::ZExt: return 1 << 0;
489 case Attribute::SExt: return 1 << 1;
490 case Attribute::NoReturn: return 1 << 2;
491 case Attribute::InReg: return 1 << 3;
492 case Attribute::StructRet: return 1 << 4;
493 case Attribute::NoUnwind: return 1 << 5;
494 case Attribute::NoAlias: return 1 << 6;
495 case Attribute::ByVal: return 1 << 7;
496 case Attribute::Nest: return 1 << 8;
497 case Attribute::ReadNone: return 1 << 9;
498 case Attribute::ReadOnly: return 1 << 10;
499 case Attribute::NoInline: return 1 << 11;
500 case Attribute::AlwaysInline: return 1 << 12;
501 case Attribute::OptimizeForSize: return 1 << 13;
502 case Attribute::StackProtect: return 1 << 14;
503 case Attribute::StackProtectReq: return 1 << 15;
504 case Attribute::Alignment: return 31 << 16;
505 case Attribute::NoCapture: return 1 << 21;
506 case Attribute::NoRedZone: return 1 << 22;
507 case Attribute::NoImplicitFloat: return 1 << 23;
508 case Attribute::Naked: return 1 << 24;
509 case Attribute::InlineHint: return 1 << 25;
510 case Attribute::StackAlignment: return 7 << 26;
511 case Attribute::ReturnsTwice: return 1 << 29;
512 case Attribute::UWTable: return 1 << 30;
513 case Attribute::NonLazyBind: return 1U << 31;
514 case Attribute::AddressSafety: return 1ULL << 32;
515 case Attribute::MinSize: return 1ULL << 33;
516 case Attribute::NoDuplicate: return 1ULL << 34;
517 case Attribute::StackProtectStrong: return 1ULL << 35;
519 llvm_unreachable("Unsupported attribute type");
522 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
523 return (Raw() & getAttrMask(A)) != 0;
526 bool AttributeImpl::hasAttributes() const {
530 uint64_t AttributeImpl::getAlignment() const {
531 uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment);
532 return 1U << ((Mask >> 16) - 1);
535 uint64_t AttributeImpl::getStackAlignment() const {
536 uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment);
537 return 1U << ((Mask >> 26) - 1);
540 void AttributeImpl::Profile(FoldingSetNodeID &ID, Constant *Data,
541 ArrayRef<Constant*> Vals) {
542 ID.AddInteger(cast<ConstantInt>(Data)->getZExtValue());
544 // FIXME: Not yet supported.
545 for (ArrayRef<Constant*>::iterator I = Vals.begin(), E = Vals.end();
551 //===----------------------------------------------------------------------===//
552 // AttributeWithIndex Definition
553 //===----------------------------------------------------------------------===//
555 AttributeWithIndex AttributeWithIndex::get(LLVMContext &C, unsigned Idx,
557 // FIXME: This is temporary, but necessary for the conversion.
558 AttrBuilder B(AS, Idx);
559 return get(Idx, Attribute::get(C, B));
562 //===----------------------------------------------------------------------===//
563 // AttributeSetNode Definition
564 //===----------------------------------------------------------------------===//
566 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
567 ArrayRef<Attribute> Attrs) {
571 // Otherwise, build a key to look up the existing attributes.
572 LLVMContextImpl *pImpl = C.pImpl;
575 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
576 std::sort(SortedAttrs.begin(), SortedAttrs.end());
578 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
579 E = SortedAttrs.end(); I != E; ++I)
583 AttributeSetNode *PA =
584 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
586 // If we didn't find any existing attributes of the same shape then create a
587 // new one and insert it.
589 PA = new AttributeSetNode(SortedAttrs);
590 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
593 // Return the AttributesListNode that we found or created.
597 //===----------------------------------------------------------------------===//
598 // AttributeSetImpl Definition
599 //===----------------------------------------------------------------------===//
601 AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
603 return AttrList && hasAttributes(Idx) ?
604 AttributeSet::get(AttrList->getContext(),
605 AttributeWithIndex::get(Idx, getAttributes(Idx))) :
609 AttributeSet AttributeSet::getRetAttributes() const {
611 return AttrList && hasAttributes(ReturnIndex) ?
612 AttributeSet::get(AttrList->getContext(),
613 AttributeWithIndex::get(ReturnIndex,
614 getAttributes(ReturnIndex))) :
618 AttributeSet AttributeSet::getFnAttributes() const {
620 return AttrList && hasAttributes(FunctionIndex) ?
621 AttributeSet::get(AttrList->getContext(),
622 AttributeWithIndex::get(FunctionIndex,
623 getAttributes(FunctionIndex))) :
627 AttributeSet AttributeSet::get(LLVMContext &C,
628 ArrayRef<AttributeWithIndex> Attrs) {
629 // If there are no attributes then return a null AttributesList pointer.
631 return AttributeSet();
634 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
635 assert(Attrs[i].Attrs.hasAttributes() &&
636 "Pointless attribute!");
637 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
638 "Misordered AttributesList!");
642 // Otherwise, build a key to look up the existing attributes.
643 LLVMContextImpl *pImpl = C.pImpl;
645 AttributeSetImpl::Profile(ID, Attrs);
648 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
650 // If we didn't find any existing attributes of the same shape then
651 // create a new one and insert it.
653 PA = new AttributeSetImpl(C, Attrs);
654 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
657 // Return the AttributesList that we found or created.
658 return AttributeSet(PA);
661 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
662 // FIXME: This should be implemented as a loop that creates the
663 // AttributeWithIndexes that then are used to create the AttributeSet.
664 if (!B.hasAttributes())
665 return AttributeSet();
666 return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, B)));
669 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
670 Attribute::AttrKind Kind) {
671 return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, Kind)));
674 //===----------------------------------------------------------------------===//
675 // AttributeSet Method Implementations
676 //===----------------------------------------------------------------------===//
678 const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) {
679 AttrList = RHS.AttrList;
683 /// getNumSlots - Return the number of slots used in this attribute list.
684 /// This is the number of arguments that have an attribute set on them
685 /// (including the function itself).
686 unsigned AttributeSet::getNumSlots() const {
687 return AttrList ? AttrList->getNumAttributes() : 0;
690 /// getSlot - Return the AttributeWithIndex at the specified slot. This
691 /// holds a number plus a set of attributes.
692 const AttributeWithIndex &AttributeSet::getSlot(unsigned Slot) const {
693 assert(AttrList && Slot < AttrList->getNumAttributes() &&
694 "Slot # out of range!");
695 return AttrList->getAttributes()[Slot];
698 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
699 return getAttributes(Index).hasAttribute(Kind);
702 bool AttributeSet::hasAttributes(unsigned Index) const {
703 return getAttributes(Index).hasAttributes();
706 std::string AttributeSet::getAsString(unsigned Index) const {
707 return getAttributes(Index).getAsString();
710 unsigned AttributeSet::getParamAlignment(unsigned Idx) const {
711 return getAttributes(Idx).getAlignment();
714 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
715 return getAttributes(Index).getStackAlignment();
718 uint64_t AttributeSet::Raw(unsigned Index) const {
719 // FIXME: Remove this.
720 return getAttributes(Index).Raw();
723 /// getAttributes - The attributes for the specified index are returned.
724 Attribute AttributeSet::getAttributes(unsigned Idx) const {
725 if (AttrList == 0) return Attribute();
727 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
728 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
729 if (Attrs[i].Index == Idx)
730 return Attrs[i].Attrs;
735 /// hasAttrSomewhere - Return true if the specified attribute is set for at
736 /// least one parameter or for the return value.
737 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
738 if (AttrList == 0) return false;
740 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
741 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
742 if (Attrs[i].Attrs.hasAttribute(Attr))
748 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
749 Attribute::AttrKind Attr) const {
750 return addAttr(C, Idx, Attribute::get(C, Attr));
753 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
754 AttributeSet Attrs) const {
755 return addAttr(C, Idx, Attrs.getAttributes(Idx));
758 AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
759 Attribute Attrs) const {
760 Attribute OldAttrs = getAttributes(Idx);
762 // FIXME it is not obvious how this should work for alignment.
763 // For now, say we can't change a known alignment.
764 unsigned OldAlign = OldAttrs.getAlignment();
765 unsigned NewAlign = Attrs.getAlignment();
766 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
767 "Attempt to change alignment!");
770 AttrBuilder NewAttrs =
771 AttrBuilder(OldAttrs).addAttributes(Attrs);
772 if (NewAttrs == AttrBuilder(OldAttrs))
775 SmallVector<AttributeWithIndex, 8> NewAttrList;
777 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
779 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
780 unsigned i = 0, e = OldAttrList.size();
781 // Copy attributes for arguments before this one.
782 for (; i != e && OldAttrList[i].Index < Idx; ++i)
783 NewAttrList.push_back(OldAttrList[i]);
785 // If there are attributes already at this index, merge them in.
786 if (i != e && OldAttrList[i].Index == Idx) {
788 Attribute::get(C, AttrBuilder(Attrs).
789 addAttributes(OldAttrList[i].Attrs));
793 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
795 // Copy attributes for arguments after this one.
796 NewAttrList.insert(NewAttrList.end(),
797 OldAttrList.begin()+i, OldAttrList.end());
800 return get(C, NewAttrList);
803 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
804 Attribute::AttrKind Attr) const {
805 return removeAttr(C, Idx, Attribute::get(C, Attr));
808 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
809 AttributeSet Attrs) const {
810 return removeAttr(C, Idx, Attrs.getAttributes(Idx));
813 AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
814 Attribute Attrs) const {
816 // FIXME it is not obvious how this should work for alignment.
817 // For now, say we can't pass in alignment, which no current use does.
818 assert(!Attrs.hasAttribute(Attribute::Alignment) &&
819 "Attempt to exclude alignment!");
821 if (AttrList == 0) return AttributeSet();
823 Attribute OldAttrs = getAttributes(Idx);
824 AttrBuilder NewAttrs =
825 AttrBuilder(OldAttrs).removeAttributes(Attrs);
826 if (NewAttrs == AttrBuilder(OldAttrs))
829 SmallVector<AttributeWithIndex, 8> NewAttrList;
830 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
831 unsigned i = 0, e = OldAttrList.size();
833 // Copy attributes for arguments before this one.
834 for (; i != e && OldAttrList[i].Index < Idx; ++i)
835 NewAttrList.push_back(OldAttrList[i]);
837 // If there are attributes already at this index, merge them in.
838 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
839 Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
840 removeAttributes(Attrs));
842 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
843 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
845 // Copy attributes for arguments after this one.
846 NewAttrList.insert(NewAttrList.end(),
847 OldAttrList.begin()+i, OldAttrList.end());
849 return get(C, NewAttrList);
852 void AttributeSet::dump() const {
854 for (unsigned i = 0; i < getNumSlots(); ++i) {
855 const AttributeWithIndex &PAWI = getSlot(i);
856 dbgs() << "{ " << PAWI.Index << ", " << PAWI.Attrs.getAsString() << " } ";