1 ; RUN: llc -mtriple riscv32-unknown-linux-gnu -o - %s | FileCheck %s
2 ; RUN: llc -mtriple riscv32-unknown-elf -o - %s | FileCheck %s
4 ; Perform tail call optimization for global address.
5 declare i32 @callee_tail(i32 %i)
6 define i32 @caller_tail(i32 %i) nounwind {
7 ; CHECK-LABEL: caller_tail
8 ; CHECK: tail callee_tail
10 %r = tail call i32 @callee_tail(i32 %i)
14 ; Perform tail call optimization for external symbol.
15 @dest = global [2 x i8] zeroinitializer
16 declare void @llvm.memcpy.p0i8.p0i8.i32(i8*, i8*, i32, i1)
17 define void @caller_extern(i8* %src) optsize {
19 ; CHECK: caller_extern
20 ; CHECK-NOT: call memcpy
22 tail call void @llvm.memcpy.p0i8.p0i8.i32(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @dest, i32 0, i32 0), i8* %src, i32 7, i1 false)
26 ; Perform tail call optimization for external symbol.
27 @dest_pgso = global [2 x i8] zeroinitializer
28 define void @caller_extern_pgso(i8* %src) !prof !14 {
30 ; CHECK: caller_extern_pgso
31 ; CHECK-NOT: call memcpy
33 tail call void @llvm.memcpy.p0i8.p0i8.i32(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @dest_pgso, i32 0, i32 0), i8* %src, i32 7, i1 false)
37 ; Perform indirect tail call optimization (for function pointer call).
38 declare void @callee_indirect1()
39 declare void @callee_indirect2()
40 define void @caller_indirect_tail(i32 %a) nounwind {
41 ; CHECK-LABEL: caller_indirect_tail
42 ; CHECK-NOT: call callee_indirect1
43 ; CHECK-NOT: call callee_indirect2
44 ; CHECK-NOT: tail callee_indirect1
45 ; CHECK-NOT: tail callee_indirect2
47 ; CHECK: lui a0, %hi(callee_indirect2)
48 ; CHECK-NEXT: addi t1, a0, %lo(callee_indirect2)
51 ; CHECK: lui a0, %hi(callee_indirect1)
52 ; CHECK-NEXT: addi t1, a0, %lo(callee_indirect1)
55 %tobool = icmp eq i32 %a, 0
56 %callee = select i1 %tobool, void ()* @callee_indirect1, void ()* @callee_indirect2
57 tail call void %callee()
61 ; Make sure we don't use t0 as the source for jr as that is a hint to pop the
62 ; return address stack on some microarchitectures.
63 define i32 @caller_indirect_no_t0(i32 (i32, i32, i32, i32, i32, i32, i32)* %0, i32 %1, i32 %2, i32 %3, i32 %4, i32 %5, i32 %6, i32 %7) {
64 ; CHECK-LABEL: caller_indirect_no_t0:
66 ; CHECK-NEXT: mv t1, a0
67 ; CHECK-NEXT: mv a0, a1
68 ; CHECK-NEXT: mv a1, a2
69 ; CHECK-NEXT: mv a2, a3
70 ; CHECK-NEXT: mv a3, a4
71 ; CHECK-NEXT: mv a4, a5
72 ; CHECK-NEXT: mv a5, a6
73 ; CHECK-NEXT: mv a6, a7
75 %9 = tail call i32 %0(i32 %1, i32 %2, i32 %3, i32 %4, i32 %5, i32 %6, i32 %7)
79 ; Do not tail call optimize functions with varargs passed by stack.
80 declare i32 @callee_varargs(i32, ...)
81 define void @caller_varargs(i32 %a, i32 %b) nounwind {
82 ; CHECK-LABEL: caller_varargs
83 ; CHECK-NOT: tail callee_varargs
84 ; CHECK: call callee_varargs
86 %call = tail call i32 (i32, ...) @callee_varargs(i32 %a, i32 %b, i32 %b, i32 %a, i32 %a, i32 %b, i32 %b, i32 %a, i32 %a)
90 ; Do not tail call optimize if stack is used to pass parameters.
91 declare i32 @callee_args(i32 %a, i32 %b, i32 %c, i32 %dd, i32 %e, i32 %ff, i32 %g, i32 %h, i32 %i, i32 %j, i32 %k, i32 %l, i32 %m, i32 %n)
92 define i32 @caller_args(i32 %a, i32 %b, i32 %c, i32 %dd, i32 %e, i32 %ff, i32 %g, i32 %h, i32 %i, i32 %j, i32 %k, i32 %l, i32 %m, i32 %n) nounwind {
93 ; CHECK-LABEL: caller_args
94 ; CHECK-NOT: tail callee_args
95 ; CHECK: call callee_args
97 %r = tail call i32 @callee_args(i32 %a, i32 %b, i32 %c, i32 %dd, i32 %e, i32 %ff, i32 %g, i32 %h, i32 %i, i32 %j, i32 %k, i32 %l, i32 %m, i32 %n)
101 ; Do not tail call optimize if parameters need to be passed indirectly.
102 declare i32 @callee_indirect_args(fp128 %a)
103 define void @caller_indirect_args() nounwind {
104 ; CHECK-LABEL: caller_indirect_args
105 ; CHECK-NOT: tail callee_indirect_args
106 ; CHECK: call callee_indirect_args
108 %call = tail call i32 @callee_indirect_args(fp128 0xL00000000000000003FFF000000000000)
112 ; Externally-defined functions with weak linkage should not be tail-called.
113 ; The behaviour of branch instructions in this situation (as used for tail
114 ; calls) is implementation-defined, so we cannot rely on the linker replacing
115 ; the tail call with a return.
116 declare extern_weak void @callee_weak()
117 define void @caller_weak() nounwind {
118 ; CHECK-LABEL: caller_weak
119 ; CHECK-NOT: tail callee_weak
120 ; CHECK: call callee_weak
122 tail call void @callee_weak()
126 ; Exception-handling functions need a special set of instructions to indicate a
127 ; return to the hardware. Tail-calling another function would probably break
129 declare void @callee_irq()
130 define void @caller_irq() #0 {
131 ; CHECK-LABEL: caller_irq
132 ; CHECK-NOT: tail callee_irq
133 ; CHECK: call callee_irq
135 tail call void @callee_irq()
138 attributes #0 = { "interrupt"="machine" }
140 ; Byval parameters hand the function a pointer directly into the stack area
141 ; we want to reuse during a tail call. Do not tail call optimize functions with
143 declare i32 @callee_byval(i32** byval(i32*) %a)
144 define i32 @caller_byval() nounwind {
145 ; CHECK-LABEL: caller_byval
146 ; CHECK-NOT: tail callee_byval
147 ; CHECK: call callee_byval
150 %r = tail call i32 @callee_byval(i32** byval(i32*) %a)
154 ; Do not tail call optimize if callee uses structret semantics.
155 %struct.A = type { i32 }
156 @a = global %struct.A zeroinitializer
158 declare void @callee_struct(%struct.A* sret(%struct.A) %a)
159 define void @caller_nostruct() nounwind {
160 ; CHECK-LABEL: caller_nostruct
161 ; CHECK-NOT: tail callee_struct
162 ; CHECK: call callee_struct
164 tail call void @callee_struct(%struct.A* sret(%struct.A) @a)
168 ; Do not tail call optimize if caller uses structret semantics.
169 declare void @callee_nostruct()
170 define void @caller_struct(%struct.A* sret(%struct.A) %a) nounwind {
171 ; CHECK-LABEL: caller_struct
172 ; CHECK-NOT: tail callee_nostruct
173 ; CHECK: call callee_nostruct
175 tail call void @callee_nostruct()
179 ; Do not tail call optimize if disabled.
180 define i32 @disable_tail_calls(i32 %i) nounwind "disable-tail-calls"="true" {
181 ; CHECK-LABEL: disable_tail_calls:
182 ; CHECK-NOT: tail callee_nostruct
183 ; CHECK: call callee_tail
185 %rv = tail call i32 @callee_tail(i32 %i)
189 ; Duplicate returns to enable tail call optimizations.
194 define i32 @duplicate_returns(i32 %a, i32 %b) nounwind {
195 ; CHECK-LABEL: duplicate_returns:
201 %cmp = icmp eq i32 %a, 0
202 br i1 %cmp, label %if.then, label %if.else
204 if.then: ; preds = %entry
205 %call = tail call i32 @test()
208 if.else: ; preds = %entry
209 %cmp1 = icmp eq i32 %b, 0
210 br i1 %cmp1, label %if.then2, label %if.else4
212 if.then2: ; preds = %if.else
213 %call3 = tail call i32 @test1()
216 if.else4: ; preds = %if.else
217 %cmp5 = icmp sgt i32 %a, %b
218 br i1 %cmp5, label %if.then6, label %if.else8
220 if.then6: ; preds = %if.else4
221 %call7 = tail call i32 @test2()
224 if.else8: ; preds = %if.else4
225 %call9 = tail call i32 @test3()
228 return: ; preds = %if.else8, %if.then6, %if.then2, %if.then
229 %retval = phi i32 [ %call, %if.then ], [ %call3, %if.then2 ], [ %call7, %if.then6 ], [ %call9, %if.else8 ]
233 !llvm.module.flags = !{!0}
234 !0 = !{i32 1, !"ProfileSummary", !1}
235 !1 = !{!2, !3, !4, !5, !6, !7, !8, !9}
236 !2 = !{!"ProfileFormat", !"InstrProf"}
237 !3 = !{!"TotalCount", i64 10000}
238 !4 = !{!"MaxCount", i64 10}
239 !5 = !{!"MaxInternalCount", i64 1}
240 !6 = !{!"MaxFunctionCount", i64 1000}
241 !7 = !{!"NumCounts", i64 3}
242 !8 = !{!"NumFunctions", i64 3}
243 !9 = !{!"DetailedSummary", !10}
244 !10 = !{!11, !12, !13}
245 !11 = !{i32 10000, i64 100, i32 1}
246 !12 = !{i32 999000, i64 100, i32 1}
247 !13 = !{i32 999999, i64 1, i32 2}
248 !14 = !{!"function_entry_count", i64 0}