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 "AttributeSetNode.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/SmallVector.h" 23 #include "llvm/ADT/STLExtras.h" 24 #include "llvm/ADT/StringExtras.h" 25 #include "llvm/ADT/StringRef.h" 26 #include "llvm/ADT/Twine.h" 27 #include "llvm/IR/Attributes.h" 28 #include "llvm/IR/Function.h" 29 #include "llvm/IR/LLVMContext.h" 30 #include "llvm/IR/Type.h" 31 #include "llvm/Support/Compiler.h" 32 #include "llvm/Support/Debug.h" 33 #include "llvm/Support/ErrorHandling.h" 34 #include "llvm/Support/MathExtras.h" 35 #include "llvm/Support/raw_ostream.h" 36 #include <algorithm> 37 #include <cassert> 38 #include <cstdint> 39 #include <limits> 40 #include <map> 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 StringRef(); 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 StringRef(); 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::AlwaysInline)) 248 return "alwaysinline"; 249 if (hasAttribute(Attribute::ArgMemOnly)) 250 return "argmemonly"; 251 if (hasAttribute(Attribute::Builtin)) 252 return "builtin"; 253 if (hasAttribute(Attribute::ByVal)) 254 return "byval"; 255 if (hasAttribute(Attribute::Convergent)) 256 return "convergent"; 257 if (hasAttribute(Attribute::SwiftError)) 258 return "swifterror"; 259 if (hasAttribute(Attribute::SwiftSelf)) 260 return "swiftself"; 261 if (hasAttribute(Attribute::InaccessibleMemOnly)) 262 return "inaccessiblememonly"; 263 if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly)) 264 return "inaccessiblemem_or_argmemonly"; 265 if (hasAttribute(Attribute::InAlloca)) 266 return "inalloca"; 267 if (hasAttribute(Attribute::InlineHint)) 268 return "inlinehint"; 269 if (hasAttribute(Attribute::InReg)) 270 return "inreg"; 271 if (hasAttribute(Attribute::JumpTable)) 272 return "jumptable"; 273 if (hasAttribute(Attribute::MinSize)) 274 return "minsize"; 275 if (hasAttribute(Attribute::Naked)) 276 return "naked"; 277 if (hasAttribute(Attribute::Nest)) 278 return "nest"; 279 if (hasAttribute(Attribute::NoAlias)) 280 return "noalias"; 281 if (hasAttribute(Attribute::NoBuiltin)) 282 return "nobuiltin"; 283 if (hasAttribute(Attribute::NoCapture)) 284 return "nocapture"; 285 if (hasAttribute(Attribute::NoDuplicate)) 286 return "noduplicate"; 287 if (hasAttribute(Attribute::NoImplicitFloat)) 288 return "noimplicitfloat"; 289 if (hasAttribute(Attribute::NoInline)) 290 return "noinline"; 291 if (hasAttribute(Attribute::NonLazyBind)) 292 return "nonlazybind"; 293 if (hasAttribute(Attribute::NonNull)) 294 return "nonnull"; 295 if (hasAttribute(Attribute::NoRedZone)) 296 return "noredzone"; 297 if (hasAttribute(Attribute::NoReturn)) 298 return "noreturn"; 299 if (hasAttribute(Attribute::NoRecurse)) 300 return "norecurse"; 301 if (hasAttribute(Attribute::NoUnwind)) 302 return "nounwind"; 303 if (hasAttribute(Attribute::OptimizeNone)) 304 return "optnone"; 305 if (hasAttribute(Attribute::OptimizeForSize)) 306 return "optsize"; 307 if (hasAttribute(Attribute::ReadNone)) 308 return "readnone"; 309 if (hasAttribute(Attribute::ReadOnly)) 310 return "readonly"; 311 if (hasAttribute(Attribute::WriteOnly)) 312 return "writeonly"; 313 if (hasAttribute(Attribute::Returned)) 314 return "returned"; 315 if (hasAttribute(Attribute::ReturnsTwice)) 316 return "returns_twice"; 317 if (hasAttribute(Attribute::SExt)) 318 return "signext"; 319 if (hasAttribute(Attribute::StackProtect)) 320 return "ssp"; 321 if (hasAttribute(Attribute::StackProtectReq)) 322 return "sspreq"; 323 if (hasAttribute(Attribute::StackProtectStrong)) 324 return "sspstrong"; 325 if (hasAttribute(Attribute::SafeStack)) 326 return "safestack"; 327 if (hasAttribute(Attribute::StructRet)) 328 return "sret"; 329 if (hasAttribute(Attribute::SanitizeThread)) 330 return "sanitize_thread"; 331 if (hasAttribute(Attribute::SanitizeMemory)) 332 return "sanitize_memory"; 333 if (hasAttribute(Attribute::UWTable)) 334 return "uwtable"; 335 if (hasAttribute(Attribute::ZExt)) 336 return "zeroext"; 337 if (hasAttribute(Attribute::Cold)) 338 return "cold"; 339 340 // FIXME: These should be output like this: 341 // 342 // align=4 343 // alignstack=8 344 // 345 if (hasAttribute(Attribute::Alignment)) { 346 std::string Result; 347 Result += "align"; 348 Result += (InAttrGrp) ? "=" : " "; 349 Result += utostr(getValueAsInt()); 350 return Result; 351 } 352 353 auto AttrWithBytesToString = [&](const char *Name) { 354 std::string Result; 355 Result += Name; 356 if (InAttrGrp) { 357 Result += "="; 358 Result += utostr(getValueAsInt()); 359 } else { 360 Result += "("; 361 Result += utostr(getValueAsInt()); 362 Result += ")"; 363 } 364 return Result; 365 }; 366 367 if (hasAttribute(Attribute::StackAlignment)) 368 return AttrWithBytesToString("alignstack"); 369 370 if (hasAttribute(Attribute::Dereferenceable)) 371 return AttrWithBytesToString("dereferenceable"); 372 373 if (hasAttribute(Attribute::DereferenceableOrNull)) 374 return AttrWithBytesToString("dereferenceable_or_null"); 375 376 if (hasAttribute(Attribute::AllocSize)) { 377 unsigned ElemSize; 378 Optional<unsigned> NumElems; 379 std::tie(ElemSize, NumElems) = getAllocSizeArgs(); 380 381 std::string Result = "allocsize("; 382 Result += utostr(ElemSize); 383 if (NumElems.hasValue()) { 384 Result += ','; 385 Result += utostr(*NumElems); 386 } 387 Result += ')'; 388 return Result; 389 } 390 391 // Convert target-dependent attributes to strings of the form: 392 // 393 // "kind" 394 // "kind" = "value" 395 // 396 if (isStringAttribute()) { 397 std::string Result; 398 Result += (Twine('"') + getKindAsString() + Twine('"')).str(); 399 400 std::string AttrVal = pImpl->getValueAsString(); 401 if (AttrVal.empty()) return Result; 402 403 // Since some attribute strings contain special characters that cannot be 404 // printable, those have to be escaped to make the attribute value printable 405 // as is. e.g. "\01__gnu_mcount_nc" 406 { 407 raw_string_ostream OS(Result); 408 OS << "=\""; 409 PrintEscapedString(AttrVal, OS); 410 OS << "\""; 411 } 412 return Result; 413 } 414 415 llvm_unreachable("Unknown attribute"); 416 } 417 418 bool Attribute::operator<(Attribute A) const { 419 if (!pImpl && !A.pImpl) return false; 420 if (!pImpl) return true; 421 if (!A.pImpl) return false; 422 return *pImpl < *A.pImpl; 423 } 424 425 //===----------------------------------------------------------------------===// 426 // AttributeImpl Definition 427 //===----------------------------------------------------------------------===// 428 429 // Pin the vtables to this file. 430 AttributeImpl::~AttributeImpl() = default; 431 432 void EnumAttributeImpl::anchor() {} 433 434 void IntAttributeImpl::anchor() {} 435 436 void StringAttributeImpl::anchor() {} 437 438 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const { 439 if (isStringAttribute()) return false; 440 return getKindAsEnum() == A; 441 } 442 443 bool AttributeImpl::hasAttribute(StringRef Kind) const { 444 if (!isStringAttribute()) return false; 445 return getKindAsString() == Kind; 446 } 447 448 Attribute::AttrKind AttributeImpl::getKindAsEnum() const { 449 assert(isEnumAttribute() || isIntAttribute()); 450 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind(); 451 } 452 453 uint64_t AttributeImpl::getValueAsInt() const { 454 assert(isIntAttribute()); 455 return static_cast<const IntAttributeImpl *>(this)->getValue(); 456 } 457 458 StringRef AttributeImpl::getKindAsString() const { 459 assert(isStringAttribute()); 460 return static_cast<const StringAttributeImpl *>(this)->getStringKind(); 461 } 462 463 StringRef AttributeImpl::getValueAsString() const { 464 assert(isStringAttribute()); 465 return static_cast<const StringAttributeImpl *>(this)->getStringValue(); 466 } 467 468 bool AttributeImpl::operator<(const AttributeImpl &AI) const { 469 // This sorts the attributes with Attribute::AttrKinds coming first (sorted 470 // relative to their enum value) and then strings. 471 if (isEnumAttribute()) { 472 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum(); 473 if (AI.isIntAttribute()) return true; 474 if (AI.isStringAttribute()) return true; 475 } 476 477 if (isIntAttribute()) { 478 if (AI.isEnumAttribute()) return false; 479 if (AI.isIntAttribute()) { 480 if (getKindAsEnum() == AI.getKindAsEnum()) 481 return getValueAsInt() < AI.getValueAsInt(); 482 return getKindAsEnum() < AI.getKindAsEnum(); 483 } 484 if (AI.isStringAttribute()) return true; 485 } 486 487 if (AI.isEnumAttribute()) return false; 488 if (AI.isIntAttribute()) return false; 489 if (getKindAsString() == AI.getKindAsString()) 490 return getValueAsString() < AI.getValueAsString(); 491 return getKindAsString() < AI.getKindAsString(); 492 } 493 494 //===----------------------------------------------------------------------===// 495 // AttributeSetNode Definition 496 //===----------------------------------------------------------------------===// 497 498 AttributeSetNode *AttributeSetNode::get(LLVMContext &C, 499 ArrayRef<Attribute> Attrs) { 500 if (Attrs.empty()) 501 return nullptr; 502 503 // Otherwise, build a key to look up the existing attributes. 504 LLVMContextImpl *pImpl = C.pImpl; 505 FoldingSetNodeID ID; 506 507 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end()); 508 std::sort(SortedAttrs.begin(), SortedAttrs.end()); 509 510 for (Attribute Attr : SortedAttrs) 511 Attr.Profile(ID); 512 513 void *InsertPoint; 514 AttributeSetNode *PA = 515 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint); 516 517 // If we didn't find any existing attributes of the same shape then create a 518 // new one and insert it. 519 if (!PA) { 520 // Coallocate entries after the AttributeSetNode itself. 521 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size())); 522 PA = new (Mem) AttributeSetNode(SortedAttrs); 523 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint); 524 } 525 526 // Return the AttributeSetNode that we found or created. 527 return PA; 528 } 529 530 bool AttributeSetNode::hasAttribute(StringRef Kind) const { 531 for (Attribute I : *this) 532 if (I.hasAttribute(Kind)) 533 return true; 534 return false; 535 } 536 537 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const { 538 if (hasAttribute(Kind)) { 539 for (Attribute I : *this) 540 if (I.hasAttribute(Kind)) 541 return I; 542 } 543 return Attribute(); 544 } 545 546 Attribute AttributeSetNode::getAttribute(StringRef Kind) const { 547 for (Attribute I : *this) 548 if (I.hasAttribute(Kind)) 549 return I; 550 return Attribute(); 551 } 552 553 unsigned AttributeSetNode::getAlignment() const { 554 for (Attribute I : *this) 555 if (I.hasAttribute(Attribute::Alignment)) 556 return I.getAlignment(); 557 return 0; 558 } 559 560 unsigned AttributeSetNode::getStackAlignment() const { 561 for (Attribute I : *this) 562 if (I.hasAttribute(Attribute::StackAlignment)) 563 return I.getStackAlignment(); 564 return 0; 565 } 566 567 uint64_t AttributeSetNode::getDereferenceableBytes() const { 568 for (Attribute I : *this) 569 if (I.hasAttribute(Attribute::Dereferenceable)) 570 return I.getDereferenceableBytes(); 571 return 0; 572 } 573 574 uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const { 575 for (Attribute I : *this) 576 if (I.hasAttribute(Attribute::DereferenceableOrNull)) 577 return I.getDereferenceableOrNullBytes(); 578 return 0; 579 } 580 581 std::pair<unsigned, Optional<unsigned>> 582 AttributeSetNode::getAllocSizeArgs() const { 583 for (Attribute I : *this) 584 if (I.hasAttribute(Attribute::AllocSize)) 585 return I.getAllocSizeArgs(); 586 return std::make_pair(0, 0); 587 } 588 589 std::string AttributeSetNode::getAsString(bool InAttrGrp) const { 590 std::string Str; 591 for (iterator I = begin(), E = end(); I != E; ++I) { 592 if (I != begin()) 593 Str += ' '; 594 Str += I->getAsString(InAttrGrp); 595 } 596 return Str; 597 } 598 599 //===----------------------------------------------------------------------===// 600 // AttributeListImpl Definition 601 //===----------------------------------------------------------------------===// 602 603 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 604 LLVM_DUMP_METHOD void AttributeListImpl::dump() const { 605 AttributeList(const_cast<AttributeListImpl *>(this)).dump(); 606 } 607 #endif 608 609 //===----------------------------------------------------------------------===// 610 // AttributeList Construction and Mutation Methods 611 //===----------------------------------------------------------------------===// 612 613 AttributeList AttributeList::getImpl( 614 LLVMContext &C, ArrayRef<std::pair<unsigned, AttributeSetNode *>> Attrs) { 615 LLVMContextImpl *pImpl = C.pImpl; 616 FoldingSetNodeID ID; 617 AttributeListImpl::Profile(ID, Attrs); 618 619 void *InsertPoint; 620 AttributeListImpl *PA = 621 pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint); 622 623 // If we didn't find any existing attributes of the same shape then 624 // create a new one and insert it. 625 if (!PA) { 626 // Coallocate entries after the AttributeListImpl itself. 627 void *Mem = ::operator new( 628 AttributeListImpl::totalSizeToAlloc<IndexAttrPair>(Attrs.size())); 629 PA = new (Mem) AttributeListImpl(C, Attrs); 630 pImpl->AttrsLists.InsertNode(PA, InsertPoint); 631 } 632 633 // Return the AttributesList that we found or created. 634 return AttributeList(PA); 635 } 636 637 AttributeList 638 AttributeList::get(LLVMContext &C, 639 ArrayRef<std::pair<unsigned, Attribute>> Attrs) { 640 // If there are no attributes then return a null AttributesList pointer. 641 if (Attrs.empty()) 642 return AttributeList(); 643 644 assert(std::is_sorted(Attrs.begin(), Attrs.end(), 645 [](const std::pair<unsigned, Attribute> &LHS, 646 const std::pair<unsigned, Attribute> &RHS) { 647 return LHS.first < RHS.first; 648 }) && "Misordered Attributes list!"); 649 assert(none_of(Attrs, 650 [](const std::pair<unsigned, Attribute> &Pair) { 651 return Pair.second.hasAttribute(Attribute::None); 652 }) && 653 "Pointless attribute!"); 654 655 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes 656 // list. 657 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec; 658 for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(), 659 E = Attrs.end(); I != E; ) { 660 unsigned Index = I->first; 661 SmallVector<Attribute, 4> AttrVec; 662 while (I != E && I->first == Index) { 663 AttrVec.push_back(I->second); 664 ++I; 665 } 666 667 AttrPairVec.emplace_back(Index, AttributeSetNode::get(C, AttrVec)); 668 } 669 670 return getImpl(C, AttrPairVec); 671 } 672 673 AttributeList 674 AttributeList::get(LLVMContext &C, 675 ArrayRef<std::pair<unsigned, AttributeSetNode *>> Attrs) { 676 // If there are no attributes then return a null AttributesList pointer. 677 if (Attrs.empty()) 678 return AttributeList(); 679 680 return getImpl(C, Attrs); 681 } 682 683 AttributeList AttributeList::get(LLVMContext &C, unsigned Index, 684 const AttrBuilder &B) { 685 if (!B.hasAttributes()) 686 return AttributeList(); 687 688 // Add target-independent attributes. 689 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs; 690 for (Attribute::AttrKind Kind = Attribute::None; 691 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) { 692 if (!B.contains(Kind)) 693 continue; 694 695 Attribute Attr; 696 switch (Kind) { 697 case Attribute::Alignment: 698 Attr = Attribute::getWithAlignment(C, B.getAlignment()); 699 break; 700 case Attribute::StackAlignment: 701 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment()); 702 break; 703 case Attribute::Dereferenceable: 704 Attr = Attribute::getWithDereferenceableBytes( 705 C, B.getDereferenceableBytes()); 706 break; 707 case Attribute::DereferenceableOrNull: 708 Attr = Attribute::getWithDereferenceableOrNullBytes( 709 C, B.getDereferenceableOrNullBytes()); 710 break; 711 case Attribute::AllocSize: { 712 auto A = B.getAllocSizeArgs(); 713 Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second); 714 break; 715 } 716 default: 717 Attr = Attribute::get(C, Kind); 718 } 719 Attrs.emplace_back(Index, Attr); 720 } 721 722 // Add target-dependent (string) attributes. 723 for (const auto &TDA : B.td_attrs()) 724 Attrs.emplace_back(Index, Attribute::get(C, TDA.first, TDA.second)); 725 726 return get(C, Attrs); 727 } 728 729 AttributeList AttributeList::get(LLVMContext &C, unsigned Index, 730 ArrayRef<Attribute::AttrKind> Kinds) { 731 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs; 732 for (Attribute::AttrKind K : Kinds) 733 Attrs.emplace_back(Index, Attribute::get(C, K)); 734 return get(C, Attrs); 735 } 736 737 AttributeList AttributeList::get(LLVMContext &C, unsigned Index, 738 ArrayRef<StringRef> Kinds) { 739 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs; 740 for (StringRef K : Kinds) 741 Attrs.emplace_back(Index, Attribute::get(C, K)); 742 return get(C, Attrs); 743 } 744 745 AttributeList AttributeList::get(LLVMContext &C, 746 ArrayRef<AttributeList> Attrs) { 747 if (Attrs.empty()) 748 return AttributeList(); 749 if (Attrs.size() == 1) return Attrs[0]; 750 751 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec; 752 AttributeListImpl *A0 = Attrs[0].pImpl; 753 if (A0) 754 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumSlots())); 755 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec 756 // ordered by index. Because we know that each list in Attrs is ordered by 757 // index we only need to merge each successive list in rather than doing a 758 // full sort. 759 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) { 760 AttributeListImpl *AS = Attrs[I].pImpl; 761 if (!AS) continue; 762 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator 763 ANVI = AttrNodeVec.begin(), ANVE; 764 for (const IndexAttrPair *AI = AS->getNode(0), 765 *AE = AS->getNode(AS->getNumSlots()); 766 AI != AE; ++AI) { 767 ANVE = AttrNodeVec.end(); 768 while (ANVI != ANVE && ANVI->first <= AI->first) 769 ++ANVI; 770 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1; 771 } 772 } 773 774 return getImpl(C, AttrNodeVec); 775 } 776 777 AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index, 778 Attribute::AttrKind Kind) const { 779 if (hasAttribute(Index, Kind)) return *this; 780 return addAttributes(C, Index, AttributeList::get(C, Index, Kind)); 781 } 782 783 AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index, 784 StringRef Kind, 785 StringRef Value) const { 786 AttrBuilder B; 787 B.addAttribute(Kind, Value); 788 return addAttributes(C, Index, AttributeList::get(C, Index, B)); 789 } 790 791 AttributeList AttributeList::addAttribute(LLVMContext &C, 792 ArrayRef<unsigned> Indices, 793 Attribute A) const { 794 unsigned I = 0, E = pImpl ? pImpl->getNumSlots() : 0; 795 auto IdxI = Indices.begin(), IdxE = Indices.end(); 796 SmallVector<AttributeList, 4> AttrSet; 797 798 while (I != E && IdxI != IdxE) { 799 if (getSlotIndex(I) < *IdxI) 800 AttrSet.emplace_back(getSlotAttributes(I++)); 801 else if (getSlotIndex(I) > *IdxI) 802 AttrSet.emplace_back(AttributeList::get(C, std::make_pair(*IdxI++, A))); 803 else { 804 AttrBuilder B(getSlotAttributes(I), *IdxI); 805 B.addAttribute(A); 806 AttrSet.emplace_back(AttributeList::get(C, *IdxI, B)); 807 ++I; 808 ++IdxI; 809 } 810 } 811 812 while (I != E) 813 AttrSet.emplace_back(getSlotAttributes(I++)); 814 815 while (IdxI != IdxE) 816 AttrSet.emplace_back(AttributeList::get(C, std::make_pair(*IdxI++, A))); 817 818 return get(C, AttrSet); 819 } 820 821 AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index, 822 AttributeList Attrs) const { 823 if (!pImpl) return Attrs; 824 if (!Attrs.pImpl) return *this; 825 826 #ifndef NDEBUG 827 // FIXME it is not obvious how this should work for alignment. For now, say 828 // we can't change a known alignment. 829 unsigned OldAlign = getParamAlignment(Index); 830 unsigned NewAlign = Attrs.getParamAlignment(Index); 831 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) && 832 "Attempt to change alignment!"); 833 #endif 834 835 // Add the attribute slots before the one we're trying to add. 836 SmallVector<AttributeList, 4> AttrSet; 837 uint64_t NumAttrs = pImpl->getNumSlots(); 838 AttributeList AS; 839 uint64_t LastIndex = 0; 840 for (unsigned I = 0, E = NumAttrs; I != E; ++I) { 841 if (getSlotIndex(I) >= Index) { 842 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++); 843 break; 844 } 845 LastIndex = I + 1; 846 AttrSet.push_back(getSlotAttributes(I)); 847 } 848 849 // Now add the attribute into the correct slot. There may already be an 850 // AttributeList there. 851 AttrBuilder B(AS, Index); 852 853 for (unsigned I = 0, E = Attrs.pImpl->getNumSlots(); I != E; ++I) 854 if (Attrs.getSlotIndex(I) == Index) { 855 for (AttributeListImpl::iterator II = Attrs.pImpl->begin(I), 856 IE = Attrs.pImpl->end(I); 857 II != IE; ++II) 858 B.addAttribute(*II); 859 break; 860 } 861 862 AttrSet.push_back(AttributeList::get(C, Index, B)); 863 864 // Add the remaining attribute slots. 865 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I) 866 AttrSet.push_back(getSlotAttributes(I)); 867 868 return get(C, AttrSet); 869 } 870 871 AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index, 872 Attribute::AttrKind Kind) const { 873 if (!hasAttribute(Index, Kind)) return *this; 874 return removeAttributes(C, Index, AttributeList::get(C, Index, Kind)); 875 } 876 877 AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index, 878 StringRef Kind) const { 879 if (!hasAttribute(Index, Kind)) return *this; 880 return removeAttributes(C, Index, AttributeList::get(C, Index, Kind)); 881 } 882 883 AttributeList AttributeList::removeAttributes(LLVMContext &C, unsigned Index, 884 AttributeList Attrs) const { 885 if (!pImpl) 886 return AttributeList(); 887 if (!Attrs.pImpl) return *this; 888 889 // FIXME it is not obvious how this should work for alignment. 890 // For now, say we can't pass in alignment, which no current use does. 891 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) && 892 "Attempt to change alignment!"); 893 894 // Add the attribute slots before the one we're trying to add. 895 SmallVector<AttributeList, 4> AttrSet; 896 uint64_t NumAttrs = pImpl->getNumSlots(); 897 AttributeList AS; 898 uint64_t LastIndex = 0; 899 for (unsigned I = 0, E = NumAttrs; I != E; ++I) { 900 if (getSlotIndex(I) >= Index) { 901 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++); 902 break; 903 } 904 LastIndex = I + 1; 905 AttrSet.push_back(getSlotAttributes(I)); 906 } 907 908 // Now remove the attribute from the correct slot. There may already be an 909 // AttributeList there. 910 AttrBuilder B(AS, Index); 911 912 for (unsigned I = 0, E = Attrs.pImpl->getNumSlots(); I != E; ++I) 913 if (Attrs.getSlotIndex(I) == Index) { 914 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index); 915 break; 916 } 917 918 AttrSet.push_back(AttributeList::get(C, Index, B)); 919 920 // Add the remaining attribute slots. 921 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I) 922 AttrSet.push_back(getSlotAttributes(I)); 923 924 return get(C, AttrSet); 925 } 926 927 AttributeList AttributeList::removeAttributes(LLVMContext &C, unsigned Index, 928 const AttrBuilder &Attrs) const { 929 if (!pImpl) 930 return AttributeList(); 931 932 // FIXME it is not obvious how this should work for alignment. 933 // For now, say we can't pass in alignment, which no current use does. 934 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!"); 935 936 // Add the attribute slots before the one we're trying to add. 937 SmallVector<AttributeList, 4> AttrSet; 938 uint64_t NumAttrs = pImpl->getNumSlots(); 939 AttributeList AS; 940 uint64_t LastIndex = 0; 941 for (unsigned I = 0, E = NumAttrs; I != E; ++I) { 942 if (getSlotIndex(I) >= Index) { 943 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++); 944 break; 945 } 946 LastIndex = I + 1; 947 AttrSet.push_back(getSlotAttributes(I)); 948 } 949 950 // Now remove the attribute from the correct slot. There may already be an 951 // AttributeList there. 952 AttrBuilder B(AS, Index); 953 B.remove(Attrs); 954 955 AttrSet.push_back(AttributeList::get(C, Index, B)); 956 957 // Add the remaining attribute slots. 958 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I) 959 AttrSet.push_back(getSlotAttributes(I)); 960 961 return get(C, AttrSet); 962 } 963 964 AttributeList AttributeList::addDereferenceableAttr(LLVMContext &C, 965 unsigned Index, 966 uint64_t Bytes) const { 967 AttrBuilder B; 968 B.addDereferenceableAttr(Bytes); 969 return addAttributes(C, Index, AttributeList::get(C, Index, B)); 970 } 971 972 AttributeList 973 AttributeList::addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index, 974 uint64_t Bytes) const { 975 AttrBuilder B; 976 B.addDereferenceableOrNullAttr(Bytes); 977 return addAttributes(C, Index, AttributeList::get(C, Index, B)); 978 } 979 980 AttributeList 981 AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index, 982 unsigned ElemSizeArg, 983 const Optional<unsigned> &NumElemsArg) { 984 AttrBuilder B; 985 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg); 986 return addAttributes(C, Index, AttributeList::get(C, Index, B)); 987 } 988 989 //===----------------------------------------------------------------------===// 990 // AttributeList Accessor Methods 991 //===----------------------------------------------------------------------===// 992 993 LLVMContext &AttributeList::getContext() const { return pImpl->getContext(); } 994 995 AttributeList AttributeList::getParamAttributes(unsigned Index) const { 996 return pImpl && hasAttributes(Index) 997 ? AttributeList::get( 998 pImpl->getContext(), 999 ArrayRef<std::pair<unsigned, AttributeSetNode *>>( 1000 std::make_pair(Index, getAttributes(Index)))) 1001 : AttributeList(); 1002 } 1003 1004 AttributeList AttributeList::getRetAttributes() const { 1005 return pImpl && hasAttributes(ReturnIndex) 1006 ? AttributeList::get( 1007 pImpl->getContext(), 1008 ArrayRef<std::pair<unsigned, AttributeSetNode *>>( 1009 std::make_pair(ReturnIndex, getAttributes(ReturnIndex)))) 1010 : AttributeList(); 1011 } 1012 1013 AttributeList AttributeList::getFnAttributes() const { 1014 return pImpl && hasAttributes(FunctionIndex) 1015 ? AttributeList::get( 1016 pImpl->getContext(), 1017 ArrayRef<std::pair<unsigned, AttributeSetNode *>>( 1018 std::make_pair(FunctionIndex, 1019 getAttributes(FunctionIndex)))) 1020 : AttributeList(); 1021 } 1022 1023 bool AttributeList::hasAttribute(unsigned Index, 1024 Attribute::AttrKind Kind) const { 1025 AttributeSetNode *ASN = getAttributes(Index); 1026 return ASN && ASN->hasAttribute(Kind); 1027 } 1028 1029 bool AttributeList::hasAttribute(unsigned Index, StringRef Kind) const { 1030 AttributeSetNode *ASN = getAttributes(Index); 1031 return ASN && ASN->hasAttribute(Kind); 1032 } 1033 1034 bool AttributeList::hasAttributes(unsigned Index) const { 1035 AttributeSetNode *ASN = getAttributes(Index); 1036 return ASN && ASN->hasAttributes(); 1037 } 1038 1039 bool AttributeList::hasFnAttribute(Attribute::AttrKind Kind) const { 1040 return pImpl && pImpl->hasFnAttribute(Kind); 1041 } 1042 1043 bool AttributeList::hasFnAttribute(StringRef Kind) const { 1044 return hasAttribute(AttributeList::FunctionIndex, Kind); 1045 } 1046 1047 bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr, 1048 unsigned *Index) const { 1049 if (!pImpl) return false; 1050 1051 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I) 1052 for (AttributeListImpl::iterator II = pImpl->begin(I), IE = pImpl->end(I); 1053 II != IE; ++II) 1054 if (II->hasAttribute(Attr)) { 1055 if (Index) *Index = pImpl->getSlotIndex(I); 1056 return true; 1057 } 1058 1059 return false; 1060 } 1061 1062 Attribute AttributeList::getAttribute(unsigned Index, 1063 Attribute::AttrKind Kind) const { 1064 AttributeSetNode *ASN = getAttributes(Index); 1065 return ASN ? ASN->getAttribute(Kind) : Attribute(); 1066 } 1067 1068 Attribute AttributeList::getAttribute(unsigned Index, StringRef Kind) const { 1069 AttributeSetNode *ASN = getAttributes(Index); 1070 return ASN ? ASN->getAttribute(Kind) : Attribute(); 1071 } 1072 1073 unsigned AttributeList::getParamAlignment(unsigned Index) const { 1074 AttributeSetNode *ASN = getAttributes(Index); 1075 return ASN ? ASN->getAlignment() : 0; 1076 } 1077 1078 unsigned AttributeList::getStackAlignment(unsigned Index) const { 1079 AttributeSetNode *ASN = getAttributes(Index); 1080 return ASN ? ASN->getStackAlignment() : 0; 1081 } 1082 1083 uint64_t AttributeList::getDereferenceableBytes(unsigned Index) const { 1084 AttributeSetNode *ASN = getAttributes(Index); 1085 return ASN ? ASN->getDereferenceableBytes() : 0; 1086 } 1087 1088 uint64_t AttributeList::getDereferenceableOrNullBytes(unsigned Index) const { 1089 AttributeSetNode *ASN = getAttributes(Index); 1090 return ASN ? ASN->getDereferenceableOrNullBytes() : 0; 1091 } 1092 1093 std::pair<unsigned, Optional<unsigned>> 1094 AttributeList::getAllocSizeArgs(unsigned Index) const { 1095 AttributeSetNode *ASN = getAttributes(Index); 1096 return ASN ? ASN->getAllocSizeArgs() : std::make_pair(0u, Optional<unsigned>(0u)); 1097 } 1098 1099 std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const { 1100 AttributeSetNode *ASN = getAttributes(Index); 1101 return ASN ? ASN->getAsString(InAttrGrp) : std::string(""); 1102 } 1103 1104 AttributeSetNode *AttributeList::getAttributes(unsigned Index) const { 1105 if (!pImpl) return nullptr; 1106 1107 // Loop through to find the attribute node we want. 1108 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I) 1109 if (pImpl->getSlotIndex(I) == Index) 1110 return pImpl->getSlotNode(I); 1111 1112 return nullptr; 1113 } 1114 1115 AttributeList::iterator AttributeList::begin(unsigned Slot) const { 1116 if (!pImpl) 1117 return ArrayRef<Attribute>().begin(); 1118 return pImpl->begin(Slot); 1119 } 1120 1121 AttributeList::iterator AttributeList::end(unsigned Slot) const { 1122 if (!pImpl) 1123 return ArrayRef<Attribute>().end(); 1124 return pImpl->end(Slot); 1125 } 1126 1127 //===----------------------------------------------------------------------===// 1128 // AttributeList Introspection Methods 1129 //===----------------------------------------------------------------------===// 1130 1131 unsigned AttributeList::getNumSlots() const { 1132 return pImpl ? pImpl->getNumSlots() : 0; 1133 } 1134 1135 unsigned AttributeList::getSlotIndex(unsigned Slot) const { 1136 assert(pImpl && Slot < pImpl->getNumSlots() && 1137 "Slot # out of range!"); 1138 return pImpl->getSlotIndex(Slot); 1139 } 1140 1141 AttributeList AttributeList::getSlotAttributes(unsigned Slot) const { 1142 assert(pImpl && Slot < pImpl->getNumSlots() && 1143 "Slot # out of range!"); 1144 return pImpl->getSlotAttributes(Slot); 1145 } 1146 1147 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1148 LLVM_DUMP_METHOD void AttributeList::dump() const { 1149 dbgs() << "PAL[\n"; 1150 1151 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) { 1152 uint64_t Index = getSlotIndex(i); 1153 dbgs() << " { "; 1154 if (Index == ~0U) 1155 dbgs() << "~0U"; 1156 else 1157 dbgs() << Index; 1158 dbgs() << " => " << getAsString(Index) << " }\n"; 1159 } 1160 1161 dbgs() << "]\n"; 1162 } 1163 #endif 1164 1165 //===----------------------------------------------------------------------===// 1166 // AttrBuilder Method Implementations 1167 //===----------------------------------------------------------------------===// 1168 1169 AttrBuilder::AttrBuilder(AttributeList AS, unsigned Index) { 1170 AttributeListImpl *pImpl = AS.pImpl; 1171 if (!pImpl) return; 1172 1173 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I) { 1174 if (pImpl->getSlotIndex(I) != Index) continue; 1175 1176 for (AttributeListImpl::iterator II = pImpl->begin(I), IE = pImpl->end(I); 1177 II != IE; ++II) 1178 addAttribute(*II); 1179 1180 break; 1181 } 1182 } 1183 1184 void AttrBuilder::clear() { 1185 Attrs.reset(); 1186 TargetDepAttrs.clear(); 1187 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0; 1188 AllocSizeArgs = 0; 1189 } 1190 1191 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) { 1192 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!"); 1193 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment && 1194 Val != Attribute::Dereferenceable && Val != Attribute::AllocSize && 1195 "Adding integer attribute without adding a value!"); 1196 Attrs[Val] = true; 1197 return *this; 1198 } 1199 1200 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) { 1201 if (Attr.isStringAttribute()) { 1202 addAttribute(Attr.getKindAsString(), Attr.getValueAsString()); 1203 return *this; 1204 } 1205 1206 Attribute::AttrKind Kind = Attr.getKindAsEnum(); 1207 Attrs[Kind] = true; 1208 1209 if (Kind == Attribute::Alignment) 1210 Alignment = Attr.getAlignment(); 1211 else if (Kind == Attribute::StackAlignment) 1212 StackAlignment = Attr.getStackAlignment(); 1213 else if (Kind == Attribute::Dereferenceable) 1214 DerefBytes = Attr.getDereferenceableBytes(); 1215 else if (Kind == Attribute::DereferenceableOrNull) 1216 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes(); 1217 else if (Kind == Attribute::AllocSize) 1218 AllocSizeArgs = Attr.getValueAsInt(); 1219 return *this; 1220 } 1221 1222 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) { 1223 TargetDepAttrs[A] = V; 1224 return *this; 1225 } 1226 1227 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) { 1228 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!"); 1229 Attrs[Val] = false; 1230 1231 if (Val == Attribute::Alignment) 1232 Alignment = 0; 1233 else if (Val == Attribute::StackAlignment) 1234 StackAlignment = 0; 1235 else if (Val == Attribute::Dereferenceable) 1236 DerefBytes = 0; 1237 else if (Val == Attribute::DereferenceableOrNull) 1238 DerefOrNullBytes = 0; 1239 else if (Val == Attribute::AllocSize) 1240 AllocSizeArgs = 0; 1241 1242 return *this; 1243 } 1244 1245 AttrBuilder &AttrBuilder::removeAttributes(AttributeList A, uint64_t Index) { 1246 unsigned Slot = ~0U; 1247 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I) 1248 if (A.getSlotIndex(I) == Index) { 1249 Slot = I; 1250 break; 1251 } 1252 1253 assert(Slot != ~0U && "Couldn't find index in AttributeList!"); 1254 1255 for (AttributeList::iterator I = A.begin(Slot), E = A.end(Slot); I != E; 1256 ++I) { 1257 Attribute Attr = *I; 1258 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) { 1259 removeAttribute(Attr.getKindAsEnum()); 1260 } else { 1261 assert(Attr.isStringAttribute() && "Invalid attribute type!"); 1262 removeAttribute(Attr.getKindAsString()); 1263 } 1264 } 1265 1266 return *this; 1267 } 1268 1269 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) { 1270 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A); 1271 if (I != TargetDepAttrs.end()) 1272 TargetDepAttrs.erase(I); 1273 return *this; 1274 } 1275 1276 std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const { 1277 return unpackAllocSizeArgs(AllocSizeArgs); 1278 } 1279 1280 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) { 1281 if (Align == 0) return *this; 1282 1283 assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); 1284 assert(Align <= 0x40000000 && "Alignment too large."); 1285 1286 Attrs[Attribute::Alignment] = true; 1287 Alignment = Align; 1288 return *this; 1289 } 1290 1291 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) { 1292 // Default alignment, allow the target to define how to align it. 1293 if (Align == 0) return *this; 1294 1295 assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); 1296 assert(Align <= 0x100 && "Alignment too large."); 1297 1298 Attrs[Attribute::StackAlignment] = true; 1299 StackAlignment = Align; 1300 return *this; 1301 } 1302 1303 AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) { 1304 if (Bytes == 0) return *this; 1305 1306 Attrs[Attribute::Dereferenceable] = true; 1307 DerefBytes = Bytes; 1308 return *this; 1309 } 1310 1311 AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) { 1312 if (Bytes == 0) 1313 return *this; 1314 1315 Attrs[Attribute::DereferenceableOrNull] = true; 1316 DerefOrNullBytes = Bytes; 1317 return *this; 1318 } 1319 1320 AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize, 1321 const Optional<unsigned> &NumElems) { 1322 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems)); 1323 } 1324 1325 AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) { 1326 // (0, 0) is our "not present" value, so we need to check for it here. 1327 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)"); 1328 1329 Attrs[Attribute::AllocSize] = true; 1330 // Reuse existing machinery to store this as a single 64-bit integer so we can 1331 // save a few bytes over using a pair<unsigned, Optional<unsigned>>. 1332 AllocSizeArgs = RawArgs; 1333 return *this; 1334 } 1335 1336 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) { 1337 // FIXME: What if both have alignments, but they don't match?! 1338 if (!Alignment) 1339 Alignment = B.Alignment; 1340 1341 if (!StackAlignment) 1342 StackAlignment = B.StackAlignment; 1343 1344 if (!DerefBytes) 1345 DerefBytes = B.DerefBytes; 1346 1347 if (!DerefOrNullBytes) 1348 DerefOrNullBytes = B.DerefOrNullBytes; 1349 1350 if (!AllocSizeArgs) 1351 AllocSizeArgs = B.AllocSizeArgs; 1352 1353 Attrs |= B.Attrs; 1354 1355 for (auto I : B.td_attrs()) 1356 TargetDepAttrs[I.first] = I.second; 1357 1358 return *this; 1359 } 1360 1361 AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) { 1362 // FIXME: What if both have alignments, but they don't match?! 1363 if (B.Alignment) 1364 Alignment = 0; 1365 1366 if (B.StackAlignment) 1367 StackAlignment = 0; 1368 1369 if (B.DerefBytes) 1370 DerefBytes = 0; 1371 1372 if (B.DerefOrNullBytes) 1373 DerefOrNullBytes = 0; 1374 1375 if (B.AllocSizeArgs) 1376 AllocSizeArgs = 0; 1377 1378 Attrs &= ~B.Attrs; 1379 1380 for (auto I : B.td_attrs()) 1381 TargetDepAttrs.erase(I.first); 1382 1383 return *this; 1384 } 1385 1386 bool AttrBuilder::overlaps(const AttrBuilder &B) const { 1387 // First check if any of the target independent attributes overlap. 1388 if ((Attrs & B.Attrs).any()) 1389 return true; 1390 1391 // Then check if any target dependent ones do. 1392 for (const auto &I : td_attrs()) 1393 if (B.contains(I.first)) 1394 return true; 1395 1396 return false; 1397 } 1398 1399 bool AttrBuilder::contains(StringRef A) const { 1400 return TargetDepAttrs.find(A) != TargetDepAttrs.end(); 1401 } 1402 1403 bool AttrBuilder::hasAttributes() const { 1404 return !Attrs.none() || !TargetDepAttrs.empty(); 1405 } 1406 1407 bool AttrBuilder::hasAttributes(AttributeList A, uint64_t Index) const { 1408 unsigned Slot = ~0U; 1409 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I) 1410 if (A.getSlotIndex(I) == Index) { 1411 Slot = I; 1412 break; 1413 } 1414 1415 assert(Slot != ~0U && "Couldn't find the index!"); 1416 1417 for (AttributeList::iterator I = A.begin(Slot), E = A.end(Slot); I != E; 1418 ++I) { 1419 Attribute Attr = *I; 1420 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) { 1421 if (Attrs[I->getKindAsEnum()]) 1422 return true; 1423 } else { 1424 assert(Attr.isStringAttribute() && "Invalid attribute kind!"); 1425 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end(); 1426 } 1427 } 1428 1429 return false; 1430 } 1431 1432 bool AttrBuilder::hasAlignmentAttr() const { 1433 return Alignment != 0; 1434 } 1435 1436 bool AttrBuilder::operator==(const AttrBuilder &B) { 1437 if (Attrs != B.Attrs) 1438 return false; 1439 1440 for (td_const_iterator I = TargetDepAttrs.begin(), 1441 E = TargetDepAttrs.end(); I != E; ++I) 1442 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end()) 1443 return false; 1444 1445 return Alignment == B.Alignment && StackAlignment == B.StackAlignment && 1446 DerefBytes == B.DerefBytes; 1447 } 1448 1449 //===----------------------------------------------------------------------===// 1450 // AttributeFuncs Function Defintions 1451 //===----------------------------------------------------------------------===// 1452 1453 /// \brief Which attributes cannot be applied to a type. 1454 AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) { 1455 AttrBuilder Incompatible; 1456 1457 if (!Ty->isIntegerTy()) 1458 // Attribute that only apply to integers. 1459 Incompatible.addAttribute(Attribute::SExt) 1460 .addAttribute(Attribute::ZExt); 1461 1462 if (!Ty->isPointerTy()) 1463 // Attribute that only apply to pointers. 1464 Incompatible.addAttribute(Attribute::ByVal) 1465 .addAttribute(Attribute::Nest) 1466 .addAttribute(Attribute::NoAlias) 1467 .addAttribute(Attribute::NoCapture) 1468 .addAttribute(Attribute::NonNull) 1469 .addDereferenceableAttr(1) // the int here is ignored 1470 .addDereferenceableOrNullAttr(1) // the int here is ignored 1471 .addAttribute(Attribute::ReadNone) 1472 .addAttribute(Attribute::ReadOnly) 1473 .addAttribute(Attribute::StructRet) 1474 .addAttribute(Attribute::InAlloca); 1475 1476 return Incompatible; 1477 } 1478 1479 template<typename AttrClass> 1480 static bool isEqual(const Function &Caller, const Function &Callee) { 1481 return Caller.getFnAttribute(AttrClass::getKind()) == 1482 Callee.getFnAttribute(AttrClass::getKind()); 1483 } 1484 1485 /// \brief Compute the logical AND of the attributes of the caller and the 1486 /// callee. 1487 /// 1488 /// This function sets the caller's attribute to false if the callee's attribute 1489 /// is false. 1490 template<typename AttrClass> 1491 static void setAND(Function &Caller, const Function &Callee) { 1492 if (AttrClass::isSet(Caller, AttrClass::getKind()) && 1493 !AttrClass::isSet(Callee, AttrClass::getKind())) 1494 AttrClass::set(Caller, AttrClass::getKind(), false); 1495 } 1496 1497 /// \brief Compute the logical OR of the attributes of the caller and the 1498 /// callee. 1499 /// 1500 /// This function sets the caller's attribute to true if the callee's attribute 1501 /// is true. 1502 template<typename AttrClass> 1503 static void setOR(Function &Caller, const Function &Callee) { 1504 if (!AttrClass::isSet(Caller, AttrClass::getKind()) && 1505 AttrClass::isSet(Callee, AttrClass::getKind())) 1506 AttrClass::set(Caller, AttrClass::getKind(), true); 1507 } 1508 1509 /// \brief If the inlined function had a higher stack protection level than the 1510 /// calling function, then bump up the caller's stack protection level. 1511 static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) { 1512 // If upgrading the SSP attribute, clear out the old SSP Attributes first. 1513 // Having multiple SSP attributes doesn't actually hurt, but it adds useless 1514 // clutter to the IR. 1515 AttrBuilder B; 1516 B.addAttribute(Attribute::StackProtect) 1517 .addAttribute(Attribute::StackProtectStrong) 1518 .addAttribute(Attribute::StackProtectReq); 1519 AttributeList OldSSPAttr = 1520 AttributeList::get(Caller.getContext(), AttributeList::FunctionIndex, B); 1521 1522 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) { 1523 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr); 1524 Caller.addFnAttr(Attribute::StackProtectReq); 1525 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) && 1526 !Caller.hasFnAttribute(Attribute::StackProtectReq)) { 1527 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr); 1528 Caller.addFnAttr(Attribute::StackProtectStrong); 1529 } else if (Callee.hasFnAttribute(Attribute::StackProtect) && 1530 !Caller.hasFnAttribute(Attribute::StackProtectReq) && 1531 !Caller.hasFnAttribute(Attribute::StackProtectStrong)) 1532 Caller.addFnAttr(Attribute::StackProtect); 1533 } 1534 1535 #define GET_ATTR_COMPAT_FUNC 1536 #include "AttributesCompatFunc.inc" 1537 1538 bool AttributeFuncs::areInlineCompatible(const Function &Caller, 1539 const Function &Callee) { 1540 return hasCompatibleFnAttrs(Caller, Callee); 1541 } 1542 1543 void AttributeFuncs::mergeAttributesForInlining(Function &Caller, 1544 const Function &Callee) { 1545 mergeFnAttrs(Caller, Callee); 1546 } 1547