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. 12 // 13 //===----------------------------------------------------------------------===// 14 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/StringSwitch.h" 26 #include "llvm/ADT/Twine.h" 27 #include "llvm/Config/llvm-config.h" 28 #include "llvm/IR/Function.h" 29 #include "llvm/IR/LLVMContext.h" 30 #include "llvm/IR/Type.h" 31 #include "llvm/Support/Compiler.h" 32 #include "llvm/Support/Debug.h" 33 #include "llvm/Support/ErrorHandling.h" 34 #include "llvm/Support/MathExtras.h" 35 #include "llvm/Support/raw_ostream.h" 36 #include <algorithm> 37 #include <cassert> 38 #include <climits> 39 #include <cstddef> 40 #include <cstdint> 41 #include <limits> 42 #include <string> 43 #include <tuple> 44 #include <utility> 45 46 using namespace llvm; 47 48 //===----------------------------------------------------------------------===// 49 // Attribute Construction Methods 50 //===----------------------------------------------------------------------===// 51 52 // allocsize has two integer arguments, but because they're both 32 bits, we can 53 // pack them into one 64-bit value, at the cost of making said value 54 // nonsensical. 55 // 56 // In order to do this, we need to reserve one value of the second (optional) 57 // allocsize argument to signify "not present." 58 static const unsigned AllocSizeNumElemsNotPresent = -1; 59 60 static uint64_t packAllocSizeArgs(unsigned ElemSizeArg, 61 const Optional<unsigned> &NumElemsArg) { 62 assert((!NumElemsArg.hasValue() || 63 *NumElemsArg != AllocSizeNumElemsNotPresent) && 64 "Attempting to pack a reserved value"); 65 66 return uint64_t(ElemSizeArg) << 32 | 67 NumElemsArg.getValueOr(AllocSizeNumElemsNotPresent); 68 } 69 70 static std::pair<unsigned, Optional<unsigned>> 71 unpackAllocSizeArgs(uint64_t Num) { 72 unsigned NumElems = Num & std::numeric_limits<unsigned>::max(); 73 unsigned ElemSizeArg = Num >> 32; 74 75 Optional<unsigned> NumElemsArg; 76 if (NumElems != AllocSizeNumElemsNotPresent) 77 NumElemsArg = NumElems; 78 return std::make_pair(ElemSizeArg, NumElemsArg); 79 } 80 81 static uint64_t packVScaleRangeArgs(unsigned MinValue, unsigned MaxValue) { 82 return uint64_t(MinValue) << 32 | MaxValue; 83 } 84 85 static std::pair<unsigned, unsigned> unpackVScaleRangeArgs(uint64_t Value) { 86 unsigned MaxValue = Value & std::numeric_limits<unsigned>::max(); 87 unsigned MinValue = Value >> 32; 88 89 return std::make_pair(MinValue, MaxValue); 90 } 91 92 Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind, 93 uint64_t Val) { 94 if (Val) 95 assert(Attribute::isIntAttrKind(Kind) && "Not an int attribute"); 96 else 97 assert(Attribute::isEnumAttrKind(Kind) && "Not an enum attribute"); 98 99 LLVMContextImpl *pImpl = Context.pImpl; 100 FoldingSetNodeID ID; 101 ID.AddInteger(Kind); 102 if (Val) ID.AddInteger(Val); 103 104 void *InsertPoint; 105 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint); 106 107 if (!PA) { 108 // If we didn't find any existing attributes of the same shape then create a 109 // new one and insert it. 110 if (!Val) 111 PA = new (pImpl->Alloc) EnumAttributeImpl(Kind); 112 else 113 PA = new (pImpl->Alloc) IntAttributeImpl(Kind, Val); 114 pImpl->AttrsSet.InsertNode(PA, InsertPoint); 115 } 116 117 // Return the Attribute that we found or created. 118 return Attribute(PA); 119 } 120 121 Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) { 122 LLVMContextImpl *pImpl = Context.pImpl; 123 FoldingSetNodeID ID; 124 ID.AddString(Kind); 125 if (!Val.empty()) ID.AddString(Val); 126 127 void *InsertPoint; 128 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint); 129 130 if (!PA) { 131 // If we didn't find any existing attributes of the same shape then create a 132 // new one and insert it. 133 void *Mem = 134 pImpl->Alloc.Allocate(StringAttributeImpl::totalSizeToAlloc(Kind, Val), 135 alignof(StringAttributeImpl)); 136 PA = new (Mem) StringAttributeImpl(Kind, Val); 137 pImpl->AttrsSet.InsertNode(PA, InsertPoint); 138 } 139 140 // Return the Attribute that we found or created. 141 return Attribute(PA); 142 } 143 144 Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind, 145 Type *Ty) { 146 assert(Attribute::isTypeAttrKind(Kind) && "Not a type attribute"); 147 LLVMContextImpl *pImpl = Context.pImpl; 148 FoldingSetNodeID ID; 149 ID.AddInteger(Kind); 150 ID.AddPointer(Ty); 151 152 void *InsertPoint; 153 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint); 154 155 if (!PA) { 156 // If we didn't find any existing attributes of the same shape then create a 157 // new one and insert it. 158 PA = new (pImpl->Alloc) TypeAttributeImpl(Kind, Ty); 159 pImpl->AttrsSet.InsertNode(PA, InsertPoint); 160 } 161 162 // Return the Attribute that we found or created. 163 return Attribute(PA); 164 } 165 166 Attribute Attribute::getWithAlignment(LLVMContext &Context, Align A) { 167 assert(A <= llvm::Value::MaximumAlignment && "Alignment too large."); 168 return get(Context, Alignment, A.value()); 169 } 170 171 Attribute Attribute::getWithStackAlignment(LLVMContext &Context, Align A) { 172 assert(A <= 0x100 && "Alignment too large."); 173 return get(Context, StackAlignment, A.value()); 174 } 175 176 Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context, 177 uint64_t Bytes) { 178 assert(Bytes && "Bytes must be non-zero."); 179 return get(Context, Dereferenceable, Bytes); 180 } 181 182 Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context, 183 uint64_t Bytes) { 184 assert(Bytes && "Bytes must be non-zero."); 185 return get(Context, DereferenceableOrNull, Bytes); 186 } 187 188 Attribute Attribute::getWithByValType(LLVMContext &Context, Type *Ty) { 189 return get(Context, ByVal, Ty); 190 } 191 192 Attribute Attribute::getWithStructRetType(LLVMContext &Context, Type *Ty) { 193 return get(Context, StructRet, Ty); 194 } 195 196 Attribute Attribute::getWithByRefType(LLVMContext &Context, Type *Ty) { 197 return get(Context, ByRef, Ty); 198 } 199 200 Attribute Attribute::getWithPreallocatedType(LLVMContext &Context, Type *Ty) { 201 return get(Context, Preallocated, Ty); 202 } 203 204 Attribute Attribute::getWithInAllocaType(LLVMContext &Context, Type *Ty) { 205 return get(Context, InAlloca, Ty); 206 } 207 208 Attribute 209 Attribute::getWithAllocSizeArgs(LLVMContext &Context, unsigned ElemSizeArg, 210 const Optional<unsigned> &NumElemsArg) { 211 assert(!(ElemSizeArg == 0 && NumElemsArg && *NumElemsArg == 0) && 212 "Invalid allocsize arguments -- given allocsize(0, 0)"); 213 return get(Context, AllocSize, packAllocSizeArgs(ElemSizeArg, NumElemsArg)); 214 } 215 216 Attribute Attribute::getWithVScaleRangeArgs(LLVMContext &Context, 217 unsigned MinValue, 218 unsigned MaxValue) { 219 return get(Context, VScaleRange, packVScaleRangeArgs(MinValue, MaxValue)); 220 } 221 222 Attribute::AttrKind Attribute::getAttrKindFromName(StringRef AttrName) { 223 return StringSwitch<Attribute::AttrKind>(AttrName) 224 #define GET_ATTR_NAMES 225 #define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) \ 226 .Case(#DISPLAY_NAME, Attribute::ENUM_NAME) 227 #include "llvm/IR/Attributes.inc" 228 .Default(Attribute::None); 229 } 230 231 StringRef Attribute::getNameFromAttrKind(Attribute::AttrKind AttrKind) { 232 switch (AttrKind) { 233 #define GET_ATTR_NAMES 234 #define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) \ 235 case Attribute::ENUM_NAME: \ 236 return #DISPLAY_NAME; 237 #include "llvm/IR/Attributes.inc" 238 case Attribute::None: 239 return "none"; 240 default: 241 llvm_unreachable("invalid Kind"); 242 } 243 } 244 245 bool Attribute::isExistingAttribute(StringRef Name) { 246 return StringSwitch<bool>(Name) 247 #define GET_ATTR_NAMES 248 #define ATTRIBUTE_ALL(ENUM_NAME, DISPLAY_NAME) .Case(#DISPLAY_NAME, true) 249 #include "llvm/IR/Attributes.inc" 250 .Default(false); 251 } 252 253 //===----------------------------------------------------------------------===// 254 // Attribute Accessor Methods 255 //===----------------------------------------------------------------------===// 256 257 bool Attribute::isEnumAttribute() const { 258 return pImpl && pImpl->isEnumAttribute(); 259 } 260 261 bool Attribute::isIntAttribute() const { 262 return pImpl && pImpl->isIntAttribute(); 263 } 264 265 bool Attribute::isStringAttribute() const { 266 return pImpl && pImpl->isStringAttribute(); 267 } 268 269 bool Attribute::isTypeAttribute() const { 270 return pImpl && pImpl->isTypeAttribute(); 271 } 272 273 Attribute::AttrKind Attribute::getKindAsEnum() const { 274 if (!pImpl) return None; 275 assert((isEnumAttribute() || isIntAttribute() || isTypeAttribute()) && 276 "Invalid attribute type to get the kind as an enum!"); 277 return pImpl->getKindAsEnum(); 278 } 279 280 uint64_t Attribute::getValueAsInt() const { 281 if (!pImpl) return 0; 282 assert(isIntAttribute() && 283 "Expected the attribute to be an integer attribute!"); 284 return pImpl->getValueAsInt(); 285 } 286 287 bool Attribute::getValueAsBool() const { 288 if (!pImpl) return false; 289 assert(isStringAttribute() && 290 "Expected the attribute to be a string attribute!"); 291 return pImpl->getValueAsBool(); 292 } 293 294 StringRef Attribute::getKindAsString() const { 295 if (!pImpl) return {}; 296 assert(isStringAttribute() && 297 "Invalid attribute type to get the kind as a string!"); 298 return pImpl->getKindAsString(); 299 } 300 301 StringRef Attribute::getValueAsString() const { 302 if (!pImpl) return {}; 303 assert(isStringAttribute() && 304 "Invalid attribute type to get the value as a string!"); 305 return pImpl->getValueAsString(); 306 } 307 308 Type *Attribute::getValueAsType() const { 309 if (!pImpl) return {}; 310 assert(isTypeAttribute() && 311 "Invalid attribute type to get the value as a type!"); 312 return pImpl->getValueAsType(); 313 } 314 315 316 bool Attribute::hasAttribute(AttrKind Kind) const { 317 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None); 318 } 319 320 bool Attribute::hasAttribute(StringRef Kind) const { 321 if (!isStringAttribute()) return false; 322 return pImpl && pImpl->hasAttribute(Kind); 323 } 324 325 MaybeAlign Attribute::getAlignment() const { 326 assert(hasAttribute(Attribute::Alignment) && 327 "Trying to get alignment from non-alignment attribute!"); 328 return MaybeAlign(pImpl->getValueAsInt()); 329 } 330 331 MaybeAlign Attribute::getStackAlignment() const { 332 assert(hasAttribute(Attribute::StackAlignment) && 333 "Trying to get alignment from non-alignment attribute!"); 334 return MaybeAlign(pImpl->getValueAsInt()); 335 } 336 337 uint64_t Attribute::getDereferenceableBytes() const { 338 assert(hasAttribute(Attribute::Dereferenceable) && 339 "Trying to get dereferenceable bytes from " 340 "non-dereferenceable attribute!"); 341 return pImpl->getValueAsInt(); 342 } 343 344 uint64_t Attribute::getDereferenceableOrNullBytes() const { 345 assert(hasAttribute(Attribute::DereferenceableOrNull) && 346 "Trying to get dereferenceable bytes from " 347 "non-dereferenceable attribute!"); 348 return pImpl->getValueAsInt(); 349 } 350 351 std::pair<unsigned, Optional<unsigned>> Attribute::getAllocSizeArgs() const { 352 assert(hasAttribute(Attribute::AllocSize) && 353 "Trying to get allocsize args from non-allocsize attribute"); 354 return unpackAllocSizeArgs(pImpl->getValueAsInt()); 355 } 356 357 std::pair<unsigned, unsigned> Attribute::getVScaleRangeArgs() const { 358 assert(hasAttribute(Attribute::VScaleRange) && 359 "Trying to get vscale args from non-vscale attribute"); 360 return unpackVScaleRangeArgs(pImpl->getValueAsInt()); 361 } 362 363 std::string Attribute::getAsString(bool InAttrGrp) const { 364 if (!pImpl) return {}; 365 366 if (isEnumAttribute()) 367 return getNameFromAttrKind(getKindAsEnum()).str(); 368 369 if (isTypeAttribute()) { 370 std::string Result = getNameFromAttrKind(getKindAsEnum()).str(); 371 Result += '('; 372 raw_string_ostream OS(Result); 373 getValueAsType()->print(OS, false, true); 374 OS.flush(); 375 Result += ')'; 376 return Result; 377 } 378 379 // FIXME: These should be output like this: 380 // 381 // align=4 382 // alignstack=8 383 // 384 if (hasAttribute(Attribute::Alignment)) { 385 std::string Result; 386 Result += "align"; 387 Result += (InAttrGrp) ? "=" : " "; 388 Result += utostr(getValueAsInt()); 389 return Result; 390 } 391 392 auto AttrWithBytesToString = [&](const char *Name) { 393 std::string Result; 394 Result += Name; 395 if (InAttrGrp) { 396 Result += "="; 397 Result += utostr(getValueAsInt()); 398 } else { 399 Result += "("; 400 Result += utostr(getValueAsInt()); 401 Result += ")"; 402 } 403 return Result; 404 }; 405 406 if (hasAttribute(Attribute::StackAlignment)) 407 return AttrWithBytesToString("alignstack"); 408 409 if (hasAttribute(Attribute::Dereferenceable)) 410 return AttrWithBytesToString("dereferenceable"); 411 412 if (hasAttribute(Attribute::DereferenceableOrNull)) 413 return AttrWithBytesToString("dereferenceable_or_null"); 414 415 if (hasAttribute(Attribute::AllocSize)) { 416 unsigned ElemSize; 417 Optional<unsigned> NumElems; 418 std::tie(ElemSize, NumElems) = getAllocSizeArgs(); 419 420 std::string Result = "allocsize("; 421 Result += utostr(ElemSize); 422 if (NumElems.hasValue()) { 423 Result += ','; 424 Result += utostr(*NumElems); 425 } 426 Result += ')'; 427 return Result; 428 } 429 430 if (hasAttribute(Attribute::VScaleRange)) { 431 unsigned MinValue, MaxValue; 432 std::tie(MinValue, MaxValue) = getVScaleRangeArgs(); 433 434 std::string Result = "vscale_range("; 435 Result += utostr(MinValue); 436 Result += ','; 437 Result += utostr(MaxValue); 438 Result += ')'; 439 return Result; 440 } 441 442 // Convert target-dependent attributes to strings of the form: 443 // 444 // "kind" 445 // "kind" = "value" 446 // 447 if (isStringAttribute()) { 448 std::string Result; 449 { 450 raw_string_ostream OS(Result); 451 OS << '"' << getKindAsString() << '"'; 452 453 // Since some attribute strings contain special characters that cannot be 454 // printable, those have to be escaped to make the attribute value 455 // printable as is. e.g. "\01__gnu_mcount_nc" 456 const auto &AttrVal = pImpl->getValueAsString(); 457 if (!AttrVal.empty()) { 458 OS << "=\""; 459 printEscapedString(AttrVal, OS); 460 OS << "\""; 461 } 462 } 463 return Result; 464 } 465 466 llvm_unreachable("Unknown attribute"); 467 } 468 469 bool Attribute::hasParentContext(LLVMContext &C) const { 470 assert(isValid() && "invalid Attribute doesn't refer to any context"); 471 FoldingSetNodeID ID; 472 pImpl->Profile(ID); 473 void *Unused; 474 return C.pImpl->AttrsSet.FindNodeOrInsertPos(ID, Unused) == pImpl; 475 } 476 477 bool Attribute::operator<(Attribute A) const { 478 if (!pImpl && !A.pImpl) return false; 479 if (!pImpl) return true; 480 if (!A.pImpl) return false; 481 return *pImpl < *A.pImpl; 482 } 483 484 void Attribute::Profile(FoldingSetNodeID &ID) const { 485 ID.AddPointer(pImpl); 486 } 487 488 enum AttributeProperty { 489 FnAttr = (1 << 0), 490 ParamAttr = (1 << 1), 491 RetAttr = (1 << 2), 492 }; 493 494 #define GET_ATTR_PROP_TABLE 495 #include "llvm/IR/Attributes.inc" 496 497 static bool hasAttributeProperty(Attribute::AttrKind Kind, 498 AttributeProperty Prop) { 499 unsigned Index = Kind - 1; 500 assert(Index < sizeof(AttrPropTable) / sizeof(AttrPropTable[0]) && 501 "Invalid attribute kind"); 502 return AttrPropTable[Index] & Prop; 503 } 504 505 bool Attribute::canUseAsFnAttr(AttrKind Kind) { 506 return hasAttributeProperty(Kind, AttributeProperty::FnAttr); 507 } 508 509 bool Attribute::canUseAsParamAttr(AttrKind Kind) { 510 return hasAttributeProperty(Kind, AttributeProperty::ParamAttr); 511 } 512 513 bool Attribute::canUseAsRetAttr(AttrKind Kind) { 514 return hasAttributeProperty(Kind, AttributeProperty::RetAttr); 515 } 516 517 //===----------------------------------------------------------------------===// 518 // AttributeImpl Definition 519 //===----------------------------------------------------------------------===// 520 521 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const { 522 if (isStringAttribute()) return false; 523 return getKindAsEnum() == A; 524 } 525 526 bool AttributeImpl::hasAttribute(StringRef Kind) const { 527 if (!isStringAttribute()) return false; 528 return getKindAsString() == Kind; 529 } 530 531 Attribute::AttrKind AttributeImpl::getKindAsEnum() const { 532 assert(isEnumAttribute() || isIntAttribute() || isTypeAttribute()); 533 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind(); 534 } 535 536 uint64_t AttributeImpl::getValueAsInt() const { 537 assert(isIntAttribute()); 538 return static_cast<const IntAttributeImpl *>(this)->getValue(); 539 } 540 541 bool AttributeImpl::getValueAsBool() const { 542 assert(getValueAsString().empty() || getValueAsString() == "false" || getValueAsString() == "true"); 543 return getValueAsString() == "true"; 544 } 545 546 StringRef AttributeImpl::getKindAsString() const { 547 assert(isStringAttribute()); 548 return static_cast<const StringAttributeImpl *>(this)->getStringKind(); 549 } 550 551 StringRef AttributeImpl::getValueAsString() const { 552 assert(isStringAttribute()); 553 return static_cast<const StringAttributeImpl *>(this)->getStringValue(); 554 } 555 556 Type *AttributeImpl::getValueAsType() const { 557 assert(isTypeAttribute()); 558 return static_cast<const TypeAttributeImpl *>(this)->getTypeValue(); 559 } 560 561 bool AttributeImpl::operator<(const AttributeImpl &AI) const { 562 if (this == &AI) 563 return false; 564 565 // This sorts the attributes with Attribute::AttrKinds coming first (sorted 566 // relative to their enum value) and then strings. 567 if (!isStringAttribute()) { 568 if (AI.isStringAttribute()) 569 return true; 570 if (getKindAsEnum() != AI.getKindAsEnum()) 571 return getKindAsEnum() < AI.getKindAsEnum(); 572 assert(!AI.isEnumAttribute() && "Non-unique attribute"); 573 assert(!AI.isTypeAttribute() && "Comparison of types would be unstable"); 574 // TODO: Is this actually needed? 575 assert(AI.isIntAttribute() && "Only possibility left"); 576 return getValueAsInt() < AI.getValueAsInt(); 577 } 578 579 if (!AI.isStringAttribute()) 580 return false; 581 if (getKindAsString() == AI.getKindAsString()) 582 return getValueAsString() < AI.getValueAsString(); 583 return getKindAsString() < AI.getKindAsString(); 584 } 585 586 //===----------------------------------------------------------------------===// 587 // AttributeSet Definition 588 //===----------------------------------------------------------------------===// 589 590 AttributeSet AttributeSet::get(LLVMContext &C, const AttrBuilder &B) { 591 return AttributeSet(AttributeSetNode::get(C, B)); 592 } 593 594 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<Attribute> Attrs) { 595 return AttributeSet(AttributeSetNode::get(C, Attrs)); 596 } 597 598 AttributeSet AttributeSet::addAttribute(LLVMContext &C, 599 Attribute::AttrKind Kind) const { 600 if (hasAttribute(Kind)) return *this; 601 AttrBuilder B; 602 B.addAttribute(Kind); 603 return addAttributes(C, AttributeSet::get(C, B)); 604 } 605 606 AttributeSet AttributeSet::addAttribute(LLVMContext &C, StringRef Kind, 607 StringRef Value) const { 608 AttrBuilder B; 609 B.addAttribute(Kind, Value); 610 return addAttributes(C, AttributeSet::get(C, B)); 611 } 612 613 AttributeSet AttributeSet::addAttributes(LLVMContext &C, 614 const AttributeSet AS) const { 615 if (!hasAttributes()) 616 return AS; 617 618 if (!AS.hasAttributes()) 619 return *this; 620 621 AttrBuilder B(AS); 622 for (const auto &I : *this) 623 B.addAttribute(I); 624 625 return get(C, B); 626 } 627 628 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, 629 Attribute::AttrKind Kind) const { 630 if (!hasAttribute(Kind)) return *this; 631 AttrBuilder B(*this); 632 B.removeAttribute(Kind); 633 return get(C, B); 634 } 635 636 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, 637 StringRef Kind) const { 638 if (!hasAttribute(Kind)) return *this; 639 AttrBuilder B(*this); 640 B.removeAttribute(Kind); 641 return get(C, B); 642 } 643 644 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, 645 const AttrBuilder &Attrs) const { 646 AttrBuilder B(*this); 647 // If there is nothing to remove, directly return the original set. 648 if (!B.overlaps(Attrs)) 649 return *this; 650 651 B.remove(Attrs); 652 return get(C, B); 653 } 654 655 unsigned AttributeSet::getNumAttributes() const { 656 return SetNode ? SetNode->getNumAttributes() : 0; 657 } 658 659 bool AttributeSet::hasAttribute(Attribute::AttrKind Kind) const { 660 return SetNode ? SetNode->hasAttribute(Kind) : false; 661 } 662 663 bool AttributeSet::hasAttribute(StringRef Kind) const { 664 return SetNode ? SetNode->hasAttribute(Kind) : false; 665 } 666 667 Attribute AttributeSet::getAttribute(Attribute::AttrKind Kind) const { 668 return SetNode ? SetNode->getAttribute(Kind) : Attribute(); 669 } 670 671 Attribute AttributeSet::getAttribute(StringRef Kind) const { 672 return SetNode ? SetNode->getAttribute(Kind) : Attribute(); 673 } 674 675 MaybeAlign AttributeSet::getAlignment() const { 676 return SetNode ? SetNode->getAlignment() : None; 677 } 678 679 MaybeAlign AttributeSet::getStackAlignment() const { 680 return SetNode ? SetNode->getStackAlignment() : None; 681 } 682 683 uint64_t AttributeSet::getDereferenceableBytes() const { 684 return SetNode ? SetNode->getDereferenceableBytes() : 0; 685 } 686 687 uint64_t AttributeSet::getDereferenceableOrNullBytes() const { 688 return SetNode ? SetNode->getDereferenceableOrNullBytes() : 0; 689 } 690 691 Type *AttributeSet::getByRefType() const { 692 return SetNode ? SetNode->getAttributeType(Attribute::ByRef) : nullptr; 693 } 694 695 Type *AttributeSet::getByValType() const { 696 return SetNode ? SetNode->getAttributeType(Attribute::ByVal) : nullptr; 697 } 698 699 Type *AttributeSet::getStructRetType() const { 700 return SetNode ? SetNode->getAttributeType(Attribute::StructRet) : nullptr; 701 } 702 703 Type *AttributeSet::getPreallocatedType() const { 704 return SetNode ? SetNode->getAttributeType(Attribute::Preallocated) : nullptr; 705 } 706 707 Type *AttributeSet::getInAllocaType() const { 708 return SetNode ? SetNode->getAttributeType(Attribute::InAlloca) : nullptr; 709 } 710 711 Type *AttributeSet::getElementType() const { 712 return SetNode ? SetNode->getAttributeType(Attribute::ElementType) : nullptr; 713 } 714 715 std::pair<unsigned, Optional<unsigned>> AttributeSet::getAllocSizeArgs() const { 716 return SetNode ? SetNode->getAllocSizeArgs() 717 : std::pair<unsigned, Optional<unsigned>>(0, 0); 718 } 719 720 std::pair<unsigned, unsigned> AttributeSet::getVScaleRangeArgs() const { 721 return SetNode ? SetNode->getVScaleRangeArgs() 722 : std::pair<unsigned, unsigned>(0, 0); 723 } 724 725 std::string AttributeSet::getAsString(bool InAttrGrp) const { 726 return SetNode ? SetNode->getAsString(InAttrGrp) : ""; 727 } 728 729 bool AttributeSet::hasParentContext(LLVMContext &C) const { 730 assert(hasAttributes() && "empty AttributeSet doesn't refer to any context"); 731 FoldingSetNodeID ID; 732 SetNode->Profile(ID); 733 void *Unused; 734 return C.pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, Unused) == SetNode; 735 } 736 737 AttributeSet::iterator AttributeSet::begin() const { 738 return SetNode ? SetNode->begin() : nullptr; 739 } 740 741 AttributeSet::iterator AttributeSet::end() const { 742 return SetNode ? SetNode->end() : nullptr; 743 } 744 745 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 746 LLVM_DUMP_METHOD void AttributeSet::dump() const { 747 dbgs() << "AS =\n"; 748 dbgs() << " { "; 749 dbgs() << getAsString(true) << " }\n"; 750 } 751 #endif 752 753 //===----------------------------------------------------------------------===// 754 // AttributeSetNode Definition 755 //===----------------------------------------------------------------------===// 756 757 AttributeSetNode::AttributeSetNode(ArrayRef<Attribute> Attrs) 758 : NumAttrs(Attrs.size()) { 759 // There's memory after the node where we can store the entries in. 760 llvm::copy(Attrs, getTrailingObjects<Attribute>()); 761 762 for (const auto &I : *this) { 763 if (I.isStringAttribute()) 764 StringAttrs.insert({ I.getKindAsString(), I }); 765 else 766 AvailableAttrs.addAttribute(I.getKindAsEnum()); 767 } 768 } 769 770 AttributeSetNode *AttributeSetNode::get(LLVMContext &C, 771 ArrayRef<Attribute> Attrs) { 772 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end()); 773 llvm::sort(SortedAttrs); 774 return getSorted(C, SortedAttrs); 775 } 776 777 AttributeSetNode *AttributeSetNode::getSorted(LLVMContext &C, 778 ArrayRef<Attribute> SortedAttrs) { 779 if (SortedAttrs.empty()) 780 return nullptr; 781 782 // Build a key to look up the existing attributes. 783 LLVMContextImpl *pImpl = C.pImpl; 784 FoldingSetNodeID ID; 785 786 assert(llvm::is_sorted(SortedAttrs) && "Expected sorted attributes!"); 787 for (const auto &Attr : SortedAttrs) 788 Attr.Profile(ID); 789 790 void *InsertPoint; 791 AttributeSetNode *PA = 792 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint); 793 794 // If we didn't find any existing attributes of the same shape then create a 795 // new one and insert it. 796 if (!PA) { 797 // Coallocate entries after the AttributeSetNode itself. 798 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size())); 799 PA = new (Mem) AttributeSetNode(SortedAttrs); 800 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint); 801 } 802 803 // Return the AttributeSetNode that we found or created. 804 return PA; 805 } 806 807 AttributeSetNode *AttributeSetNode::get(LLVMContext &C, const AttrBuilder &B) { 808 // Add target-independent attributes. 809 SmallVector<Attribute, 8> Attrs; 810 for (Attribute::AttrKind Kind = Attribute::None; 811 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) { 812 if (!B.contains(Kind)) 813 continue; 814 815 Attribute Attr; 816 if (Attribute::isTypeAttrKind(Kind)) 817 Attr = Attribute::get(C, Kind, B.getTypeAttr(Kind)); 818 else if (Attribute::isIntAttrKind(Kind)) 819 Attr = Attribute::get(C, Kind, B.getRawIntAttr(Kind)); 820 else 821 Attr = Attribute::get(C, Kind); 822 Attrs.push_back(Attr); 823 } 824 825 // Add target-dependent (string) attributes. 826 for (const auto &TDA : B.td_attrs()) 827 Attrs.emplace_back(Attribute::get(C, TDA.first, TDA.second)); 828 829 return getSorted(C, Attrs); 830 } 831 832 bool AttributeSetNode::hasAttribute(StringRef Kind) const { 833 return StringAttrs.count(Kind); 834 } 835 836 Optional<Attribute> 837 AttributeSetNode::findEnumAttribute(Attribute::AttrKind Kind) const { 838 // Do a quick presence check. 839 if (!hasAttribute(Kind)) 840 return None; 841 842 // Attributes in a set are sorted by enum value, followed by string 843 // attributes. Binary search the one we want. 844 const Attribute *I = 845 std::lower_bound(begin(), end() - StringAttrs.size(), Kind, 846 [](Attribute A, Attribute::AttrKind Kind) { 847 return A.getKindAsEnum() < Kind; 848 }); 849 assert(I != end() && I->hasAttribute(Kind) && "Presence check failed?"); 850 return *I; 851 } 852 853 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const { 854 if (auto A = findEnumAttribute(Kind)) 855 return *A; 856 return {}; 857 } 858 859 Attribute AttributeSetNode::getAttribute(StringRef Kind) const { 860 return StringAttrs.lookup(Kind); 861 } 862 863 MaybeAlign AttributeSetNode::getAlignment() const { 864 if (auto A = findEnumAttribute(Attribute::Alignment)) 865 return A->getAlignment(); 866 return None; 867 } 868 869 MaybeAlign AttributeSetNode::getStackAlignment() const { 870 if (auto A = findEnumAttribute(Attribute::StackAlignment)) 871 return A->getStackAlignment(); 872 return None; 873 } 874 875 Type *AttributeSetNode::getAttributeType(Attribute::AttrKind Kind) const { 876 if (auto A = findEnumAttribute(Kind)) 877 return A->getValueAsType(); 878 return nullptr; 879 } 880 881 uint64_t AttributeSetNode::getDereferenceableBytes() const { 882 if (auto A = findEnumAttribute(Attribute::Dereferenceable)) 883 return A->getDereferenceableBytes(); 884 return 0; 885 } 886 887 uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const { 888 if (auto A = findEnumAttribute(Attribute::DereferenceableOrNull)) 889 return A->getDereferenceableOrNullBytes(); 890 return 0; 891 } 892 893 std::pair<unsigned, Optional<unsigned>> 894 AttributeSetNode::getAllocSizeArgs() const { 895 if (auto A = findEnumAttribute(Attribute::AllocSize)) 896 return A->getAllocSizeArgs(); 897 return std::make_pair(0, 0); 898 } 899 900 std::pair<unsigned, unsigned> AttributeSetNode::getVScaleRangeArgs() const { 901 if (auto A = findEnumAttribute(Attribute::VScaleRange)) 902 return A->getVScaleRangeArgs(); 903 return std::make_pair(0, 0); 904 } 905 906 std::string AttributeSetNode::getAsString(bool InAttrGrp) const { 907 std::string Str; 908 for (iterator I = begin(), E = end(); I != E; ++I) { 909 if (I != begin()) 910 Str += ' '; 911 Str += I->getAsString(InAttrGrp); 912 } 913 return Str; 914 } 915 916 //===----------------------------------------------------------------------===// 917 // AttributeListImpl Definition 918 //===----------------------------------------------------------------------===// 919 920 /// Map from AttributeList index to the internal array index. Adding one happens 921 /// to work, because -1 wraps around to 0. 922 static unsigned attrIdxToArrayIdx(unsigned Index) { 923 return Index + 1; 924 } 925 926 AttributeListImpl::AttributeListImpl(ArrayRef<AttributeSet> Sets) 927 : NumAttrSets(Sets.size()) { 928 assert(!Sets.empty() && "pointless AttributeListImpl"); 929 930 // There's memory after the node where we can store the entries in. 931 llvm::copy(Sets, getTrailingObjects<AttributeSet>()); 932 933 // Initialize AvailableFunctionAttrs and AvailableSomewhereAttrs 934 // summary bitsets. 935 for (const auto &I : Sets[attrIdxToArrayIdx(AttributeList::FunctionIndex)]) 936 if (!I.isStringAttribute()) 937 AvailableFunctionAttrs.addAttribute(I.getKindAsEnum()); 938 939 for (const auto &Set : Sets) 940 for (const auto &I : Set) 941 if (!I.isStringAttribute()) 942 AvailableSomewhereAttrs.addAttribute(I.getKindAsEnum()); 943 } 944 945 void AttributeListImpl::Profile(FoldingSetNodeID &ID) const { 946 Profile(ID, makeArrayRef(begin(), end())); 947 } 948 949 void AttributeListImpl::Profile(FoldingSetNodeID &ID, 950 ArrayRef<AttributeSet> Sets) { 951 for (const auto &Set : Sets) 952 ID.AddPointer(Set.SetNode); 953 } 954 955 bool AttributeListImpl::hasAttrSomewhere(Attribute::AttrKind Kind, 956 unsigned *Index) const { 957 if (!AvailableSomewhereAttrs.hasAttribute(Kind)) 958 return false; 959 960 if (Index) { 961 for (unsigned I = 0, E = NumAttrSets; I != E; ++I) { 962 if (begin()[I].hasAttribute(Kind)) { 963 *Index = I - 1; 964 break; 965 } 966 } 967 } 968 969 return true; 970 } 971 972 973 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 974 LLVM_DUMP_METHOD void AttributeListImpl::dump() const { 975 AttributeList(const_cast<AttributeListImpl *>(this)).dump(); 976 } 977 #endif 978 979 //===----------------------------------------------------------------------===// 980 // AttributeList Construction and Mutation Methods 981 //===----------------------------------------------------------------------===// 982 983 AttributeList AttributeList::getImpl(LLVMContext &C, 984 ArrayRef<AttributeSet> AttrSets) { 985 assert(!AttrSets.empty() && "pointless AttributeListImpl"); 986 987 LLVMContextImpl *pImpl = C.pImpl; 988 FoldingSetNodeID ID; 989 AttributeListImpl::Profile(ID, AttrSets); 990 991 void *InsertPoint; 992 AttributeListImpl *PA = 993 pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint); 994 995 // If we didn't find any existing attributes of the same shape then 996 // create a new one and insert it. 997 if (!PA) { 998 // Coallocate entries after the AttributeListImpl itself. 999 void *Mem = pImpl->Alloc.Allocate( 1000 AttributeListImpl::totalSizeToAlloc<AttributeSet>(AttrSets.size()), 1001 alignof(AttributeListImpl)); 1002 PA = new (Mem) AttributeListImpl(AttrSets); 1003 pImpl->AttrsLists.InsertNode(PA, InsertPoint); 1004 } 1005 1006 // Return the AttributesList that we found or created. 1007 return AttributeList(PA); 1008 } 1009 1010 AttributeList 1011 AttributeList::get(LLVMContext &C, 1012 ArrayRef<std::pair<unsigned, Attribute>> Attrs) { 1013 // If there are no attributes then return a null AttributesList pointer. 1014 if (Attrs.empty()) 1015 return {}; 1016 1017 assert(llvm::is_sorted(Attrs, 1018 [](const std::pair<unsigned, Attribute> &LHS, 1019 const std::pair<unsigned, Attribute> &RHS) { 1020 return LHS.first < RHS.first; 1021 }) && 1022 "Misordered Attributes list!"); 1023 assert(llvm::all_of(Attrs, 1024 [](const std::pair<unsigned, Attribute> &Pair) { 1025 return Pair.second.isValid(); 1026 }) && 1027 "Pointless attribute!"); 1028 1029 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes 1030 // list. 1031 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairVec; 1032 for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(), 1033 E = Attrs.end(); I != E; ) { 1034 unsigned Index = I->first; 1035 SmallVector<Attribute, 4> AttrVec; 1036 while (I != E && I->first == Index) { 1037 AttrVec.push_back(I->second); 1038 ++I; 1039 } 1040 1041 AttrPairVec.emplace_back(Index, AttributeSet::get(C, AttrVec)); 1042 } 1043 1044 return get(C, AttrPairVec); 1045 } 1046 1047 AttributeList 1048 AttributeList::get(LLVMContext &C, 1049 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) { 1050 // If there are no attributes then return a null AttributesList pointer. 1051 if (Attrs.empty()) 1052 return {}; 1053 1054 assert(llvm::is_sorted(Attrs, 1055 [](const std::pair<unsigned, AttributeSet> &LHS, 1056 const std::pair<unsigned, AttributeSet> &RHS) { 1057 return LHS.first < RHS.first; 1058 }) && 1059 "Misordered Attributes list!"); 1060 assert(llvm::none_of(Attrs, 1061 [](const std::pair<unsigned, AttributeSet> &Pair) { 1062 return !Pair.second.hasAttributes(); 1063 }) && 1064 "Pointless attribute!"); 1065 1066 unsigned MaxIndex = Attrs.back().first; 1067 // If the MaxIndex is FunctionIndex and there are other indices in front 1068 // of it, we need to use the largest of those to get the right size. 1069 if (MaxIndex == FunctionIndex && Attrs.size() > 1) 1070 MaxIndex = Attrs[Attrs.size() - 2].first; 1071 1072 SmallVector<AttributeSet, 4> AttrVec(attrIdxToArrayIdx(MaxIndex) + 1); 1073 for (const auto &Pair : Attrs) 1074 AttrVec[attrIdxToArrayIdx(Pair.first)] = Pair.second; 1075 1076 return getImpl(C, AttrVec); 1077 } 1078 1079 AttributeList AttributeList::get(LLVMContext &C, AttributeSet FnAttrs, 1080 AttributeSet RetAttrs, 1081 ArrayRef<AttributeSet> ArgAttrs) { 1082 // Scan from the end to find the last argument with attributes. Most 1083 // arguments don't have attributes, so it's nice if we can have fewer unique 1084 // AttributeListImpls by dropping empty attribute sets at the end of the list. 1085 unsigned NumSets = 0; 1086 for (size_t I = ArgAttrs.size(); I != 0; --I) { 1087 if (ArgAttrs[I - 1].hasAttributes()) { 1088 NumSets = I + 2; 1089 break; 1090 } 1091 } 1092 if (NumSets == 0) { 1093 // Check function and return attributes if we didn't have argument 1094 // attributes. 1095 if (RetAttrs.hasAttributes()) 1096 NumSets = 2; 1097 else if (FnAttrs.hasAttributes()) 1098 NumSets = 1; 1099 } 1100 1101 // If all attribute sets were empty, we can use the empty attribute list. 1102 if (NumSets == 0) 1103 return {}; 1104 1105 SmallVector<AttributeSet, 8> AttrSets; 1106 AttrSets.reserve(NumSets); 1107 // If we have any attributes, we always have function attributes. 1108 AttrSets.push_back(FnAttrs); 1109 if (NumSets > 1) 1110 AttrSets.push_back(RetAttrs); 1111 if (NumSets > 2) { 1112 // Drop the empty argument attribute sets at the end. 1113 ArgAttrs = ArgAttrs.take_front(NumSets - 2); 1114 llvm::append_range(AttrSets, ArgAttrs); 1115 } 1116 1117 return getImpl(C, AttrSets); 1118 } 1119 1120 AttributeList AttributeList::get(LLVMContext &C, unsigned Index, 1121 const AttrBuilder &B) { 1122 if (!B.hasAttributes()) 1123 return {}; 1124 Index = attrIdxToArrayIdx(Index); 1125 SmallVector<AttributeSet, 8> AttrSets(Index + 1); 1126 AttrSets[Index] = AttributeSet::get(C, B); 1127 return getImpl(C, AttrSets); 1128 } 1129 1130 AttributeList AttributeList::get(LLVMContext &C, unsigned Index, 1131 ArrayRef<Attribute::AttrKind> Kinds) { 1132 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs; 1133 for (const auto K : Kinds) 1134 Attrs.emplace_back(Index, Attribute::get(C, K)); 1135 return get(C, Attrs); 1136 } 1137 1138 AttributeList AttributeList::get(LLVMContext &C, unsigned Index, 1139 ArrayRef<Attribute::AttrKind> Kinds, 1140 ArrayRef<uint64_t> Values) { 1141 assert(Kinds.size() == Values.size() && "Mismatched attribute values."); 1142 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs; 1143 auto VI = Values.begin(); 1144 for (const auto K : Kinds) 1145 Attrs.emplace_back(Index, Attribute::get(C, K, *VI++)); 1146 return get(C, Attrs); 1147 } 1148 1149 AttributeList AttributeList::get(LLVMContext &C, unsigned Index, 1150 ArrayRef<StringRef> Kinds) { 1151 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs; 1152 for (const auto &K : Kinds) 1153 Attrs.emplace_back(Index, Attribute::get(C, K)); 1154 return get(C, Attrs); 1155 } 1156 1157 AttributeList AttributeList::get(LLVMContext &C, 1158 ArrayRef<AttributeList> Attrs) { 1159 if (Attrs.empty()) 1160 return {}; 1161 if (Attrs.size() == 1) 1162 return Attrs[0]; 1163 1164 unsigned MaxSize = 0; 1165 for (const auto &List : Attrs) 1166 MaxSize = std::max(MaxSize, List.getNumAttrSets()); 1167 1168 // If every list was empty, there is no point in merging the lists. 1169 if (MaxSize == 0) 1170 return {}; 1171 1172 SmallVector<AttributeSet, 8> NewAttrSets(MaxSize); 1173 for (unsigned I = 0; I < MaxSize; ++I) { 1174 AttrBuilder CurBuilder; 1175 for (const auto &List : Attrs) 1176 CurBuilder.merge(List.getAttributes(I - 1)); 1177 NewAttrSets[I] = AttributeSet::get(C, CurBuilder); 1178 } 1179 1180 return getImpl(C, NewAttrSets); 1181 } 1182 1183 AttributeList 1184 AttributeList::addAttributeAtIndex(LLVMContext &C, unsigned Index, 1185 Attribute::AttrKind Kind) const { 1186 if (hasAttributeAtIndex(Index, Kind)) 1187 return *this; 1188 AttributeSet Attrs = getAttributes(Index); 1189 // TODO: Insert at correct position and avoid sort. 1190 SmallVector<Attribute, 8> NewAttrs(Attrs.begin(), Attrs.end()); 1191 NewAttrs.push_back(Attribute::get(C, Kind)); 1192 return setAttributesAtIndex(C, Index, AttributeSet::get(C, NewAttrs)); 1193 } 1194 1195 AttributeList AttributeList::addAttributeAtIndex(LLVMContext &C, unsigned Index, 1196 StringRef Kind, 1197 StringRef Value) const { 1198 AttrBuilder B; 1199 B.addAttribute(Kind, Value); 1200 return addAttributesAtIndex(C, Index, B); 1201 } 1202 1203 AttributeList AttributeList::addAttributeAtIndex(LLVMContext &C, unsigned Index, 1204 Attribute A) const { 1205 AttrBuilder B; 1206 B.addAttribute(A); 1207 return addAttributesAtIndex(C, Index, B); 1208 } 1209 1210 AttributeList AttributeList::setAttributesAtIndex(LLVMContext &C, 1211 unsigned Index, 1212 AttributeSet Attrs) const { 1213 Index = attrIdxToArrayIdx(Index); 1214 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end()); 1215 if (Index >= AttrSets.size()) 1216 AttrSets.resize(Index + 1); 1217 AttrSets[Index] = Attrs; 1218 return AttributeList::getImpl(C, AttrSets); 1219 } 1220 1221 AttributeList AttributeList::addAttributesAtIndex(LLVMContext &C, 1222 unsigned Index, 1223 const AttrBuilder &B) const { 1224 if (!B.hasAttributes()) 1225 return *this; 1226 1227 if (!pImpl) 1228 return AttributeList::get(C, {{Index, AttributeSet::get(C, B)}}); 1229 1230 #ifndef NDEBUG 1231 // FIXME it is not obvious how this should work for alignment. For now, say 1232 // we can't change a known alignment. 1233 const MaybeAlign OldAlign = getAttributes(Index).getAlignment(); 1234 const MaybeAlign NewAlign = B.getAlignment(); 1235 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) && 1236 "Attempt to change alignment!"); 1237 #endif 1238 1239 AttrBuilder Merged(getAttributes(Index)); 1240 Merged.merge(B); 1241 return setAttributesAtIndex(C, Index, AttributeSet::get(C, Merged)); 1242 } 1243 1244 AttributeList AttributeList::addParamAttribute(LLVMContext &C, 1245 ArrayRef<unsigned> ArgNos, 1246 Attribute A) const { 1247 assert(llvm::is_sorted(ArgNos)); 1248 1249 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end()); 1250 unsigned MaxIndex = attrIdxToArrayIdx(ArgNos.back() + FirstArgIndex); 1251 if (MaxIndex >= AttrSets.size()) 1252 AttrSets.resize(MaxIndex + 1); 1253 1254 for (unsigned ArgNo : ArgNos) { 1255 unsigned Index = attrIdxToArrayIdx(ArgNo + FirstArgIndex); 1256 AttrBuilder B(AttrSets[Index]); 1257 B.addAttribute(A); 1258 AttrSets[Index] = AttributeSet::get(C, B); 1259 } 1260 1261 return getImpl(C, AttrSets); 1262 } 1263 1264 AttributeList 1265 AttributeList::removeAttributeAtIndex(LLVMContext &C, unsigned Index, 1266 Attribute::AttrKind Kind) const { 1267 if (!hasAttributeAtIndex(Index, Kind)) 1268 return *this; 1269 1270 Index = attrIdxToArrayIdx(Index); 1271 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end()); 1272 assert(Index < AttrSets.size()); 1273 1274 AttrSets[Index] = AttrSets[Index].removeAttribute(C, Kind); 1275 1276 return getImpl(C, AttrSets); 1277 } 1278 1279 AttributeList AttributeList::removeAttributeAtIndex(LLVMContext &C, 1280 unsigned Index, 1281 StringRef Kind) const { 1282 if (!hasAttributeAtIndex(Index, Kind)) 1283 return *this; 1284 1285 Index = attrIdxToArrayIdx(Index); 1286 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end()); 1287 assert(Index < AttrSets.size()); 1288 1289 AttrSets[Index] = AttrSets[Index].removeAttribute(C, Kind); 1290 1291 return getImpl(C, AttrSets); 1292 } 1293 1294 AttributeList 1295 AttributeList::removeAttributesAtIndex(LLVMContext &C, unsigned Index, 1296 const AttrBuilder &AttrsToRemove) const { 1297 AttributeSet Attrs = getAttributes(Index); 1298 AttributeSet NewAttrs = Attrs.removeAttributes(C, AttrsToRemove); 1299 // If nothing was removed, return the original list. 1300 if (Attrs == NewAttrs) 1301 return *this; 1302 return setAttributesAtIndex(C, Index, NewAttrs); 1303 } 1304 1305 AttributeList 1306 AttributeList::removeAttributesAtIndex(LLVMContext &C, 1307 unsigned WithoutIndex) const { 1308 if (!pImpl) 1309 return {}; 1310 WithoutIndex = attrIdxToArrayIdx(WithoutIndex); 1311 if (WithoutIndex >= getNumAttrSets()) 1312 return *this; 1313 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end()); 1314 AttrSets[WithoutIndex] = AttributeSet(); 1315 return getImpl(C, AttrSets); 1316 } 1317 1318 AttributeList AttributeList::addDereferenceableRetAttr(LLVMContext &C, 1319 uint64_t Bytes) const { 1320 AttrBuilder B; 1321 B.addDereferenceableAttr(Bytes); 1322 return addRetAttributes(C, B); 1323 } 1324 1325 AttributeList AttributeList::addDereferenceableParamAttr(LLVMContext &C, 1326 unsigned Index, 1327 uint64_t Bytes) const { 1328 AttrBuilder B; 1329 B.addDereferenceableAttr(Bytes); 1330 return addParamAttributes(C, Index, B); 1331 } 1332 1333 AttributeList 1334 AttributeList::addDereferenceableOrNullParamAttr(LLVMContext &C, unsigned Index, 1335 uint64_t Bytes) const { 1336 AttrBuilder B; 1337 B.addDereferenceableOrNullAttr(Bytes); 1338 return addParamAttributes(C, Index, B); 1339 } 1340 1341 AttributeList 1342 AttributeList::addAllocSizeParamAttr(LLVMContext &C, unsigned Index, 1343 unsigned ElemSizeArg, 1344 const Optional<unsigned> &NumElemsArg) { 1345 AttrBuilder B; 1346 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg); 1347 return addParamAttributes(C, Index, B); 1348 } 1349 1350 //===----------------------------------------------------------------------===// 1351 // AttributeList Accessor Methods 1352 //===----------------------------------------------------------------------===// 1353 1354 AttributeSet AttributeList::getParamAttrs(unsigned ArgNo) const { 1355 return getAttributes(ArgNo + FirstArgIndex); 1356 } 1357 1358 AttributeSet AttributeList::getRetAttrs() const { 1359 return getAttributes(ReturnIndex); 1360 } 1361 1362 AttributeSet AttributeList::getFnAttrs() const { 1363 return getAttributes(FunctionIndex); 1364 } 1365 1366 bool AttributeList::hasAttributeAtIndex(unsigned Index, 1367 Attribute::AttrKind Kind) const { 1368 return getAttributes(Index).hasAttribute(Kind); 1369 } 1370 1371 bool AttributeList::hasAttributeAtIndex(unsigned Index, StringRef Kind) const { 1372 return getAttributes(Index).hasAttribute(Kind); 1373 } 1374 1375 bool AttributeList::hasAttributesAtIndex(unsigned Index) const { 1376 return getAttributes(Index).hasAttributes(); 1377 } 1378 1379 bool AttributeList::hasFnAttr(Attribute::AttrKind Kind) const { 1380 return pImpl && pImpl->hasFnAttribute(Kind); 1381 } 1382 1383 bool AttributeList::hasFnAttr(StringRef Kind) const { 1384 return hasAttributeAtIndex(AttributeList::FunctionIndex, Kind); 1385 } 1386 1387 bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr, 1388 unsigned *Index) const { 1389 return pImpl && pImpl->hasAttrSomewhere(Attr, Index); 1390 } 1391 1392 Attribute AttributeList::getAttributeAtIndex(unsigned Index, 1393 Attribute::AttrKind Kind) const { 1394 return getAttributes(Index).getAttribute(Kind); 1395 } 1396 1397 Attribute AttributeList::getAttributeAtIndex(unsigned Index, 1398 StringRef Kind) const { 1399 return getAttributes(Index).getAttribute(Kind); 1400 } 1401 1402 MaybeAlign AttributeList::getRetAlignment() const { 1403 return getAttributes(ReturnIndex).getAlignment(); 1404 } 1405 1406 MaybeAlign AttributeList::getParamAlignment(unsigned ArgNo) const { 1407 return getAttributes(ArgNo + FirstArgIndex).getAlignment(); 1408 } 1409 1410 MaybeAlign AttributeList::getParamStackAlignment(unsigned ArgNo) const { 1411 return getAttributes(ArgNo + FirstArgIndex).getStackAlignment(); 1412 } 1413 1414 Type *AttributeList::getParamByValType(unsigned Index) const { 1415 return getAttributes(Index+FirstArgIndex).getByValType(); 1416 } 1417 1418 Type *AttributeList::getParamStructRetType(unsigned Index) const { 1419 return getAttributes(Index + FirstArgIndex).getStructRetType(); 1420 } 1421 1422 Type *AttributeList::getParamByRefType(unsigned Index) const { 1423 return getAttributes(Index + FirstArgIndex).getByRefType(); 1424 } 1425 1426 Type *AttributeList::getParamPreallocatedType(unsigned Index) const { 1427 return getAttributes(Index + FirstArgIndex).getPreallocatedType(); 1428 } 1429 1430 Type *AttributeList::getParamInAllocaType(unsigned Index) const { 1431 return getAttributes(Index + FirstArgIndex).getInAllocaType(); 1432 } 1433 1434 Type *AttributeList::getParamElementType(unsigned Index) const { 1435 return getAttributes(Index + FirstArgIndex).getElementType(); 1436 } 1437 1438 MaybeAlign AttributeList::getFnStackAlignment() const { 1439 return getFnAttrs().getStackAlignment(); 1440 } 1441 1442 MaybeAlign AttributeList::getRetStackAlignment() const { 1443 return getRetAttrs().getStackAlignment(); 1444 } 1445 1446 uint64_t AttributeList::getRetDereferenceableBytes() const { 1447 return getRetAttrs().getDereferenceableBytes(); 1448 } 1449 1450 uint64_t AttributeList::getParamDereferenceableBytes(unsigned Index) const { 1451 return getParamAttrs(Index).getDereferenceableBytes(); 1452 } 1453 1454 uint64_t AttributeList::getRetDereferenceableOrNullBytes() const { 1455 return getRetAttrs().getDereferenceableOrNullBytes(); 1456 } 1457 1458 uint64_t 1459 AttributeList::getParamDereferenceableOrNullBytes(unsigned Index) const { 1460 return getParamAttrs(Index).getDereferenceableOrNullBytes(); 1461 } 1462 1463 std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const { 1464 return getAttributes(Index).getAsString(InAttrGrp); 1465 } 1466 1467 AttributeSet AttributeList::getAttributes(unsigned Index) const { 1468 Index = attrIdxToArrayIdx(Index); 1469 if (!pImpl || Index >= getNumAttrSets()) 1470 return {}; 1471 return pImpl->begin()[Index]; 1472 } 1473 1474 bool AttributeList::hasParentContext(LLVMContext &C) const { 1475 assert(!isEmpty() && "an empty attribute list has no parent context"); 1476 FoldingSetNodeID ID; 1477 pImpl->Profile(ID); 1478 void *Unused; 1479 return C.pImpl->AttrsLists.FindNodeOrInsertPos(ID, Unused) == pImpl; 1480 } 1481 1482 AttributeList::iterator AttributeList::begin() const { 1483 return pImpl ? pImpl->begin() : nullptr; 1484 } 1485 1486 AttributeList::iterator AttributeList::end() const { 1487 return pImpl ? pImpl->end() : nullptr; 1488 } 1489 1490 //===----------------------------------------------------------------------===// 1491 // AttributeList Introspection Methods 1492 //===----------------------------------------------------------------------===// 1493 1494 unsigned AttributeList::getNumAttrSets() const { 1495 return pImpl ? pImpl->NumAttrSets : 0; 1496 } 1497 1498 void AttributeList::print(raw_ostream &O) const { 1499 O << "AttributeList[\n"; 1500 1501 for (unsigned i : indexes()) { 1502 if (!getAttributes(i).hasAttributes()) 1503 continue; 1504 O << " { "; 1505 switch (i) { 1506 case AttrIndex::ReturnIndex: 1507 O << "return"; 1508 break; 1509 case AttrIndex::FunctionIndex: 1510 O << "function"; 1511 break; 1512 default: 1513 O << "arg(" << i - AttrIndex::FirstArgIndex << ")"; 1514 } 1515 O << " => " << getAsString(i) << " }\n"; 1516 } 1517 1518 O << "]\n"; 1519 } 1520 1521 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1522 LLVM_DUMP_METHOD void AttributeList::dump() const { print(dbgs()); } 1523 #endif 1524 1525 //===----------------------------------------------------------------------===// 1526 // AttrBuilder Method Implementations 1527 //===----------------------------------------------------------------------===// 1528 1529 // FIXME: Remove this ctor, use AttributeSet. 1530 AttrBuilder::AttrBuilder(AttributeList AL, unsigned Index) { 1531 AttributeSet AS = AL.getAttributes(Index); 1532 for (const auto &A : AS) 1533 addAttribute(A); 1534 } 1535 1536 AttrBuilder::AttrBuilder(AttributeSet AS) { 1537 for (const auto &A : AS) 1538 addAttribute(A); 1539 } 1540 1541 void AttrBuilder::clear() { 1542 Attrs.reset(); 1543 TargetDepAttrs.clear(); 1544 IntAttrs = {}; 1545 TypeAttrs = {}; 1546 } 1547 1548 Optional<unsigned> 1549 AttrBuilder::kindToIntIndex(Attribute::AttrKind Kind) const { 1550 if (Attribute::isIntAttrKind(Kind)) 1551 return Kind - Attribute::FirstIntAttr; 1552 return None; 1553 } 1554 1555 Optional<unsigned> 1556 AttrBuilder::kindToTypeIndex(Attribute::AttrKind Kind) const { 1557 if (Attribute::isTypeAttrKind(Kind)) 1558 return Kind - Attribute::FirstTypeAttr; 1559 return None; 1560 } 1561 1562 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) { 1563 if (Attr.isStringAttribute()) { 1564 addAttribute(Attr.getKindAsString(), Attr.getValueAsString()); 1565 return *this; 1566 } 1567 1568 Attribute::AttrKind Kind = Attr.getKindAsEnum(); 1569 Attrs[Kind] = true; 1570 1571 if (Optional<unsigned> TypeIndex = kindToTypeIndex(Kind)) 1572 TypeAttrs[*TypeIndex] = Attr.getValueAsType(); 1573 else if (Optional<unsigned> IntIndex = kindToIntIndex(Kind)) 1574 IntAttrs[*IntIndex] = Attr.getValueAsInt(); 1575 1576 return *this; 1577 } 1578 1579 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) { 1580 TargetDepAttrs[A] = V; 1581 return *this; 1582 } 1583 1584 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) { 1585 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!"); 1586 Attrs[Val] = false; 1587 1588 if (Optional<unsigned> TypeIndex = kindToTypeIndex(Val)) 1589 TypeAttrs[*TypeIndex] = nullptr; 1590 else if (Optional<unsigned> IntIndex = kindToIntIndex(Val)) 1591 IntAttrs[*IntIndex] = 0; 1592 1593 return *this; 1594 } 1595 1596 AttrBuilder &AttrBuilder::removeAttributes(AttributeList A, uint64_t Index) { 1597 remove(A.getAttributes(Index)); 1598 return *this; 1599 } 1600 1601 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) { 1602 TargetDepAttrs.erase(A); 1603 return *this; 1604 } 1605 1606 uint64_t AttrBuilder::getRawIntAttr(Attribute::AttrKind Kind) const { 1607 Optional<unsigned> IntIndex = kindToIntIndex(Kind); 1608 assert(IntIndex && "Not an int attribute"); 1609 return IntAttrs[*IntIndex]; 1610 } 1611 1612 AttrBuilder &AttrBuilder::addRawIntAttr(Attribute::AttrKind Kind, 1613 uint64_t Value) { 1614 Optional<unsigned> IntIndex = kindToIntIndex(Kind); 1615 assert(IntIndex && "Not an int attribute"); 1616 assert(Value && "Value cannot be zero"); 1617 Attrs[Kind] = true; 1618 IntAttrs[*IntIndex] = Value; 1619 return *this; 1620 } 1621 1622 std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const { 1623 return unpackAllocSizeArgs(getRawIntAttr(Attribute::AllocSize)); 1624 } 1625 1626 std::pair<unsigned, unsigned> AttrBuilder::getVScaleRangeArgs() const { 1627 return unpackVScaleRangeArgs(getRawIntAttr(Attribute::VScaleRange)); 1628 } 1629 1630 AttrBuilder &AttrBuilder::addAlignmentAttr(MaybeAlign Align) { 1631 if (!Align) 1632 return *this; 1633 1634 assert(*Align <= llvm::Value::MaximumAlignment && "Alignment too large."); 1635 return addRawIntAttr(Attribute::Alignment, Align->value()); 1636 } 1637 1638 AttrBuilder &AttrBuilder::addStackAlignmentAttr(MaybeAlign Align) { 1639 // Default alignment, allow the target to define how to align it. 1640 if (!Align) 1641 return *this; 1642 1643 assert(*Align <= 0x100 && "Alignment too large."); 1644 return addRawIntAttr(Attribute::StackAlignment, Align->value()); 1645 } 1646 1647 AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) { 1648 if (Bytes == 0) return *this; 1649 1650 return addRawIntAttr(Attribute::Dereferenceable, Bytes); 1651 } 1652 1653 AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) { 1654 if (Bytes == 0) 1655 return *this; 1656 1657 return addRawIntAttr(Attribute::DereferenceableOrNull, Bytes); 1658 } 1659 1660 AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize, 1661 const Optional<unsigned> &NumElems) { 1662 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems)); 1663 } 1664 1665 AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) { 1666 // (0, 0) is our "not present" value, so we need to check for it here. 1667 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)"); 1668 return addRawIntAttr(Attribute::AllocSize, RawArgs); 1669 } 1670 1671 AttrBuilder &AttrBuilder::addVScaleRangeAttr(unsigned MinValue, 1672 unsigned MaxValue) { 1673 return addVScaleRangeAttrFromRawRepr(packVScaleRangeArgs(MinValue, MaxValue)); 1674 } 1675 1676 AttrBuilder &AttrBuilder::addVScaleRangeAttrFromRawRepr(uint64_t RawArgs) { 1677 // (0, 0) is not present hence ignore this case 1678 if (RawArgs == 0) 1679 return *this; 1680 1681 return addRawIntAttr(Attribute::VScaleRange, RawArgs); 1682 } 1683 1684 Type *AttrBuilder::getTypeAttr(Attribute::AttrKind Kind) const { 1685 Optional<unsigned> TypeIndex = kindToTypeIndex(Kind); 1686 assert(TypeIndex && "Not a type attribute"); 1687 return TypeAttrs[*TypeIndex]; 1688 } 1689 1690 AttrBuilder &AttrBuilder::addTypeAttr(Attribute::AttrKind Kind, Type *Ty) { 1691 Optional<unsigned> TypeIndex = kindToTypeIndex(Kind); 1692 assert(TypeIndex && "Not a type attribute"); 1693 Attrs[Kind] = true; 1694 TypeAttrs[*TypeIndex] = Ty; 1695 return *this; 1696 } 1697 1698 AttrBuilder &AttrBuilder::addByValAttr(Type *Ty) { 1699 return addTypeAttr(Attribute::ByVal, Ty); 1700 } 1701 1702 AttrBuilder &AttrBuilder::addStructRetAttr(Type *Ty) { 1703 return addTypeAttr(Attribute::StructRet, Ty); 1704 } 1705 1706 AttrBuilder &AttrBuilder::addByRefAttr(Type *Ty) { 1707 return addTypeAttr(Attribute::ByRef, Ty); 1708 } 1709 1710 AttrBuilder &AttrBuilder::addPreallocatedAttr(Type *Ty) { 1711 return addTypeAttr(Attribute::Preallocated, Ty); 1712 } 1713 1714 AttrBuilder &AttrBuilder::addInAllocaAttr(Type *Ty) { 1715 return addTypeAttr(Attribute::InAlloca, Ty); 1716 } 1717 1718 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) { 1719 // FIXME: What if both have an int/type attribute, but they don't match?! 1720 for (unsigned Index = 0; Index < Attribute::NumIntAttrKinds; ++Index) 1721 if (!IntAttrs[Index]) 1722 IntAttrs[Index] = B.IntAttrs[Index]; 1723 1724 for (unsigned Index = 0; Index < Attribute::NumTypeAttrKinds; ++Index) 1725 if (!TypeAttrs[Index]) 1726 TypeAttrs[Index] = B.TypeAttrs[Index]; 1727 1728 Attrs |= B.Attrs; 1729 1730 for (const auto &I : B.td_attrs()) 1731 TargetDepAttrs[I.first] = I.second; 1732 1733 return *this; 1734 } 1735 1736 AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) { 1737 // FIXME: What if both have an int/type attribute, but they don't match?! 1738 for (unsigned Index = 0; Index < Attribute::NumIntAttrKinds; ++Index) 1739 if (B.IntAttrs[Index]) 1740 IntAttrs[Index] = 0; 1741 1742 for (unsigned Index = 0; Index < Attribute::NumTypeAttrKinds; ++Index) 1743 if (B.TypeAttrs[Index]) 1744 TypeAttrs[Index] = nullptr; 1745 1746 Attrs &= ~B.Attrs; 1747 1748 for (const auto &I : B.td_attrs()) 1749 TargetDepAttrs.erase(I.first); 1750 1751 return *this; 1752 } 1753 1754 bool AttrBuilder::overlaps(const AttrBuilder &B) const { 1755 // First check if any of the target independent attributes overlap. 1756 if ((Attrs & B.Attrs).any()) 1757 return true; 1758 1759 // Then check if any target dependent ones do. 1760 for (const auto &I : td_attrs()) 1761 if (B.contains(I.first)) 1762 return true; 1763 1764 return false; 1765 } 1766 1767 bool AttrBuilder::contains(StringRef A) const { 1768 return TargetDepAttrs.find(A) != TargetDepAttrs.end(); 1769 } 1770 1771 bool AttrBuilder::hasAttributes() const { 1772 return !Attrs.none() || !TargetDepAttrs.empty(); 1773 } 1774 1775 bool AttrBuilder::hasAttributes(AttributeList AL, uint64_t Index) const { 1776 AttributeSet AS = AL.getAttributes(Index); 1777 1778 for (const auto &Attr : AS) { 1779 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) { 1780 if (contains(Attr.getKindAsEnum())) 1781 return true; 1782 } else { 1783 assert(Attr.isStringAttribute() && "Invalid attribute kind!"); 1784 return contains(Attr.getKindAsString()); 1785 } 1786 } 1787 1788 return false; 1789 } 1790 1791 bool AttrBuilder::hasAlignmentAttr() const { 1792 return getRawIntAttr(Attribute::Alignment) != 0; 1793 } 1794 1795 bool AttrBuilder::operator==(const AttrBuilder &B) const { 1796 if (Attrs != B.Attrs) 1797 return false; 1798 1799 for (const auto &TDA : TargetDepAttrs) 1800 if (B.TargetDepAttrs.find(TDA.first) == B.TargetDepAttrs.end()) 1801 return false; 1802 1803 return IntAttrs == B.IntAttrs && TypeAttrs == B.TypeAttrs; 1804 } 1805 1806 //===----------------------------------------------------------------------===// 1807 // AttributeFuncs Function Defintions 1808 //===----------------------------------------------------------------------===// 1809 1810 /// Which attributes cannot be applied to a type. 1811 AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) { 1812 AttrBuilder Incompatible; 1813 1814 if (!Ty->isIntegerTy()) 1815 // Attribute that only apply to integers. 1816 Incompatible.addAttribute(Attribute::SExt) 1817 .addAttribute(Attribute::ZExt); 1818 1819 if (!Ty->isPointerTy()) 1820 // Attribute that only apply to pointers. 1821 Incompatible.addAttribute(Attribute::Nest) 1822 .addAttribute(Attribute::NoAlias) 1823 .addAttribute(Attribute::NoCapture) 1824 .addAttribute(Attribute::NonNull) 1825 .addAttribute(Attribute::ReadNone) 1826 .addAttribute(Attribute::ReadOnly) 1827 .addAttribute(Attribute::SwiftError) 1828 .addAlignmentAttr(1) // the int here is ignored 1829 .addDereferenceableAttr(1) // the int here is ignored 1830 .addDereferenceableOrNullAttr(1) // the int here is ignored 1831 .addPreallocatedAttr(Ty) 1832 .addInAllocaAttr(Ty) 1833 .addByValAttr(Ty) 1834 .addStructRetAttr(Ty) 1835 .addByRefAttr(Ty) 1836 .addTypeAttr(Attribute::ElementType, Ty); 1837 1838 // Some attributes can apply to all "values" but there are no `void` values. 1839 if (Ty->isVoidTy()) 1840 Incompatible.addAttribute(Attribute::NoUndef); 1841 1842 return Incompatible; 1843 } 1844 1845 AttrBuilder AttributeFuncs::getUBImplyingAttributes() { 1846 AttrBuilder B; 1847 B.addAttribute(Attribute::NoUndef); 1848 B.addDereferenceableAttr(1); 1849 B.addDereferenceableOrNullAttr(1); 1850 return B; 1851 } 1852 1853 template<typename AttrClass> 1854 static bool isEqual(const Function &Caller, const Function &Callee) { 1855 return Caller.getFnAttribute(AttrClass::getKind()) == 1856 Callee.getFnAttribute(AttrClass::getKind()); 1857 } 1858 1859 /// Compute the logical AND of the attributes of the caller and the 1860 /// callee. 1861 /// 1862 /// This function sets the caller's attribute to false if the callee's attribute 1863 /// is false. 1864 template<typename AttrClass> 1865 static void setAND(Function &Caller, const Function &Callee) { 1866 if (AttrClass::isSet(Caller, AttrClass::getKind()) && 1867 !AttrClass::isSet(Callee, AttrClass::getKind())) 1868 AttrClass::set(Caller, AttrClass::getKind(), false); 1869 } 1870 1871 /// Compute the logical OR of the attributes of the caller and the 1872 /// callee. 1873 /// 1874 /// This function sets the caller's attribute to true if the callee's attribute 1875 /// is true. 1876 template<typename AttrClass> 1877 static void setOR(Function &Caller, const Function &Callee) { 1878 if (!AttrClass::isSet(Caller, AttrClass::getKind()) && 1879 AttrClass::isSet(Callee, AttrClass::getKind())) 1880 AttrClass::set(Caller, AttrClass::getKind(), true); 1881 } 1882 1883 /// If the inlined function had a higher stack protection level than the 1884 /// calling function, then bump up the caller's stack protection level. 1885 static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) { 1886 // If upgrading the SSP attribute, clear out the old SSP Attributes first. 1887 // Having multiple SSP attributes doesn't actually hurt, but it adds useless 1888 // clutter to the IR. 1889 AttrBuilder OldSSPAttr; 1890 OldSSPAttr.addAttribute(Attribute::StackProtect) 1891 .addAttribute(Attribute::StackProtectStrong) 1892 .addAttribute(Attribute::StackProtectReq); 1893 1894 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) { 1895 Caller.removeFnAttrs(OldSSPAttr); 1896 Caller.addFnAttr(Attribute::StackProtectReq); 1897 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) && 1898 !Caller.hasFnAttribute(Attribute::StackProtectReq)) { 1899 Caller.removeFnAttrs(OldSSPAttr); 1900 Caller.addFnAttr(Attribute::StackProtectStrong); 1901 } else if (Callee.hasFnAttribute(Attribute::StackProtect) && 1902 !Caller.hasFnAttribute(Attribute::StackProtectReq) && 1903 !Caller.hasFnAttribute(Attribute::StackProtectStrong)) 1904 Caller.addFnAttr(Attribute::StackProtect); 1905 } 1906 1907 /// If the inlined function required stack probes, then ensure that 1908 /// the calling function has those too. 1909 static void adjustCallerStackProbes(Function &Caller, const Function &Callee) { 1910 if (!Caller.hasFnAttribute("probe-stack") && 1911 Callee.hasFnAttribute("probe-stack")) { 1912 Caller.addFnAttr(Callee.getFnAttribute("probe-stack")); 1913 } 1914 } 1915 1916 /// If the inlined function defines the size of guard region 1917 /// on the stack, then ensure that the calling function defines a guard region 1918 /// that is no larger. 1919 static void 1920 adjustCallerStackProbeSize(Function &Caller, const Function &Callee) { 1921 Attribute CalleeAttr = Callee.getFnAttribute("stack-probe-size"); 1922 if (CalleeAttr.isValid()) { 1923 Attribute CallerAttr = Caller.getFnAttribute("stack-probe-size"); 1924 if (CallerAttr.isValid()) { 1925 uint64_t CallerStackProbeSize, CalleeStackProbeSize; 1926 CallerAttr.getValueAsString().getAsInteger(0, CallerStackProbeSize); 1927 CalleeAttr.getValueAsString().getAsInteger(0, CalleeStackProbeSize); 1928 1929 if (CallerStackProbeSize > CalleeStackProbeSize) { 1930 Caller.addFnAttr(CalleeAttr); 1931 } 1932 } else { 1933 Caller.addFnAttr(CalleeAttr); 1934 } 1935 } 1936 } 1937 1938 /// If the inlined function defines a min legal vector width, then ensure 1939 /// the calling function has the same or larger min legal vector width. If the 1940 /// caller has the attribute, but the callee doesn't, we need to remove the 1941 /// attribute from the caller since we can't make any guarantees about the 1942 /// caller's requirements. 1943 /// This function is called after the inlining decision has been made so we have 1944 /// to merge the attribute this way. Heuristics that would use 1945 /// min-legal-vector-width to determine inline compatibility would need to be 1946 /// handled as part of inline cost analysis. 1947 static void 1948 adjustMinLegalVectorWidth(Function &Caller, const Function &Callee) { 1949 Attribute CallerAttr = Caller.getFnAttribute("min-legal-vector-width"); 1950 if (CallerAttr.isValid()) { 1951 Attribute CalleeAttr = Callee.getFnAttribute("min-legal-vector-width"); 1952 if (CalleeAttr.isValid()) { 1953 uint64_t CallerVectorWidth, CalleeVectorWidth; 1954 CallerAttr.getValueAsString().getAsInteger(0, CallerVectorWidth); 1955 CalleeAttr.getValueAsString().getAsInteger(0, CalleeVectorWidth); 1956 if (CallerVectorWidth < CalleeVectorWidth) 1957 Caller.addFnAttr(CalleeAttr); 1958 } else { 1959 // If the callee doesn't have the attribute then we don't know anything 1960 // and must drop the attribute from the caller. 1961 Caller.removeFnAttr("min-legal-vector-width"); 1962 } 1963 } 1964 } 1965 1966 /// If the inlined function has null_pointer_is_valid attribute, 1967 /// set this attribute in the caller post inlining. 1968 static void 1969 adjustNullPointerValidAttr(Function &Caller, const Function &Callee) { 1970 if (Callee.nullPointerIsDefined() && !Caller.nullPointerIsDefined()) { 1971 Caller.addFnAttr(Attribute::NullPointerIsValid); 1972 } 1973 } 1974 1975 struct EnumAttr { 1976 static bool isSet(const Function &Fn, 1977 Attribute::AttrKind Kind) { 1978 return Fn.hasFnAttribute(Kind); 1979 } 1980 1981 static void set(Function &Fn, 1982 Attribute::AttrKind Kind, bool Val) { 1983 if (Val) 1984 Fn.addFnAttr(Kind); 1985 else 1986 Fn.removeFnAttr(Kind); 1987 } 1988 }; 1989 1990 struct StrBoolAttr { 1991 static bool isSet(const Function &Fn, 1992 StringRef Kind) { 1993 auto A = Fn.getFnAttribute(Kind); 1994 return A.getValueAsString().equals("true"); 1995 } 1996 1997 static void set(Function &Fn, 1998 StringRef Kind, bool Val) { 1999 Fn.addFnAttr(Kind, Val ? "true" : "false"); 2000 } 2001 }; 2002 2003 #define GET_ATTR_NAMES 2004 #define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) \ 2005 struct ENUM_NAME##Attr : EnumAttr { \ 2006 static enum Attribute::AttrKind getKind() { \ 2007 return llvm::Attribute::ENUM_NAME; \ 2008 } \ 2009 }; 2010 #define ATTRIBUTE_STRBOOL(ENUM_NAME, DISPLAY_NAME) \ 2011 struct ENUM_NAME##Attr : StrBoolAttr { \ 2012 static StringRef getKind() { return #DISPLAY_NAME; } \ 2013 }; 2014 #include "llvm/IR/Attributes.inc" 2015 2016 #define GET_ATTR_COMPAT_FUNC 2017 #include "llvm/IR/Attributes.inc" 2018 2019 bool AttributeFuncs::areInlineCompatible(const Function &Caller, 2020 const Function &Callee) { 2021 return hasCompatibleFnAttrs(Caller, Callee); 2022 } 2023 2024 bool AttributeFuncs::areOutlineCompatible(const Function &A, 2025 const Function &B) { 2026 return hasCompatibleFnAttrs(A, B); 2027 } 2028 2029 void AttributeFuncs::mergeAttributesForInlining(Function &Caller, 2030 const Function &Callee) { 2031 mergeFnAttrs(Caller, Callee); 2032 } 2033 2034 void AttributeFuncs::mergeAttributesForOutlining(Function &Base, 2035 const Function &ToMerge) { 2036 2037 // We merge functions so that they meet the most general case. 2038 // For example, if the NoNansFPMathAttr is set in one function, but not in 2039 // the other, in the merged function we can say that the NoNansFPMathAttr 2040 // is not set. 2041 // However if we have the SpeculativeLoadHardeningAttr set true in one 2042 // function, but not the other, we make sure that the function retains 2043 // that aspect in the merged function. 2044 mergeFnAttrs(Base, ToMerge); 2045 } 2046