This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix bug in int_fits_type_p


The following patch fixes an oversight in int_fits_type_p that was
uncovered by my testing of a patch to resolve PR 19100.  Following
discussion with the Ada folks on the gcc list, I preferred Olivier's
suggestion that we add an additional test for TYPE_PRECISION, which
should also help performance.

The following patch has been tested on i686-pc-linux-gnu with a full
"make bootstrap", all default languages, and regression tested with
a top-level "make -k check" with no new failures.

Ok for mainline?  Best wishes for the season...



2004-12-24  Roger Sayle  <roger@eyesopen.com>
	    Olivier Hainque  <hainque@act-europe.fr>

	* tree.c (int_fits_type_p): A narrower type always fits in a
	wider one, except for negative values into unsigned types.


Index: tree.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/tree.c,v
retrieving revision 1.458
diff -c -3 -p -r1.458 tree.c
*** tree.c	23 Dec 2004 16:21:31 -0000	1.458
--- tree.c	24 Dec 2004 21:52:15 -0000
*************** int_fits_type_p (tree c, tree type)
*** 4879,4888 ****
    /* Perform some generic filtering first, which may allow making a decision
       even if the bounds are not constant.  First, negative integers never fit
       in unsigned types, */
!   if ((TYPE_UNSIGNED (type) && tree_int_cst_sgn (c) < 0)
!       /* Also, unsigned integers with top bit set never fit signed types.  */
!       || (! TYPE_UNSIGNED (type)
! 	  && TYPE_UNSIGNED (TREE_TYPE (c)) && tree_int_cst_msb (c)))
      return 0;

    /* If at least one bound of the type is a constant integer, we can check
--- 4879,4895 ----
    /* Perform some generic filtering first, which may allow making a decision
       even if the bounds are not constant.  First, negative integers never fit
       in unsigned types, */
!   if (TYPE_UNSIGNED (type) && tree_int_cst_sgn (c) < 0)
!     return 0;
!
!   /* Second, narrower types always fit in wider ones.  */
!   if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
!     return 1;
!
!   /* Third, unsigned integers with top bit set never fit signed types.  */
!   if (! TYPE_UNSIGNED (type)
!       && TYPE_UNSIGNED (TREE_TYPE (c))
!       && tree_int_cst_msb (c))
      return 0;

    /* If at least one bound of the type is a constant integer, we can check


Roger
--
Roger Sayle,                         E-mail: roger@eyesopen.com
OpenEye Scientific Software,         WWW: http://www.eyesopen.com/
Suite 1107, 3600 Cerrillos Road,     Tel: (+1) 505-473-7385
Santa Fe, New Mexico, 87507.         Fax: (+1) 505-473-0833


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]