User account creation filtered due to spam.

Bug 36137 - gcc can't do math
Summary: gcc can't do math
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.2.3
: P3 normal
Target Milestone: 4.3.1
Assignee: Jakub Jelinek
URL: http://gcc.gnu.org/ml/gcc-patches/200...
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2008-05-05 14:16 UTC by Benjamin Otte
Modified: 2008-05-07 19:31 UTC (History)
4 users (show)

See Also:
Host: i486-linux-gnu
Target: i486-linux-gnu
Build: i486-linux-gnu
Known to work: 4.3.1
Known to fail: 4.2.3
Last reconfirmed: 2008-05-06 10:50:49


Attachments
testcase (181 bytes, text/x-csrc)
2008-05-05 14:17 UTC, Benjamin Otte
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Benjamin Otte 2008-05-05 14:16:32 UTC
The attached program prints (unsigned) -1 as opposed to 1.
This doesn't happen if intermediate variables are used somewhere in the process.

I tested gcc 3.4, 4.2.3 (on both x86 and x86-64) with -O0 and -O2, all ended up with -1.
suncc printed 1.
Comment 1 Benjamin Otte 2008-05-05 14:17:24 UTC
Created attachment 15581 [details]
testcase
Comment 2 Tom Tromey 2008-05-05 19:10:16 UTC
test.c.t02.original says:

  u = MAX_EXPR <(unsigned int) i, 1>;

... but this is wrong.
Comment 3 Tom Tromey 2008-05-05 22:27:59 UTC
fold_binary sees both MAX_EXPRs and reassociates.
(I think this is why it works with intermediate variables.)

Then associate_trees creates a new MAX_EXPR, converting each
argument to unsigned:

  return fold_build2 (code, type, fold_convert (type, t1),
		      fold_convert (type, t2));

So, this is where the error enters.
I'm not sure where this is best fixed though.
Comment 4 Jakub Jelinek 2008-05-07 07:41:06 UTC
Subject: Bug 36137

Author: jakub
Date: Wed May  7 07:40:01 2008
New Revision: 135028

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=135028
Log:
	PR middle-end/36137
	* fold-const.c (fold_binary): Use STRIP_SIGN_NOPS instead of
	STRIP_NOPS on arguments even for MIN_EXPR and MAX_EXPR.

	* gcc.c-torture/execute/20080506-1.c: New test.

Added:
    trunk/gcc/testsuite/gcc.c-torture/execute/20080506-1.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/fold-const.c
    trunk/gcc/testsuite/ChangeLog

Comment 5 Jakub Jelinek 2008-05-07 07:59:18 UTC
Subject: Bug 36137

Author: jakub
Date: Wed May  7 07:58:33 2008
New Revision: 135031

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=135031
Log:
	PR middle-end/36137
	* fold-const.c (fold_binary): Use STRIP_SIGN_NOPS instead of
	STRIP_NOPS on arguments even for MIN_EXPR and MAX_EXPR.

	* gcc.c-torture/execute/20080506-1.c: New test.

Added:
    branches/gcc-4_3-branch/gcc/testsuite/gcc.c-torture/execute/20080506-1.c
Modified:
    branches/gcc-4_3-branch/gcc/ChangeLog
    branches/gcc-4_3-branch/gcc/fold-const.c
    branches/gcc-4_3-branch/gcc/testsuite/ChangeLog

Comment 6 Jakub Jelinek 2008-05-07 08:06:49 UTC
Fixed in 4.3 and on the trunk.