]> granicus.if.org Git - clang/commitdiff
When building with optimizations, emit vtables where the key is not in the
authorAnders Carlsson <andersca@mac.com>
Sun, 30 Jan 2011 20:45:54 +0000 (20:45 +0000)
committerAnders Carlsson <andersca@mac.com>
Sun, 30 Jan 2011 20:45:54 +0000 (20:45 +0000)
current translation unit as available_externally.

This helps devirtualize the second example in PR3100, comment 18:

struct S { S() {}; virtual void xyzzy(); };
inline void foo(S *s) { s->xyzzy(); }
void bar() { S s; foo(&s); }

This involved four major changes:

1. In DefineUsedVTables, always mark virtual member functions as referenced for
   non-template classes and class template specializations.
2. In CodeGenVTables::ShouldEmitVTableInThisTU return true if optimizations are
   enabled, even if the key function is not implemented in this translation
   unit. We don't ever do this for code compiled with -fapple-kext, because we
   don't ever want to devirtualize virtual member function calls in that case.
3. Give the correct linkage for vtables where the key function is not defined.
4. Update the linkage for RTTI structures when necessary.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@124565 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGRTTI.cpp
lib/CodeGen/CGVTables.cpp
lib/CodeGen/CodeGenModule.cpp
lib/Sema/SemaDeclCXX.cpp
test/CodeGenCXX/vtable-available-externally.cpp [new file with mode: 0644]

index 100f73377d6799bc0c60091648796a20a6b929b8..c358c48e80ccebbb21a4d9ae4e386c6500322ee0 100644 (file)
@@ -484,6 +484,58 @@ void RTTIBuilder::BuildVTablePointer(const Type *Ty) {
   Fields.push_back(VTable);
 }
 
+// maybeUpdateRTTILinkage - Will update the linkage of the RTTI data structures
+// from available_externally to the correct linkage if necessary. An example of
+// this is:
+//
+//   struct A {
+//     virtual void f();
+//   };
+//
+//   const std::type_info &g() {
+//     return typeid(A);
+//   }
+//
+//   void A::f() { }
+//
+// When we're generating the typeid(A) expression, we do not yet know that
+// A's key function is defined in this translation unit, so we will give the
+// typeinfo and typename structures available_externally linkage. When A::f
+// forces the vtable to be generated, we need to change the linkage of the
+// typeinfo and typename structs, otherwise we'll end up with undefined
+// externals when linking.
+static void 
+maybeUpdateRTTILinkage(CodeGenModule &CGM, llvm::GlobalVariable *GV,
+                       QualType Ty) {
+  // We're only interested in globals with available_externally linkage.
+  if (!GV->hasAvailableExternallyLinkage())
+    return;
+
+  // Get the real linkage for the type.
+  llvm::GlobalVariable::LinkageTypes Linkage = getTypeInfoLinkage(CGM, Ty);
+
+  // If variable is supposed to have available_externally linkage, we don't
+  // need to do anything.
+  if (Linkage == llvm::GlobalVariable::AvailableExternallyLinkage)
+    return;
+
+  // Update the typeinfo linkage.
+  GV->setLinkage(Linkage);
+
+  // Get the typename global.
+  llvm::SmallString<256> OutName;
+  CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, OutName);
+  llvm::StringRef Name = OutName.str();
+
+  llvm::GlobalVariable *TypeNameGV = CGM.getModule().getNamedGlobal(Name);
+
+  assert(TypeNameGV->hasAvailableExternallyLinkage() &&
+         "Type name has different linkage from type info!");
+
+  // And update its linkage.
+  TypeNameGV->setLinkage(Linkage);
+}
+
 llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) {
   // We want to operate on the canonical type.
   Ty = CGM.getContext().getCanonicalType(Ty);
@@ -494,8 +546,11 @@ llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) {
   llvm::StringRef Name = OutName.str();
 
   llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
-  if (OldGV && !OldGV->isDeclaration())
+  if (OldGV && !OldGV->isDeclaration()) {
+    maybeUpdateRTTILinkage(CGM, OldGV, Ty);
+
     return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
+  }
 
   // Check if there is already an external RTTI descriptor for this type.
   bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty);
index c189e1d8ed3ad0d11b2ac2f5fdc807fd68343b96..de6cbf52f36fdb453fa43fc82a84a95d5b1f4b17 100644 (file)
@@ -2374,6 +2374,12 @@ bool CodeGenVTables::ShouldEmitVTableInThisTU(const CXXRecordDecl *RD) {
       TSK == TSK_ExplicitInstantiationDefinition)
     return true;
 
+  // If we're building with optimization, we always emit VTables since that
+  // allows for virtual function calls to be devirtualized.
+  // (We don't want to do this in -fapple-kext mode however).
+  if (CGM.getCodeGenOpts().OptimizationLevel && !CGM.getLangOptions().AppleKext)
+    return true;
+
   return KeyFunction->hasBody();
 }
 
index acf2592c2493a4d2f893e5d4437e0a74c66de1f4..b8f91b464ddac50f509675c4a4e0e7a9cec34582 100644 (file)
@@ -1082,6 +1082,12 @@ CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
     switch (KeyFunction->getTemplateSpecializationKind()) {
       case TSK_Undeclared:
       case TSK_ExplicitSpecialization:
+        // When compiling with optimizations turned on, we emit all vtables,
+        // even if the key function is not defined in the current translation
+        // unit. If this is the case, use available_externally linkage.
+        if (!Def && CodeGenOpts.OptimizationLevel)
+          return llvm::GlobalVariable::AvailableExternallyLinkage;
+
         if (KeyFunction->isInlined())
           return llvm::GlobalVariable::LinkOnceODRLinkage;
         
index 5da10352cc10b6fd07b80e4fc02a0c19265e3be5..c9c58b3e3a48d754ab95237933e64d4b94da5332 100644 (file)
@@ -7110,6 +7110,13 @@ bool Sema::DefineUsedVTables() {
       switch (KeyFunction->getTemplateSpecializationKind()) {
       case TSK_Undeclared:
       case TSK_ExplicitSpecialization:
+        // The key function is in another translation unit. Mark all of the
+        // virtual members of this class as referenced so that we can build a
+        // vtable anyway (in order to do devirtualization when optimizations
+        // are turned on for example.
+        MarkVirtualMembersReferenced(Loc, Class);
+        continue;
+
       case TSK_ExplicitInstantiationDeclaration:
         // The key function is in another translation unit.
         continue;
diff --git a/test/CodeGenCXX/vtable-available-externally.cpp b/test/CodeGenCXX/vtable-available-externally.cpp
new file mode 100644 (file)
index 0000000..b6e48c8
--- /dev/null
@@ -0,0 +1,55 @@
+// RUN: %clang_cc1 %s -I%S -triple=x86_64-apple-darwin10 -emit-llvm -O3 -o %t 
+// RUN: FileCheck --check-prefix=CHECK-TEST1 %s < %t
+// RUN: FileCheck --check-prefix=CHECK-TEST2 %s < %t
+
+#include <typeinfo>
+
+// Test1::A's key function (f) is not defined in this translation unit, but in
+// order to devirtualize calls, we emit the class related data with
+// available_externally linkage.
+
+// CHECK-TEST1: @_ZTVN5Test11AE = available_externally
+// CHECK-TEST1: @_ZTSN5Test11AE = available_externally
+// CHECK-TEST1: @_ZTIN5Test11AE = available_externally
+namespace Test1 {
+
+struct A {
+  A();
+  virtual void f();
+  virtual ~A() { }
+};
+
+A::A() { }
+
+void f(A* a) {
+  a->f();
+};
+
+// CHECK: define void @_ZN5Test11gEv
+// CHECK: call void @_ZN5Test11A1fEv
+void g() {
+  A a;
+  f(&a);
+}
+
+}
+
+// Test2::A's key function (f) is defined in this translation unit, but when
+// we're doing codegen for the typeid(A) call, we don't know that yet.
+// This tests mainly that the typeinfo and typename constants have their linkage
+// updated correctly.
+
+// CHECK-TEST2: @_ZTSN5Test21AE = constant
+// CHECK-TEST2: @_ZTIN5Test21AE = unnamed_addr constant
+// CHECK-TEST2: @_ZTVN5Test21AE = unnamed_addr constant
+namespace Test2 {
+  struct A {
+    virtual void f();
+  };
+
+  const std::type_info &g() {
+    return typeid(A);
+  };
+
+  void A::f() { }
+}