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]: remove redundant code in expand_builtin_mempcpy


This patch addresses expand_builtin_mempcpy by moving the tree munging
code into the folder and deleting some duplicated code.  In the
process, I had to add `type' and `endp' arguments to the folder.

Bootstrapped on sparc-sun-solaris2.9 (minus libgcj due to PR17265), no
regressions.

Ok for mainline?

		Thanks,
		--Kaveh


2004-10-07  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>

	* builtins.c (expand_builtin_mempcpy): Move tree handling code to
	fold_builtin_mempcpy.  Accept a type parameter.  Delete duplicate
	code.
	(expand_builtin_strcat): Accept a type parameter.
	(fold_builtin_mempcpy): Accept a type and endp parameter.

diff -rcp orig/egcc-CVS20041006/gcc/builtins.c egcc-CVS20041006/gcc/builtins.c
*** orig/egcc-CVS20041006/gcc/builtins.c	Wed Oct  6 09:35:47 2004
--- egcc-CVS20041006/gcc/builtins.c	Thu Oct  7 11:42:37 2004
*************** static rtx expand_builtin_memcmp (tree, 
*** 107,118 ****
  static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode);
  static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
  static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
! static rtx expand_builtin_strcat (tree, rtx, enum machine_mode);
  static rtx expand_builtin_strncat (tree, rtx, enum machine_mode);
  static rtx expand_builtin_strspn (tree, rtx, enum machine_mode);
  static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode);
  static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode);
! static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode, int);
  static rtx expand_builtin_memmove (tree, tree, rtx, enum machine_mode);
  static rtx expand_builtin_bcopy (tree, tree);
  static rtx expand_builtin_strcpy (tree, rtx, enum machine_mode);
--- 107,118 ----
  static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode);
  static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
  static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
! static rtx expand_builtin_strcat (tree, tree, rtx, enum machine_mode);
  static rtx expand_builtin_strncat (tree, rtx, enum machine_mode);
  static rtx expand_builtin_strspn (tree, rtx, enum machine_mode);
  static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode);
  static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode);
! static rtx expand_builtin_mempcpy (tree, tree, rtx, enum machine_mode, int);
  static rtx expand_builtin_memmove (tree, tree, rtx, enum machine_mode);
  static rtx expand_builtin_bcopy (tree, tree);
  static rtx expand_builtin_strcpy (tree, rtx, enum machine_mode);
*************** static tree fold_builtin_ceil (tree);
*** 162,168 ****
  static tree fold_builtin_round (tree);
  static tree fold_builtin_bitop (tree);
  static tree fold_builtin_memcpy (tree);
! static tree fold_builtin_mempcpy (tree);
  static tree fold_builtin_memmove (tree, tree);
  static tree fold_builtin_strchr (tree);
  static tree fold_builtin_memcmp (tree);
--- 162,168 ----
  static tree fold_builtin_round (tree);
  static tree fold_builtin_bitop (tree);
  static tree fold_builtin_memcpy (tree);
! static tree fold_builtin_mempcpy (tree, tree, int);
  static tree fold_builtin_memmove (tree, tree);
  static tree fold_builtin_strchr (tree);
  static tree fold_builtin_memcmp (tree);
*************** expand_builtin_memcpy (tree exp, rtx tar
*** 2621,2627 ****
     stpcpy.  */
  
  static rtx
! expand_builtin_mempcpy (tree arglist, rtx target, enum machine_mode mode,
  			int endp)
  {
    if (!validate_arglist (arglist,
--- 2621,2627 ----
     stpcpy.  */
  
  static rtx
! expand_builtin_mempcpy (tree arglist, tree type, rtx target, enum machine_mode mode,
  			int endp)
  {
    if (!validate_arglist (arglist,
*************** expand_builtin_mempcpy (tree arglist, rt
*** 2648,2695 ****
        unsigned int dest_align
  	= get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
        rtx dest_mem, src_mem, len_rtx;
  
!       /* If DEST is not a pointer type, call the normal function.  */
!       if (dest_align == 0)
  	return 0;
  
-       /* If SRC and DEST are the same (and not volatile), do nothing.  */
-       if (operand_equal_p (src, dest, 0))
- 	{
- 	  tree expr;
- 
- 	  if (endp == 0)
- 	    {
- 	      /* Evaluate and ignore LEN in case it has side-effects.  */
- 	      expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
- 	      return expand_expr (dest, target, mode, EXPAND_NORMAL);
- 	    }
- 
- 	  if (endp == 2)
- 	    len = fold (build2 (MINUS_EXPR, TREE_TYPE (len), len,
- 				integer_one_node));
- 	  len = fold_convert (TREE_TYPE (dest), len);
- 	  expr = fold (build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len));
- 	  return expand_expr (expr, target, mode, EXPAND_NORMAL);
- 	}
- 
        /* If LEN is not constant, call the normal function.  */
        if (! host_integerp (len, 1))
  	return 0;
  
-       /* If the LEN parameter is zero, return DEST.  */
-       if (tree_low_cst (len, 1) == 0)
- 	{
- 	  /* Evaluate and ignore SRC in case it has side-effects.  */
- 	  expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
- 	  return expand_expr (dest, target, mode, EXPAND_NORMAL);
- 	}
- 
-       /* If either SRC is not a pointer type, don't do this
-          operation in-line.  */
-       if (src_align == 0)
- 	return 0;
- 
        len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
        src_str = c_getstr (src);
  
--- 2648,2667 ----
        unsigned int dest_align
  	= get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
        rtx dest_mem, src_mem, len_rtx;
+       tree result = fold_builtin_mempcpy (arglist, type, endp);
  
!       if (result)
! 	return expand_expr (result, target, mode, EXPAND_NORMAL);
!       
!       /* If either SRC or DEST is not a pointer type, don't do this
!          operation in-line.  */
!       if (dest_align == 0 || src_align == 0)
  	return 0;
  
        /* If LEN is not constant, call the normal function.  */
        if (! host_integerp (len, 1))
  	return 0;
  
        len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
        src_str = c_getstr (src);
  
*************** expand_builtin_memmove (tree arglist, tr
*** 2779,2785 ****
  	 it is ok to use memcpy as well.  */
        if (integer_onep (len))
          {
! 	  rtx ret = expand_builtin_mempcpy (arglist, target, mode,
  					    /*endp=*/0);
  	  if (ret)
  	    return ret;
--- 2751,2757 ----
  	 it is ok to use memcpy as well.  */
        if (integer_onep (len))
          {
! 	  rtx ret = expand_builtin_mempcpy (arglist, type, target, mode,
  					    /*endp=*/0);
  	  if (ret)
  	    return ret;
*************** expand_builtin_stpcpy (tree exp, rtx tar
*** 2950,2956 ****
        narglist = build_tree_list (NULL_TREE, lenp1);
        narglist = tree_cons (NULL_TREE, src, narglist);
        narglist = tree_cons (NULL_TREE, dst, narglist);
!       ret = expand_builtin_mempcpy (narglist, target, mode, /*endp=*/2);
  
        if (ret)
  	return ret;
--- 2922,2929 ----
        narglist = build_tree_list (NULL_TREE, lenp1);
        narglist = tree_cons (NULL_TREE, src, narglist);
        narglist = tree_cons (NULL_TREE, dst, narglist);
!       ret = expand_builtin_mempcpy (narglist, TREE_TYPE (exp),
! 				    target, mode, /*endp=*/2);
  
        if (ret)
  	return ret;
*************** expand_builtin_strncmp (tree exp, rtx ta
*** 3610,3616 ****
     otherwise try to get the result in TARGET, if convenient.  */
  
  static rtx
! expand_builtin_strcat (tree arglist, rtx target, enum machine_mode mode)
  {
    if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
      return 0;
--- 3583,3589 ----
     otherwise try to get the result in TARGET, if convenient.  */
  
  static rtx
! expand_builtin_strcat (tree arglist, tree type, rtx target, enum machine_mode mode)
  {
    if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
      return 0;
*************** expand_builtin_strcat (tree arglist, rtx
*** 3660,3666 ****
                   pass in a target of zero, it should never actually be
                   used.  If this was successful return the original
                   dst, not the result of mempcpy.  */
! 	      if (expand_builtin_mempcpy (arglist, /*target=*/0, mode, /*endp=*/0))
  		return expand_expr (dst, target, mode, EXPAND_NORMAL);
  	      else
  		return 0;
--- 3633,3639 ----
                   pass in a target of zero, it should never actually be
                   used.  If this was successful return the original
                   dst, not the result of mempcpy.  */
! 	      if (expand_builtin_mempcpy (arglist, type, /*target=*/0, mode, /*endp=*/0))
  		return expand_expr (dst, target, mode, EXPAND_NORMAL);
  	      else
  		return 0;
*************** expand_builtin (tree exp, rtx target, rt
*** 5403,5409 ****
        break;
  
      case BUILT_IN_STRCAT:
!       target = expand_builtin_strcat (arglist, target, mode);
        if (target)
  	return target;
        break;
--- 5376,5382 ----
        break;
  
      case BUILT_IN_STRCAT:
!       target = expand_builtin_strcat (arglist, TREE_TYPE (exp), target, mode);
        if (target)
  	return target;
        break;
*************** expand_builtin (tree exp, rtx target, rt
*** 5459,5465 ****
        break;
  
      case BUILT_IN_MEMPCPY:
!       target = expand_builtin_mempcpy (arglist, target, mode, /*endp=*/ 1);
        if (target)
  	return target;
        break;
--- 5432,5438 ----
        break;
  
      case BUILT_IN_MEMPCPY:
!       target = expand_builtin_mempcpy (arglist, TREE_TYPE (exp), target, mode, /*endp=*/ 1);
        if (target)
  	return target;
        break;
*************** fold_builtin_memcpy (tree exp)
*** 6991,7021 ****
     NULL_TREE if no simplification can be made.  */
  
  static tree
! fold_builtin_mempcpy (tree exp)
  {
!   tree arglist = TREE_OPERAND (exp, 1);
!   tree dest, src, len;
! 
!   if (!validate_arglist (arglist,
! 			 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
!     return 0;
  
!   dest = TREE_VALUE (arglist);
!   src = TREE_VALUE (TREE_CHAIN (arglist));
!   len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
  
!   /* If the LEN parameter is zero, return DEST.  */
!   if (integer_zerop (len))
!     return omit_one_operand (TREE_TYPE (exp), dest, src);
  
!   /* If SRC and DEST are the same (and not volatile), return DEST+LEN.  */
!   if (operand_equal_p (src, dest, 0))
!     {
!       tree temp = fold_convert (TREE_TYPE (dest), len);
!       temp = fold (build2 (PLUS_EXPR, TREE_TYPE (dest), dest, temp));
!       return fold_convert (TREE_TYPE (exp), temp);
      }
- 
    return 0;
  }
  
--- 6964,6997 ----
     NULL_TREE if no simplification can be made.  */
  
  static tree
! fold_builtin_mempcpy (tree arglist, tree type, int endp)
  {
!   if (validate_arglist (arglist,
! 			POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
!     {
!       tree dest = TREE_VALUE (arglist);
!       tree src = TREE_VALUE (TREE_CHAIN (arglist));
!       tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
  
!       /* If the LEN parameter is zero, return DEST.  */
!       if (integer_zerop (len))
! 	return omit_one_operand (type, dest, src);
  
!       /* If SRC and DEST are the same (and not volatile), return DEST+LEN.  */
!       if (operand_equal_p (src, dest, 0))
!         {
! 	  if (endp == 0)
! 	    return omit_one_operand (type, dest, len);
  
! 	  if (endp == 2)
! 	    len = fold (build2 (MINUS_EXPR, TREE_TYPE (len), len,
! 				ssize_int (1)));
!       
! 	  len = fold_convert (TREE_TYPE (dest), len);
! 	  len = fold (build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len));
! 	  return fold_convert (type, len);
! 	}
      }
    return 0;
  }
  
*************** fold_builtin_1 (tree exp, bool ignore)
*** 7984,7990 ****
        return fold_builtin_memcpy (exp);
  
      case BUILT_IN_MEMPCPY:
!       return fold_builtin_mempcpy (exp);
  
      case BUILT_IN_MEMMOVE:
        return fold_builtin_memmove (arglist, type);
--- 7960,7966 ----
        return fold_builtin_memcpy (exp);
  
      case BUILT_IN_MEMPCPY:
!       return fold_builtin_mempcpy (arglist, type, /*endp=*/1);
  
      case BUILT_IN_MEMMOVE:
        return fold_builtin_memmove (arglist, type);


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