Transforms: reapply SVN r219899
[oota-llvm.git] / test / Transforms / Inline / inline-tail.ll
1 ; RUN: opt < %s -inline -S | FileCheck %s
2
3 ; We have to apply the less restrictive TailCallKind of the call site being
4 ; inlined and any call sites cloned into the caller.
5
6 ; No tail marker after inlining, since test_capture_c captures an alloca.
7 ; CHECK: define void @test_capture_a(
8 ; CHECK-NOT: tail
9 ; CHECK: call void @test_capture_c(
10
11 declare void @test_capture_c(i32*)
12 define internal void @test_capture_b(i32* %P) {
13   tail call void @test_capture_c(i32* %P)
14   ret void
15 }
16 define void @test_capture_a() {
17   %A = alloca i32               ; captured by test_capture_b
18   call void @test_capture_b(i32* %A)
19   ret void
20 }
21
22 ; No musttail marker after inlining, since the prototypes don't match.
23 ; CHECK: define void @test_proto_mismatch_a(
24 ; CHECK-NOT: musttail
25 ; CHECK: call void @test_proto_mismatch_c(
26
27 declare void @test_proto_mismatch_c(i32*)
28 define internal void @test_proto_mismatch_b(i32* %p) {
29   musttail call void @test_proto_mismatch_c(i32* %p)
30   ret void
31 }
32 define void @test_proto_mismatch_a() {
33   call void @test_proto_mismatch_b(i32* null)
34   ret void
35 }
36
37 ; After inlining through a musttail call site, we need to keep musttail markers
38 ; to prevent unbounded stack growth.
39 ; CHECK: define void @test_musttail_basic_a(
40 ; CHECK: musttail call void @test_musttail_basic_c(
41
42 declare void @test_musttail_basic_c(i32* %p)
43 define internal void @test_musttail_basic_b(i32* %p) {
44   musttail call void @test_musttail_basic_c(i32* %p)
45   ret void
46 }
47 define void @test_musttail_basic_a(i32* %p) {
48   musttail call void @test_musttail_basic_b(i32* %p)
49   ret void
50 }
51
52 ; We can't merge the returns.
53 ; CHECK: define void @test_multiret_a(
54 ; CHECK: musttail call void @test_multiret_c(
55 ; CHECK-NEXT: ret void
56 ; CHECK: musttail call void @test_multiret_d(
57 ; CHECK-NEXT: ret void
58
59 declare void @test_multiret_c(i1 zeroext %b)
60 declare void @test_multiret_d(i1 zeroext %b)
61 define internal void @test_multiret_b(i1 zeroext %b) {
62   br i1 %b, label %c, label %d
63 c:
64   musttail call void @test_multiret_c(i1 zeroext %b)
65   ret void
66 d:
67   musttail call void @test_multiret_d(i1 zeroext %b)
68   ret void
69 }
70 define void @test_multiret_a(i1 zeroext %b) {
71   musttail call void @test_multiret_b(i1 zeroext %b)
72   ret void
73 }
74
75 ; We have to avoid bitcast chains.
76 ; CHECK: define i32* @test_retptr_a(
77 ; CHECK: musttail call i8* @test_retptr_c(
78 ; CHECK-NEXT: bitcast i8* {{.*}} to i32*
79 ; CHECK-NEXT: ret i32*
80
81 declare i8* @test_retptr_c()
82 define internal i16* @test_retptr_b() {
83   %rv = musttail call i8* @test_retptr_c()
84   %v = bitcast i8* %rv to i16*
85   ret i16* %v
86 }
87 define i32* @test_retptr_a() {
88   %rv = musttail call i16* @test_retptr_b()
89   %v = bitcast i16* %rv to i32*
90   ret i32* %v
91 }
92
93 ; Combine the last two cases: multiple returns with pointer bitcasts.
94 ; CHECK: define i32* @test_multiptrret_a(
95 ; CHECK: musttail call i8* @test_multiptrret_c(
96 ; CHECK-NEXT: bitcast i8* {{.*}} to i32*
97 ; CHECK-NEXT: ret i32*
98 ; CHECK: musttail call i8* @test_multiptrret_d(
99 ; CHECK-NEXT: bitcast i8* {{.*}} to i32*
100 ; CHECK-NEXT: ret i32*
101
102 declare i8* @test_multiptrret_c(i1 zeroext %b)
103 declare i8* @test_multiptrret_d(i1 zeroext %b)
104 define internal i16* @test_multiptrret_b(i1 zeroext %b) {
105   br i1 %b, label %c, label %d
106 c:
107   %c_rv = musttail call i8* @test_multiptrret_c(i1 zeroext %b)
108   %c_v = bitcast i8* %c_rv to i16*
109   ret i16* %c_v
110 d:
111   %d_rv = musttail call i8* @test_multiptrret_d(i1 zeroext %b)
112   %d_v = bitcast i8* %d_rv to i16*
113   ret i16* %d_v
114 }
115 define i32* @test_multiptrret_a(i1 zeroext %b) {
116   %rv = musttail call i16* @test_multiptrret_b(i1 zeroext %b)
117   %v = bitcast i16* %rv to i32*
118   ret i32* %v
119 }
120
121 ; Inline a musttail call site which contains a normal return and a musttail call.
122 ; CHECK: define i32 @test_mixedret_a(
123 ; CHECK: br i1 %b
124 ; CHECK: musttail call i32 @test_mixedret_c(
125 ; CHECK-NEXT: ret i32
126 ; CHECK: call i32 @test_mixedret_d(i1 zeroext %b)
127 ; CHECK: add i32 1,
128 ; CHECK-NOT: br
129 ; CHECK: ret i32
130
131 declare i32 @test_mixedret_c(i1 zeroext %b)
132 declare i32 @test_mixedret_d(i1 zeroext %b)
133 define internal i32 @test_mixedret_b(i1 zeroext %b) {
134   br i1 %b, label %c, label %d
135 c:
136   %c_rv = musttail call i32 @test_mixedret_c(i1 zeroext %b)
137   ret i32 %c_rv
138 d:
139   %d_rv = call i32 @test_mixedret_d(i1 zeroext %b)
140   %d_rv1 = add i32 1, %d_rv
141   ret i32 %d_rv1
142 }
143 define i32 @test_mixedret_a(i1 zeroext %b) {
144   %rv = musttail call i32 @test_mixedret_b(i1 zeroext %b)
145   ret i32 %rv
146 }