1; RUN: llc -verify-machineinstrs -stack-symbol-ordering=0 < %s | FileCheck %s
2
3target datalayout = "e-i64:64-f80:128-n8:16:32:64-S128"
4target triple = "x86_64-pc-linux-gnu"
5
6; This test is checking to make sure that we reuse the same stack slots
7; for GC values spilled over two different call sites.  Since the order
8; of GC arguments differ, niave lowering code would insert loads and
9; stores to rearrange items on the stack.  We need to make sure (for
10; performance) that this doesn't happen.
11define i32 @back_to_back_calls(i32 addrspace(1)* %a, i32 addrspace(1)* %b, i32 addrspace(1)* %c) #1 gc "statepoint-example" {
12; CHECK-LABEL: back_to_back_calls
13; The exact stores don't matter, but there need to be three stack slots created
14; CHECK-DAG: movq	%rdi, {{[0-9]*}}(%rsp)
15; CHECK-DAG: movq	%rdx, {{[0-9]*}}(%rsp)
16; CHECK-DAG: movq	%rsi, {{[0-9]*}}(%rsp)
17; There should be no more than three moves
18; CHECK-NOT: movq
19  %safepoint_token = tail call token (i64, i32, void ()*, i32, i32, ...) @llvm.experimental.gc.statepoint.p0f_isVoidf(i64 0, i32 0, void ()* elementtype(void ()) undef, i32 0, i32 0, i32 0, i32 0) ["gc-live" (i32 addrspace(1)* %a, i32 addrspace(1)* %b, i32 addrspace(1)* %c), "deopt" (i32 0, i32 -1, i32 0, i32 0, i32 0)]
20  %a1 = tail call coldcc i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token %safepoint_token, i32 0, i32 0)
21  %b1 = tail call coldcc i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token %safepoint_token, i32 0, i32 1)
22  %c1 = tail call coldcc i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token %safepoint_token, i32 0, i32 2)
23; CHECK: callq
24; This is the key check.  There should NOT be any memory moves here
25; CHECK-NOT: movq
26  %safepoint_token2 = tail call token (i64, i32, void ()*, i32, i32, ...) @llvm.experimental.gc.statepoint.p0f_isVoidf(i64 0, i32 0, void ()* elementtype(void ()) undef, i32 0, i32 0, i32 0, i32 0) ["gc-live" (i32 addrspace(1)* %c1, i32 addrspace(1)* %b1, i32 addrspace(1)* %a1), "deopt" (i32 0, i32 -1, i32 0, i32 0, i32 0)]
27  %a2 = tail call coldcc i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token %safepoint_token2, i32 0, i32 2)
28  %b2 = tail call coldcc i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token %safepoint_token2, i32 0, i32 1)
29  %c2 = tail call coldcc i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token %safepoint_token2, i32 0, i32 0)
30; CHECK: callq
31  ret i32 1
32}
33
34; This test simply checks that minor changes in vm state don't prevent slots
35; being reused for gc values.
36define i32 @reserve_first(i32 addrspace(1)* %a, i32 addrspace(1)* %b, i32 addrspace(1)* %c) #1 gc "statepoint-example" {
37; CHECK-LABEL: reserve_first
38; The exact stores don't matter, but there need to be three stack slots created
39; CHECK-DAG: movq	%rdi, {{[0-9]*}}(%rsp)
40; CHECK-DAG: movq	%rdx, {{[0-9]*}}(%rsp)
41; CHECK-DAG: movq	%rsi, {{[0-9]*}}(%rsp)
42  %safepoint_token = tail call token (i64, i32, void ()*, i32, i32, ...) @llvm.experimental.gc.statepoint.p0f_isVoidf(i64 0, i32 0, void ()* elementtype(void ()) undef, i32 0, i32 0, i32 0, i32 0) ["gc-live" (i32 addrspace(1)* %a, i32 addrspace(1)* %b, i32 addrspace(1)* %c), "deopt" (i32 0, i32 -1, i32 0, i32 0, i32 0)]
43  %a1 = tail call coldcc i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token %safepoint_token, i32 0, i32 0)
44  %b1 = tail call coldcc i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token %safepoint_token, i32 0, i32 1)
45  %c1 = tail call coldcc i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token %safepoint_token, i32 0, i32 2)
46; CHECK: callq
47; This is the key check.  There should NOT be any memory moves here
48; CHECK-NOT: movq
49  %safepoint_token2 = tail call token (i64, i32, void ()*, i32, i32, ...) @llvm.experimental.gc.statepoint.p0f_isVoidf(i64 0, i32 0, void ()* elementtype(void ()) undef, i32 0, i32 0, i32 0, i32 0) ["gc-live" (i32 addrspace(1)* %c1, i32 addrspace(1)* %b1, i32 addrspace(1)* %a1), "deopt" (i32 addrspace(1)* %a1, i32 0, i32 addrspace(1)* %c1, i32 0, i32 0)]
50  %a2 = tail call coldcc i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token %safepoint_token2, i32 0, i32 2)
51  %b2 = tail call coldcc i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token %safepoint_token2, i32 0, i32 1)
52  %c2 = tail call coldcc i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token %safepoint_token2, i32 0, i32 0)
53; CHECK: callq
54  ret i32 1
55}
56
57; Check that we reuse the same stack slot across multiple calls.  The use of
58; more than two calls here is critical.  We've had a bug which allowed reuse
59; exactly once which went undetected for a long time.
60define i32 @back_to_back_deopt(i32 %a, i32 %b, i32 %c) #1
61  gc "statepoint-example" {
62; CHECK-LABEL: back_to_back_deopt
63; The exact stores don't matter, but there need to be three stack slots created
64; CHECK-DAG: movl	%edi, 12(%rsp)
65; CHECK-DAG: movl	%esi, 8(%rsp)
66; CHECK-DAG: movl	%edx, 4(%rsp)
67; CHECK: callq
68; CHECK-DAG: movl	%ebx, 12(%rsp)
69; CHECK-DAG: movl	%ebp, 8(%rsp)
70; CHECK-DAG: movl	%r14d, 4(%rsp)
71; CHECK: callq
72; CHECK-DAG: movl	%ebx, 12(%rsp)
73; CHECK-DAG: movl	%ebp, 8(%rsp)
74; CHECK-DAG: movl	%r14d, 4(%rsp)
75; CHECK: callq
76; CHECK-DAG: movl	%ebx, 12(%rsp)
77; CHECK-DAG: movl	%ebp, 8(%rsp)
78; CHECK-DAG: movl	%r14d, 4(%rsp)
79; CHECK: callq
80  call token (i64, i32, void ()*, i32, i32, ...) @llvm.experimental.gc.statepoint.p0f_isVoidf(i64 0, i32 0, void ()* elementtype(void ()) undef, i32 0, i32 0, i32 0, i32 0) ["deopt" (i32 %a, i32 %b, i32 %c)]
81call token (i64, i32, void ()*, i32, i32, ...) @llvm.experimental.gc.statepoint.p0f_isVoidf(i64 0, i32 0, void ()* elementtype(void ()) undef, i32 0, i32 0, i32 0, i32 0) ["deopt" (i32 %a, i32 %b, i32 %c)]
82call token (i64, i32, void ()*, i32, i32, ...) @llvm.experimental.gc.statepoint.p0f_isVoidf(i64 0, i32 0, void ()* elementtype(void ()) undef, i32 0, i32 0, i32 0, i32 0) ["deopt" (i32 %a, i32 %b, i32 %c)]
83call token (i64, i32, void ()*, i32, i32, ...) @llvm.experimental.gc.statepoint.p0f_isVoidf(i64 0, i32 0, void ()* elementtype(void ()) undef, i32 0, i32 0, i32 0, i32 0) ["deopt" (i32 %a, i32 %b, i32 %c)]
84  ret i32 1
85}
86
87; Test that stack slots are reused for invokes
88define i32 @back_to_back_invokes(i32 addrspace(1)* %a, i32 addrspace(1)* %b, i32 addrspace(1)* %c) #1 gc "statepoint-example" personality i32 ()* @"personality_function" {
89; CHECK-LABEL: back_to_back_invokes
90entry:
91  ; The exact stores don't matter, but there need to be three stack slots created
92  ; CHECK-DAG: movq	%rdi, {{[0-9]*}}(%rsp)
93  ; CHECK-DAG: movq	%rdx, {{[0-9]*}}(%rsp)
94  ; CHECK-DAG: movq	%rsi, {{[0-9]*}}(%rsp)
95  ; CHECK: callq
96  %safepoint_token = invoke token (i64, i32, void ()*, i32, i32, ...) @llvm.experimental.gc.statepoint.p0f_isVoidf(i64 0, i32 0, void ()* elementtype(void ()) undef, i32 0, i32 0, i32 0, i32 0) ["gc-live" (i32 addrspace(1)* %a, i32 addrspace(1)* %b, i32 addrspace(1)* %c), "deopt" (i32 0, i32 -1, i32 0, i32 0, i32 0)]
97                   to label %normal_return unwind label %exceptional_return
98
99normal_return:
100  %a1 = tail call coldcc i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token %safepoint_token, i32 0, i32 0)
101  %b1 = tail call coldcc i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token %safepoint_token, i32 0, i32 1)
102  %c1 = tail call coldcc i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token %safepoint_token, i32 0, i32 2)
103  ; Should work even through bitcasts
104  %c1.casted = bitcast i32 addrspace(1)* %c1 to i8 addrspace(1)*
105  ; This is the key check.  There should NOT be any memory moves here
106  ; CHECK-NOT: movq
107  ; CHECK: callq
108  %safepoint_token2 = invoke token (i64, i32, void ()*, i32, i32, ...) @llvm.experimental.gc.statepoint.p0f_isVoidf(i64 0, i32 0, void ()* elementtype(void ()) undef, i32 0, i32 0, i32 0, i32 0) ["gc-live" (i8 addrspace(1)* %c1.casted, i32 addrspace(1)* %b1, i32 addrspace(1)* %a1), "deopt" (i32 0, i32 -1, i32 0, i32 0, i32 0)]
109                    to label %normal_return2 unwind label %exceptional_return2
110
111normal_return2:
112  %a2 = tail call coldcc i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token %safepoint_token2, i32 0, i32 2)
113  %b2 = tail call coldcc i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token %safepoint_token2, i32 0, i32 1)
114  %c2 = tail call coldcc i8 addrspace(1)* @llvm.experimental.gc.relocate.p1i8(token %safepoint_token2, i32 0, i32 0)
115  ret i32 1
116
117exceptional_return:
118  %landing_pad = landingpad { i8*, i32 }
119          cleanup
120  ret i32 0
121
122exceptional_return2:
123  %landing_pad2 = landingpad { i8*, i32 }
124          cleanup
125  ret i32 0
126}
127
128; Function Attrs: nounwind
129declare i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token, i32, i32) #3
130declare i8 addrspace(1)* @llvm.experimental.gc.relocate.p1i8(token, i32, i32) #3
131
132declare token @llvm.experimental.gc.statepoint.p0f_isVoidf(i64, i32, void ()*, i32, i32, ...)
133
134declare i32 @"personality_function"()
135
136attributes #1 = { uwtable }
137