public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/106070] New: Wrong code with -O1
@ 2022-06-24  4:45 vsevolod.livinskiy at gmail dot com
  2022-06-24  4:49 ` [Bug tree-optimization/106070] " pinskia at gcc dot gnu.org
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: vsevolod.livinskiy at gmail dot com @ 2022-06-24  4:45 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106070
           Summary: Wrong code with -O1
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vsevolod.livinskiy at gmail dot com
  Target Milestone: ---

GCC produces incorrect code with -O1.
I was not able to merge the test into a single file.

Reproducer:
// func.cpp
extern unsigned var_2;
extern int var_4;
extern signed char var_10;
extern unsigned long long arr_252;
void test() {
  for (unsigned a = 0; a < (unsigned char)var_10; a += 2)
    arr_252 = var_2 != var_4 ? var_4 : (long)var_2;
}

//driver.cpp
#include <stdio.h>

unsigned int var_2 = 1;
int var_4 = -1;
signed char var_10 = (signed char)(-127 - 1);
unsigned long long int arr_252;

void test();

int main() {
    test();
    printf("%llx\n", arr_252);
    if (arr_252 != 0xffffffffffffffff)
        __builtin_abort();
}

Error:
>$ g++ -O0 func.cpp driver.cpp && ./a.out 
ffffffffffffffff
>$  g++ -O1 func.cpp driver.cpp && ./a.out 
ffffffff
Aborted (core dumped)

gcc version 13.0.0 20220623 (31ce821a790caec8a2849dd67a9847e78a33d14c)

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

* [Bug tree-optimization/106070] Wrong code with -O1
  2022-06-24  4:45 [Bug tree-optimization/106070] New: Wrong code with -O1 vsevolod.livinskiy at gmail dot com
@ 2022-06-24  4:49 ` pinskia at gcc dot gnu.org
  2022-06-24  5:04 ` pinskia at gcc dot gnu.org
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-06-24  4:49 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Single file testcase:
#include <stdio.h>
unsigned int var_2 = 1;
int var_4 = -1;
signed char var_10 = (signed char)(-127 - 1);
unsigned long long int arr_252;
[[gnu::noipa]]
void test() {
  for (unsigned a = 0; a < (unsigned char)var_10; a += 2)
    arr_252 = var_2 != var_4 ? var_4 : (long)var_2;
}

void test();

int main() {
    test();
    printf("%llx\n", arr_252);
    if (arr_252 != 0xffffffffffffffff)
        __builtin_abort();
}

(The [[gnu::noipa]] makes things easier for single file testcases so no
inlining or other IPA passes on the function).

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

* [Bug tree-optimization/106070] Wrong code with -O1
  2022-06-24  4:45 [Bug tree-optimization/106070] New: Wrong code with -O1 vsevolod.livinskiy at gmail dot com
  2022-06-24  4:49 ` [Bug tree-optimization/106070] " pinskia at gcc dot gnu.org
@ 2022-06-24  5:04 ` pinskia at gcc dot gnu.org
  2022-06-24  8:46 ` [Bug tree-optimization/106070] [13 Regression] Wrong code with -O1 since r13-469-g9a53101caadae1b5 marxin at gcc dot gnu.org
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-06-24  5:04 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2022-06-24
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
           Keywords|                            |needs-bisection

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Reduced testcase:
#include <stdio.h>
unsigned int var_2 = 1;
int var_4 = -1;
int var_10 = 4;
unsigned long arr_252;
[[gnu::noipa]]
void test() {
  for (int a = 0; a < var_10; a += 2)
    arr_252 = var_2 != (int)var_4 ? (unsigned long)var_4 : (unsigned
long)var_2;
}

void test();

int main() {
    test();
    fprintf(stderr, "%lx\n", arr_252);
    if (arr_252 != 0xffffffffffffffff)
        __builtin_abort();
}

---- CUT ---
There is a missing sign extend from 32bit to 64bit.

Confirmed.

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

* [Bug tree-optimization/106070] [13 Regression] Wrong code with -O1 since r13-469-g9a53101caadae1b5
  2022-06-24  4:45 [Bug tree-optimization/106070] New: Wrong code with -O1 vsevolod.livinskiy at gmail dot com
  2022-06-24  4:49 ` [Bug tree-optimization/106070] " pinskia at gcc dot gnu.org
  2022-06-24  5:04 ` pinskia at gcc dot gnu.org
@ 2022-06-24  8:46 ` marxin at gcc dot gnu.org
  2022-06-24 10:32 ` rguenth at gcc dot gnu.org
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: marxin at gcc dot gnu.org @ 2022-06-24  8:46 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Liška <marxin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|needs-bisection             |
                 CC|                            |marxin at gcc dot gnu.org,
                   |                            |rguenth at gcc dot gnu.org
            Summary|[13 Regression] Wrong code  |[13 Regression] Wrong code
                   |with -O1                    |with -O1 since
                   |                            |r13-469-g9a53101caadae1b5

--- Comment #3 from Martin Liška <marxin at gcc dot gnu.org> ---
Started with r13-469-g9a53101caadae1b5.

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

* [Bug tree-optimization/106070] [13 Regression] Wrong code with -O1 since r13-469-g9a53101caadae1b5
  2022-06-24  4:45 [Bug tree-optimization/106070] New: Wrong code with -O1 vsevolod.livinskiy at gmail dot com
                   ` (2 preceding siblings ...)
  2022-06-24  8:46 ` [Bug tree-optimization/106070] [13 Regression] Wrong code with -O1 since r13-469-g9a53101caadae1b5 marxin at gcc dot gnu.org
@ 2022-06-24 10:32 ` rguenth at gcc dot gnu.org
  2022-06-24 11:08 ` rguenth at gcc dot gnu.org
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-06-24 10:32 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |rguenth at gcc dot gnu.org
   Target Milestone|---                         |13.0
             Status|NEW                         |ASSIGNED

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
Ah, smaller.  Nice.

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

* [Bug tree-optimization/106070] [13 Regression] Wrong code with -O1 since r13-469-g9a53101caadae1b5
  2022-06-24  4:45 [Bug tree-optimization/106070] New: Wrong code with -O1 vsevolod.livinskiy at gmail dot com
                   ` (3 preceding siblings ...)
  2022-06-24 10:32 ` rguenth at gcc dot gnu.org
@ 2022-06-24 11:08 ` rguenth at gcc dot gnu.org
  2022-06-24 12:50 ` cvs-commit at gcc dot gnu.org
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-06-24 11:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Richard Biener <rguenth at gcc dot gnu.org> ---
+Applying pattern match.pd:4591, gimple-match.cc:59644
...
@@ -31,7 +41,7 @@
   var_4.3_3 = (unsigned int) var_4.2_2;
   iftmp.0_9 = (long unsigned int) var_2.1_1;
   iftmp.0_10 = (long unsigned int) var_4.2_2;
   _4 = var_2.1_1 != var_4.3_3;
-  iftmp.0_6 = _4 ? iftmp.0_10 : iftmp.0_9;
+  iftmp.0_6 = (long unsigned int) var_4.3_3;

   <bb 4> [local count: 955630225]:
   # a_16 = PHI <a_12(7), 0(3)>

so that's

 var_2 != (unsigned) var_4 ? -> (unsigned long) var_4 : (unsigned long) var_2

which we turn into (unsigned long) (unsigned) var_4.  I thought that's what
generic also does but I have to double-check operand_equal_for_comparison_p
here.

Ah, it checks

  /* Discard a single widening conversion from ARG1 and see if the inner
     value is the same as ARG0.  */
  if (CONVERT_EXPR_P (arg1)
      && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
      && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
         < TYPE_PRECISION (TREE_TYPE (arg1))
      && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))

and the operand_equal_p matches against the non-NOP-stripped arg0.  So it's
either a same precision or widening of the comparison op.

Hmm, will have to think how to translate that into a sign check to make it
fit match.pd (I don't want to export and use operand_equal_for_comparison_p)

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

* [Bug tree-optimization/106070] [13 Regression] Wrong code with -O1 since r13-469-g9a53101caadae1b5
  2022-06-24  4:45 [Bug tree-optimization/106070] New: Wrong code with -O1 vsevolod.livinskiy at gmail dot com
                   ` (4 preceding siblings ...)
  2022-06-24 11:08 ` rguenth at gcc dot gnu.org
@ 2022-06-24 12:50 ` cvs-commit at gcc dot gnu.org
  2022-06-24 12:50 ` rguenth at gcc dot gnu.org
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-06-24 12:50 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Richard Biener <rguenth@gcc.gnu.org>:

https://gcc.gnu.org/g:b36a1c964f99758de1f3b169628965d3c3af812b

commit r13-1243-gb36a1c964f99758de1f3b169628965d3c3af812b
Author: Richard Biener <rguenther@suse.de>
Date:   Fri Jun 24 13:37:22 2022 +0200

    middle-end/106070 - bogus cond-expr folding

    The following fixes up r13-469-g9a53101caadae1b5 by properly
    implementing what operand_equal_for_comparison_p did.

    2022-06-24  Richard Biener  <rguenther@suse.de>

            PR middle-end/106070
            * match.pd (a != b ? a : b): Fix translation of
            operand_equal_for_comparison_p.

            * gcc.dg/torture/pr106070.c: New testcase.

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

* [Bug tree-optimization/106070] [13 Regression] Wrong code with -O1 since r13-469-g9a53101caadae1b5
  2022-06-24  4:45 [Bug tree-optimization/106070] New: Wrong code with -O1 vsevolod.livinskiy at gmail dot com
                   ` (5 preceding siblings ...)
  2022-06-24 12:50 ` cvs-commit at gcc dot gnu.org
@ 2022-06-24 12:50 ` rguenth at gcc dot gnu.org
  2022-06-24 12:52 ` rguenth at gcc dot gnu.org
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-06-24 12:50 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED

--- Comment #7 from Richard Biener <rguenth at gcc dot gnu.org> ---
Fixed.

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

* [Bug tree-optimization/106070] [13 Regression] Wrong code with -O1 since r13-469-g9a53101caadae1b5
  2022-06-24  4:45 [Bug tree-optimization/106070] New: Wrong code with -O1 vsevolod.livinskiy at gmail dot com
                   ` (6 preceding siblings ...)
  2022-06-24 12:50 ` rguenth at gcc dot gnu.org
@ 2022-06-24 12:52 ` rguenth at gcc dot gnu.org
  2022-06-27  9:46 ` rearnsha at gcc dot gnu.org
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-06-24 12:52 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Richard Biener <rguenth at gcc dot gnu.org> ---
*** Bug 106025 has been marked as a duplicate of this bug. ***

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

* [Bug tree-optimization/106070] [13 Regression] Wrong code with -O1 since r13-469-g9a53101caadae1b5
  2022-06-24  4:45 [Bug tree-optimization/106070] New: Wrong code with -O1 vsevolod.livinskiy at gmail dot com
                   ` (7 preceding siblings ...)
  2022-06-24 12:52 ` rguenth at gcc dot gnu.org
@ 2022-06-27  9:46 ` rearnsha at gcc dot gnu.org
  2022-06-27  9:47 ` rearnsha at gcc dot gnu.org
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rearnsha at gcc dot gnu.org @ 2022-06-27  9:46 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Earnshaw <rearnsha at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|FIXED                       |---

--- Comment #9 from Richard Earnshaw <rearnsha at gcc dot gnu.org> ---
The testcase incorrectly assumes that long is 64-bits.

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

* [Bug tree-optimization/106070] [13 Regression] Wrong code with -O1 since r13-469-g9a53101caadae1b5
  2022-06-24  4:45 [Bug tree-optimization/106070] New: Wrong code with -O1 vsevolod.livinskiy at gmail dot com
                   ` (8 preceding siblings ...)
  2022-06-27  9:46 ` rearnsha at gcc dot gnu.org
@ 2022-06-27  9:47 ` rearnsha at gcc dot gnu.org
  2022-06-27 13:36 ` cvs-commit at gcc dot gnu.org
  2022-06-28  6:41 ` rguenth at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: rearnsha at gcc dot gnu.org @ 2022-06-27  9:47 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #10 from Richard Earnshaw <rearnsha at gcc dot gnu.org> ---
the testcase in the committed patch, that is.

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

* [Bug tree-optimization/106070] [13 Regression] Wrong code with -O1 since r13-469-g9a53101caadae1b5
  2022-06-24  4:45 [Bug tree-optimization/106070] New: Wrong code with -O1 vsevolod.livinskiy at gmail dot com
                   ` (9 preceding siblings ...)
  2022-06-27  9:47 ` rearnsha at gcc dot gnu.org
@ 2022-06-27 13:36 ` cvs-commit at gcc dot gnu.org
  2022-06-28  6:41 ` rguenth at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-06-27 13:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #11 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:00063459f683adcd92ada8325984e6b10e9f7a95

commit r13-1299-g00063459f683adcd92ada8325984e6b10e9f7a95
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Mon Jun 27 15:35:25 2022 +0200

    testsuite: Fix up pr106070.c test [PR106070]

    The test FAILs on 32-bit targets, because when unsigned long
    is 32-bit, (unsigned long) -1 isn't 0xffffffffffffffff.
    The options to fix this would be either using -1UL, or switch
    to unsigned long long and using -1ULL, I chose the latter because
    the test then FAILs in r13-1242 even on 32-bit targets.
    And while at it, some deobfuscation and formatting tweaks.

    2022-06-27  Jakub Jelinek  <jakub@redhat.com>

            PR tree-optimization/106070
            * gcc.dg/torture/pr106070.c: Use unsigned long long instead of
            unsigned long and -1ULL instead of 0xffffffffffffffff, deobcuscate
            and improve formatting.

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

* [Bug tree-optimization/106070] [13 Regression] Wrong code with -O1 since r13-469-g9a53101caadae1b5
  2022-06-24  4:45 [Bug tree-optimization/106070] New: Wrong code with -O1 vsevolod.livinskiy at gmail dot com
                   ` (10 preceding siblings ...)
  2022-06-27 13:36 ` cvs-commit at gcc dot gnu.org
@ 2022-06-28  6:41 ` rguenth at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-06-28  6:41 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|REOPENED                    |RESOLVED

--- Comment #12 from Richard Biener <rguenth at gcc dot gnu.org> ---
Jakub fixed it.

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

end of thread, other threads:[~2022-06-28  6:41 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-24  4:45 [Bug tree-optimization/106070] New: Wrong code with -O1 vsevolod.livinskiy at gmail dot com
2022-06-24  4:49 ` [Bug tree-optimization/106070] " pinskia at gcc dot gnu.org
2022-06-24  5:04 ` pinskia at gcc dot gnu.org
2022-06-24  8:46 ` [Bug tree-optimization/106070] [13 Regression] Wrong code with -O1 since r13-469-g9a53101caadae1b5 marxin at gcc dot gnu.org
2022-06-24 10:32 ` rguenth at gcc dot gnu.org
2022-06-24 11:08 ` rguenth at gcc dot gnu.org
2022-06-24 12:50 ` cvs-commit at gcc dot gnu.org
2022-06-24 12:50 ` rguenth at gcc dot gnu.org
2022-06-24 12:52 ` rguenth at gcc dot gnu.org
2022-06-27  9:46 ` rearnsha at gcc dot gnu.org
2022-06-27  9:47 ` rearnsha at gcc dot gnu.org
2022-06-27 13:36 ` cvs-commit at gcc dot gnu.org
2022-06-28  6:41 ` rguenth 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).