Produce direct calls instead of alias to linkonce_odr functions.

This is a small optimization on linux, but should help more on windows
where msvc only outputs one destructor if there would be two identical ones.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@194046 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CGCXX.cpp b/lib/CodeGen/CGCXX.cpp
index 25f3e6f..ab45e32 100644
--- a/lib/CodeGen/CGCXX.cpp
+++ b/lib/CodeGen/CGCXX.cpp
@@ -112,28 +112,13 @@
   // support aliases with that linkage, fail.
   llvm::GlobalValue::LinkageTypes Linkage = getFunctionLinkage(AliasDecl);
 
-  llvm::GlobalValue::LinkageTypes TargetLinkage
-    = getFunctionLinkage(TargetDecl);
-
-  // Don't create an alias to a linker weak symbol unless we know we can do
-  // that in every TU. This avoids producing different COMDATs in different
-  // TUs.
-  if (llvm::GlobalValue::isWeakForLinker(TargetLinkage)) {
-    if (!InEveryTU)
-      return true;
-
-    // In addition to making sure we produce it in every TU, we have to make
-    // sure llvm keeps it.
-    // FIXME: Instead of outputting an alias we could just replace every use of
-    // AliasDecl with TargetDecl.
-    assert(Linkage == TargetLinkage);
-    Linkage = llvm::GlobalValue::WeakODRLinkage;
-  }
-
   // We can't use an alias if the linkage is not valid for one.
   if (!llvm::GlobalAlias::isValidLinkage(Linkage))
     return true;
 
+  llvm::GlobalValue::LinkageTypes TargetLinkage =
+      getFunctionLinkage(TargetDecl);
+
   // Check if we have it already.
   StringRef MangledName = getMangledName(AliasDecl);
   llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
@@ -152,6 +137,26 @@
   if (Ref->getType() != AliasType)
     Aliasee = llvm::ConstantExpr::getBitCast(Ref, AliasType);
 
+  // Don't create an alias to a linker weak symbol unless we know we can do
+  // that in every TU. This avoids producing different COMDATs in different
+  // TUs.
+  if (llvm::GlobalValue::isWeakForLinker(TargetLinkage)) {
+    if (!InEveryTU)
+      return true;
+
+    assert(Linkage == TargetLinkage);
+    // Instead of creating as alias to a linkonce_odr, replace all of the uses
+    // of the aliassee.
+    if (TargetLinkage == llvm::GlobalValue::LinkOnceODRLinkage) {
+      if (!Entry)
+        return false;
+
+      Entry->replaceAllUsesWith(Aliasee);
+      Entry->eraseFromParent();
+      return false;
+    }
+  }
+
   // Create the alias with no name.
   llvm::GlobalAlias *Alias = 
     new llvm::GlobalAlias(AliasType, Linkage, "", Aliasee, &getModule());