-dA enhancement
[official-gcc.git] / gcc / cfgrtl.c
blobc450ca005a0e0872d7fabf75b7f55c0144cc9fe7
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, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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
33 - CFG fixing after coarse manipulation
34 fixup_abnormal_edges
36 Functions not supposed for generic use:
37 - Infrastructure to determine quickly basic block for insn
38 compute_bb_for_insn, update_bb_for_insn, set_block_for_insn,
39 - Edge redirection with updating and optimizing of insn chain
40 block_label, tidy_fallthru_edge, force_nonfallthru */
42 #include "config.h"
43 #include "system.h"
44 #include "coretypes.h"
45 #include "tm.h"
46 #include "tree.h"
47 #include "hard-reg-set.h"
48 #include "basic-block.h"
49 #include "regs.h"
50 #include "flags.h"
51 #include "output.h"
52 #include "function.h"
53 #include "except.h"
54 #include "rtl-error.h"
55 #include "tm_p.h"
56 #include "obstack.h"
57 #include "insn-attr.h"
58 #include "insn-config.h"
59 #include "cfglayout.h"
60 #include "expr.h"
61 #include "target.h"
62 #include "cfgloop.h"
63 #include "ggc.h"
64 #include "tree-pass.h"
65 #include "df.h"
67 static int can_delete_note_p (const_rtx);
68 static int can_delete_label_p (const_rtx);
69 static basic_block rtl_split_edge (edge);
70 static bool rtl_move_block_after (basic_block, basic_block);
71 static int rtl_verify_flow_info (void);
72 static basic_block cfg_layout_split_block (basic_block, void *);
73 static edge cfg_layout_redirect_edge_and_branch (edge, basic_block);
74 static basic_block cfg_layout_redirect_edge_and_branch_force (edge, basic_block);
75 static void cfg_layout_delete_block (basic_block);
76 static void rtl_delete_block (basic_block);
77 static basic_block rtl_redirect_edge_and_branch_force (edge, basic_block);
78 static edge rtl_redirect_edge_and_branch (edge, basic_block);
79 static basic_block rtl_split_block (basic_block, void *);
80 static void rtl_dump_bb (basic_block, FILE *, int, int);
81 static int rtl_verify_flow_info_1 (void);
82 static void rtl_make_forwarder_block (edge);
84 /* Return true if NOTE is not one of the ones that must be kept paired,
85 so that we may simply delete it. */
87 static int
88 can_delete_note_p (const_rtx note)
90 switch (NOTE_KIND (note))
92 case NOTE_INSN_DELETED:
93 case NOTE_INSN_BASIC_BLOCK:
94 case NOTE_INSN_EPILOGUE_BEG:
95 return true;
97 default:
98 return false;
102 /* True if a given label can be deleted. */
104 static int
105 can_delete_label_p (const_rtx label)
107 return (!LABEL_PRESERVE_P (label)
108 /* User declared labels must be preserved. */
109 && LABEL_NAME (label) == 0
110 && !in_expr_list_p (forced_labels, label));
113 /* Delete INSN by patching it out. Return the next insn. */
116 delete_insn (rtx insn)
118 rtx next = NEXT_INSN (insn);
119 rtx note;
120 bool really_delete = true;
122 if (LABEL_P (insn))
124 /* Some labels can't be directly removed from the INSN chain, as they
125 might be references via variables, constant pool etc.
126 Convert them to the special NOTE_INSN_DELETED_LABEL note. */
127 if (! can_delete_label_p (insn))
129 const char *name = LABEL_NAME (insn);
131 really_delete = false;
132 PUT_CODE (insn, NOTE);
133 NOTE_KIND (insn) = NOTE_INSN_DELETED_LABEL;
134 NOTE_DELETED_LABEL_NAME (insn) = name;
137 remove_node_from_expr_list (insn, &nonlocal_goto_handler_labels);
140 if (really_delete)
142 /* If this insn has already been deleted, something is very wrong. */
143 gcc_assert (!INSN_DELETED_P (insn));
144 remove_insn (insn);
145 INSN_DELETED_P (insn) = 1;
148 /* If deleting a jump, decrement the use count of the label. Deleting
149 the label itself should happen in the normal course of block merging. */
150 if (JUMP_P (insn))
152 if (JUMP_LABEL (insn)
153 && LABEL_P (JUMP_LABEL (insn)))
154 LABEL_NUSES (JUMP_LABEL (insn))--;
156 /* If there are more targets, remove them too. */
157 while ((note
158 = find_reg_note (insn, REG_LABEL_TARGET, NULL_RTX)) != NULL_RTX
159 && LABEL_P (XEXP (note, 0)))
161 LABEL_NUSES (XEXP (note, 0))--;
162 remove_note (insn, note);
166 /* Also if deleting any insn that references a label as an operand. */
167 while ((note = find_reg_note (insn, REG_LABEL_OPERAND, NULL_RTX)) != NULL_RTX
168 && LABEL_P (XEXP (note, 0)))
170 LABEL_NUSES (XEXP (note, 0))--;
171 remove_note (insn, note);
174 if (JUMP_TABLE_DATA_P (insn))
176 rtx pat = PATTERN (insn);
177 int diff_vec_p = GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC;
178 int len = XVECLEN (pat, diff_vec_p);
179 int i;
181 for (i = 0; i < len; i++)
183 rtx label = XEXP (XVECEXP (pat, diff_vec_p, i), 0);
185 /* When deleting code in bulk (e.g. removing many unreachable
186 blocks) we can delete a label that's a target of the vector
187 before deleting the vector itself. */
188 if (!NOTE_P (label))
189 LABEL_NUSES (label)--;
193 return next;
196 /* Like delete_insn but also purge dead edges from BB. */
199 delete_insn_and_edges (rtx insn)
201 rtx x;
202 bool purge = false;
204 if (INSN_P (insn)
205 && BLOCK_FOR_INSN (insn)
206 && BB_END (BLOCK_FOR_INSN (insn)) == insn)
207 purge = true;
208 x = delete_insn (insn);
209 if (purge)
210 purge_dead_edges (BLOCK_FOR_INSN (insn));
211 return x;
214 /* Unlink a chain of insns between START and FINISH, leaving notes
215 that must be paired. If CLEAR_BB is true, we set bb field for
216 insns that cannot be removed to NULL. */
218 void
219 delete_insn_chain (rtx start, rtx finish, bool clear_bb)
221 rtx next;
223 /* Unchain the insns one by one. It would be quicker to delete all of these
224 with a single unchaining, rather than one at a time, but we need to keep
225 the NOTE's. */
226 while (1)
228 next = NEXT_INSN (start);
229 if (NOTE_P (start) && !can_delete_note_p (start))
231 else
232 next = delete_insn (start);
234 if (clear_bb && !INSN_DELETED_P (start))
235 set_block_for_insn (start, NULL);
237 if (start == finish)
238 break;
239 start = next;
243 /* Create a new basic block consisting of the instructions between HEAD and END
244 inclusive. This function is designed to allow fast BB construction - reuses
245 the note and basic block struct in BB_NOTE, if any and do not grow
246 BASIC_BLOCK chain and should be used directly only by CFG construction code.
247 END can be NULL in to create new empty basic block before HEAD. Both END
248 and HEAD can be NULL to create basic block at the end of INSN chain.
249 AFTER is the basic block we should be put after. */
251 basic_block
252 create_basic_block_structure (rtx head, rtx end, rtx bb_note, basic_block after)
254 basic_block bb;
256 if (bb_note
257 && (bb = NOTE_BASIC_BLOCK (bb_note)) != NULL
258 && bb->aux == NULL)
260 /* If we found an existing note, thread it back onto the chain. */
262 rtx after;
264 if (LABEL_P (head))
265 after = head;
266 else
268 after = PREV_INSN (head);
269 head = bb_note;
272 if (after != bb_note && NEXT_INSN (after) != bb_note)
273 reorder_insns_nobb (bb_note, bb_note, after);
275 else
277 /* Otherwise we must create a note and a basic block structure. */
279 bb = alloc_block ();
281 init_rtl_bb_info (bb);
282 if (!head && !end)
283 head = end = bb_note
284 = emit_note_after (NOTE_INSN_BASIC_BLOCK, get_last_insn ());
285 else if (LABEL_P (head) && end)
287 bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, head);
288 if (head == end)
289 end = bb_note;
291 else
293 bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, head);
294 head = bb_note;
295 if (!end)
296 end = head;
299 NOTE_BASIC_BLOCK (bb_note) = bb;
302 /* Always include the bb note in the block. */
303 if (NEXT_INSN (end) == bb_note)
304 end = bb_note;
306 BB_HEAD (bb) = head;
307 BB_END (bb) = end;
308 bb->index = last_basic_block++;
309 bb->flags = BB_NEW | BB_RTL;
310 link_block (bb, after);
311 SET_BASIC_BLOCK (bb->index, bb);
312 df_bb_refs_record (bb->index, false);
313 update_bb_for_insn (bb);
314 BB_SET_PARTITION (bb, BB_UNPARTITIONED);
316 /* Tag the block so that we know it has been used when considering
317 other basic block notes. */
318 bb->aux = bb;
320 return bb;
323 /* Create new basic block consisting of instructions in between HEAD and END
324 and place it to the BB chain after block AFTER. END can be NULL in to
325 create new empty basic block before HEAD. Both END and HEAD can be NULL to
326 create basic block at the end of INSN chain. */
328 static basic_block
329 rtl_create_basic_block (void *headp, void *endp, basic_block after)
331 rtx head = (rtx) headp, end = (rtx) endp;
332 basic_block bb;
334 /* Grow the basic block array if needed. */
335 if ((size_t) last_basic_block >= VEC_length (basic_block, basic_block_info))
337 size_t new_size = last_basic_block + (last_basic_block + 3) / 4;
338 VEC_safe_grow_cleared (basic_block, gc, basic_block_info, new_size);
341 n_basic_blocks++;
343 bb = create_basic_block_structure (head, end, NULL, after);
344 bb->aux = NULL;
345 return bb;
348 static basic_block
349 cfg_layout_create_basic_block (void *head, void *end, basic_block after)
351 basic_block newbb = rtl_create_basic_block (head, end, after);
353 return newbb;
356 /* Delete the insns in a (non-live) block. We physically delete every
357 non-deleted-note insn, and update the flow graph appropriately.
359 Return nonzero if we deleted an exception handler. */
361 /* ??? Preserving all such notes strikes me as wrong. It would be nice
362 to post-process the stream to remove empty blocks, loops, ranges, etc. */
364 static void
365 rtl_delete_block (basic_block b)
367 rtx insn, end;
369 /* If the head of this block is a CODE_LABEL, then it might be the
370 label for an exception handler which can't be reached. We need
371 to remove the label from the exception_handler_label list. */
372 insn = BB_HEAD (b);
374 end = get_last_bb_insn (b);
376 /* Selectively delete the entire chain. */
377 BB_HEAD (b) = NULL;
378 delete_insn_chain (insn, end, true);
381 if (dump_file)
382 fprintf (dump_file, "deleting block %d\n", b->index);
383 df_bb_delete (b->index);
386 /* Records the basic block struct in BLOCK_FOR_INSN for every insn. */
388 void
389 compute_bb_for_insn (void)
391 basic_block bb;
393 FOR_EACH_BB (bb)
395 rtx end = BB_END (bb);
396 rtx insn;
398 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
400 BLOCK_FOR_INSN (insn) = bb;
401 if (insn == end)
402 break;
407 /* Release the basic_block_for_insn array. */
409 unsigned int
410 free_bb_for_insn (void)
412 rtx insn;
413 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
414 if (!BARRIER_P (insn))
415 BLOCK_FOR_INSN (insn) = NULL;
416 return 0;
419 static unsigned int
420 rest_of_pass_free_cfg (void)
422 #ifdef DELAY_SLOTS
423 /* The resource.c machinery uses DF but the CFG isn't guaranteed to be
424 valid at that point so it would be too late to call df_analyze. */
425 if (optimize > 0 && flag_delayed_branch)
427 df_note_add_problem ();
428 df_analyze ();
430 #endif
432 free_bb_for_insn ();
433 return 0;
436 struct rtl_opt_pass pass_free_cfg =
439 RTL_PASS,
440 "*free_cfg", /* name */
441 NULL, /* gate */
442 rest_of_pass_free_cfg, /* execute */
443 NULL, /* sub */
444 NULL, /* next */
445 0, /* static_pass_number */
446 TV_NONE, /* tv_id */
447 0, /* properties_required */
448 0, /* properties_provided */
449 PROP_cfg, /* properties_destroyed */
450 0, /* todo_flags_start */
451 0, /* todo_flags_finish */
455 /* Return RTX to emit after when we want to emit code on the entry of function. */
457 entry_of_function (void)
459 return (n_basic_blocks > NUM_FIXED_BLOCKS ?
460 BB_HEAD (ENTRY_BLOCK_PTR->next_bb) : get_insns ());
463 /* Emit INSN at the entry point of the function, ensuring that it is only
464 executed once per function. */
465 void
466 emit_insn_at_entry (rtx insn)
468 edge_iterator ei = ei_start (ENTRY_BLOCK_PTR->succs);
469 edge e = ei_safe_edge (ei);
470 gcc_assert (e->flags & EDGE_FALLTHRU);
472 insert_insn_on_edge (insn, e);
473 commit_edge_insertions ();
476 /* Update BLOCK_FOR_INSN of insns between BEGIN and END
477 (or BARRIER if found) and notify df of the bb change.
478 The insn chain range is inclusive
479 (i.e. both BEGIN and END will be updated. */
481 static void
482 update_bb_for_insn_chain (rtx begin, rtx end, basic_block bb)
484 rtx insn;
486 end = NEXT_INSN (end);
487 for (insn = begin; insn != end; insn = NEXT_INSN (insn))
488 if (!BARRIER_P (insn))
489 df_insn_change_bb (insn, bb);
492 /* Update BLOCK_FOR_INSN of insns in BB to BB,
493 and notify df of the change. */
495 void
496 update_bb_for_insn (basic_block bb)
498 update_bb_for_insn_chain (BB_HEAD (bb), BB_END (bb), bb);
502 /* Return the INSN immediately following the NOTE_INSN_BASIC_BLOCK
503 note associated with the BLOCK. */
505 static rtx
506 first_insn_after_basic_block_note (basic_block block)
508 rtx insn;
510 /* Get the first instruction in the block. */
511 insn = BB_HEAD (block);
513 if (insn == NULL_RTX)
514 return NULL_RTX;
515 if (LABEL_P (insn))
516 insn = NEXT_INSN (insn);
517 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
519 return NEXT_INSN (insn);
522 /* Creates a new basic block just after basic block B by splitting
523 everything after specified instruction I. */
525 static basic_block
526 rtl_split_block (basic_block bb, void *insnp)
528 basic_block new_bb;
529 rtx insn = (rtx) insnp;
530 edge e;
531 edge_iterator ei;
533 if (!insn)
535 insn = first_insn_after_basic_block_note (bb);
537 if (insn)
539 rtx next = insn;
541 insn = PREV_INSN (insn);
543 /* If the block contains only debug insns, insn would have
544 been NULL in a non-debug compilation, and then we'd end
545 up emitting a DELETED note. For -fcompare-debug
546 stability, emit the note too. */
547 if (insn != BB_END (bb)
548 && DEBUG_INSN_P (next)
549 && DEBUG_INSN_P (BB_END (bb)))
551 while (next != BB_END (bb) && DEBUG_INSN_P (next))
552 next = NEXT_INSN (next);
554 if (next == BB_END (bb))
555 emit_note_after (NOTE_INSN_DELETED, next);
558 else
559 insn = get_last_insn ();
562 /* We probably should check type of the insn so that we do not create
563 inconsistent cfg. It is checked in verify_flow_info anyway, so do not
564 bother. */
565 if (insn == BB_END (bb))
566 emit_note_after (NOTE_INSN_DELETED, insn);
568 /* Create the new basic block. */
569 new_bb = create_basic_block (NEXT_INSN (insn), BB_END (bb), bb);
570 BB_COPY_PARTITION (new_bb, bb);
571 BB_END (bb) = insn;
573 /* Redirect the outgoing edges. */
574 new_bb->succs = bb->succs;
575 bb->succs = NULL;
576 FOR_EACH_EDGE (e, ei, new_bb->succs)
577 e->src = new_bb;
579 /* The new block starts off being dirty. */
580 df_set_bb_dirty (bb);
581 return new_bb;
584 /* Blocks A and B are to be merged into a single block A. The insns
585 are already contiguous. */
587 static void
588 rtl_merge_blocks (basic_block a, basic_block b)
590 rtx b_head = BB_HEAD (b), b_end = BB_END (b), a_end = BB_END (a);
591 rtx del_first = NULL_RTX, del_last = NULL_RTX;
592 rtx b_debug_start = b_end, b_debug_end = b_end;
593 bool forwarder_p = (b->flags & BB_FORWARDER_BLOCK) != 0;
594 int b_empty = 0;
596 if (dump_file)
597 fprintf (dump_file, "Merging block %d into block %d...\n", b->index,
598 a->index);
600 while (DEBUG_INSN_P (b_end))
601 b_end = PREV_INSN (b_debug_start = b_end);
603 /* If there was a CODE_LABEL beginning B, delete it. */
604 if (LABEL_P (b_head))
606 /* Detect basic blocks with nothing but a label. This can happen
607 in particular at the end of a function. */
608 if (b_head == b_end)
609 b_empty = 1;
611 del_first = del_last = b_head;
612 b_head = NEXT_INSN (b_head);
615 /* Delete the basic block note and handle blocks containing just that
616 note. */
617 if (NOTE_INSN_BASIC_BLOCK_P (b_head))
619 if (b_head == b_end)
620 b_empty = 1;
621 if (! del_last)
622 del_first = b_head;
624 del_last = b_head;
625 b_head = NEXT_INSN (b_head);
628 /* If there was a jump out of A, delete it. */
629 if (JUMP_P (a_end))
631 rtx prev;
633 for (prev = PREV_INSN (a_end); ; prev = PREV_INSN (prev))
634 if (!NOTE_P (prev)
635 || NOTE_INSN_BASIC_BLOCK_P (prev)
636 || prev == BB_HEAD (a))
637 break;
639 del_first = a_end;
641 #ifdef HAVE_cc0
642 /* If this was a conditional jump, we need to also delete
643 the insn that set cc0. */
644 if (only_sets_cc0_p (prev))
646 rtx tmp = prev;
648 prev = prev_nonnote_insn (prev);
649 if (!prev)
650 prev = BB_HEAD (a);
651 del_first = tmp;
653 #endif
655 a_end = PREV_INSN (del_first);
657 else if (BARRIER_P (NEXT_INSN (a_end)))
658 del_first = NEXT_INSN (a_end);
660 /* Delete everything marked above as well as crap that might be
661 hanging out between the two blocks. */
662 BB_HEAD (b) = NULL;
663 delete_insn_chain (del_first, del_last, true);
665 /* Reassociate the insns of B with A. */
666 if (!b_empty)
668 update_bb_for_insn_chain (a_end, b_debug_end, a);
670 a_end = b_debug_end;
672 else if (b_end != b_debug_end)
674 /* Move any deleted labels and other notes between the end of A
675 and the debug insns that make up B after the debug insns,
676 bringing the debug insns into A while keeping the notes after
677 the end of A. */
678 if (NEXT_INSN (a_end) != b_debug_start)
679 reorder_insns_nobb (NEXT_INSN (a_end), PREV_INSN (b_debug_start),
680 b_debug_end);
681 update_bb_for_insn_chain (b_debug_start, b_debug_end, a);
682 a_end = b_debug_end;
685 df_bb_delete (b->index);
686 BB_END (a) = a_end;
688 /* If B was a forwarder block, propagate the locus on the edge. */
689 if (forwarder_p && !EDGE_SUCC (b, 0)->goto_locus)
690 EDGE_SUCC (b, 0)->goto_locus = EDGE_SUCC (a, 0)->goto_locus;
692 if (dump_file)
693 fprintf (dump_file, "Merged blocks %d and %d.\n", a->index, b->index);
697 /* Return true when block A and B can be merged. */
699 static bool
700 rtl_can_merge_blocks (basic_block a, basic_block b)
702 /* If we are partitioning hot/cold basic blocks, we don't want to
703 mess up unconditional or indirect jumps that cross between hot
704 and cold sections.
706 Basic block partitioning may result in some jumps that appear to
707 be optimizable (or blocks that appear to be mergeable), but which really
708 must be left untouched (they are required to make it safely across
709 partition boundaries). See the comments at the top of
710 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
712 if (BB_PARTITION (a) != BB_PARTITION (b))
713 return false;
715 /* There must be exactly one edge in between the blocks. */
716 return (single_succ_p (a)
717 && single_succ (a) == b
718 && single_pred_p (b)
719 && a != b
720 /* Must be simple edge. */
721 && !(single_succ_edge (a)->flags & EDGE_COMPLEX)
722 && a->next_bb == b
723 && a != ENTRY_BLOCK_PTR && b != EXIT_BLOCK_PTR
724 /* If the jump insn has side effects,
725 we can't kill the edge. */
726 && (!JUMP_P (BB_END (a))
727 || (reload_completed
728 ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
731 /* Return the label in the head of basic block BLOCK. Create one if it doesn't
732 exist. */
735 block_label (basic_block block)
737 if (block == EXIT_BLOCK_PTR)
738 return NULL_RTX;
740 if (!LABEL_P (BB_HEAD (block)))
742 BB_HEAD (block) = emit_label_before (gen_label_rtx (), BB_HEAD (block));
745 return BB_HEAD (block);
748 /* Attempt to perform edge redirection by replacing possibly complex jump
749 instruction by unconditional jump or removing jump completely. This can
750 apply only if all edges now point to the same block. The parameters and
751 return values are equivalent to redirect_edge_and_branch. */
753 edge
754 try_redirect_by_replacing_jump (edge e, basic_block target, bool in_cfglayout)
756 basic_block src = e->src;
757 rtx insn = BB_END (src), kill_from;
758 rtx set;
759 int fallthru = 0;
761 /* If we are partitioning hot/cold basic blocks, we don't want to
762 mess up unconditional or indirect jumps that cross between hot
763 and cold sections.
765 Basic block partitioning may result in some jumps that appear to
766 be optimizable (or blocks that appear to be mergeable), but which really
767 must be left untouched (they are required to make it safely across
768 partition boundaries). See the comments at the top of
769 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
771 if (find_reg_note (insn, REG_CROSSING_JUMP, NULL_RTX)
772 || BB_PARTITION (src) != BB_PARTITION (target))
773 return NULL;
775 /* We can replace or remove a complex jump only when we have exactly
776 two edges. Also, if we have exactly one outgoing edge, we can
777 redirect that. */
778 if (EDGE_COUNT (src->succs) >= 3
779 /* Verify that all targets will be TARGET. Specifically, the
780 edge that is not E must also go to TARGET. */
781 || (EDGE_COUNT (src->succs) == 2
782 && EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target))
783 return NULL;
785 if (!onlyjump_p (insn))
786 return NULL;
787 if ((!optimize || reload_completed) && tablejump_p (insn, NULL, NULL))
788 return NULL;
790 /* Avoid removing branch with side effects. */
791 set = single_set (insn);
792 if (!set || side_effects_p (set))
793 return NULL;
795 /* In case we zap a conditional jump, we'll need to kill
796 the cc0 setter too. */
797 kill_from = insn;
798 #ifdef HAVE_cc0
799 if (reg_mentioned_p (cc0_rtx, PATTERN (insn))
800 && only_sets_cc0_p (PREV_INSN (insn)))
801 kill_from = PREV_INSN (insn);
802 #endif
804 /* See if we can create the fallthru edge. */
805 if (in_cfglayout || can_fallthru (src, target))
807 if (dump_file)
808 fprintf (dump_file, "Removing jump %i.\n", INSN_UID (insn));
809 fallthru = 1;
811 /* Selectively unlink whole insn chain. */
812 if (in_cfglayout)
814 rtx insn = src->il.rtl->footer;
816 delete_insn_chain (kill_from, BB_END (src), false);
818 /* Remove barriers but keep jumptables. */
819 while (insn)
821 if (BARRIER_P (insn))
823 if (PREV_INSN (insn))
824 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
825 else
826 src->il.rtl->footer = NEXT_INSN (insn);
827 if (NEXT_INSN (insn))
828 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
830 if (LABEL_P (insn))
831 break;
832 insn = NEXT_INSN (insn);
835 else
836 delete_insn_chain (kill_from, PREV_INSN (BB_HEAD (target)),
837 false);
840 /* If this already is simplejump, redirect it. */
841 else if (simplejump_p (insn))
843 if (e->dest == target)
844 return NULL;
845 if (dump_file)
846 fprintf (dump_file, "Redirecting jump %i from %i to %i.\n",
847 INSN_UID (insn), e->dest->index, target->index);
848 if (!redirect_jump (insn, block_label (target), 0))
850 gcc_assert (target == EXIT_BLOCK_PTR);
851 return NULL;
855 /* Cannot do anything for target exit block. */
856 else if (target == EXIT_BLOCK_PTR)
857 return NULL;
859 /* Or replace possibly complicated jump insn by simple jump insn. */
860 else
862 rtx target_label = block_label (target);
863 rtx barrier, label, table;
865 emit_jump_insn_after_noloc (gen_jump (target_label), insn);
866 JUMP_LABEL (BB_END (src)) = target_label;
867 LABEL_NUSES (target_label)++;
868 if (dump_file)
869 fprintf (dump_file, "Replacing insn %i by jump %i\n",
870 INSN_UID (insn), INSN_UID (BB_END (src)));
873 delete_insn_chain (kill_from, insn, false);
875 /* Recognize a tablejump that we are converting to a
876 simple jump and remove its associated CODE_LABEL
877 and ADDR_VEC or ADDR_DIFF_VEC. */
878 if (tablejump_p (insn, &label, &table))
879 delete_insn_chain (label, table, false);
881 barrier = next_nonnote_insn (BB_END (src));
882 if (!barrier || !BARRIER_P (barrier))
883 emit_barrier_after (BB_END (src));
884 else
886 if (barrier != NEXT_INSN (BB_END (src)))
888 /* Move the jump before barrier so that the notes
889 which originally were or were created before jump table are
890 inside the basic block. */
891 rtx new_insn = BB_END (src);
893 update_bb_for_insn_chain (NEXT_INSN (BB_END (src)),
894 PREV_INSN (barrier), src);
896 NEXT_INSN (PREV_INSN (new_insn)) = NEXT_INSN (new_insn);
897 PREV_INSN (NEXT_INSN (new_insn)) = PREV_INSN (new_insn);
899 NEXT_INSN (new_insn) = barrier;
900 NEXT_INSN (PREV_INSN (barrier)) = new_insn;
902 PREV_INSN (new_insn) = PREV_INSN (barrier);
903 PREV_INSN (barrier) = new_insn;
908 /* Keep only one edge out and set proper flags. */
909 if (!single_succ_p (src))
910 remove_edge (e);
911 gcc_assert (single_succ_p (src));
913 e = single_succ_edge (src);
914 if (fallthru)
915 e->flags = EDGE_FALLTHRU;
916 else
917 e->flags = 0;
919 e->probability = REG_BR_PROB_BASE;
920 e->count = src->count;
922 if (e->dest != target)
923 redirect_edge_succ (e, target);
924 return e;
927 /* Subroutine of redirect_branch_edge that tries to patch the jump
928 instruction INSN so that it reaches block NEW. Do this
929 only when it originally reached block OLD. Return true if this
930 worked or the original target wasn't OLD, return false if redirection
931 doesn't work. */
933 static bool
934 patch_jump_insn (rtx insn, rtx old_label, basic_block new_bb)
936 rtx tmp;
937 /* Recognize a tablejump and adjust all matching cases. */
938 if (tablejump_p (insn, NULL, &tmp))
940 rtvec vec;
941 int j;
942 rtx new_label = block_label (new_bb);
944 if (new_bb == EXIT_BLOCK_PTR)
945 return false;
946 if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
947 vec = XVEC (PATTERN (tmp), 0);
948 else
949 vec = XVEC (PATTERN (tmp), 1);
951 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
952 if (XEXP (RTVEC_ELT (vec, j), 0) == old_label)
954 RTVEC_ELT (vec, j) = gen_rtx_LABEL_REF (Pmode, new_label);
955 --LABEL_NUSES (old_label);
956 ++LABEL_NUSES (new_label);
959 /* Handle casesi dispatch insns. */
960 if ((tmp = single_set (insn)) != NULL
961 && SET_DEST (tmp) == pc_rtx
962 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
963 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF
964 && XEXP (XEXP (SET_SRC (tmp), 2), 0) == old_label)
966 XEXP (SET_SRC (tmp), 2) = gen_rtx_LABEL_REF (Pmode,
967 new_label);
968 --LABEL_NUSES (old_label);
969 ++LABEL_NUSES (new_label);
972 else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL)
974 int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp);
975 rtx new_label, note;
977 if (new_bb == EXIT_BLOCK_PTR)
978 return false;
979 new_label = block_label (new_bb);
981 for (i = 0; i < n; ++i)
983 rtx old_ref = ASM_OPERANDS_LABEL (tmp, i);
984 gcc_assert (GET_CODE (old_ref) == LABEL_REF);
985 if (XEXP (old_ref, 0) == old_label)
987 ASM_OPERANDS_LABEL (tmp, i)
988 = gen_rtx_LABEL_REF (Pmode, new_label);
989 --LABEL_NUSES (old_label);
990 ++LABEL_NUSES (new_label);
994 if (JUMP_LABEL (insn) == old_label)
996 JUMP_LABEL (insn) = new_label;
997 note = find_reg_note (insn, REG_LABEL_TARGET, new_label);
998 if (note)
999 remove_note (insn, note);
1001 else
1003 note = find_reg_note (insn, REG_LABEL_TARGET, old_label);
1004 if (note)
1005 remove_note (insn, note);
1006 if (JUMP_LABEL (insn) != new_label
1007 && !find_reg_note (insn, REG_LABEL_TARGET, new_label))
1008 add_reg_note (insn, REG_LABEL_TARGET, new_label);
1010 while ((note = find_reg_note (insn, REG_LABEL_OPERAND, old_label))
1011 != NULL_RTX)
1012 XEXP (note, 0) = new_label;
1014 else
1016 /* ?? We may play the games with moving the named labels from
1017 one basic block to the other in case only one computed_jump is
1018 available. */
1019 if (computed_jump_p (insn)
1020 /* A return instruction can't be redirected. */
1021 || returnjump_p (insn))
1022 return false;
1024 if (!currently_expanding_to_rtl || JUMP_LABEL (insn) == old_label)
1026 /* If the insn doesn't go where we think, we're confused. */
1027 gcc_assert (JUMP_LABEL (insn) == old_label);
1029 /* If the substitution doesn't succeed, die. This can happen
1030 if the back end emitted unrecognizable instructions or if
1031 target is exit block on some arches. */
1032 if (!redirect_jump (insn, block_label (new_bb), 0))
1034 gcc_assert (new_bb == EXIT_BLOCK_PTR);
1035 return false;
1039 return true;
1043 /* Redirect edge representing branch of (un)conditional jump or tablejump,
1044 NULL on failure */
1045 static edge
1046 redirect_branch_edge (edge e, basic_block target)
1048 rtx old_label = BB_HEAD (e->dest);
1049 basic_block src = e->src;
1050 rtx insn = BB_END (src);
1052 /* We can only redirect non-fallthru edges of jump insn. */
1053 if (e->flags & EDGE_FALLTHRU)
1054 return NULL;
1055 else if (!JUMP_P (insn) && !currently_expanding_to_rtl)
1056 return NULL;
1058 if (!currently_expanding_to_rtl)
1060 if (!patch_jump_insn (insn, old_label, target))
1061 return NULL;
1063 else
1064 /* When expanding this BB might actually contain multiple
1065 jumps (i.e. not yet split by find_many_sub_basic_blocks).
1066 Redirect all of those that match our label. */
1067 FOR_BB_INSNS (src, insn)
1068 if (JUMP_P (insn) && !patch_jump_insn (insn, old_label, target))
1069 return NULL;
1071 if (dump_file)
1072 fprintf (dump_file, "Edge %i->%i redirected to %i\n",
1073 e->src->index, e->dest->index, target->index);
1075 if (e->dest != target)
1076 e = redirect_edge_succ_nodup (e, target);
1078 return e;
1081 /* Attempt to change code to redirect edge E to TARGET. Don't do that on
1082 expense of adding new instructions or reordering basic blocks.
1084 Function can be also called with edge destination equivalent to the TARGET.
1085 Then it should try the simplifications and do nothing if none is possible.
1087 Return edge representing the branch if transformation succeeded. Return NULL
1088 on failure.
1089 We still return NULL in case E already destinated TARGET and we didn't
1090 managed to simplify instruction stream. */
1092 static edge
1093 rtl_redirect_edge_and_branch (edge e, basic_block target)
1095 edge ret;
1096 basic_block src = e->src;
1098 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
1099 return NULL;
1101 if (e->dest == target)
1102 return e;
1104 if ((ret = try_redirect_by_replacing_jump (e, target, false)) != NULL)
1106 df_set_bb_dirty (src);
1107 return ret;
1110 ret = redirect_branch_edge (e, target);
1111 if (!ret)
1112 return NULL;
1114 df_set_bb_dirty (src);
1115 return ret;
1118 /* Like force_nonfallthru below, but additionally performs redirection
1119 Used by redirect_edge_and_branch_force. */
1121 static basic_block
1122 force_nonfallthru_and_redirect (edge e, basic_block target)
1124 basic_block jump_block, new_bb = NULL, src = e->src;
1125 rtx note;
1126 edge new_edge;
1127 int abnormal_edge_flags = 0;
1128 int loc;
1130 /* In the case the last instruction is conditional jump to the next
1131 instruction, first redirect the jump itself and then continue
1132 by creating a basic block afterwards to redirect fallthru edge. */
1133 if (e->src != ENTRY_BLOCK_PTR && e->dest != EXIT_BLOCK_PTR
1134 && any_condjump_p (BB_END (e->src))
1135 && JUMP_LABEL (BB_END (e->src)) == BB_HEAD (e->dest))
1137 rtx note;
1138 edge b = unchecked_make_edge (e->src, target, 0);
1139 bool redirected;
1141 redirected = redirect_jump (BB_END (e->src), block_label (target), 0);
1142 gcc_assert (redirected);
1144 note = find_reg_note (BB_END (e->src), REG_BR_PROB, NULL_RTX);
1145 if (note)
1147 int prob = INTVAL (XEXP (note, 0));
1149 b->probability = prob;
1150 b->count = e->count * prob / REG_BR_PROB_BASE;
1151 e->probability -= e->probability;
1152 e->count -= b->count;
1153 if (e->probability < 0)
1154 e->probability = 0;
1155 if (e->count < 0)
1156 e->count = 0;
1160 if (e->flags & EDGE_ABNORMAL)
1162 /* Irritating special case - fallthru edge to the same block as abnormal
1163 edge.
1164 We can't redirect abnormal edge, but we still can split the fallthru
1165 one and create separate abnormal edge to original destination.
1166 This allows bb-reorder to make such edge non-fallthru. */
1167 gcc_assert (e->dest == target);
1168 abnormal_edge_flags = e->flags & ~(EDGE_FALLTHRU | EDGE_CAN_FALLTHRU);
1169 e->flags &= EDGE_FALLTHRU | EDGE_CAN_FALLTHRU;
1171 else
1173 gcc_assert (e->flags & EDGE_FALLTHRU);
1174 if (e->src == ENTRY_BLOCK_PTR)
1176 /* We can't redirect the entry block. Create an empty block
1177 at the start of the function which we use to add the new
1178 jump. */
1179 edge tmp;
1180 edge_iterator ei;
1181 bool found = false;
1183 basic_block bb = create_basic_block (BB_HEAD (e->dest), NULL, ENTRY_BLOCK_PTR);
1185 /* Change the existing edge's source to be the new block, and add
1186 a new edge from the entry block to the new block. */
1187 e->src = bb;
1188 for (ei = ei_start (ENTRY_BLOCK_PTR->succs); (tmp = ei_safe_edge (ei)); )
1190 if (tmp == e)
1192 VEC_unordered_remove (edge, ENTRY_BLOCK_PTR->succs, ei.index);
1193 found = true;
1194 break;
1196 else
1197 ei_next (&ei);
1200 gcc_assert (found);
1202 VEC_safe_push (edge, gc, bb->succs, e);
1203 make_single_succ_edge (ENTRY_BLOCK_PTR, bb, EDGE_FALLTHRU);
1207 if (EDGE_COUNT (e->src->succs) >= 2 || abnormal_edge_flags)
1209 /* Create the new structures. */
1211 /* If the old block ended with a tablejump, skip its table
1212 by searching forward from there. Otherwise start searching
1213 forward from the last instruction of the old block. */
1214 if (!tablejump_p (BB_END (e->src), NULL, &note))
1215 note = BB_END (e->src);
1216 note = NEXT_INSN (note);
1218 jump_block = create_basic_block (note, NULL, e->src);
1219 jump_block->count = e->count;
1220 jump_block->frequency = EDGE_FREQUENCY (e);
1221 jump_block->loop_depth = target->loop_depth;
1223 /* Make sure new block ends up in correct hot/cold section. */
1225 BB_COPY_PARTITION (jump_block, e->src);
1226 if (flag_reorder_blocks_and_partition
1227 && targetm.have_named_sections
1228 && JUMP_P (BB_END (jump_block))
1229 && !any_condjump_p (BB_END (jump_block))
1230 && (EDGE_SUCC (jump_block, 0)->flags & EDGE_CROSSING))
1231 add_reg_note (BB_END (jump_block), REG_CROSSING_JUMP, NULL_RTX);
1233 /* Wire edge in. */
1234 new_edge = make_edge (e->src, jump_block, EDGE_FALLTHRU);
1235 new_edge->probability = e->probability;
1236 new_edge->count = e->count;
1238 /* Redirect old edge. */
1239 redirect_edge_pred (e, jump_block);
1240 e->probability = REG_BR_PROB_BASE;
1242 new_bb = jump_block;
1244 else
1245 jump_block = e->src;
1247 if (e->goto_locus && e->goto_block == NULL)
1248 loc = e->goto_locus;
1249 else
1250 loc = 0;
1251 e->flags &= ~EDGE_FALLTHRU;
1252 if (target == EXIT_BLOCK_PTR)
1254 #ifdef HAVE_return
1255 emit_jump_insn_after_setloc (gen_return (), BB_END (jump_block), loc);
1256 #else
1257 gcc_unreachable ();
1258 #endif
1260 else
1262 rtx label = block_label (target);
1263 emit_jump_insn_after_setloc (gen_jump (label), BB_END (jump_block), loc);
1264 JUMP_LABEL (BB_END (jump_block)) = label;
1265 LABEL_NUSES (label)++;
1268 emit_barrier_after (BB_END (jump_block));
1269 redirect_edge_succ_nodup (e, target);
1271 if (abnormal_edge_flags)
1272 make_edge (src, target, abnormal_edge_flags);
1274 df_mark_solutions_dirty ();
1275 return new_bb;
1278 /* Edge E is assumed to be fallthru edge. Emit needed jump instruction
1279 (and possibly create new basic block) to make edge non-fallthru.
1280 Return newly created BB or NULL if none. */
1282 static basic_block
1283 rtl_force_nonfallthru (edge e)
1285 return force_nonfallthru_and_redirect (e, e->dest);
1288 /* Redirect edge even at the expense of creating new jump insn or
1289 basic block. Return new basic block if created, NULL otherwise.
1290 Conversion must be possible. */
1292 static basic_block
1293 rtl_redirect_edge_and_branch_force (edge e, basic_block target)
1295 if (redirect_edge_and_branch (e, target)
1296 || e->dest == target)
1297 return NULL;
1299 /* In case the edge redirection failed, try to force it to be non-fallthru
1300 and redirect newly created simplejump. */
1301 df_set_bb_dirty (e->src);
1302 return force_nonfallthru_and_redirect (e, target);
1305 /* The given edge should potentially be a fallthru edge. If that is in
1306 fact true, delete the jump and barriers that are in the way. */
1308 static void
1309 rtl_tidy_fallthru_edge (edge e)
1311 rtx q;
1312 basic_block b = e->src, c = b->next_bb;
1314 /* ??? In a late-running flow pass, other folks may have deleted basic
1315 blocks by nopping out blocks, leaving multiple BARRIERs between here
1316 and the target label. They ought to be chastised and fixed.
1318 We can also wind up with a sequence of undeletable labels between
1319 one block and the next.
1321 So search through a sequence of barriers, labels, and notes for
1322 the head of block C and assert that we really do fall through. */
1324 for (q = NEXT_INSN (BB_END (b)); q != BB_HEAD (c); q = NEXT_INSN (q))
1325 if (INSN_P (q))
1326 return;
1328 /* Remove what will soon cease being the jump insn from the source block.
1329 If block B consisted only of this single jump, turn it into a deleted
1330 note. */
1331 q = BB_END (b);
1332 if (JUMP_P (q)
1333 && onlyjump_p (q)
1334 && (any_uncondjump_p (q)
1335 || single_succ_p (b)))
1337 #ifdef HAVE_cc0
1338 /* If this was a conditional jump, we need to also delete
1339 the insn that set cc0. */
1340 if (any_condjump_p (q) && only_sets_cc0_p (PREV_INSN (q)))
1341 q = PREV_INSN (q);
1342 #endif
1344 q = PREV_INSN (q);
1347 /* Selectively unlink the sequence. */
1348 if (q != PREV_INSN (BB_HEAD (c)))
1349 delete_insn_chain (NEXT_INSN (q), PREV_INSN (BB_HEAD (c)), false);
1351 e->flags |= EDGE_FALLTHRU;
1354 /* Should move basic block BB after basic block AFTER. NIY. */
1356 static bool
1357 rtl_move_block_after (basic_block bb ATTRIBUTE_UNUSED,
1358 basic_block after ATTRIBUTE_UNUSED)
1360 return false;
1363 /* Split a (typically critical) edge. Return the new block.
1364 The edge must not be abnormal.
1366 ??? The code generally expects to be called on critical edges.
1367 The case of a block ending in an unconditional jump to a
1368 block with multiple predecessors is not handled optimally. */
1370 static basic_block
1371 rtl_split_edge (edge edge_in)
1373 basic_block bb;
1374 rtx before;
1376 /* Abnormal edges cannot be split. */
1377 gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
1379 /* We are going to place the new block in front of edge destination.
1380 Avoid existence of fallthru predecessors. */
1381 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1383 edge e = find_fallthru_edge (edge_in->dest->preds);
1385 if (e)
1386 force_nonfallthru (e);
1389 /* Create the basic block note. */
1390 if (edge_in->dest != EXIT_BLOCK_PTR)
1391 before = BB_HEAD (edge_in->dest);
1392 else
1393 before = NULL_RTX;
1395 /* If this is a fall through edge to the exit block, the blocks might be
1396 not adjacent, and the right place is the after the source. */
1397 if (edge_in->flags & EDGE_FALLTHRU && edge_in->dest == EXIT_BLOCK_PTR)
1399 before = NEXT_INSN (BB_END (edge_in->src));
1400 bb = create_basic_block (before, NULL, edge_in->src);
1401 BB_COPY_PARTITION (bb, edge_in->src);
1403 else
1405 bb = create_basic_block (before, NULL, edge_in->dest->prev_bb);
1406 /* ??? Why not edge_in->dest->prev_bb here? */
1407 BB_COPY_PARTITION (bb, edge_in->dest);
1410 make_single_succ_edge (bb, edge_in->dest, EDGE_FALLTHRU);
1412 /* For non-fallthru edges, we must adjust the predecessor's
1413 jump instruction to target our new block. */
1414 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1416 edge redirected = redirect_edge_and_branch (edge_in, bb);
1417 gcc_assert (redirected);
1419 else
1421 if (edge_in->src != ENTRY_BLOCK_PTR)
1423 /* For asm goto even splitting of fallthru edge might
1424 need insn patching, as other labels might point to the
1425 old label. */
1426 rtx last = BB_END (edge_in->src);
1427 if (last
1428 && JUMP_P (last)
1429 && edge_in->dest != EXIT_BLOCK_PTR
1430 && extract_asm_operands (PATTERN (last)) != NULL_RTX
1431 && patch_jump_insn (last, before, bb))
1432 df_set_bb_dirty (edge_in->src);
1434 redirect_edge_succ (edge_in, bb);
1437 return bb;
1440 /* Queue instructions for insertion on an edge between two basic blocks.
1441 The new instructions and basic blocks (if any) will not appear in the
1442 CFG until commit_edge_insertions is called. */
1444 void
1445 insert_insn_on_edge (rtx pattern, edge e)
1447 /* We cannot insert instructions on an abnormal critical edge.
1448 It will be easier to find the culprit if we die now. */
1449 gcc_assert (!((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e)));
1451 if (e->insns.r == NULL_RTX)
1452 start_sequence ();
1453 else
1454 push_to_sequence (e->insns.r);
1456 emit_insn (pattern);
1458 e->insns.r = get_insns ();
1459 end_sequence ();
1462 /* Update the CFG for the instructions queued on edge E. */
1464 void
1465 commit_one_edge_insertion (edge e)
1467 rtx before = NULL_RTX, after = NULL_RTX, insns, tmp, last;
1468 basic_block bb = NULL;
1470 /* Pull the insns off the edge now since the edge might go away. */
1471 insns = e->insns.r;
1472 e->insns.r = NULL_RTX;
1474 if (!before && !after)
1476 /* Figure out where to put these things. If the destination has
1477 one predecessor, insert there. Except for the exit block. */
1478 if (single_pred_p (e->dest) && e->dest != EXIT_BLOCK_PTR)
1480 bb = e->dest;
1482 /* Get the location correct wrt a code label, and "nice" wrt
1483 a basic block note, and before everything else. */
1484 tmp = BB_HEAD (bb);
1485 if (LABEL_P (tmp))
1486 tmp = NEXT_INSN (tmp);
1487 if (NOTE_INSN_BASIC_BLOCK_P (tmp))
1488 tmp = NEXT_INSN (tmp);
1489 if (tmp == BB_HEAD (bb))
1490 before = tmp;
1491 else if (tmp)
1492 after = PREV_INSN (tmp);
1493 else
1494 after = get_last_insn ();
1497 /* If the source has one successor and the edge is not abnormal,
1498 insert there. Except for the entry block. */
1499 else if ((e->flags & EDGE_ABNORMAL) == 0
1500 && single_succ_p (e->src)
1501 && e->src != ENTRY_BLOCK_PTR)
1503 bb = e->src;
1505 /* It is possible to have a non-simple jump here. Consider a target
1506 where some forms of unconditional jumps clobber a register. This
1507 happens on the fr30 for example.
1509 We know this block has a single successor, so we can just emit
1510 the queued insns before the jump. */
1511 if (JUMP_P (BB_END (bb)))
1512 before = BB_END (bb);
1513 else
1515 /* We'd better be fallthru, or we've lost track of
1516 what's what. */
1517 gcc_assert (e->flags & EDGE_FALLTHRU);
1519 after = BB_END (bb);
1522 /* Otherwise we must split the edge. */
1523 else
1525 bb = split_edge (e);
1526 after = BB_END (bb);
1528 if (flag_reorder_blocks_and_partition
1529 && targetm.have_named_sections
1530 && e->src != ENTRY_BLOCK_PTR
1531 && BB_PARTITION (e->src) == BB_COLD_PARTITION
1532 && !(e->flags & EDGE_CROSSING)
1533 && JUMP_P (after)
1534 && !any_condjump_p (after)
1535 && (single_succ_edge (bb)->flags & EDGE_CROSSING))
1536 add_reg_note (after, REG_CROSSING_JUMP, NULL_RTX);
1540 /* Now that we've found the spot, do the insertion. */
1542 if (before)
1544 emit_insn_before_noloc (insns, before, bb);
1545 last = prev_nonnote_insn (before);
1547 else
1548 last = emit_insn_after_noloc (insns, after, bb);
1550 if (returnjump_p (last))
1552 /* ??? Remove all outgoing edges from BB and add one for EXIT.
1553 This is not currently a problem because this only happens
1554 for the (single) epilogue, which already has a fallthru edge
1555 to EXIT. */
1557 e = single_succ_edge (bb);
1558 gcc_assert (e->dest == EXIT_BLOCK_PTR
1559 && single_succ_p (bb) && (e->flags & EDGE_FALLTHRU));
1561 e->flags &= ~EDGE_FALLTHRU;
1562 emit_barrier_after (last);
1564 if (before)
1565 delete_insn (before);
1567 else
1568 gcc_assert (!JUMP_P (last));
1571 /* Update the CFG for all queued instructions. */
1573 void
1574 commit_edge_insertions (void)
1576 basic_block bb;
1578 #ifdef ENABLE_CHECKING
1579 verify_flow_info ();
1580 #endif
1582 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
1584 edge e;
1585 edge_iterator ei;
1587 FOR_EACH_EDGE (e, ei, bb->succs)
1588 if (e->insns.r)
1589 commit_one_edge_insertion (e);
1594 /* Print out RTL-specific basic block information (live information
1595 at start and end). */
1597 static void
1598 rtl_dump_bb (basic_block bb, FILE *outf, int indent, int flags ATTRIBUTE_UNUSED)
1600 rtx insn;
1601 rtx last;
1602 char *s_indent;
1604 s_indent = (char *) alloca ((size_t) indent + 1);
1605 memset (s_indent, ' ', (size_t) indent);
1606 s_indent[indent] = '\0';
1608 if (df)
1610 df_dump_top (bb, outf);
1611 putc ('\n', outf);
1614 for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb)); insn != last;
1615 insn = NEXT_INSN (insn))
1616 print_rtl_single (outf, insn);
1618 if (df)
1620 df_dump_bottom (bb, outf);
1621 putc ('\n', outf);
1626 /* Like print_rtl, but also print out live information for the start of each
1627 basic block. */
1629 void
1630 print_rtl_with_bb (FILE *outf, const_rtx rtx_first)
1632 const_rtx tmp_rtx;
1633 if (rtx_first == 0)
1634 fprintf (outf, "(nil)\n");
1635 else
1637 enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
1638 int max_uid = get_max_uid ();
1639 basic_block *start = XCNEWVEC (basic_block, max_uid);
1640 basic_block *end = XCNEWVEC (basic_block, max_uid);
1641 enum bb_state *in_bb_p = XCNEWVEC (enum bb_state, max_uid);
1643 basic_block bb;
1645 if (df)
1646 df_dump_start (outf);
1648 FOR_EACH_BB_REVERSE (bb)
1650 rtx x;
1652 start[INSN_UID (BB_HEAD (bb))] = bb;
1653 end[INSN_UID (BB_END (bb))] = bb;
1654 for (x = BB_HEAD (bb); x != NULL_RTX; x = NEXT_INSN (x))
1656 enum bb_state state = IN_MULTIPLE_BB;
1658 if (in_bb_p[INSN_UID (x)] == NOT_IN_BB)
1659 state = IN_ONE_BB;
1660 in_bb_p[INSN_UID (x)] = state;
1662 if (x == BB_END (bb))
1663 break;
1667 for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
1669 int did_output;
1670 if ((bb = start[INSN_UID (tmp_rtx)]) != NULL)
1672 edge e;
1673 edge_iterator ei;
1675 fprintf (outf, ";; Start of basic block (");
1676 FOR_EACH_EDGE (e, ei, bb->preds)
1677 fprintf (outf, " %d", e->src->index);
1678 fprintf (outf, ") -> %d\n", bb->index);
1680 if (df)
1682 df_dump_top (bb, outf);
1683 putc ('\n', outf);
1685 FOR_EACH_EDGE (e, ei, bb->preds)
1687 fputs (";; Pred edge ", outf);
1688 dump_edge_info (outf, e, 0);
1689 fputc ('\n', outf);
1693 if (in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB
1694 && !NOTE_P (tmp_rtx)
1695 && !BARRIER_P (tmp_rtx))
1696 fprintf (outf, ";; Insn is not within a basic block\n");
1697 else if (in_bb_p[INSN_UID (tmp_rtx)] == IN_MULTIPLE_BB)
1698 fprintf (outf, ";; Insn is in multiple basic blocks\n");
1700 did_output = print_rtl_single (outf, tmp_rtx);
1702 if ((bb = end[INSN_UID (tmp_rtx)]) != NULL)
1704 edge e;
1705 edge_iterator ei;
1707 fprintf (outf, ";; End of basic block %d -> (", bb->index);
1708 FOR_EACH_EDGE (e, ei, bb->succs)
1709 fprintf (outf, " %d", e->dest->index);
1710 fprintf (outf, ")\n");
1712 if (df)
1714 df_dump_bottom (bb, outf);
1715 putc ('\n', outf);
1717 putc ('\n', outf);
1718 FOR_EACH_EDGE (e, ei, bb->succs)
1720 fputs (";; Succ edge ", outf);
1721 dump_edge_info (outf, e, 1);
1722 fputc ('\n', outf);
1725 if (did_output)
1726 putc ('\n', outf);
1729 free (start);
1730 free (end);
1731 free (in_bb_p);
1734 if (crtl->epilogue_delay_list != 0)
1736 fprintf (outf, "\n;; Insns in epilogue delay list:\n\n");
1737 for (tmp_rtx = crtl->epilogue_delay_list; tmp_rtx != 0;
1738 tmp_rtx = XEXP (tmp_rtx, 1))
1739 print_rtl_single (outf, XEXP (tmp_rtx, 0));
1743 void
1744 update_br_prob_note (basic_block bb)
1746 rtx note;
1747 if (!JUMP_P (BB_END (bb)))
1748 return;
1749 note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX);
1750 if (!note || INTVAL (XEXP (note, 0)) == BRANCH_EDGE (bb)->probability)
1751 return;
1752 XEXP (note, 0) = GEN_INT (BRANCH_EDGE (bb)->probability);
1755 /* Get the last insn associated with block BB (that includes barriers and
1756 tablejumps after BB). */
1758 get_last_bb_insn (basic_block bb)
1760 rtx tmp;
1761 rtx end = BB_END (bb);
1763 /* Include any jump table following the basic block. */
1764 if (tablejump_p (end, NULL, &tmp))
1765 end = tmp;
1767 /* Include any barriers that may follow the basic block. */
1768 tmp = next_nonnote_insn_bb (end);
1769 while (tmp && BARRIER_P (tmp))
1771 end = tmp;
1772 tmp = next_nonnote_insn_bb (end);
1775 return end;
1778 /* Verify the CFG and RTL consistency common for both underlying RTL and
1779 cfglayout RTL.
1781 Currently it does following checks:
1783 - overlapping of basic blocks
1784 - insns with wrong BLOCK_FOR_INSN pointers
1785 - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note)
1786 - tails of basic blocks (ensure that boundary is necessary)
1787 - scans body of the basic block for JUMP_INSN, CODE_LABEL
1788 and NOTE_INSN_BASIC_BLOCK
1789 - verify that no fall_thru edge crosses hot/cold partition boundaries
1790 - verify that there are no pending RTL branch predictions
1792 In future it can be extended check a lot of other stuff as well
1793 (reachability of basic blocks, life information, etc. etc.). */
1795 static int
1796 rtl_verify_flow_info_1 (void)
1798 rtx x;
1799 int err = 0;
1800 basic_block bb;
1802 /* Check the general integrity of the basic blocks. */
1803 FOR_EACH_BB_REVERSE (bb)
1805 rtx insn;
1807 if (!(bb->flags & BB_RTL))
1809 error ("BB_RTL flag not set for block %d", bb->index);
1810 err = 1;
1813 FOR_BB_INSNS (bb, insn)
1814 if (BLOCK_FOR_INSN (insn) != bb)
1816 error ("insn %d basic block pointer is %d, should be %d",
1817 INSN_UID (insn),
1818 BLOCK_FOR_INSN (insn) ? BLOCK_FOR_INSN (insn)->index : 0,
1819 bb->index);
1820 err = 1;
1823 for (insn = bb->il.rtl->header; insn; insn = NEXT_INSN (insn))
1824 if (!BARRIER_P (insn)
1825 && BLOCK_FOR_INSN (insn) != NULL)
1827 error ("insn %d in header of bb %d has non-NULL basic block",
1828 INSN_UID (insn), bb->index);
1829 err = 1;
1831 for (insn = bb->il.rtl->footer; insn; insn = NEXT_INSN (insn))
1832 if (!BARRIER_P (insn)
1833 && BLOCK_FOR_INSN (insn) != NULL)
1835 error ("insn %d in footer of bb %d has non-NULL basic block",
1836 INSN_UID (insn), bb->index);
1837 err = 1;
1841 /* Now check the basic blocks (boundaries etc.) */
1842 FOR_EACH_BB_REVERSE (bb)
1844 int n_fallthru = 0, n_eh = 0, n_call = 0, n_abnormal = 0, n_branch = 0;
1845 edge e, fallthru = NULL;
1846 rtx note;
1847 edge_iterator ei;
1849 if (JUMP_P (BB_END (bb))
1850 && (note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX))
1851 && EDGE_COUNT (bb->succs) >= 2
1852 && any_condjump_p (BB_END (bb)))
1854 if (INTVAL (XEXP (note, 0)) != BRANCH_EDGE (bb)->probability
1855 && profile_status != PROFILE_ABSENT)
1857 error ("verify_flow_info: REG_BR_PROB does not match cfg %wi %i",
1858 INTVAL (XEXP (note, 0)), BRANCH_EDGE (bb)->probability);
1859 err = 1;
1862 FOR_EACH_EDGE (e, ei, bb->succs)
1864 if (e->flags & EDGE_FALLTHRU)
1866 n_fallthru++, fallthru = e;
1867 if ((e->flags & EDGE_CROSSING)
1868 || (BB_PARTITION (e->src) != BB_PARTITION (e->dest)
1869 && e->src != ENTRY_BLOCK_PTR
1870 && e->dest != EXIT_BLOCK_PTR))
1872 error ("fallthru edge crosses section boundary (bb %i)",
1873 e->src->index);
1874 err = 1;
1878 if ((e->flags & ~(EDGE_DFS_BACK
1879 | EDGE_CAN_FALLTHRU
1880 | EDGE_IRREDUCIBLE_LOOP
1881 | EDGE_LOOP_EXIT
1882 | EDGE_CROSSING)) == 0)
1883 n_branch++;
1885 if (e->flags & EDGE_ABNORMAL_CALL)
1886 n_call++;
1888 if (e->flags & EDGE_EH)
1889 n_eh++;
1890 else if (e->flags & EDGE_ABNORMAL)
1891 n_abnormal++;
1894 if (n_eh && !find_reg_note (BB_END (bb), REG_EH_REGION, NULL_RTX))
1896 error ("missing REG_EH_REGION note in the end of bb %i", bb->index);
1897 err = 1;
1899 if (n_eh > 1)
1901 error ("too many eh edges %i", bb->index);
1902 err = 1;
1904 if (n_branch
1905 && (!JUMP_P (BB_END (bb))
1906 || (n_branch > 1 && (any_uncondjump_p (BB_END (bb))
1907 || any_condjump_p (BB_END (bb))))))
1909 error ("too many outgoing branch edges from bb %i", bb->index);
1910 err = 1;
1912 if (n_fallthru && any_uncondjump_p (BB_END (bb)))
1914 error ("fallthru edge after unconditional jump %i", bb->index);
1915 err = 1;
1917 if (n_branch != 1 && any_uncondjump_p (BB_END (bb)))
1919 error ("wrong number of branch edges after unconditional jump %i",
1920 bb->index);
1921 err = 1;
1923 if (n_branch != 1 && any_condjump_p (BB_END (bb))
1924 && JUMP_LABEL (BB_END (bb)) != BB_HEAD (fallthru->dest))
1926 error ("wrong amount of branch edges after conditional jump %i",
1927 bb->index);
1928 err = 1;
1930 if (n_call && !CALL_P (BB_END (bb)))
1932 error ("call edges for non-call insn in bb %i", bb->index);
1933 err = 1;
1935 if (n_abnormal
1936 && (!CALL_P (BB_END (bb)) && n_call != n_abnormal)
1937 && (!JUMP_P (BB_END (bb))
1938 || any_condjump_p (BB_END (bb))
1939 || any_uncondjump_p (BB_END (bb))))
1941 error ("abnormal edges for no purpose in bb %i", bb->index);
1942 err = 1;
1945 for (x = BB_HEAD (bb); x != NEXT_INSN (BB_END (bb)); x = NEXT_INSN (x))
1946 /* We may have a barrier inside a basic block before dead code
1947 elimination. There is no BLOCK_FOR_INSN field in a barrier. */
1948 if (!BARRIER_P (x) && BLOCK_FOR_INSN (x) != bb)
1950 debug_rtx (x);
1951 if (! BLOCK_FOR_INSN (x))
1952 error
1953 ("insn %d inside basic block %d but block_for_insn is NULL",
1954 INSN_UID (x), bb->index);
1955 else
1956 error
1957 ("insn %d inside basic block %d but block_for_insn is %i",
1958 INSN_UID (x), bb->index, BLOCK_FOR_INSN (x)->index);
1960 err = 1;
1963 /* OK pointers are correct. Now check the header of basic
1964 block. It ought to contain optional CODE_LABEL followed
1965 by NOTE_BASIC_BLOCK. */
1966 x = BB_HEAD (bb);
1967 if (LABEL_P (x))
1969 if (BB_END (bb) == x)
1971 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
1972 bb->index);
1973 err = 1;
1976 x = NEXT_INSN (x);
1979 if (!NOTE_INSN_BASIC_BLOCK_P (x) || NOTE_BASIC_BLOCK (x) != bb)
1981 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
1982 bb->index);
1983 err = 1;
1986 if (BB_END (bb) == x)
1987 /* Do checks for empty blocks here. */
1989 else
1990 for (x = NEXT_INSN (x); x; x = NEXT_INSN (x))
1992 if (NOTE_INSN_BASIC_BLOCK_P (x))
1994 error ("NOTE_INSN_BASIC_BLOCK %d in middle of basic block %d",
1995 INSN_UID (x), bb->index);
1996 err = 1;
1999 if (x == BB_END (bb))
2000 break;
2002 if (control_flow_insn_p (x))
2004 error ("in basic block %d:", bb->index);
2005 fatal_insn ("flow control insn inside a basic block", x);
2010 /* Clean up. */
2011 return err;
2014 /* Verify the CFG and RTL consistency common for both underlying RTL and
2015 cfglayout RTL.
2017 Currently it does following checks:
2018 - all checks of rtl_verify_flow_info_1
2019 - test head/end pointers
2020 - check that all insns are in the basic blocks
2021 (except the switch handling code, barriers and notes)
2022 - check that all returns are followed by barriers
2023 - check that all fallthru edge points to the adjacent blocks. */
2025 static int
2026 rtl_verify_flow_info (void)
2028 basic_block bb;
2029 int err = rtl_verify_flow_info_1 ();
2030 rtx x;
2031 rtx last_head = get_last_insn ();
2032 basic_block *bb_info;
2033 int num_bb_notes;
2034 const rtx rtx_first = get_insns ();
2035 basic_block last_bb_seen = ENTRY_BLOCK_PTR, curr_bb = NULL;
2036 const int max_uid = get_max_uid ();
2038 bb_info = XCNEWVEC (basic_block, max_uid);
2040 FOR_EACH_BB_REVERSE (bb)
2042 edge e;
2043 rtx head = BB_HEAD (bb);
2044 rtx end = BB_END (bb);
2046 for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
2048 /* Verify the end of the basic block is in the INSN chain. */
2049 if (x == end)
2050 break;
2052 /* And that the code outside of basic blocks has NULL bb field. */
2053 if (!BARRIER_P (x)
2054 && BLOCK_FOR_INSN (x) != NULL)
2056 error ("insn %d outside of basic blocks has non-NULL bb field",
2057 INSN_UID (x));
2058 err = 1;
2062 if (!x)
2064 error ("end insn %d for block %d not found in the insn stream",
2065 INSN_UID (end), bb->index);
2066 err = 1;
2069 /* Work backwards from the end to the head of the basic block
2070 to verify the head is in the RTL chain. */
2071 for (; x != NULL_RTX; x = PREV_INSN (x))
2073 /* While walking over the insn chain, verify insns appear
2074 in only one basic block. */
2075 if (bb_info[INSN_UID (x)] != NULL)
2077 error ("insn %d is in multiple basic blocks (%d and %d)",
2078 INSN_UID (x), bb->index, bb_info[INSN_UID (x)]->index);
2079 err = 1;
2082 bb_info[INSN_UID (x)] = bb;
2084 if (x == head)
2085 break;
2087 if (!x)
2089 error ("head insn %d for block %d not found in the insn stream",
2090 INSN_UID (head), bb->index);
2091 err = 1;
2094 last_head = PREV_INSN (x);
2096 e = find_fallthru_edge (bb->succs);
2097 if (!e)
2099 rtx insn;
2101 /* Ensure existence of barrier in BB with no fallthru edges. */
2102 for (insn = NEXT_INSN (BB_END (bb)); ; insn = NEXT_INSN (insn))
2104 if (!insn || NOTE_INSN_BASIC_BLOCK_P (insn))
2106 error ("missing barrier after block %i", bb->index);
2107 err = 1;
2108 break;
2110 if (BARRIER_P (insn))
2111 break;
2114 else if (e->src != ENTRY_BLOCK_PTR
2115 && e->dest != EXIT_BLOCK_PTR)
2117 rtx insn;
2119 if (e->src->next_bb != e->dest)
2121 error
2122 ("verify_flow_info: Incorrect blocks for fallthru %i->%i",
2123 e->src->index, e->dest->index);
2124 err = 1;
2126 else
2127 for (insn = NEXT_INSN (BB_END (e->src)); insn != BB_HEAD (e->dest);
2128 insn = NEXT_INSN (insn))
2129 if (BARRIER_P (insn) || INSN_P (insn))
2131 error ("verify_flow_info: Incorrect fallthru %i->%i",
2132 e->src->index, e->dest->index);
2133 fatal_insn ("wrong insn in the fallthru edge", insn);
2134 err = 1;
2139 for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
2141 /* Check that the code before the first basic block has NULL
2142 bb field. */
2143 if (!BARRIER_P (x)
2144 && BLOCK_FOR_INSN (x) != NULL)
2146 error ("insn %d outside of basic blocks has non-NULL bb field",
2147 INSN_UID (x));
2148 err = 1;
2151 free (bb_info);
2153 num_bb_notes = 0;
2154 last_bb_seen = ENTRY_BLOCK_PTR;
2156 for (x = rtx_first; x; x = NEXT_INSN (x))
2158 if (NOTE_INSN_BASIC_BLOCK_P (x))
2160 bb = NOTE_BASIC_BLOCK (x);
2162 num_bb_notes++;
2163 if (bb != last_bb_seen->next_bb)
2164 internal_error ("basic blocks not laid down consecutively");
2166 curr_bb = last_bb_seen = bb;
2169 if (!curr_bb)
2171 switch (GET_CODE (x))
2173 case BARRIER:
2174 case NOTE:
2175 break;
2177 case CODE_LABEL:
2178 /* An addr_vec is placed outside any basic block. */
2179 if (NEXT_INSN (x)
2180 && JUMP_TABLE_DATA_P (NEXT_INSN (x)))
2181 x = NEXT_INSN (x);
2183 /* But in any case, non-deletable labels can appear anywhere. */
2184 break;
2186 default:
2187 fatal_insn ("insn outside basic block", x);
2191 if (JUMP_P (x)
2192 && returnjump_p (x) && ! condjump_p (x)
2193 && ! (next_nonnote_insn (x) && BARRIER_P (next_nonnote_insn (x))))
2194 fatal_insn ("return not followed by barrier", x);
2195 if (curr_bb && x == BB_END (curr_bb))
2196 curr_bb = NULL;
2199 if (num_bb_notes != n_basic_blocks - NUM_FIXED_BLOCKS)
2200 internal_error
2201 ("number of bb notes in insn chain (%d) != n_basic_blocks (%d)",
2202 num_bb_notes, n_basic_blocks);
2204 return err;
2207 /* Assume that the preceding pass has possibly eliminated jump instructions
2208 or converted the unconditional jumps. Eliminate the edges from CFG.
2209 Return true if any edges are eliminated. */
2211 bool
2212 purge_dead_edges (basic_block bb)
2214 edge e;
2215 rtx insn = BB_END (bb), note;
2216 bool purged = false;
2217 bool found;
2218 edge_iterator ei;
2220 if (DEBUG_INSN_P (insn) && insn != BB_HEAD (bb))
2222 insn = PREV_INSN (insn);
2223 while ((DEBUG_INSN_P (insn) || NOTE_P (insn)) && insn != BB_HEAD (bb));
2225 /* If this instruction cannot trap, remove REG_EH_REGION notes. */
2226 if (NONJUMP_INSN_P (insn)
2227 && (note = find_reg_note (insn, REG_EH_REGION, NULL)))
2229 rtx eqnote;
2231 if (! may_trap_p (PATTERN (insn))
2232 || ((eqnote = find_reg_equal_equiv_note (insn))
2233 && ! may_trap_p (XEXP (eqnote, 0))))
2234 remove_note (insn, note);
2237 /* Cleanup abnormal edges caused by exceptions or non-local gotos. */
2238 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2240 bool remove = false;
2242 /* There are three types of edges we need to handle correctly here: EH
2243 edges, abnormal call EH edges, and abnormal call non-EH edges. The
2244 latter can appear when nonlocal gotos are used. */
2245 if (e->flags & EDGE_ABNORMAL_CALL)
2247 if (!CALL_P (insn))
2248 remove = true;
2249 else if (can_nonlocal_goto (insn))
2251 else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
2253 else
2254 remove = true;
2256 else if (e->flags & EDGE_EH)
2257 remove = !can_throw_internal (insn);
2259 if (remove)
2261 remove_edge (e);
2262 df_set_bb_dirty (bb);
2263 purged = true;
2265 else
2266 ei_next (&ei);
2269 if (JUMP_P (insn))
2271 rtx note;
2272 edge b,f;
2273 edge_iterator ei;
2275 /* We do care only about conditional jumps and simplejumps. */
2276 if (!any_condjump_p (insn)
2277 && !returnjump_p (insn)
2278 && !simplejump_p (insn))
2279 return purged;
2281 /* Branch probability/prediction notes are defined only for
2282 condjumps. We've possibly turned condjump into simplejump. */
2283 if (simplejump_p (insn))
2285 note = find_reg_note (insn, REG_BR_PROB, NULL);
2286 if (note)
2287 remove_note (insn, note);
2288 while ((note = find_reg_note (insn, REG_BR_PRED, NULL)))
2289 remove_note (insn, note);
2292 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2294 /* Avoid abnormal flags to leak from computed jumps turned
2295 into simplejumps. */
2297 e->flags &= ~EDGE_ABNORMAL;
2299 /* See if this edge is one we should keep. */
2300 if ((e->flags & EDGE_FALLTHRU) && any_condjump_p (insn))
2301 /* A conditional jump can fall through into the next
2302 block, so we should keep the edge. */
2304 ei_next (&ei);
2305 continue;
2307 else if (e->dest != EXIT_BLOCK_PTR
2308 && BB_HEAD (e->dest) == JUMP_LABEL (insn))
2309 /* If the destination block is the target of the jump,
2310 keep the edge. */
2312 ei_next (&ei);
2313 continue;
2315 else if (e->dest == EXIT_BLOCK_PTR && returnjump_p (insn))
2316 /* If the destination block is the exit block, and this
2317 instruction is a return, then keep the edge. */
2319 ei_next (&ei);
2320 continue;
2322 else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
2323 /* Keep the edges that correspond to exceptions thrown by
2324 this instruction and rematerialize the EDGE_ABNORMAL
2325 flag we just cleared above. */
2327 e->flags |= EDGE_ABNORMAL;
2328 ei_next (&ei);
2329 continue;
2332 /* We do not need this edge. */
2333 df_set_bb_dirty (bb);
2334 purged = true;
2335 remove_edge (e);
2338 if (EDGE_COUNT (bb->succs) == 0 || !purged)
2339 return purged;
2341 if (dump_file)
2342 fprintf (dump_file, "Purged edges from bb %i\n", bb->index);
2344 if (!optimize)
2345 return purged;
2347 /* Redistribute probabilities. */
2348 if (single_succ_p (bb))
2350 single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
2351 single_succ_edge (bb)->count = bb->count;
2353 else
2355 note = find_reg_note (insn, REG_BR_PROB, NULL);
2356 if (!note)
2357 return purged;
2359 b = BRANCH_EDGE (bb);
2360 f = FALLTHRU_EDGE (bb);
2361 b->probability = INTVAL (XEXP (note, 0));
2362 f->probability = REG_BR_PROB_BASE - b->probability;
2363 b->count = bb->count * b->probability / REG_BR_PROB_BASE;
2364 f->count = bb->count * f->probability / REG_BR_PROB_BASE;
2367 return purged;
2369 else if (CALL_P (insn) && SIBLING_CALL_P (insn))
2371 /* First, there should not be any EH or ABCALL edges resulting
2372 from non-local gotos and the like. If there were, we shouldn't
2373 have created the sibcall in the first place. Second, there
2374 should of course never have been a fallthru edge. */
2375 gcc_assert (single_succ_p (bb));
2376 gcc_assert (single_succ_edge (bb)->flags
2377 == (EDGE_SIBCALL | EDGE_ABNORMAL));
2379 return 0;
2382 /* If we don't see a jump insn, we don't know exactly why the block would
2383 have been broken at this point. Look for a simple, non-fallthru edge,
2384 as these are only created by conditional branches. If we find such an
2385 edge we know that there used to be a jump here and can then safely
2386 remove all non-fallthru edges. */
2387 found = false;
2388 FOR_EACH_EDGE (e, ei, bb->succs)
2389 if (! (e->flags & (EDGE_COMPLEX | EDGE_FALLTHRU)))
2391 found = true;
2392 break;
2395 if (!found)
2396 return purged;
2398 /* Remove all but the fake and fallthru edges. The fake edge may be
2399 the only successor for this block in the case of noreturn
2400 calls. */
2401 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2403 if (!(e->flags & (EDGE_FALLTHRU | EDGE_FAKE)))
2405 df_set_bb_dirty (bb);
2406 remove_edge (e);
2407 purged = true;
2409 else
2410 ei_next (&ei);
2413 gcc_assert (single_succ_p (bb));
2415 single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
2416 single_succ_edge (bb)->count = bb->count;
2418 if (dump_file)
2419 fprintf (dump_file, "Purged non-fallthru edges from bb %i\n",
2420 bb->index);
2421 return purged;
2424 /* Search all basic blocks for potentially dead edges and purge them. Return
2425 true if some edge has been eliminated. */
2427 bool
2428 purge_all_dead_edges (void)
2430 int purged = false;
2431 basic_block bb;
2433 FOR_EACH_BB (bb)
2435 bool purged_here = purge_dead_edges (bb);
2437 purged |= purged_here;
2440 return purged;
2443 /* This is used by a few passes that emit some instructions after abnormal
2444 calls, moving the basic block's end, while they in fact do want to emit
2445 them on the fallthru edge. Look for abnormal call edges, find backward
2446 the call in the block and insert the instructions on the edge instead.
2448 Similarly, handle instructions throwing exceptions internally.
2450 Return true when instructions have been found and inserted on edges. */
2452 bool
2453 fixup_abnormal_edges (void)
2455 bool inserted = false;
2456 basic_block bb;
2458 FOR_EACH_BB (bb)
2460 edge e;
2461 edge_iterator ei;
2463 /* Look for cases we are interested in - calls or instructions causing
2464 exceptions. */
2465 FOR_EACH_EDGE (e, ei, bb->succs)
2466 if ((e->flags & EDGE_ABNORMAL_CALL)
2467 || ((e->flags & (EDGE_ABNORMAL | EDGE_EH))
2468 == (EDGE_ABNORMAL | EDGE_EH)))
2469 break;
2471 if (e && !CALL_P (BB_END (bb)) && !can_throw_internal (BB_END (bb)))
2473 rtx insn;
2475 /* Get past the new insns generated. Allow notes, as the insns
2476 may be already deleted. */
2477 insn = BB_END (bb);
2478 while ((NONJUMP_INSN_P (insn) || NOTE_P (insn))
2479 && !can_throw_internal (insn)
2480 && insn != BB_HEAD (bb))
2481 insn = PREV_INSN (insn);
2483 if (CALL_P (insn) || can_throw_internal (insn))
2485 rtx stop, next;
2487 e = find_fallthru_edge (bb->succs);
2489 stop = NEXT_INSN (BB_END (bb));
2490 BB_END (bb) = insn;
2492 for (insn = NEXT_INSN (insn); insn != stop; insn = next)
2494 next = NEXT_INSN (insn);
2495 if (INSN_P (insn))
2497 delete_insn (insn);
2499 /* Sometimes there's still the return value USE.
2500 If it's placed after a trapping call (i.e. that
2501 call is the last insn anyway), we have no fallthru
2502 edge. Simply delete this use and don't try to insert
2503 on the non-existent edge. */
2504 if (GET_CODE (PATTERN (insn)) != USE)
2506 /* We're not deleting it, we're moving it. */
2507 INSN_DELETED_P (insn) = 0;
2508 PREV_INSN (insn) = NULL_RTX;
2509 NEXT_INSN (insn) = NULL_RTX;
2511 insert_insn_on_edge (insn, e);
2512 inserted = true;
2515 else if (!BARRIER_P (insn))
2516 set_block_for_insn (insn, NULL);
2520 /* It may be that we don't find any trapping insn. In this
2521 case we discovered quite late that the insn that had been
2522 marked as can_throw_internal in fact couldn't trap at all.
2523 So we should in fact delete the EH edges out of the block. */
2524 else
2525 purge_dead_edges (bb);
2529 return inserted;
2532 /* Same as split_block but update cfg_layout structures. */
2534 static basic_block
2535 cfg_layout_split_block (basic_block bb, void *insnp)
2537 rtx insn = (rtx) insnp;
2538 basic_block new_bb = rtl_split_block (bb, insn);
2540 new_bb->il.rtl->footer = bb->il.rtl->footer;
2541 bb->il.rtl->footer = NULL;
2543 return new_bb;
2546 /* Redirect Edge to DEST. */
2547 static edge
2548 cfg_layout_redirect_edge_and_branch (edge e, basic_block dest)
2550 basic_block src = e->src;
2551 edge ret;
2553 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
2554 return NULL;
2556 if (e->dest == dest)
2557 return e;
2559 if (e->src != ENTRY_BLOCK_PTR
2560 && (ret = try_redirect_by_replacing_jump (e, dest, true)))
2562 df_set_bb_dirty (src);
2563 return ret;
2566 if (e->src == ENTRY_BLOCK_PTR
2567 && (e->flags & EDGE_FALLTHRU) && !(e->flags & EDGE_COMPLEX))
2569 if (dump_file)
2570 fprintf (dump_file, "Redirecting entry edge from bb %i to %i\n",
2571 e->src->index, dest->index);
2573 df_set_bb_dirty (e->src);
2574 redirect_edge_succ (e, dest);
2575 return e;
2578 /* Redirect_edge_and_branch may decide to turn branch into fallthru edge
2579 in the case the basic block appears to be in sequence. Avoid this
2580 transformation. */
2582 if (e->flags & EDGE_FALLTHRU)
2584 /* Redirect any branch edges unified with the fallthru one. */
2585 if (JUMP_P (BB_END (src))
2586 && label_is_jump_target_p (BB_HEAD (e->dest),
2587 BB_END (src)))
2589 edge redirected;
2591 if (dump_file)
2592 fprintf (dump_file, "Fallthru edge unified with branch "
2593 "%i->%i redirected to %i\n",
2594 e->src->index, e->dest->index, dest->index);
2595 e->flags &= ~EDGE_FALLTHRU;
2596 redirected = redirect_branch_edge (e, dest);
2597 gcc_assert (redirected);
2598 redirected->flags |= EDGE_FALLTHRU;
2599 df_set_bb_dirty (redirected->src);
2600 return redirected;
2602 /* In case we are redirecting fallthru edge to the branch edge
2603 of conditional jump, remove it. */
2604 if (EDGE_COUNT (src->succs) == 2)
2606 /* Find the edge that is different from E. */
2607 edge s = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e);
2609 if (s->dest == dest
2610 && any_condjump_p (BB_END (src))
2611 && onlyjump_p (BB_END (src)))
2612 delete_insn (BB_END (src));
2614 if (dump_file)
2615 fprintf (dump_file, "Fallthru edge %i->%i redirected to %i\n",
2616 e->src->index, e->dest->index, dest->index);
2617 ret = redirect_edge_succ_nodup (e, dest);
2619 else
2620 ret = redirect_branch_edge (e, dest);
2622 /* We don't want simplejumps in the insn stream during cfglayout. */
2623 gcc_assert (!simplejump_p (BB_END (src)));
2625 df_set_bb_dirty (src);
2626 return ret;
2629 /* Simple wrapper as we always can redirect fallthru edges. */
2630 static basic_block
2631 cfg_layout_redirect_edge_and_branch_force (edge e, basic_block dest)
2633 edge redirected = cfg_layout_redirect_edge_and_branch (e, dest);
2635 gcc_assert (redirected);
2636 return NULL;
2639 /* Same as delete_basic_block but update cfg_layout structures. */
2641 static void
2642 cfg_layout_delete_block (basic_block bb)
2644 rtx insn, next, prev = PREV_INSN (BB_HEAD (bb)), *to, remaints;
2646 if (bb->il.rtl->header)
2648 next = BB_HEAD (bb);
2649 if (prev)
2650 NEXT_INSN (prev) = bb->il.rtl->header;
2651 else
2652 set_first_insn (bb->il.rtl->header);
2653 PREV_INSN (bb->il.rtl->header) = prev;
2654 insn = bb->il.rtl->header;
2655 while (NEXT_INSN (insn))
2656 insn = NEXT_INSN (insn);
2657 NEXT_INSN (insn) = next;
2658 PREV_INSN (next) = insn;
2660 next = NEXT_INSN (BB_END (bb));
2661 if (bb->il.rtl->footer)
2663 insn = bb->il.rtl->footer;
2664 while (insn)
2666 if (BARRIER_P (insn))
2668 if (PREV_INSN (insn))
2669 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2670 else
2671 bb->il.rtl->footer = NEXT_INSN (insn);
2672 if (NEXT_INSN (insn))
2673 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2675 if (LABEL_P (insn))
2676 break;
2677 insn = NEXT_INSN (insn);
2679 if (bb->il.rtl->footer)
2681 insn = BB_END (bb);
2682 NEXT_INSN (insn) = bb->il.rtl->footer;
2683 PREV_INSN (bb->il.rtl->footer) = insn;
2684 while (NEXT_INSN (insn))
2685 insn = NEXT_INSN (insn);
2686 NEXT_INSN (insn) = next;
2687 if (next)
2688 PREV_INSN (next) = insn;
2689 else
2690 set_last_insn (insn);
2693 if (bb->next_bb != EXIT_BLOCK_PTR)
2694 to = &bb->next_bb->il.rtl->header;
2695 else
2696 to = &cfg_layout_function_footer;
2698 rtl_delete_block (bb);
2700 if (prev)
2701 prev = NEXT_INSN (prev);
2702 else
2703 prev = get_insns ();
2704 if (next)
2705 next = PREV_INSN (next);
2706 else
2707 next = get_last_insn ();
2709 if (next && NEXT_INSN (next) != prev)
2711 remaints = unlink_insn_chain (prev, next);
2712 insn = remaints;
2713 while (NEXT_INSN (insn))
2714 insn = NEXT_INSN (insn);
2715 NEXT_INSN (insn) = *to;
2716 if (*to)
2717 PREV_INSN (*to) = insn;
2718 *to = remaints;
2722 /* Return true when blocks A and B can be safely merged. */
2724 static bool
2725 cfg_layout_can_merge_blocks_p (basic_block a, basic_block b)
2727 /* If we are partitioning hot/cold basic blocks, we don't want to
2728 mess up unconditional or indirect jumps that cross between hot
2729 and cold sections.
2731 Basic block partitioning may result in some jumps that appear to
2732 be optimizable (or blocks that appear to be mergeable), but which really
2733 must be left untouched (they are required to make it safely across
2734 partition boundaries). See the comments at the top of
2735 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
2737 if (BB_PARTITION (a) != BB_PARTITION (b))
2738 return false;
2740 /* There must be exactly one edge in between the blocks. */
2741 return (single_succ_p (a)
2742 && single_succ (a) == b
2743 && single_pred_p (b) == 1
2744 && a != b
2745 /* Must be simple edge. */
2746 && !(single_succ_edge (a)->flags & EDGE_COMPLEX)
2747 && a != ENTRY_BLOCK_PTR && b != EXIT_BLOCK_PTR
2748 /* If the jump insn has side effects, we can't kill the edge.
2749 When not optimizing, try_redirect_by_replacing_jump will
2750 not allow us to redirect an edge by replacing a table jump. */
2751 && (!JUMP_P (BB_END (a))
2752 || ((!optimize || reload_completed)
2753 ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
2756 /* Merge block A and B. The blocks must be mergeable. */
2758 static void
2759 cfg_layout_merge_blocks (basic_block a, basic_block b)
2761 bool forwarder_p = (b->flags & BB_FORWARDER_BLOCK) != 0;
2763 gcc_checking_assert (cfg_layout_can_merge_blocks_p (a, b));
2765 if (dump_file)
2766 fprintf (dump_file, "Merging block %d into block %d...\n", b->index,
2767 a->index);
2769 /* If there was a CODE_LABEL beginning B, delete it. */
2770 if (LABEL_P (BB_HEAD (b)))
2772 delete_insn (BB_HEAD (b));
2775 /* We should have fallthru edge in a, or we can do dummy redirection to get
2776 it cleaned up. */
2777 if (JUMP_P (BB_END (a)))
2778 try_redirect_by_replacing_jump (EDGE_SUCC (a, 0), b, true);
2779 gcc_assert (!JUMP_P (BB_END (a)));
2781 /* When not optimizing and the edge is the only place in RTL which holds
2782 some unique locus, emit a nop with that locus in between. */
2783 if (!optimize && EDGE_SUCC (a, 0)->goto_locus)
2785 rtx insn = BB_END (a), end = PREV_INSN (BB_HEAD (a));
2786 int goto_locus = EDGE_SUCC (a, 0)->goto_locus;
2788 while (insn != end && (!INSN_P (insn) || INSN_LOCATOR (insn) == 0))
2789 insn = PREV_INSN (insn);
2790 if (insn != end && locator_eq (INSN_LOCATOR (insn), goto_locus))
2791 goto_locus = 0;
2792 else
2794 insn = BB_HEAD (b);
2795 end = NEXT_INSN (BB_END (b));
2796 while (insn != end && !INSN_P (insn))
2797 insn = NEXT_INSN (insn);
2798 if (insn != end && INSN_LOCATOR (insn) != 0
2799 && locator_eq (INSN_LOCATOR (insn), goto_locus))
2800 goto_locus = 0;
2802 if (goto_locus)
2804 BB_END (a) = emit_insn_after_noloc (gen_nop (), BB_END (a), a);
2805 INSN_LOCATOR (BB_END (a)) = goto_locus;
2809 /* Possible line number notes should appear in between. */
2810 if (b->il.rtl->header)
2812 rtx first = BB_END (a), last;
2814 last = emit_insn_after_noloc (b->il.rtl->header, BB_END (a), a);
2815 delete_insn_chain (NEXT_INSN (first), last, false);
2816 b->il.rtl->header = NULL;
2819 /* In the case basic blocks are not adjacent, move them around. */
2820 if (NEXT_INSN (BB_END (a)) != BB_HEAD (b))
2822 rtx first = unlink_insn_chain (BB_HEAD (b), BB_END (b));
2824 emit_insn_after_noloc (first, BB_END (a), a);
2825 /* Skip possible DELETED_LABEL insn. */
2826 if (!NOTE_INSN_BASIC_BLOCK_P (first))
2827 first = NEXT_INSN (first);
2828 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (first));
2829 BB_HEAD (b) = NULL;
2831 /* emit_insn_after_noloc doesn't call df_insn_change_bb.
2832 We need to explicitly call. */
2833 update_bb_for_insn_chain (NEXT_INSN (first),
2834 BB_END (b),
2837 delete_insn (first);
2839 /* Otherwise just re-associate the instructions. */
2840 else
2842 rtx insn;
2844 update_bb_for_insn_chain (BB_HEAD (b), BB_END (b), a);
2846 insn = BB_HEAD (b);
2847 /* Skip possible DELETED_LABEL insn. */
2848 if (!NOTE_INSN_BASIC_BLOCK_P (insn))
2849 insn = NEXT_INSN (insn);
2850 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
2851 BB_HEAD (b) = NULL;
2852 BB_END (a) = BB_END (b);
2853 delete_insn (insn);
2856 df_bb_delete (b->index);
2858 /* Possible tablejumps and barriers should appear after the block. */
2859 if (b->il.rtl->footer)
2861 if (!a->il.rtl->footer)
2862 a->il.rtl->footer = b->il.rtl->footer;
2863 else
2865 rtx last = a->il.rtl->footer;
2867 while (NEXT_INSN (last))
2868 last = NEXT_INSN (last);
2869 NEXT_INSN (last) = b->il.rtl->footer;
2870 PREV_INSN (b->il.rtl->footer) = last;
2872 b->il.rtl->footer = NULL;
2875 /* If B was a forwarder block, propagate the locus on the edge. */
2876 if (forwarder_p && !EDGE_SUCC (b, 0)->goto_locus)
2877 EDGE_SUCC (b, 0)->goto_locus = EDGE_SUCC (a, 0)->goto_locus;
2879 if (dump_file)
2880 fprintf (dump_file, "Merged blocks %d and %d.\n", a->index, b->index);
2883 /* Split edge E. */
2885 static basic_block
2886 cfg_layout_split_edge (edge e)
2888 basic_block new_bb =
2889 create_basic_block (e->src != ENTRY_BLOCK_PTR
2890 ? NEXT_INSN (BB_END (e->src)) : get_insns (),
2891 NULL_RTX, e->src);
2893 if (e->dest == EXIT_BLOCK_PTR)
2894 BB_COPY_PARTITION (new_bb, e->src);
2895 else
2896 BB_COPY_PARTITION (new_bb, e->dest);
2897 make_edge (new_bb, e->dest, EDGE_FALLTHRU);
2898 redirect_edge_and_branch_force (e, new_bb);
2900 return new_bb;
2903 /* Do postprocessing after making a forwarder block joined by edge FALLTHRU. */
2905 static void
2906 rtl_make_forwarder_block (edge fallthru ATTRIBUTE_UNUSED)
2910 /* Return 1 if BB ends with a call, possibly followed by some
2911 instructions that must stay with the call, 0 otherwise. */
2913 static bool
2914 rtl_block_ends_with_call_p (basic_block bb)
2916 rtx insn = BB_END (bb);
2918 while (!CALL_P (insn)
2919 && insn != BB_HEAD (bb)
2920 && (keep_with_call_p (insn)
2921 || NOTE_P (insn)
2922 || DEBUG_INSN_P (insn)))
2923 insn = PREV_INSN (insn);
2924 return (CALL_P (insn));
2927 /* Return 1 if BB ends with a conditional branch, 0 otherwise. */
2929 static bool
2930 rtl_block_ends_with_condjump_p (const_basic_block bb)
2932 return any_condjump_p (BB_END (bb));
2935 /* Return true if we need to add fake edge to exit.
2936 Helper function for rtl_flow_call_edges_add. */
2938 static bool
2939 need_fake_edge_p (const_rtx insn)
2941 if (!INSN_P (insn))
2942 return false;
2944 if ((CALL_P (insn)
2945 && !SIBLING_CALL_P (insn)
2946 && !find_reg_note (insn, REG_NORETURN, NULL)
2947 && !(RTL_CONST_OR_PURE_CALL_P (insn))))
2948 return true;
2950 return ((GET_CODE (PATTERN (insn)) == ASM_OPERANDS
2951 && MEM_VOLATILE_P (PATTERN (insn)))
2952 || (GET_CODE (PATTERN (insn)) == PARALLEL
2953 && asm_noperands (insn) != -1
2954 && MEM_VOLATILE_P (XVECEXP (PATTERN (insn), 0, 0)))
2955 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
2958 /* Add fake edges to the function exit for any non constant and non noreturn
2959 calls, volatile inline assembly in the bitmap of blocks specified by
2960 BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
2961 that were split.
2963 The goal is to expose cases in which entering a basic block does not imply
2964 that all subsequent instructions must be executed. */
2966 static int
2967 rtl_flow_call_edges_add (sbitmap blocks)
2969 int i;
2970 int blocks_split = 0;
2971 int last_bb = last_basic_block;
2972 bool check_last_block = false;
2974 if (n_basic_blocks == NUM_FIXED_BLOCKS)
2975 return 0;
2977 if (! blocks)
2978 check_last_block = true;
2979 else
2980 check_last_block = TEST_BIT (blocks, EXIT_BLOCK_PTR->prev_bb->index);
2982 /* In the last basic block, before epilogue generation, there will be
2983 a fallthru edge to EXIT. Special care is required if the last insn
2984 of the last basic block is a call because make_edge folds duplicate
2985 edges, which would result in the fallthru edge also being marked
2986 fake, which would result in the fallthru edge being removed by
2987 remove_fake_edges, which would result in an invalid CFG.
2989 Moreover, we can't elide the outgoing fake edge, since the block
2990 profiler needs to take this into account in order to solve the minimal
2991 spanning tree in the case that the call doesn't return.
2993 Handle this by adding a dummy instruction in a new last basic block. */
2994 if (check_last_block)
2996 basic_block bb = EXIT_BLOCK_PTR->prev_bb;
2997 rtx insn = BB_END (bb);
2999 /* Back up past insns that must be kept in the same block as a call. */
3000 while (insn != BB_HEAD (bb)
3001 && keep_with_call_p (insn))
3002 insn = PREV_INSN (insn);
3004 if (need_fake_edge_p (insn))
3006 edge e;
3008 e = find_edge (bb, EXIT_BLOCK_PTR);
3009 if (e)
3011 insert_insn_on_edge (gen_use (const0_rtx), e);
3012 commit_edge_insertions ();
3017 /* Now add fake edges to the function exit for any non constant
3018 calls since there is no way that we can determine if they will
3019 return or not... */
3021 for (i = NUM_FIXED_BLOCKS; i < last_bb; i++)
3023 basic_block bb = BASIC_BLOCK (i);
3024 rtx insn;
3025 rtx prev_insn;
3027 if (!bb)
3028 continue;
3030 if (blocks && !TEST_BIT (blocks, i))
3031 continue;
3033 for (insn = BB_END (bb); ; insn = prev_insn)
3035 prev_insn = PREV_INSN (insn);
3036 if (need_fake_edge_p (insn))
3038 edge e;
3039 rtx split_at_insn = insn;
3041 /* Don't split the block between a call and an insn that should
3042 remain in the same block as the call. */
3043 if (CALL_P (insn))
3044 while (split_at_insn != BB_END (bb)
3045 && keep_with_call_p (NEXT_INSN (split_at_insn)))
3046 split_at_insn = NEXT_INSN (split_at_insn);
3048 /* The handling above of the final block before the epilogue
3049 should be enough to verify that there is no edge to the exit
3050 block in CFG already. Calling make_edge in such case would
3051 cause us to mark that edge as fake and remove it later. */
3053 #ifdef ENABLE_CHECKING
3054 if (split_at_insn == BB_END (bb))
3056 e = find_edge (bb, EXIT_BLOCK_PTR);
3057 gcc_assert (e == NULL);
3059 #endif
3061 /* Note that the following may create a new basic block
3062 and renumber the existing basic blocks. */
3063 if (split_at_insn != BB_END (bb))
3065 e = split_block (bb, split_at_insn);
3066 if (e)
3067 blocks_split++;
3070 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
3073 if (insn == BB_HEAD (bb))
3074 break;
3078 if (blocks_split)
3079 verify_flow_info ();
3081 return blocks_split;
3084 /* Add COMP_RTX as a condition at end of COND_BB. FIRST_HEAD is
3085 the conditional branch target, SECOND_HEAD should be the fall-thru
3086 there is no need to handle this here the loop versioning code handles
3087 this. the reason for SECON_HEAD is that it is needed for condition
3088 in trees, and this should be of the same type since it is a hook. */
3089 static void
3090 rtl_lv_add_condition_to_bb (basic_block first_head ,
3091 basic_block second_head ATTRIBUTE_UNUSED,
3092 basic_block cond_bb, void *comp_rtx)
3094 rtx label, seq, jump;
3095 rtx op0 = XEXP ((rtx)comp_rtx, 0);
3096 rtx op1 = XEXP ((rtx)comp_rtx, 1);
3097 enum rtx_code comp = GET_CODE ((rtx)comp_rtx);
3098 enum machine_mode mode;
3101 label = block_label (first_head);
3102 mode = GET_MODE (op0);
3103 if (mode == VOIDmode)
3104 mode = GET_MODE (op1);
3106 start_sequence ();
3107 op0 = force_operand (op0, NULL_RTX);
3108 op1 = force_operand (op1, NULL_RTX);
3109 do_compare_rtx_and_jump (op0, op1, comp, 0,
3110 mode, NULL_RTX, NULL_RTX, label, -1);
3111 jump = get_last_insn ();
3112 JUMP_LABEL (jump) = label;
3113 LABEL_NUSES (label)++;
3114 seq = get_insns ();
3115 end_sequence ();
3117 /* Add the new cond , in the new head. */
3118 emit_insn_after(seq, BB_END(cond_bb));
3122 /* Given a block B with unconditional branch at its end, get the
3123 store the return the branch edge and the fall-thru edge in
3124 BRANCH_EDGE and FALLTHRU_EDGE respectively. */
3125 static void
3126 rtl_extract_cond_bb_edges (basic_block b, edge *branch_edge,
3127 edge *fallthru_edge)
3129 edge e = EDGE_SUCC (b, 0);
3131 if (e->flags & EDGE_FALLTHRU)
3133 *fallthru_edge = e;
3134 *branch_edge = EDGE_SUCC (b, 1);
3136 else
3138 *branch_edge = e;
3139 *fallthru_edge = EDGE_SUCC (b, 1);
3143 void
3144 init_rtl_bb_info (basic_block bb)
3146 gcc_assert (!bb->il.rtl);
3147 bb->il.rtl = ggc_alloc_cleared_rtl_bb_info ();
3150 /* Returns true if it is possible to remove edge E by redirecting
3151 it to the destination of the other edge from E->src. */
3153 static bool
3154 rtl_can_remove_branch_p (const_edge e)
3156 const_basic_block src = e->src;
3157 const_basic_block target = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest;
3158 const_rtx insn = BB_END (src), set;
3160 /* The conditions are taken from try_redirect_by_replacing_jump. */
3161 if (target == EXIT_BLOCK_PTR)
3162 return false;
3164 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
3165 return false;
3167 if (find_reg_note (insn, REG_CROSSING_JUMP, NULL_RTX)
3168 || BB_PARTITION (src) != BB_PARTITION (target))
3169 return false;
3171 if (!onlyjump_p (insn)
3172 || tablejump_p (insn, NULL, NULL))
3173 return false;
3175 set = single_set (insn);
3176 if (!set || side_effects_p (set))
3177 return false;
3179 return true;
3182 /* Implementation of CFG manipulation for linearized RTL. */
3183 struct cfg_hooks rtl_cfg_hooks = {
3184 "rtl",
3185 rtl_verify_flow_info,
3186 rtl_dump_bb,
3187 rtl_create_basic_block,
3188 rtl_redirect_edge_and_branch,
3189 rtl_redirect_edge_and_branch_force,
3190 rtl_can_remove_branch_p,
3191 rtl_delete_block,
3192 rtl_split_block,
3193 rtl_move_block_after,
3194 rtl_can_merge_blocks, /* can_merge_blocks_p */
3195 rtl_merge_blocks,
3196 rtl_predict_edge,
3197 rtl_predicted_by_p,
3198 NULL, /* can_duplicate_block_p */
3199 NULL, /* duplicate_block */
3200 rtl_split_edge,
3201 rtl_make_forwarder_block,
3202 rtl_tidy_fallthru_edge,
3203 rtl_force_nonfallthru,
3204 rtl_block_ends_with_call_p,
3205 rtl_block_ends_with_condjump_p,
3206 rtl_flow_call_edges_add,
3207 NULL, /* execute_on_growing_pred */
3208 NULL, /* execute_on_shrinking_pred */
3209 NULL, /* duplicate loop for trees */
3210 NULL, /* lv_add_condition_to_bb */
3211 NULL, /* lv_adjust_loop_header_phi*/
3212 NULL, /* extract_cond_bb_edges */
3213 NULL /* flush_pending_stmts */
3216 /* Implementation of CFG manipulation for cfg layout RTL, where
3217 basic block connected via fallthru edges does not have to be adjacent.
3218 This representation will hopefully become the default one in future
3219 version of the compiler. */
3221 /* We do not want to declare these functions in a header file, since they
3222 should only be used through the cfghooks interface, and we do not want to
3223 move them here since it would require also moving quite a lot of related
3224 code. They are in cfglayout.c. */
3225 extern bool cfg_layout_can_duplicate_bb_p (const_basic_block);
3226 extern basic_block cfg_layout_duplicate_bb (basic_block);
3228 struct cfg_hooks cfg_layout_rtl_cfg_hooks = {
3229 "cfglayout mode",
3230 rtl_verify_flow_info_1,
3231 rtl_dump_bb,
3232 cfg_layout_create_basic_block,
3233 cfg_layout_redirect_edge_and_branch,
3234 cfg_layout_redirect_edge_and_branch_force,
3235 rtl_can_remove_branch_p,
3236 cfg_layout_delete_block,
3237 cfg_layout_split_block,
3238 rtl_move_block_after,
3239 cfg_layout_can_merge_blocks_p,
3240 cfg_layout_merge_blocks,
3241 rtl_predict_edge,
3242 rtl_predicted_by_p,
3243 cfg_layout_can_duplicate_bb_p,
3244 cfg_layout_duplicate_bb,
3245 cfg_layout_split_edge,
3246 rtl_make_forwarder_block,
3247 NULL, /* tidy_fallthru_edge */
3248 rtl_force_nonfallthru,
3249 rtl_block_ends_with_call_p,
3250 rtl_block_ends_with_condjump_p,
3251 rtl_flow_call_edges_add,
3252 NULL, /* execute_on_growing_pred */
3253 NULL, /* execute_on_shrinking_pred */
3254 duplicate_loop_to_header_edge, /* duplicate loop for trees */
3255 rtl_lv_add_condition_to_bb, /* lv_add_condition_to_bb */
3256 NULL, /* lv_adjust_loop_header_phi*/
3257 rtl_extract_cond_bb_edges, /* extract_cond_bb_edges */
3258 NULL /* flush_pending_stmts */