As already discussed here, a nice blog post and video from Matt Godbolt about common compiler optimization.
The statement that `xor eax, eax` effectively costs zero cycles caught my eye in particular:
\> It gets better though! Since this is a very common operation, x86 CPUs spot this “zeroing idiom” early in the pipeline and can specifically optimise around it: the out-of-order tracking systems knows that the value of “eax” (or whichever register is being zeroed) does not depend on the previous value of eax, so it can allocate a fresh, dependency-free zero register renamer slot. And, having done that it removes the operation from the execution queue \- that is the
xor takes zero execution cycles!^(1) It’s essentially optimised out by the CPU!How do you know? I guess I can verify it in LLVM's MCA?
https://redd.it/1pbdrgo
@r_cpp