News for hackers. Stuff that matters.
Re:Popcount CPU instruction by woodruffw on Sunday September 08, 2019 @10:49PM (#20914835)
> What other idioms of similar or higher complexity can these compilers recognize and convert to instructions?

Both GCC and clang (really LLVM) will try to autovectorize your loops, replacing operations on individual words with SIMD instructions (provided the compilation target supports them).

Similarly, both should replace `popcnt`-style functions (e.g., count-leading-zeros) with their instruction equivalents, provided the implementation is idiomatic enough. The overarching challenge is that users who don't know about instructions like `popcnt` and `clz` are also less likely to write implementations that are easy to detect and swap.

Re:Popcount CPU instruction by vnorilo on Monday September 09, 2019 @12:34AM (#20915224)
I don't recall seeing bit counting in the LLVM idiom recognition, but it's been years since I pored over it. Could you maybe Godbolt an example where this substitution happens? I'd be happy to be wrong in this case :)
Re:Popcount CPU instruction by kardos on Monday September 09, 2019 @12:39AM (#20915242)
Re:Popcount CPU instruction by account42 on Monday September 09, 2019 @03:52AM (#20916049)
Any idea why GCC zeroes out eax before overwriting it?

It never fails to amaze me how smart compilers can be while also being so stupid sometimes.

Re:Popcount CPU instruction by vnorilo on Monday September 09, 2019 @04:26AM (#20916199)
It could be a bug; I don't see an immediate reason to zero eax in this fragment. Sometimes it's easy to miss the reasons why compilers zero apparently unused things though: it can be about breaking data dependencies or making sure flags are in a known state.
Re:Popcount CPU instruction by gpderetta on Monday September 09, 2019 @04:49AM (#20916314)
It is a bug, but in the CPU. Until recently, on intel cpu, the popcnt instruction had an implicit dependency on its output register, which can be a performance issue. Explicitly clearing the register (which is very fast and almost free), is an easy way to workaround it.
Re:Popcount CPU instruction by account42 on Monday September 09, 2019 @07:28AM (#20917135)
Oh, nice info. It's gone when using e.g. -march=znver1.
Re:Popcount CPU instruction by vnorilo on Monday September 09, 2019 @04:13AM (#20916145)
This thread [1] might give you some idea of the complexity involved in a similar issue.

OTOH, if you see RAX being zeroed before using EAX, it is likely to be about breaking false deps.

1: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86314

Re:Popcount CPU instruction by vnorilo on Monday September 09, 2019 @12:42AM (#20915249)
Indeed, thanks! Skimmed a bit too quickly.