2013-11-27 Kugan Vivekanandarajah <kuganv@linaro.org>
[official-gcc.git] / gcc / cfgrtl.c
blob63f44afbbae1eb15c41ba6970e328881005fd2eb
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_for_fn (cfun)++;
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_for_fn (cfun) > NUM_FIXED_BLOCKS ?
504 BB_HEAD (ENTRY_BLOCK_PTR_FOR_FN (cfun)->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_FOR_FN (cfun)->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_FOR_FN (cfun) || bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)
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, the result is only valid in cfgrtl mode. */
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_FOR_FN (cfun))
624 return true;
625 if (src->next_bb != target)
626 return false;
628 /* ??? Later we may add code to move jump tables offline. */
629 if (tablejump_p (insn, NULL, NULL))
630 return false;
632 FOR_EACH_EDGE (e, ei, src->succs)
633 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
634 && e->flags & EDGE_FALLTHRU)
635 return false;
637 insn2 = BB_HEAD (target);
638 if (!active_insn_p (insn2))
639 insn2 = next_active_insn (insn2);
641 return next_active_insn (insn) == insn2;
644 /* Return nonzero if we could reach target from src by falling through,
645 if the target was made adjacent. If we already have a fall-through
646 edge to the exit block, we can't do that. */
647 static bool
648 could_fall_through (basic_block src, basic_block target)
650 edge e;
651 edge_iterator ei;
653 if (target == EXIT_BLOCK_PTR_FOR_FN (cfun))
654 return true;
655 FOR_EACH_EDGE (e, ei, src->succs)
656 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
657 && e->flags & EDGE_FALLTHRU)
658 return 0;
659 return true;
662 /* Return the NOTE_INSN_BASIC_BLOCK of BB. */
664 bb_note (basic_block bb)
666 rtx note;
668 note = BB_HEAD (bb);
669 if (LABEL_P (note))
670 note = NEXT_INSN (note);
672 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
673 return note;
676 /* Return the INSN immediately following the NOTE_INSN_BASIC_BLOCK
677 note associated with the BLOCK. */
679 static rtx
680 first_insn_after_basic_block_note (basic_block block)
682 rtx insn;
684 /* Get the first instruction in the block. */
685 insn = BB_HEAD (block);
687 if (insn == NULL_RTX)
688 return NULL_RTX;
689 if (LABEL_P (insn))
690 insn = NEXT_INSN (insn);
691 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
693 return NEXT_INSN (insn);
696 /* Creates a new basic block just after basic block B by splitting
697 everything after specified instruction I. */
699 static basic_block
700 rtl_split_block (basic_block bb, void *insnp)
702 basic_block new_bb;
703 rtx insn = (rtx) insnp;
704 edge e;
705 edge_iterator ei;
707 if (!insn)
709 insn = first_insn_after_basic_block_note (bb);
711 if (insn)
713 rtx next = insn;
715 insn = PREV_INSN (insn);
717 /* If the block contains only debug insns, insn would have
718 been NULL in a non-debug compilation, and then we'd end
719 up emitting a DELETED note. For -fcompare-debug
720 stability, emit the note too. */
721 if (insn != BB_END (bb)
722 && DEBUG_INSN_P (next)
723 && DEBUG_INSN_P (BB_END (bb)))
725 while (next != BB_END (bb) && DEBUG_INSN_P (next))
726 next = NEXT_INSN (next);
728 if (next == BB_END (bb))
729 emit_note_after (NOTE_INSN_DELETED, next);
732 else
733 insn = get_last_insn ();
736 /* We probably should check type of the insn so that we do not create
737 inconsistent cfg. It is checked in verify_flow_info anyway, so do not
738 bother. */
739 if (insn == BB_END (bb))
740 emit_note_after (NOTE_INSN_DELETED, insn);
742 /* Create the new basic block. */
743 new_bb = create_basic_block (NEXT_INSN (insn), BB_END (bb), bb);
744 BB_COPY_PARTITION (new_bb, bb);
745 BB_END (bb) = insn;
747 /* Redirect the outgoing edges. */
748 new_bb->succs = bb->succs;
749 bb->succs = NULL;
750 FOR_EACH_EDGE (e, ei, new_bb->succs)
751 e->src = new_bb;
753 /* The new block starts off being dirty. */
754 df_set_bb_dirty (bb);
755 return new_bb;
758 /* Return true if the single edge between blocks A and B is the only place
759 in RTL which holds some unique locus. */
761 static bool
762 unique_locus_on_edge_between_p (basic_block a, basic_block b)
764 const location_t goto_locus = EDGE_SUCC (a, 0)->goto_locus;
765 rtx insn, end;
767 if (LOCATION_LOCUS (goto_locus) == UNKNOWN_LOCATION)
768 return false;
770 /* First scan block A backward. */
771 insn = BB_END (a);
772 end = PREV_INSN (BB_HEAD (a));
773 while (insn != end && (!NONDEBUG_INSN_P (insn) || !INSN_HAS_LOCATION (insn)))
774 insn = PREV_INSN (insn);
776 if (insn != end && INSN_LOCATION (insn) == goto_locus)
777 return false;
779 /* Then scan block B forward. */
780 insn = BB_HEAD (b);
781 if (insn)
783 end = NEXT_INSN (BB_END (b));
784 while (insn != end && !NONDEBUG_INSN_P (insn))
785 insn = NEXT_INSN (insn);
787 if (insn != end && INSN_HAS_LOCATION (insn)
788 && INSN_LOCATION (insn) == goto_locus)
789 return false;
792 return true;
795 /* If the single edge between blocks A and B is the only place in RTL which
796 holds some unique locus, emit a nop with that locus between the blocks. */
798 static void
799 emit_nop_for_unique_locus_between (basic_block a, basic_block b)
801 if (!unique_locus_on_edge_between_p (a, b))
802 return;
804 BB_END (a) = emit_insn_after_noloc (gen_nop (), BB_END (a), a);
805 INSN_LOCATION (BB_END (a)) = EDGE_SUCC (a, 0)->goto_locus;
808 /* Blocks A and B are to be merged into a single block A. The insns
809 are already contiguous. */
811 static void
812 rtl_merge_blocks (basic_block a, basic_block b)
814 rtx b_head = BB_HEAD (b), b_end = BB_END (b), a_end = BB_END (a);
815 rtx del_first = NULL_RTX, del_last = NULL_RTX;
816 rtx b_debug_start = b_end, b_debug_end = b_end;
817 bool forwarder_p = (b->flags & BB_FORWARDER_BLOCK) != 0;
818 int b_empty = 0;
820 if (dump_file)
821 fprintf (dump_file, "Merging block %d into block %d...\n", b->index,
822 a->index);
824 while (DEBUG_INSN_P (b_end))
825 b_end = PREV_INSN (b_debug_start = b_end);
827 /* If there was a CODE_LABEL beginning B, delete it. */
828 if (LABEL_P (b_head))
830 /* Detect basic blocks with nothing but a label. This can happen
831 in particular at the end of a function. */
832 if (b_head == b_end)
833 b_empty = 1;
835 del_first = del_last = b_head;
836 b_head = NEXT_INSN (b_head);
839 /* Delete the basic block note and handle blocks containing just that
840 note. */
841 if (NOTE_INSN_BASIC_BLOCK_P (b_head))
843 if (b_head == b_end)
844 b_empty = 1;
845 if (! del_last)
846 del_first = b_head;
848 del_last = b_head;
849 b_head = NEXT_INSN (b_head);
852 /* If there was a jump out of A, delete it. */
853 if (JUMP_P (a_end))
855 rtx prev;
857 for (prev = PREV_INSN (a_end); ; prev = PREV_INSN (prev))
858 if (!NOTE_P (prev)
859 || NOTE_INSN_BASIC_BLOCK_P (prev)
860 || prev == BB_HEAD (a))
861 break;
863 del_first = a_end;
865 #ifdef HAVE_cc0
866 /* If this was a conditional jump, we need to also delete
867 the insn that set cc0. */
868 if (only_sets_cc0_p (prev))
870 rtx tmp = prev;
872 prev = prev_nonnote_insn (prev);
873 if (!prev)
874 prev = BB_HEAD (a);
875 del_first = tmp;
877 #endif
879 a_end = PREV_INSN (del_first);
881 else if (BARRIER_P (NEXT_INSN (a_end)))
882 del_first = NEXT_INSN (a_end);
884 /* Delete everything marked above as well as crap that might be
885 hanging out between the two blocks. */
886 BB_END (a) = a_end;
887 BB_HEAD (b) = b_empty ? NULL_RTX : b_head;
888 delete_insn_chain (del_first, del_last, true);
890 /* When not optimizing CFG and the edge is the only place in RTL which holds
891 some unique locus, emit a nop with that locus in between. */
892 if (!optimize)
894 emit_nop_for_unique_locus_between (a, b);
895 a_end = BB_END (a);
898 /* Reassociate the insns of B with A. */
899 if (!b_empty)
901 update_bb_for_insn_chain (a_end, b_debug_end, a);
903 BB_END (a) = b_debug_end;
904 BB_HEAD (b) = NULL_RTX;
906 else if (b_end != b_debug_end)
908 /* Move any deleted labels and other notes between the end of A
909 and the debug insns that make up B after the debug insns,
910 bringing the debug insns into A while keeping the notes after
911 the end of A. */
912 if (NEXT_INSN (a_end) != b_debug_start)
913 reorder_insns_nobb (NEXT_INSN (a_end), PREV_INSN (b_debug_start),
914 b_debug_end);
915 update_bb_for_insn_chain (b_debug_start, b_debug_end, a);
916 BB_END (a) = b_debug_end;
919 df_bb_delete (b->index);
921 /* If B was a forwarder block, propagate the locus on the edge. */
922 if (forwarder_p
923 && LOCATION_LOCUS (EDGE_SUCC (b, 0)->goto_locus) == UNKNOWN_LOCATION)
924 EDGE_SUCC (b, 0)->goto_locus = EDGE_SUCC (a, 0)->goto_locus;
926 if (dump_file)
927 fprintf (dump_file, "Merged blocks %d and %d.\n", a->index, b->index);
931 /* Return true when block A and B can be merged. */
933 static bool
934 rtl_can_merge_blocks (basic_block a, basic_block b)
936 /* If we are partitioning hot/cold basic blocks, we don't want to
937 mess up unconditional or indirect jumps that cross between hot
938 and cold sections.
940 Basic block partitioning may result in some jumps that appear to
941 be optimizable (or blocks that appear to be mergeable), but which really
942 must be left untouched (they are required to make it safely across
943 partition boundaries). See the comments at the top of
944 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
946 if (BB_PARTITION (a) != BB_PARTITION (b))
947 return false;
949 /* Protect the loop latches. */
950 if (current_loops && b->loop_father->latch == b)
951 return false;
953 /* There must be exactly one edge in between the blocks. */
954 return (single_succ_p (a)
955 && single_succ (a) == b
956 && single_pred_p (b)
957 && a != b
958 /* Must be simple edge. */
959 && !(single_succ_edge (a)->flags & EDGE_COMPLEX)
960 && a->next_bb == b
961 && a != ENTRY_BLOCK_PTR_FOR_FN (cfun)
962 && b != EXIT_BLOCK_PTR_FOR_FN (cfun)
963 /* If the jump insn has side effects,
964 we can't kill the edge. */
965 && (!JUMP_P (BB_END (a))
966 || (reload_completed
967 ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
970 /* Return the label in the head of basic block BLOCK. Create one if it doesn't
971 exist. */
974 block_label (basic_block block)
976 if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
977 return NULL_RTX;
979 if (!LABEL_P (BB_HEAD (block)))
981 BB_HEAD (block) = emit_label_before (gen_label_rtx (), BB_HEAD (block));
984 return BB_HEAD (block);
987 /* Attempt to perform edge redirection by replacing possibly complex jump
988 instruction by unconditional jump or removing jump completely. This can
989 apply only if all edges now point to the same block. The parameters and
990 return values are equivalent to redirect_edge_and_branch. */
992 edge
993 try_redirect_by_replacing_jump (edge e, basic_block target, bool in_cfglayout)
995 basic_block src = e->src;
996 rtx insn = BB_END (src), kill_from;
997 rtx set;
998 int fallthru = 0;
1000 /* If we are partitioning hot/cold basic blocks, we don't want to
1001 mess up unconditional or indirect jumps that cross between hot
1002 and cold sections.
1004 Basic block partitioning may result in some jumps that appear to
1005 be optimizable (or blocks that appear to be mergeable), but which really
1006 must be left untouched (they are required to make it safely across
1007 partition boundaries). See the comments at the top of
1008 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
1010 if (BB_PARTITION (src) != BB_PARTITION (target))
1011 return NULL;
1013 /* We can replace or remove a complex jump only when we have exactly
1014 two edges. Also, if we have exactly one outgoing edge, we can
1015 redirect that. */
1016 if (EDGE_COUNT (src->succs) >= 3
1017 /* Verify that all targets will be TARGET. Specifically, the
1018 edge that is not E must also go to TARGET. */
1019 || (EDGE_COUNT (src->succs) == 2
1020 && EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target))
1021 return NULL;
1023 if (!onlyjump_p (insn))
1024 return NULL;
1025 if ((!optimize || reload_completed) && tablejump_p (insn, NULL, NULL))
1026 return NULL;
1028 /* Avoid removing branch with side effects. */
1029 set = single_set (insn);
1030 if (!set || side_effects_p (set))
1031 return NULL;
1033 /* In case we zap a conditional jump, we'll need to kill
1034 the cc0 setter too. */
1035 kill_from = insn;
1036 #ifdef HAVE_cc0
1037 if (reg_mentioned_p (cc0_rtx, PATTERN (insn))
1038 && only_sets_cc0_p (PREV_INSN (insn)))
1039 kill_from = PREV_INSN (insn);
1040 #endif
1042 /* See if we can create the fallthru edge. */
1043 if (in_cfglayout || can_fallthru (src, target))
1045 if (dump_file)
1046 fprintf (dump_file, "Removing jump %i.\n", INSN_UID (insn));
1047 fallthru = 1;
1049 /* Selectively unlink whole insn chain. */
1050 if (in_cfglayout)
1052 rtx insn = BB_FOOTER (src);
1054 delete_insn_chain (kill_from, BB_END (src), false);
1056 /* Remove barriers but keep jumptables. */
1057 while (insn)
1059 if (BARRIER_P (insn))
1061 if (PREV_INSN (insn))
1062 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
1063 else
1064 BB_FOOTER (src) = NEXT_INSN (insn);
1065 if (NEXT_INSN (insn))
1066 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
1068 if (LABEL_P (insn))
1069 break;
1070 insn = NEXT_INSN (insn);
1073 else
1074 delete_insn_chain (kill_from, PREV_INSN (BB_HEAD (target)),
1075 false);
1078 /* If this already is simplejump, redirect it. */
1079 else if (simplejump_p (insn))
1081 if (e->dest == target)
1082 return NULL;
1083 if (dump_file)
1084 fprintf (dump_file, "Redirecting jump %i from %i to %i.\n",
1085 INSN_UID (insn), e->dest->index, target->index);
1086 if (!redirect_jump (insn, block_label (target), 0))
1088 gcc_assert (target == EXIT_BLOCK_PTR_FOR_FN (cfun));
1089 return NULL;
1093 /* Cannot do anything for target exit block. */
1094 else if (target == EXIT_BLOCK_PTR_FOR_FN (cfun))
1095 return NULL;
1097 /* Or replace possibly complicated jump insn by simple jump insn. */
1098 else
1100 rtx target_label = block_label (target);
1101 rtx barrier, label, table;
1103 emit_jump_insn_after_noloc (gen_jump (target_label), insn);
1104 JUMP_LABEL (BB_END (src)) = target_label;
1105 LABEL_NUSES (target_label)++;
1106 if (dump_file)
1107 fprintf (dump_file, "Replacing insn %i by jump %i\n",
1108 INSN_UID (insn), INSN_UID (BB_END (src)));
1111 delete_insn_chain (kill_from, insn, false);
1113 /* Recognize a tablejump that we are converting to a
1114 simple jump and remove its associated CODE_LABEL
1115 and ADDR_VEC or ADDR_DIFF_VEC. */
1116 if (tablejump_p (insn, &label, &table))
1117 delete_insn_chain (label, table, false);
1119 barrier = next_nonnote_insn (BB_END (src));
1120 if (!barrier || !BARRIER_P (barrier))
1121 emit_barrier_after (BB_END (src));
1122 else
1124 if (barrier != NEXT_INSN (BB_END (src)))
1126 /* Move the jump before barrier so that the notes
1127 which originally were or were created before jump table are
1128 inside the basic block. */
1129 rtx new_insn = BB_END (src);
1131 update_bb_for_insn_chain (NEXT_INSN (BB_END (src)),
1132 PREV_INSN (barrier), src);
1134 NEXT_INSN (PREV_INSN (new_insn)) = NEXT_INSN (new_insn);
1135 PREV_INSN (NEXT_INSN (new_insn)) = PREV_INSN (new_insn);
1137 NEXT_INSN (new_insn) = barrier;
1138 NEXT_INSN (PREV_INSN (barrier)) = new_insn;
1140 PREV_INSN (new_insn) = PREV_INSN (barrier);
1141 PREV_INSN (barrier) = new_insn;
1146 /* Keep only one edge out and set proper flags. */
1147 if (!single_succ_p (src))
1148 remove_edge (e);
1149 gcc_assert (single_succ_p (src));
1151 e = single_succ_edge (src);
1152 if (fallthru)
1153 e->flags = EDGE_FALLTHRU;
1154 else
1155 e->flags = 0;
1157 e->probability = REG_BR_PROB_BASE;
1158 e->count = src->count;
1160 if (e->dest != target)
1161 redirect_edge_succ (e, target);
1162 return e;
1165 /* Subroutine of redirect_branch_edge that tries to patch the jump
1166 instruction INSN so that it reaches block NEW. Do this
1167 only when it originally reached block OLD. Return true if this
1168 worked or the original target wasn't OLD, return false if redirection
1169 doesn't work. */
1171 static bool
1172 patch_jump_insn (rtx insn, rtx old_label, basic_block new_bb)
1174 rtx tmp;
1175 /* Recognize a tablejump and adjust all matching cases. */
1176 if (tablejump_p (insn, NULL, &tmp))
1178 rtvec vec;
1179 int j;
1180 rtx new_label = block_label (new_bb);
1182 if (new_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1183 return false;
1184 if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
1185 vec = XVEC (PATTERN (tmp), 0);
1186 else
1187 vec = XVEC (PATTERN (tmp), 1);
1189 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
1190 if (XEXP (RTVEC_ELT (vec, j), 0) == old_label)
1192 RTVEC_ELT (vec, j) = gen_rtx_LABEL_REF (Pmode, new_label);
1193 --LABEL_NUSES (old_label);
1194 ++LABEL_NUSES (new_label);
1197 /* Handle casesi dispatch insns. */
1198 if ((tmp = single_set (insn)) != NULL
1199 && SET_DEST (tmp) == pc_rtx
1200 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
1201 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF
1202 && XEXP (XEXP (SET_SRC (tmp), 2), 0) == old_label)
1204 XEXP (SET_SRC (tmp), 2) = gen_rtx_LABEL_REF (Pmode,
1205 new_label);
1206 --LABEL_NUSES (old_label);
1207 ++LABEL_NUSES (new_label);
1210 else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL)
1212 int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp);
1213 rtx new_label, note;
1215 if (new_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1216 return false;
1217 new_label = block_label (new_bb);
1219 for (i = 0; i < n; ++i)
1221 rtx old_ref = ASM_OPERANDS_LABEL (tmp, i);
1222 gcc_assert (GET_CODE (old_ref) == LABEL_REF);
1223 if (XEXP (old_ref, 0) == old_label)
1225 ASM_OPERANDS_LABEL (tmp, i)
1226 = gen_rtx_LABEL_REF (Pmode, new_label);
1227 --LABEL_NUSES (old_label);
1228 ++LABEL_NUSES (new_label);
1232 if (JUMP_LABEL (insn) == old_label)
1234 JUMP_LABEL (insn) = new_label;
1235 note = find_reg_note (insn, REG_LABEL_TARGET, new_label);
1236 if (note)
1237 remove_note (insn, note);
1239 else
1241 note = find_reg_note (insn, REG_LABEL_TARGET, old_label);
1242 if (note)
1243 remove_note (insn, note);
1244 if (JUMP_LABEL (insn) != new_label
1245 && !find_reg_note (insn, REG_LABEL_TARGET, new_label))
1246 add_reg_note (insn, REG_LABEL_TARGET, new_label);
1248 while ((note = find_reg_note (insn, REG_LABEL_OPERAND, old_label))
1249 != NULL_RTX)
1250 XEXP (note, 0) = new_label;
1252 else
1254 /* ?? We may play the games with moving the named labels from
1255 one basic block to the other in case only one computed_jump is
1256 available. */
1257 if (computed_jump_p (insn)
1258 /* A return instruction can't be redirected. */
1259 || returnjump_p (insn))
1260 return false;
1262 if (!currently_expanding_to_rtl || JUMP_LABEL (insn) == old_label)
1264 /* If the insn doesn't go where we think, we're confused. */
1265 gcc_assert (JUMP_LABEL (insn) == old_label);
1267 /* If the substitution doesn't succeed, die. This can happen
1268 if the back end emitted unrecognizable instructions or if
1269 target is exit block on some arches. */
1270 if (!redirect_jump (insn, block_label (new_bb), 0))
1272 gcc_assert (new_bb == EXIT_BLOCK_PTR_FOR_FN (cfun));
1273 return false;
1277 return true;
1281 /* Redirect edge representing branch of (un)conditional jump or tablejump,
1282 NULL on failure */
1283 static edge
1284 redirect_branch_edge (edge e, basic_block target)
1286 rtx old_label = BB_HEAD (e->dest);
1287 basic_block src = e->src;
1288 rtx insn = BB_END (src);
1290 /* We can only redirect non-fallthru edges of jump insn. */
1291 if (e->flags & EDGE_FALLTHRU)
1292 return NULL;
1293 else if (!JUMP_P (insn) && !currently_expanding_to_rtl)
1294 return NULL;
1296 if (!currently_expanding_to_rtl)
1298 if (!patch_jump_insn (insn, old_label, target))
1299 return NULL;
1301 else
1302 /* When expanding this BB might actually contain multiple
1303 jumps (i.e. not yet split by find_many_sub_basic_blocks).
1304 Redirect all of those that match our label. */
1305 FOR_BB_INSNS (src, insn)
1306 if (JUMP_P (insn) && !patch_jump_insn (insn, old_label, target))
1307 return NULL;
1309 if (dump_file)
1310 fprintf (dump_file, "Edge %i->%i redirected to %i\n",
1311 e->src->index, e->dest->index, target->index);
1313 if (e->dest != target)
1314 e = redirect_edge_succ_nodup (e, target);
1316 return e;
1319 /* Called when edge E has been redirected to a new destination,
1320 in order to update the region crossing flag on the edge and
1321 jump. */
1323 static void
1324 fixup_partition_crossing (edge e)
1326 rtx note;
1328 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun) || e->dest
1329 == EXIT_BLOCK_PTR_FOR_FN (cfun))
1330 return;
1331 /* If we redirected an existing edge, it may already be marked
1332 crossing, even though the new src is missing a reg crossing note.
1333 But make sure reg crossing note doesn't already exist before
1334 inserting. */
1335 if (BB_PARTITION (e->src) != BB_PARTITION (e->dest))
1337 e->flags |= EDGE_CROSSING;
1338 note = find_reg_note (BB_END (e->src), REG_CROSSING_JUMP, NULL_RTX);
1339 if (JUMP_P (BB_END (e->src))
1340 && !note)
1341 add_reg_note (BB_END (e->src), REG_CROSSING_JUMP, NULL_RTX);
1343 else if (BB_PARTITION (e->src) == BB_PARTITION (e->dest))
1345 e->flags &= ~EDGE_CROSSING;
1346 /* Remove the section crossing note from jump at end of
1347 src if it exists, and if no other successors are
1348 still crossing. */
1349 note = find_reg_note (BB_END (e->src), REG_CROSSING_JUMP, NULL_RTX);
1350 if (note)
1352 bool has_crossing_succ = false;
1353 edge e2;
1354 edge_iterator ei;
1355 FOR_EACH_EDGE (e2, ei, e->src->succs)
1357 has_crossing_succ |= (e2->flags & EDGE_CROSSING);
1358 if (has_crossing_succ)
1359 break;
1361 if (!has_crossing_succ)
1362 remove_note (BB_END (e->src), note);
1367 /* Called when block BB has been reassigned to the cold partition,
1368 because it is now dominated by another cold block,
1369 to ensure that the region crossing attributes are updated. */
1371 static void
1372 fixup_new_cold_bb (basic_block bb)
1374 edge e;
1375 edge_iterator ei;
1377 /* This is called when a hot bb is found to now be dominated
1378 by a cold bb and therefore needs to become cold. Therefore,
1379 its preds will no longer be region crossing. Any non-dominating
1380 preds that were previously hot would also have become cold
1381 in the caller for the same region. Any preds that were previously
1382 region-crossing will be adjusted in fixup_partition_crossing. */
1383 FOR_EACH_EDGE (e, ei, bb->preds)
1385 fixup_partition_crossing (e);
1388 /* Possibly need to make bb's successor edges region crossing,
1389 or remove stale region crossing. */
1390 FOR_EACH_EDGE (e, ei, bb->succs)
1392 /* We can't have fall-through edges across partition boundaries.
1393 Note that force_nonfallthru will do any necessary partition
1394 boundary fixup by calling fixup_partition_crossing itself. */
1395 if ((e->flags & EDGE_FALLTHRU)
1396 && BB_PARTITION (bb) != BB_PARTITION (e->dest)
1397 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1398 force_nonfallthru (e);
1399 else
1400 fixup_partition_crossing (e);
1404 /* Attempt to change code to redirect edge E to TARGET. Don't do that on
1405 expense of adding new instructions or reordering basic blocks.
1407 Function can be also called with edge destination equivalent to the TARGET.
1408 Then it should try the simplifications and do nothing if none is possible.
1410 Return edge representing the branch if transformation succeeded. Return NULL
1411 on failure.
1412 We still return NULL in case E already destinated TARGET and we didn't
1413 managed to simplify instruction stream. */
1415 static edge
1416 rtl_redirect_edge_and_branch (edge e, basic_block target)
1418 edge ret;
1419 basic_block src = e->src;
1420 basic_block dest = e->dest;
1422 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
1423 return NULL;
1425 if (dest == target)
1426 return e;
1428 if ((ret = try_redirect_by_replacing_jump (e, target, false)) != NULL)
1430 df_set_bb_dirty (src);
1431 fixup_partition_crossing (ret);
1432 return ret;
1435 ret = redirect_branch_edge (e, target);
1436 if (!ret)
1437 return NULL;
1439 df_set_bb_dirty (src);
1440 fixup_partition_crossing (ret);
1441 return ret;
1444 /* Emit a barrier after BB, into the footer if we are in CFGLAYOUT mode. */
1446 void
1447 emit_barrier_after_bb (basic_block bb)
1449 rtx barrier = emit_barrier_after (BB_END (bb));
1450 gcc_assert (current_ir_type () == IR_RTL_CFGRTL
1451 || current_ir_type () == IR_RTL_CFGLAYOUT);
1452 if (current_ir_type () == IR_RTL_CFGLAYOUT)
1453 BB_FOOTER (bb) = unlink_insn_chain (barrier, barrier);
1456 /* Like force_nonfallthru below, but additionally performs redirection
1457 Used by redirect_edge_and_branch_force. JUMP_LABEL is used only
1458 when redirecting to the EXIT_BLOCK, it is either ret_rtx or
1459 simple_return_rtx, indicating which kind of returnjump to create.
1460 It should be NULL otherwise. */
1462 basic_block
1463 force_nonfallthru_and_redirect (edge e, basic_block target, rtx jump_label)
1465 basic_block jump_block, new_bb = NULL, src = e->src;
1466 rtx note;
1467 edge new_edge;
1468 int abnormal_edge_flags = 0;
1469 bool asm_goto_edge = false;
1470 int loc;
1472 /* In the case the last instruction is conditional jump to the next
1473 instruction, first redirect the jump itself and then continue
1474 by creating a basic block afterwards to redirect fallthru edge. */
1475 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
1476 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
1477 && any_condjump_p (BB_END (e->src))
1478 && JUMP_LABEL (BB_END (e->src)) == BB_HEAD (e->dest))
1480 rtx note;
1481 edge b = unchecked_make_edge (e->src, target, 0);
1482 bool redirected;
1484 redirected = redirect_jump (BB_END (e->src), block_label (target), 0);
1485 gcc_assert (redirected);
1487 note = find_reg_note (BB_END (e->src), REG_BR_PROB, NULL_RTX);
1488 if (note)
1490 int prob = XINT (note, 0);
1492 b->probability = prob;
1493 /* Update this to use GCOV_COMPUTE_SCALE. */
1494 b->count = e->count * prob / REG_BR_PROB_BASE;
1495 e->probability -= e->probability;
1496 e->count -= b->count;
1497 if (e->probability < 0)
1498 e->probability = 0;
1499 if (e->count < 0)
1500 e->count = 0;
1504 if (e->flags & EDGE_ABNORMAL)
1506 /* Irritating special case - fallthru edge to the same block as abnormal
1507 edge.
1508 We can't redirect abnormal edge, but we still can split the fallthru
1509 one and create separate abnormal edge to original destination.
1510 This allows bb-reorder to make such edge non-fallthru. */
1511 gcc_assert (e->dest == target);
1512 abnormal_edge_flags = e->flags & ~EDGE_FALLTHRU;
1513 e->flags &= EDGE_FALLTHRU;
1515 else
1517 gcc_assert (e->flags & EDGE_FALLTHRU);
1518 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1520 /* We can't redirect the entry block. Create an empty block
1521 at the start of the function which we use to add the new
1522 jump. */
1523 edge tmp;
1524 edge_iterator ei;
1525 bool found = false;
1527 basic_block bb = create_basic_block (BB_HEAD (e->dest), NULL,
1528 ENTRY_BLOCK_PTR_FOR_FN (cfun));
1530 /* Change the existing edge's source to be the new block, and add
1531 a new edge from the entry block to the new block. */
1532 e->src = bb;
1533 for (ei = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
1534 (tmp = ei_safe_edge (ei)); )
1536 if (tmp == e)
1538 ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs->unordered_remove (ei.index);
1539 found = true;
1540 break;
1542 else
1543 ei_next (&ei);
1546 gcc_assert (found);
1548 vec_safe_push (bb->succs, e);
1549 make_single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb,
1550 EDGE_FALLTHRU);
1554 /* If e->src ends with asm goto, see if any of the ASM_OPERANDS_LABELs
1555 don't point to the target or fallthru label. */
1556 if (JUMP_P (BB_END (e->src))
1557 && target != EXIT_BLOCK_PTR_FOR_FN (cfun)
1558 && (e->flags & EDGE_FALLTHRU)
1559 && (note = extract_asm_operands (PATTERN (BB_END (e->src)))))
1561 int i, n = ASM_OPERANDS_LABEL_LENGTH (note);
1562 bool adjust_jump_target = false;
1564 for (i = 0; i < n; ++i)
1566 if (XEXP (ASM_OPERANDS_LABEL (note, i), 0) == BB_HEAD (e->dest))
1568 LABEL_NUSES (XEXP (ASM_OPERANDS_LABEL (note, i), 0))--;
1569 XEXP (ASM_OPERANDS_LABEL (note, i), 0) = block_label (target);
1570 LABEL_NUSES (XEXP (ASM_OPERANDS_LABEL (note, i), 0))++;
1571 adjust_jump_target = true;
1573 if (XEXP (ASM_OPERANDS_LABEL (note, i), 0) == BB_HEAD (target))
1574 asm_goto_edge = true;
1576 if (adjust_jump_target)
1578 rtx insn = BB_END (e->src), note;
1579 rtx old_label = BB_HEAD (e->dest);
1580 rtx new_label = BB_HEAD (target);
1582 if (JUMP_LABEL (insn) == old_label)
1584 JUMP_LABEL (insn) = new_label;
1585 note = find_reg_note (insn, REG_LABEL_TARGET, new_label);
1586 if (note)
1587 remove_note (insn, note);
1589 else
1591 note = find_reg_note (insn, REG_LABEL_TARGET, old_label);
1592 if (note)
1593 remove_note (insn, note);
1594 if (JUMP_LABEL (insn) != new_label
1595 && !find_reg_note (insn, REG_LABEL_TARGET, new_label))
1596 add_reg_note (insn, REG_LABEL_TARGET, new_label);
1598 while ((note = find_reg_note (insn, REG_LABEL_OPERAND, old_label))
1599 != NULL_RTX)
1600 XEXP (note, 0) = new_label;
1604 if (EDGE_COUNT (e->src->succs) >= 2 || abnormal_edge_flags || asm_goto_edge)
1606 gcov_type count = e->count;
1607 int probability = e->probability;
1608 /* Create the new structures. */
1610 /* If the old block ended with a tablejump, skip its table
1611 by searching forward from there. Otherwise start searching
1612 forward from the last instruction of the old block. */
1613 if (!tablejump_p (BB_END (e->src), NULL, &note))
1614 note = BB_END (e->src);
1615 note = NEXT_INSN (note);
1617 jump_block = create_basic_block (note, NULL, e->src);
1618 jump_block->count = count;
1619 jump_block->frequency = EDGE_FREQUENCY (e);
1621 /* Make sure new block ends up in correct hot/cold section. */
1623 BB_COPY_PARTITION (jump_block, e->src);
1625 /* Wire edge in. */
1626 new_edge = make_edge (e->src, jump_block, EDGE_FALLTHRU);
1627 new_edge->probability = probability;
1628 new_edge->count = count;
1630 /* Redirect old edge. */
1631 redirect_edge_pred (e, jump_block);
1632 e->probability = REG_BR_PROB_BASE;
1634 /* If e->src was previously region crossing, it no longer is
1635 and the reg crossing note should be removed. */
1636 fixup_partition_crossing (new_edge);
1638 /* If asm goto has any label refs to target's label,
1639 add also edge from asm goto bb to target. */
1640 if (asm_goto_edge)
1642 new_edge->probability /= 2;
1643 new_edge->count /= 2;
1644 jump_block->count /= 2;
1645 jump_block->frequency /= 2;
1646 new_edge = make_edge (new_edge->src, target,
1647 e->flags & ~EDGE_FALLTHRU);
1648 new_edge->probability = probability - probability / 2;
1649 new_edge->count = count - count / 2;
1652 new_bb = jump_block;
1654 else
1655 jump_block = e->src;
1657 loc = e->goto_locus;
1658 e->flags &= ~EDGE_FALLTHRU;
1659 if (target == EXIT_BLOCK_PTR_FOR_FN (cfun))
1661 if (jump_label == ret_rtx)
1663 #ifdef HAVE_return
1664 emit_jump_insn_after_setloc (gen_return (), BB_END (jump_block), loc);
1665 #else
1666 gcc_unreachable ();
1667 #endif
1669 else
1671 gcc_assert (jump_label == simple_return_rtx);
1672 #ifdef HAVE_simple_return
1673 emit_jump_insn_after_setloc (gen_simple_return (),
1674 BB_END (jump_block), loc);
1675 #else
1676 gcc_unreachable ();
1677 #endif
1679 set_return_jump_label (BB_END (jump_block));
1681 else
1683 rtx label = block_label (target);
1684 emit_jump_insn_after_setloc (gen_jump (label), BB_END (jump_block), loc);
1685 JUMP_LABEL (BB_END (jump_block)) = label;
1686 LABEL_NUSES (label)++;
1689 /* We might be in cfg layout mode, and if so, the following routine will
1690 insert the barrier correctly. */
1691 emit_barrier_after_bb (jump_block);
1692 redirect_edge_succ_nodup (e, target);
1694 if (abnormal_edge_flags)
1695 make_edge (src, target, abnormal_edge_flags);
1697 df_mark_solutions_dirty ();
1698 fixup_partition_crossing (e);
1699 return new_bb;
1702 /* Edge E is assumed to be fallthru edge. Emit needed jump instruction
1703 (and possibly create new basic block) to make edge non-fallthru.
1704 Return newly created BB or NULL if none. */
1706 static basic_block
1707 rtl_force_nonfallthru (edge e)
1709 return force_nonfallthru_and_redirect (e, e->dest, NULL_RTX);
1712 /* Redirect edge even at the expense of creating new jump insn or
1713 basic block. Return new basic block if created, NULL otherwise.
1714 Conversion must be possible. */
1716 static basic_block
1717 rtl_redirect_edge_and_branch_force (edge e, basic_block target)
1719 if (redirect_edge_and_branch (e, target)
1720 || e->dest == target)
1721 return NULL;
1723 /* In case the edge redirection failed, try to force it to be non-fallthru
1724 and redirect newly created simplejump. */
1725 df_set_bb_dirty (e->src);
1726 return force_nonfallthru_and_redirect (e, target, NULL_RTX);
1729 /* The given edge should potentially be a fallthru edge. If that is in
1730 fact true, delete the jump and barriers that are in the way. */
1732 static void
1733 rtl_tidy_fallthru_edge (edge e)
1735 rtx q;
1736 basic_block b = e->src, c = b->next_bb;
1738 /* ??? In a late-running flow pass, other folks may have deleted basic
1739 blocks by nopping out blocks, leaving multiple BARRIERs between here
1740 and the target label. They ought to be chastised and fixed.
1742 We can also wind up with a sequence of undeletable labels between
1743 one block and the next.
1745 So search through a sequence of barriers, labels, and notes for
1746 the head of block C and assert that we really do fall through. */
1748 for (q = NEXT_INSN (BB_END (b)); q != BB_HEAD (c); q = NEXT_INSN (q))
1749 if (INSN_P (q))
1750 return;
1752 /* Remove what will soon cease being the jump insn from the source block.
1753 If block B consisted only of this single jump, turn it into a deleted
1754 note. */
1755 q = BB_END (b);
1756 if (JUMP_P (q)
1757 && onlyjump_p (q)
1758 && (any_uncondjump_p (q)
1759 || single_succ_p (b)))
1761 #ifdef HAVE_cc0
1762 /* If this was a conditional jump, we need to also delete
1763 the insn that set cc0. */
1764 if (any_condjump_p (q) && only_sets_cc0_p (PREV_INSN (q)))
1765 q = PREV_INSN (q);
1766 #endif
1768 q = PREV_INSN (q);
1771 /* Selectively unlink the sequence. */
1772 if (q != PREV_INSN (BB_HEAD (c)))
1773 delete_insn_chain (NEXT_INSN (q), PREV_INSN (BB_HEAD (c)), false);
1775 e->flags |= EDGE_FALLTHRU;
1778 /* Should move basic block BB after basic block AFTER. NIY. */
1780 static bool
1781 rtl_move_block_after (basic_block bb ATTRIBUTE_UNUSED,
1782 basic_block after ATTRIBUTE_UNUSED)
1784 return false;
1787 /* Locate the last bb in the same partition as START_BB. */
1789 static basic_block
1790 last_bb_in_partition (basic_block start_bb)
1792 basic_block bb;
1793 FOR_BB_BETWEEN (bb, start_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
1795 if (BB_PARTITION (start_bb) != BB_PARTITION (bb->next_bb))
1796 return bb;
1798 /* Return bb before the exit block. */
1799 return bb->prev_bb;
1802 /* Split a (typically critical) edge. Return the new block.
1803 The edge must not be abnormal.
1805 ??? The code generally expects to be called on critical edges.
1806 The case of a block ending in an unconditional jump to a
1807 block with multiple predecessors is not handled optimally. */
1809 static basic_block
1810 rtl_split_edge (edge edge_in)
1812 basic_block bb, new_bb;
1813 rtx before;
1815 /* Abnormal edges cannot be split. */
1816 gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
1818 /* We are going to place the new block in front of edge destination.
1819 Avoid existence of fallthru predecessors. */
1820 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1822 edge e = find_fallthru_edge (edge_in->dest->preds);
1824 if (e)
1825 force_nonfallthru (e);
1828 /* Create the basic block note. */
1829 if (edge_in->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1830 before = BB_HEAD (edge_in->dest);
1831 else
1832 before = NULL_RTX;
1834 /* If this is a fall through edge to the exit block, the blocks might be
1835 not adjacent, and the right place is after the source. */
1836 if ((edge_in->flags & EDGE_FALLTHRU)
1837 && edge_in->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1839 before = NEXT_INSN (BB_END (edge_in->src));
1840 bb = create_basic_block (before, NULL, edge_in->src);
1841 BB_COPY_PARTITION (bb, edge_in->src);
1843 else
1845 if (edge_in->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1847 bb = create_basic_block (before, NULL, edge_in->dest->prev_bb);
1848 BB_COPY_PARTITION (bb, edge_in->dest);
1850 else
1852 basic_block after = edge_in->dest->prev_bb;
1853 /* If this is post-bb reordering, and the edge crosses a partition
1854 boundary, the new block needs to be inserted in the bb chain
1855 at the end of the src partition (since we put the new bb into
1856 that partition, see below). Otherwise we may end up creating
1857 an extra partition crossing in the chain, which is illegal.
1858 It can't go after the src, because src may have a fall-through
1859 to a different block. */
1860 if (crtl->bb_reorder_complete
1861 && (edge_in->flags & EDGE_CROSSING))
1863 after = last_bb_in_partition (edge_in->src);
1864 before = NEXT_INSN (BB_END (after));
1865 /* The instruction following the last bb in partition should
1866 be a barrier, since it cannot end in a fall-through. */
1867 gcc_checking_assert (BARRIER_P (before));
1868 before = NEXT_INSN (before);
1870 bb = create_basic_block (before, NULL, after);
1871 /* Put the split bb into the src partition, to avoid creating
1872 a situation where a cold bb dominates a hot bb, in the case
1873 where src is cold and dest is hot. The src will dominate
1874 the new bb (whereas it might not have dominated dest). */
1875 BB_COPY_PARTITION (bb, edge_in->src);
1879 make_single_succ_edge (bb, edge_in->dest, EDGE_FALLTHRU);
1881 /* Can't allow a region crossing edge to be fallthrough. */
1882 if (BB_PARTITION (bb) != BB_PARTITION (edge_in->dest)
1883 && edge_in->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1885 new_bb = force_nonfallthru (single_succ_edge (bb));
1886 gcc_assert (!new_bb);
1889 /* For non-fallthru edges, we must adjust the predecessor's
1890 jump instruction to target our new block. */
1891 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1893 edge redirected = redirect_edge_and_branch (edge_in, bb);
1894 gcc_assert (redirected);
1896 else
1898 if (edge_in->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1900 /* For asm goto even splitting of fallthru edge might
1901 need insn patching, as other labels might point to the
1902 old label. */
1903 rtx last = BB_END (edge_in->src);
1904 if (last
1905 && JUMP_P (last)
1906 && edge_in->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
1907 && extract_asm_operands (PATTERN (last)) != NULL_RTX
1908 && patch_jump_insn (last, before, bb))
1909 df_set_bb_dirty (edge_in->src);
1911 redirect_edge_succ (edge_in, bb);
1914 return bb;
1917 /* Queue instructions for insertion on an edge between two basic blocks.
1918 The new instructions and basic blocks (if any) will not appear in the
1919 CFG until commit_edge_insertions is called. */
1921 void
1922 insert_insn_on_edge (rtx pattern, edge e)
1924 /* We cannot insert instructions on an abnormal critical edge.
1925 It will be easier to find the culprit if we die now. */
1926 gcc_assert (!((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e)));
1928 if (e->insns.r == NULL_RTX)
1929 start_sequence ();
1930 else
1931 push_to_sequence (e->insns.r);
1933 emit_insn (pattern);
1935 e->insns.r = get_insns ();
1936 end_sequence ();
1939 /* Update the CFG for the instructions queued on edge E. */
1941 void
1942 commit_one_edge_insertion (edge e)
1944 rtx before = NULL_RTX, after = NULL_RTX, insns, tmp, last;
1945 basic_block bb;
1947 /* Pull the insns off the edge now since the edge might go away. */
1948 insns = e->insns.r;
1949 e->insns.r = NULL_RTX;
1951 /* Figure out where to put these insns. If the destination has
1952 one predecessor, insert there. Except for the exit block. */
1953 if (single_pred_p (e->dest) && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1955 bb = e->dest;
1957 /* Get the location correct wrt a code label, and "nice" wrt
1958 a basic block note, and before everything else. */
1959 tmp = BB_HEAD (bb);
1960 if (LABEL_P (tmp))
1961 tmp = NEXT_INSN (tmp);
1962 if (NOTE_INSN_BASIC_BLOCK_P (tmp))
1963 tmp = NEXT_INSN (tmp);
1964 if (tmp == BB_HEAD (bb))
1965 before = tmp;
1966 else if (tmp)
1967 after = PREV_INSN (tmp);
1968 else
1969 after = get_last_insn ();
1972 /* If the source has one successor and the edge is not abnormal,
1973 insert there. Except for the entry block.
1974 Don't do this if the predecessor ends in a jump other than
1975 unconditional simple jump. E.g. for asm goto that points all
1976 its labels at the fallthru basic block, we can't insert instructions
1977 before the asm goto, as the asm goto can have various of side effects,
1978 and can't emit instructions after the asm goto, as it must end
1979 the basic block. */
1980 else if ((e->flags & EDGE_ABNORMAL) == 0
1981 && single_succ_p (e->src)
1982 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
1983 && (!JUMP_P (BB_END (e->src))
1984 || simplejump_p (BB_END (e->src))))
1986 bb = e->src;
1988 /* It is possible to have a non-simple jump here. Consider a target
1989 where some forms of unconditional jumps clobber a register. This
1990 happens on the fr30 for example.
1992 We know this block has a single successor, so we can just emit
1993 the queued insns before the jump. */
1994 if (JUMP_P (BB_END (bb)))
1995 before = BB_END (bb);
1996 else
1998 /* We'd better be fallthru, or we've lost track of what's what. */
1999 gcc_assert (e->flags & EDGE_FALLTHRU);
2001 after = BB_END (bb);
2005 /* Otherwise we must split the edge. */
2006 else
2008 bb = split_edge (e);
2010 /* If E crossed a partition boundary, we needed to make bb end in
2011 a region-crossing jump, even though it was originally fallthru. */
2012 if (JUMP_P (BB_END (bb)))
2013 before = BB_END (bb);
2014 else
2015 after = BB_END (bb);
2018 /* Now that we've found the spot, do the insertion. */
2019 if (before)
2021 emit_insn_before_noloc (insns, before, bb);
2022 last = prev_nonnote_insn (before);
2024 else
2025 last = emit_insn_after_noloc (insns, after, bb);
2027 if (returnjump_p (last))
2029 /* ??? Remove all outgoing edges from BB and add one for EXIT.
2030 This is not currently a problem because this only happens
2031 for the (single) epilogue, which already has a fallthru edge
2032 to EXIT. */
2034 e = single_succ_edge (bb);
2035 gcc_assert (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
2036 && single_succ_p (bb) && (e->flags & EDGE_FALLTHRU));
2038 e->flags &= ~EDGE_FALLTHRU;
2039 emit_barrier_after (last);
2041 if (before)
2042 delete_insn (before);
2044 else
2045 gcc_assert (!JUMP_P (last));
2048 /* Update the CFG for all queued instructions. */
2050 void
2051 commit_edge_insertions (void)
2053 basic_block bb;
2055 /* Optimization passes that invoke this routine can cause hot blocks
2056 previously reached by both hot and cold blocks to become dominated only
2057 by cold blocks. This will cause the verification below to fail,
2058 and lead to now cold code in the hot section. In some cases this
2059 may only be visible after newly unreachable blocks are deleted,
2060 which will be done by fixup_partitions. */
2061 fixup_partitions ();
2063 #ifdef ENABLE_CHECKING
2064 verify_flow_info ();
2065 #endif
2067 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
2068 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
2070 edge e;
2071 edge_iterator ei;
2073 FOR_EACH_EDGE (e, ei, bb->succs)
2074 if (e->insns.r)
2075 commit_one_edge_insertion (e);
2080 /* Print out RTL-specific basic block information (live information
2081 at start and end with TDF_DETAILS). FLAGS are the TDF_* masks
2082 documented in dumpfile.h. */
2084 static void
2085 rtl_dump_bb (FILE *outf, basic_block bb, int indent, int flags)
2087 rtx insn;
2088 rtx last;
2089 char *s_indent;
2091 s_indent = (char *) alloca ((size_t) indent + 1);
2092 memset (s_indent, ' ', (size_t) indent);
2093 s_indent[indent] = '\0';
2095 if (df && (flags & TDF_DETAILS))
2097 df_dump_top (bb, outf);
2098 putc ('\n', outf);
2101 if (bb->index != ENTRY_BLOCK && bb->index != EXIT_BLOCK)
2102 for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb)); insn != last;
2103 insn = NEXT_INSN (insn))
2105 if (flags & TDF_DETAILS)
2106 df_dump_insn_top (insn, outf);
2107 if (! (flags & TDF_SLIM))
2108 print_rtl_single (outf, insn);
2109 else
2110 dump_insn_slim (outf, insn);
2111 if (flags & TDF_DETAILS)
2112 df_dump_insn_bottom (insn, outf);
2115 if (df && (flags & TDF_DETAILS))
2117 df_dump_bottom (bb, outf);
2118 putc ('\n', outf);
2123 /* Like dump_function_to_file, but for RTL. Print out dataflow information
2124 for the start of each basic block. FLAGS are the TDF_* masks documented
2125 in dumpfile.h. */
2127 void
2128 print_rtl_with_bb (FILE *outf, const_rtx rtx_first, int flags)
2130 const_rtx tmp_rtx;
2131 if (rtx_first == 0)
2132 fprintf (outf, "(nil)\n");
2133 else
2135 enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
2136 int max_uid = get_max_uid ();
2137 basic_block *start = XCNEWVEC (basic_block, max_uid);
2138 basic_block *end = XCNEWVEC (basic_block, max_uid);
2139 enum bb_state *in_bb_p = XCNEWVEC (enum bb_state, max_uid);
2140 basic_block bb;
2142 /* After freeing the CFG, we still have BLOCK_FOR_INSN set on most
2143 insns, but the CFG is not maintained so the basic block info
2144 is not reliable. Therefore it's omitted from the dumps. */
2145 if (! (cfun->curr_properties & PROP_cfg))
2146 flags &= ~TDF_BLOCKS;
2148 if (df)
2149 df_dump_start (outf);
2151 if (flags & TDF_BLOCKS)
2153 FOR_EACH_BB_REVERSE (bb)
2155 rtx x;
2157 start[INSN_UID (BB_HEAD (bb))] = bb;
2158 end[INSN_UID (BB_END (bb))] = bb;
2159 for (x = BB_HEAD (bb); x != NULL_RTX; x = NEXT_INSN (x))
2161 enum bb_state state = IN_MULTIPLE_BB;
2163 if (in_bb_p[INSN_UID (x)] == NOT_IN_BB)
2164 state = IN_ONE_BB;
2165 in_bb_p[INSN_UID (x)] = state;
2167 if (x == BB_END (bb))
2168 break;
2173 for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
2175 if (flags & TDF_BLOCKS)
2177 bb = start[INSN_UID (tmp_rtx)];
2178 if (bb != NULL)
2180 dump_bb_info (outf, bb, 0, dump_flags | TDF_COMMENT, true, false);
2181 if (df && (flags & TDF_DETAILS))
2182 df_dump_top (bb, outf);
2185 if (in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB
2186 && !NOTE_P (tmp_rtx)
2187 && !BARRIER_P (tmp_rtx))
2188 fprintf (outf, ";; Insn is not within a basic block\n");
2189 else if (in_bb_p[INSN_UID (tmp_rtx)] == IN_MULTIPLE_BB)
2190 fprintf (outf, ";; Insn is in multiple basic blocks\n");
2193 if (flags & TDF_DETAILS)
2194 df_dump_insn_top (tmp_rtx, outf);
2195 if (! (flags & TDF_SLIM))
2196 print_rtl_single (outf, tmp_rtx);
2197 else
2198 dump_insn_slim (outf, tmp_rtx);
2199 if (flags & TDF_DETAILS)
2200 df_dump_insn_bottom (tmp_rtx, outf);
2202 if (flags & TDF_BLOCKS)
2204 bb = end[INSN_UID (tmp_rtx)];
2205 if (bb != NULL)
2207 dump_bb_info (outf, bb, 0, dump_flags | TDF_COMMENT, false, true);
2208 if (df && (flags & TDF_DETAILS))
2209 df_dump_bottom (bb, outf);
2210 putc ('\n', outf);
2215 free (start);
2216 free (end);
2217 free (in_bb_p);
2221 /* Update the branch probability of BB if a REG_BR_PROB is present. */
2223 void
2224 update_br_prob_note (basic_block bb)
2226 rtx note;
2227 if (!JUMP_P (BB_END (bb)))
2228 return;
2229 note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX);
2230 if (!note || XINT (note, 0) == BRANCH_EDGE (bb)->probability)
2231 return;
2232 XINT (note, 0) = BRANCH_EDGE (bb)->probability;
2235 /* Get the last insn associated with block BB (that includes barriers and
2236 tablejumps after BB). */
2238 get_last_bb_insn (basic_block bb)
2240 rtx tmp;
2241 rtx end = BB_END (bb);
2243 /* Include any jump table following the basic block. */
2244 if (tablejump_p (end, NULL, &tmp))
2245 end = tmp;
2247 /* Include any barriers that may follow the basic block. */
2248 tmp = next_nonnote_insn_bb (end);
2249 while (tmp && BARRIER_P (tmp))
2251 end = tmp;
2252 tmp = next_nonnote_insn_bb (end);
2255 return end;
2258 /* Sanity check partition hotness to ensure that basic blocks in
2259   the cold partition don't dominate basic blocks in the hot partition.
2260 If FLAG_ONLY is true, report violations as errors. Otherwise
2261 re-mark the dominated blocks as cold, since this is run after
2262 cfg optimizations that may make hot blocks previously reached
2263 by both hot and cold blocks now only reachable along cold paths. */
2265 static vec<basic_block>
2266 find_partition_fixes (bool flag_only)
2268 basic_block bb;
2269 vec<basic_block> bbs_in_cold_partition = vNULL;
2270 vec<basic_block> bbs_to_fix = vNULL;
2272 /* Callers check this. */
2273 gcc_checking_assert (crtl->has_bb_partition);
2275 FOR_EACH_BB (bb)
2276 if ((BB_PARTITION (bb) == BB_COLD_PARTITION))
2277 bbs_in_cold_partition.safe_push (bb);
2279 if (bbs_in_cold_partition.is_empty ())
2280 return vNULL;
2282 bool dom_calculated_here = !dom_info_available_p (CDI_DOMINATORS);
2284 if (dom_calculated_here)
2285 calculate_dominance_info (CDI_DOMINATORS);
2287 while (! bbs_in_cold_partition.is_empty ())
2289 bb = bbs_in_cold_partition.pop ();
2290 /* Any blocks dominated by a block in the cold section
2291 must also be cold. */
2292 basic_block son;
2293 for (son = first_dom_son (CDI_DOMINATORS, bb);
2294 son;
2295 son = next_dom_son (CDI_DOMINATORS, son))
2297 /* If son is not yet cold, then mark it cold here and
2298 enqueue it for further processing. */
2299 if ((BB_PARTITION (son) != BB_COLD_PARTITION))
2301 if (flag_only)
2302 error ("non-cold basic block %d dominated "
2303 "by a block in the cold partition (%d)", son->index, bb->index);
2304 else
2305 BB_SET_PARTITION (son, BB_COLD_PARTITION);
2306 bbs_to_fix.safe_push (son);
2307 bbs_in_cold_partition.safe_push (son);
2312 if (dom_calculated_here)
2313 free_dominance_info (CDI_DOMINATORS);
2315 return bbs_to_fix;
2318 /* Perform cleanup on the hot/cold bb partitioning after optimization
2319 passes that modify the cfg. */
2321 void
2322 fixup_partitions (void)
2324 basic_block bb;
2326 if (!crtl->has_bb_partition)
2327 return;
2329 /* Delete any blocks that became unreachable and weren't
2330 already cleaned up, for example during edge forwarding
2331 and convert_jumps_to_returns. This will expose more
2332 opportunities for fixing the partition boundaries here.
2333 Also, the calculation of the dominance graph during verification
2334 will assert if there are unreachable nodes. */
2335 delete_unreachable_blocks ();
2337 /* If there are partitions, do a sanity check on them: A basic block in
2338   a cold partition cannot dominate a basic block in a hot partition.
2339 Fixup any that now violate this requirement, as a result of edge
2340 forwarding and unreachable block deletion.  */
2341 vec<basic_block> bbs_to_fix = find_partition_fixes (false);
2343 /* Do the partition fixup after all necessary blocks have been converted to
2344 cold, so that we only update the region crossings the minimum number of
2345 places, which can require forcing edges to be non fallthru. */
2346 while (! bbs_to_fix.is_empty ())
2348 bb = bbs_to_fix.pop ();
2349 fixup_new_cold_bb (bb);
2353 /* Verify, in the basic block chain, that there is at most one switch
2354 between hot/cold partitions. This condition will not be true until
2355 after reorder_basic_blocks is called. */
2357 static int
2358 verify_hot_cold_block_grouping (void)
2360 basic_block bb;
2361 int err = 0;
2362 bool switched_sections = false;
2363 int current_partition = BB_UNPARTITIONED;
2365 /* Even after bb reordering is complete, we go into cfglayout mode
2366 again (in compgoto). Ensure we don't call this before going back
2367 into linearized RTL when any layout fixes would have been committed. */
2368 if (!crtl->bb_reorder_complete
2369 || current_ir_type () != IR_RTL_CFGRTL)
2370 return err;
2372 FOR_EACH_BB (bb)
2374 if (current_partition != BB_UNPARTITIONED
2375 && BB_PARTITION (bb) != current_partition)
2377 if (switched_sections)
2379 error ("multiple hot/cold transitions found (bb %i)",
2380 bb->index);
2381 err = 1;
2383 else
2384 switched_sections = true;
2386 if (!crtl->has_bb_partition)
2387 error ("partition found but function partition flag not set");
2389 current_partition = BB_PARTITION (bb);
2392 return err;
2396 /* Perform several checks on the edges out of each block, such as
2397 the consistency of the branch probabilities, the correctness
2398 of hot/cold partition crossing edges, and the number of expected
2399 successor edges. Also verify that the dominance relationship
2400 between hot/cold blocks is sane. */
2402 static int
2403 rtl_verify_edges (void)
2405 int err = 0;
2406 basic_block bb;
2408 FOR_EACH_BB_REVERSE (bb)
2410 int n_fallthru = 0, n_branch = 0, n_abnormal_call = 0, n_sibcall = 0;
2411 int n_eh = 0, n_abnormal = 0;
2412 edge e, fallthru = NULL;
2413 edge_iterator ei;
2414 rtx note;
2415 bool has_crossing_edge = false;
2417 if (JUMP_P (BB_END (bb))
2418 && (note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX))
2419 && EDGE_COUNT (bb->succs) >= 2
2420 && any_condjump_p (BB_END (bb)))
2422 if (XINT (note, 0) != BRANCH_EDGE (bb)->probability
2423 && profile_status != PROFILE_ABSENT)
2425 error ("verify_flow_info: REG_BR_PROB does not match cfg %i %i",
2426 XINT (note, 0), BRANCH_EDGE (bb)->probability);
2427 err = 1;
2431 FOR_EACH_EDGE (e, ei, bb->succs)
2433 bool is_crossing;
2435 if (e->flags & EDGE_FALLTHRU)
2436 n_fallthru++, fallthru = e;
2438 is_crossing = (BB_PARTITION (e->src) != BB_PARTITION (e->dest)
2439 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
2440 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun));
2441 has_crossing_edge |= is_crossing;
2442 if (e->flags & EDGE_CROSSING)
2444 if (!is_crossing)
2446 error ("EDGE_CROSSING incorrectly set across same section");
2447 err = 1;
2449 if (e->flags & EDGE_FALLTHRU)
2451 error ("fallthru edge crosses section boundary in bb %i",
2452 e->src->index);
2453 err = 1;
2455 if (e->flags & EDGE_EH)
2457 error ("EH edge crosses section boundary in bb %i",
2458 e->src->index);
2459 err = 1;
2461 if (JUMP_P (BB_END (bb))
2462 && !find_reg_note (BB_END (bb), REG_CROSSING_JUMP, NULL_RTX))
2464 error ("No region crossing jump at section boundary in bb %i",
2465 bb->index);
2466 err = 1;
2469 else if (is_crossing)
2471 error ("EDGE_CROSSING missing across section boundary");
2472 err = 1;
2475 if ((e->flags & ~(EDGE_DFS_BACK
2476 | EDGE_CAN_FALLTHRU
2477 | EDGE_IRREDUCIBLE_LOOP
2478 | EDGE_LOOP_EXIT
2479 | EDGE_CROSSING
2480 | EDGE_PRESERVE)) == 0)
2481 n_branch++;
2483 if (e->flags & EDGE_ABNORMAL_CALL)
2484 n_abnormal_call++;
2486 if (e->flags & EDGE_SIBCALL)
2487 n_sibcall++;
2489 if (e->flags & EDGE_EH)
2490 n_eh++;
2492 if (e->flags & EDGE_ABNORMAL)
2493 n_abnormal++;
2496 if (!has_crossing_edge
2497 && find_reg_note (BB_END (bb), REG_CROSSING_JUMP, NULL_RTX))
2499 print_rtl_with_bb (stderr, get_insns (), TDF_RTL | TDF_BLOCKS | TDF_DETAILS);
2500 error ("Region crossing jump across same section in bb %i",
2501 bb->index);
2502 err = 1;
2505 if (n_eh && !find_reg_note (BB_END (bb), REG_EH_REGION, NULL_RTX))
2507 error ("missing REG_EH_REGION note at the end of bb %i", bb->index);
2508 err = 1;
2510 if (n_eh > 1)
2512 error ("too many exception handling edges in bb %i", bb->index);
2513 err = 1;
2515 if (n_branch
2516 && (!JUMP_P (BB_END (bb))
2517 || (n_branch > 1 && (any_uncondjump_p (BB_END (bb))
2518 || any_condjump_p (BB_END (bb))))))
2520 error ("too many outgoing branch edges from bb %i", bb->index);
2521 err = 1;
2523 if (n_fallthru && any_uncondjump_p (BB_END (bb)))
2525 error ("fallthru edge after unconditional jump in bb %i", bb->index);
2526 err = 1;
2528 if (n_branch != 1 && any_uncondjump_p (BB_END (bb)))
2530 error ("wrong number of branch edges after unconditional jump"
2531 " in bb %i", bb->index);
2532 err = 1;
2534 if (n_branch != 1 && any_condjump_p (BB_END (bb))
2535 && JUMP_LABEL (BB_END (bb)) != BB_HEAD (fallthru->dest))
2537 error ("wrong amount of branch edges after conditional jump"
2538 " in bb %i", bb->index);
2539 err = 1;
2541 if (n_abnormal_call && !CALL_P (BB_END (bb)))
2543 error ("abnormal call edges for non-call insn in bb %i", bb->index);
2544 err = 1;
2546 if (n_sibcall && !CALL_P (BB_END (bb)))
2548 error ("sibcall edges for non-call insn in bb %i", bb->index);
2549 err = 1;
2551 if (n_abnormal > n_eh
2552 && !(CALL_P (BB_END (bb))
2553 && n_abnormal == n_abnormal_call + n_sibcall)
2554 && (!JUMP_P (BB_END (bb))
2555 || any_condjump_p (BB_END (bb))
2556 || any_uncondjump_p (BB_END (bb))))
2558 error ("abnormal edges for no purpose in bb %i", bb->index);
2559 err = 1;
2563 /* If there are partitions, do a sanity check on them: A basic block in
2564   a cold partition cannot dominate a basic block in a hot partition.  */
2565 if (crtl->has_bb_partition && !err)
2567 vec<basic_block> bbs_to_fix = find_partition_fixes (true);
2568 err = !bbs_to_fix.is_empty ();
2571 /* Clean up. */
2572 return err;
2575 /* Checks on the instructions within blocks. Currently checks that each
2576 block starts with a basic block note, and that basic block notes and
2577 control flow jumps are not found in the middle of the block. */
2579 static int
2580 rtl_verify_bb_insns (void)
2582 rtx x;
2583 int err = 0;
2584 basic_block bb;
2586 FOR_EACH_BB_REVERSE (bb)
2588 /* Now check the header of basic
2589 block. It ought to contain optional CODE_LABEL followed
2590 by NOTE_BASIC_BLOCK. */
2591 x = BB_HEAD (bb);
2592 if (LABEL_P (x))
2594 if (BB_END (bb) == x)
2596 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
2597 bb->index);
2598 err = 1;
2601 x = NEXT_INSN (x);
2604 if (!NOTE_INSN_BASIC_BLOCK_P (x) || NOTE_BASIC_BLOCK (x) != bb)
2606 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
2607 bb->index);
2608 err = 1;
2611 if (BB_END (bb) == x)
2612 /* Do checks for empty blocks here. */
2614 else
2615 for (x = NEXT_INSN (x); x; x = NEXT_INSN (x))
2617 if (NOTE_INSN_BASIC_BLOCK_P (x))
2619 error ("NOTE_INSN_BASIC_BLOCK %d in middle of basic block %d",
2620 INSN_UID (x), bb->index);
2621 err = 1;
2624 if (x == BB_END (bb))
2625 break;
2627 if (control_flow_insn_p (x))
2629 error ("in basic block %d:", bb->index);
2630 fatal_insn ("flow control insn inside a basic block", x);
2635 /* Clean up. */
2636 return err;
2639 /* Verify that block pointers for instructions in basic blocks, headers and
2640 footers are set appropriately. */
2642 static int
2643 rtl_verify_bb_pointers (void)
2645 int err = 0;
2646 basic_block bb;
2648 /* Check the general integrity of the basic blocks. */
2649 FOR_EACH_BB_REVERSE (bb)
2651 rtx insn;
2653 if (!(bb->flags & BB_RTL))
2655 error ("BB_RTL flag not set for block %d", bb->index);
2656 err = 1;
2659 FOR_BB_INSNS (bb, insn)
2660 if (BLOCK_FOR_INSN (insn) != bb)
2662 error ("insn %d basic block pointer is %d, should be %d",
2663 INSN_UID (insn),
2664 BLOCK_FOR_INSN (insn) ? BLOCK_FOR_INSN (insn)->index : 0,
2665 bb->index);
2666 err = 1;
2669 for (insn = BB_HEADER (bb); insn; insn = NEXT_INSN (insn))
2670 if (!BARRIER_P (insn)
2671 && BLOCK_FOR_INSN (insn) != NULL)
2673 error ("insn %d in header of bb %d has non-NULL basic block",
2674 INSN_UID (insn), bb->index);
2675 err = 1;
2677 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2678 if (!BARRIER_P (insn)
2679 && BLOCK_FOR_INSN (insn) != NULL)
2681 error ("insn %d in footer of bb %d has non-NULL basic block",
2682 INSN_UID (insn), bb->index);
2683 err = 1;
2687 /* Clean up. */
2688 return err;
2691 /* Verify the CFG and RTL consistency common for both underlying RTL and
2692 cfglayout RTL.
2694 Currently it does following checks:
2696 - overlapping of basic blocks
2697 - insns with wrong BLOCK_FOR_INSN pointers
2698 - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note)
2699 - tails of basic blocks (ensure that boundary is necessary)
2700 - scans body of the basic block for JUMP_INSN, CODE_LABEL
2701 and NOTE_INSN_BASIC_BLOCK
2702 - verify that no fall_thru edge crosses hot/cold partition boundaries
2703 - verify that there are no pending RTL branch predictions
2704 - verify that hot blocks are not dominated by cold blocks
2706 In future it can be extended check a lot of other stuff as well
2707 (reachability of basic blocks, life information, etc. etc.). */
2709 static int
2710 rtl_verify_flow_info_1 (void)
2712 int err = 0;
2714 err |= rtl_verify_bb_pointers ();
2716 err |= rtl_verify_bb_insns ();
2718 err |= rtl_verify_edges ();
2720 return err;
2723 /* Walk the instruction chain and verify that bb head/end pointers
2724 are correct, and that instructions are in exactly one bb and have
2725 correct block pointers. */
2727 static int
2728 rtl_verify_bb_insn_chain (void)
2730 basic_block bb;
2731 int err = 0;
2732 rtx x;
2733 rtx last_head = get_last_insn ();
2734 basic_block *bb_info;
2735 const int max_uid = get_max_uid ();
2737 bb_info = XCNEWVEC (basic_block, max_uid);
2739 FOR_EACH_BB_REVERSE (bb)
2741 rtx head = BB_HEAD (bb);
2742 rtx end = BB_END (bb);
2744 for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
2746 /* Verify the end of the basic block is in the INSN chain. */
2747 if (x == end)
2748 break;
2750 /* And that the code outside of basic blocks has NULL bb field. */
2751 if (!BARRIER_P (x)
2752 && BLOCK_FOR_INSN (x) != NULL)
2754 error ("insn %d outside of basic blocks has non-NULL bb field",
2755 INSN_UID (x));
2756 err = 1;
2760 if (!x)
2762 error ("end insn %d for block %d not found in the insn stream",
2763 INSN_UID (end), bb->index);
2764 err = 1;
2767 /* Work backwards from the end to the head of the basic block
2768 to verify the head is in the RTL chain. */
2769 for (; x != NULL_RTX; x = PREV_INSN (x))
2771 /* While walking over the insn chain, verify insns appear
2772 in only one basic block. */
2773 if (bb_info[INSN_UID (x)] != NULL)
2775 error ("insn %d is in multiple basic blocks (%d and %d)",
2776 INSN_UID (x), bb->index, bb_info[INSN_UID (x)]->index);
2777 err = 1;
2780 bb_info[INSN_UID (x)] = bb;
2782 if (x == head)
2783 break;
2785 if (!x)
2787 error ("head insn %d for block %d not found in the insn stream",
2788 INSN_UID (head), bb->index);
2789 err = 1;
2792 last_head = PREV_INSN (x);
2795 for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
2797 /* Check that the code before the first basic block has NULL
2798 bb field. */
2799 if (!BARRIER_P (x)
2800 && BLOCK_FOR_INSN (x) != NULL)
2802 error ("insn %d outside of basic blocks has non-NULL bb field",
2803 INSN_UID (x));
2804 err = 1;
2807 free (bb_info);
2809 return err;
2812 /* Verify that fallthru edges point to adjacent blocks in layout order and
2813 that barriers exist after non-fallthru blocks. */
2815 static int
2816 rtl_verify_fallthru (void)
2818 basic_block bb;
2819 int err = 0;
2821 FOR_EACH_BB_REVERSE (bb)
2823 edge e;
2825 e = find_fallthru_edge (bb->succs);
2826 if (!e)
2828 rtx insn;
2830 /* Ensure existence of barrier in BB with no fallthru edges. */
2831 for (insn = NEXT_INSN (BB_END (bb)); ; insn = NEXT_INSN (insn))
2833 if (!insn || NOTE_INSN_BASIC_BLOCK_P (insn))
2835 error ("missing barrier after block %i", bb->index);
2836 err = 1;
2837 break;
2839 if (BARRIER_P (insn))
2840 break;
2843 else if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
2844 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
2846 rtx insn;
2848 if (e->src->next_bb != e->dest)
2850 error
2851 ("verify_flow_info: Incorrect blocks for fallthru %i->%i",
2852 e->src->index, e->dest->index);
2853 err = 1;
2855 else
2856 for (insn = NEXT_INSN (BB_END (e->src)); insn != BB_HEAD (e->dest);
2857 insn = NEXT_INSN (insn))
2858 if (BARRIER_P (insn) || INSN_P (insn))
2860 error ("verify_flow_info: Incorrect fallthru %i->%i",
2861 e->src->index, e->dest->index);
2862 fatal_insn ("wrong insn in the fallthru edge", insn);
2863 err = 1;
2868 return err;
2871 /* Verify that blocks are laid out in consecutive order. While walking the
2872 instructions, verify that all expected instructions are inside the basic
2873 blocks, and that all returns are followed by barriers. */
2875 static int
2876 rtl_verify_bb_layout (void)
2878 basic_block bb;
2879 int err = 0;
2880 rtx x;
2881 int num_bb_notes;
2882 const rtx rtx_first = get_insns ();
2883 basic_block last_bb_seen = ENTRY_BLOCK_PTR_FOR_FN (cfun), curr_bb = NULL;
2885 num_bb_notes = 0;
2886 last_bb_seen = ENTRY_BLOCK_PTR_FOR_FN (cfun);
2888 for (x = rtx_first; x; x = NEXT_INSN (x))
2890 if (NOTE_INSN_BASIC_BLOCK_P (x))
2892 bb = NOTE_BASIC_BLOCK (x);
2894 num_bb_notes++;
2895 if (bb != last_bb_seen->next_bb)
2896 internal_error ("basic blocks not laid down consecutively");
2898 curr_bb = last_bb_seen = bb;
2901 if (!curr_bb)
2903 switch (GET_CODE (x))
2905 case BARRIER:
2906 case NOTE:
2907 break;
2909 case CODE_LABEL:
2910 /* An ADDR_VEC is placed outside any basic block. */
2911 if (NEXT_INSN (x)
2912 && JUMP_TABLE_DATA_P (NEXT_INSN (x)))
2913 x = NEXT_INSN (x);
2915 /* But in any case, non-deletable labels can appear anywhere. */
2916 break;
2918 default:
2919 fatal_insn ("insn outside basic block", x);
2923 if (JUMP_P (x)
2924 && returnjump_p (x) && ! condjump_p (x)
2925 && ! (next_nonnote_insn (x) && BARRIER_P (next_nonnote_insn (x))))
2926 fatal_insn ("return not followed by barrier", x);
2928 if (curr_bb && x == BB_END (curr_bb))
2929 curr_bb = NULL;
2932 if (num_bb_notes != n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS)
2933 internal_error
2934 ("number of bb notes in insn chain (%d) != n_basic_blocks (%d)",
2935 num_bb_notes, n_basic_blocks_for_fn (cfun));
2937 return err;
2940 /* Verify the CFG and RTL consistency common for both underlying RTL and
2941 cfglayout RTL, plus consistency checks specific to linearized RTL mode.
2943 Currently it does following checks:
2944 - all checks of rtl_verify_flow_info_1
2945 - test head/end pointers
2946 - check that blocks are laid out in consecutive order
2947 - check that all insns are in the basic blocks
2948 (except the switch handling code, barriers and notes)
2949 - check that all returns are followed by barriers
2950 - check that all fallthru edge points to the adjacent blocks
2951 - verify that there is a single hot/cold partition boundary after bbro */
2953 static int
2954 rtl_verify_flow_info (void)
2956 int err = 0;
2958 err |= rtl_verify_flow_info_1 ();
2960 err |= rtl_verify_bb_insn_chain ();
2962 err |= rtl_verify_fallthru ();
2964 err |= rtl_verify_bb_layout ();
2966 err |= verify_hot_cold_block_grouping ();
2968 return err;
2971 /* Assume that the preceding pass has possibly eliminated jump instructions
2972 or converted the unconditional jumps. Eliminate the edges from CFG.
2973 Return true if any edges are eliminated. */
2975 bool
2976 purge_dead_edges (basic_block bb)
2978 edge e;
2979 rtx insn = BB_END (bb), note;
2980 bool purged = false;
2981 bool found;
2982 edge_iterator ei;
2984 if (DEBUG_INSN_P (insn) && insn != BB_HEAD (bb))
2986 insn = PREV_INSN (insn);
2987 while ((DEBUG_INSN_P (insn) || NOTE_P (insn)) && insn != BB_HEAD (bb));
2989 /* If this instruction cannot trap, remove REG_EH_REGION notes. */
2990 if (NONJUMP_INSN_P (insn)
2991 && (note = find_reg_note (insn, REG_EH_REGION, NULL)))
2993 rtx eqnote;
2995 if (! may_trap_p (PATTERN (insn))
2996 || ((eqnote = find_reg_equal_equiv_note (insn))
2997 && ! may_trap_p (XEXP (eqnote, 0))))
2998 remove_note (insn, note);
3001 /* Cleanup abnormal edges caused by exceptions or non-local gotos. */
3002 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3004 bool remove = false;
3006 /* There are three types of edges we need to handle correctly here: EH
3007 edges, abnormal call EH edges, and abnormal call non-EH edges. The
3008 latter can appear when nonlocal gotos are used. */
3009 if (e->flags & EDGE_ABNORMAL_CALL)
3011 if (!CALL_P (insn))
3012 remove = true;
3013 else if (can_nonlocal_goto (insn))
3015 else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
3017 else if (flag_tm && find_reg_note (insn, REG_TM, NULL))
3019 else
3020 remove = true;
3022 else if (e->flags & EDGE_EH)
3023 remove = !can_throw_internal (insn);
3025 if (remove)
3027 remove_edge (e);
3028 df_set_bb_dirty (bb);
3029 purged = true;
3031 else
3032 ei_next (&ei);
3035 if (JUMP_P (insn))
3037 rtx note;
3038 edge b,f;
3039 edge_iterator ei;
3041 /* We do care only about conditional jumps and simplejumps. */
3042 if (!any_condjump_p (insn)
3043 && !returnjump_p (insn)
3044 && !simplejump_p (insn))
3045 return purged;
3047 /* Branch probability/prediction notes are defined only for
3048 condjumps. We've possibly turned condjump into simplejump. */
3049 if (simplejump_p (insn))
3051 note = find_reg_note (insn, REG_BR_PROB, NULL);
3052 if (note)
3053 remove_note (insn, note);
3054 while ((note = find_reg_note (insn, REG_BR_PRED, NULL)))
3055 remove_note (insn, note);
3058 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3060 /* Avoid abnormal flags to leak from computed jumps turned
3061 into simplejumps. */
3063 e->flags &= ~EDGE_ABNORMAL;
3065 /* See if this edge is one we should keep. */
3066 if ((e->flags & EDGE_FALLTHRU) && any_condjump_p (insn))
3067 /* A conditional jump can fall through into the next
3068 block, so we should keep the edge. */
3070 ei_next (&ei);
3071 continue;
3073 else if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
3074 && BB_HEAD (e->dest) == JUMP_LABEL (insn))
3075 /* If the destination block is the target of the jump,
3076 keep the edge. */
3078 ei_next (&ei);
3079 continue;
3081 else if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
3082 && returnjump_p (insn))
3083 /* If the destination block is the exit block, and this
3084 instruction is a return, then keep the edge. */
3086 ei_next (&ei);
3087 continue;
3089 else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
3090 /* Keep the edges that correspond to exceptions thrown by
3091 this instruction and rematerialize the EDGE_ABNORMAL
3092 flag we just cleared above. */
3094 e->flags |= EDGE_ABNORMAL;
3095 ei_next (&ei);
3096 continue;
3099 /* We do not need this edge. */
3100 df_set_bb_dirty (bb);
3101 purged = true;
3102 remove_edge (e);
3105 if (EDGE_COUNT (bb->succs) == 0 || !purged)
3106 return purged;
3108 if (dump_file)
3109 fprintf (dump_file, "Purged edges from bb %i\n", bb->index);
3111 if (!optimize)
3112 return purged;
3114 /* Redistribute probabilities. */
3115 if (single_succ_p (bb))
3117 single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
3118 single_succ_edge (bb)->count = bb->count;
3120 else
3122 note = find_reg_note (insn, REG_BR_PROB, NULL);
3123 if (!note)
3124 return purged;
3126 b = BRANCH_EDGE (bb);
3127 f = FALLTHRU_EDGE (bb);
3128 b->probability = XINT (note, 0);
3129 f->probability = REG_BR_PROB_BASE - b->probability;
3130 /* Update these to use GCOV_COMPUTE_SCALE. */
3131 b->count = bb->count * b->probability / REG_BR_PROB_BASE;
3132 f->count = bb->count * f->probability / REG_BR_PROB_BASE;
3135 return purged;
3137 else if (CALL_P (insn) && SIBLING_CALL_P (insn))
3139 /* First, there should not be any EH or ABCALL edges resulting
3140 from non-local gotos and the like. If there were, we shouldn't
3141 have created the sibcall in the first place. Second, there
3142 should of course never have been a fallthru edge. */
3143 gcc_assert (single_succ_p (bb));
3144 gcc_assert (single_succ_edge (bb)->flags
3145 == (EDGE_SIBCALL | EDGE_ABNORMAL));
3147 return 0;
3150 /* If we don't see a jump insn, we don't know exactly why the block would
3151 have been broken at this point. Look for a simple, non-fallthru edge,
3152 as these are only created by conditional branches. If we find such an
3153 edge we know that there used to be a jump here and can then safely
3154 remove all non-fallthru edges. */
3155 found = false;
3156 FOR_EACH_EDGE (e, ei, bb->succs)
3157 if (! (e->flags & (EDGE_COMPLEX | EDGE_FALLTHRU)))
3159 found = true;
3160 break;
3163 if (!found)
3164 return purged;
3166 /* Remove all but the fake and fallthru edges. The fake edge may be
3167 the only successor for this block in the case of noreturn
3168 calls. */
3169 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3171 if (!(e->flags & (EDGE_FALLTHRU | EDGE_FAKE)))
3173 df_set_bb_dirty (bb);
3174 remove_edge (e);
3175 purged = true;
3177 else
3178 ei_next (&ei);
3181 gcc_assert (single_succ_p (bb));
3183 single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
3184 single_succ_edge (bb)->count = bb->count;
3186 if (dump_file)
3187 fprintf (dump_file, "Purged non-fallthru edges from bb %i\n",
3188 bb->index);
3189 return purged;
3192 /* Search all basic blocks for potentially dead edges and purge them. Return
3193 true if some edge has been eliminated. */
3195 bool
3196 purge_all_dead_edges (void)
3198 int purged = false;
3199 basic_block bb;
3201 FOR_EACH_BB (bb)
3203 bool purged_here = purge_dead_edges (bb);
3205 purged |= purged_here;
3208 return purged;
3211 /* This is used by a few passes that emit some instructions after abnormal
3212 calls, moving the basic block's end, while they in fact do want to emit
3213 them on the fallthru edge. Look for abnormal call edges, find backward
3214 the call in the block and insert the instructions on the edge instead.
3216 Similarly, handle instructions throwing exceptions internally.
3218 Return true when instructions have been found and inserted on edges. */
3220 bool
3221 fixup_abnormal_edges (void)
3223 bool inserted = false;
3224 basic_block bb;
3226 FOR_EACH_BB (bb)
3228 edge e;
3229 edge_iterator ei;
3231 /* Look for cases we are interested in - calls or instructions causing
3232 exceptions. */
3233 FOR_EACH_EDGE (e, ei, bb->succs)
3234 if ((e->flags & EDGE_ABNORMAL_CALL)
3235 || ((e->flags & (EDGE_ABNORMAL | EDGE_EH))
3236 == (EDGE_ABNORMAL | EDGE_EH)))
3237 break;
3239 if (e && !CALL_P (BB_END (bb)) && !can_throw_internal (BB_END (bb)))
3241 rtx insn;
3243 /* Get past the new insns generated. Allow notes, as the insns
3244 may be already deleted. */
3245 insn = BB_END (bb);
3246 while ((NONJUMP_INSN_P (insn) || NOTE_P (insn))
3247 && !can_throw_internal (insn)
3248 && insn != BB_HEAD (bb))
3249 insn = PREV_INSN (insn);
3251 if (CALL_P (insn) || can_throw_internal (insn))
3253 rtx stop, next;
3255 e = find_fallthru_edge (bb->succs);
3257 stop = NEXT_INSN (BB_END (bb));
3258 BB_END (bb) = insn;
3260 for (insn = NEXT_INSN (insn); insn != stop; insn = next)
3262 next = NEXT_INSN (insn);
3263 if (INSN_P (insn))
3265 delete_insn (insn);
3267 /* Sometimes there's still the return value USE.
3268 If it's placed after a trapping call (i.e. that
3269 call is the last insn anyway), we have no fallthru
3270 edge. Simply delete this use and don't try to insert
3271 on the non-existent edge. */
3272 if (GET_CODE (PATTERN (insn)) != USE)
3274 /* We're not deleting it, we're moving it. */
3275 INSN_DELETED_P (insn) = 0;
3276 PREV_INSN (insn) = NULL_RTX;
3277 NEXT_INSN (insn) = NULL_RTX;
3279 insert_insn_on_edge (insn, e);
3280 inserted = true;
3283 else if (!BARRIER_P (insn))
3284 set_block_for_insn (insn, NULL);
3288 /* It may be that we don't find any trapping insn. In this
3289 case we discovered quite late that the insn that had been
3290 marked as can_throw_internal in fact couldn't trap at all.
3291 So we should in fact delete the EH edges out of the block. */
3292 else
3293 purge_dead_edges (bb);
3297 return inserted;
3300 /* Cut the insns from FIRST to LAST out of the insns stream. */
3303 unlink_insn_chain (rtx first, rtx last)
3305 rtx prevfirst = PREV_INSN (first);
3306 rtx nextlast = NEXT_INSN (last);
3308 PREV_INSN (first) = NULL;
3309 NEXT_INSN (last) = NULL;
3310 if (prevfirst)
3311 NEXT_INSN (prevfirst) = nextlast;
3312 if (nextlast)
3313 PREV_INSN (nextlast) = prevfirst;
3314 else
3315 set_last_insn (prevfirst);
3316 if (!prevfirst)
3317 set_first_insn (nextlast);
3318 return first;
3321 /* Skip over inter-block insns occurring after BB which are typically
3322 associated with BB (e.g., barriers). If there are any such insns,
3323 we return the last one. Otherwise, we return the end of BB. */
3325 static rtx
3326 skip_insns_after_block (basic_block bb)
3328 rtx insn, last_insn, next_head, prev;
3330 next_head = NULL_RTX;
3331 if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3332 next_head = BB_HEAD (bb->next_bb);
3334 for (last_insn = insn = BB_END (bb); (insn = NEXT_INSN (insn)) != 0; )
3336 if (insn == next_head)
3337 break;
3339 switch (GET_CODE (insn))
3341 case BARRIER:
3342 last_insn = insn;
3343 continue;
3345 case NOTE:
3346 switch (NOTE_KIND (insn))
3348 case NOTE_INSN_BLOCK_END:
3349 gcc_unreachable ();
3350 continue;
3351 default:
3352 continue;
3353 break;
3355 break;
3357 case CODE_LABEL:
3358 if (NEXT_INSN (insn)
3359 && JUMP_TABLE_DATA_P (NEXT_INSN (insn)))
3361 insn = NEXT_INSN (insn);
3362 last_insn = insn;
3363 continue;
3365 break;
3367 default:
3368 break;
3371 break;
3374 /* It is possible to hit contradictory sequence. For instance:
3376 jump_insn
3377 NOTE_INSN_BLOCK_BEG
3378 barrier
3380 Where barrier belongs to jump_insn, but the note does not. This can be
3381 created by removing the basic block originally following
3382 NOTE_INSN_BLOCK_BEG. In such case reorder the notes. */
3384 for (insn = last_insn; insn != BB_END (bb); insn = prev)
3386 prev = PREV_INSN (insn);
3387 if (NOTE_P (insn))
3388 switch (NOTE_KIND (insn))
3390 case NOTE_INSN_BLOCK_END:
3391 gcc_unreachable ();
3392 break;
3393 case NOTE_INSN_DELETED:
3394 case NOTE_INSN_DELETED_LABEL:
3395 case NOTE_INSN_DELETED_DEBUG_LABEL:
3396 continue;
3397 default:
3398 reorder_insns (insn, insn, last_insn);
3402 return last_insn;
3405 /* Locate or create a label for a given basic block. */
3407 static rtx
3408 label_for_bb (basic_block bb)
3410 rtx label = BB_HEAD (bb);
3412 if (!LABEL_P (label))
3414 if (dump_file)
3415 fprintf (dump_file, "Emitting label for block %d\n", bb->index);
3417 label = block_label (bb);
3420 return label;
3423 /* Locate the effective beginning and end of the insn chain for each
3424 block, as defined by skip_insns_after_block above. */
3426 static void
3427 record_effective_endpoints (void)
3429 rtx next_insn;
3430 basic_block bb;
3431 rtx insn;
3433 for (insn = get_insns ();
3434 insn
3435 && NOTE_P (insn)
3436 && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK;
3437 insn = NEXT_INSN (insn))
3438 continue;
3439 /* No basic blocks at all? */
3440 gcc_assert (insn);
3442 if (PREV_INSN (insn))
3443 cfg_layout_function_header =
3444 unlink_insn_chain (get_insns (), PREV_INSN (insn));
3445 else
3446 cfg_layout_function_header = NULL_RTX;
3448 next_insn = get_insns ();
3449 FOR_EACH_BB (bb)
3451 rtx end;
3453 if (PREV_INSN (BB_HEAD (bb)) && next_insn != BB_HEAD (bb))
3454 BB_HEADER (bb) = unlink_insn_chain (next_insn,
3455 PREV_INSN (BB_HEAD (bb)));
3456 end = skip_insns_after_block (bb);
3457 if (NEXT_INSN (BB_END (bb)) && BB_END (bb) != end)
3458 BB_FOOTER (bb) = unlink_insn_chain (NEXT_INSN (BB_END (bb)), end);
3459 next_insn = NEXT_INSN (BB_END (bb));
3462 cfg_layout_function_footer = next_insn;
3463 if (cfg_layout_function_footer)
3464 cfg_layout_function_footer = unlink_insn_chain (cfg_layout_function_footer, get_last_insn ());
3467 static unsigned int
3468 into_cfg_layout_mode (void)
3470 cfg_layout_initialize (0);
3471 return 0;
3474 static unsigned int
3475 outof_cfg_layout_mode (void)
3477 basic_block bb;
3479 FOR_EACH_BB (bb)
3480 if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3481 bb->aux = bb->next_bb;
3483 cfg_layout_finalize ();
3485 return 0;
3488 namespace {
3490 const pass_data pass_data_into_cfg_layout_mode =
3492 RTL_PASS, /* type */
3493 "into_cfglayout", /* name */
3494 OPTGROUP_NONE, /* optinfo_flags */
3495 false, /* has_gate */
3496 true, /* has_execute */
3497 TV_CFG, /* tv_id */
3498 0, /* properties_required */
3499 PROP_cfglayout, /* properties_provided */
3500 0, /* properties_destroyed */
3501 0, /* todo_flags_start */
3502 0, /* todo_flags_finish */
3505 class pass_into_cfg_layout_mode : public rtl_opt_pass
3507 public:
3508 pass_into_cfg_layout_mode (gcc::context *ctxt)
3509 : rtl_opt_pass (pass_data_into_cfg_layout_mode, ctxt)
3512 /* opt_pass methods: */
3513 unsigned int execute () { return into_cfg_layout_mode (); }
3515 }; // class pass_into_cfg_layout_mode
3517 } // anon namespace
3519 rtl_opt_pass *
3520 make_pass_into_cfg_layout_mode (gcc::context *ctxt)
3522 return new pass_into_cfg_layout_mode (ctxt);
3525 namespace {
3527 const pass_data pass_data_outof_cfg_layout_mode =
3529 RTL_PASS, /* type */
3530 "outof_cfglayout", /* name */
3531 OPTGROUP_NONE, /* optinfo_flags */
3532 false, /* has_gate */
3533 true, /* has_execute */
3534 TV_CFG, /* tv_id */
3535 0, /* properties_required */
3536 0, /* properties_provided */
3537 PROP_cfglayout, /* properties_destroyed */
3538 0, /* todo_flags_start */
3539 0, /* todo_flags_finish */
3542 class pass_outof_cfg_layout_mode : public rtl_opt_pass
3544 public:
3545 pass_outof_cfg_layout_mode (gcc::context *ctxt)
3546 : rtl_opt_pass (pass_data_outof_cfg_layout_mode, ctxt)
3549 /* opt_pass methods: */
3550 unsigned int execute () { return outof_cfg_layout_mode (); }
3552 }; // class pass_outof_cfg_layout_mode
3554 } // anon namespace
3556 rtl_opt_pass *
3557 make_pass_outof_cfg_layout_mode (gcc::context *ctxt)
3559 return new pass_outof_cfg_layout_mode (ctxt);
3563 /* Link the basic blocks in the correct order, compacting the basic
3564 block queue while at it. If STAY_IN_CFGLAYOUT_MODE is false, this
3565 function also clears the basic block header and footer fields.
3567 This function is usually called after a pass (e.g. tracer) finishes
3568 some transformations while in cfglayout mode. The required sequence
3569 of the basic blocks is in a linked list along the bb->aux field.
3570 This functions re-links the basic block prev_bb and next_bb pointers
3571 accordingly, and it compacts and renumbers the blocks.
3573 FIXME: This currently works only for RTL, but the only RTL-specific
3574 bits are the STAY_IN_CFGLAYOUT_MODE bits. The tracer pass was moved
3575 to GIMPLE a long time ago, but it doesn't relink the basic block
3576 chain. It could do that (to give better initial RTL) if this function
3577 is made IR-agnostic (and moved to cfganal.c or cfg.c while at it). */
3579 void
3580 relink_block_chain (bool stay_in_cfglayout_mode)
3582 basic_block bb, prev_bb;
3583 int index;
3585 /* Maybe dump the re-ordered sequence. */
3586 if (dump_file)
3588 fprintf (dump_file, "Reordered sequence:\n");
3589 for (bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb, index =
3590 NUM_FIXED_BLOCKS;
3592 bb = (basic_block) bb->aux, index++)
3594 fprintf (dump_file, " %i ", index);
3595 if (get_bb_original (bb))
3596 fprintf (dump_file, "duplicate of %i ",
3597 get_bb_original (bb)->index);
3598 else if (forwarder_block_p (bb)
3599 && !LABEL_P (BB_HEAD (bb)))
3600 fprintf (dump_file, "compensation ");
3601 else
3602 fprintf (dump_file, "bb %i ", bb->index);
3603 fprintf (dump_file, " [%i]\n", bb->frequency);
3607 /* Now reorder the blocks. */
3608 prev_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
3609 bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb;
3610 for (; bb; prev_bb = bb, bb = (basic_block) bb->aux)
3612 bb->prev_bb = prev_bb;
3613 prev_bb->next_bb = bb;
3615 prev_bb->next_bb = EXIT_BLOCK_PTR_FOR_FN (cfun);
3616 EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb = prev_bb;
3618 /* Then, clean up the aux fields. */
3619 FOR_ALL_BB (bb)
3621 bb->aux = NULL;
3622 if (!stay_in_cfglayout_mode)
3623 BB_HEADER (bb) = BB_FOOTER (bb) = NULL;
3626 /* Maybe reset the original copy tables, they are not valid anymore
3627 when we renumber the basic blocks in compact_blocks. If we are
3628 are going out of cfglayout mode, don't re-allocate the tables. */
3629 free_original_copy_tables ();
3630 if (stay_in_cfglayout_mode)
3631 initialize_original_copy_tables ();
3633 /* Finally, put basic_block_info in the new order. */
3634 compact_blocks ();
3638 /* Given a reorder chain, rearrange the code to match. */
3640 static void
3641 fixup_reorder_chain (void)
3643 basic_block bb;
3644 rtx insn = NULL;
3646 if (cfg_layout_function_header)
3648 set_first_insn (cfg_layout_function_header);
3649 insn = cfg_layout_function_header;
3650 while (NEXT_INSN (insn))
3651 insn = NEXT_INSN (insn);
3654 /* First do the bulk reordering -- rechain the blocks without regard to
3655 the needed changes to jumps and labels. */
3657 for (bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; bb; bb = (basic_block)
3658 bb->aux)
3660 if (BB_HEADER (bb))
3662 if (insn)
3663 NEXT_INSN (insn) = BB_HEADER (bb);
3664 else
3665 set_first_insn (BB_HEADER (bb));
3666 PREV_INSN (BB_HEADER (bb)) = insn;
3667 insn = BB_HEADER (bb);
3668 while (NEXT_INSN (insn))
3669 insn = NEXT_INSN (insn);
3671 if (insn)
3672 NEXT_INSN (insn) = BB_HEAD (bb);
3673 else
3674 set_first_insn (BB_HEAD (bb));
3675 PREV_INSN (BB_HEAD (bb)) = insn;
3676 insn = BB_END (bb);
3677 if (BB_FOOTER (bb))
3679 NEXT_INSN (insn) = BB_FOOTER (bb);
3680 PREV_INSN (BB_FOOTER (bb)) = insn;
3681 while (NEXT_INSN (insn))
3682 insn = NEXT_INSN (insn);
3686 NEXT_INSN (insn) = cfg_layout_function_footer;
3687 if (cfg_layout_function_footer)
3688 PREV_INSN (cfg_layout_function_footer) = insn;
3690 while (NEXT_INSN (insn))
3691 insn = NEXT_INSN (insn);
3693 set_last_insn (insn);
3694 #ifdef ENABLE_CHECKING
3695 verify_insn_chain ();
3696 #endif
3698 /* Now add jumps and labels as needed to match the blocks new
3699 outgoing edges. */
3701 for (bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; bb ; bb = (basic_block)
3702 bb->aux)
3704 edge e_fall, e_taken, e;
3705 rtx bb_end_insn;
3706 rtx ret_label = NULL_RTX;
3707 basic_block nb;
3708 edge_iterator ei;
3710 if (EDGE_COUNT (bb->succs) == 0)
3711 continue;
3713 /* Find the old fallthru edge, and another non-EH edge for
3714 a taken jump. */
3715 e_taken = e_fall = NULL;
3717 FOR_EACH_EDGE (e, ei, bb->succs)
3718 if (e->flags & EDGE_FALLTHRU)
3719 e_fall = e;
3720 else if (! (e->flags & EDGE_EH))
3721 e_taken = e;
3723 bb_end_insn = BB_END (bb);
3724 if (JUMP_P (bb_end_insn))
3726 ret_label = JUMP_LABEL (bb_end_insn);
3727 if (any_condjump_p (bb_end_insn))
3729 /* This might happen if the conditional jump has side
3730 effects and could therefore not be optimized away.
3731 Make the basic block to end with a barrier in order
3732 to prevent rtl_verify_flow_info from complaining. */
3733 if (!e_fall)
3735 gcc_assert (!onlyjump_p (bb_end_insn)
3736 || returnjump_p (bb_end_insn));
3737 emit_barrier_after (bb_end_insn);
3738 continue;
3741 /* If the old fallthru is still next, nothing to do. */
3742 if (bb->aux == e_fall->dest
3743 || e_fall->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
3744 continue;
3746 /* The degenerated case of conditional jump jumping to the next
3747 instruction can happen for jumps with side effects. We need
3748 to construct a forwarder block and this will be done just
3749 fine by force_nonfallthru below. */
3750 if (!e_taken)
3753 /* There is another special case: if *neither* block is next,
3754 such as happens at the very end of a function, then we'll
3755 need to add a new unconditional jump. Choose the taken
3756 edge based on known or assumed probability. */
3757 else if (bb->aux != e_taken->dest)
3759 rtx note = find_reg_note (bb_end_insn, REG_BR_PROB, 0);
3761 if (note
3762 && XINT (note, 0) < REG_BR_PROB_BASE / 2
3763 && invert_jump (bb_end_insn,
3764 (e_fall->dest
3765 == EXIT_BLOCK_PTR_FOR_FN (cfun)
3766 ? NULL_RTX
3767 : label_for_bb (e_fall->dest)), 0))
3769 e_fall->flags &= ~EDGE_FALLTHRU;
3770 gcc_checking_assert (could_fall_through
3771 (e_taken->src, e_taken->dest));
3772 e_taken->flags |= EDGE_FALLTHRU;
3773 update_br_prob_note (bb);
3774 e = e_fall, e_fall = e_taken, e_taken = e;
3778 /* If the "jumping" edge is a crossing edge, and the fall
3779 through edge is non-crossing, leave things as they are. */
3780 else if ((e_taken->flags & EDGE_CROSSING)
3781 && !(e_fall->flags & EDGE_CROSSING))
3782 continue;
3784 /* Otherwise we can try to invert the jump. This will
3785 basically never fail, however, keep up the pretense. */
3786 else if (invert_jump (bb_end_insn,
3787 (e_fall->dest
3788 == EXIT_BLOCK_PTR_FOR_FN (cfun)
3789 ? NULL_RTX
3790 : label_for_bb (e_fall->dest)), 0))
3792 e_fall->flags &= ~EDGE_FALLTHRU;
3793 gcc_checking_assert (could_fall_through
3794 (e_taken->src, e_taken->dest));
3795 e_taken->flags |= EDGE_FALLTHRU;
3796 update_br_prob_note (bb);
3797 if (LABEL_NUSES (ret_label) == 0
3798 && single_pred_p (e_taken->dest))
3799 delete_insn (ret_label);
3800 continue;
3803 else if (extract_asm_operands (PATTERN (bb_end_insn)) != NULL)
3805 /* If the old fallthru is still next or if
3806 asm goto doesn't have a fallthru (e.g. when followed by
3807 __builtin_unreachable ()), nothing to do. */
3808 if (! e_fall
3809 || bb->aux == e_fall->dest
3810 || e_fall->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
3811 continue;
3813 /* Otherwise we'll have to use the fallthru fixup below. */
3815 else
3817 /* Otherwise we have some return, switch or computed
3818 jump. In the 99% case, there should not have been a
3819 fallthru edge. */
3820 gcc_assert (returnjump_p (bb_end_insn) || !e_fall);
3821 continue;
3824 else
3826 /* No fallthru implies a noreturn function with EH edges, or
3827 something similarly bizarre. In any case, we don't need to
3828 do anything. */
3829 if (! e_fall)
3830 continue;
3832 /* If the fallthru block is still next, nothing to do. */
3833 if (bb->aux == e_fall->dest)
3834 continue;
3836 /* A fallthru to exit block. */
3837 if (e_fall->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
3838 continue;
3841 /* We got here if we need to add a new jump insn.
3842 Note force_nonfallthru can delete E_FALL and thus we have to
3843 save E_FALL->src prior to the call to force_nonfallthru. */
3844 nb = force_nonfallthru_and_redirect (e_fall, e_fall->dest, ret_label);
3845 if (nb)
3847 nb->aux = bb->aux;
3848 bb->aux = nb;
3849 /* Don't process this new block. */
3850 bb = nb;
3854 relink_block_chain (/*stay_in_cfglayout_mode=*/false);
3856 /* Annoying special case - jump around dead jumptables left in the code. */
3857 FOR_EACH_BB (bb)
3859 edge e = find_fallthru_edge (bb->succs);
3861 if (e && !can_fallthru (e->src, e->dest))
3862 force_nonfallthru (e);
3865 /* Ensure goto_locus from edges has some instructions with that locus
3866 in RTL. */
3867 if (!optimize)
3868 FOR_EACH_BB (bb)
3870 edge e;
3871 edge_iterator ei;
3873 FOR_EACH_EDGE (e, ei, bb->succs)
3874 if (LOCATION_LOCUS (e->goto_locus) != UNKNOWN_LOCATION
3875 && !(e->flags & EDGE_ABNORMAL))
3877 edge e2;
3878 edge_iterator ei2;
3879 basic_block dest, nb;
3880 rtx end;
3882 insn = BB_END (e->src);
3883 end = PREV_INSN (BB_HEAD (e->src));
3884 while (insn != end
3885 && (!NONDEBUG_INSN_P (insn) || !INSN_HAS_LOCATION (insn)))
3886 insn = PREV_INSN (insn);
3887 if (insn != end
3888 && INSN_LOCATION (insn) == e->goto_locus)
3889 continue;
3890 if (simplejump_p (BB_END (e->src))
3891 && !INSN_HAS_LOCATION (BB_END (e->src)))
3893 INSN_LOCATION (BB_END (e->src)) = e->goto_locus;
3894 continue;
3896 dest = e->dest;
3897 if (dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
3899 /* Non-fallthru edges to the exit block cannot be split. */
3900 if (!(e->flags & EDGE_FALLTHRU))
3901 continue;
3903 else
3905 insn = BB_HEAD (dest);
3906 end = NEXT_INSN (BB_END (dest));
3907 while (insn != end && !NONDEBUG_INSN_P (insn))
3908 insn = NEXT_INSN (insn);
3909 if (insn != end && INSN_HAS_LOCATION (insn)
3910 && INSN_LOCATION (insn) == e->goto_locus)
3911 continue;
3913 nb = split_edge (e);
3914 if (!INSN_P (BB_END (nb)))
3915 BB_END (nb) = emit_insn_after_noloc (gen_nop (), BB_END (nb),
3916 nb);
3917 INSN_LOCATION (BB_END (nb)) = e->goto_locus;
3919 /* If there are other incoming edges to the destination block
3920 with the same goto locus, redirect them to the new block as
3921 well, this can prevent other such blocks from being created
3922 in subsequent iterations of the loop. */
3923 for (ei2 = ei_start (dest->preds); (e2 = ei_safe_edge (ei2)); )
3924 if (LOCATION_LOCUS (e2->goto_locus) != UNKNOWN_LOCATION
3925 && !(e2->flags & (EDGE_ABNORMAL | EDGE_FALLTHRU))
3926 && e->goto_locus == e2->goto_locus)
3927 redirect_edge_and_branch (e2, nb);
3928 else
3929 ei_next (&ei2);
3934 /* Perform sanity checks on the insn chain.
3935 1. Check that next/prev pointers are consistent in both the forward and
3936 reverse direction.
3937 2. Count insns in chain, going both directions, and check if equal.
3938 3. Check that get_last_insn () returns the actual end of chain. */
3940 DEBUG_FUNCTION void
3941 verify_insn_chain (void)
3943 rtx x, prevx, nextx;
3944 int insn_cnt1, insn_cnt2;
3946 for (prevx = NULL, insn_cnt1 = 1, x = get_insns ();
3947 x != 0;
3948 prevx = x, insn_cnt1++, x = NEXT_INSN (x))
3949 gcc_assert (PREV_INSN (x) == prevx);
3951 gcc_assert (prevx == get_last_insn ());
3953 for (nextx = NULL, insn_cnt2 = 1, x = get_last_insn ();
3954 x != 0;
3955 nextx = x, insn_cnt2++, x = PREV_INSN (x))
3956 gcc_assert (NEXT_INSN (x) == nextx);
3958 gcc_assert (insn_cnt1 == insn_cnt2);
3961 /* If we have assembler epilogues, the block falling through to exit must
3962 be the last one in the reordered chain when we reach final. Ensure
3963 that this condition is met. */
3964 static void
3965 fixup_fallthru_exit_predecessor (void)
3967 edge e;
3968 basic_block bb = NULL;
3970 /* This transformation is not valid before reload, because we might
3971 separate a call from the instruction that copies the return
3972 value. */
3973 gcc_assert (reload_completed);
3975 e = find_fallthru_edge (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
3976 if (e)
3977 bb = e->src;
3979 if (bb && bb->aux)
3981 basic_block c = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb;
3983 /* If the very first block is the one with the fall-through exit
3984 edge, we have to split that block. */
3985 if (c == bb)
3987 bb = split_block (bb, NULL)->dest;
3988 bb->aux = c->aux;
3989 c->aux = bb;
3990 BB_FOOTER (bb) = BB_FOOTER (c);
3991 BB_FOOTER (c) = NULL;
3994 while (c->aux != bb)
3995 c = (basic_block) c->aux;
3997 c->aux = bb->aux;
3998 while (c->aux)
3999 c = (basic_block) c->aux;
4001 c->aux = bb;
4002 bb->aux = NULL;
4006 /* In case there are more than one fallthru predecessors of exit, force that
4007 there is only one. */
4009 static void
4010 force_one_exit_fallthru (void)
4012 edge e, predecessor = NULL;
4013 bool more = false;
4014 edge_iterator ei;
4015 basic_block forwarder, bb;
4017 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
4018 if (e->flags & EDGE_FALLTHRU)
4020 if (predecessor == NULL)
4021 predecessor = e;
4022 else
4024 more = true;
4025 break;
4029 if (!more)
4030 return;
4032 /* Exit has several fallthru predecessors. Create a forwarder block for
4033 them. */
4034 forwarder = split_edge (predecessor);
4035 for (ei = ei_start (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
4036 (e = ei_safe_edge (ei)); )
4038 if (e->src == forwarder
4039 || !(e->flags & EDGE_FALLTHRU))
4040 ei_next (&ei);
4041 else
4042 redirect_edge_and_branch_force (e, forwarder);
4045 /* Fix up the chain of blocks -- make FORWARDER immediately precede the
4046 exit block. */
4047 FOR_EACH_BB (bb)
4049 if (bb->aux == NULL && bb != forwarder)
4051 bb->aux = forwarder;
4052 break;
4057 /* Return true in case it is possible to duplicate the basic block BB. */
4059 static bool
4060 cfg_layout_can_duplicate_bb_p (const_basic_block bb)
4062 /* Do not attempt to duplicate tablejumps, as we need to unshare
4063 the dispatch table. This is difficult to do, as the instructions
4064 computing jump destination may be hoisted outside the basic block. */
4065 if (tablejump_p (BB_END (bb), NULL, NULL))
4066 return false;
4068 /* Do not duplicate blocks containing insns that can't be copied. */
4069 if (targetm.cannot_copy_insn_p)
4071 rtx insn = BB_HEAD (bb);
4072 while (1)
4074 if (INSN_P (insn) && targetm.cannot_copy_insn_p (insn))
4075 return false;
4076 if (insn == BB_END (bb))
4077 break;
4078 insn = NEXT_INSN (insn);
4082 return true;
4086 duplicate_insn_chain (rtx from, rtx to)
4088 rtx insn, next, last, copy;
4090 /* Avoid updating of boundaries of previous basic block. The
4091 note will get removed from insn stream in fixup. */
4092 last = emit_note (NOTE_INSN_DELETED);
4094 /* Create copy at the end of INSN chain. The chain will
4095 be reordered later. */
4096 for (insn = from; insn != NEXT_INSN (to); insn = NEXT_INSN (insn))
4098 switch (GET_CODE (insn))
4100 case DEBUG_INSN:
4101 /* Don't duplicate label debug insns. */
4102 if (TREE_CODE (INSN_VAR_LOCATION_DECL (insn)) == LABEL_DECL)
4103 break;
4104 /* FALLTHRU */
4105 case INSN:
4106 case CALL_INSN:
4107 case JUMP_INSN:
4108 copy = emit_copy_of_insn_after (insn, get_last_insn ());
4109 if (JUMP_P (insn) && JUMP_LABEL (insn) != NULL_RTX
4110 && ANY_RETURN_P (JUMP_LABEL (insn)))
4111 JUMP_LABEL (copy) = JUMP_LABEL (insn);
4112 maybe_copy_prologue_epilogue_insn (insn, copy);
4113 break;
4115 case JUMP_TABLE_DATA:
4116 /* Avoid copying of dispatch tables. We never duplicate
4117 tablejumps, so this can hit only in case the table got
4118 moved far from original jump.
4119 Avoid copying following barrier as well if any
4120 (and debug insns in between). */
4121 for (next = NEXT_INSN (insn);
4122 next != NEXT_INSN (to);
4123 next = NEXT_INSN (next))
4124 if (!DEBUG_INSN_P (next))
4125 break;
4126 if (next != NEXT_INSN (to) && BARRIER_P (next))
4127 insn = next;
4128 break;
4130 case CODE_LABEL:
4131 break;
4133 case BARRIER:
4134 emit_barrier ();
4135 break;
4137 case NOTE:
4138 switch (NOTE_KIND (insn))
4140 /* In case prologue is empty and function contain label
4141 in first BB, we may want to copy the block. */
4142 case NOTE_INSN_PROLOGUE_END:
4144 case NOTE_INSN_DELETED:
4145 case NOTE_INSN_DELETED_LABEL:
4146 case NOTE_INSN_DELETED_DEBUG_LABEL:
4147 /* No problem to strip these. */
4148 case NOTE_INSN_FUNCTION_BEG:
4149 /* There is always just single entry to function. */
4150 case NOTE_INSN_BASIC_BLOCK:
4151 /* We should only switch text sections once. */
4152 case NOTE_INSN_SWITCH_TEXT_SECTIONS:
4153 break;
4155 case NOTE_INSN_EPILOGUE_BEG:
4156 emit_note_copy (insn);
4157 break;
4159 default:
4160 /* All other notes should have already been eliminated. */
4161 gcc_unreachable ();
4163 break;
4164 default:
4165 gcc_unreachable ();
4168 insn = NEXT_INSN (last);
4169 delete_insn (last);
4170 return insn;
4173 /* Create a duplicate of the basic block BB. */
4175 static basic_block
4176 cfg_layout_duplicate_bb (basic_block bb)
4178 rtx insn;
4179 basic_block new_bb;
4181 insn = duplicate_insn_chain (BB_HEAD (bb), BB_END (bb));
4182 new_bb = create_basic_block (insn,
4183 insn ? get_last_insn () : NULL,
4184 EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb);
4186 BB_COPY_PARTITION (new_bb, bb);
4187 if (BB_HEADER (bb))
4189 insn = BB_HEADER (bb);
4190 while (NEXT_INSN (insn))
4191 insn = NEXT_INSN (insn);
4192 insn = duplicate_insn_chain (BB_HEADER (bb), insn);
4193 if (insn)
4194 BB_HEADER (new_bb) = unlink_insn_chain (insn, get_last_insn ());
4197 if (BB_FOOTER (bb))
4199 insn = BB_FOOTER (bb);
4200 while (NEXT_INSN (insn))
4201 insn = NEXT_INSN (insn);
4202 insn = duplicate_insn_chain (BB_FOOTER (bb), insn);
4203 if (insn)
4204 BB_FOOTER (new_bb) = unlink_insn_chain (insn, get_last_insn ());
4207 return new_bb;
4211 /* Main entry point to this module - initialize the datastructures for
4212 CFG layout changes. It keeps LOOPS up-to-date if not null.
4214 FLAGS is a set of additional flags to pass to cleanup_cfg(). */
4216 void
4217 cfg_layout_initialize (unsigned int flags)
4219 rtx x;
4220 basic_block bb;
4222 /* Once bb reordering is complete, cfg layout mode should not be re-entered.
4223 Entering cfg layout mode will perform optimizations on the cfg that
4224 could affect the bb layout negatively or even require fixups. An
4225 example of the latter is if edge forwarding performed when optimizing
4226 the cfg layout required moving a block from the hot to the cold section
4227 under -freorder-blocks-and-partition. This would create an illegal
4228 partitioning unless some manual fixup was performed. */
4229 gcc_assert (!crtl->bb_reorder_complete);
4231 initialize_original_copy_tables ();
4233 cfg_layout_rtl_register_cfg_hooks ();
4235 record_effective_endpoints ();
4237 /* Make sure that the targets of non local gotos are marked. */
4238 for (x = nonlocal_goto_handler_labels; x; x = XEXP (x, 1))
4240 bb = BLOCK_FOR_INSN (XEXP (x, 0));
4241 bb->flags |= BB_NON_LOCAL_GOTO_TARGET;
4244 cleanup_cfg (CLEANUP_CFGLAYOUT | flags);
4247 /* Splits superblocks. */
4248 void
4249 break_superblocks (void)
4251 sbitmap superblocks;
4252 bool need = false;
4253 basic_block bb;
4255 superblocks = sbitmap_alloc (last_basic_block);
4256 bitmap_clear (superblocks);
4258 FOR_EACH_BB (bb)
4259 if (bb->flags & BB_SUPERBLOCK)
4261 bb->flags &= ~BB_SUPERBLOCK;
4262 bitmap_set_bit (superblocks, bb->index);
4263 need = true;
4266 if (need)
4268 rebuild_jump_labels (get_insns ());
4269 find_many_sub_basic_blocks (superblocks);
4272 free (superblocks);
4275 /* Finalize the changes: reorder insn list according to the sequence specified
4276 by aux pointers, enter compensation code, rebuild scope forest. */
4278 void
4279 cfg_layout_finalize (void)
4281 #ifdef ENABLE_CHECKING
4282 verify_flow_info ();
4283 #endif
4284 force_one_exit_fallthru ();
4285 rtl_register_cfg_hooks ();
4286 if (reload_completed
4287 #ifdef HAVE_epilogue
4288 && !HAVE_epilogue
4289 #endif
4291 fixup_fallthru_exit_predecessor ();
4292 fixup_reorder_chain ();
4294 rebuild_jump_labels (get_insns ());
4295 delete_dead_jumptables ();
4297 #ifdef ENABLE_CHECKING
4298 verify_insn_chain ();
4299 verify_flow_info ();
4300 #endif
4304 /* Same as split_block but update cfg_layout structures. */
4306 static basic_block
4307 cfg_layout_split_block (basic_block bb, void *insnp)
4309 rtx insn = (rtx) insnp;
4310 basic_block new_bb = rtl_split_block (bb, insn);
4312 BB_FOOTER (new_bb) = BB_FOOTER (bb);
4313 BB_FOOTER (bb) = NULL;
4315 return new_bb;
4318 /* Redirect Edge to DEST. */
4319 static edge
4320 cfg_layout_redirect_edge_and_branch (edge e, basic_block dest)
4322 basic_block src = e->src;
4323 edge ret;
4325 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
4326 return NULL;
4328 if (e->dest == dest)
4329 return e;
4331 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
4332 && (ret = try_redirect_by_replacing_jump (e, dest, true)))
4334 df_set_bb_dirty (src);
4335 return ret;
4338 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun)
4339 && (e->flags & EDGE_FALLTHRU) && !(e->flags & EDGE_COMPLEX))
4341 if (dump_file)
4342 fprintf (dump_file, "Redirecting entry edge from bb %i to %i\n",
4343 e->src->index, dest->index);
4345 df_set_bb_dirty (e->src);
4346 redirect_edge_succ (e, dest);
4347 return e;
4350 /* Redirect_edge_and_branch may decide to turn branch into fallthru edge
4351 in the case the basic block appears to be in sequence. Avoid this
4352 transformation. */
4354 if (e->flags & EDGE_FALLTHRU)
4356 /* Redirect any branch edges unified with the fallthru one. */
4357 if (JUMP_P (BB_END (src))
4358 && label_is_jump_target_p (BB_HEAD (e->dest),
4359 BB_END (src)))
4361 edge redirected;
4363 if (dump_file)
4364 fprintf (dump_file, "Fallthru edge unified with branch "
4365 "%i->%i redirected to %i\n",
4366 e->src->index, e->dest->index, dest->index);
4367 e->flags &= ~EDGE_FALLTHRU;
4368 redirected = redirect_branch_edge (e, dest);
4369 gcc_assert (redirected);
4370 redirected->flags |= EDGE_FALLTHRU;
4371 df_set_bb_dirty (redirected->src);
4372 return redirected;
4374 /* In case we are redirecting fallthru edge to the branch edge
4375 of conditional jump, remove it. */
4376 if (EDGE_COUNT (src->succs) == 2)
4378 /* Find the edge that is different from E. */
4379 edge s = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e);
4381 if (s->dest == dest
4382 && any_condjump_p (BB_END (src))
4383 && onlyjump_p (BB_END (src)))
4384 delete_insn (BB_END (src));
4386 if (dump_file)
4387 fprintf (dump_file, "Redirecting fallthru edge %i->%i to %i\n",
4388 e->src->index, e->dest->index, dest->index);
4389 ret = redirect_edge_succ_nodup (e, dest);
4391 else
4392 ret = redirect_branch_edge (e, dest);
4394 /* We don't want simplejumps in the insn stream during cfglayout. */
4395 gcc_assert (!simplejump_p (BB_END (src)));
4397 df_set_bb_dirty (src);
4398 return ret;
4401 /* Simple wrapper as we always can redirect fallthru edges. */
4402 static basic_block
4403 cfg_layout_redirect_edge_and_branch_force (edge e, basic_block dest)
4405 edge redirected = cfg_layout_redirect_edge_and_branch (e, dest);
4407 gcc_assert (redirected);
4408 return NULL;
4411 /* Same as delete_basic_block but update cfg_layout structures. */
4413 static void
4414 cfg_layout_delete_block (basic_block bb)
4416 rtx insn, next, prev = PREV_INSN (BB_HEAD (bb)), *to, remaints;
4418 if (BB_HEADER (bb))
4420 next = BB_HEAD (bb);
4421 if (prev)
4422 NEXT_INSN (prev) = BB_HEADER (bb);
4423 else
4424 set_first_insn (BB_HEADER (bb));
4425 PREV_INSN (BB_HEADER (bb)) = prev;
4426 insn = BB_HEADER (bb);
4427 while (NEXT_INSN (insn))
4428 insn = NEXT_INSN (insn);
4429 NEXT_INSN (insn) = next;
4430 PREV_INSN (next) = insn;
4432 next = NEXT_INSN (BB_END (bb));
4433 if (BB_FOOTER (bb))
4435 insn = BB_FOOTER (bb);
4436 while (insn)
4438 if (BARRIER_P (insn))
4440 if (PREV_INSN (insn))
4441 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
4442 else
4443 BB_FOOTER (bb) = NEXT_INSN (insn);
4444 if (NEXT_INSN (insn))
4445 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
4447 if (LABEL_P (insn))
4448 break;
4449 insn = NEXT_INSN (insn);
4451 if (BB_FOOTER (bb))
4453 insn = BB_END (bb);
4454 NEXT_INSN (insn) = BB_FOOTER (bb);
4455 PREV_INSN (BB_FOOTER (bb)) = insn;
4456 while (NEXT_INSN (insn))
4457 insn = NEXT_INSN (insn);
4458 NEXT_INSN (insn) = next;
4459 if (next)
4460 PREV_INSN (next) = insn;
4461 else
4462 set_last_insn (insn);
4465 if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
4466 to = &BB_HEADER (bb->next_bb);
4467 else
4468 to = &cfg_layout_function_footer;
4470 rtl_delete_block (bb);
4472 if (prev)
4473 prev = NEXT_INSN (prev);
4474 else
4475 prev = get_insns ();
4476 if (next)
4477 next = PREV_INSN (next);
4478 else
4479 next = get_last_insn ();
4481 if (next && NEXT_INSN (next) != prev)
4483 remaints = unlink_insn_chain (prev, next);
4484 insn = remaints;
4485 while (NEXT_INSN (insn))
4486 insn = NEXT_INSN (insn);
4487 NEXT_INSN (insn) = *to;
4488 if (*to)
4489 PREV_INSN (*to) = insn;
4490 *to = remaints;
4494 /* Return true when blocks A and B can be safely merged. */
4496 static bool
4497 cfg_layout_can_merge_blocks_p (basic_block a, basic_block b)
4499 /* If we are partitioning hot/cold basic blocks, we don't want to
4500 mess up unconditional or indirect jumps that cross between hot
4501 and cold sections.
4503 Basic block partitioning may result in some jumps that appear to
4504 be optimizable (or blocks that appear to be mergeable), but which really
4505 must be left untouched (they are required to make it safely across
4506 partition boundaries). See the comments at the top of
4507 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
4509 if (BB_PARTITION (a) != BB_PARTITION (b))
4510 return false;
4512 /* Protect the loop latches. */
4513 if (current_loops && b->loop_father->latch == b)
4514 return false;
4516 /* If we would end up moving B's instructions, make sure it doesn't fall
4517 through into the exit block, since we cannot recover from a fallthrough
4518 edge into the exit block occurring in the middle of a function. */
4519 if (NEXT_INSN (BB_END (a)) != BB_HEAD (b))
4521 edge e = find_fallthru_edge (b->succs);
4522 if (e && e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
4523 return false;
4526 /* There must be exactly one edge in between the blocks. */
4527 return (single_succ_p (a)
4528 && single_succ (a) == b
4529 && single_pred_p (b) == 1
4530 && a != b
4531 /* Must be simple edge. */
4532 && !(single_succ_edge (a)->flags & EDGE_COMPLEX)
4533 && a != ENTRY_BLOCK_PTR_FOR_FN (cfun)
4534 && b != EXIT_BLOCK_PTR_FOR_FN (cfun)
4535 /* If the jump insn has side effects, we can't kill the edge.
4536 When not optimizing, try_redirect_by_replacing_jump will
4537 not allow us to redirect an edge by replacing a table jump. */
4538 && (!JUMP_P (BB_END (a))
4539 || ((!optimize || reload_completed)
4540 ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
4543 /* Merge block A and B. The blocks must be mergeable. */
4545 static void
4546 cfg_layout_merge_blocks (basic_block a, basic_block b)
4548 bool forwarder_p = (b->flags & BB_FORWARDER_BLOCK) != 0;
4549 rtx insn;
4551 gcc_checking_assert (cfg_layout_can_merge_blocks_p (a, b));
4553 if (dump_file)
4554 fprintf (dump_file, "Merging block %d into block %d...\n", b->index,
4555 a->index);
4557 /* If there was a CODE_LABEL beginning B, delete it. */
4558 if (LABEL_P (BB_HEAD (b)))
4560 delete_insn (BB_HEAD (b));
4563 /* We should have fallthru edge in a, or we can do dummy redirection to get
4564 it cleaned up. */
4565 if (JUMP_P (BB_END (a)))
4566 try_redirect_by_replacing_jump (EDGE_SUCC (a, 0), b, true);
4567 gcc_assert (!JUMP_P (BB_END (a)));
4569 /* When not optimizing CFG and the edge is the only place in RTL which holds
4570 some unique locus, emit a nop with that locus in between. */
4571 if (!optimize)
4572 emit_nop_for_unique_locus_between (a, b);
4574 /* Move things from b->footer after a->footer. */
4575 if (BB_FOOTER (b))
4577 if (!BB_FOOTER (a))
4578 BB_FOOTER (a) = BB_FOOTER (b);
4579 else
4581 rtx last = BB_FOOTER (a);
4583 while (NEXT_INSN (last))
4584 last = NEXT_INSN (last);
4585 NEXT_INSN (last) = BB_FOOTER (b);
4586 PREV_INSN (BB_FOOTER (b)) = last;
4588 BB_FOOTER (b) = NULL;
4591 /* Move things from b->header before a->footer.
4592 Note that this may include dead tablejump data, but we don't clean
4593 those up until we go out of cfglayout mode. */
4594 if (BB_HEADER (b))
4596 if (! BB_FOOTER (a))
4597 BB_FOOTER (a) = BB_HEADER (b);
4598 else
4600 rtx last = BB_HEADER (b);
4602 while (NEXT_INSN (last))
4603 last = NEXT_INSN (last);
4604 NEXT_INSN (last) = BB_FOOTER (a);
4605 PREV_INSN (BB_FOOTER (a)) = last;
4606 BB_FOOTER (a) = BB_HEADER (b);
4608 BB_HEADER (b) = NULL;
4611 /* In the case basic blocks are not adjacent, move them around. */
4612 if (NEXT_INSN (BB_END (a)) != BB_HEAD (b))
4614 insn = unlink_insn_chain (BB_HEAD (b), BB_END (b));
4616 emit_insn_after_noloc (insn, BB_END (a), a);
4618 /* Otherwise just re-associate the instructions. */
4619 else
4621 insn = BB_HEAD (b);
4622 BB_END (a) = BB_END (b);
4625 /* emit_insn_after_noloc doesn't call df_insn_change_bb.
4626 We need to explicitly call. */
4627 update_bb_for_insn_chain (insn, BB_END (b), a);
4629 /* Skip possible DELETED_LABEL insn. */
4630 if (!NOTE_INSN_BASIC_BLOCK_P (insn))
4631 insn = NEXT_INSN (insn);
4632 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
4633 BB_HEAD (b) = BB_END (b) = NULL;
4634 delete_insn (insn);
4636 df_bb_delete (b->index);
4638 /* If B was a forwarder block, propagate the locus on the edge. */
4639 if (forwarder_p
4640 && LOCATION_LOCUS (EDGE_SUCC (b, 0)->goto_locus) == UNKNOWN_LOCATION)
4641 EDGE_SUCC (b, 0)->goto_locus = EDGE_SUCC (a, 0)->goto_locus;
4643 if (dump_file)
4644 fprintf (dump_file, "Merged blocks %d and %d.\n", a->index, b->index);
4647 /* Split edge E. */
4649 static basic_block
4650 cfg_layout_split_edge (edge e)
4652 basic_block new_bb =
4653 create_basic_block (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
4654 ? NEXT_INSN (BB_END (e->src)) : get_insns (),
4655 NULL_RTX, e->src);
4657 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
4658 BB_COPY_PARTITION (new_bb, e->src);
4659 else
4660 BB_COPY_PARTITION (new_bb, e->dest);
4661 make_edge (new_bb, e->dest, EDGE_FALLTHRU);
4662 redirect_edge_and_branch_force (e, new_bb);
4664 return new_bb;
4667 /* Do postprocessing after making a forwarder block joined by edge FALLTHRU. */
4669 static void
4670 rtl_make_forwarder_block (edge fallthru ATTRIBUTE_UNUSED)
4674 /* Return true if BB contains only labels or non-executable
4675 instructions. */
4677 static bool
4678 rtl_block_empty_p (basic_block bb)
4680 rtx insn;
4682 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)
4683 || bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
4684 return true;
4686 FOR_BB_INSNS (bb, insn)
4687 if (NONDEBUG_INSN_P (insn) && !any_uncondjump_p (insn))
4688 return false;
4690 return true;
4693 /* Split a basic block if it ends with a conditional branch and if
4694 the other part of the block is not empty. */
4696 static basic_block
4697 rtl_split_block_before_cond_jump (basic_block bb)
4699 rtx insn;
4700 rtx split_point = NULL;
4701 rtx last = NULL;
4702 bool found_code = false;
4704 FOR_BB_INSNS (bb, insn)
4706 if (any_condjump_p (insn))
4707 split_point = last;
4708 else if (NONDEBUG_INSN_P (insn))
4709 found_code = true;
4710 last = insn;
4713 /* Did not find everything. */
4714 if (found_code && split_point)
4715 return split_block (bb, split_point)->dest;
4716 else
4717 return NULL;
4720 /* Return 1 if BB ends with a call, possibly followed by some
4721 instructions that must stay with the call, 0 otherwise. */
4723 static bool
4724 rtl_block_ends_with_call_p (basic_block bb)
4726 rtx insn = BB_END (bb);
4728 while (!CALL_P (insn)
4729 && insn != BB_HEAD (bb)
4730 && (keep_with_call_p (insn)
4731 || NOTE_P (insn)
4732 || DEBUG_INSN_P (insn)))
4733 insn = PREV_INSN (insn);
4734 return (CALL_P (insn));
4737 /* Return 1 if BB ends with a conditional branch, 0 otherwise. */
4739 static bool
4740 rtl_block_ends_with_condjump_p (const_basic_block bb)
4742 return any_condjump_p (BB_END (bb));
4745 /* Return true if we need to add fake edge to exit.
4746 Helper function for rtl_flow_call_edges_add. */
4748 static bool
4749 need_fake_edge_p (const_rtx insn)
4751 if (!INSN_P (insn))
4752 return false;
4754 if ((CALL_P (insn)
4755 && !SIBLING_CALL_P (insn)
4756 && !find_reg_note (insn, REG_NORETURN, NULL)
4757 && !(RTL_CONST_OR_PURE_CALL_P (insn))))
4758 return true;
4760 return ((GET_CODE (PATTERN (insn)) == ASM_OPERANDS
4761 && MEM_VOLATILE_P (PATTERN (insn)))
4762 || (GET_CODE (PATTERN (insn)) == PARALLEL
4763 && asm_noperands (insn) != -1
4764 && MEM_VOLATILE_P (XVECEXP (PATTERN (insn), 0, 0)))
4765 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
4768 /* Add fake edges to the function exit for any non constant and non noreturn
4769 calls, volatile inline assembly in the bitmap of blocks specified by
4770 BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
4771 that were split.
4773 The goal is to expose cases in which entering a basic block does not imply
4774 that all subsequent instructions must be executed. */
4776 static int
4777 rtl_flow_call_edges_add (sbitmap blocks)
4779 int i;
4780 int blocks_split = 0;
4781 int last_bb = last_basic_block;
4782 bool check_last_block = false;
4784 if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS)
4785 return 0;
4787 if (! blocks)
4788 check_last_block = true;
4789 else
4790 check_last_block = bitmap_bit_p (blocks,
4791 EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb->index);
4793 /* In the last basic block, before epilogue generation, there will be
4794 a fallthru edge to EXIT. Special care is required if the last insn
4795 of the last basic block is a call because make_edge folds duplicate
4796 edges, which would result in the fallthru edge also being marked
4797 fake, which would result in the fallthru edge being removed by
4798 remove_fake_edges, which would result in an invalid CFG.
4800 Moreover, we can't elide the outgoing fake edge, since the block
4801 profiler needs to take this into account in order to solve the minimal
4802 spanning tree in the case that the call doesn't return.
4804 Handle this by adding a dummy instruction in a new last basic block. */
4805 if (check_last_block)
4807 basic_block bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
4808 rtx insn = BB_END (bb);
4810 /* Back up past insns that must be kept in the same block as a call. */
4811 while (insn != BB_HEAD (bb)
4812 && keep_with_call_p (insn))
4813 insn = PREV_INSN (insn);
4815 if (need_fake_edge_p (insn))
4817 edge e;
4819 e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
4820 if (e)
4822 insert_insn_on_edge (gen_use (const0_rtx), e);
4823 commit_edge_insertions ();
4828 /* Now add fake edges to the function exit for any non constant
4829 calls since there is no way that we can determine if they will
4830 return or not... */
4832 for (i = NUM_FIXED_BLOCKS; i < last_bb; i++)
4834 basic_block bb = BASIC_BLOCK (i);
4835 rtx insn;
4836 rtx prev_insn;
4838 if (!bb)
4839 continue;
4841 if (blocks && !bitmap_bit_p (blocks, i))
4842 continue;
4844 for (insn = BB_END (bb); ; insn = prev_insn)
4846 prev_insn = PREV_INSN (insn);
4847 if (need_fake_edge_p (insn))
4849 edge e;
4850 rtx split_at_insn = insn;
4852 /* Don't split the block between a call and an insn that should
4853 remain in the same block as the call. */
4854 if (CALL_P (insn))
4855 while (split_at_insn != BB_END (bb)
4856 && keep_with_call_p (NEXT_INSN (split_at_insn)))
4857 split_at_insn = NEXT_INSN (split_at_insn);
4859 /* The handling above of the final block before the epilogue
4860 should be enough to verify that there is no edge to the exit
4861 block in CFG already. Calling make_edge in such case would
4862 cause us to mark that edge as fake and remove it later. */
4864 #ifdef ENABLE_CHECKING
4865 if (split_at_insn == BB_END (bb))
4867 e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
4868 gcc_assert (e == NULL);
4870 #endif
4872 /* Note that the following may create a new basic block
4873 and renumber the existing basic blocks. */
4874 if (split_at_insn != BB_END (bb))
4876 e = split_block (bb, split_at_insn);
4877 if (e)
4878 blocks_split++;
4881 make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FAKE);
4884 if (insn == BB_HEAD (bb))
4885 break;
4889 if (blocks_split)
4890 verify_flow_info ();
4892 return blocks_split;
4895 /* Add COMP_RTX as a condition at end of COND_BB. FIRST_HEAD is
4896 the conditional branch target, SECOND_HEAD should be the fall-thru
4897 there is no need to handle this here the loop versioning code handles
4898 this. the reason for SECON_HEAD is that it is needed for condition
4899 in trees, and this should be of the same type since it is a hook. */
4900 static void
4901 rtl_lv_add_condition_to_bb (basic_block first_head ,
4902 basic_block second_head ATTRIBUTE_UNUSED,
4903 basic_block cond_bb, void *comp_rtx)
4905 rtx label, seq, jump;
4906 rtx op0 = XEXP ((rtx)comp_rtx, 0);
4907 rtx op1 = XEXP ((rtx)comp_rtx, 1);
4908 enum rtx_code comp = GET_CODE ((rtx)comp_rtx);
4909 enum machine_mode mode;
4912 label = block_label (first_head);
4913 mode = GET_MODE (op0);
4914 if (mode == VOIDmode)
4915 mode = GET_MODE (op1);
4917 start_sequence ();
4918 op0 = force_operand (op0, NULL_RTX);
4919 op1 = force_operand (op1, NULL_RTX);
4920 do_compare_rtx_and_jump (op0, op1, comp, 0,
4921 mode, NULL_RTX, NULL_RTX, label, -1);
4922 jump = get_last_insn ();
4923 JUMP_LABEL (jump) = label;
4924 LABEL_NUSES (label)++;
4925 seq = get_insns ();
4926 end_sequence ();
4928 /* Add the new cond , in the new head. */
4929 emit_insn_after (seq, BB_END (cond_bb));
4933 /* Given a block B with unconditional branch at its end, get the
4934 store the return the branch edge and the fall-thru edge in
4935 BRANCH_EDGE and FALLTHRU_EDGE respectively. */
4936 static void
4937 rtl_extract_cond_bb_edges (basic_block b, edge *branch_edge,
4938 edge *fallthru_edge)
4940 edge e = EDGE_SUCC (b, 0);
4942 if (e->flags & EDGE_FALLTHRU)
4944 *fallthru_edge = e;
4945 *branch_edge = EDGE_SUCC (b, 1);
4947 else
4949 *branch_edge = e;
4950 *fallthru_edge = EDGE_SUCC (b, 1);
4954 void
4955 init_rtl_bb_info (basic_block bb)
4957 gcc_assert (!bb->il.x.rtl);
4958 bb->il.x.head_ = NULL;
4959 bb->il.x.rtl = ggc_alloc_cleared_rtl_bb_info ();
4962 /* Returns true if it is possible to remove edge E by redirecting
4963 it to the destination of the other edge from E->src. */
4965 static bool
4966 rtl_can_remove_branch_p (const_edge e)
4968 const_basic_block src = e->src;
4969 const_basic_block target = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest;
4970 const_rtx insn = BB_END (src), set;
4972 /* The conditions are taken from try_redirect_by_replacing_jump. */
4973 if (target == EXIT_BLOCK_PTR_FOR_FN (cfun))
4974 return false;
4976 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
4977 return false;
4979 if (BB_PARTITION (src) != BB_PARTITION (target))
4980 return false;
4982 if (!onlyjump_p (insn)
4983 || tablejump_p (insn, NULL, NULL))
4984 return false;
4986 set = single_set (insn);
4987 if (!set || side_effects_p (set))
4988 return false;
4990 return true;
4993 static basic_block
4994 rtl_duplicate_bb (basic_block bb)
4996 bb = cfg_layout_duplicate_bb (bb);
4997 bb->aux = NULL;
4998 return bb;
5001 /* Do book-keeping of basic block BB for the profile consistency checker.
5002 If AFTER_PASS is 0, do pre-pass accounting, or if AFTER_PASS is 1
5003 then do post-pass accounting. Store the counting in RECORD. */
5004 static void
5005 rtl_account_profile_record (basic_block bb, int after_pass,
5006 struct profile_record *record)
5008 rtx insn;
5009 FOR_BB_INSNS (bb, insn)
5010 if (INSN_P (insn))
5012 record->size[after_pass]
5013 += insn_rtx_cost (PATTERN (insn), false);
5014 if (profile_status == PROFILE_READ)
5015 record->time[after_pass]
5016 += insn_rtx_cost (PATTERN (insn), true) * bb->count;
5017 else if (profile_status == PROFILE_GUESSED)
5018 record->time[after_pass]
5019 += insn_rtx_cost (PATTERN (insn), true) * bb->frequency;
5023 /* Implementation of CFG manipulation for linearized RTL. */
5024 struct cfg_hooks rtl_cfg_hooks = {
5025 "rtl",
5026 rtl_verify_flow_info,
5027 rtl_dump_bb,
5028 rtl_dump_bb_for_graph,
5029 rtl_create_basic_block,
5030 rtl_redirect_edge_and_branch,
5031 rtl_redirect_edge_and_branch_force,
5032 rtl_can_remove_branch_p,
5033 rtl_delete_block,
5034 rtl_split_block,
5035 rtl_move_block_after,
5036 rtl_can_merge_blocks, /* can_merge_blocks_p */
5037 rtl_merge_blocks,
5038 rtl_predict_edge,
5039 rtl_predicted_by_p,
5040 cfg_layout_can_duplicate_bb_p,
5041 rtl_duplicate_bb,
5042 rtl_split_edge,
5043 rtl_make_forwarder_block,
5044 rtl_tidy_fallthru_edge,
5045 rtl_force_nonfallthru,
5046 rtl_block_ends_with_call_p,
5047 rtl_block_ends_with_condjump_p,
5048 rtl_flow_call_edges_add,
5049 NULL, /* execute_on_growing_pred */
5050 NULL, /* execute_on_shrinking_pred */
5051 NULL, /* duplicate loop for trees */
5052 NULL, /* lv_add_condition_to_bb */
5053 NULL, /* lv_adjust_loop_header_phi*/
5054 NULL, /* extract_cond_bb_edges */
5055 NULL, /* flush_pending_stmts */
5056 rtl_block_empty_p, /* block_empty_p */
5057 rtl_split_block_before_cond_jump, /* split_block_before_cond_jump */
5058 rtl_account_profile_record,
5061 /* Implementation of CFG manipulation for cfg layout RTL, where
5062 basic block connected via fallthru edges does not have to be adjacent.
5063 This representation will hopefully become the default one in future
5064 version of the compiler. */
5066 struct cfg_hooks cfg_layout_rtl_cfg_hooks = {
5067 "cfglayout mode",
5068 rtl_verify_flow_info_1,
5069 rtl_dump_bb,
5070 rtl_dump_bb_for_graph,
5071 cfg_layout_create_basic_block,
5072 cfg_layout_redirect_edge_and_branch,
5073 cfg_layout_redirect_edge_and_branch_force,
5074 rtl_can_remove_branch_p,
5075 cfg_layout_delete_block,
5076 cfg_layout_split_block,
5077 rtl_move_block_after,
5078 cfg_layout_can_merge_blocks_p,
5079 cfg_layout_merge_blocks,
5080 rtl_predict_edge,
5081 rtl_predicted_by_p,
5082 cfg_layout_can_duplicate_bb_p,
5083 cfg_layout_duplicate_bb,
5084 cfg_layout_split_edge,
5085 rtl_make_forwarder_block,
5086 NULL, /* tidy_fallthru_edge */
5087 rtl_force_nonfallthru,
5088 rtl_block_ends_with_call_p,
5089 rtl_block_ends_with_condjump_p,
5090 rtl_flow_call_edges_add,
5091 NULL, /* execute_on_growing_pred */
5092 NULL, /* execute_on_shrinking_pred */
5093 duplicate_loop_to_header_edge, /* duplicate loop for trees */
5094 rtl_lv_add_condition_to_bb, /* lv_add_condition_to_bb */
5095 NULL, /* lv_adjust_loop_header_phi*/
5096 rtl_extract_cond_bb_edges, /* extract_cond_bb_edges */
5097 NULL, /* flush_pending_stmts */
5098 rtl_block_empty_p, /* block_empty_p */
5099 rtl_split_block_before_cond_jump, /* split_block_before_cond_jump */
5100 rtl_account_profile_record,
5103 #include "gt-cfgrtl.h"