1 ; RUN: opt < %s -passes=tailcallelim -verify-dom-info -S | FileCheck %s
5 declare void @use_nocapture(ptr nocapture)
6 declare void @use2_nocapture(ptr nocapture, ptr nocapture)
8 ; Trivial case. Mark @noarg with tail call.
10 ; CHECK: tail call void @noarg()
15 ; Make sure that we do not do TRE if pointer to local stack
16 ; escapes through function call.
20 %A = alloca i32 ; <ptr> [#uses=2]
22 call void @use(ptr %A)
23 ; CHECK: call i32 @test1
24 %X = call i32 @test1() ; <i32> [#uses=1]
28 ; This function contains intervening instructions which should be moved out of the way
29 define i32 @test2(i32 %X) {
34 %tmp.1 = icmp eq i32 %X, 0 ; <i1> [#uses=1]
35 br i1 %tmp.1, label %then.0, label %endif.0
36 then.0: ; preds = %entry
37 %tmp.4 = add i32 %X, 1 ; <i32> [#uses=1]
39 endif.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]
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.
48 define i32 @test3(i32 %c) {
51 ; CHECK: %ret.tr = phi i32 [ poison, %entry ], [ %current.ret.tr, %else ]
52 ; CHECK: %ret.known.tr = phi i1 [ false, %entry ], [ true, %else ]
55 ; CHECK: %current.ret.tr = select i1 %ret.known.tr, i32 %ret.tr, i32 0
58 ; CHECK: %current.ret.tr1 = select i1 %ret.known.tr, i32 %ret.tr, i32 0
59 ; CHECK: ret i32 %current.ret.tr1
61 %tmp.1 = icmp eq i32 %c, 0 ; <i1> [#uses=1]
62 br i1 %tmp.1, label %return, label %else
63 else: ; preds = %entry
64 %tmp.5 = add i32 %c, -1 ; <i32> [#uses=1]
65 %tmp.3 = call i32 @test3(i32 %tmp.5) ; <i32> [#uses=0]
67 return: ; preds = %entry
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
76 define void @test4() {
78 ; CHECK-NOT: tail call void @use_nocapture
79 ; CHECK: tail call void @noarg()
82 call void @use_nocapture(ptr %a)
87 ; Make sure that we do not perform TRE even with a nocapture use. This is due to
88 ; bad codegen caused by PR962.
91 define ptr @test5(ptr nocapture %A, i1 %cond) {
93 ; CHECK-NOT: tailrecurse:
96 br i1 %cond, label %cond_true, label %cond_false
98 call ptr @test5(ptr %B, i1 false)
101 call void @use2_nocapture(ptr %A, ptr %B)
106 ; PR14143: Make sure that we do not mark functions with nocapture allocas with tail.
109 define void @test6(ptr %a, ptr %b) {
110 ; CHECK-LABEL: @test6(
111 ; CHECK-NOT: tail call
113 %c = alloca [100 x i8], align 16
114 call void @use2_nocapture(ptr %b, ptr %c)
118 ; PR14143: Make sure that we do not mark functions with nocapture allocas with tail.
121 define void @test7(ptr %a, ptr %b) nounwind uwtable {
123 ; CHECK-LABEL: @test7(
124 ; CHECK-NOT: tail call
126 %c = alloca [100 x i8], align 16
127 call void @use2_nocapture(ptr %c, ptr %a)
128 call void @use2_nocapture(ptr %b, ptr %c)
132 ; If we have a mix of escaping captured/non-captured allocas, ensure that we do
133 ; not do anything including marking callsites with the tail call marker.
136 define ptr @test8(ptr nocapture %A, i1 %cond) {
138 ; CHECK-NOT: tailrecurse:
139 ; CHECK-NOT: tail call
140 ; CHECK: ret ptr null
143 br i1 %cond, label %cond_true, label %cond_false
145 call void @use(ptr %B2)
146 call ptr @test8(ptr %B, i1 false)
149 call void @use2_nocapture(ptr %A, ptr %B)
154 ; Don't tail call if a byval arg is captured.
155 define void @test9(ptr byval(i32) %a) {
156 ; CHECK-LABEL: define void @test9(
157 ; CHECK: {{^ *}}call void @use(
158 call void @use(ptr %a)
162 %struct.X = type { ptr }
164 declare void @ctor(ptr)
165 define void @test10(ptr noalias sret(%struct.X) %agg.result, i1 zeroext %b) {
166 ; CHECK-LABEL: @test10
168 %x = alloca %struct.X, align 8
169 br i1 %b, label %if.then, label %if.end
171 if.then: ; preds = %entry
172 call void @ctor(ptr %agg.result)
173 ; CHECK: tail call void @ctor
177 call void @ctor(ptr %x)
178 ; CHECK: call void @ctor
185 declare void @test11_helper1(ptr nocapture, ptr)
186 declare void @test11_helper2(ptr)
187 define void @test11() {
188 ; CHECK-LABEL: @test11
192 call void @test11_helper1(ptr %a, ptr %b) ; a = &b
193 %c = load ptr, ptr %a
194 call void @test11_helper2(ptr %c)
195 ; CHECK: call void @test11_helper2
200 define void @test12() {
202 ; CHECK-LABEL: @test12
203 ; CHECK: {{^ *}} call void undef(ptr undef) [ "foo"(ptr %e) ]
205 call void undef(ptr undef) [ "foo"(ptr %e) ]
209 %struct.foo = type { [10 x i32] }
211 ; If an alloca is passed byval it is not a use of the alloca or an escape
212 ; point, and both calls below can be marked tail.
213 define void @test13() {
214 ; CHECK-LABEL: @test13
215 ; CHECK: tail call void @bar(ptr byval(%struct.foo) %f)
216 ; CHECK: tail call void @bar(ptr byval(%struct.foo) null)
218 %f = alloca %struct.foo
219 call void @bar(ptr byval(%struct.foo) %f)
220 call void @bar(ptr byval(%struct.foo) null)
224 ; A call which passes a byval parameter using byval can be marked tail.
225 define void @test14(ptr byval(%struct.foo) %f) {
226 ; CHECK-LABEL: @test14
227 ; CHECK: tail call void @bar
229 call void @bar(ptr byval(%struct.foo) %f)
233 ; If a byval parameter is copied into an alloca and passed byval the call can
235 define void @test15(ptr byval(%struct.foo) %f) {
236 ; CHECK-LABEL: @test15
237 ; CHECK: tail call void @bar
239 %agg.tmp = alloca %struct.foo
240 call void @llvm.memcpy.p0.p0.i64(ptr %agg.tmp, ptr %f, i64 40, i1 false)
241 call void @bar(ptr byval(%struct.foo) %agg.tmp)
245 declare void @bar(ptr byval(%struct.foo))
246 declare void @llvm.memcpy.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1)