[cfe-commits] r90687 - in /cfe/trunk: lib/CodeGen/CGVtable.cpp lib/CodeGen/CGVtable.h test/CodeGenCXX/vtable-linkage.cpp
Anders Carlsson
andersca at mac.com
Sat Dec 5 16:53:22 PST 2009
Author: andersca
Date: Sat Dec 5 18:53:22 2009
New Revision: 90687
URL: http://llvm.org/viewvc/llvm-project?rev=90687&view=rev
Log:
More linkage improvements.
Modified:
cfe/trunk/lib/CodeGen/CGVtable.cpp
cfe/trunk/lib/CodeGen/CGVtable.h
cfe/trunk/test/CodeGenCXX/vtable-linkage.cpp
Modified: cfe/trunk/lib/CodeGen/CGVtable.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGVtable.cpp?rev=90687&r1=90686&r2=90687&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGVtable.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGVtable.cpp Sat Dec 5 18:53:22 2009
@@ -1113,6 +1113,7 @@
llvm::GlobalVariable *
CGVtableInfo::GenerateVtable(llvm::GlobalVariable::LinkageTypes Linkage,
+ bool GenerateDefinition,
const CXXRecordDecl *LayoutClass,
const CXXRecordDecl *RD, uint64_t Offset) {
llvm::SmallString<256> OutName;
@@ -1134,9 +1135,7 @@
if (AddressPoint == 0)
AddressPoint = 1;
} else {
- bool CreateDefinition = Linkage != llvm::GlobalVariable::ExternalLinkage;
-
- VtableBuilder b(RD, LayoutClass, Offset, CGM, CreateDefinition);
+ VtableBuilder b(RD, LayoutClass, Offset, CGM, GenerateDefinition);
D1(printf("vtable %s\n", RD->getNameAsCString()));
// First comes the vtables for all the non-virtual bases...
@@ -1150,12 +1149,15 @@
llvm::ArrayType *ArrayType =
llvm::ArrayType::get(Int8PtrTy, b.getVtable().size());
- if (CreateDefinition) {
+ if (GenerateDefinition)
Init = llvm::ConstantArray::get(ArrayType, &b.getVtable()[0],
b.getVtable().size());
- }
+
llvm::GlobalVariable *OGV = GV;
- GV = createGlobalVariable(CGM, LayoutClass, ArrayType, Init, Name);
+
+ GV = new llvm::GlobalVariable(CGM.getModule(), ArrayType,
+ /*isConstant=*/true, Linkage, Init, Name);
+
if (OGV) {
GV->takeName(OGV);
llvm::Constant *NewPtr =
@@ -1369,17 +1371,21 @@
return llvm::ConstantExpr::getBitCast(VTT, Int8PtrTy);
}
-void CGVtableInfo::GenerateClassData(const CXXRecordDecl *RD) {
- Vtables[RD] = GenerateVtable(llvm::GlobalValue::WeakODRLinkage, RD, RD, 0);
+void CGVtableInfo::GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
+ const CXXRecordDecl *RD) {
+ assert(!Vtables.count(RD) && "Vtable has already been generated!");
+
+ Vtables[RD] = GenerateVtable(Linkage, /*GenerateDefinition=*/true, RD, RD, 0);
CGM.GenerateRTTI(RD);
CGM.GenerateVTT(RD);
}
llvm::GlobalVariable *CGVtableInfo::getVtable(const CXXRecordDecl *RD) {
- llvm::GlobalVariable *Vtable = Vtables[RD];
+ llvm::GlobalVariable *Vtable = Vtables.lookup(RD);
if (!Vtable)
- Vtable = GenerateVtable(llvm::GlobalValue::ExternalLinkage, RD, RD, 0);
+ Vtable = GenerateVtable(llvm::GlobalValue::ExternalLinkage,
+ /*GenerateDefinition=*/false, RD, RD, 0);
return Vtable;
}
@@ -1388,6 +1394,7 @@
CGVtableInfo::getCtorVtable(const CXXRecordDecl *LayoutClass,
const CXXRecordDecl *RD, uint64_t Offset) {
return GenerateVtable(llvm::GlobalValue::InternalLinkage,
+ /*GenerateDefinition=*/true,
LayoutClass, RD, Offset);
}
@@ -1420,7 +1427,15 @@
return;
}
+ llvm::GlobalVariable::LinkageTypes Linkage;
+ if (RD->isInAnonymousNamespace())
+ Linkage = llvm::GlobalVariable::InternalLinkage;
+ else if (KeyFunction)
+ Linkage = llvm::GlobalVariable::ExternalLinkage;
+ else
+ Linkage = llvm::GlobalVariable::WeakODRLinkage;
+
// Emit the data.
- GenerateClassData(RD);
+ GenerateClassData(Linkage, RD);
}
Modified: cfe/trunk/lib/CodeGen/CGVtable.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGVtable.h?rev=90687&r1=90686&r2=90687&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGVtable.h (original)
+++ cfe/trunk/lib/CodeGen/CGVtable.h Sat Dec 5 18:53:22 2009
@@ -92,13 +92,15 @@
/// GenerateClassData - Generate all the class data requires to be generated
/// upon definition of a KeyFunction. This includes the vtable, the
/// rtti data structure and the VTT.
- void GenerateClassData(const CXXRecordDecl *RD);
+ ///
+ /// \param Linkage - The desired linkage of the vtable, the RTTI and the VTT.
+ void GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
+ const CXXRecordDecl *RD);
llvm::GlobalVariable *
-
GenerateVtable(llvm::GlobalVariable::LinkageTypes Linkage,
- const CXXRecordDecl *LayoutClass, const CXXRecordDecl *RD,
- uint64_t Offset);
+ bool GenerateDefinition, const CXXRecordDecl *LayoutClass,
+ const CXXRecordDecl *RD, uint64_t Offset);
public:
CGVtableInfo(CodeGenModule &CGM)
Modified: cfe/trunk/test/CodeGenCXX/vtable-linkage.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/vtable-linkage.cpp?rev=90687&r1=90686&r2=90687&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/vtable-linkage.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/vtable-linkage.cpp Sat Dec 5 18:53:22 2009
@@ -22,6 +22,12 @@
C::C() { }
+struct D {
+ virtual void f();
+};
+
+void D::f() { }
+
// B has a key function that is not defined in this translation unit so its vtable
// has external linkage.
// CHECK: @_ZTV1B = external constant
@@ -29,6 +35,10 @@
// C has no key function, so its vtable should have weak_odr linkage.
// 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: @_ZTV1D = constant
+
// The A vtable should have internal linkage since it is inside an anonymous
// namespace.
// CHECK: @_ZTVN12_GLOBAL__N_11AE = internal constant
More information about the cfe-commits
mailing list