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