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