diff mbox series

[3/5] gimple-match: Make code_helper conversions explicit

Message ID mpt35o4w55d.fsf@arm.com
State Committed
Headers show
Series [1/5] Add IFN_COND_FMIN/FMAX functions | expand

Commit Message

Richard Sandiford Nov. 10, 2021, 12:45 p.m. UTC
code_helper provides conversions to tree_code and combined_fn.
Now that the codebase is C++11, we can mark these conversions as
explicit.  This avoids accidentally using code_helpers with
functions that take tree_codes, which would previously entail
a hidden unchecked conversion.

Tested on aarch64-linux-gnu and x86_64-linux-gnu.  OK to install?

Richard


gcc/
	* gimple-match.h (code_helper): Provide == and != overloads.
	(code_helper::operator tree_code): Make explicit.
	(code_helper::operator combined_fn): Likewise.
	* gimple-match-head.c (convert_conditional_op): Use explicit
	conversions where necessary.
	(gimple_resimplify1, gimple_resimplify2, gimple_resimplify3): Likewise.
	(maybe_push_res_to_seq, gimple_simplify): Likewise.
	* gimple-fold.c (replace_stmt_with_simplification): Likewise.
---
 gcc/gimple-fold.c       | 18 ++++++++-------
 gcc/gimple-match-head.c | 51 ++++++++++++++++++++++-------------------
 gcc/gimple-match.h      |  9 ++++++--
 3 files changed, 45 insertions(+), 33 deletions(-)

Comments

Richard Biener Nov. 11, 2021, 10:50 a.m. UTC | #1
On Wed, Nov 10, 2021 at 1:47 PM Richard Sandiford via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> code_helper provides conversions to tree_code and combined_fn.
> Now that the codebase is C++11, we can mark these conversions as
> explicit.  This avoids accidentally using code_helpers with
> functions that take tree_codes, which would previously entail
> a hidden unchecked conversion.
>
> Tested on aarch64-linux-gnu and x86_64-linux-gnu.  OK to install?

OK.

Thanks,
Richard.

> Richard
>
>
> gcc/
>         * gimple-match.h (code_helper): Provide == and != overloads.
>         (code_helper::operator tree_code): Make explicit.
>         (code_helper::operator combined_fn): Likewise.
>         * gimple-match-head.c (convert_conditional_op): Use explicit
>         conversions where necessary.
>         (gimple_resimplify1, gimple_resimplify2, gimple_resimplify3): Likewise.
>         (maybe_push_res_to_seq, gimple_simplify): Likewise.
>         * gimple-fold.c (replace_stmt_with_simplification): Likewise.
> ---
>  gcc/gimple-fold.c       | 18 ++++++++-------
>  gcc/gimple-match-head.c | 51 ++++++++++++++++++++++-------------------
>  gcc/gimple-match.h      |  9 ++++++--
>  3 files changed, 45 insertions(+), 33 deletions(-)
>
> diff --git a/gcc/gimple-fold.c b/gcc/gimple-fold.c
> index 6e25a7c05db..9daf2cc590c 100644
> --- a/gcc/gimple-fold.c
> +++ b/gcc/gimple-fold.c
> @@ -5828,18 +5828,19 @@ replace_stmt_with_simplification (gimple_stmt_iterator *gsi,
>    if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
>      {
>        gcc_assert (res_op->code.is_tree_code ());
> -      if (TREE_CODE_CLASS ((enum tree_code) res_op->code) == tcc_comparison
> +      auto code = tree_code (res_op->code);
> +      if (TREE_CODE_CLASS (code) == tcc_comparison
>           /* GIMPLE_CONDs condition may not throw.  */
>           && (!flag_exceptions
>               || !cfun->can_throw_non_call_exceptions
> -             || !operation_could_trap_p (res_op->code,
> +             || !operation_could_trap_p (code,
>                                           FLOAT_TYPE_P (TREE_TYPE (ops[0])),
>                                           false, NULL_TREE)))
> -       gimple_cond_set_condition (cond_stmt, res_op->code, ops[0], ops[1]);
> -      else if (res_op->code == SSA_NAME)
> +       gimple_cond_set_condition (cond_stmt, code, ops[0], ops[1]);
> +      else if (code == SSA_NAME)
>         gimple_cond_set_condition (cond_stmt, NE_EXPR, ops[0],
>                                    build_zero_cst (TREE_TYPE (ops[0])));
> -      else if (res_op->code == INTEGER_CST)
> +      else if (code == INTEGER_CST)
>         {
>           if (integer_zerop (ops[0]))
>             gimple_cond_make_false (cond_stmt);
> @@ -5870,11 +5871,12 @@ replace_stmt_with_simplification (gimple_stmt_iterator *gsi,
>    else if (is_gimple_assign (stmt)
>            && res_op->code.is_tree_code ())
>      {
> +      auto code = tree_code (res_op->code);
>        if (!inplace
> -         || gimple_num_ops (stmt) > get_gimple_rhs_num_ops (res_op->code))
> +         || gimple_num_ops (stmt) > get_gimple_rhs_num_ops (code))
>         {
>           maybe_build_generic_op (res_op);
> -         gimple_assign_set_rhs_with_ops (gsi, res_op->code,
> +         gimple_assign_set_rhs_with_ops (gsi, code,
>                                           res_op->op_or_null (0),
>                                           res_op->op_or_null (1),
>                                           res_op->op_or_null (2));
> @@ -5891,7 +5893,7 @@ replace_stmt_with_simplification (gimple_stmt_iterator *gsi,
>         }
>      }
>    else if (res_op->code.is_fn_code ()
> -          && gimple_call_combined_fn (stmt) == res_op->code)
> +          && gimple_call_combined_fn (stmt) == combined_fn (res_op->code))
>      {
>        gcc_assert (num_ops == gimple_call_num_args (stmt));
>        for (unsigned int i = 0; i < num_ops; ++i)
> diff --git a/gcc/gimple-match-head.c b/gcc/gimple-match-head.c
> index 4c6e0883ba4..d4d7d767075 100644
> --- a/gcc/gimple-match-head.c
> +++ b/gcc/gimple-match-head.c
> @@ -96,7 +96,7 @@ convert_conditional_op (gimple_match_op *orig_op,
>      ifn = get_conditional_internal_fn ((tree_code) orig_op->code);
>    else
>      {
> -      combined_fn cfn = orig_op->code;
> +      auto cfn = combined_fn (orig_op->code);
>        if (!internal_fn_p (cfn))
>         return false;
>        ifn = get_conditional_internal_fn (as_internal_fn (cfn));
> @@ -206,10 +206,10 @@ gimple_resimplify1 (gimple_seq *seq, gimple_match_op *res_op,
>        tree tem = NULL_TREE;
>        if (res_op->code.is_tree_code ())
>         {
> -         tree_code code = res_op->code;
> +         auto code = tree_code (res_op->code);
>           if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
>               && TREE_CODE_LENGTH (code) == 1)
> -           tem = const_unop (res_op->code, res_op->type, res_op->ops[0]);
> +           tem = const_unop (code, res_op->type, res_op->ops[0]);
>         }
>        else
>         tem = fold_const_call (combined_fn (res_op->code), res_op->type,
> @@ -272,10 +272,10 @@ gimple_resimplify2 (gimple_seq *seq, gimple_match_op *res_op,
>        tree tem = NULL_TREE;
>        if (res_op->code.is_tree_code ())
>         {
> -         tree_code code = res_op->code;
> +         auto code = tree_code (res_op->code);
>           if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
>               && TREE_CODE_LENGTH (code) == 2)
> -           tem = const_binop (res_op->code, res_op->type,
> +           tem = const_binop (code, res_op->type,
>                                res_op->ops[0], res_op->ops[1]);
>         }
>        else
> @@ -294,15 +294,18 @@ gimple_resimplify2 (gimple_seq *seq, gimple_match_op *res_op,
>
>    /* Canonicalize operand order.  */
>    bool canonicalized = false;
> -  if (res_op->code.is_tree_code ()
> -      && (TREE_CODE_CLASS ((enum tree_code) res_op->code) == tcc_comparison
> -         || commutative_tree_code (res_op->code))
> -      && tree_swap_operands_p (res_op->ops[0], res_op->ops[1]))
> +  if (res_op->code.is_tree_code ())
>      {
> -      std::swap (res_op->ops[0], res_op->ops[1]);
> -      if (TREE_CODE_CLASS ((enum tree_code) res_op->code) == tcc_comparison)
> -       res_op->code = swap_tree_comparison (res_op->code);
> -      canonicalized = true;
> +      auto code = tree_code (res_op->code);
> +      if ((TREE_CODE_CLASS (code) == tcc_comparison
> +          || commutative_tree_code (code))
> +         && tree_swap_operands_p (res_op->ops[0], res_op->ops[1]))
> +       {
> +         std::swap (res_op->ops[0], res_op->ops[1]);
> +         if (TREE_CODE_CLASS (code) == tcc_comparison)
> +           res_op->code = swap_tree_comparison (code);
> +         canonicalized = true;
> +       }
>      }
>
>    /* Limit recursion, see gimple_resimplify1.  */
> @@ -350,10 +353,10 @@ gimple_resimplify3 (gimple_seq *seq, gimple_match_op *res_op,
>        tree tem = NULL_TREE;
>        if (res_op->code.is_tree_code ())
>         {
> -         tree_code code = res_op->code;
> +         auto code = tree_code (res_op->code);
>           if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
>               && TREE_CODE_LENGTH (code) == 3)
> -           tem = fold_ternary/*_to_constant*/ (res_op->code, res_op->type,
> +           tem = fold_ternary/*_to_constant*/ (code, res_op->type,
>                                                 res_op->ops[0], res_op->ops[1],
>                                                 res_op->ops[2]);
>         }
> @@ -374,7 +377,7 @@ gimple_resimplify3 (gimple_seq *seq, gimple_match_op *res_op,
>    /* Canonicalize operand order.  */
>    bool canonicalized = false;
>    if (res_op->code.is_tree_code ()
> -      && commutative_ternary_tree_code (res_op->code)
> +      && commutative_ternary_tree_code (tree_code (res_op->code))
>        && tree_swap_operands_p (res_op->ops[0], res_op->ops[1]))
>      {
>        std::swap (res_op->ops[0], res_op->ops[1]);
> @@ -599,6 +602,7 @@ maybe_push_res_to_seq (gimple_match_op *res_op, gimple_seq *seq, tree res)
>
>    if (res_op->code.is_tree_code ())
>      {
> +      auto code = tree_code (res_op->code);
>        if (!res)
>         {
>           if (gimple_in_ssa_p (cfun))
> @@ -607,7 +611,7 @@ maybe_push_res_to_seq (gimple_match_op *res_op, gimple_seq *seq, tree res)
>             res = create_tmp_reg (res_op->type);
>         }
>        maybe_build_generic_op (res_op);
> -      gimple *new_stmt = gimple_build_assign (res, res_op->code,
> +      gimple *new_stmt = gimple_build_assign (res, code,
>                                               res_op->op_or_null (0),
>                                               res_op->op_or_null (1),
>                                               res_op->op_or_null (2));
> @@ -617,7 +621,7 @@ maybe_push_res_to_seq (gimple_match_op *res_op, gimple_seq *seq, tree res)
>    else
>      {
>        gcc_assert (num_ops != 0);
> -      combined_fn fn = res_op->code;
> +      auto fn = combined_fn (res_op->code);
>        gcall *new_stmt = NULL;
>        if (internal_fn_p (fn))
>         {
> @@ -1070,15 +1074,16 @@ gimple_simplify (gimple *stmt, gimple_match_op *res_op, gimple_seq *seq,
>            || cond_valueized)
>           && res_op2.code.is_tree_code ())
>         {
> -         if (TREE_CODE_CLASS ((tree_code) res_op2.code) == tcc_comparison)
> +         auto code = tree_code (res_op2.code);
> +         if (TREE_CODE_CLASS (code) == tcc_comparison)
>             {
>               valueized = true;
> -             return build2 (res_op2.code, TREE_TYPE (op),
> +             return build2 (code, TREE_TYPE (op),
>                              res_op2.ops[0], res_op2.ops[1]);
>             }
> -         else if (res_op2.code == SSA_NAME
> -                  || res_op2.code == INTEGER_CST
> -                  || res_op2.code == VECTOR_CST)
> +         else if (code == SSA_NAME
> +                  || code == INTEGER_CST
> +                  || code == VECTOR_CST)
>             {
>               valueized = true;
>               return res_op2.ops[0];
> diff --git a/gcc/gimple-match.h b/gcc/gimple-match.h
> index 15a0f584db7..1b9dc3851c2 100644
> --- a/gcc/gimple-match.h
> +++ b/gcc/gimple-match.h
> @@ -31,11 +31,16 @@ public:
>    code_helper () {}
>    code_helper (tree_code code) : rep ((int) code) {}
>    code_helper (combined_fn fn) : rep (-(int) fn) {}
> -  operator tree_code () const { return (tree_code) rep; }
> -  operator combined_fn () const { return (combined_fn) -rep; }
> +  explicit operator tree_code () const { return (tree_code) rep; }
> +  explicit operator combined_fn () const { return (combined_fn) -rep; }
>    bool is_tree_code () const { return rep > 0; }
>    bool is_fn_code () const { return rep < 0; }
>    int get_rep () const { return rep; }
> +  bool operator== (const code_helper &other) { return rep == other.rep; }
> +  bool operator!= (const code_helper &other) { return rep != other.rep; }
> +  bool operator== (tree_code c) { return rep == code_helper (c).rep; }
> +  bool operator!= (tree_code c) { return rep != code_helper (c).rep; }
> +
>  private:
>    int rep;
>  };
> --
> 2.25.1
>
diff mbox series

Patch

diff --git a/gcc/gimple-fold.c b/gcc/gimple-fold.c
index 6e25a7c05db..9daf2cc590c 100644
--- a/gcc/gimple-fold.c
+++ b/gcc/gimple-fold.c
@@ -5828,18 +5828,19 @@  replace_stmt_with_simplification (gimple_stmt_iterator *gsi,
   if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
     {
       gcc_assert (res_op->code.is_tree_code ());
-      if (TREE_CODE_CLASS ((enum tree_code) res_op->code) == tcc_comparison
+      auto code = tree_code (res_op->code);
+      if (TREE_CODE_CLASS (code) == tcc_comparison
 	  /* GIMPLE_CONDs condition may not throw.  */
 	  && (!flag_exceptions
 	      || !cfun->can_throw_non_call_exceptions
-	      || !operation_could_trap_p (res_op->code,
+	      || !operation_could_trap_p (code,
 					  FLOAT_TYPE_P (TREE_TYPE (ops[0])),
 					  false, NULL_TREE)))
-	gimple_cond_set_condition (cond_stmt, res_op->code, ops[0], ops[1]);
-      else if (res_op->code == SSA_NAME)
+	gimple_cond_set_condition (cond_stmt, code, ops[0], ops[1]);
+      else if (code == SSA_NAME)
 	gimple_cond_set_condition (cond_stmt, NE_EXPR, ops[0],
 				   build_zero_cst (TREE_TYPE (ops[0])));
-      else if (res_op->code == INTEGER_CST)
+      else if (code == INTEGER_CST)
 	{
 	  if (integer_zerop (ops[0]))
 	    gimple_cond_make_false (cond_stmt);
@@ -5870,11 +5871,12 @@  replace_stmt_with_simplification (gimple_stmt_iterator *gsi,
   else if (is_gimple_assign (stmt)
 	   && res_op->code.is_tree_code ())
     {
+      auto code = tree_code (res_op->code);
       if (!inplace
-	  || gimple_num_ops (stmt) > get_gimple_rhs_num_ops (res_op->code))
+	  || gimple_num_ops (stmt) > get_gimple_rhs_num_ops (code))
 	{
 	  maybe_build_generic_op (res_op);
-	  gimple_assign_set_rhs_with_ops (gsi, res_op->code,
+	  gimple_assign_set_rhs_with_ops (gsi, code,
 					  res_op->op_or_null (0),
 					  res_op->op_or_null (1),
 					  res_op->op_or_null (2));
@@ -5891,7 +5893,7 @@  replace_stmt_with_simplification (gimple_stmt_iterator *gsi,
 	}
     }
   else if (res_op->code.is_fn_code ()
-	   && gimple_call_combined_fn (stmt) == res_op->code)
+	   && gimple_call_combined_fn (stmt) == combined_fn (res_op->code))
     {
       gcc_assert (num_ops == gimple_call_num_args (stmt));
       for (unsigned int i = 0; i < num_ops; ++i)
diff --git a/gcc/gimple-match-head.c b/gcc/gimple-match-head.c
index 4c6e0883ba4..d4d7d767075 100644
--- a/gcc/gimple-match-head.c
+++ b/gcc/gimple-match-head.c
@@ -96,7 +96,7 @@  convert_conditional_op (gimple_match_op *orig_op,
     ifn = get_conditional_internal_fn ((tree_code) orig_op->code);
   else
     {
-      combined_fn cfn = orig_op->code;
+      auto cfn = combined_fn (orig_op->code);
       if (!internal_fn_p (cfn))
 	return false;
       ifn = get_conditional_internal_fn (as_internal_fn (cfn));
@@ -206,10 +206,10 @@  gimple_resimplify1 (gimple_seq *seq, gimple_match_op *res_op,
       tree tem = NULL_TREE;
       if (res_op->code.is_tree_code ())
 	{
-	  tree_code code = res_op->code;
+	  auto code = tree_code (res_op->code);
 	  if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
 	      && TREE_CODE_LENGTH (code) == 1)
-	    tem = const_unop (res_op->code, res_op->type, res_op->ops[0]);
+	    tem = const_unop (code, res_op->type, res_op->ops[0]);
 	}
       else
 	tem = fold_const_call (combined_fn (res_op->code), res_op->type,
@@ -272,10 +272,10 @@  gimple_resimplify2 (gimple_seq *seq, gimple_match_op *res_op,
       tree tem = NULL_TREE;
       if (res_op->code.is_tree_code ())
 	{
-	  tree_code code = res_op->code;
+	  auto code = tree_code (res_op->code);
 	  if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
 	      && TREE_CODE_LENGTH (code) == 2)
-	    tem = const_binop (res_op->code, res_op->type,
+	    tem = const_binop (code, res_op->type,
 			       res_op->ops[0], res_op->ops[1]);
 	}
       else
@@ -294,15 +294,18 @@  gimple_resimplify2 (gimple_seq *seq, gimple_match_op *res_op,
 
   /* Canonicalize operand order.  */
   bool canonicalized = false;
-  if (res_op->code.is_tree_code ()
-      && (TREE_CODE_CLASS ((enum tree_code) res_op->code) == tcc_comparison
-	  || commutative_tree_code (res_op->code))
-      && tree_swap_operands_p (res_op->ops[0], res_op->ops[1]))
+  if (res_op->code.is_tree_code ())
     {
-      std::swap (res_op->ops[0], res_op->ops[1]);
-      if (TREE_CODE_CLASS ((enum tree_code) res_op->code) == tcc_comparison)
-	res_op->code = swap_tree_comparison (res_op->code);
-      canonicalized = true;
+      auto code = tree_code (res_op->code);
+      if ((TREE_CODE_CLASS (code) == tcc_comparison
+	   || commutative_tree_code (code))
+	  && tree_swap_operands_p (res_op->ops[0], res_op->ops[1]))
+	{
+	  std::swap (res_op->ops[0], res_op->ops[1]);
+	  if (TREE_CODE_CLASS (code) == tcc_comparison)
+	    res_op->code = swap_tree_comparison (code);
+	  canonicalized = true;
+	}
     }
 
   /* Limit recursion, see gimple_resimplify1.  */
@@ -350,10 +353,10 @@  gimple_resimplify3 (gimple_seq *seq, gimple_match_op *res_op,
       tree tem = NULL_TREE;
       if (res_op->code.is_tree_code ())
 	{
-	  tree_code code = res_op->code;
+	  auto code = tree_code (res_op->code);
 	  if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
 	      && TREE_CODE_LENGTH (code) == 3)
-	    tem = fold_ternary/*_to_constant*/ (res_op->code, res_op->type,
+	    tem = fold_ternary/*_to_constant*/ (code, res_op->type,
 						res_op->ops[0], res_op->ops[1],
 						res_op->ops[2]);
 	}
@@ -374,7 +377,7 @@  gimple_resimplify3 (gimple_seq *seq, gimple_match_op *res_op,
   /* Canonicalize operand order.  */
   bool canonicalized = false;
   if (res_op->code.is_tree_code ()
-      && commutative_ternary_tree_code (res_op->code)
+      && commutative_ternary_tree_code (tree_code (res_op->code))
       && tree_swap_operands_p (res_op->ops[0], res_op->ops[1]))
     {
       std::swap (res_op->ops[0], res_op->ops[1]);
@@ -599,6 +602,7 @@  maybe_push_res_to_seq (gimple_match_op *res_op, gimple_seq *seq, tree res)
 
   if (res_op->code.is_tree_code ())
     {
+      auto code = tree_code (res_op->code);
       if (!res)
 	{
 	  if (gimple_in_ssa_p (cfun))
@@ -607,7 +611,7 @@  maybe_push_res_to_seq (gimple_match_op *res_op, gimple_seq *seq, tree res)
 	    res = create_tmp_reg (res_op->type);
 	}
       maybe_build_generic_op (res_op);
-      gimple *new_stmt = gimple_build_assign (res, res_op->code,
+      gimple *new_stmt = gimple_build_assign (res, code,
 					      res_op->op_or_null (0),
 					      res_op->op_or_null (1),
 					      res_op->op_or_null (2));
@@ -617,7 +621,7 @@  maybe_push_res_to_seq (gimple_match_op *res_op, gimple_seq *seq, tree res)
   else
     {
       gcc_assert (num_ops != 0);
-      combined_fn fn = res_op->code;
+      auto fn = combined_fn (res_op->code);
       gcall *new_stmt = NULL;
       if (internal_fn_p (fn))
 	{
@@ -1070,15 +1074,16 @@  gimple_simplify (gimple *stmt, gimple_match_op *res_op, gimple_seq *seq,
 	   || cond_valueized)
 	  && res_op2.code.is_tree_code ())
 	{
-	  if (TREE_CODE_CLASS ((tree_code) res_op2.code) == tcc_comparison)
+	  auto code = tree_code (res_op2.code);
+	  if (TREE_CODE_CLASS (code) == tcc_comparison)
 	    {
 	      valueized = true;
-	      return build2 (res_op2.code, TREE_TYPE (op),
+	      return build2 (code, TREE_TYPE (op),
 			     res_op2.ops[0], res_op2.ops[1]);
 	    }
-	  else if (res_op2.code == SSA_NAME
-		   || res_op2.code == INTEGER_CST
-		   || res_op2.code == VECTOR_CST)
+	  else if (code == SSA_NAME
+		   || code == INTEGER_CST
+		   || code == VECTOR_CST)
 	    {
 	      valueized = true;
 	      return res_op2.ops[0];
diff --git a/gcc/gimple-match.h b/gcc/gimple-match.h
index 15a0f584db7..1b9dc3851c2 100644
--- a/gcc/gimple-match.h
+++ b/gcc/gimple-match.h
@@ -31,11 +31,16 @@  public:
   code_helper () {}
   code_helper (tree_code code) : rep ((int) code) {}
   code_helper (combined_fn fn) : rep (-(int) fn) {}
-  operator tree_code () const { return (tree_code) rep; }
-  operator combined_fn () const { return (combined_fn) -rep; }
+  explicit operator tree_code () const { return (tree_code) rep; }
+  explicit operator combined_fn () const { return (combined_fn) -rep; }
   bool is_tree_code () const { return rep > 0; }
   bool is_fn_code () const { return rep < 0; }
   int get_rep () const { return rep; }
+  bool operator== (const code_helper &other) { return rep == other.rep; }
+  bool operator!= (const code_helper &other) { return rep != other.rep; }
+  bool operator== (tree_code c) { return rep == code_helper (c).rep; }
+  bool operator!= (tree_code c) { return rep != code_helper (c).rep; }
+
 private:
   int rep;
 };