Bug 9540 - C99 initializers generate lots of code
Summary: C99 initializers generate lots of code
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 3.2.1
: P3 enhancement
Target Milestone: 4.0.0
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2003-02-02 20:16 UTC by mariube+gcc+
Modified: 2004-05-13 11:26 UTC (History)
2 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-linux-pc-gnu
Build: i686-pc-linux-gnu
Known to work: tree-ssa
Known to fail:
Last reconfirmed: 2004-03-04 02:55:11


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description mariube+gcc+ 2003-02-02 20:16:00 UTC
When using C99 initializers, i.e.:

    struct bleh *blah = malloc( sizeof *blah );

    *blah = (struct bleh){ .field1 = value1, ... };

the code generated is far bigger than the traditional

    blah->field1 = value1; ...

Release:
3.2.1

Environment:
x86
Comment 1 Wolfgang Bangerth 2003-02-02 22:15:57 UTC
State-Changed-From-To: open->feedback
State-Changed-Why: Can you send a self-contained code example that shows this?
    
    Thanks
     Wolfgang
Comment 2 The gcc-bugs mailing list 2003-02-03 13:34:19 UTC
From: Marius Bernklev <gcc-bugs@gcc.gnu.org>
To: bangerth@dealii.org
Cc: gcc-bugs@gcc.gnu.org, gcc-prs@gcc.gnu.org, nobody@gcc.gnu.org,
   gcc-gnats@gcc.gnu.org
Subject: Re: c/9540: C99 initializers generate lots of code
Date: Mon, 03 Feb 2003 13:34:19 +0100

 bangerth@dealii.org writes:
 
 > Synopsis: C99 initializers generate lots of code
 >
 > State-Changed-From-To: open->feedback
 > State-Changed-By: bangerth
 > State-Changed-When: Sun Feb  2 22:15:57 2003
 > State-Changed-Why:
 >     Can you send a self-contained code example that shows this?
 >     
 >     Thanks
 >      Wolfgang
 >
 > http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=9540
 >
 
 Certainly. :)
 
 
 Here goes:
 
 
 // Start of vector.c
 #include <stdlib.h>  // well, I need size_t
 
 // imaginary garbage collector allocator
 extern void *gc_malloc(size_t);
 
 struct string {
   size_t size;
   size_t length;
   char *restrict string;
   int hash;
 };
 
 struct string *new_vector1( const size_t length ) {
   struct string *const restrict result = gc_malloc( sizeof *result );
 
   *result = (struct string) { .size = sizeof *result,
                               .length = length,
                               .string = gc_malloc( length ),
                               .hash = -1 };
 
   return result;
 }
 
 struct string *new_vector2( const size_t length ) {
   struct string *const restrict result = gc_malloc( sizeof *result );
 
   result->size = sizeof *result;
   result->length = length;
   result->string = gc_malloc( length );
   result->hash = -1;
   
   return result;
 }
 ---------------------------
 
 with gcc 3.2.1 on x86, with the command line
 
 gcc -std=c99 -S vector.c,
 
 the code generated for new_vector1 is somewhat larger than for
 new_vector2.
 
 
 
 Marius
 -- 
 <URL: http://home.ifi.uio.no/~mariube/ >
 
 Life sucks.  Death swallows.
Comment 3 Wolfgang Bangerth 2003-02-03 21:02:25 UTC
State-Changed-From-To: feedback->open
State-Changed-Why: Feedback received. The sizes of the two functions are
    indeed different, but I have too little knowledge of 
    the optimizers to analyze whether they differ in any
    measurable quantity (run-time, clock-ticks), so leave
    this to others.
    
    W.
Comment 4 Christian Ehrhardt 2003-05-13 15:17:39 UTC
From: "Christian Ehrhardt" <ehrhardt@mathematik.uni-ulm.de>
To: gcc-prs@gcc.gnu.org, mariube+gcc+@ifi.uio.no, gcc-bugs@gcc.gnu.org,
  gcc-gnats@gcc.gnu.org, nobody@gcc.gnu.org
Cc:  
Subject: Re: optimization/9540: C99 initializers generate lots of code
Date: Tue, 13 May 2003 15:17:39 +0200

 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=9540
 
 The code size of the two functions differs because you do two
 fundamentally different things:
 
 * new_vector1 creates a compound literal (this is a separate object not
   just an initializer!) of type string. The contents of this object
   are then assigned to *result.
 * new_vector2 initializes *result directly without the construction of
   an intermediate compound literal.
 
 The additional instructions that you see are the result of the copying
 which is in general necessary because we can't know if gc_malloc might
 rely on the memory in *result or might modify previously initialized
 elements of the compound literal. The latter might be impossible
 due to aliasing rules but I'm not entirly sure about this. The former
 is definitely possible, e.g. if the first call to gc_malloc also stored
 the return value in some static or global variable.
 
 Given the fact that gcc does optimize the mem to mem copy away if
 the compound literal is declared const and gc_malloc is called outside
 of the initializer I guess that there aren't enough aliasing restrictions
 to allow the optimization otherwise.
 
    regards  Christian
 
 -- 
 THAT'S ALL FOLKS!
Comment 5 Dara Hazeghi 2003-06-04 17:51:02 UTC
Confirmed with gcc 3.3 branch and mainline (20030601) on powerpc-darwin, althought it's only 4 
instructions longer on that platform... Christian, your last post indicated you thought this was a 
bug (missed opportunity) with alias optimization, correct? 
Thanks,

Dara
Comment 6 Andrew Pinski 2003-07-21 02:49:16 UTC
This bug has nothing to do with aliasing but it has to do with GCC creating a temp 
variable to store the C99 initializer and then copying that into the global variable.
Comment 7 Andrew Pinski 2003-11-16 18:16:00 UTC
Is another case where GCC puts structs (objects) on the stack to early, setting target to the tree-
ssa.
Comment 8 Andrew Pinski 2003-12-01 04:24:24 UTC
Suspending as it is already fixed on the tre-ssa.
Comment 9 Andrew Pinski 2003-12-17 00:30:48 UTC
Fixed on the tree-ssa so closing.
Comment 10 Andrew Pinski 2003-12-17 17:05:50 UTC
Reopening the bugs that are fixed on the tree-ssa (but not reported against the branch).
Comment 11 Andrew Pinski 2003-12-17 17:10:20 UTC
Suspending based on this is fixed on the tree-ssa.
Comment 12 Andrew Pinski 2004-05-13 11:26:26 UTC
Fixed for 3.5.0 by the merge of the tree-ssa.