configure.ac (multiarch): Use $enableval instead of $withval.
[official-gcc.git] / gcc / cfgloop.h
blob9e2e02de93dac26403ce714a1d9b6b7c12447aae
1 /* Natural loop functions
2 Copyright (C) 1987, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #ifndef GCC_CFGLOOP_H
22 #define GCC_CFGLOOP_H
24 #include "basic-block.h"
25 #include "double-int.h"
27 #include "bitmap.h"
28 #include "sbitmap.h"
30 /* Structure to hold decision about unrolling/peeling. */
31 enum lpt_dec
33 LPT_NONE,
34 LPT_PEEL_COMPLETELY,
35 LPT_PEEL_SIMPLE,
36 LPT_UNROLL_CONSTANT,
37 LPT_UNROLL_RUNTIME,
38 LPT_UNROLL_STUPID
41 struct GTY (()) lpt_decision {
42 enum lpt_dec decision;
43 unsigned times;
46 /* The type of extend applied to an IV. */
47 enum iv_extend_code
49 IV_SIGN_EXTEND,
50 IV_ZERO_EXTEND,
51 IV_UNKNOWN_EXTEND
54 /* The structure describing a bound on number of iterations of a loop. */
56 struct GTY ((chain_next ("%h.next"))) nb_iter_bound {
57 /* The statement STMT is executed at most ... */
58 gimple stmt;
60 /* ... BOUND + 1 times (BOUND must be an unsigned constant).
61 The + 1 is added for the following reasons:
63 a) 0 would otherwise be unused, while we would need to care more about
64 overflows (as MAX + 1 is sometimes produced as the estimate on number
65 of executions of STMT).
66 b) it is consistent with the result of number_of_iterations_exit. */
67 double_int bound;
69 /* True if the statement will cause the loop to be leaved the (at most)
70 BOUND + 1-st time it is executed, that is, all the statements after it
71 are executed at most BOUND times. */
72 bool is_exit;
74 /* The next bound in the list. */
75 struct nb_iter_bound *next;
78 /* Description of the loop exit. */
80 struct GTY (()) loop_exit {
81 /* The exit edge. */
82 edge e;
84 /* Previous and next exit in the list of the exits of the loop. */
85 struct loop_exit *prev;
86 struct loop_exit *next;
88 /* Next element in the list of loops from that E exits. */
89 struct loop_exit *next_e;
92 typedef struct loop *loop_p;
94 /* An integer estimation of the number of iterations. Estimate_state
95 describes what is the state of the estimation. */
96 enum loop_estimation
98 /* Estimate was not computed yet. */
99 EST_NOT_COMPUTED,
100 /* Estimate is ready. */
101 EST_AVAILABLE
104 /* Structure to hold information for each natural loop. */
105 struct GTY ((chain_next ("%h.next"))) loop {
106 /* Index into loops array. */
107 int num;
109 /* Number of loop insns. */
110 unsigned ninsns;
112 /* Basic block of loop header. */
113 basic_block header;
115 /* Basic block of loop latch. */
116 basic_block latch;
118 /* For loop unrolling/peeling decision. */
119 struct lpt_decision lpt_decision;
121 /* Average number of executed insns per iteration. */
122 unsigned av_ninsns;
124 /* Number of blocks contained within the loop. */
125 unsigned num_nodes;
127 /* Superloops of the loop, starting with the outermost loop. */
128 vec<loop_p, va_gc> *superloops;
130 /* The first inner (child) loop or NULL if innermost loop. */
131 struct loop *inner;
133 /* Link to the next (sibling) loop. */
134 struct loop *next;
136 /* Auxiliary info specific to a pass. */
137 PTR GTY ((skip (""))) aux;
139 /* The number of times the latch of the loop is executed. This can be an
140 INTEGER_CST, or a symbolic expression representing the number of
141 iterations like "N - 1", or a COND_EXPR containing the runtime
142 conditions under which the number of iterations is non zero.
144 Don't access this field directly: number_of_latch_executions
145 computes and caches the computed information in this field. */
146 tree nb_iterations;
148 /* An integer guaranteed to be greater or equal to nb_iterations. Only
149 valid if any_upper_bound is true. */
150 double_int nb_iterations_upper_bound;
152 /* An integer giving an estimate on nb_iterations. Unlike
153 nb_iterations_upper_bound, there is no guarantee that it is at least
154 nb_iterations. */
155 double_int nb_iterations_estimate;
157 bool any_upper_bound;
158 bool any_estimate;
160 /* True if the loop can be parallel. */
161 bool can_be_parallel;
163 /* An integer estimation of the number of iterations. Estimate_state
164 describes what is the state of the estimation. */
165 enum loop_estimation estimate_state;
167 /* Upper bound on number of iterations of a loop. */
168 struct nb_iter_bound *bounds;
170 /* Head of the cyclic list of the exits of the loop. */
171 struct loop_exit *exits;
174 /* Flags for state of loop structure. */
175 enum
177 LOOPS_HAVE_PREHEADERS = 1,
178 LOOPS_HAVE_SIMPLE_LATCHES = 2,
179 LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS = 4,
180 LOOPS_HAVE_RECORDED_EXITS = 8,
181 LOOPS_MAY_HAVE_MULTIPLE_LATCHES = 16,
182 LOOP_CLOSED_SSA = 32,
183 LOOPS_NEED_FIXUP = 64,
184 LOOPS_HAVE_FALLTHRU_PREHEADERS = 128
187 #define LOOPS_NORMAL (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_SIMPLE_LATCHES \
188 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
189 #define AVOID_CFG_MODIFICATIONS (LOOPS_MAY_HAVE_MULTIPLE_LATCHES)
191 /* Structure to hold CFG information about natural loops within a function. */
192 struct GTY (()) loops {
193 /* State of loops. */
194 int state;
196 /* Array of the loops. */
197 vec<loop_p, va_gc> *larray;
199 /* Maps edges to the list of their descriptions as loop exits. Edges
200 whose sources or destinations have loop_father == NULL (which may
201 happen during the cfg manipulations) should not appear in EXITS. */
202 htab_t GTY((param_is (struct loop_exit))) exits;
204 /* Pointer to root of loop hierarchy tree. */
205 struct loop *tree_root;
208 /* Loop recognition. */
209 extern int flow_loops_find (struct loops *);
210 extern void disambiguate_loops_with_multiple_latches (void);
211 extern void flow_loops_free (struct loops *);
212 extern void flow_loops_dump (FILE *,
213 void (*)(const struct loop *, FILE *, int), int);
214 extern void flow_loop_dump (const struct loop *, FILE *,
215 void (*)(const struct loop *, FILE *, int), int);
216 struct loop *alloc_loop (void);
217 extern void flow_loop_free (struct loop *);
218 int flow_loop_nodes_find (basic_block, struct loop *);
219 void fix_loop_structure (bitmap changed_bbs);
220 bool mark_irreducible_loops (void);
221 void release_recorded_exits (void);
222 void record_loop_exits (void);
223 void rescan_loop_exit (edge, bool, bool);
225 /* Loop data structure manipulation/querying. */
226 extern void flow_loop_tree_node_add (struct loop *, struct loop *);
227 extern void flow_loop_tree_node_remove (struct loop *);
228 extern void add_loop (struct loop *, struct loop *);
229 extern bool flow_loop_nested_p (const struct loop *, const struct loop *);
230 extern bool flow_bb_inside_loop_p (const struct loop *, const_basic_block);
231 extern struct loop * find_common_loop (struct loop *, struct loop *);
232 struct loop *superloop_at_depth (struct loop *, unsigned);
233 struct eni_weights_d;
234 extern unsigned tree_num_loop_insns (struct loop *, struct eni_weights_d *);
235 extern int num_loop_insns (const struct loop *);
236 extern int average_num_loop_insns (const struct loop *);
237 extern unsigned get_loop_level (const struct loop *);
238 extern bool loop_exit_edge_p (const struct loop *, const_edge);
239 extern bool loop_exits_to_bb_p (struct loop *, basic_block);
240 extern bool loop_exits_from_bb_p (struct loop *, basic_block);
241 extern void mark_loop_exit_edges (void);
243 /* Loops & cfg manipulation. */
244 extern basic_block *get_loop_body (const struct loop *);
245 extern unsigned get_loop_body_with_size (const struct loop *, basic_block *,
246 unsigned);
247 extern basic_block *get_loop_body_in_dom_order (const struct loop *);
248 extern basic_block *get_loop_body_in_bfs_order (const struct loop *);
249 extern basic_block *get_loop_body_in_custom_order (const struct loop *,
250 int (*) (const void *, const void *));
252 extern vec<edge> get_loop_exit_edges (const struct loop *);
253 extern edge single_exit (const struct loop *);
254 extern edge single_likely_exit (struct loop *loop);
255 extern unsigned num_loop_branches (const struct loop *);
257 extern edge loop_preheader_edge (const struct loop *);
258 extern edge loop_latch_edge (const struct loop *);
260 extern void add_bb_to_loop (basic_block, struct loop *);
261 extern void remove_bb_from_loops (basic_block);
263 extern void cancel_loop_tree (struct loop *);
264 extern void delete_loop (struct loop *);
266 enum
268 CP_SIMPLE_PREHEADERS = 1,
269 CP_FALLTHRU_PREHEADERS = 2
272 basic_block create_preheader (struct loop *, int);
273 extern void create_preheaders (int);
274 extern void force_single_succ_latches (void);
276 extern void verify_loop_structure (void);
278 /* Loop analysis. */
279 extern bool just_once_each_iteration_p (const struct loop *, const_basic_block);
280 gcov_type expected_loop_iterations_unbounded (const struct loop *);
281 extern unsigned expected_loop_iterations (const struct loop *);
282 extern rtx doloop_condition_get (rtx);
284 void estimate_numbers_of_iterations_loop (struct loop *);
285 void record_niter_bound (struct loop *, double_int, bool, bool);
286 bool estimated_loop_iterations (struct loop *, double_int *);
287 bool max_loop_iterations (struct loop *, double_int *);
288 HOST_WIDE_INT estimated_loop_iterations_int (struct loop *);
289 HOST_WIDE_INT max_loop_iterations_int (struct loop *);
290 bool max_stmt_executions (struct loop *, double_int *);
291 bool estimated_stmt_executions (struct loop *, double_int *);
292 HOST_WIDE_INT max_stmt_executions_int (struct loop *);
293 HOST_WIDE_INT estimated_stmt_executions_int (struct loop *);
295 /* Loop manipulation. */
296 extern bool can_duplicate_loop_p (const struct loop *loop);
298 #define DLTHE_FLAG_UPDATE_FREQ 1 /* Update frequencies in
299 duplicate_loop_to_header_edge. */
300 #define DLTHE_RECORD_COPY_NUMBER 2 /* Record copy number in the aux
301 field of newly create BB. */
302 #define DLTHE_FLAG_COMPLETTE_PEEL 4 /* Update frequencies expecting
303 a complete peeling. */
305 extern edge create_empty_if_region_on_edge (edge, tree);
306 extern struct loop *create_empty_loop_on_edge (edge, tree, tree, tree, tree,
307 tree *, tree *, struct loop *);
308 extern struct loop * duplicate_loop (struct loop *, struct loop *);
309 extern void copy_loop_info (struct loop *loop, struct loop *target);
310 extern void duplicate_subloops (struct loop *, struct loop *);
311 extern bool duplicate_loop_to_header_edge (struct loop *, edge,
312 unsigned, sbitmap, edge,
313 vec<edge> *, int);
314 extern struct loop *loopify (edge, edge,
315 basic_block, edge, edge, bool,
316 unsigned, unsigned);
317 struct loop * loop_version (struct loop *, void *,
318 basic_block *, unsigned, unsigned, unsigned, bool);
319 extern bool remove_path (edge);
320 extern void unloop (struct loop *, bool *, bitmap);
321 extern void scale_loop_frequencies (struct loop *, int, int);
323 /* Induction variable analysis. */
325 /* The description of induction variable. The things are a bit complicated
326 due to need to handle subregs and extends. The value of the object described
327 by it can be obtained as follows (all computations are done in extend_mode):
329 Value in i-th iteration is
330 delta + mult * extend_{extend_mode} (subreg_{mode} (base + i * step)).
332 If first_special is true, the value in the first iteration is
333 delta + mult * base
335 If extend = UNKNOWN, first_special must be false, delta 0, mult 1 and value is
336 subreg_{mode} (base + i * step)
338 The get_iv_value function can be used to obtain these expressions.
340 ??? Add a third mode field that would specify the mode in that inner
341 computation is done, which would enable it to be different from the
342 outer one? */
344 struct rtx_iv
346 /* Its base and step (mode of base and step is supposed to be extend_mode,
347 see the description above). */
348 rtx base, step;
350 /* The type of extend applied to it (IV_SIGN_EXTEND, IV_ZERO_EXTEND,
351 or IV_UNKNOWN_EXTEND). */
352 enum iv_extend_code extend;
354 /* Operations applied in the extended mode. */
355 rtx delta, mult;
357 /* The mode it is extended to. */
358 enum machine_mode extend_mode;
360 /* The mode the variable iterates in. */
361 enum machine_mode mode;
363 /* Whether the first iteration needs to be handled specially. */
364 unsigned first_special : 1;
367 /* The description of an exit from the loop and of the number of iterations
368 till we take the exit. */
370 struct niter_desc
372 /* The edge out of the loop. */
373 edge out_edge;
375 /* The other edge leading from the condition. */
376 edge in_edge;
378 /* True if we are able to say anything about number of iterations of the
379 loop. */
380 bool simple_p;
382 /* True if the loop iterates the constant number of times. */
383 bool const_iter;
385 /* Number of iterations if constant. */
386 unsigned HOST_WIDEST_INT niter;
388 /* Assumptions under that the rest of the information is valid. */
389 rtx assumptions;
391 /* Assumptions under that the loop ends before reaching the latch,
392 even if value of niter_expr says otherwise. */
393 rtx noloop_assumptions;
395 /* Condition under that the loop is infinite. */
396 rtx infinite;
398 /* Whether the comparison is signed. */
399 bool signed_p;
401 /* The mode in that niter_expr should be computed. */
402 enum machine_mode mode;
404 /* The number of iterations of the loop. */
405 rtx niter_expr;
408 extern void iv_analysis_loop_init (struct loop *);
409 extern bool iv_analyze (rtx, rtx, struct rtx_iv *);
410 extern bool iv_analyze_result (rtx, rtx, struct rtx_iv *);
411 extern bool iv_analyze_expr (rtx, rtx, enum machine_mode, struct rtx_iv *);
412 extern rtx get_iv_value (struct rtx_iv *, rtx);
413 extern bool biv_p (rtx, rtx);
414 extern void find_simple_exit (struct loop *, struct niter_desc *);
415 extern void iv_analysis_done (void);
417 extern struct niter_desc *get_simple_loop_desc (struct loop *loop);
418 extern void free_simple_loop_desc (struct loop *loop);
420 static inline struct niter_desc *
421 simple_loop_desc (struct loop *loop)
423 return (struct niter_desc *) loop->aux;
426 /* Accessors for the loop structures. */
428 /* Returns the loop with index NUM from current_loops. */
430 static inline struct loop *
431 get_loop (unsigned num)
433 return (*current_loops->larray)[num];
436 /* Returns the number of superloops of LOOP. */
438 static inline unsigned
439 loop_depth (const struct loop *loop)
441 return vec_safe_length (loop->superloops);
444 /* Returns the loop depth of the loop BB belongs to. */
446 static inline int
447 bb_loop_depth (const_basic_block bb)
449 return bb->loop_father ? loop_depth (bb->loop_father) : 0;
452 /* Returns the immediate superloop of LOOP, or NULL if LOOP is the outermost
453 loop. */
455 static inline struct loop *
456 loop_outer (const struct loop *loop)
458 unsigned n = vec_safe_length (loop->superloops);
460 if (n == 0)
461 return NULL;
463 return (*loop->superloops)[n - 1];
466 /* Returns true if LOOP has at least one exit edge. */
468 static inline bool
469 loop_has_exit_edges (const struct loop *loop)
471 return loop->exits->next->e != NULL;
474 /* Returns the list of loops in current_loops. */
476 static inline vec<loop_p, va_gc> *
477 get_loops (void)
479 if (!current_loops)
480 return NULL;
482 return current_loops->larray;
485 /* Returns the number of loops in current_loops (including the removed
486 ones and the fake loop that forms the root of the loop tree). */
488 static inline unsigned
489 number_of_loops (void)
491 if (!current_loops)
492 return 0;
494 return vec_safe_length (current_loops->larray);
497 /* Returns true if state of the loops satisfies all properties
498 described by FLAGS. */
500 static inline bool
501 loops_state_satisfies_p (unsigned flags)
503 return (current_loops->state & flags) == flags;
506 /* Sets FLAGS to the loops state. */
508 static inline void
509 loops_state_set (unsigned flags)
511 current_loops->state |= flags;
514 /* Clears FLAGS from the loops state. */
516 static inline void
517 loops_state_clear (unsigned flags)
519 if (!current_loops)
520 return;
521 current_loops->state &= ~flags;
524 /* Loop iterators. */
526 /* Flags for loop iteration. */
528 enum li_flags
530 LI_INCLUDE_ROOT = 1, /* Include the fake root of the loop tree. */
531 LI_FROM_INNERMOST = 2, /* Iterate over the loops in the reverse order,
532 starting from innermost ones. */
533 LI_ONLY_INNERMOST = 4 /* Iterate only over innermost loops. */
536 /* The iterator for loops. */
538 typedef struct
540 /* The list of loops to visit. */
541 vec<int> to_visit;
543 /* The index of the actual loop. */
544 unsigned idx;
545 } loop_iterator;
547 static inline void
548 fel_next (loop_iterator *li, loop_p *loop)
550 int anum;
552 while (li->to_visit.iterate (li->idx, &anum))
554 li->idx++;
555 *loop = get_loop (anum);
556 if (*loop)
557 return;
560 li->to_visit.release ();
561 *loop = NULL;
564 static inline void
565 fel_init (loop_iterator *li, loop_p *loop, unsigned flags)
567 struct loop *aloop;
568 unsigned i;
569 int mn;
571 li->idx = 0;
572 if (!current_loops)
574 li->to_visit.create (0);
575 *loop = NULL;
576 return;
579 li->to_visit.create (number_of_loops ());
580 mn = (flags & LI_INCLUDE_ROOT) ? 0 : 1;
582 if (flags & LI_ONLY_INNERMOST)
584 for (i = 0; vec_safe_iterate (current_loops->larray, i, &aloop); i++)
585 if (aloop != NULL
586 && aloop->inner == NULL
587 && aloop->num >= mn)
588 li->to_visit.quick_push (aloop->num);
590 else if (flags & LI_FROM_INNERMOST)
592 /* Push the loops to LI->TO_VISIT in postorder. */
593 for (aloop = current_loops->tree_root;
594 aloop->inner != NULL;
595 aloop = aloop->inner)
596 continue;
598 while (1)
600 if (aloop->num >= mn)
601 li->to_visit.quick_push (aloop->num);
603 if (aloop->next)
605 for (aloop = aloop->next;
606 aloop->inner != NULL;
607 aloop = aloop->inner)
608 continue;
610 else if (!loop_outer (aloop))
611 break;
612 else
613 aloop = loop_outer (aloop);
616 else
618 /* Push the loops to LI->TO_VISIT in preorder. */
619 aloop = current_loops->tree_root;
620 while (1)
622 if (aloop->num >= mn)
623 li->to_visit.quick_push (aloop->num);
625 if (aloop->inner != NULL)
626 aloop = aloop->inner;
627 else
629 while (aloop != NULL && aloop->next == NULL)
630 aloop = loop_outer (aloop);
631 if (aloop == NULL)
632 break;
633 aloop = aloop->next;
638 fel_next (li, loop);
641 #define FOR_EACH_LOOP(LI, LOOP, FLAGS) \
642 for (fel_init (&(LI), &(LOOP), FLAGS); \
643 (LOOP); \
644 fel_next (&(LI), &(LOOP)))
646 #define FOR_EACH_LOOP_BREAK(LI) \
648 (LI).to_visit.release (); \
649 break; \
652 /* The properties of the target. */
653 struct target_cfgloop {
654 /* Number of available registers. */
655 unsigned x_target_avail_regs;
657 /* Number of available registers that are call-clobbered. */
658 unsigned x_target_clobbered_regs;
660 /* Number of registers reserved for temporary expressions. */
661 unsigned x_target_res_regs;
663 /* The cost for register when there still is some reserve, but we are
664 approaching the number of available registers. */
665 unsigned x_target_reg_cost[2];
667 /* The cost for register when we need to spill. */
668 unsigned x_target_spill_cost[2];
671 extern struct target_cfgloop default_target_cfgloop;
672 #if SWITCHABLE_TARGET
673 extern struct target_cfgloop *this_target_cfgloop;
674 #else
675 #define this_target_cfgloop (&default_target_cfgloop)
676 #endif
678 #define target_avail_regs \
679 (this_target_cfgloop->x_target_avail_regs)
680 #define target_clobbered_regs \
681 (this_target_cfgloop->x_target_clobbered_regs)
682 #define target_res_regs \
683 (this_target_cfgloop->x_target_res_regs)
684 #define target_reg_cost \
685 (this_target_cfgloop->x_target_reg_cost)
686 #define target_spill_cost \
687 (this_target_cfgloop->x_target_spill_cost)
689 /* Register pressure estimation for induction variable optimizations & loop
690 invariant motion. */
691 extern unsigned estimate_reg_pressure_cost (unsigned, unsigned, bool, bool);
692 extern void init_set_costs (void);
694 /* Loop optimizer initialization. */
695 extern void loop_optimizer_init (unsigned);
696 extern void loop_optimizer_finalize (void);
698 /* Optimization passes. */
699 extern void unswitch_loops (void);
701 enum
703 UAP_PEEL = 1, /* Enables loop peeling. */
704 UAP_UNROLL = 2, /* Enables unrolling of loops if it seems profitable. */
705 UAP_UNROLL_ALL = 4 /* Enables unrolling of all loops. */
708 extern void unroll_and_peel_loops (int);
709 extern void doloop_optimize_loops (void);
710 extern void move_loop_invariants (void);
711 extern bool finite_loop_p (struct loop *);
712 extern void scale_loop_profile (struct loop *loop, int scale, int iteration_bound);
713 extern vec<basic_block> get_loop_hot_path (const struct loop *loop);
715 /* Returns the outermost loop of the loop nest that contains LOOP.*/
716 static inline struct loop *
717 loop_outermost (struct loop *loop)
719 unsigned n = vec_safe_length (loop->superloops);
721 if (n <= 1)
722 return loop;
724 return (*loop->superloops)[1];
728 #endif /* GCC_CFGLOOP_H */