]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cfgloop.h
semantics.c (expand_or_defer_fn): Do not call c_record_cdtor_fn.
[thirdparty/gcc.git] / gcc / cfgloop.h
CommitLineData
3d436d2a 1/* Natural loop functions
613c5cd0 2 Copyright (C) 1987, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3d436d2a
ZD
3 Free Software Foundation, Inc.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING. If not, write to the Free
366ccddb
KC
19Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
2002110-1301, USA. */
3d436d2a 21
59587b18
JQ
22#ifndef GCC_CFGLOOP_H
23#define GCC_CFGLOOP_H
24
25#include "basic-block.h"
26/* For rtx_code. */
27#include "rtl.h"
677cc14d 28#include "vecprim.h"
df582833 29#include "double-int.h"
59587b18 30
3d436d2a
ZD
31/* Structure to hold decision about unrolling/peeling. */
32enum lpt_dec
33{
34 LPT_NONE,
35 LPT_PEEL_COMPLETELY,
36 LPT_PEEL_SIMPLE,
37 LPT_UNROLL_CONSTANT,
38 LPT_UNROLL_RUNTIME,
39 LPT_UNROLL_STUPID
40};
41
42struct lpt_decision
43{
44 enum lpt_dec decision;
45 unsigned times;
46};
47
86df10e3
SP
48/* The structure describing a bound on number of iterations of a loop. */
49
50struct nb_iter_bound
51{
946e1bc7
ZD
52 /* The statement STMT is executed at most ... */
53 tree stmt;
54
55 /* ... BOUND + 1 times (BOUND must be an unsigned constant).
56 The + 1 is added for the following reasons:
57
58 a) 0 would otherwise be unused, while we would need to care more about
59 overflows (as MAX + 1 is sometimes produced as the estimate on number
60 of executions of STMT).
61 b) it is consistent with the result of number_of_iterations_exit. */
62 double_int bound;
63
64 /* True if the statement will cause the loop to be leaved the (at most)
65 BOUND + 1-st time it is executed, that is, all the statements after it
66 are executed at most BOUND times. */
67 bool is_exit;
68
946e1bc7 69 /* The next bound in the list. */
86df10e3 70 struct nb_iter_bound *next;
86df10e3
SP
71};
72
6270df4c
ZD
73/* Description of the loop exit. */
74
75struct loop_exit
76{
77 /* The exit edge. */
78 edge e;
79
80 /* Previous and next exit in the list of the exits of the loop. */
81 struct loop_exit *prev;
82 struct loop_exit *next;
83
84 /* Next element in the list of loops from that E exits. */
85 struct loop_exit *next_e;
86};
87
3d436d2a
ZD
88/* Structure to hold information for each natural loop. */
89struct loop
90{
91 /* Index into loops array. */
92 int num;
93
94 /* Basic block of loop header. */
95 basic_block header;
96
97 /* Basic block of loop latch. */
98 basic_block latch;
99
3d436d2a
ZD
100 /* For loop unrolling/peeling decision. */
101 struct lpt_decision lpt_decision;
102
3d436d2a
ZD
103 /* Number of loop insns. */
104 unsigned ninsns;
105
106 /* Average number of executed insns per iteration. */
107 unsigned av_ninsns;
108
3d436d2a
ZD
109 /* Number of blocks contained within the loop. */
110 unsigned num_nodes;
111
3d436d2a
ZD
112 /* The loop nesting depth. */
113 int depth;
114
115 /* Superloops of the loop. */
116 struct loop **pred;
117
3d436d2a
ZD
118 /* The outer (parent) loop or NULL if outermost loop. */
119 struct loop *outer;
120
121 /* The first inner (child) loop or NULL if innermost loop. */
122 struct loop *inner;
123
124 /* Link to the next (sibling) loop. */
125 struct loop *next;
126
127 /* Loop that is copy of this loop. */
128 struct loop *copy;
129
3d436d2a
ZD
130 /* Auxiliary info specific to a pass. */
131 void *aux;
132
a14865db 133 /* The number of times the latch of the loop is executed.
9baba81b
SP
134 This is an INTEGER_CST or an expression containing symbolic
135 names. Don't access this field directly:
a14865db 136 number_of_latch_executions computes and caches the computed
9baba81b
SP
137 information in this field. */
138 tree nb_iterations;
139
946e1bc7
ZD
140 /* An integer estimation of the number of iterations. Estimate_state
141 describes what is the state of the estimation. */
142 enum
143 {
144 /* Estimate was not computed yet. */
145 EST_NOT_COMPUTED,
946e1bc7
ZD
146 /* Estimate is ready. */
147 EST_AVAILABLE
148 } estimate_state;
9bdb685e
ZD
149
150 /* An integer guaranteed to bound the number of iterations of the loop
151 from above. */
152 bool any_upper_bound;
153 double_int nb_iterations_upper_bound;
154
155 /* An integer giving the expected number of iterations of the loop. */
156 bool any_estimate;
157 double_int nb_iterations_estimate;
86df10e3 158
e9eb809d
ZD
159 /* Upper bound on number of iterations of a loop. */
160 struct nb_iter_bound *bounds;
82b85a85 161
6270df4c
ZD
162 /* Head of the cyclic list of the exits of the loop. */
163 struct loop_exit exits;
3d436d2a
ZD
164};
165
166/* Flags for state of loop structure. */
167enum
168{
169 LOOPS_HAVE_PREHEADERS = 1,
170 LOOPS_HAVE_SIMPLE_LATCHES = 2,
82b85a85 171 LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS = 4,
89f8f30f 172 LOOPS_HAVE_RECORDED_EXITS = 8,
c7b852c8
ZD
173 LOOPS_MAY_HAVE_MULTIPLE_LATCHES = 16,
174 LOOP_CLOSED_SSA = 32
3d436d2a
ZD
175};
176
d78f3f78
ZD
177#define LOOPS_NORMAL (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_SIMPLE_LATCHES \
178 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
89f8f30f 179#define AVOID_CFG_MODIFICATIONS (LOOPS_MAY_HAVE_MULTIPLE_LATCHES)
d78f3f78 180
42fd6772
ZD
181typedef struct loop *loop_p;
182DEF_VEC_P (loop_p);
183DEF_VEC_ALLOC_P (loop_p, heap);
184
3d436d2a
ZD
185/* Structure to hold CFG information about natural loops within a function. */
186struct loops
187{
0162f155
RG
188 /* State of loops. */
189 int state;
3d436d2a 190
42fd6772
ZD
191 /* Array of the loops. */
192 VEC (loop_p, heap) *larray;
3d436d2a 193
6270df4c
ZD
194 /* Maps edges to the list of their descriptions as loop exits. Edges
195 whose sources or destinations have loop_father == NULL (which may
196 happen during the cfg manipulations) should not appear in EXITS. */
197 htab_t exits;
198
3d436d2a
ZD
199 /* Pointer to root of loop hierarchy tree. */
200 struct loop *tree_root;
3d436d2a
ZD
201};
202
3d436d2a 203/* Loop recognition. */
70388d94 204extern int flow_loops_find (struct loops *);
89f8f30f 205extern void disambiguate_loops_with_multiple_latches (void);
d329e058 206extern void flow_loops_free (struct loops *);
d73be268 207extern void flow_loops_dump (FILE *,
d329e058
AJ
208 void (*)(const struct loop *, FILE *, int), int);
209extern void flow_loop_dump (const struct loop *, FILE *,
210 void (*)(const struct loop *, FILE *, int), int);
6270df4c 211struct loop *alloc_loop (void);
d329e058 212extern void flow_loop_free (struct loop *);
2b271002 213int flow_loop_nodes_find (basic_block, struct loop *);
d73be268
ZD
214void fix_loop_structure (bitmap changed_bbs);
215void mark_irreducible_loops (void);
6270df4c
ZD
216void release_recorded_exits (void);
217void record_loop_exits (void);
218void rescan_loop_exit (edge, bool, bool);
3d436d2a 219
4d6922ee 220/* Loop data structure manipulation/querying. */
d329e058
AJ
221extern void flow_loop_tree_node_add (struct loop *, struct loop *);
222extern void flow_loop_tree_node_remove (struct loop *);
89f8f30f 223extern void add_loop (struct loop *, struct loop *);
d329e058
AJ
224extern bool flow_loop_nested_p (const struct loop *, const struct loop *);
225extern bool flow_bb_inside_loop_p (const struct loop *, const basic_block);
226extern struct loop * find_common_loop (struct loop *, struct loop *);
a7e5372d 227struct loop *superloop_at_depth (struct loop *, unsigned);
7f9bc51b
ZD
228struct eni_weights_d;
229extern unsigned tree_num_loop_insns (struct loop *, struct eni_weights_d *);
d329e058
AJ
230extern int num_loop_insns (struct loop *);
231extern int average_num_loop_insns (struct loop *);
689ba89d 232extern unsigned get_loop_level (const struct loop *);
70388d94 233extern bool loop_exit_edge_p (const struct loop *, edge);
d73be268 234extern void mark_loop_exit_edges (void);
3d436d2a
ZD
235
236/* Loops & cfg manipulation. */
d329e058 237extern basic_block *get_loop_body (const struct loop *);
89f8f30f
ZD
238extern unsigned get_loop_body_with_size (const struct loop *, basic_block *,
239 unsigned);
50654f6c 240extern basic_block *get_loop_body_in_dom_order (const struct loop *);
40923b20 241extern basic_block *get_loop_body_in_bfs_order (const struct loop *);
ca83d385 242extern VEC (edge, heap) *get_loop_exit_edges (const struct loop *);
ac8f6c69 243edge single_exit (const struct loop *);
50654f6c 244extern unsigned num_loop_branches (const struct loop *);
3d436d2a 245
d329e058
AJ
246extern edge loop_preheader_edge (const struct loop *);
247extern edge loop_latch_edge (const struct loop *);
3d436d2a 248
d329e058
AJ
249extern void add_bb_to_loop (basic_block, struct loop *);
250extern void remove_bb_from_loops (basic_block);
3d436d2a 251
d73be268 252extern void cancel_loop_tree (struct loop *);
42fd6772 253extern void delete_loop (struct loop *);
3d436d2a 254
3d436d2a
ZD
255enum
256{
bc35512f 257 CP_SIMPLE_PREHEADERS = 1
3d436d2a
ZD
258};
259
d73be268
ZD
260extern void create_preheaders (int);
261extern void force_single_succ_latches (void);
3d436d2a 262
d73be268 263extern void verify_loop_structure (void);
3d436d2a
ZD
264
265/* Loop analysis. */
6c878b23 266extern bool just_once_each_iteration_p (const struct loop *, basic_block);
ac84e05e 267gcov_type expected_loop_iterations_unbounded (const struct loop *);
d329e058 268extern unsigned expected_loop_iterations (const struct loop *);
75c70254 269extern rtx doloop_condition_get (rtx);
617b465c 270
4839cb59
ZD
271void estimate_numbers_of_iterations_loop (struct loop *);
272HOST_WIDE_INT estimated_loop_iterations_int (struct loop *, bool);
ac84e05e 273bool estimated_loop_iterations (struct loop *, bool, double_int *);
4839cb59 274
617b465c 275/* Loop manipulation. */
d329e058 276extern bool can_duplicate_loop_p (struct loop *loop);
617b465c
ZD
277
278#define DLTHE_FLAG_UPDATE_FREQ 1 /* Update frequencies in
279 duplicate_loop_to_header_edge. */
7f7b1718
JH
280#define DLTHE_RECORD_COPY_NUMBER 2 /* Record copy number in the aux
281 field of newly create BB. */
178df94f 282#define DLTHE_FLAG_COMPLETTE_PEEL 4 /* Update frequencies expecting
a4d05547 283 a complete peeling. */
617b465c 284
d73be268
ZD
285extern struct loop * duplicate_loop (struct loop *, struct loop *);
286extern bool duplicate_loop_to_header_edge (struct loop *, edge,
ee8c1b05
ZD
287 unsigned, sbitmap, edge,
288 VEC (edge, heap) **, int);
d73be268 289extern struct loop *loopify (edge, edge,
03cb2019
ZD
290 basic_block, edge, edge, bool,
291 unsigned, unsigned);
d73be268 292struct loop * loop_version (struct loop *, void *,
03cb2019 293 basic_block *, unsigned, unsigned, unsigned, bool);
d73be268 294extern bool remove_path (edge);
03cb2019 295void scale_loop_frequencies (struct loop *, int, int);
617b465c 296
50654f6c
ZD
297/* Induction variable analysis. */
298
299/* The description of induction variable. The things are a bit complicated
300 due to need to handle subregs and extends. The value of the object described
301 by it can be obtained as follows (all computations are done in extend_mode):
302
303 Value in i-th iteration is
304 delta + mult * extend_{extend_mode} (subreg_{mode} (base + i * step)).
305
306 If first_special is true, the value in the first iteration is
307 delta + mult * base
c22cacf3 308
f822d252 309 If extend = UNKNOWN, first_special must be false, delta 0, mult 1 and value is
50654f6c
ZD
310 subreg_{mode} (base + i * step)
311
312 The get_iv_value function can be used to obtain these expressions.
313
314 ??? Add a third mode field that would specify the mode in that inner
315 computation is done, which would enable it to be different from the
316 outer one? */
317
318struct rtx_iv
319{
320 /* Its base and step (mode of base and step is supposed to be extend_mode,
321 see the description above). */
322 rtx base, step;
323
f822d252 324 /* The type of extend applied to it (SIGN_EXTEND, ZERO_EXTEND or UNKNOWN). */
50654f6c
ZD
325 enum rtx_code extend;
326
327 /* Operations applied in the extended mode. */
328 rtx delta, mult;
329
330 /* The mode it is extended to. */
331 enum machine_mode extend_mode;
332
333 /* The mode the variable iterates in. */
334 enum machine_mode mode;
335
50654f6c
ZD
336 /* Whether the first iteration needs to be handled specially. */
337 unsigned first_special : 1;
338};
339
f2dca510
ZD
340/* The description of an exit from the loop and of the number of iterations
341 till we take the exit. */
50654f6c
ZD
342
343struct niter_desc
344{
345 /* The edge out of the loop. */
346 edge out_edge;
347
348 /* The other edge leading from the condition. */
349 edge in_edge;
350
351 /* True if we are able to say anything about number of iterations of the
352 loop. */
353 bool simple_p;
354
355 /* True if the loop iterates the constant number of times. */
356 bool const_iter;
357
358 /* Number of iterations if constant. */
359 unsigned HOST_WIDEST_INT niter;
360
361 /* Upper bound on the number of iterations. */
362 unsigned HOST_WIDEST_INT niter_max;
363
364 /* Assumptions under that the rest of the information is valid. */
365 rtx assumptions;
366
367 /* Assumptions under that the loop ends before reaching the latch,
368 even if value of niter_expr says otherwise. */
369 rtx noloop_assumptions;
370
371 /* Condition under that the loop is infinite. */
372 rtx infinite;
373
374 /* Whether the comparison is signed. */
375 bool signed_p;
376
377 /* The mode in that niter_expr should be computed. */
378 enum machine_mode mode;
379
380 /* The number of iterations of the loop. */
381 rtx niter_expr;
382};
383
384extern void iv_analysis_loop_init (struct loop *);
6d4e0ecc 385extern bool iv_analyze (rtx, rtx, struct rtx_iv *);
03fd2215
ZD
386extern bool iv_analyze_result (rtx, rtx, struct rtx_iv *);
387extern bool iv_analyze_expr (rtx, rtx, enum machine_mode, struct rtx_iv *);
50654f6c 388extern rtx get_iv_value (struct rtx_iv *, rtx);
113d659a 389extern bool biv_p (rtx, rtx);
50654f6c 390extern void find_simple_exit (struct loop *, struct niter_desc *);
50654f6c 391extern void iv_analysis_done (void);
03fd2215 392extern struct df *iv_current_loop_df (void);
50654f6c
ZD
393
394extern struct niter_desc *get_simple_loop_desc (struct loop *loop);
395extern void free_simple_loop_desc (struct loop *loop);
396
397static inline struct niter_desc *
398simple_loop_desc (struct loop *loop)
399{
cceb1885 400 return (struct niter_desc *) loop->aux;
50654f6c
ZD
401}
402
42fd6772
ZD
403/* Accessors for the loop structures. */
404
405/* Returns the loop with index NUM from current_loops. */
406
407static inline struct loop *
408get_loop (unsigned num)
409{
410 return VEC_index (loop_p, current_loops->larray, num);
411}
412
413/* Returns the list of loops in current_loops. */
414
415static inline VEC (loop_p, heap) *
416get_loops (void)
417{
418 if (!current_loops)
419 return NULL;
420
421 return current_loops->larray;
422}
423
424/* Returns the number of loops in current_loops (including the removed
425 ones and the fake loop that forms the root of the loop tree). */
426
427static inline unsigned
428number_of_loops (void)
429{
430 if (!current_loops)
431 return 0;
432
433 return VEC_length (loop_p, current_loops->larray);
434}
435
436/* Loop iterators. */
437
438/* Flags for loop iteration. */
439
440enum li_flags
441{
677cc14d
ZD
442 LI_INCLUDE_ROOT = 1, /* Include the fake root of the loop tree. */
443 LI_FROM_INNERMOST = 2, /* Iterate over the loops in the reverse order,
444 starting from innermost ones. */
445 LI_ONLY_INNERMOST = 4 /* Iterate only over innermost loops. */
42fd6772
ZD
446};
447
448/* The iterator for loops. */
449
450typedef struct
451{
677cc14d
ZD
452 /* The list of loops to visit. */
453 VEC(int,heap) *to_visit;
454
455 /* The index of the actual loop. */
456 unsigned idx;
42fd6772
ZD
457} loop_iterator;
458
459static inline void
677cc14d 460fel_next (loop_iterator *li, loop_p *loop)
42fd6772 461{
677cc14d
ZD
462 int anum;
463
464 while (VEC_iterate (int, li->to_visit, li->idx, anum))
42fd6772 465 {
42fd6772 466 li->idx++;
677cc14d
ZD
467 *loop = get_loop (anum);
468 if (*loop)
469 return;
42fd6772
ZD
470 }
471
677cc14d 472 VEC_free (int, heap, li->to_visit);
42fd6772
ZD
473 *loop = NULL;
474}
475
476static inline void
477fel_init (loop_iterator *li, loop_p *loop, unsigned flags)
478{
677cc14d
ZD
479 struct loop *aloop;
480 unsigned i;
481 int mn;
482
483 li->idx = 0;
42fd6772
ZD
484 if (!current_loops)
485 {
677cc14d 486 li->to_visit = NULL;
42fd6772
ZD
487 *loop = NULL;
488 return;
489 }
490
677cc14d
ZD
491 li->to_visit = VEC_alloc (int, heap, number_of_loops ());
492 mn = (flags & LI_INCLUDE_ROOT) ? 0 : 1;
493
494 if (flags & LI_ONLY_INNERMOST)
495 {
496 for (i = 0; VEC_iterate (loop_p, current_loops->larray, i, aloop); i++)
497 if (aloop != NULL
498 && aloop->inner == NULL
499 && aloop->num >= mn)
500 VEC_quick_push (int, li->to_visit, aloop->num);
501 }
502 else if (flags & LI_FROM_INNERMOST)
42fd6772 503 {
677cc14d
ZD
504 /* Push the loops to LI->TO_VISIT in postorder. */
505 for (aloop = current_loops->tree_root;
506 aloop->inner != NULL;
507 aloop = aloop->inner)
508 continue;
509
510 while (1)
511 {
512 if (aloop->num >= mn)
513 VEC_quick_push (int, li->to_visit, aloop->num);
514
515 if (aloop->next)
516 {
517 for (aloop = aloop->next;
518 aloop->inner != NULL;
519 aloop = aloop->inner)
520 continue;
521 }
522 else if (!aloop->outer)
523 break;
524 else
525 aloop = aloop->outer;
526 }
42fd6772
ZD
527 }
528 else
529 {
677cc14d
ZD
530 /* Push the loops to LI->TO_VISIT in preorder. */
531 aloop = current_loops->tree_root;
532 while (1)
533 {
534 if (aloop->num >= mn)
535 VEC_quick_push (int, li->to_visit, aloop->num);
536
537 if (aloop->inner != NULL)
538 aloop = aloop->inner;
539 else
540 {
541 while (aloop != NULL && aloop->next == NULL)
542 aloop = aloop->outer;
543 if (aloop == NULL)
544 break;
545 aloop = aloop->next;
546 }
547 }
42fd6772 548 }
677cc14d
ZD
549
550 fel_next (li, loop);
42fd6772
ZD
551}
552
553#define FOR_EACH_LOOP(LI, LOOP, FLAGS) \
554 for (fel_init (&(LI), &(LOOP), FLAGS); \
555 (LOOP); \
677cc14d
ZD
556 fel_next (&(LI), &(LOOP)))
557
558#define FOR_EACH_LOOP_BREAK(LI) \
559 { \
560 VEC_free (int, heap, (LI)->to_visit); \
561 break; \
562 }
42fd6772 563
8b11a64c
ZD
564/* The properties of the target. */
565
a154b43a
ZD
566extern unsigned target_avail_regs;
567extern unsigned target_res_regs;
568extern unsigned target_reg_cost;
569extern unsigned target_spill_cost;
8b11a64c 570
5e962776
ZD
571/* Register pressure estimation for induction variable optimizations & loop
572 invariant motion. */
a154b43a 573extern unsigned estimate_reg_pressure_cost (unsigned, unsigned);
5e962776
ZD
574extern void init_set_costs (void);
575
617b465c 576/* Loop optimizer initialization. */
598ec7bd
ZD
577extern void loop_optimizer_init (unsigned);
578extern void loop_optimizer_finalize (void);
617b465c
ZD
579
580/* Optimization passes. */
d73be268 581extern void unswitch_loops (void);
617b465c 582
b17d5d7c
ZD
583enum
584{
585 UAP_PEEL = 1, /* Enables loop peeling. */
598ec7bd
ZD
586 UAP_UNROLL = 2, /* Enables unrolling of loops if it seems profitable. */
587 UAP_UNROLL_ALL = 4 /* Enables unrolling of all loops. */
b17d5d7c
ZD
588};
589
d73be268
ZD
590extern void unroll_and_peel_loops (int);
591extern void doloop_optimize_loops (void);
592extern void move_loop_invariants (void);
59587b18
JQ
593
594#endif /* GCC_CFGLOOP_H */