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