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