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 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 with CFG and analyze it.
23 All other modules should not transform the datastructure directly and use
24 abstraction instead. The file is supposed to be ordered bottom-up.
26 Available functionality:
27 - Initialization/deallocation
28 init_flow, clear_edges
29 - CFG aware instruction chain manipulation
30 flow_delete_insn, flow_delete_insn_chain
31 - Basic block manipulation
32 create_basic_block, flow_delete_block, split_block, merge_blocks_nomove
33 - Infrastructure to determine quickly basic block for instruction.
34 compute_bb_for_insn, update_bb_for_insn, set_block_for_insn,
35 set_block_for_new_insns
37 make_edge, remove_edge
38 - Low level edge redirection (without updating instruction chain)
39 redirect_edge_succ, redirect_edge_succ_nodup, redirect_edge_pred
40 - High level edge redirection (with updating and optimizing instruction
42 block_label, redirect_edge_and_branch,
43 redirect_edge_and_branch_force, tidy_fallthru_edge
44 - Edge splitting and commiting to edges
45 split_edge, insert_insn_on_edge, commit_edge_insertions
46 - Dumpipng and debugging
47 dump_flow_info, debug_flow_info, dump_edge_info, dump_bb, debug_bb,
48 debug_bb_n, print_rtl_with_bb
49 - Consistency checking
51 - CFG updating after constant propagation
52 purge_dead_edges, purge_all_dead_edges
59 #include "hard-reg-set.h"
60 #include "basic-block.h"
70 /* The obstack on which the flow graph components are allocated. */
72 struct obstack flow_obstack;
73 static char *flow_firstobj;
75 /* Number of basic blocks in the current function. */
79 /* Number of edges in the current function. */
83 /* The basic block array. */
85 varray_type basic_block_info;
87 /* The special entry and exit blocks. */
89 struct basic_block_def entry_exit_blocks[2]
97 NULL, /* cond_local_set */
98 NULL, /* global_live_at_start */
99 NULL, /* global_live_at_end */
101 ENTRY_BLOCK, /* index */
110 NULL, /* head_tree */
114 NULL, /* local_set */
115 NULL, /* cond_local_set */
116 NULL, /* global_live_at_start */
117 NULL, /* global_live_at_end */
119 EXIT_BLOCK, /* index */
127 /* The basic block structure for every insn, indexed by uid. */
129 varray_type basic_block_for_insn;
131 /* The labels mentioned in non-jump rtl. Valid during find_basic_blocks. */
132 /* ??? Should probably be using LABEL_NUSES instead. It would take a
133 bit of surgery to be able to use or co-opt the routines in jump. */
135 rtx label_value_list;
136 rtx tail_recursion_label_list;
138 void debug_flow_info PARAMS ((void));
139 static int can_delete_note_p PARAMS ((rtx));
140 static int can_delete_label_p PARAMS ((rtx));
141 static void commit_one_edge_insertion PARAMS ((edge));
142 static bool try_redirect_by_replacing_jump PARAMS ((edge, basic_block));
143 static void expunge_block PARAMS ((basic_block));
144 static rtx last_loop_beg_note PARAMS ((rtx));
145 static bool back_edge_of_syntactic_loop_p PARAMS ((basic_block, basic_block));
147 /* Called once at intialization time. */
152 static int initialized;
156 gcc_obstack_init (&flow_obstack);
157 flow_firstobj = (char *) obstack_alloc (&flow_obstack, 0);
162 obstack_free (&flow_obstack, flow_firstobj);
163 flow_firstobj = (char *) obstack_alloc (&flow_obstack, 0);
167 /* Free the memory associated with the edge structures. */
175 for (i = 0; i < n_basic_blocks; ++i)
177 basic_block bb = BASIC_BLOCK (i);
179 for (e = bb->succ; e; e = n)
189 for (e = ENTRY_BLOCK_PTR->succ; e; e = n)
195 ENTRY_BLOCK_PTR->succ = 0;
196 EXIT_BLOCK_PTR->pred = 0;
201 /* Return true if NOTE is not one of the ones that must be kept paired,
202 so that we may simply delete them. */
205 can_delete_note_p (note)
208 return (NOTE_LINE_NUMBER (note) == NOTE_INSN_DELETED
209 || NOTE_LINE_NUMBER (note) == NOTE_INSN_BASIC_BLOCK);
212 /* True if a given label can be deleted. */
215 can_delete_label_p (label)
220 if (LABEL_PRESERVE_P (label))
223 for (x = forced_labels; x; x = XEXP (x, 1))
224 if (label == XEXP (x, 0))
226 for (x = label_value_list; x; x = XEXP (x, 1))
227 if (label == XEXP (x, 0))
229 for (x = exception_handler_labels; x; x = XEXP (x, 1))
230 if (label == XEXP (x, 0))
233 /* User declared labels must be preserved. */
234 if (LABEL_NAME (label) != 0)
240 /* Delete INSN by patching it out. Return the next insn. */
243 flow_delete_insn (insn)
246 rtx prev = PREV_INSN (insn);
247 rtx next = NEXT_INSN (insn);
250 PREV_INSN (insn) = NULL_RTX;
251 NEXT_INSN (insn) = NULL_RTX;
252 INSN_DELETED_P (insn) = 1;
255 NEXT_INSN (prev) = next;
257 PREV_INSN (next) = prev;
259 set_last_insn (prev);
261 if (GET_CODE (insn) == CODE_LABEL)
262 remove_node_from_expr_list (insn, &nonlocal_goto_handler_labels);
264 /* If deleting a jump, decrement the use count of the label. Deleting
265 the label itself should happen in the normal course of block merging. */
266 if (GET_CODE (insn) == JUMP_INSN
268 && GET_CODE (JUMP_LABEL (insn)) == CODE_LABEL)
269 LABEL_NUSES (JUMP_LABEL (insn))--;
271 /* Also if deleting an insn that references a label. */
272 else if ((note = find_reg_note (insn, REG_LABEL, NULL_RTX)) != NULL_RTX
273 && GET_CODE (XEXP (note, 0)) == CODE_LABEL)
274 LABEL_NUSES (XEXP (note, 0))--;
276 if (GET_CODE (insn) == JUMP_INSN
277 && (GET_CODE (PATTERN (insn)) == ADDR_VEC
278 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
280 rtx pat = PATTERN (insn);
281 int diff_vec_p = GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC;
282 int len = XVECLEN (pat, diff_vec_p);
285 for (i = 0; i < len; i++)
286 LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0))--;
292 /* Unlink a chain of insns between START and FINISH, leaving notes
293 that must be paired. */
296 flow_delete_insn_chain (start, finish)
299 /* Unchain the insns one by one. It would be quicker to delete all
300 of these with a single unchaining, rather than one at a time, but
301 we need to keep the NOTE's. */
307 next = NEXT_INSN (start);
308 if (GET_CODE (start) == NOTE && !can_delete_note_p (start))
310 else if (GET_CODE (start) == CODE_LABEL
311 && ! can_delete_label_p (start))
313 const char *name = LABEL_NAME (start);
314 PUT_CODE (start, NOTE);
315 NOTE_LINE_NUMBER (start) = NOTE_INSN_DELETED_LABEL;
316 NOTE_SOURCE_FILE (start) = name;
319 next = flow_delete_insn (start);
327 /* Create a new basic block consisting of the instructions between
328 HEAD and END inclusive. Reuses the note and basic block struct
329 in BB_NOTE, if any. */
332 create_basic_block (index, head, end, bb_note)
334 rtx head, end, bb_note;
339 && ! RTX_INTEGRATED_P (bb_note)
340 && (bb = NOTE_BASIC_BLOCK (bb_note)) != NULL
343 /* If we found an existing note, thread it back onto the chain. */
347 if (GET_CODE (head) == CODE_LABEL)
351 after = PREV_INSN (head);
355 if (after != bb_note && NEXT_INSN (after) != bb_note)
356 reorder_insns (bb_note, bb_note, after);
360 /* Otherwise we must create a note and a basic block structure.
361 Since we allow basic block structs in rtl, give the struct
362 the same lifetime by allocating it off the function obstack
363 rather than using malloc. */
365 bb = (basic_block) obstack_alloc (&flow_obstack, sizeof (*bb));
366 memset (bb, 0, sizeof (*bb));
368 if (GET_CODE (head) == CODE_LABEL)
369 bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, head);
372 bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, head);
375 NOTE_BASIC_BLOCK (bb_note) = bb;
378 /* Always include the bb note in the block. */
379 if (NEXT_INSN (end) == bb_note)
385 BASIC_BLOCK (index) = bb;
387 /* Tag the block so that we know it has been used when considering
388 other basic block notes. */
392 /* Remove block B from the basic block array and compact behind it. */
398 int i, n = n_basic_blocks;
400 for (i = b->index; i + 1 < n; ++i)
402 basic_block x = BASIC_BLOCK (i + 1);
407 basic_block_info->num_elements--;
411 /* Delete the insns in a (non-live) block. We physically delete every
412 non-deleted-note insn, and update the flow graph appropriately.
414 Return nonzero if we deleted an exception handler. */
416 /* ??? Preserving all such notes strikes me as wrong. It would be nice
417 to post-process the stream to remove empty blocks, loops, ranges, etc. */
420 flow_delete_block (b)
423 int deleted_handler = 0;
426 /* If the head of this block is a CODE_LABEL, then it might be the
427 label for an exception handler which can't be reached.
429 We need to remove the label from the exception_handler_label list
430 and remove the associated NOTE_INSN_EH_REGION_BEG and
431 NOTE_INSN_EH_REGION_END notes. */
435 never_reached_warning (insn);
437 if (GET_CODE (insn) == CODE_LABEL)
438 maybe_remove_eh_handler (insn);
440 /* Include any jump table following the basic block. */
442 if (GET_CODE (end) == JUMP_INSN
443 && (tmp = JUMP_LABEL (end)) != NULL_RTX
444 && (tmp = NEXT_INSN (tmp)) != NULL_RTX
445 && GET_CODE (tmp) == JUMP_INSN
446 && (GET_CODE (PATTERN (tmp)) == ADDR_VEC
447 || GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC))
450 /* Include any barrier that may follow the basic block. */
451 tmp = next_nonnote_insn (end);
452 if (tmp && GET_CODE (tmp) == BARRIER)
455 /* Selectively delete the entire chain. */
456 flow_delete_insn_chain (insn, end);
458 /* Remove the edges into and out of this block. Note that there may
459 indeed be edges in, if we are removing an unreachable loop. */
463 for (e = b->pred; e; e = next)
465 for (q = &e->src->succ; *q != e; q = &(*q)->succ_next)
472 for (e = b->succ; e; e = next)
474 for (q = &e->dest->pred; *q != e; q = &(*q)->pred_next)
486 /* Remove the basic block from the array, and compact behind it. */
489 return deleted_handler;
492 /* Records the basic block struct in BB_FOR_INSN, for every instruction
493 indexed by INSN_UID. MAX is the size of the array. */
496 compute_bb_for_insn (max)
501 if (basic_block_for_insn)
502 VARRAY_FREE (basic_block_for_insn);
503 VARRAY_BB_INIT (basic_block_for_insn, max, "basic_block_for_insn");
505 for (i = 0; i < n_basic_blocks; ++i)
507 basic_block bb = BASIC_BLOCK (i);
514 int uid = INSN_UID (insn);
516 VARRAY_BB (basic_block_for_insn, uid) = bb;
519 insn = NEXT_INSN (insn);
524 /* Update insns block within BB. */
527 update_bb_for_insn (bb)
532 if (! basic_block_for_insn)
535 for (insn = bb->head; ; insn = NEXT_INSN (insn))
537 set_block_for_insn (insn, bb);
544 /* Record INSN's block as BB. */
547 set_block_for_insn (insn, bb)
551 size_t uid = INSN_UID (insn);
552 if (uid >= basic_block_for_insn->num_elements)
556 /* Add one-eighth the size so we don't keep calling xrealloc. */
557 new_size = uid + (uid + 7) / 8;
559 VARRAY_GROW (basic_block_for_insn, new_size);
561 VARRAY_BB (basic_block_for_insn, uid) = bb;
564 /* When a new insn has been inserted into an existing block, it will
565 sometimes emit more than a single insn. This routine will set the
566 block number for the specified insn, and look backwards in the insn
567 chain to see if there are any other uninitialized insns immediately
568 previous to this one, and set the block number for them too. */
571 set_block_for_new_insns (insn, bb)
575 set_block_for_insn (insn, bb);
577 /* Scan the previous instructions setting the block number until we find
578 an instruction that has the block number set, or we find a note
580 for (insn = PREV_INSN (insn); insn != NULL_RTX; insn = PREV_INSN (insn))
582 if (GET_CODE (insn) == NOTE)
584 if ((unsigned) INSN_UID (insn) >= basic_block_for_insn->num_elements
585 || BLOCK_FOR_INSN (insn) == 0)
586 set_block_for_insn (insn, bb);
593 make_edge (edge_cache, src, dst, flags)
595 basic_block src, dst;
601 /* Don't bother with edge cache for ENTRY or EXIT; there aren't that
602 many edges to them, and we didn't allocate memory for it. */
603 use_edge_cache = (edge_cache
604 && src != ENTRY_BLOCK_PTR
605 && dst != EXIT_BLOCK_PTR);
607 /* Make sure we don't add duplicate edges. */
608 switch (use_edge_cache)
611 /* Quick test for non-existance of the edge. */
612 if (! TEST_BIT (edge_cache[src->index], dst->index))
615 /* The edge exists; early exit if no work to do. */
621 for (e = src->succ; e; e = e->succ_next)
630 e = (edge) xcalloc (1, sizeof (*e));
633 e->succ_next = src->succ;
634 e->pred_next = dst->pred;
643 SET_BIT (edge_cache[src->index], dst->index);
646 /* This function will remove an edge from the flow graph. */
652 edge last_pred = NULL;
653 edge last_succ = NULL;
655 basic_block src, dest;
658 for (tmp = src->succ; tmp && tmp != e; tmp = tmp->succ_next)
664 last_succ->succ_next = e->succ_next;
666 src->succ = e->succ_next;
668 for (tmp = dest->pred; tmp && tmp != e; tmp = tmp->pred_next)
674 last_pred->pred_next = e->pred_next;
676 dest->pred = e->pred_next;
682 /* Redirect an edge's successor from one block to another. */
685 redirect_edge_succ (e, new_succ)
687 basic_block new_succ;
691 /* Disconnect the edge from the old successor block. */
692 for (pe = &e->dest->pred; *pe != e; pe = &(*pe)->pred_next)
694 *pe = (*pe)->pred_next;
696 /* Reconnect the edge to the new successor block. */
697 e->pred_next = new_succ->pred;
702 /* Like previous but avoid possible dupplicate edge. */
705 redirect_edge_succ_nodup (e, new_succ)
707 basic_block new_succ;
710 /* Check whether the edge is already present. */
711 for (s = e->src->succ; s; s = s->succ_next)
712 if (s->dest == new_succ && s != e)
716 s->flags |= e->flags;
717 s->probability += e->probability;
718 s->count += e->count;
723 redirect_edge_succ (e, new_succ);
727 /* Redirect an edge's predecessor from one block to another. */
730 redirect_edge_pred (e, new_pred)
732 basic_block new_pred;
736 /* Disconnect the edge from the old predecessor block. */
737 for (pe = &e->src->succ; *pe != e; pe = &(*pe)->succ_next)
739 *pe = (*pe)->succ_next;
741 /* Reconnect the edge to the new predecessor block. */
742 e->succ_next = new_pred->succ;
747 /* Split a block BB after insn INSN creating a new fallthru edge.
748 Return the new edge. Note that to keep other parts of the compiler happy,
749 this function renumbers all the basic blocks so that the new
750 one has a number one greater than the block split. */
753 split_block (bb, insn)
763 /* There is no point splitting the block after its end. */
767 /* Create the new structures. */
768 new_bb = (basic_block) obstack_alloc (&flow_obstack, sizeof (*new_bb));
769 new_edge = (edge) xcalloc (1, sizeof (*new_edge));
772 memset (new_bb, 0, sizeof (*new_bb));
774 new_bb->head = NEXT_INSN (insn);
775 new_bb->end = bb->end;
778 new_bb->succ = bb->succ;
780 new_bb->pred = new_edge;
781 new_bb->count = bb->count;
782 new_bb->frequency = bb->frequency;
783 new_bb->loop_depth = bb->loop_depth;
786 new_edge->dest = new_bb;
787 new_edge->flags = EDGE_FALLTHRU;
788 new_edge->probability = REG_BR_PROB_BASE;
789 new_edge->count = bb->count;
791 /* Redirect the src of the successor edges of bb to point to new_bb. */
792 for (e = new_bb->succ; e; e = e->succ_next)
795 /* Place the new block just after the block being split. */
796 VARRAY_GROW (basic_block_info, ++n_basic_blocks);
798 /* Some parts of the compiler expect blocks to be number in
799 sequential order so insert the new block immediately after the
800 block being split.. */
802 for (i = n_basic_blocks - 1; i > j + 1; --i)
804 basic_block tmp = BASIC_BLOCK (i - 1);
805 BASIC_BLOCK (i) = tmp;
809 BASIC_BLOCK (i) = new_bb;
812 if (GET_CODE (new_bb->head) == CODE_LABEL)
814 /* Create the basic block note. */
815 bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK,
817 NOTE_BASIC_BLOCK (bb_note) = new_bb;
819 /* If the only thing in this new block was the label, make sure
820 the block note gets included. */
821 if (new_bb->head == new_bb->end)
822 new_bb->end = bb_note;
826 /* Create the basic block note. */
827 bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK,
829 NOTE_BASIC_BLOCK (bb_note) = new_bb;
830 new_bb->head = bb_note;
833 update_bb_for_insn (new_bb);
835 if (bb->global_live_at_start)
837 new_bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (&flow_obstack);
838 new_bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (&flow_obstack);
839 COPY_REG_SET (new_bb->global_live_at_end, bb->global_live_at_end);
841 /* We now have to calculate which registers are live at the end
842 of the split basic block and at the start of the new basic
843 block. Start with those registers that are known to be live
844 at the end of the original basic block and get
845 propagate_block to determine which registers are live. */
846 COPY_REG_SET (new_bb->global_live_at_start, bb->global_live_at_end);
847 propagate_block (new_bb, new_bb->global_live_at_start, NULL, NULL, 0);
848 COPY_REG_SET (bb->global_live_at_end,
849 new_bb->global_live_at_start);
855 /* Blocks A and B are to be merged into a single block A. The insns
856 are already contiguous, hence `nomove'. */
859 merge_blocks_nomove (a, b)
863 rtx b_head, b_end, a_end;
864 rtx del_first = NULL_RTX, del_last = NULL_RTX;
867 /* If there was a CODE_LABEL beginning B, delete it. */
870 if (GET_CODE (b_head) == CODE_LABEL)
872 /* Detect basic blocks with nothing but a label. This can happen
873 in particular at the end of a function. */
876 del_first = del_last = b_head;
877 b_head = NEXT_INSN (b_head);
880 /* Delete the basic block note. */
881 if (NOTE_INSN_BASIC_BLOCK_P (b_head))
888 b_head = NEXT_INSN (b_head);
891 /* If there was a jump out of A, delete it. */
893 if (GET_CODE (a_end) == JUMP_INSN)
897 for (prev = PREV_INSN (a_end); ; prev = PREV_INSN (prev))
898 if (GET_CODE (prev) != NOTE
899 || NOTE_LINE_NUMBER (prev) == NOTE_INSN_BASIC_BLOCK
906 /* If this was a conditional jump, we need to also delete
907 the insn that set cc0. */
908 if (only_sets_cc0_p (prev))
911 prev = prev_nonnote_insn (prev);
920 else if (GET_CODE (NEXT_INSN (a_end)) == BARRIER)
921 del_first = NEXT_INSN (a_end);
923 /* Delete everything marked above as well as crap that might be
924 hanging out between the two blocks. */
925 flow_delete_insn_chain (del_first, del_last);
927 /* Normally there should only be one successor of A and that is B, but
928 partway though the merge of blocks for conditional_execution we'll
929 be merging a TEST block with THEN and ELSE successors. Free the
930 whole lot of them and hope the caller knows what they're doing. */
932 remove_edge (a->succ);
934 /* Adjust the edges out of B for the new owner. */
935 for (e = b->succ; e; e = e->succ_next)
939 /* B hasn't quite yet ceased to exist. Attempt to prevent mishap. */
940 b->pred = b->succ = NULL;
942 /* Reassociate the insns of B with A. */
945 if (basic_block_for_insn)
947 BLOCK_FOR_INSN (b_head) = a;
948 while (b_head != b_end)
950 b_head = NEXT_INSN (b_head);
951 BLOCK_FOR_INSN (b_head) = a;
961 /* Return label in the head of basic block. Create one if it doesn't exist. */
967 if (block == EXIT_BLOCK_PTR)
969 if (GET_CODE (block->head) != CODE_LABEL)
971 block->head = emit_label_before (gen_label_rtx (), block->head);
972 if (basic_block_for_insn)
973 set_block_for_insn (block->head, block);
978 /* Attempt to perform edge redirection by replacing possibly complex jump
979 instruction by unconditional jump or removing jump completely.
980 This can apply only if all edges now point to the same block.
982 The parameters and return values are equivalent to redirect_edge_and_branch.
986 try_redirect_by_replacing_jump (e, target)
990 basic_block src = e->src;
991 rtx insn = src->end, kill_from;
996 /* Verify that all targets will be TARGET. */
997 for (tmp = src->succ; tmp; tmp = tmp->succ_next)
998 if (tmp->dest != target && tmp != e)
1000 if (tmp || !onlyjump_p (insn))
1003 /* Avoid removing branch with side effects. */
1004 set = single_set (insn);
1005 if (!set || side_effects_p (set))
1008 /* In case we zap a conditional jump, we'll need to kill
1009 the cc0 setter too. */
1012 if (reg_mentioned_p (cc0_rtx, PATTERN (insn)))
1013 kill_from = PREV_INSN (insn);
1016 /* See if we can create the fallthru edge. */
1017 if (can_fallthru (src, target))
1019 src->end = PREV_INSN (kill_from);
1021 fprintf (rtl_dump_file, "Removing jump %i.\n", INSN_UID (insn));
1024 /* Selectivly unlink whole insn chain. */
1025 flow_delete_insn_chain (kill_from, PREV_INSN (target->head));
1027 /* If this already is simplejump, redirect it. */
1028 else if (simplejump_p (insn))
1030 if (e->dest == target)
1033 fprintf (rtl_dump_file, "Redirecting jump %i from %i to %i.\n",
1034 INSN_UID (insn), e->dest->index, target->index);
1035 redirect_jump (insn, block_label (target), 0);
1037 /* Or replace possibly complicated jump insn by simple jump insn. */
1040 rtx target_label = block_label (target);
1043 src->end = emit_jump_insn_before (gen_jump (target_label), kill_from);
1044 JUMP_LABEL (src->end) = target_label;
1045 LABEL_NUSES (target_label)++;
1046 if (basic_block_for_insn)
1047 set_block_for_new_insns (src->end, src);
1049 fprintf (rtl_dump_file, "Replacing insn %i by jump %i\n",
1050 INSN_UID (insn), INSN_UID (src->end));
1052 flow_delete_insn_chain (kill_from, insn);
1054 barrier = next_nonnote_insn (src->end);
1055 if (!barrier || GET_CODE (barrier) != BARRIER)
1056 emit_barrier_after (src->end);
1059 /* Keep only one edge out and set proper flags. */
1060 while (src->succ->succ_next)
1061 remove_edge (src->succ);
1064 e->flags = EDGE_FALLTHRU;
1067 e->probability = REG_BR_PROB_BASE;
1068 e->count = src->count;
1070 /* We don't want a block to end on a line-number note since that has
1071 the potential of changing the code between -g and not -g. */
1072 while (GET_CODE (e->src->end) == NOTE
1073 && NOTE_LINE_NUMBER (e->src->end) >= 0)
1075 rtx prev = PREV_INSN (e->src->end);
1076 flow_delete_insn (e->src->end);
1080 if (e->dest != target)
1081 redirect_edge_succ (e, target);
1085 /* Return last loop_beg note appearing after INSN, before start of next
1086 basic block. Return INSN if there are no such notes.
1088 When emmiting jump to redirect an fallthru edge, it should always
1089 appear after the LOOP_BEG notes, as loop optimizer expect loop to
1090 eighter start by fallthru edge or jump following the LOOP_BEG note
1091 jumping to the loop exit test. */
1094 last_loop_beg_note (insn)
1098 insn = NEXT_INSN (insn);
1099 while (GET_CODE (insn) == NOTE
1100 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_BASIC_BLOCK)
1102 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
1104 insn = NEXT_INSN (insn);
1109 /* Attempt to change code to redirect edge E to TARGET.
1110 Don't do that on expense of adding new instructions or reordering
1113 Function can be also called with edge destionation equivalent to the
1114 TARGET. Then it should try the simplifications and do nothing if
1117 Return true if transformation suceeded. We still return flase in case
1118 E already destinated TARGET and we didn't managed to simplify instruction
1122 redirect_edge_and_branch (e, target)
1127 rtx old_label = e->dest->head;
1128 basic_block src = e->src;
1129 rtx insn = src->end;
1131 if (e->flags & EDGE_COMPLEX)
1134 if (try_redirect_by_replacing_jump (e, target))
1136 /* Do this fast path late, as we want above code to simplify for cases
1137 where called on single edge leaving basic block containing nontrivial
1139 else if (e->dest == target)
1142 /* We can only redirect non-fallthru edges of jump insn. */
1143 if (e->flags & EDGE_FALLTHRU)
1145 if (GET_CODE (insn) != JUMP_INSN)
1148 /* Recognize a tablejump and adjust all matching cases. */
1149 if ((tmp = JUMP_LABEL (insn)) != NULL_RTX
1150 && (tmp = NEXT_INSN (tmp)) != NULL_RTX
1151 && GET_CODE (tmp) == JUMP_INSN
1152 && (GET_CODE (PATTERN (tmp)) == ADDR_VEC
1153 || GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC))
1157 rtx new_label = block_label (target);
1159 if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
1160 vec = XVEC (PATTERN (tmp), 0);
1162 vec = XVEC (PATTERN (tmp), 1);
1164 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
1165 if (XEXP (RTVEC_ELT (vec, j), 0) == old_label)
1167 RTVEC_ELT (vec, j) = gen_rtx_LABEL_REF (Pmode, new_label);
1168 --LABEL_NUSES (old_label);
1169 ++LABEL_NUSES (new_label);
1172 /* Handle casesi dispatch insns */
1173 if ((tmp = single_set (insn)) != NULL
1174 && SET_DEST (tmp) == pc_rtx
1175 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
1176 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF
1177 && XEXP (XEXP (SET_SRC (tmp), 2), 0) == old_label)
1179 XEXP (SET_SRC (tmp), 2) = gen_rtx_LABEL_REF (VOIDmode,
1181 --LABEL_NUSES (old_label);
1182 ++LABEL_NUSES (new_label);
1187 /* ?? We may play the games with moving the named labels from
1188 one basic block to the other in case only one computed_jump is
1190 if (computed_jump_p (insn))
1193 /* A return instruction can't be redirected. */
1194 if (returnjump_p (insn))
1197 /* If the insn doesn't go where we think, we're confused. */
1198 if (JUMP_LABEL (insn) != old_label)
1200 redirect_jump (insn, block_label (target), 0);
1204 fprintf (rtl_dump_file, "Edge %i->%i redirected to %i\n",
1205 e->src->index, e->dest->index, target->index);
1206 if (e->dest != target)
1207 redirect_edge_succ_nodup (e, target);
1211 /* Redirect edge even at the expense of creating new jump insn or
1212 basic block. Return new basic block if created, NULL otherwise.
1213 Abort if converison is impossible. */
1216 redirect_edge_and_branch_force (e, target)
1226 if (redirect_edge_and_branch (e, target))
1228 if (e->dest == target)
1230 if (e->flags & EDGE_ABNORMAL)
1232 if (!(e->flags & EDGE_FALLTHRU))
1235 e->flags &= ~EDGE_FALLTHRU;
1236 label = block_label (target);
1237 /* Case of the fallthru block. */
1238 if (!e->src->succ->succ_next)
1240 e->src->end = emit_jump_insn_after (gen_jump (label),
1241 last_loop_beg_note (e->src->end));
1242 JUMP_LABEL (e->src->end) = label;
1243 LABEL_NUSES (label)++;
1244 if (basic_block_for_insn)
1245 set_block_for_new_insns (e->src->end, e->src);
1246 emit_barrier_after (e->src->end);
1248 fprintf (rtl_dump_file,
1249 "Emitting jump insn %i to redirect edge %i->%i to %i\n",
1250 INSN_UID (e->src->end), e->src->index, e->dest->index,
1252 redirect_edge_succ (e, target);
1255 /* Redirecting fallthru edge of the conditional needs extra work. */
1258 fprintf (rtl_dump_file,
1259 "Emitting jump insn %i in new BB to redirect edge %i->%i to %i\n",
1260 INSN_UID (e->src->end), e->src->index, e->dest->index,
1263 /* Create the new structures. */
1264 new_bb = (basic_block) obstack_alloc (&flow_obstack, sizeof (*new_bb));
1265 new_edge = (edge) xcalloc (1, sizeof (*new_edge));
1268 memset (new_bb, 0, sizeof (*new_bb));
1270 new_bb->end = new_bb->head = last_loop_beg_note (e->src->end);
1271 new_bb->succ = NULL;
1272 new_bb->pred = new_edge;
1273 new_bb->count = e->count;
1274 new_bb->frequency = EDGE_FREQUENCY (e);
1275 new_bb->loop_depth = e->dest->loop_depth;
1277 new_edge->flags = EDGE_FALLTHRU;
1278 new_edge->probability = e->probability;
1279 new_edge->count = e->count;
1281 if (target->global_live_at_start)
1283 new_bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1284 new_bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1285 COPY_REG_SET (new_bb->global_live_at_start,
1286 target->global_live_at_start);
1287 COPY_REG_SET (new_bb->global_live_at_end, new_bb->global_live_at_start);
1291 new_edge->src = e->src;
1292 new_edge->dest = new_bb;
1293 new_edge->succ_next = e->src->succ;
1294 e->src->succ = new_edge;
1295 new_edge->pred_next = NULL;
1297 /* Redirect old edge. */
1298 redirect_edge_succ (e, target);
1299 redirect_edge_pred (e, new_bb);
1300 e->probability = REG_BR_PROB_BASE;
1302 /* Place the new block just after the block being split. */
1303 VARRAY_GROW (basic_block_info, ++n_basic_blocks);
1305 /* Some parts of the compiler expect blocks to be number in
1306 sequential order so insert the new block immediately after the
1307 block being split.. */
1308 j = new_edge->src->index;
1309 for (i = n_basic_blocks - 1; i > j + 1; --i)
1311 basic_block tmp = BASIC_BLOCK (i - 1);
1312 BASIC_BLOCK (i) = tmp;
1316 BASIC_BLOCK (i) = new_bb;
1319 /* Create the basic block note. */
1320 bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, new_bb->head);
1321 NOTE_BASIC_BLOCK (bb_note) = new_bb;
1322 new_bb->head = bb_note;
1324 new_bb->end = emit_jump_insn_after (gen_jump (label), new_bb->head);
1325 JUMP_LABEL (new_bb->end) = label;
1326 LABEL_NUSES (label)++;
1327 if (basic_block_for_insn)
1328 set_block_for_new_insns (new_bb->end, new_bb);
1329 emit_barrier_after (new_bb->end);
1333 /* The given edge should potentially be a fallthru edge. If that is in
1334 fact true, delete the jump and barriers that are in the way. */
1337 tidy_fallthru_edge (e, b, c)
1343 /* ??? In a late-running flow pass, other folks may have deleted basic
1344 blocks by nopping out blocks, leaving multiple BARRIERs between here
1345 and the target label. They ought to be chastized and fixed.
1347 We can also wind up with a sequence of undeletable labels between
1348 one block and the next.
1350 So search through a sequence of barriers, labels, and notes for
1351 the head of block C and assert that we really do fall through. */
1353 if (next_real_insn (b->end) != next_real_insn (PREV_INSN (c->head)))
1356 /* Remove what will soon cease being the jump insn from the source block.
1357 If block B consisted only of this single jump, turn it into a deleted
1360 if (GET_CODE (q) == JUMP_INSN
1362 && (any_uncondjump_p (q)
1363 || (b->succ == e && e->succ_next == NULL)))
1366 /* If this was a conditional jump, we need to also delete
1367 the insn that set cc0. */
1368 if (any_condjump_p (q) && only_sets_cc0_p (PREV_INSN (q)))
1375 NOTE_LINE_NUMBER (q) = NOTE_INSN_DELETED;
1376 NOTE_SOURCE_FILE (q) = 0;
1382 /* We don't want a block to end on a line-number note since that has
1383 the potential of changing the code between -g and not -g. */
1384 while (GET_CODE (q) == NOTE && NOTE_LINE_NUMBER (q) >= 0)
1391 /* Selectively unlink the sequence. */
1392 if (q != PREV_INSN (c->head))
1393 flow_delete_insn_chain (NEXT_INSN (q), PREV_INSN (c->head));
1395 e->flags |= EDGE_FALLTHRU;
1398 /* Fix up edges that now fall through, or rather should now fall through
1399 but previously required a jump around now deleted blocks. Simplify
1400 the search by only examining blocks numerically adjacent, since this
1401 is how find_basic_blocks created them. */
1404 tidy_fallthru_edges ()
1408 for (i = 1; i < n_basic_blocks; ++i)
1410 basic_block b = BASIC_BLOCK (i - 1);
1411 basic_block c = BASIC_BLOCK (i);
1414 /* We care about simple conditional or unconditional jumps with
1417 If we had a conditional branch to the next instruction when
1418 find_basic_blocks was called, then there will only be one
1419 out edge for the block which ended with the conditional
1420 branch (since we do not create duplicate edges).
1422 Furthermore, the edge will be marked as a fallthru because we
1423 merge the flags for the duplicate edges. So we do not want to
1424 check that the edge is not a FALLTHRU edge. */
1425 if ((s = b->succ) != NULL
1426 && ! (s->flags & EDGE_COMPLEX)
1427 && s->succ_next == NULL
1429 /* If the jump insn has side effects, we can't tidy the edge. */
1430 && (GET_CODE (b->end) != JUMP_INSN
1431 || onlyjump_p (b->end)))
1432 tidy_fallthru_edge (s, b, c);
1436 /* Helper function for split_edge. Return true in case edge BB2 to BB1
1437 is back edge of syntactic loop. */
1440 back_edge_of_syntactic_loop_p (bb1, bb2)
1441 basic_block bb1, bb2;
1446 if (bb1->index > bb2->index)
1449 if (bb1->index == bb2->index)
1452 for (insn = bb1->end; insn != bb2->head && count >= 0;
1453 insn = NEXT_INSN (insn))
1454 if (GET_CODE (insn) == NOTE)
1456 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
1458 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
1465 /* Split a (typically critical) edge. Return the new block.
1466 Abort on abnormal edges.
1468 ??? The code generally expects to be called on critical edges.
1469 The case of a block ending in an unconditional jump to a
1470 block with multiple predecessors is not handled optimally. */
1473 split_edge (edge_in)
1476 basic_block old_pred, bb, old_succ;
1481 /* Abnormal edges cannot be split. */
1482 if ((edge_in->flags & EDGE_ABNORMAL) != 0)
1485 old_pred = edge_in->src;
1486 old_succ = edge_in->dest;
1488 /* Create the new structures. */
1489 bb = (basic_block) obstack_alloc (&flow_obstack, sizeof (*bb));
1490 edge_out = (edge) xcalloc (1, sizeof (*edge_out));
1493 memset (bb, 0, sizeof (*bb));
1495 /* ??? This info is likely going to be out of date very soon. */
1496 if (old_succ->global_live_at_start)
1498 bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1499 bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1500 COPY_REG_SET (bb->global_live_at_start, old_succ->global_live_at_start);
1501 COPY_REG_SET (bb->global_live_at_end, old_succ->global_live_at_start);
1505 bb->succ = edge_out;
1506 bb->count = edge_in->count;
1507 bb->frequency = EDGE_FREQUENCY (edge_in);
1509 edge_in->flags &= ~EDGE_CRITICAL;
1511 edge_out->pred_next = old_succ->pred;
1512 edge_out->succ_next = NULL;
1514 edge_out->dest = old_succ;
1515 edge_out->flags = EDGE_FALLTHRU;
1516 edge_out->probability = REG_BR_PROB_BASE;
1517 edge_out->count = edge_in->count;
1519 old_succ->pred = edge_out;
1521 /* Tricky case -- if there existed a fallthru into the successor
1522 (and we're not it) we must add a new unconditional jump around
1523 the new block we're actually interested in.
1525 Further, if that edge is critical, this means a second new basic
1526 block must be created to hold it. In order to simplify correct
1527 insn placement, do this before we touch the existing basic block
1528 ordering for the block we were really wanting. */
1529 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1532 for (e = edge_out->pred_next; e; e = e->pred_next)
1533 if (e->flags & EDGE_FALLTHRU)
1538 basic_block jump_block;
1541 if ((e->flags & EDGE_CRITICAL) == 0
1542 && e->src != ENTRY_BLOCK_PTR)
1544 /* Non critical -- we can simply add a jump to the end
1545 of the existing predecessor. */
1546 jump_block = e->src;
1550 /* We need a new block to hold the jump. The simplest
1551 way to do the bulk of the work here is to recursively
1553 jump_block = split_edge (e);
1554 e = jump_block->succ;
1557 /* Now add the jump insn ... */
1558 pos = emit_jump_insn_after (gen_jump (old_succ->head),
1559 last_loop_beg_note (jump_block->end));
1560 jump_block->end = pos;
1561 if (basic_block_for_insn)
1562 set_block_for_new_insns (pos, jump_block);
1563 emit_barrier_after (pos);
1565 /* ... let jump know that label is in use, ... */
1566 JUMP_LABEL (pos) = old_succ->head;
1567 ++LABEL_NUSES (old_succ->head);
1569 /* ... and clear fallthru on the outgoing edge. */
1570 e->flags &= ~EDGE_FALLTHRU;
1572 /* Continue splitting the interesting edge. */
1576 /* Place the new block just in front of the successor. */
1577 VARRAY_GROW (basic_block_info, ++n_basic_blocks);
1578 if (old_succ == EXIT_BLOCK_PTR)
1579 j = n_basic_blocks - 1;
1581 j = old_succ->index;
1582 for (i = n_basic_blocks - 1; i > j; --i)
1584 basic_block tmp = BASIC_BLOCK (i - 1);
1585 BASIC_BLOCK (i) = tmp;
1588 BASIC_BLOCK (i) = bb;
1591 /* Create the basic block note.
1593 Where we place the note can have a noticable impact on the generated
1594 code. Consider this cfg:
1604 If we need to insert an insn on the edge from block 0 to block 1,
1605 we want to ensure the instructions we insert are outside of any
1606 loop notes that physically sit between block 0 and block 1. Otherwise
1607 we confuse the loop optimizer into thinking the loop is a phony. */
1608 if (old_succ != EXIT_BLOCK_PTR
1609 && PREV_INSN (old_succ->head)
1610 && GET_CODE (PREV_INSN (old_succ->head)) == NOTE
1611 && NOTE_LINE_NUMBER (PREV_INSN (old_succ->head)) == NOTE_INSN_LOOP_BEG
1612 && !back_edge_of_syntactic_loop_p (old_succ, old_pred))
1613 bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK,
1614 PREV_INSN (old_succ->head));
1615 else if (old_succ != EXIT_BLOCK_PTR)
1616 bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, old_succ->head);
1618 bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, get_last_insn ());
1619 NOTE_BASIC_BLOCK (bb_note) = bb;
1620 bb->head = bb->end = bb_note;
1622 /* For non-fallthry edges, we must adjust the predecessor's
1623 jump instruction to target our new block. */
1624 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1626 if (!redirect_edge_and_branch (edge_in, bb))
1630 redirect_edge_succ (edge_in, bb);
1635 /* Queue instructions for insertion on an edge between two basic blocks.
1636 The new instructions and basic blocks (if any) will not appear in the
1637 CFG until commit_edge_insertions is called. */
1640 insert_insn_on_edge (pattern, e)
1644 /* We cannot insert instructions on an abnormal critical edge.
1645 It will be easier to find the culprit if we die now. */
1646 if ((e->flags & (EDGE_ABNORMAL|EDGE_CRITICAL))
1647 == (EDGE_ABNORMAL|EDGE_CRITICAL))
1650 if (e->insns == NULL_RTX)
1653 push_to_sequence (e->insns);
1655 emit_insn (pattern);
1657 e->insns = get_insns ();
1661 /* Update the CFG for the instructions queued on edge E. */
1664 commit_one_edge_insertion (e)
1667 rtx before = NULL_RTX, after = NULL_RTX, insns, tmp, last;
1670 /* Pull the insns off the edge now since the edge might go away. */
1672 e->insns = NULL_RTX;
1674 /* Figure out where to put these things. If the destination has
1675 one predecessor, insert there. Except for the exit block. */
1676 if (e->dest->pred->pred_next == NULL
1677 && e->dest != EXIT_BLOCK_PTR)
1681 /* Get the location correct wrt a code label, and "nice" wrt
1682 a basic block note, and before everything else. */
1684 if (GET_CODE (tmp) == CODE_LABEL)
1685 tmp = NEXT_INSN (tmp);
1686 if (NOTE_INSN_BASIC_BLOCK_P (tmp))
1687 tmp = NEXT_INSN (tmp);
1688 if (tmp == bb->head)
1691 after = PREV_INSN (tmp);
1694 /* If the source has one successor and the edge is not abnormal,
1695 insert there. Except for the entry block. */
1696 else if ((e->flags & EDGE_ABNORMAL) == 0
1697 && e->src->succ->succ_next == NULL
1698 && e->src != ENTRY_BLOCK_PTR)
1701 /* It is possible to have a non-simple jump here. Consider a target
1702 where some forms of unconditional jumps clobber a register. This
1703 happens on the fr30 for example.
1705 We know this block has a single successor, so we can just emit
1706 the queued insns before the jump. */
1707 if (GET_CODE (bb->end) == JUMP_INSN)
1710 while (GET_CODE (PREV_INSN (before)) == NOTE
1711 && NOTE_LINE_NUMBER (PREV_INSN (before)) == NOTE_INSN_LOOP_BEG)
1712 before = PREV_INSN (before);
1716 /* We'd better be fallthru, or we've lost track of what's what. */
1717 if ((e->flags & EDGE_FALLTHRU) == 0)
1724 /* Otherwise we must split the edge. */
1727 bb = split_edge (e);
1731 /* Now that we've found the spot, do the insertion. */
1733 /* Set the new block number for these insns, if structure is allocated. */
1734 if (basic_block_for_insn)
1737 for (i = insns; i != NULL_RTX; i = NEXT_INSN (i))
1738 set_block_for_insn (i, bb);
1743 emit_insns_before (insns, before);
1744 if (before == bb->head)
1747 last = prev_nonnote_insn (before);
1751 last = emit_insns_after (insns, after);
1752 if (after == bb->end)
1756 if (returnjump_p (last))
1758 /* ??? Remove all outgoing edges from BB and add one for EXIT.
1759 This is not currently a problem because this only happens
1760 for the (single) epilogue, which already has a fallthru edge
1764 if (e->dest != EXIT_BLOCK_PTR
1765 || e->succ_next != NULL
1766 || (e->flags & EDGE_FALLTHRU) == 0)
1768 e->flags &= ~EDGE_FALLTHRU;
1770 emit_barrier_after (last);
1774 flow_delete_insn (before);
1776 else if (GET_CODE (last) == JUMP_INSN)
1778 find_sub_basic_blocks (bb);
1781 /* Update the CFG for all queued instructions. */
1784 commit_edge_insertions ()
1788 compute_bb_for_insn (get_max_uid ());
1790 #ifdef ENABLE_CHECKING
1791 verify_flow_info ();
1795 bb = ENTRY_BLOCK_PTR;
1800 for (e = bb->succ; e; e = next)
1802 next = e->succ_next;
1804 commit_one_edge_insertion (e);
1807 if (++i >= n_basic_blocks)
1809 bb = BASIC_BLOCK (i);
1814 dump_flow_info (file)
1818 static const char * const reg_class_names[] = REG_CLASS_NAMES;
1820 fprintf (file, "%d registers.\n", max_regno);
1821 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1824 enum reg_class class, altclass;
1825 fprintf (file, "\nRegister %d used %d times across %d insns",
1826 i, REG_N_REFS (i), REG_LIVE_LENGTH (i));
1827 if (REG_BASIC_BLOCK (i) >= 0)
1828 fprintf (file, " in block %d", REG_BASIC_BLOCK (i));
1830 fprintf (file, "; set %d time%s", REG_N_SETS (i),
1831 (REG_N_SETS (i) == 1) ? "" : "s");
1832 if (REG_USERVAR_P (regno_reg_rtx[i]))
1833 fprintf (file, "; user var");
1834 if (REG_N_DEATHS (i) != 1)
1835 fprintf (file, "; dies in %d places", REG_N_DEATHS (i));
1836 if (REG_N_CALLS_CROSSED (i) == 1)
1837 fprintf (file, "; crosses 1 call");
1838 else if (REG_N_CALLS_CROSSED (i))
1839 fprintf (file, "; crosses %d calls", REG_N_CALLS_CROSSED (i));
1840 if (PSEUDO_REGNO_BYTES (i) != UNITS_PER_WORD)
1841 fprintf (file, "; %d bytes", PSEUDO_REGNO_BYTES (i));
1842 class = reg_preferred_class (i);
1843 altclass = reg_alternate_class (i);
1844 if (class != GENERAL_REGS || altclass != ALL_REGS)
1846 if (altclass == ALL_REGS || class == ALL_REGS)
1847 fprintf (file, "; pref %s", reg_class_names[(int) class]);
1848 else if (altclass == NO_REGS)
1849 fprintf (file, "; %s or none", reg_class_names[(int) class]);
1851 fprintf (file, "; pref %s, else %s",
1852 reg_class_names[(int) class],
1853 reg_class_names[(int) altclass]);
1855 if (REG_POINTER (regno_reg_rtx[i]))
1856 fprintf (file, "; pointer");
1857 fprintf (file, ".\n");
1860 fprintf (file, "\n%d basic blocks, %d edges.\n", n_basic_blocks, n_edges);
1861 for (i = 0; i < n_basic_blocks; i++)
1863 register basic_block bb = BASIC_BLOCK (i);
1866 fprintf (file, "\nBasic block %d: first insn %d, last %d, loop_depth %d, count ",
1867 i, INSN_UID (bb->head), INSN_UID (bb->end), bb->loop_depth);
1868 fprintf (file, HOST_WIDEST_INT_PRINT_DEC, (HOST_WIDEST_INT) bb->count);
1869 fprintf (file, ", freq %i.\n", bb->frequency);
1871 fprintf (file, "Predecessors: ");
1872 for (e = bb->pred; e; e = e->pred_next)
1873 dump_edge_info (file, e, 0);
1875 fprintf (file, "\nSuccessors: ");
1876 for (e = bb->succ; e; e = e->succ_next)
1877 dump_edge_info (file, e, 1);
1879 fprintf (file, "\nRegisters live at start:");
1880 dump_regset (bb->global_live_at_start, file);
1882 fprintf (file, "\nRegisters live at end:");
1883 dump_regset (bb->global_live_at_end, file);
1894 dump_flow_info (stderr);
1898 dump_edge_info (file, e, do_succ)
1903 basic_block side = (do_succ ? e->dest : e->src);
1905 if (side == ENTRY_BLOCK_PTR)
1906 fputs (" ENTRY", file);
1907 else if (side == EXIT_BLOCK_PTR)
1908 fputs (" EXIT", file);
1910 fprintf (file, " %d", side->index);
1913 fprintf (file, " [%.1f%%] ", e->probability * 100.0 / REG_BR_PROB_BASE);
1917 fprintf (file, " count:");
1918 fprintf (file, HOST_WIDEST_INT_PRINT_DEC, (HOST_WIDEST_INT) e->count);
1923 static const char * const bitnames[] = {
1924 "fallthru", "crit", "ab", "abcall", "eh", "fake", "dfs_back"
1927 int i, flags = e->flags;
1931 for (i = 0; flags; i++)
1932 if (flags & (1 << i))
1938 if (i < (int) ARRAY_SIZE (bitnames))
1939 fputs (bitnames[i], file);
1941 fprintf (file, "%d", i);
1948 /* Print out one basic block with live information at start and end. */
1959 fprintf (outf, ";; Basic block %d, loop depth %d, count ",
1960 bb->index, bb->loop_depth);
1961 fprintf (outf, HOST_WIDEST_INT_PRINT_DEC, (HOST_WIDEST_INT) bb->count);
1964 fputs (";; Predecessors: ", outf);
1965 for (e = bb->pred; e; e = e->pred_next)
1966 dump_edge_info (outf, e, 0);
1969 fputs (";; Registers live at start:", outf);
1970 dump_regset (bb->global_live_at_start, outf);
1973 for (insn = bb->head, last = NEXT_INSN (bb->end);
1975 insn = NEXT_INSN (insn))
1976 print_rtl_single (outf, insn);
1978 fputs (";; Registers live at end:", outf);
1979 dump_regset (bb->global_live_at_end, outf);
1982 fputs (";; Successors: ", outf);
1983 for (e = bb->succ; e; e = e->succ_next)
1984 dump_edge_info (outf, e, 1);
1992 dump_bb (bb, stderr);
1999 dump_bb (BASIC_BLOCK (n), stderr);
2002 /* Like print_rtl, but also print out live information for the start of each
2006 print_rtl_with_bb (outf, rtx_first)
2010 register rtx tmp_rtx;
2013 fprintf (outf, "(nil)\n");
2017 enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
2018 int max_uid = get_max_uid ();
2019 basic_block *start = (basic_block *)
2020 xcalloc (max_uid, sizeof (basic_block));
2021 basic_block *end = (basic_block *)
2022 xcalloc (max_uid, sizeof (basic_block));
2023 enum bb_state *in_bb_p = (enum bb_state *)
2024 xcalloc (max_uid, sizeof (enum bb_state));
2026 for (i = n_basic_blocks - 1; i >= 0; i--)
2028 basic_block bb = BASIC_BLOCK (i);
2031 start[INSN_UID (bb->head)] = bb;
2032 end[INSN_UID (bb->end)] = bb;
2033 for (x = bb->head; x != NULL_RTX; x = NEXT_INSN (x))
2035 enum bb_state state = IN_MULTIPLE_BB;
2036 if (in_bb_p[INSN_UID (x)] == NOT_IN_BB)
2038 in_bb_p[INSN_UID (x)] = state;
2045 for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
2050 if ((bb = start[INSN_UID (tmp_rtx)]) != NULL)
2052 fprintf (outf, ";; Start of basic block %d, registers live:",
2054 dump_regset (bb->global_live_at_start, outf);
2058 if (in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB
2059 && GET_CODE (tmp_rtx) != NOTE
2060 && GET_CODE (tmp_rtx) != BARRIER)
2061 fprintf (outf, ";; Insn is not within a basic block\n");
2062 else if (in_bb_p[INSN_UID (tmp_rtx)] == IN_MULTIPLE_BB)
2063 fprintf (outf, ";; Insn is in multiple basic blocks\n");
2065 did_output = print_rtl_single (outf, tmp_rtx);
2067 if ((bb = end[INSN_UID (tmp_rtx)]) != NULL)
2069 fprintf (outf, ";; End of basic block %d, registers live:\n",
2071 dump_regset (bb->global_live_at_end, outf);
2084 if (current_function_epilogue_delay_list != 0)
2086 fprintf (outf, "\n;; Insns in epilogue delay list:\n\n");
2087 for (tmp_rtx = current_function_epilogue_delay_list; tmp_rtx != 0;
2088 tmp_rtx = XEXP (tmp_rtx, 1))
2089 print_rtl_single (outf, XEXP (tmp_rtx, 0));
2093 /* Verify the CFG consistency. This function check some CFG invariants and
2094 aborts when something is wrong. Hope that this function will help to
2095 convert many optimization passes to preserve CFG consistent.
2097 Currently it does following checks:
2099 - test head/end pointers
2100 - overlapping of basic blocks
2101 - edge list correctness
2102 - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note)
2103 - tails of basic blocks (ensure that boundary is necesary)
2104 - scans body of the basic block for JUMP_INSN, CODE_LABEL
2105 and NOTE_INSN_BASIC_BLOCK
2106 - check that all insns are in the basic blocks
2107 (except the switch handling code, barriers and notes)
2108 - check that all returns are followed by barriers
2110 In future it can be extended check a lot of other stuff as well
2111 (reachability of basic blocks, life information, etc. etc.). */
2116 const int max_uid = get_max_uid ();
2117 const rtx rtx_first = get_insns ();
2118 rtx last_head = get_last_insn ();
2119 basic_block *bb_info, *last_visited;
2120 size_t *edge_checksum;
2122 int i, last_bb_num_seen, num_bb_notes, err = 0;
2124 bb_info = (basic_block *) xcalloc (max_uid, sizeof (basic_block));
2125 last_visited = (basic_block *) xcalloc (n_basic_blocks + 2,
2126 sizeof (basic_block));
2127 edge_checksum = (size_t *) xcalloc (n_basic_blocks + 2, sizeof (size_t));
2129 for (i = n_basic_blocks - 1; i >= 0; i--)
2131 basic_block bb = BASIC_BLOCK (i);
2132 rtx head = bb->head;
2135 /* Verify the end of the basic block is in the INSN chain. */
2136 for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
2141 error ("End insn %d for block %d not found in the insn stream.",
2142 INSN_UID (end), bb->index);
2146 /* Work backwards from the end to the head of the basic block
2147 to verify the head is in the RTL chain. */
2148 for (; x != NULL_RTX; x = PREV_INSN (x))
2150 /* While walking over the insn chain, verify insns appear
2151 in only one basic block and initialize the BB_INFO array
2152 used by other passes. */
2153 if (bb_info[INSN_UID (x)] != NULL)
2155 error ("Insn %d is in multiple basic blocks (%d and %d)",
2156 INSN_UID (x), bb->index, bb_info[INSN_UID (x)]->index);
2159 bb_info[INSN_UID (x)] = bb;
2166 error ("Head insn %d for block %d not found in the insn stream.",
2167 INSN_UID (head), bb->index);
2174 /* Now check the basic blocks (boundaries etc.) */
2175 for (i = n_basic_blocks - 1; i >= 0; i--)
2177 basic_block bb = BASIC_BLOCK (i);
2178 int has_fallthru = 0;
2184 if (last_visited [e->dest->index + 2] == bb)
2186 error ("verify_flow_info: Duplicate edge %i->%i",
2187 e->src->index, e->dest->index);
2190 last_visited [e->dest->index + 2] = bb;
2192 if (e->flags & EDGE_FALLTHRU)
2195 if ((e->flags & EDGE_FALLTHRU)
2196 && e->src != ENTRY_BLOCK_PTR
2197 && e->dest != EXIT_BLOCK_PTR)
2200 if (e->src->index + 1 != e->dest->index)
2202 error ("verify_flow_info: Incorrect blocks for fallthru %i->%i",
2203 e->src->index, e->dest->index);
2207 for (insn = NEXT_INSN (e->src->end); insn != e->dest->head;
2208 insn = NEXT_INSN (insn))
2209 if (GET_CODE (insn) == BARRIER || INSN_P (insn))
2211 error ("verify_flow_info: Incorrect fallthru %i->%i",
2212 e->src->index, e->dest->index);
2213 fatal_insn ("Wrong insn in the fallthru edge", insn);
2219 error ("verify_flow_info: Basic block %d succ edge is corrupted",
2221 fprintf (stderr, "Predecessor: ");
2222 dump_edge_info (stderr, e, 0);
2223 fprintf (stderr, "\nSuccessor: ");
2224 dump_edge_info (stderr, e, 1);
2225 fprintf (stderr, "\n");
2228 edge_checksum[e->dest->index + 2] += (size_t) e;
2235 /* Ensure existence of barrier in BB with no fallthru edges. */
2236 for (insn = bb->end; GET_CODE (insn) != BARRIER;
2237 insn = NEXT_INSN (insn))
2239 || (GET_CODE (insn) == NOTE
2240 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_BASIC_BLOCK))
2242 error ("Missing barrier after block %i", bb->index);
2252 error ("Basic block %d pred edge is corrupted", bb->index);
2253 fputs ("Predecessor: ", stderr);
2254 dump_edge_info (stderr, e, 0);
2255 fputs ("\nSuccessor: ", stderr);
2256 dump_edge_info (stderr, e, 1);
2257 fputc ('\n', stderr);
2260 edge_checksum[e->dest->index + 2] -= (size_t) e;
2264 /* OK pointers are correct. Now check the header of basic
2265 block. It ought to contain optional CODE_LABEL followed
2266 by NOTE_BASIC_BLOCK. */
2268 if (GET_CODE (x) == CODE_LABEL)
2272 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
2278 if (!NOTE_INSN_BASIC_BLOCK_P (x) || NOTE_BASIC_BLOCK (x) != bb)
2280 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
2287 /* Do checks for empty blocks here */
2294 if (NOTE_INSN_BASIC_BLOCK_P (x))
2296 error ("NOTE_INSN_BASIC_BLOCK %d in the middle of basic block %d",
2297 INSN_UID (x), bb->index);
2304 if (GET_CODE (x) == JUMP_INSN
2305 || GET_CODE (x) == CODE_LABEL
2306 || GET_CODE (x) == BARRIER)
2308 error ("In basic block %d:", bb->index);
2309 fatal_insn ("Flow control insn inside a basic block", x);
2317 /* Complete edge checksumming for ENTRY and EXIT. */
2320 for (e = ENTRY_BLOCK_PTR->succ; e ; e = e->succ_next)
2321 edge_checksum[e->dest->index + 2] += (size_t) e;
2322 for (e = EXIT_BLOCK_PTR->pred; e ; e = e->pred_next)
2323 edge_checksum[e->dest->index + 2] -= (size_t) e;
2326 for (i = -2; i < n_basic_blocks; ++i)
2327 if (edge_checksum[i + 2])
2329 error ("Basic block %i edge lists are corrupted", i);
2333 last_bb_num_seen = -1;
2338 if (NOTE_INSN_BASIC_BLOCK_P (x))
2340 basic_block bb = NOTE_BASIC_BLOCK (x);
2342 if (bb->index != last_bb_num_seen + 1)
2343 internal_error ("Basic blocks not numbered consecutively.");
2345 last_bb_num_seen = bb->index;
2348 if (!bb_info[INSN_UID (x)])
2350 switch (GET_CODE (x))
2357 /* An addr_vec is placed outside any block block. */
2359 && GET_CODE (NEXT_INSN (x)) == JUMP_INSN
2360 && (GET_CODE (PATTERN (NEXT_INSN (x))) == ADDR_DIFF_VEC
2361 || GET_CODE (PATTERN (NEXT_INSN (x))) == ADDR_VEC))
2366 /* But in any case, non-deletable labels can appear anywhere. */
2370 fatal_insn ("Insn outside basic block", x);
2375 && GET_CODE (x) == JUMP_INSN
2376 && returnjump_p (x) && ! condjump_p (x)
2377 && ! (NEXT_INSN (x) && GET_CODE (NEXT_INSN (x)) == BARRIER))
2378 fatal_insn ("Return not followed by barrier", x);
2383 if (num_bb_notes != n_basic_blocks)
2385 ("number of bb notes in insn chain (%d) != n_basic_blocks (%d)",
2386 num_bb_notes, n_basic_blocks);
2389 internal_error ("verify_flow_info failed.");
2393 free (last_visited);
2394 free (edge_checksum);
2398 /* Assume that the preceeding pass has possibly eliminated jump instructions
2399 or converted the unconditional jumps. Eliminate the edges from CFG.
2400 Return true if any edges are eliminated. */
2403 purge_dead_edges (bb)
2408 bool purged = false;
2410 if (GET_CODE (insn) == JUMP_INSN && !simplejump_p (insn))
2412 if (GET_CODE (insn) == JUMP_INSN)
2416 /* We do care only about conditional jumps and simplejumps. */
2417 if (!any_condjump_p (insn)
2418 && !returnjump_p (insn)
2419 && !simplejump_p (insn))
2421 for (e = bb->succ; e; e = next)
2423 next = e->succ_next;
2425 /* Check purposes we can have edge. */
2426 if ((e->flags & EDGE_FALLTHRU)
2427 && any_condjump_p (insn))
2429 if (e->dest != EXIT_BLOCK_PTR
2430 && e->dest->head == JUMP_LABEL (insn))
2432 if (e->dest == EXIT_BLOCK_PTR
2433 && returnjump_p (insn))
2438 if (!bb->succ || !purged)
2441 fprintf (rtl_dump_file, "Purged edges from bb %i\n", bb->index);
2445 /* Redistribute probabilities. */
2446 if (!bb->succ->succ_next)
2448 bb->succ->probability = REG_BR_PROB_BASE;
2449 bb->succ->count = bb->count;
2453 note = find_reg_note (insn, REG_BR_PROB, NULL);
2456 b = BRANCH_EDGE (bb);
2457 f = FALLTHRU_EDGE (bb);
2458 b->probability = INTVAL (XEXP (note, 0));
2459 f->probability = REG_BR_PROB_BASE - b->probability;
2460 b->count = bb->count * b->probability / REG_BR_PROB_BASE;
2461 f->count = bb->count * f->probability / REG_BR_PROB_BASE;
2466 /* Cleanup abnormal edges caused by throwing insns that have been
2468 if (! can_throw_internal (bb->end))
2469 for (e = bb->succ; e; e = next)
2471 next = e->succ_next;
2472 if (e->flags & EDGE_EH)
2479 /* If we don't see a jump insn, we don't know exactly why the block would
2480 have been broken at this point. Look for a simple, non-fallthru edge,
2481 as these are only created by conditional branches. If we find such an
2482 edge we know that there used to be a jump here and can then safely
2483 remove all non-fallthru edges. */
2484 for (e = bb->succ; e && (e->flags & (EDGE_COMPLEX | EDGE_FALLTHRU));
2488 for (e = bb->succ; e; e = next)
2490 next = e->succ_next;
2491 if (!(e->flags & EDGE_FALLTHRU))
2492 remove_edge (e), purged = true;
2494 if (!bb->succ || bb->succ->succ_next)
2496 bb->succ->probability = REG_BR_PROB_BASE;
2497 bb->succ->count = bb->count;
2500 fprintf (rtl_dump_file, "Purged non-fallthru edges from bb %i\n",
2505 /* Search all basic blocks for potentionally dead edges and purge them.
2507 Return true ifif some edge has been elliminated.
2511 purge_all_dead_edges ()
2513 int i, purged = false;
2514 for (i = 0; i < n_basic_blocks; i++)
2515 purged |= purge_dead_edges (BASIC_BLOCK (i));