https://gcc.gnu.org/g:1f2d2c09eab4dbb18c531ab177fac1b977687d79
commit r17-6650-g1f2d2c09eab4dbb18c531ab177fac1b977687d79 Author: Igor Shevlyakov <[email protected]> Date: Wed Oct 28 08:13:22 2026 -0700 vect-generic: Do not give up on a divisor element of -1 The multiply-high analysis in expand_vector_divmod returned NULL_TREE for a signed divisor element of -1, which also discarded the power-of-two path. Since division by a negative power of two is handled there, -1 is -2^0 and qualifies: the quotient lane is -(x >> 0) and the remainder lane x & 0. So a single -1 element made a remainder expand element-wise where the same divisor with 1 in its place uses the mask sequence: on x86-64, for example, x % { -1, 2, -4, 8, -16, 32, -64, 128 } on V8HI with -msse2, or x % { -4, 2, -1, 8 } on V4SI with -mavx2. Richard Biener pointed out the early return in the review of r17-5677-ge88c76e0a7ef. Drop the early return. -1 now takes the abs_d <= 1 case, which only disables the multiply-high path, as it already does for 1; when the power-of-two path does not apply either, the result is NULL_TREE as before. Assisted-by: Claude Opus 5.5 gcc/ChangeLog: * tree-vect-generic.cc (expand_vector_divmod): Do not return early for a signed divisor element of -1. gcc/testsuite/ChangeLog: * gcc.dg/vect/veclower/vec-div-neg-pow2-2.c: New test. Signed-off-by: Igor Shevlyakov <[email protected]> Diff: --- .../gcc.dg/vect/veclower/vec-div-neg-pow2-2.c | 25 ++++++++++++++++++++++ gcc/tree-vect-generic.cc | 3 --- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/gcc/testsuite/gcc.dg/vect/veclower/vec-div-neg-pow2-2.c b/gcc/testsuite/gcc.dg/vect/veclower/vec-div-neg-pow2-2.c new file mode 100644 index 000000000000..567103e3f212 --- /dev/null +++ b/gcc/testsuite/gcc.dg/vect/veclower/vec-div-neg-pow2-2.c @@ -0,0 +1,25 @@ +/* { dg-do compile } */ +/* { dg-require-effective-target vect_int } */ +/* { dg-require-effective-target vect_condition } */ +/* { dg-require-effective-target vect128 } */ + +/* A divisor element of -1 is -2^0: the remainder is a mask, as for the + other power-of-two elements, not an element-wise expansion. */ + +typedef int v4si __attribute__ ((vector_size (16))); +typedef short v8hi __attribute__ ((vector_size (16))); + +v4si +f1 (v4si x) +{ + return x % (v4si) { -4, 2, -1, 8 }; +} + +v8hi +f2 (v8hi x) +{ + return x % (v8hi) { -1, 2, -4, 8, -16, 32, -64, 128 }; +} + +/* { dg-final { scan-tree-dump-not "BIT_FIELD_REF" "veclower21" } } */ +/* { dg-final { scan-tree-dump-not " h\\* " "veclower21" } } */ diff --git a/gcc/tree-vect-generic.cc b/gcc/tree-vect-generic.cc index f9cd17db8b49..df4db71f9dcf 100644 --- a/gcc/tree-vect-generic.cc +++ b/gcc/tree-vect-generic.cc @@ -863,9 +863,6 @@ expand_vector_divmod (gimple_stmt_iterator *gsi, tree type, tree op0, HOST_WIDE_INT d = TREE_INT_CST_LOW (cst); unsigned HOST_WIDE_INT abs_d; - if (d == -1) - return NULL_TREE; - /* Since d might be INT_MIN, we have to cast to unsigned HOST_WIDE_INT before negating to avoid undefined signed overflow. */
