* config/rl78/rl78.c (rl78_alloc_address_registers_macax): Verify
[official-gcc.git] / gcc / cfgrtl.c
blobd6733a2af31e781377b8376e8387d4f9c17ed484
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 = XINT (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 Don't do this if the predecessor ends in a jump other than
1964 unconditional simple jump. E.g. for asm goto that points all
1965 its labels at the fallthru basic block, we can't insert instructions
1966 before the asm goto, as the asm goto can have various of side effects,
1967 and can't emit instructions after the asm goto, as it must end
1968 the basic block. */
1969 else if ((e->flags & EDGE_ABNORMAL) == 0
1970 && single_succ_p (e->src)
1971 && e->src != ENTRY_BLOCK_PTR
1972 && (!JUMP_P (BB_END (e->src))
1973 || simplejump_p (BB_END (e->src))))
1975 bb = e->src;
1977 /* It is possible to have a non-simple jump here. Consider a target
1978 where some forms of unconditional jumps clobber a register. This
1979 happens on the fr30 for example.
1981 We know this block has a single successor, so we can just emit
1982 the queued insns before the jump. */
1983 if (JUMP_P (BB_END (bb)))
1984 before = BB_END (bb);
1985 else
1987 /* We'd better be fallthru, or we've lost track of what's what. */
1988 gcc_assert (e->flags & EDGE_FALLTHRU);
1990 after = BB_END (bb);
1994 /* Otherwise we must split the edge. */
1995 else
1997 bb = split_edge (e);
1999 /* If E crossed a partition boundary, we needed to make bb end in
2000 a region-crossing jump, even though it was originally fallthru. */
2001 if (JUMP_P (BB_END (bb)))
2002 before = BB_END (bb);
2003 else
2004 after = BB_END (bb);
2007 /* Now that we've found the spot, do the insertion. */
2008 if (before)
2010 emit_insn_before_noloc (insns, before, bb);
2011 last = prev_nonnote_insn (before);
2013 else
2014 last = emit_insn_after_noloc (insns, after, bb);
2016 if (returnjump_p (last))
2018 /* ??? Remove all outgoing edges from BB and add one for EXIT.
2019 This is not currently a problem because this only happens
2020 for the (single) epilogue, which already has a fallthru edge
2021 to EXIT. */
2023 e = single_succ_edge (bb);
2024 gcc_assert (e->dest == EXIT_BLOCK_PTR
2025 && single_succ_p (bb) && (e->flags & EDGE_FALLTHRU));
2027 e->flags &= ~EDGE_FALLTHRU;
2028 emit_barrier_after (last);
2030 if (before)
2031 delete_insn (before);
2033 else
2034 gcc_assert (!JUMP_P (last));
2037 /* Update the CFG for all queued instructions. */
2039 void
2040 commit_edge_insertions (void)
2042 basic_block bb;
2044 /* Optimization passes that invoke this routine can cause hot blocks
2045 previously reached by both hot and cold blocks to become dominated only
2046 by cold blocks. This will cause the verification below to fail,
2047 and lead to now cold code in the hot section. In some cases this
2048 may only be visible after newly unreachable blocks are deleted,
2049 which will be done by fixup_partitions. */
2050 fixup_partitions ();
2052 #ifdef ENABLE_CHECKING
2053 verify_flow_info ();
2054 #endif
2056 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
2058 edge e;
2059 edge_iterator ei;
2061 FOR_EACH_EDGE (e, ei, bb->succs)
2062 if (e->insns.r)
2063 commit_one_edge_insertion (e);
2068 /* Print out RTL-specific basic block information (live information
2069 at start and end with TDF_DETAILS). FLAGS are the TDF_* masks
2070 documented in dumpfile.h. */
2072 static void
2073 rtl_dump_bb (FILE *outf, basic_block bb, int indent, int flags)
2075 rtx insn;
2076 rtx last;
2077 char *s_indent;
2079 s_indent = (char *) alloca ((size_t) indent + 1);
2080 memset (s_indent, ' ', (size_t) indent);
2081 s_indent[indent] = '\0';
2083 if (df && (flags & TDF_DETAILS))
2085 df_dump_top (bb, outf);
2086 putc ('\n', outf);
2089 if (bb->index != ENTRY_BLOCK && bb->index != EXIT_BLOCK)
2090 for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb)); insn != last;
2091 insn = NEXT_INSN (insn))
2093 if (flags & TDF_DETAILS)
2094 df_dump_insn_top (insn, outf);
2095 if (! (flags & TDF_SLIM))
2096 print_rtl_single (outf, insn);
2097 else
2098 dump_insn_slim (outf, insn);
2099 if (flags & TDF_DETAILS)
2100 df_dump_insn_bottom (insn, outf);
2103 if (df && (flags & TDF_DETAILS))
2105 df_dump_bottom (bb, outf);
2106 putc ('\n', outf);
2111 /* Like dump_function_to_file, but for RTL. Print out dataflow information
2112 for the start of each basic block. FLAGS are the TDF_* masks documented
2113 in dumpfile.h. */
2115 void
2116 print_rtl_with_bb (FILE *outf, const_rtx rtx_first, int flags)
2118 const_rtx tmp_rtx;
2119 if (rtx_first == 0)
2120 fprintf (outf, "(nil)\n");
2121 else
2123 enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
2124 int max_uid = get_max_uid ();
2125 basic_block *start = XCNEWVEC (basic_block, max_uid);
2126 basic_block *end = XCNEWVEC (basic_block, max_uid);
2127 enum bb_state *in_bb_p = XCNEWVEC (enum bb_state, max_uid);
2128 basic_block bb;
2130 /* After freeing the CFG, we still have BLOCK_FOR_INSN set on most
2131 insns, but the CFG is not maintained so the basic block info
2132 is not reliable. Therefore it's omitted from the dumps. */
2133 if (! (cfun->curr_properties & PROP_cfg))
2134 flags &= ~TDF_BLOCKS;
2136 if (df)
2137 df_dump_start (outf);
2139 if (flags & TDF_BLOCKS)
2141 FOR_EACH_BB_REVERSE (bb)
2143 rtx x;
2145 start[INSN_UID (BB_HEAD (bb))] = bb;
2146 end[INSN_UID (BB_END (bb))] = bb;
2147 for (x = BB_HEAD (bb); x != NULL_RTX; x = NEXT_INSN (x))
2149 enum bb_state state = IN_MULTIPLE_BB;
2151 if (in_bb_p[INSN_UID (x)] == NOT_IN_BB)
2152 state = IN_ONE_BB;
2153 in_bb_p[INSN_UID (x)] = state;
2155 if (x == BB_END (bb))
2156 break;
2161 for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
2163 if (flags & TDF_BLOCKS)
2165 bb = start[INSN_UID (tmp_rtx)];
2166 if (bb != NULL)
2168 dump_bb_info (outf, bb, 0, dump_flags | TDF_COMMENT, true, false);
2169 if (df && (flags & TDF_DETAILS))
2170 df_dump_top (bb, outf);
2173 if (in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB
2174 && !NOTE_P (tmp_rtx)
2175 && !BARRIER_P (tmp_rtx))
2176 fprintf (outf, ";; Insn is not within a basic block\n");
2177 else if (in_bb_p[INSN_UID (tmp_rtx)] == IN_MULTIPLE_BB)
2178 fprintf (outf, ";; Insn is in multiple basic blocks\n");
2181 if (flags & TDF_DETAILS)
2182 df_dump_insn_top (tmp_rtx, outf);
2183 if (! (flags & TDF_SLIM))
2184 print_rtl_single (outf, tmp_rtx);
2185 else
2186 dump_insn_slim (outf, tmp_rtx);
2187 if (flags & TDF_DETAILS)
2188 df_dump_insn_bottom (tmp_rtx, outf);
2190 if (flags & TDF_BLOCKS)
2192 bb = end[INSN_UID (tmp_rtx)];
2193 if (bb != NULL)
2195 dump_bb_info (outf, bb, 0, dump_flags | TDF_COMMENT, false, true);
2196 if (df && (flags & TDF_DETAILS))
2197 df_dump_bottom (bb, outf);
2198 putc ('\n', outf);
2203 free (start);
2204 free (end);
2205 free (in_bb_p);
2209 /* Update the branch probability of BB if a REG_BR_PROB is present. */
2211 void
2212 update_br_prob_note (basic_block bb)
2214 rtx note;
2215 if (!JUMP_P (BB_END (bb)))
2216 return;
2217 note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX);
2218 if (!note || XINT (note, 0) == BRANCH_EDGE (bb)->probability)
2219 return;
2220 XINT (note, 0) = BRANCH_EDGE (bb)->probability;
2223 /* Get the last insn associated with block BB (that includes barriers and
2224 tablejumps after BB). */
2226 get_last_bb_insn (basic_block bb)
2228 rtx tmp;
2229 rtx end = BB_END (bb);
2231 /* Include any jump table following the basic block. */
2232 if (tablejump_p (end, NULL, &tmp))
2233 end = tmp;
2235 /* Include any barriers that may follow the basic block. */
2236 tmp = next_nonnote_insn_bb (end);
2237 while (tmp && BARRIER_P (tmp))
2239 end = tmp;
2240 tmp = next_nonnote_insn_bb (end);
2243 return end;
2246 /* Sanity check partition hotness to ensure that basic blocks in
2247   the cold partition don't dominate basic blocks in the hot partition.
2248 If FLAG_ONLY is true, report violations as errors. Otherwise
2249 re-mark the dominated blocks as cold, since this is run after
2250 cfg optimizations that may make hot blocks previously reached
2251 by both hot and cold blocks now only reachable along cold paths. */
2253 static vec<basic_block>
2254 find_partition_fixes (bool flag_only)
2256 basic_block bb;
2257 vec<basic_block> bbs_in_cold_partition = vNULL;
2258 vec<basic_block> bbs_to_fix = vNULL;
2260 /* Callers check this. */
2261 gcc_checking_assert (crtl->has_bb_partition);
2263 FOR_EACH_BB (bb)
2264 if ((BB_PARTITION (bb) == BB_COLD_PARTITION))
2265 bbs_in_cold_partition.safe_push (bb);
2267 if (bbs_in_cold_partition.is_empty ())
2268 return vNULL;
2270 bool dom_calculated_here = !dom_info_available_p (CDI_DOMINATORS);
2272 if (dom_calculated_here)
2273 calculate_dominance_info (CDI_DOMINATORS);
2275 while (! bbs_in_cold_partition.is_empty ())
2277 bb = bbs_in_cold_partition.pop ();
2278 /* Any blocks dominated by a block in the cold section
2279 must also be cold. */
2280 basic_block son;
2281 for (son = first_dom_son (CDI_DOMINATORS, bb);
2282 son;
2283 son = next_dom_son (CDI_DOMINATORS, son))
2285 /* If son is not yet cold, then mark it cold here and
2286 enqueue it for further processing. */
2287 if ((BB_PARTITION (son) != BB_COLD_PARTITION))
2289 if (flag_only)
2290 error ("non-cold basic block %d dominated "
2291 "by a block in the cold partition (%d)", son->index, bb->index);
2292 else
2293 BB_SET_PARTITION (son, BB_COLD_PARTITION);
2294 bbs_to_fix.safe_push (son);
2295 bbs_in_cold_partition.safe_push (son);
2300 if (dom_calculated_here)
2301 free_dominance_info (CDI_DOMINATORS);
2303 return bbs_to_fix;
2306 /* Perform cleanup on the hot/cold bb partitioning after optimization
2307 passes that modify the cfg. */
2309 void
2310 fixup_partitions (void)
2312 basic_block bb;
2314 if (!crtl->has_bb_partition)
2315 return;
2317 /* Delete any blocks that became unreachable and weren't
2318 already cleaned up, for example during edge forwarding
2319 and convert_jumps_to_returns. This will expose more
2320 opportunities for fixing the partition boundaries here.
2321 Also, the calculation of the dominance graph during verification
2322 will assert if there are unreachable nodes. */
2323 delete_unreachable_blocks ();
2325 /* If there are partitions, do a sanity check on them: A basic block in
2326   a cold partition cannot dominate a basic block in a hot partition.
2327 Fixup any that now violate this requirement, as a result of edge
2328 forwarding and unreachable block deletion.  */
2329 vec<basic_block> bbs_to_fix = find_partition_fixes (false);
2331 /* Do the partition fixup after all necessary blocks have been converted to
2332 cold, so that we only update the region crossings the minimum number of
2333 places, which can require forcing edges to be non fallthru. */
2334 while (! bbs_to_fix.is_empty ())
2336 bb = bbs_to_fix.pop ();
2337 fixup_new_cold_bb (bb);
2341 /* Verify, in the basic block chain, that there is at most one switch
2342 between hot/cold partitions. This condition will not be true until
2343 after reorder_basic_blocks is called. */
2345 static int
2346 verify_hot_cold_block_grouping (void)
2348 basic_block bb;
2349 int err = 0;
2350 bool switched_sections = false;
2351 int current_partition = BB_UNPARTITIONED;
2353 /* Even after bb reordering is complete, we go into cfglayout mode
2354 again (in compgoto). Ensure we don't call this before going back
2355 into linearized RTL when any layout fixes would have been committed. */
2356 if (!crtl->bb_reorder_complete
2357 || current_ir_type () != IR_RTL_CFGRTL)
2358 return err;
2360 FOR_EACH_BB (bb)
2362 if (current_partition != BB_UNPARTITIONED
2363 && BB_PARTITION (bb) != current_partition)
2365 if (switched_sections)
2367 error ("multiple hot/cold transitions found (bb %i)",
2368 bb->index);
2369 err = 1;
2371 else
2372 switched_sections = true;
2374 if (!crtl->has_bb_partition)
2375 error ("partition found but function partition flag not set");
2377 current_partition = BB_PARTITION (bb);
2380 return err;
2384 /* Perform several checks on the edges out of each block, such as
2385 the consistency of the branch probabilities, the correctness
2386 of hot/cold partition crossing edges, and the number of expected
2387 successor edges. Also verify that the dominance relationship
2388 between hot/cold blocks is sane. */
2390 static int
2391 rtl_verify_edges (void)
2393 int err = 0;
2394 basic_block bb;
2396 FOR_EACH_BB_REVERSE (bb)
2398 int n_fallthru = 0, n_branch = 0, n_abnormal_call = 0, n_sibcall = 0;
2399 int n_eh = 0, n_abnormal = 0;
2400 edge e, fallthru = NULL;
2401 edge_iterator ei;
2402 rtx note;
2403 bool has_crossing_edge = false;
2405 if (JUMP_P (BB_END (bb))
2406 && (note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX))
2407 && EDGE_COUNT (bb->succs) >= 2
2408 && any_condjump_p (BB_END (bb)))
2410 if (XINT (note, 0) != BRANCH_EDGE (bb)->probability
2411 && profile_status != PROFILE_ABSENT)
2413 error ("verify_flow_info: REG_BR_PROB does not match cfg %i %i",
2414 XINT (note, 0), BRANCH_EDGE (bb)->probability);
2415 err = 1;
2419 FOR_EACH_EDGE (e, ei, bb->succs)
2421 bool is_crossing;
2423 if (e->flags & EDGE_FALLTHRU)
2424 n_fallthru++, fallthru = e;
2426 is_crossing = (BB_PARTITION (e->src) != BB_PARTITION (e->dest)
2427 && e->src != ENTRY_BLOCK_PTR
2428 && e->dest != EXIT_BLOCK_PTR);
2429 has_crossing_edge |= is_crossing;
2430 if (e->flags & EDGE_CROSSING)
2432 if (!is_crossing)
2434 error ("EDGE_CROSSING incorrectly set across same section");
2435 err = 1;
2437 if (e->flags & EDGE_FALLTHRU)
2439 error ("fallthru edge crosses section boundary in bb %i",
2440 e->src->index);
2441 err = 1;
2443 if (e->flags & EDGE_EH)
2445 error ("EH edge crosses section boundary in bb %i",
2446 e->src->index);
2447 err = 1;
2449 if (JUMP_P (BB_END (bb))
2450 && !find_reg_note (BB_END (bb), REG_CROSSING_JUMP, NULL_RTX))
2452 error ("No region crossing jump at section boundary in bb %i",
2453 bb->index);
2454 err = 1;
2457 else if (is_crossing)
2459 error ("EDGE_CROSSING missing across section boundary");
2460 err = 1;
2463 if ((e->flags & ~(EDGE_DFS_BACK
2464 | EDGE_CAN_FALLTHRU
2465 | EDGE_IRREDUCIBLE_LOOP
2466 | EDGE_LOOP_EXIT
2467 | EDGE_CROSSING
2468 | EDGE_PRESERVE)) == 0)
2469 n_branch++;
2471 if (e->flags & EDGE_ABNORMAL_CALL)
2472 n_abnormal_call++;
2474 if (e->flags & EDGE_SIBCALL)
2475 n_sibcall++;
2477 if (e->flags & EDGE_EH)
2478 n_eh++;
2480 if (e->flags & EDGE_ABNORMAL)
2481 n_abnormal++;
2484 if (!has_crossing_edge
2485 && find_reg_note (BB_END (bb), REG_CROSSING_JUMP, NULL_RTX))
2487 print_rtl_with_bb (stderr, get_insns (), TDF_RTL | TDF_BLOCKS | TDF_DETAILS);
2488 error ("Region crossing jump across same section in bb %i",
2489 bb->index);
2490 err = 1;
2493 if (n_eh && !find_reg_note (BB_END (bb), REG_EH_REGION, NULL_RTX))
2495 error ("missing REG_EH_REGION note at the end of bb %i", bb->index);
2496 err = 1;
2498 if (n_eh > 1)
2500 error ("too many exception handling edges in bb %i", bb->index);
2501 err = 1;
2503 if (n_branch
2504 && (!JUMP_P (BB_END (bb))
2505 || (n_branch > 1 && (any_uncondjump_p (BB_END (bb))
2506 || any_condjump_p (BB_END (bb))))))
2508 error ("too many outgoing branch edges from bb %i", bb->index);
2509 err = 1;
2511 if (n_fallthru && any_uncondjump_p (BB_END (bb)))
2513 error ("fallthru edge after unconditional jump in bb %i", bb->index);
2514 err = 1;
2516 if (n_branch != 1 && any_uncondjump_p (BB_END (bb)))
2518 error ("wrong number of branch edges after unconditional jump"
2519 " in bb %i", bb->index);
2520 err = 1;
2522 if (n_branch != 1 && any_condjump_p (BB_END (bb))
2523 && JUMP_LABEL (BB_END (bb)) != BB_HEAD (fallthru->dest))
2525 error ("wrong amount of branch edges after conditional jump"
2526 " in bb %i", bb->index);
2527 err = 1;
2529 if (n_abnormal_call && !CALL_P (BB_END (bb)))
2531 error ("abnormal call edges for non-call insn in bb %i", bb->index);
2532 err = 1;
2534 if (n_sibcall && !CALL_P (BB_END (bb)))
2536 error ("sibcall edges for non-call insn in bb %i", bb->index);
2537 err = 1;
2539 if (n_abnormal > n_eh
2540 && !(CALL_P (BB_END (bb))
2541 && n_abnormal == n_abnormal_call + n_sibcall)
2542 && (!JUMP_P (BB_END (bb))
2543 || any_condjump_p (BB_END (bb))
2544 || any_uncondjump_p (BB_END (bb))))
2546 error ("abnormal edges for no purpose in bb %i", bb->index);
2547 err = 1;
2551 /* If there are partitions, do a sanity check on them: A basic block in
2552   a cold partition cannot dominate a basic block in a hot partition.  */
2553 if (crtl->has_bb_partition && !err)
2555 vec<basic_block> bbs_to_fix = find_partition_fixes (true);
2556 err = !bbs_to_fix.is_empty ();
2559 /* Clean up. */
2560 return err;
2563 /* Checks on the instructions within blocks. Currently checks that each
2564 block starts with a basic block note, and that basic block notes and
2565 control flow jumps are not found in the middle of the block. */
2567 static int
2568 rtl_verify_bb_insns (void)
2570 rtx x;
2571 int err = 0;
2572 basic_block bb;
2574 FOR_EACH_BB_REVERSE (bb)
2576 /* Now check the header of basic
2577 block. It ought to contain optional CODE_LABEL followed
2578 by NOTE_BASIC_BLOCK. */
2579 x = BB_HEAD (bb);
2580 if (LABEL_P (x))
2582 if (BB_END (bb) == x)
2584 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
2585 bb->index);
2586 err = 1;
2589 x = NEXT_INSN (x);
2592 if (!NOTE_INSN_BASIC_BLOCK_P (x) || NOTE_BASIC_BLOCK (x) != bb)
2594 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
2595 bb->index);
2596 err = 1;
2599 if (BB_END (bb) == x)
2600 /* Do checks for empty blocks here. */
2602 else
2603 for (x = NEXT_INSN (x); x; x = NEXT_INSN (x))
2605 if (NOTE_INSN_BASIC_BLOCK_P (x))
2607 error ("NOTE_INSN_BASIC_BLOCK %d in middle of basic block %d",
2608 INSN_UID (x), bb->index);
2609 err = 1;
2612 if (x == BB_END (bb))
2613 break;
2615 if (control_flow_insn_p (x))
2617 error ("in basic block %d:", bb->index);
2618 fatal_insn ("flow control insn inside a basic block", x);
2623 /* Clean up. */
2624 return err;
2627 /* Verify that block pointers for instructions in basic blocks, headers and
2628 footers are set appropriately. */
2630 static int
2631 rtl_verify_bb_pointers (void)
2633 int err = 0;
2634 basic_block bb;
2636 /* Check the general integrity of the basic blocks. */
2637 FOR_EACH_BB_REVERSE (bb)
2639 rtx insn;
2641 if (!(bb->flags & BB_RTL))
2643 error ("BB_RTL flag not set for block %d", bb->index);
2644 err = 1;
2647 FOR_BB_INSNS (bb, insn)
2648 if (BLOCK_FOR_INSN (insn) != bb)
2650 error ("insn %d basic block pointer is %d, should be %d",
2651 INSN_UID (insn),
2652 BLOCK_FOR_INSN (insn) ? BLOCK_FOR_INSN (insn)->index : 0,
2653 bb->index);
2654 err = 1;
2657 for (insn = BB_HEADER (bb); insn; insn = NEXT_INSN (insn))
2658 if (!BARRIER_P (insn)
2659 && BLOCK_FOR_INSN (insn) != NULL)
2661 error ("insn %d in header of bb %d has non-NULL basic block",
2662 INSN_UID (insn), bb->index);
2663 err = 1;
2665 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2666 if (!BARRIER_P (insn)
2667 && BLOCK_FOR_INSN (insn) != NULL)
2669 error ("insn %d in footer of bb %d has non-NULL basic block",
2670 INSN_UID (insn), bb->index);
2671 err = 1;
2675 /* Clean up. */
2676 return err;
2679 /* Verify the CFG and RTL consistency common for both underlying RTL and
2680 cfglayout RTL.
2682 Currently it does following checks:
2684 - overlapping of basic blocks
2685 - insns with wrong BLOCK_FOR_INSN pointers
2686 - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note)
2687 - tails of basic blocks (ensure that boundary is necessary)
2688 - scans body of the basic block for JUMP_INSN, CODE_LABEL
2689 and NOTE_INSN_BASIC_BLOCK
2690 - verify that no fall_thru edge crosses hot/cold partition boundaries
2691 - verify that there are no pending RTL branch predictions
2692 - verify that hot blocks are not dominated by cold blocks
2694 In future it can be extended check a lot of other stuff as well
2695 (reachability of basic blocks, life information, etc. etc.). */
2697 static int
2698 rtl_verify_flow_info_1 (void)
2700 int err = 0;
2702 err |= rtl_verify_bb_pointers ();
2704 err |= rtl_verify_bb_insns ();
2706 err |= rtl_verify_edges ();
2708 return err;
2711 /* Walk the instruction chain and verify that bb head/end pointers
2712 are correct, and that instructions are in exactly one bb and have
2713 correct block pointers. */
2715 static int
2716 rtl_verify_bb_insn_chain (void)
2718 basic_block bb;
2719 int err = 0;
2720 rtx x;
2721 rtx last_head = get_last_insn ();
2722 basic_block *bb_info;
2723 const int max_uid = get_max_uid ();
2725 bb_info = XCNEWVEC (basic_block, max_uid);
2727 FOR_EACH_BB_REVERSE (bb)
2729 rtx head = BB_HEAD (bb);
2730 rtx end = BB_END (bb);
2732 for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
2734 /* Verify the end of the basic block is in the INSN chain. */
2735 if (x == end)
2736 break;
2738 /* And that the code outside of basic blocks has NULL bb field. */
2739 if (!BARRIER_P (x)
2740 && BLOCK_FOR_INSN (x) != NULL)
2742 error ("insn %d outside of basic blocks has non-NULL bb field",
2743 INSN_UID (x));
2744 err = 1;
2748 if (!x)
2750 error ("end insn %d for block %d not found in the insn stream",
2751 INSN_UID (end), bb->index);
2752 err = 1;
2755 /* Work backwards from the end to the head of the basic block
2756 to verify the head is in the RTL chain. */
2757 for (; x != NULL_RTX; x = PREV_INSN (x))
2759 /* While walking over the insn chain, verify insns appear
2760 in only one basic block. */
2761 if (bb_info[INSN_UID (x)] != NULL)
2763 error ("insn %d is in multiple basic blocks (%d and %d)",
2764 INSN_UID (x), bb->index, bb_info[INSN_UID (x)]->index);
2765 err = 1;
2768 bb_info[INSN_UID (x)] = bb;
2770 if (x == head)
2771 break;
2773 if (!x)
2775 error ("head insn %d for block %d not found in the insn stream",
2776 INSN_UID (head), bb->index);
2777 err = 1;
2780 last_head = PREV_INSN (x);
2783 for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
2785 /* Check that the code before the first basic block has NULL
2786 bb field. */
2787 if (!BARRIER_P (x)
2788 && BLOCK_FOR_INSN (x) != NULL)
2790 error ("insn %d outside of basic blocks has non-NULL bb field",
2791 INSN_UID (x));
2792 err = 1;
2795 free (bb_info);
2797 return err;
2800 /* Verify that fallthru edges point to adjacent blocks in layout order and
2801 that barriers exist after non-fallthru blocks. */
2803 static int
2804 rtl_verify_fallthru (void)
2806 basic_block bb;
2807 int err = 0;
2809 FOR_EACH_BB_REVERSE (bb)
2811 edge e;
2813 e = find_fallthru_edge (bb->succs);
2814 if (!e)
2816 rtx insn;
2818 /* Ensure existence of barrier in BB with no fallthru edges. */
2819 for (insn = NEXT_INSN (BB_END (bb)); ; insn = NEXT_INSN (insn))
2821 if (!insn || NOTE_INSN_BASIC_BLOCK_P (insn))
2823 error ("missing barrier after block %i", bb->index);
2824 err = 1;
2825 break;
2827 if (BARRIER_P (insn))
2828 break;
2831 else if (e->src != ENTRY_BLOCK_PTR
2832 && e->dest != EXIT_BLOCK_PTR)
2834 rtx insn;
2836 if (e->src->next_bb != e->dest)
2838 error
2839 ("verify_flow_info: Incorrect blocks for fallthru %i->%i",
2840 e->src->index, e->dest->index);
2841 err = 1;
2843 else
2844 for (insn = NEXT_INSN (BB_END (e->src)); insn != BB_HEAD (e->dest);
2845 insn = NEXT_INSN (insn))
2846 if (BARRIER_P (insn) || INSN_P (insn))
2848 error ("verify_flow_info: Incorrect fallthru %i->%i",
2849 e->src->index, e->dest->index);
2850 fatal_insn ("wrong insn in the fallthru edge", insn);
2851 err = 1;
2856 return err;
2859 /* Verify that blocks are laid out in consecutive order. While walking the
2860 instructions, verify that all expected instructions are inside the basic
2861 blocks, and that all returns are followed by barriers. */
2863 static int
2864 rtl_verify_bb_layout (void)
2866 basic_block bb;
2867 int err = 0;
2868 rtx x;
2869 int num_bb_notes;
2870 const rtx rtx_first = get_insns ();
2871 basic_block last_bb_seen = ENTRY_BLOCK_PTR, curr_bb = NULL;
2873 num_bb_notes = 0;
2874 last_bb_seen = ENTRY_BLOCK_PTR;
2876 for (x = rtx_first; x; x = NEXT_INSN (x))
2878 if (NOTE_INSN_BASIC_BLOCK_P (x))
2880 bb = NOTE_BASIC_BLOCK (x);
2882 num_bb_notes++;
2883 if (bb != last_bb_seen->next_bb)
2884 internal_error ("basic blocks not laid down consecutively");
2886 curr_bb = last_bb_seen = bb;
2889 if (!curr_bb)
2891 switch (GET_CODE (x))
2893 case BARRIER:
2894 case NOTE:
2895 break;
2897 case CODE_LABEL:
2898 /* An ADDR_VEC is placed outside any basic block. */
2899 if (NEXT_INSN (x)
2900 && JUMP_TABLE_DATA_P (NEXT_INSN (x)))
2901 x = NEXT_INSN (x);
2903 /* But in any case, non-deletable labels can appear anywhere. */
2904 break;
2906 default:
2907 fatal_insn ("insn outside basic block", x);
2911 if (JUMP_P (x)
2912 && returnjump_p (x) && ! condjump_p (x)
2913 && ! (next_nonnote_insn (x) && BARRIER_P (next_nonnote_insn (x))))
2914 fatal_insn ("return not followed by barrier", x);
2916 if (curr_bb && x == BB_END (curr_bb))
2917 curr_bb = NULL;
2920 if (num_bb_notes != n_basic_blocks - NUM_FIXED_BLOCKS)
2921 internal_error
2922 ("number of bb notes in insn chain (%d) != n_basic_blocks (%d)",
2923 num_bb_notes, n_basic_blocks);
2925 return err;
2928 /* Verify the CFG and RTL consistency common for both underlying RTL and
2929 cfglayout RTL, plus consistency checks specific to linearized RTL mode.
2931 Currently it does following checks:
2932 - all checks of rtl_verify_flow_info_1
2933 - test head/end pointers
2934 - check that blocks are laid out in consecutive order
2935 - check that all insns are in the basic blocks
2936 (except the switch handling code, barriers and notes)
2937 - check that all returns are followed by barriers
2938 - check that all fallthru edge points to the adjacent blocks
2939 - verify that there is a single hot/cold partition boundary after bbro */
2941 static int
2942 rtl_verify_flow_info (void)
2944 int err = 0;
2946 err |= rtl_verify_flow_info_1 ();
2948 err |= rtl_verify_bb_insn_chain ();
2950 err |= rtl_verify_fallthru ();
2952 err |= rtl_verify_bb_layout ();
2954 err |= verify_hot_cold_block_grouping ();
2956 return err;
2959 /* Assume that the preceding pass has possibly eliminated jump instructions
2960 or converted the unconditional jumps. Eliminate the edges from CFG.
2961 Return true if any edges are eliminated. */
2963 bool
2964 purge_dead_edges (basic_block bb)
2966 edge e;
2967 rtx insn = BB_END (bb), note;
2968 bool purged = false;
2969 bool found;
2970 edge_iterator ei;
2972 if (DEBUG_INSN_P (insn) && insn != BB_HEAD (bb))
2974 insn = PREV_INSN (insn);
2975 while ((DEBUG_INSN_P (insn) || NOTE_P (insn)) && insn != BB_HEAD (bb));
2977 /* If this instruction cannot trap, remove REG_EH_REGION notes. */
2978 if (NONJUMP_INSN_P (insn)
2979 && (note = find_reg_note (insn, REG_EH_REGION, NULL)))
2981 rtx eqnote;
2983 if (! may_trap_p (PATTERN (insn))
2984 || ((eqnote = find_reg_equal_equiv_note (insn))
2985 && ! may_trap_p (XEXP (eqnote, 0))))
2986 remove_note (insn, note);
2989 /* Cleanup abnormal edges caused by exceptions or non-local gotos. */
2990 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2992 bool remove = false;
2994 /* There are three types of edges we need to handle correctly here: EH
2995 edges, abnormal call EH edges, and abnormal call non-EH edges. The
2996 latter can appear when nonlocal gotos are used. */
2997 if (e->flags & EDGE_ABNORMAL_CALL)
2999 if (!CALL_P (insn))
3000 remove = true;
3001 else if (can_nonlocal_goto (insn))
3003 else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
3005 else if (flag_tm && find_reg_note (insn, REG_TM, NULL))
3007 else
3008 remove = true;
3010 else if (e->flags & EDGE_EH)
3011 remove = !can_throw_internal (insn);
3013 if (remove)
3015 remove_edge (e);
3016 df_set_bb_dirty (bb);
3017 purged = true;
3019 else
3020 ei_next (&ei);
3023 if (JUMP_P (insn))
3025 rtx note;
3026 edge b,f;
3027 edge_iterator ei;
3029 /* We do care only about conditional jumps and simplejumps. */
3030 if (!any_condjump_p (insn)
3031 && !returnjump_p (insn)
3032 && !simplejump_p (insn))
3033 return purged;
3035 /* Branch probability/prediction notes are defined only for
3036 condjumps. We've possibly turned condjump into simplejump. */
3037 if (simplejump_p (insn))
3039 note = find_reg_note (insn, REG_BR_PROB, NULL);
3040 if (note)
3041 remove_note (insn, note);
3042 while ((note = find_reg_note (insn, REG_BR_PRED, NULL)))
3043 remove_note (insn, note);
3046 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3048 /* Avoid abnormal flags to leak from computed jumps turned
3049 into simplejumps. */
3051 e->flags &= ~EDGE_ABNORMAL;
3053 /* See if this edge is one we should keep. */
3054 if ((e->flags & EDGE_FALLTHRU) && any_condjump_p (insn))
3055 /* A conditional jump can fall through into the next
3056 block, so we should keep the edge. */
3058 ei_next (&ei);
3059 continue;
3061 else if (e->dest != EXIT_BLOCK_PTR
3062 && BB_HEAD (e->dest) == JUMP_LABEL (insn))
3063 /* If the destination block is the target of the jump,
3064 keep the edge. */
3066 ei_next (&ei);
3067 continue;
3069 else if (e->dest == EXIT_BLOCK_PTR && returnjump_p (insn))
3070 /* If the destination block is the exit block, and this
3071 instruction is a return, then keep the edge. */
3073 ei_next (&ei);
3074 continue;
3076 else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
3077 /* Keep the edges that correspond to exceptions thrown by
3078 this instruction and rematerialize the EDGE_ABNORMAL
3079 flag we just cleared above. */
3081 e->flags |= EDGE_ABNORMAL;
3082 ei_next (&ei);
3083 continue;
3086 /* We do not need this edge. */
3087 df_set_bb_dirty (bb);
3088 purged = true;
3089 remove_edge (e);
3092 if (EDGE_COUNT (bb->succs) == 0 || !purged)
3093 return purged;
3095 if (dump_file)
3096 fprintf (dump_file, "Purged edges from bb %i\n", bb->index);
3098 if (!optimize)
3099 return purged;
3101 /* Redistribute probabilities. */
3102 if (single_succ_p (bb))
3104 single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
3105 single_succ_edge (bb)->count = bb->count;
3107 else
3109 note = find_reg_note (insn, REG_BR_PROB, NULL);
3110 if (!note)
3111 return purged;
3113 b = BRANCH_EDGE (bb);
3114 f = FALLTHRU_EDGE (bb);
3115 b->probability = XINT (note, 0);
3116 f->probability = REG_BR_PROB_BASE - b->probability;
3117 /* Update these to use GCOV_COMPUTE_SCALE. */
3118 b->count = bb->count * b->probability / REG_BR_PROB_BASE;
3119 f->count = bb->count * f->probability / REG_BR_PROB_BASE;
3122 return purged;
3124 else if (CALL_P (insn) && SIBLING_CALL_P (insn))
3126 /* First, there should not be any EH or ABCALL edges resulting
3127 from non-local gotos and the like. If there were, we shouldn't
3128 have created the sibcall in the first place. Second, there
3129 should of course never have been a fallthru edge. */
3130 gcc_assert (single_succ_p (bb));
3131 gcc_assert (single_succ_edge (bb)->flags
3132 == (EDGE_SIBCALL | EDGE_ABNORMAL));
3134 return 0;
3137 /* If we don't see a jump insn, we don't know exactly why the block would
3138 have been broken at this point. Look for a simple, non-fallthru edge,
3139 as these are only created by conditional branches. If we find such an
3140 edge we know that there used to be a jump here and can then safely
3141 remove all non-fallthru edges. */
3142 found = false;
3143 FOR_EACH_EDGE (e, ei, bb->succs)
3144 if (! (e->flags & (EDGE_COMPLEX | EDGE_FALLTHRU)))
3146 found = true;
3147 break;
3150 if (!found)
3151 return purged;
3153 /* Remove all but the fake and fallthru edges. The fake edge may be
3154 the only successor for this block in the case of noreturn
3155 calls. */
3156 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3158 if (!(e->flags & (EDGE_FALLTHRU | EDGE_FAKE)))
3160 df_set_bb_dirty (bb);
3161 remove_edge (e);
3162 purged = true;
3164 else
3165 ei_next (&ei);
3168 gcc_assert (single_succ_p (bb));
3170 single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
3171 single_succ_edge (bb)->count = bb->count;
3173 if (dump_file)
3174 fprintf (dump_file, "Purged non-fallthru edges from bb %i\n",
3175 bb->index);
3176 return purged;
3179 /* Search all basic blocks for potentially dead edges and purge them. Return
3180 true if some edge has been eliminated. */
3182 bool
3183 purge_all_dead_edges (void)
3185 int purged = false;
3186 basic_block bb;
3188 FOR_EACH_BB (bb)
3190 bool purged_here = purge_dead_edges (bb);
3192 purged |= purged_here;
3195 return purged;
3198 /* This is used by a few passes that emit some instructions after abnormal
3199 calls, moving the basic block's end, while they in fact do want to emit
3200 them on the fallthru edge. Look for abnormal call edges, find backward
3201 the call in the block and insert the instructions on the edge instead.
3203 Similarly, handle instructions throwing exceptions internally.
3205 Return true when instructions have been found and inserted on edges. */
3207 bool
3208 fixup_abnormal_edges (void)
3210 bool inserted = false;
3211 basic_block bb;
3213 FOR_EACH_BB (bb)
3215 edge e;
3216 edge_iterator ei;
3218 /* Look for cases we are interested in - calls or instructions causing
3219 exceptions. */
3220 FOR_EACH_EDGE (e, ei, bb->succs)
3221 if ((e->flags & EDGE_ABNORMAL_CALL)
3222 || ((e->flags & (EDGE_ABNORMAL | EDGE_EH))
3223 == (EDGE_ABNORMAL | EDGE_EH)))
3224 break;
3226 if (e && !CALL_P (BB_END (bb)) && !can_throw_internal (BB_END (bb)))
3228 rtx insn;
3230 /* Get past the new insns generated. Allow notes, as the insns
3231 may be already deleted. */
3232 insn = BB_END (bb);
3233 while ((NONJUMP_INSN_P (insn) || NOTE_P (insn))
3234 && !can_throw_internal (insn)
3235 && insn != BB_HEAD (bb))
3236 insn = PREV_INSN (insn);
3238 if (CALL_P (insn) || can_throw_internal (insn))
3240 rtx stop, next;
3242 e = find_fallthru_edge (bb->succs);
3244 stop = NEXT_INSN (BB_END (bb));
3245 BB_END (bb) = insn;
3247 for (insn = NEXT_INSN (insn); insn != stop; insn = next)
3249 next = NEXT_INSN (insn);
3250 if (INSN_P (insn))
3252 delete_insn (insn);
3254 /* Sometimes there's still the return value USE.
3255 If it's placed after a trapping call (i.e. that
3256 call is the last insn anyway), we have no fallthru
3257 edge. Simply delete this use and don't try to insert
3258 on the non-existent edge. */
3259 if (GET_CODE (PATTERN (insn)) != USE)
3261 /* We're not deleting it, we're moving it. */
3262 INSN_DELETED_P (insn) = 0;
3263 PREV_INSN (insn) = NULL_RTX;
3264 NEXT_INSN (insn) = NULL_RTX;
3266 insert_insn_on_edge (insn, e);
3267 inserted = true;
3270 else if (!BARRIER_P (insn))
3271 set_block_for_insn (insn, NULL);
3275 /* It may be that we don't find any trapping insn. In this
3276 case we discovered quite late that the insn that had been
3277 marked as can_throw_internal in fact couldn't trap at all.
3278 So we should in fact delete the EH edges out of the block. */
3279 else
3280 purge_dead_edges (bb);
3284 return inserted;
3287 /* Cut the insns from FIRST to LAST out of the insns stream. */
3290 unlink_insn_chain (rtx first, rtx last)
3292 rtx prevfirst = PREV_INSN (first);
3293 rtx nextlast = NEXT_INSN (last);
3295 PREV_INSN (first) = NULL;
3296 NEXT_INSN (last) = NULL;
3297 if (prevfirst)
3298 NEXT_INSN (prevfirst) = nextlast;
3299 if (nextlast)
3300 PREV_INSN (nextlast) = prevfirst;
3301 else
3302 set_last_insn (prevfirst);
3303 if (!prevfirst)
3304 set_first_insn (nextlast);
3305 return first;
3308 /* Skip over inter-block insns occurring after BB which are typically
3309 associated with BB (e.g., barriers). If there are any such insns,
3310 we return the last one. Otherwise, we return the end of BB. */
3312 static rtx
3313 skip_insns_after_block (basic_block bb)
3315 rtx insn, last_insn, next_head, prev;
3317 next_head = NULL_RTX;
3318 if (bb->next_bb != EXIT_BLOCK_PTR)
3319 next_head = BB_HEAD (bb->next_bb);
3321 for (last_insn = insn = BB_END (bb); (insn = NEXT_INSN (insn)) != 0; )
3323 if (insn == next_head)
3324 break;
3326 switch (GET_CODE (insn))
3328 case BARRIER:
3329 last_insn = insn;
3330 continue;
3332 case NOTE:
3333 switch (NOTE_KIND (insn))
3335 case NOTE_INSN_BLOCK_END:
3336 gcc_unreachable ();
3337 continue;
3338 default:
3339 continue;
3340 break;
3342 break;
3344 case CODE_LABEL:
3345 if (NEXT_INSN (insn)
3346 && JUMP_TABLE_DATA_P (NEXT_INSN (insn)))
3348 insn = NEXT_INSN (insn);
3349 last_insn = insn;
3350 continue;
3352 break;
3354 default:
3355 break;
3358 break;
3361 /* It is possible to hit contradictory sequence. For instance:
3363 jump_insn
3364 NOTE_INSN_BLOCK_BEG
3365 barrier
3367 Where barrier belongs to jump_insn, but the note does not. This can be
3368 created by removing the basic block originally following
3369 NOTE_INSN_BLOCK_BEG. In such case reorder the notes. */
3371 for (insn = last_insn; insn != BB_END (bb); insn = prev)
3373 prev = PREV_INSN (insn);
3374 if (NOTE_P (insn))
3375 switch (NOTE_KIND (insn))
3377 case NOTE_INSN_BLOCK_END:
3378 gcc_unreachable ();
3379 break;
3380 case NOTE_INSN_DELETED:
3381 case NOTE_INSN_DELETED_LABEL:
3382 case NOTE_INSN_DELETED_DEBUG_LABEL:
3383 continue;
3384 default:
3385 reorder_insns (insn, insn, last_insn);
3389 return last_insn;
3392 /* Locate or create a label for a given basic block. */
3394 static rtx
3395 label_for_bb (basic_block bb)
3397 rtx label = BB_HEAD (bb);
3399 if (!LABEL_P (label))
3401 if (dump_file)
3402 fprintf (dump_file, "Emitting label for block %d\n", bb->index);
3404 label = block_label (bb);
3407 return label;
3410 /* Locate the effective beginning and end of the insn chain for each
3411 block, as defined by skip_insns_after_block above. */
3413 static void
3414 record_effective_endpoints (void)
3416 rtx next_insn;
3417 basic_block bb;
3418 rtx insn;
3420 for (insn = get_insns ();
3421 insn
3422 && NOTE_P (insn)
3423 && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK;
3424 insn = NEXT_INSN (insn))
3425 continue;
3426 /* No basic blocks at all? */
3427 gcc_assert (insn);
3429 if (PREV_INSN (insn))
3430 cfg_layout_function_header =
3431 unlink_insn_chain (get_insns (), PREV_INSN (insn));
3432 else
3433 cfg_layout_function_header = NULL_RTX;
3435 next_insn = get_insns ();
3436 FOR_EACH_BB (bb)
3438 rtx end;
3440 if (PREV_INSN (BB_HEAD (bb)) && next_insn != BB_HEAD (bb))
3441 BB_HEADER (bb) = unlink_insn_chain (next_insn,
3442 PREV_INSN (BB_HEAD (bb)));
3443 end = skip_insns_after_block (bb);
3444 if (NEXT_INSN (BB_END (bb)) && BB_END (bb) != end)
3445 BB_FOOTER (bb) = unlink_insn_chain (NEXT_INSN (BB_END (bb)), end);
3446 next_insn = NEXT_INSN (BB_END (bb));
3449 cfg_layout_function_footer = next_insn;
3450 if (cfg_layout_function_footer)
3451 cfg_layout_function_footer = unlink_insn_chain (cfg_layout_function_footer, get_last_insn ());
3454 static unsigned int
3455 into_cfg_layout_mode (void)
3457 cfg_layout_initialize (0);
3458 return 0;
3461 static unsigned int
3462 outof_cfg_layout_mode (void)
3464 basic_block bb;
3466 FOR_EACH_BB (bb)
3467 if (bb->next_bb != EXIT_BLOCK_PTR)
3468 bb->aux = bb->next_bb;
3470 cfg_layout_finalize ();
3472 return 0;
3475 namespace {
3477 const pass_data pass_data_into_cfg_layout_mode =
3479 RTL_PASS, /* type */
3480 "into_cfglayout", /* name */
3481 OPTGROUP_NONE, /* optinfo_flags */
3482 false, /* has_gate */
3483 true, /* has_execute */
3484 TV_CFG, /* tv_id */
3485 0, /* properties_required */
3486 PROP_cfglayout, /* properties_provided */
3487 0, /* properties_destroyed */
3488 0, /* todo_flags_start */
3489 0, /* todo_flags_finish */
3492 class pass_into_cfg_layout_mode : public rtl_opt_pass
3494 public:
3495 pass_into_cfg_layout_mode (gcc::context *ctxt)
3496 : rtl_opt_pass (pass_data_into_cfg_layout_mode, ctxt)
3499 /* opt_pass methods: */
3500 unsigned int execute () { return into_cfg_layout_mode (); }
3502 }; // class pass_into_cfg_layout_mode
3504 } // anon namespace
3506 rtl_opt_pass *
3507 make_pass_into_cfg_layout_mode (gcc::context *ctxt)
3509 return new pass_into_cfg_layout_mode (ctxt);
3512 namespace {
3514 const pass_data pass_data_outof_cfg_layout_mode =
3516 RTL_PASS, /* type */
3517 "outof_cfglayout", /* name */
3518 OPTGROUP_NONE, /* optinfo_flags */
3519 false, /* has_gate */
3520 true, /* has_execute */
3521 TV_CFG, /* tv_id */
3522 0, /* properties_required */
3523 0, /* properties_provided */
3524 PROP_cfglayout, /* properties_destroyed */
3525 0, /* todo_flags_start */
3526 0, /* todo_flags_finish */
3529 class pass_outof_cfg_layout_mode : public rtl_opt_pass
3531 public:
3532 pass_outof_cfg_layout_mode (gcc::context *ctxt)
3533 : rtl_opt_pass (pass_data_outof_cfg_layout_mode, ctxt)
3536 /* opt_pass methods: */
3537 unsigned int execute () { return outof_cfg_layout_mode (); }
3539 }; // class pass_outof_cfg_layout_mode
3541 } // anon namespace
3543 rtl_opt_pass *
3544 make_pass_outof_cfg_layout_mode (gcc::context *ctxt)
3546 return new pass_outof_cfg_layout_mode (ctxt);
3550 /* Link the basic blocks in the correct order, compacting the basic
3551 block queue while at it. If STAY_IN_CFGLAYOUT_MODE is false, this
3552 function also clears the basic block header and footer fields.
3554 This function is usually called after a pass (e.g. tracer) finishes
3555 some transformations while in cfglayout mode. The required sequence
3556 of the basic blocks is in a linked list along the bb->aux field.
3557 This functions re-links the basic block prev_bb and next_bb pointers
3558 accordingly, and it compacts and renumbers the blocks.
3560 FIXME: This currently works only for RTL, but the only RTL-specific
3561 bits are the STAY_IN_CFGLAYOUT_MODE bits. The tracer pass was moved
3562 to GIMPLE a long time ago, but it doesn't relink the basic block
3563 chain. It could do that (to give better initial RTL) if this function
3564 is made IR-agnostic (and moved to cfganal.c or cfg.c while at it). */
3566 void
3567 relink_block_chain (bool stay_in_cfglayout_mode)
3569 basic_block bb, prev_bb;
3570 int index;
3572 /* Maybe dump the re-ordered sequence. */
3573 if (dump_file)
3575 fprintf (dump_file, "Reordered sequence:\n");
3576 for (bb = ENTRY_BLOCK_PTR->next_bb, index = NUM_FIXED_BLOCKS;
3578 bb = (basic_block) bb->aux, index++)
3580 fprintf (dump_file, " %i ", index);
3581 if (get_bb_original (bb))
3582 fprintf (dump_file, "duplicate of %i ",
3583 get_bb_original (bb)->index);
3584 else if (forwarder_block_p (bb)
3585 && !LABEL_P (BB_HEAD (bb)))
3586 fprintf (dump_file, "compensation ");
3587 else
3588 fprintf (dump_file, "bb %i ", bb->index);
3589 fprintf (dump_file, " [%i]\n", bb->frequency);
3593 /* Now reorder the blocks. */
3594 prev_bb = ENTRY_BLOCK_PTR;
3595 bb = ENTRY_BLOCK_PTR->next_bb;
3596 for (; bb; prev_bb = bb, bb = (basic_block) bb->aux)
3598 bb->prev_bb = prev_bb;
3599 prev_bb->next_bb = bb;
3601 prev_bb->next_bb = EXIT_BLOCK_PTR;
3602 EXIT_BLOCK_PTR->prev_bb = prev_bb;
3604 /* Then, clean up the aux fields. */
3605 FOR_ALL_BB (bb)
3607 bb->aux = NULL;
3608 if (!stay_in_cfglayout_mode)
3609 BB_HEADER (bb) = BB_FOOTER (bb) = NULL;
3612 /* Maybe reset the original copy tables, they are not valid anymore
3613 when we renumber the basic blocks in compact_blocks. If we are
3614 are going out of cfglayout mode, don't re-allocate the tables. */
3615 free_original_copy_tables ();
3616 if (stay_in_cfglayout_mode)
3617 initialize_original_copy_tables ();
3619 /* Finally, put basic_block_info in the new order. */
3620 compact_blocks ();
3624 /* Given a reorder chain, rearrange the code to match. */
3626 static void
3627 fixup_reorder_chain (void)
3629 basic_block bb;
3630 rtx insn = NULL;
3632 if (cfg_layout_function_header)
3634 set_first_insn (cfg_layout_function_header);
3635 insn = cfg_layout_function_header;
3636 while (NEXT_INSN (insn))
3637 insn = NEXT_INSN (insn);
3640 /* First do the bulk reordering -- rechain the blocks without regard to
3641 the needed changes to jumps and labels. */
3643 for (bb = ENTRY_BLOCK_PTR->next_bb; bb; bb = (basic_block) bb->aux)
3645 if (BB_HEADER (bb))
3647 if (insn)
3648 NEXT_INSN (insn) = BB_HEADER (bb);
3649 else
3650 set_first_insn (BB_HEADER (bb));
3651 PREV_INSN (BB_HEADER (bb)) = insn;
3652 insn = BB_HEADER (bb);
3653 while (NEXT_INSN (insn))
3654 insn = NEXT_INSN (insn);
3656 if (insn)
3657 NEXT_INSN (insn) = BB_HEAD (bb);
3658 else
3659 set_first_insn (BB_HEAD (bb));
3660 PREV_INSN (BB_HEAD (bb)) = insn;
3661 insn = BB_END (bb);
3662 if (BB_FOOTER (bb))
3664 NEXT_INSN (insn) = BB_FOOTER (bb);
3665 PREV_INSN (BB_FOOTER (bb)) = insn;
3666 while (NEXT_INSN (insn))
3667 insn = NEXT_INSN (insn);
3671 NEXT_INSN (insn) = cfg_layout_function_footer;
3672 if (cfg_layout_function_footer)
3673 PREV_INSN (cfg_layout_function_footer) = insn;
3675 while (NEXT_INSN (insn))
3676 insn = NEXT_INSN (insn);
3678 set_last_insn (insn);
3679 #ifdef ENABLE_CHECKING
3680 verify_insn_chain ();
3681 #endif
3683 /* Now add jumps and labels as needed to match the blocks new
3684 outgoing edges. */
3686 for (bb = ENTRY_BLOCK_PTR->next_bb; bb ; bb = (basic_block) bb->aux)
3688 edge e_fall, e_taken, e;
3689 rtx bb_end_insn;
3690 rtx ret_label = NULL_RTX;
3691 basic_block nb;
3692 edge_iterator ei;
3694 if (EDGE_COUNT (bb->succs) == 0)
3695 continue;
3697 /* Find the old fallthru edge, and another non-EH edge for
3698 a taken jump. */
3699 e_taken = e_fall = NULL;
3701 FOR_EACH_EDGE (e, ei, bb->succs)
3702 if (e->flags & EDGE_FALLTHRU)
3703 e_fall = e;
3704 else if (! (e->flags & EDGE_EH))
3705 e_taken = e;
3707 bb_end_insn = BB_END (bb);
3708 if (JUMP_P (bb_end_insn))
3710 ret_label = JUMP_LABEL (bb_end_insn);
3711 if (any_condjump_p (bb_end_insn))
3713 /* This might happen if the conditional jump has side
3714 effects and could therefore not be optimized away.
3715 Make the basic block to end with a barrier in order
3716 to prevent rtl_verify_flow_info from complaining. */
3717 if (!e_fall)
3719 gcc_assert (!onlyjump_p (bb_end_insn)
3720 || returnjump_p (bb_end_insn));
3721 emit_barrier_after (bb_end_insn);
3722 continue;
3725 /* If the old fallthru is still next, nothing to do. */
3726 if (bb->aux == e_fall->dest
3727 || e_fall->dest == EXIT_BLOCK_PTR)
3728 continue;
3730 /* The degenerated case of conditional jump jumping to the next
3731 instruction can happen for jumps with side effects. We need
3732 to construct a forwarder block and this will be done just
3733 fine by force_nonfallthru below. */
3734 if (!e_taken)
3737 /* There is another special case: if *neither* block is next,
3738 such as happens at the very end of a function, then we'll
3739 need to add a new unconditional jump. Choose the taken
3740 edge based on known or assumed probability. */
3741 else if (bb->aux != e_taken->dest)
3743 rtx note = find_reg_note (bb_end_insn, REG_BR_PROB, 0);
3745 if (note
3746 && XINT (note, 0) < REG_BR_PROB_BASE / 2
3747 && invert_jump (bb_end_insn,
3748 (e_fall->dest == EXIT_BLOCK_PTR
3749 ? NULL_RTX
3750 : label_for_bb (e_fall->dest)), 0))
3752 e_fall->flags &= ~EDGE_FALLTHRU;
3753 gcc_checking_assert (could_fall_through
3754 (e_taken->src, e_taken->dest));
3755 e_taken->flags |= EDGE_FALLTHRU;
3756 update_br_prob_note (bb);
3757 e = e_fall, e_fall = e_taken, e_taken = e;
3761 /* If the "jumping" edge is a crossing edge, and the fall
3762 through edge is non-crossing, leave things as they are. */
3763 else if ((e_taken->flags & EDGE_CROSSING)
3764 && !(e_fall->flags & EDGE_CROSSING))
3765 continue;
3767 /* Otherwise we can try to invert the jump. This will
3768 basically never fail, however, keep up the pretense. */
3769 else if (invert_jump (bb_end_insn,
3770 (e_fall->dest == EXIT_BLOCK_PTR
3771 ? NULL_RTX
3772 : label_for_bb (e_fall->dest)), 0))
3774 e_fall->flags &= ~EDGE_FALLTHRU;
3775 gcc_checking_assert (could_fall_through
3776 (e_taken->src, e_taken->dest));
3777 e_taken->flags |= EDGE_FALLTHRU;
3778 update_br_prob_note (bb);
3779 if (LABEL_NUSES (ret_label) == 0
3780 && single_pred_p (e_taken->dest))
3781 delete_insn (ret_label);
3782 continue;
3785 else if (extract_asm_operands (PATTERN (bb_end_insn)) != NULL)
3787 /* If the old fallthru is still next or if
3788 asm goto doesn't have a fallthru (e.g. when followed by
3789 __builtin_unreachable ()), nothing to do. */
3790 if (! e_fall
3791 || bb->aux == e_fall->dest
3792 || e_fall->dest == EXIT_BLOCK_PTR)
3793 continue;
3795 /* Otherwise we'll have to use the fallthru fixup below. */
3797 else
3799 /* Otherwise we have some return, switch or computed
3800 jump. In the 99% case, there should not have been a
3801 fallthru edge. */
3802 gcc_assert (returnjump_p (bb_end_insn) || !e_fall);
3803 continue;
3806 else
3808 /* No fallthru implies a noreturn function with EH edges, or
3809 something similarly bizarre. In any case, we don't need to
3810 do anything. */
3811 if (! e_fall)
3812 continue;
3814 /* If the fallthru block is still next, nothing to do. */
3815 if (bb->aux == e_fall->dest)
3816 continue;
3818 /* A fallthru to exit block. */
3819 if (e_fall->dest == EXIT_BLOCK_PTR)
3820 continue;
3823 /* We got here if we need to add a new jump insn.
3824 Note force_nonfallthru can delete E_FALL and thus we have to
3825 save E_FALL->src prior to the call to force_nonfallthru. */
3826 nb = force_nonfallthru_and_redirect (e_fall, e_fall->dest, ret_label);
3827 if (nb)
3829 nb->aux = bb->aux;
3830 bb->aux = nb;
3831 /* Don't process this new block. */
3832 bb = nb;
3836 relink_block_chain (/*stay_in_cfglayout_mode=*/false);
3838 /* Annoying special case - jump around dead jumptables left in the code. */
3839 FOR_EACH_BB (bb)
3841 edge e = find_fallthru_edge (bb->succs);
3843 if (e && !can_fallthru (e->src, e->dest))
3844 force_nonfallthru (e);
3847 /* Ensure goto_locus from edges has some instructions with that locus
3848 in RTL. */
3849 if (!optimize)
3850 FOR_EACH_BB (bb)
3852 edge e;
3853 edge_iterator ei;
3855 FOR_EACH_EDGE (e, ei, bb->succs)
3856 if (LOCATION_LOCUS (e->goto_locus) != UNKNOWN_LOCATION
3857 && !(e->flags & EDGE_ABNORMAL))
3859 edge e2;
3860 edge_iterator ei2;
3861 basic_block dest, nb;
3862 rtx end;
3864 insn = BB_END (e->src);
3865 end = PREV_INSN (BB_HEAD (e->src));
3866 while (insn != end
3867 && (!NONDEBUG_INSN_P (insn) || !INSN_HAS_LOCATION (insn)))
3868 insn = PREV_INSN (insn);
3869 if (insn != end
3870 && INSN_LOCATION (insn) == e->goto_locus)
3871 continue;
3872 if (simplejump_p (BB_END (e->src))
3873 && !INSN_HAS_LOCATION (BB_END (e->src)))
3875 INSN_LOCATION (BB_END (e->src)) = e->goto_locus;
3876 continue;
3878 dest = e->dest;
3879 if (dest == EXIT_BLOCK_PTR)
3881 /* Non-fallthru edges to the exit block cannot be split. */
3882 if (!(e->flags & EDGE_FALLTHRU))
3883 continue;
3885 else
3887 insn = BB_HEAD (dest);
3888 end = NEXT_INSN (BB_END (dest));
3889 while (insn != end && !NONDEBUG_INSN_P (insn))
3890 insn = NEXT_INSN (insn);
3891 if (insn != end && INSN_HAS_LOCATION (insn)
3892 && INSN_LOCATION (insn) == e->goto_locus)
3893 continue;
3895 nb = split_edge (e);
3896 if (!INSN_P (BB_END (nb)))
3897 BB_END (nb) = emit_insn_after_noloc (gen_nop (), BB_END (nb),
3898 nb);
3899 INSN_LOCATION (BB_END (nb)) = e->goto_locus;
3901 /* If there are other incoming edges to the destination block
3902 with the same goto locus, redirect them to the new block as
3903 well, this can prevent other such blocks from being created
3904 in subsequent iterations of the loop. */
3905 for (ei2 = ei_start (dest->preds); (e2 = ei_safe_edge (ei2)); )
3906 if (LOCATION_LOCUS (e2->goto_locus) != UNKNOWN_LOCATION
3907 && !(e2->flags & (EDGE_ABNORMAL | EDGE_FALLTHRU))
3908 && e->goto_locus == e2->goto_locus)
3909 redirect_edge_and_branch (e2, nb);
3910 else
3911 ei_next (&ei2);
3916 /* Perform sanity checks on the insn chain.
3917 1. Check that next/prev pointers are consistent in both the forward and
3918 reverse direction.
3919 2. Count insns in chain, going both directions, and check if equal.
3920 3. Check that get_last_insn () returns the actual end of chain. */
3922 DEBUG_FUNCTION void
3923 verify_insn_chain (void)
3925 rtx x, prevx, nextx;
3926 int insn_cnt1, insn_cnt2;
3928 for (prevx = NULL, insn_cnt1 = 1, x = get_insns ();
3929 x != 0;
3930 prevx = x, insn_cnt1++, x = NEXT_INSN (x))
3931 gcc_assert (PREV_INSN (x) == prevx);
3933 gcc_assert (prevx == get_last_insn ());
3935 for (nextx = NULL, insn_cnt2 = 1, x = get_last_insn ();
3936 x != 0;
3937 nextx = x, insn_cnt2++, x = PREV_INSN (x))
3938 gcc_assert (NEXT_INSN (x) == nextx);
3940 gcc_assert (insn_cnt1 == insn_cnt2);
3943 /* If we have assembler epilogues, the block falling through to exit must
3944 be the last one in the reordered chain when we reach final. Ensure
3945 that this condition is met. */
3946 static void
3947 fixup_fallthru_exit_predecessor (void)
3949 edge e;
3950 basic_block bb = NULL;
3952 /* This transformation is not valid before reload, because we might
3953 separate a call from the instruction that copies the return
3954 value. */
3955 gcc_assert (reload_completed);
3957 e = find_fallthru_edge (EXIT_BLOCK_PTR->preds);
3958 if (e)
3959 bb = e->src;
3961 if (bb && bb->aux)
3963 basic_block c = ENTRY_BLOCK_PTR->next_bb;
3965 /* If the very first block is the one with the fall-through exit
3966 edge, we have to split that block. */
3967 if (c == bb)
3969 bb = split_block (bb, NULL)->dest;
3970 bb->aux = c->aux;
3971 c->aux = bb;
3972 BB_FOOTER (bb) = BB_FOOTER (c);
3973 BB_FOOTER (c) = NULL;
3976 while (c->aux != bb)
3977 c = (basic_block) c->aux;
3979 c->aux = bb->aux;
3980 while (c->aux)
3981 c = (basic_block) c->aux;
3983 c->aux = bb;
3984 bb->aux = NULL;
3988 /* In case there are more than one fallthru predecessors of exit, force that
3989 there is only one. */
3991 static void
3992 force_one_exit_fallthru (void)
3994 edge e, predecessor = NULL;
3995 bool more = false;
3996 edge_iterator ei;
3997 basic_block forwarder, bb;
3999 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
4000 if (e->flags & EDGE_FALLTHRU)
4002 if (predecessor == NULL)
4003 predecessor = e;
4004 else
4006 more = true;
4007 break;
4011 if (!more)
4012 return;
4014 /* Exit has several fallthru predecessors. Create a forwarder block for
4015 them. */
4016 forwarder = split_edge (predecessor);
4017 for (ei = ei_start (EXIT_BLOCK_PTR->preds); (e = ei_safe_edge (ei)); )
4019 if (e->src == forwarder
4020 || !(e->flags & EDGE_FALLTHRU))
4021 ei_next (&ei);
4022 else
4023 redirect_edge_and_branch_force (e, forwarder);
4026 /* Fix up the chain of blocks -- make FORWARDER immediately precede the
4027 exit block. */
4028 FOR_EACH_BB (bb)
4030 if (bb->aux == NULL && bb != forwarder)
4032 bb->aux = forwarder;
4033 break;
4038 /* Return true in case it is possible to duplicate the basic block BB. */
4040 static bool
4041 cfg_layout_can_duplicate_bb_p (const_basic_block bb)
4043 /* Do not attempt to duplicate tablejumps, as we need to unshare
4044 the dispatch table. This is difficult to do, as the instructions
4045 computing jump destination may be hoisted outside the basic block. */
4046 if (tablejump_p (BB_END (bb), NULL, NULL))
4047 return false;
4049 /* Do not duplicate blocks containing insns that can't be copied. */
4050 if (targetm.cannot_copy_insn_p)
4052 rtx insn = BB_HEAD (bb);
4053 while (1)
4055 if (INSN_P (insn) && targetm.cannot_copy_insn_p (insn))
4056 return false;
4057 if (insn == BB_END (bb))
4058 break;
4059 insn = NEXT_INSN (insn);
4063 return true;
4067 duplicate_insn_chain (rtx from, rtx to)
4069 rtx insn, next, last, copy;
4071 /* Avoid updating of boundaries of previous basic block. The
4072 note will get removed from insn stream in fixup. */
4073 last = emit_note (NOTE_INSN_DELETED);
4075 /* Create copy at the end of INSN chain. The chain will
4076 be reordered later. */
4077 for (insn = from; insn != NEXT_INSN (to); insn = NEXT_INSN (insn))
4079 switch (GET_CODE (insn))
4081 case DEBUG_INSN:
4082 /* Don't duplicate label debug insns. */
4083 if (TREE_CODE (INSN_VAR_LOCATION_DECL (insn)) == LABEL_DECL)
4084 break;
4085 /* FALLTHRU */
4086 case INSN:
4087 case CALL_INSN:
4088 case JUMP_INSN:
4089 copy = emit_copy_of_insn_after (insn, get_last_insn ());
4090 if (JUMP_P (insn) && JUMP_LABEL (insn) != NULL_RTX
4091 && ANY_RETURN_P (JUMP_LABEL (insn)))
4092 JUMP_LABEL (copy) = JUMP_LABEL (insn);
4093 maybe_copy_prologue_epilogue_insn (insn, copy);
4094 break;
4096 case JUMP_TABLE_DATA:
4097 /* Avoid copying of dispatch tables. We never duplicate
4098 tablejumps, so this can hit only in case the table got
4099 moved far from original jump.
4100 Avoid copying following barrier as well if any
4101 (and debug insns in between). */
4102 for (next = NEXT_INSN (insn);
4103 next != NEXT_INSN (to);
4104 next = NEXT_INSN (next))
4105 if (!DEBUG_INSN_P (next))
4106 break;
4107 if (next != NEXT_INSN (to) && BARRIER_P (next))
4108 insn = next;
4109 break;
4111 case CODE_LABEL:
4112 break;
4114 case BARRIER:
4115 emit_barrier ();
4116 break;
4118 case NOTE:
4119 switch (NOTE_KIND (insn))
4121 /* In case prologue is empty and function contain label
4122 in first BB, we may want to copy the block. */
4123 case NOTE_INSN_PROLOGUE_END:
4125 case NOTE_INSN_DELETED:
4126 case NOTE_INSN_DELETED_LABEL:
4127 case NOTE_INSN_DELETED_DEBUG_LABEL:
4128 /* No problem to strip these. */
4129 case NOTE_INSN_FUNCTION_BEG:
4130 /* There is always just single entry to function. */
4131 case NOTE_INSN_BASIC_BLOCK:
4132 /* We should only switch text sections once. */
4133 case NOTE_INSN_SWITCH_TEXT_SECTIONS:
4134 break;
4136 case NOTE_INSN_EPILOGUE_BEG:
4137 emit_note_copy (insn);
4138 break;
4140 default:
4141 /* All other notes should have already been eliminated. */
4142 gcc_unreachable ();
4144 break;
4145 default:
4146 gcc_unreachable ();
4149 insn = NEXT_INSN (last);
4150 delete_insn (last);
4151 return insn;
4154 /* Create a duplicate of the basic block BB. */
4156 static basic_block
4157 cfg_layout_duplicate_bb (basic_block bb)
4159 rtx insn;
4160 basic_block new_bb;
4162 insn = duplicate_insn_chain (BB_HEAD (bb), BB_END (bb));
4163 new_bb = create_basic_block (insn,
4164 insn ? get_last_insn () : NULL,
4165 EXIT_BLOCK_PTR->prev_bb);
4167 BB_COPY_PARTITION (new_bb, bb);
4168 if (BB_HEADER (bb))
4170 insn = BB_HEADER (bb);
4171 while (NEXT_INSN (insn))
4172 insn = NEXT_INSN (insn);
4173 insn = duplicate_insn_chain (BB_HEADER (bb), insn);
4174 if (insn)
4175 BB_HEADER (new_bb) = unlink_insn_chain (insn, get_last_insn ());
4178 if (BB_FOOTER (bb))
4180 insn = BB_FOOTER (bb);
4181 while (NEXT_INSN (insn))
4182 insn = NEXT_INSN (insn);
4183 insn = duplicate_insn_chain (BB_FOOTER (bb), insn);
4184 if (insn)
4185 BB_FOOTER (new_bb) = unlink_insn_chain (insn, get_last_insn ());
4188 return new_bb;
4192 /* Main entry point to this module - initialize the datastructures for
4193 CFG layout changes. It keeps LOOPS up-to-date if not null.
4195 FLAGS is a set of additional flags to pass to cleanup_cfg(). */
4197 void
4198 cfg_layout_initialize (unsigned int flags)
4200 rtx x;
4201 basic_block bb;
4203 initialize_original_copy_tables ();
4205 cfg_layout_rtl_register_cfg_hooks ();
4207 record_effective_endpoints ();
4209 /* Make sure that the targets of non local gotos are marked. */
4210 for (x = nonlocal_goto_handler_labels; x; x = XEXP (x, 1))
4212 bb = BLOCK_FOR_INSN (XEXP (x, 0));
4213 bb->flags |= BB_NON_LOCAL_GOTO_TARGET;
4216 cleanup_cfg (CLEANUP_CFGLAYOUT | flags);
4219 /* Splits superblocks. */
4220 void
4221 break_superblocks (void)
4223 sbitmap superblocks;
4224 bool need = false;
4225 basic_block bb;
4227 superblocks = sbitmap_alloc (last_basic_block);
4228 bitmap_clear (superblocks);
4230 FOR_EACH_BB (bb)
4231 if (bb->flags & BB_SUPERBLOCK)
4233 bb->flags &= ~BB_SUPERBLOCK;
4234 bitmap_set_bit (superblocks, bb->index);
4235 need = true;
4238 if (need)
4240 rebuild_jump_labels (get_insns ());
4241 find_many_sub_basic_blocks (superblocks);
4244 free (superblocks);
4247 /* Finalize the changes: reorder insn list according to the sequence specified
4248 by aux pointers, enter compensation code, rebuild scope forest. */
4250 void
4251 cfg_layout_finalize (void)
4253 #ifdef ENABLE_CHECKING
4254 verify_flow_info ();
4255 #endif
4256 force_one_exit_fallthru ();
4257 rtl_register_cfg_hooks ();
4258 if (reload_completed
4259 #ifdef HAVE_epilogue
4260 && !HAVE_epilogue
4261 #endif
4263 fixup_fallthru_exit_predecessor ();
4264 fixup_reorder_chain ();
4266 rebuild_jump_labels (get_insns ());
4267 delete_dead_jumptables ();
4269 #ifdef ENABLE_CHECKING
4270 verify_insn_chain ();
4271 verify_flow_info ();
4272 #endif
4276 /* Same as split_block but update cfg_layout structures. */
4278 static basic_block
4279 cfg_layout_split_block (basic_block bb, void *insnp)
4281 rtx insn = (rtx) insnp;
4282 basic_block new_bb = rtl_split_block (bb, insn);
4284 BB_FOOTER (new_bb) = BB_FOOTER (bb);
4285 BB_FOOTER (bb) = NULL;
4287 return new_bb;
4290 /* Redirect Edge to DEST. */
4291 static edge
4292 cfg_layout_redirect_edge_and_branch (edge e, basic_block dest)
4294 basic_block src = e->src;
4295 edge ret;
4297 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
4298 return NULL;
4300 if (e->dest == dest)
4301 return e;
4303 if (e->src != ENTRY_BLOCK_PTR
4304 && (ret = try_redirect_by_replacing_jump (e, dest, true)))
4306 df_set_bb_dirty (src);
4307 return ret;
4310 if (e->src == ENTRY_BLOCK_PTR
4311 && (e->flags & EDGE_FALLTHRU) && !(e->flags & EDGE_COMPLEX))
4313 if (dump_file)
4314 fprintf (dump_file, "Redirecting entry edge from bb %i to %i\n",
4315 e->src->index, dest->index);
4317 df_set_bb_dirty (e->src);
4318 redirect_edge_succ (e, dest);
4319 return e;
4322 /* Redirect_edge_and_branch may decide to turn branch into fallthru edge
4323 in the case the basic block appears to be in sequence. Avoid this
4324 transformation. */
4326 if (e->flags & EDGE_FALLTHRU)
4328 /* Redirect any branch edges unified with the fallthru one. */
4329 if (JUMP_P (BB_END (src))
4330 && label_is_jump_target_p (BB_HEAD (e->dest),
4331 BB_END (src)))
4333 edge redirected;
4335 if (dump_file)
4336 fprintf (dump_file, "Fallthru edge unified with branch "
4337 "%i->%i redirected to %i\n",
4338 e->src->index, e->dest->index, dest->index);
4339 e->flags &= ~EDGE_FALLTHRU;
4340 redirected = redirect_branch_edge (e, dest);
4341 gcc_assert (redirected);
4342 redirected->flags |= EDGE_FALLTHRU;
4343 df_set_bb_dirty (redirected->src);
4344 return redirected;
4346 /* In case we are redirecting fallthru edge to the branch edge
4347 of conditional jump, remove it. */
4348 if (EDGE_COUNT (src->succs) == 2)
4350 /* Find the edge that is different from E. */
4351 edge s = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e);
4353 if (s->dest == dest
4354 && any_condjump_p (BB_END (src))
4355 && onlyjump_p (BB_END (src)))
4356 delete_insn (BB_END (src));
4358 if (dump_file)
4359 fprintf (dump_file, "Redirecting fallthru edge %i->%i to %i\n",
4360 e->src->index, e->dest->index, dest->index);
4361 ret = redirect_edge_succ_nodup (e, dest);
4363 else
4364 ret = redirect_branch_edge (e, dest);
4366 /* We don't want simplejumps in the insn stream during cfglayout. */
4367 gcc_assert (!simplejump_p (BB_END (src)));
4369 df_set_bb_dirty (src);
4370 return ret;
4373 /* Simple wrapper as we always can redirect fallthru edges. */
4374 static basic_block
4375 cfg_layout_redirect_edge_and_branch_force (edge e, basic_block dest)
4377 edge redirected = cfg_layout_redirect_edge_and_branch (e, dest);
4379 gcc_assert (redirected);
4380 return NULL;
4383 /* Same as delete_basic_block but update cfg_layout structures. */
4385 static void
4386 cfg_layout_delete_block (basic_block bb)
4388 rtx insn, next, prev = PREV_INSN (BB_HEAD (bb)), *to, remaints;
4390 if (BB_HEADER (bb))
4392 next = BB_HEAD (bb);
4393 if (prev)
4394 NEXT_INSN (prev) = BB_HEADER (bb);
4395 else
4396 set_first_insn (BB_HEADER (bb));
4397 PREV_INSN (BB_HEADER (bb)) = prev;
4398 insn = BB_HEADER (bb);
4399 while (NEXT_INSN (insn))
4400 insn = NEXT_INSN (insn);
4401 NEXT_INSN (insn) = next;
4402 PREV_INSN (next) = insn;
4404 next = NEXT_INSN (BB_END (bb));
4405 if (BB_FOOTER (bb))
4407 insn = BB_FOOTER (bb);
4408 while (insn)
4410 if (BARRIER_P (insn))
4412 if (PREV_INSN (insn))
4413 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
4414 else
4415 BB_FOOTER (bb) = NEXT_INSN (insn);
4416 if (NEXT_INSN (insn))
4417 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
4419 if (LABEL_P (insn))
4420 break;
4421 insn = NEXT_INSN (insn);
4423 if (BB_FOOTER (bb))
4425 insn = BB_END (bb);
4426 NEXT_INSN (insn) = BB_FOOTER (bb);
4427 PREV_INSN (BB_FOOTER (bb)) = insn;
4428 while (NEXT_INSN (insn))
4429 insn = NEXT_INSN (insn);
4430 NEXT_INSN (insn) = next;
4431 if (next)
4432 PREV_INSN (next) = insn;
4433 else
4434 set_last_insn (insn);
4437 if (bb->next_bb != EXIT_BLOCK_PTR)
4438 to = &BB_HEADER (bb->next_bb);
4439 else
4440 to = &cfg_layout_function_footer;
4442 rtl_delete_block (bb);
4444 if (prev)
4445 prev = NEXT_INSN (prev);
4446 else
4447 prev = get_insns ();
4448 if (next)
4449 next = PREV_INSN (next);
4450 else
4451 next = get_last_insn ();
4453 if (next && NEXT_INSN (next) != prev)
4455 remaints = unlink_insn_chain (prev, next);
4456 insn = remaints;
4457 while (NEXT_INSN (insn))
4458 insn = NEXT_INSN (insn);
4459 NEXT_INSN (insn) = *to;
4460 if (*to)
4461 PREV_INSN (*to) = insn;
4462 *to = remaints;
4466 /* Return true when blocks A and B can be safely merged. */
4468 static bool
4469 cfg_layout_can_merge_blocks_p (basic_block a, basic_block b)
4471 /* If we are partitioning hot/cold basic blocks, we don't want to
4472 mess up unconditional or indirect jumps that cross between hot
4473 and cold sections.
4475 Basic block partitioning may result in some jumps that appear to
4476 be optimizable (or blocks that appear to be mergeable), but which really
4477 must be left untouched (they are required to make it safely across
4478 partition boundaries). See the comments at the top of
4479 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
4481 if (BB_PARTITION (a) != BB_PARTITION (b))
4482 return false;
4484 /* Protect the loop latches. */
4485 if (current_loops && b->loop_father->latch == b)
4486 return false;
4488 /* If we would end up moving B's instructions, make sure it doesn't fall
4489 through into the exit block, since we cannot recover from a fallthrough
4490 edge into the exit block occurring in the middle of a function. */
4491 if (NEXT_INSN (BB_END (a)) != BB_HEAD (b))
4493 edge e = find_fallthru_edge (b->succs);
4494 if (e && e->dest == EXIT_BLOCK_PTR)
4495 return false;
4498 /* There must be exactly one edge in between the blocks. */
4499 return (single_succ_p (a)
4500 && single_succ (a) == b
4501 && single_pred_p (b) == 1
4502 && a != b
4503 /* Must be simple edge. */
4504 && !(single_succ_edge (a)->flags & EDGE_COMPLEX)
4505 && a != ENTRY_BLOCK_PTR && b != EXIT_BLOCK_PTR
4506 /* If the jump insn has side effects, we can't kill the edge.
4507 When not optimizing, try_redirect_by_replacing_jump will
4508 not allow us to redirect an edge by replacing a table jump. */
4509 && (!JUMP_P (BB_END (a))
4510 || ((!optimize || reload_completed)
4511 ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
4514 /* Merge block A and B. The blocks must be mergeable. */
4516 static void
4517 cfg_layout_merge_blocks (basic_block a, basic_block b)
4519 bool forwarder_p = (b->flags & BB_FORWARDER_BLOCK) != 0;
4520 rtx insn;
4522 gcc_checking_assert (cfg_layout_can_merge_blocks_p (a, b));
4524 if (dump_file)
4525 fprintf (dump_file, "Merging block %d into block %d...\n", b->index,
4526 a->index);
4528 /* If there was a CODE_LABEL beginning B, delete it. */
4529 if (LABEL_P (BB_HEAD (b)))
4531 delete_insn (BB_HEAD (b));
4534 /* We should have fallthru edge in a, or we can do dummy redirection to get
4535 it cleaned up. */
4536 if (JUMP_P (BB_END (a)))
4537 try_redirect_by_replacing_jump (EDGE_SUCC (a, 0), b, true);
4538 gcc_assert (!JUMP_P (BB_END (a)));
4540 /* When not optimizing CFG and the edge is the only place in RTL which holds
4541 some unique locus, emit a nop with that locus in between. */
4542 if (!optimize)
4543 emit_nop_for_unique_locus_between (a, b);
4545 /* Move things from b->footer after a->footer. */
4546 if (BB_FOOTER (b))
4548 if (!BB_FOOTER (a))
4549 BB_FOOTER (a) = BB_FOOTER (b);
4550 else
4552 rtx last = BB_FOOTER (a);
4554 while (NEXT_INSN (last))
4555 last = NEXT_INSN (last);
4556 NEXT_INSN (last) = BB_FOOTER (b);
4557 PREV_INSN (BB_FOOTER (b)) = last;
4559 BB_FOOTER (b) = NULL;
4562 /* Move things from b->header before a->footer.
4563 Note that this may include dead tablejump data, but we don't clean
4564 those up until we go out of cfglayout mode. */
4565 if (BB_HEADER (b))
4567 if (! BB_FOOTER (a))
4568 BB_FOOTER (a) = BB_HEADER (b);
4569 else
4571 rtx last = BB_HEADER (b);
4573 while (NEXT_INSN (last))
4574 last = NEXT_INSN (last);
4575 NEXT_INSN (last) = BB_FOOTER (a);
4576 PREV_INSN (BB_FOOTER (a)) = last;
4577 BB_FOOTER (a) = BB_HEADER (b);
4579 BB_HEADER (b) = NULL;
4582 /* In the case basic blocks are not adjacent, move them around. */
4583 if (NEXT_INSN (BB_END (a)) != BB_HEAD (b))
4585 insn = unlink_insn_chain (BB_HEAD (b), BB_END (b));
4587 emit_insn_after_noloc (insn, BB_END (a), a);
4589 /* Otherwise just re-associate the instructions. */
4590 else
4592 insn = BB_HEAD (b);
4593 BB_END (a) = BB_END (b);
4596 /* emit_insn_after_noloc doesn't call df_insn_change_bb.
4597 We need to explicitly call. */
4598 update_bb_for_insn_chain (insn, BB_END (b), a);
4600 /* Skip possible DELETED_LABEL insn. */
4601 if (!NOTE_INSN_BASIC_BLOCK_P (insn))
4602 insn = NEXT_INSN (insn);
4603 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
4604 BB_HEAD (b) = BB_END (b) = NULL;
4605 delete_insn (insn);
4607 df_bb_delete (b->index);
4609 /* If B was a forwarder block, propagate the locus on the edge. */
4610 if (forwarder_p
4611 && LOCATION_LOCUS (EDGE_SUCC (b, 0)->goto_locus) == UNKNOWN_LOCATION)
4612 EDGE_SUCC (b, 0)->goto_locus = EDGE_SUCC (a, 0)->goto_locus;
4614 if (dump_file)
4615 fprintf (dump_file, "Merged blocks %d and %d.\n", a->index, b->index);
4618 /* Split edge E. */
4620 static basic_block
4621 cfg_layout_split_edge (edge e)
4623 basic_block new_bb =
4624 create_basic_block (e->src != ENTRY_BLOCK_PTR
4625 ? NEXT_INSN (BB_END (e->src)) : get_insns (),
4626 NULL_RTX, e->src);
4628 if (e->dest == EXIT_BLOCK_PTR)
4629 BB_COPY_PARTITION (new_bb, e->src);
4630 else
4631 BB_COPY_PARTITION (new_bb, e->dest);
4632 make_edge (new_bb, e->dest, EDGE_FALLTHRU);
4633 redirect_edge_and_branch_force (e, new_bb);
4635 return new_bb;
4638 /* Do postprocessing after making a forwarder block joined by edge FALLTHRU. */
4640 static void
4641 rtl_make_forwarder_block (edge fallthru ATTRIBUTE_UNUSED)
4645 /* Return true if BB contains only labels or non-executable
4646 instructions. */
4648 static bool
4649 rtl_block_empty_p (basic_block bb)
4651 rtx insn;
4653 if (bb == ENTRY_BLOCK_PTR || bb == EXIT_BLOCK_PTR)
4654 return true;
4656 FOR_BB_INSNS (bb, insn)
4657 if (NONDEBUG_INSN_P (insn) && !any_uncondjump_p (insn))
4658 return false;
4660 return true;
4663 /* Split a basic block if it ends with a conditional branch and if
4664 the other part of the block is not empty. */
4666 static basic_block
4667 rtl_split_block_before_cond_jump (basic_block bb)
4669 rtx insn;
4670 rtx split_point = NULL;
4671 rtx last = NULL;
4672 bool found_code = false;
4674 FOR_BB_INSNS (bb, insn)
4676 if (any_condjump_p (insn))
4677 split_point = last;
4678 else if (NONDEBUG_INSN_P (insn))
4679 found_code = true;
4680 last = insn;
4683 /* Did not find everything. */
4684 if (found_code && split_point)
4685 return split_block (bb, split_point)->dest;
4686 else
4687 return NULL;
4690 /* Return 1 if BB ends with a call, possibly followed by some
4691 instructions that must stay with the call, 0 otherwise. */
4693 static bool
4694 rtl_block_ends_with_call_p (basic_block bb)
4696 rtx insn = BB_END (bb);
4698 while (!CALL_P (insn)
4699 && insn != BB_HEAD (bb)
4700 && (keep_with_call_p (insn)
4701 || NOTE_P (insn)
4702 || DEBUG_INSN_P (insn)))
4703 insn = PREV_INSN (insn);
4704 return (CALL_P (insn));
4707 /* Return 1 if BB ends with a conditional branch, 0 otherwise. */
4709 static bool
4710 rtl_block_ends_with_condjump_p (const_basic_block bb)
4712 return any_condjump_p (BB_END (bb));
4715 /* Return true if we need to add fake edge to exit.
4716 Helper function for rtl_flow_call_edges_add. */
4718 static bool
4719 need_fake_edge_p (const_rtx insn)
4721 if (!INSN_P (insn))
4722 return false;
4724 if ((CALL_P (insn)
4725 && !SIBLING_CALL_P (insn)
4726 && !find_reg_note (insn, REG_NORETURN, NULL)
4727 && !(RTL_CONST_OR_PURE_CALL_P (insn))))
4728 return true;
4730 return ((GET_CODE (PATTERN (insn)) == ASM_OPERANDS
4731 && MEM_VOLATILE_P (PATTERN (insn)))
4732 || (GET_CODE (PATTERN (insn)) == PARALLEL
4733 && asm_noperands (insn) != -1
4734 && MEM_VOLATILE_P (XVECEXP (PATTERN (insn), 0, 0)))
4735 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
4738 /* Add fake edges to the function exit for any non constant and non noreturn
4739 calls, volatile inline assembly in the bitmap of blocks specified by
4740 BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
4741 that were split.
4743 The goal is to expose cases in which entering a basic block does not imply
4744 that all subsequent instructions must be executed. */
4746 static int
4747 rtl_flow_call_edges_add (sbitmap blocks)
4749 int i;
4750 int blocks_split = 0;
4751 int last_bb = last_basic_block;
4752 bool check_last_block = false;
4754 if (n_basic_blocks == NUM_FIXED_BLOCKS)
4755 return 0;
4757 if (! blocks)
4758 check_last_block = true;
4759 else
4760 check_last_block = bitmap_bit_p (blocks, EXIT_BLOCK_PTR->prev_bb->index);
4762 /* In the last basic block, before epilogue generation, there will be
4763 a fallthru edge to EXIT. Special care is required if the last insn
4764 of the last basic block is a call because make_edge folds duplicate
4765 edges, which would result in the fallthru edge also being marked
4766 fake, which would result in the fallthru edge being removed by
4767 remove_fake_edges, which would result in an invalid CFG.
4769 Moreover, we can't elide the outgoing fake edge, since the block
4770 profiler needs to take this into account in order to solve the minimal
4771 spanning tree in the case that the call doesn't return.
4773 Handle this by adding a dummy instruction in a new last basic block. */
4774 if (check_last_block)
4776 basic_block bb = EXIT_BLOCK_PTR->prev_bb;
4777 rtx insn = BB_END (bb);
4779 /* Back up past insns that must be kept in the same block as a call. */
4780 while (insn != BB_HEAD (bb)
4781 && keep_with_call_p (insn))
4782 insn = PREV_INSN (insn);
4784 if (need_fake_edge_p (insn))
4786 edge e;
4788 e = find_edge (bb, EXIT_BLOCK_PTR);
4789 if (e)
4791 insert_insn_on_edge (gen_use (const0_rtx), e);
4792 commit_edge_insertions ();
4797 /* Now add fake edges to the function exit for any non constant
4798 calls since there is no way that we can determine if they will
4799 return or not... */
4801 for (i = NUM_FIXED_BLOCKS; i < last_bb; i++)
4803 basic_block bb = BASIC_BLOCK (i);
4804 rtx insn;
4805 rtx prev_insn;
4807 if (!bb)
4808 continue;
4810 if (blocks && !bitmap_bit_p (blocks, i))
4811 continue;
4813 for (insn = BB_END (bb); ; insn = prev_insn)
4815 prev_insn = PREV_INSN (insn);
4816 if (need_fake_edge_p (insn))
4818 edge e;
4819 rtx split_at_insn = insn;
4821 /* Don't split the block between a call and an insn that should
4822 remain in the same block as the call. */
4823 if (CALL_P (insn))
4824 while (split_at_insn != BB_END (bb)
4825 && keep_with_call_p (NEXT_INSN (split_at_insn)))
4826 split_at_insn = NEXT_INSN (split_at_insn);
4828 /* The handling above of the final block before the epilogue
4829 should be enough to verify that there is no edge to the exit
4830 block in CFG already. Calling make_edge in such case would
4831 cause us to mark that edge as fake and remove it later. */
4833 #ifdef ENABLE_CHECKING
4834 if (split_at_insn == BB_END (bb))
4836 e = find_edge (bb, EXIT_BLOCK_PTR);
4837 gcc_assert (e == NULL);
4839 #endif
4841 /* Note that the following may create a new basic block
4842 and renumber the existing basic blocks. */
4843 if (split_at_insn != BB_END (bb))
4845 e = split_block (bb, split_at_insn);
4846 if (e)
4847 blocks_split++;
4850 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
4853 if (insn == BB_HEAD (bb))
4854 break;
4858 if (blocks_split)
4859 verify_flow_info ();
4861 return blocks_split;
4864 /* Add COMP_RTX as a condition at end of COND_BB. FIRST_HEAD is
4865 the conditional branch target, SECOND_HEAD should be the fall-thru
4866 there is no need to handle this here the loop versioning code handles
4867 this. the reason for SECON_HEAD is that it is needed for condition
4868 in trees, and this should be of the same type since it is a hook. */
4869 static void
4870 rtl_lv_add_condition_to_bb (basic_block first_head ,
4871 basic_block second_head ATTRIBUTE_UNUSED,
4872 basic_block cond_bb, void *comp_rtx)
4874 rtx label, seq, jump;
4875 rtx op0 = XEXP ((rtx)comp_rtx, 0);
4876 rtx op1 = XEXP ((rtx)comp_rtx, 1);
4877 enum rtx_code comp = GET_CODE ((rtx)comp_rtx);
4878 enum machine_mode mode;
4881 label = block_label (first_head);
4882 mode = GET_MODE (op0);
4883 if (mode == VOIDmode)
4884 mode = GET_MODE (op1);
4886 start_sequence ();
4887 op0 = force_operand (op0, NULL_RTX);
4888 op1 = force_operand (op1, NULL_RTX);
4889 do_compare_rtx_and_jump (op0, op1, comp, 0,
4890 mode, NULL_RTX, NULL_RTX, label, -1);
4891 jump = get_last_insn ();
4892 JUMP_LABEL (jump) = label;
4893 LABEL_NUSES (label)++;
4894 seq = get_insns ();
4895 end_sequence ();
4897 /* Add the new cond , in the new head. */
4898 emit_insn_after (seq, BB_END (cond_bb));
4902 /* Given a block B with unconditional branch at its end, get the
4903 store the return the branch edge and the fall-thru edge in
4904 BRANCH_EDGE and FALLTHRU_EDGE respectively. */
4905 static void
4906 rtl_extract_cond_bb_edges (basic_block b, edge *branch_edge,
4907 edge *fallthru_edge)
4909 edge e = EDGE_SUCC (b, 0);
4911 if (e->flags & EDGE_FALLTHRU)
4913 *fallthru_edge = e;
4914 *branch_edge = EDGE_SUCC (b, 1);
4916 else
4918 *branch_edge = e;
4919 *fallthru_edge = EDGE_SUCC (b, 1);
4923 void
4924 init_rtl_bb_info (basic_block bb)
4926 gcc_assert (!bb->il.x.rtl);
4927 bb->il.x.head_ = NULL;
4928 bb->il.x.rtl = ggc_alloc_cleared_rtl_bb_info ();
4931 /* Returns true if it is possible to remove edge E by redirecting
4932 it to the destination of the other edge from E->src. */
4934 static bool
4935 rtl_can_remove_branch_p (const_edge e)
4937 const_basic_block src = e->src;
4938 const_basic_block target = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest;
4939 const_rtx insn = BB_END (src), set;
4941 /* The conditions are taken from try_redirect_by_replacing_jump. */
4942 if (target == EXIT_BLOCK_PTR)
4943 return false;
4945 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
4946 return false;
4948 if (BB_PARTITION (src) != BB_PARTITION (target))
4949 return false;
4951 if (!onlyjump_p (insn)
4952 || tablejump_p (insn, NULL, NULL))
4953 return false;
4955 set = single_set (insn);
4956 if (!set || side_effects_p (set))
4957 return false;
4959 return true;
4962 static basic_block
4963 rtl_duplicate_bb (basic_block bb)
4965 bb = cfg_layout_duplicate_bb (bb);
4966 bb->aux = NULL;
4967 return bb;
4970 /* Do book-keeping of basic block BB for the profile consistency checker.
4971 If AFTER_PASS is 0, do pre-pass accounting, or if AFTER_PASS is 1
4972 then do post-pass accounting. Store the counting in RECORD. */
4973 static void
4974 rtl_account_profile_record (basic_block bb, int after_pass,
4975 struct profile_record *record)
4977 rtx insn;
4978 FOR_BB_INSNS (bb, insn)
4979 if (INSN_P (insn))
4981 record->size[after_pass]
4982 += insn_rtx_cost (PATTERN (insn), false);
4983 if (profile_status == PROFILE_READ)
4984 record->time[after_pass]
4985 += insn_rtx_cost (PATTERN (insn), true) * bb->count;
4986 else if (profile_status == PROFILE_GUESSED)
4987 record->time[after_pass]
4988 += insn_rtx_cost (PATTERN (insn), true) * bb->frequency;
4992 /* Implementation of CFG manipulation for linearized RTL. */
4993 struct cfg_hooks rtl_cfg_hooks = {
4994 "rtl",
4995 rtl_verify_flow_info,
4996 rtl_dump_bb,
4997 rtl_dump_bb_for_graph,
4998 rtl_create_basic_block,
4999 rtl_redirect_edge_and_branch,
5000 rtl_redirect_edge_and_branch_force,
5001 rtl_can_remove_branch_p,
5002 rtl_delete_block,
5003 rtl_split_block,
5004 rtl_move_block_after,
5005 rtl_can_merge_blocks, /* can_merge_blocks_p */
5006 rtl_merge_blocks,
5007 rtl_predict_edge,
5008 rtl_predicted_by_p,
5009 cfg_layout_can_duplicate_bb_p,
5010 rtl_duplicate_bb,
5011 rtl_split_edge,
5012 rtl_make_forwarder_block,
5013 rtl_tidy_fallthru_edge,
5014 rtl_force_nonfallthru,
5015 rtl_block_ends_with_call_p,
5016 rtl_block_ends_with_condjump_p,
5017 rtl_flow_call_edges_add,
5018 NULL, /* execute_on_growing_pred */
5019 NULL, /* execute_on_shrinking_pred */
5020 NULL, /* duplicate loop for trees */
5021 NULL, /* lv_add_condition_to_bb */
5022 NULL, /* lv_adjust_loop_header_phi*/
5023 NULL, /* extract_cond_bb_edges */
5024 NULL, /* flush_pending_stmts */
5025 rtl_block_empty_p, /* block_empty_p */
5026 rtl_split_block_before_cond_jump, /* split_block_before_cond_jump */
5027 rtl_account_profile_record,
5030 /* Implementation of CFG manipulation for cfg layout RTL, where
5031 basic block connected via fallthru edges does not have to be adjacent.
5032 This representation will hopefully become the default one in future
5033 version of the compiler. */
5035 struct cfg_hooks cfg_layout_rtl_cfg_hooks = {
5036 "cfglayout mode",
5037 rtl_verify_flow_info_1,
5038 rtl_dump_bb,
5039 rtl_dump_bb_for_graph,
5040 cfg_layout_create_basic_block,
5041 cfg_layout_redirect_edge_and_branch,
5042 cfg_layout_redirect_edge_and_branch_force,
5043 rtl_can_remove_branch_p,
5044 cfg_layout_delete_block,
5045 cfg_layout_split_block,
5046 rtl_move_block_after,
5047 cfg_layout_can_merge_blocks_p,
5048 cfg_layout_merge_blocks,
5049 rtl_predict_edge,
5050 rtl_predicted_by_p,
5051 cfg_layout_can_duplicate_bb_p,
5052 cfg_layout_duplicate_bb,
5053 cfg_layout_split_edge,
5054 rtl_make_forwarder_block,
5055 NULL, /* tidy_fallthru_edge */
5056 rtl_force_nonfallthru,
5057 rtl_block_ends_with_call_p,
5058 rtl_block_ends_with_condjump_p,
5059 rtl_flow_call_edges_add,
5060 NULL, /* execute_on_growing_pred */
5061 NULL, /* execute_on_shrinking_pred */
5062 duplicate_loop_to_header_edge, /* duplicate loop for trees */
5063 rtl_lv_add_condition_to_bb, /* lv_add_condition_to_bb */
5064 NULL, /* lv_adjust_loop_header_phi*/
5065 rtl_extract_cond_bb_edges, /* extract_cond_bb_edges */
5066 NULL, /* flush_pending_stmts */
5067 rtl_block_empty_p, /* block_empty_p */
5068 rtl_split_block_before_cond_jump, /* split_block_before_cond_jump */
5069 rtl_account_profile_record,
5072 #include "gt-cfgrtl.h"