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