PR libgcc/63832
[official-gcc.git] / gcc / cfghooks.c
bloba0cdbfc02daf918a2af666bcf8defecef2851a1b
1 /* Hooks for cfg representation specific functions.
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <s.pop@laposte.net>
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "dumpfile.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "predict.h"
29 #include "vec.h"
30 #include "hashtab.h"
31 #include "hash-set.h"
32 #include "machmode.h"
33 #include "hard-reg-set.h"
34 #include "input.h"
35 #include "function.h"
36 #include "dominance.h"
37 #include "cfg.h"
38 #include "cfganal.h"
39 #include "basic-block.h"
40 #include "tree-ssa.h"
41 #include "timevar.h"
42 #include "diagnostic-core.h"
43 #include "cfgloop.h"
44 #include "pretty-print.h"
46 /* A pointer to one of the hooks containers. */
47 static struct cfg_hooks *cfg_hooks;
49 /* Initialization of functions specific to the rtl IR. */
50 void
51 rtl_register_cfg_hooks (void)
53 cfg_hooks = &rtl_cfg_hooks;
56 /* Initialization of functions specific to the rtl IR. */
57 void
58 cfg_layout_rtl_register_cfg_hooks (void)
60 cfg_hooks = &cfg_layout_rtl_cfg_hooks;
63 /* Initialization of functions specific to the tree IR. */
65 void
66 gimple_register_cfg_hooks (void)
68 cfg_hooks = &gimple_cfg_hooks;
71 struct cfg_hooks
72 get_cfg_hooks (void)
74 return *cfg_hooks;
77 void
78 set_cfg_hooks (struct cfg_hooks new_cfg_hooks)
80 *cfg_hooks = new_cfg_hooks;
83 /* Returns current ir type. */
85 enum ir_type
86 current_ir_type (void)
88 if (cfg_hooks == &gimple_cfg_hooks)
89 return IR_GIMPLE;
90 else if (cfg_hooks == &rtl_cfg_hooks)
91 return IR_RTL_CFGRTL;
92 else if (cfg_hooks == &cfg_layout_rtl_cfg_hooks)
93 return IR_RTL_CFGLAYOUT;
94 else
95 gcc_unreachable ();
98 /* Verify the CFG consistency.
100 Currently it does following: checks edge and basic block list correctness
101 and calls into IL dependent checking then. */
103 DEBUG_FUNCTION void
104 verify_flow_info (void)
106 size_t *edge_checksum;
107 int err = 0;
108 basic_block bb, last_bb_seen;
109 basic_block *last_visited;
111 timevar_push (TV_CFG_VERIFY);
112 last_visited = XCNEWVEC (basic_block, last_basic_block_for_fn (cfun));
113 edge_checksum = XCNEWVEC (size_t, last_basic_block_for_fn (cfun));
115 /* Check bb chain & numbers. */
116 last_bb_seen = ENTRY_BLOCK_PTR_FOR_FN (cfun);
117 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb, NULL, next_bb)
119 if (bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
120 && bb != BASIC_BLOCK_FOR_FN (cfun, bb->index))
122 error ("bb %d on wrong place", bb->index);
123 err = 1;
126 if (bb->prev_bb != last_bb_seen)
128 error ("prev_bb of %d should be %d, not %d",
129 bb->index, last_bb_seen->index, bb->prev_bb->index);
130 err = 1;
133 last_bb_seen = bb;
136 /* Now check the basic blocks (boundaries etc.) */
137 FOR_EACH_BB_REVERSE_FN (bb, cfun)
139 int n_fallthru = 0;
140 edge e;
141 edge_iterator ei;
143 if (bb->loop_father != NULL && current_loops == NULL)
145 error ("verify_flow_info: Block %i has loop_father, but there are no loops",
146 bb->index);
147 err = 1;
149 if (bb->loop_father == NULL && current_loops != NULL)
151 error ("verify_flow_info: Block %i lacks loop_father", bb->index);
152 err = 1;
155 if (bb->count < 0)
157 error ("verify_flow_info: Wrong count of block %i %i",
158 bb->index, (int)bb->count);
159 err = 1;
161 if (bb->frequency < 0)
163 error ("verify_flow_info: Wrong frequency of block %i %i",
164 bb->index, bb->frequency);
165 err = 1;
167 FOR_EACH_EDGE (e, ei, bb->succs)
169 if (last_visited [e->dest->index] == bb)
171 error ("verify_flow_info: Duplicate edge %i->%i",
172 e->src->index, e->dest->index);
173 err = 1;
175 if (e->probability < 0 || e->probability > REG_BR_PROB_BASE)
177 error ("verify_flow_info: Wrong probability of edge %i->%i %i",
178 e->src->index, e->dest->index, e->probability);
179 err = 1;
181 if (e->count < 0)
183 error ("verify_flow_info: Wrong count of edge %i->%i %i",
184 e->src->index, e->dest->index, (int)e->count);
185 err = 1;
188 last_visited [e->dest->index] = bb;
190 if (e->flags & EDGE_FALLTHRU)
191 n_fallthru++;
193 if (e->src != bb)
195 error ("verify_flow_info: Basic block %d succ edge is corrupted",
196 bb->index);
197 fprintf (stderr, "Predecessor: ");
198 dump_edge_info (stderr, e, TDF_DETAILS, 0);
199 fprintf (stderr, "\nSuccessor: ");
200 dump_edge_info (stderr, e, TDF_DETAILS, 1);
201 fprintf (stderr, "\n");
202 err = 1;
205 edge_checksum[e->dest->index] += (size_t) e;
207 if (n_fallthru > 1)
209 error ("wrong amount of branch edges after unconditional jump %i", bb->index);
210 err = 1;
213 FOR_EACH_EDGE (e, ei, bb->preds)
215 if (e->dest != bb)
217 error ("basic block %d pred edge is corrupted", bb->index);
218 fputs ("Predecessor: ", stderr);
219 dump_edge_info (stderr, e, TDF_DETAILS, 0);
220 fputs ("\nSuccessor: ", stderr);
221 dump_edge_info (stderr, e, TDF_DETAILS, 1);
222 fputc ('\n', stderr);
223 err = 1;
226 if (ei.index != e->dest_idx)
228 error ("basic block %d pred edge is corrupted", bb->index);
229 error ("its dest_idx should be %d, not %d",
230 ei.index, e->dest_idx);
231 fputs ("Predecessor: ", stderr);
232 dump_edge_info (stderr, e, TDF_DETAILS, 0);
233 fputs ("\nSuccessor: ", stderr);
234 dump_edge_info (stderr, e, TDF_DETAILS, 1);
235 fputc ('\n', stderr);
236 err = 1;
239 edge_checksum[e->dest->index] -= (size_t) e;
243 /* Complete edge checksumming for ENTRY and EXIT. */
245 edge e;
246 edge_iterator ei;
248 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
249 edge_checksum[e->dest->index] += (size_t) e;
251 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
252 edge_checksum[e->dest->index] -= (size_t) e;
255 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
256 if (edge_checksum[bb->index])
258 error ("basic block %i edge lists are corrupted", bb->index);
259 err = 1;
262 last_bb_seen = ENTRY_BLOCK_PTR_FOR_FN (cfun);
264 /* Clean up. */
265 free (last_visited);
266 free (edge_checksum);
268 if (cfg_hooks->verify_flow_info)
269 err |= cfg_hooks->verify_flow_info ();
270 if (err)
271 internal_error ("verify_flow_info failed");
272 timevar_pop (TV_CFG_VERIFY);
275 /* Print out one basic block BB to file OUTF. INDENT is printed at the
276 start of each new line. FLAGS are the TDF_* flags in dumpfile.h.
278 This function takes care of the purely graph related information.
279 The cfg hook for the active representation should dump
280 representation-specific information. */
282 void
283 dump_bb (FILE *outf, basic_block bb, int indent, int flags)
285 if (flags & TDF_BLOCKS)
286 dump_bb_info (outf, bb, indent, flags, true, false);
287 if (cfg_hooks->dump_bb)
288 cfg_hooks->dump_bb (outf, bb, indent, flags);
289 if (flags & TDF_BLOCKS)
290 dump_bb_info (outf, bb, indent, flags, false, true);
291 fputc ('\n', outf);
294 DEBUG_FUNCTION void
295 debug (basic_block_def &ref)
297 dump_bb (stderr, &ref, 0, 0);
300 DEBUG_FUNCTION void
301 debug (basic_block_def *ptr)
303 if (ptr)
304 debug (*ptr);
305 else
306 fprintf (stderr, "<nil>\n");
310 /* Dumps basic block BB to pretty-printer PP, for use as a label of
311 a DOT graph record-node. The implementation of this hook is
312 expected to write the label to the stream that is attached to PP.
313 Field separators between instructions are pipe characters printed
314 verbatim. Instructions should be written with some characters
315 escaped, using pp_write_text_as_dot_label_to_stream(). */
317 void
318 dump_bb_for_graph (pretty_printer *pp, basic_block bb)
320 if (!cfg_hooks->dump_bb_for_graph)
321 internal_error ("%s does not support dump_bb_for_graph",
322 cfg_hooks->name);
323 if (bb->count)
324 pp_printf (pp, "COUNT:" "%"PRId64, bb->count);
325 pp_printf (pp, " FREQ:%i |", bb->frequency);
326 pp_write_text_to_stream (pp);
327 if (!(dump_flags & TDF_SLIM))
328 cfg_hooks->dump_bb_for_graph (pp, bb);
331 /* Dump the complete CFG to FILE. FLAGS are the TDF_* flags in dumpfile.h. */
332 void
333 dump_flow_info (FILE *file, int flags)
335 basic_block bb;
337 fprintf (file, "\n%d basic blocks, %d edges.\n", n_basic_blocks_for_fn (cfun),
338 n_edges_for_fn (cfun));
339 FOR_ALL_BB_FN (bb, cfun)
340 dump_bb (file, bb, 0, flags);
342 putc ('\n', file);
345 /* Like above, but dump to stderr. To be called from debuggers. */
346 void debug_flow_info (void);
347 DEBUG_FUNCTION void
348 debug_flow_info (void)
350 dump_flow_info (stderr, TDF_DETAILS);
353 /* Redirect edge E to the given basic block DEST and update underlying program
354 representation. Returns edge representing redirected branch (that may not
355 be equivalent to E in the case of duplicate edges being removed) or NULL
356 if edge is not easily redirectable for whatever reason. */
358 edge
359 redirect_edge_and_branch (edge e, basic_block dest)
361 edge ret;
363 if (!cfg_hooks->redirect_edge_and_branch)
364 internal_error ("%s does not support redirect_edge_and_branch",
365 cfg_hooks->name);
367 ret = cfg_hooks->redirect_edge_and_branch (e, dest);
369 /* If RET != E, then either the redirection failed, or the edge E
370 was removed since RET already lead to the same destination. */
371 if (current_loops != NULL && ret == e)
372 rescan_loop_exit (e, false, false);
374 return ret;
377 /* Returns true if it is possible to remove the edge E by redirecting it
378 to the destination of the other edge going from its source. */
380 bool
381 can_remove_branch_p (const_edge e)
383 if (!cfg_hooks->can_remove_branch_p)
384 internal_error ("%s does not support can_remove_branch_p",
385 cfg_hooks->name);
387 if (EDGE_COUNT (e->src->succs) != 2)
388 return false;
390 return cfg_hooks->can_remove_branch_p (e);
393 /* Removes E, by redirecting it to the destination of the other edge going
394 from its source. Can_remove_branch_p must be true for E, hence this
395 operation cannot fail. */
397 void
398 remove_branch (edge e)
400 edge other;
401 basic_block src = e->src;
402 int irr;
404 gcc_assert (EDGE_COUNT (e->src->succs) == 2);
406 other = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e);
407 irr = other->flags & EDGE_IRREDUCIBLE_LOOP;
409 e = redirect_edge_and_branch (e, other->dest);
410 gcc_assert (e != NULL);
412 e->flags &= ~EDGE_IRREDUCIBLE_LOOP;
413 e->flags |= irr;
416 /* Removes edge E from cfg. Unlike remove_branch, it does not update IL. */
418 void
419 remove_edge (edge e)
421 if (current_loops != NULL)
422 rescan_loop_exit (e, false, true);
424 /* This is probably not needed, but it doesn't hurt. */
425 /* FIXME: This should be called via a remove_edge hook. */
426 if (current_ir_type () == IR_GIMPLE)
427 redirect_edge_var_map_clear (e);
429 remove_edge_raw (e);
432 /* Like redirect_edge_succ but avoid possible duplicate edge. */
434 edge
435 redirect_edge_succ_nodup (edge e, basic_block new_succ)
437 edge s;
439 s = find_edge (e->src, new_succ);
440 if (s && s != e)
442 s->flags |= e->flags;
443 s->probability += e->probability;
444 if (s->probability > REG_BR_PROB_BASE)
445 s->probability = REG_BR_PROB_BASE;
446 s->count += e->count;
447 /* FIXME: This should be called via a hook and only for IR_GIMPLE. */
448 redirect_edge_var_map_dup (s, e);
449 remove_edge (e);
450 e = s;
452 else
453 redirect_edge_succ (e, new_succ);
455 return e;
458 /* Redirect the edge E to basic block DEST even if it requires creating
459 of a new basic block; then it returns the newly created basic block.
460 Aborts when redirection is impossible. */
462 basic_block
463 redirect_edge_and_branch_force (edge e, basic_block dest)
465 basic_block ret, src = e->src;
467 if (!cfg_hooks->redirect_edge_and_branch_force)
468 internal_error ("%s does not support redirect_edge_and_branch_force",
469 cfg_hooks->name);
471 if (current_loops != NULL)
472 rescan_loop_exit (e, false, true);
474 ret = cfg_hooks->redirect_edge_and_branch_force (e, dest);
476 if (ret != NULL && dom_info_available_p (CDI_DOMINATORS))
477 set_immediate_dominator (CDI_DOMINATORS, ret, src);
479 if (current_loops != NULL)
481 if (ret != NULL)
483 struct loop *loop
484 = find_common_loop (single_pred (ret)->loop_father,
485 single_succ (ret)->loop_father);
486 add_bb_to_loop (ret, loop);
488 else if (find_edge (src, dest) == e)
489 rescan_loop_exit (e, true, false);
492 return ret;
495 /* Splits basic block BB after the specified instruction I (but at least after
496 the labels). If I is NULL, splits just after labels. The newly created edge
497 is returned. The new basic block is created just after the old one. */
499 edge
500 split_block (basic_block bb, void *i)
502 basic_block new_bb;
503 edge res;
505 if (!cfg_hooks->split_block)
506 internal_error ("%s does not support split_block", cfg_hooks->name);
508 new_bb = cfg_hooks->split_block (bb, i);
509 if (!new_bb)
510 return NULL;
512 new_bb->count = bb->count;
513 new_bb->frequency = bb->frequency;
514 new_bb->discriminator = bb->discriminator;
516 if (dom_info_available_p (CDI_DOMINATORS))
518 redirect_immediate_dominators (CDI_DOMINATORS, bb, new_bb);
519 set_immediate_dominator (CDI_DOMINATORS, new_bb, bb);
522 if (current_loops != NULL)
524 edge_iterator ei;
525 edge e;
526 add_bb_to_loop (new_bb, bb->loop_father);
527 /* Identify all loops bb may have been the latch of and adjust them. */
528 FOR_EACH_EDGE (e, ei, new_bb->succs)
529 if (e->dest->loop_father->latch == bb)
530 e->dest->loop_father->latch = new_bb;
533 res = make_single_succ_edge (bb, new_bb, EDGE_FALLTHRU);
535 if (bb->flags & BB_IRREDUCIBLE_LOOP)
537 new_bb->flags |= BB_IRREDUCIBLE_LOOP;
538 res->flags |= EDGE_IRREDUCIBLE_LOOP;
541 return res;
544 /* Splits block BB just after labels. The newly created edge is returned. */
546 edge
547 split_block_after_labels (basic_block bb)
549 return split_block (bb, NULL);
552 /* Moves block BB immediately after block AFTER. Returns false if the
553 movement was impossible. */
555 bool
556 move_block_after (basic_block bb, basic_block after)
558 bool ret;
560 if (!cfg_hooks->move_block_after)
561 internal_error ("%s does not support move_block_after", cfg_hooks->name);
563 ret = cfg_hooks->move_block_after (bb, after);
565 return ret;
568 /* Deletes the basic block BB. */
570 void
571 delete_basic_block (basic_block bb)
573 if (!cfg_hooks->delete_basic_block)
574 internal_error ("%s does not support delete_basic_block", cfg_hooks->name);
576 cfg_hooks->delete_basic_block (bb);
578 if (current_loops != NULL)
580 struct loop *loop = bb->loop_father;
582 /* If we remove the header or the latch of a loop, mark the loop for
583 removal. */
584 if (loop->latch == bb
585 || loop->header == bb)
586 mark_loop_for_removal (loop);
588 remove_bb_from_loops (bb);
591 /* Remove the edges into and out of this block. Note that there may
592 indeed be edges in, if we are removing an unreachable loop. */
593 while (EDGE_COUNT (bb->preds) != 0)
594 remove_edge (EDGE_PRED (bb, 0));
595 while (EDGE_COUNT (bb->succs) != 0)
596 remove_edge (EDGE_SUCC (bb, 0));
598 if (dom_info_available_p (CDI_DOMINATORS))
599 delete_from_dominance_info (CDI_DOMINATORS, bb);
600 if (dom_info_available_p (CDI_POST_DOMINATORS))
601 delete_from_dominance_info (CDI_POST_DOMINATORS, bb);
603 /* Remove the basic block from the array. */
604 expunge_block (bb);
607 /* Splits edge E and returns the newly created basic block. */
609 basic_block
610 split_edge (edge e)
612 basic_block ret;
613 gcov_type count = e->count;
614 int freq = EDGE_FREQUENCY (e);
615 edge f;
616 bool irr = (e->flags & EDGE_IRREDUCIBLE_LOOP) != 0;
617 struct loop *loop;
618 basic_block src = e->src, dest = e->dest;
620 if (!cfg_hooks->split_edge)
621 internal_error ("%s does not support split_edge", cfg_hooks->name);
623 if (current_loops != NULL)
624 rescan_loop_exit (e, false, true);
626 ret = cfg_hooks->split_edge (e);
627 ret->count = count;
628 ret->frequency = freq;
629 single_succ_edge (ret)->probability = REG_BR_PROB_BASE;
630 single_succ_edge (ret)->count = count;
632 if (irr)
634 ret->flags |= BB_IRREDUCIBLE_LOOP;
635 single_pred_edge (ret)->flags |= EDGE_IRREDUCIBLE_LOOP;
636 single_succ_edge (ret)->flags |= EDGE_IRREDUCIBLE_LOOP;
639 if (dom_info_available_p (CDI_DOMINATORS))
640 set_immediate_dominator (CDI_DOMINATORS, ret, single_pred (ret));
642 if (dom_info_state (CDI_DOMINATORS) >= DOM_NO_FAST_QUERY)
644 /* There are two cases:
646 If the immediate dominator of e->dest is not e->src, it
647 remains unchanged.
649 If immediate dominator of e->dest is e->src, it may become
650 ret, provided that all other predecessors of e->dest are
651 dominated by e->dest. */
653 if (get_immediate_dominator (CDI_DOMINATORS, single_succ (ret))
654 == single_pred (ret))
656 edge_iterator ei;
657 FOR_EACH_EDGE (f, ei, single_succ (ret)->preds)
659 if (f == single_succ_edge (ret))
660 continue;
662 if (!dominated_by_p (CDI_DOMINATORS, f->src,
663 single_succ (ret)))
664 break;
667 if (!f)
668 set_immediate_dominator (CDI_DOMINATORS, single_succ (ret), ret);
672 if (current_loops != NULL)
674 loop = find_common_loop (src->loop_father, dest->loop_father);
675 add_bb_to_loop (ret, loop);
677 /* If we split the latch edge of loop adjust the latch block. */
678 if (loop->latch == src
679 && loop->header == dest)
680 loop->latch = ret;
683 return ret;
686 /* Creates a new basic block just after the basic block AFTER.
687 HEAD and END are the first and the last statement belonging
688 to the block. If both are NULL, an empty block is created. */
690 basic_block
691 create_basic_block (void *head, void *end, basic_block after)
693 basic_block ret;
695 if (!cfg_hooks->create_basic_block)
696 internal_error ("%s does not support create_basic_block", cfg_hooks->name);
698 ret = cfg_hooks->create_basic_block (head, end, after);
700 if (dom_info_available_p (CDI_DOMINATORS))
701 add_to_dominance_info (CDI_DOMINATORS, ret);
702 if (dom_info_available_p (CDI_POST_DOMINATORS))
703 add_to_dominance_info (CDI_POST_DOMINATORS, ret);
705 return ret;
708 /* Creates an empty basic block just after basic block AFTER. */
710 basic_block
711 create_empty_bb (basic_block after)
713 return create_basic_block (NULL, NULL, after);
716 /* Checks whether we may merge blocks BB1 and BB2. */
718 bool
719 can_merge_blocks_p (basic_block bb1, basic_block bb2)
721 bool ret;
723 if (!cfg_hooks->can_merge_blocks_p)
724 internal_error ("%s does not support can_merge_blocks_p", cfg_hooks->name);
726 ret = cfg_hooks->can_merge_blocks_p (bb1, bb2);
728 return ret;
731 void
732 predict_edge (edge e, enum br_predictor predictor, int probability)
734 if (!cfg_hooks->predict_edge)
735 internal_error ("%s does not support predict_edge", cfg_hooks->name);
737 cfg_hooks->predict_edge (e, predictor, probability);
740 bool
741 predicted_by_p (const_basic_block bb, enum br_predictor predictor)
743 if (!cfg_hooks->predict_edge)
744 internal_error ("%s does not support predicted_by_p", cfg_hooks->name);
746 return cfg_hooks->predicted_by_p (bb, predictor);
749 /* Merges basic block B into basic block A. */
751 void
752 merge_blocks (basic_block a, basic_block b)
754 edge e;
755 edge_iterator ei;
757 if (!cfg_hooks->merge_blocks)
758 internal_error ("%s does not support merge_blocks", cfg_hooks->name);
760 cfg_hooks->merge_blocks (a, b);
762 if (current_loops != NULL)
764 /* If the block we merge into is a loop header do nothing unless ... */
765 if (a->loop_father->header == a)
767 /* ... we merge two loop headers, in which case we kill
768 the inner loop. */
769 if (b->loop_father->header == b)
770 mark_loop_for_removal (b->loop_father);
772 /* If we merge a loop header into its predecessor, update the loop
773 structure. */
774 else if (b->loop_father->header == b)
776 remove_bb_from_loops (a);
777 add_bb_to_loop (a, b->loop_father);
778 a->loop_father->header = a;
780 /* If we merge a loop latch into its predecessor, update the loop
781 structure. */
782 if (b->loop_father->latch
783 && b->loop_father->latch == b)
784 b->loop_father->latch = a;
785 remove_bb_from_loops (b);
788 /* Normally there should only be one successor of A and that is B, but
789 partway though the merge of blocks for conditional_execution we'll
790 be merging a TEST block with THEN and ELSE successors. Free the
791 whole lot of them and hope the caller knows what they're doing. */
793 while (EDGE_COUNT (a->succs) != 0)
794 remove_edge (EDGE_SUCC (a, 0));
796 /* Adjust the edges out of B for the new owner. */
797 FOR_EACH_EDGE (e, ei, b->succs)
799 e->src = a;
800 if (current_loops != NULL)
802 /* If b was a latch, a now is. */
803 if (e->dest->loop_father->latch == b)
804 e->dest->loop_father->latch = a;
805 rescan_loop_exit (e, true, false);
808 a->succs = b->succs;
809 a->flags |= b->flags;
811 /* B hasn't quite yet ceased to exist. Attempt to prevent mishap. */
812 b->preds = b->succs = NULL;
814 if (dom_info_available_p (CDI_DOMINATORS))
815 redirect_immediate_dominators (CDI_DOMINATORS, b, a);
817 if (dom_info_available_p (CDI_DOMINATORS))
818 delete_from_dominance_info (CDI_DOMINATORS, b);
819 if (dom_info_available_p (CDI_POST_DOMINATORS))
820 delete_from_dominance_info (CDI_POST_DOMINATORS, b);
822 expunge_block (b);
825 /* Split BB into entry part and the rest (the rest is the newly created block).
826 Redirect those edges for that REDIRECT_EDGE_P returns true to the entry
827 part. Returns the edge connecting the entry part to the rest. */
829 edge
830 make_forwarder_block (basic_block bb, bool (*redirect_edge_p) (edge),
831 void (*new_bb_cbk) (basic_block))
833 edge e, fallthru;
834 edge_iterator ei;
835 basic_block dummy, jump;
836 struct loop *loop, *ploop, *cloop;
838 if (!cfg_hooks->make_forwarder_block)
839 internal_error ("%s does not support make_forwarder_block",
840 cfg_hooks->name);
842 fallthru = split_block_after_labels (bb);
843 dummy = fallthru->src;
844 dummy->count = 0;
845 dummy->frequency = 0;
846 fallthru->count = 0;
847 bb = fallthru->dest;
849 /* Redirect back edges we want to keep. */
850 for (ei = ei_start (dummy->preds); (e = ei_safe_edge (ei)); )
852 basic_block e_src;
854 if (redirect_edge_p (e))
856 dummy->frequency += EDGE_FREQUENCY (e);
857 dummy->count += e->count;
858 fallthru->count += e->count;
859 ei_next (&ei);
860 continue;
863 e_src = e->src;
864 jump = redirect_edge_and_branch_force (e, bb);
865 if (jump != NULL)
867 /* If we redirected the loop latch edge, the JUMP block now acts like
868 the new latch of the loop. */
869 if (current_loops != NULL
870 && dummy->loop_father != NULL
871 && dummy->loop_father->header == dummy
872 && dummy->loop_father->latch == e_src)
873 dummy->loop_father->latch = jump;
875 if (new_bb_cbk != NULL)
876 new_bb_cbk (jump);
880 if (dom_info_available_p (CDI_DOMINATORS))
882 vec<basic_block> doms_to_fix;
883 doms_to_fix.create (2);
884 doms_to_fix.quick_push (dummy);
885 doms_to_fix.quick_push (bb);
886 iterate_fix_dominators (CDI_DOMINATORS, doms_to_fix, false);
887 doms_to_fix.release ();
890 if (current_loops != NULL)
892 /* If we do not split a loop header, then both blocks belong to the
893 same loop. In case we split loop header and do not redirect the
894 latch edge to DUMMY, then DUMMY belongs to the outer loop, and
895 BB becomes the new header. If latch is not recorded for the loop,
896 we leave this updating on the caller (this may only happen during
897 loop analysis). */
898 loop = dummy->loop_father;
899 if (loop->header == dummy
900 && loop->latch != NULL
901 && find_edge (loop->latch, dummy) == NULL)
903 remove_bb_from_loops (dummy);
904 loop->header = bb;
906 cloop = loop;
907 FOR_EACH_EDGE (e, ei, dummy->preds)
909 cloop = find_common_loop (cloop, e->src->loop_father);
911 add_bb_to_loop (dummy, cloop);
914 /* In case we split loop latch, update it. */
915 for (ploop = loop; ploop; ploop = loop_outer (ploop))
916 if (ploop->latch == dummy)
917 ploop->latch = bb;
920 cfg_hooks->make_forwarder_block (fallthru);
922 return fallthru;
925 /* Try to make the edge fallthru. */
927 void
928 tidy_fallthru_edge (edge e)
930 if (cfg_hooks->tidy_fallthru_edge)
931 cfg_hooks->tidy_fallthru_edge (e);
934 /* Fix up edges that now fall through, or rather should now fall through
935 but previously required a jump around now deleted blocks. Simplify
936 the search by only examining blocks numerically adjacent, since this
937 is how they were created.
939 ??? This routine is currently RTL specific. */
941 void
942 tidy_fallthru_edges (void)
944 basic_block b, c;
946 if (!cfg_hooks->tidy_fallthru_edge)
947 return;
949 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
950 return;
952 FOR_BB_BETWEEN (b, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb,
953 EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb, next_bb)
955 edge s;
957 c = b->next_bb;
959 /* We care about simple conditional or unconditional jumps with
960 a single successor.
962 If we had a conditional branch to the next instruction when
963 CFG was built, then there will only be one out edge for the
964 block which ended with the conditional branch (since we do
965 not create duplicate edges).
967 Furthermore, the edge will be marked as a fallthru because we
968 merge the flags for the duplicate edges. So we do not want to
969 check that the edge is not a FALLTHRU edge. */
971 if (single_succ_p (b))
973 s = single_succ_edge (b);
974 if (! (s->flags & EDGE_COMPLEX)
975 && s->dest == c
976 && !(JUMP_P (BB_END (b)) && CROSSING_JUMP_P (BB_END (b))))
977 tidy_fallthru_edge (s);
982 /* Edge E is assumed to be fallthru edge. Emit needed jump instruction
983 (and possibly create new basic block) to make edge non-fallthru.
984 Return newly created BB or NULL if none. */
986 basic_block
987 force_nonfallthru (edge e)
989 basic_block ret, src = e->src;
991 if (!cfg_hooks->force_nonfallthru)
992 internal_error ("%s does not support force_nonfallthru",
993 cfg_hooks->name);
995 ret = cfg_hooks->force_nonfallthru (e);
996 if (ret != NULL)
998 if (dom_info_available_p (CDI_DOMINATORS))
999 set_immediate_dominator (CDI_DOMINATORS, ret, src);
1001 if (current_loops != NULL)
1003 struct loop *loop
1004 = find_common_loop (single_pred (ret)->loop_father,
1005 single_succ (ret)->loop_father);
1006 rescan_loop_exit (e, false, true);
1007 add_bb_to_loop (ret, loop);
1011 return ret;
1014 /* Returns true if we can duplicate basic block BB. */
1016 bool
1017 can_duplicate_block_p (const_basic_block bb)
1019 if (!cfg_hooks->can_duplicate_block_p)
1020 internal_error ("%s does not support can_duplicate_block_p",
1021 cfg_hooks->name);
1023 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun) || bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1024 return false;
1026 return cfg_hooks->can_duplicate_block_p (bb);
1029 /* Duplicates basic block BB and redirects edge E to it. Returns the
1030 new basic block. The new basic block is placed after the basic block
1031 AFTER. */
1033 basic_block
1034 duplicate_block (basic_block bb, edge e, basic_block after)
1036 edge s, n;
1037 basic_block new_bb;
1038 gcov_type new_count = e ? e->count : 0;
1039 edge_iterator ei;
1041 if (!cfg_hooks->duplicate_block)
1042 internal_error ("%s does not support duplicate_block",
1043 cfg_hooks->name);
1045 if (bb->count < new_count)
1046 new_count = bb->count;
1048 gcc_checking_assert (can_duplicate_block_p (bb));
1050 new_bb = cfg_hooks->duplicate_block (bb);
1051 if (after)
1052 move_block_after (new_bb, after);
1054 new_bb->flags = bb->flags;
1055 FOR_EACH_EDGE (s, ei, bb->succs)
1057 /* Since we are creating edges from a new block to successors
1058 of another block (which therefore are known to be disjoint), there
1059 is no need to actually check for duplicated edges. */
1060 n = unchecked_make_edge (new_bb, s->dest, s->flags);
1061 n->probability = s->probability;
1062 if (e && bb->count)
1064 /* Take care for overflows! */
1065 n->count = s->count * (new_count * 10000 / bb->count) / 10000;
1066 s->count -= n->count;
1068 else
1069 n->count = s->count;
1070 n->aux = s->aux;
1073 if (e)
1075 new_bb->count = new_count;
1076 bb->count -= new_count;
1078 new_bb->frequency = EDGE_FREQUENCY (e);
1079 bb->frequency -= EDGE_FREQUENCY (e);
1081 redirect_edge_and_branch_force (e, new_bb);
1083 if (bb->count < 0)
1084 bb->count = 0;
1085 if (bb->frequency < 0)
1086 bb->frequency = 0;
1088 else
1090 new_bb->count = bb->count;
1091 new_bb->frequency = bb->frequency;
1094 set_bb_original (new_bb, bb);
1095 set_bb_copy (bb, new_bb);
1097 /* Add the new block to the copy of the loop of BB, or directly to the loop
1098 of BB if the loop is not being copied. */
1099 if (current_loops != NULL)
1101 struct loop *cloop = bb->loop_father;
1102 struct loop *copy = get_loop_copy (cloop);
1103 /* If we copied the loop header block but not the loop
1104 we have created a loop with multiple entries. Ditch the loop,
1105 add the new block to the outer loop and arrange for a fixup. */
1106 if (!copy
1107 && cloop->header == bb)
1109 add_bb_to_loop (new_bb, loop_outer (cloop));
1110 mark_loop_for_removal (cloop);
1112 else
1114 add_bb_to_loop (new_bb, copy ? copy : cloop);
1115 /* If we copied the loop latch block but not the loop, adjust
1116 loop state. */
1117 if (!copy
1118 && cloop->latch == bb)
1120 cloop->latch = NULL;
1121 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
1126 return new_bb;
1129 /* Return 1 if BB ends with a call, possibly followed by some
1130 instructions that must stay with the call, 0 otherwise. */
1132 bool
1133 block_ends_with_call_p (basic_block bb)
1135 if (!cfg_hooks->block_ends_with_call_p)
1136 internal_error ("%s does not support block_ends_with_call_p", cfg_hooks->name);
1138 return (cfg_hooks->block_ends_with_call_p) (bb);
1141 /* Return 1 if BB ends with a conditional branch, 0 otherwise. */
1143 bool
1144 block_ends_with_condjump_p (const_basic_block bb)
1146 if (!cfg_hooks->block_ends_with_condjump_p)
1147 internal_error ("%s does not support block_ends_with_condjump_p",
1148 cfg_hooks->name);
1150 return (cfg_hooks->block_ends_with_condjump_p) (bb);
1153 /* Add fake edges to the function exit for any non constant and non noreturn
1154 calls, volatile inline assembly in the bitmap of blocks specified by
1155 BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
1156 that were split.
1158 The goal is to expose cases in which entering a basic block does not imply
1159 that all subsequent instructions must be executed. */
1162 flow_call_edges_add (sbitmap blocks)
1164 if (!cfg_hooks->flow_call_edges_add)
1165 internal_error ("%s does not support flow_call_edges_add",
1166 cfg_hooks->name);
1168 return (cfg_hooks->flow_call_edges_add) (blocks);
1171 /* This function is called immediately after edge E is added to the
1172 edge vector E->dest->preds. */
1174 void
1175 execute_on_growing_pred (edge e)
1177 if (cfg_hooks->execute_on_growing_pred)
1178 cfg_hooks->execute_on_growing_pred (e);
1181 /* This function is called immediately before edge E is removed from
1182 the edge vector E->dest->preds. */
1184 void
1185 execute_on_shrinking_pred (edge e)
1187 if (cfg_hooks->execute_on_shrinking_pred)
1188 cfg_hooks->execute_on_shrinking_pred (e);
1191 /* This is used inside loop versioning when we want to insert
1192 stmts/insns on the edges, which have a different behavior
1193 in tree's and in RTL, so we made a CFG hook. */
1194 void
1195 lv_flush_pending_stmts (edge e)
1197 if (cfg_hooks->flush_pending_stmts)
1198 cfg_hooks->flush_pending_stmts (e);
1201 /* Loop versioning uses the duplicate_loop_to_header_edge to create
1202 a new version of the loop basic-blocks, the parameters here are
1203 exactly the same as in duplicate_loop_to_header_edge or
1204 tree_duplicate_loop_to_header_edge; while in tree-ssa there is
1205 additional work to maintain ssa information that's why there is
1206 a need to call the tree_duplicate_loop_to_header_edge rather
1207 than duplicate_loop_to_header_edge when we are in tree mode. */
1208 bool
1209 cfg_hook_duplicate_loop_to_header_edge (struct loop *loop, edge e,
1210 unsigned int ndupl,
1211 sbitmap wont_exit, edge orig,
1212 vec<edge> *to_remove,
1213 int flags)
1215 gcc_assert (cfg_hooks->cfg_hook_duplicate_loop_to_header_edge);
1216 return cfg_hooks->cfg_hook_duplicate_loop_to_header_edge (loop, e,
1217 ndupl, wont_exit,
1218 orig, to_remove,
1219 flags);
1222 /* Conditional jumps are represented differently in trees and RTL,
1223 this hook takes a basic block that is known to have a cond jump
1224 at its end and extracts the taken and not taken edges out of it
1225 and store it in E1 and E2 respectively. */
1226 void
1227 extract_cond_bb_edges (basic_block b, edge *e1, edge *e2)
1229 gcc_assert (cfg_hooks->extract_cond_bb_edges);
1230 cfg_hooks->extract_cond_bb_edges (b, e1, e2);
1233 /* Responsible for updating the ssa info (PHI nodes) on the
1234 new condition basic block that guards the versioned loop. */
1235 void
1236 lv_adjust_loop_header_phi (basic_block first, basic_block second,
1237 basic_block new_block, edge e)
1239 if (cfg_hooks->lv_adjust_loop_header_phi)
1240 cfg_hooks->lv_adjust_loop_header_phi (first, second, new_block, e);
1243 /* Conditions in trees and RTL are different so we need
1244 a different handling when we add the condition to the
1245 versioning code. */
1246 void
1247 lv_add_condition_to_bb (basic_block first, basic_block second,
1248 basic_block new_block, void *cond)
1250 gcc_assert (cfg_hooks->lv_add_condition_to_bb);
1251 cfg_hooks->lv_add_condition_to_bb (first, second, new_block, cond);
1254 /* Checks whether all N blocks in BBS array can be copied. */
1255 bool
1256 can_copy_bbs_p (basic_block *bbs, unsigned n)
1258 unsigned i;
1259 edge e;
1260 int ret = true;
1262 for (i = 0; i < n; i++)
1263 bbs[i]->flags |= BB_DUPLICATED;
1265 for (i = 0; i < n; i++)
1267 /* In case we should redirect abnormal edge during duplication, fail. */
1268 edge_iterator ei;
1269 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
1270 if ((e->flags & EDGE_ABNORMAL)
1271 && (e->dest->flags & BB_DUPLICATED))
1273 ret = false;
1274 goto end;
1277 if (!can_duplicate_block_p (bbs[i]))
1279 ret = false;
1280 break;
1284 end:
1285 for (i = 0; i < n; i++)
1286 bbs[i]->flags &= ~BB_DUPLICATED;
1288 return ret;
1291 /* Duplicates N basic blocks stored in array BBS. Newly created basic blocks
1292 are placed into array NEW_BBS in the same order. Edges from basic blocks
1293 in BBS are also duplicated and copies of those that lead into BBS are
1294 redirected to appropriate newly created block. The function assigns bbs
1295 into loops (copy of basic block bb is assigned to bb->loop_father->copy
1296 loop, so this must be set up correctly in advance)
1298 If UPDATE_DOMINANCE is true then this function updates dominators locally
1299 (LOOPS structure that contains the information about dominators is passed
1300 to enable this), otherwise it does not update the dominator information
1301 and it assumed that the caller will do this, perhaps by destroying and
1302 recreating it instead of trying to do an incremental update like this
1303 function does when update_dominance is true.
1305 BASE is the superloop to that basic block belongs; if its header or latch
1306 is copied, we do not set the new blocks as header or latch.
1308 Created copies of N_EDGES edges in array EDGES are stored in array NEW_EDGES,
1309 also in the same order.
1311 Newly created basic blocks are put after the basic block AFTER in the
1312 instruction stream, and the order of the blocks in BBS array is preserved. */
1314 void
1315 copy_bbs (basic_block *bbs, unsigned n, basic_block *new_bbs,
1316 edge *edges, unsigned num_edges, edge *new_edges,
1317 struct loop *base, basic_block after, bool update_dominance)
1319 unsigned i, j;
1320 basic_block bb, new_bb, dom_bb;
1321 edge e;
1323 /* Duplicate bbs, update dominators, assign bbs to loops. */
1324 for (i = 0; i < n; i++)
1326 /* Duplicate. */
1327 bb = bbs[i];
1328 new_bb = new_bbs[i] = duplicate_block (bb, NULL, after);
1329 after = new_bb;
1330 bb->flags |= BB_DUPLICATED;
1331 if (bb->loop_father)
1333 /* Possibly set loop header. */
1334 if (bb->loop_father->header == bb && bb->loop_father != base)
1335 new_bb->loop_father->header = new_bb;
1336 /* Or latch. */
1337 if (bb->loop_father->latch == bb && bb->loop_father != base)
1338 new_bb->loop_father->latch = new_bb;
1342 /* Set dominators. */
1343 if (update_dominance)
1345 for (i = 0; i < n; i++)
1347 bb = bbs[i];
1348 new_bb = new_bbs[i];
1350 dom_bb = get_immediate_dominator (CDI_DOMINATORS, bb);
1351 if (dom_bb->flags & BB_DUPLICATED)
1353 dom_bb = get_bb_copy (dom_bb);
1354 set_immediate_dominator (CDI_DOMINATORS, new_bb, dom_bb);
1359 /* Redirect edges. */
1360 for (j = 0; j < num_edges; j++)
1361 new_edges[j] = NULL;
1362 for (i = 0; i < n; i++)
1364 edge_iterator ei;
1365 new_bb = new_bbs[i];
1366 bb = bbs[i];
1368 FOR_EACH_EDGE (e, ei, new_bb->succs)
1370 for (j = 0; j < num_edges; j++)
1371 if (edges[j] && edges[j]->src == bb && edges[j]->dest == e->dest)
1372 new_edges[j] = e;
1374 if (!(e->dest->flags & BB_DUPLICATED))
1375 continue;
1376 redirect_edge_and_branch_force (e, get_bb_copy (e->dest));
1380 /* Clear information about duplicates. */
1381 for (i = 0; i < n; i++)
1382 bbs[i]->flags &= ~BB_DUPLICATED;
1385 /* Return true if BB contains only labels or non-executable
1386 instructions */
1387 bool
1388 empty_block_p (basic_block bb)
1390 gcc_assert (cfg_hooks->empty_block_p);
1391 return cfg_hooks->empty_block_p (bb);
1394 /* Split a basic block if it ends with a conditional branch and if
1395 the other part of the block is not empty. */
1396 basic_block
1397 split_block_before_cond_jump (basic_block bb)
1399 gcc_assert (cfg_hooks->split_block_before_cond_jump);
1400 return cfg_hooks->split_block_before_cond_jump (bb);
1403 /* Work-horse for passes.c:check_profile_consistency.
1404 Do book-keeping of the CFG for the profile consistency checker.
1405 If AFTER_PASS is 0, do pre-pass accounting, or if AFTER_PASS is 1
1406 then do post-pass accounting. Store the counting in RECORD. */
1408 void
1409 account_profile_record (struct profile_record *record, int after_pass)
1411 basic_block bb;
1412 edge_iterator ei;
1413 edge e;
1414 int sum;
1415 gcov_type lsum;
1417 FOR_ALL_BB_FN (bb, cfun)
1419 if (bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
1420 && profile_status_for_fn (cfun) != PROFILE_ABSENT)
1422 sum = 0;
1423 FOR_EACH_EDGE (e, ei, bb->succs)
1424 sum += e->probability;
1425 if (EDGE_COUNT (bb->succs) && abs (sum - REG_BR_PROB_BASE) > 100)
1426 record->num_mismatched_freq_out[after_pass]++;
1427 lsum = 0;
1428 FOR_EACH_EDGE (e, ei, bb->succs)
1429 lsum += e->count;
1430 if (EDGE_COUNT (bb->succs)
1431 && (lsum - bb->count > 100 || lsum - bb->count < -100))
1432 record->num_mismatched_count_out[after_pass]++;
1434 if (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
1435 && profile_status_for_fn (cfun) != PROFILE_ABSENT)
1437 sum = 0;
1438 FOR_EACH_EDGE (e, ei, bb->preds)
1439 sum += EDGE_FREQUENCY (e);
1440 if (abs (sum - bb->frequency) > 100
1441 || (MAX (sum, bb->frequency) > 10
1442 && abs ((sum - bb->frequency) * 100 / (MAX (sum, bb->frequency) + 1)) > 10))
1443 record->num_mismatched_freq_in[after_pass]++;
1444 lsum = 0;
1445 FOR_EACH_EDGE (e, ei, bb->preds)
1446 lsum += e->count;
1447 if (lsum - bb->count > 100 || lsum - bb->count < -100)
1448 record->num_mismatched_count_in[after_pass]++;
1450 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)
1451 || bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1452 continue;
1453 gcc_assert (cfg_hooks->account_profile_record);
1454 cfg_hooks->account_profile_record (bb, after_pass, record);