Bug 107263 - Memcpy not elided when initializing struct
Summary: Memcpy not elided when initializing struct
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 13.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks: 121364
  Show dependency tree / graph
 
Reported: 2022-10-14 13:13 UTC by Jeff Muizelaar
Modified: 2025-10-16 04:53 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2022-10-17 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jeff Muizelaar 2022-10-14 13:13:28 UTC
With the following code

struct Foo {
    Foo* next;
    char arr[580];
};

void ctx_push(Foo* f) {
    Foo tmp = { f->next };
    *f = tmp;
}

Clang is able to generate code that just memsets `arr`. GCC instead initializes the entire struct on the stack and then copies it into `f`.

https://gcc.godbolt.org/z/Yzcbs4G71
Comment 1 Richard Biener 2022-10-17 07:43:39 UTC
Confirmed.  The frontend leaves us with

  <<cleanup_point   struct Foo tmp = {};>>;
  <<cleanup_point <<< Unknown tree: expr_stmt
    tmp.next = f->next >>>>>;
  <<cleanup_point <<< Unknown tree: expr_stmt
    (void) (*NON_LVALUE_EXPR <f> = *(const struct Foo &) &tmp) >>>>>;

and ESRA sees

  <bb 2> :
  tmp = {};
  _1 = f_4(D)->next;
  tmp.next = _1;
  *f_4(D) = tmp;
  tmp ={v} {CLOBBER(eol)};
  return;

ESRA somewhat senselessly does

  <bb 2> :
  tmp = {};
  tmp$next_8 = 0B;
  _1 = f_4(D)->next;
  tmp$next_9 = _1;
  tmp.next = tmp$next_9;
  *f_4(D) = tmp;
  tmp ={v} {CLOBBER(eol)};
  return;

it doesn't scalarize the array because that's too large.  I would guess
that Clang doesn't split the initializer and thus its aggregate copy
propagation somehow manages to elide 'tmp'.  We don't have a good
place to peform the desired optimization, certainly the split
initialization of 'tmp' complicates things.

In principle it would be SRAs job since I think it does most of the
necessary analysis, it just lacks knowledge on how to re-materialize
*f_4(D) efficiently at the point of the aggregate assignment?
It has

Candidate (2384): tmp
Too big to totally scalarize: tmp (UID: 2384)
Created a replacement for tmp offset: 0, size: 64: tmp$nextD.2425

Access trees for tmp (UID: 2384):
access { base = (2384)'tmp', offset = 0, size = 4736, expr = tmp, type = struct Foo, reverse = 0, grp_read = 1, grp_write = 1, grp_assignment_read = 1, grp_assignment_write = 1, grp_scalar_read = 0, grp_scalar_write = 0, grp_total_scalarization = 0, grp_hint = 0, grp_covered = 0, grp_unscalarizable_region = 0, grp_unscalarized_data = 1, grp_same_access_path = 1, grp_partial_lhs = 0, grp_to_be_replaced = 0, grp_to_be_debug_replaced = 0}
* access { base = (2384)'tmp', offset = 0, size = 64, expr = tmp.next, type = struct Foo *, reverse = 0, grp_read = 1, grp_write = 1, grp_assignment_read = 1, grp_assignment_write = 1, grp_scalar_read = 0, grp_scalar_write = 1, grp_total_scalarization = 0, grp_hint = 0, grp_covered = 1, grp_unscalarizable_region = 0, grp_unscalarized_data = 0, grp_same_access_path = 1, grp_partial_lhs = 0, grp_to_be_replaced = 1, grp_to_be_debug_replaced = 0}

but it fails to record that for the size 4736 write there's a clear performed
that's cheaply to re-materialize (and no variables need to be created).  SRA
could probably track writes from only constants that way, avoiding to create
scalar replacements.
Comment 2 Jeff Muizelaar 2022-10-17 13:28:35 UTC
Even for small arrays clang does a noticeably better job:
https://gcc.godbolt.org/z/4d3TjGazY
Comment 3 AK 2024-03-19 17:27:36 UTC
Seems like a duplicate of #59863 ?
Comment 4 Drea Pinski 2025-02-21 06:19:30 UTC
(In reply to AK from comment #3)
> Seems like a duplicate of #59863 ?

No different issue . There we have an array which is all the way constant but here we have a non-constant part.
Comment 5 Drea Pinski 2025-06-08 00:19:38 UTC
  tmp = {};
  _1 = f_4(D)->next;
  tmp.next = _1;
  *f_4(D) = tmp;


I thought dse would decrease the zeroing store but it didn't.