vect: Fix wrong code with pr108692.c on targets with only non-widening ABD [PR118727]mastertrunk
commitda88e7027a34a44de84f6d8d5a96d262c29080a7
authorXi Ruoyao <xry111@xry111.site>
Sun, 2 Feb 2025 13:22:36 +0000 (2 21:22 +0800)
committerXi Ruoyao <xry111@xry111.site>
Wed, 5 Feb 2025 09:44:07 +0000 (5 17:44 +0800)
tree887991b0d826b9c75a8adc3f6df63a53ce971a7c
parent754137d9cb741dcbbd27b03bddb3dc10d1376421
vect: Fix wrong code with pr108692.c on targets with only non-widening ABD [PR118727]

With things like

  // signed char a_14, a_16;
  a.0_4 = (unsigned char) a_14;
  _5 = (int) a.0_4;
  b.1_6 = (unsigned char) b_16;
  _7 = (int) b.1_6;
  c_17 = _5 - _7;
  _8 = ABS_EXPR <c_17>;
  r_18 = _8 + r_23;

An ABD pattern will be recognized for _8:

  patt_31 = .ABD (a.0_4, b.1_6);

It's still correct.  But then when the SAD pattern is recognized:

  patt_29 = SAD_EXPR <a_14, b_16, r_23>;

This is not correct.  This only happens for targets with both uabd and
sabd but not vec_widen_{s,u}abd, currently LoongArch is the only target
affected.

The problem is vect_look_through_possible_promotion will throw away a
series of conversions if the effect is equivalent to a sign change and a
promotion, but here the sign change is definitely relevant, and the
promotion is also relevant for "mixed sign" cases like
r += abs((unsigned int)(unsigned char) a - (signed int)(signed char) b
(we need to promote to HImode as the difference can exceed the range of
QImode).

If there were any redundant promotion, it should have been stripped in
vect_recog_abd_pattern (i.e. when patt_31 = .ABD (a.0_4, b.1_6) is
recognized) instead of in vect_recog_sad_pattern, or we'd have a
missed-optimization if the ABD output is not summerized.  So anyway
vect_recog_sad_pattern is just not a proper location to call
vect_look_through_possible_promotion for the ABD inputs, remove the
calls to fix the issue.

gcc/ChangeLog:

PR tree-optimization/118727
* tree-vect-patterns.cc (vect_recog_sad_pattern): Don't call
vect_look_through_possible_promotion on ABD inputs.

gcc/testsuite/ChangeLog:

PR tree-optimization/118727
* gcc.dg/pr108692.c: Mention PR 118727 in the comment.
* gcc.dg/pr118727.c: New test case.
gcc/testsuite/gcc.dg/pr108692.c
gcc/testsuite/gcc.dg/pr118727.c [copied from gcc/testsuite/gcc.dg/pr108692.c with 67% similarity]
gcc/tree-vect-patterns.cc