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 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;
102 uint64_t Attribute::Raw() const {
103 return pImpl ? pImpl->Raw() : 0;
106 std::string Attribute::getAsString() const {
108 if (hasAttribute(Attribute::ZExt))
109 Result += "zeroext ";
110 if (hasAttribute(Attribute::SExt))
111 Result += "signext ";
112 if (hasAttribute(Attribute::NoReturn))
113 Result += "noreturn ";
114 if (hasAttribute(Attribute::NoUnwind))
115 Result += "nounwind ";
116 if (hasAttribute(Attribute::UWTable))
117 Result += "uwtable ";
118 if (hasAttribute(Attribute::ReturnsTwice))
119 Result += "returns_twice ";
120 if (hasAttribute(Attribute::InReg))
122 if (hasAttribute(Attribute::NoAlias))
123 Result += "noalias ";
124 if (hasAttribute(Attribute::NoCapture))
125 Result += "nocapture ";
126 if (hasAttribute(Attribute::StructRet))
128 if (hasAttribute(Attribute::ByVal))
130 if (hasAttribute(Attribute::Nest))
132 if (hasAttribute(Attribute::ReadNone))
133 Result += "readnone ";
134 if (hasAttribute(Attribute::ReadOnly))
135 Result += "readonly ";
136 if (hasAttribute(Attribute::OptimizeForSize))
137 Result += "optsize ";
138 if (hasAttribute(Attribute::NoInline))
139 Result += "noinline ";
140 if (hasAttribute(Attribute::InlineHint))
141 Result += "inlinehint ";
142 if (hasAttribute(Attribute::AlwaysInline))
143 Result += "alwaysinline ";
144 if (hasAttribute(Attribute::StackProtect))
146 if (hasAttribute(Attribute::StackProtectReq))
148 if (hasAttribute(Attribute::StackProtectStrong))
149 Result += "sspstrong ";
150 if (hasAttribute(Attribute::NoRedZone))
151 Result += "noredzone ";
152 if (hasAttribute(Attribute::NoImplicitFloat))
153 Result += "noimplicitfloat ";
154 if (hasAttribute(Attribute::Naked))
156 if (hasAttribute(Attribute::NonLazyBind))
157 Result += "nonlazybind ";
158 if (hasAttribute(Attribute::AddressSafety))
159 Result += "address_safety ";
160 if (hasAttribute(Attribute::MinSize))
161 Result += "minsize ";
162 if (hasAttribute(Attribute::StackAlignment)) {
163 Result += "alignstack(";
164 Result += utostr(getStackAlignment());
167 if (hasAttribute(Attribute::Alignment)) {
169 Result += utostr(getAlignment());
172 if (hasAttribute(Attribute::NoDuplicate))
173 Result += "noduplicate ";
174 // Trim the trailing space.
175 assert(!Result.empty() && "Unknown attribute!");
176 Result.erase(Result.end()-1);
180 //===----------------------------------------------------------------------===//
181 // AttrBuilder Method Implementations
182 //===----------------------------------------------------------------------===//
184 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
185 : Alignment(0), StackAlignment(0) {
186 AttributeSetImpl *pImpl = AS.AttrList;
189 ArrayRef<AttributeWithIndex> AttrList = pImpl->getAttributes();
190 const AttributeWithIndex *AWI = 0;
191 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
192 if (AttrList[I].Index == Idx) {
199 uint64_t Mask = AWI->Attrs.Raw();
201 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
202 I = Attribute::AttrKind(I + 1)) {
203 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
206 if (I == Attribute::Alignment)
207 Alignment = 1ULL << ((A >> 16) - 1);
208 else if (I == Attribute::StackAlignment)
209 StackAlignment = 1ULL << ((A >> 26)-1);
214 void AttrBuilder::clear() {
216 Alignment = StackAlignment = 0;
219 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
224 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
226 if (Val == Attribute::Alignment)
228 else if (Val == Attribute::StackAlignment)
234 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
235 if (Align == 0) return *this;
237 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
238 assert(Align <= 0x40000000 && "Alignment too large.");
240 Attrs.insert(Attribute::Alignment);
245 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
246 // Default alignment, allow the target to define how to align it.
247 if (Align == 0) return *this;
249 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
250 assert(Align <= 0x100 && "Alignment too large.");
252 Attrs.insert(Attribute::StackAlignment);
253 StackAlignment = Align;
257 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
258 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
259 I = Attribute::AttrKind(I + 1)) {
260 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
263 if (I == Attribute::Alignment)
264 Alignment = 1ULL << ((A >> 16) - 1);
265 else if (I == Attribute::StackAlignment)
266 StackAlignment = 1ULL << ((A >> 26)-1);
273 AttrBuilder &AttrBuilder::addAttributes(const Attribute &Attr) {
274 uint64_t Mask = Attr.Raw();
276 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
277 I = Attribute::AttrKind(I + 1))
278 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
281 if (Attr.getAlignment())
282 Alignment = Attr.getAlignment();
283 if (Attr.getStackAlignment())
284 StackAlignment = Attr.getStackAlignment();
288 AttrBuilder &AttrBuilder::removeAttributes(const Attribute &A){
289 uint64_t Mask = A.Raw();
291 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
292 I = Attribute::AttrKind(I + 1)) {
293 if (Mask & AttributeImpl::getAttrMask(I)) {
296 if (I == Attribute::Alignment)
298 else if (I == Attribute::StackAlignment)
306 bool AttrBuilder::contains(Attribute::AttrKind A) const {
307 return Attrs.count(A);
310 bool AttrBuilder::hasAttributes() const {
311 return !Attrs.empty();
314 bool AttrBuilder::hasAttributes(const Attribute &A) const {
315 return Raw() & A.Raw();
318 bool AttrBuilder::hasAlignmentAttr() const {
319 return Alignment != 0;
322 uint64_t AttrBuilder::Raw() const {
325 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
326 E = Attrs.end(); I != E; ++I) {
327 Attribute::AttrKind Kind = *I;
329 if (Kind == Attribute::Alignment)
330 Mask |= (Log2_32(Alignment) + 1) << 16;
331 else if (Kind == Attribute::StackAlignment)
332 Mask |= (Log2_32(StackAlignment) + 1) << 26;
334 Mask |= AttributeImpl::getAttrMask(Kind);
340 bool AttrBuilder::operator==(const AttrBuilder &B) {
341 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
342 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
346 //===----------------------------------------------------------------------===//
347 // AttributeImpl Definition
348 //===----------------------------------------------------------------------===//
350 AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data)
352 Data = ConstantInt::get(Type::getInt64Ty(C), data);
354 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data)
356 Data = ConstantInt::get(Type::getInt64Ty(C), data);
358 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
359 ArrayRef<Constant*> values)
361 Data = ConstantInt::get(Type::getInt64Ty(C), data);
362 Vals.reserve(values.size());
363 Vals.append(values.begin(), values.end());
365 AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data)
367 Data = ConstantDataArray::getString(C, data);
370 bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
371 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
372 return CI->getZExtValue() == Kind;
375 bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
376 return !(*this == Kind);
379 bool AttributeImpl::operator==(StringRef Kind) const {
380 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
382 return CDA->getAsString() == Kind;
386 bool AttributeImpl::operator!=(StringRef Kind) const {
387 return !(*this == Kind);
390 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
391 if (!Data && !AI.Data) return false;
392 if (!Data && AI.Data) return true;
393 if (Data && !AI.Data) return false;
395 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Data);
396 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Data);
398 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Data);
399 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Data);
401 if (ThisCI && ThatCI)
402 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
404 if (ThisCI && ThatCDA)
407 if (ThisCDA && ThatCI)
410 return ThisCDA->getAsString() < ThatCDA->getAsString();
413 uint64_t AttributeImpl::Raw() const {
414 // FIXME: Remove this.
415 return cast<ConstantInt>(Data)->getZExtValue();
418 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
420 case Attribute::EndAttrKinds:
421 case Attribute::AttrKindEmptyKey:
422 case Attribute::AttrKindTombstoneKey:
423 llvm_unreachable("Synthetic enumerators which should never get here");
425 case Attribute::None: return 0;
426 case Attribute::ZExt: return 1 << 0;
427 case Attribute::SExt: return 1 << 1;
428 case Attribute::NoReturn: return 1 << 2;
429 case Attribute::InReg: return 1 << 3;
430 case Attribute::StructRet: return 1 << 4;
431 case Attribute::NoUnwind: return 1 << 5;
432 case Attribute::NoAlias: return 1 << 6;
433 case Attribute::ByVal: return 1 << 7;
434 case Attribute::Nest: return 1 << 8;
435 case Attribute::ReadNone: return 1 << 9;
436 case Attribute::ReadOnly: return 1 << 10;
437 case Attribute::NoInline: return 1 << 11;
438 case Attribute::AlwaysInline: return 1 << 12;
439 case Attribute::OptimizeForSize: return 1 << 13;
440 case Attribute::StackProtect: return 1 << 14;
441 case Attribute::StackProtectReq: return 1 << 15;
442 case Attribute::Alignment: return 31 << 16;
443 case Attribute::NoCapture: return 1 << 21;
444 case Attribute::NoRedZone: return 1 << 22;
445 case Attribute::NoImplicitFloat: return 1 << 23;
446 case Attribute::Naked: return 1 << 24;
447 case Attribute::InlineHint: return 1 << 25;
448 case Attribute::StackAlignment: return 7 << 26;
449 case Attribute::ReturnsTwice: return 1 << 29;
450 case Attribute::UWTable: return 1 << 30;
451 case Attribute::NonLazyBind: return 1U << 31;
452 case Attribute::AddressSafety: return 1ULL << 32;
453 case Attribute::MinSize: return 1ULL << 33;
454 case Attribute::NoDuplicate: return 1ULL << 34;
455 case Attribute::StackProtectStrong: return 1ULL << 35;
457 llvm_unreachable("Unsupported attribute type");
460 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
461 return (Raw() & getAttrMask(A)) != 0;
464 bool AttributeImpl::hasAttributes() const {
468 uint64_t AttributeImpl::getAlignment() const {
469 uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment);
470 return 1ULL << ((Mask >> 16) - 1);
473 uint64_t AttributeImpl::getStackAlignment() const {
474 uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment);
475 return 1ULL << ((Mask >> 26) - 1);
478 void AttributeImpl::Profile(FoldingSetNodeID &ID, Constant *Data,
479 ArrayRef<Constant*> Vals) {
480 ID.AddInteger(cast<ConstantInt>(Data)->getZExtValue());
482 // FIXME: Not yet supported.
483 for (ArrayRef<Constant*>::iterator I = Vals.begin(), E = Vals.end();
489 //===----------------------------------------------------------------------===//
490 // AttributeSetNode Definition
491 //===----------------------------------------------------------------------===//
493 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
494 ArrayRef<Attribute> Attrs) {
498 // Otherwise, build a key to look up the existing attributes.
499 LLVMContextImpl *pImpl = C.pImpl;
502 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
503 std::sort(SortedAttrs.begin(), SortedAttrs.end());
505 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
506 E = SortedAttrs.end(); I != E; ++I)
510 AttributeSetNode *PA =
511 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
513 // If we didn't find any existing attributes of the same shape then create a
514 // new one and insert it.
516 PA = new AttributeSetNode(SortedAttrs);
517 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
520 // Return the AttributesListNode that we found or created.
524 //===----------------------------------------------------------------------===//
525 // AttributeSetImpl Definition
526 //===----------------------------------------------------------------------===//
529 AttributeSetImpl(LLVMContext &C,
530 ArrayRef<AttributeWithIndex> attrs)
531 : Context(C), AttrList(attrs.begin(), attrs.end()) {
532 for (unsigned I = 0, E = attrs.size(); I != E; ++I) {
533 const AttributeWithIndex &AWI = attrs[I];
534 uint64_t Mask = AWI.Attrs.Raw();
535 SmallVector<Attribute, 8> Attrs;
537 for (Attribute::AttrKind II = Attribute::None;
538 II != Attribute::EndAttrKinds; II = Attribute::AttrKind(II + 1)) {
539 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(II))) {
542 if (II == Attribute::Alignment)
543 B.addAlignmentAttr(1ULL << ((A >> 16) - 1));
544 else if (II == Attribute::StackAlignment)
545 B.addStackAlignmentAttr(1ULL << ((A >> 26) - 1));
549 Attrs.push_back(Attribute::get(C, B));
553 AttrNodes.push_back(std::make_pair(AWI.Index,
554 AttributeSetNode::get(C, Attrs)));
559 AttributeSetImpl(LLVMContext &C,
560 ArrayRef<std::pair<uint64_t, AttributeSetNode*> > attrs)
561 : Context(C), AttrNodes(attrs.begin(), attrs.end()) {
564 //===----------------------------------------------------------------------===//
565 // AttributeSet Method Implementations
566 //===----------------------------------------------------------------------===//
568 AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
570 return AttrList && hasAttributes(Idx) ?
571 AttributeSet::get(AttrList->getContext(),
572 AttributeWithIndex::get(Idx, getAttributes(Idx))) :
576 AttributeSet AttributeSet::getRetAttributes() const {
578 return AttrList && hasAttributes(ReturnIndex) ?
579 AttributeSet::get(AttrList->getContext(),
580 AttributeWithIndex::get(ReturnIndex,
581 getAttributes(ReturnIndex))) :
585 AttributeSet AttributeSet::getFnAttributes() const {
587 return AttrList && hasAttributes(FunctionIndex) ?
588 AttributeSet::get(AttrList->getContext(),
589 AttributeWithIndex::get(FunctionIndex,
590 getAttributes(FunctionIndex))) :
594 AttributeSet AttributeSet::get(LLVMContext &C,
595 ArrayRef<AttributeWithIndex> Attrs) {
596 // If there are no attributes then return a null AttributesList pointer.
598 return AttributeSet();
601 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
602 assert(Attrs[i].Attrs.hasAttributes() &&
603 "Pointless attribute!");
604 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
605 "Misordered AttributesList!");
609 // Otherwise, build a key to look up the existing attributes.
610 LLVMContextImpl *pImpl = C.pImpl;
612 AttributeSetImpl::Profile(ID, Attrs);
615 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
617 // If we didn't find any existing attributes of the same shape then
618 // create a new one and insert it.
620 PA = new AttributeSetImpl(C, Attrs);
621 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
624 // Return the AttributesList that we found or created.
625 return AttributeSet(PA);
628 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
629 // FIXME: This should be implemented as a loop that creates the
630 // AttributeWithIndexes that then are used to create the AttributeSet.
631 if (!B.hasAttributes())
632 return AttributeSet();
633 return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, B)));
636 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
637 ArrayRef<Attribute::AttrKind> Kind) {
638 // FIXME: This is temporary. Ultimately, the AttributeWithIndex will be
639 // replaced by an object that holds multiple Attribute::AttrKinds.
641 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
642 E = Kind.end(); I != E; ++I)
644 return get(C, Idx, B);
647 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
648 SmallVector<AttributeWithIndex, 8> AttrList;
649 for (ArrayRef<AttributeSet>::iterator I = Attrs.begin(), E = Attrs.end();
651 AttributeSet AS = *I;
652 if (!AS.AttrList) continue;
653 AttrList.append(AS.AttrList->AttrList.begin(), AS.AttrList->AttrList.end());
656 return get(C, AttrList);
659 const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) {
660 AttrList = RHS.AttrList;
664 /// getNumSlots - Return the number of slots used in this attribute list.
665 /// This is the number of arguments that have an attribute set on them
666 /// (including the function itself).
667 unsigned AttributeSet::getNumSlots() const {
668 return AttrList ? AttrList->getNumAttributes() : 0;
671 unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
672 assert(AttrList && Slot < AttrList->getNumAttributes() &&
673 "Slot # out of range!");
674 return AttrList->getSlotIndex(Slot);
677 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
678 assert(AttrList && Slot < AttrList->getNumAttributes() &&
679 "Slot # out of range!");
680 return AttrList->getSlotAttributes(Slot);
683 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
684 return getAttributes(Index).hasAttribute(Kind);
687 bool AttributeSet::hasAttributes(unsigned Index) const {
688 return getAttributes(Index).hasAttributes();
691 std::string AttributeSet::getAsString(unsigned Index) const {
692 return getAttributes(Index).getAsString();
695 unsigned AttributeSet::getParamAlignment(unsigned Idx) const {
696 return getAttributes(Idx).getAlignment();
699 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
700 return getAttributes(Index).getStackAlignment();
703 uint64_t AttributeSet::Raw(unsigned Index) const {
704 // FIXME: Remove this.
705 return getAttributes(Index).Raw();
708 /// getAttributes - The attributes for the specified index are returned.
709 Attribute AttributeSet::getAttributes(unsigned Idx) const {
710 if (AttrList == 0) return Attribute();
712 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
713 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
714 if (Attrs[i].Index == Idx)
715 return Attrs[i].Attrs;
720 /// hasAttrSomewhere - Return true if the specified attribute is set for at
721 /// least one parameter or for the return value.
722 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
723 if (AttrList == 0) return false;
725 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
726 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
727 if (Attrs[i].Attrs.hasAttribute(Attr))
733 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
734 Attribute::AttrKind Attr) const {
735 return addAttr(C, Idx, Attribute::get(C, Attr));
738 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
739 AttributeSet Attrs) const {
740 return addAttr(C, Idx, Attrs.getAttributes(Idx));
743 AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
744 Attribute Attrs) const {
745 Attribute OldAttrs = getAttributes(Idx);
747 // FIXME it is not obvious how this should work for alignment.
748 // For now, say we can't change a known alignment.
749 unsigned OldAlign = OldAttrs.getAlignment();
750 unsigned NewAlign = Attrs.getAlignment();
751 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
752 "Attempt to change alignment!");
755 AttrBuilder NewAttrs =
756 AttrBuilder(OldAttrs).addAttributes(Attrs);
757 if (NewAttrs == AttrBuilder(OldAttrs))
760 SmallVector<AttributeWithIndex, 8> NewAttrList;
762 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
764 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
765 unsigned i = 0, e = OldAttrList.size();
766 // Copy attributes for arguments before this one.
767 for (; i != e && OldAttrList[i].Index < Idx; ++i)
768 NewAttrList.push_back(OldAttrList[i]);
770 // If there are attributes already at this index, merge them in.
771 if (i != e && OldAttrList[i].Index == Idx) {
773 Attribute::get(C, AttrBuilder(Attrs).
774 addAttributes(OldAttrList[i].Attrs));
778 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
780 // Copy attributes for arguments after this one.
781 NewAttrList.insert(NewAttrList.end(),
782 OldAttrList.begin()+i, OldAttrList.end());
785 return get(C, NewAttrList);
788 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
789 Attribute::AttrKind Attr) const {
790 return removeAttr(C, Idx, Attribute::get(C, Attr));
793 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
794 AttributeSet Attrs) const {
795 return removeAttr(C, Idx, Attrs.getAttributes(Idx));
798 AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
799 Attribute Attrs) const {
801 // FIXME it is not obvious how this should work for alignment.
802 // For now, say we can't pass in alignment, which no current use does.
803 assert(!Attrs.hasAttribute(Attribute::Alignment) &&
804 "Attempt to exclude alignment!");
806 if (AttrList == 0) return AttributeSet();
808 Attribute OldAttrs = getAttributes(Idx);
809 AttrBuilder NewAttrs =
810 AttrBuilder(OldAttrs).removeAttributes(Attrs);
811 if (NewAttrs == AttrBuilder(OldAttrs))
814 SmallVector<AttributeWithIndex, 8> NewAttrList;
815 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
816 unsigned i = 0, e = OldAttrList.size();
818 // Copy attributes for arguments before this one.
819 for (; i != e && OldAttrList[i].Index < Idx; ++i)
820 NewAttrList.push_back(OldAttrList[i]);
822 // If there are attributes already at this index, merge them in.
823 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
824 Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
825 removeAttributes(Attrs));
827 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
828 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
830 // Copy attributes for arguments after this one.
831 NewAttrList.insert(NewAttrList.end(),
832 OldAttrList.begin()+i, OldAttrList.end());
834 return get(C, NewAttrList);
837 void AttributeSet::dump() const {
839 for (unsigned i = 0; i < getNumSlots(); ++i) {
840 unsigned Index = getSlotIndex(i);
841 dbgs() << "{ " << Index << " => " << getAsString(Index) << " } ";
847 //===----------------------------------------------------------------------===//
848 // AttributeFuncs Function Defintions
849 //===----------------------------------------------------------------------===//
851 Attribute AttributeFuncs::typeIncompatible(Type *Ty) {
852 AttrBuilder Incompatible;
854 if (!Ty->isIntegerTy())
855 // Attribute that only apply to integers.
856 Incompatible.addAttribute(Attribute::SExt)
857 .addAttribute(Attribute::ZExt);
859 if (!Ty->isPointerTy())
860 // Attribute that only apply to pointers.
861 Incompatible.addAttribute(Attribute::ByVal)
862 .addAttribute(Attribute::Nest)
863 .addAttribute(Attribute::NoAlias)
864 .addAttribute(Attribute::NoCapture)
865 .addAttribute(Attribute::StructRet);
867 return Attribute::get(Ty->getContext(), Incompatible);
870 /// encodeLLVMAttributesForBitcode - This returns an integer containing an
871 /// encoding of all the LLVM attributes found in the given attribute bitset.
872 /// Any change to this encoding is a breaking change to bitcode compatibility.
873 uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
875 // FIXME: It doesn't make sense to store the alignment information as an
876 // expanded out value, we should store it as a log2 value. However, we can't
877 // just change that here without breaking bitcode compatibility. If this ever
878 // becomes a problem in practice, we should introduce new tag numbers in the
879 // bitcode file and have those tags use a more efficiently encoded alignment
882 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
883 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
884 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
885 if (Attrs.hasAttribute(Index, Attribute::Alignment))
886 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
887 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
891 /// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
892 /// the LLVM attributes that have been decoded from the given integer. This
893 /// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
894 Attribute AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
895 uint64_t EncodedAttrs){
896 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
897 // the bits above 31 down by 11 bits.
898 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
899 assert((!Alignment || isPowerOf2_32(Alignment)) &&
900 "Alignment must be a power of two.");
902 AttrBuilder B(EncodedAttrs & 0xffff);
904 B.addAlignmentAttr(Alignment);
905 B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
906 return Attribute::get(C, B);