1 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2 ; RUN: llc %s -o - -mtriple=thumbv8m.base | FileCheck %s
6 declare i32 @h0(i32, i32, i32, i32)
7 define hidden i32 @f0() {
10 ; CHECK-NEXT: push {r7, lr}
12 ; CHECK-NEXT: movs r1, #1
13 ; CHECK-NEXT: movs r2, #2
14 ; CHECK-NEXT: movs r3, #3
15 ; CHECK-NEXT: ldr r7, [sp, #4]
16 ; CHECK-NEXT: mov lr, r7
17 ; CHECK-NEXT: pop {r7}
18 ; CHECK-NEXT: add sp, #4
20 %1 = tail call i32 bitcast (i32 (...)* @g to i32 ()*)()
21 %2 = tail call i32 @h0(i32 %1, i32 1, i32 2, i32 3)
26 define hidden i32 @f1() {
29 ; CHECK-NEXT: push {r7, lr}
31 ; CHECK-NEXT: pop {r7}
32 ; CHECK-NEXT: pop {r1}
33 ; CHECK-NEXT: mov lr, r1
35 %1 = tail call i32 bitcast (i32 (...)* @g to i32 ()*)()
36 %2 = tail call i32 @h1(i32 %1)
40 declare i32 @h2(i32, i32, i32, i32, i32)
41 define hidden i32 @f2(i32, i32, i32, i32, i32) {
44 ; CHECK-NEXT: push {r4, r5, r6, lr}
45 ; CHECK-NEXT: mov r4, r3
46 ; CHECK-NEXT: mov r5, r2
47 ; CHECK-NEXT: mov r6, r1
49 ; CHECK-NEXT: cbz r0, .LBB2_2
50 ; CHECK-NEXT: @ %bb.1:
51 ; CHECK-NEXT: mov r1, r6
52 ; CHECK-NEXT: mov r2, r5
53 ; CHECK-NEXT: mov r3, r4
54 ; CHECK-NEXT: ldr r4, [sp, #12]
55 ; CHECK-NEXT: mov lr, r4
56 ; CHECK-NEXT: pop {r4, r5, r6}
57 ; CHECK-NEXT: add sp, #4
59 ; CHECK-NEXT: .LBB2_2:
60 ; CHECK-NEXT: movs r0, #0
61 ; CHECK-NEXT: mvns r0, r0
62 ; CHECK-NEXT: pop {r4, r5, r6, pc}
63 %6 = tail call i32 bitcast (i32 (...)* @g to i32 ()*)()
64 %7 = icmp eq i32 %6, 0
65 br i1 %7, label %10, label %8
67 %9 = tail call i32 @h2(i32 %6, i32 %1, i32 %2, i32 %3, i32 %4)
70 %11 = phi i32 [ %9, %8 ], [ -1, %5 ]
74 ; Make sure that tail calls to function pointers that require r0-r3 for argument
75 ; passing do not break the compiler.
76 @fnptr = global i32 (i32, i32, i32, i32)* null
80 ; CHECK-NEXT: push {r4, lr}
81 ; CHECK-NEXT: movw r0, :lower16:fnptr
82 ; CHECK-NEXT: movt r0, :upper16:fnptr
83 ; CHECK-NEXT: ldr r4, [r0]
84 ; CHECK-NEXT: movs r0, #1
85 ; CHECK-NEXT: movs r1, #2
86 ; CHECK-NEXT: movs r2, #3
87 ; CHECK-NEXT: movs r3, #4
89 ; CHECK-NEXT: pop {r4, pc}
90 %1 = load i32 (i32, i32, i32, i32)*, i32 (i32, i32, i32, i32)** @fnptr
91 %2 = tail call i32 %1(i32 1, i32 2, i32 3, i32 4)
95 @fnptr2 = global i32 (i32, i32, i64)* null
99 ; CHECK-NEXT: push {r4, lr}
100 ; CHECK-NEXT: movw r0, :lower16:fnptr2
101 ; CHECK-NEXT: movt r0, :upper16:fnptr2
102 ; CHECK-NEXT: ldr r4, [r0]
103 ; CHECK-NEXT: movs r0, #1
104 ; CHECK-NEXT: movs r1, #2
105 ; CHECK-NEXT: movs r2, #3
106 ; CHECK-NEXT: movs r3, #0
108 ; CHECK-NEXT: pop {r4, pc}
109 %1 = load i32 (i32, i32, i64)*, i32 (i32, i32, i64)** @fnptr2
110 %2 = tail call i32 %1(i32 1, i32 2, i64 3)
114 ; Check that tail calls to function pointers where not all of r0-r3 are used for
115 ; parameter passing are tail-call optimized.
116 ; test5: params in r0, r1. r2 & r3 are free.
117 @fnptr3 = global i32 (i32, i32)* null
118 define i32 @test5() {
119 ; CHECK-LABEL: test5:
121 ; CHECK-NEXT: movw r0, :lower16:fnptr3
122 ; CHECK-NEXT: movt r0, :upper16:fnptr3
123 ; CHECK-NEXT: ldr r2, [r0]
124 ; CHECK-NEXT: movs r0, #1
125 ; CHECK-NEXT: movs r1, #2
127 %1 = load i32 (i32, i32)*, i32 (i32, i32)** @fnptr3
128 %2 = tail call i32 %1(i32 1, i32 2)
132 ; test6: params in r0 and r2-r3. r1 is free.
133 @fnptr4 = global i32 (i32, i64)* null
134 define i32 @test6() {
135 ; CHECK-LABEL: test6:
137 ; CHECK-NEXT: movw r0, :lower16:fnptr4
138 ; CHECK-NEXT: movt r0, :upper16:fnptr4
139 ; CHECK-NEXT: ldr r1, [r0]
140 ; CHECK-NEXT: movs r0, #1
141 ; CHECK-NEXT: movs r2, #2
142 ; CHECK-NEXT: movs r3, #0
144 %1 = load i32 (i32, i64)*, i32 (i32, i64)** @fnptr4
145 %2 = tail call i32 %1(i32 1, i64 2)
149 ; Check that tail calls to functions other than function pointers are
150 ; tail-call optimized.
151 define i32 @test7() {
152 ; CHECK-LABEL: test7:
154 ; CHECK-NEXT: movs r0, #1
155 ; CHECK-NEXT: movs r1, #2
156 ; CHECK-NEXT: movs r2, #3
157 ; CHECK-NEXT: movs r3, #4
159 %tail = tail call i32 @bar(i32 1, i32 2, i32 3, i32 4)
163 declare i32 @bar(i32, i32, i32, i32)
165 ; Regression test for failure to load indirect branch target (class tcGPR) from
167 %struct.S = type { i32 }
169 define void @test8(i32 (i32, i32, i32)* nocapture %fn, i32 %x) local_unnamed_addr {
170 ; CHECK-LABEL: test8:
171 ; CHECK: @ %bb.0: @ %entry
172 ; CHECK-NEXT: push {r4, r5, r6, r7, lr}
173 ; CHECK-NEXT: sub sp, #4
174 ; CHECK-NEXT: mov r4, r1
175 ; CHECK-NEXT: str r0, [sp] @ 4-byte Spill
176 ; CHECK-NEXT: bl test8_u
177 ; CHECK-NEXT: mov r5, r0
178 ; CHECK-NEXT: ldr r6, [r0]
179 ; CHECK-NEXT: movs r7, #0
180 ; CHECK-NEXT: mov r0, r7
181 ; CHECK-NEXT: bl test8_h
182 ; CHECK-NEXT: mov r1, r0
183 ; CHECK-NEXT: mov r0, r6
184 ; CHECK-NEXT: mov r2, r7
185 ; CHECK-NEXT: bl test8_g
186 ; CHECK-NEXT: str r4, [r5]
187 ; CHECK-NEXT: movs r0, #1
188 ; CHECK-NEXT: movs r1, #2
189 ; CHECK-NEXT: movs r2, #3
190 ; CHECK-NEXT: ldr r3, [sp] @ 4-byte Reload
191 ; CHECK-NEXT: add sp, #4
192 ; CHECK-NEXT: ldr r4, [sp, #16]
193 ; CHECK-NEXT: mov lr, r4
194 ; CHECK-NEXT: pop {r4, r5, r6, r7}
195 ; CHECK-NEXT: add sp, #4
198 %call = tail call %struct.S* bitcast (%struct.S* (...)* @test8_u to %struct.S* ()*)()
199 %a = getelementptr inbounds %struct.S, %struct.S* %call, i32 0, i32 0
200 %0 = load i32, i32* %a, align 4
201 %call1 = tail call i32 @test8_h(i32 0)
202 %call2 = tail call i32 @test8_g(i32 %0, i32 %call1, i32 0)
203 store i32 %x, i32* %a, align 4
204 %call4 = tail call i32 %fn(i32 1, i32 2, i32 3)
208 declare %struct.S* @test8_u(...)
210 declare i32 @test8_g(i32, i32, i32)
212 declare i32 @test8_h(i32)
214 ; Check that we don't introduce an unnecessary spill of lr.
215 declare i32 @h9(i32, i32, i32, i32)
216 define i32 @test9(i32* %x, i32* %y, i32* %z, i32* %a) {
217 ; CHECK-LABEL: test9:
219 ; CHECK-NEXT: push {r4, r7}
220 ; CHECK-NEXT: ldr r4, [r3]
221 ; CHECK-NEXT: ldr r3, [r3, #4]
222 ; CHECK-NEXT: adds r3, r4, r3
223 ; CHECK-NEXT: ldr r1, [r1]
224 ; CHECK-NEXT: ldr r0, [r0]
225 ; CHECK-NEXT: ldr r2, [r2]
226 ; CHECK-NEXT: pop {r4, r7}
228 %zz = load i32, i32* %z
229 %xx = load i32, i32* %x
230 %yy = load i32, i32* %y
231 %aa1 = load i32, i32* %a
232 %a2 = getelementptr i32, i32* %a, i32 1
233 %aa2 = load i32, i32* %a2
234 %aa = add i32 %aa1, %aa2
235 %r = tail call i32 @h9(i32 %xx, i32 %yy, i32 %zz, i32 %aa)