From 3f207ab314c071c6060c7c9a429fcf29fd87b594 Mon Sep 17 00:00:00 2001 From: Trevor Saunders Date: Fri, 11 Jun 2021 23:49:22 -0400 Subject: use range based for loops to iterate over vec<> This changes users of FOR_EACH_VEC_ELT to use range based for loops, where the index variables are otherwise unused. As such the index variables are all deleted, producing shorter and simpler code. Signed-off-by: Trevor Saunders gcc/analyzer/ChangeLog: * call-string.cc (call_string::call_string): Use range based for to iterate over vec<>. (call_string::to_json): Likewise. (call_string::hash): Likewise. (call_string::calc_recursion_depth): Likewise. * checker-path.cc (checker_path::fixup_locations): Likewise. * constraint-manager.cc (equiv_class::equiv_class): Likewise. (equiv_class::to_json): Likewise. (equiv_class::hash): Likewise. (constraint_manager::to_json): Likewise. * engine.cc (impl_region_model_context::on_svalue_leak): Likewise. (on_liveness_change): Likewise. (impl_region_model_context::on_unknown_change): Likewise. * program-state.cc (sm_state_map::set_state): Likewise. * region-model.cc (test_canonicalization_4): Likewise. gcc/ChangeLog: * attribs.c (find_attribute_namespace): Iterate over vec<> with range based for. * auto-profile.c (afdo_find_equiv_class): Likewise. * gcc.c (do_specs_vec): Likewise. (do_spec_1): Likewise. (driver::set_up_specs): Likewise. * gimple-loop-jam.c (any_access_function_variant_p): Likewise. * gimple-ssa-store-merging.c (compatible_load_p): Likewise. (imm_store_chain_info::try_coalesce_bswap): Likewise. (imm_store_chain_info::coalesce_immediate_stores): Likewise. (get_location_for_stmts): Likewise. * graphite-poly.c (print_iteration_domains): Likewise. (free_poly_bb): Likewise. (remove_gbbs_in_scop): Likewise. (free_scop): Likewise. (dump_gbb_cases): Likewise. (dump_gbb_conditions): Likewise. (print_pdrs): Likewise. (print_scop): Likewise. * ifcvt.c (cond_move_process_if_block): Likewise. * lower-subreg.c (decompose_multiword_subregs): Likewise. * regcprop.c (pass_cprop_hardreg::execute): Likewise. * sanopt.c (sanitize_rewrite_addressable_params): Likewise. * sel-sched-dump.c (dump_insn_vector): Likewise. * store-motion.c (store_ops_ok): Likewise. (store_killed_in_insn): Likewise. * timevar.c (timer::named_items::print): Likewise. * tree-cfgcleanup.c (cleanup_control_flow_pre): Likewise. (cleanup_tree_cfg_noloop): Likewise. * tree-data-ref.c (dump_data_references): Likewise. (print_dir_vectors): Likewise. (print_dist_vectors): Likewise. (dump_data_dependence_relations): Likewise. (dump_dist_dir_vectors): Likewise. (dump_ddrs): Likewise. (create_runtime_alias_checks): Likewise. (free_subscripts): Likewise. (save_dist_v): Likewise. (save_dir_v): Likewise. (invariant_access_functions): Likewise. (same_access_functions): Likewise. (access_functions_are_affine_or_constant_p): Likewise. (find_data_references_in_stmt): Likewise. (graphite_find_data_references_in_stmt): Likewise. (free_dependence_relations): Likewise. (free_data_refs): Likewise. * tree-inline.c (copy_debug_stmts): Likewise. * tree-into-ssa.c (dump_currdefs): Likewise. (rewrite_update_phi_arguments): Likewise. * tree-ssa-propagate.c (clean_up_loop_closed_phi): Likewise. * tree-vect-data-refs.c (vect_analyze_possibly_independent_ddr): Likewise. (vect_slp_analyze_node_dependences): Likewise. (vect_slp_analyze_instance_dependence): Likewise. (vect_record_base_alignments): Likewise. (vect_get_peeling_costs_all_drs): Likewise. (vect_peeling_supportable): Likewise. * tree-vectorizer.c (vec_info::~vec_info): Likewise. (vec_info::free_stmt_vec_infos): Likewise. gcc/cp/ChangeLog: * constexpr.c (cxx_eval_call_expression): Iterate over vec<> with range based for. (cxx_eval_store_expression): Likewise. (cxx_eval_loop_expr): Likewise. * decl.c (wrapup_namespace_globals): Likewise. (cp_finish_decl): Likewise. (cxx_simulate_enum_decl): Likewise. * parser.c (cp_parser_postfix_expression): Likewise. --- gcc/gimple-loop-jam.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'gcc/gimple-loop-jam.c') diff --git a/gcc/gimple-loop-jam.c b/gcc/gimple-loop-jam.c index 69dbaeb..4842f0d 100644 --- a/gcc/gimple-loop-jam.c +++ b/gcc/gimple-loop-jam.c @@ -365,11 +365,9 @@ static bool any_access_function_variant_p (const struct data_reference *a, const class loop *loop_nest) { - unsigned int i; vec fns = DR_ACCESS_FNS (a); - tree t; - FOR_EACH_VEC_ELT (fns, i, t) + for (tree t : fns) if (!evolution_function_is_invariant_p (t, loop_nest->num)) return true; -- cgit v1.1 From 957952ce64e067c56e58df5ee36bbb004eecffa1 Mon Sep 17 00:00:00 2001 From: Richard Sandiford Date: Wed, 21 Jul 2021 07:50:20 +0100 Subject: unroll: Run VN on unrolled-and-jammed loops Unroll and jam can sometimes leave redundancies. E.g. for: for (int j = 0; j < 100; ++j) for (int i = 0; i < 100; ++i) x[i] += y[i] * z[j][i]; the new loop will do the equivalent of: for (int j = 0; j < 100; j += 2) for (int i = 0; i < 100; ++i) { x[i] += y[i] * z[j][i]; x[i] += y[i] * z[j + 1][i]; } with two reads of y[i] and with a round trip through memory for x[i]. At the moment these redundancies survive till vectorisation, so if vectorisation succeeds, we're reliant on being able to remove the redundancies from the vector form. This can be hard to do if a vector loop uses predication. E.g. on SVE we end up with: .L3: ld1w z3.s, p0/z, [x3, x0, lsl 2] ld1w z0.s, p0/z, [x5, x0, lsl 2] ld1w z1.s, p0/z, [x2, x0, lsl 2] mad z1.s, p1/m, z0.s, z3.s ld1w z2.s, p0/z, [x4, x0, lsl 2] st1w z1.s, p0, [x3, x0, lsl 2] // store to x[i] ld1w z1.s, p0/z, [x3, x0, lsl 2] // load back from x[i] mad z0.s, p1/m, z2.s, z1.s st1w z0.s, p0, [x3, x0, lsl 2] add x0, x0, x6 whilelo p0.s, w0, w1 b.any .L3 This patch runs a value-numbering pass on loops after a successful unroll-and-jam, which gets rid of the unnecessary load and gives a more accurate idea of vector costs. Unfortunately the redundant store still persists without a pre-vect DSE, but that feels like a separate issue. Note that the pass requires the loop to have a single exit, hence the simple calculation of exit_bbs. gcc/ * gimple-loop-jam.c: Include tree-ssa-sccvn.h. (tree_loop_unroll_and_jam): Run value-numbering on a loop that has been successfully unrolled. gcc/testsuite/ * gcc.dg/unroll-10.c: New test. --- gcc/gimple-loop-jam.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'gcc/gimple-loop-jam.c') diff --git a/gcc/gimple-loop-jam.c b/gcc/gimple-loop-jam.c index 4842f0d..544ad77 100644 --- a/gcc/gimple-loop-jam.c +++ b/gcc/gimple-loop-jam.c @@ -38,6 +38,7 @@ along with GCC; see the file COPYING3. If not see #include "tree-data-ref.h" #include "tree-ssa-loop-ivopts.h" #include "tree-vectorizer.h" +#include "tree-ssa-sccvn.h" /* Unroll and Jam transformation @@ -487,7 +488,7 @@ static unsigned int tree_loop_unroll_and_jam (void) { class loop *loop; - bool changed = false; + unsigned int todo = 0; gcc_assert (scev_initialized_p ()); @@ -591,7 +592,11 @@ tree_loop_unroll_and_jam (void) &desc); free_original_copy_tables (); fuse_loops (outer->inner); - changed = true; + todo |= TODO_cleanup_cfg; + + auto_bitmap exit_bbs; + bitmap_set_bit (exit_bbs, single_dom_exit (outer)->dest->index); + todo |= do_rpo_vn (cfun, loop_preheader_edge (outer), exit_bbs); } loop_nest.release (); @@ -599,13 +604,12 @@ tree_loop_unroll_and_jam (void) free_data_refs (datarefs); } - if (changed) + if (todo) { scev_reset (); free_dominance_info (CDI_DOMINATORS); - return TODO_cleanup_cfg; } - return 0; + return todo; } /* Pass boilerplate */ -- 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/gimple-loop-jam.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'gcc/gimple-loop-jam.c') diff --git a/gcc/gimple-loop-jam.c b/gcc/gimple-loop-jam.c index 544ad77..d212e39 100644 --- a/gcc/gimple-loop-jam.c +++ b/gcc/gimple-loop-jam.c @@ -487,13 +487,12 @@ adjust_unroll_factor (class loop *inner, struct data_dependence_relation *ddr, static unsigned int tree_loop_unroll_and_jam (void) { - class loop *loop; unsigned int todo = 0; gcc_assert (scev_initialized_p ()); /* Go through all innermost loops. */ - FOR_EACH_LOOP (loop, LI_ONLY_INNERMOST) + for (auto loop : loops_list (cfun, LI_ONLY_INNERMOST)) { class loop *outer = loop_outer (loop); -- cgit v1.1