https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127860
--- Comment #3 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Richard Biener <[email protected]>: https://gcc.gnu.org/g:caab3df20accebf99913287c148ed64ea809fb9b commit r17-5215-gcaab3df20accebf99913287c148ed64ea809fb9b Author: Igor Shevlyakov <[email protected]> Date: Tue Oct 6 17:24:40 2026 -0700 optabs: Reduce const permute selectors by the input length [PR127860] expand_vec_perm_const builds its vec_perm_indices with the element count of the result mode. Since GCC 13 the inputs of a VEC_PERM_EXPR with a constant selector may have a different number of elements than the result, and the selector indexes the concatenation of the inputs. The vec_perm_builder only carries the length of the selector, which is the length of the result, so for inputs with more elements than the result every index at or above twice the result length is wrapped into the first input before the target hook sees it. That is silent wrong code: a V4SI permute of two V8SI inputs with selector { 0, 8, 1, 9 } reaches the hook as { 0, 0, 1, 1 }, although can_vec_perm_const_p asked the hook about { 0, 8, 1, 9 } (tree_to_vec_perm_indices uses the input length). Build the indices with the input mode's element count. As in can_vec_perm_const_p, only try vec_shr and vec_shl when the inputs have the result mode and a constant number of elements. The fallbacks after the target hook permute in the result mode, so give up before them for other inputs. No in-tree target currently accepts a narrowing permute in its vec_perm_const hook, so this is latent upstream; aarch64 accepts mixed widths only for SVE dupq, which widens and only uses indices below the input length, so it is unaffected. Assisted-by: Claude Opus 5.5 gcc/ChangeLog: PR middle-end/127860 * optabs.cc (expand_vec_perm_const): Reduce the selector by twice the input length. Only use vec_shr and vec_shl for constant-length inputs of the result mode, and the fallbacks for inputs of the result mode. Check earlier that the inputs have the same mode. gcc/testsuite/ChangeLog: PR middle-end/127860 * gcc.dg/torture/vec-perm-wide-input-1.c: New test. Signed-off-by: Igor Shevlyakov <[email protected]>
