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::StackProtect)) 429 return "ssp"; 430 if (hasAttribute(Attribute::StackProtectReq)) 431 return "sspreq"; 432 if (hasAttribute(Attribute::StackProtectStrong)) 433 return "sspstrong"; 434 if (hasAttribute(Attribute::SafeStack)) 435 return "safestack"; 436 if (hasAttribute(Attribute::ShadowCallStack)) 437 return "shadowcallstack"; 438 if (hasAttribute(Attribute::StrictFP)) 439 return "strictfp"; 440 if (hasAttribute(Attribute::SanitizeThread)) 441 return "sanitize_thread"; 442 if (hasAttribute(Attribute::SanitizeMemory)) 443 return "sanitize_memory"; 444 if (hasAttribute(Attribute::UWTable)) 445 return "uwtable"; 446 if (hasAttribute(Attribute::ZExt)) 447 return "zeroext"; 448 if (hasAttribute(Attribute::Cold)) 449 return "cold"; 450 if (hasAttribute(Attribute::ImmArg)) 451 return "immarg"; 452 if (hasAttribute(Attribute::NoUndef)) 453 return "noundef"; 454 455 const bool IsByVal = hasAttribute(Attribute::ByVal); 456 if (IsByVal || hasAttribute(Attribute::StructRet)) { 457 std::string Result; 458 Result += IsByVal ? "byval" : "sret"; 459 if (Type *Ty = getValueAsType()) { 460 raw_string_ostream OS(Result); 461 Result += '('; 462 Ty->print(OS, false, true); 463 OS.flush(); 464 Result += ')'; 465 } 466 return Result; 467 } 468 469 const bool IsByRef = hasAttribute(Attribute::ByRef); 470 if (IsByRef || hasAttribute(Attribute::Preallocated)) { 471 std::string Result = IsByRef ? "byref" : "preallocated"; 472 raw_string_ostream OS(Result); 473 Result += '('; 474 getValueAsType()->print(OS, false, true); 475 OS.flush(); 476 Result += ')'; 477 return Result; 478 } 479 480 // FIXME: These should be output like this: 481 // 482 // align=4 483 // alignstack=8 484 // 485 if (hasAttribute(Attribute::Alignment)) { 486 std::string Result; 487 Result += "align"; 488 Result += (InAttrGrp) ? "=" : " "; 489 Result += utostr(getValueAsInt()); 490 return Result; 491 } 492 493 auto AttrWithBytesToString = [&](const char *Name) { 494 std::string Result; 495 Result += Name; 496 if (InAttrGrp) { 497 Result += "="; 498 Result += utostr(getValueAsInt()); 499 } else { 500 Result += "("; 501 Result += utostr(getValueAsInt()); 502 Result += ")"; 503 } 504 return Result; 505 }; 506 507 if (hasAttribute(Attribute::StackAlignment)) 508 return AttrWithBytesToString("alignstack"); 509 510 if (hasAttribute(Attribute::Dereferenceable)) 511 return AttrWithBytesToString("dereferenceable"); 512 513 if (hasAttribute(Attribute::DereferenceableOrNull)) 514 return AttrWithBytesToString("dereferenceable_or_null"); 515 516 if (hasAttribute(Attribute::AllocSize)) { 517 unsigned ElemSize; 518 Optional<unsigned> NumElems; 519 std::tie(ElemSize, NumElems) = getAllocSizeArgs(); 520 521 std::string Result = "allocsize("; 522 Result += utostr(ElemSize); 523 if (NumElems.hasValue()) { 524 Result += ','; 525 Result += utostr(*NumElems); 526 } 527 Result += ')'; 528 return Result; 529 } 530 531 // Convert target-dependent attributes to strings of the form: 532 // 533 // "kind" 534 // "kind" = "value" 535 // 536 if (isStringAttribute()) { 537 std::string Result; 538 { 539 raw_string_ostream OS(Result); 540 OS << '"' << getKindAsString() << '"'; 541 542 // Since some attribute strings contain special characters that cannot be 543 // printable, those have to be escaped to make the attribute value 544 // printable as is. e.g. "\01__gnu_mcount_nc" 545 const auto &AttrVal = pImpl->getValueAsString(); 546 if (!AttrVal.empty()) { 547 OS << "=\""; 548 printEscapedString(AttrVal, OS); 549 OS << "\""; 550 } 551 } 552 return Result; 553 } 554 555 llvm_unreachable("Unknown attribute"); 556 } 557 558 bool Attribute::operator<(Attribute A) const { 559 if (!pImpl && !A.pImpl) return false; 560 if (!pImpl) return true; 561 if (!A.pImpl) return false; 562 return *pImpl < *A.pImpl; 563 } 564 565 void Attribute::Profile(FoldingSetNodeID &ID) const { 566 ID.AddPointer(pImpl); 567 } 568 569 //===----------------------------------------------------------------------===// 570 // AttributeImpl Definition 571 //===----------------------------------------------------------------------===// 572 573 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const { 574 if (isStringAttribute()) return false; 575 return getKindAsEnum() == A; 576 } 577 578 bool AttributeImpl::hasAttribute(StringRef Kind) const { 579 if (!isStringAttribute()) return false; 580 return getKindAsString() == Kind; 581 } 582 583 Attribute::AttrKind AttributeImpl::getKindAsEnum() const { 584 assert(isEnumAttribute() || isIntAttribute() || isTypeAttribute()); 585 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind(); 586 } 587 588 uint64_t AttributeImpl::getValueAsInt() const { 589 assert(isIntAttribute()); 590 return static_cast<const IntAttributeImpl *>(this)->getValue(); 591 } 592 593 StringRef AttributeImpl::getKindAsString() const { 594 assert(isStringAttribute()); 595 return static_cast<const StringAttributeImpl *>(this)->getStringKind(); 596 } 597 598 StringRef AttributeImpl::getValueAsString() const { 599 assert(isStringAttribute()); 600 return static_cast<const StringAttributeImpl *>(this)->getStringValue(); 601 } 602 603 Type *AttributeImpl::getValueAsType() const { 604 assert(isTypeAttribute()); 605 return static_cast<const TypeAttributeImpl *>(this)->getTypeValue(); 606 } 607 608 bool AttributeImpl::operator<(const AttributeImpl &AI) const { 609 if (this == &AI) 610 return false; 611 // This sorts the attributes with Attribute::AttrKinds coming first (sorted 612 // relative to their enum value) and then strings. 613 if (isEnumAttribute()) { 614 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum(); 615 if (AI.isIntAttribute()) return true; 616 if (AI.isStringAttribute()) return true; 617 if (AI.isTypeAttribute()) return true; 618 } 619 620 if (isTypeAttribute()) { 621 if (AI.isEnumAttribute()) return false; 622 if (AI.isTypeAttribute()) { 623 assert(getKindAsEnum() != AI.getKindAsEnum() && 624 "Comparison of types would be unstable"); 625 return getKindAsEnum() < AI.getKindAsEnum(); 626 } 627 if (AI.isIntAttribute()) return true; 628 if (AI.isStringAttribute()) return true; 629 } 630 631 if (isIntAttribute()) { 632 if (AI.isEnumAttribute()) return false; 633 if (AI.isTypeAttribute()) return false; 634 if (AI.isIntAttribute()) { 635 if (getKindAsEnum() == AI.getKindAsEnum()) 636 return getValueAsInt() < AI.getValueAsInt(); 637 return getKindAsEnum() < AI.getKindAsEnum(); 638 } 639 if (AI.isStringAttribute()) return true; 640 } 641 642 assert(isStringAttribute()); 643 if (AI.isEnumAttribute()) return false; 644 if (AI.isTypeAttribute()) return false; 645 if (AI.isIntAttribute()) return false; 646 if (getKindAsString() == AI.getKindAsString()) 647 return getValueAsString() < AI.getValueAsString(); 648 return getKindAsString() < AI.getKindAsString(); 649 } 650 651 //===----------------------------------------------------------------------===// 652 // AttributeSet Definition 653 //===----------------------------------------------------------------------===// 654 655 AttributeSet AttributeSet::get(LLVMContext &C, const AttrBuilder &B) { 656 return AttributeSet(AttributeSetNode::get(C, B)); 657 } 658 659 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<Attribute> Attrs) { 660 return AttributeSet(AttributeSetNode::get(C, Attrs)); 661 } 662 663 AttributeSet AttributeSet::addAttribute(LLVMContext &C, 664 Attribute::AttrKind Kind) const { 665 if (hasAttribute(Kind)) return *this; 666 AttrBuilder B; 667 B.addAttribute(Kind); 668 return addAttributes(C, AttributeSet::get(C, B)); 669 } 670 671 AttributeSet AttributeSet::addAttribute(LLVMContext &C, StringRef Kind, 672 StringRef Value) const { 673 AttrBuilder B; 674 B.addAttribute(Kind, Value); 675 return addAttributes(C, AttributeSet::get(C, B)); 676 } 677 678 AttributeSet AttributeSet::addAttributes(LLVMContext &C, 679 const AttributeSet AS) const { 680 if (!hasAttributes()) 681 return AS; 682 683 if (!AS.hasAttributes()) 684 return *this; 685 686 AttrBuilder B(AS); 687 for (const auto &I : *this) 688 B.addAttribute(I); 689 690 return get(C, B); 691 } 692 693 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, 694 Attribute::AttrKind Kind) const { 695 if (!hasAttribute(Kind)) return *this; 696 AttrBuilder B(*this); 697 B.removeAttribute(Kind); 698 return get(C, B); 699 } 700 701 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, 702 StringRef Kind) const { 703 if (!hasAttribute(Kind)) return *this; 704 AttrBuilder B(*this); 705 B.removeAttribute(Kind); 706 return get(C, B); 707 } 708 709 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, 710 const AttrBuilder &Attrs) const { 711 AttrBuilder B(*this); 712 B.remove(Attrs); 713 return get(C, B); 714 } 715 716 unsigned AttributeSet::getNumAttributes() const { 717 return SetNode ? SetNode->getNumAttributes() : 0; 718 } 719 720 bool AttributeSet::hasAttribute(Attribute::AttrKind Kind) const { 721 return SetNode ? SetNode->hasAttribute(Kind) : false; 722 } 723 724 bool AttributeSet::hasAttribute(StringRef Kind) const { 725 return SetNode ? SetNode->hasAttribute(Kind) : false; 726 } 727 728 Attribute AttributeSet::getAttribute(Attribute::AttrKind Kind) const { 729 return SetNode ? SetNode->getAttribute(Kind) : Attribute(); 730 } 731 732 Attribute AttributeSet::getAttribute(StringRef Kind) const { 733 return SetNode ? SetNode->getAttribute(Kind) : Attribute(); 734 } 735 736 MaybeAlign AttributeSet::getAlignment() const { 737 return SetNode ? SetNode->getAlignment() : None; 738 } 739 740 MaybeAlign AttributeSet::getStackAlignment() const { 741 return SetNode ? SetNode->getStackAlignment() : None; 742 } 743 744 uint64_t AttributeSet::getDereferenceableBytes() const { 745 return SetNode ? SetNode->getDereferenceableBytes() : 0; 746 } 747 748 uint64_t AttributeSet::getDereferenceableOrNullBytes() const { 749 return SetNode ? SetNode->getDereferenceableOrNullBytes() : 0; 750 } 751 752 Type *AttributeSet::getByRefType() const { 753 return SetNode ? SetNode->getByRefType() : nullptr; 754 } 755 756 Type *AttributeSet::getByValType() const { 757 return SetNode ? SetNode->getByValType() : nullptr; 758 } 759 760 Type *AttributeSet::getStructRetType() const { 761 return SetNode ? SetNode->getStructRetType() : nullptr; 762 } 763 764 Type *AttributeSet::getPreallocatedType() const { 765 return SetNode ? SetNode->getPreallocatedType() : nullptr; 766 } 767 768 std::pair<unsigned, Optional<unsigned>> AttributeSet::getAllocSizeArgs() const { 769 return SetNode ? SetNode->getAllocSizeArgs() 770 : std::pair<unsigned, Optional<unsigned>>(0, 0); 771 } 772 773 std::string AttributeSet::getAsString(bool InAttrGrp) const { 774 return SetNode ? SetNode->getAsString(InAttrGrp) : ""; 775 } 776 777 AttributeSet::iterator AttributeSet::begin() const { 778 return SetNode ? SetNode->begin() : nullptr; 779 } 780 781 AttributeSet::iterator AttributeSet::end() const { 782 return SetNode ? SetNode->end() : nullptr; 783 } 784 785 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 786 LLVM_DUMP_METHOD void AttributeSet::dump() const { 787 dbgs() << "AS =\n"; 788 dbgs() << " { "; 789 dbgs() << getAsString(true) << " }\n"; 790 } 791 #endif 792 793 //===----------------------------------------------------------------------===// 794 // AttributeSetNode Definition 795 //===----------------------------------------------------------------------===// 796 797 AttributeSetNode::AttributeSetNode(ArrayRef<Attribute> Attrs) 798 : NumAttrs(Attrs.size()) { 799 // There's memory after the node where we can store the entries in. 800 llvm::copy(Attrs, getTrailingObjects<Attribute>()); 801 802 for (const auto &I : *this) { 803 if (I.isStringAttribute()) 804 StringAttrs.insert({ I.getKindAsString(), I }); 805 else 806 AvailableAttrs.addAttribute(I.getKindAsEnum()); 807 } 808 } 809 810 AttributeSetNode *AttributeSetNode::get(LLVMContext &C, 811 ArrayRef<Attribute> Attrs) { 812 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end()); 813 llvm::sort(SortedAttrs); 814 return getSorted(C, SortedAttrs); 815 } 816 817 AttributeSetNode *AttributeSetNode::getSorted(LLVMContext &C, 818 ArrayRef<Attribute> SortedAttrs) { 819 if (SortedAttrs.empty()) 820 return nullptr; 821 822 // Build a key to look up the existing attributes. 823 LLVMContextImpl *pImpl = C.pImpl; 824 FoldingSetNodeID ID; 825 826 assert(llvm::is_sorted(SortedAttrs) && "Expected sorted attributes!"); 827 for (const auto &Attr : SortedAttrs) 828 Attr.Profile(ID); 829 830 void *InsertPoint; 831 AttributeSetNode *PA = 832 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint); 833 834 // If we didn't find any existing attributes of the same shape then create a 835 // new one and insert it. 836 if (!PA) { 837 // Coallocate entries after the AttributeSetNode itself. 838 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size())); 839 PA = new (Mem) AttributeSetNode(SortedAttrs); 840 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint); 841 } 842 843 // Return the AttributeSetNode that we found or created. 844 return PA; 845 } 846 847 AttributeSetNode *AttributeSetNode::get(LLVMContext &C, const AttrBuilder &B) { 848 // Add target-independent attributes. 849 SmallVector<Attribute, 8> Attrs; 850 for (Attribute::AttrKind Kind = Attribute::None; 851 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) { 852 if (!B.contains(Kind)) 853 continue; 854 855 Attribute Attr; 856 switch (Kind) { 857 case Attribute::ByVal: 858 Attr = Attribute::getWithByValType(C, B.getByValType()); 859 break; 860 case Attribute::StructRet: 861 Attr = Attribute::getWithStructRetType(C, B.getStructRetType()); 862 break; 863 case Attribute::ByRef: 864 Attr = Attribute::getWithByRefType(C, B.getByRefType()); 865 break; 866 case Attribute::Preallocated: 867 Attr = Attribute::getWithPreallocatedType(C, B.getPreallocatedType()); 868 break; 869 case Attribute::Alignment: 870 assert(B.getAlignment() && "Alignment must be set"); 871 Attr = Attribute::getWithAlignment(C, *B.getAlignment()); 872 break; 873 case Attribute::StackAlignment: 874 assert(B.getStackAlignment() && "StackAlignment must be set"); 875 Attr = Attribute::getWithStackAlignment(C, *B.getStackAlignment()); 876 break; 877 case Attribute::Dereferenceable: 878 Attr = Attribute::getWithDereferenceableBytes( 879 C, B.getDereferenceableBytes()); 880 break; 881 case Attribute::DereferenceableOrNull: 882 Attr = Attribute::getWithDereferenceableOrNullBytes( 883 C, B.getDereferenceableOrNullBytes()); 884 break; 885 case Attribute::AllocSize: { 886 auto A = B.getAllocSizeArgs(); 887 Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second); 888 break; 889 } 890 default: 891 Attr = Attribute::get(C, Kind); 892 } 893 Attrs.push_back(Attr); 894 } 895 896 // Add target-dependent (string) attributes. 897 for (const auto &TDA : B.td_attrs()) 898 Attrs.emplace_back(Attribute::get(C, TDA.first, TDA.second)); 899 900 return getSorted(C, Attrs); 901 } 902 903 bool AttributeSetNode::hasAttribute(StringRef Kind) const { 904 return StringAttrs.count(Kind); 905 } 906 907 Optional<Attribute> 908 AttributeSetNode::findEnumAttribute(Attribute::AttrKind Kind) const { 909 // Do a quick presence check. 910 if (!hasAttribute(Kind)) 911 return None; 912 913 // Attributes in a set are sorted by enum value, followed by string 914 // attributes. Binary search the one we want. 915 const Attribute *I = 916 std::lower_bound(begin(), end() - StringAttrs.size(), Kind, 917 [](Attribute A, Attribute::AttrKind Kind) { 918 return A.getKindAsEnum() < Kind; 919 }); 920 assert(I != end() && I->hasAttribute(Kind) && "Presence check failed?"); 921 return *I; 922 } 923 924 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const { 925 if (auto A = findEnumAttribute(Kind)) 926 return *A; 927 return {}; 928 } 929 930 Attribute AttributeSetNode::getAttribute(StringRef Kind) const { 931 return StringAttrs.lookup(Kind); 932 } 933 934 MaybeAlign AttributeSetNode::getAlignment() const { 935 if (auto A = findEnumAttribute(Attribute::Alignment)) 936 return A->getAlignment(); 937 return None; 938 } 939 940 MaybeAlign AttributeSetNode::getStackAlignment() const { 941 if (auto A = findEnumAttribute(Attribute::StackAlignment)) 942 return A->getStackAlignment(); 943 return None; 944 } 945 946 Type *AttributeSetNode::getByValType() const { 947 if (auto A = findEnumAttribute(Attribute::ByVal)) 948 return A->getValueAsType(); 949 return nullptr; 950 } 951 952 Type *AttributeSetNode::getStructRetType() const { 953 if (auto A = findEnumAttribute(Attribute::StructRet)) 954 return A->getValueAsType(); 955 return nullptr; 956 } 957 958 Type *AttributeSetNode::getByRefType() const { 959 if (auto A = findEnumAttribute(Attribute::ByRef)) 960 return A->getValueAsType(); 961 return nullptr; 962 } 963 964 Type *AttributeSetNode::getPreallocatedType() const { 965 if (auto A = findEnumAttribute(Attribute::Preallocated)) 966 return A->getValueAsType(); 967 return nullptr; 968 } 969 970 uint64_t AttributeSetNode::getDereferenceableBytes() const { 971 if (auto A = findEnumAttribute(Attribute::Dereferenceable)) 972 return A->getDereferenceableBytes(); 973 return 0; 974 } 975 976 uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const { 977 if (auto A = findEnumAttribute(Attribute::DereferenceableOrNull)) 978 return A->getDereferenceableOrNullBytes(); 979 return 0; 980 } 981 982 std::pair<unsigned, Optional<unsigned>> 983 AttributeSetNode::getAllocSizeArgs() const { 984 if (auto A = findEnumAttribute(Attribute::AllocSize)) 985 return A->getAllocSizeArgs(); 986 return std::make_pair(0, 0); 987 } 988 989 std::string AttributeSetNode::getAsString(bool InAttrGrp) const { 990 std::string Str; 991 for (iterator I = begin(), E = end(); I != E; ++I) { 992 if (I != begin()) 993 Str += ' '; 994 Str += I->getAsString(InAttrGrp); 995 } 996 return Str; 997 } 998 999 //===----------------------------------------------------------------------===// 1000 // AttributeListImpl Definition 1001 //===----------------------------------------------------------------------===// 1002 1003 /// Map from AttributeList index to the internal array index. Adding one happens 1004 /// to work, because -1 wraps around to 0. 1005 static unsigned attrIdxToArrayIdx(unsigned Index) { 1006 return Index + 1; 1007 } 1008 1009 AttributeListImpl::AttributeListImpl(ArrayRef<AttributeSet> Sets) 1010 : NumAttrSets(Sets.size()) { 1011 assert(!Sets.empty() && "pointless AttributeListImpl"); 1012 1013 // There's memory after the node where we can store the entries in. 1014 llvm::copy(Sets, getTrailingObjects<AttributeSet>()); 1015 1016 // Initialize AvailableFunctionAttrs and AvailableSomewhereAttrs 1017 // summary bitsets. 1018 for (const auto &I : Sets[attrIdxToArrayIdx(AttributeList::FunctionIndex)]) 1019 if (!I.isStringAttribute()) 1020 AvailableFunctionAttrs.addAttribute(I.getKindAsEnum()); 1021 1022 for (const auto &Set : Sets) 1023 for (const auto &I : Set) 1024 if (!I.isStringAttribute()) 1025 AvailableSomewhereAttrs.addAttribute(I.getKindAsEnum()); 1026 } 1027 1028 void AttributeListImpl::Profile(FoldingSetNodeID &ID) const { 1029 Profile(ID, makeArrayRef(begin(), end())); 1030 } 1031 1032 void AttributeListImpl::Profile(FoldingSetNodeID &ID, 1033 ArrayRef<AttributeSet> Sets) { 1034 for (const auto &Set : Sets) 1035 ID.AddPointer(Set.SetNode); 1036 } 1037 1038 bool AttributeListImpl::hasAttrSomewhere(Attribute::AttrKind Kind, 1039 unsigned *Index) const { 1040 if (!AvailableSomewhereAttrs.hasAttribute(Kind)) 1041 return false; 1042 1043 if (Index) { 1044 for (unsigned I = 0, E = NumAttrSets; I != E; ++I) { 1045 if (begin()[I].hasAttribute(Kind)) { 1046 *Index = I - 1; 1047 break; 1048 } 1049 } 1050 } 1051 1052 return true; 1053 } 1054 1055 1056 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1057 LLVM_DUMP_METHOD void AttributeListImpl::dump() const { 1058 AttributeList(const_cast<AttributeListImpl *>(this)).dump(); 1059 } 1060 #endif 1061 1062 //===----------------------------------------------------------------------===// 1063 // AttributeList Construction and Mutation Methods 1064 //===----------------------------------------------------------------------===// 1065 1066 AttributeList AttributeList::getImpl(LLVMContext &C, 1067 ArrayRef<AttributeSet> AttrSets) { 1068 assert(!AttrSets.empty() && "pointless AttributeListImpl"); 1069 1070 LLVMContextImpl *pImpl = C.pImpl; 1071 FoldingSetNodeID ID; 1072 AttributeListImpl::Profile(ID, AttrSets); 1073 1074 void *InsertPoint; 1075 AttributeListImpl *PA = 1076 pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint); 1077 1078 // If we didn't find any existing attributes of the same shape then 1079 // create a new one and insert it. 1080 if (!PA) { 1081 // Coallocate entries after the AttributeListImpl itself. 1082 void *Mem = pImpl->Alloc.Allocate( 1083 AttributeListImpl::totalSizeToAlloc<AttributeSet>(AttrSets.size()), 1084 alignof(AttributeListImpl)); 1085 PA = new (Mem) AttributeListImpl(AttrSets); 1086 pImpl->AttrsLists.InsertNode(PA, InsertPoint); 1087 } 1088 1089 // Return the AttributesList that we found or created. 1090 return AttributeList(PA); 1091 } 1092 1093 AttributeList 1094 AttributeList::get(LLVMContext &C, 1095 ArrayRef<std::pair<unsigned, Attribute>> Attrs) { 1096 // If there are no attributes then return a null AttributesList pointer. 1097 if (Attrs.empty()) 1098 return {}; 1099 1100 assert(llvm::is_sorted(Attrs, 1101 [](const std::pair<unsigned, Attribute> &LHS, 1102 const std::pair<unsigned, Attribute> &RHS) { 1103 return LHS.first < RHS.first; 1104 }) && 1105 "Misordered Attributes list!"); 1106 assert(llvm::all_of(Attrs, 1107 [](const std::pair<unsigned, Attribute> &Pair) { 1108 return Pair.second.isValid(); 1109 }) && 1110 "Pointless attribute!"); 1111 1112 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes 1113 // list. 1114 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairVec; 1115 for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(), 1116 E = Attrs.end(); I != E; ) { 1117 unsigned Index = I->first; 1118 SmallVector<Attribute, 4> AttrVec; 1119 while (I != E && I->first == Index) { 1120 AttrVec.push_back(I->second); 1121 ++I; 1122 } 1123 1124 AttrPairVec.emplace_back(Index, AttributeSet::get(C, AttrVec)); 1125 } 1126 1127 return get(C, AttrPairVec); 1128 } 1129 1130 AttributeList 1131 AttributeList::get(LLVMContext &C, 1132 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) { 1133 // If there are no attributes then return a null AttributesList pointer. 1134 if (Attrs.empty()) 1135 return {}; 1136 1137 assert(llvm::is_sorted(Attrs, 1138 [](const std::pair<unsigned, AttributeSet> &LHS, 1139 const std::pair<unsigned, AttributeSet> &RHS) { 1140 return LHS.first < RHS.first; 1141 }) && 1142 "Misordered Attributes list!"); 1143 assert(llvm::none_of(Attrs, 1144 [](const std::pair<unsigned, AttributeSet> &Pair) { 1145 return !Pair.second.hasAttributes(); 1146 }) && 1147 "Pointless attribute!"); 1148 1149 unsigned MaxIndex = Attrs.back().first; 1150 // If the MaxIndex is FunctionIndex and there are other indices in front 1151 // of it, we need to use the largest of those to get the right size. 1152 if (MaxIndex == FunctionIndex && Attrs.size() > 1) 1153 MaxIndex = Attrs[Attrs.size() - 2].first; 1154 1155 SmallVector<AttributeSet, 4> AttrVec(attrIdxToArrayIdx(MaxIndex) + 1); 1156 for (const auto &Pair : Attrs) 1157 AttrVec[attrIdxToArrayIdx(Pair.first)] = Pair.second; 1158 1159 return getImpl(C, AttrVec); 1160 } 1161 1162 AttributeList AttributeList::get(LLVMContext &C, AttributeSet FnAttrs, 1163 AttributeSet RetAttrs, 1164 ArrayRef<AttributeSet> ArgAttrs) { 1165 // Scan from the end to find the last argument with attributes. Most 1166 // arguments don't have attributes, so it's nice if we can have fewer unique 1167 // AttributeListImpls by dropping empty attribute sets at the end of the list. 1168 unsigned NumSets = 0; 1169 for (size_t I = ArgAttrs.size(); I != 0; --I) { 1170 if (ArgAttrs[I - 1].hasAttributes()) { 1171 NumSets = I + 2; 1172 break; 1173 } 1174 } 1175 if (NumSets == 0) { 1176 // Check function and return attributes if we didn't have argument 1177 // attributes. 1178 if (RetAttrs.hasAttributes()) 1179 NumSets = 2; 1180 else if (FnAttrs.hasAttributes()) 1181 NumSets = 1; 1182 } 1183 1184 // If all attribute sets were empty, we can use the empty attribute list. 1185 if (NumSets == 0) 1186 return {}; 1187 1188 SmallVector<AttributeSet, 8> AttrSets; 1189 AttrSets.reserve(NumSets); 1190 // If we have any attributes, we always have function attributes. 1191 AttrSets.push_back(FnAttrs); 1192 if (NumSets > 1) 1193 AttrSets.push_back(RetAttrs); 1194 if (NumSets > 2) { 1195 // Drop the empty argument attribute sets at the end. 1196 ArgAttrs = ArgAttrs.take_front(NumSets - 2); 1197 AttrSets.insert(AttrSets.end(), ArgAttrs.begin(), ArgAttrs.end()); 1198 } 1199 1200 return getImpl(C, AttrSets); 1201 } 1202 1203 AttributeList AttributeList::get(LLVMContext &C, unsigned Index, 1204 const AttrBuilder &B) { 1205 if (!B.hasAttributes()) 1206 return {}; 1207 Index = attrIdxToArrayIdx(Index); 1208 SmallVector<AttributeSet, 8> AttrSets(Index + 1); 1209 AttrSets[Index] = AttributeSet::get(C, B); 1210 return getImpl(C, AttrSets); 1211 } 1212 1213 AttributeList AttributeList::get(LLVMContext &C, unsigned Index, 1214 ArrayRef<Attribute::AttrKind> Kinds) { 1215 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs; 1216 for (const auto K : Kinds) 1217 Attrs.emplace_back(Index, Attribute::get(C, K)); 1218 return get(C, Attrs); 1219 } 1220 1221 AttributeList AttributeList::get(LLVMContext &C, unsigned Index, 1222 ArrayRef<Attribute::AttrKind> Kinds, 1223 ArrayRef<uint64_t> Values) { 1224 assert(Kinds.size() == Values.size() && "Mismatched attribute values."); 1225 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs; 1226 auto VI = Values.begin(); 1227 for (const auto K : Kinds) 1228 Attrs.emplace_back(Index, Attribute::get(C, K, *VI++)); 1229 return get(C, Attrs); 1230 } 1231 1232 AttributeList AttributeList::get(LLVMContext &C, unsigned Index, 1233 ArrayRef<StringRef> Kinds) { 1234 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs; 1235 for (const auto &K : Kinds) 1236 Attrs.emplace_back(Index, Attribute::get(C, K)); 1237 return get(C, Attrs); 1238 } 1239 1240 AttributeList AttributeList::get(LLVMContext &C, 1241 ArrayRef<AttributeList> Attrs) { 1242 if (Attrs.empty()) 1243 return {}; 1244 if (Attrs.size() == 1) 1245 return Attrs[0]; 1246 1247 unsigned MaxSize = 0; 1248 for (const auto &List : Attrs) 1249 MaxSize = std::max(MaxSize, List.getNumAttrSets()); 1250 1251 // If every list was empty, there is no point in merging the lists. 1252 if (MaxSize == 0) 1253 return {}; 1254 1255 SmallVector<AttributeSet, 8> NewAttrSets(MaxSize); 1256 for (unsigned I = 0; I < MaxSize; ++I) { 1257 AttrBuilder CurBuilder; 1258 for (const auto &List : Attrs) 1259 CurBuilder.merge(List.getAttributes(I - 1)); 1260 NewAttrSets[I] = AttributeSet::get(C, CurBuilder); 1261 } 1262 1263 return getImpl(C, NewAttrSets); 1264 } 1265 1266 AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index, 1267 Attribute::AttrKind Kind) const { 1268 if (hasAttribute(Index, Kind)) return *this; 1269 AttrBuilder B; 1270 B.addAttribute(Kind); 1271 return addAttributes(C, Index, B); 1272 } 1273 1274 AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index, 1275 StringRef Kind, 1276 StringRef Value) const { 1277 AttrBuilder B; 1278 B.addAttribute(Kind, Value); 1279 return addAttributes(C, Index, B); 1280 } 1281 1282 AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index, 1283 Attribute A) const { 1284 AttrBuilder B; 1285 B.addAttribute(A); 1286 return addAttributes(C, Index, B); 1287 } 1288 1289 AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index, 1290 const AttrBuilder &B) const { 1291 if (!B.hasAttributes()) 1292 return *this; 1293 1294 if (!pImpl) 1295 return AttributeList::get(C, {{Index, AttributeSet::get(C, B)}}); 1296 1297 #ifndef NDEBUG 1298 // FIXME it is not obvious how this should work for alignment. For now, say 1299 // we can't change a known alignment. 1300 const MaybeAlign OldAlign = getAttributes(Index).getAlignment(); 1301 const MaybeAlign NewAlign = B.getAlignment(); 1302 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) && 1303 "Attempt to change alignment!"); 1304 #endif 1305 1306 Index = attrIdxToArrayIdx(Index); 1307 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end()); 1308 if (Index >= AttrSets.size()) 1309 AttrSets.resize(Index + 1); 1310 1311 AttrBuilder Merged(AttrSets[Index]); 1312 Merged.merge(B); 1313 AttrSets[Index] = AttributeSet::get(C, Merged); 1314 1315 return getImpl(C, AttrSets); 1316 } 1317 1318 AttributeList AttributeList::addParamAttribute(LLVMContext &C, 1319 ArrayRef<unsigned> ArgNos, 1320 Attribute A) const { 1321 assert(llvm::is_sorted(ArgNos)); 1322 1323 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end()); 1324 unsigned MaxIndex = attrIdxToArrayIdx(ArgNos.back() + FirstArgIndex); 1325 if (MaxIndex >= AttrSets.size()) 1326 AttrSets.resize(MaxIndex + 1); 1327 1328 for (unsigned ArgNo : ArgNos) { 1329 unsigned Index = attrIdxToArrayIdx(ArgNo + FirstArgIndex); 1330 AttrBuilder B(AttrSets[Index]); 1331 B.addAttribute(A); 1332 AttrSets[Index] = AttributeSet::get(C, B); 1333 } 1334 1335 return getImpl(C, AttrSets); 1336 } 1337 1338 AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index, 1339 Attribute::AttrKind Kind) const { 1340 if (!hasAttribute(Index, Kind)) return *this; 1341 1342 Index = attrIdxToArrayIdx(Index); 1343 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end()); 1344 assert(Index < AttrSets.size()); 1345 1346 AttrSets[Index] = AttrSets[Index].removeAttribute(C, Kind); 1347 1348 return getImpl(C, AttrSets); 1349 } 1350 1351 AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index, 1352 StringRef Kind) const { 1353 if (!hasAttribute(Index, Kind)) return *this; 1354 1355 Index = attrIdxToArrayIdx(Index); 1356 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end()); 1357 assert(Index < AttrSets.size()); 1358 1359 AttrSets[Index] = AttrSets[Index].removeAttribute(C, Kind); 1360 1361 return getImpl(C, AttrSets); 1362 } 1363 1364 AttributeList 1365 AttributeList::removeAttributes(LLVMContext &C, unsigned Index, 1366 const AttrBuilder &AttrsToRemove) const { 1367 if (!pImpl) 1368 return {}; 1369 1370 Index = attrIdxToArrayIdx(Index); 1371 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end()); 1372 if (Index >= AttrSets.size()) 1373 AttrSets.resize(Index + 1); 1374 1375 AttrSets[Index] = AttrSets[Index].removeAttributes(C, AttrsToRemove); 1376 1377 return getImpl(C, AttrSets); 1378 } 1379 1380 AttributeList AttributeList::removeAttributes(LLVMContext &C, 1381 unsigned WithoutIndex) const { 1382 if (!pImpl) 1383 return {}; 1384 WithoutIndex = attrIdxToArrayIdx(WithoutIndex); 1385 if (WithoutIndex >= getNumAttrSets()) 1386 return *this; 1387 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end()); 1388 AttrSets[WithoutIndex] = AttributeSet(); 1389 return getImpl(C, AttrSets); 1390 } 1391 1392 AttributeList AttributeList::addDereferenceableAttr(LLVMContext &C, 1393 unsigned Index, 1394 uint64_t Bytes) const { 1395 AttrBuilder B; 1396 B.addDereferenceableAttr(Bytes); 1397 return addAttributes(C, Index, B); 1398 } 1399 1400 AttributeList 1401 AttributeList::addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index, 1402 uint64_t Bytes) const { 1403 AttrBuilder B; 1404 B.addDereferenceableOrNullAttr(Bytes); 1405 return addAttributes(C, Index, B); 1406 } 1407 1408 AttributeList 1409 AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index, 1410 unsigned ElemSizeArg, 1411 const Optional<unsigned> &NumElemsArg) { 1412 AttrBuilder B; 1413 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg); 1414 return addAttributes(C, Index, B); 1415 } 1416 1417 //===----------------------------------------------------------------------===// 1418 // AttributeList Accessor Methods 1419 //===----------------------------------------------------------------------===// 1420 1421 AttributeSet AttributeList::getParamAttributes(unsigned ArgNo) const { 1422 return getAttributes(ArgNo + FirstArgIndex); 1423 } 1424 1425 AttributeSet AttributeList::getRetAttributes() const { 1426 return getAttributes(ReturnIndex); 1427 } 1428 1429 AttributeSet AttributeList::getFnAttributes() const { 1430 return getAttributes(FunctionIndex); 1431 } 1432 1433 bool AttributeList::hasAttribute(unsigned Index, 1434 Attribute::AttrKind Kind) const { 1435 return getAttributes(Index).hasAttribute(Kind); 1436 } 1437 1438 bool AttributeList::hasAttribute(unsigned Index, StringRef Kind) const { 1439 return getAttributes(Index).hasAttribute(Kind); 1440 } 1441 1442 bool AttributeList::hasAttributes(unsigned Index) const { 1443 return getAttributes(Index).hasAttributes(); 1444 } 1445 1446 bool AttributeList::hasFnAttribute(Attribute::AttrKind Kind) const { 1447 return pImpl && pImpl->hasFnAttribute(Kind); 1448 } 1449 1450 bool AttributeList::hasFnAttribute(StringRef Kind) const { 1451 return hasAttribute(AttributeList::FunctionIndex, Kind); 1452 } 1453 1454 bool AttributeList::hasParamAttribute(unsigned ArgNo, 1455 Attribute::AttrKind Kind) const { 1456 return hasAttribute(ArgNo + FirstArgIndex, Kind); 1457 } 1458 1459 bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr, 1460 unsigned *Index) const { 1461 return pImpl && pImpl->hasAttrSomewhere(Attr, Index); 1462 } 1463 1464 Attribute AttributeList::getAttribute(unsigned Index, 1465 Attribute::AttrKind Kind) const { 1466 return getAttributes(Index).getAttribute(Kind); 1467 } 1468 1469 Attribute AttributeList::getAttribute(unsigned Index, StringRef Kind) const { 1470 return getAttributes(Index).getAttribute(Kind); 1471 } 1472 1473 MaybeAlign AttributeList::getRetAlignment() const { 1474 return getAttributes(ReturnIndex).getAlignment(); 1475 } 1476 1477 MaybeAlign AttributeList::getParamAlignment(unsigned ArgNo) const { 1478 return getAttributes(ArgNo + FirstArgIndex).getAlignment(); 1479 } 1480 1481 Type *AttributeList::getParamByValType(unsigned Index) const { 1482 return getAttributes(Index+FirstArgIndex).getByValType(); 1483 } 1484 1485 Type *AttributeList::getParamStructRetType(unsigned Index) const { 1486 return getAttributes(Index + FirstArgIndex).getStructRetType(); 1487 } 1488 1489 Type *AttributeList::getParamByRefType(unsigned Index) const { 1490 return getAttributes(Index + FirstArgIndex).getByRefType(); 1491 } 1492 1493 Type *AttributeList::getParamPreallocatedType(unsigned Index) const { 1494 return getAttributes(Index + FirstArgIndex).getPreallocatedType(); 1495 } 1496 1497 MaybeAlign AttributeList::getStackAlignment(unsigned Index) const { 1498 return getAttributes(Index).getStackAlignment(); 1499 } 1500 1501 uint64_t AttributeList::getDereferenceableBytes(unsigned Index) const { 1502 return getAttributes(Index).getDereferenceableBytes(); 1503 } 1504 1505 uint64_t AttributeList::getDereferenceableOrNullBytes(unsigned Index) const { 1506 return getAttributes(Index).getDereferenceableOrNullBytes(); 1507 } 1508 1509 std::pair<unsigned, Optional<unsigned>> 1510 AttributeList::getAllocSizeArgs(unsigned Index) const { 1511 return getAttributes(Index).getAllocSizeArgs(); 1512 } 1513 1514 std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const { 1515 return getAttributes(Index).getAsString(InAttrGrp); 1516 } 1517 1518 AttributeSet AttributeList::getAttributes(unsigned Index) const { 1519 Index = attrIdxToArrayIdx(Index); 1520 if (!pImpl || Index >= getNumAttrSets()) 1521 return {}; 1522 return pImpl->begin()[Index]; 1523 } 1524 1525 AttributeList::iterator AttributeList::begin() const { 1526 return pImpl ? pImpl->begin() : nullptr; 1527 } 1528 1529 AttributeList::iterator AttributeList::end() const { 1530 return pImpl ? pImpl->end() : nullptr; 1531 } 1532 1533 //===----------------------------------------------------------------------===// 1534 // AttributeList Introspection Methods 1535 //===----------------------------------------------------------------------===// 1536 1537 unsigned AttributeList::getNumAttrSets() const { 1538 return pImpl ? pImpl->NumAttrSets : 0; 1539 } 1540 1541 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1542 LLVM_DUMP_METHOD void AttributeList::dump() const { 1543 dbgs() << "PAL[\n"; 1544 1545 for (unsigned i = index_begin(), e = index_end(); i != e; ++i) { 1546 if (getAttributes(i).hasAttributes()) 1547 dbgs() << " { " << i << " => " << getAsString(i) << " }\n"; 1548 } 1549 1550 dbgs() << "]\n"; 1551 } 1552 #endif 1553 1554 //===----------------------------------------------------------------------===// 1555 // AttrBuilder Method Implementations 1556 //===----------------------------------------------------------------------===// 1557 1558 // FIXME: Remove this ctor, use AttributeSet. 1559 AttrBuilder::AttrBuilder(AttributeList AL, unsigned Index) { 1560 AttributeSet AS = AL.getAttributes(Index); 1561 for (const auto &A : AS) 1562 addAttribute(A); 1563 } 1564 1565 AttrBuilder::AttrBuilder(AttributeSet AS) { 1566 for (const auto &A : AS) 1567 addAttribute(A); 1568 } 1569 1570 void AttrBuilder::clear() { 1571 Attrs.reset(); 1572 TargetDepAttrs.clear(); 1573 Alignment.reset(); 1574 StackAlignment.reset(); 1575 DerefBytes = DerefOrNullBytes = 0; 1576 AllocSizeArgs = 0; 1577 ByValType = nullptr; 1578 StructRetType = nullptr; 1579 ByRefType = nullptr; 1580 PreallocatedType = nullptr; 1581 } 1582 1583 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) { 1584 if (Attr.isStringAttribute()) { 1585 addAttribute(Attr.getKindAsString(), Attr.getValueAsString()); 1586 return *this; 1587 } 1588 1589 Attribute::AttrKind Kind = Attr.getKindAsEnum(); 1590 Attrs[Kind] = true; 1591 1592 if (Kind == Attribute::Alignment) 1593 Alignment = Attr.getAlignment(); 1594 else if (Kind == Attribute::StackAlignment) 1595 StackAlignment = Attr.getStackAlignment(); 1596 else if (Kind == Attribute::ByVal) 1597 ByValType = Attr.getValueAsType(); 1598 else if (Kind == Attribute::StructRet) 1599 StructRetType = Attr.getValueAsType(); 1600 else if (Kind == Attribute::ByRef) 1601 ByRefType = Attr.getValueAsType(); 1602 else if (Kind == Attribute::Preallocated) 1603 PreallocatedType = Attr.getValueAsType(); 1604 else if (Kind == Attribute::Dereferenceable) 1605 DerefBytes = Attr.getDereferenceableBytes(); 1606 else if (Kind == Attribute::DereferenceableOrNull) 1607 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes(); 1608 else if (Kind == Attribute::AllocSize) 1609 AllocSizeArgs = Attr.getValueAsInt(); 1610 return *this; 1611 } 1612 1613 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) { 1614 TargetDepAttrs[std::string(A)] = std::string(V); 1615 return *this; 1616 } 1617 1618 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) { 1619 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!"); 1620 Attrs[Val] = false; 1621 1622 if (Val == Attribute::Alignment) 1623 Alignment.reset(); 1624 else if (Val == Attribute::StackAlignment) 1625 StackAlignment.reset(); 1626 else if (Val == Attribute::ByVal) 1627 ByValType = nullptr; 1628 else if (Val == Attribute::StructRet) 1629 StructRetType = nullptr; 1630 else if (Val == Attribute::ByRef) 1631 ByRefType = nullptr; 1632 else if (Val == Attribute::Preallocated) 1633 PreallocatedType = nullptr; 1634 else if (Val == Attribute::Dereferenceable) 1635 DerefBytes = 0; 1636 else if (Val == Attribute::DereferenceableOrNull) 1637 DerefOrNullBytes = 0; 1638 else if (Val == Attribute::AllocSize) 1639 AllocSizeArgs = 0; 1640 1641 return *this; 1642 } 1643 1644 AttrBuilder &AttrBuilder::removeAttributes(AttributeList A, uint64_t Index) { 1645 remove(A.getAttributes(Index)); 1646 return *this; 1647 } 1648 1649 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) { 1650 auto I = TargetDepAttrs.find(A); 1651 if (I != TargetDepAttrs.end()) 1652 TargetDepAttrs.erase(I); 1653 return *this; 1654 } 1655 1656 std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const { 1657 return unpackAllocSizeArgs(AllocSizeArgs); 1658 } 1659 1660 AttrBuilder &AttrBuilder::addAlignmentAttr(MaybeAlign Align) { 1661 if (!Align) 1662 return *this; 1663 1664 assert(*Align <= llvm::Value::MaximumAlignment && "Alignment too large."); 1665 1666 Attrs[Attribute::Alignment] = true; 1667 Alignment = Align; 1668 return *this; 1669 } 1670 1671 AttrBuilder &AttrBuilder::addStackAlignmentAttr(MaybeAlign Align) { 1672 // Default alignment, allow the target to define how to align it. 1673 if (!Align) 1674 return *this; 1675 1676 assert(*Align <= 0x100 && "Alignment too large."); 1677 1678 Attrs[Attribute::StackAlignment] = true; 1679 StackAlignment = Align; 1680 return *this; 1681 } 1682 1683 AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) { 1684 if (Bytes == 0) return *this; 1685 1686 Attrs[Attribute::Dereferenceable] = true; 1687 DerefBytes = Bytes; 1688 return *this; 1689 } 1690 1691 AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) { 1692 if (Bytes == 0) 1693 return *this; 1694 1695 Attrs[Attribute::DereferenceableOrNull] = true; 1696 DerefOrNullBytes = Bytes; 1697 return *this; 1698 } 1699 1700 AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize, 1701 const Optional<unsigned> &NumElems) { 1702 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems)); 1703 } 1704 1705 AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) { 1706 // (0, 0) is our "not present" value, so we need to check for it here. 1707 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)"); 1708 1709 Attrs[Attribute::AllocSize] = true; 1710 // Reuse existing machinery to store this as a single 64-bit integer so we can 1711 // save a few bytes over using a pair<unsigned, Optional<unsigned>>. 1712 AllocSizeArgs = RawArgs; 1713 return *this; 1714 } 1715 1716 AttrBuilder &AttrBuilder::addByValAttr(Type *Ty) { 1717 Attrs[Attribute::ByVal] = true; 1718 ByValType = Ty; 1719 return *this; 1720 } 1721 1722 AttrBuilder &AttrBuilder::addStructRetAttr(Type *Ty) { 1723 Attrs[Attribute::StructRet] = true; 1724 StructRetType = Ty; 1725 return *this; 1726 } 1727 1728 AttrBuilder &AttrBuilder::addByRefAttr(Type *Ty) { 1729 Attrs[Attribute::ByRef] = true; 1730 ByRefType = Ty; 1731 return *this; 1732 } 1733 1734 AttrBuilder &AttrBuilder::addPreallocatedAttr(Type *Ty) { 1735 Attrs[Attribute::Preallocated] = true; 1736 PreallocatedType = Ty; 1737 return *this; 1738 } 1739 1740 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) { 1741 // FIXME: What if both have alignments, but they don't match?! 1742 if (!Alignment) 1743 Alignment = B.Alignment; 1744 1745 if (!StackAlignment) 1746 StackAlignment = B.StackAlignment; 1747 1748 if (!DerefBytes) 1749 DerefBytes = B.DerefBytes; 1750 1751 if (!DerefOrNullBytes) 1752 DerefOrNullBytes = B.DerefOrNullBytes; 1753 1754 if (!AllocSizeArgs) 1755 AllocSizeArgs = B.AllocSizeArgs; 1756 1757 if (!ByValType) 1758 ByValType = B.ByValType; 1759 1760 if (!StructRetType) 1761 StructRetType = B.StructRetType; 1762 1763 if (!ByRefType) 1764 ByRefType = B.ByRefType; 1765 1766 if (!PreallocatedType) 1767 PreallocatedType = B.PreallocatedType; 1768 1769 Attrs |= B.Attrs; 1770 1771 for (const auto &I : B.td_attrs()) 1772 TargetDepAttrs[I.first] = I.second; 1773 1774 return *this; 1775 } 1776 1777 AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) { 1778 // FIXME: What if both have alignments, but they don't match?! 1779 if (B.Alignment) 1780 Alignment.reset(); 1781 1782 if (B.StackAlignment) 1783 StackAlignment.reset(); 1784 1785 if (B.DerefBytes) 1786 DerefBytes = 0; 1787 1788 if (B.DerefOrNullBytes) 1789 DerefOrNullBytes = 0; 1790 1791 if (B.AllocSizeArgs) 1792 AllocSizeArgs = 0; 1793 1794 if (B.ByValType) 1795 ByValType = nullptr; 1796 1797 if (B.StructRetType) 1798 StructRetType = nullptr; 1799 1800 if (B.ByRefType) 1801 ByRefType = nullptr; 1802 1803 if (B.PreallocatedType) 1804 PreallocatedType = nullptr; 1805 1806 Attrs &= ~B.Attrs; 1807 1808 for (const auto &I : B.td_attrs()) 1809 TargetDepAttrs.erase(I.first); 1810 1811 return *this; 1812 } 1813 1814 bool AttrBuilder::overlaps(const AttrBuilder &B) const { 1815 // First check if any of the target independent attributes overlap. 1816 if ((Attrs & B.Attrs).any()) 1817 return true; 1818 1819 // Then check if any target dependent ones do. 1820 for (const auto &I : td_attrs()) 1821 if (B.contains(I.first)) 1822 return true; 1823 1824 return false; 1825 } 1826 1827 bool AttrBuilder::contains(StringRef A) const { 1828 return TargetDepAttrs.find(A) != TargetDepAttrs.end(); 1829 } 1830 1831 bool AttrBuilder::hasAttributes() const { 1832 return !Attrs.none() || !TargetDepAttrs.empty(); 1833 } 1834 1835 bool AttrBuilder::hasAttributes(AttributeList AL, uint64_t Index) const { 1836 AttributeSet AS = AL.getAttributes(Index); 1837 1838 for (const auto &Attr : AS) { 1839 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) { 1840 if (contains(Attr.getKindAsEnum())) 1841 return true; 1842 } else { 1843 assert(Attr.isStringAttribute() && "Invalid attribute kind!"); 1844 return contains(Attr.getKindAsString()); 1845 } 1846 } 1847 1848 return false; 1849 } 1850 1851 bool AttrBuilder::hasAlignmentAttr() const { 1852 return Alignment != 0; 1853 } 1854 1855 bool AttrBuilder::operator==(const AttrBuilder &B) { 1856 if (Attrs != B.Attrs) 1857 return false; 1858 1859 for (td_const_iterator I = TargetDepAttrs.begin(), 1860 E = TargetDepAttrs.end(); I != E; ++I) 1861 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end()) 1862 return false; 1863 1864 return Alignment == B.Alignment && StackAlignment == B.StackAlignment && 1865 DerefBytes == B.DerefBytes && ByValType == B.ByValType && 1866 StructRetType == B.StructRetType && ByRefType == B.ByRefType && 1867 PreallocatedType == B.PreallocatedType; 1868 } 1869 1870 //===----------------------------------------------------------------------===// 1871 // AttributeFuncs Function Defintions 1872 //===----------------------------------------------------------------------===// 1873 1874 /// Which attributes cannot be applied to a type. 1875 AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) { 1876 AttrBuilder Incompatible; 1877 1878 if (!Ty->isIntegerTy()) 1879 // Attribute that only apply to integers. 1880 Incompatible.addAttribute(Attribute::SExt) 1881 .addAttribute(Attribute::ZExt); 1882 1883 if (!Ty->isPointerTy()) 1884 // Attribute that only apply to pointers. 1885 Incompatible.addAttribute(Attribute::Nest) 1886 .addAttribute(Attribute::NoAlias) 1887 .addAttribute(Attribute::NoCapture) 1888 .addAttribute(Attribute::NonNull) 1889 .addAlignmentAttr(1) // the int here is ignored 1890 .addDereferenceableAttr(1) // the int here is ignored 1891 .addDereferenceableOrNullAttr(1) // the int here is ignored 1892 .addAttribute(Attribute::ReadNone) 1893 .addAttribute(Attribute::ReadOnly) 1894 .addAttribute(Attribute::InAlloca) 1895 .addPreallocatedAttr(Ty) 1896 .addByValAttr(Ty) 1897 .addStructRetAttr(Ty) 1898 .addByRefAttr(Ty); 1899 1900 // Some attributes can apply to all "values" but there are no `void` values. 1901 if (Ty->isVoidTy()) 1902 Incompatible.addAttribute(Attribute::NoUndef); 1903 1904 return Incompatible; 1905 } 1906 1907 template<typename AttrClass> 1908 static bool isEqual(const Function &Caller, const Function &Callee) { 1909 return Caller.getFnAttribute(AttrClass::getKind()) == 1910 Callee.getFnAttribute(AttrClass::getKind()); 1911 } 1912 1913 /// Compute the logical AND of the attributes of the caller and the 1914 /// callee. 1915 /// 1916 /// This function sets the caller's attribute to false if the callee's attribute 1917 /// is false. 1918 template<typename AttrClass> 1919 static void setAND(Function &Caller, const Function &Callee) { 1920 if (AttrClass::isSet(Caller, AttrClass::getKind()) && 1921 !AttrClass::isSet(Callee, AttrClass::getKind())) 1922 AttrClass::set(Caller, AttrClass::getKind(), false); 1923 } 1924 1925 /// Compute the logical OR of the attributes of the caller and the 1926 /// callee. 1927 /// 1928 /// This function sets the caller's attribute to true if the callee's attribute 1929 /// is true. 1930 template<typename AttrClass> 1931 static void setOR(Function &Caller, const Function &Callee) { 1932 if (!AttrClass::isSet(Caller, AttrClass::getKind()) && 1933 AttrClass::isSet(Callee, AttrClass::getKind())) 1934 AttrClass::set(Caller, AttrClass::getKind(), true); 1935 } 1936 1937 /// If the inlined function had a higher stack protection level than the 1938 /// calling function, then bump up the caller's stack protection level. 1939 static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) { 1940 // If upgrading the SSP attribute, clear out the old SSP Attributes first. 1941 // Having multiple SSP attributes doesn't actually hurt, but it adds useless 1942 // clutter to the IR. 1943 AttrBuilder OldSSPAttr; 1944 OldSSPAttr.addAttribute(Attribute::StackProtect) 1945 .addAttribute(Attribute::StackProtectStrong) 1946 .addAttribute(Attribute::StackProtectReq); 1947 1948 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) { 1949 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr); 1950 Caller.addFnAttr(Attribute::StackProtectReq); 1951 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) && 1952 !Caller.hasFnAttribute(Attribute::StackProtectReq)) { 1953 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr); 1954 Caller.addFnAttr(Attribute::StackProtectStrong); 1955 } else if (Callee.hasFnAttribute(Attribute::StackProtect) && 1956 !Caller.hasFnAttribute(Attribute::StackProtectReq) && 1957 !Caller.hasFnAttribute(Attribute::StackProtectStrong)) 1958 Caller.addFnAttr(Attribute::StackProtect); 1959 } 1960 1961 /// If the inlined function required stack probes, then ensure that 1962 /// the calling function has those too. 1963 static void adjustCallerStackProbes(Function &Caller, const Function &Callee) { 1964 if (!Caller.hasFnAttribute("probe-stack") && 1965 Callee.hasFnAttribute("probe-stack")) { 1966 Caller.addFnAttr(Callee.getFnAttribute("probe-stack")); 1967 } 1968 } 1969 1970 /// If the inlined function defines the size of guard region 1971 /// on the stack, then ensure that the calling function defines a guard region 1972 /// that is no larger. 1973 static void 1974 adjustCallerStackProbeSize(Function &Caller, const Function &Callee) { 1975 Attribute CalleeAttr = Callee.getFnAttribute("stack-probe-size"); 1976 if (CalleeAttr.isValid()) { 1977 Attribute CallerAttr = Caller.getFnAttribute("stack-probe-size"); 1978 if (CallerAttr.isValid()) { 1979 uint64_t CallerStackProbeSize, CalleeStackProbeSize; 1980 CallerAttr.getValueAsString().getAsInteger(0, CallerStackProbeSize); 1981 CalleeAttr.getValueAsString().getAsInteger(0, CalleeStackProbeSize); 1982 1983 if (CallerStackProbeSize > CalleeStackProbeSize) { 1984 Caller.addFnAttr(CalleeAttr); 1985 } 1986 } else { 1987 Caller.addFnAttr(CalleeAttr); 1988 } 1989 } 1990 } 1991 1992 /// If the inlined function defines a min legal vector width, then ensure 1993 /// the calling function has the same or larger min legal vector width. If the 1994 /// caller has the attribute, but the callee doesn't, we need to remove the 1995 /// attribute from the caller since we can't make any guarantees about the 1996 /// caller's requirements. 1997 /// This function is called after the inlining decision has been made so we have 1998 /// to merge the attribute this way. Heuristics that would use 1999 /// min-legal-vector-width to determine inline compatibility would need to be 2000 /// handled as part of inline cost analysis. 2001 static void 2002 adjustMinLegalVectorWidth(Function &Caller, const Function &Callee) { 2003 Attribute CallerAttr = Caller.getFnAttribute("min-legal-vector-width"); 2004 if (CallerAttr.isValid()) { 2005 Attribute CalleeAttr = Callee.getFnAttribute("min-legal-vector-width"); 2006 if (CalleeAttr.isValid()) { 2007 uint64_t CallerVectorWidth, CalleeVectorWidth; 2008 CallerAttr.getValueAsString().getAsInteger(0, CallerVectorWidth); 2009 CalleeAttr.getValueAsString().getAsInteger(0, CalleeVectorWidth); 2010 if (CallerVectorWidth < CalleeVectorWidth) 2011 Caller.addFnAttr(CalleeAttr); 2012 } else { 2013 // If the callee doesn't have the attribute then we don't know anything 2014 // and must drop the attribute from the caller. 2015 Caller.removeFnAttr("min-legal-vector-width"); 2016 } 2017 } 2018 } 2019 2020 /// If the inlined function has null_pointer_is_valid attribute, 2021 /// set this attribute in the caller post inlining. 2022 static void 2023 adjustNullPointerValidAttr(Function &Caller, const Function &Callee) { 2024 if (Callee.nullPointerIsDefined() && !Caller.nullPointerIsDefined()) { 2025 Caller.addFnAttr(Attribute::NullPointerIsValid); 2026 } 2027 } 2028 2029 struct EnumAttr { 2030 static bool isSet(const Function &Fn, 2031 Attribute::AttrKind Kind) { 2032 return Fn.hasFnAttribute(Kind); 2033 } 2034 2035 static void set(Function &Fn, 2036 Attribute::AttrKind Kind, bool Val) { 2037 if (Val) 2038 Fn.addFnAttr(Kind); 2039 else 2040 Fn.removeFnAttr(Kind); 2041 } 2042 }; 2043 2044 struct StrBoolAttr { 2045 static bool isSet(const Function &Fn, 2046 StringRef Kind) { 2047 auto A = Fn.getFnAttribute(Kind); 2048 return A.getValueAsString().equals("true"); 2049 } 2050 2051 static void set(Function &Fn, 2052 StringRef Kind, bool Val) { 2053 Fn.addFnAttr(Kind, Val ? "true" : "false"); 2054 } 2055 }; 2056 2057 #define GET_ATTR_NAMES 2058 #define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) \ 2059 struct ENUM_NAME##Attr : EnumAttr { \ 2060 static enum Attribute::AttrKind getKind() { \ 2061 return llvm::Attribute::ENUM_NAME; \ 2062 } \ 2063 }; 2064 #define ATTRIBUTE_STRBOOL(ENUM_NAME, DISPLAY_NAME) \ 2065 struct ENUM_NAME##Attr : StrBoolAttr { \ 2066 static StringRef getKind() { return #DISPLAY_NAME; } \ 2067 }; 2068 #include "llvm/IR/Attributes.inc" 2069 2070 #define GET_ATTR_COMPAT_FUNC 2071 #include "llvm/IR/Attributes.inc" 2072 2073 bool AttributeFuncs::areInlineCompatible(const Function &Caller, 2074 const Function &Callee) { 2075 return hasCompatibleFnAttrs(Caller, Callee); 2076 } 2077 2078 void AttributeFuncs::mergeAttributesForInlining(Function &Caller, 2079 const Function &Callee) { 2080 mergeFnAttrs(Caller, Callee); 2081 } 2082