This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Why so bad optimized code ?


----- Original Message -----
From: "Jamie Lokier" <egcs@tantalophile.demon.co.uk>
Sent: Saturday, March 24, 2001 11:42 PM

> Although a general "char *" can alias anything (as a special case),
> dereferencing a struct field does not have the same property.  E.g. in
> this case the char struct fields _can_ alias other targets of "char *"
> pointers, for the same reason that int struct fields can alias other
> targets of "int *" pointers and in some cases int fields of other struct
> types.  However the char struct fields cannot alias e.g. other int
> types.
>
> So the generated code could be better.

  Looking at the source:

--------------------
struct time_struct
{
  char day;
  char hour;
  char min;
  char sec;
};

static struct time_struct *time;

int main(void)
{
  time->day = 5;
  time->hour= 9;
  time->min = 10;
  time->sec =10;
}

--------------------

one interesting thing is that since the dereference of time occurs in main,
and since the pointer 'time' is of static storage scope, and hence known to
be initialised to NULL when main starts up, the compiler can (at least
potentially) *know* for absolute certain that this is a dereference of NULL.
Hence the program is, strictly speaking, invoking undefined behaviour.  Of
course, that is not necessarily the reason why it generates bad code.

  It would be interesting to see what the compiler generates if you changed
the definition of 'time' to

    static struct time_struct * const time

  Would it then assume that the value of 'time' does not change and
therefore not need to reload it each time?

     DaveK



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]