Instruction pmaxsw not generated
The code generated by 'x86-64 clang 13.0.1' in /O3 will generate pmaxsw instructions. With 'x86-64 clang 14.0.0' and 'x86-64 clang (trunk)', the pmaxw is not generated, and the code size is slightly bigger.
short vecreduce_smax_v2i16(int n, short* v)
{
short p = 0;
for (int i = 0; i < n; ++i)
p = p < v[i] ? v[i] : p;
return p;
}
@llvm/issue-subscribers-backend-x86
Regressed with new intrinsics?
@rotateright @nikic
Godbolt: https://godbolt.org/z/7vxqbMzaa
This looks to be generating pmaxsw instructions to me?
Here is what I was seeing: https://godbolt.org/z/hxz1xhMPh
hm, lul. Difference is whether we are in C or C++ mode.
https://godbolt.org/z/8zeqzoff9
Interestingly, Clang in C mode creates phi i32, in C++ phi i16.
Caused by some language rules, @AaronBallman ?
@llvm/issue-subscribers-clang-codegen
Independent of the front-end question, there seems to be an optimizer enhancement opportunity. With the phi i32 (seen in the 'C' source IR example), both incoming values are sexts initially, but -ipsccp manages to convert one into a zext.
This patch made ipsccp smarter, but I'm not sure if that's where the difference on this example shows up (might be in some underlying analysis too): https://reviews.llvm.org/D81756
Instcombine / ValueTracking then can't reconcile the difference when used in an icmp, and so we miss recognizing the min/max pattern.
So we either need to make ipsccp smarter again or fix ValueTracking (isKnownNonNegative) + instcombine...or both.
There's a potentially-related ValueTracking improvement proposed here: https://reviews.llvm.org/D123748
Ie, maybe we can use matchSimpleRecurrence to recognize that a phi value is never negative.
https://reviews.llvm.org/D124419
Strangely enough, changing the < into a > in the test unrolls the loop correctly with calls to pminsw... It seems to be a regression of the 13.0.1 version: https://godbolt.org/z/cPzWjT86T