Hasty Briefsbeta

Bilingual

Matt Godbolt's blog

15 hours ago
  • Loop-invariant code motion (LICM) can pull computations like `strlen` out of loops for efficient assembly.
  • Adding a global variable (e.g., `num_compares`) that is modified inside the loop can cause LICM to fail.
  • The failure is due to aliasing: `char*` is allowed to alias any type, so the compiler cannot prove the string is unchanged when the global is modified.
  • Despite strict aliasing rules, `char*` (and `unsigned char*`, `std::byte*`) have a special exception allowing them to alias anything.
  • In practice, GCC, Clang, and MSVC were all unable to perform LICM on the modified code.
  • The next post will explore more aliasing issues and how to help the compiler optimize such code.