public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jeff Law <law@redhat.com>
To: Mikael Tillenius <mti-1@tillenius.com>, gcc-help@gcc.gnu.org
Subject: Re: Non-optimal code generated for H8
Date: Tue, 29 Oct 2019 20:19:00 -0000	[thread overview]
Message-ID: <13ffbba6-2b7b-f3b3-9b7a-20923b8271cc@redhat.com> (raw)
In-Reply-To: <5b46f0b2-6ded-784c-00ff-fe592819b397@tillenius.com>

On 10/29/19 2:03 PM, Mikael Tillenius wrote:
> 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.
As we leave gimple the code looks like:

  MEM <struct s[2]> [(struct s *)&x][n_1(D)].a = 1;
  MEM <struct s[2]> [(struct s *)&x][n_1(D)].b = 1;

One might argue that DOM or FRE should have created a common
subexpression for the address arithmetic here.  Even so it's not bad.

CSE doesn't do its job though.  THere's clearly a REG_EQUAL note which
should have allowed it to at least cleanup the redundant multiplication
for the address calculation.

I recommend filing a bug report.

Note that the H8 port is on the list of ports that are be deprecated in
gcc-10 unless someone steps forward to take care of some significant
maintenance tasks.   Deprecation in gcc-10 would mean the port would be
removed in gcc-11 unless someone steps up to take care of those
maintenance tasks.

Jeff

  reply	other threads:[~2019-10-29 20:19 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-29 20:03 Mikael Tillenius
2019-10-29 20:19 ` Jeff Law [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=13ffbba6-2b7b-f3b3-9b7a-20923b8271cc@redhat.com \
    --to=law@redhat.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=mti-1@tillenius.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).