https://gcc.gnu.org/g:9961d5f117c4afe323dc1be8b0df9031b28265e4
commit r17-6280-g9961d5f117c4afe323dc1be8b0df9031b28265e4 Author: Jan Hubicka <[email protected]> Date: Sat Nov 14 23:33:22 2026 +0100 Improve nonconstant predicates and PHI walking for unknown-offset loads This makes the testcase from PR ipa/127171 to be analyzed correctly when dispatch_impl is marked always_inline. There are few changes needed: 1) Extend will_be_nonconstant_predicate to handle comparisons and switches on aggregate parameter loads with unknown_offset or maybe_modified, and use it in phi_result_unknown_predicate for both GIMPLE_COND and GIMPLE_SWITCH. Fix the inverted unknown_offset check when simplifying complementary clauses in ipa_predicate::add_clause. 2) Ignore unreachable null-pointer operands when walking pointer definitions in ipa_load_from_parm_agg_unknown_offset when -fdelete-null-pointer-checks is active and address zero is invalid 3) Raise the default -param=ipa-jump-function-lookups limit from 8 to 64 so deeper pointer chains across loops and inlined helpers can be tracked. There is quite complex loop and we need to skip all PHIs of the parameter. If this becomes a problem, it is also possible to remember the results of earlier analysis. Finally I also noticed last minute change bug I introduced to ipa-predicate and fixed it too. For actual testcase to work we sadly still need more work, since display_impl takes the pointer to pointer as parameter and adjust it. We don't have return functions and here we would need return function for value passed by refernece. gcc/ChangeLog: PR ipa/127171 * ipa-fnsummary.cc (compute_switch_edge_predicates): Break out out from set_switch_stmt_execution_predicate; support computing per-edge predicates for nonconstant evaluation. (set_switch_stmt_execution_predicate): Use compute_switch_edge_predicates. (will_be_nonconstant_predicate): Handle comparisons and switches on aggregate loads with unknown_offset or maybe_modified. (phi_result_unknown_predicate): Handle GIMPLE_SWITCH and use will_be_nonconstant_predicate. * ipa-predicate.cc (ipa_predicate::add_clause): Fix inverted unknown_offset check when matching complementary conditions. * ipa-prop.cc (ipa_load_from_parm_agg_unknown_offset): Skip null pointer operands when null pointer dereferences are invalid and require matching address spaces on pointer conversions. * params.opt (-param=ipa-jump-function-lookups=): Increase default from 8 to 64. gcc/testsuite/ChangeLog: PR ipa/127171 * gcc.dg/ipa/fnsummary-11.c: New test. * gcc.dg/ipa/fnsummary-12.c: New test. * gcc.dg/ipa/fnsummary-13.c: New test. Assisted-by: Google Carbon Diff: --- gcc/ipa-fnsummary.cc | 324 +++++++++++++++++++++++--------- gcc/ipa-predicate.cc | 2 +- gcc/ipa-prop.cc | 26 ++- gcc/params.opt | 2 +- gcc/testsuite/gcc.dg/ipa/fnsummary-11.c | 278 +++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/ipa/fnsummary-12.c | 184 ++++++++++++++++++ gcc/testsuite/gcc.dg/ipa/fnsummary-13.c | 126 +++++++++++++ 7 files changed, 848 insertions(+), 94 deletions(-) diff --git a/gcc/ipa-fnsummary.cc b/gcc/ipa-fnsummary.cc index e194a8ca3d93..f06d0999eaaf 100644 --- a/gcc/ipa-fnsummary.cc +++ b/gcc/ipa-fnsummary.cc @@ -2088,14 +2088,20 @@ set_cond_stmt_execution_predicate (struct ipa_func_body_info *fbi, } -/* If BB ends by a switch we can turn into predicates, attach corresponding - predicates to the CFG edges. */ +/* Compute predicates for outgoing edges of switch statement SWTCH in BB. + When FOR_EXECUTION_P is true, bypass computing predicates for edges whose + destination post-dominates BB (since BB's execution predicate already implies + execution of its post-dominators). Populate *EDGE_PREDS with one predicate + per edge in BB->succs and return true on success. */ -static void -set_switch_stmt_execution_predicate (struct ipa_func_body_info *fbi, - class ipa_fn_summary *summary, - class ipa_node_params *params_summary, - basic_block bb) +static bool +compute_switch_edge_predicates (struct ipa_func_body_info *fbi, + class ipa_fn_summary *summary, + class ipa_node_params *params_summary, + basic_block bb, + gswitch *swtch, + bool for_execution_p, + vec<ipa_predicate> *edge_preds) { tree op; int index; @@ -2107,13 +2113,10 @@ set_switch_stmt_execution_predicate (struct ipa_func_body_info *fbi, tree param_type; expr_eval_ops param_ops; - gswitch *last = safe_dyn_cast <gswitch *> (*gsi_last_bb (bb)); - if (!last) - return; - op = gimple_switch_index (last); - if (!decompose_param_expr (fbi, last, op, &index, ¶m_type, &aggpos, + op = gimple_switch_index (swtch); + if (!decompose_param_expr (fbi, swtch, op, &index, ¶m_type, &aggpos, ¶m_ops)) - return; + return false; auto_vec<std::pair<tree, tree> > ranges; tree type = TREE_TYPE (op); @@ -2135,29 +2138,34 @@ set_switch_stmt_execution_predicate (struct ipa_func_body_info *fbi, wide_int vr_wmin = wi::to_wide (vr_min); wide_int vr_wmax = wi::to_wide (vr_max); + hash_map<edge, unsigned> edge_map; + unsigned edge_idx = 0; FOR_EACH_EDGE (e, ei, bb->succs) { - e->aux = edge_predicate_pool.allocate (); - *(ipa_predicate *) e->aux = false; + edge_map.put (e, edge_idx++); + edge_preds->safe_push (false); } - e = gimple_switch_edge (cfun, last, 0); + edge default_edge = gimple_switch_edge (cfun, swtch, 0); /* Set BOUND_COUNT to maximum count to bypass computing predicate for default case if its target basic block is in convergence point of all switch cases, which can be determined by checking whether it post-dominates the switch statement. */ - if (dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest)) + if (for_execution_p + && dominated_by_p (CDI_POST_DOMINATORS, bb, default_edge->dest)) bound_count = INT_MAX; - n = gimple_switch_num_labels (last); + n = gimple_switch_num_labels (swtch); for (case_idx = 1; case_idx < n; ++case_idx) { - tree cl = gimple_switch_label (last, case_idx); + tree cl = gimple_switch_label (swtch, case_idx); tree min = CASE_LOW (cl); tree max = CASE_HIGH (cl); ipa_predicate p; - e = gimple_switch_edge (cfun, last, case_idx); + e = gimple_switch_edge (cfun, swtch, case_idx); + if (!for_execution_p && e == default_edge) + continue; /* The case value might not have same type as switch expression, extend the value based on the expression type. */ @@ -2172,22 +2180,24 @@ set_switch_stmt_execution_predicate (struct ipa_func_body_info *fbi, /* The case's target basic block is in convergence point of all switch cases, its predicate should be at least as that of the switch statement. */ - if (dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest)) + if (for_execution_p && dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest)) p = true; else if (min == max) p = add_condition (summary, params_summary, index, param_type, - &aggpos, EQ_EXPR, min, param_ops); + &aggpos, EQ_EXPR, min, param_ops); else { ipa_predicate p1, p2; p1 = add_condition (summary, params_summary, index, param_type, &aggpos, GE_EXPR, min, param_ops); - p2 = add_condition (summary, params_summary,index, param_type, + p2 = add_condition (summary, params_summary, index, param_type, &aggpos, LE_EXPR, max, param_ops); p = p1 & p2; } - *(ipa_predicate *) e->aux - = p.or_with (summary->conds, *(ipa_predicate *) e->aux); + unsigned *slot = edge_map.get (e); + gcc_checking_assert (slot); + (*edge_preds)[*slot] + = p.or_with (summary->conds, (*edge_preds)[*slot]); /* If there are too many disjoint case ranges, predicate for default case might become too complicated. So add a limit here. */ @@ -2230,74 +2240,106 @@ set_switch_stmt_execution_predicate (struct ipa_func_body_info *fbi, } } - e = gimple_switch_edge (cfun, last, 0); + unsigned default_slot = *edge_map.get (default_edge); if (bound_count > bound_limit) + (*edge_preds)[default_slot] = true; + else { - *(ipa_predicate *) e->aux = true; - vec_free (param_ops); - return; - } + ipa_predicate p_seg = true; + ipa_predicate p_all = false; - ipa_predicate p_seg = true; - ipa_predicate p_all = false; - - if (vr_type != VR_RANGE) - { - vr_wmin = wi::to_wide (TYPE_MIN_VALUE (type)); - vr_wmax = wi::to_wide (TYPE_MAX_VALUE (type)); - } + if (vr_type != VR_RANGE) + { + vr_wmin = wi::to_wide (TYPE_MIN_VALUE (type)); + vr_wmax = wi::to_wide (TYPE_MAX_VALUE (type)); + } - /* Construct predicate to represent default range set that is negation of - all case ranges. Case range is classified as containing single/non-single - values. Suppose a piece of case ranges in the following. + /* Construct predicate to represent default range set that is negation of + all case ranges. Case range is classified as containing + single/non-single values. Suppose a piece of case ranges in the + following. - [D1...D2] [S1] ... [Sn] [D3...D4] + [D1...D2] [S1] ... [Sn] [D3...D4] - To represent default case's range sets between two non-single value - case ranges (From D2 to D3), we construct predicate as: + To represent default case's range sets between two non-single value + case ranges (From D2 to D3), we construct predicate as: - D2 < x < D3 && x != S1 && ... && x != Sn - */ - for (size_t i = 0; i < ranges.length (); i++) - { - tree min = ranges[i].first; - tree max = ranges[i].second; - - if (min == max) - p_seg &= add_condition (summary, params_summary, index, - param_type, &aggpos, NE_EXPR, - min, param_ops); - else + D2 < x < D3 && x != S1 && ... && x != Sn + */ + for (size_t i = 0; i < ranges.length (); i++) { - /* Do not create sub-predicate for range that is beyond low bound - of switch index. */ - if (wi::lt_p (vr_wmin, wi::to_wide (min), TYPE_SIGN (type))) - { - p_seg &= add_condition (summary, params_summary, index, - param_type, &aggpos, - LT_EXPR, min, param_ops); - p_all = p_all.or_with (summary->conds, p_seg); - } + tree min = ranges[i].first; + tree max = ranges[i].second; - /* Do not create sub-predicate for range that is beyond up bound - of switch index. */ - if (wi::le_p (vr_wmax, wi::to_wide (max), TYPE_SIGN (type))) + if (min == max) + p_seg &= add_condition (summary, params_summary, index, + param_type, &aggpos, NE_EXPR, + min, param_ops); + else { - p_seg = false; - break; - } + /* Do not create sub-predicate for range that is beyond low + bound of switch index. */ + if (wi::lt_p (vr_wmin, wi::to_wide (min), TYPE_SIGN (type))) + { + p_seg &= add_condition (summary, params_summary, index, + param_type, &aggpos, + LT_EXPR, min, param_ops); + p_all = p_all.or_with (summary->conds, p_seg); + } + + /* Do not create sub-predicate for range that is beyond up bound + of switch index. */ + if (wi::le_p (vr_wmax, wi::to_wide (max), TYPE_SIGN (type))) + { + p_seg = false; + break; + } - p_seg = add_condition (summary, params_summary, index, - param_type, &aggpos, GT_EXPR, - max, param_ops); + p_seg = add_condition (summary, params_summary, index, + param_type, &aggpos, GT_EXPR, + max, param_ops); + } } + + p_all = p_all.or_with (summary->conds, p_seg); + (*edge_preds)[default_slot] + = p_all.or_with (summary->conds, (*edge_preds)[default_slot]); } - p_all = p_all.or_with (summary->conds, p_seg); - *(ipa_predicate *) e->aux - = p_all.or_with (summary->conds, *(ipa_predicate *) e->aux); + if (!for_execution_p && default_slot != 0) + std::swap ((*edge_preds)[0], (*edge_preds)[default_slot]); vec_free (param_ops); + return true; +} + + +/* If BB ends by a switch we can turn into predicates, attach corresponding + predicates to the CFG edges. */ + +static void +set_switch_stmt_execution_predicate (struct ipa_func_body_info *fbi, + class ipa_fn_summary *summary, + class ipa_node_params *params_summary, + basic_block bb) +{ + gswitch *last = safe_dyn_cast <gswitch *> (*gsi_last_bb (bb)); + if (!last) + return; + + auto_vec<ipa_predicate, 8> edge_preds; + if (!compute_switch_edge_predicates (fbi, summary, params_summary, + bb, last, true, &edge_preds)) + return; + + edge e; + edge_iterator ei; + unsigned edge_idx = 0; + FOR_EACH_EDGE (e, ei, bb->succs) + { + e->aux = edge_predicate_pool.allocate (); + *(ipa_predicate *) e->aux = edge_preds[edge_idx++]; + } } @@ -2514,7 +2556,10 @@ will_be_nonconstant_predicate (struct ipa_func_body_info *fbi, is_load = gimple_assign_load_p (stmt); - /* Loads can be optimized when the value is known. */ + /* Loads can be optimized when the value is known. An unknown-offset load + itself can still read different values across offsets or iterations, so + leave nonconstant_names true for it; comparisons of it against an + invariant are handled below. */ if (is_load) { tree op = gimple_assign_rhs1 (stmt); @@ -2528,6 +2573,7 @@ will_be_nonconstant_predicate (struct ipa_func_body_info *fbi, /* See if we understand all operands before we start adding conditionals. */ + bool understood = true; FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE) { tree parm = unmodified_parm (fbi, stmt, use, NULL); @@ -2535,12 +2581,114 @@ will_be_nonconstant_predicate (struct ipa_func_body_info *fbi, if (parm && ipa_get_param_decl_index (fbi->info, parm) >= 0) continue; if (TREE_CODE (use) != SSA_NAME) - return p; + { + understood = false; + break; + } /* If we know when operand is constant, we still can say something useful. */ if (nonconstant_names[SSA_NAME_VERSION (use)] != true) continue; - return p; + understood = false; + break; + } + + /* Even if an operand itself is non-constant (such as a load at an unknown + or maybe-modified offset), a comparison against an IP-invariant or a + switch is constant whenever at most one outcome is possible. */ + if (!understood) + { + if (gswitch *swtch = dyn_cast <gswitch *> (stmt)) + { + auto_vec<ipa_predicate, 8> edge_preds; + if (!compute_switch_edge_predicates (fbi, summary, params_summary, + gimple_bb (swtch), swtch, + false, &edge_preds)) + return p; + + unsigned n_edges = edge_preds.length (); + if (n_edges < 2) + return false; + + unsigned n_true = 0; + for (unsigned i = 0; i < n_edges; i++) + if (edge_preds[i] == true && ++n_true >= 2) + return p; + + /* Switch is non-constant when at least two distinct outgoing edges + may be taken, i.e. for each edge m some edge j != m is not + excluded. */ + auto_vec<ipa_predicate, 8> suffix_or; + suffix_or.safe_grow_cleared (n_edges + 1, true); + suffix_or[n_edges] = false; + for (unsigned i = n_edges; i-- > 0;) + suffix_or[i] + = suffix_or[i + 1].or_with (summary->conds, edge_preds[i]); + + ipa_predicate prefix_or = false; + op_non_const = true; + for (unsigned m = 0; m < n_edges; m++) + { + ipa_predicate others + = prefix_or.or_with (summary->conds, suffix_or[m + 1]); + op_non_const &= others; + prefix_or = prefix_or.or_with (summary->conds, edge_preds[m]); + } + return op_non_const; + } + + tree lhs, rhs; + enum tree_code code; + if (gcond *cond = dyn_cast <gcond *> (stmt)) + { + code = gimple_cond_code (cond); + lhs = gimple_cond_lhs (cond); + rhs = gimple_cond_rhs (cond); + } + else if (gimple_code (stmt) == GIMPLE_ASSIGN + && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) + == tcc_comparison) + { + code = gimple_assign_rhs_code (stmt); + lhs = gimple_assign_rhs1 (stmt); + rhs = gimple_assign_rhs2 (stmt); + } + else + return p; + + if (!is_gimple_ip_invariant (rhs) && is_gimple_ip_invariant (lhs)) + { + std::swap (lhs, rhs); + code = swap_tree_comparison (code); + } + + expr_eval_ops param_ops = NULL; + if (!is_gimple_ip_invariant (rhs) + || !decompose_param_expr (fbi, stmt, lhs, &base_index, ¶m_type, + &aggpos, ¶m_ops)) + return p; + + enum tree_code inverted_code + = invert_tree_comparison (code, HONOR_NANS (lhs)); + if (inverted_code == ERROR_MARK) + { + vec_free (param_ops); + return p; + } + + ipa_predicate p1 + = add_condition (summary, params_summary, base_index, param_type, + &aggpos, code, rhs, param_ops); + ipa_predicate p2 + = add_condition (summary, params_summary, base_index, param_type, + &aggpos, inverted_code, rhs, param_ops); + op_non_const = p1 & p2; + vec_free (param_ops); + if (gimple_code (stmt) == GIMPLE_ASSIGN + && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME) + nonconstant_names[SSA_NAME_VERSION (gimple_assign_lhs (stmt))] + = op_non_const; + return op_non_const; } if (is_load) @@ -2745,9 +2893,9 @@ param_change_prob (ipa_func_body_info *fbi, gimple *stmt, int i) } } -/* Find whether a basic block BB is the final block of a (half) diamond CFG - sub-graph and if the predicate the condition depends on is known. If so, - return true and store the pointer the predicate in *P. */ +/* Find whether a basic block BB is the final block of a (half) diamond or + switch CFG sub-graph and if the predicate the condition depends on is known. + If so, return true and store the predicate in *P. */ static bool phi_result_unknown_predicate (ipa_func_body_info *fbi, @@ -2790,14 +2938,14 @@ phi_result_unknown_predicate (ipa_func_body_info *fbi, if (!first_bb) return false; - gcond *stmt = safe_dyn_cast <gcond *> (*gsi_last_bb (first_bb)); + gimple *stmt = *gsi_last_bb (first_bb); if (!stmt - || !is_gimple_ip_invariant (gimple_cond_rhs (stmt))) + || (gimple_code (stmt) != GIMPLE_COND + && gimple_code (stmt) != GIMPLE_SWITCH)) return false; - *p = will_be_nonconstant_expr_predicate (fbi, summary, params_summary, - gimple_cond_lhs (stmt), - nonconstant_names); + *p = will_be_nonconstant_predicate (fbi, summary, params_summary, + stmt, nonconstant_names); if (*p == true) return false; else diff --git a/gcc/ipa-predicate.cc b/gcc/ipa-predicate.cc index 6a7ba6cb77a1..452c1e5ecc10 100644 --- a/gcc/ipa-predicate.cc +++ b/gcc/ipa-predicate.cc @@ -151,7 +151,7 @@ ipa_predicate::add_clause (conditions conditions, clause_t new_clause) && cc2->by_ref == cc1->by_ref && (!cc1->agg_contents || (cc1->unknown_offset == cc2->unknown_offset - && (!cc1->unknown_offset || cc2->offset == cc1->offset) + && (cc1->unknown_offset || cc2->offset == cc1->offset) && cc2->maybe_modified == cc1->maybe_modified)) && types_compatible_p (cc2->type, cc1->type) && cc1->code == invert_tree_comparison (cc2->code, diff --git a/gcc/ipa-prop.cc b/gcc/ipa-prop.cc index f762410d0c09..23fbfd29f2f7 100644 --- a/gcc/ipa-prop.cc +++ b/gcc/ipa-prop.cc @@ -21,6 +21,7 @@ along with GCC; see the file COPYING3. If not see #include "system.h" #include "coretypes.h" #include "backend.h" +#include "target.h" #include "rtl.h" #include "tree.h" #include "gimple.h" @@ -1313,9 +1314,20 @@ ipa_load_from_parm_agg_unknown_offset (struct ipa_func_body_info *fbi, if (TREE_CODE (start_ptr) != SSA_NAME) return false; + bool null_invalid_p + = ((fbi && fbi->node + ? opt_for_fn (fbi->node->decl, flag_delete_null_pointer_checks) + : flag_delete_null_pointer_checks) + && !targetm.addr_space.zero_address_valid + (TYPE_ADDR_SPACE (TREE_TYPE (base)))); + /* Walk backwards through pointer adjustments, copies, conversions and PHIs, verifying that all reachable definitions originate from the same function - parameter. */ + parameter. Since BASE is dereferenced at STMT and POINTER_PLUS_EXPR, + same-address-space conversions and PHIs preserve null provenance, + integer_zerop operands cannot reach a valid dereference when + NULL_INVALID_P holds and can be skipped (FOUND_INDEX < 0 still ensures + at least one parameter path exists). */ auto_vec<tree, 8> worklist; hash_set<int_hash<unsigned, 0>> visited; visited.add (SSA_NAME_VERSION (start_ptr)); @@ -1354,6 +1366,8 @@ ipa_load_from_parm_agg_unknown_offset (struct ipa_func_body_info *fbi, for (unsigned int i = 0; i < n; i++) { tree arg = gimple_phi_arg_def (phi, i); + if (null_invalid_p && integer_zerop (arg)) + continue; if (TREE_CODE (arg) != SSA_NAME) return false; if (!visited.add (SSA_NAME_VERSION (arg))) @@ -1370,6 +1384,8 @@ ipa_load_from_parm_agg_unknown_offset (struct ipa_func_body_info *fbi, && POINTER_TYPE_P (TREE_TYPE (ptr)) && POINTER_TYPE_P (TREE_TYPE (rhs1)))) { + if (null_invalid_p && integer_zerop (rhs1)) + continue; if (TREE_CODE (rhs1) != SSA_NAME) return false; if (!visited.add (SSA_NAME_VERSION (rhs1))) @@ -1378,11 +1394,13 @@ ipa_load_from_parm_agg_unknown_offset (struct ipa_func_body_info *fbi, else if (rhs_code == ADDR_EXPR) { tree inner = get_base_address (TREE_OPERAND (rhs1, 0)); - if (!inner - || TREE_CODE (inner) != MEM_REF - || TREE_CODE (TREE_OPERAND (inner, 0)) != SSA_NAME) + if (!inner || TREE_CODE (inner) != MEM_REF) return false; tree next = TREE_OPERAND (inner, 0); + if (null_invalid_p && integer_zerop (next)) + continue; + if (TREE_CODE (next) != SSA_NAME) + return false; if (!visited.add (SSA_NAME_VERSION (next))) worklist.safe_push (next); } diff --git a/gcc/params.opt b/gcc/params.opt index b1be4339bb67..e6a53950acfd 100644 --- a/gcc/params.opt +++ b/gcc/params.opt @@ -302,7 +302,7 @@ Common Joined UInteger Var(param_ipa_cp_value_list_size) Init(8) Param Optimizat Maximum size of a list of values associated with each parameter for interprocedural constant propagation. -param=ipa-jump-function-lookups= -Common Joined UInteger Var(param_ipa_jump_function_lookups) Init(8) Param Optimization +Common Joined UInteger Var(param_ipa_jump_function_lookups) Init(64) Param Optimization Maximum number of statements visited during jump function offset discovery. -param=ipa-max-aa-steps= diff --git a/gcc/testsuite/gcc.dg/ipa/fnsummary-11.c b/gcc/testsuite/gcc.dg/ipa/fnsummary-11.c new file mode 100644 index 000000000000..ab060e267ed8 --- /dev/null +++ b/gcc/testsuite/gcc.dg/ipa/fnsummary-11.c @@ -0,0 +1,278 @@ +/* { dg-do run } */ +/* { dg-options "-O2 -fno-partial-inlining -fdump-ipa-fnsummary-details -fdump-ipa-inline-details" } */ + +struct Node +{ + const char *value; + unsigned type; + unsigned id; + struct Node *next; +}; + +enum NodeType { NAME = 1, NUMBER = 2, OP = 3 }; + +__attribute__((noipa)) static int +is_builtin_type (const char *v) +{ + static const char *const types[] = { + "int", "char", "long", "short", "float", "double", "void", "bool" + }; + for (unsigned i = 0; i < sizeof (types) / sizeof (types[0]); ++i) + { + const char *a = v; + const char *b = types[i]; + while (*a && *a == *b) + { + ++a; + ++b; + } + if (*a == *b) + return 1; + } + return 0; +} + +static inline int +match_specifier (const struct Node *node, const char *p) +{ + switch (p[1]) + { + case 'n': + if (p[2] == 'a') + { + if (node->type != NAME) + return 0; + unsigned char ch = (unsigned char) node->value[0]; + return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'); + } + else + { + const char *v = node->value; + if (node->type != NUMBER) + return 0; + if (v[0] == '0' && (v[1] == 'x' || v[1] == 'X')) + return 1; + if (v[0] >= '0' && v[0] <= '9') + return 1; + if (v[0] == '-' && v[1] >= '0' && v[1] <= '9') + return 1; + return 0; + } + case 't': + { + const char *v = node->value; + if (node->type != NAME) + return 0; + if (v[0] >= 'A' && v[0] <= 'Z') + return 1; + return is_builtin_type (v); + } + default: + return 0; + } +} + +static inline int +match_bracket (const struct Node *node, const char *p) +{ + if (node->value[0] == '\0' || node->value[1] != '\0') + return 0; + int negated = (p[1] == '^'); + const char *c = negated ? p + 2 : p + 1; + int found = 0; + while (*c && *c != ' ' && *c != ']') + { + if (*c == node->value[0]) + found = 1; + ++c; + } + if (*c != ']') + return 0; + return negated ? !found : found; +} + +static inline int +match_negation (const struct Node *node, const char *p) +{ + const char *s = node->value; + const char *w = p + 2; + for (;;) + { + if (*s != *w) + { + if (*s == '\0' && (*w == ' ' || *w == '\0')) + return 0; + return 1; + } + if (*s == '\0') + return 0; + ++s; + ++w; + } +} + +static inline int +match_alternatives (const struct Node *node, const char *p) +{ + const char *needle = node->value; + const char *np = needle; + for (;;) + { + if (*p == '|') + { + if (*np == '\0') + return 1; + np = needle; + ++p; + } + else if (*np == *p) + { + if (*np == '\0' || *p == ' ') + return 1; + ++np; + ++p; + } + else if (*p == ' ' || *p == '\0') + return *np == '\0'; + else + { + np = needle; + do + { + ++p; + if (*p == ' ' || *p == '\0') + return 0; + } + while (*p != '|'); + ++p; + } + } +} + +static inline int +match (const struct Node *node, const char *pattern) +{ + if (!*pattern) + return 1; + + const char *p = pattern; + for (;;) + { + while (*p == ' ') + ++p; + if (*p == '\0') + break; + + if (!node) + { + if (p[0] == '!' && p[1] == '!' && p[2] != '\0') + { + while (*p && *p != ' ') + ++p; + continue; + } + return 0; + } + + switch (*p) + { + case '%': + if (!match_specifier (node, p)) + return 0; + break; + case '[': + if (!match_bracket (node, p)) + return 0; + break; + case '!': + if (p[1] == '!' && p[2] != '\0') + { + if (!match_negation (node, p)) + return 0; + break; + } + /* Fall through. */ + default: + if (!match_alternatives (node, p)) + return 0; + break; + } + + while (*p && *p != ' ') + ++p; + if (*p == '\0') + break; + node = node->next; + } + return 1; +} + +__attribute__((noinline)) int +caller_specifier (const struct Node *n) +{ + return match (n, "%name% :: %type%"); +} + +__attribute__((noinline)) int +caller_bracket (const struct Node *n) +{ + return match (n, "[;,{}]"); +} + +__attribute__((noinline)) int +caller_negation (const struct Node *n) +{ + return match (n, "!!else"); +} + +__attribute__((noinline)) int +caller_alternatives (const struct Node *n) +{ + return match (n, "class|struct|union"); +} + +__attribute__((noinline)) int +caller_mixed (const struct Node *n) +{ + return match (n, "class|struct %name%"); +} + +int +main (void) +{ + struct Node n3 = { "int", NAME, 0, 0 }; + struct Node n2 = { "::", OP, 0, &n3 }; + struct Node n1 = { "foo", NAME, 42, &n2 }; + struct Node br = { ";", OP, 0, 0 }; + struct Node kw = { "struct", NAME, 0, &n1 }; + struct Node kw_else = { "else", NAME, 0, 0 }; + + if (!caller_specifier (&n1)) + __builtin_abort (); + if (caller_specifier (&n2)) + __builtin_abort (); + if (!caller_bracket (&br)) + __builtin_abort (); + if (caller_bracket (&n1)) + __builtin_abort (); + if (!caller_negation (&n1)) + __builtin_abort (); + if (caller_negation (&kw_else)) + __builtin_abort (); + if (!caller_alternatives (&kw)) + __builtin_abort (); + if (caller_alternatives (&n2)) + __builtin_abort (); + if (!caller_mixed (&kw)) + __builtin_abort (); + if (caller_mixed (&n2)) + __builtin_abort (); + + return 0; +} + +/* { dg-final { scan-ipa-dump "nonconst:\[^\n\]*op1\\\[ref maybe_modified unknown_offset\\\] != 32\[^\n\]*op1\\\[ref maybe_modified unknown_offset\\\] == 32" "fnsummary" } } */ +/* { dg-final { scan-ipa-dump "nonconst:\[^\n\]*op1\\\[ref maybe_modified unknown_offset\\\] == 33 \\|\\| op1\\\[ref maybe_modified unknown_offset\\\] == 37 \\|\\| op1\\\[ref maybe_modified unknown_offset\\\] == 91" "fnsummary" } } */ +/* { dg-final { scan-ipa-dump "nonconst:\[^\n\]*op1\\\[ref unknown_offset\\\]\[^\n\]*== 124" "fnsummary" } } */ +/* { dg-final { scan-ipa-dump "Known to be false:\[^\n\]*op1\\\[ref maybe_modified unknown_offset\\\] == 32,\[^\n\]*== 33,\[^\n\]*== 37,\[^\n\]*== 91" "inline" } } */ +/* { dg-final { scan-ipa-dump "Inlined match\[^\n\]* into caller_alternatives/" "inline" } } */ diff --git a/gcc/testsuite/gcc.dg/ipa/fnsummary-12.c b/gcc/testsuite/gcc.dg/ipa/fnsummary-12.c new file mode 100644 index 000000000000..408819c2583d --- /dev/null +++ b/gcc/testsuite/gcc.dg/ipa/fnsummary-12.c @@ -0,0 +1,184 @@ +/* { dg-do run } */ +/* { dg-options "-O2 -fno-early-inlining -fno-partial-inlining -fdump-ipa-fnsummary-details -fdump-ipa-inline-details" } */ + +__attribute__((noipa)) static void +sink (int x) +{ + (void) x; +} + +int +test_sw_loop (const char *p) +{ + int acc = 0; + while (*p) + { + switch (*p) + { + case '%': + sink (1); + sink (11); + sink (111); + acc += 1; + break; + case '[': + sink (2); + sink (12); + acc += 2; + break; + case '!': + sink (3); + sink (13); + acc += 3; + break; + case '?': + sink (4); + sink (14); + acc += 4; + break; + default: + break; + } + p++; + } + return acc; +} + +int +test_sw_range (const char *p, int n) +{ + int acc = 0; + for (int i = 0; i < n; i++) + { + switch (p[i]) + { + case 0 ... 'f': + acc += 1; + break; + case 'z': + sink (21); + sink (22); + sink (23); + sink (24); + sink (25); + sink (26); + sink (31); + acc += 2; + break; + default: + sink (27); + sink (28); + sink (29); + sink (30); + sink (32); + sink (33); + sink (34); + acc += 100; + break; + } + } + return acc; +} + +int +test_sw_phi (const char *p, int i) +{ + int r; + switch (p[i]) + { + case '%': + sink (31); + sink (32); + sink (33); + sink (34); + r = 10; + break; + case '[': + sink (35); + sink (36); + sink (37); + sink (38); + r = 20; + break; + case '!': + sink (39); + sink (40); + sink (41); + sink (42); + r = 30; + break; + default: + r = 0; + break; + } + int t = (r + 1) * (r + 3); + t ^= (t + 5) * (r + 7); + t += (r != 0 ? 17 : 0); + if (r != 0) + sink (t); + return t; +} + +__attribute__((noinline)) int +caller_loop_const (void) +{ + return test_sw_loop ("plain_identifier"); +} + +__attribute__((noinline)) int +caller_loop_nonconst (void) +{ + return test_sw_loop ("plain %s"); +} + +__attribute__((noinline)) int +caller_range_const (void) +{ + return test_sw_range ("deadbeef", 8); +} + +__attribute__((noinline)) int +caller_range_nonconst (void) +{ + return test_sw_range ("az~!", 4); +} + +__attribute__((noinline)) int +caller_phi_const (int i) +{ + return test_sw_phi ("hello", i); +} + +__attribute__((noinline)) int +caller_phi_nonconst (int i) +{ + return test_sw_phi ("hi%!", i); +} + +int +main (void) +{ + if (caller_loop_const () != 0) + __builtin_abort (); + if (caller_loop_nonconst () != 1) + __builtin_abort (); + if (caller_range_const () != 8) + __builtin_abort (); + if (caller_range_nonconst () != 104) + __builtin_abort (); + if (caller_phi_const (1) != 59) + __builtin_abort (); + if (caller_phi_nonconst (2) == 59) + __builtin_abort (); + return 0; +} + +/* { dg-final { scan-ipa-dump "nonconst:\[^\n\]*op0\\\[ref maybe_modified unknown_offset\\\] == 33 \\|\\| op0\\\[ref maybe_modified unknown_offset\\\] == 37 \\|\\| op0\\\[ref maybe_modified unknown_offset\\\] == 63 \\|\\| op0\\\[ref maybe_modified unknown_offset\\\] == 91" "fnsummary" } } */ +/* { dg-final { scan-ipa-dump "nonconst:\[^\n\]*op0\\\[ref maybe_modified unknown_offset\\\] > 102\[^\n\]*op0\\\[ref maybe_modified unknown_offset\\\] == 122" "fnsummary" } } */ +/* { dg-final { scan-ipa-dump "phi predicate: \[^\n\]*op0\\\[ref unknown_offset\\\] == 33 \\|\\| op0\\\[ref unknown_offset\\\] == 37 \\|\\| op0\\\[ref unknown_offset\\\] == 91" "fnsummary" } } */ +/* { dg-final { scan-ipa-dump "Inlined test_sw_loop\[^\n\]* into caller_loop_const/" "inline" } } */ +/* { dg-final { scan-ipa-dump "Inlined test_sw_range\[^\n\]* into caller_range_const/" "inline" } } */ +/* { dg-final { scan-ipa-dump "Inlined test_sw_phi\[^\n\]* into caller_phi_const/" "inline" } } */ +/* { dg-final { scan-ipa-dump-not "Inlined test_sw_loop\[^\n\]* into caller_loop_nonconst/" "inline" } } */ +/* { dg-final { scan-ipa-dump-not "Inlined test_sw_range\[^\n\]* into caller_range_nonconst/" "inline" } } */ +/* { dg-final { scan-ipa-dump-not "Inlined test_sw_phi\[^\n\]* into caller_phi_nonconst/" "inline" } } */ diff --git a/gcc/testsuite/gcc.dg/ipa/fnsummary-13.c b/gcc/testsuite/gcc.dg/ipa/fnsummary-13.c new file mode 100644 index 000000000000..154dd95c041c --- /dev/null +++ b/gcc/testsuite/gcc.dg/ipa/fnsummary-13.c @@ -0,0 +1,126 @@ +/* { dg-do run } */ +/* { dg-options "-O2 -fno-partial-inlining -fdump-ipa-fnsummary-details -fdump-ipa-inline-details" } */ + +static inline const char * +find_char (const char *s, char c) +{ + for (; *s; ++s) + if (*s == c) + return s; + return 0; +} + +static inline int +match_pattern (int target, const char *pattern) +{ + if (!*pattern) + return 1; + + const char *p = pattern; + for (;;) + { + while (*p == ' ') + ++p; + if (*p == '\0') + break; + + if (*p == '[') + { + const char *end = find_char (p + 1, ']'); + if (!end) + return 0; + int matched = 0; + for (const char *c = p + 1; c < end; ++c) + if (*c == target) + matched = 1; + if (!matched) + return 0; + p = end + 1; + } + else if (*p == '!') + { + if (p[1] == '!') + p += 2; + if (*p == target) + return 0; + while (*p && *p != ' ') + ++p; + } + else + { + if (*p != target) + return 0; + while (*p && *p != ' ') + ++p; + } + + if (*p == '\0') + break; + } + return 1; +} + +__attribute__((noinline)) int +caller_bang (int target) +{ + return match_pattern (target, "!!x"); +} + +__attribute__((noinline)) int +caller_bracket (int target) +{ + return match_pattern (target, "[ab] [bc]"); +} + +__attribute__((noinline)) int +caller_plain (int target) +{ + return match_pattern (target, "a"); +} + +__attribute__((optimize ("no-delete-null-pointer-checks"), noinline)) int +check_hash_null_phi (int dummy, const char *pattern) +{ + (void) dummy; + const char *end = find_char (pattern, ']'); + if (!end) + return 0; + if (end[1] == '#') + return 1; + return 0; +} + +__attribute__((noinline)) int +caller_no_delete_null (void) +{ + return check_hash_null_phi (0, "a]#") + + check_hash_null_phi (0, "a]b") + + check_hash_null_phi (0, "abc"); +} + +int +main (void) +{ + if (!caller_bang ('y')) + __builtin_abort (); + if (caller_bang ('x')) + __builtin_abort (); + if (!caller_bracket ('b')) + __builtin_abort (); + if (caller_bracket ('z')) + __builtin_abort (); + if (!caller_plain ('a')) + __builtin_abort (); + if (caller_plain ('b')) + __builtin_abort (); + if (caller_no_delete_null () != 1) + __builtin_abort (); + return 0; +} + +/* { dg-final { scan-ipa-dump "nonconst:\[^\n\]*op1\\\[ref unknown_offset\\\] != 32\[^\n\]*op1\\\[ref unknown_offset\\\] == 32" "fnsummary" } } */ +/* { dg-final { scan-ipa-dump "nonconst:\[^\n\]*op1\\\[ref unknown_offset\\\] != 91\[^\n\]*op1\\\[ref unknown_offset\\\] == 91" "fnsummary" } } */ +/* { dg-final { scan-ipa-dump "nonconst:\[^\n\]*op1\\\[ref unknown_offset\\\] != 93\[^\n\]*op1\\\[ref unknown_offset\\\] == 93" "fnsummary" } } */ +/* { dg-final { scan-ipa-dump "nonconst:\[^\n\]*op1\\\[ref unknown_offset\\\] != 33\[^\n\]*op1\\\[ref unknown_offset\\\] == 33" "fnsummary" } } */ +/* { dg-final { scan-ipa-dump "Known to be false:\[^\n\]*op1\\\[ref unknown_offset\\\] == 32\[^\n\]*op1\\\[ref unknown_offset\\\] == 91\[^\n\]*op1\\\[ref unknown_offset\\\] == 93\[^\n\]*op1\\\[ref unknown_offset\\\] == 33" "inline" } } */ +/* { dg-final { scan-ipa-dump-not "op1\\\[ref unknown_offset\\\] == 35" "fnsummary" } } */
