d8ecd44d1192d8ba74ada600ccb4f1396e567cbd
[oota-llvm.git] / test / CodeGen / X86 / win32_sret.ll
1 ; We specify -mcpu explicitly to avoid instruction reordering that happens on
2 ; some setups (e.g., Atom) from affecting the output.
3 ; RUN: llc < %s -mcpu=core2 -mtriple=i686-pc-win32 | FileCheck %s -check-prefix=WIN32
4 ; RUN: llc < %s -mcpu=core2 -mtriple=i686-pc-mingw32 | FileCheck %s -check-prefix=MINGW_X86
5 ; RUN: llc < %s -mcpu=core2 -mtriple=i386-pc-linux | FileCheck %s -check-prefix=LINUX
6 ; RUN: llc < %s -mcpu=core2 -O0 -mtriple=i686-pc-win32 | FileCheck %s -check-prefix=WIN32
7 ; RUN: llc < %s -mcpu=core2 -O0 -mtriple=i686-pc-mingw32 | FileCheck %s -check-prefix=MINGW_X86
8 ; RUN: llc < %s -mcpu=core2 -O0 -mtriple=i386-pc-linux | FileCheck %s -check-prefix=LINUX
9
10 ; The SysV ABI used by most Unixes and Mingw on x86 specifies that an sret pointer
11 ; is callee-cleanup. However, in MSVC's cdecl calling convention, sret pointer
12 ; arguments are caller-cleanup like normal arguments.
13
14 define void @sret1(i8* sret %x) nounwind {
15 entry:
16 ; WIN32-LABEL:      _sret1:
17 ; WIN32:      movb $42, (%eax)
18 ; WIN32-NOT:  popl %eax
19 ; WIN32:    {{retl$}}
20
21 ; MINGW_X86-LABEL:  _sret1:
22 ; MINGW_X86:  {{retl$}}
23
24 ; LINUX-LABEL:      sret1:
25 ; LINUX:      retl $4
26
27   store i8 42, i8* %x, align 4
28   ret void
29 }
30
31 define void @sret2(i8* sret %x, i8 %y) nounwind {
32 entry:
33 ; WIN32-LABEL:      _sret2:
34 ; WIN32:      movb {{.*}}, (%eax)
35 ; WIN32-NOT:  popl %eax
36 ; WIN32:    {{retl$}}
37
38 ; MINGW_X86-LABEL:  _sret2:
39 ; MINGW_X86:  {{retl$}}
40
41 ; LINUX-LABEL:      sret2:
42 ; LINUX:      retl $4
43
44   store i8 %y, i8* %x
45   ret void
46 }
47
48 define void @sret3(i8* sret %x, i8* %y) nounwind {
49 entry:
50 ; WIN32-LABEL:      _sret3:
51 ; WIN32:      movb $42, (%eax)
52 ; WIN32-NOT:  movb $13, (%eax)
53 ; WIN32-NOT:  popl %eax
54 ; WIN32:    {{retl$}}
55
56 ; MINGW_X86-LABEL:  _sret3:
57 ; MINGW_X86:  {{retl$}}
58
59 ; LINUX-LABEL:      sret3:
60 ; LINUX:      retl $4
61
62   store i8 42, i8* %x
63   store i8 13, i8* %y
64   ret void
65 }
66
67 ; PR15556
68 %struct.S4 = type { i32, i32, i32 }
69
70 define void @sret4(%struct.S4* noalias sret %agg.result) {
71 entry:
72 ; WIN32-LABEL:     _sret4:
73 ; WIN32:     movl $42, (%eax)
74 ; WIN32-NOT: popl %eax
75 ; WIN32:   {{retl$}}
76
77 ; MINGW_X86-LABEL: _sret4:
78 ; MINGW_X86: {{retl$}}
79
80 ; LINUX-LABEL:     sret4:
81 ; LINUX:     retl $4
82
83   %x = getelementptr inbounds %struct.S4* %agg.result, i32 0, i32 0
84   store i32 42, i32* %x, align 4
85   ret void
86 }
87
88 %struct.S5 = type { i32 }
89 %class.C5 = type { i8 }
90
91 define x86_thiscallcc void @"\01?foo@C5@@QAE?AUS5@@XZ"(%struct.S5* noalias sret %agg.result, %class.C5* %this) {
92 entry:
93   %this.addr = alloca %class.C5*, align 4
94   store %class.C5* %this, %class.C5** %this.addr, align 4
95   %this1 = load %class.C5** %this.addr
96   %x = getelementptr inbounds %struct.S5* %agg.result, i32 0, i32 0
97   store i32 42, i32* %x, align 4
98   ret void
99 ; WIN32-LABEL:     {{^}}"?foo@C5@@QAE?AUS5@@XZ":
100 ; MINGW_X86-LABEL: {{^}}"?foo@C5@@QAE?AUS5@@XZ":
101 ; LINUX-LABEL:     {{^}}"?foo@C5@@QAE?AUS5@@XZ":
102
103 ; The address of the return structure is passed as an implicit parameter.
104 ; In the -O0 build, %eax is spilled at the beginning of the function, hence we
105 ; should match both 4(%esp) and 8(%esp).
106 ; WIN32:     {{[48]}}(%esp), %eax
107 ; WIN32:     movl $42, (%eax)
108 ; WIN32:     retl $4
109 }
110
111 define void @call_foo5() {
112 entry:
113   %c = alloca %class.C5, align 1
114   %s = alloca %struct.S5, align 4
115   call x86_thiscallcc void @"\01?foo@C5@@QAE?AUS5@@XZ"(%struct.S5* sret %s, %class.C5* %c)
116 ; WIN32-LABEL:      {{^}}_call_foo5:
117 ; MINGW_X86-LABEL:  {{^}}_call_foo5:
118 ; LINUX-LABEL:      {{^}}call_foo5:
119
120
121 ; Load the address of the result and put it onto stack
122 ; (through %ecx in the -O0 build).
123 ; WIN32:      leal {{[0-9]+}}(%esp), %e{{[a-d]}}x
124 ; WIN32:      movl %e{{[a-d]}}x, (%e{{([a-d]x)|(sp)}})
125
126 ; The this pointer goes to ECX.
127 ; WIN32-NEXT: leal {{[0-9]+}}(%esp), %ecx
128 ; WIN32-NEXT: calll "?foo@C5@@QAE?AUS5@@XZ"
129 ; WIN32:      retl
130   ret void
131 }
132
133
134 %struct.test6 = type { i32, i32, i32 }
135 define void @test6_f(%struct.test6* %x) nounwind {
136 ; WIN32-LABEL: _test6_f:
137 ; MINGW_X86-LABEL: _test6_f:
138 ; LINUX-LABEL: test6_f:
139
140 ; The %x argument is moved to %ecx. It will be the this pointer.
141 ; WIN32: movl    8(%ebp), %ecx
142
143 ; The %x argument is moved to (%esp). It will be the this pointer. With -O0
144 ; we copy esp to ecx and use (ecx) instead of (esp).
145 ; MINGW_X86: movl    8(%ebp), %eax
146 ; MINGW_X86: movl    %eax, (%e{{([a-d]x)|(sp)}})
147
148 ; The sret pointer is (%esp)
149 ; WIN32:          leal    8(%esp), %[[REG:e[a-d]x]]
150 ; WIN32-NEXT:     movl    %[[REG]], (%e{{([a-d]x)|(sp)}})
151
152 ; The sret pointer is %ecx
153 ; MINGW_X86-NEXT: leal    8(%esp), %ecx
154 ; MINGW_X86-NEXT: calll   _test6_g
155
156   %tmp = alloca %struct.test6, align 4
157   call x86_thiscallcc void @test6_g(%struct.test6* sret %tmp, %struct.test6* %x)
158   ret void
159 }
160 declare x86_thiscallcc void @test6_g(%struct.test6* sret, %struct.test6*)