]> gcc.gnu.org Git - gcc.git/blame - gcc/cfgloop.c
re PR target/52692 ([avr]: Add support for avr-specific built-ins + LTO)
[gcc.git] / gcc / cfgloop.c
CommitLineData
402209ff 1/* Natural loop discovery code for GNU compiler.
d652f226 2 Copyright (C) 2000, 2001, 2003, 2004, 2005, 2006, 2007, 2008, 2010
6fb5fa3c 3 Free Software Foundation, Inc.
402209ff
JH
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
9dcd6f09 9Software Foundation; either version 3, or (at your option) any later
402209ff
JH
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
9dcd6f09
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
402209ff
JH
20
21#include "config.h"
22#include "system.h"
4977bab6
ZW
23#include "coretypes.h"
24#include "tm.h"
402209ff
JH
25#include "rtl.h"
26#include "hard-reg-set.h"
7932a3db 27#include "obstack.h"
a310245f 28#include "function.h"
402209ff 29#include "basic-block.h"
3d436d2a 30#include "cfgloop.h"
718f9c0f 31#include "diagnostic-core.h"
3d436d2a 32#include "flags.h"
6de9cd9a
DN
33#include "tree.h"
34#include "tree-flow.h"
89f8f30f
ZD
35#include "pointer-set.h"
36#include "output.h"
9e2f83a5 37#include "ggc.h"
f470c378 38
d73be268 39static void flow_loops_cfg_dump (FILE *);
402209ff
JH
40\f
41/* Dump loop related CFG information. */
42
43static void
d73be268 44flow_loops_cfg_dump (FILE *file)
402209ff 45{
e0082a72 46 basic_block bb;
402209ff 47
d73be268 48 if (!file)
402209ff
JH
49 return;
50
e0082a72 51 FOR_EACH_BB (bb)
402209ff
JH
52 {
53 edge succ;
628f6a4e 54 edge_iterator ei;
402209ff 55
e0082a72 56 fprintf (file, ";; %d succs { ", bb->index);
628f6a4e 57 FOR_EACH_EDGE (succ, ei, bb->succs)
0b17ab2f 58 fprintf (file, "%d ", succ->dest->index);
2ecfd709 59 fprintf (file, "}\n");
402209ff 60 }
402209ff
JH
61}
62
da7d8304 63/* Return nonzero if the nodes of LOOP are a subset of OUTER. */
402209ff 64
2ecfd709 65bool
d329e058 66flow_loop_nested_p (const struct loop *outer, const struct loop *loop)
402209ff 67{
9ba025a2
ZD
68 unsigned odepth = loop_depth (outer);
69
70 return (loop_depth (loop) > odepth
71 && VEC_index (loop_p, loop->superloops, odepth) == outer);
402209ff
JH
72}
73
1ad03593
SP
74/* Returns the loop such that LOOP is nested DEPTH (indexed from zero)
75 loops within LOOP. */
a7e5372d
ZD
76
77struct loop *
78superloop_at_depth (struct loop *loop, unsigned depth)
79{
9ba025a2
ZD
80 unsigned ldepth = loop_depth (loop);
81
82 gcc_assert (depth <= ldepth);
a7e5372d 83
9ba025a2 84 if (depth == ldepth)
a7e5372d
ZD
85 return loop;
86
9ba025a2 87 return VEC_index (loop_p, loop->superloops, depth);
a7e5372d
ZD
88}
89
89f8f30f
ZD
90/* Returns the list of the latch edges of LOOP. */
91
92static VEC (edge, heap) *
93get_loop_latch_edges (const struct loop *loop)
94{
95 edge_iterator ei;
96 edge e;
97 VEC (edge, heap) *ret = NULL;
98
99 FOR_EACH_EDGE (e, ei, loop->header->preds)
100 {
101 if (dominated_by_p (CDI_DOMINATORS, e->src, loop->header))
102 VEC_safe_push (edge, heap, ret, e);
103 }
104
105 return ret;
106}
107
402209ff
JH
108/* Dump the loop information specified by LOOP to the stream FILE
109 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
110
111void
d329e058
AJ
112flow_loop_dump (const struct loop *loop, FILE *file,
113 void (*loop_dump_aux) (const struct loop *, FILE *, int),
114 int verbose)
402209ff 115{
2ecfd709 116 basic_block *bbs;
3d436d2a 117 unsigned i;
89f8f30f
ZD
118 VEC (edge, heap) *latches;
119 edge e;
2ecfd709 120
402209ff
JH
121 if (! loop || ! loop->header)
122 return;
123
7490e6c4 124 fprintf (file, ";;\n;; Loop %d\n", loop->num);
402209ff 125
89f8f30f
ZD
126 fprintf (file, ";; header %d, ", loop->header->index);
127 if (loop->latch)
128 fprintf (file, "latch %d\n", loop->latch->index);
129 else
130 {
131 fprintf (file, "multiple latches:");
132 latches = get_loop_latch_edges (loop);
ac47786e 133 FOR_EACH_VEC_ELT (edge, latches, i, e)
89f8f30f
ZD
134 fprintf (file, " %d", e->src->index);
135 VEC_free (edge, heap, latches);
136 fprintf (file, "\n");
137 }
138
99f8a411 139 fprintf (file, ";; depth %d, outer %ld\n",
9ba025a2
ZD
140 loop_depth (loop), (long) (loop_outer (loop)
141 ? loop_outer (loop)->num : -1));
402209ff 142
2ecfd709
ZD
143 fprintf (file, ";; nodes:");
144 bbs = get_loop_body (loop);
145 for (i = 0; i < loop->num_nodes; i++)
146 fprintf (file, " %d", bbs[i]->index);
147 free (bbs);
148 fprintf (file, "\n");
5f0d2358 149
402209ff
JH
150 if (loop_dump_aux)
151 loop_dump_aux (loop, file, verbose);
152}
153
d73be268 154/* Dump the loop information about loops to the stream FILE,
402209ff
JH
155 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
156
157void
d73be268 158flow_loops_dump (FILE *file, void (*loop_dump_aux) (const struct loop *, FILE *, int), int verbose)
402209ff 159{
42fd6772
ZD
160 loop_iterator li;
161 struct loop *loop;
402209ff 162
d73be268 163 if (!current_loops || ! file)
402209ff
JH
164 return;
165
42fd6772 166 fprintf (file, ";; %d loops found\n", number_of_loops ());
2ecfd709 167
42fd6772 168 FOR_EACH_LOOP (li, loop, LI_INCLUDE_ROOT)
402209ff 169 {
2ecfd709 170 flow_loop_dump (loop, file, loop_dump_aux, verbose);
402209ff
JH
171 }
172
173 if (verbose)
d73be268 174 flow_loops_cfg_dump (file);
402209ff
JH
175}
176
2ecfd709 177/* Free data allocated for LOOP. */
9e2f83a5 178
35b07080 179void
d329e058 180flow_loop_free (struct loop *loop)
2ecfd709 181{
6270df4c
ZD
182 struct loop_exit *exit, *next;
183
9e2f83a5 184 VEC_free (loop_p, gc, loop->superloops);
6270df4c
ZD
185
186 /* Break the list of the loop exit records. They will be freed when the
187 corresponding edge is rescanned or removed, and this avoids
188 accessing the (already released) head of the list stored in the
189 loop structure. */
9e2f83a5 190 for (exit = loop->exits->next; exit != loop->exits; exit = next)
6270df4c
ZD
191 {
192 next = exit->next;
193 exit->next = exit;
194 exit->prev = exit;
195 }
9e2f83a5
ZD
196
197 ggc_free (loop->exits);
198 ggc_free (loop);
2ecfd709
ZD
199}
200
402209ff
JH
201/* Free all the memory allocated for LOOPS. */
202
203void
d329e058 204flow_loops_free (struct loops *loops)
402209ff 205{
42fd6772 206 if (loops->larray)
402209ff 207 {
3d436d2a 208 unsigned i;
42fd6772 209 loop_p loop;
402209ff
JH
210
211 /* Free the loop descriptors. */
ac47786e 212 FOR_EACH_VEC_ELT (loop_p, loops->larray, i, loop)
402209ff 213 {
2ecfd709
ZD
214 if (!loop)
215 continue;
216
217 flow_loop_free (loop);
402209ff 218 }
5f0d2358 219
9e2f83a5 220 VEC_free (loop_p, gc, loops->larray);
402209ff
JH
221 }
222}
223
2ecfd709
ZD
224/* Find the nodes contained within the LOOP with header HEADER.
225 Return the number of nodes within the loop. */
402209ff 226
2b271002 227int
d329e058 228flow_loop_nodes_find (basic_block header, struct loop *loop)
402209ff 229{
89f8f30f 230 VEC (basic_block, heap) *stack = NULL;
2ecfd709 231 int num_nodes = 1;
89f8f30f
ZD
232 edge latch;
233 edge_iterator latch_ei;
9ba025a2 234 unsigned depth = loop_depth (loop);
402209ff 235
2ecfd709 236 header->loop_father = loop;
9ba025a2 237 header->loop_depth = depth;
402209ff 238
89f8f30f 239 FOR_EACH_EDGE (latch, latch_ei, loop->header->preds)
402209ff 240 {
89f8f30f
ZD
241 if (latch->src->loop_father == loop
242 || !dominated_by_p (CDI_DOMINATORS, latch->src, loop->header))
243 continue;
244
402209ff 245 num_nodes++;
89f8f30f
ZD
246 VEC_safe_push (basic_block, heap, stack, latch->src);
247 latch->src->loop_father = loop;
9ba025a2 248 latch->src->loop_depth = depth;
d329e058 249
89f8f30f 250 while (!VEC_empty (basic_block, stack))
402209ff 251 {
2ecfd709
ZD
252 basic_block node;
253 edge e;
628f6a4e 254 edge_iterator ei;
402209ff 255
89f8f30f 256 node = VEC_pop (basic_block, stack);
d329e058 257
628f6a4e 258 FOR_EACH_EDGE (e, ei, node->preds)
402209ff 259 {
2ecfd709
ZD
260 basic_block ancestor = e->src;
261
89f8f30f 262 if (ancestor->loop_father != loop)
2ecfd709
ZD
263 {
264 ancestor->loop_father = loop;
9ba025a2 265 ancestor->loop_depth = depth;
2ecfd709 266 num_nodes++;
89f8f30f 267 VEC_safe_push (basic_block, heap, stack, ancestor);
2ecfd709 268 }
402209ff
JH
269 }
270 }
271 }
89f8f30f
ZD
272 VEC_free (basic_block, heap, stack);
273
402209ff
JH
274 return num_nodes;
275}
276
9ba025a2
ZD
277/* Records the vector of superloops of the loop LOOP, whose immediate
278 superloop is FATHER. */
279
35b07080 280static void
9ba025a2 281establish_preds (struct loop *loop, struct loop *father)
35b07080 282{
9ba025a2
ZD
283 loop_p ploop;
284 unsigned depth = loop_depth (father) + 1;
285 unsigned i;
a310245f 286
9ba025a2 287 VEC_truncate (loop_p, loop->superloops, 0);
9e2f83a5 288 VEC_reserve (loop_p, gc, loop->superloops, depth);
ac47786e 289 FOR_EACH_VEC_ELT (loop_p, father->superloops, i, ploop)
9ba025a2
ZD
290 VEC_quick_push (loop_p, loop->superloops, ploop);
291 VEC_quick_push (loop_p, loop->superloops, father);
35b07080
ZD
292
293 for (ploop = loop->inner; ploop; ploop = ploop->next)
9ba025a2 294 establish_preds (ploop, loop);
35b07080
ZD
295}
296
2ecfd709 297/* Add LOOP to the loop hierarchy tree where FATHER is father of the
35b07080
ZD
298 added loop. If LOOP has some children, take care of that their
299 pred field will be initialized correctly. */
402209ff 300
2ecfd709 301void
d329e058 302flow_loop_tree_node_add (struct loop *father, struct loop *loop)
402209ff 303{
2ecfd709
ZD
304 loop->next = father->inner;
305 father->inner = loop;
2ecfd709 306
9ba025a2 307 establish_preds (loop, father);
402209ff
JH
308}
309
2ecfd709 310/* Remove LOOP from the loop hierarchy tree. */
402209ff 311
2ecfd709 312void
d329e058 313flow_loop_tree_node_remove (struct loop *loop)
402209ff 314{
2ecfd709 315 struct loop *prev, *father;
402209ff 316
9ba025a2 317 father = loop_outer (loop);
402209ff 318
2ecfd709
ZD
319 /* Remove loop from the list of sons. */
320 if (father->inner == loop)
321 father->inner = loop->next;
322 else
323 {
9ba025a2
ZD
324 for (prev = father->inner; prev->next != loop; prev = prev->next)
325 continue;
2ecfd709
ZD
326 prev->next = loop->next;
327 }
402209ff 328
9ba025a2 329 VEC_truncate (loop_p, loop->superloops, 0);
402209ff
JH
330}
331
6270df4c
ZD
332/* Allocates and returns new loop structure. */
333
334struct loop *
335alloc_loop (void)
336{
a9429e29 337 struct loop *loop = ggc_alloc_cleared_loop ();
9e2f83a5 338
a9429e29 339 loop->exits = ggc_alloc_cleared_loop_exit ();
9e2f83a5 340 loop->exits->next = loop->exits->prev = loop->exits;
204b560f 341 loop->can_be_parallel = false;
6270df4c 342
6270df4c
ZD
343 return loop;
344}
345
4ed88ee3
ZD
346/* Initializes loops structure LOOPS, reserving place for NUM_LOOPS loops
347 (including the root of the loop tree). */
348
349static void
350init_loops_structure (struct loops *loops, unsigned num_loops)
351{
352 struct loop *root;
353
354 memset (loops, 0, sizeof *loops);
355 loops->larray = VEC_alloc (loop_p, gc, num_loops);
356
357 /* Dummy loop containing whole function. */
358 root = alloc_loop ();
359 root->num_nodes = n_basic_blocks;
360 root->latch = EXIT_BLOCK_PTR;
361 root->header = ENTRY_BLOCK_PTR;
362 ENTRY_BLOCK_PTR->loop_father = root;
363 EXIT_BLOCK_PTR->loop_father = root;
364
365 VEC_quick_push (loop_p, loops->larray, root);
366 loops->tree_root = root;
367}
368
5f0d2358 369/* Find all the natural loops in the function and save in LOOPS structure and
70388d94
ZD
370 recalculate loop_depth information in basic block structures.
371 Return the number of natural loops found. */
402209ff
JH
372
373int
70388d94 374flow_loops_find (struct loops *loops)
402209ff 375{
0b17ab2f 376 int b;
402209ff
JH
377 int num_loops;
378 edge e;
379 sbitmap headers;
402209ff
JH
380 int *dfs_order;
381 int *rc_order;
355be0dc
JH
382 basic_block header;
383 basic_block bb;
402209ff 384
4ed88ee3
ZD
385 /* Ensure that the dominators are computed. */
386 calculate_dominance_info (CDI_DOMINATORS);
402209ff
JH
387
388 /* Taking care of this degenerate case makes the rest of
389 this code simpler. */
24bd1a0b 390 if (n_basic_blocks == NUM_FIXED_BLOCKS)
4ed88ee3
ZD
391 {
392 init_loops_structure (loops, 1);
393 return 1;
394 }
402209ff
JH
395
396 dfs_order = NULL;
397 rc_order = NULL;
398
2ecfd709 399 /* Count the number of loop headers. This should be the
402209ff 400 same as the number of natural loops. */
2ecfd709
ZD
401 headers = sbitmap_alloc (last_basic_block);
402 sbitmap_zero (headers);
403
402209ff 404 num_loops = 0;
e0082a72 405 FOR_EACH_BB (header)
402209ff 406 {
628f6a4e 407 edge_iterator ei;
d329e058 408
402209ff
JH
409 header->loop_depth = 0;
410
16f2b86a
ZD
411 /* If we have an abnormal predecessor, do not consider the
412 loop (not worth the problems). */
31ff2426 413 if (bb_has_abnormal_pred (header))
16f2b86a
ZD
414 continue;
415
628f6a4e 416 FOR_EACH_EDGE (e, ei, header->preds)
402209ff
JH
417 {
418 basic_block latch = e->src;
419
341c100f 420 gcc_assert (!(e->flags & EDGE_ABNORMAL));
2ecfd709 421
402209ff
JH
422 /* Look for back edges where a predecessor is dominated
423 by this block. A natural loop has a single entry
424 node (header) that dominates all the nodes in the
425 loop. It also has single back edge to the header
2ecfd709 426 from a latch node. */
d47cc544
SB
427 if (latch != ENTRY_BLOCK_PTR
428 && dominated_by_p (CDI_DOMINATORS, latch, header))
2ecfd709
ZD
429 {
430 /* Shared headers should be eliminated by now. */
2ecfd709
ZD
431 SET_BIT (headers, header->index);
432 num_loops++;
433 }
402209ff
JH
434 }
435 }
436
2ecfd709 437 /* Allocate loop structures. */
4ed88ee3 438 init_loops_structure (loops, num_loops + 1);
2ecfd709
ZD
439
440 /* Find and record information about all the natural loops
441 in the CFG. */
2ecfd709
ZD
442 FOR_EACH_BB (bb)
443 bb->loop_father = loops->tree_root;
444
402209ff
JH
445 if (num_loops)
446 {
447 /* Compute depth first search order of the CFG so that outer
448 natural loops will be found before inner natural loops. */
5ed6ace5
MD
449 dfs_order = XNEWVEC (int, n_basic_blocks);
450 rc_order = XNEWVEC (int, n_basic_blocks);
f91a0beb 451 pre_and_rev_post_order_compute (dfs_order, rc_order, false);
402209ff 452
2ecfd709 453 num_loops = 1;
402209ff 454
24bd1a0b 455 for (b = 0; b < n_basic_blocks - NUM_FIXED_BLOCKS; b++)
402209ff 456 {
2ecfd709 457 struct loop *loop;
628f6a4e 458 edge_iterator ei;
402209ff
JH
459
460 /* Search the nodes of the CFG in reverse completion order
461 so that we can find outer loops first. */
2ecfd709
ZD
462 if (!TEST_BIT (headers, rc_order[b]))
463 continue;
464
465 header = BASIC_BLOCK (rc_order[b]);
d329e058 466
6270df4c 467 loop = alloc_loop ();
42fd6772 468 VEC_quick_push (loop_p, loops->larray, loop);
402209ff 469
2ecfd709
ZD
470 loop->header = header;
471 loop->num = num_loops;
472 num_loops++;
473
89f8f30f
ZD
474 flow_loop_tree_node_add (header->loop_father, loop);
475 loop->num_nodes = flow_loop_nodes_find (loop->header, loop);
476
477 /* Look for the latch for this header block, if it has just a
478 single one. */
628f6a4e 479 FOR_EACH_EDGE (e, ei, header->preds)
402209ff 480 {
2ecfd709
ZD
481 basic_block latch = e->src;
482
89f8f30f 483 if (flow_bb_inside_loop_p (loop, latch))
402209ff 484 {
89f8f30f
ZD
485 if (loop->latch != NULL)
486 {
487 /* More than one latch edge. */
488 loop->latch = NULL;
489 break;
490 }
402209ff 491 loop->latch = latch;
402209ff
JH
492 }
493 }
402209ff
JH
494 }
495
598ec7bd
ZD
496 free (dfs_order);
497 free (rc_order);
2ecfd709 498 }
3d436d2a 499
36579663
AP
500 sbitmap_free (headers);
501
6270df4c 502 loops->exits = NULL;
42fd6772 503 return VEC_length (loop_p, loops->larray);
402209ff
JH
504}
505
89f8f30f
ZD
506/* Ratio of frequencies of edges so that one of more latch edges is
507 considered to belong to inner loop with same header. */
508#define HEAVY_EDGE_RATIO 8
509
510/* Minimum number of samples for that we apply
511 find_subloop_latch_edge_by_profile heuristics. */
512#define HEAVY_EDGE_MIN_SAMPLES 10
513
514/* If the profile info is available, finds an edge in LATCHES that much more
515 frequent than the remaining edges. Returns such an edge, or NULL if we do
516 not find one.
517
518 We do not use guessed profile here, only the measured one. The guessed
519 profile is usually too flat and unreliable for this (and it is mostly based
520 on the loop structure of the program, so it does not make much sense to
521 derive the loop structure from it). */
b8698a0f 522
89f8f30f
ZD
523static edge
524find_subloop_latch_edge_by_profile (VEC (edge, heap) *latches)
525{
526 unsigned i;
527 edge e, me = NULL;
528 gcov_type mcount = 0, tcount = 0;
529
ac47786e 530 FOR_EACH_VEC_ELT (edge, latches, i, e)
89f8f30f
ZD
531 {
532 if (e->count > mcount)
533 {
534 me = e;
535 mcount = e->count;
536 }
537 tcount += e->count;
538 }
539
540 if (tcount < HEAVY_EDGE_MIN_SAMPLES
541 || (tcount - mcount) * HEAVY_EDGE_RATIO > tcount)
542 return NULL;
543
544 if (dump_file)
545 fprintf (dump_file,
546 "Found latch edge %d -> %d using profile information.\n",
547 me->src->index, me->dest->index);
548 return me;
549}
550
551/* Among LATCHES, guesses a latch edge of LOOP corresponding to subloop, based
552 on the structure of induction variables. Returns this edge, or NULL if we
553 do not find any.
554
555 We are quite conservative, and look just for an obvious simple innermost
556 loop (which is the case where we would lose the most performance by not
557 disambiguating the loop). More precisely, we look for the following
558 situation: The source of the chosen latch edge dominates sources of all
559 the other latch edges. Additionally, the header does not contain a phi node
560 such that the argument from the chosen edge is equal to the argument from
561 another edge. */
562
563static edge
726a989a 564find_subloop_latch_edge_by_ivs (struct loop *loop ATTRIBUTE_UNUSED, VEC (edge, heap) *latches)
89f8f30f
ZD
565{
566 edge e, latch = VEC_index (edge, latches, 0);
567 unsigned i;
726a989a
RB
568 gimple phi;
569 gimple_stmt_iterator psi;
570 tree lop;
89f8f30f
ZD
571 basic_block bb;
572
573 /* Find the candidate for the latch edge. */
574 for (i = 1; VEC_iterate (edge, latches, i, e); i++)
575 if (dominated_by_p (CDI_DOMINATORS, latch->src, e->src))
576 latch = e;
577
578 /* Verify that it dominates all the latch edges. */
ac47786e 579 FOR_EACH_VEC_ELT (edge, latches, i, e)
89f8f30f
ZD
580 if (!dominated_by_p (CDI_DOMINATORS, e->src, latch->src))
581 return NULL;
582
583 /* Check for a phi node that would deny that this is a latch edge of
584 a subloop. */
726a989a 585 for (psi = gsi_start_phis (loop->header); !gsi_end_p (psi); gsi_next (&psi))
89f8f30f 586 {
726a989a 587 phi = gsi_stmt (psi);
89f8f30f
ZD
588 lop = PHI_ARG_DEF_FROM_EDGE (phi, latch);
589
590 /* Ignore the values that are not changed inside the subloop. */
591 if (TREE_CODE (lop) != SSA_NAME
592 || SSA_NAME_DEF_STMT (lop) == phi)
593 continue;
726a989a 594 bb = gimple_bb (SSA_NAME_DEF_STMT (lop));
89f8f30f
ZD
595 if (!bb || !flow_bb_inside_loop_p (loop, bb))
596 continue;
597
ac47786e 598 FOR_EACH_VEC_ELT (edge, latches, i, e)
89f8f30f
ZD
599 if (e != latch
600 && PHI_ARG_DEF_FROM_EDGE (phi, e) == lop)
601 return NULL;
602 }
603
604 if (dump_file)
605 fprintf (dump_file,
606 "Found latch edge %d -> %d using iv structure.\n",
607 latch->src->index, latch->dest->index);
608 return latch;
609}
610
611/* If we can determine that one of the several latch edges of LOOP behaves
612 as a latch edge of a separate subloop, returns this edge. Otherwise
613 returns NULL. */
614
615static edge
616find_subloop_latch_edge (struct loop *loop)
617{
618 VEC (edge, heap) *latches = get_loop_latch_edges (loop);
619 edge latch = NULL;
620
621 if (VEC_length (edge, latches) > 1)
622 {
623 latch = find_subloop_latch_edge_by_profile (latches);
624
625 if (!latch
626 /* We consider ivs to guess the latch edge only in SSA. Perhaps we
627 should use cfghook for this, but it is hard to imagine it would
628 be useful elsewhere. */
629 && current_ir_type () == IR_GIMPLE)
630 latch = find_subloop_latch_edge_by_ivs (loop, latches);
631 }
632
633 VEC_free (edge, heap, latches);
634 return latch;
635}
636
637/* Callback for make_forwarder_block. Returns true if the edge E is marked
638 in the set MFB_REIS_SET. */
639
640static struct pointer_set_t *mfb_reis_set;
641static bool
642mfb_redirect_edges_in_set (edge e)
643{
644 return pointer_set_contains (mfb_reis_set, e);
645}
646
647/* Creates a subloop of LOOP with latch edge LATCH. */
648
649static void
650form_subloop (struct loop *loop, edge latch)
651{
652 edge_iterator ei;
653 edge e, new_entry;
654 struct loop *new_loop;
b8698a0f 655
89f8f30f
ZD
656 mfb_reis_set = pointer_set_create ();
657 FOR_EACH_EDGE (e, ei, loop->header->preds)
658 {
659 if (e != latch)
660 pointer_set_insert (mfb_reis_set, e);
661 }
662 new_entry = make_forwarder_block (loop->header, mfb_redirect_edges_in_set,
663 NULL);
664 pointer_set_destroy (mfb_reis_set);
665
666 loop->header = new_entry->src;
667
668 /* Find the blocks and subloops that belong to the new loop, and add it to
669 the appropriate place in the loop tree. */
670 new_loop = alloc_loop ();
671 new_loop->header = new_entry->dest;
672 new_loop->latch = latch->src;
673 add_loop (new_loop, loop);
674}
675
676/* Make all the latch edges of LOOP to go to a single forwarder block --
677 a new latch of LOOP. */
678
679static void
680merge_latch_edges (struct loop *loop)
681{
682 VEC (edge, heap) *latches = get_loop_latch_edges (loop);
683 edge latch, e;
684 unsigned i;
685
686 gcc_assert (VEC_length (edge, latches) > 0);
687
688 if (VEC_length (edge, latches) == 1)
689 loop->latch = VEC_index (edge, latches, 0)->src;
690 else
691 {
692 if (dump_file)
693 fprintf (dump_file, "Merged latch edges of loop %d\n", loop->num);
694
695 mfb_reis_set = pointer_set_create ();
ac47786e 696 FOR_EACH_VEC_ELT (edge, latches, i, e)
89f8f30f
ZD
697 pointer_set_insert (mfb_reis_set, e);
698 latch = make_forwarder_block (loop->header, mfb_redirect_edges_in_set,
699 NULL);
700 pointer_set_destroy (mfb_reis_set);
701
702 loop->header = latch->dest;
703 loop->latch = latch->src;
704 }
705
706 VEC_free (edge, heap, latches);
707}
708
709/* LOOP may have several latch edges. Transform it into (possibly several)
710 loops with single latch edge. */
711
712static void
713disambiguate_multiple_latches (struct loop *loop)
714{
715 edge e;
716
ea2c620c 717 /* We eliminate the multiple latches by splitting the header to the forwarder
89f8f30f
ZD
718 block F and the rest R, and redirecting the edges. There are two cases:
719
720 1) If there is a latch edge E that corresponds to a subloop (we guess
721 that based on profile -- if it is taken much more often than the
722 remaining edges; and on trees, using the information about induction
723 variables of the loops), we redirect E to R, all the remaining edges to
724 F, then rescan the loops and try again for the outer loop.
725 2) If there is no such edge, we redirect all latch edges to F, and the
726 entry edges to R, thus making F the single latch of the loop. */
727
728 if (dump_file)
729 fprintf (dump_file, "Disambiguating loop %d with multiple latches\n",
730 loop->num);
731
732 /* During latch merging, we may need to redirect the entry edges to a new
733 block. This would cause problems if the entry edge was the one from the
734 entry block. To avoid having to handle this case specially, split
735 such entry edge. */
736 e = find_edge (ENTRY_BLOCK_PTR, loop->header);
737 if (e)
738 split_edge (e);
739
740 while (1)
741 {
742 e = find_subloop_latch_edge (loop);
743 if (!e)
744 break;
745
746 form_subloop (loop, e);
747 }
748
749 merge_latch_edges (loop);
750}
751
752/* Split loops with multiple latch edges. */
753
754void
755disambiguate_loops_with_multiple_latches (void)
756{
757 loop_iterator li;
758 struct loop *loop;
759
760 FOR_EACH_LOOP (li, loop, 0)
761 {
762 if (!loop->latch)
763 disambiguate_multiple_latches (loop);
764 }
765}
766
da7d8304 767/* Return nonzero if basic block BB belongs to LOOP. */
2ecfd709 768bool
ed7a4b4b 769flow_bb_inside_loop_p (const struct loop *loop, const_basic_block bb)
2ecfd709
ZD
770{
771 struct loop *source_loop;
772
773 if (bb == ENTRY_BLOCK_PTR || bb == EXIT_BLOCK_PTR)
774 return 0;
775
776 source_loop = bb->loop_father;
777 return loop == source_loop || flow_loop_nested_p (loop, source_loop);
778}
779
89f8f30f 780/* Enumeration predicate for get_loop_body_with_size. */
2ecfd709 781static bool
ed7a4b4b 782glb_enum_p (const_basic_block bb, const void *glb_loop)
2ecfd709 783{
ed7a4b4b 784 const struct loop *const loop = (const struct loop *) glb_loop;
89f8f30f
ZD
785 return (bb != loop->header
786 && dominated_by_p (CDI_DOMINATORS, bb, loop->header));
787}
788
789/* Gets basic blocks of a LOOP. Header is the 0-th block, rest is in dfs
790 order against direction of edges from latch. Specially, if
791 header != latch, latch is the 1-st block. LOOP cannot be the fake
792 loop tree root, and its size must be at most MAX_SIZE. The blocks
793 in the LOOP body are stored to BODY, and the size of the LOOP is
794 returned. */
795
796unsigned
797get_loop_body_with_size (const struct loop *loop, basic_block *body,
798 unsigned max_size)
799{
800 return dfs_enumerate_from (loop->header, 1, glb_enum_p,
ed7a4b4b 801 body, max_size, loop);
2ecfd709
ZD
802}
803
8d28e87d
ZD
804/* Gets basic blocks of a LOOP. Header is the 0-th block, rest is in dfs
805 order against direction of edges from latch. Specially, if
806 header != latch, latch is the 1-st block. */
89f8f30f 807
2ecfd709 808basic_block *
d329e058 809get_loop_body (const struct loop *loop)
2ecfd709 810{
89f8f30f 811 basic_block *body, bb;
3d436d2a 812 unsigned tv = 0;
2ecfd709 813
341c100f 814 gcc_assert (loop->num_nodes);
2ecfd709 815
89f8f30f 816 body = XCNEWVEC (basic_block, loop->num_nodes);
2ecfd709
ZD
817
818 if (loop->latch == EXIT_BLOCK_PTR)
819 {
89f8f30f
ZD
820 /* There may be blocks unreachable from EXIT_BLOCK, hence we need to
821 special-case the fake loop that contains the whole function. */
24bd1a0b 822 gcc_assert (loop->num_nodes == (unsigned) n_basic_blocks);
89f8f30f
ZD
823 body[tv++] = loop->header;
824 body[tv++] = EXIT_BLOCK_PTR;
2ecfd709 825 FOR_EACH_BB (bb)
89f8f30f 826 body[tv++] = bb;
2ecfd709 827 }
89f8f30f
ZD
828 else
829 tv = get_loop_body_with_size (loop, body, loop->num_nodes);
2ecfd709 830
341c100f 831 gcc_assert (tv == loop->num_nodes);
89f8f30f 832 return body;
2ecfd709
ZD
833}
834
50654f6c
ZD
835/* Fills dominance descendants inside LOOP of the basic block BB into
836 array TOVISIT from index *TV. */
837
838static void
839fill_sons_in_loop (const struct loop *loop, basic_block bb,
840 basic_block *tovisit, int *tv)
841{
842 basic_block son, postpone = NULL;
843
844 tovisit[(*tv)++] = bb;
845 for (son = first_dom_son (CDI_DOMINATORS, bb);
846 son;
847 son = next_dom_son (CDI_DOMINATORS, son))
848 {
849 if (!flow_bb_inside_loop_p (loop, son))
850 continue;
851
852 if (dominated_by_p (CDI_DOMINATORS, loop->latch, son))
853 {
854 postpone = son;
855 continue;
856 }
857 fill_sons_in_loop (loop, son, tovisit, tv);
858 }
859
860 if (postpone)
861 fill_sons_in_loop (loop, postpone, tovisit, tv);
862}
863
864/* Gets body of a LOOP (that must be different from the outermost loop)
865 sorted by dominance relation. Additionally, if a basic block s dominates
866 the latch, then only blocks dominated by s are be after it. */
867
868basic_block *
869get_loop_body_in_dom_order (const struct loop *loop)
870{
871 basic_block *tovisit;
872 int tv;
873
341c100f 874 gcc_assert (loop->num_nodes);
50654f6c 875
5ed6ace5 876 tovisit = XCNEWVEC (basic_block, loop->num_nodes);
50654f6c 877
341c100f 878 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
50654f6c
ZD
879
880 tv = 0;
881 fill_sons_in_loop (loop, loop->header, tovisit, &tv);
882
341c100f 883 gcc_assert (tv == (int) loop->num_nodes);
50654f6c
ZD
884
885 return tovisit;
886}
887
e855c69d
AB
888/* Gets body of a LOOP sorted via provided BB_COMPARATOR. */
889
890basic_block *
b8698a0f 891get_loop_body_in_custom_order (const struct loop *loop,
e855c69d
AB
892 int (*bb_comparator) (const void *, const void *))
893{
894 basic_block *bbs = get_loop_body (loop);
895
896 qsort (bbs, loop->num_nodes, sizeof (basic_block), bb_comparator);
897
898 return bbs;
899}
900
40923b20
DP
901/* Get body of a LOOP in breadth first sort order. */
902
903basic_block *
904get_loop_body_in_bfs_order (const struct loop *loop)
905{
906 basic_block *blocks;
907 basic_block bb;
908 bitmap visited;
909 unsigned int i = 0;
910 unsigned int vc = 1;
911
341c100f
NS
912 gcc_assert (loop->num_nodes);
913 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
40923b20 914
5ed6ace5 915 blocks = XCNEWVEC (basic_block, loop->num_nodes);
8bdbfff5 916 visited = BITMAP_ALLOC (NULL);
40923b20
DP
917
918 bb = loop->header;
919 while (i < loop->num_nodes)
920 {
921 edge e;
628f6a4e 922 edge_iterator ei;
c22cacf3 923
fcaa4ca4
NF
924 if (bitmap_set_bit (visited, bb->index))
925 /* This basic block is now visited */
926 blocks[i++] = bb;
c22cacf3 927
628f6a4e 928 FOR_EACH_EDGE (e, ei, bb->succs)
c22cacf3
MS
929 {
930 if (flow_bb_inside_loop_p (loop, e->dest))
931 {
fcaa4ca4
NF
932 if (bitmap_set_bit (visited, e->dest->index))
933 blocks[i++] = e->dest;
c22cacf3
MS
934 }
935 }
936
341c100f 937 gcc_assert (i >= vc);
c22cacf3 938
40923b20
DP
939 bb = blocks[vc++];
940 }
c22cacf3 941
8bdbfff5 942 BITMAP_FREE (visited);
40923b20
DP
943 return blocks;
944}
945
6270df4c
ZD
946/* Hash function for struct loop_exit. */
947
948static hashval_t
949loop_exit_hash (const void *ex)
950{
5f754896 951 const struct loop_exit *const exit = (const struct loop_exit *) ex;
6270df4c
ZD
952
953 return htab_hash_pointer (exit->e);
954}
955
956/* Equality function for struct loop_exit. Compares with edge. */
957
958static int
959loop_exit_eq (const void *ex, const void *e)
960{
5f754896 961 const struct loop_exit *const exit = (const struct loop_exit *) ex;
6270df4c
ZD
962
963 return exit->e == e;
964}
965
966/* Frees the list of loop exit descriptions EX. */
967
968static void
969loop_exit_free (void *ex)
970{
971 struct loop_exit *exit = (struct loop_exit *) ex, *next;
972
973 for (; exit; exit = next)
974 {
975 next = exit->next_e;
b8698a0f 976
6270df4c
ZD
977 exit->next->prev = exit->prev;
978 exit->prev->next = exit->next;
979
9e2f83a5 980 ggc_free (exit);
6270df4c
ZD
981 }
982}
983
984/* Returns the list of records for E as an exit of a loop. */
985
986static struct loop_exit *
987get_exit_descriptions (edge e)
988{
ae50c0cb
TN
989 return (struct loop_exit *) htab_find_with_hash (current_loops->exits, e,
990 htab_hash_pointer (e));
6270df4c
ZD
991}
992
993/* Updates the lists of loop exits in that E appears.
994 If REMOVED is true, E is being removed, and we
995 just remove it from the lists of exits.
996 If NEW_EDGE is true and E is not a loop exit, we
997 do not try to remove it from loop exit lists. */
998
999void
1000rescan_loop_exit (edge e, bool new_edge, bool removed)
1001{
1002 void **slot;
1003 struct loop_exit *exits = NULL, *exit;
1004 struct loop *aloop, *cloop;
1005
f87000d0 1006 if (!loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
6270df4c
ZD
1007 return;
1008
1009 if (!removed
1010 && e->src->loop_father != NULL
1011 && e->dest->loop_father != NULL
1012 && !flow_bb_inside_loop_p (e->src->loop_father, e->dest))
1013 {
1014 cloop = find_common_loop (e->src->loop_father, e->dest->loop_father);
1015 for (aloop = e->src->loop_father;
1016 aloop != cloop;
9ba025a2 1017 aloop = loop_outer (aloop))
6270df4c 1018 {
a9429e29 1019 exit = ggc_alloc_loop_exit ();
6270df4c
ZD
1020 exit->e = e;
1021
9e2f83a5
ZD
1022 exit->next = aloop->exits->next;
1023 exit->prev = aloop->exits;
6270df4c
ZD
1024 exit->next->prev = exit;
1025 exit->prev->next = exit;
1026
1027 exit->next_e = exits;
1028 exits = exit;
1029 }
b8698a0f 1030 }
6270df4c
ZD
1031
1032 if (!exits && new_edge)
1033 return;
1034
1035 slot = htab_find_slot_with_hash (current_loops->exits, e,
1036 htab_hash_pointer (e),
1037 exits ? INSERT : NO_INSERT);
1038 if (!slot)
1039 return;
1040
1041 if (exits)
1042 {
1043 if (*slot)
1044 loop_exit_free (*slot);
1045 *slot = exits;
1046 }
1047 else
1048 htab_clear_slot (current_loops->exits, slot);
1049}
1050
1051/* For each loop, record list of exit edges, and start maintaining these
1052 lists. */
1053
1054void
1055record_loop_exits (void)
1056{
1057 basic_block bb;
1058 edge_iterator ei;
1059 edge e;
1060
4839cb59
ZD
1061 if (!current_loops)
1062 return;
1063
f87000d0 1064 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
6270df4c 1065 return;
f87000d0 1066 loops_state_set (LOOPS_HAVE_RECORDED_EXITS);
6270df4c
ZD
1067
1068 gcc_assert (current_loops->exits == NULL);
a9429e29
LB
1069 current_loops->exits = htab_create_ggc (2 * number_of_loops (),
1070 loop_exit_hash, loop_exit_eq,
1071 loop_exit_free);
6270df4c
ZD
1072
1073 FOR_EACH_BB (bb)
1074 {
1075 FOR_EACH_EDGE (e, ei, bb->succs)
1076 {
1077 rescan_loop_exit (e, true, false);
1078 }
1079 }
1080}
1081
1082/* Dumps information about the exit in *SLOT to FILE.
1083 Callback for htab_traverse. */
1084
1085static int
1086dump_recorded_exit (void **slot, void *file)
1087{
ae50c0cb 1088 struct loop_exit *exit = (struct loop_exit *) *slot;
6270df4c
ZD
1089 unsigned n = 0;
1090 edge e = exit->e;
1091
1092 for (; exit != NULL; exit = exit->next_e)
1093 n++;
1094
ae50c0cb 1095 fprintf ((FILE*) file, "Edge %d->%d exits %u loops\n",
6270df4c
ZD
1096 e->src->index, e->dest->index, n);
1097
1098 return 1;
1099}
1100
1101/* Dumps the recorded exits of loops to FILE. */
1102
1103extern void dump_recorded_exits (FILE *);
1104void
1105dump_recorded_exits (FILE *file)
1106{
1107 if (!current_loops->exits)
1108 return;
1109 htab_traverse (current_loops->exits, dump_recorded_exit, file);
1110}
1111
1112/* Releases lists of loop exits. */
1113
1114void
1115release_recorded_exits (void)
1116{
f87000d0 1117 gcc_assert (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS));
6270df4c
ZD
1118 htab_delete (current_loops->exits);
1119 current_loops->exits = NULL;
f87000d0 1120 loops_state_clear (LOOPS_HAVE_RECORDED_EXITS);
6270df4c
ZD
1121}
1122
ca83d385
ZD
1123/* Returns the list of the exit edges of a LOOP. */
1124
1125VEC (edge, heap) *
1126get_loop_exit_edges (const struct loop *loop)
35b07080 1127{
ca83d385
ZD
1128 VEC (edge, heap) *edges = NULL;
1129 edge e;
1130 unsigned i;
1131 basic_block *body;
628f6a4e 1132 edge_iterator ei;
6270df4c 1133 struct loop_exit *exit;
35b07080 1134
341c100f 1135 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
35b07080 1136
6270df4c
ZD
1137 /* If we maintain the lists of exits, use them. Otherwise we must
1138 scan the body of the loop. */
f87000d0 1139 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
6270df4c 1140 {
9e2f83a5 1141 for (exit = loop->exits->next; exit->e; exit = exit->next)
6270df4c
ZD
1142 VEC_safe_push (edge, heap, edges, exit->e);
1143 }
1144 else
1145 {
1146 body = get_loop_body (loop);
1147 for (i = 0; i < loop->num_nodes; i++)
1148 FOR_EACH_EDGE (e, ei, body[i]->succs)
1149 {
1150 if (!flow_bb_inside_loop_p (loop, e->dest))
1151 VEC_safe_push (edge, heap, edges, e);
1152 }
1153 free (body);
1154 }
35b07080
ZD
1155
1156 return edges;
1157}
1158
50654f6c
ZD
1159/* Counts the number of conditional branches inside LOOP. */
1160
1161unsigned
1162num_loop_branches (const struct loop *loop)
1163{
1164 unsigned i, n;
1165 basic_block * body;
1166
341c100f 1167 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
50654f6c
ZD
1168
1169 body = get_loop_body (loop);
1170 n = 0;
1171 for (i = 0; i < loop->num_nodes; i++)
628f6a4e 1172 if (EDGE_COUNT (body[i]->succs) >= 2)
50654f6c
ZD
1173 n++;
1174 free (body);
1175
1176 return n;
1177}
1178
2ecfd709
ZD
1179/* Adds basic block BB to LOOP. */
1180void
d329e058
AJ
1181add_bb_to_loop (basic_block bb, struct loop *loop)
1182{
9ba025a2
ZD
1183 unsigned i;
1184 loop_p ploop;
6270df4c
ZD
1185 edge_iterator ei;
1186 edge e;
1187
1188 gcc_assert (bb->loop_father == NULL);
1189 bb->loop_father = loop;
9ba025a2 1190 bb->loop_depth = loop_depth (loop);
6270df4c 1191 loop->num_nodes++;
ac47786e 1192 FOR_EACH_VEC_ELT (loop_p, loop->superloops, i, ploop)
9ba025a2 1193 ploop->num_nodes++;
6270df4c
ZD
1194
1195 FOR_EACH_EDGE (e, ei, bb->succs)
1196 {
1197 rescan_loop_exit (e, true, false);
1198 }
1199 FOR_EACH_EDGE (e, ei, bb->preds)
1200 {
1201 rescan_loop_exit (e, true, false);
1202 }
598ec7bd 1203}
2ecfd709
ZD
1204
1205/* Remove basic block BB from loops. */
1206void
d329e058
AJ
1207remove_bb_from_loops (basic_block bb)
1208{
6270df4c
ZD
1209 int i;
1210 struct loop *loop = bb->loop_father;
9ba025a2 1211 loop_p ploop;
6270df4c
ZD
1212 edge_iterator ei;
1213 edge e;
1214
1215 gcc_assert (loop != NULL);
1216 loop->num_nodes--;
ac47786e 1217 FOR_EACH_VEC_ELT (loop_p, loop->superloops, i, ploop)
9ba025a2 1218 ploop->num_nodes--;
6270df4c
ZD
1219 bb->loop_father = NULL;
1220 bb->loop_depth = 0;
1221
1222 FOR_EACH_EDGE (e, ei, bb->succs)
1223 {
1224 rescan_loop_exit (e, false, true);
1225 }
1226 FOR_EACH_EDGE (e, ei, bb->preds)
1227 {
1228 rescan_loop_exit (e, false, true);
1229 }
a310245f 1230}
2ecfd709
ZD
1231
1232/* Finds nearest common ancestor in loop tree for given loops. */
1233struct loop *
d329e058 1234find_common_loop (struct loop *loop_s, struct loop *loop_d)
2ecfd709 1235{
9ba025a2
ZD
1236 unsigned sdepth, ddepth;
1237
2ecfd709
ZD
1238 if (!loop_s) return loop_d;
1239 if (!loop_d) return loop_s;
d329e058 1240
9ba025a2
ZD
1241 sdepth = loop_depth (loop_s);
1242 ddepth = loop_depth (loop_d);
1243
1244 if (sdepth < ddepth)
1245 loop_d = VEC_index (loop_p, loop_d->superloops, sdepth);
1246 else if (sdepth > ddepth)
1247 loop_s = VEC_index (loop_p, loop_s->superloops, ddepth);
2ecfd709
ZD
1248
1249 while (loop_s != loop_d)
1250 {
9ba025a2
ZD
1251 loop_s = loop_outer (loop_s);
1252 loop_d = loop_outer (loop_d);
2ecfd709
ZD
1253 }
1254 return loop_s;
1255}
1256
42fd6772
ZD
1257/* Removes LOOP from structures and frees its data. */
1258
1259void
1260delete_loop (struct loop *loop)
1261{
1262 /* Remove the loop from structure. */
1263 flow_loop_tree_node_remove (loop);
1264
1265 /* Remove loop from loops array. */
1266 VEC_replace (loop_p, current_loops->larray, loop->num, NULL);
1267
1268 /* Free loop data. */
1269 flow_loop_free (loop);
1270}
1271
3d436d2a 1272/* Cancels the LOOP; it must be innermost one. */
b00bf166
KH
1273
1274static void
d73be268 1275cancel_loop (struct loop *loop)
3d436d2a
ZD
1276{
1277 basic_block *bbs;
1278 unsigned i;
9ba025a2 1279 struct loop *outer = loop_outer (loop);
3d436d2a 1280
341c100f 1281 gcc_assert (!loop->inner);
3d436d2a
ZD
1282
1283 /* Move blocks up one level (they should be removed as soon as possible). */
1284 bbs = get_loop_body (loop);
1285 for (i = 0; i < loop->num_nodes; i++)
9ba025a2 1286 bbs[i]->loop_father = outer;
3d436d2a 1287
b78384e0 1288 free (bbs);
42fd6772 1289 delete_loop (loop);
3d436d2a
ZD
1290}
1291
1292/* Cancels LOOP and all its subloops. */
1293void
d73be268 1294cancel_loop_tree (struct loop *loop)
3d436d2a
ZD
1295{
1296 while (loop->inner)
d73be268
ZD
1297 cancel_loop_tree (loop->inner);
1298 cancel_loop (loop);
3d436d2a
ZD
1299}
1300
d73be268 1301/* Checks that information about loops is correct
e0bb17a8 1302 -- sizes of loops are all right
2ecfd709
ZD
1303 -- results of get_loop_body really belong to the loop
1304 -- loop header have just single entry edge and single latch edge
1305 -- loop latches have only single successor that is header of their loop
3d436d2a 1306 -- irreducible loops are correctly marked
2ecfd709 1307 */
24e47c76 1308DEBUG_FUNCTION void
d73be268 1309verify_loop_structure (void)
2ecfd709 1310{
3d436d2a
ZD
1311 unsigned *sizes, i, j;
1312 sbitmap irreds;
2ecfd709
ZD
1313 basic_block *bbs, bb;
1314 struct loop *loop;
1315 int err = 0;
35b07080 1316 edge e;
42fd6772
ZD
1317 unsigned num = number_of_loops ();
1318 loop_iterator li;
6270df4c 1319 struct loop_exit *exit, *mexit;
2ecfd709 1320
510dbcce
RG
1321 /* We need up-to-date dominators, verify them. */
1322 verify_dominators (CDI_DOMINATORS);
1323
2ecfd709 1324 /* Check sizes. */
42fd6772 1325 sizes = XCNEWVEC (unsigned, num);
2ecfd709
ZD
1326 sizes[0] = 2;
1327
1328 FOR_EACH_BB (bb)
9ba025a2 1329 for (loop = bb->loop_father; loop; loop = loop_outer (loop))
2ecfd709
ZD
1330 sizes[loop->num]++;
1331
42fd6772 1332 FOR_EACH_LOOP (li, loop, LI_INCLUDE_ROOT)
2ecfd709 1333 {
42fd6772 1334 i = loop->num;
2ecfd709 1335
42fd6772 1336 if (loop->num_nodes != sizes[i])
2ecfd709 1337 {
ab532386 1338 error ("size of loop %d should be %d, not %d",
42fd6772 1339 i, sizes[i], loop->num_nodes);
2ecfd709
ZD
1340 err = 1;
1341 }
1342 }
1343
2ecfd709 1344 /* Check get_loop_body. */
42fd6772 1345 FOR_EACH_LOOP (li, loop, 0)
2ecfd709 1346 {
2ecfd709
ZD
1347 bbs = get_loop_body (loop);
1348
1349 for (j = 0; j < loop->num_nodes; j++)
1350 if (!flow_bb_inside_loop_p (loop, bbs[j]))
1351 {
ab532386 1352 error ("bb %d do not belong to loop %d",
42fd6772 1353 bbs[j]->index, loop->num);
2ecfd709
ZD
1354 err = 1;
1355 }
1356 free (bbs);
1357 }
1358
1359 /* Check headers and latches. */
42fd6772 1360 FOR_EACH_LOOP (li, loop, 0)
2ecfd709 1361 {
42fd6772 1362 i = loop->num;
2ecfd709 1363
f87000d0 1364 if (loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS)
628f6a4e 1365 && EDGE_COUNT (loop->header->preds) != 2)
2ecfd709 1366 {
d8a07487 1367 error ("loop %d%'s header does not have exactly 2 entries", i);
2ecfd709
ZD
1368 err = 1;
1369 }
f87000d0 1370 if (loops_state_satisfies_p (LOOPS_HAVE_SIMPLE_LATCHES))
2ecfd709 1371 {
c5cbcccf 1372 if (!single_succ_p (loop->latch))
2ecfd709 1373 {
d8a07487 1374 error ("loop %d%'s latch does not have exactly 1 successor", i);
2ecfd709
ZD
1375 err = 1;
1376 }
c5cbcccf 1377 if (single_succ (loop->latch) != loop->header)
2ecfd709 1378 {
d8a07487 1379 error ("loop %d%'s latch does not have header as successor", i);
2ecfd709
ZD
1380 err = 1;
1381 }
1382 if (loop->latch->loop_father != loop)
1383 {
d8a07487 1384 error ("loop %d%'s latch does not belong directly to it", i);
2ecfd709
ZD
1385 err = 1;
1386 }
1387 }
1388 if (loop->header->loop_father != loop)
1389 {
d8a07487 1390 error ("loop %d%'s header does not belong directly to it", i);
2ecfd709
ZD
1391 err = 1;
1392 }
f87000d0 1393 if (loops_state_satisfies_p (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
35b07080
ZD
1394 && (loop_latch_edge (loop)->flags & EDGE_IRREDUCIBLE_LOOP))
1395 {
d8a07487 1396 error ("loop %d%'s latch is marked as part of irreducible region", i);
35b07080
ZD
1397 err = 1;
1398 }
2ecfd709
ZD
1399 }
1400
3d436d2a 1401 /* Check irreducible loops. */
f87000d0 1402 if (loops_state_satisfies_p (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS))
3d436d2a
ZD
1403 {
1404 /* Record old info. */
1405 irreds = sbitmap_alloc (last_basic_block);
1406 FOR_EACH_BB (bb)
35b07080 1407 {
628f6a4e 1408 edge_iterator ei;
35b07080
ZD
1409 if (bb->flags & BB_IRREDUCIBLE_LOOP)
1410 SET_BIT (irreds, bb->index);
1411 else
1412 RESET_BIT (irreds, bb->index);
628f6a4e 1413 FOR_EACH_EDGE (e, ei, bb->succs)
35b07080 1414 if (e->flags & EDGE_IRREDUCIBLE_LOOP)
d329e058 1415 e->flags |= EDGE_ALL_FLAGS + 1;
35b07080 1416 }
3d436d2a
ZD
1417
1418 /* Recount it. */
d73be268 1419 mark_irreducible_loops ();
3d436d2a
ZD
1420
1421 /* Compare. */
1422 FOR_EACH_BB (bb)
1423 {
628f6a4e
BE
1424 edge_iterator ei;
1425
3d436d2a
ZD
1426 if ((bb->flags & BB_IRREDUCIBLE_LOOP)
1427 && !TEST_BIT (irreds, bb->index))
1428 {
ab532386 1429 error ("basic block %d should be marked irreducible", bb->index);
3d436d2a
ZD
1430 err = 1;
1431 }
1432 else if (!(bb->flags & BB_IRREDUCIBLE_LOOP)
1433 && TEST_BIT (irreds, bb->index))
1434 {
ab532386 1435 error ("basic block %d should not be marked irreducible", bb->index);
3d436d2a
ZD
1436 err = 1;
1437 }
628f6a4e 1438 FOR_EACH_EDGE (e, ei, bb->succs)
35b07080
ZD
1439 {
1440 if ((e->flags & EDGE_IRREDUCIBLE_LOOP)
1441 && !(e->flags & (EDGE_ALL_FLAGS + 1)))
1442 {
ab532386 1443 error ("edge from %d to %d should be marked irreducible",
35b07080
ZD
1444 e->src->index, e->dest->index);
1445 err = 1;
1446 }
1447 else if (!(e->flags & EDGE_IRREDUCIBLE_LOOP)
1448 && (e->flags & (EDGE_ALL_FLAGS + 1)))
1449 {
ab532386 1450 error ("edge from %d to %d should not be marked irreducible",
35b07080
ZD
1451 e->src->index, e->dest->index);
1452 err = 1;
1453 }
1454 e->flags &= ~(EDGE_ALL_FLAGS + 1);
1455 }
3d436d2a
ZD
1456 }
1457 free (irreds);
1458 }
1459
6270df4c
ZD
1460 /* Check the recorded loop exits. */
1461 FOR_EACH_LOOP (li, loop, 0)
82b85a85 1462 {
9e2f83a5 1463 if (!loop->exits || loop->exits->e != NULL)
6270df4c
ZD
1464 {
1465 error ("corrupted head of the exits list of loop %d",
1466 loop->num);
1467 err = 1;
1468 }
1469 else
1470 {
1471 /* Check that the list forms a cycle, and all elements except
1472 for the head are nonnull. */
9e2f83a5 1473 for (mexit = loop->exits, exit = mexit->next, i = 0;
6270df4c
ZD
1474 exit->e && exit != mexit;
1475 exit = exit->next)
1476 {
1477 if (i++ & 1)
1478 mexit = mexit->next;
1479 }
1480
9e2f83a5 1481 if (exit != loop->exits)
6270df4c
ZD
1482 {
1483 error ("corrupted exits list of loop %d", loop->num);
1484 err = 1;
1485 }
1486 }
1487
f87000d0 1488 if (!loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
6270df4c 1489 {
9e2f83a5 1490 if (loop->exits->next != loop->exits)
6270df4c
ZD
1491 {
1492 error ("nonempty exits list of loop %d, but exits are not recorded",
1493 loop->num);
1494 err = 1;
1495 }
1496 }
1497 }
1498
f87000d0 1499 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
6270df4c
ZD
1500 {
1501 unsigned n_exits = 0, eloops;
1502
42fd6772 1503 memset (sizes, 0, sizeof (unsigned) * num);
82b85a85
ZD
1504 FOR_EACH_BB (bb)
1505 {
628f6a4e 1506 edge_iterator ei;
d73be268 1507 if (bb->loop_father == current_loops->tree_root)
82b85a85 1508 continue;
628f6a4e 1509 FOR_EACH_EDGE (e, ei, bb->succs)
82b85a85 1510 {
82b85a85
ZD
1511 if (flow_bb_inside_loop_p (bb->loop_father, e->dest))
1512 continue;
1513
6270df4c
ZD
1514 n_exits++;
1515 exit = get_exit_descriptions (e);
1516 if (!exit)
1517 {
d8a07487 1518 error ("exit %d->%d not recorded",
6270df4c
ZD
1519 e->src->index, e->dest->index);
1520 err = 1;
1521 }
1522 eloops = 0;
1523 for (; exit; exit = exit->next_e)
1524 eloops++;
1525
82b85a85
ZD
1526 for (loop = bb->loop_father;
1527 loop != e->dest->loop_father;
9ba025a2 1528 loop = loop_outer (loop))
82b85a85 1529 {
6270df4c 1530 eloops--;
82b85a85 1531 sizes[loop->num]++;
6270df4c
ZD
1532 }
1533
1534 if (eloops != 0)
1535 {
d8a07487 1536 error ("wrong list of exited loops for edge %d->%d",
6270df4c
ZD
1537 e->src->index, e->dest->index);
1538 err = 1;
82b85a85
ZD
1539 }
1540 }
1541 }
1542
6270df4c 1543 if (n_exits != htab_elements (current_loops->exits))
82b85a85 1544 {
d8a07487 1545 error ("too many loop exits recorded");
6270df4c
ZD
1546 err = 1;
1547 }
82b85a85 1548
6270df4c
ZD
1549 FOR_EACH_LOOP (li, loop, 0)
1550 {
1551 eloops = 0;
9e2f83a5 1552 for (exit = loop->exits->next; exit->e; exit = exit->next)
6270df4c
ZD
1553 eloops++;
1554 if (eloops != sizes[loop->num])
82b85a85 1555 {
6270df4c
ZD
1556 error ("%d exits recorded for loop %d (having %d exits)",
1557 eloops, loop->num, sizes[loop->num]);
82b85a85
ZD
1558 err = 1;
1559 }
1560 }
1561 }
1562
341c100f 1563 gcc_assert (!err);
82b85a85
ZD
1564
1565 free (sizes);
2ecfd709
ZD
1566}
1567
1568/* Returns latch edge of LOOP. */
1569edge
d329e058 1570loop_latch_edge (const struct loop *loop)
2ecfd709 1571{
9ff3d2de 1572 return find_edge (loop->latch, loop->header);
402209ff 1573}
2ecfd709
ZD
1574
1575/* Returns preheader edge of LOOP. */
1576edge
d329e058 1577loop_preheader_edge (const struct loop *loop)
2ecfd709
ZD
1578{
1579 edge e;
628f6a4e 1580 edge_iterator ei;
2ecfd709 1581
f87000d0 1582 gcc_assert (loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS));
c7b852c8 1583
628f6a4e
BE
1584 FOR_EACH_EDGE (e, ei, loop->header->preds)
1585 if (e->src != loop->latch)
1586 break;
2ecfd709
ZD
1587
1588 return e;
1589}
70388d94
ZD
1590
1591/* Returns true if E is an exit of LOOP. */
1592
1593bool
ed7a4b4b 1594loop_exit_edge_p (const struct loop *loop, const_edge e)
70388d94
ZD
1595{
1596 return (flow_bb_inside_loop_p (loop, e->src)
1597 && !flow_bb_inside_loop_p (loop, e->dest));
1598}
ac8f6c69
ZD
1599
1600/* Returns the single exit edge of LOOP, or NULL if LOOP has either no exit
6270df4c
ZD
1601 or more than one exit. If loops do not have the exits recorded, NULL
1602 is returned always. */
ac8f6c69
ZD
1603
1604edge
1605single_exit (const struct loop *loop)
1606{
9e2f83a5 1607 struct loop_exit *exit = loop->exits->next;
ac8f6c69 1608
f87000d0 1609 if (!loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
6270df4c 1610 return NULL;
ac8f6c69 1611
9e2f83a5 1612 if (exit->e && exit->next == loop->exits)
6270df4c
ZD
1613 return exit->e;
1614 else
1615 return NULL;
ac8f6c69 1616}
f8bf9252 1617
f4ce375d 1618/* Returns true when BB has an incoming edge exiting LOOP. */
f8bf9252
SP
1619
1620bool
f4ce375d 1621loop_exits_to_bb_p (struct loop *loop, basic_block bb)
f8bf9252
SP
1622{
1623 edge e;
1624 edge_iterator ei;
1625
1626 FOR_EACH_EDGE (e, ei, bb->preds)
1627 if (loop_exit_edge_p (loop, e))
1628 return true;
1629
1630 return false;
1631}
f4ce375d
VK
1632
1633/* Returns true when BB has an outgoing edge exiting LOOP. */
1634
1635bool
1636loop_exits_from_bb_p (struct loop *loop, basic_block bb)
1637{
1638 edge e;
1639 edge_iterator ei;
1640
1641 FOR_EACH_EDGE (e, ei, bb->succs)
1642 if (loop_exit_edge_p (loop, e))
1643 return true;
1644
1645 return false;
1646}
This page took 2.61915 seconds and 5 git commands to generate.