remove #if for HAVE_cc0 in combine.c
[official-gcc.git] / gcc / cfghooks.c
blob285ec3d291fed727fb8e239c682db74b4871f908
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>
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 "backend.h"
26 #include "alias.h"
27 #include "tree.h"
28 #include "rtl.h"
29 #include "cfganal.h"
30 #include "tree-ssa.h"
31 #include "timevar.h"
32 #include "diagnostic-core.h"
33 #include "cfgloop.h"
34 #include "pretty-print.h"
36 /* A pointer to one of the hooks containers. */
37 static struct cfg_hooks *cfg_hooks;
39 /* Initialization of functions specific to the rtl IR. */
40 void
41 rtl_register_cfg_hooks (void)
43 cfg_hooks = &rtl_cfg_hooks;
46 /* Initialization of functions specific to the rtl IR. */
47 void
48 cfg_layout_rtl_register_cfg_hooks (void)
50 cfg_hooks = &cfg_layout_rtl_cfg_hooks;
53 /* Initialization of functions specific to the tree IR. */
55 void
56 gimple_register_cfg_hooks (void)
58 cfg_hooks = &gimple_cfg_hooks;
61 struct cfg_hooks
62 get_cfg_hooks (void)
64 return *cfg_hooks;
67 void
68 set_cfg_hooks (struct cfg_hooks new_cfg_hooks)
70 *cfg_hooks = new_cfg_hooks;
73 /* Returns current ir type. */
75 enum ir_type
76 current_ir_type (void)
78 if (cfg_hooks == &gimple_cfg_hooks)
79 return IR_GIMPLE;
80 else if (cfg_hooks == &rtl_cfg_hooks)
81 return IR_RTL_CFGRTL;
82 else if (cfg_hooks == &cfg_layout_rtl_cfg_hooks)
83 return IR_RTL_CFGLAYOUT;
84 else
85 gcc_unreachable ();
88 /* Verify the CFG consistency.
90 Currently it does following: checks edge and basic block list correctness
91 and calls into IL dependent checking then. */
93 DEBUG_FUNCTION void
94 verify_flow_info (void)
96 size_t *edge_checksum;
97 int err = 0;
98 basic_block bb, last_bb_seen;
99 basic_block *last_visited;
101 timevar_push (TV_CFG_VERIFY);
102 last_visited = XCNEWVEC (basic_block, last_basic_block_for_fn (cfun));
103 edge_checksum = XCNEWVEC (size_t, last_basic_block_for_fn (cfun));
105 /* Check bb chain & numbers. */
106 last_bb_seen = ENTRY_BLOCK_PTR_FOR_FN (cfun);
107 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb, NULL, next_bb)
109 if (bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
110 && bb != BASIC_BLOCK_FOR_FN (cfun, bb->index))
112 error ("bb %d on wrong place", bb->index);
113 err = 1;
116 if (bb->prev_bb != last_bb_seen)
118 error ("prev_bb of %d should be %d, not %d",
119 bb->index, last_bb_seen->index, bb->prev_bb->index);
120 err = 1;
123 last_bb_seen = bb;
126 /* Now check the basic blocks (boundaries etc.) */
127 FOR_EACH_BB_REVERSE_FN (bb, cfun)
129 int n_fallthru = 0;
130 edge e;
131 edge_iterator ei;
133 if (bb->loop_father != NULL && current_loops == NULL)
135 error ("verify_flow_info: Block %i has loop_father, but there are no loops",
136 bb->index);
137 err = 1;
139 if (bb->loop_father == NULL && current_loops != NULL)
141 error ("verify_flow_info: Block %i lacks loop_father", bb->index);
142 err = 1;
145 if (bb->count < 0)
147 error ("verify_flow_info: Wrong count of block %i %i",
148 bb->index, (int)bb->count);
149 err = 1;
151 if (bb->frequency < 0)
153 error ("verify_flow_info: Wrong frequency of block %i %i",
154 bb->index, bb->frequency);
155 err = 1;
157 FOR_EACH_EDGE (e, ei, bb->succs)
159 if (last_visited [e->dest->index] == bb)
161 error ("verify_flow_info: Duplicate edge %i->%i",
162 e->src->index, e->dest->index);
163 err = 1;
165 if (e->probability < 0 || e->probability > REG_BR_PROB_BASE)
167 error ("verify_flow_info: Wrong probability of edge %i->%i %i",
168 e->src->index, e->dest->index, e->probability);
169 err = 1;
171 if (e->count < 0)
173 error ("verify_flow_info: Wrong count of edge %i->%i %i",
174 e->src->index, e->dest->index, (int)e->count);
175 err = 1;
178 last_visited [e->dest->index] = bb;
180 if (e->flags & EDGE_FALLTHRU)
181 n_fallthru++;
183 if (e->src != bb)
185 error ("verify_flow_info: Basic block %d succ edge is corrupted",
186 bb->index);
187 fprintf (stderr, "Predecessor: ");
188 dump_edge_info (stderr, e, TDF_DETAILS, 0);
189 fprintf (stderr, "\nSuccessor: ");
190 dump_edge_info (stderr, e, TDF_DETAILS, 1);
191 fprintf (stderr, "\n");
192 err = 1;
195 edge_checksum[e->dest->index] += (size_t) e;
197 if (n_fallthru > 1)
199 error ("wrong amount of branch edges after unconditional jump %i", bb->index);
200 err = 1;
203 FOR_EACH_EDGE (e, ei, bb->preds)
205 if (e->dest != bb)
207 error ("basic block %d pred edge is corrupted", bb->index);
208 fputs ("Predecessor: ", stderr);
209 dump_edge_info (stderr, e, TDF_DETAILS, 0);
210 fputs ("\nSuccessor: ", stderr);
211 dump_edge_info (stderr, e, TDF_DETAILS, 1);
212 fputc ('\n', stderr);
213 err = 1;
216 if (ei.index != e->dest_idx)
218 error ("basic block %d pred edge is corrupted", bb->index);
219 error ("its dest_idx should be %d, not %d",
220 ei.index, e->dest_idx);
221 fputs ("Predecessor: ", stderr);
222 dump_edge_info (stderr, e, TDF_DETAILS, 0);
223 fputs ("\nSuccessor: ", stderr);
224 dump_edge_info (stderr, e, TDF_DETAILS, 1);
225 fputc ('\n', stderr);
226 err = 1;
229 edge_checksum[e->dest->index] -= (size_t) e;
233 /* Complete edge checksumming for ENTRY and EXIT. */
235 edge e;
236 edge_iterator ei;
238 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
239 edge_checksum[e->dest->index] += (size_t) e;
241 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
242 edge_checksum[e->dest->index] -= (size_t) e;
245 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
246 if (edge_checksum[bb->index])
248 error ("basic block %i edge lists are corrupted", bb->index);
249 err = 1;
252 last_bb_seen = ENTRY_BLOCK_PTR_FOR_FN (cfun);
254 /* Clean up. */
255 free (last_visited);
256 free (edge_checksum);
258 if (cfg_hooks->verify_flow_info)
259 err |= cfg_hooks->verify_flow_info ();
260 if (err)
261 internal_error ("verify_flow_info failed");
262 timevar_pop (TV_CFG_VERIFY);
265 /* Print out one basic block BB to file OUTF. INDENT is printed at the
266 start of each new line. FLAGS are the TDF_* flags in dumpfile.h.
268 This function takes care of the purely graph related information.
269 The cfg hook for the active representation should dump
270 representation-specific information. */
272 void
273 dump_bb (FILE *outf, basic_block bb, int indent, int flags)
275 if (flags & TDF_BLOCKS)
276 dump_bb_info (outf, bb, indent, flags, true, false);
277 if (cfg_hooks->dump_bb)
278 cfg_hooks->dump_bb (outf, bb, indent, flags);
279 if (flags & TDF_BLOCKS)
280 dump_bb_info (outf, bb, indent, flags, false, true);
281 fputc ('\n', outf);
284 DEBUG_FUNCTION void
285 debug (basic_block_def &ref)
287 dump_bb (stderr, &ref, 0, 0);
290 DEBUG_FUNCTION void
291 debug (basic_block_def *ptr)
293 if (ptr)
294 debug (*ptr);
295 else
296 fprintf (stderr, "<nil>\n");
300 /* Dumps basic block BB to pretty-printer PP, for use as a label of
301 a DOT graph record-node. The implementation of this hook is
302 expected to write the label to the stream that is attached to PP.
303 Field separators between instructions are pipe characters printed
304 verbatim. Instructions should be written with some characters
305 escaped, using pp_write_text_as_dot_label_to_stream(). */
307 void
308 dump_bb_for_graph (pretty_printer *pp, basic_block bb)
310 if (!cfg_hooks->dump_bb_for_graph)
311 internal_error ("%s does not support dump_bb_for_graph",
312 cfg_hooks->name);
313 if (bb->count)
314 pp_printf (pp, "COUNT:" "%" PRId64, bb->count);
315 pp_printf (pp, " FREQ:%i |", bb->frequency);
316 pp_write_text_to_stream (pp);
317 if (!(dump_flags & TDF_SLIM))
318 cfg_hooks->dump_bb_for_graph (pp, bb);
321 /* Dump the complete CFG to FILE. FLAGS are the TDF_* flags in dumpfile.h. */
322 void
323 dump_flow_info (FILE *file, int flags)
325 basic_block bb;
327 fprintf (file, "\n%d basic blocks, %d edges.\n", n_basic_blocks_for_fn (cfun),
328 n_edges_for_fn (cfun));
329 FOR_ALL_BB_FN (bb, cfun)
330 dump_bb (file, bb, 0, flags);
332 putc ('\n', file);
335 /* Like above, but dump to stderr. To be called from debuggers. */
336 void debug_flow_info (void);
337 DEBUG_FUNCTION void
338 debug_flow_info (void)
340 dump_flow_info (stderr, TDF_DETAILS);
343 /* Redirect edge E to the given basic block DEST and update underlying program
344 representation. Returns edge representing redirected branch (that may not
345 be equivalent to E in the case of duplicate edges being removed) or NULL
346 if edge is not easily redirectable for whatever reason. */
348 edge
349 redirect_edge_and_branch (edge e, basic_block dest)
351 edge ret;
353 if (!cfg_hooks->redirect_edge_and_branch)
354 internal_error ("%s does not support redirect_edge_and_branch",
355 cfg_hooks->name);
357 ret = cfg_hooks->redirect_edge_and_branch (e, dest);
359 /* If RET != E, then either the redirection failed, or the edge E
360 was removed since RET already lead to the same destination. */
361 if (current_loops != NULL && ret == e)
362 rescan_loop_exit (e, false, false);
364 return ret;
367 /* Returns true if it is possible to remove the edge E by redirecting it
368 to the destination of the other edge going from its source. */
370 bool
371 can_remove_branch_p (const_edge e)
373 if (!cfg_hooks->can_remove_branch_p)
374 internal_error ("%s does not support can_remove_branch_p",
375 cfg_hooks->name);
377 if (EDGE_COUNT (e->src->succs) != 2)
378 return false;
380 return cfg_hooks->can_remove_branch_p (e);
383 /* Removes E, by redirecting it to the destination of the other edge going
384 from its source. Can_remove_branch_p must be true for E, hence this
385 operation cannot fail. */
387 void
388 remove_branch (edge e)
390 edge other;
391 basic_block src = e->src;
392 int irr;
394 gcc_assert (EDGE_COUNT (e->src->succs) == 2);
396 other = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e);
397 irr = other->flags & EDGE_IRREDUCIBLE_LOOP;
399 e = redirect_edge_and_branch (e, other->dest);
400 gcc_assert (e != NULL);
402 e->flags &= ~EDGE_IRREDUCIBLE_LOOP;
403 e->flags |= irr;
406 /* Removes edge E from cfg. Unlike remove_branch, it does not update IL. */
408 void
409 remove_edge (edge e)
411 if (current_loops != NULL)
412 rescan_loop_exit (e, false, true);
414 /* This is probably not needed, but it doesn't hurt. */
415 /* FIXME: This should be called via a remove_edge hook. */
416 if (current_ir_type () == IR_GIMPLE)
417 redirect_edge_var_map_clear (e);
419 remove_edge_raw (e);
422 /* Like redirect_edge_succ but avoid possible duplicate edge. */
424 edge
425 redirect_edge_succ_nodup (edge e, basic_block new_succ)
427 edge s;
429 s = find_edge (e->src, new_succ);
430 if (s && s != e)
432 s->flags |= e->flags;
433 s->probability += e->probability;
434 if (s->probability > REG_BR_PROB_BASE)
435 s->probability = REG_BR_PROB_BASE;
436 s->count += e->count;
437 /* FIXME: This should be called via a hook and only for IR_GIMPLE. */
438 redirect_edge_var_map_dup (s, e);
439 remove_edge (e);
440 e = s;
442 else
443 redirect_edge_succ (e, new_succ);
445 return e;
448 /* Redirect the edge E to basic block DEST even if it requires creating
449 of a new basic block; then it returns the newly created basic block.
450 Aborts when redirection is impossible. */
452 basic_block
453 redirect_edge_and_branch_force (edge e, basic_block dest)
455 basic_block ret, src = e->src;
457 if (!cfg_hooks->redirect_edge_and_branch_force)
458 internal_error ("%s does not support redirect_edge_and_branch_force",
459 cfg_hooks->name);
461 if (current_loops != NULL)
462 rescan_loop_exit (e, false, true);
464 ret = cfg_hooks->redirect_edge_and_branch_force (e, dest);
466 if (ret != NULL && dom_info_available_p (CDI_DOMINATORS))
467 set_immediate_dominator (CDI_DOMINATORS, ret, src);
469 if (current_loops != NULL)
471 if (ret != NULL)
473 struct loop *loop
474 = find_common_loop (single_pred (ret)->loop_father,
475 single_succ (ret)->loop_father);
476 add_bb_to_loop (ret, loop);
478 else if (find_edge (src, dest) == e)
479 rescan_loop_exit (e, true, false);
482 return ret;
485 /* Splits basic block BB after the specified instruction I (but at least after
486 the labels). If I is NULL, splits just after labels. The newly created edge
487 is returned. The new basic block is created just after the old one. */
489 static edge
490 split_block_1 (basic_block bb, void *i)
492 basic_block new_bb;
493 edge res;
495 if (!cfg_hooks->split_block)
496 internal_error ("%s does not support split_block", cfg_hooks->name);
498 new_bb = cfg_hooks->split_block (bb, i);
499 if (!new_bb)
500 return NULL;
502 new_bb->count = bb->count;
503 new_bb->frequency = bb->frequency;
504 new_bb->discriminator = bb->discriminator;
506 if (dom_info_available_p (CDI_DOMINATORS))
508 redirect_immediate_dominators (CDI_DOMINATORS, bb, new_bb);
509 set_immediate_dominator (CDI_DOMINATORS, new_bb, bb);
512 if (current_loops != NULL)
514 edge_iterator ei;
515 edge e;
516 add_bb_to_loop (new_bb, bb->loop_father);
517 /* Identify all loops bb may have been the latch of and adjust them. */
518 FOR_EACH_EDGE (e, ei, new_bb->succs)
519 if (e->dest->loop_father->latch == bb)
520 e->dest->loop_father->latch = new_bb;
523 res = make_single_succ_edge (bb, new_bb, EDGE_FALLTHRU);
525 if (bb->flags & BB_IRREDUCIBLE_LOOP)
527 new_bb->flags |= BB_IRREDUCIBLE_LOOP;
528 res->flags |= EDGE_IRREDUCIBLE_LOOP;
531 return res;
534 edge
535 split_block (basic_block bb, gimple i)
537 return split_block_1 (bb, i);
540 edge
541 split_block (basic_block bb, rtx i)
543 return split_block_1 (bb, i);
546 /* Splits block BB just after labels. The newly created edge is returned. */
548 edge
549 split_block_after_labels (basic_block bb)
551 return split_block_1 (bb, NULL);
554 /* Moves block BB immediately after block AFTER. Returns false if the
555 movement was impossible. */
557 bool
558 move_block_after (basic_block bb, basic_block after)
560 bool ret;
562 if (!cfg_hooks->move_block_after)
563 internal_error ("%s does not support move_block_after", cfg_hooks->name);
565 ret = cfg_hooks->move_block_after (bb, after);
567 return ret;
570 /* Deletes the basic block BB. */
572 void
573 delete_basic_block (basic_block bb)
575 if (!cfg_hooks->delete_basic_block)
576 internal_error ("%s does not support delete_basic_block", cfg_hooks->name);
578 cfg_hooks->delete_basic_block (bb);
580 if (current_loops != NULL)
582 struct loop *loop = bb->loop_father;
584 /* If we remove the header or the latch of a loop, mark the loop for
585 removal. */
586 if (loop->latch == bb
587 || loop->header == bb)
588 mark_loop_for_removal (loop);
590 remove_bb_from_loops (bb);
593 /* Remove the edges into and out of this block. Note that there may
594 indeed be edges in, if we are removing an unreachable loop. */
595 while (EDGE_COUNT (bb->preds) != 0)
596 remove_edge (EDGE_PRED (bb, 0));
597 while (EDGE_COUNT (bb->succs) != 0)
598 remove_edge (EDGE_SUCC (bb, 0));
600 if (dom_info_available_p (CDI_DOMINATORS))
601 delete_from_dominance_info (CDI_DOMINATORS, bb);
602 if (dom_info_available_p (CDI_POST_DOMINATORS))
603 delete_from_dominance_info (CDI_POST_DOMINATORS, bb);
605 /* Remove the basic block from the array. */
606 expunge_block (bb);
609 /* Splits edge E and returns the newly created basic block. */
611 basic_block
612 split_edge (edge e)
614 basic_block ret;
615 gcov_type count = e->count;
616 int freq = EDGE_FREQUENCY (e);
617 edge f;
618 bool irr = (e->flags & EDGE_IRREDUCIBLE_LOOP) != 0;
619 struct loop *loop;
620 basic_block src = e->src, dest = e->dest;
622 if (!cfg_hooks->split_edge)
623 internal_error ("%s does not support split_edge", cfg_hooks->name);
625 if (current_loops != NULL)
626 rescan_loop_exit (e, false, true);
628 ret = cfg_hooks->split_edge (e);
629 ret->count = count;
630 ret->frequency = freq;
631 single_succ_edge (ret)->probability = REG_BR_PROB_BASE;
632 single_succ_edge (ret)->count = count;
634 if (irr)
636 ret->flags |= BB_IRREDUCIBLE_LOOP;
637 single_pred_edge (ret)->flags |= EDGE_IRREDUCIBLE_LOOP;
638 single_succ_edge (ret)->flags |= EDGE_IRREDUCIBLE_LOOP;
641 if (dom_info_available_p (CDI_DOMINATORS))
642 set_immediate_dominator (CDI_DOMINATORS, ret, single_pred (ret));
644 if (dom_info_state (CDI_DOMINATORS) >= DOM_NO_FAST_QUERY)
646 /* There are two cases:
648 If the immediate dominator of e->dest is not e->src, it
649 remains unchanged.
651 If immediate dominator of e->dest is e->src, it may become
652 ret, provided that all other predecessors of e->dest are
653 dominated by e->dest. */
655 if (get_immediate_dominator (CDI_DOMINATORS, single_succ (ret))
656 == single_pred (ret))
658 edge_iterator ei;
659 FOR_EACH_EDGE (f, ei, single_succ (ret)->preds)
661 if (f == single_succ_edge (ret))
662 continue;
664 if (!dominated_by_p (CDI_DOMINATORS, f->src,
665 single_succ (ret)))
666 break;
669 if (!f)
670 set_immediate_dominator (CDI_DOMINATORS, single_succ (ret), ret);
674 if (current_loops != NULL)
676 loop = find_common_loop (src->loop_father, dest->loop_father);
677 add_bb_to_loop (ret, loop);
679 /* If we split the latch edge of loop adjust the latch block. */
680 if (loop->latch == src
681 && loop->header == dest)
682 loop->latch = ret;
685 return ret;
688 /* Creates a new basic block just after the basic block AFTER.
689 HEAD and END are the first and the last statement belonging
690 to the block. If both are NULL, an empty block is created. */
692 static basic_block
693 create_basic_block_1 (void *head, void *end, basic_block after)
695 basic_block ret;
697 if (!cfg_hooks->create_basic_block)
698 internal_error ("%s does not support create_basic_block", cfg_hooks->name);
700 ret = cfg_hooks->create_basic_block (head, end, after);
702 if (dom_info_available_p (CDI_DOMINATORS))
703 add_to_dominance_info (CDI_DOMINATORS, ret);
704 if (dom_info_available_p (CDI_POST_DOMINATORS))
705 add_to_dominance_info (CDI_POST_DOMINATORS, ret);
707 return ret;
710 basic_block
711 create_basic_block (gimple_seq seq, basic_block after)
713 return create_basic_block_1 (seq, NULL, after);
716 basic_block
717 create_basic_block (rtx head, rtx end, basic_block after)
719 return create_basic_block_1 (head, end, after);
723 /* Creates an empty basic block just after basic block AFTER. */
725 basic_block
726 create_empty_bb (basic_block after)
728 return create_basic_block_1 (NULL, NULL, after);
731 /* Checks whether we may merge blocks BB1 and BB2. */
733 bool
734 can_merge_blocks_p (basic_block bb1, basic_block bb2)
736 bool ret;
738 if (!cfg_hooks->can_merge_blocks_p)
739 internal_error ("%s does not support can_merge_blocks_p", cfg_hooks->name);
741 ret = cfg_hooks->can_merge_blocks_p (bb1, bb2);
743 return ret;
746 void
747 predict_edge (edge e, enum br_predictor predictor, int probability)
749 if (!cfg_hooks->predict_edge)
750 internal_error ("%s does not support predict_edge", cfg_hooks->name);
752 cfg_hooks->predict_edge (e, predictor, probability);
755 bool
756 predicted_by_p (const_basic_block bb, enum br_predictor predictor)
758 if (!cfg_hooks->predict_edge)
759 internal_error ("%s does not support predicted_by_p", cfg_hooks->name);
761 return cfg_hooks->predicted_by_p (bb, predictor);
764 /* Merges basic block B into basic block A. */
766 void
767 merge_blocks (basic_block a, basic_block b)
769 edge e;
770 edge_iterator ei;
772 if (!cfg_hooks->merge_blocks)
773 internal_error ("%s does not support merge_blocks", cfg_hooks->name);
775 cfg_hooks->merge_blocks (a, b);
777 if (current_loops != NULL)
779 /* If the block we merge into is a loop header do nothing unless ... */
780 if (a->loop_father->header == a)
782 /* ... we merge two loop headers, in which case we kill
783 the inner loop. */
784 if (b->loop_father->header == b)
785 mark_loop_for_removal (b->loop_father);
787 /* If we merge a loop header into its predecessor, update the loop
788 structure. */
789 else if (b->loop_father->header == b)
791 remove_bb_from_loops (a);
792 add_bb_to_loop (a, b->loop_father);
793 a->loop_father->header = a;
795 /* If we merge a loop latch into its predecessor, update the loop
796 structure. */
797 if (b->loop_father->latch
798 && b->loop_father->latch == b)
799 b->loop_father->latch = a;
800 remove_bb_from_loops (b);
803 /* Normally there should only be one successor of A and that is B, but
804 partway though the merge of blocks for conditional_execution we'll
805 be merging a TEST block with THEN and ELSE successors. Free the
806 whole lot of them and hope the caller knows what they're doing. */
808 while (EDGE_COUNT (a->succs) != 0)
809 remove_edge (EDGE_SUCC (a, 0));
811 /* Adjust the edges out of B for the new owner. */
812 FOR_EACH_EDGE (e, ei, b->succs)
814 e->src = a;
815 if (current_loops != NULL)
817 /* If b was a latch, a now is. */
818 if (e->dest->loop_father->latch == b)
819 e->dest->loop_father->latch = a;
820 rescan_loop_exit (e, true, false);
823 a->succs = b->succs;
824 a->flags |= b->flags;
826 /* B hasn't quite yet ceased to exist. Attempt to prevent mishap. */
827 b->preds = b->succs = NULL;
829 if (dom_info_available_p (CDI_DOMINATORS))
830 redirect_immediate_dominators (CDI_DOMINATORS, b, a);
832 if (dom_info_available_p (CDI_DOMINATORS))
833 delete_from_dominance_info (CDI_DOMINATORS, b);
834 if (dom_info_available_p (CDI_POST_DOMINATORS))
835 delete_from_dominance_info (CDI_POST_DOMINATORS, b);
837 expunge_block (b);
840 /* Split BB into entry part and the rest (the rest is the newly created block).
841 Redirect those edges for that REDIRECT_EDGE_P returns true to the entry
842 part. Returns the edge connecting the entry part to the rest. */
844 edge
845 make_forwarder_block (basic_block bb, bool (*redirect_edge_p) (edge),
846 void (*new_bb_cbk) (basic_block))
848 edge e, fallthru;
849 edge_iterator ei;
850 basic_block dummy, jump;
851 struct loop *loop, *ploop, *cloop;
853 if (!cfg_hooks->make_forwarder_block)
854 internal_error ("%s does not support make_forwarder_block",
855 cfg_hooks->name);
857 fallthru = split_block_after_labels (bb);
858 dummy = fallthru->src;
859 dummy->count = 0;
860 dummy->frequency = 0;
861 fallthru->count = 0;
862 bb = fallthru->dest;
864 /* Redirect back edges we want to keep. */
865 for (ei = ei_start (dummy->preds); (e = ei_safe_edge (ei)); )
867 basic_block e_src;
869 if (redirect_edge_p (e))
871 dummy->frequency += EDGE_FREQUENCY (e);
872 if (dummy->frequency > BB_FREQ_MAX)
873 dummy->frequency = BB_FREQ_MAX;
875 dummy->count += e->count;
876 fallthru->count += e->count;
877 ei_next (&ei);
878 continue;
881 e_src = e->src;
882 jump = redirect_edge_and_branch_force (e, bb);
883 if (jump != NULL)
885 /* If we redirected the loop latch edge, the JUMP block now acts like
886 the new latch of the loop. */
887 if (current_loops != NULL
888 && dummy->loop_father != NULL
889 && dummy->loop_father->header == dummy
890 && dummy->loop_father->latch == e_src)
891 dummy->loop_father->latch = jump;
893 if (new_bb_cbk != NULL)
894 new_bb_cbk (jump);
898 if (dom_info_available_p (CDI_DOMINATORS))
900 vec<basic_block> doms_to_fix;
901 doms_to_fix.create (2);
902 doms_to_fix.quick_push (dummy);
903 doms_to_fix.quick_push (bb);
904 iterate_fix_dominators (CDI_DOMINATORS, doms_to_fix, false);
905 doms_to_fix.release ();
908 if (current_loops != NULL)
910 /* If we do not split a loop header, then both blocks belong to the
911 same loop. In case we split loop header and do not redirect the
912 latch edge to DUMMY, then DUMMY belongs to the outer loop, and
913 BB becomes the new header. If latch is not recorded for the loop,
914 we leave this updating on the caller (this may only happen during
915 loop analysis). */
916 loop = dummy->loop_father;
917 if (loop->header == dummy
918 && loop->latch != NULL
919 && find_edge (loop->latch, dummy) == NULL)
921 remove_bb_from_loops (dummy);
922 loop->header = bb;
924 cloop = loop;
925 FOR_EACH_EDGE (e, ei, dummy->preds)
927 cloop = find_common_loop (cloop, e->src->loop_father);
929 add_bb_to_loop (dummy, cloop);
932 /* In case we split loop latch, update it. */
933 for (ploop = loop; ploop; ploop = loop_outer (ploop))
934 if (ploop->latch == dummy)
935 ploop->latch = bb;
938 cfg_hooks->make_forwarder_block (fallthru);
940 return fallthru;
943 /* Try to make the edge fallthru. */
945 void
946 tidy_fallthru_edge (edge e)
948 if (cfg_hooks->tidy_fallthru_edge)
949 cfg_hooks->tidy_fallthru_edge (e);
952 /* Fix up edges that now fall through, or rather should now fall through
953 but previously required a jump around now deleted blocks. Simplify
954 the search by only examining blocks numerically adjacent, since this
955 is how they were created.
957 ??? This routine is currently RTL specific. */
959 void
960 tidy_fallthru_edges (void)
962 basic_block b, c;
964 if (!cfg_hooks->tidy_fallthru_edge)
965 return;
967 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
968 return;
970 FOR_BB_BETWEEN (b, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb,
971 EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb, next_bb)
973 edge s;
975 c = b->next_bb;
977 /* We care about simple conditional or unconditional jumps with
978 a single successor.
980 If we had a conditional branch to the next instruction when
981 CFG was built, then there will only be one out edge for the
982 block which ended with the conditional branch (since we do
983 not create duplicate edges).
985 Furthermore, the edge will be marked as a fallthru because we
986 merge the flags for the duplicate edges. So we do not want to
987 check that the edge is not a FALLTHRU edge. */
989 if (single_succ_p (b))
991 s = single_succ_edge (b);
992 if (! (s->flags & EDGE_COMPLEX)
993 && s->dest == c
994 && !(JUMP_P (BB_END (b)) && CROSSING_JUMP_P (BB_END (b))))
995 tidy_fallthru_edge (s);
1000 /* Edge E is assumed to be fallthru edge. Emit needed jump instruction
1001 (and possibly create new basic block) to make edge non-fallthru.
1002 Return newly created BB or NULL if none. */
1004 basic_block
1005 force_nonfallthru (edge e)
1007 basic_block ret, src = e->src;
1009 if (!cfg_hooks->force_nonfallthru)
1010 internal_error ("%s does not support force_nonfallthru",
1011 cfg_hooks->name);
1013 ret = cfg_hooks->force_nonfallthru (e);
1014 if (ret != NULL)
1016 if (dom_info_available_p (CDI_DOMINATORS))
1017 set_immediate_dominator (CDI_DOMINATORS, ret, src);
1019 if (current_loops != NULL)
1021 struct loop *loop
1022 = find_common_loop (single_pred (ret)->loop_father,
1023 single_succ (ret)->loop_father);
1024 rescan_loop_exit (e, false, true);
1025 add_bb_to_loop (ret, loop);
1029 return ret;
1032 /* Returns true if we can duplicate basic block BB. */
1034 bool
1035 can_duplicate_block_p (const_basic_block bb)
1037 if (!cfg_hooks->can_duplicate_block_p)
1038 internal_error ("%s does not support can_duplicate_block_p",
1039 cfg_hooks->name);
1041 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun) || bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1042 return false;
1044 return cfg_hooks->can_duplicate_block_p (bb);
1047 /* Duplicates basic block BB and redirects edge E to it. Returns the
1048 new basic block. The new basic block is placed after the basic block
1049 AFTER. */
1051 basic_block
1052 duplicate_block (basic_block bb, edge e, basic_block after)
1054 edge s, n;
1055 basic_block new_bb;
1056 gcov_type new_count = e ? e->count : 0;
1057 edge_iterator ei;
1059 if (!cfg_hooks->duplicate_block)
1060 internal_error ("%s does not support duplicate_block",
1061 cfg_hooks->name);
1063 if (bb->count < new_count)
1064 new_count = bb->count;
1066 gcc_checking_assert (can_duplicate_block_p (bb));
1068 new_bb = cfg_hooks->duplicate_block (bb);
1069 if (after)
1070 move_block_after (new_bb, after);
1072 new_bb->flags = bb->flags;
1073 FOR_EACH_EDGE (s, ei, bb->succs)
1075 /* Since we are creating edges from a new block to successors
1076 of another block (which therefore are known to be disjoint), there
1077 is no need to actually check for duplicated edges. */
1078 n = unchecked_make_edge (new_bb, s->dest, s->flags);
1079 n->probability = s->probability;
1080 if (e && bb->count)
1082 /* Take care for overflows! */
1083 n->count = s->count * (new_count * 10000 / bb->count) / 10000;
1084 s->count -= n->count;
1086 else
1087 n->count = s->count;
1088 n->aux = s->aux;
1091 if (e)
1093 new_bb->count = new_count;
1094 bb->count -= new_count;
1096 new_bb->frequency = EDGE_FREQUENCY (e);
1097 bb->frequency -= EDGE_FREQUENCY (e);
1099 redirect_edge_and_branch_force (e, new_bb);
1101 if (bb->count < 0)
1102 bb->count = 0;
1103 if (bb->frequency < 0)
1104 bb->frequency = 0;
1106 else
1108 new_bb->count = bb->count;
1109 new_bb->frequency = bb->frequency;
1112 set_bb_original (new_bb, bb);
1113 set_bb_copy (bb, new_bb);
1115 /* Add the new block to the copy of the loop of BB, or directly to the loop
1116 of BB if the loop is not being copied. */
1117 if (current_loops != NULL)
1119 struct loop *cloop = bb->loop_father;
1120 struct loop *copy = get_loop_copy (cloop);
1121 /* If we copied the loop header block but not the loop
1122 we have created a loop with multiple entries. Ditch the loop,
1123 add the new block to the outer loop and arrange for a fixup. */
1124 if (!copy
1125 && cloop->header == bb)
1127 add_bb_to_loop (new_bb, loop_outer (cloop));
1128 mark_loop_for_removal (cloop);
1130 else
1132 add_bb_to_loop (new_bb, copy ? copy : cloop);
1133 /* If we copied the loop latch block but not the loop, adjust
1134 loop state. */
1135 if (!copy
1136 && cloop->latch == bb)
1138 cloop->latch = NULL;
1139 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
1144 return new_bb;
1147 /* Return 1 if BB ends with a call, possibly followed by some
1148 instructions that must stay with the call, 0 otherwise. */
1150 bool
1151 block_ends_with_call_p (basic_block bb)
1153 if (!cfg_hooks->block_ends_with_call_p)
1154 internal_error ("%s does not support block_ends_with_call_p", cfg_hooks->name);
1156 return (cfg_hooks->block_ends_with_call_p) (bb);
1159 /* Return 1 if BB ends with a conditional branch, 0 otherwise. */
1161 bool
1162 block_ends_with_condjump_p (const_basic_block bb)
1164 if (!cfg_hooks->block_ends_with_condjump_p)
1165 internal_error ("%s does not support block_ends_with_condjump_p",
1166 cfg_hooks->name);
1168 return (cfg_hooks->block_ends_with_condjump_p) (bb);
1171 /* Add fake edges to the function exit for any non constant and non noreturn
1172 calls, volatile inline assembly in the bitmap of blocks specified by
1173 BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
1174 that were split.
1176 The goal is to expose cases in which entering a basic block does not imply
1177 that all subsequent instructions must be executed. */
1180 flow_call_edges_add (sbitmap blocks)
1182 if (!cfg_hooks->flow_call_edges_add)
1183 internal_error ("%s does not support flow_call_edges_add",
1184 cfg_hooks->name);
1186 return (cfg_hooks->flow_call_edges_add) (blocks);
1189 /* This function is called immediately after edge E is added to the
1190 edge vector E->dest->preds. */
1192 void
1193 execute_on_growing_pred (edge e)
1195 if (cfg_hooks->execute_on_growing_pred)
1196 cfg_hooks->execute_on_growing_pred (e);
1199 /* This function is called immediately before edge E is removed from
1200 the edge vector E->dest->preds. */
1202 void
1203 execute_on_shrinking_pred (edge e)
1205 if (cfg_hooks->execute_on_shrinking_pred)
1206 cfg_hooks->execute_on_shrinking_pred (e);
1209 /* This is used inside loop versioning when we want to insert
1210 stmts/insns on the edges, which have a different behavior
1211 in tree's and in RTL, so we made a CFG hook. */
1212 void
1213 lv_flush_pending_stmts (edge e)
1215 if (cfg_hooks->flush_pending_stmts)
1216 cfg_hooks->flush_pending_stmts (e);
1219 /* Loop versioning uses the duplicate_loop_to_header_edge to create
1220 a new version of the loop basic-blocks, the parameters here are
1221 exactly the same as in duplicate_loop_to_header_edge or
1222 tree_duplicate_loop_to_header_edge; while in tree-ssa there is
1223 additional work to maintain ssa information that's why there is
1224 a need to call the tree_duplicate_loop_to_header_edge rather
1225 than duplicate_loop_to_header_edge when we are in tree mode. */
1226 bool
1227 cfg_hook_duplicate_loop_to_header_edge (struct loop *loop, edge e,
1228 unsigned int ndupl,
1229 sbitmap wont_exit, edge orig,
1230 vec<edge> *to_remove,
1231 int flags)
1233 gcc_assert (cfg_hooks->cfg_hook_duplicate_loop_to_header_edge);
1234 return cfg_hooks->cfg_hook_duplicate_loop_to_header_edge (loop, e,
1235 ndupl, wont_exit,
1236 orig, to_remove,
1237 flags);
1240 /* Conditional jumps are represented differently in trees and RTL,
1241 this hook takes a basic block that is known to have a cond jump
1242 at its end and extracts the taken and not taken edges out of it
1243 and store it in E1 and E2 respectively. */
1244 void
1245 extract_cond_bb_edges (basic_block b, edge *e1, edge *e2)
1247 gcc_assert (cfg_hooks->extract_cond_bb_edges);
1248 cfg_hooks->extract_cond_bb_edges (b, e1, e2);
1251 /* Responsible for updating the ssa info (PHI nodes) on the
1252 new condition basic block that guards the versioned loop. */
1253 void
1254 lv_adjust_loop_header_phi (basic_block first, basic_block second,
1255 basic_block new_block, edge e)
1257 if (cfg_hooks->lv_adjust_loop_header_phi)
1258 cfg_hooks->lv_adjust_loop_header_phi (first, second, new_block, e);
1261 /* Conditions in trees and RTL are different so we need
1262 a different handling when we add the condition to the
1263 versioning code. */
1264 void
1265 lv_add_condition_to_bb (basic_block first, basic_block second,
1266 basic_block new_block, void *cond)
1268 gcc_assert (cfg_hooks->lv_add_condition_to_bb);
1269 cfg_hooks->lv_add_condition_to_bb (first, second, new_block, cond);
1272 /* Checks whether all N blocks in BBS array can be copied. */
1273 bool
1274 can_copy_bbs_p (basic_block *bbs, unsigned n)
1276 unsigned i;
1277 edge e;
1278 int ret = true;
1280 for (i = 0; i < n; i++)
1281 bbs[i]->flags |= BB_DUPLICATED;
1283 for (i = 0; i < n; i++)
1285 /* In case we should redirect abnormal edge during duplication, fail. */
1286 edge_iterator ei;
1287 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
1288 if ((e->flags & EDGE_ABNORMAL)
1289 && (e->dest->flags & BB_DUPLICATED))
1291 ret = false;
1292 goto end;
1295 if (!can_duplicate_block_p (bbs[i]))
1297 ret = false;
1298 break;
1302 end:
1303 for (i = 0; i < n; i++)
1304 bbs[i]->flags &= ~BB_DUPLICATED;
1306 return ret;
1309 /* Duplicates N basic blocks stored in array BBS. Newly created basic blocks
1310 are placed into array NEW_BBS in the same order. Edges from basic blocks
1311 in BBS are also duplicated and copies of those that lead into BBS are
1312 redirected to appropriate newly created block. The function assigns bbs
1313 into loops (copy of basic block bb is assigned to bb->loop_father->copy
1314 loop, so this must be set up correctly in advance)
1316 If UPDATE_DOMINANCE is true then this function updates dominators locally
1317 (LOOPS structure that contains the information about dominators is passed
1318 to enable this), otherwise it does not update the dominator information
1319 and it assumed that the caller will do this, perhaps by destroying and
1320 recreating it instead of trying to do an incremental update like this
1321 function does when update_dominance is true.
1323 BASE is the superloop to that basic block belongs; if its header or latch
1324 is copied, we do not set the new blocks as header or latch.
1326 Created copies of N_EDGES edges in array EDGES are stored in array NEW_EDGES,
1327 also in the same order.
1329 Newly created basic blocks are put after the basic block AFTER in the
1330 instruction stream, and the order of the blocks in BBS array is preserved. */
1332 void
1333 copy_bbs (basic_block *bbs, unsigned n, basic_block *new_bbs,
1334 edge *edges, unsigned num_edges, edge *new_edges,
1335 struct loop *base, basic_block after, bool update_dominance)
1337 unsigned i, j;
1338 basic_block bb, new_bb, dom_bb;
1339 edge e;
1341 /* Duplicate bbs, update dominators, assign bbs to loops. */
1342 for (i = 0; i < n; i++)
1344 /* Duplicate. */
1345 bb = bbs[i];
1346 new_bb = new_bbs[i] = duplicate_block (bb, NULL, after);
1347 after = new_bb;
1348 bb->flags |= BB_DUPLICATED;
1349 if (bb->loop_father)
1351 /* Possibly set loop header. */
1352 if (bb->loop_father->header == bb && bb->loop_father != base)
1353 new_bb->loop_father->header = new_bb;
1354 /* Or latch. */
1355 if (bb->loop_father->latch == bb && bb->loop_father != base)
1356 new_bb->loop_father->latch = new_bb;
1360 /* Set dominators. */
1361 if (update_dominance)
1363 for (i = 0; i < n; i++)
1365 bb = bbs[i];
1366 new_bb = new_bbs[i];
1368 dom_bb = get_immediate_dominator (CDI_DOMINATORS, bb);
1369 if (dom_bb->flags & BB_DUPLICATED)
1371 dom_bb = get_bb_copy (dom_bb);
1372 set_immediate_dominator (CDI_DOMINATORS, new_bb, dom_bb);
1377 /* Redirect edges. */
1378 for (j = 0; j < num_edges; j++)
1379 new_edges[j] = NULL;
1380 for (i = 0; i < n; i++)
1382 edge_iterator ei;
1383 new_bb = new_bbs[i];
1384 bb = bbs[i];
1386 FOR_EACH_EDGE (e, ei, new_bb->succs)
1388 for (j = 0; j < num_edges; j++)
1389 if (edges[j] && edges[j]->src == bb && edges[j]->dest == e->dest)
1390 new_edges[j] = e;
1392 if (!(e->dest->flags & BB_DUPLICATED))
1393 continue;
1394 redirect_edge_and_branch_force (e, get_bb_copy (e->dest));
1398 /* Clear information about duplicates. */
1399 for (i = 0; i < n; i++)
1400 bbs[i]->flags &= ~BB_DUPLICATED;
1403 /* Return true if BB contains only labels or non-executable
1404 instructions */
1405 bool
1406 empty_block_p (basic_block bb)
1408 gcc_assert (cfg_hooks->empty_block_p);
1409 return cfg_hooks->empty_block_p (bb);
1412 /* Split a basic block if it ends with a conditional branch and if
1413 the other part of the block is not empty. */
1414 basic_block
1415 split_block_before_cond_jump (basic_block bb)
1417 gcc_assert (cfg_hooks->split_block_before_cond_jump);
1418 return cfg_hooks->split_block_before_cond_jump (bb);
1421 /* Work-horse for passes.c:check_profile_consistency.
1422 Do book-keeping of the CFG for the profile consistency checker.
1423 If AFTER_PASS is 0, do pre-pass accounting, or if AFTER_PASS is 1
1424 then do post-pass accounting. Store the counting in RECORD. */
1426 void
1427 account_profile_record (struct profile_record *record, int after_pass)
1429 basic_block bb;
1430 edge_iterator ei;
1431 edge e;
1432 int sum;
1433 gcov_type lsum;
1435 FOR_ALL_BB_FN (bb, cfun)
1437 if (bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
1438 && profile_status_for_fn (cfun) != PROFILE_ABSENT)
1440 sum = 0;
1441 FOR_EACH_EDGE (e, ei, bb->succs)
1442 sum += e->probability;
1443 if (EDGE_COUNT (bb->succs) && abs (sum - REG_BR_PROB_BASE) > 100)
1444 record->num_mismatched_freq_out[after_pass]++;
1445 lsum = 0;
1446 FOR_EACH_EDGE (e, ei, bb->succs)
1447 lsum += e->count;
1448 if (EDGE_COUNT (bb->succs)
1449 && (lsum - bb->count > 100 || lsum - bb->count < -100))
1450 record->num_mismatched_count_out[after_pass]++;
1452 if (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)
1453 && profile_status_for_fn (cfun) != PROFILE_ABSENT)
1455 sum = 0;
1456 FOR_EACH_EDGE (e, ei, bb->preds)
1457 sum += EDGE_FREQUENCY (e);
1458 if (abs (sum - bb->frequency) > 100
1459 || (MAX (sum, bb->frequency) > 10
1460 && abs ((sum - bb->frequency) * 100 / (MAX (sum, bb->frequency) + 1)) > 10))
1461 record->num_mismatched_freq_in[after_pass]++;
1462 lsum = 0;
1463 FOR_EACH_EDGE (e, ei, bb->preds)
1464 lsum += e->count;
1465 if (lsum - bb->count > 100 || lsum - bb->count < -100)
1466 record->num_mismatched_count_in[after_pass]++;
1468 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)
1469 || bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1470 continue;
1471 gcc_assert (cfg_hooks->account_profile_record);
1472 cfg_hooks->account_profile_record (bb, after_pass, record);