public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/49872] New: Missed optimization: Could coalesce neighboring memsets
@ 2011-07-27 16:51 sgunderson at bigfoot dot com
  2011-07-28 10:01 ` [Bug tree-optimization/49872] " rguenth at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: sgunderson at bigfoot dot com @ 2011-07-27 16:51 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49872

           Summary: Missed optimization: Could coalesce neighboring
                    memsets
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: sgunderson@bigfoot.com


Given the following code:

#include <string.h>

struct S {
    int f[1024];
    int g[1024];
};

void func(struct S* s)
{
    memset(s->f, 0, sizeof(s->f));
    memset(s->g, 0, sizeof(s->g));
}

GCC currently generates two memsets. The code with -O2 is a bit hard to read,
so I'm just pasting the -Os assembly for clarity:

00000000 <func>:
   0:    55                       push   %ebp
   1:    31 c0                    xor    %eax,%eax
   3:    89 e5                    mov    %esp,%ebp
   5:    b9 00 04 00 00           mov    $0x400,%ecx
   a:    57                       push   %edi
   b:    8b 7d 08                 mov    0x8(%ebp),%edi
   e:    f3 ab                    rep stos %eax,%es:(%edi)
  10:    8b 55 08                 mov    0x8(%ebp),%edx
  13:    66 b9 00 04              mov    $0x400,%cx
  17:    81 c2 00 10 00 00        add    $0x1000,%edx
  1d:    89 d7                    mov    %edx,%edi
  1f:    f3 ab                    rep stos %eax,%es:(%edi)
  21:    5f                       pop    %edi
  22:    5d                       pop    %ebp
  23:    c3                       ret    

Ideally GCC should also be able to coalesce this together with memsets not
written as memset, e.g. s->g[0] = 0;.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug tree-optimization/49872] Missed optimization: Could coalesce neighboring memsets
  2011-07-27 16:51 [Bug tree-optimization/49872] New: Missed optimization: Could coalesce neighboring memsets sgunderson at bigfoot dot com
@ 2011-07-28 10:01 ` rguenth at gcc dot gnu.org
  2011-07-28 10:10 ` sgunderson at bigfoot dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2011-07-28 10:01 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49872

Richard Guenther <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2011.07.28 10:00:34
     Ever Confirmed|0                           |1

--- Comment #1 from Richard Guenther <rguenth at gcc dot gnu.org> 2011-07-28 10:00:34 UTC ---
Confirmed.

It should be hard to implement this, but did you see this in real-world code
(to asses the importance of this optimization)?

Other cases would include (partially) overlapping memsets (or related
functions such as memory copying routines and the respective string variants).

We already optimize some cases (that appeared in GCC IIRC) in
tree-ssa-forwprop.c,
namely

/* *GSI_P is a GIMPLE_CALL to a builtin function.
   Optimize
   memcpy (p, "abcd", 4);
   memset (p + 4, ' ', 3);
   into
   memcpy (p, "abcd   ", 7);
   call if the latter can be stored by pieces during expansion.  */


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug tree-optimization/49872] Missed optimization: Could coalesce neighboring memsets
  2011-07-27 16:51 [Bug tree-optimization/49872] New: Missed optimization: Could coalesce neighboring memsets sgunderson at bigfoot dot com
  2011-07-28 10:01 ` [Bug tree-optimization/49872] " rguenth at gcc dot gnu.org
@ 2011-07-28 10:10 ` sgunderson at bigfoot dot com
  2021-08-21 23:57 ` pinskia at gcc dot gnu.org
  2024-04-21 19:26 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: sgunderson at bigfoot dot com @ 2011-07-28 10:10 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49872

--- Comment #2 from sgunderson at bigfoot dot com 2011-07-28 10:09:51 UTC ---
I'm not sure if I've seen exactly this construction in real-world code, but
I've certainly seen examples of the hybrid I sketched out (looking at one was
what motivated me to file the bug), ie. something like:

struct S {
    int f[1024];
    int g;
};

void func(struct S* s)
{
    memset(s->f, 0, sizeof(s->f));
    s->g = 0;
}

which I would argue should be rewritten to

void func(struct S* s)
{
    memset(s->f, 0, sizeof(s->f) + sizeof(s->g));
}

I'd argue that programmers should not be doing this kind of optimization
themselves, since it's very prone to break when changing the structure,
especially as alignment etc. comes into play.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug tree-optimization/49872] Missed optimization: Could coalesce neighboring memsets
  2011-07-27 16:51 [Bug tree-optimization/49872] New: Missed optimization: Could coalesce neighboring memsets sgunderson at bigfoot dot com
  2011-07-28 10:01 ` [Bug tree-optimization/49872] " rguenth at gcc dot gnu.org
  2011-07-28 10:10 ` sgunderson at bigfoot dot com
@ 2021-08-21 23:57 ` pinskia at gcc dot gnu.org
  2024-04-21 19:26 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-21 23:57 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |msebor at gcc dot gnu.org

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 86017 has been marked as a duplicate of this bug. ***

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug tree-optimization/49872] Missed optimization: Could coalesce neighboring memsets
  2011-07-27 16:51 [Bug tree-optimization/49872] New: Missed optimization: Could coalesce neighboring memsets sgunderson at bigfoot dot com
                   ` (2 preceding siblings ...)
  2021-08-21 23:57 ` pinskia at gcc dot gnu.org
@ 2024-04-21 19:26 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-21 19:26 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |xxs_chy at outlook dot com

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 114797 has been marked as a duplicate of this bug. ***

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-04-21 19:26 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-27 16:51 [Bug tree-optimization/49872] New: Missed optimization: Could coalesce neighboring memsets sgunderson at bigfoot dot com
2011-07-28 10:01 ` [Bug tree-optimization/49872] " rguenth at gcc dot gnu.org
2011-07-28 10:10 ` sgunderson at bigfoot dot com
2021-08-21 23:57 ` pinskia at gcc dot gnu.org
2024-04-21 19:26 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).