https://gcc.gnu.org/g:0841d1b8c9e0dd9f3caa0806146dffc9369e25f1

commit r17-5872-g0841d1b8c9e0dd9f3caa0806146dffc9369e25f1
Author: liuhongt <[email protected]>
Date:   Tue Oct 27 20:24:19 2026 -0700

    i386: Cost scalar division by a constant as its expansion
    
    ix86_vector_costs::add_stmt_cost costed every scalar integer division
    and modulo by the divide instruction, also when the divisor is a
    constant and the operation is expanded to shifts or a multiplication.
    This made the scalar cost of division by a constant inaccurate during
    vectorization costing; for example, an exact division by a power of two
    such as a pointer difference is a single scalar arithmetic shift, but
    charging the divide instruction cost made basic-block SLP vectorization
    into an emulated 64-bit vector shift look profitable.
    
    Return a rough cost of the scalar integer TRUNC_DIV_EXPR, TRUNC_MOD_EXPR
    or EXACT_DIV_EXPR by the nonzero constant divisor, approximating
    expand_divmod in expmed.cc.
    
    gcc/ChangeLog:
    
            * config/i386/i386.cc (ix86_const_divmod_cost): New function.
            (ix86_vector_costs::add_stmt_cost): Use it for scalar
            TRUNC_DIV_EXPR, TRUNC_MOD_EXPR and EXACT_DIV_EXPR by a nonzero
            constant.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.target/i386/slp-exact-div-cost.c: New test.

Diff:
---
 gcc/config/i386/i386.cc                            | 56 +++++++++++++++++++++-
 gcc/testsuite/gcc.target/i386/slp-exact-div-cost.c | 18 +++++++
 2 files changed, 72 insertions(+), 2 deletions(-)

diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc
index 49267103f1ca..69672ea94a94 100644
--- a/gcc/config/i386/i386.cc
+++ b/gcc/config/i386/i386.cc
@@ -22901,6 +22901,43 @@ ix86_division_cost (const struct processor_costs *cost,
     return cost->divide[MODE_INDEX (mode)];
 }
 
+/* Return a rough cost of the scalar integer TRUNC_DIV_EXPR, TRUNC_MOD_EXPR
+   or EXACT_DIV_EXPR CODE in MODE by the nonzero constant DIVISOR,
+   approximating expand_divmod in expmed.cc.  UNS is true for unsigned
+   operations.  */
+
+static int
+ix86_const_divmod_cost (const struct processor_costs *cost,
+                       enum tree_code code, machine_mode mode,
+                       tree divisor, bool uns)
+{
+  bool mod_p = code == TRUNC_MOD_EXPR;
+  if (tree_int_cst_sgn (divisor) == 1 && integer_pow2p (divisor))
+    {
+      /* Unsigned or exact division/modulo is a single shift or mask.  */
+      if (uns || code == EXACT_DIV_EXPR)
+       return mod_p ? cost->add : cost->shift_const;
+
+      /* Signed division/modulo adjusts negative dividends first.  */
+      return cost->shift_const + 2 * cost->add + (mod_p ? cost->add : 0);
+    }
+
+  int mult = ix86_multiplication_cost (cost, mode);
+
+  /* Exact division multiplies by the modular inverse.  */
+  if (code == EXACT_DIV_EXPR)
+    return mult;
+
+  /* Truncating division uses high-part multiply, shift and add.  */
+  int c = mult + cost->shift_const + cost->add;
+
+  /* Modulo computes X - (X / D) * D.  */
+  if (mod_p)
+    c += mult + cost->add;
+
+  return c;
+}
+
 /* Return cost of shift in MODE.
    If CONSTANT_OP1 is true, the op1 value is known and set in OP1_VAL.
    AND_IN_OP1 specify in op1 is result of AND and SHIFT_AND_TRUNCATE
@@ -27028,15 +27065,30 @@ ix86_vector_costs::add_stmt_cost (int count, 
vect_cost_for_stmt kind,
            stmt_cost = ix86_cost->add;
          break;
        case TRUNC_DIV_EXPR:
+       case TRUNC_MOD_EXPR:
+       case EXACT_DIV_EXPR:
+         {
+           tree op2 = gimple_assign_rhs2 (stmt_info->stmt);
+           if (kind == scalar_stmt
+               && SCALAR_INT_MODE_P (mode)
+               && GET_MODE_SIZE (mode) <= UNITS_PER_WORD
+               && TREE_CODE (op2) == INTEGER_CST
+               && !integer_zerop (op2))
+             {
+               stmt_cost = ix86_const_divmod_cost
+                             (ix86_cost, subcode, mode, op2,
+                              TYPE_UNSIGNED (TREE_TYPE (op2)));
+               break;
+             }
+         }
+         /* FALLTHRU */
        case CEIL_DIV_EXPR:
        case FLOOR_DIV_EXPR:
        case ROUND_DIV_EXPR:
-       case TRUNC_MOD_EXPR:
        case CEIL_MOD_EXPR:
        case FLOOR_MOD_EXPR:
        case RDIV_EXPR:
        case ROUND_MOD_EXPR:
-       case EXACT_DIV_EXPR:
          stmt_cost = ix86_division_cost (ix86_cost, mode);
          break;
 
diff --git a/gcc/testsuite/gcc.target/i386/slp-exact-div-cost.c 
b/gcc/testsuite/gcc.target/i386/slp-exact-div-cost.c
new file mode 100644
index 000000000000..12356eec8ab6
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/slp-exact-div-cost.c
@@ -0,0 +1,18 @@
+/* { dg-do compile { target { ! ia32 } } } */
+/* { dg-options "-O2 -ftree-slp-vectorize -march=x86-64-v3" } */
+
+/* The scalar cost of an exact division by a power of two is a shift, so
+   the two pointer differences of the std::deque size computation are not
+   worth vectorizing.  */
+
+struct it { void **cur, **first, **last, ***node; };
+struct dq { void ***map; unsigned long map_size; struct it start, finish; };
+
+long
+size (const struct dq *d)
+{
+  return 64 * (d->finish.node - d->start.node - (d->finish.node != 0))
+        + (d->finish.cur - d->finish.first) + (d->start.last - d->start.cur);
+}
+
+/* { dg-final { scan-assembler-not "vpsubq" } } */

Reply via email to