public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/114937] New: [11 regression] -ftree-vrp optimizes out range check before conditional increment
@ 2024-05-03 13:41 mital at mitalashok dot co.uk
  2024-05-03 13:50 ` [Bug tree-optimization/114937] " rguenth at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: mital at mitalashok dot co.uk @ 2024-05-03 13:41 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 114937
           Summary: [11 regression] -ftree-vrp optimizes out range check
                    before conditional increment
           Product: gcc
           Version: 11.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mital at mitalashok dot co.uk
  Target Milestone: ---

https://godbolt.org/z/fdMGWa4nq

    int f(int x) {
      const int y = x;
      if (x != INT_MAX) {
        ++x;
      }

      return (x > y) ? x : 0;
    }

    int z = INT_MAX;
    int main(void) {
        // Prints INT_MIN when it should print 0
        __builtin_printf("%d\n", f(z));
    }

`f` is miscompiled at `-O1 -ftree-vrp` (also `-O2`) in GCC10/11 to return `x+1`
unconditionally.  
The same happens with `if (x != INT_MIN) --x; return (x < y) ? x : 0;`.

This does not happen in GCC9 or GCC12+

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

* [Bug tree-optimization/114937] [11 regression] -ftree-vrp optimizes out range check before conditional increment
  2024-05-03 13:41 [Bug tree-optimization/114937] New: [11 regression] -ftree-vrp optimizes out range check before conditional increment mital at mitalashok dot co.uk
@ 2024-05-03 13:50 ` rguenth at gcc dot gnu.org
  2024-05-03 13:54 ` rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-05-03 13:50 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |11.5
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2024-05-03
           Keywords|                            |wrong-code
     Ever confirmed|0                           |1

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.  This is the ifcombine bug still not fixed on the 11 branch, aka
PR105142 I think.

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

* [Bug tree-optimization/114937] [11 regression] -ftree-vrp optimizes out range check before conditional increment
  2024-05-03 13:41 [Bug tree-optimization/114937] New: [11 regression] -ftree-vrp optimizes out range check before conditional increment mital at mitalashok dot co.uk
  2024-05-03 13:50 ` [Bug tree-optimization/114937] " rguenth at gcc dot gnu.org
@ 2024-05-03 13:54 ` rguenth at gcc dot gnu.org
  2024-05-03 14:21 ` mital at mitalashok dot co.uk
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-05-03 13:54 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to fail|                            |10.5.0, 11.4.1
      Known to work|                            |12.1.0, 9.5.0
           Priority|P3                          |P2

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
int __attribute__((noipa))
f(int x)
{
  const int y = x;
  if (x != __INT_MAX__)
      ++x;
  return (x > y) ? x : 0;
}

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


Did you run into this for real-world code?

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

* [Bug tree-optimization/114937] [11 regression] -ftree-vrp optimizes out range check before conditional increment
  2024-05-03 13:41 [Bug tree-optimization/114937] New: [11 regression] -ftree-vrp optimizes out range check before conditional increment mital at mitalashok dot co.uk
  2024-05-03 13:50 ` [Bug tree-optimization/114937] " rguenth at gcc dot gnu.org
  2024-05-03 13:54 ` rguenth at gcc dot gnu.org
@ 2024-05-03 14:21 ` mital at mitalashok dot co.uk
  2024-05-06 10:50 ` rguenth at gcc dot gnu.org
  2024-05-12 10:35 ` mikpelinux at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: mital at mitalashok dot co.uk @ 2024-05-03 14:21 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Mital Ashok <mital at mitalashok dot co.uk> ---
My real code looks more like:

void sat_inc(int& y) {
    if (y < __INT_MAX__)
        ++y;
}

template<typename... F>
void f(int& x, F&&... functions) {
    int copy = x;
    (functions(copy), ...);
    if (copy > x)
        x = copy;
}

void g(int& x) {
    f(x, sat_inc);
}

... Where `g(x)` became `++x` unconditionally

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

* [Bug tree-optimization/114937] [11 regression] -ftree-vrp optimizes out range check before conditional increment
  2024-05-03 13:41 [Bug tree-optimization/114937] New: [11 regression] -ftree-vrp optimizes out range check before conditional increment mital at mitalashok dot co.uk
                   ` (2 preceding siblings ...)
  2024-05-03 14:21 ` mital at mitalashok dot co.uk
@ 2024-05-06 10:50 ` rguenth at gcc dot gnu.org
  2024-05-12 10:35 ` mikpelinux at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-05-06 10:50 UTC (permalink / raw)
  To: gcc-bugs

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

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
             Status|NEW                         |ASSIGNED

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
I'll see if it's possible to pick the fix, but I won't promise anything since
GCC 11 is now quite old.

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

* [Bug tree-optimization/114937] [11 regression] -ftree-vrp optimizes out range check before conditional increment
  2024-05-03 13:41 [Bug tree-optimization/114937] New: [11 regression] -ftree-vrp optimizes out range check before conditional increment mital at mitalashok dot co.uk
                   ` (3 preceding siblings ...)
  2024-05-06 10:50 ` rguenth at gcc dot gnu.org
@ 2024-05-12 10:35 ` mikpelinux at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: mikpelinux at gmail dot com @ 2024-05-12 10:35 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Mikael Pettersson <mikpelinux at gmail dot com> ---
I ran a git bisect between 11.4.0 and 12.3.0, which identified the following as
fixing this test case:

2e96b5f14e4025691b57d2301d71aa6092ed44bc is the first new commit
commit 2e96b5f14e4025691b57d2301d71aa6092ed44bc
Author: Aldy Hernandez <aldyh@redhat.com>
Date:   Tue Jun 15 12:32:51 2021 +0200

    Backwards jump threader rewrite with ranger.

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

end of thread, other threads:[~2024-05-12 10:35 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-03 13:41 [Bug tree-optimization/114937] New: [11 regression] -ftree-vrp optimizes out range check before conditional increment mital at mitalashok dot co.uk
2024-05-03 13:50 ` [Bug tree-optimization/114937] " rguenth at gcc dot gnu.org
2024-05-03 13:54 ` rguenth at gcc dot gnu.org
2024-05-03 14:21 ` mital at mitalashok dot co.uk
2024-05-06 10:50 ` rguenth at gcc dot gnu.org
2024-05-12 10:35 ` mikpelinux 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).