From: Andrew Cooper <andrew.cooper3@citrix.com>
To: Xen-devel <xen-devel@lists.xenproject.org>
Cc: "Andrew Cooper" <andrew.cooper3@citrix.com>,
"Jan Beulich" <jbeulich@suse.com>,
"Roger Pau Monné" <roger@xenproject.org>,
"Teddy Astie" <teddy.astie@vates.tech>
Subject: Re: [PATCH v3 1/5] x86/emul: Introduce x86_decode_lite()
Date: Mon, 3 Aug 2026 10:14:29 +0100 [thread overview]
Message-ID: <c97a44db-bf91-4d22-b775-190e69741dae@citrix.com> (raw)
In-Reply-To: <20260803072006.9678-2-andrew.cooper3@citrix.com>
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
next prev parent reply other threads:[~2026-08-03 9:14 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-03 7:20 [PATCH v3 0/5] x86/alternatives: Adjust all insn-relative fields Andrew Cooper
2026-08-03 7:20 ` [PATCH v3 1/5] x86/emul: Introduce x86_decode_lite() Andrew Cooper
2026-08-03 9:14 ` Andrew Cooper [this message]
2026-08-03 15:26 ` Jan Beulich
2026-08-04 15:39 ` Jan Beulich
2026-08-04 18:56 ` Andrew Cooper
2026-08-05 6:24 ` Jan Beulich
2026-08-03 7:20 ` [PATCH v3 2/5] tests/x86: Introduce a userspace test harness for x86_decode_lite() Andrew Cooper
2026-08-03 16:03 ` Jan Beulich
2026-08-04 19:37 ` Andrew Cooper
2026-08-05 6:45 ` Jan Beulich
2026-08-03 7:20 ` [PATCH v3 3/5] x86/alternative: Walk all replacements during self tests Andrew Cooper
2026-08-03 7:20 ` [PATCH v3 4/5] x86/alternative: Relocate all insn-relative fields Andrew Cooper
2026-08-03 7:20 ` [PATCH v3 5/5] x86/spec-ctrl: Introduce and use DO_COND_BHB_SEQ Andrew Cooper
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=c97a44db-bf91-4d22-b775-190e69741dae@citrix.com \
--to=andrew.cooper3@citrix.com \
--cc=jbeulich@suse.com \
--cc=roger@xenproject.org \
--cc=teddy.astie@vates.tech \
--cc=xen-devel@lists.xenproject.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.