1 //===-- Attributes.cpp - Implement AttributesList -------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // \file 11 // \brief This file implements the Attribute, AttributeImpl, AttrBuilder, 12 // AttributeSetImpl, and AttributeSet classes. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "llvm/IR/Attributes.h" 17 #include "llvm/IR/Function.h" 18 #include "AttributeImpl.h" 19 #include "LLVMContextImpl.h" 20 #include "llvm/ADT/STLExtras.h" 21 #include "llvm/ADT/StringExtras.h" 22 #include "llvm/IR/Type.h" 23 #include "llvm/Support/Atomic.h" 24 #include "llvm/Support/Debug.h" 25 #include "llvm/Support/ManagedStatic.h" 26 #include "llvm/Support/Mutex.h" 27 #include "llvm/Support/raw_ostream.h" 28 #include <algorithm> 29 using namespace llvm; 30 31 //===----------------------------------------------------------------------===// 32 // Attribute Construction Methods 33 //===----------------------------------------------------------------------===// 34 35 Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind, 36 uint64_t Val) { 37 LLVMContextImpl *pImpl = Context.pImpl; 38 FoldingSetNodeID ID; 39 ID.AddInteger(Kind); 40 if (Val) ID.AddInteger(Val); 41 42 void *InsertPoint; 43 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint); 44 45 if (!PA) { 46 // If we didn't find any existing attributes of the same shape then create a 47 // new one and insert it. 48 if (!Val) 49 PA = new EnumAttributeImpl(Kind); 50 else 51 PA = new IntAttributeImpl(Kind, Val); 52 pImpl->AttrsSet.InsertNode(PA, InsertPoint); 53 } 54 55 // Return the Attribute that we found or created. 56 return Attribute(PA); 57 } 58 59 Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) { 60 LLVMContextImpl *pImpl = Context.pImpl; 61 FoldingSetNodeID ID; 62 ID.AddString(Kind); 63 if (!Val.empty()) ID.AddString(Val); 64 65 void *InsertPoint; 66 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint); 67 68 if (!PA) { 69 // If we didn't find any existing attributes of the same shape then create a 70 // new one and insert it. 71 PA = new StringAttributeImpl(Kind, Val); 72 pImpl->AttrsSet.InsertNode(PA, InsertPoint); 73 } 74 75 // Return the Attribute that we found or created. 76 return Attribute(PA); 77 } 78 79 Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) { 80 assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); 81 assert(Align <= 0x40000000 && "Alignment too large."); 82 return get(Context, Alignment, Align); 83 } 84 85 Attribute Attribute::getWithStackAlignment(LLVMContext &Context, 86 uint64_t Align) { 87 assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); 88 assert(Align <= 0x100 && "Alignment too large."); 89 return get(Context, StackAlignment, Align); 90 } 91 92 Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context, 93 uint64_t Bytes) { 94 assert(Bytes && "Bytes must be non-zero."); 95 return get(Context, Dereferenceable, Bytes); 96 } 97 98 Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context, 99 uint64_t Bytes) { 100 assert(Bytes && "Bytes must be non-zero."); 101 return get(Context, DereferenceableOrNull, Bytes); 102 } 103 104 //===----------------------------------------------------------------------===// 105 // Attribute Accessor Methods 106 //===----------------------------------------------------------------------===// 107 108 bool Attribute::isEnumAttribute() const { 109 return pImpl && pImpl->isEnumAttribute(); 110 } 111 112 bool Attribute::isIntAttribute() const { 113 return pImpl && pImpl->isIntAttribute(); 114 } 115 116 bool Attribute::isStringAttribute() const { 117 return pImpl && pImpl->isStringAttribute(); 118 } 119 120 Attribute::AttrKind Attribute::getKindAsEnum() const { 121 if (!pImpl) return None; 122 assert((isEnumAttribute() || isIntAttribute()) && 123 "Invalid attribute type to get the kind as an enum!"); 124 return pImpl->getKindAsEnum(); 125 } 126 127 uint64_t Attribute::getValueAsInt() const { 128 if (!pImpl) return 0; 129 assert(isIntAttribute() && 130 "Expected the attribute to be an integer attribute!"); 131 return pImpl->getValueAsInt(); 132 } 133 134 StringRef Attribute::getKindAsString() const { 135 if (!pImpl) return StringRef(); 136 assert(isStringAttribute() && 137 "Invalid attribute type to get the kind as a string!"); 138 return pImpl->getKindAsString(); 139 } 140 141 StringRef Attribute::getValueAsString() const { 142 if (!pImpl) return StringRef(); 143 assert(isStringAttribute() && 144 "Invalid attribute type to get the value as a string!"); 145 return pImpl->getValueAsString(); 146 } 147 148 bool Attribute::hasAttribute(AttrKind Kind) const { 149 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None); 150 } 151 152 bool Attribute::hasAttribute(StringRef Kind) const { 153 if (!isStringAttribute()) return false; 154 return pImpl && pImpl->hasAttribute(Kind); 155 } 156 157 unsigned Attribute::getAlignment() const { 158 assert(hasAttribute(Attribute::Alignment) && 159 "Trying to get alignment from non-alignment attribute!"); 160 return pImpl->getValueAsInt(); 161 } 162 163 unsigned Attribute::getStackAlignment() const { 164 assert(hasAttribute(Attribute::StackAlignment) && 165 "Trying to get alignment from non-alignment attribute!"); 166 return pImpl->getValueAsInt(); 167 } 168 169 uint64_t Attribute::getDereferenceableBytes() const { 170 assert(hasAttribute(Attribute::Dereferenceable) && 171 "Trying to get dereferenceable bytes from " 172 "non-dereferenceable attribute!"); 173 return pImpl->getValueAsInt(); 174 } 175 176 uint64_t Attribute::getDereferenceableOrNullBytes() const { 177 assert(hasAttribute(Attribute::DereferenceableOrNull) && 178 "Trying to get dereferenceable bytes from " 179 "non-dereferenceable attribute!"); 180 return pImpl->getValueAsInt(); 181 } 182 183 std::string Attribute::getAsString(bool InAttrGrp) const { 184 if (!pImpl) return ""; 185 186 if (hasAttribute(Attribute::SanitizeAddress)) 187 return "sanitize_address"; 188 if (hasAttribute(Attribute::AlwaysInline)) 189 return "alwaysinline"; 190 if (hasAttribute(Attribute::ArgMemOnly)) 191 return "argmemonly"; 192 if (hasAttribute(Attribute::Builtin)) 193 return "builtin"; 194 if (hasAttribute(Attribute::ByVal)) 195 return "byval"; 196 if (hasAttribute(Attribute::Convergent)) 197 return "convergent"; 198 if (hasAttribute(Attribute::InaccessibleMemOnly)) 199 return "inaccessiblememonly"; 200 if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly)) 201 return "inaccessiblemem_or_argmemonly"; 202 if (hasAttribute(Attribute::InAlloca)) 203 return "inalloca"; 204 if (hasAttribute(Attribute::InlineHint)) 205 return "inlinehint"; 206 if (hasAttribute(Attribute::InReg)) 207 return "inreg"; 208 if (hasAttribute(Attribute::JumpTable)) 209 return "jumptable"; 210 if (hasAttribute(Attribute::MinSize)) 211 return "minsize"; 212 if (hasAttribute(Attribute::Naked)) 213 return "naked"; 214 if (hasAttribute(Attribute::Nest)) 215 return "nest"; 216 if (hasAttribute(Attribute::NoAlias)) 217 return "noalias"; 218 if (hasAttribute(Attribute::NoBuiltin)) 219 return "nobuiltin"; 220 if (hasAttribute(Attribute::NoCapture)) 221 return "nocapture"; 222 if (hasAttribute(Attribute::NoDuplicate)) 223 return "noduplicate"; 224 if (hasAttribute(Attribute::NoImplicitFloat)) 225 return "noimplicitfloat"; 226 if (hasAttribute(Attribute::NoInline)) 227 return "noinline"; 228 if (hasAttribute(Attribute::NonLazyBind)) 229 return "nonlazybind"; 230 if (hasAttribute(Attribute::NonNull)) 231 return "nonnull"; 232 if (hasAttribute(Attribute::NoRedZone)) 233 return "noredzone"; 234 if (hasAttribute(Attribute::NoReturn)) 235 return "noreturn"; 236 if (hasAttribute(Attribute::NoRecurse)) 237 return "norecurse"; 238 if (hasAttribute(Attribute::NoUnwind)) 239 return "nounwind"; 240 if (hasAttribute(Attribute::OptimizeNone)) 241 return "optnone"; 242 if (hasAttribute(Attribute::OptimizeForSize)) 243 return "optsize"; 244 if (hasAttribute(Attribute::ReadNone)) 245 return "readnone"; 246 if (hasAttribute(Attribute::ReadOnly)) 247 return "readonly"; 248 if (hasAttribute(Attribute::Returned)) 249 return "returned"; 250 if (hasAttribute(Attribute::ReturnsTwice)) 251 return "returns_twice"; 252 if (hasAttribute(Attribute::SExt)) 253 return "signext"; 254 if (hasAttribute(Attribute::StackProtect)) 255 return "ssp"; 256 if (hasAttribute(Attribute::StackProtectReq)) 257 return "sspreq"; 258 if (hasAttribute(Attribute::StackProtectStrong)) 259 return "sspstrong"; 260 if (hasAttribute(Attribute::SafeStack)) 261 return "safestack"; 262 if (hasAttribute(Attribute::StructRet)) 263 return "sret"; 264 if (hasAttribute(Attribute::SanitizeThread)) 265 return "sanitize_thread"; 266 if (hasAttribute(Attribute::SanitizeMemory)) 267 return "sanitize_memory"; 268 if (hasAttribute(Attribute::UWTable)) 269 return "uwtable"; 270 if (hasAttribute(Attribute::ZExt)) 271 return "zeroext"; 272 if (hasAttribute(Attribute::Cold)) 273 return "cold"; 274 275 // FIXME: These should be output like this: 276 // 277 // align=4 278 // alignstack=8 279 // 280 if (hasAttribute(Attribute::Alignment)) { 281 std::string Result; 282 Result += "align"; 283 Result += (InAttrGrp) ? "=" : " "; 284 Result += utostr(getValueAsInt()); 285 return Result; 286 } 287 288 auto AttrWithBytesToString = [&](const char *Name) { 289 std::string Result; 290 Result += Name; 291 if (InAttrGrp) { 292 Result += "="; 293 Result += utostr(getValueAsInt()); 294 } else { 295 Result += "("; 296 Result += utostr(getValueAsInt()); 297 Result += ")"; 298 } 299 return Result; 300 }; 301 302 if (hasAttribute(Attribute::StackAlignment)) 303 return AttrWithBytesToString("alignstack"); 304 305 if (hasAttribute(Attribute::Dereferenceable)) 306 return AttrWithBytesToString("dereferenceable"); 307 308 if (hasAttribute(Attribute::DereferenceableOrNull)) 309 return AttrWithBytesToString("dereferenceable_or_null"); 310 311 // Convert target-dependent attributes to strings of the form: 312 // 313 // "kind" 314 // "kind" = "value" 315 // 316 if (isStringAttribute()) { 317 std::string Result; 318 Result += (Twine('"') + getKindAsString() + Twine('"')).str(); 319 320 StringRef Val = pImpl->getValueAsString(); 321 if (Val.empty()) return Result; 322 323 Result += ("=\"" + Val + Twine('"')).str(); 324 return Result; 325 } 326 327 llvm_unreachable("Unknown attribute"); 328 } 329 330 bool Attribute::operator<(Attribute A) const { 331 if (!pImpl && !A.pImpl) return false; 332 if (!pImpl) return true; 333 if (!A.pImpl) return false; 334 return *pImpl < *A.pImpl; 335 } 336 337 //===----------------------------------------------------------------------===// 338 // AttributeImpl Definition 339 //===----------------------------------------------------------------------===// 340 341 // Pin the vtables to this file. 342 AttributeImpl::~AttributeImpl() {} 343 void EnumAttributeImpl::anchor() {} 344 void IntAttributeImpl::anchor() {} 345 void StringAttributeImpl::anchor() {} 346 347 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const { 348 if (isStringAttribute()) return false; 349 return getKindAsEnum() == A; 350 } 351 352 bool AttributeImpl::hasAttribute(StringRef Kind) const { 353 if (!isStringAttribute()) return false; 354 return getKindAsString() == Kind; 355 } 356 357 Attribute::AttrKind AttributeImpl::getKindAsEnum() const { 358 assert(isEnumAttribute() || isIntAttribute()); 359 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind(); 360 } 361 362 uint64_t AttributeImpl::getValueAsInt() const { 363 assert(isIntAttribute()); 364 return static_cast<const IntAttributeImpl *>(this)->getValue(); 365 } 366 367 StringRef AttributeImpl::getKindAsString() const { 368 assert(isStringAttribute()); 369 return static_cast<const StringAttributeImpl *>(this)->getStringKind(); 370 } 371 372 StringRef AttributeImpl::getValueAsString() const { 373 assert(isStringAttribute()); 374 return static_cast<const StringAttributeImpl *>(this)->getStringValue(); 375 } 376 377 bool AttributeImpl::operator<(const AttributeImpl &AI) const { 378 // This sorts the attributes with Attribute::AttrKinds coming first (sorted 379 // relative to their enum value) and then strings. 380 if (isEnumAttribute()) { 381 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum(); 382 if (AI.isIntAttribute()) return true; 383 if (AI.isStringAttribute()) return true; 384 } 385 386 if (isIntAttribute()) { 387 if (AI.isEnumAttribute()) return false; 388 if (AI.isIntAttribute()) return getValueAsInt() < AI.getValueAsInt(); 389 if (AI.isStringAttribute()) return true; 390 } 391 392 if (AI.isEnumAttribute()) return false; 393 if (AI.isIntAttribute()) return false; 394 if (getKindAsString() == AI.getKindAsString()) 395 return getValueAsString() < AI.getValueAsString(); 396 return getKindAsString() < AI.getKindAsString(); 397 } 398 399 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) { 400 // FIXME: Remove this. 401 switch (Val) { 402 case Attribute::EndAttrKinds: 403 llvm_unreachable("Synthetic enumerators which should never get here"); 404 405 case Attribute::None: return 0; 406 case Attribute::ZExt: return 1 << 0; 407 case Attribute::SExt: return 1 << 1; 408 case Attribute::NoReturn: return 1 << 2; 409 case Attribute::InReg: return 1 << 3; 410 case Attribute::StructRet: return 1 << 4; 411 case Attribute::NoUnwind: return 1 << 5; 412 case Attribute::NoAlias: return 1 << 6; 413 case Attribute::ByVal: return 1 << 7; 414 case Attribute::Nest: return 1 << 8; 415 case Attribute::ReadNone: return 1 << 9; 416 case Attribute::ReadOnly: return 1 << 10; 417 case Attribute::NoInline: return 1 << 11; 418 case Attribute::AlwaysInline: return 1 << 12; 419 case Attribute::OptimizeForSize: return 1 << 13; 420 case Attribute::StackProtect: return 1 << 14; 421 case Attribute::StackProtectReq: return 1 << 15; 422 case Attribute::Alignment: return 31 << 16; 423 case Attribute::NoCapture: return 1 << 21; 424 case Attribute::NoRedZone: return 1 << 22; 425 case Attribute::NoImplicitFloat: return 1 << 23; 426 case Attribute::Naked: return 1 << 24; 427 case Attribute::InlineHint: return 1 << 25; 428 case Attribute::StackAlignment: return 7 << 26; 429 case Attribute::ReturnsTwice: return 1 << 29; 430 case Attribute::UWTable: return 1 << 30; 431 case Attribute::NonLazyBind: return 1U << 31; 432 case Attribute::SanitizeAddress: return 1ULL << 32; 433 case Attribute::MinSize: return 1ULL << 33; 434 case Attribute::NoDuplicate: return 1ULL << 34; 435 case Attribute::StackProtectStrong: return 1ULL << 35; 436 case Attribute::SanitizeThread: return 1ULL << 36; 437 case Attribute::SanitizeMemory: return 1ULL << 37; 438 case Attribute::NoBuiltin: return 1ULL << 38; 439 case Attribute::Returned: return 1ULL << 39; 440 case Attribute::Cold: return 1ULL << 40; 441 case Attribute::Builtin: return 1ULL << 41; 442 case Attribute::OptimizeNone: return 1ULL << 42; 443 case Attribute::InAlloca: return 1ULL << 43; 444 case Attribute::NonNull: return 1ULL << 44; 445 case Attribute::JumpTable: return 1ULL << 45; 446 case Attribute::Convergent: return 1ULL << 46; 447 case Attribute::SafeStack: return 1ULL << 47; 448 case Attribute::NoRecurse: return 1ULL << 48; 449 case Attribute::InaccessibleMemOnly: return 1ULL << 49; 450 case Attribute::InaccessibleMemOrArgMemOnly: return 1ULL << 50; 451 case Attribute::Dereferenceable: 452 llvm_unreachable("dereferenceable attribute not supported in raw format"); 453 break; 454 case Attribute::DereferenceableOrNull: 455 llvm_unreachable("dereferenceable_or_null attribute not supported in raw " 456 "format"); 457 break; 458 case Attribute::ArgMemOnly: 459 llvm_unreachable("argmemonly attribute not supported in raw format"); 460 break; 461 } 462 llvm_unreachable("Unsupported attribute type"); 463 } 464 465 //===----------------------------------------------------------------------===// 466 // AttributeSetNode Definition 467 //===----------------------------------------------------------------------===// 468 469 AttributeSetNode *AttributeSetNode::get(LLVMContext &C, 470 ArrayRef<Attribute> Attrs) { 471 if (Attrs.empty()) 472 return nullptr; 473 474 // Otherwise, build a key to look up the existing attributes. 475 LLVMContextImpl *pImpl = C.pImpl; 476 FoldingSetNodeID ID; 477 478 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end()); 479 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end()); 480 481 for (Attribute Attr : SortedAttrs) 482 Attr.Profile(ID); 483 484 void *InsertPoint; 485 AttributeSetNode *PA = 486 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint); 487 488 // If we didn't find any existing attributes of the same shape then create a 489 // new one and insert it. 490 if (!PA) { 491 // Coallocate entries after the AttributeSetNode itself. 492 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size())); 493 PA = new (Mem) AttributeSetNode(SortedAttrs); 494 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint); 495 } 496 497 // Return the AttributesListNode that we found or created. 498 return PA; 499 } 500 501 bool AttributeSetNode::hasAttribute(StringRef Kind) const { 502 for (iterator I = begin(), E = end(); I != E; ++I) 503 if (I->hasAttribute(Kind)) 504 return true; 505 return false; 506 } 507 508 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const { 509 if (hasAttribute(Kind)) { 510 for (iterator I = begin(), E = end(); I != E; ++I) 511 if (I->hasAttribute(Kind)) 512 return *I; 513 } 514 return Attribute(); 515 } 516 517 Attribute AttributeSetNode::getAttribute(StringRef Kind) const { 518 for (iterator I = begin(), E = end(); I != E; ++I) 519 if (I->hasAttribute(Kind)) 520 return *I; 521 return Attribute(); 522 } 523 524 unsigned AttributeSetNode::getAlignment() const { 525 for (iterator I = begin(), E = end(); I != E; ++I) 526 if (I->hasAttribute(Attribute::Alignment)) 527 return I->getAlignment(); 528 return 0; 529 } 530 531 unsigned AttributeSetNode::getStackAlignment() const { 532 for (iterator I = begin(), E = end(); I != E; ++I) 533 if (I->hasAttribute(Attribute::StackAlignment)) 534 return I->getStackAlignment(); 535 return 0; 536 } 537 538 uint64_t AttributeSetNode::getDereferenceableBytes() const { 539 for (iterator I = begin(), E = end(); I != E; ++I) 540 if (I->hasAttribute(Attribute::Dereferenceable)) 541 return I->getDereferenceableBytes(); 542 return 0; 543 } 544 545 uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const { 546 for (iterator I = begin(), E = end(); I != E; ++I) 547 if (I->hasAttribute(Attribute::DereferenceableOrNull)) 548 return I->getDereferenceableOrNullBytes(); 549 return 0; 550 } 551 552 std::string AttributeSetNode::getAsString(bool InAttrGrp) const { 553 std::string Str; 554 for (iterator I = begin(), E = end(); I != E; ++I) { 555 if (I != begin()) 556 Str += ' '; 557 Str += I->getAsString(InAttrGrp); 558 } 559 return Str; 560 } 561 562 //===----------------------------------------------------------------------===// 563 // AttributeSetImpl Definition 564 //===----------------------------------------------------------------------===// 565 566 uint64_t AttributeSetImpl::Raw(unsigned Index) const { 567 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) { 568 if (getSlotIndex(I) != Index) continue; 569 const AttributeSetNode *ASN = getSlotNode(I); 570 uint64_t Mask = 0; 571 572 for (AttributeSetNode::iterator II = ASN->begin(), 573 IE = ASN->end(); II != IE; ++II) { 574 Attribute Attr = *II; 575 576 // This cannot handle string attributes. 577 if (Attr.isStringAttribute()) continue; 578 579 Attribute::AttrKind Kind = Attr.getKindAsEnum(); 580 581 if (Kind == Attribute::Alignment) 582 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16; 583 else if (Kind == Attribute::StackAlignment) 584 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26; 585 else if (Kind == Attribute::Dereferenceable) 586 llvm_unreachable("dereferenceable not supported in bit mask"); 587 else 588 Mask |= AttributeImpl::getAttrMask(Kind); 589 } 590 591 return Mask; 592 } 593 594 return 0; 595 } 596 597 LLVM_DUMP_METHOD void AttributeSetImpl::dump() const { 598 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump(); 599 } 600 601 //===----------------------------------------------------------------------===// 602 // AttributeSet Construction and Mutation Methods 603 //===----------------------------------------------------------------------===// 604 605 AttributeSet 606 AttributeSet::getImpl(LLVMContext &C, 607 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) { 608 LLVMContextImpl *pImpl = C.pImpl; 609 FoldingSetNodeID ID; 610 AttributeSetImpl::Profile(ID, Attrs); 611 612 void *InsertPoint; 613 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint); 614 615 // If we didn't find any existing attributes of the same shape then 616 // create a new one and insert it. 617 if (!PA) { 618 // Coallocate entries after the AttributeSetImpl itself. 619 void *Mem = ::operator new( 620 AttributeSetImpl::totalSizeToAlloc<IndexAttrPair>(Attrs.size())); 621 PA = new (Mem) AttributeSetImpl(C, Attrs); 622 pImpl->AttrsLists.InsertNode(PA, InsertPoint); 623 } 624 625 // Return the AttributesList that we found or created. 626 return AttributeSet(PA); 627 } 628 629 AttributeSet AttributeSet::get(LLVMContext &C, 630 ArrayRef<std::pair<unsigned, Attribute> > Attrs){ 631 // If there are no attributes then return a null AttributesList pointer. 632 if (Attrs.empty()) 633 return AttributeSet(); 634 635 assert(std::is_sorted(Attrs.begin(), Attrs.end(), 636 [](const std::pair<unsigned, Attribute> &LHS, 637 const std::pair<unsigned, Attribute> &RHS) { 638 return LHS.first < RHS.first; 639 }) && "Misordered Attributes list!"); 640 assert(std::none_of(Attrs.begin(), Attrs.end(), 641 [](const std::pair<unsigned, Attribute> &Pair) { 642 return Pair.second.hasAttribute(Attribute::None); 643 }) && "Pointless attribute!"); 644 645 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes 646 // list. 647 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec; 648 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(), 649 E = Attrs.end(); I != E; ) { 650 unsigned Index = I->first; 651 SmallVector<Attribute, 4> AttrVec; 652 while (I != E && I->first == Index) { 653 AttrVec.push_back(I->second); 654 ++I; 655 } 656 657 AttrPairVec.push_back(std::make_pair(Index, 658 AttributeSetNode::get(C, AttrVec))); 659 } 660 661 return getImpl(C, AttrPairVec); 662 } 663 664 AttributeSet AttributeSet::get(LLVMContext &C, 665 ArrayRef<std::pair<unsigned, 666 AttributeSetNode*> > Attrs) { 667 // If there are no attributes then return a null AttributesList pointer. 668 if (Attrs.empty()) 669 return AttributeSet(); 670 671 return getImpl(C, Attrs); 672 } 673 674 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index, 675 const AttrBuilder &B) { 676 if (!B.hasAttributes()) 677 return AttributeSet(); 678 679 // Add target-independent attributes. 680 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs; 681 for (Attribute::AttrKind Kind = Attribute::None; 682 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) { 683 if (!B.contains(Kind)) 684 continue; 685 686 Attribute Attr; 687 switch (Kind) { 688 case Attribute::Alignment: 689 Attr = Attribute::getWithAlignment(C, B.getAlignment()); 690 break; 691 case Attribute::StackAlignment: 692 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment()); 693 break; 694 case Attribute::Dereferenceable: 695 Attr = Attribute::getWithDereferenceableBytes( 696 C, B.getDereferenceableBytes()); 697 break; 698 case Attribute::DereferenceableOrNull: 699 Attr = Attribute::getWithDereferenceableOrNullBytes( 700 C, B.getDereferenceableOrNullBytes()); 701 break; 702 default: 703 Attr = Attribute::get(C, Kind); 704 } 705 Attrs.push_back(std::make_pair(Index, Attr)); 706 } 707 708 // Add target-dependent (string) attributes. 709 for (const AttrBuilder::td_type &TDA : B.td_attrs()) 710 Attrs.push_back( 711 std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second))); 712 713 return get(C, Attrs); 714 } 715 716 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index, 717 ArrayRef<Attribute::AttrKind> Kind) { 718 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs; 719 for (Attribute::AttrKind K : Kind) 720 Attrs.push_back(std::make_pair(Index, Attribute::get(C, K))); 721 return get(C, Attrs); 722 } 723 724 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) { 725 if (Attrs.empty()) return AttributeSet(); 726 if (Attrs.size() == 1) return Attrs[0]; 727 728 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec; 729 AttributeSetImpl *A0 = Attrs[0].pImpl; 730 if (A0) 731 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes())); 732 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec 733 // ordered by index. Because we know that each list in Attrs is ordered by 734 // index we only need to merge each successive list in rather than doing a 735 // full sort. 736 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) { 737 AttributeSetImpl *AS = Attrs[I].pImpl; 738 if (!AS) continue; 739 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator 740 ANVI = AttrNodeVec.begin(), ANVE; 741 for (const IndexAttrPair *AI = AS->getNode(0), 742 *AE = AS->getNode(AS->getNumAttributes()); 743 AI != AE; ++AI) { 744 ANVE = AttrNodeVec.end(); 745 while (ANVI != ANVE && ANVI->first <= AI->first) 746 ++ANVI; 747 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1; 748 } 749 } 750 751 return getImpl(C, AttrNodeVec); 752 } 753 754 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index, 755 Attribute::AttrKind Attr) const { 756 if (hasAttribute(Index, Attr)) return *this; 757 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr)); 758 } 759 760 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index, 761 StringRef Kind) const { 762 llvm::AttrBuilder B; 763 B.addAttribute(Kind); 764 return addAttributes(C, Index, AttributeSet::get(C, Index, B)); 765 } 766 767 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index, 768 StringRef Kind, StringRef Value) const { 769 llvm::AttrBuilder B; 770 B.addAttribute(Kind, Value); 771 return addAttributes(C, Index, AttributeSet::get(C, Index, B)); 772 } 773 774 AttributeSet AttributeSet::addAttribute(LLVMContext &C, 775 ArrayRef<unsigned> Indices, 776 Attribute A) const { 777 unsigned I = 0, E = pImpl ? pImpl->getNumAttributes() : 0; 778 auto IdxI = Indices.begin(), IdxE = Indices.end(); 779 SmallVector<AttributeSet, 4> AttrSet; 780 781 while (I != E && IdxI != IdxE) { 782 if (getSlotIndex(I) < *IdxI) 783 AttrSet.emplace_back(getSlotAttributes(I++)); 784 else if (getSlotIndex(I) > *IdxI) 785 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A))); 786 else { 787 AttrBuilder B(getSlotAttributes(I), *IdxI); 788 B.addAttribute(A); 789 AttrSet.emplace_back(AttributeSet::get(C, *IdxI, B)); 790 ++I; 791 ++IdxI; 792 } 793 } 794 795 while (I != E) 796 AttrSet.emplace_back(getSlotAttributes(I++)); 797 798 while (IdxI != IdxE) 799 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A))); 800 801 return get(C, AttrSet); 802 } 803 804 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index, 805 AttributeSet Attrs) const { 806 if (!pImpl) return Attrs; 807 if (!Attrs.pImpl) return *this; 808 809 #ifndef NDEBUG 810 // FIXME it is not obvious how this should work for alignment. For now, say 811 // we can't change a known alignment. 812 unsigned OldAlign = getParamAlignment(Index); 813 unsigned NewAlign = Attrs.getParamAlignment(Index); 814 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) && 815 "Attempt to change alignment!"); 816 #endif 817 818 // Add the attribute slots before the one we're trying to add. 819 SmallVector<AttributeSet, 4> AttrSet; 820 uint64_t NumAttrs = pImpl->getNumAttributes(); 821 AttributeSet AS; 822 uint64_t LastIndex = 0; 823 for (unsigned I = 0, E = NumAttrs; I != E; ++I) { 824 if (getSlotIndex(I) >= Index) { 825 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++); 826 break; 827 } 828 LastIndex = I + 1; 829 AttrSet.push_back(getSlotAttributes(I)); 830 } 831 832 // Now add the attribute into the correct slot. There may already be an 833 // AttributeSet there. 834 AttrBuilder B(AS, Index); 835 836 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I) 837 if (Attrs.getSlotIndex(I) == Index) { 838 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I), 839 IE = Attrs.pImpl->end(I); II != IE; ++II) 840 B.addAttribute(*II); 841 break; 842 } 843 844 AttrSet.push_back(AttributeSet::get(C, Index, B)); 845 846 // Add the remaining attribute slots. 847 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I) 848 AttrSet.push_back(getSlotAttributes(I)); 849 850 return get(C, AttrSet); 851 } 852 853 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index, 854 Attribute::AttrKind Attr) const { 855 if (!hasAttribute(Index, Attr)) return *this; 856 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr)); 857 } 858 859 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index, 860 AttributeSet Attrs) const { 861 if (!pImpl) return AttributeSet(); 862 if (!Attrs.pImpl) return *this; 863 864 // FIXME it is not obvious how this should work for alignment. 865 // For now, say we can't pass in alignment, which no current use does. 866 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) && 867 "Attempt to change alignment!"); 868 869 // Add the attribute slots before the one we're trying to add. 870 SmallVector<AttributeSet, 4> AttrSet; 871 uint64_t NumAttrs = pImpl->getNumAttributes(); 872 AttributeSet AS; 873 uint64_t LastIndex = 0; 874 for (unsigned I = 0, E = NumAttrs; I != E; ++I) { 875 if (getSlotIndex(I) >= Index) { 876 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++); 877 break; 878 } 879 LastIndex = I + 1; 880 AttrSet.push_back(getSlotAttributes(I)); 881 } 882 883 // Now remove the attribute from the correct slot. There may already be an 884 // AttributeSet there. 885 AttrBuilder B(AS, Index); 886 887 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I) 888 if (Attrs.getSlotIndex(I) == Index) { 889 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index); 890 break; 891 } 892 893 AttrSet.push_back(AttributeSet::get(C, Index, B)); 894 895 // Add the remaining attribute slots. 896 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I) 897 AttrSet.push_back(getSlotAttributes(I)); 898 899 return get(C, AttrSet); 900 } 901 902 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index, 903 const AttrBuilder &Attrs) const { 904 if (!pImpl) return AttributeSet(); 905 906 // FIXME it is not obvious how this should work for alignment. 907 // For now, say we can't pass in alignment, which no current use does. 908 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!"); 909 910 // Add the attribute slots before the one we're trying to add. 911 SmallVector<AttributeSet, 4> AttrSet; 912 uint64_t NumAttrs = pImpl->getNumAttributes(); 913 AttributeSet AS; 914 uint64_t LastIndex = 0; 915 for (unsigned I = 0, E = NumAttrs; I != E; ++I) { 916 if (getSlotIndex(I) >= Index) { 917 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++); 918 break; 919 } 920 LastIndex = I + 1; 921 AttrSet.push_back(getSlotAttributes(I)); 922 } 923 924 // Now remove the attribute from the correct slot. There may already be an 925 // AttributeSet there. 926 AttrBuilder B(AS, Index); 927 B.remove(Attrs); 928 929 AttrSet.push_back(AttributeSet::get(C, Index, B)); 930 931 // Add the remaining attribute slots. 932 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I) 933 AttrSet.push_back(getSlotAttributes(I)); 934 935 return get(C, AttrSet); 936 } 937 938 AttributeSet AttributeSet::addDereferenceableAttr(LLVMContext &C, unsigned Index, 939 uint64_t Bytes) const { 940 llvm::AttrBuilder B; 941 B.addDereferenceableAttr(Bytes); 942 return addAttributes(C, Index, AttributeSet::get(C, Index, B)); 943 } 944 945 AttributeSet AttributeSet::addDereferenceableOrNullAttr(LLVMContext &C, 946 unsigned Index, 947 uint64_t Bytes) const { 948 llvm::AttrBuilder B; 949 B.addDereferenceableOrNullAttr(Bytes); 950 return addAttributes(C, Index, AttributeSet::get(C, Index, B)); 951 } 952 953 //===----------------------------------------------------------------------===// 954 // AttributeSet Accessor Methods 955 //===----------------------------------------------------------------------===// 956 957 LLVMContext &AttributeSet::getContext() const { 958 return pImpl->getContext(); 959 } 960 961 AttributeSet AttributeSet::getParamAttributes(unsigned Index) const { 962 return pImpl && hasAttributes(Index) ? 963 AttributeSet::get(pImpl->getContext(), 964 ArrayRef<std::pair<unsigned, AttributeSetNode*> >( 965 std::make_pair(Index, getAttributes(Index)))) : 966 AttributeSet(); 967 } 968 969 AttributeSet AttributeSet::getRetAttributes() const { 970 return pImpl && hasAttributes(ReturnIndex) ? 971 AttributeSet::get(pImpl->getContext(), 972 ArrayRef<std::pair<unsigned, AttributeSetNode*> >( 973 std::make_pair(ReturnIndex, 974 getAttributes(ReturnIndex)))) : 975 AttributeSet(); 976 } 977 978 AttributeSet AttributeSet::getFnAttributes() const { 979 return pImpl && hasAttributes(FunctionIndex) ? 980 AttributeSet::get(pImpl->getContext(), 981 ArrayRef<std::pair<unsigned, AttributeSetNode*> >( 982 std::make_pair(FunctionIndex, 983 getAttributes(FunctionIndex)))) : 984 AttributeSet(); 985 } 986 987 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{ 988 AttributeSetNode *ASN = getAttributes(Index); 989 return ASN && ASN->hasAttribute(Kind); 990 } 991 992 bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const { 993 AttributeSetNode *ASN = getAttributes(Index); 994 return ASN && ASN->hasAttribute(Kind); 995 } 996 997 bool AttributeSet::hasAttributes(unsigned Index) const { 998 AttributeSetNode *ASN = getAttributes(Index); 999 return ASN && ASN->hasAttributes(); 1000 } 1001 1002 bool AttributeSet::hasFnAttribute(Attribute::AttrKind Kind) const { 1003 return pImpl && pImpl->hasFnAttribute(Kind); 1004 } 1005 1006 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const { 1007 if (!pImpl) return false; 1008 1009 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) 1010 for (AttributeSetImpl::iterator II = pImpl->begin(I), 1011 IE = pImpl->end(I); II != IE; ++II) 1012 if (II->hasAttribute(Attr)) 1013 return true; 1014 1015 return false; 1016 } 1017 1018 Attribute AttributeSet::getAttribute(unsigned Index, 1019 Attribute::AttrKind Kind) const { 1020 AttributeSetNode *ASN = getAttributes(Index); 1021 return ASN ? ASN->getAttribute(Kind) : Attribute(); 1022 } 1023 1024 Attribute AttributeSet::getAttribute(unsigned Index, 1025 StringRef Kind) const { 1026 AttributeSetNode *ASN = getAttributes(Index); 1027 return ASN ? ASN->getAttribute(Kind) : Attribute(); 1028 } 1029 1030 unsigned AttributeSet::getParamAlignment(unsigned Index) const { 1031 AttributeSetNode *ASN = getAttributes(Index); 1032 return ASN ? ASN->getAlignment() : 0; 1033 } 1034 1035 unsigned AttributeSet::getStackAlignment(unsigned Index) const { 1036 AttributeSetNode *ASN = getAttributes(Index); 1037 return ASN ? ASN->getStackAlignment() : 0; 1038 } 1039 1040 uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const { 1041 AttributeSetNode *ASN = getAttributes(Index); 1042 return ASN ? ASN->getDereferenceableBytes() : 0; 1043 } 1044 1045 uint64_t AttributeSet::getDereferenceableOrNullBytes(unsigned Index) const { 1046 AttributeSetNode *ASN = getAttributes(Index); 1047 return ASN ? ASN->getDereferenceableOrNullBytes() : 0; 1048 } 1049 1050 std::string AttributeSet::getAsString(unsigned Index, 1051 bool InAttrGrp) const { 1052 AttributeSetNode *ASN = getAttributes(Index); 1053 return ASN ? ASN->getAsString(InAttrGrp) : std::string(""); 1054 } 1055 1056 AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const { 1057 if (!pImpl) return nullptr; 1058 1059 // Loop through to find the attribute node we want. 1060 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) 1061 if (pImpl->getSlotIndex(I) == Index) 1062 return pImpl->getSlotNode(I); 1063 1064 return nullptr; 1065 } 1066 1067 AttributeSet::iterator AttributeSet::begin(unsigned Slot) const { 1068 if (!pImpl) 1069 return ArrayRef<Attribute>().begin(); 1070 return pImpl->begin(Slot); 1071 } 1072 1073 AttributeSet::iterator AttributeSet::end(unsigned Slot) const { 1074 if (!pImpl) 1075 return ArrayRef<Attribute>().end(); 1076 return pImpl->end(Slot); 1077 } 1078 1079 //===----------------------------------------------------------------------===// 1080 // AttributeSet Introspection Methods 1081 //===----------------------------------------------------------------------===// 1082 1083 unsigned AttributeSet::getNumSlots() const { 1084 return pImpl ? pImpl->getNumAttributes() : 0; 1085 } 1086 1087 unsigned AttributeSet::getSlotIndex(unsigned Slot) const { 1088 assert(pImpl && Slot < pImpl->getNumAttributes() && 1089 "Slot # out of range!"); 1090 return pImpl->getSlotIndex(Slot); 1091 } 1092 1093 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const { 1094 assert(pImpl && Slot < pImpl->getNumAttributes() && 1095 "Slot # out of range!"); 1096 return pImpl->getSlotAttributes(Slot); 1097 } 1098 1099 uint64_t AttributeSet::Raw(unsigned Index) const { 1100 // FIXME: Remove this. 1101 return pImpl ? pImpl->Raw(Index) : 0; 1102 } 1103 1104 LLVM_DUMP_METHOD void AttributeSet::dump() const { 1105 dbgs() << "PAL[\n"; 1106 1107 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) { 1108 uint64_t Index = getSlotIndex(i); 1109 dbgs() << " { "; 1110 if (Index == ~0U) 1111 dbgs() << "~0U"; 1112 else 1113 dbgs() << Index; 1114 dbgs() << " => " << getAsString(Index) << " }\n"; 1115 } 1116 1117 dbgs() << "]\n"; 1118 } 1119 1120 //===----------------------------------------------------------------------===// 1121 // AttrBuilder Method Implementations 1122 //===----------------------------------------------------------------------===// 1123 1124 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index) 1125 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0), 1126 DerefOrNullBytes(0) { 1127 AttributeSetImpl *pImpl = AS.pImpl; 1128 if (!pImpl) return; 1129 1130 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) { 1131 if (pImpl->getSlotIndex(I) != Index) continue; 1132 1133 for (AttributeSetImpl::iterator II = pImpl->begin(I), 1134 IE = pImpl->end(I); II != IE; ++II) 1135 addAttribute(*II); 1136 1137 break; 1138 } 1139 } 1140 1141 void AttrBuilder::clear() { 1142 Attrs.reset(); 1143 TargetDepAttrs.clear(); 1144 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0; 1145 } 1146 1147 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) { 1148 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!"); 1149 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment && 1150 Val != Attribute::Dereferenceable && 1151 "Adding integer attribute without adding a value!"); 1152 Attrs[Val] = true; 1153 return *this; 1154 } 1155 1156 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) { 1157 if (Attr.isStringAttribute()) { 1158 addAttribute(Attr.getKindAsString(), Attr.getValueAsString()); 1159 return *this; 1160 } 1161 1162 Attribute::AttrKind Kind = Attr.getKindAsEnum(); 1163 Attrs[Kind] = true; 1164 1165 if (Kind == Attribute::Alignment) 1166 Alignment = Attr.getAlignment(); 1167 else if (Kind == Attribute::StackAlignment) 1168 StackAlignment = Attr.getStackAlignment(); 1169 else if (Kind == Attribute::Dereferenceable) 1170 DerefBytes = Attr.getDereferenceableBytes(); 1171 else if (Kind == Attribute::DereferenceableOrNull) 1172 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes(); 1173 return *this; 1174 } 1175 1176 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) { 1177 TargetDepAttrs[A] = V; 1178 return *this; 1179 } 1180 1181 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) { 1182 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!"); 1183 Attrs[Val] = false; 1184 1185 if (Val == Attribute::Alignment) 1186 Alignment = 0; 1187 else if (Val == Attribute::StackAlignment) 1188 StackAlignment = 0; 1189 else if (Val == Attribute::Dereferenceable) 1190 DerefBytes = 0; 1191 else if (Val == Attribute::DereferenceableOrNull) 1192 DerefOrNullBytes = 0; 1193 1194 return *this; 1195 } 1196 1197 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) { 1198 unsigned Slot = ~0U; 1199 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I) 1200 if (A.getSlotIndex(I) == Index) { 1201 Slot = I; 1202 break; 1203 } 1204 1205 assert(Slot != ~0U && "Couldn't find index in AttributeSet!"); 1206 1207 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) { 1208 Attribute Attr = *I; 1209 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) { 1210 removeAttribute(Attr.getKindAsEnum()); 1211 } else { 1212 assert(Attr.isStringAttribute() && "Invalid attribute type!"); 1213 removeAttribute(Attr.getKindAsString()); 1214 } 1215 } 1216 1217 return *this; 1218 } 1219 1220 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) { 1221 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A); 1222 if (I != TargetDepAttrs.end()) 1223 TargetDepAttrs.erase(I); 1224 return *this; 1225 } 1226 1227 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) { 1228 if (Align == 0) return *this; 1229 1230 assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); 1231 assert(Align <= 0x40000000 && "Alignment too large."); 1232 1233 Attrs[Attribute::Alignment] = true; 1234 Alignment = Align; 1235 return *this; 1236 } 1237 1238 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) { 1239 // Default alignment, allow the target to define how to align it. 1240 if (Align == 0) return *this; 1241 1242 assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); 1243 assert(Align <= 0x100 && "Alignment too large."); 1244 1245 Attrs[Attribute::StackAlignment] = true; 1246 StackAlignment = Align; 1247 return *this; 1248 } 1249 1250 AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) { 1251 if (Bytes == 0) return *this; 1252 1253 Attrs[Attribute::Dereferenceable] = true; 1254 DerefBytes = Bytes; 1255 return *this; 1256 } 1257 1258 AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) { 1259 if (Bytes == 0) 1260 return *this; 1261 1262 Attrs[Attribute::DereferenceableOrNull] = true; 1263 DerefOrNullBytes = Bytes; 1264 return *this; 1265 } 1266 1267 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) { 1268 // FIXME: What if both have alignments, but they don't match?! 1269 if (!Alignment) 1270 Alignment = B.Alignment; 1271 1272 if (!StackAlignment) 1273 StackAlignment = B.StackAlignment; 1274 1275 if (!DerefBytes) 1276 DerefBytes = B.DerefBytes; 1277 1278 if (!DerefOrNullBytes) 1279 DerefOrNullBytes = B.DerefOrNullBytes; 1280 1281 Attrs |= B.Attrs; 1282 1283 for (auto I : B.td_attrs()) 1284 TargetDepAttrs[I.first] = I.second; 1285 1286 return *this; 1287 } 1288 1289 AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) { 1290 // FIXME: What if both have alignments, but they don't match?! 1291 if (B.Alignment) 1292 Alignment = 0; 1293 1294 if (B.StackAlignment) 1295 StackAlignment = 0; 1296 1297 if (B.DerefBytes) 1298 DerefBytes = 0; 1299 1300 if (B.DerefOrNullBytes) 1301 DerefOrNullBytes = 0; 1302 1303 Attrs &= ~B.Attrs; 1304 1305 for (auto I : B.td_attrs()) 1306 TargetDepAttrs.erase(I.first); 1307 1308 return *this; 1309 } 1310 1311 bool AttrBuilder::overlaps(const AttrBuilder &B) const { 1312 // First check if any of the target independent attributes overlap. 1313 if ((Attrs & B.Attrs).any()) 1314 return true; 1315 1316 // Then check if any target dependent ones do. 1317 for (auto I : td_attrs()) 1318 if (B.contains(I.first)) 1319 return true; 1320 1321 return false; 1322 } 1323 1324 bool AttrBuilder::contains(StringRef A) const { 1325 return TargetDepAttrs.find(A) != TargetDepAttrs.end(); 1326 } 1327 1328 bool AttrBuilder::hasAttributes() const { 1329 return !Attrs.none() || !TargetDepAttrs.empty(); 1330 } 1331 1332 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const { 1333 unsigned Slot = ~0U; 1334 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I) 1335 if (A.getSlotIndex(I) == Index) { 1336 Slot = I; 1337 break; 1338 } 1339 1340 assert(Slot != ~0U && "Couldn't find the index!"); 1341 1342 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) { 1343 Attribute Attr = *I; 1344 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) { 1345 if (Attrs[I->getKindAsEnum()]) 1346 return true; 1347 } else { 1348 assert(Attr.isStringAttribute() && "Invalid attribute kind!"); 1349 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end(); 1350 } 1351 } 1352 1353 return false; 1354 } 1355 1356 bool AttrBuilder::hasAlignmentAttr() const { 1357 return Alignment != 0; 1358 } 1359 1360 bool AttrBuilder::operator==(const AttrBuilder &B) { 1361 if (Attrs != B.Attrs) 1362 return false; 1363 1364 for (td_const_iterator I = TargetDepAttrs.begin(), 1365 E = TargetDepAttrs.end(); I != E; ++I) 1366 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end()) 1367 return false; 1368 1369 return Alignment == B.Alignment && StackAlignment == B.StackAlignment && 1370 DerefBytes == B.DerefBytes; 1371 } 1372 1373 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) { 1374 // FIXME: Remove this in 4.0. 1375 if (!Val) return *this; 1376 1377 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds; 1378 I = Attribute::AttrKind(I + 1)) { 1379 if (I == Attribute::Dereferenceable || 1380 I == Attribute::DereferenceableOrNull || 1381 I == Attribute::ArgMemOnly) 1382 continue; 1383 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) { 1384 Attrs[I] = true; 1385 1386 if (I == Attribute::Alignment) 1387 Alignment = 1ULL << ((A >> 16) - 1); 1388 else if (I == Attribute::StackAlignment) 1389 StackAlignment = 1ULL << ((A >> 26)-1); 1390 } 1391 } 1392 1393 return *this; 1394 } 1395 1396 //===----------------------------------------------------------------------===// 1397 // AttributeFuncs Function Defintions 1398 //===----------------------------------------------------------------------===// 1399 1400 /// \brief Which attributes cannot be applied to a type. 1401 AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) { 1402 AttrBuilder Incompatible; 1403 1404 if (!Ty->isIntegerTy()) 1405 // Attribute that only apply to integers. 1406 Incompatible.addAttribute(Attribute::SExt) 1407 .addAttribute(Attribute::ZExt); 1408 1409 if (!Ty->isPointerTy()) 1410 // Attribute that only apply to pointers. 1411 Incompatible.addAttribute(Attribute::ByVal) 1412 .addAttribute(Attribute::Nest) 1413 .addAttribute(Attribute::NoAlias) 1414 .addAttribute(Attribute::NoCapture) 1415 .addAttribute(Attribute::NonNull) 1416 .addDereferenceableAttr(1) // the int here is ignored 1417 .addDereferenceableOrNullAttr(1) // the int here is ignored 1418 .addAttribute(Attribute::ReadNone) 1419 .addAttribute(Attribute::ReadOnly) 1420 .addAttribute(Attribute::StructRet) 1421 .addAttribute(Attribute::InAlloca); 1422 1423 return Incompatible; 1424 } 1425 1426 template<typename AttrClass> 1427 static bool isEqual(const Function &Caller, const Function &Callee) { 1428 return Caller.getFnAttribute(AttrClass::getKind()) == 1429 Callee.getFnAttribute(AttrClass::getKind()); 1430 } 1431 1432 /// \brief Compute the logical AND of the attributes of the caller and the 1433 /// callee. 1434 /// 1435 /// This function sets the caller's attribute to false if the callee's attribute 1436 /// is false. 1437 template<typename AttrClass> 1438 static void setAND(Function &Caller, const Function &Callee) { 1439 if (AttrClass::isSet(Caller, AttrClass::getKind()) && 1440 !AttrClass::isSet(Callee, AttrClass::getKind())) 1441 AttrClass::set(Caller, AttrClass::getKind(), false); 1442 } 1443 1444 /// \brief Compute the logical OR of the attributes of the caller and the 1445 /// callee. 1446 /// 1447 /// This function sets the caller's attribute to true if the callee's attribute 1448 /// is true. 1449 template<typename AttrClass> 1450 static void setOR(Function &Caller, const Function &Callee) { 1451 if (!AttrClass::isSet(Caller, AttrClass::getKind()) && 1452 AttrClass::isSet(Callee, AttrClass::getKind())) 1453 AttrClass::set(Caller, AttrClass::getKind(), true); 1454 } 1455 1456 /// \brief If the inlined function had a higher stack protection level than the 1457 /// calling function, then bump up the caller's stack protection level. 1458 static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) { 1459 // If upgrading the SSP attribute, clear out the old SSP Attributes first. 1460 // Having multiple SSP attributes doesn't actually hurt, but it adds useless 1461 // clutter to the IR. 1462 AttrBuilder B; 1463 B.addAttribute(Attribute::StackProtect) 1464 .addAttribute(Attribute::StackProtectStrong) 1465 .addAttribute(Attribute::StackProtectReq); 1466 AttributeSet OldSSPAttr = AttributeSet::get(Caller.getContext(), 1467 AttributeSet::FunctionIndex, 1468 B); 1469 1470 if (Callee.hasFnAttribute(Attribute::SafeStack)) { 1471 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr); 1472 Caller.addFnAttr(Attribute::SafeStack); 1473 } else if (Callee.hasFnAttribute(Attribute::StackProtectReq) && 1474 !Caller.hasFnAttribute(Attribute::SafeStack)) { 1475 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr); 1476 Caller.addFnAttr(Attribute::StackProtectReq); 1477 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) && 1478 !Caller.hasFnAttribute(Attribute::SafeStack) && 1479 !Caller.hasFnAttribute(Attribute::StackProtectReq)) { 1480 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr); 1481 Caller.addFnAttr(Attribute::StackProtectStrong); 1482 } else if (Callee.hasFnAttribute(Attribute::StackProtect) && 1483 !Caller.hasFnAttribute(Attribute::SafeStack) && 1484 !Caller.hasFnAttribute(Attribute::StackProtectReq) && 1485 !Caller.hasFnAttribute(Attribute::StackProtectStrong)) 1486 Caller.addFnAttr(Attribute::StackProtect); 1487 } 1488 1489 #define GET_ATTR_COMPAT_FUNC 1490 #include "AttributesCompatFunc.inc" 1491 1492 bool AttributeFuncs::areInlineCompatible(const Function &Caller, 1493 const Function &Callee) { 1494 return hasCompatibleFnAttrs(Caller, Callee); 1495 } 1496 1497 1498 void AttributeFuncs::mergeAttributesForInlining(Function &Caller, 1499 const Function &Callee) { 1500 mergeFnAttrs(Caller, Callee); 1501 } 1502