[LTO] Make processing of combined module more consistent

Summary:
1. Use stream 0 only for combined module. Previously if combined module was not
processes ThinLTO used the stream for own output. However small changes in input,
could trigger combined module  and shuffle outputs making life of llvm::LTO harder.

2. Always process combined module and write output to stream 0. Processing empty
combined module is cheap and allows llvm::LTO users to avoid implementing processing
which is already done in llvm::LTO.

Subscribers: mehdi_amini, inglorion, eraman, hiraditya

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

llvm-svn: 320905
diff --git a/llvm/test/ThinLTO/X86/deadstrip.ll b/llvm/test/ThinLTO/X86/deadstrip.ll
index 36115e98..651f48b 100644
--- a/llvm/test/ThinLTO/X86/deadstrip.ll
+++ b/llvm/test/ThinLTO/X86/deadstrip.ll
@@ -19,7 +19,7 @@
 ; RUN:   -r %t2.bc,_dead_func,l \
 ; RUN:   -r %t2.bc,_another_dead_func,pl
 ; RUN: llvm-dis < %t.out.0.3.import.bc | FileCheck %s --check-prefix=LTO2
-; RUN: llvm-dis < %t.out.1.3.import.bc | FileCheck %s --check-prefix=LTO2-CHECK2
+; RUN: llvm-dis < %t.out.2.3.import.bc | FileCheck %s --check-prefix=LTO2-CHECK2
 ; RUN: llvm-nm %t.out.1 | FileCheck %s --check-prefix=CHECK2-NM
 
 ; RUN: llvm-bcanalyzer -dump %t.out.index.bc | FileCheck %s --check-prefix=COMBINED
diff --git a/llvm/test/ThinLTO/X86/debuginfo-compositetype-import.ll b/llvm/test/ThinLTO/X86/debuginfo-compositetype-import.ll
index ae2f5f2..04fab9d 100644
--- a/llvm/test/ThinLTO/X86/debuginfo-compositetype-import.ll
+++ b/llvm/test/ThinLTO/X86/debuginfo-compositetype-import.ll
@@ -11,7 +11,7 @@
 ; RUN:   -r %t2.bc,main,plx \
 ; RUN:   -r %t2.bc,foo,l \
 ; RUN:   -r %t1.bc,foo,pl
-; RUN: llvm-dis < %t.out.1.3.import.bc | FileCheck %s
+; RUN: llvm-dis < %t.out.2.3.import.bc | FileCheck %s
 
 ; CHECK: distinct !DICompositeType(tag: DW_TAG_enumeration_type, name: "enum", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: 50, size: 32, flags: DIFlagFwdDecl, identifier: "enum")
 ; CHECK: distinct !DICompositeType(tag: DW_TAG_class_type, name: "class", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: 728, size: 448, flags: DIFlagFwdDecl, identifier: "class")
@@ -25,7 +25,7 @@
 ; RUN:   -r %t2.bc,main,plx \
 ; RUN:   -r %t2.bc,foo,l \
 ; RUN:   -r %t1.bc,foo,pl
-; RUN: llvm-dis < %t.out.1.3.import.bc | FileCheck %s --check-prefix=FULL
+; RUN: llvm-dis < %t.out.2.3.import.bc | FileCheck %s --check-prefix=FULL
 
 ; FULL: distinct !DICompositeType(tag: DW_TAG_enumeration_type, name: "enum", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: 50, size: 32, elements: !{{[0-9]+}}, identifier: "enum")
 ; FULL: distinct !DICompositeType(tag: DW_TAG_class_type, name: "class", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: 728, size: 448, elements: !{{[0-9]+}}, identifier: "class")
diff --git a/llvm/test/ThinLTO/X86/distributed_import.ll b/llvm/test/ThinLTO/X86/distributed_import.ll
index 2536302..a67fe20 100644
--- a/llvm/test/ThinLTO/X86/distributed_import.ll
+++ b/llvm/test/ThinLTO/X86/distributed_import.ll
@@ -58,6 +58,7 @@
 ; IMPORT: define available_externally void @analias() !thinlto_src_module
 ; EXPORT: @G.llvm.
 
+target triple = "x86_64-unknown-linux-gnu"
 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
 
 declare i32 @g(...)
diff --git a/llvm/test/ThinLTO/X86/empty-module.ll b/llvm/test/ThinLTO/X86/empty-module.ll
new file mode 100644
index 0000000..f97e0f0
--- /dev/null
+++ b/llvm/test/ThinLTO/X86/empty-module.ll
@@ -0,0 +1,18 @@
+; RUN: opt -module-summary -o %t.bc %s
+
+; RUN: rm -f %t2.0
+; RUN: llvm-lto2 run  %t.bc -r %t.bc,foo,pl -o %t2 -thinlto-distributed-indexes 
+; RUN: llvm-readobj -h %t2.0 | FileCheck %s
+; RUN: llvm-nm %t2.0 | count 0
+
+; CHECK: Format: ELF64-x86-64
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+@foo = ifunc i32 (i32), i64 ()* @foo_ifunc
+
+define internal i64 @foo_ifunc() {
+entry:
+  ret i64 0
+}
diff --git a/llvm/test/ThinLTO/X86/funcimport2.ll b/llvm/test/ThinLTO/X86/funcimport2.ll
index 86ce715..4ee95be 100644
--- a/llvm/test/ThinLTO/X86/funcimport2.ll
+++ b/llvm/test/ThinLTO/X86/funcimport2.ll
@@ -6,7 +6,7 @@
 ; RUN:     -r=%t1.bc,_foo,plx \
 ; RUN:     -r=%t2.bc,_main,plx \
 ; RUN:     -r=%t2.bc,_foo,l
-; RUN: llvm-dis %t.o.1.3.import.bc -o - | FileCheck %s
+; RUN: llvm-dis %t.o.2.3.import.bc -o - | FileCheck %s
 ; CHECK: define available_externally dso_local void @foo()
 
 ; We shouldn't do any importing at -O0
@@ -16,7 +16,7 @@
 ; RUN:     -r=%t1.bc,_foo,plx \
 ; RUN:     -r=%t2.bc,_main,plx \
 ; RUN:     -r=%t2.bc,_foo,l
-; RUN: llvm-dis %t.o.1.3.import.bc -o - | FileCheck %s --check-prefix=CHECKO0
+; RUN: llvm-dis %t.o.2.3.import.bc -o - | FileCheck %s --check-prefix=CHECKO0
 ; CHECKO0: declare dso_local void @foo(...)
 
 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
diff --git a/llvm/test/ThinLTO/X86/internalize.ll b/llvm/test/ThinLTO/X86/internalize.ll
index f40fbcd..2392a4e 100644
--- a/llvm/test/ThinLTO/X86/internalize.ll
+++ b/llvm/test/ThinLTO/X86/internalize.ll
@@ -7,7 +7,7 @@
 ; RUN:     -r=%t1.bc,_foo,pxl \
 ; RUN:     -r=%t1.bc,_bar,pl \
 ; RUN:     -r=%t1.bc,_linkonce_func,pl
-; RUN: llvm-dis < %t.o.0.2.internalize.bc | FileCheck  %s --check-prefix=INTERNALIZE2
+; RUN: llvm-dis < %t.o.1.2.internalize.bc | FileCheck  %s --check-prefix=INTERNALIZE2
 
 
 ; REGULAR: define void @foo
diff --git a/llvm/test/ThinLTO/X86/module_asm2.ll b/llvm/test/ThinLTO/X86/module_asm2.ll
index b79d7ad..b6e2f23 100644
--- a/llvm/test/ThinLTO/X86/module_asm2.ll
+++ b/llvm/test/ThinLTO/X86/module_asm2.ll
@@ -27,8 +27,8 @@
 ; RUN:     -r=%t2.bc,func3,l \
 ; RUN:     -r=%t2.bc,callglobalfunc,l \
 ; RUN:     -r=%t2.bc,callweakfunc,l
-; RUN: llvm-nm %t.o.0 | FileCheck  %s --check-prefix=NM0
-; RUN: llvm-nm %t.o.1 | FileCheck  %s --check-prefix=NM1
+; RUN: llvm-nm %t.o.1 | FileCheck  %s --check-prefix=NM0
+; RUN: llvm-nm %t.o.2 | FileCheck  %s --check-prefix=NM1
 
 ; Check that local values b and x, which are referenced on
 ; llvm.used and llvm.compiler.used, respectively, are not promoted.
diff --git a/llvm/test/ThinLTO/X86/module_asm_glob.ll b/llvm/test/ThinLTO/X86/module_asm_glob.ll
index e270075..3ce4816 100644
--- a/llvm/test/ThinLTO/X86/module_asm_glob.ll
+++ b/llvm/test/ThinLTO/X86/module_asm_glob.ll
@@ -11,8 +11,8 @@
 ; RUN:     -r=%t1.bc,_simplefunction,pl \
 ; RUN:     -r=%t2.bc,main,plx \
 ; RUN:     -r=%t2.bc,_simplefunction,l
-; RUN: llvm-nm %t.o.0 | FileCheck  %s --check-prefix=NM0
-; RUN: llvm-nm %t.o.1 | FileCheck  %s --check-prefix=NM1
+; RUN: llvm-nm %t.o.1 | FileCheck  %s --check-prefix=NM0
+; RUN: llvm-nm %t.o.2 | FileCheck  %s --check-prefix=NM1
 
 ; NM0: T foo
 ; NM1-NOT: foo
diff --git a/llvm/test/ThinLTO/X86/personality-local.ll b/llvm/test/ThinLTO/X86/personality-local.ll
index 650e882..5111128 100644
--- a/llvm/test/ThinLTO/X86/personality-local.ll
+++ b/llvm/test/ThinLTO/X86/personality-local.ll
@@ -6,7 +6,7 @@
 ; RUN:   -r %t1.bc,foo,l \
 ; RUN:   -r %t1.bc,bar,p \
 ; RUN:   -r %t1.bc,main,xp
-; RUN: llvm-readobj -t %t.o.1 | FileCheck %s
+; RUN: llvm-readobj -t %t.o.2 | FileCheck %s
 
 ; CHECK:      Symbol {
 ; CHECK:        Name: foo
diff --git a/llvm/test/ThinLTO/X86/personality.ll b/llvm/test/ThinLTO/X86/personality.ll
index a6caf37..319bd94 100644
--- a/llvm/test/ThinLTO/X86/personality.ll
+++ b/llvm/test/ThinLTO/X86/personality.ll
@@ -14,7 +14,7 @@
 ; RUN:   -r %t1.bc,personality_routine3,l \
 ; RUN:   -r %t1.bc,main,xp \
 ; RUN:   -r %t1.bc,bar,l
-; RUN: llvm-readobj -t %t.o.1 | FileCheck %s --check-prefix=BINDING
+; RUN: llvm-readobj -t %t.o.2 | FileCheck %s --check-prefix=BINDING
 
 ; BINDING:     Symbol {
 ; BINDING:       Name: personality_routine
diff --git a/llvm/test/ThinLTO/X86/reference_non_importable.ll b/llvm/test/ThinLTO/X86/reference_non_importable.ll
index 99b79ce..51e6f38 100644
--- a/llvm/test/ThinLTO/X86/reference_non_importable.ll
+++ b/llvm/test/ThinLTO/X86/reference_non_importable.ll
@@ -14,14 +14,14 @@
 target triple = "x86_64-apple-macosx10.11.0"
 
 ; We shouldn't promote the private because it has a section
-; RUN: llvm-dis < %t.o.0.2.internalize.bc | FileCheck  %s --check-prefix=PROMOTE
+; RUN: llvm-dis < %t.o.1.2.internalize.bc | FileCheck  %s --check-prefix=PROMOTE
 ; PROMOTE: @a = private global i8 0, section "__TEXT,__cstring,cstring_literals"
 @a = private global i8 0, section "__TEXT,__cstring,cstring_literals"
 @b = global i8 *@a
 
 
 ; We want foo to be imported in the main module!
-; RUN: llvm-dis < %t.o.1.3.import.bc  | FileCheck  %s --check-prefix=IMPORT
+; RUN: llvm-dis < %t.o.2.3.import.bc  | FileCheck  %s --check-prefix=IMPORT
 ; IMPORT: define available_externally dso_local i8** @foo()
 define i8 **@foo() {
 	ret i8 **@b