TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 229 subscribers
Post #24397 43
Advent of Compiler Optimizations 1/25: Why xor eax, eax?

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
Reddit From the cpp community on Reddit: Introducing the Advent of Compiler Optimisations 2025 — Matt Godbolt’s blog Posted by delta_p_delta_x - 170 votes and 4 comments
More from @r_cpp
  1. Oct 12, 2026ACCU Overload Journal 195 - October 2026 https://accu.org/journals/overload/overload195 ht…
  2. Oct 12, 2026C++ Contracts C++ contracts are a good thing, why are people saying it's a bad thing? Anyb…
  3. Oct 11, 2026wxWidgets 3.2.12 and 3.3.4 Released https://wxwidgets.org/news/2026/10/wxwidgets-3.2.12-an…
  4. Oct 11, 2026VKRT NES Remaster — an open-source C++20 NES emulator with an observation-driven remasteri…
  5. Oct 11, 2026Right way to test async code https://redd.it/1x2wqgz @r_cpp
  6. Oct 10, 2026Yantra: an LALR(1) parser generator for C++23 Yantra: an LALR(1) parser generator for C++2…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →