1 //===- Attributes.cpp - Implement AttributesList --------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // \file 10 // This file implements the Attribute, AttributeImpl, AttrBuilder, 11 // AttributeListImpl, and AttributeList classes. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/IR/Attributes.h" 16 #include "AttributeImpl.h" 17 #include "LLVMContextImpl.h" 18 #include "llvm/ADT/ArrayRef.h" 19 #include "llvm/ADT/FoldingSet.h" 20 #include "llvm/ADT/Optional.h" 21 #include "llvm/ADT/STLExtras.h" 22 #include "llvm/ADT/SmallVector.h" 23 #include "llvm/ADT/StringExtras.h" 24 #include "llvm/ADT/StringRef.h" 25 #include "llvm/ADT/StringSwitch.h" 26 #include "llvm/ADT/Twine.h" 27 #include "llvm/Config/llvm-config.h" 28 #include "llvm/IR/Function.h" 29 #include "llvm/IR/LLVMContext.h" 30 #include "llvm/IR/Type.h" 31 #include "llvm/Support/Compiler.h" 32 #include "llvm/Support/Debug.h" 33 #include "llvm/Support/ErrorHandling.h" 34 #include "llvm/Support/MathExtras.h" 35 #include "llvm/Support/raw_ostream.h" 36 #include <algorithm> 37 #include <cassert> 38 #include <climits> 39 #include <cstddef> 40 #include <cstdint> 41 #include <limits> 42 #include <string> 43 #include <tuple> 44 #include <utility> 45 46 using namespace llvm; 47 48 //===----------------------------------------------------------------------===// 49 // Attribute Construction Methods 50 //===----------------------------------------------------------------------===// 51 52 // allocsize has two integer arguments, but because they're both 32 bits, we can 53 // pack them into one 64-bit value, at the cost of making said value 54 // nonsensical. 55 // 56 // In order to do this, we need to reserve one value of the second (optional) 57 // allocsize argument to signify "not present." 58 static const unsigned AllocSizeNumElemsNotPresent = -1; 59 60 static uint64_t packAllocSizeArgs(unsigned ElemSizeArg, 61 const Optional<unsigned> &NumElemsArg) { 62 assert((!NumElemsArg.hasValue() || 63 *NumElemsArg != AllocSizeNumElemsNotPresent) && 64 "Attempting to pack a reserved value"); 65 66 return uint64_t(ElemSizeArg) << 32 | 67 NumElemsArg.getValueOr(AllocSizeNumElemsNotPresent); 68 } 69 70 static std::pair<unsigned, Optional<unsigned>> 71 unpackAllocSizeArgs(uint64_t Num) { 72 unsigned NumElems = Num & std::numeric_limits<unsigned>::max(); 73 unsigned ElemSizeArg = Num >> 32; 74 75 Optional<unsigned> NumElemsArg; 76 if (NumElems != AllocSizeNumElemsNotPresent) 77 NumElemsArg = NumElems; 78 return std::make_pair(ElemSizeArg, NumElemsArg); 79 } 80 81 Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind, 82 uint64_t Val) { 83 LLVMContextImpl *pImpl = Context.pImpl; 84 FoldingSetNodeID ID; 85 ID.AddInteger(Kind); 86 if (Val) ID.AddInteger(Val); 87 88 void *InsertPoint; 89 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint); 90 91 if (!PA) { 92 // If we didn't find any existing attributes of the same shape then create a 93 // new one and insert it. 94 if (!Val) 95 PA = new (pImpl->Alloc) EnumAttributeImpl(Kind); 96 else 97 PA = new (pImpl->Alloc) IntAttributeImpl(Kind, Val); 98 pImpl->AttrsSet.InsertNode(PA, InsertPoint); 99 } 100 101 // Return the Attribute that we found or created. 102 return Attribute(PA); 103 } 104 105 Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) { 106 LLVMContextImpl *pImpl = Context.pImpl; 107 FoldingSetNodeID ID; 108 ID.AddString(Kind); 109 if (!Val.empty()) ID.AddString(Val); 110 111 void *InsertPoint; 112 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint); 113 114 if (!PA) { 115 // If we didn't find any existing attributes of the same shape then create a 116 // new one and insert it. 117 void *Mem = 118 pImpl->Alloc.Allocate(StringAttributeImpl::totalSizeToAlloc(Kind, Val), 119 alignof(StringAttributeImpl)); 120 PA = new (Mem) StringAttributeImpl(Kind, Val); 121 pImpl->AttrsSet.InsertNode(PA, InsertPoint); 122 } 123 124 // Return the Attribute that we found or created. 125 return Attribute(PA); 126 } 127 128 Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind, 129 Type *Ty) { 130 LLVMContextImpl *pImpl = Context.pImpl; 131 FoldingSetNodeID ID; 132 ID.AddInteger(Kind); 133 ID.AddPointer(Ty); 134 135 void *InsertPoint; 136 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint); 137 138 if (!PA) { 139 // If we didn't find any existing attributes of the same shape then create a 140 // new one and insert it. 141 PA = new (pImpl->Alloc) TypeAttributeImpl(Kind, Ty); 142 pImpl->AttrsSet.InsertNode(PA, InsertPoint); 143 } 144 145 // Return the Attribute that we found or created. 146 return Attribute(PA); 147 } 148 149 Attribute Attribute::getWithAlignment(LLVMContext &Context, Align A) { 150 assert(A <= llvm::Value::MaximumAlignment && "Alignment too large."); 151 return get(Context, Alignment, A.value()); 152 } 153 154 Attribute Attribute::getWithStackAlignment(LLVMContext &Context, Align A) { 155 assert(A <= 0x100 && "Alignment too large."); 156 return get(Context, StackAlignment, A.value()); 157 } 158 159 Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context, 160 uint64_t Bytes) { 161 assert(Bytes && "Bytes must be non-zero."); 162 return get(Context, Dereferenceable, Bytes); 163 } 164 165 Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context, 166 uint64_t Bytes) { 167 assert(Bytes && "Bytes must be non-zero."); 168 return get(Context, DereferenceableOrNull, Bytes); 169 } 170 171 Attribute Attribute::getWithByValType(LLVMContext &Context, Type *Ty) { 172 return get(Context, ByVal, Ty); 173 } 174 175 Attribute Attribute::getWithStructRetType(LLVMContext &Context, Type *Ty) { 176 return get(Context, StructRet, Ty); 177 } 178 179 Attribute Attribute::getWithByRefType(LLVMContext &Context, Type *Ty) { 180 return get(Context, ByRef, Ty); 181 } 182 183 Attribute Attribute::getWithPreallocatedType(LLVMContext &Context, Type *Ty) { 184 return get(Context, Preallocated, Ty); 185 } 186 187 Attribute 188 Attribute::getWithAllocSizeArgs(LLVMContext &Context, unsigned ElemSizeArg, 189 const Optional<unsigned> &NumElemsArg) { 190 assert(!(ElemSizeArg == 0 && NumElemsArg && *NumElemsArg == 0) && 191 "Invalid allocsize arguments -- given allocsize(0, 0)"); 192 return get(Context, AllocSize, packAllocSizeArgs(ElemSizeArg, NumElemsArg)); 193 } 194 195 Attribute::AttrKind Attribute::getAttrKindFromName(StringRef AttrName) { 196 return StringSwitch<Attribute::AttrKind>(AttrName) 197 #define GET_ATTR_NAMES 198 #define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) \ 199 .Case(#DISPLAY_NAME, Attribute::ENUM_NAME) 200 #include "llvm/IR/Attributes.inc" 201 .Default(Attribute::None); 202 } 203 204 StringRef Attribute::getNameFromAttrKind(Attribute::AttrKind AttrKind) { 205 switch (AttrKind) { 206 #define GET_ATTR_NAMES 207 #define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) \ 208 case Attribute::ENUM_NAME: \ 209 return #DISPLAY_NAME; 210 #include "llvm/IR/Attributes.inc" 211 case Attribute::None: 212 return "none"; 213 default: 214 llvm_unreachable("invalid Kind"); 215 } 216 } 217 218 bool Attribute::doesAttrKindHaveArgument(Attribute::AttrKind AttrKind) { 219 return AttrKind == Attribute::Alignment || 220 AttrKind == Attribute::StackAlignment || 221 AttrKind == Attribute::Dereferenceable || 222 AttrKind == Attribute::AllocSize || 223 AttrKind == Attribute::DereferenceableOrNull; 224 } 225 226 bool Attribute::isExistingAttribute(StringRef Name) { 227 return StringSwitch<bool>(Name) 228 #define GET_ATTR_NAMES 229 #define ATTRIBUTE_ALL(ENUM_NAME, DISPLAY_NAME) .Case(#DISPLAY_NAME, true) 230 #include "llvm/IR/Attributes.inc" 231 .Default(false); 232 } 233 234 //===----------------------------------------------------------------------===// 235 // Attribute Accessor Methods 236 //===----------------------------------------------------------------------===// 237 238 bool Attribute::isEnumAttribute() const { 239 return pImpl && pImpl->isEnumAttribute(); 240 } 241 242 bool Attribute::isIntAttribute() const { 243 return pImpl && pImpl->isIntAttribute(); 244 } 245 246 bool Attribute::isStringAttribute() const { 247 return pImpl && pImpl->isStringAttribute(); 248 } 249 250 bool Attribute::isTypeAttribute() const { 251 return pImpl && pImpl->isTypeAttribute(); 252 } 253 254 Attribute::AttrKind Attribute::getKindAsEnum() const { 255 if (!pImpl) return None; 256 assert((isEnumAttribute() || isIntAttribute() || isTypeAttribute()) && 257 "Invalid attribute type to get the kind as an enum!"); 258 return pImpl->getKindAsEnum(); 259 } 260 261 uint64_t Attribute::getValueAsInt() const { 262 if (!pImpl) return 0; 263 assert(isIntAttribute() && 264 "Expected the attribute to be an integer attribute!"); 265 return pImpl->getValueAsInt(); 266 } 267 268 StringRef Attribute::getKindAsString() const { 269 if (!pImpl) return {}; 270 assert(isStringAttribute() && 271 "Invalid attribute type to get the kind as a string!"); 272 return pImpl->getKindAsString(); 273 } 274 275 StringRef Attribute::getValueAsString() const { 276 if (!pImpl) return {}; 277 assert(isStringAttribute() && 278 "Invalid attribute type to get the value as a string!"); 279 return pImpl->getValueAsString(); 280 } 281 282 Type *Attribute::getValueAsType() const { 283 if (!pImpl) return {}; 284 assert(isTypeAttribute() && 285 "Invalid attribute type to get the value as a type!"); 286 return pImpl->getValueAsType(); 287 } 288 289 290 bool Attribute::hasAttribute(AttrKind Kind) const { 291 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None); 292 } 293 294 bool Attribute::hasAttribute(StringRef Kind) const { 295 if (!isStringAttribute()) return false; 296 return pImpl && pImpl->hasAttribute(Kind); 297 } 298 299 MaybeAlign Attribute::getAlignment() const { 300 assert(hasAttribute(Attribute::Alignment) && 301 "Trying to get alignment from non-alignment attribute!"); 302 return MaybeAlign(pImpl->getValueAsInt()); 303 } 304 305 MaybeAlign Attribute::getStackAlignment() const { 306 assert(hasAttribute(Attribute::StackAlignment) && 307 "Trying to get alignment from non-alignment attribute!"); 308 return MaybeAlign(pImpl->getValueAsInt()); 309 } 310 311 uint64_t Attribute::getDereferenceableBytes() const { 312 assert(hasAttribute(Attribute::Dereferenceable) && 313 "Trying to get dereferenceable bytes from " 314 "non-dereferenceable attribute!"); 315 return pImpl->getValueAsInt(); 316 } 317 318 uint64_t Attribute::getDereferenceableOrNullBytes() const { 319 assert(hasAttribute(Attribute::DereferenceableOrNull) && 320 "Trying to get dereferenceable bytes from " 321 "non-dereferenceable attribute!"); 322 return pImpl->getValueAsInt(); 323 } 324 325 std::pair<unsigned, Optional<unsigned>> Attribute::getAllocSizeArgs() const { 326 assert(hasAttribute(Attribute::AllocSize) && 327 "Trying to get allocsize args from non-allocsize attribute"); 328 return unpackAllocSizeArgs(pImpl->getValueAsInt()); 329 } 330 331 std::string Attribute::getAsString(bool InAttrGrp) const { 332 if (!pImpl) return {}; 333 334 if (hasAttribute(Attribute::SanitizeAddress)) 335 return "sanitize_address"; 336 if (hasAttribute(Attribute::SanitizeHWAddress)) 337 return "sanitize_hwaddress"; 338 if (hasAttribute(Attribute::SanitizeMemTag)) 339 return "sanitize_memtag"; 340 if (hasAttribute(Attribute::AlwaysInline)) 341 return "alwaysinline"; 342 if (hasAttribute(Attribute::ArgMemOnly)) 343 return "argmemonly"; 344 if (hasAttribute(Attribute::Builtin)) 345 return "builtin"; 346 if (hasAttribute(Attribute::Convergent)) 347 return "convergent"; 348 if (hasAttribute(Attribute::SwiftError)) 349 return "swifterror"; 350 if (hasAttribute(Attribute::SwiftSelf)) 351 return "swiftself"; 352 if (hasAttribute(Attribute::InaccessibleMemOnly)) 353 return "inaccessiblememonly"; 354 if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly)) 355 return "inaccessiblemem_or_argmemonly"; 356 if (hasAttribute(Attribute::InAlloca)) 357 return "inalloca"; 358 if (hasAttribute(Attribute::InlineHint)) 359 return "inlinehint"; 360 if (hasAttribute(Attribute::InReg)) 361 return "inreg"; 362 if (hasAttribute(Attribute::JumpTable)) 363 return "jumptable"; 364 if (hasAttribute(Attribute::MinSize)) 365 return "minsize"; 366 if (hasAttribute(Attribute::Naked)) 367 return "naked"; 368 if (hasAttribute(Attribute::Nest)) 369 return "nest"; 370 if (hasAttribute(Attribute::NoAlias)) 371 return "noalias"; 372 if (hasAttribute(Attribute::NoBuiltin)) 373 return "nobuiltin"; 374 if (hasAttribute(Attribute::NoCapture)) 375 return "nocapture"; 376 if (hasAttribute(Attribute::NoDuplicate)) 377 return "noduplicate"; 378 if (hasAttribute(Attribute::NoFree)) 379 return "nofree"; 380 if (hasAttribute(Attribute::NoImplicitFloat)) 381 return "noimplicitfloat"; 382 if (hasAttribute(Attribute::NoInline)) 383 return "noinline"; 384 if (hasAttribute(Attribute::NonLazyBind)) 385 return "nonlazybind"; 386 if (hasAttribute(Attribute::NoMerge)) 387 return "nomerge"; 388 if (hasAttribute(Attribute::NonNull)) 389 return "nonnull"; 390 if (hasAttribute(Attribute::NoRedZone)) 391 return "noredzone"; 392 if (hasAttribute(Attribute::NoReturn)) 393 return "noreturn"; 394 if (hasAttribute(Attribute::NoSync)) 395 return "nosync"; 396 if (hasAttribute(Attribute::NullPointerIsValid)) 397 return "null_pointer_is_valid"; 398 if (hasAttribute(Attribute::WillReturn)) 399 return "willreturn"; 400 if (hasAttribute(Attribute::NoCfCheck)) 401 return "nocf_check"; 402 if (hasAttribute(Attribute::NoRecurse)) 403 return "norecurse"; 404 if (hasAttribute(Attribute::NoUnwind)) 405 return "nounwind"; 406 if (hasAttribute(Attribute::OptForFuzzing)) 407 return "optforfuzzing"; 408 if (hasAttribute(Attribute::OptimizeNone)) 409 return "optnone"; 410 if (hasAttribute(Attribute::OptimizeForSize)) 411 return "optsize"; 412 if (hasAttribute(Attribute::ReadNone)) 413 return "readnone"; 414 if (hasAttribute(Attribute::ReadOnly)) 415 return "readonly"; 416 if (hasAttribute(Attribute::WriteOnly)) 417 return "writeonly"; 418 if (hasAttribute(Attribute::Returned)) 419 return "returned"; 420 if (hasAttribute(Attribute::ReturnsTwice)) 421 return "returns_twice"; 422 if (hasAttribute(Attribute::SExt)) 423 return "signext"; 424 if (hasAttribute(Attribute::SpeculativeLoadHardening)) 425 return "speculative_load_hardening"; 426 if (hasAttribute(Attribute::Speculatable)) 427 return "speculatable"; 428 if (hasAttribute(Attribute::NoStackProtect)) 429 return "nossp"; 430 if (hasAttribute(Attribute::StackProtect)) 431 return "ssp"; 432 if (hasAttribute(Attribute::StackProtectReq)) 433 return "sspreq"; 434 if (hasAttribute(Attribute::StackProtectStrong)) 435 return "sspstrong"; 436 if (hasAttribute(Attribute::SafeStack)) 437 return "safestack"; 438 if (hasAttribute(Attribute::ShadowCallStack)) 439 return "shadowcallstack"; 440 if (hasAttribute(Attribute::StrictFP)) 441 return "strictfp"; 442 if (hasAttribute(Attribute::SanitizeThread)) 443 return "sanitize_thread"; 444 if (hasAttribute(Attribute::SanitizeMemory)) 445 return "sanitize_memory"; 446 if (hasAttribute(Attribute::UWTable)) 447 return "uwtable"; 448 if (hasAttribute(Attribute::ZExt)) 449 return "zeroext"; 450 if (hasAttribute(Attribute::Cold)) 451 return "cold"; 452 if (hasAttribute(Attribute::ImmArg)) 453 return "immarg"; 454 if (hasAttribute(Attribute::NoUndef)) 455 return "noundef"; 456 if (hasAttribute(Attribute::MustProgress)) 457 return "mustprogress"; 458 459 const bool IsByVal = hasAttribute(Attribute::ByVal); 460 if (IsByVal || hasAttribute(Attribute::StructRet)) { 461 std::string Result; 462 Result += IsByVal ? "byval" : "sret"; 463 if (Type *Ty = getValueAsType()) { 464 raw_string_ostream OS(Result); 465 Result += '('; 466 Ty->print(OS, false, true); 467 OS.flush(); 468 Result += ')'; 469 } 470 return Result; 471 } 472 473 const bool IsByRef = hasAttribute(Attribute::ByRef); 474 if (IsByRef || hasAttribute(Attribute::Preallocated)) { 475 std::string Result = IsByRef ? "byref" : "preallocated"; 476 raw_string_ostream OS(Result); 477 Result += '('; 478 getValueAsType()->print(OS, false, true); 479 OS.flush(); 480 Result += ')'; 481 return Result; 482 } 483 484 // FIXME: These should be output like this: 485 // 486 // align=4 487 // alignstack=8 488 // 489 if (hasAttribute(Attribute::Alignment)) { 490 std::string Result; 491 Result += "align"; 492 Result += (InAttrGrp) ? "=" : " "; 493 Result += utostr(getValueAsInt()); 494 return Result; 495 } 496 497 auto AttrWithBytesToString = [&](const char *Name) { 498 std::string Result; 499 Result += Name; 500 if (InAttrGrp) { 501 Result += "="; 502 Result += utostr(getValueAsInt()); 503 } else { 504 Result += "("; 505 Result += utostr(getValueAsInt()); 506 Result += ")"; 507 } 508 return Result; 509 }; 510 511 if (hasAttribute(Attribute::StackAlignment)) 512 return AttrWithBytesToString("alignstack"); 513 514 if (hasAttribute(Attribute::Dereferenceable)) 515 return AttrWithBytesToString("dereferenceable"); 516 517 if (hasAttribute(Attribute::DereferenceableOrNull)) 518 return AttrWithBytesToString("dereferenceable_or_null"); 519 520 if (hasAttribute(Attribute::AllocSize)) { 521 unsigned ElemSize; 522 Optional<unsigned> NumElems; 523 std::tie(ElemSize, NumElems) = getAllocSizeArgs(); 524 525 std::string Result = "allocsize("; 526 Result += utostr(ElemSize); 527 if (NumElems.hasValue()) { 528 Result += ','; 529 Result += utostr(*NumElems); 530 } 531 Result += ')'; 532 return Result; 533 } 534 535 // Convert target-dependent attributes to strings of the form: 536 // 537 // "kind" 538 // "kind" = "value" 539 // 540 if (isStringAttribute()) { 541 std::string Result; 542 { 543 raw_string_ostream OS(Result); 544 OS << '"' << getKindAsString() << '"'; 545 546 // Since some attribute strings contain special characters that cannot be 547 // printable, those have to be escaped to make the attribute value 548 // printable as is. e.g. "\01__gnu_mcount_nc" 549 const auto &AttrVal = pImpl->getValueAsString(); 550 if (!AttrVal.empty()) { 551 OS << "=\""; 552 printEscapedString(AttrVal, OS); 553 OS << "\""; 554 } 555 } 556 return Result; 557 } 558 559 llvm_unreachable("Unknown attribute"); 560 } 561 562 bool Attribute::operator<(Attribute A) const { 563 if (!pImpl && !A.pImpl) return false; 564 if (!pImpl) return true; 565 if (!A.pImpl) return false; 566 return *pImpl < *A.pImpl; 567 } 568 569 void Attribute::Profile(FoldingSetNodeID &ID) const { 570 ID.AddPointer(pImpl); 571 } 572 573 //===----------------------------------------------------------------------===// 574 // AttributeImpl Definition 575 //===----------------------------------------------------------------------===// 576 577 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const { 578 if (isStringAttribute()) return false; 579 return getKindAsEnum() == A; 580 } 581 582 bool AttributeImpl::hasAttribute(StringRef Kind) const { 583 if (!isStringAttribute()) return false; 584 return getKindAsString() == Kind; 585 } 586 587 Attribute::AttrKind AttributeImpl::getKindAsEnum() const { 588 assert(isEnumAttribute() || isIntAttribute() || isTypeAttribute()); 589 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind(); 590 } 591 592 uint64_t AttributeImpl::getValueAsInt() const { 593 assert(isIntAttribute()); 594 return static_cast<const IntAttributeImpl *>(this)->getValue(); 595 } 596 597 StringRef AttributeImpl::getKindAsString() const { 598 assert(isStringAttribute()); 599 return static_cast<const StringAttributeImpl *>(this)->getStringKind(); 600 } 601 602 StringRef AttributeImpl::getValueAsString() const { 603 assert(isStringAttribute()); 604 return static_cast<const StringAttributeImpl *>(this)->getStringValue(); 605 } 606 607 Type *AttributeImpl::getValueAsType() const { 608 assert(isTypeAttribute()); 609 return static_cast<const TypeAttributeImpl *>(this)->getTypeValue(); 610 } 611 612 bool AttributeImpl::operator<(const AttributeImpl &AI) const { 613 if (this == &AI) 614 return false; 615 // This sorts the attributes with Attribute::AttrKinds coming first (sorted 616 // relative to their enum value) and then strings. 617 if (isEnumAttribute()) { 618 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum(); 619 if (AI.isIntAttribute()) return true; 620 if (AI.isStringAttribute()) return true; 621 if (AI.isTypeAttribute()) return true; 622 } 623 624 if (isTypeAttribute()) { 625 if (AI.isEnumAttribute()) return false; 626 if (AI.isTypeAttribute()) { 627 assert(getKindAsEnum() != AI.getKindAsEnum() && 628 "Comparison of types would be unstable"); 629 return getKindAsEnum() < AI.getKindAsEnum(); 630 } 631 if (AI.isIntAttribute()) return true; 632 if (AI.isStringAttribute()) return true; 633 } 634 635 if (isIntAttribute()) { 636 if (AI.isEnumAttribute()) return false; 637 if (AI.isTypeAttribute()) return false; 638 if (AI.isIntAttribute()) { 639 if (getKindAsEnum() == AI.getKindAsEnum()) 640 return getValueAsInt() < AI.getValueAsInt(); 641 return getKindAsEnum() < AI.getKindAsEnum(); 642 } 643 if (AI.isStringAttribute()) return true; 644 } 645 646 assert(isStringAttribute()); 647 if (AI.isEnumAttribute()) return false; 648 if (AI.isTypeAttribute()) return false; 649 if (AI.isIntAttribute()) return false; 650 if (getKindAsString() == AI.getKindAsString()) 651 return getValueAsString() < AI.getValueAsString(); 652 return getKindAsString() < AI.getKindAsString(); 653 } 654 655 //===----------------------------------------------------------------------===// 656 // AttributeSet Definition 657 //===----------------------------------------------------------------------===// 658 659 AttributeSet AttributeSet::get(LLVMContext &C, const AttrBuilder &B) { 660 return AttributeSet(AttributeSetNode::get(C, B)); 661 } 662 663 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<Attribute> Attrs) { 664 return AttributeSet(AttributeSetNode::get(C, Attrs)); 665 } 666 667 AttributeSet AttributeSet::addAttribute(LLVMContext &C, 668 Attribute::AttrKind Kind) const { 669 if (hasAttribute(Kind)) return *this; 670 AttrBuilder B; 671 B.addAttribute(Kind); 672 return addAttributes(C, AttributeSet::get(C, B)); 673 } 674 675 AttributeSet AttributeSet::addAttribute(LLVMContext &C, StringRef Kind, 676 StringRef Value) const { 677 AttrBuilder B; 678 B.addAttribute(Kind, Value); 679 return addAttributes(C, AttributeSet::get(C, B)); 680 } 681 682 AttributeSet AttributeSet::addAttributes(LLVMContext &C, 683 const AttributeSet AS) const { 684 if (!hasAttributes()) 685 return AS; 686 687 if (!AS.hasAttributes()) 688 return *this; 689 690 AttrBuilder B(AS); 691 for (const auto &I : *this) 692 B.addAttribute(I); 693 694 return get(C, B); 695 } 696 697 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, 698 Attribute::AttrKind Kind) const { 699 if (!hasAttribute(Kind)) return *this; 700 AttrBuilder B(*this); 701 B.removeAttribute(Kind); 702 return get(C, B); 703 } 704 705 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, 706 StringRef Kind) const { 707 if (!hasAttribute(Kind)) return *this; 708 AttrBuilder B(*this); 709 B.removeAttribute(Kind); 710 return get(C, B); 711 } 712 713 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, 714 const AttrBuilder &Attrs) const { 715 AttrBuilder B(*this); 716 B.remove(Attrs); 717 return get(C, B); 718 } 719 720 unsigned AttributeSet::getNumAttributes() const { 721 return SetNode ? SetNode->getNumAttributes() : 0; 722 } 723 724 bool AttributeSet::hasAttribute(Attribute::AttrKind Kind) const { 725 return SetNode ? SetNode->hasAttribute(Kind) : false; 726 } 727 728 bool AttributeSet::hasAttribute(StringRef Kind) const { 729 return SetNode ? SetNode->hasAttribute(Kind) : false; 730 } 731 732 Attribute AttributeSet::getAttribute(Attribute::AttrKind Kind) const { 733 return SetNode ? SetNode->getAttribute(Kind) : Attribute(); 734 } 735 736 Attribute AttributeSet::getAttribute(StringRef Kind) const { 737 return SetNode ? SetNode->getAttribute(Kind) : Attribute(); 738 } 739 740 MaybeAlign AttributeSet::getAlignment() const { 741 return SetNode ? SetNode->getAlignment() : None; 742 } 743 744 MaybeAlign AttributeSet::getStackAlignment() const { 745 return SetNode ? SetNode->getStackAlignment() : None; 746 } 747 748 uint64_t AttributeSet::getDereferenceableBytes() const { 749 return SetNode ? SetNode->getDereferenceableBytes() : 0; 750 } 751 752 uint64_t AttributeSet::getDereferenceableOrNullBytes() const { 753 return SetNode ? SetNode->getDereferenceableOrNullBytes() : 0; 754 } 755 756 Type *AttributeSet::getByRefType() const { 757 return SetNode ? SetNode->getByRefType() : nullptr; 758 } 759 760 Type *AttributeSet::getByValType() const { 761 return SetNode ? SetNode->getByValType() : nullptr; 762 } 763 764 Type *AttributeSet::getStructRetType() const { 765 return SetNode ? SetNode->getStructRetType() : nullptr; 766 } 767 768 Type *AttributeSet::getPreallocatedType() const { 769 return SetNode ? SetNode->getPreallocatedType() : nullptr; 770 } 771 772 std::pair<unsigned, Optional<unsigned>> AttributeSet::getAllocSizeArgs() const { 773 return SetNode ? SetNode->getAllocSizeArgs() 774 : std::pair<unsigned, Optional<unsigned>>(0, 0); 775 } 776 777 std::string AttributeSet::getAsString(bool InAttrGrp) const { 778 return SetNode ? SetNode->getAsString(InAttrGrp) : ""; 779 } 780 781 AttributeSet::iterator AttributeSet::begin() const { 782 return SetNode ? SetNode->begin() : nullptr; 783 } 784 785 AttributeSet::iterator AttributeSet::end() const { 786 return SetNode ? SetNode->end() : nullptr; 787 } 788 789 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 790 LLVM_DUMP_METHOD void AttributeSet::dump() const { 791 dbgs() << "AS =\n"; 792 dbgs() << " { "; 793 dbgs() << getAsString(true) << " }\n"; 794 } 795 #endif 796 797 //===----------------------------------------------------------------------===// 798 // AttributeSetNode Definition 799 //===----------------------------------------------------------------------===// 800 801 AttributeSetNode::AttributeSetNode(ArrayRef<Attribute> Attrs) 802 : NumAttrs(Attrs.size()) { 803 // There's memory after the node where we can store the entries in. 804 llvm::copy(Attrs, getTrailingObjects<Attribute>()); 805 806 for (const auto &I : *this) { 807 if (I.isStringAttribute()) 808 StringAttrs.insert({ I.getKindAsString(), I }); 809 else 810 AvailableAttrs.addAttribute(I.getKindAsEnum()); 811 } 812 } 813 814 AttributeSetNode *AttributeSetNode::get(LLVMContext &C, 815 ArrayRef<Attribute> Attrs) { 816 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end()); 817 llvm::sort(SortedAttrs); 818 return getSorted(C, SortedAttrs); 819 } 820 821 AttributeSetNode *AttributeSetNode::getSorted(LLVMContext &C, 822 ArrayRef<Attribute> SortedAttrs) { 823 if (SortedAttrs.empty()) 824 return nullptr; 825 826 // Build a key to look up the existing attributes. 827 LLVMContextImpl *pImpl = C.pImpl; 828 FoldingSetNodeID ID; 829 830 assert(llvm::is_sorted(SortedAttrs) && "Expected sorted attributes!"); 831 for (const auto &Attr : SortedAttrs) 832 Attr.Profile(ID); 833 834 void *InsertPoint; 835 AttributeSetNode *PA = 836 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint); 837 838 // If we didn't find any existing attributes of the same shape then create a 839 // new one and insert it. 840 if (!PA) { 841 // Coallocate entries after the AttributeSetNode itself. 842 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size())); 843 PA = new (Mem) AttributeSetNode(SortedAttrs); 844 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint); 845 } 846 847 // Return the AttributeSetNode that we found or created. 848 return PA; 849 } 850 851 AttributeSetNode *AttributeSetNode::get(LLVMContext &C, const AttrBuilder &B) { 852 // Add target-independent attributes. 853 SmallVector<Attribute, 8> Attrs; 854 for (Attribute::AttrKind Kind = Attribute::None; 855 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) { 856 if (!B.contains(Kind)) 857 continue; 858 859 Attribute Attr; 860 switch (Kind) { 861 case Attribute::ByVal: 862 Attr = Attribute::getWithByValType(C, B.getByValType()); 863 break; 864 case Attribute::StructRet: 865 Attr = Attribute::getWithStructRetType(C, B.getStructRetType()); 866 break; 867 case Attribute::ByRef: 868 Attr = Attribute::getWithByRefType(C, B.getByRefType()); 869 break; 870 case Attribute::Preallocated: 871 Attr = Attribute::getWithPreallocatedType(C, B.getPreallocatedType()); 872 break; 873 case Attribute::Alignment: 874 assert(B.getAlignment() && "Alignment must be set"); 875 Attr = Attribute::getWithAlignment(C, *B.getAlignment()); 876 break; 877 case Attribute::StackAlignment: 878 assert(B.getStackAlignment() && "StackAlignment must be set"); 879 Attr = Attribute::getWithStackAlignment(C, *B.getStackAlignment()); 880 break; 881 case Attribute::Dereferenceable: 882 Attr = Attribute::getWithDereferenceableBytes( 883 C, B.getDereferenceableBytes()); 884 break; 885 case Attribute::DereferenceableOrNull: 886 Attr = Attribute::getWithDereferenceableOrNullBytes( 887 C, B.getDereferenceableOrNullBytes()); 888 break; 889 case Attribute::AllocSize: { 890 auto A = B.getAllocSizeArgs(); 891 Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second); 892 break; 893 } 894 default: 895 Attr = Attribute::get(C, Kind); 896 } 897 Attrs.push_back(Attr); 898 } 899 900 // Add target-dependent (string) attributes. 901 for (const auto &TDA : B.td_attrs()) 902 Attrs.emplace_back(Attribute::get(C, TDA.first, TDA.second)); 903 904 return getSorted(C, Attrs); 905 } 906 907 bool AttributeSetNode::hasAttribute(StringRef Kind) const { 908 return StringAttrs.count(Kind); 909 } 910 911 Optional<Attribute> 912 AttributeSetNode::findEnumAttribute(Attribute::AttrKind Kind) const { 913 // Do a quick presence check. 914 if (!hasAttribute(Kind)) 915 return None; 916 917 // Attributes in a set are sorted by enum value, followed by string 918 // attributes. Binary search the one we want. 919 const Attribute *I = 920 std::lower_bound(begin(), end() - StringAttrs.size(), Kind, 921 [](Attribute A, Attribute::AttrKind Kind) { 922 return A.getKindAsEnum() < Kind; 923 }); 924 assert(I != end() && I->hasAttribute(Kind) && "Presence check failed?"); 925 return *I; 926 } 927 928 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const { 929 if (auto A = findEnumAttribute(Kind)) 930 return *A; 931 return {}; 932 } 933 934 Attribute AttributeSetNode::getAttribute(StringRef Kind) const { 935 return StringAttrs.lookup(Kind); 936 } 937 938 MaybeAlign AttributeSetNode::getAlignment() const { 939 if (auto A = findEnumAttribute(Attribute::Alignment)) 940 return A->getAlignment(); 941 return None; 942 } 943 944 MaybeAlign AttributeSetNode::getStackAlignment() const { 945 if (auto A = findEnumAttribute(Attribute::StackAlignment)) 946 return A->getStackAlignment(); 947 return None; 948 } 949 950 Type *AttributeSetNode::getByValType() const { 951 if (auto A = findEnumAttribute(Attribute::ByVal)) 952 return A->getValueAsType(); 953 return nullptr; 954 } 955 956 Type *AttributeSetNode::getStructRetType() const { 957 if (auto A = findEnumAttribute(Attribute::StructRet)) 958 return A->getValueAsType(); 959 return nullptr; 960 } 961 962 Type *AttributeSetNode::getByRefType() const { 963 if (auto A = findEnumAttribute(Attribute::ByRef)) 964 return A->getValueAsType(); 965 return nullptr; 966 } 967 968 Type *AttributeSetNode::getPreallocatedType() const { 969 if (auto A = findEnumAttribute(Attribute::Preallocated)) 970 return A->getValueAsType(); 971 return nullptr; 972 } 973 974 uint64_t AttributeSetNode::getDereferenceableBytes() const { 975 if (auto A = findEnumAttribute(Attribute::Dereferenceable)) 976 return A->getDereferenceableBytes(); 977 return 0; 978 } 979 980 uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const { 981 if (auto A = findEnumAttribute(Attribute::DereferenceableOrNull)) 982 return A->getDereferenceableOrNullBytes(); 983 return 0; 984 } 985 986 std::pair<unsigned, Optional<unsigned>> 987 AttributeSetNode::getAllocSizeArgs() const { 988 if (auto A = findEnumAttribute(Attribute::AllocSize)) 989 return A->getAllocSizeArgs(); 990 return std::make_pair(0, 0); 991 } 992 993 std::string AttributeSetNode::getAsString(bool InAttrGrp) const { 994 std::string Str; 995 for (iterator I = begin(), E = end(); I != E; ++I) { 996 if (I != begin()) 997 Str += ' '; 998 Str += I->getAsString(InAttrGrp); 999 } 1000 return Str; 1001 } 1002 1003 //===----------------------------------------------------------------------===// 1004 // AttributeListImpl Definition 1005 //===----------------------------------------------------------------------===// 1006 1007 /// Map from AttributeList index to the internal array index. Adding one happens 1008 /// to work, because -1 wraps around to 0. 1009 static unsigned attrIdxToArrayIdx(unsigned Index) { 1010 return Index + 1; 1011 } 1012 1013 AttributeListImpl::AttributeListImpl(ArrayRef<AttributeSet> Sets) 1014 : NumAttrSets(Sets.size()) { 1015 assert(!Sets.empty() && "pointless AttributeListImpl"); 1016 1017 // There's memory after the node where we can store the entries in. 1018 llvm::copy(Sets, getTrailingObjects<AttributeSet>()); 1019 1020 // Initialize AvailableFunctionAttrs and AvailableSomewhereAttrs 1021 // summary bitsets. 1022 for (const auto &I : Sets[attrIdxToArrayIdx(AttributeList::FunctionIndex)]) 1023 if (!I.isStringAttribute()) 1024 AvailableFunctionAttrs.addAttribute(I.getKindAsEnum()); 1025 1026 for (const auto &Set : Sets) 1027 for (const auto &I : Set) 1028 if (!I.isStringAttribute()) 1029 AvailableSomewhereAttrs.addAttribute(I.getKindAsEnum()); 1030 } 1031 1032 void AttributeListImpl::Profile(FoldingSetNodeID &ID) const { 1033 Profile(ID, makeArrayRef(begin(), end())); 1034 } 1035 1036 void AttributeListImpl::Profile(FoldingSetNodeID &ID, 1037 ArrayRef<AttributeSet> Sets) { 1038 for (const auto &Set : Sets) 1039 ID.AddPointer(Set.SetNode); 1040 } 1041 1042 bool AttributeListImpl::hasAttrSomewhere(Attribute::AttrKind Kind, 1043 unsigned *Index) const { 1044 if (!AvailableSomewhereAttrs.hasAttribute(Kind)) 1045 return false; 1046 1047 if (Index) { 1048 for (unsigned I = 0, E = NumAttrSets; I != E; ++I) { 1049 if (begin()[I].hasAttribute(Kind)) { 1050 *Index = I - 1; 1051 break; 1052 } 1053 } 1054 } 1055 1056 return true; 1057 } 1058 1059 1060 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1061 LLVM_DUMP_METHOD void AttributeListImpl::dump() const { 1062 AttributeList(const_cast<AttributeListImpl *>(this)).dump(); 1063 } 1064 #endif 1065 1066 //===----------------------------------------------------------------------===// 1067 // AttributeList Construction and Mutation Methods 1068 //===----------------------------------------------------------------------===// 1069 1070 AttributeList AttributeList::getImpl(LLVMContext &C, 1071 ArrayRef<AttributeSet> AttrSets) { 1072 assert(!AttrSets.empty() && "pointless AttributeListImpl"); 1073 1074 LLVMContextImpl *pImpl = C.pImpl; 1075 FoldingSetNodeID ID; 1076 AttributeListImpl::Profile(ID, AttrSets); 1077 1078 void *InsertPoint; 1079 AttributeListImpl *PA = 1080 pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint); 1081 1082 // If we didn't find any existing attributes of the same shape then 1083 // create a new one and insert it. 1084 if (!PA) { 1085 // Coallocate entries after the AttributeListImpl itself. 1086 void *Mem = pImpl->Alloc.Allocate( 1087 AttributeListImpl::totalSizeToAlloc<AttributeSet>(AttrSets.size()), 1088 alignof(AttributeListImpl)); 1089 PA = new (Mem) AttributeListImpl(AttrSets); 1090 pImpl->AttrsLists.InsertNode(PA, InsertPoint); 1091 } 1092 1093 // Return the AttributesList that we found or created. 1094 return AttributeList(PA); 1095 } 1096 1097 AttributeList 1098 AttributeList::get(LLVMContext &C, 1099 ArrayRef<std::pair<unsigned, Attribute>> Attrs) { 1100 // If there are no attributes then return a null AttributesList pointer. 1101 if (Attrs.empty()) 1102 return {}; 1103 1104 assert(llvm::is_sorted(Attrs, 1105 [](const std::pair<unsigned, Attribute> &LHS, 1106 const std::pair<unsigned, Attribute> &RHS) { 1107 return LHS.first < RHS.first; 1108 }) && 1109 "Misordered Attributes list!"); 1110 assert(llvm::all_of(Attrs, 1111 [](const std::pair<unsigned, Attribute> &Pair) { 1112 return Pair.second.isValid(); 1113 }) && 1114 "Pointless attribute!"); 1115 1116 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes 1117 // list. 1118 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairVec; 1119 for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(), 1120 E = Attrs.end(); I != E; ) { 1121 unsigned Index = I->first; 1122 SmallVector<Attribute, 4> AttrVec; 1123 while (I != E && I->first == Index) { 1124 AttrVec.push_back(I->second); 1125 ++I; 1126 } 1127 1128 AttrPairVec.emplace_back(Index, AttributeSet::get(C, AttrVec)); 1129 } 1130 1131 return get(C, AttrPairVec); 1132 } 1133 1134 AttributeList 1135 AttributeList::get(LLVMContext &C, 1136 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) { 1137 // If there are no attributes then return a null AttributesList pointer. 1138 if (Attrs.empty()) 1139 return {}; 1140 1141 assert(llvm::is_sorted(Attrs, 1142 [](const std::pair<unsigned, AttributeSet> &LHS, 1143 const std::pair<unsigned, AttributeSet> &RHS) { 1144 return LHS.first < RHS.first; 1145 }) && 1146 "Misordered Attributes list!"); 1147 assert(llvm::none_of(Attrs, 1148 [](const std::pair<unsigned, AttributeSet> &Pair) { 1149 return !Pair.second.hasAttributes(); 1150 }) && 1151 "Pointless attribute!"); 1152 1153 unsigned MaxIndex = Attrs.back().first; 1154 // If the MaxIndex is FunctionIndex and there are other indices in front 1155 // of it, we need to use the largest of those to get the right size. 1156 if (MaxIndex == FunctionIndex && Attrs.size() > 1) 1157 MaxIndex = Attrs[Attrs.size() - 2].first; 1158 1159 SmallVector<AttributeSet, 4> AttrVec(attrIdxToArrayIdx(MaxIndex) + 1); 1160 for (const auto &Pair : Attrs) 1161 AttrVec[attrIdxToArrayIdx(Pair.first)] = Pair.second; 1162 1163 return getImpl(C, AttrVec); 1164 } 1165 1166 AttributeList AttributeList::get(LLVMContext &C, AttributeSet FnAttrs, 1167 AttributeSet RetAttrs, 1168 ArrayRef<AttributeSet> ArgAttrs) { 1169 // Scan from the end to find the last argument with attributes. Most 1170 // arguments don't have attributes, so it's nice if we can have fewer unique 1171 // AttributeListImpls by dropping empty attribute sets at the end of the list. 1172 unsigned NumSets = 0; 1173 for (size_t I = ArgAttrs.size(); I != 0; --I) { 1174 if (ArgAttrs[I - 1].hasAttributes()) { 1175 NumSets = I + 2; 1176 break; 1177 } 1178 } 1179 if (NumSets == 0) { 1180 // Check function and return attributes if we didn't have argument 1181 // attributes. 1182 if (RetAttrs.hasAttributes()) 1183 NumSets = 2; 1184 else if (FnAttrs.hasAttributes()) 1185 NumSets = 1; 1186 } 1187 1188 // If all attribute sets were empty, we can use the empty attribute list. 1189 if (NumSets == 0) 1190 return {}; 1191 1192 SmallVector<AttributeSet, 8> AttrSets; 1193 AttrSets.reserve(NumSets); 1194 // If we have any attributes, we always have function attributes. 1195 AttrSets.push_back(FnAttrs); 1196 if (NumSets > 1) 1197 AttrSets.push_back(RetAttrs); 1198 if (NumSets > 2) { 1199 // Drop the empty argument attribute sets at the end. 1200 ArgAttrs = ArgAttrs.take_front(NumSets - 2); 1201 AttrSets.insert(AttrSets.end(), ArgAttrs.begin(), ArgAttrs.end()); 1202 } 1203 1204 return getImpl(C, AttrSets); 1205 } 1206 1207 AttributeList AttributeList::get(LLVMContext &C, unsigned Index, 1208 const AttrBuilder &B) { 1209 if (!B.hasAttributes()) 1210 return {}; 1211 Index = attrIdxToArrayIdx(Index); 1212 SmallVector<AttributeSet, 8> AttrSets(Index + 1); 1213 AttrSets[Index] = AttributeSet::get(C, B); 1214 return getImpl(C, AttrSets); 1215 } 1216 1217 AttributeList AttributeList::get(LLVMContext &C, unsigned Index, 1218 ArrayRef<Attribute::AttrKind> Kinds) { 1219 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs; 1220 for (const auto K : Kinds) 1221 Attrs.emplace_back(Index, Attribute::get(C, K)); 1222 return get(C, Attrs); 1223 } 1224 1225 AttributeList AttributeList::get(LLVMContext &C, unsigned Index, 1226 ArrayRef<Attribute::AttrKind> Kinds, 1227 ArrayRef<uint64_t> Values) { 1228 assert(Kinds.size() == Values.size() && "Mismatched attribute values."); 1229 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs; 1230 auto VI = Values.begin(); 1231 for (const auto K : Kinds) 1232 Attrs.emplace_back(Index, Attribute::get(C, K, *VI++)); 1233 return get(C, Attrs); 1234 } 1235 1236 AttributeList AttributeList::get(LLVMContext &C, unsigned Index, 1237 ArrayRef<StringRef> Kinds) { 1238 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs; 1239 for (const auto &K : Kinds) 1240 Attrs.emplace_back(Index, Attribute::get(C, K)); 1241 return get(C, Attrs); 1242 } 1243 1244 AttributeList AttributeList::get(LLVMContext &C, 1245 ArrayRef<AttributeList> Attrs) { 1246 if (Attrs.empty()) 1247 return {}; 1248 if (Attrs.size() == 1) 1249 return Attrs[0]; 1250 1251 unsigned MaxSize = 0; 1252 for (const auto &List : Attrs) 1253 MaxSize = std::max(MaxSize, List.getNumAttrSets()); 1254 1255 // If every list was empty, there is no point in merging the lists. 1256 if (MaxSize == 0) 1257 return {}; 1258 1259 SmallVector<AttributeSet, 8> NewAttrSets(MaxSize); 1260 for (unsigned I = 0; I < MaxSize; ++I) { 1261 AttrBuilder CurBuilder; 1262 for (const auto &List : Attrs) 1263 CurBuilder.merge(List.getAttributes(I - 1)); 1264 NewAttrSets[I] = AttributeSet::get(C, CurBuilder); 1265 } 1266 1267 return getImpl(C, NewAttrSets); 1268 } 1269 1270 AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index, 1271 Attribute::AttrKind Kind) const { 1272 if (hasAttribute(Index, Kind)) return *this; 1273 AttrBuilder B; 1274 B.addAttribute(Kind); 1275 return addAttributes(C, Index, B); 1276 } 1277 1278 AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index, 1279 StringRef Kind, 1280 StringRef Value) const { 1281 AttrBuilder B; 1282 B.addAttribute(Kind, Value); 1283 return addAttributes(C, Index, B); 1284 } 1285 1286 AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index, 1287 Attribute A) const { 1288 AttrBuilder B; 1289 B.addAttribute(A); 1290 return addAttributes(C, Index, B); 1291 } 1292 1293 AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index, 1294 const AttrBuilder &B) const { 1295 if (!B.hasAttributes()) 1296 return *this; 1297 1298 if (!pImpl) 1299 return AttributeList::get(C, {{Index, AttributeSet::get(C, B)}}); 1300 1301 #ifndef NDEBUG 1302 // FIXME it is not obvious how this should work for alignment. For now, say 1303 // we can't change a known alignment. 1304 const MaybeAlign OldAlign = getAttributes(Index).getAlignment(); 1305 const MaybeAlign NewAlign = B.getAlignment(); 1306 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) && 1307 "Attempt to change alignment!"); 1308 #endif 1309 1310 Index = attrIdxToArrayIdx(Index); 1311 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end()); 1312 if (Index >= AttrSets.size()) 1313 AttrSets.resize(Index + 1); 1314 1315 AttrBuilder Merged(AttrSets[Index]); 1316 Merged.merge(B); 1317 AttrSets[Index] = AttributeSet::get(C, Merged); 1318 1319 return getImpl(C, AttrSets); 1320 } 1321 1322 AttributeList AttributeList::addParamAttribute(LLVMContext &C, 1323 ArrayRef<unsigned> ArgNos, 1324 Attribute A) const { 1325 assert(llvm::is_sorted(ArgNos)); 1326 1327 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end()); 1328 unsigned MaxIndex = attrIdxToArrayIdx(ArgNos.back() + FirstArgIndex); 1329 if (MaxIndex >= AttrSets.size()) 1330 AttrSets.resize(MaxIndex + 1); 1331 1332 for (unsigned ArgNo : ArgNos) { 1333 unsigned Index = attrIdxToArrayIdx(ArgNo + FirstArgIndex); 1334 AttrBuilder B(AttrSets[Index]); 1335 B.addAttribute(A); 1336 AttrSets[Index] = AttributeSet::get(C, B); 1337 } 1338 1339 return getImpl(C, AttrSets); 1340 } 1341 1342 AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index, 1343 Attribute::AttrKind Kind) const { 1344 if (!hasAttribute(Index, Kind)) return *this; 1345 1346 Index = attrIdxToArrayIdx(Index); 1347 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end()); 1348 assert(Index < AttrSets.size()); 1349 1350 AttrSets[Index] = AttrSets[Index].removeAttribute(C, Kind); 1351 1352 return getImpl(C, AttrSets); 1353 } 1354 1355 AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index, 1356 StringRef Kind) const { 1357 if (!hasAttribute(Index, Kind)) return *this; 1358 1359 Index = attrIdxToArrayIdx(Index); 1360 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end()); 1361 assert(Index < AttrSets.size()); 1362 1363 AttrSets[Index] = AttrSets[Index].removeAttribute(C, Kind); 1364 1365 return getImpl(C, AttrSets); 1366 } 1367 1368 AttributeList 1369 AttributeList::removeAttributes(LLVMContext &C, unsigned Index, 1370 const AttrBuilder &AttrsToRemove) const { 1371 if (!pImpl) 1372 return {}; 1373 1374 Index = attrIdxToArrayIdx(Index); 1375 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end()); 1376 if (Index >= AttrSets.size()) 1377 AttrSets.resize(Index + 1); 1378 1379 AttrSets[Index] = AttrSets[Index].removeAttributes(C, AttrsToRemove); 1380 1381 return getImpl(C, AttrSets); 1382 } 1383 1384 AttributeList AttributeList::removeAttributes(LLVMContext &C, 1385 unsigned WithoutIndex) const { 1386 if (!pImpl) 1387 return {}; 1388 WithoutIndex = attrIdxToArrayIdx(WithoutIndex); 1389 if (WithoutIndex >= getNumAttrSets()) 1390 return *this; 1391 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end()); 1392 AttrSets[WithoutIndex] = AttributeSet(); 1393 return getImpl(C, AttrSets); 1394 } 1395 1396 AttributeList AttributeList::addDereferenceableAttr(LLVMContext &C, 1397 unsigned Index, 1398 uint64_t Bytes) const { 1399 AttrBuilder B; 1400 B.addDereferenceableAttr(Bytes); 1401 return addAttributes(C, Index, B); 1402 } 1403 1404 AttributeList 1405 AttributeList::addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index, 1406 uint64_t Bytes) const { 1407 AttrBuilder B; 1408 B.addDereferenceableOrNullAttr(Bytes); 1409 return addAttributes(C, Index, B); 1410 } 1411 1412 AttributeList 1413 AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index, 1414 unsigned ElemSizeArg, 1415 const Optional<unsigned> &NumElemsArg) { 1416 AttrBuilder B; 1417 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg); 1418 return addAttributes(C, Index, B); 1419 } 1420 1421 //===----------------------------------------------------------------------===// 1422 // AttributeList Accessor Methods 1423 //===----------------------------------------------------------------------===// 1424 1425 AttributeSet AttributeList::getParamAttributes(unsigned ArgNo) const { 1426 return getAttributes(ArgNo + FirstArgIndex); 1427 } 1428 1429 AttributeSet AttributeList::getRetAttributes() const { 1430 return getAttributes(ReturnIndex); 1431 } 1432 1433 AttributeSet AttributeList::getFnAttributes() const { 1434 return getAttributes(FunctionIndex); 1435 } 1436 1437 bool AttributeList::hasAttribute(unsigned Index, 1438 Attribute::AttrKind Kind) const { 1439 return getAttributes(Index).hasAttribute(Kind); 1440 } 1441 1442 bool AttributeList::hasAttribute(unsigned Index, StringRef Kind) const { 1443 return getAttributes(Index).hasAttribute(Kind); 1444 } 1445 1446 bool AttributeList::hasAttributes(unsigned Index) const { 1447 return getAttributes(Index).hasAttributes(); 1448 } 1449 1450 bool AttributeList::hasFnAttribute(Attribute::AttrKind Kind) const { 1451 return pImpl && pImpl->hasFnAttribute(Kind); 1452 } 1453 1454 bool AttributeList::hasFnAttribute(StringRef Kind) const { 1455 return hasAttribute(AttributeList::FunctionIndex, Kind); 1456 } 1457 1458 bool AttributeList::hasParamAttribute(unsigned ArgNo, 1459 Attribute::AttrKind Kind) const { 1460 return hasAttribute(ArgNo + FirstArgIndex, Kind); 1461 } 1462 1463 bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr, 1464 unsigned *Index) const { 1465 return pImpl && pImpl->hasAttrSomewhere(Attr, Index); 1466 } 1467 1468 Attribute AttributeList::getAttribute(unsigned Index, 1469 Attribute::AttrKind Kind) const { 1470 return getAttributes(Index).getAttribute(Kind); 1471 } 1472 1473 Attribute AttributeList::getAttribute(unsigned Index, StringRef Kind) const { 1474 return getAttributes(Index).getAttribute(Kind); 1475 } 1476 1477 MaybeAlign AttributeList::getRetAlignment() const { 1478 return getAttributes(ReturnIndex).getAlignment(); 1479 } 1480 1481 MaybeAlign AttributeList::getParamAlignment(unsigned ArgNo) const { 1482 return getAttributes(ArgNo + FirstArgIndex).getAlignment(); 1483 } 1484 1485 Type *AttributeList::getParamByValType(unsigned Index) const { 1486 return getAttributes(Index+FirstArgIndex).getByValType(); 1487 } 1488 1489 Type *AttributeList::getParamStructRetType(unsigned Index) const { 1490 return getAttributes(Index + FirstArgIndex).getStructRetType(); 1491 } 1492 1493 Type *AttributeList::getParamByRefType(unsigned Index) const { 1494 return getAttributes(Index + FirstArgIndex).getByRefType(); 1495 } 1496 1497 Type *AttributeList::getParamPreallocatedType(unsigned Index) const { 1498 return getAttributes(Index + FirstArgIndex).getPreallocatedType(); 1499 } 1500 1501 MaybeAlign AttributeList::getStackAlignment(unsigned Index) const { 1502 return getAttributes(Index).getStackAlignment(); 1503 } 1504 1505 uint64_t AttributeList::getDereferenceableBytes(unsigned Index) const { 1506 return getAttributes(Index).getDereferenceableBytes(); 1507 } 1508 1509 uint64_t AttributeList::getDereferenceableOrNullBytes(unsigned Index) const { 1510 return getAttributes(Index).getDereferenceableOrNullBytes(); 1511 } 1512 1513 std::pair<unsigned, Optional<unsigned>> 1514 AttributeList::getAllocSizeArgs(unsigned Index) const { 1515 return getAttributes(Index).getAllocSizeArgs(); 1516 } 1517 1518 std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const { 1519 return getAttributes(Index).getAsString(InAttrGrp); 1520 } 1521 1522 AttributeSet AttributeList::getAttributes(unsigned Index) const { 1523 Index = attrIdxToArrayIdx(Index); 1524 if (!pImpl || Index >= getNumAttrSets()) 1525 return {}; 1526 return pImpl->begin()[Index]; 1527 } 1528 1529 AttributeList::iterator AttributeList::begin() const { 1530 return pImpl ? pImpl->begin() : nullptr; 1531 } 1532 1533 AttributeList::iterator AttributeList::end() const { 1534 return pImpl ? pImpl->end() : nullptr; 1535 } 1536 1537 //===----------------------------------------------------------------------===// 1538 // AttributeList Introspection Methods 1539 //===----------------------------------------------------------------------===// 1540 1541 unsigned AttributeList::getNumAttrSets() const { 1542 return pImpl ? pImpl->NumAttrSets : 0; 1543 } 1544 1545 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1546 LLVM_DUMP_METHOD void AttributeList::dump() const { 1547 dbgs() << "PAL[\n"; 1548 1549 for (unsigned i = index_begin(), e = index_end(); i != e; ++i) { 1550 if (getAttributes(i).hasAttributes()) 1551 dbgs() << " { " << i << " => " << getAsString(i) << " }\n"; 1552 } 1553 1554 dbgs() << "]\n"; 1555 } 1556 #endif 1557 1558 //===----------------------------------------------------------------------===// 1559 // AttrBuilder Method Implementations 1560 //===----------------------------------------------------------------------===// 1561 1562 // FIXME: Remove this ctor, use AttributeSet. 1563 AttrBuilder::AttrBuilder(AttributeList AL, unsigned Index) { 1564 AttributeSet AS = AL.getAttributes(Index); 1565 for (const auto &A : AS) 1566 addAttribute(A); 1567 } 1568 1569 AttrBuilder::AttrBuilder(AttributeSet AS) { 1570 for (const auto &A : AS) 1571 addAttribute(A); 1572 } 1573 1574 void AttrBuilder::clear() { 1575 Attrs.reset(); 1576 TargetDepAttrs.clear(); 1577 Alignment.reset(); 1578 StackAlignment.reset(); 1579 DerefBytes = DerefOrNullBytes = 0; 1580 AllocSizeArgs = 0; 1581 ByValType = nullptr; 1582 StructRetType = nullptr; 1583 ByRefType = nullptr; 1584 PreallocatedType = nullptr; 1585 } 1586 1587 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) { 1588 if (Attr.isStringAttribute()) { 1589 addAttribute(Attr.getKindAsString(), Attr.getValueAsString()); 1590 return *this; 1591 } 1592 1593 Attribute::AttrKind Kind = Attr.getKindAsEnum(); 1594 Attrs[Kind] = true; 1595 1596 if (Kind == Attribute::Alignment) 1597 Alignment = Attr.getAlignment(); 1598 else if (Kind == Attribute::StackAlignment) 1599 StackAlignment = Attr.getStackAlignment(); 1600 else if (Kind == Attribute::ByVal) 1601 ByValType = Attr.getValueAsType(); 1602 else if (Kind == Attribute::StructRet) 1603 StructRetType = Attr.getValueAsType(); 1604 else if (Kind == Attribute::ByRef) 1605 ByRefType = Attr.getValueAsType(); 1606 else if (Kind == Attribute::Preallocated) 1607 PreallocatedType = Attr.getValueAsType(); 1608 else if (Kind == Attribute::Dereferenceable) 1609 DerefBytes = Attr.getDereferenceableBytes(); 1610 else if (Kind == Attribute::DereferenceableOrNull) 1611 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes(); 1612 else if (Kind == Attribute::AllocSize) 1613 AllocSizeArgs = Attr.getValueAsInt(); 1614 return *this; 1615 } 1616 1617 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) { 1618 TargetDepAttrs[std::string(A)] = std::string(V); 1619 return *this; 1620 } 1621 1622 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) { 1623 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!"); 1624 Attrs[Val] = false; 1625 1626 if (Val == Attribute::Alignment) 1627 Alignment.reset(); 1628 else if (Val == Attribute::StackAlignment) 1629 StackAlignment.reset(); 1630 else if (Val == Attribute::ByVal) 1631 ByValType = nullptr; 1632 else if (Val == Attribute::StructRet) 1633 StructRetType = nullptr; 1634 else if (Val == Attribute::ByRef) 1635 ByRefType = nullptr; 1636 else if (Val == Attribute::Preallocated) 1637 PreallocatedType = nullptr; 1638 else if (Val == Attribute::Dereferenceable) 1639 DerefBytes = 0; 1640 else if (Val == Attribute::DereferenceableOrNull) 1641 DerefOrNullBytes = 0; 1642 else if (Val == Attribute::AllocSize) 1643 AllocSizeArgs = 0; 1644 1645 return *this; 1646 } 1647 1648 AttrBuilder &AttrBuilder::removeAttributes(AttributeList A, uint64_t Index) { 1649 remove(A.getAttributes(Index)); 1650 return *this; 1651 } 1652 1653 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) { 1654 auto I = TargetDepAttrs.find(A); 1655 if (I != TargetDepAttrs.end()) 1656 TargetDepAttrs.erase(I); 1657 return *this; 1658 } 1659 1660 std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const { 1661 return unpackAllocSizeArgs(AllocSizeArgs); 1662 } 1663 1664 AttrBuilder &AttrBuilder::addAlignmentAttr(MaybeAlign Align) { 1665 if (!Align) 1666 return *this; 1667 1668 assert(*Align <= llvm::Value::MaximumAlignment && "Alignment too large."); 1669 1670 Attrs[Attribute::Alignment] = true; 1671 Alignment = Align; 1672 return *this; 1673 } 1674 1675 AttrBuilder &AttrBuilder::addStackAlignmentAttr(MaybeAlign Align) { 1676 // Default alignment, allow the target to define how to align it. 1677 if (!Align) 1678 return *this; 1679 1680 assert(*Align <= 0x100 && "Alignment too large."); 1681 1682 Attrs[Attribute::StackAlignment] = true; 1683 StackAlignment = Align; 1684 return *this; 1685 } 1686 1687 AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) { 1688 if (Bytes == 0) return *this; 1689 1690 Attrs[Attribute::Dereferenceable] = true; 1691 DerefBytes = Bytes; 1692 return *this; 1693 } 1694 1695 AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) { 1696 if (Bytes == 0) 1697 return *this; 1698 1699 Attrs[Attribute::DereferenceableOrNull] = true; 1700 DerefOrNullBytes = Bytes; 1701 return *this; 1702 } 1703 1704 AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize, 1705 const Optional<unsigned> &NumElems) { 1706 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems)); 1707 } 1708 1709 AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) { 1710 // (0, 0) is our "not present" value, so we need to check for it here. 1711 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)"); 1712 1713 Attrs[Attribute::AllocSize] = true; 1714 // Reuse existing machinery to store this as a single 64-bit integer so we can 1715 // save a few bytes over using a pair<unsigned, Optional<unsigned>>. 1716 AllocSizeArgs = RawArgs; 1717 return *this; 1718 } 1719 1720 AttrBuilder &AttrBuilder::addByValAttr(Type *Ty) { 1721 Attrs[Attribute::ByVal] = true; 1722 ByValType = Ty; 1723 return *this; 1724 } 1725 1726 AttrBuilder &AttrBuilder::addStructRetAttr(Type *Ty) { 1727 Attrs[Attribute::StructRet] = true; 1728 StructRetType = Ty; 1729 return *this; 1730 } 1731 1732 AttrBuilder &AttrBuilder::addByRefAttr(Type *Ty) { 1733 Attrs[Attribute::ByRef] = true; 1734 ByRefType = Ty; 1735 return *this; 1736 } 1737 1738 AttrBuilder &AttrBuilder::addPreallocatedAttr(Type *Ty) { 1739 Attrs[Attribute::Preallocated] = true; 1740 PreallocatedType = Ty; 1741 return *this; 1742 } 1743 1744 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) { 1745 // FIXME: What if both have alignments, but they don't match?! 1746 if (!Alignment) 1747 Alignment = B.Alignment; 1748 1749 if (!StackAlignment) 1750 StackAlignment = B.StackAlignment; 1751 1752 if (!DerefBytes) 1753 DerefBytes = B.DerefBytes; 1754 1755 if (!DerefOrNullBytes) 1756 DerefOrNullBytes = B.DerefOrNullBytes; 1757 1758 if (!AllocSizeArgs) 1759 AllocSizeArgs = B.AllocSizeArgs; 1760 1761 if (!ByValType) 1762 ByValType = B.ByValType; 1763 1764 if (!StructRetType) 1765 StructRetType = B.StructRetType; 1766 1767 if (!ByRefType) 1768 ByRefType = B.ByRefType; 1769 1770 if (!PreallocatedType) 1771 PreallocatedType = B.PreallocatedType; 1772 1773 Attrs |= B.Attrs; 1774 1775 for (const auto &I : B.td_attrs()) 1776 TargetDepAttrs[I.first] = I.second; 1777 1778 return *this; 1779 } 1780 1781 AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) { 1782 // FIXME: What if both have alignments, but they don't match?! 1783 if (B.Alignment) 1784 Alignment.reset(); 1785 1786 if (B.StackAlignment) 1787 StackAlignment.reset(); 1788 1789 if (B.DerefBytes) 1790 DerefBytes = 0; 1791 1792 if (B.DerefOrNullBytes) 1793 DerefOrNullBytes = 0; 1794 1795 if (B.AllocSizeArgs) 1796 AllocSizeArgs = 0; 1797 1798 if (B.ByValType) 1799 ByValType = nullptr; 1800 1801 if (B.StructRetType) 1802 StructRetType = nullptr; 1803 1804 if (B.ByRefType) 1805 ByRefType = nullptr; 1806 1807 if (B.PreallocatedType) 1808 PreallocatedType = nullptr; 1809 1810 Attrs &= ~B.Attrs; 1811 1812 for (const auto &I : B.td_attrs()) 1813 TargetDepAttrs.erase(I.first); 1814 1815 return *this; 1816 } 1817 1818 bool AttrBuilder::overlaps(const AttrBuilder &B) const { 1819 // First check if any of the target independent attributes overlap. 1820 if ((Attrs & B.Attrs).any()) 1821 return true; 1822 1823 // Then check if any target dependent ones do. 1824 for (const auto &I : td_attrs()) 1825 if (B.contains(I.first)) 1826 return true; 1827 1828 return false; 1829 } 1830 1831 bool AttrBuilder::contains(StringRef A) const { 1832 return TargetDepAttrs.find(A) != TargetDepAttrs.end(); 1833 } 1834 1835 bool AttrBuilder::hasAttributes() const { 1836 return !Attrs.none() || !TargetDepAttrs.empty(); 1837 } 1838 1839 bool AttrBuilder::hasAttributes(AttributeList AL, uint64_t Index) const { 1840 AttributeSet AS = AL.getAttributes(Index); 1841 1842 for (const auto &Attr : AS) { 1843 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) { 1844 if (contains(Attr.getKindAsEnum())) 1845 return true; 1846 } else { 1847 assert(Attr.isStringAttribute() && "Invalid attribute kind!"); 1848 return contains(Attr.getKindAsString()); 1849 } 1850 } 1851 1852 return false; 1853 } 1854 1855 bool AttrBuilder::hasAlignmentAttr() const { 1856 return Alignment != 0; 1857 } 1858 1859 bool AttrBuilder::operator==(const AttrBuilder &B) { 1860 if (Attrs != B.Attrs) 1861 return false; 1862 1863 for (td_const_iterator I = TargetDepAttrs.begin(), 1864 E = TargetDepAttrs.end(); I != E; ++I) 1865 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end()) 1866 return false; 1867 1868 return Alignment == B.Alignment && StackAlignment == B.StackAlignment && 1869 DerefBytes == B.DerefBytes && ByValType == B.ByValType && 1870 StructRetType == B.StructRetType && ByRefType == B.ByRefType && 1871 PreallocatedType == B.PreallocatedType; 1872 } 1873 1874 //===----------------------------------------------------------------------===// 1875 // AttributeFuncs Function Defintions 1876 //===----------------------------------------------------------------------===// 1877 1878 /// Which attributes cannot be applied to a type. 1879 AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) { 1880 AttrBuilder Incompatible; 1881 1882 if (!Ty->isIntegerTy()) 1883 // Attribute that only apply to integers. 1884 Incompatible.addAttribute(Attribute::SExt) 1885 .addAttribute(Attribute::ZExt); 1886 1887 if (!Ty->isPointerTy()) 1888 // Attribute that only apply to pointers. 1889 Incompatible.addAttribute(Attribute::Nest) 1890 .addAttribute(Attribute::NoAlias) 1891 .addAttribute(Attribute::NoCapture) 1892 .addAttribute(Attribute::NonNull) 1893 .addAlignmentAttr(1) // the int here is ignored 1894 .addDereferenceableAttr(1) // the int here is ignored 1895 .addDereferenceableOrNullAttr(1) // the int here is ignored 1896 .addAttribute(Attribute::ReadNone) 1897 .addAttribute(Attribute::ReadOnly) 1898 .addAttribute(Attribute::InAlloca) 1899 .addPreallocatedAttr(Ty) 1900 .addByValAttr(Ty) 1901 .addStructRetAttr(Ty) 1902 .addByRefAttr(Ty); 1903 1904 // Some attributes can apply to all "values" but there are no `void` values. 1905 if (Ty->isVoidTy()) 1906 Incompatible.addAttribute(Attribute::NoUndef); 1907 1908 return Incompatible; 1909 } 1910 1911 template<typename AttrClass> 1912 static bool isEqual(const Function &Caller, const Function &Callee) { 1913 return Caller.getFnAttribute(AttrClass::getKind()) == 1914 Callee.getFnAttribute(AttrClass::getKind()); 1915 } 1916 1917 /// Compute the logical AND of the attributes of the caller and the 1918 /// callee. 1919 /// 1920 /// This function sets the caller's attribute to false if the callee's attribute 1921 /// is false. 1922 template<typename AttrClass> 1923 static void setAND(Function &Caller, const Function &Callee) { 1924 if (AttrClass::isSet(Caller, AttrClass::getKind()) && 1925 !AttrClass::isSet(Callee, AttrClass::getKind())) 1926 AttrClass::set(Caller, AttrClass::getKind(), false); 1927 } 1928 1929 /// Compute the logical OR of the attributes of the caller and the 1930 /// callee. 1931 /// 1932 /// This function sets the caller's attribute to true if the callee's attribute 1933 /// is true. 1934 template<typename AttrClass> 1935 static void setOR(Function &Caller, const Function &Callee) { 1936 if (!AttrClass::isSet(Caller, AttrClass::getKind()) && 1937 AttrClass::isSet(Callee, AttrClass::getKind())) 1938 AttrClass::set(Caller, AttrClass::getKind(), true); 1939 } 1940 1941 /// If the inlined function had a higher stack protection level than the 1942 /// calling function, then bump up the caller's stack protection level. 1943 static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) { 1944 assert(!(Callee.hasFnAttribute(Attribute::NoStackProtect) && 1945 (Caller.hasFnAttribute(Attribute::StackProtect) || 1946 Caller.hasFnAttribute(Attribute::StackProtectStrong) || 1947 Caller.hasFnAttribute(Attribute::StackProtectReq))) && 1948 "stack protected caller but callee requested no stack protector"); 1949 assert(!(Caller.hasFnAttribute(Attribute::NoStackProtect) && 1950 (Callee.hasFnAttribute(Attribute::StackProtect) || 1951 Callee.hasFnAttribute(Attribute::StackProtectStrong) || 1952 Callee.hasFnAttribute(Attribute::StackProtectReq))) && 1953 "stack protected callee but caller requested no stack protector"); 1954 // If upgrading the SSP attribute, clear out the old SSP Attributes first. 1955 AttrBuilder OldSSPAttr; 1956 OldSSPAttr.addAttribute(Attribute::StackProtect) 1957 .addAttribute(Attribute::StackProtectStrong) 1958 .addAttribute(Attribute::StackProtectReq); 1959 1960 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) { 1961 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr); 1962 Caller.addFnAttr(Attribute::StackProtectReq); 1963 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) && 1964 !Caller.hasFnAttribute(Attribute::StackProtectReq)) { 1965 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr); 1966 Caller.addFnAttr(Attribute::StackProtectStrong); 1967 } else if (Callee.hasFnAttribute(Attribute::StackProtect) && 1968 !Caller.hasFnAttribute(Attribute::StackProtectReq) && 1969 !Caller.hasFnAttribute(Attribute::StackProtectStrong)) 1970 Caller.addFnAttr(Attribute::StackProtect); 1971 } 1972 1973 /// If the inlined function required stack probes, then ensure that 1974 /// the calling function has those too. 1975 static void adjustCallerStackProbes(Function &Caller, const Function &Callee) { 1976 if (!Caller.hasFnAttribute("probe-stack") && 1977 Callee.hasFnAttribute("probe-stack")) { 1978 Caller.addFnAttr(Callee.getFnAttribute("probe-stack")); 1979 } 1980 } 1981 1982 /// If the inlined function defines the size of guard region 1983 /// on the stack, then ensure that the calling function defines a guard region 1984 /// that is no larger. 1985 static void 1986 adjustCallerStackProbeSize(Function &Caller, const Function &Callee) { 1987 Attribute CalleeAttr = Callee.getFnAttribute("stack-probe-size"); 1988 if (CalleeAttr.isValid()) { 1989 Attribute CallerAttr = Caller.getFnAttribute("stack-probe-size"); 1990 if (CallerAttr.isValid()) { 1991 uint64_t CallerStackProbeSize, CalleeStackProbeSize; 1992 CallerAttr.getValueAsString().getAsInteger(0, CallerStackProbeSize); 1993 CalleeAttr.getValueAsString().getAsInteger(0, CalleeStackProbeSize); 1994 1995 if (CallerStackProbeSize > CalleeStackProbeSize) { 1996 Caller.addFnAttr(CalleeAttr); 1997 } 1998 } else { 1999 Caller.addFnAttr(CalleeAttr); 2000 } 2001 } 2002 } 2003 2004 /// If the inlined function defines a min legal vector width, then ensure 2005 /// the calling function has the same or larger min legal vector width. If the 2006 /// caller has the attribute, but the callee doesn't, we need to remove the 2007 /// attribute from the caller since we can't make any guarantees about the 2008 /// caller's requirements. 2009 /// This function is called after the inlining decision has been made so we have 2010 /// to merge the attribute this way. Heuristics that would use 2011 /// min-legal-vector-width to determine inline compatibility would need to be 2012 /// handled as part of inline cost analysis. 2013 static void 2014 adjustMinLegalVectorWidth(Function &Caller, const Function &Callee) { 2015 Attribute CallerAttr = Caller.getFnAttribute("min-legal-vector-width"); 2016 if (CallerAttr.isValid()) { 2017 Attribute CalleeAttr = Callee.getFnAttribute("min-legal-vector-width"); 2018 if (CalleeAttr.isValid()) { 2019 uint64_t CallerVectorWidth, CalleeVectorWidth; 2020 CallerAttr.getValueAsString().getAsInteger(0, CallerVectorWidth); 2021 CalleeAttr.getValueAsString().getAsInteger(0, CalleeVectorWidth); 2022 if (CallerVectorWidth < CalleeVectorWidth) 2023 Caller.addFnAttr(CalleeAttr); 2024 } else { 2025 // If the callee doesn't have the attribute then we don't know anything 2026 // and must drop the attribute from the caller. 2027 Caller.removeFnAttr("min-legal-vector-width"); 2028 } 2029 } 2030 } 2031 2032 /// If the inlined function has null_pointer_is_valid attribute, 2033 /// set this attribute in the caller post inlining. 2034 static void 2035 adjustNullPointerValidAttr(Function &Caller, const Function &Callee) { 2036 if (Callee.nullPointerIsDefined() && !Caller.nullPointerIsDefined()) { 2037 Caller.addFnAttr(Attribute::NullPointerIsValid); 2038 } 2039 } 2040 2041 struct EnumAttr { 2042 static bool isSet(const Function &Fn, 2043 Attribute::AttrKind Kind) { 2044 return Fn.hasFnAttribute(Kind); 2045 } 2046 2047 static void set(Function &Fn, 2048 Attribute::AttrKind Kind, bool Val) { 2049 if (Val) 2050 Fn.addFnAttr(Kind); 2051 else 2052 Fn.removeFnAttr(Kind); 2053 } 2054 }; 2055 2056 struct StrBoolAttr { 2057 static bool isSet(const Function &Fn, 2058 StringRef Kind) { 2059 auto A = Fn.getFnAttribute(Kind); 2060 return A.getValueAsString().equals("true"); 2061 } 2062 2063 static void set(Function &Fn, 2064 StringRef Kind, bool Val) { 2065 Fn.addFnAttr(Kind, Val ? "true" : "false"); 2066 } 2067 }; 2068 2069 #define GET_ATTR_NAMES 2070 #define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) \ 2071 struct ENUM_NAME##Attr : EnumAttr { \ 2072 static enum Attribute::AttrKind getKind() { \ 2073 return llvm::Attribute::ENUM_NAME; \ 2074 } \ 2075 }; 2076 #define ATTRIBUTE_STRBOOL(ENUM_NAME, DISPLAY_NAME) \ 2077 struct ENUM_NAME##Attr : StrBoolAttr { \ 2078 static StringRef getKind() { return #DISPLAY_NAME; } \ 2079 }; 2080 #include "llvm/IR/Attributes.inc" 2081 2082 #define GET_ATTR_COMPAT_FUNC 2083 #include "llvm/IR/Attributes.inc" 2084 2085 bool AttributeFuncs::areInlineCompatible(const Function &Caller, 2086 const Function &Callee) { 2087 return hasCompatibleFnAttrs(Caller, Callee); 2088 } 2089 2090 void AttributeFuncs::mergeAttributesForInlining(Function &Caller, 2091 const Function &Callee) { 2092 mergeFnAttrs(Caller, Callee); 2093 } 2094