public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/98501] New: potential optimization for base<->derived pointer casts
@ 2021-01-02 22:56 vanyacpp at gmail dot com
  2021-01-05 11:10 ` [Bug c++/98501] " rguenth at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: vanyacpp at gmail dot com @ 2021-01-02 22:56 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 98501
           Summary: potential optimization for base<->derived pointer
                    casts
           Product: gcc
           Version: 10.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vanyacpp at gmail dot com
  Target Milestone: ---

Consider this code:

struct base1 { int a; };
struct base2 { int b; };
struct derived : base1, base2 {};

derived& to_derived_bad(base2* b)
{
    return *static_cast<derived*>(b);
}

derived& to_derived_good(base2* b)
{
    return static_cast<derived&>(*b);
}

I believe both of these functions are functionally equivalent and should
generate the same code. Both functions cast pointer from base to derived if it
is not nullptr and both cause undefined behavior if it is nullptr.

GCC optimizes to_derived_good() to a single subtraction, but it inserts
nullptr-check into to_derived_bad():

to_derived_good(base2*):
    lea rax, [rdi-4]
    ret
to_derived_bad(base2*):
    lea rax, [rdi-4]
    test rdi, rdi
    mov edx, 0
    cmove rax, rdx
    ret

Could GCC omit the nullptr-check in to_derived_bad?

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

* [Bug c++/98501] potential optimization for base<->derived pointer casts
  2021-01-02 22:56 [Bug c++/98501] New: potential optimization for base<->derived pointer casts vanyacpp at gmail dot com
@ 2021-01-05 11:10 ` rguenth at gcc dot gnu.org
  2021-01-05 17:23 ` vanyacpp at gmail dot com
  2021-01-07 13:13 ` redi at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-01-05 11:10 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

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

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
I think there's a duplicate of this PR.

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

* [Bug c++/98501] potential optimization for base<->derived pointer casts
  2021-01-02 22:56 [Bug c++/98501] New: potential optimization for base<->derived pointer casts vanyacpp at gmail dot com
  2021-01-05 11:10 ` [Bug c++/98501] " rguenth at gcc dot gnu.org
@ 2021-01-05 17:23 ` vanyacpp at gmail dot com
  2021-01-07 13:13 ` redi at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: vanyacpp at gmail dot com @ 2021-01-05 17:23 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Ivan Sorokin <vanyacpp at gmail dot com> ---
(In reply to Richard Biener from comment #1)
> I think there's a duplicate of this PR.

I searched the list of bugs and I found PR95663. Is it it?

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

* [Bug c++/98501] potential optimization for base<->derived pointer casts
  2021-01-02 22:56 [Bug c++/98501] New: potential optimization for base<->derived pointer casts vanyacpp at gmail dot com
  2021-01-05 11:10 ` [Bug c++/98501] " rguenth at gcc dot gnu.org
  2021-01-05 17:23 ` vanyacpp at gmail dot com
@ 2021-01-07 13:13 ` redi at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: redi at gcc dot gnu.org @ 2021-01-07 13:13 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Yes, that looks like the same thing. Thanks for finding it.

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

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

end of thread, other threads:[~2021-01-07 13:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-02 22:56 [Bug c++/98501] New: potential optimization for base<->derived pointer casts vanyacpp at gmail dot com
2021-01-05 11:10 ` [Bug c++/98501] " rguenth at gcc dot gnu.org
2021-01-05 17:23 ` vanyacpp at gmail dot com
2021-01-07 13:13 ` 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).