diff options
Diffstat (limited to 'gcc/tree-ssa-forwprop.cc')
-rw-r--r-- | gcc/tree-ssa-forwprop.cc | 247 |
1 files changed, 209 insertions, 38 deletions
diff --git a/gcc/tree-ssa-forwprop.cc b/gcc/tree-ssa-forwprop.cc index fafc4d6..4c048a9 100644 --- a/gcc/tree-ssa-forwprop.cc +++ b/gcc/tree-ssa-forwprop.cc @@ -523,6 +523,14 @@ forward_propagate_into_comparison (gimple_stmt_iterator *gsi) type, rhs1, rhs2); if (tmp && useless_type_conversion_p (type, TREE_TYPE (tmp))) { + if (dump_file) + { + fprintf (dump_file, " Replaced '"); + print_gimple_expr (dump_file, stmt, 0); + fprintf (dump_file, "' with '"); + print_generic_expr (dump_file, tmp); + fprintf (dump_file, "'\n"); + } gimple_assign_set_rhs_from_tree (gsi, tmp); fold_stmt (gsi); update_stmt (gsi_stmt (*gsi)); @@ -551,9 +559,8 @@ forward_propagate_into_gimple_cond (gcond *stmt) tree rhs1 = gimple_cond_lhs (stmt); tree rhs2 = gimple_cond_rhs (stmt); - /* We can do tree combining on SSA_NAME and comparison expressions. */ - if (TREE_CODE_CLASS (gimple_cond_code (stmt)) != tcc_comparison) - return 0; + /* GIMPLE_COND will always be a comparison. */ + gcc_assert (TREE_CODE_CLASS (gimple_cond_code (stmt)) == tcc_comparison); tmp = forward_propagate_into_comparison_1 (stmt, code, boolean_type_node, @@ -580,22 +587,8 @@ forward_propagate_into_gimple_cond (gcond *stmt) return (cfg_changed || is_gimple_min_invariant (tmp)) ? 2 : 1; } - /* Canonicalize _Bool == 0 and _Bool != 1 to _Bool != 0 by swapping edges. */ - if ((TREE_CODE (TREE_TYPE (rhs1)) == BOOLEAN_TYPE - || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)) - && TYPE_PRECISION (TREE_TYPE (rhs1)) == 1)) - && ((code == EQ_EXPR - && integer_zerop (rhs2)) - || (code == NE_EXPR - && integer_onep (rhs2)))) - { - basic_block bb = gimple_bb (stmt); - gimple_cond_set_code (stmt, NE_EXPR); - gimple_cond_set_rhs (stmt, build_zero_cst (TREE_TYPE (rhs1))); - EDGE_SUCC (bb, 0)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE); - EDGE_SUCC (bb, 1)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE); - return 1; - } + if (canonicalize_bool_cond (stmt, gimple_bb (stmt))) + return 1; return 0; } @@ -1198,6 +1191,157 @@ constant_pointer_difference (tree p1, tree p2) return NULL_TREE; } + +/* Optimize + a = {}; + b = a; + into + a = {}; + b = {}; + Similarly for memset (&a, ..., sizeof (a)); instead of a = {}; + and/or memcpy (&b, &a, sizeof (a)); instead of b = a; */ + +static bool +optimize_memcpy_to_memset (gimple_stmt_iterator *gsip, tree dest, tree src, tree len) +{ + ao_ref read; + gimple *stmt = gsi_stmt (*gsip); + if (gimple_has_volatile_ops (stmt)) + return false; + + tree src2 = NULL_TREE, len2 = NULL_TREE; + poly_int64 offset, offset2; + tree val = integer_zero_node; + bool len_was_null = len == NULL_TREE; + if (len == NULL_TREE) + len = (TREE_CODE (src) == COMPONENT_REF + ? DECL_SIZE_UNIT (TREE_OPERAND (src, 1)) + : TYPE_SIZE_UNIT (TREE_TYPE (src))); + if (len == NULL_TREE + || !poly_int_tree_p (len)) + return false; + + ao_ref_init (&read, src); + tree vuse = gimple_vuse (stmt); + gimple *defstmt; + unsigned limit = param_sccvn_max_alias_queries_per_access; + do { + if (vuse == NULL || TREE_CODE (vuse) != SSA_NAME) + return false; + defstmt = SSA_NAME_DEF_STMT (vuse); + if (is_a <gphi*>(defstmt)) + return false; + if (limit-- == 0) + return false; + /* If the len was null, then we can use TBBA. */ + if (stmt_may_clobber_ref_p_1 (defstmt, &read, + /* tbaa_p = */ len_was_null)) + break; + vuse = gimple_vuse (defstmt); + } while (true); + + if (gimple_store_p (defstmt) + && gimple_assign_single_p (defstmt) + && TREE_CODE (gimple_assign_rhs1 (defstmt)) == STRING_CST + && !gimple_clobber_p (defstmt)) + { + tree str = gimple_assign_rhs1 (defstmt); + src2 = gimple_assign_lhs (defstmt); + /* The string must contain all null char's for now. */ + for (int i = 0; i < TREE_STRING_LENGTH (str); i++) + { + if (TREE_STRING_POINTER (str)[i] != 0) + { + src2 = NULL_TREE; + break; + } + } + } + else if (gimple_store_p (defstmt) + && gimple_assign_single_p (defstmt) + && TREE_CODE (gimple_assign_rhs1 (defstmt)) == CONSTRUCTOR + && !gimple_clobber_p (defstmt)) + src2 = gimple_assign_lhs (defstmt); + else if (gimple_call_builtin_p (defstmt, BUILT_IN_MEMSET) + && TREE_CODE (gimple_call_arg (defstmt, 0)) == ADDR_EXPR + && TREE_CODE (gimple_call_arg (defstmt, 1)) == INTEGER_CST) + { + src2 = TREE_OPERAND (gimple_call_arg (defstmt, 0), 0); + len2 = gimple_call_arg (defstmt, 2); + val = gimple_call_arg (defstmt, 1); + /* For non-0 val, we'd have to transform stmt from assignment + into memset (only if dest is addressable). */ + if (!integer_zerop (val) && is_gimple_assign (stmt)) + src2 = NULL_TREE; + } + + if (src2 == NULL_TREE) + return false; + + if (len2 == NULL_TREE) + len2 = (TREE_CODE (src2) == COMPONENT_REF + ? DECL_SIZE_UNIT (TREE_OPERAND (src2, 1)) + : TYPE_SIZE_UNIT (TREE_TYPE (src2))); + if (len2 == NULL_TREE + || !poly_int_tree_p (len2)) + return false; + + src = get_addr_base_and_unit_offset (src, &offset); + src2 = get_addr_base_and_unit_offset (src2, &offset2); + if (src == NULL_TREE + || src2 == NULL_TREE + || maybe_lt (offset, offset2)) + return false; + + if (!operand_equal_p (src, src2, 0)) + return false; + + /* [ src + offset2, src + offset2 + len2 - 1 ] is set to val. + Make sure that + [ src + offset, src + offset + len - 1 ] is a subset of that. */ + if (maybe_gt (wi::to_poly_offset (len) + (offset - offset2), + wi::to_poly_offset (len2))) + return false; + + if (dump_file && (dump_flags & TDF_DETAILS)) + { + fprintf (dump_file, "Simplified\n "); + print_gimple_stmt (dump_file, stmt, 0, dump_flags); + fprintf (dump_file, "after previous\n "); + print_gimple_stmt (dump_file, defstmt, 0, dump_flags); + } + + /* For simplicity, don't change the kind of the stmt, + turn dest = src; into dest = {}; and memcpy (&dest, &src, len); + into memset (&dest, val, len); + In theory we could change dest = src into memset if dest + is addressable (maybe beneficial if val is not 0), or + memcpy (&dest, &src, len) into dest = {} if len is the size + of dest, dest isn't volatile. */ + if (is_gimple_assign (stmt)) + { + tree ctor = build_constructor (TREE_TYPE (dest), NULL); + gimple_assign_set_rhs_from_tree (gsip, ctor); + update_stmt (stmt); + } + else /* If stmt is memcpy, transform it into memset. */ + { + gcall *call = as_a <gcall *> (stmt); + tree fndecl = builtin_decl_implicit (BUILT_IN_MEMSET); + gimple_call_set_fndecl (call, fndecl); + gimple_call_set_fntype (call, TREE_TYPE (fndecl)); + gimple_call_set_arg (call, 1, val); + update_stmt (stmt); + } + + if (dump_file && (dump_flags & TDF_DETAILS)) + { + fprintf (dump_file, "into\n "); + print_gimple_stmt (dump_file, stmt, 0, dump_flags); + } + return true; +} + /* *GSI_P is a GIMPLE_CALL to a builtin function. Optimize memcpy (p, "abcd", 4); @@ -1235,6 +1379,22 @@ simplify_builtin_call (gimple_stmt_iterator *gsi_p, tree callee2) switch (DECL_FUNCTION_CODE (callee2)) { + case BUILT_IN_MEMCPY: + if (gimple_call_num_args (stmt2) == 3) + { + tree dest = gimple_call_arg (stmt2, 0); + tree src = gimple_call_arg (stmt2, 1); + tree len = gimple_call_arg (stmt2, 2); + /* Try to optimize the memcpy to memset if src + and dest are addresses. */ + if (TREE_CODE (dest) == ADDR_EXPR + && TREE_CODE (src) == ADDR_EXPR + && TREE_CODE (len) == INTEGER_CST + && optimize_memcpy_to_memset (gsi_p, TREE_OPERAND (dest, 0), + TREE_OPERAND (src, 0), len)) + return true; + } + break; case BUILT_IN_MEMCHR: if (gimple_call_num_args (stmt2) == 3 && (res = gimple_call_lhs (stmt2)) != nullptr @@ -4497,6 +4657,8 @@ pass_forwprop::execute (function *fun) } } } + if (substituted_p) + update_stmt (stmt); if (substituted_p && is_gimple_assign (stmt) && gimple_assign_rhs_code (stmt) == ADDR_EXPR) @@ -4536,17 +4698,7 @@ pass_forwprop::execute (function *fun) && !SSA_NAME_IS_DEFAULT_DEF (use)) bitmap_set_bit (simple_dce_worklist, SSA_NAME_VERSION (use)); - } - - if (changed || substituted_p) - { - if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt)) - bitmap_set_bit (to_purge, bb->index); - if (!was_noreturn - && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt)) - to_fixup.safe_push (stmt); update_stmt (stmt); - substituted_p = false; } switch (gimple_code (stmt)) @@ -4555,16 +4707,25 @@ pass_forwprop::execute (function *fun) { tree rhs1 = gimple_assign_rhs1 (stmt); enum tree_code code = gimple_assign_rhs_code (stmt); + if (gimple_assign_load_p (stmt) && gimple_store_p (stmt)) + { + if (optimize_memcpy_to_memset (&gsi, + gimple_assign_lhs (stmt), + gimple_assign_rhs1 (stmt), + /* len = */NULL_TREE)) + { + changed = true; + break; + } + } if (TREE_CODE_CLASS (code) == tcc_comparison) { int did_something; did_something = forward_propagate_into_comparison (&gsi); - if (maybe_clean_or_replace_eh_stmt (stmt, gsi_stmt (gsi))) - bitmap_set_bit (to_purge, bb->index); if (did_something == 2) cfg_changed = true; - changed = did_something != 0; + changed |= did_something != 0; } else if ((code == PLUS_EXPR || code == BIT_IOR_EXPR @@ -4580,15 +4741,15 @@ pass_forwprop::execute (function *fun) } else if (code == CONSTRUCTOR && TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE) - changed = simplify_vector_constructor (&gsi); + changed |= simplify_vector_constructor (&gsi); else if (code == ARRAY_REF) - changed = simplify_count_trailing_zeroes (&gsi); + changed |= simplify_count_trailing_zeroes (&gsi); break; } case GIMPLE_SWITCH: - changed = simplify_gimple_switch (as_a <gswitch *> (stmt), - edges_to_remove); + changed |= simplify_gimple_switch (as_a <gswitch *> (stmt), + edges_to_remove); break; case GIMPLE_COND: @@ -4597,7 +4758,7 @@ pass_forwprop::execute (function *fun) (as_a <gcond *> (stmt)); if (did_something == 2) cfg_changed = true; - changed = did_something != 0; + changed |= did_something != 0; break; } @@ -4606,13 +4767,23 @@ pass_forwprop::execute (function *fun) tree callee = gimple_call_fndecl (stmt); if (callee != NULL_TREE && fndecl_built_in_p (callee, BUILT_IN_NORMAL)) - changed = simplify_builtin_call (&gsi, callee); + changed |= simplify_builtin_call (&gsi, callee); break; } default:; } + if (changed || substituted_p) + { + substituted_p = false; + stmt = gsi_stmt (gsi); + if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt)) + bitmap_set_bit (to_purge, bb->index); + if (!was_noreturn + && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt)) + to_fixup.safe_push (stmt); + } if (changed) { /* If the stmt changed then re-visit it and the statements |