Day 1 of Advent of Compiler Optimisations!

Why do compilers love `xor eax, eax` for zeroing registers? It's brilliant: saves bytes compared to `mov eax, 0`, AND x86 CPUs recognise this "zeroing idiom" early in the pipeline—breaking register dependencies and removing it from execution entirely. Even better: writing to `eax` zeroes the top 32 bits of `rax` for free, handling 64-bit longs in one instruction.

Read more: https://xania.org/202512/01-xor-eax-eax
Watch: https://youtu.be/eLjZ48gqbyg

#AoCO2025

Why xor eax, eax? — Matt Godbolt’s blog

Why do compilers love xor-ing registers so much?

@mattgodbolt wait xor eax, eax clears the top of rax?

I never knew that, and also that seems pretty bad. Does xor ax, ax also clear ALL of eax and rax?

@hp @mattgodbolt this quirk of amd64 with 32-bit ops clearing the high bits on 64-bit registers almost messed up the 0x90 opcode used for NOPs, since it wasn't actually a NOP instruction: https://www.pagetable.com/?p=1216
How NOP nearly became a non-NOP on AMD64 – pagetable.com

@th @hp @mattgodbolt That NOP thing reminds me of the IBM mainframe NOP, which is actually a special case of BCR 0,R0, a conditional branch to address in a register. The first zero is a 4-bit mask, meaning never branch :-)