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 COMMITTED: Small cleanup in convert_to_integer


While looking at some code in convert_to_integer in convert.c, I
noticed a missing break statement after the NEGATE_EXPR/BIT_NOT_EXPR
case.  The code clearly should not fall through--the next case will do
the wrong thing.  I tracked the code back, and to my surprise the
break statement is missing, and the same error is possible, in the
very earliest version of this code that I could find, in gcc-1.26,
released in 1988.  Clearly this code must not fall through very often.

In fact it could only fall through when an enum type E is smaller than
an integer type I, but the integer type corresponding to E, according
to type_for_size, is the same size as I.  For example, it would fall
through if E were 12 bits and I were 16 bits.  As far as I know this
is not possible in C or C++.  I believe it is also not possible in
Ada, because it appears that type_for_size will always return an
integer type of the requested size.  So I don't know if it is ever
possible.

Furthermore, it actually can never fall through in current gcc even in
principle, because it will never see an enum type, due to this patch:

Mon Jul 17 06:41:19 1995  Richard Kenner  (kenner@vlsi1.ultra.nyu.edu)

	* convert.c (convert_to_integer): If TYPE is a enumeral type or
	if its precision is not the same as the size of its mode,
	convert in two steps.

So I just checked in a patch to remove two conditionals which will
always be false and true, respectively.  This makes it clear that
there is no need for the break statement.  Tested with bootstrap and
testsuite run on i686-pc-linux-gnu.

There is a separate issue with this code, which is that it is probably
an unwise transformation to make on a RISC processor when dealing with
a type smaller than word size.  Where i is int, this code transforms
    (short) - i
to
    - (short) i
That transformation may be a good idea on i386 or m68k, but it is
unlikely to be useful on any RISC chip--it will just lead to useless
truncations and sign extensions which may or may not be cleaned up in
later passes.  However addressing that is not appropriate in stage 3.

Ian


2005-09-24  Ian Lance Taylor  <ian@airs.com>

	* convert.c (convert_to_integer): Don't test for ENUMERAL_TYPE in
	NEGATE_EXPR/BIT_NOT_EXPR case.


Index: convert.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/convert.c,v
retrieving revision 1.68
diff -p -u -r1.68 convert.c
--- convert.c	23 Aug 2005 17:48:37 -0000	1.68
+++ convert.c	25 Sep 2005 00:09:41 -0000
@@ -620,30 +620,18 @@ convert_to_integer (tree type, tree expr
 	  /* This is not correct for ABS_EXPR,
 	     since we must test the sign before truncation.  */
 	  {
-	    tree typex = type;
+	    tree typex;
 
-	    /* Can't do arithmetic in enumeral types
-	       so use an integer type that will hold the values.  */
-	    if (TREE_CODE (typex) == ENUMERAL_TYPE)
-	      typex = lang_hooks.types.type_for_size
-		(TYPE_PRECISION (typex), TYPE_UNSIGNED (typex));
-
-	    /* But now perhaps TYPEX is as wide as INPREC.
-	       In that case, do nothing special here.
-	       (Otherwise would recurse infinitely in convert.  */
-	    if (TYPE_PRECISION (typex) != inprec)
-	      {
-		/* Don't do unsigned arithmetic where signed was wanted,
-		   or vice versa.  */
-		if (TYPE_UNSIGNED (TREE_TYPE (expr)))
-		  typex = lang_hooks.types.unsigned_type (typex);
-		else
-		  typex = lang_hooks.types.signed_type (typex);
-		return convert (type,
-				fold_build1 (ex_form, typex,
-					     convert (typex,
-						      TREE_OPERAND (expr, 0))));
-	      }
+	    /* Don't do unsigned arithmetic where signed was wanted,
+	       or vice versa.  */
+	    if (TYPE_UNSIGNED (TREE_TYPE (expr)))
+	      typex = lang_hooks.types.unsigned_type (type);
+	    else
+	      typex = lang_hooks.types.signed_type (type);
+	    return convert (type,
+			    fold_build1 (ex_form, typex,
+					 convert (typex,
+						  TREE_OPERAND (expr, 0))));
 	  }
 
 	case NOP_EXPR:


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