https://gcc.gnu.org/g:173072492587491dea9e2b0a9d02da9e4dff2c65
commit r17-6278-g173072492587491dea9e2b0a9d02da9e4dff2c65 Author: Jan Hubicka <[email protected]> Date: Sat Nov 14 20:05:40 2026 +0100 Extend ipa_load_from_parm_agg_unknown_offset to walk PHIs This adds logic to walk chained PHIs so loops walking strings are handled correctly. Bootstrapped/regtesed x86_64-linux, comitted. gcc/ChangeLog: PR ipa/127171 * ipa-prop.cc (ipa_load_from_parm_agg_unknown_offset): Walk PHIs. gcc/testsuite/ChangeLog: * gcc.dg/ipa/fnsummary-10.c: Add PHI walking test. * gcc.dg/ipa/fnsummary-9.c: Add PHI walking test. Assisted by Google Carbon Diff: --- gcc/ipa-prop.cc | 133 +++++++++++++++++++++++--------- gcc/testsuite/gcc.dg/ipa/fnsummary-10.c | 40 ++++++++++ gcc/testsuite/gcc.dg/ipa/fnsummary-9.c | 105 +++++++++++++++++++++++++ 3 files changed, 243 insertions(+), 35 deletions(-) diff --git a/gcc/ipa-prop.cc b/gcc/ipa-prop.cc index 28d58018fdfe..f762410d0c09 100644 --- a/gcc/ipa-prop.cc +++ b/gcc/ipa-prop.cc @@ -1281,7 +1281,9 @@ parm_ref_data_pass_through_p (struct ipa_func_body_info *fbi, int index, load whose byte offset is unknown (for example due to a variable ARRAY_REF index or pointer arithmetic from a parameter). Use get_base_address to strip component references down to a MEM_REF and walk backwards through - pointer adjustments to locate the parameter; if found, record *OFFSET_P as + pointer adjustments, copies, conversions and PHI nodes using a worklist and + a visited set of SSA_NAME versions, verifying that every reachable path + originates from the same function parameter. If so, record *OFFSET_P as -1 and set *UNKNOWN_OFFSET_P to true. */ static bool @@ -1307,54 +1309,115 @@ ipa_load_from_parm_agg_unknown_offset (struct ipa_func_body_info *fbi, if (!base || TREE_CODE (base) != MEM_REF) return false; - /* Strip pointer arithmetic and pointer conversions from the MEM_REF base - pointer (e.g. p[i] lowers to _2 = p_1(D) + _1; *_2). */ - tree ptr = TREE_OPERAND (base, 0); + tree start_ptr = TREE_OPERAND (base, 0); + if (TREE_CODE (start_ptr) != SSA_NAME) + return false; + + /* Walk backwards through pointer adjustments, copies, conversions and PHIs, + verifying that all reachable definitions originate from the same function + parameter. */ + auto_vec<tree, 8> worklist; + hash_set<int_hash<unsigned, 0>> visited; + visited.add (SSA_NAME_VERSION (start_ptr)); + worklist.quick_push (start_ptr); + + int found_index = -1; + tree parm_ptr = NULL_TREE; + bool saw_phi = false; int steps = 0; - while (TREE_CODE (ptr) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (ptr)) + + while (!worklist.is_empty ()) { + tree ptr = worklist.pop (); + if (SSA_NAME_IS_DEFAULT_DEF (ptr)) + { + tree parm = SSA_NAME_VAR (ptr); + if (!parm || TREE_CODE (parm) != PARM_DECL) + return false; + int index = ipa_get_param_decl_index_1 (descriptors, parm); + if (index < 0 + || (found_index != -1 && found_index != index)) + return false; + found_index = index; + parm_ptr = ptr; + continue; + } + if (++steps > param_ipa_jump_function_lookups) return false; gimple *def = SSA_NAME_DEF_STMT (ptr); - if (is_gimple_assign (def) - && (gimple_assign_rhs_code (def) == POINTER_PLUS_EXPR - || gimple_assign_rhs_code (def) == SSA_NAME - || (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)) - && POINTER_TYPE_P (TREE_TYPE (gimple_assign_rhs1 (def))) - && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (ptr))) - == TYPE_ADDR_SPACE (TREE_TYPE - (TREE_TYPE - (gimple_assign_rhs1 (def)))))))) - ptr = gimple_assign_rhs1 (def); + if (gphi *phi = dyn_cast <gphi *> (def)) + { + saw_phi = true; + unsigned int n = gimple_phi_num_args (phi); + for (unsigned int i = 0; i < n; i++) + { + tree arg = gimple_phi_arg_def (phi, i); + if (TREE_CODE (arg) != SSA_NAME) + return false; + if (!visited.add (SSA_NAME_VERSION (arg))) + worklist.safe_push (arg); + } + } + else if (is_gimple_assign (def)) + { + enum tree_code rhs_code = gimple_assign_rhs_code (def); + tree rhs1 = gimple_assign_rhs1 (def); + if (rhs_code == POINTER_PLUS_EXPR + || rhs_code == SSA_NAME + || (CONVERT_EXPR_CODE_P (rhs_code) + && POINTER_TYPE_P (TREE_TYPE (ptr)) + && POINTER_TYPE_P (TREE_TYPE (rhs1)))) + { + if (TREE_CODE (rhs1) != SSA_NAME) + return false; + if (!visited.add (SSA_NAME_VERSION (rhs1))) + worklist.safe_push (rhs1); + } + 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) + return false; + tree next = TREE_OPERAND (inner, 0); + if (!visited.add (SSA_NAME_VERSION (next))) + worklist.safe_push (next); + } + else + { + int index = load_from_unmodified_param (fbi, descriptors, def); + if (index < 0 + || (found_index != -1 && found_index != index)) + return false; + found_index = index; + parm_ptr = ptr; + } + } else - break; - } - - if (TREE_CODE (ptr) != SSA_NAME) - return false; - - int index = -1; - if (SSA_NAME_IS_DEFAULT_DEF (ptr)) - { - tree parm = SSA_NAME_VAR (ptr); - if (parm && TREE_CODE (parm) == PARM_DECL) - index = ipa_get_param_decl_index_1 (descriptors, parm); + return false; } - else - index = load_from_unmodified_param (fbi, descriptors, - SSA_NAME_DEF_STMT (ptr)); - if (index < 0) + if (found_index < 0) return false; - bool data_preserved = (gimple_vuse (stmt) - ? parm_ref_data_preserved_p (fbi, index, stmt, op) - : false); + /* When a PHI was crossed (for instance a pointer induction variable in a + loop), a store to ptr + 1 in a previous iteration aliases *ptr in the + next iteration even though both share the same SSA_NAME base, so check + preservation of the whole pointed-to object via + parm_ref_data_pass_through_p. */ + bool data_preserved + = (gimple_vuse (stmt) + ? (saw_phi + ? parm_ref_data_pass_through_p (fbi, found_index, stmt, parm_ptr) + : parm_ref_data_preserved_p (fbi, found_index, stmt, op)) + : false); if (!data_preserved && !guaranteed_unmodified) return false; - *index_p = index; + *index_p = found_index; *offset_p = -1; *by_ref_p = true; if (size_p) diff --git a/gcc/testsuite/gcc.dg/ipa/fnsummary-10.c b/gcc/testsuite/gcc.dg/ipa/fnsummary-10.c index fc9117d19cfa..499f760a86e6 100644 --- a/gcc/testsuite/gcc.dg/ipa/fnsummary-10.c +++ b/gcc/testsuite/gcc.dg/ipa/fnsummary-10.c @@ -189,6 +189,35 @@ test_else_lt130 (const unsigned char *p, int i) } } +int +test_loop_z (const char *c) +{ + while (*c) + { + COLD_WORK (); + c++; + } + return 0; +} + +int +test_loop_pct (const char *c, int a, int b, int d) +{ + (void) a; + (void) b; + (void) d; + while (*c) + { + if (*c == '%') + { + COLD_WORK (); + return 1; + } + c++; + } + return 0; +} + int main (void) { @@ -226,6 +255,14 @@ main (void) if (test_eq (A300 "\0%bcdef", i301) != 1) __builtin_abort (); + /* Pointer loop (PHI induction variable) over > 256-char strings. */ + if (test_loop_z (Z300) != 0) + __builtin_abort (); + if (test_loop_pct (STR320, 1, 2, 3) != 0) + __builtin_abort (); + if (test_loop_pct ("ab%def", 1, 2, 3) != 1) + __builtin_abort (); + return 0; } @@ -249,6 +286,8 @@ main (void) /* { dg-final { scan-tree-dump "optimized: Inlining test_else_le0/" "einline" } } */ /* { dg-final { scan-tree-dump "optimized: Inlining test_else_le_j/" "einline" } } */ /* { dg-final { scan-tree-dump "optimized: Inlining test_else_lt130/" "einline" } } */ +/* { dg-final { scan-tree-dump "optimized: Inlining test_loop_z/" "einline" } } */ +/* { dg-final { scan-tree-dump-times "optimized: Inlining test_loop_pct/" 1 "einline" } } */ /* { dg-final { scan-tree-dump-not " test_eq_neg \\(" "optimized" } } */ /* { dg-final { scan-tree-dump-not " test_ne_nul \\(" "optimized" } } */ /* { dg-final { scan-tree-dump-not " test_lt0 \\(" "optimized" } } */ @@ -261,3 +300,4 @@ main (void) /* { dg-final { scan-tree-dump-not " test_else_le0 \\(" "optimized" } } */ /* { dg-final { scan-tree-dump-not " test_else_le_j \\(" "optimized" } } */ /* { dg-final { scan-tree-dump-not " test_else_lt130 \\(" "optimized" } } */ +/* { dg-final { scan-tree-dump-not " test_loop_z \\(" "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/ipa/fnsummary-9.c b/gcc/testsuite/gcc.dg/ipa/fnsummary-9.c index 4eb112d2a070..c6cb793a6300 100644 --- a/gcc/testsuite/gcc.dg/ipa/fnsummary-9.c +++ b/gcc/testsuite/gcc.dg/ipa/fnsummary-9.c @@ -228,6 +228,89 @@ chk_signed_to_uint (const signed char *p, int i) return 0; } +int +chk_phi_loop (const char *c, int a, int b, int d) +{ + (void) a; + (void) b; + (void) d; + while (*c) + { + if (*c == '~') + { + COLD_WORK (); + return 1; + } + c++; + } + return 0; +} + +int +chk_phi_diamond (const char *c, int flag, int a, int b) +{ + (void) a; + (void) b; + if (flag) + c += 2; + else + c += 1; + if (*c == '~') + { + COLD_WORK (); + return 1; + } + return 0; +} + +int +chk_phi_two_parms (const char *p, const char *q, int flag, int a) +{ + (void) a; + const char *c = flag ? p : q; + if (*c == '~') + { + COLD_WORK (); + return 1; + } + return 0; +} + +int +chk_phi_lit (const char *p, int flag, int a, int b) +{ + (void) a; + (void) b; + const char *c = flag ? p : "~bcdef"; + if (*c == '~') + { + COLD_WORK (); + return 1; + } + return 0; +} + +static int +chk_phi_mod_helper (char *c) +{ + int found = 0; + while (*c) + { + if (*c == '$') + found = 1; + if (*c == 'a') + c[1] = '$'; + c++; + } + return found; +} + +__attribute__((noipa)) int +chk_phi_mod_caller (char *buf) +{ + return chk_phi_mod_helper (buf); +} + int main (void) { @@ -318,6 +401,24 @@ main (void) if (chk_switch ("ab%def", i2) != 1) __builtin_abort (); + /* PHI handling: loop induction variable, diamond, two params, literal, + and loop modification soundness. */ + if (chk_phi_loop ("abcdef", 1, 2, 3) != 0) + __builtin_abort (); + if (chk_phi_loop ("ab~def", 1, 2, 3) != 1) + __builtin_abort (); + if (chk_phi_diamond ("abcdef", i, 1, 2) != 0) + __builtin_abort (); + if (chk_phi_diamond ("a~cdef", i, 1, 2) != 1) + __builtin_abort (); + if (chk_phi_two_parms ("abcdef", "~bcdef", 0, 0) != 1) + __builtin_abort (); + if (chk_phi_lit ("abcdef", 0, 1, 2) != 1) + __builtin_abort (); + char modbuf[] = "abcdef"; + if (chk_phi_mod_caller (modbuf) != 1) + __builtin_abort (); + return 0; } @@ -345,3 +446,7 @@ main (void) /* { dg-final { scan-ipa-dump-times "Inlined chk_arr_ptr/" 1 "inline" } } */ /* { dg-final { scan-ipa-dump "Known to be false:.*unknown_offset\\\] == 40" "inline" } } */ /* { dg-final { scan-ipa-dump-not "Inlined chk_u_ge128_cast/" "inline" } } */ +/* { dg-final { scan-ipa-dump-times "Inlined chk_phi_loop/" 1 "inline" } } */ +/* { dg-final { scan-ipa-dump-times "Inlined chk_phi_diamond/" 1 "inline" } } */ +/* { dg-final { scan-ipa-dump-not "Inlined chk_phi_two_parms/" "inline" } } */ +/* { dg-final { scan-ipa-dump-not "Inlined chk_phi_lit/" "inline" } } */
