Rework when and how vtables are emitted, by tracking where vtables are
"used" (e.g., we will refer to the vtable in the generated code) and
when they are defined (i.e., because we've seen the key function
definition). Previously, we were effectively tracking "potential
definitions" rather than uses, so we were a bit too eager about emitting
vtables for classes without key functions. 

The new scheme:
  - For every use of a vtable, Sema calls MarkVTableUsed() to indicate
  the use. For example, this occurs when calling a virtual member
  function of the class, defining a constructor of that class type,
  dynamic_cast'ing from that type to a derived class, casting
  to/through a virtual base class, etc.
  - For every definition of a vtable, Sema calls MarkVTableUsed() to
  indicate the definition. This happens at the end of the translation
  unit for classes whose key function has been defined (so we can
  delay computation of the key function; see PR6564), and will also
  occur with explicit template instantiation definitions.
 - For every vtable defined/used, we mark all of the virtual member
 functions of that vtable as defined/used, unless we know that the key
 function is in another translation unit. This instantiates virtual
 member functions when needed.
  - At the end of the translation unit, Sema tells CodeGen (via the
  ASTConsumer) which vtables must be defined (CodeGen will define
  them) and which may be used (for which CodeGen will define the
  vtables lazily). 

From a language perspective, both the old and the new schemes are
permissible: we're allowed to instantiate virtual member functions
whenever we want per the standard. However, all other C++ compilers
were more lazy than we were, and our eagerness was both a performance
issue (we instantiated too much) and a portability problem (we broke
Boost test cases, which now pass).

Notes:
  (1) There's a ton of churn in the tests, because the order in which
  vtables get emitted to IR has changed. I've tried to isolate some of
  the larger tests from these issues.
  (2) Some diagnostics related to
  implicitly-instantiated/implicitly-defined virtual member functions
  have moved to the point of first use/definition. It's better this
  way.
  (3) I could use a review of the places where we MarkVTableUsed, to
  see if I missed any place where the language effectively requires a
  vtable.

Fixes PR7114 and PR6564.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@103718 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/test/CodeGenCXX/vtable-linkage.cpp b/test/CodeGenCXX/vtable-linkage.cpp
index c75efe2..02b1d5c 100644
--- a/test/CodeGenCXX/vtable-linkage.cpp
+++ b/test/CodeGenCXX/vtable-linkage.cpp
@@ -1,4 +1,16 @@
-// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o %t
+// RUN: FileCheck --check-prefix=CHECK-1 %s < %t
+// RUN: FileCheck --check-prefix=CHECK-2 %s < %t
+// RUN: FileCheck --check-prefix=CHECK-3 %s < %t
+// RUN: FileCheck --check-prefix=CHECK-4 %s < %t
+// RUN: FileCheck --check-prefix=CHECK-5 %s < %t
+// RUN: FileCheck --check-prefix=CHECK-6 %s < %t
+// RUN: FileCheck --check-prefix=CHECK-7 %s < %t
+// RUN: FileCheck --check-prefix=CHECK-8 %s < %t
+// RUN: FileCheck --check-prefix=CHECK-9 %s < %t
+// RUN: FileCheck --check-prefix=CHECK-10 %s < %t
+// RUN: FileCheck --check-prefix=CHECK-11 %s < %t
+// RUN: FileCheck --check-prefix=CHECK-12 %s < %t
 
 namespace {
   struct A {
@@ -73,7 +85,7 @@
 
 void use_F(F<char> &fc) {
   F<int> fi;
-  (void)fi;
+  fi.foo();
   F<long> fl;
   (void)fl;
   fc.foo();
@@ -81,71 +93,71 @@
 
 // B has a key function that is not defined in this translation unit so its vtable
 // has external linkage.
-// CHECK: @_ZTV1B = external constant
+// CHECK-1: @_ZTV1B = external constant
 
 // C has no key function, so its vtable should have weak_odr linkage.
-// CHECK: @_ZTV1C = weak_odr constant
-// CHECK: @_ZTS1C = weak_odr constant
-// CHECK: @_ZTI1C = weak_odr constant
+// CHECK-2: @_ZTV1C = weak_odr constant
+// CHECK-2: @_ZTS1C = weak_odr constant
+// CHECK-2: @_ZTI1C = 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
-// CHECK: @_ZTS1D = constant
-// CHECK: @_ZTI1D = constant
+// CHECK-3: @_ZTV1D = constant
+// CHECK-3: @_ZTS1D = constant
+// CHECK-3: @_ZTI1D = constant
 
 // E<char> is an explicit specialization with a key function defined
 // in this translation unit, so its vtable should have external
 // linkage.
-// CHECK: @_ZTV1EIcE = constant
-// CHECK: @_ZTS1EIcE = constant
-// CHECK: @_ZTI1EIcE = constant
+// CHECK-4: @_ZTV1EIcE = constant
+// CHECK-4: @_ZTS1EIcE = constant
+// CHECK-4: @_ZTI1EIcE = constant
 
 // E<short> is an explicit template instantiation with a key function
 // defined in this translation unit, so its vtable should have
 // weak_odr linkage.
-// CHECK: @_ZTV1EIsE = weak_odr constant
-// CHECK: @_ZTS1EIsE = weak_odr constant
-// CHECK: @_ZTI1EIsE = weak_odr constant
+// CHECK-5: @_ZTV1EIsE = weak_odr constant
+// CHECK-5: @_ZTS1EIsE = weak_odr constant
+// CHECK-5: @_ZTI1EIsE = weak_odr constant
 
 // F<short> is an explicit template instantiation without a key
 // function, so its vtable should have weak_odr linkage
-// CHECK: @_ZTV1FIsE = weak_odr constant
-// CHECK: @_ZTS1FIsE = weak_odr constant
-// CHECK: @_ZTI1FIsE = weak_odr constant
+// CHECK-6: @_ZTV1FIsE = weak_odr constant
+// CHECK-6: @_ZTS1FIsE = weak_odr constant
+// CHECK-6: @_ZTI1FIsE = weak_odr constant
 
 // E<long> is an implicit template instantiation with a key function
 // defined in this translation unit, so its vtable should have
 // weak_odr linkage.
-// CHECK: @_ZTV1EIlE = weak_odr constant
-// CHECK: @_ZTS1EIlE = weak_odr constant
-// CHECK: @_ZTI1EIlE = weak_odr constant
+// CHECK-7: @_ZTV1EIlE = weak_odr constant
+// CHECK-7: @_ZTS1EIlE = weak_odr constant
+// CHECK-7: @_ZTI1EIlE = weak_odr constant
 
 // F<long> is an implicit template instantiation with no key function,
 // so its vtable should have weak_odr linkage.
-// CHECK: @_ZTV1FIlE = weak_odr constant
-// CHECK: @_ZTS1FIlE = weak_odr constant
-// CHECK: @_ZTI1FIlE = weak_odr constant
+// CHECK-8: @_ZTV1FIlE = weak_odr constant
+// CHECK-8: @_ZTS1FIlE = weak_odr constant
+// CHECK-8: @_ZTI1FIlE = weak_odr constant
 
 // F<int> is an explicit template instantiation declaration without a
 // key function, so its vtable should have external linkage.
-// CHECK: @_ZTV1FIiE = external constant
+// CHECK-9: @_ZTV1FIiE = external constant
 
 // E<int> is an explicit template instantiation declaration. It has a
 // key function that is not instantiated, so we should only reference
 // its vtable, not define it.
-// CHECK: @_ZTV1EIiE = external constant
+// CHECK-10: @_ZTV1EIiE = external constant
 
 // The anonymous struct for e has no linkage, so the vtable should have
 // internal linkage.
-// CHECK: @"_ZTV3$_0" = internal constant
-// CHECK: @"_ZTS3$_0" = internal constant
-// CHECK: @"_ZTI3$_0" = internal constant
+// CHECK-11: @"_ZTV3$_0" = internal constant
+// CHECK-11: @"_ZTS3$_0" = internal constant
+// CHECK-11: @"_ZTI3$_0" = internal constant
 
 // The A vtable should have internal linkage since it is inside an anonymous 
 // namespace.
-// CHECK: @_ZTVN12_GLOBAL__N_11AE = internal constant
-// CHECK: @_ZTSN12_GLOBAL__N_11AE = internal constant
-// CHECK: @_ZTIN12_GLOBAL__N_11AE = internal constant
+// CHECK-12: @_ZTVN12_GLOBAL__N_11AE = internal constant
+// CHECK-12: @_ZTSN12_GLOBAL__N_11AE = internal constant
+// CHECK-12: @_ZTIN12_GLOBAL__N_11AE = internal constant