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, Attribute::AttrKind Kind,
35 LLVMContextImpl *pImpl = Context.pImpl;
38 if (Val) ID.AddInteger(Val);
41 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
44 // If we didn't find any existing attributes of the same shape then create a
45 // new one and insert it.
47 PA = new EnumAttributeImpl(Kind);
49 PA = new AlignAttributeImpl(Kind, Val);
50 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
53 // Return the Attribute that we found or created.
57 Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
58 LLVMContextImpl *pImpl = Context.pImpl;
61 if (!Val.empty()) ID.AddString(Val);
64 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
67 // If we didn't find any existing attributes of the same shape then create a
68 // new one and insert it.
69 PA = new StringAttributeImpl(Kind, Val);
70 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
73 // Return the Attribute that we found or created.
77 Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
78 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
79 assert(Align <= 0x40000000 && "Alignment too large.");
80 return get(Context, Alignment, Align);
83 Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
85 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
86 assert(Align <= 0x100 && "Alignment too large.");
87 return get(Context, StackAlignment, Align);
90 //===----------------------------------------------------------------------===//
91 // Attribute Accessor Methods
92 //===----------------------------------------------------------------------===//
94 bool Attribute::isEnumAttribute() const {
95 return pImpl && pImpl->isEnumAttribute();
98 bool Attribute::isAlignAttribute() const {
99 return pImpl && pImpl->isAlignAttribute();
102 bool Attribute::isStringAttribute() const {
103 return pImpl && pImpl->isStringAttribute();
106 Attribute::AttrKind Attribute::getKindAsEnum() const {
107 if (!pImpl) return None;
108 assert((isEnumAttribute() || isAlignAttribute()) &&
109 "Invalid attribute type to get the kind as an enum!");
110 return pImpl ? pImpl->getKindAsEnum() : None;
113 uint64_t Attribute::getValueAsInt() const {
114 if (!pImpl) return 0;
115 assert(isAlignAttribute() &&
116 "Expected the attribute to be an alignment attribute!");
117 return pImpl ? pImpl->getValueAsInt() : 0;
120 StringRef Attribute::getKindAsString() const {
121 if (!pImpl) return StringRef();
122 assert(isStringAttribute() &&
123 "Invalid attribute type to get the kind as a string!");
124 return pImpl ? pImpl->getKindAsString() : StringRef();
127 StringRef Attribute::getValueAsString() const {
128 if (!pImpl) return StringRef();
129 assert(isStringAttribute() &&
130 "Invalid attribute type to get the value as a string!");
131 return pImpl ? pImpl->getValueAsString() : StringRef();
134 bool Attribute::hasAttribute(AttrKind Kind) const {
135 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
138 bool Attribute::hasAttribute(StringRef Kind) const {
139 if (!isStringAttribute()) return false;
140 return pImpl && pImpl->hasAttribute(Kind);
143 /// This returns the alignment field of an attribute as a byte alignment value.
144 unsigned Attribute::getAlignment() const {
145 assert(hasAttribute(Attribute::Alignment) &&
146 "Trying to get alignment from non-alignment attribute!");
147 return pImpl->getValueAsInt();
150 /// This returns the stack alignment field of an attribute as a byte alignment
152 unsigned Attribute::getStackAlignment() const {
153 assert(hasAttribute(Attribute::StackAlignment) &&
154 "Trying to get alignment from non-alignment attribute!");
155 return pImpl->getValueAsInt();
158 std::string Attribute::getAsString(bool InAttrGrp) const {
159 if (!pImpl) return "";
161 if (hasAttribute(Attribute::SanitizeAddress))
162 return "sanitize_address";
163 if (hasAttribute(Attribute::AlwaysInline))
164 return "alwaysinline";
165 if (hasAttribute(Attribute::Builtin))
167 if (hasAttribute(Attribute::ByVal))
169 if (hasAttribute(Attribute::InlineHint))
171 if (hasAttribute(Attribute::InReg))
173 if (hasAttribute(Attribute::MinSize))
175 if (hasAttribute(Attribute::Naked))
177 if (hasAttribute(Attribute::Nest))
179 if (hasAttribute(Attribute::NoAlias))
181 if (hasAttribute(Attribute::NoBuiltin))
183 if (hasAttribute(Attribute::NoCapture))
185 if (hasAttribute(Attribute::NoDuplicate))
186 return "noduplicate";
187 if (hasAttribute(Attribute::NoImplicitFloat))
188 return "noimplicitfloat";
189 if (hasAttribute(Attribute::NoInline))
191 if (hasAttribute(Attribute::NonLazyBind))
192 return "nonlazybind";
193 if (hasAttribute(Attribute::NoRedZone))
195 if (hasAttribute(Attribute::NoReturn))
197 if (hasAttribute(Attribute::NoUnwind))
199 if (hasAttribute(Attribute::OptimizeForSize))
201 if (hasAttribute(Attribute::ReadNone))
203 if (hasAttribute(Attribute::ReadOnly))
205 if (hasAttribute(Attribute::Returned))
207 if (hasAttribute(Attribute::ReturnsTwice))
208 return "returns_twice";
209 if (hasAttribute(Attribute::SExt))
211 if (hasAttribute(Attribute::StackProtect))
213 if (hasAttribute(Attribute::StackProtectReq))
215 if (hasAttribute(Attribute::StackProtectStrong))
217 if (hasAttribute(Attribute::StructRet))
219 if (hasAttribute(Attribute::SanitizeThread))
220 return "sanitize_thread";
221 if (hasAttribute(Attribute::SanitizeMemory))
222 return "sanitize_memory";
223 if (hasAttribute(Attribute::UWTable))
225 if (hasAttribute(Attribute::ZExt))
227 if (hasAttribute(Attribute::Cold))
230 // FIXME: These should be output like this:
235 if (hasAttribute(Attribute::Alignment)) {
238 Result += (InAttrGrp) ? "=" : " ";
239 Result += utostr(getValueAsInt());
243 if (hasAttribute(Attribute::StackAlignment)) {
245 Result += "alignstack";
248 Result += utostr(getValueAsInt());
251 Result += utostr(getValueAsInt());
257 // Convert target-dependent attributes to strings of the form:
262 if (isStringAttribute()) {
264 Result += '\"' + getKindAsString().str() + '"';
266 StringRef Val = pImpl->getValueAsString();
267 if (Val.empty()) return Result;
269 Result += "=\"" + Val.str() + '"';
273 llvm_unreachable("Unknown attribute");
276 bool Attribute::operator<(Attribute A) const {
277 if (!pImpl && !A.pImpl) return false;
278 if (!pImpl) return true;
279 if (!A.pImpl) return false;
280 return *pImpl < *A.pImpl;
283 //===----------------------------------------------------------------------===//
284 // AttributeImpl Definition
285 //===----------------------------------------------------------------------===//
287 AttributeImpl::~AttributeImpl() {}
289 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
290 if (isStringAttribute()) return false;
291 return getKindAsEnum() == A;
294 bool AttributeImpl::hasAttribute(StringRef Kind) const {
295 if (!isStringAttribute()) return false;
296 return getKindAsString() == Kind;
299 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
300 assert(isEnumAttribute() || isAlignAttribute());
301 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
304 uint64_t AttributeImpl::getValueAsInt() const {
305 assert(isAlignAttribute());
306 return static_cast<const AlignAttributeImpl *>(this)->getAlignment();
309 StringRef AttributeImpl::getKindAsString() const {
310 assert(isStringAttribute());
311 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
314 StringRef AttributeImpl::getValueAsString() const {
315 assert(isStringAttribute());
316 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
319 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
320 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
321 // relative to their enum value) and then strings.
322 if (isEnumAttribute()) {
323 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
324 if (AI.isAlignAttribute()) return true;
325 if (AI.isStringAttribute()) return true;
328 if (isAlignAttribute()) {
329 if (AI.isEnumAttribute()) return false;
330 if (AI.isAlignAttribute()) return getValueAsInt() < AI.getValueAsInt();
331 if (AI.isStringAttribute()) return true;
334 if (AI.isEnumAttribute()) return false;
335 if (AI.isAlignAttribute()) return false;
336 if (getKindAsString() == AI.getKindAsString())
337 return getValueAsString() < AI.getValueAsString();
338 return getKindAsString() < AI.getKindAsString();
341 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
342 // FIXME: Remove this.
344 case Attribute::EndAttrKinds:
345 llvm_unreachable("Synthetic enumerators which should never get here");
347 case Attribute::None: return 0;
348 case Attribute::ZExt: return 1 << 0;
349 case Attribute::SExt: return 1 << 1;
350 case Attribute::NoReturn: return 1 << 2;
351 case Attribute::InReg: return 1 << 3;
352 case Attribute::StructRet: return 1 << 4;
353 case Attribute::NoUnwind: return 1 << 5;
354 case Attribute::NoAlias: return 1 << 6;
355 case Attribute::ByVal: return 1 << 7;
356 case Attribute::Nest: return 1 << 8;
357 case Attribute::ReadNone: return 1 << 9;
358 case Attribute::ReadOnly: return 1 << 10;
359 case Attribute::NoInline: return 1 << 11;
360 case Attribute::AlwaysInline: return 1 << 12;
361 case Attribute::OptimizeForSize: return 1 << 13;
362 case Attribute::StackProtect: return 1 << 14;
363 case Attribute::StackProtectReq: return 1 << 15;
364 case Attribute::Alignment: return 31 << 16;
365 case Attribute::NoCapture: return 1 << 21;
366 case Attribute::NoRedZone: return 1 << 22;
367 case Attribute::NoImplicitFloat: return 1 << 23;
368 case Attribute::Naked: return 1 << 24;
369 case Attribute::InlineHint: return 1 << 25;
370 case Attribute::StackAlignment: return 7 << 26;
371 case Attribute::ReturnsTwice: return 1 << 29;
372 case Attribute::UWTable: return 1 << 30;
373 case Attribute::NonLazyBind: return 1U << 31;
374 case Attribute::SanitizeAddress: return 1ULL << 32;
375 case Attribute::MinSize: return 1ULL << 33;
376 case Attribute::NoDuplicate: return 1ULL << 34;
377 case Attribute::StackProtectStrong: return 1ULL << 35;
378 case Attribute::SanitizeThread: return 1ULL << 36;
379 case Attribute::SanitizeMemory: return 1ULL << 37;
380 case Attribute::NoBuiltin: return 1ULL << 38;
381 case Attribute::Returned: return 1ULL << 39;
382 case Attribute::Cold: return 1ULL << 40;
383 case Attribute::Builtin: return 1ULL << 41;
385 llvm_unreachable("Unsupported attribute type");
388 //===----------------------------------------------------------------------===//
389 // AttributeSetNode Definition
390 //===----------------------------------------------------------------------===//
392 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
393 ArrayRef<Attribute> Attrs) {
397 // Otherwise, build a key to look up the existing attributes.
398 LLVMContextImpl *pImpl = C.pImpl;
401 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
402 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
404 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
405 E = SortedAttrs.end(); I != E; ++I)
409 AttributeSetNode *PA =
410 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
412 // If we didn't find any existing attributes of the same shape then create a
413 // new one and insert it.
415 // Coallocate entries after the AttributeSetNode itself.
416 void *Mem = ::operator new(sizeof(AttributeSetNode) +
417 sizeof(Attribute) * SortedAttrs.size());
418 PA = new (Mem) AttributeSetNode(SortedAttrs);
419 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
422 // Return the AttributesListNode that we found or created.
426 bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
427 for (iterator I = begin(), E = end(); I != E; ++I)
428 if (I->hasAttribute(Kind))
433 bool AttributeSetNode::hasAttribute(StringRef Kind) const {
434 for (iterator I = begin(), E = end(); I != E; ++I)
435 if (I->hasAttribute(Kind))
440 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
441 for (iterator I = begin(), E = end(); I != E; ++I)
442 if (I->hasAttribute(Kind))
447 Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
448 for (iterator I = begin(), E = end(); I != E; ++I)
449 if (I->hasAttribute(Kind))
454 unsigned AttributeSetNode::getAlignment() const {
455 for (iterator I = begin(), E = end(); I != E; ++I)
456 if (I->hasAttribute(Attribute::Alignment))
457 return I->getAlignment();
461 unsigned AttributeSetNode::getStackAlignment() const {
462 for (iterator I = begin(), E = end(); I != E; ++I)
463 if (I->hasAttribute(Attribute::StackAlignment))
464 return I->getStackAlignment();
468 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
470 for (iterator I = begin(), E = end(); I != E; ++I) {
473 Str += I->getAsString(InAttrGrp);
478 //===----------------------------------------------------------------------===//
479 // AttributeSetImpl Definition
480 //===----------------------------------------------------------------------===//
482 uint64_t AttributeSetImpl::Raw(unsigned Index) const {
483 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
484 if (getSlotIndex(I) != Index) continue;
485 const AttributeSetNode *ASN = getSlotNode(I);
488 for (AttributeSetNode::iterator II = ASN->begin(),
489 IE = ASN->end(); II != IE; ++II) {
490 Attribute Attr = *II;
492 // This cannot handle string attributes.
493 if (Attr.isStringAttribute()) continue;
495 Attribute::AttrKind Kind = Attr.getKindAsEnum();
497 if (Kind == Attribute::Alignment)
498 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
499 else if (Kind == Attribute::StackAlignment)
500 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
502 Mask |= AttributeImpl::getAttrMask(Kind);
511 //===----------------------------------------------------------------------===//
512 // AttributeSet Construction and Mutation Methods
513 //===----------------------------------------------------------------------===//
516 AttributeSet::getImpl(LLVMContext &C,
517 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
518 LLVMContextImpl *pImpl = C.pImpl;
520 AttributeSetImpl::Profile(ID, Attrs);
523 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
525 // If we didn't find any existing attributes of the same shape then
526 // create a new one and insert it.
528 // Coallocate entries after the AttributeSetImpl itself.
529 void *Mem = ::operator new(sizeof(AttributeSetImpl) +
530 sizeof(std::pair<unsigned, AttributeSetNode *>) *
532 PA = new (Mem) AttributeSetImpl(C, Attrs);
533 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
536 // Return the AttributesList that we found or created.
537 return AttributeSet(PA);
540 AttributeSet AttributeSet::get(LLVMContext &C,
541 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
542 // If there are no attributes then return a null AttributesList pointer.
544 return AttributeSet();
547 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
548 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
549 "Misordered Attributes list!");
550 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
551 "Pointless attribute!");
555 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
557 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
558 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
559 E = Attrs.end(); I != E; ) {
560 unsigned Index = I->first;
561 SmallVector<Attribute, 4> AttrVec;
562 while (I != E && I->first == Index) {
563 AttrVec.push_back(I->second);
567 AttrPairVec.push_back(std::make_pair(Index,
568 AttributeSetNode::get(C, AttrVec)));
571 return getImpl(C, AttrPairVec);
574 AttributeSet AttributeSet::get(LLVMContext &C,
575 ArrayRef<std::pair<unsigned,
576 AttributeSetNode*> > Attrs) {
577 // If there are no attributes then return a null AttributesList pointer.
579 return AttributeSet();
581 return getImpl(C, Attrs);
584 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index, AttrBuilder &B) {
585 if (!B.hasAttributes())
586 return AttributeSet();
588 // Add target-independent attributes.
589 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
590 for (Attribute::AttrKind Kind = Attribute::None;
591 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
592 if (!B.contains(Kind))
595 if (Kind == Attribute::Alignment)
596 Attrs.push_back(std::make_pair(Index, Attribute::
597 getWithAlignment(C, B.getAlignment())));
598 else if (Kind == Attribute::StackAlignment)
599 Attrs.push_back(std::make_pair(Index, Attribute::
600 getWithStackAlignment(C, B.getStackAlignment())));
602 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
605 // Add target-dependent (string) attributes.
606 for (AttrBuilder::td_iterator I = B.td_begin(), E = B.td_end();
608 Attrs.push_back(std::make_pair(Index, Attribute::get(C, I->first,I->second)));
610 return get(C, Attrs);
613 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
614 ArrayRef<Attribute::AttrKind> Kind) {
615 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
616 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
617 E = Kind.end(); I != E; ++I)
618 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
619 return get(C, Attrs);
622 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
623 if (Attrs.empty()) return AttributeSet();
624 if (Attrs.size() == 1) return Attrs[0];
626 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
627 AttributeSetImpl *A0 = Attrs[0].pImpl;
629 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
630 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
631 // ordered by index. Because we know that each list in Attrs is ordered by
632 // index we only need to merge each successive list in rather than doing a
634 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
635 AttributeSetImpl *AS = Attrs[I].pImpl;
637 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
638 ANVI = AttrNodeVec.begin(), ANVE;
639 for (const AttributeSetImpl::IndexAttrPair
640 *AI = AS->getNode(0),
641 *AE = AS->getNode(AS->getNumAttributes());
643 ANVE = AttrNodeVec.end();
644 while (ANVI != ANVE && ANVI->first <= AI->first)
646 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
650 return getImpl(C, AttrNodeVec);
653 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
654 Attribute::AttrKind Attr) const {
655 if (hasAttribute(Index, Attr)) return *this;
656 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
659 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
660 StringRef Kind) const {
662 B.addAttribute(Kind);
663 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
666 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
667 StringRef Kind, StringRef Value) const {
669 B.addAttribute(Kind, Value);
670 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
673 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
674 AttributeSet Attrs) const {
675 if (!pImpl) return Attrs;
676 if (!Attrs.pImpl) return *this;
679 // FIXME it is not obvious how this should work for alignment. For now, say
680 // we can't change a known alignment.
681 unsigned OldAlign = getParamAlignment(Index);
682 unsigned NewAlign = Attrs.getParamAlignment(Index);
683 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
684 "Attempt to change alignment!");
687 // Add the attribute slots before the one we're trying to add.
688 SmallVector<AttributeSet, 4> AttrSet;
689 uint64_t NumAttrs = pImpl->getNumAttributes();
691 uint64_t LastIndex = 0;
692 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
693 if (getSlotIndex(I) >= Index) {
694 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
698 AttrSet.push_back(getSlotAttributes(I));
701 // Now add the attribute into the correct slot. There may already be an
702 // AttributeSet there.
703 AttrBuilder B(AS, Index);
705 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
706 if (Attrs.getSlotIndex(I) == Index) {
707 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
708 IE = Attrs.pImpl->end(I); II != IE; ++II)
713 AttrSet.push_back(AttributeSet::get(C, Index, B));
715 // Add the remaining attribute slots.
716 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
717 AttrSet.push_back(getSlotAttributes(I));
719 return get(C, AttrSet);
722 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
723 Attribute::AttrKind Attr) const {
724 if (!hasAttribute(Index, Attr)) return *this;
725 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
728 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
729 AttributeSet Attrs) const {
730 if (!pImpl) return AttributeSet();
731 if (!Attrs.pImpl) return *this;
734 // FIXME it is not obvious how this should work for alignment.
735 // For now, say we can't pass in alignment, which no current use does.
736 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
737 "Attempt to change alignment!");
740 // Add the attribute slots before the one we're trying to add.
741 SmallVector<AttributeSet, 4> AttrSet;
742 uint64_t NumAttrs = pImpl->getNumAttributes();
744 uint64_t LastIndex = 0;
745 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
746 if (getSlotIndex(I) >= Index) {
747 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
751 AttrSet.push_back(getSlotAttributes(I));
754 // Now remove the attribute from the correct slot. There may already be an
755 // AttributeSet there.
756 AttrBuilder B(AS, Index);
758 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
759 if (Attrs.getSlotIndex(I) == Index) {
760 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
764 AttrSet.push_back(AttributeSet::get(C, Index, B));
766 // Add the remaining attribute slots.
767 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
768 AttrSet.push_back(getSlotAttributes(I));
770 return get(C, AttrSet);
773 //===----------------------------------------------------------------------===//
774 // AttributeSet Accessor Methods
775 //===----------------------------------------------------------------------===//
777 LLVMContext &AttributeSet::getContext() const {
778 return pImpl->getContext();
781 AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
782 return pImpl && hasAttributes(Index) ?
783 AttributeSet::get(pImpl->getContext(),
784 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
785 std::make_pair(Index, getAttributes(Index)))) :
789 AttributeSet AttributeSet::getRetAttributes() const {
790 return pImpl && hasAttributes(ReturnIndex) ?
791 AttributeSet::get(pImpl->getContext(),
792 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
793 std::make_pair(ReturnIndex,
794 getAttributes(ReturnIndex)))) :
798 AttributeSet AttributeSet::getFnAttributes() const {
799 return pImpl && hasAttributes(FunctionIndex) ?
800 AttributeSet::get(pImpl->getContext(),
801 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
802 std::make_pair(FunctionIndex,
803 getAttributes(FunctionIndex)))) :
807 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
808 AttributeSetNode *ASN = getAttributes(Index);
809 return ASN ? ASN->hasAttribute(Kind) : false;
812 bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
813 AttributeSetNode *ASN = getAttributes(Index);
814 return ASN ? ASN->hasAttribute(Kind) : false;
817 bool AttributeSet::hasAttributes(unsigned Index) const {
818 AttributeSetNode *ASN = getAttributes(Index);
819 return ASN ? ASN->hasAttributes() : false;
822 /// \brief Return true if the specified attribute is set for at least one
823 /// parameter or for the return value.
824 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
825 if (pImpl == 0) return false;
827 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
828 for (AttributeSetImpl::iterator II = pImpl->begin(I),
829 IE = pImpl->end(I); II != IE; ++II)
830 if (II->hasAttribute(Attr))
836 Attribute AttributeSet::getAttribute(unsigned Index,
837 Attribute::AttrKind Kind) const {
838 AttributeSetNode *ASN = getAttributes(Index);
839 return ASN ? ASN->getAttribute(Kind) : Attribute();
842 Attribute AttributeSet::getAttribute(unsigned Index,
843 StringRef Kind) const {
844 AttributeSetNode *ASN = getAttributes(Index);
845 return ASN ? ASN->getAttribute(Kind) : Attribute();
848 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
849 AttributeSetNode *ASN = getAttributes(Index);
850 return ASN ? ASN->getAlignment() : 0;
853 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
854 AttributeSetNode *ASN = getAttributes(Index);
855 return ASN ? ASN->getStackAlignment() : 0;
858 std::string AttributeSet::getAsString(unsigned Index,
859 bool InAttrGrp) const {
860 AttributeSetNode *ASN = getAttributes(Index);
861 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
864 /// \brief The attributes for the specified index are returned.
865 AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
866 if (!pImpl) return 0;
868 // Loop through to find the attribute node we want.
869 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
870 if (pImpl->getSlotIndex(I) == Index)
871 return pImpl->getSlotNode(I);
876 AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
878 return ArrayRef<Attribute>().begin();
879 return pImpl->begin(Slot);
882 AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
884 return ArrayRef<Attribute>().end();
885 return pImpl->end(Slot);
888 //===----------------------------------------------------------------------===//
889 // AttributeSet Introspection Methods
890 //===----------------------------------------------------------------------===//
892 /// \brief Return the number of slots used in this attribute list. This is the
893 /// number of arguments that have an attribute set on them (including the
894 /// function itself).
895 unsigned AttributeSet::getNumSlots() const {
896 return pImpl ? pImpl->getNumAttributes() : 0;
899 unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
900 assert(pImpl && Slot < pImpl->getNumAttributes() &&
901 "Slot # out of range!");
902 return pImpl->getSlotIndex(Slot);
905 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
906 assert(pImpl && Slot < pImpl->getNumAttributes() &&
907 "Slot # out of range!");
908 return pImpl->getSlotAttributes(Slot);
911 uint64_t AttributeSet::Raw(unsigned Index) const {
912 // FIXME: Remove this.
913 return pImpl ? pImpl->Raw(Index) : 0;
916 void AttributeSet::dump() const {
919 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
920 uint64_t Index = getSlotIndex(i);
926 dbgs() << " => " << getAsString(Index) << " }\n";
932 //===----------------------------------------------------------------------===//
933 // AttrBuilder Method Implementations
934 //===----------------------------------------------------------------------===//
936 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
937 : Attrs(0), Alignment(0), StackAlignment(0) {
938 AttributeSetImpl *pImpl = AS.pImpl;
941 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
942 if (pImpl->getSlotIndex(I) != Index) continue;
944 for (AttributeSetImpl::iterator II = pImpl->begin(I),
945 IE = pImpl->end(I); II != IE; ++II)
952 void AttrBuilder::clear() {
954 Alignment = StackAlignment = 0;
957 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
958 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
959 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
960 "Adding alignment attribute without adding alignment value!");
965 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
966 if (Attr.isStringAttribute()) {
967 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
971 Attribute::AttrKind Kind = Attr.getKindAsEnum();
974 if (Kind == Attribute::Alignment)
975 Alignment = Attr.getAlignment();
976 else if (Kind == Attribute::StackAlignment)
977 StackAlignment = Attr.getStackAlignment();
981 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
982 TargetDepAttrs[A] = V;
986 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
987 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
990 if (Val == Attribute::Alignment)
992 else if (Val == Attribute::StackAlignment)
998 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
1000 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1001 if (A.getSlotIndex(I) == Index) {
1006 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
1008 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
1009 Attribute Attr = *I;
1010 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1011 Attribute::AttrKind Kind = I->getKindAsEnum();
1012 Attrs[Kind] = false;
1014 if (Kind == Attribute::Alignment)
1016 else if (Kind == Attribute::StackAlignment)
1019 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1020 std::map<std::string, std::string>::iterator
1021 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1022 if (Iter != TargetDepAttrs.end())
1023 TargetDepAttrs.erase(Iter);
1030 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1031 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1032 if (I != TargetDepAttrs.end())
1033 TargetDepAttrs.erase(I);
1037 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
1038 if (Align == 0) return *this;
1040 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1041 assert(Align <= 0x40000000 && "Alignment too large.");
1043 Attrs[Attribute::Alignment] = true;
1048 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1049 // Default alignment, allow the target to define how to align it.
1050 if (Align == 0) return *this;
1052 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1053 assert(Align <= 0x100 && "Alignment too large.");
1055 Attrs[Attribute::StackAlignment] = true;
1056 StackAlignment = Align;
1060 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1061 // FIXME: What if both have alignments, but they don't match?!
1063 Alignment = B.Alignment;
1065 if (!StackAlignment)
1066 StackAlignment = B.StackAlignment;
1070 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1071 E = B.TargetDepAttrs.end(); I != E; ++I)
1072 TargetDepAttrs[I->first] = I->second;
1077 bool AttrBuilder::contains(StringRef A) const {
1078 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1081 bool AttrBuilder::hasAttributes() const {
1082 return !Attrs.none() || !TargetDepAttrs.empty();
1085 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
1086 unsigned Slot = ~0U;
1087 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1088 if (A.getSlotIndex(I) == Index) {
1093 assert(Slot != ~0U && "Couldn't find the index!");
1095 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
1097 Attribute Attr = *I;
1098 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1099 if (Attrs[I->getKindAsEnum()])
1102 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1103 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1110 bool AttrBuilder::hasAlignmentAttr() const {
1111 return Alignment != 0;
1114 bool AttrBuilder::operator==(const AttrBuilder &B) {
1115 if (Attrs != B.Attrs)
1118 for (td_const_iterator I = TargetDepAttrs.begin(),
1119 E = TargetDepAttrs.end(); I != E; ++I)
1120 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1123 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
1126 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
1127 // FIXME: Remove this in 4.0.
1128 if (!Val) return *this;
1130 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1131 I = Attribute::AttrKind(I + 1)) {
1132 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1135 if (I == Attribute::Alignment)
1136 Alignment = 1ULL << ((A >> 16) - 1);
1137 else if (I == Attribute::StackAlignment)
1138 StackAlignment = 1ULL << ((A >> 26)-1);
1145 //===----------------------------------------------------------------------===//
1146 // AttributeFuncs Function Defintions
1147 //===----------------------------------------------------------------------===//
1149 /// \brief Which attributes cannot be applied to a type.
1150 AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
1151 AttrBuilder Incompatible;
1153 if (!Ty->isIntegerTy())
1154 // Attribute that only apply to integers.
1155 Incompatible.addAttribute(Attribute::SExt)
1156 .addAttribute(Attribute::ZExt);
1158 if (!Ty->isPointerTy())
1159 // Attribute that only apply to pointers.
1160 Incompatible.addAttribute(Attribute::ByVal)
1161 .addAttribute(Attribute::Nest)
1162 .addAttribute(Attribute::NoAlias)
1163 .addAttribute(Attribute::NoCapture)
1164 .addAttribute(Attribute::ReadNone)
1165 .addAttribute(Attribute::ReadOnly)
1166 .addAttribute(Attribute::StructRet);
1168 return AttributeSet::get(Ty->getContext(), Index, Incompatible);