1 /* Shrink-wrapping related optimizations.
2 Copyright (C) 1987-2021 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"
34 #include "insn-config.h"
37 #include "tree-pass.h"
40 #include "bb-reorder.h"
41 #include "shrink-wrap.h"
45 #include "function-abi.h"
46 #include "print-rtl.h"
48 /* Return true if INSN requires the stack frame to be set up.
49 PROLOGUE_USED contains the hard registers used in the function
50 prologue. SET_UP_BY_PROLOGUE is the set of registers we expect the
51 prologue to set up for the function. */
53 requires_stack_frame_p (rtx_insn
*insn
, HARD_REG_SET prologue_used
,
54 HARD_REG_SET set_up_by_prologue
)
57 HARD_REG_SET hardregs
;
60 if (CALL_P (insn
) && !FAKE_CALL_P (insn
))
61 return !SIBLING_CALL_P (insn
);
63 /* We need a frame to get the unique CFA expected by the unwinder. */
64 if (cfun
->can_throw_non_call_exceptions
&& can_throw_internal (insn
))
67 CLEAR_HARD_REG_SET (hardregs
);
68 FOR_EACH_INSN_DEF (def
, insn
)
70 rtx dreg
= DF_REF_REG (def
);
75 add_to_hard_reg_set (&hardregs
, GET_MODE (dreg
), REGNO (dreg
));
77 if (hard_reg_set_intersect_p (hardregs
, prologue_used
))
79 hardregs
&= ~crtl
->abi
->full_reg_clobbers ();
80 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
81 if (TEST_HARD_REG_BIT (hardregs
, regno
)
82 && df_regs_ever_live_p (regno
))
85 FOR_EACH_INSN_USE (use
, insn
)
87 rtx reg
= DF_REF_REG (use
);
92 add_to_hard_reg_set (&hardregs
, GET_MODE (reg
),
95 if (hard_reg_set_intersect_p (hardregs
, set_up_by_prologue
))
101 /* See whether there has a single live edge from BB, which dest uses
102 [REGNO, END_REGNO). Return the live edge if its dest bb has
103 one or two predecessors. Otherwise return NULL. */
106 live_edge_for_reg (basic_block bb
, int regno
, int end_regno
)
114 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
116 live
= df_get_live_in (e
->dest
);
117 for (i
= regno
; i
< end_regno
; i
++)
118 if (REGNO_REG_SET_P (live
, i
))
120 if (live_edge
&& live_edge
!= e
)
126 /* We can sometimes encounter dead code. Don't try to move it
127 into the exit block. */
128 if (!live_edge
|| live_edge
->dest
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
131 /* Reject targets of abnormal edges. This is needed for correctness
132 on ports like Alpha and MIPS, whose pic_offset_table_rtx can die on
133 exception edges even though it is generally treated as call-saved
134 for the majority of the compilation. Moving across abnormal edges
135 isn't going to be interesting for shrink-wrap usage anyway. */
136 if (live_edge
->flags
& EDGE_ABNORMAL
)
139 /* When live_edge->dest->preds == 2, we can create a new block on
140 the edge to make it meet the requirement. */
141 if (EDGE_COUNT (live_edge
->dest
->preds
) > 2)
147 /* Try to move INSN from BB to a successor. Return true on success.
148 USES and DEFS are the set of registers that are used and defined
149 after INSN in BB. SPLIT_P indicates whether a live edge from BB
150 is splitted or not. */
153 move_insn_for_shrink_wrap (basic_block bb
, rtx_insn
*insn
,
154 const_hard_reg_set uses
,
155 const_hard_reg_set defs
,
157 struct dead_debug_local
*debug
)
160 bitmap live_out
, live_in
, bb_uses
= NULL
, bb_defs
= NULL
;
161 unsigned int i
, dregno
, end_dregno
;
162 unsigned int sregno
= FIRST_PSEUDO_REGISTER
;
163 unsigned int end_sregno
= FIRST_PSEUDO_REGISTER
;
164 basic_block next_block
;
169 /* Look for a simple register assignment. We don't use single_set here
170 because we can't deal with any CLOBBERs, USEs, or REG_UNUSED secondary
174 set
= PATTERN (insn
);
175 if (GET_CODE (set
) != SET
)
178 dest
= SET_DEST (set
);
180 /* For the destination, we want only a register. Also disallow STACK
181 or FRAME related adjustments. They are likely part of the prologue,
182 so keep them in the entry block. */
184 || dest
== stack_pointer_rtx
185 || dest
== frame_pointer_rtx
186 || dest
== hard_frame_pointer_rtx
)
189 /* For the source, we want one of:
190 (1) A (non-overlapping) register
192 (3) An expression involving no more than one register.
194 That last point comes from the code following, which was originally
195 written to handle only register move operations, and still only handles
196 a single source register when checking for overlaps. Happily, the
197 same checks can be applied to expressions like (plus reg const). */
199 if (CONSTANT_P (src
))
201 else if (!REG_P (src
))
203 rtx src_inner
= NULL_RTX
;
205 if (can_throw_internal (insn
))
208 subrtx_var_iterator::array_type array
;
209 FOR_EACH_SUBRTX_VAR (iter
, array
, src
, ALL
)
212 switch (GET_RTX_CLASS (GET_CODE (x
)))
216 case RTX_COMM_COMPARE
:
221 /* Constant or expression. Continue. */
226 switch (GET_CODE (x
))
230 case STRICT_LOW_PART
:
237 /* Fail if we see a second inner register. */
238 if (src_inner
!= NULL
)
253 if (src_inner
!= NULL
)
257 /* Make sure that the source register isn't defined later in BB. */
260 sregno
= REGNO (src
);
261 end_sregno
= END_REGNO (src
);
262 if (overlaps_hard_reg_set_p (defs
, GET_MODE (src
), sregno
))
266 /* Make sure that the destination register isn't referenced later in BB. */
267 dregno
= REGNO (dest
);
268 end_dregno
= END_REGNO (dest
);
269 if (overlaps_hard_reg_set_p (uses
, GET_MODE (dest
), dregno
)
270 || overlaps_hard_reg_set_p (defs
, GET_MODE (dest
), dregno
))
273 /* See whether there is a successor block to which we could move INSN. */
274 live_edge
= live_edge_for_reg (bb
, dregno
, end_dregno
);
278 next_block
= live_edge
->dest
;
279 /* Create a new basic block on the edge. */
280 if (EDGE_COUNT (next_block
->preds
) == 2)
282 /* split_edge for a block with only one successor is meaningless. */
283 if (EDGE_COUNT (bb
->succs
) == 1)
286 /* If DF_LIVE doesn't exist, i.e. at -O1, just give up. */
290 basic_block old_dest
= live_edge
->dest
;
291 next_block
= split_edge (live_edge
);
293 /* We create a new basic block. Call df_grow_bb_info to make sure
294 all data structures are allocated. */
295 df_grow_bb_info (df_live
);
297 bitmap_and (df_get_live_in (next_block
), df_get_live_out (bb
),
298 df_get_live_in (old_dest
));
299 df_set_bb_dirty (next_block
);
301 /* We should not split more than once for a function. */
308 /* At this point we are committed to moving INSN, but let's try to
309 move it as far as we can. */
312 if (MAY_HAVE_DEBUG_BIND_INSNS
)
314 FOR_BB_INSNS_REVERSE (bb
, dinsn
)
315 if (DEBUG_BIND_INSN_P (dinsn
))
318 FOR_EACH_INSN_USE (use
, dinsn
)
319 if (refers_to_regno_p (dregno
, end_dregno
,
320 DF_REF_REG (use
), (rtx
*) NULL
))
321 dead_debug_add (debug
, use
, DF_REF_REGNO (use
));
323 else if (dinsn
== insn
)
326 live_out
= df_get_live_out (bb
);
327 live_in
= df_get_live_in (next_block
);
330 /* Check whether BB uses DEST or clobbers DEST. We need to add
331 INSN to BB if so. Either way, DEST is no longer live on entry,
332 except for any part that overlaps SRC (next loop). */
335 bb_uses
= &DF_LR_BB_INFO (bb
)->use
;
336 bb_defs
= &DF_LR_BB_INFO (bb
)->def
;
340 for (i
= dregno
; i
< end_dregno
; i
++)
343 || REGNO_REG_SET_P (bb_uses
, i
)
344 || REGNO_REG_SET_P (bb_defs
, i
)
345 || REGNO_REG_SET_P (&DF_LIVE_BB_INFO (bb
)->gen
, i
))
347 CLEAR_REGNO_REG_SET (live_out
, i
);
348 CLEAR_REGNO_REG_SET (live_in
, i
);
351 /* Check whether BB clobbers SRC. We need to add INSN to BB if so.
352 Either way, SRC is now live on entry. */
353 for (i
= sregno
; i
< end_sregno
; i
++)
356 || REGNO_REG_SET_P (bb_defs
, i
)
357 || REGNO_REG_SET_P (&DF_LIVE_BB_INFO (bb
)->gen
, i
))
359 SET_REGNO_REG_SET (live_out
, i
);
360 SET_REGNO_REG_SET (live_in
, i
);
365 /* DF_LR_BB_INFO (bb)->def does not comprise the DF_REF_PARTIAL and
366 DF_REF_CONDITIONAL defs. So if DF_LIVE doesn't exist, i.e.
367 at -O1, just give up searching NEXT_BLOCK. */
369 for (i
= dregno
; i
< end_dregno
; i
++)
371 CLEAR_REGNO_REG_SET (live_out
, i
);
372 CLEAR_REGNO_REG_SET (live_in
, i
);
375 for (i
= sregno
; i
< end_sregno
; i
++)
377 SET_REGNO_REG_SET (live_out
, i
);
378 SET_REGNO_REG_SET (live_in
, i
);
382 /* If we don't need to add the move to BB, look for a single
386 live_edge
= live_edge_for_reg (next_block
, dregno
, end_dregno
);
387 if (!live_edge
|| EDGE_COUNT (live_edge
->dest
->preds
) > 1)
389 next_block
= live_edge
->dest
;
394 /* For the new created basic block, there is no dataflow info at all.
395 So skip the following dataflow update and check. */
398 /* BB now defines DEST. It only uses the parts of DEST that overlap SRC
400 for (i
= dregno
; i
< end_dregno
; i
++)
402 CLEAR_REGNO_REG_SET (bb_uses
, i
);
403 SET_REGNO_REG_SET (bb_defs
, i
);
406 /* BB now uses SRC. */
407 for (i
= sregno
; i
< end_sregno
; i
++)
408 SET_REGNO_REG_SET (bb_uses
, i
);
411 /* Insert debug temps for dead REGs used in subsequent debug insns. */
412 if (debug
->used
&& !bitmap_empty_p (debug
->used
))
413 FOR_EACH_INSN_DEF (def
, insn
)
414 dead_debug_insert_temp (debug
, DF_REF_REGNO (def
), insn
,
415 DEBUG_TEMP_BEFORE_WITH_VALUE
);
417 rtx_insn
*insn_copy
= emit_insn_after (PATTERN (insn
), bb_note (bb
));
418 /* Update the LABEL_NUSES count on any referenced labels. The ideal
419 solution here would be to actually move the instruction instead
420 of copying/deleting it as this loses some notations on the
422 mark_jump_label (PATTERN (insn
), insn_copy
, 0);
427 /* Look for register copies in the first block of the function, and move
428 them down into successor blocks if the register is used only on one
429 path. This exposes more opportunities for shrink-wrapping. These
430 kinds of sets often occur when incoming argument registers are moved
431 to call-saved registers because their values are live across one or
432 more calls during the function. */
435 prepare_shrink_wrap (basic_block entry_block
)
437 rtx_insn
*insn
, *curr
;
439 HARD_REG_SET uses
, defs
;
441 bool split_p
= false;
443 struct dead_debug_local debug
;
445 if (JUMP_P (BB_END (entry_block
)))
447 /* To have more shrink-wrapping opportunities, prepare_shrink_wrap tries
448 to sink the copies from parameter to callee saved register out of
449 entry block. copyprop_hardreg_forward_bb_without_debug_insn is called
450 to release some dependences. */
451 copyprop_hardreg_forward_bb_without_debug_insn (entry_block
);
454 dead_debug_local_init (&debug
, NULL
, NULL
);
455 CLEAR_HARD_REG_SET (uses
);
456 CLEAR_HARD_REG_SET (defs
);
458 FOR_BB_INSNS_REVERSE_SAFE (entry_block
, insn
, curr
)
459 if (NONDEBUG_INSN_P (insn
)
460 && !move_insn_for_shrink_wrap (entry_block
, insn
, uses
, defs
,
463 /* Add all defined registers to DEFs. */
464 FOR_EACH_INSN_DEF (def
, insn
)
466 x
= DF_REF_REG (def
);
467 if (REG_P (x
) && HARD_REGISTER_P (x
))
468 for (i
= REGNO (x
); i
< END_REGNO (x
); i
++)
469 SET_HARD_REG_BIT (defs
, i
);
472 /* Add all used registers to USESs. */
473 FOR_EACH_INSN_USE (use
, insn
)
475 x
= DF_REF_REG (use
);
476 if (REG_P (x
) && HARD_REGISTER_P (x
))
477 for (i
= REGNO (x
); i
< END_REGNO (x
); i
++)
478 SET_HARD_REG_BIT (uses
, i
);
482 dead_debug_local_finish (&debug
, NULL
);
485 /* Return whether basic block PRO can get the prologue. It cannot if it
486 has incoming complex edges that need a prologue inserted (we make a new
487 block for the prologue, so those edges would need to be redirected, which
488 does not work). It also cannot if there exist registers live on entry
489 to PRO that are clobbered by the prologue. */
492 can_get_prologue (basic_block pro
, HARD_REG_SET prologue_clobbered
)
496 FOR_EACH_EDGE (e
, ei
, pro
->preds
)
497 if (e
->flags
& EDGE_COMPLEX
498 && !dominated_by_p (CDI_DOMINATORS
, e
->src
, pro
))
502 REG_SET_TO_HARD_REG_SET (live
, df_get_live_in (pro
));
503 if (hard_reg_set_intersect_p (live
, prologue_clobbered
))
509 /* Return whether we can duplicate basic block BB for shrink wrapping. We
510 cannot if the block cannot be duplicated at all, or if any of its incoming
511 edges are complex and come from a block that does not require a prologue
512 (we cannot redirect such edges), or if the block is too big to copy.
513 PRO is the basic block before which we would put the prologue, MAX_SIZE is
514 the maximum size block we allow to be copied. */
517 can_dup_for_shrink_wrapping (basic_block bb
, basic_block pro
, unsigned max_size
)
519 if (!can_duplicate_block_p (bb
))
524 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
525 if (e
->flags
& (EDGE_COMPLEX
| EDGE_CROSSING
)
526 && !dominated_by_p (CDI_DOMINATORS
, e
->src
, pro
))
532 FOR_BB_INSNS (bb
, insn
)
533 if (NONDEBUG_INSN_P (insn
))
535 size
+= get_attr_min_length (insn
);
543 /* Do whatever needs to be done for exits that run without prologue.
544 Sibcalls need nothing done. Normal exits get a simple_return inserted. */
547 handle_simple_exit (edge e
)
550 if (e
->flags
& EDGE_SIBCALL
)
552 /* Tell function.c to take no further action on this edge. */
553 e
->flags
|= EDGE_IGNORE
;
555 e
->flags
&= ~EDGE_FALLTHRU
;
556 emit_barrier_after_bb (e
->src
);
560 /* If the basic block the edge comes from has multiple successors,
562 if (EDGE_COUNT (e
->src
->succs
) > 1)
564 basic_block old_bb
= e
->src
;
565 rtx_insn
*end
= BB_END (old_bb
);
566 rtx_note
*note
= emit_note_after (NOTE_INSN_DELETED
, end
);
567 basic_block new_bb
= create_basic_block (note
, note
, old_bb
);
568 BB_COPY_PARTITION (new_bb
, old_bb
);
569 BB_END (old_bb
) = end
;
571 redirect_edge_succ (e
, new_bb
);
572 new_bb
->count
= e
->count ();
573 e
->flags
|= EDGE_FALLTHRU
;
575 e
= make_single_succ_edge (new_bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
578 e
->flags
&= ~EDGE_FALLTHRU
;
579 rtx_jump_insn
*ret
= emit_jump_insn_after (targetm
.gen_simple_return (),
581 JUMP_LABEL (ret
) = simple_return_rtx
;
582 emit_barrier_after_bb (e
->src
);
585 fprintf (dump_file
, "Made simple_return with UID %d in bb %d\n",
586 INSN_UID (ret
), e
->src
->index
);
589 /* Try to perform a kind of shrink-wrapping, making sure the
590 prologue/epilogue is emitted only around those parts of the
591 function that require it.
593 There will be exactly one prologue, and it will be executed either
594 zero or one time, on any path. Depending on where the prologue is
595 placed, some of the basic blocks can be reached via both paths with
596 and without a prologue. Such blocks will be duplicated here, and the
597 edges changed to match.
599 Paths that go to the exit without going through the prologue will use
600 a simple_return instead of the epilogue. We maximize the number of
601 those, making sure to only duplicate blocks that can be duplicated.
602 If the prologue can then still be placed in multiple locations, we
603 place it as early as possible.
605 An example, where we duplicate blocks with control flow (legend:
606 _B_egin, _R_eturn and _S_imple_return; edges without arrowhead should
607 be taken to point down or to the right, to simplify the diagram; here,
608 block 3 needs a prologue, the rest does not):
626 (bb 4 is duplicated to 7, and so on; the prologue is inserted on the
629 Another example, where part of a loop is duplicated (again, bb 3 is
630 the only block that needs a prologue):
635 | v | becomes | | v |
636 2---4--- 2---5-- 4---
641 (bb 4 is duplicated to 5; the prologue is inserted on the edge 5->3).
643 ENTRY_EDGE is the edge where the prologue will be placed, possibly
644 changed by this function. PROLOGUE_SEQ is the prologue we will insert. */
647 try_shrink_wrapping (edge
*entry_edge
, rtx_insn
*prologue_seq
)
649 /* If we cannot shrink-wrap, are told not to shrink-wrap, or it makes
650 no sense to shrink-wrap: then do not shrink-wrap! */
652 if (!SHRINK_WRAPPING_ENABLED
)
655 if (crtl
->profile
&& !targetm
.profile_before_prologue ())
658 if (crtl
->calls_eh_return
)
661 bool empty_prologue
= true;
662 for (rtx_insn
*insn
= prologue_seq
; insn
; insn
= NEXT_INSN (insn
))
663 if (!(NOTE_P (insn
) && NOTE_KIND (insn
) == NOTE_INSN_PROLOGUE_END
))
665 empty_prologue
= false;
671 /* Move some code down to expose more shrink-wrapping opportunities. */
673 basic_block entry
= (*entry_edge
)->dest
;
674 prepare_shrink_wrap (entry
);
677 fprintf (dump_file
, "Attempting shrink-wrapping optimization.\n");
679 /* Compute the registers set and used in the prologue. */
681 HARD_REG_SET prologue_clobbered
, prologue_used
;
682 CLEAR_HARD_REG_SET (prologue_clobbered
);
683 CLEAR_HARD_REG_SET (prologue_used
);
684 for (rtx_insn
*insn
= prologue_seq
; insn
; insn
= NEXT_INSN (insn
))
685 if (NONDEBUG_INSN_P (insn
))
687 HARD_REG_SET this_used
;
688 CLEAR_HARD_REG_SET (this_used
);
689 note_uses (&PATTERN (insn
), record_hard_reg_uses
, &this_used
);
690 this_used
&= ~prologue_clobbered
;
691 prologue_used
|= this_used
;
692 note_stores (insn
, record_hard_reg_sets
, &prologue_clobbered
);
694 CLEAR_HARD_REG_BIT (prologue_clobbered
, STACK_POINTER_REGNUM
);
695 if (frame_pointer_needed
)
696 CLEAR_HARD_REG_BIT (prologue_clobbered
, HARD_FRAME_POINTER_REGNUM
);
698 /* Find out what registers are set up by the prologue; any use of these
699 cannot happen before the prologue. */
701 struct hard_reg_set_container set_up_by_prologue
;
702 CLEAR_HARD_REG_SET (set_up_by_prologue
.set
);
703 add_to_hard_reg_set (&set_up_by_prologue
.set
, Pmode
, STACK_POINTER_REGNUM
);
704 add_to_hard_reg_set (&set_up_by_prologue
.set
, Pmode
, ARG_POINTER_REGNUM
);
705 if (frame_pointer_needed
)
706 add_to_hard_reg_set (&set_up_by_prologue
.set
, Pmode
,
707 HARD_FRAME_POINTER_REGNUM
);
708 if (pic_offset_table_rtx
709 && (unsigned) PIC_OFFSET_TABLE_REGNUM
!= INVALID_REGNUM
)
710 add_to_hard_reg_set (&set_up_by_prologue
.set
, Pmode
,
711 PIC_OFFSET_TABLE_REGNUM
);
713 add_to_hard_reg_set (&set_up_by_prologue
.set
,
714 GET_MODE (crtl
->drap_reg
),
715 REGNO (crtl
->drap_reg
));
716 if (targetm
.set_up_by_prologue
)
717 targetm
.set_up_by_prologue (&set_up_by_prologue
);
719 /* We will insert the prologue before the basic block PRO. PRO should
720 dominate all basic blocks that need the prologue to be executed
721 before them. First, make PRO the "tightest wrap" possible. */
723 calculate_dominance_info (CDI_DOMINATORS
);
730 FOR_EACH_BB_FN (bb
, cfun
)
733 FOR_BB_INSNS (bb
, insn
)
734 if (NONDEBUG_INSN_P (insn
)
735 && requires_stack_frame_p (insn
, prologue_used
,
736 set_up_by_prologue
.set
))
740 fprintf (dump_file
, "Block %d needs prologue due to insn %d:\n",
741 bb
->index
, INSN_UID (insn
));
742 print_rtl_single (dump_file
, insn
);
744 pro
= nearest_common_dominator (CDI_DOMINATORS
, pro
, bb
);
749 /* If nothing needs a prologue, just put it at the start. This really
750 shouldn't happen, but we cannot fix it here. */
755 fprintf(dump_file
, "Nothing needs a prologue, but it isn't empty; "
756 "putting it at the start.\n");
761 fprintf (dump_file
, "After wrapping required blocks, PRO is now %d\n",
764 /* Now see if we can put the prologue at the start of PRO. Putting it
765 there might require duplicating a block that cannot be duplicated,
766 or in some cases we cannot insert the prologue there at all. If PRO
767 wont't do, try again with the immediate dominator of PRO, and so on.
769 The blocks that need duplicating are those reachable from PRO but
770 not dominated by it. We keep in BB_WITH a bitmap of the blocks
771 reachable from PRO that we already found, and in VEC a stack of
772 those we still need to consider (to find successors). */
775 bitmap_set_bit (bb_with
, pro
->index
);
777 vec
<basic_block
> vec
;
778 vec
.create (n_basic_blocks_for_fn (cfun
));
779 vec
.quick_push (pro
);
781 unsigned max_grow_size
= get_uncond_jump_length ();
782 max_grow_size
*= param_max_grow_copy_bb_insns
;
784 while (!vec
.is_empty () && pro
!= entry
)
786 while (pro
!= entry
&& !can_get_prologue (pro
, prologue_clobbered
))
788 pro
= get_immediate_dominator (CDI_DOMINATORS
, pro
);
790 if (bitmap_set_bit (bb_with
, pro
->index
))
791 vec
.quick_push (pro
);
794 basic_block bb
= vec
.pop ();
795 if (!can_dup_for_shrink_wrapping (bb
, pro
, max_grow_size
))
796 while (!dominated_by_p (CDI_DOMINATORS
, bb
, pro
))
798 gcc_assert (pro
!= entry
);
800 pro
= get_immediate_dominator (CDI_DOMINATORS
, pro
);
802 if (bitmap_set_bit (bb_with
, pro
->index
))
803 vec
.quick_push (pro
);
806 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
807 if (e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
)
808 && bitmap_set_bit (bb_with
, e
->dest
->index
))
809 vec
.quick_push (e
->dest
);
813 fprintf (dump_file
, "Avoiding non-duplicatable blocks, PRO is now %d\n",
816 /* If we can move PRO back without having to duplicate more blocks, do so.
817 We do this because putting the prologue earlier is better for scheduling.
819 We can move back to a block PRE if every path from PRE will eventually
820 need a prologue, that is, PRO is a post-dominator of PRE. PRE needs
821 to dominate every block reachable from itself. We keep in BB_TMP a
822 bitmap of the blocks reachable from PRE that we already found, and in
823 VEC a stack of those we still need to consider.
825 Any block reachable from PRE is also reachable from all predecessors
826 of PRE, so if we find we need to move PRE back further we can leave
827 everything not considered so far on the stack. Any block dominated
828 by PRE is also dominated by all other dominators of PRE, so anything
829 found good for some PRE does not need to be reconsidered later.
831 We don't need to update BB_WITH because none of the new blocks found
832 can jump to a block that does not need the prologue. */
836 calculate_dominance_info (CDI_POST_DOMINATORS
);
839 bitmap_copy (bb_tmp
, bb_with
);
840 basic_block last_ok
= pro
;
845 basic_block pre
= get_immediate_dominator (CDI_DOMINATORS
, pro
);
846 if (!dominated_by_p (CDI_POST_DOMINATORS
, pre
, pro
))
849 if (bitmap_set_bit (bb_tmp
, pre
->index
))
850 vec
.quick_push (pre
);
853 while (!vec
.is_empty ())
855 if (!dominated_by_p (CDI_DOMINATORS
, vec
.last (), pre
))
861 basic_block bb
= vec
.pop ();
862 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
863 if (bitmap_set_bit (bb_tmp
, e
->dest
->index
))
864 vec
.quick_push (e
->dest
);
867 if (ok
&& can_get_prologue (pre
, prologue_clobbered
))
875 free_dominance_info (CDI_POST_DOMINATORS
);
881 fprintf (dump_file
, "Bumping back to anticipatable blocks, PRO is now %d\n",
886 free_dominance_info (CDI_DOMINATORS
);
890 /* Compute what fraction of the frequency and count of the blocks that run
891 both with and without prologue are for running with prologue. This gives
892 the correct answer for reducible flow graphs; for irreducible flow graphs
893 our profile is messed up beyond repair anyway. */
895 profile_count num
= profile_count::zero ();
896 profile_count den
= profile_count::zero ();
898 FOR_EACH_EDGE (e
, ei
, pro
->preds
)
899 if (!dominated_by_p (CDI_DOMINATORS
, e
->src
, pro
))
901 if (e
->count ().initialized_p ())
903 if (e
->src
->count
.initialized_p ())
904 den
+= e
->src
->count
;
907 /* All is okay, so do it. */
909 crtl
->shrink_wrapped
= true;
911 fprintf (dump_file
, "Performing shrink-wrapping.\n");
913 /* Copy the blocks that can run both with and without prologue. The
914 originals run with prologue, the copies without. Store a pointer to
915 the copy in the ->aux field of the original. */
917 FOR_EACH_BB_FN (bb
, cfun
)
918 if (bitmap_bit_p (bb_with
, bb
->index
)
919 && !dominated_by_p (CDI_DOMINATORS
, bb
, pro
))
921 basic_block dup
= duplicate_block (bb
, 0, 0);
925 if (JUMP_P (BB_END (dup
)) && !any_condjump_p (BB_END (dup
)))
926 emit_barrier_after_bb (dup
);
928 if (EDGE_COUNT (dup
->succs
) == 0)
929 emit_barrier_after_bb (dup
);
932 fprintf (dump_file
, "Duplicated %d to %d\n", bb
->index
, dup
->index
);
934 if (num
== profile_count::zero () || den
.nonzero_p ())
935 bb
->count
= bb
->count
.apply_scale (num
, den
);
936 dup
->count
-= bb
->count
;
939 /* Now change the edges to point to the copies, where appropriate. */
941 FOR_EACH_BB_FN (bb
, cfun
)
942 if (!dominated_by_p (CDI_DOMINATORS
, bb
, pro
))
944 basic_block src
= bb
;
945 if (bitmap_bit_p (bb_with
, bb
->index
))
946 src
= (basic_block
) bb
->aux
;
948 FOR_EACH_EDGE (e
, ei
, src
->succs
)
950 if (e
->dest
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
953 if (bitmap_bit_p (bb_with
, e
->dest
->index
)
954 && !dominated_by_p (CDI_DOMINATORS
, e
->dest
, pro
))
957 fprintf (dump_file
, "Redirecting edge %d->%d to %d\n",
958 e
->src
->index
, e
->dest
->index
,
959 ((basic_block
) e
->dest
->aux
)->index
);
960 redirect_edge_and_branch_force (e
, (basic_block
) e
->dest
->aux
);
962 else if (e
->flags
& EDGE_FALLTHRU
963 && bitmap_bit_p (bb_with
, bb
->index
))
964 force_nonfallthru (e
);
968 /* Also redirect the function entry edge if necessary. */
970 FOR_EACH_EDGE (e
, ei
, ENTRY_BLOCK_PTR_FOR_FN (cfun
)->succs
)
971 if (bitmap_bit_p (bb_with
, e
->dest
->index
)
972 && !dominated_by_p (CDI_DOMINATORS
, e
->dest
, pro
))
974 basic_block split_bb
= split_edge (e
);
975 e
= single_succ_edge (split_bb
);
976 redirect_edge_and_branch_force (e
, (basic_block
) e
->dest
->aux
);
979 /* Make a simple_return for those exits that run without prologue. */
981 FOR_EACH_BB_REVERSE_FN (bb
, cfun
)
982 if (!bitmap_bit_p (bb_with
, bb
->index
))
983 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
984 if (e
->dest
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
985 handle_simple_exit (e
);
987 /* Finally, we want a single edge to put the prologue on. Make a new
988 block before the PRO block; the edge beteen them is the edge we want.
989 Then redirect those edges into PRO that come from blocks without the
990 prologue, to point to the new block instead. The new prologue block
991 is put at the end of the insn chain. */
993 basic_block new_bb
= create_empty_bb (EXIT_BLOCK_PTR_FOR_FN (cfun
)->prev_bb
);
994 BB_COPY_PARTITION (new_bb
, pro
);
995 new_bb
->count
= profile_count::zero ();
997 fprintf (dump_file
, "Made prologue block %d\n", new_bb
->index
);
999 for (ei
= ei_start (pro
->preds
); (e
= ei_safe_edge (ei
)); )
1001 if (bitmap_bit_p (bb_with
, e
->src
->index
)
1002 || dominated_by_p (CDI_DOMINATORS
, e
->src
, pro
))
1008 new_bb
->count
+= e
->count ();
1010 redirect_edge_and_branch_force (e
, new_bb
);
1012 fprintf (dump_file
, "Redirected edge from %d\n", e
->src
->index
);
1015 *entry_edge
= make_single_succ_edge (new_bb
, pro
, EDGE_FALLTHRU
);
1016 force_nonfallthru (*entry_edge
);
1018 free_dominance_info (CDI_DOMINATORS
);
1021 /* Separate shrink-wrapping
1023 Instead of putting all of the prologue and epilogue in one spot, we
1024 can put parts of it in places where those components are executed less
1025 frequently. The following code does this, for prologue and epilogue
1026 components that can be put in more than one location, and where those
1027 components can be executed more than once (the epilogue component will
1028 always be executed before the prologue component is executed a second
1031 What exactly is a component is target-dependent. The more usual
1032 components are simple saves/restores to/from the frame of callee-saved
1033 registers. This code treats components abstractly (as an sbitmap),
1034 letting the target handle all details.
1036 Prologue components are placed in such a way that for every component
1037 the prologue is executed as infrequently as possible. We do this by
1038 walking the dominator tree, comparing the cost of placing a prologue
1039 component before a block to the sum of costs determined for all subtrees
1042 From this placement, we then determine for each component all blocks
1043 where at least one of this block's dominators (including itself) will
1044 get a prologue inserted. That then is how the components are placed.
1045 We could place the epilogue components a bit smarter (we can save a
1046 bit of code size sometimes); this is a possible future improvement.
1048 Prologues and epilogues are preferably placed into a block, either at
1049 the beginning or end of it, if it is needed for all predecessor resp.
1050 successor edges; or placed on the edge otherwise.
1052 If the placement of any prologue/epilogue leads to a situation we cannot
1053 handle (for example, an abnormal edge would need to be split, or some
1054 targets want to use some specific registers that may not be available
1055 where we want to put them), separate shrink-wrapping for the components
1056 in that prologue/epilogue is aborted. */
1059 /* Print the sbitmap COMPONENTS to the DUMP_FILE if not empty, with the
1062 dump_components (const char *label
, sbitmap components
)
1064 if (bitmap_empty_p (components
))
1067 fprintf (dump_file
, " [%s", label
);
1069 for (unsigned int j
= 0; j
< components
->n_bits
; j
++)
1070 if (bitmap_bit_p (components
, j
))
1071 fprintf (dump_file
, " %u", j
);
1073 fprintf (dump_file
, "]");
1076 /* The data we collect for each bb. */
1078 /* What components does this BB need? */
1079 sbitmap needs_components
;
1081 /* What components does this BB have? This is the main decision this
1083 sbitmap has_components
;
1085 /* The components for which we placed code at the start of the BB (instead
1086 of on all incoming edges). */
1087 sbitmap head_components
;
1089 /* The components for which we placed code at the end of the BB (instead
1090 of on all outgoing edges). */
1091 sbitmap tail_components
;
1093 /* The frequency of executing the prologue for this BB, if a prologue is
1094 placed on this BB. This is a pessimistic estimate (no prologue is
1095 needed for edges from blocks that have the component under consideration
1099 /* The frequency of executing the prologue for this BB and all BBs
1101 gcov_type total_cost
;
1104 /* A helper function for accessing the pass-specific info. */
1105 static inline struct sw
*
1108 gcc_assert (bb
->aux
);
1109 return (struct sw
*) bb
->aux
;
1112 /* Create the pass-specific data structures for separately shrink-wrapping
1113 with components COMPONENTS. */
1115 init_separate_shrink_wrap (sbitmap components
)
1118 FOR_ALL_BB_FN (bb
, cfun
)
1120 bb
->aux
= xcalloc (1, sizeof (struct sw
));
1122 SW (bb
)->needs_components
= targetm
.shrink_wrap
.components_for_bb (bb
);
1124 /* Mark all basic blocks without successor as needing all components.
1125 This avoids problems in at least cfgcleanup, sel-sched, and
1126 regrename (largely to do with all paths to such a block still
1127 needing the same dwarf CFI info). */
1128 if (EDGE_COUNT (bb
->succs
) == 0)
1129 bitmap_copy (SW (bb
)->needs_components
, components
);
1133 fprintf (dump_file
, "bb %d components:", bb
->index
);
1134 dump_components ("has", SW (bb
)->needs_components
);
1135 fprintf (dump_file
, "\n");
1138 SW (bb
)->has_components
= sbitmap_alloc (SBITMAP_SIZE (components
));
1139 SW (bb
)->head_components
= sbitmap_alloc (SBITMAP_SIZE (components
));
1140 SW (bb
)->tail_components
= sbitmap_alloc (SBITMAP_SIZE (components
));
1141 bitmap_clear (SW (bb
)->has_components
);
1145 /* Destroy the pass-specific data. */
1147 fini_separate_shrink_wrap (void)
1150 FOR_ALL_BB_FN (bb
, cfun
)
1153 sbitmap_free (SW (bb
)->needs_components
);
1154 sbitmap_free (SW (bb
)->has_components
);
1155 sbitmap_free (SW (bb
)->head_components
);
1156 sbitmap_free (SW (bb
)->tail_components
);
1162 /* Place the prologue for component WHICH, in the basic blocks dominated
1163 by HEAD. Do a DFS over the dominator tree, and set bit WHICH in the
1164 HAS_COMPONENTS of a block if either the block has that bit set in
1165 NEEDS_COMPONENTS, or it is cheaper to place the prologue here than in all
1166 dominator subtrees separately. */
1168 place_prologue_for_one_component (unsigned int which
, basic_block head
)
1170 /* The block we are currently dealing with. */
1171 basic_block bb
= head
;
1172 /* Is this the first time we visit this block, i.e. have we just gone
1174 bool first_visit
= true;
1176 /* Walk the dominator tree, visit one block per iteration of this loop.
1177 Each basic block is visited twice: once before visiting any children
1178 of the block, and once after visiting all of them (leaf nodes are
1179 visited only once). As an optimization, we do not visit subtrees
1180 that can no longer influence the prologue placement. */
1183 /* First visit of a block: set the (children) cost accumulator to zero;
1184 if the block does not have the component itself, walk down. */
1187 /* Initialize the cost. The cost is the block execution frequency
1188 that does not come from backedges. Calculating this by simply
1189 adding the cost of all edges that aren't backedges does not
1190 work: this does not always add up to the block frequency at
1191 all, and even if it does, rounding error makes for bad
1193 SW (bb
)->own_cost
= bb
->count
.to_frequency (cfun
);
1197 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1198 if (dominated_by_p (CDI_DOMINATORS
, e
->src
, bb
))
1200 if (SW (bb
)->own_cost
> EDGE_FREQUENCY (e
))
1201 SW (bb
)->own_cost
-= EDGE_FREQUENCY (e
);
1203 SW (bb
)->own_cost
= 0;
1206 SW (bb
)->total_cost
= 0;
1208 if (!bitmap_bit_p (SW (bb
)->needs_components
, which
)
1209 && first_dom_son (CDI_DOMINATORS
, bb
))
1211 bb
= first_dom_son (CDI_DOMINATORS
, bb
);
1216 /* If this block does need the component itself, or it is cheaper to
1217 put the prologue here than in all the descendants that need it,
1218 mark it so. If this block's immediate post-dominator is dominated
1219 by this block, and that needs the prologue, we can put it on this
1220 block as well (earlier is better). */
1221 if (bitmap_bit_p (SW (bb
)->needs_components
, which
)
1222 || SW (bb
)->total_cost
> SW (bb
)->own_cost
)
1224 SW (bb
)->total_cost
= SW (bb
)->own_cost
;
1225 bitmap_set_bit (SW (bb
)->has_components
, which
);
1229 basic_block kid
= get_immediate_dominator (CDI_POST_DOMINATORS
, bb
);
1230 if (dominated_by_p (CDI_DOMINATORS
, kid
, bb
)
1231 && bitmap_bit_p (SW (kid
)->has_components
, which
))
1233 SW (bb
)->total_cost
= SW (bb
)->own_cost
;
1234 bitmap_set_bit (SW (bb
)->has_components
, which
);
1238 /* We are back where we started, so we are done now. */
1242 /* We now know the cost of the subtree rooted at the current block.
1243 Accumulate this cost in the parent. */
1244 basic_block parent
= get_immediate_dominator (CDI_DOMINATORS
, bb
);
1245 SW (parent
)->total_cost
+= SW (bb
)->total_cost
;
1247 /* Don't walk the tree down unless necessary. */
1248 if (next_dom_son (CDI_DOMINATORS
, bb
)
1249 && SW (parent
)->total_cost
<= SW (parent
)->own_cost
)
1251 bb
= next_dom_son (CDI_DOMINATORS
, bb
);
1257 first_visit
= false;
1262 /* Set HAS_COMPONENTS in every block to the maximum it can be set to without
1263 setting it on any path from entry to exit where it was not already set
1264 somewhere (or, for blocks that have no path to the exit, consider only
1265 paths from the entry to the block itself). Return whether any changes
1266 were made to some HAS_COMPONENTS. */
1268 spread_components (sbitmap components
)
1270 basic_block entry_block
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
1271 basic_block exit_block
= EXIT_BLOCK_PTR_FOR_FN (cfun
);
1273 /* A stack of all blocks left to consider, and a bitmap of all blocks
1275 vec
<basic_block
> todo
;
1276 todo
.create (n_basic_blocks_for_fn (cfun
));
1279 auto_sbitmap
old (SBITMAP_SIZE (components
));
1281 /* Find for every block the components that are *not* needed on some path
1282 from the entry to that block. Do this with a flood fill from the entry
1283 block. Every block can be visited at most as often as the number of
1284 components (plus one), and usually much less often. */
1287 fprintf (dump_file
, "Spreading down...\n");
1290 FOR_ALL_BB_FN (bb
, cfun
)
1291 bitmap_clear (SW (bb
)->head_components
);
1293 bitmap_copy (SW (entry_block
)->head_components
, components
);
1298 todo
.quick_push (single_succ (entry_block
));
1299 bitmap_set_bit (seen
, single_succ (entry_block
)->index
);
1300 while (!todo
.is_empty ())
1304 bitmap_copy (old
, SW (bb
)->head_components
);
1306 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1307 bitmap_ior (SW (bb
)->head_components
, SW (bb
)->head_components
,
1308 SW (e
->src
)->head_components
);
1310 bitmap_and_compl (SW (bb
)->head_components
, SW (bb
)->head_components
,
1311 SW (bb
)->has_components
);
1313 if (!bitmap_equal_p (old
, SW (bb
)->head_components
))
1314 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1315 if (bitmap_set_bit (seen
, e
->dest
->index
))
1316 todo
.quick_push (e
->dest
);
1318 bitmap_clear_bit (seen
, bb
->index
);
1321 /* Find for every block the components that are *not* needed on some reverse
1322 path from the exit to that block. */
1325 fprintf (dump_file
, "Spreading up...\n");
1327 /* First, mark all blocks not reachable from the exit block as not needing
1328 any component on any path to the exit. Mark everything, and then clear
1329 again by a flood fill. */
1331 FOR_ALL_BB_FN (bb
, cfun
)
1332 bitmap_copy (SW (bb
)->tail_components
, components
);
1334 FOR_EACH_EDGE (e
, ei
, exit_block
->preds
)
1336 todo
.quick_push (e
->src
);
1337 bitmap_set_bit (seen
, e
->src
->index
);
1340 while (!todo
.is_empty ())
1344 if (!bitmap_empty_p (SW (bb
)->tail_components
))
1345 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1346 if (bitmap_set_bit (seen
, e
->src
->index
))
1347 todo
.quick_push (e
->src
);
1349 bitmap_clear (SW (bb
)->tail_components
);
1351 bitmap_clear_bit (seen
, bb
->index
);
1354 /* And then, flood fill backwards to find for every block the components
1355 not needed on some path to the exit. */
1357 bitmap_copy (SW (exit_block
)->tail_components
, components
);
1359 FOR_EACH_EDGE (e
, ei
, exit_block
->preds
)
1361 todo
.quick_push (e
->src
);
1362 bitmap_set_bit (seen
, e
->src
->index
);
1365 while (!todo
.is_empty ())
1369 bitmap_copy (old
, SW (bb
)->tail_components
);
1371 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1372 bitmap_ior (SW (bb
)->tail_components
, SW (bb
)->tail_components
,
1373 SW (e
->dest
)->tail_components
);
1375 bitmap_and_compl (SW (bb
)->tail_components
, SW (bb
)->tail_components
,
1376 SW (bb
)->has_components
);
1378 if (!bitmap_equal_p (old
, SW (bb
)->tail_components
))
1379 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1380 if (bitmap_set_bit (seen
, e
->src
->index
))
1381 todo
.quick_push (e
->src
);
1383 bitmap_clear_bit (seen
, bb
->index
);
1388 /* Finally, mark everything not needed both forwards and backwards. */
1390 bool did_changes
= false;
1392 FOR_EACH_BB_FN (bb
, cfun
)
1394 bitmap_copy (old
, SW (bb
)->has_components
);
1396 bitmap_and (SW (bb
)->head_components
, SW (bb
)->head_components
,
1397 SW (bb
)->tail_components
);
1398 bitmap_and_compl (SW (bb
)->has_components
, components
,
1399 SW (bb
)->head_components
);
1401 if (!did_changes
&& !bitmap_equal_p (old
, SW (bb
)->has_components
))
1405 FOR_ALL_BB_FN (bb
, cfun
)
1409 fprintf (dump_file
, "bb %d components:", bb
->index
);
1410 dump_components ("has", SW (bb
)->has_components
);
1411 fprintf (dump_file
, "\n");
1418 /* If we cannot handle placing some component's prologues or epilogues where
1419 we decided we should place them, unmark that component in COMPONENTS so
1420 that it is not wrapped separately. */
1422 disqualify_problematic_components (sbitmap components
)
1424 auto_sbitmap
pro (SBITMAP_SIZE (components
));
1425 auto_sbitmap
epi (SBITMAP_SIZE (components
));
1428 FOR_EACH_BB_FN (bb
, cfun
)
1432 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1434 /* Find which components we want pro/epilogues for here. */
1435 bitmap_and_compl (epi
, SW (e
->src
)->has_components
,
1436 SW (e
->dest
)->has_components
);
1437 bitmap_and_compl (pro
, SW (e
->dest
)->has_components
,
1438 SW (e
->src
)->has_components
);
1440 /* Ask the target what it thinks about things. */
1441 if (!bitmap_empty_p (epi
))
1442 targetm
.shrink_wrap
.disqualify_components (components
, e
, epi
,
1444 if (!bitmap_empty_p (pro
))
1445 targetm
.shrink_wrap
.disqualify_components (components
, e
, pro
,
1448 /* If this edge doesn't need splitting, we're fine. */
1449 if (single_pred_p (e
->dest
)
1450 && e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
1453 /* If the edge can be split, that is fine too. */
1454 if ((e
->flags
& EDGE_ABNORMAL
) == 0)
1457 /* We also can handle sibcalls. */
1458 if (e
->dest
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
1460 gcc_assert (e
->flags
& EDGE_SIBCALL
);
1464 /* Remove from consideration those components we would need
1465 pro/epilogues for on edges where we cannot insert them. */
1466 bitmap_and_compl (components
, components
, epi
);
1467 bitmap_and_compl (components
, components
, pro
);
1469 if (dump_file
&& !bitmap_subset_p (epi
, components
))
1471 fprintf (dump_file
, " BAD epi %d->%d", e
->src
->index
,
1473 if (e
->flags
& EDGE_EH
)
1474 fprintf (dump_file
, " for EH");
1475 dump_components ("epi", epi
);
1476 fprintf (dump_file
, "\n");
1479 if (dump_file
&& !bitmap_subset_p (pro
, components
))
1481 fprintf (dump_file
, " BAD pro %d->%d", e
->src
->index
,
1483 if (e
->flags
& EDGE_EH
)
1484 fprintf (dump_file
, " for EH");
1485 dump_components ("pro", pro
);
1486 fprintf (dump_file
, "\n");
1492 /* Place code for prologues and epilogues for COMPONENTS where we can put
1493 that code at the start of basic blocks. */
1495 emit_common_heads_for_components (sbitmap components
)
1497 auto_sbitmap
pro (SBITMAP_SIZE (components
));
1498 auto_sbitmap
epi (SBITMAP_SIZE (components
));
1499 auto_sbitmap
tmp (SBITMAP_SIZE (components
));
1502 FOR_ALL_BB_FN (bb
, cfun
)
1503 bitmap_clear (SW (bb
)->head_components
);
1505 FOR_EACH_BB_FN (bb
, cfun
)
1507 /* Find which prologue resp. epilogue components are needed for all
1508 predecessor edges to this block. */
1510 /* First, select all possible components. */
1511 bitmap_copy (epi
, components
);
1512 bitmap_copy (pro
, components
);
1516 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1518 if (e
->flags
& EDGE_ABNORMAL
)
1525 /* Deselect those epilogue components that should not be inserted
1527 bitmap_and_compl (tmp
, SW (e
->src
)->has_components
,
1528 SW (e
->dest
)->has_components
);
1529 bitmap_and (epi
, epi
, tmp
);
1531 /* Similar, for the prologue. */
1532 bitmap_and_compl (tmp
, SW (e
->dest
)->has_components
,
1533 SW (e
->src
)->has_components
);
1534 bitmap_and (pro
, pro
, tmp
);
1537 if (dump_file
&& !(bitmap_empty_p (epi
) && bitmap_empty_p (pro
)))
1538 fprintf (dump_file
, " bb %d", bb
->index
);
1540 if (dump_file
&& !bitmap_empty_p (epi
))
1541 dump_components ("epi", epi
);
1542 if (dump_file
&& !bitmap_empty_p (pro
))
1543 dump_components ("pro", pro
);
1545 if (dump_file
&& !(bitmap_empty_p (epi
) && bitmap_empty_p (pro
)))
1546 fprintf (dump_file
, "\n");
1548 /* Place code after the BB note. */
1549 if (!bitmap_empty_p (pro
))
1552 targetm
.shrink_wrap
.emit_prologue_components (pro
);
1553 rtx_insn
*seq
= get_insns ();
1555 record_prologue_seq (seq
);
1557 emit_insn_after (seq
, bb_note (bb
));
1559 bitmap_ior (SW (bb
)->head_components
, SW (bb
)->head_components
, pro
);
1562 if (!bitmap_empty_p (epi
))
1565 targetm
.shrink_wrap
.emit_epilogue_components (epi
);
1566 rtx_insn
*seq
= get_insns ();
1568 record_epilogue_seq (seq
);
1570 emit_insn_after (seq
, bb_note (bb
));
1572 bitmap_ior (SW (bb
)->head_components
, SW (bb
)->head_components
, epi
);
1577 /* Place code for prologues and epilogues for COMPONENTS where we can put
1578 that code at the end of basic blocks. */
1580 emit_common_tails_for_components (sbitmap components
)
1582 auto_sbitmap
pro (SBITMAP_SIZE (components
));
1583 auto_sbitmap
epi (SBITMAP_SIZE (components
));
1584 auto_sbitmap
tmp (SBITMAP_SIZE (components
));
1587 FOR_ALL_BB_FN (bb
, cfun
)
1588 bitmap_clear (SW (bb
)->tail_components
);
1590 FOR_EACH_BB_FN (bb
, cfun
)
1592 /* Find which prologue resp. epilogue components are needed for all
1593 successor edges from this block. */
1594 if (EDGE_COUNT (bb
->succs
) == 0)
1597 /* First, select all possible components. */
1598 bitmap_copy (epi
, components
);
1599 bitmap_copy (pro
, components
);
1603 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1605 if (e
->flags
& EDGE_ABNORMAL
)
1612 /* Deselect those epilogue components that should not be inserted
1613 for this edge, and also those that are already put at the head
1614 of the successor block. */
1615 bitmap_and_compl (tmp
, SW (e
->src
)->has_components
,
1616 SW (e
->dest
)->has_components
);
1617 bitmap_and_compl (tmp
, tmp
, SW (e
->dest
)->head_components
);
1618 bitmap_and (epi
, epi
, tmp
);
1620 /* Similarly, for the prologue. */
1621 bitmap_and_compl (tmp
, SW (e
->dest
)->has_components
,
1622 SW (e
->src
)->has_components
);
1623 bitmap_and_compl (tmp
, tmp
, SW (e
->dest
)->head_components
);
1624 bitmap_and (pro
, pro
, tmp
);
1627 /* If the last insn of this block is a control flow insn we cannot
1628 put anything after it. We can put our code before it instead,
1629 but only if that jump insn is a simple jump. */
1630 rtx_insn
*last_insn
= BB_END (bb
);
1631 if (control_flow_insn_p (last_insn
) && !simplejump_p (last_insn
))
1637 if (dump_file
&& !(bitmap_empty_p (epi
) && bitmap_empty_p (pro
)))
1638 fprintf (dump_file
, " bb %d", bb
->index
);
1640 if (dump_file
&& !bitmap_empty_p (epi
))
1641 dump_components ("epi", epi
);
1642 if (dump_file
&& !bitmap_empty_p (pro
))
1643 dump_components ("pro", pro
);
1645 if (dump_file
&& !(bitmap_empty_p (epi
) && bitmap_empty_p (pro
)))
1646 fprintf (dump_file
, "\n");
1648 /* Put the code at the end of the BB, but before any final jump. */
1649 if (!bitmap_empty_p (epi
))
1652 targetm
.shrink_wrap
.emit_epilogue_components (epi
);
1653 rtx_insn
*seq
= get_insns ();
1655 record_epilogue_seq (seq
);
1657 if (control_flow_insn_p (last_insn
))
1658 emit_insn_before (seq
, last_insn
);
1660 emit_insn_after (seq
, last_insn
);
1662 bitmap_ior (SW (bb
)->tail_components
, SW (bb
)->tail_components
, epi
);
1665 if (!bitmap_empty_p (pro
))
1668 targetm
.shrink_wrap
.emit_prologue_components (pro
);
1669 rtx_insn
*seq
= get_insns ();
1671 record_prologue_seq (seq
);
1673 if (control_flow_insn_p (last_insn
))
1674 emit_insn_before (seq
, last_insn
);
1676 emit_insn_after (seq
, last_insn
);
1678 bitmap_ior (SW (bb
)->tail_components
, SW (bb
)->tail_components
, pro
);
1683 /* Place prologues and epilogues for COMPONENTS on edges, if we haven't already
1684 placed them inside blocks directly. */
1686 insert_prologue_epilogue_for_components (sbitmap components
)
1688 auto_sbitmap
pro (SBITMAP_SIZE (components
));
1689 auto_sbitmap
epi (SBITMAP_SIZE (components
));
1692 FOR_EACH_BB_FN (bb
, cfun
)
1699 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1701 /* Find which pro/epilogue components are needed on this edge. */
1702 bitmap_and_compl (epi
, SW (e
->src
)->has_components
,
1703 SW (e
->dest
)->has_components
);
1704 bitmap_and_compl (pro
, SW (e
->dest
)->has_components
,
1705 SW (e
->src
)->has_components
);
1706 bitmap_and (epi
, epi
, components
);
1707 bitmap_and (pro
, pro
, components
);
1709 /* Deselect those we already have put at the head or tail of the
1710 edge's dest resp. src. */
1711 bitmap_and_compl (epi
, epi
, SW (e
->dest
)->head_components
);
1712 bitmap_and_compl (pro
, pro
, SW (e
->dest
)->head_components
);
1713 bitmap_and_compl (epi
, epi
, SW (e
->src
)->tail_components
);
1714 bitmap_and_compl (pro
, pro
, SW (e
->src
)->tail_components
);
1716 if (!bitmap_empty_p (epi
) || !bitmap_empty_p (pro
))
1720 fprintf (dump_file
, " %d->%d", e
->src
->index
,
1722 dump_components ("epi", epi
);
1723 dump_components ("pro", pro
);
1724 if (e
->flags
& EDGE_SIBCALL
)
1725 fprintf (dump_file
, " (SIBCALL)");
1726 else if (e
->flags
& EDGE_ABNORMAL
)
1727 fprintf (dump_file
, " (ABNORMAL)");
1728 fprintf (dump_file
, "\n");
1731 /* Put the epilogue components in place. */
1733 targetm
.shrink_wrap
.emit_epilogue_components (epi
);
1734 rtx_insn
*seq
= get_insns ();
1736 record_epilogue_seq (seq
);
1738 if (e
->flags
& EDGE_SIBCALL
)
1740 gcc_assert (e
->dest
== EXIT_BLOCK_PTR_FOR_FN (cfun
));
1742 rtx_insn
*insn
= BB_END (e
->src
);
1743 gcc_assert (CALL_P (insn
) && SIBLING_CALL_P (insn
));
1744 emit_insn_before (seq
, insn
);
1746 else if (e
->dest
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
1748 gcc_assert (e
->flags
& EDGE_FALLTHRU
);
1749 basic_block new_bb
= split_edge (e
);
1750 emit_insn_after (seq
, BB_END (new_bb
));
1753 insert_insn_on_edge (seq
, e
);
1755 /* Put the prologue components in place. */
1757 targetm
.shrink_wrap
.emit_prologue_components (pro
);
1760 record_prologue_seq (seq
);
1762 insert_insn_on_edge (seq
, e
);
1767 commit_edge_insertions ();
1770 /* The main entry point to this subpass. FIRST_BB is where the prologue
1771 would be normally put. */
1773 try_shrink_wrapping_separate (basic_block first_bb
)
1775 if (!(SHRINK_WRAPPING_ENABLED
1776 && flag_shrink_wrap_separate
1777 && optimize_function_for_speed_p (cfun
)
1778 && targetm
.shrink_wrap
.get_separate_components
))
1781 /* We don't handle "strange" functions. */
1782 if (cfun
->calls_alloca
1783 || cfun
->calls_setjmp
1784 || cfun
->can_throw_non_call_exceptions
1785 || crtl
->calls_eh_return
1786 || crtl
->has_nonlocal_goto
1787 || crtl
->saves_all_registers
)
1790 /* Ask the target what components there are. If it returns NULL, don't
1792 sbitmap components
= targetm
.shrink_wrap
.get_separate_components ();
1796 /* We need LIVE info, not defining anything in the entry block and not
1797 using anything in the exit block. A block then needs a component if
1798 the register for that component is in the IN or GEN or KILL set for
1800 df_scan
->local_flags
|= DF_SCAN_EMPTY_ENTRY_EXIT
;
1801 df_update_entry_exit_and_calls ();
1802 df_live_add_problem ();
1803 df_live_set_all_dirty ();
1806 calculate_dominance_info (CDI_DOMINATORS
);
1807 calculate_dominance_info (CDI_POST_DOMINATORS
);
1809 init_separate_shrink_wrap (components
);
1811 sbitmap_iterator sbi
;
1813 EXECUTE_IF_SET_IN_BITMAP (components
, 0, j
, sbi
)
1814 place_prologue_for_one_component (j
, first_bb
);
1816 /* Try to minimize the number of saves and restores. Do this as long as
1817 it changes anything. This does not iterate more than a few times. */
1818 int spread_times
= 0;
1819 while (spread_components (components
))
1824 fprintf (dump_file
, "Now spread %d times.\n", spread_times
);
1827 disqualify_problematic_components (components
);
1829 /* Don't separately shrink-wrap anything where the "main" prologue will
1830 go; the target code can often optimize things if it is presented with
1831 all components together (say, if it generates store-multiple insns). */
1832 bitmap_and_compl (components
, components
, SW (first_bb
)->has_components
);
1834 if (bitmap_empty_p (components
))
1837 fprintf (dump_file
, "Not wrapping anything separately.\n");
1843 fprintf (dump_file
, "The components we wrap separately are");
1844 dump_components ("sep", components
);
1845 fprintf (dump_file
, "\n");
1847 fprintf (dump_file
, "... Inserting common heads...\n");
1850 emit_common_heads_for_components (components
);
1853 fprintf (dump_file
, "... Inserting common tails...\n");
1855 emit_common_tails_for_components (components
);
1858 fprintf (dump_file
, "... Inserting the more difficult ones...\n");
1860 insert_prologue_epilogue_for_components (components
);
1863 fprintf (dump_file
, "... Done.\n");
1865 targetm
.shrink_wrap
.set_handled_components (components
);
1867 crtl
->shrink_wrapped_separate
= true;
1870 fini_separate_shrink_wrap ();
1872 sbitmap_free (components
);
1873 free_dominance_info (CDI_DOMINATORS
);
1874 free_dominance_info (CDI_POST_DOMINATORS
);
1877 df_scan
->local_flags
&= ~DF_SCAN_EMPTY_ENTRY_EXIT
;
1878 df_update_entry_exit_and_calls ();
1879 df_live_set_all_dirty ();