https://gcc.gnu.org/g:ac26de540a2f2b7913dae3a0bb21521054dd567c
commit r17-6323-gac26de540a2f2b7913dae3a0bb21521054dd567c Author: Andrea Pinski <[email protected]> Date: Fri Nov 13 15:30:14 2026 -0800 phiopt: limit overall references for match-simplify-phiopt to one of the bb This was brought to the attention to me from Jeff about the possibility that phiopt could in theory reference both bb and make both statements unconditional. THis does not happen currently but it would be better to add a check to make sure we reject these cases. This will be used also by the allow phiopt with multiply statements patch which is why the interface to check_uses is basic blocks rather than a statement. I am going be breaking that patch up into a few different patches too. Bootstrapped and tested on x86_64-linux-gnu. gcc/ChangeLog: * tree-ssa-phiopt.cc (check_uses): New function (match_simplify_replacement): Call and check check_uses. Signed-off-by: Andrea Pinski <[email protected]> Diff: --- gcc/tree-ssa-phiopt.cc | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc index 2add156bcd73..256f8eccffa1 100644 --- a/gcc/tree-ssa-phiopt.cc +++ b/gcc/tree-ssa-phiopt.cc @@ -889,6 +889,68 @@ gimple_simplify_phiopt (bool early_p, tree type, gimple *comp_stmt, return NULL; } + +/* Returns true if the uses of the sequence SEQ contains only references to + BB0 or BB1 (but not both) or the references are outside of either BB0/BB1. */ + +static bool +check_uses (gimple_seq seq, basic_block bb0, basic_block bb1) +{ + if (!seq) + return true; + gcc_checking_assert (bb0); + gcc_checking_assert (bb1); + struct bb_info { + basic_block bb0; + basic_block bb1; + bool bb0_referenced; + bool bb1_referenced; + }; + bb_info info = { bb0, bb1, false, false }; + + walk_stmt_info wi = {}; + wi.info = &info; + auto fn = [](tree *tp, int *, void *data) -> tree + { + walk_stmt_info *wi = (walk_stmt_info*)data; + if (wi->is_lhs) + return NULL_TREE; + bb_info *uses = (bb_info*)wi->info; + tree use = *tp; + if (TREE_CODE (use) != SSA_NAME) + return NULL_TREE; + gimple *stmt = SSA_NAME_DEF_STMT (use); + basic_block sbb = gimple_bb (stmt); + // A stmt with a null bb is ok, either it is the sequnce + // or is outside default defined one. + if (sbb == nullptr) + return NULL_TREE; + else if (sbb == uses->bb0) + { + if (uses->bb1_referenced) + return use; + uses->bb0_referenced = true; + } + else if (sbb == uses->bb1) + { + if (uses->bb0_referenced) + return use; + uses->bb1_referenced = true; + } + return NULL_TREE; + }; + + + gimple_stmt_iterator gsi = gsi_last (seq); + for( ; !gsi_end_p (gsi); gsi_prev (&gsi)) + { + gimple *s = *gsi; + if (walk_gimple_op (s, fn, &wi)) + return false; + } + return true; +} + /* one_feeding_comparison_into_p returns true if BB has one comparison statement and it sets STMT to that statement. Note the comparison can be trapping too. */ @@ -1299,6 +1361,18 @@ match_simplify_replacement (basic_block cond_bb, basic_block middle_bb, statistics_counter_event (cfun, "Non-IEEE FP MIN/MAX PHI replacement", 1); } + + // Make sure the sequence does not reference both basic blocks. + if (threeway_p && middle_bb != middle_bb_alt + && !check_uses (seq, middle_bb, middle_bb_alt)) + { + if (dump_file && (dump_flags & TDF_FOLDING)) + fprintf (dump_file, "rejected the phiopt match-simplify as it uses" + "both conditional statements.\n"); + gimple_seq_discard (seq); + return false; + } + if (dump_file && (dump_flags & TDF_FOLDING)) fprintf (dump_file, "accepted the phiopt match-simplify.\n");
