public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [RTL] Canonicalize commutative operations more
@ 2013-03-19 17:14 Marc Glisse
  2013-03-27 10:01 ` Eric Botcazou
  0 siblings, 1 reply; 8+ messages in thread
From: Marc Glisse @ 2013-03-19 17:14 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: TEXT/PLAIN, Size: 1440 bytes --]

Hello,

as explained in the PR, the goal of this patch is to have a more canonical 
form for RTL expressions, so it is easier to model them with a small 
number of patterns.

This patch passes bootstrap+testsuite on x86_64-linux-gnu. Using the 
opposite arbitrary order in compare_commutative_operands_precedence 
(exchange x and y in the line with GET_CODE) passes as well. The 
simplify-rtx bit is because I get an infinite recursion otherwise. 
Surprisingly, that infinite recursion occurs only in var-tracking, and 
only for a single file in bootstrap+testsuite (not the same one depending 
on the arbitrary order). I am not sure that is the best place to break the 
recursion, but it seems to work.

I don't really expect the patch to be accepted as is, but it seems good 
enough to start the conversation.

2013-03-19  Marc Glisse  <marc.glisse@inria.fr>

 	PR rtl-optimization/55611
 	* rtl.h (commutative_operand_precedence): Remove.
 	(compare_commutative_operands_precedence): Declare.
 	* optabs.c (swap_commutative_operands_with_target): Use
 	compare_commutative_operands_precedence.
 	* simplify-rtx.c (simplify_associative_operation): Break the
 	recursion cycle.
 	(simplify_plus_minus_op_data_cmp): Use
 	compare_commutative_operands_precedence.
 	* rtlanal.c (commutative_operand_precedence): Make static.
 	(compare_commutative_operands_precedence): New function.
 	(swap_commutative_operands_p): Use it.

-- 
Marc Glisse

[-- Attachment #2: Type: TEXT/PLAIN, Size: 7047 bytes --]

Index: rtlanal.c
===================================================================
--- rtlanal.c	(revision 196633)
+++ rtlanal.c	(working copy)
@@ -3076,21 +3076,21 @@ regno_use_in (unsigned int regno, rtx x)
 
   return NULL_RTX;
 }
 
 /* Return a value indicating whether OP, an operand of a commutative
    operation, is preferred as the first or second operand.  The higher
    the value, the stronger the preference for being the first operand.
    We use negative values to indicate a preference for the first operand
    and positive values for the second operand.  */
 
-int
+static int
 commutative_operand_precedence (rtx op)
 {
   enum rtx_code code = GET_CODE (op);
 
   /* Constants always come the second operand.  Prefer "nice" constants.  */
   if (code == CONST_INT)
     return -8;
   if (code == CONST_DOUBLE)
     return -7;
   if (code == CONST_FIXED)
@@ -3138,28 +3138,43 @@ commutative_operand_precedence (rtx op)
     case RTX_UNARY:
       /* Then prefer NEG and NOT.  */
       if (code == NEG || code == NOT)
         return 1;
 
     default:
       return 0;
     }
 }
 
+/* Return < 0 if it is necessary to swap operands of commutative operation
+   in order to canonicalize expression, > 0 if it is wrong to swap the
+   operands, and 0 if we don't know.  */
+
+int
+compare_commutative_operands_precedence (rtx x, rtx y)
+{
+  int px = commutative_operand_precedence (x);
+  int py = commutative_operand_precedence (y);
+  if (px != py)
+    return px - py;
+
+  /* Use some arbitrary order to avoid pattern explosion.  */
+  return (int) GET_CODE (x) - (int) GET_CODE (y);
+}
+
 /* Return 1 iff it is necessary to swap operands of commutative operation
    in order to canonicalize expression.  */
 
 bool
 swap_commutative_operands_p (rtx x, rtx y)
 {
-  return (commutative_operand_precedence (x)
-	  < commutative_operand_precedence (y));
+  return compare_commutative_operands_precedence (x, y) < 0;
 }
 
 /* Return 1 if X is an autoincrement side effect and the register is
    not the stack pointer.  */
 int
 auto_inc_p (const_rtx x)
 {
   switch (GET_CODE (x))
     {
     case PRE_INC:
Index: optabs.c
===================================================================
--- optabs.c	(revision 196633)
+++ optabs.c	(working copy)
@@ -1285,33 +1285,29 @@ expand_simple_binop (enum machine_mode m
 		     rtx op1, rtx target, int unsignedp,
 		     enum optab_methods methods)
 {
   optab binop = code_to_optab (code);
   gcc_assert (binop);
 
   return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
 }
 
 /* Return whether OP0 and OP1 should be swapped when expanding a commutative
-   binop.  Order them according to commutative_operand_precedence and, if
-   possible, try to put TARGET or a pseudo first.  */
+   binop.  Order them according to compare_commutative_operands_precedence and,
+   if possible, try to put TARGET or a pseudo first.  */
 static bool
 swap_commutative_operands_with_target (rtx target, rtx op0, rtx op1)
 {
-  int op0_prec = commutative_operand_precedence (op0);
-  int op1_prec = commutative_operand_precedence (op1);
+  int cmp = compare_commutative_operands_precedence (op0, op1);
 
-  if (op0_prec < op1_prec)
-    return true;
-
-  if (op0_prec > op1_prec)
-    return false;
+  if (cmp != 0)
+    return cmp < 0;
 
   /* With equal precedence, both orders are ok, but it is better if the
      first operand is TARGET, or if both TARGET and OP0 are pseudos.  */
   if (target == 0 || REG_P (target))
     return (REG_P (op1) && !REG_P (op0)) || target == op1;
   else
     return rtx_equal_p (op1, target);
 }
 
 /* Return true if BINOPTAB implements a shift operation.  */
Index: simplify-rtx.c
===================================================================
--- simplify-rtx.c	(revision 196633)
+++ simplify-rtx.c	(working copy)
@@ -2076,22 +2076,24 @@ simplify_associative_operation (enum rtx
       if (! swap_commutative_operands_p (op1, op0))
 	return simplify_gen_binary (code, mode, op1, op0);
 
       tem = op0;
       op0 = op1;
       op1 = tem;
     }
 
   if (GET_CODE (op0) == code)
     {
-      /* Canonicalize "(x op c) op y" as "(x op y) op c".  */
-      if (swap_commutative_operands_p (XEXP (op0, 1), op1))
+      /* Canonicalize "(x op c) op y" as "(x op y) op c".
+	 GET_CODE != code is here to avoid an infinite recursion.  */
+      if (GET_CODE (XEXP (op0, 1)) != code
+	  && swap_commutative_operands_p (XEXP (op0, 1), op1))
 	{
 	  tem = simplify_gen_binary (code, mode, XEXP (op0, 0), op1);
 	  return simplify_gen_binary (code, mode, tem, XEXP (op0, 1));
 	}
 
       /* Attempt to simplify "(a op b) op c" as "a op (b op c)".  */
       tem = simplify_binary_operation (code, mode, XEXP (op0, 1), op1);
       if (tem != 0)
         return simplify_gen_binary (code, mode, XEXP (op0, 0), tem);
 
@@ -4158,26 +4160,24 @@ simplify_const_binary_operation (enum rt
 
 struct simplify_plus_minus_op_data
 {
   rtx op;
   short neg;
 };
 
 static bool
 simplify_plus_minus_op_data_cmp (rtx x, rtx y)
 {
-  int result;
+  int result = compare_commutative_operands_precedence (x, y);
 
-  result = (commutative_operand_precedence (y)
-	    - commutative_operand_precedence (x));
   if (result)
-    return result > 0;
+    return result < 0;
 
   /* Group together equal REGs to do more simplification.  */
   if (REG_P (x) && REG_P (y))
     return REGNO (x) > REGNO (y);
   else
     return false;
 }
 
 static rtx
 simplify_plus_minus (enum rtx_code code, enum machine_mode mode, rtx op0,
Index: rtl.h
===================================================================
--- rtl.h	(revision 196633)
+++ rtl.h	(working copy)
@@ -2015,21 +2015,21 @@ extern rtx get_call_rtx_from (rtx);
 extern HOST_WIDE_INT get_integer_term (const_rtx);
 extern rtx get_related_value (const_rtx);
 extern bool offset_within_block_p (const_rtx, HOST_WIDE_INT);
 extern void split_const (rtx, rtx *, rtx *);
 extern bool unsigned_reg_p (rtx);
 extern int reg_mentioned_p (const_rtx, const_rtx);
 extern int count_occurrences (const_rtx, const_rtx, int);
 extern int reg_referenced_p (const_rtx, const_rtx);
 extern int reg_used_between_p (const_rtx, const_rtx, const_rtx);
 extern int reg_set_between_p (const_rtx, const_rtx, const_rtx);
-extern int commutative_operand_precedence (rtx);
+extern int compare_commutative_operands_precedence (rtx, rtx);
 extern bool swap_commutative_operands_p (rtx, rtx);
 extern int modified_between_p (const_rtx, const_rtx, const_rtx);
 extern int no_labels_between_p (const_rtx, const_rtx);
 extern int modified_in_p (const_rtx, const_rtx);
 extern int reg_set_p (const_rtx, const_rtx);
 extern rtx single_set_2 (const_rtx, const_rtx);
 extern int multiple_sets (const_rtx);
 extern int set_noop_p (const_rtx);
 extern int noop_move_p (const_rtx);
 extern rtx find_last_value (rtx, rtx *, rtx, int);

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2013-04-04 15:15 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-03-19 17:14 [RTL] Canonicalize commutative operations more Marc Glisse
2013-03-27 10:01 ` Eric Botcazou
2013-03-30 12:49   ` Marc Glisse
2013-04-02  9:23     ` Eric Botcazou
2013-04-02 11:12       ` Marc Glisse
2013-04-02 11:44         ` Eric Botcazou
2013-04-02 17:19           ` Jeff Law
2013-04-04 18:57       ` Mike Stump

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).