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