public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/28134]  New: optimize redundant memset + assignment
@ 2006-06-22  3:30 raeburn at raeburn dot org
  2006-06-22  4:16 ` [Bug tree-optimization/28134] " raeburn at raeburn dot org
  0 siblings, 1 reply; 4+ messages in thread
From: raeburn at raeburn dot org @ 2006-06-22  3:30 UTC (permalink / raw)
  To: gcc-bugs

In a compiler built from svn revision 114878, a memset(,0,) followed by storing
0 into some of the just-cleared locations produces redundant stores.

#include <string.h>
struct blob { int a[3]; void *p; };
void foo (struct blob *bp) {
    int i;
    memset (bp, 0, 1024 * sizeof(*bp));
    /* Null pointer not required by ANSI to be all-bits-0, so: */
    for (i = 0; i < 1024; i++) bp[i].p = 0;
}

With "gcc -O9 -fomit-frame-pointer -march=pentiumpro -mtune=pentiumpro" the
assembly code produces a call to memset, then a loop storing 0 into the pointer
slots.  But on this platform, since a pointer has all bits clear, the loop is
redundant.  If I add "-minline-all-stringops", it doesn't help; the memset call
is replaced by a sequence using "rep stosl", and the following loop is still
there.

If I change the array size from 1024 to 1, then gcc expands the memset inline
(no loop), and figures out the redundancy.

Same issue with storing zero in bit fields after memset:

#include <string.h>
struct blob { unsigned char a:1, b:7; };
void foo (struct blob *bp) {
    int i;
    memset(bp, 0, 1024 * sizeof(*bp));
    for (i = 0; i < 1024; i++) bp[i].a = 0;
}

The memset is followed by a loop with "andb $-2,...".

A possible optimization I'm less would be allowed for odd cases: If I change
the second example to use "bp[i].a = 1", is the compiler allowed to optimize
this into memset(,1,)?  If so, add that to the wish list. :-)


-- 
           Summary: optimize redundant memset + assignment
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: raeburn at raeburn dot org
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


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


^ permalink raw reply	[flat|nested] 4+ messages in thread
[parent not found: <bug-28134-4@http.gcc.gnu.org/bugzilla/>]

end of thread, other threads:[~2022-01-28 23:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-06-22  3:30 [Bug tree-optimization/28134] New: optimize redundant memset + assignment raeburn at raeburn dot org
2006-06-22  4:16 ` [Bug tree-optimization/28134] " raeburn at raeburn dot org
     [not found] <bug-28134-4@http.gcc.gnu.org/bugzilla/>
2011-12-24  3:16 ` pinskia at gcc dot gnu.org
2022-01-28 23:44 ` 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).