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