Day 24 of Advent of Compiler Optimisations!

A simple loop that sums integers from 0 to n. GCC cleverly unrolls it to process two numbers at once. But clang? The loop completely disappears—replaced by a few multiplies and shifts that compute the answer directly. How does it recognise this pattern and transform O(n) code into O(1)?

Read more: xania.org/202512/24-cunning-cl
Watch: youtu.be/V9dy34slaxA

0

If you have a fediverse account, you can quote this note from your own instance. Search https://hachyderm.io/users/mattgodbolt/statuses/115774750975693379 on your instance and quote it. (Note that quoting is not supported in Mastodon.)