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

commit r17-5810-ge9a08d39562f585346818a6edc8c28b05c417328
Author: Kyrylo Tkachov <[email protected]>
Date:   Thu Oct 15 20:58:19 2026 +0200

    forwprop: do not fold a CONSTRUCTOR into an abnormal PHI vector [PR128013]
    
    simplify_vector_constructor replaces a CONSTRUCTOR of BIT_FIELD_REFs of
    one or two vectors by a permutation of these vectors, or by the vector
    itself, at the place of the CONSTRUCTOR.  This extends the live ranges
    of the vectors.  When such a vector is an SSA name that occurs in an
    abnormal PHI and is redefined before the CONSTRUCTOR, the live ranges of
    the two versions overlap and out-of-SSA fails.  For the second testcase
    of PR128013 at -O1, forwprop1 changes
    
      <bb 2>:
      a0_10 = BIT_FIELD_REF <v_9(D)(ab), 32, 0>;
      ...
      a3_13 = BIT_FIELD_REF <v_9(D)(ab), 32, 96>;
      bar ();
    
      <bb 3>:
      ...
      v_16(ab) = _2;
      _3 = {a0_10, a1_11, a2_12, a3_13};
      *out_17(D) = _3;
    
    into
    
      v_16(ab) = _2;
      _3 = v_9(D)(ab);
      *out_17(D) = _3;
    
    Do not use an SSA name that occurs in an abnormal PHI there.
    gimple_simplify has the same rule for the statements that it makes (see
    maybe_push_res_to_seq).  For the same reason, the fix for PR125502 stops
    optimize_vector_load from moving the definition of such an SSA name.
    
    The new test ICEs on trunk on aarch64 and x86_64 at -O1 and above.
    On aarch64 the patch changes the assembly of one file in about 18600
    files of gcc.dg, gcc.dg/torture, gcc.dg/tree-ssa, gcc.c-torture,
    gcc.dg/vect and g++.dg/vect, at -O3 and at -O2 -ftree-vectorize
    -fno-vect-cost-model: f2 of bb-slp-pr128013.c builds the vector of the
    lanes of v again instead of using v (6 more instructions).  At -O2
    -ftree-vectorize -fno-vect-cost-model, f1 also does this (8 more
    instructions).
    
    Bootstrapped and regression tested on aarch64-unknown-linux-gnu and
    x86_64-pc-linux-gnu.
    
    gcc/ChangeLog:
    
            PR tree-optimization/128013
            * tree-ssa-forwprop.cc (simplify_vector_constructor): Do not use
            an SSA name that occurs in an abnormal PHI.
    
    gcc/testsuite/ChangeLog:
    
            PR tree-optimization/128013
            * gcc.dg/torture/pr128013.c: New test.
    
    Signed-off-by: Kyrylo Tkachov <[email protected]>

Diff:
---
 gcc/testsuite/gcc.dg/torture/pr128013.c | 23 +++++++++++++++++++++++
 gcc/tree-ssa-forwprop.cc                |  3 +++
 2 files changed, 26 insertions(+)

diff --git a/gcc/testsuite/gcc.dg/torture/pr128013.c 
b/gcc/testsuite/gcc.dg/torture/pr128013.c
new file mode 100644
index 000000000000..402137aa912b
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr128013.c
@@ -0,0 +1,23 @@
+/* PR tree-optimization/128013 */
+/* { dg-do compile } */
+/* { dg-additional-options "-Wno-psabi" } */
+
+typedef int v4si __attribute__ ((vector_size (16)));
+extern int rt (void) __attribute__ ((returns_twice));
+extern void bar (void);
+extern void use (v4si);
+
+/* v occurs in abnormal PHIs.  The CONSTRUCTOR must not become a use of v
+   after its redefinition.  */
+
+void
+f (v4si *out, v4si v)
+{
+  int a0 = v[0], a1 = v[1], a2 = v[2], a3 = v[3];
+  bar ();
+  v = v * 3;
+  *out = (v4si) { a0, a1, a2, a3 };
+  bar ();
+  rt ();
+  use (v);
+}
diff --git a/gcc/tree-ssa-forwprop.cc b/gcc/tree-ssa-forwprop.cc
index ac858e66100c..44f92810d1c2 100644
--- a/gcc/tree-ssa-forwprop.cc
+++ b/gcc/tree-ssa-forwprop.cc
@@ -5338,6 +5338,9 @@ simplify_vector_constructor (gimple_stmt_iterator *gsi)
       op1 = get_bit_field_ref_def (elt->value, conv_code);
       if (op1
          && TREE_CODE ((ref = TREE_OPERAND (op1, 0))) == SSA_NAME
+         /* Do not extend the live range of an SSA name that occurs in an
+            abnormal PHI.  */
+         && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ref)
          && VECTOR_TYPE_P (TREE_TYPE (ref))
          && (tree_nop_conversion_p (TREE_TYPE (op1),
                                     TREE_TYPE (TREE_TYPE (ref)))

Reply via email to