]> granicus.if.org Git - clang/commitdiff
Improve linkage of RTTI data structures. Introduce CodeGenModule::GetAddrOfRTTI which...
authorAnders Carlsson <andersca@mac.com>
Fri, 11 Dec 2009 02:46:30 +0000 (02:46 +0000)
committerAnders Carlsson <andersca@mac.com>
Fri, 11 Dec 2009 02:46:30 +0000 (02:46 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@91098 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 3548669058c796e12da36badd73472d88ff51d79..ad8734d8da7e9b1d9e7be91f753d252ec242e2a1 100644 (file)
@@ -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);
index 0e89e83c128a5cab61a4a7ac374db285b86e44d3..c8ce4ec6b991a31c280db849d63c62be0f1359a9 100644 (file)
@@ -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 *GV;
-    GV = CGM.getModule().getGlobalVariable(Name);
-    if (GV && !GV->isDeclaration())
-      return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
+    llvm::GlobalVariable *OGV = CGM.getModule().getGlobalVariable(Name);
+    if (OGV && !OGV->isDeclaration())
+      return llvm::ConstantExpr::getBitCast(OGV, Int8PtrTy);
 
-    llvm::Constant *C;
-    C = llvm::ConstantArray::get(VMContext, Name.substr(4));
+    llvm::Constant *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) {
index ef680d927d5be2b1120394594159945c072794a5..752f69c7c297cca7690027f85b3439ff6eedcdfb 100644 (file)
@@ -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);  
 }
 
index 466aaa67cc038c2841808d9589f9060a4a5d57fe..cc7ec9c301602261ec2e834f6ee69f3d1566549e 100644 (file)
@@ -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);
   
index a1ff1ff68729ecc14b7aa895aa3a8546fdb05492..762c22ef9ea8561ca03b41343aa4a219f5c28059 100644 (file)
@@ -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
index 30f3563d8a2f285d2749139cf8aa095a615a4f1c..9cfd58dae2d35c3a22684639417338d42e0f2de3 100644 (file)
@@ -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();
index 81ac0e43b4c563f07daa6f1c5b4934adbea1af59..5bc509b65e32f9b82c6aa977764744b496bb744d 100644 (file)
@@ -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