🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #131

Re: Performance of xchg reg,reg

#131
From: Anton Ertl
Date: Thu, 15 Oct 2026 07:06
104 lines
4278 bytes
anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
>MitchAlsup <user5857@newsgrouper.org.invalid> writes:
>>Unlike Mainframes where sort is bread-and-butter operation, on PCs
>>(and similar) it is not.
>
>And the relevance of that for "Performance of xchg reg,reg" is?

To give you an idea what xchg reg,reg is good for:

long foo(long a, long b);

long bar(long a, long b)
{
  return foo(b,a);
}

Both gcc-16 and clang-19 (even with -mtune=znver5) compile this to:

0000000000000000 <bar>:
   0:   48 89 f8                mov    %rdi,%rax
   3:   48 89 f7                mov    %rsi,%rdi
   6:   48 89 c6                mov    %rax,%rsi
   9:   e9 00 00 00 00          jmp    e <bar+0xe> #actually jmp foo

This could be replaced with

xchg %rdi,%rsi
jmp foo


It's less obvious for

long foo(long a, long b);

long bar(long a, long b, long c, long d)
{
  return foo(b-c,a-d);
}

The gcc-12 (different machines) output even with -mtune=znver3 is:

0000000000000000 <bar>:
   0:   48 89 f8                mov    %rdi,%rax
   3:   48 29 d6                sub    %rdx,%rsi
   6:   48 29 c8                sub    %rcx,%rax
   9:   48 89 f7                mov    %rsi,%rdi
   c:   48 89 c6                mov    %rax,%rsi
   f:   e9 00 00 00 00          jmp    14 <bar+0x14>

and the clang-14 output is similar.  This could be shortened to:

sub %rdx,%rsi
sub %rcx,%rdi
xchg %rdi,%rsi
jmp foo

It also happens with control flow (that's what the SSA destruction on
my web page refers to):

long bar(long a, long b, long c, long d)
{
  if (a<b) {
    long tmp=c;
    c=d-a;
    d=tmp-b;
  }
  return 2*c+d;
}

compiles (gcc-12 -O3 -mtune=znver3) to:

0000000000000000 <bar>:
   0:   48 39 f7                cmp    %rsi,%rdi
   3:   7d 0f                   jge    14 <bar+0x14>
   5:   48 89 c8                mov    %rcx,%rax
   8:   48 29 f2                sub    %rsi,%rdx
   b:   48 29 f8                sub    %rdi,%rax
   e:   48 89 d1                mov    %rdx,%rcx
  11:   48 89 c2                mov    %rax,%rdx
  14:   48 8d 04 51             lea    (%rcx,%rdx,2),%rax
  18:   c3                      ret

and could be:

       cmp    %rsi,%rdi
       jge    L1
       sub    %rsi,%rdx
       sub    %rdi,%rcx
       xchg   %rdx,%rcx
L1:    lea    (%rcx,%rdx,2),%rax
       ret

Given that most operations on AMD64 are performed with two-address
(for binary operations) or one-address (for unary operations), the
compiler can usually not use target preferencing to get data into the
required registers with little or no extra move instructions.  So the
need to rearrange registers at some point is especially relevant on
this architecture; and it has the xchg instruction to make the
rearrangements shorter, and, on AMD processors, faster.

- anton
-- 
'Anyone trying for "industrial quality" ISA should avoid undefined behavior.'
  Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Technical Details
Message-ID: <2026Oct15.090653@mips.complang.tuwien.ac.at>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <2026Oct13.104347@mips.complang.tuwien.ac.at> <11ami2v$11dqu$3@dont-email.me> <2026Oct14.090023@mips.complang.tuwien.ac.at> <1792017353-5857@newsgrouper.org> <2026Oct15.080534@mips.complang.tuwien.ac.at>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.arch
Subject: Re: Performance of xchg reg,reg
Date: Thu, 15 Oct 2026 07:06:53 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 104
Message-ID: <2026Oct15.090653@mips.complang.tuwien.ac.at>
References: <2026Oct13.104347@mips.complang.tuwien.ac.at> <11ami2v$11dqu$3@dont-email.me> <2026Oct14.090023@mips.complang.tuwien.ac.at> <1792017353-5857@newsgrouper.org> <2026Oct15.080534@mips.complang.tuwien.ac.at>
Injection-Date: Thu, 15 Oct 2026 07:41:32 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2314569"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+iPIsXGnn6ytHxJiXVdk2I"; posting-host="597db5ff5fb8d21fc1309deab4e71b28"
Cancel-Lock: sha1:O2lelH8CgUxlCz3pAQh/y1H7MEs= sha256:gJ+NDFFEJ5wPQpv3mcNXaLNYxPpbr3P+2SkicKiO5Dk=
	sha1:YwJ4vUELid2e95Peqb5sQmgx/AE= sha256:GeR+GwFxWQ5/SuZ0J/aS7Z+EkgyafynaAaXezyecxos=
X-newsreader: xrn 10.11