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

Drea Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2026-11-08
     Ever confirmed|0                           |1

--- Comment #1 from Drea Pinski <pinskia at gcc dot gnu.org> ---
  _1 = acc_5(D) + _2;
  _7 = _1 >> 1;

is being turned into:
  _7 = .AVG_FLOOR (_2, acc_5(D));

By:
```
 /* Fold (x + y) >> 1 into IFN_AVG_FLOOR (x, y) and ((x + y) + 1) >> 1
    into IFN_AVG_CEIL (x, y) if x and y are vectors in which the sum
    cannot wrap: overflow is undefined, or each element of x and y is
    known to have at least one leading zero bit, or two if the type is
    signed, since the sum of two nonnegative values can then wrap to a
    negative one.  The IFN_AVG_CEIL fold comes first because
    ((x + y) + 1) >> 1 is also (x' + 1) >> 1.  */
(simplify
 (rshift (plus:s (plus:cs @0 @1) integer_onep) integer_onep)
 (if (VECTOR_TYPE_P (type)
      && direct_internal_fn_supported_p (IFN_AVG_CEIL, type,
                                         OPTIMIZE_FOR_BOTH))
  (with { int min_clz = TYPE_UNSIGNED (type) ? 1 : 2; }
   (if (TYPE_OVERFLOW_UNDEFINED (type)
        || (wi::clz (get_nonzero_bits (@0)) >= min_clz
            && wi::clz (get_nonzero_bits (@1)) >= min_clz))
    (IFN_AVG_CEIL @0 @1)))))
(simplify
 (rshift (plus:cs @0 @1) integer_onep)
 (if (VECTOR_TYPE_P (type)
      && direct_internal_fn_supported_p (IFN_AVG_FLOOR, type,
                                         OPTIMIZE_FOR_BOTH))
  (with { int min_clz = TYPE_UNSIGNED (type) ? 1 : 2; }
   (if (TYPE_OVERFLOW_UNDEFINED (type)
        || (wi::clz (get_nonzero_bits (@0)) >= min_clz
            && wi::clz (get_nonzero_bits (@1)) >= min_clz))
    (IFN_AVG_FLOOR @0 @1)))))
```
Which I think is correct.

The problem is before that.
vaba_s8 is being turned into:
```
  _2 = .ABD (x_3(D), y_4(D));
  _1 = acc_5(D) + _2;
```
But this assumes there is no overflow for `acc_5(D) + _2` but there is in this
case.

Reply via email to