From 4cf3b10f27b1994cf4a9eb12079d85412ebc7cad Mon Sep 17 00:00:00 2001 From: Roman Zhuykov Date: Fri, 30 Apr 2021 11:08:03 +0300 Subject: modulo-sched: skip loops with strange register defs [PR100225] PR84878 fix adds an assertion which can fail, e.g. when stack pointer is adjusted inside the loop. We have to prevent it and search earlier for any 'strange' instruction. The solution is to skip the whole loop if using 'note_stores' we found that one of hard registers is in 'df->regular_block_artificial_uses' set. Also patch properly prohibit not single-set instruction in loop body. gcc/ChangeLog: PR rtl-optimization/100225 PR rtl-optimization/84878 * modulo-sched.c (sms_schedule): Use note_stores to skip loops where we have an instruction which touches (writes) any hard register from df->regular_block_artificial_uses set. Allow not-single-set instruction only right before basic block tail. gcc/testsuite/ChangeLog: PR rtl-optimization/100225 PR rtl-optimization/84878 * gcc.dg/pr100225.c: New test. libgomp/ChangeLog: * testsuite/libgomp.oacc-c-c++-common/atomic_capture-3.c: New test. --- gcc/modulo-sched.c | 56 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 21 deletions(-) (limited to 'gcc/modulo-sched.c') diff --git a/gcc/modulo-sched.c b/gcc/modulo-sched.c index 6ad960e..e72e46d 100644 --- a/gcc/modulo-sched.c +++ b/gcc/modulo-sched.c @@ -43,6 +43,7 @@ along with GCC; see the file COPYING3. If not see #include "tree-pass.h" #include "dbgcnt.h" #include "loop-unroll.h" +#include "hard-reg-set.h" #ifdef INSN_SCHEDULING @@ -1356,6 +1357,7 @@ sms_schedule (void) basic_block condition_bb = NULL; edge latch_edge; HOST_WIDE_INT trip_count, max_trip_count; + HARD_REG_SET prohibited_regs; loop_optimizer_init (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_RECORDED_EXITS); @@ -1385,6 +1387,8 @@ sms_schedule (void) We use loop->num as index into this array. */ g_arr = XCNEWVEC (ddg_ptr, number_of_loops (cfun)); + REG_SET_TO_HARD_REG_SET (prohibited_regs, &df->regular_block_artificial_uses); + if (dump_file) { fprintf (dump_file, "\n\nSMS analysis phase\n"); @@ -1469,23 +1473,31 @@ sms_schedule (void) } /* Don't handle BBs with calls or barriers - or !single_set with the exception of instructions that include - count_reg---these instructions are part of the control part - that do-loop recognizes. + or !single_set with the exception of do-loop control part insns. ??? Should handle insns defining subregs. */ - for (insn = head; insn != NEXT_INSN (tail); insn = NEXT_INSN (insn)) - { - rtx set; - - if (CALL_P (insn) - || BARRIER_P (insn) - || (NONDEBUG_INSN_P (insn) && !JUMP_P (insn) - && !single_set (insn) && GET_CODE (PATTERN (insn)) != USE - && !reg_mentioned_p (count_reg, insn)) - || (INSN_P (insn) && (set = single_set (insn)) - && GET_CODE (SET_DEST (set)) == SUBREG)) - break; - } + for (insn = head; insn != NEXT_INSN (tail); insn = NEXT_INSN (insn)) + { + if (INSN_P (insn)) + { + HARD_REG_SET regs; + CLEAR_HARD_REG_SET (regs); + note_stores (insn, record_hard_reg_sets, ®s); + if (hard_reg_set_intersect_p (regs, prohibited_regs)) + break; + } + + if (CALL_P (insn) + || BARRIER_P (insn) + || (INSN_P (insn) && single_set (insn) + && GET_CODE (SET_DEST (single_set (insn))) == SUBREG) + /* Not a single set. */ + || (NONDEBUG_INSN_P (insn) && !JUMP_P (insn) + && !single_set (insn) && GET_CODE (PATTERN (insn)) != USE + /* But non-single-set allowed in one special case. */ + && (insn != prev_nondebug_insn (tail) + || !reg_mentioned_p (count_reg, insn)))) + break; + } if (insn != NEXT_INSN (tail)) { @@ -1495,11 +1507,13 @@ sms_schedule (void) fprintf (dump_file, "SMS loop-with-call\n"); else if (BARRIER_P (insn)) fprintf (dump_file, "SMS loop-with-barrier\n"); - else if ((NONDEBUG_INSN_P (insn) && !JUMP_P (insn) - && !single_set (insn) && GET_CODE (PATTERN (insn)) != USE)) - fprintf (dump_file, "SMS loop-with-not-single-set\n"); - else - fprintf (dump_file, "SMS loop with subreg in lhs\n"); + else if (INSN_P (insn) && single_set (insn) + && GET_CODE (SET_DEST (single_set (insn))) == SUBREG) + fprintf (dump_file, "SMS loop with subreg in lhs\n"); + else + fprintf (dump_file, + "SMS loop-with-not-single-set-or-prohibited-reg\n"); + print_rtl_single (dump_file, insn); } -- cgit v1.1 From e41ba804ba5f5ca433e09238d561b1b4c8b10985 Mon Sep 17 00:00:00 2001 From: Kewen Lin Date: Thu, 29 Jul 2021 22:26:25 -0500 Subject: Use range-based for loops for traversing loops This patch follows Martin's suggestion here[1], to support range based loop for iterating loops, analogously to the patch for vec[2]. For example, use below range-based for loop for (auto loop : loops_list (cfun, 0)) to replace the previous macro FOR_EACH_LOOP FOR_EACH_LOOP (loop, 0) [1] https://gcc.gnu.org/pipermail/gcc-patches/2021-June/573424.html [2] https://gcc.gnu.org/pipermail/gcc-patches/2021-June/572315.html gcc/ChangeLog: * cfgloop.h (as_const): New function. (class loop_iterator): Rename to ... (class loops_list): ... this. (loop_iterator::next): Rename to ... (loops_list::Iter::fill_curr_loop): ... this and adjust. (loop_iterator::loop_iterator): Rename to ... (loops_list::loops_list): ... this and adjust. (loops_list::Iter): New class. (loops_list::iterator): New type. (loops_list::const_iterator): New type. (loops_list::begin): New function. (loops_list::end): Likewise. (loops_list::begin const): Likewise. (loops_list::end const): Likewise. (FOR_EACH_LOOP): Remove. (FOR_EACH_LOOP_FN): Remove. * cfgloop.c (flow_loops_dump): Adjust FOR_EACH_LOOP* with range-based for loop with loops_list instance. (sort_sibling_loops): Likewise. (disambiguate_loops_with_multiple_latches): Likewise. (verify_loop_structure): Likewise. * cfgloopmanip.c (create_preheaders): Likewise. (force_single_succ_latches): Likewise. * config/aarch64/falkor-tag-collision-avoidance.c (execute_tag_collision_avoidance): Likewise. * config/mn10300/mn10300.c (mn10300_scan_for_setlb_lcc): Likewise. * config/s390/s390.c (s390_adjust_loops): Likewise. * doc/loop.texi: Likewise. * gimple-loop-interchange.cc (pass_linterchange::execute): Likewise. * gimple-loop-jam.c (tree_loop_unroll_and_jam): Likewise. * gimple-loop-versioning.cc (loop_versioning::analyze_blocks): Likewise. (loop_versioning::make_versioning_decisions): Likewise. * gimple-ssa-split-paths.c (split_paths): Likewise. * graphite-isl-ast-to-gimple.c (graphite_regenerate_ast_isl): Likewise. * graphite.c (canonicalize_loop_form): Likewise. (graphite_transform_loops): Likewise. * ipa-fnsummary.c (analyze_function_body): Likewise. * ipa-pure-const.c (analyze_function): Likewise. * loop-doloop.c (doloop_optimize_loops): Likewise. * loop-init.c (loop_optimizer_finalize): Likewise. (fix_loop_structure): Likewise. * loop-invariant.c (calculate_loop_reg_pressure): Likewise. (move_loop_invariants): Likewise. * loop-unroll.c (decide_unrolling): Likewise. (unroll_loops): Likewise. * modulo-sched.c (sms_schedule): Likewise. * predict.c (predict_loops): Likewise. (pass_profile::execute): Likewise. * profile.c (branch_prob): Likewise. * sel-sched-ir.c (sel_finish_pipelining): Likewise. (sel_find_rgns): Likewise. * tree-cfg.c (replace_loop_annotate): Likewise. (replace_uses_by): Likewise. (move_sese_region_to_fn): Likewise. * tree-if-conv.c (pass_if_conversion::execute): Likewise. * tree-loop-distribution.c (loop_distribution::execute): Likewise. * tree-parloops.c (parallelize_loops): Likewise. * tree-predcom.c (tree_predictive_commoning): Likewise. * tree-scalar-evolution.c (scev_initialize): Likewise. (scev_reset): Likewise. * tree-ssa-dce.c (find_obviously_necessary_stmts): Likewise. * tree-ssa-live.c (remove_unused_locals): Likewise. * tree-ssa-loop-ch.c (ch_base::copy_headers): Likewise. * tree-ssa-loop-im.c (analyze_memory_references): Likewise. (tree_ssa_lim_initialize): Likewise. * tree-ssa-loop-ivcanon.c (canonicalize_induction_variables): Likewise. * tree-ssa-loop-ivopts.c (tree_ssa_iv_optimize): Likewise. * tree-ssa-loop-manip.c (get_loops_exits): Likewise. * tree-ssa-loop-niter.c (estimate_numbers_of_iterations): Likewise. (free_numbers_of_iterations_estimates): Likewise. * tree-ssa-loop-prefetch.c (tree_ssa_prefetch_arrays): Likewise. * tree-ssa-loop-split.c (tree_ssa_split_loops): Likewise. * tree-ssa-loop-unswitch.c (tree_ssa_unswitch_loops): Likewise. * tree-ssa-loop.c (gate_oacc_kernels): Likewise. (pass_scev_cprop::execute): Likewise. * tree-ssa-propagate.c (clean_up_loop_closed_phi): Likewise. * tree-ssa-sccvn.c (do_rpo_vn): Likewise. * tree-ssa-threadupdate.c (jump_thread_path_registry::thread_through_all_blocks): Likewise. * tree-vectorizer.c (vectorize_loops): Likewise. * tree-vrp.c (vrp_asserts::find_assert_locations): Likewise. --- gcc/modulo-sched.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'gcc/modulo-sched.c') diff --git a/gcc/modulo-sched.c b/gcc/modulo-sched.c index e72e46d..1c1b459 100644 --- a/gcc/modulo-sched.c +++ b/gcc/modulo-sched.c @@ -1353,7 +1353,6 @@ sms_schedule (void) int maxii, max_asap; partial_schedule_ptr ps; basic_block bb = NULL; - class loop *loop; basic_block condition_bb = NULL; edge latch_edge; HOST_WIDE_INT trip_count, max_trip_count; @@ -1397,7 +1396,7 @@ sms_schedule (void) /* Build DDGs for all the relevant loops and hold them in G_ARR indexed by the loop index. */ - FOR_EACH_LOOP (loop, 0) + for (auto loop : loops_list (cfun, 0)) { rtx_insn *head, *tail; rtx count_reg; @@ -1543,7 +1542,7 @@ sms_schedule (void) } /* We don't want to perform SMS on new loops - created by versioning. */ - FOR_EACH_LOOP (loop, 0) + for (auto loop : loops_list (cfun, 0)) { rtx_insn *head, *tail; rtx count_reg; -- cgit v1.1