1 //===- Attributes.cpp - Implement AttributesList --------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // \file 11 // \brief This file implements the Attribute, AttributeImpl, AttrBuilder, 12 // AttributeListImpl, and AttributeList classes. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "llvm/IR/Attributes.h" 17 #include "AttributeImpl.h" 18 #include "LLVMContextImpl.h" 19 #include "llvm/ADT/ArrayRef.h" 20 #include "llvm/ADT/FoldingSet.h" 21 #include "llvm/ADT/Optional.h" 22 #include "llvm/ADT/STLExtras.h" 23 #include "llvm/ADT/SmallVector.h" 24 #include "llvm/ADT/StringExtras.h" 25 #include "llvm/ADT/StringRef.h" 26 #include "llvm/ADT/Twine.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> 44 45 using namespace llvm; 46 47 //===----------------------------------------------------------------------===// 48 // Attribute Construction Methods 49 //===----------------------------------------------------------------------===// 50 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. 54 // 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; 58 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"); 64 65 return uint64_t(ElemSizeArg) << 32 | 66 NumElemsArg.getValueOr(AllocSizeNumElemsNotPresent); 67 } 68 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; 73 74 Optional<unsigned> NumElemsArg; 75 if (NumElems != AllocSizeNumElemsNotPresent) 76 NumElemsArg = NumElems; 77 return std::make_pair(ElemSizeArg, NumElemsArg); 78 } 79 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); 86 87 void *InsertPoint; 88 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint); 89 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); 98 } 99 100 // Return the Attribute that we found or created. 101 return Attribute(PA); 102 } 103 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); 109 110 void *InsertPoint; 111 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint); 112 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); 118 } 119 120 // Return the Attribute that we found or created. 121 return Attribute(PA); 122 } 123 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); 128 } 129 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); 135 } 136 137 Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context, 138 uint64_t Bytes) { 139 assert(Bytes && "Bytes must be non-zero."); 140 return get(Context, Dereferenceable, Bytes); 141 } 142 143 Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context, 144 uint64_t Bytes) { 145 assert(Bytes && "Bytes must be non-zero."); 146 return get(Context, DereferenceableOrNull, Bytes); 147 } 148 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)); 155 } 156 157 //===----------------------------------------------------------------------===// 158 // Attribute Accessor Methods 159 //===----------------------------------------------------------------------===// 160 161 bool Attribute::isEnumAttribute() const { 162 return pImpl && pImpl->isEnumAttribute(); 163 } 164 165 bool Attribute::isIntAttribute() const { 166 return pImpl && pImpl->isIntAttribute(); 167 } 168 169 bool Attribute::isStringAttribute() const { 170 return pImpl && pImpl->isStringAttribute(); 171 } 172 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(); 178 } 179 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(); 185 } 186 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(); 192 } 193 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(); 199 } 200 201 bool Attribute::hasAttribute(AttrKind Kind) const { 202 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None); 203 } 204 205 bool Attribute::hasAttribute(StringRef Kind) const { 206 if (!isStringAttribute()) return false; 207 return pImpl && pImpl->hasAttribute(Kind); 208 } 209 210 unsigned Attribute::getAlignment() const { 211 assert(hasAttribute(Attribute::Alignment) && 212 "Trying to get alignment from non-alignment attribute!"); 213 return pImpl->getValueAsInt(); 214 } 215 216 unsigned Attribute::getStackAlignment() const { 217 assert(hasAttribute(Attribute::StackAlignment) && 218 "Trying to get alignment from non-alignment attribute!"); 219 return pImpl->getValueAsInt(); 220 } 221 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(); 227 } 228 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(); 234 } 235 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()); 240 } 241 242 std::string Attribute::getAsString(bool InAttrGrp) const { 243 if (!pImpl) return {}; 244 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::Speculatable)) 326 return "speculatable"; 327 if (hasAttribute(Attribute::StackProtect)) 328 return "ssp"; 329 if (hasAttribute(Attribute::StackProtectReq)) 330 return "sspreq"; 331 if (hasAttribute(Attribute::StackProtectStrong)) 332 return "sspstrong"; 333 if (hasAttribute(Attribute::SafeStack)) 334 return "safestack"; 335 if (hasAttribute(Attribute::ShadowCallStack)) 336 return "shadowcallstack"; 337 if (hasAttribute(Attribute::StrictFP)) 338 return "strictfp"; 339 if (hasAttribute(Attribute::StructRet)) 340 return "sret"; 341 if (hasAttribute(Attribute::SanitizeThread)) 342 return "sanitize_thread"; 343 if (hasAttribute(Attribute::SanitizeMemory)) 344 return "sanitize_memory"; 345 if (hasAttribute(Attribute::UWTable)) 346 return "uwtable"; 347 if (hasAttribute(Attribute::ZExt)) 348 return "zeroext"; 349 if (hasAttribute(Attribute::Cold)) 350 return "cold"; 351 352 // FIXME: These should be output like this: 353 // 354 // align=4 355 // alignstack=8 356 // 357 if (hasAttribute(Attribute::Alignment)) { 358 std::string Result; 359 Result += "align"; 360 Result += (InAttrGrp) ? "=" : " "; 361 Result += utostr(getValueAsInt()); 362 return Result; 363 } 364 365 auto AttrWithBytesToString = [&](const char *Name) { 366 std::string Result; 367 Result += Name; 368 if (InAttrGrp) { 369 Result += "="; 370 Result += utostr(getValueAsInt()); 371 } else { 372 Result += "("; 373 Result += utostr(getValueAsInt()); 374 Result += ")"; 375 } 376 return Result; 377 }; 378 379 if (hasAttribute(Attribute::StackAlignment)) 380 return AttrWithBytesToString("alignstack"); 381 382 if (hasAttribute(Attribute::Dereferenceable)) 383 return AttrWithBytesToString("dereferenceable"); 384 385 if (hasAttribute(Attribute::DereferenceableOrNull)) 386 return AttrWithBytesToString("dereferenceable_or_null"); 387 388 if (hasAttribute(Attribute::AllocSize)) { 389 unsigned ElemSize; 390 Optional<unsigned> NumElems; 391 std::tie(ElemSize, NumElems) = getAllocSizeArgs(); 392 393 std::string Result = "allocsize("; 394 Result += utostr(ElemSize); 395 if (NumElems.hasValue()) { 396 Result += ','; 397 Result += utostr(*NumElems); 398 } 399 Result += ')'; 400 return Result; 401 } 402 403 // Convert target-dependent attributes to strings of the form: 404 // 405 // "kind" 406 // "kind" = "value" 407 // 408 if (isStringAttribute()) { 409 std::string Result; 410 Result += (Twine('"') + getKindAsString() + Twine('"')).str(); 411 412 std::string AttrVal = pImpl->getValueAsString(); 413 if (AttrVal.empty()) return Result; 414 415 // Since some attribute strings contain special characters that cannot be 416 // printable, those have to be escaped to make the attribute value printable 417 // as is. e.g. "\01__gnu_mcount_nc" 418 { 419 raw_string_ostream OS(Result); 420 OS << "=\""; 421 PrintEscapedString(AttrVal, OS); 422 OS << "\""; 423 } 424 return Result; 425 } 426 427 llvm_unreachable("Unknown attribute"); 428 } 429 430 bool Attribute::operator<(Attribute A) const { 431 if (!pImpl && !A.pImpl) return false; 432 if (!pImpl) return true; 433 if (!A.pImpl) return false; 434 return *pImpl < *A.pImpl; 435 } 436 437 //===----------------------------------------------------------------------===// 438 // AttributeImpl Definition 439 //===----------------------------------------------------------------------===// 440 441 // Pin the vtables to this file. 442 AttributeImpl::~AttributeImpl() = default; 443 444 void EnumAttributeImpl::anchor() {} 445 446 void IntAttributeImpl::anchor() {} 447 448 void StringAttributeImpl::anchor() {} 449 450 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const { 451 if (isStringAttribute()) return false; 452 return getKindAsEnum() == A; 453 } 454 455 bool AttributeImpl::hasAttribute(StringRef Kind) const { 456 if (!isStringAttribute()) return false; 457 return getKindAsString() == Kind; 458 } 459 460 Attribute::AttrKind AttributeImpl::getKindAsEnum() const { 461 assert(isEnumAttribute() || isIntAttribute()); 462 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind(); 463 } 464 465 uint64_t AttributeImpl::getValueAsInt() const { 466 assert(isIntAttribute()); 467 return static_cast<const IntAttributeImpl *>(this)->getValue(); 468 } 469 470 StringRef AttributeImpl::getKindAsString() const { 471 assert(isStringAttribute()); 472 return static_cast<const StringAttributeImpl *>(this)->getStringKind(); 473 } 474 475 StringRef AttributeImpl::getValueAsString() const { 476 assert(isStringAttribute()); 477 return static_cast<const StringAttributeImpl *>(this)->getStringValue(); 478 } 479 480 bool AttributeImpl::operator<(const AttributeImpl &AI) const { 481 // This sorts the attributes with Attribute::AttrKinds coming first (sorted 482 // relative to their enum value) and then strings. 483 if (isEnumAttribute()) { 484 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum(); 485 if (AI.isIntAttribute()) return true; 486 if (AI.isStringAttribute()) return true; 487 } 488 489 if (isIntAttribute()) { 490 if (AI.isEnumAttribute()) return false; 491 if (AI.isIntAttribute()) { 492 if (getKindAsEnum() == AI.getKindAsEnum()) 493 return getValueAsInt() < AI.getValueAsInt(); 494 return getKindAsEnum() < AI.getKindAsEnum(); 495 } 496 if (AI.isStringAttribute()) return true; 497 } 498 499 if (AI.isEnumAttribute()) return false; 500 if (AI.isIntAttribute()) return false; 501 if (getKindAsString() == AI.getKindAsString()) 502 return getValueAsString() < AI.getValueAsString(); 503 return getKindAsString() < AI.getKindAsString(); 504 } 505 506 //===----------------------------------------------------------------------===// 507 // AttributeSet Definition 508 //===----------------------------------------------------------------------===// 509 510 AttributeSet AttributeSet::get(LLVMContext &C, const AttrBuilder &B) { 511 return AttributeSet(AttributeSetNode::get(C, B)); 512 } 513 514 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<Attribute> Attrs) { 515 return AttributeSet(AttributeSetNode::get(C, Attrs)); 516 } 517 518 AttributeSet AttributeSet::addAttribute(LLVMContext &C, 519 Attribute::AttrKind Kind) const { 520 if (hasAttribute(Kind)) return *this; 521 AttrBuilder B; 522 B.addAttribute(Kind); 523 return addAttributes(C, AttributeSet::get(C, B)); 524 } 525 526 AttributeSet AttributeSet::addAttribute(LLVMContext &C, StringRef Kind, 527 StringRef Value) const { 528 AttrBuilder B; 529 B.addAttribute(Kind, Value); 530 return addAttributes(C, AttributeSet::get(C, B)); 531 } 532 533 AttributeSet AttributeSet::addAttributes(LLVMContext &C, 534 const AttributeSet AS) const { 535 if (!hasAttributes()) 536 return AS; 537 538 if (!AS.hasAttributes()) 539 return *this; 540 541 AttrBuilder B(AS); 542 for (const auto I : *this) 543 B.addAttribute(I); 544 545 return get(C, B); 546 } 547 548 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, 549 Attribute::AttrKind Kind) const { 550 if (!hasAttribute(Kind)) return *this; 551 AttrBuilder B(*this); 552 B.removeAttribute(Kind); 553 return get(C, B); 554 } 555 556 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, 557 StringRef Kind) const { 558 if (!hasAttribute(Kind)) return *this; 559 AttrBuilder B(*this); 560 B.removeAttribute(Kind); 561 return get(C, B); 562 } 563 564 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, 565 const AttrBuilder &Attrs) const { 566 AttrBuilder B(*this); 567 B.remove(Attrs); 568 return get(C, B); 569 } 570 571 unsigned AttributeSet::getNumAttributes() const { 572 return SetNode ? SetNode->getNumAttributes() : 0; 573 } 574 575 bool AttributeSet::hasAttribute(Attribute::AttrKind Kind) const { 576 return SetNode ? SetNode->hasAttribute(Kind) : false; 577 } 578 579 bool AttributeSet::hasAttribute(StringRef Kind) const { 580 return SetNode ? SetNode->hasAttribute(Kind) : false; 581 } 582 583 Attribute AttributeSet::getAttribute(Attribute::AttrKind Kind) const { 584 return SetNode ? SetNode->getAttribute(Kind) : Attribute(); 585 } 586 587 Attribute AttributeSet::getAttribute(StringRef Kind) const { 588 return SetNode ? SetNode->getAttribute(Kind) : Attribute(); 589 } 590 591 unsigned AttributeSet::getAlignment() const { 592 return SetNode ? SetNode->getAlignment() : 0; 593 } 594 595 unsigned AttributeSet::getStackAlignment() const { 596 return SetNode ? SetNode->getStackAlignment() : 0; 597 } 598 599 uint64_t AttributeSet::getDereferenceableBytes() const { 600 return SetNode ? SetNode->getDereferenceableBytes() : 0; 601 } 602 603 uint64_t AttributeSet::getDereferenceableOrNullBytes() const { 604 return SetNode ? SetNode->getDereferenceableOrNullBytes() : 0; 605 } 606 607 std::pair<unsigned, Optional<unsigned>> AttributeSet::getAllocSizeArgs() const { 608 return SetNode ? SetNode->getAllocSizeArgs() 609 : std::pair<unsigned, Optional<unsigned>>(0, 0); 610 } 611 612 std::string AttributeSet::getAsString(bool InAttrGrp) const { 613 return SetNode ? SetNode->getAsString(InAttrGrp) : ""; 614 } 615 616 AttributeSet::iterator AttributeSet::begin() const { 617 return SetNode ? SetNode->begin() : nullptr; 618 } 619 620 AttributeSet::iterator AttributeSet::end() const { 621 return SetNode ? SetNode->end() : nullptr; 622 } 623 624 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 625 LLVM_DUMP_METHOD void AttributeSet::dump() const { 626 dbgs() << "AS =\n"; 627 dbgs() << " { "; 628 dbgs() << getAsString(true) << " }\n"; 629 } 630 #endif 631 632 //===----------------------------------------------------------------------===// 633 // AttributeSetNode Definition 634 //===----------------------------------------------------------------------===// 635 636 AttributeSetNode::AttributeSetNode(ArrayRef<Attribute> Attrs) 637 : AvailableAttrs(0), NumAttrs(Attrs.size()) { 638 // There's memory after the node where we can store the entries in. 639 std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<Attribute>()); 640 641 for (const auto I : *this) { 642 if (!I.isStringAttribute()) { 643 AvailableAttrs |= ((uint64_t)1) << I.getKindAsEnum(); 644 } 645 } 646 } 647 648 AttributeSetNode *AttributeSetNode::get(LLVMContext &C, 649 ArrayRef<Attribute> Attrs) { 650 if (Attrs.empty()) 651 return nullptr; 652 653 // Otherwise, build a key to look up the existing attributes. 654 LLVMContextImpl *pImpl = C.pImpl; 655 FoldingSetNodeID ID; 656 657 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end()); 658 llvm::sort(SortedAttrs.begin(), SortedAttrs.end()); 659 660 for (const auto Attr : SortedAttrs) 661 Attr.Profile(ID); 662 663 void *InsertPoint; 664 AttributeSetNode *PA = 665 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint); 666 667 // If we didn't find any existing attributes of the same shape then create a 668 // new one and insert it. 669 if (!PA) { 670 // Coallocate entries after the AttributeSetNode itself. 671 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size())); 672 PA = new (Mem) AttributeSetNode(SortedAttrs); 673 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint); 674 } 675 676 // Return the AttributeSetNode that we found or created. 677 return PA; 678 } 679 680 AttributeSetNode *AttributeSetNode::get(LLVMContext &C, const AttrBuilder &B) { 681 // Add target-independent attributes. 682 SmallVector<Attribute, 8> Attrs; 683 for (Attribute::AttrKind Kind = Attribute::None; 684 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) { 685 if (!B.contains(Kind)) 686 continue; 687 688 Attribute Attr; 689 switch (Kind) { 690 case Attribute::Alignment: 691 Attr = Attribute::getWithAlignment(C, B.getAlignment()); 692 break; 693 case Attribute::StackAlignment: 694 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment()); 695 break; 696 case Attribute::Dereferenceable: 697 Attr = Attribute::getWithDereferenceableBytes( 698 C, B.getDereferenceableBytes()); 699 break; 700 case Attribute::DereferenceableOrNull: 701 Attr = Attribute::getWithDereferenceableOrNullBytes( 702 C, B.getDereferenceableOrNullBytes()); 703 break; 704 case Attribute::AllocSize: { 705 auto A = B.getAllocSizeArgs(); 706 Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second); 707 break; 708 } 709 default: 710 Attr = Attribute::get(C, Kind); 711 } 712 Attrs.push_back(Attr); 713 } 714 715 // Add target-dependent (string) attributes. 716 for (const auto &TDA : B.td_attrs()) 717 Attrs.emplace_back(Attribute::get(C, TDA.first, TDA.second)); 718 719 return get(C, Attrs); 720 } 721 722 bool AttributeSetNode::hasAttribute(StringRef Kind) const { 723 for (const auto I : *this) 724 if (I.hasAttribute(Kind)) 725 return true; 726 return false; 727 } 728 729 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const { 730 if (hasAttribute(Kind)) { 731 for (const auto I : *this) 732 if (I.hasAttribute(Kind)) 733 return I; 734 } 735 return {}; 736 } 737 738 Attribute AttributeSetNode::getAttribute(StringRef Kind) const { 739 for (const auto I : *this) 740 if (I.hasAttribute(Kind)) 741 return I; 742 return {}; 743 } 744 745 unsigned AttributeSetNode::getAlignment() const { 746 for (const auto I : *this) 747 if (I.hasAttribute(Attribute::Alignment)) 748 return I.getAlignment(); 749 return 0; 750 } 751 752 unsigned AttributeSetNode::getStackAlignment() const { 753 for (const auto I : *this) 754 if (I.hasAttribute(Attribute::StackAlignment)) 755 return I.getStackAlignment(); 756 return 0; 757 } 758 759 uint64_t AttributeSetNode::getDereferenceableBytes() const { 760 for (const auto I : *this) 761 if (I.hasAttribute(Attribute::Dereferenceable)) 762 return I.getDereferenceableBytes(); 763 return 0; 764 } 765 766 uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const { 767 for (const auto I : *this) 768 if (I.hasAttribute(Attribute::DereferenceableOrNull)) 769 return I.getDereferenceableOrNullBytes(); 770 return 0; 771 } 772 773 std::pair<unsigned, Optional<unsigned>> 774 AttributeSetNode::getAllocSizeArgs() const { 775 for (const auto I : *this) 776 if (I.hasAttribute(Attribute::AllocSize)) 777 return I.getAllocSizeArgs(); 778 return std::make_pair(0, 0); 779 } 780 781 std::string AttributeSetNode::getAsString(bool InAttrGrp) const { 782 std::string Str; 783 for (iterator I = begin(), E = end(); I != E; ++I) { 784 if (I != begin()) 785 Str += ' '; 786 Str += I->getAsString(InAttrGrp); 787 } 788 return Str; 789 } 790 791 //===----------------------------------------------------------------------===// 792 // AttributeListImpl Definition 793 //===----------------------------------------------------------------------===// 794 795 /// Map from AttributeList index to the internal array index. Adding one happens 796 /// to work, but it relies on unsigned integer wrapping. MSVC warns about 797 /// unsigned wrapping in constexpr functions, so write out the conditional. LLVM 798 /// folds it to add anyway. 799 static constexpr unsigned attrIdxToArrayIdx(unsigned Index) { 800 return Index == AttributeList::FunctionIndex ? 0 : Index + 1; 801 } 802 803 AttributeListImpl::AttributeListImpl(LLVMContext &C, 804 ArrayRef<AttributeSet> Sets) 805 : AvailableFunctionAttrs(0), Context(C), NumAttrSets(Sets.size()) { 806 assert(!Sets.empty() && "pointless AttributeListImpl"); 807 808 // There's memory after the node where we can store the entries in. 809 std::copy(Sets.begin(), Sets.end(), getTrailingObjects<AttributeSet>()); 810 811 // Initialize AvailableFunctionAttrs summary bitset. 812 static_assert(Attribute::EndAttrKinds <= 813 sizeof(AvailableFunctionAttrs) * CHAR_BIT, 814 "Too many attributes"); 815 static_assert(attrIdxToArrayIdx(AttributeList::FunctionIndex) == 0U, 816 "function should be stored in slot 0"); 817 for (const auto I : Sets[0]) { 818 if (!I.isStringAttribute()) 819 AvailableFunctionAttrs |= 1ULL << I.getKindAsEnum(); 820 } 821 } 822 823 void AttributeListImpl::Profile(FoldingSetNodeID &ID) const { 824 Profile(ID, makeArrayRef(begin(), end())); 825 } 826 827 void AttributeListImpl::Profile(FoldingSetNodeID &ID, 828 ArrayRef<AttributeSet> Sets) { 829 for (const auto &Set : Sets) 830 ID.AddPointer(Set.SetNode); 831 } 832 833 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 834 LLVM_DUMP_METHOD void AttributeListImpl::dump() const { 835 AttributeList(const_cast<AttributeListImpl *>(this)).dump(); 836 } 837 #endif 838 839 //===----------------------------------------------------------------------===// 840 // AttributeList Construction and Mutation Methods 841 //===----------------------------------------------------------------------===// 842 843 AttributeList AttributeList::getImpl(LLVMContext &C, 844 ArrayRef<AttributeSet> AttrSets) { 845 assert(!AttrSets.empty() && "pointless AttributeListImpl"); 846 847 LLVMContextImpl *pImpl = C.pImpl; 848 FoldingSetNodeID ID; 849 AttributeListImpl::Profile(ID, AttrSets); 850 851 void *InsertPoint; 852 AttributeListImpl *PA = 853 pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint); 854 855 // If we didn't find any existing attributes of the same shape then 856 // create a new one and insert it. 857 if (!PA) { 858 // Coallocate entries after the AttributeListImpl itself. 859 void *Mem = ::operator new( 860 AttributeListImpl::totalSizeToAlloc<AttributeSet>(AttrSets.size())); 861 PA = new (Mem) AttributeListImpl(C, AttrSets); 862 pImpl->AttrsLists.InsertNode(PA, InsertPoint); 863 } 864 865 // Return the AttributesList that we found or created. 866 return AttributeList(PA); 867 } 868 869 AttributeList 870 AttributeList::get(LLVMContext &C, 871 ArrayRef<std::pair<unsigned, Attribute>> Attrs) { 872 // If there are no attributes then return a null AttributesList pointer. 873 if (Attrs.empty()) 874 return {}; 875 876 assert(std::is_sorted(Attrs.begin(), Attrs.end(), 877 [](const std::pair<unsigned, Attribute> &LHS, 878 const std::pair<unsigned, Attribute> &RHS) { 879 return LHS.first < RHS.first; 880 }) && "Misordered Attributes list!"); 881 assert(llvm::none_of(Attrs, 882 [](const std::pair<unsigned, Attribute> &Pair) { 883 return Pair.second.hasAttribute(Attribute::None); 884 }) && 885 "Pointless attribute!"); 886 887 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes 888 // list. 889 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairVec; 890 for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(), 891 E = Attrs.end(); I != E; ) { 892 unsigned Index = I->first; 893 SmallVector<Attribute, 4> AttrVec; 894 while (I != E && I->first == Index) { 895 AttrVec.push_back(I->second); 896 ++I; 897 } 898 899 AttrPairVec.emplace_back(Index, AttributeSet::get(C, AttrVec)); 900 } 901 902 return get(C, AttrPairVec); 903 } 904 905 AttributeList 906 AttributeList::get(LLVMContext &C, 907 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) { 908 // If there are no attributes then return a null AttributesList pointer. 909 if (Attrs.empty()) 910 return {}; 911 912 assert(std::is_sorted(Attrs.begin(), Attrs.end(), 913 [](const std::pair<unsigned, AttributeSet> &LHS, 914 const std::pair<unsigned, AttributeSet> &RHS) { 915 return LHS.first < RHS.first; 916 }) && 917 "Misordered Attributes list!"); 918 assert(llvm::none_of(Attrs, 919 [](const std::pair<unsigned, AttributeSet> &Pair) { 920 return !Pair.second.hasAttributes(); 921 }) && 922 "Pointless attribute!"); 923 924 unsigned MaxIndex = Attrs.back().first; 925 // If the MaxIndex is FunctionIndex and there are other indices in front 926 // of it, we need to use the largest of those to get the right size. 927 if (MaxIndex == FunctionIndex && Attrs.size() > 1) 928 MaxIndex = Attrs[Attrs.size() - 2].first; 929 930 SmallVector<AttributeSet, 4> AttrVec(attrIdxToArrayIdx(MaxIndex) + 1); 931 for (const auto Pair : Attrs) 932 AttrVec[attrIdxToArrayIdx(Pair.first)] = Pair.second; 933 934 return getImpl(C, AttrVec); 935 } 936 937 AttributeList AttributeList::get(LLVMContext &C, AttributeSet FnAttrs, 938 AttributeSet RetAttrs, 939 ArrayRef<AttributeSet> ArgAttrs) { 940 // Scan from the end to find the last argument with attributes. Most 941 // arguments don't have attributes, so it's nice if we can have fewer unique 942 // AttributeListImpls by dropping empty attribute sets at the end of the list. 943 unsigned NumSets = 0; 944 for (size_t I = ArgAttrs.size(); I != 0; --I) { 945 if (ArgAttrs[I - 1].hasAttributes()) { 946 NumSets = I + 2; 947 break; 948 } 949 } 950 if (NumSets == 0) { 951 // Check function and return attributes if we didn't have argument 952 // attributes. 953 if (RetAttrs.hasAttributes()) 954 NumSets = 2; 955 else if (FnAttrs.hasAttributes()) 956 NumSets = 1; 957 } 958 959 // If all attribute sets were empty, we can use the empty attribute list. 960 if (NumSets == 0) 961 return {}; 962 963 SmallVector<AttributeSet, 8> AttrSets; 964 AttrSets.reserve(NumSets); 965 // If we have any attributes, we always have function attributes. 966 AttrSets.push_back(FnAttrs); 967 if (NumSets > 1) 968 AttrSets.push_back(RetAttrs); 969 if (NumSets > 2) { 970 // Drop the empty argument attribute sets at the end. 971 ArgAttrs = ArgAttrs.take_front(NumSets - 2); 972 AttrSets.insert(AttrSets.end(), ArgAttrs.begin(), ArgAttrs.end()); 973 } 974 975 return getImpl(C, AttrSets); 976 } 977 978 AttributeList AttributeList::get(LLVMContext &C, unsigned Index, 979 const AttrBuilder &B) { 980 if (!B.hasAttributes()) 981 return {}; 982 Index = attrIdxToArrayIdx(Index); 983 SmallVector<AttributeSet, 8> AttrSets(Index + 1); 984 AttrSets[Index] = AttributeSet::get(C, B); 985 return getImpl(C, AttrSets); 986 } 987 988 AttributeList AttributeList::get(LLVMContext &C, unsigned Index, 989 ArrayRef<Attribute::AttrKind> Kinds) { 990 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs; 991 for (const auto K : Kinds) 992 Attrs.emplace_back(Index, Attribute::get(C, K)); 993 return get(C, Attrs); 994 } 995 996 AttributeList AttributeList::get(LLVMContext &C, unsigned Index, 997 ArrayRef<StringRef> Kinds) { 998 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs; 999 for (const auto K : Kinds) 1000 Attrs.emplace_back(Index, Attribute::get(C, K)); 1001 return get(C, Attrs); 1002 } 1003 1004 AttributeList AttributeList::get(LLVMContext &C, 1005 ArrayRef<AttributeList> Attrs) { 1006 if (Attrs.empty()) 1007 return {}; 1008 if (Attrs.size() == 1) 1009 return Attrs[0]; 1010 1011 unsigned MaxSize = 0; 1012 for (const auto List : Attrs) 1013 MaxSize = std::max(MaxSize, List.getNumAttrSets()); 1014 1015 // If every list was empty, there is no point in merging the lists. 1016 if (MaxSize == 0) 1017 return {}; 1018 1019 SmallVector<AttributeSet, 8> NewAttrSets(MaxSize); 1020 for (unsigned I = 0; I < MaxSize; ++I) { 1021 AttrBuilder CurBuilder; 1022 for (const auto List : Attrs) 1023 CurBuilder.merge(List.getAttributes(I - 1)); 1024 NewAttrSets[I] = AttributeSet::get(C, CurBuilder); 1025 } 1026 1027 return getImpl(C, NewAttrSets); 1028 } 1029 1030 AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index, 1031 Attribute::AttrKind Kind) const { 1032 if (hasAttribute(Index, Kind)) return *this; 1033 AttrBuilder B; 1034 B.addAttribute(Kind); 1035 return addAttributes(C, Index, B); 1036 } 1037 1038 AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index, 1039 StringRef Kind, 1040 StringRef Value) const { 1041 AttrBuilder B; 1042 B.addAttribute(Kind, Value); 1043 return addAttributes(C, Index, B); 1044 } 1045 1046 AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index, 1047 Attribute A) const { 1048 AttrBuilder B; 1049 B.addAttribute(A); 1050 return addAttributes(C, Index, B); 1051 } 1052 1053 AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index, 1054 const AttrBuilder &B) const { 1055 if (!B.hasAttributes()) 1056 return *this; 1057 1058 if (!pImpl) 1059 return AttributeList::get(C, {{Index, AttributeSet::get(C, B)}}); 1060 1061 #ifndef NDEBUG 1062 // FIXME it is not obvious how this should work for alignment. For now, say 1063 // we can't change a known alignment. 1064 unsigned OldAlign = getAttributes(Index).getAlignment(); 1065 unsigned NewAlign = B.getAlignment(); 1066 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) && 1067 "Attempt to change alignment!"); 1068 #endif 1069 1070 Index = attrIdxToArrayIdx(Index); 1071 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end()); 1072 if (Index >= AttrSets.size()) 1073 AttrSets.resize(Index + 1); 1074 1075 AttrBuilder Merged(AttrSets[Index]); 1076 Merged.merge(B); 1077 AttrSets[Index] = AttributeSet::get(C, Merged); 1078 1079 return getImpl(C, AttrSets); 1080 } 1081 1082 AttributeList AttributeList::addParamAttribute(LLVMContext &C, 1083 ArrayRef<unsigned> ArgNos, 1084 Attribute A) const { 1085 assert(std::is_sorted(ArgNos.begin(), ArgNos.end())); 1086 1087 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end()); 1088 unsigned MaxIndex = attrIdxToArrayIdx(ArgNos.back() + FirstArgIndex); 1089 if (MaxIndex >= AttrSets.size()) 1090 AttrSets.resize(MaxIndex + 1); 1091 1092 for (unsigned ArgNo : ArgNos) { 1093 unsigned Index = attrIdxToArrayIdx(ArgNo + FirstArgIndex); 1094 AttrBuilder B(AttrSets[Index]); 1095 B.addAttribute(A); 1096 AttrSets[Index] = AttributeSet::get(C, B); 1097 } 1098 1099 return getImpl(C, AttrSets); 1100 } 1101 1102 AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index, 1103 Attribute::AttrKind Kind) const { 1104 if (!hasAttribute(Index, Kind)) return *this; 1105 1106 Index = attrIdxToArrayIdx(Index); 1107 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end()); 1108 assert(Index < AttrSets.size()); 1109 1110 AttrSets[Index] = AttrSets[Index].removeAttribute(C, Kind); 1111 1112 return getImpl(C, AttrSets); 1113 } 1114 1115 AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index, 1116 StringRef Kind) const { 1117 if (!hasAttribute(Index, Kind)) return *this; 1118 1119 Index = attrIdxToArrayIdx(Index); 1120 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end()); 1121 assert(Index < AttrSets.size()); 1122 1123 AttrSets[Index] = AttrSets[Index].removeAttribute(C, Kind); 1124 1125 return getImpl(C, AttrSets); 1126 } 1127 1128 AttributeList 1129 AttributeList::removeAttributes(LLVMContext &C, unsigned Index, 1130 const AttrBuilder &AttrsToRemove) const { 1131 if (!pImpl) 1132 return {}; 1133 1134 Index = attrIdxToArrayIdx(Index); 1135 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end()); 1136 if (Index >= AttrSets.size()) 1137 AttrSets.resize(Index + 1); 1138 1139 AttrSets[Index] = AttrSets[Index].removeAttributes(C, AttrsToRemove); 1140 1141 return getImpl(C, AttrSets); 1142 } 1143 1144 AttributeList AttributeList::removeAttributes(LLVMContext &C, 1145 unsigned WithoutIndex) const { 1146 if (!pImpl) 1147 return {}; 1148 WithoutIndex = attrIdxToArrayIdx(WithoutIndex); 1149 if (WithoutIndex >= getNumAttrSets()) 1150 return *this; 1151 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end()); 1152 AttrSets[WithoutIndex] = AttributeSet(); 1153 return getImpl(C, AttrSets); 1154 } 1155 1156 AttributeList AttributeList::addDereferenceableAttr(LLVMContext &C, 1157 unsigned Index, 1158 uint64_t Bytes) const { 1159 AttrBuilder B; 1160 B.addDereferenceableAttr(Bytes); 1161 return addAttributes(C, Index, B); 1162 } 1163 1164 AttributeList 1165 AttributeList::addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index, 1166 uint64_t Bytes) const { 1167 AttrBuilder B; 1168 B.addDereferenceableOrNullAttr(Bytes); 1169 return addAttributes(C, Index, B); 1170 } 1171 1172 AttributeList 1173 AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index, 1174 unsigned ElemSizeArg, 1175 const Optional<unsigned> &NumElemsArg) { 1176 AttrBuilder B; 1177 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg); 1178 return addAttributes(C, Index, B); 1179 } 1180 1181 //===----------------------------------------------------------------------===// 1182 // AttributeList Accessor Methods 1183 //===----------------------------------------------------------------------===// 1184 1185 LLVMContext &AttributeList::getContext() const { return pImpl->getContext(); } 1186 1187 AttributeSet AttributeList::getParamAttributes(unsigned ArgNo) const { 1188 return getAttributes(ArgNo + FirstArgIndex); 1189 } 1190 1191 AttributeSet AttributeList::getRetAttributes() const { 1192 return getAttributes(ReturnIndex); 1193 } 1194 1195 AttributeSet AttributeList::getFnAttributes() const { 1196 return getAttributes(FunctionIndex); 1197 } 1198 1199 bool AttributeList::hasAttribute(unsigned Index, 1200 Attribute::AttrKind Kind) const { 1201 return getAttributes(Index).hasAttribute(Kind); 1202 } 1203 1204 bool AttributeList::hasAttribute(unsigned Index, StringRef Kind) const { 1205 return getAttributes(Index).hasAttribute(Kind); 1206 } 1207 1208 bool AttributeList::hasAttributes(unsigned Index) const { 1209 return getAttributes(Index).hasAttributes(); 1210 } 1211 1212 bool AttributeList::hasFnAttribute(Attribute::AttrKind Kind) const { 1213 return pImpl && pImpl->hasFnAttribute(Kind); 1214 } 1215 1216 bool AttributeList::hasFnAttribute(StringRef Kind) const { 1217 return hasAttribute(AttributeList::FunctionIndex, Kind); 1218 } 1219 1220 bool AttributeList::hasParamAttribute(unsigned ArgNo, 1221 Attribute::AttrKind Kind) const { 1222 return hasAttribute(ArgNo + FirstArgIndex, Kind); 1223 } 1224 1225 bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr, 1226 unsigned *Index) const { 1227 if (!pImpl) return false; 1228 1229 for (unsigned I = index_begin(), E = index_end(); I != E; ++I) { 1230 if (hasAttribute(I, Attr)) { 1231 if (Index) 1232 *Index = I; 1233 return true; 1234 } 1235 } 1236 1237 return false; 1238 } 1239 1240 Attribute AttributeList::getAttribute(unsigned Index, 1241 Attribute::AttrKind Kind) const { 1242 return getAttributes(Index).getAttribute(Kind); 1243 } 1244 1245 Attribute AttributeList::getAttribute(unsigned Index, StringRef Kind) const { 1246 return getAttributes(Index).getAttribute(Kind); 1247 } 1248 1249 unsigned AttributeList::getRetAlignment() const { 1250 return getAttributes(ReturnIndex).getAlignment(); 1251 } 1252 1253 unsigned AttributeList::getParamAlignment(unsigned ArgNo) const { 1254 return getAttributes(ArgNo + FirstArgIndex).getAlignment(); 1255 } 1256 1257 unsigned AttributeList::getStackAlignment(unsigned Index) const { 1258 return getAttributes(Index).getStackAlignment(); 1259 } 1260 1261 uint64_t AttributeList::getDereferenceableBytes(unsigned Index) const { 1262 return getAttributes(Index).getDereferenceableBytes(); 1263 } 1264 1265 uint64_t AttributeList::getDereferenceableOrNullBytes(unsigned Index) const { 1266 return getAttributes(Index).getDereferenceableOrNullBytes(); 1267 } 1268 1269 std::pair<unsigned, Optional<unsigned>> 1270 AttributeList::getAllocSizeArgs(unsigned Index) const { 1271 return getAttributes(Index).getAllocSizeArgs(); 1272 } 1273 1274 std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const { 1275 return getAttributes(Index).getAsString(InAttrGrp); 1276 } 1277 1278 AttributeSet AttributeList::getAttributes(unsigned Index) const { 1279 Index = attrIdxToArrayIdx(Index); 1280 if (!pImpl || Index >= getNumAttrSets()) 1281 return {}; 1282 return pImpl->begin()[Index]; 1283 } 1284 1285 AttributeList::iterator AttributeList::begin() const { 1286 return pImpl ? pImpl->begin() : nullptr; 1287 } 1288 1289 AttributeList::iterator AttributeList::end() const { 1290 return pImpl ? pImpl->end() : nullptr; 1291 } 1292 1293 //===----------------------------------------------------------------------===// 1294 // AttributeList Introspection Methods 1295 //===----------------------------------------------------------------------===// 1296 1297 unsigned AttributeList::getNumAttrSets() const { 1298 return pImpl ? pImpl->NumAttrSets : 0; 1299 } 1300 1301 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1302 LLVM_DUMP_METHOD void AttributeList::dump() const { 1303 dbgs() << "PAL[\n"; 1304 1305 for (unsigned i = index_begin(), e = index_end(); i != e; ++i) { 1306 if (getAttributes(i).hasAttributes()) 1307 dbgs() << " { " << i << " => " << getAsString(i) << " }\n"; 1308 } 1309 1310 dbgs() << "]\n"; 1311 } 1312 #endif 1313 1314 //===----------------------------------------------------------------------===// 1315 // AttrBuilder Method Implementations 1316 //===----------------------------------------------------------------------===// 1317 1318 // FIXME: Remove this ctor, use AttributeSet. 1319 AttrBuilder::AttrBuilder(AttributeList AL, unsigned Index) { 1320 AttributeSet AS = AL.getAttributes(Index); 1321 for (const auto &A : AS) 1322 addAttribute(A); 1323 } 1324 1325 AttrBuilder::AttrBuilder(AttributeSet AS) { 1326 for (const auto &A : AS) 1327 addAttribute(A); 1328 } 1329 1330 void AttrBuilder::clear() { 1331 Attrs.reset(); 1332 TargetDepAttrs.clear(); 1333 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0; 1334 AllocSizeArgs = 0; 1335 } 1336 1337 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) { 1338 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!"); 1339 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment && 1340 Val != Attribute::Dereferenceable && Val != Attribute::AllocSize && 1341 "Adding integer attribute without adding a value!"); 1342 Attrs[Val] = true; 1343 return *this; 1344 } 1345 1346 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) { 1347 if (Attr.isStringAttribute()) { 1348 addAttribute(Attr.getKindAsString(), Attr.getValueAsString()); 1349 return *this; 1350 } 1351 1352 Attribute::AttrKind Kind = Attr.getKindAsEnum(); 1353 Attrs[Kind] = true; 1354 1355 if (Kind == Attribute::Alignment) 1356 Alignment = Attr.getAlignment(); 1357 else if (Kind == Attribute::StackAlignment) 1358 StackAlignment = Attr.getStackAlignment(); 1359 else if (Kind == Attribute::Dereferenceable) 1360 DerefBytes = Attr.getDereferenceableBytes(); 1361 else if (Kind == Attribute::DereferenceableOrNull) 1362 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes(); 1363 else if (Kind == Attribute::AllocSize) 1364 AllocSizeArgs = Attr.getValueAsInt(); 1365 return *this; 1366 } 1367 1368 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) { 1369 TargetDepAttrs[A] = V; 1370 return *this; 1371 } 1372 1373 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) { 1374 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!"); 1375 Attrs[Val] = false; 1376 1377 if (Val == Attribute::Alignment) 1378 Alignment = 0; 1379 else if (Val == Attribute::StackAlignment) 1380 StackAlignment = 0; 1381 else if (Val == Attribute::Dereferenceable) 1382 DerefBytes = 0; 1383 else if (Val == Attribute::DereferenceableOrNull) 1384 DerefOrNullBytes = 0; 1385 else if (Val == Attribute::AllocSize) 1386 AllocSizeArgs = 0; 1387 1388 return *this; 1389 } 1390 1391 AttrBuilder &AttrBuilder::removeAttributes(AttributeList A, uint64_t Index) { 1392 remove(A.getAttributes(Index)); 1393 return *this; 1394 } 1395 1396 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) { 1397 auto I = TargetDepAttrs.find(A); 1398 if (I != TargetDepAttrs.end()) 1399 TargetDepAttrs.erase(I); 1400 return *this; 1401 } 1402 1403 std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const { 1404 return unpackAllocSizeArgs(AllocSizeArgs); 1405 } 1406 1407 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) { 1408 if (Align == 0) return *this; 1409 1410 assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); 1411 assert(Align <= 0x40000000 && "Alignment too large."); 1412 1413 Attrs[Attribute::Alignment] = true; 1414 Alignment = Align; 1415 return *this; 1416 } 1417 1418 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) { 1419 // Default alignment, allow the target to define how to align it. 1420 if (Align == 0) return *this; 1421 1422 assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); 1423 assert(Align <= 0x100 && "Alignment too large."); 1424 1425 Attrs[Attribute::StackAlignment] = true; 1426 StackAlignment = Align; 1427 return *this; 1428 } 1429 1430 AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) { 1431 if (Bytes == 0) return *this; 1432 1433 Attrs[Attribute::Dereferenceable] = true; 1434 DerefBytes = Bytes; 1435 return *this; 1436 } 1437 1438 AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) { 1439 if (Bytes == 0) 1440 return *this; 1441 1442 Attrs[Attribute::DereferenceableOrNull] = true; 1443 DerefOrNullBytes = Bytes; 1444 return *this; 1445 } 1446 1447 AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize, 1448 const Optional<unsigned> &NumElems) { 1449 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems)); 1450 } 1451 1452 AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) { 1453 // (0, 0) is our "not present" value, so we need to check for it here. 1454 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)"); 1455 1456 Attrs[Attribute::AllocSize] = true; 1457 // Reuse existing machinery to store this as a single 64-bit integer so we can 1458 // save a few bytes over using a pair<unsigned, Optional<unsigned>>. 1459 AllocSizeArgs = RawArgs; 1460 return *this; 1461 } 1462 1463 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) { 1464 // FIXME: What if both have alignments, but they don't match?! 1465 if (!Alignment) 1466 Alignment = B.Alignment; 1467 1468 if (!StackAlignment) 1469 StackAlignment = B.StackAlignment; 1470 1471 if (!DerefBytes) 1472 DerefBytes = B.DerefBytes; 1473 1474 if (!DerefOrNullBytes) 1475 DerefOrNullBytes = B.DerefOrNullBytes; 1476 1477 if (!AllocSizeArgs) 1478 AllocSizeArgs = B.AllocSizeArgs; 1479 1480 Attrs |= B.Attrs; 1481 1482 for (auto I : B.td_attrs()) 1483 TargetDepAttrs[I.first] = I.second; 1484 1485 return *this; 1486 } 1487 1488 AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) { 1489 // FIXME: What if both have alignments, but they don't match?! 1490 if (B.Alignment) 1491 Alignment = 0; 1492 1493 if (B.StackAlignment) 1494 StackAlignment = 0; 1495 1496 if (B.DerefBytes) 1497 DerefBytes = 0; 1498 1499 if (B.DerefOrNullBytes) 1500 DerefOrNullBytes = 0; 1501 1502 if (B.AllocSizeArgs) 1503 AllocSizeArgs = 0; 1504 1505 Attrs &= ~B.Attrs; 1506 1507 for (auto I : B.td_attrs()) 1508 TargetDepAttrs.erase(I.first); 1509 1510 return *this; 1511 } 1512 1513 bool AttrBuilder::overlaps(const AttrBuilder &B) const { 1514 // First check if any of the target independent attributes overlap. 1515 if ((Attrs & B.Attrs).any()) 1516 return true; 1517 1518 // Then check if any target dependent ones do. 1519 for (const auto &I : td_attrs()) 1520 if (B.contains(I.first)) 1521 return true; 1522 1523 return false; 1524 } 1525 1526 bool AttrBuilder::contains(StringRef A) const { 1527 return TargetDepAttrs.find(A) != TargetDepAttrs.end(); 1528 } 1529 1530 bool AttrBuilder::hasAttributes() const { 1531 return !Attrs.none() || !TargetDepAttrs.empty(); 1532 } 1533 1534 bool AttrBuilder::hasAttributes(AttributeList AL, uint64_t Index) const { 1535 AttributeSet AS = AL.getAttributes(Index); 1536 1537 for (const auto Attr : AS) { 1538 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) { 1539 if (contains(Attr.getKindAsEnum())) 1540 return true; 1541 } else { 1542 assert(Attr.isStringAttribute() && "Invalid attribute kind!"); 1543 return contains(Attr.getKindAsString()); 1544 } 1545 } 1546 1547 return false; 1548 } 1549 1550 bool AttrBuilder::hasAlignmentAttr() const { 1551 return Alignment != 0; 1552 } 1553 1554 bool AttrBuilder::operator==(const AttrBuilder &B) { 1555 if (Attrs != B.Attrs) 1556 return false; 1557 1558 for (td_const_iterator I = TargetDepAttrs.begin(), 1559 E = TargetDepAttrs.end(); I != E; ++I) 1560 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end()) 1561 return false; 1562 1563 return Alignment == B.Alignment && StackAlignment == B.StackAlignment && 1564 DerefBytes == B.DerefBytes; 1565 } 1566 1567 //===----------------------------------------------------------------------===// 1568 // AttributeFuncs Function Defintions 1569 //===----------------------------------------------------------------------===// 1570 1571 /// \brief Which attributes cannot be applied to a type. 1572 AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) { 1573 AttrBuilder Incompatible; 1574 1575 if (!Ty->isIntegerTy()) 1576 // Attribute that only apply to integers. 1577 Incompatible.addAttribute(Attribute::SExt) 1578 .addAttribute(Attribute::ZExt); 1579 1580 if (!Ty->isPointerTy()) 1581 // Attribute that only apply to pointers. 1582 Incompatible.addAttribute(Attribute::ByVal) 1583 .addAttribute(Attribute::Nest) 1584 .addAttribute(Attribute::NoAlias) 1585 .addAttribute(Attribute::NoCapture) 1586 .addAttribute(Attribute::NonNull) 1587 .addDereferenceableAttr(1) // the int here is ignored 1588 .addDereferenceableOrNullAttr(1) // the int here is ignored 1589 .addAttribute(Attribute::ReadNone) 1590 .addAttribute(Attribute::ReadOnly) 1591 .addAttribute(Attribute::StructRet) 1592 .addAttribute(Attribute::InAlloca); 1593 1594 return Incompatible; 1595 } 1596 1597 template<typename AttrClass> 1598 static bool isEqual(const Function &Caller, const Function &Callee) { 1599 return Caller.getFnAttribute(AttrClass::getKind()) == 1600 Callee.getFnAttribute(AttrClass::getKind()); 1601 } 1602 1603 /// \brief Compute the logical AND of the attributes of the caller and the 1604 /// callee. 1605 /// 1606 /// This function sets the caller's attribute to false if the callee's attribute 1607 /// is false. 1608 template<typename AttrClass> 1609 static void setAND(Function &Caller, const Function &Callee) { 1610 if (AttrClass::isSet(Caller, AttrClass::getKind()) && 1611 !AttrClass::isSet(Callee, AttrClass::getKind())) 1612 AttrClass::set(Caller, AttrClass::getKind(), false); 1613 } 1614 1615 /// \brief Compute the logical OR of the attributes of the caller and the 1616 /// callee. 1617 /// 1618 /// This function sets the caller's attribute to true if the callee's attribute 1619 /// is true. 1620 template<typename AttrClass> 1621 static void setOR(Function &Caller, const Function &Callee) { 1622 if (!AttrClass::isSet(Caller, AttrClass::getKind()) && 1623 AttrClass::isSet(Callee, AttrClass::getKind())) 1624 AttrClass::set(Caller, AttrClass::getKind(), true); 1625 } 1626 1627 /// \brief If the inlined function had a higher stack protection level than the 1628 /// calling function, then bump up the caller's stack protection level. 1629 static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) { 1630 // If upgrading the SSP attribute, clear out the old SSP Attributes first. 1631 // Having multiple SSP attributes doesn't actually hurt, but it adds useless 1632 // clutter to the IR. 1633 AttrBuilder OldSSPAttr; 1634 OldSSPAttr.addAttribute(Attribute::StackProtect) 1635 .addAttribute(Attribute::StackProtectStrong) 1636 .addAttribute(Attribute::StackProtectReq); 1637 1638 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) { 1639 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr); 1640 Caller.addFnAttr(Attribute::StackProtectReq); 1641 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) && 1642 !Caller.hasFnAttribute(Attribute::StackProtectReq)) { 1643 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr); 1644 Caller.addFnAttr(Attribute::StackProtectStrong); 1645 } else if (Callee.hasFnAttribute(Attribute::StackProtect) && 1646 !Caller.hasFnAttribute(Attribute::StackProtectReq) && 1647 !Caller.hasFnAttribute(Attribute::StackProtectStrong)) 1648 Caller.addFnAttr(Attribute::StackProtect); 1649 } 1650 1651 /// \brief If the inlined function required stack probes, then ensure that 1652 /// the calling function has those too. 1653 static void adjustCallerStackProbes(Function &Caller, const Function &Callee) { 1654 if (!Caller.hasFnAttribute("probe-stack") && 1655 Callee.hasFnAttribute("probe-stack")) { 1656 Caller.addFnAttr(Callee.getFnAttribute("probe-stack")); 1657 } 1658 } 1659 1660 /// \brief If the inlined function defines the size of guard region 1661 /// on the stack, then ensure that the calling function defines a guard region 1662 /// that is no larger. 1663 static void 1664 adjustCallerStackProbeSize(Function &Caller, const Function &Callee) { 1665 if (Callee.hasFnAttribute("stack-probe-size")) { 1666 uint64_t CalleeStackProbeSize; 1667 Callee.getFnAttribute("stack-probe-size") 1668 .getValueAsString() 1669 .getAsInteger(0, CalleeStackProbeSize); 1670 if (Caller.hasFnAttribute("stack-probe-size")) { 1671 uint64_t CallerStackProbeSize; 1672 Caller.getFnAttribute("stack-probe-size") 1673 .getValueAsString() 1674 .getAsInteger(0, CallerStackProbeSize); 1675 if (CallerStackProbeSize > CalleeStackProbeSize) { 1676 Caller.addFnAttr(Callee.getFnAttribute("stack-probe-size")); 1677 } 1678 } else { 1679 Caller.addFnAttr(Callee.getFnAttribute("stack-probe-size")); 1680 } 1681 } 1682 } 1683 1684 #define GET_ATTR_COMPAT_FUNC 1685 #include "AttributesCompatFunc.inc" 1686 1687 bool AttributeFuncs::areInlineCompatible(const Function &Caller, 1688 const Function &Callee) { 1689 return hasCompatibleFnAttrs(Caller, Callee); 1690 } 1691 1692 void AttributeFuncs::mergeAttributesForInlining(Function &Caller, 1693 const Function &Callee) { 1694 mergeFnAttrs(Caller, Callee); 1695 } 1696