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 if (Kind == None) return Attribute();
37 // Otherwise, build a key to look up the existing attributes.
38 LLVMContextImpl *pImpl = Context.pImpl;
44 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
47 // If we didn't find any existing attributes of the same shape then create a
48 // new one and insert it.
50 new AttributeImpl(Context, Kind) :
51 new AttributeImpl(Context, Kind, Val);
52 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
55 // Return the AttributesList that we found or created.
59 Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
60 return get(Context, Attribute::Alignment,
61 ConstantInt::get(Type::getInt64Ty(Context), Align));
64 Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
66 return get(Context, Attribute::StackAlignment,
67 ConstantInt::get(Type::getInt64Ty(Context), Align));
70 //===----------------------------------------------------------------------===//
71 // Attribute Accessor Methods
72 //===----------------------------------------------------------------------===//
74 bool Attribute::hasAttribute(AttrKind Val) const {
75 return pImpl && pImpl->hasAttribute(Val);
78 bool Attribute::hasAttributes() const {
79 return pImpl && pImpl->hasAttributes();
82 Constant *Attribute::getAttributeKind() const {
83 return pImpl ? pImpl->getAttributeKind() : 0;
86 ArrayRef<Constant*> Attribute::getAttributeValues() const {
87 return pImpl ? pImpl->getAttributeValues() : ArrayRef<Constant*>();
90 /// This returns the alignment field of an attribute as a byte alignment value.
91 unsigned Attribute::getAlignment() const {
92 if (!hasAttribute(Attribute::Alignment))
94 return pImpl->getAlignment();
97 /// This returns the stack alignment field of an attribute as a byte alignment
99 unsigned Attribute::getStackAlignment() const {
100 if (!hasAttribute(Attribute::StackAlignment))
102 return pImpl->getStackAlignment();
105 std::string Attribute::getAsString() const {
106 if (hasAttribute(Attribute::ZExt))
108 if (hasAttribute(Attribute::SExt))
110 if (hasAttribute(Attribute::NoReturn))
112 if (hasAttribute(Attribute::NoUnwind))
114 if (hasAttribute(Attribute::UWTable))
116 if (hasAttribute(Attribute::ReturnsTwice))
117 return "returns_twice";
118 if (hasAttribute(Attribute::InReg))
120 if (hasAttribute(Attribute::NoAlias))
122 if (hasAttribute(Attribute::NoCapture))
124 if (hasAttribute(Attribute::StructRet))
126 if (hasAttribute(Attribute::ByVal))
128 if (hasAttribute(Attribute::Nest))
130 if (hasAttribute(Attribute::ReadNone))
132 if (hasAttribute(Attribute::ReadOnly))
134 if (hasAttribute(Attribute::OptimizeForSize))
136 if (hasAttribute(Attribute::NoInline))
138 if (hasAttribute(Attribute::InlineHint))
140 if (hasAttribute(Attribute::AlwaysInline))
141 return "alwaysinline";
142 if (hasAttribute(Attribute::StackProtect))
144 if (hasAttribute(Attribute::StackProtectReq))
146 if (hasAttribute(Attribute::StackProtectStrong))
148 if (hasAttribute(Attribute::NoRedZone))
150 if (hasAttribute(Attribute::NoImplicitFloat))
151 return "noimplicitfloat";
152 if (hasAttribute(Attribute::Naked))
154 if (hasAttribute(Attribute::NonLazyBind))
155 return "nonlazybind";
156 if (hasAttribute(Attribute::AddressSafety))
157 return "address_safety";
158 if (hasAttribute(Attribute::MinSize))
160 if (hasAttribute(Attribute::StackAlignment)) {
162 Result += "alignstack(";
163 Result += utostr(getStackAlignment());
167 if (hasAttribute(Attribute::Alignment)) {
170 Result += utostr(getAlignment());
174 if (hasAttribute(Attribute::NoDuplicate))
175 return "noduplicate";
177 llvm_unreachable("Unknown attribute");
180 bool Attribute::operator==(AttrKind K) const {
181 return pImpl && *pImpl == K;
183 bool Attribute::operator!=(AttrKind K) const {
184 return !(*this == K);
187 bool Attribute::operator<(Attribute A) const {
188 if (!pImpl && !A.pImpl) return false;
189 if (!pImpl) return true;
190 if (!A.pImpl) return false;
191 return *pImpl < *A.pImpl;
194 uint64_t Attribute::Raw() const {
195 return pImpl ? pImpl->Raw() : 0;
198 //===----------------------------------------------------------------------===//
199 // AttributeImpl Definition
200 //===----------------------------------------------------------------------===//
202 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind kind)
204 Kind = ConstantInt::get(Type::getInt64Ty(C), kind);
206 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind kind,
207 ArrayRef<Constant*> values)
209 Kind = ConstantInt::get(Type::getInt64Ty(C), kind);
210 Vals.reserve(values.size());
211 Vals.append(values.begin(), values.end());
213 AttributeImpl::AttributeImpl(LLVMContext &C, StringRef kind)
215 Kind = ConstantDataArray::getString(C, kind);
218 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
219 return (Raw() & getAttrMask(A)) != 0;
222 bool AttributeImpl::hasAttributes() const {
226 uint64_t AttributeImpl::getAlignment() const {
227 uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment);
228 return 1ULL << ((Mask >> 16) - 1);
231 uint64_t AttributeImpl::getStackAlignment() const {
232 uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment);
233 return 1ULL << ((Mask >> 26) - 1);
236 bool AttributeImpl::operator==(Attribute::AttrKind kind) const {
237 if (ConstantInt *CI = dyn_cast<ConstantInt>(Kind))
238 return CI->getZExtValue() == kind;
241 bool AttributeImpl::operator!=(Attribute::AttrKind kind) const {
242 return !(*this == kind);
245 bool AttributeImpl::operator==(StringRef kind) const {
246 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Kind))
248 return CDA->getAsString() == kind;
252 bool AttributeImpl::operator!=(StringRef kind) const {
253 return !(*this == kind);
256 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
257 if (!Kind && !AI.Kind) return false;
258 if (!Kind && AI.Kind) return true;
259 if (Kind && !AI.Kind) return false;
261 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Kind);
262 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Kind);
264 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Kind);
265 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Kind);
267 if (ThisCI && ThatCI)
268 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
270 if (ThisCI && ThatCDA)
273 if (ThisCDA && ThatCI)
276 return ThisCDA->getAsString() < ThatCDA->getAsString();
279 uint64_t AttributeImpl::Raw() const {
280 // FIXME: Remove this.
281 return cast<ConstantInt>(Kind)->getZExtValue();
284 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
285 // FIXME: Remove this.
287 case Attribute::EndAttrKinds:
288 case Attribute::AttrKindEmptyKey:
289 case Attribute::AttrKindTombstoneKey:
290 llvm_unreachable("Synthetic enumerators which should never get here");
292 case Attribute::None: return 0;
293 case Attribute::ZExt: return 1 << 0;
294 case Attribute::SExt: return 1 << 1;
295 case Attribute::NoReturn: return 1 << 2;
296 case Attribute::InReg: return 1 << 3;
297 case Attribute::StructRet: return 1 << 4;
298 case Attribute::NoUnwind: return 1 << 5;
299 case Attribute::NoAlias: return 1 << 6;
300 case Attribute::ByVal: return 1 << 7;
301 case Attribute::Nest: return 1 << 8;
302 case Attribute::ReadNone: return 1 << 9;
303 case Attribute::ReadOnly: return 1 << 10;
304 case Attribute::NoInline: return 1 << 11;
305 case Attribute::AlwaysInline: return 1 << 12;
306 case Attribute::OptimizeForSize: return 1 << 13;
307 case Attribute::StackProtect: return 1 << 14;
308 case Attribute::StackProtectReq: return 1 << 15;
309 case Attribute::Alignment: return 31 << 16;
310 case Attribute::NoCapture: return 1 << 21;
311 case Attribute::NoRedZone: return 1 << 22;
312 case Attribute::NoImplicitFloat: return 1 << 23;
313 case Attribute::Naked: return 1 << 24;
314 case Attribute::InlineHint: return 1 << 25;
315 case Attribute::StackAlignment: return 7 << 26;
316 case Attribute::ReturnsTwice: return 1 << 29;
317 case Attribute::UWTable: return 1 << 30;
318 case Attribute::NonLazyBind: return 1U << 31;
319 case Attribute::AddressSafety: return 1ULL << 32;
320 case Attribute::MinSize: return 1ULL << 33;
321 case Attribute::NoDuplicate: return 1ULL << 34;
322 case Attribute::StackProtectStrong: return 1ULL << 35;
324 llvm_unreachable("Unsupported attribute type");
327 //===----------------------------------------------------------------------===//
328 // AttributeSetNode Definition
329 //===----------------------------------------------------------------------===//
331 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
332 ArrayRef<Attribute> Attrs) {
336 // Otherwise, build a key to look up the existing attributes.
337 LLVMContextImpl *pImpl = C.pImpl;
340 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
341 std::sort(SortedAttrs.begin(), SortedAttrs.end());
343 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
344 E = SortedAttrs.end(); I != E; ++I)
348 AttributeSetNode *PA =
349 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
351 // If we didn't find any existing attributes of the same shape then create a
352 // new one and insert it.
354 PA = new AttributeSetNode(SortedAttrs);
355 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
358 // Return the AttributesListNode that we found or created.
362 bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
363 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
364 E = AttrList.end(); I != E; ++I)
365 if (I->hasAttribute(Kind))
370 unsigned AttributeSetNode::getAlignment() const {
371 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
372 E = AttrList.end(); I != E; ++I)
373 if (I->hasAttribute(Attribute::Alignment))
374 return I->getAlignment();
378 unsigned AttributeSetNode::getStackAlignment() const {
379 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
380 E = AttrList.end(); I != E; ++I)
381 if (I->hasAttribute(Attribute::StackAlignment))
382 return I->getStackAlignment();
386 std::string AttributeSetNode::getAsString() const {
387 std::string Str = "";
388 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
389 E = AttrList.end(); I != E; ++I) {
390 if (I != AttrList.begin()) Str += " ";
391 Str += I->getAsString();
396 //===----------------------------------------------------------------------===//
397 // AttributeSetImpl Definition
398 //===----------------------------------------------------------------------===//
400 uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
401 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
402 if (getSlotIndex(I) != Index) continue;
403 const AttributeSetNode *ASN = AttrNodes[I].second;
406 for (AttributeSetNode::const_iterator II = ASN->begin(),
407 IE = ASN->end(); II != IE; ++II)
408 B.addAttributes(*II);
415 //===----------------------------------------------------------------------===//
416 // AttributeSet Construction and Mutation Methods
417 //===----------------------------------------------------------------------===//
420 AttributeSet::getImpl(LLVMContext &C,
421 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
422 LLVMContextImpl *pImpl = C.pImpl;
424 AttributeSetImpl::Profile(ID, Attrs);
427 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
429 // If we didn't find any existing attributes of the same shape then
430 // create a new one and insert it.
432 PA = new AttributeSetImpl(C, Attrs);
433 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
436 // Return the AttributesList that we found or created.
437 return AttributeSet(PA);
440 AttributeSet AttributeSet::get(LLVMContext &C,
441 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
442 // If there are no attributes then return a null AttributesList pointer.
444 return AttributeSet();
447 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
448 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
449 "Misordered Attributes list!");
450 assert(Attrs[i].second.hasAttributes() &&
451 "Pointless attribute!");
455 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
457 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
458 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
459 E = Attrs.end(); I != E; ) {
460 unsigned Index = I->first;
461 SmallVector<Attribute, 4> AttrVec;
462 while (I != E && I->first == Index) {
463 AttrVec.push_back(I->second);
467 AttrPairVec.push_back(std::make_pair(Index,
468 AttributeSetNode::get(C, AttrVec)));
471 return getImpl(C, AttrPairVec);
474 AttributeSet AttributeSet::get(LLVMContext &C,
475 ArrayRef<std::pair<unsigned,
476 AttributeSetNode*> > Attrs) {
477 // If there are no attributes then return a null AttributesList pointer.
479 return AttributeSet();
481 return getImpl(C, Attrs);
484 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
485 if (!B.hasAttributes())
486 return AttributeSet();
488 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
489 for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) {
490 Attribute::AttrKind Kind = *I;
491 if (Kind == Attribute::Alignment)
492 Attrs.push_back(std::make_pair(Idx, Attribute::
493 getWithAlignment(C, B.getAlignment())));
494 else if (Kind == Attribute::StackAlignment)
495 Attrs.push_back(std::make_pair(Idx, Attribute::
496 getWithStackAlignment(C, B.getStackAlignment())));
498 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind)));
501 return get(C, Attrs);
504 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
505 ArrayRef<Attribute::AttrKind> Kind) {
506 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
507 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
508 E = Kind.end(); I != E; ++I)
509 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
510 return get(C, Attrs);
513 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
514 if (Attrs.empty()) return AttributeSet();
516 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
517 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
518 AttributeSet AS = Attrs[I];
519 if (!AS.pImpl) continue;
520 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
523 return getImpl(C, AttrNodeVec);
526 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
527 Attribute::AttrKind Attr) const {
528 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
531 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
532 AttributeSet Attrs) const {
533 if (!pImpl) return Attrs;
534 if (!Attrs.pImpl) return *this;
537 // FIXME it is not obvious how this should work for alignment. For now, say
538 // we can't change a known alignment.
539 unsigned OldAlign = getParamAlignment(Idx);
540 unsigned NewAlign = Attrs.getParamAlignment(Idx);
541 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
542 "Attempt to change alignment!");
545 // Add the attribute slots before the one we're trying to add.
546 SmallVector<AttributeSet, 4> AttrSet;
547 uint64_t NumAttrs = pImpl->getNumAttributes();
549 uint64_t LastIndex = 0;
550 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
551 if (getSlotIndex(I) >= Idx) {
552 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
556 AttrSet.push_back(getSlotAttributes(I));
559 // Now add the attribute into the correct slot. There may already be an
560 // AttributeSet there.
561 AttrBuilder B(AS, Idx);
563 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
564 if (Attrs.getSlotIndex(I) == Idx) {
565 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
566 IE = Attrs.pImpl->end(I); II != IE; ++II)
567 B.addAttributes(*II);
571 AttrSet.push_back(AttributeSet::get(C, Idx, B));
573 // Add the remaining attribute slots.
574 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
575 AttrSet.push_back(getSlotAttributes(I));
577 return get(C, AttrSet);
580 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
581 Attribute::AttrKind Attr) const {
582 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
585 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
586 AttributeSet Attrs) const {
587 if (!pImpl) return AttributeSet();
588 if (!Attrs.pImpl) return *this;
591 // FIXME it is not obvious how this should work for alignment.
592 // For now, say we can't pass in alignment, which no current use does.
593 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
594 "Attempt to change alignment!");
597 // Add the attribute slots before the one we're trying to add.
598 SmallVector<AttributeSet, 4> AttrSet;
599 uint64_t NumAttrs = pImpl->getNumAttributes();
601 uint64_t LastIndex = 0;
602 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
603 if (getSlotIndex(I) >= Idx) {
604 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
608 AttrSet.push_back(getSlotAttributes(I));
611 // Now remove the attribute from the correct slot. There may already be an
612 // AttributeSet there.
613 AttrBuilder B(AS, Idx);
615 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
616 if (Attrs.getSlotIndex(I) == Idx) {
617 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Idx);
621 AttrSet.push_back(AttributeSet::get(C, Idx, B));
623 // Add the remaining attribute slots.
624 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
625 AttrSet.push_back(getSlotAttributes(I));
627 return get(C, AttrSet);
630 //===----------------------------------------------------------------------===//
631 // AttributeSet Accessor Methods
632 //===----------------------------------------------------------------------===//
634 AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
635 return pImpl && hasAttributes(Idx) ?
636 AttributeSet::get(pImpl->getContext(),
637 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
638 std::make_pair(Idx, getAttributes(Idx)))) :
642 AttributeSet AttributeSet::getRetAttributes() const {
643 return pImpl && hasAttributes(ReturnIndex) ?
644 AttributeSet::get(pImpl->getContext(),
645 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
646 std::make_pair(ReturnIndex,
647 getAttributes(ReturnIndex)))) :
651 AttributeSet AttributeSet::getFnAttributes() const {
652 return pImpl && hasAttributes(FunctionIndex) ?
653 AttributeSet::get(pImpl->getContext(),
654 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
655 std::make_pair(FunctionIndex,
656 getAttributes(FunctionIndex)))) :
660 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
661 AttributeSetNode *ASN = getAttributes(Index);
662 return ASN ? ASN->hasAttribute(Kind) : false;
665 bool AttributeSet::hasAttributes(unsigned Index) const {
666 AttributeSetNode *ASN = getAttributes(Index);
667 return ASN ? ASN->hasAttributes() : false;
670 /// \brief Return true if the specified attribute is set for at least one
671 /// parameter or for the return value.
672 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
673 if (pImpl == 0) return false;
675 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
676 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
677 IE = pImpl->end(I); II != IE; ++II)
678 if (II->hasAttribute(Attr))
684 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
685 AttributeSetNode *ASN = getAttributes(Index);
686 return ASN ? ASN->getAlignment() : 0;
689 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
690 AttributeSetNode *ASN = getAttributes(Index);
691 return ASN ? ASN->getStackAlignment() : 0;
694 std::string AttributeSet::getAsString(unsigned Index) const {
695 AttributeSetNode *ASN = getAttributes(Index);
696 return ASN ? ASN->getAsString() : std::string("");
699 /// \brief The attributes for the specified index are returned.
700 AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
701 if (!pImpl) return 0;
703 // Loop through to find the attribute node we want.
704 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
705 if (pImpl->getSlotIndex(I) == Idx)
706 return pImpl->getSlotNode(I);
711 //===----------------------------------------------------------------------===//
712 // AttributeSet Introspection Methods
713 //===----------------------------------------------------------------------===//
715 /// \brief Return the number of slots used in this attribute list. This is the
716 /// number of arguments that have an attribute set on them (including the
717 /// function itself).
718 unsigned AttributeSet::getNumSlots() const {
719 return pImpl ? pImpl->getNumAttributes() : 0;
722 uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
723 assert(pImpl && Slot < pImpl->getNumAttributes() &&
724 "Slot # out of range!");
725 return pImpl->getSlotIndex(Slot);
728 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
729 assert(pImpl && Slot < pImpl->getNumAttributes() &&
730 "Slot # out of range!");
731 return pImpl->getSlotAttributes(Slot);
734 uint64_t AttributeSet::Raw(unsigned Index) const {
735 // FIXME: Remove this.
736 return pImpl ? pImpl->Raw(Index) : 0;
739 void AttributeSet::dump() const {
742 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
743 uint64_t Index = getSlotIndex(i);
749 dbgs() << " => " << getAsString(Index) << " }\n";
755 //===----------------------------------------------------------------------===//
756 // AttrBuilder Method Implementations
757 //===----------------------------------------------------------------------===//
759 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
760 : Alignment(0), StackAlignment(0) {
761 AttributeSetImpl *pImpl = AS.pImpl;
764 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
765 if (pImpl->getSlotIndex(I) != Idx) continue;
767 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
768 IE = pImpl->end(I); II != IE; ++II)
775 void AttrBuilder::clear() {
777 Alignment = StackAlignment = 0;
780 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
785 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
787 if (Val == Attribute::Alignment)
789 else if (Val == Attribute::StackAlignment)
795 AttrBuilder &AttrBuilder::addAttributes(Attribute Attr) {
796 uint64_t Mask = Attr.Raw();
798 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
799 I = Attribute::AttrKind(I + 1))
800 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
803 if (Attr.getAlignment())
804 Alignment = Attr.getAlignment();
805 if (Attr.getStackAlignment())
806 StackAlignment = Attr.getStackAlignment();
810 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
811 uint64_t Mask = A.Raw(Index);
813 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
814 I = Attribute::AttrKind(I + 1)) {
815 if (Mask & AttributeImpl::getAttrMask(I)) {
818 if (I == Attribute::Alignment)
820 else if (I == Attribute::StackAlignment)
828 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
829 if (Align == 0) return *this;
831 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
832 assert(Align <= 0x40000000 && "Alignment too large.");
834 Attrs.insert(Attribute::Alignment);
839 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
840 // Default alignment, allow the target to define how to align it.
841 if (Align == 0) return *this;
843 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
844 assert(Align <= 0x100 && "Alignment too large.");
846 Attrs.insert(Attribute::StackAlignment);
847 StackAlignment = Align;
851 bool AttrBuilder::contains(Attribute::AttrKind A) const {
852 return Attrs.count(A);
855 bool AttrBuilder::hasAttributes() const {
856 return !Attrs.empty();
859 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
860 return Raw() & A.Raw(Index);
863 bool AttrBuilder::hasAlignmentAttr() const {
864 return Alignment != 0;
867 bool AttrBuilder::operator==(const AttrBuilder &B) {
868 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
869 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
873 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
874 if (!Val) return *this;
876 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
877 I = Attribute::AttrKind(I + 1)) {
878 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
881 if (I == Attribute::Alignment)
882 Alignment = 1ULL << ((A >> 16) - 1);
883 else if (I == Attribute::StackAlignment)
884 StackAlignment = 1ULL << ((A >> 26)-1);
891 uint64_t AttrBuilder::Raw() const {
894 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
895 E = Attrs.end(); I != E; ++I) {
896 Attribute::AttrKind Kind = *I;
898 if (Kind == Attribute::Alignment)
899 Mask |= (Log2_32(Alignment) + 1) << 16;
900 else if (Kind == Attribute::StackAlignment)
901 Mask |= (Log2_32(StackAlignment) + 1) << 26;
903 Mask |= AttributeImpl::getAttrMask(Kind);
909 //===----------------------------------------------------------------------===//
910 // AttributeFuncs Function Defintions
911 //===----------------------------------------------------------------------===//
913 AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
914 AttrBuilder Incompatible;
916 if (!Ty->isIntegerTy())
917 // Attribute that only apply to integers.
918 Incompatible.addAttribute(Attribute::SExt)
919 .addAttribute(Attribute::ZExt);
921 if (!Ty->isPointerTy())
922 // Attribute that only apply to pointers.
923 Incompatible.addAttribute(Attribute::ByVal)
924 .addAttribute(Attribute::Nest)
925 .addAttribute(Attribute::NoAlias)
926 .addAttribute(Attribute::NoCapture)
927 .addAttribute(Attribute::StructRet);
929 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
932 /// \brief This returns an integer containing an encoding of all the LLVM
933 /// attributes found in the given attribute bitset. Any change to this encoding
934 /// is a breaking change to bitcode compatibility.
935 /// N.B. This should be used only by the bitcode reader!
936 uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
938 // FIXME: It doesn't make sense to store the alignment information as an
939 // expanded out value, we should store it as a log2 value. However, we can't
940 // just change that here without breaking bitcode compatibility. If this ever
941 // becomes a problem in practice, we should introduce new tag numbers in the
942 // bitcode file and have those tags use a more efficiently encoded alignment
945 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
946 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
947 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
948 if (Attrs.hasAttribute(Index, Attribute::Alignment))
949 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
950 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
954 /// \brief This fills an AttrBuilder object with the LLVM attributes that have
955 /// been decoded from the given integer. This function must stay in sync with
956 /// 'encodeLLVMAttributesForBitcode'.
957 /// N.B. This should be used only by the bitcode reader!
958 void AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
960 uint64_t EncodedAttrs) {
961 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
962 // the bits above 31 down by 11 bits.
963 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
964 assert((!Alignment || isPowerOf2_32(Alignment)) &&
965 "Alignment must be a power of two.");
968 B.addAlignmentAttr(Alignment);
969 B.addRawValue(((EncodedAttrs & (0xffffULL << 32)) >> 11) |
970 (EncodedAttrs & 0xffff));