https://gcc.gnu.org/g:c72a38721a678c540e821c68b3e9e1ed2bd6017e

commit r17-6638-gc72a38721a678c540e821c68b3e9e1ed2bd6017e
Author: Michele Dionisio <[email protected]>
Date:   Tue Nov 24 10:15:11 2026 -0700

    [PATCH v2] forwprop: Fold chains of BIT_INSERT_EXPR into a CONSTRUCTOR
    
    A vector built element by element, like
    
      v4sf v = { 0 };
      for (int i = 0; i < 4; i++)
        v[i] = c;
    
    ends up as a chain of BIT_INSERT_EXPRs that overwrites every element:
    
      v_13 = BIT_INSERT_EXPR <v_4(D), c_7(D), 0>;
      v_17 = BIT_INSERT_EXPR <v_13, c_7(D), 32>;
      v_21 = BIT_INSERT_EXPR <v_17, c_7(D), 64>;
      v_25 = BIT_INSERT_EXPR <v_21, c_7(D), 96>;
    
    and is expanded as one insert per element (4 x ins on aarch64, 13
    instructions on x86-64) instead of the single splat produced for the
    equivalent initializer { c, c, c, c }.
    
    The BB SLP vectorizer uses such chains as roots only to vectorize the
    computation of the inserted values: when they are function arguments,
    or the same scalar in every lane, SLP discovery fails and the chain is
    left as it is.
    
    Rewrite a chain of at least two inserts, from its last statement, into
    a CONSTRUCTOR (or a VECTOR_CST when all the elements are constant) when
    the inserts overwrite every element, or when the vector at the start of
    the chain is a VECTOR_CST or a CONSTRUCTOR that provides the remaining
    elements.  A single insert is left alone: it is already one operation
    and other simplifications, like the BIT_FIELD_REF of a BIT_INSERT_EXPR
    in match.pd, only handle that form (gcc.target/i386/pr81502.c).  SSA
    names that occur in abnormal PHIs are not moved into the CONSTRUCTOR.
    
    On x86-64 the splat above becomes a single shufps, on aarch64 a single
    dup.
    
    gcc/ChangeLog:
    
            * tree-ssa-forwprop.cc (simplify_bit_insert_chain): New function.
            (pass_forwprop::execute): Call it for BIT_INSERT_EXPR.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/tree-ssa/forwprop-45.c: New test.
            * gcc.dg/tree-ssa/forwprop-46.c: New test.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Signed-off-by: Michele Dionisio <[email protected]>

Diff:
---
 gcc/testsuite/gcc.dg/tree-ssa/forwprop-45.c |  65 +++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/forwprop-46.c |  15 +++
 gcc/tree-ssa-forwprop.cc                    | 172 ++++++++++++++++++++++++++++
 3 files changed, 252 insertions(+)

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/forwprop-45.c 
b/gcc/testsuite/gcc.dg/tree-ssa/forwprop-45.c
new file mode 100644
index 000000000000..b38f0fa2e75c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/forwprop-45.c
@@ -0,0 +1,65 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+/* A chain of BIT_INSERT_EXPRs that writes every element of a vector, or
+   that starts from a constant or a CONSTRUCTOR, is turned into a single
+   CONSTRUCTOR.  */
+
+typedef float v4sf __attribute__((vector_size(16)));
+typedef int v4si __attribute__((vector_size(16)));
+
+v4sf
+splat_loop (float c)
+{
+  v4sf v = { 0 };
+#pragma GCC unroll 4
+  for (int i = 0; i < 4; i++)
+    v[i] = c;
+  return v;
+}
+
+v4sf
+splat_insert (float c)
+{
+  v4sf v = { 0 };
+  v[0] = c; v[1] = c; v[2] = c; v[3] = c;
+  return v;
+}
+
+v4si
+all_lanes (int a, int b, int c, int d)
+{
+  v4si v;
+  v[3] = d; v[1] = b; v[0] = a; v[2] = c;
+  return v;
+}
+
+v4si
+overwrite (int a, int b, int c, int d)
+{
+  v4si v;
+  v[0] = d; v[1] = b; v[2] = c; v[3] = d; v[0] = a;
+  return v;
+}
+
+v4si
+partial_const (int a, int b)
+{
+  v4si v = { 1, 2, 3, 4 };
+  v[1] = a; v[3] = b;
+  return v;
+}
+
+v4si
+partial_ctor (int a, int b, int c)
+{
+  v4si v = { a, a, a, a };
+  v[2] = b; v[0] = c;
+  return v;
+}
+
+/* { dg-final { scan-tree-dump-not "BIT_INSERT_EXPR" "optimized" } } */
+/* { dg-final { scan-tree-dump-times "= {c_\[0-9\]+\\(D\\), c_\[0-9\]+\\(D\\), 
c_\[0-9\]+\\(D\\), c_\[0-9\]+\\(D\\)}" 2 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "= {a_\[0-9\]+\\(D\\), b_\[0-9\]+\\(D\\), 
c_\[0-9\]+\\(D\\), d_\[0-9\]+\\(D\\)}" 2 "optimized" } } */
+/* { dg-final { scan-tree-dump "= \[{\]1, a_\[0-9\]+\\(D\\), 3, 
b_\[0-9\]+\\(D\\)}" "optimized" } } */
+/* { dg-final { scan-tree-dump "= {c_\[0-9\]+\\(D\\), a_\[0-9\]+\\(D\\), 
b_\[0-9\]+\\(D\\), a_\[0-9\]+\\(D\\)}" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/forwprop-46.c 
b/gcc/testsuite/gcc.dg/tree-ssa/forwprop-46.c
new file mode 100644
index 000000000000..7e3837f985b1
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/forwprop-46.c
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+/* A single insert into a one-element vector overwrites the whole vector,
+   it used to ICE in simple_dce_from_worklist when the vector was a
+   default definition.  */
+
+typedef long long v1di __attribute__((vector_size(8)));
+typedef double v1df __attribute__((vector_size(8)));
+typedef long long v2di __attribute__((vector_size(16)));
+
+v1di f1 (v1di a, v1di b) { a[0] = b[0]; return a; }
+v1df f2 (v1df a, v1df b) { a[0] = b[0]; return a; }
+v1di f3 (v1di a, v2di b) { a[0] = b[1]; return a; }
+v1di f4 (v1di a, long long x) { a[0] = x; return a; }
diff --git a/gcc/tree-ssa-forwprop.cc b/gcc/tree-ssa-forwprop.cc
index 508b49ae4919..d6ffd090f584 100644
--- a/gcc/tree-ssa-forwprop.cc
+++ b/gcc/tree-ssa-forwprop.cc
@@ -5289,6 +5289,175 @@ get_bit_field_ref_def (tree val, enum tree_code 
&conv_code)
   return gimple_assign_rhs1 (def_stmt);
 }
 
+/* Rewrite a chain of BIT_INSERT_EXPRs that each insert one element into
+   a vector, like
+     v_1 = BIT_INSERT_EXPR <v_0, a_2, 0>;
+     v_3 = BIT_INSERT_EXPR <v_1, b_4, 32>;
+     ...
+   into a single CONSTRUCTOR {a_2, b_4, ...} (or a VECTOR_CST) when the
+   inserts overwrite every element of the vector, or when the vector at
+   the start of the chain is a VECTOR_CST or a CONSTRUCTOR that provides
+   the remaining elements.  A CONSTRUCTOR is expanded with vec_init, which
+   for instance turns a broadcast written element by element into a single
+   splat instruction.  The statements of the chain that become dead are
+   queued in SIMPLE_DCE_WORKLIST.  Returns true if there were any
+   changes.  */
+
+static bool
+simplify_bit_insert_chain (gimple_stmt_iterator *gsi,
+                          bitmap simple_dce_worklist)
+{
+  gassign *stmt = as_a <gassign *> (gsi_stmt (*gsi));
+  tree lhs = gimple_assign_lhs (stmt);
+  tree type = TREE_TYPE (lhs);
+  unsigned HOST_WIDE_INT nelts;
+
+  if (TREE_CODE (type) != VECTOR_TYPE
+      || VECTOR_BOOLEAN_TYPE_P (type)
+      || !TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
+    return false;
+
+  /* Handle the chain once, from its last statement.  */
+  use_operand_p use_p;
+  gimple *use_stmt;
+  if (single_imm_use (lhs, &use_p, &use_stmt)
+      && is_gimple_assign (use_stmt)
+      && gimple_assign_rhs_code (use_stmt) == BIT_INSERT_EXPR
+      && gimple_assign_rhs1 (use_stmt) == lhs)
+    return false;
+
+  tree elt_type = TREE_TYPE (type);
+  unsigned HOST_WIDE_INT elt_bits = vector_element_bits (type);
+  auto_vec<tree, 16> elts;
+  elts.safe_grow_cleared (nelts, true);
+  unsigned HOST_WIDE_INT filled = 0;
+  unsigned HOST_WIDE_INT ninserts = 0;
+  gassign *def = stmt;
+  tree base = NULL_TREE;
+  do
+    {
+      tree val = gimple_assign_rhs2 (def);
+      tree pos = gimple_assign_rhs3 (def);
+      if (!types_compatible_p (TREE_TYPE (val), elt_type)
+         || !tree_fits_uhwi_p (pos)
+         || tree_to_uhwi (pos) % elt_bits != 0)
+       break;
+      unsigned HOST_WIDE_INT lane = tree_to_uhwi (pos) / elt_bits;
+      if (lane >= nelts)
+       break;
+      /* A later insert into the same lane wins.  */
+      if (!elts[lane])
+       {
+         elts[lane] = val;
+         filled++;
+       }
+      ninserts++;
+      base = gimple_assign_rhs1 (def);
+      /* Continue with the previous insert of the chain while some lanes
+        are still unknown.  Only look through inserts used by the chain
+        alone, the others have to stay anyway.  */
+      def = NULL;
+      if (filled < nelts && TREE_CODE (base) == SSA_NAME)
+       {
+         /* Bound the walk if the same lanes are written many times.  */
+         if (ninserts >= 2 * nelts)
+           return false;
+         gassign *prev = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (base));
+         if (prev
+             && gimple_assign_rhs_code (prev) == BIT_INSERT_EXPR
+             && has_single_use (base))
+           def = prev;
+       }
+    }
+  while (def);
+  /* A single insert is already a single operation, and other
+     simplifications (like extracting part of the inserted value with a
+     BIT_FIELD_REF) only handle BIT_INSERT_EXPR.  Requiring two inserts
+     also means that the first operand of STMT is defined by an insert of
+     the chain, which is what is queued for DCE below.  */
+  if (ninserts < 2)
+    return false;
+
+  /* Take the elements that are not overwritten from the start of the
+     chain.  */
+  if (filled < nelts)
+    {
+      if (TREE_CODE (base) == VECTOR_CST)
+       {
+         for (unsigned HOST_WIDE_INT i = 0; i < nelts; i++)
+           if (!elts[i])
+             elts[i] = VECTOR_CST_ELT (base, i);
+       }
+      else if (TREE_CODE (base) == SSA_NAME)
+       {
+         gassign *bdef = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (base));
+         if (!bdef || gimple_assign_rhs_code (bdef) != CONSTRUCTOR)
+           return false;
+         tree ctor = gimple_assign_rhs1 (bdef);
+         if (!types_compatible_p (TREE_TYPE (ctor), type))
+           return false;
+         /* Elements that are vectors themselves are not handled.  */
+         if (CONSTRUCTOR_NELTS (ctor) != 0
+             && TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value))
+                == VECTOR_TYPE)
+           return false;
+         for (unsigned HOST_WIDE_INT i = 0; i < nelts; i++)
+           if (!elts[i])
+             {
+               if (i < CONSTRUCTOR_NELTS (ctor))
+                 {
+                   constructor_elt *ce = CONSTRUCTOR_ELT (ctor, i);
+                   if (ce->index
+                       && (!tree_fits_uhwi_p (ce->index)
+                           || tree_to_uhwi (ce->index) != i))
+                     return false;
+                   elts[i] = ce->value;
+                 }
+               else
+                 /* Trailing elements of a CONSTRUCTOR are zero.  */
+                 elts[i] = build_zero_cst (elt_type);
+             }
+       }
+      else
+       return false;
+    }
+
+  /* The CONSTRUCTOR uses the elements at the place of the last insert,
+     which can extend their live range.  Do not do that for SSA names
+     that occur in abnormal PHIs.  */
+  bool all_constant = true;
+  for (tree elt : elts)
+    if (TREE_CODE (elt) == SSA_NAME
+       && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (elt))
+      return false;
+    else if (!CONSTANT_CLASS_P (elt))
+      all_constant = false;
+
+  tree new_rhs;
+  if (all_constant)
+    {
+      tree_vector_builder builder (type, nelts, 1);
+      for (tree elt : elts)
+       builder.quick_push (elt);
+      new_rhs = builder.build ();
+    }
+  else
+    {
+      vec<constructor_elt, va_gc> *ce;
+      vec_alloc (ce, nelts);
+      for (tree elt : elts)
+       CONSTRUCTOR_APPEND_ELT (ce, NULL_TREE, elt);
+      new_rhs = build_constructor (type, ce);
+    }
+
+  tree old_vec = gimple_assign_rhs1 (stmt);
+  if (TREE_CODE (old_vec) == SSA_NAME)
+    bitmap_set_bit (simple_dce_worklist, SSA_NAME_VERSION (old_vec));
+  gimple_assign_set_rhs_from_tree (gsi, new_rhs);
+  update_stmt (gsi_stmt (*gsi));
+  return true;
+}
+
 /* Recognize a VEC_PERM_EXPR.  Returns true if there were any changes.  */
 
 static bool
@@ -7271,6 +7440,9 @@ pass_forwprop::execute (function *fun)
                    else if (code == CONSTRUCTOR
                             && TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE)
                      changed |= simplify_vector_constructor (&gsi);
+                   else if (code == BIT_INSERT_EXPR)
+                     changed |= simplify_bit_insert_chain (&gsi,
+                                                          simple_dce_worklist);
                    else if (code == ARRAY_REF)
                      changed |= simplify_count_zeroes (&gsi);
                    else if (get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS

Reply via email to