From ff6662210913adae846a6fd28932f28bddea0e8a Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Tue, 26 May 2020 16:57:13 -0400 Subject: [PATCH 182/315] FIXME: more on binop simplification --- gcc/analyzer/region-model2.cc | 54 +++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/gcc/analyzer/region-model2.cc b/gcc/analyzer/region-model2.cc index b7efa94cf84..70faab6366f 100644 --- a/gcc/analyzer/region-model2.cc +++ b/gcc/analyzer/region-model2.cc @@ -2960,13 +2960,6 @@ region_model2_manager::maybe_fold_binop (tree type, enum tree_code op, || (arg1->get_type () && FLOAT_TYPE_P (arg1->get_type ()))) return NULL; - /* For commutative ops, put any constant on the RHS. */ - if (cst0 && commutative_tree_code (op)) - { - std::swap (arg0, arg1); - std::swap (cst0, cst1); - } - switch (op) { default: @@ -2976,6 +2969,11 @@ region_model2_manager::maybe_fold_binop (tree type, enum tree_code op, if (cst1 && zerop (cst1) && type == arg0->get_type ()) return arg0; break; + case MINUS_EXPR: + /* (VAL - 0) -> VAL. */ + if (cst1 && zerop (cst1) && type == arg0->get_type ()) + return arg0; + break; case MULT_EXPR: /* (VAL * 0). */ if (cst1 && zerop (cst1)) @@ -2985,6 +2983,30 @@ region_model2_manager::maybe_fold_binop (tree type, enum tree_code op, if (cst1 && integer_onep (cst1)) return arg0; break; + case TRUTH_ANDIF_EXPR: + case TRUTH_AND_EXPR: + if (cst1) + { + if (zerop (cst1)) + /* "(ARG0 && 0)" -> "0". */ + return get_or_create_constant_svalue2 (build_int_cst (type, 0)); + else + /* "(ARG0 && nonzero-cst)" -> "ARG0". */ + return get_or_create_cast (type, arg0); + } + break; + case TRUTH_ORIF_EXPR: + case TRUTH_OR_EXPR: + if (cst1) + { + if (zerop (cst1)) + /* "(ARG0 || 0)" -> "ARG0". */ + return get_or_create_cast (type, arg0); + else + /* "(ARG0 && nonzero-cst)" -> "nonzero-cst". */ + return get_or_create_cast (type, arg1); + } + break; } return NULL; } @@ -2994,8 +3016,13 @@ region_model2_manager::get_or_create_binop (tree type, enum tree_code op, const svalue2 *arg0, const svalue2 *arg1) { + /* For commutative ops, put any constant on the RHS. */ + if (arg0->maybe_get_constant () && commutative_tree_code (op)) + std::swap (arg0, arg1); + if (const svalue2 *folded = maybe_fold_binop (type, op, arg0, arg1)) return folded; + binop_svalue2::key_t key (type, op, arg0, arg1); if (binop_svalue2 **slot = m_binop_values_map.get (key)) return *slot; @@ -8266,7 +8293,18 @@ test_binop_svalue2_folding () ASSERT_EQ (x_init_plus_one_plus_one, x_init_plus_two); #endif - /* FIXME: do we want to canonicalize binops to put constants on the RHS ? */ + /* Verify that binops put any constants on the RHS. */ + const svalue2 *four_times_x_init + = mgr.get_or_create_binop (integer_type_node, MULT_EXPR, + cst_sval[4], x_init); + const svalue2 *x_init_times_four + = mgr.get_or_create_binop (integer_type_node, MULT_EXPR, + x_init, cst_sval[4]); + ASSERT_EQ (four_times_x_init, x_init_times_four); + const binop_svalue2 *binop = four_times_x_init->dyn_cast_binop_svalue2 (); + ASSERT_EQ (binop->get_op (), MULT_EXPR); + ASSERT_EQ (binop->get_arg0 (), x_init); + ASSERT_EQ (binop->get_arg1 (), cst_sval[4]); } /* Verify that sub_svalue2s are folded as expected. */ -- 2.26.2