1 /* Expands front end tree to back end RTL for GCC.
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"
57 #ifdef HAVE_simple_return
59 /* Return true if INSN requires the stack frame to be set up.
60 PROLOGUE_USED contains the hard registers used in the function
61 prologue. SET_UP_BY_PROLOGUE is the set of registers we expect the
62 prologue to set up for the function. */
64 requires_stack_frame_p (rtx insn
, HARD_REG_SET prologue_used
,
65 HARD_REG_SET set_up_by_prologue
)
68 HARD_REG_SET hardregs
;
72 return !SIBLING_CALL_P (insn
);
74 /* We need a frame to get the unique CFA expected by the unwinder. */
75 if (cfun
->can_throw_non_call_exceptions
&& can_throw_internal (insn
))
78 CLEAR_HARD_REG_SET (hardregs
);
79 for (df_rec
= DF_INSN_DEFS (insn
); *df_rec
; df_rec
++)
81 rtx dreg
= DF_REF_REG (*df_rec
);
86 add_to_hard_reg_set (&hardregs
, GET_MODE (dreg
),
89 if (hard_reg_set_intersect_p (hardregs
, prologue_used
))
91 AND_COMPL_HARD_REG_SET (hardregs
, call_used_reg_set
);
92 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
93 if (TEST_HARD_REG_BIT (hardregs
, regno
)
94 && df_regs_ever_live_p (regno
))
97 for (df_rec
= DF_INSN_USES (insn
); *df_rec
; df_rec
++)
99 rtx reg
= DF_REF_REG (*df_rec
);
104 add_to_hard_reg_set (&hardregs
, GET_MODE (reg
),
107 if (hard_reg_set_intersect_p (hardregs
, set_up_by_prologue
))
113 /* See whether BB has a single successor that uses [REGNO, END_REGNO),
114 and if BB is its only predecessor. Return that block if so,
115 otherwise return null. */
118 next_block_for_reg (basic_block bb
, int regno
, int end_regno
)
126 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
128 live
= df_get_live_in (e
->dest
);
129 for (i
= regno
; i
< end_regno
; i
++)
130 if (REGNO_REG_SET_P (live
, i
))
132 if (live_edge
&& live_edge
!= e
)
138 /* We can sometimes encounter dead code. Don't try to move it
139 into the exit block. */
140 if (!live_edge
|| live_edge
->dest
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
143 /* Reject targets of abnormal edges. This is needed for correctness
144 on ports like Alpha and MIPS, whose pic_offset_table_rtx can die on
145 exception edges even though it is generally treated as call-saved
146 for the majority of the compilation. Moving across abnormal edges
147 isn't going to be interesting for shrink-wrap usage anyway. */
148 if (live_edge
->flags
& EDGE_ABNORMAL
)
151 if (EDGE_COUNT (live_edge
->dest
->preds
) > 1)
154 return live_edge
->dest
;
157 /* Try to move INSN from BB to a successor. Return true on success.
158 USES and DEFS are the set of registers that are used and defined
162 move_insn_for_shrink_wrap (basic_block bb
, rtx insn
,
163 const HARD_REG_SET uses
,
164 const HARD_REG_SET defs
)
167 bitmap live_out
, live_in
, bb_uses
, bb_defs
;
168 unsigned int i
, dregno
, end_dregno
, sregno
, end_sregno
;
169 basic_block next_block
;
171 /* Look for a simple register copy. */
172 set
= single_set (insn
);
176 dest
= SET_DEST (set
);
177 if (!REG_P (dest
) || !REG_P (src
))
180 /* Make sure that the source register isn't defined later in BB. */
181 sregno
= REGNO (src
);
182 end_sregno
= END_REGNO (src
);
183 if (overlaps_hard_reg_set_p (defs
, GET_MODE (src
), sregno
))
186 /* Make sure that the destination register isn't referenced later in BB. */
187 dregno
= REGNO (dest
);
188 end_dregno
= END_REGNO (dest
);
189 if (overlaps_hard_reg_set_p (uses
, GET_MODE (dest
), dregno
)
190 || overlaps_hard_reg_set_p (defs
, GET_MODE (dest
), dregno
))
193 /* See whether there is a successor block to which we could move INSN. */
194 next_block
= next_block_for_reg (bb
, dregno
, end_dregno
);
198 /* At this point we are committed to moving INSN, but let's try to
199 move it as far as we can. */
202 live_out
= df_get_live_out (bb
);
203 live_in
= df_get_live_in (next_block
);
206 /* Check whether BB uses DEST or clobbers DEST. We need to add
207 INSN to BB if so. Either way, DEST is no longer live on entry,
208 except for any part that overlaps SRC (next loop). */
209 bb_uses
= &DF_LR_BB_INFO (bb
)->use
;
210 bb_defs
= &DF_LR_BB_INFO (bb
)->def
;
213 for (i
= dregno
; i
< end_dregno
; i
++)
215 if (REGNO_REG_SET_P (bb_uses
, i
) || REGNO_REG_SET_P (bb_defs
, i
)
216 || REGNO_REG_SET_P (&DF_LIVE_BB_INFO (bb
)->gen
, i
))
218 CLEAR_REGNO_REG_SET (live_out
, i
);
219 CLEAR_REGNO_REG_SET (live_in
, i
);
222 /* Check whether BB clobbers SRC. We need to add INSN to BB if so.
223 Either way, SRC is now live on entry. */
224 for (i
= sregno
; i
< end_sregno
; i
++)
226 if (REGNO_REG_SET_P (bb_defs
, i
)
227 || REGNO_REG_SET_P (&DF_LIVE_BB_INFO (bb
)->gen
, i
))
229 SET_REGNO_REG_SET (live_out
, i
);
230 SET_REGNO_REG_SET (live_in
, i
);
235 /* DF_LR_BB_INFO (bb)->def does not comprise the DF_REF_PARTIAL and
236 DF_REF_CONDITIONAL defs. So if DF_LIVE doesn't exist, i.e.
237 at -O1, just give up searching NEXT_BLOCK. */
239 for (i
= dregno
; i
< end_dregno
; i
++)
241 CLEAR_REGNO_REG_SET (live_out
, i
);
242 CLEAR_REGNO_REG_SET (live_in
, i
);
245 for (i
= sregno
; i
< end_sregno
; i
++)
247 SET_REGNO_REG_SET (live_out
, i
);
248 SET_REGNO_REG_SET (live_in
, i
);
252 /* If we don't need to add the move to BB, look for a single
255 next_block
= next_block_for_reg (next_block
, dregno
, end_dregno
);
259 /* BB now defines DEST. It only uses the parts of DEST that overlap SRC
261 for (i
= dregno
; i
< end_dregno
; i
++)
263 CLEAR_REGNO_REG_SET (bb_uses
, i
);
264 SET_REGNO_REG_SET (bb_defs
, i
);
267 /* BB now uses SRC. */
268 for (i
= sregno
; i
< end_sregno
; i
++)
269 SET_REGNO_REG_SET (bb_uses
, i
);
271 emit_insn_after (PATTERN (insn
), bb_note (bb
));
276 /* Look for register copies in the first block of the function, and move
277 them down into successor blocks if the register is used only on one
278 path. This exposes more opportunities for shrink-wrapping. These
279 kinds of sets often occur when incoming argument registers are moved
280 to call-saved registers because their values are live across one or
281 more calls during the function. */
284 prepare_shrink_wrap (basic_block entry_block
)
287 HARD_REG_SET uses
, defs
;
290 CLEAR_HARD_REG_SET (uses
);
291 CLEAR_HARD_REG_SET (defs
);
292 FOR_BB_INSNS_REVERSE_SAFE (entry_block
, insn
, curr
)
293 if (NONDEBUG_INSN_P (insn
)
294 && !move_insn_for_shrink_wrap (entry_block
, insn
, uses
, defs
))
296 /* Add all defined registers to DEFs. */
297 for (ref
= DF_INSN_DEFS (insn
); *ref
; ref
++)
299 x
= DF_REF_REG (*ref
);
300 if (REG_P (x
) && HARD_REGISTER_P (x
))
301 SET_HARD_REG_BIT (defs
, REGNO (x
));
304 /* Add all used registers to USESs. */
305 for (ref
= DF_INSN_USES (insn
); *ref
; ref
++)
307 x
= DF_REF_REG (*ref
);
308 if (REG_P (x
) && HARD_REGISTER_P (x
))
309 SET_HARD_REG_BIT (uses
, REGNO (x
));
314 /* Create a copy of BB instructions and insert at BEFORE. Redirect
315 preds of BB to COPY_BB if they don't appear in NEED_PROLOGUE. */
317 dup_block_and_redirect (basic_block bb
, basic_block copy_bb
, rtx before
,
318 bitmap_head
*need_prologue
)
322 rtx insn
= BB_END (bb
);
324 /* We know BB has a single successor, so there is no need to copy a
325 simple jump at the end of BB. */
326 if (simplejump_p (insn
))
327 insn
= PREV_INSN (insn
);
330 duplicate_insn_chain (BB_HEAD (bb
), insn
);
334 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
335 if (active_insn_p (insn
))
337 fprintf (dump_file
, "Duplicating bb %d to bb %d, %u active insns.\n",
338 bb
->index
, copy_bb
->index
, count
);
342 emit_insn_before (insn
, before
);
344 /* Redirect all the paths that need no prologue into copy_bb. */
345 for (ei
= ei_start (bb
->preds
); (e
= ei_safe_edge (ei
));)
346 if (!bitmap_bit_p (need_prologue
, e
->src
->index
))
348 int freq
= EDGE_FREQUENCY (e
);
349 copy_bb
->count
+= e
->count
;
350 copy_bb
->frequency
+= EDGE_FREQUENCY (e
);
351 e
->dest
->count
-= e
->count
;
352 if (e
->dest
->count
< 0)
354 e
->dest
->frequency
-= freq
;
355 if (e
->dest
->frequency
< 0)
356 e
->dest
->frequency
= 0;
357 redirect_edge_and_branch_force (e
, copy_bb
);
365 /* Try to perform a kind of shrink-wrapping, making sure the
366 prologue/epilogue is emitted only around those parts of the
367 function that require it. */
370 try_shrink_wrapping (edge
*entry_edge
, edge orig_entry_edge
,
371 bitmap_head
*bb_flags
, rtx prologue_seq
)
375 bool nonempty_prologue
= false;
376 unsigned max_grow_size
;
379 for (seq
= prologue_seq
; seq
; seq
= NEXT_INSN (seq
))
380 if (!NOTE_P (seq
) || NOTE_KIND (seq
) != NOTE_INSN_PROLOGUE_END
)
382 nonempty_prologue
= true;
386 if (flag_shrink_wrap
&& HAVE_simple_return
387 && (targetm
.profile_before_prologue () || !crtl
->profile
)
388 && nonempty_prologue
&& !crtl
->calls_eh_return
)
390 HARD_REG_SET prologue_clobbered
, prologue_used
, live_on_edge
;
391 struct hard_reg_set_container set_up_by_prologue
;
393 vec
<basic_block
> vec
;
395 bitmap_head bb_antic_flags
;
396 bitmap_head bb_on_list
;
400 fprintf (dump_file
, "Attempting shrink-wrapping optimization.\n");
402 /* Compute the registers set and used in the prologue. */
403 CLEAR_HARD_REG_SET (prologue_clobbered
);
404 CLEAR_HARD_REG_SET (prologue_used
);
405 for (p_insn
= prologue_seq
; p_insn
; p_insn
= NEXT_INSN (p_insn
))
407 HARD_REG_SET this_used
;
408 if (!NONDEBUG_INSN_P (p_insn
))
411 CLEAR_HARD_REG_SET (this_used
);
412 note_uses (&PATTERN (p_insn
), record_hard_reg_uses
,
414 AND_COMPL_HARD_REG_SET (this_used
, prologue_clobbered
);
415 IOR_HARD_REG_SET (prologue_used
, this_used
);
416 note_stores (PATTERN (p_insn
), record_hard_reg_sets
,
417 &prologue_clobbered
);
420 prepare_shrink_wrap ((*entry_edge
)->dest
);
422 bitmap_initialize (&bb_antic_flags
, &bitmap_default_obstack
);
423 bitmap_initialize (&bb_on_list
, &bitmap_default_obstack
);
424 bitmap_initialize (&bb_tail
, &bitmap_default_obstack
);
426 /* Find the set of basic blocks that require a stack frame,
427 and blocks that are too big to be duplicated. */
429 vec
.create (n_basic_blocks_for_fn (cfun
));
431 CLEAR_HARD_REG_SET (set_up_by_prologue
.set
);
432 add_to_hard_reg_set (&set_up_by_prologue
.set
, Pmode
,
433 STACK_POINTER_REGNUM
);
434 add_to_hard_reg_set (&set_up_by_prologue
.set
, Pmode
, ARG_POINTER_REGNUM
);
435 if (frame_pointer_needed
)
436 add_to_hard_reg_set (&set_up_by_prologue
.set
, Pmode
,
437 HARD_FRAME_POINTER_REGNUM
);
438 if (pic_offset_table_rtx
)
439 add_to_hard_reg_set (&set_up_by_prologue
.set
, Pmode
,
440 PIC_OFFSET_TABLE_REGNUM
);
442 add_to_hard_reg_set (&set_up_by_prologue
.set
,
443 GET_MODE (crtl
->drap_reg
),
444 REGNO (crtl
->drap_reg
));
445 if (targetm
.set_up_by_prologue
)
446 targetm
.set_up_by_prologue (&set_up_by_prologue
);
448 /* We don't use a different max size depending on
449 optimize_bb_for_speed_p because increasing shrink-wrapping
450 opportunities by duplicating tail blocks can actually result
451 in an overall decrease in code size. */
452 max_grow_size
= get_uncond_jump_length ();
453 max_grow_size
*= PARAM_VALUE (PARAM_MAX_GROW_COPY_BB_INSNS
);
455 FOR_EACH_BB_FN (bb
, cfun
)
460 FOR_BB_INSNS (bb
, insn
)
461 if (NONDEBUG_INSN_P (insn
))
463 if (requires_stack_frame_p (insn
, prologue_used
,
464 set_up_by_prologue
.set
))
466 if (bb
== (*entry_edge
)->dest
)
467 goto fail_shrinkwrap
;
468 bitmap_set_bit (bb_flags
, bb
->index
);
472 else if (size
<= max_grow_size
)
474 size
+= get_attr_min_length (insn
);
475 if (size
> max_grow_size
)
476 bitmap_set_bit (&bb_on_list
, bb
->index
);
481 /* Blocks that really need a prologue, or are too big for tails. */
482 bitmap_ior_into (&bb_on_list
, bb_flags
);
484 /* For every basic block that needs a prologue, mark all blocks
485 reachable from it, so as to ensure they are also seen as
486 requiring a prologue. */
487 while (!vec
.is_empty ())
489 basic_block tmp_bb
= vec
.pop ();
491 FOR_EACH_EDGE (e
, ei
, tmp_bb
->succs
)
492 if (e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
)
493 && bitmap_set_bit (bb_flags
, e
->dest
->index
))
494 vec
.quick_push (e
->dest
);
497 /* Find the set of basic blocks that need no prologue, have a
498 single successor, can be duplicated, meet a max size
499 requirement, and go to the exit via like blocks. */
500 vec
.quick_push (EXIT_BLOCK_PTR_FOR_FN (cfun
));
501 while (!vec
.is_empty ())
503 basic_block tmp_bb
= vec
.pop ();
505 FOR_EACH_EDGE (e
, ei
, tmp_bb
->preds
)
506 if (single_succ_p (e
->src
)
507 && !bitmap_bit_p (&bb_on_list
, e
->src
->index
)
508 && can_duplicate_block_p (e
->src
))
513 /* If there is predecessor of e->src which doesn't
514 need prologue and the edge is complex,
515 we might not be able to redirect the branch
516 to a copy of e->src. */
517 FOR_EACH_EDGE (pe
, pei
, e
->src
->preds
)
518 if ((pe
->flags
& EDGE_COMPLEX
) != 0
519 && !bitmap_bit_p (bb_flags
, pe
->src
->index
))
521 if (pe
== NULL
&& bitmap_set_bit (&bb_tail
, e
->src
->index
))
522 vec
.quick_push (e
->src
);
526 /* Now walk backwards from every block that is marked as needing
527 a prologue to compute the bb_antic_flags bitmap. Exclude
528 tail blocks; They can be duplicated to be used on paths not
529 needing a prologue. */
530 bitmap_clear (&bb_on_list
);
531 bitmap_and_compl (&bb_antic_flags
, bb_flags
, &bb_tail
);
532 FOR_EACH_BB_FN (bb
, cfun
)
534 if (!bitmap_bit_p (&bb_antic_flags
, bb
->index
))
536 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
537 if (!bitmap_bit_p (&bb_antic_flags
, e
->src
->index
)
538 && bitmap_set_bit (&bb_on_list
, e
->src
->index
))
539 vec
.quick_push (e
->src
);
541 while (!vec
.is_empty ())
543 basic_block tmp_bb
= vec
.pop ();
546 bitmap_clear_bit (&bb_on_list
, tmp_bb
->index
);
547 FOR_EACH_EDGE (e
, ei
, tmp_bb
->succs
)
548 if (!bitmap_bit_p (&bb_antic_flags
, e
->dest
->index
))
556 bitmap_set_bit (&bb_antic_flags
, tmp_bb
->index
);
557 FOR_EACH_EDGE (e
, ei
, tmp_bb
->preds
)
558 if (!bitmap_bit_p (&bb_antic_flags
, e
->src
->index
)
559 && bitmap_set_bit (&bb_on_list
, e
->src
->index
))
560 vec
.quick_push (e
->src
);
563 /* Find exactly one edge that leads to a block in ANTIC from
564 a block that isn't. */
565 if (!bitmap_bit_p (&bb_antic_flags
, (*entry_edge
)->dest
->index
))
566 FOR_EACH_BB_FN (bb
, cfun
)
568 if (!bitmap_bit_p (&bb_antic_flags
, bb
->index
))
570 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
571 if (!bitmap_bit_p (&bb_antic_flags
, e
->src
->index
))
573 if (*entry_edge
!= orig_entry_edge
)
575 *entry_edge
= orig_entry_edge
;
577 fprintf (dump_file
, "More than one candidate edge.\n");
578 goto fail_shrinkwrap
;
581 fprintf (dump_file
, "Found candidate edge for "
582 "shrink-wrapping, %d->%d.\n", e
->src
->index
,
588 if (*entry_edge
!= orig_entry_edge
)
590 /* Test whether the prologue is known to clobber any register
591 (other than FP or SP) which are live on the edge. */
592 CLEAR_HARD_REG_BIT (prologue_clobbered
, STACK_POINTER_REGNUM
);
593 if (frame_pointer_needed
)
594 CLEAR_HARD_REG_BIT (prologue_clobbered
, HARD_FRAME_POINTER_REGNUM
);
595 REG_SET_TO_HARD_REG_SET (live_on_edge
,
596 df_get_live_in ((*entry_edge
)->dest
));
597 if (hard_reg_set_intersect_p (live_on_edge
, prologue_clobbered
))
599 *entry_edge
= orig_entry_edge
;
602 "Shrink-wrapping aborted due to clobber.\n");
605 if (*entry_edge
!= orig_entry_edge
)
607 crtl
->shrink_wrapped
= true;
609 fprintf (dump_file
, "Performing shrink-wrapping.\n");
611 /* Find tail blocks reachable from both blocks needing a
612 prologue and blocks not needing a prologue. */
613 if (!bitmap_empty_p (&bb_tail
))
614 FOR_EACH_BB_FN (bb
, cfun
)
616 bool some_pro
, some_no_pro
;
617 if (!bitmap_bit_p (&bb_tail
, bb
->index
))
619 some_pro
= some_no_pro
= false;
620 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
622 if (bitmap_bit_p (bb_flags
, e
->src
->index
))
627 if (some_pro
&& some_no_pro
)
630 bitmap_clear_bit (&bb_tail
, bb
->index
);
632 /* Find the head of each tail. */
633 while (!vec
.is_empty ())
635 basic_block tbb
= vec
.pop ();
637 if (!bitmap_bit_p (&bb_tail
, tbb
->index
))
640 while (single_succ_p (tbb
))
642 tbb
= single_succ (tbb
);
643 bitmap_clear_bit (&bb_tail
, tbb
->index
);
646 /* Now duplicate the tails. */
647 if (!bitmap_empty_p (&bb_tail
))
648 FOR_EACH_BB_REVERSE_FN (bb
, cfun
)
650 basic_block copy_bb
, tbb
;
654 if (!bitmap_clear_bit (&bb_tail
, bb
->index
))
657 /* Create a copy of BB, instructions and all, for
658 use on paths that don't need a prologue.
659 Ideal placement of the copy is on a fall-thru edge
660 or after a block that would jump to the copy. */
661 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
662 if (!bitmap_bit_p (bb_flags
, e
->src
->index
)
663 && single_succ_p (e
->src
))
667 /* Make sure we insert after any barriers. */
668 rtx end
= get_last_bb_insn (e
->src
);
669 copy_bb
= create_basic_block (NEXT_INSN (end
),
671 BB_COPY_PARTITION (copy_bb
, e
->src
);
675 /* Otherwise put the copy at the end of the function. */
676 copy_bb
= create_basic_block (NULL_RTX
, NULL_RTX
,
677 EXIT_BLOCK_PTR_FOR_FN (cfun
)->prev_bb
);
678 BB_COPY_PARTITION (copy_bb
, bb
);
681 insert_point
= emit_note_after (NOTE_INSN_DELETED
,
683 emit_barrier_after (BB_END (copy_bb
));
688 dup_block_and_redirect (tbb
, copy_bb
, insert_point
,
690 tbb
= single_succ (tbb
);
691 if (tbb
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
693 e
= split_block (copy_bb
, PREV_INSN (insert_point
));
697 /* Quiet verify_flow_info by (ab)using EDGE_FAKE.
698 We have yet to add a simple_return to the tails,
699 as we'd like to first convert_jumps_to_returns in
700 case the block is no longer used after that. */
702 if (CALL_P (PREV_INSN (insert_point
))
703 && SIBLING_CALL_P (PREV_INSN (insert_point
)))
704 eflags
= EDGE_SIBCALL
| EDGE_ABNORMAL
;
705 make_single_succ_edge (copy_bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
),
708 /* verify_flow_info doesn't like a note after a
710 delete_insn (insert_point
);
711 if (bitmap_empty_p (&bb_tail
))
717 bitmap_clear (&bb_tail
);
718 bitmap_clear (&bb_antic_flags
);
719 bitmap_clear (&bb_on_list
);
724 /* If we're allowed to generate a simple return instruction, then by
725 definition we don't need a full epilogue. If the last basic
726 block before the exit block does not contain active instructions,
727 examine its predecessors and try to emit (conditional) return
731 get_unconverted_simple_return (edge exit_fallthru_edge
, bitmap_head bb_flags
,
732 vec
<edge
> *unconverted_simple_returns
,
739 /* convert_jumps_to_returns may add to preds of the exit block
740 (but won't remove). Stop at end of current preds. */
741 last
= EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun
)->preds
);
742 for (i
= 0; i
< last
; i
++)
744 edge e
= EDGE_I (EXIT_BLOCK_PTR_FOR_FN (cfun
)->preds
, i
);
745 if (LABEL_P (BB_HEAD (e
->src
))
746 && !bitmap_bit_p (&bb_flags
, e
->src
->index
)
747 && !active_insn_between (BB_HEAD (e
->src
), BB_END (e
->src
)))
748 *unconverted_simple_returns
749 = convert_jumps_to_returns (e
->src
, true,
750 *unconverted_simple_returns
);
754 if (exit_fallthru_edge
!= NULL
755 && EDGE_COUNT (exit_fallthru_edge
->src
->preds
) != 0
756 && !bitmap_bit_p (&bb_flags
, exit_fallthru_edge
->src
->index
))
760 last_bb
= emit_return_for_exit (exit_fallthru_edge
, true);
761 *returnjump
= BB_END (last_bb
);
762 exit_fallthru_edge
= NULL
;
764 return exit_fallthru_edge
;
767 /* If there were branches to an empty LAST_BB which we tried to
768 convert to conditional simple_returns, but couldn't for some
769 reason, create a block to hold a simple_return insn and redirect
770 those remaining edges. */
773 convert_to_simple_return (edge entry_edge
, edge orig_entry_edge
,
774 bitmap_head bb_flags
, rtx returnjump
,
775 vec
<edge
> unconverted_simple_returns
)
780 if (!unconverted_simple_returns
.is_empty ())
782 basic_block simple_return_block_hot
= NULL
;
783 basic_block simple_return_block_cold
= NULL
;
784 edge pending_edge_hot
= NULL
;
785 edge pending_edge_cold
= NULL
;
786 basic_block exit_pred
;
789 gcc_assert (entry_edge
!= orig_entry_edge
);
791 /* See if we can reuse the last insn that was emitted for the
793 if (returnjump
!= NULL_RTX
794 && JUMP_LABEL (returnjump
) == simple_return_rtx
)
796 e
= split_block (BLOCK_FOR_INSN (returnjump
), PREV_INSN (returnjump
));
797 if (BB_PARTITION (e
->src
) == BB_HOT_PARTITION
)
798 simple_return_block_hot
= e
->dest
;
800 simple_return_block_cold
= e
->dest
;
803 /* Also check returns we might need to add to tail blocks. */
804 FOR_EACH_EDGE (e
, ei
, EXIT_BLOCK_PTR_FOR_FN (cfun
)->preds
)
805 if (EDGE_COUNT (e
->src
->preds
) != 0
806 && (e
->flags
& EDGE_FAKE
) != 0
807 && !bitmap_bit_p (&bb_flags
, e
->src
->index
))
809 if (BB_PARTITION (e
->src
) == BB_HOT_PARTITION
)
810 pending_edge_hot
= e
;
812 pending_edge_cold
= e
;
815 /* Save a pointer to the exit's predecessor BB for use in
816 inserting new BBs at the end of the function. Do this
817 after the call to split_block above which may split
818 the original exit pred. */
819 exit_pred
= EXIT_BLOCK_PTR_FOR_FN (cfun
)->prev_bb
;
821 FOR_EACH_VEC_ELT (unconverted_simple_returns
, i
, e
)
823 basic_block
*pdest_bb
;
826 if (BB_PARTITION (e
->src
) == BB_HOT_PARTITION
)
828 pdest_bb
= &simple_return_block_hot
;
829 pending
= pending_edge_hot
;
833 pdest_bb
= &simple_return_block_cold
;
834 pending
= pending_edge_cold
;
837 if (*pdest_bb
== NULL
&& pending
!= NULL
)
839 emit_return_into_block (true, pending
->src
);
840 pending
->flags
&= ~(EDGE_FALLTHRU
| EDGE_FAKE
);
841 *pdest_bb
= pending
->src
;
843 else if (*pdest_bb
== NULL
)
848 bb
= create_basic_block (NULL
, NULL
, exit_pred
);
849 BB_COPY_PARTITION (bb
, e
->src
);
850 start
= emit_jump_insn_after (gen_simple_return (),
852 JUMP_LABEL (start
) = simple_return_rtx
;
853 emit_barrier_after (start
);
856 make_edge (bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
858 redirect_edge_and_branch_force (e
, *pdest_bb
);
860 unconverted_simple_returns
.release ();
863 if (entry_edge
!= orig_entry_edge
)
865 FOR_EACH_EDGE (e
, ei
, EXIT_BLOCK_PTR_FOR_FN (cfun
)->preds
)
866 if (EDGE_COUNT (e
->src
->preds
) != 0
867 && (e
->flags
& EDGE_FAKE
) != 0
868 && !bitmap_bit_p (&bb_flags
, e
->src
->index
))
870 emit_return_into_block (true, e
->src
);
871 e
->flags
&= ~(EDGE_FALLTHRU
| EDGE_FAKE
);