3 ms·
From a high-level academic view, yes, the compiler is allowed to perform any legal transformation. But in practice C compilers are pretty conservative about wha
by jmillikin 2y ago
From a high-level academic view, yes, the compiler is allowed to perform any legal transformation. But in practice C compilers are pretty conservative about what they emit, especially when code is compiled without -march= .
You don't have to take my word for it. Go find a moderately complex open-source library written in C, compile it, then open up the result in Hexrays/Ghidra/radare2/whatever. Compare the compiled functions with their original source and you'll see there's not that much magic going on.
- hun3 2y ago-O3 does autovectorization: turning your loops into a bunch of SIMD instructions, sometimes even drastically changing performance profile. If autovectorization is "not that much magic" then idk what else it is.
- lifthrasiir 2y agoNowadays it's -O2. I was also surprised when I first learned this.
- gpderetta 2y agoAny optimization you are familiar with is trivial and expected. Everything else is broken compilers optimizing UB to win benchmarks.
- cogman10 2y agoThey are as aggressive as they can be. Here's an example of a C compiler completely eliminating a loop because it has figure out how to transform the loop into a constant calculation. https://godbolt.org/z/cfndqMj4j https://godbolt.org/z/cfndqMj4j The place where C compilers are conservative is when dealing with arrays and pointers. That's because it's impossible for C to know if a pointer is to an element of an array or something completely different. Pointer math further complicates what a pointer could actually reference.