public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/114090] New: forwprop -fwrapv miscompilation
@ 2024-02-24 18:02 kristerw at gcc dot gnu.org
  2024-02-24 18:10 ` [Bug tree-optimization/114090] [13/14 Regression] " pinskia at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: kristerw at gcc dot gnu.org @ 2024-02-24 18:02 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 114090
           Summary: forwprop -fwrapv miscompilation
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kristerw at gcc dot gnu.org
  Target Milestone: ---

The function f below returns an incorrect result for INT_MIN when compiled with
-O1 -fwrapv for X86_64:


__attribute__((noipa)) int f(int x) {
    int w = (x >= 0 ? x : 0);
    int y = -x;
    int z = (y >= 0 ? y : 0);
    return w + z;
}

int
main ()
{
  if (f(0x80000000) != 0)
    __builtin_abort ();
  return 0;
}


What is happening is that forwprop has optimized

  w_2 = MAX_EXPR <x_1(D), 0>;
  y_3 = -x_1(D);
  z_4 = MAX_EXPR <y_3, 0>;
  _5 = w_2 + z_4;
  return _5;

to

  _5 = ABS_EXPR <x_1(D)>;
  return _5;

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

* [Bug tree-optimization/114090] [13/14 Regression] forwprop -fwrapv miscompilation
  2024-02-24 18:02 [Bug tree-optimization/114090] New: forwprop -fwrapv miscompilation kristerw at gcc dot gnu.org
@ 2024-02-24 18:10 ` pinskia at gcc dot gnu.org
  2024-02-24 18:14 ` pinskia at gcc dot gnu.org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-02-24 18:10 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=94920
           Keywords|                            |wrong-code
   Target Milestone|---                         |13.3
            Summary|forwprop -fwrapv            |[13/14 Regression] forwprop
                   |miscompilation              |-fwrapv miscompilation

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The pattern:
/* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x.  */
(simplify
  (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
  (abs @0))

introduced by r13-1785-g633e9920589ddf .

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

* [Bug tree-optimization/114090] [13/14 Regression] forwprop -fwrapv miscompilation
  2024-02-24 18:02 [Bug tree-optimization/114090] New: forwprop -fwrapv miscompilation kristerw at gcc dot gnu.org
  2024-02-24 18:10 ` [Bug tree-optimization/114090] [13/14 Regression] " pinskia at gcc dot gnu.org
@ 2024-02-24 18:14 ` pinskia at gcc dot gnu.org
  2024-02-24 18:16 ` jakub at gcc dot gnu.org
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-02-24 18:14 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2024-02-24
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
There most likely should be this check added:
ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)

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

* [Bug tree-optimization/114090] [13/14 Regression] forwprop -fwrapv miscompilation
  2024-02-24 18:02 [Bug tree-optimization/114090] New: forwprop -fwrapv miscompilation kristerw at gcc dot gnu.org
  2024-02-24 18:10 ` [Bug tree-optimization/114090] [13/14 Regression] " pinskia at gcc dot gnu.org
  2024-02-24 18:14 ` pinskia at gcc dot gnu.org
@ 2024-02-24 18:16 ` jakub at gcc dot gnu.org
  2024-02-24 18:24 ` jakub at gcc dot gnu.org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-02-24 18:16 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Both the patterns look wrong for TYPE_OVERFLOW_WRAPS and the first one also for
TYPE_UNSIGNED (the second one is ok for TYPE_UNSIGNED but doesn't make much
sense there, we should have folded it to 0.  Of course, the first one is
unlikely to trigger for TYPE_UNSIGNED because MAX <something, 0U> should have
been folded to 0.

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

* [Bug tree-optimization/114090] [13/14 Regression] forwprop -fwrapv miscompilation
  2024-02-24 18:02 [Bug tree-optimization/114090] New: forwprop -fwrapv miscompilation kristerw at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2024-02-24 18:16 ` jakub at gcc dot gnu.org
@ 2024-02-24 18:24 ` jakub at gcc dot gnu.org
  2024-02-24 18:27 ` jakub at gcc dot gnu.org
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-02-24 18:24 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

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

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
I'd go with
--- gcc/match.pd.jj     2024-02-22 10:09:48.678446435 +0100
+++ gcc/match.pd        2024-02-24 19:23:32.201014245 +0100
@@ -453,8 +453,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)

 /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x.  */
 (simplify
-  (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
-  (abs @0))
+ (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
+ (if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
+  (abs @0)))

 /* X * 1, X / 1 -> X.  */
 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
@@ -4218,8 +4219,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)

 /* (x <= 0 ? -x : 0) -> max(-x, 0).  */
 (simplify
-  (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
-  (max @2 @1))
+ (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
+ (if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
+  (max @2 @1)))

 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
 (for op (bit_xor bit_ior plus)

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

* [Bug tree-optimization/114090] [13/14 Regression] forwprop -fwrapv miscompilation
  2024-02-24 18:02 [Bug tree-optimization/114090] New: forwprop -fwrapv miscompilation kristerw at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2024-02-24 18:24 ` jakub at gcc dot gnu.org
@ 2024-02-24 18:27 ` jakub at gcc dot gnu.org
  2024-02-24 18:45 ` jakub at gcc dot gnu.org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-02-24 18:27 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P2

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
&& !TYPE_OVERFLOW_SANITIZED (type) is IMHO not needed, because both
transformations for
INT_MIN trigger UB before and after.

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

* [Bug tree-optimization/114090] [13/14 Regression] forwprop -fwrapv miscompilation
  2024-02-24 18:02 [Bug tree-optimization/114090] New: forwprop -fwrapv miscompilation kristerw at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2024-02-24 18:27 ` jakub at gcc dot gnu.org
@ 2024-02-24 18:45 ` jakub at gcc dot gnu.org
  2024-02-26  9:09 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-02-24 18:45 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |jakub at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Created attachment 57521
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=57521&action=edit
gcc14-pr114090.patch

Full untested patch.

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

* [Bug tree-optimization/114090] [13/14 Regression] forwprop -fwrapv miscompilation
  2024-02-24 18:02 [Bug tree-optimization/114090] New: forwprop -fwrapv miscompilation kristerw at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2024-02-24 18:45 ` jakub at gcc dot gnu.org
@ 2024-02-26  9:09 ` cvs-commit at gcc dot gnu.org
  2024-02-26  9:27 ` [Bug tree-optimization/114090] [13 " jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-02-26  9:09 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from GCC 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:24aa051af7c59f37ec45aea754b48b97d210ea6d

commit r14-9175-g24aa051af7c59f37ec45aea754b48b97d210ea6d
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Mon Feb 26 10:08:45 2024 +0100

    match.pd: Guard 2 simplifications on integral TYPE_OVERFLOW_UNDEFINED
[PR114090]

    These 2 patterns are incorrect on floating point, or for -fwrapv, or
    for -ftrapv, or the first one for unsigned types (the second one is
    mathematically correct, but we ought to just fold that to 0 instead).

    So, the following patch properly guards this.

    I think we don't need && !TYPE_OVERFLOW_SANITIZED (type) because
    in both simplifications there would be UB before and after on
    signed integer minimum.

    2024-02-26  Jakub Jelinek  <jakub@redhat.com>

            PR tree-optimization/114090
            * match.pd ((x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x):
            Restrict pattern to ANY_INTEGRAL_TYPE_P and TYPE_OVERFLOW_UNDEFINED
            types.
            ((x <= 0 ? -x : 0) -> max(-x, 0)): Likewise.

            * gcc.dg/pr114090.c: New test.

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

* [Bug tree-optimization/114090] [13 Regression] forwprop -fwrapv miscompilation
  2024-02-24 18:02 [Bug tree-optimization/114090] New: forwprop -fwrapv miscompilation kristerw at gcc dot gnu.org
                   ` (6 preceding siblings ...)
  2024-02-26  9:09 ` cvs-commit at gcc dot gnu.org
@ 2024-02-26  9:27 ` jakub at gcc dot gnu.org
  2024-03-02  0:39 ` cvs-commit at gcc dot gnu.org
  2024-03-04 12:13 ` jakub at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-02-26  9:27 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[13/14 Regression] forwprop |[13 Regression] forwprop
                   |-fwrapv miscompilation      |-fwrapv miscompilation

--- Comment #8 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed on the trunk so far.

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

* [Bug tree-optimization/114090] [13 Regression] forwprop -fwrapv miscompilation
  2024-02-24 18:02 [Bug tree-optimization/114090] New: forwprop -fwrapv miscompilation kristerw at gcc dot gnu.org
                   ` (7 preceding siblings ...)
  2024-02-26  9:27 ` [Bug tree-optimization/114090] [13 " jakub at gcc dot gnu.org
@ 2024-03-02  0:39 ` cvs-commit at gcc dot gnu.org
  2024-03-04 12:13 ` jakub at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-03-02  0:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-13 branch has been updated by Jakub Jelinek
<jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:6b8d8a8bce88981554e5ffeb66bcb6b71a869e62

commit r13-8393-g6b8d8a8bce88981554e5ffeb66bcb6b71a869e62
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Mon Feb 26 10:08:45 2024 +0100

    match.pd: Guard 2 simplifications on integral TYPE_OVERFLOW_UNDEFINED
[PR114090]

    These 2 patterns are incorrect on floating point, or for -fwrapv, or
    for -ftrapv, or the first one for unsigned types (the second one is
    mathematically correct, but we ought to just fold that to 0 instead).

    So, the following patch properly guards this.

    I think we don't need && !TYPE_OVERFLOW_SANITIZED (type) because
    in both simplifications there would be UB before and after on
    signed integer minimum.

    2024-02-26  Jakub Jelinek  <jakub@redhat.com>

            PR tree-optimization/114090
            * match.pd ((x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x):
            Restrict pattern to ANY_INTEGRAL_TYPE_P and TYPE_OVERFLOW_UNDEFINED
            types.
            ((x <= 0 ? -x : 0) -> max(-x, 0)): Likewise.

            * gcc.dg/pr114090.c: New test.

    (cherry picked from commit 24aa051af7c59f37ec45aea754b48b97d210ea6d)

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

* [Bug tree-optimization/114090] [13 Regression] forwprop -fwrapv miscompilation
  2024-02-24 18:02 [Bug tree-optimization/114090] New: forwprop -fwrapv miscompilation kristerw at gcc dot gnu.org
                   ` (8 preceding siblings ...)
  2024-03-02  0:39 ` cvs-commit at gcc dot gnu.org
@ 2024-03-04 12:13 ` jakub at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-03-04 12:13 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

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

--- Comment #10 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Should be now fixed for GCC 13.3+ too.

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

end of thread, other threads:[~2024-03-04 12:13 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-24 18:02 [Bug tree-optimization/114090] New: forwprop -fwrapv miscompilation kristerw at gcc dot gnu.org
2024-02-24 18:10 ` [Bug tree-optimization/114090] [13/14 Regression] " pinskia at gcc dot gnu.org
2024-02-24 18:14 ` pinskia at gcc dot gnu.org
2024-02-24 18:16 ` jakub at gcc dot gnu.org
2024-02-24 18:24 ` jakub at gcc dot gnu.org
2024-02-24 18:27 ` jakub at gcc dot gnu.org
2024-02-24 18:45 ` jakub at gcc dot gnu.org
2024-02-26  9:09 ` cvs-commit at gcc dot gnu.org
2024-02-26  9:27 ` [Bug tree-optimization/114090] [13 " jakub at gcc dot gnu.org
2024-03-02  0:39 ` cvs-commit at gcc dot gnu.org
2024-03-04 12:13 ` jakub 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).