https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113727

--- Comment #23 from Richard Biener <rguenth at gcc dot gnu.org> ---
Note with the 2nd patch it's still broken when the BIT_FIELD_REFs in the IL are
not byte aligned.

Both patches passed bootstrap & regtest, there is unknown effect on
optimization of __imag / __real.

Looking at such an example we see

  _1 = x.f;
  s$f = _1;
  REALPART_EXPR <s$f> = 1.0e+0;
  _7 = s$f;
  s.f = _7;
  bar (s);

so while we don't "handle" REALPART_EXPR we assume its base is fully
replaced.  For the BIT_FIELD_REF case we cannot know any such thing.

Ah, and the fix is a bit wrong I guess.  It should be

diff --git a/gcc/tree-sra.cc b/gcc/tree-sra.cc
index f8e71ec48b9..f8176de817f 100644
--- a/gcc/tree-sra.cc
+++ b/gcc/tree-sra.cc
@@ -2358,6 +2358,12 @@ sort_and_splice_var_accesses (tree var)
          j++;
        }

+      /* When there is a partial LHS involved we have no way to see what it
+        accesses, so if it's not the only access to the subregion
+        we have to fail.  */
+      if (grp_partial_lhs && j != i + 1)
+       return NULL;
+
       i = j;

       access->group_representative = access;

and that indeed does disable SRA for

struct S { _Complex float f; } x;

void bar (struct S);

float foo ()
{
  struct S s;
  s.f = x.f;
  __real s.f = 1.f;
  bar (s);
  float x = __real s.f;
  return x;
}

so maybe grp_partial_lhs isn't supposed to handle the BIT_FIELD_REF case
in question but only handles the case where there's a sub-access in its
base?

I suppose it simply wasn't designed to handle mixed accesses.

Maybe the problem is that SRA fails to see that for a ->grp_partial_lhs
access we _have_ to do a replacement.  For the testcase in this PR the
access is 'as' itself and we create a replacement for as.au and as.f3
but then we have overlapping replacements (with the covering 'as').

The following fixes that, but maybe not in the correct place:

diff --git a/gcc/tree-sra.cc b/gcc/tree-sra.cc
index f8e71ec48b9..dbfae5e7fdd 100644
--- a/gcc/tree-sra.cc
+++ b/gcc/tree-sra.cc
@@ -2735,7 +2735,8 @@ analyze_access_subtree (struct access *root, struct
access *parent,
     {
       hole |= covered_to < child->offset;
       sth_created |= analyze_access_subtree (child, root,
-                                            allow_replacements && !scalar,
+                                            allow_replacements && !scalar
+                                            && !root->grp_partial_lhs,
                                             totally);

       root->grp_unscalarized_data |= child->grp_unscalarized_data;

Reply via email to