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
diff --git a/lib/CodeGen/CGExprCXX.cpp b/lib/CodeGen/CGExprCXX.cpp
index 3548669..ad8734d 100644
--- a/lib/CodeGen/CGExprCXX.cpp
+++ b/lib/CodeGen/CGExprCXX.cpp
@@ -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);
diff --git a/lib/CodeGen/CGRTTI.cpp b/lib/CodeGen/CGRTTI.cpp
index 0e89e83..c8ce4ec 100644
--- a/lib/CodeGen/CGRTTI.cpp
+++ b/lib/CodeGen/CGRTTI.cpp
@@ -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 *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 @@
   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) {
diff --git a/lib/CodeGen/CGVtable.cpp b/lib/CodeGen/CGVtable.cpp
index ef680d9..752f69c 100644
--- a/lib/CodeGen/CGVtable.cpp
+++ b/lib/CodeGen/CGVtable.cpp
@@ -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);  
 }
 
diff --git a/lib/CodeGen/CodeGenModule.h b/lib/CodeGen/CodeGenModule.h
index 466aaa6..cc7ec9c 100644
--- a/lib/CodeGen/CodeGenModule.h
+++ b/lib/CodeGen/CodeGenModule.h
@@ -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);