public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/97307] New: Optimization for pure vs. const function
@ 2020-10-06 16:46 benjamin.meier70 at gmail dot com
  2020-10-07  6:49 ` [Bug tree-optimization/97307] " rguenth at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: benjamin.meier70 at gmail dot com @ 2020-10-06 16:46 UTC (permalink / raw)
  To: gcc-bugs

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.

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

end of thread, other threads:[~2020-10-07 17:27 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-06 16:46 [Bug c/97307] New: Optimization for pure vs. const function benjamin.meier70 at gmail dot com
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

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