public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "glisse at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug ipa/59948] New: Optimize std::function
Date: Sun, 26 Jan 2014 11:31:00 -0000	[thread overview]
Message-ID: <bug-59948-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 59948
           Summary: Optimize std::function
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: ipa
          Assignee: unassigned at gcc dot gnu.org
          Reporter: glisse at gcc dot gnu.org

#include <functional>

inline int f(int i){return i-1;}
int m(){
  std::function<int(int)> h=f;
  return h(1);
}

clang manages to optimize this to just:
int m(){return 0;}

However, g++ is stuck with a much longer code (comments inline):

int m() ()
{
  void * D.29797;
  struct function h;
  bool (*<T5503>) (union _Any_data &, const union _Any_data &,
_Manager_operation) _8;
  bool (*<T5503>) (union _Any_data &, const union _Any_data &,
_Manager_operation) _9;
  int (*<T590e>) (const union _Any_data &, int) _24;
  int _26;

  <bb 2>:
  MEM[(struct _Function_base *)&h]._M_manager = 0B;
  if (f != 0B)
// Shouldn't we know that f!=0? It is defined just above.
    goto <bb 3>;
  else
    goto <bb 4>;

  <bb 3>:
  MEM[(int (*<T5912>) (int) *)&h] = f;
  h._M_invoker = _M_invoke;
  h.D.26519._M_manager = _M_manager;
  if (_M_manager == 0B)
// Same, shouldn't we know that _M_manager!=0?
    goto <bb 4>;
  else
    goto <bb 5>;

  <bb 4>:
  std::__throw_bad_function_call ();

  <bb 5>:
  _24 = h._M_invoker;
// Why not _M_invoke directly, we can only arrive here from bb3
// and nothing can clobber h._M_invoker. Then we might have a chance
// to inline the next line.
  _26 = _24 (&h.D.26519._M_functor, 1);

  <bb 6>:
  _8 = MEM[(struct _Function_base *)&h]._M_manager;
// I guess we need to fix the call to _24 above to know
// there is nothing clobbering h._M_manager.
  if (_8 != 0B)
    goto <bb 7>;
  else
    goto <bb 8>;

  <bb 7>:
  _8 (&MEM[(struct _Function_base *)&h]._M_functor, &MEM[(struct _Function_base
*)&h]._M_functor, 3);

  <bb 8>:
  h ={v} {CLOBBER};
  h ={v} {CLOBBER};
  return _26;

<L3>:
  _9 = MEM[(struct _Function_base *)&h]._M_manager;
  if (_9 != 0B)
    goto <bb 10>;
  else
    goto <bb 11>;

  <bb 10>:
  _9 (&MEM[(struct _Function_base *)&h]._M_functor, &MEM[(struct _Function_base
*)&h]._M_functor, 3);

  <bb 11>:
  h ={v} {CLOBBER};
  _10 = __builtin_eh_pointer (2);
  __builtin_unwind_resume (_10);

}

Trying to modify std::function by removing tests, compiling with
-fno-exceptions, etc, I got to situations with f(1) (not inlined), or we still
had an uninlined call to _M_manager (which, inlined, would reduce to nothing),
but never a clean return 0.

If this simple example gets fixed, please check on the example in
http://gcc.gnu.org/ml/gcc/2014-01/msg00261.html

Related issues:
PR 45631
PR 45632
PR 47413
(PR 56551)


             reply	other threads:[~2014-01-26 11:31 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-26 11:31 glisse at gcc dot gnu.org [this message]
2014-01-26 13:24 ` [Bug ipa/59948] " glisse at gcc dot gnu.org
2014-01-29 13:27 ` rguenth at gcc dot gnu.org
2014-02-04  2:35 ` hubicka at gcc dot gnu.org
2014-02-04  3:00 ` hubicka at gcc dot gnu.org
2014-05-02 14:06 ` glisse at gcc dot gnu.org
2014-09-26 16:54 ` hubicka at gcc dot gnu.org
2015-02-26  0:09 ` glisse at gcc dot gnu.org
2023-09-25  9:04 ` hubicka at gcc dot gnu.org

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