[Valgrind-developers] [PATCH 1/2] x86: optimize XCHG to MOV for same-register forms
Mark Wielaard
mark@klomp.org
Tue Jul 7 09:00:46 GMT 2026
Hi Michael,
On Mon, Jul 06, 2026 at 05:03:22PM +0200, Michael Matz wrote:
> On Mon, 6 Jul 2026, Mark Wielaard wrote:
> > > 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.
>
> Sure, that's why you also want to change valgrind.h to use concrete
> encodings. Eventually, after many years perhaps, it will then be
> impossible to see any other sequence after the preamble than those we
> already know by then (what GAS or other random assemblers currently and in
> some future years to come can emit). Until then, yes, you will have to
> play some chase-the-encodings game, but (a) that isn't unknown to valgrind
> anyway :) and (b) it won't occur _that_ often. I really don't see why one
> would _not_ want to recognize known sequences as a service to users.
Because it feels like an unnecessary whack-a-mole. We already know
that you should not use an "optimizing" assembler if you care about
the exact encoding emitted by the compiler or the inline assembly. So
it is better to warn early when someone does. Because we know they
will run into trouble eventually.
> > 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.
>
> I think give_hint() plus recognizing the current encodings plus using
> hard-coded sequences in valgrind.h is strictly better than just
> give_hint(), so the latter cannot be the best.
OK. So it is the best I am willing to do. Because I personally
wouldn't want to do all that unnecessary work if we can just document
to not to use an optimizing compiler when building and we can detect
that at runtime. Patches welcome of course if you (or someone else)
believes they can do better and are willing to do the extra work.
Cheers,
Mark
More information about the Binutils
mailing list