+#ifdef HAVE_return
+/* Insert use of return register before the end of BB. */
+
+static void
+emit_use_return_register_into_block (basic_block bb)
+{
+ rtx seq;
+ start_sequence ();
+ use_return_register ();
+ seq = get_insns ();
+ end_sequence ();
+ emit_insn_before (seq, BB_END (bb));
+}
+
+
+/* Create a return pattern, either simple_return or return, depending on
+ simple_p. */
+
+static rtx
+gen_return_pattern (bool simple_p)
+{
+#ifdef HAVE_simple_return
+ return simple_p ? gen_simple_return () : gen_return ();
+#else
+ gcc_assert (!simple_p);
+ return gen_return ();
+#endif
+}
+
+/* Insert an appropriate return pattern at the end of block BB. This
+ also means updating block_for_insn appropriately. SIMPLE_P is
+ the same as in gen_return_pattern and passed to it. */
+
+static void
+emit_return_into_block (bool simple_p, basic_block bb)
+{
+ rtx jump, pat;
+ jump = emit_jump_insn_after (gen_return_pattern (simple_p), BB_END (bb));
+ pat = PATTERN (jump);
+ if (GET_CODE (pat) == PARALLEL)
+ pat = XVECEXP (pat, 0, 0);
+ gcc_assert (ANY_RETURN_P (pat));
+ JUMP_LABEL (jump) = pat;
+}
+#endif
+
+/* Set JUMP_LABEL for a return insn. */
+
+void
+set_return_jump_label (rtx returnjump)
+{
+ rtx pat = PATTERN (returnjump);
+ if (GET_CODE (pat) == PARALLEL)
+ pat = XVECEXP (pat, 0, 0);
+ if (ANY_RETURN_P (pat))
+ JUMP_LABEL (returnjump) = pat;
+ else
+ JUMP_LABEL (returnjump) = ret_rtx;
+}
+
+#ifdef HAVE_simple_return
+/* Create a copy of BB instructions and insert at BEFORE. Redirect
+ preds of BB to COPY_BB if they don't appear in NEED_PROLOGUE. */
+static void
+dup_block_and_redirect (basic_block bb, basic_block copy_bb, rtx before,
+ bitmap_head *need_prologue)
+{
+ edge_iterator ei;
+ edge e;
+ rtx insn = BB_END (bb);
+
+ /* We know BB has a single successor, so there is no need to copy a
+ simple jump at the end of BB. */
+ if (simplejump_p (insn))
+ insn = PREV_INSN (insn);
+
+ start_sequence ();
+ duplicate_insn_chain (BB_HEAD (bb), insn);
+ if (dump_file)
+ {
+ unsigned count = 0;
+ for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
+ if (active_insn_p (insn))
+ ++count;
+ fprintf (dump_file, "Duplicating bb %d to bb %d, %u active insns.\n",
+ bb->index, copy_bb->index, count);
+ }
+ insn = get_insns ();
+ end_sequence ();
+ emit_insn_before (insn, before);
+
+ /* Redirect all the paths that need no prologue into copy_bb. */
+ for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
+ if (!bitmap_bit_p (need_prologue, e->src->index))
+ {
+ redirect_edge_and_branch_force (e, copy_bb);
+ continue;
+ }
+ else
+ ei_next (&ei);
+}
+#endif
+
+#if defined (HAVE_return) || defined (HAVE_simple_return)
+/* Return true if there are any active insns between HEAD and TAIL. */
+static bool
+active_insn_between (rtx head, rtx tail)
+{
+ while (tail)
+ {
+ if (active_insn_p (tail))
+ return true;
+ if (tail == head)
+ return false;
+ tail = PREV_INSN (tail);
+ }
+ return false;
+}
+
+/* LAST_BB is a block that exits, and empty of active instructions.
+ Examine its predecessors for jumps that can be converted to
+ (conditional) returns. */
+static VEC (edge, heap) *
+convert_jumps_to_returns (basic_block last_bb, bool simple_p,
+ VEC (edge, heap) *unconverted ATTRIBUTE_UNUSED)
+{
+ int i;
+ basic_block bb;
+ rtx label;
+ edge_iterator ei;
+ edge e;
+ VEC(basic_block,heap) *src_bbs;
+
+ src_bbs = VEC_alloc (basic_block, heap, EDGE_COUNT (last_bb->preds));
+ FOR_EACH_EDGE (e, ei, last_bb->preds)
+ if (e->src != ENTRY_BLOCK_PTR)
+ VEC_quick_push (basic_block, src_bbs, e->src);
+
+ label = BB_HEAD (last_bb);
+
+ FOR_EACH_VEC_ELT (basic_block, src_bbs, i, bb)
+ {
+ rtx jump = BB_END (bb);
+
+ if (!JUMP_P (jump) || JUMP_LABEL (jump) != label)
+ continue;
+
+ e = find_edge (bb, last_bb);
+
+ /* If we have an unconditional jump, we can replace that
+ with a simple return instruction. */
+ if (simplejump_p (jump))
+ {
+ /* The use of the return register might be present in the exit
+ fallthru block. Either:
+ - removing the use is safe, and we should remove the use in
+ the exit fallthru block, or
+ - removing the use is not safe, and we should add it here.
+ For now, we conservatively choose the latter. Either of the
+ 2 helps in crossjumping. */
+ emit_use_return_register_into_block (bb);
+
+ emit_return_into_block (simple_p, bb);
+ delete_insn (jump);
+ }
+
+ /* If we have a conditional jump branching to the last
+ block, we can try to replace that with a conditional
+ return instruction. */
+ else if (condjump_p (jump))
+ {
+ rtx dest;
+
+ if (simple_p)
+ dest = simple_return_rtx;
+ else
+ dest = ret_rtx;
+ if (!redirect_jump (jump, dest, 0))
+ {
+#ifdef HAVE_simple_return
+ if (simple_p)
+ {
+ if (dump_file)
+ fprintf (dump_file,
+ "Failed to redirect bb %d branch.\n", bb->index);
+ VEC_safe_push (edge, heap, unconverted, e);
+ }
+#endif
+ continue;
+ }
+
+ /* See comment in simplejump_p case above. */
+ emit_use_return_register_into_block (bb);
+
+ /* If this block has only one successor, it both jumps
+ and falls through to the fallthru block, so we can't
+ delete the edge. */
+ if (single_succ_p (bb))
+ continue;
+ }
+ else
+ {
+#ifdef HAVE_simple_return
+ if (simple_p)
+ {
+ if (dump_file)
+ fprintf (dump_file,
+ "Failed to redirect bb %d branch.\n", bb->index);
+ VEC_safe_push (edge, heap, unconverted, e);
+ }
+#endif
+ continue;
+ }
+
+ /* Fix up the CFG for the successful change we just made. */
+ redirect_edge_succ (e, EXIT_BLOCK_PTR);
+ e->flags &= ~EDGE_CROSSING;
+ }
+ VEC_free (basic_block, heap, src_bbs);
+ return unconverted;
+}
+
+/* Emit a return insn for the exit fallthru block. */
+static basic_block
+emit_return_for_exit (edge exit_fallthru_edge, bool simple_p)
+{
+ basic_block last_bb = exit_fallthru_edge->src;
+
+ if (JUMP_P (BB_END (last_bb)))
+ {
+ last_bb = split_edge (exit_fallthru_edge);
+ exit_fallthru_edge = single_succ_edge (last_bb);
+ }
+ emit_barrier_after (BB_END (last_bb));
+ emit_return_into_block (simple_p, last_bb);
+ exit_fallthru_edge->flags &= ~EDGE_FALLTHRU;
+ return last_bb;
+}
+#endif
+
+
+/* Generate the prologue and epilogue RTL if the machine supports it. Thread
+ this into place with notes indicating where the prologue ends and where
+ the epilogue begins. Update the basic block information when possible.
+
+ Notes on epilogue placement:
+ There are several kinds of edges to the exit block:
+ * a single fallthru edge from LAST_BB
+ * possibly, edges from blocks containing sibcalls
+ * possibly, fake edges from infinite loops
+
+ The epilogue is always emitted on the fallthru edge from the last basic
+ block in the function, LAST_BB, into the exit block.
+
+ If LAST_BB is empty except for a label, it is the target of every
+ other basic block in the function that ends in a return. If a
+ target has a return or simple_return pattern (possibly with
+ conditional variants), these basic blocks can be changed so that a
+ return insn is emitted into them, and their target is adjusted to
+ the real exit block.
+
+ Notes on shrink wrapping: We implement a fairly conservative
+ version of shrink-wrapping rather than the textbook one. We only
+ generate a single prologue and a single epilogue. This is
+ sufficient to catch a number of interesting cases involving early
+ exits.
+
+ First, we identify the blocks that require the prologue to occur before
+ them. These are the ones that modify a call-saved register, or reference
+ any of the stack or frame pointer registers. To simplify things, we then
+ mark everything reachable from these blocks as also requiring a prologue.
+ This takes care of loops automatically, and avoids the need to examine
+ whether MEMs reference the frame, since it is sufficient to check for
+ occurrences of the stack or frame pointer.
+
+ We then compute the set of blocks for which the need for a prologue
+ is anticipatable (borrowing terminology from the shrink-wrapping
+ description in Muchnick's book). These are the blocks which either
+ require a prologue themselves, or those that have only successors
+ where the prologue is anticipatable. The prologue needs to be
+ inserted on all edges from BB1->BB2 where BB2 is in ANTIC and BB1
+ is not. For the moment, we ensure that only one such edge exists.
+
+ The epilogue is placed as described above, but we make a
+ distinction between inserting return and simple_return patterns
+ when modifying other blocks that end in a return. Blocks that end
+ in a sibcall omit the sibcall_epilogue if the block is not in
+ ANTIC. */
+
+static void
+thread_prologue_and_epilogue_insns (void)
+{
+ bool inserted;
+#ifdef HAVE_simple_return
+ VEC (edge, heap) *unconverted_simple_returns = NULL;
+ bool nonempty_prologue;
+ bitmap_head bb_flags;
+ unsigned max_grow_size;
+#endif
+ rtx returnjump;
+ rtx seq ATTRIBUTE_UNUSED, epilogue_end ATTRIBUTE_UNUSED;
+ rtx prologue_seq ATTRIBUTE_UNUSED, split_prologue_seq ATTRIBUTE_UNUSED;
+ edge e, entry_edge, orig_entry_edge, exit_fallthru_edge;
+ edge_iterator ei;
+
+ df_analyze ();
+
+ rtl_profile_for_bb (ENTRY_BLOCK_PTR);
+
+ inserted = false;
+ seq = NULL_RTX;
+ epilogue_end = NULL_RTX;
+ returnjump = NULL_RTX;
+
+ /* Can't deal with multiple successors of the entry block at the
+ moment. Function should always have at least one entry
+ point. */
+ gcc_assert (single_succ_p (ENTRY_BLOCK_PTR));
+ entry_edge = single_succ_edge (ENTRY_BLOCK_PTR);
+ orig_entry_edge = entry_edge;
+
+ split_prologue_seq = NULL_RTX;
+ if (flag_split_stack
+ && (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (cfun->decl))
+ == NULL))
+ {
+#ifndef HAVE_split_stack_prologue
+ gcc_unreachable ();
+#else
+ gcc_assert (HAVE_split_stack_prologue);
+
+ start_sequence ();
+ emit_insn (gen_split_stack_prologue ());
+ split_prologue_seq = get_insns ();
+ end_sequence ();
+
+ record_insns (split_prologue_seq, NULL, &prologue_insn_hash);
+ set_insn_locators (split_prologue_seq, prologue_locator);
+#endif
+ }
+
+ prologue_seq = NULL_RTX;
+#ifdef HAVE_prologue
+ if (HAVE_prologue)
+ {
+ start_sequence ();
+ seq = gen_prologue ();
+ emit_insn (seq);
+
+ /* Insert an explicit USE for the frame pointer
+ if the profiling is on and the frame pointer is required. */
+ if (crtl->profile && frame_pointer_needed)
+ emit_use (hard_frame_pointer_rtx);
+
+ /* Retain a map of the prologue insns. */
+ record_insns (seq, NULL, &prologue_insn_hash);
+ emit_note (NOTE_INSN_PROLOGUE_END);
+
+ /* Ensure that instructions are not moved into the prologue when
+ profiling is on. The call to the profiling routine can be
+ emitted within the live range of a call-clobbered register. */
+ if (!targetm.profile_before_prologue () && crtl->profile)
+ emit_insn (gen_blockage ());
+
+ prologue_seq = get_insns ();
+ end_sequence ();
+ set_insn_locators (prologue_seq, prologue_locator);
+ }
+#endif
+
+#ifdef HAVE_simple_return
+ bitmap_initialize (&bb_flags, &bitmap_default_obstack);
+
+ /* Try to perform a kind of shrink-wrapping, making sure the
+ prologue/epilogue is emitted only around those parts of the
+ function that require it. */
+
+ nonempty_prologue = false;
+ for (seq = prologue_seq; seq; seq = NEXT_INSN (seq))
+ if (!NOTE_P (seq) || NOTE_KIND (seq) != NOTE_INSN_PROLOGUE_END)
+ {
+ nonempty_prologue = true;
+ break;
+ }
+
+ if (flag_shrink_wrap && HAVE_simple_return
+ && (targetm.profile_before_prologue () || !crtl->profile)
+ && nonempty_prologue && !crtl->calls_eh_return)
+ {
+ HARD_REG_SET prologue_clobbered, prologue_used, live_on_edge;
+ struct hard_reg_set_container set_up_by_prologue;
+ rtx p_insn;
+ VEC(basic_block, heap) *vec;
+ basic_block bb;
+ bitmap_head bb_antic_flags;
+ bitmap_head bb_on_list;
+ bitmap_head bb_tail;
+
+ if (dump_file)
+ fprintf (dump_file, "Attempting shrink-wrapping optimization.\n");
+
+ /* Compute the registers set and used in the prologue. */
+ CLEAR_HARD_REG_SET (prologue_clobbered);
+ CLEAR_HARD_REG_SET (prologue_used);
+ for (p_insn = prologue_seq; p_insn; p_insn = NEXT_INSN (p_insn))
+ {
+ HARD_REG_SET this_used;
+ if (!NONDEBUG_INSN_P (p_insn))
+ continue;
+
+ CLEAR_HARD_REG_SET (this_used);
+ note_uses (&PATTERN (p_insn), record_hard_reg_uses,
+ &this_used);
+ AND_COMPL_HARD_REG_SET (this_used, prologue_clobbered);
+ IOR_HARD_REG_SET (prologue_used, this_used);
+ note_stores (PATTERN (p_insn), record_hard_reg_sets,
+ &prologue_clobbered);
+ }
+
+ prepare_shrink_wrap (entry_edge->dest);
+
+ bitmap_initialize (&bb_antic_flags, &bitmap_default_obstack);
+ bitmap_initialize (&bb_on_list, &bitmap_default_obstack);
+ bitmap_initialize (&bb_tail, &bitmap_default_obstack);
+
+ /* Find the set of basic blocks that require a stack frame,
+ and blocks that are too big to be duplicated. */
+
+ vec = VEC_alloc (basic_block, heap, n_basic_blocks);
+
+ CLEAR_HARD_REG_SET (set_up_by_prologue.set);
+ add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
+ STACK_POINTER_REGNUM);
+ add_to_hard_reg_set (&set_up_by_prologue.set, Pmode, ARG_POINTER_REGNUM);
+ if (frame_pointer_needed)
+ add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
+ HARD_FRAME_POINTER_REGNUM);
+ if (pic_offset_table_rtx)
+ add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
+ PIC_OFFSET_TABLE_REGNUM);
+ if (crtl->drap_reg)
+ add_to_hard_reg_set (&set_up_by_prologue.set,
+ GET_MODE (crtl->drap_reg),
+ REGNO (crtl->drap_reg));
+ if (targetm.set_up_by_prologue)
+ targetm.set_up_by_prologue (&set_up_by_prologue);
+
+ /* We don't use a different max size depending on
+ optimize_bb_for_speed_p because increasing shrink-wrapping
+ opportunities by duplicating tail blocks can actually result
+ in an overall decrease in code size. */
+ max_grow_size = get_uncond_jump_length ();
+ max_grow_size *= PARAM_VALUE (PARAM_MAX_GROW_COPY_BB_INSNS);
+
+ FOR_EACH_BB (bb)
+ {
+ rtx insn;
+ unsigned size = 0;
+
+ FOR_BB_INSNS (bb, insn)
+ if (NONDEBUG_INSN_P (insn))
+ {
+ if (requires_stack_frame_p (insn, prologue_used,
+ set_up_by_prologue.set))
+ {
+ if (bb == entry_edge->dest)
+ goto fail_shrinkwrap;
+ bitmap_set_bit (&bb_flags, bb->index);
+ VEC_quick_push (basic_block, vec, bb);
+ break;
+ }
+ else if (size <= max_grow_size)
+ {
+ size += get_attr_min_length (insn);
+ if (size > max_grow_size)
+ bitmap_set_bit (&bb_on_list, bb->index);
+ }
+ }
+ }
+
+ /* Blocks that really need a prologue, or are too big for tails. */
+ bitmap_ior_into (&bb_on_list, &bb_flags);
+
+ /* For every basic block that needs a prologue, mark all blocks
+ reachable from it, so as to ensure they are also seen as
+ requiring a prologue. */
+ while (!VEC_empty (basic_block, vec))
+ {
+ basic_block tmp_bb = VEC_pop (basic_block, vec);
+
+ FOR_EACH_EDGE (e, ei, tmp_bb->succs)
+ if (e->dest != EXIT_BLOCK_PTR
+ && bitmap_set_bit (&bb_flags, e->dest->index))
+ VEC_quick_push (basic_block, vec, e->dest);
+ }
+
+ /* Find the set of basic blocks that need no prologue, have a
+ single successor, can be duplicated, meet a max size
+ requirement, and go to the exit via like blocks. */
+ VEC_quick_push (basic_block, vec, EXIT_BLOCK_PTR);
+ while (!VEC_empty (basic_block, vec))
+ {
+ basic_block tmp_bb = VEC_pop (basic_block, vec);
+
+ FOR_EACH_EDGE (e, ei, tmp_bb->preds)
+ if (single_succ_p (e->src)
+ && !bitmap_bit_p (&bb_on_list, e->src->index)
+ && can_duplicate_block_p (e->src))
+ {
+ edge pe;
+ edge_iterator pei;
+
+ /* If there is predecessor of e->src which doesn't
+ need prologue and the edge is complex,
+ we might not be able to redirect the branch
+ to a copy of e->src. */
+ FOR_EACH_EDGE (pe, pei, e->src->preds)
+ if ((pe->flags & EDGE_COMPLEX) != 0
+ && !bitmap_bit_p (&bb_flags, pe->src->index))
+ break;
+ if (pe == NULL && bitmap_set_bit (&bb_tail, e->src->index))
+ VEC_quick_push (basic_block, vec, e->src);
+ }
+ }
+
+ /* Now walk backwards from every block that is marked as needing
+ a prologue to compute the bb_antic_flags bitmap. Exclude
+ tail blocks; They can be duplicated to be used on paths not
+ needing a prologue. */
+ bitmap_clear (&bb_on_list);
+ bitmap_and_compl (&bb_antic_flags, &bb_flags, &bb_tail);
+ FOR_EACH_BB (bb)
+ {
+ if (!bitmap_bit_p (&bb_antic_flags, bb->index))
+ continue;
+ FOR_EACH_EDGE (e, ei, bb->preds)
+ if (!bitmap_bit_p (&bb_antic_flags, e->src->index)
+ && bitmap_set_bit (&bb_on_list, e->src->index))
+ VEC_quick_push (basic_block, vec, e->src);
+ }
+ while (!VEC_empty (basic_block, vec))
+ {
+ basic_block tmp_bb = VEC_pop (basic_block, vec);
+ bool all_set = true;
+
+ bitmap_clear_bit (&bb_on_list, tmp_bb->index);
+ FOR_EACH_EDGE (e, ei, tmp_bb->succs)
+ if (!bitmap_bit_p (&bb_antic_flags, e->dest->index))
+ {
+ all_set = false;
+ break;
+ }
+
+ if (all_set)
+ {
+ bitmap_set_bit (&bb_antic_flags, tmp_bb->index);
+ FOR_EACH_EDGE (e, ei, tmp_bb->preds)
+ if (!bitmap_bit_p (&bb_antic_flags, e->src->index)
+ && bitmap_set_bit (&bb_on_list, e->src->index))
+ VEC_quick_push (basic_block, vec, e->src);
+ }
+ }
+ /* Find exactly one edge that leads to a block in ANTIC from
+ a block that isn't. */
+ if (!bitmap_bit_p (&bb_antic_flags, entry_edge->dest->index))
+ FOR_EACH_BB (bb)
+ {
+ if (!bitmap_bit_p (&bb_antic_flags, bb->index))
+ continue;
+ FOR_EACH_EDGE (e, ei, bb->preds)
+ if (!bitmap_bit_p (&bb_antic_flags, e->src->index))
+ {
+ if (entry_edge != orig_entry_edge)
+ {
+ entry_edge = orig_entry_edge;
+ if (dump_file)
+ fprintf (dump_file, "More than one candidate edge.\n");
+ goto fail_shrinkwrap;
+ }
+ if (dump_file)
+ fprintf (dump_file, "Found candidate edge for "
+ "shrink-wrapping, %d->%d.\n", e->src->index,
+ e->dest->index);
+ entry_edge = e;
+ }
+ }
+
+ if (entry_edge != orig_entry_edge)
+ {
+ /* Test whether the prologue is known to clobber any register
+ (other than FP or SP) which are live on the edge. */
+ CLEAR_HARD_REG_BIT (prologue_clobbered, STACK_POINTER_REGNUM);
+ if (frame_pointer_needed)
+ CLEAR_HARD_REG_BIT (prologue_clobbered, HARD_FRAME_POINTER_REGNUM);
+ CLEAR_HARD_REG_SET (live_on_edge);
+ reg_set_to_hard_reg_set (&live_on_edge,
+ df_get_live_in (entry_edge->dest));
+ if (hard_reg_set_intersect_p (live_on_edge, prologue_clobbered))
+ {
+ entry_edge = orig_entry_edge;
+ if (dump_file)
+ fprintf (dump_file,
+ "Shrink-wrapping aborted due to clobber.\n");
+ }
+ }
+ if (entry_edge != orig_entry_edge)
+ {
+ crtl->shrink_wrapped = true;
+ if (dump_file)
+ fprintf (dump_file, "Performing shrink-wrapping.\n");
+
+ /* Find tail blocks reachable from both blocks needing a
+ prologue and blocks not needing a prologue. */
+ if (!bitmap_empty_p (&bb_tail))
+ FOR_EACH_BB (bb)
+ {
+ bool some_pro, some_no_pro;
+ if (!bitmap_bit_p (&bb_tail, bb->index))
+ continue;
+ some_pro = some_no_pro = false;
+ FOR_EACH_EDGE (e, ei, bb->preds)
+ {
+ if (bitmap_bit_p (&bb_flags, e->src->index))
+ some_pro = true;
+ else
+ some_no_pro = true;
+ }
+ if (some_pro && some_no_pro)
+ VEC_quick_push (basic_block, vec, bb);
+ else
+ bitmap_clear_bit (&bb_tail, bb->index);
+ }
+ /* Find the head of each tail. */
+ while (!VEC_empty (basic_block, vec))
+ {
+ basic_block tbb = VEC_pop (basic_block, vec);
+
+ if (!bitmap_bit_p (&bb_tail, tbb->index))
+ continue;
+
+ while (single_succ_p (tbb))
+ {
+ tbb = single_succ (tbb);
+ bitmap_clear_bit (&bb_tail, tbb->index);
+ }
+ }
+ /* Now duplicate the tails. */
+ if (!bitmap_empty_p (&bb_tail))
+ FOR_EACH_BB_REVERSE (bb)
+ {
+ basic_block copy_bb, tbb;
+ rtx insert_point;
+ int eflags;
+
+ if (!bitmap_clear_bit (&bb_tail, bb->index))
+ continue;
+
+ /* Create a copy of BB, instructions and all, for
+ use on paths that don't need a prologue.
+ Ideal placement of the copy is on a fall-thru edge
+ or after a block that would jump to the copy. */
+ FOR_EACH_EDGE (e, ei, bb->preds)
+ if (!bitmap_bit_p (&bb_flags, e->src->index)
+ && single_succ_p (e->src))
+ break;
+ if (e)
+ {
+ copy_bb = create_basic_block (NEXT_INSN (BB_END (e->src)),
+ NULL_RTX, e->src);
+ BB_COPY_PARTITION (copy_bb, e->src);
+ }
+ else
+ {
+ /* Otherwise put the copy at the end of the function. */
+ copy_bb = create_basic_block (NULL_RTX, NULL_RTX,
+ EXIT_BLOCK_PTR->prev_bb);
+ BB_COPY_PARTITION (copy_bb, bb);
+ }
+
+ insert_point = emit_note_after (NOTE_INSN_DELETED,
+ BB_END (copy_bb));
+ emit_barrier_after (BB_END (copy_bb));
+
+ tbb = bb;
+ while (1)
+ {
+ dup_block_and_redirect (tbb, copy_bb, insert_point,
+ &bb_flags);
+ tbb = single_succ (tbb);
+ if (tbb == EXIT_BLOCK_PTR)
+ break;
+ e = split_block (copy_bb, PREV_INSN (insert_point));
+ copy_bb = e->dest;
+ }
+
+ /* Quiet verify_flow_info by (ab)using EDGE_FAKE.
+ We have yet to add a simple_return to the tails,
+ as we'd like to first convert_jumps_to_returns in
+ case the block is no longer used after that. */
+ eflags = EDGE_FAKE;
+ if (CALL_P (PREV_INSN (insert_point))
+ && SIBLING_CALL_P (PREV_INSN (insert_point)))
+ eflags = EDGE_SIBCALL | EDGE_ABNORMAL;
+ make_single_succ_edge (copy_bb, EXIT_BLOCK_PTR, eflags);
+
+ /* verify_flow_info doesn't like a note after a
+ sibling call. */
+ delete_insn (insert_point);
+ if (bitmap_empty_p (&bb_tail))
+ break;
+ }
+ }
+
+ fail_shrinkwrap:
+ bitmap_clear (&bb_tail);
+ bitmap_clear (&bb_antic_flags);
+ bitmap_clear (&bb_on_list);
+ VEC_free (basic_block, heap, vec);
+ }
+#endif
+
+ if (split_prologue_seq != NULL_RTX)
+ {
+ insert_insn_on_edge (split_prologue_seq, orig_entry_edge);
+ inserted = true;
+ }
+ if (prologue_seq != NULL_RTX)
+ {
+ insert_insn_on_edge (prologue_seq, entry_edge);
+ inserted = true;
+ }
+
+ /* If the exit block has no non-fake predecessors, we don't need
+ an epilogue. */
+ FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
+ if ((e->flags & EDGE_FAKE) == 0)
+ break;
+ if (e == NULL)
+ goto epilogue_done;
+
+ rtl_profile_for_bb (EXIT_BLOCK_PTR);
+
+ exit_fallthru_edge = find_fallthru_edge (EXIT_BLOCK_PTR->preds);
+
+ /* If we're allowed to generate a simple return instruction, then by
+ definition we don't need a full epilogue. If the last basic
+ block before the exit block does not contain active instructions,
+ examine its predecessors and try to emit (conditional) return
+ instructions. */
+#ifdef HAVE_simple_return
+ if (entry_edge != orig_entry_edge)
+ {
+ if (optimize)
+ {
+ unsigned i, last;
+
+ /* convert_jumps_to_returns may add to EXIT_BLOCK_PTR->preds
+ (but won't remove). Stop at end of current preds. */
+ last = EDGE_COUNT (EXIT_BLOCK_PTR->preds);
+ for (i = 0; i < last; i++)
+ {
+ e = EDGE_I (EXIT_BLOCK_PTR->preds, i);
+ if (LABEL_P (BB_HEAD (e->src))
+ && !bitmap_bit_p (&bb_flags, e->src->index)
+ && !active_insn_between (BB_HEAD (e->src), BB_END (e->src)))
+ unconverted_simple_returns
+ = convert_jumps_to_returns (e->src, true,
+ unconverted_simple_returns);
+ }
+ }
+
+ if (exit_fallthru_edge != NULL
+ && EDGE_COUNT (exit_fallthru_edge->src->preds) != 0
+ && !bitmap_bit_p (&bb_flags, exit_fallthru_edge->src->index))
+ {
+ basic_block last_bb;
+
+ last_bb = emit_return_for_exit (exit_fallthru_edge, true);
+ returnjump = BB_END (last_bb);
+ exit_fallthru_edge = NULL;
+ }
+ }
+#endif
+#ifdef HAVE_return
+ if (HAVE_return)
+ {
+ if (exit_fallthru_edge == NULL)
+ goto epilogue_done;
+
+ if (optimize)
+ {
+ basic_block last_bb = exit_fallthru_edge->src;
+
+ if (LABEL_P (BB_HEAD (last_bb))
+ && !active_insn_between (BB_HEAD (last_bb), BB_END (last_bb)))
+ convert_jumps_to_returns (last_bb, false, NULL);
+
+ if (EDGE_COUNT (last_bb->preds) != 0
+ && single_succ_p (last_bb))
+ {
+ last_bb = emit_return_for_exit (exit_fallthru_edge, false);
+ epilogue_end = returnjump = BB_END (last_bb);
+#ifdef HAVE_simple_return
+ /* Emitting the return may add a basic block.
+ Fix bb_flags for the added block. */
+ if (last_bb != exit_fallthru_edge->src)
+ bitmap_set_bit (&bb_flags, last_bb->index);
+#endif
+ goto epilogue_done;
+ }
+ }
+ }
+#endif
+
+ /* A small fib -- epilogue is not yet completed, but we wish to re-use
+ this marker for the splits of EH_RETURN patterns, and nothing else