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