1 /* Natural loop functions
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
23 #include "double-int.h"
32 #include "hard-reg-set.h"
35 #include "cfgloopmanip.h"
37 /* Structure to hold decision about unrolling/peeling. */
46 struct GTY (()) lpt_decision {
47 enum lpt_dec decision;
51 /* The type of extend applied to an IV. */
59 /* The structure describing a bound on number of iterations of a loop. */
61 struct GTY ((chain_next ("%h.next"))) nb_iter_bound {
62 /* The statement STMT is executed at most ... */
65 /* ... BOUND + 1 times (BOUND must be an unsigned constant).
66 The + 1 is added for the following reasons:
68 a) 0 would otherwise be unused, while we would need to care more about
69 overflows (as MAX + 1 is sometimes produced as the estimate on number
70 of executions of STMT).
71 b) it is consistent with the result of number_of_iterations_exit. */
74 /* True if the statement will cause the loop to be leaved the (at most)
75 BOUND + 1-st time it is executed, that is, all the statements after it
76 are executed at most BOUND times. */
79 /* The next bound in the list. */
80 struct nb_iter_bound *next;
83 /* Description of the loop exit. */
85 struct GTY ((for_user)) loop_exit {
89 /* Previous and next exit in the list of the exits of the loop. */
90 struct loop_exit *prev;
91 struct loop_exit *next;
93 /* Next element in the list of loops from that E exits. */
94 struct loop_exit *next_e;
97 struct loop_exit_hasher : ggc_hasher<loop_exit *>
99 typedef edge compare_type;
101 static hashval_t hash (loop_exit *);
102 static bool equal (loop_exit *, edge);
103 static void remove (loop_exit *);
106 typedef struct loop *loop_p;
108 /* An integer estimation of the number of iterations. Estimate_state
109 describes what is the state of the estimation. */
112 /* Estimate was not computed yet. */
114 /* Estimate is ready. */
119 /* The structure describing non-overflow control induction variable for
121 struct GTY ((chain_next ("%h.next"))) control_iv {
124 struct control_iv *next;
127 /* Structure to hold information for each natural loop. */
128 struct GTY ((chain_next ("%h.next"))) loop {
129 /* Index into loops array. */
132 /* Number of loop insns. */
135 /* Basic block of loop header. */
138 /* Basic block of loop latch. */
141 /* For loop unrolling/peeling decision. */
142 struct lpt_decision lpt_decision;
144 /* Average number of executed insns per iteration. */
147 /* Number of blocks contained within the loop. */
150 /* Superloops of the loop, starting with the outermost loop. */
151 vec<loop_p, va_gc> *superloops;
153 /* The first inner (child) loop or NULL if innermost loop. */
156 /* Link to the next (sibling) loop. */
159 /* Auxiliary info specific to a pass. */
160 PTR GTY ((skip (""))) aux;
162 /* The number of times the latch of the loop is executed. This can be an
163 INTEGER_CST, or a symbolic expression representing the number of
164 iterations like "N - 1", or a COND_EXPR containing the runtime
165 conditions under which the number of iterations is non zero.
167 Don't access this field directly: number_of_latch_executions
168 computes and caches the computed information in this field. */
171 /* An integer guaranteed to be greater or equal to nb_iterations. Only
172 valid if any_upper_bound is true. */
173 widest_int nb_iterations_upper_bound;
175 /* An integer giving an estimate on nb_iterations. Unlike
176 nb_iterations_upper_bound, there is no guarantee that it is at least
178 widest_int nb_iterations_estimate;
180 bool any_upper_bound;
183 /* True if the loop can be parallel. */
184 bool can_be_parallel;
186 /* True if -Waggressive-loop-optimizations warned about this loop
188 bool warned_aggressive_loop_optimizations;
190 /* An integer estimation of the number of iterations. Estimate_state
191 describes what is the state of the estimation. */
192 enum loop_estimation estimate_state;
194 /* If > 0, an integer, where the user asserted that for any
195 I in [ 0, nb_iterations ) and for any J in
196 [ I, min ( I + safelen, nb_iterations ) ), the Ith and Jth iterations
197 of the loop can be safely evaluated concurrently. */
200 /* True if this loop should never be vectorized. */
203 /* True if we should try harder to vectorize this loop. */
204 bool force_vectorize;
206 /* For SIMD loops, this is a unique identifier of the loop, referenced
207 by IFN_GOMP_SIMD_VF, IFN_GOMP_SIMD_LANE and IFN_GOMP_SIMD_LAST_LANE
211 /* Upper bound on number of iterations of a loop. */
212 struct nb_iter_bound *bounds;
214 /* Non-overflow control ivs of a loop. */
215 struct control_iv *control_ivs;
217 /* Head of the cyclic list of the exits of the loop. */
218 struct loop_exit *exits;
220 /* Number of iteration analysis data for RTL. */
221 struct niter_desc *simple_loop_desc;
223 /* For sanity checking during loop fixup we record here the former
224 loop header for loops marked for removal. Note that this prevents
225 the basic-block from being collected but its index can still be
227 basic_block former_header;
230 /* Flags for state of loop structure. */
233 LOOPS_HAVE_PREHEADERS = 1,
234 LOOPS_HAVE_SIMPLE_LATCHES = 2,
235 LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS = 4,
236 LOOPS_HAVE_RECORDED_EXITS = 8,
237 LOOPS_MAY_HAVE_MULTIPLE_LATCHES = 16,
238 LOOP_CLOSED_SSA = 32,
239 LOOPS_NEED_FIXUP = 64,
240 LOOPS_HAVE_FALLTHRU_PREHEADERS = 128
243 #define LOOPS_NORMAL (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_SIMPLE_LATCHES \
244 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
245 #define AVOID_CFG_MODIFICATIONS (LOOPS_MAY_HAVE_MULTIPLE_LATCHES)
247 /* Structure to hold CFG information about natural loops within a function. */
248 struct GTY (()) loops {
249 /* State of loops. */
252 /* Array of the loops. */
253 vec<loop_p, va_gc> *larray;
255 /* Maps edges to the list of their descriptions as loop exits. Edges
256 whose sources or destinations have loop_father == NULL (which may
257 happen during the cfg manipulations) should not appear in EXITS. */
258 hash_table<loop_exit_hasher> *GTY(()) exits;
260 /* Pointer to root of loop hierarchy tree. */
261 struct loop *tree_root;
264 /* Loop recognition. */
265 bool bb_loop_header_p (basic_block);
266 void init_loops_structure (struct function *, struct loops *, unsigned);
267 extern struct loops *flow_loops_find (struct loops *);
268 extern void disambiguate_loops_with_multiple_latches (void);
269 extern void flow_loops_free (struct loops *);
270 extern void flow_loops_dump (FILE *,
271 void (*)(const struct loop *, FILE *, int), int);
272 extern void flow_loop_dump (const struct loop *, FILE *,
273 void (*)(const struct loop *, FILE *, int), int);
274 struct loop *alloc_loop (void);
275 extern void flow_loop_free (struct loop *);
276 int flow_loop_nodes_find (basic_block, struct loop *);
277 unsigned fix_loop_structure (bitmap changed_bbs);
278 bool mark_irreducible_loops (void);
279 void release_recorded_exits (void);
280 void record_loop_exits (void);
281 void rescan_loop_exit (edge, bool, bool);
283 /* Loop data structure manipulation/querying. */
284 extern void flow_loop_tree_node_add (struct loop *, struct loop *);
285 extern void flow_loop_tree_node_remove (struct loop *);
286 extern bool flow_loop_nested_p (const struct loop *, const struct loop *);
287 extern bool flow_bb_inside_loop_p (const struct loop *, const_basic_block);
288 extern struct loop * find_common_loop (struct loop *, struct loop *);
289 struct loop *superloop_at_depth (struct loop *, unsigned);
290 struct eni_weights_d;
291 extern int num_loop_insns (const struct loop *);
292 extern int average_num_loop_insns (const struct loop *);
293 extern unsigned get_loop_level (const struct loop *);
294 extern bool loop_exit_edge_p (const struct loop *, const_edge);
295 extern bool loop_exits_to_bb_p (struct loop *, basic_block);
296 extern bool loop_exits_from_bb_p (struct loop *, basic_block);
297 extern void mark_loop_exit_edges (void);
298 extern location_t get_loop_location (struct loop *loop);
300 /* Loops & cfg manipulation. */
301 extern basic_block *get_loop_body (const struct loop *);
302 extern unsigned get_loop_body_with_size (const struct loop *, basic_block *,
304 extern basic_block *get_loop_body_in_dom_order (const struct loop *);
305 extern basic_block *get_loop_body_in_bfs_order (const struct loop *);
306 extern basic_block *get_loop_body_in_custom_order (const struct loop *,
307 int (*) (const void *, const void *));
309 extern vec<edge> get_loop_exit_edges (const struct loop *);
310 extern edge single_exit (const struct loop *);
311 extern edge single_likely_exit (struct loop *loop);
312 extern unsigned num_loop_branches (const struct loop *);
314 extern edge loop_preheader_edge (const struct loop *);
315 extern edge loop_latch_edge (const struct loop *);
317 extern void add_bb_to_loop (basic_block, struct loop *);
318 extern void remove_bb_from_loops (basic_block);
320 extern void cancel_loop_tree (struct loop *);
321 extern void delete_loop (struct loop *);
324 extern void verify_loop_structure (void);
327 extern bool just_once_each_iteration_p (const struct loop *, const_basic_block);
328 gcov_type expected_loop_iterations_unbounded (const struct loop *);
329 extern unsigned expected_loop_iterations (const struct loop *);
330 extern rtx doloop_condition_get (rtx);
332 void mark_loop_for_removal (loop_p);
334 /* Induction variable analysis. */
336 /* The description of induction variable. The things are a bit complicated
337 due to need to handle subregs and extends. The value of the object described
338 by it can be obtained as follows (all computations are done in extend_mode):
340 Value in i-th iteration is
341 delta + mult * extend_{extend_mode} (subreg_{mode} (base + i * step)).
343 If first_special is true, the value in the first iteration is
346 If extend = UNKNOWN, first_special must be false, delta 0, mult 1 and value is
347 subreg_{mode} (base + i * step)
349 The get_iv_value function can be used to obtain these expressions.
351 ??? Add a third mode field that would specify the mode in that inner
352 computation is done, which would enable it to be different from the
357 /* Its base and step (mode of base and step is supposed to be extend_mode,
358 see the description above). */
361 /* The type of extend applied to it (IV_SIGN_EXTEND, IV_ZERO_EXTEND,
362 or IV_UNKNOWN_EXTEND). */
363 enum iv_extend_code extend;
365 /* Operations applied in the extended mode. */
368 /* The mode it is extended to. */
369 machine_mode extend_mode;
371 /* The mode the variable iterates in. */
374 /* Whether the first iteration needs to be handled specially. */
375 unsigned first_special : 1;
378 /* The description of an exit from the loop and of the number of iterations
379 till we take the exit. */
381 struct GTY(()) niter_desc
383 /* The edge out of the loop. */
386 /* The other edge leading from the condition. */
389 /* True if we are able to say anything about number of iterations of the
393 /* True if the loop iterates the constant number of times. */
396 /* Number of iterations if constant. */
399 /* Assumptions under that the rest of the information is valid. */
402 /* Assumptions under that the loop ends before reaching the latch,
403 even if value of niter_expr says otherwise. */
404 rtx noloop_assumptions;
406 /* Condition under that the loop is infinite. */
409 /* Whether the comparison is signed. */
412 /* The mode in that niter_expr should be computed. */
415 /* The number of iterations of the loop. */
419 extern void iv_analysis_loop_init (struct loop *);
420 extern bool iv_analyze (rtx_insn *, rtx, struct rtx_iv *);
421 extern bool iv_analyze_result (rtx_insn *, rtx, struct rtx_iv *);
422 extern bool iv_analyze_expr (rtx_insn *, rtx, machine_mode,
424 extern rtx get_iv_value (struct rtx_iv *, rtx);
425 extern bool biv_p (rtx_insn *, rtx);
426 extern void find_simple_exit (struct loop *, struct niter_desc *);
427 extern void iv_analysis_done (void);
429 extern struct niter_desc *get_simple_loop_desc (struct loop *loop);
430 extern void free_simple_loop_desc (struct loop *loop);
432 static inline struct niter_desc *
433 simple_loop_desc (struct loop *loop)
435 return loop->simple_loop_desc;
438 /* Accessors for the loop structures. */
440 /* Returns the loop with index NUM from FNs loop tree. */
442 static inline struct loop *
443 get_loop (struct function *fn, unsigned num)
445 return (*loops_for_fn (fn)->larray)[num];
448 /* Returns the number of superloops of LOOP. */
450 static inline unsigned
451 loop_depth (const struct loop *loop)
453 return vec_safe_length (loop->superloops);
456 /* Returns the immediate superloop of LOOP, or NULL if LOOP is the outermost
459 static inline struct loop *
460 loop_outer (const struct loop *loop)
462 unsigned n = vec_safe_length (loop->superloops);
467 return (*loop->superloops)[n - 1];
470 /* Returns true if LOOP has at least one exit edge. */
473 loop_has_exit_edges (const struct loop *loop)
475 return loop->exits->next->e != NULL;
478 /* Returns the list of loops in FN. */
480 inline vec<loop_p, va_gc> *
481 get_loops (struct function *fn)
483 struct loops *loops = loops_for_fn (fn);
487 return loops->larray;
490 /* Returns the number of loops in FN (including the removed
491 ones and the fake loop that forms the root of the loop tree). */
493 static inline unsigned
494 number_of_loops (struct function *fn)
496 struct loops *loops = loops_for_fn (fn);
500 return vec_safe_length (loops->larray);
503 /* Returns true if state of the loops satisfies all properties
504 described by FLAGS. */
507 loops_state_satisfies_p (unsigned flags)
509 return (current_loops->state & flags) == flags;
512 /* Sets FLAGS to the loops state. */
515 loops_state_set (unsigned flags)
517 current_loops->state |= flags;
520 /* Clears FLAGS from the loops state. */
523 loops_state_clear (unsigned flags)
527 current_loops->state &= ~flags;
530 /* Loop iterators. */
532 /* Flags for loop iteration. */
536 LI_INCLUDE_ROOT = 1, /* Include the fake root of the loop tree. */
537 LI_FROM_INNERMOST = 2, /* Iterate over the loops in the reverse order,
538 starting from innermost ones. */
539 LI_ONLY_INNERMOST = 4 /* Iterate only over innermost loops. */
542 /* The iterator for loops. */
546 loop_iterator (loop_p *loop, unsigned flags);
549 inline loop_p next ();
551 /* The list of loops to visit. */
554 /* The index of the actual loop. */
559 loop_iterator::next ()
563 while (this->to_visit.iterate (this->idx, &anum))
566 loop_p loop = get_loop (cfun, anum);
575 loop_iterator::loop_iterator (loop_p *loop, unsigned flags)
584 this->to_visit.create (0);
589 this->to_visit.create (number_of_loops (cfun));
590 mn = (flags & LI_INCLUDE_ROOT) ? 0 : 1;
592 if (flags & LI_ONLY_INNERMOST)
594 for (i = 0; vec_safe_iterate (current_loops->larray, i, &aloop); i++)
596 && aloop->inner == NULL
598 this->to_visit.quick_push (aloop->num);
600 else if (flags & LI_FROM_INNERMOST)
602 /* Push the loops to LI->TO_VISIT in postorder. */
603 for (aloop = current_loops->tree_root;
604 aloop->inner != NULL;
605 aloop = aloop->inner)
610 if (aloop->num >= mn)
611 this->to_visit.quick_push (aloop->num);
615 for (aloop = aloop->next;
616 aloop->inner != NULL;
617 aloop = aloop->inner)
620 else if (!loop_outer (aloop))
623 aloop = loop_outer (aloop);
628 /* Push the loops to LI->TO_VISIT in preorder. */
629 aloop = current_loops->tree_root;
632 if (aloop->num >= mn)
633 this->to_visit.quick_push (aloop->num);
635 if (aloop->inner != NULL)
636 aloop = aloop->inner;
639 while (aloop != NULL && aloop->next == NULL)
640 aloop = loop_outer (aloop);
648 *loop = this->next ();
652 loop_iterator::~loop_iterator ()
654 this->to_visit.release ();
657 #define FOR_EACH_LOOP(LOOP, FLAGS) \
658 for (loop_iterator li(&(LOOP), FLAGS); \
662 /* The properties of the target. */
663 struct target_cfgloop {
664 /* Number of available registers. */
665 unsigned x_target_avail_regs;
667 /* Number of available registers that are call-clobbered. */
668 unsigned x_target_clobbered_regs;
670 /* Number of registers reserved for temporary expressions. */
671 unsigned x_target_res_regs;
673 /* The cost for register when there still is some reserve, but we are
674 approaching the number of available registers. */
675 unsigned x_target_reg_cost[2];
677 /* The cost for register when we need to spill. */
678 unsigned x_target_spill_cost[2];
681 extern struct target_cfgloop default_target_cfgloop;
682 #if SWITCHABLE_TARGET
683 extern struct target_cfgloop *this_target_cfgloop;
685 #define this_target_cfgloop (&default_target_cfgloop)
688 #define target_avail_regs \
689 (this_target_cfgloop->x_target_avail_regs)
690 #define target_clobbered_regs \
691 (this_target_cfgloop->x_target_clobbered_regs)
692 #define target_res_regs \
693 (this_target_cfgloop->x_target_res_regs)
694 #define target_reg_cost \
695 (this_target_cfgloop->x_target_reg_cost)
696 #define target_spill_cost \
697 (this_target_cfgloop->x_target_spill_cost)
699 /* Register pressure estimation for induction variable optimizations & loop
701 extern unsigned estimate_reg_pressure_cost (unsigned, unsigned, bool, bool);
702 extern void init_set_costs (void);
704 /* Loop optimizer initialization. */
705 extern void loop_optimizer_init (unsigned);
706 extern void loop_optimizer_finalize (void);
708 /* Optimization passes. */
711 UAP_UNROLL = 1, /* Enables unrolling of loops if it seems profitable. */
712 UAP_UNROLL_ALL = 2 /* Enables unrolling of all loops. */
715 extern void doloop_optimize_loops (void);
716 extern void move_loop_invariants (void);
717 extern vec<basic_block> get_loop_hot_path (const struct loop *loop);
719 /* Returns the outermost loop of the loop nest that contains LOOP.*/
720 static inline struct loop *
721 loop_outermost (struct loop *loop)
723 unsigned n = vec_safe_length (loop->superloops);
728 return (*loop->superloops)[1];
731 extern void record_niter_bound (struct loop *, const widest_int &, bool, bool);
732 extern HOST_WIDE_INT get_estimated_loop_iterations_int (struct loop *);
733 extern HOST_WIDE_INT get_max_loop_iterations_int (struct loop *);
734 extern bool get_estimated_loop_iterations (struct loop *loop, widest_int *nit);
735 extern bool get_max_loop_iterations (struct loop *loop, widest_int *nit);
736 extern int bb_loop_depth (const_basic_block);
738 /* Converts VAL to widest_int. */
740 static inline widest_int
741 gcov_type_to_wide_int (gcov_type val)
745 a[0] = (unsigned HOST_WIDE_INT) val;
746 /* If HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_WIDEST_INT, avoid shifting by
748 val >>= HOST_BITS_PER_WIDE_INT - 1;
750 a[1] = (unsigned HOST_WIDE_INT) val;
752 return widest_int::from_array (a, 2);
754 #endif /* GCC_CFGLOOP_H */