Re: [PATCH v3 1/5] x86/emul: Introduce x86_decode_lite()
Andrew Cooper <[email protected]> Mon, 3 Aug 2026 10:14:29 +0100
| Newsgroups | gmane.comp.emulators.xen.devel |
|---|---|
| Message-ID | <[email protected]> |
On 03/08/2026 8:20 am, Andrew Cooper wrote:
> diff --git a/xen/arch/x86/x86_emulate/decode-lite.c b/xen/arch/x86/x86_emulate/decode-lite.c
> new file mode 100644
> index 000000000000..131cc07d5516
> --- /dev/null
> +++ b/xen/arch/x86/x86_emulate/decode-lite.c
> @@ -0,0 +1,330 @@
> +
> + if ( d & (Imm | Imm8 | Moffs) )
> + {
> + if ( d & Imm8 )
> + osize = 1;
> + else if ( d & Moffs )
> + osize = 8;
> + else if ( osize == 8 && !(opc >= 0xb8 && opc <= 0xbf) )
> + osize = 4;
GCC 12 does transform this into sub $0xb8; cmp $7.
> +
> + switch ( osize )
> + {
> + case 1: FETCH(uint8_t); break;
> + case 2: FETCH(uint16_t); break;
> + case 4: FETCH(uint32_t); break;
> + case 8: FETCH(uint64_t); break;
> + default: goto bad_osize;
> + }
On further consideration:
switch ( osize )
{
case 1:
case 2:
case 4:
case 8:
if ( ip + osize > end )
goto overrun;
ip += osize;
break;
default: goto bad_osize;
}
drops nearly 10% of the function:
add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-91 (-91)
Function old new delta
x86_decode_lite 972 881 -91
GCC clearly can't reason about the relationship between osize and
sizeof(type), and needs the help.
I also tried the further simplification:
if ( osize > 8 || (osize & (osize - 1)) != 0 )
goto bad_osize;
if ( ip + osize > end )
goto overrun;
ip += osize;
but interestingly this delta grows the function by 30 bytes. It only
seems to add the block checking osize, meaning that GCC managed to
optimise away all of the switch dispatch previously. In hindsight this
is probably quite easy; because we're 64bit only, osize only ever has
constant values that GCC can see.
Anyway, I've folded in the first optimisation.
~Andrew