diff mbox series

[4/7] Expand the comparison argument of fold_cond_expr_with_comparison

Message ID 1624138164-5419-1-git-send-email-apinski@marvell.com
State New
Headers show
Series None | expand

Commit Message

Li, Pan2 via Gcc-patches June 19, 2021, 9:29 p.m. UTC
From: Andrew Pinski <apinski@marvell.com>

To make things slightly easiler to convert fold_cond_expr_with_comparison
over to match.pd, expanding the arg0 argument into 3 different arguments
is done. Also this was simple because we don't use arg0 after grabbing
the code and the two operands.
Also since we do this, we don't need to fold the comparison to
get the inverse but just use invert_tree_comparison directly.

OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

gcc/ChangeLog:

	* fold-const.c (fold_cond_expr_with_comparison):
	Exand arg0 into comp_code, arg00, and arg01.
	(fold_ternary_loc): Use invert_tree_comparison
	instead of fold_invert_truthvalue for the case
	where we have A CMP B ? C : A.
---
 gcc/fold-const.c | 39 ++++++++++++++++++++++-----------------
 1 file changed, 22 insertions(+), 17 deletions(-)

Comments

Richard Biener June 21, 2021, 6:55 a.m. UTC | #1
On Sat, Jun 19, 2021 at 11:30 PM apinski--- via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> From: Andrew Pinski <apinski@marvell.com>
>
> To make things slightly easiler to convert fold_cond_expr_with_comparison
> over to match.pd, expanding the arg0 argument into 3 different arguments
> is done. Also this was simple because we don't use arg0 after grabbing
> the code and the two operands.
> Also since we do this, we don't need to fold the comparison to
> get the inverse but just use invert_tree_comparison directly.
>
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

Probaby fold_invert_truthvalue is not 100% equivalent to
just invert_tree_comparison but definitely simple inverting was the intent.

Thus OK.

Richard.

> gcc/ChangeLog:
>
>         * fold-const.c (fold_cond_expr_with_comparison):
>         Exand arg0 into comp_code, arg00, and arg01.
>         (fold_ternary_loc): Use invert_tree_comparison
>         instead of fold_invert_truthvalue for the case
>         where we have A CMP B ? C : A.
> ---
>  gcc/fold-const.c | 39 ++++++++++++++++++++++-----------------
>  1 file changed, 22 insertions(+), 17 deletions(-)
>
> diff --git a/gcc/fold-const.c b/gcc/fold-const.c
> index 95673d2..85e90f4 100644
> --- a/gcc/fold-const.c
> +++ b/gcc/fold-const.c
> @@ -126,7 +126,8 @@ static tree range_binop (enum tree_code, tree, tree, int, tree, int);
>  static tree range_predecessor (tree);
>  static tree range_successor (tree);
>  static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
> -static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
> +static tree fold_cond_expr_with_comparison (location_t, tree, enum tree_code,
> +                                           tree, tree, tree, tree);
>  static tree unextend (tree, int, int, tree);
>  static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
>  static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
> @@ -5735,20 +5736,19 @@ merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
>
>
>  /* Subroutine of fold, looking inside expressions of the form
> -   A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
> -   of the COND_EXPR.  This function is being used also to optimize
> -   A op B ? C : A, by reversing the comparison first.
> +   A op B ? A : C, where (ARG00, COMP_CODE, ARG01), ARG1 and ARG2
> +   are the three operands of the COND_EXPR.  This function is
> +   being used also to optimize A op B ? C : A, by reversing the
> +   comparison first.
>
>     Return a folded expression whose code is not a COND_EXPR
>     anymore, or NULL_TREE if no folding opportunity is found.  */
>
>  static tree
>  fold_cond_expr_with_comparison (location_t loc, tree type,
> -                               tree arg0, tree arg1, tree arg2)
> +                               enum tree_code comp_code,
> +                               tree arg00, tree arg01, tree arg1, tree arg2)
>  {
> -  enum tree_code comp_code = TREE_CODE (arg0);
> -  tree arg00 = TREE_OPERAND (arg0, 0);
> -  tree arg01 = TREE_OPERAND (arg0, 1);
>    tree arg1_type = TREE_TYPE (arg1);
>    tree tem;
>
> @@ -12822,7 +12822,10 @@ fold_ternary_loc (location_t loc, enum tree_code code, tree type,
>           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
>           && !HONOR_SIGNED_ZEROS (element_mode (op1)))
>         {
> -         tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
> +         tem = fold_cond_expr_with_comparison (loc, type, TREE_CODE (arg0),
> +                                               TREE_OPERAND (arg0, 0),
> +                                               TREE_OPERAND (arg0, 1),
> +                                               op1, op2);
>           if (tem)
>             return tem;
>         }
> @@ -12831,14 +12834,16 @@ fold_ternary_loc (location_t loc, enum tree_code code, tree type,
>           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
>           && !HONOR_SIGNED_ZEROS (element_mode (op2)))
>         {
> -         location_t loc0 = expr_location_or (arg0, loc);
> -         tem = fold_invert_truthvalue (loc0, arg0);
> -         if (tem && COMPARISON_CLASS_P (tem))
> -           {
> -             tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
> -             if (tem)
> -               return tem;
> -           }
> +         enum tree_code comp_code = TREE_CODE (arg0);
> +         tree arg00 = TREE_OPERAND (arg0, 0);
> +         tree arg01 = TREE_OPERAND (arg0, 1);
> +         comp_code = invert_tree_comparison (comp_code, HONOR_NANS (arg00));
> +         tem = fold_cond_expr_with_comparison (loc, type, comp_code,
> +                                               arg00,
> +                                               arg01,
> +                                               op2, op1);
> +         if (tem)
> +           return tem;
>         }
>
>        /* If the second operand is simpler than the third, swap them
> --
> 1.8.3.1
>
diff mbox series

Patch

diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index 95673d2..85e90f4 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -126,7 +126,8 @@  static tree range_binop (enum tree_code, tree, tree, int, tree, int);
 static tree range_predecessor (tree);
 static tree range_successor (tree);
 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
-static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
+static tree fold_cond_expr_with_comparison (location_t, tree, enum tree_code,
+					    tree, tree, tree, tree);
 static tree unextend (tree, int, int, tree);
 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
@@ -5735,20 +5736,19 @@  merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
 
 
 /* Subroutine of fold, looking inside expressions of the form
-   A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
-   of the COND_EXPR.  This function is being used also to optimize
-   A op B ? C : A, by reversing the comparison first.
+   A op B ? A : C, where (ARG00, COMP_CODE, ARG01), ARG1 and ARG2
+   are the three operands of the COND_EXPR.  This function is
+   being used also to optimize A op B ? C : A, by reversing the
+   comparison first.
 
    Return a folded expression whose code is not a COND_EXPR
    anymore, or NULL_TREE if no folding opportunity is found.  */
 
 static tree
 fold_cond_expr_with_comparison (location_t loc, tree type,
-				tree arg0, tree arg1, tree arg2)
+				enum tree_code comp_code,
+				tree arg00, tree arg01, tree arg1, tree arg2)
 {
-  enum tree_code comp_code = TREE_CODE (arg0);
-  tree arg00 = TREE_OPERAND (arg0, 0);
-  tree arg01 = TREE_OPERAND (arg0, 1);
   tree arg1_type = TREE_TYPE (arg1);
   tree tem;
 
@@ -12822,7 +12822,10 @@  fold_ternary_loc (location_t loc, enum tree_code code, tree type,
 	  && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
 	  && !HONOR_SIGNED_ZEROS (element_mode (op1)))
 	{
-	  tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
+	  tem = fold_cond_expr_with_comparison (loc, type, TREE_CODE (arg0),
+						TREE_OPERAND (arg0, 0),
+						TREE_OPERAND (arg0, 1),
+						op1, op2);
 	  if (tem)
 	    return tem;
 	}
@@ -12831,14 +12834,16 @@  fold_ternary_loc (location_t loc, enum tree_code code, tree type,
 	  && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
 	  && !HONOR_SIGNED_ZEROS (element_mode (op2)))
 	{
-	  location_t loc0 = expr_location_or (arg0, loc);
-	  tem = fold_invert_truthvalue (loc0, arg0);
-	  if (tem && COMPARISON_CLASS_P (tem))
-	    {
-	      tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
-	      if (tem)
-		return tem;
-	    }
+	  enum tree_code comp_code = TREE_CODE (arg0);
+	  tree arg00 = TREE_OPERAND (arg0, 0);
+	  tree arg01 = TREE_OPERAND (arg0, 1);
+	  comp_code = invert_tree_comparison (comp_code, HONOR_NANS (arg00));
+	  tem = fold_cond_expr_with_comparison (loc, type, comp_code,
+						arg00,
+						arg01,
+						op2, op1);
+	  if (tem)
+	    return tem;
 	}
 
       /* If the second operand is simpler than the third, swap them