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"
28 //===----------------------------------------------------------------------===//
29 // Attribute Implementation
30 //===----------------------------------------------------------------------===//
32 Attribute Attribute::get(LLVMContext &Context, ArrayRef<AttrKind> Vals) {
34 for (ArrayRef<AttrKind>::iterator I = Vals.begin(), E = Vals.end();
37 return Attribute::get(Context, B);
40 Attribute Attribute::get(LLVMContext &Context, AttrBuilder &B) {
41 // If there are no attributes, return an empty Attribute class.
42 if (!B.hasAttributes())
45 // Otherwise, build a key to look up the existing attributes.
46 LLVMContextImpl *pImpl = Context.pImpl;
48 ID.AddInteger(B.Raw());
51 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
54 // If we didn't find any existing attributes of the same shape then create a
55 // new one and insert it.
56 PA = new AttributeImpl(Context, B.Raw());
57 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
60 // Return the AttributesList that we found or created.
64 bool Attribute::hasAttribute(AttrKind Val) const {
65 return pImpl && pImpl->hasAttribute(Val);
68 bool Attribute::hasAttributes() const {
69 return pImpl && pImpl->hasAttributes();
72 /// This returns the alignment field of an attribute as a byte alignment value.
73 unsigned Attribute::getAlignment() const {
74 if (!hasAttribute(Attribute::Alignment))
76 return 1U << ((pImpl->getAlignment() >> 16) - 1);
79 /// This returns the stack alignment field of an attribute as a byte alignment
81 unsigned Attribute::getStackAlignment() const {
82 if (!hasAttribute(Attribute::StackAlignment))
84 return 1U << ((pImpl->getStackAlignment() >> 26) - 1);
87 bool Attribute::operator==(AttrKind K) const {
88 return pImpl && *pImpl == K;
90 bool Attribute::operator!=(AttrKind K) const {
94 uint64_t Attribute::Raw() const {
95 return pImpl ? pImpl->Raw() : 0;
98 Attribute Attribute::typeIncompatible(Type *Ty) {
99 AttrBuilder Incompatible;
101 if (!Ty->isIntegerTy())
102 // Attribute that only apply to integers.
103 Incompatible.addAttribute(Attribute::SExt)
104 .addAttribute(Attribute::ZExt);
106 if (!Ty->isPointerTy())
107 // Attribute that only apply to pointers.
108 Incompatible.addAttribute(Attribute::ByVal)
109 .addAttribute(Attribute::Nest)
110 .addAttribute(Attribute::NoAlias)
111 .addAttribute(Attribute::NoCapture)
112 .addAttribute(Attribute::StructRet);
114 return Attribute::get(Ty->getContext(), Incompatible);
117 /// encodeLLVMAttributesForBitcode - This returns an integer containing an
118 /// encoding of all the LLVM attributes found in the given attribute bitset.
119 /// Any change to this encoding is a breaking change to bitcode compatibility.
120 uint64_t Attribute::encodeLLVMAttributesForBitcode(Attribute Attrs) {
121 // FIXME: It doesn't make sense to store the alignment information as an
122 // expanded out value, we should store it as a log2 value. However, we can't
123 // just change that here without breaking bitcode compatibility. If this ever
124 // becomes a problem in practice, we should introduce new tag numbers in the
125 // bitcode file and have those tags use a more efficiently encoded alignment
128 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
129 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
130 uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
131 if (Attrs.hasAttribute(Attribute::Alignment))
132 EncodedAttrs |= Attrs.getAlignment() << 16;
133 EncodedAttrs |= (Attrs.Raw() & (0xffffULL << 21)) << 11;
137 /// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
138 /// the LLVM attributes that have been decoded from the given integer. This
139 /// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
140 Attribute Attribute::decodeLLVMAttributesForBitcode(LLVMContext &C,
141 uint64_t EncodedAttrs) {
142 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
143 // the bits above 31 down by 11 bits.
144 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
145 assert((!Alignment || isPowerOf2_32(Alignment)) &&
146 "Alignment must be a power of two.");
148 AttrBuilder B(EncodedAttrs & 0xffff);
150 B.addAlignmentAttr(Alignment);
151 B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
152 return Attribute::get(C, B);
155 std::string Attribute::getAsString() const {
157 if (hasAttribute(Attribute::ZExt))
158 Result += "zeroext ";
159 if (hasAttribute(Attribute::SExt))
160 Result += "signext ";
161 if (hasAttribute(Attribute::NoReturn))
162 Result += "noreturn ";
163 if (hasAttribute(Attribute::NoUnwind))
164 Result += "nounwind ";
165 if (hasAttribute(Attribute::UWTable))
166 Result += "uwtable ";
167 if (hasAttribute(Attribute::ReturnsTwice))
168 Result += "returns_twice ";
169 if (hasAttribute(Attribute::InReg))
171 if (hasAttribute(Attribute::NoAlias))
172 Result += "noalias ";
173 if (hasAttribute(Attribute::NoCapture))
174 Result += "nocapture ";
175 if (hasAttribute(Attribute::StructRet))
177 if (hasAttribute(Attribute::ByVal))
179 if (hasAttribute(Attribute::Nest))
181 if (hasAttribute(Attribute::ReadNone))
182 Result += "readnone ";
183 if (hasAttribute(Attribute::ReadOnly))
184 Result += "readonly ";
185 if (hasAttribute(Attribute::OptimizeForSize))
186 Result += "optsize ";
187 if (hasAttribute(Attribute::NoInline))
188 Result += "noinline ";
189 if (hasAttribute(Attribute::InlineHint))
190 Result += "inlinehint ";
191 if (hasAttribute(Attribute::AlwaysInline))
192 Result += "alwaysinline ";
193 if (hasAttribute(Attribute::StackProtect))
195 if (hasAttribute(Attribute::StackProtectReq))
197 if (hasAttribute(Attribute::StackProtectStrong))
198 Result += "sspstrong ";
199 if (hasAttribute(Attribute::NoRedZone))
200 Result += "noredzone ";
201 if (hasAttribute(Attribute::NoImplicitFloat))
202 Result += "noimplicitfloat ";
203 if (hasAttribute(Attribute::Naked))
205 if (hasAttribute(Attribute::NonLazyBind))
206 Result += "nonlazybind ";
207 if (hasAttribute(Attribute::AddressSafety))
208 Result += "address_safety ";
209 if (hasAttribute(Attribute::MinSize))
210 Result += "minsize ";
211 if (hasAttribute(Attribute::StackAlignment)) {
212 Result += "alignstack(";
213 Result += utostr(getStackAlignment());
216 if (hasAttribute(Attribute::Alignment)) {
218 Result += utostr(getAlignment());
221 if (hasAttribute(Attribute::NoDuplicate))
222 Result += "noduplicate ";
223 // Trim the trailing space.
224 assert(!Result.empty() && "Unknown attribute!");
225 Result.erase(Result.end()-1);
229 //===----------------------------------------------------------------------===//
230 // AttrBuilder Method Implementations
231 //===----------------------------------------------------------------------===//
233 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
234 : Alignment(0), StackAlignment(0) {
235 AttributeSetImpl *pImpl = AS.AttrList;
238 ArrayRef<AttributeWithIndex> AttrList = pImpl->getAttributes();
239 const AttributeWithIndex *AWI = 0;
240 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
241 if (AttrList[I].Index == Idx) {
248 uint64_t Mask = AWI->Attrs.Raw();
250 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
251 I = Attribute::AttrKind(I + 1)) {
252 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
255 if (I == Attribute::Alignment)
256 Alignment = 1ULL << ((A >> 16) - 1);
257 else if (I == Attribute::StackAlignment)
258 StackAlignment = 1ULL << ((A >> 26)-1);
263 void AttrBuilder::clear() {
265 Alignment = StackAlignment = 0;
268 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
273 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
275 if (Val == Attribute::Alignment)
277 else if (Val == Attribute::StackAlignment)
283 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
284 if (Align == 0) return *this;
286 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
287 assert(Align <= 0x40000000 && "Alignment too large.");
289 Attrs.insert(Attribute::Alignment);
294 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
295 // Default alignment, allow the target to define how to align it.
296 if (Align == 0) return *this;
298 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
299 assert(Align <= 0x100 && "Alignment too large.");
301 Attrs.insert(Attribute::StackAlignment);
302 StackAlignment = Align;
306 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
307 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
308 I = Attribute::AttrKind(I + 1)) {
309 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
312 if (I == Attribute::Alignment)
313 Alignment = 1ULL << ((A >> 16) - 1);
314 else if (I == Attribute::StackAlignment)
315 StackAlignment = 1ULL << ((A >> 26)-1);
322 AttrBuilder &AttrBuilder::addAttributes(const Attribute &Attr) {
323 uint64_t Mask = Attr.Raw();
325 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
326 I = Attribute::AttrKind(I + 1))
327 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
330 if (Attr.getAlignment())
331 Alignment = Attr.getAlignment();
332 if (Attr.getStackAlignment())
333 StackAlignment = Attr.getStackAlignment();
337 AttrBuilder &AttrBuilder::removeAttributes(const Attribute &A){
338 uint64_t Mask = A.Raw();
340 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
341 I = Attribute::AttrKind(I + 1)) {
342 if (Mask & AttributeImpl::getAttrMask(I)) {
345 if (I == Attribute::Alignment)
347 else if (I == Attribute::StackAlignment)
355 bool AttrBuilder::contains(Attribute::AttrKind A) const {
356 return Attrs.count(A);
359 bool AttrBuilder::hasAttributes() const {
360 return !Attrs.empty();
363 bool AttrBuilder::hasAttributes(const Attribute &A) const {
364 return Raw() & A.Raw();
367 bool AttrBuilder::hasAlignmentAttr() const {
368 return Alignment != 0;
371 uint64_t AttrBuilder::Raw() const {
374 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
375 E = Attrs.end(); I != E; ++I) {
376 Attribute::AttrKind Kind = *I;
378 if (Kind == Attribute::Alignment)
379 Mask |= (Log2_32(Alignment) + 1) << 16;
380 else if (Kind == Attribute::StackAlignment)
381 Mask |= (Log2_32(StackAlignment) + 1) << 26;
383 Mask |= AttributeImpl::getAttrMask(Kind);
389 bool AttrBuilder::operator==(const AttrBuilder &B) {
390 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
391 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
395 //===----------------------------------------------------------------------===//
396 // AttributeImpl Definition
397 //===----------------------------------------------------------------------===//
399 AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data)
401 Data = ConstantInt::get(Type::getInt64Ty(C), data);
403 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data)
405 Data = ConstantInt::get(Type::getInt64Ty(C), data);
407 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
408 ArrayRef<Constant*> values)
410 Data = ConstantInt::get(Type::getInt64Ty(C), data);
411 Vals.reserve(values.size());
412 Vals.append(values.begin(), values.end());
414 AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data)
416 Data = ConstantDataArray::getString(C, data);
419 bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
420 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
421 return CI->getZExtValue() == Kind;
424 bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
425 return !(*this == Kind);
428 bool AttributeImpl::operator==(StringRef Kind) const {
429 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
431 return CDA->getAsString() == Kind;
434 bool AttributeImpl::operator!=(StringRef Kind) const {
435 return !(*this == Kind);
438 uint64_t AttributeImpl::Raw() const {
439 // FIXME: Remove this.
440 return cast<ConstantInt>(Data)->getZExtValue();
443 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
445 case Attribute::EndAttrKinds:
446 case Attribute::AttrKindEmptyKey:
447 case Attribute::AttrKindTombstoneKey:
448 llvm_unreachable("Synthetic enumerators which should never get here");
450 case Attribute::None: return 0;
451 case Attribute::ZExt: return 1 << 0;
452 case Attribute::SExt: return 1 << 1;
453 case Attribute::NoReturn: return 1 << 2;
454 case Attribute::InReg: return 1 << 3;
455 case Attribute::StructRet: return 1 << 4;
456 case Attribute::NoUnwind: return 1 << 5;
457 case Attribute::NoAlias: return 1 << 6;
458 case Attribute::ByVal: return 1 << 7;
459 case Attribute::Nest: return 1 << 8;
460 case Attribute::ReadNone: return 1 << 9;
461 case Attribute::ReadOnly: return 1 << 10;
462 case Attribute::NoInline: return 1 << 11;
463 case Attribute::AlwaysInline: return 1 << 12;
464 case Attribute::OptimizeForSize: return 1 << 13;
465 case Attribute::StackProtect: return 1 << 14;
466 case Attribute::StackProtectReq: return 1 << 15;
467 case Attribute::Alignment: return 31 << 16;
468 case Attribute::NoCapture: return 1 << 21;
469 case Attribute::NoRedZone: return 1 << 22;
470 case Attribute::NoImplicitFloat: return 1 << 23;
471 case Attribute::Naked: return 1 << 24;
472 case Attribute::InlineHint: return 1 << 25;
473 case Attribute::StackAlignment: return 7 << 26;
474 case Attribute::ReturnsTwice: return 1 << 29;
475 case Attribute::UWTable: return 1 << 30;
476 case Attribute::NonLazyBind: return 1U << 31;
477 case Attribute::AddressSafety: return 1ULL << 32;
478 case Attribute::MinSize: return 1ULL << 33;
479 case Attribute::NoDuplicate: return 1ULL << 34;
480 case Attribute::StackProtectStrong: return 1ULL << 35;
482 llvm_unreachable("Unsupported attribute type");
485 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
486 return (Raw() & getAttrMask(A)) != 0;
489 bool AttributeImpl::hasAttributes() const {
493 uint64_t AttributeImpl::getAlignment() const {
494 return Raw() & getAttrMask(Attribute::Alignment);
497 uint64_t AttributeImpl::getStackAlignment() const {
498 return Raw() & getAttrMask(Attribute::StackAlignment);
501 void AttributeImpl::Profile(FoldingSetNodeID &ID, Constant *Data,
502 ArrayRef<Constant*> Vals) {
503 ID.AddInteger(cast<ConstantInt>(Data)->getZExtValue());
505 // FIXME: Not yet supported.
506 for (ArrayRef<Constant*>::iterator I = Vals.begin(), E = Vals.end();
512 //===----------------------------------------------------------------------===//
513 // AttributeWithIndex Definition
514 //===----------------------------------------------------------------------===//
516 AttributeWithIndex AttributeWithIndex::get(LLVMContext &C, unsigned Idx,
518 // FIXME: This is temporary, but necessary for the conversion.
519 AttrBuilder B(AS, Idx);
520 return get(Idx, Attribute::get(C, B));
523 //===----------------------------------------------------------------------===//
524 // AttributeSetImpl Definition
525 //===----------------------------------------------------------------------===//
527 AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
529 return AttrList && hasAttributes(Idx) ?
530 AttributeSet::get(AttrList->getContext(),
531 AttributeWithIndex::get(Idx, getAttributes(Idx))) :
535 AttributeSet AttributeSet::getRetAttributes() const {
537 return AttrList && hasAttributes(ReturnIndex) ?
538 AttributeSet::get(AttrList->getContext(),
539 AttributeWithIndex::get(ReturnIndex,
540 getAttributes(ReturnIndex))) :
544 AttributeSet AttributeSet::getFnAttributes() const {
546 return AttrList && hasAttributes(FunctionIndex) ?
547 AttributeSet::get(AttrList->getContext(),
548 AttributeWithIndex::get(FunctionIndex,
549 getAttributes(FunctionIndex))) :
553 AttributeSet AttributeSet::get(LLVMContext &C,
554 ArrayRef<AttributeWithIndex> Attrs) {
555 // If there are no attributes then return a null AttributesList pointer.
557 return AttributeSet();
560 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
561 assert(Attrs[i].Attrs.hasAttributes() &&
562 "Pointless attribute!");
563 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
564 "Misordered AttributesList!");
568 // Otherwise, build a key to look up the existing attributes.
569 LLVMContextImpl *pImpl = C.pImpl;
571 AttributeSetImpl::Profile(ID, Attrs);
574 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
576 // If we didn't find any existing attributes of the same shape then
577 // create a new one and insert it.
579 PA = new AttributeSetImpl(C, Attrs);
580 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
583 // Return the AttributesList that we found or created.
584 return AttributeSet(PA);
587 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
588 // FIXME: This should be implemented as a loop that creates the
589 // AttributeWithIndexes that then are used to create the AttributeSet.
590 if (!B.hasAttributes())
591 return AttributeSet();
592 return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, B)));
595 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
596 Attribute::AttrKind Kind) {
597 return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, Kind)));
600 //===----------------------------------------------------------------------===//
601 // AttributeSet Method Implementations
602 //===----------------------------------------------------------------------===//
604 const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) {
605 AttrList = RHS.AttrList;
609 /// getNumSlots - Return the number of slots used in this attribute list.
610 /// This is the number of arguments that have an attribute set on them
611 /// (including the function itself).
612 unsigned AttributeSet::getNumSlots() const {
613 return AttrList ? AttrList->getNumAttributes() : 0;
616 /// getSlot - Return the AttributeWithIndex at the specified slot. This
617 /// holds a number plus a set of attributes.
618 const AttributeWithIndex &AttributeSet::getSlot(unsigned Slot) const {
619 assert(AttrList && Slot < AttrList->getNumAttributes() &&
620 "Slot # out of range!");
621 return AttrList->getAttributes()[Slot];
624 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
625 return getAttributes(Index).hasAttribute(Kind);
628 bool AttributeSet::hasAttributes(unsigned Index) const {
629 return getAttributes(Index).hasAttributes();
632 std::string AttributeSet::getAsString(unsigned Index) const {
633 return getAttributes(Index).getAsString();
636 unsigned AttributeSet::getParamAlignment(unsigned Idx) const {
637 return getAttributes(Idx).getAlignment();
640 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
641 return getAttributes(Index).getStackAlignment();
644 uint64_t AttributeSet::Raw(unsigned Index) const {
645 // FIXME: Remove this.
646 return getAttributes(Index).Raw();
649 /// getAttributes - The attributes for the specified index are returned.
650 Attribute AttributeSet::getAttributes(unsigned Idx) const {
651 if (AttrList == 0) return Attribute();
653 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
654 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
655 if (Attrs[i].Index == Idx)
656 return Attrs[i].Attrs;
661 /// hasAttrSomewhere - Return true if the specified attribute is set for at
662 /// least one parameter or for the return value.
663 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
664 if (AttrList == 0) return false;
666 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
667 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
668 if (Attrs[i].Attrs.hasAttribute(Attr))
674 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
675 Attribute::AttrKind Attr) const {
676 return addAttr(C, Idx, Attribute::get(C, Attr));
679 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
680 AttributeSet Attrs) const {
681 return addAttr(C, Idx, Attrs.getAttributes(Idx));
684 AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
685 Attribute Attrs) const {
686 Attribute OldAttrs = getAttributes(Idx);
688 // FIXME it is not obvious how this should work for alignment.
689 // For now, say we can't change a known alignment.
690 unsigned OldAlign = OldAttrs.getAlignment();
691 unsigned NewAlign = Attrs.getAlignment();
692 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
693 "Attempt to change alignment!");
696 AttrBuilder NewAttrs =
697 AttrBuilder(OldAttrs).addAttributes(Attrs);
698 if (NewAttrs == AttrBuilder(OldAttrs))
701 SmallVector<AttributeWithIndex, 8> NewAttrList;
703 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
705 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
706 unsigned i = 0, e = OldAttrList.size();
707 // Copy attributes for arguments before this one.
708 for (; i != e && OldAttrList[i].Index < Idx; ++i)
709 NewAttrList.push_back(OldAttrList[i]);
711 // If there are attributes already at this index, merge them in.
712 if (i != e && OldAttrList[i].Index == Idx) {
714 Attribute::get(C, AttrBuilder(Attrs).
715 addAttributes(OldAttrList[i].Attrs));
719 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
721 // Copy attributes for arguments after this one.
722 NewAttrList.insert(NewAttrList.end(),
723 OldAttrList.begin()+i, OldAttrList.end());
726 return get(C, NewAttrList);
729 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
730 Attribute::AttrKind Attr) const {
731 return removeAttr(C, Idx, Attribute::get(C, Attr));
734 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
735 AttributeSet Attrs) const {
736 return removeAttr(C, Idx, Attrs.getAttributes(Idx));
739 AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
740 Attribute Attrs) const {
742 // FIXME it is not obvious how this should work for alignment.
743 // For now, say we can't pass in alignment, which no current use does.
744 assert(!Attrs.hasAttribute(Attribute::Alignment) &&
745 "Attempt to exclude alignment!");
747 if (AttrList == 0) return AttributeSet();
749 Attribute OldAttrs = getAttributes(Idx);
750 AttrBuilder NewAttrs =
751 AttrBuilder(OldAttrs).removeAttributes(Attrs);
752 if (NewAttrs == AttrBuilder(OldAttrs))
755 SmallVector<AttributeWithIndex, 8> NewAttrList;
756 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
757 unsigned i = 0, e = OldAttrList.size();
759 // Copy attributes for arguments before this one.
760 for (; i != e && OldAttrList[i].Index < Idx; ++i)
761 NewAttrList.push_back(OldAttrList[i]);
763 // If there are attributes already at this index, merge them in.
764 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
765 Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
766 removeAttributes(Attrs));
768 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
769 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
771 // Copy attributes for arguments after this one.
772 NewAttrList.insert(NewAttrList.end(),
773 OldAttrList.begin()+i, OldAttrList.end());
775 return get(C, NewAttrList);
778 void AttributeSet::dump() const {
780 for (unsigned i = 0; i < getNumSlots(); ++i) {
781 const AttributeWithIndex &PAWI = getSlot(i);
782 dbgs() << "{ " << PAWI.Index << ", " << PAWI.Attrs.getAsString() << " } ";