1 //===-- Attributes.cpp - Implement AttributesList -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 // \brief This file implements the Attribute, AttributeImpl, AttrBuilder,
12 // AttributeSetImpl, and AttributeSet classes.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/IR/Attributes.h"
17 #include "AttributeImpl.h"
18 #include "LLVMContextImpl.h"
19 #include "llvm/ADT/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 Construction Methods
31 //===----------------------------------------------------------------------===//
33 Attribute Attribute::get(LLVMContext &Context, AttrKind Kind) {
35 return Attribute::get(Context, B.addAttribute(Kind));
38 Attribute Attribute::get(LLVMContext &Context, AttrBuilder &B) {
39 // If there are no attributes, return an empty Attribute class.
40 if (!B.hasAttributes())
43 // Otherwise, build a key to look up the existing attributes.
44 LLVMContextImpl *pImpl = Context.pImpl;
46 ConstantInt *CI = ConstantInt::get(Type::getInt64Ty(Context), B.Raw());
50 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
53 // If we didn't find any existing attributes of the same shape then create a
54 // new one and insert it.
55 PA = new AttributeImpl(Context, CI);
56 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
59 // Return the AttributesList that we found or created.
63 Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
65 return get(Context, B.addAlignmentAttr(Align));
68 Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
71 return get(Context, B.addStackAlignmentAttr(Align));
74 //===----------------------------------------------------------------------===//
75 // Attribute Accessor Methods
76 //===----------------------------------------------------------------------===//
78 bool Attribute::hasAttribute(AttrKind Val) const {
79 return pImpl && pImpl->hasAttribute(Val);
82 bool Attribute::hasAttributes() const {
83 return pImpl && pImpl->hasAttributes();
86 /// This returns the alignment field of an attribute as a byte alignment value.
87 unsigned Attribute::getAlignment() const {
88 if (!hasAttribute(Attribute::Alignment))
90 return pImpl->getAlignment();
93 /// This returns the stack alignment field of an attribute as a byte alignment
95 unsigned Attribute::getStackAlignment() const {
96 if (!hasAttribute(Attribute::StackAlignment))
98 return pImpl->getStackAlignment();
101 std::string Attribute::getAsString() const {
102 if (hasAttribute(Attribute::ZExt))
104 if (hasAttribute(Attribute::SExt))
106 if (hasAttribute(Attribute::NoReturn))
108 if (hasAttribute(Attribute::NoUnwind))
110 if (hasAttribute(Attribute::UWTable))
112 if (hasAttribute(Attribute::ReturnsTwice))
113 return "returns_twice";
114 if (hasAttribute(Attribute::InReg))
116 if (hasAttribute(Attribute::NoAlias))
118 if (hasAttribute(Attribute::NoCapture))
120 if (hasAttribute(Attribute::StructRet))
122 if (hasAttribute(Attribute::ByVal))
124 if (hasAttribute(Attribute::Nest))
126 if (hasAttribute(Attribute::ReadNone))
128 if (hasAttribute(Attribute::ReadOnly))
130 if (hasAttribute(Attribute::OptimizeForSize))
132 if (hasAttribute(Attribute::NoInline))
134 if (hasAttribute(Attribute::InlineHint))
136 if (hasAttribute(Attribute::AlwaysInline))
137 return "alwaysinline";
138 if (hasAttribute(Attribute::StackProtect))
140 if (hasAttribute(Attribute::StackProtectReq))
142 if (hasAttribute(Attribute::StackProtectStrong))
144 if (hasAttribute(Attribute::NoRedZone))
146 if (hasAttribute(Attribute::NoImplicitFloat))
147 return "noimplicitfloat";
148 if (hasAttribute(Attribute::Naked))
150 if (hasAttribute(Attribute::NonLazyBind))
151 return "nonlazybind";
152 if (hasAttribute(Attribute::AddressSafety))
153 return "address_safety";
154 if (hasAttribute(Attribute::MinSize))
156 if (hasAttribute(Attribute::StackAlignment)) {
158 Result += "alignstack(";
159 Result += utostr(getStackAlignment());
163 if (hasAttribute(Attribute::Alignment)) {
166 Result += utostr(getAlignment());
170 if (hasAttribute(Attribute::NoDuplicate))
171 return "noduplicate";
173 llvm_unreachable("Unknown attribute");
176 bool Attribute::operator==(AttrKind K) const {
177 return pImpl && *pImpl == K;
179 bool Attribute::operator!=(AttrKind K) const {
180 return !(*this == K);
183 bool Attribute::operator<(Attribute A) const {
184 if (!pImpl && !A.pImpl) return false;
185 if (!pImpl) return true;
186 if (!A.pImpl) return false;
187 return *pImpl < *A.pImpl;
190 uint64_t Attribute::Raw() const {
191 return pImpl ? pImpl->Raw() : 0;
194 //===----------------------------------------------------------------------===//
195 // AttributeImpl Definition
196 //===----------------------------------------------------------------------===//
198 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data)
200 Data = ConstantInt::get(Type::getInt64Ty(C), data);
202 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
203 ArrayRef<Constant*> values)
205 Data = ConstantInt::get(Type::getInt64Ty(C), data);
206 Vals.reserve(values.size());
207 Vals.append(values.begin(), values.end());
209 AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data)
211 Data = ConstantDataArray::getString(C, data);
214 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
215 return (Raw() & getAttrMask(A)) != 0;
218 bool AttributeImpl::hasAttributes() const {
222 uint64_t AttributeImpl::getAlignment() const {
223 uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment);
224 return 1ULL << ((Mask >> 16) - 1);
227 uint64_t AttributeImpl::getStackAlignment() const {
228 uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment);
229 return 1ULL << ((Mask >> 26) - 1);
232 bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
233 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
234 return CI->getZExtValue() == Kind;
237 bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
238 return !(*this == Kind);
241 bool AttributeImpl::operator==(StringRef Kind) const {
242 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
244 return CDA->getAsString() == Kind;
248 bool AttributeImpl::operator!=(StringRef Kind) const {
249 return !(*this == Kind);
252 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
253 if (!Data && !AI.Data) return false;
254 if (!Data && AI.Data) return true;
255 if (Data && !AI.Data) return false;
257 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Data);
258 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Data);
260 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Data);
261 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Data);
263 if (ThisCI && ThatCI)
264 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
266 if (ThisCI && ThatCDA)
269 if (ThisCDA && ThatCI)
272 return ThisCDA->getAsString() < ThatCDA->getAsString();
275 uint64_t AttributeImpl::Raw() const {
276 // FIXME: Remove this.
277 return cast<ConstantInt>(Data)->getZExtValue();
280 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
281 // FIXME: Remove this.
283 case Attribute::EndAttrKinds:
284 case Attribute::AttrKindEmptyKey:
285 case Attribute::AttrKindTombstoneKey:
286 llvm_unreachable("Synthetic enumerators which should never get here");
288 case Attribute::None: return 0;
289 case Attribute::ZExt: return 1 << 0;
290 case Attribute::SExt: return 1 << 1;
291 case Attribute::NoReturn: return 1 << 2;
292 case Attribute::InReg: return 1 << 3;
293 case Attribute::StructRet: return 1 << 4;
294 case Attribute::NoUnwind: return 1 << 5;
295 case Attribute::NoAlias: return 1 << 6;
296 case Attribute::ByVal: return 1 << 7;
297 case Attribute::Nest: return 1 << 8;
298 case Attribute::ReadNone: return 1 << 9;
299 case Attribute::ReadOnly: return 1 << 10;
300 case Attribute::NoInline: return 1 << 11;
301 case Attribute::AlwaysInline: return 1 << 12;
302 case Attribute::OptimizeForSize: return 1 << 13;
303 case Attribute::StackProtect: return 1 << 14;
304 case Attribute::StackProtectReq: return 1 << 15;
305 case Attribute::Alignment: return 31 << 16;
306 case Attribute::NoCapture: return 1 << 21;
307 case Attribute::NoRedZone: return 1 << 22;
308 case Attribute::NoImplicitFloat: return 1 << 23;
309 case Attribute::Naked: return 1 << 24;
310 case Attribute::InlineHint: return 1 << 25;
311 case Attribute::StackAlignment: return 7 << 26;
312 case Attribute::ReturnsTwice: return 1 << 29;
313 case Attribute::UWTable: return 1 << 30;
314 case Attribute::NonLazyBind: return 1U << 31;
315 case Attribute::AddressSafety: return 1ULL << 32;
316 case Attribute::MinSize: return 1ULL << 33;
317 case Attribute::NoDuplicate: return 1ULL << 34;
318 case Attribute::StackProtectStrong: return 1ULL << 35;
320 llvm_unreachable("Unsupported attribute type");
323 //===----------------------------------------------------------------------===//
324 // AttributeSetNode Definition
325 //===----------------------------------------------------------------------===//
327 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
328 ArrayRef<Attribute> Attrs) {
332 // Otherwise, build a key to look up the existing attributes.
333 LLVMContextImpl *pImpl = C.pImpl;
336 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
337 std::sort(SortedAttrs.begin(), SortedAttrs.end());
339 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
340 E = SortedAttrs.end(); I != E; ++I)
344 AttributeSetNode *PA =
345 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
347 // If we didn't find any existing attributes of the same shape then create a
348 // new one and insert it.
350 PA = new AttributeSetNode(SortedAttrs);
351 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
354 // Return the AttributesListNode that we found or created.
358 bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
359 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
360 E = AttrList.end(); I != E; ++I)
361 if (I->hasAttribute(Kind))
366 unsigned AttributeSetNode::getAlignment() const {
367 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
368 E = AttrList.end(); I != E; ++I)
369 if (I->hasAttribute(Attribute::Alignment))
370 return I->getAlignment();
374 unsigned AttributeSetNode::getStackAlignment() const {
375 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
376 E = AttrList.end(); I != E; ++I)
377 if (I->hasAttribute(Attribute::StackAlignment))
378 return I->getStackAlignment();
382 std::string AttributeSetNode::getAsString() const {
383 std::string Str = "";
384 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
385 E = AttrList.end(); I != E; ++I) {
386 if (I != AttrList.begin()) Str += " ";
387 Str += I->getAsString();
392 //===----------------------------------------------------------------------===//
393 // AttributeSetImpl Definition
394 //===----------------------------------------------------------------------===//
396 uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
397 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
398 if (getSlotIndex(I) != Index) continue;
399 const AttributeSetNode *ASN = AttrNodes[I].second;
402 for (AttributeSetNode::const_iterator II = ASN->begin(),
403 IE = ASN->end(); II != IE; ++II)
404 B.addAttributes(*II);
411 //===----------------------------------------------------------------------===//
412 // AttributeSet Construction and Mutation Methods
413 //===----------------------------------------------------------------------===//
416 AttributeSet::getImpl(LLVMContext &C,
417 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
418 LLVMContextImpl *pImpl = C.pImpl;
420 AttributeSetImpl::Profile(ID, Attrs);
423 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
425 // If we didn't find any existing attributes of the same shape then
426 // create a new one and insert it.
428 PA = new AttributeSetImpl(C, Attrs);
429 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
432 // Return the AttributesList that we found or created.
433 return AttributeSet(PA);
436 AttributeSet AttributeSet::get(LLVMContext &C,
437 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
438 // If there are no attributes then return a null AttributesList pointer.
440 return AttributeSet();
443 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
444 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
445 "Misordered Attributes list!");
446 assert(Attrs[i].second.hasAttributes() &&
447 "Pointless attribute!");
451 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
453 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
454 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
455 E = Attrs.end(); I != E; ) {
456 unsigned Index = I->first;
457 SmallVector<Attribute, 4> AttrVec;
458 while (I != E && I->first == Index) {
459 AttrVec.push_back(I->second);
463 AttrPairVec.push_back(std::make_pair(Index,
464 AttributeSetNode::get(C, AttrVec)));
467 return getImpl(C, AttrPairVec);
470 AttributeSet AttributeSet::get(LLVMContext &C,
471 ArrayRef<std::pair<unsigned,
472 AttributeSetNode*> > Attrs) {
473 // If there are no attributes then return a null AttributesList pointer.
475 return AttributeSet();
477 return getImpl(C, Attrs);
480 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
481 if (!B.hasAttributes())
482 return AttributeSet();
484 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
485 for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) {
486 Attribute::AttrKind Kind = *I;
487 if (Kind == Attribute::Alignment)
488 Attrs.push_back(std::make_pair(Idx, Attribute::
489 getWithAlignment(C, B.getAlignment())));
490 else if (Kind == Attribute::StackAlignment)
491 Attrs.push_back(std::make_pair(Idx, Attribute::
492 getWithStackAlignment(C, B.getStackAlignment())));
494 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind)));
497 return get(C, Attrs);
500 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
501 ArrayRef<Attribute::AttrKind> Kind) {
502 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
503 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
504 E = Kind.end(); I != E; ++I)
505 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
506 return get(C, Attrs);
509 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
510 if (Attrs.empty()) return AttributeSet();
512 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
513 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
514 AttributeSet AS = Attrs[I];
515 if (!AS.pImpl) continue;
516 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
519 return getImpl(C, AttrNodeVec);
522 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
523 Attribute::AttrKind Attr) const {
524 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
527 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
528 AttributeSet Attrs) const {
529 if (!pImpl) return Attrs;
530 if (!Attrs.pImpl) return *this;
533 // FIXME it is not obvious how this should work for alignment. For now, say
534 // we can't change a known alignment.
535 unsigned OldAlign = getParamAlignment(Idx);
536 unsigned NewAlign = Attrs.getParamAlignment(Idx);
537 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
538 "Attempt to change alignment!");
541 // Add the attribute slots before the one we're trying to add.
542 SmallVector<AttributeSet, 4> AttrSet;
543 uint64_t NumAttrs = pImpl->getNumAttributes();
545 uint64_t LastIndex = 0;
546 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
547 if (getSlotIndex(I) >= Idx) {
548 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
552 AttrSet.push_back(getSlotAttributes(I));
555 // Now add the attribute into the correct slot. There may already be an
556 // AttributeSet there.
557 AttrBuilder B(AS, Idx);
559 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
560 if (Attrs.getSlotIndex(I) == Idx) {
561 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
562 IE = Attrs.pImpl->end(I); II != IE; ++II)
563 B.addAttributes(*II);
567 AttrSet.push_back(AttributeSet::get(C, Idx, B));
569 // Add the remaining attribute slots.
570 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
571 AttrSet.push_back(getSlotAttributes(I));
573 return get(C, AttrSet);
576 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
577 Attribute::AttrKind Attr) const {
578 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
581 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
582 AttributeSet Attrs) const {
583 if (!pImpl) return AttributeSet();
584 if (!Attrs.pImpl) return *this;
587 // FIXME it is not obvious how this should work for alignment.
588 // For now, say we can't pass in alignment, which no current use does.
589 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
590 "Attempt to change alignment!");
593 // Add the attribute slots before the one we're trying to add.
594 SmallVector<AttributeSet, 4> AttrSet;
595 uint64_t NumAttrs = pImpl->getNumAttributes();
597 uint64_t LastIndex = 0;
598 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
599 if (getSlotIndex(I) >= Idx) {
600 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
604 AttrSet.push_back(getSlotAttributes(I));
607 // Now add the attribute into the correct slot. There may already be an
608 // AttributeSet there.
609 AttrBuilder B(AS, Idx);
611 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
612 if (Attrs.getSlotIndex(I) == Idx) {
613 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
614 IE = Attrs.pImpl->end(I); II != IE; ++II)
615 B.removeAttributes(*II);
619 AttrSet.push_back(AttributeSet::get(C, Idx, B));
621 // Add the remaining attribute slots.
622 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
623 AttrSet.push_back(getSlotAttributes(I));
625 return get(C, AttrSet);
628 //===----------------------------------------------------------------------===//
629 // AttributeSet Accessor Methods
630 //===----------------------------------------------------------------------===//
632 AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
633 return pImpl && hasAttributes(Idx) ?
634 AttributeSet::get(pImpl->getContext(),
635 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
636 std::make_pair(Idx, getAttributes(Idx)))) :
640 AttributeSet AttributeSet::getRetAttributes() const {
641 return pImpl && hasAttributes(ReturnIndex) ?
642 AttributeSet::get(pImpl->getContext(),
643 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
644 std::make_pair(ReturnIndex,
645 getAttributes(ReturnIndex)))) :
649 AttributeSet AttributeSet::getFnAttributes() const {
650 return pImpl && hasAttributes(FunctionIndex) ?
651 AttributeSet::get(pImpl->getContext(),
652 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
653 std::make_pair(FunctionIndex,
654 getAttributes(FunctionIndex)))) :
658 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
659 AttributeSetNode *ASN = getAttributes(Index);
660 return ASN ? ASN->hasAttribute(Kind) : false;
663 bool AttributeSet::hasAttributes(unsigned Index) const {
664 AttributeSetNode *ASN = getAttributes(Index);
665 return ASN ? ASN->hasAttributes() : false;
668 /// \brief Return true if the specified attribute is set for at least one
669 /// parameter or for the return value.
670 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
671 if (pImpl == 0) return false;
673 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
674 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
675 IE = pImpl->end(I); II != IE; ++II)
676 if (II->hasAttribute(Attr))
682 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
683 AttributeSetNode *ASN = getAttributes(Index);
684 return ASN ? ASN->getAlignment() : 0;
687 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
688 AttributeSetNode *ASN = getAttributes(Index);
689 return ASN ? ASN->getStackAlignment() : 0;
692 std::string AttributeSet::getAsString(unsigned Index) const {
693 AttributeSetNode *ASN = getAttributes(Index);
694 return ASN ? ASN->getAsString() : std::string("");
697 /// \brief The attributes for the specified index are returned.
698 AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
699 if (!pImpl) return 0;
701 // Loop through to find the attribute node we want.
702 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
703 if (pImpl->getSlotIndex(I) == Idx)
704 return pImpl->getSlotNode(I);
709 //===----------------------------------------------------------------------===//
710 // AttributeSet Introspection Methods
711 //===----------------------------------------------------------------------===//
713 /// \brief Return the number of slots used in this attribute list. This is the
714 /// number of arguments that have an attribute set on them (including the
715 /// function itself).
716 unsigned AttributeSet::getNumSlots() const {
717 return pImpl ? pImpl->getNumAttributes() : 0;
720 uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
721 assert(pImpl && Slot < pImpl->getNumAttributes() &&
722 "Slot # out of range!");
723 return pImpl->getSlotIndex(Slot);
726 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
727 assert(pImpl && Slot < pImpl->getNumAttributes() &&
728 "Slot # out of range!");
729 return pImpl->getSlotAttributes(Slot);
732 uint64_t AttributeSet::Raw(unsigned Index) const {
733 // FIXME: Remove this.
734 return pImpl ? pImpl->Raw(Index) : 0;
737 void AttributeSet::dump() const {
740 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
741 uint64_t Index = getSlotIndex(i);
747 dbgs() << " => " << getAsString(Index) << " }\n";
753 //===----------------------------------------------------------------------===//
754 // AttrBuilder Method Implementations
755 //===----------------------------------------------------------------------===//
757 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
758 : Alignment(0), StackAlignment(0) {
759 AttributeSetImpl *pImpl = AS.pImpl;
764 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
765 if (pImpl->getSlotIndex(I) != Idx) continue;
767 for (AttributeSetNode::const_iterator II = pImpl->begin(I),
768 IE = pImpl->end(I); II != IE; ++II)
769 B.addAttributes(*II);
774 if (!B.hasAttributes()) return;
776 uint64_t Mask = B.Raw();
778 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
779 I = Attribute::AttrKind(I + 1)) {
780 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
783 if (I == Attribute::Alignment)
784 Alignment = 1ULL << ((A >> 16) - 1);
785 else if (I == Attribute::StackAlignment)
786 StackAlignment = 1ULL << ((A >> 26)-1);
791 void AttrBuilder::clear() {
793 Alignment = StackAlignment = 0;
796 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
801 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
803 if (Val == Attribute::Alignment)
805 else if (Val == Attribute::StackAlignment)
811 AttrBuilder &AttrBuilder::addAttributes(Attribute Attr) {
812 uint64_t Mask = Attr.Raw();
814 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
815 I = Attribute::AttrKind(I + 1))
816 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
819 if (Attr.getAlignment())
820 Alignment = Attr.getAlignment();
821 if (Attr.getStackAlignment())
822 StackAlignment = Attr.getStackAlignment();
826 AttrBuilder &AttrBuilder::removeAttributes(Attribute A) {
827 uint64_t Mask = A.Raw();
829 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
830 I = Attribute::AttrKind(I + 1)) {
831 if (Mask & AttributeImpl::getAttrMask(I)) {
834 if (I == Attribute::Alignment)
836 else if (I == Attribute::StackAlignment)
844 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
845 if (Align == 0) return *this;
847 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
848 assert(Align <= 0x40000000 && "Alignment too large.");
850 Attrs.insert(Attribute::Alignment);
855 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
856 // Default alignment, allow the target to define how to align it.
857 if (Align == 0) return *this;
859 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
860 assert(Align <= 0x100 && "Alignment too large.");
862 Attrs.insert(Attribute::StackAlignment);
863 StackAlignment = Align;
867 bool AttrBuilder::contains(Attribute::AttrKind A) const {
868 return Attrs.count(A);
871 bool AttrBuilder::hasAttributes() const {
872 return !Attrs.empty();
875 bool AttrBuilder::hasAttributes(const Attribute &A) const {
876 return Raw() & A.Raw();
879 bool AttrBuilder::hasAlignmentAttr() const {
880 return Alignment != 0;
883 bool AttrBuilder::operator==(const AttrBuilder &B) {
884 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
885 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
889 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
890 if (!Val) return *this;
892 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
893 I = Attribute::AttrKind(I + 1)) {
894 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
897 if (I == Attribute::Alignment)
898 Alignment = 1ULL << ((A >> 16) - 1);
899 else if (I == Attribute::StackAlignment)
900 StackAlignment = 1ULL << ((A >> 26)-1);
907 uint64_t AttrBuilder::Raw() const {
910 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
911 E = Attrs.end(); I != E; ++I) {
912 Attribute::AttrKind Kind = *I;
914 if (Kind == Attribute::Alignment)
915 Mask |= (Log2_32(Alignment) + 1) << 16;
916 else if (Kind == Attribute::StackAlignment)
917 Mask |= (Log2_32(StackAlignment) + 1) << 26;
919 Mask |= AttributeImpl::getAttrMask(Kind);
925 //===----------------------------------------------------------------------===//
926 // AttributeFuncs Function Defintions
927 //===----------------------------------------------------------------------===//
929 Attribute AttributeFuncs::typeIncompatible(Type *Ty) {
930 AttrBuilder Incompatible;
932 if (!Ty->isIntegerTy())
933 // Attribute that only apply to integers.
934 Incompatible.addAttribute(Attribute::SExt)
935 .addAttribute(Attribute::ZExt);
937 if (!Ty->isPointerTy())
938 // Attribute that only apply to pointers.
939 Incompatible.addAttribute(Attribute::ByVal)
940 .addAttribute(Attribute::Nest)
941 .addAttribute(Attribute::NoAlias)
942 .addAttribute(Attribute::NoCapture)
943 .addAttribute(Attribute::StructRet);
945 return Attribute::get(Ty->getContext(), Incompatible);
948 /// \brief This returns an integer containing an encoding of all the LLVM
949 /// attributes found in the given attribute bitset. Any change to this encoding
950 /// is a breaking change to bitcode compatibility.
951 /// N.B. This should be used only by the bitcode reader!
952 uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
954 // FIXME: It doesn't make sense to store the alignment information as an
955 // expanded out value, we should store it as a log2 value. However, we can't
956 // just change that here without breaking bitcode compatibility. If this ever
957 // becomes a problem in practice, we should introduce new tag numbers in the
958 // bitcode file and have those tags use a more efficiently encoded alignment
961 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
962 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
963 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
964 if (Attrs.hasAttribute(Index, Attribute::Alignment))
965 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
966 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
970 /// \brief This fills an AttrBuilder object with the LLVM attributes that have
971 /// been decoded from the given integer. This function must stay in sync with
972 /// 'encodeLLVMAttributesForBitcode'.
973 /// N.B. This should be used only by the bitcode reader!
974 void AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
976 uint64_t EncodedAttrs) {
977 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
978 // the bits above 31 down by 11 bits.
979 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
980 assert((!Alignment || isPowerOf2_32(Alignment)) &&
981 "Alignment must be a power of two.");
984 B.addAlignmentAttr(Alignment);
985 B.addRawValue(((EncodedAttrs & (0xffffULL << 32)) >> 11) |
986 (EncodedAttrs & 0xffff));