342 case TargetCXXABI::GenericItanium: 343 if (CGM.getContext().getTargetInfo().getTriple().getArch() 344 == llvm::Triple::le32) { 345 // For PNaCl, use ARM-style method pointers so that PNaCl code 346 // does not assume anything about the alignment of function 347 // pointers. 348 return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true, 349 /* UseARMGuardVarABI = */ false); 350 } 351 return new ItaniumCXXABI(CGM); 352 353 case TargetCXXABI::Microsoft: 354 llvm_unreachable("Microsoft ABI is not Itanium-based"); 355 } 356 llvm_unreachable("bad ABI kind"); 357} 358 359llvm::Type * 360ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) { 361 if (MPT->isMemberDataPointer()) 362 return CGM.PtrDiffTy; 363 return llvm::StructType::get(CGM.PtrDiffTy, CGM.PtrDiffTy, nullptr); 364} 365 366/// In the Itanium and ARM ABIs, method pointers have the form: 367/// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr; 368/// 369/// In the Itanium ABI: 370/// - method pointers are virtual if (memptr.ptr & 1) is nonzero 371/// - the this-adjustment is (memptr.adj) 372/// - the virtual offset is (memptr.ptr - 1) 373/// 374/// In the ARM ABI: 375/// - method pointers are virtual if (memptr.adj & 1) is nonzero 376/// - the this-adjustment is (memptr.adj >> 1) 377/// - the virtual offset is (memptr.ptr) 378/// ARM uses 'adj' for the virtual flag because Thumb functions 379/// may be only single-byte aligned. 380/// 381/// If the member is virtual, the adjusted 'this' pointer points 382/// to a vtable pointer from which the virtual offset is applied. 383/// 384/// If the member is non-virtual, memptr.ptr is the address of 385/// the function to call. 386llvm::Value *ItaniumCXXABI::EmitLoadOfMemberFunctionPointer( 387 CodeGenFunction &CGF, const Expr *E, llvm::Value *&This, 388 llvm::Value *MemFnPtr, const MemberPointerType *MPT) { 389 CGBuilderTy &Builder = CGF.Builder; 390 391 const FunctionProtoType *FPT = 392 MPT->getPointeeType()->getAs<FunctionProtoType>(); 393 const CXXRecordDecl *RD = 394 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); 395 396 llvm::FunctionType *FTy = 397 CGM.getTypes().GetFunctionType( 398 CGM.getTypes().arrangeCXXMethodType(RD, FPT)); 399 400 llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(CGM.PtrDiffTy, 1); 401 402 llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual"); 403 llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual"); 404 llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end"); 405 406 // Extract memptr.adj, which is in the second field. 407 llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj"); 408 409 // Compute the true adjustment. 410 llvm::Value *Adj = RawAdj; 411 if (UseARMMethodPtrABI) 412 Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted"); 413 414 // Apply the adjustment and cast back to the original struct type 415 // for consistency. 416 llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy()); 417 Ptr = Builder.CreateInBoundsGEP(Ptr, Adj); 418 This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted"); 419 420 // Load the function pointer. 421 llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr"); 422 423 // If the LSB in the function pointer is 1, the function pointer points to 424 // a virtual function. 425 llvm::Value *IsVirtual; 426 if (UseARMMethodPtrABI) 427 IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1); 428 else 429 IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1); 430 IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual"); 431 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual); 432 433 // In the virtual path, the adjustment left 'This' pointing to the 434 // vtable of the correct base subobject. The "function pointer" is an 435 // offset within the vtable (+1 for the virtual flag on non-ARM). 436 CGF.EmitBlock(FnVirtual); 437 438 // Cast the adjusted this to a pointer to vtable pointer and load. 439 llvm::Type *VTableTy = Builder.getInt8PtrTy(); 440 llvm::Value *VTable = CGF.GetVTablePtr(This, VTableTy); 441 442 // Apply the offset. 443 llvm::Value *VTableOffset = FnAsInt; 444 if (!UseARMMethodPtrABI) 445 VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1); 446 VTable = Builder.CreateGEP(VTable, VTableOffset); 447 448 // Load the virtual function to call. 449 VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo()); 450 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn"); 451 CGF.EmitBranch(FnEnd); 452 453 // In the non-virtual path, the function pointer is actually a 454 // function pointer. 455 CGF.EmitBlock(FnNonVirtual); 456 llvm::Value *NonVirtualFn = 457 Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn"); 458 459 // We're done. 460 CGF.EmitBlock(FnEnd); 461 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo(), 2); 462 Callee->addIncoming(VirtualFn, FnVirtual); 463 Callee->addIncoming(NonVirtualFn, FnNonVirtual); 464 return Callee; 465} 466 467/// Compute an l-value by applying the given pointer-to-member to a 468/// base object. 469llvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress( 470 CodeGenFunction &CGF, const Expr *E, llvm::Value *Base, llvm::Value *MemPtr, 471 const MemberPointerType *MPT) { 472 assert(MemPtr->getType() == CGM.PtrDiffTy); 473 474 CGBuilderTy &Builder = CGF.Builder; 475 476 unsigned AS = Base->getType()->getPointerAddressSpace(); 477 478 // Cast to char*. 479 Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS)); 480 481 // Apply the offset, which we assume is non-null. 482 llvm::Value *Addr = Builder.CreateInBoundsGEP(Base, MemPtr, "memptr.offset"); 483 484 // Cast the address to the appropriate pointer type, adopting the 485 // address space of the base pointer. 486 llvm::Type *PType 487 = CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS); 488 return Builder.CreateBitCast(Addr, PType); 489} 490 491/// Perform a bitcast, derived-to-base, or base-to-derived member pointer 492/// conversion. 493/// 494/// Bitcast conversions are always a no-op under Itanium. 495/// 496/// Obligatory offset/adjustment diagram: 497/// <-- offset --> <-- adjustment --> 498/// |--------------------------|----------------------|--------------------| 499/// ^Derived address point ^Base address point ^Member address point 500/// 501/// So when converting a base member pointer to a derived member pointer, 502/// we add the offset to the adjustment because the address point has 503/// decreased; and conversely, when converting a derived MP to a base MP 504/// we subtract the offset from the adjustment because the address point 505/// has increased. 506/// 507/// The standard forbids (at compile time) conversion to and from 508/// virtual bases, which is why we don't have to consider them here. 509/// 510/// The standard forbids (at run time) casting a derived MP to a base 511/// MP when the derived MP does not point to a member of the base. 512/// This is why -1 is a reasonable choice for null data member 513/// pointers. 514llvm::Value * 515ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF, 516 const CastExpr *E, 517 llvm::Value *src) { 518 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || 519 E->getCastKind() == CK_BaseToDerivedMemberPointer || 520 E->getCastKind() == CK_ReinterpretMemberPointer); 521 522 // Under Itanium, reinterprets don't require any additional processing. 523 if (E->getCastKind() == CK_ReinterpretMemberPointer) return src; 524 525 // Use constant emission if we can. 526 if (isa<llvm::Constant>(src)) 527 return EmitMemberPointerConversion(E, cast<llvm::Constant>(src)); 528 529 llvm::Constant *adj = getMemberPointerAdjustment(E); 530 if (!adj) return src; 531 532 CGBuilderTy &Builder = CGF.Builder; 533 bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); 534 535 const MemberPointerType *destTy = 536 E->getType()->castAs<MemberPointerType>(); 537 538 // For member data pointers, this is just a matter of adding the 539 // offset if the source is non-null. 540 if (destTy->isMemberDataPointer()) { 541 llvm::Value *dst; 542 if (isDerivedToBase) 543 dst = Builder.CreateNSWSub(src, adj, "adj"); 544 else 545 dst = Builder.CreateNSWAdd(src, adj, "adj"); 546 547 // Null check. 548 llvm::Value *null = llvm::Constant::getAllOnesValue(src->getType()); 549 llvm::Value *isNull = Builder.CreateICmpEQ(src, null, "memptr.isnull"); 550 return Builder.CreateSelect(isNull, src, dst); 551 } 552 553 // The this-adjustment is left-shifted by 1 on ARM. 554 if (UseARMMethodPtrABI) { 555 uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue(); 556 offset <<= 1; 557 adj = llvm::ConstantInt::get(adj->getType(), offset); 558 } 559 560 llvm::Value *srcAdj = Builder.CreateExtractValue(src, 1, "src.adj"); 561 llvm::Value *dstAdj; 562 if (isDerivedToBase) 563 dstAdj = Builder.CreateNSWSub(srcAdj, adj, "adj"); 564 else 565 dstAdj = Builder.CreateNSWAdd(srcAdj, adj, "adj"); 566 567 return Builder.CreateInsertValue(src, dstAdj, 1); 568} 569 570llvm::Constant * 571ItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E, 572 llvm::Constant *src) { 573 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || 574 E->getCastKind() == CK_BaseToDerivedMemberPointer || 575 E->getCastKind() == CK_ReinterpretMemberPointer); 576 577 // Under Itanium, reinterprets don't require any additional processing. 578 if (E->getCastKind() == CK_ReinterpretMemberPointer) return src; 579 580 // If the adjustment is trivial, we don't need to do anything. 581 llvm::Constant *adj = getMemberPointerAdjustment(E); 582 if (!adj) return src; 583 584 bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); 585 586 const MemberPointerType *destTy = 587 E->getType()->castAs<MemberPointerType>(); 588 589 // For member data pointers, this is just a matter of adding the 590 // offset if the source is non-null. 591 if (destTy->isMemberDataPointer()) { 592 // null maps to null. 593 if (src->isAllOnesValue()) return src; 594 595 if (isDerivedToBase) 596 return llvm::ConstantExpr::getNSWSub(src, adj); 597 else 598 return llvm::ConstantExpr::getNSWAdd(src, adj); 599 } 600 601 // The this-adjustment is left-shifted by 1 on ARM. 602 if (UseARMMethodPtrABI) { 603 uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue(); 604 offset <<= 1; 605 adj = llvm::ConstantInt::get(adj->getType(), offset); 606 } 607 608 llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1); 609 llvm::Constant *dstAdj; 610 if (isDerivedToBase) 611 dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj); 612 else 613 dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj); 614 615 return llvm::ConstantExpr::getInsertValue(src, dstAdj, 1); 616} 617 618llvm::Constant * 619ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) { 620 // Itanium C++ ABI 2.3: 621 // A NULL pointer is represented as -1. 622 if (MPT->isMemberDataPointer()) 623 return llvm::ConstantInt::get(CGM.PtrDiffTy, -1ULL, /*isSigned=*/true); 624 625 llvm::Constant *Zero = llvm::ConstantInt::get(CGM.PtrDiffTy, 0); 626 llvm::Constant *Values[2] = { Zero, Zero }; 627 return llvm::ConstantStruct::getAnon(Values); 628} 629 630llvm::Constant * 631ItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT, 632 CharUnits offset) { 633 // Itanium C++ ABI 2.3: 634 // A pointer to data member is an offset from the base address of 635 // the class object containing it, represented as a ptrdiff_t 636 return llvm::ConstantInt::get(CGM.PtrDiffTy, offset.getQuantity()); 637} 638 639llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) { 640 return BuildMemberPointer(MD, CharUnits::Zero()); 641} 642 643llvm::Constant *ItaniumCXXABI::BuildMemberPointer(const CXXMethodDecl *MD, 644 CharUnits ThisAdjustment) { 645 assert(MD->isInstance() && "Member function must not be static!"); 646 MD = MD->getCanonicalDecl(); 647 648 CodeGenTypes &Types = CGM.getTypes(); 649 650 // Get the function pointer (or index if this is a virtual function). 651 llvm::Constant *MemPtr[2]; 652 if (MD->isVirtual()) { 653 uint64_t Index = CGM.getItaniumVTableContext().getMethodVTableIndex(MD); 654 655 const ASTContext &Context = getContext(); 656 CharUnits PointerWidth = 657 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); 658 uint64_t VTableOffset = (Index * PointerWidth.getQuantity()); 659 660 if (UseARMMethodPtrABI) { 661 // ARM C++ ABI 3.2.1: 662 // This ABI specifies that adj contains twice the this 663 // adjustment, plus 1 if the member function is virtual. The 664 // least significant bit of adj then makes exactly the same 665 // discrimination as the least significant bit of ptr does for 666 // Itanium. 667 MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset); 668 MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy, 669 2 * ThisAdjustment.getQuantity() + 1); 670 } else { 671 // Itanium C++ ABI 2.3: 672 // For a virtual function, [the pointer field] is 1 plus the 673 // virtual table offset (in bytes) of the function, 674 // represented as a ptrdiff_t. 675 MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset + 1); 676 MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy, 677 ThisAdjustment.getQuantity()); 678 } 679 } else { 680 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); 681 llvm::Type *Ty; 682 // Check whether the function has a computable LLVM signature. 683 if (Types.isFuncTypeConvertible(FPT)) { 684 // The function has a computable LLVM signature; use the correct type. 685 Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD)); 686 } else { 687 // Use an arbitrary non-function type to tell GetAddrOfFunction that the 688 // function type is incomplete. 689 Ty = CGM.PtrDiffTy; 690 } 691 llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty); 692 693 MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, CGM.PtrDiffTy); 694 MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy, 695 (UseARMMethodPtrABI ? 2 : 1) * 696 ThisAdjustment.getQuantity()); 697 } 698 699 return llvm::ConstantStruct::getAnon(MemPtr); 700} 701 702llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const APValue &MP, 703 QualType MPType) { 704 const MemberPointerType *MPT = MPType->castAs<MemberPointerType>(); 705 const ValueDecl *MPD = MP.getMemberPointerDecl(); 706 if (!MPD) 707 return EmitNullMemberPointer(MPT); 708 709 CharUnits ThisAdjustment = getMemberPointerPathAdjustment(MP); 710 711 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD)) 712 return BuildMemberPointer(MD, ThisAdjustment); 713 714 CharUnits FieldOffset = 715 getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD)); 716 return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset); 717} 718 719/// The comparison algorithm is pretty easy: the member pointers are 720/// the same if they're either bitwise identical *or* both null. 721/// 722/// ARM is different here only because null-ness is more complicated. 723llvm::Value * 724ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF, 725 llvm::Value *L, 726 llvm::Value *R, 727 const MemberPointerType *MPT, 728 bool Inequality) { 729 CGBuilderTy &Builder = CGF.Builder; 730 731 llvm::ICmpInst::Predicate Eq; 732 llvm::Instruction::BinaryOps And, Or; 733 if (Inequality) { 734 Eq = llvm::ICmpInst::ICMP_NE; 735 And = llvm::Instruction::Or; 736 Or = llvm::Instruction::And; 737 } else { 738 Eq = llvm::ICmpInst::ICMP_EQ; 739 And = llvm::Instruction::And; 740 Or = llvm::Instruction::Or; 741 } 742 743 // Member data pointers are easy because there's a unique null 744 // value, so it just comes down to bitwise equality. 745 if (MPT->isMemberDataPointer()) 746 return Builder.CreateICmp(Eq, L, R); 747 748 // For member function pointers, the tautologies are more complex. 749 // The Itanium tautology is: 750 // (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj)) 751 // The ARM tautology is: 752 // (L == R) <==> (L.ptr == R.ptr && 753 // (L.adj == R.adj || 754 // (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0))) 755 // The inequality tautologies have exactly the same structure, except 756 // applying De Morgan's laws. 757 758 llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr"); 759 llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr"); 760 761 // This condition tests whether L.ptr == R.ptr. This must always be 762 // true for equality to hold. 763 llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr"); 764 765 // This condition, together with the assumption that L.ptr == R.ptr, 766 // tests whether the pointers are both null. ARM imposes an extra 767 // condition. 768 llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType()); 769 llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null"); 770 771 // This condition tests whether L.adj == R.adj. If this isn't 772 // true, the pointers are unequal unless they're both null. 773 llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj"); 774 llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj"); 775 llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj"); 776 777 // Null member function pointers on ARM clear the low bit of Adj, 778 // so the zero condition has to check that neither low bit is set. 779 if (UseARMMethodPtrABI) { 780 llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1); 781 782 // Compute (l.adj | r.adj) & 1 and test it against zero. 783 llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj"); 784 llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One); 785 llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero, 786 "cmp.or.adj"); 787 EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero); 788 } 789 790 // Tie together all our conditions. 791 llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq); 792 Result = Builder.CreateBinOp(And, PtrEq, Result, 793 Inequality ? "memptr.ne" : "memptr.eq"); 794 return Result; 795} 796 797llvm::Value * 798ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF, 799 llvm::Value *MemPtr, 800 const MemberPointerType *MPT) { 801 CGBuilderTy &Builder = CGF.Builder; 802 803 /// For member data pointers, this is just a check against -1. 804 if (MPT->isMemberDataPointer()) { 805 assert(MemPtr->getType() == CGM.PtrDiffTy); 806 llvm::Value *NegativeOne = 807 llvm::Constant::getAllOnesValue(MemPtr->getType()); 808 return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool"); 809 } 810 811 // In Itanium, a member function pointer is not null if 'ptr' is not null. 812 llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr"); 813 814 llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0); 815 llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool"); 816 817 // On ARM, a member function pointer is also non-null if the low bit of 'adj' 818 // (the virtual bit) is set. 819 if (UseARMMethodPtrABI) { 820 llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1); 821 llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj"); 822 llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit"); 823 llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero, 824 "memptr.isvirtual"); 825 Result = Builder.CreateOr(Result, IsVirtual); 826 } 827 828 return Result; 829} 830 831bool ItaniumCXXABI::classifyReturnType(CGFunctionInfo &FI) const { 832 const CXXRecordDecl *RD = FI.getReturnType()->getAsCXXRecordDecl(); 833 if (!RD) 834 return false; 835 836 // Return indirectly if we have a non-trivial copy ctor or non-trivial dtor. 837 // FIXME: Use canCopyArgument() when it is fixed to handle lazily declared 838 // special members. 839 if (RD->hasNonTrivialDestructor() || RD->hasNonTrivialCopyConstructor()) { 840 FI.getReturnInfo() = ABIArgInfo::getIndirect(0, /*ByVal=*/false); 841 return true; 842 } 843 return false; 844} 845 846/// The Itanium ABI requires non-zero initialization only for data 847/// member pointers, for which '0' is a valid offset. 848bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) { 849 return MPT->getPointeeType()->isFunctionType(); 850} 851 852/// The Itanium ABI always places an offset to the complete object 853/// at entry -2 in the vtable. 854void ItaniumCXXABI::emitVirtualObjectDelete(CodeGenFunction &CGF, 855 const CXXDeleteExpr *DE, 856 llvm::Value *Ptr, 857 QualType ElementType, 858 const CXXDestructorDecl *Dtor) { 859 bool UseGlobalDelete = DE->isGlobalDelete(); 860 if (UseGlobalDelete) { 861 // Derive the complete-object pointer, which is what we need 862 // to pass to the deallocation function. 863 864 // Grab the vtable pointer as an intptr_t*. 865 llvm::Value *VTable = CGF.GetVTablePtr(Ptr, CGF.IntPtrTy->getPointerTo()); 866 867 // Track back to entry -2 and pull out the offset there. 868 llvm::Value *OffsetPtr = CGF.Builder.CreateConstInBoundsGEP1_64( 869 VTable, -2, "complete-offset.ptr"); 870 llvm::LoadInst *Offset = CGF.Builder.CreateLoad(OffsetPtr); 871 Offset->setAlignment(CGF.PointerAlignInBytes); 872 873 // Apply the offset. 874 llvm::Value *CompletePtr = CGF.Builder.CreateBitCast(Ptr, CGF.Int8PtrTy); 875 CompletePtr = CGF.Builder.CreateInBoundsGEP(CompletePtr, Offset); 876 877 // If we're supposed to call the global delete, make sure we do so 878 // even if the destructor throws. 879 CGF.pushCallObjectDeleteCleanup(DE->getOperatorDelete(), CompletePtr, 880 ElementType); 881 } 882 883 // FIXME: Provide a source location here even though there's no 884 // CXXMemberCallExpr for dtor call. 885 CXXDtorType DtorType = UseGlobalDelete ? Dtor_Complete : Dtor_Deleting; 886 EmitVirtualDestructorCall(CGF, Dtor, DtorType, Ptr, /*CE=*/nullptr); 887 888 if (UseGlobalDelete) 889 CGF.PopCleanupBlock(); 890} 891 892void ItaniumCXXABI::emitRethrow(CodeGenFunction &CGF, bool isNoReturn) { 893 // void __cxa_rethrow(); 894 895 llvm::FunctionType *FTy = 896 llvm::FunctionType::get(CGM.VoidTy, /*IsVarArgs=*/false); 897 898 llvm::Constant *Fn = CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow"); 899 900 if (isNoReturn) 901 CGF.EmitNoreturnRuntimeCallOrInvoke(Fn, None); 902 else 903 CGF.EmitRuntimeCallOrInvoke(Fn); 904} 905 906static llvm::Constant *getItaniumDynamicCastFn(CodeGenFunction &CGF) { 907 // void *__dynamic_cast(const void *sub, 908 // const abi::__class_type_info *src, 909 // const abi::__class_type_info *dst, 910 // std::ptrdiff_t src2dst_offset); 911 912 llvm::Type *Int8PtrTy = CGF.Int8PtrTy; 913 llvm::Type *PtrDiffTy = 914 CGF.ConvertType(CGF.getContext().getPointerDiffType()); 915 916 llvm::Type *Args[4] = { Int8PtrTy, Int8PtrTy, Int8PtrTy, PtrDiffTy }; 917 918 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false); 919 920 // Mark the function as nounwind readonly. 921 llvm::Attribute::AttrKind FuncAttrs[] = { llvm::Attribute::NoUnwind, 922 llvm::Attribute::ReadOnly }; 923 llvm::AttributeSet Attrs = llvm::AttributeSet::get( 924 CGF.getLLVMContext(), llvm::AttributeSet::FunctionIndex, FuncAttrs); 925 926 return CGF.CGM.CreateRuntimeFunction(FTy, "__dynamic_cast", Attrs); 927} 928 929static llvm::Constant *getBadCastFn(CodeGenFunction &CGF) { 930 // void __cxa_bad_cast(); 931 llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false); 932 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_cast"); 933} 934 935/// \brief Compute the src2dst_offset hint as described in the 936/// Itanium C++ ABI [2.9.7] 937static CharUnits computeOffsetHint(ASTContext &Context, 938 const CXXRecordDecl *Src, 939 const CXXRecordDecl *Dst) { 940 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 941 /*DetectVirtual=*/false); 942 943 // If Dst is not derived from Src we can skip the whole computation below and 944 // return that Src is not a public base of Dst. Record all inheritance paths. 945 if (!Dst->isDerivedFrom(Src, Paths)) 946 return CharUnits::fromQuantity(-2ULL); 947 948 unsigned NumPublicPaths = 0; 949 CharUnits Offset; 950 951 // Now walk all possible inheritance paths. 952 for (CXXBasePaths::paths_iterator I = Paths.begin(), E = Paths.end(); I != E; 953 ++I) { 954 if (I->Access != AS_public) // Ignore non-public inheritance. 955 continue; 956 957 ++NumPublicPaths; 958 959 for (CXXBasePath::iterator J = I->begin(), JE = I->end(); J != JE; ++J) { 960 // If the path contains a virtual base class we can't give any hint. 961 // -1: no hint. 962 if (J->Base->isVirtual()) 963 return CharUnits::fromQuantity(-1ULL); 964 965 if (NumPublicPaths > 1) // Won't use offsets, skip computation. 966 continue; 967 968 // Accumulate the base class offsets. 969 const ASTRecordLayout &L = Context.getASTRecordLayout(J->Class); 970 Offset += L.getBaseClassOffset(J->Base->getType()->getAsCXXRecordDecl()); 971 } 972 } 973 974 // -2: Src is not a public base of Dst. 975 if (NumPublicPaths == 0) 976 return CharUnits::fromQuantity(-2ULL); 977 978 // -3: Src is a multiple public base type but never a virtual base type. 979 if (NumPublicPaths > 1) 980 return CharUnits::fromQuantity(-3ULL); 981 982 // Otherwise, the Src type is a unique public nonvirtual base type of Dst. 983 // Return the offset of Src from the origin of Dst. 984 return Offset; 985} 986 987static llvm::Constant *getBadTypeidFn(CodeGenFunction &CGF) { 988 // void __cxa_bad_typeid(); 989 llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false); 990 991 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid"); 992} 993 994bool ItaniumCXXABI::shouldTypeidBeNullChecked(bool IsDeref, 995 QualType SrcRecordTy) { 996 return IsDeref; 997} 998 999void ItaniumCXXABI::EmitBadTypeidCall(CodeGenFunction &CGF) { 1000 llvm::Value *Fn = getBadTypeidFn(CGF); 1001 CGF.EmitRuntimeCallOrInvoke(Fn).setDoesNotReturn(); 1002 CGF.Builder.CreateUnreachable(); 1003} 1004 1005llvm::Value *ItaniumCXXABI::EmitTypeid(CodeGenFunction &CGF, 1006 QualType SrcRecordTy, 1007 llvm::Value *ThisPtr, 1008 llvm::Type *StdTypeInfoPtrTy) { 1009 llvm::Value *Value = 1010 CGF.GetVTablePtr(ThisPtr, StdTypeInfoPtrTy->getPointerTo()); 1011 1012 // Load the type info. 1013 Value = CGF.Builder.CreateConstInBoundsGEP1_64(Value, -1ULL); 1014 return CGF.Builder.CreateLoad(Value); 1015} 1016 1017bool ItaniumCXXABI::shouldDynamicCastCallBeNullChecked(bool SrcIsPtr, 1018 QualType SrcRecordTy) { 1019 return SrcIsPtr; 1020} 1021 1022llvm::Value *ItaniumCXXABI::EmitDynamicCastCall( 1023 CodeGenFunction &CGF, llvm::Value *Value, QualType SrcRecordTy, 1024 QualType DestTy, QualType DestRecordTy, llvm::BasicBlock *CastEnd) { 1025 llvm::Type *PtrDiffLTy = 1026 CGF.ConvertType(CGF.getContext().getPointerDiffType()); 1027 llvm::Type *DestLTy = CGF.ConvertType(DestTy); 1028 1029 llvm::Value *SrcRTTI = 1030 CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType()); 1031 llvm::Value *DestRTTI = 1032 CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType()); 1033 1034 // Compute the offset hint. 1035 const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl(); 1036 const CXXRecordDecl *DestDecl = DestRecordTy->getAsCXXRecordDecl(); 1037 llvm::Value *OffsetHint = llvm::ConstantInt::get( 1038 PtrDiffLTy, 1039 computeOffsetHint(CGF.getContext(), SrcDecl, DestDecl).getQuantity()); 1040 1041 // Emit the call to __dynamic_cast. 1042 Value = CGF.EmitCastToVoidPtr(Value); 1043 1044 llvm::Value *args[] = {Value, SrcRTTI, DestRTTI, OffsetHint}; 1045 Value = CGF.EmitNounwindRuntimeCall(getItaniumDynamicCastFn(CGF), args); 1046 Value = CGF.Builder.CreateBitCast(Value, DestLTy); 1047 1048 /// C++ [expr.dynamic.cast]p9: 1049 /// A failed cast to reference type throws std::bad_cast 1050 if (DestTy->isReferenceType()) { 1051 llvm::BasicBlock *BadCastBlock = 1052 CGF.createBasicBlock("dynamic_cast.bad_cast"); 1053 1054 llvm::Value *IsNull = CGF.Builder.CreateIsNull(Value); 1055 CGF.Builder.CreateCondBr(IsNull, BadCastBlock, CastEnd); 1056 1057 CGF.EmitBlock(BadCastBlock); 1058 EmitBadCastCall(CGF); 1059 } 1060 1061 return Value; 1062} 1063 1064llvm::Value *ItaniumCXXABI::EmitDynamicCastToVoid(CodeGenFunction &CGF, 1065 llvm::Value *Value, 1066 QualType SrcRecordTy, 1067 QualType DestTy) { 1068 llvm::Type *PtrDiffLTy = 1069 CGF.ConvertType(CGF.getContext().getPointerDiffType()); 1070 llvm::Type *DestLTy = CGF.ConvertType(DestTy); 1071 1072 // Get the vtable pointer. 1073 llvm::Value *VTable = CGF.GetVTablePtr(Value, PtrDiffLTy->getPointerTo()); 1074 1075 // Get the offset-to-top from the vtable. 1076 llvm::Value *OffsetToTop = 1077 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, -2ULL); 1078 OffsetToTop = CGF.Builder.CreateLoad(OffsetToTop, "offset.to.top"); 1079 1080 // Finally, add the offset to the pointer. 1081 Value = CGF.EmitCastToVoidPtr(Value); 1082 Value = CGF.Builder.CreateInBoundsGEP(Value, OffsetToTop); 1083 1084 return CGF.Builder.CreateBitCast(Value, DestLTy); 1085} 1086 1087bool ItaniumCXXABI::EmitBadCastCall(CodeGenFunction &CGF) { 1088 llvm::Value *Fn = getBadCastFn(CGF); 1089 CGF.EmitRuntimeCallOrInvoke(Fn).setDoesNotReturn(); 1090 CGF.Builder.CreateUnreachable(); 1091 return true; 1092} 1093 1094llvm::Value * 1095ItaniumCXXABI::GetVirtualBaseClassOffset(CodeGenFunction &CGF, 1096 llvm::Value *This, 1097 const CXXRecordDecl *ClassDecl, 1098 const CXXRecordDecl *BaseClassDecl) { 1099 llvm::Value *VTablePtr = CGF.GetVTablePtr(This, CGM.Int8PtrTy); 1100 CharUnits VBaseOffsetOffset = 1101 CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(ClassDecl, 1102 BaseClassDecl); 1103 1104 llvm::Value *VBaseOffsetPtr = 1105 CGF.Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetOffset.getQuantity(), 1106 "vbase.offset.ptr"); 1107 VBaseOffsetPtr = CGF.Builder.CreateBitCast(VBaseOffsetPtr, 1108 CGM.PtrDiffTy->getPointerTo()); 1109 1110 llvm::Value *VBaseOffset = 1111 CGF.Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset"); 1112 1113 return VBaseOffset; 1114} 1115 1116void ItaniumCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) { 1117 // Just make sure we're in sync with TargetCXXABI. 1118 assert(CGM.getTarget().getCXXABI().hasConstructorVariants()); 1119 1120 // The constructor used for constructing this as a base class; 1121 // ignores virtual bases. 1122 CGM.EmitGlobal(GlobalDecl(D, Ctor_Base)); 1123 1124 // The constructor used for constructing this as a complete class; 1125 // constructs the virtual bases, then calls the base constructor. 1126 if (!D->getParent()->isAbstract()) { 1127 // We don't need to emit the complete ctor if the class is abstract. 1128 CGM.EmitGlobal(GlobalDecl(D, Ctor_Complete)); 1129 } 1130} 1131 1132void 1133ItaniumCXXABI::buildStructorSignature(const CXXMethodDecl *MD, StructorType T, 1134 SmallVectorImpl<CanQualType> &ArgTys) { 1135 ASTContext &Context = getContext(); 1136 1137 // All parameters are already in place except VTT, which goes after 'this'. 1138 // These are Clang types, so we don't need to worry about sret yet. 1139 1140 // Check if we need to add a VTT parameter (which has type void **). 1141 if (T == StructorType::Base && MD->getParent()->getNumVBases() != 0) 1142 ArgTys.insert(ArgTys.begin() + 1, 1143 Context.getPointerType(Context.VoidPtrTy)); 1144} 1145 1146void ItaniumCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) { 1147 // The destructor used for destructing this as a base class; ignores 1148 // virtual bases. 1149 CGM.EmitGlobal(GlobalDecl(D, Dtor_Base)); 1150 1151 // The destructor used for destructing this as a most-derived class; 1152 // call the base destructor and then destructs any virtual bases. 1153 CGM.EmitGlobal(GlobalDecl(D, Dtor_Complete)); 1154 1155 // The destructor in a virtual table is always a 'deleting' 1156 // destructor, which calls the complete destructor and then uses the 1157 // appropriate operator delete. 1158 if (D->isVirtual()) 1159 CGM.EmitGlobal(GlobalDecl(D, Dtor_Deleting)); 1160} 1161 1162void ItaniumCXXABI::addImplicitStructorParams(CodeGenFunction &CGF, 1163 QualType &ResTy, 1164 FunctionArgList &Params) { 1165 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); 1166 assert(isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)); 1167 1168 // Check if we need a VTT parameter as well. 1169 if (NeedsVTTParameter(CGF.CurGD)) { 1170 ASTContext &Context = getContext(); 1171 1172 // FIXME: avoid the fake decl 1173 QualType T = Context.getPointerType(Context.VoidPtrTy); 1174 ImplicitParamDecl *VTTDecl 1175 = ImplicitParamDecl::Create(Context, nullptr, MD->getLocation(), 1176 &Context.Idents.get("vtt"), T); 1177 Params.insert(Params.begin() + 1, VTTDecl); 1178 getStructorImplicitParamDecl(CGF) = VTTDecl; 1179 } 1180} 1181 1182void ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) { 1183 /// Initialize the 'this' slot. 1184 EmitThisParam(CGF); 1185 1186 /// Initialize the 'vtt' slot if needed. 1187 if (getStructorImplicitParamDecl(CGF)) { 1188 getStructorImplicitParamValue(CGF) = CGF.Builder.CreateLoad( 1189 CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)), "vtt"); 1190 } 1191 1192 /// If this is a function that the ABI specifies returns 'this', initialize 1193 /// the return slot to 'this' at the start of the function. 1194 /// 1195 /// Unlike the setting of return types, this is done within the ABI 1196 /// implementation instead of by clients of CGCXXABI because: 1197 /// 1) getThisValue is currently protected 1198 /// 2) in theory, an ABI could implement 'this' returns some other way; 1199 /// HasThisReturn only specifies a contract, not the implementation 1200 if (HasThisReturn(CGF.CurGD)) 1201 CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue); 1202} 1203 1204unsigned ItaniumCXXABI::addImplicitConstructorArgs( 1205 CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type, 1206 bool ForVirtualBase, bool Delegating, CallArgList &Args) { 1207 if (!NeedsVTTParameter(GlobalDecl(D, Type))) 1208 return 0; 1209 1210 // Insert the implicit 'vtt' argument as the second argument. 1211 llvm::Value *VTT = 1212 CGF.GetVTTParameter(GlobalDecl(D, Type), ForVirtualBase, Delegating); 1213 QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy); 1214 Args.insert(Args.begin() + 1, 1215 CallArg(RValue::get(VTT), VTTTy, /*needscopy=*/false)); 1216 return 1; // Added one arg. 1217} 1218 1219void ItaniumCXXABI::EmitDestructorCall(CodeGenFunction &CGF, 1220 const CXXDestructorDecl *DD, 1221 CXXDtorType Type, bool ForVirtualBase, 1222 bool Delegating, llvm::Value *This) { 1223 GlobalDecl GD(DD, Type); 1224 llvm::Value *VTT = CGF.GetVTTParameter(GD, ForVirtualBase, Delegating); 1225 QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy); 1226 1227 llvm::Value *Callee = nullptr; 1228 if (getContext().getLangOpts().AppleKext) 1229 Callee = CGF.BuildAppleKextVirtualDestructorCall(DD, Type, DD->getParent()); 1230 1231 if (!Callee) 1232 Callee = CGM.getAddrOfCXXStructor(DD, getFromDtorType(Type)); 1233 1234 CGF.EmitCXXMemberOrOperatorCall(DD, Callee, ReturnValueSlot(), This, VTT, 1235 VTTTy, nullptr); 1236} 1237 1238void ItaniumCXXABI::emitVTableDefinitions(CodeGenVTables &CGVT, 1239 const CXXRecordDecl *RD) { 1240 llvm::GlobalVariable *VTable = getAddrOfVTable(RD, CharUnits()); 1241 if (VTable->hasInitializer()) 1242 return; 1243 1244 ItaniumVTableContext &VTContext = CGM.getItaniumVTableContext(); 1245 const VTableLayout &VTLayout = VTContext.getVTableLayout(RD); 1246 llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD); 1247 llvm::Constant *RTTI = 1248 CGM.GetAddrOfRTTIDescriptor(CGM.getContext().getTagDeclType(RD)); 1249 1250 // Create and set the initializer. 1251 llvm::Constant *Init = CGVT.CreateVTableInitializer( 1252 RD, VTLayout.vtable_component_begin(), VTLayout.getNumVTableComponents(), 1253 VTLayout.vtable_thunk_begin(), VTLayout.getNumVTableThunks(), RTTI); 1254 VTable->setInitializer(Init); 1255 1256 // Set the correct linkage. 1257 VTable->setLinkage(Linkage); 1258 1259 // Set the right visibility. 1260 CGM.setGlobalVisibility(VTable, RD); 1261 1262 // Use pointer alignment for the vtable. Otherwise we would align them based 1263 // on the size of the initializer which doesn't make sense as only single 1264 // values are read. 1265 unsigned PAlign = CGM.getTarget().getPointerAlign(0); 1266 VTable->setAlignment(getContext().toCharUnitsFromBits(PAlign).getQuantity()); 1267 1268 // If this is the magic class __cxxabiv1::__fundamental_type_info, 1269 // we will emit the typeinfo for the fundamental types. This is the 1270 // same behaviour as GCC. 1271 const DeclContext *DC = RD->getDeclContext(); 1272 if (RD->getIdentifier() && 1273 RD->getIdentifier()->isStr("__fundamental_type_info") && 1274 isa<NamespaceDecl>(DC) && cast<NamespaceDecl>(DC)->getIdentifier() && 1275 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") && 1276 DC->getParent()->isTranslationUnit()) 1277 EmitFundamentalRTTIDescriptors(); 1278} 1279 1280llvm::Value *ItaniumCXXABI::getVTableAddressPointInStructor( 1281 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base, 1282 const CXXRecordDecl *NearestVBase, bool &NeedsVirtualOffset) { 1283 bool NeedsVTTParam = CGM.getCXXABI().NeedsVTTParameter(CGF.CurGD); 1284 NeedsVirtualOffset = (NeedsVTTParam && NearestVBase); 1285 1286 llvm::Value *VTableAddressPoint; 1287 if (NeedsVTTParam && (Base.getBase()->getNumVBases() || NearestVBase)) { 1288 // Get the secondary vpointer index. 1289 uint64_t VirtualPointerIndex = 1290 CGM.getVTables().getSecondaryVirtualPointerIndex(VTableClass, Base); 1291 1292 /// Load the VTT. 1293 llvm::Value *VTT = CGF.LoadCXXVTT(); 1294 if (VirtualPointerIndex) 1295 VTT = CGF.Builder.CreateConstInBoundsGEP1_64(VTT, VirtualPointerIndex); 1296 1297 // And load the address point from the VTT. 1298 VTableAddressPoint = CGF.Builder.CreateLoad(VTT); 1299 } else { 1300 llvm::Constant *VTable = 1301 CGM.getCXXABI().getAddrOfVTable(VTableClass, CharUnits()); 1302 uint64_t AddressPoint = CGM.getItaniumVTableContext() 1303 .getVTableLayout(VTableClass) 1304 .getAddressPoint(Base); 1305 VTableAddressPoint = 1306 CGF.Builder.CreateConstInBoundsGEP2_64(VTable, 0, AddressPoint); 1307 } 1308 1309 return VTableAddressPoint; 1310} 1311 1312llvm::Constant *ItaniumCXXABI::getVTableAddressPointForConstExpr( 1313 BaseSubobject Base, const CXXRecordDecl *VTableClass) { 1314 llvm::Constant *VTable = getAddrOfVTable(VTableClass, CharUnits()); 1315 1316 // Find the appropriate vtable within the vtable group. 1317 uint64_t AddressPoint = CGM.getItaniumVTableContext() 1318 .getVTableLayout(VTableClass) 1319 .getAddressPoint(Base); 1320 llvm::Value *Indices[] = { 1321 llvm::ConstantInt::get(CGM.Int64Ty, 0), 1322 llvm::ConstantInt::get(CGM.Int64Ty, AddressPoint) 1323 }; 1324 1325 return llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, Indices); 1326} 1327 1328llvm::GlobalVariable *ItaniumCXXABI::getAddrOfVTable(const CXXRecordDecl *RD, 1329 CharUnits VPtrOffset) { 1330 assert(VPtrOffset.isZero() && "Itanium ABI only supports zero vptr offsets"); 1331 1332 llvm::GlobalVariable *&VTable = VTables[RD]; 1333 if (VTable) 1334 return VTable; 1335 1336 // Queue up this v-table for possible deferred emission. 1337 CGM.addDeferredVTable(RD); 1338 1339 SmallString<256> OutName; 1340 llvm::raw_svector_ostream Out(OutName); 1341 getMangleContext().mangleCXXVTable(RD, Out); 1342 Out.flush(); 1343 StringRef Name = OutName.str(); 1344 1345 ItaniumVTableContext &VTContext = CGM.getItaniumVTableContext(); 1346 llvm::ArrayType *ArrayType = llvm::ArrayType::get( 1347 CGM.Int8PtrTy, VTContext.getVTableLayout(RD).getNumVTableComponents()); 1348 1349 VTable = CGM.CreateOrReplaceCXXRuntimeVariable( 1350 Name, ArrayType, llvm::GlobalValue::ExternalLinkage); 1351 VTable->setUnnamedAddr(true); 1352 1353 if (RD->hasAttr<DLLImportAttr>()) 1354 VTable->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); 1355 else if (RD->hasAttr<DLLExportAttr>()) 1356 VTable->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); 1357 1358 return VTable; 1359} 1360 1361llvm::Value *ItaniumCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF, 1362 GlobalDecl GD, 1363 llvm::Value *This, 1364 llvm::Type *Ty) { 1365 GD = GD.getCanonicalDecl(); 1366 Ty = Ty->getPointerTo()->getPointerTo(); 1367 llvm::Value *VTable = CGF.GetVTablePtr(This, Ty); 1368 1369 uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD); 1370 llvm::Value *VFuncPtr = 1371 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn"); 1372 return CGF.Builder.CreateLoad(VFuncPtr); 1373} 1374 1375llvm::Value *ItaniumCXXABI::EmitVirtualDestructorCall( 1376 CodeGenFunction &CGF, const CXXDestructorDecl *Dtor, CXXDtorType DtorType, 1377 llvm::Value *This, const CXXMemberCallExpr *CE) { 1378 assert(CE == nullptr || CE->arg_begin() == CE->arg_end()); 1379 assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete); 1380 1381 const CGFunctionInfo *FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration( 1382 Dtor, getFromDtorType(DtorType)); 1383 llvm::Type *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo); 1384 llvm::Value *Callee = 1385 getVirtualFunctionPointer(CGF, GlobalDecl(Dtor, DtorType), This, Ty); 1386 1387 CGF.EmitCXXMemberOrOperatorCall(Dtor, Callee, ReturnValueSlot(), This, 1388 /*ImplicitParam=*/nullptr, QualType(), CE); 1389 return nullptr; 1390} 1391 1392void ItaniumCXXABI::emitVirtualInheritanceTables(const CXXRecordDecl *RD) { 1393 CodeGenVTables &VTables = CGM.getVTables(); 1394 llvm::GlobalVariable *VTT = VTables.GetAddrOfVTT(RD); 1395 VTables.EmitVTTDefinition(VTT, CGM.getVTableLinkage(RD), RD); 1396} 1397 1398static llvm::Value *performTypeAdjustment(CodeGenFunction &CGF, 1399 llvm::Value *Ptr, 1400 int64_t NonVirtualAdjustment, 1401 int64_t VirtualAdjustment, 1402 bool IsReturnAdjustment) { 1403 if (!NonVirtualAdjustment && !VirtualAdjustment) 1404 return Ptr; 1405 1406 llvm::Type *Int8PtrTy = CGF.Int8PtrTy; 1407 llvm::Value *V = CGF.Builder.CreateBitCast(Ptr, Int8PtrTy); 1408 1409 if (NonVirtualAdjustment && !IsReturnAdjustment) { 1410 // Perform the non-virtual adjustment for a base-to-derived cast. 1411 V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment); 1412 } 1413 1414 if (VirtualAdjustment) { 1415 llvm::Type *PtrDiffTy = 1416 CGF.ConvertType(CGF.getContext().getPointerDiffType()); 1417 1418 // Perform the virtual adjustment. 1419 llvm::Value *VTablePtrPtr = 1420 CGF.Builder.CreateBitCast(V, Int8PtrTy->getPointerTo()); 1421 1422 llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr); 1423 1424 llvm::Value *OffsetPtr = 1425 CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment); 1426 1427 OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo()); 1428 1429 // Load the adjustment offset from the vtable. 1430 llvm::Value *Offset = CGF.Builder.CreateLoad(OffsetPtr); 1431 1432 // Adjust our pointer. 1433 V = CGF.Builder.CreateInBoundsGEP(V, Offset); 1434 } 1435 1436 if (NonVirtualAdjustment && IsReturnAdjustment) { 1437 // Perform the non-virtual adjustment for a derived-to-base cast. 1438 V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment); 1439 } 1440 1441 // Cast back to the original type. 1442 return CGF.Builder.CreateBitCast(V, Ptr->getType()); 1443} 1444 1445llvm::Value *ItaniumCXXABI::performThisAdjustment(CodeGenFunction &CGF, 1446 llvm::Value *This, 1447 const ThisAdjustment &TA) { 1448 return performTypeAdjustment(CGF, This, TA.NonVirtual, 1449 TA.Virtual.Itanium.VCallOffsetOffset, 1450 /*IsReturnAdjustment=*/false); 1451} 1452 1453llvm::Value * 1454ItaniumCXXABI::performReturnAdjustment(CodeGenFunction &CGF, llvm::Value *Ret, 1455 const ReturnAdjustment &RA) { 1456 return performTypeAdjustment(CGF, Ret, RA.NonVirtual, 1457 RA.Virtual.Itanium.VBaseOffsetOffset, 1458 /*IsReturnAdjustment=*/true); 1459} 1460 1461void ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF, 1462 RValue RV, QualType ResultType) { 1463 if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl())) 1464 return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType); 1465 1466 // Destructor thunks in the ARM ABI have indeterminate results. 1467 llvm::Type *T = 1468 cast<llvm::PointerType>(CGF.ReturnValue->getType())->getElementType(); 1469 RValue Undef = RValue::get(llvm::UndefValue::get(T)); 1470 return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType); 1471} 1472 1473/************************** Array allocation cookies **************************/ 1474 1475CharUnits ItaniumCXXABI::getArrayCookieSizeImpl(QualType elementType) { 1476 // The array cookie is a size_t; pad that up to the element alignment. 1477 // The cookie is actually right-justified in that space. 1478 return std::max(CharUnits::fromQuantity(CGM.SizeSizeInBytes), 1479 CGM.getContext().getTypeAlignInChars(elementType)); 1480} 1481 1482llvm::Value *ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, 1483 llvm::Value *NewPtr, 1484 llvm::Value *NumElements, 1485 const CXXNewExpr *expr, 1486 QualType ElementType) { 1487 assert(requiresArrayCookie(expr)); 1488 1489 unsigned AS = NewPtr->getType()->getPointerAddressSpace(); 1490 1491 ASTContext &Ctx = getContext(); 1492 QualType SizeTy = Ctx.getSizeType(); 1493 CharUnits SizeSize = Ctx.getTypeSizeInChars(SizeTy); 1494 1495 // The size of the cookie. 1496 CharUnits CookieSize = 1497 std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType)); 1498 assert(CookieSize == getArrayCookieSizeImpl(ElementType)); 1499 1500 // Compute an offset to the cookie. 1501 llvm::Value *CookiePtr = NewPtr; 1502 CharUnits CookieOffset = CookieSize - SizeSize; 1503 if (!CookieOffset.isZero()) 1504 CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_64(CookiePtr, 1505 CookieOffset.getQuantity()); 1506 1507 // Write the number of elements into the appropriate slot. 1508 llvm::Type *NumElementsTy = CGF.ConvertType(SizeTy)->getPointerTo(AS); 1509 llvm::Value *NumElementsPtr = 1510 CGF.Builder.CreateBitCast(CookiePtr, NumElementsTy); 1511 llvm::Instruction *SI = CGF.Builder.CreateStore(NumElements, NumElementsPtr); 1512 if (CGM.getLangOpts().Sanitize.has(SanitizerKind::Address) && AS == 0 && 1513 expr->getOperatorNew()->isReplaceableGlobalAllocationFunction()) { 1514 // The store to the CookiePtr does not need to be instrumented. 1515 CGM.getSanitizerMetadata()->disableSanitizerForInstruction(SI); 1516 llvm::FunctionType *FTy = 1517 llvm::FunctionType::get(CGM.VoidTy, NumElementsTy, false); 1518 llvm::Constant *F = 1519 CGM.CreateRuntimeFunction(FTy, "__asan_poison_cxx_array_cookie"); 1520 CGF.Builder.CreateCall(F, NumElementsPtr); 1521 } 1522 1523 // Finally, compute a pointer to the actual data buffer by skipping 1524 // over the cookie completely. 1525 return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr, 1526 CookieSize.getQuantity()); 1527} 1528 1529llvm::Value *ItaniumCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, 1530 llvm::Value *allocPtr, 1531 CharUnits cookieSize) { 1532 // The element size is right-justified in the cookie. 1533 llvm::Value *numElementsPtr = allocPtr; 1534 CharUnits numElementsOffset = 1535 cookieSize - CharUnits::fromQuantity(CGF.SizeSizeInBytes); 1536 if (!numElementsOffset.isZero()) 1537 numElementsPtr = 1538 CGF.Builder.CreateConstInBoundsGEP1_64(numElementsPtr, 1539 numElementsOffset.getQuantity()); 1540 1541 unsigned AS = allocPtr->getType()->getPointerAddressSpace(); 1542 numElementsPtr = 1543 CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS)); 1544 if (!CGM.getLangOpts().Sanitize.has(SanitizerKind::Address) || AS != 0) 1545 return CGF.Builder.CreateLoad(numElementsPtr); 1546 // In asan mode emit a function call instead of a regular load and let the 1547 // run-time deal with it: if the shadow is properly poisoned return the 1548 // cookie, otherwise return 0 to avoid an infinite loop calling DTORs. 1549 // We can't simply ignore this load using nosanitize metadata because 1550 // the metadata may be lost. 1551 llvm::FunctionType *FTy = 1552 llvm::FunctionType::get(CGF.SizeTy, CGF.SizeTy->getPointerTo(0), false); 1553 llvm::Constant *F = 1554 CGM.CreateRuntimeFunction(FTy, "__asan_load_cxx_array_cookie"); 1555 return CGF.Builder.CreateCall(F, numElementsPtr); 1556} 1557 1558CharUnits ARMCXXABI::getArrayCookieSizeImpl(QualType elementType) { 1559 // ARM says that the cookie is always: 1560 // struct array_cookie { 1561 // std::size_t element_size; // element_size != 0 1562 // std::size_t element_count; 1563 // }; 1564 // But the base ABI doesn't give anything an alignment greater than 1565 // 8, so we can dismiss this as typical ABI-author blindness to 1566 // actual language complexity and round up to the element alignment. 1567 return std::max(CharUnits::fromQuantity(2 * CGM.SizeSizeInBytes), 1568 CGM.getContext().getTypeAlignInChars(elementType)); 1569} 1570 1571llvm::Value *ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, 1572 llvm::Value *newPtr, 1573 llvm::Value *numElements, 1574 const CXXNewExpr *expr, 1575 QualType elementType) { 1576 assert(requiresArrayCookie(expr)); 1577 1578 // NewPtr is a char*, but we generalize to arbitrary addrspaces. 1579 unsigned AS = newPtr->getType()->getPointerAddressSpace(); 1580 1581 // The cookie is always at the start of the buffer. 1582 llvm::Value *cookie = newPtr; 1583 1584 // The first element is the element size. 1585 cookie = CGF.Builder.CreateBitCast(cookie, CGF.SizeTy->getPointerTo(AS)); 1586 llvm::Value *elementSize = llvm::ConstantInt::get(CGF.SizeTy, 1587 getContext().getTypeSizeInChars(elementType).getQuantity()); 1588 CGF.Builder.CreateStore(elementSize, cookie); 1589 1590 // The second element is the element count. 1591 cookie = CGF.Builder.CreateConstInBoundsGEP1_32(cookie, 1); 1592 CGF.Builder.CreateStore(numElements, cookie); 1593 1594 // Finally, compute a pointer to the actual data buffer by skipping 1595 // over the cookie completely. 1596 CharUnits cookieSize = ARMCXXABI::getArrayCookieSizeImpl(elementType); 1597 return CGF.Builder.CreateConstInBoundsGEP1_64(newPtr, 1598 cookieSize.getQuantity()); 1599} 1600 1601llvm::Value *ARMCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, 1602 llvm::Value *allocPtr, 1603 CharUnits cookieSize) { 1604 // The number of elements is at offset sizeof(size_t) relative to 1605 // the allocated pointer. 1606 llvm::Value *numElementsPtr 1607 = CGF.Builder.CreateConstInBoundsGEP1_64(allocPtr, CGF.SizeSizeInBytes); 1608 1609 unsigned AS = allocPtr->getType()->getPointerAddressSpace(); 1610 numElementsPtr = 1611 CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS)); 1612 return CGF.Builder.CreateLoad(numElementsPtr); 1613} 1614 1615/*********************** Static local initialization **************************/ 1616 1617static llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM, 1618 llvm::PointerType *GuardPtrTy) { 1619 // int __cxa_guard_acquire(__guard *guard_object); 1620 llvm::FunctionType *FTy = 1621 llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy), 1622 GuardPtrTy, /*isVarArg=*/false); 1623 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire", 1624 llvm::AttributeSet::get(CGM.getLLVMContext(), 1625 llvm::AttributeSet::FunctionIndex, 1626 llvm::Attribute::NoUnwind)); 1627} 1628 1629static llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM, 1630 llvm::PointerType *GuardPtrTy) { 1631 // void __cxa_guard_release(__guard *guard_object); 1632 llvm::FunctionType *FTy = 1633 llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false); 1634 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release", 1635 llvm::AttributeSet::get(CGM.getLLVMContext(), 1636 llvm::AttributeSet::FunctionIndex, 1637 llvm::Attribute::NoUnwind)); 1638} 1639 1640static llvm::Constant *getGuardAbortFn(CodeGenModule &CGM, 1641 llvm::PointerType *GuardPtrTy) { 1642 // void __cxa_guard_abort(__guard *guard_object); 1643 llvm::FunctionType *FTy = 1644 llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false); 1645 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort", 1646 llvm::AttributeSet::get(CGM.getLLVMContext(), 1647 llvm::AttributeSet::FunctionIndex, 1648 llvm::Attribute::NoUnwind)); 1649} 1650 1651namespace { 1652 struct CallGuardAbort : EHScopeStack::Cleanup { 1653 llvm::GlobalVariable *Guard; 1654 CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {} 1655 1656 void Emit(CodeGenFunction &CGF, Flags flags) override { 1657 CGF.EmitNounwindRuntimeCall(getGuardAbortFn(CGF.CGM, Guard->getType()), 1658 Guard); 1659 } 1660 }; 1661} 1662 1663/// The ARM code here follows the Itanium code closely enough that we 1664/// just special-case it at particular places. 1665void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF, 1666 const VarDecl &D, 1667 llvm::GlobalVariable *var, 1668 bool shouldPerformInit) { 1669 CGBuilderTy &Builder = CGF.Builder; 1670 1671 // We only need to use thread-safe statics for local non-TLS variables; 1672 // global initialization is always single-threaded. 1673 bool threadsafe = getContext().getLangOpts().ThreadsafeStatics && 1674 D.isLocalVarDecl() && !D.getTLSKind(); 1675 1676 // If we have a global variable with internal linkage and thread-safe statics 1677 // are disabled, we can just let the guard variable be of type i8. 1678 bool useInt8GuardVariable = !threadsafe && var->hasInternalLinkage(); 1679 1680 llvm::IntegerType *guardTy; 1681 if (useInt8GuardVariable) { 1682 guardTy = CGF.Int8Ty; 1683 } else { 1684 // Guard variables are 64 bits in the generic ABI and size width on ARM 1685 // (i.e. 32-bit on AArch32, 64-bit on AArch64). 1686 guardTy = (UseARMGuardVarABI ? CGF.SizeTy : CGF.Int64Ty); 1687 } 1688 llvm::PointerType *guardPtrTy = guardTy->getPointerTo(); 1689 1690 // Create the guard variable if we don't already have it (as we 1691 // might if we're double-emitting this function body). 1692 llvm::GlobalVariable *guard = CGM.getStaticLocalDeclGuardAddress(&D); 1693 if (!guard) { 1694 // Mangle the name for the guard. 1695 SmallString<256> guardName; 1696 { 1697 llvm::raw_svector_ostream out(guardName); 1698 getMangleContext().mangleStaticGuardVariable(&D, out); 1699 out.flush(); 1700 } 1701 1702 // Create the guard variable with a zero-initializer. 1703 // Just absorb linkage and visibility from the guarded variable. 1704 guard = new llvm::GlobalVariable(CGM.getModule(), guardTy, 1705 false, var->getLinkage(), 1706 llvm::ConstantInt::get(guardTy, 0), 1707 guardName.str()); 1708 guard->setVisibility(var->getVisibility()); 1709 // If the variable is thread-local, so is its guard variable. 1710 guard->setThreadLocalMode(var->getThreadLocalMode()); 1711 1712 // The ABI says: It is suggested that it be emitted in the same COMDAT group 1713 // as the associated data object 1714 if (!D.isLocalVarDecl() && var->isWeakForLinker() && CGM.supportsCOMDAT()) { 1715 llvm::Comdat *C = CGM.getModule().getOrInsertComdat(var->getName()); 1716 guard->setComdat(C); 1717 var->setComdat(C); 1718 CGF.CurFn->setComdat(C); 1719 } 1720 1721 CGM.setStaticLocalDeclGuardAddress(&D, guard); 1722 } 1723 1724 // Test whether the variable has completed initialization. 1725 // 1726 // Itanium C++ ABI 3.3.2: 1727 // The following is pseudo-code showing how these functions can be used: 1728 // if (obj_guard.first_byte == 0) { 1729 // if ( __cxa_guard_acquire (&obj_guard) ) { 1730 // try { 1731 // ... initialize the object ...; 1732 // } catch (...) { 1733 // __cxa_guard_abort (&obj_guard); 1734 // throw; 1735 // } 1736 // ... queue object destructor with __cxa_atexit() ...; 1737 // __cxa_guard_release (&obj_guard); 1738 // } 1739 // } 1740 1741 // Load the first byte of the guard variable. 1742 llvm::LoadInst *LI = 1743 Builder.CreateLoad(Builder.CreateBitCast(guard, CGM.Int8PtrTy)); 1744 LI->setAlignment(1); 1745 1746 // Itanium ABI: 1747 // An implementation supporting thread-safety on multiprocessor 1748 // systems must also guarantee that references to the initialized 1749 // object do not occur before the load of the initialization flag. 1750 // 1751 // In LLVM, we do this by marking the load Acquire. 1752 if (threadsafe) 1753 LI->setAtomic(llvm::Acquire); 1754 1755 // For ARM, we should only check the first bit, rather than the entire byte: 1756 // 1757 // ARM C++ ABI 3.2.3.1: 1758 // To support the potential use of initialization guard variables 1759 // as semaphores that are the target of ARM SWP and LDREX/STREX 1760 // synchronizing instructions we define a static initialization 1761 // guard variable to be a 4-byte aligned, 4-byte word with the 1762 // following inline access protocol. 1763 // #define INITIALIZED 1 1764 // if ((obj_guard & INITIALIZED) != INITIALIZED) { 1765 // if (__cxa_guard_acquire(&obj_guard)) 1766 // ... 1767 // } 1768 // 1769 // and similarly for ARM64: 1770 // 1771 // ARM64 C++ ABI 3.2.2: 1772 // This ABI instead only specifies the value bit 0 of the static guard 1773 // variable; all other bits are platform defined. Bit 0 shall be 0 when the 1774 // variable is not initialized and 1 when it is. 1775 llvm::Value *V = 1776 (UseARMGuardVarABI && !useInt8GuardVariable) 1777 ? Builder.CreateAnd(LI, llvm::ConstantInt::get(CGM.Int8Ty, 1)) 1778 : LI; 1779 llvm::Value *isInitialized = Builder.CreateIsNull(V, "guard.uninitialized"); 1780 1781 llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check"); 1782 llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end"); 1783 1784 // Check if the first byte of the guard variable is zero. 1785 Builder.CreateCondBr(isInitialized, InitCheckBlock, EndBlock); 1786 1787 CGF.EmitBlock(InitCheckBlock); 1788 1789 // Variables used when coping with thread-safe statics and exceptions. 1790 if (threadsafe) { 1791 // Call __cxa_guard_acquire. 1792 llvm::Value *V 1793 = CGF.EmitNounwindRuntimeCall(getGuardAcquireFn(CGM, guardPtrTy), guard); 1794 1795 llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init"); 1796 1797 Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"), 1798 InitBlock, EndBlock); 1799 1800 // Call __cxa_guard_abort along the exceptional edge. 1801 CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, guard); 1802 1803 CGF.EmitBlock(InitBlock); 1804 } 1805 1806 // Emit the initializer and add a global destructor if appropriate. 1807 CGF.EmitCXXGlobalVarDeclInit(D, var, shouldPerformInit); 1808 1809 if (threadsafe) { 1810 // Pop the guard-abort cleanup if we pushed one. 1811 CGF.PopCleanupBlock(); 1812 1813 // Call __cxa_guard_release. This cannot throw. 1814 CGF.EmitNounwindRuntimeCall(getGuardReleaseFn(CGM, guardPtrTy), guard); 1815 } else { 1816 Builder.CreateStore(llvm::ConstantInt::get(guardTy, 1), guard); 1817 } 1818 1819 CGF.EmitBlock(EndBlock); 1820} 1821 1822/// Register a global destructor using __cxa_atexit. 1823static void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF, 1824 llvm::Constant *dtor, 1825 llvm::Constant *addr, 1826 bool TLS) { 1827 const char *Name = "__cxa_atexit"; 1828 if (TLS) { 1829 const llvm::Triple &T = CGF.getTarget().getTriple(); 1830 Name = T.isMacOSX() ? "_tlv_atexit" : "__cxa_thread_atexit"; 1831 } 1832 1833 // We're assuming that the destructor function is something we can 1834 // reasonably call with the default CC. Go ahead and cast it to the 1835 // right prototype. 1836 llvm::Type *dtorTy = 1837 llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, false)->getPointerTo(); 1838 1839 // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d); 1840 llvm::Type *paramTys[] = { dtorTy, CGF.Int8PtrTy, CGF.Int8PtrTy }; 1841 llvm::FunctionType *atexitTy = 1842 llvm::FunctionType::get(CGF.IntTy, paramTys, false); 1843 1844 // Fetch the actual function. 1845 llvm::Constant *atexit = CGF.CGM.CreateRuntimeFunction(atexitTy, Name); 1846 if (llvm::Function *fn = dyn_cast<llvm::Function>(atexit)) 1847 fn->setDoesNotThrow(); 1848 1849 // Create a variable that binds the atexit to this shared object. 1850 llvm::Constant *handle = 1851 CGF.CGM.CreateRuntimeVariable(CGF.Int8Ty, "__dso_handle"); 1852 1853 llvm::Value *args[] = { 1854 llvm::ConstantExpr::getBitCast(dtor, dtorTy), 1855 llvm::ConstantExpr::getBitCast(addr, CGF.Int8PtrTy), 1856 handle 1857 }; 1858 CGF.EmitNounwindRuntimeCall(atexit, args); 1859} 1860 1861/// Register a global destructor as best as we know how. 1862void ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF, 1863 const VarDecl &D, 1864 llvm::Constant *dtor, 1865 llvm::Constant *addr) { 1866 // Use __cxa_atexit if available. 1867 if (CGM.getCodeGenOpts().CXAAtExit) 1868 return emitGlobalDtorWithCXAAtExit(CGF, dtor, addr, D.getTLSKind()); 1869 1870 if (D.getTLSKind()) 1871 CGM.ErrorUnsupported(&D, "non-trivial TLS destruction"); 1872 1873 // In Apple kexts, we want to add a global destructor entry. 1874 // FIXME: shouldn't this be guarded by some variable? 1875 if (CGM.getLangOpts().AppleKext) { 1876 // Generate a global destructor entry. 1877 return CGM.AddCXXDtorEntry(dtor, addr); 1878 } 1879 1880 CGF.registerGlobalDtorWithAtExit(D, dtor, addr); 1881} 1882 1883static bool isThreadWrapperReplaceable(const VarDecl *VD, 1884 CodeGen::CodeGenModule &CGM) { 1885 assert(!VD->isStaticLocal() && "static local VarDecls don't need wrappers!"); 1886 // OS X prefers to have references to thread local variables to go through 1887 // the thread wrapper instead of directly referencing the backing variable. 1888 return VD->getTLSKind() == VarDecl::TLS_Dynamic && 1889 CGM.getTarget().getTriple().isMacOSX(); 1890} 1891 1892/// Get the appropriate linkage for the wrapper function. This is essentially 1893/// the weak form of the variable's linkage; every translation unit which needs 1894/// the wrapper emits a copy, and we want the linker to merge them. 1895static llvm::GlobalValue::LinkageTypes 1896getThreadLocalWrapperLinkage(const VarDecl *VD, CodeGen::CodeGenModule &CGM) { 1897 llvm::GlobalValue::LinkageTypes VarLinkage = 1898 CGM.getLLVMLinkageVarDefinition(VD, /*isConstant=*/false); 1899 1900 // For internal linkage variables, we don't need an external or weak wrapper. 1901 if (llvm::GlobalValue::isLocalLinkage(VarLinkage)) 1902 return VarLinkage; 1903 1904 // If the thread wrapper is replaceable, give it appropriate linkage. 1905 if (isThreadWrapperReplaceable(VD, CGM)) { 1906 if (llvm::GlobalVariable::isLinkOnceLinkage(VarLinkage) || 1907 llvm::GlobalVariable::isWeakODRLinkage(VarLinkage)) 1908 return llvm::GlobalVariable::WeakAnyLinkage; 1909 return VarLinkage; 1910 } 1911 return llvm::GlobalValue::WeakODRLinkage; 1912} 1913 1914llvm::Function * 1915ItaniumCXXABI::getOrCreateThreadLocalWrapper(const VarDecl *VD, 1916 llvm::Value *Val) { 1917 // Mangle the name for the thread_local wrapper function. 1918 SmallString<256> WrapperName; 1919 { 1920 llvm::raw_svector_ostream Out(WrapperName); 1921 getMangleContext().mangleItaniumThreadLocalWrapper(VD, Out); 1922 Out.flush(); 1923 } 1924 1925 if (llvm::Value *V = CGM.getModule().getNamedValue(WrapperName)) 1926 return cast<llvm::Function>(V); 1927 1928 llvm::Type *RetTy = Val->getType(); 1929 if (VD->getType()->isReferenceType()) 1930 RetTy = RetTy->getPointerElementType(); 1931 1932 llvm::FunctionType *FnTy = llvm::FunctionType::get(RetTy, false); 1933 llvm::Function *Wrapper = 1934 llvm::Function::Create(FnTy, getThreadLocalWrapperLinkage(VD, CGM), 1935 WrapperName.str(), &CGM.getModule()); 1936 // Always resolve references to the wrapper at link time. 1937 if (!Wrapper->hasLocalLinkage() && !isThreadWrapperReplaceable(VD, CGM)) 1938 Wrapper->setVisibility(llvm::GlobalValue::HiddenVisibility); 1939 return Wrapper; 1940} 1941 1942void ItaniumCXXABI::EmitThreadLocalInitFuncs( 1943 CodeGenModule &CGM, 1944 ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *>> 1945 CXXThreadLocals, ArrayRef<llvm::Function *> CXXThreadLocalInits, 1946 ArrayRef<llvm::GlobalVariable *> CXXThreadLocalInitVars) { 1947 llvm::Function *InitFunc = nullptr; 1948 if (!CXXThreadLocalInits.empty()) { 1949 // Generate a guarded initialization function. 1950 llvm::FunctionType *FTy = 1951 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false); 1952 InitFunc = CGM.CreateGlobalInitOrDestructFunction(FTy, "__tls_init", 1953 SourceLocation(), 1954 /*TLS=*/true); 1955 llvm::GlobalVariable *Guard = new llvm::GlobalVariable( 1956 CGM.getModule(), CGM.Int8Ty, /*isConstant=*/false, 1957 llvm::GlobalVariable::InternalLinkage, 1958 llvm::ConstantInt::get(CGM.Int8Ty, 0), "__tls_guard"); 1959 Guard->setThreadLocal(true); 1960 CodeGenFunction(CGM) 1961 .GenerateCXXGlobalInitFunc(InitFunc, CXXThreadLocalInits, Guard); 1962 } 1963 for (unsigned I = 0, N = CXXThreadLocals.size(); I != N; ++I) { 1964 const VarDecl *VD = CXXThreadLocals[I].first; 1965 llvm::GlobalVariable *Var = CXXThreadLocals[I].second; 1966 1967 // Some targets require that all access to thread local variables go through 1968 // the thread wrapper. This means that we cannot attempt to create a thread 1969 // wrapper or a thread helper. 1970 if (isThreadWrapperReplaceable(VD, CGM) && !VD->hasDefinition()) 1971 continue; 1972 1973 // Mangle the name for the thread_local initialization function. 1974 SmallString<256> InitFnName; 1975 { 1976 llvm::raw_svector_ostream Out(InitFnName); 1977 getMangleContext().mangleItaniumThreadLocalInit(VD, Out); 1978 Out.flush(); 1979 } 1980 1981 // If we have a definition for the variable, emit the initialization 1982 // function as an alias to the global Init function (if any). Otherwise, 1983 // produce a declaration of the initialization function. 1984 llvm::GlobalValue *Init = nullptr; 1985 bool InitIsInitFunc = false; 1986 if (VD->hasDefinition()) { 1987 InitIsInitFunc = true; 1988 if (InitFunc) 1989 Init = llvm::GlobalAlias::create(Var->getLinkage(), InitFnName.str(), 1990 InitFunc); 1991 } else { 1992 // Emit a weak global function referring to the initialization function. 1993 // This function will not exist if the TU defining the thread_local 1994 // variable in question does not need any dynamic initialization for 1995 // its thread_local variables. 1996 llvm::FunctionType *FnTy = llvm::FunctionType::get(CGM.VoidTy, false); 1997 Init = llvm::Function::Create( 1998 FnTy, llvm::GlobalVariable::ExternalWeakLinkage, InitFnName.str(), 1999 &CGM.getModule()); 2000 } 2001 2002 if (Init) 2003 Init->setVisibility(Var->getVisibility()); 2004 2005 llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Var); 2006 llvm::LLVMContext &Context = CGM.getModule().getContext(); 2007 llvm::BasicBlock *Entry = llvm::BasicBlock::Create(Context, "", Wrapper); 2008 CGBuilderTy Builder(Entry); 2009 if (InitIsInitFunc) { 2010 if (Init) 2011 Builder.CreateCall(Init); 2012 } else { 2013 // Don't know whether we have an init function. Call it if it exists. 2014 llvm::Value *Have = Builder.CreateIsNotNull(Init); 2015 llvm::BasicBlock *InitBB = llvm::BasicBlock::Create(Context, "", Wrapper); 2016 llvm::BasicBlock *ExitBB = llvm::BasicBlock::Create(Context, "", Wrapper); 2017 Builder.CreateCondBr(Have, InitBB, ExitBB); 2018 2019 Builder.SetInsertPoint(InitBB); 2020 Builder.CreateCall(Init); 2021 Builder.CreateBr(ExitBB); 2022 2023 Builder.SetInsertPoint(ExitBB); 2024 } 2025 2026 // For a reference, the result of the wrapper function is a pointer to 2027 // the referenced object. 2028 llvm::Value *Val = Var; 2029 if (VD->getType()->isReferenceType()) { 2030 llvm::LoadInst *LI = Builder.CreateLoad(Val); 2031 LI->setAlignment(CGM.getContext().getDeclAlign(VD).getQuantity()); 2032 Val = LI; 2033 } 2034 if (Val->getType() != Wrapper->getReturnType()) 2035 Val = Builder.CreatePointerBitCastOrAddrSpaceCast( 2036 Val, Wrapper->getReturnType(), ""); 2037 Builder.CreateRet(Val); 2038 } 2039} 2040 2041LValue ItaniumCXXABI::EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, 2042 const VarDecl *VD, 2043 QualType LValType) { 2044 QualType T = VD->getType(); 2045 llvm::Type *Ty = CGF.getTypes().ConvertTypeForMem(T); 2046 llvm::Value *Val = CGF.CGM.GetAddrOfGlobalVar(VD, Ty); 2047 llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Val); 2048 2049 Val = CGF.Builder.CreateCall(Wrapper); 2050 2051 LValue LV; 2052 if (VD->getType()->isReferenceType()) 2053 LV = CGF.MakeNaturalAlignAddrLValue(Val, LValType); 2054 else 2055 LV = CGF.MakeAddrLValue(Val, LValType, CGF.getContext().getDeclAlign(VD)); 2056 // FIXME: need setObjCGCLValueClass? 2057 return LV; 2058} 2059 2060/// Return whether the given global decl needs a VTT parameter, which it does 2061/// if it's a base constructor or destructor with virtual bases. 2062bool ItaniumCXXABI::NeedsVTTParameter(GlobalDecl GD) { 2063 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 2064 2065 // We don't have any virtual bases, just return early. 2066 if (!MD->getParent()->getNumVBases()) 2067 return false; 2068 2069 // Check if we have a base constructor. 2070 if (isa<CXXConstructorDecl>(MD) && GD.getCtorType() == Ctor_Base) 2071 return true; 2072 2073 // Check if we have a base destructor. 2074 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base) 2075 return true; 2076 2077 return false; 2078} 2079 2080namespace { 2081class ItaniumRTTIBuilder { 2082 CodeGenModule &CGM; // Per-module state. 2083 llvm::LLVMContext &VMContext; 2084 const ItaniumCXXABI &CXXABI; // Per-module state. 2085 2086 /// Fields - The fields of the RTTI descriptor currently being built. 2087 SmallVector<llvm::Constant *, 16> Fields; 2088 2089 /// GetAddrOfTypeName - Returns the mangled type name of the given type. 2090 llvm::GlobalVariable * 2091 GetAddrOfTypeName(QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage); 2092 2093 /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI 2094 /// descriptor of the given type. 2095 llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty); 2096 2097 /// BuildVTablePointer - Build the vtable pointer for the given type. 2098 void BuildVTablePointer(const Type *Ty); 2099 2100 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single 2101 /// inheritance, according to the Itanium C++ ABI, 2.9.5p6b. 2102 void BuildSIClassTypeInfo(const CXXRecordDecl *RD); 2103 2104 /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for 2105 /// classes with bases that do not satisfy the abi::__si_class_type_info 2106 /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c. 2107 void BuildVMIClassTypeInfo(const CXXRecordDecl *RD); 2108 2109 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used 2110 /// for pointer types. 2111 void BuildPointerTypeInfo(QualType PointeeTy); 2112 2113 /// BuildObjCObjectTypeInfo - Build the appropriate kind of 2114 /// type_info for an object type. 2115 void BuildObjCObjectTypeInfo(const ObjCObjectType *Ty); 2116 2117 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info 2118 /// struct, used for member pointer types. 2119 void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty); 2120 2121public: 2122 ItaniumRTTIBuilder(const ItaniumCXXABI &ABI) 2123 : CGM(ABI.CGM), VMContext(CGM.getModule().getContext()), CXXABI(ABI) {} 2124 2125 // Pointer type info flags. 2126 enum { 2127 /// PTI_Const - Type has const qualifier. 2128 PTI_Const = 0x1, 2129 2130 /// PTI_Volatile - Type has volatile qualifier. 2131 PTI_Volatile = 0x2, 2132 2133 /// PTI_Restrict - Type has restrict qualifier. 2134 PTI_Restrict = 0x4, 2135 2136 /// PTI_Incomplete - Type is incomplete. 2137 PTI_Incomplete = 0x8, 2138 2139 /// PTI_ContainingClassIncomplete - Containing class is incomplete. 2140 /// (in pointer to member). 2141 PTI_ContainingClassIncomplete = 0x10 2142 }; 2143 2144 // VMI type info flags. 2145 enum { 2146 /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance. 2147 VMI_NonDiamondRepeat = 0x1, 2148 2149 /// VMI_DiamondShaped - Class is diamond shaped. 2150 VMI_DiamondShaped = 0x2 2151 }; 2152 2153 // Base class type info flags. 2154 enum { 2155 /// BCTI_Virtual - Base class is virtual. 2156 BCTI_Virtual = 0x1, 2157 2158 /// BCTI_Public - Base class is public. 2159 BCTI_Public = 0x2 2160 }; 2161 2162 /// BuildTypeInfo - Build the RTTI type info struct for the given type. 2163 /// 2164 /// \param Force - true to force the creation of this RTTI value 2165 llvm::Constant *BuildTypeInfo(QualType Ty, bool Force = false); 2166}; 2167} 2168 2169llvm::GlobalVariable *ItaniumRTTIBuilder::GetAddrOfTypeName( 2170 QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage) { 2171 SmallString<256> OutName; 2172 llvm::raw_svector_ostream Out(OutName); 2173 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out); 2174 Out.flush(); 2175 StringRef Name = OutName.str(); 2176 2177 // We know that the mangled name of the type starts at index 4 of the 2178 // mangled name of the typename, so we can just index into it in order to 2179 // get the mangled name of the type. 2180 llvm::Constant *Init = llvm::ConstantDataArray::getString(VMContext, 2181 Name.substr(4)); 2182 2183 llvm::GlobalVariable *GV = 2184 CGM.CreateOrReplaceCXXRuntimeVariable(Name, Init->getType(), Linkage); 2185 2186 GV->setInitializer(Init); 2187 2188 return GV; 2189} 2190 2191llvm::Constant * 2192ItaniumRTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) { 2193 // Mangle the RTTI name. 2194 SmallString<256> OutName; 2195 llvm::raw_svector_ostream Out(OutName); 2196 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out); 2197 Out.flush(); 2198 StringRef Name = OutName.str(); 2199 2200 // Look for an existing global. 2201 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name); 2202 2203 if (!GV) { 2204 // Create a new global variable. 2205 GV = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy, 2206 /*Constant=*/true, 2207 llvm::GlobalValue::ExternalLinkage, nullptr, 2208 Name); 2209 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { 2210 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); 2211 if (RD->hasAttr<DLLImportAttr>()) 2212 GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass); 2213 } 2214 } 2215 2216 return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy); 2217} 2218 2219/// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type 2220/// info for that type is defined in the standard library. 2221static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) { 2222 // Itanium C++ ABI 2.9.2: 2223 // Basic type information (e.g. for "int", "bool", etc.) will be kept in 2224 // the run-time support library. Specifically, the run-time support 2225 // library should contain type_info objects for the types X, X* and 2226 // X const*, for every X in: void, std::nullptr_t, bool, wchar_t, char, 2227 // unsigned char, signed char, short, unsigned short, int, unsigned int, 2228 // long, unsigned long, long long, unsigned long long, float, double, 2229 // long double, char16_t, char32_t, and the IEEE 754r decimal and 2230 // half-precision floating point types. 2231 switch (Ty->getKind()) { 2232 case BuiltinType::Void: 2233 case BuiltinType::NullPtr: 2234 case BuiltinType::Bool: 2235 case BuiltinType::WChar_S: 2236 case BuiltinType::WChar_U: 2237 case BuiltinType::Char_U: 2238 case BuiltinType::Char_S: 2239 case BuiltinType::UChar: 2240 case BuiltinType::SChar: 2241 case BuiltinType::Short: 2242 case BuiltinType::UShort: 2243 case BuiltinType::Int: 2244 case BuiltinType::UInt: 2245 case BuiltinType::Long: 2246 case BuiltinType::ULong: 2247 case BuiltinType::LongLong: 2248 case BuiltinType::ULongLong: 2249 case BuiltinType::Half: 2250 case BuiltinType::Float: 2251 case BuiltinType::Double: 2252 case BuiltinType::LongDouble: 2253 case BuiltinType::Char16: 2254 case BuiltinType::Char32: 2255 case BuiltinType::Int128: 2256 case BuiltinType::UInt128: 2257 case BuiltinType::OCLImage1d: 2258 case BuiltinType::OCLImage1dArray: 2259 case BuiltinType::OCLImage1dBuffer: 2260 case BuiltinType::OCLImage2d: 2261 case BuiltinType::OCLImage2dArray: 2262 case BuiltinType::OCLImage3d: 2263 case BuiltinType::OCLSampler: 2264 case BuiltinType::OCLEvent: 2265 return true; 2266 2267 case BuiltinType::Dependent: 2268#define BUILTIN_TYPE(Id, SingletonId) 2269#define PLACEHOLDER_TYPE(Id, SingletonId) \ 2270 case BuiltinType::Id: 2271#include "clang/AST/BuiltinTypes.def" 2272 llvm_unreachable("asking for RRTI for a placeholder type!"); 2273 2274 case BuiltinType::ObjCId: 2275 case BuiltinType::ObjCClass: 2276 case BuiltinType::ObjCSel: 2277 llvm_unreachable("FIXME: Objective-C types are unsupported!"); 2278 } 2279 2280 llvm_unreachable("Invalid BuiltinType Kind!"); 2281} 2282 2283static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) { 2284 QualType PointeeTy = PointerTy->getPointeeType(); 2285 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy); 2286 if (!BuiltinTy) 2287 return false; 2288 2289 // Check the qualifiers. 2290 Qualifiers Quals = PointeeTy.getQualifiers(); 2291 Quals.removeConst(); 2292 2293 if (!Quals.empty()) 2294 return false; 2295 2296 return TypeInfoIsInStandardLibrary(BuiltinTy); 2297} 2298 2299/// IsStandardLibraryRTTIDescriptor - Returns whether the type 2300/// information for the given type exists in the standard library. 2301static bool IsStandardLibraryRTTIDescriptor(QualType Ty) { 2302 // Type info for builtin types is defined in the standard library. 2303 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty)) 2304 return TypeInfoIsInStandardLibrary(BuiltinTy); 2305 2306 // Type info for some pointer types to builtin types is defined in the 2307 // standard library. 2308 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty)) 2309 return TypeInfoIsInStandardLibrary(PointerTy); 2310 2311 return false; 2312} 2313 2314/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for 2315/// the given type exists somewhere else, and that we should not emit the type 2316/// information in this translation unit. Assumes that it is not a 2317/// standard-library type. 2318static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM, 2319 QualType Ty) { 2320 ASTContext &Context = CGM.getContext(); 2321 2322 // If RTTI is disabled, assume it might be disabled in the 2323 // translation unit that defines any potential key function, too. 2324 if (!Context.getLangOpts().RTTI) return false; 2325 2326 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { 2327 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); 2328 if (!RD->hasDefinition()) 2329 return false; 2330 2331 if (!RD->isDynamicClass()) 2332 return false; 2333 2334 // FIXME: this may need to be reconsidered if the key function 2335 // changes. 2336 if (CGM.getVTables().isVTableExternal(RD)) 2337 return true; 2338 2339 if (RD->hasAttr<DLLImportAttr>()) 2340 return true; 2341 } 2342 2343 return false; 2344} 2345 2346/// IsIncompleteClassType - Returns whether the given record type is incomplete. 2347static bool IsIncompleteClassType(const RecordType *RecordTy) { 2348 return !RecordTy->getDecl()->isCompleteDefinition(); 2349} 2350 2351/// ContainsIncompleteClassType - Returns whether the given type contains an 2352/// incomplete class type. This is true if 2353/// 2354/// * The given type is an incomplete class type. 2355/// * The given type is a pointer type whose pointee type contains an 2356/// incomplete class type. 2357/// * The given type is a member pointer type whose class is an incomplete 2358/// class type. 2359/// * The given type is a member pointer type whoise pointee type contains an 2360/// incomplete class type. 2361/// is an indirect or direct pointer to an incomplete class type. 2362static bool ContainsIncompleteClassType(QualType Ty) { 2363 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { 2364 if (IsIncompleteClassType(RecordTy)) 2365 return true; 2366 } 2367 2368 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty)) 2369 return ContainsIncompleteClassType(PointerTy->getPointeeType()); 2370 2371 if (const MemberPointerType *MemberPointerTy = 2372 dyn_cast<MemberPointerType>(Ty)) { 2373 // Check if the class type is incomplete. 2374 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass()); 2375 if (IsIncompleteClassType(ClassType)) 2376 return true; 2377 2378 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType()); 2379 } 2380 2381 return false; 2382} 2383 2384// CanUseSingleInheritance - Return whether the given record decl has a "single, 2385// public, non-virtual base at offset zero (i.e. the derived class is dynamic 2386// iff the base is)", according to Itanium C++ ABI, 2.95p6b. 2387static bool CanUseSingleInheritance(const CXXRecordDecl *RD) { 2388 // Check the number of bases. 2389 if (RD->getNumBases() != 1) 2390 return false; 2391 2392 // Get the base. 2393 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin(); 2394 2395 // Check that the base is not virtual. 2396 if (Base->isVirtual()) 2397 return false; 2398 2399 // Check that the base is public. 2400 if (Base->getAccessSpecifier() != AS_public) 2401 return false; 2402 2403 // Check that the class is dynamic iff the base is. 2404 const CXXRecordDecl *BaseDecl = 2405 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 2406 if (!BaseDecl->isEmpty() && 2407 BaseDecl->isDynamicClass() != RD->isDynamicClass()) 2408 return false; 2409 2410 return true; 2411} 2412 2413void ItaniumRTTIBuilder::BuildVTablePointer(const Type *Ty) { 2414 // abi::__class_type_info. 2415 static const char * const ClassTypeInfo = 2416 "_ZTVN10__cxxabiv117__class_type_infoE"; 2417 // abi::__si_class_type_info. 2418 static const char * const SIClassTypeInfo = 2419 "_ZTVN10__cxxabiv120__si_class_type_infoE"; 2420 // abi::__vmi_class_type_info. 2421 static const char * const VMIClassTypeInfo = 2422 "_ZTVN10__cxxabiv121__vmi_class_type_infoE"; 2423 2424 const char *VTableName = nullptr; 2425 2426 switch (Ty->getTypeClass()) { 2427#define TYPE(Class, Base) 2428#define ABSTRACT_TYPE(Class, Base) 2429#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: 2430#define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 2431#define DEPENDENT_TYPE(Class, Base) case Type::Class: 2432#include "clang/AST/TypeNodes.def" 2433 llvm_unreachable("Non-canonical and dependent types shouldn't get here"); 2434 2435 case Type::LValueReference: 2436 case Type::RValueReference: 2437 llvm_unreachable("References shouldn't get here"); 2438 2439 case Type::Auto: 2440 llvm_unreachable("Undeduced auto type shouldn't get here"); 2441 2442 case Type::Builtin: 2443 // GCC treats vector and complex types as fundamental types. 2444 case Type::Vector: 2445 case Type::ExtVector: 2446 case Type::Complex: 2447 case Type::Atomic: 2448 // FIXME: GCC treats block pointers as fundamental types?! 2449 case Type::BlockPointer: 2450 // abi::__fundamental_type_info. 2451 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE"; 2452 break; 2453 2454 case Type::ConstantArray: 2455 case Type::IncompleteArray: 2456 case Type::VariableArray: 2457 // abi::__array_type_info. 2458 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE"; 2459 break; 2460 2461 case Type::FunctionNoProto: 2462 case Type::FunctionProto: 2463 // abi::__function_type_info. 2464 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE"; 2465 break; 2466 2467 case Type::Enum: 2468 // abi::__enum_type_info. 2469 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE"; 2470 break; 2471 2472 case Type::Record: { 2473 const CXXRecordDecl *RD = 2474 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl()); 2475 2476 if (!RD->hasDefinition() || !RD->getNumBases()) { 2477 VTableName = ClassTypeInfo; 2478 } else if (CanUseSingleInheritance(RD)) { 2479 VTableName = SIClassTypeInfo; 2480 } else { 2481 VTableName = VMIClassTypeInfo; 2482 } 2483 2484 break; 2485 } 2486 2487 case Type::ObjCObject: 2488 // Ignore protocol qualifiers. 2489 Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr(); 2490 2491 // Handle id and Class. 2492 if (isa<BuiltinType>(Ty)) { 2493 VTableName = ClassTypeInfo; 2494 break; 2495 } 2496 2497 assert(isa<ObjCInterfaceType>(Ty)); 2498 // Fall through. 2499 2500 case Type::ObjCInterface: 2501 if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) { 2502 VTableName = SIClassTypeInfo; 2503 } else { 2504 VTableName = ClassTypeInfo; 2505 } 2506 break; 2507 2508 case Type::ObjCObjectPointer: 2509 case Type::Pointer: 2510 // abi::__pointer_type_info. 2511 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE"; 2512 break; 2513 2514 case Type::MemberPointer: 2515 // abi::__pointer_to_member_type_info. 2516 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE"; 2517 break; 2518 } 2519 2520 llvm::Constant *VTable = 2521 CGM.getModule().getOrInsertGlobal(VTableName, CGM.Int8PtrTy); 2522 2523 llvm::Type *PtrDiffTy = 2524 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType()); 2525 2526 // The vtable address point is 2. 2527 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2); 2528 VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, Two); 2529 VTable = llvm::ConstantExpr::getBitCast(VTable, CGM.Int8PtrTy); 2530 2531 Fields.push_back(VTable); 2532} 2533 2534/// \brief Return the linkage that the type info and type info name constants 2535/// should have for the given type. 2536static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(CodeGenModule &CGM, 2537 QualType Ty) { 2538 // Itanium C++ ABI 2.9.5p7: 2539 // In addition, it and all of the intermediate abi::__pointer_type_info 2540 // structs in the chain down to the abi::__class_type_info for the 2541 // incomplete class type must be prevented from resolving to the 2542 // corresponding type_info structs for the complete class type, possibly 2543 // by making them local static objects. Finally, a dummy class RTTI is 2544 // generated for the incomplete type that will not resolve to the final 2545 // complete class RTTI (because the latter need not exist), possibly by 2546 // making it a local static object. 2547 if (ContainsIncompleteClassType(Ty)) 2548 return llvm::GlobalValue::InternalLinkage; 2549 2550 switch (Ty->getLinkage()) { 2551 case NoLinkage: 2552 case InternalLinkage: 2553 case UniqueExternalLinkage: 2554 return llvm::GlobalValue::InternalLinkage; 2555 2556 case VisibleNoLinkage: 2557 case ExternalLinkage: 2558 if (!CGM.getLangOpts().RTTI) { 2559 // RTTI is not enabled, which means that this type info struct is going 2560 // to be used for exception handling. Give it linkonce_odr linkage. 2561 return llvm::GlobalValue::LinkOnceODRLinkage; 2562 } 2563 2564 if (const RecordType *Record = dyn_cast<RecordType>(Ty)) { 2565 const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); 2566 if (RD->hasAttr<WeakAttr>()) 2567 return llvm::GlobalValue::WeakODRLinkage; 2568 if (RD->isDynamicClass()) 2569 return CGM.getVTableLinkage(RD); 2570 } 2571 2572 return llvm::GlobalValue::LinkOnceODRLinkage; 2573 } 2574 2575 llvm_unreachable("Invalid linkage!"); 2576} 2577 2578llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) { 2579 // We want to operate on the canonical type. 2580 Ty = CGM.getContext().getCanonicalType(Ty); 2581 2582 // Check if we've already emitted an RTTI descriptor for this type. 2583 SmallString<256> OutName; 2584 llvm::raw_svector_ostream Out(OutName); 2585 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out); 2586 Out.flush(); 2587 StringRef Name = OutName.str(); 2588 2589 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name); 2590 if (OldGV && !OldGV->isDeclaration()) { 2591 assert(!OldGV->hasAvailableExternallyLinkage() && 2592 "available_externally typeinfos not yet implemented"); 2593 2594 return llvm::ConstantExpr::getBitCast(OldGV, CGM.Int8PtrTy); 2595 } 2596 2597 // Check if there is already an external RTTI descriptor for this type. 2598 bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty); 2599 if (!Force && (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM, Ty))) 2600 return GetAddrOfExternalRTTIDescriptor(Ty); 2601 2602 // Emit the standard library with external linkage. 2603 llvm::GlobalVariable::LinkageTypes Linkage; 2604 if (IsStdLib) 2605 Linkage = llvm::GlobalValue::ExternalLinkage; 2606 else 2607 Linkage = getTypeInfoLinkage(CGM, Ty); 2608 2609 // Add the vtable pointer. 2610 BuildVTablePointer(cast<Type>(Ty)); 2611 2612 // And the name. 2613 llvm::GlobalVariable *TypeName = GetAddrOfTypeName(Ty, Linkage); 2614 llvm::Constant *TypeNameField; 2615 2616 // If we're supposed to demote the visibility, be sure to set a flag 2617 // to use a string comparison for type_info comparisons. 2618 ItaniumCXXABI::RTTIUniquenessKind RTTIUniqueness = 2619 CXXABI.classifyRTTIUniqueness(Ty, Linkage); 2620 if (RTTIUniqueness != ItaniumCXXABI::RUK_Unique) { 2621 // The flag is the sign bit, which on ARM64 is defined to be clear 2622 // for global pointers. This is very ARM64-specific. 2623 TypeNameField = llvm::ConstantExpr::getPtrToInt(TypeName, CGM.Int64Ty); 2624 llvm::Constant *flag = 2625 llvm::ConstantInt::get(CGM.Int64Ty, ((uint64_t)1) << 63); 2626 TypeNameField = llvm::ConstantExpr::getAdd(TypeNameField, flag); 2627 TypeNameField = 2628 llvm::ConstantExpr::getIntToPtr(TypeNameField, CGM.Int8PtrTy); 2629 } else { 2630 TypeNameField = llvm::ConstantExpr::getBitCast(TypeName, CGM.Int8PtrTy); 2631 } 2632 Fields.push_back(TypeNameField); 2633 2634 switch (Ty->getTypeClass()) { 2635#define TYPE(Class, Base) 2636#define ABSTRACT_TYPE(Class, Base) 2637#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: 2638#define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 2639#define DEPENDENT_TYPE(Class, Base) case Type::Class: 2640#include "clang/AST/TypeNodes.def" 2641 llvm_unreachable("Non-canonical and dependent types shouldn't get here"); 2642 2643 // GCC treats vector types as fundamental types. 2644 case Type::Builtin: 2645 case Type::Vector: 2646 case Type::ExtVector: 2647 case Type::Complex: 2648 case Type::BlockPointer: 2649 // Itanium C++ ABI 2.9.5p4: 2650 // abi::__fundamental_type_info adds no data members to std::type_info. 2651 break; 2652 2653 case Type::LValueReference: 2654 case Type::RValueReference: 2655 llvm_unreachable("References shouldn't get here"); 2656 2657 case Type::Auto: 2658 llvm_unreachable("Undeduced auto type shouldn't get here"); 2659 2660 case Type::ConstantArray: 2661 case Type::IncompleteArray: 2662 case Type::VariableArray: 2663 // Itanium C++ ABI 2.9.5p5: 2664 // abi::__array_type_info adds no data members to std::type_info. 2665 break; 2666 2667 case Type::FunctionNoProto: 2668 case Type::FunctionProto: 2669 // Itanium C++ ABI 2.9.5p5: 2670 // abi::__function_type_info adds no data members to std::type_info. 2671 break; 2672 2673 case Type::Enum: 2674 // Itanium C++ ABI 2.9.5p5: 2675 // abi::__enum_type_info adds no data members to std::type_info. 2676 break; 2677 2678 case Type::Record: { 2679 const CXXRecordDecl *RD = 2680 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl()); 2681 if (!RD->hasDefinition() || !RD->getNumBases()) { 2682 // We don't need to emit any fields. 2683 break; 2684 } 2685 2686 if (CanUseSingleInheritance(RD)) 2687 BuildSIClassTypeInfo(RD); 2688 else 2689 BuildVMIClassTypeInfo(RD); 2690 2691 break; 2692 } 2693 2694 case Type::ObjCObject: 2695 case Type::ObjCInterface: 2696 BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty)); 2697 break; 2698 2699 case Type::ObjCObjectPointer: 2700 BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType()); 2701 break; 2702 2703 case Type::Pointer: 2704 BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType()); 2705 break; 2706 2707 case Type::MemberPointer: 2708 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty)); 2709 break; 2710 2711 case Type::Atomic: 2712 // No fields, at least for the moment. 2713 break; 2714 } 2715 2716 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Fields); 2717 2718 llvm::GlobalVariable *GV = 2719 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), 2720 /*Constant=*/true, Linkage, Init, Name); 2721 2722 // If there's already an old global variable, replace it with the new one. 2723 if (OldGV) { 2724 GV->takeName(OldGV); 2725 llvm::Constant *NewPtr = 2726 llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); 2727 OldGV->replaceAllUsesWith(NewPtr); 2728 OldGV->eraseFromParent(); 2729 } 2730 2731 // The Itanium ABI specifies that type_info objects must be globally 2732 // unique, with one exception: if the type is an incomplete class 2733 // type or a (possibly indirect) pointer to one. That exception 2734 // affects the general case of comparing type_info objects produced 2735 // by the typeid operator, which is why the comparison operators on 2736 // std::type_info generally use the type_info name pointers instead 2737 // of the object addresses. However, the language's built-in uses 2738 // of RTTI generally require class types to be complete, even when 2739 // manipulating pointers to those class types. This allows the 2740 // implementation of dynamic_cast to rely on address equality tests, 2741 // which is much faster. 2742 2743 // All of this is to say that it's important that both the type_info 2744 // object and the type_info name be uniqued when weakly emitted. 2745 2746 // Give the type_info object and name the formal visibility of the 2747 // type itself. 2748 llvm::GlobalValue::VisibilityTypes llvmVisibility; 2749 if (llvm::GlobalValue::isLocalLinkage(Linkage)) 2750 // If the linkage is local, only default visibility makes sense. 2751 llvmVisibility = llvm::GlobalValue::DefaultVisibility; 2752 else if (RTTIUniqueness == ItaniumCXXABI::RUK_NonUniqueHidden) 2753 llvmVisibility = llvm::GlobalValue::HiddenVisibility; 2754 else 2755 llvmVisibility = CodeGenModule::GetLLVMVisibility(Ty->getVisibility()); 2756 TypeName->setVisibility(llvmVisibility); 2757 GV->setVisibility(llvmVisibility); 2758 2759 return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy); 2760} 2761 2762/// ComputeQualifierFlags - Compute the pointer type info flags from the 2763/// given qualifier. 2764static unsigned ComputeQualifierFlags(Qualifiers Quals) { 2765 unsigned Flags = 0; 2766 2767 if (Quals.hasConst()) 2768 Flags |= ItaniumRTTIBuilder::PTI_Const; 2769 if (Quals.hasVolatile()) 2770 Flags |= ItaniumRTTIBuilder::PTI_Volatile; 2771 if (Quals.hasRestrict()) 2772 Flags |= ItaniumRTTIBuilder::PTI_Restrict; 2773 2774 return Flags; 2775} 2776 2777/// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info 2778/// for the given Objective-C object type. 2779void ItaniumRTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) { 2780 // Drop qualifiers. 2781 const Type *T = OT->getBaseType().getTypePtr(); 2782 assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T)); 2783 2784 // The builtin types are abi::__class_type_infos and don't require 2785 // extra fields. 2786 if (isa<BuiltinType>(T)) return; 2787 2788 ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl(); 2789 ObjCInterfaceDecl *Super = Class->getSuperClass(); 2790 2791 // Root classes are also __class_type_info. 2792 if (!Super) return; 2793 2794 QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super); 2795 2796 // Everything else is single inheritance. 2797 llvm::Constant *BaseTypeInfo = 2798 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(SuperTy); 2799 Fields.push_back(BaseTypeInfo); 2800} 2801 2802/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single 2803/// inheritance, according to the Itanium C++ ABI, 2.95p6b. 2804void ItaniumRTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) { 2805 // Itanium C++ ABI 2.9.5p6b: 2806 // It adds to abi::__class_type_info a single member pointing to the 2807 // type_info structure for the base type, 2808 llvm::Constant *BaseTypeInfo = 2809 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(RD->bases_begin()->getType()); 2810 Fields.push_back(BaseTypeInfo); 2811} 2812 2813namespace { 2814 /// SeenBases - Contains virtual and non-virtual bases seen when traversing 2815 /// a class hierarchy. 2816 struct SeenBases { 2817 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases; 2818 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases; 2819 }; 2820} 2821 2822/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in 2823/// abi::__vmi_class_type_info. 2824/// 2825static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base, 2826 SeenBases &Bases) { 2827 2828 unsigned Flags = 0; 2829 2830 const CXXRecordDecl *BaseDecl = 2831 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 2832 2833 if (Base->isVirtual()) { 2834 // Mark the virtual base as seen. 2835 if (!Bases.VirtualBases.insert(BaseDecl).second) { 2836 // If this virtual base has been seen before, then the class is diamond 2837 // shaped. 2838 Flags |= ItaniumRTTIBuilder::VMI_DiamondShaped; 2839 } else { 2840 if (Bases.NonVirtualBases.count(BaseDecl)) 2841 Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat; 2842 } 2843 } else { 2844 // Mark the non-virtual base as seen. 2845 if (!Bases.NonVirtualBases.insert(BaseDecl).second) { 2846 // If this non-virtual base has been seen before, then the class has non- 2847 // diamond shaped repeated inheritance. 2848 Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat; 2849 } else { 2850 if (Bases.VirtualBases.count(BaseDecl)) 2851 Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat; 2852 } 2853 } 2854 2855 // Walk all bases. 2856 for (const auto &I : BaseDecl->bases()) 2857 Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases); 2858 2859 return Flags; 2860} 2861 2862static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) { 2863 unsigned Flags = 0; 2864 SeenBases Bases; 2865 2866 // Walk all bases. 2867 for (const auto &I : RD->bases()) 2868 Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases); 2869 2870 return Flags; 2871} 2872 2873/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for 2874/// classes with bases that do not satisfy the abi::__si_class_type_info 2875/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c. 2876void ItaniumRTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) { 2877 llvm::Type *UnsignedIntLTy = 2878 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy); 2879 2880 // Itanium C++ ABI 2.9.5p6c: 2881 // __flags is a word with flags describing details about the class 2882 // structure, which may be referenced by using the __flags_masks 2883 // enumeration. These flags refer to both direct and indirect bases. 2884 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD); 2885 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags)); 2886 2887 // Itanium C++ ABI 2.9.5p6c: 2888 // __base_count is a word with the number of direct proper base class 2889 // descriptions that follow. 2890 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases())); 2891 2892 if (!RD->getNumBases()) 2893 return; 2894 2895 llvm::Type *LongLTy = 2896 CGM.getTypes().ConvertType(CGM.getContext().LongTy); 2897 2898 // Now add the base class descriptions. 2899 2900 // Itanium C++ ABI 2.9.5p6c: 2901 // __base_info[] is an array of base class descriptions -- one for every 2902 // direct proper base. Each description is of the type: 2903 // 2904 // struct abi::__base_class_type_info { 2905 // public: 2906 // const __class_type_info *__base_type; 2907 // long __offset_flags; 2908 // 2909 // enum __offset_flags_masks { 2910 // __virtual_mask = 0x1, 2911 // __public_mask = 0x2, 2912 // __offset_shift = 8 2913 // }; 2914 // }; 2915 for (const auto &Base : RD->bases()) { 2916 // The __base_type member points to the RTTI for the base type. 2917 Fields.push_back(ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(Base.getType())); 2918 2919 const CXXRecordDecl *BaseDecl = 2920 cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); 2921 2922 int64_t OffsetFlags = 0; 2923 2924 // All but the lower 8 bits of __offset_flags are a signed offset. 2925 // For a non-virtual base, this is the offset in the object of the base 2926 // subobject. For a virtual base, this is the offset in the virtual table of 2927 // the virtual base offset for the virtual base referenced (negative). 2928 CharUnits Offset; 2929 if (Base.isVirtual()) 2930 Offset = 2931 CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(RD, BaseDecl); 2932 else { 2933 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); 2934 Offset = Layout.getBaseClassOffset(BaseDecl); 2935 }; 2936 2937 OffsetFlags = uint64_t(Offset.getQuantity()) << 8; 2938 2939 // The low-order byte of __offset_flags contains flags, as given by the 2940 // masks from the enumeration __offset_flags_masks. 2941 if (Base.isVirtual()) 2942 OffsetFlags |= BCTI_Virtual; 2943 if (Base.getAccessSpecifier() == AS_public) 2944 OffsetFlags |= BCTI_Public; 2945 2946 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags)); 2947 } 2948} 2949 2950/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, 2951/// used for pointer types. 2952void ItaniumRTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) { 2953 Qualifiers Quals; 2954 QualType UnqualifiedPointeeTy = 2955 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals); 2956 2957 // Itanium C++ ABI 2.9.5p7: 2958 // __flags is a flag word describing the cv-qualification and other 2959 // attributes of the type pointed to 2960 unsigned Flags = ComputeQualifierFlags(Quals); 2961 2962 // Itanium C++ ABI 2.9.5p7: 2963 // When the abi::__pbase_type_info is for a direct or indirect pointer to an 2964 // incomplete class type, the incomplete target type flag is set. 2965 if (ContainsIncompleteClassType(UnqualifiedPointeeTy)) 2966 Flags |= PTI_Incomplete; 2967 2968 llvm::Type *UnsignedIntLTy = 2969 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy); 2970 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags)); 2971 2972 // Itanium C++ ABI 2.9.5p7: 2973 // __pointee is a pointer to the std::type_info derivation for the 2974 // unqualified type being pointed to. 2975 llvm::Constant *PointeeTypeInfo = 2976 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(UnqualifiedPointeeTy); 2977 Fields.push_back(PointeeTypeInfo); 2978} 2979 2980/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info 2981/// struct, used for member pointer types. 2982void 2983ItaniumRTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) { 2984 QualType PointeeTy = Ty->getPointeeType(); 2985 2986 Qualifiers Quals; 2987 QualType UnqualifiedPointeeTy = 2988 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals); 2989 2990 // Itanium C++ ABI 2.9.5p7: 2991 // __flags is a flag word describing the cv-qualification and other 2992 // attributes of the type pointed to. 2993 unsigned Flags = ComputeQualifierFlags(Quals); 2994 2995 const RecordType *ClassType = cast<RecordType>(Ty->getClass()); 2996 2997 // Itanium C++ ABI 2.9.5p7: 2998 // When the abi::__pbase_type_info is for a direct or indirect pointer to an 2999 // incomplete class type, the incomplete target type flag is set. 3000 if (ContainsIncompleteClassType(UnqualifiedPointeeTy)) 3001 Flags |= PTI_Incomplete; 3002 3003 if (IsIncompleteClassType(ClassType)) 3004 Flags |= PTI_ContainingClassIncomplete; 3005 3006 llvm::Type *UnsignedIntLTy = 3007 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy); 3008 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags)); 3009 3010 // Itanium C++ ABI 2.9.5p7: 3011 // __pointee is a pointer to the std::type_info derivation for the 3012 // unqualified type being pointed to. 3013 llvm::Constant *PointeeTypeInfo = 3014 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(UnqualifiedPointeeTy); 3015 Fields.push_back(PointeeTypeInfo); 3016 3017 // Itanium C++ ABI 2.9.5p9: 3018 // __context is a pointer to an abi::__class_type_info corresponding to the 3019 // class type containing the member pointed to 3020 // (e.g., the "A" in "int A::*"). 3021 Fields.push_back( 3022 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(QualType(ClassType, 0))); 3023} 3024 3025llvm::Constant *ItaniumCXXABI::getAddrOfRTTIDescriptor(QualType Ty) { 3026 return ItaniumRTTIBuilder(*this).BuildTypeInfo(Ty); 3027} 3028 3029void ItaniumCXXABI::EmitFundamentalRTTIDescriptor(QualType Type) { 3030 QualType PointerType = getContext().getPointerType(Type); 3031 QualType PointerTypeConst = getContext().getPointerType(Type.withConst()); 3032 ItaniumRTTIBuilder(*this).BuildTypeInfo(Type, true); 3033 ItaniumRTTIBuilder(*this).BuildTypeInfo(PointerType, true); 3034 ItaniumRTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true); 3035} 3036 3037void ItaniumCXXABI::EmitFundamentalRTTIDescriptors() { 3038 QualType FundamentalTypes[] = { 3039 getContext().VoidTy, getContext().NullPtrTy, 3040 getContext().BoolTy, getContext().WCharTy, 3041 getContext().CharTy, getContext().UnsignedCharTy, 3042 getContext().SignedCharTy, getContext().ShortTy, 3043 getContext().UnsignedShortTy, getContext().IntTy, 3044 getContext().UnsignedIntTy, getContext().LongTy, 3045 getContext().UnsignedLongTy, getContext().LongLongTy, 3046 getContext().UnsignedLongLongTy, getContext().HalfTy, 3047 getContext().FloatTy, getContext().DoubleTy, 3048 getContext().LongDoubleTy, getContext().Char16Ty, 3049 getContext().Char32Ty, 3050 }; 3051 for (const QualType &FundamentalType : FundamentalTypes) 3052 EmitFundamentalRTTIDescriptor(FundamentalType); 3053} 3054 3055/// What sort of uniqueness rules should we use for the RTTI for the 3056/// given type? 3057ItaniumCXXABI::RTTIUniquenessKind ItaniumCXXABI::classifyRTTIUniqueness( 3058 QualType CanTy, llvm::GlobalValue::LinkageTypes Linkage) const { 3059 if (shouldRTTIBeUnique()) 3060 return RUK_Unique; 3061 3062 // It's only necessary for linkonce_odr or weak_odr linkage. 3063 if (Linkage != llvm::GlobalValue::LinkOnceODRLinkage && 3064 Linkage != llvm::GlobalValue::WeakODRLinkage) 3065 return RUK_Unique; 3066 3067 // It's only necessary with default visibility. 3068 if (CanTy->getVisibility() != DefaultVisibility) 3069 return RUK_Unique; 3070 3071 // If we're not required to publish this symbol, hide it. 3072 if (Linkage == llvm::GlobalValue::LinkOnceODRLinkage) 3073 return RUK_NonUniqueHidden; 3074 3075 // If we're required to publish this symbol, as we might be under an 3076 // explicit instantiation, leave it with default visibility but 3077 // enable string-comparisons. 3078 assert(Linkage == llvm::GlobalValue::WeakODRLinkage); 3079 return RUK_NonUniqueVisible; 3080} 3081 3082// Find out how to codegen the complete destructor and constructor 3083namespace { 3084enum class StructorCodegen { Emit, RAUW, Alias, COMDAT }; 3085} 3086static StructorCodegen getCodegenToUse(CodeGenModule &CGM, 3087 const CXXMethodDecl *MD) { 3088 if (!CGM.getCodeGenOpts().CXXCtorDtorAliases) 3089 return StructorCodegen::Emit; 3090 3091 // The complete and base structors are not equivalent if there are any virtual 3092 // bases, so emit separate functions. 3093 if (MD->getParent()->getNumVBases()) 3094 return StructorCodegen::Emit; 3095 3096 GlobalDecl AliasDecl; 3097 if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD)) { 3098 AliasDecl = GlobalDecl(DD, Dtor_Complete); 3099 } else { 3100 const auto *CD = cast<CXXConstructorDecl>(MD); 3101 AliasDecl = GlobalDecl(CD, Ctor_Complete); 3102 } 3103 llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl); 3104 3105 if (llvm::GlobalValue::isDiscardableIfUnused(Linkage)) 3106 return StructorCodegen::RAUW; 3107 3108 // FIXME: Should we allow available_externally aliases? 3109 if (!llvm::GlobalAlias::isValidLinkage(Linkage)) 3110 return StructorCodegen::RAUW; 3111 3112 if (llvm::GlobalValue::isWeakForLinker(Linkage)) { 3113 // Only ELF supports COMDATs with arbitrary names (C5/D5). 3114 if (CGM.getTarget().getTriple().isOSBinFormatELF()) 3115 return StructorCodegen::COMDAT; 3116 return StructorCodegen::Emit; 3117 } 3118 3119 return StructorCodegen::Alias; 3120} 3121 3122static void emitConstructorDestructorAlias(CodeGenModule &CGM, 3123 GlobalDecl AliasDecl, 3124 GlobalDecl TargetDecl) { 3125 llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl); 3126 3127 StringRef MangledName = CGM.getMangledName(AliasDecl); 3128 llvm::GlobalValue *Entry = CGM.GetGlobalValue(MangledName); 3129 if (Entry && !Entry->isDeclaration()) 3130 return; 3131 3132 auto *Aliasee = cast<llvm::GlobalValue>(CGM.GetAddrOfGlobal(TargetDecl)); 3133 llvm::PointerType *AliasType = Aliasee->getType(); 3134 3135 // Create the alias with no name. 3136 auto *Alias = llvm::GlobalAlias::create( 3137 AliasType->getElementType(), 0, Linkage, "", Aliasee, &CGM.getModule()); 3138 3139 // Switch any previous uses to the alias. 3140 if (Entry) { 3141 assert(Entry->getType() == AliasType && 3142 "declaration exists with different type"); 3143 Alias->takeName(Entry); 3144 Entry->replaceAllUsesWith(Alias); 3145 Entry->eraseFromParent(); 3146 } else { 3147 Alias->setName(MangledName); 3148 } 3149 3150 // Finally, set up the alias with its proper name and attributes. 3151 CGM.setAliasAttributes(cast<NamedDecl>(AliasDecl.getDecl()), Alias); 3152} 3153 3154void ItaniumCXXABI::emitCXXStructor(const CXXMethodDecl *MD, 3155 StructorType Type) { 3156 auto *CD = dyn_cast<CXXConstructorDecl>(MD); 3157 const CXXDestructorDecl *DD = CD ? nullptr : cast<CXXDestructorDecl>(MD); 3158 3159 StructorCodegen CGType = getCodegenToUse(CGM, MD); 3160 3161 if (Type == StructorType::Complete) { 3162 GlobalDecl CompleteDecl; 3163 GlobalDecl BaseDecl; 3164 if (CD) { 3165 CompleteDecl = GlobalDecl(CD, Ctor_Complete); 3166 BaseDecl = GlobalDecl(CD, Ctor_Base); 3167 } else { 3168 CompleteDecl = GlobalDecl(DD, Dtor_Complete); 3169 BaseDecl = GlobalDecl(DD, Dtor_Base); 3170 } 3171 3172 if (CGType == StructorCodegen::Alias || CGType == StructorCodegen::COMDAT) { 3173 emitConstructorDestructorAlias(CGM, CompleteDecl, BaseDecl); 3174 return; 3175 } 3176 3177 if (CGType == StructorCodegen::RAUW) { 3178 StringRef MangledName = CGM.getMangledName(CompleteDecl); 3179 auto *Aliasee = cast<llvm::GlobalValue>(CGM.GetAddrOfGlobal(BaseDecl)); 3180 CGM.addReplacement(MangledName, Aliasee); 3181 return; 3182 } 3183 } 3184 3185 // The base destructor is equivalent to the base destructor of its 3186 // base class if there is exactly one non-virtual base class with a 3187 // non-trivial destructor, there are no fields with a non-trivial 3188 // destructor, and the body of the destructor is trivial. 3189 if (DD && Type == StructorType::Base && CGType != StructorCodegen::COMDAT && 3190 !CGM.TryEmitBaseDestructorAsAlias(DD)) 3191 return; 3192 3193 llvm::Function *Fn = CGM.codegenCXXStructor(MD, Type); 3194 3195 if (CGType == StructorCodegen::COMDAT) { 3196 SmallString<256> Buffer; 3197 llvm::raw_svector_ostream Out(Buffer); 3198 if (DD) 3199 getMangleContext().mangleCXXDtorComdat(DD, Out); 3200 else 3201 getMangleContext().mangleCXXCtorComdat(CD, Out); 3202 llvm::Comdat *C = CGM.getModule().getOrInsertComdat(Out.str()); 3203 Fn->setComdat(C); 3204 } 3205}
| 345 case TargetCXXABI::GenericItanium: 346 if (CGM.getContext().getTargetInfo().getTriple().getArch() 347 == llvm::Triple::le32) { 348 // For PNaCl, use ARM-style method pointers so that PNaCl code 349 // does not assume anything about the alignment of function 350 // pointers. 351 return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true, 352 /* UseARMGuardVarABI = */ false); 353 } 354 return new ItaniumCXXABI(CGM); 355 356 case TargetCXXABI::Microsoft: 357 llvm_unreachable("Microsoft ABI is not Itanium-based"); 358 } 359 llvm_unreachable("bad ABI kind"); 360} 361 362llvm::Type * 363ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) { 364 if (MPT->isMemberDataPointer()) 365 return CGM.PtrDiffTy; 366 return llvm::StructType::get(CGM.PtrDiffTy, CGM.PtrDiffTy, nullptr); 367} 368 369/// In the Itanium and ARM ABIs, method pointers have the form: 370/// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr; 371/// 372/// In the Itanium ABI: 373/// - method pointers are virtual if (memptr.ptr & 1) is nonzero 374/// - the this-adjustment is (memptr.adj) 375/// - the virtual offset is (memptr.ptr - 1) 376/// 377/// In the ARM ABI: 378/// - method pointers are virtual if (memptr.adj & 1) is nonzero 379/// - the this-adjustment is (memptr.adj >> 1) 380/// - the virtual offset is (memptr.ptr) 381/// ARM uses 'adj' for the virtual flag because Thumb functions 382/// may be only single-byte aligned. 383/// 384/// If the member is virtual, the adjusted 'this' pointer points 385/// to a vtable pointer from which the virtual offset is applied. 386/// 387/// If the member is non-virtual, memptr.ptr is the address of 388/// the function to call. 389llvm::Value *ItaniumCXXABI::EmitLoadOfMemberFunctionPointer( 390 CodeGenFunction &CGF, const Expr *E, llvm::Value *&This, 391 llvm::Value *MemFnPtr, const MemberPointerType *MPT) { 392 CGBuilderTy &Builder = CGF.Builder; 393 394 const FunctionProtoType *FPT = 395 MPT->getPointeeType()->getAs<FunctionProtoType>(); 396 const CXXRecordDecl *RD = 397 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); 398 399 llvm::FunctionType *FTy = 400 CGM.getTypes().GetFunctionType( 401 CGM.getTypes().arrangeCXXMethodType(RD, FPT)); 402 403 llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(CGM.PtrDiffTy, 1); 404 405 llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual"); 406 llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual"); 407 llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end"); 408 409 // Extract memptr.adj, which is in the second field. 410 llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj"); 411 412 // Compute the true adjustment. 413 llvm::Value *Adj = RawAdj; 414 if (UseARMMethodPtrABI) 415 Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted"); 416 417 // Apply the adjustment and cast back to the original struct type 418 // for consistency. 419 llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy()); 420 Ptr = Builder.CreateInBoundsGEP(Ptr, Adj); 421 This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted"); 422 423 // Load the function pointer. 424 llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr"); 425 426 // If the LSB in the function pointer is 1, the function pointer points to 427 // a virtual function. 428 llvm::Value *IsVirtual; 429 if (UseARMMethodPtrABI) 430 IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1); 431 else 432 IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1); 433 IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual"); 434 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual); 435 436 // In the virtual path, the adjustment left 'This' pointing to the 437 // vtable of the correct base subobject. The "function pointer" is an 438 // offset within the vtable (+1 for the virtual flag on non-ARM). 439 CGF.EmitBlock(FnVirtual); 440 441 // Cast the adjusted this to a pointer to vtable pointer and load. 442 llvm::Type *VTableTy = Builder.getInt8PtrTy(); 443 llvm::Value *VTable = CGF.GetVTablePtr(This, VTableTy); 444 445 // Apply the offset. 446 llvm::Value *VTableOffset = FnAsInt; 447 if (!UseARMMethodPtrABI) 448 VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1); 449 VTable = Builder.CreateGEP(VTable, VTableOffset); 450 451 // Load the virtual function to call. 452 VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo()); 453 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn"); 454 CGF.EmitBranch(FnEnd); 455 456 // In the non-virtual path, the function pointer is actually a 457 // function pointer. 458 CGF.EmitBlock(FnNonVirtual); 459 llvm::Value *NonVirtualFn = 460 Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn"); 461 462 // We're done. 463 CGF.EmitBlock(FnEnd); 464 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo(), 2); 465 Callee->addIncoming(VirtualFn, FnVirtual); 466 Callee->addIncoming(NonVirtualFn, FnNonVirtual); 467 return Callee; 468} 469 470/// Compute an l-value by applying the given pointer-to-member to a 471/// base object. 472llvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress( 473 CodeGenFunction &CGF, const Expr *E, llvm::Value *Base, llvm::Value *MemPtr, 474 const MemberPointerType *MPT) { 475 assert(MemPtr->getType() == CGM.PtrDiffTy); 476 477 CGBuilderTy &Builder = CGF.Builder; 478 479 unsigned AS = Base->getType()->getPointerAddressSpace(); 480 481 // Cast to char*. 482 Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS)); 483 484 // Apply the offset, which we assume is non-null. 485 llvm::Value *Addr = Builder.CreateInBoundsGEP(Base, MemPtr, "memptr.offset"); 486 487 // Cast the address to the appropriate pointer type, adopting the 488 // address space of the base pointer. 489 llvm::Type *PType 490 = CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS); 491 return Builder.CreateBitCast(Addr, PType); 492} 493 494/// Perform a bitcast, derived-to-base, or base-to-derived member pointer 495/// conversion. 496/// 497/// Bitcast conversions are always a no-op under Itanium. 498/// 499/// Obligatory offset/adjustment diagram: 500/// <-- offset --> <-- adjustment --> 501/// |--------------------------|----------------------|--------------------| 502/// ^Derived address point ^Base address point ^Member address point 503/// 504/// So when converting a base member pointer to a derived member pointer, 505/// we add the offset to the adjustment because the address point has 506/// decreased; and conversely, when converting a derived MP to a base MP 507/// we subtract the offset from the adjustment because the address point 508/// has increased. 509/// 510/// The standard forbids (at compile time) conversion to and from 511/// virtual bases, which is why we don't have to consider them here. 512/// 513/// The standard forbids (at run time) casting a derived MP to a base 514/// MP when the derived MP does not point to a member of the base. 515/// This is why -1 is a reasonable choice for null data member 516/// pointers. 517llvm::Value * 518ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF, 519 const CastExpr *E, 520 llvm::Value *src) { 521 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || 522 E->getCastKind() == CK_BaseToDerivedMemberPointer || 523 E->getCastKind() == CK_ReinterpretMemberPointer); 524 525 // Under Itanium, reinterprets don't require any additional processing. 526 if (E->getCastKind() == CK_ReinterpretMemberPointer) return src; 527 528 // Use constant emission if we can. 529 if (isa<llvm::Constant>(src)) 530 return EmitMemberPointerConversion(E, cast<llvm::Constant>(src)); 531 532 llvm::Constant *adj = getMemberPointerAdjustment(E); 533 if (!adj) return src; 534 535 CGBuilderTy &Builder = CGF.Builder; 536 bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); 537 538 const MemberPointerType *destTy = 539 E->getType()->castAs<MemberPointerType>(); 540 541 // For member data pointers, this is just a matter of adding the 542 // offset if the source is non-null. 543 if (destTy->isMemberDataPointer()) { 544 llvm::Value *dst; 545 if (isDerivedToBase) 546 dst = Builder.CreateNSWSub(src, adj, "adj"); 547 else 548 dst = Builder.CreateNSWAdd(src, adj, "adj"); 549 550 // Null check. 551 llvm::Value *null = llvm::Constant::getAllOnesValue(src->getType()); 552 llvm::Value *isNull = Builder.CreateICmpEQ(src, null, "memptr.isnull"); 553 return Builder.CreateSelect(isNull, src, dst); 554 } 555 556 // The this-adjustment is left-shifted by 1 on ARM. 557 if (UseARMMethodPtrABI) { 558 uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue(); 559 offset <<= 1; 560 adj = llvm::ConstantInt::get(adj->getType(), offset); 561 } 562 563 llvm::Value *srcAdj = Builder.CreateExtractValue(src, 1, "src.adj"); 564 llvm::Value *dstAdj; 565 if (isDerivedToBase) 566 dstAdj = Builder.CreateNSWSub(srcAdj, adj, "adj"); 567 else 568 dstAdj = Builder.CreateNSWAdd(srcAdj, adj, "adj"); 569 570 return Builder.CreateInsertValue(src, dstAdj, 1); 571} 572 573llvm::Constant * 574ItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E, 575 llvm::Constant *src) { 576 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || 577 E->getCastKind() == CK_BaseToDerivedMemberPointer || 578 E->getCastKind() == CK_ReinterpretMemberPointer); 579 580 // Under Itanium, reinterprets don't require any additional processing. 581 if (E->getCastKind() == CK_ReinterpretMemberPointer) return src; 582 583 // If the adjustment is trivial, we don't need to do anything. 584 llvm::Constant *adj = getMemberPointerAdjustment(E); 585 if (!adj) return src; 586 587 bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); 588 589 const MemberPointerType *destTy = 590 E->getType()->castAs<MemberPointerType>(); 591 592 // For member data pointers, this is just a matter of adding the 593 // offset if the source is non-null. 594 if (destTy->isMemberDataPointer()) { 595 // null maps to null. 596 if (src->isAllOnesValue()) return src; 597 598 if (isDerivedToBase) 599 return llvm::ConstantExpr::getNSWSub(src, adj); 600 else 601 return llvm::ConstantExpr::getNSWAdd(src, adj); 602 } 603 604 // The this-adjustment is left-shifted by 1 on ARM. 605 if (UseARMMethodPtrABI) { 606 uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue(); 607 offset <<= 1; 608 adj = llvm::ConstantInt::get(adj->getType(), offset); 609 } 610 611 llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1); 612 llvm::Constant *dstAdj; 613 if (isDerivedToBase) 614 dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj); 615 else 616 dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj); 617 618 return llvm::ConstantExpr::getInsertValue(src, dstAdj, 1); 619} 620 621llvm::Constant * 622ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) { 623 // Itanium C++ ABI 2.3: 624 // A NULL pointer is represented as -1. 625 if (MPT->isMemberDataPointer()) 626 return llvm::ConstantInt::get(CGM.PtrDiffTy, -1ULL, /*isSigned=*/true); 627 628 llvm::Constant *Zero = llvm::ConstantInt::get(CGM.PtrDiffTy, 0); 629 llvm::Constant *Values[2] = { Zero, Zero }; 630 return llvm::ConstantStruct::getAnon(Values); 631} 632 633llvm::Constant * 634ItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT, 635 CharUnits offset) { 636 // Itanium C++ ABI 2.3: 637 // A pointer to data member is an offset from the base address of 638 // the class object containing it, represented as a ptrdiff_t 639 return llvm::ConstantInt::get(CGM.PtrDiffTy, offset.getQuantity()); 640} 641 642llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) { 643 return BuildMemberPointer(MD, CharUnits::Zero()); 644} 645 646llvm::Constant *ItaniumCXXABI::BuildMemberPointer(const CXXMethodDecl *MD, 647 CharUnits ThisAdjustment) { 648 assert(MD->isInstance() && "Member function must not be static!"); 649 MD = MD->getCanonicalDecl(); 650 651 CodeGenTypes &Types = CGM.getTypes(); 652 653 // Get the function pointer (or index if this is a virtual function). 654 llvm::Constant *MemPtr[2]; 655 if (MD->isVirtual()) { 656 uint64_t Index = CGM.getItaniumVTableContext().getMethodVTableIndex(MD); 657 658 const ASTContext &Context = getContext(); 659 CharUnits PointerWidth = 660 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); 661 uint64_t VTableOffset = (Index * PointerWidth.getQuantity()); 662 663 if (UseARMMethodPtrABI) { 664 // ARM C++ ABI 3.2.1: 665 // This ABI specifies that adj contains twice the this 666 // adjustment, plus 1 if the member function is virtual. The 667 // least significant bit of adj then makes exactly the same 668 // discrimination as the least significant bit of ptr does for 669 // Itanium. 670 MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset); 671 MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy, 672 2 * ThisAdjustment.getQuantity() + 1); 673 } else { 674 // Itanium C++ ABI 2.3: 675 // For a virtual function, [the pointer field] is 1 plus the 676 // virtual table offset (in bytes) of the function, 677 // represented as a ptrdiff_t. 678 MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset + 1); 679 MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy, 680 ThisAdjustment.getQuantity()); 681 } 682 } else { 683 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); 684 llvm::Type *Ty; 685 // Check whether the function has a computable LLVM signature. 686 if (Types.isFuncTypeConvertible(FPT)) { 687 // The function has a computable LLVM signature; use the correct type. 688 Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD)); 689 } else { 690 // Use an arbitrary non-function type to tell GetAddrOfFunction that the 691 // function type is incomplete. 692 Ty = CGM.PtrDiffTy; 693 } 694 llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty); 695 696 MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, CGM.PtrDiffTy); 697 MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy, 698 (UseARMMethodPtrABI ? 2 : 1) * 699 ThisAdjustment.getQuantity()); 700 } 701 702 return llvm::ConstantStruct::getAnon(MemPtr); 703} 704 705llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const APValue &MP, 706 QualType MPType) { 707 const MemberPointerType *MPT = MPType->castAs<MemberPointerType>(); 708 const ValueDecl *MPD = MP.getMemberPointerDecl(); 709 if (!MPD) 710 return EmitNullMemberPointer(MPT); 711 712 CharUnits ThisAdjustment = getMemberPointerPathAdjustment(MP); 713 714 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD)) 715 return BuildMemberPointer(MD, ThisAdjustment); 716 717 CharUnits FieldOffset = 718 getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD)); 719 return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset); 720} 721 722/// The comparison algorithm is pretty easy: the member pointers are 723/// the same if they're either bitwise identical *or* both null. 724/// 725/// ARM is different here only because null-ness is more complicated. 726llvm::Value * 727ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF, 728 llvm::Value *L, 729 llvm::Value *R, 730 const MemberPointerType *MPT, 731 bool Inequality) { 732 CGBuilderTy &Builder = CGF.Builder; 733 734 llvm::ICmpInst::Predicate Eq; 735 llvm::Instruction::BinaryOps And, Or; 736 if (Inequality) { 737 Eq = llvm::ICmpInst::ICMP_NE; 738 And = llvm::Instruction::Or; 739 Or = llvm::Instruction::And; 740 } else { 741 Eq = llvm::ICmpInst::ICMP_EQ; 742 And = llvm::Instruction::And; 743 Or = llvm::Instruction::Or; 744 } 745 746 // Member data pointers are easy because there's a unique null 747 // value, so it just comes down to bitwise equality. 748 if (MPT->isMemberDataPointer()) 749 return Builder.CreateICmp(Eq, L, R); 750 751 // For member function pointers, the tautologies are more complex. 752 // The Itanium tautology is: 753 // (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj)) 754 // The ARM tautology is: 755 // (L == R) <==> (L.ptr == R.ptr && 756 // (L.adj == R.adj || 757 // (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0))) 758 // The inequality tautologies have exactly the same structure, except 759 // applying De Morgan's laws. 760 761 llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr"); 762 llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr"); 763 764 // This condition tests whether L.ptr == R.ptr. This must always be 765 // true for equality to hold. 766 llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr"); 767 768 // This condition, together with the assumption that L.ptr == R.ptr, 769 // tests whether the pointers are both null. ARM imposes an extra 770 // condition. 771 llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType()); 772 llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null"); 773 774 // This condition tests whether L.adj == R.adj. If this isn't 775 // true, the pointers are unequal unless they're both null. 776 llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj"); 777 llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj"); 778 llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj"); 779 780 // Null member function pointers on ARM clear the low bit of Adj, 781 // so the zero condition has to check that neither low bit is set. 782 if (UseARMMethodPtrABI) { 783 llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1); 784 785 // Compute (l.adj | r.adj) & 1 and test it against zero. 786 llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj"); 787 llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One); 788 llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero, 789 "cmp.or.adj"); 790 EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero); 791 } 792 793 // Tie together all our conditions. 794 llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq); 795 Result = Builder.CreateBinOp(And, PtrEq, Result, 796 Inequality ? "memptr.ne" : "memptr.eq"); 797 return Result; 798} 799 800llvm::Value * 801ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF, 802 llvm::Value *MemPtr, 803 const MemberPointerType *MPT) { 804 CGBuilderTy &Builder = CGF.Builder; 805 806 /// For member data pointers, this is just a check against -1. 807 if (MPT->isMemberDataPointer()) { 808 assert(MemPtr->getType() == CGM.PtrDiffTy); 809 llvm::Value *NegativeOne = 810 llvm::Constant::getAllOnesValue(MemPtr->getType()); 811 return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool"); 812 } 813 814 // In Itanium, a member function pointer is not null if 'ptr' is not null. 815 llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr"); 816 817 llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0); 818 llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool"); 819 820 // On ARM, a member function pointer is also non-null if the low bit of 'adj' 821 // (the virtual bit) is set. 822 if (UseARMMethodPtrABI) { 823 llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1); 824 llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj"); 825 llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit"); 826 llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero, 827 "memptr.isvirtual"); 828 Result = Builder.CreateOr(Result, IsVirtual); 829 } 830 831 return Result; 832} 833 834bool ItaniumCXXABI::classifyReturnType(CGFunctionInfo &FI) const { 835 const CXXRecordDecl *RD = FI.getReturnType()->getAsCXXRecordDecl(); 836 if (!RD) 837 return false; 838 839 // Return indirectly if we have a non-trivial copy ctor or non-trivial dtor. 840 // FIXME: Use canCopyArgument() when it is fixed to handle lazily declared 841 // special members. 842 if (RD->hasNonTrivialDestructor() || RD->hasNonTrivialCopyConstructor()) { 843 FI.getReturnInfo() = ABIArgInfo::getIndirect(0, /*ByVal=*/false); 844 return true; 845 } 846 return false; 847} 848 849/// The Itanium ABI requires non-zero initialization only for data 850/// member pointers, for which '0' is a valid offset. 851bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) { 852 return MPT->getPointeeType()->isFunctionType(); 853} 854 855/// The Itanium ABI always places an offset to the complete object 856/// at entry -2 in the vtable. 857void ItaniumCXXABI::emitVirtualObjectDelete(CodeGenFunction &CGF, 858 const CXXDeleteExpr *DE, 859 llvm::Value *Ptr, 860 QualType ElementType, 861 const CXXDestructorDecl *Dtor) { 862 bool UseGlobalDelete = DE->isGlobalDelete(); 863 if (UseGlobalDelete) { 864 // Derive the complete-object pointer, which is what we need 865 // to pass to the deallocation function. 866 867 // Grab the vtable pointer as an intptr_t*. 868 llvm::Value *VTable = CGF.GetVTablePtr(Ptr, CGF.IntPtrTy->getPointerTo()); 869 870 // Track back to entry -2 and pull out the offset there. 871 llvm::Value *OffsetPtr = CGF.Builder.CreateConstInBoundsGEP1_64( 872 VTable, -2, "complete-offset.ptr"); 873 llvm::LoadInst *Offset = CGF.Builder.CreateLoad(OffsetPtr); 874 Offset->setAlignment(CGF.PointerAlignInBytes); 875 876 // Apply the offset. 877 llvm::Value *CompletePtr = CGF.Builder.CreateBitCast(Ptr, CGF.Int8PtrTy); 878 CompletePtr = CGF.Builder.CreateInBoundsGEP(CompletePtr, Offset); 879 880 // If we're supposed to call the global delete, make sure we do so 881 // even if the destructor throws. 882 CGF.pushCallObjectDeleteCleanup(DE->getOperatorDelete(), CompletePtr, 883 ElementType); 884 } 885 886 // FIXME: Provide a source location here even though there's no 887 // CXXMemberCallExpr for dtor call. 888 CXXDtorType DtorType = UseGlobalDelete ? Dtor_Complete : Dtor_Deleting; 889 EmitVirtualDestructorCall(CGF, Dtor, DtorType, Ptr, /*CE=*/nullptr); 890 891 if (UseGlobalDelete) 892 CGF.PopCleanupBlock(); 893} 894 895void ItaniumCXXABI::emitRethrow(CodeGenFunction &CGF, bool isNoReturn) { 896 // void __cxa_rethrow(); 897 898 llvm::FunctionType *FTy = 899 llvm::FunctionType::get(CGM.VoidTy, /*IsVarArgs=*/false); 900 901 llvm::Constant *Fn = CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow"); 902 903 if (isNoReturn) 904 CGF.EmitNoreturnRuntimeCallOrInvoke(Fn, None); 905 else 906 CGF.EmitRuntimeCallOrInvoke(Fn); 907} 908 909static llvm::Constant *getItaniumDynamicCastFn(CodeGenFunction &CGF) { 910 // void *__dynamic_cast(const void *sub, 911 // const abi::__class_type_info *src, 912 // const abi::__class_type_info *dst, 913 // std::ptrdiff_t src2dst_offset); 914 915 llvm::Type *Int8PtrTy = CGF.Int8PtrTy; 916 llvm::Type *PtrDiffTy = 917 CGF.ConvertType(CGF.getContext().getPointerDiffType()); 918 919 llvm::Type *Args[4] = { Int8PtrTy, Int8PtrTy, Int8PtrTy, PtrDiffTy }; 920 921 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false); 922 923 // Mark the function as nounwind readonly. 924 llvm::Attribute::AttrKind FuncAttrs[] = { llvm::Attribute::NoUnwind, 925 llvm::Attribute::ReadOnly }; 926 llvm::AttributeSet Attrs = llvm::AttributeSet::get( 927 CGF.getLLVMContext(), llvm::AttributeSet::FunctionIndex, FuncAttrs); 928 929 return CGF.CGM.CreateRuntimeFunction(FTy, "__dynamic_cast", Attrs); 930} 931 932static llvm::Constant *getBadCastFn(CodeGenFunction &CGF) { 933 // void __cxa_bad_cast(); 934 llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false); 935 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_cast"); 936} 937 938/// \brief Compute the src2dst_offset hint as described in the 939/// Itanium C++ ABI [2.9.7] 940static CharUnits computeOffsetHint(ASTContext &Context, 941 const CXXRecordDecl *Src, 942 const CXXRecordDecl *Dst) { 943 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 944 /*DetectVirtual=*/false); 945 946 // If Dst is not derived from Src we can skip the whole computation below and 947 // return that Src is not a public base of Dst. Record all inheritance paths. 948 if (!Dst->isDerivedFrom(Src, Paths)) 949 return CharUnits::fromQuantity(-2ULL); 950 951 unsigned NumPublicPaths = 0; 952 CharUnits Offset; 953 954 // Now walk all possible inheritance paths. 955 for (CXXBasePaths::paths_iterator I = Paths.begin(), E = Paths.end(); I != E; 956 ++I) { 957 if (I->Access != AS_public) // Ignore non-public inheritance. 958 continue; 959 960 ++NumPublicPaths; 961 962 for (CXXBasePath::iterator J = I->begin(), JE = I->end(); J != JE; ++J) { 963 // If the path contains a virtual base class we can't give any hint. 964 // -1: no hint. 965 if (J->Base->isVirtual()) 966 return CharUnits::fromQuantity(-1ULL); 967 968 if (NumPublicPaths > 1) // Won't use offsets, skip computation. 969 continue; 970 971 // Accumulate the base class offsets. 972 const ASTRecordLayout &L = Context.getASTRecordLayout(J->Class); 973 Offset += L.getBaseClassOffset(J->Base->getType()->getAsCXXRecordDecl()); 974 } 975 } 976 977 // -2: Src is not a public base of Dst. 978 if (NumPublicPaths == 0) 979 return CharUnits::fromQuantity(-2ULL); 980 981 // -3: Src is a multiple public base type but never a virtual base type. 982 if (NumPublicPaths > 1) 983 return CharUnits::fromQuantity(-3ULL); 984 985 // Otherwise, the Src type is a unique public nonvirtual base type of Dst. 986 // Return the offset of Src from the origin of Dst. 987 return Offset; 988} 989 990static llvm::Constant *getBadTypeidFn(CodeGenFunction &CGF) { 991 // void __cxa_bad_typeid(); 992 llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false); 993 994 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid"); 995} 996 997bool ItaniumCXXABI::shouldTypeidBeNullChecked(bool IsDeref, 998 QualType SrcRecordTy) { 999 return IsDeref; 1000} 1001 1002void ItaniumCXXABI::EmitBadTypeidCall(CodeGenFunction &CGF) { 1003 llvm::Value *Fn = getBadTypeidFn(CGF); 1004 CGF.EmitRuntimeCallOrInvoke(Fn).setDoesNotReturn(); 1005 CGF.Builder.CreateUnreachable(); 1006} 1007 1008llvm::Value *ItaniumCXXABI::EmitTypeid(CodeGenFunction &CGF, 1009 QualType SrcRecordTy, 1010 llvm::Value *ThisPtr, 1011 llvm::Type *StdTypeInfoPtrTy) { 1012 llvm::Value *Value = 1013 CGF.GetVTablePtr(ThisPtr, StdTypeInfoPtrTy->getPointerTo()); 1014 1015 // Load the type info. 1016 Value = CGF.Builder.CreateConstInBoundsGEP1_64(Value, -1ULL); 1017 return CGF.Builder.CreateLoad(Value); 1018} 1019 1020bool ItaniumCXXABI::shouldDynamicCastCallBeNullChecked(bool SrcIsPtr, 1021 QualType SrcRecordTy) { 1022 return SrcIsPtr; 1023} 1024 1025llvm::Value *ItaniumCXXABI::EmitDynamicCastCall( 1026 CodeGenFunction &CGF, llvm::Value *Value, QualType SrcRecordTy, 1027 QualType DestTy, QualType DestRecordTy, llvm::BasicBlock *CastEnd) { 1028 llvm::Type *PtrDiffLTy = 1029 CGF.ConvertType(CGF.getContext().getPointerDiffType()); 1030 llvm::Type *DestLTy = CGF.ConvertType(DestTy); 1031 1032 llvm::Value *SrcRTTI = 1033 CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType()); 1034 llvm::Value *DestRTTI = 1035 CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType()); 1036 1037 // Compute the offset hint. 1038 const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl(); 1039 const CXXRecordDecl *DestDecl = DestRecordTy->getAsCXXRecordDecl(); 1040 llvm::Value *OffsetHint = llvm::ConstantInt::get( 1041 PtrDiffLTy, 1042 computeOffsetHint(CGF.getContext(), SrcDecl, DestDecl).getQuantity()); 1043 1044 // Emit the call to __dynamic_cast. 1045 Value = CGF.EmitCastToVoidPtr(Value); 1046 1047 llvm::Value *args[] = {Value, SrcRTTI, DestRTTI, OffsetHint}; 1048 Value = CGF.EmitNounwindRuntimeCall(getItaniumDynamicCastFn(CGF), args); 1049 Value = CGF.Builder.CreateBitCast(Value, DestLTy); 1050 1051 /// C++ [expr.dynamic.cast]p9: 1052 /// A failed cast to reference type throws std::bad_cast 1053 if (DestTy->isReferenceType()) { 1054 llvm::BasicBlock *BadCastBlock = 1055 CGF.createBasicBlock("dynamic_cast.bad_cast"); 1056 1057 llvm::Value *IsNull = CGF.Builder.CreateIsNull(Value); 1058 CGF.Builder.CreateCondBr(IsNull, BadCastBlock, CastEnd); 1059 1060 CGF.EmitBlock(BadCastBlock); 1061 EmitBadCastCall(CGF); 1062 } 1063 1064 return Value; 1065} 1066 1067llvm::Value *ItaniumCXXABI::EmitDynamicCastToVoid(CodeGenFunction &CGF, 1068 llvm::Value *Value, 1069 QualType SrcRecordTy, 1070 QualType DestTy) { 1071 llvm::Type *PtrDiffLTy = 1072 CGF.ConvertType(CGF.getContext().getPointerDiffType()); 1073 llvm::Type *DestLTy = CGF.ConvertType(DestTy); 1074 1075 // Get the vtable pointer. 1076 llvm::Value *VTable = CGF.GetVTablePtr(Value, PtrDiffLTy->getPointerTo()); 1077 1078 // Get the offset-to-top from the vtable. 1079 llvm::Value *OffsetToTop = 1080 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, -2ULL); 1081 OffsetToTop = CGF.Builder.CreateLoad(OffsetToTop, "offset.to.top"); 1082 1083 // Finally, add the offset to the pointer. 1084 Value = CGF.EmitCastToVoidPtr(Value); 1085 Value = CGF.Builder.CreateInBoundsGEP(Value, OffsetToTop); 1086 1087 return CGF.Builder.CreateBitCast(Value, DestLTy); 1088} 1089 1090bool ItaniumCXXABI::EmitBadCastCall(CodeGenFunction &CGF) { 1091 llvm::Value *Fn = getBadCastFn(CGF); 1092 CGF.EmitRuntimeCallOrInvoke(Fn).setDoesNotReturn(); 1093 CGF.Builder.CreateUnreachable(); 1094 return true; 1095} 1096 1097llvm::Value * 1098ItaniumCXXABI::GetVirtualBaseClassOffset(CodeGenFunction &CGF, 1099 llvm::Value *This, 1100 const CXXRecordDecl *ClassDecl, 1101 const CXXRecordDecl *BaseClassDecl) { 1102 llvm::Value *VTablePtr = CGF.GetVTablePtr(This, CGM.Int8PtrTy); 1103 CharUnits VBaseOffsetOffset = 1104 CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(ClassDecl, 1105 BaseClassDecl); 1106 1107 llvm::Value *VBaseOffsetPtr = 1108 CGF.Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetOffset.getQuantity(), 1109 "vbase.offset.ptr"); 1110 VBaseOffsetPtr = CGF.Builder.CreateBitCast(VBaseOffsetPtr, 1111 CGM.PtrDiffTy->getPointerTo()); 1112 1113 llvm::Value *VBaseOffset = 1114 CGF.Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset"); 1115 1116 return VBaseOffset; 1117} 1118 1119void ItaniumCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) { 1120 // Just make sure we're in sync with TargetCXXABI. 1121 assert(CGM.getTarget().getCXXABI().hasConstructorVariants()); 1122 1123 // The constructor used for constructing this as a base class; 1124 // ignores virtual bases. 1125 CGM.EmitGlobal(GlobalDecl(D, Ctor_Base)); 1126 1127 // The constructor used for constructing this as a complete class; 1128 // constructs the virtual bases, then calls the base constructor. 1129 if (!D->getParent()->isAbstract()) { 1130 // We don't need to emit the complete ctor if the class is abstract. 1131 CGM.EmitGlobal(GlobalDecl(D, Ctor_Complete)); 1132 } 1133} 1134 1135void 1136ItaniumCXXABI::buildStructorSignature(const CXXMethodDecl *MD, StructorType T, 1137 SmallVectorImpl<CanQualType> &ArgTys) { 1138 ASTContext &Context = getContext(); 1139 1140 // All parameters are already in place except VTT, which goes after 'this'. 1141 // These are Clang types, so we don't need to worry about sret yet. 1142 1143 // Check if we need to add a VTT parameter (which has type void **). 1144 if (T == StructorType::Base && MD->getParent()->getNumVBases() != 0) 1145 ArgTys.insert(ArgTys.begin() + 1, 1146 Context.getPointerType(Context.VoidPtrTy)); 1147} 1148 1149void ItaniumCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) { 1150 // The destructor used for destructing this as a base class; ignores 1151 // virtual bases. 1152 CGM.EmitGlobal(GlobalDecl(D, Dtor_Base)); 1153 1154 // The destructor used for destructing this as a most-derived class; 1155 // call the base destructor and then destructs any virtual bases. 1156 CGM.EmitGlobal(GlobalDecl(D, Dtor_Complete)); 1157 1158 // The destructor in a virtual table is always a 'deleting' 1159 // destructor, which calls the complete destructor and then uses the 1160 // appropriate operator delete. 1161 if (D->isVirtual()) 1162 CGM.EmitGlobal(GlobalDecl(D, Dtor_Deleting)); 1163} 1164 1165void ItaniumCXXABI::addImplicitStructorParams(CodeGenFunction &CGF, 1166 QualType &ResTy, 1167 FunctionArgList &Params) { 1168 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); 1169 assert(isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)); 1170 1171 // Check if we need a VTT parameter as well. 1172 if (NeedsVTTParameter(CGF.CurGD)) { 1173 ASTContext &Context = getContext(); 1174 1175 // FIXME: avoid the fake decl 1176 QualType T = Context.getPointerType(Context.VoidPtrTy); 1177 ImplicitParamDecl *VTTDecl 1178 = ImplicitParamDecl::Create(Context, nullptr, MD->getLocation(), 1179 &Context.Idents.get("vtt"), T); 1180 Params.insert(Params.begin() + 1, VTTDecl); 1181 getStructorImplicitParamDecl(CGF) = VTTDecl; 1182 } 1183} 1184 1185void ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) { 1186 /// Initialize the 'this' slot. 1187 EmitThisParam(CGF); 1188 1189 /// Initialize the 'vtt' slot if needed. 1190 if (getStructorImplicitParamDecl(CGF)) { 1191 getStructorImplicitParamValue(CGF) = CGF.Builder.CreateLoad( 1192 CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)), "vtt"); 1193 } 1194 1195 /// If this is a function that the ABI specifies returns 'this', initialize 1196 /// the return slot to 'this' at the start of the function. 1197 /// 1198 /// Unlike the setting of return types, this is done within the ABI 1199 /// implementation instead of by clients of CGCXXABI because: 1200 /// 1) getThisValue is currently protected 1201 /// 2) in theory, an ABI could implement 'this' returns some other way; 1202 /// HasThisReturn only specifies a contract, not the implementation 1203 if (HasThisReturn(CGF.CurGD)) 1204 CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue); 1205} 1206 1207unsigned ItaniumCXXABI::addImplicitConstructorArgs( 1208 CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type, 1209 bool ForVirtualBase, bool Delegating, CallArgList &Args) { 1210 if (!NeedsVTTParameter(GlobalDecl(D, Type))) 1211 return 0; 1212 1213 // Insert the implicit 'vtt' argument as the second argument. 1214 llvm::Value *VTT = 1215 CGF.GetVTTParameter(GlobalDecl(D, Type), ForVirtualBase, Delegating); 1216 QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy); 1217 Args.insert(Args.begin() + 1, 1218 CallArg(RValue::get(VTT), VTTTy, /*needscopy=*/false)); 1219 return 1; // Added one arg. 1220} 1221 1222void ItaniumCXXABI::EmitDestructorCall(CodeGenFunction &CGF, 1223 const CXXDestructorDecl *DD, 1224 CXXDtorType Type, bool ForVirtualBase, 1225 bool Delegating, llvm::Value *This) { 1226 GlobalDecl GD(DD, Type); 1227 llvm::Value *VTT = CGF.GetVTTParameter(GD, ForVirtualBase, Delegating); 1228 QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy); 1229 1230 llvm::Value *Callee = nullptr; 1231 if (getContext().getLangOpts().AppleKext) 1232 Callee = CGF.BuildAppleKextVirtualDestructorCall(DD, Type, DD->getParent()); 1233 1234 if (!Callee) 1235 Callee = CGM.getAddrOfCXXStructor(DD, getFromDtorType(Type)); 1236 1237 CGF.EmitCXXMemberOrOperatorCall(DD, Callee, ReturnValueSlot(), This, VTT, 1238 VTTTy, nullptr); 1239} 1240 1241void ItaniumCXXABI::emitVTableDefinitions(CodeGenVTables &CGVT, 1242 const CXXRecordDecl *RD) { 1243 llvm::GlobalVariable *VTable = getAddrOfVTable(RD, CharUnits()); 1244 if (VTable->hasInitializer()) 1245 return; 1246 1247 ItaniumVTableContext &VTContext = CGM.getItaniumVTableContext(); 1248 const VTableLayout &VTLayout = VTContext.getVTableLayout(RD); 1249 llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD); 1250 llvm::Constant *RTTI = 1251 CGM.GetAddrOfRTTIDescriptor(CGM.getContext().getTagDeclType(RD)); 1252 1253 // Create and set the initializer. 1254 llvm::Constant *Init = CGVT.CreateVTableInitializer( 1255 RD, VTLayout.vtable_component_begin(), VTLayout.getNumVTableComponents(), 1256 VTLayout.vtable_thunk_begin(), VTLayout.getNumVTableThunks(), RTTI); 1257 VTable->setInitializer(Init); 1258 1259 // Set the correct linkage. 1260 VTable->setLinkage(Linkage); 1261 1262 // Set the right visibility. 1263 CGM.setGlobalVisibility(VTable, RD); 1264 1265 // Use pointer alignment for the vtable. Otherwise we would align them based 1266 // on the size of the initializer which doesn't make sense as only single 1267 // values are read. 1268 unsigned PAlign = CGM.getTarget().getPointerAlign(0); 1269 VTable->setAlignment(getContext().toCharUnitsFromBits(PAlign).getQuantity()); 1270 1271 // If this is the magic class __cxxabiv1::__fundamental_type_info, 1272 // we will emit the typeinfo for the fundamental types. This is the 1273 // same behaviour as GCC. 1274 const DeclContext *DC = RD->getDeclContext(); 1275 if (RD->getIdentifier() && 1276 RD->getIdentifier()->isStr("__fundamental_type_info") && 1277 isa<NamespaceDecl>(DC) && cast<NamespaceDecl>(DC)->getIdentifier() && 1278 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") && 1279 DC->getParent()->isTranslationUnit()) 1280 EmitFundamentalRTTIDescriptors(); 1281} 1282 1283llvm::Value *ItaniumCXXABI::getVTableAddressPointInStructor( 1284 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base, 1285 const CXXRecordDecl *NearestVBase, bool &NeedsVirtualOffset) { 1286 bool NeedsVTTParam = CGM.getCXXABI().NeedsVTTParameter(CGF.CurGD); 1287 NeedsVirtualOffset = (NeedsVTTParam && NearestVBase); 1288 1289 llvm::Value *VTableAddressPoint; 1290 if (NeedsVTTParam && (Base.getBase()->getNumVBases() || NearestVBase)) { 1291 // Get the secondary vpointer index. 1292 uint64_t VirtualPointerIndex = 1293 CGM.getVTables().getSecondaryVirtualPointerIndex(VTableClass, Base); 1294 1295 /// Load the VTT. 1296 llvm::Value *VTT = CGF.LoadCXXVTT(); 1297 if (VirtualPointerIndex) 1298 VTT = CGF.Builder.CreateConstInBoundsGEP1_64(VTT, VirtualPointerIndex); 1299 1300 // And load the address point from the VTT. 1301 VTableAddressPoint = CGF.Builder.CreateLoad(VTT); 1302 } else { 1303 llvm::Constant *VTable = 1304 CGM.getCXXABI().getAddrOfVTable(VTableClass, CharUnits()); 1305 uint64_t AddressPoint = CGM.getItaniumVTableContext() 1306 .getVTableLayout(VTableClass) 1307 .getAddressPoint(Base); 1308 VTableAddressPoint = 1309 CGF.Builder.CreateConstInBoundsGEP2_64(VTable, 0, AddressPoint); 1310 } 1311 1312 return VTableAddressPoint; 1313} 1314 1315llvm::Constant *ItaniumCXXABI::getVTableAddressPointForConstExpr( 1316 BaseSubobject Base, const CXXRecordDecl *VTableClass) { 1317 llvm::Constant *VTable = getAddrOfVTable(VTableClass, CharUnits()); 1318 1319 // Find the appropriate vtable within the vtable group. 1320 uint64_t AddressPoint = CGM.getItaniumVTableContext() 1321 .getVTableLayout(VTableClass) 1322 .getAddressPoint(Base); 1323 llvm::Value *Indices[] = { 1324 llvm::ConstantInt::get(CGM.Int64Ty, 0), 1325 llvm::ConstantInt::get(CGM.Int64Ty, AddressPoint) 1326 }; 1327 1328 return llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, Indices); 1329} 1330 1331llvm::GlobalVariable *ItaniumCXXABI::getAddrOfVTable(const CXXRecordDecl *RD, 1332 CharUnits VPtrOffset) { 1333 assert(VPtrOffset.isZero() && "Itanium ABI only supports zero vptr offsets"); 1334 1335 llvm::GlobalVariable *&VTable = VTables[RD]; 1336 if (VTable) 1337 return VTable; 1338 1339 // Queue up this v-table for possible deferred emission. 1340 CGM.addDeferredVTable(RD); 1341 1342 SmallString<256> OutName; 1343 llvm::raw_svector_ostream Out(OutName); 1344 getMangleContext().mangleCXXVTable(RD, Out); 1345 Out.flush(); 1346 StringRef Name = OutName.str(); 1347 1348 ItaniumVTableContext &VTContext = CGM.getItaniumVTableContext(); 1349 llvm::ArrayType *ArrayType = llvm::ArrayType::get( 1350 CGM.Int8PtrTy, VTContext.getVTableLayout(RD).getNumVTableComponents()); 1351 1352 VTable = CGM.CreateOrReplaceCXXRuntimeVariable( 1353 Name, ArrayType, llvm::GlobalValue::ExternalLinkage); 1354 VTable->setUnnamedAddr(true); 1355 1356 if (RD->hasAttr<DLLImportAttr>()) 1357 VTable->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); 1358 else if (RD->hasAttr<DLLExportAttr>()) 1359 VTable->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); 1360 1361 return VTable; 1362} 1363 1364llvm::Value *ItaniumCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF, 1365 GlobalDecl GD, 1366 llvm::Value *This, 1367 llvm::Type *Ty) { 1368 GD = GD.getCanonicalDecl(); 1369 Ty = Ty->getPointerTo()->getPointerTo(); 1370 llvm::Value *VTable = CGF.GetVTablePtr(This, Ty); 1371 1372 uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD); 1373 llvm::Value *VFuncPtr = 1374 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn"); 1375 return CGF.Builder.CreateLoad(VFuncPtr); 1376} 1377 1378llvm::Value *ItaniumCXXABI::EmitVirtualDestructorCall( 1379 CodeGenFunction &CGF, const CXXDestructorDecl *Dtor, CXXDtorType DtorType, 1380 llvm::Value *This, const CXXMemberCallExpr *CE) { 1381 assert(CE == nullptr || CE->arg_begin() == CE->arg_end()); 1382 assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete); 1383 1384 const CGFunctionInfo *FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration( 1385 Dtor, getFromDtorType(DtorType)); 1386 llvm::Type *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo); 1387 llvm::Value *Callee = 1388 getVirtualFunctionPointer(CGF, GlobalDecl(Dtor, DtorType), This, Ty); 1389 1390 CGF.EmitCXXMemberOrOperatorCall(Dtor, Callee, ReturnValueSlot(), This, 1391 /*ImplicitParam=*/nullptr, QualType(), CE); 1392 return nullptr; 1393} 1394 1395void ItaniumCXXABI::emitVirtualInheritanceTables(const CXXRecordDecl *RD) { 1396 CodeGenVTables &VTables = CGM.getVTables(); 1397 llvm::GlobalVariable *VTT = VTables.GetAddrOfVTT(RD); 1398 VTables.EmitVTTDefinition(VTT, CGM.getVTableLinkage(RD), RD); 1399} 1400 1401static llvm::Value *performTypeAdjustment(CodeGenFunction &CGF, 1402 llvm::Value *Ptr, 1403 int64_t NonVirtualAdjustment, 1404 int64_t VirtualAdjustment, 1405 bool IsReturnAdjustment) { 1406 if (!NonVirtualAdjustment && !VirtualAdjustment) 1407 return Ptr; 1408 1409 llvm::Type *Int8PtrTy = CGF.Int8PtrTy; 1410 llvm::Value *V = CGF.Builder.CreateBitCast(Ptr, Int8PtrTy); 1411 1412 if (NonVirtualAdjustment && !IsReturnAdjustment) { 1413 // Perform the non-virtual adjustment for a base-to-derived cast. 1414 V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment); 1415 } 1416 1417 if (VirtualAdjustment) { 1418 llvm::Type *PtrDiffTy = 1419 CGF.ConvertType(CGF.getContext().getPointerDiffType()); 1420 1421 // Perform the virtual adjustment. 1422 llvm::Value *VTablePtrPtr = 1423 CGF.Builder.CreateBitCast(V, Int8PtrTy->getPointerTo()); 1424 1425 llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr); 1426 1427 llvm::Value *OffsetPtr = 1428 CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment); 1429 1430 OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo()); 1431 1432 // Load the adjustment offset from the vtable. 1433 llvm::Value *Offset = CGF.Builder.CreateLoad(OffsetPtr); 1434 1435 // Adjust our pointer. 1436 V = CGF.Builder.CreateInBoundsGEP(V, Offset); 1437 } 1438 1439 if (NonVirtualAdjustment && IsReturnAdjustment) { 1440 // Perform the non-virtual adjustment for a derived-to-base cast. 1441 V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment); 1442 } 1443 1444 // Cast back to the original type. 1445 return CGF.Builder.CreateBitCast(V, Ptr->getType()); 1446} 1447 1448llvm::Value *ItaniumCXXABI::performThisAdjustment(CodeGenFunction &CGF, 1449 llvm::Value *This, 1450 const ThisAdjustment &TA) { 1451 return performTypeAdjustment(CGF, This, TA.NonVirtual, 1452 TA.Virtual.Itanium.VCallOffsetOffset, 1453 /*IsReturnAdjustment=*/false); 1454} 1455 1456llvm::Value * 1457ItaniumCXXABI::performReturnAdjustment(CodeGenFunction &CGF, llvm::Value *Ret, 1458 const ReturnAdjustment &RA) { 1459 return performTypeAdjustment(CGF, Ret, RA.NonVirtual, 1460 RA.Virtual.Itanium.VBaseOffsetOffset, 1461 /*IsReturnAdjustment=*/true); 1462} 1463 1464void ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF, 1465 RValue RV, QualType ResultType) { 1466 if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl())) 1467 return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType); 1468 1469 // Destructor thunks in the ARM ABI have indeterminate results. 1470 llvm::Type *T = 1471 cast<llvm::PointerType>(CGF.ReturnValue->getType())->getElementType(); 1472 RValue Undef = RValue::get(llvm::UndefValue::get(T)); 1473 return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType); 1474} 1475 1476/************************** Array allocation cookies **************************/ 1477 1478CharUnits ItaniumCXXABI::getArrayCookieSizeImpl(QualType elementType) { 1479 // The array cookie is a size_t; pad that up to the element alignment. 1480 // The cookie is actually right-justified in that space. 1481 return std::max(CharUnits::fromQuantity(CGM.SizeSizeInBytes), 1482 CGM.getContext().getTypeAlignInChars(elementType)); 1483} 1484 1485llvm::Value *ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, 1486 llvm::Value *NewPtr, 1487 llvm::Value *NumElements, 1488 const CXXNewExpr *expr, 1489 QualType ElementType) { 1490 assert(requiresArrayCookie(expr)); 1491 1492 unsigned AS = NewPtr->getType()->getPointerAddressSpace(); 1493 1494 ASTContext &Ctx = getContext(); 1495 QualType SizeTy = Ctx.getSizeType(); 1496 CharUnits SizeSize = Ctx.getTypeSizeInChars(SizeTy); 1497 1498 // The size of the cookie. 1499 CharUnits CookieSize = 1500 std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType)); 1501 assert(CookieSize == getArrayCookieSizeImpl(ElementType)); 1502 1503 // Compute an offset to the cookie. 1504 llvm::Value *CookiePtr = NewPtr; 1505 CharUnits CookieOffset = CookieSize - SizeSize; 1506 if (!CookieOffset.isZero()) 1507 CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_64(CookiePtr, 1508 CookieOffset.getQuantity()); 1509 1510 // Write the number of elements into the appropriate slot. 1511 llvm::Type *NumElementsTy = CGF.ConvertType(SizeTy)->getPointerTo(AS); 1512 llvm::Value *NumElementsPtr = 1513 CGF.Builder.CreateBitCast(CookiePtr, NumElementsTy); 1514 llvm::Instruction *SI = CGF.Builder.CreateStore(NumElements, NumElementsPtr); 1515 if (CGM.getLangOpts().Sanitize.has(SanitizerKind::Address) && AS == 0 && 1516 expr->getOperatorNew()->isReplaceableGlobalAllocationFunction()) { 1517 // The store to the CookiePtr does not need to be instrumented. 1518 CGM.getSanitizerMetadata()->disableSanitizerForInstruction(SI); 1519 llvm::FunctionType *FTy = 1520 llvm::FunctionType::get(CGM.VoidTy, NumElementsTy, false); 1521 llvm::Constant *F = 1522 CGM.CreateRuntimeFunction(FTy, "__asan_poison_cxx_array_cookie"); 1523 CGF.Builder.CreateCall(F, NumElementsPtr); 1524 } 1525 1526 // Finally, compute a pointer to the actual data buffer by skipping 1527 // over the cookie completely. 1528 return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr, 1529 CookieSize.getQuantity()); 1530} 1531 1532llvm::Value *ItaniumCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, 1533 llvm::Value *allocPtr, 1534 CharUnits cookieSize) { 1535 // The element size is right-justified in the cookie. 1536 llvm::Value *numElementsPtr = allocPtr; 1537 CharUnits numElementsOffset = 1538 cookieSize - CharUnits::fromQuantity(CGF.SizeSizeInBytes); 1539 if (!numElementsOffset.isZero()) 1540 numElementsPtr = 1541 CGF.Builder.CreateConstInBoundsGEP1_64(numElementsPtr, 1542 numElementsOffset.getQuantity()); 1543 1544 unsigned AS = allocPtr->getType()->getPointerAddressSpace(); 1545 numElementsPtr = 1546 CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS)); 1547 if (!CGM.getLangOpts().Sanitize.has(SanitizerKind::Address) || AS != 0) 1548 return CGF.Builder.CreateLoad(numElementsPtr); 1549 // In asan mode emit a function call instead of a regular load and let the 1550 // run-time deal with it: if the shadow is properly poisoned return the 1551 // cookie, otherwise return 0 to avoid an infinite loop calling DTORs. 1552 // We can't simply ignore this load using nosanitize metadata because 1553 // the metadata may be lost. 1554 llvm::FunctionType *FTy = 1555 llvm::FunctionType::get(CGF.SizeTy, CGF.SizeTy->getPointerTo(0), false); 1556 llvm::Constant *F = 1557 CGM.CreateRuntimeFunction(FTy, "__asan_load_cxx_array_cookie"); 1558 return CGF.Builder.CreateCall(F, numElementsPtr); 1559} 1560 1561CharUnits ARMCXXABI::getArrayCookieSizeImpl(QualType elementType) { 1562 // ARM says that the cookie is always: 1563 // struct array_cookie { 1564 // std::size_t element_size; // element_size != 0 1565 // std::size_t element_count; 1566 // }; 1567 // But the base ABI doesn't give anything an alignment greater than 1568 // 8, so we can dismiss this as typical ABI-author blindness to 1569 // actual language complexity and round up to the element alignment. 1570 return std::max(CharUnits::fromQuantity(2 * CGM.SizeSizeInBytes), 1571 CGM.getContext().getTypeAlignInChars(elementType)); 1572} 1573 1574llvm::Value *ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, 1575 llvm::Value *newPtr, 1576 llvm::Value *numElements, 1577 const CXXNewExpr *expr, 1578 QualType elementType) { 1579 assert(requiresArrayCookie(expr)); 1580 1581 // NewPtr is a char*, but we generalize to arbitrary addrspaces. 1582 unsigned AS = newPtr->getType()->getPointerAddressSpace(); 1583 1584 // The cookie is always at the start of the buffer. 1585 llvm::Value *cookie = newPtr; 1586 1587 // The first element is the element size. 1588 cookie = CGF.Builder.CreateBitCast(cookie, CGF.SizeTy->getPointerTo(AS)); 1589 llvm::Value *elementSize = llvm::ConstantInt::get(CGF.SizeTy, 1590 getContext().getTypeSizeInChars(elementType).getQuantity()); 1591 CGF.Builder.CreateStore(elementSize, cookie); 1592 1593 // The second element is the element count. 1594 cookie = CGF.Builder.CreateConstInBoundsGEP1_32(cookie, 1); 1595 CGF.Builder.CreateStore(numElements, cookie); 1596 1597 // Finally, compute a pointer to the actual data buffer by skipping 1598 // over the cookie completely. 1599 CharUnits cookieSize = ARMCXXABI::getArrayCookieSizeImpl(elementType); 1600 return CGF.Builder.CreateConstInBoundsGEP1_64(newPtr, 1601 cookieSize.getQuantity()); 1602} 1603 1604llvm::Value *ARMCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, 1605 llvm::Value *allocPtr, 1606 CharUnits cookieSize) { 1607 // The number of elements is at offset sizeof(size_t) relative to 1608 // the allocated pointer. 1609 llvm::Value *numElementsPtr 1610 = CGF.Builder.CreateConstInBoundsGEP1_64(allocPtr, CGF.SizeSizeInBytes); 1611 1612 unsigned AS = allocPtr->getType()->getPointerAddressSpace(); 1613 numElementsPtr = 1614 CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS)); 1615 return CGF.Builder.CreateLoad(numElementsPtr); 1616} 1617 1618/*********************** Static local initialization **************************/ 1619 1620static llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM, 1621 llvm::PointerType *GuardPtrTy) { 1622 // int __cxa_guard_acquire(__guard *guard_object); 1623 llvm::FunctionType *FTy = 1624 llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy), 1625 GuardPtrTy, /*isVarArg=*/false); 1626 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire", 1627 llvm::AttributeSet::get(CGM.getLLVMContext(), 1628 llvm::AttributeSet::FunctionIndex, 1629 llvm::Attribute::NoUnwind)); 1630} 1631 1632static llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM, 1633 llvm::PointerType *GuardPtrTy) { 1634 // void __cxa_guard_release(__guard *guard_object); 1635 llvm::FunctionType *FTy = 1636 llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false); 1637 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release", 1638 llvm::AttributeSet::get(CGM.getLLVMContext(), 1639 llvm::AttributeSet::FunctionIndex, 1640 llvm::Attribute::NoUnwind)); 1641} 1642 1643static llvm::Constant *getGuardAbortFn(CodeGenModule &CGM, 1644 llvm::PointerType *GuardPtrTy) { 1645 // void __cxa_guard_abort(__guard *guard_object); 1646 llvm::FunctionType *FTy = 1647 llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false); 1648 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort", 1649 llvm::AttributeSet::get(CGM.getLLVMContext(), 1650 llvm::AttributeSet::FunctionIndex, 1651 llvm::Attribute::NoUnwind)); 1652} 1653 1654namespace { 1655 struct CallGuardAbort : EHScopeStack::Cleanup { 1656 llvm::GlobalVariable *Guard; 1657 CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {} 1658 1659 void Emit(CodeGenFunction &CGF, Flags flags) override { 1660 CGF.EmitNounwindRuntimeCall(getGuardAbortFn(CGF.CGM, Guard->getType()), 1661 Guard); 1662 } 1663 }; 1664} 1665 1666/// The ARM code here follows the Itanium code closely enough that we 1667/// just special-case it at particular places. 1668void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF, 1669 const VarDecl &D, 1670 llvm::GlobalVariable *var, 1671 bool shouldPerformInit) { 1672 CGBuilderTy &Builder = CGF.Builder; 1673 1674 // We only need to use thread-safe statics for local non-TLS variables; 1675 // global initialization is always single-threaded. 1676 bool threadsafe = getContext().getLangOpts().ThreadsafeStatics && 1677 D.isLocalVarDecl() && !D.getTLSKind(); 1678 1679 // If we have a global variable with internal linkage and thread-safe statics 1680 // are disabled, we can just let the guard variable be of type i8. 1681 bool useInt8GuardVariable = !threadsafe && var->hasInternalLinkage(); 1682 1683 llvm::IntegerType *guardTy; 1684 if (useInt8GuardVariable) { 1685 guardTy = CGF.Int8Ty; 1686 } else { 1687 // Guard variables are 64 bits in the generic ABI and size width on ARM 1688 // (i.e. 32-bit on AArch32, 64-bit on AArch64). 1689 guardTy = (UseARMGuardVarABI ? CGF.SizeTy : CGF.Int64Ty); 1690 } 1691 llvm::PointerType *guardPtrTy = guardTy->getPointerTo(); 1692 1693 // Create the guard variable if we don't already have it (as we 1694 // might if we're double-emitting this function body). 1695 llvm::GlobalVariable *guard = CGM.getStaticLocalDeclGuardAddress(&D); 1696 if (!guard) { 1697 // Mangle the name for the guard. 1698 SmallString<256> guardName; 1699 { 1700 llvm::raw_svector_ostream out(guardName); 1701 getMangleContext().mangleStaticGuardVariable(&D, out); 1702 out.flush(); 1703 } 1704 1705 // Create the guard variable with a zero-initializer. 1706 // Just absorb linkage and visibility from the guarded variable. 1707 guard = new llvm::GlobalVariable(CGM.getModule(), guardTy, 1708 false, var->getLinkage(), 1709 llvm::ConstantInt::get(guardTy, 0), 1710 guardName.str()); 1711 guard->setVisibility(var->getVisibility()); 1712 // If the variable is thread-local, so is its guard variable. 1713 guard->setThreadLocalMode(var->getThreadLocalMode()); 1714 1715 // The ABI says: It is suggested that it be emitted in the same COMDAT group 1716 // as the associated data object 1717 if (!D.isLocalVarDecl() && var->isWeakForLinker() && CGM.supportsCOMDAT()) { 1718 llvm::Comdat *C = CGM.getModule().getOrInsertComdat(var->getName()); 1719 guard->setComdat(C); 1720 var->setComdat(C); 1721 CGF.CurFn->setComdat(C); 1722 } 1723 1724 CGM.setStaticLocalDeclGuardAddress(&D, guard); 1725 } 1726 1727 // Test whether the variable has completed initialization. 1728 // 1729 // Itanium C++ ABI 3.3.2: 1730 // The following is pseudo-code showing how these functions can be used: 1731 // if (obj_guard.first_byte == 0) { 1732 // if ( __cxa_guard_acquire (&obj_guard) ) { 1733 // try { 1734 // ... initialize the object ...; 1735 // } catch (...) { 1736 // __cxa_guard_abort (&obj_guard); 1737 // throw; 1738 // } 1739 // ... queue object destructor with __cxa_atexit() ...; 1740 // __cxa_guard_release (&obj_guard); 1741 // } 1742 // } 1743 1744 // Load the first byte of the guard variable. 1745 llvm::LoadInst *LI = 1746 Builder.CreateLoad(Builder.CreateBitCast(guard, CGM.Int8PtrTy)); 1747 LI->setAlignment(1); 1748 1749 // Itanium ABI: 1750 // An implementation supporting thread-safety on multiprocessor 1751 // systems must also guarantee that references to the initialized 1752 // object do not occur before the load of the initialization flag. 1753 // 1754 // In LLVM, we do this by marking the load Acquire. 1755 if (threadsafe) 1756 LI->setAtomic(llvm::Acquire); 1757 1758 // For ARM, we should only check the first bit, rather than the entire byte: 1759 // 1760 // ARM C++ ABI 3.2.3.1: 1761 // To support the potential use of initialization guard variables 1762 // as semaphores that are the target of ARM SWP and LDREX/STREX 1763 // synchronizing instructions we define a static initialization 1764 // guard variable to be a 4-byte aligned, 4-byte word with the 1765 // following inline access protocol. 1766 // #define INITIALIZED 1 1767 // if ((obj_guard & INITIALIZED) != INITIALIZED) { 1768 // if (__cxa_guard_acquire(&obj_guard)) 1769 // ... 1770 // } 1771 // 1772 // and similarly for ARM64: 1773 // 1774 // ARM64 C++ ABI 3.2.2: 1775 // This ABI instead only specifies the value bit 0 of the static guard 1776 // variable; all other bits are platform defined. Bit 0 shall be 0 when the 1777 // variable is not initialized and 1 when it is. 1778 llvm::Value *V = 1779 (UseARMGuardVarABI && !useInt8GuardVariable) 1780 ? Builder.CreateAnd(LI, llvm::ConstantInt::get(CGM.Int8Ty, 1)) 1781 : LI; 1782 llvm::Value *isInitialized = Builder.CreateIsNull(V, "guard.uninitialized"); 1783 1784 llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check"); 1785 llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end"); 1786 1787 // Check if the first byte of the guard variable is zero. 1788 Builder.CreateCondBr(isInitialized, InitCheckBlock, EndBlock); 1789 1790 CGF.EmitBlock(InitCheckBlock); 1791 1792 // Variables used when coping with thread-safe statics and exceptions. 1793 if (threadsafe) { 1794 // Call __cxa_guard_acquire. 1795 llvm::Value *V 1796 = CGF.EmitNounwindRuntimeCall(getGuardAcquireFn(CGM, guardPtrTy), guard); 1797 1798 llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init"); 1799 1800 Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"), 1801 InitBlock, EndBlock); 1802 1803 // Call __cxa_guard_abort along the exceptional edge. 1804 CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, guard); 1805 1806 CGF.EmitBlock(InitBlock); 1807 } 1808 1809 // Emit the initializer and add a global destructor if appropriate. 1810 CGF.EmitCXXGlobalVarDeclInit(D, var, shouldPerformInit); 1811 1812 if (threadsafe) { 1813 // Pop the guard-abort cleanup if we pushed one. 1814 CGF.PopCleanupBlock(); 1815 1816 // Call __cxa_guard_release. This cannot throw. 1817 CGF.EmitNounwindRuntimeCall(getGuardReleaseFn(CGM, guardPtrTy), guard); 1818 } else { 1819 Builder.CreateStore(llvm::ConstantInt::get(guardTy, 1), guard); 1820 } 1821 1822 CGF.EmitBlock(EndBlock); 1823} 1824 1825/// Register a global destructor using __cxa_atexit. 1826static void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF, 1827 llvm::Constant *dtor, 1828 llvm::Constant *addr, 1829 bool TLS) { 1830 const char *Name = "__cxa_atexit"; 1831 if (TLS) { 1832 const llvm::Triple &T = CGF.getTarget().getTriple(); 1833 Name = T.isMacOSX() ? "_tlv_atexit" : "__cxa_thread_atexit"; 1834 } 1835 1836 // We're assuming that the destructor function is something we can 1837 // reasonably call with the default CC. Go ahead and cast it to the 1838 // right prototype. 1839 llvm::Type *dtorTy = 1840 llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, false)->getPointerTo(); 1841 1842 // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d); 1843 llvm::Type *paramTys[] = { dtorTy, CGF.Int8PtrTy, CGF.Int8PtrTy }; 1844 llvm::FunctionType *atexitTy = 1845 llvm::FunctionType::get(CGF.IntTy, paramTys, false); 1846 1847 // Fetch the actual function. 1848 llvm::Constant *atexit = CGF.CGM.CreateRuntimeFunction(atexitTy, Name); 1849 if (llvm::Function *fn = dyn_cast<llvm::Function>(atexit)) 1850 fn->setDoesNotThrow(); 1851 1852 // Create a variable that binds the atexit to this shared object. 1853 llvm::Constant *handle = 1854 CGF.CGM.CreateRuntimeVariable(CGF.Int8Ty, "__dso_handle"); 1855 1856 llvm::Value *args[] = { 1857 llvm::ConstantExpr::getBitCast(dtor, dtorTy), 1858 llvm::ConstantExpr::getBitCast(addr, CGF.Int8PtrTy), 1859 handle 1860 }; 1861 CGF.EmitNounwindRuntimeCall(atexit, args); 1862} 1863 1864/// Register a global destructor as best as we know how. 1865void ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF, 1866 const VarDecl &D, 1867 llvm::Constant *dtor, 1868 llvm::Constant *addr) { 1869 // Use __cxa_atexit if available. 1870 if (CGM.getCodeGenOpts().CXAAtExit) 1871 return emitGlobalDtorWithCXAAtExit(CGF, dtor, addr, D.getTLSKind()); 1872 1873 if (D.getTLSKind()) 1874 CGM.ErrorUnsupported(&D, "non-trivial TLS destruction"); 1875 1876 // In Apple kexts, we want to add a global destructor entry. 1877 // FIXME: shouldn't this be guarded by some variable? 1878 if (CGM.getLangOpts().AppleKext) { 1879 // Generate a global destructor entry. 1880 return CGM.AddCXXDtorEntry(dtor, addr); 1881 } 1882 1883 CGF.registerGlobalDtorWithAtExit(D, dtor, addr); 1884} 1885 1886static bool isThreadWrapperReplaceable(const VarDecl *VD, 1887 CodeGen::CodeGenModule &CGM) { 1888 assert(!VD->isStaticLocal() && "static local VarDecls don't need wrappers!"); 1889 // OS X prefers to have references to thread local variables to go through 1890 // the thread wrapper instead of directly referencing the backing variable. 1891 return VD->getTLSKind() == VarDecl::TLS_Dynamic && 1892 CGM.getTarget().getTriple().isMacOSX(); 1893} 1894 1895/// Get the appropriate linkage for the wrapper function. This is essentially 1896/// the weak form of the variable's linkage; every translation unit which needs 1897/// the wrapper emits a copy, and we want the linker to merge them. 1898static llvm::GlobalValue::LinkageTypes 1899getThreadLocalWrapperLinkage(const VarDecl *VD, CodeGen::CodeGenModule &CGM) { 1900 llvm::GlobalValue::LinkageTypes VarLinkage = 1901 CGM.getLLVMLinkageVarDefinition(VD, /*isConstant=*/false); 1902 1903 // For internal linkage variables, we don't need an external or weak wrapper. 1904 if (llvm::GlobalValue::isLocalLinkage(VarLinkage)) 1905 return VarLinkage; 1906 1907 // If the thread wrapper is replaceable, give it appropriate linkage. 1908 if (isThreadWrapperReplaceable(VD, CGM)) { 1909 if (llvm::GlobalVariable::isLinkOnceLinkage(VarLinkage) || 1910 llvm::GlobalVariable::isWeakODRLinkage(VarLinkage)) 1911 return llvm::GlobalVariable::WeakAnyLinkage; 1912 return VarLinkage; 1913 } 1914 return llvm::GlobalValue::WeakODRLinkage; 1915} 1916 1917llvm::Function * 1918ItaniumCXXABI::getOrCreateThreadLocalWrapper(const VarDecl *VD, 1919 llvm::Value *Val) { 1920 // Mangle the name for the thread_local wrapper function. 1921 SmallString<256> WrapperName; 1922 { 1923 llvm::raw_svector_ostream Out(WrapperName); 1924 getMangleContext().mangleItaniumThreadLocalWrapper(VD, Out); 1925 Out.flush(); 1926 } 1927 1928 if (llvm::Value *V = CGM.getModule().getNamedValue(WrapperName)) 1929 return cast<llvm::Function>(V); 1930 1931 llvm::Type *RetTy = Val->getType(); 1932 if (VD->getType()->isReferenceType()) 1933 RetTy = RetTy->getPointerElementType(); 1934 1935 llvm::FunctionType *FnTy = llvm::FunctionType::get(RetTy, false); 1936 llvm::Function *Wrapper = 1937 llvm::Function::Create(FnTy, getThreadLocalWrapperLinkage(VD, CGM), 1938 WrapperName.str(), &CGM.getModule()); 1939 // Always resolve references to the wrapper at link time. 1940 if (!Wrapper->hasLocalLinkage() && !isThreadWrapperReplaceable(VD, CGM)) 1941 Wrapper->setVisibility(llvm::GlobalValue::HiddenVisibility); 1942 return Wrapper; 1943} 1944 1945void ItaniumCXXABI::EmitThreadLocalInitFuncs( 1946 CodeGenModule &CGM, 1947 ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *>> 1948 CXXThreadLocals, ArrayRef<llvm::Function *> CXXThreadLocalInits, 1949 ArrayRef<llvm::GlobalVariable *> CXXThreadLocalInitVars) { 1950 llvm::Function *InitFunc = nullptr; 1951 if (!CXXThreadLocalInits.empty()) { 1952 // Generate a guarded initialization function. 1953 llvm::FunctionType *FTy = 1954 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false); 1955 InitFunc = CGM.CreateGlobalInitOrDestructFunction(FTy, "__tls_init", 1956 SourceLocation(), 1957 /*TLS=*/true); 1958 llvm::GlobalVariable *Guard = new llvm::GlobalVariable( 1959 CGM.getModule(), CGM.Int8Ty, /*isConstant=*/false, 1960 llvm::GlobalVariable::InternalLinkage, 1961 llvm::ConstantInt::get(CGM.Int8Ty, 0), "__tls_guard"); 1962 Guard->setThreadLocal(true); 1963 CodeGenFunction(CGM) 1964 .GenerateCXXGlobalInitFunc(InitFunc, CXXThreadLocalInits, Guard); 1965 } 1966 for (unsigned I = 0, N = CXXThreadLocals.size(); I != N; ++I) { 1967 const VarDecl *VD = CXXThreadLocals[I].first; 1968 llvm::GlobalVariable *Var = CXXThreadLocals[I].second; 1969 1970 // Some targets require that all access to thread local variables go through 1971 // the thread wrapper. This means that we cannot attempt to create a thread 1972 // wrapper or a thread helper. 1973 if (isThreadWrapperReplaceable(VD, CGM) && !VD->hasDefinition()) 1974 continue; 1975 1976 // Mangle the name for the thread_local initialization function. 1977 SmallString<256> InitFnName; 1978 { 1979 llvm::raw_svector_ostream Out(InitFnName); 1980 getMangleContext().mangleItaniumThreadLocalInit(VD, Out); 1981 Out.flush(); 1982 } 1983 1984 // If we have a definition for the variable, emit the initialization 1985 // function as an alias to the global Init function (if any). Otherwise, 1986 // produce a declaration of the initialization function. 1987 llvm::GlobalValue *Init = nullptr; 1988 bool InitIsInitFunc = false; 1989 if (VD->hasDefinition()) { 1990 InitIsInitFunc = true; 1991 if (InitFunc) 1992 Init = llvm::GlobalAlias::create(Var->getLinkage(), InitFnName.str(), 1993 InitFunc); 1994 } else { 1995 // Emit a weak global function referring to the initialization function. 1996 // This function will not exist if the TU defining the thread_local 1997 // variable in question does not need any dynamic initialization for 1998 // its thread_local variables. 1999 llvm::FunctionType *FnTy = llvm::FunctionType::get(CGM.VoidTy, false); 2000 Init = llvm::Function::Create( 2001 FnTy, llvm::GlobalVariable::ExternalWeakLinkage, InitFnName.str(), 2002 &CGM.getModule()); 2003 } 2004 2005 if (Init) 2006 Init->setVisibility(Var->getVisibility()); 2007 2008 llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Var); 2009 llvm::LLVMContext &Context = CGM.getModule().getContext(); 2010 llvm::BasicBlock *Entry = llvm::BasicBlock::Create(Context, "", Wrapper); 2011 CGBuilderTy Builder(Entry); 2012 if (InitIsInitFunc) { 2013 if (Init) 2014 Builder.CreateCall(Init); 2015 } else { 2016 // Don't know whether we have an init function. Call it if it exists. 2017 llvm::Value *Have = Builder.CreateIsNotNull(Init); 2018 llvm::BasicBlock *InitBB = llvm::BasicBlock::Create(Context, "", Wrapper); 2019 llvm::BasicBlock *ExitBB = llvm::BasicBlock::Create(Context, "", Wrapper); 2020 Builder.CreateCondBr(Have, InitBB, ExitBB); 2021 2022 Builder.SetInsertPoint(InitBB); 2023 Builder.CreateCall(Init); 2024 Builder.CreateBr(ExitBB); 2025 2026 Builder.SetInsertPoint(ExitBB); 2027 } 2028 2029 // For a reference, the result of the wrapper function is a pointer to 2030 // the referenced object. 2031 llvm::Value *Val = Var; 2032 if (VD->getType()->isReferenceType()) { 2033 llvm::LoadInst *LI = Builder.CreateLoad(Val); 2034 LI->setAlignment(CGM.getContext().getDeclAlign(VD).getQuantity()); 2035 Val = LI; 2036 } 2037 if (Val->getType() != Wrapper->getReturnType()) 2038 Val = Builder.CreatePointerBitCastOrAddrSpaceCast( 2039 Val, Wrapper->getReturnType(), ""); 2040 Builder.CreateRet(Val); 2041 } 2042} 2043 2044LValue ItaniumCXXABI::EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, 2045 const VarDecl *VD, 2046 QualType LValType) { 2047 QualType T = VD->getType(); 2048 llvm::Type *Ty = CGF.getTypes().ConvertTypeForMem(T); 2049 llvm::Value *Val = CGF.CGM.GetAddrOfGlobalVar(VD, Ty); 2050 llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Val); 2051 2052 Val = CGF.Builder.CreateCall(Wrapper); 2053 2054 LValue LV; 2055 if (VD->getType()->isReferenceType()) 2056 LV = CGF.MakeNaturalAlignAddrLValue(Val, LValType); 2057 else 2058 LV = CGF.MakeAddrLValue(Val, LValType, CGF.getContext().getDeclAlign(VD)); 2059 // FIXME: need setObjCGCLValueClass? 2060 return LV; 2061} 2062 2063/// Return whether the given global decl needs a VTT parameter, which it does 2064/// if it's a base constructor or destructor with virtual bases. 2065bool ItaniumCXXABI::NeedsVTTParameter(GlobalDecl GD) { 2066 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 2067 2068 // We don't have any virtual bases, just return early. 2069 if (!MD->getParent()->getNumVBases()) 2070 return false; 2071 2072 // Check if we have a base constructor. 2073 if (isa<CXXConstructorDecl>(MD) && GD.getCtorType() == Ctor_Base) 2074 return true; 2075 2076 // Check if we have a base destructor. 2077 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base) 2078 return true; 2079 2080 return false; 2081} 2082 2083namespace { 2084class ItaniumRTTIBuilder { 2085 CodeGenModule &CGM; // Per-module state. 2086 llvm::LLVMContext &VMContext; 2087 const ItaniumCXXABI &CXXABI; // Per-module state. 2088 2089 /// Fields - The fields of the RTTI descriptor currently being built. 2090 SmallVector<llvm::Constant *, 16> Fields; 2091 2092 /// GetAddrOfTypeName - Returns the mangled type name of the given type. 2093 llvm::GlobalVariable * 2094 GetAddrOfTypeName(QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage); 2095 2096 /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI 2097 /// descriptor of the given type. 2098 llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty); 2099 2100 /// BuildVTablePointer - Build the vtable pointer for the given type. 2101 void BuildVTablePointer(const Type *Ty); 2102 2103 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single 2104 /// inheritance, according to the Itanium C++ ABI, 2.9.5p6b. 2105 void BuildSIClassTypeInfo(const CXXRecordDecl *RD); 2106 2107 /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for 2108 /// classes with bases that do not satisfy the abi::__si_class_type_info 2109 /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c. 2110 void BuildVMIClassTypeInfo(const CXXRecordDecl *RD); 2111 2112 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used 2113 /// for pointer types. 2114 void BuildPointerTypeInfo(QualType PointeeTy); 2115 2116 /// BuildObjCObjectTypeInfo - Build the appropriate kind of 2117 /// type_info for an object type. 2118 void BuildObjCObjectTypeInfo(const ObjCObjectType *Ty); 2119 2120 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info 2121 /// struct, used for member pointer types. 2122 void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty); 2123 2124public: 2125 ItaniumRTTIBuilder(const ItaniumCXXABI &ABI) 2126 : CGM(ABI.CGM), VMContext(CGM.getModule().getContext()), CXXABI(ABI) {} 2127 2128 // Pointer type info flags. 2129 enum { 2130 /// PTI_Const - Type has const qualifier. 2131 PTI_Const = 0x1, 2132 2133 /// PTI_Volatile - Type has volatile qualifier. 2134 PTI_Volatile = 0x2, 2135 2136 /// PTI_Restrict - Type has restrict qualifier. 2137 PTI_Restrict = 0x4, 2138 2139 /// PTI_Incomplete - Type is incomplete. 2140 PTI_Incomplete = 0x8, 2141 2142 /// PTI_ContainingClassIncomplete - Containing class is incomplete. 2143 /// (in pointer to member). 2144 PTI_ContainingClassIncomplete = 0x10 2145 }; 2146 2147 // VMI type info flags. 2148 enum { 2149 /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance. 2150 VMI_NonDiamondRepeat = 0x1, 2151 2152 /// VMI_DiamondShaped - Class is diamond shaped. 2153 VMI_DiamondShaped = 0x2 2154 }; 2155 2156 // Base class type info flags. 2157 enum { 2158 /// BCTI_Virtual - Base class is virtual. 2159 BCTI_Virtual = 0x1, 2160 2161 /// BCTI_Public - Base class is public. 2162 BCTI_Public = 0x2 2163 }; 2164 2165 /// BuildTypeInfo - Build the RTTI type info struct for the given type. 2166 /// 2167 /// \param Force - true to force the creation of this RTTI value 2168 llvm::Constant *BuildTypeInfo(QualType Ty, bool Force = false); 2169}; 2170} 2171 2172llvm::GlobalVariable *ItaniumRTTIBuilder::GetAddrOfTypeName( 2173 QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage) { 2174 SmallString<256> OutName; 2175 llvm::raw_svector_ostream Out(OutName); 2176 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out); 2177 Out.flush(); 2178 StringRef Name = OutName.str(); 2179 2180 // We know that the mangled name of the type starts at index 4 of the 2181 // mangled name of the typename, so we can just index into it in order to 2182 // get the mangled name of the type. 2183 llvm::Constant *Init = llvm::ConstantDataArray::getString(VMContext, 2184 Name.substr(4)); 2185 2186 llvm::GlobalVariable *GV = 2187 CGM.CreateOrReplaceCXXRuntimeVariable(Name, Init->getType(), Linkage); 2188 2189 GV->setInitializer(Init); 2190 2191 return GV; 2192} 2193 2194llvm::Constant * 2195ItaniumRTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) { 2196 // Mangle the RTTI name. 2197 SmallString<256> OutName; 2198 llvm::raw_svector_ostream Out(OutName); 2199 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out); 2200 Out.flush(); 2201 StringRef Name = OutName.str(); 2202 2203 // Look for an existing global. 2204 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name); 2205 2206 if (!GV) { 2207 // Create a new global variable. 2208 GV = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy, 2209 /*Constant=*/true, 2210 llvm::GlobalValue::ExternalLinkage, nullptr, 2211 Name); 2212 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { 2213 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); 2214 if (RD->hasAttr<DLLImportAttr>()) 2215 GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass); 2216 } 2217 } 2218 2219 return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy); 2220} 2221 2222/// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type 2223/// info for that type is defined in the standard library. 2224static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) { 2225 // Itanium C++ ABI 2.9.2: 2226 // Basic type information (e.g. for "int", "bool", etc.) will be kept in 2227 // the run-time support library. Specifically, the run-time support 2228 // library should contain type_info objects for the types X, X* and 2229 // X const*, for every X in: void, std::nullptr_t, bool, wchar_t, char, 2230 // unsigned char, signed char, short, unsigned short, int, unsigned int, 2231 // long, unsigned long, long long, unsigned long long, float, double, 2232 // long double, char16_t, char32_t, and the IEEE 754r decimal and 2233 // half-precision floating point types. 2234 switch (Ty->getKind()) { 2235 case BuiltinType::Void: 2236 case BuiltinType::NullPtr: 2237 case BuiltinType::Bool: 2238 case BuiltinType::WChar_S: 2239 case BuiltinType::WChar_U: 2240 case BuiltinType::Char_U: 2241 case BuiltinType::Char_S: 2242 case BuiltinType::UChar: 2243 case BuiltinType::SChar: 2244 case BuiltinType::Short: 2245 case BuiltinType::UShort: 2246 case BuiltinType::Int: 2247 case BuiltinType::UInt: 2248 case BuiltinType::Long: 2249 case BuiltinType::ULong: 2250 case BuiltinType::LongLong: 2251 case BuiltinType::ULongLong: 2252 case BuiltinType::Half: 2253 case BuiltinType::Float: 2254 case BuiltinType::Double: 2255 case BuiltinType::LongDouble: 2256 case BuiltinType::Char16: 2257 case BuiltinType::Char32: 2258 case BuiltinType::Int128: 2259 case BuiltinType::UInt128: 2260 case BuiltinType::OCLImage1d: 2261 case BuiltinType::OCLImage1dArray: 2262 case BuiltinType::OCLImage1dBuffer: 2263 case BuiltinType::OCLImage2d: 2264 case BuiltinType::OCLImage2dArray: 2265 case BuiltinType::OCLImage3d: 2266 case BuiltinType::OCLSampler: 2267 case BuiltinType::OCLEvent: 2268 return true; 2269 2270 case BuiltinType::Dependent: 2271#define BUILTIN_TYPE(Id, SingletonId) 2272#define PLACEHOLDER_TYPE(Id, SingletonId) \ 2273 case BuiltinType::Id: 2274#include "clang/AST/BuiltinTypes.def" 2275 llvm_unreachable("asking for RRTI for a placeholder type!"); 2276 2277 case BuiltinType::ObjCId: 2278 case BuiltinType::ObjCClass: 2279 case BuiltinType::ObjCSel: 2280 llvm_unreachable("FIXME: Objective-C types are unsupported!"); 2281 } 2282 2283 llvm_unreachable("Invalid BuiltinType Kind!"); 2284} 2285 2286static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) { 2287 QualType PointeeTy = PointerTy->getPointeeType(); 2288 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy); 2289 if (!BuiltinTy) 2290 return false; 2291 2292 // Check the qualifiers. 2293 Qualifiers Quals = PointeeTy.getQualifiers(); 2294 Quals.removeConst(); 2295 2296 if (!Quals.empty()) 2297 return false; 2298 2299 return TypeInfoIsInStandardLibrary(BuiltinTy); 2300} 2301 2302/// IsStandardLibraryRTTIDescriptor - Returns whether the type 2303/// information for the given type exists in the standard library. 2304static bool IsStandardLibraryRTTIDescriptor(QualType Ty) { 2305 // Type info for builtin types is defined in the standard library. 2306 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty)) 2307 return TypeInfoIsInStandardLibrary(BuiltinTy); 2308 2309 // Type info for some pointer types to builtin types is defined in the 2310 // standard library. 2311 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty)) 2312 return TypeInfoIsInStandardLibrary(PointerTy); 2313 2314 return false; 2315} 2316 2317/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for 2318/// the given type exists somewhere else, and that we should not emit the type 2319/// information in this translation unit. Assumes that it is not a 2320/// standard-library type. 2321static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM, 2322 QualType Ty) { 2323 ASTContext &Context = CGM.getContext(); 2324 2325 // If RTTI is disabled, assume it might be disabled in the 2326 // translation unit that defines any potential key function, too. 2327 if (!Context.getLangOpts().RTTI) return false; 2328 2329 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { 2330 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); 2331 if (!RD->hasDefinition()) 2332 return false; 2333 2334 if (!RD->isDynamicClass()) 2335 return false; 2336 2337 // FIXME: this may need to be reconsidered if the key function 2338 // changes. 2339 if (CGM.getVTables().isVTableExternal(RD)) 2340 return true; 2341 2342 if (RD->hasAttr<DLLImportAttr>()) 2343 return true; 2344 } 2345 2346 return false; 2347} 2348 2349/// IsIncompleteClassType - Returns whether the given record type is incomplete. 2350static bool IsIncompleteClassType(const RecordType *RecordTy) { 2351 return !RecordTy->getDecl()->isCompleteDefinition(); 2352} 2353 2354/// ContainsIncompleteClassType - Returns whether the given type contains an 2355/// incomplete class type. This is true if 2356/// 2357/// * The given type is an incomplete class type. 2358/// * The given type is a pointer type whose pointee type contains an 2359/// incomplete class type. 2360/// * The given type is a member pointer type whose class is an incomplete 2361/// class type. 2362/// * The given type is a member pointer type whoise pointee type contains an 2363/// incomplete class type. 2364/// is an indirect or direct pointer to an incomplete class type. 2365static bool ContainsIncompleteClassType(QualType Ty) { 2366 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { 2367 if (IsIncompleteClassType(RecordTy)) 2368 return true; 2369 } 2370 2371 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty)) 2372 return ContainsIncompleteClassType(PointerTy->getPointeeType()); 2373 2374 if (const MemberPointerType *MemberPointerTy = 2375 dyn_cast<MemberPointerType>(Ty)) { 2376 // Check if the class type is incomplete. 2377 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass()); 2378 if (IsIncompleteClassType(ClassType)) 2379 return true; 2380 2381 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType()); 2382 } 2383 2384 return false; 2385} 2386 2387// CanUseSingleInheritance - Return whether the given record decl has a "single, 2388// public, non-virtual base at offset zero (i.e. the derived class is dynamic 2389// iff the base is)", according to Itanium C++ ABI, 2.95p6b. 2390static bool CanUseSingleInheritance(const CXXRecordDecl *RD) { 2391 // Check the number of bases. 2392 if (RD->getNumBases() != 1) 2393 return false; 2394 2395 // Get the base. 2396 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin(); 2397 2398 // Check that the base is not virtual. 2399 if (Base->isVirtual()) 2400 return false; 2401 2402 // Check that the base is public. 2403 if (Base->getAccessSpecifier() != AS_public) 2404 return false; 2405 2406 // Check that the class is dynamic iff the base is. 2407 const CXXRecordDecl *BaseDecl = 2408 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 2409 if (!BaseDecl->isEmpty() && 2410 BaseDecl->isDynamicClass() != RD->isDynamicClass()) 2411 return false; 2412 2413 return true; 2414} 2415 2416void ItaniumRTTIBuilder::BuildVTablePointer(const Type *Ty) { 2417 // abi::__class_type_info. 2418 static const char * const ClassTypeInfo = 2419 "_ZTVN10__cxxabiv117__class_type_infoE"; 2420 // abi::__si_class_type_info. 2421 static const char * const SIClassTypeInfo = 2422 "_ZTVN10__cxxabiv120__si_class_type_infoE"; 2423 // abi::__vmi_class_type_info. 2424 static const char * const VMIClassTypeInfo = 2425 "_ZTVN10__cxxabiv121__vmi_class_type_infoE"; 2426 2427 const char *VTableName = nullptr; 2428 2429 switch (Ty->getTypeClass()) { 2430#define TYPE(Class, Base) 2431#define ABSTRACT_TYPE(Class, Base) 2432#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: 2433#define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 2434#define DEPENDENT_TYPE(Class, Base) case Type::Class: 2435#include "clang/AST/TypeNodes.def" 2436 llvm_unreachable("Non-canonical and dependent types shouldn't get here"); 2437 2438 case Type::LValueReference: 2439 case Type::RValueReference: 2440 llvm_unreachable("References shouldn't get here"); 2441 2442 case Type::Auto: 2443 llvm_unreachable("Undeduced auto type shouldn't get here"); 2444 2445 case Type::Builtin: 2446 // GCC treats vector and complex types as fundamental types. 2447 case Type::Vector: 2448 case Type::ExtVector: 2449 case Type::Complex: 2450 case Type::Atomic: 2451 // FIXME: GCC treats block pointers as fundamental types?! 2452 case Type::BlockPointer: 2453 // abi::__fundamental_type_info. 2454 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE"; 2455 break; 2456 2457 case Type::ConstantArray: 2458 case Type::IncompleteArray: 2459 case Type::VariableArray: 2460 // abi::__array_type_info. 2461 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE"; 2462 break; 2463 2464 case Type::FunctionNoProto: 2465 case Type::FunctionProto: 2466 // abi::__function_type_info. 2467 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE"; 2468 break; 2469 2470 case Type::Enum: 2471 // abi::__enum_type_info. 2472 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE"; 2473 break; 2474 2475 case Type::Record: { 2476 const CXXRecordDecl *RD = 2477 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl()); 2478 2479 if (!RD->hasDefinition() || !RD->getNumBases()) { 2480 VTableName = ClassTypeInfo; 2481 } else if (CanUseSingleInheritance(RD)) { 2482 VTableName = SIClassTypeInfo; 2483 } else { 2484 VTableName = VMIClassTypeInfo; 2485 } 2486 2487 break; 2488 } 2489 2490 case Type::ObjCObject: 2491 // Ignore protocol qualifiers. 2492 Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr(); 2493 2494 // Handle id and Class. 2495 if (isa<BuiltinType>(Ty)) { 2496 VTableName = ClassTypeInfo; 2497 break; 2498 } 2499 2500 assert(isa<ObjCInterfaceType>(Ty)); 2501 // Fall through. 2502 2503 case Type::ObjCInterface: 2504 if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) { 2505 VTableName = SIClassTypeInfo; 2506 } else { 2507 VTableName = ClassTypeInfo; 2508 } 2509 break; 2510 2511 case Type::ObjCObjectPointer: 2512 case Type::Pointer: 2513 // abi::__pointer_type_info. 2514 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE"; 2515 break; 2516 2517 case Type::MemberPointer: 2518 // abi::__pointer_to_member_type_info. 2519 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE"; 2520 break; 2521 } 2522 2523 llvm::Constant *VTable = 2524 CGM.getModule().getOrInsertGlobal(VTableName, CGM.Int8PtrTy); 2525 2526 llvm::Type *PtrDiffTy = 2527 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType()); 2528 2529 // The vtable address point is 2. 2530 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2); 2531 VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, Two); 2532 VTable = llvm::ConstantExpr::getBitCast(VTable, CGM.Int8PtrTy); 2533 2534 Fields.push_back(VTable); 2535} 2536 2537/// \brief Return the linkage that the type info and type info name constants 2538/// should have for the given type. 2539static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(CodeGenModule &CGM, 2540 QualType Ty) { 2541 // Itanium C++ ABI 2.9.5p7: 2542 // In addition, it and all of the intermediate abi::__pointer_type_info 2543 // structs in the chain down to the abi::__class_type_info for the 2544 // incomplete class type must be prevented from resolving to the 2545 // corresponding type_info structs for the complete class type, possibly 2546 // by making them local static objects. Finally, a dummy class RTTI is 2547 // generated for the incomplete type that will not resolve to the final 2548 // complete class RTTI (because the latter need not exist), possibly by 2549 // making it a local static object. 2550 if (ContainsIncompleteClassType(Ty)) 2551 return llvm::GlobalValue::InternalLinkage; 2552 2553 switch (Ty->getLinkage()) { 2554 case NoLinkage: 2555 case InternalLinkage: 2556 case UniqueExternalLinkage: 2557 return llvm::GlobalValue::InternalLinkage; 2558 2559 case VisibleNoLinkage: 2560 case ExternalLinkage: 2561 if (!CGM.getLangOpts().RTTI) { 2562 // RTTI is not enabled, which means that this type info struct is going 2563 // to be used for exception handling. Give it linkonce_odr linkage. 2564 return llvm::GlobalValue::LinkOnceODRLinkage; 2565 } 2566 2567 if (const RecordType *Record = dyn_cast<RecordType>(Ty)) { 2568 const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); 2569 if (RD->hasAttr<WeakAttr>()) 2570 return llvm::GlobalValue::WeakODRLinkage; 2571 if (RD->isDynamicClass()) 2572 return CGM.getVTableLinkage(RD); 2573 } 2574 2575 return llvm::GlobalValue::LinkOnceODRLinkage; 2576 } 2577 2578 llvm_unreachable("Invalid linkage!"); 2579} 2580 2581llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) { 2582 // We want to operate on the canonical type. 2583 Ty = CGM.getContext().getCanonicalType(Ty); 2584 2585 // Check if we've already emitted an RTTI descriptor for this type. 2586 SmallString<256> OutName; 2587 llvm::raw_svector_ostream Out(OutName); 2588 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out); 2589 Out.flush(); 2590 StringRef Name = OutName.str(); 2591 2592 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name); 2593 if (OldGV && !OldGV->isDeclaration()) { 2594 assert(!OldGV->hasAvailableExternallyLinkage() && 2595 "available_externally typeinfos not yet implemented"); 2596 2597 return llvm::ConstantExpr::getBitCast(OldGV, CGM.Int8PtrTy); 2598 } 2599 2600 // Check if there is already an external RTTI descriptor for this type. 2601 bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty); 2602 if (!Force && (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM, Ty))) 2603 return GetAddrOfExternalRTTIDescriptor(Ty); 2604 2605 // Emit the standard library with external linkage. 2606 llvm::GlobalVariable::LinkageTypes Linkage; 2607 if (IsStdLib) 2608 Linkage = llvm::GlobalValue::ExternalLinkage; 2609 else 2610 Linkage = getTypeInfoLinkage(CGM, Ty); 2611 2612 // Add the vtable pointer. 2613 BuildVTablePointer(cast<Type>(Ty)); 2614 2615 // And the name. 2616 llvm::GlobalVariable *TypeName = GetAddrOfTypeName(Ty, Linkage); 2617 llvm::Constant *TypeNameField; 2618 2619 // If we're supposed to demote the visibility, be sure to set a flag 2620 // to use a string comparison for type_info comparisons. 2621 ItaniumCXXABI::RTTIUniquenessKind RTTIUniqueness = 2622 CXXABI.classifyRTTIUniqueness(Ty, Linkage); 2623 if (RTTIUniqueness != ItaniumCXXABI::RUK_Unique) { 2624 // The flag is the sign bit, which on ARM64 is defined to be clear 2625 // for global pointers. This is very ARM64-specific. 2626 TypeNameField = llvm::ConstantExpr::getPtrToInt(TypeName, CGM.Int64Ty); 2627 llvm::Constant *flag = 2628 llvm::ConstantInt::get(CGM.Int64Ty, ((uint64_t)1) << 63); 2629 TypeNameField = llvm::ConstantExpr::getAdd(TypeNameField, flag); 2630 TypeNameField = 2631 llvm::ConstantExpr::getIntToPtr(TypeNameField, CGM.Int8PtrTy); 2632 } else { 2633 TypeNameField = llvm::ConstantExpr::getBitCast(TypeName, CGM.Int8PtrTy); 2634 } 2635 Fields.push_back(TypeNameField); 2636 2637 switch (Ty->getTypeClass()) { 2638#define TYPE(Class, Base) 2639#define ABSTRACT_TYPE(Class, Base) 2640#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: 2641#define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 2642#define DEPENDENT_TYPE(Class, Base) case Type::Class: 2643#include "clang/AST/TypeNodes.def" 2644 llvm_unreachable("Non-canonical and dependent types shouldn't get here"); 2645 2646 // GCC treats vector types as fundamental types. 2647 case Type::Builtin: 2648 case Type::Vector: 2649 case Type::ExtVector: 2650 case Type::Complex: 2651 case Type::BlockPointer: 2652 // Itanium C++ ABI 2.9.5p4: 2653 // abi::__fundamental_type_info adds no data members to std::type_info. 2654 break; 2655 2656 case Type::LValueReference: 2657 case Type::RValueReference: 2658 llvm_unreachable("References shouldn't get here"); 2659 2660 case Type::Auto: 2661 llvm_unreachable("Undeduced auto type shouldn't get here"); 2662 2663 case Type::ConstantArray: 2664 case Type::IncompleteArray: 2665 case Type::VariableArray: 2666 // Itanium C++ ABI 2.9.5p5: 2667 // abi::__array_type_info adds no data members to std::type_info. 2668 break; 2669 2670 case Type::FunctionNoProto: 2671 case Type::FunctionProto: 2672 // Itanium C++ ABI 2.9.5p5: 2673 // abi::__function_type_info adds no data members to std::type_info. 2674 break; 2675 2676 case Type::Enum: 2677 // Itanium C++ ABI 2.9.5p5: 2678 // abi::__enum_type_info adds no data members to std::type_info. 2679 break; 2680 2681 case Type::Record: { 2682 const CXXRecordDecl *RD = 2683 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl()); 2684 if (!RD->hasDefinition() || !RD->getNumBases()) { 2685 // We don't need to emit any fields. 2686 break; 2687 } 2688 2689 if (CanUseSingleInheritance(RD)) 2690 BuildSIClassTypeInfo(RD); 2691 else 2692 BuildVMIClassTypeInfo(RD); 2693 2694 break; 2695 } 2696 2697 case Type::ObjCObject: 2698 case Type::ObjCInterface: 2699 BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty)); 2700 break; 2701 2702 case Type::ObjCObjectPointer: 2703 BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType()); 2704 break; 2705 2706 case Type::Pointer: 2707 BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType()); 2708 break; 2709 2710 case Type::MemberPointer: 2711 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty)); 2712 break; 2713 2714 case Type::Atomic: 2715 // No fields, at least for the moment. 2716 break; 2717 } 2718 2719 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Fields); 2720 2721 llvm::GlobalVariable *GV = 2722 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), 2723 /*Constant=*/true, Linkage, Init, Name); 2724 2725 // If there's already an old global variable, replace it with the new one. 2726 if (OldGV) { 2727 GV->takeName(OldGV); 2728 llvm::Constant *NewPtr = 2729 llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); 2730 OldGV->replaceAllUsesWith(NewPtr); 2731 OldGV->eraseFromParent(); 2732 } 2733 2734 // The Itanium ABI specifies that type_info objects must be globally 2735 // unique, with one exception: if the type is an incomplete class 2736 // type or a (possibly indirect) pointer to one. That exception 2737 // affects the general case of comparing type_info objects produced 2738 // by the typeid operator, which is why the comparison operators on 2739 // std::type_info generally use the type_info name pointers instead 2740 // of the object addresses. However, the language's built-in uses 2741 // of RTTI generally require class types to be complete, even when 2742 // manipulating pointers to those class types. This allows the 2743 // implementation of dynamic_cast to rely on address equality tests, 2744 // which is much faster. 2745 2746 // All of this is to say that it's important that both the type_info 2747 // object and the type_info name be uniqued when weakly emitted. 2748 2749 // Give the type_info object and name the formal visibility of the 2750 // type itself. 2751 llvm::GlobalValue::VisibilityTypes llvmVisibility; 2752 if (llvm::GlobalValue::isLocalLinkage(Linkage)) 2753 // If the linkage is local, only default visibility makes sense. 2754 llvmVisibility = llvm::GlobalValue::DefaultVisibility; 2755 else if (RTTIUniqueness == ItaniumCXXABI::RUK_NonUniqueHidden) 2756 llvmVisibility = llvm::GlobalValue::HiddenVisibility; 2757 else 2758 llvmVisibility = CodeGenModule::GetLLVMVisibility(Ty->getVisibility()); 2759 TypeName->setVisibility(llvmVisibility); 2760 GV->setVisibility(llvmVisibility); 2761 2762 return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy); 2763} 2764 2765/// ComputeQualifierFlags - Compute the pointer type info flags from the 2766/// given qualifier. 2767static unsigned ComputeQualifierFlags(Qualifiers Quals) { 2768 unsigned Flags = 0; 2769 2770 if (Quals.hasConst()) 2771 Flags |= ItaniumRTTIBuilder::PTI_Const; 2772 if (Quals.hasVolatile()) 2773 Flags |= ItaniumRTTIBuilder::PTI_Volatile; 2774 if (Quals.hasRestrict()) 2775 Flags |= ItaniumRTTIBuilder::PTI_Restrict; 2776 2777 return Flags; 2778} 2779 2780/// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info 2781/// for the given Objective-C object type. 2782void ItaniumRTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) { 2783 // Drop qualifiers. 2784 const Type *T = OT->getBaseType().getTypePtr(); 2785 assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T)); 2786 2787 // The builtin types are abi::__class_type_infos and don't require 2788 // extra fields. 2789 if (isa<BuiltinType>(T)) return; 2790 2791 ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl(); 2792 ObjCInterfaceDecl *Super = Class->getSuperClass(); 2793 2794 // Root classes are also __class_type_info. 2795 if (!Super) return; 2796 2797 QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super); 2798 2799 // Everything else is single inheritance. 2800 llvm::Constant *BaseTypeInfo = 2801 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(SuperTy); 2802 Fields.push_back(BaseTypeInfo); 2803} 2804 2805/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single 2806/// inheritance, according to the Itanium C++ ABI, 2.95p6b. 2807void ItaniumRTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) { 2808 // Itanium C++ ABI 2.9.5p6b: 2809 // It adds to abi::__class_type_info a single member pointing to the 2810 // type_info structure for the base type, 2811 llvm::Constant *BaseTypeInfo = 2812 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(RD->bases_begin()->getType()); 2813 Fields.push_back(BaseTypeInfo); 2814} 2815 2816namespace { 2817 /// SeenBases - Contains virtual and non-virtual bases seen when traversing 2818 /// a class hierarchy. 2819 struct SeenBases { 2820 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases; 2821 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases; 2822 }; 2823} 2824 2825/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in 2826/// abi::__vmi_class_type_info. 2827/// 2828static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base, 2829 SeenBases &Bases) { 2830 2831 unsigned Flags = 0; 2832 2833 const CXXRecordDecl *BaseDecl = 2834 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 2835 2836 if (Base->isVirtual()) { 2837 // Mark the virtual base as seen. 2838 if (!Bases.VirtualBases.insert(BaseDecl).second) { 2839 // If this virtual base has been seen before, then the class is diamond 2840 // shaped. 2841 Flags |= ItaniumRTTIBuilder::VMI_DiamondShaped; 2842 } else { 2843 if (Bases.NonVirtualBases.count(BaseDecl)) 2844 Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat; 2845 } 2846 } else { 2847 // Mark the non-virtual base as seen. 2848 if (!Bases.NonVirtualBases.insert(BaseDecl).second) { 2849 // If this non-virtual base has been seen before, then the class has non- 2850 // diamond shaped repeated inheritance. 2851 Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat; 2852 } else { 2853 if (Bases.VirtualBases.count(BaseDecl)) 2854 Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat; 2855 } 2856 } 2857 2858 // Walk all bases. 2859 for (const auto &I : BaseDecl->bases()) 2860 Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases); 2861 2862 return Flags; 2863} 2864 2865static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) { 2866 unsigned Flags = 0; 2867 SeenBases Bases; 2868 2869 // Walk all bases. 2870 for (const auto &I : RD->bases()) 2871 Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases); 2872 2873 return Flags; 2874} 2875 2876/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for 2877/// classes with bases that do not satisfy the abi::__si_class_type_info 2878/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c. 2879void ItaniumRTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) { 2880 llvm::Type *UnsignedIntLTy = 2881 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy); 2882 2883 // Itanium C++ ABI 2.9.5p6c: 2884 // __flags is a word with flags describing details about the class 2885 // structure, which may be referenced by using the __flags_masks 2886 // enumeration. These flags refer to both direct and indirect bases. 2887 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD); 2888 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags)); 2889 2890 // Itanium C++ ABI 2.9.5p6c: 2891 // __base_count is a word with the number of direct proper base class 2892 // descriptions that follow. 2893 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases())); 2894 2895 if (!RD->getNumBases()) 2896 return; 2897 2898 llvm::Type *LongLTy = 2899 CGM.getTypes().ConvertType(CGM.getContext().LongTy); 2900 2901 // Now add the base class descriptions. 2902 2903 // Itanium C++ ABI 2.9.5p6c: 2904 // __base_info[] is an array of base class descriptions -- one for every 2905 // direct proper base. Each description is of the type: 2906 // 2907 // struct abi::__base_class_type_info { 2908 // public: 2909 // const __class_type_info *__base_type; 2910 // long __offset_flags; 2911 // 2912 // enum __offset_flags_masks { 2913 // __virtual_mask = 0x1, 2914 // __public_mask = 0x2, 2915 // __offset_shift = 8 2916 // }; 2917 // }; 2918 for (const auto &Base : RD->bases()) { 2919 // The __base_type member points to the RTTI for the base type. 2920 Fields.push_back(ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(Base.getType())); 2921 2922 const CXXRecordDecl *BaseDecl = 2923 cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); 2924 2925 int64_t OffsetFlags = 0; 2926 2927 // All but the lower 8 bits of __offset_flags are a signed offset. 2928 // For a non-virtual base, this is the offset in the object of the base 2929 // subobject. For a virtual base, this is the offset in the virtual table of 2930 // the virtual base offset for the virtual base referenced (negative). 2931 CharUnits Offset; 2932 if (Base.isVirtual()) 2933 Offset = 2934 CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(RD, BaseDecl); 2935 else { 2936 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); 2937 Offset = Layout.getBaseClassOffset(BaseDecl); 2938 }; 2939 2940 OffsetFlags = uint64_t(Offset.getQuantity()) << 8; 2941 2942 // The low-order byte of __offset_flags contains flags, as given by the 2943 // masks from the enumeration __offset_flags_masks. 2944 if (Base.isVirtual()) 2945 OffsetFlags |= BCTI_Virtual; 2946 if (Base.getAccessSpecifier() == AS_public) 2947 OffsetFlags |= BCTI_Public; 2948 2949 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags)); 2950 } 2951} 2952 2953/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, 2954/// used for pointer types. 2955void ItaniumRTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) { 2956 Qualifiers Quals; 2957 QualType UnqualifiedPointeeTy = 2958 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals); 2959 2960 // Itanium C++ ABI 2.9.5p7: 2961 // __flags is a flag word describing the cv-qualification and other 2962 // attributes of the type pointed to 2963 unsigned Flags = ComputeQualifierFlags(Quals); 2964 2965 // Itanium C++ ABI 2.9.5p7: 2966 // When the abi::__pbase_type_info is for a direct or indirect pointer to an 2967 // incomplete class type, the incomplete target type flag is set. 2968 if (ContainsIncompleteClassType(UnqualifiedPointeeTy)) 2969 Flags |= PTI_Incomplete; 2970 2971 llvm::Type *UnsignedIntLTy = 2972 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy); 2973 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags)); 2974 2975 // Itanium C++ ABI 2.9.5p7: 2976 // __pointee is a pointer to the std::type_info derivation for the 2977 // unqualified type being pointed to. 2978 llvm::Constant *PointeeTypeInfo = 2979 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(UnqualifiedPointeeTy); 2980 Fields.push_back(PointeeTypeInfo); 2981} 2982 2983/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info 2984/// struct, used for member pointer types. 2985void 2986ItaniumRTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) { 2987 QualType PointeeTy = Ty->getPointeeType(); 2988 2989 Qualifiers Quals; 2990 QualType UnqualifiedPointeeTy = 2991 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals); 2992 2993 // Itanium C++ ABI 2.9.5p7: 2994 // __flags is a flag word describing the cv-qualification and other 2995 // attributes of the type pointed to. 2996 unsigned Flags = ComputeQualifierFlags(Quals); 2997 2998 const RecordType *ClassType = cast<RecordType>(Ty->getClass()); 2999 3000 // Itanium C++ ABI 2.9.5p7: 3001 // When the abi::__pbase_type_info is for a direct or indirect pointer to an 3002 // incomplete class type, the incomplete target type flag is set. 3003 if (ContainsIncompleteClassType(UnqualifiedPointeeTy)) 3004 Flags |= PTI_Incomplete; 3005 3006 if (IsIncompleteClassType(ClassType)) 3007 Flags |= PTI_ContainingClassIncomplete; 3008 3009 llvm::Type *UnsignedIntLTy = 3010 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy); 3011 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags)); 3012 3013 // Itanium C++ ABI 2.9.5p7: 3014 // __pointee is a pointer to the std::type_info derivation for the 3015 // unqualified type being pointed to. 3016 llvm::Constant *PointeeTypeInfo = 3017 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(UnqualifiedPointeeTy); 3018 Fields.push_back(PointeeTypeInfo); 3019 3020 // Itanium C++ ABI 2.9.5p9: 3021 // __context is a pointer to an abi::__class_type_info corresponding to the 3022 // class type containing the member pointed to 3023 // (e.g., the "A" in "int A::*"). 3024 Fields.push_back( 3025 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(QualType(ClassType, 0))); 3026} 3027 3028llvm::Constant *ItaniumCXXABI::getAddrOfRTTIDescriptor(QualType Ty) { 3029 return ItaniumRTTIBuilder(*this).BuildTypeInfo(Ty); 3030} 3031 3032void ItaniumCXXABI::EmitFundamentalRTTIDescriptor(QualType Type) { 3033 QualType PointerType = getContext().getPointerType(Type); 3034 QualType PointerTypeConst = getContext().getPointerType(Type.withConst()); 3035 ItaniumRTTIBuilder(*this).BuildTypeInfo(Type, true); 3036 ItaniumRTTIBuilder(*this).BuildTypeInfo(PointerType, true); 3037 ItaniumRTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true); 3038} 3039 3040void ItaniumCXXABI::EmitFundamentalRTTIDescriptors() { 3041 QualType FundamentalTypes[] = { 3042 getContext().VoidTy, getContext().NullPtrTy, 3043 getContext().BoolTy, getContext().WCharTy, 3044 getContext().CharTy, getContext().UnsignedCharTy, 3045 getContext().SignedCharTy, getContext().ShortTy, 3046 getContext().UnsignedShortTy, getContext().IntTy, 3047 getContext().UnsignedIntTy, getContext().LongTy, 3048 getContext().UnsignedLongTy, getContext().LongLongTy, 3049 getContext().UnsignedLongLongTy, getContext().HalfTy, 3050 getContext().FloatTy, getContext().DoubleTy, 3051 getContext().LongDoubleTy, getContext().Char16Ty, 3052 getContext().Char32Ty, 3053 }; 3054 for (const QualType &FundamentalType : FundamentalTypes) 3055 EmitFundamentalRTTIDescriptor(FundamentalType); 3056} 3057 3058/// What sort of uniqueness rules should we use for the RTTI for the 3059/// given type? 3060ItaniumCXXABI::RTTIUniquenessKind ItaniumCXXABI::classifyRTTIUniqueness( 3061 QualType CanTy, llvm::GlobalValue::LinkageTypes Linkage) const { 3062 if (shouldRTTIBeUnique()) 3063 return RUK_Unique; 3064 3065 // It's only necessary for linkonce_odr or weak_odr linkage. 3066 if (Linkage != llvm::GlobalValue::LinkOnceODRLinkage && 3067 Linkage != llvm::GlobalValue::WeakODRLinkage) 3068 return RUK_Unique; 3069 3070 // It's only necessary with default visibility. 3071 if (CanTy->getVisibility() != DefaultVisibility) 3072 return RUK_Unique; 3073 3074 // If we're not required to publish this symbol, hide it. 3075 if (Linkage == llvm::GlobalValue::LinkOnceODRLinkage) 3076 return RUK_NonUniqueHidden; 3077 3078 // If we're required to publish this symbol, as we might be under an 3079 // explicit instantiation, leave it with default visibility but 3080 // enable string-comparisons. 3081 assert(Linkage == llvm::GlobalValue::WeakODRLinkage); 3082 return RUK_NonUniqueVisible; 3083} 3084 3085// Find out how to codegen the complete destructor and constructor 3086namespace { 3087enum class StructorCodegen { Emit, RAUW, Alias, COMDAT }; 3088} 3089static StructorCodegen getCodegenToUse(CodeGenModule &CGM, 3090 const CXXMethodDecl *MD) { 3091 if (!CGM.getCodeGenOpts().CXXCtorDtorAliases) 3092 return StructorCodegen::Emit; 3093 3094 // The complete and base structors are not equivalent if there are any virtual 3095 // bases, so emit separate functions. 3096 if (MD->getParent()->getNumVBases()) 3097 return StructorCodegen::Emit; 3098 3099 GlobalDecl AliasDecl; 3100 if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD)) { 3101 AliasDecl = GlobalDecl(DD, Dtor_Complete); 3102 } else { 3103 const auto *CD = cast<CXXConstructorDecl>(MD); 3104 AliasDecl = GlobalDecl(CD, Ctor_Complete); 3105 } 3106 llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl); 3107 3108 if (llvm::GlobalValue::isDiscardableIfUnused(Linkage)) 3109 return StructorCodegen::RAUW; 3110 3111 // FIXME: Should we allow available_externally aliases? 3112 if (!llvm::GlobalAlias::isValidLinkage(Linkage)) 3113 return StructorCodegen::RAUW; 3114 3115 if (llvm::GlobalValue::isWeakForLinker(Linkage)) { 3116 // Only ELF supports COMDATs with arbitrary names (C5/D5). 3117 if (CGM.getTarget().getTriple().isOSBinFormatELF()) 3118 return StructorCodegen::COMDAT; 3119 return StructorCodegen::Emit; 3120 } 3121 3122 return StructorCodegen::Alias; 3123} 3124 3125static void emitConstructorDestructorAlias(CodeGenModule &CGM, 3126 GlobalDecl AliasDecl, 3127 GlobalDecl TargetDecl) { 3128 llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl); 3129 3130 StringRef MangledName = CGM.getMangledName(AliasDecl); 3131 llvm::GlobalValue *Entry = CGM.GetGlobalValue(MangledName); 3132 if (Entry && !Entry->isDeclaration()) 3133 return; 3134 3135 auto *Aliasee = cast<llvm::GlobalValue>(CGM.GetAddrOfGlobal(TargetDecl)); 3136 llvm::PointerType *AliasType = Aliasee->getType(); 3137 3138 // Create the alias with no name. 3139 auto *Alias = llvm::GlobalAlias::create( 3140 AliasType->getElementType(), 0, Linkage, "", Aliasee, &CGM.getModule()); 3141 3142 // Switch any previous uses to the alias. 3143 if (Entry) { 3144 assert(Entry->getType() == AliasType && 3145 "declaration exists with different type"); 3146 Alias->takeName(Entry); 3147 Entry->replaceAllUsesWith(Alias); 3148 Entry->eraseFromParent(); 3149 } else { 3150 Alias->setName(MangledName); 3151 } 3152 3153 // Finally, set up the alias with its proper name and attributes. 3154 CGM.setAliasAttributes(cast<NamedDecl>(AliasDecl.getDecl()), Alias); 3155} 3156 3157void ItaniumCXXABI::emitCXXStructor(const CXXMethodDecl *MD, 3158 StructorType Type) { 3159 auto *CD = dyn_cast<CXXConstructorDecl>(MD); 3160 const CXXDestructorDecl *DD = CD ? nullptr : cast<CXXDestructorDecl>(MD); 3161 3162 StructorCodegen CGType = getCodegenToUse(CGM, MD); 3163 3164 if (Type == StructorType::Complete) { 3165 GlobalDecl CompleteDecl; 3166 GlobalDecl BaseDecl; 3167 if (CD) { 3168 CompleteDecl = GlobalDecl(CD, Ctor_Complete); 3169 BaseDecl = GlobalDecl(CD, Ctor_Base); 3170 } else { 3171 CompleteDecl = GlobalDecl(DD, Dtor_Complete); 3172 BaseDecl = GlobalDecl(DD, Dtor_Base); 3173 } 3174 3175 if (CGType == StructorCodegen::Alias || CGType == StructorCodegen::COMDAT) { 3176 emitConstructorDestructorAlias(CGM, CompleteDecl, BaseDecl); 3177 return; 3178 } 3179 3180 if (CGType == StructorCodegen::RAUW) { 3181 StringRef MangledName = CGM.getMangledName(CompleteDecl); 3182 auto *Aliasee = cast<llvm::GlobalValue>(CGM.GetAddrOfGlobal(BaseDecl)); 3183 CGM.addReplacement(MangledName, Aliasee); 3184 return; 3185 } 3186 } 3187 3188 // The base destructor is equivalent to the base destructor of its 3189 // base class if there is exactly one non-virtual base class with a 3190 // non-trivial destructor, there are no fields with a non-trivial 3191 // destructor, and the body of the destructor is trivial. 3192 if (DD && Type == StructorType::Base && CGType != StructorCodegen::COMDAT && 3193 !CGM.TryEmitBaseDestructorAsAlias(DD)) 3194 return; 3195 3196 llvm::Function *Fn = CGM.codegenCXXStructor(MD, Type); 3197 3198 if (CGType == StructorCodegen::COMDAT) { 3199 SmallString<256> Buffer; 3200 llvm::raw_svector_ostream Out(Buffer); 3201 if (DD) 3202 getMangleContext().mangleCXXDtorComdat(DD, Out); 3203 else 3204 getMangleContext().mangleCXXCtorComdat(CD, Out); 3205 llvm::Comdat *C = CGM.getModule().getOrInsertComdat(Out.str()); 3206 Fn->setComdat(C); 3207 } 3208}
|