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] Constant fold RTL "long" division


The comment in simplify-rtx.c suggests that it isn't worth the effort to
include tree.h just to be able to call div_and_round_double.  The good
news is that since this comment was originally written, we now do include
tree.h in simplify-rtx.c, so there's no reason *not* to constant fold
CONST_DOUBLE divisions and remainders during RTL simplification.


With this patch (and the earlier CSE fix), GCC can now evaluate the
following code at compile-time (on i686-pc-linux-gnu):

long long foo()
{
  long long y = 87654321;
  long long x = (long long)12345678 * (long long)87654321;
  return x / y;
}


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

Committed to mainline CVS.



2004-04-04  Roger Sayle  <roger@eyesopen.com>

	* simplify-rtx.c (simplify_binary_operation): Constant fold
	DIV, MOD, UDIV and UMOD using div_and_round_double.


Index: simplify-rtx.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/simplify-rtx.c,v
retrieving revision 1.184
diff -c -3 -p -r1.184 simplify-rtx.c
*** simplify-rtx.c	20 Mar 2004 04:52:57 -0000	1.184
--- simplify-rtx.c	3 Apr 2004 22:13:54 -0000
*************** simplify_binary_operation (enum rtx_code
*** 1285,1292 ****
        && (GET_CODE (trueop1) == CONST_DOUBLE
  	  || GET_CODE (trueop1) == CONST_INT))
      {
!       unsigned HOST_WIDE_INT l1, l2, lv;
!       HOST_WIDE_INT h1, h2, hv;

        if (GET_CODE (trueop0) == CONST_DOUBLE)
  	l1 = CONST_DOUBLE_LOW (trueop0), h1 = CONST_DOUBLE_HIGH (trueop0);
--- 1285,1292 ----
        && (GET_CODE (trueop1) == CONST_DOUBLE
  	  || GET_CODE (trueop1) == CONST_INT))
      {
!       unsigned HOST_WIDE_INT l1, l2, lv, lt;
!       HOST_WIDE_INT h1, h2, hv, ht;

        if (GET_CODE (trueop0) == CONST_DOUBLE)
  	l1 = CONST_DOUBLE_LOW (trueop0), h1 = CONST_DOUBLE_HIGH (trueop0);
*************** simplify_binary_operation (enum rtx_code
*** 1315,1324 ****
  	  mul_double (l1, h1, l2, h2, &lv, &hv);
  	  break;

! 	case DIV:  case MOD:   case UDIV:  case UMOD:
! 	  /* We'd need to include tree.h to do this and it doesn't seem worth
! 	     it.  */
! 	  return 0;

  	case AND:
  	  lv = l1 & l2, hv = h1 & h2;
--- 1315,1343 ----
  	  mul_double (l1, h1, l2, h2, &lv, &hv);
  	  break;

! 	case DIV:
! 	  if (div_and_round_double (TRUNC_DIV_EXPR, 0, l1, h1, l2, h2,
! 				    &lv, &hv, &lt, &ht))
! 	    return 0;
! 	  break;
!
! 	case MOD:
! 	  if (div_and_round_double (TRUNC_DIV_EXPR, 0, l1, h1, l2, h2,
! 				    &lt, &ht, &lv, &hv))
! 	    return 0;
! 	  break;
!
! 	case UDIV:
! 	  if (div_and_round_double (TRUNC_DIV_EXPR, 1, l1, h1, l2, h2,
! 				    &lv, &hv, &lt, &ht))
! 	    return 0;
! 	  break;
!
! 	case UMOD:
! 	  if (div_and_round_double (TRUNC_DIV_EXPR, 1, l1, h1, l2, h2,
! 				    &lt, &ht, &lv, &hv))
! 	    return 0;
! 	  break;

  	case AND:
  	  lv = l1 & l2, hv = h1 & h2;


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]