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