Debian Codegraph @

internal/engine/wazevo/backend/isa/arm64/lower_mem_test.go

raw
1 package arm64
2
3 import (
4 "fmt"
5 "math"
6 "strings"
7 "testing"
8
9 "github.com/tetratelabs/wazero/internal/engine/wazevo/backend"
10 "github.com/tetratelabs/wazero/internal/engine/wazevo/backend/regalloc"
11 "github.com/tetratelabs/wazero/internal/engine/wazevo/ssa"
12 "github.com/tetratelabs/wazero/internal/engine/wazevo/wazevoapi"
13 "github.com/tetratelabs/wazero/internal/testing/require"
14 )
15
16 func TestAddressMode_format(t *testing.T) {
17 t.Run("addressModeKindRegScaledExtended", func(t *testing.T) {
18 require.Equal(t,
19 "[x1, w0, UXTW #0x1]",
20 addressMode{
21 kind: addressModeKindRegScaledExtended,
22 rn: regalloc.FromRealReg(x1, regalloc.RegTypeInt),
23 rm: regalloc.FromRealReg(x0, regalloc.RegTypeInt),
24 extOp: extendOpUXTW,
25 imm: 0,
26 }.format(16),
27 )
28 require.Equal(t,
29 "[x1, w0, SXTW #0x1]",
30 addressMode{
31 kind: addressModeKindRegScaledExtended,
32 rn: regalloc.FromRealReg(x1, regalloc.RegTypeInt),
33 rm: regalloc.FromRealReg(x0, regalloc.RegTypeInt),
34 extOp: extendOpSXTW,
35 imm: 0,
36 }.format(16),
37 )
38 require.Equal(t,
39 "[x1, w0, UXTW #0x2]",
40 addressMode{
41 kind: addressModeKindRegScaledExtended,
42 rn: regalloc.FromRealReg(x1, regalloc.RegTypeInt),
43 rm: regalloc.FromRealReg(x0, regalloc.RegTypeInt),
44 extOp: extendOpUXTW,
45 imm: 0,
46 }.format(32),
47 )
48 require.Equal(t,
49 "[x1, w0, SXTW #0x2]",
50 addressMode{
51 kind: addressModeKindRegScaledExtended,
52 rn: regalloc.FromRealReg(x1, regalloc.RegTypeInt),
53 rm: regalloc.FromRealReg(x0, regalloc.RegTypeInt),
54 extOp: extendOpSXTW,
55 imm: 0,
56 }.format(32),
57 )
58 require.Equal(t,
59 "[x1, w0, UXTW #0x3]",
60 addressMode{
61 kind: addressModeKindRegScaledExtended,
62 rn: regalloc.FromRealReg(x1, regalloc.RegTypeInt),
63 rm: regalloc.FromRealReg(x0, regalloc.RegTypeInt),
64 extOp: extendOpUXTW,
65 imm: 0,
66 }.format(64),
67 )
68 require.Equal(t,
69 "[x1, w0, SXTW #0x3]",
70 addressMode{
71 kind: addressModeKindRegScaledExtended,
72 rn: regalloc.FromRealReg(x1, regalloc.RegTypeInt),
73 rm: regalloc.FromRealReg(x0, regalloc.RegTypeInt),
74 extOp: extendOpSXTW,
75 imm: 0,
76 }.format(64),
77 )
78 })
79 t.Run("addressModeKindRegScaled", func(t *testing.T) {
80 require.Equal(t,
81 "[x1, w0, lsl #0x1]",
82 addressMode{
83 kind: addressModeKindRegScaled,
84 rn: regalloc.FromRealReg(x1, regalloc.RegTypeInt),
85 rm: regalloc.FromRealReg(x0, regalloc.RegTypeInt),
86 extOp: extendOpUXTW,
87 imm: 0,
88 }.format(16),
89 )
90 require.Equal(t,
91 "[x1, w0, lsl #0x1]",
92 addressMode{
93 kind: addressModeKindRegScaled,
94 rn: regalloc.FromRealReg(x1, regalloc.RegTypeInt),
95 rm: regalloc.FromRealReg(x0, regalloc.RegTypeInt),
96 extOp: extendOpSXTW,
97 imm: 0,
98 }.format(16),
99 )
100 require.Equal(t,
101 "[x1, w0, lsl #0x2]",
102 addressMode{
103 kind: addressModeKindRegScaled,
104 rn: regalloc.FromRealReg(x1, regalloc.RegTypeInt),
105 rm: regalloc.FromRealReg(x0, regalloc.RegTypeInt),
106 extOp: extendOpUXTW,
107 imm: 0,
108 }.format(32),
109 )
110 require.Equal(t,
111 "[x1, w0, lsl #0x2]",
112 addressMode{
113 kind: addressModeKindRegScaled,
114 rn: regalloc.FromRealReg(x1, regalloc.RegTypeInt),
115 rm: regalloc.FromRealReg(x0, regalloc.RegTypeInt),
116 extOp: extendOpSXTW,
117 imm: 0,
118 }.format(32),
119 )
120 require.Equal(t,
121 "[x1, w0, lsl #0x3]",
122 addressMode{
123 kind: addressModeKindRegScaled,
124 rn: regalloc.FromRealReg(x1, regalloc.RegTypeInt),
125 rm: regalloc.FromRealReg(x0, regalloc.RegTypeInt),
126 extOp: extendOpUXTW,
127 imm: 0,
128 }.format(64),
129 )
130 require.Equal(t,
131 "[x1, w0, lsl #0x3]",
132 addressMode{
133 kind: addressModeKindRegScaled,
134 rn: regalloc.FromRealReg(x1, regalloc.RegTypeInt),
135 rm: regalloc.FromRealReg(x0, regalloc.RegTypeInt),
136 extOp: extendOpSXTW,
137 imm: 0,
138 }.format(64),
139 )
140 })
141 t.Run("addressModeKindRegExtended", func(t *testing.T) {
142 require.Equal(t,
143 "[x1, w0, UXTW]",
144 addressMode{
145 kind: addressModeKindRegExtended,
146 rn: regalloc.FromRealReg(x1, regalloc.RegTypeInt),
147 rm: regalloc.FromRealReg(x0, regalloc.RegTypeInt),
148 extOp: extendOpUXTW,
149 imm: 0,
150 }.format(16),
151 )
152 require.Equal(t,
153 "[x1, w0, SXTW]",
154 addressMode{
155 kind: addressModeKindRegExtended,
156 rn: regalloc.FromRealReg(x1, regalloc.RegTypeInt),
157 rm: regalloc.FromRealReg(x0, regalloc.RegTypeInt),
158 extOp: extendOpSXTW,
159 imm: 0,
160 }.format(64),
161 )
162 })
163 t.Run("addressModeKindRegReg", func(t *testing.T) {
164 require.Equal(t,
165 "[x1, w29]",
166 addressMode{
167 kind: addressModeKindRegReg,
168 rn: regalloc.FromRealReg(x1, regalloc.RegTypeInt),
169 rm: regalloc.FromRealReg(x29, regalloc.RegTypeInt),
170 extOp: extendOpUXTW, // To indicate that the index reg is 32-bit.
171 imm: 0,
172 }.format(64),
173 )
174 require.Equal(t,
175 "[x1, x29]",
176 addressMode{
177 kind: addressModeKindRegReg,
178 rn: regalloc.FromRealReg(x1, regalloc.RegTypeInt),
179 rm: regalloc.FromRealReg(x29, regalloc.RegTypeInt),
180 extOp: extendOpUXTX, // To indicate that the index reg is 64-bit.
181 imm: 0,
182 }.format(64),
183 )
184 })
185 t.Run("addressModeKindRegSignedImm9", func(t *testing.T) {
186 require.Equal(t,
187 "[x1]",
188 addressMode{
189 kind: addressModeKindRegSignedImm9,
190 rn: regalloc.FromRealReg(x1, regalloc.RegTypeInt),
191 imm: 0,
192 }.format(64),
193 )
194 require.Equal(t,
195 "[x1, #-0x100]",
196 addressMode{
197 kind: addressModeKindRegSignedImm9,
198 rn: regalloc.FromRealReg(x1, regalloc.RegTypeInt),
199 imm: math.MinInt8 << 1,
200 }.format(64),
201 )
202 require.Equal(t,
203 "[x1, #0xff]",
204 addressMode{
205 kind: addressModeKindRegSignedImm9,
206 rn: regalloc.FromRealReg(x1, regalloc.RegTypeInt),
207 imm: (math.MaxInt8 << 1) + 1,
208 }.format(64),
209 )
210 })
211
212 t.Run("addressModeKindRegUnsignedImm12", func(t *testing.T) {
213 require.Equal(t,
214 "[x1]",
215 addressMode{
216 kind: addressModeKindRegUnsignedImm12,
217 rn: regalloc.FromRealReg(x1, regalloc.RegTypeInt),
218 imm: 0,
219 }.format(64),
220 )
221 require.Equal(t,
222 "[x1, #0xfff]",
223 addressMode{
224 kind: addressModeKindRegUnsignedImm12,
225 rn: regalloc.FromRealReg(x1, regalloc.RegTypeInt),
226 imm: 4095,
227 }.format(64),
228 )
229 })
230 }
231
232 func Test_offsetFitsInAddressModeKindRegUnsignedImm12(t *testing.T) {
233 for _, tc := range []struct {
234 dstSizeInBits byte
235 offset int64
236 exp bool
237 }{
238 {dstSizeInBits: 8, offset: -1, exp: false},
239 {dstSizeInBits: 8, offset: 0, exp: false},
240 {dstSizeInBits: 8, offset: 1, exp: true},
241 {dstSizeInBits: 8, offset: 2, exp: true},
242 {dstSizeInBits: 8, offset: 3, exp: true},
243 {dstSizeInBits: 8, offset: 4095, exp: true},
244 {dstSizeInBits: 8, offset: 4096, exp: false},
245 {dstSizeInBits: 16, offset: -2, exp: false},
246 {dstSizeInBits: 16, offset: -1, exp: false},
247 {dstSizeInBits: 16, offset: 0, exp: false},
248 {dstSizeInBits: 16, offset: 1, exp: false},
249 {dstSizeInBits: 16, offset: 2, exp: true},
250 {dstSizeInBits: 16, offset: 3, exp: false},
251 {dstSizeInBits: 16, offset: 4095, exp: false},
252 {dstSizeInBits: 16, offset: 4096, exp: true},
253 {dstSizeInBits: 16, offset: 4095 * 2, exp: true},
254 {dstSizeInBits: 16, offset: 4095*2 + 1, exp: false},
255 {dstSizeInBits: 32, offset: -4, exp: false},
256 {dstSizeInBits: 32, offset: -1, exp: false},
257 {dstSizeInBits: 32, offset: 0, exp: false},
258 {dstSizeInBits: 32, offset: 1, exp: false},
259 {dstSizeInBits: 32, offset: 2, exp: false},
260 {dstSizeInBits: 32, offset: 3, exp: false},
261 {dstSizeInBits: 32, offset: 4, exp: true},
262 {dstSizeInBits: 32, offset: 4095, exp: false},
263 {dstSizeInBits: 32, offset: 4096, exp: true},
264 {dstSizeInBits: 32, offset: 4095 * 4, exp: true},
265 {dstSizeInBits: 32, offset: 4095*4 + 1, exp: false},
266 {dstSizeInBits: 64, offset: -8, exp: false},
267 {dstSizeInBits: 64, offset: -1, exp: false},
268 {dstSizeInBits: 64, offset: 0, exp: false},
269 {dstSizeInBits: 64, offset: 1, exp: false},
270 {dstSizeInBits: 64, offset: 2, exp: false},
271 {dstSizeInBits: 64, offset: 3, exp: false},
272 {dstSizeInBits: 64, offset: 4, exp: false},
273 {dstSizeInBits: 64, offset: 8, exp: true},
274 {dstSizeInBits: 64, offset: 4095, exp: false},
275 {dstSizeInBits: 64, offset: 4096, exp: true},
276 {dstSizeInBits: 64, offset: 4095 * 8, exp: true},
277 {dstSizeInBits: 64, offset: 4095*8 + 1, exp: false},
278 } {
279 require.Equal(
280 t, tc.exp,
281 offsetFitsInAddressModeKindRegUnsignedImm12(tc.dstSizeInBits, tc.offset),
282 fmt.Sprintf("dstSizeInBits=%d, offset=%d", tc.dstSizeInBits, tc.offset),
283 )
284 }
285 }
286
287 func Test_offsetFitsInAddressModeKindRegSignedImm9(t *testing.T) {
288 require.Equal(t, true, offsetFitsInAddressModeKindRegSignedImm9(0))
289 require.Equal(t, false, offsetFitsInAddressModeKindRegSignedImm9(-257))
290 require.Equal(t, true, offsetFitsInAddressModeKindRegSignedImm9(-256))
291 require.Equal(t, true, offsetFitsInAddressModeKindRegSignedImm9(255))
292 require.Equal(t, false, offsetFitsInAddressModeKindRegSignedImm9(256))
293 }
294
295 func TestMachine_collectAddends(t *testing.T) {
296 v1000, v2000 := regalloc.VReg(1000).SetRegType(regalloc.RegTypeInt), regalloc.VReg(2000).SetRegType(regalloc.RegTypeInt)
297 addParam := func(ctx *mockCompiler, b ssa.Builder, typ ssa.Type) ssa.Value {
298 p := b.CurrentBlock().AddParam(b, typ)
299 ctx.vRegMap[p] = v1000
300 ctx.definitions[p] = backend.SSAValueDefinition{V: p}
301 return p
302 }
303 insertI32Const := func(m *mockCompiler, b ssa.Builder, v uint32) *ssa.Instruction {
304 inst := b.AllocateInstruction()
305 inst.AsIconst32(v)
306 b.InsertInstruction(inst)
307 m.definitions[inst.Return()] = backend.SSAValueDefinition{Instr: inst}
308 return inst
309 }
310 insertI64Const := func(m *mockCompiler, b ssa.Builder, v uint64) *ssa.Instruction {
311 inst := b.AllocateInstruction()
312 inst.AsIconst64(v)
313 b.InsertInstruction(inst)
314 m.definitions[inst.Return()] = backend.SSAValueDefinition{Instr: inst, V: inst.Return()}
315 return inst
316 }
317 insertIadd := func(m *mockCompiler, b ssa.Builder, lhs, rhs ssa.Value) *ssa.Instruction {
318 inst := b.AllocateInstruction()
319 inst.AsIadd(lhs, rhs)
320 b.InsertInstruction(inst)
321 m.definitions[inst.Return()] = backend.SSAValueDefinition{Instr: inst, V: inst.Return()}
322 return inst
323 }
324 insertExt := func(m *mockCompiler, b ssa.Builder, v ssa.Value, from, to byte, signed bool) *ssa.Instruction {
325 inst := b.AllocateInstruction()
326 if signed {
327 inst.AsSExtend(v, from, to)
328 } else {
329 inst.AsUExtend(v, from, to)
330 }
331 b.InsertInstruction(inst)
332 m.definitions[inst.Return()] = backend.SSAValueDefinition{Instr: inst, V: v}
333 return inst
334 }
335
336 for _, tc := range []struct {
337 name string
338 setup func(*mockCompiler, ssa.Builder, *machine) (ptr ssa.Value, verify func(t *testing.T))
339 exp32s []addend32
340 exp64s []regalloc.VReg
341 offset int64
342 }{
343 {
344 name: "non merged",
345 setup: func(ctx *mockCompiler, b ssa.Builder, m *machine) (ptr ssa.Value, verify func(t *testing.T)) {
346 ptr = addParam(ctx, b, ssa.TypeI64)
347 return ptr, func(t *testing.T) {}
348 },
349 exp64s: []regalloc.VReg{v1000},
350 },
351 {
352 name: "i32 constant folded",
353 setup: func(ctx *mockCompiler, b ssa.Builder, m *machine) (ptr ssa.Value, verify func(t *testing.T)) {
354 minus1 := int32(-1)
355 c1, c2, c3, c4 := insertI32Const(ctx, b, 1), insertI32Const(ctx, b, 2), insertI32Const(ctx, b, 3), insertI32Const(ctx, b, uint32(minus1))
356 iadd1, iadd2 := insertIadd(ctx, b, c1.Return(), c2.Return()), insertIadd(ctx, b, c3.Return(), c4.Return())
357 iadd3 := insertIadd(ctx, b, iadd1.Return(), iadd2.Return())
358 return iadd3.Return(), func(t *testing.T) {
359 for _, instr := range []*ssa.Instruction{iadd1, iadd2, iadd3} {
360 require.True(t, instr.Lowered())
361 }
362 }
363 },
364 offset: 1 + 2 + 3 - 1,
365 },
366 {
367 name: "i64 constant folded",
368 setup: func(ctx *mockCompiler, b ssa.Builder, m *machine) (ptr ssa.Value, verify func(t *testing.T)) {
369 minus1 := int32(-1)
370 c1, c2, c3, c4 := insertI64Const(ctx, b, 1), insertI64Const(ctx, b, 2), insertI64Const(ctx, b, 3), insertI64Const(ctx, b, uint64(minus1))
371 iadd1, iadd2 := insertIadd(ctx, b, c1.Return(), c2.Return()), insertIadd(ctx, b, c3.Return(), c4.Return())
372 iadd3 := insertIadd(ctx, b, iadd1.Return(), iadd2.Return())
373 return iadd3.Return(), func(t *testing.T) {
374 for _, instr := range []*ssa.Instruction{iadd1, iadd2, iadd3} {
375 require.True(t, instr.Lowered())
376 }
377 }
378 },
379 offset: 1 + 2 + 3 - 1,
380 },
381 {
382 name: "constant folded with one 32 value",
383 setup: func(ctx *mockCompiler, b ssa.Builder, m *machine) (ptr ssa.Value, verify func(t *testing.T)) {
384 param := addParam(ctx, b, ssa.TypeI32)
385 minus1 := int32(-1)
386 c1, c2, c3, c4 := insertI32Const(ctx, b, 1), insertI32Const(ctx, b, 2), insertI32Const(ctx, b, 3), insertI32Const(ctx, b, uint32(minus1))
387 iadd1, iadd2 := insertIadd(ctx, b, c1.Return(), c2.Return()), insertIadd(ctx, b, c3.Return(), c4.Return())
388 iadd3 := insertIadd(ctx, b, iadd1.Return(), iadd2.Return())
389 iadd4 := insertIadd(ctx, b, param, iadd3.Return())
390
391 return iadd4.Return(), func(t *testing.T) {
392 for _, instr := range []*ssa.Instruction{iadd1, iadd2, iadd3, iadd4} {
393 require.True(t, instr.Lowered())
394 }
395 // Param must be zero-extended.
396 require.Equal(t, "uxtw x1?, w1000?", formatEmittedInstructionsInCurrentBlock(m))
397 }
398 },
399 exp64s: []regalloc.VReg{regalloc.VReg(1).SetRegType(regalloc.RegTypeInt)},
400 offset: 1 + 2 + 3 - 1,
401 },
402 {
403 name: "one 64 value + sign-extended (32->64) instr",
404 setup: func(ctx *mockCompiler, b ssa.Builder, m *machine) (ptr ssa.Value, verify func(t *testing.T)) {
405 param := addParam(ctx, b, ssa.TypeI64)
406 c1, c2 := insertI32Const(ctx, b, 1), insertI32Const(ctx, b, 2)
407 iadd1 := insertIadd(ctx, b, c1.Return(), c2.Return())
408 ext := insertExt(ctx, b, iadd1.Return(), 32, 64, true)
409 ctx.vRegMap[ext.Arg()] = v2000
410 iadd4 := insertIadd(ctx, b, param, ext.Return())
411 return iadd4.Return(), func(t *testing.T) {
412 for _, instr := range []*ssa.Instruction{ext, iadd4} {
413 require.True(t, instr.Lowered())
414 }
415 }
416 },
417 exp64s: []regalloc.VReg{v1000 /* == param */},
418 exp32s: []addend32{{v2000, extendOpSXTW} /* sign-extended iadd1 */},
419 },
420 {
421 name: "one 64 value + sign-extended (32->64) const",
422 setup: func(ctx *mockCompiler, b ssa.Builder, m *machine) (ptr ssa.Value, verify func(t *testing.T)) {
423 param := addParam(ctx, b, ssa.TypeI64)
424 minus1 := int32(-1)
425 c1 := insertI32Const(ctx, b, uint32(minus1))
426 ext := insertExt(ctx, b, c1.Return(), 32, 64, true)
427 ctx.vRegMap[ext.Arg()] = v2000
428 iadd4 := insertIadd(ctx, b, param, ext.Return())
429 return iadd4.Return(), func(t *testing.T) {
430 for _, instr := range []*ssa.Instruction{ext, iadd4} {
431 require.True(t, instr.Lowered())
432 }
433 }
434 },
435 exp64s: []regalloc.VReg{v1000 /* == param */},
436 offset: -1,
437 },
438 {
439 name: "one 64 value + zero-extended (32->64) const",
440 setup: func(ctx *mockCompiler, b ssa.Builder, m *machine) (ptr ssa.Value, verify func(t *testing.T)) {
441 param := addParam(ctx, b, ssa.TypeI64)
442 minus1 := int32(-1)
443 c1 := insertI32Const(ctx, b, uint32(minus1))
444 ext := insertExt(ctx, b, c1.Return(), 32, 64, false)
445 ctx.vRegMap[ext.Arg()] = v2000
446 iadd4 := insertIadd(ctx, b, param, ext.Return())
447 return iadd4.Return(), func(t *testing.T) {
448 for _, instr := range []*ssa.Instruction{ext, iadd4} {
449 require.True(t, instr.Lowered())
450 }
451 }
452 },
453 exp64s: []regalloc.VReg{v1000 /* == param */},
454 offset: math.MaxUint32, // zero-extended -1
455 },
456 } {
457 tc := tc
458 t.Run(tc.name, func(t *testing.T) {
459 ctx, b, m := newSetupWithMockContext()
460 ptr, verify := tc.setup(ctx, b, m)
461 actual32sQ, actual64sQ, actualOffset := m.collectAddends(ptr)
462 require.Equal(t, tc.exp32s, actual32sQ.Data)
463 require.Equal(t, tc.exp64s, actual64sQ.Data)
464 require.Equal(t, tc.offset, actualOffset)
465 verify(t)
466 })
467 }
468 }
469
470 func TestMachine_addConstToReg64(t *testing.T) {
471 const nextVRegID = 100
472 t.Run("positive imm12", func(t *testing.T) {
473 c := int64(0xaaa)
474 ctx, _, m := newSetupWithMockContext()
475 ctx.vRegCounter = nextVRegID - 1
476 m.addConstToReg64(regalloc.FromRealReg(x15, regalloc.RegTypeInt), c)
477 require.Equal(t, `add x100?, x15, #0xaaa`, formatEmittedInstructionsInCurrentBlock(m))
478 })
479 t.Run("negative imm12", func(t *testing.T) {
480 c := int64(-0xaaa)
481 ctx, _, m := newSetupWithMockContext()
482 ctx.vRegCounter = nextVRegID - 1
483 m.addConstToReg64(regalloc.FromRealReg(x15, regalloc.RegTypeInt), c)
484 require.Equal(t, `sub x100?, x15, #0xaaa`, formatEmittedInstructionsInCurrentBlock(m))
485 })
486 t.Run("non imm12", func(t *testing.T) {
487 c := int64(1<<32 | 1)
488 ctx, _, m := newSetupWithMockContext()
489 ctx.vRegCounter = nextVRegID - 1
490 m.addConstToReg64(regalloc.FromRealReg(x15, regalloc.RegTypeInt), c)
491 require.Equal(t, `movz x101?, #0x1, lsl 0
492 movk x101?, #0x1, lsl 32
493 add x100?, x15, x101?`, formatEmittedInstructionsInCurrentBlock(m))
494 })
495 }
496
497 func TestMachine_addReg64ToReg64(t *testing.T) {
498 const nextVRegID = 100
499 for _, tc := range []struct {
500 exp string
501 rn, rm regalloc.VReg
502 }{
503 {
504 exp: "add x100?, x0, x1",
505 rn: regalloc.FromRealReg(x0, regalloc.RegTypeInt),
506 rm: regalloc.FromRealReg(x1, regalloc.RegTypeInt),
507 },
508 {
509 exp: "add x100?, x10, x12",
510 rn: regalloc.FromRealReg(x10, regalloc.RegTypeInt),
511 rm: regalloc.FromRealReg(x12, regalloc.RegTypeInt),
512 },
513 } {
514 tc := tc
515 t.Run(tc.exp, func(t *testing.T) {
516 ctx, _, m := newSetupWithMockContext()
517 ctx.vRegCounter = nextVRegID - 1
518 rd := m.addReg64ToReg64(tc.rn, tc.rm)
519 require.Equal(t, tc.exp, formatEmittedInstructionsInCurrentBlock(m))
520 require.Equal(t, rd, regalloc.VReg(nextVRegID).SetRegType(regalloc.RegTypeInt))
521 })
522 }
523 }
524
525 func TestMachine_addRegToReg64Ext(t *testing.T) {
526 const nextVRegID = 100
527 for _, tc := range []struct {
528 exp string
529 rn, rm regalloc.VReg
530 ext extendOp
531 }{
532 {
533 exp: "add x100?, x0, w1 UXTW",
534 rn: regalloc.FromRealReg(x0, regalloc.RegTypeInt),
535 rm: regalloc.FromRealReg(x1, regalloc.RegTypeInt),
536 ext: extendOpUXTW,
537 },
538 {
539 exp: "add x100?, x0, w1 SXTW",
540 rn: regalloc.FromRealReg(x0, regalloc.RegTypeInt),
541 rm: regalloc.FromRealReg(x1, regalloc.RegTypeInt),
542 ext: extendOpSXTW,
543 },
544 } {
545 tc := tc
546 t.Run(tc.exp, func(t *testing.T) {
547 ctx, _, m := newSetupWithMockContext()
548 ctx.vRegCounter = nextVRegID - 1
549 rd := m.addRegToReg64Ext(tc.rn, tc.rm, tc.ext)
550 require.Equal(t, tc.exp, formatEmittedInstructionsInCurrentBlock(m))
551 require.Equal(t, rd, regalloc.VReg(nextVRegID).SetRegType(regalloc.RegTypeInt))
552 })
553 }
554 }
555
556 func TestMachine_lowerToAddressModeFromAddends(t *testing.T) {
557 x1, x2, x3 := regalloc.FromRealReg(x1, regalloc.RegTypeInt), regalloc.FromRealReg(x2, regalloc.RegTypeInt), regalloc.FromRealReg(x3, regalloc.RegTypeInt)
558 x4, x5, x6 := regalloc.FromRealReg(x4, regalloc.RegTypeInt), regalloc.FromRealReg(x5, regalloc.RegTypeInt), regalloc.FromRealReg(x6, regalloc.RegTypeInt)
559
560 nextVReg, nextNextVeg := regalloc.VReg(100).SetRegType(regalloc.RegTypeInt), regalloc.VReg(101).SetRegType(regalloc.RegTypeInt)
561 for _, tc := range []struct {
562 name string
563 a32s []addend32
564 a64s []regalloc.VReg
565 dstSizeInBits byte
566 offset int64
567 exp addressMode
568 insts []string
569 }{
570 {
571 name: "only offset",
572 offset: 4095,
573 insts: []string{"orr x100?, xzr, #0xfff"},
574 exp: addressMode{kind: addressModeKindRegUnsignedImm12, rn: nextVReg, imm: 0},
575 },
576 {
577 name: "only offset",
578 offset: 4095 << 12,
579 insts: []string{"orr x100?, xzr, #0xfff000"},
580 exp: addressMode{kind: addressModeKindRegUnsignedImm12, rn: nextVReg, imm: 0},
581 },
582 {
583 name: "one a64 with imm12",
584 a64s: []regalloc.VReg{x1},
585 offset: 4095,
586 dstSizeInBits: 8,
587 exp: addressMode{kind: addressModeKindRegUnsignedImm12, rn: x1, imm: 4095},
588 },
589 {
590 name: "one a64 with imm12",
591 a64s: []regalloc.VReg{x1},
592 offset: 4095 * 2,
593 dstSizeInBits: 16,
594 exp: addressMode{kind: addressModeKindRegUnsignedImm12, rn: x1, imm: 4095 * 2},
595 },
596 {
597 name: "one a64 with imm12",
598 a64s: []regalloc.VReg{x1},
599 offset: 4095 * 4,
600 dstSizeInBits: 32,
601 exp: addressMode{kind: addressModeKindRegUnsignedImm12, rn: x1, imm: 4095 * 4},
602 },
603 {
604 name: "one a64 with imm12",
605 a64s: []regalloc.VReg{x1},
606 offset: 4095 * 8,
607 dstSizeInBits: 64,
608 exp: addressMode{kind: addressModeKindRegUnsignedImm12, rn: x1, imm: 4095 * 8},
609 },
610 {
611 name: "one a64 with imm9",
612 a64s: []regalloc.VReg{x1},
613 dstSizeInBits: 64,
614 exp: addressMode{kind: addressModeKindRegSignedImm9, rn: x1, imm: 0},
615 },
616 {
617 name: "one a64 with imm9",
618 a64s: []regalloc.VReg{x1},
619 offset: -256,
620 dstSizeInBits: 64,
621 exp: addressMode{kind: addressModeKindRegSignedImm9, rn: x1, imm: -256},
622 },
623 {
624 name: "one a64 with offset not fitting",
625 a64s: []regalloc.VReg{x1},
626 offset: 1 << 16,
627 dstSizeInBits: 64,
628 insts: []string{"add x100?, x1, #0x10000"},
629 exp: addressMode{kind: addressModeKindRegUnsignedImm12, rn: nextVReg, imm: 0},
630 },
631 {
632 name: "two a64 with imm12",
633 a64s: []regalloc.VReg{x1, x2},
634 offset: 4095,
635 dstSizeInBits: 8,
636 insts: []string{"add x100?, x1, x2"},
637 exp: addressMode{kind: addressModeKindRegUnsignedImm12, rn: nextVReg, imm: 4095},
638 },
639 {
640 name: "two a64 with imm12",
641 a64s: []regalloc.VReg{x1, x2},
642 offset: 4095 * 2,
643 dstSizeInBits: 16,
644 insts: []string{"add x100?, x1, x2"},
645 exp: addressMode{kind: addressModeKindRegUnsignedImm12, rn: nextVReg, imm: 4095 * 2},
646 },
647 {
648 name: "two a64 with imm12",
649 a64s: []regalloc.VReg{x1, x2},
650 offset: 4095 * 4,
651 dstSizeInBits: 32,
652 insts: []string{"add x100?, x1, x2"},
653 exp: addressMode{kind: addressModeKindRegUnsignedImm12, rn: nextVReg, imm: 4095 * 4},
654 },
655 {
656 name: "two a64 with imm12",
657 a64s: []regalloc.VReg{x1, x2},
658 offset: 4095 * 8,
659 dstSizeInBits: 64,
660 insts: []string{"add x100?, x1, x2"},
661 exp: addressMode{kind: addressModeKindRegUnsignedImm12, rn: nextVReg, imm: 4095 * 8},
662 },
663 {
664 name: "two a64 with imm9",
665 a64s: []regalloc.VReg{x1, x2},
666 dstSizeInBits: 64,
667 insts: []string{"add x100?, x1, x2"},
668 exp: addressMode{kind: addressModeKindRegSignedImm9, rn: nextVReg, imm: 0},
669 },
670 {
671 name: "two a64 with imm9",
672 a64s: []regalloc.VReg{x1, x2},
673 offset: -256,
674 dstSizeInBits: 64,
675 insts: []string{"add x100?, x1, x2"},
676 exp: addressMode{kind: addressModeKindRegSignedImm9, rn: nextVReg, imm: -256},
677 },
678 {
679 name: "two a64 with offset not fitting",
680 a64s: []regalloc.VReg{x1, x2},
681 offset: 1 << 16,
682 dstSizeInBits: 64,
683 insts: []string{"add x100?, x1, #0x10000"},
684 exp: addressMode{kind: addressModeKindRegReg, rn: nextVReg, rm: x2, extOp: extendOpUXTX},
685 },
686 {
687 name: "three a64 with imm12",
688 a64s: []regalloc.VReg{x1, x2, x3},
689 offset: 4095 * 2,
690 dstSizeInBits: 16,
691 insts: []string{
692 "add x100?, x1, x2",
693 "add x101?, x100?, x3",
694 },
695 exp: addressMode{kind: addressModeKindRegUnsignedImm12, rn: nextNextVeg, imm: 4095 * 2},
696 },
697 {
698 name: "three a64 with imm12",
699 a64s: []regalloc.VReg{x1, x2, x3},
700 offset: 4095 * 4,
701 dstSizeInBits: 32,
702 insts: []string{
703 "add x100?, x1, x2",
704 "add x101?, x100?, x3",
705 },
706 exp: addressMode{kind: addressModeKindRegUnsignedImm12, rn: nextNextVeg, imm: 4095 * 4},
707 },
708 {
709 name: "three a64 with imm12",
710 a64s: []regalloc.VReg{x1, x2, x3},
711 offset: 4095 * 8,
712 dstSizeInBits: 64,
713 insts: []string{
714 "add x100?, x1, x2",
715 "add x101?, x100?, x3",
716 },
717 exp: addressMode{kind: addressModeKindRegUnsignedImm12, rn: nextNextVeg, imm: 4095 * 8},
718 },
719 {
720 name: "three a64 with imm9",
721 a64s: []regalloc.VReg{x1, x2, x3},
722 dstSizeInBits: 64,
723 insts: []string{
724 "add x100?, x1, x2",
725 "add x101?, x100?, x3",
726 },
727 exp: addressMode{kind: addressModeKindRegSignedImm9, rn: nextNextVeg, imm: 0},
728 },
729 {
730 name: "three a64 with imm9",
731 a64s: []regalloc.VReg{x1, x2, x3},
732 offset: -256,
733 dstSizeInBits: 64,
734 insts: []string{
735 "add x100?, x1, x2",
736 "add x101?, x100?, x3",
737 },
738 exp: addressMode{kind: addressModeKindRegSignedImm9, rn: nextNextVeg, imm: -256},
739 },
740 {
741 name: "three a64 with offset not fitting",
742 a64s: []regalloc.VReg{x1, x2, x3},
743 offset: 1 << 16,
744 dstSizeInBits: 64,
745 insts: []string{
746 "add x100?, x1, #0x10000",
747 "add x101?, x100?, x3",
748 },
749 exp: addressMode{kind: addressModeKindRegReg, rn: nextNextVeg, rm: x2, extOp: extendOpUXTX},
750 },
751 {
752 name: "three a32/a64 with offset",
753 a64s: []regalloc.VReg{x1, x2, x3},
754 a32s: []addend32{{r: x4, ext: extendOpSXTW}, {r: x5, ext: extendOpUXTW}, {r: x6, ext: extendOpSXTW}},
755 offset: 1 << 16,
756 dstSizeInBits: 64,
757 insts: []string{
758 "add x100?, x1, #0x10000",
759 "add x101?, x100?, x2",
760 "add x102?, x101?, x3",
761 "add x103?, x102?, w5 UXTW",
762 "add x104?, x103?, w6 SXTW",
763 },
764 exp: addressMode{
765 kind: addressModeKindRegExtended,
766 rn: regalloc.VReg(104).SetRegType(regalloc.RegTypeInt),
767 rm: x4, extOp: extendOpSXTW,
768 },
769 },
770 {
771 name: "one a32 with offset",
772 a32s: []addend32{{r: x1, ext: extendOpSXTW}},
773 offset: 1 << 16,
774 insts: []string{
775 "sxtw x100?, w1",
776 "add x101?, x100?, #0x10000",
777 },
778 exp: addressMode{kind: addressModeKindRegUnsignedImm12, rn: nextNextVeg, imm: 0},
779 },
780 {
781 name: "two a32s with offset",
782 a32s: []addend32{{r: x1, ext: extendOpSXTW}, {r: x2, ext: extendOpUXTW}},
783 offset: 1 << 16,
784 insts: []string{
785 "sxtw x100?, w1",
786 "add x101?, x100?, #0x10000",
787 },
788 exp: addressMode{
789 kind: addressModeKindRegExtended,
790 rn: regalloc.VReg(101).SetRegType(regalloc.RegTypeInt),
791 rm: x2,
792 imm: 0,
793 extOp: extendOpUXTW,
794 },
795 },
796 } {
797 t.Run(tc.name, func(t *testing.T) {
798 ctx, _, m := newSetupWithMockContext()
799 ctx.vRegCounter = int(nextVReg.ID()) - 1
800
801 var a32s wazevoapi.Queue[addend32]
802 var a64s wazevoapi.Queue[regalloc.VReg]
803 for _, a32 := range tc.a32s {
804 a32s.Enqueue(a32)
805 }
806 for _, a64 := range tc.a64s {
807 a64s.Enqueue(a64)
808 }
809 actual := m.lowerToAddressModeFromAddends(&a32s, &a64s, tc.dstSizeInBits, tc.offset)
810 require.Equal(t, strings.Join(tc.insts, "\n"), formatEmittedInstructionsInCurrentBlock(m))
811 require.Equal(t, &tc.exp, actual, actual.format(tc.dstSizeInBits))
812 })
813 }
814 }
815
816 func Test_extLoadSizeSign(t *testing.T) {
817 for _, tc := range []struct {
818 op ssa.Opcode
819 expSize byte
820 signed bool
821 }{
822 {op: ssa.OpcodeUload8, expSize: 8, signed: false},
823 {op: ssa.OpcodeUload16, expSize: 16, signed: false},
824 {op: ssa.OpcodeUload32, expSize: 32, signed: false},
825 {op: ssa.OpcodeSload8, expSize: 8, signed: true},
826 {op: ssa.OpcodeSload16, expSize: 16, signed: true},
827 {op: ssa.OpcodeSload32, expSize: 32, signed: true},
828 } {
829 size, signed := extLoadSignSize(tc.op)
830 require.Equal(t, tc.expSize, size)
831 require.Equal(t, tc.signed, signed)
832 }
833 }