[Valgrind-developers] [PATCH 1/2] x86: optimize XCHG to MOV for same-register forms

Mark Wielaard mark@klomp.org
Mon Jul 6 14:25:52 GMT 2026


Hi Michael,

On Mon, 2026-07-06 at 15:36 +0200, Michael Matz wrote:
> On Mon, 6 Jul 2026, Mark Wielaard wrote:
> 
> > > VALGRIND_GET_NR_CONTEXT is defined in 
> > > /usr/include/valgrind/valgrind.h. It will take a long time to get it 
> > > changed in both valgrind source as well as all its users.  I don't 
> > > think half a year is sufficient.
> > 
> > Note that we don't intent to change the inline assembly inside 
> > valgrind.h. Instead we'll just add an explicit runtime check/warning 
> > that tells users to not compile valgrind.h with gas optimizations 
> > enabled. 
> 
> Isn't that unnecessarily disservicing to users?  When you want to detect 
> invalid blobs after the preamble, which presumably does something like 
> this currently:
> 
> switch (read_3byte(ptr)) {
>   case 0xdb8748: do_client_req(); break;
>   case 0xc98748: do_get_nr_context(); break;
>   case 0xd28748: do_call_noredir(); break;
>   case 0xff8748: do_vec_inject(); break;
> }
> 
> and you add a 'default: give_hint(); break;'.

Yes, that is the idea. We'll do that for all supported arches and all
their special sequences (amd64, x86, mips, s390, arm, arm64, ppc,
riscv64, etc.) There is a concrete patch attached to
https://bugs.kde.org/show_bug.cgi?id=522533

>   When changing that piece of 
> code at all, why then not also add the four cases that's emitted by gas 
> right now?  Be lenient in what you accept, and all that?

Because Jan said we cannot rely on what it is optimized to, it might
even get optimized away completely because they are technically nops.
So best we can do is the give_hint () when we detect the user enabled
the assembler optimizer and it has mangled our special instruction
sequence.

Cheers,

Mark


More information about the Binutils mailing list