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/cfgloop.h | 140 ++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 107 insertions(+), 33 deletions(-) (limited to 'gcc/cfgloop.h') diff --git a/gcc/cfgloop.h b/gcc/cfgloop.h index 5c2b98d..885632b 100644 --- a/gcc/cfgloop.h +++ b/gcc/cfgloop.h @@ -658,55 +658,141 @@ enum li_flags LI_ONLY_INNERMOST = 4 /* Iterate only over innermost loops. */ }; -/* The iterator for loops. */ +/* Provide the functionality of std::as_const to support range-based for + to use const iterator. (We can't use std::as_const itself because it's + a C++17 feature.) */ +template +constexpr const T & +as_const (T &t) +{ + return t; +} + +/* A list for visiting loops, which contains the loop numbers instead of + the loop pointers. The scope is restricted in function FN and the + visiting order is specified by FLAGS. */ -class loop_iterator +class loops_list { public: - loop_iterator (function *fn, loop_p *loop, unsigned flags); + loops_list (function *fn, unsigned flags); + + template class Iter + { + public: + Iter (const loops_list &l, unsigned idx) : list (l), curr_idx (idx) + { + fill_curr_loop (); + } + + T operator* () const { return curr_loop; } + + Iter & + operator++ () + { + if (curr_idx < list.to_visit.length ()) + { + /* Bump the index and fill a new one. */ + curr_idx++; + fill_curr_loop (); + } + else + gcc_assert (!curr_loop); + + return *this; + } + + bool + operator!= (const Iter &rhs) const + { + return this->curr_idx != rhs.curr_idx; + } + + private: + /* Fill the current loop starting from the current index. */ + void fill_curr_loop (); + + /* Reference to the loop list to visit. */ + const loops_list &list; + + /* The current index in the list to visit. */ + unsigned curr_idx; - inline loop_p next (); + /* The loop implied by the current index. */ + class loop *curr_loop; + }; + using iterator = Iter; + using const_iterator = Iter; + + iterator + begin () + { + return iterator (*this, 0); + } + + iterator + end () + { + return iterator (*this, to_visit.length ()); + } + + const_iterator + begin () const + { + return const_iterator (*this, 0); + } + + const_iterator + end () const + { + return const_iterator (*this, to_visit.length ()); + } + +private: /* The function we are visiting. */ function *fn; /* The list of loops to visit. */ auto_vec to_visit; - - /* The index of the actual loop. */ - unsigned idx; }; -inline loop_p -loop_iterator::next () +/* Starting from current index CURR_IDX (inclusive), find one index + which stands for one valid loop and fill the found loop as CURR_LOOP, + if we can't find one, set CURR_LOOP as null. */ + +template +inline void +loops_list::Iter::fill_curr_loop () { int anum; - while (this->to_visit.iterate (this->idx, &anum)) + while (this->list.to_visit.iterate (this->curr_idx, &anum)) { - this->idx++; - loop_p loop = get_loop (fn, anum); + class loop *loop = get_loop (this->list.fn, anum); if (loop) - return loop; + { + curr_loop = loop; + return; + } + this->curr_idx++; } - return NULL; + curr_loop = nullptr; } -inline -loop_iterator::loop_iterator (function *fn, loop_p *loop, unsigned flags) +/* Set up the loops list to visit according to the specified + function scope FN and iterating order FLAGS. */ + +inline loops_list::loops_list (function *fn, unsigned flags) { class loop *aloop; unsigned i; int mn; - this->idx = 0; this->fn = fn; if (!loops_for_fn (fn)) - { - *loop = NULL; - return; - } + return; this->to_visit.reserve_exact (number_of_loops (fn)); mn = (flags & LI_INCLUDE_ROOT) ? 0 : 1; @@ -766,20 +852,8 @@ loop_iterator::loop_iterator (function *fn, loop_p *loop, unsigned flags) } } } - - *loop = this->next (); } -#define FOR_EACH_LOOP(LOOP, FLAGS) \ - for (loop_iterator li(cfun, &(LOOP), FLAGS); \ - (LOOP); \ - (LOOP) = li.next ()) - -#define FOR_EACH_LOOP_FN(FN, LOOP, FLAGS) \ - for (loop_iterator li(FN, &(LOOP), FLAGS); \ - (LOOP); \ - (LOOP) = li.next ()) - /* The properties of the target. */ struct target_cfgloop { /* Number of available registers. */ -- cgit v1.1