public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/106502] New: Three calls to __attribute__((const)) function
@ 2022-08-01 23:04 redi at gcc dot gnu.org
  2022-08-01 23:10 ` [Bug c++/106502] " pinskia at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2022-08-01 23:04 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106502
           Summary: Three calls to __attribute__((const)) function
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

#include <system_error>

int main()
{
  __builtin_puts(std::system_category().name());
}

With gcc 12 the resulting assembly is:

        .file   "cat.C"
        .text
        .globl  main
        .type   main, @function
main:
.LFB1309:
        .cfi_startproc
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register 6
        pushq   %rbx
        subq    $8, %rsp
        .cfi_offset 3, -24
        call    _ZNSt3_V215system_categoryEv
        call    _ZNSt3_V215system_categoryEv
        movq    (%rax), %rax
        addq    $16, %rax
        movq    (%rax), %rbx
        call    _ZNSt3_V215system_categoryEv
        movq    %rax, %rdi
        call    *%rbx
        movq    %rax, %rdi
        call    puts
        movl    $0, %eax
        movq    -8(%rbp), %rbx
        leave
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc
.LFE1309:
        .size   main, .-main
        .ident  "GCC: (GNU) 12.1.1 20220507 (Red Hat 12.1.1-1)"
        .section        .note.GNU-stack,"",@progbits


Why is the std::system_category() function called three times?

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

* [Bug c++/106502] Three calls to __attribute__((const)) function
  2022-08-01 23:04 [Bug c++/106502] New: Three calls to __attribute__((const)) function redi at gcc dot gnu.org
@ 2022-08-01 23:10 ` pinskia at gcc dot gnu.org
  2022-08-01 23:11 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-08-01 23:10 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2022-08-01

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
  (void) __builtin_puts (OBJ_TYPE_REF(*(((const struct error_category *)
std::_V2::system_category ())->_vptr.error_category + 16);(const struct
error_category)(const struct error_category *) std::_V2::system_category
()->2B) ((const struct error_category *) std::_V2::system_category ())) >>>>>;


Confirmed. with the const, the front-end didn't wrap the function call with a
SAVE_EXPR so it was duplicated three times with the OBJ_TYPE_REF call.

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

* [Bug c++/106502] Three calls to __attribute__((const)) function
  2022-08-01 23:04 [Bug c++/106502] New: Three calls to __attribute__((const)) function redi at gcc dot gnu.org
  2022-08-01 23:10 ` [Bug c++/106502] " pinskia at gcc dot gnu.org
@ 2022-08-01 23:11 ` pinskia at gcc dot gnu.org
  2022-08-02  6:41 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-08-01 23:11 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
As you can see by the gimplification:
  _1 = std::_V2::system_category ();
  _2 = std::_V2::system_category ();
  _3 = _2->_vptr.error_category;
  _4 = _3 + 16;
  _5 = *_4;
  _6 = std::_V2::system_category ();
  _7 = OBJ_TYPE_REF(_5;(const struct error_category)_1->2B) (_6);

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

* [Bug c++/106502] Three calls to __attribute__((const)) function
  2022-08-01 23:04 [Bug c++/106502] New: Three calls to __attribute__((const)) function redi at gcc dot gnu.org
  2022-08-01 23:10 ` [Bug c++/106502] " pinskia at gcc dot gnu.org
  2022-08-01 23:11 ` pinskia at gcc dot gnu.org
@ 2022-08-02  6:41 ` redi at gcc dot gnu.org
  2022-08-02 10:04 ` rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2022-08-02  6:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
It doesn't happen unless the name() function is virtual. Reduced:


extern "C" int puts(const char*);

struct Cat
{
  virtual const char* name();
};

__attribute__((const)) Cat& cat();

int main()
{
  puts(cat().name());
}


Produces this gimple:

int main ()
{
  int D.2388;

  _1 = cat ();
  _2 = cat ();
  _3 = _2->_vptr.Cat;
  _4 = *_3;
  _5 = cat ();
  _6 = OBJ_TYPE_REF(_4;(struct Cat)_1->0B) (_5);
  puts (_6);
  D.2388 = 0;
  return D.2388;
}

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

* [Bug c++/106502] Three calls to __attribute__((const)) function
  2022-08-01 23:04 [Bug c++/106502] New: Three calls to __attribute__((const)) function redi at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2022-08-02  6:41 ` redi at gcc dot gnu.org
@ 2022-08-02 10:04 ` rguenth at gcc dot gnu.org
  2022-08-02 10:13 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-08-02 10:04 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
Once you enable optimization a single cat () call remains (as expected).  The
FE does

9931          if (TREE_SIDE_EFFECTS (argarray[0]))
9932            argarray[0] = save_expr (argarray[0]);
9933          t = build_pointer_type (TREE_TYPE (fn));
9934          fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));

but argarray[0] doesn't have TREE_SIDE_EFFECTS set, it even has TREE_READONLY
set on the CONVERT_EXPR wrapping the call.  Note that save_expr would do
nothing on this tree as well (because TREE_READONLY and !TREE_SIDE_EFFECTS).

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

* [Bug c++/106502] Three calls to __attribute__((const)) function
  2022-08-01 23:04 [Bug c++/106502] New: Three calls to __attribute__((const)) function redi at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2022-08-02 10:04 ` rguenth at gcc dot gnu.org
@ 2022-08-02 10:13 ` redi at gcc dot gnu.org
  2022-08-04  2:31 ` egallager at gcc dot gnu.org
  2022-08-04  6:27 ` redi at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2022-08-02 10:13 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I noticed this by adding a printf statement to the const function for temporary
debugging purposes, which is obviously incorrect and not a problem for real
code. It's not observable that it gets called more than once, it just makes
unoptimized code a bit slower.

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

* [Bug c++/106502] Three calls to __attribute__((const)) function
  2022-08-01 23:04 [Bug c++/106502] New: Three calls to __attribute__((const)) function redi at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2022-08-02 10:13 ` redi at gcc dot gnu.org
@ 2022-08-04  2:31 ` egallager at gcc dot gnu.org
  2022-08-04  6:27 ` redi at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: egallager at gcc dot gnu.org @ 2022-08-04  2:31 UTC (permalink / raw)
  To: gcc-bugs

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

Eric Gallager <egallager at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=18487
                 CC|                            |egallager at gcc dot gnu.org

--- Comment #6 from Eric Gallager <egallager at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #5)
> I noticed this by adding a printf statement to the const function for
> temporary debugging purposes, which is obviously incorrect

Seems related to bug 18487 IMO.

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

* [Bug c++/106502] Three calls to __attribute__((const)) function
  2022-08-01 23:04 [Bug c++/106502] New: Three calls to __attribute__((const)) function redi at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2022-08-04  2:31 ` egallager at gcc dot gnu.org
@ 2022-08-04  6:27 ` redi at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2022-08-04  6:27 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The way I found the bug might be, but the bug itself is nothing to do with
that.

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

end of thread, other threads:[~2022-08-04  6:27 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-01 23:04 [Bug c++/106502] New: Three calls to __attribute__((const)) function redi at gcc dot gnu.org
2022-08-01 23:10 ` [Bug c++/106502] " pinskia at gcc dot gnu.org
2022-08-01 23:11 ` pinskia at gcc dot gnu.org
2022-08-02  6:41 ` redi at gcc dot gnu.org
2022-08-02 10:04 ` rguenth at gcc dot gnu.org
2022-08-02 10:13 ` redi at gcc dot gnu.org
2022-08-04  2:31 ` egallager at gcc dot gnu.org
2022-08-04  6:27 ` redi 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).