public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/58116] New: missed-optimization: const temporaries could be promoted to static
@ 2013-08-10  0:02 luto at mit dot edu
  2013-11-09 22:46 ` [Bug c++/58116] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: luto at mit dot edu @ 2013-08-10  0:02 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 58116
           Summary: missed-optimization: const temporaries could be
                    promoted to static
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: luto at mit dot edu

This code:

struct S
{
  int a, b, c;
};

extern void callee(const S &s);

void test()
{
  const S s{1,2,3};
  callee(s);

  callee((const S){1,2,3});
}

would be shorter and faster if both S instances were promoted to statics.  This
would be correct because it's undefined behavior to modify a non-mutable member
of an object declared const in C++.  (I'm not sure about C.)


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

* [Bug c++/58116] missed-optimization: const temporaries could be promoted to static
  2013-08-10  0:02 [Bug c++/58116] New: missed-optimization: const temporaries could be promoted to static luto at mit dot edu
@ 2013-11-09 22:46 ` pinskia at gcc dot gnu.org
  2014-01-17 23:33 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2013-11-09 22:46 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I think there is a duplicated bug of this already.


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

* [Bug c++/58116] missed-optimization: const temporaries could be promoted to static
  2013-08-10  0:02 [Bug c++/58116] New: missed-optimization: const temporaries could be promoted to static luto at mit dot edu
  2013-11-09 22:46 ` [Bug c++/58116] " pinskia at gcc dot gnu.org
@ 2014-01-17 23:33 ` pinskia at gcc dot gnu.org
  2014-01-17 23:36 ` pinskia at gcc dot gnu.org
  2014-01-20 18:39 ` luto at mit dot edu
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2014-01-17 23:33 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2014-01-17
     Ever confirmed|0                           |1

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed.


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

* [Bug c++/58116] missed-optimization: const temporaries could be promoted to static
  2013-08-10  0:02 [Bug c++/58116] New: missed-optimization: const temporaries could be promoted to static luto at mit dot edu
  2013-11-09 22:46 ` [Bug c++/58116] " pinskia at gcc dot gnu.org
  2014-01-17 23:33 ` pinskia at gcc dot gnu.org
@ 2014-01-17 23:36 ` pinskia at gcc dot gnu.org
  2014-01-20 18:39 ` luto at mit dot edu
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2014-01-17 23:36 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Actually this is an invalid optimization.

Take:
struct S
{
  int a, b, c;
};

extern void callee(const S &s);

void test()
{
  const S s{1,2,3};
  callee(s);

  callee((const S){1,2,3});
}

void test1()
{
  callee((const S){1,2,3});
}

void callee(const S &s)
{
  static const S *a;
  if (!a)
    {
      a = &s;
      test();
    }
  if (a == &s)
    __builtin_abort();
  return;
}

>From http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59863#c1:
If the object address can escape and the function can be called 
recursively, this would violate the requirement for distinct objects to 
have distinct addresses.  (See discussion on comp.std.c, "uniqueness of 
automatic objects", Nov 2008; I'm not sure if there was a corresponding 
GCC bug report / fix.)


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

* [Bug c++/58116] missed-optimization: const temporaries could be promoted to static
  2013-08-10  0:02 [Bug c++/58116] New: missed-optimization: const temporaries could be promoted to static luto at mit dot edu
                   ` (2 preceding siblings ...)
  2014-01-17 23:36 ` pinskia at gcc dot gnu.org
@ 2014-01-20 18:39 ` luto at mit dot edu
  3 siblings, 0 replies; 5+ messages in thread
From: luto at mit dot edu @ 2014-01-20 18:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andy Lutomirski <luto at mit dot edu> ---
Sorry -- I forgot about the recursive / threaded case.

(I keep meaning to propose something like [[non_unique_address]] to enable
optimizations like this and things zero-byte struct members.)


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

end of thread, other threads:[~2014-01-20 18:39 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-08-10  0:02 [Bug c++/58116] New: missed-optimization: const temporaries could be promoted to static luto at mit dot edu
2013-11-09 22:46 ` [Bug c++/58116] " pinskia at gcc dot gnu.org
2014-01-17 23:33 ` pinskia at gcc dot gnu.org
2014-01-17 23:36 ` pinskia at gcc dot gnu.org
2014-01-20 18:39 ` luto at mit dot edu

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