Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Reduce scope of scratch registers for SIMD shifts (#11935)

Jeffrey Charlesb73105bf4c3b
diff --git a/tests/disas/winch/x64/i64x2/shift/shr_s_all_fprs_float_local.wat b/tests/disas/winch/x64/i64x2/shift/shr_s_all_fprs_float_local.wat
new file mode 100644
index 0000000000..22bba722da
--- /dev/null
+++ b/tests/disas/winch/x64/i64x2/shift/shr_s_all_fprs_float_local.wat
@@ -0,0 +1,176 @@
+;;! target = "x86_64"
+;;! test = "winch"
+;;! flags = [ "-Ccranelift-has-avx" ]
+
+(module
+ (func (param f64)
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+
+ local.get 0
+
+ v128.const i64x2 0 0
+ i32.const 0
+ i64x2.shr_s
+ unreachable
+ )
+)
+;; wasm[0]::function[0]:
+;; pushq %rbp
+;; movq %rsp, %rbp
+;; movq 8(%rdi), %r11
+;; movq 0x10(%r11), %r11
+;; addq $0x98, %r11
+;; cmpq %rsp, %r11
+;; ja 0x2c2
+;; 1c: movq %rdi, %r14
+;; subq $0x20, %rsp
+;; movq %rdi, 0x18(%rsp)
+;; movq %rsi, 0x10(%rsp)
+;; movsd %xmm0, 8(%rsp)
+;; movsd 0x28a(%rip), %xmm0
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm0
+;; movsd 0x26e(%rip), %xmm1
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm1
+;; movsd 0x252(%rip), %xmm2
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm2
+;; movsd 0x236(%rip), %xmm3
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm3
+;; movsd 0x21a(%rip), %xmm4
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm4
+;; movsd 0x1fe(%rip), %xmm5
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm5
+;; movsd 0x1e2(%rip), %xmm6
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm6
+;; movsd 0x1c6(%rip), %xmm7
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm7
+;; movsd 0x1a9(%rip), %xmm8
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm8
+;; movsd 0x18c(%rip), %xmm9
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm9
+;; movsd 0x16f(%rip), %xmm10
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm10
+;; movsd 0x152(%rip), %xmm11
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm11
+;; movsd 0x135(%rip), %xmm12
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm12
+;; movsd 0x118(%rip), %xmm13
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm13
+;; movl $0, %eax
+;; movdqu 0xfe(%rip), %xmm14
+;; andl $0x3f, %eax
+;; subq $8, %rsp
+;; movsd %xmm0, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm1, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm2, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm3, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm4, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm5, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm6, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm7, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm8, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm9, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm10, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm11, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm12, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm13, (%rsp)
+;; movsd 0x78(%rsp), %xmm15
+;; subq $8, %rsp
+;; movsd %xmm15, (%rsp)
+;; vmovd %eax, %xmm15
+;; vmovdqu 0x3b(%rip), %xmm0
+;; vpsrlq %xmm15, %xmm0, %xmm0
+;; vpsrlq %xmm15, %xmm14, %xmm14
+;; vpxor %xmm0, %xmm14, %xmm14
+;; vpsubq %xmm0, %xmm14, %xmm14
+;; ud2
+;; addq $0x20, %rsp
+;; popq %rbp
+;; retq
+;; 2c2: ud2
+;; 2c4: addb %al, (%rax)
+;; 2c6: addb %al, (%rax)
+;; 2c8: addb %al, (%rax)
+;; 2ca: addb %al, (%rax)
+;; 2cc: addb %al, (%rax)
+;; 2ce: addb %al, (%rax)
+;; 2d0: addb %al, (%rax)
+;; 2d2: addb %al, (%rax)
+;; 2d4: addb %al, (%rax)
+;; 2d6: addb %al, (%rax)
+;; 2d8: addb %al, (%rax)
+;; 2da: addb %al, (%rax)
+;; 2dc: addb %al, (%rax)
+;; 2de: addb %al, (%rax)
+;; 2e0: addb %al, (%rax)
+;; 2e2: addb %al, (%rax)
+;; 2e4: addb %al, (%rax)
+;; 2e6: addb %al, (%rax)
+;; 2ec: addb %al, (%rax)
diff --git a/tests/disas/winch/x64/i64x2/shift/shr_s_all_fprs_int_local.wat b/tests/disas/winch/x64/i64x2/shift/shr_s_all_fprs_int_local.wat
new file mode 100644
index 0000000000..676bce8247
--- /dev/null
+++ b/tests/disas/winch/x64/i64x2/shift/shr_s_all_fprs_int_local.wat
@@ -0,0 +1,174 @@
+;;! target = "x86_64"
+;;! test = "winch"
+;;! flags = [ "-Ccranelift-has-avx" ]
+
+(module
+ (func (param i64)
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+
+ local.get 0
+
+ v128.const i64x2 0 0
+ i32.const 0
+ i64x2.shr_s
+ unreachable
+ )
+)
+;; wasm[0]::function[0]:
+;; pushq %rbp
+;; movq %rsp, %rbp
+;; movq 8(%rdi), %r11
+;; movq 0x10(%r11), %r11
+;; addq $0x98, %r11
+;; cmpq %rsp, %r11
+;; ja 0x2b4
+;; 1c: movq %rdi, %r14
+;; subq $0x20, %rsp
+;; movq %rdi, 0x18(%rsp)
+;; movq %rsi, 0x10(%rsp)
+;; movq %rdx, 8(%rsp)
+;; movsd 0x27b(%rip), %xmm0
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm0
+;; movsd 0x25f(%rip), %xmm1
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm1
+;; movsd 0x243(%rip), %xmm2
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm2
+;; movsd 0x227(%rip), %xmm3
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm3
+;; movsd 0x20b(%rip), %xmm4
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm4
+;; movsd 0x1ef(%rip), %xmm5
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm5
+;; movsd 0x1d3(%rip), %xmm6
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm6
+;; movsd 0x1b7(%rip), %xmm7
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm7
+;; movsd 0x19a(%rip), %xmm8
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm8
+;; movsd 0x17d(%rip), %xmm9
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm9
+;; movsd 0x160(%rip), %xmm10
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm10
+;; movsd 0x143(%rip), %xmm11
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm11
+;; movsd 0x126(%rip), %xmm12
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm12
+;; movsd 0x109(%rip), %xmm13
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm13
+;; movl $0, %eax
+;; movdqu 0xef(%rip), %xmm14
+;; andl $0x3f, %eax
+;; subq $8, %rsp
+;; movsd %xmm0, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm1, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm2, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm3, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm4, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm5, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm6, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm7, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm8, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm9, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm10, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm11, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm12, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm13, (%rsp)
+;; movq 0x78(%rsp), %r11
+;; pushq %r11
+;; vmovd %eax, %xmm15
+;; vmovdqu 0x39(%rip), %xmm0
+;; vpsrlq %xmm15, %xmm0, %xmm0
+;; vpsrlq %xmm15, %xmm14, %xmm14
+;; vpxor %xmm0, %xmm14, %xmm14
+;; vpsubq %xmm0, %xmm14, %xmm14
+;; ud2
+;; addq $0x20, %rsp
+;; popq %rbp
+;; retq
+;; 2b4: ud2
+;; 2b6: addb %al, (%rax)
+;; 2b8: addb %al, (%rax)
+;; 2ba: addb %al, (%rax)
+;; 2bc: addb %al, (%rax)
+;; 2be: addb %al, (%rax)
+;; 2c0: addb %al, (%rax)
+;; 2c2: addb %al, (%rax)
+;; 2c4: addb %al, (%rax)
+;; 2c6: addb %al, (%rax)
+;; 2c8: addb %al, (%rax)
+;; 2ca: addb %al, (%rax)
+;; 2cc: addb %al, (%rax)
+;; 2ce: addb %al, (%rax)
+;; 2d0: addb %al, (%rax)
+;; 2d2: addb %al, (%rax)
+;; 2d4: addb %al, (%rax)
+;; 2d6: addb %al, (%rax)
+;; 2dc: addb %al, (%rax)
diff --git a/tests/disas/winch/x64/i8x16/shift/shr_s_all_fprs_float_local.wat b/tests/disas/winch/x64/i8x16/shift/shr_s_all_fprs_float_local.wat
new file mode 100644
index 0000000000..ea54587c34
--- /dev/null
+++ b/tests/disas/winch/x64/i8x16/shift/shr_s_all_fprs_float_local.wat
@@ -0,0 +1,170 @@
+;;! target = "x86_64"
+;;! test = "winch"
+;;! flags = [ "-Ccranelift-has-avx" ]
+
+(module
+ (func (param f64)
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+
+ local.get 0
+
+ v128.const i64x2 0 0
+ i32.const 0
+ i8x16.shr_s
+ unreachable
+ )
+)
+;; wasm[0]::function[0]:
+;; pushq %rbp
+;; movq %rsp, %rbp
+;; movq 8(%rdi), %r11
+;; movq 0x10(%r11), %r11
+;; addq $0x98, %r11
+;; cmpq %rsp, %r11
+;; ja 0x2c5
+;; 1c: movq %rdi, %r14
+;; subq $0x20, %rsp
+;; movq %rdi, 0x18(%rsp)
+;; movq %rsi, 0x10(%rsp)
+;; movsd %xmm0, 8(%rsp)
+;; movsd 0x28a(%rip), %xmm0
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm0
+;; movsd 0x26e(%rip), %xmm1
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm1
+;; movsd 0x252(%rip), %xmm2
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm2
+;; movsd 0x236(%rip), %xmm3
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm3
+;; movsd 0x21a(%rip), %xmm4
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm4
+;; movsd 0x1fe(%rip), %xmm5
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm5
+;; movsd 0x1e2(%rip), %xmm6
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm6
+;; movsd 0x1c6(%rip), %xmm7
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm7
+;; movsd 0x1a9(%rip), %xmm8
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm8
+;; movsd 0x18c(%rip), %xmm9
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm9
+;; movsd 0x16f(%rip), %xmm10
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm10
+;; movsd 0x152(%rip), %xmm11
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm11
+;; movsd 0x135(%rip), %xmm12
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm12
+;; movsd 0x118(%rip), %xmm13
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm13
+;; movl $0, %eax
+;; movdqu 0xfe(%rip), %xmm14
+;; andl $7, %eax
+;; subq $8, %rsp
+;; movsd %xmm0, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm1, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm2, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm3, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm4, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm5, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm6, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm7, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm8, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm9, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm10, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm11, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm12, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm13, (%rsp)
+;; movsd 0x78(%rsp), %xmm15
+;; subq $8, %rsp
+;; movsd %xmm15, (%rsp)
+;; addl $8, %eax
+;; vmovd %eax, %xmm15
+;; vpunpcklbw %xmm14, %xmm14, %xmm0
+;; vpunpckhbw %xmm14, %xmm14, %xmm1
+;; vpsraw %xmm15, %xmm0, %xmm0
+;; vpsraw %xmm15, %xmm1, %xmm1
+;; vpacksswb %xmm1, %xmm0, %xmm14
+;; ud2
+;; addq $0x20, %rsp
+;; popq %rbp
+;; retq
+;; 2c5: ud2
+;; 2c7: addb %al, (%rax)
+;; 2c9: addb %al, (%rax)
+;; 2cb: addb %al, (%rax)
+;; 2cd: addb %al, (%rax)
+;; 2cf: addb %al, (%rax)
+;; 2d1: addb %al, (%rax)
+;; 2d3: addb %al, (%rax)
+;; 2d5: addb %al, (%rax)
+;; 2d7: addb %al, (%rax)
+;; 2d9: addb %al, (%rax)
+;; 2db: addb %al, (%rax)
+;; 2dd: addb %al, (%rax)
diff --git a/tests/disas/winch/x64/i8x16/shift/shr_s_all_fprs_int_local.wat b/tests/disas/winch/x64/i8x16/shift/shr_s_all_fprs_int_local.wat
new file mode 100644
index 0000000000..040b8dad3e
--- /dev/null
+++ b/tests/disas/winch/x64/i8x16/shift/shr_s_all_fprs_int_local.wat
@@ -0,0 +1,176 @@
+;;! target = "x86_64"
+;;! test = "winch"
+;;! flags = [ "-Ccranelift-has-avx" ]
+
+(module
+ (func (param i64)
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+ f64.const 0
+ f64.neg
+
+ local.get 0
+
+ v128.const i64x2 0 0
+ i32.const 0
+ i8x16.shr_s
+ unreachable
+ )
+)
+;; wasm[0]::function[0]:
+;; pushq %rbp
+;; movq %rsp, %rbp
+;; movq 8(%rdi), %r11
+;; movq 0x10(%r11), %r11
+;; addq $0x98, %r11
+;; cmpq %rsp, %r11
+;; ja 0x2b7
+;; 1c: movq %rdi, %r14
+;; subq $0x20, %rsp
+;; movq %rdi, 0x18(%rsp)
+;; movq %rsi, 0x10(%rsp)
+;; movq %rdx, 8(%rsp)
+;; movsd 0x283(%rip), %xmm0
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm0
+;; movsd 0x267(%rip), %xmm1
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm1
+;; movsd 0x24b(%rip), %xmm2
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm2
+;; movsd 0x22f(%rip), %xmm3
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm3
+;; movsd 0x213(%rip), %xmm4
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm4
+;; movsd 0x1f7(%rip), %xmm5
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm5
+;; movsd 0x1db(%rip), %xmm6
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm6
+;; movsd 0x1bf(%rip), %xmm7
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm7
+;; movsd 0x1a2(%rip), %xmm8
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm8
+;; movsd 0x185(%rip), %xmm9
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm9
+;; movsd 0x168(%rip), %xmm10
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm10
+;; movsd 0x14b(%rip), %xmm11
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm11
+;; movsd 0x12e(%rip), %xmm12
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm12
+;; movsd 0x111(%rip), %xmm13
+;; movabsq $9223372036854775808, %r11
+;; movq %r11, %xmm15
+;; xorpd %xmm15, %xmm13
+;; movl $0, %eax
+;; movdqu 0xff(%rip), %xmm14
+;; andl $7, %eax
+;; subq $8, %rsp
+;; movsd %xmm0, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm1, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm2, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm3, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm4, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm5, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm6, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm7, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm8, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm9, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm10, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm11, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm12, (%rsp)
+;; subq $8, %rsp
+;; movsd %xmm13, (%rsp)
+;; movq 0x78(%rsp), %r11
+;; pushq %r11
+;; addl $8, %eax
+;; vmovd %eax, %xmm15
+;; vpunpcklbw %xmm14, %xmm14, %xmm0
+;; vpunpckhbw %xmm14, %xmm14, %xmm1
+;; vpsraw %xmm15, %xmm0, %xmm0
+;; vpsraw %xmm15, %xmm1, %xmm1
+;; vpacksswb %xmm1, %xmm0, %xmm14
+;; ud2
+;; addq $0x20, %rsp
+;; popq %rbp
+;; retq
+;; 2b7: ud2
+;; 2b9: addb %al, (%rax)
+;; 2bb: addb %al, (%rax)
+;; 2bd: addb %al, (%rax)
+;; 2bf: addb %al, (%rax)
+;; 2c1: addb %al, (%rax)
+;; 2c3: addb %al, (%rax)
+;; 2c5: addb %al, (%rax)
+;; 2c7: addb %al, (%rax)
+;; 2c9: addb %al, (%rax)
+;; 2cb: addb %al, (%rax)
+;; 2cd: addb %al, (%rax)
+;; 2cf: addb %al, (%rax)
+;; 2d1: addb %al, (%rax)
+;; 2d3: addb %al, (%rax)
+;; 2d5: addb %al, (%rax)
+;; 2d7: addb %al, (%rax)
+;; 2d9: addb %al, (%rax)
+;; 2db: addb %al, (%rax)
+;; 2dd: addb %al, (%rax)
diff --git a/winch/codegen/src/isa/x64/masm.rs b/winch/codegen/src/isa/x64/masm.rs
index e5561ffe80..d465859b7d 100644
--- a/winch/codegen/src/isa/x64/masm.rs
+++ b/winch/codegen/src/isa/x64/masm.rs
@@ -2403,58 +2403,61 @@ impl Masm for MacroAssembler {
OperandSize::S32,
)?;
- self.with_scratch::<IntScratch, _>(|masm, tmp| {
- masm.with_scratch::<FloatScratch, _>(|masm, tmp_xmm| {
- let move_to_tmp_xmm = |this: &mut Self| {
- this.asm
- .avx_gpr_to_xmm(shift_amount, tmp_xmm.writable(), OperandSize::S32);
- };
+ let move_to_tmp_xmm = |this: &mut Self, tmp_xmm: Scratch| {
+ this.asm
+ .avx_gpr_to_xmm(shift_amount, tmp_xmm.writable(), OperandSize::S32);
+ };
- // A helper for deciding between `vpsllw` and `vpsrlw` in
- // `shift_i8x16`.
- enum Direction {
- Left,
- Right,
- }
+ // A helper for deciding between `vpsllw` and `vpsrlw` in
+ // `shift_i8x16`.
+ enum Direction {
+ Left,
+ Right,
+ }
- let shift_i8x16 = |this: &mut Self, masks: &'static [u8], direction: Direction| {
- // The case for i8x16 is a little bit trickier because x64 doesn't provide a 8bit
- // shift instruction. Instead, we shift as 16bits, and then mask the bits in the
- // 8bits lane, for example (with 2 8bits lanes):
- // - Before shifting:
- // 01001101 11101110
- // - shifting by 2 left:
- // 00110111 10111000
- // ^^_ these bits come from the previous byte, and need to be masked.
- // - The mask:
- // 11111100 11111111
- // - After masking:
- // 00110100 10111000
- //
- // The mask is loaded from a well known memory, depending on the shift amount.
+ let shift_i8x16 = |this: &mut Self,
+ masks: &'static [u8],
+ direction: Direction|
+ -> Result<()> {
+ // The case for i8x16 is a little bit trickier because x64 doesn't provide a 8bit
+ // shift instruction. Instead, we shift as 16bits, and then mask the bits in the
+ // 8bits lane, for example (with 2 8bits lanes):
+ // - Before shifting:
+ // 01001101 11101110
+ // - shifting by 2 left:
+ // 00110111 10111000
+ // ^^_ these bits come from the previous byte, and need to be masked.
+ // - The mask:
+ // 11111100 11111111
+ // - After masking:
+ // 00110100 10111000
+ //
+ // The mask is loaded from a well known memory, depending on the shift amount.
- this.asm
- .avx_gpr_to_xmm(shift_amount, tmp_xmm.writable(), OperandSize::S32);
-
- // Perform the 16-bit shift.
- match direction {
- Direction::Left => this.asm.xmm_vpsll_rrr(
- operand,
- tmp_xmm.inner(),
- writable!(operand),
- OperandSize::S16,
- ),
- Direction::Right => this.asm.xmm_vpsrl_rrr(
- operand,
- tmp_xmm.inner(),
- writable!(operand),
- OperandSize::S16,
- ),
- }
+ this.with_scratch::<FloatScratch, _>(|this, tmp_xmm| {
+ this.asm
+ .avx_gpr_to_xmm(shift_amount, tmp_xmm.writable(), OperandSize::S32);
- // Get a handle to the masks array constant.
- let masks_addr = this.asm.add_constant(masks);
+ // Perform the 16-bit shift.
+ match direction {
+ Direction::Left => this.asm.xmm_vpsll_rrr(
+ operand,
+ tmp_xmm.inner(),
+ writable!(operand),
+ OperandSize::S16,
+ ),
+ Direction::Right => this.asm.xmm_vpsrl_rrr(
+ operand,
+ tmp_xmm.inner(),
+ writable!(operand),
+ OperandSize::S16,
+ ),
+ }
+
+ // Get a handle to the masks array constant.
+ let masks_addr = this.asm.add_constant(masks);
+ this.with_scratch::<IntScratch, _>(|this, tmp| {
// Load the masks array effective address into the tmp register.
this.asm.lea(&masks_addr, tmp.writable(), OperandSize::S64);
@@ -2474,203 +2477,218 @@ impl Masm for MacroAssembler {
tmp_xmm.writable(),
MemFlags::trusted(),
);
+ });
- // Mask unwanted bits from operand.
- this.asm
- .xmm_vpand_rrr(tmp_xmm.inner(), operand, writable!(operand));
- };
+ // Mask unwanted bits from operand.
+ this.asm
+ .xmm_vpand_rrr(tmp_xmm.inner(), operand, writable!(operand));
+ Ok(())
+ })
+ };
- let i64x2_shr_s = |this: &mut Self,
- context: &mut CodeGenContext<Emission>|
- -> Result<()> {
- const SIGN_MASK: u128 = 0x8000000000000000_8000000000000000;
+ let i64x2_shr_s = |this: &mut Self, context: &mut CodeGenContext<Emission>| -> Result<()> {
+ const SIGN_MASK: u128 = 0x8000000000000000_8000000000000000;
- // AVX doesn't have an instruction for i64x2 signed right shift. Instead we use the
- // following formula (from hacker's delight 2-7), where x is the value and n the shift
- // amount, for each lane:
- // t = (1 << 63) >> n; ((x >> n) ^ t) - t
+ // AVX doesn't have an instruction for i64x2 signed right shift. Instead we use the
+ // following formula (from hacker's delight 2-7), where x is the value and n the shift
+ // amount, for each lane:
+ // t = (1 << 63) >> n; ((x >> n) ^ t) - t
- // We need an extra scratch register:
- let tmp_xmm2 = context.any_fpr(this)?;
+ // We need an extra scratch register:
+ let tmp_xmm2 = context.any_fpr(this)?;
- this.asm
- .avx_gpr_to_xmm(shift_amount, tmp_xmm.writable(), OperandSize::S32);
+ this.with_scratch::<FloatScratch, _>(|this, tmp_xmm| {
+ this.asm
+ .avx_gpr_to_xmm(shift_amount, tmp_xmm.writable(), OperandSize::S32);
- let cst = this.asm.add_constant(&SIGN_MASK.to_le_bytes());
+ let cst = this.asm.add_constant(&SIGN_MASK.to_le_bytes());
- this.asm
- .xmm_vmovdqu_mr(&cst, writable!(tmp_xmm2), MemFlags::trusted());
- this.asm.xmm_vpsrl_rrr(
- tmp_xmm2,
+ this.asm
+ .xmm_vmovdqu_mr(&cst, writable!(tmp_xmm2), MemFlags::trusted());
+ this.asm.xmm_vpsrl_rrr(
+ tmp_xmm2,
+ tmp_xmm.inner(),
+ writable!(tmp_xmm2),
+ OperandSize::S64,
+ );
+ this.asm.xmm_vpsrl_rrr(
+ operand,
+ tmp_xmm.inner(),
+ writable!(operand),
+ OperandSize::S64,
+ );
+ });
+ this.asm
+ .xmm_vpxor_rrr(operand, tmp_xmm2, writable!(operand));
+ this.asm
+ .xmm_vpsub_rrr(operand, tmp_xmm2, writable!(operand), OperandSize::S64);
+
+ context.free_reg(tmp_xmm2);
+
+ Ok(())
+ };
+
+ let i8x16_shr_s = |this: &mut Self, context: &mut CodeGenContext<Emission>| -> Result<()> {
+ // Since the x86 instruction set does not have an 8x16 shift instruction and the
+ // approach used for `ishl` and `ushr` cannot be easily used (the masks do not
+ // preserve the sign), we use a different approach here: separate the low and
+ // high lanes, shift them separately, and merge them into the final result.
+ //
+ // Visually, this looks like the following, where `src.i8x16 = [s0, s1, ...,
+ // s15]:
+ //
+ // lo.i16x8 = [(s0, s0), (s1, s1), ..., (s7, s7)]
+ // shifted_lo.i16x8 = shift each lane of `low`
+ // hi.i16x8 = [(s8, s8), (s9, s9), ..., (s15, s15)]
+ // shifted_hi.i16x8 = shift each lane of `high`
+ // result = [s0'', s1'', ..., s15'']
+
+ // In order for `packsswb` later to only use the high byte of each
+ // 16x8 lane, we shift right an extra 8 bits, relying on `psraw` to
+ // fill in the upper bits appropriately.
+ let tmp_lo = context.any_fpr(this)?;
+ let tmp_hi = context.any_fpr(this)?;
+
+ this.with_scratch::<FloatScratch, _>(|this, tmp_xmm| {
+ this.asm
+ .add_ir(8, writable!(shift_amount), OperandSize::S32);
+ this.asm
+ .avx_gpr_to_xmm(shift_amount, tmp_xmm.writable(), OperandSize::S32);
+
+ // Extract lower and upper bytes.
+ this.asm
+ .xmm_vpunpckl_rrr(operand, operand, writable!(tmp_lo), OperandSize::S8);
+ this.asm
+ .xmm_vpunpckh_rrr(operand, operand, writable!(tmp_hi), OperandSize::S8);
+
+ // Perform 16bit right shift of upper and lower bytes.
+ this.asm.xmm_vpsra_rrr(
+ tmp_lo,
+ tmp_xmm.inner(),
+ writable!(tmp_lo),
+ OperandSize::S16,
+ );
+ this.asm.xmm_vpsra_rrr(
+ tmp_hi,
+ tmp_xmm.inner(),
+ writable!(tmp_hi),
+ OperandSize::S16,
+ );
+ });
+
+ // Merge lower and upper bytes back.
+ this.asm
+ .xmm_vpackss_rrr(tmp_lo, tmp_hi, writable!(operand), OperandSize::S8);
+
+ context.free_reg(tmp_lo);
+ context.free_reg(tmp_hi);
+
+ Ok(())
+ };
+
+ match (lane_width, kind) {
+ // shl
+ (OperandSize::S8, ShiftKind::Shl) => {
+ shift_i8x16(self, &I8X16_ISHL_MASKS, Direction::Left)?
+ }
+ (OperandSize::S16, ShiftKind::Shl) => {
+ self.with_scratch::<FloatScratch, _>(|masm, tmp_xmm| {
+ move_to_tmp_xmm(masm, tmp_xmm);
+ masm.asm.xmm_vpsll_rrr(
+ operand,
tmp_xmm.inner(),
- writable!(tmp_xmm2),
- OperandSize::S64,
+ writable!(operand),
+ OperandSize::S16,
+ );
+ })
+ }
+ (OperandSize::S32, ShiftKind::Shl) => {
+ self.with_scratch::<FloatScratch, _>(|masm, tmp_xmm| {
+ move_to_tmp_xmm(masm, tmp_xmm);
+ masm.asm.xmm_vpsll_rrr(
+ operand,
+ tmp_xmm.inner(),
+ writable!(operand),
+ OperandSize::S32,
);
- this.asm.xmm_vpsrl_rrr(
+ })
+ }
+ (OperandSize::S64, ShiftKind::Shl) => {
+ self.with_scratch::<FloatScratch, _>(|masm, tmp_xmm| {
+ move_to_tmp_xmm(masm, tmp_xmm);
+ masm.asm.xmm_vpsll_rrr(
operand,
tmp_xmm.inner(),
writable!(operand),
OperandSize::S64,
);
- this.asm
- .xmm_vpxor_rrr(operand, tmp_xmm2, writable!(operand));
- this.asm
- .xmm_vpsub_rrr(operand, tmp_xmm2, writable!(operand), OperandSize::S64);
-
- context.free_reg(tmp_xmm2);
-
- Ok(())
- };
-
- let i8x16_shr_s = |this: &mut Self,
- context: &mut CodeGenContext<Emission>|
- -> Result<()> {
- // Since the x86 instruction set does not have an 8x16 shift instruction and the
- // approach used for `ishl` and `ushr` cannot be easily used (the masks do not
- // preserve the sign), we use a different approach here: separate the low and
- // high lanes, shift them separately, and merge them into the final result.
- //
- // Visually, this looks like the following, where `src.i8x16 = [s0, s1, ...,
- // s15]:
- //
- // lo.i16x8 = [(s0, s0), (s1, s1), ..., (s7, s7)]
- // shifted_lo.i16x8 = shift each lane of `low`
- // hi.i16x8 = [(s8, s8), (s9, s9), ..., (s15, s15)]
- // shifted_hi.i16x8 = shift each lane of `high`
- // result = [s0'', s1'', ..., s15'']
-
- // In order for `packsswb` later to only use the high byte of each
- // 16x8 lane, we shift right an extra 8 bits, relying on `psraw` to
- // fill in the upper bits appropriately.
- this.asm
- .add_ir(8, writable!(shift_amount), OperandSize::S32);
- this.asm
- .avx_gpr_to_xmm(shift_amount, tmp_xmm.writable(), OperandSize::S32);
-
- let tmp_lo = context.any_fpr(this)?;
- let tmp_hi = context.any_fpr(this)?;
-
- // Extract lower and upper bytes.
- this.asm
- .xmm_vpunpckl_rrr(operand, operand, writable!(tmp_lo), OperandSize::S8);
- this.asm
- .xmm_vpunpckh_rrr(operand, operand, writable!(tmp_hi), OperandSize::S8);
-
- // Perform 16bit right shift of upper and lower bytes.
- this.asm.xmm_vpsra_rrr(
- tmp_lo,
+ })
+ }
+ // shr_u
+ (OperandSize::S8, ShiftKind::ShrU) => {
+ shift_i8x16(self, &I8X16_USHR_MASKS, Direction::Right)?
+ }
+ (OperandSize::S16, ShiftKind::ShrU) => {
+ self.with_scratch::<FloatScratch, _>(|masm, tmp_xmm| {
+ move_to_tmp_xmm(masm, tmp_xmm);
+ masm.asm.xmm_vpsrl_rrr(
+ operand,
tmp_xmm.inner(),
- writable!(tmp_lo),
+ writable!(operand),
OperandSize::S16,
);
- this.asm.xmm_vpsra_rrr(
- tmp_hi,
+ })
+ }
+ (OperandSize::S32, ShiftKind::ShrU) => {
+ self.with_scratch::<FloatScratch, _>(|masm, tmp_xmm| {
+ move_to_tmp_xmm(masm, tmp_xmm);
+ masm.asm.xmm_vpsrl_rrr(
+ operand,
tmp_xmm.inner(),
- writable!(tmp_hi),
+ writable!(operand),
+ OperandSize::S32,
+ );
+ })
+ }
+ (OperandSize::S64, ShiftKind::ShrU) => {
+ self.with_scratch::<FloatScratch, _>(|masm, tmp_xmm| {
+ move_to_tmp_xmm(masm, tmp_xmm);
+ masm.asm.xmm_vpsrl_rrr(
+ operand,
+ tmp_xmm.inner(),
+ writable!(operand),
+ OperandSize::S64,
+ );
+ })
+ }
+ // shr_s
+ (OperandSize::S8, ShiftKind::ShrS) => i8x16_shr_s(self, context)?,
+ (OperandSize::S16, ShiftKind::ShrS) => {
+ self.with_scratch::<FloatScratch, _>(|masm, tmp_xmm| {
+ move_to_tmp_xmm(masm, tmp_xmm);
+ masm.asm.xmm_vpsra_rrr(
+ operand,
+ tmp_xmm.inner(),
+ writable!(operand),
OperandSize::S16,
);
+ })
+ }
+ (OperandSize::S32, ShiftKind::ShrS) => {
+ self.with_scratch::<FloatScratch, _>(|masm, tmp_xmm| {
+ move_to_tmp_xmm(masm, tmp_xmm);
+ masm.asm.xmm_vpsra_rrr(
+ operand,
+ tmp_xmm.inner(),
+ writable!(operand),
+ OperandSize::S32,
+ );
+ })
+ }
+ (OperandSize::S64, ShiftKind::ShrS) => i64x2_shr_s(self, context)?,
- // Merge lower and upper bytes back.
- this.asm
- .xmm_vpackss_rrr(tmp_lo, tmp_hi, writable!(operand), OperandSize::S8);
-
- context.free_reg(tmp_lo);
- context.free_reg(tmp_hi);
-
- Ok(())
- };
-
- match (lane_width, kind) {
- // shl
- (OperandSize::S8, ShiftKind::Shl) => {
- shift_i8x16(masm, &I8X16_ISHL_MASKS, Direction::Left)
- }
- (OperandSize::S16, ShiftKind::Shl) => {
- move_to_tmp_xmm(masm);
- masm.asm.xmm_vpsll_rrr(
- operand,
- tmp_xmm.inner(),
- writable!(operand),
- OperandSize::S16,
- );
- }
- (OperandSize::S32, ShiftKind::Shl) => {
- move_to_tmp_xmm(masm);
- masm.asm.xmm_vpsll_rrr(
- operand,
- tmp_xmm.inner(),
- writable!(operand),
- OperandSize::S32,
- );
- }
- (OperandSize::S64, ShiftKind::Shl) => {
- move_to_tmp_xmm(masm);
- masm.asm.xmm_vpsll_rrr(
- operand,
- tmp_xmm.inner(),
- writable!(operand),
- OperandSize::S64,
- );
- }
- // shr_u
- (OperandSize::S8, ShiftKind::ShrU) => {
- shift_i8x16(masm, &I8X16_USHR_MASKS, Direction::Right)
- }
- (OperandSize::S16, ShiftKind::ShrU) => {
- move_to_tmp_xmm(masm);
- masm.asm.xmm_vpsrl_rrr(
- operand,
- tmp_xmm.inner(),
- writable!(operand),
- OperandSize::S16,
- );
- }
- (OperandSize::S32, ShiftKind::ShrU) => {
- move_to_tmp_xmm(masm);
- masm.asm.xmm_vpsrl_rrr(
- operand,
- tmp_xmm.inner(),
- writable!(operand),
- OperandSize::S32,
- );
- }
- (OperandSize::S64, ShiftKind::ShrU) => {
- move_to_tmp_xmm(masm);
- masm.asm.xmm_vpsrl_rrr(
- operand,
- tmp_xmm.inner(),
- writable!(operand),
- OperandSize::S64,
- );
- }
- // shr_s
- (OperandSize::S8, ShiftKind::ShrS) => i8x16_shr_s(masm, context)?,
- (OperandSize::S16, ShiftKind::ShrS) => {
- move_to_tmp_xmm(masm);
- masm.asm.xmm_vpsra_rrr(
- operand,
- tmp_xmm.inner(),
- writable!(operand),
- OperandSize::S16,
- );
- }
- (OperandSize::S32, ShiftKind::ShrS) => {
- move_to_tmp_xmm(masm);
- masm.asm.xmm_vpsra_rrr(
- operand,
- tmp_xmm.inner(),
- writable!(operand),
- OperandSize::S32,
- );
- }
- (OperandSize::S64, ShiftKind::ShrS) => i64x2_shr_s(masm, context)?,
-
- _ => bail!(CodeGenError::invalid_operand_combination()),
- }
-
- Ok(())
- })
- })?;
+ _ => bail!(CodeGenError::invalid_operand_combination()),
+ }
context.free_reg(shift_amount);
context
diff --git a/winch/codegen/src/masm.rs b/winch/codegen/src/masm.rs
index f7c512f068..0c0049512d 100644
--- a/winch/codegen/src/masm.rs
+++ b/winch/codegen/src/masm.rs
@@ -217,6 +217,7 @@ impl ScratchType for FloatScratch {
}
/// A scratch register scope.
+#[derive(Debug, Clone, Copy)]
pub struct Scratch(Reg);
impl Scratch {