[cfe-commits] r91098 - in /cfe/trunk: lib/CodeGen/CGExprCXX.cpp lib/CodeGen/CGRTTI.cpp lib/CodeGen/CGVtable.cpp lib/CodeGen/CodeGenModule.h test/CodeGenCXX/rtti.cpp test/CodeGenCXX/virt-dtor-key.cpp test/CodeGenCXX/vtable-linkage.cpp

Anders Carlsson andersca at mac.com
Thu Dec 10 18:46:30 PST 2009


Author: andersca
Date: Thu Dec 10 20:46:30 2009
New Revision: 91098

URL: http://llvm.org/viewvc/llvm-project?rev=91098&view=rev
Log:
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).

Modified:
    cfe/trunk/lib/CodeGen/CGExprCXX.cpp
    cfe/trunk/lib/CodeGen/CGRTTI.cpp
    cfe/trunk/lib/CodeGen/CGVtable.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.h
    cfe/trunk/test/CodeGenCXX/rtti.cpp
    cfe/trunk/test/CodeGenCXX/virt-dtor-key.cpp
    cfe/trunk/test/CodeGenCXX/vtable-linkage.cpp

Modified: cfe/trunk/lib/CodeGen/CGExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprCXX.cpp?rev=91098&r1=91097&r2=91098&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprCXX.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprCXX.cpp Thu Dec 10 20:46:30 2009
@@ -352,18 +352,10 @@
 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);

Modified: cfe/trunk/lib/CodeGen/CGRTTI.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGRTTI.cpp?rev=91098&r1=91097&r2=91098&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGRTTI.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGRTTI.cpp Thu Dec 10 20:46:30 2009
@@ -68,27 +68,37 @@
     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 *GV;
-    GV = CGM.getModule().getGlobalVariable(Name);
-    if (GV && !GV->isDeclaration())
-      return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
-
-    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 *OGV = CGM.getModule().getGlobalVariable(Name);
+    if (OGV && !OGV->isDeclaration())
+      return llvm::ConstantExpr::getBitCast(OGV, Int8PtrTy);
+
+    llvm::Constant *C = llvm::ConstantArray::get(VMContext, Name.substr(4));
+
+    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 @@
   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 @@
 
   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 @@
   }
 
 
-  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 @@
 
     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 @@
       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 @@
       }
     }
 
-    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 @@
       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 @@
     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 @@
       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::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) {

Modified: cfe/trunk/lib/CodeGen/CGVtable.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGVtable.cpp?rev=91098&r1=91097&r2=91098&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGVtable.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGVtable.cpp Thu Dec 10 20:46:30 2009
@@ -203,7 +203,7 @@
       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 @@
   }
   
   Vtable = GenerateVtable(Linkage, /*GenerateDefinition=*/true, RD, RD, 0);
-  
-  CGM.GenerateRTTI(RD);
   GenerateVTT(Linkage, RD);  
 }
 

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=91098&r1=91097&r2=91098&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.h Thu Dec 10 20:46:30 2009
@@ -212,6 +212,13 @@
   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);
   

Modified: cfe/trunk/test/CodeGenCXX/rtti.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/rtti.cpp?rev=91098&r1=91097&r2=91098&view=diff

==============================================================================
--- cfe/trunk/test/CodeGenCXX/rtti.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/rtti.cpp Thu Dec 10 20:46:30 2009
@@ -31,6 +31,11 @@
   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 @@
 // 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 @@
 // 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 @@
 // 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

Modified: cfe/trunk/test/CodeGenCXX/virt-dtor-key.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/virt-dtor-key.cpp?rev=91098&r1=91097&r2=91098&view=diff

==============================================================================
--- cfe/trunk/test/CodeGenCXX/virt-dtor-key.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/virt-dtor-key.cpp Thu Dec 10 20:46:30 2009
@@ -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();

Modified: cfe/trunk/test/CodeGenCXX/vtable-linkage.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/vtable-linkage.cpp?rev=91098&r1=91097&r2=91098&view=diff

==============================================================================
--- cfe/trunk/test/CodeGenCXX/vtable-linkage.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/vtable-linkage.cpp Thu Dec 10 20:46:30 2009
@@ -33,12 +33,18 @@
 // 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





More information about the cfe-commits mailing list