1 /* Shrink-wrapping related optimizations.
2 Copyright (C) 1987-2014 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
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
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 handles shrink-wrapping related optimizations. */
24 #include "coretypes.h"
26 #include "rtl-error.h"
28 #include "stor-layout.h"
30 #include "stringpool.h"
37 #include "hard-reg-set.h"
44 #include "insn-config.h"
48 #include "langhooks.h"
50 #include "common/common-target.h"
51 #include "gimple-expr.h"
53 #include "tree-pass.h"
57 #include "bb-reorder.h"
58 #include "shrink-wrap.h"
62 #ifdef HAVE_simple_return
64 /* Return true if INSN requires the stack frame to be set up.
65 PROLOGUE_USED contains the hard registers used in the function
66 prologue. SET_UP_BY_PROLOGUE is the set of registers we expect the
67 prologue to set up for the function. */
69 requires_stack_frame_p (rtx_insn
*insn
, HARD_REG_SET prologue_used
,
70 HARD_REG_SET set_up_by_prologue
)
73 HARD_REG_SET hardregs
;
77 return !SIBLING_CALL_P (insn
);
79 /* We need a frame to get the unique CFA expected by the unwinder. */
80 if (cfun
->can_throw_non_call_exceptions
&& can_throw_internal (insn
))
83 CLEAR_HARD_REG_SET (hardregs
);
84 FOR_EACH_INSN_DEF (def
, insn
)
86 rtx dreg
= DF_REF_REG (def
);
91 add_to_hard_reg_set (&hardregs
, GET_MODE (dreg
),
94 if (hard_reg_set_intersect_p (hardregs
, prologue_used
))
96 AND_COMPL_HARD_REG_SET (hardregs
, call_used_reg_set
);
97 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
98 if (TEST_HARD_REG_BIT (hardregs
, regno
)
99 && df_regs_ever_live_p (regno
))
102 FOR_EACH_INSN_USE (use
, insn
)
104 rtx reg
= DF_REF_REG (use
);
109 add_to_hard_reg_set (&hardregs
, GET_MODE (reg
),
112 if (hard_reg_set_intersect_p (hardregs
, set_up_by_prologue
))
118 /* See whether there has a single live edge from BB, which dest uses
119 [REGNO, END_REGNO). Return the live edge if its dest bb has
120 one or two predecessors. Otherwise return NULL. */
123 live_edge_for_reg (basic_block bb
, int regno
, int end_regno
)
131 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
133 live
= df_get_live_in (e
->dest
);
134 for (i
= regno
; i
< end_regno
; i
++)
135 if (REGNO_REG_SET_P (live
, i
))
137 if (live_edge
&& live_edge
!= e
)
143 /* We can sometimes encounter dead code. Don't try to move it
144 into the exit block. */
145 if (!live_edge
|| live_edge
->dest
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
148 /* Reject targets of abnormal edges. This is needed for correctness
149 on ports like Alpha and MIPS, whose pic_offset_table_rtx can die on
150 exception edges even though it is generally treated as call-saved
151 for the majority of the compilation. Moving across abnormal edges
152 isn't going to be interesting for shrink-wrap usage anyway. */
153 if (live_edge
->flags
& EDGE_ABNORMAL
)
156 /* When live_edge->dest->preds == 2, we can create a new block on
157 the edge to make it meet the requirement. */
158 if (EDGE_COUNT (live_edge
->dest
->preds
) > 2)
164 /* Try to move INSN from BB to a successor. Return true on success.
165 USES and DEFS are the set of registers that are used and defined
166 after INSN in BB. SPLIT_P indicates whether a live edge from BB
167 is splitted or not. */
170 move_insn_for_shrink_wrap (basic_block bb
, rtx_insn
*insn
,
171 const HARD_REG_SET uses
,
172 const HARD_REG_SET defs
,
176 bitmap live_out
, live_in
, bb_uses
, bb_defs
;
177 unsigned int i
, dregno
, end_dregno
;
178 unsigned int sregno
= FIRST_PSEUDO_REGISTER
;
179 unsigned int end_sregno
= FIRST_PSEUDO_REGISTER
;
180 basic_block next_block
;
183 /* Look for a simple register assignment. We don't use single_set here
184 because we can't deal with any CLOBBERs, USEs, or REG_UNUSED secondary
188 set
= PATTERN (insn
);
189 if (GET_CODE (set
) != SET
)
192 dest
= SET_DEST (set
);
194 /* For the destination, we want only a register. Also disallow STACK
195 or FRAME related adjustments. They are likely part of the prologue,
196 so keep them in the entry block. */
198 || dest
== stack_pointer_rtx
199 || dest
== frame_pointer_rtx
200 || dest
== hard_frame_pointer_rtx
)
203 /* For the source, we want one of:
204 (1) A (non-overlapping) register
206 (3) An expression involving no more than one register.
208 That last point comes from the code following, which was originally
209 written to handle only register move operations, and still only handles
210 a single source register when checking for overlaps. Happily, the
211 same checks can be applied to expressions like (plus reg const). */
213 if (CONSTANT_P (src
))
215 else if (!REG_P (src
))
217 rtx src_inner
= NULL_RTX
;
219 if (can_throw_internal (insn
))
222 subrtx_var_iterator::array_type array
;
223 FOR_EACH_SUBRTX_VAR (iter
, array
, src
, ALL
)
226 switch (GET_RTX_CLASS (GET_CODE (x
)))
230 case RTX_COMM_COMPARE
:
235 /* Constant or expression. Continue. */
240 switch (GET_CODE (x
))
244 case STRICT_LOW_PART
:
250 /* Fail if we see a second inner register. */
251 if (src_inner
!= NULL
)
266 if (src_inner
!= NULL
)
270 /* Make sure that the source register isn't defined later in BB. */
273 sregno
= REGNO (src
);
274 end_sregno
= END_REGNO (src
);
275 if (overlaps_hard_reg_set_p (defs
, GET_MODE (src
), sregno
))
279 /* Make sure that the destination register isn't referenced later in BB. */
280 dregno
= REGNO (dest
);
281 end_dregno
= END_REGNO (dest
);
282 if (overlaps_hard_reg_set_p (uses
, GET_MODE (dest
), dregno
)
283 || overlaps_hard_reg_set_p (defs
, GET_MODE (dest
), dregno
))
286 /* See whether there is a successor block to which we could move INSN. */
287 live_edge
= live_edge_for_reg (bb
, dregno
, end_dregno
);
291 next_block
= live_edge
->dest
;
292 /* Create a new basic block on the edge. */
293 if (EDGE_COUNT (next_block
->preds
) == 2)
295 /* split_edge for a block with only one successor is meaningless. */
296 if (EDGE_COUNT (bb
->succs
) == 1)
299 /* If DF_LIVE doesn't exist, i.e. at -O1, just give up. */
303 basic_block old_dest
= live_edge
->dest
;
304 next_block
= split_edge (live_edge
);
306 /* We create a new basic block. Call df_grow_bb_info to make sure
307 all data structures are allocated. */
308 df_grow_bb_info (df_live
);
310 bitmap_and (df_get_live_in (next_block
), df_get_live_out (bb
),
311 df_get_live_in (old_dest
));
312 df_set_bb_dirty (next_block
);
314 /* We should not split more than once for a function. */
321 /* At this point we are committed to moving INSN, but let's try to
322 move it as far as we can. */
325 live_out
= df_get_live_out (bb
);
326 live_in
= df_get_live_in (next_block
);
329 /* Check whether BB uses DEST or clobbers DEST. We need to add
330 INSN to BB if so. Either way, DEST is no longer live on entry,
331 except for any part that overlaps SRC (next loop). */
332 bb_uses
= &DF_LR_BB_INFO (bb
)->use
;
333 bb_defs
= &DF_LR_BB_INFO (bb
)->def
;
336 for (i
= dregno
; i
< end_dregno
; i
++)
339 || REGNO_REG_SET_P (bb_uses
, i
)
340 || REGNO_REG_SET_P (bb_defs
, i
)
341 || REGNO_REG_SET_P (&DF_LIVE_BB_INFO (bb
)->gen
, i
))
343 CLEAR_REGNO_REG_SET (live_out
, i
);
344 CLEAR_REGNO_REG_SET (live_in
, i
);
347 /* Check whether BB clobbers SRC. We need to add INSN to BB if so.
348 Either way, SRC is now live on entry. */
349 for (i
= sregno
; i
< end_sregno
; i
++)
352 || REGNO_REG_SET_P (bb_defs
, i
)
353 || REGNO_REG_SET_P (&DF_LIVE_BB_INFO (bb
)->gen
, i
))
355 SET_REGNO_REG_SET (live_out
, i
);
356 SET_REGNO_REG_SET (live_in
, i
);
361 /* DF_LR_BB_INFO (bb)->def does not comprise the DF_REF_PARTIAL and
362 DF_REF_CONDITIONAL defs. So if DF_LIVE doesn't exist, i.e.
363 at -O1, just give up searching NEXT_BLOCK. */
365 for (i
= dregno
; i
< end_dregno
; i
++)
367 CLEAR_REGNO_REG_SET (live_out
, i
);
368 CLEAR_REGNO_REG_SET (live_in
, i
);
371 for (i
= sregno
; i
< end_sregno
; i
++)
373 SET_REGNO_REG_SET (live_out
, i
);
374 SET_REGNO_REG_SET (live_in
, i
);
378 /* If we don't need to add the move to BB, look for a single
382 live_edge
= live_edge_for_reg (next_block
, dregno
, end_dregno
);
383 if (!live_edge
|| EDGE_COUNT (live_edge
->dest
->preds
) > 1)
385 next_block
= live_edge
->dest
;
390 /* For the new created basic block, there is no dataflow info at all.
391 So skip the following dataflow update and check. */
394 /* BB now defines DEST. It only uses the parts of DEST that overlap SRC
396 for (i
= dregno
; i
< end_dregno
; i
++)
398 CLEAR_REGNO_REG_SET (bb_uses
, i
);
399 SET_REGNO_REG_SET (bb_defs
, i
);
402 /* BB now uses SRC. */
403 for (i
= sregno
; i
< end_sregno
; i
++)
404 SET_REGNO_REG_SET (bb_uses
, i
);
407 emit_insn_after (PATTERN (insn
), bb_note (bb
));
412 /* Look for register copies in the first block of the function, and move
413 them down into successor blocks if the register is used only on one
414 path. This exposes more opportunities for shrink-wrapping. These
415 kinds of sets often occur when incoming argument registers are moved
416 to call-saved registers because their values are live across one or
417 more calls during the function. */
420 prepare_shrink_wrap (basic_block entry_block
)
422 rtx_insn
*insn
, *curr
;
424 HARD_REG_SET uses
, defs
;
426 bool split_p
= false;
428 if (JUMP_P (BB_END (entry_block
)))
430 /* To have more shrink-wrapping opportunities, prepare_shrink_wrap tries
431 to sink the copies from parameter to callee saved register out of
432 entry block. copyprop_hardreg_forward_bb_without_debug_insn is called
433 to release some dependences. */
434 copyprop_hardreg_forward_bb_without_debug_insn (entry_block
);
437 CLEAR_HARD_REG_SET (uses
);
438 CLEAR_HARD_REG_SET (defs
);
439 FOR_BB_INSNS_REVERSE_SAFE (entry_block
, insn
, curr
)
440 if (NONDEBUG_INSN_P (insn
)
441 && !move_insn_for_shrink_wrap (entry_block
, insn
, uses
, defs
,
444 /* Add all defined registers to DEFs. */
445 FOR_EACH_INSN_DEF (def
, insn
)
447 x
= DF_REF_REG (def
);
448 if (REG_P (x
) && HARD_REGISTER_P (x
))
449 SET_HARD_REG_BIT (defs
, REGNO (x
));
452 /* Add all used registers to USESs. */
453 FOR_EACH_INSN_USE (use
, insn
)
455 x
= DF_REF_REG (use
);
456 if (REG_P (x
) && HARD_REGISTER_P (x
))
457 SET_HARD_REG_BIT (uses
, REGNO (x
));
462 /* Create a copy of BB instructions and insert at BEFORE. Redirect
463 preds of BB to COPY_BB if they don't appear in NEED_PROLOGUE. */
465 dup_block_and_redirect (basic_block bb
, basic_block copy_bb
, rtx_insn
*before
,
466 bitmap_head
*need_prologue
)
470 rtx_insn
*insn
= BB_END (bb
);
472 /* We know BB has a single successor, so there is no need to copy a
473 simple jump at the end of BB. */
474 if (simplejump_p (insn
))
475 insn
= PREV_INSN (insn
);
478 duplicate_insn_chain (BB_HEAD (bb
), insn
);
482 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
483 if (active_insn_p (insn
))
485 fprintf (dump_file
, "Duplicating bb %d to bb %d, %u active insns.\n",
486 bb
->index
, copy_bb
->index
, count
);
490 emit_insn_before (insn
, before
);
492 /* Redirect all the paths that need no prologue into copy_bb. */
493 for (ei
= ei_start (bb
->preds
); (e
= ei_safe_edge (ei
));)
494 if (!bitmap_bit_p (need_prologue
, e
->src
->index
))
496 int freq
= EDGE_FREQUENCY (e
);
497 copy_bb
->count
+= e
->count
;
498 copy_bb
->frequency
+= EDGE_FREQUENCY (e
);
499 e
->dest
->count
-= e
->count
;
500 if (e
->dest
->count
< 0)
502 e
->dest
->frequency
-= freq
;
503 if (e
->dest
->frequency
< 0)
504 e
->dest
->frequency
= 0;
505 redirect_edge_and_branch_force (e
, copy_bb
);
513 /* Try to perform a kind of shrink-wrapping, making sure the
514 prologue/epilogue is emitted only around those parts of the
515 function that require it. */
518 try_shrink_wrapping (edge
*entry_edge
, edge orig_entry_edge
,
519 bitmap_head
*bb_flags
, rtx_insn
*prologue_seq
)
523 bool nonempty_prologue
= false;
524 unsigned max_grow_size
;
527 for (seq
= prologue_seq
; seq
; seq
= NEXT_INSN (seq
))
528 if (!NOTE_P (seq
) || NOTE_KIND (seq
) != NOTE_INSN_PROLOGUE_END
)
530 nonempty_prologue
= true;
534 if (flag_shrink_wrap
&& HAVE_simple_return
535 && (targetm
.profile_before_prologue () || !crtl
->profile
)
536 && nonempty_prologue
&& !crtl
->calls_eh_return
)
538 HARD_REG_SET prologue_clobbered
, prologue_used
, live_on_edge
;
539 struct hard_reg_set_container set_up_by_prologue
;
541 vec
<basic_block
> vec
;
543 bitmap_head bb_antic_flags
;
544 bitmap_head bb_on_list
;
548 fprintf (dump_file
, "Attempting shrink-wrapping optimization.\n");
550 /* Compute the registers set and used in the prologue. */
551 CLEAR_HARD_REG_SET (prologue_clobbered
);
552 CLEAR_HARD_REG_SET (prologue_used
);
553 for (p_insn
= prologue_seq
; p_insn
; p_insn
= NEXT_INSN (p_insn
))
555 HARD_REG_SET this_used
;
556 if (!NONDEBUG_INSN_P (p_insn
))
559 CLEAR_HARD_REG_SET (this_used
);
560 note_uses (&PATTERN (p_insn
), record_hard_reg_uses
,
562 AND_COMPL_HARD_REG_SET (this_used
, prologue_clobbered
);
563 IOR_HARD_REG_SET (prologue_used
, this_used
);
564 note_stores (PATTERN (p_insn
), record_hard_reg_sets
,
565 &prologue_clobbered
);
568 prepare_shrink_wrap ((*entry_edge
)->dest
);
570 bitmap_initialize (&bb_antic_flags
, &bitmap_default_obstack
);
571 bitmap_initialize (&bb_on_list
, &bitmap_default_obstack
);
572 bitmap_initialize (&bb_tail
, &bitmap_default_obstack
);
574 /* Find the set of basic blocks that require a stack frame,
575 and blocks that are too big to be duplicated. */
577 vec
.create (n_basic_blocks_for_fn (cfun
));
579 CLEAR_HARD_REG_SET (set_up_by_prologue
.set
);
580 add_to_hard_reg_set (&set_up_by_prologue
.set
, Pmode
,
581 STACK_POINTER_REGNUM
);
582 add_to_hard_reg_set (&set_up_by_prologue
.set
, Pmode
, ARG_POINTER_REGNUM
);
583 if (frame_pointer_needed
)
584 add_to_hard_reg_set (&set_up_by_prologue
.set
, Pmode
,
585 HARD_FRAME_POINTER_REGNUM
);
586 if (pic_offset_table_rtx
587 && (unsigned) PIC_OFFSET_TABLE_REGNUM
!= INVALID_REGNUM
)
588 add_to_hard_reg_set (&set_up_by_prologue
.set
, Pmode
,
589 PIC_OFFSET_TABLE_REGNUM
);
591 add_to_hard_reg_set (&set_up_by_prologue
.set
,
592 GET_MODE (crtl
->drap_reg
),
593 REGNO (crtl
->drap_reg
));
594 if (targetm
.set_up_by_prologue
)
595 targetm
.set_up_by_prologue (&set_up_by_prologue
);
597 /* We don't use a different max size depending on
598 optimize_bb_for_speed_p because increasing shrink-wrapping
599 opportunities by duplicating tail blocks can actually result
600 in an overall decrease in code size. */
601 max_grow_size
= get_uncond_jump_length ();
602 max_grow_size
*= PARAM_VALUE (PARAM_MAX_GROW_COPY_BB_INSNS
);
604 FOR_EACH_BB_FN (bb
, cfun
)
609 FOR_BB_INSNS (bb
, insn
)
610 if (NONDEBUG_INSN_P (insn
))
612 if (requires_stack_frame_p (insn
, prologue_used
,
613 set_up_by_prologue
.set
))
615 if (bb
== (*entry_edge
)->dest
)
616 goto fail_shrinkwrap
;
617 bitmap_set_bit (bb_flags
, bb
->index
);
621 else if (size
<= max_grow_size
)
623 size
+= get_attr_min_length (insn
);
624 if (size
> max_grow_size
)
625 bitmap_set_bit (&bb_on_list
, bb
->index
);
630 /* Blocks that really need a prologue, or are too big for tails. */
631 bitmap_ior_into (&bb_on_list
, bb_flags
);
633 /* For every basic block that needs a prologue, mark all blocks
634 reachable from it, so as to ensure they are also seen as
635 requiring a prologue. */
636 while (!vec
.is_empty ())
638 basic_block tmp_bb
= vec
.pop ();
640 FOR_EACH_EDGE (e
, ei
, tmp_bb
->succs
)
641 if (e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
)
642 && bitmap_set_bit (bb_flags
, e
->dest
->index
))
643 vec
.quick_push (e
->dest
);
646 /* Find the set of basic blocks that need no prologue, have a
647 single successor, can be duplicated, meet a max size
648 requirement, and go to the exit via like blocks. */
649 vec
.quick_push (EXIT_BLOCK_PTR_FOR_FN (cfun
));
650 while (!vec
.is_empty ())
652 basic_block tmp_bb
= vec
.pop ();
654 FOR_EACH_EDGE (e
, ei
, tmp_bb
->preds
)
655 if (single_succ_p (e
->src
)
656 && !bitmap_bit_p (&bb_on_list
, e
->src
->index
)
657 && can_duplicate_block_p (e
->src
))
662 /* If there is predecessor of e->src which doesn't
663 need prologue and the edge is complex,
664 we might not be able to redirect the branch
665 to a copy of e->src. */
666 FOR_EACH_EDGE (pe
, pei
, e
->src
->preds
)
667 if ((pe
->flags
& EDGE_COMPLEX
) != 0
668 && !bitmap_bit_p (bb_flags
, pe
->src
->index
))
670 if (pe
== NULL
&& bitmap_set_bit (&bb_tail
, e
->src
->index
))
671 vec
.quick_push (e
->src
);
675 /* Now walk backwards from every block that is marked as needing
676 a prologue to compute the bb_antic_flags bitmap. Exclude
677 tail blocks; They can be duplicated to be used on paths not
678 needing a prologue. */
679 bitmap_clear (&bb_on_list
);
680 bitmap_and_compl (&bb_antic_flags
, bb_flags
, &bb_tail
);
681 FOR_EACH_BB_FN (bb
, cfun
)
683 if (!bitmap_bit_p (&bb_antic_flags
, bb
->index
))
685 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
686 if (!bitmap_bit_p (&bb_antic_flags
, e
->src
->index
)
687 && bitmap_set_bit (&bb_on_list
, e
->src
->index
))
688 vec
.quick_push (e
->src
);
690 while (!vec
.is_empty ())
692 basic_block tmp_bb
= vec
.pop ();
695 bitmap_clear_bit (&bb_on_list
, tmp_bb
->index
);
696 FOR_EACH_EDGE (e
, ei
, tmp_bb
->succs
)
697 if (!bitmap_bit_p (&bb_antic_flags
, e
->dest
->index
))
705 bitmap_set_bit (&bb_antic_flags
, tmp_bb
->index
);
706 FOR_EACH_EDGE (e
, ei
, tmp_bb
->preds
)
707 if (!bitmap_bit_p (&bb_antic_flags
, e
->src
->index
)
708 && bitmap_set_bit (&bb_on_list
, e
->src
->index
))
709 vec
.quick_push (e
->src
);
712 /* Find exactly one edge that leads to a block in ANTIC from
713 a block that isn't. */
714 if (!bitmap_bit_p (&bb_antic_flags
, (*entry_edge
)->dest
->index
))
715 FOR_EACH_BB_FN (bb
, cfun
)
717 if (!bitmap_bit_p (&bb_antic_flags
, bb
->index
))
719 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
720 if (!bitmap_bit_p (&bb_antic_flags
, e
->src
->index
))
722 if (*entry_edge
!= orig_entry_edge
)
724 *entry_edge
= orig_entry_edge
;
726 fprintf (dump_file
, "More than one candidate edge.\n");
727 goto fail_shrinkwrap
;
730 fprintf (dump_file
, "Found candidate edge for "
731 "shrink-wrapping, %d->%d.\n", e
->src
->index
,
737 if (*entry_edge
!= orig_entry_edge
)
739 /* Test whether the prologue is known to clobber any register
740 (other than FP or SP) which are live on the edge. */
741 CLEAR_HARD_REG_BIT (prologue_clobbered
, STACK_POINTER_REGNUM
);
742 if (frame_pointer_needed
)
743 CLEAR_HARD_REG_BIT (prologue_clobbered
, HARD_FRAME_POINTER_REGNUM
);
744 REG_SET_TO_HARD_REG_SET (live_on_edge
,
745 df_get_live_in ((*entry_edge
)->dest
));
746 if (hard_reg_set_intersect_p (live_on_edge
, prologue_clobbered
))
748 *entry_edge
= orig_entry_edge
;
751 "Shrink-wrapping aborted due to clobber.\n");
754 if (*entry_edge
!= orig_entry_edge
)
756 crtl
->shrink_wrapped
= true;
758 fprintf (dump_file
, "Performing shrink-wrapping.\n");
760 /* Find tail blocks reachable from both blocks needing a
761 prologue and blocks not needing a prologue. */
762 if (!bitmap_empty_p (&bb_tail
))
763 FOR_EACH_BB_FN (bb
, cfun
)
765 bool some_pro
, some_no_pro
;
766 if (!bitmap_bit_p (&bb_tail
, bb
->index
))
768 some_pro
= some_no_pro
= false;
769 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
771 if (bitmap_bit_p (bb_flags
, e
->src
->index
))
776 if (some_pro
&& some_no_pro
)
779 bitmap_clear_bit (&bb_tail
, bb
->index
);
781 /* Find the head of each tail. */
782 while (!vec
.is_empty ())
784 basic_block tbb
= vec
.pop ();
786 if (!bitmap_bit_p (&bb_tail
, tbb
->index
))
789 while (single_succ_p (tbb
))
791 tbb
= single_succ (tbb
);
792 bitmap_clear_bit (&bb_tail
, tbb
->index
);
795 /* Now duplicate the tails. */
796 if (!bitmap_empty_p (&bb_tail
))
797 FOR_EACH_BB_REVERSE_FN (bb
, cfun
)
799 basic_block copy_bb
, tbb
;
800 rtx_insn
*insert_point
;
803 if (!bitmap_clear_bit (&bb_tail
, bb
->index
))
806 /* Create a copy of BB, instructions and all, for
807 use on paths that don't need a prologue.
808 Ideal placement of the copy is on a fall-thru edge
809 or after a block that would jump to the copy. */
810 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
811 if (!bitmap_bit_p (bb_flags
, e
->src
->index
)
812 && single_succ_p (e
->src
))
816 /* Make sure we insert after any barriers. */
817 rtx_insn
*end
= get_last_bb_insn (e
->src
);
818 copy_bb
= create_basic_block (NEXT_INSN (end
),
820 BB_COPY_PARTITION (copy_bb
, e
->src
);
824 /* Otherwise put the copy at the end of the function. */
825 copy_bb
= create_basic_block (NULL_RTX
, NULL_RTX
,
826 EXIT_BLOCK_PTR_FOR_FN (cfun
)->prev_bb
);
827 BB_COPY_PARTITION (copy_bb
, bb
);
830 insert_point
= emit_note_after (NOTE_INSN_DELETED
,
832 emit_barrier_after (BB_END (copy_bb
));
837 dup_block_and_redirect (tbb
, copy_bb
, insert_point
,
839 tbb
= single_succ (tbb
);
840 if (tbb
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
842 e
= split_block (copy_bb
, PREV_INSN (insert_point
));
846 /* Quiet verify_flow_info by (ab)using EDGE_FAKE.
847 We have yet to add a simple_return to the tails,
848 as we'd like to first convert_jumps_to_returns in
849 case the block is no longer used after that. */
851 if (CALL_P (PREV_INSN (insert_point
))
852 && SIBLING_CALL_P (PREV_INSN (insert_point
)))
853 eflags
= EDGE_SIBCALL
| EDGE_ABNORMAL
;
854 make_single_succ_edge (copy_bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
),
857 /* verify_flow_info doesn't like a note after a
859 delete_insn (insert_point
);
860 if (bitmap_empty_p (&bb_tail
))
866 bitmap_clear (&bb_tail
);
867 bitmap_clear (&bb_antic_flags
);
868 bitmap_clear (&bb_on_list
);
873 /* If we're allowed to generate a simple return instruction, then by
874 definition we don't need a full epilogue. If the last basic
875 block before the exit block does not contain active instructions,
876 examine its predecessors and try to emit (conditional) return
880 get_unconverted_simple_return (edge exit_fallthru_edge
, bitmap_head bb_flags
,
881 vec
<edge
> *unconverted_simple_returns
,
882 rtx_insn
**returnjump
)
888 /* convert_jumps_to_returns may add to preds of the exit block
889 (but won't remove). Stop at end of current preds. */
890 last
= EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun
)->preds
);
891 for (i
= 0; i
< last
; i
++)
893 edge e
= EDGE_I (EXIT_BLOCK_PTR_FOR_FN (cfun
)->preds
, i
);
894 if (LABEL_P (BB_HEAD (e
->src
))
895 && !bitmap_bit_p (&bb_flags
, e
->src
->index
)
896 && !active_insn_between (BB_HEAD (e
->src
), BB_END (e
->src
)))
897 *unconverted_simple_returns
898 = convert_jumps_to_returns (e
->src
, true,
899 *unconverted_simple_returns
);
903 if (exit_fallthru_edge
!= NULL
904 && EDGE_COUNT (exit_fallthru_edge
->src
->preds
) != 0
905 && !bitmap_bit_p (&bb_flags
, exit_fallthru_edge
->src
->index
))
909 last_bb
= emit_return_for_exit (exit_fallthru_edge
, true);
910 *returnjump
= BB_END (last_bb
);
911 exit_fallthru_edge
= NULL
;
913 return exit_fallthru_edge
;
916 /* If there were branches to an empty LAST_BB which we tried to
917 convert to conditional simple_returns, but couldn't for some
918 reason, create a block to hold a simple_return insn and redirect
919 those remaining edges. */
922 convert_to_simple_return (edge entry_edge
, edge orig_entry_edge
,
923 bitmap_head bb_flags
, rtx_insn
*returnjump
,
924 vec
<edge
> unconverted_simple_returns
)
929 if (!unconverted_simple_returns
.is_empty ())
931 basic_block simple_return_block_hot
= NULL
;
932 basic_block simple_return_block_cold
= NULL
;
933 edge pending_edge_hot
= NULL
;
934 edge pending_edge_cold
= NULL
;
935 basic_block exit_pred
;
938 gcc_assert (entry_edge
!= orig_entry_edge
);
940 /* See if we can reuse the last insn that was emitted for the
942 if (returnjump
!= NULL_RTX
943 && JUMP_LABEL (returnjump
) == simple_return_rtx
)
945 e
= split_block (BLOCK_FOR_INSN (returnjump
), PREV_INSN (returnjump
));
946 if (BB_PARTITION (e
->src
) == BB_HOT_PARTITION
)
947 simple_return_block_hot
= e
->dest
;
949 simple_return_block_cold
= e
->dest
;
952 /* Also check returns we might need to add to tail blocks. */
953 FOR_EACH_EDGE (e
, ei
, EXIT_BLOCK_PTR_FOR_FN (cfun
)->preds
)
954 if (EDGE_COUNT (e
->src
->preds
) != 0
955 && (e
->flags
& EDGE_FAKE
) != 0
956 && !bitmap_bit_p (&bb_flags
, e
->src
->index
))
958 if (BB_PARTITION (e
->src
) == BB_HOT_PARTITION
)
959 pending_edge_hot
= e
;
961 pending_edge_cold
= e
;
964 /* Save a pointer to the exit's predecessor BB for use in
965 inserting new BBs at the end of the function. Do this
966 after the call to split_block above which may split
967 the original exit pred. */
968 exit_pred
= EXIT_BLOCK_PTR_FOR_FN (cfun
)->prev_bb
;
970 FOR_EACH_VEC_ELT (unconverted_simple_returns
, i
, e
)
972 basic_block
*pdest_bb
;
975 if (BB_PARTITION (e
->src
) == BB_HOT_PARTITION
)
977 pdest_bb
= &simple_return_block_hot
;
978 pending
= pending_edge_hot
;
982 pdest_bb
= &simple_return_block_cold
;
983 pending
= pending_edge_cold
;
986 if (*pdest_bb
== NULL
&& pending
!= NULL
)
988 emit_return_into_block (true, pending
->src
);
989 pending
->flags
&= ~(EDGE_FALLTHRU
| EDGE_FAKE
);
990 *pdest_bb
= pending
->src
;
992 else if (*pdest_bb
== NULL
)
997 bb
= create_basic_block (NULL
, NULL
, exit_pred
);
998 BB_COPY_PARTITION (bb
, e
->src
);
999 start
= emit_jump_insn_after (gen_simple_return (),
1001 JUMP_LABEL (start
) = simple_return_rtx
;
1002 emit_barrier_after (start
);
1005 make_edge (bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
1007 redirect_edge_and_branch_force (e
, *pdest_bb
);
1009 unconverted_simple_returns
.release ();
1012 if (entry_edge
!= orig_entry_edge
)
1014 FOR_EACH_EDGE (e
, ei
, EXIT_BLOCK_PTR_FOR_FN (cfun
)->preds
)
1015 if (EDGE_COUNT (e
->src
->preds
) != 0
1016 && (e
->flags
& EDGE_FAKE
) != 0
1017 && !bitmap_bit_p (&bb_flags
, e
->src
->index
))
1019 emit_return_into_block (true, e
->src
);
1020 e
->flags
&= ~(EDGE_FALLTHRU
| EDGE_FAKE
);