1 /* Basic block reordering routines for the GNU compiler.
2 Copyright (C) 2000, 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 #include "coretypes.h"
27 #include "hard-reg-set.h"
29 #include "basic-block.h"
30 #include "insn-config.h"
33 #include "cfglayout.h"
37 #include "alloc-pool.h"
39 #include "tree-pass.h"
42 /* Holds the interesting trailing notes for the function. */
43 rtx cfg_layout_function_footer;
44 rtx cfg_layout_function_header;
46 static rtx skip_insns_after_block (basic_block);
47 static void record_effective_endpoints (void);
48 static rtx label_for_bb (basic_block);
49 static void fixup_reorder_chain (void);
51 static void change_scope (rtx, tree, tree);
53 void verify_insn_chain (void);
54 static void fixup_fallthru_exit_predecessor (void);
55 static tree insn_scope (rtx);
58 unlink_insn_chain (rtx first, rtx last)
60 rtx prevfirst = PREV_INSN (first);
61 rtx nextlast = NEXT_INSN (last);
63 PREV_INSN (first) = NULL;
64 NEXT_INSN (last) = NULL;
66 NEXT_INSN (prevfirst) = nextlast;
68 PREV_INSN (nextlast) = prevfirst;
70 set_last_insn (prevfirst);
72 set_first_insn (nextlast);
76 /* Skip over inter-block insns occurring after BB which are typically
77 associated with BB (e.g., barriers). If there are any such insns,
78 we return the last one. Otherwise, we return the end of BB. */
81 skip_insns_after_block (basic_block bb)
83 rtx insn, last_insn, next_head, prev;
86 if (bb->next_bb != EXIT_BLOCK_PTR)
87 next_head = BB_HEAD (bb->next_bb);
89 for (last_insn = insn = BB_END (bb); (insn = NEXT_INSN (insn)) != 0; )
91 if (insn == next_head)
94 switch (GET_CODE (insn))
101 switch (NOTE_LINE_NUMBER (insn))
103 case NOTE_INSN_BLOCK_END:
106 case NOTE_INSN_DELETED:
107 case NOTE_INSN_DELETED_LABEL:
118 && JUMP_P (NEXT_INSN (insn))
119 && (GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_VEC
120 || GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_DIFF_VEC))
122 insn = NEXT_INSN (insn);
135 /* It is possible to hit contradictory sequence. For instance:
141 Where barrier belongs to jump_insn, but the note does not. This can be
142 created by removing the basic block originally following
143 NOTE_INSN_BLOCK_BEG. In such case reorder the notes. */
145 for (insn = last_insn; insn != BB_END (bb); insn = prev)
147 prev = PREV_INSN (insn);
149 switch (NOTE_LINE_NUMBER (insn))
151 case NOTE_INSN_BLOCK_END:
152 case NOTE_INSN_DELETED:
153 case NOTE_INSN_DELETED_LABEL:
156 reorder_insns (insn, insn, last_insn);
163 /* Locate or create a label for a given basic block. */
166 label_for_bb (basic_block bb)
168 rtx label = BB_HEAD (bb);
170 if (!LABEL_P (label))
173 fprintf (dump_file, "Emitting label for block %d\n", bb->index);
175 label = block_label (bb);
181 /* Locate the effective beginning and end of the insn chain for each
182 block, as defined by skip_insns_after_block above. */
185 record_effective_endpoints (void)
191 for (insn = get_insns ();
194 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_BASIC_BLOCK;
195 insn = NEXT_INSN (insn))
197 /* No basic blocks at all? */
200 if (PREV_INSN (insn))
201 cfg_layout_function_header =
202 unlink_insn_chain (get_insns (), PREV_INSN (insn));
204 cfg_layout_function_header = NULL_RTX;
206 next_insn = get_insns ();
211 if (PREV_INSN (BB_HEAD (bb)) && next_insn != BB_HEAD (bb))
212 bb->il.rtl->header = unlink_insn_chain (next_insn,
213 PREV_INSN (BB_HEAD (bb)));
214 end = skip_insns_after_block (bb);
215 if (NEXT_INSN (BB_END (bb)) && BB_END (bb) != end)
216 bb->il.rtl->footer = unlink_insn_chain (NEXT_INSN (BB_END (bb)), end);
217 next_insn = NEXT_INSN (BB_END (bb));
220 cfg_layout_function_footer = next_insn;
221 if (cfg_layout_function_footer)
222 cfg_layout_function_footer = unlink_insn_chain (cfg_layout_function_footer, get_last_insn ());
225 /* Data structures representing mapping of INSN_LOCATOR into scope blocks, line
226 numbers and files. In order to be GGC friendly we need to use separate
227 varrays. This also slightly improve the memory locality in binary search.
228 The _locs array contains locators where the given property change. The
229 block_locators_blocks contains the scope block that is used for all insn
230 locator greater than corresponding block_locators_locs value and smaller
231 than the following one. Similarly for the other properties. */
232 static VEC(int,heap) *block_locators_locs;
233 static GTY(()) VEC(tree,gc) *block_locators_blocks;
234 static VEC(int,heap) *locations_locators_locs;
235 DEF_VEC_O(location_t);
236 DEF_VEC_ALLOC_O(location_t,heap);
237 static VEC(location_t,heap) *locations_locators_vals;
238 int prologue_locator;
239 int epilogue_locator;
241 /* Hold current location information and last location information, so the
242 datastructures are built lazilly only when some instructions in given
244 location_t curr_location, last_location;
245 static tree curr_block, last_block;
246 static int curr_rtl_loc = -1;
248 /* Allocate insn locator datastructure. */
250 insn_locators_alloc (void)
252 prologue_locator = epilogue_locator = 0;
254 block_locators_locs = VEC_alloc (int, heap, 32);
255 block_locators_blocks = VEC_alloc (tree, gc, 32);
256 locations_locators_locs = VEC_alloc (int, heap, 32);
257 locations_locators_vals = VEC_alloc (location_t, heap, 32);
259 #ifdef USE_MAPPED_LOCATION
263 last_location.line = -1;
264 curr_location.line = -1;
271 /* At the end of emit stage, clear current location. */
273 insn_locators_finalize (void)
275 if (curr_rtl_loc >= 0)
276 epilogue_locator = curr_insn_locator ();
280 /* Set current location. */
282 set_curr_insn_source_location (location_t location)
284 gcc_assert (curr_rtl_loc >= 0);
285 #ifdef USE_MAPPED_LOCATION
286 if (location == last_location)
289 if (location.file && last_location.file
290 && !strcmp (location.file, last_location.file)
291 && location.line == last_location.line)
294 curr_location = location;
297 /* Set current scope block. */
299 set_curr_insn_block (tree b)
301 gcc_assert (curr_rtl_loc >= 0);
306 /* Return current insn locator. */
308 curr_insn_locator (void)
310 if (curr_rtl_loc == -1)
312 if (last_block != curr_block)
315 VEC_safe_push (int, heap, block_locators_locs, curr_rtl_loc);
316 VEC_safe_push (tree, gc, block_locators_blocks, curr_block);
317 last_block = curr_block;
319 #ifdef USE_MAPPED_LOCATION
320 if (last_location != curr_location)
322 if (last_location.file != curr_location.file
323 || last_location.line != curr_location.line)
327 VEC_safe_push (int, heap, locations_locators_locs, curr_rtl_loc);
328 VEC_safe_push (location_t, heap, locations_locators_vals, &curr_location);
329 last_location = curr_location;
335 into_cfg_layout_mode (void)
337 cfg_layout_initialize (0);
342 outof_cfg_layout_mode (void)
347 if (bb->next_bb != EXIT_BLOCK_PTR)
348 bb->aux = bb->next_bb;
350 cfg_layout_finalize ();
355 struct tree_opt_pass pass_into_cfg_layout_mode =
357 "into_cfglayout", /* name */
359 into_cfg_layout_mode, /* execute */
362 0, /* static_pass_number */
364 0, /* properties_required */
365 0, /* properties_provided */
366 0, /* properties_destroyed */
367 0, /* todo_flags_start */
368 TODO_dump_func, /* todo_flags_finish */
372 struct tree_opt_pass pass_outof_cfg_layout_mode =
374 "outof_cfglayout", /* name */
376 outof_cfg_layout_mode, /* execute */
379 0, /* static_pass_number */
381 0, /* properties_required */
382 0, /* properties_provided */
383 0, /* properties_destroyed */
384 0, /* todo_flags_start */
385 TODO_dump_func, /* todo_flags_finish */
389 /* Return sope resulting from combination of S1 and S2. */
391 choose_inner_scope (tree s1, tree s2)
397 if (BLOCK_NUMBER (s1) > BLOCK_NUMBER (s2))
402 /* Emit lexical block notes needed to change scope from S1 to S2. */
405 change_scope (rtx orig_insn, tree s1, tree s2)
407 rtx insn = orig_insn;
408 tree com = NULL_TREE;
409 tree ts1 = s1, ts2 = s2;
414 gcc_assert (ts1 && ts2);
415 if (BLOCK_NUMBER (ts1) > BLOCK_NUMBER (ts2))
416 ts1 = BLOCK_SUPERCONTEXT (ts1);
417 else if (BLOCK_NUMBER (ts1) < BLOCK_NUMBER (ts2))
418 ts2 = BLOCK_SUPERCONTEXT (ts2);
421 ts1 = BLOCK_SUPERCONTEXT (ts1);
422 ts2 = BLOCK_SUPERCONTEXT (ts2);
431 rtx note = emit_note_before (NOTE_INSN_BLOCK_END, insn);
432 NOTE_BLOCK (note) = s;
433 s = BLOCK_SUPERCONTEXT (s);
440 insn = emit_note_before (NOTE_INSN_BLOCK_BEG, insn);
441 NOTE_BLOCK (insn) = s;
442 s = BLOCK_SUPERCONTEXT (s);
446 /* Return lexical scope block insn belong to. */
448 insn_scope (rtx insn)
450 int max = VEC_length (int, block_locators_locs);
452 int loc = INSN_LOCATOR (insn);
454 /* When block_locators_locs was initialized, the pro- and epilogue
455 insns didn't exist yet and can therefore not be found this way.
456 But we know that they belong to the outer most block of the
458 Without this test, the prologue would be put inside the block of
459 the first valid instruction in the function and when that first
460 insn is part of an inlined function then the low_pc of that
461 inlined function is messed up. Likewise for the epilogue and
462 the last valid instruction. */
463 if (loc == prologue_locator || loc == epilogue_locator)
464 return DECL_INITIAL (cfun->decl);
470 int pos = (min + max) / 2;
471 int tmp = VEC_index (int, block_locators_locs, pos);
473 if (tmp <= loc && min != pos)
475 else if (tmp > loc && max != pos)
483 return VEC_index (tree, block_locators_blocks, min);
486 /* Return line number of the statement specified by the locator. */
488 locator_location (int loc)
490 int max = VEC_length (int, locations_locators_locs);
495 int pos = (min + max) / 2;
496 int tmp = VEC_index (int, locations_locators_locs, pos);
498 if (tmp <= loc && min != pos)
500 else if (tmp > loc && max != pos)
508 return *VEC_index (location_t, locations_locators_vals, min);
511 /* Return source line of the statement that produced this insn. */
513 locator_line (int loc)
515 expanded_location xloc;
519 xloc = expand_location (locator_location (loc));
523 /* Return line number of the statement that produced this insn. */
527 return locator_line (INSN_LOCATOR (insn));
530 /* Return source file of the statement specified by LOC. */
532 locator_file (int loc)
534 expanded_location xloc;
538 xloc = expand_location (locator_location (loc));
542 /* Return source file of the statement that produced this insn. */
546 return locator_file (INSN_LOCATOR (insn));
549 /* Rebuild all the NOTE_INSN_BLOCK_BEG and NOTE_INSN_BLOCK_END notes based
550 on the scope tree and the newly reordered instructions. */
553 reemit_insn_block_notes (void)
555 tree cur_block = DECL_INITIAL (cfun->decl);
559 if (!active_insn_p (insn))
560 insn = next_active_insn (insn);
561 for (; insn; insn = next_active_insn (insn))
565 /* Avoid putting scope notes between jump table and its label. */
567 && (GET_CODE (PATTERN (insn)) == ADDR_VEC
568 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
571 this_block = insn_scope (insn);
572 /* For sequences compute scope resulting from merging all scopes
573 of instructions nested inside. */
574 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
577 rtx body = PATTERN (insn);
580 for (i = 0; i < XVECLEN (body, 0); i++)
581 this_block = choose_inner_scope (this_block,
582 insn_scope (XVECEXP (body, 0, i)));
587 if (this_block != cur_block)
589 change_scope (insn, cur_block, this_block);
590 cur_block = this_block;
594 /* change_scope emits before the insn, not after. */
595 note = emit_note (NOTE_INSN_DELETED);
596 change_scope (note, cur_block, DECL_INITIAL (cfun->decl));
603 /* Link the basic blocks in the correct order, compacting the basic
604 block queue while at it. This also clears the visited flag on
605 all basic blocks. If STAY_IN_CFGLAYOUT_MODE is false, this function
606 also clears the basic block header and footer fields.
608 This function is usually called after a pass (e.g. tracer) finishes
609 some transformations while in cfglayout mode. The required sequence
610 of the basic blocks is in a linked list along the bb->aux field.
611 This functions re-links the basic block prev_bb and next_bb pointers
612 accordingly, and it compacts and renumbers the blocks. */
615 relink_block_chain (bool stay_in_cfglayout_mode)
617 basic_block bb, prev_bb;
620 /* Maybe dump the re-ordered sequence. */
623 fprintf (dump_file, "Reordered sequence:\n");
624 for (bb = ENTRY_BLOCK_PTR->next_bb, index = NUM_FIXED_BLOCKS;
626 bb = bb->aux, index++)
628 fprintf (dump_file, " %i ", index);
629 if (get_bb_original (bb))
630 fprintf (dump_file, "duplicate of %i ",
631 get_bb_original (bb)->index);
632 else if (forwarder_block_p (bb)
633 && !LABEL_P (BB_HEAD (bb)))
634 fprintf (dump_file, "compensation ");
636 fprintf (dump_file, "bb %i ", bb->index);
637 fprintf (dump_file, " [%i]\n", bb->frequency);
641 /* Now reorder the blocks. */
642 prev_bb = ENTRY_BLOCK_PTR;
643 bb = ENTRY_BLOCK_PTR->next_bb;
644 for (; bb; prev_bb = bb, bb = bb->aux)
646 bb->prev_bb = prev_bb;
647 prev_bb->next_bb = bb;
649 prev_bb->next_bb = EXIT_BLOCK_PTR;
650 EXIT_BLOCK_PTR->prev_bb = prev_bb;
652 /* Then, clean up the aux and visited fields. */
656 bb->il.rtl->visited = 0;
657 if (!stay_in_cfglayout_mode)
658 bb->il.rtl->header = bb->il.rtl->footer = NULL;
661 /* Maybe reset the original copy tables, they are not valid anymore
662 when we renumber the basic blocks in compact_blocks. If we are
663 are going out of cfglayout mode, don't re-allocate the tables. */
664 free_original_copy_tables ();
665 if (stay_in_cfglayout_mode)
666 initialize_original_copy_tables ();
668 /* Finally, put basic_block_info in the new order. */
673 /* Given a reorder chain, rearrange the code to match. */
676 fixup_reorder_chain (void)
681 if (cfg_layout_function_header)
683 set_first_insn (cfg_layout_function_header);
684 insn = cfg_layout_function_header;
685 while (NEXT_INSN (insn))
686 insn = NEXT_INSN (insn);
689 /* First do the bulk reordering -- rechain the blocks without regard to
690 the needed changes to jumps and labels. */
692 for (bb = ENTRY_BLOCK_PTR->next_bb; bb; bb = bb->aux)
694 if (bb->il.rtl->header)
697 NEXT_INSN (insn) = bb->il.rtl->header;
699 set_first_insn (bb->il.rtl->header);
700 PREV_INSN (bb->il.rtl->header) = insn;
701 insn = bb->il.rtl->header;
702 while (NEXT_INSN (insn))
703 insn = NEXT_INSN (insn);
706 NEXT_INSN (insn) = BB_HEAD (bb);
708 set_first_insn (BB_HEAD (bb));
709 PREV_INSN (BB_HEAD (bb)) = insn;
711 if (bb->il.rtl->footer)
713 NEXT_INSN (insn) = bb->il.rtl->footer;
714 PREV_INSN (bb->il.rtl->footer) = insn;
715 while (NEXT_INSN (insn))
716 insn = NEXT_INSN (insn);
720 NEXT_INSN (insn) = cfg_layout_function_footer;
721 if (cfg_layout_function_footer)
722 PREV_INSN (cfg_layout_function_footer) = insn;
724 while (NEXT_INSN (insn))
725 insn = NEXT_INSN (insn);
727 set_last_insn (insn);
728 #ifdef ENABLE_CHECKING
729 verify_insn_chain ();
732 /* Now add jumps and labels as needed to match the blocks new
735 for (bb = ENTRY_BLOCK_PTR->next_bb; bb ; bb = bb->aux)
737 edge e_fall, e_taken, e;
742 if (EDGE_COUNT (bb->succs) == 0)
745 /* Find the old fallthru edge, and another non-EH edge for
747 e_taken = e_fall = NULL;
749 FOR_EACH_EDGE (e, ei, bb->succs)
750 if (e->flags & EDGE_FALLTHRU)
752 else if (! (e->flags & EDGE_EH))
755 bb_end_insn = BB_END (bb);
756 if (JUMP_P (bb_end_insn))
758 if (any_condjump_p (bb_end_insn))
760 /* If the old fallthru is still next, nothing to do. */
761 if (bb->aux == e_fall->dest
762 || e_fall->dest == EXIT_BLOCK_PTR)
765 /* The degenerated case of conditional jump jumping to the next
766 instruction can happen for jumps with side effects. We need
767 to construct a forwarder block and this will be done just
768 fine by force_nonfallthru below. */
772 /* There is another special case: if *neither* block is next,
773 such as happens at the very end of a function, then we'll
774 need to add a new unconditional jump. Choose the taken
775 edge based on known or assumed probability. */
776 else if (bb->aux != e_taken->dest)
778 rtx note = find_reg_note (bb_end_insn, REG_BR_PROB, 0);
781 && INTVAL (XEXP (note, 0)) < REG_BR_PROB_BASE / 2
782 && invert_jump (bb_end_insn,
783 (e_fall->dest == EXIT_BLOCK_PTR
785 : label_for_bb (e_fall->dest)), 0))
787 e_fall->flags &= ~EDGE_FALLTHRU;
788 #ifdef ENABLE_CHECKING
789 gcc_assert (could_fall_through
790 (e_taken->src, e_taken->dest));
792 e_taken->flags |= EDGE_FALLTHRU;
793 update_br_prob_note (bb);
794 e = e_fall, e_fall = e_taken, e_taken = e;
798 /* If the "jumping" edge is a crossing edge, and the fall
799 through edge is non-crossing, leave things as they are. */
800 else if ((e_taken->flags & EDGE_CROSSING)
801 && !(e_fall->flags & EDGE_CROSSING))
804 /* Otherwise we can try to invert the jump. This will
805 basically never fail, however, keep up the pretense. */
806 else if (invert_jump (bb_end_insn,
807 (e_fall->dest == EXIT_BLOCK_PTR
809 : label_for_bb (e_fall->dest)), 0))
811 e_fall->flags &= ~EDGE_FALLTHRU;
812 #ifdef ENABLE_CHECKING
813 gcc_assert (could_fall_through
814 (e_taken->src, e_taken->dest));
816 e_taken->flags |= EDGE_FALLTHRU;
817 update_br_prob_note (bb);
823 /* Otherwise we have some return, switch or computed
824 jump. In the 99% case, there should not have been a
826 gcc_assert (returnjump_p (bb_end_insn) || !e_fall);
832 /* No fallthru implies a noreturn function with EH edges, or
833 something similarly bizarre. In any case, we don't need to
838 /* If the fallthru block is still next, nothing to do. */
839 if (bb->aux == e_fall->dest)
842 /* A fallthru to exit block. */
843 if (e_fall->dest == EXIT_BLOCK_PTR)
847 /* We got here if we need to add a new jump insn. */
848 nb = force_nonfallthru (e_fall);
851 nb->il.rtl->visited = 1;
854 /* Don't process this new block. */
857 /* Make sure new bb is tagged for correct section (same as
858 fall-thru source, since you cannot fall-throu across
859 section boundaries). */
860 BB_COPY_PARTITION (e_fall->src, single_pred (bb));
861 if (flag_reorder_blocks_and_partition
862 && targetm.have_named_sections
863 && JUMP_P (BB_END (bb))
864 && !any_condjump_p (BB_END (bb))
865 && (EDGE_SUCC (bb, 0)->flags & EDGE_CROSSING))
866 REG_NOTES (BB_END (bb)) = gen_rtx_EXPR_LIST
867 (REG_CROSSING_JUMP, NULL_RTX, REG_NOTES (BB_END (bb)));
871 relink_block_chain (/*stay_in_cfglayout_mode=*/false);
873 /* Annoying special case - jump around dead jumptables left in the code. */
879 FOR_EACH_EDGE (e, ei, bb->succs)
880 if (e->flags & EDGE_FALLTHRU)
883 if (e && !can_fallthru (e->src, e->dest))
884 force_nonfallthru (e);
888 /* Perform sanity checks on the insn chain.
889 1. Check that next/prev pointers are consistent in both the forward and
891 2. Count insns in chain, going both directions, and check if equal.
892 3. Check that get_last_insn () returns the actual end of chain. */
895 verify_insn_chain (void)
898 int insn_cnt1, insn_cnt2;
900 for (prevx = NULL, insn_cnt1 = 1, x = get_insns ();
902 prevx = x, insn_cnt1++, x = NEXT_INSN (x))
903 gcc_assert (PREV_INSN (x) == prevx);
905 gcc_assert (prevx == get_last_insn ());
907 for (nextx = NULL, insn_cnt2 = 1, x = get_last_insn ();
909 nextx = x, insn_cnt2++, x = PREV_INSN (x))
910 gcc_assert (NEXT_INSN (x) == nextx);
912 gcc_assert (insn_cnt1 == insn_cnt2);
915 /* If we have assembler epilogues, the block falling through to exit must
916 be the last one in the reordered chain when we reach final. Ensure
917 that this condition is met. */
919 fixup_fallthru_exit_predecessor (void)
923 basic_block bb = NULL;
925 /* This transformation is not valid before reload, because we might
926 separate a call from the instruction that copies the return
928 gcc_assert (reload_completed);
930 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
931 if (e->flags & EDGE_FALLTHRU)
936 basic_block c = ENTRY_BLOCK_PTR->next_bb;
938 /* If the very first block is the one with the fall-through exit
939 edge, we have to split that block. */
942 bb = split_block (bb, NULL)->dest;
945 bb->il.rtl->footer = c->il.rtl->footer;
946 c->il.rtl->footer = NULL;
961 /* Return true in case it is possible to duplicate the basic block BB. */
963 /* We do not want to declare the function in a header file, since it should
964 only be used through the cfghooks interface, and we do not want to move
965 it to cfgrtl.c since it would require also moving quite a lot of related
967 extern bool cfg_layout_can_duplicate_bb_p (basic_block);
970 cfg_layout_can_duplicate_bb_p (basic_block bb)
972 /* Do not attempt to duplicate tablejumps, as we need to unshare
973 the dispatch table. This is difficult to do, as the instructions
974 computing jump destination may be hoisted outside the basic block. */
975 if (tablejump_p (BB_END (bb), NULL, NULL))
978 /* Do not duplicate blocks containing insns that can't be copied. */
979 if (targetm.cannot_copy_insn_p)
981 rtx insn = BB_HEAD (bb);
984 if (INSN_P (insn) && targetm.cannot_copy_insn_p (insn))
986 if (insn == BB_END (bb))
988 insn = NEXT_INSN (insn);
996 duplicate_insn_chain (rtx from, rtx to)
1000 /* Avoid updating of boundaries of previous basic block. The
1001 note will get removed from insn stream in fixup. */
1002 last = emit_note (NOTE_INSN_DELETED);
1004 /* Create copy at the end of INSN chain. The chain will
1005 be reordered later. */
1006 for (insn = from; insn != NEXT_INSN (to); insn = NEXT_INSN (insn))
1008 switch (GET_CODE (insn))
1013 /* Avoid copying of dispatch tables. We never duplicate
1014 tablejumps, so this can hit only in case the table got
1015 moved far from original jump. */
1016 if (GET_CODE (PATTERN (insn)) == ADDR_VEC
1017 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
1019 emit_copy_of_insn_after (insn, get_last_insn ());
1030 switch (NOTE_LINE_NUMBER (insn))
1032 /* In case prologue is empty and function contain label
1033 in first BB, we may want to copy the block. */
1034 case NOTE_INSN_PROLOGUE_END:
1036 case NOTE_INSN_DELETED:
1037 case NOTE_INSN_DELETED_LABEL:
1038 /* No problem to strip these. */
1039 case NOTE_INSN_EPILOGUE_BEG:
1040 /* Debug code expect these notes to exist just once.
1041 Keep them in the master copy.
1042 ??? It probably makes more sense to duplicate them for each
1044 case NOTE_INSN_FUNCTION_BEG:
1045 /* There is always just single entry to function. */
1046 case NOTE_INSN_BASIC_BLOCK:
1049 case NOTE_INSN_SWITCH_TEXT_SECTIONS:
1050 emit_note_copy (insn);
1054 /* All other notes should have already been eliminated.
1056 gcc_assert (NOTE_LINE_NUMBER (insn) >= 0);
1058 /* It is possible that no_line_number is set and the note
1059 won't be emitted. */
1060 emit_note_copy (insn);
1067 insn = NEXT_INSN (last);
1071 /* Create a duplicate of the basic block BB. */
1073 /* We do not want to declare the function in a header file, since it should
1074 only be used through the cfghooks interface, and we do not want to move
1075 it to cfgrtl.c since it would require also moving quite a lot of related
1077 extern basic_block cfg_layout_duplicate_bb (basic_block);
1080 cfg_layout_duplicate_bb (basic_block bb)
1085 insn = duplicate_insn_chain (BB_HEAD (bb), BB_END (bb));
1086 new_bb = create_basic_block (insn,
1087 insn ? get_last_insn () : NULL,
1088 EXIT_BLOCK_PTR->prev_bb);
1090 BB_COPY_PARTITION (new_bb, bb);
1091 if (bb->il.rtl->header)
1093 insn = bb->il.rtl->header;
1094 while (NEXT_INSN (insn))
1095 insn = NEXT_INSN (insn);
1096 insn = duplicate_insn_chain (bb->il.rtl->header, insn);
1098 new_bb->il.rtl->header = unlink_insn_chain (insn, get_last_insn ());
1101 if (bb->il.rtl->footer)
1103 insn = bb->il.rtl->footer;
1104 while (NEXT_INSN (insn))
1105 insn = NEXT_INSN (insn);
1106 insn = duplicate_insn_chain (bb->il.rtl->footer, insn);
1108 new_bb->il.rtl->footer = unlink_insn_chain (insn, get_last_insn ());
1111 if (bb->il.rtl->global_live_at_start)
1113 new_bb->il.rtl->global_live_at_start = ALLOC_REG_SET (®_obstack);
1114 new_bb->il.rtl->global_live_at_end = ALLOC_REG_SET (®_obstack);
1115 COPY_REG_SET (new_bb->il.rtl->global_live_at_start,
1116 bb->il.rtl->global_live_at_start);
1117 COPY_REG_SET (new_bb->il.rtl->global_live_at_end,
1118 bb->il.rtl->global_live_at_end);
1124 /* Main entry point to this module - initialize the datastructures for
1125 CFG layout changes. It keeps LOOPS up-to-date if not null.
1127 FLAGS is a set of additional flags to pass to cleanup_cfg(). It should
1128 include CLEANUP_UPDATE_LIFE if liveness information must be kept up
1132 cfg_layout_initialize (unsigned int flags)
1134 initialize_original_copy_tables ();
1136 cfg_layout_rtl_register_cfg_hooks ();
1138 record_effective_endpoints ();
1140 cleanup_cfg (CLEANUP_CFGLAYOUT | flags);
1143 /* Splits superblocks. */
1145 break_superblocks (void)
1147 sbitmap superblocks;
1151 superblocks = sbitmap_alloc (last_basic_block);
1152 sbitmap_zero (superblocks);
1155 if (bb->flags & BB_SUPERBLOCK)
1157 bb->flags &= ~BB_SUPERBLOCK;
1158 SET_BIT (superblocks, bb->index);
1164 rebuild_jump_labels (get_insns ());
1165 find_many_sub_basic_blocks (superblocks);
1171 /* Finalize the changes: reorder insn list according to the sequence specified
1172 by aux pointers, enter compensation code, rebuild scope forest. */
1175 cfg_layout_finalize (void)
1177 #ifdef ENABLE_CHECKING
1178 verify_flow_info ();
1180 rtl_register_cfg_hooks ();
1181 if (reload_completed
1182 #ifdef HAVE_epilogue
1186 fixup_fallthru_exit_predecessor ();
1187 fixup_reorder_chain ();
1189 rebuild_jump_labels (get_insns ());
1190 delete_dead_jumptables ();
1192 #ifdef ENABLE_CHECKING
1193 verify_insn_chain ();
1194 verify_flow_info ();
1198 /* Checks whether all N blocks in BBS array can be copied. */
1200 can_copy_bbs_p (basic_block *bbs, unsigned n)
1206 for (i = 0; i < n; i++)
1207 bbs[i]->flags |= BB_DUPLICATED;
1209 for (i = 0; i < n; i++)
1211 /* In case we should redirect abnormal edge during duplication, fail. */
1213 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
1214 if ((e->flags & EDGE_ABNORMAL)
1215 && (e->dest->flags & BB_DUPLICATED))
1221 if (!can_duplicate_block_p (bbs[i]))
1229 for (i = 0; i < n; i++)
1230 bbs[i]->flags &= ~BB_DUPLICATED;
1235 /* Duplicates N basic blocks stored in array BBS. Newly created basic blocks
1236 are placed into array NEW_BBS in the same order. Edges from basic blocks
1237 in BBS are also duplicated and copies of those of them
1238 that lead into BBS are redirected to appropriate newly created block. The
1239 function assigns bbs into loops (copy of basic block bb is assigned to
1240 bb->loop_father->copy loop, so this must be set up correctly in advance)
1241 and updates dominators locally (LOOPS structure that contains the information
1242 about dominators is passed to enable this).
1244 BASE is the superloop to that basic block belongs; if its header or latch
1245 is copied, we do not set the new blocks as header or latch.
1247 Created copies of N_EDGES edges in array EDGES are stored in array NEW_EDGES,
1248 also in the same order.
1250 Newly created basic blocks are put after the basic block AFTER in the
1251 instruction stream, and the order of the blocks in BBS array is preserved. */
1254 copy_bbs (basic_block *bbs, unsigned n, basic_block *new_bbs,
1255 edge *edges, unsigned num_edges, edge *new_edges,
1256 struct loop *base, basic_block after)
1259 basic_block bb, new_bb, dom_bb;
1262 /* Duplicate bbs, update dominators, assign bbs to loops. */
1263 for (i = 0; i < n; i++)
1267 new_bb = new_bbs[i] = duplicate_block (bb, NULL, after);
1269 bb->flags |= BB_DUPLICATED;
1270 /* Possibly set loop header. */
1271 if (bb->loop_father->header == bb && bb->loop_father != base)
1272 new_bb->loop_father->header = new_bb;
1274 if (bb->loop_father->latch == bb && bb->loop_father != base)
1275 new_bb->loop_father->latch = new_bb;
1278 /* Set dominators. */
1279 for (i = 0; i < n; i++)
1282 new_bb = new_bbs[i];
1284 dom_bb = get_immediate_dominator (CDI_DOMINATORS, bb);
1285 if (dom_bb->flags & BB_DUPLICATED)
1287 dom_bb = get_bb_copy (dom_bb);
1288 set_immediate_dominator (CDI_DOMINATORS, new_bb, dom_bb);
1292 /* Redirect edges. */
1293 for (j = 0; j < num_edges; j++)
1294 new_edges[j] = NULL;
1295 for (i = 0; i < n; i++)
1298 new_bb = new_bbs[i];
1301 FOR_EACH_EDGE (e, ei, new_bb->succs)
1303 for (j = 0; j < num_edges; j++)
1304 if (edges[j] && edges[j]->src == bb && edges[j]->dest == e->dest)
1307 if (!(e->dest->flags & BB_DUPLICATED))
1309 redirect_edge_and_branch_force (e, get_bb_copy (e->dest));
1313 /* Clear information about duplicates. */
1314 for (i = 0; i < n; i++)
1315 bbs[i]->flags &= ~BB_DUPLICATED;
1318 #include "gt-cfglayout.h"