public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Jose E. Marchesi" <jose.marchesi@oracle.com>
To: Jakub Jelinek <jakub@redhat.com>
Cc: gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] expr.cc: avoid unexpected side effects in expand_expr_divmod optimization
Date: Thu, 08 Dec 2022 14:02:36 +0100	[thread overview]
Message-ID: <87y1rixd3n.fsf@oracle.com> (raw)
In-Reply-To: <Y5HWmBEVrJVAm+xQ@tucnak> (Jakub Jelinek's message of "Thu, 8 Dec 2022 13:20:40 +0100")


Hi Jakub.

> On Thu, Dec 08, 2022 at 11:59:44AM +0100, Jose E. Marchesi via Gcc-patches wrote:
>> gcc/ChangeLog
>> 
>> 	* expr.cc (expand_expr_divmod): Avoid side-effects of trying
>> 	sequences involving funcalls in optimization.
>
> That looks wrong.
> The globals for mentioned calls just shouldn't be emitted during expansion,
> especially if it is bigger annoyance than just having some extra symbols
> in the symbol table.
> expand_expr_divmod is definitely not the only place where something is
> expanded and later not used, lots of other places in the expander do that,
> and more importantly, there are over 80 optimization passes after expansion,
> many of them can remove code determined to be dead, and while lots of dead
> code is removed in GIMPLE optimizations already, definitely not all.
> So, rather than add hacks for this in a single spot, much better is to emit
> the globals only for stuff that is actually needed (so during final or
> immediately before it).

Yeah I see the point.

The culprit of the leadked .global seems to be a call to
assemble_external_libcall in emit_library_call_value_1:

expand_expr_divmod
  expand_divmod -> This will result in libcall
   sign_expand_divmod
     emit_library_call_value
       emit_library_call_value_1
         ...
         /* If this machine requires an external definition for library
            functions, write one out.  */
         assemble_external_libcall (fun);
         ...

The documented purpose of assemble_external_libcall is, as stated in
output.h, to "Assemble a string constant".

So, it seems to me that emit_library_call_value should not assemble
anything, since it is used by expand functions whose expansions may be
eventually discarded.

However, simply removing that call to assemble_external_libcall makes
.global declarations to not be emitted even when the funcall is actually
emitted in final:

For:

  int foo(unsigned int len)
  {
    return ((long)len) * 234 / 5;
  }

we get:

    .file   "foo.c"
    .text
    <------------- NO .global __divdi3
    .align  3
    .global foo
    .type   foo, @function
  foo:
    mov32   %r1,%r1
    mov     %r2,5
    mul     %r1,234
    call    __divdi3
    exit
  .size   foo, .-foo
  .ident  "GCC: (GNU) 13.0.0 20221207 (experimental)"

Note that BPF lacks signed division instructions.

So, I guess the right fix would be to call assemble_external_libcall
during final?  The `.global FOO' directive would be generated
immediately before the call sequence, but I guess that would be ok.

WDYT?

  reply	other threads:[~2022-12-08 12:58 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-08 10:59 Jose E. Marchesi
2022-12-08 12:20 ` Jakub Jelinek
2022-12-08 13:02   ` Jose E. Marchesi [this message]
2022-12-08 13:19     ` Jakub Jelinek
2022-12-08 22:40       ` Jose E. Marchesi
2023-01-04  8:58         ` Jose E. Marchesi
2023-01-09  8:05           ` Richard Biener
2023-01-09  9:57             ` Jakub Jelinek
2023-01-09 13:04               ` Richard Biener
2023-01-09 13:25                 ` Jakub Jelinek
2023-01-09 14:01               ` Jeff Law
2022-12-08 13:42 ` Richard Biener
2022-12-08 16:03   ` Jose E. Marchesi
2023-01-30 18:45 ` Andrew Pinski
2023-01-30 18:55   ` Jose E. Marchesi

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=87y1rixd3n.fsf@oracle.com \
    --to=jose.marchesi@oracle.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.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).