https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129151
Bug ID: 129151
Summary: rs6000 swap optimisation treats (subreg:V16QI
(reg:TF/TD)) as swappable: wrong 16-byte byte reversal
on POWER8 LE
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: pmenzel+gcc at molgen dot mpg.de
Target Milestone: ---
Created attachment 66336
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66336&action=edit
(`long double`, IBM double-double): correct output
`00000000000000004008000000000000`, wrong `40080000000000000000000000000000`
AI disclaimer: Found and drafted by claude-opus-5.5 and reviewed by me.
Reproducers (a `memcpy` of a 16-byte scalar into a buffer, then a `char`-wise
reversal of the 16 bytes, which SLP vectorises to a `vperm`):
- `gcc16-slp-ldouble-bswap.c` (`long double`, IBM double-double): correct
output `00000000000000004008000000000000`, wrong
`40080000000000000000000000000000`.
- `gcc-p8swap-subreg-dec128.c` (`_Decimal128`): correct output
`2203c000000000000600000000000000`, wrong `06000000000000002203c00000000000`.
In both cases the two 8-byte halves of the result are exchanged.
| Compiler | `long double`
`-O2`/`-O3` | `_Decimal128` `-O3` |
|-------------------------------------------------------|---------------------------|---------------------|
| GCC 7.5, 8.5, 9.4, 10.3 (Ubuntu) | correct
| correct (no SLP) |
| GCC 11.2 (Ubuntu) | correct
| wrong |
| GCC 12.5.0, 13.1.0, 14.1.0 | correct
| wrong |
| GCC 15.1.0 | wrong
| not tested |
| GCC 16.1.0 | wrong
| wrong |
| trunk 230b204d15b4 (2026-11-29, bootstrapped, 17.0.0) | wrong
| wrong |
All wrong cases are correct with `-mno-optimize-swaps` or
`-fno-tree-slp-vectorize`, and at `-O1`. The release builds are
non-bootstrapped (`--disable-bootstrap`, C only); the trunk build is a full
bootstrap. `_Complex double`, a struct of two doubles, `__float128` and
`__int128` give correct code with all of them; `__ibm128` behaves like `long
double`.
For `long double`, bisecting between `basepoints/gcc-15` (good) and
`basepoints/gcc-16` (bad) gives
33dca0a4c1c421625cedb2d6105ef1c05f6b774e is the first bad commit
rs6000: Make all 128 bit scalar FP modes have 128 bit precision [PR112993]
but that commit only exposes an older problem in the swap optimisation, which
`_Decimal128` already hits with GCC 11 (GCC 10 does not SLP-vectorise the
loop).
Analysis (trunk, `-fdump-rtl-swaps`): the vectorised value enters the web
through
(insn 17 16 18 4 (set (reg:V16QI 127 [ _45 ])
(subreg:V16QI (reg:TF 125 [ _1 ]) 0)) 1251 {vsx_movv16qi_64bit}
where reg 125 is set by `extenddftf2_vsx`. The pass lists insn 17 as
`swappable`, adjusts the `vperm` mask and removes the two doubleword swaps of
the web. In `rs6000_analyze_swaps` a use only counts as a vector mention if
`GET_MODE (DF_REF_REAL_REG (mention))` is a vector mode or TI/PTI
(`rs6000-p8swap.cc:2486`). For this use, `DF_REF_REAL_REG` looks through the
subreg to `reg:TF`, which is neither, so the use is not unioned with its
definition and `contains_subreg` is not set. The insn is then handled like a
plain register copy although its input is in natural element order, not
doubleword-swapped. With GCC 11 to 14 the same happens for `(subreg:V16QI
(reg:TD …))`. `KFmode` is unaffected because `FLOAT128_VECTOR_P` holds for it.
Before r15-2084, expand stores the IBM `long double` to a stack slot and
reloads it as `(vec_select:V16QI (mem/c:V16QI …))`, a swapping load the pass
handles correctly; since that commit the value goes through the register subreg
above.
Generated code of the swap branch for `long double`:
```
# GCC 14.1.0 (correct) # 33dca0a4c1c and later (wrong)
addis 9,2,.LC1@toc@ha addis 9,2,.LC1@toc@ha
li 10,32 addi 9,9,.LC1@toc@l
addi 9,9,.LC1@toc@l lxvd2x 32,0,9 # mask
addi 8,1,-48 addi 9,1,-16
lxvd2x 33,0,9 # mask lvx 1,0,9 # the long double
lxvd2x 32,8,10 # the long double vperm 0,1,1,0
vperm 0,0,0,1 stxvd2x 32,0,3
stxvd2x 32,0,3
```
GCC 11.2 generates the right-hand pattern for `_Decimal128`. A possible fix is
to treat a use whose subreg looks through to a non-vector 16-byte mode (TF, IF,
TD, or generally any mode the web does not track) as making the web
unoptimisable; I have not tried this.