Debian Codegraph @

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

raw
1 package arm64
2
3 import (
4 "strings"
5 "testing"
6
7 "github.com/tetratelabs/wazero/internal/engine/wazevo/backend"
8 "github.com/tetratelabs/wazero/internal/engine/wazevo/backend/regalloc"
9 "github.com/tetratelabs/wazero/internal/engine/wazevo/ssa"
10 "github.com/tetratelabs/wazero/internal/testing/require"
11 )
12
13 func Test_asImm12(t *testing.T) {
14 v, shift, ok := asImm12(0xfff)
15 require.True(t, ok)
16 require.True(t, shift == 0)
17 require.Equal(t, uint16(0xfff), v)
18
19 _, _, ok = asImm12(0xfff << 1)
20 require.False(t, ok)
21
22 v, shift, ok = asImm12(0xabc << 12)
23 require.True(t, ok)
24 require.True(t, shift == 1)
25 require.Equal(t, uint16(0xabc), v)
26
27 _, _, ok = asImm12(0xabc<<12 | 0b1)
28 require.False(t, ok)
29 }
30
31 func TestMachine_getOperand_NR(t *testing.T) {
32 for _, tc := range []struct {
33 name string
34 setup func(*mockCompiler, ssa.Builder, *machine) (def backend.SSAValueDefinition, mode extMode)
35 exp operand
36 instructions []string
37 }{
38 {
39 name: "block param - no extend",
40 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) (def backend.SSAValueDefinition, mode extMode) {
41 blk := builder.CurrentBlock()
42 v := blk.AddParam(builder, ssa.TypeI64)
43 ctx.vRegMap[v] = regToVReg(x4)
44 def = backend.SSAValueDefinition{V: v}
45 return def, extModeZeroExtend64
46 },
47 exp: operandNR(regToVReg(x4)),
48 },
49 {
50 name: "block param - zero extend",
51 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) (def backend.SSAValueDefinition, mode extMode) {
52 blk := builder.CurrentBlock()
53 v := blk.AddParam(builder, ssa.TypeI32)
54 ctx.vRegMap[v] = regToVReg(x4)
55 def = backend.SSAValueDefinition{V: v}
56 return def, extModeZeroExtend64
57 },
58 exp: operandNR(regalloc.VReg(1).SetRegType(regalloc.RegTypeInt)),
59 instructions: []string{"uxtw x1?, w4"},
60 },
61 {
62 name: "block param - sign extend",
63 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) (def backend.SSAValueDefinition, mode extMode) {
64 blk := builder.CurrentBlock()
65 v := blk.AddParam(builder, ssa.TypeI32)
66 ctx.vRegMap[v] = regToVReg(x4)
67 def = backend.SSAValueDefinition{V: v}
68 return def, extModeSignExtend64
69 },
70 exp: operandNR(regalloc.VReg(1).SetRegType(regalloc.RegTypeInt)),
71 instructions: []string{"sxtw x1?, w4"},
72 },
73 {
74 name: "const instr",
75 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) (def backend.SSAValueDefinition, mode extMode) {
76 instr := builder.AllocateInstruction()
77 instr.AsIconst32(0xf00000f)
78 builder.InsertInstruction(instr)
79 def = backend.SSAValueDefinition{Instr: instr, V: instr.Return()}
80 ctx.vRegCounter = 99
81 return
82 },
83 exp: operandNR(regalloc.VReg(100).SetRegType(regalloc.RegTypeInt)),
84 instructions: []string{
85 "movz w100?, #0xf, lsl 0",
86 "movk w100?, #0xf00, lsl 16",
87 },
88 },
89 {
90 name: "non const instr",
91 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) (def backend.SSAValueDefinition, mode extMode) {
92 c := builder.AllocateInstruction()
93 sig := &ssa.Signature{Results: []ssa.Type{ssa.TypeI64, ssa.TypeF64, ssa.TypeF64}}
94 builder.DeclareSignature(sig)
95 c.AsCall(ssa.FuncRef(0), sig, ssa.ValuesNil)
96 builder.InsertInstruction(c)
97 _, rs := c.Returns()
98 ctx.vRegMap[rs[1]] = regalloc.VReg(50)
99 def = backend.SSAValueDefinition{Instr: c, V: rs[1]}
100 return
101 },
102 exp: operandNR(regalloc.VReg(50)),
103 },
104 } {
105 t.Run(tc.name, func(t *testing.T) {
106 ctx, b, m := newSetupWithMockContext()
107 def, mode := tc.setup(ctx, b, m)
108 actual := m.getOperand_NR(def, mode)
109 require.Equal(t, tc.exp, actual)
110 require.Equal(t, strings.Join(tc.instructions, "\n"), formatEmittedInstructionsInCurrentBlock(m))
111 })
112 }
113 }
114
115 func TestMachine_getOperand_SR_NR(t *testing.T) {
116 ishlWithConstAmount := func(
117 ctx *mockCompiler, builder ssa.Builder, m *machine,
118 ) (def backend.SSAValueDefinition, mode extMode, verify func(t *testing.T)) {
119 blk := builder.CurrentBlock()
120 // (p1+p2) << amount
121 p1 := blk.AddParam(builder, ssa.TypeI64)
122 p2 := blk.AddParam(builder, ssa.TypeI64)
123 add := builder.AllocateInstruction()
124 add.AsIadd(p1, p2)
125 builder.InsertInstruction(add)
126 addResult := add.Return()
127
128 amount := builder.AllocateInstruction()
129 amount.AsIconst32(14)
130 builder.InsertInstruction(amount)
131
132 amountVal := amount.Return()
133
134 ishl := builder.AllocateInstruction()
135 ishl.AsIshl(addResult, amountVal)
136 builder.InsertInstruction(ishl)
137
138 ctx.vRegMap[p1] = regalloc.VReg(1)
139 ctx.definitions[p1] = backend.SSAValueDefinition{V: p1}
140 ctx.vRegMap[p2] = regalloc.VReg(2)
141 ctx.definitions[p2] = backend.SSAValueDefinition{V: p2}
142 ctx.definitions[addResult] = backend.SSAValueDefinition{Instr: add, V: addResult}
143 ctx.definitions[amountVal] = backend.SSAValueDefinition{Instr: amount, V: amountVal}
144
145 ctx.vRegMap[addResult] = regalloc.VReg(1234)
146 ctx.vRegMap[ishl.Return()] = regalloc.VReg(10)
147 def = backend.SSAValueDefinition{Instr: ishl, V: ishl.Return()}
148 mode = extModeNone
149 verify = func(t *testing.T) {
150 require.True(t, ishl.Lowered())
151 require.True(t, amount.Lowered())
152 }
153 return
154 }
155
156 for _, tc := range []struct {
157 name string
158 setup func(*mockCompiler, ssa.Builder, *machine) (def backend.SSAValueDefinition, mode extMode, verify func(t *testing.T))
159 exp operand
160 instructions []string
161 }{
162 {
163 name: "block param",
164 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) (def backend.SSAValueDefinition, mode extMode, verify func(t *testing.T)) {
165 blk := builder.CurrentBlock()
166 v := blk.AddParam(builder, ssa.TypeI64)
167 ctx.vRegMap[v] = regToVReg(x4)
168 def = backend.SSAValueDefinition{V: v}
169 return def, extModeNone, func(t *testing.T) {}
170 },
171 exp: operandNR(regToVReg(x4)),
172 },
173 {
174 name: "ishl but not const amount",
175 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) (def backend.SSAValueDefinition, mode extMode, verify func(t *testing.T)) {
176 blk := builder.CurrentBlock()
177 // (p1+p2) << p3
178 p1 := blk.AddParam(builder, ssa.TypeI64)
179 p2 := blk.AddParam(builder, ssa.TypeI64)
180 p3 := blk.AddParam(builder, ssa.TypeI64)
181 add := builder.AllocateInstruction()
182 add.AsIadd(p1, p2)
183 builder.InsertInstruction(add)
184 addResult := add.Return()
185
186 ishl := builder.AllocateInstruction()
187 ishl.AsIshl(addResult, p3)
188 builder.InsertInstruction(ishl)
189
190 ctx.vRegMap[p1] = regalloc.VReg(1)
191 ctx.definitions[p1] = backend.SSAValueDefinition{V: p1}
192 ctx.vRegMap[p2] = regalloc.VReg(2)
193 ctx.definitions[p2] = backend.SSAValueDefinition{V: p2}
194 ctx.vRegMap[p3] = regalloc.VReg(3)
195 ctx.definitions[p3] = backend.SSAValueDefinition{V: p3}
196 ctx.definitions[addResult] = backend.SSAValueDefinition{Instr: add, V: addResult}
197 ctx.vRegMap[addResult] = regalloc.VReg(1234) // whatever is fine.
198 ctx.vRegMap[ishl.Return()] = regalloc.VReg(10)
199 def = backend.SSAValueDefinition{Instr: ishl, V: ishl.Return()}
200 return def, extModeNone, func(t *testing.T) {}
201 },
202 exp: operandNR(regalloc.VReg(10)),
203 },
204 {
205 name: "ishl with const amount",
206 setup: ishlWithConstAmount,
207 exp: operandSR(regalloc.VReg(1234), 14, shiftOpLSL),
208 },
209 {
210 name: "ishl with const amount with i32",
211 setup: func(
212 ctx *mockCompiler, builder ssa.Builder, m *machine,
213 ) (def backend.SSAValueDefinition, mode extMode, verify func(t *testing.T)) {
214 blk := builder.CurrentBlock()
215 // (p1+p2) << amount
216 p1 := blk.AddParam(builder, ssa.TypeI32)
217 p2 := blk.AddParam(builder, ssa.TypeI32)
218 add := builder.AllocateInstruction()
219 add.AsIadd(p1, p2)
220 builder.InsertInstruction(add)
221 addResult := add.Return()
222
223 amount := builder.AllocateInstruction()
224 amount.AsIconst32(45) // should be taken modulo by 31.
225 builder.InsertInstruction(amount)
226
227 amountVal := amount.Return()
228
229 ishl := builder.AllocateInstruction()
230 ishl.AsIshl(addResult, amountVal)
231 builder.InsertInstruction(ishl)
232
233 ctx.vRegMap[p1] = regalloc.VReg(1)
234 ctx.definitions[p1] = backend.SSAValueDefinition{V: p1}
235 ctx.vRegMap[p2] = regalloc.VReg(2)
236 ctx.definitions[p2] = backend.SSAValueDefinition{V: p2}
237 ctx.definitions[addResult] = backend.SSAValueDefinition{Instr: add, V: addResult}
238 ctx.definitions[amountVal] = backend.SSAValueDefinition{Instr: amount, V: amountVal}
239
240 ctx.vRegMap[addResult] = regalloc.VReg(1234)
241 ctx.vRegMap[ishl.Return()] = regalloc.VReg(10)
242 def = backend.SSAValueDefinition{Instr: ishl}
243 mode = extModeNone
244 verify = func(t *testing.T) {
245 require.True(t, ishl.Lowered())
246 require.True(t, amount.Lowered())
247 }
248 return
249 },
250 exp: operandSR(regalloc.VReg(1234), 13, shiftOpLSL),
251 },
252 {
253 name: "ishl with const amount with const shift target",
254 setup: func(
255 ctx *mockCompiler, builder ssa.Builder, m *machine,
256 ) (def backend.SSAValueDefinition, mode extMode, verify func(t *testing.T)) {
257 const nextVReg = 100
258 ctx.vRegCounter = nextVReg - 1
259
260 target := builder.AllocateInstruction().AsIconst64(0xff).Insert(builder)
261 amount := builder.AllocateInstruction().AsIconst32(14).Insert(builder)
262 targetVal, amountVal := target.Return(), amount.Return()
263
264 ishl := builder.AllocateInstruction()
265 ishl.AsIshl(target.Return(), amountVal)
266 builder.InsertInstruction(ishl)
267
268 ctx.definitions[targetVal] = backend.SSAValueDefinition{Instr: target, V: targetVal}
269 ctx.definitions[amountVal] = backend.SSAValueDefinition{Instr: amount, V: amountVal}
270 ctx.vRegMap[targetVal] = regalloc.VReg(1234)
271 ctx.vRegMap[ishl.Return()] = regalloc.VReg(10)
272 def = backend.SSAValueDefinition{Instr: ishl}
273 mode = extModeNone
274 verify = func(t *testing.T) {
275 require.True(t, ishl.Lowered())
276 require.True(t, amount.Lowered())
277 }
278 return
279 },
280 exp: operandSR(regalloc.VReg(100).SetRegType(regalloc.RegTypeInt), 14, shiftOpLSL),
281 instructions: []string{"orr x100?, xzr, #0xff"},
282 },
283 {
284 name: "ishl with const amount but group id is different",
285 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) (def backend.SSAValueDefinition, mode extMode, verify func(t *testing.T)) {
286 def, mode, _ = ishlWithConstAmount(ctx, builder, m)
287 ctx.currentGID = 1230
288 return
289 },
290 exp: operandNR(regalloc.VReg(10)),
291 },
292 {
293 name: "ishl with const amount but ref count is larger than 1",
294 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) (def backend.SSAValueDefinition, mode extMode, verify func(t *testing.T)) {
295 def, mode, _ = ishlWithConstAmount(ctx, builder, m)
296 def.RefCount = 10
297 return
298 },
299 exp: operandNR(regalloc.VReg(10)),
300 },
301 } {
302 t.Run(tc.name, func(t *testing.T) {
303 ctx, b, m := newSetupWithMockContext()
304 def, mode, verify := tc.setup(ctx, b, m)
305 actual := m.getOperand_SR_NR(def, mode)
306 require.Equal(t, tc.exp, actual)
307 if verify != nil {
308 verify(t)
309 }
310 require.Equal(t, strings.Join(tc.instructions, "\n"), formatEmittedInstructionsInCurrentBlock(m))
311 })
312 }
313 }
314
315 func TestMachine_getOperand_ER_SR_NR(t *testing.T) {
316 const nextVReg = 100
317 type testCase struct {
318 setup func(*mockCompiler, ssa.Builder, *machine) (def backend.SSAValueDefinition, mode extMode, verify func(t *testing.T))
319 exp operand
320 instructions []string
321 }
322 runner := func(tc testCase) {
323 ctx, b, m := newSetupWithMockContext()
324 ctx.vRegCounter = nextVReg - 1
325 def, mode, verify := tc.setup(ctx, b, m)
326 actual := m.getOperand_ER_SR_NR(def, mode)
327 require.Equal(t, tc.exp, actual)
328 verify(t)
329 require.Equal(t, strings.Join(tc.instructions, "\n"), formatEmittedInstructionsInCurrentBlock(m))
330 }
331
332 t.Run("block param", func(t *testing.T) {
333 runner(testCase{
334 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) (def backend.SSAValueDefinition, mode extMode, verify func(t *testing.T)) {
335 blk := builder.CurrentBlock()
336 v := blk.AddParam(builder, ssa.TypeI64)
337 ctx.vRegMap[v] = regToVReg(x4)
338 def = backend.SSAValueDefinition{V: v}
339 return def, extModeZeroExtend64, func(t *testing.T) {}
340 },
341 exp: operandNR(regToVReg(x4)),
342 })
343 })
344
345 t.Run("mode none", func(t *testing.T) {
346 for _, c := range []struct {
347 from, to byte
348 signed bool
349 exp operand
350 }{
351 {from: 8, to: 32, signed: true, exp: operandER(regalloc.VReg(10), extendOpSXTB, 32)},
352 {from: 16, to: 32, signed: true, exp: operandER(regalloc.VReg(10), extendOpSXTH, 32)},
353 {from: 8, to: 32, signed: false, exp: operandER(regalloc.VReg(10), extendOpUXTB, 32)},
354 {from: 16, to: 32, signed: false, exp: operandER(regalloc.VReg(10), extendOpUXTH, 32)},
355 {from: 8, to: 64, signed: true, exp: operandER(regalloc.VReg(10), extendOpSXTB, 64)},
356 {from: 16, to: 64, signed: true, exp: operandER(regalloc.VReg(10), extendOpSXTH, 64)},
357 {from: 32, to: 64, signed: true, exp: operandER(regalloc.VReg(10), extendOpSXTW, 64)},
358 {from: 8, to: 64, signed: false, exp: operandER(regalloc.VReg(10), extendOpUXTB, 64)},
359 {from: 16, to: 64, signed: false, exp: operandER(regalloc.VReg(10), extendOpUXTH, 64)},
360 {from: 32, to: 64, signed: false, exp: operandER(regalloc.VReg(10), extendOpUXTW, 64)},
361 } {
362 runner(testCase{
363 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) (def backend.SSAValueDefinition, mode extMode, verify func(t *testing.T)) {
364 blk := builder.CurrentBlock()
365 v := blk.AddParam(builder, ssa.TypeI64)
366 ext := builder.AllocateInstruction()
367 if c.signed {
368 ext.AsSExtend(v, c.from, c.to)
369 } else {
370 ext.AsUExtend(v, c.from, c.to)
371 }
372 builder.InsertInstruction(ext)
373 extArg := ext.Arg()
374 ctx.vRegMap[extArg] = regalloc.VReg(10)
375 ctx.definitions[v] = backend.SSAValueDefinition{V: extArg}
376 def = backend.SSAValueDefinition{Instr: ext, V: ext.Return()}
377 return def, extModeNone, func(t *testing.T) {
378 require.True(t, ext.Lowered())
379 }
380 },
381 exp: c.exp,
382 })
383 }
384 })
385
386 t.Run("valid mode", func(t *testing.T) {
387 const argVReg, resultVReg = regalloc.VReg(10), regalloc.VReg(11)
388 for _, c := range []struct {
389 name string
390 from, to byte
391 signed bool
392 mode extMode
393 exp operand
394 lowered bool
395 extArgConst bool
396 instructions []string
397 }{
398 {
399 name: "8->16->32: signed",
400 from: 8, to: 16, signed: true, mode: extModeSignExtend32,
401 exp: operandER(argVReg, extendOpSXTB, 32),
402 lowered: true,
403 },
404 {
405 name: "8->16->32: unsigned",
406 from: 8, to: 16, signed: false, mode: extModeZeroExtend32,
407 exp: operandER(argVReg, extendOpUXTB, 32),
408 lowered: true,
409 },
410 {
411 name: "8->32->64: signed",
412 from: 8, to: 32, signed: true, mode: extModeSignExtend64,
413 exp: operandER(argVReg, extendOpSXTB, 64),
414 lowered: true,
415 },
416 {
417 name: "16->32->64: signed",
418 from: 16, to: 32, signed: true, mode: extModeSignExtend64,
419 exp: operandER(argVReg, extendOpSXTH, 64),
420 lowered: true,
421 },
422 {
423 name: "8->32->64: unsigned",
424 from: 8, to: 32, signed: false, mode: extModeZeroExtend64,
425 exp: operandER(argVReg, extendOpUXTB, 64),
426 lowered: true,
427 },
428 {
429 name: "16->32->64: unsigned",
430 from: 16, to: 32, signed: false, mode: extModeZeroExtend64,
431 exp: operandER(argVReg, extendOpUXTH, 64),
432 lowered: true,
433 },
434 {
435 name: "8->16->64: signed",
436 from: 8, to: 16, signed: true, mode: extModeSignExtend64,
437 exp: operandER(argVReg, extendOpSXTB, 64),
438 lowered: true,
439 },
440 {
441 name: "8->16->64: unsigned",
442 from: 8, to: 16, signed: false, mode: extModeZeroExtend64,
443 exp: operandER(argVReg, extendOpUXTB, 64),
444 lowered: true,
445 },
446 {
447 name: "8(VReg)->64->64: unsigned",
448 from: 8, to: 64, signed: false, mode: extModeZeroExtend64,
449 exp: operandER(argVReg, extendOpUXTB, 64),
450 lowered: true,
451 },
452 {
453 name: "8(VReg,Const)->64->64: unsigned",
454 from: 8, to: 64, signed: false, mode: extModeZeroExtend64,
455 exp: operandER(regalloc.VReg(nextVReg).SetRegType(regalloc.RegTypeInt), extendOpUXTB, 64),
456 lowered: true,
457 extArgConst: true,
458 instructions: []string{"movz w100?, #0xffff, lsl 0"},
459 },
460 {
461 name: "16(VReg)->64->64: unsigned",
462 from: 16, to: 64, signed: false, mode: extModeZeroExtend64,
463 exp: operandER(argVReg, extendOpUXTH, 64),
464 lowered: true,
465 },
466 {
467 name: "16(VReg,Const)->64->64: unsigned",
468 from: 16, to: 64, signed: false, mode: extModeZeroExtend64,
469 exp: operandER(regalloc.VReg(nextVReg).SetRegType(regalloc.RegTypeInt), extendOpUXTH, 64),
470 lowered: true,
471 extArgConst: true,
472 instructions: []string{"movz w100?, #0xffff, lsl 0"},
473 },
474 {
475 name: "32(VReg)->64->64: unsigned",
476 from: 32, to: 64, signed: false, mode: extModeZeroExtend64,
477 exp: operandER(argVReg, extendOpUXTW, 64),
478 lowered: true,
479 },
480 {
481 name: "32(VReg,Const)->64->64: unsigned",
482 from: 32, to: 64, signed: false, mode: extModeZeroExtend64,
483 exp: operandER(regalloc.VReg(nextVReg).SetRegType(regalloc.RegTypeInt), extendOpUXTW, 64),
484 lowered: true,
485 extArgConst: true,
486 instructions: []string{"movz w100?, #0xffff, lsl 0"},
487 },
488 {
489 name: "8(VReg)->64->64: signed",
490 from: 8, to: 64, signed: true, mode: extModeZeroExtend64,
491 exp: operandER(argVReg, extendOpSXTB, 64),
492 lowered: true,
493 },
494 {
495 name: "8(VReg,Const)->64->64: signed",
496 from: 8, to: 64, signed: true, mode: extModeZeroExtend64,
497 exp: operandER(regalloc.VReg(nextVReg).SetRegType(regalloc.RegTypeInt), extendOpSXTB, 64),
498 lowered: true,
499 extArgConst: true,
500 instructions: []string{"movz w100?, #0xffff, lsl 0"},
501 },
502 {
503 name: "16(VReg)->64->64: signed",
504 from: 16, to: 64, signed: true, mode: extModeZeroExtend64,
505 exp: operandER(argVReg, extendOpSXTH, 64),
506 lowered: true,
507 },
508 {
509 name: "16(VReg,Const)->64->64: signed",
510 from: 16, to: 64, signed: true, mode: extModeZeroExtend64,
511 exp: operandER(regalloc.VReg(nextVReg).SetRegType(regalloc.RegTypeInt), extendOpSXTH, 64),
512 lowered: true,
513 extArgConst: true,
514 instructions: []string{"movz w100?, #0xffff, lsl 0"},
515 },
516 {
517 name: "32(VReg)->64->64: signed",
518 from: 32, to: 64, signed: true, mode: extModeZeroExtend64,
519 exp: operandER(argVReg, extendOpSXTW, 64),
520 lowered: true,
521 },
522 {
523 name: "32(VReg,Const)->64->64: signed",
524 from: 32, to: 64, signed: true, mode: extModeZeroExtend64,
525 exp: operandER(regalloc.VReg(nextVReg).SetRegType(regalloc.RegTypeInt), extendOpSXTW, 64),
526 lowered: true,
527 extArgConst: true,
528 instructions: []string{"movz w100?, #0xffff, lsl 0"},
529 },
530
531 // Not lowered cases.
532 {
533 name: "8-signed->16-zero->64",
534 from: 8, to: 16, signed: true, mode: extModeZeroExtend64,
535 exp: operandER(resultVReg, extendOpUXTH, 64),
536 lowered: false,
537 },
538 {
539 name: "8-signed->32-zero->64",
540 from: 8, to: 32, signed: true, mode: extModeZeroExtend64,
541 exp: operandER(resultVReg, extendOpUXTW, 64),
542 lowered: false,
543 },
544 {
545 name: "16-signed->32-zero->64",
546 from: 16, to: 32, signed: true, mode: extModeZeroExtend64,
547 exp: operandER(resultVReg, extendOpUXTW, 64),
548 lowered: false,
549 },
550 {
551 name: "8-zero->16-signed->64",
552 from: 8, to: 16, signed: false, mode: extModeSignExtend64,
553 exp: operandER(resultVReg, extendOpSXTH, 64),
554 lowered: false,
555 },
556 {
557 name: "8-zero->32-signed->64",
558 from: 8, to: 32, signed: false, mode: extModeSignExtend64,
559 exp: operandER(resultVReg, extendOpSXTW, 64),
560 lowered: false,
561 },
562 {
563 name: "16-zero->32-signed->64",
564 from: 16, to: 32, signed: false, mode: extModeSignExtend64,
565 exp: operandER(resultVReg, extendOpSXTW, 64),
566 lowered: false,
567 },
568 } {
569 t.Run(c.name, func(t *testing.T) {
570 runner(testCase{
571 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) (def backend.SSAValueDefinition, mode extMode, verify func(t *testing.T)) {
572 blk := builder.CurrentBlock()
573 v := blk.AddParam(builder, ssa.TypeI64)
574 ext := builder.AllocateInstruction()
575 if c.signed {
576 ext.AsSExtend(v, c.from, c.to)
577 } else {
578 ext.AsUExtend(v, c.from, c.to)
579 }
580 builder.InsertInstruction(ext)
581 extArg := ext.Arg()
582 ctx.vRegMap[extArg] = argVReg
583 ctx.vRegMap[ext.Return()] = resultVReg
584 if c.extArgConst {
585 iconst := builder.AllocateInstruction().AsIconst32(0xffff).Insert(builder)
586 m.compiler.(*mockCompiler).definitions[extArg] = backend.SSAValueDefinition{Instr: iconst, V: iconst.Return()}
587 } else {
588 m.compiler.(*mockCompiler).definitions[extArg] = backend.SSAValueDefinition{V: extArg}
589 }
590 def = backend.SSAValueDefinition{Instr: ext}
591 return def, c.mode, func(t *testing.T) {
592 require.Equal(t, c.lowered, ext.Lowered())
593 }
594 },
595 exp: c.exp,
596 instructions: c.instructions,
597 })
598 })
599 }
600 })
601 }
602
603 func TestMachine_getOperand_Imm12_ER_SR_NR(t *testing.T) {
604 for _, tc := range []struct {
605 name string
606 setup func(*mockCompiler, ssa.Builder, *machine) (def backend.SSAValueDefinition, mode extMode)
607 exp operand
608 instructions []string
609 }{
610 {
611 name: "block param",
612 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) (def backend.SSAValueDefinition, mode extMode) {
613 blk := builder.CurrentBlock()
614 v := blk.AddParam(builder, ssa.TypeI64)
615 ctx.vRegMap[v] = regToVReg(x4)
616 def = backend.SSAValueDefinition{V: v}
617 return def, extModeZeroExtend64
618 },
619 exp: operandNR(regToVReg(x4)),
620 },
621 {
622 name: "const imm 12 no shift",
623 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) (def backend.SSAValueDefinition, mode extMode) {
624 cinst := builder.AllocateInstruction()
625 cinst.AsIconst32(0xfff)
626 builder.InsertInstruction(cinst)
627 def = backend.SSAValueDefinition{Instr: cinst}
628 ctx.currentGID = 0xff // const can be merged anytime, regardless of the group id.
629 return
630 },
631 exp: operandImm12(0xfff, 0),
632 },
633 {
634 name: "const imm 12 with shift bit",
635 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) (def backend.SSAValueDefinition, mode extMode) {
636 cinst := builder.AllocateInstruction()
637 cinst.AsIconst32(0xabc_000)
638 builder.InsertInstruction(cinst)
639 def = backend.SSAValueDefinition{Instr: cinst}
640 ctx.currentGID = 0xff // const can be merged anytime, regardless of the group id.
641 return
642 },
643 exp: operandImm12(0xabc, 1),
644 },
645 } {
646 t.Run(tc.name, func(t *testing.T) {
647 ctx, b, m := newSetupWithMockContext()
648 def, mode := tc.setup(ctx, b, m)
649 actual := m.getOperand_Imm12_ER_SR_NR(def, mode)
650 require.Equal(t, tc.exp, actual)
651 require.Equal(t, strings.Join(tc.instructions, "\n"), formatEmittedInstructionsInCurrentBlock(m))
652 })
653 }
654 }
655
656 func TestMachine_getOperand_MaybeNegatedImm12_ER_SR_NR(t *testing.T) {
657 for _, tc := range []struct {
658 name string
659 setup func(*mockCompiler, ssa.Builder, *machine) (def backend.SSAValueDefinition, mode extMode)
660 exp operand
661 expNegated bool
662 instructions []string
663 }{
664 {
665 name: "block param",
666 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) (def backend.SSAValueDefinition, mode extMode) {
667 blk := builder.CurrentBlock()
668 v := blk.AddParam(builder, ssa.TypeI64)
669 ctx.vRegMap[v] = regToVReg(x4)
670 def = backend.SSAValueDefinition{V: v}
671 return def, extModeZeroExtend64
672 },
673 exp: operandNR(regToVReg(x4)),
674 },
675 {
676 name: "const imm 12 no shift",
677 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) (def backend.SSAValueDefinition, mode extMode) {
678 cinst := builder.AllocateInstruction()
679 cinst.AsIconst32(0xfff)
680 builder.InsertInstruction(cinst)
681 def = backend.SSAValueDefinition{Instr: cinst}
682 ctx.currentGID = 0xff // const can be merged anytime, regardless of the group id.
683 return
684 },
685 exp: operandImm12(0xfff, 0),
686 },
687 {
688 name: "const negative imm 12 no shift",
689 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) (def backend.SSAValueDefinition, mode extMode) {
690 c := int32(-1)
691 cinst := builder.AllocateInstruction()
692 cinst.AsIconst32(uint32(c))
693 builder.InsertInstruction(cinst)
694 def = backend.SSAValueDefinition{Instr: cinst, V: cinst.Return()}
695 ctx.currentGID = 0xff // const can be merged anytime, regardless of the group id.
696 return
697 },
698 exp: operandImm12(1, 0),
699 expNegated: true,
700 },
701 {
702 name: "const imm 12 with shift bit",
703 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) (def backend.SSAValueDefinition, mode extMode) {
704 cinst := builder.AllocateInstruction()
705 cinst.AsIconst32(0xabc_000)
706 builder.InsertInstruction(cinst)
707 def = backend.SSAValueDefinition{Instr: cinst}
708 ctx.currentGID = 0xff // const can be merged anytime, regardless of the group id.
709 return
710 },
711 exp: operandImm12(0xabc, 1),
712 },
713 {
714 name: "const negated imm 12 with shift bit",
715 setup: func(ctx *mockCompiler, builder ssa.Builder, m *machine) (def backend.SSAValueDefinition, mode extMode) {
716 c := int32(-0xabc_000)
717 cinst := builder.AllocateInstruction()
718 cinst.AsIconst32(uint32(c))
719 builder.InsertInstruction(cinst)
720 def = backend.SSAValueDefinition{Instr: cinst, V: cinst.Return()}
721 ctx.currentGID = 0xff // const can be merged anytime, regardless of the group id.
722 return
723 },
724 exp: operandImm12(0xabc, 1),
725 expNegated: true,
726 },
727 } {
728 t.Run(tc.name, func(t *testing.T) {
729 ctx, b, m := newSetupWithMockContext()
730 def, mode := tc.setup(ctx, b, m)
731 actual, negated := m.getOperand_MaybeNegatedImm12_ER_SR_NR(def, mode)
732 require.Equal(t, tc.exp, actual)
733 require.Equal(t, tc.expNegated, negated)
734 require.Equal(t, strings.Join(tc.instructions, "\n"), formatEmittedInstructionsInCurrentBlock(m))
735 })
736 }
737 }