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] Ensure valid RTL from simplify_expand_binop (PR 25662)


	simplify_expand_binop() currently calls simplify_gen_binary() if
the operands are constant.  simplify_gen_binary() always produces a
result, but if simplify_binary_operation() fails, it directly generates
the raw pattern without invoking the corresponding named pattern for the
optab nor checking that the operands satisfy constraints.  This is not
safe behavior for a function that can be called with a user-created
expression, as demonstrated in the PR.

	Ian proposed the first part of the appended patch which modifies
simlify_expand_binop() to invoke simplify_binary_operation() directly and
fall back to expand_binop() if it fails to produce a result.

	While investigating this with Ian, I noticed that
simplify_gen_binary() tries to optimize the order of commutative operands.
simplify_gen_binary() calls simplify_binary_operation(), which also tries
to optimize the order or commutative operands.  The second part of this
patch avoids the duplication by moving the operand swap after trying
simplify_binary_operation().

Bootstrapped and regression tested on powerpc-ibm-aix5.2.0.0.

:ADDPATCH middle-end:

2005-01-07  Ian Lance Taylor  <ian@airs.com>
	    David Edelsohn  <edelsohn@gnu.org>

	PR rtl-optimization/25662
	* optabs.c (simplify_expand_binop): Use simplify_binary_operation
	for constant operands instead of simplify_gen_binary.
	* simplify-rtx.c (simplify_gen_binary): Swap commutative operands
	after trying simplify_binary_operation

Index: optabs.c
===================================================================
*** optabs.c	(revision 109448)
--- optabs.c	(working copy)
*************** simplify_expand_binop (enum machine_mode
*** 427,435 ****
  		       enum optab_methods methods)
  {
    if (CONSTANT_P (op0) && CONSTANT_P (op1))
!     return simplify_gen_binary (binoptab->code, mode, op0, op1);
!   else
!     return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
  }
  
  /* Like simplify_expand_binop, but always put the result in TARGET.
--- 427,440 ----
  		       enum optab_methods methods)
  {
    if (CONSTANT_P (op0) && CONSTANT_P (op1))
!     {
!       rtx x = simplify_binary_operation (binoptab->code, mode, op0, op1);
! 
!       if (x)
! 	return x;
!     }
! 
!   return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
  }
  
  /* Like simplify_expand_binop, but always put the result in TARGET.
Index: simplify-rtx.c
===================================================================
*** simplify-rtx.c	(revision 109448)
--- simplify-rtx.c	(working copy)
*************** simplify_gen_binary (enum rtx_code code,
*** 114,128 ****
  {
    rtx tem;
  
-   /* Put complex operands first and constants second if commutative.  */
-   if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
-       && swap_commutative_operands_p (op0, op1))
-     tem = op0, op0 = op1, op1 = tem;
- 
    /* If this simplifies, do it.  */
    tem = simplify_binary_operation (code, mode, op0, op1);
    if (tem)
      return tem;
  
    return gen_rtx_fmt_ee (code, mode, op0, op1);
  }
--- 114,128 ----
  {
    rtx tem;
  
    /* If this simplifies, do it.  */
    tem = simplify_binary_operation (code, mode, op0, op1);
    if (tem)
      return tem;
+ 
+   /* Put complex operands first and constants second if commutative.  */
+   if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
+       && swap_commutative_operands_p (op0, op1))
+     tem = op0, op0 = op1, op1 = tem;
  
    return gen_rtx_fmt_ee (code, mode, op0, op1);
  }


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