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"
38 #include "hard-reg-set.h"
39 #include "insn-config.h"
44 #include "langhooks.h"
46 #include "common/common-target.h"
47 #include "gimple-expr.h"
49 #include "tree-pass.h"
53 #include "bb-reorder.h"
54 #include "shrink-wrap.h"
58 #ifdef HAVE_simple_return
60 /* Return true if INSN requires the stack frame to be set up.
61 PROLOGUE_USED contains the hard registers used in the function
62 prologue. SET_UP_BY_PROLOGUE is the set of registers we expect the
63 prologue to set up for the function. */
65 requires_stack_frame_p (rtx_insn
*insn
, HARD_REG_SET prologue_used
,
66 HARD_REG_SET set_up_by_prologue
)
69 HARD_REG_SET hardregs
;
73 return !SIBLING_CALL_P (insn
);
75 /* We need a frame to get the unique CFA expected by the unwinder. */
76 if (cfun
->can_throw_non_call_exceptions
&& can_throw_internal (insn
))
79 CLEAR_HARD_REG_SET (hardregs
);
80 FOR_EACH_INSN_DEF (def
, insn
)
82 rtx dreg
= DF_REF_REG (def
);
87 add_to_hard_reg_set (&hardregs
, GET_MODE (dreg
),
90 if (hard_reg_set_intersect_p (hardregs
, prologue_used
))
92 AND_COMPL_HARD_REG_SET (hardregs
, call_used_reg_set
);
93 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
94 if (TEST_HARD_REG_BIT (hardregs
, regno
)
95 && df_regs_ever_live_p (regno
))
98 FOR_EACH_INSN_USE (use
, insn
)
100 rtx reg
= DF_REF_REG (use
);
105 add_to_hard_reg_set (&hardregs
, GET_MODE (reg
),
108 if (hard_reg_set_intersect_p (hardregs
, set_up_by_prologue
))
114 /* See whether there has a single live edge from BB, which dest uses
115 [REGNO, END_REGNO). Return the live edge if its dest bb has
116 one or two predecessors. Otherwise return NULL. */
119 live_edge_for_reg (basic_block bb
, int regno
, int end_regno
)
127 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
129 live
= df_get_live_in (e
->dest
);
130 for (i
= regno
; i
< end_regno
; i
++)
131 if (REGNO_REG_SET_P (live
, i
))
133 if (live_edge
&& live_edge
!= e
)
139 /* We can sometimes encounter dead code. Don't try to move it
140 into the exit block. */
141 if (!live_edge
|| live_edge
->dest
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
144 /* Reject targets of abnormal edges. This is needed for correctness
145 on ports like Alpha and MIPS, whose pic_offset_table_rtx can die on
146 exception edges even though it is generally treated as call-saved
147 for the majority of the compilation. Moving across abnormal edges
148 isn't going to be interesting for shrink-wrap usage anyway. */
149 if (live_edge
->flags
& EDGE_ABNORMAL
)
152 /* When live_edge->dest->preds == 2, we can create a new block on
153 the edge to make it meet the requirement. */
154 if (EDGE_COUNT (live_edge
->dest
->preds
) > 2)
160 /* Try to move INSN from BB to a successor. Return true on success.
161 USES and DEFS are the set of registers that are used and defined
162 after INSN in BB. SPLIT_P indicates whether a live edge from BB
163 is splitted or not. */
166 move_insn_for_shrink_wrap (basic_block bb
, rtx_insn
*insn
,
167 const HARD_REG_SET uses
,
168 const HARD_REG_SET defs
,
172 bitmap live_out
, live_in
, bb_uses
, bb_defs
;
173 unsigned int i
, dregno
, end_dregno
;
174 unsigned int sregno
= FIRST_PSEUDO_REGISTER
;
175 unsigned int end_sregno
= FIRST_PSEUDO_REGISTER
;
176 basic_block next_block
;
179 /* Look for a simple register copy. */
180 set
= single_set (insn
);
184 dest
= SET_DEST (set
);
188 unsigned int reg_num
= 0;
189 unsigned int nonconstobj_num
= 0;
190 rtx src_inner
= NULL_RTX
;
192 if (can_throw_internal (insn
))
195 subrtx_var_iterator::array_type array
;
196 FOR_EACH_SUBRTX_VAR (iter
, array
, src
, ALL
)
204 else if (!CONSTANT_P (x
) && OBJECT_P (x
))
208 if (nonconstobj_num
> 0
211 else if (reg_num
== 1)
215 if (!REG_P (dest
) || src
== NULL_RTX
216 /* STACK or FRAME related adjustment might be part of prologue.
217 So keep them in the entry block. */
218 || dest
== stack_pointer_rtx
219 || dest
== frame_pointer_rtx
220 || dest
== hard_frame_pointer_rtx
)
223 /* Make sure that the source register isn't defined later in BB. */
226 sregno
= REGNO (src
);
227 end_sregno
= END_REGNO (src
);
228 if (overlaps_hard_reg_set_p (defs
, GET_MODE (src
), sregno
))
232 /* Make sure that the destination register isn't referenced later in BB. */
233 dregno
= REGNO (dest
);
234 end_dregno
= END_REGNO (dest
);
235 if (overlaps_hard_reg_set_p (uses
, GET_MODE (dest
), dregno
)
236 || overlaps_hard_reg_set_p (defs
, GET_MODE (dest
), dregno
))
239 /* See whether there is a successor block to which we could move INSN. */
240 live_edge
= live_edge_for_reg (bb
, dregno
, end_dregno
);
244 next_block
= live_edge
->dest
;
245 /* Create a new basic block on the edge. */
246 if (EDGE_COUNT (next_block
->preds
) == 2)
248 /* split_edge for a block with only one successor is meaningless. */
249 if (EDGE_COUNT (bb
->succs
) == 1)
252 /* If DF_LIVE doesn't exist, i.e. at -O1, just give up. */
256 basic_block old_dest
= live_edge
->dest
;
257 next_block
= split_edge (live_edge
);
259 /* We create a new basic block. Call df_grow_bb_info to make sure
260 all data structures are allocated. */
261 df_grow_bb_info (df_live
);
263 bitmap_and (df_get_live_in (next_block
), df_get_live_out (bb
),
264 df_get_live_in (old_dest
));
265 df_set_bb_dirty (next_block
);
267 /* We should not split more than once for a function. */
274 /* At this point we are committed to moving INSN, but let's try to
275 move it as far as we can. */
278 live_out
= df_get_live_out (bb
);
279 live_in
= df_get_live_in (next_block
);
282 /* Check whether BB uses DEST or clobbers DEST. We need to add
283 INSN to BB if so. Either way, DEST is no longer live on entry,
284 except for any part that overlaps SRC (next loop). */
285 bb_uses
= &DF_LR_BB_INFO (bb
)->use
;
286 bb_defs
= &DF_LR_BB_INFO (bb
)->def
;
289 for (i
= dregno
; i
< end_dregno
; i
++)
292 || REGNO_REG_SET_P (bb_uses
, i
)
293 || REGNO_REG_SET_P (bb_defs
, i
)
294 || REGNO_REG_SET_P (&DF_LIVE_BB_INFO (bb
)->gen
, i
))
296 CLEAR_REGNO_REG_SET (live_out
, i
);
297 CLEAR_REGNO_REG_SET (live_in
, i
);
300 /* Check whether BB clobbers SRC. We need to add INSN to BB if so.
301 Either way, SRC is now live on entry. */
302 for (i
= sregno
; i
< end_sregno
; i
++)
305 || REGNO_REG_SET_P (bb_defs
, i
)
306 || REGNO_REG_SET_P (&DF_LIVE_BB_INFO (bb
)->gen
, i
))
308 SET_REGNO_REG_SET (live_out
, i
);
309 SET_REGNO_REG_SET (live_in
, i
);
314 /* DF_LR_BB_INFO (bb)->def does not comprise the DF_REF_PARTIAL and
315 DF_REF_CONDITIONAL defs. So if DF_LIVE doesn't exist, i.e.
316 at -O1, just give up searching NEXT_BLOCK. */
318 for (i
= dregno
; i
< end_dregno
; i
++)
320 CLEAR_REGNO_REG_SET (live_out
, i
);
321 CLEAR_REGNO_REG_SET (live_in
, i
);
324 for (i
= sregno
; i
< end_sregno
; i
++)
326 SET_REGNO_REG_SET (live_out
, i
);
327 SET_REGNO_REG_SET (live_in
, i
);
331 /* If we don't need to add the move to BB, look for a single
335 live_edge
= live_edge_for_reg (next_block
, dregno
, end_dregno
);
336 if (!live_edge
|| EDGE_COUNT (live_edge
->dest
->preds
) > 1)
338 next_block
= live_edge
->dest
;
343 /* For the new created basic block, there is no dataflow info at all.
344 So skip the following dataflow update and check. */
347 /* BB now defines DEST. It only uses the parts of DEST that overlap SRC
349 for (i
= dregno
; i
< end_dregno
; i
++)
351 CLEAR_REGNO_REG_SET (bb_uses
, i
);
352 SET_REGNO_REG_SET (bb_defs
, i
);
355 /* BB now uses SRC. */
356 for (i
= sregno
; i
< end_sregno
; i
++)
357 SET_REGNO_REG_SET (bb_uses
, i
);
360 emit_insn_after (PATTERN (insn
), bb_note (bb
));
365 /* Look for register copies in the first block of the function, and move
366 them down into successor blocks if the register is used only on one
367 path. This exposes more opportunities for shrink-wrapping. These
368 kinds of sets often occur when incoming argument registers are moved
369 to call-saved registers because their values are live across one or
370 more calls during the function. */
373 prepare_shrink_wrap (basic_block entry_block
)
375 rtx_insn
*insn
, *curr
;
377 HARD_REG_SET uses
, defs
;
379 bool split_p
= false;
381 if (JUMP_P (BB_END (entry_block
)))
383 /* To have more shrink-wrapping opportunities, prepare_shrink_wrap tries
384 to sink the copies from parameter to callee saved register out of
385 entry block. copyprop_hardreg_forward_bb_without_debug_insn is called
386 to release some dependences. */
387 copyprop_hardreg_forward_bb_without_debug_insn (entry_block
);
390 CLEAR_HARD_REG_SET (uses
);
391 CLEAR_HARD_REG_SET (defs
);
392 FOR_BB_INSNS_REVERSE_SAFE (entry_block
, insn
, curr
)
393 if (NONDEBUG_INSN_P (insn
)
394 && !move_insn_for_shrink_wrap (entry_block
, insn
, uses
, defs
,
397 /* Add all defined registers to DEFs. */
398 FOR_EACH_INSN_DEF (def
, insn
)
400 x
= DF_REF_REG (def
);
401 if (REG_P (x
) && HARD_REGISTER_P (x
))
402 SET_HARD_REG_BIT (defs
, REGNO (x
));
405 /* Add all used registers to USESs. */
406 FOR_EACH_INSN_USE (use
, insn
)
408 x
= DF_REF_REG (use
);
409 if (REG_P (x
) && HARD_REGISTER_P (x
))
410 SET_HARD_REG_BIT (uses
, REGNO (x
));
415 /* Create a copy of BB instructions and insert at BEFORE. Redirect
416 preds of BB to COPY_BB if they don't appear in NEED_PROLOGUE. */
418 dup_block_and_redirect (basic_block bb
, basic_block copy_bb
, rtx_insn
*before
,
419 bitmap_head
*need_prologue
)
423 rtx_insn
*insn
= BB_END (bb
);
425 /* We know BB has a single successor, so there is no need to copy a
426 simple jump at the end of BB. */
427 if (simplejump_p (insn
))
428 insn
= PREV_INSN (insn
);
431 duplicate_insn_chain (BB_HEAD (bb
), insn
);
435 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
436 if (active_insn_p (insn
))
438 fprintf (dump_file
, "Duplicating bb %d to bb %d, %u active insns.\n",
439 bb
->index
, copy_bb
->index
, count
);
443 emit_insn_before (insn
, before
);
445 /* Redirect all the paths that need no prologue into copy_bb. */
446 for (ei
= ei_start (bb
->preds
); (e
= ei_safe_edge (ei
));)
447 if (!bitmap_bit_p (need_prologue
, e
->src
->index
))
449 int freq
= EDGE_FREQUENCY (e
);
450 copy_bb
->count
+= e
->count
;
451 copy_bb
->frequency
+= EDGE_FREQUENCY (e
);
452 e
->dest
->count
-= e
->count
;
453 if (e
->dest
->count
< 0)
455 e
->dest
->frequency
-= freq
;
456 if (e
->dest
->frequency
< 0)
457 e
->dest
->frequency
= 0;
458 redirect_edge_and_branch_force (e
, copy_bb
);
466 /* Try to perform a kind of shrink-wrapping, making sure the
467 prologue/epilogue is emitted only around those parts of the
468 function that require it. */
471 try_shrink_wrapping (edge
*entry_edge
, edge orig_entry_edge
,
472 bitmap_head
*bb_flags
, rtx_insn
*prologue_seq
)
476 bool nonempty_prologue
= false;
477 unsigned max_grow_size
;
480 for (seq
= prologue_seq
; seq
; seq
= NEXT_INSN (seq
))
481 if (!NOTE_P (seq
) || NOTE_KIND (seq
) != NOTE_INSN_PROLOGUE_END
)
483 nonempty_prologue
= true;
487 if (flag_shrink_wrap
&& HAVE_simple_return
488 && (targetm
.profile_before_prologue () || !crtl
->profile
)
489 && nonempty_prologue
&& !crtl
->calls_eh_return
)
491 HARD_REG_SET prologue_clobbered
, prologue_used
, live_on_edge
;
492 struct hard_reg_set_container set_up_by_prologue
;
494 vec
<basic_block
> vec
;
496 bitmap_head bb_antic_flags
;
497 bitmap_head bb_on_list
;
501 fprintf (dump_file
, "Attempting shrink-wrapping optimization.\n");
503 /* Compute the registers set and used in the prologue. */
504 CLEAR_HARD_REG_SET (prologue_clobbered
);
505 CLEAR_HARD_REG_SET (prologue_used
);
506 for (p_insn
= prologue_seq
; p_insn
; p_insn
= NEXT_INSN (p_insn
))
508 HARD_REG_SET this_used
;
509 if (!NONDEBUG_INSN_P (p_insn
))
512 CLEAR_HARD_REG_SET (this_used
);
513 note_uses (&PATTERN (p_insn
), record_hard_reg_uses
,
515 AND_COMPL_HARD_REG_SET (this_used
, prologue_clobbered
);
516 IOR_HARD_REG_SET (prologue_used
, this_used
);
517 note_stores (PATTERN (p_insn
), record_hard_reg_sets
,
518 &prologue_clobbered
);
521 prepare_shrink_wrap ((*entry_edge
)->dest
);
523 bitmap_initialize (&bb_antic_flags
, &bitmap_default_obstack
);
524 bitmap_initialize (&bb_on_list
, &bitmap_default_obstack
);
525 bitmap_initialize (&bb_tail
, &bitmap_default_obstack
);
527 /* Find the set of basic blocks that require a stack frame,
528 and blocks that are too big to be duplicated. */
530 vec
.create (n_basic_blocks_for_fn (cfun
));
532 CLEAR_HARD_REG_SET (set_up_by_prologue
.set
);
533 add_to_hard_reg_set (&set_up_by_prologue
.set
, Pmode
,
534 STACK_POINTER_REGNUM
);
535 add_to_hard_reg_set (&set_up_by_prologue
.set
, Pmode
, ARG_POINTER_REGNUM
);
536 if (frame_pointer_needed
)
537 add_to_hard_reg_set (&set_up_by_prologue
.set
, Pmode
,
538 HARD_FRAME_POINTER_REGNUM
);
539 if ((unsigned) PIC_OFFSET_TABLE_REGNUM
!= INVALID_REGNUM
)
540 add_to_hard_reg_set (&set_up_by_prologue
.set
, Pmode
,
541 PIC_OFFSET_TABLE_REGNUM
);
543 add_to_hard_reg_set (&set_up_by_prologue
.set
,
544 GET_MODE (crtl
->drap_reg
),
545 REGNO (crtl
->drap_reg
));
546 if (targetm
.set_up_by_prologue
)
547 targetm
.set_up_by_prologue (&set_up_by_prologue
);
549 /* We don't use a different max size depending on
550 optimize_bb_for_speed_p because increasing shrink-wrapping
551 opportunities by duplicating tail blocks can actually result
552 in an overall decrease in code size. */
553 max_grow_size
= get_uncond_jump_length ();
554 max_grow_size
*= PARAM_VALUE (PARAM_MAX_GROW_COPY_BB_INSNS
);
556 FOR_EACH_BB_FN (bb
, cfun
)
561 FOR_BB_INSNS (bb
, insn
)
562 if (NONDEBUG_INSN_P (insn
))
564 if (requires_stack_frame_p (insn
, prologue_used
,
565 set_up_by_prologue
.set
))
567 if (bb
== (*entry_edge
)->dest
)
568 goto fail_shrinkwrap
;
569 bitmap_set_bit (bb_flags
, bb
->index
);
573 else if (size
<= max_grow_size
)
575 size
+= get_attr_min_length (insn
);
576 if (size
> max_grow_size
)
577 bitmap_set_bit (&bb_on_list
, bb
->index
);
582 /* Blocks that really need a prologue, or are too big for tails. */
583 bitmap_ior_into (&bb_on_list
, bb_flags
);
585 /* For every basic block that needs a prologue, mark all blocks
586 reachable from it, so as to ensure they are also seen as
587 requiring a prologue. */
588 while (!vec
.is_empty ())
590 basic_block tmp_bb
= vec
.pop ();
592 FOR_EACH_EDGE (e
, ei
, tmp_bb
->succs
)
593 if (e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
)
594 && bitmap_set_bit (bb_flags
, e
->dest
->index
))
595 vec
.quick_push (e
->dest
);
598 /* Find the set of basic blocks that need no prologue, have a
599 single successor, can be duplicated, meet a max size
600 requirement, and go to the exit via like blocks. */
601 vec
.quick_push (EXIT_BLOCK_PTR_FOR_FN (cfun
));
602 while (!vec
.is_empty ())
604 basic_block tmp_bb
= vec
.pop ();
606 FOR_EACH_EDGE (e
, ei
, tmp_bb
->preds
)
607 if (single_succ_p (e
->src
)
608 && !bitmap_bit_p (&bb_on_list
, e
->src
->index
)
609 && can_duplicate_block_p (e
->src
))
614 /* If there is predecessor of e->src which doesn't
615 need prologue and the edge is complex,
616 we might not be able to redirect the branch
617 to a copy of e->src. */
618 FOR_EACH_EDGE (pe
, pei
, e
->src
->preds
)
619 if ((pe
->flags
& EDGE_COMPLEX
) != 0
620 && !bitmap_bit_p (bb_flags
, pe
->src
->index
))
622 if (pe
== NULL
&& bitmap_set_bit (&bb_tail
, e
->src
->index
))
623 vec
.quick_push (e
->src
);
627 /* Now walk backwards from every block that is marked as needing
628 a prologue to compute the bb_antic_flags bitmap. Exclude
629 tail blocks; They can be duplicated to be used on paths not
630 needing a prologue. */
631 bitmap_clear (&bb_on_list
);
632 bitmap_and_compl (&bb_antic_flags
, bb_flags
, &bb_tail
);
633 FOR_EACH_BB_FN (bb
, cfun
)
635 if (!bitmap_bit_p (&bb_antic_flags
, bb
->index
))
637 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
638 if (!bitmap_bit_p (&bb_antic_flags
, e
->src
->index
)
639 && bitmap_set_bit (&bb_on_list
, e
->src
->index
))
640 vec
.quick_push (e
->src
);
642 while (!vec
.is_empty ())
644 basic_block tmp_bb
= vec
.pop ();
647 bitmap_clear_bit (&bb_on_list
, tmp_bb
->index
);
648 FOR_EACH_EDGE (e
, ei
, tmp_bb
->succs
)
649 if (!bitmap_bit_p (&bb_antic_flags
, e
->dest
->index
))
657 bitmap_set_bit (&bb_antic_flags
, tmp_bb
->index
);
658 FOR_EACH_EDGE (e
, ei
, tmp_bb
->preds
)
659 if (!bitmap_bit_p (&bb_antic_flags
, e
->src
->index
)
660 && bitmap_set_bit (&bb_on_list
, e
->src
->index
))
661 vec
.quick_push (e
->src
);
664 /* Find exactly one edge that leads to a block in ANTIC from
665 a block that isn't. */
666 if (!bitmap_bit_p (&bb_antic_flags
, (*entry_edge
)->dest
->index
))
667 FOR_EACH_BB_FN (bb
, cfun
)
669 if (!bitmap_bit_p (&bb_antic_flags
, bb
->index
))
671 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
672 if (!bitmap_bit_p (&bb_antic_flags
, e
->src
->index
))
674 if (*entry_edge
!= orig_entry_edge
)
676 *entry_edge
= orig_entry_edge
;
678 fprintf (dump_file
, "More than one candidate edge.\n");
679 goto fail_shrinkwrap
;
682 fprintf (dump_file
, "Found candidate edge for "
683 "shrink-wrapping, %d->%d.\n", e
->src
->index
,
689 if (*entry_edge
!= orig_entry_edge
)
691 /* Test whether the prologue is known to clobber any register
692 (other than FP or SP) which are live on the edge. */
693 CLEAR_HARD_REG_BIT (prologue_clobbered
, STACK_POINTER_REGNUM
);
694 if (frame_pointer_needed
)
695 CLEAR_HARD_REG_BIT (prologue_clobbered
, HARD_FRAME_POINTER_REGNUM
);
696 REG_SET_TO_HARD_REG_SET (live_on_edge
,
697 df_get_live_in ((*entry_edge
)->dest
));
698 if (hard_reg_set_intersect_p (live_on_edge
, prologue_clobbered
))
700 *entry_edge
= orig_entry_edge
;
703 "Shrink-wrapping aborted due to clobber.\n");
706 if (*entry_edge
!= orig_entry_edge
)
708 crtl
->shrink_wrapped
= true;
710 fprintf (dump_file
, "Performing shrink-wrapping.\n");
712 /* Find tail blocks reachable from both blocks needing a
713 prologue and blocks not needing a prologue. */
714 if (!bitmap_empty_p (&bb_tail
))
715 FOR_EACH_BB_FN (bb
, cfun
)
717 bool some_pro
, some_no_pro
;
718 if (!bitmap_bit_p (&bb_tail
, bb
->index
))
720 some_pro
= some_no_pro
= false;
721 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
723 if (bitmap_bit_p (bb_flags
, e
->src
->index
))
728 if (some_pro
&& some_no_pro
)
731 bitmap_clear_bit (&bb_tail
, bb
->index
);
733 /* Find the head of each tail. */
734 while (!vec
.is_empty ())
736 basic_block tbb
= vec
.pop ();
738 if (!bitmap_bit_p (&bb_tail
, tbb
->index
))
741 while (single_succ_p (tbb
))
743 tbb
= single_succ (tbb
);
744 bitmap_clear_bit (&bb_tail
, tbb
->index
);
747 /* Now duplicate the tails. */
748 if (!bitmap_empty_p (&bb_tail
))
749 FOR_EACH_BB_REVERSE_FN (bb
, cfun
)
751 basic_block copy_bb
, tbb
;
752 rtx_insn
*insert_point
;
755 if (!bitmap_clear_bit (&bb_tail
, bb
->index
))
758 /* Create a copy of BB, instructions and all, for
759 use on paths that don't need a prologue.
760 Ideal placement of the copy is on a fall-thru edge
761 or after a block that would jump to the copy. */
762 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
763 if (!bitmap_bit_p (bb_flags
, e
->src
->index
)
764 && single_succ_p (e
->src
))
768 /* Make sure we insert after any barriers. */
769 rtx_insn
*end
= get_last_bb_insn (e
->src
);
770 copy_bb
= create_basic_block (NEXT_INSN (end
),
772 BB_COPY_PARTITION (copy_bb
, e
->src
);
776 /* Otherwise put the copy at the end of the function. */
777 copy_bb
= create_basic_block (NULL_RTX
, NULL_RTX
,
778 EXIT_BLOCK_PTR_FOR_FN (cfun
)->prev_bb
);
779 BB_COPY_PARTITION (copy_bb
, bb
);
782 insert_point
= emit_note_after (NOTE_INSN_DELETED
,
784 emit_barrier_after (BB_END (copy_bb
));
789 dup_block_and_redirect (tbb
, copy_bb
, insert_point
,
791 tbb
= single_succ (tbb
);
792 if (tbb
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
794 e
= split_block (copy_bb
, PREV_INSN (insert_point
));
798 /* Quiet verify_flow_info by (ab)using EDGE_FAKE.
799 We have yet to add a simple_return to the tails,
800 as we'd like to first convert_jumps_to_returns in
801 case the block is no longer used after that. */
803 if (CALL_P (PREV_INSN (insert_point
))
804 && SIBLING_CALL_P (PREV_INSN (insert_point
)))
805 eflags
= EDGE_SIBCALL
| EDGE_ABNORMAL
;
806 make_single_succ_edge (copy_bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
),
809 /* verify_flow_info doesn't like a note after a
811 delete_insn (insert_point
);
812 if (bitmap_empty_p (&bb_tail
))
818 bitmap_clear (&bb_tail
);
819 bitmap_clear (&bb_antic_flags
);
820 bitmap_clear (&bb_on_list
);
825 /* If we're allowed to generate a simple return instruction, then by
826 definition we don't need a full epilogue. If the last basic
827 block before the exit block does not contain active instructions,
828 examine its predecessors and try to emit (conditional) return
832 get_unconverted_simple_return (edge exit_fallthru_edge
, bitmap_head bb_flags
,
833 vec
<edge
> *unconverted_simple_returns
,
834 rtx_insn
**returnjump
)
840 /* convert_jumps_to_returns may add to preds of the exit block
841 (but won't remove). Stop at end of current preds. */
842 last
= EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun
)->preds
);
843 for (i
= 0; i
< last
; i
++)
845 edge e
= EDGE_I (EXIT_BLOCK_PTR_FOR_FN (cfun
)->preds
, i
);
846 if (LABEL_P (BB_HEAD (e
->src
))
847 && !bitmap_bit_p (&bb_flags
, e
->src
->index
)
848 && !active_insn_between (BB_HEAD (e
->src
), BB_END (e
->src
)))
849 *unconverted_simple_returns
850 = convert_jumps_to_returns (e
->src
, true,
851 *unconverted_simple_returns
);
855 if (exit_fallthru_edge
!= NULL
856 && EDGE_COUNT (exit_fallthru_edge
->src
->preds
) != 0
857 && !bitmap_bit_p (&bb_flags
, exit_fallthru_edge
->src
->index
))
861 last_bb
= emit_return_for_exit (exit_fallthru_edge
, true);
862 *returnjump
= BB_END (last_bb
);
863 exit_fallthru_edge
= NULL
;
865 return exit_fallthru_edge
;
868 /* If there were branches to an empty LAST_BB which we tried to
869 convert to conditional simple_returns, but couldn't for some
870 reason, create a block to hold a simple_return insn and redirect
871 those remaining edges. */
874 convert_to_simple_return (edge entry_edge
, edge orig_entry_edge
,
875 bitmap_head bb_flags
, rtx_insn
*returnjump
,
876 vec
<edge
> unconverted_simple_returns
)
881 if (!unconverted_simple_returns
.is_empty ())
883 basic_block simple_return_block_hot
= NULL
;
884 basic_block simple_return_block_cold
= NULL
;
885 edge pending_edge_hot
= NULL
;
886 edge pending_edge_cold
= NULL
;
887 basic_block exit_pred
;
890 gcc_assert (entry_edge
!= orig_entry_edge
);
892 /* See if we can reuse the last insn that was emitted for the
894 if (returnjump
!= NULL_RTX
895 && JUMP_LABEL (returnjump
) == simple_return_rtx
)
897 e
= split_block (BLOCK_FOR_INSN (returnjump
), PREV_INSN (returnjump
));
898 if (BB_PARTITION (e
->src
) == BB_HOT_PARTITION
)
899 simple_return_block_hot
= e
->dest
;
901 simple_return_block_cold
= e
->dest
;
904 /* Also check returns we might need to add to tail blocks. */
905 FOR_EACH_EDGE (e
, ei
, EXIT_BLOCK_PTR_FOR_FN (cfun
)->preds
)
906 if (EDGE_COUNT (e
->src
->preds
) != 0
907 && (e
->flags
& EDGE_FAKE
) != 0
908 && !bitmap_bit_p (&bb_flags
, e
->src
->index
))
910 if (BB_PARTITION (e
->src
) == BB_HOT_PARTITION
)
911 pending_edge_hot
= e
;
913 pending_edge_cold
= e
;
916 /* Save a pointer to the exit's predecessor BB for use in
917 inserting new BBs at the end of the function. Do this
918 after the call to split_block above which may split
919 the original exit pred. */
920 exit_pred
= EXIT_BLOCK_PTR_FOR_FN (cfun
)->prev_bb
;
922 FOR_EACH_VEC_ELT (unconverted_simple_returns
, i
, e
)
924 basic_block
*pdest_bb
;
927 if (BB_PARTITION (e
->src
) == BB_HOT_PARTITION
)
929 pdest_bb
= &simple_return_block_hot
;
930 pending
= pending_edge_hot
;
934 pdest_bb
= &simple_return_block_cold
;
935 pending
= pending_edge_cold
;
938 if (*pdest_bb
== NULL
&& pending
!= NULL
)
940 emit_return_into_block (true, pending
->src
);
941 pending
->flags
&= ~(EDGE_FALLTHRU
| EDGE_FAKE
);
942 *pdest_bb
= pending
->src
;
944 else if (*pdest_bb
== NULL
)
949 bb
= create_basic_block (NULL
, NULL
, exit_pred
);
950 BB_COPY_PARTITION (bb
, e
->src
);
951 start
= emit_jump_insn_after (gen_simple_return (),
953 JUMP_LABEL (start
) = simple_return_rtx
;
954 emit_barrier_after (start
);
957 make_edge (bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
959 redirect_edge_and_branch_force (e
, *pdest_bb
);
961 unconverted_simple_returns
.release ();
964 if (entry_edge
!= orig_entry_edge
)
966 FOR_EACH_EDGE (e
, ei
, EXIT_BLOCK_PTR_FOR_FN (cfun
)->preds
)
967 if (EDGE_COUNT (e
->src
->preds
) != 0
968 && (e
->flags
& EDGE_FAKE
) != 0
969 && !bitmap_bit_p (&bb_flags
, e
->src
->index
))
971 emit_return_into_block (true, e
->src
);
972 e
->flags
&= ~(EDGE_FALLTHRU
| EDGE_FAKE
);