public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/45221] missed optimization with multiple bases and casting
       [not found] <bug-45221-4@http.gcc.gnu.org/bugzilla/>
@ 2011-05-17 15:49 ` navin.kumar at gmail dot com
  2023-05-06 18:14 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 9+ messages in thread
From: navin.kumar at gmail dot com @ 2011-05-17 15:49 UTC (permalink / raw)
  To: gcc-bugs

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

Navin Kumar <navin.kumar at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|4.5.0                       |4.6.0

--- Comment #11 from Navin Kumar <navin.kumar at gmail dot com> 2011-05-17 15:18:06 UTC ---
Bumping this to 4.6.0 since the problem still exists.

As a review (since this has been open and not updated for a while), the problem
is very slow performance when dealing with C++'s multiple-inheritance (even
under -O3).  In the example below, the 'simple' fooA function generates slow
code, whereas the equivalent code written with more steps in fooB generates
optimal code.

Base2* fooA(Derived* x)
{
    Base2& y = *x;
    return &y;
}
Base2* fooB(Derived* x) {
    Derived& x2 = *x;
    Base2& y = x2;
    return &y;
}

Both fooA and fooB are funtionally identical.

Yet the assembly generated for fooA is:
    leaq    4(%rdi), %rdx
    xorl    %eax, %eax
    testq   %rdi, %rdi
    cmovne  %rdx, %rax
    ret

and the assembly generated for fooB is:
    leaq    4(%rdi), %rax
    ret


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

* [Bug c++/45221] missed optimization with multiple bases and casting
       [not found] <bug-45221-4@http.gcc.gnu.org/bugzilla/>
  2011-05-17 15:49 ` [Bug c++/45221] missed optimization with multiple bases and casting navin.kumar at gmail dot com
@ 2023-05-06 18:14 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-06 18:14 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |8.0
         Resolution|---                         |FIXED
      Known to work|                            |8.1.0, 9.1.0
             Status|NEW                         |RESOLVED

--- Comment #12 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
foo1 is fixed in GCC 8. 
foo2 (and foo1 before GCC 8) produces a trap instruction since GCC 4.9.0.
That is foo2 produces:
        testq   %rdi, %rdi
        je      .L4
        movl    4(%rdi), %eax
        ret
.L4:
        movl    8, %eax
        ud2

So I think I can close this as fixed.

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

* [Bug c++/45221] missed optimization with multiple bases and casting
  2010-08-07  1:28 [Bug c++/45221] New: missed optimization on casting pointers even under -O3 navin dot kumar at gmail dot com
                   ` (5 preceding siblings ...)
  2010-08-07 16:27 ` rguenth at gcc dot gnu dot org
@ 2010-08-07 17:28 ` navin dot kumar at gmail dot com
  6 siblings, 0 replies; 9+ messages in thread
From: navin dot kumar at gmail dot com @ 2010-08-07 17:28 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #10 from navin dot kumar at gmail dot com  2010-08-07 17:27 -------
Richard, correct references in C++ cannot bind to NULL.  So gcc should derive
non-NULL-ness when the argument is a reference.  It seems to correctly do this
in the case of foo3 and fooB, but fails to do so for foo1 and fooA.


-- 


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


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

* [Bug c++/45221] missed optimization with multiple bases and casting
  2010-08-07  1:28 [Bug c++/45221] New: missed optimization on casting pointers even under -O3 navin dot kumar at gmail dot com
                   ` (4 preceding siblings ...)
  2010-08-07 16:25 ` rguenth at gcc dot gnu dot org
@ 2010-08-07 16:27 ` rguenth at gcc dot gnu dot org
  2010-08-07 17:28 ` navin dot kumar at gmail dot com
  6 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2010-08-07 16:27 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #9 from rguenth at gcc dot gnu dot org  2010-08-07 16:27 -------
(In reply to comment #7)
> Richard, if you can't derive non-NULL-ness from X& y = *x, how do foo3 and fooB
> avoid the null check?

For both cases the C++ frontend do not emit the NULL check.


-- 


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


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

* [Bug c++/45221] missed optimization with multiple bases and casting
  2010-08-07  1:28 [Bug c++/45221] New: missed optimization on casting pointers even under -O3 navin dot kumar at gmail dot com
                   ` (3 preceding siblings ...)
  2010-08-07 16:23 ` navin dot kumar at gmail dot com
@ 2010-08-07 16:25 ` rguenth at gcc dot gnu dot org
  2010-08-07 16:27 ` rguenth at gcc dot gnu dot org
  2010-08-07 17:28 ` navin dot kumar at gmail dot com
  6 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2010-08-07 16:25 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from rguenth at gcc dot gnu dot org  2010-08-07 16:25 -------
Which means that if the language guarantees that for

Base2* fooA(Derived* x)
{
    Base2& y = *x;
    return &y;
}

x being a null pointer invokes undefined behavior (because references can't
bind to nothing(?)) then the frontend should emit not

<<cleanup_point   struct Base2 & y = (struct Base2 &) (NON_LVALUE_EXPR <x> !=
0B ? &NON_LVALUE_EXPR <x>->D.1702 : 0B);>>;

but instead

<<cleanup_point   struct Base2 & y = (struct Base2 &) &NON_LVALUE_EXPR
<x>->D.1702;>>;

CCing Jason.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jason at gcc dot gnu dot org


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


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

* [Bug c++/45221] missed optimization with multiple bases and casting
  2010-08-07  1:28 [Bug c++/45221] New: missed optimization on casting pointers even under -O3 navin dot kumar at gmail dot com
                   ` (2 preceding siblings ...)
  2010-08-07 16:19 ` rguenth at gcc dot gnu dot org
@ 2010-08-07 16:23 ` navin dot kumar at gmail dot com
  2010-08-07 16:25 ` rguenth at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: navin dot kumar at gmail dot com @ 2010-08-07 16:23 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from navin dot kumar at gmail dot com  2010-08-07 16:22 -------
Richard, if you can't derive non-NULL-ness from X& y = *x, how do foo3 and fooB
avoid the null check?


-- 


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


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

* [Bug c++/45221] missed optimization with multiple bases and casting
  2010-08-07  1:28 [Bug c++/45221] New: missed optimization on casting pointers even under -O3 navin dot kumar at gmail dot com
  2010-08-07 10:37 ` [Bug c++/45221] missed optimization with multiple bases and casting rguenth at gcc dot gnu dot org
  2010-08-07 15:26 ` navin dot kumar at gmail dot com
@ 2010-08-07 16:19 ` rguenth at gcc dot gnu dot org
  2010-08-07 16:23 ` navin dot kumar at gmail dot com
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2010-08-07 16:19 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from rguenth at gcc dot gnu dot org  2010-08-07 16:19 -------
(In reply to comment #5)
> Hi Richard,
> 
> Your explanation doesn't explain why foo1 would emit poorer assembly than foo3.
> 
> Or for that matter why fooA would emit poorer assembly than fooB.
> 
> In the case of foo1, foo3, fooA, and fooB, dereferencing occurs first, before
> casting.  Yet only foo3 and fooB generate optimal assembly (so gcc is clearly
> "capable" of outputting the desired asm).  This is all at -O3, where
> "-fdelete-null-pointer-checks" is already enabled.

X* x;
X& = *x;

is not considered a dereference - internally it is just a pointer assignment
we can't derive non-NULL-ness from.


-- 


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


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

* [Bug c++/45221] missed optimization with multiple bases and casting
  2010-08-07  1:28 [Bug c++/45221] New: missed optimization on casting pointers even under -O3 navin dot kumar at gmail dot com
  2010-08-07 10:37 ` [Bug c++/45221] missed optimization with multiple bases and casting rguenth at gcc dot gnu dot org
@ 2010-08-07 15:26 ` navin dot kumar at gmail dot com
  2010-08-07 16:19 ` rguenth at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: navin dot kumar at gmail dot com @ 2010-08-07 15:26 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from navin dot kumar at gmail dot com  2010-08-07 15:25 -------
Hi Richard,

Your explanation doesn't explain why foo1 would emit poorer assembly than foo3.

Or for that matter why fooA would emit poorer assembly than fooB.

In the case of foo1, foo3, fooA, and fooB, dereferencing occurs first, before
casting.  Yet only foo3 and fooB generate optimal assembly (so gcc is clearly
"capable" of outputting the desired asm).  This is all at -O3, where
"-fdelete-null-pointer-checks" is already enabled.


-- 


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


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

* [Bug c++/45221] missed optimization with multiple bases and casting
  2010-08-07  1:28 [Bug c++/45221] New: missed optimization on casting pointers even under -O3 navin dot kumar at gmail dot com
@ 2010-08-07 10:37 ` rguenth at gcc dot gnu dot org
  2010-08-07 15:26 ` navin dot kumar at gmail dot com
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2010-08-07 10:37 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from rguenth at gcc dot gnu dot org  2010-08-07 10:37 -------
I don't see what the bug is here - this is a feature of the C++ standard,
we can't really "optimize" anything here.  5.2.9/8 ... The null pointer
value is converted to the null pointer value of the destination type.
In the case of foo3 'y' is not a pointer so that special case does not apply.

The only thing we can do is excercise knowledge of undefined behavior as you
dereference the resulting pointer in foo1 and foo2 and thus the behavior
is undefined if that would be a null pointer.

The FE hands us

;; Function int foo2(Base2*) (null)
;; enabled by -tree-original

return <retval> = (x != 0B ? (struct Derived *) x + -4 : 0B)->data;

and we arrive with

<bb 2>:
  if (x_2(D) != 0B)
    goto <bb 3>;
  else
    goto <bb 4>;

<bb 3>:
  iftmp.1_3 = x_2(D) + -4;
  goto <bb 5>;

<bb 4>:
  iftmp.1_4 = 0;

<bb 5>:
  # iftmp.1_1 = PHI <iftmp.1_3(3), 0(4)>
  D.1726_5 = iftmp.1_1->data;

which we could for example (with -fdelete-null-pointer-checks, where no
objects at address zero can exist), optimize during phiprop if we
insert undefined values as loads from NULL.  We could also value-number
loads based on NULL to VN_TOP which would optimize the case during PRE.

Much less fragile is when the code gets inlined into a context where
we know that x isn't a NULL pointer.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rguenth at gcc dot gnu dot
                   |                            |org


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


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

end of thread, other threads:[~2023-05-06 18:14 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-45221-4@http.gcc.gnu.org/bugzilla/>
2011-05-17 15:49 ` [Bug c++/45221] missed optimization with multiple bases and casting navin.kumar at gmail dot com
2023-05-06 18:14 ` pinskia at gcc dot gnu.org
2010-08-07  1:28 [Bug c++/45221] New: missed optimization on casting pointers even under -O3 navin dot kumar at gmail dot com
2010-08-07 10:37 ` [Bug c++/45221] missed optimization with multiple bases and casting rguenth at gcc dot gnu dot org
2010-08-07 15:26 ` navin dot kumar at gmail dot com
2010-08-07 16:19 ` rguenth at gcc dot gnu dot org
2010-08-07 16:23 ` navin dot kumar at gmail dot com
2010-08-07 16:25 ` rguenth at gcc dot gnu dot org
2010-08-07 16:27 ` rguenth at gcc dot gnu dot org
2010-08-07 17:28 ` navin dot kumar at gmail 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).