This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] Use more switch statements.
- From: Martin Liška <mliska at suse dot cz>
- To: gcc-patches at gcc dot gnu dot org
- Cc: Richard Biener <richard dot guenther at gmail dot com>
- Date: Tue, 24 Sep 2019 12:15:23 +0200
- Subject: [PATCH] Use more switch statements.
Hi.
The patch is about a refactoring where we should use
more switch statements rather that if-elseif-elseif
chains.
I've been testing the patch.
Ready to be installed after tests?
Martin
gcc/ChangeLog:
2019-09-24 Martin Liska <mliska@suse.cz>
* cfgexpand.c (gimple_assign_rhs_to_tree): Use switch statement
instead of if-elseif-elseif-...
* gimple-expr.c (extract_ops_from_tree): Likewise.
* gimple.c (get_gimple_rhs_num_ops): Likewise.
* tree-ssa-forwprop.c (rhs_to_tree): Likewise.
---
gcc/cfgexpand.c | 62 ++++++++++++++++++++---------------------
gcc/gimple-expr.c | 59 ++++++++++++++++++++-------------------
gcc/gimple.c | 22 ++++++++-------
gcc/tree-ssa-forwprop.c | 29 ++++++++++---------
4 files changed, 90 insertions(+), 82 deletions(-)
diff --git a/gcc/cfgexpand.c b/gcc/cfgexpand.c
index 5a93447f520..a2f96239e2f 100644
--- a/gcc/cfgexpand.c
+++ b/gcc/cfgexpand.c
@@ -104,38 +104,38 @@ tree
gimple_assign_rhs_to_tree (gimple *stmt)
{
tree t;
- enum gimple_rhs_class grhs_class;
-
- grhs_class = get_gimple_rhs_class (gimple_expr_code (stmt));
-
- if (grhs_class == GIMPLE_TERNARY_RHS)
- t = build3 (gimple_assign_rhs_code (stmt),
- TREE_TYPE (gimple_assign_lhs (stmt)),
- gimple_assign_rhs1 (stmt),
- gimple_assign_rhs2 (stmt),
- gimple_assign_rhs3 (stmt));
- else if (grhs_class == GIMPLE_BINARY_RHS)
- t = build2 (gimple_assign_rhs_code (stmt),
- TREE_TYPE (gimple_assign_lhs (stmt)),
- gimple_assign_rhs1 (stmt),
- gimple_assign_rhs2 (stmt));
- else if (grhs_class == GIMPLE_UNARY_RHS)
- t = build1 (gimple_assign_rhs_code (stmt),
- TREE_TYPE (gimple_assign_lhs (stmt)),
- gimple_assign_rhs1 (stmt));
- else if (grhs_class == GIMPLE_SINGLE_RHS)
- {
- t = gimple_assign_rhs1 (stmt);
- /* Avoid modifying this tree in place below. */
- if ((gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t)
- && gimple_location (stmt) != EXPR_LOCATION (t))
- || (gimple_block (stmt)
- && currently_expanding_to_rtl
- && EXPR_P (t)))
- t = copy_node (t);
+ switch (get_gimple_rhs_class (gimple_expr_code (stmt)))
+ {
+ case GIMPLE_TERNARY_RHS:
+ t = build3 (gimple_assign_rhs_code (stmt),
+ TREE_TYPE (gimple_assign_lhs (stmt)),
+ gimple_assign_rhs1 (stmt), gimple_assign_rhs2 (stmt),
+ gimple_assign_rhs3 (stmt));
+ break;
+ case GIMPLE_BINARY_RHS:
+ t = build2 (gimple_assign_rhs_code (stmt),
+ TREE_TYPE (gimple_assign_lhs (stmt)),
+ gimple_assign_rhs1 (stmt), gimple_assign_rhs2 (stmt));
+ break;
+ case GIMPLE_UNARY_RHS:
+ t = build1 (gimple_assign_rhs_code (stmt),
+ TREE_TYPE (gimple_assign_lhs (stmt)),
+ gimple_assign_rhs1 (stmt));
+ break;
+ case GIMPLE_SINGLE_RHS:
+ {
+ t = gimple_assign_rhs1 (stmt);
+ /* Avoid modifying this tree in place below. */
+ if ((gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t)
+ && gimple_location (stmt) != EXPR_LOCATION (t))
+ || (gimple_block (stmt) && currently_expanding_to_rtl
+ && EXPR_P (t)))
+ t = copy_node (t);
+ break;
+ }
+ default:
+ gcc_unreachable ();
}
- else
- gcc_unreachable ();
if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t))
SET_EXPR_LOCATION (t, gimple_location (stmt));
diff --git a/gcc/gimple-expr.c b/gcc/gimple-expr.c
index b0c9f9b671a..4082828e198 100644
--- a/gcc/gimple-expr.c
+++ b/gcc/gimple-expr.c
@@ -528,37 +528,40 @@ void
extract_ops_from_tree (tree expr, enum tree_code *subcode_p, tree *op1_p,
tree *op2_p, tree *op3_p)
{
- enum gimple_rhs_class grhs_class;
-
*subcode_p = TREE_CODE (expr);
- grhs_class = get_gimple_rhs_class (*subcode_p);
-
- if (grhs_class == GIMPLE_TERNARY_RHS)
- {
- *op1_p = TREE_OPERAND (expr, 0);
- *op2_p = TREE_OPERAND (expr, 1);
- *op3_p = TREE_OPERAND (expr, 2);
- }
- else if (grhs_class == GIMPLE_BINARY_RHS)
- {
- *op1_p = TREE_OPERAND (expr, 0);
- *op2_p = TREE_OPERAND (expr, 1);
- *op3_p = NULL_TREE;
- }
- else if (grhs_class == GIMPLE_UNARY_RHS)
- {
- *op1_p = TREE_OPERAND (expr, 0);
- *op2_p = NULL_TREE;
- *op3_p = NULL_TREE;
- }
- else if (grhs_class == GIMPLE_SINGLE_RHS)
+ switch (get_gimple_rhs_class (*subcode_p))
{
- *op1_p = expr;
- *op2_p = NULL_TREE;
- *op3_p = NULL_TREE;
+ case GIMPLE_TERNARY_RHS:
+ {
+ *op1_p = TREE_OPERAND (expr, 0);
+ *op2_p = TREE_OPERAND (expr, 1);
+ *op3_p = TREE_OPERAND (expr, 2);
+ break;
+ }
+ case GIMPLE_BINARY_RHS:
+ {
+ *op1_p = TREE_OPERAND (expr, 0);
+ *op2_p = TREE_OPERAND (expr, 1);
+ *op3_p = NULL_TREE;
+ break;
+ }
+ case GIMPLE_UNARY_RHS:
+ {
+ *op1_p = TREE_OPERAND (expr, 0);
+ *op2_p = NULL_TREE;
+ *op3_p = NULL_TREE;
+ break;
+ }
+ case GIMPLE_SINGLE_RHS:
+ {
+ *op1_p = expr;
+ *op2_p = NULL_TREE;
+ *op3_p = NULL_TREE;
+ break;
+ }
+ default:
+ gcc_unreachable ();
}
- else
- gcc_unreachable ();
}
/* Extract operands for a GIMPLE_COND statement out of COND_EXPR tree COND. */
diff --git a/gcc/gimple.c b/gcc/gimple.c
index 88250cad16b..af62c8bf477 100644
--- a/gcc/gimple.c
+++ b/gcc/gimple.c
@@ -2225,16 +2225,18 @@ dump_gimple_statistics (void)
unsigned
get_gimple_rhs_num_ops (enum tree_code code)
{
- enum gimple_rhs_class rhs_class = get_gimple_rhs_class (code);
-
- if (rhs_class == GIMPLE_UNARY_RHS || rhs_class == GIMPLE_SINGLE_RHS)
- return 1;
- else if (rhs_class == GIMPLE_BINARY_RHS)
- return 2;
- else if (rhs_class == GIMPLE_TERNARY_RHS)
- return 3;
- else
- gcc_unreachable ();
+ switch (get_gimple_rhs_class (code))
+ {
+ case GIMPLE_UNARY_RHS:
+ case GIMPLE_SINGLE_RHS:
+ return 1;
+ case GIMPLE_BINARY_RHS:
+ return 2;
+ case GIMPLE_TERNARY_RHS:
+ return 3;
+ default:
+ gcc_unreachable ();
+ }
}
#define DEFTREECODE(SYM, STRING, TYPE, NARGS) \
diff --git a/gcc/tree-ssa-forwprop.c b/gcc/tree-ssa-forwprop.c
index c464c899586..221f140b356 100644
--- a/gcc/tree-ssa-forwprop.c
+++ b/gcc/tree-ssa-forwprop.c
@@ -347,19 +347,22 @@ rhs_to_tree (tree type, gimple *stmt)
{
location_t loc = gimple_location (stmt);
enum tree_code code = gimple_assign_rhs_code (stmt);
- if (get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS)
- return fold_build3_loc (loc, code, type, gimple_assign_rhs1 (stmt),
- gimple_assign_rhs2 (stmt),
- gimple_assign_rhs3 (stmt));
- else if (get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS)
- return fold_build2_loc (loc, code, type, gimple_assign_rhs1 (stmt),
- gimple_assign_rhs2 (stmt));
- else if (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS)
- return build1 (code, type, gimple_assign_rhs1 (stmt));
- else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
- return gimple_assign_rhs1 (stmt);
- else
- gcc_unreachable ();
+ switch (get_gimple_rhs_class (code))
+ {
+ case GIMPLE_TERNARY_RHS:
+ return fold_build3_loc (loc, code, type, gimple_assign_rhs1 (stmt),
+ gimple_assign_rhs2 (stmt),
+ gimple_assign_rhs3 (stmt));
+ case GIMPLE_BINARY_RHS:
+ return fold_build2_loc (loc, code, type, gimple_assign_rhs1 (stmt),
+ gimple_assign_rhs2 (stmt));
+ case GIMPLE_UNARY_RHS:
+ return build1 (code, type, gimple_assign_rhs1 (stmt));
+ case GIMPLE_SINGLE_RHS:
+ return gimple_assign_rhs1 (stmt);
+ default:
+ gcc_unreachable ();
+ }
}
/* Combine OP0 CODE OP1 in the context of a COND_EXPR. Returns