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