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