1 /* Control flow graph manipulation code for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* This file contains low level functions to manipulate the CFG and analyze it
23 that are aware of the RTL intermediate language.
25 Available functionality:
26 - Basic CFG/RTL manipulation API documented in cfghooks.h
27 - CFG-aware instruction chain manipulation
28 delete_insn, delete_insn_chain
29 - Edge splitting and committing to edges
30 insert_insn_on_edge, commit_edge_insertions
31 - CFG updating after insn simplification
32 purge_dead_edges, purge_all_dead_edges
34 Functions not supposed for generic use:
35 - Infrastructure to determine quickly basic block for insn
36 compute_bb_for_insn, update_bb_for_insn, set_block_for_insn,
37 - Edge redirection with updating and optimizing of insn chain
38 block_label, tidy_fallthru_edge, force_nonfallthru */
42 #include "coretypes.h"
46 #include "hard-reg-set.h"
47 #include "basic-block.h"
56 #include "insn-config.h"
57 #include "cfglayout.h"
60 /* Stubs in case we don't have a return insn. */
63 #define gen_return() NULL_RTX
66 /* The labels mentioned in non-jump rtl. Valid during find_basic_blocks. */
67 /* ??? Should probably be using LABEL_NUSES instead. It would take a
68 bit of surgery to be able to use or co-opt the routines in jump. */
70 rtx tail_recursion_label_list;
72 static int can_delete_note_p (rtx);
73 static int can_delete_label_p (rtx);
74 static void commit_one_edge_insertion (edge, int);
75 static rtx last_loop_beg_note (rtx);
76 static bool back_edge_of_syntactic_loop_p (basic_block, basic_block);
77 basic_block force_nonfallthru_and_redirect (edge, basic_block);
78 static basic_block rtl_split_edge (edge);
79 static bool rtl_move_block_after (basic_block, basic_block);
80 static int rtl_verify_flow_info (void);
81 static basic_block cfg_layout_split_block (basic_block, void *);
82 static bool cfg_layout_redirect_edge_and_branch (edge, basic_block);
83 static basic_block cfg_layout_redirect_edge_and_branch_force (edge, basic_block);
84 static void cfg_layout_delete_block (basic_block);
85 static void rtl_delete_block (basic_block);
86 static basic_block rtl_redirect_edge_and_branch_force (edge, basic_block);
87 static bool rtl_redirect_edge_and_branch (edge, basic_block);
88 static basic_block rtl_split_block (basic_block, void *);
89 static void rtl_dump_bb (basic_block, FILE *, int);
90 static int rtl_verify_flow_info_1 (void);
91 static void mark_killed_regs (rtx, rtx, void *);
92 static void rtl_make_forwarder_block (edge);
94 /* Return true if NOTE is not one of the ones that must be kept paired,
95 so that we may simply delete it. */
98 can_delete_note_p (rtx note)
100 return (NOTE_LINE_NUMBER (note) == NOTE_INSN_DELETED
101 || NOTE_LINE_NUMBER (note) == NOTE_INSN_BASIC_BLOCK
102 || NOTE_LINE_NUMBER (note) == NOTE_INSN_UNLIKELY_EXECUTED_CODE
103 || NOTE_LINE_NUMBER (note) == NOTE_INSN_PREDICTION);
106 /* True if a given label can be deleted. */
109 can_delete_label_p (rtx label)
111 return (!LABEL_PRESERVE_P (label)
112 /* User declared labels must be preserved. */
113 && LABEL_NAME (label) == 0
114 && !in_expr_list_p (forced_labels, label)
115 && !in_expr_list_p (label_value_list, label));
118 /* Delete INSN by patching it out. Return the next insn. */
121 delete_insn (rtx insn)
123 rtx next = NEXT_INSN (insn);
125 bool really_delete = true;
127 if (GET_CODE (insn) == CODE_LABEL)
129 /* Some labels can't be directly removed from the INSN chain, as they
130 might be references via variables, constant pool etc.
131 Convert them to the special NOTE_INSN_DELETED_LABEL note. */
132 if (! can_delete_label_p (insn))
134 const char *name = LABEL_NAME (insn);
136 really_delete = false;
137 PUT_CODE (insn, NOTE);
138 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED_LABEL;
139 NOTE_SOURCE_FILE (insn) = name;
142 remove_node_from_expr_list (insn, &nonlocal_goto_handler_labels);
147 /* If this insn has already been deleted, something is very wrong. */
148 if (INSN_DELETED_P (insn))
151 INSN_DELETED_P (insn) = 1;
154 /* If deleting a jump, decrement the use count of the label. Deleting
155 the label itself should happen in the normal course of block merging. */
156 if (GET_CODE (insn) == JUMP_INSN
158 && GET_CODE (JUMP_LABEL (insn)) == CODE_LABEL)
159 LABEL_NUSES (JUMP_LABEL (insn))--;
161 /* Also if deleting an insn that references a label. */
164 while ((note = find_reg_note (insn, REG_LABEL, NULL_RTX)) != NULL_RTX
165 && GET_CODE (XEXP (note, 0)) == CODE_LABEL)
167 LABEL_NUSES (XEXP (note, 0))--;
168 remove_note (insn, note);
172 if (GET_CODE (insn) == JUMP_INSN
173 && (GET_CODE (PATTERN (insn)) == ADDR_VEC
174 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
176 rtx pat = PATTERN (insn);
177 int diff_vec_p = GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC;
178 int len = XVECLEN (pat, diff_vec_p);
181 for (i = 0; i < len; i++)
183 rtx label = XEXP (XVECEXP (pat, diff_vec_p, i), 0);
185 /* When deleting code in bulk (e.g. removing many unreachable
186 blocks) we can delete a label that's a target of the vector
187 before deleting the vector itself. */
188 if (GET_CODE (label) != NOTE)
189 LABEL_NUSES (label)--;
196 /* Like delete_insn but also purge dead edges from BB. */
198 delete_insn_and_edges (rtx insn)
204 && BLOCK_FOR_INSN (insn)
205 && BB_END (BLOCK_FOR_INSN (insn)) == insn)
207 x = delete_insn (insn);
209 purge_dead_edges (BLOCK_FOR_INSN (insn));
213 /* Unlink a chain of insns between START and FINISH, leaving notes
214 that must be paired. */
217 delete_insn_chain (rtx start, rtx finish)
221 /* Unchain the insns one by one. It would be quicker to delete all of these
222 with a single unchaining, rather than one at a time, but we need to keep
226 next = NEXT_INSN (start);
227 if (GET_CODE (start) == NOTE && !can_delete_note_p (start))
230 next = delete_insn (start);
238 /* Like delete_insn but also purge dead edges from BB. */
240 delete_insn_chain_and_edges (rtx first, rtx last)
245 && BLOCK_FOR_INSN (last)
246 && BB_END (BLOCK_FOR_INSN (last)) == last)
248 delete_insn_chain (first, last);
250 purge_dead_edges (BLOCK_FOR_INSN (last));
253 /* Create a new basic block consisting of the instructions between HEAD and END
254 inclusive. This function is designed to allow fast BB construction - reuses
255 the note and basic block struct in BB_NOTE, if any and do not grow
256 BASIC_BLOCK chain and should be used directly only by CFG construction code.
257 END can be NULL in to create new empty basic block before HEAD. Both END
258 and HEAD can be NULL to create basic block at the end of INSN chain.
259 AFTER is the basic block we should be put after. */
262 create_basic_block_structure (rtx head, rtx end, rtx bb_note, basic_block after)
267 && ! RTX_INTEGRATED_P (bb_note)
268 && (bb = NOTE_BASIC_BLOCK (bb_note)) != NULL
271 /* If we found an existing note, thread it back onto the chain. */
275 if (GET_CODE (head) == CODE_LABEL)
279 after = PREV_INSN (head);
283 if (after != bb_note && NEXT_INSN (after) != bb_note)
284 reorder_insns_nobb (bb_note, bb_note, after);
288 /* Otherwise we must create a note and a basic block structure. */
294 = emit_note_after (NOTE_INSN_BASIC_BLOCK, get_last_insn ());
295 else if (GET_CODE (head) == CODE_LABEL && end)
297 bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, head);
303 bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, head);
309 NOTE_BASIC_BLOCK (bb_note) = bb;
312 /* Always include the bb note in the block. */
313 if (NEXT_INSN (end) == bb_note)
318 bb->index = last_basic_block++;
320 link_block (bb, after);
321 BASIC_BLOCK (bb->index) = bb;
322 update_bb_for_insn (bb);
323 bb->partition = UNPARTITIONED;
325 /* Tag the block so that we know it has been used when considering
326 other basic block notes. */
332 /* Create new basic block consisting of instructions in between HEAD and END
333 and place it to the BB chain after block AFTER. END can be NULL in to
334 create new empty basic block before HEAD. Both END and HEAD can be NULL to
335 create basic block at the end of INSN chain. */
338 rtl_create_basic_block (void *headp, void *endp, basic_block after)
340 rtx head = headp, end = endp;
343 /* Grow the basic block array if needed. */
344 if ((size_t) last_basic_block >= VARRAY_SIZE (basic_block_info))
346 size_t new_size = last_basic_block + (last_basic_block + 3) / 4;
347 VARRAY_GROW (basic_block_info, new_size);
352 bb = create_basic_block_structure (head, end, NULL, after);
358 cfg_layout_create_basic_block (void *head, void *end, basic_block after)
360 basic_block newbb = rtl_create_basic_block (head, end, after);
362 cfg_layout_initialize_rbi (newbb);
366 /* Delete the insns in a (non-live) block. We physically delete every
367 non-deleted-note insn, and update the flow graph appropriately.
369 Return nonzero if we deleted an exception handler. */
371 /* ??? Preserving all such notes strikes me as wrong. It would be nice
372 to post-process the stream to remove empty blocks, loops, ranges, etc. */
375 rtl_delete_block (basic_block b)
379 /* If the head of this block is a CODE_LABEL, then it might be the
380 label for an exception handler which can't be reached.
382 We need to remove the label from the exception_handler_label list
383 and remove the associated NOTE_INSN_EH_REGION_BEG and
384 NOTE_INSN_EH_REGION_END notes. */
386 /* Get rid of all NOTE_INSN_PREDICTIONs and NOTE_INSN_LOOP_CONTs
387 hanging before the block. */
389 for (insn = PREV_INSN (BB_HEAD (b)); insn; insn = PREV_INSN (insn))
391 if (GET_CODE (insn) != NOTE)
393 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_PREDICTION
394 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT)
395 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
400 never_reached_warning (insn, BB_END (b));
402 if (GET_CODE (insn) == CODE_LABEL)
403 maybe_remove_eh_handler (insn);
405 /* Include any jump table following the basic block. */
407 if (tablejump_p (end, NULL, &tmp))
410 /* Include any barrier that may follow the basic block. */
411 tmp = next_nonnote_insn (end);
412 if (tmp && GET_CODE (tmp) == BARRIER)
415 /* Selectively delete the entire chain. */
417 delete_insn_chain (insn, end);
420 /* Records the basic block struct in BLOCK_FOR_INSN for every insn. */
423 compute_bb_for_insn (void)
429 rtx end = BB_END (bb);
432 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
434 BLOCK_FOR_INSN (insn) = bb;
441 /* Release the basic_block_for_insn array. */
444 free_bb_for_insn (void)
447 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
448 if (GET_CODE (insn) != BARRIER)
449 BLOCK_FOR_INSN (insn) = NULL;
452 /* Update insns block within BB. */
455 update_bb_for_insn (basic_block bb)
459 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
461 if (GET_CODE (insn) != BARRIER)
462 set_block_for_insn (insn, bb);
463 if (insn == BB_END (bb))
468 /* Creates a new basic block just after basic block B by splitting
469 everything after specified instruction I. */
472 rtl_split_block (basic_block bb, void *insnp)
480 insn = first_insn_after_basic_block_note (bb);
483 insn = PREV_INSN (insn);
485 insn = get_last_insn ();
488 /* We probably should check type of the insn so that we do not create
489 inconsistent cfg. It is checked in verify_flow_info anyway, so do not
491 if (insn == BB_END (bb))
492 emit_note_after (NOTE_INSN_DELETED, insn);
494 /* Create the new basic block. */
495 new_bb = create_basic_block (NEXT_INSN (insn), BB_END (bb), bb);
498 /* Redirect the outgoing edges. */
499 new_bb->succ = bb->succ;
501 for (e = new_bb->succ; e; e = e->succ_next)
504 if (bb->global_live_at_start)
506 new_bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (&flow_obstack);
507 new_bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (&flow_obstack);
508 COPY_REG_SET (new_bb->global_live_at_end, bb->global_live_at_end);
510 /* We now have to calculate which registers are live at the end
511 of the split basic block and at the start of the new basic
512 block. Start with those registers that are known to be live
513 at the end of the original basic block and get
514 propagate_block to determine which registers are live. */
515 COPY_REG_SET (new_bb->global_live_at_start, bb->global_live_at_end);
516 propagate_block (new_bb, new_bb->global_live_at_start, NULL, NULL, 0);
517 COPY_REG_SET (bb->global_live_at_end,
518 new_bb->global_live_at_start);
519 #ifdef HAVE_conditional_execution
520 /* In the presence of conditional execution we are not able to update
521 liveness precisely. */
522 if (reload_completed)
524 bb->flags |= BB_DIRTY;
525 new_bb->flags |= BB_DIRTY;
533 /* Blocks A and B are to be merged into a single block A. The insns
534 are already contiguous. */
537 rtl_merge_blocks (basic_block a, basic_block b)
539 rtx b_head = BB_HEAD (b), b_end = BB_END (b), a_end = BB_END (a);
540 rtx del_first = NULL_RTX, del_last = NULL_RTX;
543 /* If there was a CODE_LABEL beginning B, delete it. */
544 if (GET_CODE (b_head) == CODE_LABEL)
546 /* Detect basic blocks with nothing but a label. This can happen
547 in particular at the end of a function. */
551 del_first = del_last = b_head;
552 b_head = NEXT_INSN (b_head);
555 /* Delete the basic block note and handle blocks containing just that
557 if (NOTE_INSN_BASIC_BLOCK_P (b_head))
565 b_head = NEXT_INSN (b_head);
568 /* If there was a jump out of A, delete it. */
569 if (GET_CODE (a_end) == JUMP_INSN)
573 for (prev = PREV_INSN (a_end); ; prev = PREV_INSN (prev))
574 if (GET_CODE (prev) != NOTE
575 || NOTE_LINE_NUMBER (prev) == NOTE_INSN_BASIC_BLOCK
576 || prev == BB_HEAD (a))
582 /* If this was a conditional jump, we need to also delete
583 the insn that set cc0. */
584 if (only_sets_cc0_p (prev))
588 prev = prev_nonnote_insn (prev);
595 a_end = PREV_INSN (del_first);
597 else if (GET_CODE (NEXT_INSN (a_end)) == BARRIER)
598 del_first = NEXT_INSN (a_end);
600 /* Delete everything marked above as well as crap that might be
601 hanging out between the two blocks. */
603 delete_insn_chain (del_first, del_last);
605 /* Reassociate the insns of B with A. */
610 for (x = a_end; x != b_end; x = NEXT_INSN (x))
611 set_block_for_insn (x, a);
613 set_block_for_insn (b_end, a);
621 /* Return true when block A and B can be merged. */
623 rtl_can_merge_blocks (basic_block a,basic_block b)
625 bool partitions_ok = true;
627 /* If we are partitioning hot/cold basic blocks, we don't want to
628 mess up unconditional or indirect jumps that cross between hot
629 and cold sections. */
631 if (flag_reorder_blocks_and_partition
632 && (find_reg_note (BB_END (a), REG_CROSSING_JUMP, NULL_RTX)
633 || find_reg_note (BB_END (b), REG_CROSSING_JUMP, NULL_RTX)
634 || a->partition != b->partition))
635 partitions_ok = false;
637 /* There must be exactly one edge in between the blocks. */
638 return (a->succ && !a->succ->succ_next && a->succ->dest == b
639 && !b->pred->pred_next && a != b
640 /* Must be simple edge. */
641 && !(a->succ->flags & EDGE_COMPLEX)
644 && a != ENTRY_BLOCK_PTR && b != EXIT_BLOCK_PTR
645 /* If the jump insn has side effects,
646 we can't kill the edge. */
647 && (GET_CODE (BB_END (a)) != JUMP_INSN
649 ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
652 /* Return the label in the head of basic block BLOCK. Create one if it doesn't
656 block_label (basic_block block)
658 if (block == EXIT_BLOCK_PTR)
661 if (GET_CODE (BB_HEAD (block)) != CODE_LABEL)
663 BB_HEAD (block) = emit_label_before (gen_label_rtx (), BB_HEAD (block));
666 return BB_HEAD (block);
669 /* Attempt to perform edge redirection by replacing possibly complex jump
670 instruction by unconditional jump or removing jump completely. This can
671 apply only if all edges now point to the same block. The parameters and
672 return values are equivalent to redirect_edge_and_branch. */
675 try_redirect_by_replacing_jump (edge e, basic_block target, bool in_cfglayout)
677 basic_block src = e->src;
678 rtx insn = BB_END (src), kill_from;
684 /* If we are partitioning hot/cold basic blocks, we don't want to
685 mess up unconditional or indirect jumps that cross between hot
686 and cold sections. */
688 if (flag_reorder_blocks_and_partition
689 && find_reg_note (insn, REG_CROSSING_JUMP, NULL_RTX))
692 /* Verify that all targets will be TARGET. */
693 for (tmp = src->succ; tmp; tmp = tmp->succ_next)
694 if (tmp->dest != target && tmp != e)
697 if (tmp || !onlyjump_p (insn))
699 if ((!optimize || reload_completed) && tablejump_p (insn, NULL, NULL))
702 /* Avoid removing branch with side effects. */
703 set = single_set (insn);
704 if (!set || side_effects_p (set))
707 /* In case we zap a conditional jump, we'll need to kill
708 the cc0 setter too. */
711 if (reg_mentioned_p (cc0_rtx, PATTERN (insn)))
712 kill_from = PREV_INSN (insn);
715 /* See if we can create the fallthru edge. */
716 if (in_cfglayout || can_fallthru (src, target))
719 fprintf (dump_file, "Removing jump %i.\n", INSN_UID (insn));
722 /* Selectively unlink whole insn chain. */
725 rtx insn = src->rbi->footer;
727 delete_insn_chain (kill_from, BB_END (src));
729 /* Remove barriers but keep jumptables. */
732 if (GET_CODE (insn) == BARRIER)
734 if (PREV_INSN (insn))
735 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
737 src->rbi->footer = NEXT_INSN (insn);
738 if (NEXT_INSN (insn))
739 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
741 if (GET_CODE (insn) == CODE_LABEL)
743 insn = NEXT_INSN (insn);
747 delete_insn_chain (kill_from, PREV_INSN (BB_HEAD (target)));
750 /* If this already is simplejump, redirect it. */
751 else if (simplejump_p (insn))
753 if (e->dest == target)
756 fprintf (dump_file, "Redirecting jump %i from %i to %i.\n",
757 INSN_UID (insn), e->dest->index, target->index);
758 if (!redirect_jump (insn, block_label (target), 0))
760 if (target == EXIT_BLOCK_PTR)
766 /* Cannot do anything for target exit block. */
767 else if (target == EXIT_BLOCK_PTR)
770 /* Or replace possibly complicated jump insn by simple jump insn. */
773 rtx target_label = block_label (target);
774 rtx barrier, label, table;
776 emit_jump_insn_after (gen_jump (target_label), insn);
777 JUMP_LABEL (BB_END (src)) = target_label;
778 LABEL_NUSES (target_label)++;
780 fprintf (dump_file, "Replacing insn %i by jump %i\n",
781 INSN_UID (insn), INSN_UID (BB_END (src)));
784 delete_insn_chain (kill_from, insn);
786 /* Recognize a tablejump that we are converting to a
787 simple jump and remove its associated CODE_LABEL
788 and ADDR_VEC or ADDR_DIFF_VEC. */
789 if (tablejump_p (insn, &label, &table))
790 delete_insn_chain (label, table);
792 barrier = next_nonnote_insn (BB_END (src));
793 if (!barrier || GET_CODE (barrier) != BARRIER)
794 emit_barrier_after (BB_END (src));
797 if (barrier != NEXT_INSN (BB_END (src)))
799 /* Move the jump before barrier so that the notes
800 which originally were or were created before jump table are
801 inside the basic block. */
802 rtx new_insn = BB_END (src);
805 for (tmp = NEXT_INSN (BB_END (src)); tmp != barrier;
806 tmp = NEXT_INSN (tmp))
807 set_block_for_insn (tmp, src);
809 NEXT_INSN (PREV_INSN (new_insn)) = NEXT_INSN (new_insn);
810 PREV_INSN (NEXT_INSN (new_insn)) = PREV_INSN (new_insn);
812 NEXT_INSN (new_insn) = barrier;
813 NEXT_INSN (PREV_INSN (barrier)) = new_insn;
815 PREV_INSN (new_insn) = PREV_INSN (barrier);
816 PREV_INSN (barrier) = new_insn;
821 /* Keep only one edge out and set proper flags. */
822 while (src->succ->succ_next)
823 remove_edge (src->succ);
826 e->flags = EDGE_FALLTHRU;
830 e->probability = REG_BR_PROB_BASE;
831 e->count = src->count;
833 /* We don't want a block to end on a line-number note since that has
834 the potential of changing the code between -g and not -g. */
835 while (GET_CODE (BB_END (e->src)) == NOTE
836 && NOTE_LINE_NUMBER (BB_END (e->src)) >= 0)
837 delete_insn (BB_END (e->src));
839 if (e->dest != target)
840 redirect_edge_succ (e, target);
845 /* Return last loop_beg note appearing after INSN, before start of next
846 basic block. Return INSN if there are no such notes.
848 When emitting jump to redirect a fallthru edge, it should always appear
849 after the LOOP_BEG notes, as loop optimizer expect loop to either start by
850 fallthru edge or jump following the LOOP_BEG note jumping to the loop exit
854 last_loop_beg_note (rtx insn)
858 for (insn = NEXT_INSN (insn); insn && GET_CODE (insn) == NOTE
859 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_BASIC_BLOCK;
860 insn = NEXT_INSN (insn))
861 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
867 /* Redirect edge representing branch of (un)conditional jump or tablejump. */
869 redirect_branch_edge (edge e, basic_block target)
872 rtx old_label = BB_HEAD (e->dest);
873 basic_block src = e->src;
874 rtx insn = BB_END (src);
876 /* We can only redirect non-fallthru edges of jump insn. */
877 if (e->flags & EDGE_FALLTHRU)
879 else if (GET_CODE (insn) != JUMP_INSN)
882 /* Recognize a tablejump and adjust all matching cases. */
883 if (tablejump_p (insn, NULL, &tmp))
887 rtx new_label = block_label (target);
889 if (target == EXIT_BLOCK_PTR)
891 if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
892 vec = XVEC (PATTERN (tmp), 0);
894 vec = XVEC (PATTERN (tmp), 1);
896 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
897 if (XEXP (RTVEC_ELT (vec, j), 0) == old_label)
899 RTVEC_ELT (vec, j) = gen_rtx_LABEL_REF (Pmode, new_label);
900 --LABEL_NUSES (old_label);
901 ++LABEL_NUSES (new_label);
904 /* Handle casesi dispatch insns. */
905 if ((tmp = single_set (insn)) != NULL
906 && SET_DEST (tmp) == pc_rtx
907 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
908 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF
909 && XEXP (XEXP (SET_SRC (tmp), 2), 0) == old_label)
911 XEXP (SET_SRC (tmp), 2) = gen_rtx_LABEL_REF (VOIDmode,
913 --LABEL_NUSES (old_label);
914 ++LABEL_NUSES (new_label);
919 /* ?? We may play the games with moving the named labels from
920 one basic block to the other in case only one computed_jump is
922 if (computed_jump_p (insn)
923 /* A return instruction can't be redirected. */
924 || returnjump_p (insn))
927 /* If the insn doesn't go where we think, we're confused. */
928 if (JUMP_LABEL (insn) != old_label)
931 /* If the substitution doesn't succeed, die. This can happen
932 if the back end emitted unrecognizable instructions or if
933 target is exit block on some arches. */
934 if (!redirect_jump (insn, block_label (target), 0))
936 if (target == EXIT_BLOCK_PTR)
943 fprintf (dump_file, "Edge %i->%i redirected to %i\n",
944 e->src->index, e->dest->index, target->index);
946 if (e->dest != target)
947 redirect_edge_succ_nodup (e, target);
951 /* Attempt to change code to redirect edge E to TARGET. Don't do that on
952 expense of adding new instructions or reordering basic blocks.
954 Function can be also called with edge destination equivalent to the TARGET.
955 Then it should try the simplifications and do nothing if none is possible.
957 Return true if transformation succeeded. We still return false in case E
958 already destinated TARGET and we didn't managed to simplify instruction
962 rtl_redirect_edge_and_branch (edge e, basic_block target)
964 basic_block src = e->src;
966 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
969 if (e->dest == target)
972 if (try_redirect_by_replacing_jump (e, target, false))
974 src->flags |= BB_DIRTY;
978 if (!redirect_branch_edge (e, target))
981 src->flags |= BB_DIRTY;
985 /* Like force_nonfallthru below, but additionally performs redirection
986 Used by redirect_edge_and_branch_force. */
989 force_nonfallthru_and_redirect (edge e, basic_block target)
991 basic_block jump_block, new_bb = NULL, src = e->src;
994 int abnormal_edge_flags = 0;
996 /* In the case the last instruction is conditional jump to the next
997 instruction, first redirect the jump itself and then continue
998 by creating a basic block afterwards to redirect fallthru edge. */
999 if (e->src != ENTRY_BLOCK_PTR && e->dest != EXIT_BLOCK_PTR
1000 && any_condjump_p (BB_END (e->src))
1001 /* When called from cfglayout, fallthru edges do not
1002 necessarily go to the next block. */
1003 && e->src->next_bb == e->dest
1004 && JUMP_LABEL (BB_END (e->src)) == BB_HEAD (e->dest))
1007 edge b = unchecked_make_edge (e->src, target, 0);
1009 if (!redirect_jump (BB_END (e->src), block_label (target), 0))
1011 note = find_reg_note (BB_END (e->src), REG_BR_PROB, NULL_RTX);
1014 int prob = INTVAL (XEXP (note, 0));
1016 b->probability = prob;
1017 b->count = e->count * prob / REG_BR_PROB_BASE;
1018 e->probability -= e->probability;
1019 e->count -= b->count;
1020 if (e->probability < 0)
1027 if (e->flags & EDGE_ABNORMAL)
1029 /* Irritating special case - fallthru edge to the same block as abnormal
1031 We can't redirect abnormal edge, but we still can split the fallthru
1032 one and create separate abnormal edge to original destination.
1033 This allows bb-reorder to make such edge non-fallthru. */
1034 if (e->dest != target)
1036 abnormal_edge_flags = e->flags & ~(EDGE_FALLTHRU | EDGE_CAN_FALLTHRU);
1037 e->flags &= EDGE_FALLTHRU | EDGE_CAN_FALLTHRU;
1039 else if (!(e->flags & EDGE_FALLTHRU))
1041 else if (e->src == ENTRY_BLOCK_PTR)
1043 /* We can't redirect the entry block. Create an empty block at the
1044 start of the function which we use to add the new jump. */
1046 basic_block bb = create_basic_block (BB_HEAD (e->dest), NULL, ENTRY_BLOCK_PTR);
1048 /* Change the existing edge's source to be the new block, and add
1049 a new edge from the entry block to the new block. */
1051 for (pe1 = &ENTRY_BLOCK_PTR->succ; *pe1; pe1 = &(*pe1)->succ_next)
1054 *pe1 = e->succ_next;
1059 make_single_succ_edge (ENTRY_BLOCK_PTR, bb, EDGE_FALLTHRU);
1062 if (e->src->succ->succ_next || abnormal_edge_flags)
1064 /* Create the new structures. */
1066 /* If the old block ended with a tablejump, skip its table
1067 by searching forward from there. Otherwise start searching
1068 forward from the last instruction of the old block. */
1069 if (!tablejump_p (BB_END (e->src), NULL, ¬e))
1070 note = BB_END (e->src);
1072 /* Position the new block correctly relative to loop notes. */
1073 note = last_loop_beg_note (note);
1074 note = NEXT_INSN (note);
1076 jump_block = create_basic_block (note, NULL, e->src);
1077 jump_block->count = e->count;
1078 jump_block->frequency = EDGE_FREQUENCY (e);
1079 jump_block->loop_depth = target->loop_depth;
1081 if (target->global_live_at_start)
1083 jump_block->global_live_at_start
1084 = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1085 jump_block->global_live_at_end
1086 = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1087 COPY_REG_SET (jump_block->global_live_at_start,
1088 target->global_live_at_start);
1089 COPY_REG_SET (jump_block->global_live_at_end,
1090 target->global_live_at_start);
1093 /* Make sure new block ends up in correct hot/cold section. */
1095 jump_block->partition = e->src->partition;
1096 if (flag_reorder_blocks_and_partition)
1098 if (e->src->partition == COLD_PARTITION)
1100 rtx bb_note, new_note;
1101 for (bb_note = BB_HEAD (jump_block);
1102 bb_note && bb_note != NEXT_INSN (BB_END (jump_block));
1103 bb_note = NEXT_INSN (bb_note))
1104 if (GET_CODE (bb_note) == NOTE
1105 && NOTE_LINE_NUMBER (bb_note) == NOTE_INSN_BASIC_BLOCK)
1107 new_note = emit_note_after (NOTE_INSN_UNLIKELY_EXECUTED_CODE,
1109 NOTE_BASIC_BLOCK (new_note) = jump_block;
1110 jump_block->partition = COLD_PARTITION;
1112 if (GET_CODE (BB_END (jump_block)) == JUMP_INSN
1113 && !any_condjump_p (BB_END (jump_block))
1114 && jump_block->succ->crossing_edge )
1115 REG_NOTES (BB_END (jump_block)) = gen_rtx_EXPR_LIST
1116 (REG_CROSSING_JUMP, NULL_RTX,
1117 REG_NOTES (BB_END (jump_block)));
1121 new_edge = make_edge (e->src, jump_block, EDGE_FALLTHRU);
1122 new_edge->probability = e->probability;
1123 new_edge->count = e->count;
1125 /* Redirect old edge. */
1126 redirect_edge_pred (e, jump_block);
1127 e->probability = REG_BR_PROB_BASE;
1129 new_bb = jump_block;
1132 jump_block = e->src;
1134 e->flags &= ~EDGE_FALLTHRU;
1135 if (target == EXIT_BLOCK_PTR)
1138 emit_jump_insn_after (gen_return (), BB_END (jump_block));
1144 rtx label = block_label (target);
1145 emit_jump_insn_after (gen_jump (label), BB_END (jump_block));
1146 JUMP_LABEL (BB_END (jump_block)) = label;
1147 LABEL_NUSES (label)++;
1150 emit_barrier_after (BB_END (jump_block));
1151 redirect_edge_succ_nodup (e, target);
1153 if (abnormal_edge_flags)
1154 make_edge (src, target, abnormal_edge_flags);
1159 /* Edge E is assumed to be fallthru edge. Emit needed jump instruction
1160 (and possibly create new basic block) to make edge non-fallthru.
1161 Return newly created BB or NULL if none. */
1164 force_nonfallthru (edge e)
1166 return force_nonfallthru_and_redirect (e, e->dest);
1169 /* Redirect edge even at the expense of creating new jump insn or
1170 basic block. Return new basic block if created, NULL otherwise.
1171 Abort if conversion is impossible. */
1174 rtl_redirect_edge_and_branch_force (edge e, basic_block target)
1176 if (redirect_edge_and_branch (e, target)
1177 || e->dest == target)
1180 /* In case the edge redirection failed, try to force it to be non-fallthru
1181 and redirect newly created simplejump. */
1182 return force_nonfallthru_and_redirect (e, target);
1185 /* The given edge should potentially be a fallthru edge. If that is in
1186 fact true, delete the jump and barriers that are in the way. */
1189 rtl_tidy_fallthru_edge (edge e)
1192 basic_block b = e->src, c = b->next_bb;
1194 /* ??? In a late-running flow pass, other folks may have deleted basic
1195 blocks by nopping out blocks, leaving multiple BARRIERs between here
1196 and the target label. They ought to be chastized and fixed.
1198 We can also wind up with a sequence of undeletable labels between
1199 one block and the next.
1201 So search through a sequence of barriers, labels, and notes for
1202 the head of block C and assert that we really do fall through. */
1204 for (q = NEXT_INSN (BB_END (b)); q != BB_HEAD (c); q = NEXT_INSN (q))
1208 /* Remove what will soon cease being the jump insn from the source block.
1209 If block B consisted only of this single jump, turn it into a deleted
1212 if (GET_CODE (q) == JUMP_INSN
1214 && (any_uncondjump_p (q)
1215 || (b->succ == e && e->succ_next == NULL)))
1218 /* If this was a conditional jump, we need to also delete
1219 the insn that set cc0. */
1220 if (any_condjump_p (q) && only_sets_cc0_p (PREV_INSN (q)))
1226 /* We don't want a block to end on a line-number note since that has
1227 the potential of changing the code between -g and not -g. */
1228 while (GET_CODE (q) == NOTE && NOTE_LINE_NUMBER (q) >= 0)
1232 /* Selectively unlink the sequence. */
1233 if (q != PREV_INSN (BB_HEAD (c)))
1234 delete_insn_chain (NEXT_INSN (q), PREV_INSN (BB_HEAD (c)));
1236 e->flags |= EDGE_FALLTHRU;
1239 /* Helper function for split_edge. Return true in case edge BB2 to BB1
1240 is back edge of syntactic loop. */
1243 back_edge_of_syntactic_loop_p (basic_block bb1, basic_block bb2)
1252 /* ??? Could we guarantee that bb indices are monotone, so that we could
1253 just compare them? */
1254 for (bb = bb1; bb && bb != bb2; bb = bb->next_bb)
1260 for (insn = BB_END (bb1); insn != BB_HEAD (bb2) && count >= 0;
1261 insn = NEXT_INSN (insn))
1262 if (GET_CODE (insn) == NOTE)
1264 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
1266 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
1273 /* Should move basic block BB after basic block AFTER. NIY. */
1276 rtl_move_block_after (basic_block bb ATTRIBUTE_UNUSED,
1277 basic_block after ATTRIBUTE_UNUSED)
1282 /* Split a (typically critical) edge. Return the new block.
1283 Abort on abnormal edges.
1285 ??? The code generally expects to be called on critical edges.
1286 The case of a block ending in an unconditional jump to a
1287 block with multiple predecessors is not handled optimally. */
1290 rtl_split_edge (edge edge_in)
1295 /* Abnormal edges cannot be split. */
1296 if ((edge_in->flags & EDGE_ABNORMAL) != 0)
1299 /* We are going to place the new block in front of edge destination.
1300 Avoid existence of fallthru predecessors. */
1301 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1305 for (e = edge_in->dest->pred; e; e = e->pred_next)
1306 if (e->flags & EDGE_FALLTHRU)
1310 force_nonfallthru (e);
1313 /* Create the basic block note.
1315 Where we place the note can have a noticeable impact on the generated
1316 code. Consider this cfg:
1326 If we need to insert an insn on the edge from block 0 to block 1,
1327 we want to ensure the instructions we insert are outside of any
1328 loop notes that physically sit between block 0 and block 1. Otherwise
1329 we confuse the loop optimizer into thinking the loop is a phony. */
1331 if (edge_in->dest != EXIT_BLOCK_PTR
1332 && PREV_INSN (BB_HEAD (edge_in->dest))
1333 && GET_CODE (PREV_INSN (BB_HEAD (edge_in->dest))) == NOTE
1334 && (NOTE_LINE_NUMBER (PREV_INSN (BB_HEAD (edge_in->dest)))
1335 == NOTE_INSN_LOOP_BEG)
1336 && !back_edge_of_syntactic_loop_p (edge_in->dest, edge_in->src))
1337 before = PREV_INSN (BB_HEAD (edge_in->dest));
1338 else if (edge_in->dest != EXIT_BLOCK_PTR)
1339 before = BB_HEAD (edge_in->dest);
1343 bb = create_basic_block (before, NULL, edge_in->dest->prev_bb);
1345 /* ??? This info is likely going to be out of date very soon. */
1346 if (edge_in->dest->global_live_at_start)
1348 bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1349 bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1350 COPY_REG_SET (bb->global_live_at_start,
1351 edge_in->dest->global_live_at_start);
1352 COPY_REG_SET (bb->global_live_at_end,
1353 edge_in->dest->global_live_at_start);
1356 make_single_succ_edge (bb, edge_in->dest, EDGE_FALLTHRU);
1358 /* For non-fallthru edges, we must adjust the predecessor's
1359 jump instruction to target our new block. */
1360 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1362 if (!redirect_edge_and_branch (edge_in, bb))
1366 redirect_edge_succ (edge_in, bb);
1371 /* Queue instructions for insertion on an edge between two basic blocks.
1372 The new instructions and basic blocks (if any) will not appear in the
1373 CFG until commit_edge_insertions is called. */
1376 insert_insn_on_edge (rtx pattern, edge e)
1378 /* We cannot insert instructions on an abnormal critical edge.
1379 It will be easier to find the culprit if we die now. */
1380 if ((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e))
1383 if (e->insns == NULL_RTX)
1386 push_to_sequence (e->insns);
1388 emit_insn (pattern);
1390 e->insns = get_insns ();
1394 /* Called from safe_insert_insn_on_edge through note_stores, marks live
1395 registers that are killed by the store. */
1397 mark_killed_regs (rtx reg, rtx set ATTRIBUTE_UNUSED, void *data)
1399 regset killed = data;
1402 if (GET_CODE (reg) == SUBREG)
1403 reg = SUBREG_REG (reg);
1406 regno = REGNO (reg);
1407 if (regno >= FIRST_PSEUDO_REGISTER)
1408 SET_REGNO_REG_SET (killed, regno);
1411 for (i = 0; i < (int) hard_regno_nregs[regno][GET_MODE (reg)]; i++)
1412 SET_REGNO_REG_SET (killed, regno + i);
1416 /* Similar to insert_insn_on_edge, tries to put INSN to edge E. Additionally
1417 it checks whether this will not clobber the registers that are live on the
1418 edge (i.e. it requires liveness information to be up-to-date) and if there
1419 are some, then it tries to save and restore them. Returns true if
1422 safe_insert_insn_on_edge (rtx insn, edge e)
1425 regset_head killed_head;
1426 regset killed = INITIALIZE_REG_SET (killed_head);
1427 rtx save_regs = NULL_RTX;
1428 int regno, noccmode;
1429 enum machine_mode mode;
1431 #ifdef AVOID_CCMODE_COPIES
1437 for (x = insn; x; x = NEXT_INSN (x))
1439 note_stores (PATTERN (x), mark_killed_regs, killed);
1440 bitmap_operation (killed, killed, e->dest->global_live_at_start,
1443 EXECUTE_IF_SET_IN_REG_SET (killed, 0, regno,
1445 mode = regno < FIRST_PSEUDO_REGISTER
1446 ? reg_raw_mode[regno]
1447 : GET_MODE (regno_reg_rtx[regno]);
1448 if (mode == VOIDmode)
1451 if (noccmode && mode == CCmode)
1454 save_regs = alloc_EXPR_LIST (0,
1457 gen_raw_REG (mode, regno)),
1466 for (x = save_regs; x; x = XEXP (x, 1))
1468 from = XEXP (XEXP (x, 0), 1);
1469 to = XEXP (XEXP (x, 0), 0);
1470 emit_move_insn (to, from);
1473 for (x = save_regs; x; x = XEXP (x, 1))
1475 from = XEXP (XEXP (x, 0), 0);
1476 to = XEXP (XEXP (x, 0), 1);
1477 emit_move_insn (to, from);
1479 insn = get_insns ();
1481 free_EXPR_LIST_list (&save_regs);
1483 insert_insn_on_edge (insn, e);
1485 FREE_REG_SET (killed);
1489 /* Update the CFG for the instructions queued on edge E. */
1492 commit_one_edge_insertion (edge e, int watch_calls)
1494 rtx before = NULL_RTX, after = NULL_RTX, insns, tmp, last;
1495 basic_block bb = NULL;
1497 /* Pull the insns off the edge now since the edge might go away. */
1499 e->insns = NULL_RTX;
1501 /* Special case -- avoid inserting code between call and storing
1502 its return value. */
1503 if (watch_calls && (e->flags & EDGE_FALLTHRU) && !e->dest->pred->pred_next
1504 && e->src != ENTRY_BLOCK_PTR
1505 && GET_CODE (BB_END (e->src)) == CALL_INSN)
1507 rtx next = next_nonnote_insn (BB_END (e->src));
1509 after = BB_HEAD (e->dest);
1510 /* The first insn after the call may be a stack pop, skip it. */
1512 && keep_with_call_p (next))
1515 next = next_nonnote_insn (next);
1519 if (!before && !after)
1521 /* Figure out where to put these things. If the destination has
1522 one predecessor, insert there. Except for the exit block. */
1523 if (e->dest->pred->pred_next == NULL && e->dest != EXIT_BLOCK_PTR)
1527 /* Get the location correct wrt a code label, and "nice" wrt
1528 a basic block note, and before everything else. */
1530 if (GET_CODE (tmp) == CODE_LABEL)
1531 tmp = NEXT_INSN (tmp);
1532 if (NOTE_INSN_BASIC_BLOCK_P (tmp))
1533 tmp = NEXT_INSN (tmp);
1535 && GET_CODE (tmp) == NOTE
1536 && NOTE_LINE_NUMBER (tmp) == NOTE_INSN_UNLIKELY_EXECUTED_CODE)
1537 tmp = NEXT_INSN (tmp);
1538 if (tmp == BB_HEAD (bb))
1541 after = PREV_INSN (tmp);
1543 after = get_last_insn ();
1546 /* If the source has one successor and the edge is not abnormal,
1547 insert there. Except for the entry block. */
1548 else if ((e->flags & EDGE_ABNORMAL) == 0
1549 && e->src->succ->succ_next == NULL
1550 && e->src != ENTRY_BLOCK_PTR)
1554 /* It is possible to have a non-simple jump here. Consider a target
1555 where some forms of unconditional jumps clobber a register. This
1556 happens on the fr30 for example.
1558 We know this block has a single successor, so we can just emit
1559 the queued insns before the jump. */
1560 if (GET_CODE (BB_END (bb)) == JUMP_INSN)
1561 for (before = BB_END (bb);
1562 GET_CODE (PREV_INSN (before)) == NOTE
1563 && NOTE_LINE_NUMBER (PREV_INSN (before)) ==
1564 NOTE_INSN_LOOP_BEG; before = PREV_INSN (before))
1568 /* We'd better be fallthru, or we've lost track of what's what. */
1569 if ((e->flags & EDGE_FALLTHRU) == 0)
1572 after = BB_END (bb);
1575 /* Otherwise we must split the edge. */
1578 bb = split_edge (e);
1579 after = BB_END (bb);
1581 /* If we are partitioning hot/cold basic blocks, we must make sure
1582 that the new basic block ends up in the correct section. */
1584 bb->partition = e->src->partition;
1585 if (flag_reorder_blocks_and_partition
1586 && e->src != ENTRY_BLOCK_PTR
1587 && e->src->partition == COLD_PARTITION)
1589 rtx bb_note, new_note, cur_insn;
1592 for (cur_insn = BB_HEAD (bb); cur_insn != NEXT_INSN (BB_END (bb));
1593 cur_insn = NEXT_INSN (cur_insn))
1594 if (GET_CODE (cur_insn) == NOTE
1595 && NOTE_LINE_NUMBER (cur_insn) == NOTE_INSN_BASIC_BLOCK)
1601 new_note = emit_note_after (NOTE_INSN_UNLIKELY_EXECUTED_CODE,
1603 NOTE_BASIC_BLOCK (new_note) = bb;
1604 if (GET_CODE (BB_END (bb)) == JUMP_INSN
1605 && !any_condjump_p (BB_END (bb))
1606 && bb->succ->crossing_edge )
1607 REG_NOTES (BB_END (bb)) = gen_rtx_EXPR_LIST
1608 (REG_CROSSING_JUMP, NULL_RTX, REG_NOTES (BB_END (bb)));
1609 if (after == bb_note)
1615 /* Now that we've found the spot, do the insertion. */
1619 emit_insn_before (insns, before);
1620 last = prev_nonnote_insn (before);
1623 last = emit_insn_after (insns, after);
1625 if (returnjump_p (last))
1627 /* ??? Remove all outgoing edges from BB and add one for EXIT.
1628 This is not currently a problem because this only happens
1629 for the (single) epilogue, which already has a fallthru edge
1633 if (e->dest != EXIT_BLOCK_PTR
1634 || e->succ_next != NULL || (e->flags & EDGE_FALLTHRU) == 0)
1637 e->flags &= ~EDGE_FALLTHRU;
1638 emit_barrier_after (last);
1641 delete_insn (before);
1643 else if (GET_CODE (last) == JUMP_INSN)
1646 /* Mark the basic block for find_sub_basic_blocks. */
1650 /* Update the CFG for all queued instructions. */
1653 commit_edge_insertions (void)
1657 bool changed = false;
1659 #ifdef ENABLE_CHECKING
1660 verify_flow_info ();
1663 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
1667 for (e = bb->succ; e; e = next)
1669 next = e->succ_next;
1673 commit_one_edge_insertion (e, false);
1681 blocks = sbitmap_alloc (last_basic_block);
1682 sbitmap_zero (blocks);
1686 SET_BIT (blocks, bb->index);
1687 /* Check for forgotten bb->aux values before commit_edge_insertions
1689 if (bb->aux != &bb->aux)
1693 find_many_sub_basic_blocks (blocks);
1694 sbitmap_free (blocks);
1697 /* Update the CFG for all queued instructions, taking special care of inserting
1698 code on edges between call and storing its return value. */
1701 commit_edge_insertions_watch_calls (void)
1705 bool changed = false;
1707 #ifdef ENABLE_CHECKING
1708 verify_flow_info ();
1711 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
1715 for (e = bb->succ; e; e = next)
1717 next = e->succ_next;
1721 commit_one_edge_insertion (e, true);
1729 blocks = sbitmap_alloc (last_basic_block);
1730 sbitmap_zero (blocks);
1734 SET_BIT (blocks, bb->index);
1735 /* Check for forgotten bb->aux values before commit_edge_insertions
1737 if (bb->aux != &bb->aux)
1741 find_many_sub_basic_blocks (blocks);
1742 sbitmap_free (blocks);
1745 /* Print out RTL-specific basic block information (live information
1746 at start and end). */
1749 rtl_dump_bb (basic_block bb, FILE *outf, int indent)
1755 s_indent = alloca ((size_t) indent + 1);
1756 memset (s_indent, ' ', (size_t) indent);
1757 s_indent[indent] = '\0';
1759 fprintf (outf, ";;%s Registers live at start: ", s_indent);
1760 dump_regset (bb->global_live_at_start, outf);
1763 for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb)); insn != last;
1764 insn = NEXT_INSN (insn))
1765 print_rtl_single (outf, insn);
1767 fprintf (outf, ";;%s Registers live at end: ", s_indent);
1768 dump_regset (bb->global_live_at_end, outf);
1772 /* Like print_rtl, but also print out live information for the start of each
1776 print_rtl_with_bb (FILE *outf, rtx rtx_first)
1781 fprintf (outf, "(nil)\n");
1784 enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
1785 int max_uid = get_max_uid ();
1786 basic_block *start = xcalloc (max_uid, sizeof (basic_block));
1787 basic_block *end = xcalloc (max_uid, sizeof (basic_block));
1788 enum bb_state *in_bb_p = xcalloc (max_uid, sizeof (enum bb_state));
1792 FOR_EACH_BB_REVERSE (bb)
1796 start[INSN_UID (BB_HEAD (bb))] = bb;
1797 end[INSN_UID (BB_END (bb))] = bb;
1798 for (x = BB_HEAD (bb); x != NULL_RTX; x = NEXT_INSN (x))
1800 enum bb_state state = IN_MULTIPLE_BB;
1802 if (in_bb_p[INSN_UID (x)] == NOT_IN_BB)
1804 in_bb_p[INSN_UID (x)] = state;
1806 if (x == BB_END (bb))
1811 for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
1815 if ((bb = start[INSN_UID (tmp_rtx)]) != NULL)
1817 fprintf (outf, ";; Start of basic block %d, registers live:",
1819 dump_regset (bb->global_live_at_start, outf);
1823 if (in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB
1824 && GET_CODE (tmp_rtx) != NOTE
1825 && GET_CODE (tmp_rtx) != BARRIER)
1826 fprintf (outf, ";; Insn is not within a basic block\n");
1827 else if (in_bb_p[INSN_UID (tmp_rtx)] == IN_MULTIPLE_BB)
1828 fprintf (outf, ";; Insn is in multiple basic blocks\n");
1830 did_output = print_rtl_single (outf, tmp_rtx);
1832 if ((bb = end[INSN_UID (tmp_rtx)]) != NULL)
1834 fprintf (outf, ";; End of basic block %d, registers live:\n",
1836 dump_regset (bb->global_live_at_end, outf);
1849 if (current_function_epilogue_delay_list != 0)
1851 fprintf (outf, "\n;; Insns in epilogue delay list:\n\n");
1852 for (tmp_rtx = current_function_epilogue_delay_list; tmp_rtx != 0;
1853 tmp_rtx = XEXP (tmp_rtx, 1))
1854 print_rtl_single (outf, XEXP (tmp_rtx, 0));
1859 update_br_prob_note (basic_block bb)
1862 if (GET_CODE (BB_END (bb)) != JUMP_INSN)
1864 note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX);
1865 if (!note || INTVAL (XEXP (note, 0)) == BRANCH_EDGE (bb)->probability)
1867 XEXP (note, 0) = GEN_INT (BRANCH_EDGE (bb)->probability);
1870 /* Verify the CFG and RTL consistency common for both underlying RTL and
1873 Currently it does following checks:
1875 - test head/end pointers
1876 - overlapping of basic blocks
1877 - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note)
1878 - tails of basic blocks (ensure that boundary is necessary)
1879 - scans body of the basic block for JUMP_INSN, CODE_LABEL
1880 and NOTE_INSN_BASIC_BLOCK
1881 - verify that no fall_thru edge crosses hot/cold partition boundaries
1883 In future it can be extended check a lot of other stuff as well
1884 (reachability of basic blocks, life information, etc. etc.). */
1887 rtl_verify_flow_info_1 (void)
1889 const int max_uid = get_max_uid ();
1890 rtx last_head = get_last_insn ();
1891 basic_block *bb_info;
1894 basic_block bb, last_bb_seen;
1896 bb_info = xcalloc (max_uid, sizeof (basic_block));
1898 /* Check bb chain & numbers. */
1899 last_bb_seen = ENTRY_BLOCK_PTR;
1901 FOR_EACH_BB_REVERSE (bb)
1903 rtx head = BB_HEAD (bb);
1904 rtx end = BB_END (bb);
1906 /* Verify the end of the basic block is in the INSN chain. */
1907 for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
1913 error ("end insn %d for block %d not found in the insn stream",
1914 INSN_UID (end), bb->index);
1918 /* Work backwards from the end to the head of the basic block
1919 to verify the head is in the RTL chain. */
1920 for (; x != NULL_RTX; x = PREV_INSN (x))
1922 /* While walking over the insn chain, verify insns appear
1923 in only one basic block and initialize the BB_INFO array
1924 used by other passes. */
1925 if (bb_info[INSN_UID (x)] != NULL)
1927 error ("insn %d is in multiple basic blocks (%d and %d)",
1928 INSN_UID (x), bb->index, bb_info[INSN_UID (x)]->index);
1932 bb_info[INSN_UID (x)] = bb;
1939 error ("head insn %d for block %d not found in the insn stream",
1940 INSN_UID (head), bb->index);
1947 /* Now check the basic blocks (boundaries etc.) */
1948 FOR_EACH_BB_REVERSE (bb)
1950 int n_fallthru = 0, n_eh = 0, n_call = 0, n_abnormal = 0, n_branch = 0;
1951 edge e, fallthru = NULL;
1954 if (INSN_P (BB_END (bb))
1955 && (note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX))
1956 && bb->succ && bb->succ->succ_next
1957 && any_condjump_p (BB_END (bb)))
1959 if (INTVAL (XEXP (note, 0)) != BRANCH_EDGE (bb)->probability)
1961 error ("verify_flow_info: REG_BR_PROB does not match cfg %wi %i",
1962 INTVAL (XEXP (note, 0)), BRANCH_EDGE (bb)->probability);
1966 for (e = bb->succ; e; e = e->succ_next)
1968 if (e->flags & EDGE_FALLTHRU)
1970 n_fallthru++, fallthru = e;
1971 if (e->crossing_edge)
1973 error ("Fallthru edge crosses section boundary (bb %i)",
1979 if ((e->flags & ~(EDGE_DFS_BACK
1981 | EDGE_IRREDUCIBLE_LOOP
1982 | EDGE_LOOP_EXIT)) == 0)
1985 if (e->flags & EDGE_ABNORMAL_CALL)
1988 if (e->flags & EDGE_EH)
1990 else if (e->flags & EDGE_ABNORMAL)
1994 if (n_eh && GET_CODE (PATTERN (BB_END (bb))) != RESX
1995 && !find_reg_note (BB_END (bb), REG_EH_REGION, NULL_RTX))
1997 error ("Missing REG_EH_REGION note in the end of bb %i", bb->index);
2001 && (GET_CODE (BB_END (bb)) != JUMP_INSN
2002 || (n_branch > 1 && (any_uncondjump_p (BB_END (bb))
2003 || any_condjump_p (BB_END (bb))))))
2005 error ("Too many outgoing branch edges from bb %i", bb->index);
2008 if (n_fallthru && any_uncondjump_p (BB_END (bb)))
2010 error ("Fallthru edge after unconditional jump %i", bb->index);
2013 if (n_branch != 1 && any_uncondjump_p (BB_END (bb)))
2015 error ("Wrong amount of branch edges after unconditional jump %i", bb->index);
2018 if (n_branch != 1 && any_condjump_p (BB_END (bb))
2019 && JUMP_LABEL (BB_END (bb)) != BB_HEAD (fallthru->dest))
2021 error ("Wrong amount of branch edges after conditional jump %i", bb->index);
2024 if (n_call && GET_CODE (BB_END (bb)) != CALL_INSN)
2026 error ("Call edges for non-call insn in bb %i", bb->index);
2030 && (GET_CODE (BB_END (bb)) != CALL_INSN && n_call != n_abnormal)
2031 && (GET_CODE (BB_END (bb)) != JUMP_INSN
2032 || any_condjump_p (BB_END (bb))
2033 || any_uncondjump_p (BB_END (bb))))
2035 error ("Abnormal edges for no purpose in bb %i", bb->index);
2039 for (x = BB_HEAD (bb); x != NEXT_INSN (BB_END (bb)); x = NEXT_INSN (x))
2040 if (BLOCK_FOR_INSN (x) != bb)
2043 if (! BLOCK_FOR_INSN (x))
2045 ("insn %d inside basic block %d but block_for_insn is NULL",
2046 INSN_UID (x), bb->index);
2049 ("insn %d inside basic block %d but block_for_insn is %i",
2050 INSN_UID (x), bb->index, BLOCK_FOR_INSN (x)->index);
2055 /* OK pointers are correct. Now check the header of basic
2056 block. It ought to contain optional CODE_LABEL followed
2057 by NOTE_BASIC_BLOCK. */
2059 if (GET_CODE (x) == CODE_LABEL)
2061 if (BB_END (bb) == x)
2063 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
2071 if (!NOTE_INSN_BASIC_BLOCK_P (x) || NOTE_BASIC_BLOCK (x) != bb)
2073 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
2078 if (BB_END (bb) == x)
2079 /* Do checks for empty blocks her. e */
2082 for (x = NEXT_INSN (x); x; x = NEXT_INSN (x))
2084 if (NOTE_INSN_BASIC_BLOCK_P (x))
2086 error ("NOTE_INSN_BASIC_BLOCK %d in middle of basic block %d",
2087 INSN_UID (x), bb->index);
2091 if (x == BB_END (bb))
2094 if (control_flow_insn_p (x))
2096 error ("in basic block %d:", bb->index);
2097 fatal_insn ("flow control insn inside a basic block", x);
2107 /* Verify the CFG and RTL consistency common for both underlying RTL and
2110 Currently it does following checks:
2111 - all checks of rtl_verify_flow_info_1
2112 - check that all insns are in the basic blocks
2113 (except the switch handling code, barriers and notes)
2114 - check that all returns are followed by barriers
2115 - check that all fallthru edge points to the adjacent blocks. */
2117 rtl_verify_flow_info (void)
2120 int err = rtl_verify_flow_info_1 ();
2123 const rtx rtx_first = get_insns ();
2124 basic_block last_bb_seen = ENTRY_BLOCK_PTR, curr_bb = NULL;
2126 FOR_EACH_BB_REVERSE (bb)
2129 for (e = bb->succ; e; e = e->succ_next)
2130 if (e->flags & EDGE_FALLTHRU)
2136 /* Ensure existence of barrier in BB with no fallthru edges. */
2137 for (insn = BB_END (bb); !insn || GET_CODE (insn) != BARRIER;
2138 insn = NEXT_INSN (insn))
2140 || (GET_CODE (insn) == NOTE
2141 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_BASIC_BLOCK))
2143 error ("missing barrier after block %i", bb->index);
2148 else if (e->src != ENTRY_BLOCK_PTR
2149 && e->dest != EXIT_BLOCK_PTR)
2153 if (e->src->next_bb != e->dest)
2156 ("verify_flow_info: Incorrect blocks for fallthru %i->%i",
2157 e->src->index, e->dest->index);
2161 for (insn = NEXT_INSN (BB_END (e->src)); insn != BB_HEAD (e->dest);
2162 insn = NEXT_INSN (insn))
2163 if (GET_CODE (insn) == BARRIER
2164 #ifndef CASE_DROPS_THROUGH
2167 || (INSN_P (insn) && ! JUMP_TABLE_DATA_P (insn))
2171 error ("verify_flow_info: Incorrect fallthru %i->%i",
2172 e->src->index, e->dest->index);
2173 fatal_insn ("wrong insn in the fallthru edge", insn);
2180 last_bb_seen = ENTRY_BLOCK_PTR;
2182 for (x = rtx_first; x; x = NEXT_INSN (x))
2184 if (NOTE_INSN_BASIC_BLOCK_P (x))
2186 bb = NOTE_BASIC_BLOCK (x);
2189 if (bb != last_bb_seen->next_bb)
2190 internal_error ("basic blocks not laid down consecutively");
2192 curr_bb = last_bb_seen = bb;
2197 switch (GET_CODE (x))
2204 /* An addr_vec is placed outside any basic block. */
2206 && GET_CODE (NEXT_INSN (x)) == JUMP_INSN
2207 && (GET_CODE (PATTERN (NEXT_INSN (x))) == ADDR_DIFF_VEC
2208 || GET_CODE (PATTERN (NEXT_INSN (x))) == ADDR_VEC))
2211 /* But in any case, non-deletable labels can appear anywhere. */
2215 fatal_insn ("insn outside basic block", x);
2220 && GET_CODE (x) == JUMP_INSN
2221 && returnjump_p (x) && ! condjump_p (x)
2222 && ! (NEXT_INSN (x) && GET_CODE (NEXT_INSN (x)) == BARRIER))
2223 fatal_insn ("return not followed by barrier", x);
2224 if (curr_bb && x == BB_END (curr_bb))
2228 if (num_bb_notes != n_basic_blocks)
2230 ("number of bb notes in insn chain (%d) != n_basic_blocks (%d)",
2231 num_bb_notes, n_basic_blocks);
2236 /* Assume that the preceding pass has possibly eliminated jump instructions
2237 or converted the unconditional jumps. Eliminate the edges from CFG.
2238 Return true if any edges are eliminated. */
2241 purge_dead_edges (basic_block bb)
2244 rtx insn = BB_END (bb), note;
2245 bool purged = false;
2247 /* If this instruction cannot trap, remove REG_EH_REGION notes. */
2248 if (GET_CODE (insn) == INSN
2249 && (note = find_reg_note (insn, REG_EH_REGION, NULL)))
2253 if (! may_trap_p (PATTERN (insn))
2254 || ((eqnote = find_reg_equal_equiv_note (insn))
2255 && ! may_trap_p (XEXP (eqnote, 0))))
2256 remove_note (insn, note);
2259 /* Cleanup abnormal edges caused by exceptions or non-local gotos. */
2260 for (e = bb->succ; e; e = next)
2262 next = e->succ_next;
2263 if (e->flags & EDGE_EH)
2265 if (can_throw_internal (BB_END (bb)))
2268 else if (e->flags & EDGE_ABNORMAL_CALL)
2270 if (GET_CODE (BB_END (bb)) == CALL_INSN
2271 && (! (note = find_reg_note (insn, REG_EH_REGION, NULL))
2272 || INTVAL (XEXP (note, 0)) >= 0))
2279 bb->flags |= BB_DIRTY;
2283 if (GET_CODE (insn) == JUMP_INSN)
2288 /* We do care only about conditional jumps and simplejumps. */
2289 if (!any_condjump_p (insn)
2290 && !returnjump_p (insn)
2291 && !simplejump_p (insn))
2294 /* Branch probability/prediction notes are defined only for
2295 condjumps. We've possibly turned condjump into simplejump. */
2296 if (simplejump_p (insn))
2298 note = find_reg_note (insn, REG_BR_PROB, NULL);
2300 remove_note (insn, note);
2301 while ((note = find_reg_note (insn, REG_BR_PRED, NULL)))
2302 remove_note (insn, note);
2305 for (e = bb->succ; e; e = next)
2307 next = e->succ_next;
2309 /* Avoid abnormal flags to leak from computed jumps turned
2310 into simplejumps. */
2312 e->flags &= ~EDGE_ABNORMAL;
2314 /* See if this edge is one we should keep. */
2315 if ((e->flags & EDGE_FALLTHRU) && any_condjump_p (insn))
2316 /* A conditional jump can fall through into the next
2317 block, so we should keep the edge. */
2319 else if (e->dest != EXIT_BLOCK_PTR
2320 && BB_HEAD (e->dest) == JUMP_LABEL (insn))
2321 /* If the destination block is the target of the jump,
2324 else if (e->dest == EXIT_BLOCK_PTR && returnjump_p (insn))
2325 /* If the destination block is the exit block, and this
2326 instruction is a return, then keep the edge. */
2328 else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
2329 /* Keep the edges that correspond to exceptions thrown by
2330 this instruction and rematerialize the EDGE_ABNORMAL
2331 flag we just cleared above. */
2333 e->flags |= EDGE_ABNORMAL;
2337 /* We do not need this edge. */
2338 bb->flags |= BB_DIRTY;
2343 if (!bb->succ || !purged)
2347 fprintf (dump_file, "Purged edges from bb %i\n", bb->index);
2352 /* Redistribute probabilities. */
2353 if (!bb->succ->succ_next)
2355 bb->succ->probability = REG_BR_PROB_BASE;
2356 bb->succ->count = bb->count;
2360 note = find_reg_note (insn, REG_BR_PROB, NULL);
2364 b = BRANCH_EDGE (bb);
2365 f = FALLTHRU_EDGE (bb);
2366 b->probability = INTVAL (XEXP (note, 0));
2367 f->probability = REG_BR_PROB_BASE - b->probability;
2368 b->count = bb->count * b->probability / REG_BR_PROB_BASE;
2369 f->count = bb->count * f->probability / REG_BR_PROB_BASE;
2374 else if (GET_CODE (insn) == CALL_INSN && SIBLING_CALL_P (insn))
2376 /* First, there should not be any EH or ABCALL edges resulting
2377 from non-local gotos and the like. If there were, we shouldn't
2378 have created the sibcall in the first place. Second, there
2379 should of course never have been a fallthru edge. */
2380 if (!bb->succ || bb->succ->succ_next)
2382 if (bb->succ->flags != (EDGE_SIBCALL | EDGE_ABNORMAL))
2388 /* If we don't see a jump insn, we don't know exactly why the block would
2389 have been broken at this point. Look for a simple, non-fallthru edge,
2390 as these are only created by conditional branches. If we find such an
2391 edge we know that there used to be a jump here and can then safely
2392 remove all non-fallthru edges. */
2393 for (e = bb->succ; e && (e->flags & (EDGE_COMPLEX | EDGE_FALLTHRU));
2400 for (e = bb->succ; e; e = next)
2402 next = e->succ_next;
2403 if (!(e->flags & EDGE_FALLTHRU))
2405 bb->flags |= BB_DIRTY;
2411 if (!bb->succ || bb->succ->succ_next)
2414 bb->succ->probability = REG_BR_PROB_BASE;
2415 bb->succ->count = bb->count;
2418 fprintf (dump_file, "Purged non-fallthru edges from bb %i\n",
2423 /* Search all basic blocks for potentially dead edges and purge them. Return
2424 true if some edge has been eliminated. */
2427 purge_all_dead_edges (int update_life_p)
2435 blocks = sbitmap_alloc (last_basic_block);
2436 sbitmap_zero (blocks);
2441 bool purged_here = purge_dead_edges (bb);
2443 purged |= purged_here;
2444 if (purged_here && update_life_p)
2445 SET_BIT (blocks, bb->index);
2448 if (update_life_p && purged)
2449 update_life_info (blocks, UPDATE_LIFE_GLOBAL,
2450 PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
2451 | PROP_KILL_DEAD_CODE);
2454 sbitmap_free (blocks);
2458 /* Same as split_block but update cfg_layout structures. */
2461 cfg_layout_split_block (basic_block bb, void *insnp)
2464 basic_block new_bb = rtl_split_block (bb, insn);
2466 new_bb->rbi->footer = bb->rbi->footer;
2467 bb->rbi->footer = NULL;
2473 /* Redirect Edge to DEST. */
2475 cfg_layout_redirect_edge_and_branch (edge e, basic_block dest)
2477 basic_block src = e->src;
2480 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
2483 if (e->dest == dest)
2486 if (e->src != ENTRY_BLOCK_PTR
2487 && try_redirect_by_replacing_jump (e, dest, true))
2489 src->flags |= BB_DIRTY;
2493 if (e->src == ENTRY_BLOCK_PTR
2494 && (e->flags & EDGE_FALLTHRU) && !(e->flags & EDGE_COMPLEX))
2497 fprintf (dump_file, "Redirecting entry edge from bb %i to %i\n",
2498 e->src->index, dest->index);
2500 e->src->flags |= BB_DIRTY;
2501 redirect_edge_succ (e, dest);
2505 /* Redirect_edge_and_branch may decide to turn branch into fallthru edge
2506 in the case the basic block appears to be in sequence. Avoid this
2509 if (e->flags & EDGE_FALLTHRU)
2511 /* Redirect any branch edges unified with the fallthru one. */
2512 if (GET_CODE (BB_END (src)) == JUMP_INSN
2513 && label_is_jump_target_p (BB_HEAD (e->dest),
2517 fprintf (dump_file, "Fallthru edge unified with branch "
2518 "%i->%i redirected to %i\n",
2519 e->src->index, e->dest->index, dest->index);
2520 e->flags &= ~EDGE_FALLTHRU;
2521 if (!redirect_branch_edge (e, dest))
2523 e->flags |= EDGE_FALLTHRU;
2524 e->src->flags |= BB_DIRTY;
2527 /* In case we are redirecting fallthru edge to the branch edge
2528 of conditional jump, remove it. */
2529 if (src->succ->succ_next
2530 && !src->succ->succ_next->succ_next)
2532 edge s = e->succ_next ? e->succ_next : src->succ;
2534 && any_condjump_p (BB_END (src))
2535 && onlyjump_p (BB_END (src)))
2536 delete_insn (BB_END (src));
2540 fprintf (dump_file, "Fallthru edge %i->%i redirected to %i\n",
2541 e->src->index, e->dest->index, dest->index);
2542 redirect_edge_succ_nodup (e, dest);
2547 ret = redirect_branch_edge (e, dest);
2549 /* We don't want simplejumps in the insn stream during cfglayout. */
2550 if (simplejump_p (BB_END (src)))
2553 src->flags |= BB_DIRTY;
2557 /* Simple wrapper as we always can redirect fallthru edges. */
2559 cfg_layout_redirect_edge_and_branch_force (edge e, basic_block dest)
2561 if (!cfg_layout_redirect_edge_and_branch (e, dest))
2566 /* Same as delete_basic_block but update cfg_layout structures. */
2569 cfg_layout_delete_block (basic_block bb)
2571 rtx insn, next, prev = PREV_INSN (BB_HEAD (bb)), *to, remaints;
2573 if (bb->rbi->header)
2575 next = BB_HEAD (bb);
2577 NEXT_INSN (prev) = bb->rbi->header;
2579 set_first_insn (bb->rbi->header);
2580 PREV_INSN (bb->rbi->header) = prev;
2581 insn = bb->rbi->header;
2582 while (NEXT_INSN (insn))
2583 insn = NEXT_INSN (insn);
2584 NEXT_INSN (insn) = next;
2585 PREV_INSN (next) = insn;
2587 next = NEXT_INSN (BB_END (bb));
2588 if (bb->rbi->footer)
2590 insn = bb->rbi->footer;
2593 if (GET_CODE (insn) == BARRIER)
2595 if (PREV_INSN (insn))
2596 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2598 bb->rbi->footer = NEXT_INSN (insn);
2599 if (NEXT_INSN (insn))
2600 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2602 if (GET_CODE (insn) == CODE_LABEL)
2604 insn = NEXT_INSN (insn);
2606 if (bb->rbi->footer)
2609 NEXT_INSN (insn) = bb->rbi->footer;
2610 PREV_INSN (bb->rbi->footer) = insn;
2611 while (NEXT_INSN (insn))
2612 insn = NEXT_INSN (insn);
2613 NEXT_INSN (insn) = next;
2615 PREV_INSN (next) = insn;
2617 set_last_insn (insn);
2620 if (bb->next_bb != EXIT_BLOCK_PTR)
2621 to = &bb->next_bb->rbi->header;
2623 to = &cfg_layout_function_footer;
2624 rtl_delete_block (bb);
2627 prev = NEXT_INSN (prev);
2629 prev = get_insns ();
2631 next = PREV_INSN (next);
2633 next = get_last_insn ();
2635 if (next && NEXT_INSN (next) != prev)
2637 remaints = unlink_insn_chain (prev, next);
2639 while (NEXT_INSN (insn))
2640 insn = NEXT_INSN (insn);
2641 NEXT_INSN (insn) = *to;
2643 PREV_INSN (*to) = insn;
2648 /* Return true when blocks A and B can be safely merged. */
2650 cfg_layout_can_merge_blocks_p (basic_block a, basic_block b)
2652 bool partitions_ok = true;
2654 /* If we are partitioning hot/cold basic blocks, we don't want to
2655 mess up unconditional or indirect jumps that cross between hot
2656 and cold sections. */
2658 if (flag_reorder_blocks_and_partition
2659 && (find_reg_note (BB_END (a), REG_CROSSING_JUMP, NULL_RTX)
2660 || find_reg_note (BB_END (b), REG_CROSSING_JUMP, NULL_RTX)
2661 || a->partition != b->partition))
2662 partitions_ok = false;
2664 /* There must be exactly one edge in between the blocks. */
2665 return (a->succ && !a->succ->succ_next && a->succ->dest == b
2666 && !b->pred->pred_next && a != b
2667 /* Must be simple edge. */
2668 && !(a->succ->flags & EDGE_COMPLEX)
2670 && a != ENTRY_BLOCK_PTR && b != EXIT_BLOCK_PTR
2671 /* If the jump insn has side effects,
2672 we can't kill the edge. */
2673 && (GET_CODE (BB_END (a)) != JUMP_INSN
2674 || (reload_completed
2675 ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
2678 /* Merge block A and B, abort when it is not possible. */
2680 cfg_layout_merge_blocks (basic_block a, basic_block b)
2682 #ifdef ENABLE_CHECKING
2683 if (!cfg_layout_can_merge_blocks_p (a, b))
2687 /* If there was a CODE_LABEL beginning B, delete it. */
2688 if (GET_CODE (BB_HEAD (b)) == CODE_LABEL)
2689 delete_insn (BB_HEAD (b));
2691 /* We should have fallthru edge in a, or we can do dummy redirection to get
2693 if (GET_CODE (BB_END (a)) == JUMP_INSN)
2694 try_redirect_by_replacing_jump (a->succ, b, true);
2695 if (GET_CODE (BB_END (a)) == JUMP_INSN)
2698 /* Possible line number notes should appear in between. */
2701 rtx first = BB_END (a), last;
2703 last = emit_insn_after (b->rbi->header, BB_END (a));
2704 delete_insn_chain (NEXT_INSN (first), last);
2705 b->rbi->header = NULL;
2708 /* In the case basic blocks are not adjacent, move them around. */
2709 if (NEXT_INSN (BB_END (a)) != BB_HEAD (b))
2711 rtx first = unlink_insn_chain (BB_HEAD (b), BB_END (b));
2713 emit_insn_after (first, BB_END (a));
2714 /* Skip possible DELETED_LABEL insn. */
2715 if (!NOTE_INSN_BASIC_BLOCK_P (first))
2716 first = NEXT_INSN (first);
2717 if (!NOTE_INSN_BASIC_BLOCK_P (first))
2720 delete_insn (first);
2722 /* Otherwise just re-associate the instructions. */
2727 for (insn = BB_HEAD (b);
2728 insn != NEXT_INSN (BB_END (b));
2729 insn = NEXT_INSN (insn))
2730 set_block_for_insn (insn, a);
2732 /* Skip possible DELETED_LABEL insn. */
2733 if (!NOTE_INSN_BASIC_BLOCK_P (insn))
2734 insn = NEXT_INSN (insn);
2735 if (!NOTE_INSN_BASIC_BLOCK_P (insn))
2738 BB_END (a) = BB_END (b);
2742 /* Possible tablejumps and barriers should appear after the block. */
2745 if (!a->rbi->footer)
2746 a->rbi->footer = b->rbi->footer;
2749 rtx last = a->rbi->footer;
2751 while (NEXT_INSN (last))
2752 last = NEXT_INSN (last);
2753 NEXT_INSN (last) = b->rbi->footer;
2754 PREV_INSN (b->rbi->footer) = last;
2756 b->rbi->footer = NULL;
2760 fprintf (dump_file, "Merged blocks %d and %d.\n",
2761 a->index, b->index);
2767 cfg_layout_split_edge (edge e)
2770 basic_block new_bb =
2771 create_basic_block (e->src != ENTRY_BLOCK_PTR
2772 ? NEXT_INSN (BB_END (e->src)) : get_insns (),
2775 new_e = make_edge (new_bb, e->dest, EDGE_FALLTHRU);
2776 redirect_edge_and_branch_force (e, new_bb);
2781 /* Do postprocessing after making a forwarder block joined by edge FALLTHRU. */
2784 rtl_make_forwarder_block (edge fallthru ATTRIBUTE_UNUSED)
2788 /* Implementation of CFG manipulation for linearized RTL. */
2789 struct cfg_hooks rtl_cfg_hooks = {
2791 rtl_verify_flow_info,
2793 rtl_create_basic_block,
2794 rtl_redirect_edge_and_branch,
2795 rtl_redirect_edge_and_branch_force,
2798 rtl_move_block_after,
2799 rtl_can_merge_blocks, /* can_merge_blocks_p */
2802 rtl_make_forwarder_block,
2803 rtl_tidy_fallthru_edge
2806 /* Implementation of CFG manipulation for cfg layout RTL, where
2807 basic block connected via fallthru edges does not have to be adjacent.
2808 This representation will hopefully become the default one in future
2809 version of the compiler. */
2810 struct cfg_hooks cfg_layout_rtl_cfg_hooks = {
2812 rtl_verify_flow_info_1,
2814 cfg_layout_create_basic_block,
2815 cfg_layout_redirect_edge_and_branch,
2816 cfg_layout_redirect_edge_and_branch_force,
2817 cfg_layout_delete_block,
2818 cfg_layout_split_block,
2819 rtl_move_block_after,
2820 cfg_layout_can_merge_blocks_p,
2821 cfg_layout_merge_blocks,
2822 cfg_layout_split_edge,
2823 rtl_make_forwarder_block,