Apply summary-based dead stripping to regular LTO modules with summaries.

If a regular LTO module has a summary index, then instead of linking
it into the combined regular LTO module right away, add it to the
combined summary index and associate it with a special module that
represents the combined regular LTO module.

Any such modules are linked during LTO::run(), at which time we use
the results of summary-based dead stripping to control whether to
link prevailing symbols.

Differential Revision: https://reviews.llvm.org/D33922

llvm-svn: 305482
diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
index 9c4316f..bd01902 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -964,11 +964,11 @@
   if (!BMsOrErr)
     return BMsOrErr.takeError();
 
-  // The bitcode file may contain multiple modules, we want the one with a
-  // summary.
+  // The bitcode file may contain multiple modules, we want the one that is
+  // marked as being the ThinLTO module.
   for (BitcodeModule &BM : *BMsOrErr) {
-    Expected<bool> HasSummary = BM.hasSummary();
-    if (HasSummary && *HasSummary)
+    Expected<BitcodeLTOInfo> LTOInfo = BM.getLTOInfo();
+    if (LTOInfo && LTOInfo->IsThinLTO)
       return BM;
   }
 
diff --git a/clang/test/CodeGen/Inputs/thinlto-multi-module.ll b/clang/test/CodeGen/Inputs/thinlto-multi-module.ll
new file mode 100644
index 0000000..e8dc16a
--- /dev/null
+++ b/clang/test/CodeGen/Inputs/thinlto-multi-module.ll
@@ -0,0 +1,9 @@
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @f2() {
+  ret void
+}
+
+!0 = !{i32 1, !"ThinLTO", i32 0}
+!llvm.module.flags = !{ !0 }
diff --git a/clang/test/CodeGen/thinlto-multi-module.ll b/clang/test/CodeGen/thinlto-multi-module.ll
index 21d28cf..385d98c 100644
--- a/clang/test/CodeGen/thinlto-multi-module.ll
+++ b/clang/test/CodeGen/thinlto-multi-module.ll
@@ -3,8 +3,8 @@
 ; RUN: opt -module-summary -o %t1.o %s
 ; RUN: llvm-lto -thinlto -o %t %t1.o
 
-; RUN: opt -o %t2.o %S/Inputs/thinlto_backend.ll
-; RUN: llvm-cat -b -o %t1cat.o %t1.o %t2.o
+; RUN: opt -module-summary -o %t2.o %S/Inputs/thinlto-multi-module.ll
+; RUN: llvm-cat -b -o %t1cat.o %t2.o %t1.o
 ; RUN: cp %t1cat.o %t1.o
 ; RUN: %clang -target x86_64-unknown-linux-gnu -O2 -o %t3.o -x ir %t1.o -c -fthinlto-index=%t.thinlto.bc
 ; RUN: llvm-nm %t3.o | FileCheck --check-prefix=CHECK-OBJ %s