Bug 23518 - some gcc optimizations do not take overflow into account with -fwrapv
Summary: some gcc optimizations do not take overflow into account with -fwrapv
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.0.1
: P2 normal
Target Milestone: 4.2.0
Assignee: Kazu Hirata
URL: http://gcc.gnu.org/ml/gcc-patches/200...
Keywords: patch, wrong-code
Depends on:
Blocks:
 
Reported: 2005-08-22 19:34 UTC by Pascal Terjan
Modified: 2005-12-22 04:06 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 3.4.0 4.0.0 4.1.0 4.2.0
Last reconfirmed: 2005-10-27 16:57:56


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Pascal Terjan 2005-08-22 19:34:31 UTC
This has been tested on 3.3.6, 3.4.1 and 4.0.1

The following test is considered always false and the block is dropped but "a"
being "int", (a + 1 < 0) is true.

[pterjan@plop tmp]$ cat lim.c
#include <stdio.h>
#include <limits.h>

int main (void)
{
        int a = INT_MAX;
        if ((a < 0) || (a + 1 < 0)) {
                printf("Hello !\n");
        }
        return 0;
}
[pterjan@plop tmp]$ gcc -O0 lim.c ; ./a.out
Hello !
[pterjan@plop tmp]$ gcc -O1 lim.c ; ./a.out
[pterjan@plop tmp]$
Comment 1 Andrew Pinski 2005-08-22 19:45:48 UTC
Note signed overflow is undefined unless you use -fwrapv except that does not fix this.

This is a bug in fold, most likely build_range_check. 
Comment 2 Andrew Pinski 2005-08-22 19:52:39 UTC
Here is a java program (since -fwrapv is turned on by default for java front-end unlike the C front-end 
since it is undefined in C and defined in java):
class t
{
  public static void main(String as[])
  {
    int a;
    a = Integer.MAX_VALUE;
    if ((a < 0) || (a + 1 < 0))  {
      System.out.println("Hello");
    }
  }
}
Comment 3 Andrew Pinski 2005-11-28 16:28:04 UTC
fold_range_test is wrong, around fold-const.c:4635
Comment 4 Kazu Hirata 2005-12-20 19:58:35 UTC
I've got a patch.
Comment 5 Kazu Hirata 2005-12-22 04:03:41 UTC
Subject: Bug 23518

Author: kazu
Date: Thu Dec 22 04:03:32 2005
New Revision: 108940

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=108940
Log:
gcc/
	PR tree-optimization/23518
	* fold-const.c (make_range): Don't move a constant to the
	other side of the comparison if the type is signed and -fwrapv
	is given.

gcc/testsuite/
	PR tree-optimization/23518
	* testsuite/gcc.dg/pr23518.c: New.

Added:
    trunk/gcc/testsuite/gcc.dg/pr23518.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/fold-const.c
    trunk/gcc/testsuite/ChangeLog

Comment 6 Kazu Hirata 2005-12-22 04:04:25 UTC
Just checked in a patch.
Comment 7 Kazu Hirata 2005-12-22 04:06:11 UTC
Oops, I forgot to change resolution to FIXED.