Article View: comp.arch
Article #150Re: Performance of xchg reg,reg
From: Anton Ertl
Date: Fri, 16 Oct 2026 16:04
Date: Fri, 16 Oct 2026 16:04
35 lines
2737 bytes
2737 bytes
EricP <ThatWouldBeTelling@thevillage.com> writes: >On 2026-Oct-15 03:06, Anton Ertl wrote: >> 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 > >There is also the xor swap which doesn't need an extra register > > xor rax,rbx > xor rbx,rax > xor rax,rbx That has a latency of 3 cycles and (probably less of a problem) consumes 3 ALU slots. Maybe Gracemont uses this internally, the number of cycles for the xchg variants of the cycN benchmarks would be compatible with that explanation. With 0-cycle store-to-load forwarding in recent processors, storing to memory and loading from memory is preferable on Intel processors if no spare register is available; and on AMD processors, xchg for cyc2 and cyc3, and sometimes on cyc4, cyc5. My guess is that using xor would lead to huge slowdowns, even compared to storing the value in memory. - anton -- 'Anyone trying for "industrial quality" ISA should avoid undefined behavior.' Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Technical Details
Message-ID:
<2026Oct16.180410@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-mailReferences:
<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> <2026Oct15.090653@mips.complang.tuwien.ac.at> <2WpAS.163763$Zbg.15006@fx35.iad>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: Fri, 16 Oct 2026 16:04:10 GMT Organization: Institut fuer Computersprachen, Technische Universitaet Wien Lines: 35 Message-ID: <2026Oct16.180410@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> <2026Oct15.090653@mips.complang.tuwien.ac.at> <2WpAS.163763$Zbg.15006@fx35.iad> Injection-Date: Fri, 16 Oct 2026 16:21:19 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3580791"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/+rUuT2AP1lsewYz1rL6nU"; posting-host="eb1d16796a2f3c3c419013704a6bb9be" Cancel-Lock: sha1:mFZXnfsnVru+pl5DutQSRwbm9AY= sha256:13N0dhp+2piBxkLu9PckMkn7Itm4X9wAVehl4vQIz7E= sha1:Xm8fmBDwkJymfAF03dg+S14oHPU= sha256:BdjVgUlrsEnTCGrHMG/4U6uyRGs0Rxt9Td0yk8gTkZI= X-newsreader: xrn 10.11