Skip to content

Commit

Permalink
x64: Improve lowering for conditional traps
Browse files Browse the repository at this point in the history
This commit improves the lowering rules on x64 for conditional traps
(e.g. `trapz` and `trapnz`) to include a special-case with `icmp` which
enables removing some extra instructions by materializing a comparison
result into a register.
  • Loading branch information
alexcrichton committed Nov 6, 2024
1 parent 74d2df5 commit 2888210
Show file tree
Hide file tree
Showing 52 changed files with 593 additions and 761 deletions.
15 changes: 11 additions & 4 deletions cranelift/codegen/src/isa/x64/lower.isle
Original file line number Diff line number Diff line change
Expand Up @@ -1864,13 +1864,20 @@
(side_effect (x64_ud2 code)))

;;;; Rules for `trapz` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(rule (lower (trapz val code))
(side_effect (trap_if_val (ZeroCond.Zero) val code)))

(rule 0 (lower (trapz val code))
(side_effect (trap_if_val (ZeroCond.Zero) val code)))

(rule 1 (lower (trapz (icmp cc a b) code))
(side_effect (trap_if_icmp (emit_cmp (intcc_complement cc) a b) code)))

;;;; Rules for `trapnz` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(rule (lower (trapnz val code))
(side_effect (trap_if_val (ZeroCond.NonZero) val code)))
(rule 0 (lower (trapnz val code))
(side_effect (trap_if_val (ZeroCond.NonZero) val code)))

(rule 1 (lower (trapnz (icmp cc a b) code))
(side_effect (trap_if_icmp (emit_cmp cc a b) code)))

;;;; Rules for `uadd_overflow_trap` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Expand Down
16 changes: 4 additions & 12 deletions cranelift/filetests/filetests/isa/x64/traps.clif
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,7 @@ block0(v0: i64, v1: i64):
; movq %rsp, %rbp
; block0:
; cmpq %rsi, %rdi
; setz %dl
; testb %dl, %dl
; jz #trap=user1
; jnz #trap=user1
; movq %rbp, %rsp
; popq %rbp
; ret
Expand All @@ -189,9 +187,7 @@ block0(v0: i64, v1: i64):
; movq %rsp, %rbp
; block1: ; offset 0x4
; cmpq %rsi, %rdi
; sete %dl
; testb %dl, %dl
; je 0x18
; jne 0x12
; movq %rbp, %rsp
; popq %rbp
; retq
Expand All @@ -209,9 +205,7 @@ block0(v0: i64, v1: i64):
; movq %rsp, %rbp
; block0:
; cmpq %rsi, %rdi
; setz %dl
; testb %dl, %dl
; jnz #trap=user1
; jz #trap=user1
; movq %rbp, %rsp
; popq %rbp
; ret
Expand All @@ -222,9 +216,7 @@ block0(v0: i64, v1: i64):
; movq %rsp, %rbp
; block1: ; offset 0x4
; cmpq %rsi, %rdi
; sete %dl
; testb %dl, %dl
; jne 0x18
; je 0x12
; movq %rbp, %rsp
; popq %rbp
; retq
Expand Down
4 changes: 2 additions & 2 deletions tests/disas/epoch-interruption-x86.wat
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
;; jae 0x64
;; jmp 0x46
;; 57: movq %r13, %rdi
;; callq 0xf3
;; callq 0xec
;; jmp 0x46
;; 64: movq 0x10(%r12), %rax
;; cmpq %rax, %rdi
;; jb 0x46
;; 72: movq %r13, %rdi
;; callq 0xf3
;; callq 0xec
;; jmp 0x46
;; 7f: ud2
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,29 @@
;; wasm[0]::function[0]:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 0x68(%rdi), %r9
;; movl %edx, %r11d
;; subq $4, %r9
;; cmpq %r9, %r11
;; seta %sil
;; testb %sil, %sil
;; jne 0x2c
;; 1f: movq 0x60(%rdi), %rax
;; movl %ecx, (%rax, %r11)
;; movq 0x68(%rdi), %r8
;; movl %edx, %r10d
;; subq $4, %r8
;; cmpq %r8, %r10
;; ja 0x25
;; 18: movq 0x60(%rdi), %rsi
;; movl %ecx, (%rsi, %r10)
;; movq %rbp, %rsp
;; popq %rbp
;; retq
;; 2c: ud2
;; 25: ud2
;;
;; wasm[0]::function[1]:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 0x68(%rdi), %r9
;; movl %edx, %r11d
;; subq $4, %r9
;; cmpq %r9, %r11
;; seta %sil
;; testb %sil, %sil
;; jne 0x6c
;; 5f: movq 0x60(%rdi), %rax
;; movl (%rax, %r11), %eax
;; movq 0x68(%rdi), %r8
;; movl %edx, %r10d
;; subq $4, %r8
;; cmpq %r8, %r10
;; ja 0x65
;; 58: movq 0x60(%rdi), %rsi
;; movl (%rsi, %r10), %eax
;; movq %rbp, %rsp
;; popq %rbp
;; retq
;; 6c: ud2
;; 65: ud2
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,29 @@
;; wasm[0]::function[0]:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 0x68(%rdi), %r9
;; movl %edx, %r11d
;; subq $0x1004, %r9
;; cmpq %r9, %r11
;; seta %sil
;; testb %sil, %sil
;; jne 0x33
;; 22: movq 0x60(%rdi), %rax
;; movl %ecx, 0x1000(%rax, %r11)
;; movq 0x68(%rdi), %r8
;; movl %edx, %r10d
;; subq $0x1004, %r8
;; cmpq %r8, %r10
;; ja 0x2c
;; 1b: movq 0x60(%rdi), %rsi
;; movl %ecx, 0x1000(%rsi, %r10)
;; movq %rbp, %rsp
;; popq %rbp
;; retq
;; 33: ud2
;; 2c: ud2
;;
;; wasm[0]::function[1]:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 0x68(%rdi), %r9
;; movl %edx, %r11d
;; subq $0x1004, %r9
;; cmpq %r9, %r11
;; seta %sil
;; testb %sil, %sil
;; jne 0x73
;; 62: movq 0x60(%rdi), %rax
;; movl 0x1000(%rax, %r11), %eax
;; movq 0x68(%rdi), %r8
;; movl %edx, %r10d
;; subq $0x1004, %r8
;; cmpq %r8, %r10
;; ja 0x6c
;; 5b: movq 0x60(%rdi), %rsi
;; movl 0x1000(%rsi, %r10), %eax
;; movq %rbp, %rsp
;; popq %rbp
;; retq
;; 73: ud2
;; 6c: ud2
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,36 @@
;; pushq %rbp
;; movq %rsp, %rbp
;; movl %edx, %r8d
;; movq %r8, %r11
;; addq 0x2f(%rip), %r11
;; jb 0x39
;; 17: cmpq 0x68(%rdi), %r11
;; seta %al
;; testb %al, %al
;; jne 0x3b
;; 27: addq 0x60(%rdi), %r8
;; movl $0xffff0000, %edx
;; movl %ecx, (%r8, %rdx)
;; movq %r8, %r10
;; addq 0x27(%rip), %r10
;; jb 0x33
;; 17: cmpq 0x68(%rdi), %r10
;; ja 0x35
;; 21: addq 0x60(%rdi), %r8
;; movl $0xffff0000, %edi
;; movl %ecx, (%r8, %rdi)
;; movq %rbp, %rsp
;; popq %rbp
;; retq
;; 39: ud2
;; 3b: ud2
;; 3d: addb %al, (%rax)
;; 3f: addb %al, (%rax, %rax)
;; 33: ud2
;; 35: ud2
;; 37: addb %al, (%rax, %rax)
;;
;; wasm[0]::function[1]:
;; pushq %rbp
;; movq %rsp, %rbp
;; movl %edx, %r8d
;; movq %r8, %r11
;; addq 0x2f(%rip), %r11
;; jb 0x99
;; 77: cmpq 0x68(%rdi), %r11
;; seta %al
;; testb %al, %al
;; jne 0x9b
;; 87: addq 0x60(%rdi), %r8
;; movl $0xffff0000, %ecx
;; movl (%r8, %rcx), %eax
;; movq %r8, %r10
;; addq 0x27(%rip), %r10
;; jb 0x73
;; 57: cmpq 0x68(%rdi), %r10
;; ja 0x75
;; 61: addq 0x60(%rdi), %r8
;; movl $0xffff0000, %edi
;; movl (%r8, %rdi), %eax
;; movq %rbp, %rsp
;; popq %rbp
;; retq
;; 99: ud2
;; 9b: ud2
;; 9d: addb %al, (%rax)
;; 9f: addb %al, (%rax, %rax)
;; 73: ud2
;; 75: ud2
;; 77: addb %al, (%rax, %rax)
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,25 @@
;; wasm[0]::function[0]:
;; pushq %rbp
;; movq %rsp, %rbp
;; movl %edx, %r9d
;; cmpq 0x68(%rdi), %r9
;; setae %r10b
;; testb %r10b, %r10b
;; jne 0x25
;; 18: movq 0x60(%rdi), %rsi
;; movb %cl, (%rsi, %r9)
;; movl %edx, %r8d
;; cmpq 0x68(%rdi), %r8
;; jae 0x1e
;; 11: movq 0x60(%rdi), %r10
;; movb %cl, (%r10, %r8)
;; movq %rbp, %rsp
;; popq %rbp
;; retq
;; 25: ud2
;; 1e: ud2
;;
;; wasm[0]::function[1]:
;; pushq %rbp
;; movq %rsp, %rbp
;; movl %edx, %r9d
;; cmpq 0x68(%rdi), %r9
;; setae %r10b
;; testb %r10b, %r10b
;; jne 0x66
;; 58: movq 0x60(%rdi), %rsi
;; movzbq (%rsi, %r9), %rax
;; movl %edx, %r8d
;; cmpq 0x68(%rdi), %r8
;; jae 0x3f
;; 31: movq 0x60(%rdi), %r10
;; movzbq (%r10, %r8), %rax
;; movq %rbp, %rsp
;; popq %rbp
;; retq
;; 66: ud2
;; 3f: ud2
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,29 @@
;; wasm[0]::function[0]:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 0x68(%rdi), %r9
;; movl %edx, %r11d
;; subq $0x1001, %r9
;; cmpq %r9, %r11
;; seta %sil
;; testb %sil, %sil
;; jne 0x33
;; 22: movq 0x60(%rdi), %rax
;; movb %cl, 0x1000(%rax, %r11)
;; movq 0x68(%rdi), %r8
;; movl %edx, %r10d
;; subq $0x1001, %r8
;; cmpq %r8, %r10
;; ja 0x2c
;; 1b: movq 0x60(%rdi), %rsi
;; movb %cl, 0x1000(%rsi, %r10)
;; movq %rbp, %rsp
;; popq %rbp
;; retq
;; 33: ud2
;; 2c: ud2
;;
;; wasm[0]::function[1]:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 0x68(%rdi), %r9
;; movl %edx, %r11d
;; subq $0x1001, %r9
;; cmpq %r9, %r11
;; seta %sil
;; testb %sil, %sil
;; jne 0x74
;; 62: movq 0x60(%rdi), %rax
;; movzbq 0x1000(%rax, %r11), %rax
;; movq 0x68(%rdi), %r8
;; movl %edx, %r10d
;; subq $0x1001, %r8
;; cmpq %r8, %r10
;; ja 0x6d
;; 5b: movq 0x60(%rdi), %rsi
;; movzbq 0x1000(%rsi, %r10), %rax
;; movq %rbp, %rsp
;; popq %rbp
;; retq
;; 74: ud2
;; 6d: ud2
Loading

0 comments on commit 2888210

Please sign in to comment.