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::InAlloca))
171 if (hasAttribute(Attribute::InlineHint))
173 if (hasAttribute(Attribute::InReg))
175 if (hasAttribute(Attribute::MinSize))
177 if (hasAttribute(Attribute::Naked))
179 if (hasAttribute(Attribute::Nest))
181 if (hasAttribute(Attribute::NoAlias))
183 if (hasAttribute(Attribute::NoBuiltin))
185 if (hasAttribute(Attribute::NoCapture))
187 if (hasAttribute(Attribute::NoDuplicate))
188 return "noduplicate";
189 if (hasAttribute(Attribute::NoImplicitFloat))
190 return "noimplicitfloat";
191 if (hasAttribute(Attribute::NoInline))
193 if (hasAttribute(Attribute::NonLazyBind))
194 return "nonlazybind";
195 if (hasAttribute(Attribute::NoRedZone))
197 if (hasAttribute(Attribute::NoReturn))
199 if (hasAttribute(Attribute::NoUnwind))
201 if (hasAttribute(Attribute::OptimizeNone))
203 if (hasAttribute(Attribute::OptimizeForSize))
205 if (hasAttribute(Attribute::ReadNone))
207 if (hasAttribute(Attribute::ReadOnly))
209 if (hasAttribute(Attribute::Returned))
211 if (hasAttribute(Attribute::ReturnsTwice))
212 return "returns_twice";
213 if (hasAttribute(Attribute::SExt))
215 if (hasAttribute(Attribute::StackProtect))
217 if (hasAttribute(Attribute::StackProtectReq))
219 if (hasAttribute(Attribute::StackProtectStrong))
221 if (hasAttribute(Attribute::StructRet))
223 if (hasAttribute(Attribute::SanitizeThread))
224 return "sanitize_thread";
225 if (hasAttribute(Attribute::SanitizeMemory))
226 return "sanitize_memory";
227 if (hasAttribute(Attribute::UWTable))
229 if (hasAttribute(Attribute::ZExt))
231 if (hasAttribute(Attribute::Cold))
234 // FIXME: These should be output like this:
239 if (hasAttribute(Attribute::Alignment)) {
242 Result += (InAttrGrp) ? "=" : " ";
243 Result += utostr(getValueAsInt());
247 if (hasAttribute(Attribute::StackAlignment)) {
249 Result += "alignstack";
252 Result += utostr(getValueAsInt());
255 Result += utostr(getValueAsInt());
261 // Convert target-dependent attributes to strings of the form:
266 if (isStringAttribute()) {
268 Result += '\"' + getKindAsString().str() + '"';
270 StringRef Val = pImpl->getValueAsString();
271 if (Val.empty()) return Result;
273 Result += "=\"" + Val.str() + '"';
277 llvm_unreachable("Unknown attribute");
280 bool Attribute::operator<(Attribute A) const {
281 if (!pImpl && !A.pImpl) return false;
282 if (!pImpl) return true;
283 if (!A.pImpl) return false;
284 return *pImpl < *A.pImpl;
287 //===----------------------------------------------------------------------===//
288 // AttributeImpl Definition
289 //===----------------------------------------------------------------------===//
291 // Pin the vtabels to this file.
292 AttributeImpl::~AttributeImpl() {}
293 void EnumAttributeImpl::anchor() {}
294 void AlignAttributeImpl::anchor() {}
295 void StringAttributeImpl::anchor() {}
297 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
298 if (isStringAttribute()) return false;
299 return getKindAsEnum() == A;
302 bool AttributeImpl::hasAttribute(StringRef Kind) const {
303 if (!isStringAttribute()) return false;
304 return getKindAsString() == Kind;
307 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
308 assert(isEnumAttribute() || isAlignAttribute());
309 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
312 uint64_t AttributeImpl::getValueAsInt() const {
313 assert(isAlignAttribute());
314 return static_cast<const AlignAttributeImpl *>(this)->getAlignment();
317 StringRef AttributeImpl::getKindAsString() const {
318 assert(isStringAttribute());
319 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
322 StringRef AttributeImpl::getValueAsString() const {
323 assert(isStringAttribute());
324 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
327 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
328 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
329 // relative to their enum value) and then strings.
330 if (isEnumAttribute()) {
331 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
332 if (AI.isAlignAttribute()) return true;
333 if (AI.isStringAttribute()) return true;
336 if (isAlignAttribute()) {
337 if (AI.isEnumAttribute()) return false;
338 if (AI.isAlignAttribute()) return getValueAsInt() < AI.getValueAsInt();
339 if (AI.isStringAttribute()) return true;
342 if (AI.isEnumAttribute()) return false;
343 if (AI.isAlignAttribute()) return false;
344 if (getKindAsString() == AI.getKindAsString())
345 return getValueAsString() < AI.getValueAsString();
346 return getKindAsString() < AI.getKindAsString();
349 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
350 // FIXME: Remove this.
352 case Attribute::EndAttrKinds:
353 llvm_unreachable("Synthetic enumerators which should never get here");
355 case Attribute::None: return 0;
356 case Attribute::ZExt: return 1 << 0;
357 case Attribute::SExt: return 1 << 1;
358 case Attribute::NoReturn: return 1 << 2;
359 case Attribute::InReg: return 1 << 3;
360 case Attribute::StructRet: return 1 << 4;
361 case Attribute::NoUnwind: return 1 << 5;
362 case Attribute::NoAlias: return 1 << 6;
363 case Attribute::ByVal: return 1 << 7;
364 case Attribute::Nest: return 1 << 8;
365 case Attribute::ReadNone: return 1 << 9;
366 case Attribute::ReadOnly: return 1 << 10;
367 case Attribute::NoInline: return 1 << 11;
368 case Attribute::AlwaysInline: return 1 << 12;
369 case Attribute::OptimizeForSize: return 1 << 13;
370 case Attribute::StackProtect: return 1 << 14;
371 case Attribute::StackProtectReq: return 1 << 15;
372 case Attribute::Alignment: return 31 << 16;
373 case Attribute::NoCapture: return 1 << 21;
374 case Attribute::NoRedZone: return 1 << 22;
375 case Attribute::NoImplicitFloat: return 1 << 23;
376 case Attribute::Naked: return 1 << 24;
377 case Attribute::InlineHint: return 1 << 25;
378 case Attribute::StackAlignment: return 7 << 26;
379 case Attribute::ReturnsTwice: return 1 << 29;
380 case Attribute::UWTable: return 1 << 30;
381 case Attribute::NonLazyBind: return 1U << 31;
382 case Attribute::SanitizeAddress: return 1ULL << 32;
383 case Attribute::MinSize: return 1ULL << 33;
384 case Attribute::NoDuplicate: return 1ULL << 34;
385 case Attribute::StackProtectStrong: return 1ULL << 35;
386 case Attribute::SanitizeThread: return 1ULL << 36;
387 case Attribute::SanitizeMemory: return 1ULL << 37;
388 case Attribute::NoBuiltin: return 1ULL << 38;
389 case Attribute::Returned: return 1ULL << 39;
390 case Attribute::Cold: return 1ULL << 40;
391 case Attribute::Builtin: return 1ULL << 41;
392 case Attribute::OptimizeNone: return 1ULL << 42;
393 case Attribute::InAlloca: return 1ULL << 43;
395 llvm_unreachable("Unsupported attribute type");
398 //===----------------------------------------------------------------------===//
399 // AttributeSetNode Definition
400 //===----------------------------------------------------------------------===//
402 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
403 ArrayRef<Attribute> Attrs) {
407 // Otherwise, build a key to look up the existing attributes.
408 LLVMContextImpl *pImpl = C.pImpl;
411 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
412 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
414 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
415 E = SortedAttrs.end(); I != E; ++I)
419 AttributeSetNode *PA =
420 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
422 // If we didn't find any existing attributes of the same shape then create a
423 // new one and insert it.
425 // Coallocate entries after the AttributeSetNode itself.
426 void *Mem = ::operator new(sizeof(AttributeSetNode) +
427 sizeof(Attribute) * SortedAttrs.size());
428 PA = new (Mem) AttributeSetNode(SortedAttrs);
429 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
432 // Return the AttributesListNode that we found or created.
436 bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
437 for (iterator I = begin(), E = end(); I != E; ++I)
438 if (I->hasAttribute(Kind))
443 bool AttributeSetNode::hasAttribute(StringRef Kind) const {
444 for (iterator I = begin(), E = end(); I != E; ++I)
445 if (I->hasAttribute(Kind))
450 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
451 for (iterator I = begin(), E = end(); I != E; ++I)
452 if (I->hasAttribute(Kind))
457 Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
458 for (iterator I = begin(), E = end(); I != E; ++I)
459 if (I->hasAttribute(Kind))
464 unsigned AttributeSetNode::getAlignment() const {
465 for (iterator I = begin(), E = end(); I != E; ++I)
466 if (I->hasAttribute(Attribute::Alignment))
467 return I->getAlignment();
471 unsigned AttributeSetNode::getStackAlignment() const {
472 for (iterator I = begin(), E = end(); I != E; ++I)
473 if (I->hasAttribute(Attribute::StackAlignment))
474 return I->getStackAlignment();
478 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
480 for (iterator I = begin(), E = end(); I != E; ++I) {
483 Str += I->getAsString(InAttrGrp);
488 //===----------------------------------------------------------------------===//
489 // AttributeSetImpl Definition
490 //===----------------------------------------------------------------------===//
492 uint64_t AttributeSetImpl::Raw(unsigned Index) const {
493 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
494 if (getSlotIndex(I) != Index) continue;
495 const AttributeSetNode *ASN = getSlotNode(I);
498 for (AttributeSetNode::iterator II = ASN->begin(),
499 IE = ASN->end(); II != IE; ++II) {
500 Attribute Attr = *II;
502 // This cannot handle string attributes.
503 if (Attr.isStringAttribute()) continue;
505 Attribute::AttrKind Kind = Attr.getKindAsEnum();
507 if (Kind == Attribute::Alignment)
508 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
509 else if (Kind == Attribute::StackAlignment)
510 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
512 Mask |= AttributeImpl::getAttrMask(Kind);
521 void AttributeSetImpl::dump() const {
522 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
525 //===----------------------------------------------------------------------===//
526 // AttributeSet Construction and Mutation Methods
527 //===----------------------------------------------------------------------===//
530 AttributeSet::getImpl(LLVMContext &C,
531 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
532 LLVMContextImpl *pImpl = C.pImpl;
534 AttributeSetImpl::Profile(ID, Attrs);
537 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
539 // If we didn't find any existing attributes of the same shape then
540 // create a new one and insert it.
542 // Coallocate entries after the AttributeSetImpl itself.
543 void *Mem = ::operator new(sizeof(AttributeSetImpl) +
544 sizeof(std::pair<unsigned, AttributeSetNode *>) *
546 PA = new (Mem) AttributeSetImpl(C, Attrs);
547 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
550 // Return the AttributesList that we found or created.
551 return AttributeSet(PA);
554 AttributeSet AttributeSet::get(LLVMContext &C,
555 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
556 // If there are no attributes then return a null AttributesList pointer.
558 return AttributeSet();
561 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
562 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
563 "Misordered Attributes list!");
564 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
565 "Pointless attribute!");
569 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
571 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
572 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
573 E = Attrs.end(); I != E; ) {
574 unsigned Index = I->first;
575 SmallVector<Attribute, 4> AttrVec;
576 while (I != E && I->first == Index) {
577 AttrVec.push_back(I->second);
581 AttrPairVec.push_back(std::make_pair(Index,
582 AttributeSetNode::get(C, AttrVec)));
585 return getImpl(C, AttrPairVec);
588 AttributeSet AttributeSet::get(LLVMContext &C,
589 ArrayRef<std::pair<unsigned,
590 AttributeSetNode*> > Attrs) {
591 // If there are no attributes then return a null AttributesList pointer.
593 return AttributeSet();
595 return getImpl(C, Attrs);
598 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index, AttrBuilder &B) {
599 if (!B.hasAttributes())
600 return AttributeSet();
602 // Add target-independent attributes.
603 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
604 for (Attribute::AttrKind Kind = Attribute::None;
605 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
606 if (!B.contains(Kind))
609 if (Kind == Attribute::Alignment)
610 Attrs.push_back(std::make_pair(Index, Attribute::
611 getWithAlignment(C, B.getAlignment())));
612 else if (Kind == Attribute::StackAlignment)
613 Attrs.push_back(std::make_pair(Index, Attribute::
614 getWithStackAlignment(C, B.getStackAlignment())));
616 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
619 // Add target-dependent (string) attributes.
620 for (AttrBuilder::td_iterator I = B.td_begin(), E = B.td_end();
622 Attrs.push_back(std::make_pair(Index, Attribute::get(C, I->first,I->second)));
624 return get(C, Attrs);
627 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
628 ArrayRef<Attribute::AttrKind> Kind) {
629 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
630 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
631 E = Kind.end(); I != E; ++I)
632 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
633 return get(C, Attrs);
636 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
637 if (Attrs.empty()) return AttributeSet();
638 if (Attrs.size() == 1) return Attrs[0];
640 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
641 AttributeSetImpl *A0 = Attrs[0].pImpl;
643 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
644 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
645 // ordered by index. Because we know that each list in Attrs is ordered by
646 // index we only need to merge each successive list in rather than doing a
648 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
649 AttributeSetImpl *AS = Attrs[I].pImpl;
651 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
652 ANVI = AttrNodeVec.begin(), ANVE;
653 for (const AttributeSetImpl::IndexAttrPair
654 *AI = AS->getNode(0),
655 *AE = AS->getNode(AS->getNumAttributes());
657 ANVE = AttrNodeVec.end();
658 while (ANVI != ANVE && ANVI->first <= AI->first)
660 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
664 return getImpl(C, AttrNodeVec);
667 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
668 Attribute::AttrKind Attr) const {
669 if (hasAttribute(Index, Attr)) return *this;
670 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
673 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
674 StringRef Kind) const {
676 B.addAttribute(Kind);
677 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
680 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
681 StringRef Kind, StringRef Value) const {
683 B.addAttribute(Kind, Value);
684 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
687 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
688 AttributeSet Attrs) const {
689 if (!pImpl) return Attrs;
690 if (!Attrs.pImpl) return *this;
693 // FIXME it is not obvious how this should work for alignment. For now, say
694 // we can't change a known alignment.
695 unsigned OldAlign = getParamAlignment(Index);
696 unsigned NewAlign = Attrs.getParamAlignment(Index);
697 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
698 "Attempt to change alignment!");
701 // Add the attribute slots before the one we're trying to add.
702 SmallVector<AttributeSet, 4> AttrSet;
703 uint64_t NumAttrs = pImpl->getNumAttributes();
705 uint64_t LastIndex = 0;
706 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
707 if (getSlotIndex(I) >= Index) {
708 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
712 AttrSet.push_back(getSlotAttributes(I));
715 // Now add the attribute into the correct slot. There may already be an
716 // AttributeSet there.
717 AttrBuilder B(AS, Index);
719 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
720 if (Attrs.getSlotIndex(I) == Index) {
721 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
722 IE = Attrs.pImpl->end(I); II != IE; ++II)
727 AttrSet.push_back(AttributeSet::get(C, Index, B));
729 // Add the remaining attribute slots.
730 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
731 AttrSet.push_back(getSlotAttributes(I));
733 return get(C, AttrSet);
736 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
737 Attribute::AttrKind Attr) const {
738 if (!hasAttribute(Index, Attr)) return *this;
739 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
742 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
743 AttributeSet Attrs) const {
744 if (!pImpl) return AttributeSet();
745 if (!Attrs.pImpl) return *this;
748 // FIXME it is not obvious how this should work for alignment.
749 // For now, say we can't pass in alignment, which no current use does.
750 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
751 "Attempt to change alignment!");
754 // Add the attribute slots before the one we're trying to add.
755 SmallVector<AttributeSet, 4> AttrSet;
756 uint64_t NumAttrs = pImpl->getNumAttributes();
758 uint64_t LastIndex = 0;
759 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
760 if (getSlotIndex(I) >= Index) {
761 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
765 AttrSet.push_back(getSlotAttributes(I));
768 // Now remove the attribute from the correct slot. There may already be an
769 // AttributeSet there.
770 AttrBuilder B(AS, Index);
772 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
773 if (Attrs.getSlotIndex(I) == Index) {
774 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
778 AttrSet.push_back(AttributeSet::get(C, Index, B));
780 // Add the remaining attribute slots.
781 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
782 AttrSet.push_back(getSlotAttributes(I));
784 return get(C, AttrSet);
787 //===----------------------------------------------------------------------===//
788 // AttributeSet Accessor Methods
789 //===----------------------------------------------------------------------===//
791 LLVMContext &AttributeSet::getContext() const {
792 return pImpl->getContext();
795 AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
796 return pImpl && hasAttributes(Index) ?
797 AttributeSet::get(pImpl->getContext(),
798 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
799 std::make_pair(Index, getAttributes(Index)))) :
803 AttributeSet AttributeSet::getRetAttributes() const {
804 return pImpl && hasAttributes(ReturnIndex) ?
805 AttributeSet::get(pImpl->getContext(),
806 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
807 std::make_pair(ReturnIndex,
808 getAttributes(ReturnIndex)))) :
812 AttributeSet AttributeSet::getFnAttributes() const {
813 return pImpl && hasAttributes(FunctionIndex) ?
814 AttributeSet::get(pImpl->getContext(),
815 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
816 std::make_pair(FunctionIndex,
817 getAttributes(FunctionIndex)))) :
821 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
822 AttributeSetNode *ASN = getAttributes(Index);
823 return ASN ? ASN->hasAttribute(Kind) : false;
826 bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
827 AttributeSetNode *ASN = getAttributes(Index);
828 return ASN ? ASN->hasAttribute(Kind) : false;
831 bool AttributeSet::hasAttributes(unsigned Index) const {
832 AttributeSetNode *ASN = getAttributes(Index);
833 return ASN ? ASN->hasAttributes() : false;
836 /// \brief Return true if the specified attribute is set for at least one
837 /// parameter or for the return value.
838 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
839 if (pImpl == 0) return false;
841 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
842 for (AttributeSetImpl::iterator II = pImpl->begin(I),
843 IE = pImpl->end(I); II != IE; ++II)
844 if (II->hasAttribute(Attr))
850 Attribute AttributeSet::getAttribute(unsigned Index,
851 Attribute::AttrKind Kind) const {
852 AttributeSetNode *ASN = getAttributes(Index);
853 return ASN ? ASN->getAttribute(Kind) : Attribute();
856 Attribute AttributeSet::getAttribute(unsigned Index,
857 StringRef Kind) const {
858 AttributeSetNode *ASN = getAttributes(Index);
859 return ASN ? ASN->getAttribute(Kind) : Attribute();
862 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
863 AttributeSetNode *ASN = getAttributes(Index);
864 return ASN ? ASN->getAlignment() : 0;
867 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
868 AttributeSetNode *ASN = getAttributes(Index);
869 return ASN ? ASN->getStackAlignment() : 0;
872 std::string AttributeSet::getAsString(unsigned Index,
873 bool InAttrGrp) const {
874 AttributeSetNode *ASN = getAttributes(Index);
875 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
878 /// \brief The attributes for the specified index are returned.
879 AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
880 if (!pImpl) return 0;
882 // Loop through to find the attribute node we want.
883 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
884 if (pImpl->getSlotIndex(I) == Index)
885 return pImpl->getSlotNode(I);
890 AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
892 return ArrayRef<Attribute>().begin();
893 return pImpl->begin(Slot);
896 AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
898 return ArrayRef<Attribute>().end();
899 return pImpl->end(Slot);
902 //===----------------------------------------------------------------------===//
903 // AttributeSet Introspection Methods
904 //===----------------------------------------------------------------------===//
906 /// \brief Return the number of slots used in this attribute list. This is the
907 /// number of arguments that have an attribute set on them (including the
908 /// function itself).
909 unsigned AttributeSet::getNumSlots() const {
910 return pImpl ? pImpl->getNumAttributes() : 0;
913 unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
914 assert(pImpl && Slot < pImpl->getNumAttributes() &&
915 "Slot # out of range!");
916 return pImpl->getSlotIndex(Slot);
919 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
920 assert(pImpl && Slot < pImpl->getNumAttributes() &&
921 "Slot # out of range!");
922 return pImpl->getSlotAttributes(Slot);
925 uint64_t AttributeSet::Raw(unsigned Index) const {
926 // FIXME: Remove this.
927 return pImpl ? pImpl->Raw(Index) : 0;
930 void AttributeSet::dump() const {
933 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
934 uint64_t Index = getSlotIndex(i);
940 dbgs() << " => " << getAsString(Index) << " }\n";
946 //===----------------------------------------------------------------------===//
947 // AttrBuilder Method Implementations
948 //===----------------------------------------------------------------------===//
950 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
951 : Attrs(0), Alignment(0), StackAlignment(0) {
952 AttributeSetImpl *pImpl = AS.pImpl;
955 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
956 if (pImpl->getSlotIndex(I) != Index) continue;
958 for (AttributeSetImpl::iterator II = pImpl->begin(I),
959 IE = pImpl->end(I); II != IE; ++II)
966 void AttrBuilder::clear() {
968 Alignment = StackAlignment = 0;
971 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
972 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
973 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
974 "Adding alignment attribute without adding alignment value!");
979 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
980 if (Attr.isStringAttribute()) {
981 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
985 Attribute::AttrKind Kind = Attr.getKindAsEnum();
988 if (Kind == Attribute::Alignment)
989 Alignment = Attr.getAlignment();
990 else if (Kind == Attribute::StackAlignment)
991 StackAlignment = Attr.getStackAlignment();
995 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
996 TargetDepAttrs[A] = V;
1000 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
1001 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1004 if (Val == Attribute::Alignment)
1006 else if (Val == Attribute::StackAlignment)
1012 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
1013 unsigned Slot = ~0U;
1014 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1015 if (A.getSlotIndex(I) == Index) {
1020 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
1022 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
1023 Attribute Attr = *I;
1024 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1025 Attribute::AttrKind Kind = I->getKindAsEnum();
1026 Attrs[Kind] = false;
1028 if (Kind == Attribute::Alignment)
1030 else if (Kind == Attribute::StackAlignment)
1033 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1034 std::map<std::string, std::string>::iterator
1035 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1036 if (Iter != TargetDepAttrs.end())
1037 TargetDepAttrs.erase(Iter);
1044 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1045 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1046 if (I != TargetDepAttrs.end())
1047 TargetDepAttrs.erase(I);
1051 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
1052 if (Align == 0) return *this;
1054 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1055 assert(Align <= 0x40000000 && "Alignment too large.");
1057 Attrs[Attribute::Alignment] = true;
1062 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1063 // Default alignment, allow the target to define how to align it.
1064 if (Align == 0) return *this;
1066 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1067 assert(Align <= 0x100 && "Alignment too large.");
1069 Attrs[Attribute::StackAlignment] = true;
1070 StackAlignment = Align;
1074 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1075 // FIXME: What if both have alignments, but they don't match?!
1077 Alignment = B.Alignment;
1079 if (!StackAlignment)
1080 StackAlignment = B.StackAlignment;
1084 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1085 E = B.TargetDepAttrs.end(); I != E; ++I)
1086 TargetDepAttrs[I->first] = I->second;
1091 bool AttrBuilder::contains(StringRef A) const {
1092 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1095 bool AttrBuilder::hasAttributes() const {
1096 return !Attrs.none() || !TargetDepAttrs.empty();
1099 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
1100 unsigned Slot = ~0U;
1101 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1102 if (A.getSlotIndex(I) == Index) {
1107 assert(Slot != ~0U && "Couldn't find the index!");
1109 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
1111 Attribute Attr = *I;
1112 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1113 if (Attrs[I->getKindAsEnum()])
1116 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1117 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1124 bool AttrBuilder::hasAlignmentAttr() const {
1125 return Alignment != 0;
1128 bool AttrBuilder::operator==(const AttrBuilder &B) {
1129 if (Attrs != B.Attrs)
1132 for (td_const_iterator I = TargetDepAttrs.begin(),
1133 E = TargetDepAttrs.end(); I != E; ++I)
1134 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1137 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
1140 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
1141 // FIXME: Remove this in 4.0.
1142 if (!Val) return *this;
1144 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1145 I = Attribute::AttrKind(I + 1)) {
1146 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1149 if (I == Attribute::Alignment)
1150 Alignment = 1ULL << ((A >> 16) - 1);
1151 else if (I == Attribute::StackAlignment)
1152 StackAlignment = 1ULL << ((A >> 26)-1);
1159 //===----------------------------------------------------------------------===//
1160 // AttributeFuncs Function Defintions
1161 //===----------------------------------------------------------------------===//
1163 /// \brief Which attributes cannot be applied to a type.
1164 AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
1165 AttrBuilder Incompatible;
1167 if (!Ty->isIntegerTy())
1168 // Attribute that only apply to integers.
1169 Incompatible.addAttribute(Attribute::SExt)
1170 .addAttribute(Attribute::ZExt);
1172 if (!Ty->isPointerTy())
1173 // Attribute that only apply to pointers.
1174 Incompatible.addAttribute(Attribute::ByVal)
1175 .addAttribute(Attribute::Nest)
1176 .addAttribute(Attribute::NoAlias)
1177 .addAttribute(Attribute::NoCapture)
1178 .addAttribute(Attribute::ReadNone)
1179 .addAttribute(Attribute::ReadOnly)
1180 .addAttribute(Attribute::StructRet)
1181 .addAttribute(Attribute::InAlloca);
1183 return AttributeSet::get(Ty->getContext(), Index, Incompatible);