Remove a trivial assert (missed in previous checkin)
[official-gcc.git] / gcc / cfgrtl.c
blobeb6b312d5c81c9805da490aeeeb253077cd1f838
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 namespace {
464 const pass_data pass_data_free_cfg =
466 RTL_PASS, /* type */
467 "*free_cfg", /* name */
468 OPTGROUP_NONE, /* optinfo_flags */
469 false, /* has_gate */
470 true, /* has_execute */
471 TV_NONE, /* tv_id */
472 0, /* properties_required */
473 0, /* properties_provided */
474 PROP_cfg, /* properties_destroyed */
475 0, /* todo_flags_start */
476 0, /* todo_flags_finish */
479 class pass_free_cfg : public rtl_opt_pass
481 public:
482 pass_free_cfg(gcc::context *ctxt)
483 : rtl_opt_pass(pass_data_free_cfg, ctxt)
486 /* opt_pass methods: */
487 unsigned int execute () { return rest_of_pass_free_cfg (); }
489 }; // class pass_free_cfg
491 } // anon namespace
493 rtl_opt_pass *
494 make_pass_free_cfg (gcc::context *ctxt)
496 return new pass_free_cfg (ctxt);
499 /* Return RTX to emit after when we want to emit code on the entry of function. */
501 entry_of_function (void)
503 return (n_basic_blocks > NUM_FIXED_BLOCKS ?
504 BB_HEAD (ENTRY_BLOCK_PTR->next_bb) : get_insns ());
507 /* Emit INSN at the entry point of the function, ensuring that it is only
508 executed once per function. */
509 void
510 emit_insn_at_entry (rtx insn)
512 edge_iterator ei = ei_start (ENTRY_BLOCK_PTR->succs);
513 edge e = ei_safe_edge (ei);
514 gcc_assert (e->flags & EDGE_FALLTHRU);
516 insert_insn_on_edge (insn, e);
517 commit_edge_insertions ();
520 /* Update BLOCK_FOR_INSN of insns between BEGIN and END
521 (or BARRIER if found) and notify df of the bb change.
522 The insn chain range is inclusive
523 (i.e. both BEGIN and END will be updated. */
525 static void
526 update_bb_for_insn_chain (rtx begin, rtx end, basic_block bb)
528 rtx insn;
530 end = NEXT_INSN (end);
531 for (insn = begin; insn != end; insn = NEXT_INSN (insn))
532 if (!BARRIER_P (insn))
533 df_insn_change_bb (insn, bb);
536 /* Update BLOCK_FOR_INSN of insns in BB to BB,
537 and notify df of the change. */
539 void
540 update_bb_for_insn (basic_block bb)
542 update_bb_for_insn_chain (BB_HEAD (bb), BB_END (bb), bb);
546 /* Like active_insn_p, except keep the return value clobber around
547 even after reload. */
549 static bool
550 flow_active_insn_p (const_rtx insn)
552 if (active_insn_p (insn))
553 return true;
555 /* A clobber of the function return value exists for buggy
556 programs that fail to return a value. Its effect is to
557 keep the return value from being live across the entire
558 function. If we allow it to be skipped, we introduce the
559 possibility for register lifetime confusion. */
560 if (GET_CODE (PATTERN (insn)) == CLOBBER
561 && REG_P (XEXP (PATTERN (insn), 0))
562 && REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))
563 return true;
565 return false;
568 /* Return true if the block has no effect and only forwards control flow to
569 its single destination. */
571 bool
572 contains_no_active_insn_p (const_basic_block bb)
574 rtx insn;
576 if (bb == EXIT_BLOCK_PTR || bb == ENTRY_BLOCK_PTR
577 || !single_succ_p (bb))
578 return false;
580 for (insn = BB_HEAD (bb); insn != BB_END (bb); insn = NEXT_INSN (insn))
581 if (INSN_P (insn) && flow_active_insn_p (insn))
582 return false;
584 return (!INSN_P (insn)
585 || (JUMP_P (insn) && simplejump_p (insn))
586 || !flow_active_insn_p (insn));
589 /* Likewise, but protect loop latches, headers and preheaders. */
590 /* FIXME: Make this a cfg hook. */
592 bool
593 forwarder_block_p (const_basic_block bb)
595 if (!contains_no_active_insn_p (bb))
596 return false;
598 /* Protect loop latches, headers and preheaders. */
599 if (current_loops)
601 basic_block dest;
602 if (bb->loop_father->header == bb)
603 return false;
604 dest = EDGE_SUCC (bb, 0)->dest;
605 if (dest->loop_father->header == dest)
606 return false;
609 return true;
612 /* Return nonzero if we can reach target from src by falling through. */
613 /* FIXME: Make this a cfg hook. */
615 bool
616 can_fallthru (basic_block src, basic_block target)
618 rtx insn = BB_END (src);
619 rtx insn2;
620 edge e;
621 edge_iterator ei;
623 if (target == EXIT_BLOCK_PTR)
624 return true;
625 if (src->next_bb != target)
626 return 0;
627 FOR_EACH_EDGE (e, ei, src->succs)
628 if (e->dest == EXIT_BLOCK_PTR
629 && e->flags & EDGE_FALLTHRU)
630 return 0;
632 insn2 = BB_HEAD (target);
633 if (insn2 && !active_insn_p (insn2))
634 insn2 = next_active_insn (insn2);
636 /* ??? Later we may add code to move jump tables offline. */
637 return next_active_insn (insn) == insn2;
640 /* Return nonzero if we could reach target from src by falling through,
641 if the target was made adjacent. If we already have a fall-through
642 edge to the exit block, we can't do that. */
643 static bool
644 could_fall_through (basic_block src, basic_block target)
646 edge e;
647 edge_iterator ei;
649 if (target == EXIT_BLOCK_PTR)
650 return true;
651 FOR_EACH_EDGE (e, ei, src->succs)
652 if (e->dest == EXIT_BLOCK_PTR
653 && e->flags & EDGE_FALLTHRU)
654 return 0;
655 return true;
658 /* Return the NOTE_INSN_BASIC_BLOCK of BB. */
660 bb_note (basic_block bb)
662 rtx note;
664 note = BB_HEAD (bb);
665 if (LABEL_P (note))
666 note = NEXT_INSN (note);
668 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
669 return note;
672 /* Return the INSN immediately following the NOTE_INSN_BASIC_BLOCK
673 note associated with the BLOCK. */
675 static rtx
676 first_insn_after_basic_block_note (basic_block block)
678 rtx insn;
680 /* Get the first instruction in the block. */
681 insn = BB_HEAD (block);
683 if (insn == NULL_RTX)
684 return NULL_RTX;
685 if (LABEL_P (insn))
686 insn = NEXT_INSN (insn);
687 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
689 return NEXT_INSN (insn);
692 /* Creates a new basic block just after basic block B by splitting
693 everything after specified instruction I. */
695 static basic_block
696 rtl_split_block (basic_block bb, void *insnp)
698 basic_block new_bb;
699 rtx insn = (rtx) insnp;
700 edge e;
701 edge_iterator ei;
703 if (!insn)
705 insn = first_insn_after_basic_block_note (bb);
707 if (insn)
709 rtx next = insn;
711 insn = PREV_INSN (insn);
713 /* If the block contains only debug insns, insn would have
714 been NULL in a non-debug compilation, and then we'd end
715 up emitting a DELETED note. For -fcompare-debug
716 stability, emit the note too. */
717 if (insn != BB_END (bb)
718 && DEBUG_INSN_P (next)
719 && DEBUG_INSN_P (BB_END (bb)))
721 while (next != BB_END (bb) && DEBUG_INSN_P (next))
722 next = NEXT_INSN (next);
724 if (next == BB_END (bb))
725 emit_note_after (NOTE_INSN_DELETED, next);
728 else
729 insn = get_last_insn ();
732 /* We probably should check type of the insn so that we do not create
733 inconsistent cfg. It is checked in verify_flow_info anyway, so do not
734 bother. */
735 if (insn == BB_END (bb))
736 emit_note_after (NOTE_INSN_DELETED, insn);
738 /* Create the new basic block. */
739 new_bb = create_basic_block (NEXT_INSN (insn), BB_END (bb), bb);
740 BB_COPY_PARTITION (new_bb, bb);
741 BB_END (bb) = insn;
743 /* Redirect the outgoing edges. */
744 new_bb->succs = bb->succs;
745 bb->succs = NULL;
746 FOR_EACH_EDGE (e, ei, new_bb->succs)
747 e->src = new_bb;
749 /* The new block starts off being dirty. */
750 df_set_bb_dirty (bb);
751 return new_bb;
754 /* Return true if the single edge between blocks A and B is the only place
755 in RTL which holds some unique locus. */
757 static bool
758 unique_locus_on_edge_between_p (basic_block a, basic_block b)
760 const location_t goto_locus = EDGE_SUCC (a, 0)->goto_locus;
761 rtx insn, end;
763 if (LOCATION_LOCUS (goto_locus) == UNKNOWN_LOCATION)
764 return false;
766 /* First scan block A backward. */
767 insn = BB_END (a);
768 end = PREV_INSN (BB_HEAD (a));
769 while (insn != end && (!NONDEBUG_INSN_P (insn) || !INSN_HAS_LOCATION (insn)))
770 insn = PREV_INSN (insn);
772 if (insn != end && INSN_LOCATION (insn) == goto_locus)
773 return false;
775 /* Then scan block B forward. */
776 insn = BB_HEAD (b);
777 if (insn)
779 end = NEXT_INSN (BB_END (b));
780 while (insn != end && !NONDEBUG_INSN_P (insn))
781 insn = NEXT_INSN (insn);
783 if (insn != end && INSN_HAS_LOCATION (insn)
784 && INSN_LOCATION (insn) == goto_locus)
785 return false;
788 return true;
791 /* If the single edge between blocks A and B is the only place in RTL which
792 holds some unique locus, emit a nop with that locus between the blocks. */
794 static void
795 emit_nop_for_unique_locus_between (basic_block a, basic_block b)
797 if (!unique_locus_on_edge_between_p (a, b))
798 return;
800 BB_END (a) = emit_insn_after_noloc (gen_nop (), BB_END (a), a);
801 INSN_LOCATION (BB_END (a)) = EDGE_SUCC (a, 0)->goto_locus;
804 /* Blocks A and B are to be merged into a single block A. The insns
805 are already contiguous. */
807 static void
808 rtl_merge_blocks (basic_block a, basic_block b)
810 rtx b_head = BB_HEAD (b), b_end = BB_END (b), a_end = BB_END (a);
811 rtx del_first = NULL_RTX, del_last = NULL_RTX;
812 rtx b_debug_start = b_end, b_debug_end = b_end;
813 bool forwarder_p = (b->flags & BB_FORWARDER_BLOCK) != 0;
814 int b_empty = 0;
816 if (dump_file)
817 fprintf (dump_file, "Merging block %d into block %d...\n", b->index,
818 a->index);
820 while (DEBUG_INSN_P (b_end))
821 b_end = PREV_INSN (b_debug_start = b_end);
823 /* If there was a CODE_LABEL beginning B, delete it. */
824 if (LABEL_P (b_head))
826 /* Detect basic blocks with nothing but a label. This can happen
827 in particular at the end of a function. */
828 if (b_head == b_end)
829 b_empty = 1;
831 del_first = del_last = b_head;
832 b_head = NEXT_INSN (b_head);
835 /* Delete the basic block note and handle blocks containing just that
836 note. */
837 if (NOTE_INSN_BASIC_BLOCK_P (b_head))
839 if (b_head == b_end)
840 b_empty = 1;
841 if (! del_last)
842 del_first = b_head;
844 del_last = b_head;
845 b_head = NEXT_INSN (b_head);
848 /* If there was a jump out of A, delete it. */
849 if (JUMP_P (a_end))
851 rtx prev;
853 for (prev = PREV_INSN (a_end); ; prev = PREV_INSN (prev))
854 if (!NOTE_P (prev)
855 || NOTE_INSN_BASIC_BLOCK_P (prev)
856 || prev == BB_HEAD (a))
857 break;
859 del_first = a_end;
861 #ifdef HAVE_cc0
862 /* If this was a conditional jump, we need to also delete
863 the insn that set cc0. */
864 if (only_sets_cc0_p (prev))
866 rtx tmp = prev;
868 prev = prev_nonnote_insn (prev);
869 if (!prev)
870 prev = BB_HEAD (a);
871 del_first = tmp;
873 #endif
875 a_end = PREV_INSN (del_first);
877 else if (BARRIER_P (NEXT_INSN (a_end)))
878 del_first = NEXT_INSN (a_end);
880 /* Delete everything marked above as well as crap that might be
881 hanging out between the two blocks. */
882 BB_END (a) = a_end;
883 BB_HEAD (b) = b_empty ? NULL_RTX : b_head;
884 delete_insn_chain (del_first, del_last, true);
886 /* When not optimizing CFG and the edge is the only place in RTL which holds
887 some unique locus, emit a nop with that locus in between. */
888 if (!optimize)
890 emit_nop_for_unique_locus_between (a, b);
891 a_end = BB_END (a);
894 /* Reassociate the insns of B with A. */
895 if (!b_empty)
897 update_bb_for_insn_chain (a_end, b_debug_end, a);
899 BB_END (a) = b_debug_end;
900 BB_HEAD (b) = NULL_RTX;
902 else if (b_end != b_debug_end)
904 /* Move any deleted labels and other notes between the end of A
905 and the debug insns that make up B after the debug insns,
906 bringing the debug insns into A while keeping the notes after
907 the end of A. */
908 if (NEXT_INSN (a_end) != b_debug_start)
909 reorder_insns_nobb (NEXT_INSN (a_end), PREV_INSN (b_debug_start),
910 b_debug_end);
911 update_bb_for_insn_chain (b_debug_start, b_debug_end, a);
912 BB_END (a) = b_debug_end;
915 df_bb_delete (b->index);
917 /* If B was a forwarder block, propagate the locus on the edge. */
918 if (forwarder_p
919 && LOCATION_LOCUS (EDGE_SUCC (b, 0)->goto_locus) == UNKNOWN_LOCATION)
920 EDGE_SUCC (b, 0)->goto_locus = EDGE_SUCC (a, 0)->goto_locus;
922 if (dump_file)
923 fprintf (dump_file, "Merged blocks %d and %d.\n", a->index, b->index);
927 /* Return true when block A and B can be merged. */
929 static bool
930 rtl_can_merge_blocks (basic_block a, basic_block b)
932 /* If we are partitioning hot/cold basic blocks, we don't want to
933 mess up unconditional or indirect jumps that cross between hot
934 and cold sections.
936 Basic block partitioning may result in some jumps that appear to
937 be optimizable (or blocks that appear to be mergeable), but which really
938 must be left untouched (they are required to make it safely across
939 partition boundaries). See the comments at the top of
940 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
942 if (BB_PARTITION (a) != BB_PARTITION (b))
943 return false;
945 /* Protect the loop latches. */
946 if (current_loops && b->loop_father->latch == b)
947 return false;
949 /* There must be exactly one edge in between the blocks. */
950 return (single_succ_p (a)
951 && single_succ (a) == b
952 && single_pred_p (b)
953 && a != b
954 /* Must be simple edge. */
955 && !(single_succ_edge (a)->flags & EDGE_COMPLEX)
956 && a->next_bb == b
957 && a != ENTRY_BLOCK_PTR && b != EXIT_BLOCK_PTR
958 /* If the jump insn has side effects,
959 we can't kill the edge. */
960 && (!JUMP_P (BB_END (a))
961 || (reload_completed
962 ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
965 /* Return the label in the head of basic block BLOCK. Create one if it doesn't
966 exist. */
969 block_label (basic_block block)
971 if (block == EXIT_BLOCK_PTR)
972 return NULL_RTX;
974 if (!LABEL_P (BB_HEAD (block)))
976 BB_HEAD (block) = emit_label_before (gen_label_rtx (), BB_HEAD (block));
979 return BB_HEAD (block);
982 /* Attempt to perform edge redirection by replacing possibly complex jump
983 instruction by unconditional jump or removing jump completely. This can
984 apply only if all edges now point to the same block. The parameters and
985 return values are equivalent to redirect_edge_and_branch. */
987 edge
988 try_redirect_by_replacing_jump (edge e, basic_block target, bool in_cfglayout)
990 basic_block src = e->src;
991 rtx insn = BB_END (src), kill_from;
992 rtx set;
993 int fallthru = 0;
995 /* If we are partitioning hot/cold basic blocks, we don't want to
996 mess up unconditional or indirect jumps that cross between hot
997 and cold sections.
999 Basic block partitioning may result in some jumps that appear to
1000 be optimizable (or blocks that appear to be mergeable), but which really
1001 must be left untouched (they are required to make it safely across
1002 partition boundaries). See the comments at the top of
1003 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
1005 if (BB_PARTITION (src) != BB_PARTITION (target))
1006 return NULL;
1008 /* We can replace or remove a complex jump only when we have exactly
1009 two edges. Also, if we have exactly one outgoing edge, we can
1010 redirect that. */
1011 if (EDGE_COUNT (src->succs) >= 3
1012 /* Verify that all targets will be TARGET. Specifically, the
1013 edge that is not E must also go to TARGET. */
1014 || (EDGE_COUNT (src->succs) == 2
1015 && EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target))
1016 return NULL;
1018 if (!onlyjump_p (insn))
1019 return NULL;
1020 if ((!optimize || reload_completed) && tablejump_p (insn, NULL, NULL))
1021 return NULL;
1023 /* Avoid removing branch with side effects. */
1024 set = single_set (insn);
1025 if (!set || side_effects_p (set))
1026 return NULL;
1028 /* In case we zap a conditional jump, we'll need to kill
1029 the cc0 setter too. */
1030 kill_from = insn;
1031 #ifdef HAVE_cc0
1032 if (reg_mentioned_p (cc0_rtx, PATTERN (insn))
1033 && only_sets_cc0_p (PREV_INSN (insn)))
1034 kill_from = PREV_INSN (insn);
1035 #endif
1037 /* See if we can create the fallthru edge. */
1038 if (in_cfglayout || can_fallthru (src, target))
1040 if (dump_file)
1041 fprintf (dump_file, "Removing jump %i.\n", INSN_UID (insn));
1042 fallthru = 1;
1044 /* Selectively unlink whole insn chain. */
1045 if (in_cfglayout)
1047 rtx insn = BB_FOOTER (src);
1049 delete_insn_chain (kill_from, BB_END (src), false);
1051 /* Remove barriers but keep jumptables. */
1052 while (insn)
1054 if (BARRIER_P (insn))
1056 if (PREV_INSN (insn))
1057 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
1058 else
1059 BB_FOOTER (src) = NEXT_INSN (insn);
1060 if (NEXT_INSN (insn))
1061 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
1063 if (LABEL_P (insn))
1064 break;
1065 insn = NEXT_INSN (insn);
1068 else
1069 delete_insn_chain (kill_from, PREV_INSN (BB_HEAD (target)),
1070 false);
1073 /* If this already is simplejump, redirect it. */
1074 else if (simplejump_p (insn))
1076 if (e->dest == target)
1077 return NULL;
1078 if (dump_file)
1079 fprintf (dump_file, "Redirecting jump %i from %i to %i.\n",
1080 INSN_UID (insn), e->dest->index, target->index);
1081 if (!redirect_jump (insn, block_label (target), 0))
1083 gcc_assert (target == EXIT_BLOCK_PTR);
1084 return NULL;
1088 /* Cannot do anything for target exit block. */
1089 else if (target == EXIT_BLOCK_PTR)
1090 return NULL;
1092 /* Or replace possibly complicated jump insn by simple jump insn. */
1093 else
1095 rtx target_label = block_label (target);
1096 rtx barrier, label, table;
1098 emit_jump_insn_after_noloc (gen_jump (target_label), insn);
1099 JUMP_LABEL (BB_END (src)) = target_label;
1100 LABEL_NUSES (target_label)++;
1101 if (dump_file)
1102 fprintf (dump_file, "Replacing insn %i by jump %i\n",
1103 INSN_UID (insn), INSN_UID (BB_END (src)));
1106 delete_insn_chain (kill_from, insn, false);
1108 /* Recognize a tablejump that we are converting to a
1109 simple jump and remove its associated CODE_LABEL
1110 and ADDR_VEC or ADDR_DIFF_VEC. */
1111 if (tablejump_p (insn, &label, &table))
1112 delete_insn_chain (label, table, false);
1114 barrier = next_nonnote_insn (BB_END (src));
1115 if (!barrier || !BARRIER_P (barrier))
1116 emit_barrier_after (BB_END (src));
1117 else
1119 if (barrier != NEXT_INSN (BB_END (src)))
1121 /* Move the jump before barrier so that the notes
1122 which originally were or were created before jump table are
1123 inside the basic block. */
1124 rtx new_insn = BB_END (src);
1126 update_bb_for_insn_chain (NEXT_INSN (BB_END (src)),
1127 PREV_INSN (barrier), src);
1129 NEXT_INSN (PREV_INSN (new_insn)) = NEXT_INSN (new_insn);
1130 PREV_INSN (NEXT_INSN (new_insn)) = PREV_INSN (new_insn);
1132 NEXT_INSN (new_insn) = barrier;
1133 NEXT_INSN (PREV_INSN (barrier)) = new_insn;
1135 PREV_INSN (new_insn) = PREV_INSN (barrier);
1136 PREV_INSN (barrier) = new_insn;
1141 /* Keep only one edge out and set proper flags. */
1142 if (!single_succ_p (src))
1143 remove_edge (e);
1144 gcc_assert (single_succ_p (src));
1146 e = single_succ_edge (src);
1147 if (fallthru)
1148 e->flags = EDGE_FALLTHRU;
1149 else
1150 e->flags = 0;
1152 e->probability = REG_BR_PROB_BASE;
1153 e->count = src->count;
1155 if (e->dest != target)
1156 redirect_edge_succ (e, target);
1157 return e;
1160 /* Subroutine of redirect_branch_edge that tries to patch the jump
1161 instruction INSN so that it reaches block NEW. Do this
1162 only when it originally reached block OLD. Return true if this
1163 worked or the original target wasn't OLD, return false if redirection
1164 doesn't work. */
1166 static bool
1167 patch_jump_insn (rtx insn, rtx old_label, basic_block new_bb)
1169 rtx tmp;
1170 /* Recognize a tablejump and adjust all matching cases. */
1171 if (tablejump_p (insn, NULL, &tmp))
1173 rtvec vec;
1174 int j;
1175 rtx new_label = block_label (new_bb);
1177 if (new_bb == EXIT_BLOCK_PTR)
1178 return false;
1179 if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
1180 vec = XVEC (PATTERN (tmp), 0);
1181 else
1182 vec = XVEC (PATTERN (tmp), 1);
1184 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
1185 if (XEXP (RTVEC_ELT (vec, j), 0) == old_label)
1187 RTVEC_ELT (vec, j) = gen_rtx_LABEL_REF (Pmode, new_label);
1188 --LABEL_NUSES (old_label);
1189 ++LABEL_NUSES (new_label);
1192 /* Handle casesi dispatch insns. */
1193 if ((tmp = single_set (insn)) != NULL
1194 && SET_DEST (tmp) == pc_rtx
1195 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
1196 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF
1197 && XEXP (XEXP (SET_SRC (tmp), 2), 0) == old_label)
1199 XEXP (SET_SRC (tmp), 2) = gen_rtx_LABEL_REF (Pmode,
1200 new_label);
1201 --LABEL_NUSES (old_label);
1202 ++LABEL_NUSES (new_label);
1205 else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL)
1207 int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp);
1208 rtx new_label, note;
1210 if (new_bb == EXIT_BLOCK_PTR)
1211 return false;
1212 new_label = block_label (new_bb);
1214 for (i = 0; i < n; ++i)
1216 rtx old_ref = ASM_OPERANDS_LABEL (tmp, i);
1217 gcc_assert (GET_CODE (old_ref) == LABEL_REF);
1218 if (XEXP (old_ref, 0) == old_label)
1220 ASM_OPERANDS_LABEL (tmp, i)
1221 = gen_rtx_LABEL_REF (Pmode, new_label);
1222 --LABEL_NUSES (old_label);
1223 ++LABEL_NUSES (new_label);
1227 if (JUMP_LABEL (insn) == old_label)
1229 JUMP_LABEL (insn) = new_label;
1230 note = find_reg_note (insn, REG_LABEL_TARGET, new_label);
1231 if (note)
1232 remove_note (insn, note);
1234 else
1236 note = find_reg_note (insn, REG_LABEL_TARGET, old_label);
1237 if (note)
1238 remove_note (insn, note);
1239 if (JUMP_LABEL (insn) != new_label
1240 && !find_reg_note (insn, REG_LABEL_TARGET, new_label))
1241 add_reg_note (insn, REG_LABEL_TARGET, new_label);
1243 while ((note = find_reg_note (insn, REG_LABEL_OPERAND, old_label))
1244 != NULL_RTX)
1245 XEXP (note, 0) = new_label;
1247 else
1249 /* ?? We may play the games with moving the named labels from
1250 one basic block to the other in case only one computed_jump is
1251 available. */
1252 if (computed_jump_p (insn)
1253 /* A return instruction can't be redirected. */
1254 || returnjump_p (insn))
1255 return false;
1257 if (!currently_expanding_to_rtl || JUMP_LABEL (insn) == old_label)
1259 /* If the insn doesn't go where we think, we're confused. */
1260 gcc_assert (JUMP_LABEL (insn) == old_label);
1262 /* If the substitution doesn't succeed, die. This can happen
1263 if the back end emitted unrecognizable instructions or if
1264 target is exit block on some arches. */
1265 if (!redirect_jump (insn, block_label (new_bb), 0))
1267 gcc_assert (new_bb == EXIT_BLOCK_PTR);
1268 return false;
1272 return true;
1276 /* Redirect edge representing branch of (un)conditional jump or tablejump,
1277 NULL on failure */
1278 static edge
1279 redirect_branch_edge (edge e, basic_block target)
1281 rtx old_label = BB_HEAD (e->dest);
1282 basic_block src = e->src;
1283 rtx insn = BB_END (src);
1285 /* We can only redirect non-fallthru edges of jump insn. */
1286 if (e->flags & EDGE_FALLTHRU)
1287 return NULL;
1288 else if (!JUMP_P (insn) && !currently_expanding_to_rtl)
1289 return NULL;
1291 if (!currently_expanding_to_rtl)
1293 if (!patch_jump_insn (insn, old_label, target))
1294 return NULL;
1296 else
1297 /* When expanding this BB might actually contain multiple
1298 jumps (i.e. not yet split by find_many_sub_basic_blocks).
1299 Redirect all of those that match our label. */
1300 FOR_BB_INSNS (src, insn)
1301 if (JUMP_P (insn) && !patch_jump_insn (insn, old_label, target))
1302 return NULL;
1304 if (dump_file)
1305 fprintf (dump_file, "Edge %i->%i redirected to %i\n",
1306 e->src->index, e->dest->index, target->index);
1308 if (e->dest != target)
1309 e = redirect_edge_succ_nodup (e, target);
1311 return e;
1314 /* Called when edge E has been redirected to a new destination,
1315 in order to update the region crossing flag on the edge and
1316 jump. */
1318 static void
1319 fixup_partition_crossing (edge e)
1321 rtx note;
1323 if (e->src == ENTRY_BLOCK_PTR || e->dest == EXIT_BLOCK_PTR)
1324 return;
1325 /* If we redirected an existing edge, it may already be marked
1326 crossing, even though the new src is missing a reg crossing note.
1327 But make sure reg crossing note doesn't already exist before
1328 inserting. */
1329 if (BB_PARTITION (e->src) != BB_PARTITION (e->dest))
1331 e->flags |= EDGE_CROSSING;
1332 note = find_reg_note (BB_END (e->src), REG_CROSSING_JUMP, NULL_RTX);
1333 if (JUMP_P (BB_END (e->src))
1334 && !note)
1335 add_reg_note (BB_END (e->src), REG_CROSSING_JUMP, NULL_RTX);
1337 else if (BB_PARTITION (e->src) == BB_PARTITION (e->dest))
1339 e->flags &= ~EDGE_CROSSING;
1340 /* Remove the section crossing note from jump at end of
1341 src if it exists, and if no other successors are
1342 still crossing. */
1343 note = find_reg_note (BB_END (e->src), REG_CROSSING_JUMP, NULL_RTX);
1344 if (note)
1346 bool has_crossing_succ = false;
1347 edge e2;
1348 edge_iterator ei;
1349 FOR_EACH_EDGE (e2, ei, e->src->succs)
1351 has_crossing_succ |= (e2->flags & EDGE_CROSSING);
1352 if (has_crossing_succ)
1353 break;
1355 if (!has_crossing_succ)
1356 remove_note (BB_END (e->src), note);
1361 /* Called when block BB has been reassigned to the cold partition,
1362 because it is now dominated by another cold block,
1363 to ensure that the region crossing attributes are updated. */
1365 static void
1366 fixup_new_cold_bb (basic_block bb)
1368 edge e;
1369 edge_iterator ei;
1371 /* This is called when a hot bb is found to now be dominated
1372 by a cold bb and therefore needs to become cold. Therefore,
1373 its preds will no longer be region crossing. Any non-dominating
1374 preds that were previously hot would also have become cold
1375 in the caller for the same region. Any preds that were previously
1376 region-crossing will be adjusted in fixup_partition_crossing. */
1377 FOR_EACH_EDGE (e, ei, bb->preds)
1379 fixup_partition_crossing (e);
1382 /* Possibly need to make bb's successor edges region crossing,
1383 or remove stale region crossing. */
1384 FOR_EACH_EDGE (e, ei, bb->succs)
1386 /* We can't have fall-through edges across partition boundaries.
1387 Note that force_nonfallthru will do any necessary partition
1388 boundary fixup by calling fixup_partition_crossing itself. */
1389 if ((e->flags & EDGE_FALLTHRU)
1390 && BB_PARTITION (bb) != BB_PARTITION (e->dest)
1391 && e->dest != EXIT_BLOCK_PTR)
1392 force_nonfallthru (e);
1393 else
1394 fixup_partition_crossing (e);
1398 /* Attempt to change code to redirect edge E to TARGET. Don't do that on
1399 expense of adding new instructions or reordering basic blocks.
1401 Function can be also called with edge destination equivalent to the TARGET.
1402 Then it should try the simplifications and do nothing if none is possible.
1404 Return edge representing the branch if transformation succeeded. Return NULL
1405 on failure.
1406 We still return NULL in case E already destinated TARGET and we didn't
1407 managed to simplify instruction stream. */
1409 static edge
1410 rtl_redirect_edge_and_branch (edge e, basic_block target)
1412 edge ret;
1413 basic_block src = e->src;
1414 basic_block dest = e->dest;
1416 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
1417 return NULL;
1419 if (dest == target)
1420 return e;
1422 if ((ret = try_redirect_by_replacing_jump (e, target, false)) != NULL)
1424 df_set_bb_dirty (src);
1425 fixup_partition_crossing (ret);
1426 return ret;
1429 ret = redirect_branch_edge (e, target);
1430 if (!ret)
1431 return NULL;
1433 df_set_bb_dirty (src);
1434 fixup_partition_crossing (ret);
1435 return ret;
1438 /* Emit a barrier after BB, into the footer if we are in CFGLAYOUT mode. */
1440 void
1441 emit_barrier_after_bb (basic_block bb)
1443 rtx barrier = emit_barrier_after (BB_END (bb));
1444 gcc_assert (current_ir_type() == IR_RTL_CFGRTL
1445 || current_ir_type () == IR_RTL_CFGLAYOUT);
1446 if (current_ir_type () == IR_RTL_CFGLAYOUT)
1447 BB_FOOTER (bb) = unlink_insn_chain (barrier, barrier);
1450 /* Like force_nonfallthru below, but additionally performs redirection
1451 Used by redirect_edge_and_branch_force. JUMP_LABEL is used only
1452 when redirecting to the EXIT_BLOCK, it is either ret_rtx or
1453 simple_return_rtx, indicating which kind of returnjump to create.
1454 It should be NULL otherwise. */
1456 basic_block
1457 force_nonfallthru_and_redirect (edge e, basic_block target, rtx jump_label)
1459 basic_block jump_block, new_bb = NULL, src = e->src;
1460 rtx note;
1461 edge new_edge;
1462 int abnormal_edge_flags = 0;
1463 bool asm_goto_edge = false;
1464 int loc;
1466 /* In the case the last instruction is conditional jump to the next
1467 instruction, first redirect the jump itself and then continue
1468 by creating a basic block afterwards to redirect fallthru edge. */
1469 if (e->src != ENTRY_BLOCK_PTR && e->dest != EXIT_BLOCK_PTR
1470 && any_condjump_p (BB_END (e->src))
1471 && JUMP_LABEL (BB_END (e->src)) == BB_HEAD (e->dest))
1473 rtx note;
1474 edge b = unchecked_make_edge (e->src, target, 0);
1475 bool redirected;
1477 redirected = redirect_jump (BB_END (e->src), block_label (target), 0);
1478 gcc_assert (redirected);
1480 note = find_reg_note (BB_END (e->src), REG_BR_PROB, NULL_RTX);
1481 if (note)
1483 int prob = INTVAL (XEXP (note, 0));
1485 b->probability = prob;
1486 /* Update this to use GCOV_COMPUTE_SCALE. */
1487 b->count = e->count * prob / REG_BR_PROB_BASE;
1488 e->probability -= e->probability;
1489 e->count -= b->count;
1490 if (e->probability < 0)
1491 e->probability = 0;
1492 if (e->count < 0)
1493 e->count = 0;
1497 if (e->flags & EDGE_ABNORMAL)
1499 /* Irritating special case - fallthru edge to the same block as abnormal
1500 edge.
1501 We can't redirect abnormal edge, but we still can split the fallthru
1502 one and create separate abnormal edge to original destination.
1503 This allows bb-reorder to make such edge non-fallthru. */
1504 gcc_assert (e->dest == target);
1505 abnormal_edge_flags = e->flags & ~EDGE_FALLTHRU;
1506 e->flags &= EDGE_FALLTHRU;
1508 else
1510 gcc_assert (e->flags & EDGE_FALLTHRU);
1511 if (e->src == ENTRY_BLOCK_PTR)
1513 /* We can't redirect the entry block. Create an empty block
1514 at the start of the function which we use to add the new
1515 jump. */
1516 edge tmp;
1517 edge_iterator ei;
1518 bool found = false;
1520 basic_block bb = create_basic_block (BB_HEAD (e->dest), NULL, ENTRY_BLOCK_PTR);
1522 /* Change the existing edge's source to be the new block, and add
1523 a new edge from the entry block to the new block. */
1524 e->src = bb;
1525 for (ei = ei_start (ENTRY_BLOCK_PTR->succs); (tmp = ei_safe_edge (ei)); )
1527 if (tmp == e)
1529 ENTRY_BLOCK_PTR->succs->unordered_remove (ei.index);
1530 found = true;
1531 break;
1533 else
1534 ei_next (&ei);
1537 gcc_assert (found);
1539 vec_safe_push (bb->succs, e);
1540 make_single_succ_edge (ENTRY_BLOCK_PTR, bb, EDGE_FALLTHRU);
1544 /* If e->src ends with asm goto, see if any of the ASM_OPERANDS_LABELs
1545 don't point to the target or fallthru label. */
1546 if (JUMP_P (BB_END (e->src))
1547 && target != EXIT_BLOCK_PTR
1548 && (e->flags & EDGE_FALLTHRU)
1549 && (note = extract_asm_operands (PATTERN (BB_END (e->src)))))
1551 int i, n = ASM_OPERANDS_LABEL_LENGTH (note);
1552 bool adjust_jump_target = false;
1554 for (i = 0; i < n; ++i)
1556 if (XEXP (ASM_OPERANDS_LABEL (note, i), 0) == BB_HEAD (e->dest))
1558 LABEL_NUSES (XEXP (ASM_OPERANDS_LABEL (note, i), 0))--;
1559 XEXP (ASM_OPERANDS_LABEL (note, i), 0) = block_label (target);
1560 LABEL_NUSES (XEXP (ASM_OPERANDS_LABEL (note, i), 0))++;
1561 adjust_jump_target = true;
1563 if (XEXP (ASM_OPERANDS_LABEL (note, i), 0) == BB_HEAD (target))
1564 asm_goto_edge = true;
1566 if (adjust_jump_target)
1568 rtx insn = BB_END (e->src), note;
1569 rtx old_label = BB_HEAD (e->dest);
1570 rtx new_label = BB_HEAD (target);
1572 if (JUMP_LABEL (insn) == old_label)
1574 JUMP_LABEL (insn) = new_label;
1575 note = find_reg_note (insn, REG_LABEL_TARGET, new_label);
1576 if (note)
1577 remove_note (insn, note);
1579 else
1581 note = find_reg_note (insn, REG_LABEL_TARGET, old_label);
1582 if (note)
1583 remove_note (insn, note);
1584 if (JUMP_LABEL (insn) != new_label
1585 && !find_reg_note (insn, REG_LABEL_TARGET, new_label))
1586 add_reg_note (insn, REG_LABEL_TARGET, new_label);
1588 while ((note = find_reg_note (insn, REG_LABEL_OPERAND, old_label))
1589 != NULL_RTX)
1590 XEXP (note, 0) = new_label;
1594 if (EDGE_COUNT (e->src->succs) >= 2 || abnormal_edge_flags || asm_goto_edge)
1596 gcov_type count = e->count;
1597 int probability = e->probability;
1598 /* Create the new structures. */
1600 /* If the old block ended with a tablejump, skip its table
1601 by searching forward from there. Otherwise start searching
1602 forward from the last instruction of the old block. */
1603 if (!tablejump_p (BB_END (e->src), NULL, &note))
1604 note = BB_END (e->src);
1605 note = NEXT_INSN (note);
1607 jump_block = create_basic_block (note, NULL, e->src);
1608 jump_block->count = count;
1609 jump_block->frequency = EDGE_FREQUENCY (e);
1611 /* Make sure new block ends up in correct hot/cold section. */
1613 BB_COPY_PARTITION (jump_block, e->src);
1615 /* Wire edge in. */
1616 new_edge = make_edge (e->src, jump_block, EDGE_FALLTHRU);
1617 new_edge->probability = probability;
1618 new_edge->count = count;
1620 /* Redirect old edge. */
1621 redirect_edge_pred (e, jump_block);
1622 e->probability = REG_BR_PROB_BASE;
1624 /* If e->src was previously region crossing, it no longer is
1625 and the reg crossing note should be removed. */
1626 fixup_partition_crossing (new_edge);
1628 /* If asm goto has any label refs to target's label,
1629 add also edge from asm goto bb to target. */
1630 if (asm_goto_edge)
1632 new_edge->probability /= 2;
1633 new_edge->count /= 2;
1634 jump_block->count /= 2;
1635 jump_block->frequency /= 2;
1636 new_edge = make_edge (new_edge->src, target,
1637 e->flags & ~EDGE_FALLTHRU);
1638 new_edge->probability = probability - probability / 2;
1639 new_edge->count = count - count / 2;
1642 new_bb = jump_block;
1644 else
1645 jump_block = e->src;
1647 loc = e->goto_locus;
1648 e->flags &= ~EDGE_FALLTHRU;
1649 if (target == EXIT_BLOCK_PTR)
1651 if (jump_label == ret_rtx)
1653 #ifdef HAVE_return
1654 emit_jump_insn_after_setloc (gen_return (), BB_END (jump_block), loc);
1655 #else
1656 gcc_unreachable ();
1657 #endif
1659 else
1661 gcc_assert (jump_label == simple_return_rtx);
1662 #ifdef HAVE_simple_return
1663 emit_jump_insn_after_setloc (gen_simple_return (),
1664 BB_END (jump_block), loc);
1665 #else
1666 gcc_unreachable ();
1667 #endif
1669 set_return_jump_label (BB_END (jump_block));
1671 else
1673 rtx label = block_label (target);
1674 emit_jump_insn_after_setloc (gen_jump (label), BB_END (jump_block), loc);
1675 JUMP_LABEL (BB_END (jump_block)) = label;
1676 LABEL_NUSES (label)++;
1679 /* We might be in cfg layout mode, and if so, the following routine will
1680 insert the barrier correctly. */
1681 emit_barrier_after_bb (jump_block);
1682 redirect_edge_succ_nodup (e, target);
1684 if (abnormal_edge_flags)
1685 make_edge (src, target, abnormal_edge_flags);
1687 df_mark_solutions_dirty ();
1688 fixup_partition_crossing (e);
1689 return new_bb;
1692 /* Edge E is assumed to be fallthru edge. Emit needed jump instruction
1693 (and possibly create new basic block) to make edge non-fallthru.
1694 Return newly created BB or NULL if none. */
1696 static basic_block
1697 rtl_force_nonfallthru (edge e)
1699 return force_nonfallthru_and_redirect (e, e->dest, NULL_RTX);
1702 /* Redirect edge even at the expense of creating new jump insn or
1703 basic block. Return new basic block if created, NULL otherwise.
1704 Conversion must be possible. */
1706 static basic_block
1707 rtl_redirect_edge_and_branch_force (edge e, basic_block target)
1709 if (redirect_edge_and_branch (e, target)
1710 || e->dest == target)
1711 return NULL;
1713 /* In case the edge redirection failed, try to force it to be non-fallthru
1714 and redirect newly created simplejump. */
1715 df_set_bb_dirty (e->src);
1716 return force_nonfallthru_and_redirect (e, target, NULL_RTX);
1719 /* The given edge should potentially be a fallthru edge. If that is in
1720 fact true, delete the jump and barriers that are in the way. */
1722 static void
1723 rtl_tidy_fallthru_edge (edge e)
1725 rtx q;
1726 basic_block b = e->src, c = b->next_bb;
1728 /* ??? In a late-running flow pass, other folks may have deleted basic
1729 blocks by nopping out blocks, leaving multiple BARRIERs between here
1730 and the target label. They ought to be chastised and fixed.
1732 We can also wind up with a sequence of undeletable labels between
1733 one block and the next.
1735 So search through a sequence of barriers, labels, and notes for
1736 the head of block C and assert that we really do fall through. */
1738 for (q = NEXT_INSN (BB_END (b)); q != BB_HEAD (c); q = NEXT_INSN (q))
1739 if (INSN_P (q))
1740 return;
1742 /* Remove what will soon cease being the jump insn from the source block.
1743 If block B consisted only of this single jump, turn it into a deleted
1744 note. */
1745 q = BB_END (b);
1746 if (JUMP_P (q)
1747 && onlyjump_p (q)
1748 && (any_uncondjump_p (q)
1749 || single_succ_p (b)))
1751 #ifdef HAVE_cc0
1752 /* If this was a conditional jump, we need to also delete
1753 the insn that set cc0. */
1754 if (any_condjump_p (q) && only_sets_cc0_p (PREV_INSN (q)))
1755 q = PREV_INSN (q);
1756 #endif
1758 q = PREV_INSN (q);
1761 /* Selectively unlink the sequence. */
1762 if (q != PREV_INSN (BB_HEAD (c)))
1763 delete_insn_chain (NEXT_INSN (q), PREV_INSN (BB_HEAD (c)), false);
1765 e->flags |= EDGE_FALLTHRU;
1768 /* Should move basic block BB after basic block AFTER. NIY. */
1770 static bool
1771 rtl_move_block_after (basic_block bb ATTRIBUTE_UNUSED,
1772 basic_block after ATTRIBUTE_UNUSED)
1774 return false;
1777 /* Locate the last bb in the same partition as START_BB. */
1779 static basic_block
1780 last_bb_in_partition (basic_block start_bb)
1782 basic_block bb;
1783 FOR_BB_BETWEEN (bb, start_bb, EXIT_BLOCK_PTR, next_bb)
1785 if (BB_PARTITION (start_bb) != BB_PARTITION (bb->next_bb))
1786 return bb;
1788 /* Return bb before EXIT_BLOCK_PTR. */
1789 return bb->prev_bb;
1792 /* Split a (typically critical) edge. Return the new block.
1793 The edge must not be abnormal.
1795 ??? The code generally expects to be called on critical edges.
1796 The case of a block ending in an unconditional jump to a
1797 block with multiple predecessors is not handled optimally. */
1799 static basic_block
1800 rtl_split_edge (edge edge_in)
1802 basic_block bb, new_bb;
1803 rtx before;
1805 /* Abnormal edges cannot be split. */
1806 gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
1808 /* We are going to place the new block in front of edge destination.
1809 Avoid existence of fallthru predecessors. */
1810 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1812 edge e = find_fallthru_edge (edge_in->dest->preds);
1814 if (e)
1815 force_nonfallthru (e);
1818 /* Create the basic block note. */
1819 if (edge_in->dest != EXIT_BLOCK_PTR)
1820 before = BB_HEAD (edge_in->dest);
1821 else
1822 before = NULL_RTX;
1824 /* If this is a fall through edge to the exit block, the blocks might be
1825 not adjacent, and the right place is after the source. */
1826 if ((edge_in->flags & EDGE_FALLTHRU) && edge_in->dest == EXIT_BLOCK_PTR)
1828 before = NEXT_INSN (BB_END (edge_in->src));
1829 bb = create_basic_block (before, NULL, edge_in->src);
1830 BB_COPY_PARTITION (bb, edge_in->src);
1832 else
1834 if (edge_in->src == ENTRY_BLOCK_PTR)
1836 bb = create_basic_block (before, NULL, edge_in->dest->prev_bb);
1837 BB_COPY_PARTITION (bb, edge_in->dest);
1839 else
1841 basic_block after = edge_in->dest->prev_bb;
1842 /* If this is post-bb reordering, and the edge crosses a partition
1843 boundary, the new block needs to be inserted in the bb chain
1844 at the end of the src partition (since we put the new bb into
1845 that partition, see below). Otherwise we may end up creating
1846 an extra partition crossing in the chain, which is illegal.
1847 It can't go after the src, because src may have a fall-through
1848 to a different block. */
1849 if (crtl->bb_reorder_complete
1850 && (edge_in->flags & EDGE_CROSSING))
1852 after = last_bb_in_partition (edge_in->src);
1853 before = NEXT_INSN (BB_END (after));
1854 /* The instruction following the last bb in partition should
1855 be a barrier, since it cannot end in a fall-through. */
1856 gcc_checking_assert (BARRIER_P (before));
1857 before = NEXT_INSN (before);
1859 bb = create_basic_block (before, NULL, after);
1860 /* Put the split bb into the src partition, to avoid creating
1861 a situation where a cold bb dominates a hot bb, in the case
1862 where src is cold and dest is hot. The src will dominate
1863 the new bb (whereas it might not have dominated dest). */
1864 BB_COPY_PARTITION (bb, edge_in->src);
1868 make_single_succ_edge (bb, edge_in->dest, EDGE_FALLTHRU);
1870 /* Can't allow a region crossing edge to be fallthrough. */
1871 if (BB_PARTITION (bb) != BB_PARTITION (edge_in->dest)
1872 && edge_in->dest != EXIT_BLOCK_PTR)
1874 new_bb = force_nonfallthru (single_succ_edge (bb));
1875 gcc_assert (!new_bb);
1878 /* For non-fallthru edges, we must adjust the predecessor's
1879 jump instruction to target our new block. */
1880 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1882 edge redirected = redirect_edge_and_branch (edge_in, bb);
1883 gcc_assert (redirected);
1885 else
1887 if (edge_in->src != ENTRY_BLOCK_PTR)
1889 /* For asm goto even splitting of fallthru edge might
1890 need insn patching, as other labels might point to the
1891 old label. */
1892 rtx last = BB_END (edge_in->src);
1893 if (last
1894 && JUMP_P (last)
1895 && edge_in->dest != EXIT_BLOCK_PTR
1896 && extract_asm_operands (PATTERN (last)) != NULL_RTX
1897 && patch_jump_insn (last, before, bb))
1898 df_set_bb_dirty (edge_in->src);
1900 redirect_edge_succ (edge_in, bb);
1903 return bb;
1906 /* Queue instructions for insertion on an edge between two basic blocks.
1907 The new instructions and basic blocks (if any) will not appear in the
1908 CFG until commit_edge_insertions is called. */
1910 void
1911 insert_insn_on_edge (rtx pattern, edge e)
1913 /* We cannot insert instructions on an abnormal critical edge.
1914 It will be easier to find the culprit if we die now. */
1915 gcc_assert (!((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e)));
1917 if (e->insns.r == NULL_RTX)
1918 start_sequence ();
1919 else
1920 push_to_sequence (e->insns.r);
1922 emit_insn (pattern);
1924 e->insns.r = get_insns ();
1925 end_sequence ();
1928 /* Update the CFG for the instructions queued on edge E. */
1930 void
1931 commit_one_edge_insertion (edge e)
1933 rtx before = NULL_RTX, after = NULL_RTX, insns, tmp, last;
1934 basic_block bb;
1936 /* Pull the insns off the edge now since the edge might go away. */
1937 insns = e->insns.r;
1938 e->insns.r = NULL_RTX;
1940 /* Figure out where to put these insns. If the destination has
1941 one predecessor, insert there. Except for the exit block. */
1942 if (single_pred_p (e->dest) && e->dest != EXIT_BLOCK_PTR)
1944 bb = e->dest;
1946 /* Get the location correct wrt a code label, and "nice" wrt
1947 a basic block note, and before everything else. */
1948 tmp = BB_HEAD (bb);
1949 if (LABEL_P (tmp))
1950 tmp = NEXT_INSN (tmp);
1951 if (NOTE_INSN_BASIC_BLOCK_P (tmp))
1952 tmp = NEXT_INSN (tmp);
1953 if (tmp == BB_HEAD (bb))
1954 before = tmp;
1955 else if (tmp)
1956 after = PREV_INSN (tmp);
1957 else
1958 after = get_last_insn ();
1961 /* If the source has one successor and the edge is not abnormal,
1962 insert there. Except for the entry block. */
1963 else if ((e->flags & EDGE_ABNORMAL) == 0
1964 && single_succ_p (e->src)
1965 && e->src != ENTRY_BLOCK_PTR)
1967 bb = e->src;
1969 /* It is possible to have a non-simple jump here. Consider a target
1970 where some forms of unconditional jumps clobber a register. This
1971 happens on the fr30 for example.
1973 We know this block has a single successor, so we can just emit
1974 the queued insns before the jump. */
1975 if (JUMP_P (BB_END (bb)))
1976 before = BB_END (bb);
1977 else
1979 /* We'd better be fallthru, or we've lost track of what's what. */
1980 gcc_assert (e->flags & EDGE_FALLTHRU);
1982 after = BB_END (bb);
1986 /* Otherwise we must split the edge. */
1987 else
1989 bb = split_edge (e);
1991 /* If E crossed a partition boundary, we needed to make bb end in
1992 a region-crossing jump, even though it was originally fallthru. */
1993 if (JUMP_P (BB_END (bb)))
1994 before = BB_END (bb);
1995 else
1996 after = BB_END (bb);
1999 /* Now that we've found the spot, do the insertion. */
2000 if (before)
2002 emit_insn_before_noloc (insns, before, bb);
2003 last = prev_nonnote_insn (before);
2005 else
2006 last = emit_insn_after_noloc (insns, after, bb);
2008 if (returnjump_p (last))
2010 /* ??? Remove all outgoing edges from BB and add one for EXIT.
2011 This is not currently a problem because this only happens
2012 for the (single) epilogue, which already has a fallthru edge
2013 to EXIT. */
2015 e = single_succ_edge (bb);
2016 gcc_assert (e->dest == EXIT_BLOCK_PTR
2017 && single_succ_p (bb) && (e->flags & EDGE_FALLTHRU));
2019 e->flags &= ~EDGE_FALLTHRU;
2020 emit_barrier_after (last);
2022 if (before)
2023 delete_insn (before);
2025 else
2026 gcc_assert (!JUMP_P (last));
2029 /* Update the CFG for all queued instructions. */
2031 void
2032 commit_edge_insertions (void)
2034 basic_block bb;
2036 /* Optimization passes that invoke this routine can cause hot blocks
2037 previously reached by both hot and cold blocks to become dominated only
2038 by cold blocks. This will cause the verification below to fail,
2039 and lead to now cold code in the hot section. In some cases this
2040 may only be visible after newly unreachable blocks are deleted,
2041 which will be done by fixup_partitions. */
2042 fixup_partitions ();
2044 #ifdef ENABLE_CHECKING
2045 verify_flow_info ();
2046 #endif
2048 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
2050 edge e;
2051 edge_iterator ei;
2053 FOR_EACH_EDGE (e, ei, bb->succs)
2054 if (e->insns.r)
2055 commit_one_edge_insertion (e);
2060 /* Print out RTL-specific basic block information (live information
2061 at start and end with TDF_DETAILS). FLAGS are the TDF_* masks
2062 documented in dumpfile.h. */
2064 static void
2065 rtl_dump_bb (FILE *outf, basic_block bb, int indent, int flags)
2067 rtx insn;
2068 rtx last;
2069 char *s_indent;
2071 s_indent = (char *) alloca ((size_t) indent + 1);
2072 memset (s_indent, ' ', (size_t) indent);
2073 s_indent[indent] = '\0';
2075 if (df && (flags & TDF_DETAILS))
2077 df_dump_top (bb, outf);
2078 putc ('\n', outf);
2081 if (bb->index != ENTRY_BLOCK && bb->index != EXIT_BLOCK)
2082 for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb)); insn != last;
2083 insn = NEXT_INSN (insn))
2085 if (flags & TDF_DETAILS)
2086 df_dump_insn_top (insn, outf);
2087 if (! (flags & TDF_SLIM))
2088 print_rtl_single (outf, insn);
2089 else
2090 dump_insn_slim (outf, insn);
2091 if (flags & TDF_DETAILS)
2092 df_dump_insn_bottom (insn, outf);
2095 if (df && (flags & TDF_DETAILS))
2097 df_dump_bottom (bb, outf);
2098 putc ('\n', outf);
2103 /* Like dump_function_to_file, but for RTL. Print out dataflow information
2104 for the start of each basic block. FLAGS are the TDF_* masks documented
2105 in dumpfile.h. */
2107 void
2108 print_rtl_with_bb (FILE *outf, const_rtx rtx_first, int flags)
2110 const_rtx tmp_rtx;
2111 if (rtx_first == 0)
2112 fprintf (outf, "(nil)\n");
2113 else
2115 enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
2116 int max_uid = get_max_uid ();
2117 basic_block *start = XCNEWVEC (basic_block, max_uid);
2118 basic_block *end = XCNEWVEC (basic_block, max_uid);
2119 enum bb_state *in_bb_p = XCNEWVEC (enum bb_state, max_uid);
2120 basic_block bb;
2122 /* After freeing the CFG, we still have BLOCK_FOR_INSN set on most
2123 insns, but the CFG is not maintained so the basic block info
2124 is not reliable. Therefore it's omitted from the dumps. */
2125 if (! (cfun->curr_properties & PROP_cfg))
2126 flags &= ~TDF_BLOCKS;
2128 if (df)
2129 df_dump_start (outf);
2131 if (flags & TDF_BLOCKS)
2133 FOR_EACH_BB_REVERSE (bb)
2135 rtx x;
2137 start[INSN_UID (BB_HEAD (bb))] = bb;
2138 end[INSN_UID (BB_END (bb))] = bb;
2139 for (x = BB_HEAD (bb); x != NULL_RTX; x = NEXT_INSN (x))
2141 enum bb_state state = IN_MULTIPLE_BB;
2143 if (in_bb_p[INSN_UID (x)] == NOT_IN_BB)
2144 state = IN_ONE_BB;
2145 in_bb_p[INSN_UID (x)] = state;
2147 if (x == BB_END (bb))
2148 break;
2153 for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
2155 if (flags & TDF_BLOCKS)
2157 bb = start[INSN_UID (tmp_rtx)];
2158 if (bb != NULL)
2160 dump_bb_info (outf, bb, 0, dump_flags | TDF_COMMENT, true, false);
2161 if (df && (flags & TDF_DETAILS))
2162 df_dump_top (bb, outf);
2165 if (in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB
2166 && !NOTE_P (tmp_rtx)
2167 && !BARRIER_P (tmp_rtx))
2168 fprintf (outf, ";; Insn is not within a basic block\n");
2169 else if (in_bb_p[INSN_UID (tmp_rtx)] == IN_MULTIPLE_BB)
2170 fprintf (outf, ";; Insn is in multiple basic blocks\n");
2173 if (flags & TDF_DETAILS)
2174 df_dump_insn_top (tmp_rtx, outf);
2175 if (! (flags & TDF_SLIM))
2176 print_rtl_single (outf, tmp_rtx);
2177 else
2178 dump_insn_slim (outf, tmp_rtx);
2179 if (flags & TDF_DETAILS)
2180 df_dump_insn_bottom (tmp_rtx, outf);
2182 if (flags & TDF_BLOCKS)
2184 bb = end[INSN_UID (tmp_rtx)];
2185 if (bb != NULL)
2187 dump_bb_info (outf, bb, 0, dump_flags | TDF_COMMENT, false, true);
2188 if (df && (flags & TDF_DETAILS))
2189 df_dump_bottom (bb, outf);
2190 putc ('\n', outf);
2195 free (start);
2196 free (end);
2197 free (in_bb_p);
2201 /* Update the branch probability of BB if a REG_BR_PROB is present. */
2203 void
2204 update_br_prob_note (basic_block bb)
2206 rtx note;
2207 if (!JUMP_P (BB_END (bb)))
2208 return;
2209 note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX);
2210 if (!note || INTVAL (XEXP (note, 0)) == BRANCH_EDGE (bb)->probability)
2211 return;
2212 XEXP (note, 0) = GEN_INT (BRANCH_EDGE (bb)->probability);
2215 /* Get the last insn associated with block BB (that includes barriers and
2216 tablejumps after BB). */
2218 get_last_bb_insn (basic_block bb)
2220 rtx tmp;
2221 rtx end = BB_END (bb);
2223 /* Include any jump table following the basic block. */
2224 if (tablejump_p (end, NULL, &tmp))
2225 end = tmp;
2227 /* Include any barriers that may follow the basic block. */
2228 tmp = next_nonnote_insn_bb (end);
2229 while (tmp && BARRIER_P (tmp))
2231 end = tmp;
2232 tmp = next_nonnote_insn_bb (end);
2235 return end;
2238 /* Sanity check partition hotness to ensure that basic blocks in
2239   the cold partition don't dominate basic blocks in the hot partition.
2240 If FLAG_ONLY is true, report violations as errors. Otherwise
2241 re-mark the dominated blocks as cold, since this is run after
2242 cfg optimizations that may make hot blocks previously reached
2243 by both hot and cold blocks now only reachable along cold paths. */
2245 static vec<basic_block>
2246 find_partition_fixes (bool flag_only)
2248 basic_block bb;
2249 vec<basic_block> bbs_in_cold_partition = vNULL;
2250 vec<basic_block> bbs_to_fix = vNULL;
2252 /* Callers check this. */
2253 gcc_checking_assert (crtl->has_bb_partition);
2255 FOR_EACH_BB (bb)
2256 if ((BB_PARTITION (bb) == BB_COLD_PARTITION))
2257 bbs_in_cold_partition.safe_push (bb);
2259 if (bbs_in_cold_partition.is_empty ())
2260 return vNULL;
2262 bool dom_calculated_here = !dom_info_available_p (CDI_DOMINATORS);
2264 if (dom_calculated_here)
2265 calculate_dominance_info (CDI_DOMINATORS);
2267 while (! bbs_in_cold_partition.is_empty ())
2269 bb = bbs_in_cold_partition.pop ();
2270 /* Any blocks dominated by a block in the cold section
2271 must also be cold. */
2272 basic_block son;
2273 for (son = first_dom_son (CDI_DOMINATORS, bb);
2274 son;
2275 son = next_dom_son (CDI_DOMINATORS, son))
2277 /* If son is not yet cold, then mark it cold here and
2278 enqueue it for further processing. */
2279 if ((BB_PARTITION (son) != BB_COLD_PARTITION))
2281 if (flag_only)
2282 error ("non-cold basic block %d dominated "
2283 "by a block in the cold partition (%d)", son->index, bb->index);
2284 else
2285 BB_SET_PARTITION (son, BB_COLD_PARTITION);
2286 bbs_to_fix.safe_push (son);
2287 bbs_in_cold_partition.safe_push (son);
2292 if (dom_calculated_here)
2293 free_dominance_info (CDI_DOMINATORS);
2295 return bbs_to_fix;
2298 /* Perform cleanup on the hot/cold bb partitioning after optimization
2299 passes that modify the cfg. */
2301 void
2302 fixup_partitions (void)
2304 basic_block bb;
2306 if (!crtl->has_bb_partition)
2307 return;
2309 /* Delete any blocks that became unreachable and weren't
2310 already cleaned up, for example during edge forwarding
2311 and convert_jumps_to_returns. This will expose more
2312 opportunities for fixing the partition boundaries here.
2313 Also, the calculation of the dominance graph during verification
2314 will assert if there are unreachable nodes. */
2315 delete_unreachable_blocks ();
2317 /* If there are partitions, do a sanity check on them: A basic block in
2318   a cold partition cannot dominate a basic block in a hot partition.
2319 Fixup any that now violate this requirement, as a result of edge
2320 forwarding and unreachable block deletion.  */
2321 vec<basic_block> bbs_to_fix = find_partition_fixes (false);
2323 /* Do the partition fixup after all necessary blocks have been converted to
2324 cold, so that we only update the region crossings the minimum number of
2325 places, which can require forcing edges to be non fallthru. */
2326 while (! bbs_to_fix.is_empty ())
2328 bb = bbs_to_fix.pop ();
2329 fixup_new_cold_bb (bb);
2333 /* Verify, in the basic block chain, that there is at most one switch
2334 between hot/cold partitions. This condition will not be true until
2335 after reorder_basic_blocks is called. */
2337 static int
2338 verify_hot_cold_block_grouping (void)
2340 basic_block bb;
2341 int err = 0;
2342 bool switched_sections = false;
2343 int current_partition = BB_UNPARTITIONED;
2345 /* Even after bb reordering is complete, we go into cfglayout mode
2346 again (in compgoto). Ensure we don't call this before going back
2347 into linearized RTL when any layout fixes would have been committed. */
2348 if (!crtl->bb_reorder_complete
2349 || current_ir_type() != IR_RTL_CFGRTL)
2350 return err;
2352 FOR_EACH_BB (bb)
2354 if (current_partition != BB_UNPARTITIONED
2355 && BB_PARTITION (bb) != current_partition)
2357 if (switched_sections)
2359 error ("multiple hot/cold transitions found (bb %i)",
2360 bb->index);
2361 err = 1;
2363 else
2364 switched_sections = true;
2366 if (!crtl->has_bb_partition)
2367 error ("partition found but function partition flag not set");
2369 current_partition = BB_PARTITION (bb);
2372 return err;
2376 /* Perform several checks on the edges out of each block, such as
2377 the consistency of the branch probabilities, the correctness
2378 of hot/cold partition crossing edges, and the number of expected
2379 successor edges. Also verify that the dominance relationship
2380 between hot/cold blocks is sane. */
2382 static int
2383 rtl_verify_edges (void)
2385 int err = 0;
2386 basic_block bb;
2388 FOR_EACH_BB_REVERSE (bb)
2390 int n_fallthru = 0, n_branch = 0, n_abnormal_call = 0, n_sibcall = 0;
2391 int n_eh = 0, n_abnormal = 0;
2392 edge e, fallthru = NULL;
2393 edge_iterator ei;
2394 rtx note;
2395 bool has_crossing_edge = false;
2397 if (JUMP_P (BB_END (bb))
2398 && (note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX))
2399 && EDGE_COUNT (bb->succs) >= 2
2400 && any_condjump_p (BB_END (bb)))
2402 if (INTVAL (XEXP (note, 0)) != BRANCH_EDGE (bb)->probability
2403 && profile_status != PROFILE_ABSENT)
2405 error ("verify_flow_info: REG_BR_PROB does not match cfg %wi %i",
2406 INTVAL (XEXP (note, 0)), BRANCH_EDGE (bb)->probability);
2407 err = 1;
2411 FOR_EACH_EDGE (e, ei, bb->succs)
2413 bool is_crossing;
2415 if (e->flags & EDGE_FALLTHRU)
2416 n_fallthru++, fallthru = e;
2418 is_crossing = (BB_PARTITION (e->src) != BB_PARTITION (e->dest)
2419 && e->src != ENTRY_BLOCK_PTR
2420 && e->dest != EXIT_BLOCK_PTR);
2421 has_crossing_edge |= is_crossing;
2422 if (e->flags & EDGE_CROSSING)
2424 if (!is_crossing)
2426 error ("EDGE_CROSSING incorrectly set across same section");
2427 err = 1;
2429 if (e->flags & EDGE_FALLTHRU)
2431 error ("fallthru edge crosses section boundary in bb %i",
2432 e->src->index);
2433 err = 1;
2435 if (e->flags & EDGE_EH)
2437 error ("EH edge crosses section boundary in bb %i",
2438 e->src->index);
2439 err = 1;
2441 if (JUMP_P (BB_END (bb))
2442 && !find_reg_note (BB_END (bb), REG_CROSSING_JUMP, NULL_RTX))
2444 error ("No region crossing jump at section boundary in bb %i",
2445 bb->index);
2446 err = 1;
2449 else if (is_crossing)
2451 error ("EDGE_CROSSING missing across section boundary");
2452 err = 1;
2455 if ((e->flags & ~(EDGE_DFS_BACK
2456 | EDGE_CAN_FALLTHRU
2457 | EDGE_IRREDUCIBLE_LOOP
2458 | EDGE_LOOP_EXIT
2459 | EDGE_CROSSING
2460 | EDGE_PRESERVE)) == 0)
2461 n_branch++;
2463 if (e->flags & EDGE_ABNORMAL_CALL)
2464 n_abnormal_call++;
2466 if (e->flags & EDGE_SIBCALL)
2467 n_sibcall++;
2469 if (e->flags & EDGE_EH)
2470 n_eh++;
2472 if (e->flags & EDGE_ABNORMAL)
2473 n_abnormal++;
2476 if (!has_crossing_edge
2477 && find_reg_note (BB_END (bb), REG_CROSSING_JUMP, NULL_RTX))
2479 print_rtl_with_bb (stderr, get_insns (), TDF_RTL | TDF_BLOCKS | TDF_DETAILS);
2480 error ("Region crossing jump across same section in bb %i",
2481 bb->index);
2482 err = 1;
2485 if (n_eh && !find_reg_note (BB_END (bb), REG_EH_REGION, NULL_RTX))
2487 error ("missing REG_EH_REGION note at the end of bb %i", bb->index);
2488 err = 1;
2490 if (n_eh > 1)
2492 error ("too many exception handling edges in bb %i", bb->index);
2493 err = 1;
2495 if (n_branch
2496 && (!JUMP_P (BB_END (bb))
2497 || (n_branch > 1 && (any_uncondjump_p (BB_END (bb))
2498 || any_condjump_p (BB_END (bb))))))
2500 error ("too many outgoing branch edges from bb %i", bb->index);
2501 err = 1;
2503 if (n_fallthru && any_uncondjump_p (BB_END (bb)))
2505 error ("fallthru edge after unconditional jump in bb %i", bb->index);
2506 err = 1;
2508 if (n_branch != 1 && any_uncondjump_p (BB_END (bb)))
2510 error ("wrong number of branch edges after unconditional jump"
2511 " in bb %i", bb->index);
2512 err = 1;
2514 if (n_branch != 1 && any_condjump_p (BB_END (bb))
2515 && JUMP_LABEL (BB_END (bb)) != BB_HEAD (fallthru->dest))
2517 error ("wrong amount of branch edges after conditional jump"
2518 " in bb %i", bb->index);
2519 err = 1;
2521 if (n_abnormal_call && !CALL_P (BB_END (bb)))
2523 error ("abnormal call edges for non-call insn in bb %i", bb->index);
2524 err = 1;
2526 if (n_sibcall && !CALL_P (BB_END (bb)))
2528 error ("sibcall edges for non-call insn in bb %i", bb->index);
2529 err = 1;
2531 if (n_abnormal > n_eh
2532 && !(CALL_P (BB_END (bb))
2533 && n_abnormal == n_abnormal_call + n_sibcall)
2534 && (!JUMP_P (BB_END (bb))
2535 || any_condjump_p (BB_END (bb))
2536 || any_uncondjump_p (BB_END (bb))))
2538 error ("abnormal edges for no purpose in bb %i", bb->index);
2539 err = 1;
2543 /* If there are partitions, do a sanity check on them: A basic block in
2544   a cold partition cannot dominate a basic block in a hot partition.  */
2545 if (crtl->has_bb_partition && !err)
2547 vec<basic_block> bbs_to_fix = find_partition_fixes (true);
2548 err = !bbs_to_fix.is_empty ();
2551 /* Clean up. */
2552 return err;
2555 /* Checks on the instructions within blocks. Currently checks that each
2556 block starts with a basic block note, and that basic block notes and
2557 control flow jumps are not found in the middle of the block. */
2559 static int
2560 rtl_verify_bb_insns (void)
2562 rtx x;
2563 int err = 0;
2564 basic_block bb;
2566 FOR_EACH_BB_REVERSE (bb)
2568 /* Now check the header of basic
2569 block. It ought to contain optional CODE_LABEL followed
2570 by NOTE_BASIC_BLOCK. */
2571 x = BB_HEAD (bb);
2572 if (LABEL_P (x))
2574 if (BB_END (bb) == x)
2576 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
2577 bb->index);
2578 err = 1;
2581 x = NEXT_INSN (x);
2584 if (!NOTE_INSN_BASIC_BLOCK_P (x) || NOTE_BASIC_BLOCK (x) != bb)
2586 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
2587 bb->index);
2588 err = 1;
2591 if (BB_END (bb) == x)
2592 /* Do checks for empty blocks here. */
2594 else
2595 for (x = NEXT_INSN (x); x; x = NEXT_INSN (x))
2597 if (NOTE_INSN_BASIC_BLOCK_P (x))
2599 error ("NOTE_INSN_BASIC_BLOCK %d in middle of basic block %d",
2600 INSN_UID (x), bb->index);
2601 err = 1;
2604 if (x == BB_END (bb))
2605 break;
2607 if (control_flow_insn_p (x))
2609 error ("in basic block %d:", bb->index);
2610 fatal_insn ("flow control insn inside a basic block", x);
2615 /* Clean up. */
2616 return err;
2619 /* Verify that block pointers for instructions in basic blocks, headers and
2620 footers are set appropriately. */
2622 static int
2623 rtl_verify_bb_pointers (void)
2625 int err = 0;
2626 basic_block bb;
2628 /* Check the general integrity of the basic blocks. */
2629 FOR_EACH_BB_REVERSE (bb)
2631 rtx insn;
2633 if (!(bb->flags & BB_RTL))
2635 error ("BB_RTL flag not set for block %d", bb->index);
2636 err = 1;
2639 FOR_BB_INSNS (bb, insn)
2640 if (BLOCK_FOR_INSN (insn) != bb)
2642 error ("insn %d basic block pointer is %d, should be %d",
2643 INSN_UID (insn),
2644 BLOCK_FOR_INSN (insn) ? BLOCK_FOR_INSN (insn)->index : 0,
2645 bb->index);
2646 err = 1;
2649 for (insn = BB_HEADER (bb); insn; insn = NEXT_INSN (insn))
2650 if (!BARRIER_P (insn)
2651 && BLOCK_FOR_INSN (insn) != NULL)
2653 error ("insn %d in header of bb %d has non-NULL basic block",
2654 INSN_UID (insn), bb->index);
2655 err = 1;
2657 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2658 if (!BARRIER_P (insn)
2659 && BLOCK_FOR_INSN (insn) != NULL)
2661 error ("insn %d in footer of bb %d has non-NULL basic block",
2662 INSN_UID (insn), bb->index);
2663 err = 1;
2667 /* Clean up. */
2668 return err;
2671 /* Verify the CFG and RTL consistency common for both underlying RTL and
2672 cfglayout RTL.
2674 Currently it does following checks:
2676 - overlapping of basic blocks
2677 - insns with wrong BLOCK_FOR_INSN pointers
2678 - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note)
2679 - tails of basic blocks (ensure that boundary is necessary)
2680 - scans body of the basic block for JUMP_INSN, CODE_LABEL
2681 and NOTE_INSN_BASIC_BLOCK
2682 - verify that no fall_thru edge crosses hot/cold partition boundaries
2683 - verify that there are no pending RTL branch predictions
2684 - verify that hot blocks are not dominated by cold blocks
2686 In future it can be extended check a lot of other stuff as well
2687 (reachability of basic blocks, life information, etc. etc.). */
2689 static int
2690 rtl_verify_flow_info_1 (void)
2692 int err = 0;
2694 err |= rtl_verify_bb_pointers ();
2696 err |= rtl_verify_bb_insns ();
2698 err |= rtl_verify_edges ();
2700 return err;
2703 /* Walk the instruction chain and verify that bb head/end pointers
2704 are correct, and that instructions are in exactly one bb and have
2705 correct block pointers. */
2707 static int
2708 rtl_verify_bb_insn_chain (void)
2710 basic_block bb;
2711 int err = 0;
2712 rtx x;
2713 rtx last_head = get_last_insn ();
2714 basic_block *bb_info;
2715 const int max_uid = get_max_uid ();
2717 bb_info = XCNEWVEC (basic_block, max_uid);
2719 FOR_EACH_BB_REVERSE (bb)
2721 rtx head = BB_HEAD (bb);
2722 rtx end = BB_END (bb);
2724 for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
2726 /* Verify the end of the basic block is in the INSN chain. */
2727 if (x == end)
2728 break;
2730 /* And that the code outside of basic blocks has NULL bb field. */
2731 if (!BARRIER_P (x)
2732 && BLOCK_FOR_INSN (x) != NULL)
2734 error ("insn %d outside of basic blocks has non-NULL bb field",
2735 INSN_UID (x));
2736 err = 1;
2740 if (!x)
2742 error ("end insn %d for block %d not found in the insn stream",
2743 INSN_UID (end), bb->index);
2744 err = 1;
2747 /* Work backwards from the end to the head of the basic block
2748 to verify the head is in the RTL chain. */
2749 for (; x != NULL_RTX; x = PREV_INSN (x))
2751 /* While walking over the insn chain, verify insns appear
2752 in only one basic block. */
2753 if (bb_info[INSN_UID (x)] != NULL)
2755 error ("insn %d is in multiple basic blocks (%d and %d)",
2756 INSN_UID (x), bb->index, bb_info[INSN_UID (x)]->index);
2757 err = 1;
2760 bb_info[INSN_UID (x)] = bb;
2762 if (x == head)
2763 break;
2765 if (!x)
2767 error ("head insn %d for block %d not found in the insn stream",
2768 INSN_UID (head), bb->index);
2769 err = 1;
2772 last_head = PREV_INSN (x);
2775 for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
2777 /* Check that the code before the first basic block has NULL
2778 bb field. */
2779 if (!BARRIER_P (x)
2780 && BLOCK_FOR_INSN (x) != NULL)
2782 error ("insn %d outside of basic blocks has non-NULL bb field",
2783 INSN_UID (x));
2784 err = 1;
2787 free (bb_info);
2789 return err;
2792 /* Verify that fallthru edges point to adjacent blocks in layout order and
2793 that barriers exist after non-fallthru blocks. */
2795 static int
2796 rtl_verify_fallthru (void)
2798 basic_block bb;
2799 int err = 0;
2801 FOR_EACH_BB_REVERSE (bb)
2803 edge e;
2805 e = find_fallthru_edge (bb->succs);
2806 if (!e)
2808 rtx insn;
2810 /* Ensure existence of barrier in BB with no fallthru edges. */
2811 for (insn = NEXT_INSN (BB_END (bb)); ; insn = NEXT_INSN (insn))
2813 if (!insn || NOTE_INSN_BASIC_BLOCK_P (insn))
2815 error ("missing barrier after block %i", bb->index);
2816 err = 1;
2817 break;
2819 if (BARRIER_P (insn))
2820 break;
2823 else if (e->src != ENTRY_BLOCK_PTR
2824 && e->dest != EXIT_BLOCK_PTR)
2826 rtx insn;
2828 if (e->src->next_bb != e->dest)
2830 error
2831 ("verify_flow_info: Incorrect blocks for fallthru %i->%i",
2832 e->src->index, e->dest->index);
2833 err = 1;
2835 else
2836 for (insn = NEXT_INSN (BB_END (e->src)); insn != BB_HEAD (e->dest);
2837 insn = NEXT_INSN (insn))
2838 if (BARRIER_P (insn) || INSN_P (insn))
2840 error ("verify_flow_info: Incorrect fallthru %i->%i",
2841 e->src->index, e->dest->index);
2842 fatal_insn ("wrong insn in the fallthru edge", insn);
2843 err = 1;
2848 return err;
2851 /* Verify that blocks are laid out in consecutive order. While walking the
2852 instructions, verify that all expected instructions are inside the basic
2853 blocks, and that all returns are followed by barriers. */
2855 static int
2856 rtl_verify_bb_layout (void)
2858 basic_block bb;
2859 int err = 0;
2860 rtx x;
2861 int num_bb_notes;
2862 const rtx rtx_first = get_insns ();
2863 basic_block last_bb_seen = ENTRY_BLOCK_PTR, curr_bb = NULL;
2865 num_bb_notes = 0;
2866 last_bb_seen = ENTRY_BLOCK_PTR;
2868 for (x = rtx_first; x; x = NEXT_INSN (x))
2870 if (NOTE_INSN_BASIC_BLOCK_P (x))
2872 bb = NOTE_BASIC_BLOCK (x);
2874 num_bb_notes++;
2875 if (bb != last_bb_seen->next_bb)
2876 internal_error ("basic blocks not laid down consecutively");
2878 curr_bb = last_bb_seen = bb;
2881 if (!curr_bb)
2883 switch (GET_CODE (x))
2885 case BARRIER:
2886 case NOTE:
2887 break;
2889 case CODE_LABEL:
2890 /* An ADDR_VEC is placed outside any basic block. */
2891 if (NEXT_INSN (x)
2892 && JUMP_TABLE_DATA_P (NEXT_INSN (x)))
2893 x = NEXT_INSN (x);
2895 /* But in any case, non-deletable labels can appear anywhere. */
2896 break;
2898 default:
2899 fatal_insn ("insn outside basic block", x);
2903 if (JUMP_P (x)
2904 && returnjump_p (x) && ! condjump_p (x)
2905 && ! (next_nonnote_insn (x) && BARRIER_P (next_nonnote_insn (x))))
2906 fatal_insn ("return not followed by barrier", x);
2908 if (curr_bb && x == BB_END (curr_bb))
2909 curr_bb = NULL;
2912 if (num_bb_notes != n_basic_blocks - NUM_FIXED_BLOCKS)
2913 internal_error
2914 ("number of bb notes in insn chain (%d) != n_basic_blocks (%d)",
2915 num_bb_notes, n_basic_blocks);
2917 return err;
2920 /* Verify the CFG and RTL consistency common for both underlying RTL and
2921 cfglayout RTL, plus consistency checks specific to linearized RTL mode.
2923 Currently it does following checks:
2924 - all checks of rtl_verify_flow_info_1
2925 - test head/end pointers
2926 - check that blocks are laid out in consecutive order
2927 - check that all insns are in the basic blocks
2928 (except the switch handling code, barriers and notes)
2929 - check that all returns are followed by barriers
2930 - check that all fallthru edge points to the adjacent blocks
2931 - verify that there is a single hot/cold partition boundary after bbro */
2933 static int
2934 rtl_verify_flow_info (void)
2936 int err = 0;
2938 err |= rtl_verify_flow_info_1 ();
2940 err |= rtl_verify_bb_insn_chain ();
2942 err |= rtl_verify_fallthru ();
2944 err |= rtl_verify_bb_layout ();
2946 err |= verify_hot_cold_block_grouping ();
2948 return err;
2951 /* Assume that the preceding pass has possibly eliminated jump instructions
2952 or converted the unconditional jumps. Eliminate the edges from CFG.
2953 Return true if any edges are eliminated. */
2955 bool
2956 purge_dead_edges (basic_block bb)
2958 edge e;
2959 rtx insn = BB_END (bb), note;
2960 bool purged = false;
2961 bool found;
2962 edge_iterator ei;
2964 if (DEBUG_INSN_P (insn) && insn != BB_HEAD (bb))
2966 insn = PREV_INSN (insn);
2967 while ((DEBUG_INSN_P (insn) || NOTE_P (insn)) && insn != BB_HEAD (bb));
2969 /* If this instruction cannot trap, remove REG_EH_REGION notes. */
2970 if (NONJUMP_INSN_P (insn)
2971 && (note = find_reg_note (insn, REG_EH_REGION, NULL)))
2973 rtx eqnote;
2975 if (! may_trap_p (PATTERN (insn))
2976 || ((eqnote = find_reg_equal_equiv_note (insn))
2977 && ! may_trap_p (XEXP (eqnote, 0))))
2978 remove_note (insn, note);
2981 /* Cleanup abnormal edges caused by exceptions or non-local gotos. */
2982 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2984 bool remove = false;
2986 /* There are three types of edges we need to handle correctly here: EH
2987 edges, abnormal call EH edges, and abnormal call non-EH edges. The
2988 latter can appear when nonlocal gotos are used. */
2989 if (e->flags & EDGE_ABNORMAL_CALL)
2991 if (!CALL_P (insn))
2992 remove = true;
2993 else if (can_nonlocal_goto (insn))
2995 else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
2997 else if (flag_tm && find_reg_note (insn, REG_TM, NULL))
2999 else
3000 remove = true;
3002 else if (e->flags & EDGE_EH)
3003 remove = !can_throw_internal (insn);
3005 if (remove)
3007 remove_edge (e);
3008 df_set_bb_dirty (bb);
3009 purged = true;
3011 else
3012 ei_next (&ei);
3015 if (JUMP_P (insn))
3017 rtx note;
3018 edge b,f;
3019 edge_iterator ei;
3021 /* We do care only about conditional jumps and simplejumps. */
3022 if (!any_condjump_p (insn)
3023 && !returnjump_p (insn)
3024 && !simplejump_p (insn))
3025 return purged;
3027 /* Branch probability/prediction notes are defined only for
3028 condjumps. We've possibly turned condjump into simplejump. */
3029 if (simplejump_p (insn))
3031 note = find_reg_note (insn, REG_BR_PROB, NULL);
3032 if (note)
3033 remove_note (insn, note);
3034 while ((note = find_reg_note (insn, REG_BR_PRED, NULL)))
3035 remove_note (insn, note);
3038 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3040 /* Avoid abnormal flags to leak from computed jumps turned
3041 into simplejumps. */
3043 e->flags &= ~EDGE_ABNORMAL;
3045 /* See if this edge is one we should keep. */
3046 if ((e->flags & EDGE_FALLTHRU) && any_condjump_p (insn))
3047 /* A conditional jump can fall through into the next
3048 block, so we should keep the edge. */
3050 ei_next (&ei);
3051 continue;
3053 else if (e->dest != EXIT_BLOCK_PTR
3054 && BB_HEAD (e->dest) == JUMP_LABEL (insn))
3055 /* If the destination block is the target of the jump,
3056 keep the edge. */
3058 ei_next (&ei);
3059 continue;
3061 else if (e->dest == EXIT_BLOCK_PTR && returnjump_p (insn))
3062 /* If the destination block is the exit block, and this
3063 instruction is a return, then keep the edge. */
3065 ei_next (&ei);
3066 continue;
3068 else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
3069 /* Keep the edges that correspond to exceptions thrown by
3070 this instruction and rematerialize the EDGE_ABNORMAL
3071 flag we just cleared above. */
3073 e->flags |= EDGE_ABNORMAL;
3074 ei_next (&ei);
3075 continue;
3078 /* We do not need this edge. */
3079 df_set_bb_dirty (bb);
3080 purged = true;
3081 remove_edge (e);
3084 if (EDGE_COUNT (bb->succs) == 0 || !purged)
3085 return purged;
3087 if (dump_file)
3088 fprintf (dump_file, "Purged edges from bb %i\n", bb->index);
3090 if (!optimize)
3091 return purged;
3093 /* Redistribute probabilities. */
3094 if (single_succ_p (bb))
3096 single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
3097 single_succ_edge (bb)->count = bb->count;
3099 else
3101 note = find_reg_note (insn, REG_BR_PROB, NULL);
3102 if (!note)
3103 return purged;
3105 b = BRANCH_EDGE (bb);
3106 f = FALLTHRU_EDGE (bb);
3107 b->probability = INTVAL (XEXP (note, 0));
3108 f->probability = REG_BR_PROB_BASE - b->probability;
3109 /* Update these to use GCOV_COMPUTE_SCALE. */
3110 b->count = bb->count * b->probability / REG_BR_PROB_BASE;
3111 f->count = bb->count * f->probability / REG_BR_PROB_BASE;
3114 return purged;
3116 else if (CALL_P (insn) && SIBLING_CALL_P (insn))
3118 /* First, there should not be any EH or ABCALL edges resulting
3119 from non-local gotos and the like. If there were, we shouldn't
3120 have created the sibcall in the first place. Second, there
3121 should of course never have been a fallthru edge. */
3122 gcc_assert (single_succ_p (bb));
3123 gcc_assert (single_succ_edge (bb)->flags
3124 == (EDGE_SIBCALL | EDGE_ABNORMAL));
3126 return 0;
3129 /* If we don't see a jump insn, we don't know exactly why the block would
3130 have been broken at this point. Look for a simple, non-fallthru edge,
3131 as these are only created by conditional branches. If we find such an
3132 edge we know that there used to be a jump here and can then safely
3133 remove all non-fallthru edges. */
3134 found = false;
3135 FOR_EACH_EDGE (e, ei, bb->succs)
3136 if (! (e->flags & (EDGE_COMPLEX | EDGE_FALLTHRU)))
3138 found = true;
3139 break;
3142 if (!found)
3143 return purged;
3145 /* Remove all but the fake and fallthru edges. The fake edge may be
3146 the only successor for this block in the case of noreturn
3147 calls. */
3148 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3150 if (!(e->flags & (EDGE_FALLTHRU | EDGE_FAKE)))
3152 df_set_bb_dirty (bb);
3153 remove_edge (e);
3154 purged = true;
3156 else
3157 ei_next (&ei);
3160 gcc_assert (single_succ_p (bb));
3162 single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
3163 single_succ_edge (bb)->count = bb->count;
3165 if (dump_file)
3166 fprintf (dump_file, "Purged non-fallthru edges from bb %i\n",
3167 bb->index);
3168 return purged;
3171 /* Search all basic blocks for potentially dead edges and purge them. Return
3172 true if some edge has been eliminated. */
3174 bool
3175 purge_all_dead_edges (void)
3177 int purged = false;
3178 basic_block bb;
3180 FOR_EACH_BB (bb)
3182 bool purged_here = purge_dead_edges (bb);
3184 purged |= purged_here;
3187 return purged;
3190 /* This is used by a few passes that emit some instructions after abnormal
3191 calls, moving the basic block's end, while they in fact do want to emit
3192 them on the fallthru edge. Look for abnormal call edges, find backward
3193 the call in the block and insert the instructions on the edge instead.
3195 Similarly, handle instructions throwing exceptions internally.
3197 Return true when instructions have been found and inserted on edges. */
3199 bool
3200 fixup_abnormal_edges (void)
3202 bool inserted = false;
3203 basic_block bb;
3205 FOR_EACH_BB (bb)
3207 edge e;
3208 edge_iterator ei;
3210 /* Look for cases we are interested in - calls or instructions causing
3211 exceptions. */
3212 FOR_EACH_EDGE (e, ei, bb->succs)
3213 if ((e->flags & EDGE_ABNORMAL_CALL)
3214 || ((e->flags & (EDGE_ABNORMAL | EDGE_EH))
3215 == (EDGE_ABNORMAL | EDGE_EH)))
3216 break;
3218 if (e && !CALL_P (BB_END (bb)) && !can_throw_internal (BB_END (bb)))
3220 rtx insn;
3222 /* Get past the new insns generated. Allow notes, as the insns
3223 may be already deleted. */
3224 insn = BB_END (bb);
3225 while ((NONJUMP_INSN_P (insn) || NOTE_P (insn))
3226 && !can_throw_internal (insn)
3227 && insn != BB_HEAD (bb))
3228 insn = PREV_INSN (insn);
3230 if (CALL_P (insn) || can_throw_internal (insn))
3232 rtx stop, next;
3234 e = find_fallthru_edge (bb->succs);
3236 stop = NEXT_INSN (BB_END (bb));
3237 BB_END (bb) = insn;
3239 for (insn = NEXT_INSN (insn); insn != stop; insn = next)
3241 next = NEXT_INSN (insn);
3242 if (INSN_P (insn))
3244 delete_insn (insn);
3246 /* Sometimes there's still the return value USE.
3247 If it's placed after a trapping call (i.e. that
3248 call is the last insn anyway), we have no fallthru
3249 edge. Simply delete this use and don't try to insert
3250 on the non-existent edge. */
3251 if (GET_CODE (PATTERN (insn)) != USE)
3253 /* We're not deleting it, we're moving it. */
3254 INSN_DELETED_P (insn) = 0;
3255 PREV_INSN (insn) = NULL_RTX;
3256 NEXT_INSN (insn) = NULL_RTX;
3258 insert_insn_on_edge (insn, e);
3259 inserted = true;
3262 else if (!BARRIER_P (insn))
3263 set_block_for_insn (insn, NULL);
3267 /* It may be that we don't find any trapping insn. In this
3268 case we discovered quite late that the insn that had been
3269 marked as can_throw_internal in fact couldn't trap at all.
3270 So we should in fact delete the EH edges out of the block. */
3271 else
3272 purge_dead_edges (bb);
3276 return inserted;
3279 /* Cut the insns from FIRST to LAST out of the insns stream. */
3282 unlink_insn_chain (rtx first, rtx last)
3284 rtx prevfirst = PREV_INSN (first);
3285 rtx nextlast = NEXT_INSN (last);
3287 PREV_INSN (first) = NULL;
3288 NEXT_INSN (last) = NULL;
3289 if (prevfirst)
3290 NEXT_INSN (prevfirst) = nextlast;
3291 if (nextlast)
3292 PREV_INSN (nextlast) = prevfirst;
3293 else
3294 set_last_insn (prevfirst);
3295 if (!prevfirst)
3296 set_first_insn (nextlast);
3297 return first;
3300 /* Skip over inter-block insns occurring after BB which are typically
3301 associated with BB (e.g., barriers). If there are any such insns,
3302 we return the last one. Otherwise, we return the end of BB. */
3304 static rtx
3305 skip_insns_after_block (basic_block bb)
3307 rtx insn, last_insn, next_head, prev;
3309 next_head = NULL_RTX;
3310 if (bb->next_bb != EXIT_BLOCK_PTR)
3311 next_head = BB_HEAD (bb->next_bb);
3313 for (last_insn = insn = BB_END (bb); (insn = NEXT_INSN (insn)) != 0; )
3315 if (insn == next_head)
3316 break;
3318 switch (GET_CODE (insn))
3320 case BARRIER:
3321 last_insn = insn;
3322 continue;
3324 case NOTE:
3325 switch (NOTE_KIND (insn))
3327 case NOTE_INSN_BLOCK_END:
3328 gcc_unreachable ();
3329 continue;
3330 default:
3331 continue;
3332 break;
3334 break;
3336 case CODE_LABEL:
3337 if (NEXT_INSN (insn)
3338 && JUMP_TABLE_DATA_P (NEXT_INSN (insn)))
3340 insn = NEXT_INSN (insn);
3341 last_insn = insn;
3342 continue;
3344 break;
3346 default:
3347 break;
3350 break;
3353 /* It is possible to hit contradictory sequence. For instance:
3355 jump_insn
3356 NOTE_INSN_BLOCK_BEG
3357 barrier
3359 Where barrier belongs to jump_insn, but the note does not. This can be
3360 created by removing the basic block originally following
3361 NOTE_INSN_BLOCK_BEG. In such case reorder the notes. */
3363 for (insn = last_insn; insn != BB_END (bb); insn = prev)
3365 prev = PREV_INSN (insn);
3366 if (NOTE_P (insn))
3367 switch (NOTE_KIND (insn))
3369 case NOTE_INSN_BLOCK_END:
3370 gcc_unreachable ();
3371 break;
3372 case NOTE_INSN_DELETED:
3373 case NOTE_INSN_DELETED_LABEL:
3374 case NOTE_INSN_DELETED_DEBUG_LABEL:
3375 continue;
3376 default:
3377 reorder_insns (insn, insn, last_insn);
3381 return last_insn;
3384 /* Locate or create a label for a given basic block. */
3386 static rtx
3387 label_for_bb (basic_block bb)
3389 rtx label = BB_HEAD (bb);
3391 if (!LABEL_P (label))
3393 if (dump_file)
3394 fprintf (dump_file, "Emitting label for block %d\n", bb->index);
3396 label = block_label (bb);
3399 return label;
3402 /* Locate the effective beginning and end of the insn chain for each
3403 block, as defined by skip_insns_after_block above. */
3405 static void
3406 record_effective_endpoints (void)
3408 rtx next_insn;
3409 basic_block bb;
3410 rtx insn;
3412 for (insn = get_insns ();
3413 insn
3414 && NOTE_P (insn)
3415 && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK;
3416 insn = NEXT_INSN (insn))
3417 continue;
3418 /* No basic blocks at all? */
3419 gcc_assert (insn);
3421 if (PREV_INSN (insn))
3422 cfg_layout_function_header =
3423 unlink_insn_chain (get_insns (), PREV_INSN (insn));
3424 else
3425 cfg_layout_function_header = NULL_RTX;
3427 next_insn = get_insns ();
3428 FOR_EACH_BB (bb)
3430 rtx end;
3432 if (PREV_INSN (BB_HEAD (bb)) && next_insn != BB_HEAD (bb))
3433 BB_HEADER (bb) = unlink_insn_chain (next_insn,
3434 PREV_INSN (BB_HEAD (bb)));
3435 end = skip_insns_after_block (bb);
3436 if (NEXT_INSN (BB_END (bb)) && BB_END (bb) != end)
3437 BB_FOOTER (bb) = unlink_insn_chain (NEXT_INSN (BB_END (bb)), end);
3438 next_insn = NEXT_INSN (BB_END (bb));
3441 cfg_layout_function_footer = next_insn;
3442 if (cfg_layout_function_footer)
3443 cfg_layout_function_footer = unlink_insn_chain (cfg_layout_function_footer, get_last_insn ());
3446 static unsigned int
3447 into_cfg_layout_mode (void)
3449 cfg_layout_initialize (0);
3450 return 0;
3453 static unsigned int
3454 outof_cfg_layout_mode (void)
3456 basic_block bb;
3458 FOR_EACH_BB (bb)
3459 if (bb->next_bb != EXIT_BLOCK_PTR)
3460 bb->aux = bb->next_bb;
3462 cfg_layout_finalize ();
3464 return 0;
3467 namespace {
3469 const pass_data pass_data_into_cfg_layout_mode =
3471 RTL_PASS, /* type */
3472 "into_cfglayout", /* name */
3473 OPTGROUP_NONE, /* optinfo_flags */
3474 false, /* has_gate */
3475 true, /* has_execute */
3476 TV_CFG, /* tv_id */
3477 0, /* properties_required */
3478 PROP_cfglayout, /* properties_provided */
3479 0, /* properties_destroyed */
3480 0, /* todo_flags_start */
3481 0, /* todo_flags_finish */
3484 class pass_into_cfg_layout_mode : public rtl_opt_pass
3486 public:
3487 pass_into_cfg_layout_mode(gcc::context *ctxt)
3488 : rtl_opt_pass(pass_data_into_cfg_layout_mode, ctxt)
3491 /* opt_pass methods: */
3492 unsigned int execute () { return into_cfg_layout_mode (); }
3494 }; // class pass_into_cfg_layout_mode
3496 } // anon namespace
3498 rtl_opt_pass *
3499 make_pass_into_cfg_layout_mode (gcc::context *ctxt)
3501 return new pass_into_cfg_layout_mode (ctxt);
3504 namespace {
3506 const pass_data pass_data_outof_cfg_layout_mode =
3508 RTL_PASS, /* type */
3509 "outof_cfglayout", /* name */
3510 OPTGROUP_NONE, /* optinfo_flags */
3511 false, /* has_gate */
3512 true, /* has_execute */
3513 TV_CFG, /* tv_id */
3514 0, /* properties_required */
3515 0, /* properties_provided */
3516 PROP_cfglayout, /* properties_destroyed */
3517 0, /* todo_flags_start */
3518 0, /* todo_flags_finish */
3521 class pass_outof_cfg_layout_mode : public rtl_opt_pass
3523 public:
3524 pass_outof_cfg_layout_mode(gcc::context *ctxt)
3525 : rtl_opt_pass(pass_data_outof_cfg_layout_mode, ctxt)
3528 /* opt_pass methods: */
3529 unsigned int execute () { return outof_cfg_layout_mode (); }
3531 }; // class pass_outof_cfg_layout_mode
3533 } // anon namespace
3535 rtl_opt_pass *
3536 make_pass_outof_cfg_layout_mode (gcc::context *ctxt)
3538 return new pass_outof_cfg_layout_mode (ctxt);
3542 /* Link the basic blocks in the correct order, compacting the basic
3543 block queue while at it. If STAY_IN_CFGLAYOUT_MODE is false, this
3544 function also clears the basic block header and footer fields.
3546 This function is usually called after a pass (e.g. tracer) finishes
3547 some transformations while in cfglayout mode. The required sequence
3548 of the basic blocks is in a linked list along the bb->aux field.
3549 This functions re-links the basic block prev_bb and next_bb pointers
3550 accordingly, and it compacts and renumbers the blocks.
3552 FIXME: This currently works only for RTL, but the only RTL-specific
3553 bits are the STAY_IN_CFGLAYOUT_MODE bits. The tracer pass was moved
3554 to GIMPLE a long time ago, but it doesn't relink the basic block
3555 chain. It could do that (to give better initial RTL) if this function
3556 is made IR-agnostic (and moved to cfganal.c or cfg.c while at it). */
3558 void
3559 relink_block_chain (bool stay_in_cfglayout_mode)
3561 basic_block bb, prev_bb;
3562 int index;
3564 /* Maybe dump the re-ordered sequence. */
3565 if (dump_file)
3567 fprintf (dump_file, "Reordered sequence:\n");
3568 for (bb = ENTRY_BLOCK_PTR->next_bb, index = NUM_FIXED_BLOCKS;
3570 bb = (basic_block) bb->aux, index++)
3572 fprintf (dump_file, " %i ", index);
3573 if (get_bb_original (bb))
3574 fprintf (dump_file, "duplicate of %i ",
3575 get_bb_original (bb)->index);
3576 else if (forwarder_block_p (bb)
3577 && !LABEL_P (BB_HEAD (bb)))
3578 fprintf (dump_file, "compensation ");
3579 else
3580 fprintf (dump_file, "bb %i ", bb->index);
3581 fprintf (dump_file, " [%i]\n", bb->frequency);
3585 /* Now reorder the blocks. */
3586 prev_bb = ENTRY_BLOCK_PTR;
3587 bb = ENTRY_BLOCK_PTR->next_bb;
3588 for (; bb; prev_bb = bb, bb = (basic_block) bb->aux)
3590 bb->prev_bb = prev_bb;
3591 prev_bb->next_bb = bb;
3593 prev_bb->next_bb = EXIT_BLOCK_PTR;
3594 EXIT_BLOCK_PTR->prev_bb = prev_bb;
3596 /* Then, clean up the aux fields. */
3597 FOR_ALL_BB (bb)
3599 bb->aux = NULL;
3600 if (!stay_in_cfglayout_mode)
3601 BB_HEADER (bb) = BB_FOOTER (bb) = NULL;
3604 /* Maybe reset the original copy tables, they are not valid anymore
3605 when we renumber the basic blocks in compact_blocks. If we are
3606 are going out of cfglayout mode, don't re-allocate the tables. */
3607 free_original_copy_tables ();
3608 if (stay_in_cfglayout_mode)
3609 initialize_original_copy_tables ();
3611 /* Finally, put basic_block_info in the new order. */
3612 compact_blocks ();
3616 /* Given a reorder chain, rearrange the code to match. */
3618 static void
3619 fixup_reorder_chain (void)
3621 basic_block bb;
3622 rtx insn = NULL;
3624 if (cfg_layout_function_header)
3626 set_first_insn (cfg_layout_function_header);
3627 insn = cfg_layout_function_header;
3628 while (NEXT_INSN (insn))
3629 insn = NEXT_INSN (insn);
3632 /* First do the bulk reordering -- rechain the blocks without regard to
3633 the needed changes to jumps and labels. */
3635 for (bb = ENTRY_BLOCK_PTR->next_bb; bb; bb = (basic_block) bb->aux)
3637 if (BB_HEADER (bb))
3639 if (insn)
3640 NEXT_INSN (insn) = BB_HEADER (bb);
3641 else
3642 set_first_insn (BB_HEADER (bb));
3643 PREV_INSN (BB_HEADER (bb)) = insn;
3644 insn = BB_HEADER (bb);
3645 while (NEXT_INSN (insn))
3646 insn = NEXT_INSN (insn);
3648 if (insn)
3649 NEXT_INSN (insn) = BB_HEAD (bb);
3650 else
3651 set_first_insn (BB_HEAD (bb));
3652 PREV_INSN (BB_HEAD (bb)) = insn;
3653 insn = BB_END (bb);
3654 if (BB_FOOTER (bb))
3656 NEXT_INSN (insn) = BB_FOOTER (bb);
3657 PREV_INSN (BB_FOOTER (bb)) = insn;
3658 while (NEXT_INSN (insn))
3659 insn = NEXT_INSN (insn);
3663 NEXT_INSN (insn) = cfg_layout_function_footer;
3664 if (cfg_layout_function_footer)
3665 PREV_INSN (cfg_layout_function_footer) = insn;
3667 while (NEXT_INSN (insn))
3668 insn = NEXT_INSN (insn);
3670 set_last_insn (insn);
3671 #ifdef ENABLE_CHECKING
3672 verify_insn_chain ();
3673 #endif
3675 /* Now add jumps and labels as needed to match the blocks new
3676 outgoing edges. */
3678 for (bb = ENTRY_BLOCK_PTR->next_bb; bb ; bb = (basic_block) bb->aux)
3680 edge e_fall, e_taken, e;
3681 rtx bb_end_insn;
3682 rtx ret_label = NULL_RTX;
3683 basic_block nb;
3684 edge_iterator ei;
3686 if (EDGE_COUNT (bb->succs) == 0)
3687 continue;
3689 /* Find the old fallthru edge, and another non-EH edge for
3690 a taken jump. */
3691 e_taken = e_fall = NULL;
3693 FOR_EACH_EDGE (e, ei, bb->succs)
3694 if (e->flags & EDGE_FALLTHRU)
3695 e_fall = e;
3696 else if (! (e->flags & EDGE_EH))
3697 e_taken = e;
3699 bb_end_insn = BB_END (bb);
3700 if (JUMP_P (bb_end_insn))
3702 ret_label = JUMP_LABEL (bb_end_insn);
3703 if (any_condjump_p (bb_end_insn))
3705 /* This might happen if the conditional jump has side
3706 effects and could therefore not be optimized away.
3707 Make the basic block to end with a barrier in order
3708 to prevent rtl_verify_flow_info from complaining. */
3709 if (!e_fall)
3711 gcc_assert (!onlyjump_p (bb_end_insn)
3712 || returnjump_p (bb_end_insn));
3713 emit_barrier_after (bb_end_insn);
3714 continue;
3717 /* If the old fallthru is still next, nothing to do. */
3718 if (bb->aux == e_fall->dest
3719 || e_fall->dest == EXIT_BLOCK_PTR)
3720 continue;
3722 /* The degenerated case of conditional jump jumping to the next
3723 instruction can happen for jumps with side effects. We need
3724 to construct a forwarder block and this will be done just
3725 fine by force_nonfallthru below. */
3726 if (!e_taken)
3729 /* There is another special case: if *neither* block is next,
3730 such as happens at the very end of a function, then we'll
3731 need to add a new unconditional jump. Choose the taken
3732 edge based on known or assumed probability. */
3733 else if (bb->aux != e_taken->dest)
3735 rtx note = find_reg_note (bb_end_insn, REG_BR_PROB, 0);
3737 if (note
3738 && INTVAL (XEXP (note, 0)) < REG_BR_PROB_BASE / 2
3739 && invert_jump (bb_end_insn,
3740 (e_fall->dest == EXIT_BLOCK_PTR
3741 ? NULL_RTX
3742 : label_for_bb (e_fall->dest)), 0))
3744 e_fall->flags &= ~EDGE_FALLTHRU;
3745 gcc_checking_assert (could_fall_through
3746 (e_taken->src, e_taken->dest));
3747 e_taken->flags |= EDGE_FALLTHRU;
3748 update_br_prob_note (bb);
3749 e = e_fall, e_fall = e_taken, e_taken = e;
3753 /* If the "jumping" edge is a crossing edge, and the fall
3754 through edge is non-crossing, leave things as they are. */
3755 else if ((e_taken->flags & EDGE_CROSSING)
3756 && !(e_fall->flags & EDGE_CROSSING))
3757 continue;
3759 /* Otherwise we can try to invert the jump. This will
3760 basically never fail, however, keep up the pretense. */
3761 else if (invert_jump (bb_end_insn,
3762 (e_fall->dest == EXIT_BLOCK_PTR
3763 ? NULL_RTX
3764 : label_for_bb (e_fall->dest)), 0))
3766 e_fall->flags &= ~EDGE_FALLTHRU;
3767 gcc_checking_assert (could_fall_through
3768 (e_taken->src, e_taken->dest));
3769 e_taken->flags |= EDGE_FALLTHRU;
3770 update_br_prob_note (bb);
3771 if (LABEL_NUSES (ret_label) == 0
3772 && single_pred_p (e_taken->dest))
3773 delete_insn (ret_label);
3774 continue;
3777 else if (extract_asm_operands (PATTERN (bb_end_insn)) != NULL)
3779 /* If the old fallthru is still next or if
3780 asm goto doesn't have a fallthru (e.g. when followed by
3781 __builtin_unreachable ()), nothing to do. */
3782 if (! e_fall
3783 || bb->aux == e_fall->dest
3784 || e_fall->dest == EXIT_BLOCK_PTR)
3785 continue;
3787 /* Otherwise we'll have to use the fallthru fixup below. */
3789 else
3791 /* Otherwise we have some return, switch or computed
3792 jump. In the 99% case, there should not have been a
3793 fallthru edge. */
3794 gcc_assert (returnjump_p (bb_end_insn) || !e_fall);
3795 continue;
3798 else
3800 /* No fallthru implies a noreturn function with EH edges, or
3801 something similarly bizarre. In any case, we don't need to
3802 do anything. */
3803 if (! e_fall)
3804 continue;
3806 /* If the fallthru block is still next, nothing to do. */
3807 if (bb->aux == e_fall->dest)
3808 continue;
3810 /* A fallthru to exit block. */
3811 if (e_fall->dest == EXIT_BLOCK_PTR)
3812 continue;
3815 /* We got here if we need to add a new jump insn.
3816 Note force_nonfallthru can delete E_FALL and thus we have to
3817 save E_FALL->src prior to the call to force_nonfallthru. */
3818 nb = force_nonfallthru_and_redirect (e_fall, e_fall->dest, ret_label);
3819 if (nb)
3821 nb->aux = bb->aux;
3822 bb->aux = nb;
3823 /* Don't process this new block. */
3824 bb = nb;
3828 relink_block_chain (/*stay_in_cfglayout_mode=*/false);
3830 /* Annoying special case - jump around dead jumptables left in the code. */
3831 FOR_EACH_BB (bb)
3833 edge e = find_fallthru_edge (bb->succs);
3835 if (e && !can_fallthru (e->src, e->dest))
3836 force_nonfallthru (e);
3839 /* Ensure goto_locus from edges has some instructions with that locus
3840 in RTL. */
3841 if (!optimize)
3842 FOR_EACH_BB (bb)
3844 edge e;
3845 edge_iterator ei;
3847 FOR_EACH_EDGE (e, ei, bb->succs)
3848 if (LOCATION_LOCUS (e->goto_locus) != UNKNOWN_LOCATION
3849 && !(e->flags & EDGE_ABNORMAL))
3851 edge e2;
3852 edge_iterator ei2;
3853 basic_block dest, nb;
3854 rtx end;
3856 insn = BB_END (e->src);
3857 end = PREV_INSN (BB_HEAD (e->src));
3858 while (insn != end
3859 && (!NONDEBUG_INSN_P (insn) || !INSN_HAS_LOCATION (insn)))
3860 insn = PREV_INSN (insn);
3861 if (insn != end
3862 && INSN_LOCATION (insn) == e->goto_locus)
3863 continue;
3864 if (simplejump_p (BB_END (e->src))
3865 && !INSN_HAS_LOCATION (BB_END (e->src)))
3867 INSN_LOCATION (BB_END (e->src)) = e->goto_locus;
3868 continue;
3870 dest = e->dest;
3871 if (dest == EXIT_BLOCK_PTR)
3873 /* Non-fallthru edges to the exit block cannot be split. */
3874 if (!(e->flags & EDGE_FALLTHRU))
3875 continue;
3877 else
3879 insn = BB_HEAD (dest);
3880 end = NEXT_INSN (BB_END (dest));
3881 while (insn != end && !NONDEBUG_INSN_P (insn))
3882 insn = NEXT_INSN (insn);
3883 if (insn != end && INSN_HAS_LOCATION (insn)
3884 && INSN_LOCATION (insn) == e->goto_locus)
3885 continue;
3887 nb = split_edge (e);
3888 if (!INSN_P (BB_END (nb)))
3889 BB_END (nb) = emit_insn_after_noloc (gen_nop (), BB_END (nb),
3890 nb);
3891 INSN_LOCATION (BB_END (nb)) = e->goto_locus;
3893 /* If there are other incoming edges to the destination block
3894 with the same goto locus, redirect them to the new block as
3895 well, this can prevent other such blocks from being created
3896 in subsequent iterations of the loop. */
3897 for (ei2 = ei_start (dest->preds); (e2 = ei_safe_edge (ei2)); )
3898 if (LOCATION_LOCUS (e2->goto_locus) != UNKNOWN_LOCATION
3899 && !(e2->flags & (EDGE_ABNORMAL | EDGE_FALLTHRU))
3900 && e->goto_locus == e2->goto_locus)
3901 redirect_edge_and_branch (e2, nb);
3902 else
3903 ei_next (&ei2);
3908 /* Perform sanity checks on the insn chain.
3909 1. Check that next/prev pointers are consistent in both the forward and
3910 reverse direction.
3911 2. Count insns in chain, going both directions, and check if equal.
3912 3. Check that get_last_insn () returns the actual end of chain. */
3914 DEBUG_FUNCTION void
3915 verify_insn_chain (void)
3917 rtx x, prevx, nextx;
3918 int insn_cnt1, insn_cnt2;
3920 for (prevx = NULL, insn_cnt1 = 1, x = get_insns ();
3921 x != 0;
3922 prevx = x, insn_cnt1++, x = NEXT_INSN (x))
3923 gcc_assert (PREV_INSN (x) == prevx);
3925 gcc_assert (prevx == get_last_insn ());
3927 for (nextx = NULL, insn_cnt2 = 1, x = get_last_insn ();
3928 x != 0;
3929 nextx = x, insn_cnt2++, x = PREV_INSN (x))
3930 gcc_assert (NEXT_INSN (x) == nextx);
3932 gcc_assert (insn_cnt1 == insn_cnt2);
3935 /* If we have assembler epilogues, the block falling through to exit must
3936 be the last one in the reordered chain when we reach final. Ensure
3937 that this condition is met. */
3938 static void
3939 fixup_fallthru_exit_predecessor (void)
3941 edge e;
3942 basic_block bb = NULL;
3944 /* This transformation is not valid before reload, because we might
3945 separate a call from the instruction that copies the return
3946 value. */
3947 gcc_assert (reload_completed);
3949 e = find_fallthru_edge (EXIT_BLOCK_PTR->preds);
3950 if (e)
3951 bb = e->src;
3953 if (bb && bb->aux)
3955 basic_block c = ENTRY_BLOCK_PTR->next_bb;
3957 /* If the very first block is the one with the fall-through exit
3958 edge, we have to split that block. */
3959 if (c == bb)
3961 bb = split_block (bb, NULL)->dest;
3962 bb->aux = c->aux;
3963 c->aux = bb;
3964 BB_FOOTER (bb) = BB_FOOTER (c);
3965 BB_FOOTER (c) = NULL;
3968 while (c->aux != bb)
3969 c = (basic_block) c->aux;
3971 c->aux = bb->aux;
3972 while (c->aux)
3973 c = (basic_block) c->aux;
3975 c->aux = bb;
3976 bb->aux = NULL;
3980 /* In case there are more than one fallthru predecessors of exit, force that
3981 there is only one. */
3983 static void
3984 force_one_exit_fallthru (void)
3986 edge e, predecessor = NULL;
3987 bool more = false;
3988 edge_iterator ei;
3989 basic_block forwarder, bb;
3991 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
3992 if (e->flags & EDGE_FALLTHRU)
3994 if (predecessor == NULL)
3995 predecessor = e;
3996 else
3998 more = true;
3999 break;
4003 if (!more)
4004 return;
4006 /* Exit has several fallthru predecessors. Create a forwarder block for
4007 them. */
4008 forwarder = split_edge (predecessor);
4009 for (ei = ei_start (EXIT_BLOCK_PTR->preds); (e = ei_safe_edge (ei)); )
4011 if (e->src == forwarder
4012 || !(e->flags & EDGE_FALLTHRU))
4013 ei_next (&ei);
4014 else
4015 redirect_edge_and_branch_force (e, forwarder);
4018 /* Fix up the chain of blocks -- make FORWARDER immediately precede the
4019 exit block. */
4020 FOR_EACH_BB (bb)
4022 if (bb->aux == NULL && bb != forwarder)
4024 bb->aux = forwarder;
4025 break;
4030 /* Return true in case it is possible to duplicate the basic block BB. */
4032 static bool
4033 cfg_layout_can_duplicate_bb_p (const_basic_block bb)
4035 /* Do not attempt to duplicate tablejumps, as we need to unshare
4036 the dispatch table. This is difficult to do, as the instructions
4037 computing jump destination may be hoisted outside the basic block. */
4038 if (tablejump_p (BB_END (bb), NULL, NULL))
4039 return false;
4041 /* Do not duplicate blocks containing insns that can't be copied. */
4042 if (targetm.cannot_copy_insn_p)
4044 rtx insn = BB_HEAD (bb);
4045 while (1)
4047 if (INSN_P (insn) && targetm.cannot_copy_insn_p (insn))
4048 return false;
4049 if (insn == BB_END (bb))
4050 break;
4051 insn = NEXT_INSN (insn);
4055 return true;
4059 duplicate_insn_chain (rtx from, rtx to)
4061 rtx insn, next, last, copy;
4063 /* Avoid updating of boundaries of previous basic block. The
4064 note will get removed from insn stream in fixup. */
4065 last = emit_note (NOTE_INSN_DELETED);
4067 /* Create copy at the end of INSN chain. The chain will
4068 be reordered later. */
4069 for (insn = from; insn != NEXT_INSN (to); insn = NEXT_INSN (insn))
4071 switch (GET_CODE (insn))
4073 case DEBUG_INSN:
4074 /* Don't duplicate label debug insns. */
4075 if (TREE_CODE (INSN_VAR_LOCATION_DECL (insn)) == LABEL_DECL)
4076 break;
4077 /* FALLTHRU */
4078 case INSN:
4079 case CALL_INSN:
4080 case JUMP_INSN:
4081 copy = emit_copy_of_insn_after (insn, get_last_insn ());
4082 if (JUMP_P (insn) && JUMP_LABEL (insn) != NULL_RTX
4083 && ANY_RETURN_P (JUMP_LABEL (insn)))
4084 JUMP_LABEL (copy) = JUMP_LABEL (insn);
4085 maybe_copy_prologue_epilogue_insn (insn, copy);
4086 break;
4088 case JUMP_TABLE_DATA:
4089 /* Avoid copying of dispatch tables. We never duplicate
4090 tablejumps, so this can hit only in case the table got
4091 moved far from original jump.
4092 Avoid copying following barrier as well if any
4093 (and debug insns in between). */
4094 for (next = NEXT_INSN (insn);
4095 next != NEXT_INSN (to);
4096 next = NEXT_INSN (next))
4097 if (!DEBUG_INSN_P (next))
4098 break;
4099 if (next != NEXT_INSN (to) && BARRIER_P (next))
4100 insn = next;
4101 break;
4103 case CODE_LABEL:
4104 break;
4106 case BARRIER:
4107 emit_barrier ();
4108 break;
4110 case NOTE:
4111 switch (NOTE_KIND (insn))
4113 /* In case prologue is empty and function contain label
4114 in first BB, we may want to copy the block. */
4115 case NOTE_INSN_PROLOGUE_END:
4117 case NOTE_INSN_DELETED:
4118 case NOTE_INSN_DELETED_LABEL:
4119 case NOTE_INSN_DELETED_DEBUG_LABEL:
4120 /* No problem to strip these. */
4121 case NOTE_INSN_FUNCTION_BEG:
4122 /* There is always just single entry to function. */
4123 case NOTE_INSN_BASIC_BLOCK:
4124 /* We should only switch text sections once. */
4125 case NOTE_INSN_SWITCH_TEXT_SECTIONS:
4126 break;
4128 case NOTE_INSN_EPILOGUE_BEG:
4129 emit_note_copy (insn);
4130 break;
4132 default:
4133 /* All other notes should have already been eliminated. */
4134 gcc_unreachable ();
4136 break;
4137 default:
4138 gcc_unreachable ();
4141 insn = NEXT_INSN (last);
4142 delete_insn (last);
4143 return insn;
4146 /* Create a duplicate of the basic block BB. */
4148 static basic_block
4149 cfg_layout_duplicate_bb (basic_block bb)
4151 rtx insn;
4152 basic_block new_bb;
4154 insn = duplicate_insn_chain (BB_HEAD (bb), BB_END (bb));
4155 new_bb = create_basic_block (insn,
4156 insn ? get_last_insn () : NULL,
4157 EXIT_BLOCK_PTR->prev_bb);
4159 BB_COPY_PARTITION (new_bb, bb);
4160 if (BB_HEADER (bb))
4162 insn = BB_HEADER (bb);
4163 while (NEXT_INSN (insn))
4164 insn = NEXT_INSN (insn);
4165 insn = duplicate_insn_chain (BB_HEADER (bb), insn);
4166 if (insn)
4167 BB_HEADER (new_bb) = unlink_insn_chain (insn, get_last_insn ());
4170 if (BB_FOOTER (bb))
4172 insn = BB_FOOTER (bb);
4173 while (NEXT_INSN (insn))
4174 insn = NEXT_INSN (insn);
4175 insn = duplicate_insn_chain (BB_FOOTER (bb), insn);
4176 if (insn)
4177 BB_FOOTER (new_bb) = unlink_insn_chain (insn, get_last_insn ());
4180 return new_bb;
4184 /* Main entry point to this module - initialize the datastructures for
4185 CFG layout changes. It keeps LOOPS up-to-date if not null.
4187 FLAGS is a set of additional flags to pass to cleanup_cfg(). */
4189 void
4190 cfg_layout_initialize (unsigned int flags)
4192 rtx x;
4193 basic_block bb;
4195 initialize_original_copy_tables ();
4197 cfg_layout_rtl_register_cfg_hooks ();
4199 record_effective_endpoints ();
4201 /* Make sure that the targets of non local gotos are marked. */
4202 for (x = nonlocal_goto_handler_labels; x; x = XEXP (x, 1))
4204 bb = BLOCK_FOR_INSN (XEXP (x, 0));
4205 bb->flags |= BB_NON_LOCAL_GOTO_TARGET;
4208 cleanup_cfg (CLEANUP_CFGLAYOUT | flags);
4211 /* Splits superblocks. */
4212 void
4213 break_superblocks (void)
4215 sbitmap superblocks;
4216 bool need = false;
4217 basic_block bb;
4219 superblocks = sbitmap_alloc (last_basic_block);
4220 bitmap_clear (superblocks);
4222 FOR_EACH_BB (bb)
4223 if (bb->flags & BB_SUPERBLOCK)
4225 bb->flags &= ~BB_SUPERBLOCK;
4226 bitmap_set_bit (superblocks, bb->index);
4227 need = true;
4230 if (need)
4232 rebuild_jump_labels (get_insns ());
4233 find_many_sub_basic_blocks (superblocks);
4236 free (superblocks);
4239 /* Finalize the changes: reorder insn list according to the sequence specified
4240 by aux pointers, enter compensation code, rebuild scope forest. */
4242 void
4243 cfg_layout_finalize (void)
4245 #ifdef ENABLE_CHECKING
4246 verify_flow_info ();
4247 #endif
4248 force_one_exit_fallthru ();
4249 rtl_register_cfg_hooks ();
4250 if (reload_completed
4251 #ifdef HAVE_epilogue
4252 && !HAVE_epilogue
4253 #endif
4255 fixup_fallthru_exit_predecessor ();
4256 fixup_reorder_chain ();
4258 rebuild_jump_labels (get_insns ());
4259 delete_dead_jumptables ();
4261 #ifdef ENABLE_CHECKING
4262 verify_insn_chain ();
4263 verify_flow_info ();
4264 #endif
4268 /* Same as split_block but update cfg_layout structures. */
4270 static basic_block
4271 cfg_layout_split_block (basic_block bb, void *insnp)
4273 rtx insn = (rtx) insnp;
4274 basic_block new_bb = rtl_split_block (bb, insn);
4276 BB_FOOTER (new_bb) = BB_FOOTER (bb);
4277 BB_FOOTER (bb) = NULL;
4279 return new_bb;
4282 /* Redirect Edge to DEST. */
4283 static edge
4284 cfg_layout_redirect_edge_and_branch (edge e, basic_block dest)
4286 basic_block src = e->src;
4287 edge ret;
4289 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
4290 return NULL;
4292 if (e->dest == dest)
4293 return e;
4295 if (e->src != ENTRY_BLOCK_PTR
4296 && (ret = try_redirect_by_replacing_jump (e, dest, true)))
4298 df_set_bb_dirty (src);
4299 return ret;
4302 if (e->src == ENTRY_BLOCK_PTR
4303 && (e->flags & EDGE_FALLTHRU) && !(e->flags & EDGE_COMPLEX))
4305 if (dump_file)
4306 fprintf (dump_file, "Redirecting entry edge from bb %i to %i\n",
4307 e->src->index, dest->index);
4309 df_set_bb_dirty (e->src);
4310 redirect_edge_succ (e, dest);
4311 return e;
4314 /* Redirect_edge_and_branch may decide to turn branch into fallthru edge
4315 in the case the basic block appears to be in sequence. Avoid this
4316 transformation. */
4318 if (e->flags & EDGE_FALLTHRU)
4320 /* Redirect any branch edges unified with the fallthru one. */
4321 if (JUMP_P (BB_END (src))
4322 && label_is_jump_target_p (BB_HEAD (e->dest),
4323 BB_END (src)))
4325 edge redirected;
4327 if (dump_file)
4328 fprintf (dump_file, "Fallthru edge unified with branch "
4329 "%i->%i redirected to %i\n",
4330 e->src->index, e->dest->index, dest->index);
4331 e->flags &= ~EDGE_FALLTHRU;
4332 redirected = redirect_branch_edge (e, dest);
4333 gcc_assert (redirected);
4334 redirected->flags |= EDGE_FALLTHRU;
4335 df_set_bb_dirty (redirected->src);
4336 return redirected;
4338 /* In case we are redirecting fallthru edge to the branch edge
4339 of conditional jump, remove it. */
4340 if (EDGE_COUNT (src->succs) == 2)
4342 /* Find the edge that is different from E. */
4343 edge s = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e);
4345 if (s->dest == dest
4346 && any_condjump_p (BB_END (src))
4347 && onlyjump_p (BB_END (src)))
4348 delete_insn (BB_END (src));
4350 if (dump_file)
4351 fprintf (dump_file, "Redirecting fallthru edge %i->%i to %i\n",
4352 e->src->index, e->dest->index, dest->index);
4353 ret = redirect_edge_succ_nodup (e, dest);
4355 else
4356 ret = redirect_branch_edge (e, dest);
4358 /* We don't want simplejumps in the insn stream during cfglayout. */
4359 gcc_assert (!simplejump_p (BB_END (src)));
4361 df_set_bb_dirty (src);
4362 return ret;
4365 /* Simple wrapper as we always can redirect fallthru edges. */
4366 static basic_block
4367 cfg_layout_redirect_edge_and_branch_force (edge e, basic_block dest)
4369 edge redirected = cfg_layout_redirect_edge_and_branch (e, dest);
4371 gcc_assert (redirected);
4372 return NULL;
4375 /* Same as delete_basic_block but update cfg_layout structures. */
4377 static void
4378 cfg_layout_delete_block (basic_block bb)
4380 rtx insn, next, prev = PREV_INSN (BB_HEAD (bb)), *to, remaints;
4382 if (BB_HEADER (bb))
4384 next = BB_HEAD (bb);
4385 if (prev)
4386 NEXT_INSN (prev) = BB_HEADER (bb);
4387 else
4388 set_first_insn (BB_HEADER (bb));
4389 PREV_INSN (BB_HEADER (bb)) = prev;
4390 insn = BB_HEADER (bb);
4391 while (NEXT_INSN (insn))
4392 insn = NEXT_INSN (insn);
4393 NEXT_INSN (insn) = next;
4394 PREV_INSN (next) = insn;
4396 next = NEXT_INSN (BB_END (bb));
4397 if (BB_FOOTER (bb))
4399 insn = BB_FOOTER (bb);
4400 while (insn)
4402 if (BARRIER_P (insn))
4404 if (PREV_INSN (insn))
4405 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
4406 else
4407 BB_FOOTER (bb) = NEXT_INSN (insn);
4408 if (NEXT_INSN (insn))
4409 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
4411 if (LABEL_P (insn))
4412 break;
4413 insn = NEXT_INSN (insn);
4415 if (BB_FOOTER (bb))
4417 insn = BB_END (bb);
4418 NEXT_INSN (insn) = BB_FOOTER (bb);
4419 PREV_INSN (BB_FOOTER (bb)) = insn;
4420 while (NEXT_INSN (insn))
4421 insn = NEXT_INSN (insn);
4422 NEXT_INSN (insn) = next;
4423 if (next)
4424 PREV_INSN (next) = insn;
4425 else
4426 set_last_insn (insn);
4429 if (bb->next_bb != EXIT_BLOCK_PTR)
4430 to = &BB_HEADER (bb->next_bb);
4431 else
4432 to = &cfg_layout_function_footer;
4434 rtl_delete_block (bb);
4436 if (prev)
4437 prev = NEXT_INSN (prev);
4438 else
4439 prev = get_insns ();
4440 if (next)
4441 next = PREV_INSN (next);
4442 else
4443 next = get_last_insn ();
4445 if (next && NEXT_INSN (next) != prev)
4447 remaints = unlink_insn_chain (prev, next);
4448 insn = remaints;
4449 while (NEXT_INSN (insn))
4450 insn = NEXT_INSN (insn);
4451 NEXT_INSN (insn) = *to;
4452 if (*to)
4453 PREV_INSN (*to) = insn;
4454 *to = remaints;
4458 /* Return true when blocks A and B can be safely merged. */
4460 static bool
4461 cfg_layout_can_merge_blocks_p (basic_block a, basic_block b)
4463 /* If we are partitioning hot/cold basic blocks, we don't want to
4464 mess up unconditional or indirect jumps that cross between hot
4465 and cold sections.
4467 Basic block partitioning may result in some jumps that appear to
4468 be optimizable (or blocks that appear to be mergeable), but which really
4469 must be left untouched (they are required to make it safely across
4470 partition boundaries). See the comments at the top of
4471 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
4473 if (BB_PARTITION (a) != BB_PARTITION (b))
4474 return false;
4476 /* Protect the loop latches. */
4477 if (current_loops && b->loop_father->latch == b)
4478 return false;
4480 /* If we would end up moving B's instructions, make sure it doesn't fall
4481 through into the exit block, since we cannot recover from a fallthrough
4482 edge into the exit block occurring in the middle of a function. */
4483 if (NEXT_INSN (BB_END (a)) != BB_HEAD (b))
4485 edge e = find_fallthru_edge (b->succs);
4486 if (e && e->dest == EXIT_BLOCK_PTR)
4487 return false;
4490 /* There must be exactly one edge in between the blocks. */
4491 return (single_succ_p (a)
4492 && single_succ (a) == b
4493 && single_pred_p (b) == 1
4494 && a != b
4495 /* Must be simple edge. */
4496 && !(single_succ_edge (a)->flags & EDGE_COMPLEX)
4497 && a != ENTRY_BLOCK_PTR && b != EXIT_BLOCK_PTR
4498 /* If the jump insn has side effects, we can't kill the edge.
4499 When not optimizing, try_redirect_by_replacing_jump will
4500 not allow us to redirect an edge by replacing a table jump. */
4501 && (!JUMP_P (BB_END (a))
4502 || ((!optimize || reload_completed)
4503 ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
4506 /* Merge block A and B. The blocks must be mergeable. */
4508 static void
4509 cfg_layout_merge_blocks (basic_block a, basic_block b)
4511 bool forwarder_p = (b->flags & BB_FORWARDER_BLOCK) != 0;
4512 rtx insn;
4514 gcc_checking_assert (cfg_layout_can_merge_blocks_p (a, b));
4516 if (dump_file)
4517 fprintf (dump_file, "Merging block %d into block %d...\n", b->index,
4518 a->index);
4520 /* If there was a CODE_LABEL beginning B, delete it. */
4521 if (LABEL_P (BB_HEAD (b)))
4523 delete_insn (BB_HEAD (b));
4526 /* We should have fallthru edge in a, or we can do dummy redirection to get
4527 it cleaned up. */
4528 if (JUMP_P (BB_END (a)))
4529 try_redirect_by_replacing_jump (EDGE_SUCC (a, 0), b, true);
4530 gcc_assert (!JUMP_P (BB_END (a)));
4532 /* When not optimizing CFG and the edge is the only place in RTL which holds
4533 some unique locus, emit a nop with that locus in between. */
4534 if (!optimize)
4535 emit_nop_for_unique_locus_between (a, b);
4537 /* Move things from b->footer after a->footer. */
4538 if (BB_FOOTER (b))
4540 if (!BB_FOOTER (a))
4541 BB_FOOTER (a) = BB_FOOTER (b);
4542 else
4544 rtx last = BB_FOOTER (a);
4546 while (NEXT_INSN (last))
4547 last = NEXT_INSN (last);
4548 NEXT_INSN (last) = BB_FOOTER (b);
4549 PREV_INSN (BB_FOOTER (b)) = last;
4551 BB_FOOTER (b) = NULL;
4554 /* Move things from b->header before a->footer.
4555 Note that this may include dead tablejump data, but we don't clean
4556 those up until we go out of cfglayout mode. */
4557 if (BB_HEADER (b))
4559 if (! BB_FOOTER (a))
4560 BB_FOOTER (a) = BB_HEADER (b);
4561 else
4563 rtx last = BB_HEADER (b);
4565 while (NEXT_INSN (last))
4566 last = NEXT_INSN (last);
4567 NEXT_INSN (last) = BB_FOOTER (a);
4568 PREV_INSN (BB_FOOTER (a)) = last;
4569 BB_FOOTER (a) = BB_HEADER (b);
4571 BB_HEADER (b) = NULL;
4574 /* In the case basic blocks are not adjacent, move them around. */
4575 if (NEXT_INSN (BB_END (a)) != BB_HEAD (b))
4577 insn = unlink_insn_chain (BB_HEAD (b), BB_END (b));
4579 emit_insn_after_noloc (insn, BB_END (a), a);
4581 /* Otherwise just re-associate the instructions. */
4582 else
4584 insn = BB_HEAD (b);
4585 BB_END (a) = BB_END (b);
4588 /* emit_insn_after_noloc doesn't call df_insn_change_bb.
4589 We need to explicitly call. */
4590 update_bb_for_insn_chain (insn, BB_END (b), a);
4592 /* Skip possible DELETED_LABEL insn. */
4593 if (!NOTE_INSN_BASIC_BLOCK_P (insn))
4594 insn = NEXT_INSN (insn);
4595 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
4596 BB_HEAD (b) = BB_END (b) = NULL;
4597 delete_insn (insn);
4599 df_bb_delete (b->index);
4601 /* If B was a forwarder block, propagate the locus on the edge. */
4602 if (forwarder_p
4603 && LOCATION_LOCUS (EDGE_SUCC (b, 0)->goto_locus) == UNKNOWN_LOCATION)
4604 EDGE_SUCC (b, 0)->goto_locus = EDGE_SUCC (a, 0)->goto_locus;
4606 if (dump_file)
4607 fprintf (dump_file, "Merged blocks %d and %d.\n", a->index, b->index);
4610 /* Split edge E. */
4612 static basic_block
4613 cfg_layout_split_edge (edge e)
4615 basic_block new_bb =
4616 create_basic_block (e->src != ENTRY_BLOCK_PTR
4617 ? NEXT_INSN (BB_END (e->src)) : get_insns (),
4618 NULL_RTX, e->src);
4620 if (e->dest == EXIT_BLOCK_PTR)
4621 BB_COPY_PARTITION (new_bb, e->src);
4622 else
4623 BB_COPY_PARTITION (new_bb, e->dest);
4624 make_edge (new_bb, e->dest, EDGE_FALLTHRU);
4625 redirect_edge_and_branch_force (e, new_bb);
4627 return new_bb;
4630 /* Do postprocessing after making a forwarder block joined by edge FALLTHRU. */
4632 static void
4633 rtl_make_forwarder_block (edge fallthru ATTRIBUTE_UNUSED)
4637 /* Return true if BB contains only labels or non-executable
4638 instructions. */
4640 static bool
4641 rtl_block_empty_p (basic_block bb)
4643 rtx insn;
4645 if (bb == ENTRY_BLOCK_PTR || bb == EXIT_BLOCK_PTR)
4646 return true;
4648 FOR_BB_INSNS (bb, insn)
4649 if (NONDEBUG_INSN_P (insn) && !any_uncondjump_p (insn))
4650 return false;
4652 return true;
4655 /* Split a basic block if it ends with a conditional branch and if
4656 the other part of the block is not empty. */
4658 static basic_block
4659 rtl_split_block_before_cond_jump (basic_block bb)
4661 rtx insn;
4662 rtx split_point = NULL;
4663 rtx last = NULL;
4664 bool found_code = false;
4666 FOR_BB_INSNS (bb, insn)
4668 if (any_condjump_p (insn))
4669 split_point = last;
4670 else if (NONDEBUG_INSN_P (insn))
4671 found_code = true;
4672 last = insn;
4675 /* Did not find everything. */
4676 if (found_code && split_point)
4677 return split_block (bb, split_point)->dest;
4678 else
4679 return NULL;
4682 /* Return 1 if BB ends with a call, possibly followed by some
4683 instructions that must stay with the call, 0 otherwise. */
4685 static bool
4686 rtl_block_ends_with_call_p (basic_block bb)
4688 rtx insn = BB_END (bb);
4690 while (!CALL_P (insn)
4691 && insn != BB_HEAD (bb)
4692 && (keep_with_call_p (insn)
4693 || NOTE_P (insn)
4694 || DEBUG_INSN_P (insn)))
4695 insn = PREV_INSN (insn);
4696 return (CALL_P (insn));
4699 /* Return 1 if BB ends with a conditional branch, 0 otherwise. */
4701 static bool
4702 rtl_block_ends_with_condjump_p (const_basic_block bb)
4704 return any_condjump_p (BB_END (bb));
4707 /* Return true if we need to add fake edge to exit.
4708 Helper function for rtl_flow_call_edges_add. */
4710 static bool
4711 need_fake_edge_p (const_rtx insn)
4713 if (!INSN_P (insn))
4714 return false;
4716 if ((CALL_P (insn)
4717 && !SIBLING_CALL_P (insn)
4718 && !find_reg_note (insn, REG_NORETURN, NULL)
4719 && !(RTL_CONST_OR_PURE_CALL_P (insn))))
4720 return true;
4722 return ((GET_CODE (PATTERN (insn)) == ASM_OPERANDS
4723 && MEM_VOLATILE_P (PATTERN (insn)))
4724 || (GET_CODE (PATTERN (insn)) == PARALLEL
4725 && asm_noperands (insn) != -1
4726 && MEM_VOLATILE_P (XVECEXP (PATTERN (insn), 0, 0)))
4727 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
4730 /* Add fake edges to the function exit for any non constant and non noreturn
4731 calls, volatile inline assembly in the bitmap of blocks specified by
4732 BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
4733 that were split.
4735 The goal is to expose cases in which entering a basic block does not imply
4736 that all subsequent instructions must be executed. */
4738 static int
4739 rtl_flow_call_edges_add (sbitmap blocks)
4741 int i;
4742 int blocks_split = 0;
4743 int last_bb = last_basic_block;
4744 bool check_last_block = false;
4746 if (n_basic_blocks == NUM_FIXED_BLOCKS)
4747 return 0;
4749 if (! blocks)
4750 check_last_block = true;
4751 else
4752 check_last_block = bitmap_bit_p (blocks, EXIT_BLOCK_PTR->prev_bb->index);
4754 /* In the last basic block, before epilogue generation, there will be
4755 a fallthru edge to EXIT. Special care is required if the last insn
4756 of the last basic block is a call because make_edge folds duplicate
4757 edges, which would result in the fallthru edge also being marked
4758 fake, which would result in the fallthru edge being removed by
4759 remove_fake_edges, which would result in an invalid CFG.
4761 Moreover, we can't elide the outgoing fake edge, since the block
4762 profiler needs to take this into account in order to solve the minimal
4763 spanning tree in the case that the call doesn't return.
4765 Handle this by adding a dummy instruction in a new last basic block. */
4766 if (check_last_block)
4768 basic_block bb = EXIT_BLOCK_PTR->prev_bb;
4769 rtx insn = BB_END (bb);
4771 /* Back up past insns that must be kept in the same block as a call. */
4772 while (insn != BB_HEAD (bb)
4773 && keep_with_call_p (insn))
4774 insn = PREV_INSN (insn);
4776 if (need_fake_edge_p (insn))
4778 edge e;
4780 e = find_edge (bb, EXIT_BLOCK_PTR);
4781 if (e)
4783 insert_insn_on_edge (gen_use (const0_rtx), e);
4784 commit_edge_insertions ();
4789 /* Now add fake edges to the function exit for any non constant
4790 calls since there is no way that we can determine if they will
4791 return or not... */
4793 for (i = NUM_FIXED_BLOCKS; i < last_bb; i++)
4795 basic_block bb = BASIC_BLOCK (i);
4796 rtx insn;
4797 rtx prev_insn;
4799 if (!bb)
4800 continue;
4802 if (blocks && !bitmap_bit_p (blocks, i))
4803 continue;
4805 for (insn = BB_END (bb); ; insn = prev_insn)
4807 prev_insn = PREV_INSN (insn);
4808 if (need_fake_edge_p (insn))
4810 edge e;
4811 rtx split_at_insn = insn;
4813 /* Don't split the block between a call and an insn that should
4814 remain in the same block as the call. */
4815 if (CALL_P (insn))
4816 while (split_at_insn != BB_END (bb)
4817 && keep_with_call_p (NEXT_INSN (split_at_insn)))
4818 split_at_insn = NEXT_INSN (split_at_insn);
4820 /* The handling above of the final block before the epilogue
4821 should be enough to verify that there is no edge to the exit
4822 block in CFG already. Calling make_edge in such case would
4823 cause us to mark that edge as fake and remove it later. */
4825 #ifdef ENABLE_CHECKING
4826 if (split_at_insn == BB_END (bb))
4828 e = find_edge (bb, EXIT_BLOCK_PTR);
4829 gcc_assert (e == NULL);
4831 #endif
4833 /* Note that the following may create a new basic block
4834 and renumber the existing basic blocks. */
4835 if (split_at_insn != BB_END (bb))
4837 e = split_block (bb, split_at_insn);
4838 if (e)
4839 blocks_split++;
4842 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
4845 if (insn == BB_HEAD (bb))
4846 break;
4850 if (blocks_split)
4851 verify_flow_info ();
4853 return blocks_split;
4856 /* Add COMP_RTX as a condition at end of COND_BB. FIRST_HEAD is
4857 the conditional branch target, SECOND_HEAD should be the fall-thru
4858 there is no need to handle this here the loop versioning code handles
4859 this. the reason for SECON_HEAD is that it is needed for condition
4860 in trees, and this should be of the same type since it is a hook. */
4861 static void
4862 rtl_lv_add_condition_to_bb (basic_block first_head ,
4863 basic_block second_head ATTRIBUTE_UNUSED,
4864 basic_block cond_bb, void *comp_rtx)
4866 rtx label, seq, jump;
4867 rtx op0 = XEXP ((rtx)comp_rtx, 0);
4868 rtx op1 = XEXP ((rtx)comp_rtx, 1);
4869 enum rtx_code comp = GET_CODE ((rtx)comp_rtx);
4870 enum machine_mode mode;
4873 label = block_label (first_head);
4874 mode = GET_MODE (op0);
4875 if (mode == VOIDmode)
4876 mode = GET_MODE (op1);
4878 start_sequence ();
4879 op0 = force_operand (op0, NULL_RTX);
4880 op1 = force_operand (op1, NULL_RTX);
4881 do_compare_rtx_and_jump (op0, op1, comp, 0,
4882 mode, NULL_RTX, NULL_RTX, label, -1);
4883 jump = get_last_insn ();
4884 JUMP_LABEL (jump) = label;
4885 LABEL_NUSES (label)++;
4886 seq = get_insns ();
4887 end_sequence ();
4889 /* Add the new cond , in the new head. */
4890 emit_insn_after(seq, BB_END(cond_bb));
4894 /* Given a block B with unconditional branch at its end, get the
4895 store the return the branch edge and the fall-thru edge in
4896 BRANCH_EDGE and FALLTHRU_EDGE respectively. */
4897 static void
4898 rtl_extract_cond_bb_edges (basic_block b, edge *branch_edge,
4899 edge *fallthru_edge)
4901 edge e = EDGE_SUCC (b, 0);
4903 if (e->flags & EDGE_FALLTHRU)
4905 *fallthru_edge = e;
4906 *branch_edge = EDGE_SUCC (b, 1);
4908 else
4910 *branch_edge = e;
4911 *fallthru_edge = EDGE_SUCC (b, 1);
4915 void
4916 init_rtl_bb_info (basic_block bb)
4918 gcc_assert (!bb->il.x.rtl);
4919 bb->il.x.head_ = NULL;
4920 bb->il.x.rtl = ggc_alloc_cleared_rtl_bb_info ();
4923 /* Returns true if it is possible to remove edge E by redirecting
4924 it to the destination of the other edge from E->src. */
4926 static bool
4927 rtl_can_remove_branch_p (const_edge e)
4929 const_basic_block src = e->src;
4930 const_basic_block target = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest;
4931 const_rtx insn = BB_END (src), set;
4933 /* The conditions are taken from try_redirect_by_replacing_jump. */
4934 if (target == EXIT_BLOCK_PTR)
4935 return false;
4937 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
4938 return false;
4940 if (BB_PARTITION (src) != BB_PARTITION (target))
4941 return false;
4943 if (!onlyjump_p (insn)
4944 || tablejump_p (insn, NULL, NULL))
4945 return false;
4947 set = single_set (insn);
4948 if (!set || side_effects_p (set))
4949 return false;
4951 return true;
4954 static basic_block
4955 rtl_duplicate_bb (basic_block bb)
4957 bb = cfg_layout_duplicate_bb (bb);
4958 bb->aux = NULL;
4959 return bb;
4962 /* Do book-keeping of basic block BB for the profile consistency checker.
4963 If AFTER_PASS is 0, do pre-pass accounting, or if AFTER_PASS is 1
4964 then do post-pass accounting. Store the counting in RECORD. */
4965 static void
4966 rtl_account_profile_record (basic_block bb, int after_pass,
4967 struct profile_record *record)
4969 rtx insn;
4970 FOR_BB_INSNS (bb, insn)
4971 if (INSN_P (insn))
4973 record->size[after_pass]
4974 += insn_rtx_cost (PATTERN (insn), false);
4975 if (profile_status == PROFILE_READ)
4976 record->time[after_pass]
4977 += insn_rtx_cost (PATTERN (insn), true) * bb->count;
4978 else if (profile_status == PROFILE_GUESSED)
4979 record->time[after_pass]
4980 += insn_rtx_cost (PATTERN (insn), true) * bb->frequency;
4984 /* Implementation of CFG manipulation for linearized RTL. */
4985 struct cfg_hooks rtl_cfg_hooks = {
4986 "rtl",
4987 rtl_verify_flow_info,
4988 rtl_dump_bb,
4989 rtl_dump_bb_for_graph,
4990 rtl_create_basic_block,
4991 rtl_redirect_edge_and_branch,
4992 rtl_redirect_edge_and_branch_force,
4993 rtl_can_remove_branch_p,
4994 rtl_delete_block,
4995 rtl_split_block,
4996 rtl_move_block_after,
4997 rtl_can_merge_blocks, /* can_merge_blocks_p */
4998 rtl_merge_blocks,
4999 rtl_predict_edge,
5000 rtl_predicted_by_p,
5001 cfg_layout_can_duplicate_bb_p,
5002 rtl_duplicate_bb,
5003 rtl_split_edge,
5004 rtl_make_forwarder_block,
5005 rtl_tidy_fallthru_edge,
5006 rtl_force_nonfallthru,
5007 rtl_block_ends_with_call_p,
5008 rtl_block_ends_with_condjump_p,
5009 rtl_flow_call_edges_add,
5010 NULL, /* execute_on_growing_pred */
5011 NULL, /* execute_on_shrinking_pred */
5012 NULL, /* duplicate loop for trees */
5013 NULL, /* lv_add_condition_to_bb */
5014 NULL, /* lv_adjust_loop_header_phi*/
5015 NULL, /* extract_cond_bb_edges */
5016 NULL, /* flush_pending_stmts */
5017 rtl_block_empty_p, /* block_empty_p */
5018 rtl_split_block_before_cond_jump, /* split_block_before_cond_jump */
5019 rtl_account_profile_record,
5022 /* Implementation of CFG manipulation for cfg layout RTL, where
5023 basic block connected via fallthru edges does not have to be adjacent.
5024 This representation will hopefully become the default one in future
5025 version of the compiler. */
5027 struct cfg_hooks cfg_layout_rtl_cfg_hooks = {
5028 "cfglayout mode",
5029 rtl_verify_flow_info_1,
5030 rtl_dump_bb,
5031 rtl_dump_bb_for_graph,
5032 cfg_layout_create_basic_block,
5033 cfg_layout_redirect_edge_and_branch,
5034 cfg_layout_redirect_edge_and_branch_force,
5035 rtl_can_remove_branch_p,
5036 cfg_layout_delete_block,
5037 cfg_layout_split_block,
5038 rtl_move_block_after,
5039 cfg_layout_can_merge_blocks_p,
5040 cfg_layout_merge_blocks,
5041 rtl_predict_edge,
5042 rtl_predicted_by_p,
5043 cfg_layout_can_duplicate_bb_p,
5044 cfg_layout_duplicate_bb,
5045 cfg_layout_split_edge,
5046 rtl_make_forwarder_block,
5047 NULL, /* tidy_fallthru_edge */
5048 rtl_force_nonfallthru,
5049 rtl_block_ends_with_call_p,
5050 rtl_block_ends_with_condjump_p,
5051 rtl_flow_call_edges_add,
5052 NULL, /* execute_on_growing_pred */
5053 NULL, /* execute_on_shrinking_pred */
5054 duplicate_loop_to_header_edge, /* duplicate loop for trees */
5055 rtl_lv_add_condition_to_bb, /* lv_add_condition_to_bb */
5056 NULL, /* lv_adjust_loop_header_phi*/
5057 rtl_extract_cond_bb_edges, /* extract_cond_bb_edges */
5058 NULL, /* flush_pending_stmts */
5059 rtl_block_empty_p, /* block_empty_p */
5060 rtl_split_block_before_cond_jump, /* split_block_before_cond_jump */
5061 rtl_account_profile_record,
5064 #include "gt-cfgrtl.h"