1; RUN: opt < %s -passes=tailcallelim -verify-dom-info -S | FileCheck %s
2
3declare void @noarg()
4declare void @use(i32*)
5declare void @use_nocapture(i32* nocapture)
6declare void @use2_nocapture(i32* nocapture, i32* nocapture)
7
8; Trivial case. Mark @noarg with tail call.
9define void @test0() {
10; CHECK: tail call void @noarg()
11	call void @noarg()
12	ret void
13}
14
15; Make sure that we do not do TRE if pointer to local stack
16; escapes through function call.
17define i32 @test1() {
18; CHECK: i32 @test1()
19; CHECK-NEXT: alloca
20	%A = alloca i32		; <i32*> [#uses=2]
21	store i32 5, i32* %A
22	call void @use(i32* %A)
23; CHECK: call i32 @test1
24	%X = call i32 @test1()		; <i32> [#uses=1]
25	ret i32 %X
26}
27
28; This function contains intervening instructions which should be moved out of the way
29define i32 @test2(i32 %X) {
30; CHECK: i32 @test2
31; CHECK-NOT: call
32; CHECK: ret i32
33entry:
34	%tmp.1 = icmp eq i32 %X, 0		; <i1> [#uses=1]
35	br i1 %tmp.1, label %then.0, label %endif.0
36then.0:		; preds = %entry
37	%tmp.4 = add i32 %X, 1		; <i32> [#uses=1]
38	ret i32 %tmp.4
39endif.0:		; preds = %entry
40	%tmp.10 = add i32 %X, -1		; <i32> [#uses=1]
41	%tmp.8 = call i32 @test2(i32 %tmp.10)		; <i32> [#uses=1]
42	%DUMMY = add i32 %X, 1		; <i32> [#uses=0]
43	ret i32 %tmp.8
44}
45
46; Though this case seems to be fairly unlikely to occur in the wild, someone
47; plunked it into the demo script, so maybe they care about it.
48define i32 @test3(i32 %c) {
49; CHECK: i32 @test3
50; CHECK: tailrecurse:
51; CHECK: %ret.tr = phi i32 [ poison, %entry ], [ %current.ret.tr, %else ]
52; CHECK: %ret.known.tr = phi i1 [ false, %entry ], [ true, %else ]
53; CHECK: else:
54; CHECK-NOT: call
55; CHECK: %current.ret.tr = select i1 %ret.known.tr, i32 %ret.tr, i32 0
56; CHECK-NOT: ret
57; CHECK: return:
58; CHECK: %current.ret.tr1 = select i1 %ret.known.tr, i32 %ret.tr, i32 0
59; CHECK: ret i32 %current.ret.tr1
60entry:
61	%tmp.1 = icmp eq i32 %c, 0		; <i1> [#uses=1]
62	br i1 %tmp.1, label %return, label %else
63else:		; preds = %entry
64	%tmp.5 = add i32 %c, -1		; <i32> [#uses=1]
65	%tmp.3 = call i32 @test3(i32 %tmp.5)		; <i32> [#uses=0]
66	ret i32 0
67return:		; preds = %entry
68	ret i32 0
69}
70
71; Make sure that a nocapture pointer does not stop adding a tail call marker to
72; an unrelated call and additionally that we do not mark the nocapture call with
73; a tail call.
74;
75; rdar://14324281
76define void @test4() {
77; CHECK: void @test4
78; CHECK-NOT: tail call void @use_nocapture
79; CHECK: tail call void @noarg()
80; CHECK: ret void
81  %a = alloca i32
82  call void @use_nocapture(i32* %a)
83  call void @noarg()
84  ret void
85}
86
87; Make sure that we do not perform TRE even with a nocapture use. This is due to
88; bad codegen caused by PR962.
89;
90; rdar://14324281.
91define i32* @test5(i32* nocapture %A, i1 %cond) {
92; CHECK: i32* @test5
93; CHECK-NOT: tailrecurse:
94; CHECK: ret i32* null
95  %B = alloca i32
96  br i1 %cond, label %cond_true, label %cond_false
97cond_true:
98  call i32* @test5(i32* %B, i1 false)
99  ret i32* null
100cond_false:
101  call void @use2_nocapture(i32* %A, i32* %B)
102  call void @noarg()
103  ret i32* null
104}
105
106; PR14143: Make sure that we do not mark functions with nocapture allocas with tail.
107;
108; rdar://14324281.
109define void @test6(i32* %a, i32* %b) {
110; CHECK-LABEL: @test6(
111; CHECK-NOT: tail call
112; CHECK: ret void
113  %c = alloca [100 x i8], align 16
114  %tmp = bitcast [100 x i8]* %c to i32*
115  call void @use2_nocapture(i32* %b, i32* %tmp)
116  ret void
117}
118
119; PR14143: Make sure that we do not mark functions with nocapture allocas with tail.
120;
121; rdar://14324281
122define void @test7(i32* %a, i32* %b) nounwind uwtable {
123entry:
124; CHECK-LABEL: @test7(
125; CHECK-NOT: tail call
126; CHECK: ret void
127  %c = alloca [100 x i8], align 16
128  %0 = bitcast [100 x i8]* %c to i32*
129  call void @use2_nocapture(i32* %0, i32* %a)
130  call void @use2_nocapture(i32* %b, i32* %0)
131  ret void
132}
133
134; If we have a mix of escaping captured/non-captured allocas, ensure that we do
135; not do anything including marking callsites with the tail call marker.
136;
137; rdar://14324281.
138define i32* @test8(i32* nocapture %A, i1 %cond) {
139; CHECK: i32* @test8
140; CHECK-NOT: tailrecurse:
141; CHECK-NOT: tail call
142; CHECK: ret i32* null
143  %B = alloca i32
144  %B2 = alloca i32
145  br i1 %cond, label %cond_true, label %cond_false
146cond_true:
147  call void @use(i32* %B2)
148  call i32* @test8(i32* %B, i1 false)
149  ret i32* null
150cond_false:
151  call void @use2_nocapture(i32* %A, i32* %B)
152  call void @noarg()
153  ret i32* null
154}
155
156; Don't tail call if a byval arg is captured.
157define void @test9(i32* byval(i32) %a) {
158; CHECK-LABEL: define void @test9(
159; CHECK: {{^ *}}call void @use(
160  call void @use(i32* %a)
161  ret void
162}
163
164%struct.X = type { i8* }
165
166declare void @ctor(%struct.X*)
167define void @test10(%struct.X* noalias sret(%struct.X) %agg.result, i1 zeroext %b) {
168; CHECK-LABEL: @test10
169entry:
170  %x = alloca %struct.X, align 8
171  br i1 %b, label %if.then, label %if.end
172
173if.then:                                          ; preds = %entry
174  call void @ctor(%struct.X* %agg.result)
175; CHECK: tail call void @ctor
176  br label %return
177
178if.end:
179  call void @ctor(%struct.X* %x)
180; CHECK: call void @ctor
181  br label %return
182
183return:
184  ret void
185}
186
187declare void @test11_helper1(i8** nocapture, i8*)
188declare void @test11_helper2(i8*)
189define void @test11() {
190; CHECK-LABEL: @test11
191; CHECK-NOT: tail
192  %a = alloca i8*
193  %b = alloca i8
194  call void @test11_helper1(i8** %a, i8* %b)  ; a = &b
195  %c = load i8*, i8** %a
196  call void @test11_helper2(i8* %c)
197; CHECK: call void @test11_helper2
198  ret void
199}
200
201; PR25928
202define void @test12() {
203entry:
204; CHECK-LABEL: @test12
205; CHECK: {{^ *}} call void undef(i8* undef) [ "foo"(i8* %e) ]
206  %e = alloca i8
207  call void undef(i8* undef) [ "foo"(i8* %e) ]
208  unreachable
209}
210
211%struct.foo = type { [10 x i32] }
212
213; If an alloca is passed byval it is not a use of the alloca or an escape
214; point, and both calls below can be marked tail.
215define void @test13() {
216; CHECK-LABEL: @test13
217; CHECK: tail call void @bar(%struct.foo* byval(%struct.foo) %f)
218; CHECK: tail call void @bar(%struct.foo* byval(%struct.foo) null)
219entry:
220  %f = alloca %struct.foo
221  call void @bar(%struct.foo* byval(%struct.foo) %f)
222  call void @bar(%struct.foo* byval(%struct.foo) null)
223  ret void
224}
225
226; A call which passes a byval parameter using byval can be marked tail.
227define void @test14(%struct.foo* byval(%struct.foo) %f) {
228; CHECK-LABEL: @test14
229; CHECK: tail call void @bar
230entry:
231  call void @bar(%struct.foo* byval(%struct.foo) %f)
232  ret void
233}
234
235; If a byval parameter is copied into an alloca and passed byval the call can
236; be marked tail.
237define void @test15(%struct.foo* byval(%struct.foo) %f) {
238; CHECK-LABEL: @test15
239; CHECK: tail call void @bar
240entry:
241  %agg.tmp = alloca %struct.foo
242  %0 = bitcast %struct.foo* %agg.tmp to i8*
243  %1 = bitcast %struct.foo* %f to i8*
244  call void @llvm.memcpy.p0i8.p0i8.i64(i8* %0, i8* %1, i64 40, i1 false)
245  call void @bar(%struct.foo* byval(%struct.foo) %agg.tmp)
246  ret void
247}
248
249declare void @bar(%struct.foo* byval(%struct.foo))
250declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture writeonly, i8* nocapture readonly, i64, i1)
251