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 "llvm/IR/Function.h"
18 #include "AttributeImpl.h"
19 #include "LLVMContextImpl.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/IR/Type.h"
23 #include "llvm/Support/Atomic.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ManagedStatic.h"
26 #include "llvm/Support/Mutex.h"
27 #include "llvm/Support/raw_ostream.h"
31 //===----------------------------------------------------------------------===//
32 // Attribute Construction Methods
33 //===----------------------------------------------------------------------===//
35 Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
37 LLVMContextImpl *pImpl = Context.pImpl;
40 if (Val) ID.AddInteger(Val);
43 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
46 // If we didn't find any existing attributes of the same shape then create a
47 // new one and insert it.
49 PA = new EnumAttributeImpl(Kind);
51 PA = new IntAttributeImpl(Kind, Val);
52 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
55 // Return the Attribute that we found or created.
59 Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
60 LLVMContextImpl *pImpl = Context.pImpl;
63 if (!Val.empty()) ID.AddString(Val);
66 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
69 // If we didn't find any existing attributes of the same shape then create a
70 // new one and insert it.
71 PA = new StringAttributeImpl(Kind, Val);
72 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
75 // Return the Attribute that we found or created.
79 Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
80 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
81 assert(Align <= 0x40000000 && "Alignment too large.");
82 return get(Context, Alignment, Align);
85 Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
87 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
88 assert(Align <= 0x100 && "Alignment too large.");
89 return get(Context, StackAlignment, Align);
92 Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context,
94 assert(Bytes && "Bytes must be non-zero.");
95 return get(Context, Dereferenceable, Bytes);
98 Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context,
100 assert(Bytes && "Bytes must be non-zero.");
101 return get(Context, DereferenceableOrNull, Bytes);
104 //===----------------------------------------------------------------------===//
105 // Attribute Accessor Methods
106 //===----------------------------------------------------------------------===//
108 bool Attribute::isEnumAttribute() const {
109 return pImpl && pImpl->isEnumAttribute();
112 bool Attribute::isIntAttribute() const {
113 return pImpl && pImpl->isIntAttribute();
116 bool Attribute::isStringAttribute() const {
117 return pImpl && pImpl->isStringAttribute();
120 Attribute::AttrKind Attribute::getKindAsEnum() const {
121 if (!pImpl) return None;
122 assert((isEnumAttribute() || isIntAttribute()) &&
123 "Invalid attribute type to get the kind as an enum!");
124 return pImpl->getKindAsEnum();
127 uint64_t Attribute::getValueAsInt() const {
128 if (!pImpl) return 0;
129 assert(isIntAttribute() &&
130 "Expected the attribute to be an integer attribute!");
131 return pImpl->getValueAsInt();
134 StringRef Attribute::getKindAsString() const {
135 if (!pImpl) return StringRef();
136 assert(isStringAttribute() &&
137 "Invalid attribute type to get the kind as a string!");
138 return pImpl->getKindAsString();
141 StringRef Attribute::getValueAsString() const {
142 if (!pImpl) return StringRef();
143 assert(isStringAttribute() &&
144 "Invalid attribute type to get the value as a string!");
145 return pImpl->getValueAsString();
148 bool Attribute::hasAttribute(AttrKind Kind) const {
149 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
152 bool Attribute::hasAttribute(StringRef Kind) const {
153 if (!isStringAttribute()) return false;
154 return pImpl && pImpl->hasAttribute(Kind);
157 /// This returns the alignment field of an attribute as a byte alignment value.
158 unsigned Attribute::getAlignment() const {
159 assert(hasAttribute(Attribute::Alignment) &&
160 "Trying to get alignment from non-alignment attribute!");
161 return pImpl->getValueAsInt();
164 /// This returns the stack alignment field of an attribute as a byte alignment
166 unsigned Attribute::getStackAlignment() const {
167 assert(hasAttribute(Attribute::StackAlignment) &&
168 "Trying to get alignment from non-alignment attribute!");
169 return pImpl->getValueAsInt();
172 /// This returns the number of dereferenceable bytes.
173 uint64_t Attribute::getDereferenceableBytes() const {
174 assert(hasAttribute(Attribute::Dereferenceable) &&
175 "Trying to get dereferenceable bytes from "
176 "non-dereferenceable attribute!");
177 return pImpl->getValueAsInt();
180 uint64_t Attribute::getDereferenceableOrNullBytes() const {
181 assert(hasAttribute(Attribute::DereferenceableOrNull) &&
182 "Trying to get dereferenceable bytes from "
183 "non-dereferenceable attribute!");
184 return pImpl->getValueAsInt();
187 std::string Attribute::getAsString(bool InAttrGrp) const {
188 if (!pImpl) return "";
190 if (hasAttribute(Attribute::SanitizeAddress))
191 return "sanitize_address";
192 if (hasAttribute(Attribute::AlwaysInline))
193 return "alwaysinline";
194 if (hasAttribute(Attribute::ArgMemOnly))
196 if (hasAttribute(Attribute::Builtin))
198 if (hasAttribute(Attribute::ByVal))
200 if (hasAttribute(Attribute::Convergent))
202 if (hasAttribute(Attribute::InaccessibleMemOnly))
203 return "inaccessiblememonly";
204 if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly))
205 return "inaccessiblemem_or_argmemonly";
206 if (hasAttribute(Attribute::InAlloca))
208 if (hasAttribute(Attribute::InlineHint))
210 if (hasAttribute(Attribute::InReg))
212 if (hasAttribute(Attribute::JumpTable))
214 if (hasAttribute(Attribute::MinSize))
216 if (hasAttribute(Attribute::Naked))
218 if (hasAttribute(Attribute::Nest))
220 if (hasAttribute(Attribute::NoAlias))
222 if (hasAttribute(Attribute::NoBuiltin))
224 if (hasAttribute(Attribute::NoCapture))
226 if (hasAttribute(Attribute::NoDuplicate))
227 return "noduplicate";
228 if (hasAttribute(Attribute::NoImplicitFloat))
229 return "noimplicitfloat";
230 if (hasAttribute(Attribute::NoInline))
232 if (hasAttribute(Attribute::NonLazyBind))
233 return "nonlazybind";
234 if (hasAttribute(Attribute::NonNull))
236 if (hasAttribute(Attribute::NoRedZone))
238 if (hasAttribute(Attribute::NoReturn))
240 if (hasAttribute(Attribute::NoRecurse))
242 if (hasAttribute(Attribute::NoUnwind))
244 if (hasAttribute(Attribute::OptimizeNone))
246 if (hasAttribute(Attribute::OptimizeForSize))
248 if (hasAttribute(Attribute::ReadNone))
250 if (hasAttribute(Attribute::ReadOnly))
252 if (hasAttribute(Attribute::Returned))
254 if (hasAttribute(Attribute::ReturnsTwice))
255 return "returns_twice";
256 if (hasAttribute(Attribute::SExt))
258 if (hasAttribute(Attribute::StackProtect))
260 if (hasAttribute(Attribute::StackProtectReq))
262 if (hasAttribute(Attribute::StackProtectStrong))
264 if (hasAttribute(Attribute::SafeStack))
266 if (hasAttribute(Attribute::StructRet))
268 if (hasAttribute(Attribute::SanitizeThread))
269 return "sanitize_thread";
270 if (hasAttribute(Attribute::SanitizeMemory))
271 return "sanitize_memory";
272 if (hasAttribute(Attribute::UWTable))
274 if (hasAttribute(Attribute::ZExt))
276 if (hasAttribute(Attribute::Cold))
279 // FIXME: These should be output like this:
284 if (hasAttribute(Attribute::Alignment)) {
287 Result += (InAttrGrp) ? "=" : " ";
288 Result += utostr(getValueAsInt());
292 auto AttrWithBytesToString = [&](const char *Name) {
297 Result += utostr(getValueAsInt());
300 Result += utostr(getValueAsInt());
306 if (hasAttribute(Attribute::StackAlignment))
307 return AttrWithBytesToString("alignstack");
309 if (hasAttribute(Attribute::Dereferenceable))
310 return AttrWithBytesToString("dereferenceable");
312 if (hasAttribute(Attribute::DereferenceableOrNull))
313 return AttrWithBytesToString("dereferenceable_or_null");
315 // Convert target-dependent attributes to strings of the form:
320 if (isStringAttribute()) {
322 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
324 StringRef Val = pImpl->getValueAsString();
325 if (Val.empty()) return Result;
327 Result += ("=\"" + Val + Twine('"')).str();
331 llvm_unreachable("Unknown attribute");
334 bool Attribute::operator<(Attribute A) const {
335 if (!pImpl && !A.pImpl) return false;
336 if (!pImpl) return true;
337 if (!A.pImpl) return false;
338 return *pImpl < *A.pImpl;
341 //===----------------------------------------------------------------------===//
342 // AttributeImpl Definition
343 //===----------------------------------------------------------------------===//
345 // Pin the vtables to this file.
346 AttributeImpl::~AttributeImpl() {}
347 void EnumAttributeImpl::anchor() {}
348 void IntAttributeImpl::anchor() {}
349 void StringAttributeImpl::anchor() {}
351 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
352 if (isStringAttribute()) return false;
353 return getKindAsEnum() == A;
356 bool AttributeImpl::hasAttribute(StringRef Kind) const {
357 if (!isStringAttribute()) return false;
358 return getKindAsString() == Kind;
361 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
362 assert(isEnumAttribute() || isIntAttribute());
363 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
366 uint64_t AttributeImpl::getValueAsInt() const {
367 assert(isIntAttribute());
368 return static_cast<const IntAttributeImpl *>(this)->getValue();
371 StringRef AttributeImpl::getKindAsString() const {
372 assert(isStringAttribute());
373 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
376 StringRef AttributeImpl::getValueAsString() const {
377 assert(isStringAttribute());
378 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
381 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
382 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
383 // relative to their enum value) and then strings.
384 if (isEnumAttribute()) {
385 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
386 if (AI.isIntAttribute()) return true;
387 if (AI.isStringAttribute()) return true;
390 if (isIntAttribute()) {
391 if (AI.isEnumAttribute()) return false;
392 if (AI.isIntAttribute()) return getValueAsInt() < AI.getValueAsInt();
393 if (AI.isStringAttribute()) return true;
396 if (AI.isEnumAttribute()) return false;
397 if (AI.isIntAttribute()) return false;
398 if (getKindAsString() == AI.getKindAsString())
399 return getValueAsString() < AI.getValueAsString();
400 return getKindAsString() < AI.getKindAsString();
403 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
404 // FIXME: Remove this.
406 case Attribute::EndAttrKinds:
407 llvm_unreachable("Synthetic enumerators which should never get here");
409 case Attribute::None: return 0;
410 case Attribute::ZExt: return 1 << 0;
411 case Attribute::SExt: return 1 << 1;
412 case Attribute::NoReturn: return 1 << 2;
413 case Attribute::InReg: return 1 << 3;
414 case Attribute::StructRet: return 1 << 4;
415 case Attribute::NoUnwind: return 1 << 5;
416 case Attribute::NoAlias: return 1 << 6;
417 case Attribute::ByVal: return 1 << 7;
418 case Attribute::Nest: return 1 << 8;
419 case Attribute::ReadNone: return 1 << 9;
420 case Attribute::ReadOnly: return 1 << 10;
421 case Attribute::NoInline: return 1 << 11;
422 case Attribute::AlwaysInline: return 1 << 12;
423 case Attribute::OptimizeForSize: return 1 << 13;
424 case Attribute::StackProtect: return 1 << 14;
425 case Attribute::StackProtectReq: return 1 << 15;
426 case Attribute::Alignment: return 31 << 16;
427 case Attribute::NoCapture: return 1 << 21;
428 case Attribute::NoRedZone: return 1 << 22;
429 case Attribute::NoImplicitFloat: return 1 << 23;
430 case Attribute::Naked: return 1 << 24;
431 case Attribute::InlineHint: return 1 << 25;
432 case Attribute::StackAlignment: return 7 << 26;
433 case Attribute::ReturnsTwice: return 1 << 29;
434 case Attribute::UWTable: return 1 << 30;
435 case Attribute::NonLazyBind: return 1U << 31;
436 case Attribute::SanitizeAddress: return 1ULL << 32;
437 case Attribute::MinSize: return 1ULL << 33;
438 case Attribute::NoDuplicate: return 1ULL << 34;
439 case Attribute::StackProtectStrong: return 1ULL << 35;
440 case Attribute::SanitizeThread: return 1ULL << 36;
441 case Attribute::SanitizeMemory: return 1ULL << 37;
442 case Attribute::NoBuiltin: return 1ULL << 38;
443 case Attribute::Returned: return 1ULL << 39;
444 case Attribute::Cold: return 1ULL << 40;
445 case Attribute::Builtin: return 1ULL << 41;
446 case Attribute::OptimizeNone: return 1ULL << 42;
447 case Attribute::InAlloca: return 1ULL << 43;
448 case Attribute::NonNull: return 1ULL << 44;
449 case Attribute::JumpTable: return 1ULL << 45;
450 case Attribute::Convergent: return 1ULL << 46;
451 case Attribute::SafeStack: return 1ULL << 47;
452 case Attribute::NoRecurse: return 1ULL << 48;
453 case Attribute::InaccessibleMemOnly: return 1ULL << 49;
454 case Attribute::InaccessibleMemOrArgMemOnly: return 1ULL << 50;
455 case Attribute::Dereferenceable:
456 llvm_unreachable("dereferenceable attribute not supported in raw format");
458 case Attribute::DereferenceableOrNull:
459 llvm_unreachable("dereferenceable_or_null attribute not supported in raw "
462 case Attribute::ArgMemOnly:
463 llvm_unreachable("argmemonly attribute not supported in raw format");
466 llvm_unreachable("Unsupported attribute type");
469 //===----------------------------------------------------------------------===//
470 // AttributeSetNode Definition
471 //===----------------------------------------------------------------------===//
473 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
474 ArrayRef<Attribute> Attrs) {
478 // Otherwise, build a key to look up the existing attributes.
479 LLVMContextImpl *pImpl = C.pImpl;
482 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
483 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
485 for (Attribute Attr : SortedAttrs)
489 AttributeSetNode *PA =
490 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
492 // If we didn't find any existing attributes of the same shape then create a
493 // new one and insert it.
495 // Coallocate entries after the AttributeSetNode itself.
496 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
497 PA = new (Mem) AttributeSetNode(SortedAttrs);
498 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
501 // Return the AttributesListNode that we found or created.
505 bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
506 for (iterator I = begin(), E = end(); I != E; ++I)
507 if (I->hasAttribute(Kind))
512 bool AttributeSetNode::hasAttribute(StringRef Kind) const {
513 for (iterator I = begin(), E = end(); I != E; ++I)
514 if (I->hasAttribute(Kind))
519 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
520 for (iterator I = begin(), E = end(); I != E; ++I)
521 if (I->hasAttribute(Kind))
526 Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
527 for (iterator I = begin(), E = end(); I != E; ++I)
528 if (I->hasAttribute(Kind))
533 unsigned AttributeSetNode::getAlignment() const {
534 for (iterator I = begin(), E = end(); I != E; ++I)
535 if (I->hasAttribute(Attribute::Alignment))
536 return I->getAlignment();
540 unsigned AttributeSetNode::getStackAlignment() const {
541 for (iterator I = begin(), E = end(); I != E; ++I)
542 if (I->hasAttribute(Attribute::StackAlignment))
543 return I->getStackAlignment();
547 uint64_t AttributeSetNode::getDereferenceableBytes() const {
548 for (iterator I = begin(), E = end(); I != E; ++I)
549 if (I->hasAttribute(Attribute::Dereferenceable))
550 return I->getDereferenceableBytes();
554 uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
555 for (iterator I = begin(), E = end(); I != E; ++I)
556 if (I->hasAttribute(Attribute::DereferenceableOrNull))
557 return I->getDereferenceableOrNullBytes();
561 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
563 for (iterator I = begin(), E = end(); I != E; ++I) {
566 Str += I->getAsString(InAttrGrp);
571 //===----------------------------------------------------------------------===//
572 // AttributeSetImpl Definition
573 //===----------------------------------------------------------------------===//
575 uint64_t AttributeSetImpl::Raw(unsigned Index) const {
576 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
577 if (getSlotIndex(I) != Index) continue;
578 const AttributeSetNode *ASN = getSlotNode(I);
581 for (AttributeSetNode::iterator II = ASN->begin(),
582 IE = ASN->end(); II != IE; ++II) {
583 Attribute Attr = *II;
585 // This cannot handle string attributes.
586 if (Attr.isStringAttribute()) continue;
588 Attribute::AttrKind Kind = Attr.getKindAsEnum();
590 if (Kind == Attribute::Alignment)
591 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
592 else if (Kind == Attribute::StackAlignment)
593 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
594 else if (Kind == Attribute::Dereferenceable)
595 llvm_unreachable("dereferenceable not supported in bit mask");
597 Mask |= AttributeImpl::getAttrMask(Kind);
606 void AttributeSetImpl::dump() const {
607 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
610 //===----------------------------------------------------------------------===//
611 // AttributeSet Construction and Mutation Methods
612 //===----------------------------------------------------------------------===//
615 AttributeSet::getImpl(LLVMContext &C,
616 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
617 LLVMContextImpl *pImpl = C.pImpl;
619 AttributeSetImpl::Profile(ID, Attrs);
622 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
624 // If we didn't find any existing attributes of the same shape then
625 // create a new one and insert it.
627 // Coallocate entries after the AttributeSetImpl itself.
628 void *Mem = ::operator new(
629 AttributeSetImpl::totalSizeToAlloc<IndexAttrPair>(Attrs.size()));
630 PA = new (Mem) AttributeSetImpl(C, Attrs);
631 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
634 // Return the AttributesList that we found or created.
635 return AttributeSet(PA);
638 AttributeSet AttributeSet::get(LLVMContext &C,
639 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
640 // If there are no attributes then return a null AttributesList pointer.
642 return AttributeSet();
645 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
646 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
647 "Misordered Attributes list!");
648 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
649 "Pointless attribute!");
653 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
655 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
656 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
657 E = Attrs.end(); I != E; ) {
658 unsigned Index = I->first;
659 SmallVector<Attribute, 4> AttrVec;
660 while (I != E && I->first == Index) {
661 AttrVec.push_back(I->second);
665 AttrPairVec.push_back(std::make_pair(Index,
666 AttributeSetNode::get(C, AttrVec)));
669 return getImpl(C, AttrPairVec);
672 AttributeSet AttributeSet::get(LLVMContext &C,
673 ArrayRef<std::pair<unsigned,
674 AttributeSetNode*> > Attrs) {
675 // If there are no attributes then return a null AttributesList pointer.
677 return AttributeSet();
679 return getImpl(C, Attrs);
682 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
683 const AttrBuilder &B) {
684 if (!B.hasAttributes())
685 return AttributeSet();
687 // Add target-independent attributes.
688 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
689 for (Attribute::AttrKind Kind = Attribute::None;
690 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
691 if (!B.contains(Kind))
696 case Attribute::Alignment:
697 Attr = Attribute::getWithAlignment(C, B.getAlignment());
699 case Attribute::StackAlignment:
700 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
702 case Attribute::Dereferenceable:
703 Attr = Attribute::getWithDereferenceableBytes(
704 C, B.getDereferenceableBytes());
706 case Attribute::DereferenceableOrNull:
707 Attr = Attribute::getWithDereferenceableOrNullBytes(
708 C, B.getDereferenceableOrNullBytes());
711 Attr = Attribute::get(C, Kind);
713 Attrs.push_back(std::make_pair(Index, Attr));
716 // Add target-dependent (string) attributes.
717 for (const AttrBuilder::td_type &TDA : B.td_attrs())
719 std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
721 return get(C, Attrs);
724 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
725 ArrayRef<Attribute::AttrKind> Kind) {
726 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
727 for (Attribute::AttrKind K : Kind)
728 Attrs.push_back(std::make_pair(Index, Attribute::get(C, K)));
729 return get(C, Attrs);
732 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
733 if (Attrs.empty()) return AttributeSet();
734 if (Attrs.size() == 1) return Attrs[0];
736 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
737 AttributeSetImpl *A0 = Attrs[0].pImpl;
739 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
740 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
741 // ordered by index. Because we know that each list in Attrs is ordered by
742 // index we only need to merge each successive list in rather than doing a
744 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
745 AttributeSetImpl *AS = Attrs[I].pImpl;
747 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
748 ANVI = AttrNodeVec.begin(), ANVE;
749 for (const IndexAttrPair *AI = AS->getNode(0),
750 *AE = AS->getNode(AS->getNumAttributes());
752 ANVE = AttrNodeVec.end();
753 while (ANVI != ANVE && ANVI->first <= AI->first)
755 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
759 return getImpl(C, AttrNodeVec);
762 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
763 Attribute::AttrKind Attr) const {
764 if (hasAttribute(Index, Attr)) return *this;
765 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
768 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
769 StringRef Kind) const {
771 B.addAttribute(Kind);
772 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
775 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
776 StringRef Kind, StringRef Value) const {
778 B.addAttribute(Kind, Value);
779 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
782 AttributeSet AttributeSet::addAttribute(LLVMContext &C,
783 ArrayRef<unsigned> Indices,
785 unsigned I = 0, E = pImpl ? pImpl->getNumAttributes() : 0;
786 auto IdxI = Indices.begin(), IdxE = Indices.end();
787 SmallVector<AttributeSet, 4> AttrSet;
789 while (I != E && IdxI != IdxE) {
790 if (getSlotIndex(I) < *IdxI)
791 AttrSet.emplace_back(getSlotAttributes(I++));
792 else if (getSlotIndex(I) > *IdxI)
793 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
795 AttrBuilder B(getSlotAttributes(I), *IdxI);
797 AttrSet.emplace_back(AttributeSet::get(C, *IdxI, B));
804 AttrSet.emplace_back(getSlotAttributes(I++));
807 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
809 return get(C, AttrSet);
812 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
813 AttributeSet Attrs) const {
814 if (!pImpl) return Attrs;
815 if (!Attrs.pImpl) return *this;
818 // FIXME it is not obvious how this should work for alignment. For now, say
819 // we can't change a known alignment.
820 unsigned OldAlign = getParamAlignment(Index);
821 unsigned NewAlign = Attrs.getParamAlignment(Index);
822 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
823 "Attempt to change alignment!");
826 // Add the attribute slots before the one we're trying to add.
827 SmallVector<AttributeSet, 4> AttrSet;
828 uint64_t NumAttrs = pImpl->getNumAttributes();
830 uint64_t LastIndex = 0;
831 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
832 if (getSlotIndex(I) >= Index) {
833 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
837 AttrSet.push_back(getSlotAttributes(I));
840 // Now add the attribute into the correct slot. There may already be an
841 // AttributeSet there.
842 AttrBuilder B(AS, Index);
844 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
845 if (Attrs.getSlotIndex(I) == Index) {
846 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
847 IE = Attrs.pImpl->end(I); II != IE; ++II)
852 AttrSet.push_back(AttributeSet::get(C, Index, B));
854 // Add the remaining attribute slots.
855 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
856 AttrSet.push_back(getSlotAttributes(I));
858 return get(C, AttrSet);
861 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
862 Attribute::AttrKind Attr) const {
863 if (!hasAttribute(Index, Attr)) return *this;
864 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
867 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
868 AttributeSet Attrs) const {
869 if (!pImpl) return AttributeSet();
870 if (!Attrs.pImpl) return *this;
872 // FIXME it is not obvious how this should work for alignment.
873 // For now, say we can't pass in alignment, which no current use does.
874 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
875 "Attempt to change alignment!");
877 // Add the attribute slots before the one we're trying to add.
878 SmallVector<AttributeSet, 4> AttrSet;
879 uint64_t NumAttrs = pImpl->getNumAttributes();
881 uint64_t LastIndex = 0;
882 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
883 if (getSlotIndex(I) >= Index) {
884 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
888 AttrSet.push_back(getSlotAttributes(I));
891 // Now remove the attribute from the correct slot. There may already be an
892 // AttributeSet there.
893 AttrBuilder B(AS, Index);
895 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
896 if (Attrs.getSlotIndex(I) == Index) {
897 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
901 AttrSet.push_back(AttributeSet::get(C, Index, B));
903 // Add the remaining attribute slots.
904 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
905 AttrSet.push_back(getSlotAttributes(I));
907 return get(C, AttrSet);
910 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
911 const AttrBuilder &Attrs) const {
912 if (!pImpl) return AttributeSet();
914 // FIXME it is not obvious how this should work for alignment.
915 // For now, say we can't pass in alignment, which no current use does.
916 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
918 // Add the attribute slots before the one we're trying to add.
919 SmallVector<AttributeSet, 4> AttrSet;
920 uint64_t NumAttrs = pImpl->getNumAttributes();
922 uint64_t LastIndex = 0;
923 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
924 if (getSlotIndex(I) >= Index) {
925 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
929 AttrSet.push_back(getSlotAttributes(I));
932 // Now remove the attribute from the correct slot. There may already be an
933 // AttributeSet there.
934 AttrBuilder B(AS, Index);
937 AttrSet.push_back(AttributeSet::get(C, Index, B));
939 // Add the remaining attribute slots.
940 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
941 AttrSet.push_back(getSlotAttributes(I));
943 return get(C, AttrSet);
946 AttributeSet AttributeSet::addDereferenceableAttr(LLVMContext &C, unsigned Index,
947 uint64_t Bytes) const {
949 B.addDereferenceableAttr(Bytes);
950 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
953 AttributeSet AttributeSet::addDereferenceableOrNullAttr(LLVMContext &C,
955 uint64_t Bytes) const {
957 B.addDereferenceableOrNullAttr(Bytes);
958 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
961 //===----------------------------------------------------------------------===//
962 // AttributeSet Accessor Methods
963 //===----------------------------------------------------------------------===//
965 LLVMContext &AttributeSet::getContext() const {
966 return pImpl->getContext();
969 AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
970 return pImpl && hasAttributes(Index) ?
971 AttributeSet::get(pImpl->getContext(),
972 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
973 std::make_pair(Index, getAttributes(Index)))) :
977 AttributeSet AttributeSet::getRetAttributes() const {
978 return pImpl && hasAttributes(ReturnIndex) ?
979 AttributeSet::get(pImpl->getContext(),
980 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
981 std::make_pair(ReturnIndex,
982 getAttributes(ReturnIndex)))) :
986 AttributeSet AttributeSet::getFnAttributes() const {
987 return pImpl && hasAttributes(FunctionIndex) ?
988 AttributeSet::get(pImpl->getContext(),
989 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
990 std::make_pair(FunctionIndex,
991 getAttributes(FunctionIndex)))) :
995 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
996 AttributeSetNode *ASN = getAttributes(Index);
997 return ASN && ASN->hasAttribute(Kind);
1000 bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
1001 AttributeSetNode *ASN = getAttributes(Index);
1002 return ASN && ASN->hasAttribute(Kind);
1005 bool AttributeSet::hasAttributes(unsigned Index) const {
1006 AttributeSetNode *ASN = getAttributes(Index);
1007 return ASN && ASN->hasAttributes();
1010 /// \brief Return true if the specified attribute is set for at least one
1011 /// parameter or for the return value.
1012 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
1013 if (!pImpl) return false;
1015 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
1016 for (AttributeSetImpl::iterator II = pImpl->begin(I),
1017 IE = pImpl->end(I); II != IE; ++II)
1018 if (II->hasAttribute(Attr))
1024 Attribute AttributeSet::getAttribute(unsigned Index,
1025 Attribute::AttrKind Kind) const {
1026 AttributeSetNode *ASN = getAttributes(Index);
1027 return ASN ? ASN->getAttribute(Kind) : Attribute();
1030 Attribute AttributeSet::getAttribute(unsigned Index,
1031 StringRef Kind) const {
1032 AttributeSetNode *ASN = getAttributes(Index);
1033 return ASN ? ASN->getAttribute(Kind) : Attribute();
1036 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
1037 AttributeSetNode *ASN = getAttributes(Index);
1038 return ASN ? ASN->getAlignment() : 0;
1041 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
1042 AttributeSetNode *ASN = getAttributes(Index);
1043 return ASN ? ASN->getStackAlignment() : 0;
1046 uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
1047 AttributeSetNode *ASN = getAttributes(Index);
1048 return ASN ? ASN->getDereferenceableBytes() : 0;
1051 uint64_t AttributeSet::getDereferenceableOrNullBytes(unsigned Index) const {
1052 AttributeSetNode *ASN = getAttributes(Index);
1053 return ASN ? ASN->getDereferenceableOrNullBytes() : 0;
1056 std::string AttributeSet::getAsString(unsigned Index,
1057 bool InAttrGrp) const {
1058 AttributeSetNode *ASN = getAttributes(Index);
1059 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
1062 /// \brief The attributes for the specified index are returned.
1063 AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
1064 if (!pImpl) return nullptr;
1066 // Loop through to find the attribute node we want.
1067 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
1068 if (pImpl->getSlotIndex(I) == Index)
1069 return pImpl->getSlotNode(I);
1074 AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
1076 return ArrayRef<Attribute>().begin();
1077 return pImpl->begin(Slot);
1080 AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
1082 return ArrayRef<Attribute>().end();
1083 return pImpl->end(Slot);
1086 //===----------------------------------------------------------------------===//
1087 // AttributeSet Introspection Methods
1088 //===----------------------------------------------------------------------===//
1090 /// \brief Return the number of slots used in this attribute list. This is the
1091 /// number of arguments that have an attribute set on them (including the
1092 /// function itself).
1093 unsigned AttributeSet::getNumSlots() const {
1094 return pImpl ? pImpl->getNumAttributes() : 0;
1097 unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
1098 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1099 "Slot # out of range!");
1100 return pImpl->getSlotIndex(Slot);
1103 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
1104 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1105 "Slot # out of range!");
1106 return pImpl->getSlotAttributes(Slot);
1109 uint64_t AttributeSet::Raw(unsigned Index) const {
1110 // FIXME: Remove this.
1111 return pImpl ? pImpl->Raw(Index) : 0;
1114 void AttributeSet::dump() const {
1117 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1118 uint64_t Index = getSlotIndex(i);
1124 dbgs() << " => " << getAsString(Index) << " }\n";
1130 //===----------------------------------------------------------------------===//
1131 // AttrBuilder Method Implementations
1132 //===----------------------------------------------------------------------===//
1134 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
1135 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
1136 DerefOrNullBytes(0) {
1137 AttributeSetImpl *pImpl = AS.pImpl;
1140 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
1141 if (pImpl->getSlotIndex(I) != Index) continue;
1143 for (AttributeSetImpl::iterator II = pImpl->begin(I),
1144 IE = pImpl->end(I); II != IE; ++II)
1151 void AttrBuilder::clear() {
1153 TargetDepAttrs.clear();
1154 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
1157 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
1158 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1159 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
1160 Val != Attribute::Dereferenceable &&
1161 "Adding integer attribute without adding a value!");
1166 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
1167 if (Attr.isStringAttribute()) {
1168 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1172 Attribute::AttrKind Kind = Attr.getKindAsEnum();
1175 if (Kind == Attribute::Alignment)
1176 Alignment = Attr.getAlignment();
1177 else if (Kind == Attribute::StackAlignment)
1178 StackAlignment = Attr.getStackAlignment();
1179 else if (Kind == Attribute::Dereferenceable)
1180 DerefBytes = Attr.getDereferenceableBytes();
1181 else if (Kind == Attribute::DereferenceableOrNull)
1182 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
1186 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1187 TargetDepAttrs[A] = V;
1191 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
1192 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1195 if (Val == Attribute::Alignment)
1197 else if (Val == Attribute::StackAlignment)
1199 else if (Val == Attribute::Dereferenceable)
1201 else if (Val == Attribute::DereferenceableOrNull)
1202 DerefOrNullBytes = 0;
1207 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
1208 unsigned Slot = ~0U;
1209 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1210 if (A.getSlotIndex(I) == Index) {
1215 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
1217 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
1218 Attribute Attr = *I;
1219 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1220 removeAttribute(Attr.getKindAsEnum());
1222 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1223 removeAttribute(Attr.getKindAsString());
1230 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1231 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1232 if (I != TargetDepAttrs.end())
1233 TargetDepAttrs.erase(I);
1237 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
1238 if (Align == 0) return *this;
1240 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1241 assert(Align <= 0x40000000 && "Alignment too large.");
1243 Attrs[Attribute::Alignment] = true;
1248 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1249 // Default alignment, allow the target to define how to align it.
1250 if (Align == 0) return *this;
1252 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1253 assert(Align <= 0x100 && "Alignment too large.");
1255 Attrs[Attribute::StackAlignment] = true;
1256 StackAlignment = Align;
1260 AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1261 if (Bytes == 0) return *this;
1263 Attrs[Attribute::Dereferenceable] = true;
1268 AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1272 Attrs[Attribute::DereferenceableOrNull] = true;
1273 DerefOrNullBytes = Bytes;
1277 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1278 // FIXME: What if both have alignments, but they don't match?!
1280 Alignment = B.Alignment;
1282 if (!StackAlignment)
1283 StackAlignment = B.StackAlignment;
1286 DerefBytes = B.DerefBytes;
1288 if (!DerefOrNullBytes)
1289 DerefOrNullBytes = B.DerefOrNullBytes;
1293 for (auto I : B.td_attrs())
1294 TargetDepAttrs[I.first] = I.second;
1299 AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1300 // FIXME: What if both have alignments, but they don't match?!
1304 if (B.StackAlignment)
1310 if (B.DerefOrNullBytes)
1311 DerefOrNullBytes = 0;
1315 for (auto I : B.td_attrs())
1316 TargetDepAttrs.erase(I.first);
1321 bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1322 // First check if any of the target independent attributes overlap.
1323 if ((Attrs & B.Attrs).any())
1326 // Then check if any target dependent ones do.
1327 for (auto I : td_attrs())
1328 if (B.contains(I.first))
1334 bool AttrBuilder::contains(StringRef A) const {
1335 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1338 bool AttrBuilder::hasAttributes() const {
1339 return !Attrs.none() || !TargetDepAttrs.empty();
1342 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
1343 unsigned Slot = ~0U;
1344 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1345 if (A.getSlotIndex(I) == Index) {
1350 assert(Slot != ~0U && "Couldn't find the index!");
1352 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
1353 Attribute Attr = *I;
1354 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1355 if (Attrs[I->getKindAsEnum()])
1358 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1359 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1366 bool AttrBuilder::hasAlignmentAttr() const {
1367 return Alignment != 0;
1370 bool AttrBuilder::operator==(const AttrBuilder &B) {
1371 if (Attrs != B.Attrs)
1374 for (td_const_iterator I = TargetDepAttrs.begin(),
1375 E = TargetDepAttrs.end(); I != E; ++I)
1376 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1379 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1380 DerefBytes == B.DerefBytes;
1383 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
1384 // FIXME: Remove this in 4.0.
1385 if (!Val) return *this;
1387 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1388 I = Attribute::AttrKind(I + 1)) {
1389 if (I == Attribute::Dereferenceable ||
1390 I == Attribute::DereferenceableOrNull ||
1391 I == Attribute::ArgMemOnly)
1393 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1396 if (I == Attribute::Alignment)
1397 Alignment = 1ULL << ((A >> 16) - 1);
1398 else if (I == Attribute::StackAlignment)
1399 StackAlignment = 1ULL << ((A >> 26)-1);
1406 //===----------------------------------------------------------------------===//
1407 // AttributeFuncs Function Defintions
1408 //===----------------------------------------------------------------------===//
1410 /// \brief Which attributes cannot be applied to a type.
1411 AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
1412 AttrBuilder Incompatible;
1414 if (!Ty->isIntegerTy())
1415 // Attribute that only apply to integers.
1416 Incompatible.addAttribute(Attribute::SExt)
1417 .addAttribute(Attribute::ZExt);
1419 if (!Ty->isPointerTy())
1420 // Attribute that only apply to pointers.
1421 Incompatible.addAttribute(Attribute::ByVal)
1422 .addAttribute(Attribute::Nest)
1423 .addAttribute(Attribute::NoAlias)
1424 .addAttribute(Attribute::NoCapture)
1425 .addAttribute(Attribute::NonNull)
1426 .addDereferenceableAttr(1) // the int here is ignored
1427 .addDereferenceableOrNullAttr(1) // the int here is ignored
1428 .addAttribute(Attribute::ReadNone)
1429 .addAttribute(Attribute::ReadOnly)
1430 .addAttribute(Attribute::StructRet)
1431 .addAttribute(Attribute::InAlloca);
1433 return Incompatible;
1436 template<typename AttrClass>
1437 static bool isEqual(const Function &Caller, const Function &Callee) {
1438 return Caller.getFnAttribute(AttrClass::getKind()) ==
1439 Callee.getFnAttribute(AttrClass::getKind());
1442 /// \brief Compute the logical AND of the attributes of the caller and the
1445 /// This function sets the caller's attribute to false if the callee's attribute
1447 template<typename AttrClass>
1448 static void setAND(Function &Caller, const Function &Callee) {
1449 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1450 !AttrClass::isSet(Callee, AttrClass::getKind()))
1451 AttrClass::set(Caller, AttrClass::getKind(), false);
1454 /// \brief Compute the logical OR of the attributes of the caller and the
1457 /// This function sets the caller's attribute to true if the callee's attribute
1459 template<typename AttrClass>
1460 static void setOR(Function &Caller, const Function &Callee) {
1461 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1462 AttrClass::isSet(Callee, AttrClass::getKind()))
1463 AttrClass::set(Caller, AttrClass::getKind(), true);
1466 /// \brief If the inlined function had a higher stack protection level than the
1467 /// calling function, then bump up the caller's stack protection level.
1468 static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1469 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1470 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1471 // clutter to the IR.
1473 B.addAttribute(Attribute::StackProtect)
1474 .addAttribute(Attribute::StackProtectStrong)
1475 .addAttribute(Attribute::StackProtectReq);
1476 AttributeSet OldSSPAttr = AttributeSet::get(Caller.getContext(),
1477 AttributeSet::FunctionIndex,
1480 if (Callee.hasFnAttribute(Attribute::SafeStack)) {
1481 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
1482 Caller.addFnAttr(Attribute::SafeStack);
1483 } else if (Callee.hasFnAttribute(Attribute::StackProtectReq) &&
1484 !Caller.hasFnAttribute(Attribute::SafeStack)) {
1485 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
1486 Caller.addFnAttr(Attribute::StackProtectReq);
1487 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
1488 !Caller.hasFnAttribute(Attribute::SafeStack) &&
1489 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
1490 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
1491 Caller.addFnAttr(Attribute::StackProtectStrong);
1492 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
1493 !Caller.hasFnAttribute(Attribute::SafeStack) &&
1494 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1495 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1496 Caller.addFnAttr(Attribute::StackProtect);
1499 #define GET_ATTR_COMPAT_FUNC
1500 #include "AttributesCompatFunc.inc"
1502 bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1503 const Function &Callee) {
1504 return hasCompatibleFnAttrs(Caller, Callee);
1508 void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1509 const Function &Callee) {
1510 mergeFnAttrs(Caller, Callee);