]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cfghooks.c
2015-06-04 Andrew MacLeod <amacleod@redhat.com>
[thirdparty/gcc.git] / gcc / cfghooks.c
1 /* Hooks for cfg representation specific functions.
2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <s.pop@laposte.net>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "dumpfile.h"
25 #include "tm.h"
26 #include "hash-set.h"
27 #include "vec.h"
28 #include "input.h"
29 #include "alias.h"
30 #include "symtab.h"
31 #include "inchash.h"
32 #include "tree.h"
33 #include "rtl.h"
34 #include "predict.h"
35 #include "vec.h"
36 #include "hashtab.h"
37 #include "hash-set.h"
38 #include "hard-reg-set.h"
39 #include "input.h"
40 #include "function.h"
41 #include "dominance.h"
42 #include "cfg.h"
43 #include "cfganal.h"
44 #include "basic-block.h"
45 #include "tree-ssa.h"
46 #include "timevar.h"
47 #include "diagnostic-core.h"
48 #include "cfgloop.h"
49 #include "pretty-print.h"
50
51 /* A pointer to one of the hooks containers. */
52 static struct cfg_hooks *cfg_hooks;
53
54 /* Initialization of functions specific to the rtl IR. */
55 void
56 rtl_register_cfg_hooks (void)
57 {
58 cfg_hooks = &rtl_cfg_hooks;
59 }
60
61 /* Initialization of functions specific to the rtl IR. */
62 void
63 cfg_layout_rtl_register_cfg_hooks (void)
64 {
65 cfg_hooks = &cfg_layout_rtl_cfg_hooks;
66 }
67
68 /* Initialization of functions specific to the tree IR. */
69
70 void
71 gimple_register_cfg_hooks (void)
72 {
73 cfg_hooks = &gimple_cfg_hooks;
74 }
75
76 struct cfg_hooks
77 get_cfg_hooks (void)
78 {
79 return *cfg_hooks;
80 }
81
82 void
83 set_cfg_hooks (struct cfg_hooks new_cfg_hooks)
84 {
85 *cfg_hooks = new_cfg_hooks;
86 }
87
88 /* Returns current ir type. */
89
90 enum ir_type
91 current_ir_type (void)
92 {
93 if (cfg_hooks == &gimple_cfg_hooks)
94 return IR_GIMPLE;
95 else if (cfg_hooks == &rtl_cfg_hooks)
96 return IR_RTL_CFGRTL;
97 else if (cfg_hooks == &cfg_layout_rtl_cfg_hooks)
98 return IR_RTL_CFGLAYOUT;
99 else
100 gcc_unreachable ();
101 }
102
103 /* Verify the CFG consistency.
104
105 Currently it does following: checks edge and basic block list correctness
106 and calls into IL dependent checking then. */
107
108 DEBUG_FUNCTION void
109 verify_flow_info (void)
110 {
111 size_t *edge_checksum;
112 int err = 0;
113 basic_block bb, last_bb_seen;
114 basic_block *last_visited;
115
116 timevar_push (TV_CFG_VERIFY);
117 last_visited = XCNEWVEC (basic_block, last_basic_block_for_fn (cfun));
118 edge_checksum = XCNEWVEC (size_t, last_basic_block_for_fn (cfun));
119
120 /* Check bb chain & numbers. */
121 last_bb_seen = ENTRY_BLOCK_PTR_FOR_FN (cfun);
122 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb, NULL, next_bb)
123 {
124 if (bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
125 && bb != BASIC_BLOCK_FOR_FN (cfun, bb->index))
126 {
127 error ("bb %d on wrong place", bb->index);
128 err = 1;
129 }
130
131 if (bb->prev_bb != last_bb_seen)
132 {
133 error ("prev_bb of %d should be %d, not %d",
134 bb->index, last_bb_seen->index, bb->prev_bb->index);
135 err = 1;
136 }
137
138 last_bb_seen = bb;
139 }
140
141 /* Now check the basic blocks (boundaries etc.) */
142 FOR_EACH_BB_REVERSE_FN (bb, cfun)
143 {
144 int n_fallthru = 0;
145 edge e;
146 edge_iterator ei;
147
148 if (bb->loop_father != NULL && current_loops == NULL)
149 {
150 error ("verify_flow_info: Block %i has loop_father, but there are no loops",
151 bb->index);
152 err = 1;
153 }
154 if (bb->loop_father == NULL && current_loops != NULL)
155 {
156 error ("verify_flow_info: Block %i lacks loop_father", bb->index);
157 err = 1;
158 }
159
160 if (bb->count < 0)
161 {
162 error ("verify_flow_info: Wrong count of block %i %i",
163 bb->index, (int)bb->count);
164 err = 1;
165 }
166 if (bb->frequency < 0)
167 {
168 error ("verify_flow_info: Wrong frequency of block %i %i",
169 bb->index, bb->frequency);
170 err = 1;
171 }
172 FOR_EACH_EDGE (e, ei, bb->succs)
173 {
174 if (last_visited [e->dest->index] == bb)
175 {
176 error ("verify_flow_info: Duplicate edge %i->%i",
177 e->src->index, e->dest->index);
178 err = 1;
179 }
180 if (e->probability < 0 || e->probability > REG_BR_PROB_BASE)
181 {
182 error ("verify_flow_info: Wrong probability of edge %i->%i %i",
183 e->src->index, e->dest->index, e->probability);
184 err = 1;
185 }
186 if (e->count < 0)
187 {
188 error ("verify_flow_info: Wrong count of edge %i->%i %i",
189 e->src->index, e->dest->index, (int)e->count);
190 err = 1;
191 }
192
193 last_visited [e->dest->index] = bb;
194
195 if (e->flags & EDGE_FALLTHRU)
196 n_fallthru++;
197
198 if (e->src != bb)
199 {
200 error ("verify_flow_info: Basic block %d succ edge is corrupted",
201 bb->index);
202 fprintf (stderr, "Predecessor: ");
203 dump_edge_info (stderr, e, TDF_DETAILS, 0);
204 fprintf (stderr, "\nSuccessor: ");
205 dump_edge_info (stderr, e, TDF_DETAILS, 1);
206 fprintf (stderr, "\n");
207 err = 1;
208 }
209
210 edge_checksum[e->dest->index] += (size_t) e;
211 }
212 if (n_fallthru > 1)
213 {
214 error ("wrong amount of branch edges after unconditional jump %i", bb->index);
215 err = 1;
216 }
217
218 FOR_EACH_EDGE (e, ei, bb->preds)
219 {
220 if (e->dest != bb)
221 {
222 error ("basic block %d pred edge is corrupted", bb->index);
223 fputs ("Predecessor: ", stderr);
224 dump_edge_info (stderr, e, TDF_DETAILS, 0);
225 fputs ("\nSuccessor: ", stderr);
226 dump_edge_info (stderr, e, TDF_DETAILS, 1);
227 fputc ('\n', stderr);
228 err = 1;
229 }
230
231 if (ei.index != e->dest_idx)
232 {
233 error ("basic block %d pred edge is corrupted", bb->index);
234 error ("its dest_idx should be %d, not %d",
235 ei.index, e->dest_idx);
236 fputs ("Predecessor: ", stderr);
237 dump_edge_info (stderr, e, TDF_DETAILS, 0);
238 fputs ("\nSuccessor: ", stderr);
239 dump_edge_info (stderr, e, TDF_DETAILS, 1);
240 fputc ('\n', stderr);
241 err = 1;
242 }
243
244 edge_checksum[e->dest->index] -= (size_t) e;
245 }
246 }
247
248 /* Complete edge checksumming for ENTRY and EXIT. */
249 {
250 edge e;
251 edge_iterator ei;
252
253 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
254 edge_checksum[e->dest->index] += (size_t) e;
255
256 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
257 edge_checksum[e->dest->index] -= (size_t) e;
258 }
259
260 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
261 if (edge_checksum[bb->index])
262 {
263 error ("basic block %i edge lists are corrupted", bb->index);
264 err = 1;
265 }
266
267 last_bb_seen = ENTRY_BLOCK_PTR_FOR_FN (cfun);
268
269 /* Clean up. */
270 free (last_visited);
271 free (edge_checksum);
272
273 if (cfg_hooks->verify_flow_info)
274 err |= cfg_hooks->verify_flow_info ();
275 if (err)
276 internal_error ("verify_flow_info failed");
277 timevar_pop (TV_CFG_VERIFY);
278 }
279
280 /* Print out one basic block BB to file OUTF. INDENT is printed at the
281 start of each new line. FLAGS are the TDF_* flags in dumpfile.h.
282
283 This function takes care of the purely graph related information.
284 The cfg hook for the active representation should dump
285 representation-specific information. */
286
287 void
288 dump_bb (FILE *outf, basic_block bb, int indent, int flags)
289 {
290 if (flags & TDF_BLOCKS)
291 dump_bb_info (outf, bb, indent, flags, true, false);
292 if (cfg_hooks->dump_bb)
293 cfg_hooks->dump_bb (outf, bb, indent, flags);
294 if (flags & TDF_BLOCKS)
295 dump_bb_info (outf, bb, indent, flags, false, true);
296 fputc ('\n', outf);
297 }
298
299 DEBUG_FUNCTION void
300 debug (basic_block_def &ref)
301 {
302 dump_bb (stderr, &ref, 0, 0);
303 }
304
305 DEBUG_FUNCTION void
306 debug (basic_block_def *ptr)
307 {
308 if (ptr)
309 debug (*ptr);
310 else
311 fprintf (stderr, "<nil>\n");
312 }
313
314
315 /* Dumps basic block BB to pretty-printer PP, for use as a label of
316 a DOT graph record-node. The implementation of this hook is
317 expected to write the label to the stream that is attached to PP.
318 Field separators between instructions are pipe characters printed
319 verbatim. Instructions should be written with some characters
320 escaped, using pp_write_text_as_dot_label_to_stream(). */
321
322 void
323 dump_bb_for_graph (pretty_printer *pp, basic_block bb)
324 {
325 if (!cfg_hooks->dump_bb_for_graph)
326 internal_error ("%s does not support dump_bb_for_graph",
327 cfg_hooks->name);
328 if (bb->count)
329 pp_printf (pp, "COUNT:" "%" PRId64, bb->count);
330 pp_printf (pp, " FREQ:%i |", bb->frequency);
331 pp_write_text_to_stream (pp);
332 if (!(dump_flags & TDF_SLIM))
333 cfg_hooks->dump_bb_for_graph (pp, bb);
334 }
335
336 /* Dump the complete CFG to FILE. FLAGS are the TDF_* flags in dumpfile.h. */
337 void
338 dump_flow_info (FILE *file, int flags)
339 {
340 basic_block bb;
341
342 fprintf (file, "\n%d basic blocks, %d edges.\n", n_basic_blocks_for_fn (cfun),
343 n_edges_for_fn (cfun));
344 FOR_ALL_BB_FN (bb, cfun)
345 dump_bb (file, bb, 0, flags);
346
347 putc ('\n', file);
348 }
349
350 /* Like above, but dump to stderr. To be called from debuggers. */
351 void debug_flow_info (void);
352 DEBUG_FUNCTION void
353 debug_flow_info (void)
354 {
355 dump_flow_info (stderr, TDF_DETAILS);
356 }
357
358 /* Redirect edge E to the given basic block DEST and update underlying program
359 representation. Returns edge representing redirected branch (that may not
360 be equivalent to E in the case of duplicate edges being removed) or NULL
361 if edge is not easily redirectable for whatever reason. */
362
363 edge
364 redirect_edge_and_branch (edge e, basic_block dest)
365 {
366 edge ret;
367
368 if (!cfg_hooks->redirect_edge_and_branch)
369 internal_error ("%s does not support redirect_edge_and_branch",
370 cfg_hooks->name);
371
372 ret = cfg_hooks->redirect_edge_and_branch (e, dest);
373
374 /* If RET != E, then either the redirection failed, or the edge E
375 was removed since RET already lead to the same destination. */
376 if (current_loops != NULL && ret == e)
377 rescan_loop_exit (e, false, false);
378
379 return ret;
380 }
381
382 /* Returns true if it is possible to remove the edge E by redirecting it
383 to the destination of the other edge going from its source. */
384
385 bool
386 can_remove_branch_p (const_edge e)
387 {
388 if (!cfg_hooks->can_remove_branch_p)
389 internal_error ("%s does not support can_remove_branch_p",
390 cfg_hooks->name);
391
392 if (EDGE_COUNT (e->src->succs) != 2)
393 return false;
394
395 return cfg_hooks->can_remove_branch_p (e);
396 }
397
398 /* Removes E, by redirecting it to the destination of the other edge going
399 from its source. Can_remove_branch_p must be true for E, hence this
400 operation cannot fail. */
401
402 void
403 remove_branch (edge e)
404 {
405 edge other;
406 basic_block src = e->src;
407 int irr;
408
409 gcc_assert (EDGE_COUNT (e->src->succs) == 2);
410
411 other = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e);
412 irr = other->flags & EDGE_IRREDUCIBLE_LOOP;
413
414 e = redirect_edge_and_branch (e, other->dest);
415 gcc_assert (e != NULL);
416
417 e->flags &= ~EDGE_IRREDUCIBLE_LOOP;
418 e->flags |= irr;
419 }
420
421 /* Removes edge E from cfg. Unlike remove_branch, it does not update IL. */
422
423 void
424 remove_edge (edge e)
425 {
426 if (current_loops != NULL)
427 rescan_loop_exit (e, false, true);
428
429 /* This is probably not needed, but it doesn't hurt. */
430 /* FIXME: This should be called via a remove_edge hook. */
431 if (current_ir_type () == IR_GIMPLE)
432 redirect_edge_var_map_clear (e);
433
434 remove_edge_raw (e);
435 }
436
437 /* Like redirect_edge_succ but avoid possible duplicate edge. */
438
439 edge
440 redirect_edge_succ_nodup (edge e, basic_block new_succ)
441 {
442 edge s;
443
444 s = find_edge (e->src, new_succ);
445 if (s && s != e)
446 {
447 s->flags |= e->flags;
448 s->probability += e->probability;
449 if (s->probability > REG_BR_PROB_BASE)
450 s->probability = REG_BR_PROB_BASE;
451 s->count += e->count;
452 /* FIXME: This should be called via a hook and only for IR_GIMPLE. */
453 redirect_edge_var_map_dup (s, e);
454 remove_edge (e);
455 e = s;
456 }
457 else
458 redirect_edge_succ (e, new_succ);
459
460 return e;
461 }
462
463 /* Redirect the edge E to basic block DEST even if it requires creating
464 of a new basic block; then it returns the newly created basic block.
465 Aborts when redirection is impossible. */
466
467 basic_block
468 redirect_edge_and_branch_force (edge e, basic_block dest)
469 {
470 basic_block ret, src = e->src;
471
472 if (!cfg_hooks->redirect_edge_and_branch_force)
473 internal_error ("%s does not support redirect_edge_and_branch_force",
474 cfg_hooks->name);
475
476 if (current_loops != NULL)
477 rescan_loop_exit (e, false, true);
478
479 ret = cfg_hooks->redirect_edge_and_branch_force (e, dest);
480
481 if (ret != NULL && dom_info_available_p (CDI_DOMINATORS))
482 set_immediate_dominator (CDI_DOMINATORS, ret, src);
483
484 if (current_loops != NULL)
485 {
486 if (ret != NULL)
487 {
488 struct loop *loop
489 = find_common_loop (single_pred (ret)->loop_father,
490 single_succ (ret)->loop_father);
491 add_bb_to_loop (ret, loop);
492 }
493 else if (find_edge (src, dest) == e)
494 rescan_loop_exit (e, true, false);
495 }
496
497 return ret;
498 }
499
500 /* Splits basic block BB after the specified instruction I (but at least after
501 the labels). If I is NULL, splits just after labels. The newly created edge
502 is returned. The new basic block is created just after the old one. */
503
504 static edge
505 split_block_1 (basic_block bb, void *i)
506 {
507 basic_block new_bb;
508 edge res;
509
510 if (!cfg_hooks->split_block)
511 internal_error ("%s does not support split_block", cfg_hooks->name);
512
513 new_bb = cfg_hooks->split_block (bb, i);
514 if (!new_bb)
515 return NULL;
516
517 new_bb->count = bb->count;
518 new_bb->frequency = bb->frequency;
519 new_bb->discriminator = bb->discriminator;
520
521 if (dom_info_available_p (CDI_DOMINATORS))
522 {
523 redirect_immediate_dominators (CDI_DOMINATORS, bb, new_bb);
524 set_immediate_dominator (CDI_DOMINATORS, new_bb, bb);
525 }
526
527 if (current_loops != NULL)
528 {
529 edge_iterator ei;
530 edge e;
531 add_bb_to_loop (new_bb, bb->loop_father);
532 /* Identify all loops bb may have been the latch of and adjust them. */
533 FOR_EACH_EDGE (e, ei, new_bb->succs)
534 if (e->dest->loop_father->latch == bb)
535 e->dest->loop_father->latch = new_bb;
536 }
537
538 res = make_single_succ_edge (bb, new_bb, EDGE_FALLTHRU);
539
540 if (bb->flags & BB_IRREDUCIBLE_LOOP)
541 {
542 new_bb->flags |= BB_IRREDUCIBLE_LOOP;
543 res->flags |= EDGE_IRREDUCIBLE_LOOP;
544 }
545
546 return res;
547 }
548
549 edge
550 split_block (basic_block bb, gimple i)
551 {
552 return split_block_1 (bb, i);
553 }
554
555 edge
556 split_block (basic_block bb, rtx i)
557 {
558 return split_block_1 (bb, i);
559 }
560
561 /* Splits block BB just after labels. The newly created edge is returned. */
562
563 edge
564 split_block_after_labels (basic_block bb)
565 {
566 return split_block_1 (bb, NULL);
567 }
568
569 /* Moves block BB immediately after block AFTER. Returns false if the
570 movement was impossible. */
571
572 bool
573 move_block_after (basic_block bb, basic_block after)
574 {
575 bool ret;
576
577 if (!cfg_hooks->move_block_after)
578 internal_error ("%s does not support move_block_after", cfg_hooks->name);
579
580 ret = cfg_hooks->move_block_after (bb, after);
581
582 return ret;
583 }
584
585 /* Deletes the basic block BB. */
586
587 void
588 delete_basic_block (basic_block bb)
589 {
590 if (!cfg_hooks->delete_basic_block)
591 internal_error ("%s does not support delete_basic_block", cfg_hooks->name);
592
593 cfg_hooks->delete_basic_block (bb);
594
595 if (current_loops != NULL)
596 {
597 struct loop *loop = bb->loop_father;
598
599 /* If we remove the header or the latch of a loop, mark the loop for
600 removal. */
601 if (loop->latch == bb
602 || loop->header == bb)
603 mark_loop_for_removal (loop);
604
605 remove_bb_from_loops (bb);
606 }
607
608 /* Remove the edges into and out of this block. Note that there may
609 indeed be edges in, if we are removing an unreachable loop. */
610 while (EDGE_COUNT (bb->preds) != 0)
611 remove_edge (EDGE_PRED (bb, 0));
612 while (EDGE_COUNT (bb->succs) != 0)
613 remove_edge (EDGE_SUCC (bb, 0));
614
615 if (dom_info_available_p (CDI_DOMINATORS))
616 delete_from_dominance_info (CDI_DOMINATORS, bb);
617 if (dom_info_available_p (CDI_POST_DOMINATORS))
618 delete_from_dominance_info (CDI_POST_DOMINATORS, bb);
619
620 /* Remove the basic block from the array. */
621 expunge_block (bb);
622 }
623
624 /* Splits edge E and returns the newly created basic block. */
625
626 basic_block
627 split_edge (edge e)
628 {
629 basic_block ret;
630 gcov_type count = e->count;
631 int freq = EDGE_FREQUENCY (e);
632 edge f;
633 bool irr = (e->flags & EDGE_IRREDUCIBLE_LOOP) != 0;
634 struct loop *loop;
635 basic_block src = e->src, dest = e->dest;
636
637 if (!cfg_hooks->split_edge)
638 internal_error ("%s does not support split_edge", cfg_hooks->name);
639
640 if (current_loops != NULL)
641 rescan_loop_exit (e, false, true);
642
643 ret = cfg_hooks->split_edge (e);
644 ret->count = count;
645 ret->frequency = freq;
646 single_succ_edge (ret)->probability = REG_BR_PROB_BASE;
647 single_succ_edge (ret)->count = count;
648
649 if (irr)
650 {
651 ret->flags |= BB_IRREDUCIBLE_LOOP;
652 single_pred_edge (ret)->flags |= EDGE_IRREDUCIBLE_LOOP;
653 single_succ_edge (ret)->flags |= EDGE_IRREDUCIBLE_LOOP;
654 }
655
656 if (dom_info_available_p (CDI_DOMINATORS))
657 set_immediate_dominator (CDI_DOMINATORS, ret, single_pred (ret));
658
659 if (dom_info_state (CDI_DOMINATORS) >= DOM_NO_FAST_QUERY)
660 {
661 /* There are two cases:
662
663 If the immediate dominator of e->dest is not e->src, it
664 remains unchanged.
665
666 If immediate dominator of e->dest is e->src, it may become
667 ret, provided that all other predecessors of e->dest are
668 dominated by e->dest. */
669
670 if (get_immediate_dominator (CDI_DOMINATORS, single_succ (ret))
671 == single_pred (ret))
672 {
673 edge_iterator ei;
674 FOR_EACH_EDGE (f, ei, single_succ (ret)->preds)
675 {
676 if (f == single_succ_edge (ret))
677 continue;
678
679 if (!dominated_by_p (CDI_DOMINATORS, f->src,
680 single_succ (ret)))
681 break;
682 }
683
684 if (!f)
685 set_immediate_dominator (CDI_DOMINATORS, single_succ (ret), ret);
686 }
687 }
688
689 if (current_loops != NULL)
690 {
691 loop = find_common_loop (src->loop_father, dest->loop_father);
692 add_bb_to_loop (ret, loop);
693
694 /* If we split the latch edge of loop adjust the latch block. */
695 if (loop->latch == src
696 && loop->header == dest)
697 loop->latch = ret;
698 }
699
700 return ret;
701 }
702
703 /* Creates a new basic block just after the basic block AFTER.
704 HEAD and END are the first and the last statement belonging
705 to the block. If both are NULL, an empty block is created. */
706
707 static basic_block
708 create_basic_block_1 (void *head, void *end, basic_block after)
709 {
710 basic_block ret;
711
712 if (!cfg_hooks->create_basic_block)
713 internal_error ("%s does not support create_basic_block", cfg_hooks->name);
714
715 ret = cfg_hooks->create_basic_block (head, end, after);
716
717 if (dom_info_available_p (CDI_DOMINATORS))
718 add_to_dominance_info (CDI_DOMINATORS, ret);
719 if (dom_info_available_p (CDI_POST_DOMINATORS))
720 add_to_dominance_info (CDI_POST_DOMINATORS, ret);
721
722 return ret;
723 }
724
725 basic_block
726 create_basic_block (gimple_seq seq, basic_block after)
727 {
728 return create_basic_block_1 (seq, NULL, after);
729 }
730
731 basic_block
732 create_basic_block (rtx head, rtx end, basic_block after)
733 {
734 return create_basic_block_1 (head, end, after);
735 }
736
737
738 /* Creates an empty basic block just after basic block AFTER. */
739
740 basic_block
741 create_empty_bb (basic_block after)
742 {
743 return create_basic_block_1 (NULL, NULL, after);
744 }
745
746 /* Checks whether we may merge blocks BB1 and BB2. */
747
748 bool
749 can_merge_blocks_p (basic_block bb1, basic_block bb2)
750 {
751 bool ret;
752
753 if (!cfg_hooks->can_merge_blocks_p)
754 internal_error ("%s does not support can_merge_blocks_p", cfg_hooks->name);
755
756 ret = cfg_hooks->can_merge_blocks_p (bb1, bb2);
757
758 return ret;
759 }
760
761 void
762 predict_edge (edge e, enum br_predictor predictor, int probability)
763 {
764 if (!cfg_hooks->predict_edge)
765 internal_error ("%s does not support predict_edge", cfg_hooks->name);
766
767 cfg_hooks->predict_edge (e, predictor, probability);
768 }
769
770 bool
771 predicted_by_p (const_basic_block bb, enum br_predictor predictor)
772 {
773 if (!cfg_hooks->predict_edge)
774 internal_error ("%s does not support predicted_by_p", cfg_hooks->name);
775
776 return cfg_hooks->predicted_by_p (bb, predictor);
777 }
778
779 /* Merges basic block B into basic block A. */
780
781 void
782 merge_blocks (basic_block a, basic_block b)
783 {
784 edge e;
785 edge_iterator ei;
786
787 if (!cfg_hooks->merge_blocks)
788 internal_error ("%s does not support merge_blocks", cfg_hooks->name);
789
790 cfg_hooks->merge_blocks (a, b);
791
792 if (current_loops != NULL)
793 {
794 /* If the block we merge into is a loop header do nothing unless ... */
795 if (a->loop_father->header == a)
796 {
797 /* ... we merge two loop headers, in which case we kill
798 the inner loop. */
799 if (b->loop_father->header == b)
800 mark_loop_for_removal (b->loop_father);
801 }
802 /* If we merge a loop header into its predecessor, update the loop
803 structure. */
804 else if (b->loop_father->header == b)
805 {
806 remove_bb_from_loops (a);
807 add_bb_to_loop (a, b->loop_father);
808 a->loop_father->header = a;
809 }
810 /* If we merge a loop latch into its predecessor, update the loop
811 structure. */
812 if (b->loop_father->latch
813 && b->loop_father->latch == b)
814 b->loop_father->latch = a;
815 remove_bb_from_loops (b);
816 }
817
818 /* Normally there should only be one successor of A and that is B, but
819 partway though the merge of blocks for conditional_execution we'll
820 be merging a TEST block with THEN and ELSE successors. Free the
821 whole lot of them and hope the caller knows what they're doing. */
822
823 while (EDGE_COUNT (a->succs) != 0)
824 remove_edge (EDGE_SUCC (a, 0));
825
826 /* Adjust the edges out of B for the new owner. */
827 FOR_EACH_EDGE (e, ei, b->succs)
828 {
829 e->src = a;
830 if (current_loops != NULL)
831 {
832 /* If b was a latch, a now is. */
833 if (e->dest->loop_father->latch == b)
834 e->dest->loop_father->latch = a;
835 rescan_loop_exit (e, true, false);
836 }
837 }
838 a->succs = b->succs;
839 a->flags |= b->flags;
840
841 /* B hasn't quite yet ceased to exist. Attempt to prevent mishap. */
842 b->preds = b->succs = NULL;
843
844 if (dom_info_available_p (CDI_DOMINATORS))
845 redirect_immediate_dominators (CDI_DOMINATORS, b, a);
846
847 if (dom_info_available_p (CDI_DOMINATORS))
848 delete_from_dominance_info (CDI_DOMINATORS, b);
849 if (dom_info_available_p (CDI_POST_DOMINATORS))
850 delete_from_dominance_info (CDI_POST_DOMINATORS, b);
851
852 expunge_block (b);
853 }
854
855 /* Split BB into entry part and the rest (the rest is the newly created block).
856 Redirect those edges for that REDIRECT_EDGE_P returns true to the entry
857 part. Returns the edge connecting the entry part to the rest. */
858
859 edge
860 make_forwarder_block (basic_block bb, bool (*redirect_edge_p) (edge),
861 void (*new_bb_cbk) (basic_block))
862 {
863 edge e, fallthru;
864 edge_iterator ei;
865 basic_block dummy, jump;
866 struct loop *loop, *ploop, *cloop;
867
868 if (!cfg_hooks->make_forwarder_block)
869 internal_error ("%s does not support make_forwarder_block",
870 cfg_hooks->name);
871
872 fallthru = split_block_after_labels (bb);
873 dummy = fallthru->src;
874 dummy->count = 0;
875 dummy->frequency = 0;
876 fallthru->count = 0;
877 bb = fallthru->dest;
878
879 /* Redirect back edges we want to keep. */
880 for (ei = ei_start (dummy->preds); (e = ei_safe_edge (ei)); )
881 {
882 basic_block e_src;
883
884 if (redirect_edge_p (e))
885 {
886 dummy->frequency += EDGE_FREQUENCY (e);
887 if (dummy->frequency > BB_FREQ_MAX)
888 dummy->frequency = BB_FREQ_MAX;
889
890 dummy->count += e->count;
891 fallthru->count += e->count;
892 ei_next (&ei);
893 continue;
894 }
895
896 e_src = e->src;
897 jump = redirect_edge_and_branch_force (e, bb);
898 if (jump != NULL)
899 {
900 /* If we redirected the loop latch edge, the JUMP block now acts like
901 the new latch of the loop. */
902 if (current_loops != NULL
903 && dummy->loop_father != NULL
904 && dummy->loop_father->header == dummy
905 && dummy->loop_father->latch == e_src)
906 dummy->loop_father->latch = jump;
907
908 if (new_bb_cbk != NULL)
909 new_bb_cbk (jump);
910 }
911 }
912
913 if (dom_info_available_p (CDI_DOMINATORS))
914 {
915 vec<basic_block> doms_to_fix;
916 doms_to_fix.create (2);
917 doms_to_fix.quick_push (dummy);
918 doms_to_fix.quick_push (bb);
919 iterate_fix_dominators (CDI_DOMINATORS, doms_to_fix, false);
920 doms_to_fix.release ();
921 }
922
923 if (current_loops != NULL)
924 {
925 /* If we do not split a loop header, then both blocks belong to the
926 same loop. In case we split loop header and do not redirect the
927 latch edge to DUMMY, then DUMMY belongs to the outer loop, and
928 BB becomes the new header. If latch is not recorded for the loop,
929 we leave this updating on the caller (this may only happen during
930 loop analysis). */
931 loop = dummy->loop_father;
932 if (loop->header == dummy
933 && loop->latch != NULL
934 && find_edge (loop->latch, dummy) == NULL)
935 {
936 remove_bb_from_loops (dummy);
937 loop->header = bb;
938
939 cloop = loop;
940 FOR_EACH_EDGE (e, ei, dummy->preds)
941 {
942 cloop = find_common_loop (cloop, e->src->loop_father);
943 }
944 add_bb_to_loop (dummy, cloop);
945 }
946
947 /* In case we split loop latch, update it. */
948 for (ploop = loop; ploop; ploop = loop_outer (ploop))
949 if (ploop->latch == dummy)
950 ploop->latch = bb;
951 }
952
953 cfg_hooks->make_forwarder_block (fallthru);
954
955 return fallthru;
956 }
957
958 /* Try to make the edge fallthru. */
959
960 void
961 tidy_fallthru_edge (edge e)
962 {
963 if (cfg_hooks->tidy_fallthru_edge)
964 cfg_hooks->tidy_fallthru_edge (e);
965 }
966
967 /* Fix up edges that now fall through, or rather should now fall through
968 but previously required a jump around now deleted blocks. Simplify
969 the search by only examining blocks numerically adjacent, since this
970 is how they were created.
971
972 ??? This routine is currently RTL specific. */
973
974 void
975 tidy_fallthru_edges (void)
976 {
977 basic_block b, c;
978
979 if (!cfg_hooks->tidy_fallthru_edge)
980 return;
981
982 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
983 return;
984
985 FOR_BB_BETWEEN (b, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb,
986 EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb, next_bb)
987 {
988 edge s;
989
990 c = b->next_bb;
991
992 /* We care about simple conditional or unconditional jumps with
993 a single successor.
994
995 If we had a conditional branch to the next instruction when
996 CFG was built, then there will only be one out edge for the
997 block which ended with the conditional branch (since we do
998 not create duplicate edges).
999
1000 Furthermore, the edge will be marked as a fallthru because we
1001 merge the flags for the duplicate edges. So we do not want to
1002 check that the edge is not a FALLTHRU edge. */
1003
1004 if (single_succ_p (b))
1005 {
1006 s = single_succ_edge (b);
1007 if (! (s->flags & EDGE_COMPLEX)
1008 && s->dest == c
1009 && !(JUMP_P (BB_END (b)) && CROSSING_JUMP_P (BB_END (b))))
1010 tidy_fallthru_edge (s);
1011 }
1012 }
1013 }
1014
1015 /* Edge E is assumed to be fallthru edge. Emit needed jump instruction
1016 (and possibly create new basic block) to make edge non-fallthru.
1017 Return newly created BB or NULL if none. */
1018
1019 basic_block
1020 force_nonfallthru (edge e)
1021 {
1022 basic_block ret, src = e->src;
1023
1024 if (!cfg_hooks->force_nonfallthru)
1025 internal_error ("%s does not support force_nonfallthru",
1026 cfg_hooks->name);
1027
1028 ret = cfg_hooks->force_nonfallthru (e);
1029 if (ret != NULL)
1030 {
1031 if (dom_info_available_p (CDI_DOMINATORS))
1032 set_immediate_dominator (CDI_DOMINATORS, ret, src);
1033
1034 if (current_loops != NULL)
1035 {
1036 struct loop *loop
1037 = find_common_loop (single_pred (ret)->loop_father,
1038 single_succ (ret)->loop_father);
1039 rescan_loop_exit (e, false, true);
1040 add_bb_to_loop (ret, loop);
1041 }
1042 }
1043
1044 return ret;
1045 }
1046
1047 /* Returns true if we can duplicate basic block BB. */
1048
1049 bool
1050 can_duplicate_block_p (const_basic_block bb)
1051 {
1052 if (!cfg_hooks->can_duplicate_block_p)
1053 internal_error ("%s does not support can_duplicate_block_p",
1054 cfg_hooks->name);
1055
1056 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun) || bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1057 return false;
1058
1059 return cfg_hooks->can_duplicate_block_p (bb);
1060 }
1061
1062 /* Duplicates basic block BB and redirects edge E to it. Returns the
1063 new basic block. The new basic block is placed after the basic block
1064 AFTER. */
1065
1066 basic_block
1067 duplicate_block (basic_block bb, edge e, basic_block after)
1068 {
1069 edge s, n;
1070 basic_block new_bb;
1071 gcov_type new_count = e ? e->count : 0;
1072 edge_iterator ei;
1073
1074 if (!cfg_hooks->duplicate_block)
1075 internal_error ("%s does not support duplicate_block",
1076 cfg_hooks->name);
1077
1078 if (bb->count < new_count)
1079 new_count = bb->count;
1080
1081 gcc_checking_assert (can_duplicate_block_p (bb));
1082
1083 new_bb = cfg_hooks->duplicate_block (bb);
1084 if (after)
1085 move_block_after (new_bb, after);
1086
1087 new_bb->flags = bb->flags;
1088 FOR_EACH_EDGE (s, ei, bb->succs)
1089 {
1090 /* Since we are creating edges from a new block to successors
1091 of another block (which therefore are known to be disjoint), there
1092 is no need to actually check for duplicated edges. */
1093 n = unchecked_make_edge (new_bb, s->dest, s->flags);
1094 n->probability = s->probability;
1095 if (e && bb->count)
1096 {
1097 /* Take care for overflows! */
1098 n->count = s->count * (new_count * 10000 / bb->count) / 10000;
1099 s->count -= n->count;
1100 }
1101 else
1102 n->count = s->count;
1103 n->aux = s->aux;
1104 }
1105
1106 if (e)
1107 {
1108 new_bb->count = new_count;
1109 bb->count -= new_count;
1110
1111 new_bb->frequency = EDGE_FREQUENCY (e);
1112 bb->frequency -= EDGE_FREQUENCY (e);
1113
1114 redirect_edge_and_branch_force (e, new_bb);
1115
1116 if (bb->count < 0)
1117 bb->count = 0;
1118 if (bb->frequency < 0)
1119 bb->frequency = 0;
1120 }
1121 else
1122 {
1123 new_bb->count = bb->count;
1124 new_bb->frequency = bb->frequency;
1125 }
1126
1127 set_bb_original (new_bb, bb);
1128 set_bb_copy (bb, new_bb);
1129
1130 /* Add the new block to the copy of the loop of BB, or directly to the loop
1131 of BB if the loop is not being copied. */
1132 if (current_loops != NULL)
1133 {
1134 struct loop *cloop = bb->loop_father;
1135 struct loop *copy = get_loop_copy (cloop);
1136 /* If we copied the loop header block but not the loop
1137 we have created a loop with multiple entries. Ditch the loop,
1138 add the new block to the outer loop and arrange for a fixup. */
1139 if (!copy
1140 && cloop->header == bb)
1141 {
1142 add_bb_to_loop (new_bb, loop_outer (cloop));
1143 mark_loop_for_removal (cloop);
1144 }
1145 else
1146 {
1147 add_bb_to_loop (new_bb, copy ? copy : cloop);
1148 /* If we copied the loop latch block but not the loop, adjust
1149 loop state. */
1150 if (!copy
1151 && cloop->latch == bb)
1152 {
1153 cloop->latch = NULL;
1154 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
1155 }
1156 }
1157 }
1158
1159 return new_bb;
1160 }
1161
1162 /* Return 1 if BB ends with a call, possibly followed by some
1163 instructions that must stay with the call, 0 otherwise. */
1164
1165 bool
1166 block_ends_with_call_p (basic_block bb)
1167 {
1168 if (!cfg_hooks->block_ends_with_call_p)
1169 internal_error ("%s does not support block_ends_with_call_p", cfg_hooks->name);
1170
1171 return (cfg_hooks->block_ends_with_call_p) (bb);
1172 }
1173
1174 /* Return 1 if BB ends with a conditional branch, 0 otherwise. */
1175
1176 bool
1177 block_ends_with_condjump_p (const_basic_block bb)
1178 {
1179 if (!cfg_hooks->block_ends_with_condjump_p)
1180 internal_error ("%s does not support block_ends_with_condjump_p",
1181 cfg_hooks->name);
1182
1183 return (cfg_hooks->block_ends_with_condjump_p) (bb);
1184 }
1185
1186 /* Add fake edges to the function exit for any non constant and non noreturn
1187 calls, volatile inline assembly in the bitmap of blocks specified by
1188 BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
1189 that were split.
1190
1191 The goal is to expose cases in which entering a basic block does not imply
1192 that all subsequent instructions must be executed. */
1193
1194 int
1195 flow_call_edges_add (sbitmap blocks)
1196 {
1197 if (!cfg_hooks->flow_call_edges_add)
1198 internal_error ("%s does not support flow_call_edges_add",
1199 cfg_hooks->name);
1200
1201 return (cfg_hooks->flow_call_edges_add) (blocks);
1202 }
1203
1204 /* This function is called immediately after edge E is added to the
1205 edge vector E->dest->preds. */
1206
1207 void
1208 execute_on_growing_pred (edge e)
1209 {
1210 if (cfg_hooks->execute_on_growing_pred)
1211 cfg_hooks->execute_on_growing_pred (e);
1212 }
1213
1214 /* This function is called immediately before edge E is removed from
1215 the edge vector E->dest->preds. */
1216
1217 void
1218 execute_on_shrinking_pred (edge e)
1219 {
1220 if (cfg_hooks->execute_on_shrinking_pred)
1221 cfg_hooks->execute_on_shrinking_pred (e);
1222 }
1223
1224 /* This is used inside loop versioning when we want to insert
1225 stmts/insns on the edges, which have a different behavior
1226 in tree's and in RTL, so we made a CFG hook. */
1227 void
1228 lv_flush_pending_stmts (edge e)
1229 {
1230 if (cfg_hooks->flush_pending_stmts)
1231 cfg_hooks->flush_pending_stmts (e);
1232 }
1233
1234 /* Loop versioning uses the duplicate_loop_to_header_edge to create
1235 a new version of the loop basic-blocks, the parameters here are
1236 exactly the same as in duplicate_loop_to_header_edge or
1237 tree_duplicate_loop_to_header_edge; while in tree-ssa there is
1238 additional work to maintain ssa information that's why there is
1239 a need to call the tree_duplicate_loop_to_header_edge rather
1240 than duplicate_loop_to_header_edge when we are in tree mode. */
1241 bool
1242 cfg_hook_duplicate_loop_to_header_edge (struct loop *loop, edge e,
1243 unsigned int ndupl,
1244 sbitmap wont_exit, edge orig,
1245 vec<edge> *to_remove,
1246 int flags)
1247 {
1248 gcc_assert (cfg_hooks->cfg_hook_duplicate_loop_to_header_edge);
1249 return cfg_hooks->cfg_hook_duplicate_loop_to_header_edge (loop, e,
1250 ndupl, wont_exit,
1251 orig, to_remove,
1252 flags);
1253 }
1254
1255 /* Conditional jumps are represented differently in trees and RTL,
1256 this hook takes a basic block that is known to have a cond jump
1257 at its end and extracts the taken and not taken edges out of it
1258 and store it in E1 and E2 respectively. */
1259 void
1260 extract_cond_bb_edges (basic_block b, edge *e1, edge *e2)
1261 {
1262 gcc_assert (cfg_hooks->extract_cond_bb_edges);
1263 cfg_hooks->extract_cond_bb_edges (b, e1, e2);
1264 }
1265
1266 /* Responsible for updating the ssa info (PHI nodes) on the
1267 new condition basic block that guards the versioned loop. */
1268 void
1269 lv_adjust_loop_header_phi (basic_block first, basic_block second,
1270 basic_block new_block, edge e)
1271 {
1272 if (cfg_hooks->lv_adjust_loop_header_phi)
1273 cfg_hooks->lv_adjust_loop_header_phi (first, second, new_block, e);
1274 }
1275
1276 /* Conditions in trees and RTL are different so we need
1277 a different handling when we add the condition to the
1278 versioning code. */
1279 void
1280 lv_add_condition_to_bb (basic_block first, basic_block second,
1281 basic_block new_block, void *cond)
1282 {
1283 gcc_assert (cfg_hooks->lv_add_condition_to_bb);
1284 cfg_hooks->lv_add_condition_to_bb (first, second, new_block, cond);
1285 }
1286
1287 /* Checks whether all N blocks in BBS array can be copied. */
1288 bool
1289 can_copy_bbs_p (basic_block *bbs, unsigned n)
1290 {
1291 unsigned i;
1292 edge e;
1293 int ret = true;
1294
1295 for (i = 0; i < n; i++)
1296 bbs[i]->flags |= BB_DUPLICATED;
1297
1298 for (i = 0; i < n; i++)
1299 {
1300 /* In case we should redirect abnormal edge during duplication, fail. */
1301 edge_iterator ei;
1302 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
1303 if ((e->flags & EDGE_ABNORMAL)
1304 && (e->dest->flags & BB_DUPLICATED))
1305 {
1306 ret = false;
1307 goto end;
1308 }
1309
1310 if (!can_duplicate_block_p (bbs[i]))
1311 {
1312 ret = false;
1313 break;
1314 }
1315 }
1316
1317 end:
1318 for (i = 0; i < n; i++)
1319 bbs[i]->flags &= ~BB_DUPLICATED;
1320
1321 return ret;
1322 }
1323
1324 /* Duplicates N basic blocks stored in array BBS. Newly created basic blocks
1325 are placed into array NEW_BBS in the same order. Edges from basic blocks
1326 in BBS are also duplicated and copies of those that lead into BBS are
1327 redirected to appropriate newly created block. The function assigns bbs
1328 into loops (copy of basic block bb is assigned to bb->loop_father->copy
1329 loop, so this must be set up correctly in advance)
1330
1331 If UPDATE_DOMINANCE is true then this function updates dominators locally
1332 (LOOPS structure that contains the information about dominators is passed
1333 to enable this), otherwise it does not update the dominator information
1334 and it assumed that the caller will do this, perhaps by destroying and
1335 recreating it instead of trying to do an incremental update like this
1336 function does when update_dominance is true.
1337
1338 BASE is the superloop to that basic block belongs; if its header or latch
1339 is copied, we do not set the new blocks as header or latch.
1340
1341 Created copies of N_EDGES edges in array EDGES are stored in array NEW_EDGES,
1342 also in the same order.
1343
1344 Newly created basic blocks are put after the basic block AFTER in the
1345 instruction stream, and the order of the blocks in BBS array is preserved. */
1346
1347 void
1348 copy_bbs (basic_block *bbs, unsigned n, basic_block *new_bbs,
1349 edge *edges, unsigned num_edges, edge *new_edges,
1350 struct loop *base, basic_block after, bool update_dominance)
1351 {
1352 unsigned i, j;
1353 basic_block bb, new_bb, dom_bb;
1354 edge e;
1355
1356 /* Duplicate bbs, update dominators, assign bbs to loops. */
1357 for (i = 0; i < n; i++)
1358 {
1359 /* Duplicate. */
1360 bb = bbs[i];
1361 new_bb = new_bbs[i] = duplicate_block (bb, NULL, after);
1362 after = new_bb;
1363 bb->flags |= BB_DUPLICATED;
1364 if (bb->loop_father)
1365 {
1366 /* Possibly set loop header. */
1367 if (bb->loop_father->header == bb && bb->loop_father != base)
1368 new_bb->loop_father->header = new_bb;
1369 /* Or latch. */
1370 if (bb->loop_father->latch == bb && bb->loop_father != base)
1371 new_bb->loop_father->latch = new_bb;
1372 }
1373 }
1374
1375 /* Set dominators. */
1376 if (update_dominance)
1377 {
1378 for (i = 0; i < n; i++)
1379 {
1380 bb = bbs[i];
1381 new_bb = new_bbs[i];
1382
1383 dom_bb = get_immediate_dominator (CDI_DOMINATORS, bb);
1384 if (dom_bb->flags & BB_DUPLICATED)
1385 {
1386 dom_bb = get_bb_copy (dom_bb);
1387 set_immediate_dominator (CDI_DOMINATORS, new_bb, dom_bb);
1388 }
1389 }
1390 }
1391
1392 /* Redirect edges. */
1393 for (j = 0; j < num_edges; j++)
1394 new_edges[j] = NULL;
1395 for (i = 0; i < n; i++)
1396 {
1397 edge_iterator ei;
1398 new_bb = new_bbs[i];
1399 bb = bbs[i];
1400
1401 FOR_EACH_EDGE (e, ei, new_bb->succs)
1402 {
1403 for (j = 0; j < num_edges; j++)
1404 if (edges[j] && edges[j]->src == bb && edges[j]->dest == e->dest)
1405 new_edges[j] = e;
1406
1407 if (!(e->dest->flags & BB_DUPLICATED))
1408 continue;
1409 redirect_edge_and_branch_force (e, get_bb_copy (e->dest));
1410 }
1411 }
1412
1413 /* Clear information about duplicates. */
1414 for (i = 0; i < n; i++)
1415 bbs[i]->flags &= ~BB_DUPLICATED;
1416 }
1417
1418 /* Return true if BB contains only labels or non-executable
1419 instructions */
1420 bool
1421 empty_block_p (basic_block bb)
1422 {
1423 gcc_assert (cfg_hooks->empty_block_p);
1424 return cfg_hooks->empty_block_p (bb);
1425 }
1426
1427 /* Split a basic block if it ends with a conditional branch and if
1428 the other part of the block is not empty. */
1429 basic_block
1430 split_block_before_cond_jump (basic_block bb)
1431 {
1432 gcc_assert (cfg_hooks->split_block_before_cond_jump);
1433 return cfg_hooks->split_block_before_cond_jump (bb);
1434 }
1435
1436 /* Work-horse for passes.c:check_profile_consistency.
1437 Do book-keeping of the CFG for the profile consistency checker.
1438 If AFTER_PASS is 0, do pre-pass accounting, or if AFTER_PASS is 1
1439 then do post-pass accounting. Store the counting in RECORD. */
1440
1441 void
1442 account_profile_record (struct profile_record *record, int after_pass)
1443 {
1444 basic_block bb;
1445 edge_iterator ei;
1446 edge e;
1447 int sum;
1448 gcov_type lsum;
1449
1450 FOR_ALL_BB_FN (bb, cfun)
1451 {
1452 if (bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
1453 && profile_status_for_fn (cfun) != PROFILE_ABSENT)
1454 {
1455 sum = 0;
1456 FOR_EACH_EDGE (e, ei, bb->succs)
1457 sum += e->probability;
1458 if (EDGE_COUNT (bb->succs) && abs (sum - REG_BR_PROB_BASE) > 100)
1459 record->num_mismatched_freq_out[after_pass]++;
1460 lsum = 0;
1461 FOR_EACH_EDGE (e, ei, bb->succs)
1462 lsum += e->count;
1463 if (EDGE_COUNT (bb->succs)
1464 && (lsum - bb->count > 100 || lsum - bb->count < -100))
1465 record->num_mismatched_count_out[after_pass]++;
1466 }
1467 if (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
1468 && profile_status_for_fn (cfun) != PROFILE_ABSENT)
1469 {
1470 sum = 0;
1471 FOR_EACH_EDGE (e, ei, bb->preds)
1472 sum += EDGE_FREQUENCY (e);
1473 if (abs (sum - bb->frequency) > 100
1474 || (MAX (sum, bb->frequency) > 10
1475 && abs ((sum - bb->frequency) * 100 / (MAX (sum, bb->frequency) + 1)) > 10))
1476 record->num_mismatched_freq_in[after_pass]++;
1477 lsum = 0;
1478 FOR_EACH_EDGE (e, ei, bb->preds)
1479 lsum += e->count;
1480 if (lsum - bb->count > 100 || lsum - bb->count < -100)
1481 record->num_mismatched_count_in[after_pass]++;
1482 }
1483 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)
1484 || bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1485 continue;
1486 gcc_assert (cfg_hooks->account_profile_record);
1487 cfg_hooks->account_profile_record (bb, after_pass, record);
1488 }
1489 }