* ipa-devirt.c (type_pair, default_hashset_traits): New types.
[official-gcc.git] / gcc / shrink-wrap.c
blobfd24135e06a7e9bf323f362f956e568511f9b301
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
9 version.
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
14 for more details.
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. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl-error.h"
27 #include "tree.h"
28 #include "stor-layout.h"
29 #include "varasm.h"
30 #include "stringpool.h"
31 #include "flags.h"
32 #include "except.h"
33 #include "function.h"
34 #include "expr.h"
35 #include "optabs.h"
36 #include "libfuncs.h"
37 #include "regs.h"
38 #include "hard-reg-set.h"
39 #include "insn-config.h"
40 #include "recog.h"
41 #include "output.h"
42 #include "hashtab.h"
43 #include "tm_p.h"
44 #include "langhooks.h"
45 #include "target.h"
46 #include "common/common-target.h"
47 #include "gimple-expr.h"
48 #include "gimplify.h"
49 #include "tree-pass.h"
50 #include "predict.h"
51 #include "df.h"
52 #include "params.h"
53 #include "bb-reorder.h"
54 #include "shrink-wrap.h"
55 #include "regcprop.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. */
63 bool
64 requires_stack_frame_p (rtx_insn *insn, HARD_REG_SET prologue_used,
65 HARD_REG_SET set_up_by_prologue)
67 df_ref def, use;
68 HARD_REG_SET hardregs;
69 unsigned regno;
71 if (CALL_P (insn))
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))
76 return true;
78 CLEAR_HARD_REG_SET (hardregs);
79 FOR_EACH_INSN_DEF (def, insn)
81 rtx dreg = DF_REF_REG (def);
83 if (!REG_P (dreg))
84 continue;
86 add_to_hard_reg_set (&hardregs, GET_MODE (dreg),
87 REGNO (dreg));
89 if (hard_reg_set_intersect_p (hardregs, prologue_used))
90 return true;
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))
95 return true;
97 FOR_EACH_INSN_USE (use, insn)
99 rtx reg = DF_REF_REG (use);
101 if (!REG_P (reg))
102 continue;
104 add_to_hard_reg_set (&hardregs, GET_MODE (reg),
105 REGNO (reg));
107 if (hard_reg_set_intersect_p (hardregs, set_up_by_prologue))
108 return true;
110 return false;
113 /* See whether there has a single live edge from BB, which dest uses
114 [REGNO, END_REGNO). Return the live edge if its dest bb has
115 one or two predecessors. Otherwise return NULL. */
117 static edge
118 live_edge_for_reg (basic_block bb, int regno, int end_regno)
120 edge e, live_edge;
121 edge_iterator ei;
122 bitmap live;
123 int i;
125 live_edge = NULL;
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)
133 return NULL;
134 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))
141 return NULL;
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)
149 return NULL;
151 /* When live_edge->dest->preds == 2, we can create a new block on
152 the edge to make it meet the requirement. */
153 if (EDGE_COUNT (live_edge->dest->preds) > 2)
154 return NULL;
156 return live_edge;
159 /* Try to move INSN from BB to a successor. Return true on success.
160 USES and DEFS are the set of registers that are used and defined
161 after INSN in BB. SPLIT_P indicates whether a live edge from BB
162 is splitted or not. */
164 static bool
165 move_insn_for_shrink_wrap (basic_block bb, rtx_insn *insn,
166 const HARD_REG_SET uses,
167 const HARD_REG_SET defs,
168 bool *split_p)
170 rtx set, src, dest;
171 bitmap live_out, live_in, bb_uses, bb_defs;
172 unsigned int i, dregno, end_dregno, sregno, end_sregno;
173 basic_block next_block;
174 edge live_edge;
176 /* Look for a simple register copy. */
177 set = single_set (insn);
178 if (!set)
179 return false;
180 src = SET_SRC (set);
181 dest = SET_DEST (set);
182 if (!REG_P (dest) || !REG_P (src)
183 /* STACK or FRAME related adjustment might be part of prologue.
184 So keep them in the entry block. */
185 || dest == stack_pointer_rtx
186 || dest == frame_pointer_rtx
187 || dest == hard_frame_pointer_rtx)
188 return false;
190 /* Make sure that the source register isn't defined later in BB. */
191 sregno = REGNO (src);
192 end_sregno = END_REGNO (src);
193 if (overlaps_hard_reg_set_p (defs, GET_MODE (src), sregno))
194 return false;
196 /* Make sure that the destination register isn't referenced later in BB. */
197 dregno = REGNO (dest);
198 end_dregno = END_REGNO (dest);
199 if (overlaps_hard_reg_set_p (uses, GET_MODE (dest), dregno)
200 || overlaps_hard_reg_set_p (defs, GET_MODE (dest), dregno))
201 return false;
203 /* See whether there is a successor block to which we could move INSN. */
204 live_edge = live_edge_for_reg (bb, dregno, end_dregno);
205 if (!live_edge)
206 return false;
208 next_block = live_edge->dest;
209 /* Create a new basic block on the edge. */
210 if (EDGE_COUNT (next_block->preds) == 2)
212 /* split_edge for a block with only one successor is meaningless. */
213 if (EDGE_COUNT (bb->succs) == 1)
214 return false;
216 /* If DF_LIVE doesn't exist, i.e. at -O1, just give up. */
217 if (!df_live)
218 return false;
220 next_block = split_edge (live_edge);
222 /* We create a new basic block. Call df_grow_bb_info to make sure
223 all data structures are allocated. */
224 df_grow_bb_info (df_live);
225 bitmap_copy (df_get_live_in (next_block), df_get_live_out (bb));
226 df_set_bb_dirty (next_block);
228 /* We should not split more than once for a function. */
229 gcc_assert (!(*split_p));
230 *split_p = true;
233 /* At this point we are committed to moving INSN, but let's try to
234 move it as far as we can. */
237 live_out = df_get_live_out (bb);
238 live_in = df_get_live_in (next_block);
239 bb = next_block;
241 /* Check whether BB uses DEST or clobbers DEST. We need to add
242 INSN to BB if so. Either way, DEST is no longer live on entry,
243 except for any part that overlaps SRC (next loop). */
244 bb_uses = &DF_LR_BB_INFO (bb)->use;
245 bb_defs = &DF_LR_BB_INFO (bb)->def;
246 if (df_live)
248 for (i = dregno; i < end_dregno; i++)
250 if (*split_p
251 || REGNO_REG_SET_P (bb_uses, i)
252 || REGNO_REG_SET_P (bb_defs, i)
253 || REGNO_REG_SET_P (&DF_LIVE_BB_INFO (bb)->gen, i))
254 next_block = NULL;
255 CLEAR_REGNO_REG_SET (live_out, i);
256 CLEAR_REGNO_REG_SET (live_in, i);
259 /* Check whether BB clobbers SRC. We need to add INSN to BB if so.
260 Either way, SRC is now live on entry. */
261 for (i = sregno; i < end_sregno; i++)
263 if (*split_p
264 || REGNO_REG_SET_P (bb_defs, i)
265 || REGNO_REG_SET_P (&DF_LIVE_BB_INFO (bb)->gen, i))
266 next_block = NULL;
267 SET_REGNO_REG_SET (live_out, i);
268 SET_REGNO_REG_SET (live_in, i);
271 else
273 /* DF_LR_BB_INFO (bb)->def does not comprise the DF_REF_PARTIAL and
274 DF_REF_CONDITIONAL defs. So if DF_LIVE doesn't exist, i.e.
275 at -O1, just give up searching NEXT_BLOCK. */
276 next_block = NULL;
277 for (i = dregno; i < end_dregno; i++)
279 CLEAR_REGNO_REG_SET (live_out, i);
280 CLEAR_REGNO_REG_SET (live_in, i);
283 for (i = sregno; i < end_sregno; i++)
285 SET_REGNO_REG_SET (live_out, i);
286 SET_REGNO_REG_SET (live_in, i);
290 /* If we don't need to add the move to BB, look for a single
291 successor block. */
292 if (next_block)
294 live_edge = live_edge_for_reg (next_block, dregno, end_dregno);
295 if (!live_edge || EDGE_COUNT (live_edge->dest->preds) > 1)
296 break;
297 next_block = live_edge->dest;
300 while (next_block);
302 /* For the new created basic block, there is no dataflow info at all.
303 So skip the following dataflow update and check. */
304 if (!(*split_p))
306 /* BB now defines DEST. It only uses the parts of DEST that overlap SRC
307 (next loop). */
308 for (i = dregno; i < end_dregno; i++)
310 CLEAR_REGNO_REG_SET (bb_uses, i);
311 SET_REGNO_REG_SET (bb_defs, i);
314 /* BB now uses SRC. */
315 for (i = sregno; i < end_sregno; i++)
316 SET_REGNO_REG_SET (bb_uses, i);
319 emit_insn_after (PATTERN (insn), bb_note (bb));
320 delete_insn (insn);
321 return true;
324 /* Look for register copies in the first block of the function, and move
325 them down into successor blocks if the register is used only on one
326 path. This exposes more opportunities for shrink-wrapping. These
327 kinds of sets often occur when incoming argument registers are moved
328 to call-saved registers because their values are live across one or
329 more calls during the function. */
331 void
332 prepare_shrink_wrap (basic_block entry_block)
334 rtx_insn *insn, *curr;
335 rtx x;
336 HARD_REG_SET uses, defs;
337 df_ref def, use;
338 bool split_p = false;
340 if (JUMP_P (BB_END (entry_block)))
342 /* To have more shrink-wrapping opportunities, prepare_shrink_wrap tries
343 to sink the copies from parameter to callee saved register out of
344 entry block. copyprop_hardreg_forward_bb_without_debug_insn is called
345 to release some dependences. */
346 copyprop_hardreg_forward_bb_without_debug_insn (entry_block);
349 CLEAR_HARD_REG_SET (uses);
350 CLEAR_HARD_REG_SET (defs);
351 FOR_BB_INSNS_REVERSE_SAFE (entry_block, insn, curr)
352 if (NONDEBUG_INSN_P (insn)
353 && !move_insn_for_shrink_wrap (entry_block, insn, uses, defs,
354 &split_p))
356 /* Add all defined registers to DEFs. */
357 FOR_EACH_INSN_DEF (def, insn)
359 x = DF_REF_REG (def);
360 if (REG_P (x) && HARD_REGISTER_P (x))
361 SET_HARD_REG_BIT (defs, REGNO (x));
364 /* Add all used registers to USESs. */
365 FOR_EACH_INSN_USE (use, insn)
367 x = DF_REF_REG (use);
368 if (REG_P (x) && HARD_REGISTER_P (x))
369 SET_HARD_REG_BIT (uses, REGNO (x));
374 /* Create a copy of BB instructions and insert at BEFORE. Redirect
375 preds of BB to COPY_BB if they don't appear in NEED_PROLOGUE. */
376 void
377 dup_block_and_redirect (basic_block bb, basic_block copy_bb, rtx_insn *before,
378 bitmap_head *need_prologue)
380 edge_iterator ei;
381 edge e;
382 rtx_insn *insn = BB_END (bb);
384 /* We know BB has a single successor, so there is no need to copy a
385 simple jump at the end of BB. */
386 if (simplejump_p (insn))
387 insn = PREV_INSN (insn);
389 start_sequence ();
390 duplicate_insn_chain (BB_HEAD (bb), insn);
391 if (dump_file)
393 unsigned count = 0;
394 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
395 if (active_insn_p (insn))
396 ++count;
397 fprintf (dump_file, "Duplicating bb %d to bb %d, %u active insns.\n",
398 bb->index, copy_bb->index, count);
400 insn = get_insns ();
401 end_sequence ();
402 emit_insn_before (insn, before);
404 /* Redirect all the paths that need no prologue into copy_bb. */
405 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei));)
406 if (!bitmap_bit_p (need_prologue, e->src->index))
408 int freq = EDGE_FREQUENCY (e);
409 copy_bb->count += e->count;
410 copy_bb->frequency += EDGE_FREQUENCY (e);
411 e->dest->count -= e->count;
412 if (e->dest->count < 0)
413 e->dest->count = 0;
414 e->dest->frequency -= freq;
415 if (e->dest->frequency < 0)
416 e->dest->frequency = 0;
417 redirect_edge_and_branch_force (e, copy_bb);
418 continue;
420 else
421 ei_next (&ei);
425 /* Try to perform a kind of shrink-wrapping, making sure the
426 prologue/epilogue is emitted only around those parts of the
427 function that require it. */
429 void
430 try_shrink_wrapping (edge *entry_edge, edge orig_entry_edge,
431 bitmap_head *bb_flags, rtx_insn *prologue_seq)
433 edge e;
434 edge_iterator ei;
435 bool nonempty_prologue = false;
436 unsigned max_grow_size;
437 rtx_insn *seq;
439 for (seq = prologue_seq; seq; seq = NEXT_INSN (seq))
440 if (!NOTE_P (seq) || NOTE_KIND (seq) != NOTE_INSN_PROLOGUE_END)
442 nonempty_prologue = true;
443 break;
446 if (flag_shrink_wrap && HAVE_simple_return
447 && (targetm.profile_before_prologue () || !crtl->profile)
448 && nonempty_prologue && !crtl->calls_eh_return)
450 HARD_REG_SET prologue_clobbered, prologue_used, live_on_edge;
451 struct hard_reg_set_container set_up_by_prologue;
452 rtx_insn *p_insn;
453 vec<basic_block> vec;
454 basic_block bb;
455 bitmap_head bb_antic_flags;
456 bitmap_head bb_on_list;
457 bitmap_head bb_tail;
459 if (dump_file)
460 fprintf (dump_file, "Attempting shrink-wrapping optimization.\n");
462 /* Compute the registers set and used in the prologue. */
463 CLEAR_HARD_REG_SET (prologue_clobbered);
464 CLEAR_HARD_REG_SET (prologue_used);
465 for (p_insn = prologue_seq; p_insn; p_insn = NEXT_INSN (p_insn))
467 HARD_REG_SET this_used;
468 if (!NONDEBUG_INSN_P (p_insn))
469 continue;
471 CLEAR_HARD_REG_SET (this_used);
472 note_uses (&PATTERN (p_insn), record_hard_reg_uses,
473 &this_used);
474 AND_COMPL_HARD_REG_SET (this_used, prologue_clobbered);
475 IOR_HARD_REG_SET (prologue_used, this_used);
476 note_stores (PATTERN (p_insn), record_hard_reg_sets,
477 &prologue_clobbered);
480 prepare_shrink_wrap ((*entry_edge)->dest);
482 bitmap_initialize (&bb_antic_flags, &bitmap_default_obstack);
483 bitmap_initialize (&bb_on_list, &bitmap_default_obstack);
484 bitmap_initialize (&bb_tail, &bitmap_default_obstack);
486 /* Find the set of basic blocks that require a stack frame,
487 and blocks that are too big to be duplicated. */
489 vec.create (n_basic_blocks_for_fn (cfun));
491 CLEAR_HARD_REG_SET (set_up_by_prologue.set);
492 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
493 STACK_POINTER_REGNUM);
494 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode, ARG_POINTER_REGNUM);
495 if (frame_pointer_needed)
496 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
497 HARD_FRAME_POINTER_REGNUM);
498 if (pic_offset_table_rtx)
499 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
500 PIC_OFFSET_TABLE_REGNUM);
501 if (crtl->drap_reg)
502 add_to_hard_reg_set (&set_up_by_prologue.set,
503 GET_MODE (crtl->drap_reg),
504 REGNO (crtl->drap_reg));
505 if (targetm.set_up_by_prologue)
506 targetm.set_up_by_prologue (&set_up_by_prologue);
508 /* We don't use a different max size depending on
509 optimize_bb_for_speed_p because increasing shrink-wrapping
510 opportunities by duplicating tail blocks can actually result
511 in an overall decrease in code size. */
512 max_grow_size = get_uncond_jump_length ();
513 max_grow_size *= PARAM_VALUE (PARAM_MAX_GROW_COPY_BB_INSNS);
515 FOR_EACH_BB_FN (bb, cfun)
517 rtx_insn *insn;
518 unsigned size = 0;
520 FOR_BB_INSNS (bb, insn)
521 if (NONDEBUG_INSN_P (insn))
523 if (requires_stack_frame_p (insn, prologue_used,
524 set_up_by_prologue.set))
526 if (bb == (*entry_edge)->dest)
527 goto fail_shrinkwrap;
528 bitmap_set_bit (bb_flags, bb->index);
529 vec.quick_push (bb);
530 break;
532 else if (size <= max_grow_size)
534 size += get_attr_min_length (insn);
535 if (size > max_grow_size)
536 bitmap_set_bit (&bb_on_list, bb->index);
541 /* Blocks that really need a prologue, or are too big for tails. */
542 bitmap_ior_into (&bb_on_list, bb_flags);
544 /* For every basic block that needs a prologue, mark all blocks
545 reachable from it, so as to ensure they are also seen as
546 requiring a prologue. */
547 while (!vec.is_empty ())
549 basic_block tmp_bb = vec.pop ();
551 FOR_EACH_EDGE (e, ei, tmp_bb->succs)
552 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
553 && bitmap_set_bit (bb_flags, e->dest->index))
554 vec.quick_push (e->dest);
557 /* Find the set of basic blocks that need no prologue, have a
558 single successor, can be duplicated, meet a max size
559 requirement, and go to the exit via like blocks. */
560 vec.quick_push (EXIT_BLOCK_PTR_FOR_FN (cfun));
561 while (!vec.is_empty ())
563 basic_block tmp_bb = vec.pop ();
565 FOR_EACH_EDGE (e, ei, tmp_bb->preds)
566 if (single_succ_p (e->src)
567 && !bitmap_bit_p (&bb_on_list, e->src->index)
568 && can_duplicate_block_p (e->src))
570 edge pe;
571 edge_iterator pei;
573 /* If there is predecessor of e->src which doesn't
574 need prologue and the edge is complex,
575 we might not be able to redirect the branch
576 to a copy of e->src. */
577 FOR_EACH_EDGE (pe, pei, e->src->preds)
578 if ((pe->flags & EDGE_COMPLEX) != 0
579 && !bitmap_bit_p (bb_flags, pe->src->index))
580 break;
581 if (pe == NULL && bitmap_set_bit (&bb_tail, e->src->index))
582 vec.quick_push (e->src);
586 /* Now walk backwards from every block that is marked as needing
587 a prologue to compute the bb_antic_flags bitmap. Exclude
588 tail blocks; They can be duplicated to be used on paths not
589 needing a prologue. */
590 bitmap_clear (&bb_on_list);
591 bitmap_and_compl (&bb_antic_flags, bb_flags, &bb_tail);
592 FOR_EACH_BB_FN (bb, cfun)
594 if (!bitmap_bit_p (&bb_antic_flags, bb->index))
595 continue;
596 FOR_EACH_EDGE (e, ei, bb->preds)
597 if (!bitmap_bit_p (&bb_antic_flags, e->src->index)
598 && bitmap_set_bit (&bb_on_list, e->src->index))
599 vec.quick_push (e->src);
601 while (!vec.is_empty ())
603 basic_block tmp_bb = vec.pop ();
604 bool all_set = true;
606 bitmap_clear_bit (&bb_on_list, tmp_bb->index);
607 FOR_EACH_EDGE (e, ei, tmp_bb->succs)
608 if (!bitmap_bit_p (&bb_antic_flags, e->dest->index))
610 all_set = false;
611 break;
614 if (all_set)
616 bitmap_set_bit (&bb_antic_flags, tmp_bb->index);
617 FOR_EACH_EDGE (e, ei, tmp_bb->preds)
618 if (!bitmap_bit_p (&bb_antic_flags, e->src->index)
619 && bitmap_set_bit (&bb_on_list, e->src->index))
620 vec.quick_push (e->src);
623 /* Find exactly one edge that leads to a block in ANTIC from
624 a block that isn't. */
625 if (!bitmap_bit_p (&bb_antic_flags, (*entry_edge)->dest->index))
626 FOR_EACH_BB_FN (bb, cfun)
628 if (!bitmap_bit_p (&bb_antic_flags, bb->index))
629 continue;
630 FOR_EACH_EDGE (e, ei, bb->preds)
631 if (!bitmap_bit_p (&bb_antic_flags, e->src->index))
633 if (*entry_edge != orig_entry_edge)
635 *entry_edge = orig_entry_edge;
636 if (dump_file)
637 fprintf (dump_file, "More than one candidate edge.\n");
638 goto fail_shrinkwrap;
640 if (dump_file)
641 fprintf (dump_file, "Found candidate edge for "
642 "shrink-wrapping, %d->%d.\n", e->src->index,
643 e->dest->index);
644 *entry_edge = e;
648 if (*entry_edge != orig_entry_edge)
650 /* Test whether the prologue is known to clobber any register
651 (other than FP or SP) which are live on the edge. */
652 CLEAR_HARD_REG_BIT (prologue_clobbered, STACK_POINTER_REGNUM);
653 if (frame_pointer_needed)
654 CLEAR_HARD_REG_BIT (prologue_clobbered, HARD_FRAME_POINTER_REGNUM);
655 REG_SET_TO_HARD_REG_SET (live_on_edge,
656 df_get_live_in ((*entry_edge)->dest));
657 if (hard_reg_set_intersect_p (live_on_edge, prologue_clobbered))
659 *entry_edge = orig_entry_edge;
660 if (dump_file)
661 fprintf (dump_file,
662 "Shrink-wrapping aborted due to clobber.\n");
665 if (*entry_edge != orig_entry_edge)
667 crtl->shrink_wrapped = true;
668 if (dump_file)
669 fprintf (dump_file, "Performing shrink-wrapping.\n");
671 /* Find tail blocks reachable from both blocks needing a
672 prologue and blocks not needing a prologue. */
673 if (!bitmap_empty_p (&bb_tail))
674 FOR_EACH_BB_FN (bb, cfun)
676 bool some_pro, some_no_pro;
677 if (!bitmap_bit_p (&bb_tail, bb->index))
678 continue;
679 some_pro = some_no_pro = false;
680 FOR_EACH_EDGE (e, ei, bb->preds)
682 if (bitmap_bit_p (bb_flags, e->src->index))
683 some_pro = true;
684 else
685 some_no_pro = true;
687 if (some_pro && some_no_pro)
688 vec.quick_push (bb);
689 else
690 bitmap_clear_bit (&bb_tail, bb->index);
692 /* Find the head of each tail. */
693 while (!vec.is_empty ())
695 basic_block tbb = vec.pop ();
697 if (!bitmap_bit_p (&bb_tail, tbb->index))
698 continue;
700 while (single_succ_p (tbb))
702 tbb = single_succ (tbb);
703 bitmap_clear_bit (&bb_tail, tbb->index);
706 /* Now duplicate the tails. */
707 if (!bitmap_empty_p (&bb_tail))
708 FOR_EACH_BB_REVERSE_FN (bb, cfun)
710 basic_block copy_bb, tbb;
711 rtx_insn *insert_point;
712 int eflags;
714 if (!bitmap_clear_bit (&bb_tail, bb->index))
715 continue;
717 /* Create a copy of BB, instructions and all, for
718 use on paths that don't need a prologue.
719 Ideal placement of the copy is on a fall-thru edge
720 or after a block that would jump to the copy. */
721 FOR_EACH_EDGE (e, ei, bb->preds)
722 if (!bitmap_bit_p (bb_flags, e->src->index)
723 && single_succ_p (e->src))
724 break;
725 if (e)
727 /* Make sure we insert after any barriers. */
728 rtx_insn *end = get_last_bb_insn (e->src);
729 copy_bb = create_basic_block (NEXT_INSN (end),
730 NULL_RTX, e->src);
731 BB_COPY_PARTITION (copy_bb, e->src);
733 else
735 /* Otherwise put the copy at the end of the function. */
736 copy_bb = create_basic_block (NULL_RTX, NULL_RTX,
737 EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb);
738 BB_COPY_PARTITION (copy_bb, bb);
741 insert_point = emit_note_after (NOTE_INSN_DELETED,
742 BB_END (copy_bb));
743 emit_barrier_after (BB_END (copy_bb));
745 tbb = bb;
746 while (1)
748 dup_block_and_redirect (tbb, copy_bb, insert_point,
749 bb_flags);
750 tbb = single_succ (tbb);
751 if (tbb == EXIT_BLOCK_PTR_FOR_FN (cfun))
752 break;
753 e = split_block (copy_bb, PREV_INSN (insert_point));
754 copy_bb = e->dest;
757 /* Quiet verify_flow_info by (ab)using EDGE_FAKE.
758 We have yet to add a simple_return to the tails,
759 as we'd like to first convert_jumps_to_returns in
760 case the block is no longer used after that. */
761 eflags = EDGE_FAKE;
762 if (CALL_P (PREV_INSN (insert_point))
763 && SIBLING_CALL_P (PREV_INSN (insert_point)))
764 eflags = EDGE_SIBCALL | EDGE_ABNORMAL;
765 make_single_succ_edge (copy_bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
766 eflags);
768 /* verify_flow_info doesn't like a note after a
769 sibling call. */
770 delete_insn (insert_point);
771 if (bitmap_empty_p (&bb_tail))
772 break;
776 fail_shrinkwrap:
777 bitmap_clear (&bb_tail);
778 bitmap_clear (&bb_antic_flags);
779 bitmap_clear (&bb_on_list);
780 vec.release ();
784 /* If we're allowed to generate a simple return instruction, then by
785 definition we don't need a full epilogue. If the last basic
786 block before the exit block does not contain active instructions,
787 examine its predecessors and try to emit (conditional) return
788 instructions. */
790 edge
791 get_unconverted_simple_return (edge exit_fallthru_edge, bitmap_head bb_flags,
792 vec<edge> *unconverted_simple_returns,
793 rtx_insn **returnjump)
795 if (optimize)
797 unsigned i, last;
799 /* convert_jumps_to_returns may add to preds of the exit block
800 (but won't remove). Stop at end of current preds. */
801 last = EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
802 for (i = 0; i < last; i++)
804 edge e = EDGE_I (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds, i);
805 if (LABEL_P (BB_HEAD (e->src))
806 && !bitmap_bit_p (&bb_flags, e->src->index)
807 && !active_insn_between (BB_HEAD (e->src), BB_END (e->src)))
808 *unconverted_simple_returns
809 = convert_jumps_to_returns (e->src, true,
810 *unconverted_simple_returns);
814 if (exit_fallthru_edge != NULL
815 && EDGE_COUNT (exit_fallthru_edge->src->preds) != 0
816 && !bitmap_bit_p (&bb_flags, exit_fallthru_edge->src->index))
818 basic_block last_bb;
820 last_bb = emit_return_for_exit (exit_fallthru_edge, true);
821 *returnjump = BB_END (last_bb);
822 exit_fallthru_edge = NULL;
824 return exit_fallthru_edge;
827 /* If there were branches to an empty LAST_BB which we tried to
828 convert to conditional simple_returns, but couldn't for some
829 reason, create a block to hold a simple_return insn and redirect
830 those remaining edges. */
832 void
833 convert_to_simple_return (edge entry_edge, edge orig_entry_edge,
834 bitmap_head bb_flags, rtx_insn *returnjump,
835 vec<edge> unconverted_simple_returns)
837 edge e;
838 edge_iterator ei;
840 if (!unconverted_simple_returns.is_empty ())
842 basic_block simple_return_block_hot = NULL;
843 basic_block simple_return_block_cold = NULL;
844 edge pending_edge_hot = NULL;
845 edge pending_edge_cold = NULL;
846 basic_block exit_pred;
847 int i;
849 gcc_assert (entry_edge != orig_entry_edge);
851 /* See if we can reuse the last insn that was emitted for the
852 epilogue. */
853 if (returnjump != NULL_RTX
854 && JUMP_LABEL (returnjump) == simple_return_rtx)
856 e = split_block (BLOCK_FOR_INSN (returnjump), PREV_INSN (returnjump));
857 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
858 simple_return_block_hot = e->dest;
859 else
860 simple_return_block_cold = e->dest;
863 /* Also check returns we might need to add to tail blocks. */
864 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
865 if (EDGE_COUNT (e->src->preds) != 0
866 && (e->flags & EDGE_FAKE) != 0
867 && !bitmap_bit_p (&bb_flags, e->src->index))
869 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
870 pending_edge_hot = e;
871 else
872 pending_edge_cold = e;
875 /* Save a pointer to the exit's predecessor BB for use in
876 inserting new BBs at the end of the function. Do this
877 after the call to split_block above which may split
878 the original exit pred. */
879 exit_pred = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
881 FOR_EACH_VEC_ELT (unconverted_simple_returns, i, e)
883 basic_block *pdest_bb;
884 edge pending;
886 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
888 pdest_bb = &simple_return_block_hot;
889 pending = pending_edge_hot;
891 else
893 pdest_bb = &simple_return_block_cold;
894 pending = pending_edge_cold;
897 if (*pdest_bb == NULL && pending != NULL)
899 emit_return_into_block (true, pending->src);
900 pending->flags &= ~(EDGE_FALLTHRU | EDGE_FAKE);
901 *pdest_bb = pending->src;
903 else if (*pdest_bb == NULL)
905 basic_block bb;
906 rtx_insn *start;
908 bb = create_basic_block (NULL, NULL, exit_pred);
909 BB_COPY_PARTITION (bb, e->src);
910 start = emit_jump_insn_after (gen_simple_return (),
911 BB_END (bb));
912 JUMP_LABEL (start) = simple_return_rtx;
913 emit_barrier_after (start);
915 *pdest_bb = bb;
916 make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
918 redirect_edge_and_branch_force (e, *pdest_bb);
920 unconverted_simple_returns.release ();
923 if (entry_edge != orig_entry_edge)
925 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
926 if (EDGE_COUNT (e->src->preds) != 0
927 && (e->flags & EDGE_FAKE) != 0
928 && !bitmap_bit_p (&bb_flags, e->src->index))
930 emit_return_into_block (true, e->src);
931 e->flags &= ~(EDGE_FALLTHRU | EDGE_FAKE);
936 #endif