4 ms·
The part of this that's the most interesting to me is the fact that it is worthwhile for the compiler to expend the effort looking for this optimization opportu
by piinbinary 3mo ago
The part of this that's the most interesting to me is the fact that it is worthwhile for the compiler to expend the effort looking for this optimization opportunity. I would expect (x << 2) & -4 to be a fairly rare pattern, and even then removing the & -4 only saves one or two assembly instructions.
- ghusbands 3mo agoEvery optimisation like this increases the chance of other optimisations/vectorisations being able to usefully fire, and sometimes these very specific cases are themselves from optimisations or type/range restrictions.
- SideQuark 3mo agoIt's because the underlying optimizer follows patterns in LLVM, which is used for a lot of languages, most applicable here is C/C++, and bit things like this are used a lot for performance. The particular patterns were added to C2 just recently, being copied from the implementations in LLVM and GCC. Also it's unlikely the compiler looks for this in particular, but this falls out of a set of optimizations that do matter and it collapses into the output assembly on relevant architectures.
- pron 3mo agogcc and LLVM have also copied ideas from C2, and there's been cross-pollination between Java and C++ compilers going back decades. All three are among the most sophisticated optimising compilers right now.