public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/45115]  New: attribute((pure)) does not work when returning structs
@ 2010-07-28 19:45 marco at technoboredom dot net
  2010-07-28 19:48 ` [Bug tree-optimization/45115] pure functions returning structs are not optimized pinskia at gcc dot gnu dot org
  2010-07-28 20:05 ` rguenth at gcc dot gnu dot org
  0 siblings, 2 replies; 6+ messages in thread
From: marco at technoboredom dot net @ 2010-07-28 19:45 UTC (permalink / raw)
  To: gcc-bugs

compiling this with 'gcc -O3 -S x.c'

-------- x.c -----------
#define PURE __attribute__((pure))

struct res {int u; };
extern struct res calc_1() PURE; 
extern int        calc_2() PURE;

int fun_1() 
{
        return calc_1().u+calc_1().u;
}
int fun_2() 
{
        return calc_2()+calc_2();
}
--------------------

yields code which calls calc_2() *once* in fun_2() but calls calc_1() *twice*
in fun_1(). Obviously, fun_1 misses an optimization.

Tested on gcc 4.4.3 (Ubuntu 10.04), gcc 4.5.0/4.4.4/4.3.5 (MacPorts)


-- 
           Summary: attribute((pure)) does not work when returning structs
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: marco at technoboredom dot net
  GCC host triplet: x86_64-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45115


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

* [Bug tree-optimization/45115] pure functions returning structs are not optimized.
  2010-07-28 19:45 [Bug c/45115] New: attribute((pure)) does not work when returning structs marco at technoboredom dot net
@ 2010-07-28 19:48 ` pinskia at gcc dot gnu dot org
  2010-07-28 20:05 ` rguenth at gcc dot gnu dot org
  1 sibling, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2010-07-28 19:48 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2010-07-28 19:48 -------
Confirmed.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
             Status|UNCONFIRMED                 |NEW
          Component|c                           |tree-optimization
     Ever Confirmed|0                           |1
   GCC host triplet|x86_64-linux-gnu            |
           Keywords|                            |missed-optimization
   Last reconfirmed|0000-00-00 00:00:00         |2010-07-28 19:48:03
               date|                            |
            Summary|attribute((pure)) does not  |pure functions returning
                   |work when returning structs |structs are not optimized.


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45115


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

* [Bug tree-optimization/45115] pure functions returning structs are not optimized.
  2010-07-28 19:45 [Bug c/45115] New: attribute((pure)) does not work when returning structs marco at technoboredom dot net
  2010-07-28 19:48 ` [Bug tree-optimization/45115] pure functions returning structs are not optimized pinskia at gcc dot gnu dot org
@ 2010-07-28 20:05 ` rguenth at gcc dot gnu dot org
  1 sibling, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2010-07-28 20:05 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from rguenth at gcc dot gnu dot org  2010-07-28 20:05 -------
We can only value-number things which define an SSA name.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |matz at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45115


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

* [Bug tree-optimization/45115] pure functions returning structs are not optimized.
       [not found] <bug-45115-4@http.gcc.gnu.org/bugzilla/>
  2023-02-02  9:43 ` pinskia at gcc dot gnu.org
  2023-02-02 10:15 ` redi at gcc dot gnu.org
@ 2024-04-09  7:06 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-09  7:06 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Dup of bug 59739; even though that is a newer bug it has some more analysis and
even been assigned (will add the C++20 testcase there too).

*** This bug has been marked as a duplicate of bug 59739 ***

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

* [Bug tree-optimization/45115] pure functions returning structs are not optimized.
       [not found] <bug-45115-4@http.gcc.gnu.org/bugzilla/>
  2023-02-02  9:43 ` pinskia at gcc dot gnu.org
@ 2023-02-02 10:15 ` redi at gcc dot gnu.org
  2024-04-09  7:06 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2023-02-02 10:15 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This affects C++20 three-way comparisons, which return trivial structs wrapping
an integer.

From PR 108635:

    #include <compare>

    struct S
    {
        std::weak_ordering operator<=>(const S&) const __attribute__((const));
    };

    int compare3way(S& a, S& b)
    {
        return (a < b) ? -1 : (a > b) ? 1 : 0;
    }

I expect operator<=> to be called once, but it is called twice.  This can be a
major missed optimization if operator<=> is expensive.  It happens regardless
of:

 1. Using attribute((const)) or attribute((pure)).
 2. Making operator<=> a free function or a member.
 3. Comparing (a > b) or (a < b) in the second ternary expression.  This is
especially strange, because it's really calling the same pure function twice,
and that's optimized correctly when the function being called is operator<
instead of operator<=>.

Clang optimizes it as expected.

Demo: https://godbolt.org/z/jP51E6xaz

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

* [Bug tree-optimization/45115] pure functions returning structs are not optimized.
       [not found] <bug-45115-4@http.gcc.gnu.org/bugzilla/>
@ 2023-02-02  9:43 ` pinskia at gcc dot gnu.org
  2023-02-02 10:15 ` redi at gcc dot gnu.org
  2024-04-09  7:06 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-02-02  9:43 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jzwinck at gmail dot com

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 108635 has been marked as a duplicate of this bug. ***

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

end of thread, other threads:[~2024-04-09  7:06 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-07-28 19:45 [Bug c/45115] New: attribute((pure)) does not work when returning structs marco at technoboredom dot net
2010-07-28 19:48 ` [Bug tree-optimization/45115] pure functions returning structs are not optimized pinskia at gcc dot gnu dot org
2010-07-28 20:05 ` rguenth at gcc dot gnu dot org
     [not found] <bug-45115-4@http.gcc.gnu.org/bugzilla/>
2023-02-02  9:43 ` pinskia at gcc dot gnu.org
2023-02-02 10:15 ` redi at gcc dot gnu.org
2024-04-09  7:06 ` pinskia at gcc dot gnu.org

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