* basic-block.h (ei_safe_edge): New function.
[official-gcc.git] / gcc / cfgrtl.c
blob0977fd6dc61ace2de7fa3be9951e731cbbe6d0db
1 /* Control flow graph manipulation code for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
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 - Basic CFG/RTL manipulation API documented in cfghooks.h
27 - CFG-aware instruction chain manipulation
28 delete_insn, delete_insn_chain
29 - Edge splitting and committing to edges
30 insert_insn_on_edge, commit_edge_insertions
31 - CFG updating after insn simplification
32 purge_dead_edges, purge_all_dead_edges
34 Functions not supposed for generic use:
35 - Infrastructure to determine quickly basic block for insn
36 compute_bb_for_insn, update_bb_for_insn, set_block_for_insn,
37 - Edge redirection with updating and optimizing of insn chain
38 block_label, tidy_fallthru_edge, force_nonfallthru */
40 #include "config.h"
41 #include "system.h"
42 #include "coretypes.h"
43 #include "tm.h"
44 #include "tree.h"
45 #include "rtl.h"
46 #include "hard-reg-set.h"
47 #include "basic-block.h"
48 #include "regs.h"
49 #include "flags.h"
50 #include "output.h"
51 #include "function.h"
52 #include "except.h"
53 #include "toplev.h"
54 #include "tm_p.h"
55 #include "obstack.h"
56 #include "insn-config.h"
57 #include "cfglayout.h"
58 #include "expr.h"
59 #include "target.h"
62 /* The labels mentioned in non-jump rtl. Valid during find_basic_blocks. */
63 /* ??? Should probably be using LABEL_NUSES instead. It would take a
64 bit of surgery to be able to use or co-opt the routines in jump. */
65 rtx label_value_list;
67 static int can_delete_note_p (rtx);
68 static int can_delete_label_p (rtx);
69 static void commit_one_edge_insertion (edge, int);
70 static rtx last_loop_beg_note (rtx);
71 static bool back_edge_of_syntactic_loop_p (basic_block, basic_block);
72 basic_block force_nonfallthru_and_redirect (edge, basic_block);
73 static basic_block rtl_split_edge (edge);
74 static bool rtl_move_block_after (basic_block, basic_block);
75 static int rtl_verify_flow_info (void);
76 static basic_block cfg_layout_split_block (basic_block, void *);
77 static edge cfg_layout_redirect_edge_and_branch (edge, basic_block);
78 static basic_block cfg_layout_redirect_edge_and_branch_force (edge, basic_block);
79 static void cfg_layout_delete_block (basic_block);
80 static void rtl_delete_block (basic_block);
81 static basic_block rtl_redirect_edge_and_branch_force (edge, basic_block);
82 static edge rtl_redirect_edge_and_branch (edge, basic_block);
83 static basic_block rtl_split_block (basic_block, void *);
84 static void rtl_dump_bb (basic_block, FILE *, int);
85 static int rtl_verify_flow_info_1 (void);
86 static void mark_killed_regs (rtx, rtx, void *);
87 static void rtl_make_forwarder_block (edge);
89 /* Return true if NOTE is not one of the ones that must be kept paired,
90 so that we may simply delete it. */
92 static int
93 can_delete_note_p (rtx note)
95 return (NOTE_LINE_NUMBER (note) == NOTE_INSN_DELETED
96 || NOTE_LINE_NUMBER (note) == NOTE_INSN_BASIC_BLOCK
97 || NOTE_LINE_NUMBER (note) == NOTE_INSN_UNLIKELY_EXECUTED_CODE);
100 /* True if a given label can be deleted. */
102 static int
103 can_delete_label_p (rtx label)
105 return (!LABEL_PRESERVE_P (label)
106 /* User declared labels must be preserved. */
107 && LABEL_NAME (label) == 0
108 && !in_expr_list_p (forced_labels, label)
109 && !in_expr_list_p (label_value_list, label));
112 /* Delete INSN by patching it out. Return the next insn. */
115 delete_insn (rtx insn)
117 rtx next = NEXT_INSN (insn);
118 rtx note;
119 bool really_delete = true;
121 if (LABEL_P (insn))
123 /* Some labels can't be directly removed from the INSN chain, as they
124 might be references via variables, constant pool etc.
125 Convert them to the special NOTE_INSN_DELETED_LABEL note. */
126 if (! can_delete_label_p (insn))
128 const char *name = LABEL_NAME (insn);
130 really_delete = false;
131 PUT_CODE (insn, NOTE);
132 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED_LABEL;
133 NOTE_DELETED_LABEL_NAME (insn) = name;
136 remove_node_from_expr_list (insn, &nonlocal_goto_handler_labels);
139 if (really_delete)
141 /* If this insn has already been deleted, something is very wrong. */
142 gcc_assert (!INSN_DELETED_P (insn));
143 remove_insn (insn);
144 INSN_DELETED_P (insn) = 1;
147 /* If deleting a jump, decrement the use count of the label. Deleting
148 the label itself should happen in the normal course of block merging. */
149 if (JUMP_P (insn)
150 && JUMP_LABEL (insn)
151 && LABEL_P (JUMP_LABEL (insn)))
152 LABEL_NUSES (JUMP_LABEL (insn))--;
154 /* Also if deleting an insn that references a label. */
155 else
157 while ((note = find_reg_note (insn, REG_LABEL, NULL_RTX)) != NULL_RTX
158 && LABEL_P (XEXP (note, 0)))
160 LABEL_NUSES (XEXP (note, 0))--;
161 remove_note (insn, note);
165 if (JUMP_P (insn)
166 && (GET_CODE (PATTERN (insn)) == ADDR_VEC
167 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
169 rtx pat = PATTERN (insn);
170 int diff_vec_p = GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC;
171 int len = XVECLEN (pat, diff_vec_p);
172 int i;
174 for (i = 0; i < len; i++)
176 rtx label = XEXP (XVECEXP (pat, diff_vec_p, i), 0);
178 /* When deleting code in bulk (e.g. removing many unreachable
179 blocks) we can delete a label that's a target of the vector
180 before deleting the vector itself. */
181 if (!NOTE_P (label))
182 LABEL_NUSES (label)--;
186 return next;
189 /* Like delete_insn but also purge dead edges from BB. */
191 delete_insn_and_edges (rtx insn)
193 rtx x;
194 bool purge = false;
196 if (INSN_P (insn)
197 && BLOCK_FOR_INSN (insn)
198 && BB_END (BLOCK_FOR_INSN (insn)) == insn)
199 purge = true;
200 x = delete_insn (insn);
201 if (purge)
202 purge_dead_edges (BLOCK_FOR_INSN (insn));
203 return x;
206 /* Unlink a chain of insns between START and FINISH, leaving notes
207 that must be paired. */
209 void
210 delete_insn_chain (rtx start, rtx finish)
212 rtx next;
214 /* Unchain the insns one by one. It would be quicker to delete all of these
215 with a single unchaining, rather than one at a time, but we need to keep
216 the NOTE's. */
217 while (1)
219 next = NEXT_INSN (start);
220 if (NOTE_P (start) && !can_delete_note_p (start))
222 else
223 next = delete_insn (start);
225 if (start == finish)
226 break;
227 start = next;
231 /* Like delete_insn but also purge dead edges from BB. */
232 void
233 delete_insn_chain_and_edges (rtx first, rtx last)
235 bool purge = false;
237 if (INSN_P (last)
238 && BLOCK_FOR_INSN (last)
239 && BB_END (BLOCK_FOR_INSN (last)) == last)
240 purge = true;
241 delete_insn_chain (first, last);
242 if (purge)
243 purge_dead_edges (BLOCK_FOR_INSN (last));
246 /* Create a new basic block consisting of the instructions between HEAD and END
247 inclusive. This function is designed to allow fast BB construction - reuses
248 the note and basic block struct in BB_NOTE, if any and do not grow
249 BASIC_BLOCK chain and should be used directly only by CFG construction code.
250 END can be NULL in to create new empty basic block before HEAD. Both END
251 and HEAD can be NULL to create basic block at the end of INSN chain.
252 AFTER is the basic block we should be put after. */
254 basic_block
255 create_basic_block_structure (rtx head, rtx end, rtx bb_note, basic_block after)
257 basic_block bb;
259 if (bb_note
260 && (bb = NOTE_BASIC_BLOCK (bb_note)) != NULL
261 && bb->aux == NULL)
263 /* If we found an existing note, thread it back onto the chain. */
265 rtx after;
267 if (LABEL_P (head))
268 after = head;
269 else
271 after = PREV_INSN (head);
272 head = bb_note;
275 if (after != bb_note && NEXT_INSN (after) != bb_note)
276 reorder_insns_nobb (bb_note, bb_note, after);
278 else
280 /* Otherwise we must create a note and a basic block structure. */
282 bb = alloc_block ();
284 if (!head && !end)
285 head = end = bb_note
286 = emit_note_after (NOTE_INSN_BASIC_BLOCK, get_last_insn ());
287 else if (LABEL_P (head) && end)
289 bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, head);
290 if (head == end)
291 end = bb_note;
293 else
295 bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, head);
296 head = bb_note;
297 if (!end)
298 end = head;
301 NOTE_BASIC_BLOCK (bb_note) = bb;
304 /* Always include the bb note in the block. */
305 if (NEXT_INSN (end) == bb_note)
306 end = bb_note;
308 BB_HEAD (bb) = head;
309 BB_END (bb) = end;
310 bb->index = last_basic_block++;
311 bb->flags = BB_NEW;
312 link_block (bb, after);
313 BASIC_BLOCK (bb->index) = bb;
314 update_bb_for_insn (bb);
315 BB_SET_PARTITION (bb, BB_UNPARTITIONED);
317 /* Tag the block so that we know it has been used when considering
318 other basic block notes. */
319 bb->aux = bb;
321 return bb;
324 /* Create new basic block consisting of instructions in between HEAD and END
325 and place it to the BB chain after block AFTER. END can be NULL in to
326 create new empty basic block before HEAD. Both END and HEAD can be NULL to
327 create basic block at the end of INSN chain. */
329 static basic_block
330 rtl_create_basic_block (void *headp, void *endp, basic_block after)
332 rtx head = headp, end = endp;
333 basic_block bb;
335 /* Grow the basic block array if needed. */
336 if ((size_t) last_basic_block >= VARRAY_SIZE (basic_block_info))
338 size_t new_size = last_basic_block + (last_basic_block + 3) / 4;
339 VARRAY_GROW (basic_block_info, new_size);
342 n_basic_blocks++;
344 bb = create_basic_block_structure (head, end, NULL, after);
345 bb->aux = NULL;
346 return bb;
349 static basic_block
350 cfg_layout_create_basic_block (void *head, void *end, basic_block after)
352 basic_block newbb = rtl_create_basic_block (head, end, after);
354 initialize_bb_rbi (newbb);
355 return newbb;
358 /* Delete the insns in a (non-live) block. We physically delete every
359 non-deleted-note insn, and update the flow graph appropriately.
361 Return nonzero if we deleted an exception handler. */
363 /* ??? Preserving all such notes strikes me as wrong. It would be nice
364 to post-process the stream to remove empty blocks, loops, ranges, etc. */
366 static void
367 rtl_delete_block (basic_block b)
369 rtx insn, end, tmp;
371 /* If the head of this block is a CODE_LABEL, then it might be the
372 label for an exception handler which can't be reached.
374 We need to remove the label from the exception_handler_label list
375 and remove the associated NOTE_INSN_EH_REGION_BEG and
376 NOTE_INSN_EH_REGION_END notes. */
378 insn = BB_HEAD (b);
380 if (LABEL_P (insn))
381 maybe_remove_eh_handler (insn);
383 /* Include any jump table following the basic block. */
384 end = BB_END (b);
385 if (tablejump_p (end, NULL, &tmp))
386 end = tmp;
388 /* Include any barrier that may follow the basic block. */
389 tmp = next_nonnote_insn (end);
390 if (tmp && BARRIER_P (tmp))
391 end = tmp;
393 /* Selectively delete the entire chain. */
394 BB_HEAD (b) = NULL;
395 delete_insn_chain (insn, end);
398 /* Records the basic block struct in BLOCK_FOR_INSN for every insn. */
400 void
401 compute_bb_for_insn (void)
403 basic_block bb;
405 FOR_EACH_BB (bb)
407 rtx end = BB_END (bb);
408 rtx insn;
410 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
412 BLOCK_FOR_INSN (insn) = bb;
413 if (insn == end)
414 break;
419 /* Release the basic_block_for_insn array. */
421 void
422 free_bb_for_insn (void)
424 rtx insn;
425 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
426 if (!BARRIER_P (insn))
427 BLOCK_FOR_INSN (insn) = NULL;
430 /* Return RTX to emit after when we want to emit code on the entry of function. */
432 entry_of_function (void)
434 return (n_basic_blocks ? BB_HEAD (ENTRY_BLOCK_PTR->next_bb) : get_insns ());
437 /* Update insns block within BB. */
439 void
440 update_bb_for_insn (basic_block bb)
442 rtx insn;
444 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
446 if (!BARRIER_P (insn))
447 set_block_for_insn (insn, bb);
448 if (insn == BB_END (bb))
449 break;
453 /* Creates a new basic block just after basic block B by splitting
454 everything after specified instruction I. */
456 static basic_block
457 rtl_split_block (basic_block bb, void *insnp)
459 basic_block new_bb;
460 rtx insn = insnp;
461 edge e;
462 edge_iterator ei;
464 if (!insn)
466 insn = first_insn_after_basic_block_note (bb);
468 if (insn)
469 insn = PREV_INSN (insn);
470 else
471 insn = get_last_insn ();
474 /* We probably should check type of the insn so that we do not create
475 inconsistent cfg. It is checked in verify_flow_info anyway, so do not
476 bother. */
477 if (insn == BB_END (bb))
478 emit_note_after (NOTE_INSN_DELETED, insn);
480 /* Create the new basic block. */
481 new_bb = create_basic_block (NEXT_INSN (insn), BB_END (bb), bb);
482 BB_COPY_PARTITION (new_bb, bb);
483 BB_END (bb) = insn;
485 /* Redirect the outgoing edges. */
486 new_bb->succs = bb->succs;
487 bb->succs = NULL;
488 FOR_EACH_EDGE (e, ei, new_bb->succs)
490 e->src = new_bb;
493 if (bb->global_live_at_start)
495 new_bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (&flow_obstack);
496 new_bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (&flow_obstack);
497 COPY_REG_SET (new_bb->global_live_at_end, bb->global_live_at_end);
499 /* We now have to calculate which registers are live at the end
500 of the split basic block and at the start of the new basic
501 block. Start with those registers that are known to be live
502 at the end of the original basic block and get
503 propagate_block to determine which registers are live. */
504 COPY_REG_SET (new_bb->global_live_at_start, bb->global_live_at_end);
505 propagate_block (new_bb, new_bb->global_live_at_start, NULL, NULL, 0);
506 COPY_REG_SET (bb->global_live_at_end,
507 new_bb->global_live_at_start);
508 #ifdef HAVE_conditional_execution
509 /* In the presence of conditional execution we are not able to update
510 liveness precisely. */
511 if (reload_completed)
513 bb->flags |= BB_DIRTY;
514 new_bb->flags |= BB_DIRTY;
516 #endif
519 return new_bb;
522 /* Blocks A and B are to be merged into a single block A. The insns
523 are already contiguous. */
525 static void
526 rtl_merge_blocks (basic_block a, basic_block b)
528 rtx b_head = BB_HEAD (b), b_end = BB_END (b), a_end = BB_END (a);
529 rtx del_first = NULL_RTX, del_last = NULL_RTX;
530 int b_empty = 0;
532 /* If there was a CODE_LABEL beginning B, delete it. */
533 if (LABEL_P (b_head))
535 /* Detect basic blocks with nothing but a label. This can happen
536 in particular at the end of a function. */
537 if (b_head == b_end)
538 b_empty = 1;
540 del_first = del_last = b_head;
541 b_head = NEXT_INSN (b_head);
544 /* Delete the basic block note and handle blocks containing just that
545 note. */
546 if (NOTE_INSN_BASIC_BLOCK_P (b_head))
548 if (b_head == b_end)
549 b_empty = 1;
550 if (! del_last)
551 del_first = b_head;
553 del_last = b_head;
554 b_head = NEXT_INSN (b_head);
557 /* If there was a jump out of A, delete it. */
558 if (JUMP_P (a_end))
560 rtx prev;
562 for (prev = PREV_INSN (a_end); ; prev = PREV_INSN (prev))
563 if (!NOTE_P (prev)
564 || NOTE_LINE_NUMBER (prev) == NOTE_INSN_BASIC_BLOCK
565 || prev == BB_HEAD (a))
566 break;
568 del_first = a_end;
570 #ifdef HAVE_cc0
571 /* If this was a conditional jump, we need to also delete
572 the insn that set cc0. */
573 if (only_sets_cc0_p (prev))
575 rtx tmp = prev;
577 prev = prev_nonnote_insn (prev);
578 if (!prev)
579 prev = BB_HEAD (a);
580 del_first = tmp;
582 #endif
584 a_end = PREV_INSN (del_first);
586 else if (BARRIER_P (NEXT_INSN (a_end)))
587 del_first = NEXT_INSN (a_end);
589 /* Delete everything marked above as well as crap that might be
590 hanging out between the two blocks. */
591 BB_HEAD (b) = NULL;
592 delete_insn_chain (del_first, del_last);
594 /* Reassociate the insns of B with A. */
595 if (!b_empty)
597 rtx x;
599 for (x = a_end; x != b_end; x = NEXT_INSN (x))
600 set_block_for_insn (x, a);
602 set_block_for_insn (b_end, a);
604 a_end = b_end;
607 BB_END (a) = a_end;
610 /* Return true when block A and B can be merged. */
611 static bool
612 rtl_can_merge_blocks (basic_block a, basic_block b)
614 /* If we are partitioning hot/cold basic blocks, we don't want to
615 mess up unconditional or indirect jumps that cross between hot
616 and cold sections.
618 Basic block partitioning may result in some jumps that appear to
619 be optimizable (or blocks that appear to be mergeable), but which really
620 must be left untouched (they are required to make it safely across
621 partition boundaries). See the comments at the top of
622 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
624 if (flag_reorder_blocks_and_partition
625 && (find_reg_note (BB_END (a), REG_CROSSING_JUMP, NULL_RTX)
626 || find_reg_note (BB_END (b), REG_CROSSING_JUMP, NULL_RTX)
627 || BB_PARTITION (a) != BB_PARTITION (b)))
628 return false;
630 /* There must be exactly one edge in between the blocks. */
631 return (EDGE_COUNT (a->succs) == 1
632 && EDGE_SUCC (a, 0)->dest == b
633 && EDGE_COUNT (b->preds) == 1
634 && a != b
635 /* Must be simple edge. */
636 && !(EDGE_SUCC (a, 0)->flags & EDGE_COMPLEX)
637 && a->next_bb == b
638 && a != ENTRY_BLOCK_PTR && b != EXIT_BLOCK_PTR
639 /* If the jump insn has side effects,
640 we can't kill the edge. */
641 && (!JUMP_P (BB_END (a))
642 || (reload_completed
643 ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
646 /* Return the label in the head of basic block BLOCK. Create one if it doesn't
647 exist. */
650 block_label (basic_block block)
652 if (block == EXIT_BLOCK_PTR)
653 return NULL_RTX;
655 if (!LABEL_P (BB_HEAD (block)))
657 BB_HEAD (block) = emit_label_before (gen_label_rtx (), BB_HEAD (block));
660 return BB_HEAD (block);
663 /* Attempt to perform edge redirection by replacing possibly complex jump
664 instruction by unconditional jump or removing jump completely. This can
665 apply only if all edges now point to the same block. The parameters and
666 return values are equivalent to redirect_edge_and_branch. */
668 edge
669 try_redirect_by_replacing_jump (edge e, basic_block target, bool in_cfglayout)
671 basic_block src = e->src;
672 rtx insn = BB_END (src), kill_from;
673 edge tmp;
674 rtx set;
675 int fallthru = 0;
676 edge_iterator ei;
678 /* If we are partitioning hot/cold basic blocks, we don't want to
679 mess up unconditional or indirect jumps that cross between hot
680 and cold sections.
682 Basic block partitioning may result in some jumps that appear to
683 be optimizable (or blocks that appear to be mergeable), but which really
684 must be left untouched (they are required to make it safely across
685 partition boundaries). See the comments at the top of
686 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
688 if (flag_reorder_blocks_and_partition
689 && (find_reg_note (insn, REG_CROSSING_JUMP, NULL_RTX)
690 || BB_PARTITION (src) != BB_PARTITION (target)))
691 return NULL;
693 /* Verify that all targets will be TARGET. */
694 FOR_EACH_EDGE (tmp, ei, src->succs)
696 if (tmp->dest != target && tmp != e)
697 break;
700 if (tmp || !onlyjump_p (insn))
701 return NULL;
702 if ((!optimize || reload_completed) && tablejump_p (insn, NULL, NULL))
703 return NULL;
705 /* Avoid removing branch with side effects. */
706 set = single_set (insn);
707 if (!set || side_effects_p (set))
708 return NULL;
710 /* In case we zap a conditional jump, we'll need to kill
711 the cc0 setter too. */
712 kill_from = insn;
713 #ifdef HAVE_cc0
714 if (reg_mentioned_p (cc0_rtx, PATTERN (insn)))
715 kill_from = PREV_INSN (insn);
716 #endif
718 /* See if we can create the fallthru edge. */
719 if (in_cfglayout || can_fallthru (src, target))
721 if (dump_file)
722 fprintf (dump_file, "Removing jump %i.\n", INSN_UID (insn));
723 fallthru = 1;
725 /* Selectively unlink whole insn chain. */
726 if (in_cfglayout)
728 rtx insn = src->rbi->footer;
730 delete_insn_chain (kill_from, BB_END (src));
732 /* Remove barriers but keep jumptables. */
733 while (insn)
735 if (BARRIER_P (insn))
737 if (PREV_INSN (insn))
738 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
739 else
740 src->rbi->footer = NEXT_INSN (insn);
741 if (NEXT_INSN (insn))
742 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
744 if (LABEL_P (insn))
745 break;
746 insn = NEXT_INSN (insn);
749 else
750 delete_insn_chain (kill_from, PREV_INSN (BB_HEAD (target)));
753 /* If this already is simplejump, redirect it. */
754 else if (simplejump_p (insn))
756 if (e->dest == target)
757 return NULL;
758 if (dump_file)
759 fprintf (dump_file, "Redirecting jump %i from %i to %i.\n",
760 INSN_UID (insn), e->dest->index, target->index);
761 if (!redirect_jump (insn, block_label (target), 0))
763 gcc_assert (target == EXIT_BLOCK_PTR);
764 return NULL;
768 /* Cannot do anything for target exit block. */
769 else if (target == EXIT_BLOCK_PTR)
770 return NULL;
772 /* Or replace possibly complicated jump insn by simple jump insn. */
773 else
775 rtx target_label = block_label (target);
776 rtx barrier, label, table;
778 emit_jump_insn_after (gen_jump (target_label), insn);
779 JUMP_LABEL (BB_END (src)) = target_label;
780 LABEL_NUSES (target_label)++;
781 if (dump_file)
782 fprintf (dump_file, "Replacing insn %i by jump %i\n",
783 INSN_UID (insn), INSN_UID (BB_END (src)));
786 delete_insn_chain (kill_from, insn);
788 /* Recognize a tablejump that we are converting to a
789 simple jump and remove its associated CODE_LABEL
790 and ADDR_VEC or ADDR_DIFF_VEC. */
791 if (tablejump_p (insn, &label, &table))
792 delete_insn_chain (label, table);
794 barrier = next_nonnote_insn (BB_END (src));
795 if (!barrier || !BARRIER_P (barrier))
796 emit_barrier_after (BB_END (src));
797 else
799 if (barrier != NEXT_INSN (BB_END (src)))
801 /* Move the jump before barrier so that the notes
802 which originally were or were created before jump table are
803 inside the basic block. */
804 rtx new_insn = BB_END (src);
805 rtx tmp;
807 for (tmp = NEXT_INSN (BB_END (src)); tmp != barrier;
808 tmp = NEXT_INSN (tmp))
809 set_block_for_insn (tmp, src);
811 NEXT_INSN (PREV_INSN (new_insn)) = NEXT_INSN (new_insn);
812 PREV_INSN (NEXT_INSN (new_insn)) = PREV_INSN (new_insn);
814 NEXT_INSN (new_insn) = barrier;
815 NEXT_INSN (PREV_INSN (barrier)) = new_insn;
817 PREV_INSN (new_insn) = PREV_INSN (barrier);
818 PREV_INSN (barrier) = new_insn;
823 /* Keep only one edge out and set proper flags. */
824 while (EDGE_COUNT (src->succs) > 1)
825 remove_edge (e);
827 e = EDGE_SUCC (src, 0);
828 if (fallthru)
829 e->flags = EDGE_FALLTHRU;
830 else
831 e->flags = 0;
833 e->probability = REG_BR_PROB_BASE;
834 e->count = src->count;
836 /* We don't want a block to end on a line-number note since that has
837 the potential of changing the code between -g and not -g. */
838 while (NOTE_P (BB_END (e->src))
839 && NOTE_LINE_NUMBER (BB_END (e->src)) >= 0)
840 delete_insn (BB_END (e->src));
842 if (e->dest != target)
843 redirect_edge_succ (e, target);
845 return e;
848 /* Return last loop_beg note appearing after INSN, before start of next
849 basic block. Return INSN if there are no such notes.
851 When emitting jump to redirect a fallthru edge, it should always appear
852 after the LOOP_BEG notes, as loop optimizer expect loop to either start by
853 fallthru edge or jump following the LOOP_BEG note jumping to the loop exit
854 test. */
856 static rtx
857 last_loop_beg_note (rtx insn)
859 rtx last = insn;
861 for (insn = NEXT_INSN (insn); insn && NOTE_P (insn)
862 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_BASIC_BLOCK;
863 insn = NEXT_INSN (insn))
864 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
865 last = insn;
867 return last;
870 /* Redirect edge representing branch of (un)conditional jump or tablejump,
871 NULL on failure */
872 static edge
873 redirect_branch_edge (edge e, basic_block target)
875 rtx tmp;
876 rtx old_label = BB_HEAD (e->dest);
877 basic_block src = e->src;
878 rtx insn = BB_END (src);
880 /* We can only redirect non-fallthru edges of jump insn. */
881 if (e->flags & EDGE_FALLTHRU)
882 return NULL;
883 else if (!JUMP_P (insn))
884 return NULL;
886 /* Recognize a tablejump and adjust all matching cases. */
887 if (tablejump_p (insn, NULL, &tmp))
889 rtvec vec;
890 int j;
891 rtx new_label = block_label (target);
893 if (target == EXIT_BLOCK_PTR)
894 return NULL;
895 if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
896 vec = XVEC (PATTERN (tmp), 0);
897 else
898 vec = XVEC (PATTERN (tmp), 1);
900 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
901 if (XEXP (RTVEC_ELT (vec, j), 0) == old_label)
903 RTVEC_ELT (vec, j) = gen_rtx_LABEL_REF (Pmode, new_label);
904 --LABEL_NUSES (old_label);
905 ++LABEL_NUSES (new_label);
908 /* Handle casesi dispatch insns. */
909 if ((tmp = single_set (insn)) != NULL
910 && SET_DEST (tmp) == pc_rtx
911 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
912 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF
913 && XEXP (XEXP (SET_SRC (tmp), 2), 0) == old_label)
915 XEXP (SET_SRC (tmp), 2) = gen_rtx_LABEL_REF (VOIDmode,
916 new_label);
917 --LABEL_NUSES (old_label);
918 ++LABEL_NUSES (new_label);
921 else
923 /* ?? We may play the games with moving the named labels from
924 one basic block to the other in case only one computed_jump is
925 available. */
926 if (computed_jump_p (insn)
927 /* A return instruction can't be redirected. */
928 || returnjump_p (insn))
929 return NULL;
931 /* If the insn doesn't go where we think, we're confused. */
932 gcc_assert (JUMP_LABEL (insn) == old_label);
934 /* If the substitution doesn't succeed, die. This can happen
935 if the back end emitted unrecognizable instructions or if
936 target is exit block on some arches. */
937 if (!redirect_jump (insn, block_label (target), 0))
939 gcc_assert (target == EXIT_BLOCK_PTR);
940 return NULL;
944 if (dump_file)
945 fprintf (dump_file, "Edge %i->%i redirected to %i\n",
946 e->src->index, e->dest->index, target->index);
948 if (e->dest != target)
949 e = redirect_edge_succ_nodup (e, target);
950 return e;
953 /* Attempt to change code to redirect edge E to TARGET. Don't do that on
954 expense of adding new instructions or reordering basic blocks.
956 Function can be also called with edge destination equivalent to the TARGET.
957 Then it should try the simplifications and do nothing if none is possible.
959 Return edge representing the branch if transformation succeeded. Return NULL
960 on failure.
961 We still return NULL in case E already destinated TARGET and we didn't
962 managed to simplify instruction stream. */
964 static edge
965 rtl_redirect_edge_and_branch (edge e, basic_block target)
967 edge ret;
968 basic_block src = e->src;
970 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
971 return NULL;
973 if (e->dest == target)
974 return e;
976 if ((ret = try_redirect_by_replacing_jump (e, target, false)) != NULL)
978 src->flags |= BB_DIRTY;
979 return ret;
982 ret = redirect_branch_edge (e, target);
983 if (!ret)
984 return NULL;
986 src->flags |= BB_DIRTY;
987 return ret;
990 /* Like force_nonfallthru below, but additionally performs redirection
991 Used by redirect_edge_and_branch_force. */
993 basic_block
994 force_nonfallthru_and_redirect (edge e, basic_block target)
996 basic_block jump_block, new_bb = NULL, src = e->src;
997 rtx note;
998 edge new_edge;
999 int abnormal_edge_flags = 0;
1001 /* In the case the last instruction is conditional jump to the next
1002 instruction, first redirect the jump itself and then continue
1003 by creating a basic block afterwards to redirect fallthru edge. */
1004 if (e->src != ENTRY_BLOCK_PTR && e->dest != EXIT_BLOCK_PTR
1005 && any_condjump_p (BB_END (e->src))
1006 /* When called from cfglayout, fallthru edges do not
1007 necessarily go to the next block. */
1008 && e->src->next_bb == e->dest
1009 && JUMP_LABEL (BB_END (e->src)) == BB_HEAD (e->dest))
1011 rtx note;
1012 edge b = unchecked_make_edge (e->src, target, 0);
1013 bool redirected;
1015 redirected = redirect_jump (BB_END (e->src), block_label (target), 0);
1016 gcc_assert (redirected);
1018 note = find_reg_note (BB_END (e->src), REG_BR_PROB, NULL_RTX);
1019 if (note)
1021 int prob = INTVAL (XEXP (note, 0));
1023 b->probability = prob;
1024 b->count = e->count * prob / REG_BR_PROB_BASE;
1025 e->probability -= e->probability;
1026 e->count -= b->count;
1027 if (e->probability < 0)
1028 e->probability = 0;
1029 if (e->count < 0)
1030 e->count = 0;
1034 if (e->flags & EDGE_ABNORMAL)
1036 /* Irritating special case - fallthru edge to the same block as abnormal
1037 edge.
1038 We can't redirect abnormal edge, but we still can split the fallthru
1039 one and create separate abnormal edge to original destination.
1040 This allows bb-reorder to make such edge non-fallthru. */
1041 gcc_assert (e->dest == target);
1042 abnormal_edge_flags = e->flags & ~(EDGE_FALLTHRU | EDGE_CAN_FALLTHRU);
1043 e->flags &= EDGE_FALLTHRU | EDGE_CAN_FALLTHRU;
1045 else
1047 gcc_assert (e->flags & EDGE_FALLTHRU);
1048 if (e->src == ENTRY_BLOCK_PTR)
1050 /* We can't redirect the entry block. Create an empty block
1051 at the start of the function which we use to add the new
1052 jump. */
1053 edge tmp;
1054 unsigned ix;
1055 bool found = false;
1057 basic_block bb = create_basic_block (BB_HEAD (e->dest), NULL, ENTRY_BLOCK_PTR);
1059 /* Change the existing edge's source to be the new block, and add
1060 a new edge from the entry block to the new block. */
1061 e->src = bb;
1062 for (ix = 0; VEC_iterate (edge, ENTRY_BLOCK_PTR->succs, ix, tmp); )
1064 if (tmp == e)
1066 VEC_unordered_remove (edge, ENTRY_BLOCK_PTR->succs, ix);
1067 found = true;
1068 break;
1070 else
1071 ix++;
1074 if (!found)
1075 abort ();
1077 VEC_safe_push (edge, bb->succs, e);
1078 make_single_succ_edge (ENTRY_BLOCK_PTR, bb, EDGE_FALLTHRU);
1082 if (EDGE_COUNT (e->src->succs) >= 2 || abnormal_edge_flags)
1084 /* Create the new structures. */
1086 /* If the old block ended with a tablejump, skip its table
1087 by searching forward from there. Otherwise start searching
1088 forward from the last instruction of the old block. */
1089 if (!tablejump_p (BB_END (e->src), NULL, &note))
1090 note = BB_END (e->src);
1092 /* Position the new block correctly relative to loop notes. */
1093 note = last_loop_beg_note (note);
1094 note = NEXT_INSN (note);
1096 jump_block = create_basic_block (note, NULL, e->src);
1097 jump_block->count = e->count;
1098 jump_block->frequency = EDGE_FREQUENCY (e);
1099 jump_block->loop_depth = target->loop_depth;
1101 if (target->global_live_at_start)
1103 jump_block->global_live_at_start
1104 = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1105 jump_block->global_live_at_end
1106 = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1107 COPY_REG_SET (jump_block->global_live_at_start,
1108 target->global_live_at_start);
1109 COPY_REG_SET (jump_block->global_live_at_end,
1110 target->global_live_at_start);
1113 /* Make sure new block ends up in correct hot/cold section. */
1115 BB_COPY_PARTITION (jump_block, e->src);
1116 if (flag_reorder_blocks_and_partition
1117 && targetm.have_named_sections)
1119 if (BB_PARTITION (jump_block) == BB_COLD_PARTITION)
1121 rtx bb_note, new_note;
1122 for (bb_note = BB_HEAD (jump_block);
1123 bb_note && bb_note != NEXT_INSN (BB_END (jump_block));
1124 bb_note = NEXT_INSN (bb_note))
1125 if (NOTE_P (bb_note)
1126 && NOTE_LINE_NUMBER (bb_note) == NOTE_INSN_BASIC_BLOCK)
1127 break;
1128 new_note = emit_note_after (NOTE_INSN_UNLIKELY_EXECUTED_CODE,
1129 bb_note);
1130 NOTE_BASIC_BLOCK (new_note) = jump_block;
1132 if (JUMP_P (BB_END (jump_block))
1133 && !any_condjump_p (BB_END (jump_block))
1134 && (EDGE_SUCC (jump_block, 0)->flags & EDGE_CROSSING))
1135 REG_NOTES (BB_END (jump_block)) = gen_rtx_EXPR_LIST
1136 (REG_CROSSING_JUMP, NULL_RTX,
1137 REG_NOTES (BB_END (jump_block)));
1140 /* Wire edge in. */
1141 new_edge = make_edge (e->src, jump_block, EDGE_FALLTHRU);
1142 new_edge->probability = e->probability;
1143 new_edge->count = e->count;
1145 /* Redirect old edge. */
1146 redirect_edge_pred (e, jump_block);
1147 e->probability = REG_BR_PROB_BASE;
1149 new_bb = jump_block;
1151 else
1152 jump_block = e->src;
1154 e->flags &= ~EDGE_FALLTHRU;
1155 if (target == EXIT_BLOCK_PTR)
1157 #ifdef HAVE_return
1158 emit_jump_insn_after (gen_return (), BB_END (jump_block));
1159 #else
1160 gcc_unreachable ();
1161 #endif
1163 else
1165 rtx label = block_label (target);
1166 emit_jump_insn_after (gen_jump (label), BB_END (jump_block));
1167 JUMP_LABEL (BB_END (jump_block)) = label;
1168 LABEL_NUSES (label)++;
1171 emit_barrier_after (BB_END (jump_block));
1172 redirect_edge_succ_nodup (e, target);
1174 if (abnormal_edge_flags)
1175 make_edge (src, target, abnormal_edge_flags);
1177 return new_bb;
1180 /* Edge E is assumed to be fallthru edge. Emit needed jump instruction
1181 (and possibly create new basic block) to make edge non-fallthru.
1182 Return newly created BB or NULL if none. */
1184 basic_block
1185 force_nonfallthru (edge e)
1187 return force_nonfallthru_and_redirect (e, e->dest);
1190 /* Redirect edge even at the expense of creating new jump insn or
1191 basic block. Return new basic block if created, NULL otherwise.
1192 Abort if conversion is impossible. */
1194 static basic_block
1195 rtl_redirect_edge_and_branch_force (edge e, basic_block target)
1197 if (redirect_edge_and_branch (e, target)
1198 || e->dest == target)
1199 return NULL;
1201 /* In case the edge redirection failed, try to force it to be non-fallthru
1202 and redirect newly created simplejump. */
1203 return force_nonfallthru_and_redirect (e, target);
1206 /* The given edge should potentially be a fallthru edge. If that is in
1207 fact true, delete the jump and barriers that are in the way. */
1209 static void
1210 rtl_tidy_fallthru_edge (edge e)
1212 rtx q;
1213 basic_block b = e->src, c = b->next_bb;
1214 edge e2;
1215 unsigned ix;
1217 for (ix = 0; ix < EDGE_COUNT (b->succs); ix++)
1219 e2 = EDGE_I (b->succs, ix);
1220 if (e == e2)
1221 break;
1224 /* ??? In a late-running flow pass, other folks may have deleted basic
1225 blocks by nopping out blocks, leaving multiple BARRIERs between here
1226 and the target label. They ought to be chastized and fixed.
1228 We can also wind up with a sequence of undeletable labels between
1229 one block and the next.
1231 So search through a sequence of barriers, labels, and notes for
1232 the head of block C and assert that we really do fall through. */
1234 for (q = NEXT_INSN (BB_END (b)); q != BB_HEAD (c); q = NEXT_INSN (q))
1235 if (INSN_P (q))
1236 return;
1238 /* Remove what will soon cease being the jump insn from the source block.
1239 If block B consisted only of this single jump, turn it into a deleted
1240 note. */
1241 q = BB_END (b);
1242 if (JUMP_P (q)
1243 && onlyjump_p (q)
1244 && (any_uncondjump_p (q)
1245 /* FIXME: correct? */
1246 || (EDGE_SUCC (b, 0) == e && ix == EDGE_COUNT (b->succs) - 1)))
1248 #ifdef HAVE_cc0
1249 /* If this was a conditional jump, we need to also delete
1250 the insn that set cc0. */
1251 if (any_condjump_p (q) && only_sets_cc0_p (PREV_INSN (q)))
1252 q = PREV_INSN (q);
1253 #endif
1255 q = PREV_INSN (q);
1257 /* We don't want a block to end on a line-number note since that has
1258 the potential of changing the code between -g and not -g. */
1259 while (NOTE_P (q) && NOTE_LINE_NUMBER (q) >= 0)
1260 q = PREV_INSN (q);
1263 /* Selectively unlink the sequence. */
1264 if (q != PREV_INSN (BB_HEAD (c)))
1265 delete_insn_chain (NEXT_INSN (q), PREV_INSN (BB_HEAD (c)));
1267 e->flags |= EDGE_FALLTHRU;
1270 /* Helper function for split_edge. Return true in case edge BB2 to BB1
1271 is back edge of syntactic loop. */
1273 static bool
1274 back_edge_of_syntactic_loop_p (basic_block bb1, basic_block bb2)
1276 rtx insn;
1277 int count = 0;
1278 basic_block bb;
1280 if (bb1 == bb2)
1281 return true;
1283 /* ??? Could we guarantee that bb indices are monotone, so that we could
1284 just compare them? */
1285 for (bb = bb1; bb && bb != bb2; bb = bb->next_bb)
1286 continue;
1288 if (!bb)
1289 return false;
1291 for (insn = BB_END (bb1); insn != BB_HEAD (bb2) && count >= 0;
1292 insn = NEXT_INSN (insn))
1293 if (NOTE_P (insn))
1295 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
1296 count++;
1297 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
1298 count--;
1301 return count >= 0;
1304 /* Should move basic block BB after basic block AFTER. NIY. */
1306 static bool
1307 rtl_move_block_after (basic_block bb ATTRIBUTE_UNUSED,
1308 basic_block after ATTRIBUTE_UNUSED)
1310 return false;
1313 /* Split a (typically critical) edge. Return the new block.
1314 Abort on abnormal edges.
1316 ??? The code generally expects to be called on critical edges.
1317 The case of a block ending in an unconditional jump to a
1318 block with multiple predecessors is not handled optimally. */
1320 static basic_block
1321 rtl_split_edge (edge edge_in)
1323 basic_block bb;
1324 rtx before;
1326 /* Abnormal edges cannot be split. */
1327 gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
1329 /* We are going to place the new block in front of edge destination.
1330 Avoid existence of fallthru predecessors. */
1331 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1333 edge e;
1334 edge_iterator ei;
1336 FOR_EACH_EDGE (e, ei, edge_in->dest->preds)
1338 if (e->flags & EDGE_FALLTHRU)
1339 break;
1342 if (e)
1343 force_nonfallthru (e);
1346 /* Create the basic block note.
1348 Where we place the note can have a noticeable impact on the generated
1349 code. Consider this cfg:
1355 +->1-->2--->E
1357 +--+
1359 If we need to insert an insn on the edge from block 0 to block 1,
1360 we want to ensure the instructions we insert are outside of any
1361 loop notes that physically sit between block 0 and block 1. Otherwise
1362 we confuse the loop optimizer into thinking the loop is a phony. */
1364 if (edge_in->dest != EXIT_BLOCK_PTR
1365 && PREV_INSN (BB_HEAD (edge_in->dest))
1366 && NOTE_P (PREV_INSN (BB_HEAD (edge_in->dest)))
1367 && (NOTE_LINE_NUMBER (PREV_INSN (BB_HEAD (edge_in->dest)))
1368 == NOTE_INSN_LOOP_BEG)
1369 && !back_edge_of_syntactic_loop_p (edge_in->dest, edge_in->src))
1370 before = PREV_INSN (BB_HEAD (edge_in->dest));
1371 else if (edge_in->dest != EXIT_BLOCK_PTR)
1372 before = BB_HEAD (edge_in->dest);
1373 else
1374 before = NULL_RTX;
1376 /* If this is a fall through edge to the exit block, the blocks might be
1377 not adjacent, and the right place is the after the source. */
1378 if (edge_in->flags & EDGE_FALLTHRU && edge_in->dest == EXIT_BLOCK_PTR)
1380 before = NEXT_INSN (BB_END (edge_in->src));
1381 if (before
1382 && NOTE_P (before)
1383 && NOTE_LINE_NUMBER (before) == NOTE_INSN_LOOP_END)
1384 before = NEXT_INSN (before);
1385 bb = create_basic_block (before, NULL, edge_in->src);
1386 BB_COPY_PARTITION (bb, edge_in->src);
1388 else
1390 bb = create_basic_block (before, NULL, edge_in->dest->prev_bb);
1391 /* ??? Why not edge_in->dest->prev_bb here? */
1392 BB_COPY_PARTITION (bb, edge_in->dest);
1395 /* ??? This info is likely going to be out of date very soon. */
1396 if (edge_in->dest->global_live_at_start)
1398 bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1399 bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1400 COPY_REG_SET (bb->global_live_at_start,
1401 edge_in->dest->global_live_at_start);
1402 COPY_REG_SET (bb->global_live_at_end,
1403 edge_in->dest->global_live_at_start);
1406 make_single_succ_edge (bb, edge_in->dest, EDGE_FALLTHRU);
1408 /* For non-fallthru edges, we must adjust the predecessor's
1409 jump instruction to target our new block. */
1410 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1412 edge redirected = redirect_edge_and_branch (edge_in, bb);
1413 gcc_assert (redirected);
1415 else
1416 redirect_edge_succ (edge_in, bb);
1418 return bb;
1421 /* Queue instructions for insertion on an edge between two basic blocks.
1422 The new instructions and basic blocks (if any) will not appear in the
1423 CFG until commit_edge_insertions is called. */
1425 void
1426 insert_insn_on_edge (rtx pattern, edge e)
1428 /* We cannot insert instructions on an abnormal critical edge.
1429 It will be easier to find the culprit if we die now. */
1430 gcc_assert (!((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e)));
1432 if (e->insns.r == NULL_RTX)
1433 start_sequence ();
1434 else
1435 push_to_sequence (e->insns.r);
1437 emit_insn (pattern);
1439 e->insns.r = get_insns ();
1440 end_sequence ();
1443 /* Called from safe_insert_insn_on_edge through note_stores, marks live
1444 registers that are killed by the store. */
1445 static void
1446 mark_killed_regs (rtx reg, rtx set ATTRIBUTE_UNUSED, void *data)
1448 regset killed = data;
1449 int regno, i;
1451 if (GET_CODE (reg) == SUBREG)
1452 reg = SUBREG_REG (reg);
1453 if (!REG_P (reg))
1454 return;
1455 regno = REGNO (reg);
1456 if (regno >= FIRST_PSEUDO_REGISTER)
1457 SET_REGNO_REG_SET (killed, regno);
1458 else
1460 for (i = 0; i < (int) hard_regno_nregs[regno][GET_MODE (reg)]; i++)
1461 SET_REGNO_REG_SET (killed, regno + i);
1465 /* Similar to insert_insn_on_edge, tries to put INSN to edge E. Additionally
1466 it checks whether this will not clobber the registers that are live on the
1467 edge (i.e. it requires liveness information to be up-to-date) and if there
1468 are some, then it tries to save and restore them. Returns true if
1469 successful. */
1470 bool
1471 safe_insert_insn_on_edge (rtx insn, edge e)
1473 rtx x;
1474 regset_head killed_head;
1475 regset killed = INITIALIZE_REG_SET (killed_head);
1476 rtx save_regs = NULL_RTX;
1477 int regno, noccmode;
1478 enum machine_mode mode;
1480 #ifdef AVOID_CCMODE_COPIES
1481 noccmode = true;
1482 #else
1483 noccmode = false;
1484 #endif
1486 for (x = insn; x; x = NEXT_INSN (x))
1487 if (INSN_P (x))
1488 note_stores (PATTERN (x), mark_killed_regs, killed);
1489 bitmap_operation (killed, killed, e->dest->global_live_at_start,
1490 BITMAP_AND);
1492 EXECUTE_IF_SET_IN_REG_SET (killed, 0, regno,
1494 mode = regno < FIRST_PSEUDO_REGISTER
1495 ? reg_raw_mode[regno]
1496 : GET_MODE (regno_reg_rtx[regno]);
1497 if (mode == VOIDmode)
1498 return false;
1500 if (noccmode && mode == CCmode)
1501 return false;
1503 save_regs = alloc_EXPR_LIST (0,
1504 alloc_EXPR_LIST (0,
1505 gen_reg_rtx (mode),
1506 gen_raw_REG (mode, regno)),
1507 save_regs);
1510 if (save_regs)
1512 rtx from, to;
1514 start_sequence ();
1515 for (x = save_regs; x; x = XEXP (x, 1))
1517 from = XEXP (XEXP (x, 0), 1);
1518 to = XEXP (XEXP (x, 0), 0);
1519 emit_move_insn (to, from);
1521 emit_insn (insn);
1522 for (x = save_regs; x; x = XEXP (x, 1))
1524 from = XEXP (XEXP (x, 0), 0);
1525 to = XEXP (XEXP (x, 0), 1);
1526 emit_move_insn (to, from);
1528 insn = get_insns ();
1529 end_sequence ();
1530 free_EXPR_LIST_list (&save_regs);
1532 insert_insn_on_edge (insn, e);
1534 FREE_REG_SET (killed);
1535 return true;
1538 /* Update the CFG for the instructions queued on edge E. */
1540 static void
1541 commit_one_edge_insertion (edge e, int watch_calls)
1543 rtx before = NULL_RTX, after = NULL_RTX, insns, tmp, last;
1544 basic_block bb = NULL;
1546 /* Pull the insns off the edge now since the edge might go away. */
1547 insns = e->insns.r;
1548 e->insns.r = NULL_RTX;
1550 /* Special case -- avoid inserting code between call and storing
1551 its return value. */
1552 if (watch_calls && (e->flags & EDGE_FALLTHRU)
1553 && EDGE_COUNT (e->dest->preds) == 1
1554 && e->src != ENTRY_BLOCK_PTR
1555 && CALL_P (BB_END (e->src)))
1557 rtx next = next_nonnote_insn (BB_END (e->src));
1559 after = BB_HEAD (e->dest);
1560 /* The first insn after the call may be a stack pop, skip it. */
1561 while (next
1562 && keep_with_call_p (next))
1564 after = next;
1565 next = next_nonnote_insn (next);
1567 bb = e->dest;
1569 if (!before && !after)
1571 /* Figure out where to put these things. If the destination has
1572 one predecessor, insert there. Except for the exit block. */
1573 if (EDGE_COUNT (e->dest->preds) == 1 && e->dest != EXIT_BLOCK_PTR)
1575 bb = e->dest;
1577 /* Get the location correct wrt a code label, and "nice" wrt
1578 a basic block note, and before everything else. */
1579 tmp = BB_HEAD (bb);
1580 if (LABEL_P (tmp))
1581 tmp = NEXT_INSN (tmp);
1582 if (NOTE_INSN_BASIC_BLOCK_P (tmp))
1583 tmp = NEXT_INSN (tmp);
1584 if (tmp
1585 && NOTE_P (tmp)
1586 && NOTE_LINE_NUMBER (tmp) == NOTE_INSN_UNLIKELY_EXECUTED_CODE)
1587 tmp = NEXT_INSN (tmp);
1588 if (tmp == BB_HEAD (bb))
1589 before = tmp;
1590 else if (tmp)
1591 after = PREV_INSN (tmp);
1592 else
1593 after = get_last_insn ();
1596 /* If the source has one successor and the edge is not abnormal,
1597 insert there. Except for the entry block. */
1598 else if ((e->flags & EDGE_ABNORMAL) == 0
1599 && EDGE_COUNT (e->src->succs) == 1
1600 && e->src != ENTRY_BLOCK_PTR)
1602 bb = e->src;
1604 /* It is possible to have a non-simple jump here. Consider a target
1605 where some forms of unconditional jumps clobber a register. This
1606 happens on the fr30 for example.
1608 We know this block has a single successor, so we can just emit
1609 the queued insns before the jump. */
1610 if (JUMP_P (BB_END (bb)))
1611 for (before = BB_END (bb);
1612 NOTE_P (PREV_INSN (before))
1613 && NOTE_LINE_NUMBER (PREV_INSN (before)) ==
1614 NOTE_INSN_LOOP_BEG; before = PREV_INSN (before))
1616 else
1618 /* We'd better be fallthru, or we've lost track of
1619 what's what. */
1620 gcc_assert (e->flags & EDGE_FALLTHRU);
1622 after = BB_END (bb);
1625 /* Otherwise we must split the edge. */
1626 else
1628 bb = split_edge (e);
1629 after = BB_END (bb);
1631 if (flag_reorder_blocks_and_partition
1632 && targetm.have_named_sections
1633 && e->src != ENTRY_BLOCK_PTR
1634 && BB_PARTITION (e->src) == BB_COLD_PARTITION
1635 && !(e->flags & EDGE_CROSSING))
1637 rtx bb_note, new_note, cur_insn;
1639 bb_note = NULL_RTX;
1640 for (cur_insn = BB_HEAD (bb); cur_insn != NEXT_INSN (BB_END (bb));
1641 cur_insn = NEXT_INSN (cur_insn))
1642 if (NOTE_P (cur_insn)
1643 && NOTE_LINE_NUMBER (cur_insn) == NOTE_INSN_BASIC_BLOCK)
1645 bb_note = cur_insn;
1646 break;
1649 new_note = emit_note_after (NOTE_INSN_UNLIKELY_EXECUTED_CODE,
1650 bb_note);
1651 NOTE_BASIC_BLOCK (new_note) = bb;
1652 if (JUMP_P (BB_END (bb))
1653 && !any_condjump_p (BB_END (bb))
1654 && (EDGE_SUCC (bb, 0)->flags & EDGE_CROSSING))
1655 REG_NOTES (BB_END (bb)) = gen_rtx_EXPR_LIST
1656 (REG_CROSSING_JUMP, NULL_RTX, REG_NOTES (BB_END (bb)));
1657 if (after == bb_note)
1658 after = new_note;
1663 /* Now that we've found the spot, do the insertion. */
1665 if (before)
1667 emit_insn_before (insns, before);
1668 last = prev_nonnote_insn (before);
1670 else
1671 last = emit_insn_after (insns, after);
1673 if (returnjump_p (last))
1675 /* ??? Remove all outgoing edges from BB and add one for EXIT.
1676 This is not currently a problem because this only happens
1677 for the (single) epilogue, which already has a fallthru edge
1678 to EXIT. */
1680 e = EDGE_SUCC (bb, 0);
1681 gcc_assert (e->dest == EXIT_BLOCK_PTR
1682 && EDGE_COUNT (bb->succs) == 1 && (e->flags & EDGE_FALLTHRU));
1684 e->flags &= ~EDGE_FALLTHRU;
1685 emit_barrier_after (last);
1687 if (before)
1688 delete_insn (before);
1690 else
1691 gcc_assert (!JUMP_P (last));
1693 /* Mark the basic block for find_sub_basic_blocks. */
1694 bb->aux = &bb->aux;
1697 /* Update the CFG for all queued instructions. */
1699 void
1700 commit_edge_insertions (void)
1702 basic_block bb;
1703 sbitmap blocks;
1704 bool changed = false;
1706 #ifdef ENABLE_CHECKING
1707 verify_flow_info ();
1708 #endif
1710 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
1712 edge e;
1713 edge_iterator ei;
1715 FOR_EACH_EDGE (e, ei, bb->succs)
1717 if (e->insns.r)
1719 changed = true;
1720 commit_one_edge_insertion (e, false);
1725 if (!changed)
1726 return;
1728 blocks = sbitmap_alloc (last_basic_block);
1729 sbitmap_zero (blocks);
1730 FOR_EACH_BB (bb)
1731 if (bb->aux)
1733 SET_BIT (blocks, bb->index);
1734 /* Check for forgotten bb->aux values before commit_edge_insertions
1735 call. */
1736 gcc_assert (bb->aux == &bb->aux);
1737 bb->aux = NULL;
1739 find_many_sub_basic_blocks (blocks);
1740 sbitmap_free (blocks);
1743 /* Update the CFG for all queued instructions, taking special care of inserting
1744 code on edges between call and storing its return value. */
1746 void
1747 commit_edge_insertions_watch_calls (void)
1749 basic_block bb;
1750 sbitmap blocks;
1751 bool changed = false;
1753 #ifdef ENABLE_CHECKING
1754 verify_flow_info ();
1755 #endif
1757 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
1759 edge e;
1760 edge_iterator ei;
1762 FOR_EACH_EDGE (e, ei, bb->succs)
1764 if (e->insns.r)
1766 changed = true;
1767 commit_one_edge_insertion (e, true);
1772 if (!changed)
1773 return;
1775 blocks = sbitmap_alloc (last_basic_block);
1776 sbitmap_zero (blocks);
1777 FOR_EACH_BB (bb)
1778 if (bb->aux)
1780 SET_BIT (blocks, bb->index);
1781 /* Check for forgotten bb->aux values before commit_edge_insertions
1782 call. */
1783 gcc_assert (bb->aux == &bb->aux);
1784 bb->aux = NULL;
1786 find_many_sub_basic_blocks (blocks);
1787 sbitmap_free (blocks);
1790 /* Print out RTL-specific basic block information (live information
1791 at start and end). */
1793 static void
1794 rtl_dump_bb (basic_block bb, FILE *outf, int indent)
1796 rtx insn;
1797 rtx last;
1798 char *s_indent;
1800 s_indent = alloca ((size_t) indent + 1);
1801 memset (s_indent, ' ', (size_t) indent);
1802 s_indent[indent] = '\0';
1804 fprintf (outf, ";;%s Registers live at start: ", s_indent);
1805 dump_regset (bb->global_live_at_start, outf);
1806 putc ('\n', outf);
1808 for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb)); insn != last;
1809 insn = NEXT_INSN (insn))
1810 print_rtl_single (outf, insn);
1812 fprintf (outf, ";;%s Registers live at end: ", s_indent);
1813 dump_regset (bb->global_live_at_end, outf);
1814 putc ('\n', outf);
1817 /* Like print_rtl, but also print out live information for the start of each
1818 basic block. */
1820 void
1821 print_rtl_with_bb (FILE *outf, rtx rtx_first)
1823 rtx tmp_rtx;
1825 if (rtx_first == 0)
1826 fprintf (outf, "(nil)\n");
1827 else
1829 enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
1830 int max_uid = get_max_uid ();
1831 basic_block *start = xcalloc (max_uid, sizeof (basic_block));
1832 basic_block *end = xcalloc (max_uid, sizeof (basic_block));
1833 enum bb_state *in_bb_p = xcalloc (max_uid, sizeof (enum bb_state));
1835 basic_block bb;
1837 FOR_EACH_BB_REVERSE (bb)
1839 rtx x;
1841 start[INSN_UID (BB_HEAD (bb))] = bb;
1842 end[INSN_UID (BB_END (bb))] = bb;
1843 for (x = BB_HEAD (bb); x != NULL_RTX; x = NEXT_INSN (x))
1845 enum bb_state state = IN_MULTIPLE_BB;
1847 if (in_bb_p[INSN_UID (x)] == NOT_IN_BB)
1848 state = IN_ONE_BB;
1849 in_bb_p[INSN_UID (x)] = state;
1851 if (x == BB_END (bb))
1852 break;
1856 for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
1858 int did_output;
1860 if ((bb = start[INSN_UID (tmp_rtx)]) != NULL)
1862 fprintf (outf, ";; Start of basic block %d, registers live:",
1863 bb->index);
1864 dump_regset (bb->global_live_at_start, outf);
1865 putc ('\n', outf);
1868 if (in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB
1869 && !NOTE_P (tmp_rtx)
1870 && !BARRIER_P (tmp_rtx))
1871 fprintf (outf, ";; Insn is not within a basic block\n");
1872 else if (in_bb_p[INSN_UID (tmp_rtx)] == IN_MULTIPLE_BB)
1873 fprintf (outf, ";; Insn is in multiple basic blocks\n");
1875 did_output = print_rtl_single (outf, tmp_rtx);
1877 if ((bb = end[INSN_UID (tmp_rtx)]) != NULL)
1879 fprintf (outf, ";; End of basic block %d, registers live:\n",
1880 bb->index);
1881 dump_regset (bb->global_live_at_end, outf);
1882 putc ('\n', outf);
1885 if (did_output)
1886 putc ('\n', outf);
1889 free (start);
1890 free (end);
1891 free (in_bb_p);
1894 if (current_function_epilogue_delay_list != 0)
1896 fprintf (outf, "\n;; Insns in epilogue delay list:\n\n");
1897 for (tmp_rtx = current_function_epilogue_delay_list; tmp_rtx != 0;
1898 tmp_rtx = XEXP (tmp_rtx, 1))
1899 print_rtl_single (outf, XEXP (tmp_rtx, 0));
1903 void
1904 update_br_prob_note (basic_block bb)
1906 rtx note;
1907 if (!JUMP_P (BB_END (bb)))
1908 return;
1909 note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX);
1910 if (!note || INTVAL (XEXP (note, 0)) == BRANCH_EDGE (bb)->probability)
1911 return;
1912 XEXP (note, 0) = GEN_INT (BRANCH_EDGE (bb)->probability);
1915 /* Verify the CFG and RTL consistency common for both underlying RTL and
1916 cfglayout RTL.
1918 Currently it does following checks:
1920 - test head/end pointers
1921 - overlapping of basic blocks
1922 - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note)
1923 - tails of basic blocks (ensure that boundary is necessary)
1924 - scans body of the basic block for JUMP_INSN, CODE_LABEL
1925 and NOTE_INSN_BASIC_BLOCK
1926 - verify that no fall_thru edge crosses hot/cold partition boundaries
1928 In future it can be extended check a lot of other stuff as well
1929 (reachability of basic blocks, life information, etc. etc.). */
1931 static int
1932 rtl_verify_flow_info_1 (void)
1934 const int max_uid = get_max_uid ();
1935 rtx last_head = get_last_insn ();
1936 basic_block *bb_info;
1937 rtx x;
1938 int err = 0;
1939 basic_block bb, last_bb_seen;
1941 bb_info = xcalloc (max_uid, sizeof (basic_block));
1943 /* Check bb chain & numbers. */
1944 last_bb_seen = ENTRY_BLOCK_PTR;
1946 FOR_EACH_BB_REVERSE (bb)
1948 rtx head = BB_HEAD (bb);
1949 rtx end = BB_END (bb);
1951 /* Verify the end of the basic block is in the INSN chain. */
1952 for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
1953 if (x == end)
1954 break;
1956 if (!x)
1958 error ("end insn %d for block %d not found in the insn stream",
1959 INSN_UID (end), bb->index);
1960 err = 1;
1963 /* Work backwards from the end to the head of the basic block
1964 to verify the head is in the RTL chain. */
1965 for (; x != NULL_RTX; x = PREV_INSN (x))
1967 /* While walking over the insn chain, verify insns appear
1968 in only one basic block and initialize the BB_INFO array
1969 used by other passes. */
1970 if (bb_info[INSN_UID (x)] != NULL)
1972 error ("insn %d is in multiple basic blocks (%d and %d)",
1973 INSN_UID (x), bb->index, bb_info[INSN_UID (x)]->index);
1974 err = 1;
1977 bb_info[INSN_UID (x)] = bb;
1979 if (x == head)
1980 break;
1982 if (!x)
1984 error ("head insn %d for block %d not found in the insn stream",
1985 INSN_UID (head), bb->index);
1986 err = 1;
1989 last_head = x;
1992 /* Now check the basic blocks (boundaries etc.) */
1993 FOR_EACH_BB_REVERSE (bb)
1995 int n_fallthru = 0, n_eh = 0, n_call = 0, n_abnormal = 0, n_branch = 0;
1996 edge e, fallthru = NULL;
1997 rtx note;
1998 edge_iterator ei;
2000 if (INSN_P (BB_END (bb))
2001 && (note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX))
2002 && EDGE_COUNT (bb->succs) >= 2
2003 && any_condjump_p (BB_END (bb)))
2005 if (INTVAL (XEXP (note, 0)) != BRANCH_EDGE (bb)->probability)
2007 error ("verify_flow_info: REG_BR_PROB does not match cfg %wi %i",
2008 INTVAL (XEXP (note, 0)), BRANCH_EDGE (bb)->probability);
2009 err = 1;
2012 FOR_EACH_EDGE (e, ei, bb->succs)
2014 if (e->flags & EDGE_FALLTHRU)
2016 n_fallthru++, fallthru = e;
2017 if ((e->flags & EDGE_CROSSING)
2018 || (BB_PARTITION (e->src) != BB_PARTITION (e->dest)
2019 && e->src != ENTRY_BLOCK_PTR
2020 && e->dest != EXIT_BLOCK_PTR))
2022 error ("Fallthru edge crosses section boundary (bb %i)",
2023 e->src->index);
2024 err = 1;
2028 if ((e->flags & ~(EDGE_DFS_BACK
2029 | EDGE_CAN_FALLTHRU
2030 | EDGE_IRREDUCIBLE_LOOP
2031 | EDGE_LOOP_EXIT
2032 | EDGE_CROSSING)) == 0)
2033 n_branch++;
2035 if (e->flags & EDGE_ABNORMAL_CALL)
2036 n_call++;
2038 if (e->flags & EDGE_EH)
2039 n_eh++;
2040 else if (e->flags & EDGE_ABNORMAL)
2041 n_abnormal++;
2044 if (n_eh && GET_CODE (PATTERN (BB_END (bb))) != RESX
2045 && !find_reg_note (BB_END (bb), REG_EH_REGION, NULL_RTX))
2047 error ("Missing REG_EH_REGION note in the end of bb %i", bb->index);
2048 err = 1;
2050 if (n_branch
2051 && (!JUMP_P (BB_END (bb))
2052 || (n_branch > 1 && (any_uncondjump_p (BB_END (bb))
2053 || any_condjump_p (BB_END (bb))))))
2055 error ("Too many outgoing branch edges from bb %i", bb->index);
2056 err = 1;
2058 if (n_fallthru && any_uncondjump_p (BB_END (bb)))
2060 error ("Fallthru edge after unconditional jump %i", bb->index);
2061 err = 1;
2063 if (n_branch != 1 && any_uncondjump_p (BB_END (bb)))
2065 error ("Wrong amount of branch edges after unconditional jump %i", bb->index);
2066 err = 1;
2068 if (n_branch != 1 && any_condjump_p (BB_END (bb))
2069 && JUMP_LABEL (BB_END (bb)) != BB_HEAD (fallthru->dest))
2071 error ("Wrong amount of branch edges after conditional jump %i", bb->index);
2072 err = 1;
2074 if (n_call && !CALL_P (BB_END (bb)))
2076 error ("Call edges for non-call insn in bb %i", bb->index);
2077 err = 1;
2079 if (n_abnormal
2080 && (!CALL_P (BB_END (bb)) && n_call != n_abnormal)
2081 && (!JUMP_P (BB_END (bb))
2082 || any_condjump_p (BB_END (bb))
2083 || any_uncondjump_p (BB_END (bb))))
2085 error ("Abnormal edges for no purpose in bb %i", bb->index);
2086 err = 1;
2089 for (x = BB_HEAD (bb); x != NEXT_INSN (BB_END (bb)); x = NEXT_INSN (x))
2090 if (BLOCK_FOR_INSN (x) != bb)
2092 debug_rtx (x);
2093 if (! BLOCK_FOR_INSN (x))
2094 error
2095 ("insn %d inside basic block %d but block_for_insn is NULL",
2096 INSN_UID (x), bb->index);
2097 else
2098 error
2099 ("insn %d inside basic block %d but block_for_insn is %i",
2100 INSN_UID (x), bb->index, BLOCK_FOR_INSN (x)->index);
2102 err = 1;
2105 /* OK pointers are correct. Now check the header of basic
2106 block. It ought to contain optional CODE_LABEL followed
2107 by NOTE_BASIC_BLOCK. */
2108 x = BB_HEAD (bb);
2109 if (LABEL_P (x))
2111 if (BB_END (bb) == x)
2113 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
2114 bb->index);
2115 err = 1;
2118 x = NEXT_INSN (x);
2121 if (!NOTE_INSN_BASIC_BLOCK_P (x) || NOTE_BASIC_BLOCK (x) != bb)
2123 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
2124 bb->index);
2125 err = 1;
2128 if (BB_END (bb) == x)
2129 /* Do checks for empty blocks her. e */
2131 else
2132 for (x = NEXT_INSN (x); x; x = NEXT_INSN (x))
2134 if (NOTE_INSN_BASIC_BLOCK_P (x))
2136 error ("NOTE_INSN_BASIC_BLOCK %d in middle of basic block %d",
2137 INSN_UID (x), bb->index);
2138 err = 1;
2141 if (x == BB_END (bb))
2142 break;
2144 if (control_flow_insn_p (x))
2146 error ("in basic block %d:", bb->index);
2147 fatal_insn ("flow control insn inside a basic block", x);
2152 /* Clean up. */
2153 free (bb_info);
2154 return err;
2157 /* Verify the CFG and RTL consistency common for both underlying RTL and
2158 cfglayout RTL.
2160 Currently it does following checks:
2161 - all checks of rtl_verify_flow_info_1
2162 - check that all insns are in the basic blocks
2163 (except the switch handling code, barriers and notes)
2164 - check that all returns are followed by barriers
2165 - check that all fallthru edge points to the adjacent blocks. */
2166 static int
2167 rtl_verify_flow_info (void)
2169 basic_block bb;
2170 int err = rtl_verify_flow_info_1 ();
2171 rtx x;
2172 int num_bb_notes;
2173 const rtx rtx_first = get_insns ();
2174 basic_block last_bb_seen = ENTRY_BLOCK_PTR, curr_bb = NULL;
2176 FOR_EACH_BB_REVERSE (bb)
2178 edge e;
2179 edge_iterator ei;
2181 FOR_EACH_EDGE (e, ei, bb->succs)
2183 if (e->flags & EDGE_FALLTHRU)
2184 break;
2186 if (!e)
2188 rtx insn;
2190 /* Ensure existence of barrier in BB with no fallthru edges. */
2191 for (insn = BB_END (bb); !insn || !BARRIER_P (insn);
2192 insn = NEXT_INSN (insn))
2193 if (!insn
2194 || (NOTE_P (insn)
2195 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_BASIC_BLOCK))
2197 error ("missing barrier after block %i", bb->index);
2198 err = 1;
2199 break;
2202 else if (e->src != ENTRY_BLOCK_PTR
2203 && e->dest != EXIT_BLOCK_PTR)
2205 rtx insn;
2207 if (e->src->next_bb != e->dest)
2209 error
2210 ("verify_flow_info: Incorrect blocks for fallthru %i->%i",
2211 e->src->index, e->dest->index);
2212 err = 1;
2214 else
2215 for (insn = NEXT_INSN (BB_END (e->src)); insn != BB_HEAD (e->dest);
2216 insn = NEXT_INSN (insn))
2217 if (BARRIER_P (insn)
2218 #ifndef CASE_DROPS_THROUGH
2219 || INSN_P (insn)
2220 #else
2221 || (INSN_P (insn) && ! JUMP_TABLE_DATA_P (insn))
2222 #endif
2225 error ("verify_flow_info: Incorrect fallthru %i->%i",
2226 e->src->index, e->dest->index);
2227 fatal_insn ("wrong insn in the fallthru edge", insn);
2228 err = 1;
2233 num_bb_notes = 0;
2234 last_bb_seen = ENTRY_BLOCK_PTR;
2236 for (x = rtx_first; x; x = NEXT_INSN (x))
2238 if (NOTE_INSN_BASIC_BLOCK_P (x))
2240 bb = NOTE_BASIC_BLOCK (x);
2242 num_bb_notes++;
2243 if (bb != last_bb_seen->next_bb)
2244 internal_error ("basic blocks not laid down consecutively");
2246 curr_bb = last_bb_seen = bb;
2249 if (!curr_bb)
2251 switch (GET_CODE (x))
2253 case BARRIER:
2254 case NOTE:
2255 break;
2257 case CODE_LABEL:
2258 /* An addr_vec is placed outside any basic block. */
2259 if (NEXT_INSN (x)
2260 && JUMP_P (NEXT_INSN (x))
2261 && (GET_CODE (PATTERN (NEXT_INSN (x))) == ADDR_DIFF_VEC
2262 || GET_CODE (PATTERN (NEXT_INSN (x))) == ADDR_VEC))
2263 x = NEXT_INSN (x);
2265 /* But in any case, non-deletable labels can appear anywhere. */
2266 break;
2268 default:
2269 fatal_insn ("insn outside basic block", x);
2273 if (INSN_P (x)
2274 && JUMP_P (x)
2275 && returnjump_p (x) && ! condjump_p (x)
2276 && ! (NEXT_INSN (x) && BARRIER_P (NEXT_INSN (x))))
2277 fatal_insn ("return not followed by barrier", x);
2278 if (curr_bb && x == BB_END (curr_bb))
2279 curr_bb = NULL;
2282 if (num_bb_notes != n_basic_blocks)
2283 internal_error
2284 ("number of bb notes in insn chain (%d) != n_basic_blocks (%d)",
2285 num_bb_notes, n_basic_blocks);
2287 return err;
2290 /* Assume that the preceding pass has possibly eliminated jump instructions
2291 or converted the unconditional jumps. Eliminate the edges from CFG.
2292 Return true if any edges are eliminated. */
2294 bool
2295 purge_dead_edges (basic_block bb)
2297 edge e;
2298 unsigned ix;
2299 rtx insn = BB_END (bb), note;
2300 bool purged = false;
2301 bool found;
2302 edge_iterator ei;
2304 /* If this instruction cannot trap, remove REG_EH_REGION notes. */
2305 if (NONJUMP_INSN_P (insn)
2306 && (note = find_reg_note (insn, REG_EH_REGION, NULL)))
2308 rtx eqnote;
2310 if (! may_trap_p (PATTERN (insn))
2311 || ((eqnote = find_reg_equal_equiv_note (insn))
2312 && ! may_trap_p (XEXP (eqnote, 0))))
2313 remove_note (insn, note);
2316 /* Cleanup abnormal edges caused by exceptions or non-local gotos. */
2317 for (ix = 0; VEC_iterate (edge, bb->succs, ix, e); )
2319 if (e->flags & EDGE_EH)
2321 if (can_throw_internal (BB_END (bb)))
2323 ix++;
2324 continue;
2327 else if (e->flags & EDGE_ABNORMAL_CALL)
2329 if (CALL_P (BB_END (bb))
2330 && (! (note = find_reg_note (insn, REG_EH_REGION, NULL))
2331 || INTVAL (XEXP (note, 0)) >= 0))
2333 ix++;
2334 continue;
2337 else
2339 ix++;
2340 continue;
2343 remove_edge (e);
2344 bb->flags |= BB_DIRTY;
2345 purged = true;
2348 if (JUMP_P (insn))
2350 rtx note;
2351 edge b,f;
2352 unsigned ix;
2354 /* We do care only about conditional jumps and simplejumps. */
2355 if (!any_condjump_p (insn)
2356 && !returnjump_p (insn)
2357 && !simplejump_p (insn))
2358 return purged;
2360 /* Branch probability/prediction notes are defined only for
2361 condjumps. We've possibly turned condjump into simplejump. */
2362 if (simplejump_p (insn))
2364 note = find_reg_note (insn, REG_BR_PROB, NULL);
2365 if (note)
2366 remove_note (insn, note);
2367 while ((note = find_reg_note (insn, REG_BR_PRED, NULL)))
2368 remove_note (insn, note);
2371 for (ix = 0; VEC_iterate (edge, bb->succs, ix, e); )
2373 /* Avoid abnormal flags to leak from computed jumps turned
2374 into simplejumps. */
2376 e->flags &= ~EDGE_ABNORMAL;
2378 /* See if this edge is one we should keep. */
2379 if ((e->flags & EDGE_FALLTHRU) && any_condjump_p (insn))
2380 /* A conditional jump can fall through into the next
2381 block, so we should keep the edge. */
2383 ix++;
2384 continue;
2386 else if (e->dest != EXIT_BLOCK_PTR
2387 && BB_HEAD (e->dest) == JUMP_LABEL (insn))
2388 /* If the destination block is the target of the jump,
2389 keep the edge. */
2391 ix++;
2392 continue;
2394 else if (e->dest == EXIT_BLOCK_PTR && returnjump_p (insn))
2395 /* If the destination block is the exit block, and this
2396 instruction is a return, then keep the edge. */
2398 ix++;
2399 continue;
2401 else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
2402 /* Keep the edges that correspond to exceptions thrown by
2403 this instruction and rematerialize the EDGE_ABNORMAL
2404 flag we just cleared above. */
2406 e->flags |= EDGE_ABNORMAL;
2407 ix++;
2408 continue;
2411 /* We do not need this edge. */
2412 bb->flags |= BB_DIRTY;
2413 purged = true;
2414 remove_edge (e);
2417 if (EDGE_COUNT (bb->succs) == 0 || !purged)
2418 return purged;
2420 if (dump_file)
2421 fprintf (dump_file, "Purged edges from bb %i\n", bb->index);
2423 if (!optimize)
2424 return purged;
2426 /* Redistribute probabilities. */
2427 if (EDGE_COUNT (bb->succs) == 1)
2429 EDGE_SUCC (bb, 0)->probability = REG_BR_PROB_BASE;
2430 EDGE_SUCC (bb, 0)->count = bb->count;
2432 else
2434 note = find_reg_note (insn, REG_BR_PROB, NULL);
2435 if (!note)
2436 return purged;
2438 b = BRANCH_EDGE (bb);
2439 f = FALLTHRU_EDGE (bb);
2440 b->probability = INTVAL (XEXP (note, 0));
2441 f->probability = REG_BR_PROB_BASE - b->probability;
2442 b->count = bb->count * b->probability / REG_BR_PROB_BASE;
2443 f->count = bb->count * f->probability / REG_BR_PROB_BASE;
2446 return purged;
2448 else if (CALL_P (insn) && SIBLING_CALL_P (insn))
2450 /* First, there should not be any EH or ABCALL edges resulting
2451 from non-local gotos and the like. If there were, we shouldn't
2452 have created the sibcall in the first place. Second, there
2453 should of course never have been a fallthru edge. */
2454 gcc_assert (EDGE_COUNT (bb->succs) == 1);
2455 gcc_assert (EDGE_SUCC (bb, 0)->flags == (EDGE_SIBCALL | EDGE_ABNORMAL));
2457 return 0;
2460 /* If we don't see a jump insn, we don't know exactly why the block would
2461 have been broken at this point. Look for a simple, non-fallthru edge,
2462 as these are only created by conditional branches. If we find such an
2463 edge we know that there used to be a jump here and can then safely
2464 remove all non-fallthru edges. */
2465 found = false;
2466 FOR_EACH_EDGE (e, ei, bb->succs)
2468 if (! (e->flags & (EDGE_COMPLEX | EDGE_FALLTHRU)))
2470 found = true;
2471 break;
2475 if (!found)
2476 return purged;
2478 for (ix = 0; VEC_iterate (edge, bb->succs, ix, e); )
2480 if (!(e->flags & EDGE_FALLTHRU))
2482 bb->flags |= BB_DIRTY;
2483 remove_edge (e);
2484 purged = true;
2486 else
2487 ix++;
2490 gcc_assert (EDGE_COUNT (bb->succs) == 1);
2492 EDGE_SUCC (bb, 0)->probability = REG_BR_PROB_BASE;
2493 EDGE_SUCC (bb, 0)->count = bb->count;
2495 if (dump_file)
2496 fprintf (dump_file, "Purged non-fallthru edges from bb %i\n",
2497 bb->index);
2498 return purged;
2501 /* Search all basic blocks for potentially dead edges and purge them. Return
2502 true if some edge has been eliminated. */
2504 bool
2505 purge_all_dead_edges (int update_life_p)
2507 int purged = false;
2508 sbitmap blocks = 0;
2509 basic_block bb;
2511 if (update_life_p)
2513 blocks = sbitmap_alloc (last_basic_block);
2514 sbitmap_zero (blocks);
2517 FOR_EACH_BB (bb)
2519 bool purged_here = purge_dead_edges (bb);
2521 purged |= purged_here;
2522 if (purged_here && update_life_p)
2523 SET_BIT (blocks, bb->index);
2526 if (update_life_p && purged)
2527 update_life_info (blocks, UPDATE_LIFE_GLOBAL,
2528 PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
2529 | PROP_KILL_DEAD_CODE);
2531 if (update_life_p)
2532 sbitmap_free (blocks);
2533 return purged;
2536 /* Same as split_block but update cfg_layout structures. */
2538 static basic_block
2539 cfg_layout_split_block (basic_block bb, void *insnp)
2541 rtx insn = insnp;
2542 basic_block new_bb = rtl_split_block (bb, insn);
2544 new_bb->rbi->footer = bb->rbi->footer;
2545 bb->rbi->footer = NULL;
2547 return new_bb;
2551 /* Redirect Edge to DEST. */
2552 static edge
2553 cfg_layout_redirect_edge_and_branch (edge e, basic_block dest)
2555 basic_block src = e->src;
2556 edge ret;
2558 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
2559 return NULL;
2561 if (e->dest == dest)
2562 return e;
2564 if (e->src != ENTRY_BLOCK_PTR
2565 && (ret = try_redirect_by_replacing_jump (e, dest, true)))
2567 src->flags |= BB_DIRTY;
2568 return ret;
2571 if (e->src == ENTRY_BLOCK_PTR
2572 && (e->flags & EDGE_FALLTHRU) && !(e->flags & EDGE_COMPLEX))
2574 if (dump_file)
2575 fprintf (dump_file, "Redirecting entry edge from bb %i to %i\n",
2576 e->src->index, dest->index);
2578 e->src->flags |= BB_DIRTY;
2579 redirect_edge_succ (e, dest);
2580 return e;
2583 /* Redirect_edge_and_branch may decide to turn branch into fallthru edge
2584 in the case the basic block appears to be in sequence. Avoid this
2585 transformation. */
2587 if (e->flags & EDGE_FALLTHRU)
2589 /* Redirect any branch edges unified with the fallthru one. */
2590 if (JUMP_P (BB_END (src))
2591 && label_is_jump_target_p (BB_HEAD (e->dest),
2592 BB_END (src)))
2594 edge redirected;
2596 if (dump_file)
2597 fprintf (dump_file, "Fallthru edge unified with branch "
2598 "%i->%i redirected to %i\n",
2599 e->src->index, e->dest->index, dest->index);
2600 e->flags &= ~EDGE_FALLTHRU;
2601 redirected = redirect_branch_edge (e, dest);
2602 gcc_assert (redirected);
2603 e->flags |= EDGE_FALLTHRU;
2604 e->src->flags |= BB_DIRTY;
2605 return e;
2607 /* In case we are redirecting fallthru edge to the branch edge
2608 of conditional jump, remove it. */
2609 if (EDGE_COUNT (src->succs) == 2)
2611 bool found = false;
2612 unsigned ix = 0;
2613 edge tmp, s;
2614 edge_iterator ei;
2616 FOR_EACH_EDGE (tmp, ei, src->succs)
2618 if (e == tmp)
2620 found = true;
2621 /* FIXME: Don't access iterator directly. */
2622 ix = ei.index;
2623 break;
2627 if (!found)
2628 abort ();
2630 if (EDGE_COUNT (src->succs) > (ix + 1))
2631 s = EDGE_SUCC (src, ix + 1);
2632 else
2633 s = EDGE_SUCC (src, 0);
2635 if (s->dest == dest
2636 && any_condjump_p (BB_END (src))
2637 && onlyjump_p (BB_END (src)))
2638 delete_insn (BB_END (src));
2640 ret = redirect_edge_succ_nodup (e, dest);
2641 if (dump_file)
2642 fprintf (dump_file, "Fallthru edge %i->%i redirected to %i\n",
2643 e->src->index, e->dest->index, dest->index);
2645 else
2646 ret = redirect_branch_edge (e, dest);
2648 /* We don't want simplejumps in the insn stream during cfglayout. */
2649 gcc_assert (!simplejump_p (BB_END (src)));
2651 src->flags |= BB_DIRTY;
2652 return ret;
2655 /* Simple wrapper as we always can redirect fallthru edges. */
2656 static basic_block
2657 cfg_layout_redirect_edge_and_branch_force (edge e, basic_block dest)
2659 edge redirected = cfg_layout_redirect_edge_and_branch (e, dest);
2661 gcc_assert (redirected);
2662 return NULL;
2665 /* Same as delete_basic_block but update cfg_layout structures. */
2667 static void
2668 cfg_layout_delete_block (basic_block bb)
2670 rtx insn, next, prev = PREV_INSN (BB_HEAD (bb)), *to, remaints;
2672 if (bb->rbi->header)
2674 next = BB_HEAD (bb);
2675 if (prev)
2676 NEXT_INSN (prev) = bb->rbi->header;
2677 else
2678 set_first_insn (bb->rbi->header);
2679 PREV_INSN (bb->rbi->header) = prev;
2680 insn = bb->rbi->header;
2681 while (NEXT_INSN (insn))
2682 insn = NEXT_INSN (insn);
2683 NEXT_INSN (insn) = next;
2684 PREV_INSN (next) = insn;
2686 next = NEXT_INSN (BB_END (bb));
2687 if (bb->rbi->footer)
2689 insn = bb->rbi->footer;
2690 while (insn)
2692 if (BARRIER_P (insn))
2694 if (PREV_INSN (insn))
2695 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2696 else
2697 bb->rbi->footer = NEXT_INSN (insn);
2698 if (NEXT_INSN (insn))
2699 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2701 if (LABEL_P (insn))
2702 break;
2703 insn = NEXT_INSN (insn);
2705 if (bb->rbi->footer)
2707 insn = BB_END (bb);
2708 NEXT_INSN (insn) = bb->rbi->footer;
2709 PREV_INSN (bb->rbi->footer) = insn;
2710 while (NEXT_INSN (insn))
2711 insn = NEXT_INSN (insn);
2712 NEXT_INSN (insn) = next;
2713 if (next)
2714 PREV_INSN (next) = insn;
2715 else
2716 set_last_insn (insn);
2719 if (bb->next_bb != EXIT_BLOCK_PTR)
2720 to = &bb->next_bb->rbi->header;
2721 else
2722 to = &cfg_layout_function_footer;
2723 rtl_delete_block (bb);
2725 if (prev)
2726 prev = NEXT_INSN (prev);
2727 else
2728 prev = get_insns ();
2729 if (next)
2730 next = PREV_INSN (next);
2731 else
2732 next = get_last_insn ();
2734 if (next && NEXT_INSN (next) != prev)
2736 remaints = unlink_insn_chain (prev, next);
2737 insn = remaints;
2738 while (NEXT_INSN (insn))
2739 insn = NEXT_INSN (insn);
2740 NEXT_INSN (insn) = *to;
2741 if (*to)
2742 PREV_INSN (*to) = insn;
2743 *to = remaints;
2747 /* Return true when blocks A and B can be safely merged. */
2748 static bool
2749 cfg_layout_can_merge_blocks_p (basic_block a, basic_block b)
2751 /* If we are partitioning hot/cold basic blocks, we don't want to
2752 mess up unconditional or indirect jumps that cross between hot
2753 and cold sections.
2755 Basic block partitioning may result in some jumps that appear to
2756 be optimizable (or blocks that appear to be mergeable), but which really
2757 must be left untouched (they are required to make it safely across
2758 partition boundaries). See the comments at the top of
2759 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
2761 if (flag_reorder_blocks_and_partition
2762 && (find_reg_note (BB_END (a), REG_CROSSING_JUMP, NULL_RTX)
2763 || find_reg_note (BB_END (b), REG_CROSSING_JUMP, NULL_RTX)
2764 || BB_PARTITION (a) != BB_PARTITION (b)))
2765 return false;
2767 /* There must be exactly one edge in between the blocks. */
2768 return (EDGE_COUNT (a->succs) == 1
2769 && EDGE_SUCC (a, 0)->dest == b
2770 && EDGE_COUNT (b->preds) == 1
2771 && a != b
2772 /* Must be simple edge. */
2773 && !(EDGE_SUCC (a, 0)->flags & EDGE_COMPLEX)
2774 && a != ENTRY_BLOCK_PTR && b != EXIT_BLOCK_PTR
2775 /* If the jump insn has side effects,
2776 we can't kill the edge. */
2777 && (!JUMP_P (BB_END (a))
2778 || (reload_completed
2779 ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
2782 /* Merge block A and B, abort when it is not possible. */
2783 static void
2784 cfg_layout_merge_blocks (basic_block a, basic_block b)
2786 #ifdef ENABLE_CHECKING
2787 gcc_assert (cfg_layout_can_merge_blocks_p (a, b));
2788 #endif
2790 /* If there was a CODE_LABEL beginning B, delete it. */
2791 if (LABEL_P (BB_HEAD (b)))
2792 delete_insn (BB_HEAD (b));
2794 /* We should have fallthru edge in a, or we can do dummy redirection to get
2795 it cleaned up. */
2796 if (JUMP_P (BB_END (a)))
2797 try_redirect_by_replacing_jump (EDGE_SUCC (a, 0), b, true);
2798 gcc_assert (!JUMP_P (BB_END (a)));
2800 /* Possible line number notes should appear in between. */
2801 if (b->rbi->header)
2803 rtx first = BB_END (a), last;
2805 last = emit_insn_after (b->rbi->header, BB_END (a));
2806 delete_insn_chain (NEXT_INSN (first), last);
2807 b->rbi->header = NULL;
2810 /* In the case basic blocks are not adjacent, move them around. */
2811 if (NEXT_INSN (BB_END (a)) != BB_HEAD (b))
2813 rtx first = unlink_insn_chain (BB_HEAD (b), BB_END (b));
2815 emit_insn_after (first, BB_END (a));
2816 /* Skip possible DELETED_LABEL insn. */
2817 if (!NOTE_INSN_BASIC_BLOCK_P (first))
2818 first = NEXT_INSN (first);
2819 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (first));
2820 BB_HEAD (b) = NULL;
2821 delete_insn (first);
2823 /* Otherwise just re-associate the instructions. */
2824 else
2826 rtx insn;
2828 for (insn = BB_HEAD (b);
2829 insn != NEXT_INSN (BB_END (b));
2830 insn = NEXT_INSN (insn))
2831 set_block_for_insn (insn, a);
2832 insn = BB_HEAD (b);
2833 /* Skip possible DELETED_LABEL insn. */
2834 if (!NOTE_INSN_BASIC_BLOCK_P (insn))
2835 insn = NEXT_INSN (insn);
2836 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
2837 BB_HEAD (b) = NULL;
2838 BB_END (a) = BB_END (b);
2839 delete_insn (insn);
2842 /* Possible tablejumps and barriers should appear after the block. */
2843 if (b->rbi->footer)
2845 if (!a->rbi->footer)
2846 a->rbi->footer = b->rbi->footer;
2847 else
2849 rtx last = a->rbi->footer;
2851 while (NEXT_INSN (last))
2852 last = NEXT_INSN (last);
2853 NEXT_INSN (last) = b->rbi->footer;
2854 PREV_INSN (b->rbi->footer) = last;
2856 b->rbi->footer = NULL;
2859 if (dump_file)
2860 fprintf (dump_file, "Merged blocks %d and %d.\n",
2861 a->index, b->index);
2864 /* Split edge E. */
2866 static basic_block
2867 cfg_layout_split_edge (edge e)
2869 edge new_e;
2870 basic_block new_bb =
2871 create_basic_block (e->src != ENTRY_BLOCK_PTR
2872 ? NEXT_INSN (BB_END (e->src)) : get_insns (),
2873 NULL_RTX, e->src);
2875 /* ??? This info is likely going to be out of date very soon, but we must
2876 create it to avoid getting an ICE later. */
2877 if (e->dest->global_live_at_start)
2879 new_bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (&flow_obstack);
2880 new_bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (&flow_obstack);
2881 COPY_REG_SET (new_bb->global_live_at_start,
2882 e->dest->global_live_at_start);
2883 COPY_REG_SET (new_bb->global_live_at_end,
2884 e->dest->global_live_at_start);
2887 new_e = make_edge (new_bb, e->dest, EDGE_FALLTHRU);
2888 redirect_edge_and_branch_force (e, new_bb);
2890 return new_bb;
2893 /* Do postprocessing after making a forwarder block joined by edge FALLTHRU. */
2895 static void
2896 rtl_make_forwarder_block (edge fallthru ATTRIBUTE_UNUSED)
2900 /* Return 1 if BB ends with a call, possibly followed by some
2901 instructions that must stay with the call, 0 otherwise. */
2903 static bool
2904 rtl_block_ends_with_call_p (basic_block bb)
2906 rtx insn = BB_END (bb);
2908 while (!CALL_P (insn)
2909 && insn != BB_HEAD (bb)
2910 && keep_with_call_p (insn))
2911 insn = PREV_INSN (insn);
2912 return (CALL_P (insn));
2915 /* Return 1 if BB ends with a conditional branch, 0 otherwise. */
2917 static bool
2918 rtl_block_ends_with_condjump_p (basic_block bb)
2920 return any_condjump_p (BB_END (bb));
2923 /* Return true if we need to add fake edge to exit.
2924 Helper function for rtl_flow_call_edges_add. */
2926 static bool
2927 need_fake_edge_p (rtx insn)
2929 if (!INSN_P (insn))
2930 return false;
2932 if ((CALL_P (insn)
2933 && !SIBLING_CALL_P (insn)
2934 && !find_reg_note (insn, REG_NORETURN, NULL)
2935 && !find_reg_note (insn, REG_ALWAYS_RETURN, NULL)
2936 && !CONST_OR_PURE_CALL_P (insn)))
2937 return true;
2939 return ((GET_CODE (PATTERN (insn)) == ASM_OPERANDS
2940 && MEM_VOLATILE_P (PATTERN (insn)))
2941 || (GET_CODE (PATTERN (insn)) == PARALLEL
2942 && asm_noperands (insn) != -1
2943 && MEM_VOLATILE_P (XVECEXP (PATTERN (insn), 0, 0)))
2944 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
2947 /* Add fake edges to the function exit for any non constant and non noreturn
2948 calls, volatile inline assembly in the bitmap of blocks specified by
2949 BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
2950 that were split.
2952 The goal is to expose cases in which entering a basic block does not imply
2953 that all subsequent instructions must be executed. */
2955 static int
2956 rtl_flow_call_edges_add (sbitmap blocks)
2958 int i;
2959 int blocks_split = 0;
2960 int last_bb = last_basic_block;
2961 bool check_last_block = false;
2963 if (n_basic_blocks == 0)
2964 return 0;
2966 if (! blocks)
2967 check_last_block = true;
2968 else
2969 check_last_block = TEST_BIT (blocks, EXIT_BLOCK_PTR->prev_bb->index);
2971 /* In the last basic block, before epilogue generation, there will be
2972 a fallthru edge to EXIT. Special care is required if the last insn
2973 of the last basic block is a call because make_edge folds duplicate
2974 edges, which would result in the fallthru edge also being marked
2975 fake, which would result in the fallthru edge being removed by
2976 remove_fake_edges, which would result in an invalid CFG.
2978 Moreover, we can't elide the outgoing fake edge, since the block
2979 profiler needs to take this into account in order to solve the minimal
2980 spanning tree in the case that the call doesn't return.
2982 Handle this by adding a dummy instruction in a new last basic block. */
2983 if (check_last_block)
2985 basic_block bb = EXIT_BLOCK_PTR->prev_bb;
2986 rtx insn = BB_END (bb);
2988 /* Back up past insns that must be kept in the same block as a call. */
2989 while (insn != BB_HEAD (bb)
2990 && keep_with_call_p (insn))
2991 insn = PREV_INSN (insn);
2993 if (need_fake_edge_p (insn))
2995 edge e;
2996 edge_iterator ei;
2998 FOR_EACH_EDGE (e, ei, bb->succs)
3000 if (e->dest == EXIT_BLOCK_PTR)
3002 insert_insn_on_edge (gen_rtx_USE (VOIDmode, const0_rtx), e);
3003 commit_edge_insertions ();
3004 break;
3010 /* Now add fake edges to the function exit for any non constant
3011 calls since there is no way that we can determine if they will
3012 return or not... */
3014 for (i = 0; i < last_bb; i++)
3016 basic_block bb = BASIC_BLOCK (i);
3017 rtx insn;
3018 rtx prev_insn;
3020 if (!bb)
3021 continue;
3023 if (blocks && !TEST_BIT (blocks, i))
3024 continue;
3026 for (insn = BB_END (bb); ; insn = prev_insn)
3028 prev_insn = PREV_INSN (insn);
3029 if (need_fake_edge_p (insn))
3031 edge e;
3032 rtx split_at_insn = insn;
3034 /* Don't split the block between a call and an insn that should
3035 remain in the same block as the call. */
3036 if (CALL_P (insn))
3037 while (split_at_insn != BB_END (bb)
3038 && keep_with_call_p (NEXT_INSN (split_at_insn)))
3039 split_at_insn = NEXT_INSN (split_at_insn);
3041 /* The handling above of the final block before the epilogue
3042 should be enough to verify that there is no edge to the exit
3043 block in CFG already. Calling make_edge in such case would
3044 cause us to mark that edge as fake and remove it later. */
3046 #ifdef ENABLE_CHECKING
3047 if (split_at_insn == BB_END (bb))
3049 edge_iterator ei;
3050 FOR_EACH_EDGE (e, ei, bb->succs)
3052 gcc_assert (e->dest != EXIT_BLOCK_PTR);
3055 #endif
3057 /* Note that the following may create a new basic block
3058 and renumber the existing basic blocks. */
3059 if (split_at_insn != BB_END (bb))
3061 e = split_block (bb, split_at_insn);
3062 if (e)
3063 blocks_split++;
3066 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
3069 if (insn == BB_HEAD (bb))
3070 break;
3074 if (blocks_split)
3075 verify_flow_info ();
3077 return blocks_split;
3080 /* Implementation of CFG manipulation for linearized RTL. */
3081 struct cfg_hooks rtl_cfg_hooks = {
3082 "rtl",
3083 rtl_verify_flow_info,
3084 rtl_dump_bb,
3085 rtl_create_basic_block,
3086 rtl_redirect_edge_and_branch,
3087 rtl_redirect_edge_and_branch_force,
3088 rtl_delete_block,
3089 rtl_split_block,
3090 rtl_move_block_after,
3091 rtl_can_merge_blocks, /* can_merge_blocks_p */
3092 rtl_merge_blocks,
3093 rtl_predict_edge,
3094 rtl_predicted_by_p,
3095 NULL, /* can_duplicate_block_p */
3096 NULL, /* duplicate_block */
3097 rtl_split_edge,
3098 rtl_make_forwarder_block,
3099 rtl_tidy_fallthru_edge,
3100 rtl_block_ends_with_call_p,
3101 rtl_block_ends_with_condjump_p,
3102 rtl_flow_call_edges_add
3105 /* Implementation of CFG manipulation for cfg layout RTL, where
3106 basic block connected via fallthru edges does not have to be adjacent.
3107 This representation will hopefully become the default one in future
3108 version of the compiler. */
3110 /* We do not want to declare these functions in a header file, since they
3111 should only be used through the cfghooks interface, and we do not want to
3112 move them here since it would require also moving quite a lot of related
3113 code. */
3114 extern bool cfg_layout_can_duplicate_bb_p (basic_block);
3115 extern basic_block cfg_layout_duplicate_bb (basic_block);
3117 struct cfg_hooks cfg_layout_rtl_cfg_hooks = {
3118 "cfglayout mode",
3119 rtl_verify_flow_info_1,
3120 rtl_dump_bb,
3121 cfg_layout_create_basic_block,
3122 cfg_layout_redirect_edge_and_branch,
3123 cfg_layout_redirect_edge_and_branch_force,
3124 cfg_layout_delete_block,
3125 cfg_layout_split_block,
3126 rtl_move_block_after,
3127 cfg_layout_can_merge_blocks_p,
3128 cfg_layout_merge_blocks,
3129 rtl_predict_edge,
3130 rtl_predicted_by_p,
3131 cfg_layout_can_duplicate_bb_p,
3132 cfg_layout_duplicate_bb,
3133 cfg_layout_split_edge,
3134 rtl_make_forwarder_block,
3135 NULL,
3136 rtl_block_ends_with_call_p,
3137 rtl_block_ends_with_condjump_p,
3138 rtl_flow_call_edges_add