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