* cfgloop.c (flow_loop_entry_edges_find): Fix typo.
[official-gcc.git] / gcc / cfgrtl.c
blobe7e6e699accbc277b4d02ba34ad8c06d2456f439
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
10 version.
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
15 for more details.
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
20 02111-1307, USA. */
22 /* This file contains low level functions to manipulate the CFG and analyze it
23 that are aware of the RTL intermediate language.
25 Available functionality:
26 - CFG-aware instruction chain manipulation
27 delete_insn, delete_insn_chain
28 - Basic block manipulation
29 create_basic_block, flow_delete_block, split_block,
30 merge_blocks_nomove
31 - Infrastructure to determine quickly basic block for insn
32 compute_bb_for_insn, update_bb_for_insn, set_block_for_insn,
33 - Edge redirection with updating and optimizing of insn chain
34 block_label, redirect_edge_and_branch,
35 redirect_edge_and_branch_force, tidy_fallthru_edge, force_nonfallthru
36 - Edge splitting and commiting to edges
37 split_edge, insert_insn_on_edge, commit_edge_insertions
38 - Dumping and debugging
39 print_rtl_with_bb, dump_bb, debug_bb, debug_bb_n
40 - Consistency checking
41 verify_flow_info
42 - CFG updating after constant propagation
43 purge_dead_edges, purge_all_dead_edges */
45 #include "config.h"
46 #include "system.h"
47 #include "tree.h"
48 #include "rtl.h"
49 #include "hard-reg-set.h"
50 #include "basic-block.h"
51 #include "regs.h"
52 #include "flags.h"
53 #include "output.h"
54 #include "function.h"
55 #include "except.h"
56 #include "toplev.h"
57 #include "tm_p.h"
58 #include "obstack.h"
60 /* Stubs in case we don't have a return insn. */
61 #ifndef HAVE_return
62 #define HAVE_return 0
63 #define gen_return() NULL_RTX
64 #endif
66 /* The basic block structure for every insn, indexed by uid. */
67 varray_type basic_block_for_insn;
69 /* The labels mentioned in non-jump rtl. Valid during find_basic_blocks. */
70 /* ??? Should probably be using LABEL_NUSES instead. It would take a
71 bit of surgery to be able to use or co-opt the routines in jump. */
72 rtx label_value_list;
73 rtx tail_recursion_label_list;
75 static int can_delete_note_p PARAMS ((rtx));
76 static int can_delete_label_p PARAMS ((rtx));
77 static void commit_one_edge_insertion PARAMS ((edge));
78 static bool try_redirect_by_replacing_jump PARAMS ((edge, basic_block));
79 static rtx last_loop_beg_note PARAMS ((rtx));
80 static bool back_edge_of_syntactic_loop_p PARAMS ((basic_block, basic_block));
81 static basic_block force_nonfallthru_and_redirect PARAMS ((edge, basic_block));
83 /* Return true if NOTE is not one of the ones that must be kept paired,
84 so that we may simply delete it. */
86 static int
87 can_delete_note_p (note)
88 rtx note;
90 return (NOTE_LINE_NUMBER (note) == NOTE_INSN_DELETED
91 || NOTE_LINE_NUMBER (note) == NOTE_INSN_BASIC_BLOCK);
94 /* True if a given label can be deleted. */
96 static int
97 can_delete_label_p (label)
98 rtx label;
100 return (!LABEL_PRESERVE_P (label)
101 /* User declared labels must be preserved. */
102 && LABEL_NAME (label) == 0
103 && !in_expr_list_p (forced_labels, label)
104 && !in_expr_list_p (label_value_list, label)
105 && !in_expr_list_p (exception_handler_labels, label));
108 /* Delete INSN by patching it out. Return the next insn. */
111 delete_insn (insn)
112 rtx insn;
114 rtx next = NEXT_INSN (insn);
115 rtx note;
116 bool really_delete = true;
118 if (GET_CODE (insn) == CODE_LABEL)
120 /* Some labels can't be directly removed from the INSN chain, as they
121 might be references via variables, constant pool etc.
122 Convert them to the special NOTE_INSN_DELETED_LABEL note. */
123 if (! can_delete_label_p (insn))
125 const char *name = LABEL_NAME (insn);
127 really_delete = false;
128 PUT_CODE (insn, NOTE);
129 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED_LABEL;
130 NOTE_SOURCE_FILE (insn) = name;
133 remove_node_from_expr_list (insn, &nonlocal_goto_handler_labels);
136 if (really_delete)
138 remove_insn (insn);
139 INSN_DELETED_P (insn) = 1;
142 /* If deleting a jump, decrement the use count of the label. Deleting
143 the label itself should happen in the normal course of block merging. */
144 if (GET_CODE (insn) == JUMP_INSN
145 && JUMP_LABEL (insn)
146 && GET_CODE (JUMP_LABEL (insn)) == CODE_LABEL)
147 LABEL_NUSES (JUMP_LABEL (insn))--;
149 /* Also if deleting an insn that references a label. */
150 else if ((note = find_reg_note (insn, REG_LABEL, NULL_RTX)) != NULL_RTX
151 && GET_CODE (XEXP (note, 0)) == CODE_LABEL)
152 LABEL_NUSES (XEXP (note, 0))--;
154 if (GET_CODE (insn) == JUMP_INSN
155 && (GET_CODE (PATTERN (insn)) == ADDR_VEC
156 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
158 rtx pat = PATTERN (insn);
159 int diff_vec_p = GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC;
160 int len = XVECLEN (pat, diff_vec_p);
161 int i;
163 for (i = 0; i < len; i++)
164 LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0))--;
167 return next;
170 /* Unlink a chain of insns between START and FINISH, leaving notes
171 that must be paired. */
173 void
174 delete_insn_chain (start, finish)
175 rtx start, finish;
177 rtx next;
179 /* Unchain the insns one by one. It would be quicker to delete all of these
180 with a single unchaining, rather than one at a time, but we need to keep
181 the NOTE's. */
182 while (1)
184 next = NEXT_INSN (start);
185 if (GET_CODE (start) == NOTE && !can_delete_note_p (start))
187 else
188 next = delete_insn (start);
190 if (start == finish)
191 break;
192 start = next;
196 /* Create a new basic block consisting of the instructions between HEAD and END
197 inclusive. This function is designed to allow fast BB construction - reuses
198 the note and basic block struct in BB_NOTE, if any and do not grow
199 BASIC_BLOCK chain and should be used directly only by CFG construction code.
200 END can be NULL in to create new empty basic block before HEAD. Both END
201 and HEAD can be NULL to create basic block at the end of INSN chain. */
203 basic_block
204 create_basic_block_structure (index, head, end, bb_note)
205 int index;
206 rtx head, end, bb_note;
208 basic_block bb;
210 if (bb_note
211 && ! RTX_INTEGRATED_P (bb_note)
212 && (bb = NOTE_BASIC_BLOCK (bb_note)) != NULL
213 && bb->aux == NULL)
215 /* If we found an existing note, thread it back onto the chain. */
217 rtx after;
219 if (GET_CODE (head) == CODE_LABEL)
220 after = head;
221 else
223 after = PREV_INSN (head);
224 head = bb_note;
227 if (after != bb_note && NEXT_INSN (after) != bb_note)
228 reorder_insns (bb_note, bb_note, after);
230 else
232 /* Otherwise we must create a note and a basic block structure. */
234 bb = alloc_block ();
236 if (!head && !end)
237 head = end = bb_note
238 = emit_note_after (NOTE_INSN_BASIC_BLOCK, get_last_insn ());
239 else if (GET_CODE (head) == CODE_LABEL && end)
241 bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, head);
242 if (head == end)
243 end = bb_note;
245 else
247 bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, head);
248 head = bb_note;
249 if (!end)
250 end = head;
253 NOTE_BASIC_BLOCK (bb_note) = bb;
256 /* Always include the bb note in the block. */
257 if (NEXT_INSN (end) == bb_note)
258 end = bb_note;
260 bb->head = head;
261 bb->end = end;
262 bb->index = index;
263 BASIC_BLOCK (index) = bb;
264 if (basic_block_for_insn)
265 update_bb_for_insn (bb);
267 /* Tag the block so that we know it has been used when considering
268 other basic block notes. */
269 bb->aux = bb;
271 return bb;
274 /* Create new basic block consisting of instructions in between HEAD and END
275 and place it to the BB chain at position INDEX. END can be NULL in to
276 create new empty basic block before HEAD. Both END and HEAD can be NULL to
277 create basic block at the end of INSN chain. */
279 basic_block
280 create_basic_block (index, head, end)
281 int index;
282 rtx head, end;
284 basic_block bb;
285 int i;
287 /* Place the new block just after the block being split. */
288 VARRAY_GROW (basic_block_info, ++n_basic_blocks);
290 /* Some parts of the compiler expect blocks to be number in
291 sequential order so insert the new block immediately after the
292 block being split.. */
293 for (i = n_basic_blocks - 1; i > index; --i)
295 basic_block tmp = BASIC_BLOCK (i - 1);
297 BASIC_BLOCK (i) = tmp;
298 tmp->index = i;
301 bb = create_basic_block_structure (index, head, end, NULL);
302 bb->aux = NULL;
303 return bb;
306 /* Delete the insns in a (non-live) block. We physically delete every
307 non-deleted-note insn, and update the flow graph appropriately.
309 Return nonzero if we deleted an exception handler. */
311 /* ??? Preserving all such notes strikes me as wrong. It would be nice
312 to post-process the stream to remove empty blocks, loops, ranges, etc. */
315 flow_delete_block (b)
316 basic_block b;
318 int deleted_handler = 0;
319 rtx insn, end, tmp;
321 /* If the head of this block is a CODE_LABEL, then it might be the
322 label for an exception handler which can't be reached.
324 We need to remove the label from the exception_handler_label list
325 and remove the associated NOTE_INSN_EH_REGION_BEG and
326 NOTE_INSN_EH_REGION_END notes. */
328 insn = b->head;
330 never_reached_warning (insn);
332 if (GET_CODE (insn) == CODE_LABEL)
333 maybe_remove_eh_handler (insn);
335 /* Include any jump table following the basic block. */
336 end = b->end;
337 if (GET_CODE (end) == JUMP_INSN
338 && (tmp = JUMP_LABEL (end)) != NULL_RTX
339 && (tmp = NEXT_INSN (tmp)) != NULL_RTX
340 && GET_CODE (tmp) == JUMP_INSN
341 && (GET_CODE (PATTERN (tmp)) == ADDR_VEC
342 || GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC))
343 end = tmp;
345 /* Include any barrier that may follow the basic block. */
346 tmp = next_nonnote_insn (end);
347 if (tmp && GET_CODE (tmp) == BARRIER)
348 end = tmp;
350 /* Selectively delete the entire chain. */
351 b->head = NULL;
352 delete_insn_chain (insn, end);
354 /* Remove the edges into and out of this block. Note that there may
355 indeed be edges in, if we are removing an unreachable loop. */
356 while (b->pred != NULL)
357 remove_edge (b->pred);
358 while (b->succ != NULL)
359 remove_edge (b->succ);
361 b->pred = NULL;
362 b->succ = NULL;
364 /* Remove the basic block from the array, and compact behind it. */
365 expunge_block (b);
367 return deleted_handler;
370 /* Records the basic block struct in BB_FOR_INSN, for every instruction
371 indexed by INSN_UID. MAX is the size of the array. */
373 void
374 compute_bb_for_insn (max)
375 int max;
377 int i;
379 if (basic_block_for_insn)
380 VARRAY_FREE (basic_block_for_insn);
382 VARRAY_BB_INIT (basic_block_for_insn, max, "basic_block_for_insn");
384 for (i = 0; i < n_basic_blocks; ++i)
386 basic_block bb = BASIC_BLOCK (i);
387 rtx end = bb->end;
388 rtx insn;
390 for (insn = bb->head; ; insn = NEXT_INSN (insn))
392 if (INSN_UID (insn) < max)
393 VARRAY_BB (basic_block_for_insn, INSN_UID (insn)) = bb;
395 if (insn == end)
396 break;
401 /* Release the basic_block_for_insn array. */
403 void
404 free_bb_for_insn ()
406 if (basic_block_for_insn)
407 VARRAY_FREE (basic_block_for_insn);
409 basic_block_for_insn = 0;
412 /* Update insns block within BB. */
414 void
415 update_bb_for_insn (bb)
416 basic_block bb;
418 rtx insn;
420 if (! basic_block_for_insn)
421 return;
423 for (insn = bb->head; ; insn = NEXT_INSN (insn))
425 set_block_for_insn (insn, bb);
426 if (insn == bb->end)
427 break;
431 /* Record INSN's block as BB. */
433 void
434 set_block_for_insn (insn, bb)
435 rtx insn;
436 basic_block bb;
438 size_t uid = INSN_UID (insn);
440 if (uid >= basic_block_for_insn->num_elements)
442 /* Add one-eighth the size so we don't keep calling xrealloc. */
443 size_t new_size = uid + (uid + 7) / 8;
445 VARRAY_GROW (basic_block_for_insn, new_size);
448 VARRAY_BB (basic_block_for_insn, uid) = bb;
451 /* Split a block BB after insn INSN creating a new fallthru edge.
452 Return the new edge. Note that to keep other parts of the compiler happy,
453 this function renumbers all the basic blocks so that the new
454 one has a number one greater than the block split. */
456 edge
457 split_block (bb, insn)
458 basic_block bb;
459 rtx insn;
461 basic_block new_bb;
462 edge new_edge;
463 edge e;
465 /* There is no point splitting the block after its end. */
466 if (bb->end == insn)
467 return 0;
469 /* Create the new basic block. */
470 new_bb = create_basic_block (bb->index + 1, NEXT_INSN (insn), bb->end);
471 new_bb->count = bb->count;
472 new_bb->frequency = bb->frequency;
473 new_bb->loop_depth = bb->loop_depth;
474 bb->end = insn;
476 /* Redirect the outgoing edges. */
477 new_bb->succ = bb->succ;
478 bb->succ = NULL;
479 for (e = new_bb->succ; e; e = e->succ_next)
480 e->src = new_bb;
482 new_edge = make_single_succ_edge (bb, new_bb, EDGE_FALLTHRU);
484 if (bb->global_live_at_start)
486 new_bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (&flow_obstack);
487 new_bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (&flow_obstack);
488 COPY_REG_SET (new_bb->global_live_at_end, bb->global_live_at_end);
490 /* We now have to calculate which registers are live at the end
491 of the split basic block and at the start of the new basic
492 block. Start with those registers that are known to be live
493 at the end of the original basic block and get
494 propagate_block to determine which registers are live. */
495 COPY_REG_SET (new_bb->global_live_at_start, bb->global_live_at_end);
496 propagate_block (new_bb, new_bb->global_live_at_start, NULL, NULL, 0);
497 COPY_REG_SET (bb->global_live_at_end,
498 new_bb->global_live_at_start);
501 return new_edge;
504 /* Blocks A and B are to be merged into a single block A. The insns
505 are already contiguous, hence `nomove'. */
507 void
508 merge_blocks_nomove (a, b)
509 basic_block a, b;
511 rtx b_head = b->head, b_end = b->end, a_end = a->end;
512 rtx del_first = NULL_RTX, del_last = NULL_RTX;
513 int b_empty = 0;
514 edge e;
516 /* If there was a CODE_LABEL beginning B, delete it. */
517 if (GET_CODE (b_head) == CODE_LABEL)
519 /* Detect basic blocks with nothing but a label. This can happen
520 in particular at the end of a function. */
521 if (b_head == b_end)
522 b_empty = 1;
524 del_first = del_last = b_head;
525 b_head = NEXT_INSN (b_head);
528 /* Delete the basic block note and handle blocks containing just that
529 note. */
530 if (NOTE_INSN_BASIC_BLOCK_P (b_head))
532 if (b_head == b_end)
533 b_empty = 1;
534 if (! del_last)
535 del_first = b_head;
537 del_last = b_head;
538 b_head = NEXT_INSN (b_head);
541 /* If there was a jump out of A, delete it. */
542 if (GET_CODE (a_end) == JUMP_INSN)
544 rtx prev;
546 for (prev = PREV_INSN (a_end); ; prev = PREV_INSN (prev))
547 if (GET_CODE (prev) != NOTE
548 || NOTE_LINE_NUMBER (prev) == NOTE_INSN_BASIC_BLOCK
549 || prev == a->head)
550 break;
552 del_first = a_end;
554 #ifdef HAVE_cc0
555 /* If this was a conditional jump, we need to also delete
556 the insn that set cc0. */
557 if (only_sets_cc0_p (prev))
559 rtx tmp = prev;
561 prev = prev_nonnote_insn (prev);
562 if (!prev)
563 prev = a->head;
564 del_first = tmp;
566 #endif
568 a_end = PREV_INSN (del_first);
570 else if (GET_CODE (NEXT_INSN (a_end)) == BARRIER)
571 del_first = NEXT_INSN (a_end);
573 /* Normally there should only be one successor of A and that is B, but
574 partway though the merge of blocks for conditional_execution we'll
575 be merging a TEST block with THEN and ELSE successors. Free the
576 whole lot of them and hope the caller knows what they're doing. */
577 while (a->succ)
578 remove_edge (a->succ);
580 /* Adjust the edges out of B for the new owner. */
581 for (e = b->succ; e; e = e->succ_next)
582 e->src = a;
583 a->succ = b->succ;
585 /* B hasn't quite yet ceased to exist. Attempt to prevent mishap. */
586 b->pred = b->succ = NULL;
587 a->global_live_at_end = b->global_live_at_end;
589 expunge_block (b);
591 /* Delete everything marked above as well as crap that might be
592 hanging out between the two blocks. */
593 delete_insn_chain (del_first, del_last);
595 /* Reassociate the insns of B with A. */
596 if (!b_empty)
598 if (basic_block_for_insn)
600 rtx x;
602 for (x = a_end; x != b_end; x = NEXT_INSN (x))
603 BLOCK_FOR_INSN (x) = a;
605 BLOCK_FOR_INSN (b_end) = a;
608 a_end = b_end;
611 a->end = a_end;
614 /* Return the label in the head of basic block BLOCK. Create one if it doesn't
615 exist. */
618 block_label (block)
619 basic_block block;
621 if (block == EXIT_BLOCK_PTR)
622 return NULL_RTX;
624 if (GET_CODE (block->head) != CODE_LABEL)
626 block->head = emit_label_before (gen_label_rtx (), block->head);
627 if (basic_block_for_insn)
628 set_block_for_insn (block->head, block);
631 return block->head;
634 /* Attempt to perform edge redirection by replacing possibly complex jump
635 instruction by unconditional jump or removing jump completely. This can
636 apply only if all edges now point to the same block. The parameters and
637 return values are equivalent to redirect_edge_and_branch. */
639 static bool
640 try_redirect_by_replacing_jump (e, target)
641 edge e;
642 basic_block target;
644 basic_block src = e->src;
645 rtx insn = src->end, kill_from;
646 edge tmp;
647 rtx set;
648 int fallthru = 0;
650 /* Verify that all targets will be TARGET. */
651 for (tmp = src->succ; tmp; tmp = tmp->succ_next)
652 if (tmp->dest != target && tmp != e)
653 break;
655 if (tmp || !onlyjump_p (insn))
656 return false;
658 /* Avoid removing branch with side effects. */
659 set = single_set (insn);
660 if (!set || side_effects_p (set))
661 return false;
663 /* In case we zap a conditional jump, we'll need to kill
664 the cc0 setter too. */
665 kill_from = insn;
666 #ifdef HAVE_cc0
667 if (reg_mentioned_p (cc0_rtx, PATTERN (insn)))
668 kill_from = PREV_INSN (insn);
669 #endif
671 /* See if we can create the fallthru edge. */
672 if (can_fallthru (src, target))
674 if (rtl_dump_file)
675 fprintf (rtl_dump_file, "Removing jump %i.\n", INSN_UID (insn));
676 fallthru = 1;
678 /* Selectively unlink whole insn chain. */
679 delete_insn_chain (kill_from, PREV_INSN (target->head));
682 /* If this already is simplejump, redirect it. */
683 else if (simplejump_p (insn))
685 if (e->dest == target)
686 return false;
687 if (rtl_dump_file)
688 fprintf (rtl_dump_file, "Redirecting jump %i from %i to %i.\n",
689 INSN_UID (insn), e->dest->index, target->index);
690 if (!redirect_jump (insn, block_label (target), 0))
692 if (target == EXIT_BLOCK_PTR)
693 return false;
694 abort ();
698 /* Cannot do anything for target exit block. */
699 else if (target == EXIT_BLOCK_PTR)
700 return false;
702 /* Or replace possibly complicated jump insn by simple jump insn. */
703 else
705 rtx target_label = block_label (target);
706 rtx barrier;
708 emit_jump_insn_after (gen_jump (target_label), insn);
709 JUMP_LABEL (src->end) = target_label;
710 LABEL_NUSES (target_label)++;
711 if (rtl_dump_file)
712 fprintf (rtl_dump_file, "Replacing insn %i by jump %i\n",
713 INSN_UID (insn), INSN_UID (src->end));
715 delete_insn_chain (kill_from, insn);
717 barrier = next_nonnote_insn (src->end);
718 if (!barrier || GET_CODE (barrier) != BARRIER)
719 emit_barrier_after (src->end);
722 /* Keep only one edge out and set proper flags. */
723 while (src->succ->succ_next)
724 remove_edge (src->succ);
725 e = src->succ;
726 if (fallthru)
727 e->flags = EDGE_FALLTHRU;
728 else
729 e->flags = 0;
731 e->probability = REG_BR_PROB_BASE;
732 e->count = src->count;
734 /* We don't want a block to end on a line-number note since that has
735 the potential of changing the code between -g and not -g. */
736 while (GET_CODE (e->src->end) == NOTE
737 && NOTE_LINE_NUMBER (e->src->end) >= 0)
738 delete_insn (e->src->end);
740 if (e->dest != target)
741 redirect_edge_succ (e, target);
743 return true;
746 /* Return last loop_beg note appearing after INSN, before start of next
747 basic block. Return INSN if there are no such notes.
749 When emitting jump to redirect an fallthru edge, it should always appear
750 after the LOOP_BEG notes, as loop optimizer expect loop to either start by
751 fallthru edge or jump following the LOOP_BEG note jumping to the loop exit
752 test. */
754 static rtx
755 last_loop_beg_note (insn)
756 rtx insn;
758 rtx last = insn;
760 for (insn = NEXT_INSN (insn); insn && GET_CODE (insn) == NOTE
761 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_BASIC_BLOCK;
762 insn = NEXT_INSN (insn))
763 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
764 last = insn;
766 return last;
769 /* Attempt to change code to redirect edge E to TARGET. Don't do that on
770 expense of adding new instructions or reordering basic blocks.
772 Function can be also called with edge destination equivalent to the TARGET.
773 Then it should try the simplifications and do nothing if none is possible.
775 Return true if transformation succeeded. We still return false in case E
776 already destinated TARGET and we didn't managed to simplify instruction
777 stream. */
779 bool
780 redirect_edge_and_branch (e, target)
781 edge e;
782 basic_block target;
784 rtx tmp;
785 rtx old_label = e->dest->head;
786 basic_block src = e->src;
787 rtx insn = src->end;
789 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
790 return false;
792 if (try_redirect_by_replacing_jump (e, target))
793 return true;
795 /* Do this fast path late, as we want above code to simplify for cases
796 where called on single edge leaving basic block containing nontrivial
797 jump insn. */
798 else if (e->dest == target)
799 return false;
801 /* We can only redirect non-fallthru edges of jump insn. */
802 if (e->flags & EDGE_FALLTHRU)
803 return false;
804 else if (GET_CODE (insn) != JUMP_INSN)
805 return false;
807 /* Recognize a tablejump and adjust all matching cases. */
808 if ((tmp = JUMP_LABEL (insn)) != NULL_RTX
809 && (tmp = NEXT_INSN (tmp)) != NULL_RTX
810 && GET_CODE (tmp) == JUMP_INSN
811 && (GET_CODE (PATTERN (tmp)) == ADDR_VEC
812 || GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC))
814 rtvec vec;
815 int j;
816 rtx new_label = block_label (target);
818 if (target == EXIT_BLOCK_PTR)
819 return false;
820 if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
821 vec = XVEC (PATTERN (tmp), 0);
822 else
823 vec = XVEC (PATTERN (tmp), 1);
825 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
826 if (XEXP (RTVEC_ELT (vec, j), 0) == old_label)
828 RTVEC_ELT (vec, j) = gen_rtx_LABEL_REF (Pmode, new_label);
829 --LABEL_NUSES (old_label);
830 ++LABEL_NUSES (new_label);
833 /* Handle casesi dispatch insns */
834 if ((tmp = single_set (insn)) != NULL
835 && SET_DEST (tmp) == pc_rtx
836 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
837 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF
838 && XEXP (XEXP (SET_SRC (tmp), 2), 0) == old_label)
840 XEXP (SET_SRC (tmp), 2) = gen_rtx_LABEL_REF (VOIDmode,
841 new_label);
842 --LABEL_NUSES (old_label);
843 ++LABEL_NUSES (new_label);
846 else
848 /* ?? We may play the games with moving the named labels from
849 one basic block to the other in case only one computed_jump is
850 available. */
851 if (computed_jump_p (insn)
852 /* A return instruction can't be redirected. */
853 || returnjump_p (insn))
854 return false;
856 /* If the insn doesn't go where we think, we're confused. */
857 if (JUMP_LABEL (insn) != old_label)
858 abort ();
860 /* If the substitution doesn't succeed, die. This can happen
861 if the back end emitted unrecognizable instructions or if
862 target is exit block on some arches. */
863 if (!redirect_jump (insn, block_label (target), 0))
865 if (target == EXIT_BLOCK_PTR)
866 return false;
867 abort ();
871 if (rtl_dump_file)
872 fprintf (rtl_dump_file, "Edge %i->%i redirected to %i\n",
873 e->src->index, e->dest->index, target->index);
875 if (e->dest != target)
876 redirect_edge_succ_nodup (e, target);
878 return true;
881 /* Like force_nonfallthru below, but additionally performs redirection
882 Used by redirect_edge_and_branch_force. */
884 static basic_block
885 force_nonfallthru_and_redirect (e, target)
886 edge e;
887 basic_block target;
889 basic_block jump_block, new_bb = NULL;
890 rtx note;
891 edge new_edge;
893 if (e->flags & EDGE_ABNORMAL)
894 abort ();
895 else if (!(e->flags & EDGE_FALLTHRU))
896 abort ();
897 else if (e->src->succ->succ_next)
899 /* Create the new structures. */
900 note = last_loop_beg_note (e->src->end);
901 jump_block
902 = create_basic_block (e->src->index + 1, NEXT_INSN (note), NULL);
903 jump_block->count = e->count;
904 jump_block->frequency = EDGE_FREQUENCY (e);
905 jump_block->loop_depth = target->loop_depth;
907 if (target->global_live_at_start)
909 jump_block->global_live_at_start
910 = OBSTACK_ALLOC_REG_SET (&flow_obstack);
911 jump_block->global_live_at_end
912 = OBSTACK_ALLOC_REG_SET (&flow_obstack);
913 COPY_REG_SET (jump_block->global_live_at_start,
914 target->global_live_at_start);
915 COPY_REG_SET (jump_block->global_live_at_end,
916 target->global_live_at_start);
919 /* Wire edge in. */
920 new_edge = make_edge (e->src, jump_block, EDGE_FALLTHRU);
921 new_edge->probability = e->probability;
922 new_edge->count = e->count;
924 /* Redirect old edge. */
925 redirect_edge_pred (e, jump_block);
926 e->probability = REG_BR_PROB_BASE;
928 new_bb = jump_block;
930 else
931 jump_block = e->src;
933 e->flags &= ~EDGE_FALLTHRU;
934 if (target == EXIT_BLOCK_PTR)
936 if (HAVE_return)
937 emit_jump_insn_after (gen_return (), jump_block->end);
938 else
939 abort ();
941 else
943 rtx label = block_label (target);
944 emit_jump_insn_after (gen_jump (label), jump_block->end);
945 JUMP_LABEL (jump_block->end) = label;
946 LABEL_NUSES (label)++;
949 emit_barrier_after (jump_block->end);
950 redirect_edge_succ_nodup (e, target);
952 return new_bb;
955 /* Edge E is assumed to be fallthru edge. Emit needed jump instruction
956 (and possibly create new basic block) to make edge non-fallthru.
957 Return newly created BB or NULL if none. */
959 basic_block
960 force_nonfallthru (e)
961 edge e;
963 return force_nonfallthru_and_redirect (e, e->dest);
966 /* Redirect edge even at the expense of creating new jump insn or
967 basic block. Return new basic block if created, NULL otherwise.
968 Abort if conversion is impossible. */
970 basic_block
971 redirect_edge_and_branch_force (e, target)
972 edge e;
973 basic_block target;
975 if (redirect_edge_and_branch (e, target)
976 || e->dest == target)
977 return NULL;
979 /* In case the edge redirection failed, try to force it to be non-fallthru
980 and redirect newly created simplejump. */
981 return force_nonfallthru_and_redirect (e, target);
984 /* The given edge should potentially be a fallthru edge. If that is in
985 fact true, delete the jump and barriers that are in the way. */
987 void
988 tidy_fallthru_edge (e, b, c)
989 edge e;
990 basic_block b, c;
992 rtx q;
994 /* ??? In a late-running flow pass, other folks may have deleted basic
995 blocks by nopping out blocks, leaving multiple BARRIERs between here
996 and the target label. They ought to be chastized and fixed.
998 We can also wind up with a sequence of undeletable labels between
999 one block and the next.
1001 So search through a sequence of barriers, labels, and notes for
1002 the head of block C and assert that we really do fall through. */
1004 if (next_real_insn (b->end) != next_real_insn (PREV_INSN (c->head)))
1005 return;
1007 /* Remove what will soon cease being the jump insn from the source block.
1008 If block B consisted only of this single jump, turn it into a deleted
1009 note. */
1010 q = b->end;
1011 if (GET_CODE (q) == JUMP_INSN
1012 && onlyjump_p (q)
1013 && (any_uncondjump_p (q)
1014 || (b->succ == e && e->succ_next == NULL)))
1016 #ifdef HAVE_cc0
1017 /* If this was a conditional jump, we need to also delete
1018 the insn that set cc0. */
1019 if (any_condjump_p (q) && only_sets_cc0_p (PREV_INSN (q)))
1020 q = PREV_INSN (q);
1021 #endif
1023 q = PREV_INSN (q);
1025 /* We don't want a block to end on a line-number note since that has
1026 the potential of changing the code between -g and not -g. */
1027 while (GET_CODE (q) == NOTE && NOTE_LINE_NUMBER (q) >= 0)
1028 q = PREV_INSN (q);
1031 /* Selectively unlink the sequence. */
1032 if (q != PREV_INSN (c->head))
1033 delete_insn_chain (NEXT_INSN (q), PREV_INSN (c->head));
1035 e->flags |= EDGE_FALLTHRU;
1038 /* Fix up edges that now fall through, or rather should now fall through
1039 but previously required a jump around now deleted blocks. Simplify
1040 the search by only examining blocks numerically adjacent, since this
1041 is how find_basic_blocks created them. */
1043 void
1044 tidy_fallthru_edges ()
1046 int i;
1048 for (i = 1; i < n_basic_blocks; i++)
1050 basic_block b = BASIC_BLOCK (i - 1);
1051 basic_block c = BASIC_BLOCK (i);
1052 edge s;
1054 /* We care about simple conditional or unconditional jumps with
1055 a single successor.
1057 If we had a conditional branch to the next instruction when
1058 find_basic_blocks was called, then there will only be one
1059 out edge for the block which ended with the conditional
1060 branch (since we do not create duplicate edges).
1062 Furthermore, the edge will be marked as a fallthru because we
1063 merge the flags for the duplicate edges. So we do not want to
1064 check that the edge is not a FALLTHRU edge. */
1066 if ((s = b->succ) != NULL
1067 && ! (s->flags & EDGE_COMPLEX)
1068 && s->succ_next == NULL
1069 && s->dest == c
1070 /* If the jump insn has side effects, we can't tidy the edge. */
1071 && (GET_CODE (b->end) != JUMP_INSN
1072 || onlyjump_p (b->end)))
1073 tidy_fallthru_edge (s, b, c);
1077 /* Helper function for split_edge. Return true in case edge BB2 to BB1
1078 is back edge of syntactic loop. */
1080 static bool
1081 back_edge_of_syntactic_loop_p (bb1, bb2)
1082 basic_block bb1, bb2;
1084 rtx insn;
1085 int count = 0;
1087 if (bb1->index > bb2->index)
1088 return false;
1089 else if (bb1->index == bb2->index)
1090 return true;
1092 for (insn = bb1->end; insn != bb2->head && count >= 0;
1093 insn = NEXT_INSN (insn))
1094 if (GET_CODE (insn) == NOTE)
1096 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
1097 count++;
1098 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
1099 count--;
1102 return count >= 0;
1105 /* Split a (typically critical) edge. Return the new block.
1106 Abort on abnormal edges.
1108 ??? The code generally expects to be called on critical edges.
1109 The case of a block ending in an unconditional jump to a
1110 block with multiple predecessors is not handled optimally. */
1112 basic_block
1113 split_edge (edge_in)
1114 edge edge_in;
1116 basic_block bb;
1117 edge edge_out;
1118 rtx before;
1120 /* Abnormal edges cannot be split. */
1121 if ((edge_in->flags & EDGE_ABNORMAL) != 0)
1122 abort ();
1124 /* We are going to place the new block in front of edge destination.
1125 Avoid existence of fallthru predecessors. */
1126 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1128 edge e;
1130 for (e = edge_in->dest->pred; e; e = e->pred_next)
1131 if (e->flags & EDGE_FALLTHRU)
1132 break;
1134 if (e)
1135 force_nonfallthru (e);
1138 /* Create the basic block note.
1140 Where we place the note can have a noticeable impact on the generated
1141 code. Consider this cfg:
1147 +->1-->2--->E
1149 +--+
1151 If we need to insert an insn on the edge from block 0 to block 1,
1152 we want to ensure the instructions we insert are outside of any
1153 loop notes that physically sit between block 0 and block 1. Otherwise
1154 we confuse the loop optimizer into thinking the loop is a phony. */
1156 if (edge_in->dest != EXIT_BLOCK_PTR
1157 && PREV_INSN (edge_in->dest->head)
1158 && GET_CODE (PREV_INSN (edge_in->dest->head)) == NOTE
1159 && (NOTE_LINE_NUMBER (PREV_INSN (edge_in->dest->head))
1160 == NOTE_INSN_LOOP_BEG)
1161 && !back_edge_of_syntactic_loop_p (edge_in->dest, edge_in->src))
1162 before = PREV_INSN (edge_in->dest->head);
1163 else if (edge_in->dest != EXIT_BLOCK_PTR)
1164 before = edge_in->dest->head;
1165 else
1166 before = NULL_RTX;
1168 bb = create_basic_block (edge_in->dest == EXIT_BLOCK_PTR ? n_basic_blocks
1169 : edge_in->dest->index, before, NULL);
1170 bb->count = edge_in->count;
1171 bb->frequency = EDGE_FREQUENCY (edge_in);
1173 /* ??? This info is likely going to be out of date very soon. */
1174 if (edge_in->dest->global_live_at_start)
1176 bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1177 bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1178 COPY_REG_SET (bb->global_live_at_start,
1179 edge_in->dest->global_live_at_start);
1180 COPY_REG_SET (bb->global_live_at_end,
1181 edge_in->dest->global_live_at_start);
1184 edge_out = make_single_succ_edge (bb, edge_in->dest, EDGE_FALLTHRU);
1186 /* For non-fallthry edges, we must adjust the predecessor's
1187 jump instruction to target our new block. */
1188 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1190 if (!redirect_edge_and_branch (edge_in, bb))
1191 abort ();
1193 else
1194 redirect_edge_succ (edge_in, bb);
1196 return bb;
1199 /* Queue instructions for insertion on an edge between two basic blocks.
1200 The new instructions and basic blocks (if any) will not appear in the
1201 CFG until commit_edge_insertions is called. */
1203 void
1204 insert_insn_on_edge (pattern, e)
1205 rtx pattern;
1206 edge e;
1208 /* We cannot insert instructions on an abnormal critical edge.
1209 It will be easier to find the culprit if we die now. */
1210 if ((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e))
1211 abort ();
1213 if (e->insns == NULL_RTX)
1214 start_sequence ();
1215 else
1216 push_to_sequence (e->insns);
1218 emit_insn (pattern);
1220 e->insns = get_insns ();
1221 end_sequence ();
1224 /* Update the CFG for the instructions queued on edge E. */
1226 static void
1227 commit_one_edge_insertion (e)
1228 edge e;
1230 rtx before = NULL_RTX, after = NULL_RTX, insns, tmp, last;
1231 basic_block bb;
1233 /* Pull the insns off the edge now since the edge might go away. */
1234 insns = e->insns;
1235 e->insns = NULL_RTX;
1237 /* Figure out where to put these things. If the destination has
1238 one predecessor, insert there. Except for the exit block. */
1239 if (e->dest->pred->pred_next == NULL
1240 && e->dest != EXIT_BLOCK_PTR)
1242 bb = e->dest;
1244 /* Get the location correct wrt a code label, and "nice" wrt
1245 a basic block note, and before everything else. */
1246 tmp = bb->head;
1247 if (GET_CODE (tmp) == CODE_LABEL)
1248 tmp = NEXT_INSN (tmp);
1249 if (NOTE_INSN_BASIC_BLOCK_P (tmp))
1250 tmp = NEXT_INSN (tmp);
1251 if (tmp == bb->head)
1252 before = tmp;
1253 else
1254 after = PREV_INSN (tmp);
1257 /* If the source has one successor and the edge is not abnormal,
1258 insert there. Except for the entry block. */
1259 else if ((e->flags & EDGE_ABNORMAL) == 0
1260 && e->src->succ->succ_next == NULL
1261 && e->src != ENTRY_BLOCK_PTR)
1263 bb = e->src;
1265 /* It is possible to have a non-simple jump here. Consider a target
1266 where some forms of unconditional jumps clobber a register. This
1267 happens on the fr30 for example.
1269 We know this block has a single successor, so we can just emit
1270 the queued insns before the jump. */
1271 if (GET_CODE (bb->end) == JUMP_INSN)
1272 for (before = bb->end;
1273 GET_CODE (PREV_INSN (before)) == NOTE
1274 && NOTE_LINE_NUMBER (PREV_INSN (before)) == NOTE_INSN_LOOP_BEG;
1275 before = PREV_INSN (before))
1277 else
1279 /* We'd better be fallthru, or we've lost track of what's what. */
1280 if ((e->flags & EDGE_FALLTHRU) == 0)
1281 abort ();
1283 after = bb->end;
1287 /* Otherwise we must split the edge. */
1288 else
1290 bb = split_edge (e);
1291 after = bb->end;
1294 /* Now that we've found the spot, do the insertion. */
1296 if (before)
1298 emit_insns_before (insns, before);
1299 last = prev_nonnote_insn (before);
1301 else
1302 last = emit_insns_after (insns, after);
1304 if (returnjump_p (last))
1306 /* ??? Remove all outgoing edges from BB and add one for EXIT.
1307 This is not currently a problem because this only happens
1308 for the (single) epilogue, which already has a fallthru edge
1309 to EXIT. */
1311 e = bb->succ;
1312 if (e->dest != EXIT_BLOCK_PTR
1313 || e->succ_next != NULL
1314 || (e->flags & EDGE_FALLTHRU) == 0)
1315 abort ();
1317 e->flags &= ~EDGE_FALLTHRU;
1318 emit_barrier_after (last);
1320 if (before)
1321 delete_insn (before);
1323 else if (GET_CODE (last) == JUMP_INSN)
1324 abort ();
1326 find_sub_basic_blocks (bb);
1329 /* Update the CFG for all queued instructions. */
1331 void
1332 commit_edge_insertions ()
1334 int i;
1335 basic_block bb;
1337 #ifdef ENABLE_CHECKING
1338 verify_flow_info ();
1339 #endif
1341 i = -1;
1342 bb = ENTRY_BLOCK_PTR;
1343 while (1)
1345 edge e, next;
1347 for (e = bb->succ; e; e = next)
1349 next = e->succ_next;
1350 if (e->insns)
1351 commit_one_edge_insertion (e);
1354 if (++i >= n_basic_blocks)
1355 break;
1356 bb = BASIC_BLOCK (i);
1360 /* Print out one basic block with live information at start and end. */
1362 void
1363 dump_bb (bb, outf)
1364 basic_block bb;
1365 FILE *outf;
1367 rtx insn;
1368 rtx last;
1369 edge e;
1371 fprintf (outf, ";; Basic block %d, loop depth %d, count ",
1372 bb->index, bb->loop_depth);
1373 fprintf (outf, HOST_WIDEST_INT_PRINT_DEC, (HOST_WIDEST_INT) bb->count);
1374 putc ('\n', outf);
1376 fputs (";; Predecessors: ", outf);
1377 for (e = bb->pred; e; e = e->pred_next)
1378 dump_edge_info (outf, e, 0);
1379 putc ('\n', outf);
1381 fputs (";; Registers live at start:", outf);
1382 dump_regset (bb->global_live_at_start, outf);
1383 putc ('\n', outf);
1385 for (insn = bb->head, last = NEXT_INSN (bb->end); insn != last;
1386 insn = NEXT_INSN (insn))
1387 print_rtl_single (outf, insn);
1389 fputs (";; Registers live at end:", outf);
1390 dump_regset (bb->global_live_at_end, outf);
1391 putc ('\n', outf);
1393 fputs (";; Successors: ", outf);
1394 for (e = bb->succ; e; e = e->succ_next)
1395 dump_edge_info (outf, e, 1);
1396 putc ('\n', outf);
1399 void
1400 debug_bb (bb)
1401 basic_block bb;
1403 dump_bb (bb, stderr);
1406 void
1407 debug_bb_n (n)
1408 int n;
1410 dump_bb (BASIC_BLOCK (n), stderr);
1413 /* Like print_rtl, but also print out live information for the start of each
1414 basic block. */
1416 void
1417 print_rtl_with_bb (outf, rtx_first)
1418 FILE *outf;
1419 rtx rtx_first;
1421 rtx tmp_rtx;
1423 if (rtx_first == 0)
1424 fprintf (outf, "(nil)\n");
1425 else
1427 int i;
1428 enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
1429 int max_uid = get_max_uid ();
1430 basic_block *start
1431 = (basic_block *) xcalloc (max_uid, sizeof (basic_block));
1432 basic_block *end
1433 = (basic_block *) xcalloc (max_uid, sizeof (basic_block));
1434 enum bb_state *in_bb_p
1435 = (enum bb_state *) xcalloc (max_uid, sizeof (enum bb_state));
1437 for (i = n_basic_blocks - 1; i >= 0; i--)
1439 basic_block bb = BASIC_BLOCK (i);
1440 rtx x;
1442 start[INSN_UID (bb->head)] = bb;
1443 end[INSN_UID (bb->end)] = bb;
1444 for (x = bb->head; x != NULL_RTX; x = NEXT_INSN (x))
1446 enum bb_state state = IN_MULTIPLE_BB;
1448 if (in_bb_p[INSN_UID (x)] == NOT_IN_BB)
1449 state = IN_ONE_BB;
1450 in_bb_p[INSN_UID (x)] = state;
1452 if (x == bb->end)
1453 break;
1457 for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
1459 int did_output;
1460 basic_block bb;
1462 if ((bb = start[INSN_UID (tmp_rtx)]) != NULL)
1464 fprintf (outf, ";; Start of basic block %d, registers live:",
1465 bb->index);
1466 dump_regset (bb->global_live_at_start, outf);
1467 putc ('\n', outf);
1470 if (in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB
1471 && GET_CODE (tmp_rtx) != NOTE
1472 && GET_CODE (tmp_rtx) != BARRIER)
1473 fprintf (outf, ";; Insn is not within a basic block\n");
1474 else if (in_bb_p[INSN_UID (tmp_rtx)] == IN_MULTIPLE_BB)
1475 fprintf (outf, ";; Insn is in multiple basic blocks\n");
1477 did_output = print_rtl_single (outf, tmp_rtx);
1479 if ((bb = end[INSN_UID (tmp_rtx)]) != NULL)
1481 fprintf (outf, ";; End of basic block %d, registers live:\n",
1482 bb->index);
1483 dump_regset (bb->global_live_at_end, outf);
1484 putc ('\n', outf);
1487 if (did_output)
1488 putc ('\n', outf);
1491 free (start);
1492 free (end);
1493 free (in_bb_p);
1496 if (current_function_epilogue_delay_list != 0)
1498 fprintf (outf, "\n;; Insns in epilogue delay list:\n\n");
1499 for (tmp_rtx = current_function_epilogue_delay_list; tmp_rtx != 0;
1500 tmp_rtx = XEXP (tmp_rtx, 1))
1501 print_rtl_single (outf, XEXP (tmp_rtx, 0));
1505 /* Verify the CFG consistency. This function check some CFG invariants and
1506 aborts when something is wrong. Hope that this function will help to
1507 convert many optimization passes to preserve CFG consistent.
1509 Currently it does following checks:
1511 - test head/end pointers
1512 - overlapping of basic blocks
1513 - edge list correctness
1514 - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note)
1515 - tails of basic blocks (ensure that boundary is necessary)
1516 - scans body of the basic block for JUMP_INSN, CODE_LABEL
1517 and NOTE_INSN_BASIC_BLOCK
1518 - check that all insns are in the basic blocks
1519 (except the switch handling code, barriers and notes)
1520 - check that all returns are followed by barriers
1522 In future it can be extended check a lot of other stuff as well
1523 (reachability of basic blocks, life information, etc. etc.). */
1525 void
1526 verify_flow_info ()
1528 const int max_uid = get_max_uid ();
1529 const rtx rtx_first = get_insns ();
1530 rtx last_head = get_last_insn ();
1531 basic_block *bb_info, *last_visited;
1532 size_t *edge_checksum;
1533 rtx x;
1534 int i, last_bb_num_seen, num_bb_notes, err = 0;
1536 bb_info = (basic_block *) xcalloc (max_uid, sizeof (basic_block));
1537 last_visited = (basic_block *) xcalloc (n_basic_blocks + 2,
1538 sizeof (basic_block));
1539 edge_checksum = (size_t *) xcalloc (n_basic_blocks + 2, sizeof (size_t));
1541 for (i = n_basic_blocks - 1; i >= 0; i--)
1543 basic_block bb = BASIC_BLOCK (i);
1544 rtx head = bb->head;
1545 rtx end = bb->end;
1547 /* Verify the end of the basic block is in the INSN chain. */
1548 for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
1549 if (x == end)
1550 break;
1552 if (!x)
1554 error ("end insn %d for block %d not found in the insn stream",
1555 INSN_UID (end), bb->index);
1556 err = 1;
1559 /* Work backwards from the end to the head of the basic block
1560 to verify the head is in the RTL chain. */
1561 for (; x != NULL_RTX; x = PREV_INSN (x))
1563 /* While walking over the insn chain, verify insns appear
1564 in only one basic block and initialize the BB_INFO array
1565 used by other passes. */
1566 if (bb_info[INSN_UID (x)] != NULL)
1568 error ("insn %d is in multiple basic blocks (%d and %d)",
1569 INSN_UID (x), bb->index, bb_info[INSN_UID (x)]->index);
1570 err = 1;
1573 bb_info[INSN_UID (x)] = bb;
1575 if (x == head)
1576 break;
1578 if (!x)
1580 error ("head insn %d for block %d not found in the insn stream",
1581 INSN_UID (head), bb->index);
1582 err = 1;
1585 last_head = x;
1588 /* Now check the basic blocks (boundaries etc.) */
1589 for (i = n_basic_blocks - 1; i >= 0; i--)
1591 basic_block bb = BASIC_BLOCK (i);
1592 int has_fallthru = 0;
1593 edge e;
1595 for (e = bb->succ; e; e = e->succ_next)
1597 if (last_visited [e->dest->index + 2] == bb)
1599 error ("verify_flow_info: Duplicate edge %i->%i",
1600 e->src->index, e->dest->index);
1601 err = 1;
1604 last_visited [e->dest->index + 2] = bb;
1606 if (e->flags & EDGE_FALLTHRU)
1607 has_fallthru = 1;
1609 if ((e->flags & EDGE_FALLTHRU)
1610 && e->src != ENTRY_BLOCK_PTR
1611 && e->dest != EXIT_BLOCK_PTR)
1613 rtx insn;
1615 if (e->src->index + 1 != e->dest->index)
1617 error
1618 ("verify_flow_info: Incorrect blocks for fallthru %i->%i",
1619 e->src->index, e->dest->index);
1620 err = 1;
1622 else
1623 for (insn = NEXT_INSN (e->src->end); insn != e->dest->head;
1624 insn = NEXT_INSN (insn))
1625 if (GET_CODE (insn) == BARRIER
1626 #ifndef CASE_DROPS_THROUGH
1627 || INSN_P (insn)
1628 #else
1629 || (INSN_P (insn) && ! JUMP_TABLE_DATA_P (insn))
1630 #endif
1633 error ("verify_flow_info: Incorrect fallthru %i->%i",
1634 e->src->index, e->dest->index);
1635 fatal_insn ("wrong insn in the fallthru edge", insn);
1636 err = 1;
1640 if (e->src != bb)
1642 error ("verify_flow_info: Basic block %d succ edge is corrupted",
1643 bb->index);
1644 fprintf (stderr, "Predecessor: ");
1645 dump_edge_info (stderr, e, 0);
1646 fprintf (stderr, "\nSuccessor: ");
1647 dump_edge_info (stderr, e, 1);
1648 fprintf (stderr, "\n");
1649 err = 1;
1652 edge_checksum[e->dest->index + 2] += (size_t) e;
1655 if (!has_fallthru)
1657 rtx insn;
1659 /* Ensure existence of barrier in BB with no fallthru edges. */
1660 for (insn = bb->end; !insn || GET_CODE (insn) != BARRIER;
1661 insn = NEXT_INSN (insn))
1662 if (!insn
1663 || (GET_CODE (insn) == NOTE
1664 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_BASIC_BLOCK))
1666 error ("missing barrier after block %i", bb->index);
1667 err = 1;
1668 break;
1672 for (e = bb->pred; e; e = e->pred_next)
1674 if (e->dest != bb)
1676 error ("basic block %d pred edge is corrupted", bb->index);
1677 fputs ("Predecessor: ", stderr);
1678 dump_edge_info (stderr, e, 0);
1679 fputs ("\nSuccessor: ", stderr);
1680 dump_edge_info (stderr, e, 1);
1681 fputc ('\n', stderr);
1682 err = 1;
1684 edge_checksum[e->dest->index + 2] -= (size_t) e;
1687 for (x = bb->head; x != NEXT_INSN (bb->end); x = NEXT_INSN (x))
1688 if (basic_block_for_insn && BLOCK_FOR_INSN (x) != bb)
1690 debug_rtx (x);
1691 if (! BLOCK_FOR_INSN (x))
1692 error
1693 ("insn %d inside basic block %d but block_for_insn is NULL",
1694 INSN_UID (x), bb->index);
1695 else
1696 error
1697 ("insn %d inside basic block %d but block_for_insn is %i",
1698 INSN_UID (x), bb->index, BLOCK_FOR_INSN (x)->index);
1700 err = 1;
1703 /* OK pointers are correct. Now check the header of basic
1704 block. It ought to contain optional CODE_LABEL followed
1705 by NOTE_BASIC_BLOCK. */
1706 x = bb->head;
1707 if (GET_CODE (x) == CODE_LABEL)
1709 if (bb->end == x)
1711 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
1712 bb->index);
1713 err = 1;
1716 x = NEXT_INSN (x);
1719 if (!NOTE_INSN_BASIC_BLOCK_P (x) || NOTE_BASIC_BLOCK (x) != bb)
1721 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
1722 bb->index);
1723 err = 1;
1726 if (bb->end == x)
1727 /* Do checks for empty blocks her. e */
1729 else
1730 for (x = NEXT_INSN (x); x; x = NEXT_INSN (x))
1732 if (NOTE_INSN_BASIC_BLOCK_P (x))
1734 error ("NOTE_INSN_BASIC_BLOCK %d in middle of basic block %d",
1735 INSN_UID (x), bb->index);
1736 err = 1;
1739 if (x == bb->end)
1740 break;
1742 if (GET_CODE (x) == JUMP_INSN
1743 || GET_CODE (x) == CODE_LABEL
1744 || GET_CODE (x) == BARRIER)
1746 error ("in basic block %d:", bb->index);
1747 fatal_insn ("flow control insn inside a basic block", x);
1752 /* Complete edge checksumming for ENTRY and EXIT. */
1754 edge e;
1756 for (e = ENTRY_BLOCK_PTR->succ; e ; e = e->succ_next)
1757 edge_checksum[e->dest->index + 2] += (size_t) e;
1759 for (e = EXIT_BLOCK_PTR->pred; e ; e = e->pred_next)
1760 edge_checksum[e->dest->index + 2] -= (size_t) e;
1763 for (i = -2; i < n_basic_blocks; ++i)
1764 if (edge_checksum[i + 2])
1766 error ("basic block %i edge lists are corrupted", i);
1767 err = 1;
1770 last_bb_num_seen = -1;
1771 num_bb_notes = 0;
1772 for (x = rtx_first; x; x = NEXT_INSN (x))
1774 if (NOTE_INSN_BASIC_BLOCK_P (x))
1776 basic_block bb = NOTE_BASIC_BLOCK (x);
1778 num_bb_notes++;
1779 if (bb->index != last_bb_num_seen + 1)
1780 internal_error ("basic blocks not numbered consecutively");
1782 last_bb_num_seen = bb->index;
1785 if (!bb_info[INSN_UID (x)])
1787 switch (GET_CODE (x))
1789 case BARRIER:
1790 case NOTE:
1791 break;
1793 case CODE_LABEL:
1794 /* An addr_vec is placed outside any block block. */
1795 if (NEXT_INSN (x)
1796 && GET_CODE (NEXT_INSN (x)) == JUMP_INSN
1797 && (GET_CODE (PATTERN (NEXT_INSN (x))) == ADDR_DIFF_VEC
1798 || GET_CODE (PATTERN (NEXT_INSN (x))) == ADDR_VEC))
1799 x = NEXT_INSN (x);
1801 /* But in any case, non-deletable labels can appear anywhere. */
1802 break;
1804 default:
1805 fatal_insn ("insn outside basic block", x);
1809 if (INSN_P (x)
1810 && GET_CODE (x) == JUMP_INSN
1811 && returnjump_p (x) && ! condjump_p (x)
1812 && ! (NEXT_INSN (x) && GET_CODE (NEXT_INSN (x)) == BARRIER))
1813 fatal_insn ("return not followed by barrier", x);
1816 if (num_bb_notes != n_basic_blocks)
1817 internal_error
1818 ("number of bb notes in insn chain (%d) != n_basic_blocks (%d)",
1819 num_bb_notes, n_basic_blocks);
1821 if (err)
1822 internal_error ("verify_flow_info failed");
1824 /* Clean up. */
1825 free (bb_info);
1826 free (last_visited);
1827 free (edge_checksum);
1830 /* Assume that the preceding pass has possibly eliminated jump instructions
1831 or converted the unconditional jumps. Eliminate the edges from CFG.
1832 Return true if any edges are eliminated. */
1834 bool
1835 purge_dead_edges (bb)
1836 basic_block bb;
1838 edge e, next;
1839 rtx insn = bb->end, note;
1840 bool purged = false;
1842 /* ??? This makes no sense since the later test includes more cases. */
1843 if (GET_CODE (insn) == JUMP_INSN && !simplejump_p (insn))
1844 return false;
1846 if (GET_CODE (insn) == JUMP_INSN)
1848 rtx note;
1849 edge b,f;
1851 /* We do care only about conditional jumps and simplejumps. */
1852 if (!any_condjump_p (insn)
1853 && !returnjump_p (insn)
1854 && !simplejump_p (insn))
1855 return false;
1857 for (e = bb->succ; e; e = next)
1859 next = e->succ_next;
1861 /* Avoid abnormal flags to leak from computed jumps turned
1862 into simplejumps. */
1864 e->flags &= ~EDGE_ABNORMAL;
1866 /* Check purposes we can have edge. */
1867 if ((e->flags & EDGE_FALLTHRU)
1868 && any_condjump_p (insn))
1869 continue;
1870 else if (e->dest != EXIT_BLOCK_PTR
1871 && e->dest->head == JUMP_LABEL (insn))
1872 continue;
1873 else if (e->dest == EXIT_BLOCK_PTR
1874 && returnjump_p (insn))
1875 continue;
1877 purged = true;
1878 remove_edge (e);
1881 if (!bb->succ || !purged)
1882 return false;
1884 if (rtl_dump_file)
1885 fprintf (rtl_dump_file, "Purged edges from bb %i\n", bb->index);
1887 if (!optimize)
1888 return purged;
1890 /* Redistribute probabilities. */
1891 if (!bb->succ->succ_next)
1893 bb->succ->probability = REG_BR_PROB_BASE;
1894 bb->succ->count = bb->count;
1896 else
1898 note = find_reg_note (insn, REG_BR_PROB, NULL);
1899 if (!note)
1900 return purged;
1902 b = BRANCH_EDGE (bb);
1903 f = FALLTHRU_EDGE (bb);
1904 b->probability = INTVAL (XEXP (note, 0));
1905 f->probability = REG_BR_PROB_BASE - b->probability;
1906 b->count = bb->count * b->probability / REG_BR_PROB_BASE;
1907 f->count = bb->count * f->probability / REG_BR_PROB_BASE;
1910 return purged;
1913 /* If this instruction cannot trap, remove REG_EH_REGION notes. */
1914 if (GET_CODE (insn) == INSN
1915 && (note = find_reg_note (insn, REG_EH_REGION, NULL)))
1917 rtx eqnote;
1919 if (! may_trap_p (PATTERN (insn))
1920 || ((eqnote = find_reg_equal_equiv_note (insn))
1921 && ! may_trap_p (XEXP (eqnote, 0))))
1922 remove_note (insn, note);
1925 /* Cleanup abnormal edges caused by throwing insns that have been
1926 eliminated. */
1927 if (! can_throw_internal (bb->end))
1928 for (e = bb->succ; e; e = next)
1930 next = e->succ_next;
1931 if (e->flags & EDGE_EH)
1933 remove_edge (e);
1934 purged = true;
1938 /* If we don't see a jump insn, we don't know exactly why the block would
1939 have been broken at this point. Look for a simple, non-fallthru edge,
1940 as these are only created by conditional branches. If we find such an
1941 edge we know that there used to be a jump here and can then safely
1942 remove all non-fallthru edges. */
1943 for (e = bb->succ; e && (e->flags & (EDGE_COMPLEX | EDGE_FALLTHRU));
1944 e = e->succ_next)
1947 if (!e)
1948 return purged;
1950 for (e = bb->succ; e; e = next)
1952 next = e->succ_next;
1953 if (!(e->flags & EDGE_FALLTHRU))
1954 remove_edge (e), purged = true;
1957 if (!bb->succ || bb->succ->succ_next)
1958 abort ();
1960 bb->succ->probability = REG_BR_PROB_BASE;
1961 bb->succ->count = bb->count;
1963 if (rtl_dump_file)
1964 fprintf (rtl_dump_file, "Purged non-fallthru edges from bb %i\n",
1965 bb->index);
1966 return purged;
1969 /* Search all basic blocks for potentially dead edges and purge them. Return
1970 true if some edge has been eliminated. */
1972 bool
1973 purge_all_dead_edges (update_life_p)
1974 int update_life_p;
1976 int i, purged = false;
1977 sbitmap blocks = 0;
1979 if (update_life_p)
1981 blocks = sbitmap_alloc (n_basic_blocks);
1982 sbitmap_zero (blocks);
1985 for (i = 0; i < n_basic_blocks; i++)
1987 bool purged_here = purge_dead_edges (BASIC_BLOCK (i));
1989 purged |= purged_here;
1990 if (purged_here && update_life_p)
1991 SET_BIT (blocks, i);
1994 if (update_life_p && purged)
1995 update_life_info (blocks, UPDATE_LIFE_GLOBAL,
1996 PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
1997 | PROP_KILL_DEAD_CODE);
1999 if (update_life_p)
2000 sbitmap_free (blocks);
2001 return purged;