* configure.ac (*-*-sysv4*): Don't enable libgomp.
[official-gcc.git] / gcc / cfgrtl.c
blob9031886fb1689f40e432ded78e0d70176038548e
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 basic_block
1283 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));
1570 /* Mark the basic block for find_many_sub_basic_blocks. */
1571 if (current_ir_type () != IR_RTL_CFGLAYOUT)
1572 bb->aux = &bb->aux;
1575 /* Update the CFG for all queued instructions. */
1577 void
1578 commit_edge_insertions (void)
1580 basic_block bb;
1581 sbitmap blocks;
1582 bool changed = false;
1584 #ifdef ENABLE_CHECKING
1585 verify_flow_info ();
1586 #endif
1588 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
1590 edge e;
1591 edge_iterator ei;
1593 FOR_EACH_EDGE (e, ei, bb->succs)
1594 if (e->insns.r)
1596 changed = true;
1597 commit_one_edge_insertion (e);
1601 if (!changed)
1602 return;
1604 /* In the old rtl CFG API, it was OK to insert control flow on an
1605 edge, apparently? In cfglayout mode, this will *not* work, and
1606 the caller is responsible for making sure that control flow is
1607 valid at all times. */
1608 if (current_ir_type () == IR_RTL_CFGLAYOUT)
1609 return;
1611 blocks = sbitmap_alloc (last_basic_block);
1612 sbitmap_zero (blocks);
1613 FOR_EACH_BB (bb)
1614 if (bb->aux)
1616 SET_BIT (blocks, bb->index);
1617 /* Check for forgotten bb->aux values before commit_edge_insertions
1618 call. */
1619 gcc_assert (bb->aux == &bb->aux);
1620 bb->aux = NULL;
1622 find_many_sub_basic_blocks (blocks);
1623 sbitmap_free (blocks);
1627 /* Print out RTL-specific basic block information (live information
1628 at start and end). */
1630 static void
1631 rtl_dump_bb (basic_block bb, FILE *outf, int indent, int flags ATTRIBUTE_UNUSED)
1633 rtx insn;
1634 rtx last;
1635 char *s_indent;
1637 s_indent = (char *) alloca ((size_t) indent + 1);
1638 memset (s_indent, ' ', (size_t) indent);
1639 s_indent[indent] = '\0';
1641 if (df)
1643 df_dump_top (bb, outf);
1644 putc ('\n', outf);
1647 for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb)); insn != last;
1648 insn = NEXT_INSN (insn))
1649 print_rtl_single (outf, insn);
1651 if (df)
1653 df_dump_bottom (bb, outf);
1654 putc ('\n', outf);
1659 /* Like print_rtl, but also print out live information for the start of each
1660 basic block. */
1662 void
1663 print_rtl_with_bb (FILE *outf, const_rtx rtx_first)
1665 const_rtx tmp_rtx;
1666 if (rtx_first == 0)
1667 fprintf (outf, "(nil)\n");
1668 else
1670 enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
1671 int max_uid = get_max_uid ();
1672 basic_block *start = XCNEWVEC (basic_block, max_uid);
1673 basic_block *end = XCNEWVEC (basic_block, max_uid);
1674 enum bb_state *in_bb_p = XCNEWVEC (enum bb_state, max_uid);
1676 basic_block bb;
1678 if (df)
1679 df_dump_start (outf);
1681 FOR_EACH_BB_REVERSE (bb)
1683 rtx x;
1685 start[INSN_UID (BB_HEAD (bb))] = bb;
1686 end[INSN_UID (BB_END (bb))] = bb;
1687 for (x = BB_HEAD (bb); x != NULL_RTX; x = NEXT_INSN (x))
1689 enum bb_state state = IN_MULTIPLE_BB;
1691 if (in_bb_p[INSN_UID (x)] == NOT_IN_BB)
1692 state = IN_ONE_BB;
1693 in_bb_p[INSN_UID (x)] = state;
1695 if (x == BB_END (bb))
1696 break;
1700 for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
1702 int did_output;
1703 if ((bb = start[INSN_UID (tmp_rtx)]) != NULL)
1705 edge e;
1706 edge_iterator ei;
1708 fprintf (outf, ";; Start of basic block (");
1709 FOR_EACH_EDGE (e, ei, bb->preds)
1710 fprintf (outf, " %d", e->src->index);
1711 fprintf (outf, ") -> %d\n", bb->index);
1713 if (df)
1715 df_dump_top (bb, outf);
1716 putc ('\n', outf);
1718 FOR_EACH_EDGE (e, ei, bb->preds)
1720 fputs (";; Pred edge ", outf);
1721 dump_edge_info (outf, e, 0);
1722 fputc ('\n', outf);
1726 if (in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB
1727 && !NOTE_P (tmp_rtx)
1728 && !BARRIER_P (tmp_rtx))
1729 fprintf (outf, ";; Insn is not within a basic block\n");
1730 else if (in_bb_p[INSN_UID (tmp_rtx)] == IN_MULTIPLE_BB)
1731 fprintf (outf, ";; Insn is in multiple basic blocks\n");
1733 did_output = print_rtl_single (outf, tmp_rtx);
1735 if ((bb = end[INSN_UID (tmp_rtx)]) != NULL)
1737 edge e;
1738 edge_iterator ei;
1740 fprintf (outf, ";; End of basic block %d -> (", bb->index);
1741 FOR_EACH_EDGE (e, ei, bb->succs)
1742 fprintf (outf, " %d", e->dest->index);
1743 fprintf (outf, ")\n");
1745 if (df)
1747 df_dump_bottom (bb, outf);
1748 putc ('\n', outf);
1750 putc ('\n', outf);
1751 FOR_EACH_EDGE (e, ei, bb->succs)
1753 fputs (";; Succ edge ", outf);
1754 dump_edge_info (outf, e, 1);
1755 fputc ('\n', outf);
1758 if (did_output)
1759 putc ('\n', outf);
1762 free (start);
1763 free (end);
1764 free (in_bb_p);
1767 if (crtl->epilogue_delay_list != 0)
1769 fprintf (outf, "\n;; Insns in epilogue delay list:\n\n");
1770 for (tmp_rtx = crtl->epilogue_delay_list; tmp_rtx != 0;
1771 tmp_rtx = XEXP (tmp_rtx, 1))
1772 print_rtl_single (outf, XEXP (tmp_rtx, 0));
1776 void
1777 update_br_prob_note (basic_block bb)
1779 rtx note;
1780 if (!JUMP_P (BB_END (bb)))
1781 return;
1782 note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX);
1783 if (!note || INTVAL (XEXP (note, 0)) == BRANCH_EDGE (bb)->probability)
1784 return;
1785 XEXP (note, 0) = GEN_INT (BRANCH_EDGE (bb)->probability);
1788 /* Get the last insn associated with block BB (that includes barriers and
1789 tablejumps after BB). */
1791 get_last_bb_insn (basic_block bb)
1793 rtx tmp;
1794 rtx end = BB_END (bb);
1796 /* Include any jump table following the basic block. */
1797 if (tablejump_p (end, NULL, &tmp))
1798 end = tmp;
1800 /* Include any barriers that may follow the basic block. */
1801 tmp = next_nonnote_insn_bb (end);
1802 while (tmp && BARRIER_P (tmp))
1804 end = tmp;
1805 tmp = next_nonnote_insn_bb (end);
1808 return end;
1811 /* Verify the CFG and RTL consistency common for both underlying RTL and
1812 cfglayout RTL.
1814 Currently it does following checks:
1816 - overlapping of basic blocks
1817 - insns with wrong BLOCK_FOR_INSN pointers
1818 - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note)
1819 - tails of basic blocks (ensure that boundary is necessary)
1820 - scans body of the basic block for JUMP_INSN, CODE_LABEL
1821 and NOTE_INSN_BASIC_BLOCK
1822 - verify that no fall_thru edge crosses hot/cold partition boundaries
1823 - verify that there are no pending RTL branch predictions
1825 In future it can be extended check a lot of other stuff as well
1826 (reachability of basic blocks, life information, etc. etc.). */
1828 static int
1829 rtl_verify_flow_info_1 (void)
1831 rtx x;
1832 int err = 0;
1833 basic_block bb;
1835 /* Check the general integrity of the basic blocks. */
1836 FOR_EACH_BB_REVERSE (bb)
1838 rtx insn;
1840 if (!(bb->flags & BB_RTL))
1842 error ("BB_RTL flag not set for block %d", bb->index);
1843 err = 1;
1846 FOR_BB_INSNS (bb, insn)
1847 if (BLOCK_FOR_INSN (insn) != bb)
1849 error ("insn %d basic block pointer is %d, should be %d",
1850 INSN_UID (insn),
1851 BLOCK_FOR_INSN (insn) ? BLOCK_FOR_INSN (insn)->index : 0,
1852 bb->index);
1853 err = 1;
1856 for (insn = bb->il.rtl->header; insn; insn = NEXT_INSN (insn))
1857 if (!BARRIER_P (insn)
1858 && BLOCK_FOR_INSN (insn) != NULL)
1860 error ("insn %d in header of bb %d has non-NULL basic block",
1861 INSN_UID (insn), bb->index);
1862 err = 1;
1864 for (insn = bb->il.rtl->footer; insn; insn = NEXT_INSN (insn))
1865 if (!BARRIER_P (insn)
1866 && BLOCK_FOR_INSN (insn) != NULL)
1868 error ("insn %d in footer of bb %d has non-NULL basic block",
1869 INSN_UID (insn), bb->index);
1870 err = 1;
1874 /* Now check the basic blocks (boundaries etc.) */
1875 FOR_EACH_BB_REVERSE (bb)
1877 int n_fallthru = 0, n_eh = 0, n_call = 0, n_abnormal = 0, n_branch = 0;
1878 edge e, fallthru = NULL;
1879 rtx note;
1880 edge_iterator ei;
1882 if (JUMP_P (BB_END (bb))
1883 && (note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX))
1884 && EDGE_COUNT (bb->succs) >= 2
1885 && any_condjump_p (BB_END (bb)))
1887 if (INTVAL (XEXP (note, 0)) != BRANCH_EDGE (bb)->probability
1888 && profile_status != PROFILE_ABSENT)
1890 error ("verify_flow_info: REG_BR_PROB does not match cfg %wi %i",
1891 INTVAL (XEXP (note, 0)), BRANCH_EDGE (bb)->probability);
1892 err = 1;
1895 FOR_EACH_EDGE (e, ei, bb->succs)
1897 if (e->flags & EDGE_FALLTHRU)
1899 n_fallthru++, fallthru = e;
1900 if ((e->flags & EDGE_CROSSING)
1901 || (BB_PARTITION (e->src) != BB_PARTITION (e->dest)
1902 && e->src != ENTRY_BLOCK_PTR
1903 && e->dest != EXIT_BLOCK_PTR))
1905 error ("fallthru edge crosses section boundary (bb %i)",
1906 e->src->index);
1907 err = 1;
1911 if ((e->flags & ~(EDGE_DFS_BACK
1912 | EDGE_CAN_FALLTHRU
1913 | EDGE_IRREDUCIBLE_LOOP
1914 | EDGE_LOOP_EXIT
1915 | EDGE_CROSSING)) == 0)
1916 n_branch++;
1918 if (e->flags & EDGE_ABNORMAL_CALL)
1919 n_call++;
1921 if (e->flags & EDGE_EH)
1922 n_eh++;
1923 else if (e->flags & EDGE_ABNORMAL)
1924 n_abnormal++;
1927 if (n_eh && !find_reg_note (BB_END (bb), REG_EH_REGION, NULL_RTX))
1929 error ("missing REG_EH_REGION note in the end of bb %i", bb->index);
1930 err = 1;
1932 if (n_eh > 1)
1934 error ("too many eh edges %i", bb->index);
1935 err = 1;
1937 if (n_branch
1938 && (!JUMP_P (BB_END (bb))
1939 || (n_branch > 1 && (any_uncondjump_p (BB_END (bb))
1940 || any_condjump_p (BB_END (bb))))))
1942 error ("too many outgoing branch edges from bb %i", bb->index);
1943 err = 1;
1945 if (n_fallthru && any_uncondjump_p (BB_END (bb)))
1947 error ("fallthru edge after unconditional jump %i", bb->index);
1948 err = 1;
1950 if (n_branch != 1 && any_uncondjump_p (BB_END (bb)))
1952 error ("wrong number of branch edges after unconditional jump %i",
1953 bb->index);
1954 err = 1;
1956 if (n_branch != 1 && any_condjump_p (BB_END (bb))
1957 && JUMP_LABEL (BB_END (bb)) != BB_HEAD (fallthru->dest))
1959 error ("wrong amount of branch edges after conditional jump %i",
1960 bb->index);
1961 err = 1;
1963 if (n_call && !CALL_P (BB_END (bb)))
1965 error ("call edges for non-call insn in bb %i", bb->index);
1966 err = 1;
1968 if (n_abnormal
1969 && (!CALL_P (BB_END (bb)) && n_call != n_abnormal)
1970 && (!JUMP_P (BB_END (bb))
1971 || any_condjump_p (BB_END (bb))
1972 || any_uncondjump_p (BB_END (bb))))
1974 error ("abnormal edges for no purpose in bb %i", bb->index);
1975 err = 1;
1978 for (x = BB_HEAD (bb); x != NEXT_INSN (BB_END (bb)); x = NEXT_INSN (x))
1979 /* We may have a barrier inside a basic block before dead code
1980 elimination. There is no BLOCK_FOR_INSN field in a barrier. */
1981 if (!BARRIER_P (x) && BLOCK_FOR_INSN (x) != bb)
1983 debug_rtx (x);
1984 if (! BLOCK_FOR_INSN (x))
1985 error
1986 ("insn %d inside basic block %d but block_for_insn is NULL",
1987 INSN_UID (x), bb->index);
1988 else
1989 error
1990 ("insn %d inside basic block %d but block_for_insn is %i",
1991 INSN_UID (x), bb->index, BLOCK_FOR_INSN (x)->index);
1993 err = 1;
1996 /* OK pointers are correct. Now check the header of basic
1997 block. It ought to contain optional CODE_LABEL followed
1998 by NOTE_BASIC_BLOCK. */
1999 x = BB_HEAD (bb);
2000 if (LABEL_P (x))
2002 if (BB_END (bb) == x)
2004 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
2005 bb->index);
2006 err = 1;
2009 x = NEXT_INSN (x);
2012 if (!NOTE_INSN_BASIC_BLOCK_P (x) || NOTE_BASIC_BLOCK (x) != bb)
2014 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
2015 bb->index);
2016 err = 1;
2019 if (BB_END (bb) == x)
2020 /* Do checks for empty blocks here. */
2022 else
2023 for (x = NEXT_INSN (x); x; x = NEXT_INSN (x))
2025 if (NOTE_INSN_BASIC_BLOCK_P (x))
2027 error ("NOTE_INSN_BASIC_BLOCK %d in middle of basic block %d",
2028 INSN_UID (x), bb->index);
2029 err = 1;
2032 if (x == BB_END (bb))
2033 break;
2035 if (control_flow_insn_p (x))
2037 error ("in basic block %d:", bb->index);
2038 fatal_insn ("flow control insn inside a basic block", x);
2043 /* Clean up. */
2044 return err;
2047 /* Verify the CFG and RTL consistency common for both underlying RTL and
2048 cfglayout RTL.
2050 Currently it does following checks:
2051 - all checks of rtl_verify_flow_info_1
2052 - test head/end pointers
2053 - check that all insns are in the basic blocks
2054 (except the switch handling code, barriers and notes)
2055 - check that all returns are followed by barriers
2056 - check that all fallthru edge points to the adjacent blocks. */
2058 static int
2059 rtl_verify_flow_info (void)
2061 basic_block bb;
2062 int err = rtl_verify_flow_info_1 ();
2063 rtx x;
2064 rtx last_head = get_last_insn ();
2065 basic_block *bb_info;
2066 int num_bb_notes;
2067 const rtx rtx_first = get_insns ();
2068 basic_block last_bb_seen = ENTRY_BLOCK_PTR, curr_bb = NULL;
2069 const int max_uid = get_max_uid ();
2071 bb_info = XCNEWVEC (basic_block, max_uid);
2073 FOR_EACH_BB_REVERSE (bb)
2075 edge e;
2076 rtx head = BB_HEAD (bb);
2077 rtx end = BB_END (bb);
2079 for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
2081 /* Verify the end of the basic block is in the INSN chain. */
2082 if (x == end)
2083 break;
2085 /* And that the code outside of basic blocks has NULL bb field. */
2086 if (!BARRIER_P (x)
2087 && BLOCK_FOR_INSN (x) != NULL)
2089 error ("insn %d outside of basic blocks has non-NULL bb field",
2090 INSN_UID (x));
2091 err = 1;
2095 if (!x)
2097 error ("end insn %d for block %d not found in the insn stream",
2098 INSN_UID (end), bb->index);
2099 err = 1;
2102 /* Work backwards from the end to the head of the basic block
2103 to verify the head is in the RTL chain. */
2104 for (; x != NULL_RTX; x = PREV_INSN (x))
2106 /* While walking over the insn chain, verify insns appear
2107 in only one basic block. */
2108 if (bb_info[INSN_UID (x)] != NULL)
2110 error ("insn %d is in multiple basic blocks (%d and %d)",
2111 INSN_UID (x), bb->index, bb_info[INSN_UID (x)]->index);
2112 err = 1;
2115 bb_info[INSN_UID (x)] = bb;
2117 if (x == head)
2118 break;
2120 if (!x)
2122 error ("head insn %d for block %d not found in the insn stream",
2123 INSN_UID (head), bb->index);
2124 err = 1;
2127 last_head = PREV_INSN (x);
2129 e = find_fallthru_edge (bb->succs);
2130 if (!e)
2132 rtx insn;
2134 /* Ensure existence of barrier in BB with no fallthru edges. */
2135 for (insn = NEXT_INSN (BB_END (bb)); ; insn = NEXT_INSN (insn))
2137 if (!insn || NOTE_INSN_BASIC_BLOCK_P (insn))
2139 error ("missing barrier after block %i", bb->index);
2140 err = 1;
2141 break;
2143 if (BARRIER_P (insn))
2144 break;
2147 else if (e->src != ENTRY_BLOCK_PTR
2148 && e->dest != EXIT_BLOCK_PTR)
2150 rtx insn;
2152 if (e->src->next_bb != e->dest)
2154 error
2155 ("verify_flow_info: Incorrect blocks for fallthru %i->%i",
2156 e->src->index, e->dest->index);
2157 err = 1;
2159 else
2160 for (insn = NEXT_INSN (BB_END (e->src)); insn != BB_HEAD (e->dest);
2161 insn = NEXT_INSN (insn))
2162 if (BARRIER_P (insn) || INSN_P (insn))
2164 error ("verify_flow_info: Incorrect fallthru %i->%i",
2165 e->src->index, e->dest->index);
2166 fatal_insn ("wrong insn in the fallthru edge", insn);
2167 err = 1;
2172 for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
2174 /* Check that the code before the first basic block has NULL
2175 bb field. */
2176 if (!BARRIER_P (x)
2177 && BLOCK_FOR_INSN (x) != NULL)
2179 error ("insn %d outside of basic blocks has non-NULL bb field",
2180 INSN_UID (x));
2181 err = 1;
2184 free (bb_info);
2186 num_bb_notes = 0;
2187 last_bb_seen = ENTRY_BLOCK_PTR;
2189 for (x = rtx_first; x; x = NEXT_INSN (x))
2191 if (NOTE_INSN_BASIC_BLOCK_P (x))
2193 bb = NOTE_BASIC_BLOCK (x);
2195 num_bb_notes++;
2196 if (bb != last_bb_seen->next_bb)
2197 internal_error ("basic blocks not laid down consecutively");
2199 curr_bb = last_bb_seen = bb;
2202 if (!curr_bb)
2204 switch (GET_CODE (x))
2206 case BARRIER:
2207 case NOTE:
2208 break;
2210 case CODE_LABEL:
2211 /* An addr_vec is placed outside any basic block. */
2212 if (NEXT_INSN (x)
2213 && JUMP_TABLE_DATA_P (NEXT_INSN (x)))
2214 x = NEXT_INSN (x);
2216 /* But in any case, non-deletable labels can appear anywhere. */
2217 break;
2219 default:
2220 fatal_insn ("insn outside basic block", x);
2224 if (JUMP_P (x)
2225 && returnjump_p (x) && ! condjump_p (x)
2226 && ! (next_nonnote_insn (x) && BARRIER_P (next_nonnote_insn (x))))
2227 fatal_insn ("return not followed by barrier", x);
2228 if (curr_bb && x == BB_END (curr_bb))
2229 curr_bb = NULL;
2232 if (num_bb_notes != n_basic_blocks - NUM_FIXED_BLOCKS)
2233 internal_error
2234 ("number of bb notes in insn chain (%d) != n_basic_blocks (%d)",
2235 num_bb_notes, n_basic_blocks);
2237 return err;
2240 /* Assume that the preceding pass has possibly eliminated jump instructions
2241 or converted the unconditional jumps. Eliminate the edges from CFG.
2242 Return true if any edges are eliminated. */
2244 bool
2245 purge_dead_edges (basic_block bb)
2247 edge e;
2248 rtx insn = BB_END (bb), note;
2249 bool purged = false;
2250 bool found;
2251 edge_iterator ei;
2253 if (DEBUG_INSN_P (insn) && insn != BB_HEAD (bb))
2255 insn = PREV_INSN (insn);
2256 while ((DEBUG_INSN_P (insn) || NOTE_P (insn)) && insn != BB_HEAD (bb));
2258 /* If this instruction cannot trap, remove REG_EH_REGION notes. */
2259 if (NONJUMP_INSN_P (insn)
2260 && (note = find_reg_note (insn, REG_EH_REGION, NULL)))
2262 rtx eqnote;
2264 if (! may_trap_p (PATTERN (insn))
2265 || ((eqnote = find_reg_equal_equiv_note (insn))
2266 && ! may_trap_p (XEXP (eqnote, 0))))
2267 remove_note (insn, note);
2270 /* Cleanup abnormal edges caused by exceptions or non-local gotos. */
2271 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2273 bool remove = false;
2275 /* There are three types of edges we need to handle correctly here: EH
2276 edges, abnormal call EH edges, and abnormal call non-EH edges. The
2277 latter can appear when nonlocal gotos are used. */
2278 if (e->flags & EDGE_ABNORMAL_CALL)
2280 if (!CALL_P (insn))
2281 remove = true;
2282 else if (can_nonlocal_goto (insn))
2284 else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
2286 else
2287 remove = true;
2289 else if (e->flags & EDGE_EH)
2290 remove = !can_throw_internal (insn);
2292 if (remove)
2294 remove_edge (e);
2295 df_set_bb_dirty (bb);
2296 purged = true;
2298 else
2299 ei_next (&ei);
2302 if (JUMP_P (insn))
2304 rtx note;
2305 edge b,f;
2306 edge_iterator ei;
2308 /* We do care only about conditional jumps and simplejumps. */
2309 if (!any_condjump_p (insn)
2310 && !returnjump_p (insn)
2311 && !simplejump_p (insn))
2312 return purged;
2314 /* Branch probability/prediction notes are defined only for
2315 condjumps. We've possibly turned condjump into simplejump. */
2316 if (simplejump_p (insn))
2318 note = find_reg_note (insn, REG_BR_PROB, NULL);
2319 if (note)
2320 remove_note (insn, note);
2321 while ((note = find_reg_note (insn, REG_BR_PRED, NULL)))
2322 remove_note (insn, note);
2325 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2327 /* Avoid abnormal flags to leak from computed jumps turned
2328 into simplejumps. */
2330 e->flags &= ~EDGE_ABNORMAL;
2332 /* See if this edge is one we should keep. */
2333 if ((e->flags & EDGE_FALLTHRU) && any_condjump_p (insn))
2334 /* A conditional jump can fall through into the next
2335 block, so we should keep the edge. */
2337 ei_next (&ei);
2338 continue;
2340 else if (e->dest != EXIT_BLOCK_PTR
2341 && BB_HEAD (e->dest) == JUMP_LABEL (insn))
2342 /* If the destination block is the target of the jump,
2343 keep the edge. */
2345 ei_next (&ei);
2346 continue;
2348 else if (e->dest == EXIT_BLOCK_PTR && returnjump_p (insn))
2349 /* If the destination block is the exit block, and this
2350 instruction is a return, then keep the edge. */
2352 ei_next (&ei);
2353 continue;
2355 else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
2356 /* Keep the edges that correspond to exceptions thrown by
2357 this instruction and rematerialize the EDGE_ABNORMAL
2358 flag we just cleared above. */
2360 e->flags |= EDGE_ABNORMAL;
2361 ei_next (&ei);
2362 continue;
2365 /* We do not need this edge. */
2366 df_set_bb_dirty (bb);
2367 purged = true;
2368 remove_edge (e);
2371 if (EDGE_COUNT (bb->succs) == 0 || !purged)
2372 return purged;
2374 if (dump_file)
2375 fprintf (dump_file, "Purged edges from bb %i\n", bb->index);
2377 if (!optimize)
2378 return purged;
2380 /* Redistribute probabilities. */
2381 if (single_succ_p (bb))
2383 single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
2384 single_succ_edge (bb)->count = bb->count;
2386 else
2388 note = find_reg_note (insn, REG_BR_PROB, NULL);
2389 if (!note)
2390 return purged;
2392 b = BRANCH_EDGE (bb);
2393 f = FALLTHRU_EDGE (bb);
2394 b->probability = INTVAL (XEXP (note, 0));
2395 f->probability = REG_BR_PROB_BASE - b->probability;
2396 b->count = bb->count * b->probability / REG_BR_PROB_BASE;
2397 f->count = bb->count * f->probability / REG_BR_PROB_BASE;
2400 return purged;
2402 else if (CALL_P (insn) && SIBLING_CALL_P (insn))
2404 /* First, there should not be any EH or ABCALL edges resulting
2405 from non-local gotos and the like. If there were, we shouldn't
2406 have created the sibcall in the first place. Second, there
2407 should of course never have been a fallthru edge. */
2408 gcc_assert (single_succ_p (bb));
2409 gcc_assert (single_succ_edge (bb)->flags
2410 == (EDGE_SIBCALL | EDGE_ABNORMAL));
2412 return 0;
2415 /* If we don't see a jump insn, we don't know exactly why the block would
2416 have been broken at this point. Look for a simple, non-fallthru edge,
2417 as these are only created by conditional branches. If we find such an
2418 edge we know that there used to be a jump here and can then safely
2419 remove all non-fallthru edges. */
2420 found = false;
2421 FOR_EACH_EDGE (e, ei, bb->succs)
2422 if (! (e->flags & (EDGE_COMPLEX | EDGE_FALLTHRU)))
2424 found = true;
2425 break;
2428 if (!found)
2429 return purged;
2431 /* Remove all but the fake and fallthru edges. The fake edge may be
2432 the only successor for this block in the case of noreturn
2433 calls. */
2434 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2436 if (!(e->flags & (EDGE_FALLTHRU | EDGE_FAKE)))
2438 df_set_bb_dirty (bb);
2439 remove_edge (e);
2440 purged = true;
2442 else
2443 ei_next (&ei);
2446 gcc_assert (single_succ_p (bb));
2448 single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
2449 single_succ_edge (bb)->count = bb->count;
2451 if (dump_file)
2452 fprintf (dump_file, "Purged non-fallthru edges from bb %i\n",
2453 bb->index);
2454 return purged;
2457 /* Search all basic blocks for potentially dead edges and purge them. Return
2458 true if some edge has been eliminated. */
2460 bool
2461 purge_all_dead_edges (void)
2463 int purged = false;
2464 basic_block bb;
2466 FOR_EACH_BB (bb)
2468 bool purged_here = purge_dead_edges (bb);
2470 purged |= purged_here;
2473 return purged;
2476 /* This is used by a few passes that emit some instructions after abnormal
2477 calls, moving the basic block's end, while they in fact do want to emit
2478 them on the fallthru edge. Look for abnormal call edges, find backward
2479 the call in the block and insert the instructions on the edge instead.
2481 Similarly, handle instructions throwing exceptions internally.
2483 Return true when instructions have been found and inserted on edges. */
2485 bool
2486 fixup_abnormal_edges (void)
2488 bool inserted = false;
2489 basic_block bb;
2491 FOR_EACH_BB (bb)
2493 edge e;
2494 edge_iterator ei;
2496 /* Look for cases we are interested in - calls or instructions causing
2497 exceptions. */
2498 FOR_EACH_EDGE (e, ei, bb->succs)
2499 if ((e->flags & EDGE_ABNORMAL_CALL)
2500 || ((e->flags & (EDGE_ABNORMAL | EDGE_EH))
2501 == (EDGE_ABNORMAL | EDGE_EH)))
2502 break;
2504 if (e && !CALL_P (BB_END (bb)) && !can_throw_internal (BB_END (bb)))
2506 rtx insn;
2508 /* Get past the new insns generated. Allow notes, as the insns
2509 may be already deleted. */
2510 insn = BB_END (bb);
2511 while ((NONJUMP_INSN_P (insn) || NOTE_P (insn))
2512 && !can_throw_internal (insn)
2513 && insn != BB_HEAD (bb))
2514 insn = PREV_INSN (insn);
2516 if (CALL_P (insn) || can_throw_internal (insn))
2518 rtx stop, next;
2520 e = find_fallthru_edge (bb->succs);
2522 stop = NEXT_INSN (BB_END (bb));
2523 BB_END (bb) = insn;
2525 for (insn = NEXT_INSN (insn); insn != stop; insn = next)
2527 next = NEXT_INSN (insn);
2528 if (INSN_P (insn))
2530 delete_insn (insn);
2532 /* Sometimes there's still the return value USE.
2533 If it's placed after a trapping call (i.e. that
2534 call is the last insn anyway), we have no fallthru
2535 edge. Simply delete this use and don't try to insert
2536 on the non-existent edge. */
2537 if (GET_CODE (PATTERN (insn)) != USE)
2539 /* We're not deleting it, we're moving it. */
2540 INSN_DELETED_P (insn) = 0;
2541 PREV_INSN (insn) = NULL_RTX;
2542 NEXT_INSN (insn) = NULL_RTX;
2544 insert_insn_on_edge (insn, e);
2545 inserted = true;
2548 else if (!BARRIER_P (insn))
2549 set_block_for_insn (insn, NULL);
2553 /* It may be that we don't find any trapping insn. In this
2554 case we discovered quite late that the insn that had been
2555 marked as can_throw_internal in fact couldn't trap at all.
2556 So we should in fact delete the EH edges out of the block. */
2557 else
2558 purge_dead_edges (bb);
2562 return inserted;
2565 /* Same as split_block but update cfg_layout structures. */
2567 static basic_block
2568 cfg_layout_split_block (basic_block bb, void *insnp)
2570 rtx insn = (rtx) insnp;
2571 basic_block new_bb = rtl_split_block (bb, insn);
2573 new_bb->il.rtl->footer = bb->il.rtl->footer;
2574 bb->il.rtl->footer = NULL;
2576 return new_bb;
2579 /* Redirect Edge to DEST. */
2580 static edge
2581 cfg_layout_redirect_edge_and_branch (edge e, basic_block dest)
2583 basic_block src = e->src;
2584 edge ret;
2586 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
2587 return NULL;
2589 if (e->dest == dest)
2590 return e;
2592 if (e->src != ENTRY_BLOCK_PTR
2593 && (ret = try_redirect_by_replacing_jump (e, dest, true)))
2595 df_set_bb_dirty (src);
2596 return ret;
2599 if (e->src == ENTRY_BLOCK_PTR
2600 && (e->flags & EDGE_FALLTHRU) && !(e->flags & EDGE_COMPLEX))
2602 if (dump_file)
2603 fprintf (dump_file, "Redirecting entry edge from bb %i to %i\n",
2604 e->src->index, dest->index);
2606 df_set_bb_dirty (e->src);
2607 redirect_edge_succ (e, dest);
2608 return e;
2611 /* Redirect_edge_and_branch may decide to turn branch into fallthru edge
2612 in the case the basic block appears to be in sequence. Avoid this
2613 transformation. */
2615 if (e->flags & EDGE_FALLTHRU)
2617 /* Redirect any branch edges unified with the fallthru one. */
2618 if (JUMP_P (BB_END (src))
2619 && label_is_jump_target_p (BB_HEAD (e->dest),
2620 BB_END (src)))
2622 edge redirected;
2624 if (dump_file)
2625 fprintf (dump_file, "Fallthru edge unified with branch "
2626 "%i->%i redirected to %i\n",
2627 e->src->index, e->dest->index, dest->index);
2628 e->flags &= ~EDGE_FALLTHRU;
2629 redirected = redirect_branch_edge (e, dest);
2630 gcc_assert (redirected);
2631 redirected->flags |= EDGE_FALLTHRU;
2632 df_set_bb_dirty (redirected->src);
2633 return redirected;
2635 /* In case we are redirecting fallthru edge to the branch edge
2636 of conditional jump, remove it. */
2637 if (EDGE_COUNT (src->succs) == 2)
2639 /* Find the edge that is different from E. */
2640 edge s = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e);
2642 if (s->dest == dest
2643 && any_condjump_p (BB_END (src))
2644 && onlyjump_p (BB_END (src)))
2645 delete_insn (BB_END (src));
2647 if (dump_file)
2648 fprintf (dump_file, "Fallthru edge %i->%i redirected to %i\n",
2649 e->src->index, e->dest->index, dest->index);
2650 ret = redirect_edge_succ_nodup (e, dest);
2652 else
2653 ret = redirect_branch_edge (e, dest);
2655 /* We don't want simplejumps in the insn stream during cfglayout. */
2656 gcc_assert (!simplejump_p (BB_END (src)));
2658 df_set_bb_dirty (src);
2659 return ret;
2662 /* Simple wrapper as we always can redirect fallthru edges. */
2663 static basic_block
2664 cfg_layout_redirect_edge_and_branch_force (edge e, basic_block dest)
2666 edge redirected = cfg_layout_redirect_edge_and_branch (e, dest);
2668 gcc_assert (redirected);
2669 return NULL;
2672 /* Same as delete_basic_block but update cfg_layout structures. */
2674 static void
2675 cfg_layout_delete_block (basic_block bb)
2677 rtx insn, next, prev = PREV_INSN (BB_HEAD (bb)), *to, remaints;
2679 if (bb->il.rtl->header)
2681 next = BB_HEAD (bb);
2682 if (prev)
2683 NEXT_INSN (prev) = bb->il.rtl->header;
2684 else
2685 set_first_insn (bb->il.rtl->header);
2686 PREV_INSN (bb->il.rtl->header) = prev;
2687 insn = bb->il.rtl->header;
2688 while (NEXT_INSN (insn))
2689 insn = NEXT_INSN (insn);
2690 NEXT_INSN (insn) = next;
2691 PREV_INSN (next) = insn;
2693 next = NEXT_INSN (BB_END (bb));
2694 if (bb->il.rtl->footer)
2696 insn = bb->il.rtl->footer;
2697 while (insn)
2699 if (BARRIER_P (insn))
2701 if (PREV_INSN (insn))
2702 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2703 else
2704 bb->il.rtl->footer = NEXT_INSN (insn);
2705 if (NEXT_INSN (insn))
2706 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2708 if (LABEL_P (insn))
2709 break;
2710 insn = NEXT_INSN (insn);
2712 if (bb->il.rtl->footer)
2714 insn = BB_END (bb);
2715 NEXT_INSN (insn) = bb->il.rtl->footer;
2716 PREV_INSN (bb->il.rtl->footer) = insn;
2717 while (NEXT_INSN (insn))
2718 insn = NEXT_INSN (insn);
2719 NEXT_INSN (insn) = next;
2720 if (next)
2721 PREV_INSN (next) = insn;
2722 else
2723 set_last_insn (insn);
2726 if (bb->next_bb != EXIT_BLOCK_PTR)
2727 to = &bb->next_bb->il.rtl->header;
2728 else
2729 to = &cfg_layout_function_footer;
2731 rtl_delete_block (bb);
2733 if (prev)
2734 prev = NEXT_INSN (prev);
2735 else
2736 prev = get_insns ();
2737 if (next)
2738 next = PREV_INSN (next);
2739 else
2740 next = get_last_insn ();
2742 if (next && NEXT_INSN (next) != prev)
2744 remaints = unlink_insn_chain (prev, next);
2745 insn = remaints;
2746 while (NEXT_INSN (insn))
2747 insn = NEXT_INSN (insn);
2748 NEXT_INSN (insn) = *to;
2749 if (*to)
2750 PREV_INSN (*to) = insn;
2751 *to = remaints;
2755 /* Return true when blocks A and B can be safely merged. */
2757 static bool
2758 cfg_layout_can_merge_blocks_p (basic_block a, basic_block b)
2760 /* If we are partitioning hot/cold basic blocks, we don't want to
2761 mess up unconditional or indirect jumps that cross between hot
2762 and cold sections.
2764 Basic block partitioning may result in some jumps that appear to
2765 be optimizable (or blocks that appear to be mergeable), but which really
2766 must be left untouched (they are required to make it safely across
2767 partition boundaries). See the comments at the top of
2768 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
2770 if (BB_PARTITION (a) != BB_PARTITION (b))
2771 return false;
2773 /* There must be exactly one edge in between the blocks. */
2774 return (single_succ_p (a)
2775 && single_succ (a) == b
2776 && single_pred_p (b) == 1
2777 && a != b
2778 /* Must be simple edge. */
2779 && !(single_succ_edge (a)->flags & EDGE_COMPLEX)
2780 && a != ENTRY_BLOCK_PTR && b != EXIT_BLOCK_PTR
2781 /* If the jump insn has side effects, we can't kill the edge.
2782 When not optimizing, try_redirect_by_replacing_jump will
2783 not allow us to redirect an edge by replacing a table jump. */
2784 && (!JUMP_P (BB_END (a))
2785 || ((!optimize || reload_completed)
2786 ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
2789 /* Merge block A and B. The blocks must be mergeable. */
2791 static void
2792 cfg_layout_merge_blocks (basic_block a, basic_block b)
2794 bool forwarder_p = (b->flags & BB_FORWARDER_BLOCK) != 0;
2796 gcc_checking_assert (cfg_layout_can_merge_blocks_p (a, b));
2798 if (dump_file)
2799 fprintf (dump_file, "Merging block %d into block %d...\n", b->index,
2800 a->index);
2802 /* If there was a CODE_LABEL beginning B, delete it. */
2803 if (LABEL_P (BB_HEAD (b)))
2805 delete_insn (BB_HEAD (b));
2808 /* We should have fallthru edge in a, or we can do dummy redirection to get
2809 it cleaned up. */
2810 if (JUMP_P (BB_END (a)))
2811 try_redirect_by_replacing_jump (EDGE_SUCC (a, 0), b, true);
2812 gcc_assert (!JUMP_P (BB_END (a)));
2814 /* When not optimizing and the edge is the only place in RTL which holds
2815 some unique locus, emit a nop with that locus in between. */
2816 if (!optimize && EDGE_SUCC (a, 0)->goto_locus)
2818 rtx insn = BB_END (a), end = PREV_INSN (BB_HEAD (a));
2819 int goto_locus = EDGE_SUCC (a, 0)->goto_locus;
2821 while (insn != end && (!INSN_P (insn) || INSN_LOCATOR (insn) == 0))
2822 insn = PREV_INSN (insn);
2823 if (insn != end && locator_eq (INSN_LOCATOR (insn), goto_locus))
2824 goto_locus = 0;
2825 else
2827 insn = BB_HEAD (b);
2828 end = NEXT_INSN (BB_END (b));
2829 while (insn != end && !INSN_P (insn))
2830 insn = NEXT_INSN (insn);
2831 if (insn != end && INSN_LOCATOR (insn) != 0
2832 && locator_eq (INSN_LOCATOR (insn), goto_locus))
2833 goto_locus = 0;
2835 if (goto_locus)
2837 BB_END (a) = emit_insn_after_noloc (gen_nop (), BB_END (a), a);
2838 INSN_LOCATOR (BB_END (a)) = goto_locus;
2842 /* Possible line number notes should appear in between. */
2843 if (b->il.rtl->header)
2845 rtx first = BB_END (a), last;
2847 last = emit_insn_after_noloc (b->il.rtl->header, BB_END (a), a);
2848 delete_insn_chain (NEXT_INSN (first), last, false);
2849 b->il.rtl->header = NULL;
2852 /* In the case basic blocks are not adjacent, move them around. */
2853 if (NEXT_INSN (BB_END (a)) != BB_HEAD (b))
2855 rtx first = unlink_insn_chain (BB_HEAD (b), BB_END (b));
2857 emit_insn_after_noloc (first, BB_END (a), a);
2858 /* Skip possible DELETED_LABEL insn. */
2859 if (!NOTE_INSN_BASIC_BLOCK_P (first))
2860 first = NEXT_INSN (first);
2861 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (first));
2862 BB_HEAD (b) = NULL;
2864 /* emit_insn_after_noloc doesn't call df_insn_change_bb.
2865 We need to explicitly call. */
2866 update_bb_for_insn_chain (NEXT_INSN (first),
2867 BB_END (b),
2870 delete_insn (first);
2872 /* Otherwise just re-associate the instructions. */
2873 else
2875 rtx insn;
2877 update_bb_for_insn_chain (BB_HEAD (b), BB_END (b), a);
2879 insn = BB_HEAD (b);
2880 /* Skip possible DELETED_LABEL insn. */
2881 if (!NOTE_INSN_BASIC_BLOCK_P (insn))
2882 insn = NEXT_INSN (insn);
2883 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
2884 BB_HEAD (b) = NULL;
2885 BB_END (a) = BB_END (b);
2886 delete_insn (insn);
2889 df_bb_delete (b->index);
2891 /* Possible tablejumps and barriers should appear after the block. */
2892 if (b->il.rtl->footer)
2894 if (!a->il.rtl->footer)
2895 a->il.rtl->footer = b->il.rtl->footer;
2896 else
2898 rtx last = a->il.rtl->footer;
2900 while (NEXT_INSN (last))
2901 last = NEXT_INSN (last);
2902 NEXT_INSN (last) = b->il.rtl->footer;
2903 PREV_INSN (b->il.rtl->footer) = last;
2905 b->il.rtl->footer = NULL;
2908 /* If B was a forwarder block, propagate the locus on the edge. */
2909 if (forwarder_p && !EDGE_SUCC (b, 0)->goto_locus)
2910 EDGE_SUCC (b, 0)->goto_locus = EDGE_SUCC (a, 0)->goto_locus;
2912 if (dump_file)
2913 fprintf (dump_file, "Merged blocks %d and %d.\n", a->index, b->index);
2916 /* Split edge E. */
2918 static basic_block
2919 cfg_layout_split_edge (edge e)
2921 basic_block new_bb =
2922 create_basic_block (e->src != ENTRY_BLOCK_PTR
2923 ? NEXT_INSN (BB_END (e->src)) : get_insns (),
2924 NULL_RTX, e->src);
2926 if (e->dest == EXIT_BLOCK_PTR)
2927 BB_COPY_PARTITION (new_bb, e->src);
2928 else
2929 BB_COPY_PARTITION (new_bb, e->dest);
2930 make_edge (new_bb, e->dest, EDGE_FALLTHRU);
2931 redirect_edge_and_branch_force (e, new_bb);
2933 return new_bb;
2936 /* Do postprocessing after making a forwarder block joined by edge FALLTHRU. */
2938 static void
2939 rtl_make_forwarder_block (edge fallthru ATTRIBUTE_UNUSED)
2943 /* Return 1 if BB ends with a call, possibly followed by some
2944 instructions that must stay with the call, 0 otherwise. */
2946 static bool
2947 rtl_block_ends_with_call_p (basic_block bb)
2949 rtx insn = BB_END (bb);
2951 while (!CALL_P (insn)
2952 && insn != BB_HEAD (bb)
2953 && (keep_with_call_p (insn)
2954 || NOTE_P (insn)
2955 || DEBUG_INSN_P (insn)))
2956 insn = PREV_INSN (insn);
2957 return (CALL_P (insn));
2960 /* Return 1 if BB ends with a conditional branch, 0 otherwise. */
2962 static bool
2963 rtl_block_ends_with_condjump_p (const_basic_block bb)
2965 return any_condjump_p (BB_END (bb));
2968 /* Return true if we need to add fake edge to exit.
2969 Helper function for rtl_flow_call_edges_add. */
2971 static bool
2972 need_fake_edge_p (const_rtx insn)
2974 if (!INSN_P (insn))
2975 return false;
2977 if ((CALL_P (insn)
2978 && !SIBLING_CALL_P (insn)
2979 && !find_reg_note (insn, REG_NORETURN, NULL)
2980 && !(RTL_CONST_OR_PURE_CALL_P (insn))))
2981 return true;
2983 return ((GET_CODE (PATTERN (insn)) == ASM_OPERANDS
2984 && MEM_VOLATILE_P (PATTERN (insn)))
2985 || (GET_CODE (PATTERN (insn)) == PARALLEL
2986 && asm_noperands (insn) != -1
2987 && MEM_VOLATILE_P (XVECEXP (PATTERN (insn), 0, 0)))
2988 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
2991 /* Add fake edges to the function exit for any non constant and non noreturn
2992 calls, volatile inline assembly in the bitmap of blocks specified by
2993 BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
2994 that were split.
2996 The goal is to expose cases in which entering a basic block does not imply
2997 that all subsequent instructions must be executed. */
2999 static int
3000 rtl_flow_call_edges_add (sbitmap blocks)
3002 int i;
3003 int blocks_split = 0;
3004 int last_bb = last_basic_block;
3005 bool check_last_block = false;
3007 if (n_basic_blocks == NUM_FIXED_BLOCKS)
3008 return 0;
3010 if (! blocks)
3011 check_last_block = true;
3012 else
3013 check_last_block = TEST_BIT (blocks, EXIT_BLOCK_PTR->prev_bb->index);
3015 /* In the last basic block, before epilogue generation, there will be
3016 a fallthru edge to EXIT. Special care is required if the last insn
3017 of the last basic block is a call because make_edge folds duplicate
3018 edges, which would result in the fallthru edge also being marked
3019 fake, which would result in the fallthru edge being removed by
3020 remove_fake_edges, which would result in an invalid CFG.
3022 Moreover, we can't elide the outgoing fake edge, since the block
3023 profiler needs to take this into account in order to solve the minimal
3024 spanning tree in the case that the call doesn't return.
3026 Handle this by adding a dummy instruction in a new last basic block. */
3027 if (check_last_block)
3029 basic_block bb = EXIT_BLOCK_PTR->prev_bb;
3030 rtx insn = BB_END (bb);
3032 /* Back up past insns that must be kept in the same block as a call. */
3033 while (insn != BB_HEAD (bb)
3034 && keep_with_call_p (insn))
3035 insn = PREV_INSN (insn);
3037 if (need_fake_edge_p (insn))
3039 edge e;
3041 e = find_edge (bb, EXIT_BLOCK_PTR);
3042 if (e)
3044 insert_insn_on_edge (gen_use (const0_rtx), e);
3045 commit_edge_insertions ();
3050 /* Now add fake edges to the function exit for any non constant
3051 calls since there is no way that we can determine if they will
3052 return or not... */
3054 for (i = NUM_FIXED_BLOCKS; i < last_bb; i++)
3056 basic_block bb = BASIC_BLOCK (i);
3057 rtx insn;
3058 rtx prev_insn;
3060 if (!bb)
3061 continue;
3063 if (blocks && !TEST_BIT (blocks, i))
3064 continue;
3066 for (insn = BB_END (bb); ; insn = prev_insn)
3068 prev_insn = PREV_INSN (insn);
3069 if (need_fake_edge_p (insn))
3071 edge e;
3072 rtx split_at_insn = insn;
3074 /* Don't split the block between a call and an insn that should
3075 remain in the same block as the call. */
3076 if (CALL_P (insn))
3077 while (split_at_insn != BB_END (bb)
3078 && keep_with_call_p (NEXT_INSN (split_at_insn)))
3079 split_at_insn = NEXT_INSN (split_at_insn);
3081 /* The handling above of the final block before the epilogue
3082 should be enough to verify that there is no edge to the exit
3083 block in CFG already. Calling make_edge in such case would
3084 cause us to mark that edge as fake and remove it later. */
3086 #ifdef ENABLE_CHECKING
3087 if (split_at_insn == BB_END (bb))
3089 e = find_edge (bb, EXIT_BLOCK_PTR);
3090 gcc_assert (e == NULL);
3092 #endif
3094 /* Note that the following may create a new basic block
3095 and renumber the existing basic blocks. */
3096 if (split_at_insn != BB_END (bb))
3098 e = split_block (bb, split_at_insn);
3099 if (e)
3100 blocks_split++;
3103 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
3106 if (insn == BB_HEAD (bb))
3107 break;
3111 if (blocks_split)
3112 verify_flow_info ();
3114 return blocks_split;
3117 /* Add COMP_RTX as a condition at end of COND_BB. FIRST_HEAD is
3118 the conditional branch target, SECOND_HEAD should be the fall-thru
3119 there is no need to handle this here the loop versioning code handles
3120 this. the reason for SECON_HEAD is that it is needed for condition
3121 in trees, and this should be of the same type since it is a hook. */
3122 static void
3123 rtl_lv_add_condition_to_bb (basic_block first_head ,
3124 basic_block second_head ATTRIBUTE_UNUSED,
3125 basic_block cond_bb, void *comp_rtx)
3127 rtx label, seq, jump;
3128 rtx op0 = XEXP ((rtx)comp_rtx, 0);
3129 rtx op1 = XEXP ((rtx)comp_rtx, 1);
3130 enum rtx_code comp = GET_CODE ((rtx)comp_rtx);
3131 enum machine_mode mode;
3134 label = block_label (first_head);
3135 mode = GET_MODE (op0);
3136 if (mode == VOIDmode)
3137 mode = GET_MODE (op1);
3139 start_sequence ();
3140 op0 = force_operand (op0, NULL_RTX);
3141 op1 = force_operand (op1, NULL_RTX);
3142 do_compare_rtx_and_jump (op0, op1, comp, 0,
3143 mode, NULL_RTX, NULL_RTX, label, -1);
3144 jump = get_last_insn ();
3145 JUMP_LABEL (jump) = label;
3146 LABEL_NUSES (label)++;
3147 seq = get_insns ();
3148 end_sequence ();
3150 /* Add the new cond , in the new head. */
3151 emit_insn_after(seq, BB_END(cond_bb));
3155 /* Given a block B with unconditional branch at its end, get the
3156 store the return the branch edge and the fall-thru edge in
3157 BRANCH_EDGE and FALLTHRU_EDGE respectively. */
3158 static void
3159 rtl_extract_cond_bb_edges (basic_block b, edge *branch_edge,
3160 edge *fallthru_edge)
3162 edge e = EDGE_SUCC (b, 0);
3164 if (e->flags & EDGE_FALLTHRU)
3166 *fallthru_edge = e;
3167 *branch_edge = EDGE_SUCC (b, 1);
3169 else
3171 *branch_edge = e;
3172 *fallthru_edge = EDGE_SUCC (b, 1);
3176 void
3177 init_rtl_bb_info (basic_block bb)
3179 gcc_assert (!bb->il.rtl);
3180 bb->il.rtl = ggc_alloc_cleared_rtl_bb_info ();
3183 /* Returns true if it is possible to remove edge E by redirecting
3184 it to the destination of the other edge from E->src. */
3186 static bool
3187 rtl_can_remove_branch_p (const_edge e)
3189 const_basic_block src = e->src;
3190 const_basic_block target = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest;
3191 const_rtx insn = BB_END (src), set;
3193 /* The conditions are taken from try_redirect_by_replacing_jump. */
3194 if (target == EXIT_BLOCK_PTR)
3195 return false;
3197 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
3198 return false;
3200 if (find_reg_note (insn, REG_CROSSING_JUMP, NULL_RTX)
3201 || BB_PARTITION (src) != BB_PARTITION (target))
3202 return false;
3204 if (!onlyjump_p (insn)
3205 || tablejump_p (insn, NULL, NULL))
3206 return false;
3208 set = single_set (insn);
3209 if (!set || side_effects_p (set))
3210 return false;
3212 return true;
3215 /* Implementation of CFG manipulation for linearized RTL. */
3216 struct cfg_hooks rtl_cfg_hooks = {
3217 "rtl",
3218 rtl_verify_flow_info,
3219 rtl_dump_bb,
3220 rtl_create_basic_block,
3221 rtl_redirect_edge_and_branch,
3222 rtl_redirect_edge_and_branch_force,
3223 rtl_can_remove_branch_p,
3224 rtl_delete_block,
3225 rtl_split_block,
3226 rtl_move_block_after,
3227 rtl_can_merge_blocks, /* can_merge_blocks_p */
3228 rtl_merge_blocks,
3229 rtl_predict_edge,
3230 rtl_predicted_by_p,
3231 NULL, /* can_duplicate_block_p */
3232 NULL, /* duplicate_block */
3233 rtl_split_edge,
3234 rtl_make_forwarder_block,
3235 rtl_tidy_fallthru_edge,
3236 rtl_block_ends_with_call_p,
3237 rtl_block_ends_with_condjump_p,
3238 rtl_flow_call_edges_add,
3239 NULL, /* execute_on_growing_pred */
3240 NULL, /* execute_on_shrinking_pred */
3241 NULL, /* duplicate loop for trees */
3242 NULL, /* lv_add_condition_to_bb */
3243 NULL, /* lv_adjust_loop_header_phi*/
3244 NULL, /* extract_cond_bb_edges */
3245 NULL /* flush_pending_stmts */
3248 /* Implementation of CFG manipulation for cfg layout RTL, where
3249 basic block connected via fallthru edges does not have to be adjacent.
3250 This representation will hopefully become the default one in future
3251 version of the compiler. */
3253 /* We do not want to declare these functions in a header file, since they
3254 should only be used through the cfghooks interface, and we do not want to
3255 move them here since it would require also moving quite a lot of related
3256 code. They are in cfglayout.c. */
3257 extern bool cfg_layout_can_duplicate_bb_p (const_basic_block);
3258 extern basic_block cfg_layout_duplicate_bb (basic_block);
3260 struct cfg_hooks cfg_layout_rtl_cfg_hooks = {
3261 "cfglayout mode",
3262 rtl_verify_flow_info_1,
3263 rtl_dump_bb,
3264 cfg_layout_create_basic_block,
3265 cfg_layout_redirect_edge_and_branch,
3266 cfg_layout_redirect_edge_and_branch_force,
3267 rtl_can_remove_branch_p,
3268 cfg_layout_delete_block,
3269 cfg_layout_split_block,
3270 rtl_move_block_after,
3271 cfg_layout_can_merge_blocks_p,
3272 cfg_layout_merge_blocks,
3273 rtl_predict_edge,
3274 rtl_predicted_by_p,
3275 cfg_layout_can_duplicate_bb_p,
3276 cfg_layout_duplicate_bb,
3277 cfg_layout_split_edge,
3278 rtl_make_forwarder_block,
3279 NULL,
3280 rtl_block_ends_with_call_p,
3281 rtl_block_ends_with_condjump_p,
3282 rtl_flow_call_edges_add,
3283 NULL, /* execute_on_growing_pred */
3284 NULL, /* execute_on_shrinking_pred */
3285 duplicate_loop_to_header_edge, /* duplicate loop for trees */
3286 rtl_lv_add_condition_to_bb, /* lv_add_condition_to_bb */
3287 NULL, /* lv_adjust_loop_header_phi*/
3288 rtl_extract_cond_bb_edges, /* extract_cond_bb_edges */
3289 NULL /* flush_pending_stmts */