Bug 22360 - [4.0 Regression] upper_bound_in_type and lower_bound_in_type are buggy
Summary: [4.0 Regression] upper_bound_in_type and lower_bound_in_type are buggy
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.1.0
: P2 normal
Target Milestone: 4.1.1
Assignee: Not yet assigned to anyone
URL: http://gcc.gnu.org/ml/gcc-patches/200...
Keywords: patch, wrong-code
Depends on:
Blocks: 15459 20139
  Show dependency treegraph
 
Reported: 2005-07-08 02:05 UTC by Kazu Hirata
Modified: 2023-12-30 00:26 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.1.0 4.1.1 4.1.2
Known to fail:
Last reconfirmed: 2005-12-27 00:37:50


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kazu Hirata 2005-07-08 02:05:36 UTC
Consider tree.c:upper_bound_in_type

Suppose that outer is ulong64 and that inner is int32.  Then the function
returns 2^32-1, which is wrong.  Since we are widening a signed value, we need
to use a sign extension.  The largest value after casting to ulong64 is actually
2^64-1, which occurs when sign-extending (int32) -1.

Consider tree.c:lower_bound_in_type

Suppose that outer is int32 and that inner is ulong64.  Then the function
returns 0, which is wrong.  Since we are narrowing, the result of a cast
can take all values of int32.  The smallest value after casting to int32 is
-2^31, which occurs when we narrow 2^31.

These functions are used from fold-const.c, but I was unable to come up with a
testcase that exposed this bug.

This bug showed up as a latent bug while I was working on PR20139.

I've got a patch to fix this.
Comment 1 Andrew Pinski 2005-07-08 15:06:22 UTC
Confirmed, this is a latent bug in 4.0.0.  The code was not in 3.4.0 so this is 4.0.0 regression.
Comment 2 Andrew Pinski 2005-07-08 15:07:50 UTC
This also blocks the tree combiner even though I don't see the regression on ppc-darwin.
Comment 3 GCC Commits 2005-07-08 18:49:21 UTC
Subject: Bug 22360

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	kazu@gcc.gnu.org	2005-07-08 18:49:07

Modified files:
	gcc            : ChangeLog tree.c 

Log message:
	PR tree-optimization/22360
	* tree.c (upper_bound_in_type): Fix calculations for casting
	to a non-wider signed type and casting a signed value to a
	wider unsigned type.
	(lower_bound_in_type): Fix calculations for casting to a
	non-wider signed type.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.9384&r2=2.9385
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/tree.c.diff?cvsroot=gcc&r1=1.494&r2=1.495

Comment 4 Kazu Hirata 2005-07-08 18:49:54 UTC
Just checked in a fix.
Comment 5 Andrew Pinski 2005-07-09 15:57:26 UTC
Fixed at least on the mainline.
Comment 6 Kazu Hirata 2006-05-21 09:53:12 UTC
Unassigning myself.
Comment 7 Gabriel Dos Reis 2007-01-18 11:44:35 UTC
Fixed in GCC-4.1.1
not release critcal for GCC-4.0.x