Another attempt to fix the build bot breaks after r360426
[llvm-core.git] / lib / IR / Attributes.cpp
blob0be09a05e82abb24786d2296bc5639c2e03ca321
1 //===- Attributes.cpp - Implement AttributesList --------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // \file
10 // This file implements the Attribute, AttributeImpl, AttrBuilder,
11 // AttributeListImpl, and AttributeList classes.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/IR/Attributes.h"
16 #include "AttributeImpl.h"
17 #include "LLVMContextImpl.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/FoldingSet.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/ADT/Twine.h"
26 #include "llvm/Config/llvm-config.h"
27 #include "llvm/IR/Function.h"
28 #include "llvm/IR/LLVMContext.h"
29 #include "llvm/IR/Type.h"
30 #include "llvm/Support/Compiler.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/MathExtras.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include <algorithm>
36 #include <cassert>
37 #include <climits>
38 #include <cstddef>
39 #include <cstdint>
40 #include <limits>
41 #include <string>
42 #include <tuple>
43 #include <utility>
45 using namespace llvm;
47 //===----------------------------------------------------------------------===//
48 // Attribute Construction Methods
49 //===----------------------------------------------------------------------===//
51 // allocsize has two integer arguments, but because they're both 32 bits, we can
52 // pack them into one 64-bit value, at the cost of making said value
53 // nonsensical.
55 // In order to do this, we need to reserve one value of the second (optional)
56 // allocsize argument to signify "not present."
57 static const unsigned AllocSizeNumElemsNotPresent = -1;
59 static uint64_t packAllocSizeArgs(unsigned ElemSizeArg,
60 const Optional<unsigned> &NumElemsArg) {
61 assert((!NumElemsArg.hasValue() ||
62 *NumElemsArg != AllocSizeNumElemsNotPresent) &&
63 "Attempting to pack a reserved value");
65 return uint64_t(ElemSizeArg) << 32 |
66 NumElemsArg.getValueOr(AllocSizeNumElemsNotPresent);
69 static std::pair<unsigned, Optional<unsigned>>
70 unpackAllocSizeArgs(uint64_t Num) {
71 unsigned NumElems = Num & std::numeric_limits<unsigned>::max();
72 unsigned ElemSizeArg = Num >> 32;
74 Optional<unsigned> NumElemsArg;
75 if (NumElems != AllocSizeNumElemsNotPresent)
76 NumElemsArg = NumElems;
77 return std::make_pair(ElemSizeArg, NumElemsArg);
80 Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
81 uint64_t Val) {
82 LLVMContextImpl *pImpl = Context.pImpl;
83 FoldingSetNodeID ID;
84 ID.AddInteger(Kind);
85 if (Val) ID.AddInteger(Val);
87 void *InsertPoint;
88 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
90 if (!PA) {
91 // If we didn't find any existing attributes of the same shape then create a
92 // new one and insert it.
93 if (!Val)
94 PA = new EnumAttributeImpl(Kind);
95 else
96 PA = new IntAttributeImpl(Kind, Val);
97 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
100 // Return the Attribute that we found or created.
101 return Attribute(PA);
104 Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
105 LLVMContextImpl *pImpl = Context.pImpl;
106 FoldingSetNodeID ID;
107 ID.AddString(Kind);
108 if (!Val.empty()) ID.AddString(Val);
110 void *InsertPoint;
111 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
113 if (!PA) {
114 // If we didn't find any existing attributes of the same shape then create a
115 // new one and insert it.
116 PA = new StringAttributeImpl(Kind, Val);
117 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
120 // Return the Attribute that we found or created.
121 return Attribute(PA);
124 Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
125 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
126 assert(Align <= 0x40000000 && "Alignment too large.");
127 return get(Context, Alignment, Align);
130 Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
131 uint64_t Align) {
132 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
133 assert(Align <= 0x100 && "Alignment too large.");
134 return get(Context, StackAlignment, Align);
137 Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context,
138 uint64_t Bytes) {
139 assert(Bytes && "Bytes must be non-zero.");
140 return get(Context, Dereferenceable, Bytes);
143 Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context,
144 uint64_t Bytes) {
145 assert(Bytes && "Bytes must be non-zero.");
146 return get(Context, DereferenceableOrNull, Bytes);
149 Attribute
150 Attribute::getWithAllocSizeArgs(LLVMContext &Context, unsigned ElemSizeArg,
151 const Optional<unsigned> &NumElemsArg) {
152 assert(!(ElemSizeArg == 0 && NumElemsArg && *NumElemsArg == 0) &&
153 "Invalid allocsize arguments -- given allocsize(0, 0)");
154 return get(Context, AllocSize, packAllocSizeArgs(ElemSizeArg, NumElemsArg));
157 //===----------------------------------------------------------------------===//
158 // Attribute Accessor Methods
159 //===----------------------------------------------------------------------===//
161 bool Attribute::isEnumAttribute() const {
162 return pImpl && pImpl->isEnumAttribute();
165 bool Attribute::isIntAttribute() const {
166 return pImpl && pImpl->isIntAttribute();
169 bool Attribute::isStringAttribute() const {
170 return pImpl && pImpl->isStringAttribute();
173 Attribute::AttrKind Attribute::getKindAsEnum() const {
174 if (!pImpl) return None;
175 assert((isEnumAttribute() || isIntAttribute()) &&
176 "Invalid attribute type to get the kind as an enum!");
177 return pImpl->getKindAsEnum();
180 uint64_t Attribute::getValueAsInt() const {
181 if (!pImpl) return 0;
182 assert(isIntAttribute() &&
183 "Expected the attribute to be an integer attribute!");
184 return pImpl->getValueAsInt();
187 StringRef Attribute::getKindAsString() const {
188 if (!pImpl) return {};
189 assert(isStringAttribute() &&
190 "Invalid attribute type to get the kind as a string!");
191 return pImpl->getKindAsString();
194 StringRef Attribute::getValueAsString() const {
195 if (!pImpl) return {};
196 assert(isStringAttribute() &&
197 "Invalid attribute type to get the value as a string!");
198 return pImpl->getValueAsString();
201 bool Attribute::hasAttribute(AttrKind Kind) const {
202 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
205 bool Attribute::hasAttribute(StringRef Kind) const {
206 if (!isStringAttribute()) return false;
207 return pImpl && pImpl->hasAttribute(Kind);
210 unsigned Attribute::getAlignment() const {
211 assert(hasAttribute(Attribute::Alignment) &&
212 "Trying to get alignment from non-alignment attribute!");
213 return pImpl->getValueAsInt();
216 unsigned Attribute::getStackAlignment() const {
217 assert(hasAttribute(Attribute::StackAlignment) &&
218 "Trying to get alignment from non-alignment attribute!");
219 return pImpl->getValueAsInt();
222 uint64_t Attribute::getDereferenceableBytes() const {
223 assert(hasAttribute(Attribute::Dereferenceable) &&
224 "Trying to get dereferenceable bytes from "
225 "non-dereferenceable attribute!");
226 return pImpl->getValueAsInt();
229 uint64_t Attribute::getDereferenceableOrNullBytes() const {
230 assert(hasAttribute(Attribute::DereferenceableOrNull) &&
231 "Trying to get dereferenceable bytes from "
232 "non-dereferenceable attribute!");
233 return pImpl->getValueAsInt();
236 std::pair<unsigned, Optional<unsigned>> Attribute::getAllocSizeArgs() const {
237 assert(hasAttribute(Attribute::AllocSize) &&
238 "Trying to get allocsize args from non-allocsize attribute");
239 return unpackAllocSizeArgs(pImpl->getValueAsInt());
242 std::string Attribute::getAsString(bool InAttrGrp) const {
243 if (!pImpl) return {};
245 if (hasAttribute(Attribute::SanitizeAddress))
246 return "sanitize_address";
247 if (hasAttribute(Attribute::SanitizeHWAddress))
248 return "sanitize_hwaddress";
249 if (hasAttribute(Attribute::AlwaysInline))
250 return "alwaysinline";
251 if (hasAttribute(Attribute::ArgMemOnly))
252 return "argmemonly";
253 if (hasAttribute(Attribute::Builtin))
254 return "builtin";
255 if (hasAttribute(Attribute::ByVal))
256 return "byval";
257 if (hasAttribute(Attribute::Convergent))
258 return "convergent";
259 if (hasAttribute(Attribute::SwiftError))
260 return "swifterror";
261 if (hasAttribute(Attribute::SwiftSelf))
262 return "swiftself";
263 if (hasAttribute(Attribute::InaccessibleMemOnly))
264 return "inaccessiblememonly";
265 if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly))
266 return "inaccessiblemem_or_argmemonly";
267 if (hasAttribute(Attribute::InAlloca))
268 return "inalloca";
269 if (hasAttribute(Attribute::InlineHint))
270 return "inlinehint";
271 if (hasAttribute(Attribute::InReg))
272 return "inreg";
273 if (hasAttribute(Attribute::JumpTable))
274 return "jumptable";
275 if (hasAttribute(Attribute::MinSize))
276 return "minsize";
277 if (hasAttribute(Attribute::Naked))
278 return "naked";
279 if (hasAttribute(Attribute::Nest))
280 return "nest";
281 if (hasAttribute(Attribute::NoAlias))
282 return "noalias";
283 if (hasAttribute(Attribute::NoBuiltin))
284 return "nobuiltin";
285 if (hasAttribute(Attribute::NoCapture))
286 return "nocapture";
287 if (hasAttribute(Attribute::NoDuplicate))
288 return "noduplicate";
289 if (hasAttribute(Attribute::NoImplicitFloat))
290 return "noimplicitfloat";
291 if (hasAttribute(Attribute::NoInline))
292 return "noinline";
293 if (hasAttribute(Attribute::NonLazyBind))
294 return "nonlazybind";
295 if (hasAttribute(Attribute::NonNull))
296 return "nonnull";
297 if (hasAttribute(Attribute::NoRedZone))
298 return "noredzone";
299 if (hasAttribute(Attribute::NoReturn))
300 return "noreturn";
301 if (hasAttribute(Attribute::NoCfCheck))
302 return "nocf_check";
303 if (hasAttribute(Attribute::NoRecurse))
304 return "norecurse";
305 if (hasAttribute(Attribute::NoUnwind))
306 return "nounwind";
307 if (hasAttribute(Attribute::OptForFuzzing))
308 return "optforfuzzing";
309 if (hasAttribute(Attribute::OptimizeNone))
310 return "optnone";
311 if (hasAttribute(Attribute::OptimizeForSize))
312 return "optsize";
313 if (hasAttribute(Attribute::ReadNone))
314 return "readnone";
315 if (hasAttribute(Attribute::ReadOnly))
316 return "readonly";
317 if (hasAttribute(Attribute::WriteOnly))
318 return "writeonly";
319 if (hasAttribute(Attribute::Returned))
320 return "returned";
321 if (hasAttribute(Attribute::ReturnsTwice))
322 return "returns_twice";
323 if (hasAttribute(Attribute::SExt))
324 return "signext";
325 if (hasAttribute(Attribute::SpeculativeLoadHardening))
326 return "speculative_load_hardening";
327 if (hasAttribute(Attribute::Speculatable))
328 return "speculatable";
329 if (hasAttribute(Attribute::StackProtect))
330 return "ssp";
331 if (hasAttribute(Attribute::StackProtectReq))
332 return "sspreq";
333 if (hasAttribute(Attribute::StackProtectStrong))
334 return "sspstrong";
335 if (hasAttribute(Attribute::SafeStack))
336 return "safestack";
337 if (hasAttribute(Attribute::ShadowCallStack))
338 return "shadowcallstack";
339 if (hasAttribute(Attribute::StrictFP))
340 return "strictfp";
341 if (hasAttribute(Attribute::StructRet))
342 return "sret";
343 if (hasAttribute(Attribute::SanitizeThread))
344 return "sanitize_thread";
345 if (hasAttribute(Attribute::SanitizeMemory))
346 return "sanitize_memory";
347 if (hasAttribute(Attribute::UWTable))
348 return "uwtable";
349 if (hasAttribute(Attribute::ZExt))
350 return "zeroext";
351 if (hasAttribute(Attribute::Cold))
352 return "cold";
353 if (hasAttribute(Attribute::ImmArg))
354 return "immarg";
356 // FIXME: These should be output like this:
358 // align=4
359 // alignstack=8
361 if (hasAttribute(Attribute::Alignment)) {
362 std::string Result;
363 Result += "align";
364 Result += (InAttrGrp) ? "=" : " ";
365 Result += utostr(getValueAsInt());
366 return Result;
369 auto AttrWithBytesToString = [&](const char *Name) {
370 std::string Result;
371 Result += Name;
372 if (InAttrGrp) {
373 Result += "=";
374 Result += utostr(getValueAsInt());
375 } else {
376 Result += "(";
377 Result += utostr(getValueAsInt());
378 Result += ")";
380 return Result;
383 if (hasAttribute(Attribute::StackAlignment))
384 return AttrWithBytesToString("alignstack");
386 if (hasAttribute(Attribute::Dereferenceable))
387 return AttrWithBytesToString("dereferenceable");
389 if (hasAttribute(Attribute::DereferenceableOrNull))
390 return AttrWithBytesToString("dereferenceable_or_null");
392 if (hasAttribute(Attribute::AllocSize)) {
393 unsigned ElemSize;
394 Optional<unsigned> NumElems;
395 std::tie(ElemSize, NumElems) = getAllocSizeArgs();
397 std::string Result = "allocsize(";
398 Result += utostr(ElemSize);
399 if (NumElems.hasValue()) {
400 Result += ',';
401 Result += utostr(*NumElems);
403 Result += ')';
404 return Result;
407 // Convert target-dependent attributes to strings of the form:
409 // "kind"
410 // "kind" = "value"
412 if (isStringAttribute()) {
413 std::string Result;
414 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
416 std::string AttrVal = pImpl->getValueAsString();
417 if (AttrVal.empty()) return Result;
419 // Since some attribute strings contain special characters that cannot be
420 // printable, those have to be escaped to make the attribute value printable
421 // as is. e.g. "\01__gnu_mcount_nc"
423 raw_string_ostream OS(Result);
424 OS << "=\"";
425 printEscapedString(AttrVal, OS);
426 OS << "\"";
428 return Result;
431 llvm_unreachable("Unknown attribute");
434 bool Attribute::operator<(Attribute A) const {
435 if (!pImpl && !A.pImpl) return false;
436 if (!pImpl) return true;
437 if (!A.pImpl) return false;
438 return *pImpl < *A.pImpl;
441 //===----------------------------------------------------------------------===//
442 // AttributeImpl Definition
443 //===----------------------------------------------------------------------===//
445 // Pin the vtables to this file.
446 AttributeImpl::~AttributeImpl() = default;
448 void EnumAttributeImpl::anchor() {}
450 void IntAttributeImpl::anchor() {}
452 void StringAttributeImpl::anchor() {}
454 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
455 if (isStringAttribute()) return false;
456 return getKindAsEnum() == A;
459 bool AttributeImpl::hasAttribute(StringRef Kind) const {
460 if (!isStringAttribute()) return false;
461 return getKindAsString() == Kind;
464 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
465 assert(isEnumAttribute() || isIntAttribute());
466 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
469 uint64_t AttributeImpl::getValueAsInt() const {
470 assert(isIntAttribute());
471 return static_cast<const IntAttributeImpl *>(this)->getValue();
474 StringRef AttributeImpl::getKindAsString() const {
475 assert(isStringAttribute());
476 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
479 StringRef AttributeImpl::getValueAsString() const {
480 assert(isStringAttribute());
481 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
484 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
485 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
486 // relative to their enum value) and then strings.
487 if (isEnumAttribute()) {
488 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
489 if (AI.isIntAttribute()) return true;
490 if (AI.isStringAttribute()) return true;
493 if (isIntAttribute()) {
494 if (AI.isEnumAttribute()) return false;
495 if (AI.isIntAttribute()) {
496 if (getKindAsEnum() == AI.getKindAsEnum())
497 return getValueAsInt() < AI.getValueAsInt();
498 return getKindAsEnum() < AI.getKindAsEnum();
500 if (AI.isStringAttribute()) return true;
503 if (AI.isEnumAttribute()) return false;
504 if (AI.isIntAttribute()) return false;
505 if (getKindAsString() == AI.getKindAsString())
506 return getValueAsString() < AI.getValueAsString();
507 return getKindAsString() < AI.getKindAsString();
510 //===----------------------------------------------------------------------===//
511 // AttributeSet Definition
512 //===----------------------------------------------------------------------===//
514 AttributeSet AttributeSet::get(LLVMContext &C, const AttrBuilder &B) {
515 return AttributeSet(AttributeSetNode::get(C, B));
518 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<Attribute> Attrs) {
519 return AttributeSet(AttributeSetNode::get(C, Attrs));
522 AttributeSet AttributeSet::addAttribute(LLVMContext &C,
523 Attribute::AttrKind Kind) const {
524 if (hasAttribute(Kind)) return *this;
525 AttrBuilder B;
526 B.addAttribute(Kind);
527 return addAttributes(C, AttributeSet::get(C, B));
530 AttributeSet AttributeSet::addAttribute(LLVMContext &C, StringRef Kind,
531 StringRef Value) const {
532 AttrBuilder B;
533 B.addAttribute(Kind, Value);
534 return addAttributes(C, AttributeSet::get(C, B));
537 AttributeSet AttributeSet::addAttributes(LLVMContext &C,
538 const AttributeSet AS) const {
539 if (!hasAttributes())
540 return AS;
542 if (!AS.hasAttributes())
543 return *this;
545 AttrBuilder B(AS);
546 for (const auto I : *this)
547 B.addAttribute(I);
549 return get(C, B);
552 AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
553 Attribute::AttrKind Kind) const {
554 if (!hasAttribute(Kind)) return *this;
555 AttrBuilder B(*this);
556 B.removeAttribute(Kind);
557 return get(C, B);
560 AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
561 StringRef Kind) const {
562 if (!hasAttribute(Kind)) return *this;
563 AttrBuilder B(*this);
564 B.removeAttribute(Kind);
565 return get(C, B);
568 AttributeSet AttributeSet::removeAttributes(LLVMContext &C,
569 const AttrBuilder &Attrs) const {
570 AttrBuilder B(*this);
571 B.remove(Attrs);
572 return get(C, B);
575 unsigned AttributeSet::getNumAttributes() const {
576 return SetNode ? SetNode->getNumAttributes() : 0;
579 bool AttributeSet::hasAttribute(Attribute::AttrKind Kind) const {
580 return SetNode ? SetNode->hasAttribute(Kind) : false;
583 bool AttributeSet::hasAttribute(StringRef Kind) const {
584 return SetNode ? SetNode->hasAttribute(Kind) : false;
587 Attribute AttributeSet::getAttribute(Attribute::AttrKind Kind) const {
588 return SetNode ? SetNode->getAttribute(Kind) : Attribute();
591 Attribute AttributeSet::getAttribute(StringRef Kind) const {
592 return SetNode ? SetNode->getAttribute(Kind) : Attribute();
595 unsigned AttributeSet::getAlignment() const {
596 return SetNode ? SetNode->getAlignment() : 0;
599 unsigned AttributeSet::getStackAlignment() const {
600 return SetNode ? SetNode->getStackAlignment() : 0;
603 uint64_t AttributeSet::getDereferenceableBytes() const {
604 return SetNode ? SetNode->getDereferenceableBytes() : 0;
607 uint64_t AttributeSet::getDereferenceableOrNullBytes() const {
608 return SetNode ? SetNode->getDereferenceableOrNullBytes() : 0;
611 std::pair<unsigned, Optional<unsigned>> AttributeSet::getAllocSizeArgs() const {
612 return SetNode ? SetNode->getAllocSizeArgs()
613 : std::pair<unsigned, Optional<unsigned>>(0, 0);
616 std::string AttributeSet::getAsString(bool InAttrGrp) const {
617 return SetNode ? SetNode->getAsString(InAttrGrp) : "";
620 AttributeSet::iterator AttributeSet::begin() const {
621 return SetNode ? SetNode->begin() : nullptr;
624 AttributeSet::iterator AttributeSet::end() const {
625 return SetNode ? SetNode->end() : nullptr;
628 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
629 LLVM_DUMP_METHOD void AttributeSet::dump() const {
630 dbgs() << "AS =\n";
631 dbgs() << " { ";
632 dbgs() << getAsString(true) << " }\n";
634 #endif
636 //===----------------------------------------------------------------------===//
637 // AttributeSetNode Definition
638 //===----------------------------------------------------------------------===//
640 AttributeSetNode::AttributeSetNode(ArrayRef<Attribute> Attrs)
641 : AvailableAttrs(0), NumAttrs(Attrs.size()) {
642 // There's memory after the node where we can store the entries in.
643 llvm::copy(Attrs, getTrailingObjects<Attribute>());
645 for (const auto I : *this) {
646 if (!I.isStringAttribute()) {
647 AvailableAttrs |= ((uint64_t)1) << I.getKindAsEnum();
652 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
653 ArrayRef<Attribute> Attrs) {
654 if (Attrs.empty())
655 return nullptr;
657 // Otherwise, build a key to look up the existing attributes.
658 LLVMContextImpl *pImpl = C.pImpl;
659 FoldingSetNodeID ID;
661 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
662 llvm::sort(SortedAttrs);
664 for (const auto Attr : SortedAttrs)
665 Attr.Profile(ID);
667 void *InsertPoint;
668 AttributeSetNode *PA =
669 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
671 // If we didn't find any existing attributes of the same shape then create a
672 // new one and insert it.
673 if (!PA) {
674 // Coallocate entries after the AttributeSetNode itself.
675 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
676 PA = new (Mem) AttributeSetNode(SortedAttrs);
677 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
680 // Return the AttributeSetNode that we found or created.
681 return PA;
684 AttributeSetNode *AttributeSetNode::get(LLVMContext &C, const AttrBuilder &B) {
685 // Add target-independent attributes.
686 SmallVector<Attribute, 8> Attrs;
687 for (Attribute::AttrKind Kind = Attribute::None;
688 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
689 if (!B.contains(Kind))
690 continue;
692 Attribute Attr;
693 switch (Kind) {
694 case Attribute::Alignment:
695 Attr = Attribute::getWithAlignment(C, B.getAlignment());
696 break;
697 case Attribute::StackAlignment:
698 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
699 break;
700 case Attribute::Dereferenceable:
701 Attr = Attribute::getWithDereferenceableBytes(
702 C, B.getDereferenceableBytes());
703 break;
704 case Attribute::DereferenceableOrNull:
705 Attr = Attribute::getWithDereferenceableOrNullBytes(
706 C, B.getDereferenceableOrNullBytes());
707 break;
708 case Attribute::AllocSize: {
709 auto A = B.getAllocSizeArgs();
710 Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second);
711 break;
713 default:
714 Attr = Attribute::get(C, Kind);
716 Attrs.push_back(Attr);
719 // Add target-dependent (string) attributes.
720 for (const auto &TDA : B.td_attrs())
721 Attrs.emplace_back(Attribute::get(C, TDA.first, TDA.second));
723 return get(C, Attrs);
726 bool AttributeSetNode::hasAttribute(StringRef Kind) const {
727 for (const auto I : *this)
728 if (I.hasAttribute(Kind))
729 return true;
730 return false;
733 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
734 if (hasAttribute(Kind)) {
735 for (const auto I : *this)
736 if (I.hasAttribute(Kind))
737 return I;
739 return {};
742 Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
743 for (const auto I : *this)
744 if (I.hasAttribute(Kind))
745 return I;
746 return {};
749 unsigned AttributeSetNode::getAlignment() const {
750 for (const auto I : *this)
751 if (I.hasAttribute(Attribute::Alignment))
752 return I.getAlignment();
753 return 0;
756 unsigned AttributeSetNode::getStackAlignment() const {
757 for (const auto I : *this)
758 if (I.hasAttribute(Attribute::StackAlignment))
759 return I.getStackAlignment();
760 return 0;
763 uint64_t AttributeSetNode::getDereferenceableBytes() const {
764 for (const auto I : *this)
765 if (I.hasAttribute(Attribute::Dereferenceable))
766 return I.getDereferenceableBytes();
767 return 0;
770 uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
771 for (const auto I : *this)
772 if (I.hasAttribute(Attribute::DereferenceableOrNull))
773 return I.getDereferenceableOrNullBytes();
774 return 0;
777 std::pair<unsigned, Optional<unsigned>>
778 AttributeSetNode::getAllocSizeArgs() const {
779 for (const auto I : *this)
780 if (I.hasAttribute(Attribute::AllocSize))
781 return I.getAllocSizeArgs();
782 return std::make_pair(0, 0);
785 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
786 std::string Str;
787 for (iterator I = begin(), E = end(); I != E; ++I) {
788 if (I != begin())
789 Str += ' ';
790 Str += I->getAsString(InAttrGrp);
792 return Str;
795 //===----------------------------------------------------------------------===//
796 // AttributeListImpl Definition
797 //===----------------------------------------------------------------------===//
799 /// Map from AttributeList index to the internal array index. Adding one happens
800 /// to work, but it relies on unsigned integer wrapping. MSVC warns about
801 /// unsigned wrapping in constexpr functions, so write out the conditional. LLVM
802 /// folds it to add anyway.
803 static constexpr unsigned attrIdxToArrayIdx(unsigned Index) {
804 return Index == AttributeList::FunctionIndex ? 0 : Index + 1;
807 AttributeListImpl::AttributeListImpl(LLVMContext &C,
808 ArrayRef<AttributeSet> Sets)
809 : AvailableFunctionAttrs(0), Context(C), NumAttrSets(Sets.size()) {
810 assert(!Sets.empty() && "pointless AttributeListImpl");
812 // There's memory after the node where we can store the entries in.
813 llvm::copy(Sets, getTrailingObjects<AttributeSet>());
815 // Initialize AvailableFunctionAttrs summary bitset.
816 static_assert(Attribute::EndAttrKinds <=
817 sizeof(AvailableFunctionAttrs) * CHAR_BIT,
818 "Too many attributes");
819 static_assert(attrIdxToArrayIdx(AttributeList::FunctionIndex) == 0U,
820 "function should be stored in slot 0");
821 for (const auto I : Sets[0]) {
822 if (!I.isStringAttribute())
823 AvailableFunctionAttrs |= 1ULL << I.getKindAsEnum();
827 void AttributeListImpl::Profile(FoldingSetNodeID &ID) const {
828 Profile(ID, makeArrayRef(begin(), end()));
831 void AttributeListImpl::Profile(FoldingSetNodeID &ID,
832 ArrayRef<AttributeSet> Sets) {
833 for (const auto &Set : Sets)
834 ID.AddPointer(Set.SetNode);
837 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
838 LLVM_DUMP_METHOD void AttributeListImpl::dump() const {
839 AttributeList(const_cast<AttributeListImpl *>(this)).dump();
841 #endif
843 //===----------------------------------------------------------------------===//
844 // AttributeList Construction and Mutation Methods
845 //===----------------------------------------------------------------------===//
847 AttributeList AttributeList::getImpl(LLVMContext &C,
848 ArrayRef<AttributeSet> AttrSets) {
849 assert(!AttrSets.empty() && "pointless AttributeListImpl");
851 LLVMContextImpl *pImpl = C.pImpl;
852 FoldingSetNodeID ID;
853 AttributeListImpl::Profile(ID, AttrSets);
855 void *InsertPoint;
856 AttributeListImpl *PA =
857 pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
859 // If we didn't find any existing attributes of the same shape then
860 // create a new one and insert it.
861 if (!PA) {
862 // Coallocate entries after the AttributeListImpl itself.
863 void *Mem = ::operator new(
864 AttributeListImpl::totalSizeToAlloc<AttributeSet>(AttrSets.size()));
865 PA = new (Mem) AttributeListImpl(C, AttrSets);
866 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
869 // Return the AttributesList that we found or created.
870 return AttributeList(PA);
873 AttributeList
874 AttributeList::get(LLVMContext &C,
875 ArrayRef<std::pair<unsigned, Attribute>> Attrs) {
876 // If there are no attributes then return a null AttributesList pointer.
877 if (Attrs.empty())
878 return {};
880 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
881 [](const std::pair<unsigned, Attribute> &LHS,
882 const std::pair<unsigned, Attribute> &RHS) {
883 return LHS.first < RHS.first;
884 }) && "Misordered Attributes list!");
885 assert(llvm::none_of(Attrs,
886 [](const std::pair<unsigned, Attribute> &Pair) {
887 return Pair.second.hasAttribute(Attribute::None);
888 }) &&
889 "Pointless attribute!");
891 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
892 // list.
893 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairVec;
894 for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(),
895 E = Attrs.end(); I != E; ) {
896 unsigned Index = I->first;
897 SmallVector<Attribute, 4> AttrVec;
898 while (I != E && I->first == Index) {
899 AttrVec.push_back(I->second);
900 ++I;
903 AttrPairVec.emplace_back(Index, AttributeSet::get(C, AttrVec));
906 return get(C, AttrPairVec);
909 AttributeList
910 AttributeList::get(LLVMContext &C,
911 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) {
912 // If there are no attributes then return a null AttributesList pointer.
913 if (Attrs.empty())
914 return {};
916 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
917 [](const std::pair<unsigned, AttributeSet> &LHS,
918 const std::pair<unsigned, AttributeSet> &RHS) {
919 return LHS.first < RHS.first;
920 }) &&
921 "Misordered Attributes list!");
922 assert(llvm::none_of(Attrs,
923 [](const std::pair<unsigned, AttributeSet> &Pair) {
924 return !Pair.second.hasAttributes();
925 }) &&
926 "Pointless attribute!");
928 unsigned MaxIndex = Attrs.back().first;
929 // If the MaxIndex is FunctionIndex and there are other indices in front
930 // of it, we need to use the largest of those to get the right size.
931 if (MaxIndex == FunctionIndex && Attrs.size() > 1)
932 MaxIndex = Attrs[Attrs.size() - 2].first;
934 SmallVector<AttributeSet, 4> AttrVec(attrIdxToArrayIdx(MaxIndex) + 1);
935 for (const auto Pair : Attrs)
936 AttrVec[attrIdxToArrayIdx(Pair.first)] = Pair.second;
938 return getImpl(C, AttrVec);
941 AttributeList AttributeList::get(LLVMContext &C, AttributeSet FnAttrs,
942 AttributeSet RetAttrs,
943 ArrayRef<AttributeSet> ArgAttrs) {
944 // Scan from the end to find the last argument with attributes. Most
945 // arguments don't have attributes, so it's nice if we can have fewer unique
946 // AttributeListImpls by dropping empty attribute sets at the end of the list.
947 unsigned NumSets = 0;
948 for (size_t I = ArgAttrs.size(); I != 0; --I) {
949 if (ArgAttrs[I - 1].hasAttributes()) {
950 NumSets = I + 2;
951 break;
954 if (NumSets == 0) {
955 // Check function and return attributes if we didn't have argument
956 // attributes.
957 if (RetAttrs.hasAttributes())
958 NumSets = 2;
959 else if (FnAttrs.hasAttributes())
960 NumSets = 1;
963 // If all attribute sets were empty, we can use the empty attribute list.
964 if (NumSets == 0)
965 return {};
967 SmallVector<AttributeSet, 8> AttrSets;
968 AttrSets.reserve(NumSets);
969 // If we have any attributes, we always have function attributes.
970 AttrSets.push_back(FnAttrs);
971 if (NumSets > 1)
972 AttrSets.push_back(RetAttrs);
973 if (NumSets > 2) {
974 // Drop the empty argument attribute sets at the end.
975 ArgAttrs = ArgAttrs.take_front(NumSets - 2);
976 AttrSets.insert(AttrSets.end(), ArgAttrs.begin(), ArgAttrs.end());
979 return getImpl(C, AttrSets);
982 AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
983 const AttrBuilder &B) {
984 if (!B.hasAttributes())
985 return {};
986 Index = attrIdxToArrayIdx(Index);
987 SmallVector<AttributeSet, 8> AttrSets(Index + 1);
988 AttrSets[Index] = AttributeSet::get(C, B);
989 return getImpl(C, AttrSets);
992 AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
993 ArrayRef<Attribute::AttrKind> Kinds) {
994 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
995 for (const auto K : Kinds)
996 Attrs.emplace_back(Index, Attribute::get(C, K));
997 return get(C, Attrs);
1000 AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
1001 ArrayRef<StringRef> Kinds) {
1002 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
1003 for (const auto K : Kinds)
1004 Attrs.emplace_back(Index, Attribute::get(C, K));
1005 return get(C, Attrs);
1008 AttributeList AttributeList::get(LLVMContext &C,
1009 ArrayRef<AttributeList> Attrs) {
1010 if (Attrs.empty())
1011 return {};
1012 if (Attrs.size() == 1)
1013 return Attrs[0];
1015 unsigned MaxSize = 0;
1016 for (const auto List : Attrs)
1017 MaxSize = std::max(MaxSize, List.getNumAttrSets());
1019 // If every list was empty, there is no point in merging the lists.
1020 if (MaxSize == 0)
1021 return {};
1023 SmallVector<AttributeSet, 8> NewAttrSets(MaxSize);
1024 for (unsigned I = 0; I < MaxSize; ++I) {
1025 AttrBuilder CurBuilder;
1026 for (const auto List : Attrs)
1027 CurBuilder.merge(List.getAttributes(I - 1));
1028 NewAttrSets[I] = AttributeSet::get(C, CurBuilder);
1031 return getImpl(C, NewAttrSets);
1034 AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1035 Attribute::AttrKind Kind) const {
1036 if (hasAttribute(Index, Kind)) return *this;
1037 AttrBuilder B;
1038 B.addAttribute(Kind);
1039 return addAttributes(C, Index, B);
1042 AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1043 StringRef Kind,
1044 StringRef Value) const {
1045 AttrBuilder B;
1046 B.addAttribute(Kind, Value);
1047 return addAttributes(C, Index, B);
1050 AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1051 Attribute A) const {
1052 AttrBuilder B;
1053 B.addAttribute(A);
1054 return addAttributes(C, Index, B);
1057 AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
1058 const AttrBuilder &B) const {
1059 if (!B.hasAttributes())
1060 return *this;
1062 if (!pImpl)
1063 return AttributeList::get(C, {{Index, AttributeSet::get(C, B)}});
1065 #ifndef NDEBUG
1066 // FIXME it is not obvious how this should work for alignment. For now, say
1067 // we can't change a known alignment.
1068 unsigned OldAlign = getAttributes(Index).getAlignment();
1069 unsigned NewAlign = B.getAlignment();
1070 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
1071 "Attempt to change alignment!");
1072 #endif
1074 Index = attrIdxToArrayIdx(Index);
1075 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1076 if (Index >= AttrSets.size())
1077 AttrSets.resize(Index + 1);
1079 AttrBuilder Merged(AttrSets[Index]);
1080 Merged.merge(B);
1081 AttrSets[Index] = AttributeSet::get(C, Merged);
1083 return getImpl(C, AttrSets);
1086 AttributeList AttributeList::addParamAttribute(LLVMContext &C,
1087 ArrayRef<unsigned> ArgNos,
1088 Attribute A) const {
1089 assert(std::is_sorted(ArgNos.begin(), ArgNos.end()));
1091 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1092 unsigned MaxIndex = attrIdxToArrayIdx(ArgNos.back() + FirstArgIndex);
1093 if (MaxIndex >= AttrSets.size())
1094 AttrSets.resize(MaxIndex + 1);
1096 for (unsigned ArgNo : ArgNos) {
1097 unsigned Index = attrIdxToArrayIdx(ArgNo + FirstArgIndex);
1098 AttrBuilder B(AttrSets[Index]);
1099 B.addAttribute(A);
1100 AttrSets[Index] = AttributeSet::get(C, B);
1103 return getImpl(C, AttrSets);
1106 AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1107 Attribute::AttrKind Kind) const {
1108 if (!hasAttribute(Index, Kind)) return *this;
1110 Index = attrIdxToArrayIdx(Index);
1111 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1112 assert(Index < AttrSets.size());
1114 AttrSets[Index] = AttrSets[Index].removeAttribute(C, Kind);
1116 return getImpl(C, AttrSets);
1119 AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1120 StringRef Kind) const {
1121 if (!hasAttribute(Index, Kind)) return *this;
1123 Index = attrIdxToArrayIdx(Index);
1124 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1125 assert(Index < AttrSets.size());
1127 AttrSets[Index] = AttrSets[Index].removeAttribute(C, Kind);
1129 return getImpl(C, AttrSets);
1132 AttributeList
1133 AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
1134 const AttrBuilder &AttrsToRemove) const {
1135 if (!pImpl)
1136 return {};
1138 Index = attrIdxToArrayIdx(Index);
1139 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1140 if (Index >= AttrSets.size())
1141 AttrSets.resize(Index + 1);
1143 AttrSets[Index] = AttrSets[Index].removeAttributes(C, AttrsToRemove);
1145 return getImpl(C, AttrSets);
1148 AttributeList AttributeList::removeAttributes(LLVMContext &C,
1149 unsigned WithoutIndex) const {
1150 if (!pImpl)
1151 return {};
1152 WithoutIndex = attrIdxToArrayIdx(WithoutIndex);
1153 if (WithoutIndex >= getNumAttrSets())
1154 return *this;
1155 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1156 AttrSets[WithoutIndex] = AttributeSet();
1157 return getImpl(C, AttrSets);
1160 AttributeList AttributeList::addDereferenceableAttr(LLVMContext &C,
1161 unsigned Index,
1162 uint64_t Bytes) const {
1163 AttrBuilder B;
1164 B.addDereferenceableAttr(Bytes);
1165 return addAttributes(C, Index, B);
1168 AttributeList
1169 AttributeList::addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
1170 uint64_t Bytes) const {
1171 AttrBuilder B;
1172 B.addDereferenceableOrNullAttr(Bytes);
1173 return addAttributes(C, Index, B);
1176 AttributeList
1177 AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index,
1178 unsigned ElemSizeArg,
1179 const Optional<unsigned> &NumElemsArg) {
1180 AttrBuilder B;
1181 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
1182 return addAttributes(C, Index, B);
1185 //===----------------------------------------------------------------------===//
1186 // AttributeList Accessor Methods
1187 //===----------------------------------------------------------------------===//
1189 LLVMContext &AttributeList::getContext() const { return pImpl->getContext(); }
1191 AttributeSet AttributeList::getParamAttributes(unsigned ArgNo) const {
1192 return getAttributes(ArgNo + FirstArgIndex);
1195 AttributeSet AttributeList::getRetAttributes() const {
1196 return getAttributes(ReturnIndex);
1199 AttributeSet AttributeList::getFnAttributes() const {
1200 return getAttributes(FunctionIndex);
1203 bool AttributeList::hasAttribute(unsigned Index,
1204 Attribute::AttrKind Kind) const {
1205 return getAttributes(Index).hasAttribute(Kind);
1208 bool AttributeList::hasAttribute(unsigned Index, StringRef Kind) const {
1209 return getAttributes(Index).hasAttribute(Kind);
1212 bool AttributeList::hasAttributes(unsigned Index) const {
1213 return getAttributes(Index).hasAttributes();
1216 bool AttributeList::hasFnAttribute(Attribute::AttrKind Kind) const {
1217 return pImpl && pImpl->hasFnAttribute(Kind);
1220 bool AttributeList::hasFnAttribute(StringRef Kind) const {
1221 return hasAttribute(AttributeList::FunctionIndex, Kind);
1224 bool AttributeList::hasParamAttribute(unsigned ArgNo,
1225 Attribute::AttrKind Kind) const {
1226 return hasAttribute(ArgNo + FirstArgIndex, Kind);
1229 bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr,
1230 unsigned *Index) const {
1231 if (!pImpl) return false;
1233 for (unsigned I = index_begin(), E = index_end(); I != E; ++I) {
1234 if (hasAttribute(I, Attr)) {
1235 if (Index)
1236 *Index = I;
1237 return true;
1241 return false;
1244 Attribute AttributeList::getAttribute(unsigned Index,
1245 Attribute::AttrKind Kind) const {
1246 return getAttributes(Index).getAttribute(Kind);
1249 Attribute AttributeList::getAttribute(unsigned Index, StringRef Kind) const {
1250 return getAttributes(Index).getAttribute(Kind);
1253 unsigned AttributeList::getRetAlignment() const {
1254 return getAttributes(ReturnIndex).getAlignment();
1257 unsigned AttributeList::getParamAlignment(unsigned ArgNo) const {
1258 return getAttributes(ArgNo + FirstArgIndex).getAlignment();
1261 unsigned AttributeList::getStackAlignment(unsigned Index) const {
1262 return getAttributes(Index).getStackAlignment();
1265 uint64_t AttributeList::getDereferenceableBytes(unsigned Index) const {
1266 return getAttributes(Index).getDereferenceableBytes();
1269 uint64_t AttributeList::getDereferenceableOrNullBytes(unsigned Index) const {
1270 return getAttributes(Index).getDereferenceableOrNullBytes();
1273 std::pair<unsigned, Optional<unsigned>>
1274 AttributeList::getAllocSizeArgs(unsigned Index) const {
1275 return getAttributes(Index).getAllocSizeArgs();
1278 std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const {
1279 return getAttributes(Index).getAsString(InAttrGrp);
1282 AttributeSet AttributeList::getAttributes(unsigned Index) const {
1283 Index = attrIdxToArrayIdx(Index);
1284 if (!pImpl || Index >= getNumAttrSets())
1285 return {};
1286 return pImpl->begin()[Index];
1289 AttributeList::iterator AttributeList::begin() const {
1290 return pImpl ? pImpl->begin() : nullptr;
1293 AttributeList::iterator AttributeList::end() const {
1294 return pImpl ? pImpl->end() : nullptr;
1297 //===----------------------------------------------------------------------===//
1298 // AttributeList Introspection Methods
1299 //===----------------------------------------------------------------------===//
1301 unsigned AttributeList::getNumAttrSets() const {
1302 return pImpl ? pImpl->NumAttrSets : 0;
1305 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1306 LLVM_DUMP_METHOD void AttributeList::dump() const {
1307 dbgs() << "PAL[\n";
1309 for (unsigned i = index_begin(), e = index_end(); i != e; ++i) {
1310 if (getAttributes(i).hasAttributes())
1311 dbgs() << " { " << i << " => " << getAsString(i) << " }\n";
1314 dbgs() << "]\n";
1316 #endif
1318 //===----------------------------------------------------------------------===//
1319 // AttrBuilder Method Implementations
1320 //===----------------------------------------------------------------------===//
1322 // FIXME: Remove this ctor, use AttributeSet.
1323 AttrBuilder::AttrBuilder(AttributeList AL, unsigned Index) {
1324 AttributeSet AS = AL.getAttributes(Index);
1325 for (const auto &A : AS)
1326 addAttribute(A);
1329 AttrBuilder::AttrBuilder(AttributeSet AS) {
1330 for (const auto &A : AS)
1331 addAttribute(A);
1334 void AttrBuilder::clear() {
1335 Attrs.reset();
1336 TargetDepAttrs.clear();
1337 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
1338 AllocSizeArgs = 0;
1341 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
1342 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1343 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
1344 Val != Attribute::Dereferenceable && Val != Attribute::AllocSize &&
1345 "Adding integer attribute without adding a value!");
1346 Attrs[Val] = true;
1347 return *this;
1350 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
1351 if (Attr.isStringAttribute()) {
1352 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1353 return *this;
1356 Attribute::AttrKind Kind = Attr.getKindAsEnum();
1357 Attrs[Kind] = true;
1359 if (Kind == Attribute::Alignment)
1360 Alignment = Attr.getAlignment();
1361 else if (Kind == Attribute::StackAlignment)
1362 StackAlignment = Attr.getStackAlignment();
1363 else if (Kind == Attribute::Dereferenceable)
1364 DerefBytes = Attr.getDereferenceableBytes();
1365 else if (Kind == Attribute::DereferenceableOrNull)
1366 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
1367 else if (Kind == Attribute::AllocSize)
1368 AllocSizeArgs = Attr.getValueAsInt();
1369 return *this;
1372 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1373 TargetDepAttrs[A] = V;
1374 return *this;
1377 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
1378 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1379 Attrs[Val] = false;
1381 if (Val == Attribute::Alignment)
1382 Alignment = 0;
1383 else if (Val == Attribute::StackAlignment)
1384 StackAlignment = 0;
1385 else if (Val == Attribute::Dereferenceable)
1386 DerefBytes = 0;
1387 else if (Val == Attribute::DereferenceableOrNull)
1388 DerefOrNullBytes = 0;
1389 else if (Val == Attribute::AllocSize)
1390 AllocSizeArgs = 0;
1392 return *this;
1395 AttrBuilder &AttrBuilder::removeAttributes(AttributeList A, uint64_t Index) {
1396 remove(A.getAttributes(Index));
1397 return *this;
1400 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1401 auto I = TargetDepAttrs.find(A);
1402 if (I != TargetDepAttrs.end())
1403 TargetDepAttrs.erase(I);
1404 return *this;
1407 std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1408 return unpackAllocSizeArgs(AllocSizeArgs);
1411 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
1412 if (Align == 0) return *this;
1414 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1415 assert(Align <= 0x40000000 && "Alignment too large.");
1417 Attrs[Attribute::Alignment] = true;
1418 Alignment = Align;
1419 return *this;
1422 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1423 // Default alignment, allow the target to define how to align it.
1424 if (Align == 0) return *this;
1426 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1427 assert(Align <= 0x100 && "Alignment too large.");
1429 Attrs[Attribute::StackAlignment] = true;
1430 StackAlignment = Align;
1431 return *this;
1434 AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1435 if (Bytes == 0) return *this;
1437 Attrs[Attribute::Dereferenceable] = true;
1438 DerefBytes = Bytes;
1439 return *this;
1442 AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1443 if (Bytes == 0)
1444 return *this;
1446 Attrs[Attribute::DereferenceableOrNull] = true;
1447 DerefOrNullBytes = Bytes;
1448 return *this;
1451 AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
1452 const Optional<unsigned> &NumElems) {
1453 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
1456 AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
1457 // (0, 0) is our "not present" value, so we need to check for it here.
1458 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
1460 Attrs[Attribute::AllocSize] = true;
1461 // Reuse existing machinery to store this as a single 64-bit integer so we can
1462 // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
1463 AllocSizeArgs = RawArgs;
1464 return *this;
1467 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1468 // FIXME: What if both have alignments, but they don't match?!
1469 if (!Alignment)
1470 Alignment = B.Alignment;
1472 if (!StackAlignment)
1473 StackAlignment = B.StackAlignment;
1475 if (!DerefBytes)
1476 DerefBytes = B.DerefBytes;
1478 if (!DerefOrNullBytes)
1479 DerefOrNullBytes = B.DerefOrNullBytes;
1481 if (!AllocSizeArgs)
1482 AllocSizeArgs = B.AllocSizeArgs;
1484 Attrs |= B.Attrs;
1486 for (auto I : B.td_attrs())
1487 TargetDepAttrs[I.first] = I.second;
1489 return *this;
1492 AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1493 // FIXME: What if both have alignments, but they don't match?!
1494 if (B.Alignment)
1495 Alignment = 0;
1497 if (B.StackAlignment)
1498 StackAlignment = 0;
1500 if (B.DerefBytes)
1501 DerefBytes = 0;
1503 if (B.DerefOrNullBytes)
1504 DerefOrNullBytes = 0;
1506 if (B.AllocSizeArgs)
1507 AllocSizeArgs = 0;
1509 Attrs &= ~B.Attrs;
1511 for (auto I : B.td_attrs())
1512 TargetDepAttrs.erase(I.first);
1514 return *this;
1517 bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1518 // First check if any of the target independent attributes overlap.
1519 if ((Attrs & B.Attrs).any())
1520 return true;
1522 // Then check if any target dependent ones do.
1523 for (const auto &I : td_attrs())
1524 if (B.contains(I.first))
1525 return true;
1527 return false;
1530 bool AttrBuilder::contains(StringRef A) const {
1531 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1534 bool AttrBuilder::hasAttributes() const {
1535 return !Attrs.none() || !TargetDepAttrs.empty();
1538 bool AttrBuilder::hasAttributes(AttributeList AL, uint64_t Index) const {
1539 AttributeSet AS = AL.getAttributes(Index);
1541 for (const auto Attr : AS) {
1542 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1543 if (contains(Attr.getKindAsEnum()))
1544 return true;
1545 } else {
1546 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1547 return contains(Attr.getKindAsString());
1551 return false;
1554 bool AttrBuilder::hasAlignmentAttr() const {
1555 return Alignment != 0;
1558 bool AttrBuilder::operator==(const AttrBuilder &B) {
1559 if (Attrs != B.Attrs)
1560 return false;
1562 for (td_const_iterator I = TargetDepAttrs.begin(),
1563 E = TargetDepAttrs.end(); I != E; ++I)
1564 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1565 return false;
1567 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1568 DerefBytes == B.DerefBytes;
1571 //===----------------------------------------------------------------------===//
1572 // AttributeFuncs Function Defintions
1573 //===----------------------------------------------------------------------===//
1575 /// Which attributes cannot be applied to a type.
1576 AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
1577 AttrBuilder Incompatible;
1579 if (!Ty->isIntegerTy())
1580 // Attribute that only apply to integers.
1581 Incompatible.addAttribute(Attribute::SExt)
1582 .addAttribute(Attribute::ZExt);
1584 if (!Ty->isPointerTy())
1585 // Attribute that only apply to pointers.
1586 Incompatible.addAttribute(Attribute::ByVal)
1587 .addAttribute(Attribute::Nest)
1588 .addAttribute(Attribute::NoAlias)
1589 .addAttribute(Attribute::NoCapture)
1590 .addAttribute(Attribute::NonNull)
1591 .addDereferenceableAttr(1) // the int here is ignored
1592 .addDereferenceableOrNullAttr(1) // the int here is ignored
1593 .addAttribute(Attribute::ReadNone)
1594 .addAttribute(Attribute::ReadOnly)
1595 .addAttribute(Attribute::StructRet)
1596 .addAttribute(Attribute::InAlloca);
1598 return Incompatible;
1601 template<typename AttrClass>
1602 static bool isEqual(const Function &Caller, const Function &Callee) {
1603 return Caller.getFnAttribute(AttrClass::getKind()) ==
1604 Callee.getFnAttribute(AttrClass::getKind());
1607 /// Compute the logical AND of the attributes of the caller and the
1608 /// callee.
1610 /// This function sets the caller's attribute to false if the callee's attribute
1611 /// is false.
1612 template<typename AttrClass>
1613 static void setAND(Function &Caller, const Function &Callee) {
1614 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1615 !AttrClass::isSet(Callee, AttrClass::getKind()))
1616 AttrClass::set(Caller, AttrClass::getKind(), false);
1619 /// Compute the logical OR of the attributes of the caller and the
1620 /// callee.
1622 /// This function sets the caller's attribute to true if the callee's attribute
1623 /// is true.
1624 template<typename AttrClass>
1625 static void setOR(Function &Caller, const Function &Callee) {
1626 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1627 AttrClass::isSet(Callee, AttrClass::getKind()))
1628 AttrClass::set(Caller, AttrClass::getKind(), true);
1631 /// If the inlined function had a higher stack protection level than the
1632 /// calling function, then bump up the caller's stack protection level.
1633 static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1634 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1635 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1636 // clutter to the IR.
1637 AttrBuilder OldSSPAttr;
1638 OldSSPAttr.addAttribute(Attribute::StackProtect)
1639 .addAttribute(Attribute::StackProtectStrong)
1640 .addAttribute(Attribute::StackProtectReq);
1642 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
1643 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
1644 Caller.addFnAttr(Attribute::StackProtectReq);
1645 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
1646 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
1647 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
1648 Caller.addFnAttr(Attribute::StackProtectStrong);
1649 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
1650 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1651 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1652 Caller.addFnAttr(Attribute::StackProtect);
1655 /// If the inlined function required stack probes, then ensure that
1656 /// the calling function has those too.
1657 static void adjustCallerStackProbes(Function &Caller, const Function &Callee) {
1658 if (!Caller.hasFnAttribute("probe-stack") &&
1659 Callee.hasFnAttribute("probe-stack")) {
1660 Caller.addFnAttr(Callee.getFnAttribute("probe-stack"));
1664 /// If the inlined function defines the size of guard region
1665 /// on the stack, then ensure that the calling function defines a guard region
1666 /// that is no larger.
1667 static void
1668 adjustCallerStackProbeSize(Function &Caller, const Function &Callee) {
1669 if (Callee.hasFnAttribute("stack-probe-size")) {
1670 uint64_t CalleeStackProbeSize;
1671 Callee.getFnAttribute("stack-probe-size")
1672 .getValueAsString()
1673 .getAsInteger(0, CalleeStackProbeSize);
1674 if (Caller.hasFnAttribute("stack-probe-size")) {
1675 uint64_t CallerStackProbeSize;
1676 Caller.getFnAttribute("stack-probe-size")
1677 .getValueAsString()
1678 .getAsInteger(0, CallerStackProbeSize);
1679 if (CallerStackProbeSize > CalleeStackProbeSize) {
1680 Caller.addFnAttr(Callee.getFnAttribute("stack-probe-size"));
1682 } else {
1683 Caller.addFnAttr(Callee.getFnAttribute("stack-probe-size"));
1688 /// If the inlined function defines a min legal vector width, then ensure
1689 /// the calling function has the same or larger min legal vector width. If the
1690 /// caller has the attribute, but the callee doesn't, we need to remove the
1691 /// attribute from the caller since we can't make any guarantees about the
1692 /// caller's requirements.
1693 /// This function is called after the inlining decision has been made so we have
1694 /// to merge the attribute this way. Heuristics that would use
1695 /// min-legal-vector-width to determine inline compatibility would need to be
1696 /// handled as part of inline cost analysis.
1697 static void
1698 adjustMinLegalVectorWidth(Function &Caller, const Function &Callee) {
1699 if (Caller.hasFnAttribute("min-legal-vector-width")) {
1700 if (Callee.hasFnAttribute("min-legal-vector-width")) {
1701 uint64_t CallerVectorWidth;
1702 Caller.getFnAttribute("min-legal-vector-width")
1703 .getValueAsString()
1704 .getAsInteger(0, CallerVectorWidth);
1705 uint64_t CalleeVectorWidth;
1706 Callee.getFnAttribute("min-legal-vector-width")
1707 .getValueAsString()
1708 .getAsInteger(0, CalleeVectorWidth);
1709 if (CallerVectorWidth < CalleeVectorWidth)
1710 Caller.addFnAttr(Callee.getFnAttribute("min-legal-vector-width"));
1711 } else {
1712 // If the callee doesn't have the attribute then we don't know anything
1713 // and must drop the attribute from the caller.
1714 Caller.removeFnAttr("min-legal-vector-width");
1719 /// If the inlined function has "null-pointer-is-valid=true" attribute,
1720 /// set this attribute in the caller post inlining.
1721 static void
1722 adjustNullPointerValidAttr(Function &Caller, const Function &Callee) {
1723 if (Callee.nullPointerIsDefined() && !Caller.nullPointerIsDefined()) {
1724 Caller.addFnAttr(Callee.getFnAttribute("null-pointer-is-valid"));
1728 #define GET_ATTR_COMPAT_FUNC
1729 #include "AttributesCompatFunc.inc"
1731 bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1732 const Function &Callee) {
1733 return hasCompatibleFnAttrs(Caller, Callee);
1736 void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1737 const Function &Callee) {
1738 mergeFnAttrs(Caller, Callee);