News for hackers. Stuff that matters.
Re:GodBolt: Enter C, get Assembly by JoshTriplett on Thursday December 15, 2016 @02:52AM (#13183154)
One optimization I've always found impressive:

    #include <stdint.h>

    uint32_t testFunction(uint32_t x)
    {
        return ((x & 0xff) << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | ((x & 0xff000000) >> 24);
    }
compiles into:

    testFunction(unsigned int):
        mov     eax, edi
        bswap   eax
        ret
Another fun one, that only works with clang:

    #include <stdint.h>

    int testFunction(uint64_t x)
    {
        int count;
        for (count = 0; x; count++)
            x &= x - 1;
        return count;
    }
compiles into:

    testFunction(unsigned long):
        popcnt  rax, rdi
        ret
Re:GodBolt: Enter C, get Assembly by gratilup on Thursday December 15, 2016 @03:11AM (#13183209)
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.
Re:GodBolt: Enter C, get Assembly by acid__ on Thursday December 15, 2016 @05:08AM (#13183567)
Now I'm curious, what are the most impressive things modern compilers do?
Re:GodBolt: Enter C, get Assembly by wolf550e on Thursday December 15, 2016 @05:14AM (#13183588)
Re:GodBolt: Enter C, get Assembly by eridius on Thursday December 15, 2016 @03:08AM (#13183205)
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.
Re:GodBolt: Enter C, get Assembly by sunfish on Thursday December 15, 2016 @11:02AM (#13185477)
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...

Re:GodBolt: Enter C, get Assembly by dom0 on Thursday December 15, 2016 @05:39AM (#13183673)
Yes, that's called pattern matching.
Re:GodBolt: Enter C, get Assembly by IshKebab on Thursday December 15, 2016 @05:51AM (#13183730)
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

Re:GodBolt: Enter C, get Assembly by gpderetta on Thursday December 15, 2016 @07:59AM (#13184263)
There is no sse usage in the popcnt example posted by the OP. That's probably why you are getting downvoted.