xref: /aosp_15_r20/external/llvm/test/Transforms/Inline/inline-tail.ll (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker; RUN: opt < %s -inline -S | FileCheck %s
2*9880d681SAndroid Build Coastguard Worker
3*9880d681SAndroid Build Coastguard Worker; We have to apply the less restrictive TailCallKind of the call site being
4*9880d681SAndroid Build Coastguard Worker; inlined and any call sites cloned into the caller.
5*9880d681SAndroid Build Coastguard Worker
6*9880d681SAndroid Build Coastguard Worker; No tail marker after inlining, since test_capture_c captures an alloca.
7*9880d681SAndroid Build Coastguard Worker; CHECK: define void @test_capture_a(
8*9880d681SAndroid Build Coastguard Worker; CHECK-NOT: tail
9*9880d681SAndroid Build Coastguard Worker; CHECK: call void @test_capture_c(
10*9880d681SAndroid Build Coastguard Worker
11*9880d681SAndroid Build Coastguard Workerdeclare void @test_capture_c(i32*)
12*9880d681SAndroid Build Coastguard Workerdefine internal void @test_capture_b(i32* %P) {
13*9880d681SAndroid Build Coastguard Worker  tail call void @test_capture_c(i32* %P)
14*9880d681SAndroid Build Coastguard Worker  ret void
15*9880d681SAndroid Build Coastguard Worker}
16*9880d681SAndroid Build Coastguard Workerdefine void @test_capture_a() {
17*9880d681SAndroid Build Coastguard Worker  %A = alloca i32  		; captured by test_capture_b
18*9880d681SAndroid Build Coastguard Worker  call void @test_capture_b(i32* %A)
19*9880d681SAndroid Build Coastguard Worker  ret void
20*9880d681SAndroid Build Coastguard Worker}
21*9880d681SAndroid Build Coastguard Worker
22*9880d681SAndroid Build Coastguard Worker; No musttail marker after inlining, since the prototypes don't match.
23*9880d681SAndroid Build Coastguard Worker; CHECK: define void @test_proto_mismatch_a(
24*9880d681SAndroid Build Coastguard Worker; CHECK-NOT: musttail
25*9880d681SAndroid Build Coastguard Worker; CHECK: call void @test_proto_mismatch_c(
26*9880d681SAndroid Build Coastguard Worker
27*9880d681SAndroid Build Coastguard Workerdeclare void @test_proto_mismatch_c(i32*)
28*9880d681SAndroid Build Coastguard Workerdefine internal void @test_proto_mismatch_b(i32* %p) {
29*9880d681SAndroid Build Coastguard Worker  musttail call void @test_proto_mismatch_c(i32* %p)
30*9880d681SAndroid Build Coastguard Worker  ret void
31*9880d681SAndroid Build Coastguard Worker}
32*9880d681SAndroid Build Coastguard Workerdefine void @test_proto_mismatch_a() {
33*9880d681SAndroid Build Coastguard Worker  call void @test_proto_mismatch_b(i32* null)
34*9880d681SAndroid Build Coastguard Worker  ret void
35*9880d681SAndroid Build Coastguard Worker}
36*9880d681SAndroid Build Coastguard Worker
37*9880d681SAndroid Build Coastguard Worker; After inlining through a musttail call site, we need to keep musttail markers
38*9880d681SAndroid Build Coastguard Worker; to prevent unbounded stack growth.
39*9880d681SAndroid Build Coastguard Worker; CHECK: define void @test_musttail_basic_a(
40*9880d681SAndroid Build Coastguard Worker; CHECK: musttail call void @test_musttail_basic_c(
41*9880d681SAndroid Build Coastguard Worker
42*9880d681SAndroid Build Coastguard Workerdeclare void @test_musttail_basic_c(i32* %p)
43*9880d681SAndroid Build Coastguard Workerdefine internal void @test_musttail_basic_b(i32* %p) {
44*9880d681SAndroid Build Coastguard Worker  musttail call void @test_musttail_basic_c(i32* %p)
45*9880d681SAndroid Build Coastguard Worker  ret void
46*9880d681SAndroid Build Coastguard Worker}
47*9880d681SAndroid Build Coastguard Workerdefine void @test_musttail_basic_a(i32* %p) {
48*9880d681SAndroid Build Coastguard Worker  musttail call void @test_musttail_basic_b(i32* %p)
49*9880d681SAndroid Build Coastguard Worker  ret void
50*9880d681SAndroid Build Coastguard Worker}
51*9880d681SAndroid Build Coastguard Worker
52*9880d681SAndroid Build Coastguard Worker; Don't insert lifetime end markers here, the lifetime is trivially over due
53*9880d681SAndroid Build Coastguard Worker; the return.
54*9880d681SAndroid Build Coastguard Worker; CHECK: define void @test_byval_a(
55*9880d681SAndroid Build Coastguard Worker; CHECK: musttail call void @test_byval_c(
56*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret void
57*9880d681SAndroid Build Coastguard Worker
58*9880d681SAndroid Build Coastguard Workerdeclare void @test_byval_c(i32* byval %p)
59*9880d681SAndroid Build Coastguard Workerdefine internal void @test_byval_b(i32* byval %p) {
60*9880d681SAndroid Build Coastguard Worker  musttail call void @test_byval_c(i32* byval %p)
61*9880d681SAndroid Build Coastguard Worker  ret void
62*9880d681SAndroid Build Coastguard Worker}
63*9880d681SAndroid Build Coastguard Workerdefine void @test_byval_a(i32* byval %p) {
64*9880d681SAndroid Build Coastguard Worker  musttail call void @test_byval_b(i32* byval %p)
65*9880d681SAndroid Build Coastguard Worker  ret void
66*9880d681SAndroid Build Coastguard Worker}
67*9880d681SAndroid Build Coastguard Worker
68*9880d681SAndroid Build Coastguard Worker; Don't insert a stack restore, we're about to return.
69*9880d681SAndroid Build Coastguard Worker; CHECK: define void @test_dynalloca_a(
70*9880d681SAndroid Build Coastguard Worker; CHECK: call i8* @llvm.stacksave(
71*9880d681SAndroid Build Coastguard Worker; CHECK: alloca i8, i32 %n
72*9880d681SAndroid Build Coastguard Worker; CHECK: musttail call void @test_dynalloca_c(
73*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret void
74*9880d681SAndroid Build Coastguard Worker
75*9880d681SAndroid Build Coastguard Workerdeclare void @escape(i8* %buf)
76*9880d681SAndroid Build Coastguard Workerdeclare void @test_dynalloca_c(i32* byval %p, i32 %n)
77*9880d681SAndroid Build Coastguard Workerdefine internal void @test_dynalloca_b(i32* byval %p, i32 %n) alwaysinline {
78*9880d681SAndroid Build Coastguard Worker  %buf = alloca i8, i32 %n              ; dynamic alloca
79*9880d681SAndroid Build Coastguard Worker  call void @escape(i8* %buf)           ; escape it
80*9880d681SAndroid Build Coastguard Worker  musttail call void @test_dynalloca_c(i32* byval %p, i32 %n)
81*9880d681SAndroid Build Coastguard Worker  ret void
82*9880d681SAndroid Build Coastguard Worker}
83*9880d681SAndroid Build Coastguard Workerdefine void @test_dynalloca_a(i32* byval %p, i32 %n) {
84*9880d681SAndroid Build Coastguard Worker  musttail call void @test_dynalloca_b(i32* byval %p, i32 %n)
85*9880d681SAndroid Build Coastguard Worker  ret void
86*9880d681SAndroid Build Coastguard Worker}
87*9880d681SAndroid Build Coastguard Worker
88*9880d681SAndroid Build Coastguard Worker; We can't merge the returns.
89*9880d681SAndroid Build Coastguard Worker; CHECK: define void @test_multiret_a(
90*9880d681SAndroid Build Coastguard Worker; CHECK: musttail call void @test_multiret_c(
91*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret void
92*9880d681SAndroid Build Coastguard Worker; CHECK: musttail call void @test_multiret_d(
93*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret void
94*9880d681SAndroid Build Coastguard Worker
95*9880d681SAndroid Build Coastguard Workerdeclare void @test_multiret_c(i1 zeroext %b)
96*9880d681SAndroid Build Coastguard Workerdeclare void @test_multiret_d(i1 zeroext %b)
97*9880d681SAndroid Build Coastguard Workerdefine internal void @test_multiret_b(i1 zeroext %b) {
98*9880d681SAndroid Build Coastguard Worker  br i1 %b, label %c, label %d
99*9880d681SAndroid Build Coastguard Workerc:
100*9880d681SAndroid Build Coastguard Worker  musttail call void @test_multiret_c(i1 zeroext %b)
101*9880d681SAndroid Build Coastguard Worker  ret void
102*9880d681SAndroid Build Coastguard Workerd:
103*9880d681SAndroid Build Coastguard Worker  musttail call void @test_multiret_d(i1 zeroext %b)
104*9880d681SAndroid Build Coastguard Worker  ret void
105*9880d681SAndroid Build Coastguard Worker}
106*9880d681SAndroid Build Coastguard Workerdefine void @test_multiret_a(i1 zeroext %b) {
107*9880d681SAndroid Build Coastguard Worker  musttail call void @test_multiret_b(i1 zeroext %b)
108*9880d681SAndroid Build Coastguard Worker  ret void
109*9880d681SAndroid Build Coastguard Worker}
110*9880d681SAndroid Build Coastguard Worker
111*9880d681SAndroid Build Coastguard Worker; We have to avoid bitcast chains.
112*9880d681SAndroid Build Coastguard Worker; CHECK: define i32* @test_retptr_a(
113*9880d681SAndroid Build Coastguard Worker; CHECK: musttail call i8* @test_retptr_c(
114*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: bitcast i8* {{.*}} to i32*
115*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret i32*
116*9880d681SAndroid Build Coastguard Worker
117*9880d681SAndroid Build Coastguard Workerdeclare i8* @test_retptr_c()
118*9880d681SAndroid Build Coastguard Workerdefine internal i16* @test_retptr_b() {
119*9880d681SAndroid Build Coastguard Worker  %rv = musttail call i8* @test_retptr_c()
120*9880d681SAndroid Build Coastguard Worker  %v = bitcast i8* %rv to i16*
121*9880d681SAndroid Build Coastguard Worker  ret i16* %v
122*9880d681SAndroid Build Coastguard Worker}
123*9880d681SAndroid Build Coastguard Workerdefine i32* @test_retptr_a() {
124*9880d681SAndroid Build Coastguard Worker  %rv = musttail call i16* @test_retptr_b()
125*9880d681SAndroid Build Coastguard Worker  %v = bitcast i16* %rv to i32*
126*9880d681SAndroid Build Coastguard Worker  ret i32* %v
127*9880d681SAndroid Build Coastguard Worker}
128*9880d681SAndroid Build Coastguard Worker
129*9880d681SAndroid Build Coastguard Worker; Combine the last two cases: multiple returns with pointer bitcasts.
130*9880d681SAndroid Build Coastguard Worker; CHECK: define i32* @test_multiptrret_a(
131*9880d681SAndroid Build Coastguard Worker; CHECK: musttail call i8* @test_multiptrret_c(
132*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: bitcast i8* {{.*}} to i32*
133*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret i32*
134*9880d681SAndroid Build Coastguard Worker; CHECK: musttail call i8* @test_multiptrret_d(
135*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: bitcast i8* {{.*}} to i32*
136*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret i32*
137*9880d681SAndroid Build Coastguard Worker
138*9880d681SAndroid Build Coastguard Workerdeclare i8* @test_multiptrret_c(i1 zeroext %b)
139*9880d681SAndroid Build Coastguard Workerdeclare i8* @test_multiptrret_d(i1 zeroext %b)
140*9880d681SAndroid Build Coastguard Workerdefine internal i16* @test_multiptrret_b(i1 zeroext %b) {
141*9880d681SAndroid Build Coastguard Worker  br i1 %b, label %c, label %d
142*9880d681SAndroid Build Coastguard Workerc:
143*9880d681SAndroid Build Coastguard Worker  %c_rv = musttail call i8* @test_multiptrret_c(i1 zeroext %b)
144*9880d681SAndroid Build Coastguard Worker  %c_v = bitcast i8* %c_rv to i16*
145*9880d681SAndroid Build Coastguard Worker  ret i16* %c_v
146*9880d681SAndroid Build Coastguard Workerd:
147*9880d681SAndroid Build Coastguard Worker  %d_rv = musttail call i8* @test_multiptrret_d(i1 zeroext %b)
148*9880d681SAndroid Build Coastguard Worker  %d_v = bitcast i8* %d_rv to i16*
149*9880d681SAndroid Build Coastguard Worker  ret i16* %d_v
150*9880d681SAndroid Build Coastguard Worker}
151*9880d681SAndroid Build Coastguard Workerdefine i32* @test_multiptrret_a(i1 zeroext %b) {
152*9880d681SAndroid Build Coastguard Worker  %rv = musttail call i16* @test_multiptrret_b(i1 zeroext %b)
153*9880d681SAndroid Build Coastguard Worker  %v = bitcast i16* %rv to i32*
154*9880d681SAndroid Build Coastguard Worker  ret i32* %v
155*9880d681SAndroid Build Coastguard Worker}
156*9880d681SAndroid Build Coastguard Worker
157*9880d681SAndroid Build Coastguard Worker; Inline a musttail call site which contains a normal return and a musttail call.
158*9880d681SAndroid Build Coastguard Worker; CHECK: define i32 @test_mixedret_a(
159*9880d681SAndroid Build Coastguard Worker; CHECK: br i1 %b
160*9880d681SAndroid Build Coastguard Worker; CHECK: musttail call i32 @test_mixedret_c(
161*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret i32
162*9880d681SAndroid Build Coastguard Worker; CHECK: call i32 @test_mixedret_d(i1 zeroext %b)
163*9880d681SAndroid Build Coastguard Worker; CHECK: add i32 1,
164*9880d681SAndroid Build Coastguard Worker; CHECK-NOT: br
165*9880d681SAndroid Build Coastguard Worker; CHECK: ret i32
166*9880d681SAndroid Build Coastguard Worker
167*9880d681SAndroid Build Coastguard Workerdeclare i32 @test_mixedret_c(i1 zeroext %b)
168*9880d681SAndroid Build Coastguard Workerdeclare i32 @test_mixedret_d(i1 zeroext %b)
169*9880d681SAndroid Build Coastguard Workerdefine internal i32 @test_mixedret_b(i1 zeroext %b) {
170*9880d681SAndroid Build Coastguard Worker  br i1 %b, label %c, label %d
171*9880d681SAndroid Build Coastguard Workerc:
172*9880d681SAndroid Build Coastguard Worker  %c_rv = musttail call i32 @test_mixedret_c(i1 zeroext %b)
173*9880d681SAndroid Build Coastguard Worker  ret i32 %c_rv
174*9880d681SAndroid Build Coastguard Workerd:
175*9880d681SAndroid Build Coastguard Worker  %d_rv = call i32 @test_mixedret_d(i1 zeroext %b)
176*9880d681SAndroid Build Coastguard Worker  %d_rv1 = add i32 1, %d_rv
177*9880d681SAndroid Build Coastguard Worker  ret i32 %d_rv1
178*9880d681SAndroid Build Coastguard Worker}
179*9880d681SAndroid Build Coastguard Workerdefine i32 @test_mixedret_a(i1 zeroext %b) {
180*9880d681SAndroid Build Coastguard Worker  %rv = musttail call i32 @test_mixedret_b(i1 zeroext %b)
181*9880d681SAndroid Build Coastguard Worker  ret i32 %rv
182*9880d681SAndroid Build Coastguard Worker}
183