Add a directory forgotten in merge.
[official-gcc.git] / gcc / cfglayout.c
bloba3199a27d34fdd782e8b63b9af3d086574a9b777
1 /* Basic block reordering routines for the GNU compiler.
2 Copyright (C) 2000, 2001, 2003 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
9 version.
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
14 for more details.
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, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "hard-reg-set.h"
28 #include "basic-block.h"
29 #include "insn-config.h"
30 #include "output.h"
31 #include "function.h"
32 #include "obstack.h"
33 #include "cfglayout.h"
34 #include "cfgloop.h"
35 #include "target.h"
36 #include "ggc.h"
37 #include "alloc-pool.h"
39 /* The contents of the current function definition are allocated
40 in this obstack, and all are freed at the end of the function. */
41 extern struct obstack flow_obstack;
43 alloc_pool cfg_layout_pool;
45 /* Holds the interesting trailing notes for the function. */
46 rtx cfg_layout_function_footer, cfg_layout_function_header;
48 static rtx skip_insns_after_block (basic_block);
49 static void record_effective_endpoints (void);
50 static rtx label_for_bb (basic_block);
51 static void fixup_reorder_chain (void);
53 static void set_block_levels (tree, int);
54 static void change_scope (rtx, tree, tree);
56 void verify_insn_chain (void);
57 static void fixup_fallthru_exit_predecessor (void);
58 static rtx duplicate_insn_chain (rtx, rtx);
59 static void break_superblocks (void);
60 static tree insn_scope (rtx);
62 rtx
63 unlink_insn_chain (rtx first, rtx last)
65 rtx prevfirst = PREV_INSN (first);
66 rtx nextlast = NEXT_INSN (last);
68 PREV_INSN (first) = NULL;
69 NEXT_INSN (last) = NULL;
70 if (prevfirst)
71 NEXT_INSN (prevfirst) = nextlast;
72 if (nextlast)
73 PREV_INSN (nextlast) = prevfirst;
74 else
75 set_last_insn (prevfirst);
76 if (!prevfirst)
77 set_first_insn (nextlast);
78 return first;
81 /* Skip over inter-block insns occurring after BB which are typically
82 associated with BB (e.g., barriers). If there are any such insns,
83 we return the last one. Otherwise, we return the end of BB. */
85 static rtx
86 skip_insns_after_block (basic_block bb)
88 rtx insn, last_insn, next_head, prev;
90 next_head = NULL_RTX;
91 if (bb->next_bb != EXIT_BLOCK_PTR)
92 next_head = bb->next_bb->head;
94 for (last_insn = insn = bb->end; (insn = NEXT_INSN (insn)) != 0; )
96 if (insn == next_head)
97 break;
99 switch (GET_CODE (insn))
101 case BARRIER:
102 last_insn = insn;
103 continue;
105 case NOTE:
106 switch (NOTE_LINE_NUMBER (insn))
108 case NOTE_INSN_LOOP_END:
109 case NOTE_INSN_BLOCK_END:
110 last_insn = insn;
111 continue;
112 case NOTE_INSN_DELETED:
113 case NOTE_INSN_DELETED_LABEL:
114 continue;
116 default:
117 continue;
118 break;
120 break;
122 case CODE_LABEL:
123 if (NEXT_INSN (insn)
124 && GET_CODE (NEXT_INSN (insn)) == JUMP_INSN
125 && (GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_VEC
126 || GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_DIFF_VEC))
128 insn = NEXT_INSN (insn);
129 last_insn = insn;
130 continue;
132 break;
134 default:
135 break;
138 break;
141 /* It is possible to hit contradictory sequence. For instance:
143 jump_insn
144 NOTE_INSN_LOOP_BEG
145 barrier
147 Where barrier belongs to jump_insn, but the note does not. This can be
148 created by removing the basic block originally following
149 NOTE_INSN_LOOP_BEG. In such case reorder the notes. */
151 for (insn = last_insn; insn != bb->end; insn = prev)
153 prev = PREV_INSN (insn);
154 if (GET_CODE (insn) == NOTE)
155 switch (NOTE_LINE_NUMBER (insn))
157 case NOTE_INSN_LOOP_END:
158 case NOTE_INSN_BLOCK_END:
159 case NOTE_INSN_DELETED:
160 case NOTE_INSN_DELETED_LABEL:
161 continue;
162 default:
163 reorder_insns (insn, insn, last_insn);
167 return last_insn;
170 /* Locate or create a label for a given basic block. */
172 static rtx
173 label_for_bb (basic_block bb)
175 rtx label = bb->head;
177 if (GET_CODE (label) != CODE_LABEL)
179 if (rtl_dump_file)
180 fprintf (rtl_dump_file, "Emitting label for block %d\n", bb->index);
182 label = block_label (bb);
185 return label;
188 /* Locate the effective beginning and end of the insn chain for each
189 block, as defined by skip_insns_after_block above. */
191 static void
192 record_effective_endpoints (void)
194 rtx next_insn;
195 basic_block bb;
196 rtx insn;
198 for (insn = get_insns ();
199 insn
200 && GET_CODE (insn) == NOTE
201 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_BASIC_BLOCK;
202 insn = NEXT_INSN (insn))
203 continue;
204 if (!insn)
205 abort (); /* No basic blocks at all? */
206 if (PREV_INSN (insn))
207 cfg_layout_function_header =
208 unlink_insn_chain (get_insns (), PREV_INSN (insn));
209 else
210 cfg_layout_function_header = NULL_RTX;
212 next_insn = get_insns ();
213 FOR_EACH_BB (bb)
215 rtx end;
217 if (PREV_INSN (bb->head) && next_insn != bb->head)
218 bb->rbi->header = unlink_insn_chain (next_insn,
219 PREV_INSN (bb->head));
220 end = skip_insns_after_block (bb);
221 if (NEXT_INSN (bb->end) && bb->end != end)
222 bb->rbi->footer = unlink_insn_chain (NEXT_INSN (bb->end), end);
223 next_insn = NEXT_INSN (bb->end);
226 cfg_layout_function_footer = next_insn;
227 if (cfg_layout_function_footer)
228 cfg_layout_function_footer = unlink_insn_chain (cfg_layout_function_footer, get_last_insn ());
231 /* Data structures representing mapping of INSN_LOCATOR into scope blocks, line
232 numbers and files. In order to be GGC friendly we need to use separate
233 varrays. This also slightly improve the memory locality in binary search.
234 The _locs array contains locators where the given property change. The
235 block_locators_blocks contains the scope block that is used for all insn
236 locator greater than corresponding block_locators_locs value and smaller
237 than the following one. Similarly for the other properties. */
238 static GTY(()) varray_type block_locators_locs;
239 static GTY(()) varray_type block_locators_blocks;
240 static GTY(()) varray_type line_locators_locs;
241 static GTY(()) varray_type line_locators_lines;
242 static GTY(()) varray_type file_locators_locs;
243 static GTY(()) varray_type file_locators_files;
244 int prologue_locator;
245 int epilogue_locator;
247 /* During the RTL expansion the lexical blocks and line numbers are
248 represented via INSN_NOTEs. Replace them by representation using
249 INSN_LOCATORs. */
251 void
252 insn_locators_initialize (void)
254 tree block = NULL;
255 tree last_block = NULL;
256 rtx insn, next;
257 int loc = 0;
258 int line_number = 0, last_line_number = 0;
259 char *file_name = NULL, *last_file_name = NULL;
261 prologue_locator = epilogue_locator = 0;
263 VARRAY_INT_INIT (block_locators_locs, 32, "block_locators_locs");
264 VARRAY_TREE_INIT (block_locators_blocks, 32, "block_locators_blocks");
265 VARRAY_INT_INIT (line_locators_locs, 32, "line_locators_locs");
266 VARRAY_INT_INIT (line_locators_lines, 32, "line_locators_lines");
267 VARRAY_INT_INIT (file_locators_locs, 32, "file_locators_locs");
268 VARRAY_CHAR_PTR_INIT (file_locators_files, 32, "file_locators_files");
270 for (insn = get_insns (); insn; insn = next)
272 next = NEXT_INSN (insn);
274 if ((active_insn_p (insn)
275 && GET_CODE (PATTERN (insn)) != ADDR_VEC
276 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
277 || !NEXT_INSN (insn)
278 || (!prologue_locator && file_name))
280 if (last_block != block)
282 loc++;
283 VARRAY_PUSH_INT (block_locators_locs, loc);
284 VARRAY_PUSH_TREE (block_locators_blocks, block);
285 last_block = block;
287 if (last_line_number != line_number)
289 loc++;
290 VARRAY_PUSH_INT (line_locators_locs, loc);
291 VARRAY_PUSH_INT (line_locators_lines, line_number);
292 last_line_number = line_number;
294 if (last_file_name != file_name)
296 loc++;
297 VARRAY_PUSH_INT (file_locators_locs, loc);
298 VARRAY_PUSH_CHAR_PTR (file_locators_files, file_name);
299 last_file_name = file_name;
302 if (!prologue_locator && file_name)
303 prologue_locator = loc;
304 if (!NEXT_INSN (insn))
305 epilogue_locator = loc;
306 if (active_insn_p (insn))
307 INSN_LOCATOR (insn) = loc;
308 else if (GET_CODE (insn) == NOTE)
310 switch (NOTE_LINE_NUMBER (insn))
312 case NOTE_INSN_BLOCK_BEG:
313 if (cfun->dont_emit_block_notes)
314 abort ();
315 block = NOTE_BLOCK (insn);
316 delete_insn (insn);
317 break;
318 case NOTE_INSN_BLOCK_END:
319 if (cfun->dont_emit_block_notes)
320 abort ();
321 block = BLOCK_SUPERCONTEXT (block);
322 if (block && TREE_CODE (block) == FUNCTION_DECL)
323 block = 0;
324 delete_insn (insn);
325 break;
326 default:
327 if (NOTE_LINE_NUMBER (insn) > 0)
329 line_number = NOTE_LINE_NUMBER (insn);
330 file_name = (char *)NOTE_SOURCE_FILE (insn);
332 break;
336 if (cfun->dont_emit_block_notes)
337 check_block_change (insn, &block);
340 /* Tag the blocks with a depth number so that change_scope can find
341 the common parent easily. */
342 set_block_levels (DECL_INITIAL (cfun->decl), 0);
344 if (cfun->dont_emit_block_notes)
345 free_block_changes ();
348 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
349 found in the block tree. */
351 static void
352 set_block_levels (tree block, int level)
354 while (block)
356 BLOCK_NUMBER (block) = level;
357 set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
358 block = BLOCK_CHAIN (block);
362 /* Return sope resulting from combination of S1 and S2. */
363 tree
364 choose_inner_scope (tree s1, tree s2)
366 if (!s1)
367 return s2;
368 if (!s2)
369 return s1;
370 if (BLOCK_NUMBER (s1) > BLOCK_NUMBER (s2))
371 return s1;
372 return s2;
375 /* Emit lexical block notes needed to change scope from S1 to S2. */
377 static void
378 change_scope (rtx orig_insn, tree s1, tree s2)
380 rtx insn = orig_insn;
381 tree com = NULL_TREE;
382 tree ts1 = s1, ts2 = s2;
383 tree s;
385 while (ts1 != ts2)
387 if (ts1 == NULL || ts2 == NULL)
388 abort ();
389 if (BLOCK_NUMBER (ts1) > BLOCK_NUMBER (ts2))
390 ts1 = BLOCK_SUPERCONTEXT (ts1);
391 else if (BLOCK_NUMBER (ts1) < BLOCK_NUMBER (ts2))
392 ts2 = BLOCK_SUPERCONTEXT (ts2);
393 else
395 ts1 = BLOCK_SUPERCONTEXT (ts1);
396 ts2 = BLOCK_SUPERCONTEXT (ts2);
399 com = ts1;
401 /* Close scopes. */
402 s = s1;
403 while (s != com)
405 rtx note = emit_note_before (NOTE_INSN_BLOCK_END, insn);
406 NOTE_BLOCK (note) = s;
407 s = BLOCK_SUPERCONTEXT (s);
410 /* Open scopes. */
411 s = s2;
412 while (s != com)
414 insn = emit_note_before (NOTE_INSN_BLOCK_BEG, insn);
415 NOTE_BLOCK (insn) = s;
416 s = BLOCK_SUPERCONTEXT (s);
420 /* Return lexical scope block insn belong to. */
421 static tree
422 insn_scope (rtx insn)
424 int max = VARRAY_ACTIVE_SIZE (block_locators_locs);
425 int min = 0;
426 int loc = INSN_LOCATOR (insn);
428 if (!max || !loc)
429 return NULL;
430 while (1)
432 int pos = (min + max) / 2;
433 int tmp = VARRAY_INT (block_locators_locs, pos);
435 if (tmp <= loc && min != pos)
436 min = pos;
437 else if (tmp > loc && max != pos)
438 max = pos;
439 else
441 min = pos;
442 break;
445 return VARRAY_TREE (block_locators_blocks, min);
448 /* Return line number of the statement specified by the locator. */
450 locator_line (int loc)
452 int max = VARRAY_ACTIVE_SIZE (line_locators_locs);
453 int min = 0;
455 if (!max || !loc)
456 return 0;
457 while (1)
459 int pos = (min + max) / 2;
460 int tmp = VARRAY_INT (line_locators_locs, pos);
462 if (tmp <= loc && min != pos)
463 min = pos;
464 else if (tmp > loc && max != pos)
465 max = pos;
466 else
468 min = pos;
469 break;
472 return VARRAY_INT (line_locators_lines, min);
475 /* Return line number of the statement that produced this insn. */
477 insn_line (rtx insn)
479 return locator_line (INSN_LOCATOR (insn));
482 /* Return source file of the statement specified by LOC. */
483 const char *
484 locator_file (int loc)
486 int max = VARRAY_ACTIVE_SIZE (file_locators_locs);
487 int min = 0;
489 if (!max || !loc)
490 return NULL;
491 while (1)
493 int pos = (min + max) / 2;
494 int tmp = VARRAY_INT (file_locators_locs, pos);
496 if (tmp <= loc && min != pos)
497 min = pos;
498 else if (tmp > loc && max != pos)
499 max = pos;
500 else
502 min = pos;
503 break;
506 return VARRAY_CHAR_PTR (file_locators_files, min);
509 /* Return source file of the statement that produced this insn. */
510 const char *
511 insn_file (rtx insn)
513 return locator_file (INSN_LOCATOR (insn));
516 /* Rebuild all the NOTE_INSN_BLOCK_BEG and NOTE_INSN_BLOCK_END notes based
517 on the scope tree and the newly reordered instructions. */
519 void
520 reemit_insn_block_notes (void)
522 tree cur_block = DECL_INITIAL (cfun->decl);
523 rtx insn, note;
525 insn = get_insns ();
526 if (!active_insn_p (insn))
527 insn = next_active_insn (insn);
528 for (; insn; insn = next_active_insn (insn))
530 tree this_block;
532 this_block = insn_scope (insn);
533 /* For sequences compute scope resulting from merging all scopes
534 of instructions nested inside. */
535 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
537 int i;
538 rtx body = PATTERN (insn);
540 this_block = NULL;
541 for (i = 0; i < XVECLEN (body, 0); i++)
542 this_block = choose_inner_scope (this_block,
543 insn_scope (XVECEXP (body, 0, i)));
545 if (! this_block)
546 continue;
548 if (this_block != cur_block)
550 change_scope (insn, cur_block, this_block);
551 cur_block = this_block;
555 /* change_scope emits before the insn, not after. */
556 note = emit_note (NOTE_INSN_DELETED);
557 change_scope (note, cur_block, DECL_INITIAL (cfun->decl));
558 delete_insn (note);
560 reorder_blocks ();
563 /* Given a reorder chain, rearrange the code to match. */
565 static void
566 fixup_reorder_chain (void)
568 basic_block bb, prev_bb;
569 int index;
570 rtx insn = NULL;
572 if (cfg_layout_function_header)
574 set_first_insn (cfg_layout_function_header);
575 insn = cfg_layout_function_header;
576 while (NEXT_INSN (insn))
577 insn = NEXT_INSN (insn);
580 /* First do the bulk reordering -- rechain the blocks without regard to
581 the needed changes to jumps and labels. */
583 for (bb = ENTRY_BLOCK_PTR->next_bb, index = 0;
584 bb != 0;
585 bb = bb->rbi->next, index++)
587 if (bb->rbi->header)
589 if (insn)
590 NEXT_INSN (insn) = bb->rbi->header;
591 else
592 set_first_insn (bb->rbi->header);
593 PREV_INSN (bb->rbi->header) = insn;
594 insn = bb->rbi->header;
595 while (NEXT_INSN (insn))
596 insn = NEXT_INSN (insn);
598 if (insn)
599 NEXT_INSN (insn) = bb->head;
600 else
601 set_first_insn (bb->head);
602 PREV_INSN (bb->head) = insn;
603 insn = bb->end;
604 if (bb->rbi->footer)
606 NEXT_INSN (insn) = bb->rbi->footer;
607 PREV_INSN (bb->rbi->footer) = insn;
608 while (NEXT_INSN (insn))
609 insn = NEXT_INSN (insn);
613 if (index != n_basic_blocks)
614 abort ();
616 NEXT_INSN (insn) = cfg_layout_function_footer;
617 if (cfg_layout_function_footer)
618 PREV_INSN (cfg_layout_function_footer) = insn;
620 while (NEXT_INSN (insn))
621 insn = NEXT_INSN (insn);
623 set_last_insn (insn);
624 #ifdef ENABLE_CHECKING
625 verify_insn_chain ();
626 #endif
627 delete_dead_jumptables ();
629 /* Now add jumps and labels as needed to match the blocks new
630 outgoing edges. */
632 for (bb = ENTRY_BLOCK_PTR->next_bb; bb ; bb = bb->rbi->next)
634 edge e_fall, e_taken, e;
635 rtx bb_end_insn;
636 basic_block nb;
638 if (bb->succ == NULL)
639 continue;
641 /* Find the old fallthru edge, and another non-EH edge for
642 a taken jump. */
643 e_taken = e_fall = NULL;
644 for (e = bb->succ; e ; e = e->succ_next)
645 if (e->flags & EDGE_FALLTHRU)
646 e_fall = e;
647 else if (! (e->flags & EDGE_EH))
648 e_taken = e;
650 bb_end_insn = bb->end;
651 if (GET_CODE (bb_end_insn) == JUMP_INSN)
653 if (any_condjump_p (bb_end_insn))
655 /* If the old fallthru is still next, nothing to do. */
656 if (bb->rbi->next == e_fall->dest
657 || (!bb->rbi->next
658 && e_fall->dest == EXIT_BLOCK_PTR))
659 continue;
661 /* The degenerated case of conditional jump jumping to the next
662 instruction can happen on target having jumps with side
663 effects.
665 Create temporarily the duplicated edge representing branch.
666 It will get unidentified by force_nonfallthru_and_redirect
667 that would otherwise get confused by fallthru edge not pointing
668 to the next basic block. */
669 if (!e_taken)
671 rtx note;
672 edge e_fake;
674 e_fake = unchecked_make_edge (bb, e_fall->dest, 0);
676 if (!redirect_jump (bb->end, block_label (bb), 0))
677 abort ();
678 note = find_reg_note (bb->end, REG_BR_PROB, NULL_RTX);
679 if (note)
681 int prob = INTVAL (XEXP (note, 0));
683 e_fake->probability = prob;
684 e_fake->count = e_fall->count * prob / REG_BR_PROB_BASE;
685 e_fall->probability -= e_fall->probability;
686 e_fall->count -= e_fake->count;
687 if (e_fall->probability < 0)
688 e_fall->probability = 0;
689 if (e_fall->count < 0)
690 e_fall->count = 0;
693 /* There is one special case: if *neither* block is next,
694 such as happens at the very end of a function, then we'll
695 need to add a new unconditional jump. Choose the taken
696 edge based on known or assumed probability. */
697 else if (bb->rbi->next != e_taken->dest)
699 rtx note = find_reg_note (bb_end_insn, REG_BR_PROB, 0);
701 if (note
702 && INTVAL (XEXP (note, 0)) < REG_BR_PROB_BASE / 2
703 && invert_jump (bb_end_insn,
704 label_for_bb (e_fall->dest), 0))
706 e_fall->flags &= ~EDGE_FALLTHRU;
707 e_taken->flags |= EDGE_FALLTHRU;
708 update_br_prob_note (bb);
709 e = e_fall, e_fall = e_taken, e_taken = e;
713 /* Otherwise we can try to invert the jump. This will
714 basically never fail, however, keep up the pretense. */
715 else if (invert_jump (bb_end_insn,
716 label_for_bb (e_fall->dest), 0))
718 e_fall->flags &= ~EDGE_FALLTHRU;
719 e_taken->flags |= EDGE_FALLTHRU;
720 update_br_prob_note (bb);
721 continue;
724 else if (returnjump_p (bb_end_insn))
725 continue;
726 else
728 /* Otherwise we have some switch or computed jump. In the
729 99% case, there should not have been a fallthru edge. */
730 if (! e_fall)
731 continue;
733 #ifdef CASE_DROPS_THROUGH
734 /* Except for VAX. Since we didn't have predication for the
735 tablejump, the fallthru block should not have moved. */
736 if (bb->rbi->next == e_fall->dest)
737 continue;
738 bb_end_insn = skip_insns_after_block (bb);
739 #else
740 abort ();
741 #endif
744 else
746 /* No fallthru implies a noreturn function with EH edges, or
747 something similarly bizarre. In any case, we don't need to
748 do anything. */
749 if (! e_fall)
750 continue;
752 /* If the fallthru block is still next, nothing to do. */
753 if (bb->rbi->next == e_fall->dest)
754 continue;
756 /* A fallthru to exit block. */
757 if (!bb->rbi->next && e_fall->dest == EXIT_BLOCK_PTR)
758 continue;
761 /* We got here if we need to add a new jump insn. */
762 nb = force_nonfallthru (e_fall);
763 if (nb)
765 cfg_layout_initialize_rbi (nb);
766 nb->rbi->visited = 1;
767 nb->rbi->next = bb->rbi->next;
768 bb->rbi->next = nb;
769 /* Don't process this new block. */
770 bb = nb;
774 /* Put basic_block_info in the new order. */
776 if (rtl_dump_file)
778 fprintf (rtl_dump_file, "Reordered sequence:\n");
779 for (bb = ENTRY_BLOCK_PTR->next_bb, index = 0; bb; bb = bb->rbi->next, index ++)
781 fprintf (rtl_dump_file, " %i ", index);
782 if (bb->rbi->original)
783 fprintf (rtl_dump_file, "duplicate of %i ",
784 bb->rbi->original->index);
785 else if (forwarder_block_p (bb) && GET_CODE (bb->head) != CODE_LABEL)
786 fprintf (rtl_dump_file, "compensation ");
787 else
788 fprintf (rtl_dump_file, "bb %i ", bb->index);
789 fprintf (rtl_dump_file, " [%i]\n", bb->frequency);
793 prev_bb = ENTRY_BLOCK_PTR;
794 bb = ENTRY_BLOCK_PTR->next_bb;
795 index = 0;
797 for (; bb; prev_bb = bb, bb = bb->rbi->next, index ++)
799 bb->index = index;
800 BASIC_BLOCK (index) = bb;
802 bb->prev_bb = prev_bb;
803 prev_bb->next_bb = bb;
805 prev_bb->next_bb = EXIT_BLOCK_PTR;
806 EXIT_BLOCK_PTR->prev_bb = prev_bb;
808 /* Annoying special case - jump around dead jumptables left in the code. */
809 FOR_EACH_BB (bb)
811 edge e;
812 for (e = bb->succ; e && !(e->flags & EDGE_FALLTHRU); e = e->succ_next)
813 continue;
814 if (e && !can_fallthru (e->src, e->dest))
815 force_nonfallthru (e);
819 /* Perform sanity checks on the insn chain.
820 1. Check that next/prev pointers are consistent in both the forward and
821 reverse direction.
822 2. Count insns in chain, going both directions, and check if equal.
823 3. Check that get_last_insn () returns the actual end of chain. */
825 void
826 verify_insn_chain (void)
828 rtx x, prevx, nextx;
829 int insn_cnt1, insn_cnt2;
831 for (prevx = NULL, insn_cnt1 = 1, x = get_insns ();
832 x != 0;
833 prevx = x, insn_cnt1++, x = NEXT_INSN (x))
834 if (PREV_INSN (x) != prevx)
835 abort ();
837 if (prevx != get_last_insn ())
838 abort ();
840 for (nextx = NULL, insn_cnt2 = 1, x = get_last_insn ();
841 x != 0;
842 nextx = x, insn_cnt2++, x = PREV_INSN (x))
843 if (NEXT_INSN (x) != nextx)
844 abort ();
846 if (insn_cnt1 != insn_cnt2)
847 abort ();
850 /* The block falling through to exit must be the last one in the
851 reordered chain. Ensure that this condition is met. */
852 static void
853 fixup_fallthru_exit_predecessor (void)
855 edge e;
856 basic_block bb = NULL;
858 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
859 if (e->flags & EDGE_FALLTHRU)
860 bb = e->src;
862 if (bb && bb->rbi->next)
864 basic_block c = ENTRY_BLOCK_PTR->next_bb;
866 while (c->rbi->next != bb)
867 c = c->rbi->next;
869 c->rbi->next = bb->rbi->next;
870 while (c->rbi->next)
871 c = c->rbi->next;
873 c->rbi->next = bb;
874 bb->rbi->next = NULL;
878 /* Return true in case it is possible to duplicate the basic block BB. */
880 bool
881 cfg_layout_can_duplicate_bb_p (basic_block bb)
883 edge s;
885 if (bb == EXIT_BLOCK_PTR || bb == ENTRY_BLOCK_PTR)
886 return false;
888 /* Duplicating fallthru block to exit would require adding a jump
889 and splitting the real last BB. */
890 for (s = bb->succ; s; s = s->succ_next)
891 if (s->dest == EXIT_BLOCK_PTR && s->flags & EDGE_FALLTHRU)
892 return false;
894 /* Do not attempt to duplicate tablejumps, as we need to unshare
895 the dispatch table. This is difficult to do, as the instructions
896 computing jump destination may be hoisted outside the basic block. */
897 if (tablejump_p (bb->end, NULL, NULL))
898 return false;
900 /* Do not duplicate blocks containing insns that can't be copied. */
901 if (targetm.cannot_copy_insn_p)
903 rtx insn = bb->head;
904 while (1)
906 if (INSN_P (insn) && (*targetm.cannot_copy_insn_p) (insn))
907 return false;
908 if (insn == bb->end)
909 break;
910 insn = NEXT_INSN (insn);
914 return true;
917 static rtx
918 duplicate_insn_chain (rtx from, rtx to)
920 rtx insn, last;
922 /* Avoid updating of boundaries of previous basic block. The
923 note will get removed from insn stream in fixup. */
924 last = emit_note (NOTE_INSN_DELETED);
926 /* Create copy at the end of INSN chain. The chain will
927 be reordered later. */
928 for (insn = from; insn != NEXT_INSN (to); insn = NEXT_INSN (insn))
930 switch (GET_CODE (insn))
932 case INSN:
933 case CALL_INSN:
934 case JUMP_INSN:
935 /* Avoid copying of dispatch tables. We never duplicate
936 tablejumps, so this can hit only in case the table got
937 moved far from original jump. */
938 if (GET_CODE (PATTERN (insn)) == ADDR_VEC
939 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
940 break;
941 emit_copy_of_insn_after (insn, get_last_insn ());
942 break;
944 case CODE_LABEL:
945 break;
947 case BARRIER:
948 emit_barrier ();
949 break;
951 case NOTE:
952 switch (NOTE_LINE_NUMBER (insn))
954 /* In case prologue is empty and function contain label
955 in first BB, we may want to copy the block. */
956 case NOTE_INSN_PROLOGUE_END:
958 case NOTE_INSN_LOOP_VTOP:
959 case NOTE_INSN_LOOP_CONT:
960 case NOTE_INSN_LOOP_BEG:
961 case NOTE_INSN_LOOP_END:
962 /* Strip down the loop notes - we don't really want to keep
963 them consistent in loop copies. */
964 case NOTE_INSN_DELETED:
965 case NOTE_INSN_DELETED_LABEL:
966 /* No problem to strip these. */
967 case NOTE_INSN_EPILOGUE_BEG:
968 case NOTE_INSN_FUNCTION_END:
969 /* Debug code expect these notes to exist just once.
970 Keep them in the master copy.
971 ??? It probably makes more sense to duplicate them for each
972 epilogue copy. */
973 case NOTE_INSN_FUNCTION_BEG:
974 /* There is always just single entry to function. */
975 case NOTE_INSN_BASIC_BLOCK:
976 break;
978 /* There is no purpose to duplicate prologue. */
979 case NOTE_INSN_BLOCK_BEG:
980 case NOTE_INSN_BLOCK_END:
981 /* The BLOCK_BEG/BLOCK_END notes should be eliminated when BB
982 reordering is in the progress. */
983 case NOTE_INSN_EH_REGION_BEG:
984 case NOTE_INSN_EH_REGION_END:
985 /* Should never exist at BB duplication time. */
986 abort ();
987 break;
988 case NOTE_INSN_REPEATED_LINE_NUMBER:
989 emit_note_copy (insn);
990 break;
992 default:
993 if (NOTE_LINE_NUMBER (insn) < 0)
994 abort ();
995 /* It is possible that no_line_number is set and the note
996 won't be emitted. */
997 emit_note_copy (insn);
999 break;
1000 default:
1001 abort ();
1004 insn = NEXT_INSN (last);
1005 delete_insn (last);
1006 return insn;
1008 /* Create a duplicate of the basic block BB and redirect edge E into it.
1009 If E is not specified, BB is just copied, but updating the frequencies
1010 etc. is left to the caller. */
1012 basic_block
1013 cfg_layout_duplicate_bb (basic_block bb, edge e)
1015 rtx insn;
1016 edge s, n;
1017 basic_block new_bb;
1018 gcov_type new_count = e ? e->count : 0;
1020 if (bb->count < new_count)
1021 new_count = bb->count;
1022 if (!bb->pred)
1023 abort ();
1024 #ifdef ENABLE_CHECKING
1025 if (!cfg_layout_can_duplicate_bb_p (bb))
1026 abort ();
1027 #endif
1029 insn = duplicate_insn_chain (bb->head, bb->end);
1030 new_bb = create_basic_block (insn,
1031 insn ? get_last_insn () : NULL,
1032 EXIT_BLOCK_PTR->prev_bb);
1034 if (bb->rbi->header)
1036 insn = bb->rbi->header;
1037 while (NEXT_INSN (insn))
1038 insn = NEXT_INSN (insn);
1039 insn = duplicate_insn_chain (bb->rbi->header, insn);
1040 if (insn)
1041 new_bb->rbi->header = unlink_insn_chain (insn, get_last_insn ());
1044 if (bb->rbi->footer)
1046 insn = bb->rbi->footer;
1047 while (NEXT_INSN (insn))
1048 insn = NEXT_INSN (insn);
1049 insn = duplicate_insn_chain (bb->rbi->footer, insn);
1050 if (insn)
1051 new_bb->rbi->footer = unlink_insn_chain (insn, get_last_insn ());
1054 if (bb->global_live_at_start)
1056 new_bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1057 new_bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1058 COPY_REG_SET (new_bb->global_live_at_start, bb->global_live_at_start);
1059 COPY_REG_SET (new_bb->global_live_at_end, bb->global_live_at_end);
1062 new_bb->loop_depth = bb->loop_depth;
1063 new_bb->flags = bb->flags;
1064 for (s = bb->succ; s; s = s->succ_next)
1066 /* Since we are creating edges from a new block to successors
1067 of another block (which therefore are known to be disjoint), there
1068 is no need to actually check for duplicated edges. */
1069 n = unchecked_make_edge (new_bb, s->dest, s->flags);
1070 n->probability = s->probability;
1071 if (e && bb->count)
1073 /* Take care for overflows! */
1074 n->count = s->count * (new_count * 10000 / bb->count) / 10000;
1075 s->count -= n->count;
1077 else
1078 n->count = s->count;
1079 n->aux = s->aux;
1082 if (e)
1084 new_bb->count = new_count;
1085 bb->count -= new_count;
1087 new_bb->frequency = EDGE_FREQUENCY (e);
1088 bb->frequency -= EDGE_FREQUENCY (e);
1090 redirect_edge_and_branch_force (e, new_bb);
1092 if (bb->count < 0)
1093 bb->count = 0;
1094 if (bb->frequency < 0)
1095 bb->frequency = 0;
1097 else
1099 new_bb->count = bb->count;
1100 new_bb->frequency = bb->frequency;
1103 new_bb->rbi->original = bb;
1104 bb->rbi->copy = new_bb;
1106 return new_bb;
1109 void
1110 cfg_layout_initialize_rbi (basic_block bb)
1112 if (bb->rbi)
1113 abort ();
1114 bb->rbi = pool_alloc (cfg_layout_pool);
1115 memset (bb->rbi, 0, sizeof (struct reorder_block_def));
1118 /* Main entry point to this module - initialize the datastructures for
1119 CFG layout changes. It keeps LOOPS up-to-date if not null. */
1121 void
1122 cfg_layout_initialize (void)
1124 basic_block bb;
1126 /* Our algorithm depends on fact that there are now dead jumptables
1127 around the code. */
1128 cfg_layout_pool =
1129 create_alloc_pool ("cfg layout pool", sizeof (struct reorder_block_def),
1130 n_basic_blocks + 2);
1131 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1132 cfg_layout_initialize_rbi (bb);
1134 cfg_layout_rtl_register_cfg_hooks ();
1136 record_effective_endpoints ();
1138 cleanup_cfg (CLEANUP_CFGLAYOUT);
1141 /* Splits superblocks. */
1142 static void
1143 break_superblocks (void)
1145 sbitmap superblocks;
1146 int i, need;
1148 superblocks = sbitmap_alloc (n_basic_blocks);
1149 sbitmap_zero (superblocks);
1151 need = 0;
1153 for (i = 0; i < n_basic_blocks; i++)
1154 if (BASIC_BLOCK(i)->flags & BB_SUPERBLOCK)
1156 BASIC_BLOCK(i)->flags &= ~BB_SUPERBLOCK;
1157 SET_BIT (superblocks, i);
1158 need = 1;
1161 if (need)
1163 rebuild_jump_labels (get_insns ());
1164 find_many_sub_basic_blocks (superblocks);
1167 free (superblocks);
1170 /* Finalize the changes: reorder insn list according to the sequence, enter
1171 compensation code, rebuild scope forest. */
1173 void
1174 cfg_layout_finalize (void)
1176 basic_block bb;
1178 #ifdef ENABLE_CHECKING
1179 verify_flow_info ();
1180 #endif
1181 rtl_register_cfg_hooks ();
1182 fixup_fallthru_exit_predecessor ();
1183 fixup_reorder_chain ();
1185 #ifdef ENABLE_CHECKING
1186 verify_insn_chain ();
1187 #endif
1189 free_alloc_pool (cfg_layout_pool);
1190 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1191 bb->rbi = NULL;
1193 break_superblocks ();
1195 #ifdef ENABLE_CHECKING
1196 verify_flow_info ();
1197 #endif
1200 /* Checks whether all N blocks in BBS array can be copied. */
1201 bool
1202 can_copy_bbs_p (basic_block *bbs, unsigned n)
1204 unsigned i;
1205 edge e;
1206 int ret = true;
1208 for (i = 0; i < n; i++)
1209 bbs[i]->rbi->duplicated = 1;
1211 for (i = 0; i < n; i++)
1213 /* In case we should redirect abnormal edge during duplication, fail. */
1214 for (e = bbs[i]->succ; e; e = e->succ_next)
1215 if ((e->flags & EDGE_ABNORMAL)
1216 && e->dest->rbi->duplicated)
1218 ret = false;
1219 goto end;
1222 if (!cfg_layout_can_duplicate_bb_p (bbs[i]))
1224 ret = false;
1225 break;
1229 end:
1230 for (i = 0; i < n; i++)
1231 bbs[i]->rbi->duplicated = 0;
1233 return ret;
1236 /* Duplicates N basic blocks stored in array BBS. Newly created basic blocks
1237 are placed into array NEW_BBS in the same order. Edges from basic blocks
1238 in BBS are also duplicated and copies of those of them
1239 that lead into BBS are redirected to appropriate newly created block. The
1240 function assigns bbs into loops (copy of basic block bb is assigned to
1241 bb->loop_father->copy loop, so this must be set up correctly in advance)
1242 and updates dominators locally (LOOPS structure that contains the information
1243 about dominators is passed to enable this).
1245 BASE is the superloop to that basic block belongs; if its header or latch
1246 is copied, we do not set the new blocks as header or latch.
1248 Created copies of N_EDGES edges in array EDGES are stored in array NEW_EDGES,
1249 also in the same order. */
1251 void
1252 copy_bbs (basic_block *bbs, unsigned n, basic_block *new_bbs,
1253 edge *edges, unsigned n_edges, edge *new_edges,
1254 struct loop *base, struct loops *loops)
1256 unsigned i, j;
1257 basic_block bb, new_bb, dom_bb;
1258 edge e;
1260 /* Duplicate bbs, update dominators, assign bbs to loops. */
1261 for (i = 0; i < n; i++)
1263 /* Duplicate. */
1264 bb = bbs[i];
1265 new_bb = new_bbs[i] = cfg_layout_duplicate_bb (bb, NULL);
1266 bb->rbi->duplicated = 1;
1267 /* Add to loop. */
1268 add_bb_to_loop (new_bb, bb->loop_father->copy);
1269 add_to_dominance_info (loops->cfg.dom, new_bb);
1270 /* Possibly set header. */
1271 if (bb->loop_father->header == bb && bb->loop_father != base)
1272 new_bb->loop_father->header = new_bb;
1273 /* Or latch. */
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++)
1281 bb = bbs[i];
1282 new_bb = new_bbs[i];
1284 dom_bb = get_immediate_dominator (loops->cfg.dom, bb);
1285 if (dom_bb->rbi->duplicated)
1287 dom_bb = dom_bb->rbi->copy;
1288 set_immediate_dominator (loops->cfg.dom, new_bb, dom_bb);
1292 /* Redirect edges. */
1293 for (j = 0; j < n_edges; j++)
1294 new_edges[j] = NULL;
1295 for (i = 0; i < n; i++)
1297 new_bb = new_bbs[i];
1298 bb = bbs[i];
1300 for (e = new_bb->succ; e; e = e->succ_next)
1302 for (j = 0; j < n_edges; j++)
1303 if (edges[j] && edges[j]->src == bb && edges[j]->dest == e->dest)
1304 new_edges[j] = e;
1306 if (!e->dest->rbi->duplicated)
1307 continue;
1308 redirect_edge_and_branch_force (e, e->dest->rbi->copy);
1312 /* Clear information about duplicates. */
1313 for (i = 0; i < n; i++)
1314 bbs[i]->rbi->duplicated = 0;
1317 #include "gt-cfglayout.h"