public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "benjamin.meier70 at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/97307] New: Optimization for pure vs. const function
Date: Tue, 06 Oct 2020 16:46:00 +0000	[thread overview]
Message-ID: <bug-97307-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97307

            Bug ID: 97307
           Summary: Optimization for pure vs. const function
           Product: gcc
           Version: 10.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: benjamin.meier70 at gmail dot com
  Target Milestone: ---

This bug report is based on this stackoverflow post:
https://stackoverflow.com/q/64034889/916672

The source code I use in this post, is also available here:
https://gcc.godbolt.org/z/dGvxnv

Given this C source code:

> int pure_f(int a, int b) __attribute__((pure));
> 
> int const_f(int a, int b) __attribute__((const));
> 
> int my_f(int a, int b) {
>     int x = pure_f(a, b);
>     if (a > 0) {
>         return x;
>     }
>     return a;
> }

If this is compiled with gcc with -O3, I would expect that the evaluation of
pure_f(a, b) is moved into the if. But it is not done:

> my_f(int, int):
>         push    r12
>         mov     r12d, edi
>         call    pure_f(int, int)
>         test    r12d, r12d
>         cmovg   r12d, eax
>         mov     eax, r12d
>         pop     r12
>         ret

On the other side, if const_f is called instead of pure_f, it is moved into the
if:


> my_f(int, int):
>         test    edi, edi
>         jg      .L4
>         mov     eax, edi
>         ret
> .L4:
>         jmp     const_f(int, int)


Why isn't this optimization applied for a pure function? From my understanding,
this should also be possible and it seems to be beneficial.

             reply	other threads:[~2020-10-06 16:46 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-06 16:46 benjamin.meier70 at gmail dot com [this message]
2020-10-07  6:49 ` [Bug tree-optimization/97307] " rguenth at gcc dot gnu.org
2020-10-07  8:44 ` rguenth at gcc dot gnu.org
2020-10-07 14:51 ` rguenth at gcc dot gnu.org
2020-10-07 14:55 ` cvs-commit at gcc dot gnu.org
2020-10-07 14:56 ` rguenth at gcc dot gnu.org
2020-10-07 15:04 ` benjamin.meier70 at gmail dot com
2020-10-07 15:30 ` jakub at gcc dot gnu.org
2020-10-07 17:27 ` rguenther at suse dot de

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=bug-97307-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /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).