bytecodealliance / bytecodealliance/wasmtime
x64: clean up `br_if(op(a, b))` codegen
- Dominant language
- Rust
- Stars
- 18.6k
- Forks
- 1.8k
- Avg merge
- 1d 18h
- Merged PRs (30d)
- 126
Description
Follow up issue to https://github.com/bytecodealliance/wasmtime/pull/11122
The semantics of `br_if` are such that we branch to the consequent on a non-zero condition value and the alternative on a zero condition value. Often we are emitting x64 instructions to to the `op` and then explicitly test whether the result is zero or non-zero and branch accordingly. However, for many `op`s we can instead just do some kind of test/compare on `a` and `b` directly, instead of the `op` and then a non-zero test.
For example given
```wat
local.get 0
local.get 1
i32.or
if ;; label = @1
call 0
else
call 1
end
```
we currently emit the comparison as
```asm
00000019 0b d1 or edx, ecx
0000001b 85 d2 test edx, edx
0000001d 0f 85 15 00 00 00 jne 0x38
...
```
but we should be able to do something like
```asm
or edx, ecx
je 0x38
...
```
instead.
I think that we can add special cases for `br_if(op(a, b))` for all `op`s in `{iadd, isub, bor, bxor}`. Maybe more. (Note that we already have a `br_if(band(a, b))` special case).
Note that doing this might require some additional refactorings in the x64 assembler library and its ISLE constructor generation: https://github.com/bytecodealliance/wasmtime/pull/11122#issuecomment-3001204011
Contributor guide
Assessment
This issue has not been assessed yet.