Bug 20922 - missed always false conditional
Summary: missed always false conditional
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.1.0
: P2 enhancement
Target Milestone: 4.1.0
Assignee: James A. Morrison
URL:
Keywords: missed-optimization, TREE
Depends on:
Blocks: 19986
  Show dependency treegraph
 
Reported: 2005-04-09 21:35 UTC by Andrew Pinski
Modified: 2005-04-25 01:01 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-04-11 19:49:49


Attachments
Fold stuff (1.01 KB, patch)
2005-04-16 17:12 UTC, James A. Morrison
Details | Diff
tests (252 bytes, text/plain)
2005-04-16 17:13 UTC, James A. Morrison
Details
more tests (157 bytes, text/plain)
2005-04-16 17:13 UTC, James A. Morrison
Details
tests galour (211 bytes, text/plain)
2005-04-16 17:14 UTC, James A. Morrison
Details
reverse tests (252 bytes, text/plain)
2005-04-16 17:14 UTC, James A. Morrison
Details
yup, more (157 bytes, text/plain)
2005-04-16 17:15 UTC, James A. Morrison
Details
I only have 6 files so far (211 bytes, text/plain)
2005-04-16 17:15 UTC, James A. Morrison
Details
Attempt 2 (955 bytes, patch)
2005-04-18 03:44 UTC, James A. Morrison
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Pinski 2005-04-09 21:35:58 UTC
The following function should just "return 1":
int f(int i)
{
  int i1 = i -2;
  if (i1 > i)
    return 0;
  return 1;
}
We miss this on the tree level, I found this while look into the following fortran code:
SUBROUTINE d ( a, b,n)
IMPLICIT NONE
INTEGER :: n
REAL,DIMENSION(n) :: a
REAL,DIMENSION(n) :: b
b(n-2:n) = sqrt(a(n-2:n))
END SUBROUTINE d


In final_cleanup, we have:
  D.679 = *n;
  S.2 = D.679 - 2;
  if (D.679 < S.2) goto L.1; else goto <L7>;
Comment 1 Andrew Pinski 2005-04-10 21:02:38 UTC
Reduced testcase showing fold does not do it:
int f(int i)
{
  return (i - 2) > i;
}
Comment 2 Andrew Pinski 2005-04-10 21:06:31 UTC
This is not caught untill RTL combine.
Comment 3 Andrew Pinski 2005-04-10 23:43:39 UTC
Note this is only true with -fno-wrapv (which is default for C and C++ and I think Fortran too but not 
Java).
Comment 4 Andrew Pinski 2005-04-11 19:49:48 UTC
Confirmed, the code which does this on the RTL level is in simplify_const_relational_operation.
Comment 5 James A. Morrison 2005-04-16 17:12:28 UTC
Created attachment 8654 [details]
Fold stuff
Comment 6 James A. Morrison 2005-04-16 17:13:25 UTC
Created attachment 8655 [details]
tests
Comment 7 James A. Morrison 2005-04-16 17:13:43 UTC
Created attachment 8656 [details]
more tests
Comment 8 James A. Morrison 2005-04-16 17:14:01 UTC
Created attachment 8657 [details]
tests galour
Comment 9 James A. Morrison 2005-04-16 17:14:27 UTC
Created attachment 8658 [details]
reverse tests
Comment 10 James A. Morrison 2005-04-16 17:15:06 UTC
Created attachment 8659 [details]
yup, more
Comment 11 James A. Morrison 2005-04-16 17:15:29 UTC
Created attachment 8660 [details]
I only have 6 files so far
Comment 12 James A. Morrison 2005-04-18 03:44:24 UTC
Created attachment 8673 [details]
Attempt 2
Comment 14 James A. Morrison 2005-04-18 15:20:06 UTC
Fixed in the last commit.
Comment 15 Markus F.X.J. Oberhumer 2005-04-25 00:38:42 UTC
Is this optimization really legal in C/C++? Could some language lawyer look at
this (and check the output when compiling with -O3):

#include <limits.h>

int f1(int i) {  return (i - 2) > i; }
int f2(int i) {  return (i + 2) > i; }

int g1(void)  { return f1(INT_MIN); }
int g2(void)  { return f2(INT_MAX); }
Comment 16 Andrew Pinski 2005-04-25 00:40:55 UTC
(In reply to comment #15)
> Is this optimization really legal in C/C++? Could some language lawyer look at
> this (and check the output when compiling with -O3):
Overflow/Underflow of signed operands in C/C++ is undefined, now in Java it is not.
Comment 17 jim.morrison@gmail.com 2005-04-25 00:42:23 UTC
Subject: Re:  missed always false conditional

On 25 Apr 2005 00:39:23 -0000, markus at oberhumer dot com
<gcc-bugzilla@gcc.gnu.org> wrote:
> 
> 
> --
>            What    |Removed                     |Added
> ----------------------------------------------------------------------------
>                  CC|                            |markus at oberhumer dot com
> 
> 
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20922

 The overflow semantics are undefined for signed types in C/C++.
Try with -ftrapv or -fwrapv if you need defined semantics for C/C++.

Comment 18 Markus F.X.J. Oberhumer 2005-04-25 01:01:37 UTC
Ah, many thanks for the clarification. Still it's somewhat confusing that f1()
and g1() don't agree after inlining, but "undefined" probably means exactly that...