blob: 98b02c9c07e82218bfb62f783f25f01a36ac7efd [file] [log] [blame]
Hans Wennborg34d40432015-10-24 16:47:10 +00001; RUN: llc -mtriple=i686-unknown-linux-gnu -O1 < %s | FileCheck %s
2; RUN: llc -mtriple=i686-unknown-linux-gnu -O0 < %s | FileCheck %s
Reid Klecknerf02e33c2015-10-23 19:35:38 +00003
4; The MSVC family of x86 calling conventions makes tail calls really tricky.
5; Tests of all the various combinations should live here.
6
7declare i32 @cdecl_i32()
8declare void @cdecl_void()
9
10; Don't allow tail calling these cdecl functions, because we need to clear the
11; incoming stack arguments for these argument-clearing conventions.
12
13define x86_thiscallcc void @thiscall_cdecl_notail(i32 %a, i32 %b, i32 %c) {
14 tail call void @cdecl_void()
15 ret void
16}
Hans Wennborg34d40432015-10-24 16:47:10 +000017; CHECK-LABEL: thiscall_cdecl_notail
18; CHECK: calll cdecl_void
Reid Klecknerf02e33c2015-10-23 19:35:38 +000019; CHECK: retl $8
Hans Wennborg34d40432015-10-24 16:47:10 +000020
Reid Klecknerf02e33c2015-10-23 19:35:38 +000021define x86_stdcallcc void @stdcall_cdecl_notail(i32 %a, i32 %b, i32 %c) {
22 tail call void @cdecl_void()
23 ret void
24}
Hans Wennborg34d40432015-10-24 16:47:10 +000025; CHECK-LABEL: stdcall_cdecl_notail
26; CHECK: calll cdecl_void
Reid Klecknerf02e33c2015-10-23 19:35:38 +000027; CHECK: retl $12
Hans Wennborg34d40432015-10-24 16:47:10 +000028
Reid Klecknerf02e33c2015-10-23 19:35:38 +000029define x86_vectorcallcc void @vectorcall_cdecl_notail(i32 inreg %a, i32 inreg %b, i32 %c) {
30 tail call void @cdecl_void()
31 ret void
32}
Hans Wennborg34d40432015-10-24 16:47:10 +000033; CHECK-LABEL: vectorcall_cdecl_notail
34; CHECK: calll cdecl_void
Reid Klecknerf02e33c2015-10-23 19:35:38 +000035; CHECK: retl $4
Hans Wennborg34d40432015-10-24 16:47:10 +000036
Reid Klecknerf02e33c2015-10-23 19:35:38 +000037define x86_fastcallcc void @fastcall_cdecl_notail(i32 inreg %a, i32 inreg %b, i32 %c) {
38 tail call void @cdecl_void()
39 ret void
40}
Hans Wennborg34d40432015-10-24 16:47:10 +000041; CHECK-LABEL: fastcall_cdecl_notail
42; CHECK: calll cdecl_void
Reid Klecknerf02e33c2015-10-23 19:35:38 +000043; CHECK: retl $4
Hans Wennborg34d40432015-10-24 16:47:10 +000044
45
46; Tail call to/from callee pop functions can work under the right circumstances:
47
48declare x86_thiscallcc void @no_args_method(i8*)
49declare x86_thiscallcc void @one_arg_method(i8*, i32)
50declare x86_thiscallcc void @two_args_method(i8*, i32, i32)
51declare void @ccall_func()
52declare void @ccall_func1(i32)
53
54define x86_thiscallcc void @thiscall_thiscall_tail(i8* %this) {
55entry:
56 tail call x86_thiscallcc void @no_args_method(i8* %this)
57 ret void
58}
59; CHECK-LABEL: thiscall_thiscall_tail:
60; CHECK: jmp no_args_method
61
62define x86_thiscallcc void @thiscall_thiscall_tail2(i8* %this, i32 %a, i32 %b) {
63entry:
64 tail call x86_thiscallcc void @two_args_method(i8* %this, i32 %a, i32 %b)
65 ret void
66}
67; @two_args_method will take care of popping %a and %b from the stack for us.
68; CHECK-LABEL: thiscall_thiscall_tail2:
69; CHECK: jmp two_args_method
70
71define x86_thiscallcc void @thiscall_thiscall_notail(i8* %this, i32 %a, i32 %b, i32 %x) {
72entry:
73 tail call x86_thiscallcc void @two_args_method(i8* %this, i32 %a, i32 %b)
74 ret void
75}
76; @two_args_method would not pop %x.
77; CHECK-LABEL: thiscall_thiscall_notail:
78; CHECK: calll two_args_method
79; CHECK: retl $12
80
81define x86_thiscallcc void @thiscall_thiscall_notail2(i8* %this, i32 %a) {
82entry:
83 tail call x86_thiscallcc void @no_args_method(i8* %this)
84 ret void
85}
86; @no_args_method would not pop %x for us. Make sure this is checked even
87; when there are no arguments to the call.
88; CHECK-LABEL: thiscall_thiscall_notail2:
89; CHECK: calll no_args_method
90; CHECK: retl $4
91
92define void @ccall_thiscall_tail(i8* %x) {
93entry:
94 tail call x86_thiscallcc void @no_args_method(i8* %x)
95 ret void
96}
97; Tail calling from ccall to thiscall works.
98; CHECK-LABEL: ccall_thiscall_tail:
99; CHECK: jmp no_args_method
100
101define void @ccall_thiscall_notail(i8* %x, i32 %y) {
102entry:
103 tail call x86_thiscallcc void @one_arg_method(i8* %x, i32 %y);
104 ret void
105}
106; @one_arg_method would pop %y off the stack.
107; CHECK-LABEL: ccall_thiscall_notail:
108; CHECK: calll one_arg_method
109
110define x86_thiscallcc void @thiscall_ccall_tail(i8* %this) {
111entry:
112 tail call void @ccall_func()
113 ret void
114}
115; Tail call from thiscall to ccall works if no arguments need popping.
116; CHECK-LABEL: thiscall_ccall_tail:
117; CHECK: jmp ccall_func
118
119define x86_thiscallcc void @thiscall_ccall_notail(i8* %this, i32 %x) {
120entry:
121 tail call void @ccall_func1(i32 %x)
122 ret void
123}
124; No tail call: %x needs to be popped.
125; CHECK-LABEL: thiscall_ccall_notail:
126; CHECK: calll ccall_func1
127; CHECK: retl $4
128
129%S = type { i32 (...)** }
130define x86_thiscallcc void @tailcall_through_pointer(%S* %this, i32 %a) {
131entry:
132 %0 = bitcast %S* %this to void (%S*, i32)***
133 %vtable = load void (%S*, i32)**, void (%S*, i32)*** %0
134 %1 = load void (%S*, i32)*, void (%S*, i32)** %vtable
135 tail call x86_thiscallcc void %1(%S* %this, i32 %a)
136 ret void
137}
138; Tail calling works through function pointers too.
139; CHECK-LABEL: tailcall_through_pointer:
140; CHECK: jmpl
141
142define x86_stdcallcc void @stdcall_cdecl_tail() {
143 tail call void @ccall_func()
144 ret void
145}
146; stdcall to cdecl works if no arguments need popping.
147; CHECK-LABEL: stdcall_cdecl_tail
148; CHECK: jmp ccall_func
149
150define x86_vectorcallcc void @vectorcall_cdecl_tail(i32 inreg %a, i32 inreg %b) {
151 tail call void @ccall_func()
152 ret void
153}
154; vectorcall to cdecl works if no arguments need popping.
155; CHECK-LABEL: vectorcall_cdecl_tail
156; CHECK: jmp ccall_func
157
158define x86_fastcallcc void @fastcall_cdecl_tail(i32 inreg %a, i32 inreg %b) {
159 tail call void @ccall_func()
160 ret void
161}
162; fastcall to cdecl works if no arguments need popping.
163; CHECK-LABEL: fastcall_cdecl_tail
164; CHECK: jmp ccall_func
165
166define x86_stdcallcc void @stdcall_thiscall_notail(i8* %this, i32 %a, i32 %b) {
167 tail call x86_thiscallcc void @two_args_method(i8* %this, i32 %a, i32 %b)
168 ret void
169}
170; two_args_method will not pop %this.
171; CHECK-LABEL: stdcall_thiscall_notail
172; CHECK: calll two_args_method
173
174define x86_stdcallcc void @stdcall_thiscall_tail(i32 %a, i32 %b) {
175 tail call x86_thiscallcc void @two_args_method(i8* null, i32 %a, i32 %b)
176 ret void
177}
178; The callee pop amounts match up.
179; CHECK-LABEL: stdcall_thiscall_tail
180; CHECK: jmp two_args_method
181
182declare x86_fastcallcc void @fastcall2(i32 inreg %a, i32 inreg %b)
183define void @cdecl_fastcall_tail(i32 %a, i32 %b) {
184 tail call x86_fastcallcc void @fastcall2(i32 %a, i32 %b)
185 ret void
186}
187; fastcall2 won't pop anything.
188; CHECK-LABEL: cdecl_fastcall_tail
189; CHECK: jmp fastcall2