Re:GodBolt: Enter C, get Assembly
by JoshTriplett on Thursday December 15, 2016 @02:52AM
(#13183154)
One optimization I've always found impressive:
compiles into:
Another fun one, that only works with clang:
compiles into:
[ Parent
| on HN ]
As a compiler engineer, those are some of the least impressive things done by modern compilers. Why? Because there isn't anything smart behind them, it's pure pattern matching.
Now I'm curious, what are the most impressive things modern compilers do?
probably this: http://polyhedral.info/
Impressive? Those are probably specially-coded optimizations rather than the result of generic optimizations.
Re:GodBolt: Enter C, get Assembly
by JoshTriplett on Thursday December 15, 2016 @03:18AM
(#13183228)
As I understand it, the bswap optimization doesn't pattern-match for that specific code (and it works for any equivalent shift/mask code); it analyzes the flow of the bits to figure out the final bit pattern relative to the original, realizes the whole expression had the effect of a bswap, and substitutes a bswap.
Actually, in LLVM's case it really is just explicitly checking for a few specific bswap patterns rather than doing anything fancy:
https://github.com/llvm-mirror/llvm/blob/master/lib/Transfor...
Yes, that's called pattern matching.
Not the kind of pattern matching they were talking about.
Re:GodBolt: Enter C, get Assembly
by dangerbird2 on Thursday December 15, 2016 @08:23AM
(#13184370)
The second example makes sense: rdi stores the first subroutine argument in amd64 calling conventions and rax stores the return value. I imagine it's easy for the compiler to infer that the loop is just counting 1 bits, which popcnt does. I imagine if you called functions or initialized local variables in the loop, it would be much more messy
Re:GodBolt: Enter C, get Assembly
by AstralStorm on Thursday December 15, 2016 @04:36AM
(#13183461)
The latter is generally not faster on most modern chips. Results in similar microcode to non vectoring assembly, but you need to put data in the SSE registers which is not free.
A smart compiler would not use the new SSE version. (perhaps you didn't set the flags right?)
This is why: http://0x80.pl/articles/sse-popcount.html