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/FoldingSet.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/IR/Type.h"
22 #include "llvm/Support/Atomic.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ManagedStatic.h"
25 #include "llvm/Support/Mutex.h"
26 #include "llvm/Support/raw_ostream.h"
30 //===----------------------------------------------------------------------===//
31 // Attribute Implementation
32 //===----------------------------------------------------------------------===//
34 Attribute Attribute::get(LLVMContext &Context, AttrKind Kind) {
36 return Attribute::get(Context, B.addAttribute(Kind));
39 Attribute Attribute::get(LLVMContext &Context, AttrBuilder &B) {
40 // If there are no attributes, return an empty Attribute class.
41 if (!B.hasAttributes())
44 // Otherwise, build a key to look up the existing attributes.
45 LLVMContextImpl *pImpl = Context.pImpl;
47 ConstantInt *CI = ConstantInt::get(Type::getInt64Ty(Context), 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, CI);
57 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
60 // Return the AttributesList that we found or created.
64 Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
66 return get(Context, B.addAlignmentAttr(Align));
69 Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
72 return get(Context, B.addStackAlignmentAttr(Align));
75 bool Attribute::hasAttribute(AttrKind Val) const {
76 return pImpl && pImpl->hasAttribute(Val);
79 bool Attribute::hasAttributes() const {
80 return pImpl && pImpl->hasAttributes();
83 /// This returns the alignment field of an attribute as a byte alignment value.
84 unsigned Attribute::getAlignment() const {
85 if (!hasAttribute(Attribute::Alignment))
87 return pImpl->getAlignment();
90 /// This returns the stack alignment field of an attribute as a byte alignment
92 unsigned Attribute::getStackAlignment() const {
93 if (!hasAttribute(Attribute::StackAlignment))
95 return pImpl->getStackAlignment();
98 std::string Attribute::getAsString() const {
100 if (hasAttribute(Attribute::ZExt))
101 Result += "zeroext ";
102 if (hasAttribute(Attribute::SExt))
103 Result += "signext ";
104 if (hasAttribute(Attribute::NoReturn))
105 Result += "noreturn ";
106 if (hasAttribute(Attribute::NoUnwind))
107 Result += "nounwind ";
108 if (hasAttribute(Attribute::UWTable))
109 Result += "uwtable ";
110 if (hasAttribute(Attribute::ReturnsTwice))
111 Result += "returns_twice ";
112 if (hasAttribute(Attribute::InReg))
114 if (hasAttribute(Attribute::NoAlias))
115 Result += "noalias ";
116 if (hasAttribute(Attribute::NoCapture))
117 Result += "nocapture ";
118 if (hasAttribute(Attribute::StructRet))
120 if (hasAttribute(Attribute::ByVal))
122 if (hasAttribute(Attribute::Nest))
124 if (hasAttribute(Attribute::ReadNone))
125 Result += "readnone ";
126 if (hasAttribute(Attribute::ReadOnly))
127 Result += "readonly ";
128 if (hasAttribute(Attribute::OptimizeForSize))
129 Result += "optsize ";
130 if (hasAttribute(Attribute::NoInline))
131 Result += "noinline ";
132 if (hasAttribute(Attribute::InlineHint))
133 Result += "inlinehint ";
134 if (hasAttribute(Attribute::AlwaysInline))
135 Result += "alwaysinline ";
136 if (hasAttribute(Attribute::StackProtect))
138 if (hasAttribute(Attribute::StackProtectReq))
140 if (hasAttribute(Attribute::StackProtectStrong))
141 Result += "sspstrong ";
142 if (hasAttribute(Attribute::NoRedZone))
143 Result += "noredzone ";
144 if (hasAttribute(Attribute::NoImplicitFloat))
145 Result += "noimplicitfloat ";
146 if (hasAttribute(Attribute::Naked))
148 if (hasAttribute(Attribute::NonLazyBind))
149 Result += "nonlazybind ";
150 if (hasAttribute(Attribute::AddressSafety))
151 Result += "address_safety ";
152 if (hasAttribute(Attribute::MinSize))
153 Result += "minsize ";
154 if (hasAttribute(Attribute::StackAlignment)) {
155 Result += "alignstack(";
156 Result += utostr(getStackAlignment());
159 if (hasAttribute(Attribute::Alignment)) {
161 Result += utostr(getAlignment());
164 if (hasAttribute(Attribute::NoDuplicate))
165 Result += "noduplicate ";
166 // Trim the trailing space.
167 assert(!Result.empty() && "Unknown attribute!");
168 Result.erase(Result.end()-1);
172 bool Attribute::operator==(AttrKind K) const {
173 return pImpl && *pImpl == K;
175 bool Attribute::operator!=(AttrKind K) const {
176 return !(*this == K);
179 bool Attribute::operator<(Attribute A) const {
180 if (!pImpl && !A.pImpl) return false;
181 if (!pImpl) return true;
182 if (!A.pImpl) return false;
183 return *pImpl < *A.pImpl;
186 uint64_t Attribute::Raw() const {
187 return pImpl ? pImpl->Raw() : 0;
190 //===----------------------------------------------------------------------===//
191 // AttributeImpl Definition
192 //===----------------------------------------------------------------------===//
194 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data)
196 Data = ConstantInt::get(Type::getInt64Ty(C), data);
198 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
199 ArrayRef<Constant*> values)
201 Data = ConstantInt::get(Type::getInt64Ty(C), data);
202 Vals.reserve(values.size());
203 Vals.append(values.begin(), values.end());
205 AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data)
207 Data = ConstantDataArray::getString(C, data);
210 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
211 return (Raw() & getAttrMask(A)) != 0;
214 bool AttributeImpl::hasAttributes() const {
218 uint64_t AttributeImpl::getAlignment() const {
219 uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment);
220 return 1ULL << ((Mask >> 16) - 1);
223 uint64_t AttributeImpl::getStackAlignment() const {
224 uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment);
225 return 1ULL << ((Mask >> 26) - 1);
228 bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
229 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
230 return CI->getZExtValue() == Kind;
233 bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
234 return !(*this == Kind);
237 bool AttributeImpl::operator==(StringRef Kind) const {
238 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
240 return CDA->getAsString() == Kind;
244 bool AttributeImpl::operator!=(StringRef Kind) const {
245 return !(*this == Kind);
248 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
249 if (!Data && !AI.Data) return false;
250 if (!Data && AI.Data) return true;
251 if (Data && !AI.Data) return false;
253 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Data);
254 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Data);
256 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Data);
257 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Data);
259 if (ThisCI && ThatCI)
260 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
262 if (ThisCI && ThatCDA)
265 if (ThisCDA && ThatCI)
268 return ThisCDA->getAsString() < ThatCDA->getAsString();
271 uint64_t AttributeImpl::Raw() const {
272 // FIXME: Remove this.
273 return cast<ConstantInt>(Data)->getZExtValue();
276 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
277 // FIXME: Remove this.
279 case Attribute::EndAttrKinds:
280 case Attribute::AttrKindEmptyKey:
281 case Attribute::AttrKindTombstoneKey:
282 llvm_unreachable("Synthetic enumerators which should never get here");
284 case Attribute::None: return 0;
285 case Attribute::ZExt: return 1 << 0;
286 case Attribute::SExt: return 1 << 1;
287 case Attribute::NoReturn: return 1 << 2;
288 case Attribute::InReg: return 1 << 3;
289 case Attribute::StructRet: return 1 << 4;
290 case Attribute::NoUnwind: return 1 << 5;
291 case Attribute::NoAlias: return 1 << 6;
292 case Attribute::ByVal: return 1 << 7;
293 case Attribute::Nest: return 1 << 8;
294 case Attribute::ReadNone: return 1 << 9;
295 case Attribute::ReadOnly: return 1 << 10;
296 case Attribute::NoInline: return 1 << 11;
297 case Attribute::AlwaysInline: return 1 << 12;
298 case Attribute::OptimizeForSize: return 1 << 13;
299 case Attribute::StackProtect: return 1 << 14;
300 case Attribute::StackProtectReq: return 1 << 15;
301 case Attribute::Alignment: return 31 << 16;
302 case Attribute::NoCapture: return 1 << 21;
303 case Attribute::NoRedZone: return 1 << 22;
304 case Attribute::NoImplicitFloat: return 1 << 23;
305 case Attribute::Naked: return 1 << 24;
306 case Attribute::InlineHint: return 1 << 25;
307 case Attribute::StackAlignment: return 7 << 26;
308 case Attribute::ReturnsTwice: return 1 << 29;
309 case Attribute::UWTable: return 1 << 30;
310 case Attribute::NonLazyBind: return 1U << 31;
311 case Attribute::AddressSafety: return 1ULL << 32;
312 case Attribute::MinSize: return 1ULL << 33;
313 case Attribute::NoDuplicate: return 1ULL << 34;
314 case Attribute::StackProtectStrong: return 1ULL << 35;
316 llvm_unreachable("Unsupported attribute type");
319 //===----------------------------------------------------------------------===//
320 // AttributeSetNode Definition
321 //===----------------------------------------------------------------------===//
323 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
324 ArrayRef<Attribute> Attrs) {
328 // Otherwise, build a key to look up the existing attributes.
329 LLVMContextImpl *pImpl = C.pImpl;
332 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
333 std::sort(SortedAttrs.begin(), SortedAttrs.end());
335 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
336 E = SortedAttrs.end(); I != E; ++I)
340 AttributeSetNode *PA =
341 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
343 // If we didn't find any existing attributes of the same shape then create a
344 // new one and insert it.
346 PA = new AttributeSetNode(SortedAttrs);
347 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
350 // Return the AttributesListNode that we found or created.
354 //===----------------------------------------------------------------------===//
355 // AttributeSetImpl Definition
356 //===----------------------------------------------------------------------===//
358 uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
359 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
360 if (getSlotIndex(I) != Index) continue;
361 const AttributeSetNode *ASN = AttrNodes[I].second;
364 for (AttributeSetNode::const_iterator II = ASN->begin(),
365 IE = ASN->end(); II != IE; ++II)
366 B.addAttributes(*II);
373 //===----------------------------------------------------------------------===//
374 // AttributeSet Construction and Mutation Methods
375 //===----------------------------------------------------------------------===//
377 AttributeSet AttributeSet::getImpl(LLVMContext &C,
378 ArrayRef<std::pair<unsigned,
379 AttributeSetNode*> > Attrs) {
380 LLVMContextImpl *pImpl = C.pImpl;
382 AttributeSetImpl::Profile(ID, Attrs);
385 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
387 // If we didn't find any existing attributes of the same shape then
388 // create a new one and insert it.
390 PA = new AttributeSetImpl(C, Attrs);
391 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
394 // Return the AttributesList that we found or created.
395 return AttributeSet(PA);
398 AttributeSet AttributeSet::get(LLVMContext &C,
399 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
400 // If there are no attributes then return a null AttributesList pointer.
402 return AttributeSet();
405 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
406 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
407 "Misordered Attributes list!");
408 assert(Attrs[i].second.hasAttributes() &&
409 "Pointless attribute!");
413 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
415 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
416 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
417 E = Attrs.end(); I != E; ) {
418 unsigned Index = I->first;
419 SmallVector<Attribute, 4> AttrVec;
420 while (I->first == Index && I != E) {
421 AttrVec.push_back(I->second);
425 AttrPairVec.push_back(std::make_pair(Index,
426 AttributeSetNode::get(C, AttrVec)));
429 return getImpl(C, AttrPairVec);
432 AttributeSet AttributeSet::get(LLVMContext &C,
433 ArrayRef<std::pair<unsigned,
434 AttributeSetNode*> > Attrs) {
435 // If there are no attributes then return a null AttributesList pointer.
437 return AttributeSet();
439 return getImpl(C, Attrs);
442 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
443 if (!B.hasAttributes())
444 return AttributeSet();
445 return get(C, ArrayRef<std::pair<unsigned, Attribute> >(
446 std::make_pair(Idx, Attribute::get(C, B))));
449 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
450 ArrayRef<Attribute::AttrKind> Kind) {
451 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
452 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
453 E = Kind.end(); I != E; ++I)
454 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
455 return get(C, Attrs);
458 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
459 if (Attrs.empty()) return AttributeSet();
461 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
462 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
463 AttributeSet AS = Attrs[I];
464 if (!AS.pImpl) continue;
465 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
468 return getImpl(C, AttrNodeVec);
471 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
472 Attribute::AttrKind Attr) const {
473 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
476 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
477 AttributeSet Attrs) const {
478 if (!pImpl) return Attrs;
479 if (!Attrs.pImpl) return *this;
482 // FIXME it is not obvious how this should work for alignment. For now, say
483 // we can't change a known alignment.
484 unsigned OldAlign = getParamAlignment(Idx);
485 unsigned NewAlign = Attrs.getParamAlignment(Idx);
486 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
487 "Attempt to change alignment!");
490 // Add the attribute slots before the one we're trying to add.
491 SmallVector<AttributeSet, 4> AttrSet;
492 uint64_t NumAttrs = pImpl->getNumAttributes();
494 uint64_t LastIndex = 0;
495 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
496 if (getSlotIndex(I) >= Idx) {
497 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
501 AttrSet.push_back(getSlotAttributes(I));
504 // Now add the attribute into the correct slot. There may already be an
505 // AttributeSet there.
506 AttrBuilder B(AS, Idx);
508 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
509 if (Attrs.getSlotIndex(I) == Idx) {
510 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
511 IE = Attrs.pImpl->end(I); II != IE; ++II)
512 B.addAttributes(*II);
516 AttrSet.push_back(AttributeSet::get(C, Idx, B));
518 // Add the remaining attribute slots.
519 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
520 AttrSet.push_back(getSlotAttributes(I));
522 return get(C, AttrSet);
525 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
526 Attribute::AttrKind Attr) const {
527 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
530 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
531 AttributeSet Attrs) const {
532 if (!pImpl) return AttributeSet();
533 if (!Attrs.pImpl) return *this;
536 // FIXME it is not obvious how this should work for alignment.
537 // For now, say we can't pass in alignment, which no current use does.
538 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
539 "Attempt to change alignment!");
542 // Add the attribute slots before the one we're trying to add.
543 SmallVector<AttributeSet, 4> AttrSet;
544 uint64_t NumAttrs = pImpl->getNumAttributes();
546 uint64_t LastIndex = 0;
547 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
548 if (getSlotIndex(I) >= Idx) {
549 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
553 AttrSet.push_back(getSlotAttributes(I));
556 // Now add the attribute into the correct slot. There may already be an
557 // AttributeSet there.
558 AttrBuilder B(AS, Idx);
560 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
561 if (Attrs.getSlotIndex(I) == Idx) {
562 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
563 IE = Attrs.pImpl->end(I); II != IE; ++II)
564 B.removeAttributes(*II);
568 AttrSet.push_back(AttributeSet::get(C, Idx, B));
570 // Add the remaining attribute slots.
571 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
572 AttrSet.push_back(getSlotAttributes(I));
574 return get(C, AttrSet);
577 //===----------------------------------------------------------------------===//
578 // AttributeSet Accessor Methods
579 //===----------------------------------------------------------------------===//
581 AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
582 return pImpl && hasAttributes(Idx) ?
583 AttributeSet::get(pImpl->getContext(),
584 ArrayRef<std::pair<unsigned, Attribute> >(
585 std::make_pair(Idx, getAttributes(Idx)))) :
589 AttributeSet AttributeSet::getRetAttributes() const {
590 return pImpl && hasAttributes(ReturnIndex) ?
591 AttributeSet::get(pImpl->getContext(),
592 ArrayRef<std::pair<unsigned, Attribute> >(
593 std::make_pair(ReturnIndex,
594 getAttributes(ReturnIndex)))) :
598 AttributeSet AttributeSet::getFnAttributes() const {
599 return pImpl && hasAttributes(FunctionIndex) ?
600 AttributeSet::get(pImpl->getContext(),
601 ArrayRef<std::pair<unsigned, Attribute> >(
602 std::make_pair(FunctionIndex,
603 getAttributes(FunctionIndex)))) :
607 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
608 return getAttributes(Index).hasAttribute(Kind);
611 bool AttributeSet::hasAttributes(unsigned Index) const {
612 return getAttributes(Index).hasAttributes();
615 /// \brief Return true if the specified attribute is set for at least one
616 /// parameter or for the return value.
617 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
618 if (pImpl == 0) return false;
620 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
621 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
622 IE = pImpl->end(I); II != IE; ++II)
623 if (II->hasAttribute(Attr))
629 unsigned AttributeSet::getParamAlignment(unsigned Idx) const {
630 return getAttributes(Idx).getAlignment();
633 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
634 return getAttributes(Index).getStackAlignment();
637 std::string AttributeSet::getAsString(unsigned Index) const {
638 return getAttributes(Index).getAsString();
641 /// \brief The attributes for the specified index are returned.
643 /// FIXME: This shouldn't return 'Attribute'.
644 Attribute AttributeSet::getAttributes(unsigned Idx) const {
645 if (pImpl == 0) return Attribute();
647 // Loop through to find the attribute we want.
648 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
649 if (pImpl->getSlotIndex(I) != Idx) continue;
652 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
653 IE = pImpl->end(I); II != IE; ++II)
654 B.addAttributes(*II);
655 return Attribute::get(pImpl->getContext(), B);
661 //===----------------------------------------------------------------------===//
662 // AttributeSet Introspection Methods
663 //===----------------------------------------------------------------------===//
665 /// \brief Return the number of slots used in this attribute list. This is the
666 /// number of arguments that have an attribute set on them (including the
667 /// function itself).
668 unsigned AttributeSet::getNumSlots() const {
669 return pImpl ? pImpl->getNumAttributes() : 0;
672 uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
673 assert(pImpl && Slot < pImpl->getNumAttributes() &&
674 "Slot # out of range!");
675 return pImpl->getSlotIndex(Slot);
678 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
679 assert(pImpl && Slot < pImpl->getNumAttributes() &&
680 "Slot # out of range!");
681 return pImpl->getSlotAttributes(Slot);
684 uint64_t AttributeSet::Raw(unsigned Index) const {
685 // FIXME: Remove this.
686 return pImpl ? pImpl->Raw(Index) : 0;
689 void AttributeSet::dump() const {
692 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
693 uint64_t Index = getSlotIndex(i);
699 dbgs() << " => " << getAsString(Index) << " }\n";
705 //===----------------------------------------------------------------------===//
706 // AttrBuilder Method Implementations
707 //===----------------------------------------------------------------------===//
709 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
710 : Alignment(0), StackAlignment(0) {
711 AttributeSetImpl *pImpl = AS.pImpl;
716 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
717 if (pImpl->getSlotIndex(I) != Idx) continue;
719 for (AttributeSetNode::const_iterator II = pImpl->begin(I),
720 IE = pImpl->end(I); II != IE; ++II)
721 B.addAttributes(*II);
726 if (!B.hasAttributes()) return;
728 uint64_t Mask = B.Raw();
730 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
731 I = Attribute::AttrKind(I + 1)) {
732 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
735 if (I == Attribute::Alignment)
736 Alignment = 1ULL << ((A >> 16) - 1);
737 else if (I == Attribute::StackAlignment)
738 StackAlignment = 1ULL << ((A >> 26)-1);
743 void AttrBuilder::clear() {
745 Alignment = StackAlignment = 0;
748 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
753 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
755 if (Val == Attribute::Alignment)
757 else if (Val == Attribute::StackAlignment)
763 AttrBuilder &AttrBuilder::addAttributes(Attribute Attr) {
764 uint64_t Mask = Attr.Raw();
766 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
767 I = Attribute::AttrKind(I + 1))
768 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
771 if (Attr.getAlignment())
772 Alignment = Attr.getAlignment();
773 if (Attr.getStackAlignment())
774 StackAlignment = Attr.getStackAlignment();
778 AttrBuilder &AttrBuilder::removeAttributes(Attribute A) {
779 uint64_t Mask = A.Raw();
781 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
782 I = Attribute::AttrKind(I + 1)) {
783 if (Mask & AttributeImpl::getAttrMask(I)) {
786 if (I == Attribute::Alignment)
788 else if (I == Attribute::StackAlignment)
796 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
797 if (Align == 0) return *this;
799 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
800 assert(Align <= 0x40000000 && "Alignment too large.");
802 Attrs.insert(Attribute::Alignment);
807 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
808 // Default alignment, allow the target to define how to align it.
809 if (Align == 0) return *this;
811 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
812 assert(Align <= 0x100 && "Alignment too large.");
814 Attrs.insert(Attribute::StackAlignment);
815 StackAlignment = Align;
819 bool AttrBuilder::contains(Attribute::AttrKind A) const {
820 return Attrs.count(A);
823 bool AttrBuilder::hasAttributes() const {
824 return !Attrs.empty();
827 bool AttrBuilder::hasAttributes(const Attribute &A) const {
828 return Raw() & A.Raw();
831 bool AttrBuilder::hasAlignmentAttr() const {
832 return Alignment != 0;
835 bool AttrBuilder::operator==(const AttrBuilder &B) {
836 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
837 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
841 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
842 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
843 I = Attribute::AttrKind(I + 1)) {
844 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
847 if (I == Attribute::Alignment)
848 Alignment = 1ULL << ((A >> 16) - 1);
849 else if (I == Attribute::StackAlignment)
850 StackAlignment = 1ULL << ((A >> 26)-1);
857 uint64_t AttrBuilder::Raw() const {
860 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
861 E = Attrs.end(); I != E; ++I) {
862 Attribute::AttrKind Kind = *I;
864 if (Kind == Attribute::Alignment)
865 Mask |= (Log2_32(Alignment) + 1) << 16;
866 else if (Kind == Attribute::StackAlignment)
867 Mask |= (Log2_32(StackAlignment) + 1) << 26;
869 Mask |= AttributeImpl::getAttrMask(Kind);
875 //===----------------------------------------------------------------------===//
876 // AttributeFuncs Function Defintions
877 //===----------------------------------------------------------------------===//
879 Attribute AttributeFuncs::typeIncompatible(Type *Ty) {
880 AttrBuilder Incompatible;
882 if (!Ty->isIntegerTy())
883 // Attribute that only apply to integers.
884 Incompatible.addAttribute(Attribute::SExt)
885 .addAttribute(Attribute::ZExt);
887 if (!Ty->isPointerTy())
888 // Attribute that only apply to pointers.
889 Incompatible.addAttribute(Attribute::ByVal)
890 .addAttribute(Attribute::Nest)
891 .addAttribute(Attribute::NoAlias)
892 .addAttribute(Attribute::NoCapture)
893 .addAttribute(Attribute::StructRet);
895 return Attribute::get(Ty->getContext(), Incompatible);
898 /// \brief This returns an integer containing an encoding of all the LLVM
899 /// attributes found in the given attribute bitset. Any change to this encoding
900 /// is a breaking change to bitcode compatibility.
901 uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
903 // FIXME: It doesn't make sense to store the alignment information as an
904 // expanded out value, we should store it as a log2 value. However, we can't
905 // just change that here without breaking bitcode compatibility. If this ever
906 // becomes a problem in practice, we should introduce new tag numbers in the
907 // bitcode file and have those tags use a more efficiently encoded alignment
910 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
911 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
912 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
913 if (Attrs.hasAttribute(Index, Attribute::Alignment))
914 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
915 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
919 /// \brief This returns an attribute bitset containing the LLVM attributes that
920 /// have been decoded from the given integer. This function must stay in sync
921 /// with 'encodeLLVMAttributesForBitcode'.
922 Attribute AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
923 uint64_t EncodedAttrs){
924 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
925 // the bits above 31 down by 11 bits.
926 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
927 assert((!Alignment || isPowerOf2_32(Alignment)) &&
928 "Alignment must be a power of two.");
930 AttrBuilder B(EncodedAttrs & 0xffff);
932 B.addAlignmentAttr(Alignment);
933 B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
934 return Attribute::get(C, B);