1; RUN: llc < %s --mtriple=wasm32-unknown-unknown -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-keep-registers | FileCheck -DPTR=32 %s
2; RUN: llc < %s --mtriple=wasm64-unknown-unknown -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-keep-registers | FileCheck -DPTR=64 %s
3
4declare void @somefunc(i32*)
5
6; CHECK-LABEL: underalign:
7; CHECK:      global.get $push[[L1:.+]]=, __stack_pointer{{$}}
8; CHECK-NEXT: i[[PTR]].const $push[[L2:.+]]=, 16
9; CHECK-NEXT: i[[PTR]].sub $push[[L10:.+]]=, $pop[[L1]], $pop[[L2]]
10; CHECK-NEXT: local.tee $push{{.+}}=, [[SP:.+]], $pop[[L10]]
11
12; CHECK:      local.get $push[[L3:.+]]=, [[SP]]{{$}}
13; CHECK:      i[[PTR]].add $push[[underaligned:.+]]=, $pop[[L3]], $pop{{.+}}
14; CHECK-NEXT: call somefunc, $pop[[underaligned]]
15
16; CHECK:      local.get $push[[M4:.+]]=, [[SP]]{{$}}
17; CHECK:      i[[PTR]].add $push[[L5:.+]]=, $pop[[M4]], $pop{{.+}}
18; CHECK-NEXT: global.set __stack_pointer, $pop[[L5]]
19define void @underalign() {
20entry:
21  %underaligned = alloca i32, align 8
22  call void @somefunc(i32* %underaligned)
23  ret void
24}
25
26; CHECK-LABEL: overalign:
27; CHECK:      global.get $push[[L10:.+]]=, __stack_pointer{{$}}
28; CHECK-NEXT: local.tee $push[[L9:.+]]=, [[BP:.+]], $pop[[L10]]
29; CHECK-NEXT: i[[PTR]].const $push[[L2:.+]]=, 32
30; CHECK-NEXT: i[[PTR]].sub $push[[L8:.+]]=, $pop[[L9]], $pop[[L2]]
31; CHECK-NEXT: i[[PTR]].const $push[[L3:.+]]=, -32
32; CHECK-NEXT: i[[PTR]].and $push[[L7:.+]]=, $pop[[L8]], $pop[[L3]]
33; CHECK-NEXT: local.tee $push{{.+}}=, [[SP:.+]], $pop[[L7]]
34
35; CHECK:      local.get $push[[M5:.+]]=, [[SP]]{{$}}
36; CHECK:      call somefunc, $pop[[M5]]{{$}}
37
38; CHECK:      local.get $push[[M6:.+]]=, [[BP]]{{$}}
39; CHECK-NEXT: global.set __stack_pointer, $pop[[M6]]
40define void @overalign() {
41entry:
42  %overaligned = alloca i32, align 32
43  call void @somefunc(i32* %overaligned)
44  ret void
45}
46
47; CHECK-LABEL: over_and_normal_align:
48; CHECK:      global.get $push[[L14:.+]]=, __stack_pointer{{$}}
49; CHECK-NEXT: local.tee $push[[L13:.+]]=, [[BP:.+]], $pop[[L14]]
50; CHECK:      i[[PTR]].sub $push[[L12:.+]]=, $pop[[L13]], $pop{{.+}}
51; CHECK:      i[[PTR]].and $push[[L11:.+]]=, $pop[[L12]], $pop{{.+}}
52; CHECK-NEXT: local.tee $push{{.+}}=, [[SP:.+]], $pop[[L11]]
53
54; CHECK:      local.get  $push[[M6:.+]]=, [[SP]]{{$}}
55; CHECK:      i[[PTR]].add $push[[L6:.+]]=, $pop[[M6]], $pop{{.+}}
56; CHECK-NEXT: call somefunc, $pop[[L6]]
57; CHECK:      local.get $push[[M7:.+]]=, [[SP]]{{$}}
58; CHECK:      i[[PTR]].add $push[[L8:.+]]=, $pop[[M7]], $pop{{.+}}
59; CHECK-NEXT: call somefunc, $pop[[L8]]
60
61; CHECK:      local.get $push[[L6:.+]]=, [[BP]]{{$}}
62; CHECK-NEXT: global.set __stack_pointer, $pop[[L6]]
63define void @over_and_normal_align() {
64entry:
65  %over = alloca i32, align 32
66  %normal = alloca i32
67  call void @somefunc(i32* %over)
68  call void @somefunc(i32* %normal)
69  ret void
70}
71
72; CHECK-LABEL: dynamic_overalign:
73; CHECK:      global.get $push[[L18:.+]]=, __stack_pointer{{$}}
74; CHECK-NEXT: local.tee $push[[L17:.+]]=, [[SP:.+]], $pop[[L18]]
75; CHECK-NEXT: local.set [[BP:.+]], $pop[[L17]]
76; CHECK:      local.tee $push{{.+}}=, [[SP_2:.+]], $pop{{.+}}
77
78; CHECK:      local.get $push[[M8:.+]]=, [[SP_2]]{{$}}
79; CHECK:      call somefunc, $pop[[M8]]
80
81; CHECK:      local.get $push[[M9:.+]]=, [[BP]]{{$}}
82; CHECK-NEXT: global.set __stack_pointer, $pop[[M9]]
83define void @dynamic_overalign(i32 %num) {
84entry:
85  %dynamic = alloca i32, i32 %num, align 32
86  call void @somefunc(i32* %dynamic)
87  ret void
88}
89
90; CHECK-LABEL: overalign_and_dynamic:
91; CHECK:      global.get $push[[L21:.+]]=, __stack_pointer{{$}}
92; CHECK-NEXT: local.tee $push[[L20:.+]]=, [[BP:.+]], $pop[[L21]]
93; CHECK:      i[[PTR]].sub $push[[L19:.+]]=, $pop[[L20]], $pop{{.+}}
94; CHECK:      i[[PTR]].and $push[[L18:.+]]=, $pop[[L19]], $pop{{.+}}
95; CHECK:      local.tee $push{{.+}}=, [[FP:.+]], $pop[[L18]]
96; CHECK:      local.get $push[[M10:.+]]=, [[FP]]{{$}}
97; CHECK:      i[[PTR]].sub $push[[L16:.+]]=, $pop[[M10]], $pop{{.+}}
98; CHECK-NEXT: local.tee $push{{.+}}=, [[SP:.+]], $pop[[L16]]
99
100; CHECK:      local.get $push[[over:.+]]=, [[FP]]
101; CHECK-NEXT: call somefunc, $pop[[over]]
102; CHECK:      local.get $push[[another:.+]]=, [[SP]]
103; CHECK-NEXT: call somefunc, $pop[[another]]
104
105; CHECK:      local.get  $push[[M11:.+]]=, [[BP]]{{$}}
106; CHECK-NEXT: global.set __stack_pointer, $pop[[M11]]
107define void @overalign_and_dynamic(i32 %num) {
108entry:
109  %over = alloca i32, align 32
110  %dynamic = alloca i32, i32 %num
111  call void @somefunc(i32* %over)
112  call void @somefunc(i32* %dynamic)
113  ret void
114}
115
116; CHECK-LABEL: overalign_static_and_dynamic:
117; CHECK:      global.get $push[[L26:.+]]=, __stack_pointer{{$}}
118; CHECK-NEXT: local.tee $push[[L25:.+]]=, [[BP:.+]], $pop[[L26]]
119; CHECK:      i[[PTR]].sub $push[[L24:.+]]=, $pop[[L25]], $pop{{.+}}
120; CHECK:      i[[PTR]].and $push[[L23:.+]]=, $pop[[L24]], $pop{{.+}}
121; CHECK:      local.tee $push{{.+}}=, [[FP:.+]], $pop[[L23]]
122; CHECK:      local.get $push[[M12:.+]]=, [[FP]]{{$}}
123; CHECK:      i[[PTR]].sub $push[[L21:.+]]=, $pop[[M12]], $pop{{.+}}
124; CHECK-NEXT: local.tee $push{{.+}}=, [[SP:.+]], $pop[[L21]]
125
126; CHECK:      local.get $push[[L19:.+]]=, [[FP]]
127; CHECK:      local.tee $push[[L18:.+]]=, [[FP_2:.+]], $pop[[L19]]
128; CHECK:      i[[PTR]].add $push[[over:.+]]=, $pop[[L18]], $pop{{.+}}
129; CHECK-NEXT: call somefunc, $pop[[over]]
130; CHECK:      local.get $push[[M12:.+]]=, [[SP]]
131; CHECK:      call somefunc, $pop[[M12]]
132; CHECK:      local.get $push[[M13:.+]]=, [[FP_2]]
133; CHECK:      i[[PTR]].add $push[[static:.+]]=, $pop[[M13]], $pop{{.+}}
134; CHECK-NEXT: call somefunc, $pop[[static]]
135
136; CHECK:      local.get  $push[[M14:.+]]=, [[BP]]{{$}}
137; CHECK-NEXT: global.set __stack_pointer, $pop[[M14]]
138define void @overalign_static_and_dynamic(i32 %num) {
139entry:
140  %over = alloca i32, align 32
141  %dynamic = alloca i32, i32 %num
142  %static = alloca i32
143  call void @somefunc(i32* %over)
144  call void @somefunc(i32* %dynamic)
145  call void @somefunc(i32* %static)
146  ret void
147}
148