Hacker News new | ask | show | jobs
by incadenza 2322 days ago
Are compiler optimizations like summing a series done on an ad-hoc basis? Certainly the compiler couldn’t have discovered or inferred (not sure what term to use) that formula, no?

Just generally curious.

2 comments

Ad-hoc basis. Usually comes out of studying the most common classes of computation and optimizing for it. For example, the "summing a series" probably falls out of loop optimizations: https://en.wikipedia.org/wiki/Loop_optimization
Those optimizations are happening on LLVM's end (though the higher-level language will have to expose sufficiently transparent abstractions to make these optimizations possible). I'd love to read a book on the optimization techniques that are implemented in LLVM/GCC to make transformations like this possible.
Gotcha. Thanks. So presumably Clang would have done the same for C?
Easy to check, thanks to godbolt:

https://godbolt.org/z/SopXnf

Yes, and it's mentioned in the post that the C code was also brought into parity.
Yeah I saw that, but just wasn’t sure how using intrinsics played a role.