1; RUN: llc -mtriple=x86_64-windows-msvc < %s | FileCheck %s --check-prefix=X64
2; RUN: llc -mtriple=i686-windows-msvc < %s | FileCheck %s --check-prefix=X86
3
4declare i8* @llvm.frameaddress(i32)
5declare void @llvm.localescape(...)
6declare i8* @llvm.localaddress()
7declare i8* @llvm.localrecover(i8*, i8*, i32)
8declare i32 @printf(i8*, ...)
9
10@str = internal constant [10 x i8] c"asdf: %d\0A\00"
11
12define void @print_framealloc_from_fp(i8* %fp) {
13  %a.i8 = call i8* @llvm.localrecover(i8* bitcast (void(i32)* @alloc_func to i8*), i8* %fp, i32 0)
14  %a = bitcast i8* %a.i8 to i32*
15  %a.val = load i32, i32* %a
16  call i32 (i8*, ...) @printf(i8* getelementptr ([10 x i8], [10 x i8]* @str, i32 0, i32 0), i32 %a.val)
17  %b.i8 = call i8* @llvm.localrecover(i8* bitcast (void(i32)* @alloc_func to i8*), i8* %fp, i32 1)
18  %b = bitcast i8* %b.i8 to i32*
19  %b.val = load i32, i32* %b
20  call i32 (i8*, ...) @printf(i8* getelementptr ([10 x i8], [10 x i8]* @str, i32 0, i32 0), i32 %b.val)
21  store i32 42, i32* %b
22  %b2 = getelementptr i32, i32* %b, i32 1
23  %b2.val = load i32, i32* %b2
24  call i32 (i8*, ...) @printf(i8* getelementptr ([10 x i8], [10 x i8]* @str, i32 0, i32 0), i32 %b2.val)
25  ret void
26}
27
28; X64-LABEL: print_framealloc_from_fp:
29; X64: movq %rcx, %[[parent_fp:[a-z]+]]
30; X64: movl .Lalloc_func$frame_escape_0(%[[parent_fp]]), %edx
31; X64: leaq {{.*}}(%rip), %[[str:[a-z]+]]
32; X64: movq %[[str]], %rcx
33; X64: callq printf
34; X64: movl .Lalloc_func$frame_escape_1(%[[parent_fp]]), %edx
35; X64: movq %[[str]], %rcx
36; X64: callq printf
37; X64: movl    $42, .Lalloc_func$frame_escape_1(%[[parent_fp]])
38; X64: retq
39
40; X86-LABEL: print_framealloc_from_fp:
41; X86: pushl   %esi
42; X86: subl    $8, %esp
43; X86: movl    16(%esp), %esi
44; X86: movl    Lalloc_func$frame_escape_0(%esi), %eax
45; X86: movl    %eax, 4(%esp)
46; X86: movl    $_str, (%esp)
47; X86: calll   _printf
48; X86: movl    Lalloc_func$frame_escape_1(%esi), %eax
49; X86: movl    %eax, 4(%esp)
50; X86: movl    $_str, (%esp)
51; X86: calll   _printf
52; X86: movl    $42, Lalloc_func$frame_escape_1(%esi)
53; X86: movl    $4, %eax
54; X86: movl    Lalloc_func$frame_escape_1(%esi,%eax), %eax
55; X86: movl    %eax, 4(%esp)
56; X86: movl    $_str, (%esp)
57; X86: calll   _printf
58; X86: addl    $8, %esp
59; X86: popl    %esi
60; X86: retl
61
62define void @alloc_func(i32 %n) {
63  %a = alloca i32
64  %b = alloca i32, i32 2
65  call void (...) @llvm.localescape(i32* %a, i32* %b)
66  store i32 42, i32* %a
67  store i32 13, i32* %b
68
69  ; Force usage of EBP with a dynamic alloca.
70  alloca i8, i32 %n
71
72  %lp = call i8* @llvm.localaddress()
73  call void @print_framealloc_from_fp(i8* %lp)
74  ret void
75}
76
77; X64-LABEL: alloc_func:
78; X64: pushq   %rbp
79; X64: subq    $16, %rsp
80; X64: .seh_stackalloc 16
81; X64: leaq    16(%rsp), %rbp
82; X64: .seh_setframe 5, 16
83; X64: .Lalloc_func$frame_escape_0 = -4
84; X64: .Lalloc_func$frame_escape_1 = -12
85; X64: movl $42, -4(%rbp)
86; X64: movl $13, -12(%rbp)
87; X64: movq 	%rbp, %rcx
88; X64: callq print_framealloc_from_fp
89; X64: retq
90
91; X86-LABEL: alloc_func:
92; X86: pushl   %ebp
93; X86: movl    %esp, %ebp
94; X86: subl    $12, %esp
95; X86: Lalloc_func$frame_escape_0 = -4
96; X86: Lalloc_func$frame_escape_1 = -12
97; X86: movl    $42, -4(%ebp)
98; X86: movl    $13, -12(%ebp)
99; X86: pushl   %ebp
100; X86: calll   _print_framealloc_from_fp
101; X86: movl    %ebp, %esp
102; X86: popl    %ebp
103; X86: retl
104
105; Helper to make this a complete program so it can be compiled and tested.
106define i32 @main() {
107  call void @alloc_func(i32 3)
108  ret i32 0
109}
110
111define void @alloc_func_no_frameaddr() {
112  %a = alloca i32
113  %b = alloca i32
114  call void (...) @llvm.localescape(i32* %a, i32* %b)
115  store i32 42, i32* %a
116  store i32 13, i32* %b
117  call void @print_framealloc_from_fp(i8* null)
118  ret void
119}
120
121; X64-LABEL: alloc_func_no_frameaddr:
122; X64: subq    $40, %rsp
123; X64: .seh_stackalloc 40
124; X64: .seh_endprologue
125; X64: .Lalloc_func_no_frameaddr$frame_escape_0 = 36
126; X64: .Lalloc_func_no_frameaddr$frame_escape_1 = 32
127; X64: movl $42, 36(%rsp)
128; X64: movl $13, 32(%rsp)
129; X64: xorl %ecx, %ecx
130; X64: callq print_framealloc_from_fp
131; X64: addq $40, %rsp
132; X64: retq
133
134; X86-LABEL: alloc_func_no_frameaddr:
135; X86: subl    $12, %esp
136; X86: Lalloc_func_no_frameaddr$frame_escape_0 = 8
137; X86: Lalloc_func_no_frameaddr$frame_escape_1 = 4
138; X86: movl $42, 8(%esp)
139; X86: movl $13, 4(%esp)
140; X86: movl $0, (%esp)
141; X86: calll _print_framealloc_from_fp
142; X86: addl $12, %esp
143; X86: retl
144