Merge trunk version 201119 into gupc branch.
[official-gcc.git] / gcc / cfgrtl.c
blob36438b8409b273ba239d18e87d5c1ff823d94cf7
1 /* Control flow graph manipulation code for GNU compiler.
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file contains low level functions to manipulate the CFG and analyze it
21 that are aware of the RTL intermediate language.
23 Available functionality:
24 - Basic CFG/RTL manipulation API documented in cfghooks.h
25 - CFG-aware instruction chain manipulation
26 delete_insn, delete_insn_chain
27 - Edge splitting and committing to edges
28 insert_insn_on_edge, commit_edge_insertions
29 - CFG updating after insn simplification
30 purge_dead_edges, purge_all_dead_edges
31 - CFG fixing after coarse manipulation
32 fixup_abnormal_edges
34 Functions not supposed for generic use:
35 - Infrastructure to determine quickly basic block for insn
36 compute_bb_for_insn, update_bb_for_insn, set_block_for_insn,
37 - Edge redirection with updating and optimizing of insn chain
38 block_label, tidy_fallthru_edge, force_nonfallthru */
40 #include "config.h"
41 #include "system.h"
42 #include "coretypes.h"
43 #include "tm.h"
44 #include "tree.h"
45 #include "hard-reg-set.h"
46 #include "basic-block.h"
47 #include "bb-reorder.h"
48 #include "regs.h"
49 #include "flags.h"
50 #include "function.h"
51 #include "except.h"
52 #include "rtl-error.h"
53 #include "tm_p.h"
54 #include "obstack.h"
55 #include "insn-attr.h"
56 #include "insn-config.h"
57 #include "expr.h"
58 #include "target.h"
59 #include "common/common-target.h"
60 #include "cfgloop.h"
61 #include "ggc.h"
62 #include "tree-pass.h"
63 #include "df.h"
65 /* Holds the interesting leading and trailing notes for the function.
66 Only applicable if the CFG is in cfglayout mode. */
67 static GTY(()) rtx cfg_layout_function_footer;
68 static GTY(()) rtx cfg_layout_function_header;
70 static rtx skip_insns_after_block (basic_block);
71 static void record_effective_endpoints (void);
72 static rtx label_for_bb (basic_block);
73 static void fixup_reorder_chain (void);
75 void verify_insn_chain (void);
76 static void fixup_fallthru_exit_predecessor (void);
77 static int can_delete_note_p (const_rtx);
78 static int can_delete_label_p (const_rtx);
79 static basic_block rtl_split_edge (edge);
80 static bool rtl_move_block_after (basic_block, basic_block);
81 static int rtl_verify_flow_info (void);
82 static basic_block cfg_layout_split_block (basic_block, void *);
83 static edge cfg_layout_redirect_edge_and_branch (edge, basic_block);
84 static basic_block cfg_layout_redirect_edge_and_branch_force (edge, basic_block);
85 static void cfg_layout_delete_block (basic_block);
86 static void rtl_delete_block (basic_block);
87 static basic_block rtl_redirect_edge_and_branch_force (edge, basic_block);
88 static edge rtl_redirect_edge_and_branch (edge, basic_block);
89 static basic_block rtl_split_block (basic_block, void *);
90 static void rtl_dump_bb (FILE *, basic_block, int, int);
91 static int rtl_verify_flow_info_1 (void);
92 static void rtl_make_forwarder_block (edge);
94 /* Return true if NOTE is not one of the ones that must be kept paired,
95 so that we may simply delete it. */
97 static int
98 can_delete_note_p (const_rtx note)
100 switch (NOTE_KIND (note))
102 case NOTE_INSN_DELETED:
103 case NOTE_INSN_BASIC_BLOCK:
104 case NOTE_INSN_EPILOGUE_BEG:
105 return true;
107 default:
108 return false;
112 /* True if a given label can be deleted. */
114 static int
115 can_delete_label_p (const_rtx label)
117 return (!LABEL_PRESERVE_P (label)
118 /* User declared labels must be preserved. */
119 && LABEL_NAME (label) == 0
120 && !in_expr_list_p (forced_labels, label));
123 /* Delete INSN by patching it out. */
125 void
126 delete_insn (rtx insn)
128 rtx note;
129 bool really_delete = true;
131 if (LABEL_P (insn))
133 /* Some labels can't be directly removed from the INSN chain, as they
134 might be references via variables, constant pool etc.
135 Convert them to the special NOTE_INSN_DELETED_LABEL note. */
136 if (! can_delete_label_p (insn))
138 const char *name = LABEL_NAME (insn);
139 basic_block bb = BLOCK_FOR_INSN (insn);
140 rtx bb_note = NEXT_INSN (insn);
142 really_delete = false;
143 PUT_CODE (insn, NOTE);
144 NOTE_KIND (insn) = NOTE_INSN_DELETED_LABEL;
145 NOTE_DELETED_LABEL_NAME (insn) = name;
147 /* If the note following the label starts a basic block, and the
148 label is a member of the same basic block, interchange the two. */
149 if (bb_note != NULL_RTX
150 && NOTE_INSN_BASIC_BLOCK_P (bb_note)
151 && bb != NULL
152 && bb == BLOCK_FOR_INSN (bb_note))
154 reorder_insns_nobb (insn, insn, bb_note);
155 BB_HEAD (bb) = bb_note;
156 if (BB_END (bb) == bb_note)
157 BB_END (bb) = insn;
161 remove_node_from_expr_list (insn, &nonlocal_goto_handler_labels);
164 if (really_delete)
166 /* If this insn has already been deleted, something is very wrong. */
167 gcc_assert (!INSN_DELETED_P (insn));
168 if (INSN_P (insn))
169 df_insn_delete (insn);
170 remove_insn (insn);
171 INSN_DELETED_P (insn) = 1;
174 /* If deleting a jump, decrement the use count of the label. Deleting
175 the label itself should happen in the normal course of block merging. */
176 if (JUMP_P (insn))
178 if (JUMP_LABEL (insn)
179 && LABEL_P (JUMP_LABEL (insn)))
180 LABEL_NUSES (JUMP_LABEL (insn))--;
182 /* If there are more targets, remove them too. */
183 while ((note
184 = find_reg_note (insn, REG_LABEL_TARGET, NULL_RTX)) != NULL_RTX
185 && LABEL_P (XEXP (note, 0)))
187 LABEL_NUSES (XEXP (note, 0))--;
188 remove_note (insn, note);
192 /* Also if deleting any insn that references a label as an operand. */
193 while ((note = find_reg_note (insn, REG_LABEL_OPERAND, NULL_RTX)) != NULL_RTX
194 && LABEL_P (XEXP (note, 0)))
196 LABEL_NUSES (XEXP (note, 0))--;
197 remove_note (insn, note);
200 if (JUMP_TABLE_DATA_P (insn))
202 rtx pat = PATTERN (insn);
203 int diff_vec_p = GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC;
204 int len = XVECLEN (pat, diff_vec_p);
205 int i;
207 for (i = 0; i < len; i++)
209 rtx label = XEXP (XVECEXP (pat, diff_vec_p, i), 0);
211 /* When deleting code in bulk (e.g. removing many unreachable
212 blocks) we can delete a label that's a target of the vector
213 before deleting the vector itself. */
214 if (!NOTE_P (label))
215 LABEL_NUSES (label)--;
220 /* Like delete_insn but also purge dead edges from BB. */
222 void
223 delete_insn_and_edges (rtx insn)
225 bool purge = false;
227 if (INSN_P (insn)
228 && BLOCK_FOR_INSN (insn)
229 && BB_END (BLOCK_FOR_INSN (insn)) == insn)
230 purge = true;
231 delete_insn (insn);
232 if (purge)
233 purge_dead_edges (BLOCK_FOR_INSN (insn));
236 /* Unlink a chain of insns between START and FINISH, leaving notes
237 that must be paired. If CLEAR_BB is true, we set bb field for
238 insns that cannot be removed to NULL. */
240 void
241 delete_insn_chain (rtx start, rtx finish, bool clear_bb)
243 rtx prev, current;
245 /* Unchain the insns one by one. It would be quicker to delete all of these
246 with a single unchaining, rather than one at a time, but we need to keep
247 the NOTE's. */
248 current = finish;
249 while (1)
251 prev = PREV_INSN (current);
252 if (NOTE_P (current) && !can_delete_note_p (current))
254 else
255 delete_insn (current);
257 if (clear_bb && !INSN_DELETED_P (current))
258 set_block_for_insn (current, NULL);
260 if (current == start)
261 break;
262 current = prev;
266 /* Create a new basic block consisting of the instructions between HEAD and END
267 inclusive. This function is designed to allow fast BB construction - reuses
268 the note and basic block struct in BB_NOTE, if any and do not grow
269 BASIC_BLOCK chain and should be used directly only by CFG construction code.
270 END can be NULL in to create new empty basic block before HEAD. Both END
271 and HEAD can be NULL to create basic block at the end of INSN chain.
272 AFTER is the basic block we should be put after. */
274 basic_block
275 create_basic_block_structure (rtx head, rtx end, rtx bb_note, basic_block after)
277 basic_block bb;
279 if (bb_note
280 && (bb = NOTE_BASIC_BLOCK (bb_note)) != NULL
281 && bb->aux == NULL)
283 /* If we found an existing note, thread it back onto the chain. */
285 rtx after;
287 if (LABEL_P (head))
288 after = head;
289 else
291 after = PREV_INSN (head);
292 head = bb_note;
295 if (after != bb_note && NEXT_INSN (after) != bb_note)
296 reorder_insns_nobb (bb_note, bb_note, after);
298 else
300 /* Otherwise we must create a note and a basic block structure. */
302 bb = alloc_block ();
304 init_rtl_bb_info (bb);
305 if (!head && !end)
306 head = end = bb_note
307 = emit_note_after (NOTE_INSN_BASIC_BLOCK, get_last_insn ());
308 else if (LABEL_P (head) && end)
310 bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, head);
311 if (head == end)
312 end = bb_note;
314 else
316 bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, head);
317 head = bb_note;
318 if (!end)
319 end = head;
322 NOTE_BASIC_BLOCK (bb_note) = bb;
325 /* Always include the bb note in the block. */
326 if (NEXT_INSN (end) == bb_note)
327 end = bb_note;
329 BB_HEAD (bb) = head;
330 BB_END (bb) = end;
331 bb->index = last_basic_block++;
332 bb->flags = BB_NEW | BB_RTL;
333 link_block (bb, after);
334 SET_BASIC_BLOCK (bb->index, bb);
335 df_bb_refs_record (bb->index, false);
336 update_bb_for_insn (bb);
337 BB_SET_PARTITION (bb, BB_UNPARTITIONED);
339 /* Tag the block so that we know it has been used when considering
340 other basic block notes. */
341 bb->aux = bb;
343 return bb;
346 /* Create new basic block consisting of instructions in between HEAD and END
347 and place it to the BB chain after block AFTER. END can be NULL to
348 create a new empty basic block before HEAD. Both END and HEAD can be
349 NULL to create basic block at the end of INSN chain. */
351 static basic_block
352 rtl_create_basic_block (void *headp, void *endp, basic_block after)
354 rtx head = (rtx) headp, end = (rtx) endp;
355 basic_block bb;
357 /* Grow the basic block array if needed. */
358 if ((size_t) last_basic_block >= basic_block_info->length ())
360 size_t new_size = last_basic_block + (last_basic_block + 3) / 4;
361 vec_safe_grow_cleared (basic_block_info, new_size);
364 n_basic_blocks++;
366 bb = create_basic_block_structure (head, end, NULL, after);
367 bb->aux = NULL;
368 return bb;
371 static basic_block
372 cfg_layout_create_basic_block (void *head, void *end, basic_block after)
374 basic_block newbb = rtl_create_basic_block (head, end, after);
376 return newbb;
379 /* Delete the insns in a (non-live) block. We physically delete every
380 non-deleted-note insn, and update the flow graph appropriately.
382 Return nonzero if we deleted an exception handler. */
384 /* ??? Preserving all such notes strikes me as wrong. It would be nice
385 to post-process the stream to remove empty blocks, loops, ranges, etc. */
387 static void
388 rtl_delete_block (basic_block b)
390 rtx insn, end;
392 /* If the head of this block is a CODE_LABEL, then it might be the
393 label for an exception handler which can't be reached. We need
394 to remove the label from the exception_handler_label list. */
395 insn = BB_HEAD (b);
397 end = get_last_bb_insn (b);
399 /* Selectively delete the entire chain. */
400 BB_HEAD (b) = NULL;
401 delete_insn_chain (insn, end, true);
404 if (dump_file)
405 fprintf (dump_file, "deleting block %d\n", b->index);
406 df_bb_delete (b->index);
409 /* Records the basic block struct in BLOCK_FOR_INSN for every insn. */
411 void
412 compute_bb_for_insn (void)
414 basic_block bb;
416 FOR_EACH_BB (bb)
418 rtx end = BB_END (bb);
419 rtx insn;
421 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
423 BLOCK_FOR_INSN (insn) = bb;
424 if (insn == end)
425 break;
430 /* Release the basic_block_for_insn array. */
432 unsigned int
433 free_bb_for_insn (void)
435 rtx insn;
436 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
437 if (!BARRIER_P (insn))
438 BLOCK_FOR_INSN (insn) = NULL;
439 return 0;
442 static unsigned int
443 rest_of_pass_free_cfg (void)
445 #ifdef DELAY_SLOTS
446 /* The resource.c machinery uses DF but the CFG isn't guaranteed to be
447 valid at that point so it would be too late to call df_analyze. */
448 if (optimize > 0 && flag_delayed_branch)
450 df_note_add_problem ();
451 df_analyze ();
453 #endif
455 if (crtl->has_bb_partition)
456 insert_section_boundary_note ();
458 free_bb_for_insn ();
459 return 0;
462 struct rtl_opt_pass pass_free_cfg =
465 RTL_PASS,
466 "*free_cfg", /* name */
467 OPTGROUP_NONE, /* optinfo_flags */
468 NULL, /* gate */
469 rest_of_pass_free_cfg, /* execute */
470 NULL, /* sub */
471 NULL, /* next */
472 0, /* static_pass_number */
473 TV_NONE, /* tv_id */
474 0, /* properties_required */
475 0, /* properties_provided */
476 PROP_cfg, /* properties_destroyed */
477 0, /* todo_flags_start */
478 0, /* todo_flags_finish */
482 /* Return RTX to emit after when we want to emit code on the entry of function. */
484 entry_of_function (void)
486 return (n_basic_blocks > NUM_FIXED_BLOCKS ?
487 BB_HEAD (ENTRY_BLOCK_PTR->next_bb) : get_insns ());
490 /* Emit INSN at the entry point of the function, ensuring that it is only
491 executed once per function. */
492 void
493 emit_insn_at_entry (rtx insn)
495 edge_iterator ei = ei_start (ENTRY_BLOCK_PTR->succs);
496 edge e = ei_safe_edge (ei);
497 gcc_assert (e->flags & EDGE_FALLTHRU);
499 insert_insn_on_edge (insn, e);
500 commit_edge_insertions ();
503 /* Update BLOCK_FOR_INSN of insns between BEGIN and END
504 (or BARRIER if found) and notify df of the bb change.
505 The insn chain range is inclusive
506 (i.e. both BEGIN and END will be updated. */
508 static void
509 update_bb_for_insn_chain (rtx begin, rtx end, basic_block bb)
511 rtx insn;
513 end = NEXT_INSN (end);
514 for (insn = begin; insn != end; insn = NEXT_INSN (insn))
515 if (!BARRIER_P (insn))
516 df_insn_change_bb (insn, bb);
519 /* Update BLOCK_FOR_INSN of insns in BB to BB,
520 and notify df of the change. */
522 void
523 update_bb_for_insn (basic_block bb)
525 update_bb_for_insn_chain (BB_HEAD (bb), BB_END (bb), bb);
529 /* Like active_insn_p, except keep the return value clobber around
530 even after reload. */
532 static bool
533 flow_active_insn_p (const_rtx insn)
535 if (active_insn_p (insn))
536 return true;
538 /* A clobber of the function return value exists for buggy
539 programs that fail to return a value. Its effect is to
540 keep the return value from being live across the entire
541 function. If we allow it to be skipped, we introduce the
542 possibility for register lifetime confusion. */
543 if (GET_CODE (PATTERN (insn)) == CLOBBER
544 && REG_P (XEXP (PATTERN (insn), 0))
545 && REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))
546 return true;
548 return false;
551 /* Return true if the block has no effect and only forwards control flow to
552 its single destination. */
554 bool
555 contains_no_active_insn_p (const_basic_block bb)
557 rtx insn;
559 if (bb == EXIT_BLOCK_PTR || bb == ENTRY_BLOCK_PTR
560 || !single_succ_p (bb))
561 return false;
563 for (insn = BB_HEAD (bb); insn != BB_END (bb); insn = NEXT_INSN (insn))
564 if (INSN_P (insn) && flow_active_insn_p (insn))
565 return false;
567 return (!INSN_P (insn)
568 || (JUMP_P (insn) && simplejump_p (insn))
569 || !flow_active_insn_p (insn));
572 /* Likewise, but protect loop latches, headers and preheaders. */
573 /* FIXME: Make this a cfg hook. */
575 bool
576 forwarder_block_p (const_basic_block bb)
578 if (!contains_no_active_insn_p (bb))
579 return false;
581 /* Protect loop latches, headers and preheaders. */
582 if (current_loops)
584 basic_block dest;
585 if (bb->loop_father->header == bb)
586 return false;
587 dest = EDGE_SUCC (bb, 0)->dest;
588 if (dest->loop_father->header == dest)
589 return false;
592 return true;
595 /* Return nonzero if we can reach target from src by falling through. */
596 /* FIXME: Make this a cfg hook. */
598 bool
599 can_fallthru (basic_block src, basic_block target)
601 rtx insn = BB_END (src);
602 rtx insn2;
603 edge e;
604 edge_iterator ei;
606 if (target == EXIT_BLOCK_PTR)
607 return true;
608 if (src->next_bb != target)
609 return 0;
610 FOR_EACH_EDGE (e, ei, src->succs)
611 if (e->dest == EXIT_BLOCK_PTR
612 && e->flags & EDGE_FALLTHRU)
613 return 0;
615 insn2 = BB_HEAD (target);
616 if (insn2 && !active_insn_p (insn2))
617 insn2 = next_active_insn (insn2);
619 /* ??? Later we may add code to move jump tables offline. */
620 return next_active_insn (insn) == insn2;
623 /* Return nonzero if we could reach target from src by falling through,
624 if the target was made adjacent. If we already have a fall-through
625 edge to the exit block, we can't do that. */
626 static bool
627 could_fall_through (basic_block src, basic_block target)
629 edge e;
630 edge_iterator ei;
632 if (target == EXIT_BLOCK_PTR)
633 return true;
634 FOR_EACH_EDGE (e, ei, src->succs)
635 if (e->dest == EXIT_BLOCK_PTR
636 && e->flags & EDGE_FALLTHRU)
637 return 0;
638 return true;
641 /* Return the NOTE_INSN_BASIC_BLOCK of BB. */
643 bb_note (basic_block bb)
645 rtx note;
647 note = BB_HEAD (bb);
648 if (LABEL_P (note))
649 note = NEXT_INSN (note);
651 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
652 return note;
655 /* Return the INSN immediately following the NOTE_INSN_BASIC_BLOCK
656 note associated with the BLOCK. */
658 static rtx
659 first_insn_after_basic_block_note (basic_block block)
661 rtx insn;
663 /* Get the first instruction in the block. */
664 insn = BB_HEAD (block);
666 if (insn == NULL_RTX)
667 return NULL_RTX;
668 if (LABEL_P (insn))
669 insn = NEXT_INSN (insn);
670 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
672 return NEXT_INSN (insn);
675 /* Creates a new basic block just after basic block B by splitting
676 everything after specified instruction I. */
678 static basic_block
679 rtl_split_block (basic_block bb, void *insnp)
681 basic_block new_bb;
682 rtx insn = (rtx) insnp;
683 edge e;
684 edge_iterator ei;
686 if (!insn)
688 insn = first_insn_after_basic_block_note (bb);
690 if (insn)
692 rtx next = insn;
694 insn = PREV_INSN (insn);
696 /* If the block contains only debug insns, insn would have
697 been NULL in a non-debug compilation, and then we'd end
698 up emitting a DELETED note. For -fcompare-debug
699 stability, emit the note too. */
700 if (insn != BB_END (bb)
701 && DEBUG_INSN_P (next)
702 && DEBUG_INSN_P (BB_END (bb)))
704 while (next != BB_END (bb) && DEBUG_INSN_P (next))
705 next = NEXT_INSN (next);
707 if (next == BB_END (bb))
708 emit_note_after (NOTE_INSN_DELETED, next);
711 else
712 insn = get_last_insn ();
715 /* We probably should check type of the insn so that we do not create
716 inconsistent cfg. It is checked in verify_flow_info anyway, so do not
717 bother. */
718 if (insn == BB_END (bb))
719 emit_note_after (NOTE_INSN_DELETED, insn);
721 /* Create the new basic block. */
722 new_bb = create_basic_block (NEXT_INSN (insn), BB_END (bb), bb);
723 BB_COPY_PARTITION (new_bb, bb);
724 BB_END (bb) = insn;
726 /* Redirect the outgoing edges. */
727 new_bb->succs = bb->succs;
728 bb->succs = NULL;
729 FOR_EACH_EDGE (e, ei, new_bb->succs)
730 e->src = new_bb;
732 /* The new block starts off being dirty. */
733 df_set_bb_dirty (bb);
734 return new_bb;
737 /* Return true if the single edge between blocks A and B is the only place
738 in RTL which holds some unique locus. */
740 static bool
741 unique_locus_on_edge_between_p (basic_block a, basic_block b)
743 const location_t goto_locus = EDGE_SUCC (a, 0)->goto_locus;
744 rtx insn, end;
746 if (LOCATION_LOCUS (goto_locus) == UNKNOWN_LOCATION)
747 return false;
749 /* First scan block A backward. */
750 insn = BB_END (a);
751 end = PREV_INSN (BB_HEAD (a));
752 while (insn != end && (!NONDEBUG_INSN_P (insn) || !INSN_HAS_LOCATION (insn)))
753 insn = PREV_INSN (insn);
755 if (insn != end && INSN_LOCATION (insn) == goto_locus)
756 return false;
758 /* Then scan block B forward. */
759 insn = BB_HEAD (b);
760 if (insn)
762 end = NEXT_INSN (BB_END (b));
763 while (insn != end && !NONDEBUG_INSN_P (insn))
764 insn = NEXT_INSN (insn);
766 if (insn != end && INSN_HAS_LOCATION (insn)
767 && INSN_LOCATION (insn) == goto_locus)
768 return false;
771 return true;
774 /* If the single edge between blocks A and B is the only place in RTL which
775 holds some unique locus, emit a nop with that locus between the blocks. */
777 static void
778 emit_nop_for_unique_locus_between (basic_block a, basic_block b)
780 if (!unique_locus_on_edge_between_p (a, b))
781 return;
783 BB_END (a) = emit_insn_after_noloc (gen_nop (), BB_END (a), a);
784 INSN_LOCATION (BB_END (a)) = EDGE_SUCC (a, 0)->goto_locus;
787 /* Blocks A and B are to be merged into a single block A. The insns
788 are already contiguous. */
790 static void
791 rtl_merge_blocks (basic_block a, basic_block b)
793 rtx b_head = BB_HEAD (b), b_end = BB_END (b), a_end = BB_END (a);
794 rtx del_first = NULL_RTX, del_last = NULL_RTX;
795 rtx b_debug_start = b_end, b_debug_end = b_end;
796 bool forwarder_p = (b->flags & BB_FORWARDER_BLOCK) != 0;
797 int b_empty = 0;
799 if (dump_file)
800 fprintf (dump_file, "Merging block %d into block %d...\n", b->index,
801 a->index);
803 while (DEBUG_INSN_P (b_end))
804 b_end = PREV_INSN (b_debug_start = b_end);
806 /* If there was a CODE_LABEL beginning B, delete it. */
807 if (LABEL_P (b_head))
809 /* Detect basic blocks with nothing but a label. This can happen
810 in particular at the end of a function. */
811 if (b_head == b_end)
812 b_empty = 1;
814 del_first = del_last = b_head;
815 b_head = NEXT_INSN (b_head);
818 /* Delete the basic block note and handle blocks containing just that
819 note. */
820 if (NOTE_INSN_BASIC_BLOCK_P (b_head))
822 if (b_head == b_end)
823 b_empty = 1;
824 if (! del_last)
825 del_first = b_head;
827 del_last = b_head;
828 b_head = NEXT_INSN (b_head);
831 /* If there was a jump out of A, delete it. */
832 if (JUMP_P (a_end))
834 rtx prev;
836 for (prev = PREV_INSN (a_end); ; prev = PREV_INSN (prev))
837 if (!NOTE_P (prev)
838 || NOTE_INSN_BASIC_BLOCK_P (prev)
839 || prev == BB_HEAD (a))
840 break;
842 del_first = a_end;
844 #ifdef HAVE_cc0
845 /* If this was a conditional jump, we need to also delete
846 the insn that set cc0. */
847 if (only_sets_cc0_p (prev))
849 rtx tmp = prev;
851 prev = prev_nonnote_insn (prev);
852 if (!prev)
853 prev = BB_HEAD (a);
854 del_first = tmp;
856 #endif
858 a_end = PREV_INSN (del_first);
860 else if (BARRIER_P (NEXT_INSN (a_end)))
861 del_first = NEXT_INSN (a_end);
863 /* Delete everything marked above as well as crap that might be
864 hanging out between the two blocks. */
865 BB_END (a) = a_end;
866 BB_HEAD (b) = b_empty ? NULL_RTX : b_head;
867 delete_insn_chain (del_first, del_last, true);
869 /* When not optimizing CFG and the edge is the only place in RTL which holds
870 some unique locus, emit a nop with that locus in between. */
871 if (!optimize)
873 emit_nop_for_unique_locus_between (a, b);
874 a_end = BB_END (a);
877 /* Reassociate the insns of B with A. */
878 if (!b_empty)
880 update_bb_for_insn_chain (a_end, b_debug_end, a);
882 BB_END (a) = b_debug_end;
883 BB_HEAD (b) = NULL_RTX;
885 else if (b_end != b_debug_end)
887 /* Move any deleted labels and other notes between the end of A
888 and the debug insns that make up B after the debug insns,
889 bringing the debug insns into A while keeping the notes after
890 the end of A. */
891 if (NEXT_INSN (a_end) != b_debug_start)
892 reorder_insns_nobb (NEXT_INSN (a_end), PREV_INSN (b_debug_start),
893 b_debug_end);
894 update_bb_for_insn_chain (b_debug_start, b_debug_end, a);
895 BB_END (a) = b_debug_end;
898 df_bb_delete (b->index);
900 /* If B was a forwarder block, propagate the locus on the edge. */
901 if (forwarder_p
902 && LOCATION_LOCUS (EDGE_SUCC (b, 0)->goto_locus) == UNKNOWN_LOCATION)
903 EDGE_SUCC (b, 0)->goto_locus = EDGE_SUCC (a, 0)->goto_locus;
905 if (dump_file)
906 fprintf (dump_file, "Merged blocks %d and %d.\n", a->index, b->index);
910 /* Return true when block A and B can be merged. */
912 static bool
913 rtl_can_merge_blocks (basic_block a, basic_block b)
915 /* If we are partitioning hot/cold basic blocks, we don't want to
916 mess up unconditional or indirect jumps that cross between hot
917 and cold sections.
919 Basic block partitioning may result in some jumps that appear to
920 be optimizable (or blocks that appear to be mergeable), but which really
921 must be left untouched (they are required to make it safely across
922 partition boundaries). See the comments at the top of
923 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
925 if (BB_PARTITION (a) != BB_PARTITION (b))
926 return false;
928 /* Protect the loop latches. */
929 if (current_loops && b->loop_father->latch == b)
930 return false;
932 /* There must be exactly one edge in between the blocks. */
933 return (single_succ_p (a)
934 && single_succ (a) == b
935 && single_pred_p (b)
936 && a != b
937 /* Must be simple edge. */
938 && !(single_succ_edge (a)->flags & EDGE_COMPLEX)
939 && a->next_bb == b
940 && a != ENTRY_BLOCK_PTR && b != EXIT_BLOCK_PTR
941 /* If the jump insn has side effects,
942 we can't kill the edge. */
943 && (!JUMP_P (BB_END (a))
944 || (reload_completed
945 ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
948 /* Return the label in the head of basic block BLOCK. Create one if it doesn't
949 exist. */
952 block_label (basic_block block)
954 if (block == EXIT_BLOCK_PTR)
955 return NULL_RTX;
957 if (!LABEL_P (BB_HEAD (block)))
959 BB_HEAD (block) = emit_label_before (gen_label_rtx (), BB_HEAD (block));
962 return BB_HEAD (block);
965 /* Attempt to perform edge redirection by replacing possibly complex jump
966 instruction by unconditional jump or removing jump completely. This can
967 apply only if all edges now point to the same block. The parameters and
968 return values are equivalent to redirect_edge_and_branch. */
970 edge
971 try_redirect_by_replacing_jump (edge e, basic_block target, bool in_cfglayout)
973 basic_block src = e->src;
974 rtx insn = BB_END (src), kill_from;
975 rtx set;
976 int fallthru = 0;
978 /* If we are partitioning hot/cold basic blocks, we don't want to
979 mess up unconditional or indirect jumps that cross between hot
980 and cold sections.
982 Basic block partitioning may result in some jumps that appear to
983 be optimizable (or blocks that appear to be mergeable), but which really
984 must be left untouched (they are required to make it safely across
985 partition boundaries). See the comments at the top of
986 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
988 if (BB_PARTITION (src) != BB_PARTITION (target))
989 return NULL;
991 /* We can replace or remove a complex jump only when we have exactly
992 two edges. Also, if we have exactly one outgoing edge, we can
993 redirect that. */
994 if (EDGE_COUNT (src->succs) >= 3
995 /* Verify that all targets will be TARGET. Specifically, the
996 edge that is not E must also go to TARGET. */
997 || (EDGE_COUNT (src->succs) == 2
998 && EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target))
999 return NULL;
1001 if (!onlyjump_p (insn))
1002 return NULL;
1003 if ((!optimize || reload_completed) && tablejump_p (insn, NULL, NULL))
1004 return NULL;
1006 /* Avoid removing branch with side effects. */
1007 set = single_set (insn);
1008 if (!set || side_effects_p (set))
1009 return NULL;
1011 /* In case we zap a conditional jump, we'll need to kill
1012 the cc0 setter too. */
1013 kill_from = insn;
1014 #ifdef HAVE_cc0
1015 if (reg_mentioned_p (cc0_rtx, PATTERN (insn))
1016 && only_sets_cc0_p (PREV_INSN (insn)))
1017 kill_from = PREV_INSN (insn);
1018 #endif
1020 /* See if we can create the fallthru edge. */
1021 if (in_cfglayout || can_fallthru (src, target))
1023 if (dump_file)
1024 fprintf (dump_file, "Removing jump %i.\n", INSN_UID (insn));
1025 fallthru = 1;
1027 /* Selectively unlink whole insn chain. */
1028 if (in_cfglayout)
1030 rtx insn = BB_FOOTER (src);
1032 delete_insn_chain (kill_from, BB_END (src), false);
1034 /* Remove barriers but keep jumptables. */
1035 while (insn)
1037 if (BARRIER_P (insn))
1039 if (PREV_INSN (insn))
1040 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
1041 else
1042 BB_FOOTER (src) = NEXT_INSN (insn);
1043 if (NEXT_INSN (insn))
1044 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
1046 if (LABEL_P (insn))
1047 break;
1048 insn = NEXT_INSN (insn);
1051 else
1052 delete_insn_chain (kill_from, PREV_INSN (BB_HEAD (target)),
1053 false);
1056 /* If this already is simplejump, redirect it. */
1057 else if (simplejump_p (insn))
1059 if (e->dest == target)
1060 return NULL;
1061 if (dump_file)
1062 fprintf (dump_file, "Redirecting jump %i from %i to %i.\n",
1063 INSN_UID (insn), e->dest->index, target->index);
1064 if (!redirect_jump (insn, block_label (target), 0))
1066 gcc_assert (target == EXIT_BLOCK_PTR);
1067 return NULL;
1071 /* Cannot do anything for target exit block. */
1072 else if (target == EXIT_BLOCK_PTR)
1073 return NULL;
1075 /* Or replace possibly complicated jump insn by simple jump insn. */
1076 else
1078 rtx target_label = block_label (target);
1079 rtx barrier, label, table;
1081 emit_jump_insn_after_noloc (gen_jump (target_label), insn);
1082 JUMP_LABEL (BB_END (src)) = target_label;
1083 LABEL_NUSES (target_label)++;
1084 if (dump_file)
1085 fprintf (dump_file, "Replacing insn %i by jump %i\n",
1086 INSN_UID (insn), INSN_UID (BB_END (src)));
1089 delete_insn_chain (kill_from, insn, false);
1091 /* Recognize a tablejump that we are converting to a
1092 simple jump and remove its associated CODE_LABEL
1093 and ADDR_VEC or ADDR_DIFF_VEC. */
1094 if (tablejump_p (insn, &label, &table))
1095 delete_insn_chain (label, table, false);
1097 barrier = next_nonnote_insn (BB_END (src));
1098 if (!barrier || !BARRIER_P (barrier))
1099 emit_barrier_after (BB_END (src));
1100 else
1102 if (barrier != NEXT_INSN (BB_END (src)))
1104 /* Move the jump before barrier so that the notes
1105 which originally were or were created before jump table are
1106 inside the basic block. */
1107 rtx new_insn = BB_END (src);
1109 update_bb_for_insn_chain (NEXT_INSN (BB_END (src)),
1110 PREV_INSN (barrier), src);
1112 NEXT_INSN (PREV_INSN (new_insn)) = NEXT_INSN (new_insn);
1113 PREV_INSN (NEXT_INSN (new_insn)) = PREV_INSN (new_insn);
1115 NEXT_INSN (new_insn) = barrier;
1116 NEXT_INSN (PREV_INSN (barrier)) = new_insn;
1118 PREV_INSN (new_insn) = PREV_INSN (barrier);
1119 PREV_INSN (barrier) = new_insn;
1124 /* Keep only one edge out and set proper flags. */
1125 if (!single_succ_p (src))
1126 remove_edge (e);
1127 gcc_assert (single_succ_p (src));
1129 e = single_succ_edge (src);
1130 if (fallthru)
1131 e->flags = EDGE_FALLTHRU;
1132 else
1133 e->flags = 0;
1135 e->probability = REG_BR_PROB_BASE;
1136 e->count = src->count;
1138 if (e->dest != target)
1139 redirect_edge_succ (e, target);
1140 return e;
1143 /* Subroutine of redirect_branch_edge that tries to patch the jump
1144 instruction INSN so that it reaches block NEW. Do this
1145 only when it originally reached block OLD. Return true if this
1146 worked or the original target wasn't OLD, return false if redirection
1147 doesn't work. */
1149 static bool
1150 patch_jump_insn (rtx insn, rtx old_label, basic_block new_bb)
1152 rtx tmp;
1153 /* Recognize a tablejump and adjust all matching cases. */
1154 if (tablejump_p (insn, NULL, &tmp))
1156 rtvec vec;
1157 int j;
1158 rtx new_label = block_label (new_bb);
1160 if (new_bb == EXIT_BLOCK_PTR)
1161 return false;
1162 if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
1163 vec = XVEC (PATTERN (tmp), 0);
1164 else
1165 vec = XVEC (PATTERN (tmp), 1);
1167 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
1168 if (XEXP (RTVEC_ELT (vec, j), 0) == old_label)
1170 RTVEC_ELT (vec, j) = gen_rtx_LABEL_REF (Pmode, new_label);
1171 --LABEL_NUSES (old_label);
1172 ++LABEL_NUSES (new_label);
1175 /* Handle casesi dispatch insns. */
1176 if ((tmp = single_set (insn)) != NULL
1177 && SET_DEST (tmp) == pc_rtx
1178 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
1179 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF
1180 && XEXP (XEXP (SET_SRC (tmp), 2), 0) == old_label)
1182 XEXP (SET_SRC (tmp), 2) = gen_rtx_LABEL_REF (Pmode,
1183 new_label);
1184 --LABEL_NUSES (old_label);
1185 ++LABEL_NUSES (new_label);
1188 else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL)
1190 int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp);
1191 rtx new_label, note;
1193 if (new_bb == EXIT_BLOCK_PTR)
1194 return false;
1195 new_label = block_label (new_bb);
1197 for (i = 0; i < n; ++i)
1199 rtx old_ref = ASM_OPERANDS_LABEL (tmp, i);
1200 gcc_assert (GET_CODE (old_ref) == LABEL_REF);
1201 if (XEXP (old_ref, 0) == old_label)
1203 ASM_OPERANDS_LABEL (tmp, i)
1204 = gen_rtx_LABEL_REF (Pmode, new_label);
1205 --LABEL_NUSES (old_label);
1206 ++LABEL_NUSES (new_label);
1210 if (JUMP_LABEL (insn) == old_label)
1212 JUMP_LABEL (insn) = new_label;
1213 note = find_reg_note (insn, REG_LABEL_TARGET, new_label);
1214 if (note)
1215 remove_note (insn, note);
1217 else
1219 note = find_reg_note (insn, REG_LABEL_TARGET, old_label);
1220 if (note)
1221 remove_note (insn, note);
1222 if (JUMP_LABEL (insn) != new_label
1223 && !find_reg_note (insn, REG_LABEL_TARGET, new_label))
1224 add_reg_note (insn, REG_LABEL_TARGET, new_label);
1226 while ((note = find_reg_note (insn, REG_LABEL_OPERAND, old_label))
1227 != NULL_RTX)
1228 XEXP (note, 0) = new_label;
1230 else
1232 /* ?? We may play the games with moving the named labels from
1233 one basic block to the other in case only one computed_jump is
1234 available. */
1235 if (computed_jump_p (insn)
1236 /* A return instruction can't be redirected. */
1237 || returnjump_p (insn))
1238 return false;
1240 if (!currently_expanding_to_rtl || JUMP_LABEL (insn) == old_label)
1242 /* If the insn doesn't go where we think, we're confused. */
1243 gcc_assert (JUMP_LABEL (insn) == old_label);
1245 /* If the substitution doesn't succeed, die. This can happen
1246 if the back end emitted unrecognizable instructions or if
1247 target is exit block on some arches. */
1248 if (!redirect_jump (insn, block_label (new_bb), 0))
1250 gcc_assert (new_bb == EXIT_BLOCK_PTR);
1251 return false;
1255 return true;
1259 /* Redirect edge representing branch of (un)conditional jump or tablejump,
1260 NULL on failure */
1261 static edge
1262 redirect_branch_edge (edge e, basic_block target)
1264 rtx old_label = BB_HEAD (e->dest);
1265 basic_block src = e->src;
1266 rtx insn = BB_END (src);
1268 /* We can only redirect non-fallthru edges of jump insn. */
1269 if (e->flags & EDGE_FALLTHRU)
1270 return NULL;
1271 else if (!JUMP_P (insn) && !currently_expanding_to_rtl)
1272 return NULL;
1274 if (!currently_expanding_to_rtl)
1276 if (!patch_jump_insn (insn, old_label, target))
1277 return NULL;
1279 else
1280 /* When expanding this BB might actually contain multiple
1281 jumps (i.e. not yet split by find_many_sub_basic_blocks).
1282 Redirect all of those that match our label. */
1283 FOR_BB_INSNS (src, insn)
1284 if (JUMP_P (insn) && !patch_jump_insn (insn, old_label, target))
1285 return NULL;
1287 if (dump_file)
1288 fprintf (dump_file, "Edge %i->%i redirected to %i\n",
1289 e->src->index, e->dest->index, target->index);
1291 if (e->dest != target)
1292 e = redirect_edge_succ_nodup (e, target);
1294 return e;
1297 /* Called when edge E has been redirected to a new destination,
1298 in order to update the region crossing flag on the edge and
1299 jump. */
1301 static void
1302 fixup_partition_crossing (edge e)
1304 rtx note;
1306 if (e->src == ENTRY_BLOCK_PTR || e->dest == EXIT_BLOCK_PTR)
1307 return;
1308 /* If we redirected an existing edge, it may already be marked
1309 crossing, even though the new src is missing a reg crossing note.
1310 But make sure reg crossing note doesn't already exist before
1311 inserting. */
1312 if (BB_PARTITION (e->src) != BB_PARTITION (e->dest))
1314 e->flags |= EDGE_CROSSING;
1315 note = find_reg_note (BB_END (e->src), REG_CROSSING_JUMP, NULL_RTX);
1316 if (JUMP_P (BB_END (e->src))
1317 && !note)
1318 add_reg_note (BB_END (e->src), REG_CROSSING_JUMP, NULL_RTX);
1320 else if (BB_PARTITION (e->src) == BB_PARTITION (e->dest))
1322 e->flags &= ~EDGE_CROSSING;
1323 /* Remove the section crossing note from jump at end of
1324 src if it exists, and if no other successors are
1325 still crossing. */
1326 note = find_reg_note (BB_END (e->src), REG_CROSSING_JUMP, NULL_RTX);
1327 if (note)
1329 bool has_crossing_succ = false;
1330 edge e2;
1331 edge_iterator ei;
1332 FOR_EACH_EDGE (e2, ei, e->src->succs)
1334 has_crossing_succ |= (e2->flags & EDGE_CROSSING);
1335 if (has_crossing_succ)
1336 break;
1338 if (!has_crossing_succ)
1339 remove_note (BB_END (e->src), note);
1344 /* Attempt to change code to redirect edge E to TARGET. Don't do that on
1345 expense of adding new instructions or reordering basic blocks.
1347 Function can be also called with edge destination equivalent to the TARGET.
1348 Then it should try the simplifications and do nothing if none is possible.
1350 Return edge representing the branch if transformation succeeded. Return NULL
1351 on failure.
1352 We still return NULL in case E already destinated TARGET and we didn't
1353 managed to simplify instruction stream. */
1355 static edge
1356 rtl_redirect_edge_and_branch (edge e, basic_block target)
1358 edge ret;
1359 basic_block src = e->src;
1360 basic_block dest = e->dest;
1362 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
1363 return NULL;
1365 if (dest == target)
1366 return e;
1368 if ((ret = try_redirect_by_replacing_jump (e, target, false)) != NULL)
1370 df_set_bb_dirty (src);
1371 fixup_partition_crossing (ret);
1372 return ret;
1375 ret = redirect_branch_edge (e, target);
1376 if (!ret)
1377 return NULL;
1379 df_set_bb_dirty (src);
1380 fixup_partition_crossing (ret);
1381 return ret;
1384 /* Emit a barrier after BB, into the footer if we are in CFGLAYOUT mode. */
1386 void
1387 emit_barrier_after_bb (basic_block bb)
1389 rtx barrier = emit_barrier_after (BB_END (bb));
1390 gcc_assert (current_ir_type() == IR_RTL_CFGRTL
1391 || current_ir_type () == IR_RTL_CFGLAYOUT);
1392 if (current_ir_type () == IR_RTL_CFGLAYOUT)
1393 BB_FOOTER (bb) = unlink_insn_chain (barrier, barrier);
1396 /* Like force_nonfallthru below, but additionally performs redirection
1397 Used by redirect_edge_and_branch_force. JUMP_LABEL is used only
1398 when redirecting to the EXIT_BLOCK, it is either ret_rtx or
1399 simple_return_rtx, indicating which kind of returnjump to create.
1400 It should be NULL otherwise. */
1402 basic_block
1403 force_nonfallthru_and_redirect (edge e, basic_block target, rtx jump_label)
1405 basic_block jump_block, new_bb = NULL, src = e->src;
1406 rtx note;
1407 edge new_edge;
1408 int abnormal_edge_flags = 0;
1409 bool asm_goto_edge = false;
1410 int loc;
1412 /* In the case the last instruction is conditional jump to the next
1413 instruction, first redirect the jump itself and then continue
1414 by creating a basic block afterwards to redirect fallthru edge. */
1415 if (e->src != ENTRY_BLOCK_PTR && e->dest != EXIT_BLOCK_PTR
1416 && any_condjump_p (BB_END (e->src))
1417 && JUMP_LABEL (BB_END (e->src)) == BB_HEAD (e->dest))
1419 rtx note;
1420 edge b = unchecked_make_edge (e->src, target, 0);
1421 bool redirected;
1423 redirected = redirect_jump (BB_END (e->src), block_label (target), 0);
1424 gcc_assert (redirected);
1426 note = find_reg_note (BB_END (e->src), REG_BR_PROB, NULL_RTX);
1427 if (note)
1429 int prob = INTVAL (XEXP (note, 0));
1431 b->probability = prob;
1432 /* Update this to use GCOV_COMPUTE_SCALE. */
1433 b->count = e->count * prob / REG_BR_PROB_BASE;
1434 e->probability -= e->probability;
1435 e->count -= b->count;
1436 if (e->probability < 0)
1437 e->probability = 0;
1438 if (e->count < 0)
1439 e->count = 0;
1443 if (e->flags & EDGE_ABNORMAL)
1445 /* Irritating special case - fallthru edge to the same block as abnormal
1446 edge.
1447 We can't redirect abnormal edge, but we still can split the fallthru
1448 one and create separate abnormal edge to original destination.
1449 This allows bb-reorder to make such edge non-fallthru. */
1450 gcc_assert (e->dest == target);
1451 abnormal_edge_flags = e->flags & ~EDGE_FALLTHRU;
1452 e->flags &= EDGE_FALLTHRU;
1454 else
1456 gcc_assert (e->flags & EDGE_FALLTHRU);
1457 if (e->src == ENTRY_BLOCK_PTR)
1459 /* We can't redirect the entry block. Create an empty block
1460 at the start of the function which we use to add the new
1461 jump. */
1462 edge tmp;
1463 edge_iterator ei;
1464 bool found = false;
1466 basic_block bb = create_basic_block (BB_HEAD (e->dest), NULL, ENTRY_BLOCK_PTR);
1468 /* Change the existing edge's source to be the new block, and add
1469 a new edge from the entry block to the new block. */
1470 e->src = bb;
1471 for (ei = ei_start (ENTRY_BLOCK_PTR->succs); (tmp = ei_safe_edge (ei)); )
1473 if (tmp == e)
1475 ENTRY_BLOCK_PTR->succs->unordered_remove (ei.index);
1476 found = true;
1477 break;
1479 else
1480 ei_next (&ei);
1483 gcc_assert (found);
1485 vec_safe_push (bb->succs, e);
1486 make_single_succ_edge (ENTRY_BLOCK_PTR, bb, EDGE_FALLTHRU);
1490 /* If e->src ends with asm goto, see if any of the ASM_OPERANDS_LABELs
1491 don't point to the target or fallthru label. */
1492 if (JUMP_P (BB_END (e->src))
1493 && target != EXIT_BLOCK_PTR
1494 && (e->flags & EDGE_FALLTHRU)
1495 && (note = extract_asm_operands (PATTERN (BB_END (e->src)))))
1497 int i, n = ASM_OPERANDS_LABEL_LENGTH (note);
1498 bool adjust_jump_target = false;
1500 for (i = 0; i < n; ++i)
1502 if (XEXP (ASM_OPERANDS_LABEL (note, i), 0) == BB_HEAD (e->dest))
1504 LABEL_NUSES (XEXP (ASM_OPERANDS_LABEL (note, i), 0))--;
1505 XEXP (ASM_OPERANDS_LABEL (note, i), 0) = block_label (target);
1506 LABEL_NUSES (XEXP (ASM_OPERANDS_LABEL (note, i), 0))++;
1507 adjust_jump_target = true;
1509 if (XEXP (ASM_OPERANDS_LABEL (note, i), 0) == BB_HEAD (target))
1510 asm_goto_edge = true;
1512 if (adjust_jump_target)
1514 rtx insn = BB_END (e->src), note;
1515 rtx old_label = BB_HEAD (e->dest);
1516 rtx new_label = BB_HEAD (target);
1518 if (JUMP_LABEL (insn) == old_label)
1520 JUMP_LABEL (insn) = new_label;
1521 note = find_reg_note (insn, REG_LABEL_TARGET, new_label);
1522 if (note)
1523 remove_note (insn, note);
1525 else
1527 note = find_reg_note (insn, REG_LABEL_TARGET, old_label);
1528 if (note)
1529 remove_note (insn, note);
1530 if (JUMP_LABEL (insn) != new_label
1531 && !find_reg_note (insn, REG_LABEL_TARGET, new_label))
1532 add_reg_note (insn, REG_LABEL_TARGET, new_label);
1534 while ((note = find_reg_note (insn, REG_LABEL_OPERAND, old_label))
1535 != NULL_RTX)
1536 XEXP (note, 0) = new_label;
1540 if (EDGE_COUNT (e->src->succs) >= 2 || abnormal_edge_flags || asm_goto_edge)
1542 gcov_type count = e->count;
1543 int probability = e->probability;
1544 /* Create the new structures. */
1546 /* If the old block ended with a tablejump, skip its table
1547 by searching forward from there. Otherwise start searching
1548 forward from the last instruction of the old block. */
1549 if (!tablejump_p (BB_END (e->src), NULL, &note))
1550 note = BB_END (e->src);
1551 note = NEXT_INSN (note);
1553 jump_block = create_basic_block (note, NULL, e->src);
1554 jump_block->count = count;
1555 jump_block->frequency = EDGE_FREQUENCY (e);
1557 /* Make sure new block ends up in correct hot/cold section. */
1559 BB_COPY_PARTITION (jump_block, e->src);
1561 /* Wire edge in. */
1562 new_edge = make_edge (e->src, jump_block, EDGE_FALLTHRU);
1563 new_edge->probability = probability;
1564 new_edge->count = count;
1566 /* Redirect old edge. */
1567 redirect_edge_pred (e, jump_block);
1568 e->probability = REG_BR_PROB_BASE;
1570 /* If e->src was previously region crossing, it no longer is
1571 and the reg crossing note should be removed. */
1572 fixup_partition_crossing (new_edge);
1574 /* If asm goto has any label refs to target's label,
1575 add also edge from asm goto bb to target. */
1576 if (asm_goto_edge)
1578 new_edge->probability /= 2;
1579 new_edge->count /= 2;
1580 jump_block->count /= 2;
1581 jump_block->frequency /= 2;
1582 new_edge = make_edge (new_edge->src, target,
1583 e->flags & ~EDGE_FALLTHRU);
1584 new_edge->probability = probability - probability / 2;
1585 new_edge->count = count - count / 2;
1588 new_bb = jump_block;
1590 else
1591 jump_block = e->src;
1593 loc = e->goto_locus;
1594 e->flags &= ~EDGE_FALLTHRU;
1595 if (target == EXIT_BLOCK_PTR)
1597 if (jump_label == ret_rtx)
1599 #ifdef HAVE_return
1600 emit_jump_insn_after_setloc (gen_return (), BB_END (jump_block), loc);
1601 #else
1602 gcc_unreachable ();
1603 #endif
1605 else
1607 gcc_assert (jump_label == simple_return_rtx);
1608 #ifdef HAVE_simple_return
1609 emit_jump_insn_after_setloc (gen_simple_return (),
1610 BB_END (jump_block), loc);
1611 #else
1612 gcc_unreachable ();
1613 #endif
1615 set_return_jump_label (BB_END (jump_block));
1617 else
1619 rtx label = block_label (target);
1620 emit_jump_insn_after_setloc (gen_jump (label), BB_END (jump_block), loc);
1621 JUMP_LABEL (BB_END (jump_block)) = label;
1622 LABEL_NUSES (label)++;
1625 /* We might be in cfg layout mode, and if so, the following routine will
1626 insert the barrier correctly. */
1627 emit_barrier_after_bb (jump_block);
1628 redirect_edge_succ_nodup (e, target);
1630 if (abnormal_edge_flags)
1631 make_edge (src, target, abnormal_edge_flags);
1633 df_mark_solutions_dirty ();
1634 fixup_partition_crossing (e);
1635 return new_bb;
1638 /* Edge E is assumed to be fallthru edge. Emit needed jump instruction
1639 (and possibly create new basic block) to make edge non-fallthru.
1640 Return newly created BB or NULL if none. */
1642 static basic_block
1643 rtl_force_nonfallthru (edge e)
1645 return force_nonfallthru_and_redirect (e, e->dest, NULL_RTX);
1648 /* Redirect edge even at the expense of creating new jump insn or
1649 basic block. Return new basic block if created, NULL otherwise.
1650 Conversion must be possible. */
1652 static basic_block
1653 rtl_redirect_edge_and_branch_force (edge e, basic_block target)
1655 if (redirect_edge_and_branch (e, target)
1656 || e->dest == target)
1657 return NULL;
1659 /* In case the edge redirection failed, try to force it to be non-fallthru
1660 and redirect newly created simplejump. */
1661 df_set_bb_dirty (e->src);
1662 return force_nonfallthru_and_redirect (e, target, NULL_RTX);
1665 /* The given edge should potentially be a fallthru edge. If that is in
1666 fact true, delete the jump and barriers that are in the way. */
1668 static void
1669 rtl_tidy_fallthru_edge (edge e)
1671 rtx q;
1672 basic_block b = e->src, c = b->next_bb;
1674 /* ??? In a late-running flow pass, other folks may have deleted basic
1675 blocks by nopping out blocks, leaving multiple BARRIERs between here
1676 and the target label. They ought to be chastised and fixed.
1678 We can also wind up with a sequence of undeletable labels between
1679 one block and the next.
1681 So search through a sequence of barriers, labels, and notes for
1682 the head of block C and assert that we really do fall through. */
1684 for (q = NEXT_INSN (BB_END (b)); q != BB_HEAD (c); q = NEXT_INSN (q))
1685 if (INSN_P (q))
1686 return;
1688 /* Remove what will soon cease being the jump insn from the source block.
1689 If block B consisted only of this single jump, turn it into a deleted
1690 note. */
1691 q = BB_END (b);
1692 if (JUMP_P (q)
1693 && onlyjump_p (q)
1694 && (any_uncondjump_p (q)
1695 || single_succ_p (b)))
1697 #ifdef HAVE_cc0
1698 /* If this was a conditional jump, we need to also delete
1699 the insn that set cc0. */
1700 if (any_condjump_p (q) && only_sets_cc0_p (PREV_INSN (q)))
1701 q = PREV_INSN (q);
1702 #endif
1704 q = PREV_INSN (q);
1707 /* Selectively unlink the sequence. */
1708 if (q != PREV_INSN (BB_HEAD (c)))
1709 delete_insn_chain (NEXT_INSN (q), PREV_INSN (BB_HEAD (c)), false);
1711 e->flags |= EDGE_FALLTHRU;
1714 /* Should move basic block BB after basic block AFTER. NIY. */
1716 static bool
1717 rtl_move_block_after (basic_block bb ATTRIBUTE_UNUSED,
1718 basic_block after ATTRIBUTE_UNUSED)
1720 return false;
1723 /* Locate the last bb in the same partition as START_BB. */
1725 static basic_block
1726 last_bb_in_partition (basic_block start_bb)
1728 basic_block bb;
1729 FOR_BB_BETWEEN (bb, start_bb, EXIT_BLOCK_PTR, next_bb)
1731 if (BB_PARTITION (start_bb) != BB_PARTITION (bb->next_bb))
1732 return bb;
1734 /* Return bb before EXIT_BLOCK_PTR. */
1735 return bb->prev_bb;
1738 /* Split a (typically critical) edge. Return the new block.
1739 The edge must not be abnormal.
1741 ??? The code generally expects to be called on critical edges.
1742 The case of a block ending in an unconditional jump to a
1743 block with multiple predecessors is not handled optimally. */
1745 static basic_block
1746 rtl_split_edge (edge edge_in)
1748 basic_block bb, new_bb;
1749 rtx before;
1751 /* Abnormal edges cannot be split. */
1752 gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
1754 /* We are going to place the new block in front of edge destination.
1755 Avoid existence of fallthru predecessors. */
1756 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1758 edge e = find_fallthru_edge (edge_in->dest->preds);
1760 if (e)
1761 force_nonfallthru (e);
1764 /* Create the basic block note. */
1765 if (edge_in->dest != EXIT_BLOCK_PTR)
1766 before = BB_HEAD (edge_in->dest);
1767 else
1768 before = NULL_RTX;
1770 /* If this is a fall through edge to the exit block, the blocks might be
1771 not adjacent, and the right place is after the source. */
1772 if ((edge_in->flags & EDGE_FALLTHRU) && edge_in->dest == EXIT_BLOCK_PTR)
1774 before = NEXT_INSN (BB_END (edge_in->src));
1775 bb = create_basic_block (before, NULL, edge_in->src);
1776 BB_COPY_PARTITION (bb, edge_in->src);
1778 else
1780 if (edge_in->src == ENTRY_BLOCK_PTR)
1782 bb = create_basic_block (before, NULL, edge_in->dest->prev_bb);
1783 BB_COPY_PARTITION (bb, edge_in->dest);
1785 else
1787 basic_block after = edge_in->dest->prev_bb;
1788 /* If this is post-bb reordering, and the edge crosses a partition
1789 boundary, the new block needs to be inserted in the bb chain
1790 at the end of the src partition (since we put the new bb into
1791 that partition, see below). Otherwise we may end up creating
1792 an extra partition crossing in the chain, which is illegal.
1793 It can't go after the src, because src may have a fall-through
1794 to a different block. */
1795 if (crtl->bb_reorder_complete
1796 && (edge_in->flags & EDGE_CROSSING))
1798 after = last_bb_in_partition (edge_in->src);
1799 before = NEXT_INSN (BB_END (after));
1800 /* The instruction following the last bb in partition should
1801 be a barrier, since it cannot end in a fall-through. */
1802 gcc_checking_assert (BARRIER_P (before));
1803 before = NEXT_INSN (before);
1805 bb = create_basic_block (before, NULL, after);
1806 /* Put the split bb into the src partition, to avoid creating
1807 a situation where a cold bb dominates a hot bb, in the case
1808 where src is cold and dest is hot. The src will dominate
1809 the new bb (whereas it might not have dominated dest). */
1810 BB_COPY_PARTITION (bb, edge_in->src);
1814 make_single_succ_edge (bb, edge_in->dest, EDGE_FALLTHRU);
1816 /* Can't allow a region crossing edge to be fallthrough. */
1817 if (BB_PARTITION (bb) != BB_PARTITION (edge_in->dest)
1818 && edge_in->dest != EXIT_BLOCK_PTR)
1820 new_bb = force_nonfallthru (single_succ_edge (bb));
1821 gcc_assert (!new_bb);
1824 /* For non-fallthru edges, we must adjust the predecessor's
1825 jump instruction to target our new block. */
1826 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1828 edge redirected = redirect_edge_and_branch (edge_in, bb);
1829 gcc_assert (redirected);
1831 else
1833 if (edge_in->src != ENTRY_BLOCK_PTR)
1835 /* For asm goto even splitting of fallthru edge might
1836 need insn patching, as other labels might point to the
1837 old label. */
1838 rtx last = BB_END (edge_in->src);
1839 if (last
1840 && JUMP_P (last)
1841 && edge_in->dest != EXIT_BLOCK_PTR
1842 && extract_asm_operands (PATTERN (last)) != NULL_RTX
1843 && patch_jump_insn (last, before, bb))
1844 df_set_bb_dirty (edge_in->src);
1846 redirect_edge_succ (edge_in, bb);
1849 return bb;
1852 /* Queue instructions for insertion on an edge between two basic blocks.
1853 The new instructions and basic blocks (if any) will not appear in the
1854 CFG until commit_edge_insertions is called. */
1856 void
1857 insert_insn_on_edge (rtx pattern, edge e)
1859 /* We cannot insert instructions on an abnormal critical edge.
1860 It will be easier to find the culprit if we die now. */
1861 gcc_assert (!((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e)));
1863 if (e->insns.r == NULL_RTX)
1864 start_sequence ();
1865 else
1866 push_to_sequence (e->insns.r);
1868 emit_insn (pattern);
1870 e->insns.r = get_insns ();
1871 end_sequence ();
1874 /* Update the CFG for the instructions queued on edge E. */
1876 void
1877 commit_one_edge_insertion (edge e)
1879 rtx before = NULL_RTX, after = NULL_RTX, insns, tmp, last;
1880 basic_block bb;
1882 /* Pull the insns off the edge now since the edge might go away. */
1883 insns = e->insns.r;
1884 e->insns.r = NULL_RTX;
1886 /* Figure out where to put these insns. If the destination has
1887 one predecessor, insert there. Except for the exit block. */
1888 if (single_pred_p (e->dest) && e->dest != EXIT_BLOCK_PTR)
1890 bb = e->dest;
1892 /* Get the location correct wrt a code label, and "nice" wrt
1893 a basic block note, and before everything else. */
1894 tmp = BB_HEAD (bb);
1895 if (LABEL_P (tmp))
1896 tmp = NEXT_INSN (tmp);
1897 if (NOTE_INSN_BASIC_BLOCK_P (tmp))
1898 tmp = NEXT_INSN (tmp);
1899 if (tmp == BB_HEAD (bb))
1900 before = tmp;
1901 else if (tmp)
1902 after = PREV_INSN (tmp);
1903 else
1904 after = get_last_insn ();
1907 /* If the source has one successor and the edge is not abnormal,
1908 insert there. Except for the entry block. */
1909 else if ((e->flags & EDGE_ABNORMAL) == 0
1910 && single_succ_p (e->src)
1911 && e->src != ENTRY_BLOCK_PTR)
1913 bb = e->src;
1915 /* It is possible to have a non-simple jump here. Consider a target
1916 where some forms of unconditional jumps clobber a register. This
1917 happens on the fr30 for example.
1919 We know this block has a single successor, so we can just emit
1920 the queued insns before the jump. */
1921 if (JUMP_P (BB_END (bb)))
1922 before = BB_END (bb);
1923 else
1925 /* We'd better be fallthru, or we've lost track of what's what. */
1926 gcc_assert (e->flags & EDGE_FALLTHRU);
1928 after = BB_END (bb);
1932 /* Otherwise we must split the edge. */
1933 else
1935 bb = split_edge (e);
1937 /* If E crossed a partition boundary, we needed to make bb end in
1938 a region-crossing jump, even though it was originally fallthru. */
1939 if (JUMP_P (BB_END (bb)))
1940 before = BB_END (bb);
1941 else
1942 after = BB_END (bb);
1945 /* Now that we've found the spot, do the insertion. */
1946 if (before)
1948 emit_insn_before_noloc (insns, before, bb);
1949 last = prev_nonnote_insn (before);
1951 else
1952 last = emit_insn_after_noloc (insns, after, bb);
1954 if (returnjump_p (last))
1956 /* ??? Remove all outgoing edges from BB and add one for EXIT.
1957 This is not currently a problem because this only happens
1958 for the (single) epilogue, which already has a fallthru edge
1959 to EXIT. */
1961 e = single_succ_edge (bb);
1962 gcc_assert (e->dest == EXIT_BLOCK_PTR
1963 && single_succ_p (bb) && (e->flags & EDGE_FALLTHRU));
1965 e->flags &= ~EDGE_FALLTHRU;
1966 emit_barrier_after (last);
1968 if (before)
1969 delete_insn (before);
1971 else
1972 gcc_assert (!JUMP_P (last));
1975 /* Update the CFG for all queued instructions. */
1977 void
1978 commit_edge_insertions (void)
1980 basic_block bb;
1982 #ifdef ENABLE_CHECKING
1983 verify_flow_info ();
1984 #endif
1986 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
1988 edge e;
1989 edge_iterator ei;
1991 FOR_EACH_EDGE (e, ei, bb->succs)
1992 if (e->insns.r)
1993 commit_one_edge_insertion (e);
1998 /* Print out RTL-specific basic block information (live information
1999 at start and end with TDF_DETAILS). FLAGS are the TDF_* masks
2000 documented in dumpfile.h. */
2002 static void
2003 rtl_dump_bb (FILE *outf, basic_block bb, int indent, int flags)
2005 rtx insn;
2006 rtx last;
2007 char *s_indent;
2009 s_indent = (char *) alloca ((size_t) indent + 1);
2010 memset (s_indent, ' ', (size_t) indent);
2011 s_indent[indent] = '\0';
2013 if (df && (flags & TDF_DETAILS))
2015 df_dump_top (bb, outf);
2016 putc ('\n', outf);
2019 if (bb->index != ENTRY_BLOCK && bb->index != EXIT_BLOCK)
2020 for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb)); insn != last;
2021 insn = NEXT_INSN (insn))
2023 if (flags & TDF_DETAILS)
2024 df_dump_insn_top (insn, outf);
2025 if (! (flags & TDF_SLIM))
2026 print_rtl_single (outf, insn);
2027 else
2028 dump_insn_slim (outf, insn);
2029 if (flags & TDF_DETAILS)
2030 df_dump_insn_bottom (insn, outf);
2033 if (df && (flags & TDF_DETAILS))
2035 df_dump_bottom (bb, outf);
2036 putc ('\n', outf);
2041 /* Like dump_function_to_file, but for RTL. Print out dataflow information
2042 for the start of each basic block. FLAGS are the TDF_* masks documented
2043 in dumpfile.h. */
2045 void
2046 print_rtl_with_bb (FILE *outf, const_rtx rtx_first, int flags)
2048 const_rtx tmp_rtx;
2049 if (rtx_first == 0)
2050 fprintf (outf, "(nil)\n");
2051 else
2053 enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
2054 int max_uid = get_max_uid ();
2055 basic_block *start = XCNEWVEC (basic_block, max_uid);
2056 basic_block *end = XCNEWVEC (basic_block, max_uid);
2057 enum bb_state *in_bb_p = XCNEWVEC (enum bb_state, max_uid);
2058 basic_block bb;
2060 /* After freeing the CFG, we still have BLOCK_FOR_INSN set on most
2061 insns, but the CFG is not maintained so the basic block info
2062 is not reliable. Therefore it's omitted from the dumps. */
2063 if (! (cfun->curr_properties & PROP_cfg))
2064 flags &= ~TDF_BLOCKS;
2066 if (df)
2067 df_dump_start (outf);
2069 if (flags & TDF_BLOCKS)
2071 FOR_EACH_BB_REVERSE (bb)
2073 rtx x;
2075 start[INSN_UID (BB_HEAD (bb))] = bb;
2076 end[INSN_UID (BB_END (bb))] = bb;
2077 for (x = BB_HEAD (bb); x != NULL_RTX; x = NEXT_INSN (x))
2079 enum bb_state state = IN_MULTIPLE_BB;
2081 if (in_bb_p[INSN_UID (x)] == NOT_IN_BB)
2082 state = IN_ONE_BB;
2083 in_bb_p[INSN_UID (x)] = state;
2085 if (x == BB_END (bb))
2086 break;
2091 for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
2093 if (flags & TDF_BLOCKS)
2095 bb = start[INSN_UID (tmp_rtx)];
2096 if (bb != NULL)
2098 dump_bb_info (outf, bb, 0, dump_flags | TDF_COMMENT, true, false);
2099 if (df && (flags & TDF_DETAILS))
2100 df_dump_top (bb, outf);
2103 if (in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB
2104 && !NOTE_P (tmp_rtx)
2105 && !BARRIER_P (tmp_rtx))
2106 fprintf (outf, ";; Insn is not within a basic block\n");
2107 else if (in_bb_p[INSN_UID (tmp_rtx)] == IN_MULTIPLE_BB)
2108 fprintf (outf, ";; Insn is in multiple basic blocks\n");
2111 if (flags & TDF_DETAILS)
2112 df_dump_insn_top (tmp_rtx, outf);
2113 if (! (flags & TDF_SLIM))
2114 print_rtl_single (outf, tmp_rtx);
2115 else
2116 dump_insn_slim (outf, tmp_rtx);
2117 if (flags & TDF_DETAILS)
2118 df_dump_insn_bottom (tmp_rtx, outf);
2120 if (flags & TDF_BLOCKS)
2122 bb = end[INSN_UID (tmp_rtx)];
2123 if (bb != NULL)
2125 dump_bb_info (outf, bb, 0, dump_flags | TDF_COMMENT, false, true);
2126 if (df && (flags & TDF_DETAILS))
2127 df_dump_bottom (bb, outf);
2128 putc ('\n', outf);
2133 free (start);
2134 free (end);
2135 free (in_bb_p);
2139 /* Update the branch probability of BB if a REG_BR_PROB is present. */
2141 void
2142 update_br_prob_note (basic_block bb)
2144 rtx note;
2145 if (!JUMP_P (BB_END (bb)))
2146 return;
2147 note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX);
2148 if (!note || INTVAL (XEXP (note, 0)) == BRANCH_EDGE (bb)->probability)
2149 return;
2150 XEXP (note, 0) = GEN_INT (BRANCH_EDGE (bb)->probability);
2153 /* Get the last insn associated with block BB (that includes barriers and
2154 tablejumps after BB). */
2156 get_last_bb_insn (basic_block bb)
2158 rtx tmp;
2159 rtx end = BB_END (bb);
2161 /* Include any jump table following the basic block. */
2162 if (tablejump_p (end, NULL, &tmp))
2163 end = tmp;
2165 /* Include any barriers that may follow the basic block. */
2166 tmp = next_nonnote_insn_bb (end);
2167 while (tmp && BARRIER_P (tmp))
2169 end = tmp;
2170 tmp = next_nonnote_insn_bb (end);
2173 return end;
2176 /* Verify, in the basic block chain, that there is at most one switch
2177 between hot/cold partitions. This condition will not be true until
2178 after reorder_basic_blocks is called. */
2180 static int
2181 verify_hot_cold_block_grouping (void)
2183 basic_block bb;
2184 int err = 0;
2185 bool switched_sections = false;
2186 int current_partition = BB_UNPARTITIONED;
2188 /* Even after bb reordering is complete, we go into cfglayout mode
2189 again (in compgoto). Ensure we don't call this before going back
2190 into linearized RTL when any layout fixes would have been committed. */
2191 if (!crtl->bb_reorder_complete
2192 || current_ir_type() != IR_RTL_CFGRTL)
2193 return err;
2195 FOR_EACH_BB (bb)
2197 if (current_partition != BB_UNPARTITIONED
2198 && BB_PARTITION (bb) != current_partition)
2200 if (switched_sections)
2202 error ("multiple hot/cold transitions found (bb %i)",
2203 bb->index);
2204 err = 1;
2206 else
2207 switched_sections = true;
2209 if (!crtl->has_bb_partition)
2210 error ("partition found but function partition flag not set");
2212 current_partition = BB_PARTITION (bb);
2215 return err;
2219 /* Perform several checks on the edges out of each block, such as
2220 the consistency of the branch probabilities, the correctness
2221 of hot/cold partition crossing edges, and the number of expected
2222 successor edges. */
2224 static int
2225 rtl_verify_edges (void)
2227 int err = 0;
2228 basic_block bb;
2230 FOR_EACH_BB_REVERSE (bb)
2232 int n_fallthru = 0, n_branch = 0, n_abnormal_call = 0, n_sibcall = 0;
2233 int n_eh = 0, n_abnormal = 0;
2234 edge e, fallthru = NULL;
2235 edge_iterator ei;
2236 rtx note;
2237 bool has_crossing_edge = false;
2239 if (JUMP_P (BB_END (bb))
2240 && (note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX))
2241 && EDGE_COUNT (bb->succs) >= 2
2242 && any_condjump_p (BB_END (bb)))
2244 if (INTVAL (XEXP (note, 0)) != BRANCH_EDGE (bb)->probability
2245 && profile_status != PROFILE_ABSENT)
2247 error ("verify_flow_info: REG_BR_PROB does not match cfg %wi %i",
2248 INTVAL (XEXP (note, 0)), BRANCH_EDGE (bb)->probability);
2249 err = 1;
2253 FOR_EACH_EDGE (e, ei, bb->succs)
2255 bool is_crossing;
2257 if (e->flags & EDGE_FALLTHRU)
2258 n_fallthru++, fallthru = e;
2260 is_crossing = (BB_PARTITION (e->src) != BB_PARTITION (e->dest)
2261 && e->src != ENTRY_BLOCK_PTR
2262 && e->dest != EXIT_BLOCK_PTR);
2263 has_crossing_edge |= is_crossing;
2264 if (e->flags & EDGE_CROSSING)
2266 if (!is_crossing)
2268 error ("EDGE_CROSSING incorrectly set across same section");
2269 err = 1;
2271 if (e->flags & EDGE_FALLTHRU)
2273 error ("fallthru edge crosses section boundary in bb %i",
2274 e->src->index);
2275 err = 1;
2277 if (e->flags & EDGE_EH)
2279 error ("EH edge crosses section boundary in bb %i",
2280 e->src->index);
2281 err = 1;
2283 if (JUMP_P (BB_END (bb))
2284 && !find_reg_note (BB_END (bb), REG_CROSSING_JUMP, NULL_RTX))
2286 error ("No region crossing jump at section boundary in bb %i",
2287 bb->index);
2288 err = 1;
2291 else if (is_crossing)
2293 error ("EDGE_CROSSING missing across section boundary");
2294 err = 1;
2297 if ((e->flags & ~(EDGE_DFS_BACK
2298 | EDGE_CAN_FALLTHRU
2299 | EDGE_IRREDUCIBLE_LOOP
2300 | EDGE_LOOP_EXIT
2301 | EDGE_CROSSING
2302 | EDGE_PRESERVE)) == 0)
2303 n_branch++;
2305 if (e->flags & EDGE_ABNORMAL_CALL)
2306 n_abnormal_call++;
2308 if (e->flags & EDGE_SIBCALL)
2309 n_sibcall++;
2311 if (e->flags & EDGE_EH)
2312 n_eh++;
2314 if (e->flags & EDGE_ABNORMAL)
2315 n_abnormal++;
2318 if (!has_crossing_edge
2319 && find_reg_note (BB_END (bb), REG_CROSSING_JUMP, NULL_RTX))
2321 print_rtl_with_bb (stderr, get_insns (), TDF_RTL | TDF_BLOCKS | TDF_DETAILS);
2322 error ("Region crossing jump across same section in bb %i",
2323 bb->index);
2324 err = 1;
2327 if (n_eh && !find_reg_note (BB_END (bb), REG_EH_REGION, NULL_RTX))
2329 error ("missing REG_EH_REGION note at the end of bb %i", bb->index);
2330 err = 1;
2332 if (n_eh > 1)
2334 error ("too many exception handling edges in bb %i", bb->index);
2335 err = 1;
2337 if (n_branch
2338 && (!JUMP_P (BB_END (bb))
2339 || (n_branch > 1 && (any_uncondjump_p (BB_END (bb))
2340 || any_condjump_p (BB_END (bb))))))
2342 error ("too many outgoing branch edges from bb %i", bb->index);
2343 err = 1;
2345 if (n_fallthru && any_uncondjump_p (BB_END (bb)))
2347 error ("fallthru edge after unconditional jump in bb %i", bb->index);
2348 err = 1;
2350 if (n_branch != 1 && any_uncondjump_p (BB_END (bb)))
2352 error ("wrong number of branch edges after unconditional jump"
2353 " in bb %i", bb->index);
2354 err = 1;
2356 if (n_branch != 1 && any_condjump_p (BB_END (bb))
2357 && JUMP_LABEL (BB_END (bb)) != BB_HEAD (fallthru->dest))
2359 error ("wrong amount of branch edges after conditional jump"
2360 " in bb %i", bb->index);
2361 err = 1;
2363 if (n_abnormal_call && !CALL_P (BB_END (bb)))
2365 error ("abnormal call edges for non-call insn in bb %i", bb->index);
2366 err = 1;
2368 if (n_sibcall && !CALL_P (BB_END (bb)))
2370 error ("sibcall edges for non-call insn in bb %i", bb->index);
2371 err = 1;
2373 if (n_abnormal > n_eh
2374 && !(CALL_P (BB_END (bb))
2375 && n_abnormal == n_abnormal_call + n_sibcall)
2376 && (!JUMP_P (BB_END (bb))
2377 || any_condjump_p (BB_END (bb))
2378 || any_uncondjump_p (BB_END (bb))))
2380 error ("abnormal edges for no purpose in bb %i", bb->index);
2381 err = 1;
2385 /* Clean up. */
2386 return err;
2389 /* Checks on the instructions within blocks. Currently checks that each
2390 block starts with a basic block note, and that basic block notes and
2391 control flow jumps are not found in the middle of the block. */
2393 static int
2394 rtl_verify_bb_insns (void)
2396 rtx x;
2397 int err = 0;
2398 basic_block bb;
2400 FOR_EACH_BB_REVERSE (bb)
2402 /* Now check the header of basic
2403 block. It ought to contain optional CODE_LABEL followed
2404 by NOTE_BASIC_BLOCK. */
2405 x = BB_HEAD (bb);
2406 if (LABEL_P (x))
2408 if (BB_END (bb) == x)
2410 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
2411 bb->index);
2412 err = 1;
2415 x = NEXT_INSN (x);
2418 if (!NOTE_INSN_BASIC_BLOCK_P (x) || NOTE_BASIC_BLOCK (x) != bb)
2420 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
2421 bb->index);
2422 err = 1;
2425 if (BB_END (bb) == x)
2426 /* Do checks for empty blocks here. */
2428 else
2429 for (x = NEXT_INSN (x); x; x = NEXT_INSN (x))
2431 if (NOTE_INSN_BASIC_BLOCK_P (x))
2433 error ("NOTE_INSN_BASIC_BLOCK %d in middle of basic block %d",
2434 INSN_UID (x), bb->index);
2435 err = 1;
2438 if (x == BB_END (bb))
2439 break;
2441 if (control_flow_insn_p (x))
2443 error ("in basic block %d:", bb->index);
2444 fatal_insn ("flow control insn inside a basic block", x);
2449 /* Clean up. */
2450 return err;
2453 /* Verify that block pointers for instructions in basic blocks, headers and
2454 footers are set appropriately. */
2456 static int
2457 rtl_verify_bb_pointers (void)
2459 int err = 0;
2460 basic_block bb;
2462 /* Check the general integrity of the basic blocks. */
2463 FOR_EACH_BB_REVERSE (bb)
2465 rtx insn;
2467 if (!(bb->flags & BB_RTL))
2469 error ("BB_RTL flag not set for block %d", bb->index);
2470 err = 1;
2473 FOR_BB_INSNS (bb, insn)
2474 if (BLOCK_FOR_INSN (insn) != bb)
2476 error ("insn %d basic block pointer is %d, should be %d",
2477 INSN_UID (insn),
2478 BLOCK_FOR_INSN (insn) ? BLOCK_FOR_INSN (insn)->index : 0,
2479 bb->index);
2480 err = 1;
2483 for (insn = BB_HEADER (bb); insn; insn = NEXT_INSN (insn))
2484 if (!BARRIER_P (insn)
2485 && BLOCK_FOR_INSN (insn) != NULL)
2487 error ("insn %d in header of bb %d has non-NULL basic block",
2488 INSN_UID (insn), bb->index);
2489 err = 1;
2491 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2492 if (!BARRIER_P (insn)
2493 && BLOCK_FOR_INSN (insn) != NULL)
2495 error ("insn %d in footer of bb %d has non-NULL basic block",
2496 INSN_UID (insn), bb->index);
2497 err = 1;
2501 /* Clean up. */
2502 return err;
2505 /* Verify the CFG and RTL consistency common for both underlying RTL and
2506 cfglayout RTL.
2508 Currently it does following checks:
2510 - overlapping of basic blocks
2511 - insns with wrong BLOCK_FOR_INSN pointers
2512 - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note)
2513 - tails of basic blocks (ensure that boundary is necessary)
2514 - scans body of the basic block for JUMP_INSN, CODE_LABEL
2515 and NOTE_INSN_BASIC_BLOCK
2516 - verify that no fall_thru edge crosses hot/cold partition boundaries
2517 - verify that there are no pending RTL branch predictions
2518 - verify that there is a single hot/cold partition boundary after bbro
2520 In future it can be extended check a lot of other stuff as well
2521 (reachability of basic blocks, life information, etc. etc.). */
2523 static int
2524 rtl_verify_flow_info_1 (void)
2526 int err = 0;
2528 err |= rtl_verify_bb_pointers ();
2530 err |= rtl_verify_bb_insns ();
2532 err |= rtl_verify_edges ();
2534 return err;
2537 /* Walk the instruction chain and verify that bb head/end pointers
2538 are correct, and that instructions are in exactly one bb and have
2539 correct block pointers. */
2541 static int
2542 rtl_verify_bb_insn_chain (void)
2544 basic_block bb;
2545 int err = 0;
2546 rtx x;
2547 rtx last_head = get_last_insn ();
2548 basic_block *bb_info;
2549 const int max_uid = get_max_uid ();
2551 bb_info = XCNEWVEC (basic_block, max_uid);
2553 FOR_EACH_BB_REVERSE (bb)
2555 rtx head = BB_HEAD (bb);
2556 rtx end = BB_END (bb);
2558 for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
2560 /* Verify the end of the basic block is in the INSN chain. */
2561 if (x == end)
2562 break;
2564 /* And that the code outside of basic blocks has NULL bb field. */
2565 if (!BARRIER_P (x)
2566 && BLOCK_FOR_INSN (x) != NULL)
2568 error ("insn %d outside of basic blocks has non-NULL bb field",
2569 INSN_UID (x));
2570 err = 1;
2574 if (!x)
2576 error ("end insn %d for block %d not found in the insn stream",
2577 INSN_UID (end), bb->index);
2578 err = 1;
2581 /* Work backwards from the end to the head of the basic block
2582 to verify the head is in the RTL chain. */
2583 for (; x != NULL_RTX; x = PREV_INSN (x))
2585 /* While walking over the insn chain, verify insns appear
2586 in only one basic block. */
2587 if (bb_info[INSN_UID (x)] != NULL)
2589 error ("insn %d is in multiple basic blocks (%d and %d)",
2590 INSN_UID (x), bb->index, bb_info[INSN_UID (x)]->index);
2591 err = 1;
2594 bb_info[INSN_UID (x)] = bb;
2596 if (x == head)
2597 break;
2599 if (!x)
2601 error ("head insn %d for block %d not found in the insn stream",
2602 INSN_UID (head), bb->index);
2603 err = 1;
2606 last_head = PREV_INSN (x);
2609 for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
2611 /* Check that the code before the first basic block has NULL
2612 bb field. */
2613 if (!BARRIER_P (x)
2614 && BLOCK_FOR_INSN (x) != NULL)
2616 error ("insn %d outside of basic blocks has non-NULL bb field",
2617 INSN_UID (x));
2618 err = 1;
2621 free (bb_info);
2623 return err;
2626 /* Verify that fallthru edges point to adjacent blocks in layout order and
2627 that barriers exist after non-fallthru blocks. */
2629 static int
2630 rtl_verify_fallthru (void)
2632 basic_block bb;
2633 int err = 0;
2635 FOR_EACH_BB_REVERSE (bb)
2637 edge e;
2639 e = find_fallthru_edge (bb->succs);
2640 if (!e)
2642 rtx insn;
2644 /* Ensure existence of barrier in BB with no fallthru edges. */
2645 for (insn = NEXT_INSN (BB_END (bb)); ; insn = NEXT_INSN (insn))
2647 if (!insn || NOTE_INSN_BASIC_BLOCK_P (insn))
2649 error ("missing barrier after block %i", bb->index);
2650 err = 1;
2651 break;
2653 if (BARRIER_P (insn))
2654 break;
2657 else if (e->src != ENTRY_BLOCK_PTR
2658 && e->dest != EXIT_BLOCK_PTR)
2660 rtx insn;
2662 if (e->src->next_bb != e->dest)
2664 error
2665 ("verify_flow_info: Incorrect blocks for fallthru %i->%i",
2666 e->src->index, e->dest->index);
2667 err = 1;
2669 else
2670 for (insn = NEXT_INSN (BB_END (e->src)); insn != BB_HEAD (e->dest);
2671 insn = NEXT_INSN (insn))
2672 if (BARRIER_P (insn) || INSN_P (insn))
2674 error ("verify_flow_info: Incorrect fallthru %i->%i",
2675 e->src->index, e->dest->index);
2676 fatal_insn ("wrong insn in the fallthru edge", insn);
2677 err = 1;
2682 return err;
2685 /* Verify that blocks are laid out in consecutive order. While walking the
2686 instructions, verify that all expected instructions are inside the basic
2687 blocks, and that all returns are followed by barriers. */
2689 static int
2690 rtl_verify_bb_layout (void)
2692 basic_block bb;
2693 int err = 0;
2694 rtx x;
2695 int num_bb_notes;
2696 const rtx rtx_first = get_insns ();
2697 basic_block last_bb_seen = ENTRY_BLOCK_PTR, curr_bb = NULL;
2699 num_bb_notes = 0;
2700 last_bb_seen = ENTRY_BLOCK_PTR;
2702 for (x = rtx_first; x; x = NEXT_INSN (x))
2704 if (NOTE_INSN_BASIC_BLOCK_P (x))
2706 bb = NOTE_BASIC_BLOCK (x);
2708 num_bb_notes++;
2709 if (bb != last_bb_seen->next_bb)
2710 internal_error ("basic blocks not laid down consecutively");
2712 curr_bb = last_bb_seen = bb;
2715 if (!curr_bb)
2717 switch (GET_CODE (x))
2719 case BARRIER:
2720 case NOTE:
2721 break;
2723 case CODE_LABEL:
2724 /* An ADDR_VEC is placed outside any basic block. */
2725 if (NEXT_INSN (x)
2726 && JUMP_TABLE_DATA_P (NEXT_INSN (x)))
2727 x = NEXT_INSN (x);
2729 /* But in any case, non-deletable labels can appear anywhere. */
2730 break;
2732 default:
2733 fatal_insn ("insn outside basic block", x);
2737 if (JUMP_P (x)
2738 && returnjump_p (x) && ! condjump_p (x)
2739 && ! (next_nonnote_insn (x) && BARRIER_P (next_nonnote_insn (x))))
2740 fatal_insn ("return not followed by barrier", x);
2742 if (curr_bb && x == BB_END (curr_bb))
2743 curr_bb = NULL;
2746 if (num_bb_notes != n_basic_blocks - NUM_FIXED_BLOCKS)
2747 internal_error
2748 ("number of bb notes in insn chain (%d) != n_basic_blocks (%d)",
2749 num_bb_notes, n_basic_blocks);
2751 return err;
2754 /* Verify the CFG and RTL consistency common for both underlying RTL and
2755 cfglayout RTL, plus consistency checks specific to linearized RTL mode.
2757 Currently it does following checks:
2758 - all checks of rtl_verify_flow_info_1
2759 - test head/end pointers
2760 - check that blocks are laid out in consecutive order
2761 - check that all insns are in the basic blocks
2762 (except the switch handling code, barriers and notes)
2763 - check that all returns are followed by barriers
2764 - check that all fallthru edge points to the adjacent blocks. */
2766 static int
2767 rtl_verify_flow_info (void)
2769 int err = 0;
2771 err |= rtl_verify_flow_info_1 ();
2773 err |= rtl_verify_bb_insn_chain ();
2775 err |= rtl_verify_fallthru ();
2777 err |= rtl_verify_bb_layout ();
2779 err |= verify_hot_cold_block_grouping ();
2781 return err;
2784 /* Assume that the preceding pass has possibly eliminated jump instructions
2785 or converted the unconditional jumps. Eliminate the edges from CFG.
2786 Return true if any edges are eliminated. */
2788 bool
2789 purge_dead_edges (basic_block bb)
2791 edge e;
2792 rtx insn = BB_END (bb), note;
2793 bool purged = false;
2794 bool found;
2795 edge_iterator ei;
2797 if (DEBUG_INSN_P (insn) && insn != BB_HEAD (bb))
2799 insn = PREV_INSN (insn);
2800 while ((DEBUG_INSN_P (insn) || NOTE_P (insn)) && insn != BB_HEAD (bb));
2802 /* If this instruction cannot trap, remove REG_EH_REGION notes. */
2803 if (NONJUMP_INSN_P (insn)
2804 && (note = find_reg_note (insn, REG_EH_REGION, NULL)))
2806 rtx eqnote;
2808 if (! may_trap_p (PATTERN (insn))
2809 || ((eqnote = find_reg_equal_equiv_note (insn))
2810 && ! may_trap_p (XEXP (eqnote, 0))))
2811 remove_note (insn, note);
2814 /* Cleanup abnormal edges caused by exceptions or non-local gotos. */
2815 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2817 bool remove = false;
2819 /* There are three types of edges we need to handle correctly here: EH
2820 edges, abnormal call EH edges, and abnormal call non-EH edges. The
2821 latter can appear when nonlocal gotos are used. */
2822 if (e->flags & EDGE_ABNORMAL_CALL)
2824 if (!CALL_P (insn))
2825 remove = true;
2826 else if (can_nonlocal_goto (insn))
2828 else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
2830 else if (flag_tm && find_reg_note (insn, REG_TM, NULL))
2832 else
2833 remove = true;
2835 else if (e->flags & EDGE_EH)
2836 remove = !can_throw_internal (insn);
2838 if (remove)
2840 remove_edge (e);
2841 df_set_bb_dirty (bb);
2842 purged = true;
2844 else
2845 ei_next (&ei);
2848 if (JUMP_P (insn))
2850 rtx note;
2851 edge b,f;
2852 edge_iterator ei;
2854 /* We do care only about conditional jumps and simplejumps. */
2855 if (!any_condjump_p (insn)
2856 && !returnjump_p (insn)
2857 && !simplejump_p (insn))
2858 return purged;
2860 /* Branch probability/prediction notes are defined only for
2861 condjumps. We've possibly turned condjump into simplejump. */
2862 if (simplejump_p (insn))
2864 note = find_reg_note (insn, REG_BR_PROB, NULL);
2865 if (note)
2866 remove_note (insn, note);
2867 while ((note = find_reg_note (insn, REG_BR_PRED, NULL)))
2868 remove_note (insn, note);
2871 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2873 /* Avoid abnormal flags to leak from computed jumps turned
2874 into simplejumps. */
2876 e->flags &= ~EDGE_ABNORMAL;
2878 /* See if this edge is one we should keep. */
2879 if ((e->flags & EDGE_FALLTHRU) && any_condjump_p (insn))
2880 /* A conditional jump can fall through into the next
2881 block, so we should keep the edge. */
2883 ei_next (&ei);
2884 continue;
2886 else if (e->dest != EXIT_BLOCK_PTR
2887 && BB_HEAD (e->dest) == JUMP_LABEL (insn))
2888 /* If the destination block is the target of the jump,
2889 keep the edge. */
2891 ei_next (&ei);
2892 continue;
2894 else if (e->dest == EXIT_BLOCK_PTR && returnjump_p (insn))
2895 /* If the destination block is the exit block, and this
2896 instruction is a return, then keep the edge. */
2898 ei_next (&ei);
2899 continue;
2901 else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
2902 /* Keep the edges that correspond to exceptions thrown by
2903 this instruction and rematerialize the EDGE_ABNORMAL
2904 flag we just cleared above. */
2906 e->flags |= EDGE_ABNORMAL;
2907 ei_next (&ei);
2908 continue;
2911 /* We do not need this edge. */
2912 df_set_bb_dirty (bb);
2913 purged = true;
2914 remove_edge (e);
2917 if (EDGE_COUNT (bb->succs) == 0 || !purged)
2918 return purged;
2920 if (dump_file)
2921 fprintf (dump_file, "Purged edges from bb %i\n", bb->index);
2923 if (!optimize)
2924 return purged;
2926 /* Redistribute probabilities. */
2927 if (single_succ_p (bb))
2929 single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
2930 single_succ_edge (bb)->count = bb->count;
2932 else
2934 note = find_reg_note (insn, REG_BR_PROB, NULL);
2935 if (!note)
2936 return purged;
2938 b = BRANCH_EDGE (bb);
2939 f = FALLTHRU_EDGE (bb);
2940 b->probability = INTVAL (XEXP (note, 0));
2941 f->probability = REG_BR_PROB_BASE - b->probability;
2942 /* Update these to use GCOV_COMPUTE_SCALE. */
2943 b->count = bb->count * b->probability / REG_BR_PROB_BASE;
2944 f->count = bb->count * f->probability / REG_BR_PROB_BASE;
2947 return purged;
2949 else if (CALL_P (insn) && SIBLING_CALL_P (insn))
2951 /* First, there should not be any EH or ABCALL edges resulting
2952 from non-local gotos and the like. If there were, we shouldn't
2953 have created the sibcall in the first place. Second, there
2954 should of course never have been a fallthru edge. */
2955 gcc_assert (single_succ_p (bb));
2956 gcc_assert (single_succ_edge (bb)->flags
2957 == (EDGE_SIBCALL | EDGE_ABNORMAL));
2959 return 0;
2962 /* If we don't see a jump insn, we don't know exactly why the block would
2963 have been broken at this point. Look for a simple, non-fallthru edge,
2964 as these are only created by conditional branches. If we find such an
2965 edge we know that there used to be a jump here and can then safely
2966 remove all non-fallthru edges. */
2967 found = false;
2968 FOR_EACH_EDGE (e, ei, bb->succs)
2969 if (! (e->flags & (EDGE_COMPLEX | EDGE_FALLTHRU)))
2971 found = true;
2972 break;
2975 if (!found)
2976 return purged;
2978 /* Remove all but the fake and fallthru edges. The fake edge may be
2979 the only successor for this block in the case of noreturn
2980 calls. */
2981 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2983 if (!(e->flags & (EDGE_FALLTHRU | EDGE_FAKE)))
2985 df_set_bb_dirty (bb);
2986 remove_edge (e);
2987 purged = true;
2989 else
2990 ei_next (&ei);
2993 gcc_assert (single_succ_p (bb));
2995 single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
2996 single_succ_edge (bb)->count = bb->count;
2998 if (dump_file)
2999 fprintf (dump_file, "Purged non-fallthru edges from bb %i\n",
3000 bb->index);
3001 return purged;
3004 /* Search all basic blocks for potentially dead edges and purge them. Return
3005 true if some edge has been eliminated. */
3007 bool
3008 purge_all_dead_edges (void)
3010 int purged = false;
3011 basic_block bb;
3013 FOR_EACH_BB (bb)
3015 bool purged_here = purge_dead_edges (bb);
3017 purged |= purged_here;
3020 return purged;
3023 /* This is used by a few passes that emit some instructions after abnormal
3024 calls, moving the basic block's end, while they in fact do want to emit
3025 them on the fallthru edge. Look for abnormal call edges, find backward
3026 the call in the block and insert the instructions on the edge instead.
3028 Similarly, handle instructions throwing exceptions internally.
3030 Return true when instructions have been found and inserted on edges. */
3032 bool
3033 fixup_abnormal_edges (void)
3035 bool inserted = false;
3036 basic_block bb;
3038 FOR_EACH_BB (bb)
3040 edge e;
3041 edge_iterator ei;
3043 /* Look for cases we are interested in - calls or instructions causing
3044 exceptions. */
3045 FOR_EACH_EDGE (e, ei, bb->succs)
3046 if ((e->flags & EDGE_ABNORMAL_CALL)
3047 || ((e->flags & (EDGE_ABNORMAL | EDGE_EH))
3048 == (EDGE_ABNORMAL | EDGE_EH)))
3049 break;
3051 if (e && !CALL_P (BB_END (bb)) && !can_throw_internal (BB_END (bb)))
3053 rtx insn;
3055 /* Get past the new insns generated. Allow notes, as the insns
3056 may be already deleted. */
3057 insn = BB_END (bb);
3058 while ((NONJUMP_INSN_P (insn) || NOTE_P (insn))
3059 && !can_throw_internal (insn)
3060 && insn != BB_HEAD (bb))
3061 insn = PREV_INSN (insn);
3063 if (CALL_P (insn) || can_throw_internal (insn))
3065 rtx stop, next;
3067 e = find_fallthru_edge (bb->succs);
3069 stop = NEXT_INSN (BB_END (bb));
3070 BB_END (bb) = insn;
3072 for (insn = NEXT_INSN (insn); insn != stop; insn = next)
3074 next = NEXT_INSN (insn);
3075 if (INSN_P (insn))
3077 delete_insn (insn);
3079 /* Sometimes there's still the return value USE.
3080 If it's placed after a trapping call (i.e. that
3081 call is the last insn anyway), we have no fallthru
3082 edge. Simply delete this use and don't try to insert
3083 on the non-existent edge. */
3084 if (GET_CODE (PATTERN (insn)) != USE)
3086 /* We're not deleting it, we're moving it. */
3087 INSN_DELETED_P (insn) = 0;
3088 PREV_INSN (insn) = NULL_RTX;
3089 NEXT_INSN (insn) = NULL_RTX;
3091 insert_insn_on_edge (insn, e);
3092 inserted = true;
3095 else if (!BARRIER_P (insn))
3096 set_block_for_insn (insn, NULL);
3100 /* It may be that we don't find any trapping insn. In this
3101 case we discovered quite late that the insn that had been
3102 marked as can_throw_internal in fact couldn't trap at all.
3103 So we should in fact delete the EH edges out of the block. */
3104 else
3105 purge_dead_edges (bb);
3109 return inserted;
3112 /* Cut the insns from FIRST to LAST out of the insns stream. */
3115 unlink_insn_chain (rtx first, rtx last)
3117 rtx prevfirst = PREV_INSN (first);
3118 rtx nextlast = NEXT_INSN (last);
3120 PREV_INSN (first) = NULL;
3121 NEXT_INSN (last) = NULL;
3122 if (prevfirst)
3123 NEXT_INSN (prevfirst) = nextlast;
3124 if (nextlast)
3125 PREV_INSN (nextlast) = prevfirst;
3126 else
3127 set_last_insn (prevfirst);
3128 if (!prevfirst)
3129 set_first_insn (nextlast);
3130 return first;
3133 /* Skip over inter-block insns occurring after BB which are typically
3134 associated with BB (e.g., barriers). If there are any such insns,
3135 we return the last one. Otherwise, we return the end of BB. */
3137 static rtx
3138 skip_insns_after_block (basic_block bb)
3140 rtx insn, last_insn, next_head, prev;
3142 next_head = NULL_RTX;
3143 if (bb->next_bb != EXIT_BLOCK_PTR)
3144 next_head = BB_HEAD (bb->next_bb);
3146 for (last_insn = insn = BB_END (bb); (insn = NEXT_INSN (insn)) != 0; )
3148 if (insn == next_head)
3149 break;
3151 switch (GET_CODE (insn))
3153 case BARRIER:
3154 last_insn = insn;
3155 continue;
3157 case NOTE:
3158 switch (NOTE_KIND (insn))
3160 case NOTE_INSN_BLOCK_END:
3161 gcc_unreachable ();
3162 continue;
3163 default:
3164 continue;
3165 break;
3167 break;
3169 case CODE_LABEL:
3170 if (NEXT_INSN (insn)
3171 && JUMP_TABLE_DATA_P (NEXT_INSN (insn)))
3173 insn = NEXT_INSN (insn);
3174 last_insn = insn;
3175 continue;
3177 break;
3179 default:
3180 break;
3183 break;
3186 /* It is possible to hit contradictory sequence. For instance:
3188 jump_insn
3189 NOTE_INSN_BLOCK_BEG
3190 barrier
3192 Where barrier belongs to jump_insn, but the note does not. This can be
3193 created by removing the basic block originally following
3194 NOTE_INSN_BLOCK_BEG. In such case reorder the notes. */
3196 for (insn = last_insn; insn != BB_END (bb); insn = prev)
3198 prev = PREV_INSN (insn);
3199 if (NOTE_P (insn))
3200 switch (NOTE_KIND (insn))
3202 case NOTE_INSN_BLOCK_END:
3203 gcc_unreachable ();
3204 break;
3205 case NOTE_INSN_DELETED:
3206 case NOTE_INSN_DELETED_LABEL:
3207 case NOTE_INSN_DELETED_DEBUG_LABEL:
3208 continue;
3209 default:
3210 reorder_insns (insn, insn, last_insn);
3214 return last_insn;
3217 /* Locate or create a label for a given basic block. */
3219 static rtx
3220 label_for_bb (basic_block bb)
3222 rtx label = BB_HEAD (bb);
3224 if (!LABEL_P (label))
3226 if (dump_file)
3227 fprintf (dump_file, "Emitting label for block %d\n", bb->index);
3229 label = block_label (bb);
3232 return label;
3235 /* Locate the effective beginning and end of the insn chain for each
3236 block, as defined by skip_insns_after_block above. */
3238 static void
3239 record_effective_endpoints (void)
3241 rtx next_insn;
3242 basic_block bb;
3243 rtx insn;
3245 for (insn = get_insns ();
3246 insn
3247 && NOTE_P (insn)
3248 && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK;
3249 insn = NEXT_INSN (insn))
3250 continue;
3251 /* No basic blocks at all? */
3252 gcc_assert (insn);
3254 if (PREV_INSN (insn))
3255 cfg_layout_function_header =
3256 unlink_insn_chain (get_insns (), PREV_INSN (insn));
3257 else
3258 cfg_layout_function_header = NULL_RTX;
3260 next_insn = get_insns ();
3261 FOR_EACH_BB (bb)
3263 rtx end;
3265 if (PREV_INSN (BB_HEAD (bb)) && next_insn != BB_HEAD (bb))
3266 BB_HEADER (bb) = unlink_insn_chain (next_insn,
3267 PREV_INSN (BB_HEAD (bb)));
3268 end = skip_insns_after_block (bb);
3269 if (NEXT_INSN (BB_END (bb)) && BB_END (bb) != end)
3270 BB_FOOTER (bb) = unlink_insn_chain (NEXT_INSN (BB_END (bb)), end);
3271 next_insn = NEXT_INSN (BB_END (bb));
3274 cfg_layout_function_footer = next_insn;
3275 if (cfg_layout_function_footer)
3276 cfg_layout_function_footer = unlink_insn_chain (cfg_layout_function_footer, get_last_insn ());
3279 static unsigned int
3280 into_cfg_layout_mode (void)
3282 cfg_layout_initialize (0);
3283 return 0;
3286 static unsigned int
3287 outof_cfg_layout_mode (void)
3289 basic_block bb;
3291 FOR_EACH_BB (bb)
3292 if (bb->next_bb != EXIT_BLOCK_PTR)
3293 bb->aux = bb->next_bb;
3295 cfg_layout_finalize ();
3297 return 0;
3300 struct rtl_opt_pass pass_into_cfg_layout_mode =
3303 RTL_PASS,
3304 "into_cfglayout", /* name */
3305 OPTGROUP_NONE, /* optinfo_flags */
3306 NULL, /* gate */
3307 into_cfg_layout_mode, /* execute */
3308 NULL, /* sub */
3309 NULL, /* next */
3310 0, /* static_pass_number */
3311 TV_CFG, /* tv_id */
3312 0, /* properties_required */
3313 PROP_cfglayout, /* properties_provided */
3314 0, /* properties_destroyed */
3315 0, /* todo_flags_start */
3316 0 /* todo_flags_finish */
3320 struct rtl_opt_pass pass_outof_cfg_layout_mode =
3323 RTL_PASS,
3324 "outof_cfglayout", /* name */
3325 OPTGROUP_NONE, /* optinfo_flags */
3326 NULL, /* gate */
3327 outof_cfg_layout_mode, /* execute */
3328 NULL, /* sub */
3329 NULL, /* next */
3330 0, /* static_pass_number */
3331 TV_CFG, /* tv_id */
3332 0, /* properties_required */
3333 0, /* properties_provided */
3334 PROP_cfglayout, /* properties_destroyed */
3335 0, /* todo_flags_start */
3336 0 /* todo_flags_finish */
3341 /* Link the basic blocks in the correct order, compacting the basic
3342 block queue while at it. If STAY_IN_CFGLAYOUT_MODE is false, this
3343 function also clears the basic block header and footer fields.
3345 This function is usually called after a pass (e.g. tracer) finishes
3346 some transformations while in cfglayout mode. The required sequence
3347 of the basic blocks is in a linked list along the bb->aux field.
3348 This functions re-links the basic block prev_bb and next_bb pointers
3349 accordingly, and it compacts and renumbers the blocks.
3351 FIXME: This currently works only for RTL, but the only RTL-specific
3352 bits are the STAY_IN_CFGLAYOUT_MODE bits. The tracer pass was moved
3353 to GIMPLE a long time ago, but it doesn't relink the basic block
3354 chain. It could do that (to give better initial RTL) if this function
3355 is made IR-agnostic (and moved to cfganal.c or cfg.c while at it). */
3357 void
3358 relink_block_chain (bool stay_in_cfglayout_mode)
3360 basic_block bb, prev_bb;
3361 int index;
3363 /* Maybe dump the re-ordered sequence. */
3364 if (dump_file)
3366 fprintf (dump_file, "Reordered sequence:\n");
3367 for (bb = ENTRY_BLOCK_PTR->next_bb, index = NUM_FIXED_BLOCKS;
3369 bb = (basic_block) bb->aux, index++)
3371 fprintf (dump_file, " %i ", index);
3372 if (get_bb_original (bb))
3373 fprintf (dump_file, "duplicate of %i ",
3374 get_bb_original (bb)->index);
3375 else if (forwarder_block_p (bb)
3376 && !LABEL_P (BB_HEAD (bb)))
3377 fprintf (dump_file, "compensation ");
3378 else
3379 fprintf (dump_file, "bb %i ", bb->index);
3380 fprintf (dump_file, " [%i]\n", bb->frequency);
3384 /* Now reorder the blocks. */
3385 prev_bb = ENTRY_BLOCK_PTR;
3386 bb = ENTRY_BLOCK_PTR->next_bb;
3387 for (; bb; prev_bb = bb, bb = (basic_block) bb->aux)
3389 bb->prev_bb = prev_bb;
3390 prev_bb->next_bb = bb;
3392 prev_bb->next_bb = EXIT_BLOCK_PTR;
3393 EXIT_BLOCK_PTR->prev_bb = prev_bb;
3395 /* Then, clean up the aux fields. */
3396 FOR_ALL_BB (bb)
3398 bb->aux = NULL;
3399 if (!stay_in_cfglayout_mode)
3400 BB_HEADER (bb) = BB_FOOTER (bb) = NULL;
3403 /* Maybe reset the original copy tables, they are not valid anymore
3404 when we renumber the basic blocks in compact_blocks. If we are
3405 are going out of cfglayout mode, don't re-allocate the tables. */
3406 free_original_copy_tables ();
3407 if (stay_in_cfglayout_mode)
3408 initialize_original_copy_tables ();
3410 /* Finally, put basic_block_info in the new order. */
3411 compact_blocks ();
3415 /* Given a reorder chain, rearrange the code to match. */
3417 static void
3418 fixup_reorder_chain (void)
3420 basic_block bb;
3421 rtx insn = NULL;
3423 if (cfg_layout_function_header)
3425 set_first_insn (cfg_layout_function_header);
3426 insn = cfg_layout_function_header;
3427 while (NEXT_INSN (insn))
3428 insn = NEXT_INSN (insn);
3431 /* First do the bulk reordering -- rechain the blocks without regard to
3432 the needed changes to jumps and labels. */
3434 for (bb = ENTRY_BLOCK_PTR->next_bb; bb; bb = (basic_block) bb->aux)
3436 if (BB_HEADER (bb))
3438 if (insn)
3439 NEXT_INSN (insn) = BB_HEADER (bb);
3440 else
3441 set_first_insn (BB_HEADER (bb));
3442 PREV_INSN (BB_HEADER (bb)) = insn;
3443 insn = BB_HEADER (bb);
3444 while (NEXT_INSN (insn))
3445 insn = NEXT_INSN (insn);
3447 if (insn)
3448 NEXT_INSN (insn) = BB_HEAD (bb);
3449 else
3450 set_first_insn (BB_HEAD (bb));
3451 PREV_INSN (BB_HEAD (bb)) = insn;
3452 insn = BB_END (bb);
3453 if (BB_FOOTER (bb))
3455 NEXT_INSN (insn) = BB_FOOTER (bb);
3456 PREV_INSN (BB_FOOTER (bb)) = insn;
3457 while (NEXT_INSN (insn))
3458 insn = NEXT_INSN (insn);
3462 NEXT_INSN (insn) = cfg_layout_function_footer;
3463 if (cfg_layout_function_footer)
3464 PREV_INSN (cfg_layout_function_footer) = insn;
3466 while (NEXT_INSN (insn))
3467 insn = NEXT_INSN (insn);
3469 set_last_insn (insn);
3470 #ifdef ENABLE_CHECKING
3471 verify_insn_chain ();
3472 #endif
3474 /* Now add jumps and labels as needed to match the blocks new
3475 outgoing edges. */
3477 for (bb = ENTRY_BLOCK_PTR->next_bb; bb ; bb = (basic_block) bb->aux)
3479 edge e_fall, e_taken, e;
3480 rtx bb_end_insn;
3481 rtx ret_label = NULL_RTX;
3482 basic_block nb;
3483 edge_iterator ei;
3485 if (EDGE_COUNT (bb->succs) == 0)
3486 continue;
3488 /* Find the old fallthru edge, and another non-EH edge for
3489 a taken jump. */
3490 e_taken = e_fall = NULL;
3492 FOR_EACH_EDGE (e, ei, bb->succs)
3493 if (e->flags & EDGE_FALLTHRU)
3494 e_fall = e;
3495 else if (! (e->flags & EDGE_EH))
3496 e_taken = e;
3498 bb_end_insn = BB_END (bb);
3499 if (JUMP_P (bb_end_insn))
3501 ret_label = JUMP_LABEL (bb_end_insn);
3502 if (any_condjump_p (bb_end_insn))
3504 /* This might happen if the conditional jump has side
3505 effects and could therefore not be optimized away.
3506 Make the basic block to end with a barrier in order
3507 to prevent rtl_verify_flow_info from complaining. */
3508 if (!e_fall)
3510 gcc_assert (!onlyjump_p (bb_end_insn)
3511 || returnjump_p (bb_end_insn));
3512 emit_barrier_after (bb_end_insn);
3513 continue;
3516 /* If the old fallthru is still next, nothing to do. */
3517 if (bb->aux == e_fall->dest
3518 || e_fall->dest == EXIT_BLOCK_PTR)
3519 continue;
3521 /* The degenerated case of conditional jump jumping to the next
3522 instruction can happen for jumps with side effects. We need
3523 to construct a forwarder block and this will be done just
3524 fine by force_nonfallthru below. */
3525 if (!e_taken)
3528 /* There is another special case: if *neither* block is next,
3529 such as happens at the very end of a function, then we'll
3530 need to add a new unconditional jump. Choose the taken
3531 edge based on known or assumed probability. */
3532 else if (bb->aux != e_taken->dest)
3534 rtx note = find_reg_note (bb_end_insn, REG_BR_PROB, 0);
3536 if (note
3537 && INTVAL (XEXP (note, 0)) < REG_BR_PROB_BASE / 2
3538 && invert_jump (bb_end_insn,
3539 (e_fall->dest == EXIT_BLOCK_PTR
3540 ? NULL_RTX
3541 : label_for_bb (e_fall->dest)), 0))
3543 e_fall->flags &= ~EDGE_FALLTHRU;
3544 gcc_checking_assert (could_fall_through
3545 (e_taken->src, e_taken->dest));
3546 e_taken->flags |= EDGE_FALLTHRU;
3547 update_br_prob_note (bb);
3548 e = e_fall, e_fall = e_taken, e_taken = e;
3552 /* If the "jumping" edge is a crossing edge, and the fall
3553 through edge is non-crossing, leave things as they are. */
3554 else if ((e_taken->flags & EDGE_CROSSING)
3555 && !(e_fall->flags & EDGE_CROSSING))
3556 continue;
3558 /* Otherwise we can try to invert the jump. This will
3559 basically never fail, however, keep up the pretense. */
3560 else if (invert_jump (bb_end_insn,
3561 (e_fall->dest == EXIT_BLOCK_PTR
3562 ? NULL_RTX
3563 : label_for_bb (e_fall->dest)), 0))
3565 e_fall->flags &= ~EDGE_FALLTHRU;
3566 gcc_checking_assert (could_fall_through
3567 (e_taken->src, e_taken->dest));
3568 e_taken->flags |= EDGE_FALLTHRU;
3569 update_br_prob_note (bb);
3570 if (LABEL_NUSES (ret_label) == 0
3571 && single_pred_p (e_taken->dest))
3572 delete_insn (ret_label);
3573 continue;
3576 else if (extract_asm_operands (PATTERN (bb_end_insn)) != NULL)
3578 /* If the old fallthru is still next or if
3579 asm goto doesn't have a fallthru (e.g. when followed by
3580 __builtin_unreachable ()), nothing to do. */
3581 if (! e_fall
3582 || bb->aux == e_fall->dest
3583 || e_fall->dest == EXIT_BLOCK_PTR)
3584 continue;
3586 /* Otherwise we'll have to use the fallthru fixup below. */
3588 else
3590 /* Otherwise we have some return, switch or computed
3591 jump. In the 99% case, there should not have been a
3592 fallthru edge. */
3593 gcc_assert (returnjump_p (bb_end_insn) || !e_fall);
3594 continue;
3597 else
3599 /* No fallthru implies a noreturn function with EH edges, or
3600 something similarly bizarre. In any case, we don't need to
3601 do anything. */
3602 if (! e_fall)
3603 continue;
3605 /* If the fallthru block is still next, nothing to do. */
3606 if (bb->aux == e_fall->dest)
3607 continue;
3609 /* A fallthru to exit block. */
3610 if (e_fall->dest == EXIT_BLOCK_PTR)
3611 continue;
3614 /* We got here if we need to add a new jump insn.
3615 Note force_nonfallthru can delete E_FALL and thus we have to
3616 save E_FALL->src prior to the call to force_nonfallthru. */
3617 nb = force_nonfallthru_and_redirect (e_fall, e_fall->dest, ret_label);
3618 if (nb)
3620 nb->aux = bb->aux;
3621 bb->aux = nb;
3622 /* Don't process this new block. */
3623 bb = nb;
3627 relink_block_chain (/*stay_in_cfglayout_mode=*/false);
3629 /* Annoying special case - jump around dead jumptables left in the code. */
3630 FOR_EACH_BB (bb)
3632 edge e = find_fallthru_edge (bb->succs);
3634 if (e && !can_fallthru (e->src, e->dest))
3635 force_nonfallthru (e);
3638 /* Ensure goto_locus from edges has some instructions with that locus
3639 in RTL. */
3640 if (!optimize)
3641 FOR_EACH_BB (bb)
3643 edge e;
3644 edge_iterator ei;
3646 FOR_EACH_EDGE (e, ei, bb->succs)
3647 if (LOCATION_LOCUS (e->goto_locus) != UNKNOWN_LOCATION
3648 && !(e->flags & EDGE_ABNORMAL))
3650 edge e2;
3651 edge_iterator ei2;
3652 basic_block dest, nb;
3653 rtx end;
3655 insn = BB_END (e->src);
3656 end = PREV_INSN (BB_HEAD (e->src));
3657 while (insn != end
3658 && (!NONDEBUG_INSN_P (insn) || !INSN_HAS_LOCATION (insn)))
3659 insn = PREV_INSN (insn);
3660 if (insn != end
3661 && INSN_LOCATION (insn) == e->goto_locus)
3662 continue;
3663 if (simplejump_p (BB_END (e->src))
3664 && !INSN_HAS_LOCATION (BB_END (e->src)))
3666 INSN_LOCATION (BB_END (e->src)) = e->goto_locus;
3667 continue;
3669 dest = e->dest;
3670 if (dest == EXIT_BLOCK_PTR)
3672 /* Non-fallthru edges to the exit block cannot be split. */
3673 if (!(e->flags & EDGE_FALLTHRU))
3674 continue;
3676 else
3678 insn = BB_HEAD (dest);
3679 end = NEXT_INSN (BB_END (dest));
3680 while (insn != end && !NONDEBUG_INSN_P (insn))
3681 insn = NEXT_INSN (insn);
3682 if (insn != end && INSN_HAS_LOCATION (insn)
3683 && INSN_LOCATION (insn) == e->goto_locus)
3684 continue;
3686 nb = split_edge (e);
3687 if (!INSN_P (BB_END (nb)))
3688 BB_END (nb) = emit_insn_after_noloc (gen_nop (), BB_END (nb),
3689 nb);
3690 INSN_LOCATION (BB_END (nb)) = e->goto_locus;
3692 /* If there are other incoming edges to the destination block
3693 with the same goto locus, redirect them to the new block as
3694 well, this can prevent other such blocks from being created
3695 in subsequent iterations of the loop. */
3696 for (ei2 = ei_start (dest->preds); (e2 = ei_safe_edge (ei2)); )
3697 if (LOCATION_LOCUS (e2->goto_locus) != UNKNOWN_LOCATION
3698 && !(e2->flags & (EDGE_ABNORMAL | EDGE_FALLTHRU))
3699 && e->goto_locus == e2->goto_locus)
3700 redirect_edge_and_branch (e2, nb);
3701 else
3702 ei_next (&ei2);
3707 /* Perform sanity checks on the insn chain.
3708 1. Check that next/prev pointers are consistent in both the forward and
3709 reverse direction.
3710 2. Count insns in chain, going both directions, and check if equal.
3711 3. Check that get_last_insn () returns the actual end of chain. */
3713 DEBUG_FUNCTION void
3714 verify_insn_chain (void)
3716 rtx x, prevx, nextx;
3717 int insn_cnt1, insn_cnt2;
3719 for (prevx = NULL, insn_cnt1 = 1, x = get_insns ();
3720 x != 0;
3721 prevx = x, insn_cnt1++, x = NEXT_INSN (x))
3722 gcc_assert (PREV_INSN (x) == prevx);
3724 gcc_assert (prevx == get_last_insn ());
3726 for (nextx = NULL, insn_cnt2 = 1, x = get_last_insn ();
3727 x != 0;
3728 nextx = x, insn_cnt2++, x = PREV_INSN (x))
3729 gcc_assert (NEXT_INSN (x) == nextx);
3731 gcc_assert (insn_cnt1 == insn_cnt2);
3734 /* If we have assembler epilogues, the block falling through to exit must
3735 be the last one in the reordered chain when we reach final. Ensure
3736 that this condition is met. */
3737 static void
3738 fixup_fallthru_exit_predecessor (void)
3740 edge e;
3741 basic_block bb = NULL;
3743 /* This transformation is not valid before reload, because we might
3744 separate a call from the instruction that copies the return
3745 value. */
3746 gcc_assert (reload_completed);
3748 e = find_fallthru_edge (EXIT_BLOCK_PTR->preds);
3749 if (e)
3750 bb = e->src;
3752 if (bb && bb->aux)
3754 basic_block c = ENTRY_BLOCK_PTR->next_bb;
3756 /* If the very first block is the one with the fall-through exit
3757 edge, we have to split that block. */
3758 if (c == bb)
3760 bb = split_block (bb, NULL)->dest;
3761 bb->aux = c->aux;
3762 c->aux = bb;
3763 BB_FOOTER (bb) = BB_FOOTER (c);
3764 BB_FOOTER (c) = NULL;
3767 while (c->aux != bb)
3768 c = (basic_block) c->aux;
3770 c->aux = bb->aux;
3771 while (c->aux)
3772 c = (basic_block) c->aux;
3774 c->aux = bb;
3775 bb->aux = NULL;
3779 /* In case there are more than one fallthru predecessors of exit, force that
3780 there is only one. */
3782 static void
3783 force_one_exit_fallthru (void)
3785 edge e, predecessor = NULL;
3786 bool more = false;
3787 edge_iterator ei;
3788 basic_block forwarder, bb;
3790 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
3791 if (e->flags & EDGE_FALLTHRU)
3793 if (predecessor == NULL)
3794 predecessor = e;
3795 else
3797 more = true;
3798 break;
3802 if (!more)
3803 return;
3805 /* Exit has several fallthru predecessors. Create a forwarder block for
3806 them. */
3807 forwarder = split_edge (predecessor);
3808 for (ei = ei_start (EXIT_BLOCK_PTR->preds); (e = ei_safe_edge (ei)); )
3810 if (e->src == forwarder
3811 || !(e->flags & EDGE_FALLTHRU))
3812 ei_next (&ei);
3813 else
3814 redirect_edge_and_branch_force (e, forwarder);
3817 /* Fix up the chain of blocks -- make FORWARDER immediately precede the
3818 exit block. */
3819 FOR_EACH_BB (bb)
3821 if (bb->aux == NULL && bb != forwarder)
3823 bb->aux = forwarder;
3824 break;
3829 /* Return true in case it is possible to duplicate the basic block BB. */
3831 static bool
3832 cfg_layout_can_duplicate_bb_p (const_basic_block bb)
3834 /* Do not attempt to duplicate tablejumps, as we need to unshare
3835 the dispatch table. This is difficult to do, as the instructions
3836 computing jump destination may be hoisted outside the basic block. */
3837 if (tablejump_p (BB_END (bb), NULL, NULL))
3838 return false;
3840 /* Do not duplicate blocks containing insns that can't be copied. */
3841 if (targetm.cannot_copy_insn_p)
3843 rtx insn = BB_HEAD (bb);
3844 while (1)
3846 if (INSN_P (insn) && targetm.cannot_copy_insn_p (insn))
3847 return false;
3848 if (insn == BB_END (bb))
3849 break;
3850 insn = NEXT_INSN (insn);
3854 return true;
3858 duplicate_insn_chain (rtx from, rtx to)
3860 rtx insn, next, last, copy;
3862 /* Avoid updating of boundaries of previous basic block. The
3863 note will get removed from insn stream in fixup. */
3864 last = emit_note (NOTE_INSN_DELETED);
3866 /* Create copy at the end of INSN chain. The chain will
3867 be reordered later. */
3868 for (insn = from; insn != NEXT_INSN (to); insn = NEXT_INSN (insn))
3870 switch (GET_CODE (insn))
3872 case DEBUG_INSN:
3873 /* Don't duplicate label debug insns. */
3874 if (TREE_CODE (INSN_VAR_LOCATION_DECL (insn)) == LABEL_DECL)
3875 break;
3876 /* FALLTHRU */
3877 case INSN:
3878 case CALL_INSN:
3879 case JUMP_INSN:
3880 copy = emit_copy_of_insn_after (insn, get_last_insn ());
3881 if (JUMP_P (insn) && JUMP_LABEL (insn) != NULL_RTX
3882 && ANY_RETURN_P (JUMP_LABEL (insn)))
3883 JUMP_LABEL (copy) = JUMP_LABEL (insn);
3884 maybe_copy_prologue_epilogue_insn (insn, copy);
3885 break;
3887 case JUMP_TABLE_DATA:
3888 /* Avoid copying of dispatch tables. We never duplicate
3889 tablejumps, so this can hit only in case the table got
3890 moved far from original jump.
3891 Avoid copying following barrier as well if any
3892 (and debug insns in between). */
3893 for (next = NEXT_INSN (insn);
3894 next != NEXT_INSN (to);
3895 next = NEXT_INSN (next))
3896 if (!DEBUG_INSN_P (next))
3897 break;
3898 if (next != NEXT_INSN (to) && BARRIER_P (next))
3899 insn = next;
3900 break;
3902 case CODE_LABEL:
3903 break;
3905 case BARRIER:
3906 emit_barrier ();
3907 break;
3909 case NOTE:
3910 switch (NOTE_KIND (insn))
3912 /* In case prologue is empty and function contain label
3913 in first BB, we may want to copy the block. */
3914 case NOTE_INSN_PROLOGUE_END:
3916 case NOTE_INSN_DELETED:
3917 case NOTE_INSN_DELETED_LABEL:
3918 case NOTE_INSN_DELETED_DEBUG_LABEL:
3919 /* No problem to strip these. */
3920 case NOTE_INSN_FUNCTION_BEG:
3921 /* There is always just single entry to function. */
3922 case NOTE_INSN_BASIC_BLOCK:
3923 /* We should only switch text sections once. */
3924 case NOTE_INSN_SWITCH_TEXT_SECTIONS:
3925 break;
3927 case NOTE_INSN_EPILOGUE_BEG:
3928 emit_note_copy (insn);
3929 break;
3931 default:
3932 /* All other notes should have already been eliminated. */
3933 gcc_unreachable ();
3935 break;
3936 default:
3937 gcc_unreachable ();
3940 insn = NEXT_INSN (last);
3941 delete_insn (last);
3942 return insn;
3945 /* Create a duplicate of the basic block BB. */
3947 static basic_block
3948 cfg_layout_duplicate_bb (basic_block bb)
3950 rtx insn;
3951 basic_block new_bb;
3953 insn = duplicate_insn_chain (BB_HEAD (bb), BB_END (bb));
3954 new_bb = create_basic_block (insn,
3955 insn ? get_last_insn () : NULL,
3956 EXIT_BLOCK_PTR->prev_bb);
3958 BB_COPY_PARTITION (new_bb, bb);
3959 if (BB_HEADER (bb))
3961 insn = BB_HEADER (bb);
3962 while (NEXT_INSN (insn))
3963 insn = NEXT_INSN (insn);
3964 insn = duplicate_insn_chain (BB_HEADER (bb), insn);
3965 if (insn)
3966 BB_HEADER (new_bb) = unlink_insn_chain (insn, get_last_insn ());
3969 if (BB_FOOTER (bb))
3971 insn = BB_FOOTER (bb);
3972 while (NEXT_INSN (insn))
3973 insn = NEXT_INSN (insn);
3974 insn = duplicate_insn_chain (BB_FOOTER (bb), insn);
3975 if (insn)
3976 BB_FOOTER (new_bb) = unlink_insn_chain (insn, get_last_insn ());
3979 return new_bb;
3983 /* Main entry point to this module - initialize the datastructures for
3984 CFG layout changes. It keeps LOOPS up-to-date if not null.
3986 FLAGS is a set of additional flags to pass to cleanup_cfg(). */
3988 void
3989 cfg_layout_initialize (unsigned int flags)
3991 rtx x;
3992 basic_block bb;
3994 initialize_original_copy_tables ();
3996 cfg_layout_rtl_register_cfg_hooks ();
3998 record_effective_endpoints ();
4000 /* Make sure that the targets of non local gotos are marked. */
4001 for (x = nonlocal_goto_handler_labels; x; x = XEXP (x, 1))
4003 bb = BLOCK_FOR_INSN (XEXP (x, 0));
4004 bb->flags |= BB_NON_LOCAL_GOTO_TARGET;
4007 cleanup_cfg (CLEANUP_CFGLAYOUT | flags);
4010 /* Splits superblocks. */
4011 void
4012 break_superblocks (void)
4014 sbitmap superblocks;
4015 bool need = false;
4016 basic_block bb;
4018 superblocks = sbitmap_alloc (last_basic_block);
4019 bitmap_clear (superblocks);
4021 FOR_EACH_BB (bb)
4022 if (bb->flags & BB_SUPERBLOCK)
4024 bb->flags &= ~BB_SUPERBLOCK;
4025 bitmap_set_bit (superblocks, bb->index);
4026 need = true;
4029 if (need)
4031 rebuild_jump_labels (get_insns ());
4032 find_many_sub_basic_blocks (superblocks);
4035 free (superblocks);
4038 /* Finalize the changes: reorder insn list according to the sequence specified
4039 by aux pointers, enter compensation code, rebuild scope forest. */
4041 void
4042 cfg_layout_finalize (void)
4044 #ifdef ENABLE_CHECKING
4045 verify_flow_info ();
4046 #endif
4047 force_one_exit_fallthru ();
4048 rtl_register_cfg_hooks ();
4049 if (reload_completed
4050 #ifdef HAVE_epilogue
4051 && !HAVE_epilogue
4052 #endif
4054 fixup_fallthru_exit_predecessor ();
4055 fixup_reorder_chain ();
4057 rebuild_jump_labels (get_insns ());
4058 delete_dead_jumptables ();
4060 #ifdef ENABLE_CHECKING
4061 verify_insn_chain ();
4062 verify_flow_info ();
4063 #endif
4067 /* Same as split_block but update cfg_layout structures. */
4069 static basic_block
4070 cfg_layout_split_block (basic_block bb, void *insnp)
4072 rtx insn = (rtx) insnp;
4073 basic_block new_bb = rtl_split_block (bb, insn);
4075 BB_FOOTER (new_bb) = BB_FOOTER (bb);
4076 BB_FOOTER (bb) = NULL;
4078 return new_bb;
4081 /* Redirect Edge to DEST. */
4082 static edge
4083 cfg_layout_redirect_edge_and_branch (edge e, basic_block dest)
4085 basic_block src = e->src;
4086 edge ret;
4088 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
4089 return NULL;
4091 if (e->dest == dest)
4092 return e;
4094 if (e->src != ENTRY_BLOCK_PTR
4095 && (ret = try_redirect_by_replacing_jump (e, dest, true)))
4097 df_set_bb_dirty (src);
4098 return ret;
4101 if (e->src == ENTRY_BLOCK_PTR
4102 && (e->flags & EDGE_FALLTHRU) && !(e->flags & EDGE_COMPLEX))
4104 if (dump_file)
4105 fprintf (dump_file, "Redirecting entry edge from bb %i to %i\n",
4106 e->src->index, dest->index);
4108 df_set_bb_dirty (e->src);
4109 redirect_edge_succ (e, dest);
4110 return e;
4113 /* Redirect_edge_and_branch may decide to turn branch into fallthru edge
4114 in the case the basic block appears to be in sequence. Avoid this
4115 transformation. */
4117 if (e->flags & EDGE_FALLTHRU)
4119 /* Redirect any branch edges unified with the fallthru one. */
4120 if (JUMP_P (BB_END (src))
4121 && label_is_jump_target_p (BB_HEAD (e->dest),
4122 BB_END (src)))
4124 edge redirected;
4126 if (dump_file)
4127 fprintf (dump_file, "Fallthru edge unified with branch "
4128 "%i->%i redirected to %i\n",
4129 e->src->index, e->dest->index, dest->index);
4130 e->flags &= ~EDGE_FALLTHRU;
4131 redirected = redirect_branch_edge (e, dest);
4132 gcc_assert (redirected);
4133 redirected->flags |= EDGE_FALLTHRU;
4134 df_set_bb_dirty (redirected->src);
4135 return redirected;
4137 /* In case we are redirecting fallthru edge to the branch edge
4138 of conditional jump, remove it. */
4139 if (EDGE_COUNT (src->succs) == 2)
4141 /* Find the edge that is different from E. */
4142 edge s = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e);
4144 if (s->dest == dest
4145 && any_condjump_p (BB_END (src))
4146 && onlyjump_p (BB_END (src)))
4147 delete_insn (BB_END (src));
4149 if (dump_file)
4150 fprintf (dump_file, "Redirecting fallthru edge %i->%i to %i\n",
4151 e->src->index, e->dest->index, dest->index);
4152 ret = redirect_edge_succ_nodup (e, dest);
4154 else
4155 ret = redirect_branch_edge (e, dest);
4157 /* We don't want simplejumps in the insn stream during cfglayout. */
4158 gcc_assert (!simplejump_p (BB_END (src)));
4160 df_set_bb_dirty (src);
4161 return ret;
4164 /* Simple wrapper as we always can redirect fallthru edges. */
4165 static basic_block
4166 cfg_layout_redirect_edge_and_branch_force (edge e, basic_block dest)
4168 edge redirected = cfg_layout_redirect_edge_and_branch (e, dest);
4170 gcc_assert (redirected);
4171 return NULL;
4174 /* Same as delete_basic_block but update cfg_layout structures. */
4176 static void
4177 cfg_layout_delete_block (basic_block bb)
4179 rtx insn, next, prev = PREV_INSN (BB_HEAD (bb)), *to, remaints;
4181 if (BB_HEADER (bb))
4183 next = BB_HEAD (bb);
4184 if (prev)
4185 NEXT_INSN (prev) = BB_HEADER (bb);
4186 else
4187 set_first_insn (BB_HEADER (bb));
4188 PREV_INSN (BB_HEADER (bb)) = prev;
4189 insn = BB_HEADER (bb);
4190 while (NEXT_INSN (insn))
4191 insn = NEXT_INSN (insn);
4192 NEXT_INSN (insn) = next;
4193 PREV_INSN (next) = insn;
4195 next = NEXT_INSN (BB_END (bb));
4196 if (BB_FOOTER (bb))
4198 insn = BB_FOOTER (bb);
4199 while (insn)
4201 if (BARRIER_P (insn))
4203 if (PREV_INSN (insn))
4204 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
4205 else
4206 BB_FOOTER (bb) = NEXT_INSN (insn);
4207 if (NEXT_INSN (insn))
4208 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
4210 if (LABEL_P (insn))
4211 break;
4212 insn = NEXT_INSN (insn);
4214 if (BB_FOOTER (bb))
4216 insn = BB_END (bb);
4217 NEXT_INSN (insn) = BB_FOOTER (bb);
4218 PREV_INSN (BB_FOOTER (bb)) = insn;
4219 while (NEXT_INSN (insn))
4220 insn = NEXT_INSN (insn);
4221 NEXT_INSN (insn) = next;
4222 if (next)
4223 PREV_INSN (next) = insn;
4224 else
4225 set_last_insn (insn);
4228 if (bb->next_bb != EXIT_BLOCK_PTR)
4229 to = &BB_HEADER (bb->next_bb);
4230 else
4231 to = &cfg_layout_function_footer;
4233 rtl_delete_block (bb);
4235 if (prev)
4236 prev = NEXT_INSN (prev);
4237 else
4238 prev = get_insns ();
4239 if (next)
4240 next = PREV_INSN (next);
4241 else
4242 next = get_last_insn ();
4244 if (next && NEXT_INSN (next) != prev)
4246 remaints = unlink_insn_chain (prev, next);
4247 insn = remaints;
4248 while (NEXT_INSN (insn))
4249 insn = NEXT_INSN (insn);
4250 NEXT_INSN (insn) = *to;
4251 if (*to)
4252 PREV_INSN (*to) = insn;
4253 *to = remaints;
4257 /* Return true when blocks A and B can be safely merged. */
4259 static bool
4260 cfg_layout_can_merge_blocks_p (basic_block a, basic_block b)
4262 /* If we are partitioning hot/cold basic blocks, we don't want to
4263 mess up unconditional or indirect jumps that cross between hot
4264 and cold sections.
4266 Basic block partitioning may result in some jumps that appear to
4267 be optimizable (or blocks that appear to be mergeable), but which really
4268 must be left untouched (they are required to make it safely across
4269 partition boundaries). See the comments at the top of
4270 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
4272 if (BB_PARTITION (a) != BB_PARTITION (b))
4273 return false;
4275 /* Protect the loop latches. */
4276 if (current_loops && b->loop_father->latch == b)
4277 return false;
4279 /* If we would end up moving B's instructions, make sure it doesn't fall
4280 through into the exit block, since we cannot recover from a fallthrough
4281 edge into the exit block occurring in the middle of a function. */
4282 if (NEXT_INSN (BB_END (a)) != BB_HEAD (b))
4284 edge e = find_fallthru_edge (b->succs);
4285 if (e && e->dest == EXIT_BLOCK_PTR)
4286 return false;
4289 /* There must be exactly one edge in between the blocks. */
4290 return (single_succ_p (a)
4291 && single_succ (a) == b
4292 && single_pred_p (b) == 1
4293 && a != b
4294 /* Must be simple edge. */
4295 && !(single_succ_edge (a)->flags & EDGE_COMPLEX)
4296 && a != ENTRY_BLOCK_PTR && b != EXIT_BLOCK_PTR
4297 /* If the jump insn has side effects, we can't kill the edge.
4298 When not optimizing, try_redirect_by_replacing_jump will
4299 not allow us to redirect an edge by replacing a table jump. */
4300 && (!JUMP_P (BB_END (a))
4301 || ((!optimize || reload_completed)
4302 ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
4305 /* Merge block A and B. The blocks must be mergeable. */
4307 static void
4308 cfg_layout_merge_blocks (basic_block a, basic_block b)
4310 bool forwarder_p = (b->flags & BB_FORWARDER_BLOCK) != 0;
4311 rtx insn;
4313 gcc_checking_assert (cfg_layout_can_merge_blocks_p (a, b));
4315 if (dump_file)
4316 fprintf (dump_file, "Merging block %d into block %d...\n", b->index,
4317 a->index);
4319 /* If there was a CODE_LABEL beginning B, delete it. */
4320 if (LABEL_P (BB_HEAD (b)))
4322 delete_insn (BB_HEAD (b));
4325 /* We should have fallthru edge in a, or we can do dummy redirection to get
4326 it cleaned up. */
4327 if (JUMP_P (BB_END (a)))
4328 try_redirect_by_replacing_jump (EDGE_SUCC (a, 0), b, true);
4329 gcc_assert (!JUMP_P (BB_END (a)));
4331 /* When not optimizing CFG and the edge is the only place in RTL which holds
4332 some unique locus, emit a nop with that locus in between. */
4333 if (!optimize)
4334 emit_nop_for_unique_locus_between (a, b);
4336 /* Move things from b->footer after a->footer. */
4337 if (BB_FOOTER (b))
4339 if (!BB_FOOTER (a))
4340 BB_FOOTER (a) = BB_FOOTER (b);
4341 else
4343 rtx last = BB_FOOTER (a);
4345 while (NEXT_INSN (last))
4346 last = NEXT_INSN (last);
4347 NEXT_INSN (last) = BB_FOOTER (b);
4348 PREV_INSN (BB_FOOTER (b)) = last;
4350 BB_FOOTER (b) = NULL;
4353 /* Move things from b->header before a->footer.
4354 Note that this may include dead tablejump data, but we don't clean
4355 those up until we go out of cfglayout mode. */
4356 if (BB_HEADER (b))
4358 if (! BB_FOOTER (a))
4359 BB_FOOTER (a) = BB_HEADER (b);
4360 else
4362 rtx last = BB_HEADER (b);
4364 while (NEXT_INSN (last))
4365 last = NEXT_INSN (last);
4366 NEXT_INSN (last) = BB_FOOTER (a);
4367 PREV_INSN (BB_FOOTER (a)) = last;
4368 BB_FOOTER (a) = BB_HEADER (b);
4370 BB_HEADER (b) = NULL;
4373 /* In the case basic blocks are not adjacent, move them around. */
4374 if (NEXT_INSN (BB_END (a)) != BB_HEAD (b))
4376 insn = unlink_insn_chain (BB_HEAD (b), BB_END (b));
4378 emit_insn_after_noloc (insn, BB_END (a), a);
4380 /* Otherwise just re-associate the instructions. */
4381 else
4383 insn = BB_HEAD (b);
4384 BB_END (a) = BB_END (b);
4387 /* emit_insn_after_noloc doesn't call df_insn_change_bb.
4388 We need to explicitly call. */
4389 update_bb_for_insn_chain (insn, BB_END (b), a);
4391 /* Skip possible DELETED_LABEL insn. */
4392 if (!NOTE_INSN_BASIC_BLOCK_P (insn))
4393 insn = NEXT_INSN (insn);
4394 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
4395 BB_HEAD (b) = BB_END (b) = NULL;
4396 delete_insn (insn);
4398 df_bb_delete (b->index);
4400 /* If B was a forwarder block, propagate the locus on the edge. */
4401 if (forwarder_p
4402 && LOCATION_LOCUS (EDGE_SUCC (b, 0)->goto_locus) == UNKNOWN_LOCATION)
4403 EDGE_SUCC (b, 0)->goto_locus = EDGE_SUCC (a, 0)->goto_locus;
4405 if (dump_file)
4406 fprintf (dump_file, "Merged blocks %d and %d.\n", a->index, b->index);
4409 /* Split edge E. */
4411 static basic_block
4412 cfg_layout_split_edge (edge e)
4414 basic_block new_bb =
4415 create_basic_block (e->src != ENTRY_BLOCK_PTR
4416 ? NEXT_INSN (BB_END (e->src)) : get_insns (),
4417 NULL_RTX, e->src);
4419 if (e->dest == EXIT_BLOCK_PTR)
4420 BB_COPY_PARTITION (new_bb, e->src);
4421 else
4422 BB_COPY_PARTITION (new_bb, e->dest);
4423 make_edge (new_bb, e->dest, EDGE_FALLTHRU);
4424 redirect_edge_and_branch_force (e, new_bb);
4426 return new_bb;
4429 /* Do postprocessing after making a forwarder block joined by edge FALLTHRU. */
4431 static void
4432 rtl_make_forwarder_block (edge fallthru ATTRIBUTE_UNUSED)
4436 /* Return true if BB contains only labels or non-executable
4437 instructions. */
4439 static bool
4440 rtl_block_empty_p (basic_block bb)
4442 rtx insn;
4444 if (bb == ENTRY_BLOCK_PTR || bb == EXIT_BLOCK_PTR)
4445 return true;
4447 FOR_BB_INSNS (bb, insn)
4448 if (NONDEBUG_INSN_P (insn) && !any_uncondjump_p (insn))
4449 return false;
4451 return true;
4454 /* Split a basic block if it ends with a conditional branch and if
4455 the other part of the block is not empty. */
4457 static basic_block
4458 rtl_split_block_before_cond_jump (basic_block bb)
4460 rtx insn;
4461 rtx split_point = NULL;
4462 rtx last = NULL;
4463 bool found_code = false;
4465 FOR_BB_INSNS (bb, insn)
4467 if (any_condjump_p (insn))
4468 split_point = last;
4469 else if (NONDEBUG_INSN_P (insn))
4470 found_code = true;
4471 last = insn;
4474 /* Did not find everything. */
4475 if (found_code && split_point)
4476 return split_block (bb, split_point)->dest;
4477 else
4478 return NULL;
4481 /* Return 1 if BB ends with a call, possibly followed by some
4482 instructions that must stay with the call, 0 otherwise. */
4484 static bool
4485 rtl_block_ends_with_call_p (basic_block bb)
4487 rtx insn = BB_END (bb);
4489 while (!CALL_P (insn)
4490 && insn != BB_HEAD (bb)
4491 && (keep_with_call_p (insn)
4492 || NOTE_P (insn)
4493 || DEBUG_INSN_P (insn)))
4494 insn = PREV_INSN (insn);
4495 return (CALL_P (insn));
4498 /* Return 1 if BB ends with a conditional branch, 0 otherwise. */
4500 static bool
4501 rtl_block_ends_with_condjump_p (const_basic_block bb)
4503 return any_condjump_p (BB_END (bb));
4506 /* Return true if we need to add fake edge to exit.
4507 Helper function for rtl_flow_call_edges_add. */
4509 static bool
4510 need_fake_edge_p (const_rtx insn)
4512 if (!INSN_P (insn))
4513 return false;
4515 if ((CALL_P (insn)
4516 && !SIBLING_CALL_P (insn)
4517 && !find_reg_note (insn, REG_NORETURN, NULL)
4518 && !(RTL_CONST_OR_PURE_CALL_P (insn))))
4519 return true;
4521 return ((GET_CODE (PATTERN (insn)) == ASM_OPERANDS
4522 && MEM_VOLATILE_P (PATTERN (insn)))
4523 || (GET_CODE (PATTERN (insn)) == PARALLEL
4524 && asm_noperands (insn) != -1
4525 && MEM_VOLATILE_P (XVECEXP (PATTERN (insn), 0, 0)))
4526 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
4529 /* Add fake edges to the function exit for any non constant and non noreturn
4530 calls, volatile inline assembly in the bitmap of blocks specified by
4531 BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
4532 that were split.
4534 The goal is to expose cases in which entering a basic block does not imply
4535 that all subsequent instructions must be executed. */
4537 static int
4538 rtl_flow_call_edges_add (sbitmap blocks)
4540 int i;
4541 int blocks_split = 0;
4542 int last_bb = last_basic_block;
4543 bool check_last_block = false;
4545 if (n_basic_blocks == NUM_FIXED_BLOCKS)
4546 return 0;
4548 if (! blocks)
4549 check_last_block = true;
4550 else
4551 check_last_block = bitmap_bit_p (blocks, EXIT_BLOCK_PTR->prev_bb->index);
4553 /* In the last basic block, before epilogue generation, there will be
4554 a fallthru edge to EXIT. Special care is required if the last insn
4555 of the last basic block is a call because make_edge folds duplicate
4556 edges, which would result in the fallthru edge also being marked
4557 fake, which would result in the fallthru edge being removed by
4558 remove_fake_edges, which would result in an invalid CFG.
4560 Moreover, we can't elide the outgoing fake edge, since the block
4561 profiler needs to take this into account in order to solve the minimal
4562 spanning tree in the case that the call doesn't return.
4564 Handle this by adding a dummy instruction in a new last basic block. */
4565 if (check_last_block)
4567 basic_block bb = EXIT_BLOCK_PTR->prev_bb;
4568 rtx insn = BB_END (bb);
4570 /* Back up past insns that must be kept in the same block as a call. */
4571 while (insn != BB_HEAD (bb)
4572 && keep_with_call_p (insn))
4573 insn = PREV_INSN (insn);
4575 if (need_fake_edge_p (insn))
4577 edge e;
4579 e = find_edge (bb, EXIT_BLOCK_PTR);
4580 if (e)
4582 insert_insn_on_edge (gen_use (const0_rtx), e);
4583 commit_edge_insertions ();
4588 /* Now add fake edges to the function exit for any non constant
4589 calls since there is no way that we can determine if they will
4590 return or not... */
4592 for (i = NUM_FIXED_BLOCKS; i < last_bb; i++)
4594 basic_block bb = BASIC_BLOCK (i);
4595 rtx insn;
4596 rtx prev_insn;
4598 if (!bb)
4599 continue;
4601 if (blocks && !bitmap_bit_p (blocks, i))
4602 continue;
4604 for (insn = BB_END (bb); ; insn = prev_insn)
4606 prev_insn = PREV_INSN (insn);
4607 if (need_fake_edge_p (insn))
4609 edge e;
4610 rtx split_at_insn = insn;
4612 /* Don't split the block between a call and an insn that should
4613 remain in the same block as the call. */
4614 if (CALL_P (insn))
4615 while (split_at_insn != BB_END (bb)
4616 && keep_with_call_p (NEXT_INSN (split_at_insn)))
4617 split_at_insn = NEXT_INSN (split_at_insn);
4619 /* The handling above of the final block before the epilogue
4620 should be enough to verify that there is no edge to the exit
4621 block in CFG already. Calling make_edge in such case would
4622 cause us to mark that edge as fake and remove it later. */
4624 #ifdef ENABLE_CHECKING
4625 if (split_at_insn == BB_END (bb))
4627 e = find_edge (bb, EXIT_BLOCK_PTR);
4628 gcc_assert (e == NULL);
4630 #endif
4632 /* Note that the following may create a new basic block
4633 and renumber the existing basic blocks. */
4634 if (split_at_insn != BB_END (bb))
4636 e = split_block (bb, split_at_insn);
4637 if (e)
4638 blocks_split++;
4641 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
4644 if (insn == BB_HEAD (bb))
4645 break;
4649 if (blocks_split)
4650 verify_flow_info ();
4652 return blocks_split;
4655 /* Add COMP_RTX as a condition at end of COND_BB. FIRST_HEAD is
4656 the conditional branch target, SECOND_HEAD should be the fall-thru
4657 there is no need to handle this here the loop versioning code handles
4658 this. the reason for SECON_HEAD is that it is needed for condition
4659 in trees, and this should be of the same type since it is a hook. */
4660 static void
4661 rtl_lv_add_condition_to_bb (basic_block first_head ,
4662 basic_block second_head ATTRIBUTE_UNUSED,
4663 basic_block cond_bb, void *comp_rtx)
4665 rtx label, seq, jump;
4666 rtx op0 = XEXP ((rtx)comp_rtx, 0);
4667 rtx op1 = XEXP ((rtx)comp_rtx, 1);
4668 enum rtx_code comp = GET_CODE ((rtx)comp_rtx);
4669 enum machine_mode mode;
4672 label = block_label (first_head);
4673 mode = GET_MODE (op0);
4674 if (mode == VOIDmode)
4675 mode = GET_MODE (op1);
4677 start_sequence ();
4678 op0 = force_operand (op0, NULL_RTX);
4679 op1 = force_operand (op1, NULL_RTX);
4680 do_compare_rtx_and_jump (op0, op1, comp, 0,
4681 mode, NULL_RTX, NULL_RTX, label, -1);
4682 jump = get_last_insn ();
4683 JUMP_LABEL (jump) = label;
4684 LABEL_NUSES (label)++;
4685 seq = get_insns ();
4686 end_sequence ();
4688 /* Add the new cond , in the new head. */
4689 emit_insn_after(seq, BB_END(cond_bb));
4693 /* Given a block B with unconditional branch at its end, get the
4694 store the return the branch edge and the fall-thru edge in
4695 BRANCH_EDGE and FALLTHRU_EDGE respectively. */
4696 static void
4697 rtl_extract_cond_bb_edges (basic_block b, edge *branch_edge,
4698 edge *fallthru_edge)
4700 edge e = EDGE_SUCC (b, 0);
4702 if (e->flags & EDGE_FALLTHRU)
4704 *fallthru_edge = e;
4705 *branch_edge = EDGE_SUCC (b, 1);
4707 else
4709 *branch_edge = e;
4710 *fallthru_edge = EDGE_SUCC (b, 1);
4714 void
4715 init_rtl_bb_info (basic_block bb)
4717 gcc_assert (!bb->il.x.rtl);
4718 bb->il.x.head_ = NULL;
4719 bb->il.x.rtl = ggc_alloc_cleared_rtl_bb_info ();
4722 /* Returns true if it is possible to remove edge E by redirecting
4723 it to the destination of the other edge from E->src. */
4725 static bool
4726 rtl_can_remove_branch_p (const_edge e)
4728 const_basic_block src = e->src;
4729 const_basic_block target = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest;
4730 const_rtx insn = BB_END (src), set;
4732 /* The conditions are taken from try_redirect_by_replacing_jump. */
4733 if (target == EXIT_BLOCK_PTR)
4734 return false;
4736 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
4737 return false;
4739 if (BB_PARTITION (src) != BB_PARTITION (target))
4740 return false;
4742 if (!onlyjump_p (insn)
4743 || tablejump_p (insn, NULL, NULL))
4744 return false;
4746 set = single_set (insn);
4747 if (!set || side_effects_p (set))
4748 return false;
4750 return true;
4753 static basic_block
4754 rtl_duplicate_bb (basic_block bb)
4756 bb = cfg_layout_duplicate_bb (bb);
4757 bb->aux = NULL;
4758 return bb;
4761 /* Do book-keeping of basic block BB for the profile consistency checker.
4762 If AFTER_PASS is 0, do pre-pass accounting, or if AFTER_PASS is 1
4763 then do post-pass accounting. Store the counting in RECORD. */
4764 static void
4765 rtl_account_profile_record (basic_block bb, int after_pass,
4766 struct profile_record *record)
4768 rtx insn;
4769 FOR_BB_INSNS (bb, insn)
4770 if (INSN_P (insn))
4772 record->size[after_pass]
4773 += insn_rtx_cost (PATTERN (insn), false);
4774 if (profile_status == PROFILE_READ)
4775 record->time[after_pass]
4776 += insn_rtx_cost (PATTERN (insn), true) * bb->count;
4777 else if (profile_status == PROFILE_GUESSED)
4778 record->time[after_pass]
4779 += insn_rtx_cost (PATTERN (insn), true) * bb->frequency;
4783 /* Implementation of CFG manipulation for linearized RTL. */
4784 struct cfg_hooks rtl_cfg_hooks = {
4785 "rtl",
4786 rtl_verify_flow_info,
4787 rtl_dump_bb,
4788 rtl_dump_bb_for_graph,
4789 rtl_create_basic_block,
4790 rtl_redirect_edge_and_branch,
4791 rtl_redirect_edge_and_branch_force,
4792 rtl_can_remove_branch_p,
4793 rtl_delete_block,
4794 rtl_split_block,
4795 rtl_move_block_after,
4796 rtl_can_merge_blocks, /* can_merge_blocks_p */
4797 rtl_merge_blocks,
4798 rtl_predict_edge,
4799 rtl_predicted_by_p,
4800 cfg_layout_can_duplicate_bb_p,
4801 rtl_duplicate_bb,
4802 rtl_split_edge,
4803 rtl_make_forwarder_block,
4804 rtl_tidy_fallthru_edge,
4805 rtl_force_nonfallthru,
4806 rtl_block_ends_with_call_p,
4807 rtl_block_ends_with_condjump_p,
4808 rtl_flow_call_edges_add,
4809 NULL, /* execute_on_growing_pred */
4810 NULL, /* execute_on_shrinking_pred */
4811 NULL, /* duplicate loop for trees */
4812 NULL, /* lv_add_condition_to_bb */
4813 NULL, /* lv_adjust_loop_header_phi*/
4814 NULL, /* extract_cond_bb_edges */
4815 NULL, /* flush_pending_stmts */
4816 rtl_block_empty_p, /* block_empty_p */
4817 rtl_split_block_before_cond_jump, /* split_block_before_cond_jump */
4818 rtl_account_profile_record,
4821 /* Implementation of CFG manipulation for cfg layout RTL, where
4822 basic block connected via fallthru edges does not have to be adjacent.
4823 This representation will hopefully become the default one in future
4824 version of the compiler. */
4826 struct cfg_hooks cfg_layout_rtl_cfg_hooks = {
4827 "cfglayout mode",
4828 rtl_verify_flow_info_1,
4829 rtl_dump_bb,
4830 rtl_dump_bb_for_graph,
4831 cfg_layout_create_basic_block,
4832 cfg_layout_redirect_edge_and_branch,
4833 cfg_layout_redirect_edge_and_branch_force,
4834 rtl_can_remove_branch_p,
4835 cfg_layout_delete_block,
4836 cfg_layout_split_block,
4837 rtl_move_block_after,
4838 cfg_layout_can_merge_blocks_p,
4839 cfg_layout_merge_blocks,
4840 rtl_predict_edge,
4841 rtl_predicted_by_p,
4842 cfg_layout_can_duplicate_bb_p,
4843 cfg_layout_duplicate_bb,
4844 cfg_layout_split_edge,
4845 rtl_make_forwarder_block,
4846 NULL, /* tidy_fallthru_edge */
4847 rtl_force_nonfallthru,
4848 rtl_block_ends_with_call_p,
4849 rtl_block_ends_with_condjump_p,
4850 rtl_flow_call_edges_add,
4851 NULL, /* execute_on_growing_pred */
4852 NULL, /* execute_on_shrinking_pred */
4853 duplicate_loop_to_header_edge, /* duplicate loop for trees */
4854 rtl_lv_add_condition_to_bb, /* lv_add_condition_to_bb */
4855 NULL, /* lv_adjust_loop_header_phi*/
4856 rtl_extract_cond_bb_edges, /* extract_cond_bb_edges */
4857 NULL, /* flush_pending_stmts */
4858 rtl_block_empty_p, /* block_empty_p */
4859 rtl_split_block_before_cond_jump, /* split_block_before_cond_jump */
4860 rtl_account_profile_record,
4863 #include "gt-cfgrtl.h"