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]

[Committed] Remove recursive call from convert_to_integer


The following tiny clean-up removes a potential unbounded recursion
problem from the middle-end's convert_to_integer function.  When
converting a pointer or reference type to an integer, we perform
the conversion in two steps; first to an unsigned integer type of
the width of the pointer, and from there truncate/widen to the
desired destination type.

The potential problem is that the second conversion is implemented
via a recursive call to convert_to_integer rather than directly
building a NOP_EXPR (if required).  With a patch, I'm working on
this recursive call creates problems as the behaviour of
convert_to_integer for casts is to first strip the cast using
get_unwidened and then call convert.  Clearly this can create
problems as the mutual recursion infinitely adds/removes casts.

The following patch both avoids problems and improves performance
by constructing the required NOP_EXPR directly, rather than
recursively invoke convert_to_integer.


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

Committed to mainline CVS.



2005-05-25  Roger Sayle  <roger@eyesopen.com>

	* convert.c (convert_to_integer) <POINTER_TYPE>: Avoid recursive
	call to convert_to_integer by building the NOP_EXPR directly.


Index: convert.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/convert.c,v
retrieving revision 1.62
diff -c -3 -p -r1.62 convert.c
*** convert.c	3 May 2005 08:08:23 -0000	1.62
--- convert.c	23 May 2005 20:50:26 -0000
*************** convert_to_integer (tree type, tree expr
*** 411,423 ****
      case POINTER_TYPE:
      case REFERENCE_TYPE:
        if (integer_zerop (expr))
! 	expr = integer_zero_node;
!       else
! 	expr = fold (build1 (CONVERT_EXPR,
! 			     lang_hooks.types.type_for_size (POINTER_SIZE, 0),
! 			     expr));

!       return convert_to_integer (type, expr);

      case INTEGER_TYPE:
      case ENUMERAL_TYPE:
--- 411,424 ----
      case POINTER_TYPE:
      case REFERENCE_TYPE:
        if (integer_zerop (expr))
! 	return build_int_cst (type, 0);

!       /* Convert to an unsigned integer of the correct width first,
! 	 and from there widen/truncate to the required type.  */
!       expr = fold_build1 (CONVERT_EXPR,
! 			  lang_hooks.types.type_for_size (POINTER_SIZE, 0),
! 			  expr);
!       return fold_build1 (NOP_EXPR, type, expr);

      case INTEGER_TYPE:
      case ENUMERAL_TYPE:


Roger
--


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