Improve linkage of RTTI data structures. Introduce CodeGenModule::GetAddrOfRTTI which figures out the right linkage of the RTTI information for the given type and whether it should be defined or not. I will migrate clients over to GetAddrOfRTTI in subsequent commits (with tests).

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@91098 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Anders Carlsson 2009-12-11 02:46:30 +00:00
Родитель 03981014e4
Коммит 31b7f52d8c
7 изменённых файлов: 113 добавлений и 62 удалений

Просмотреть файл

@ -352,18 +352,10 @@ void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) {
llvm::Value * CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) {
QualType Ty = E->getType();
const llvm::Type *LTy = ConvertType(Ty)->getPointerTo();
if (E->isTypeOperand()) {
Ty = E->getTypeOperand();
CanQualType CanTy = CGM.getContext().getCanonicalType(Ty);
Ty = CanTy.getUnqualifiedType().getNonReferenceType();
if (const RecordType *RT = Ty->getAs<RecordType>()) {
const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
if (RD->isPolymorphic())
return Builder.CreateBitCast(CGM.GenerateRTTIRef(RD), LTy);
return Builder.CreateBitCast(CGM.GenerateRTTI(RD), LTy);
}
return Builder.CreateBitCast(CGM.GenerateRTTI(Ty), LTy);
}
if (E->isTypeOperand())
return Builder.CreateBitCast(CGM.GetAddrOfRTTI(E->getTypeOperand()), LTy);
Expr *subE = E->getExprOperand();
Ty = subE->getType();
CanQualType CanTy = CGM.getContext().getCanonicalType(Ty);

Просмотреть файл

@ -68,27 +68,37 @@ public:
return llvm::ConstantExpr::getBitCast(C, Int8PtrTy);
}
// FIXME: This should be removed, and clients should pass in the linkage
// directly instead.
static inline llvm::GlobalVariable::LinkageTypes
GetLinkageFromExternFlag(bool Extern) {
if (Extern)
return llvm::GlobalValue::WeakODRLinkage;
return llvm::GlobalValue::InternalLinkage;
}
// FIXME: This should be removed, and clients should pass in the linkage
// directly instead.
llvm::Constant *BuildName(QualType Ty, bool Hidden, bool Extern) {
return BuildName(Ty, Hidden, GetLinkageFromExternFlag(Extern));
}
llvm::Constant *BuildName(QualType Ty, bool Hidden,
llvm::GlobalVariable::LinkageTypes Linkage) {
llvm::SmallString<256> OutName;
CGM.getMangleContext().mangleCXXRTTIName(Ty, OutName);
llvm::StringRef Name = OutName.str();
llvm::GlobalVariable::LinkageTypes linktype;
linktype = llvm::GlobalValue::LinkOnceODRLinkage;
if (!Extern)
linktype = llvm::GlobalValue::InternalLinkage;
llvm::GlobalVariable *OGV = CGM.getModule().getGlobalVariable(Name);
if (OGV && !OGV->isDeclaration())
return llvm::ConstantExpr::getBitCast(OGV, Int8PtrTy);
llvm::GlobalVariable *GV;
GV = CGM.getModule().getGlobalVariable(Name);
if (GV && !GV->isDeclaration())
return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
llvm::Constant *C = llvm::ConstantArray::get(VMContext, Name.substr(4));
llvm::Constant *C;
C = llvm::ConstantArray::get(VMContext, Name.substr(4));
llvm::GlobalVariable *OGV = GV;
GV = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, linktype,
C, Name);
llvm::GlobalVariable *GV =
new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, Linkage,
C, Name);
if (OGV) {
GV->takeName(OGV);
llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV,
@ -115,9 +125,6 @@ public:
llvm::Constant *BuildTypeRef(QualType Ty) {
llvm::Constant *C;
if (!CGM.getContext().getLangOptions().RTTI)
return llvm::Constant::getNullValue(Int8PtrTy);
llvm::SmallString<256> OutName;
CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
llvm::StringRef Name = OutName.str();
@ -181,17 +188,14 @@ public:
llvm::Constant *finish(std::vector<llvm::Constant *> &info,
llvm::GlobalVariable *GV,
llvm::StringRef Name, bool Hidden, bool Extern) {
llvm::GlobalVariable::LinkageTypes linktype;
linktype = llvm::GlobalValue::LinkOnceODRLinkage;
if (!Extern)
linktype = llvm::GlobalValue::InternalLinkage;
llvm::Constant *C;
C = llvm::ConstantStruct::get(VMContext, &info[0], info.size(), false);
llvm::StringRef Name, bool Hidden,
llvm::GlobalVariable::LinkageTypes Linkage) {
llvm::Constant *C =
llvm::ConstantStruct::get(VMContext, &info[0], info.size(),
/*Packed=*/false);
llvm::GlobalVariable *OGV = GV;
GV = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, linktype,
GV = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, Linkage,
C, Name);
if (OGV) {
GV->takeName(OGV);
@ -206,10 +210,9 @@ public:
}
llvm::Constant *Buildclass_type_info(const CXXRecordDecl *RD) {
if (!CGM.getContext().getLangOptions().RTTI)
return llvm::Constant::getNullValue(Int8PtrTy);
llvm::Constant *
Buildclass_type_info(const CXXRecordDecl *RD,
llvm::GlobalVariable::LinkageTypes Linkage) {
llvm::Constant *C;
llvm::SmallString<256> OutName;
@ -224,8 +227,11 @@ public:
std::vector<llvm::Constant *> info;
// If we're in an anonymous namespace, then we always want internal linkage.
if (RD->isInAnonymousNamespace())
Linkage = llvm::GlobalVariable::InternalLinkage;
bool Hidden = CGM.getDeclVisibilityMode(RD) == LangOptions::Hidden;
bool Extern = !RD->isInAnonymousNamespace();
bool simple = false;
if (RD->getNumBases() == 0)
@ -237,7 +243,7 @@ public:
C = BuildVtableRef("_ZTVN10__cxxabiv121__vmi_class_type_infoE");
info.push_back(C);
info.push_back(BuildName(CGM.getContext().getTagDeclType(RD), Hidden,
Extern));
Linkage));
// If we have no bases, there are no more fields.
if (RD->getNumBases()) {
@ -273,7 +279,7 @@ public:
}
}
return finish(info, GV, Name, Hidden, Extern);
return finish(info, GV, Name, Hidden, Linkage);
}
/// - BuildFlags - Build a __flags value for __pbase_type_info.
@ -366,7 +372,8 @@ public:
info.push_back(BuildType(QualType(PtrMemTy->getClass(), 0)));
// We always generate these as hidden, only the name isn't hidden.
return finish(info, GV, Name, true, Extern);
return finish(info, GV, Name, /*Hidden=*/true,
GetLinkageFromExternFlag(Extern));
}
llvm::Constant *BuildSimpleType(QualType Ty, const char *vtbl) {
@ -391,16 +398,18 @@ public:
info.push_back(BuildName(Ty, Hidden, Extern));
// We always generate these as hidden, only the name isn't hidden.
return finish(info, GV, Name, true, Extern);
return finish(info, GV, Name, /*Hidden=*/true,
GetLinkageFromExternFlag(Extern));
}
/// BuildType - Builds the type info for the given type.
llvm::Constant *BuildType(QualType Ty) {
const clang::Type &Type
= *CGM.getContext().getCanonicalType(Ty).getTypePtr();
if (const RecordType *RT = Ty.getTypePtr()->getAs<RecordType>())
if (const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()))
return Buildclass_type_info(RD);
return BuildClassTypeInfo(RD);
switch (Type.getTypeClass()) {
default: {
@ -438,9 +447,51 @@ public:
return BuildSimpleType(Ty, "_ZTVN10__cxxabiv116__enum_type_infoE");
}
}
/// BuildClassTypeInfo - Builds the class type info (or a reference to it)
/// for the given record decl.
llvm::Constant *BuildClassTypeInfo(const CXXRecordDecl *RD) {
const CXXMethodDecl *KeyFunction = 0;
if (RD->isDynamicClass())
KeyFunction = CGM.getContext().getKeyFunction(RD);
if (KeyFunction) {
// If the key function is defined in this translation unit, then the RTTI
// related constants should also be emitted here, with external linkage.
if (KeyFunction->getBody())
return Buildclass_type_info(RD, llvm::GlobalValue::ExternalLinkage);
// Otherwise, we just want a reference to the type info.
return Buildclass_type_infoRef(RD);
}
// If there is no key function (or if the record doesn't have any virtual
// member functions or virtual bases), emit the type info with weak_odr
// linkage.
return Buildclass_type_info(RD, llvm::GlobalValue::WeakODRLinkage);
}
};
}
llvm::Constant *CodeGenModule::GetAddrOfRTTI(const CXXRecordDecl *RD) {
if (!getContext().getLangOptions().RTTI) {
const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
return llvm::Constant::getNullValue(Int8PtrTy);
}
return RTTIBuilder(*this).BuildClassTypeInfo(RD);
}
llvm::Constant *CodeGenModule::GetAddrOfRTTI(QualType Ty) {
if (!getContext().getLangOptions().RTTI) {
const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
return llvm::Constant::getNullValue(Int8PtrTy);
}
return RTTIBuilder(*this).BuildType(Ty);
}
llvm::Constant *CodeGenModule::GenerateRTTIRef(const CXXRecordDecl *RD) {
RTTIBuilder b(*this);
@ -450,7 +501,7 @@ llvm::Constant *CodeGenModule::GenerateRTTIRef(const CXXRecordDecl *RD) {
llvm::Constant *CodeGenModule::GenerateRTTI(const CXXRecordDecl *RD) {
RTTIBuilder b(*this);
return b.Buildclass_type_info(RD);
return b.Buildclass_type_info(RD, llvm::GlobalValue::ExternalLinkage);
}
llvm::Constant *CodeGenModule::GenerateRTTI(QualType Ty) {

Просмотреть файл

@ -203,7 +203,7 @@ public:
LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) {
Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
if (BuildVtable)
rtti = cgm.GenerateRTTIRef(MostDerivedClass);
rtti = CGM.GetAddrOfRTTI(MostDerivedClass);
}
// getVtableComponents - Returns a reference to the vtable components.
@ -1409,8 +1409,6 @@ void CGVtableInfo::GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
}
Vtable = GenerateVtable(Linkage, /*GenerateDefinition=*/true, RD, RD, 0);
CGM.GenerateRTTI(RD);
GenerateVTT(Linkage, RD);
}

Просмотреть файл

@ -212,6 +212,13 @@ public:
llvm::Constant *GetAddrOfFunction(GlobalDecl GD,
const llvm::Type *Ty = 0);
/// GetAddrOfRTTI - Get the address of the RTTI structure for the given type.
llvm::Constant *GetAddrOfRTTI(QualType Ty);
/// GetAddrOfRTTI - Get the address of the RTTI structure for the given record
/// decl.
llvm::Constant *GetAddrOfRTTI(const CXXRecordDecl *RD);
/// GenerateRTTI - Generate the rtti information for the given type.
llvm::Constant *GenerateRTTI(const CXXRecordDecl *RD);

Просмотреть файл

@ -31,6 +31,11 @@ class test1_D : public test1_B7 {
virtual void foo() { }
} d1;
// CHECK:__ZTI7test1_D:
// CHECK-NEXT: .quad (__ZTVN10__cxxabiv120__si_class_type_infoE) + 16
// CHECK-NEXT: .quad __ZTS7test1_D
// CHECK-NEXT: .quad __ZTI8test1_B7
// CHECK: __ZTSPVi:
// CHECK-NEXT: .asciz "PVi"
@ -77,13 +82,6 @@ class test1_D : public test1_B7 {
// CHECK-NEXT: .quad __ZTIFvvE
// CHECK-NEXT: .quad __ZTI7test3_A
// CHECK:__ZTI7test1_D:
// CHECK-NEXT: .quad (__ZTVN10__cxxabiv120__si_class_type_infoE) + 16
// CHECK-NEXT: .quad __ZTS7test1_D
// CHECK-NEXT: .quad __ZTI8test1_B7
// CHECK:__ZTI8test1_B7:
// CHECK-NEXT: .quad (__ZTVN10__cxxabiv121__vmi_class_type_infoE) + 16
// CHECK-NEXT: .quad __ZTS8test1_B7
@ -141,7 +139,6 @@ class test1_D : public test1_B7 {
// CHECK-NEXT: .quad __ZTS8test1_B2
// CHECK-NEXT: .quad __ZTI8test1_B1
class NP { };
void test2_1();
void test2_2(test1_D *dp) {
@ -166,7 +163,7 @@ void test2_2(test1_D *dp) {
// CHECK-LL-NEXT: %2 = load %"class.std::type_info"** %1
// CHECK-LL-NEXT: %call = call zeroext i1 @_ZNKSt9type_infoeqERKS_(%"class.std::type_info"* %2, %"class.std::type_info"* bitcast (%{{[0-9]*}}* @_ZTI7test1_D to %"class.std::type_info"*))
// CHECK-LL: %call2 = call zeroext i1 @_ZNKSt9type_infoeqERKS_(%"class.std::type_info"* bitcast (%0* @_ZTI2NP to %"class.std::type_info"*), %"class.std::type_info"* bitcast (%{{[0-9]*}}* @_ZTI7test1_D to %"class.std::type_info"*))
// CHECK-LL: %call2 = call zeroext i1 @_ZNKSt9type_infoeqERKS_(%"class.std::type_info"* bitcast (%{{[0-9]*}}* @_ZTI2NP to %"class.std::type_info"*), %"class.std::type_info"* bitcast (%{{[0-9]*}}* @_ZTI7test1_D to %"class.std::type_info"*))
// CHECK-LL: %3 = bitcast %class.test1_B7* %tmp5 to %"class.std::type_info"***
// CHECK-LL-NEXT: %4 = icmp ne %"class.std::type_info"*** %3, null

Просмотреть файл

@ -1,5 +1,5 @@
// RUN: clang-cc -emit-llvm %s -o - | FileCheck %s
// CHECK: @_ZTI3foo = linkonce_odr constant
// CHECK: @_ZTI3foo = constant
class foo {
foo();
virtual ~foo();

Просмотреть файл

@ -33,12 +33,18 @@ void D::f() { }
// CHECK: @_ZTV1B = external constant
// C has no key function, so its vtable should have weak_odr linkage.
// CHECK: @_ZTS1C = weak_odr constant
// CHECK: @_ZTI1C = weak_odr constant
// CHECK: @_ZTV1C = weak_odr constant
// D has a key function that is defined in this translation unit so its vtable is
// defined in the translation unit.
// CHECK: @_ZTS1D = constant
// CHECK: @_ZTI1D = constant
// CHECK: @_ZTV1D = constant
// The A vtable should have internal linkage since it is inside an anonymous
// namespace.
// CHECK: @_ZTSN12_GLOBAL__N_11AE = internal constant
// CHECK: @_ZTIN12_GLOBAL__N_11AE = internal constant
// CHECK: @_ZTVN12_GLOBAL__N_11AE = internal constant