Article View: gnu.gcc.bug
Article #56GCC 2.3.1 sometimes reports long long overflow when none has occurred
From: Paul Eggert
Date: Tue, 03 Nov 1992 02:56
Date: Tue, 03 Nov 1992 02:56
203 lines
6979 bytes
6979 bytes
Given the program
long long x = 9223372036854775808ULL - 1;
GCC 2.3.1 (sparc, SunOS 4.1.2) reports
t.c:1: warning: overflow in constant expression
even though no overflow has occurred. The problem is that GCC checks for
signed overflow, even though the arithmetic in question is unsigned and
therefore cannot overflow.
Here is a patch, relative to the patches I've already sent today.
Tue Nov 3 02:49:59 1992 Paul Eggert (eggert@twinsun.com)
* fold-const.c (force_fit_type, const_binop, fold_convert, fold):
Ignore signed overflows when doing unsigned arithmetic.
===================================================================
RCS file: fold-const.c,v
retrieving revision 2.3.1.2
retrieving revision 2.3.1.3
diff -c -r2.3.1.2 -r2.3.1.3
*** fold-const.c 1992/11/03 00:52:08 2.3.1.2
--- fold-const.c 1992/11/03 02:49:59 2.3.1.3
***************
*** 117,127 ****
/* Make the integer constant T valid for its type
by setting to 0 or 1 all the bits in the constant
that don't belong in the type.
! Yield nonzero if an arithmetic overflow occurs. */
static int
! force_fit_type (t)
tree t;
{
HOST_WIDE_INT low = TREE_INT_CST_LOW (t), high = TREE_INT_CST_HIGH (t);
register int prec = TYPE_PRECISION (TREE_TYPE (t));
--- 117,129 ----
/* Make the integer constant T valid for its type
by setting to 0 or 1 all the bits in the constant
that don't belong in the type.
! Yield nonzero if a signed overflow occurs.
! If OVERFLOW is nonzero, a signed overflow has already occurred. */
static int
! force_fit_type (t, overflow)
tree t;
+ int overflow;
{
HOST_WIDE_INT low = TREE_INT_CST_LOW (t), high = TREE_INT_CST_HIGH (t);
register int prec = TYPE_PRECISION (TREE_TYPE (t));
***************
*** 171,178 ****
}
}
! /* Yield nonzero if overflow occurred. */
! return ((low ^ TREE_INT_CST_LOW (t)) | (high ^ TREE_INT_CST_HIGH (t))) != 0;
}
/* Add two doubleword integers with doubleword result.
--- 173,182 ----
}
}
! /* Yield nonzero if signed overflow occurred. */
! return
! (overflow
! | (low ^ TREE_INT_CST_LOW (t)) | (high ^ TREE_INT_CST_HIGH (t)) != 0);
}
/* Add two doubleword integers with doubleword result.
***************
*** 1055,1063 ****
HOST_WIDE_INT garbagel, garbageh;
register tree t;
int uns = TREE_UNSIGNED (TREE_TYPE (arg1));
! /* Propagate overflow flags from operands; also record new overflow. */
! int overflow
! = TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2);
switch (code)
{
--- 1059,1065 ----
HOST_WIDE_INT garbagel, garbageh;
register tree t;
int uns = TREE_UNSIGNED (TREE_TYPE (arg1));
! int overflow = 0;
switch (code)
{
***************
*** 1276,1282 ****
}
got_it:
TREE_TYPE (t) = TREE_TYPE (arg1);
! TREE_CONSTANT_OVERFLOW (t) = overflow | force_fit_type (t);
return t;
}
#if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
--- 1278,1286 ----
}
got_it:
TREE_TYPE (t) = TREE_TYPE (arg1);
! TREE_CONSTANT_OVERFLOW (t)
! = (force_fit_type (t, overflow)
! | TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2));
return t;
}
#if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
***************
*** 1487,1493 ****
if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (t)))
TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg1);
TREE_TYPE (t) = type;
! force_fit_type (t);
}
#if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
else if (TREE_CODE (arg1) == REAL_CST)
--- 1491,1497 ----
if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (t)))
TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg1);
TREE_TYPE (t) = type;
! force_fit_type (t, 0);
}
#if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
else if (TREE_CODE (arg1) == REAL_CST)
***************
*** 1543,1549 ****
}
#endif
TREE_TYPE (t) = type;
! force_fit_type (t);
}
#endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
TREE_TYPE (t) = type;
--- 1547,1553 ----
}
#endif
TREE_TYPE (t) = type;
! force_fit_type (t, 0);
}
#endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
TREE_TYPE (t) = type;
***************
*** 3232,3240 ****
t = build_int_2 (low, high);
TREE_TYPE (t) = type;
TREE_CONSTANT_OVERFLOW (t)
! = (overflow
! | TREE_CONSTANT_OVERFLOW (arg0)
! | force_fit_type (t));
}
else if (TREE_CODE (arg0) == REAL_CST)
t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
--- 3236,3243 ----
t = build_int_2 (low, high);
TREE_TYPE (t) = type;
TREE_CONSTANT_OVERFLOW (t)
! = (TREE_CONSTANT_OVERFLOW (arg0)
! | force_fit_type (t, overflow));
}
else if (TREE_CODE (arg0) == REAL_CST)
t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
***************
*** 3265,3273 ****
t = build_int_2 (low, high);
TREE_TYPE (t) = type;
TREE_CONSTANT_OVERFLOW (t)
! = (overflow
! | TREE_CONSTANT_OVERFLOW (arg0)
! | force_fit_type (t));
}
}
else if (TREE_CODE (arg0) == REAL_CST)
--- 3268,3275 ----
t = build_int_2 (low, high);
TREE_TYPE (t) = type;
TREE_CONSTANT_OVERFLOW (t)
! = (TREE_CONSTANT_OVERFLOW (arg0)
! | force_fit_type (t, overflow));
}
}
else if (TREE_CODE (arg0) == REAL_CST)
***************
*** 3289,3295 ****
t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
~ TREE_INT_CST_HIGH (arg0));
TREE_TYPE (t) = type;
! force_fit_type (t);
TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg0);
}
else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
--- 3291,3297 ----
t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
~ TREE_INT_CST_HIGH (arg0));
TREE_TYPE (t) = type;
! force_fit_type (t, 0);
TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg0);
}
else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
Technical Details
Message-ID:
<9211030256.AA05593@farside.twinsun.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!portal!lll-winken!iggy.GW.Vitalink.COM!cs.widener.edu!umn.edu!spool.mu.edu!caen!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!cis.ohio-state.edu!twinsun.COM!eggertShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!portal!lll-winken!iggy.GW.Vitalink.COM!cs.widener.edu!umn.edu!spool.mu.edu!caen!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!cis.ohio-state.edu!twinsun.COM!eggert From: eggert@twinsun.COM (Paul Eggert) Newsgroups: gnu.gcc.bug Subject: GCC 2.3.1 sometimes reports long long overflow when none has occurred Message-ID: <9211030256.AA05593@farside.twinsun.com> Date: 3 Nov 92 02:56:37 GMT Sender: gnulists@ai.mit.edu Distribution: gnu Organization: GNUs Not Usenet Lines: 203 Approved: bug-gcc@prep.ai.mit.edu