From 36fd6beef1ffaf93217d8ce96d900d4ed817e463 Mon Sep 17 00:00:00 2001 From: Anders Carlsson Date: Tue, 20 Apr 2010 16:22:16 +0000 Subject: [PATCH] Fix a bug which triggered the assertion I added yesterday. Basically, when we initialize the vtable pointer for a virtual base, and there was another path from the most derived class to another base with the same class type, we would use the wrong base. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101911 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGClass.cpp | 41 +++++++++++++------ lib/CodeGen/CodeGenFunction.h | 2 + .../vtable-pointer-initialization.cpp | 20 +++++++++ 3 files changed, 50 insertions(+), 13 deletions(-) diff --git a/lib/CodeGen/CGClass.cpp b/lib/CodeGen/CGClass.cpp index a21af413fd..b2991ade43 100644 --- a/lib/CodeGen/CGClass.cpp +++ b/lib/CodeGen/CGClass.cpp @@ -1549,6 +1549,7 @@ CodeGenFunction::GetVirtualBaseClassOffset(llvm::Value *This, void CodeGenFunction::InitializeVTablePointer(BaseSubobject Base, const CXXRecordDecl *NearestVBase, + uint64_t BaseOffsetFromNearestVBase, llvm::Constant *VTable, const CXXRecordDecl *VTableClass) { const CXXRecordDecl *RD = Base.getBase(); @@ -1576,22 +1577,32 @@ CodeGenFunction::InitializeVTablePointer(BaseSubobject Base, Builder.CreateConstInBoundsGEP2_64(VTable, 0, AddressPoint); } + llvm::Value *VTableField = LoadCXXThis(); + // Compute where to store the address point. - llvm::Value *VTableField; - - if (CodeGenVTables::needsVTTParameter(CurGD) && NearestVBase) { - // We need to use the virtual base offset offset because the virtual base - // might have a different offset in the most derived class. - VTableField = GetAddressOfBaseClass(LoadCXXThis(), VTableClass, RD, - /*NullCheckValue=*/false); - } else { - const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); + uint64_t NonVirtualOffset = BaseOffsetFromNearestVBase; - VTableField = Builder.CreateBitCast(LoadCXXThis(), Int8PtrTy); - VTableField = - Builder.CreateConstInBoundsGEP1_64(VTableField, Base.getBaseOffset() / 8); + llvm::Value *VirtualOffset = 0; + if (NearestVBase) { + if (!CodeGenVTables::needsVTTParameter(CurGD)) { + // Just get the vbase offset in the complete class. + const ASTRecordLayout &Layout = + getContext().getASTRecordLayout(VTableClass); + + NonVirtualOffset += Layout.getVBaseClassOffset(NearestVBase); + } else { + // We need to use the virtual base offset offset because the virtual base + // might have a different offset in the most derived class. + VirtualOffset = GetVirtualBaseClassOffset(VTableField, + VTableClass, NearestVBase); + } } + if (NonVirtualOffset || VirtualOffset) + VTableField = + ApplyNonVirtualAndVirtualOffset(*this, VTableField, + NonVirtualOffset, VirtualOffset); + // Finally, store the address point. const llvm::Type *AddressPointPtrTy = VTableAddressPoint->getType()->getPointerTo(); @@ -1602,6 +1613,7 @@ CodeGenFunction::InitializeVTablePointer(BaseSubobject Base, void CodeGenFunction::InitializeVTablePointers(BaseSubobject Base, const CXXRecordDecl *NearestVBase, + uint64_t BaseOffsetFromNearestVBase, bool BaseIsNonVirtualPrimaryBase, llvm::Constant *VTable, const CXXRecordDecl *VTableClass, @@ -1610,7 +1622,8 @@ CodeGenFunction::InitializeVTablePointers(BaseSubobject Base, // been set. if (!BaseIsNonVirtualPrimaryBase) { // Initialize the vtable pointer for this base. - InitializeVTablePointer(Base, NearestVBase, VTable, VTableClass); + InitializeVTablePointer(Base, NearestVBase, BaseOffsetFromNearestVBase, + VTable, VTableClass); } const CXXRecordDecl *RD = Base.getBase(); @@ -1647,6 +1660,7 @@ CodeGenFunction::InitializeVTablePointers(BaseSubobject Base, InitializeVTablePointers(BaseSubobject(BaseDecl, BaseOffset), I->isVirtual() ? BaseDecl : NearestVBase, + I->isVirtual() ? 0 : BaseOffsetFromNearestVBase, BaseDeclIsNonVirtualPrimaryBase, VTable, VTableClass, VBases); } @@ -1663,6 +1677,7 @@ void CodeGenFunction::InitializeVTablePointers(const CXXRecordDecl *RD) { // Initialize the vtable pointers for this class and all of its bases. VisitedVirtualBasesSetTy VBases; InitializeVTablePointers(BaseSubobject(RD, 0), /*NearestVBase=*/0, + /*BaseOffsetFromNearestVBase=*/0, /*BaseIsNonVirtualPrimaryBase=*/false, VTable, RD, VBases); } diff --git a/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h index c43e0006e6..12e37e42d7 100644 --- a/lib/CodeGen/CodeGenFunction.h +++ b/lib/CodeGen/CodeGenFunction.h @@ -536,12 +536,14 @@ public: /// void InitializeVTablePointer(BaseSubobject Base, const CXXRecordDecl *NearestVBase, + uint64_t BaseOffsetFromNearestVBase, llvm::Constant *VTable, const CXXRecordDecl *VTableClass); typedef llvm::SmallPtrSet VisitedVirtualBasesSetTy; void InitializeVTablePointers(BaseSubobject Base, const CXXRecordDecl *NearestVBase, + uint64_t BaseOffsetFromNearestVBase, bool BaseIsNonVirtualPrimaryBase, llvm::Constant *VTable, const CXXRecordDecl *VTableClass, diff --git a/test/CodeGenCXX/vtable-pointer-initialization.cpp b/test/CodeGenCXX/vtable-pointer-initialization.cpp index 75620ab8e6..ca8fb9be88 100644 --- a/test/CodeGenCXX/vtable-pointer-initialization.cpp +++ b/test/CodeGenCXX/vtable-pointer-initialization.cpp @@ -55,3 +55,23 @@ void f() { B b; } // CHECK: store i8** getelementptr inbounds ([3 x i8*]* @_ZTV1B, i64 0, i64 2) // CHECK: call void @_ZN5FieldC1Ev // CHECK: ret void + +namespace Test1 { + +// Test that we don't assert when initializing the vtable pointers in C. +struct A { + virtual void a(); + int i; +}; + +struct B : virtual A { + virtual void b(); +}; + +struct C : A, virtual B { + virtual void c(); + C(); +}; + +C::C() { } +} -- 2.40.0