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