2 ms·
When the code has hit the IR in clang or gcc, there is no 'a' (we know that with certainty, since SSA form doesn't mutate but assigns to fresh variables). We do
by kryptiskt 2y ago
When the code has hit the IR in clang or gcc, there is no 'a' (we know that with certainty, since SSA form doesn't mutate but assigns to fresh variables). We don't know if there will be an increment of 1, the additions could be coalesced (or elided if the result can be inferred another way). The number can even decrease, say if things have been handled in chunks of 16, and needs to be adjusted down in the last chunk. Or the code may be auto-vectorized and completely rewritten, so that none of the variables at the C level are reflected on the assembler level.
- jmillikin 2y agoFrom 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.