1 ; RUN: llc -mtriple=i686-unknown-linux-gnu -O1 < %s | FileCheck %s
2 ; RUN: llc -mtriple=i686-unknown-linux-gnu -O0 < %s | FileCheck %s
4 ; The MSVC family of x86 calling conventions makes tail calls really tricky.
5 ; Tests of all the various combinations should live here.
7 declare i32 @cdecl_i32()
8 declare void @cdecl_void()
10 ; Don't allow tail calling these cdecl functions, because we need to clear the
11 ; incoming stack arguments for these argument-clearing conventions.
13 define x86_thiscallcc void @thiscall_cdecl_notail(i32 %a, i32 %b, i32 %c) {
14 tail call void @cdecl_void()
17 ; CHECK-LABEL: thiscall_cdecl_notail
18 ; CHECK: calll cdecl_void
21 define x86_stdcallcc void @stdcall_cdecl_notail(i32 %a, i32 %b, i32 %c) {
22 tail call void @cdecl_void()
25 ; CHECK-LABEL: stdcall_cdecl_notail
26 ; CHECK: calll cdecl_void
29 define x86_vectorcallcc void @vectorcall_cdecl_notail(i32 inreg %a, i32 inreg %b, i32 %c) {
30 tail call void @cdecl_void()
33 ; CHECK-LABEL: vectorcall_cdecl_notail
34 ; CHECK: calll cdecl_void
37 define x86_fastcallcc void @fastcall_cdecl_notail(i32 inreg %a, i32 inreg %b, i32 %c) {
38 tail call void @cdecl_void()
41 ; CHECK-LABEL: fastcall_cdecl_notail
42 ; CHECK: calll cdecl_void
46 ; Tail call to/from callee pop functions can work under the right circumstances:
48 declare x86_thiscallcc void @no_args_method(ptr)
49 declare x86_thiscallcc void @one_arg_method(ptr, i32)
50 declare x86_thiscallcc void @two_args_method(ptr, i32, i32)
51 declare void @ccall_func()
52 declare void @ccall_func1(i32)
54 define x86_thiscallcc void @thiscall_thiscall_tail(ptr %this) {
56 tail call x86_thiscallcc void @no_args_method(ptr %this)
59 ; CHECK-LABEL: thiscall_thiscall_tail:
60 ; CHECK: jmp no_args_method
62 define x86_thiscallcc void @thiscall_thiscall_tail2(ptr %this, i32 %a, i32 %b) {
64 tail call x86_thiscallcc void @two_args_method(ptr %this, i32 %a, i32 %b)
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
71 define x86_thiscallcc void @thiscall_thiscall_notail(ptr %this, i32 %a, i32 %b, i32 %x) {
73 tail call x86_thiscallcc void @two_args_method(ptr %this, i32 %a, i32 %b)
76 ; @two_args_method would not pop %x.
77 ; CHECK-LABEL: thiscall_thiscall_notail:
78 ; CHECK: calll two_args_method
81 define x86_thiscallcc void @thiscall_thiscall_notail2(ptr %this, i32 %a) {
83 tail call x86_thiscallcc void @no_args_method(ptr %this)
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
92 define void @ccall_thiscall_tail(ptr %x) {
94 tail call x86_thiscallcc void @no_args_method(ptr %x)
97 ; Tail calling from ccall to thiscall works.
98 ; CHECK-LABEL: ccall_thiscall_tail:
99 ; CHECK: jmp no_args_method
101 define void @ccall_thiscall_notail(ptr %x, i32 %y) {
103 tail call x86_thiscallcc void @one_arg_method(ptr %x, i32 %y);
106 ; @one_arg_method would pop %y off the stack.
107 ; CHECK-LABEL: ccall_thiscall_notail:
108 ; CHECK: calll one_arg_method
110 define x86_thiscallcc void @thiscall_ccall_tail(ptr %this) {
112 tail call void @ccall_func()
115 ; Tail call from thiscall to ccall works if no arguments need popping.
116 ; CHECK-LABEL: thiscall_ccall_tail:
117 ; CHECK: jmp ccall_func
119 define x86_thiscallcc void @thiscall_ccall_notail(ptr %this, i32 %x) {
121 tail call void @ccall_func1(i32 %x)
124 ; No tail call: %x needs to be popped.
125 ; CHECK-LABEL: thiscall_ccall_notail:
126 ; CHECK: calll ccall_func1
130 define x86_thiscallcc void @tailcall_through_pointer(ptr %this, i32 %a) {
132 %vtable = load ptr, ptr %this
133 %0 = load ptr, ptr %vtable
134 tail call x86_thiscallcc void %0(ptr %this, i32 %a)
137 ; Tail calling works through function pointers too.
138 ; CHECK-LABEL: tailcall_through_pointer:
141 define x86_stdcallcc void @stdcall_cdecl_tail() {
142 tail call void @ccall_func()
145 ; stdcall to cdecl works if no arguments need popping.
146 ; CHECK-LABEL: stdcall_cdecl_tail
147 ; CHECK: jmp ccall_func
149 define x86_vectorcallcc void @vectorcall_cdecl_tail(i32 inreg %a, i32 inreg %b) {
150 tail call void @ccall_func()
153 ; vectorcall to cdecl works if no arguments need popping.
154 ; CHECK-LABEL: vectorcall_cdecl_tail
155 ; CHECK: jmp ccall_func
157 define x86_fastcallcc void @fastcall_cdecl_tail(i32 inreg %a, i32 inreg %b) {
158 tail call void @ccall_func()
161 ; fastcall to cdecl works if no arguments need popping.
162 ; CHECK-LABEL: fastcall_cdecl_tail
163 ; CHECK: jmp ccall_func
165 define x86_stdcallcc void @stdcall_thiscall_notail(ptr %this, i32 %a, i32 %b) {
166 tail call x86_thiscallcc void @two_args_method(ptr %this, i32 %a, i32 %b)
169 ; two_args_method will not pop %this.
170 ; CHECK-LABEL: stdcall_thiscall_notail
171 ; CHECK: calll two_args_method
173 define x86_stdcallcc void @stdcall_thiscall_tail(i32 %a, i32 %b) {
174 tail call x86_thiscallcc void @two_args_method(ptr null, i32 %a, i32 %b)
177 ; The callee pop amounts match up.
178 ; CHECK-LABEL: stdcall_thiscall_tail
179 ; CHECK: jmp two_args_method
181 declare x86_fastcallcc void @fastcall2(i32 inreg %a, i32 inreg %b)
182 define void @cdecl_fastcall_tail(i32 %a, i32 %b) {
183 tail call x86_fastcallcc void @fastcall2(i32 inreg %a, i32 inreg %b)
186 ; fastcall2 won't pop anything.
187 ; CHECK-LABEL: cdecl_fastcall_tail
188 ; CHECK: jmp fastcall2