File: tailcall.ll

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (207 lines) | stat: -rw-r--r-- 5,996 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc < %s -mtriple=sparc -verify-machineinstrs | FileCheck %s

define i32 @simple_leaf(i32 %i) #0 {
; CHECK-LABEL: simple_leaf:
; CHECK:       ! %bb.0: ! %entry
; CHECK-NEXT:    mov %o7, %g1
; CHECK-NEXT:    call foo
; CHECK-NEXT:    mov %g1, %o7
entry:
  %call = tail call i32 @foo(i32 %i)
  ret i32 %call
}

define i32 @simple_standard(i32 %i) #1 {
; CHECK-LABEL: simple_standard:
; CHECK:       ! %bb.0: ! %entry
; CHECK-NEXT:    save %sp, -96, %sp
; CHECK-NEXT:    call foo
; CHECK-NEXT:    restore
entry:
  %call = tail call i32 @foo(i32 %i)
  ret i32 %call
}

define i32 @extra_arg_leaf(i32 %i) #0 {
; CHECK-LABEL: extra_arg_leaf:
; CHECK:       ! %bb.0: ! %entry
; CHECK-NEXT:    mov 12, %o1
; CHECK-NEXT:    mov %o7, %g1
; CHECK-NEXT:    call foo2
; CHECK-NEXT:    mov %g1, %o7
entry:
  %call = tail call i32 @foo2(i32 %i, i32 12)
  ret i32 %call
}

define i32 @extra_arg_standard(i32 %i) #1 {
; CHECK-LABEL: extra_arg_standard:
; CHECK:       ! %bb.0: ! %entry
; CHECK-NEXT:    save %sp, -96, %sp
; CHECK-NEXT:    call foo2
; CHECK-NEXT:    restore %g0, 12, %o1
entry:
  %call = tail call i32 @foo2(i32 %i, i32 12)
  ret i32 %call
}

; Perform tail call optimization for external symbol.

define void @caller_extern(i8* %src) optsize #0 {
; CHECK-LABEL: caller_extern:
; CHECK:       ! %bb.0: ! %entry
; CHECK-NEXT:    sethi %hi(dest), %o1
; CHECK-NEXT:    add %o1, %lo(dest), %o1
; CHECK-NEXT:    mov 7, %o2
; CHECK-NEXT:    mov %o0, %o3
; CHECK-NEXT:    mov %o1, %o0
; CHECK-NEXT:    mov %o3, %o1
; CHECK-NEXT:    mov %o7, %g1
; CHECK-NEXT:    call memcpy
; CHECK-NEXT:    mov %g1, %o7
entry:
  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)
  ret void
}

; Perform tail call optimization for function pointer.

define i32 @func_ptr_test(i32 ()* nocapture %func_ptr) #0 {
; CHECK-LABEL: func_ptr_test:
; CHECK:       ! %bb.0: ! %entry
; CHECK-NEXT:    jmp %o0
; CHECK-NEXT:    nop
entry:
  %call = tail call i32 %func_ptr() #1
  ret i32 %call
}

define i32 @func_ptr_test2(i32 (i32, i32, i32)* nocapture %func_ptr,
; CHECK-LABEL: func_ptr_test2:
; CHECK:       ! %bb.0: ! %entry
; CHECK-NEXT:    save %sp, -96, %sp
; CHECK-NEXT:    mov 10, %i3
; CHECK-NEXT:    mov %i0, %i4
; CHECK-NEXT:    mov %i1, %i0
; CHECK-NEXT:    jmp %i4
; CHECK-NEXT:    restore %g0, %i3, %o1
                           i32 %r, i32 %q) #1 {
entry:
  %call = tail call i32 %func_ptr(i32 %r, i32 10, i32 %q) #1
  ret i32 %call
}


; Do not tail call optimize if stack is used to pass parameters.

define i32 @caller_args() #0 {
; CHECK-LABEL: caller_args:
; CHECK:       ! %bb.0: ! %entry
; CHECK-NEXT:    save %sp, -104, %sp
; CHECK-NEXT:    mov 6, %i0
; CHECK-NEXT:    mov %g0, %o0
; CHECK-NEXT:    mov 1, %o1
; CHECK-NEXT:    mov 2, %o2
; CHECK-NEXT:    mov 3, %o3
; CHECK-NEXT:    mov 4, %o4
; CHECK-NEXT:    mov 5, %o5
; CHECK-NEXT:    call foo7
; CHECK-NEXT:    st %i0, [%sp+92]
; CHECK-NEXT:    ret
; CHECK-NEXT:    restore %g0, %o0, %o0
entry:
  %r = tail call i32 @foo7(i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6)
  ret i32 %r
}

; Byval parameters hand the function a pointer directly into the stack area
; we want to reuse during a tail call. Do not tail call optimize functions with
; byval parameters.

define i32 @caller_byval() #0 {
; CHECK-LABEL: caller_byval:
; CHECK:       ! %bb.0: ! %entry
; CHECK-NEXT:    save %sp, -104, %sp
; CHECK-NEXT:    ld [%fp+-4], %i0
; CHECK-NEXT:    st %i0, [%fp+-8]
; CHECK-NEXT:    call callee_byval
; CHECK-NEXT:    add %fp, -8, %o0
; CHECK-NEXT:    ret
; CHECK-NEXT:    restore %g0, %o0, %o0
entry:
  %a = alloca i32*
  %r = tail call i32 @callee_byval(i32** byval(i32*) %a)
  ret i32 %r
}

; Perform tail call optimization for sret function.

define void @sret_test(%struct.a* noalias sret(%struct.a) %agg.result) #0 {
; CHECK-LABEL: sret_test:
; CHECK:       ! %bb.0: ! %entry
; CHECK-NEXT:    mov %o7, %g1
; CHECK-NEXT:    call sret_func
; CHECK-NEXT:    mov %g1, %o7
entry:
  tail call void bitcast (void (%struct.a*)* @sret_func to
                          void (%struct.a*)*)(%struct.a* sret(%struct.a) %agg.result)
  ret void
}

; Do not tail call if either caller or callee returns
; a struct and the other does not. Returning a large
; struct will generate a memcpy as the tail function.

define void @ret_large_struct(%struct.big* noalias sret(%struct.big) %agg.result) #0 {
; CHECK-LABEL: ret_large_struct:
; CHECK:       ! %bb.0: ! %entry
; CHECK-NEXT:    save %sp, -96, %sp
; CHECK-NEXT:    ld [%fp+64], %i0
; CHECK-NEXT:    sethi %hi(bigstruct), %i1
; CHECK-NEXT:    add %i1, %lo(bigstruct), %o1
; CHECK-NEXT:    mov 400, %o2
; CHECK-NEXT:    call memcpy
; CHECK-NEXT:    mov %i0, %o0
; CHECK-NEXT:    jmp %i7+12
; CHECK-NEXT:    restore
entry:
  %0 = bitcast %struct.big* %agg.result to i8*
  tail call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 4 %0, i8* align 4 bitcast (%struct.big* @bigstruct to i8*), i32 400, i1 false)
  ret void
}

; Test register + immediate pattern.

define void @addri_test(i32 %ptr) #0 {
; CHECK-LABEL: addri_test:
; CHECK:       ! %bb.0: ! %entry
; CHECK-NEXT:    jmp %o0+4
; CHECK-NEXT:    nop
entry:
  %add = add nsw i32 %ptr, 4
  %0 = inttoptr i32 %add to void ()*
  tail call void %0() #1
  ret void
}

%struct.a = type { i32, i32 }
@dest = global [2 x i8] zeroinitializer

%struct.big = type { [100 x i32] }
@bigstruct = global %struct.big zeroinitializer

declare void @llvm.memcpy.p0i8.p0i8.i32(i8*, i8*, i32, i1)
declare void @sret_func(%struct.a* sret(%struct.a))
declare i32 @callee_byval(i32** byval(i32*) %a)
declare i32 @foo(i32)
declare i32 @foo2(i32, i32)
declare i32 @foo7(i32, i32, i32, i32, i32, i32, i32)

attributes #0 = { nounwind "disable-tail-calls"="false"
                  "frame-pointer"="none" }
attributes #1 = { nounwind "disable-tail-calls"="false"
                  "frame-pointer"="all" }