New vectorizer messages; message format change.
[official-gcc.git] / gcc / cfgrtl.c
blob610fccc46dd79070ffec6d43a9e5706ebc625d84
1 /* Control flow graph manipulation code for GNU compiler.
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file contains low level functions to manipulate the CFG and analyze it
21 that are aware of the RTL intermediate language.
23 Available functionality:
24 - Basic CFG/RTL manipulation API documented in cfghooks.h
25 - CFG-aware instruction chain manipulation
26 delete_insn, delete_insn_chain
27 - Edge splitting and committing to edges
28 insert_insn_on_edge, commit_edge_insertions
29 - CFG updating after insn simplification
30 purge_dead_edges, purge_all_dead_edges
31 - CFG fixing after coarse manipulation
32 fixup_abnormal_edges
34 Functions not supposed for generic use:
35 - Infrastructure to determine quickly basic block for insn
36 compute_bb_for_insn, update_bb_for_insn, set_block_for_insn,
37 - Edge redirection with updating and optimizing of insn chain
38 block_label, tidy_fallthru_edge, force_nonfallthru */
40 #include "config.h"
41 #include "system.h"
42 #include "coretypes.h"
43 #include "tm.h"
44 #include "tree.h"
45 #include "hard-reg-set.h"
46 #include "basic-block.h"
47 #include "bb-reorder.h"
48 #include "regs.h"
49 #include "flags.h"
50 #include "function.h"
51 #include "except.h"
52 #include "rtl-error.h"
53 #include "tm_p.h"
54 #include "obstack.h"
55 #include "insn-attr.h"
56 #include "insn-config.h"
57 #include "expr.h"
58 #include "target.h"
59 #include "common/common-target.h"
60 #include "cfgloop.h"
61 #include "ggc.h"
62 #include "tree-pass.h"
63 #include "df.h"
65 /* Holds the interesting leading and trailing notes for the function.
66 Only applicable if the CFG is in cfglayout mode. */
67 static GTY(()) rtx cfg_layout_function_footer;
68 static GTY(()) rtx cfg_layout_function_header;
70 static rtx skip_insns_after_block (basic_block);
71 static void record_effective_endpoints (void);
72 static rtx label_for_bb (basic_block);
73 static void fixup_reorder_chain (void);
75 void verify_insn_chain (void);
76 static void fixup_fallthru_exit_predecessor (void);
77 static int can_delete_note_p (const_rtx);
78 static int can_delete_label_p (const_rtx);
79 static basic_block rtl_split_edge (edge);
80 static bool rtl_move_block_after (basic_block, basic_block);
81 static int rtl_verify_flow_info (void);
82 static basic_block cfg_layout_split_block (basic_block, void *);
83 static edge cfg_layout_redirect_edge_and_branch (edge, basic_block);
84 static basic_block cfg_layout_redirect_edge_and_branch_force (edge, basic_block);
85 static void cfg_layout_delete_block (basic_block);
86 static void rtl_delete_block (basic_block);
87 static basic_block rtl_redirect_edge_and_branch_force (edge, basic_block);
88 static edge rtl_redirect_edge_and_branch (edge, basic_block);
89 static basic_block rtl_split_block (basic_block, void *);
90 static void rtl_dump_bb (FILE *, basic_block, int, int);
91 static int rtl_verify_flow_info_1 (void);
92 static void rtl_make_forwarder_block (edge);
94 /* Return true if NOTE is not one of the ones that must be kept paired,
95 so that we may simply delete it. */
97 static int
98 can_delete_note_p (const_rtx note)
100 switch (NOTE_KIND (note))
102 case NOTE_INSN_DELETED:
103 case NOTE_INSN_BASIC_BLOCK:
104 case NOTE_INSN_EPILOGUE_BEG:
105 return true;
107 default:
108 return false;
112 /* True if a given label can be deleted. */
114 static int
115 can_delete_label_p (const_rtx label)
117 return (!LABEL_PRESERVE_P (label)
118 /* User declared labels must be preserved. */
119 && LABEL_NAME (label) == 0
120 && !in_expr_list_p (forced_labels, label));
123 /* Delete INSN by patching it out. */
125 void
126 delete_insn (rtx insn)
128 rtx note;
129 bool really_delete = true;
131 if (LABEL_P (insn))
133 /* Some labels can't be directly removed from the INSN chain, as they
134 might be references via variables, constant pool etc.
135 Convert them to the special NOTE_INSN_DELETED_LABEL note. */
136 if (! can_delete_label_p (insn))
138 const char *name = LABEL_NAME (insn);
139 basic_block bb = BLOCK_FOR_INSN (insn);
140 rtx bb_note = NEXT_INSN (insn);
142 really_delete = false;
143 PUT_CODE (insn, NOTE);
144 NOTE_KIND (insn) = NOTE_INSN_DELETED_LABEL;
145 NOTE_DELETED_LABEL_NAME (insn) = name;
147 /* If the note following the label starts a basic block, and the
148 label is a member of the same basic block, interchange the two. */
149 if (bb_note != NULL_RTX
150 && NOTE_INSN_BASIC_BLOCK_P (bb_note)
151 && bb != NULL
152 && bb == BLOCK_FOR_INSN (bb_note))
154 reorder_insns_nobb (insn, insn, bb_note);
155 BB_HEAD (bb) = bb_note;
156 if (BB_END (bb) == bb_note)
157 BB_END (bb) = insn;
161 remove_node_from_expr_list (insn, &nonlocal_goto_handler_labels);
164 if (really_delete)
166 /* If this insn has already been deleted, something is very wrong. */
167 gcc_assert (!INSN_DELETED_P (insn));
168 if (INSN_P (insn))
169 df_insn_delete (insn);
170 remove_insn (insn);
171 INSN_DELETED_P (insn) = 1;
174 /* If deleting a jump, decrement the use count of the label. Deleting
175 the label itself should happen in the normal course of block merging. */
176 if (JUMP_P (insn))
178 if (JUMP_LABEL (insn)
179 && LABEL_P (JUMP_LABEL (insn)))
180 LABEL_NUSES (JUMP_LABEL (insn))--;
182 /* If there are more targets, remove them too. */
183 while ((note
184 = find_reg_note (insn, REG_LABEL_TARGET, NULL_RTX)) != NULL_RTX
185 && LABEL_P (XEXP (note, 0)))
187 LABEL_NUSES (XEXP (note, 0))--;
188 remove_note (insn, note);
192 /* Also if deleting any insn that references a label as an operand. */
193 while ((note = find_reg_note (insn, REG_LABEL_OPERAND, NULL_RTX)) != NULL_RTX
194 && LABEL_P (XEXP (note, 0)))
196 LABEL_NUSES (XEXP (note, 0))--;
197 remove_note (insn, note);
200 if (JUMP_TABLE_DATA_P (insn))
202 rtx pat = PATTERN (insn);
203 int diff_vec_p = GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC;
204 int len = XVECLEN (pat, diff_vec_p);
205 int i;
207 for (i = 0; i < len; i++)
209 rtx label = XEXP (XVECEXP (pat, diff_vec_p, i), 0);
211 /* When deleting code in bulk (e.g. removing many unreachable
212 blocks) we can delete a label that's a target of the vector
213 before deleting the vector itself. */
214 if (!NOTE_P (label))
215 LABEL_NUSES (label)--;
220 /* Like delete_insn but also purge dead edges from BB. */
222 void
223 delete_insn_and_edges (rtx insn)
225 bool purge = false;
227 if (INSN_P (insn)
228 && BLOCK_FOR_INSN (insn)
229 && BB_END (BLOCK_FOR_INSN (insn)) == insn)
230 purge = true;
231 delete_insn (insn);
232 if (purge)
233 purge_dead_edges (BLOCK_FOR_INSN (insn));
236 /* Unlink a chain of insns between START and FINISH, leaving notes
237 that must be paired. If CLEAR_BB is true, we set bb field for
238 insns that cannot be removed to NULL. */
240 void
241 delete_insn_chain (rtx start, rtx finish, bool clear_bb)
243 rtx prev, current;
245 /* Unchain the insns one by one. It would be quicker to delete all of these
246 with a single unchaining, rather than one at a time, but we need to keep
247 the NOTE's. */
248 current = finish;
249 while (1)
251 prev = PREV_INSN (current);
252 if (NOTE_P (current) && !can_delete_note_p (current))
254 else
255 delete_insn (current);
257 if (clear_bb && !INSN_DELETED_P (current))
258 set_block_for_insn (current, NULL);
260 if (current == start)
261 break;
262 current = prev;
266 /* Create a new basic block consisting of the instructions between HEAD and END
267 inclusive. This function is designed to allow fast BB construction - reuses
268 the note and basic block struct in BB_NOTE, if any and do not grow
269 BASIC_BLOCK chain and should be used directly only by CFG construction code.
270 END can be NULL in to create new empty basic block before HEAD. Both END
271 and HEAD can be NULL to create basic block at the end of INSN chain.
272 AFTER is the basic block we should be put after. */
274 basic_block
275 create_basic_block_structure (rtx head, rtx end, rtx bb_note, basic_block after)
277 basic_block bb;
279 if (bb_note
280 && (bb = NOTE_BASIC_BLOCK (bb_note)) != NULL
281 && bb->aux == NULL)
283 /* If we found an existing note, thread it back onto the chain. */
285 rtx after;
287 if (LABEL_P (head))
288 after = head;
289 else
291 after = PREV_INSN (head);
292 head = bb_note;
295 if (after != bb_note && NEXT_INSN (after) != bb_note)
296 reorder_insns_nobb (bb_note, bb_note, after);
298 else
300 /* Otherwise we must create a note and a basic block structure. */
302 bb = alloc_block ();
304 init_rtl_bb_info (bb);
305 if (!head && !end)
306 head = end = bb_note
307 = emit_note_after (NOTE_INSN_BASIC_BLOCK, get_last_insn ());
308 else if (LABEL_P (head) && end)
310 bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, head);
311 if (head == end)
312 end = bb_note;
314 else
316 bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, head);
317 head = bb_note;
318 if (!end)
319 end = head;
322 NOTE_BASIC_BLOCK (bb_note) = bb;
325 /* Always include the bb note in the block. */
326 if (NEXT_INSN (end) == bb_note)
327 end = bb_note;
329 BB_HEAD (bb) = head;
330 BB_END (bb) = end;
331 bb->index = last_basic_block++;
332 bb->flags = BB_NEW | BB_RTL;
333 link_block (bb, after);
334 SET_BASIC_BLOCK (bb->index, bb);
335 df_bb_refs_record (bb->index, false);
336 update_bb_for_insn (bb);
337 BB_SET_PARTITION (bb, BB_UNPARTITIONED);
339 /* Tag the block so that we know it has been used when considering
340 other basic block notes. */
341 bb->aux = bb;
343 return bb;
346 /* Create new basic block consisting of instructions in between HEAD and END
347 and place it to the BB chain after block AFTER. END can be NULL to
348 create a new empty basic block before HEAD. Both END and HEAD can be
349 NULL to create basic block at the end of INSN chain. */
351 static basic_block
352 rtl_create_basic_block (void *headp, void *endp, basic_block after)
354 rtx head = (rtx) headp, end = (rtx) endp;
355 basic_block bb;
357 /* Grow the basic block array if needed. */
358 if ((size_t) last_basic_block >= basic_block_info->length ())
360 size_t new_size = last_basic_block + (last_basic_block + 3) / 4;
361 vec_safe_grow_cleared (basic_block_info, new_size);
364 n_basic_blocks++;
366 bb = create_basic_block_structure (head, end, NULL, after);
367 bb->aux = NULL;
368 return bb;
371 static basic_block
372 cfg_layout_create_basic_block (void *head, void *end, basic_block after)
374 basic_block newbb = rtl_create_basic_block (head, end, after);
376 return newbb;
379 /* Delete the insns in a (non-live) block. We physically delete every
380 non-deleted-note insn, and update the flow graph appropriately.
382 Return nonzero if we deleted an exception handler. */
384 /* ??? Preserving all such notes strikes me as wrong. It would be nice
385 to post-process the stream to remove empty blocks, loops, ranges, etc. */
387 static void
388 rtl_delete_block (basic_block b)
390 rtx insn, end;
392 /* If the head of this block is a CODE_LABEL, then it might be the
393 label for an exception handler which can't be reached. We need
394 to remove the label from the exception_handler_label list. */
395 insn = BB_HEAD (b);
397 end = get_last_bb_insn (b);
399 /* Selectively delete the entire chain. */
400 BB_HEAD (b) = NULL;
401 delete_insn_chain (insn, end, true);
404 if (dump_file)
405 fprintf (dump_file, "deleting block %d\n", b->index);
406 df_bb_delete (b->index);
409 /* Records the basic block struct in BLOCK_FOR_INSN for every insn. */
411 void
412 compute_bb_for_insn (void)
414 basic_block bb;
416 FOR_EACH_BB (bb)
418 rtx end = BB_END (bb);
419 rtx insn;
421 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
423 BLOCK_FOR_INSN (insn) = bb;
424 if (insn == end)
425 break;
430 /* Release the basic_block_for_insn array. */
432 unsigned int
433 free_bb_for_insn (void)
435 rtx insn;
436 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
437 if (!BARRIER_P (insn))
438 BLOCK_FOR_INSN (insn) = NULL;
439 return 0;
442 static unsigned int
443 rest_of_pass_free_cfg (void)
445 #ifdef DELAY_SLOTS
446 /* The resource.c machinery uses DF but the CFG isn't guaranteed to be
447 valid at that point so it would be too late to call df_analyze. */
448 if (optimize > 0 && flag_delayed_branch)
450 df_note_add_problem ();
451 df_analyze ();
453 #endif
455 if (crtl->has_bb_partition)
456 insert_section_boundary_note ();
458 free_bb_for_insn ();
459 return 0;
462 namespace {
464 const pass_data pass_data_free_cfg =
466 RTL_PASS, /* type */
467 "*free_cfg", /* name */
468 OPTGROUP_NONE, /* optinfo_flags */
469 false, /* has_gate */
470 true, /* has_execute */
471 TV_NONE, /* tv_id */
472 0, /* properties_required */
473 0, /* properties_provided */
474 PROP_cfg, /* properties_destroyed */
475 0, /* todo_flags_start */
476 0, /* todo_flags_finish */
479 class pass_free_cfg : public rtl_opt_pass
481 public:
482 pass_free_cfg(gcc::context *ctxt)
483 : rtl_opt_pass(pass_data_free_cfg, ctxt)
486 /* opt_pass methods: */
487 unsigned int execute () { return rest_of_pass_free_cfg (); }
489 }; // class pass_free_cfg
491 } // anon namespace
493 rtl_opt_pass *
494 make_pass_free_cfg (gcc::context *ctxt)
496 return new pass_free_cfg (ctxt);
499 /* Return RTX to emit after when we want to emit code on the entry of function. */
501 entry_of_function (void)
503 return (n_basic_blocks > NUM_FIXED_BLOCKS ?
504 BB_HEAD (ENTRY_BLOCK_PTR->next_bb) : get_insns ());
507 /* Emit INSN at the entry point of the function, ensuring that it is only
508 executed once per function. */
509 void
510 emit_insn_at_entry (rtx insn)
512 edge_iterator ei = ei_start (ENTRY_BLOCK_PTR->succs);
513 edge e = ei_safe_edge (ei);
514 gcc_assert (e->flags & EDGE_FALLTHRU);
516 insert_insn_on_edge (insn, e);
517 commit_edge_insertions ();
520 /* Update BLOCK_FOR_INSN of insns between BEGIN and END
521 (or BARRIER if found) and notify df of the bb change.
522 The insn chain range is inclusive
523 (i.e. both BEGIN and END will be updated. */
525 static void
526 update_bb_for_insn_chain (rtx begin, rtx end, basic_block bb)
528 rtx insn;
530 end = NEXT_INSN (end);
531 for (insn = begin; insn != end; insn = NEXT_INSN (insn))
532 if (!BARRIER_P (insn))
533 df_insn_change_bb (insn, bb);
536 /* Update BLOCK_FOR_INSN of insns in BB to BB,
537 and notify df of the change. */
539 void
540 update_bb_for_insn (basic_block bb)
542 update_bb_for_insn_chain (BB_HEAD (bb), BB_END (bb), bb);
546 /* Like active_insn_p, except keep the return value clobber around
547 even after reload. */
549 static bool
550 flow_active_insn_p (const_rtx insn)
552 if (active_insn_p (insn))
553 return true;
555 /* A clobber of the function return value exists for buggy
556 programs that fail to return a value. Its effect is to
557 keep the return value from being live across the entire
558 function. If we allow it to be skipped, we introduce the
559 possibility for register lifetime confusion. */
560 if (GET_CODE (PATTERN (insn)) == CLOBBER
561 && REG_P (XEXP (PATTERN (insn), 0))
562 && REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))
563 return true;
565 return false;
568 /* Return true if the block has no effect and only forwards control flow to
569 its single destination. */
571 bool
572 contains_no_active_insn_p (const_basic_block bb)
574 rtx insn;
576 if (bb == EXIT_BLOCK_PTR || bb == ENTRY_BLOCK_PTR
577 || !single_succ_p (bb))
578 return false;
580 for (insn = BB_HEAD (bb); insn != BB_END (bb); insn = NEXT_INSN (insn))
581 if (INSN_P (insn) && flow_active_insn_p (insn))
582 return false;
584 return (!INSN_P (insn)
585 || (JUMP_P (insn) && simplejump_p (insn))
586 || !flow_active_insn_p (insn));
589 /* Likewise, but protect loop latches, headers and preheaders. */
590 /* FIXME: Make this a cfg hook. */
592 bool
593 forwarder_block_p (const_basic_block bb)
595 if (!contains_no_active_insn_p (bb))
596 return false;
598 /* Protect loop latches, headers and preheaders. */
599 if (current_loops)
601 basic_block dest;
602 if (bb->loop_father->header == bb)
603 return false;
604 dest = EDGE_SUCC (bb, 0)->dest;
605 if (dest->loop_father->header == dest)
606 return false;
609 return true;
612 /* Return nonzero if we can reach target from src by falling through. */
613 /* FIXME: Make this a cfg hook. */
615 bool
616 can_fallthru (basic_block src, basic_block target)
618 rtx insn = BB_END (src);
619 rtx insn2;
620 edge e;
621 edge_iterator ei;
623 if (target == EXIT_BLOCK_PTR)
624 return true;
625 if (src->next_bb != target)
626 return 0;
627 FOR_EACH_EDGE (e, ei, src->succs)
628 if (e->dest == EXIT_BLOCK_PTR
629 && e->flags & EDGE_FALLTHRU)
630 return 0;
632 insn2 = BB_HEAD (target);
633 if (insn2 && !active_insn_p (insn2))
634 insn2 = next_active_insn (insn2);
636 /* ??? Later we may add code to move jump tables offline. */
637 return next_active_insn (insn) == insn2;
640 /* Return nonzero if we could reach target from src by falling through,
641 if the target was made adjacent. If we already have a fall-through
642 edge to the exit block, we can't do that. */
643 static bool
644 could_fall_through (basic_block src, basic_block target)
646 edge e;
647 edge_iterator ei;
649 if (target == EXIT_BLOCK_PTR)
650 return true;
651 FOR_EACH_EDGE (e, ei, src->succs)
652 if (e->dest == EXIT_BLOCK_PTR
653 && e->flags & EDGE_FALLTHRU)
654 return 0;
655 return true;
658 /* Return the NOTE_INSN_BASIC_BLOCK of BB. */
660 bb_note (basic_block bb)
662 rtx note;
664 note = BB_HEAD (bb);
665 if (LABEL_P (note))
666 note = NEXT_INSN (note);
668 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
669 return note;
672 /* Return the INSN immediately following the NOTE_INSN_BASIC_BLOCK
673 note associated with the BLOCK. */
675 static rtx
676 first_insn_after_basic_block_note (basic_block block)
678 rtx insn;
680 /* Get the first instruction in the block. */
681 insn = BB_HEAD (block);
683 if (insn == NULL_RTX)
684 return NULL_RTX;
685 if (LABEL_P (insn))
686 insn = NEXT_INSN (insn);
687 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
689 return NEXT_INSN (insn);
692 /* Creates a new basic block just after basic block B by splitting
693 everything after specified instruction I. */
695 static basic_block
696 rtl_split_block (basic_block bb, void *insnp)
698 basic_block new_bb;
699 rtx insn = (rtx) insnp;
700 edge e;
701 edge_iterator ei;
703 if (!insn)
705 insn = first_insn_after_basic_block_note (bb);
707 if (insn)
709 rtx next = insn;
711 insn = PREV_INSN (insn);
713 /* If the block contains only debug insns, insn would have
714 been NULL in a non-debug compilation, and then we'd end
715 up emitting a DELETED note. For -fcompare-debug
716 stability, emit the note too. */
717 if (insn != BB_END (bb)
718 && DEBUG_INSN_P (next)
719 && DEBUG_INSN_P (BB_END (bb)))
721 while (next != BB_END (bb) && DEBUG_INSN_P (next))
722 next = NEXT_INSN (next);
724 if (next == BB_END (bb))
725 emit_note_after (NOTE_INSN_DELETED, next);
728 else
729 insn = get_last_insn ();
732 /* We probably should check type of the insn so that we do not create
733 inconsistent cfg. It is checked in verify_flow_info anyway, so do not
734 bother. */
735 if (insn == BB_END (bb))
736 emit_note_after (NOTE_INSN_DELETED, insn);
738 /* Create the new basic block. */
739 new_bb = create_basic_block (NEXT_INSN (insn), BB_END (bb), bb);
740 BB_COPY_PARTITION (new_bb, bb);
741 BB_END (bb) = insn;
743 /* Redirect the outgoing edges. */
744 new_bb->succs = bb->succs;
745 bb->succs = NULL;
746 FOR_EACH_EDGE (e, ei, new_bb->succs)
747 e->src = new_bb;
749 /* The new block starts off being dirty. */
750 df_set_bb_dirty (bb);
751 return new_bb;
754 /* Return true if the single edge between blocks A and B is the only place
755 in RTL which holds some unique locus. */
757 static bool
758 unique_locus_on_edge_between_p (basic_block a, basic_block b)
760 const location_t goto_locus = EDGE_SUCC (a, 0)->goto_locus;
761 rtx insn, end;
763 if (LOCATION_LOCUS (goto_locus) == UNKNOWN_LOCATION)
764 return false;
766 /* First scan block A backward. */
767 insn = BB_END (a);
768 end = PREV_INSN (BB_HEAD (a));
769 while (insn != end && (!NONDEBUG_INSN_P (insn) || !INSN_HAS_LOCATION (insn)))
770 insn = PREV_INSN (insn);
772 if (insn != end && INSN_LOCATION (insn) == goto_locus)
773 return false;
775 /* Then scan block B forward. */
776 insn = BB_HEAD (b);
777 if (insn)
779 end = NEXT_INSN (BB_END (b));
780 while (insn != end && !NONDEBUG_INSN_P (insn))
781 insn = NEXT_INSN (insn);
783 if (insn != end && INSN_HAS_LOCATION (insn)
784 && INSN_LOCATION (insn) == goto_locus)
785 return false;
788 return true;
791 /* If the single edge between blocks A and B is the only place in RTL which
792 holds some unique locus, emit a nop with that locus between the blocks. */
794 static void
795 emit_nop_for_unique_locus_between (basic_block a, basic_block b)
797 if (!unique_locus_on_edge_between_p (a, b))
798 return;
800 BB_END (a) = emit_insn_after_noloc (gen_nop (), BB_END (a), a);
801 INSN_LOCATION (BB_END (a)) = EDGE_SUCC (a, 0)->goto_locus;
804 /* Blocks A and B are to be merged into a single block A. The insns
805 are already contiguous. */
807 static void
808 rtl_merge_blocks (basic_block a, basic_block b)
810 rtx b_head = BB_HEAD (b), b_end = BB_END (b), a_end = BB_END (a);
811 rtx del_first = NULL_RTX, del_last = NULL_RTX;
812 rtx b_debug_start = b_end, b_debug_end = b_end;
813 bool forwarder_p = (b->flags & BB_FORWARDER_BLOCK) != 0;
814 int b_empty = 0;
816 if (dump_file)
817 fprintf (dump_file, "Merging block %d into block %d...\n", b->index,
818 a->index);
820 while (DEBUG_INSN_P (b_end))
821 b_end = PREV_INSN (b_debug_start = b_end);
823 /* If there was a CODE_LABEL beginning B, delete it. */
824 if (LABEL_P (b_head))
826 /* Detect basic blocks with nothing but a label. This can happen
827 in particular at the end of a function. */
828 if (b_head == b_end)
829 b_empty = 1;
831 del_first = del_last = b_head;
832 b_head = NEXT_INSN (b_head);
835 /* Delete the basic block note and handle blocks containing just that
836 note. */
837 if (NOTE_INSN_BASIC_BLOCK_P (b_head))
839 if (b_head == b_end)
840 b_empty = 1;
841 if (! del_last)
842 del_first = b_head;
844 del_last = b_head;
845 b_head = NEXT_INSN (b_head);
848 /* If there was a jump out of A, delete it. */
849 if (JUMP_P (a_end))
851 rtx prev;
853 for (prev = PREV_INSN (a_end); ; prev = PREV_INSN (prev))
854 if (!NOTE_P (prev)
855 || NOTE_INSN_BASIC_BLOCK_P (prev)
856 || prev == BB_HEAD (a))
857 break;
859 del_first = a_end;
861 #ifdef HAVE_cc0
862 /* If this was a conditional jump, we need to also delete
863 the insn that set cc0. */
864 if (only_sets_cc0_p (prev))
866 rtx tmp = prev;
868 prev = prev_nonnote_insn (prev);
869 if (!prev)
870 prev = BB_HEAD (a);
871 del_first = tmp;
873 #endif
875 a_end = PREV_INSN (del_first);
877 else if (BARRIER_P (NEXT_INSN (a_end)))
878 del_first = NEXT_INSN (a_end);
880 /* Delete everything marked above as well as crap that might be
881 hanging out between the two blocks. */
882 BB_END (a) = a_end;
883 BB_HEAD (b) = b_empty ? NULL_RTX : b_head;
884 delete_insn_chain (del_first, del_last, true);
886 /* When not optimizing CFG and the edge is the only place in RTL which holds
887 some unique locus, emit a nop with that locus in between. */
888 if (!optimize)
890 emit_nop_for_unique_locus_between (a, b);
891 a_end = BB_END (a);
894 /* Reassociate the insns of B with A. */
895 if (!b_empty)
897 update_bb_for_insn_chain (a_end, b_debug_end, a);
899 BB_END (a) = b_debug_end;
900 BB_HEAD (b) = NULL_RTX;
902 else if (b_end != b_debug_end)
904 /* Move any deleted labels and other notes between the end of A
905 and the debug insns that make up B after the debug insns,
906 bringing the debug insns into A while keeping the notes after
907 the end of A. */
908 if (NEXT_INSN (a_end) != b_debug_start)
909 reorder_insns_nobb (NEXT_INSN (a_end), PREV_INSN (b_debug_start),
910 b_debug_end);
911 update_bb_for_insn_chain (b_debug_start, b_debug_end, a);
912 BB_END (a) = b_debug_end;
915 df_bb_delete (b->index);
917 /* If B was a forwarder block, propagate the locus on the edge. */
918 if (forwarder_p
919 && LOCATION_LOCUS (EDGE_SUCC (b, 0)->goto_locus) == UNKNOWN_LOCATION)
920 EDGE_SUCC (b, 0)->goto_locus = EDGE_SUCC (a, 0)->goto_locus;
922 if (dump_file)
923 fprintf (dump_file, "Merged blocks %d and %d.\n", a->index, b->index);
927 /* Return true when block A and B can be merged. */
929 static bool
930 rtl_can_merge_blocks (basic_block a, basic_block b)
932 /* If we are partitioning hot/cold basic blocks, we don't want to
933 mess up unconditional or indirect jumps that cross between hot
934 and cold sections.
936 Basic block partitioning may result in some jumps that appear to
937 be optimizable (or blocks that appear to be mergeable), but which really
938 must be left untouched (they are required to make it safely across
939 partition boundaries). See the comments at the top of
940 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
942 if (BB_PARTITION (a) != BB_PARTITION (b))
943 return false;
945 /* Protect the loop latches. */
946 if (current_loops && b->loop_father->latch == b)
947 return false;
949 /* There must be exactly one edge in between the blocks. */
950 return (single_succ_p (a)
951 && single_succ (a) == b
952 && single_pred_p (b)
953 && a != b
954 /* Must be simple edge. */
955 && !(single_succ_edge (a)->flags & EDGE_COMPLEX)
956 && a->next_bb == b
957 && a != ENTRY_BLOCK_PTR && b != EXIT_BLOCK_PTR
958 /* If the jump insn has side effects,
959 we can't kill the edge. */
960 && (!JUMP_P (BB_END (a))
961 || (reload_completed
962 ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
965 /* Return the label in the head of basic block BLOCK. Create one if it doesn't
966 exist. */
969 block_label (basic_block block)
971 if (block == EXIT_BLOCK_PTR)
972 return NULL_RTX;
974 if (!LABEL_P (BB_HEAD (block)))
976 BB_HEAD (block) = emit_label_before (gen_label_rtx (), BB_HEAD (block));
979 return BB_HEAD (block);
982 /* Attempt to perform edge redirection by replacing possibly complex jump
983 instruction by unconditional jump or removing jump completely. This can
984 apply only if all edges now point to the same block. The parameters and
985 return values are equivalent to redirect_edge_and_branch. */
987 edge
988 try_redirect_by_replacing_jump (edge e, basic_block target, bool in_cfglayout)
990 basic_block src = e->src;
991 rtx insn = BB_END (src), kill_from;
992 rtx set;
993 int fallthru = 0;
995 /* If we are partitioning hot/cold basic blocks, we don't want to
996 mess up unconditional or indirect jumps that cross between hot
997 and cold sections.
999 Basic block partitioning may result in some jumps that appear to
1000 be optimizable (or blocks that appear to be mergeable), but which really
1001 must be left untouched (they are required to make it safely across
1002 partition boundaries). See the comments at the top of
1003 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
1005 if (BB_PARTITION (src) != BB_PARTITION (target))
1006 return NULL;
1008 /* We can replace or remove a complex jump only when we have exactly
1009 two edges. Also, if we have exactly one outgoing edge, we can
1010 redirect that. */
1011 if (EDGE_COUNT (src->succs) >= 3
1012 /* Verify that all targets will be TARGET. Specifically, the
1013 edge that is not E must also go to TARGET. */
1014 || (EDGE_COUNT (src->succs) == 2
1015 && EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target))
1016 return NULL;
1018 if (!onlyjump_p (insn))
1019 return NULL;
1020 if ((!optimize || reload_completed) && tablejump_p (insn, NULL, NULL))
1021 return NULL;
1023 /* Avoid removing branch with side effects. */
1024 set = single_set (insn);
1025 if (!set || side_effects_p (set))
1026 return NULL;
1028 /* In case we zap a conditional jump, we'll need to kill
1029 the cc0 setter too. */
1030 kill_from = insn;
1031 #ifdef HAVE_cc0
1032 if (reg_mentioned_p (cc0_rtx, PATTERN (insn))
1033 && only_sets_cc0_p (PREV_INSN (insn)))
1034 kill_from = PREV_INSN (insn);
1035 #endif
1037 /* See if we can create the fallthru edge. */
1038 if (in_cfglayout || can_fallthru (src, target))
1040 if (dump_file)
1041 fprintf (dump_file, "Removing jump %i.\n", INSN_UID (insn));
1042 fallthru = 1;
1044 /* Selectively unlink whole insn chain. */
1045 if (in_cfglayout)
1047 rtx insn = BB_FOOTER (src);
1049 delete_insn_chain (kill_from, BB_END (src), false);
1051 /* Remove barriers but keep jumptables. */
1052 while (insn)
1054 if (BARRIER_P (insn))
1056 if (PREV_INSN (insn))
1057 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
1058 else
1059 BB_FOOTER (src) = NEXT_INSN (insn);
1060 if (NEXT_INSN (insn))
1061 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
1063 if (LABEL_P (insn))
1064 break;
1065 insn = NEXT_INSN (insn);
1068 else
1069 delete_insn_chain (kill_from, PREV_INSN (BB_HEAD (target)),
1070 false);
1073 /* If this already is simplejump, redirect it. */
1074 else if (simplejump_p (insn))
1076 if (e->dest == target)
1077 return NULL;
1078 if (dump_file)
1079 fprintf (dump_file, "Redirecting jump %i from %i to %i.\n",
1080 INSN_UID (insn), e->dest->index, target->index);
1081 if (!redirect_jump (insn, block_label (target), 0))
1083 gcc_assert (target == EXIT_BLOCK_PTR);
1084 return NULL;
1088 /* Cannot do anything for target exit block. */
1089 else if (target == EXIT_BLOCK_PTR)
1090 return NULL;
1092 /* Or replace possibly complicated jump insn by simple jump insn. */
1093 else
1095 rtx target_label = block_label (target);
1096 rtx barrier, label, table;
1098 emit_jump_insn_after_noloc (gen_jump (target_label), insn);
1099 JUMP_LABEL (BB_END (src)) = target_label;
1100 LABEL_NUSES (target_label)++;
1101 if (dump_file)
1102 fprintf (dump_file, "Replacing insn %i by jump %i\n",
1103 INSN_UID (insn), INSN_UID (BB_END (src)));
1106 delete_insn_chain (kill_from, insn, false);
1108 /* Recognize a tablejump that we are converting to a
1109 simple jump and remove its associated CODE_LABEL
1110 and ADDR_VEC or ADDR_DIFF_VEC. */
1111 if (tablejump_p (insn, &label, &table))
1112 delete_insn_chain (label, table, false);
1114 barrier = next_nonnote_insn (BB_END (src));
1115 if (!barrier || !BARRIER_P (barrier))
1116 emit_barrier_after (BB_END (src));
1117 else
1119 if (barrier != NEXT_INSN (BB_END (src)))
1121 /* Move the jump before barrier so that the notes
1122 which originally were or were created before jump table are
1123 inside the basic block. */
1124 rtx new_insn = BB_END (src);
1126 update_bb_for_insn_chain (NEXT_INSN (BB_END (src)),
1127 PREV_INSN (barrier), src);
1129 NEXT_INSN (PREV_INSN (new_insn)) = NEXT_INSN (new_insn);
1130 PREV_INSN (NEXT_INSN (new_insn)) = PREV_INSN (new_insn);
1132 NEXT_INSN (new_insn) = barrier;
1133 NEXT_INSN (PREV_INSN (barrier)) = new_insn;
1135 PREV_INSN (new_insn) = PREV_INSN (barrier);
1136 PREV_INSN (barrier) = new_insn;
1141 /* Keep only one edge out and set proper flags. */
1142 if (!single_succ_p (src))
1143 remove_edge (e);
1144 gcc_assert (single_succ_p (src));
1146 e = single_succ_edge (src);
1147 if (fallthru)
1148 e->flags = EDGE_FALLTHRU;
1149 else
1150 e->flags = 0;
1152 e->probability = REG_BR_PROB_BASE;
1153 e->count = src->count;
1155 if (e->dest != target)
1156 redirect_edge_succ (e, target);
1157 return e;
1160 /* Subroutine of redirect_branch_edge that tries to patch the jump
1161 instruction INSN so that it reaches block NEW. Do this
1162 only when it originally reached block OLD. Return true if this
1163 worked or the original target wasn't OLD, return false if redirection
1164 doesn't work. */
1166 static bool
1167 patch_jump_insn (rtx insn, rtx old_label, basic_block new_bb)
1169 rtx tmp;
1170 /* Recognize a tablejump and adjust all matching cases. */
1171 if (tablejump_p (insn, NULL, &tmp))
1173 rtvec vec;
1174 int j;
1175 rtx new_label = block_label (new_bb);
1177 if (new_bb == EXIT_BLOCK_PTR)
1178 return false;
1179 if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
1180 vec = XVEC (PATTERN (tmp), 0);
1181 else
1182 vec = XVEC (PATTERN (tmp), 1);
1184 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
1185 if (XEXP (RTVEC_ELT (vec, j), 0) == old_label)
1187 RTVEC_ELT (vec, j) = gen_rtx_LABEL_REF (Pmode, new_label);
1188 --LABEL_NUSES (old_label);
1189 ++LABEL_NUSES (new_label);
1192 /* Handle casesi dispatch insns. */
1193 if ((tmp = single_set (insn)) != NULL
1194 && SET_DEST (tmp) == pc_rtx
1195 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
1196 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF
1197 && XEXP (XEXP (SET_SRC (tmp), 2), 0) == old_label)
1199 XEXP (SET_SRC (tmp), 2) = gen_rtx_LABEL_REF (Pmode,
1200 new_label);
1201 --LABEL_NUSES (old_label);
1202 ++LABEL_NUSES (new_label);
1205 else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL)
1207 int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp);
1208 rtx new_label, note;
1210 if (new_bb == EXIT_BLOCK_PTR)
1211 return false;
1212 new_label = block_label (new_bb);
1214 for (i = 0; i < n; ++i)
1216 rtx old_ref = ASM_OPERANDS_LABEL (tmp, i);
1217 gcc_assert (GET_CODE (old_ref) == LABEL_REF);
1218 if (XEXP (old_ref, 0) == old_label)
1220 ASM_OPERANDS_LABEL (tmp, i)
1221 = gen_rtx_LABEL_REF (Pmode, new_label);
1222 --LABEL_NUSES (old_label);
1223 ++LABEL_NUSES (new_label);
1227 if (JUMP_LABEL (insn) == old_label)
1229 JUMP_LABEL (insn) = new_label;
1230 note = find_reg_note (insn, REG_LABEL_TARGET, new_label);
1231 if (note)
1232 remove_note (insn, note);
1234 else
1236 note = find_reg_note (insn, REG_LABEL_TARGET, old_label);
1237 if (note)
1238 remove_note (insn, note);
1239 if (JUMP_LABEL (insn) != new_label
1240 && !find_reg_note (insn, REG_LABEL_TARGET, new_label))
1241 add_reg_note (insn, REG_LABEL_TARGET, new_label);
1243 while ((note = find_reg_note (insn, REG_LABEL_OPERAND, old_label))
1244 != NULL_RTX)
1245 XEXP (note, 0) = new_label;
1247 else
1249 /* ?? We may play the games with moving the named labels from
1250 one basic block to the other in case only one computed_jump is
1251 available. */
1252 if (computed_jump_p (insn)
1253 /* A return instruction can't be redirected. */
1254 || returnjump_p (insn))
1255 return false;
1257 if (!currently_expanding_to_rtl || JUMP_LABEL (insn) == old_label)
1259 /* If the insn doesn't go where we think, we're confused. */
1260 gcc_assert (JUMP_LABEL (insn) == old_label);
1262 /* If the substitution doesn't succeed, die. This can happen
1263 if the back end emitted unrecognizable instructions or if
1264 target is exit block on some arches. */
1265 if (!redirect_jump (insn, block_label (new_bb), 0))
1267 gcc_assert (new_bb == EXIT_BLOCK_PTR);
1268 return false;
1272 return true;
1276 /* Redirect edge representing branch of (un)conditional jump or tablejump,
1277 NULL on failure */
1278 static edge
1279 redirect_branch_edge (edge e, basic_block target)
1281 rtx old_label = BB_HEAD (e->dest);
1282 basic_block src = e->src;
1283 rtx insn = BB_END (src);
1285 /* We can only redirect non-fallthru edges of jump insn. */
1286 if (e->flags & EDGE_FALLTHRU)
1287 return NULL;
1288 else if (!JUMP_P (insn) && !currently_expanding_to_rtl)
1289 return NULL;
1291 if (!currently_expanding_to_rtl)
1293 if (!patch_jump_insn (insn, old_label, target))
1294 return NULL;
1296 else
1297 /* When expanding this BB might actually contain multiple
1298 jumps (i.e. not yet split by find_many_sub_basic_blocks).
1299 Redirect all of those that match our label. */
1300 FOR_BB_INSNS (src, insn)
1301 if (JUMP_P (insn) && !patch_jump_insn (insn, old_label, target))
1302 return NULL;
1304 if (dump_file)
1305 fprintf (dump_file, "Edge %i->%i redirected to %i\n",
1306 e->src->index, e->dest->index, target->index);
1308 if (e->dest != target)
1309 e = redirect_edge_succ_nodup (e, target);
1311 return e;
1314 /* Called when edge E has been redirected to a new destination,
1315 in order to update the region crossing flag on the edge and
1316 jump. */
1318 static void
1319 fixup_partition_crossing (edge e)
1321 rtx note;
1323 if (e->src == ENTRY_BLOCK_PTR || e->dest == EXIT_BLOCK_PTR)
1324 return;
1325 /* If we redirected an existing edge, it may already be marked
1326 crossing, even though the new src is missing a reg crossing note.
1327 But make sure reg crossing note doesn't already exist before
1328 inserting. */
1329 if (BB_PARTITION (e->src) != BB_PARTITION (e->dest))
1331 e->flags |= EDGE_CROSSING;
1332 note = find_reg_note (BB_END (e->src), REG_CROSSING_JUMP, NULL_RTX);
1333 if (JUMP_P (BB_END (e->src))
1334 && !note)
1335 add_reg_note (BB_END (e->src), REG_CROSSING_JUMP, NULL_RTX);
1337 else if (BB_PARTITION (e->src) == BB_PARTITION (e->dest))
1339 e->flags &= ~EDGE_CROSSING;
1340 /* Remove the section crossing note from jump at end of
1341 src if it exists, and if no other successors are
1342 still crossing. */
1343 note = find_reg_note (BB_END (e->src), REG_CROSSING_JUMP, NULL_RTX);
1344 if (note)
1346 bool has_crossing_succ = false;
1347 edge e2;
1348 edge_iterator ei;
1349 FOR_EACH_EDGE (e2, ei, e->src->succs)
1351 has_crossing_succ |= (e2->flags & EDGE_CROSSING);
1352 if (has_crossing_succ)
1353 break;
1355 if (!has_crossing_succ)
1356 remove_note (BB_END (e->src), note);
1361 /* Attempt to change code to redirect edge E to TARGET. Don't do that on
1362 expense of adding new instructions or reordering basic blocks.
1364 Function can be also called with edge destination equivalent to the TARGET.
1365 Then it should try the simplifications and do nothing if none is possible.
1367 Return edge representing the branch if transformation succeeded. Return NULL
1368 on failure.
1369 We still return NULL in case E already destinated TARGET and we didn't
1370 managed to simplify instruction stream. */
1372 static edge
1373 rtl_redirect_edge_and_branch (edge e, basic_block target)
1375 edge ret;
1376 basic_block src = e->src;
1377 basic_block dest = e->dest;
1379 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
1380 return NULL;
1382 if (dest == target)
1383 return e;
1385 if ((ret = try_redirect_by_replacing_jump (e, target, false)) != NULL)
1387 df_set_bb_dirty (src);
1388 fixup_partition_crossing (ret);
1389 return ret;
1392 ret = redirect_branch_edge (e, target);
1393 if (!ret)
1394 return NULL;
1396 df_set_bb_dirty (src);
1397 fixup_partition_crossing (ret);
1398 return ret;
1401 /* Emit a barrier after BB, into the footer if we are in CFGLAYOUT mode. */
1403 void
1404 emit_barrier_after_bb (basic_block bb)
1406 rtx barrier = emit_barrier_after (BB_END (bb));
1407 gcc_assert (current_ir_type() == IR_RTL_CFGRTL
1408 || current_ir_type () == IR_RTL_CFGLAYOUT);
1409 if (current_ir_type () == IR_RTL_CFGLAYOUT)
1410 BB_FOOTER (bb) = unlink_insn_chain (barrier, barrier);
1413 /* Like force_nonfallthru below, but additionally performs redirection
1414 Used by redirect_edge_and_branch_force. JUMP_LABEL is used only
1415 when redirecting to the EXIT_BLOCK, it is either ret_rtx or
1416 simple_return_rtx, indicating which kind of returnjump to create.
1417 It should be NULL otherwise. */
1419 basic_block
1420 force_nonfallthru_and_redirect (edge e, basic_block target, rtx jump_label)
1422 basic_block jump_block, new_bb = NULL, src = e->src;
1423 rtx note;
1424 edge new_edge;
1425 int abnormal_edge_flags = 0;
1426 bool asm_goto_edge = false;
1427 int loc;
1429 /* In the case the last instruction is conditional jump to the next
1430 instruction, first redirect the jump itself and then continue
1431 by creating a basic block afterwards to redirect fallthru edge. */
1432 if (e->src != ENTRY_BLOCK_PTR && e->dest != EXIT_BLOCK_PTR
1433 && any_condjump_p (BB_END (e->src))
1434 && JUMP_LABEL (BB_END (e->src)) == BB_HEAD (e->dest))
1436 rtx note;
1437 edge b = unchecked_make_edge (e->src, target, 0);
1438 bool redirected;
1440 redirected = redirect_jump (BB_END (e->src), block_label (target), 0);
1441 gcc_assert (redirected);
1443 note = find_reg_note (BB_END (e->src), REG_BR_PROB, NULL_RTX);
1444 if (note)
1446 int prob = INTVAL (XEXP (note, 0));
1448 b->probability = prob;
1449 /* Update this to use GCOV_COMPUTE_SCALE. */
1450 b->count = e->count * prob / REG_BR_PROB_BASE;
1451 e->probability -= e->probability;
1452 e->count -= b->count;
1453 if (e->probability < 0)
1454 e->probability = 0;
1455 if (e->count < 0)
1456 e->count = 0;
1460 if (e->flags & EDGE_ABNORMAL)
1462 /* Irritating special case - fallthru edge to the same block as abnormal
1463 edge.
1464 We can't redirect abnormal edge, but we still can split the fallthru
1465 one and create separate abnormal edge to original destination.
1466 This allows bb-reorder to make such edge non-fallthru. */
1467 gcc_assert (e->dest == target);
1468 abnormal_edge_flags = e->flags & ~EDGE_FALLTHRU;
1469 e->flags &= EDGE_FALLTHRU;
1471 else
1473 gcc_assert (e->flags & EDGE_FALLTHRU);
1474 if (e->src == ENTRY_BLOCK_PTR)
1476 /* We can't redirect the entry block. Create an empty block
1477 at the start of the function which we use to add the new
1478 jump. */
1479 edge tmp;
1480 edge_iterator ei;
1481 bool found = false;
1483 basic_block bb = create_basic_block (BB_HEAD (e->dest), NULL, ENTRY_BLOCK_PTR);
1485 /* Change the existing edge's source to be the new block, and add
1486 a new edge from the entry block to the new block. */
1487 e->src = bb;
1488 for (ei = ei_start (ENTRY_BLOCK_PTR->succs); (tmp = ei_safe_edge (ei)); )
1490 if (tmp == e)
1492 ENTRY_BLOCK_PTR->succs->unordered_remove (ei.index);
1493 found = true;
1494 break;
1496 else
1497 ei_next (&ei);
1500 gcc_assert (found);
1502 vec_safe_push (bb->succs, e);
1503 make_single_succ_edge (ENTRY_BLOCK_PTR, bb, EDGE_FALLTHRU);
1507 /* If e->src ends with asm goto, see if any of the ASM_OPERANDS_LABELs
1508 don't point to the target or fallthru label. */
1509 if (JUMP_P (BB_END (e->src))
1510 && target != EXIT_BLOCK_PTR
1511 && (e->flags & EDGE_FALLTHRU)
1512 && (note = extract_asm_operands (PATTERN (BB_END (e->src)))))
1514 int i, n = ASM_OPERANDS_LABEL_LENGTH (note);
1515 bool adjust_jump_target = false;
1517 for (i = 0; i < n; ++i)
1519 if (XEXP (ASM_OPERANDS_LABEL (note, i), 0) == BB_HEAD (e->dest))
1521 LABEL_NUSES (XEXP (ASM_OPERANDS_LABEL (note, i), 0))--;
1522 XEXP (ASM_OPERANDS_LABEL (note, i), 0) = block_label (target);
1523 LABEL_NUSES (XEXP (ASM_OPERANDS_LABEL (note, i), 0))++;
1524 adjust_jump_target = true;
1526 if (XEXP (ASM_OPERANDS_LABEL (note, i), 0) == BB_HEAD (target))
1527 asm_goto_edge = true;
1529 if (adjust_jump_target)
1531 rtx insn = BB_END (e->src), note;
1532 rtx old_label = BB_HEAD (e->dest);
1533 rtx new_label = BB_HEAD (target);
1535 if (JUMP_LABEL (insn) == old_label)
1537 JUMP_LABEL (insn) = new_label;
1538 note = find_reg_note (insn, REG_LABEL_TARGET, new_label);
1539 if (note)
1540 remove_note (insn, note);
1542 else
1544 note = find_reg_note (insn, REG_LABEL_TARGET, old_label);
1545 if (note)
1546 remove_note (insn, note);
1547 if (JUMP_LABEL (insn) != new_label
1548 && !find_reg_note (insn, REG_LABEL_TARGET, new_label))
1549 add_reg_note (insn, REG_LABEL_TARGET, new_label);
1551 while ((note = find_reg_note (insn, REG_LABEL_OPERAND, old_label))
1552 != NULL_RTX)
1553 XEXP (note, 0) = new_label;
1557 if (EDGE_COUNT (e->src->succs) >= 2 || abnormal_edge_flags || asm_goto_edge)
1559 gcov_type count = e->count;
1560 int probability = e->probability;
1561 /* Create the new structures. */
1563 /* If the old block ended with a tablejump, skip its table
1564 by searching forward from there. Otherwise start searching
1565 forward from the last instruction of the old block. */
1566 if (!tablejump_p (BB_END (e->src), NULL, &note))
1567 note = BB_END (e->src);
1568 note = NEXT_INSN (note);
1570 jump_block = create_basic_block (note, NULL, e->src);
1571 jump_block->count = count;
1572 jump_block->frequency = EDGE_FREQUENCY (e);
1574 /* Make sure new block ends up in correct hot/cold section. */
1576 BB_COPY_PARTITION (jump_block, e->src);
1578 /* Wire edge in. */
1579 new_edge = make_edge (e->src, jump_block, EDGE_FALLTHRU);
1580 new_edge->probability = probability;
1581 new_edge->count = count;
1583 /* Redirect old edge. */
1584 redirect_edge_pred (e, jump_block);
1585 e->probability = REG_BR_PROB_BASE;
1587 /* If e->src was previously region crossing, it no longer is
1588 and the reg crossing note should be removed. */
1589 fixup_partition_crossing (new_edge);
1591 /* If asm goto has any label refs to target's label,
1592 add also edge from asm goto bb to target. */
1593 if (asm_goto_edge)
1595 new_edge->probability /= 2;
1596 new_edge->count /= 2;
1597 jump_block->count /= 2;
1598 jump_block->frequency /= 2;
1599 new_edge = make_edge (new_edge->src, target,
1600 e->flags & ~EDGE_FALLTHRU);
1601 new_edge->probability = probability - probability / 2;
1602 new_edge->count = count - count / 2;
1605 new_bb = jump_block;
1607 else
1608 jump_block = e->src;
1610 loc = e->goto_locus;
1611 e->flags &= ~EDGE_FALLTHRU;
1612 if (target == EXIT_BLOCK_PTR)
1614 if (jump_label == ret_rtx)
1616 #ifdef HAVE_return
1617 emit_jump_insn_after_setloc (gen_return (), BB_END (jump_block), loc);
1618 #else
1619 gcc_unreachable ();
1620 #endif
1622 else
1624 gcc_assert (jump_label == simple_return_rtx);
1625 #ifdef HAVE_simple_return
1626 emit_jump_insn_after_setloc (gen_simple_return (),
1627 BB_END (jump_block), loc);
1628 #else
1629 gcc_unreachable ();
1630 #endif
1632 set_return_jump_label (BB_END (jump_block));
1634 else
1636 rtx label = block_label (target);
1637 emit_jump_insn_after_setloc (gen_jump (label), BB_END (jump_block), loc);
1638 JUMP_LABEL (BB_END (jump_block)) = label;
1639 LABEL_NUSES (label)++;
1642 /* We might be in cfg layout mode, and if so, the following routine will
1643 insert the barrier correctly. */
1644 emit_barrier_after_bb (jump_block);
1645 redirect_edge_succ_nodup (e, target);
1647 if (abnormal_edge_flags)
1648 make_edge (src, target, abnormal_edge_flags);
1650 df_mark_solutions_dirty ();
1651 fixup_partition_crossing (e);
1652 return new_bb;
1655 /* Edge E is assumed to be fallthru edge. Emit needed jump instruction
1656 (and possibly create new basic block) to make edge non-fallthru.
1657 Return newly created BB or NULL if none. */
1659 static basic_block
1660 rtl_force_nonfallthru (edge e)
1662 return force_nonfallthru_and_redirect (e, e->dest, NULL_RTX);
1665 /* Redirect edge even at the expense of creating new jump insn or
1666 basic block. Return new basic block if created, NULL otherwise.
1667 Conversion must be possible. */
1669 static basic_block
1670 rtl_redirect_edge_and_branch_force (edge e, basic_block target)
1672 if (redirect_edge_and_branch (e, target)
1673 || e->dest == target)
1674 return NULL;
1676 /* In case the edge redirection failed, try to force it to be non-fallthru
1677 and redirect newly created simplejump. */
1678 df_set_bb_dirty (e->src);
1679 return force_nonfallthru_and_redirect (e, target, NULL_RTX);
1682 /* The given edge should potentially be a fallthru edge. If that is in
1683 fact true, delete the jump and barriers that are in the way. */
1685 static void
1686 rtl_tidy_fallthru_edge (edge e)
1688 rtx q;
1689 basic_block b = e->src, c = b->next_bb;
1691 /* ??? In a late-running flow pass, other folks may have deleted basic
1692 blocks by nopping out blocks, leaving multiple BARRIERs between here
1693 and the target label. They ought to be chastised and fixed.
1695 We can also wind up with a sequence of undeletable labels between
1696 one block and the next.
1698 So search through a sequence of barriers, labels, and notes for
1699 the head of block C and assert that we really do fall through. */
1701 for (q = NEXT_INSN (BB_END (b)); q != BB_HEAD (c); q = NEXT_INSN (q))
1702 if (INSN_P (q))
1703 return;
1705 /* Remove what will soon cease being the jump insn from the source block.
1706 If block B consisted only of this single jump, turn it into a deleted
1707 note. */
1708 q = BB_END (b);
1709 if (JUMP_P (q)
1710 && onlyjump_p (q)
1711 && (any_uncondjump_p (q)
1712 || single_succ_p (b)))
1714 #ifdef HAVE_cc0
1715 /* If this was a conditional jump, we need to also delete
1716 the insn that set cc0. */
1717 if (any_condjump_p (q) && only_sets_cc0_p (PREV_INSN (q)))
1718 q = PREV_INSN (q);
1719 #endif
1721 q = PREV_INSN (q);
1724 /* Selectively unlink the sequence. */
1725 if (q != PREV_INSN (BB_HEAD (c)))
1726 delete_insn_chain (NEXT_INSN (q), PREV_INSN (BB_HEAD (c)), false);
1728 e->flags |= EDGE_FALLTHRU;
1731 /* Should move basic block BB after basic block AFTER. NIY. */
1733 static bool
1734 rtl_move_block_after (basic_block bb ATTRIBUTE_UNUSED,
1735 basic_block after ATTRIBUTE_UNUSED)
1737 return false;
1740 /* Locate the last bb in the same partition as START_BB. */
1742 static basic_block
1743 last_bb_in_partition (basic_block start_bb)
1745 basic_block bb;
1746 FOR_BB_BETWEEN (bb, start_bb, EXIT_BLOCK_PTR, next_bb)
1748 if (BB_PARTITION (start_bb) != BB_PARTITION (bb->next_bb))
1749 return bb;
1751 /* Return bb before EXIT_BLOCK_PTR. */
1752 return bb->prev_bb;
1755 /* Split a (typically critical) edge. Return the new block.
1756 The edge must not be abnormal.
1758 ??? The code generally expects to be called on critical edges.
1759 The case of a block ending in an unconditional jump to a
1760 block with multiple predecessors is not handled optimally. */
1762 static basic_block
1763 rtl_split_edge (edge edge_in)
1765 basic_block bb, new_bb;
1766 rtx before;
1768 /* Abnormal edges cannot be split. */
1769 gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
1771 /* We are going to place the new block in front of edge destination.
1772 Avoid existence of fallthru predecessors. */
1773 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1775 edge e = find_fallthru_edge (edge_in->dest->preds);
1777 if (e)
1778 force_nonfallthru (e);
1781 /* Create the basic block note. */
1782 if (edge_in->dest != EXIT_BLOCK_PTR)
1783 before = BB_HEAD (edge_in->dest);
1784 else
1785 before = NULL_RTX;
1787 /* If this is a fall through edge to the exit block, the blocks might be
1788 not adjacent, and the right place is after the source. */
1789 if ((edge_in->flags & EDGE_FALLTHRU) && edge_in->dest == EXIT_BLOCK_PTR)
1791 before = NEXT_INSN (BB_END (edge_in->src));
1792 bb = create_basic_block (before, NULL, edge_in->src);
1793 BB_COPY_PARTITION (bb, edge_in->src);
1795 else
1797 if (edge_in->src == ENTRY_BLOCK_PTR)
1799 bb = create_basic_block (before, NULL, edge_in->dest->prev_bb);
1800 BB_COPY_PARTITION (bb, edge_in->dest);
1802 else
1804 basic_block after = edge_in->dest->prev_bb;
1805 /* If this is post-bb reordering, and the edge crosses a partition
1806 boundary, the new block needs to be inserted in the bb chain
1807 at the end of the src partition (since we put the new bb into
1808 that partition, see below). Otherwise we may end up creating
1809 an extra partition crossing in the chain, which is illegal.
1810 It can't go after the src, because src may have a fall-through
1811 to a different block. */
1812 if (crtl->bb_reorder_complete
1813 && (edge_in->flags & EDGE_CROSSING))
1815 after = last_bb_in_partition (edge_in->src);
1816 before = NEXT_INSN (BB_END (after));
1817 /* The instruction following the last bb in partition should
1818 be a barrier, since it cannot end in a fall-through. */
1819 gcc_checking_assert (BARRIER_P (before));
1820 before = NEXT_INSN (before);
1822 bb = create_basic_block (before, NULL, after);
1823 /* Put the split bb into the src partition, to avoid creating
1824 a situation where a cold bb dominates a hot bb, in the case
1825 where src is cold and dest is hot. The src will dominate
1826 the new bb (whereas it might not have dominated dest). */
1827 BB_COPY_PARTITION (bb, edge_in->src);
1831 make_single_succ_edge (bb, edge_in->dest, EDGE_FALLTHRU);
1833 /* Can't allow a region crossing edge to be fallthrough. */
1834 if (BB_PARTITION (bb) != BB_PARTITION (edge_in->dest)
1835 && edge_in->dest != EXIT_BLOCK_PTR)
1837 new_bb = force_nonfallthru (single_succ_edge (bb));
1838 gcc_assert (!new_bb);
1841 /* For non-fallthru edges, we must adjust the predecessor's
1842 jump instruction to target our new block. */
1843 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1845 edge redirected = redirect_edge_and_branch (edge_in, bb);
1846 gcc_assert (redirected);
1848 else
1850 if (edge_in->src != ENTRY_BLOCK_PTR)
1852 /* For asm goto even splitting of fallthru edge might
1853 need insn patching, as other labels might point to the
1854 old label. */
1855 rtx last = BB_END (edge_in->src);
1856 if (last
1857 && JUMP_P (last)
1858 && edge_in->dest != EXIT_BLOCK_PTR
1859 && extract_asm_operands (PATTERN (last)) != NULL_RTX
1860 && patch_jump_insn (last, before, bb))
1861 df_set_bb_dirty (edge_in->src);
1863 redirect_edge_succ (edge_in, bb);
1866 return bb;
1869 /* Queue instructions for insertion on an edge between two basic blocks.
1870 The new instructions and basic blocks (if any) will not appear in the
1871 CFG until commit_edge_insertions is called. */
1873 void
1874 insert_insn_on_edge (rtx pattern, edge e)
1876 /* We cannot insert instructions on an abnormal critical edge.
1877 It will be easier to find the culprit if we die now. */
1878 gcc_assert (!((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e)));
1880 if (e->insns.r == NULL_RTX)
1881 start_sequence ();
1882 else
1883 push_to_sequence (e->insns.r);
1885 emit_insn (pattern);
1887 e->insns.r = get_insns ();
1888 end_sequence ();
1891 /* Update the CFG for the instructions queued on edge E. */
1893 void
1894 commit_one_edge_insertion (edge e)
1896 rtx before = NULL_RTX, after = NULL_RTX, insns, tmp, last;
1897 basic_block bb;
1899 /* Pull the insns off the edge now since the edge might go away. */
1900 insns = e->insns.r;
1901 e->insns.r = NULL_RTX;
1903 /* Figure out where to put these insns. If the destination has
1904 one predecessor, insert there. Except for the exit block. */
1905 if (single_pred_p (e->dest) && e->dest != EXIT_BLOCK_PTR)
1907 bb = e->dest;
1909 /* Get the location correct wrt a code label, and "nice" wrt
1910 a basic block note, and before everything else. */
1911 tmp = BB_HEAD (bb);
1912 if (LABEL_P (tmp))
1913 tmp = NEXT_INSN (tmp);
1914 if (NOTE_INSN_BASIC_BLOCK_P (tmp))
1915 tmp = NEXT_INSN (tmp);
1916 if (tmp == BB_HEAD (bb))
1917 before = tmp;
1918 else if (tmp)
1919 after = PREV_INSN (tmp);
1920 else
1921 after = get_last_insn ();
1924 /* If the source has one successor and the edge is not abnormal,
1925 insert there. Except for the entry block. */
1926 else if ((e->flags & EDGE_ABNORMAL) == 0
1927 && single_succ_p (e->src)
1928 && e->src != ENTRY_BLOCK_PTR)
1930 bb = e->src;
1932 /* It is possible to have a non-simple jump here. Consider a target
1933 where some forms of unconditional jumps clobber a register. This
1934 happens on the fr30 for example.
1936 We know this block has a single successor, so we can just emit
1937 the queued insns before the jump. */
1938 if (JUMP_P (BB_END (bb)))
1939 before = BB_END (bb);
1940 else
1942 /* We'd better be fallthru, or we've lost track of what's what. */
1943 gcc_assert (e->flags & EDGE_FALLTHRU);
1945 after = BB_END (bb);
1949 /* Otherwise we must split the edge. */
1950 else
1952 bb = split_edge (e);
1954 /* If E crossed a partition boundary, we needed to make bb end in
1955 a region-crossing jump, even though it was originally fallthru. */
1956 if (JUMP_P (BB_END (bb)))
1957 before = BB_END (bb);
1958 else
1959 after = BB_END (bb);
1962 /* Now that we've found the spot, do the insertion. */
1963 if (before)
1965 emit_insn_before_noloc (insns, before, bb);
1966 last = prev_nonnote_insn (before);
1968 else
1969 last = emit_insn_after_noloc (insns, after, bb);
1971 if (returnjump_p (last))
1973 /* ??? Remove all outgoing edges from BB and add one for EXIT.
1974 This is not currently a problem because this only happens
1975 for the (single) epilogue, which already has a fallthru edge
1976 to EXIT. */
1978 e = single_succ_edge (bb);
1979 gcc_assert (e->dest == EXIT_BLOCK_PTR
1980 && single_succ_p (bb) && (e->flags & EDGE_FALLTHRU));
1982 e->flags &= ~EDGE_FALLTHRU;
1983 emit_barrier_after (last);
1985 if (before)
1986 delete_insn (before);
1988 else
1989 gcc_assert (!JUMP_P (last));
1992 /* Update the CFG for all queued instructions. */
1994 void
1995 commit_edge_insertions (void)
1997 basic_block bb;
1999 #ifdef ENABLE_CHECKING
2000 verify_flow_info ();
2001 #endif
2003 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
2005 edge e;
2006 edge_iterator ei;
2008 FOR_EACH_EDGE (e, ei, bb->succs)
2009 if (e->insns.r)
2010 commit_one_edge_insertion (e);
2015 /* Print out RTL-specific basic block information (live information
2016 at start and end with TDF_DETAILS). FLAGS are the TDF_* masks
2017 documented in dumpfile.h. */
2019 static void
2020 rtl_dump_bb (FILE *outf, basic_block bb, int indent, int flags)
2022 rtx insn;
2023 rtx last;
2024 char *s_indent;
2026 s_indent = (char *) alloca ((size_t) indent + 1);
2027 memset (s_indent, ' ', (size_t) indent);
2028 s_indent[indent] = '\0';
2030 if (df && (flags & TDF_DETAILS))
2032 df_dump_top (bb, outf);
2033 putc ('\n', outf);
2036 if (bb->index != ENTRY_BLOCK && bb->index != EXIT_BLOCK)
2037 for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb)); insn != last;
2038 insn = NEXT_INSN (insn))
2040 if (flags & TDF_DETAILS)
2041 df_dump_insn_top (insn, outf);
2042 if (! (flags & TDF_SLIM))
2043 print_rtl_single (outf, insn);
2044 else
2045 dump_insn_slim (outf, insn);
2046 if (flags & TDF_DETAILS)
2047 df_dump_insn_bottom (insn, outf);
2050 if (df && (flags & TDF_DETAILS))
2052 df_dump_bottom (bb, outf);
2053 putc ('\n', outf);
2058 /* Like dump_function_to_file, but for RTL. Print out dataflow information
2059 for the start of each basic block. FLAGS are the TDF_* masks documented
2060 in dumpfile.h. */
2062 void
2063 print_rtl_with_bb (FILE *outf, const_rtx rtx_first, int flags)
2065 const_rtx tmp_rtx;
2066 if (rtx_first == 0)
2067 fprintf (outf, "(nil)\n");
2068 else
2070 enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
2071 int max_uid = get_max_uid ();
2072 basic_block *start = XCNEWVEC (basic_block, max_uid);
2073 basic_block *end = XCNEWVEC (basic_block, max_uid);
2074 enum bb_state *in_bb_p = XCNEWVEC (enum bb_state, max_uid);
2075 basic_block bb;
2077 /* After freeing the CFG, we still have BLOCK_FOR_INSN set on most
2078 insns, but the CFG is not maintained so the basic block info
2079 is not reliable. Therefore it's omitted from the dumps. */
2080 if (! (cfun->curr_properties & PROP_cfg))
2081 flags &= ~TDF_BLOCKS;
2083 if (df)
2084 df_dump_start (outf);
2086 if (flags & TDF_BLOCKS)
2088 FOR_EACH_BB_REVERSE (bb)
2090 rtx x;
2092 start[INSN_UID (BB_HEAD (bb))] = bb;
2093 end[INSN_UID (BB_END (bb))] = bb;
2094 for (x = BB_HEAD (bb); x != NULL_RTX; x = NEXT_INSN (x))
2096 enum bb_state state = IN_MULTIPLE_BB;
2098 if (in_bb_p[INSN_UID (x)] == NOT_IN_BB)
2099 state = IN_ONE_BB;
2100 in_bb_p[INSN_UID (x)] = state;
2102 if (x == BB_END (bb))
2103 break;
2108 for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
2110 if (flags & TDF_BLOCKS)
2112 bb = start[INSN_UID (tmp_rtx)];
2113 if (bb != NULL)
2115 dump_bb_info (outf, bb, 0, dump_flags | TDF_COMMENT, true, false);
2116 if (df && (flags & TDF_DETAILS))
2117 df_dump_top (bb, outf);
2120 if (in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB
2121 && !NOTE_P (tmp_rtx)
2122 && !BARRIER_P (tmp_rtx))
2123 fprintf (outf, ";; Insn is not within a basic block\n");
2124 else if (in_bb_p[INSN_UID (tmp_rtx)] == IN_MULTIPLE_BB)
2125 fprintf (outf, ";; Insn is in multiple basic blocks\n");
2128 if (flags & TDF_DETAILS)
2129 df_dump_insn_top (tmp_rtx, outf);
2130 if (! (flags & TDF_SLIM))
2131 print_rtl_single (outf, tmp_rtx);
2132 else
2133 dump_insn_slim (outf, tmp_rtx);
2134 if (flags & TDF_DETAILS)
2135 df_dump_insn_bottom (tmp_rtx, outf);
2137 if (flags & TDF_BLOCKS)
2139 bb = end[INSN_UID (tmp_rtx)];
2140 if (bb != NULL)
2142 dump_bb_info (outf, bb, 0, dump_flags | TDF_COMMENT, false, true);
2143 if (df && (flags & TDF_DETAILS))
2144 df_dump_bottom (bb, outf);
2145 putc ('\n', outf);
2150 free (start);
2151 free (end);
2152 free (in_bb_p);
2156 /* Update the branch probability of BB if a REG_BR_PROB is present. */
2158 void
2159 update_br_prob_note (basic_block bb)
2161 rtx note;
2162 if (!JUMP_P (BB_END (bb)))
2163 return;
2164 note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX);
2165 if (!note || INTVAL (XEXP (note, 0)) == BRANCH_EDGE (bb)->probability)
2166 return;
2167 XEXP (note, 0) = GEN_INT (BRANCH_EDGE (bb)->probability);
2170 /* Get the last insn associated with block BB (that includes barriers and
2171 tablejumps after BB). */
2173 get_last_bb_insn (basic_block bb)
2175 rtx tmp;
2176 rtx end = BB_END (bb);
2178 /* Include any jump table following the basic block. */
2179 if (tablejump_p (end, NULL, &tmp))
2180 end = tmp;
2182 /* Include any barriers that may follow the basic block. */
2183 tmp = next_nonnote_insn_bb (end);
2184 while (tmp && BARRIER_P (tmp))
2186 end = tmp;
2187 tmp = next_nonnote_insn_bb (end);
2190 return end;
2193 /* Verify, in the basic block chain, that there is at most one switch
2194 between hot/cold partitions. This condition will not be true until
2195 after reorder_basic_blocks is called. */
2197 static int
2198 verify_hot_cold_block_grouping (void)
2200 basic_block bb;
2201 int err = 0;
2202 bool switched_sections = false;
2203 int current_partition = BB_UNPARTITIONED;
2205 /* Even after bb reordering is complete, we go into cfglayout mode
2206 again (in compgoto). Ensure we don't call this before going back
2207 into linearized RTL when any layout fixes would have been committed. */
2208 if (!crtl->bb_reorder_complete
2209 || current_ir_type() != IR_RTL_CFGRTL)
2210 return err;
2212 FOR_EACH_BB (bb)
2214 if (current_partition != BB_UNPARTITIONED
2215 && BB_PARTITION (bb) != current_partition)
2217 if (switched_sections)
2219 error ("multiple hot/cold transitions found (bb %i)",
2220 bb->index);
2221 err = 1;
2223 else
2224 switched_sections = true;
2226 if (!crtl->has_bb_partition)
2227 error ("partition found but function partition flag not set");
2229 current_partition = BB_PARTITION (bb);
2232 return err;
2236 /* Perform several checks on the edges out of each block, such as
2237 the consistency of the branch probabilities, the correctness
2238 of hot/cold partition crossing edges, and the number of expected
2239 successor edges. */
2241 static int
2242 rtl_verify_edges (void)
2244 int err = 0;
2245 basic_block bb;
2247 FOR_EACH_BB_REVERSE (bb)
2249 int n_fallthru = 0, n_branch = 0, n_abnormal_call = 0, n_sibcall = 0;
2250 int n_eh = 0, n_abnormal = 0;
2251 edge e, fallthru = NULL;
2252 edge_iterator ei;
2253 rtx note;
2254 bool has_crossing_edge = false;
2256 if (JUMP_P (BB_END (bb))
2257 && (note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX))
2258 && EDGE_COUNT (bb->succs) >= 2
2259 && any_condjump_p (BB_END (bb)))
2261 if (INTVAL (XEXP (note, 0)) != BRANCH_EDGE (bb)->probability
2262 && profile_status != PROFILE_ABSENT)
2264 error ("verify_flow_info: REG_BR_PROB does not match cfg %wi %i",
2265 INTVAL (XEXP (note, 0)), BRANCH_EDGE (bb)->probability);
2266 err = 1;
2270 FOR_EACH_EDGE (e, ei, bb->succs)
2272 bool is_crossing;
2274 if (e->flags & EDGE_FALLTHRU)
2275 n_fallthru++, fallthru = e;
2277 is_crossing = (BB_PARTITION (e->src) != BB_PARTITION (e->dest)
2278 && e->src != ENTRY_BLOCK_PTR
2279 && e->dest != EXIT_BLOCK_PTR);
2280 has_crossing_edge |= is_crossing;
2281 if (e->flags & EDGE_CROSSING)
2283 if (!is_crossing)
2285 error ("EDGE_CROSSING incorrectly set across same section");
2286 err = 1;
2288 if (e->flags & EDGE_FALLTHRU)
2290 error ("fallthru edge crosses section boundary in bb %i",
2291 e->src->index);
2292 err = 1;
2294 if (e->flags & EDGE_EH)
2296 error ("EH edge crosses section boundary in bb %i",
2297 e->src->index);
2298 err = 1;
2300 if (JUMP_P (BB_END (bb))
2301 && !find_reg_note (BB_END (bb), REG_CROSSING_JUMP, NULL_RTX))
2303 error ("No region crossing jump at section boundary in bb %i",
2304 bb->index);
2305 err = 1;
2308 else if (is_crossing)
2310 error ("EDGE_CROSSING missing across section boundary");
2311 err = 1;
2314 if ((e->flags & ~(EDGE_DFS_BACK
2315 | EDGE_CAN_FALLTHRU
2316 | EDGE_IRREDUCIBLE_LOOP
2317 | EDGE_LOOP_EXIT
2318 | EDGE_CROSSING
2319 | EDGE_PRESERVE)) == 0)
2320 n_branch++;
2322 if (e->flags & EDGE_ABNORMAL_CALL)
2323 n_abnormal_call++;
2325 if (e->flags & EDGE_SIBCALL)
2326 n_sibcall++;
2328 if (e->flags & EDGE_EH)
2329 n_eh++;
2331 if (e->flags & EDGE_ABNORMAL)
2332 n_abnormal++;
2335 if (!has_crossing_edge
2336 && find_reg_note (BB_END (bb), REG_CROSSING_JUMP, NULL_RTX))
2338 print_rtl_with_bb (stderr, get_insns (), TDF_RTL | TDF_BLOCKS | TDF_DETAILS);
2339 error ("Region crossing jump across same section in bb %i",
2340 bb->index);
2341 err = 1;
2344 if (n_eh && !find_reg_note (BB_END (bb), REG_EH_REGION, NULL_RTX))
2346 error ("missing REG_EH_REGION note at the end of bb %i", bb->index);
2347 err = 1;
2349 if (n_eh > 1)
2351 error ("too many exception handling edges in bb %i", bb->index);
2352 err = 1;
2354 if (n_branch
2355 && (!JUMP_P (BB_END (bb))
2356 || (n_branch > 1 && (any_uncondjump_p (BB_END (bb))
2357 || any_condjump_p (BB_END (bb))))))
2359 error ("too many outgoing branch edges from bb %i", bb->index);
2360 err = 1;
2362 if (n_fallthru && any_uncondjump_p (BB_END (bb)))
2364 error ("fallthru edge after unconditional jump in bb %i", bb->index);
2365 err = 1;
2367 if (n_branch != 1 && any_uncondjump_p (BB_END (bb)))
2369 error ("wrong number of branch edges after unconditional jump"
2370 " in bb %i", bb->index);
2371 err = 1;
2373 if (n_branch != 1 && any_condjump_p (BB_END (bb))
2374 && JUMP_LABEL (BB_END (bb)) != BB_HEAD (fallthru->dest))
2376 error ("wrong amount of branch edges after conditional jump"
2377 " in bb %i", bb->index);
2378 err = 1;
2380 if (n_abnormal_call && !CALL_P (BB_END (bb)))
2382 error ("abnormal call edges for non-call insn in bb %i", bb->index);
2383 err = 1;
2385 if (n_sibcall && !CALL_P (BB_END (bb)))
2387 error ("sibcall edges for non-call insn in bb %i", bb->index);
2388 err = 1;
2390 if (n_abnormal > n_eh
2391 && !(CALL_P (BB_END (bb))
2392 && n_abnormal == n_abnormal_call + n_sibcall)
2393 && (!JUMP_P (BB_END (bb))
2394 || any_condjump_p (BB_END (bb))
2395 || any_uncondjump_p (BB_END (bb))))
2397 error ("abnormal edges for no purpose in bb %i", bb->index);
2398 err = 1;
2402 /* Clean up. */
2403 return err;
2406 /* Checks on the instructions within blocks. Currently checks that each
2407 block starts with a basic block note, and that basic block notes and
2408 control flow jumps are not found in the middle of the block. */
2410 static int
2411 rtl_verify_bb_insns (void)
2413 rtx x;
2414 int err = 0;
2415 basic_block bb;
2417 FOR_EACH_BB_REVERSE (bb)
2419 /* Now check the header of basic
2420 block. It ought to contain optional CODE_LABEL followed
2421 by NOTE_BASIC_BLOCK. */
2422 x = BB_HEAD (bb);
2423 if (LABEL_P (x))
2425 if (BB_END (bb) == x)
2427 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
2428 bb->index);
2429 err = 1;
2432 x = NEXT_INSN (x);
2435 if (!NOTE_INSN_BASIC_BLOCK_P (x) || NOTE_BASIC_BLOCK (x) != bb)
2437 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
2438 bb->index);
2439 err = 1;
2442 if (BB_END (bb) == x)
2443 /* Do checks for empty blocks here. */
2445 else
2446 for (x = NEXT_INSN (x); x; x = NEXT_INSN (x))
2448 if (NOTE_INSN_BASIC_BLOCK_P (x))
2450 error ("NOTE_INSN_BASIC_BLOCK %d in middle of basic block %d",
2451 INSN_UID (x), bb->index);
2452 err = 1;
2455 if (x == BB_END (bb))
2456 break;
2458 if (control_flow_insn_p (x))
2460 error ("in basic block %d:", bb->index);
2461 fatal_insn ("flow control insn inside a basic block", x);
2466 /* Clean up. */
2467 return err;
2470 /* Verify that block pointers for instructions in basic blocks, headers and
2471 footers are set appropriately. */
2473 static int
2474 rtl_verify_bb_pointers (void)
2476 int err = 0;
2477 basic_block bb;
2479 /* Check the general integrity of the basic blocks. */
2480 FOR_EACH_BB_REVERSE (bb)
2482 rtx insn;
2484 if (!(bb->flags & BB_RTL))
2486 error ("BB_RTL flag not set for block %d", bb->index);
2487 err = 1;
2490 FOR_BB_INSNS (bb, insn)
2491 if (BLOCK_FOR_INSN (insn) != bb)
2493 error ("insn %d basic block pointer is %d, should be %d",
2494 INSN_UID (insn),
2495 BLOCK_FOR_INSN (insn) ? BLOCK_FOR_INSN (insn)->index : 0,
2496 bb->index);
2497 err = 1;
2500 for (insn = BB_HEADER (bb); insn; insn = NEXT_INSN (insn))
2501 if (!BARRIER_P (insn)
2502 && BLOCK_FOR_INSN (insn) != NULL)
2504 error ("insn %d in header of bb %d has non-NULL basic block",
2505 INSN_UID (insn), bb->index);
2506 err = 1;
2508 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2509 if (!BARRIER_P (insn)
2510 && BLOCK_FOR_INSN (insn) != NULL)
2512 error ("insn %d in footer of bb %d has non-NULL basic block",
2513 INSN_UID (insn), bb->index);
2514 err = 1;
2518 /* Clean up. */
2519 return err;
2522 /* Verify the CFG and RTL consistency common for both underlying RTL and
2523 cfglayout RTL.
2525 Currently it does following checks:
2527 - overlapping of basic blocks
2528 - insns with wrong BLOCK_FOR_INSN pointers
2529 - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note)
2530 - tails of basic blocks (ensure that boundary is necessary)
2531 - scans body of the basic block for JUMP_INSN, CODE_LABEL
2532 and NOTE_INSN_BASIC_BLOCK
2533 - verify that no fall_thru edge crosses hot/cold partition boundaries
2534 - verify that there are no pending RTL branch predictions
2535 - verify that there is a single hot/cold partition boundary after bbro
2537 In future it can be extended check a lot of other stuff as well
2538 (reachability of basic blocks, life information, etc. etc.). */
2540 static int
2541 rtl_verify_flow_info_1 (void)
2543 int err = 0;
2545 err |= rtl_verify_bb_pointers ();
2547 err |= rtl_verify_bb_insns ();
2549 err |= rtl_verify_edges ();
2551 return err;
2554 /* Walk the instruction chain and verify that bb head/end pointers
2555 are correct, and that instructions are in exactly one bb and have
2556 correct block pointers. */
2558 static int
2559 rtl_verify_bb_insn_chain (void)
2561 basic_block bb;
2562 int err = 0;
2563 rtx x;
2564 rtx last_head = get_last_insn ();
2565 basic_block *bb_info;
2566 const int max_uid = get_max_uid ();
2568 bb_info = XCNEWVEC (basic_block, max_uid);
2570 FOR_EACH_BB_REVERSE (bb)
2572 rtx head = BB_HEAD (bb);
2573 rtx end = BB_END (bb);
2575 for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
2577 /* Verify the end of the basic block is in the INSN chain. */
2578 if (x == end)
2579 break;
2581 /* And that the code outside of basic blocks has NULL bb field. */
2582 if (!BARRIER_P (x)
2583 && BLOCK_FOR_INSN (x) != NULL)
2585 error ("insn %d outside of basic blocks has non-NULL bb field",
2586 INSN_UID (x));
2587 err = 1;
2591 if (!x)
2593 error ("end insn %d for block %d not found in the insn stream",
2594 INSN_UID (end), bb->index);
2595 err = 1;
2598 /* Work backwards from the end to the head of the basic block
2599 to verify the head is in the RTL chain. */
2600 for (; x != NULL_RTX; x = PREV_INSN (x))
2602 /* While walking over the insn chain, verify insns appear
2603 in only one basic block. */
2604 if (bb_info[INSN_UID (x)] != NULL)
2606 error ("insn %d is in multiple basic blocks (%d and %d)",
2607 INSN_UID (x), bb->index, bb_info[INSN_UID (x)]->index);
2608 err = 1;
2611 bb_info[INSN_UID (x)] = bb;
2613 if (x == head)
2614 break;
2616 if (!x)
2618 error ("head insn %d for block %d not found in the insn stream",
2619 INSN_UID (head), bb->index);
2620 err = 1;
2623 last_head = PREV_INSN (x);
2626 for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
2628 /* Check that the code before the first basic block has NULL
2629 bb field. */
2630 if (!BARRIER_P (x)
2631 && BLOCK_FOR_INSN (x) != NULL)
2633 error ("insn %d outside of basic blocks has non-NULL bb field",
2634 INSN_UID (x));
2635 err = 1;
2638 free (bb_info);
2640 return err;
2643 /* Verify that fallthru edges point to adjacent blocks in layout order and
2644 that barriers exist after non-fallthru blocks. */
2646 static int
2647 rtl_verify_fallthru (void)
2649 basic_block bb;
2650 int err = 0;
2652 FOR_EACH_BB_REVERSE (bb)
2654 edge e;
2656 e = find_fallthru_edge (bb->succs);
2657 if (!e)
2659 rtx insn;
2661 /* Ensure existence of barrier in BB with no fallthru edges. */
2662 for (insn = NEXT_INSN (BB_END (bb)); ; insn = NEXT_INSN (insn))
2664 if (!insn || NOTE_INSN_BASIC_BLOCK_P (insn))
2666 error ("missing barrier after block %i", bb->index);
2667 err = 1;
2668 break;
2670 if (BARRIER_P (insn))
2671 break;
2674 else if (e->src != ENTRY_BLOCK_PTR
2675 && e->dest != EXIT_BLOCK_PTR)
2677 rtx insn;
2679 if (e->src->next_bb != e->dest)
2681 error
2682 ("verify_flow_info: Incorrect blocks for fallthru %i->%i",
2683 e->src->index, e->dest->index);
2684 err = 1;
2686 else
2687 for (insn = NEXT_INSN (BB_END (e->src)); insn != BB_HEAD (e->dest);
2688 insn = NEXT_INSN (insn))
2689 if (BARRIER_P (insn) || INSN_P (insn))
2691 error ("verify_flow_info: Incorrect fallthru %i->%i",
2692 e->src->index, e->dest->index);
2693 fatal_insn ("wrong insn in the fallthru edge", insn);
2694 err = 1;
2699 return err;
2702 /* Verify that blocks are laid out in consecutive order. While walking the
2703 instructions, verify that all expected instructions are inside the basic
2704 blocks, and that all returns are followed by barriers. */
2706 static int
2707 rtl_verify_bb_layout (void)
2709 basic_block bb;
2710 int err = 0;
2711 rtx x;
2712 int num_bb_notes;
2713 const rtx rtx_first = get_insns ();
2714 basic_block last_bb_seen = ENTRY_BLOCK_PTR, curr_bb = NULL;
2716 num_bb_notes = 0;
2717 last_bb_seen = ENTRY_BLOCK_PTR;
2719 for (x = rtx_first; x; x = NEXT_INSN (x))
2721 if (NOTE_INSN_BASIC_BLOCK_P (x))
2723 bb = NOTE_BASIC_BLOCK (x);
2725 num_bb_notes++;
2726 if (bb != last_bb_seen->next_bb)
2727 internal_error ("basic blocks not laid down consecutively");
2729 curr_bb = last_bb_seen = bb;
2732 if (!curr_bb)
2734 switch (GET_CODE (x))
2736 case BARRIER:
2737 case NOTE:
2738 break;
2740 case CODE_LABEL:
2741 /* An ADDR_VEC is placed outside any basic block. */
2742 if (NEXT_INSN (x)
2743 && JUMP_TABLE_DATA_P (NEXT_INSN (x)))
2744 x = NEXT_INSN (x);
2746 /* But in any case, non-deletable labels can appear anywhere. */
2747 break;
2749 default:
2750 fatal_insn ("insn outside basic block", x);
2754 if (JUMP_P (x)
2755 && returnjump_p (x) && ! condjump_p (x)
2756 && ! (next_nonnote_insn (x) && BARRIER_P (next_nonnote_insn (x))))
2757 fatal_insn ("return not followed by barrier", x);
2759 if (curr_bb && x == BB_END (curr_bb))
2760 curr_bb = NULL;
2763 if (num_bb_notes != n_basic_blocks - NUM_FIXED_BLOCKS)
2764 internal_error
2765 ("number of bb notes in insn chain (%d) != n_basic_blocks (%d)",
2766 num_bb_notes, n_basic_blocks);
2768 return err;
2771 /* Verify the CFG and RTL consistency common for both underlying RTL and
2772 cfglayout RTL, plus consistency checks specific to linearized RTL mode.
2774 Currently it does following checks:
2775 - all checks of rtl_verify_flow_info_1
2776 - test head/end pointers
2777 - check that blocks are laid out in consecutive order
2778 - check that all insns are in the basic blocks
2779 (except the switch handling code, barriers and notes)
2780 - check that all returns are followed by barriers
2781 - check that all fallthru edge points to the adjacent blocks. */
2783 static int
2784 rtl_verify_flow_info (void)
2786 int err = 0;
2788 err |= rtl_verify_flow_info_1 ();
2790 err |= rtl_verify_bb_insn_chain ();
2792 err |= rtl_verify_fallthru ();
2794 err |= rtl_verify_bb_layout ();
2796 err |= verify_hot_cold_block_grouping ();
2798 return err;
2801 /* Assume that the preceding pass has possibly eliminated jump instructions
2802 or converted the unconditional jumps. Eliminate the edges from CFG.
2803 Return true if any edges are eliminated. */
2805 bool
2806 purge_dead_edges (basic_block bb)
2808 edge e;
2809 rtx insn = BB_END (bb), note;
2810 bool purged = false;
2811 bool found;
2812 edge_iterator ei;
2814 if (DEBUG_INSN_P (insn) && insn != BB_HEAD (bb))
2816 insn = PREV_INSN (insn);
2817 while ((DEBUG_INSN_P (insn) || NOTE_P (insn)) && insn != BB_HEAD (bb));
2819 /* If this instruction cannot trap, remove REG_EH_REGION notes. */
2820 if (NONJUMP_INSN_P (insn)
2821 && (note = find_reg_note (insn, REG_EH_REGION, NULL)))
2823 rtx eqnote;
2825 if (! may_trap_p (PATTERN (insn))
2826 || ((eqnote = find_reg_equal_equiv_note (insn))
2827 && ! may_trap_p (XEXP (eqnote, 0))))
2828 remove_note (insn, note);
2831 /* Cleanup abnormal edges caused by exceptions or non-local gotos. */
2832 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2834 bool remove = false;
2836 /* There are three types of edges we need to handle correctly here: EH
2837 edges, abnormal call EH edges, and abnormal call non-EH edges. The
2838 latter can appear when nonlocal gotos are used. */
2839 if (e->flags & EDGE_ABNORMAL_CALL)
2841 if (!CALL_P (insn))
2842 remove = true;
2843 else if (can_nonlocal_goto (insn))
2845 else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
2847 else if (flag_tm && find_reg_note (insn, REG_TM, NULL))
2849 else
2850 remove = true;
2852 else if (e->flags & EDGE_EH)
2853 remove = !can_throw_internal (insn);
2855 if (remove)
2857 remove_edge (e);
2858 df_set_bb_dirty (bb);
2859 purged = true;
2861 else
2862 ei_next (&ei);
2865 if (JUMP_P (insn))
2867 rtx note;
2868 edge b,f;
2869 edge_iterator ei;
2871 /* We do care only about conditional jumps and simplejumps. */
2872 if (!any_condjump_p (insn)
2873 && !returnjump_p (insn)
2874 && !simplejump_p (insn))
2875 return purged;
2877 /* Branch probability/prediction notes are defined only for
2878 condjumps. We've possibly turned condjump into simplejump. */
2879 if (simplejump_p (insn))
2881 note = find_reg_note (insn, REG_BR_PROB, NULL);
2882 if (note)
2883 remove_note (insn, note);
2884 while ((note = find_reg_note (insn, REG_BR_PRED, NULL)))
2885 remove_note (insn, note);
2888 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2890 /* Avoid abnormal flags to leak from computed jumps turned
2891 into simplejumps. */
2893 e->flags &= ~EDGE_ABNORMAL;
2895 /* See if this edge is one we should keep. */
2896 if ((e->flags & EDGE_FALLTHRU) && any_condjump_p (insn))
2897 /* A conditional jump can fall through into the next
2898 block, so we should keep the edge. */
2900 ei_next (&ei);
2901 continue;
2903 else if (e->dest != EXIT_BLOCK_PTR
2904 && BB_HEAD (e->dest) == JUMP_LABEL (insn))
2905 /* If the destination block is the target of the jump,
2906 keep the edge. */
2908 ei_next (&ei);
2909 continue;
2911 else if (e->dest == EXIT_BLOCK_PTR && returnjump_p (insn))
2912 /* If the destination block is the exit block, and this
2913 instruction is a return, then keep the edge. */
2915 ei_next (&ei);
2916 continue;
2918 else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
2919 /* Keep the edges that correspond to exceptions thrown by
2920 this instruction and rematerialize the EDGE_ABNORMAL
2921 flag we just cleared above. */
2923 e->flags |= EDGE_ABNORMAL;
2924 ei_next (&ei);
2925 continue;
2928 /* We do not need this edge. */
2929 df_set_bb_dirty (bb);
2930 purged = true;
2931 remove_edge (e);
2934 if (EDGE_COUNT (bb->succs) == 0 || !purged)
2935 return purged;
2937 if (dump_file)
2938 fprintf (dump_file, "Purged edges from bb %i\n", bb->index);
2940 if (!optimize)
2941 return purged;
2943 /* Redistribute probabilities. */
2944 if (single_succ_p (bb))
2946 single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
2947 single_succ_edge (bb)->count = bb->count;
2949 else
2951 note = find_reg_note (insn, REG_BR_PROB, NULL);
2952 if (!note)
2953 return purged;
2955 b = BRANCH_EDGE (bb);
2956 f = FALLTHRU_EDGE (bb);
2957 b->probability = INTVAL (XEXP (note, 0));
2958 f->probability = REG_BR_PROB_BASE - b->probability;
2959 /* Update these to use GCOV_COMPUTE_SCALE. */
2960 b->count = bb->count * b->probability / REG_BR_PROB_BASE;
2961 f->count = bb->count * f->probability / REG_BR_PROB_BASE;
2964 return purged;
2966 else if (CALL_P (insn) && SIBLING_CALL_P (insn))
2968 /* First, there should not be any EH or ABCALL edges resulting
2969 from non-local gotos and the like. If there were, we shouldn't
2970 have created the sibcall in the first place. Second, there
2971 should of course never have been a fallthru edge. */
2972 gcc_assert (single_succ_p (bb));
2973 gcc_assert (single_succ_edge (bb)->flags
2974 == (EDGE_SIBCALL | EDGE_ABNORMAL));
2976 return 0;
2979 /* If we don't see a jump insn, we don't know exactly why the block would
2980 have been broken at this point. Look for a simple, non-fallthru edge,
2981 as these are only created by conditional branches. If we find such an
2982 edge we know that there used to be a jump here and can then safely
2983 remove all non-fallthru edges. */
2984 found = false;
2985 FOR_EACH_EDGE (e, ei, bb->succs)
2986 if (! (e->flags & (EDGE_COMPLEX | EDGE_FALLTHRU)))
2988 found = true;
2989 break;
2992 if (!found)
2993 return purged;
2995 /* Remove all but the fake and fallthru edges. The fake edge may be
2996 the only successor for this block in the case of noreturn
2997 calls. */
2998 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3000 if (!(e->flags & (EDGE_FALLTHRU | EDGE_FAKE)))
3002 df_set_bb_dirty (bb);
3003 remove_edge (e);
3004 purged = true;
3006 else
3007 ei_next (&ei);
3010 gcc_assert (single_succ_p (bb));
3012 single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
3013 single_succ_edge (bb)->count = bb->count;
3015 if (dump_file)
3016 fprintf (dump_file, "Purged non-fallthru edges from bb %i\n",
3017 bb->index);
3018 return purged;
3021 /* Search all basic blocks for potentially dead edges and purge them. Return
3022 true if some edge has been eliminated. */
3024 bool
3025 purge_all_dead_edges (void)
3027 int purged = false;
3028 basic_block bb;
3030 FOR_EACH_BB (bb)
3032 bool purged_here = purge_dead_edges (bb);
3034 purged |= purged_here;
3037 return purged;
3040 /* This is used by a few passes that emit some instructions after abnormal
3041 calls, moving the basic block's end, while they in fact do want to emit
3042 them on the fallthru edge. Look for abnormal call edges, find backward
3043 the call in the block and insert the instructions on the edge instead.
3045 Similarly, handle instructions throwing exceptions internally.
3047 Return true when instructions have been found and inserted on edges. */
3049 bool
3050 fixup_abnormal_edges (void)
3052 bool inserted = false;
3053 basic_block bb;
3055 FOR_EACH_BB (bb)
3057 edge e;
3058 edge_iterator ei;
3060 /* Look for cases we are interested in - calls or instructions causing
3061 exceptions. */
3062 FOR_EACH_EDGE (e, ei, bb->succs)
3063 if ((e->flags & EDGE_ABNORMAL_CALL)
3064 || ((e->flags & (EDGE_ABNORMAL | EDGE_EH))
3065 == (EDGE_ABNORMAL | EDGE_EH)))
3066 break;
3068 if (e && !CALL_P (BB_END (bb)) && !can_throw_internal (BB_END (bb)))
3070 rtx insn;
3072 /* Get past the new insns generated. Allow notes, as the insns
3073 may be already deleted. */
3074 insn = BB_END (bb);
3075 while ((NONJUMP_INSN_P (insn) || NOTE_P (insn))
3076 && !can_throw_internal (insn)
3077 && insn != BB_HEAD (bb))
3078 insn = PREV_INSN (insn);
3080 if (CALL_P (insn) || can_throw_internal (insn))
3082 rtx stop, next;
3084 e = find_fallthru_edge (bb->succs);
3086 stop = NEXT_INSN (BB_END (bb));
3087 BB_END (bb) = insn;
3089 for (insn = NEXT_INSN (insn); insn != stop; insn = next)
3091 next = NEXT_INSN (insn);
3092 if (INSN_P (insn))
3094 delete_insn (insn);
3096 /* Sometimes there's still the return value USE.
3097 If it's placed after a trapping call (i.e. that
3098 call is the last insn anyway), we have no fallthru
3099 edge. Simply delete this use and don't try to insert
3100 on the non-existent edge. */
3101 if (GET_CODE (PATTERN (insn)) != USE)
3103 /* We're not deleting it, we're moving it. */
3104 INSN_DELETED_P (insn) = 0;
3105 PREV_INSN (insn) = NULL_RTX;
3106 NEXT_INSN (insn) = NULL_RTX;
3108 insert_insn_on_edge (insn, e);
3109 inserted = true;
3112 else if (!BARRIER_P (insn))
3113 set_block_for_insn (insn, NULL);
3117 /* It may be that we don't find any trapping insn. In this
3118 case we discovered quite late that the insn that had been
3119 marked as can_throw_internal in fact couldn't trap at all.
3120 So we should in fact delete the EH edges out of the block. */
3121 else
3122 purge_dead_edges (bb);
3126 return inserted;
3129 /* Cut the insns from FIRST to LAST out of the insns stream. */
3132 unlink_insn_chain (rtx first, rtx last)
3134 rtx prevfirst = PREV_INSN (first);
3135 rtx nextlast = NEXT_INSN (last);
3137 PREV_INSN (first) = NULL;
3138 NEXT_INSN (last) = NULL;
3139 if (prevfirst)
3140 NEXT_INSN (prevfirst) = nextlast;
3141 if (nextlast)
3142 PREV_INSN (nextlast) = prevfirst;
3143 else
3144 set_last_insn (prevfirst);
3145 if (!prevfirst)
3146 set_first_insn (nextlast);
3147 return first;
3150 /* Skip over inter-block insns occurring after BB which are typically
3151 associated with BB (e.g., barriers). If there are any such insns,
3152 we return the last one. Otherwise, we return the end of BB. */
3154 static rtx
3155 skip_insns_after_block (basic_block bb)
3157 rtx insn, last_insn, next_head, prev;
3159 next_head = NULL_RTX;
3160 if (bb->next_bb != EXIT_BLOCK_PTR)
3161 next_head = BB_HEAD (bb->next_bb);
3163 for (last_insn = insn = BB_END (bb); (insn = NEXT_INSN (insn)) != 0; )
3165 if (insn == next_head)
3166 break;
3168 switch (GET_CODE (insn))
3170 case BARRIER:
3171 last_insn = insn;
3172 continue;
3174 case NOTE:
3175 switch (NOTE_KIND (insn))
3177 case NOTE_INSN_BLOCK_END:
3178 gcc_unreachable ();
3179 continue;
3180 default:
3181 continue;
3182 break;
3184 break;
3186 case CODE_LABEL:
3187 if (NEXT_INSN (insn)
3188 && JUMP_TABLE_DATA_P (NEXT_INSN (insn)))
3190 insn = NEXT_INSN (insn);
3191 last_insn = insn;
3192 continue;
3194 break;
3196 default:
3197 break;
3200 break;
3203 /* It is possible to hit contradictory sequence. For instance:
3205 jump_insn
3206 NOTE_INSN_BLOCK_BEG
3207 barrier
3209 Where barrier belongs to jump_insn, but the note does not. This can be
3210 created by removing the basic block originally following
3211 NOTE_INSN_BLOCK_BEG. In such case reorder the notes. */
3213 for (insn = last_insn; insn != BB_END (bb); insn = prev)
3215 prev = PREV_INSN (insn);
3216 if (NOTE_P (insn))
3217 switch (NOTE_KIND (insn))
3219 case NOTE_INSN_BLOCK_END:
3220 gcc_unreachable ();
3221 break;
3222 case NOTE_INSN_DELETED:
3223 case NOTE_INSN_DELETED_LABEL:
3224 case NOTE_INSN_DELETED_DEBUG_LABEL:
3225 continue;
3226 default:
3227 reorder_insns (insn, insn, last_insn);
3231 return last_insn;
3234 /* Locate or create a label for a given basic block. */
3236 static rtx
3237 label_for_bb (basic_block bb)
3239 rtx label = BB_HEAD (bb);
3241 if (!LABEL_P (label))
3243 if (dump_file)
3244 fprintf (dump_file, "Emitting label for block %d\n", bb->index);
3246 label = block_label (bb);
3249 return label;
3252 /* Locate the effective beginning and end of the insn chain for each
3253 block, as defined by skip_insns_after_block above. */
3255 static void
3256 record_effective_endpoints (void)
3258 rtx next_insn;
3259 basic_block bb;
3260 rtx insn;
3262 for (insn = get_insns ();
3263 insn
3264 && NOTE_P (insn)
3265 && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK;
3266 insn = NEXT_INSN (insn))
3267 continue;
3268 /* No basic blocks at all? */
3269 gcc_assert (insn);
3271 if (PREV_INSN (insn))
3272 cfg_layout_function_header =
3273 unlink_insn_chain (get_insns (), PREV_INSN (insn));
3274 else
3275 cfg_layout_function_header = NULL_RTX;
3277 next_insn = get_insns ();
3278 FOR_EACH_BB (bb)
3280 rtx end;
3282 if (PREV_INSN (BB_HEAD (bb)) && next_insn != BB_HEAD (bb))
3283 BB_HEADER (bb) = unlink_insn_chain (next_insn,
3284 PREV_INSN (BB_HEAD (bb)));
3285 end = skip_insns_after_block (bb);
3286 if (NEXT_INSN (BB_END (bb)) && BB_END (bb) != end)
3287 BB_FOOTER (bb) = unlink_insn_chain (NEXT_INSN (BB_END (bb)), end);
3288 next_insn = NEXT_INSN (BB_END (bb));
3291 cfg_layout_function_footer = next_insn;
3292 if (cfg_layout_function_footer)
3293 cfg_layout_function_footer = unlink_insn_chain (cfg_layout_function_footer, get_last_insn ());
3296 static unsigned int
3297 into_cfg_layout_mode (void)
3299 cfg_layout_initialize (0);
3300 return 0;
3303 static unsigned int
3304 outof_cfg_layout_mode (void)
3306 basic_block bb;
3308 FOR_EACH_BB (bb)
3309 if (bb->next_bb != EXIT_BLOCK_PTR)
3310 bb->aux = bb->next_bb;
3312 cfg_layout_finalize ();
3314 return 0;
3317 namespace {
3319 const pass_data pass_data_into_cfg_layout_mode =
3321 RTL_PASS, /* type */
3322 "into_cfglayout", /* name */
3323 OPTGROUP_NONE, /* optinfo_flags */
3324 false, /* has_gate */
3325 true, /* has_execute */
3326 TV_CFG, /* tv_id */
3327 0, /* properties_required */
3328 PROP_cfglayout, /* properties_provided */
3329 0, /* properties_destroyed */
3330 0, /* todo_flags_start */
3331 0, /* todo_flags_finish */
3334 class pass_into_cfg_layout_mode : public rtl_opt_pass
3336 public:
3337 pass_into_cfg_layout_mode(gcc::context *ctxt)
3338 : rtl_opt_pass(pass_data_into_cfg_layout_mode, ctxt)
3341 /* opt_pass methods: */
3342 unsigned int execute () { return into_cfg_layout_mode (); }
3344 }; // class pass_into_cfg_layout_mode
3346 } // anon namespace
3348 rtl_opt_pass *
3349 make_pass_into_cfg_layout_mode (gcc::context *ctxt)
3351 return new pass_into_cfg_layout_mode (ctxt);
3354 namespace {
3356 const pass_data pass_data_outof_cfg_layout_mode =
3358 RTL_PASS, /* type */
3359 "outof_cfglayout", /* name */
3360 OPTGROUP_NONE, /* optinfo_flags */
3361 false, /* has_gate */
3362 true, /* has_execute */
3363 TV_CFG, /* tv_id */
3364 0, /* properties_required */
3365 0, /* properties_provided */
3366 PROP_cfglayout, /* properties_destroyed */
3367 0, /* todo_flags_start */
3368 0, /* todo_flags_finish */
3371 class pass_outof_cfg_layout_mode : public rtl_opt_pass
3373 public:
3374 pass_outof_cfg_layout_mode(gcc::context *ctxt)
3375 : rtl_opt_pass(pass_data_outof_cfg_layout_mode, ctxt)
3378 /* opt_pass methods: */
3379 unsigned int execute () { return outof_cfg_layout_mode (); }
3381 }; // class pass_outof_cfg_layout_mode
3383 } // anon namespace
3385 rtl_opt_pass *
3386 make_pass_outof_cfg_layout_mode (gcc::context *ctxt)
3388 return new pass_outof_cfg_layout_mode (ctxt);
3392 /* Link the basic blocks in the correct order, compacting the basic
3393 block queue while at it. If STAY_IN_CFGLAYOUT_MODE is false, this
3394 function also clears the basic block header and footer fields.
3396 This function is usually called after a pass (e.g. tracer) finishes
3397 some transformations while in cfglayout mode. The required sequence
3398 of the basic blocks is in a linked list along the bb->aux field.
3399 This functions re-links the basic block prev_bb and next_bb pointers
3400 accordingly, and it compacts and renumbers the blocks.
3402 FIXME: This currently works only for RTL, but the only RTL-specific
3403 bits are the STAY_IN_CFGLAYOUT_MODE bits. The tracer pass was moved
3404 to GIMPLE a long time ago, but it doesn't relink the basic block
3405 chain. It could do that (to give better initial RTL) if this function
3406 is made IR-agnostic (and moved to cfganal.c or cfg.c while at it). */
3408 void
3409 relink_block_chain (bool stay_in_cfglayout_mode)
3411 basic_block bb, prev_bb;
3412 int index;
3414 /* Maybe dump the re-ordered sequence. */
3415 if (dump_file)
3417 fprintf (dump_file, "Reordered sequence:\n");
3418 for (bb = ENTRY_BLOCK_PTR->next_bb, index = NUM_FIXED_BLOCKS;
3420 bb = (basic_block) bb->aux, index++)
3422 fprintf (dump_file, " %i ", index);
3423 if (get_bb_original (bb))
3424 fprintf (dump_file, "duplicate of %i ",
3425 get_bb_original (bb)->index);
3426 else if (forwarder_block_p (bb)
3427 && !LABEL_P (BB_HEAD (bb)))
3428 fprintf (dump_file, "compensation ");
3429 else
3430 fprintf (dump_file, "bb %i ", bb->index);
3431 fprintf (dump_file, " [%i]\n", bb->frequency);
3435 /* Now reorder the blocks. */
3436 prev_bb = ENTRY_BLOCK_PTR;
3437 bb = ENTRY_BLOCK_PTR->next_bb;
3438 for (; bb; prev_bb = bb, bb = (basic_block) bb->aux)
3440 bb->prev_bb = prev_bb;
3441 prev_bb->next_bb = bb;
3443 prev_bb->next_bb = EXIT_BLOCK_PTR;
3444 EXIT_BLOCK_PTR->prev_bb = prev_bb;
3446 /* Then, clean up the aux fields. */
3447 FOR_ALL_BB (bb)
3449 bb->aux = NULL;
3450 if (!stay_in_cfglayout_mode)
3451 BB_HEADER (bb) = BB_FOOTER (bb) = NULL;
3454 /* Maybe reset the original copy tables, they are not valid anymore
3455 when we renumber the basic blocks in compact_blocks. If we are
3456 are going out of cfglayout mode, don't re-allocate the tables. */
3457 free_original_copy_tables ();
3458 if (stay_in_cfglayout_mode)
3459 initialize_original_copy_tables ();
3461 /* Finally, put basic_block_info in the new order. */
3462 compact_blocks ();
3466 /* Given a reorder chain, rearrange the code to match. */
3468 static void
3469 fixup_reorder_chain (void)
3471 basic_block bb;
3472 rtx insn = NULL;
3474 if (cfg_layout_function_header)
3476 set_first_insn (cfg_layout_function_header);
3477 insn = cfg_layout_function_header;
3478 while (NEXT_INSN (insn))
3479 insn = NEXT_INSN (insn);
3482 /* First do the bulk reordering -- rechain the blocks without regard to
3483 the needed changes to jumps and labels. */
3485 for (bb = ENTRY_BLOCK_PTR->next_bb; bb; bb = (basic_block) bb->aux)
3487 if (BB_HEADER (bb))
3489 if (insn)
3490 NEXT_INSN (insn) = BB_HEADER (bb);
3491 else
3492 set_first_insn (BB_HEADER (bb));
3493 PREV_INSN (BB_HEADER (bb)) = insn;
3494 insn = BB_HEADER (bb);
3495 while (NEXT_INSN (insn))
3496 insn = NEXT_INSN (insn);
3498 if (insn)
3499 NEXT_INSN (insn) = BB_HEAD (bb);
3500 else
3501 set_first_insn (BB_HEAD (bb));
3502 PREV_INSN (BB_HEAD (bb)) = insn;
3503 insn = BB_END (bb);
3504 if (BB_FOOTER (bb))
3506 NEXT_INSN (insn) = BB_FOOTER (bb);
3507 PREV_INSN (BB_FOOTER (bb)) = insn;
3508 while (NEXT_INSN (insn))
3509 insn = NEXT_INSN (insn);
3513 NEXT_INSN (insn) = cfg_layout_function_footer;
3514 if (cfg_layout_function_footer)
3515 PREV_INSN (cfg_layout_function_footer) = insn;
3517 while (NEXT_INSN (insn))
3518 insn = NEXT_INSN (insn);
3520 set_last_insn (insn);
3521 #ifdef ENABLE_CHECKING
3522 verify_insn_chain ();
3523 #endif
3525 /* Now add jumps and labels as needed to match the blocks new
3526 outgoing edges. */
3528 for (bb = ENTRY_BLOCK_PTR->next_bb; bb ; bb = (basic_block) bb->aux)
3530 edge e_fall, e_taken, e;
3531 rtx bb_end_insn;
3532 rtx ret_label = NULL_RTX;
3533 basic_block nb;
3534 edge_iterator ei;
3536 if (EDGE_COUNT (bb->succs) == 0)
3537 continue;
3539 /* Find the old fallthru edge, and another non-EH edge for
3540 a taken jump. */
3541 e_taken = e_fall = NULL;
3543 FOR_EACH_EDGE (e, ei, bb->succs)
3544 if (e->flags & EDGE_FALLTHRU)
3545 e_fall = e;
3546 else if (! (e->flags & EDGE_EH))
3547 e_taken = e;
3549 bb_end_insn = BB_END (bb);
3550 if (JUMP_P (bb_end_insn))
3552 ret_label = JUMP_LABEL (bb_end_insn);
3553 if (any_condjump_p (bb_end_insn))
3555 /* This might happen if the conditional jump has side
3556 effects and could therefore not be optimized away.
3557 Make the basic block to end with a barrier in order
3558 to prevent rtl_verify_flow_info from complaining. */
3559 if (!e_fall)
3561 gcc_assert (!onlyjump_p (bb_end_insn)
3562 || returnjump_p (bb_end_insn));
3563 emit_barrier_after (bb_end_insn);
3564 continue;
3567 /* If the old fallthru is still next, nothing to do. */
3568 if (bb->aux == e_fall->dest
3569 || e_fall->dest == EXIT_BLOCK_PTR)
3570 continue;
3572 /* The degenerated case of conditional jump jumping to the next
3573 instruction can happen for jumps with side effects. We need
3574 to construct a forwarder block and this will be done just
3575 fine by force_nonfallthru below. */
3576 if (!e_taken)
3579 /* There is another special case: if *neither* block is next,
3580 such as happens at the very end of a function, then we'll
3581 need to add a new unconditional jump. Choose the taken
3582 edge based on known or assumed probability. */
3583 else if (bb->aux != e_taken->dest)
3585 rtx note = find_reg_note (bb_end_insn, REG_BR_PROB, 0);
3587 if (note
3588 && INTVAL (XEXP (note, 0)) < REG_BR_PROB_BASE / 2
3589 && invert_jump (bb_end_insn,
3590 (e_fall->dest == EXIT_BLOCK_PTR
3591 ? NULL_RTX
3592 : label_for_bb (e_fall->dest)), 0))
3594 e_fall->flags &= ~EDGE_FALLTHRU;
3595 gcc_checking_assert (could_fall_through
3596 (e_taken->src, e_taken->dest));
3597 e_taken->flags |= EDGE_FALLTHRU;
3598 update_br_prob_note (bb);
3599 e = e_fall, e_fall = e_taken, e_taken = e;
3603 /* If the "jumping" edge is a crossing edge, and the fall
3604 through edge is non-crossing, leave things as they are. */
3605 else if ((e_taken->flags & EDGE_CROSSING)
3606 && !(e_fall->flags & EDGE_CROSSING))
3607 continue;
3609 /* Otherwise we can try to invert the jump. This will
3610 basically never fail, however, keep up the pretense. */
3611 else if (invert_jump (bb_end_insn,
3612 (e_fall->dest == EXIT_BLOCK_PTR
3613 ? NULL_RTX
3614 : label_for_bb (e_fall->dest)), 0))
3616 e_fall->flags &= ~EDGE_FALLTHRU;
3617 gcc_checking_assert (could_fall_through
3618 (e_taken->src, e_taken->dest));
3619 e_taken->flags |= EDGE_FALLTHRU;
3620 update_br_prob_note (bb);
3621 if (LABEL_NUSES (ret_label) == 0
3622 && single_pred_p (e_taken->dest))
3623 delete_insn (ret_label);
3624 continue;
3627 else if (extract_asm_operands (PATTERN (bb_end_insn)) != NULL)
3629 /* If the old fallthru is still next or if
3630 asm goto doesn't have a fallthru (e.g. when followed by
3631 __builtin_unreachable ()), nothing to do. */
3632 if (! e_fall
3633 || bb->aux == e_fall->dest
3634 || e_fall->dest == EXIT_BLOCK_PTR)
3635 continue;
3637 /* Otherwise we'll have to use the fallthru fixup below. */
3639 else
3641 /* Otherwise we have some return, switch or computed
3642 jump. In the 99% case, there should not have been a
3643 fallthru edge. */
3644 gcc_assert (returnjump_p (bb_end_insn) || !e_fall);
3645 continue;
3648 else
3650 /* No fallthru implies a noreturn function with EH edges, or
3651 something similarly bizarre. In any case, we don't need to
3652 do anything. */
3653 if (! e_fall)
3654 continue;
3656 /* If the fallthru block is still next, nothing to do. */
3657 if (bb->aux == e_fall->dest)
3658 continue;
3660 /* A fallthru to exit block. */
3661 if (e_fall->dest == EXIT_BLOCK_PTR)
3662 continue;
3665 /* We got here if we need to add a new jump insn.
3666 Note force_nonfallthru can delete E_FALL and thus we have to
3667 save E_FALL->src prior to the call to force_nonfallthru. */
3668 nb = force_nonfallthru_and_redirect (e_fall, e_fall->dest, ret_label);
3669 if (nb)
3671 nb->aux = bb->aux;
3672 bb->aux = nb;
3673 /* Don't process this new block. */
3674 bb = nb;
3678 relink_block_chain (/*stay_in_cfglayout_mode=*/false);
3680 /* Annoying special case - jump around dead jumptables left in the code. */
3681 FOR_EACH_BB (bb)
3683 edge e = find_fallthru_edge (bb->succs);
3685 if (e && !can_fallthru (e->src, e->dest))
3686 force_nonfallthru (e);
3689 /* Ensure goto_locus from edges has some instructions with that locus
3690 in RTL. */
3691 if (!optimize)
3692 FOR_EACH_BB (bb)
3694 edge e;
3695 edge_iterator ei;
3697 FOR_EACH_EDGE (e, ei, bb->succs)
3698 if (LOCATION_LOCUS (e->goto_locus) != UNKNOWN_LOCATION
3699 && !(e->flags & EDGE_ABNORMAL))
3701 edge e2;
3702 edge_iterator ei2;
3703 basic_block dest, nb;
3704 rtx end;
3706 insn = BB_END (e->src);
3707 end = PREV_INSN (BB_HEAD (e->src));
3708 while (insn != end
3709 && (!NONDEBUG_INSN_P (insn) || !INSN_HAS_LOCATION (insn)))
3710 insn = PREV_INSN (insn);
3711 if (insn != end
3712 && INSN_LOCATION (insn) == e->goto_locus)
3713 continue;
3714 if (simplejump_p (BB_END (e->src))
3715 && !INSN_HAS_LOCATION (BB_END (e->src)))
3717 INSN_LOCATION (BB_END (e->src)) = e->goto_locus;
3718 continue;
3720 dest = e->dest;
3721 if (dest == EXIT_BLOCK_PTR)
3723 /* Non-fallthru edges to the exit block cannot be split. */
3724 if (!(e->flags & EDGE_FALLTHRU))
3725 continue;
3727 else
3729 insn = BB_HEAD (dest);
3730 end = NEXT_INSN (BB_END (dest));
3731 while (insn != end && !NONDEBUG_INSN_P (insn))
3732 insn = NEXT_INSN (insn);
3733 if (insn != end && INSN_HAS_LOCATION (insn)
3734 && INSN_LOCATION (insn) == e->goto_locus)
3735 continue;
3737 nb = split_edge (e);
3738 if (!INSN_P (BB_END (nb)))
3739 BB_END (nb) = emit_insn_after_noloc (gen_nop (), BB_END (nb),
3740 nb);
3741 INSN_LOCATION (BB_END (nb)) = e->goto_locus;
3743 /* If there are other incoming edges to the destination block
3744 with the same goto locus, redirect them to the new block as
3745 well, this can prevent other such blocks from being created
3746 in subsequent iterations of the loop. */
3747 for (ei2 = ei_start (dest->preds); (e2 = ei_safe_edge (ei2)); )
3748 if (LOCATION_LOCUS (e2->goto_locus) != UNKNOWN_LOCATION
3749 && !(e2->flags & (EDGE_ABNORMAL | EDGE_FALLTHRU))
3750 && e->goto_locus == e2->goto_locus)
3751 redirect_edge_and_branch (e2, nb);
3752 else
3753 ei_next (&ei2);
3758 /* Perform sanity checks on the insn chain.
3759 1. Check that next/prev pointers are consistent in both the forward and
3760 reverse direction.
3761 2. Count insns in chain, going both directions, and check if equal.
3762 3. Check that get_last_insn () returns the actual end of chain. */
3764 DEBUG_FUNCTION void
3765 verify_insn_chain (void)
3767 rtx x, prevx, nextx;
3768 int insn_cnt1, insn_cnt2;
3770 for (prevx = NULL, insn_cnt1 = 1, x = get_insns ();
3771 x != 0;
3772 prevx = x, insn_cnt1++, x = NEXT_INSN (x))
3773 gcc_assert (PREV_INSN (x) == prevx);
3775 gcc_assert (prevx == get_last_insn ());
3777 for (nextx = NULL, insn_cnt2 = 1, x = get_last_insn ();
3778 x != 0;
3779 nextx = x, insn_cnt2++, x = PREV_INSN (x))
3780 gcc_assert (NEXT_INSN (x) == nextx);
3782 gcc_assert (insn_cnt1 == insn_cnt2);
3785 /* If we have assembler epilogues, the block falling through to exit must
3786 be the last one in the reordered chain when we reach final. Ensure
3787 that this condition is met. */
3788 static void
3789 fixup_fallthru_exit_predecessor (void)
3791 edge e;
3792 basic_block bb = NULL;
3794 /* This transformation is not valid before reload, because we might
3795 separate a call from the instruction that copies the return
3796 value. */
3797 gcc_assert (reload_completed);
3799 e = find_fallthru_edge (EXIT_BLOCK_PTR->preds);
3800 if (e)
3801 bb = e->src;
3803 if (bb && bb->aux)
3805 basic_block c = ENTRY_BLOCK_PTR->next_bb;
3807 /* If the very first block is the one with the fall-through exit
3808 edge, we have to split that block. */
3809 if (c == bb)
3811 bb = split_block (bb, NULL)->dest;
3812 bb->aux = c->aux;
3813 c->aux = bb;
3814 BB_FOOTER (bb) = BB_FOOTER (c);
3815 BB_FOOTER (c) = NULL;
3818 while (c->aux != bb)
3819 c = (basic_block) c->aux;
3821 c->aux = bb->aux;
3822 while (c->aux)
3823 c = (basic_block) c->aux;
3825 c->aux = bb;
3826 bb->aux = NULL;
3830 /* In case there are more than one fallthru predecessors of exit, force that
3831 there is only one. */
3833 static void
3834 force_one_exit_fallthru (void)
3836 edge e, predecessor = NULL;
3837 bool more = false;
3838 edge_iterator ei;
3839 basic_block forwarder, bb;
3841 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
3842 if (e->flags & EDGE_FALLTHRU)
3844 if (predecessor == NULL)
3845 predecessor = e;
3846 else
3848 more = true;
3849 break;
3853 if (!more)
3854 return;
3856 /* Exit has several fallthru predecessors. Create a forwarder block for
3857 them. */
3858 forwarder = split_edge (predecessor);
3859 for (ei = ei_start (EXIT_BLOCK_PTR->preds); (e = ei_safe_edge (ei)); )
3861 if (e->src == forwarder
3862 || !(e->flags & EDGE_FALLTHRU))
3863 ei_next (&ei);
3864 else
3865 redirect_edge_and_branch_force (e, forwarder);
3868 /* Fix up the chain of blocks -- make FORWARDER immediately precede the
3869 exit block. */
3870 FOR_EACH_BB (bb)
3872 if (bb->aux == NULL && bb != forwarder)
3874 bb->aux = forwarder;
3875 break;
3880 /* Return true in case it is possible to duplicate the basic block BB. */
3882 static bool
3883 cfg_layout_can_duplicate_bb_p (const_basic_block bb)
3885 /* Do not attempt to duplicate tablejumps, as we need to unshare
3886 the dispatch table. This is difficult to do, as the instructions
3887 computing jump destination may be hoisted outside the basic block. */
3888 if (tablejump_p (BB_END (bb), NULL, NULL))
3889 return false;
3891 /* Do not duplicate blocks containing insns that can't be copied. */
3892 if (targetm.cannot_copy_insn_p)
3894 rtx insn = BB_HEAD (bb);
3895 while (1)
3897 if (INSN_P (insn) && targetm.cannot_copy_insn_p (insn))
3898 return false;
3899 if (insn == BB_END (bb))
3900 break;
3901 insn = NEXT_INSN (insn);
3905 return true;
3909 duplicate_insn_chain (rtx from, rtx to)
3911 rtx insn, next, last, copy;
3913 /* Avoid updating of boundaries of previous basic block. The
3914 note will get removed from insn stream in fixup. */
3915 last = emit_note (NOTE_INSN_DELETED);
3917 /* Create copy at the end of INSN chain. The chain will
3918 be reordered later. */
3919 for (insn = from; insn != NEXT_INSN (to); insn = NEXT_INSN (insn))
3921 switch (GET_CODE (insn))
3923 case DEBUG_INSN:
3924 /* Don't duplicate label debug insns. */
3925 if (TREE_CODE (INSN_VAR_LOCATION_DECL (insn)) == LABEL_DECL)
3926 break;
3927 /* FALLTHRU */
3928 case INSN:
3929 case CALL_INSN:
3930 case JUMP_INSN:
3931 copy = emit_copy_of_insn_after (insn, get_last_insn ());
3932 if (JUMP_P (insn) && JUMP_LABEL (insn) != NULL_RTX
3933 && ANY_RETURN_P (JUMP_LABEL (insn)))
3934 JUMP_LABEL (copy) = JUMP_LABEL (insn);
3935 maybe_copy_prologue_epilogue_insn (insn, copy);
3936 break;
3938 case JUMP_TABLE_DATA:
3939 /* Avoid copying of dispatch tables. We never duplicate
3940 tablejumps, so this can hit only in case the table got
3941 moved far from original jump.
3942 Avoid copying following barrier as well if any
3943 (and debug insns in between). */
3944 for (next = NEXT_INSN (insn);
3945 next != NEXT_INSN (to);
3946 next = NEXT_INSN (next))
3947 if (!DEBUG_INSN_P (next))
3948 break;
3949 if (next != NEXT_INSN (to) && BARRIER_P (next))
3950 insn = next;
3951 break;
3953 case CODE_LABEL:
3954 break;
3956 case BARRIER:
3957 emit_barrier ();
3958 break;
3960 case NOTE:
3961 switch (NOTE_KIND (insn))
3963 /* In case prologue is empty and function contain label
3964 in first BB, we may want to copy the block. */
3965 case NOTE_INSN_PROLOGUE_END:
3967 case NOTE_INSN_DELETED:
3968 case NOTE_INSN_DELETED_LABEL:
3969 case NOTE_INSN_DELETED_DEBUG_LABEL:
3970 /* No problem to strip these. */
3971 case NOTE_INSN_FUNCTION_BEG:
3972 /* There is always just single entry to function. */
3973 case NOTE_INSN_BASIC_BLOCK:
3974 /* We should only switch text sections once. */
3975 case NOTE_INSN_SWITCH_TEXT_SECTIONS:
3976 break;
3978 case NOTE_INSN_EPILOGUE_BEG:
3979 emit_note_copy (insn);
3980 break;
3982 default:
3983 /* All other notes should have already been eliminated. */
3984 gcc_unreachable ();
3986 break;
3987 default:
3988 gcc_unreachable ();
3991 insn = NEXT_INSN (last);
3992 delete_insn (last);
3993 return insn;
3996 /* Create a duplicate of the basic block BB. */
3998 static basic_block
3999 cfg_layout_duplicate_bb (basic_block bb)
4001 rtx insn;
4002 basic_block new_bb;
4004 insn = duplicate_insn_chain (BB_HEAD (bb), BB_END (bb));
4005 new_bb = create_basic_block (insn,
4006 insn ? get_last_insn () : NULL,
4007 EXIT_BLOCK_PTR->prev_bb);
4009 BB_COPY_PARTITION (new_bb, bb);
4010 if (BB_HEADER (bb))
4012 insn = BB_HEADER (bb);
4013 while (NEXT_INSN (insn))
4014 insn = NEXT_INSN (insn);
4015 insn = duplicate_insn_chain (BB_HEADER (bb), insn);
4016 if (insn)
4017 BB_HEADER (new_bb) = unlink_insn_chain (insn, get_last_insn ());
4020 if (BB_FOOTER (bb))
4022 insn = BB_FOOTER (bb);
4023 while (NEXT_INSN (insn))
4024 insn = NEXT_INSN (insn);
4025 insn = duplicate_insn_chain (BB_FOOTER (bb), insn);
4026 if (insn)
4027 BB_FOOTER (new_bb) = unlink_insn_chain (insn, get_last_insn ());
4030 return new_bb;
4034 /* Main entry point to this module - initialize the datastructures for
4035 CFG layout changes. It keeps LOOPS up-to-date if not null.
4037 FLAGS is a set of additional flags to pass to cleanup_cfg(). */
4039 void
4040 cfg_layout_initialize (unsigned int flags)
4042 rtx x;
4043 basic_block bb;
4045 initialize_original_copy_tables ();
4047 cfg_layout_rtl_register_cfg_hooks ();
4049 record_effective_endpoints ();
4051 /* Make sure that the targets of non local gotos are marked. */
4052 for (x = nonlocal_goto_handler_labels; x; x = XEXP (x, 1))
4054 bb = BLOCK_FOR_INSN (XEXP (x, 0));
4055 bb->flags |= BB_NON_LOCAL_GOTO_TARGET;
4058 cleanup_cfg (CLEANUP_CFGLAYOUT | flags);
4061 /* Splits superblocks. */
4062 void
4063 break_superblocks (void)
4065 sbitmap superblocks;
4066 bool need = false;
4067 basic_block bb;
4069 superblocks = sbitmap_alloc (last_basic_block);
4070 bitmap_clear (superblocks);
4072 FOR_EACH_BB (bb)
4073 if (bb->flags & BB_SUPERBLOCK)
4075 bb->flags &= ~BB_SUPERBLOCK;
4076 bitmap_set_bit (superblocks, bb->index);
4077 need = true;
4080 if (need)
4082 rebuild_jump_labels (get_insns ());
4083 find_many_sub_basic_blocks (superblocks);
4086 free (superblocks);
4089 /* Finalize the changes: reorder insn list according to the sequence specified
4090 by aux pointers, enter compensation code, rebuild scope forest. */
4092 void
4093 cfg_layout_finalize (void)
4095 #ifdef ENABLE_CHECKING
4096 verify_flow_info ();
4097 #endif
4098 force_one_exit_fallthru ();
4099 rtl_register_cfg_hooks ();
4100 if (reload_completed
4101 #ifdef HAVE_epilogue
4102 && !HAVE_epilogue
4103 #endif
4105 fixup_fallthru_exit_predecessor ();
4106 fixup_reorder_chain ();
4108 rebuild_jump_labels (get_insns ());
4109 delete_dead_jumptables ();
4111 #ifdef ENABLE_CHECKING
4112 verify_insn_chain ();
4113 verify_flow_info ();
4114 #endif
4118 /* Same as split_block but update cfg_layout structures. */
4120 static basic_block
4121 cfg_layout_split_block (basic_block bb, void *insnp)
4123 rtx insn = (rtx) insnp;
4124 basic_block new_bb = rtl_split_block (bb, insn);
4126 BB_FOOTER (new_bb) = BB_FOOTER (bb);
4127 BB_FOOTER (bb) = NULL;
4129 return new_bb;
4132 /* Redirect Edge to DEST. */
4133 static edge
4134 cfg_layout_redirect_edge_and_branch (edge e, basic_block dest)
4136 basic_block src = e->src;
4137 edge ret;
4139 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
4140 return NULL;
4142 if (e->dest == dest)
4143 return e;
4145 if (e->src != ENTRY_BLOCK_PTR
4146 && (ret = try_redirect_by_replacing_jump (e, dest, true)))
4148 df_set_bb_dirty (src);
4149 return ret;
4152 if (e->src == ENTRY_BLOCK_PTR
4153 && (e->flags & EDGE_FALLTHRU) && !(e->flags & EDGE_COMPLEX))
4155 if (dump_file)
4156 fprintf (dump_file, "Redirecting entry edge from bb %i to %i\n",
4157 e->src->index, dest->index);
4159 df_set_bb_dirty (e->src);
4160 redirect_edge_succ (e, dest);
4161 return e;
4164 /* Redirect_edge_and_branch may decide to turn branch into fallthru edge
4165 in the case the basic block appears to be in sequence. Avoid this
4166 transformation. */
4168 if (e->flags & EDGE_FALLTHRU)
4170 /* Redirect any branch edges unified with the fallthru one. */
4171 if (JUMP_P (BB_END (src))
4172 && label_is_jump_target_p (BB_HEAD (e->dest),
4173 BB_END (src)))
4175 edge redirected;
4177 if (dump_file)
4178 fprintf (dump_file, "Fallthru edge unified with branch "
4179 "%i->%i redirected to %i\n",
4180 e->src->index, e->dest->index, dest->index);
4181 e->flags &= ~EDGE_FALLTHRU;
4182 redirected = redirect_branch_edge (e, dest);
4183 gcc_assert (redirected);
4184 redirected->flags |= EDGE_FALLTHRU;
4185 df_set_bb_dirty (redirected->src);
4186 return redirected;
4188 /* In case we are redirecting fallthru edge to the branch edge
4189 of conditional jump, remove it. */
4190 if (EDGE_COUNT (src->succs) == 2)
4192 /* Find the edge that is different from E. */
4193 edge s = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e);
4195 if (s->dest == dest
4196 && any_condjump_p (BB_END (src))
4197 && onlyjump_p (BB_END (src)))
4198 delete_insn (BB_END (src));
4200 if (dump_file)
4201 fprintf (dump_file, "Redirecting fallthru edge %i->%i to %i\n",
4202 e->src->index, e->dest->index, dest->index);
4203 ret = redirect_edge_succ_nodup (e, dest);
4205 else
4206 ret = redirect_branch_edge (e, dest);
4208 /* We don't want simplejumps in the insn stream during cfglayout. */
4209 gcc_assert (!simplejump_p (BB_END (src)));
4211 df_set_bb_dirty (src);
4212 return ret;
4215 /* Simple wrapper as we always can redirect fallthru edges. */
4216 static basic_block
4217 cfg_layout_redirect_edge_and_branch_force (edge e, basic_block dest)
4219 edge redirected = cfg_layout_redirect_edge_and_branch (e, dest);
4221 gcc_assert (redirected);
4222 return NULL;
4225 /* Same as delete_basic_block but update cfg_layout structures. */
4227 static void
4228 cfg_layout_delete_block (basic_block bb)
4230 rtx insn, next, prev = PREV_INSN (BB_HEAD (bb)), *to, remaints;
4232 if (BB_HEADER (bb))
4234 next = BB_HEAD (bb);
4235 if (prev)
4236 NEXT_INSN (prev) = BB_HEADER (bb);
4237 else
4238 set_first_insn (BB_HEADER (bb));
4239 PREV_INSN (BB_HEADER (bb)) = prev;
4240 insn = BB_HEADER (bb);
4241 while (NEXT_INSN (insn))
4242 insn = NEXT_INSN (insn);
4243 NEXT_INSN (insn) = next;
4244 PREV_INSN (next) = insn;
4246 next = NEXT_INSN (BB_END (bb));
4247 if (BB_FOOTER (bb))
4249 insn = BB_FOOTER (bb);
4250 while (insn)
4252 if (BARRIER_P (insn))
4254 if (PREV_INSN (insn))
4255 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
4256 else
4257 BB_FOOTER (bb) = NEXT_INSN (insn);
4258 if (NEXT_INSN (insn))
4259 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
4261 if (LABEL_P (insn))
4262 break;
4263 insn = NEXT_INSN (insn);
4265 if (BB_FOOTER (bb))
4267 insn = BB_END (bb);
4268 NEXT_INSN (insn) = BB_FOOTER (bb);
4269 PREV_INSN (BB_FOOTER (bb)) = insn;
4270 while (NEXT_INSN (insn))
4271 insn = NEXT_INSN (insn);
4272 NEXT_INSN (insn) = next;
4273 if (next)
4274 PREV_INSN (next) = insn;
4275 else
4276 set_last_insn (insn);
4279 if (bb->next_bb != EXIT_BLOCK_PTR)
4280 to = &BB_HEADER (bb->next_bb);
4281 else
4282 to = &cfg_layout_function_footer;
4284 rtl_delete_block (bb);
4286 if (prev)
4287 prev = NEXT_INSN (prev);
4288 else
4289 prev = get_insns ();
4290 if (next)
4291 next = PREV_INSN (next);
4292 else
4293 next = get_last_insn ();
4295 if (next && NEXT_INSN (next) != prev)
4297 remaints = unlink_insn_chain (prev, next);
4298 insn = remaints;
4299 while (NEXT_INSN (insn))
4300 insn = NEXT_INSN (insn);
4301 NEXT_INSN (insn) = *to;
4302 if (*to)
4303 PREV_INSN (*to) = insn;
4304 *to = remaints;
4308 /* Return true when blocks A and B can be safely merged. */
4310 static bool
4311 cfg_layout_can_merge_blocks_p (basic_block a, basic_block b)
4313 /* If we are partitioning hot/cold basic blocks, we don't want to
4314 mess up unconditional or indirect jumps that cross between hot
4315 and cold sections.
4317 Basic block partitioning may result in some jumps that appear to
4318 be optimizable (or blocks that appear to be mergeable), but which really
4319 must be left untouched (they are required to make it safely across
4320 partition boundaries). See the comments at the top of
4321 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
4323 if (BB_PARTITION (a) != BB_PARTITION (b))
4324 return false;
4326 /* Protect the loop latches. */
4327 if (current_loops && b->loop_father->latch == b)
4328 return false;
4330 /* If we would end up moving B's instructions, make sure it doesn't fall
4331 through into the exit block, since we cannot recover from a fallthrough
4332 edge into the exit block occurring in the middle of a function. */
4333 if (NEXT_INSN (BB_END (a)) != BB_HEAD (b))
4335 edge e = find_fallthru_edge (b->succs);
4336 if (e && e->dest == EXIT_BLOCK_PTR)
4337 return false;
4340 /* There must be exactly one edge in between the blocks. */
4341 return (single_succ_p (a)
4342 && single_succ (a) == b
4343 && single_pred_p (b) == 1
4344 && a != b
4345 /* Must be simple edge. */
4346 && !(single_succ_edge (a)->flags & EDGE_COMPLEX)
4347 && a != ENTRY_BLOCK_PTR && b != EXIT_BLOCK_PTR
4348 /* If the jump insn has side effects, we can't kill the edge.
4349 When not optimizing, try_redirect_by_replacing_jump will
4350 not allow us to redirect an edge by replacing a table jump. */
4351 && (!JUMP_P (BB_END (a))
4352 || ((!optimize || reload_completed)
4353 ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
4356 /* Merge block A and B. The blocks must be mergeable. */
4358 static void
4359 cfg_layout_merge_blocks (basic_block a, basic_block b)
4361 bool forwarder_p = (b->flags & BB_FORWARDER_BLOCK) != 0;
4362 rtx insn;
4364 gcc_checking_assert (cfg_layout_can_merge_blocks_p (a, b));
4366 if (dump_file)
4367 fprintf (dump_file, "Merging block %d into block %d...\n", b->index,
4368 a->index);
4370 /* If there was a CODE_LABEL beginning B, delete it. */
4371 if (LABEL_P (BB_HEAD (b)))
4373 delete_insn (BB_HEAD (b));
4376 /* We should have fallthru edge in a, or we can do dummy redirection to get
4377 it cleaned up. */
4378 if (JUMP_P (BB_END (a)))
4379 try_redirect_by_replacing_jump (EDGE_SUCC (a, 0), b, true);
4380 gcc_assert (!JUMP_P (BB_END (a)));
4382 /* When not optimizing CFG and the edge is the only place in RTL which holds
4383 some unique locus, emit a nop with that locus in between. */
4384 if (!optimize)
4385 emit_nop_for_unique_locus_between (a, b);
4387 /* Move things from b->footer after a->footer. */
4388 if (BB_FOOTER (b))
4390 if (!BB_FOOTER (a))
4391 BB_FOOTER (a) = BB_FOOTER (b);
4392 else
4394 rtx last = BB_FOOTER (a);
4396 while (NEXT_INSN (last))
4397 last = NEXT_INSN (last);
4398 NEXT_INSN (last) = BB_FOOTER (b);
4399 PREV_INSN (BB_FOOTER (b)) = last;
4401 BB_FOOTER (b) = NULL;
4404 /* Move things from b->header before a->footer.
4405 Note that this may include dead tablejump data, but we don't clean
4406 those up until we go out of cfglayout mode. */
4407 if (BB_HEADER (b))
4409 if (! BB_FOOTER (a))
4410 BB_FOOTER (a) = BB_HEADER (b);
4411 else
4413 rtx last = BB_HEADER (b);
4415 while (NEXT_INSN (last))
4416 last = NEXT_INSN (last);
4417 NEXT_INSN (last) = BB_FOOTER (a);
4418 PREV_INSN (BB_FOOTER (a)) = last;
4419 BB_FOOTER (a) = BB_HEADER (b);
4421 BB_HEADER (b) = NULL;
4424 /* In the case basic blocks are not adjacent, move them around. */
4425 if (NEXT_INSN (BB_END (a)) != BB_HEAD (b))
4427 insn = unlink_insn_chain (BB_HEAD (b), BB_END (b));
4429 emit_insn_after_noloc (insn, BB_END (a), a);
4431 /* Otherwise just re-associate the instructions. */
4432 else
4434 insn = BB_HEAD (b);
4435 BB_END (a) = BB_END (b);
4438 /* emit_insn_after_noloc doesn't call df_insn_change_bb.
4439 We need to explicitly call. */
4440 update_bb_for_insn_chain (insn, BB_END (b), a);
4442 /* Skip possible DELETED_LABEL insn. */
4443 if (!NOTE_INSN_BASIC_BLOCK_P (insn))
4444 insn = NEXT_INSN (insn);
4445 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
4446 BB_HEAD (b) = BB_END (b) = NULL;
4447 delete_insn (insn);
4449 df_bb_delete (b->index);
4451 /* If B was a forwarder block, propagate the locus on the edge. */
4452 if (forwarder_p
4453 && LOCATION_LOCUS (EDGE_SUCC (b, 0)->goto_locus) == UNKNOWN_LOCATION)
4454 EDGE_SUCC (b, 0)->goto_locus = EDGE_SUCC (a, 0)->goto_locus;
4456 if (dump_file)
4457 fprintf (dump_file, "Merged blocks %d and %d.\n", a->index, b->index);
4460 /* Split edge E. */
4462 static basic_block
4463 cfg_layout_split_edge (edge e)
4465 basic_block new_bb =
4466 create_basic_block (e->src != ENTRY_BLOCK_PTR
4467 ? NEXT_INSN (BB_END (e->src)) : get_insns (),
4468 NULL_RTX, e->src);
4470 if (e->dest == EXIT_BLOCK_PTR)
4471 BB_COPY_PARTITION (new_bb, e->src);
4472 else
4473 BB_COPY_PARTITION (new_bb, e->dest);
4474 make_edge (new_bb, e->dest, EDGE_FALLTHRU);
4475 redirect_edge_and_branch_force (e, new_bb);
4477 return new_bb;
4480 /* Do postprocessing after making a forwarder block joined by edge FALLTHRU. */
4482 static void
4483 rtl_make_forwarder_block (edge fallthru ATTRIBUTE_UNUSED)
4487 /* Return true if BB contains only labels or non-executable
4488 instructions. */
4490 static bool
4491 rtl_block_empty_p (basic_block bb)
4493 rtx insn;
4495 if (bb == ENTRY_BLOCK_PTR || bb == EXIT_BLOCK_PTR)
4496 return true;
4498 FOR_BB_INSNS (bb, insn)
4499 if (NONDEBUG_INSN_P (insn) && !any_uncondjump_p (insn))
4500 return false;
4502 return true;
4505 /* Split a basic block if it ends with a conditional branch and if
4506 the other part of the block is not empty. */
4508 static basic_block
4509 rtl_split_block_before_cond_jump (basic_block bb)
4511 rtx insn;
4512 rtx split_point = NULL;
4513 rtx last = NULL;
4514 bool found_code = false;
4516 FOR_BB_INSNS (bb, insn)
4518 if (any_condjump_p (insn))
4519 split_point = last;
4520 else if (NONDEBUG_INSN_P (insn))
4521 found_code = true;
4522 last = insn;
4525 /* Did not find everything. */
4526 if (found_code && split_point)
4527 return split_block (bb, split_point)->dest;
4528 else
4529 return NULL;
4532 /* Return 1 if BB ends with a call, possibly followed by some
4533 instructions that must stay with the call, 0 otherwise. */
4535 static bool
4536 rtl_block_ends_with_call_p (basic_block bb)
4538 rtx insn = BB_END (bb);
4540 while (!CALL_P (insn)
4541 && insn != BB_HEAD (bb)
4542 && (keep_with_call_p (insn)
4543 || NOTE_P (insn)
4544 || DEBUG_INSN_P (insn)))
4545 insn = PREV_INSN (insn);
4546 return (CALL_P (insn));
4549 /* Return 1 if BB ends with a conditional branch, 0 otherwise. */
4551 static bool
4552 rtl_block_ends_with_condjump_p (const_basic_block bb)
4554 return any_condjump_p (BB_END (bb));
4557 /* Return true if we need to add fake edge to exit.
4558 Helper function for rtl_flow_call_edges_add. */
4560 static bool
4561 need_fake_edge_p (const_rtx insn)
4563 if (!INSN_P (insn))
4564 return false;
4566 if ((CALL_P (insn)
4567 && !SIBLING_CALL_P (insn)
4568 && !find_reg_note (insn, REG_NORETURN, NULL)
4569 && !(RTL_CONST_OR_PURE_CALL_P (insn))))
4570 return true;
4572 return ((GET_CODE (PATTERN (insn)) == ASM_OPERANDS
4573 && MEM_VOLATILE_P (PATTERN (insn)))
4574 || (GET_CODE (PATTERN (insn)) == PARALLEL
4575 && asm_noperands (insn) != -1
4576 && MEM_VOLATILE_P (XVECEXP (PATTERN (insn), 0, 0)))
4577 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
4580 /* Add fake edges to the function exit for any non constant and non noreturn
4581 calls, volatile inline assembly in the bitmap of blocks specified by
4582 BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
4583 that were split.
4585 The goal is to expose cases in which entering a basic block does not imply
4586 that all subsequent instructions must be executed. */
4588 static int
4589 rtl_flow_call_edges_add (sbitmap blocks)
4591 int i;
4592 int blocks_split = 0;
4593 int last_bb = last_basic_block;
4594 bool check_last_block = false;
4596 if (n_basic_blocks == NUM_FIXED_BLOCKS)
4597 return 0;
4599 if (! blocks)
4600 check_last_block = true;
4601 else
4602 check_last_block = bitmap_bit_p (blocks, EXIT_BLOCK_PTR->prev_bb->index);
4604 /* In the last basic block, before epilogue generation, there will be
4605 a fallthru edge to EXIT. Special care is required if the last insn
4606 of the last basic block is a call because make_edge folds duplicate
4607 edges, which would result in the fallthru edge also being marked
4608 fake, which would result in the fallthru edge being removed by
4609 remove_fake_edges, which would result in an invalid CFG.
4611 Moreover, we can't elide the outgoing fake edge, since the block
4612 profiler needs to take this into account in order to solve the minimal
4613 spanning tree in the case that the call doesn't return.
4615 Handle this by adding a dummy instruction in a new last basic block. */
4616 if (check_last_block)
4618 basic_block bb = EXIT_BLOCK_PTR->prev_bb;
4619 rtx insn = BB_END (bb);
4621 /* Back up past insns that must be kept in the same block as a call. */
4622 while (insn != BB_HEAD (bb)
4623 && keep_with_call_p (insn))
4624 insn = PREV_INSN (insn);
4626 if (need_fake_edge_p (insn))
4628 edge e;
4630 e = find_edge (bb, EXIT_BLOCK_PTR);
4631 if (e)
4633 insert_insn_on_edge (gen_use (const0_rtx), e);
4634 commit_edge_insertions ();
4639 /* Now add fake edges to the function exit for any non constant
4640 calls since there is no way that we can determine if they will
4641 return or not... */
4643 for (i = NUM_FIXED_BLOCKS; i < last_bb; i++)
4645 basic_block bb = BASIC_BLOCK (i);
4646 rtx insn;
4647 rtx prev_insn;
4649 if (!bb)
4650 continue;
4652 if (blocks && !bitmap_bit_p (blocks, i))
4653 continue;
4655 for (insn = BB_END (bb); ; insn = prev_insn)
4657 prev_insn = PREV_INSN (insn);
4658 if (need_fake_edge_p (insn))
4660 edge e;
4661 rtx split_at_insn = insn;
4663 /* Don't split the block between a call and an insn that should
4664 remain in the same block as the call. */
4665 if (CALL_P (insn))
4666 while (split_at_insn != BB_END (bb)
4667 && keep_with_call_p (NEXT_INSN (split_at_insn)))
4668 split_at_insn = NEXT_INSN (split_at_insn);
4670 /* The handling above of the final block before the epilogue
4671 should be enough to verify that there is no edge to the exit
4672 block in CFG already. Calling make_edge in such case would
4673 cause us to mark that edge as fake and remove it later. */
4675 #ifdef ENABLE_CHECKING
4676 if (split_at_insn == BB_END (bb))
4678 e = find_edge (bb, EXIT_BLOCK_PTR);
4679 gcc_assert (e == NULL);
4681 #endif
4683 /* Note that the following may create a new basic block
4684 and renumber the existing basic blocks. */
4685 if (split_at_insn != BB_END (bb))
4687 e = split_block (bb, split_at_insn);
4688 if (e)
4689 blocks_split++;
4692 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
4695 if (insn == BB_HEAD (bb))
4696 break;
4700 if (blocks_split)
4701 verify_flow_info ();
4703 return blocks_split;
4706 /* Add COMP_RTX as a condition at end of COND_BB. FIRST_HEAD is
4707 the conditional branch target, SECOND_HEAD should be the fall-thru
4708 there is no need to handle this here the loop versioning code handles
4709 this. the reason for SECON_HEAD is that it is needed for condition
4710 in trees, and this should be of the same type since it is a hook. */
4711 static void
4712 rtl_lv_add_condition_to_bb (basic_block first_head ,
4713 basic_block second_head ATTRIBUTE_UNUSED,
4714 basic_block cond_bb, void *comp_rtx)
4716 rtx label, seq, jump;
4717 rtx op0 = XEXP ((rtx)comp_rtx, 0);
4718 rtx op1 = XEXP ((rtx)comp_rtx, 1);
4719 enum rtx_code comp = GET_CODE ((rtx)comp_rtx);
4720 enum machine_mode mode;
4723 label = block_label (first_head);
4724 mode = GET_MODE (op0);
4725 if (mode == VOIDmode)
4726 mode = GET_MODE (op1);
4728 start_sequence ();
4729 op0 = force_operand (op0, NULL_RTX);
4730 op1 = force_operand (op1, NULL_RTX);
4731 do_compare_rtx_and_jump (op0, op1, comp, 0,
4732 mode, NULL_RTX, NULL_RTX, label, -1);
4733 jump = get_last_insn ();
4734 JUMP_LABEL (jump) = label;
4735 LABEL_NUSES (label)++;
4736 seq = get_insns ();
4737 end_sequence ();
4739 /* Add the new cond , in the new head. */
4740 emit_insn_after(seq, BB_END(cond_bb));
4744 /* Given a block B with unconditional branch at its end, get the
4745 store the return the branch edge and the fall-thru edge in
4746 BRANCH_EDGE and FALLTHRU_EDGE respectively. */
4747 static void
4748 rtl_extract_cond_bb_edges (basic_block b, edge *branch_edge,
4749 edge *fallthru_edge)
4751 edge e = EDGE_SUCC (b, 0);
4753 if (e->flags & EDGE_FALLTHRU)
4755 *fallthru_edge = e;
4756 *branch_edge = EDGE_SUCC (b, 1);
4758 else
4760 *branch_edge = e;
4761 *fallthru_edge = EDGE_SUCC (b, 1);
4765 void
4766 init_rtl_bb_info (basic_block bb)
4768 gcc_assert (!bb->il.x.rtl);
4769 bb->il.x.head_ = NULL;
4770 bb->il.x.rtl = ggc_alloc_cleared_rtl_bb_info ();
4773 /* Returns true if it is possible to remove edge E by redirecting
4774 it to the destination of the other edge from E->src. */
4776 static bool
4777 rtl_can_remove_branch_p (const_edge e)
4779 const_basic_block src = e->src;
4780 const_basic_block target = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest;
4781 const_rtx insn = BB_END (src), set;
4783 /* The conditions are taken from try_redirect_by_replacing_jump. */
4784 if (target == EXIT_BLOCK_PTR)
4785 return false;
4787 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
4788 return false;
4790 if (BB_PARTITION (src) != BB_PARTITION (target))
4791 return false;
4793 if (!onlyjump_p (insn)
4794 || tablejump_p (insn, NULL, NULL))
4795 return false;
4797 set = single_set (insn);
4798 if (!set || side_effects_p (set))
4799 return false;
4801 return true;
4804 static basic_block
4805 rtl_duplicate_bb (basic_block bb)
4807 bb = cfg_layout_duplicate_bb (bb);
4808 bb->aux = NULL;
4809 return bb;
4812 /* Do book-keeping of basic block BB for the profile consistency checker.
4813 If AFTER_PASS is 0, do pre-pass accounting, or if AFTER_PASS is 1
4814 then do post-pass accounting. Store the counting in RECORD. */
4815 static void
4816 rtl_account_profile_record (basic_block bb, int after_pass,
4817 struct profile_record *record)
4819 rtx insn;
4820 FOR_BB_INSNS (bb, insn)
4821 if (INSN_P (insn))
4823 record->size[after_pass]
4824 += insn_rtx_cost (PATTERN (insn), false);
4825 if (profile_status == PROFILE_READ)
4826 record->time[after_pass]
4827 += insn_rtx_cost (PATTERN (insn), true) * bb->count;
4828 else if (profile_status == PROFILE_GUESSED)
4829 record->time[after_pass]
4830 += insn_rtx_cost (PATTERN (insn), true) * bb->frequency;
4834 /* Implementation of CFG manipulation for linearized RTL. */
4835 struct cfg_hooks rtl_cfg_hooks = {
4836 "rtl",
4837 rtl_verify_flow_info,
4838 rtl_dump_bb,
4839 rtl_dump_bb_for_graph,
4840 rtl_create_basic_block,
4841 rtl_redirect_edge_and_branch,
4842 rtl_redirect_edge_and_branch_force,
4843 rtl_can_remove_branch_p,
4844 rtl_delete_block,
4845 rtl_split_block,
4846 rtl_move_block_after,
4847 rtl_can_merge_blocks, /* can_merge_blocks_p */
4848 rtl_merge_blocks,
4849 rtl_predict_edge,
4850 rtl_predicted_by_p,
4851 cfg_layout_can_duplicate_bb_p,
4852 rtl_duplicate_bb,
4853 rtl_split_edge,
4854 rtl_make_forwarder_block,
4855 rtl_tidy_fallthru_edge,
4856 rtl_force_nonfallthru,
4857 rtl_block_ends_with_call_p,
4858 rtl_block_ends_with_condjump_p,
4859 rtl_flow_call_edges_add,
4860 NULL, /* execute_on_growing_pred */
4861 NULL, /* execute_on_shrinking_pred */
4862 NULL, /* duplicate loop for trees */
4863 NULL, /* lv_add_condition_to_bb */
4864 NULL, /* lv_adjust_loop_header_phi*/
4865 NULL, /* extract_cond_bb_edges */
4866 NULL, /* flush_pending_stmts */
4867 rtl_block_empty_p, /* block_empty_p */
4868 rtl_split_block_before_cond_jump, /* split_block_before_cond_jump */
4869 rtl_account_profile_record,
4872 /* Implementation of CFG manipulation for cfg layout RTL, where
4873 basic block connected via fallthru edges does not have to be adjacent.
4874 This representation will hopefully become the default one in future
4875 version of the compiler. */
4877 struct cfg_hooks cfg_layout_rtl_cfg_hooks = {
4878 "cfglayout mode",
4879 rtl_verify_flow_info_1,
4880 rtl_dump_bb,
4881 rtl_dump_bb_for_graph,
4882 cfg_layout_create_basic_block,
4883 cfg_layout_redirect_edge_and_branch,
4884 cfg_layout_redirect_edge_and_branch_force,
4885 rtl_can_remove_branch_p,
4886 cfg_layout_delete_block,
4887 cfg_layout_split_block,
4888 rtl_move_block_after,
4889 cfg_layout_can_merge_blocks_p,
4890 cfg_layout_merge_blocks,
4891 rtl_predict_edge,
4892 rtl_predicted_by_p,
4893 cfg_layout_can_duplicate_bb_p,
4894 cfg_layout_duplicate_bb,
4895 cfg_layout_split_edge,
4896 rtl_make_forwarder_block,
4897 NULL, /* tidy_fallthru_edge */
4898 rtl_force_nonfallthru,
4899 rtl_block_ends_with_call_p,
4900 rtl_block_ends_with_condjump_p,
4901 rtl_flow_call_edges_add,
4902 NULL, /* execute_on_growing_pred */
4903 NULL, /* execute_on_shrinking_pred */
4904 duplicate_loop_to_header_edge, /* duplicate loop for trees */
4905 rtl_lv_add_condition_to_bb, /* lv_add_condition_to_bb */
4906 NULL, /* lv_adjust_loop_header_phi*/
4907 rtl_extract_cond_bb_edges, /* extract_cond_bb_edges */
4908 NULL, /* flush_pending_stmts */
4909 rtl_block_empty_p, /* block_empty_p */
4910 rtl_split_block_before_cond_jump, /* split_block_before_cond_jump */
4911 rtl_account_profile_record,
4914 #include "gt-cfgrtl.h"