public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/115410] New: Missing optimization: typeid(*a)==typeid(*b) could be optimized to a->_vptr==b->_vptr
@ 2024-06-10  6:06 user202729 at protonmail dot com
  2024-06-10  6:09 ` [Bug c++/115410] " pinskia at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: user202729 at protonmail dot com @ 2024-06-10  6:06 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 115410
           Summary: Missing optimization: typeid(*a)==typeid(*b) could be
                    optimized to a->_vptr==b->_vptr
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: user202729 at protonmail dot com
  Target Milestone: ---

As in the title. I think if both `*a` and `*b` are objects of type with a
virtual pointer then `typeid(*a)==typeid(*b)` could be optimized to

    if(!a) throw bad_typeid;
    if(!b) throw bad_typeid;
    return a->_vptr==b->_vptr;

Godbolt: https://godbolt.org/z/MjY67Pc4b

Although I think this optimization should be done at the level of the C++ front
end (`cp_build_binary_op`?) because once the expression is down to gimple level
then it becomes something like

    type_info::operator==(a->_vptr[-1], b->vptr[-1])

and it seems not clean to hard code the constant `-1` (it is currently hard
coded only in `get_tinfo_decl_dynamic` function).

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

* [Bug c++/115410] Missing optimization: typeid(*a)==typeid(*b) could be optimized to a->_vptr==b->_vptr
  2024-06-10  6:06 [Bug c++/115410] New: Missing optimization: typeid(*a)==typeid(*b) could be optimized to a->_vptr==b->_vptr user202729 at protonmail dot com
@ 2024-06-10  6:09 ` pinskia at gcc dot gnu.org
  2024-06-10  6:11 ` user202729 at protonmail dot com
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-10  6:09 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I am not so sure. Many times the comparison becomes a string comparison and not
comparing the vtable.

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

* [Bug c++/115410] Missing optimization: typeid(*a)==typeid(*b) could be optimized to a->_vptr==b->_vptr
  2024-06-10  6:06 [Bug c++/115410] New: Missing optimization: typeid(*a)==typeid(*b) could be optimized to a->_vptr==b->_vptr user202729 at protonmail dot com
  2024-06-10  6:09 ` [Bug c++/115410] " pinskia at gcc dot gnu.org
@ 2024-06-10  6:11 ` user202729 at protonmail dot com
  2024-06-10  6:16 ` pinskia at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: user202729 at protonmail dot com @ 2024-06-10  6:11 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from user202729 <user202729 at protonmail dot com> ---
(In reply to Andrew Pinski from comment #1)
> I am not so sure. Many times the comparison becomes a string comparison and
> not comparing the vtable.

That's my point. Pointer comparison is faster than string comparison, and it
should hold that the typeid are equal iff the types are equal iff the vptr are
equal, so the optimization should be valid.

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

* [Bug c++/115410] Missing optimization: typeid(*a)==typeid(*b) could be optimized to a->_vptr==b->_vptr
  2024-06-10  6:06 [Bug c++/115410] New: Missing optimization: typeid(*a)==typeid(*b) could be optimized to a->_vptr==b->_vptr user202729 at protonmail dot com
  2024-06-10  6:09 ` [Bug c++/115410] " pinskia at gcc dot gnu.org
  2024-06-10  6:11 ` user202729 at protonmail dot com
@ 2024-06-10  6:16 ` pinskia at gcc dot gnu.org
  2024-06-10  6:20 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-10  6:16 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Except it is not valid. 

See https://gcc.gnu.org/pipermail/libstdc++/2004-February/021443.html which
changed this to be always a string comparison.

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

* [Bug c++/115410] Missing optimization: typeid(*a)==typeid(*b) could be optimized to a->_vptr==b->_vptr
  2024-06-10  6:06 [Bug c++/115410] New: Missing optimization: typeid(*a)==typeid(*b) could be optimized to a->_vptr==b->_vptr user202729 at protonmail dot com
                   ` (2 preceding siblings ...)
  2024-06-10  6:16 ` pinskia at gcc dot gnu.org
@ 2024-06-10  6:20 ` pinskia at gcc dot gnu.org
  2024-06-10  6:29 ` user202729 at protonmail dot com
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-10  6:20 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
From the header:
Even with the new abi, on systems that support dlopen
    // we can run into cases where type_info names aren't merged,
    // so we still need to do string comparison

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

* [Bug c++/115410] Missing optimization: typeid(*a)==typeid(*b) could be optimized to a->_vptr==b->_vptr
  2024-06-10  6:06 [Bug c++/115410] New: Missing optimization: typeid(*a)==typeid(*b) could be optimized to a->_vptr==b->_vptr user202729 at protonmail dot com
                   ` (3 preceding siblings ...)
  2024-06-10  6:20 ` pinskia at gcc dot gnu.org
@ 2024-06-10  6:29 ` user202729 at protonmail dot com
  2024-06-10  6:33 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: user202729 at protonmail dot com @ 2024-06-10  6:29 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from user202729 <user202729 at protonmail dot com> ---
(In reply to Andrew Pinski from comment #3)
> Except it is not valid. 
> 
> See https://gcc.gnu.org/pipermail/libstdc++/2004-February/021443.html which
> changed this to be always a string comparison.

Thanks for informing me.

However, that comment talks about `type_info` names aren't merged (i.e. it's
possible for the `__name` pointers to be different but their contents are the
same), but is it possible for the virtual table address to be different for the
same type?

(I don't think it's possible because virtual table becomes linker symbols?)

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

* [Bug c++/115410] Missing optimization: typeid(*a)==typeid(*b) could be optimized to a->_vptr==b->_vptr
  2024-06-10  6:06 [Bug c++/115410] New: Missing optimization: typeid(*a)==typeid(*b) could be optimized to a->_vptr==b->_vptr user202729 at protonmail dot com
                   ` (4 preceding siblings ...)
  2024-06-10  6:29 ` user202729 at protonmail dot com
@ 2024-06-10  6:33 ` pinskia at gcc dot gnu.org
  2024-06-10  6:51 ` user202729 at protonmail dot com
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-10  6:33 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
They can be different due to the way shared libraries work.

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

* [Bug c++/115410] Missing optimization: typeid(*a)==typeid(*b) could be optimized to a->_vptr==b->_vptr
  2024-06-10  6:06 [Bug c++/115410] New: Missing optimization: typeid(*a)==typeid(*b) could be optimized to a->_vptr==b->_vptr user202729 at protonmail dot com
                   ` (5 preceding siblings ...)
  2024-06-10  6:33 ` pinskia at gcc dot gnu.org
@ 2024-06-10  6:51 ` user202729 at protonmail dot com
  2024-06-10  9:18 ` redi at gcc dot gnu.org
  2024-06-10  9:20 ` user202729 at protonmail dot com
  8 siblings, 0 replies; 10+ messages in thread
From: user202729 at protonmail dot com @ 2024-06-10  6:51 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from user202729 <user202729 at protonmail dot com> ---
(In reply to Andrew Pinski from comment #6)
> They can be different due to the way shared libraries work.

Ah, too bad.

Is it safe to at least assume that the function pointers inside the virtual
pointer table be equal then? The original motivation was to devirtualize the
function call inside something like (though this would be a different PR)

    if(typeid(*x)==typeid(A))
        x->f();

then would it be safe to assume `x->_vptr[0] == &A::f`?

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

* [Bug c++/115410] Missing optimization: typeid(*a)==typeid(*b) could be optimized to a->_vptr==b->_vptr
  2024-06-10  6:06 [Bug c++/115410] New: Missing optimization: typeid(*a)==typeid(*b) could be optimized to a->_vptr==b->_vptr user202729 at protonmail dot com
                   ` (6 preceding siblings ...)
  2024-06-10  6:51 ` user202729 at protonmail dot com
@ 2024-06-10  9:18 ` redi at gcc dot gnu.org
  2024-06-10  9:20 ` user202729 at protonmail dot com
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2024-06-10  9:18 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to user202729 from comment #7)
> (In reply to Andrew Pinski from comment #6)
> > They can be different due to the way shared libraries work.
> 
> Ah, too bad.
> 
> Is it safe to at least assume that the function pointers inside the virtual
> pointer table be equal then?

No, there can be multiple definitions of a symbol, with different addresses.


 The original motivation was to devirtualize the
> function call inside something like (though this would be a different PR)
> 
>     if(typeid(*x)==typeid(A))
>         x->f();
> 
> then would it be safe to assume `x->_vptr[0] == &A::f`?

No, but the transformation might be valid anyway. There could be multiple
copies of A::f with different addresses, but they all have to have the same
effects and so it doesn't matter which one gets called.

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

* [Bug c++/115410] Missing optimization: typeid(*a)==typeid(*b) could be optimized to a->_vptr==b->_vptr
  2024-06-10  6:06 [Bug c++/115410] New: Missing optimization: typeid(*a)==typeid(*b) could be optimized to a->_vptr==b->_vptr user202729 at protonmail dot com
                   ` (7 preceding siblings ...)
  2024-06-10  9:18 ` redi at gcc dot gnu.org
@ 2024-06-10  9:20 ` user202729 at protonmail dot com
  8 siblings, 0 replies; 10+ messages in thread
From: user202729 at protonmail dot com @ 2024-06-10  9:20 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from user202729 <user202729 at protonmail dot com> ---
Alright, I open https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115413 instead
(discussing one case where gcc already calls a different function from what it
should)

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

end of thread, other threads:[~2024-06-10  9:20 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-10  6:06 [Bug c++/115410] New: Missing optimization: typeid(*a)==typeid(*b) could be optimized to a->_vptr==b->_vptr user202729 at protonmail dot com
2024-06-10  6:09 ` [Bug c++/115410] " pinskia at gcc dot gnu.org
2024-06-10  6:11 ` user202729 at protonmail dot com
2024-06-10  6:16 ` pinskia at gcc dot gnu.org
2024-06-10  6:20 ` pinskia at gcc dot gnu.org
2024-06-10  6:29 ` user202729 at protonmail dot com
2024-06-10  6:33 ` pinskia at gcc dot gnu.org
2024-06-10  6:51 ` user202729 at protonmail dot com
2024-06-10  9:18 ` redi at gcc dot gnu.org
2024-06-10  9:20 ` user202729 at protonmail dot com

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