llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Instruction pmaxsw not generated

Open mariannems opened this issue 4 years ago • 10 comments

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;
}

mariannems avatar Apr 21 '22 13:04 mariannems

@llvm/issue-subscribers-backend-x86

llvmbot avatar Apr 21 '22 14:04 llvmbot

Regressed with new intrinsics?

@rotateright @nikic

davidbolvansky avatar Apr 21 '22 16:04 davidbolvansky

Godbolt: https://godbolt.org/z/7vxqbMzaa

This looks to be generating pmaxsw instructions to me?

RKSimon avatar Apr 21 '22 16:04 RKSimon

Here is what I was seeing: https://godbolt.org/z/hxz1xhMPh

mariannems avatar Apr 21 '22 16:04 mariannems

hm, lul. Difference is whether we are in C or C++ mode.

https://godbolt.org/z/8zeqzoff9

davidbolvansky avatar Apr 21 '22 17:04 davidbolvansky

Interestingly, Clang in C mode creates phi i32, in C++ phi i16.

Caused by some language rules, @AaronBallman ?

davidbolvansky avatar Apr 22 '22 20:04 davidbolvansky

@llvm/issue-subscribers-clang-codegen

llvmbot avatar Apr 22 '22 20:04 llvmbot

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.

rotateright avatar Apr 25 '22 15:04 rotateright

https://reviews.llvm.org/D124419

rotateright avatar Apr 25 '22 21:04 rotateright

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

leo-ard avatar Sep 15 '23 18:09 leo-ard