[ThinLTO] Handle bitcode without function summary sections gracefully
[oota-llvm.git] / test / Linker / funcimport.ll
1 ; First ensure that the ThinLTO handling in llvm-link and llvm-lto handles
2 ; bitcode without function summary sections gracefully.
3 ; RUN: llvm-as %s -o %t.bc
4 ; RUN: llvm-as %p/Inputs/funcimport.ll -o %t2.bc
5 ; RUN: llvm-link %t.bc -functionindex=%t.bc -S
6 ; RUN: llvm-lto -thinlto -o %t3 %t.bc %t2.bc
7
8 ; Do setup work for all below tests: generate bitcode and combined index
9 ; RUN: llvm-as -function-summary %s -o %t.bc
10 ; RUN: llvm-as -function-summary %p/Inputs/funcimport.ll -o %t2.bc
11 ; RUN: llvm-lto -thinlto -o %t3 %t.bc %t2.bc
12
13 ; Ensure statics are promoted/renamed correctly from this file (all but
14 ; constant variable need promotion).
15 ; RUN: llvm-link %t.bc -functionindex=%t3.thinlto.bc -S | FileCheck %s --check-prefix=EXPORTSTATIC
16 ; EXPORTSTATIC-DAG: @staticvar.llvm.1 = hidden global
17 ; EXPORTSTATIC-DAG: @staticconstvar = internal unnamed_addr constant
18 ; EXPORTSTATIC-DAG: @P.llvm.1 = hidden global void ()* null
19 ; EXPORTSTATIC-DAG: define hidden i32 @staticfunc.llvm.1
20 ; EXPORTSTATIC-DAG: define hidden void @staticfunc2.llvm.1
21
22 ; Ensure that both weak alias to an imported function and strong alias to a
23 ; non-imported function are correctly turned into declarations.
24 ; Also ensures that alias to a linkonce function is turned into a declaration
25 ; and that the associated linkonce function is not in the output, as it is
26 ; lazily linked and never referenced/materialized.
27 ; RUN: llvm-link %t2.bc -functionindex=%t3.thinlto.bc -import=globalfunc1:%t.bc -S | FileCheck %s --check-prefix=IMPORTGLOB1
28 ; IMPORTGLOB1-DAG: define available_externally void @globalfunc1
29 ; IMPORTGLOB1-DAG: declare void @globalfunc2
30 ; IMPORTGLOB1-DAG: declare extern_weak void @weakalias
31 ; IMPORTGLOB1-DAG: declare void @analias
32 ; IMPORTGLOB1-DAG: declare void @linkoncealias
33 ; IMPORTGLOB1-NOT: @linkoncefunc
34
35 ; Ensure that weak alias to a non-imported function is correctly
36 ; turned into a declaration, but that strong alias to an imported function
37 ; is imported as alias.
38 ; RUN: llvm-link %t2.bc -functionindex=%t3.thinlto.bc -import=globalfunc2:%t.bc -S | FileCheck %s --check-prefix=IMPORTGLOB2
39 ; IMPORTGLOB2-DAG: @analias = alias void (...), bitcast (void ()* @globalfunc2
40 ; IMPORTGLOB2-DAG: declare void @globalfunc1
41 ; IMPORTGLOB2-DAG: define available_externally void @globalfunc2
42 ; IMPORTGLOB2-DAG: declare extern_weak void @weakalias
43
44 ; Ensure that strong alias imported in second pass of importing ends up
45 ; as an alias.
46 ; RUN: llvm-link %t2.bc -functionindex=%t3.thinlto.bc -import=globalfunc1:%t.bc -import=globalfunc2:%t.bc -S | FileCheck %s --check-prefix=IMPORTGLOB3
47 ; IMPORTGLOB3-DAG: @analias = alias void (...), bitcast (void ()* @globalfunc2
48 ; IMPORTGLOB3-DAG: define available_externally void @globalfunc1
49 ; IMPORTGLOB3-DAG: define available_externally void @globalfunc2
50 ; IMPORTGLOB3-DAG: declare extern_weak void @weakalias
51
52 ; Ensure that strong alias imported in first pass of importing ends up
53 ; as an alias, and that seeing the alias definition during a second inlining
54 ; pass is handled correctly.
55 ; RUN: llvm-link %t2.bc -functionindex=%t3.thinlto.bc -import=globalfunc2:%t.bc -import=globalfunc1:%t.bc -S | FileCheck %s --check-prefix=IMPORTGLOB4
56 ; IMPORTGLOB4-DAG: @analias = alias void (...), bitcast (void ()* @globalfunc2
57 ; IMPORTGLOB4-DAG: define available_externally void @globalfunc2
58 ; IMPORTGLOB4-DAG: define available_externally void @globalfunc1
59 ; IMPORTGLOB4-DAG: declare extern_weak void @weakalias
60
61 ; Ensure that imported static variable and function references are correctly
62 ; promoted and renamed (including static constant variable).
63 ; RUN: llvm-link %t2.bc -functionindex=%t3.thinlto.bc -import=referencestatics:%t.bc -S | FileCheck %s --check-prefix=IMPORTSTATIC
64 ; IMPORTSTATIC-DAG: @staticvar.llvm.1 = available_externally hidden global
65 ; IMPORTSTATIC-DAG: @staticconstvar.llvm.1 = internal unnamed_addr constant
66 ; IMPORTSTATIC-DAG: define available_externally i32 @referencestatics
67 ; IMPORTSTATIC-DAG: %call = call i32 @staticfunc.llvm.1
68 ; IMPORTSTATIC-DAG: %0 = load i32, i32* @staticvar.llvm.1
69 ; IMPORTSTATIC-DAG: declare hidden i32 @staticfunc.llvm.1
70
71 ; Ensure that imported global (external) function and variable references
72 ; are handled correctly (including referenced variable imported as
73 ; available_externally definition)
74 ; RUN: llvm-link %t2.bc -functionindex=%t3.thinlto.bc -import=referenceglobals:%t.bc -S | FileCheck %s --check-prefix=IMPORTGLOBALS
75 ; IMPORTGLOBALS-DAG: @globalvar = available_externally global
76 ; IMPORTGLOBALS-DAG: declare void @globalfunc1()
77 ; IMPORTGLOBALS-DAG: define available_externally i32 @referenceglobals
78
79 ; Ensure that common variable correctly imported as common defition.
80 ; RUN: llvm-link %t2.bc -functionindex=%t3.thinlto.bc -import=referencecommon:%t.bc -S | FileCheck %s --check-prefix=IMPORTCOMMON
81 ; IMPORTCOMMON-DAG: @commonvar = common global
82 ; IMPORTCOMMON-DAG: define available_externally i32 @referencecommon
83
84 ; Ensure that imported static function pointer correctly promoted and renamed.
85 ; RUN: llvm-link %t2.bc -functionindex=%t3.thinlto.bc -import=callfuncptr:%t.bc -S | FileCheck %s --check-prefix=IMPORTFUNCPTR
86 ; IMPORTFUNCPTR-DAG: @P.llvm.1 = available_externally hidden global void ()* null
87 ; IMPORTFUNCPTR-DAG: define available_externally void @callfuncptr
88 ; IMPORTFUNCPTR-DAG: %0 = load void ()*, void ()** @P.llvm.1
89
90 ; Ensure that imported weak function reference/definition handled properly.
91 ; Imported weak_any definition should be skipped with warning, and imported
92 ; reference should turned into an external_weak declaration.
93 ; RUN: llvm-link %t2.bc -functionindex=%t3.thinlto.bc -import=callweakfunc:%t.bc -import=weakfunc:%t.bc -S 2>&1 | FileCheck %s --check-prefix=IMPORTWEAKFUNC
94 ; IMPORTWEAKFUNC-DAG: Ignoring import request for weak-any function weakfunc
95 ; IMPORTWEAKFUNC-DAG: @weakvar = extern_weak global i32, align 4
96 ; IMPORTWEAKFUNC-DAG: declare extern_weak void @weakfunc
97 ; IMPORTWEAKFUNC-DAG: define available_externally void @callweakfunc
98
99 @globalvar = global i32 1, align 4
100 @staticvar = internal global i32 1, align 4
101 @staticconstvar = internal unnamed_addr constant [2 x i32] [i32 10, i32 20], align 4
102 @commonvar = common global i32 0, align 4
103 @P = internal global void ()* null, align 8
104
105 @weakalias = weak alias void (...), bitcast (void ()* @globalfunc1 to void (...)*)
106 @analias = alias void (...), bitcast (void ()* @globalfunc2 to void (...)*)
107 @linkoncealias = alias void (...), bitcast (void ()* @linkoncefunc to void (...)*)
108
109 define void @globalfunc1() #0 {
110 entry:
111   ret void
112 }
113
114 define void @globalfunc2() #0 {
115 entry:
116   ret void
117 }
118
119 define linkonce_odr void @linkoncefunc() #0 {
120 entry:
121   ret void
122 }
123
124 define i32 @referencestatics(i32 %i) #0 {
125 entry:
126   %i.addr = alloca i32, align 4
127   store i32 %i, i32* %i.addr, align 4
128   %call = call i32 @staticfunc()
129   %0 = load i32, i32* @staticvar, align 4
130   %add = add nsw i32 %call, %0
131   %1 = load i32, i32* %i.addr, align 4
132   %idxprom = sext i32 %1 to i64
133   %arrayidx = getelementptr inbounds [2 x i32], [2 x i32]* @staticconstvar, i64 0, i64 %idxprom
134   %2 = load i32, i32* %arrayidx, align 4
135   %add1 = add nsw i32 %add, %2
136   ret i32 %add1
137 }
138
139 define i32 @referenceglobals(i32 %i) #0 {
140 entry:
141   %i.addr = alloca i32, align 4
142   store i32 %i, i32* %i.addr, align 4
143   call void @globalfunc1()
144   %0 = load i32, i32* @globalvar, align 4
145   ret i32 %0
146 }
147
148 define i32 @referencecommon(i32 %i) #0 {
149 entry:
150   %i.addr = alloca i32, align 4
151   store i32 %i, i32* %i.addr, align 4
152   %0 = load i32, i32* @commonvar, align 4
153   ret i32 %0
154 }
155
156 define void @setfuncptr() #0 {
157 entry:
158   store void ()* @staticfunc2, void ()** @P, align 8
159   ret void
160 }
161
162 define void @callfuncptr() #0 {
163 entry:
164   %0 = load void ()*, void ()** @P, align 8
165   call void %0()
166   ret void
167 }
168
169 @weakvar = weak global i32 1, align 4
170 define weak void @weakfunc() #0 {
171 entry:
172   ret void
173 }
174
175 define void @callweakfunc() #0 {
176 entry:
177   call void @weakfunc()
178   ret void
179 }
180
181 define internal i32 @staticfunc() #0 {
182 entry:
183   ret i32 1
184 }
185
186 define internal void @staticfunc2() #0 {
187 entry:
188   ret void
189 }