public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/106064] New: Wrong code comparing two global zero-sized arrays
@ 2022-06-23 12:36 acoplan at gcc dot gnu.org
  2022-06-23 16:21 ` [Bug tree-optimization/106064] " ktkachov at gcc dot gnu.org
                   ` (13 more replies)
  0 siblings, 14 replies; 15+ messages in thread
From: acoplan at gcc dot gnu.org @ 2022-06-23 12:36 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106064
           Summary: Wrong code comparing two global zero-sized arrays
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: acoplan at gcc dot gnu.org
  Target Milestone: ---

For the following C code:

int a[0] = {};
int b[0] = {};
int f(void)
{
    return a != b;
}
int g(void)
{
    return (__UINTPTR_TYPE__)a != (__UINTPTR_TYPE__)b;
}

on AArch64 at -O2, we generate:

f:
        mov     w0, 1
        ret
g:
        mov     w0, 0
        ret

so f and g get optimized in contradictory ways. At least one of them must be
wrong.

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

* [Bug tree-optimization/106064] Wrong code comparing two global zero-sized arrays
  2022-06-23 12:36 [Bug tree-optimization/106064] New: Wrong code comparing two global zero-sized arrays acoplan at gcc dot gnu.org
@ 2022-06-23 16:21 ` ktkachov at gcc dot gnu.org
  2022-06-24  9:56 ` mikpelinux at gmail dot com
                   ` (12 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: ktkachov at gcc dot gnu.org @ 2022-06-23 16:21 UTC (permalink / raw)
  To: gcc-bugs

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

ktkachov at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
                 CC|                            |ktkachov at gcc dot gnu.org

--- Comment #1 from ktkachov at gcc dot gnu.org ---
This seems to have changed in the GCC 9 series. GCC 8.5 generates:
f():
        mov     w0, 1
        ret
g():
        mov     w0, 1
        ret
b:
a:

Tagging as a claimed wrong-code bug.

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

* [Bug tree-optimization/106064] Wrong code comparing two global zero-sized arrays
  2022-06-23 12:36 [Bug tree-optimization/106064] New: Wrong code comparing two global zero-sized arrays acoplan at gcc dot gnu.org
  2022-06-23 16:21 ` [Bug tree-optimization/106064] " ktkachov at gcc dot gnu.org
@ 2022-06-24  9:56 ` mikpelinux at gmail dot com
  2022-06-24 10:20 ` pinskia at gcc dot gnu.org
                   ` (11 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: mikpelinux at gmail dot com @ 2022-06-24  9:56 UTC (permalink / raw)
  To: gcc-bugs

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

Mikael Pettersson <mikpelinux at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mikpelinux at gmail dot com

--- Comment #2 from Mikael Pettersson <mikpelinux at gmail dot com> ---
Seems target-dependent. I can't reproduce on x86_64-linux-gnu or
sparc64-linux-gnu: both compile f() to return 1 and g() to perform a runtime
computation. But ppc64-linux-gnu and armv7l-linux-gnueabi behave as your
aarch64 example: f() returns 1 and g() returns 0 (unconditionally, no runtime
computations).

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

* [Bug tree-optimization/106064] Wrong code comparing two global zero-sized arrays
  2022-06-23 12:36 [Bug tree-optimization/106064] New: Wrong code comparing two global zero-sized arrays acoplan at gcc dot gnu.org
  2022-06-23 16:21 ` [Bug tree-optimization/106064] " ktkachov at gcc dot gnu.org
  2022-06-24  9:56 ` mikpelinux at gmail dot com
@ 2022-06-24 10:20 ` pinskia at gcc dot gnu.org
  2022-06-24 10:21 ` pinskia at gcc dot gnu.org
                   ` (10 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-06-24 10:20 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Mikael Pettersson from comment #2)
> Seems target-dependent. I can't reproduce on x86_64-linux-gnu or
> sparc64-linux-gnu: both compile f() to return 1 and g() to perform a runtime
> computation. But ppc64-linux-gnu and armv7l-linux-gnueabi behave as your
> aarch64 example: f() returns 1 and g() returns 0 (unconditionally, no
> runtime computations).

Most likely section anchors is the cause of the difference between the targets.
The ones which implement section anchors return different values between the
functions. Is suspect MIPS has similar behavior too.

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

* [Bug tree-optimization/106064] Wrong code comparing two global zero-sized arrays
  2022-06-23 12:36 [Bug tree-optimization/106064] New: Wrong code comparing two global zero-sized arrays acoplan at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2022-06-24 10:20 ` pinskia at gcc dot gnu.org
@ 2022-06-24 10:21 ` pinskia at gcc dot gnu.org
  2022-06-24 10:28 ` acoplan at gcc dot gnu.org
                   ` (9 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-06-24 10:21 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #3)
> (In reply to Mikael Pettersson from comment #2)
> > Seems target-dependent. I can't reproduce on x86_64-linux-gnu or
> > sparc64-linux-gnu: both compile f() to return 1 and g() to perform a runtime
> > computation. But ppc64-linux-gnu and armv7l-linux-gnueabi behave as your
> > aarch64 example: f() returns 1 and g() returns 0 (unconditionally, no
> > runtime computations).
> 
> Most likely section anchors is the cause of the difference between the
> targets.
> The ones which implement section anchors return different values between the
> functions. Is suspect MIPS has similar behavior too.

It is also most likely why aarch64 changed between 8 and 9 too.

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

* [Bug tree-optimization/106064] Wrong code comparing two global zero-sized arrays
  2022-06-23 12:36 [Bug tree-optimization/106064] New: Wrong code comparing two global zero-sized arrays acoplan at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2022-06-24 10:21 ` pinskia at gcc dot gnu.org
@ 2022-06-24 10:28 ` acoplan at gcc dot gnu.org
  2022-06-24 10:30 ` rguenth at gcc dot gnu.org
                   ` (8 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: acoplan at gcc dot gnu.org @ 2022-06-24 10:28 UTC (permalink / raw)
  To: gcc-bugs

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

Alex Coplan <acoplan at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #5 from Alex Coplan <acoplan at gcc dot gnu.org> ---
Bisection shows this changed with
r9-5411-g93aa3c4aca3647645cd5bce724f9d2126de4b5ea on AArch64:

commit 93aa3c4aca3647645cd5bce724f9d2126de4b5ea (refs/bisect/bad)
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Tue Jan 15 09:11:00 2019 +0100

    re PR tree-optimization/88775 (Optimize std::string assignment)

            PR tree-optimization/88775
            * match.pd (cmp (convert1?@2 addr@0) (convert2? addr@1)): Optimize
            equal == 0 equality pointer comparisons some more if compared in
            integral types and either one points to an automatic var and the
            other to a global, or we can prove at least one points to the
middle
            or both point to start or both point to end.

            * gcc.dg/tree-ssa/pr88775-1.c: New test.
            * gcc.dg/tree-ssa/pr88775-2.c: New test.

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

* [Bug tree-optimization/106064] Wrong code comparing two global zero-sized arrays
  2022-06-23 12:36 [Bug tree-optimization/106064] New: Wrong code comparing two global zero-sized arrays acoplan at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2022-06-24 10:28 ` acoplan at gcc dot gnu.org
@ 2022-06-24 10:30 ` rguenth at gcc dot gnu.org
  2022-06-24 10:34 ` acoplan at gcc dot gnu.org
                   ` (7 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-06-24 10:30 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Richard Biener <rguenth at gcc dot gnu.org> ---
Note whether a != b is probably undefined (but zero size objects are a GNU
extension).

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

* [Bug tree-optimization/106064] Wrong code comparing two global zero-sized arrays
  2022-06-23 12:36 [Bug tree-optimization/106064] New: Wrong code comparing two global zero-sized arrays acoplan at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2022-06-24 10:30 ` rguenth at gcc dot gnu.org
@ 2022-06-24 10:34 ` acoplan at gcc dot gnu.org
  2022-06-24 10:50 ` jakub at gcc dot gnu.org
                   ` (6 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: acoplan at gcc dot gnu.org @ 2022-06-24 10:34 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Alex Coplan <acoplan at gcc dot gnu.org> ---
(In reply to Richard Biener from comment #6)
> Note whether a != b is probably undefined (but zero size objects are a GNU
> extension).

Just to clarify, are you saying this is undefined specifically for zero size
objects or undefined in general for distinct objects? I thought for the latter
== and != were ok, but < and such were UB.

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

* [Bug tree-optimization/106064] Wrong code comparing two global zero-sized arrays
  2022-06-23 12:36 [Bug tree-optimization/106064] New: Wrong code comparing two global zero-sized arrays acoplan at gcc dot gnu.org
                   ` (6 preceding siblings ...)
  2022-06-24 10:34 ` acoplan at gcc dot gnu.org
@ 2022-06-24 10:50 ` jakub at gcc dot gnu.org
  2022-06-27 10:15 ` acoplan at gcc dot gnu.org
                   ` (5 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-06-24 10:50 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
The IMHO UB case is for a != b when one address is at the start of one object
and the other address is at the end of another one, which for zero sized
objects is more often because the start address is the same as end address.
For integral comparisons we try to be more conservative.

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

* [Bug tree-optimization/106064] Wrong code comparing two global zero-sized arrays
  2022-06-23 12:36 [Bug tree-optimization/106064] New: Wrong code comparing two global zero-sized arrays acoplan at gcc dot gnu.org
                   ` (7 preceding siblings ...)
  2022-06-24 10:50 ` jakub at gcc dot gnu.org
@ 2022-06-27 10:15 ` acoplan at gcc dot gnu.org
  2022-06-27 10:19 ` jakub at gcc dot gnu.org
                   ` (4 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: acoplan at gcc dot gnu.org @ 2022-06-27 10:15 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from Alex Coplan <acoplan at gcc dot gnu.org> ---
So if f is UB, I suppose the question is whether the codegen for g is correct?

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

* [Bug tree-optimization/106064] Wrong code comparing two global zero-sized arrays
  2022-06-23 12:36 [Bug tree-optimization/106064] New: Wrong code comparing two global zero-sized arrays acoplan at gcc dot gnu.org
                   ` (8 preceding siblings ...)
  2022-06-27 10:15 ` acoplan at gcc dot gnu.org
@ 2022-06-27 10:19 ` jakub at gcc dot gnu.org
  2022-06-27 12:21 ` acoplan at gcc dot gnu.org
                   ` (3 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-06-27 10:19 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #10 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
IMHO it is.

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

* [Bug tree-optimization/106064] Wrong code comparing two global zero-sized arrays
  2022-06-23 12:36 [Bug tree-optimization/106064] New: Wrong code comparing two global zero-sized arrays acoplan at gcc dot gnu.org
                   ` (9 preceding siblings ...)
  2022-06-27 10:19 ` jakub at gcc dot gnu.org
@ 2022-06-27 12:21 ` acoplan at gcc dot gnu.org
  2022-06-27 14:18 ` rearnsha at gcc dot gnu.org
                   ` (2 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: acoplan at gcc dot gnu.org @ 2022-06-27 12:21 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #11 from Alex Coplan <acoplan at gcc dot gnu.org> ---
(In reply to Jakub Jelinek from comment #8)
> The IMHO UB case is for a != b when one address is at the start of one
> object and the other address is at the end of another one

Just to dig a little deeper on this, what makes this case UB? Is there
something in the standard to this effect?

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

* [Bug tree-optimization/106064] Wrong code comparing two global zero-sized arrays
  2022-06-23 12:36 [Bug tree-optimization/106064] New: Wrong code comparing two global zero-sized arrays acoplan at gcc dot gnu.org
                   ` (10 preceding siblings ...)
  2022-06-27 12:21 ` acoplan at gcc dot gnu.org
@ 2022-06-27 14:18 ` rearnsha at gcc dot gnu.org
  2022-06-27 14:25 ` acoplan at gcc dot gnu.org
  2022-07-08 17:02 ` acoplan at gcc dot gnu.org
  13 siblings, 0 replies; 15+ messages in thread
From: rearnsha at gcc dot gnu.org @ 2022-06-27 14:18 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #12 from Richard Earnshaw <rearnsha at gcc dot gnu.org> ---
(In reply to Alex Coplan from comment #11)
> (In reply to Jakub Jelinek from comment #8)
> > The IMHO UB case is for a != b when one address is at the start of one
> > object and the other address is at the end of another one
> 
> Just to dig a little deeper on this, what makes this case UB? Is there
> something in the standard to this effect?

As stated in #6, zero-sized objects are a GNU extension.  I guess that means we
get to define what the behaviour should be :)

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

* [Bug tree-optimization/106064] Wrong code comparing two global zero-sized arrays
  2022-06-23 12:36 [Bug tree-optimization/106064] New: Wrong code comparing two global zero-sized arrays acoplan at gcc dot gnu.org
                   ` (11 preceding siblings ...)
  2022-06-27 14:18 ` rearnsha at gcc dot gnu.org
@ 2022-06-27 14:25 ` acoplan at gcc dot gnu.org
  2022-07-08 17:02 ` acoplan at gcc dot gnu.org
  13 siblings, 0 replies; 15+ messages in thread
From: acoplan at gcc dot gnu.org @ 2022-06-27 14:25 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #13 from Alex Coplan <acoplan at gcc dot gnu.org> ---
(In reply to Richard Earnshaw from comment #12)
> (In reply to Alex Coplan from comment #11)
> > (In reply to Jakub Jelinek from comment #8)
> > > The IMHO UB case is for a != b when one address is at the start of one
> > > object and the other address is at the end of another one
> > 
> > Just to dig a little deeper on this, what makes this case UB? Is there
> > something in the standard to this effect?
> 
> As stated in #6, zero-sized objects are a GNU extension.  I guess that means
> we get to define what the behaviour should be :)

Sure, but Jakub's reply seemed to get at some underlying principle as opposed
to just saying "zero-sized objects are a GNU extension and we declare this case
to be undefined".

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

* [Bug tree-optimization/106064] Wrong code comparing two global zero-sized arrays
  2022-06-23 12:36 [Bug tree-optimization/106064] New: Wrong code comparing two global zero-sized arrays acoplan at gcc dot gnu.org
                   ` (12 preceding siblings ...)
  2022-06-27 14:25 ` acoplan at gcc dot gnu.org
@ 2022-07-08 17:02 ` acoplan at gcc dot gnu.org
  13 siblings, 0 replies; 15+ messages in thread
From: acoplan at gcc dot gnu.org @ 2022-07-08 17:02 UTC (permalink / raw)
  To: gcc-bugs

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

Alex Coplan <acoplan at gcc dot gnu.org> changed:

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

--- Comment #14 from Alex Coplan <acoplan at gcc dot gnu.org> ---
Closing as invalid, then, given that (as Jakub says) f is UB and the codegen
for g is correct.

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

end of thread, other threads:[~2022-07-08 17:02 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-23 12:36 [Bug tree-optimization/106064] New: Wrong code comparing two global zero-sized arrays acoplan at gcc dot gnu.org
2022-06-23 16:21 ` [Bug tree-optimization/106064] " ktkachov at gcc dot gnu.org
2022-06-24  9:56 ` mikpelinux at gmail dot com
2022-06-24 10:20 ` pinskia at gcc dot gnu.org
2022-06-24 10:21 ` pinskia at gcc dot gnu.org
2022-06-24 10:28 ` acoplan at gcc dot gnu.org
2022-06-24 10:30 ` rguenth at gcc dot gnu.org
2022-06-24 10:34 ` acoplan at gcc dot gnu.org
2022-06-24 10:50 ` jakub at gcc dot gnu.org
2022-06-27 10:15 ` acoplan at gcc dot gnu.org
2022-06-27 10:19 ` jakub at gcc dot gnu.org
2022-06-27 12:21 ` acoplan at gcc dot gnu.org
2022-06-27 14:18 ` rearnsha at gcc dot gnu.org
2022-06-27 14:25 ` acoplan at gcc dot gnu.org
2022-07-08 17:02 ` acoplan 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).