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