public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Non-optimal code generated for H8
@ 2019-10-29 20:03 Mikael Tillenius
  2019-10-29 20:19 ` Jeff Law
  0 siblings, 1 reply; 20+ messages in thread
From: Mikael Tillenius @ 2019-10-29 20:03 UTC (permalink / raw)
  To: gcc-help

Hi

I am using a cross compiler for Renesas H8S. In a few places it 
generates really bad code. Given the following program:

struct s {
     char a, b;
     char c[11];
} x[2];

void test(int n)
{
     struct s *sp = &x[n];

     sp->a = 1;
     sp->b = 1;
}

I would expect that the pointer "sp" is calculated once and reused to 
access the fields "a" and "b". But instead the pointer is recalculated 
for each access. This generates a lot of extra code, including calls to 
__mulhi3. I have tested with gcc 8.2 and 9.2 and with different 
optimization levels (-O1, -O2, -Os) all with the same result. With -O0 
"sp" is only calculated once and kept as a variable on the stack but the 
rest of the code is not as good as it could be. The best work around 
seems to be to declare "sp" as volatile: "struct s *volatile sp = 
&x[n];". Then "sp" is only calculated once and kept on the stack and the 
surrounding code can be optimized.

So my question is: where should I start looking for a fix to this? The 
other targets I tried (ARM, x86, x86_64) behave as expected and 
calculates the pointer once and keeps it in a register.

/Mikael

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

end of thread, other threads:[~2019-11-03 18:17 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-29 20:03 Non-optimal code generated for H8 Mikael Tillenius
2019-10-29 20:19 ` Jeff Law
2019-10-30  0:34   ` Segher Boessenkool
2019-10-30  9:07     ` David Brown
2019-10-30 16:32       ` Mikael Tillenius
2019-10-30 19:53       ` Mikael Tillenius
2019-10-30 19:59         ` Jeff Law
2019-10-31  1:01           ` Segher Boessenkool
2019-11-01 13:16           ` Mikael Tillenius
2019-11-01 14:38             ` Jeff Law
2019-10-30 22:49       ` Segher Boessenkool
2019-10-31  8:10         ` David Brown
2019-10-31 16:24           ` Richard Earnshaw (lists)
2019-10-31 18:21             ` David Brown
2019-10-31 17:30           ` Segher Boessenkool
2019-10-31 17:47             ` Richard Earnshaw (lists)
2019-11-03 12:24         ` Oleg Endo
2019-11-03 18:17           ` Segher Boessenkool
2019-10-30 16:12   ` Mikael Tillenius
2019-11-01 13:22     ` Mikael Tillenius

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).