Bug 38470 - value range propagation (VRP) would improve -Wsign-compare
value range propagation (VRP) would improve -Wsign-compare
Status: NEW
Product: gcc
Classification: Unclassified
Component: c
unknown
: P3 enhancement
: ---
Assigned To: Not yet assigned to anyone
: diagnostic
: 49426 59293 64518 (view as bug list)
Depends on: 23608
Blocks:
  Show dependency treegraph
 
Reported: 2008-12-10 10:34 UTC by Michael Thayer
Modified: 2015-01-07 11:11 UTC (History)
7 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2010-02-24 13:59:45


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Thayer 2008-12-10 10:34:29 UTC
This is a request to drop the signed/unsigned comparison warning if the compiler can clearly see that the unsigned value is positive.  E.g. in the following case:

if (foo >= 0 && foo < sizeof(bar)) ...

This would avoid the need for a typecase.  Please correct me if this is still not safe for other reasons.
Comment 1 Manuel López-Ibáñez 2010-02-24 13:16:05 UTC
We need a complete testcase.
Comment 2 Manuel López-Ibáñez 2010-02-24 13:16:56 UTC
We need a complete testcase. See http://gcc.gnu.org/bugs
Comment 3 Paolo Carlini 2010-02-24 13:59:45 UTC
I think submitter ask for something like this:

int f(int foo, double bar)
{
  if (foo >= 0 && foo < sizeof(bar))
    return 1;
  return foo;
}

to not warn with -Wsign-compare.
Comment 4 Manuel López-Ibáñez 2010-02-24 14:03:51 UTC
(In reply to comment #3)
> I think submitter ask for something like this:
[snip]
> to not warn with -Wsign-compare.

That would be nice, but it would require moving the warning to the middle-end or some form of dataflow in the front-end. And no one is working on either, so don't hold your breath.
 

Comment 5 Michael Thayer 2010-02-24 14:07:07 UTC
Comment 3 describes what I meant.  And re comment 4, it is a "would be nice to have", obviously if it is too much pain to do then such is life.  Thanks in any case.
Comment 6 Manuel López-Ibáñez 2010-02-24 14:11:06 UTC
Fixing this is even more unlikely than fixing PR 23608, since the latter only asks for constant propagation, but this one requires value range propagation.
Comment 7 Manuel López-Ibáñez 2010-02-24 14:12:57 UTC
(In reply to comment #5)
> Comment 3 describes what I meant.  And re comment 4, it is a "would be nice to
> have", obviously if it is too much pain to do then such is life.  Thanks in any
> case.

Please, do not understand me wrong. I totally agree it would be nice to have and we should leave this open in case someone steps up to the task (patches welcome, as they say). I was just commenting that you should not expect a quick fix because it is more complex than it seems. And thanks for the report.
Comment 8 Michael Thayer 2010-02-24 14:16:55 UTC
Of course.  I asked this without knowing much about compiler internals, but I do have experience of users asking for "little" features which would involve somewhat more work than they would like to think :)
Comment 9 Manuel López-Ibáñez 2011-06-15 18:15:44 UTC
*** Bug 49426 has been marked as a duplicate of this bug. ***
Comment 10 David Stone 2012-03-24 06:52:04 UTC
Simple self-contained test case:


unsigned f (int x) {
	return (x >= 0) ? x : -x;
}
int main () {
    return 0;
}


It seems like we could at least add a simple improvement that just checks for simple comparisons to 0. That probably catches most code (I often do one set of calculations for non-negative values, and another set for negative values) and avoids a lot of problems of trying to track complex calculations for the signedness of the result. I wouldn't be perfect, but it would be better than it currently is.

Being able to detect cases like this is also an optimization opportunity, because division with positive integers is simpler than division with negative integers, for example. We could reuse code that detects that optimization opportunity for this warning.
Comment 11 Manuel López-Ibáñez 2012-04-16 10:43:44 UTC
(In reply to comment #10)
> It seems like we could at least add a simple improvement that just checks for
> simple comparisons to 0. That probably catches most code (I often do one set of

You are welcome to try: http://gcc.gnu.org/contribute.html
Comment 12 Manuel López-Ibáñez 2013-11-25 23:04:27 UTC
*** Bug 59293 has been marked as a duplicate of this bug. ***
Comment 13 Manuel López-Ibáñez 2015-01-07 11:11:44 UTC
*** Bug 64518 has been marked as a duplicate of this bug. ***