Daily bump.
[official-gcc.git] / gcc / shrink-wrap.c
blobbd4813c82ae42400b678b08da76f3fad994dddcf
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"
56 #include "rtl-iter.h"
58 #ifdef HAVE_simple_return
60 /* Return true if INSN requires the stack frame to be set up.
61 PROLOGUE_USED contains the hard registers used in the function
62 prologue. SET_UP_BY_PROLOGUE is the set of registers we expect the
63 prologue to set up for the function. */
64 bool
65 requires_stack_frame_p (rtx_insn *insn, HARD_REG_SET prologue_used,
66 HARD_REG_SET set_up_by_prologue)
68 df_ref def, use;
69 HARD_REG_SET hardregs;
70 unsigned regno;
72 if (CALL_P (insn))
73 return !SIBLING_CALL_P (insn);
75 /* We need a frame to get the unique CFA expected by the unwinder. */
76 if (cfun->can_throw_non_call_exceptions && can_throw_internal (insn))
77 return true;
79 CLEAR_HARD_REG_SET (hardregs);
80 FOR_EACH_INSN_DEF (def, insn)
82 rtx dreg = DF_REF_REG (def);
84 if (!REG_P (dreg))
85 continue;
87 add_to_hard_reg_set (&hardregs, GET_MODE (dreg),
88 REGNO (dreg));
90 if (hard_reg_set_intersect_p (hardregs, prologue_used))
91 return true;
92 AND_COMPL_HARD_REG_SET (hardregs, call_used_reg_set);
93 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
94 if (TEST_HARD_REG_BIT (hardregs, regno)
95 && df_regs_ever_live_p (regno))
96 return true;
98 FOR_EACH_INSN_USE (use, insn)
100 rtx reg = DF_REF_REG (use);
102 if (!REG_P (reg))
103 continue;
105 add_to_hard_reg_set (&hardregs, GET_MODE (reg),
106 REGNO (reg));
108 if (hard_reg_set_intersect_p (hardregs, set_up_by_prologue))
109 return true;
111 return false;
114 /* See whether there has a single live edge from BB, which dest uses
115 [REGNO, END_REGNO). Return the live edge if its dest bb has
116 one or two predecessors. Otherwise return NULL. */
118 static edge
119 live_edge_for_reg (basic_block bb, int regno, int end_regno)
121 edge e, live_edge;
122 edge_iterator ei;
123 bitmap live;
124 int i;
126 live_edge = NULL;
127 FOR_EACH_EDGE (e, ei, bb->succs)
129 live = df_get_live_in (e->dest);
130 for (i = regno; i < end_regno; i++)
131 if (REGNO_REG_SET_P (live, i))
133 if (live_edge && live_edge != e)
134 return NULL;
135 live_edge = e;
139 /* We can sometimes encounter dead code. Don't try to move it
140 into the exit block. */
141 if (!live_edge || live_edge->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
142 return NULL;
144 /* Reject targets of abnormal edges. This is needed for correctness
145 on ports like Alpha and MIPS, whose pic_offset_table_rtx can die on
146 exception edges even though it is generally treated as call-saved
147 for the majority of the compilation. Moving across abnormal edges
148 isn't going to be interesting for shrink-wrap usage anyway. */
149 if (live_edge->flags & EDGE_ABNORMAL)
150 return NULL;
152 /* When live_edge->dest->preds == 2, we can create a new block on
153 the edge to make it meet the requirement. */
154 if (EDGE_COUNT (live_edge->dest->preds) > 2)
155 return NULL;
157 return live_edge;
160 /* Try to move INSN from BB to a successor. Return true on success.
161 USES and DEFS are the set of registers that are used and defined
162 after INSN in BB. SPLIT_P indicates whether a live edge from BB
163 is splitted or not. */
165 static bool
166 move_insn_for_shrink_wrap (basic_block bb, rtx_insn *insn,
167 const HARD_REG_SET uses,
168 const HARD_REG_SET defs,
169 bool *split_p)
171 rtx set, src, dest;
172 bitmap live_out, live_in, bb_uses, bb_defs;
173 unsigned int i, dregno, end_dregno;
174 unsigned int sregno = FIRST_PSEUDO_REGISTER;
175 unsigned int end_sregno = FIRST_PSEUDO_REGISTER;
176 basic_block next_block;
177 edge live_edge;
179 /* Look for a simple register copy. */
180 set = single_set (insn);
181 if (!set)
182 return false;
183 src = SET_SRC (set);
184 dest = SET_DEST (set);
186 if (!REG_P (src))
188 unsigned int reg_num = 0;
189 unsigned int nonconstobj_num = 0;
190 rtx src_inner = NULL_RTX;
192 subrtx_var_iterator::array_type array;
193 FOR_EACH_SUBRTX_VAR (iter, array, src, ALL)
195 rtx x = *iter;
196 if (REG_P (x))
198 reg_num++;
199 src_inner = x;
201 else if (!CONSTANT_P (x) && OBJECT_P (x))
202 nonconstobj_num++;
205 if (nonconstobj_num > 0
206 || reg_num > 1)
207 src = NULL_RTX;
208 else if (reg_num == 1)
209 src = src_inner;
212 if (!REG_P (dest) || src == NULL_RTX
213 /* STACK or FRAME related adjustment might be part of prologue.
214 So keep them in the entry block. */
215 || dest == stack_pointer_rtx
216 || dest == frame_pointer_rtx
217 || dest == hard_frame_pointer_rtx)
218 return false;
220 /* Make sure that the source register isn't defined later in BB. */
221 if (REG_P (src))
223 sregno = REGNO (src);
224 end_sregno = END_REGNO (src);
225 if (overlaps_hard_reg_set_p (defs, GET_MODE (src), sregno))
226 return false;
229 /* Make sure that the destination register isn't referenced later in BB. */
230 dregno = REGNO (dest);
231 end_dregno = END_REGNO (dest);
232 if (overlaps_hard_reg_set_p (uses, GET_MODE (dest), dregno)
233 || overlaps_hard_reg_set_p (defs, GET_MODE (dest), dregno))
234 return false;
236 /* See whether there is a successor block to which we could move INSN. */
237 live_edge = live_edge_for_reg (bb, dregno, end_dregno);
238 if (!live_edge)
239 return false;
241 next_block = live_edge->dest;
242 /* Create a new basic block on the edge. */
243 if (EDGE_COUNT (next_block->preds) == 2)
245 /* split_edge for a block with only one successor is meaningless. */
246 if (EDGE_COUNT (bb->succs) == 1)
247 return false;
249 /* If DF_LIVE doesn't exist, i.e. at -O1, just give up. */
250 if (!df_live)
251 return false;
253 basic_block old_dest = live_edge->dest;
254 next_block = split_edge (live_edge);
256 /* We create a new basic block. Call df_grow_bb_info to make sure
257 all data structures are allocated. */
258 df_grow_bb_info (df_live);
260 bitmap_and (df_get_live_in (next_block), df_get_live_out (bb),
261 df_get_live_in (old_dest));
262 df_set_bb_dirty (next_block);
264 /* We should not split more than once for a function. */
265 if (*split_p)
266 return false;
268 *split_p = true;
271 /* At this point we are committed to moving INSN, but let's try to
272 move it as far as we can. */
275 live_out = df_get_live_out (bb);
276 live_in = df_get_live_in (next_block);
277 bb = next_block;
279 /* Check whether BB uses DEST or clobbers DEST. We need to add
280 INSN to BB if so. Either way, DEST is no longer live on entry,
281 except for any part that overlaps SRC (next loop). */
282 bb_uses = &DF_LR_BB_INFO (bb)->use;
283 bb_defs = &DF_LR_BB_INFO (bb)->def;
284 if (df_live)
286 for (i = dregno; i < end_dregno; i++)
288 if (*split_p
289 || REGNO_REG_SET_P (bb_uses, i)
290 || REGNO_REG_SET_P (bb_defs, i)
291 || REGNO_REG_SET_P (&DF_LIVE_BB_INFO (bb)->gen, i))
292 next_block = NULL;
293 CLEAR_REGNO_REG_SET (live_out, i);
294 CLEAR_REGNO_REG_SET (live_in, i);
297 /* Check whether BB clobbers SRC. We need to add INSN to BB if so.
298 Either way, SRC is now live on entry. */
299 for (i = sregno; i < end_sregno; i++)
301 if (*split_p
302 || REGNO_REG_SET_P (bb_defs, i)
303 || REGNO_REG_SET_P (&DF_LIVE_BB_INFO (bb)->gen, i))
304 next_block = NULL;
305 SET_REGNO_REG_SET (live_out, i);
306 SET_REGNO_REG_SET (live_in, i);
309 else
311 /* DF_LR_BB_INFO (bb)->def does not comprise the DF_REF_PARTIAL and
312 DF_REF_CONDITIONAL defs. So if DF_LIVE doesn't exist, i.e.
313 at -O1, just give up searching NEXT_BLOCK. */
314 next_block = NULL;
315 for (i = dregno; i < end_dregno; i++)
317 CLEAR_REGNO_REG_SET (live_out, i);
318 CLEAR_REGNO_REG_SET (live_in, i);
321 for (i = sregno; i < end_sregno; i++)
323 SET_REGNO_REG_SET (live_out, i);
324 SET_REGNO_REG_SET (live_in, i);
328 /* If we don't need to add the move to BB, look for a single
329 successor block. */
330 if (next_block)
332 live_edge = live_edge_for_reg (next_block, dregno, end_dregno);
333 if (!live_edge || EDGE_COUNT (live_edge->dest->preds) > 1)
334 break;
335 next_block = live_edge->dest;
338 while (next_block);
340 /* For the new created basic block, there is no dataflow info at all.
341 So skip the following dataflow update and check. */
342 if (!(*split_p))
344 /* BB now defines DEST. It only uses the parts of DEST that overlap SRC
345 (next loop). */
346 for (i = dregno; i < end_dregno; i++)
348 CLEAR_REGNO_REG_SET (bb_uses, i);
349 SET_REGNO_REG_SET (bb_defs, i);
352 /* BB now uses SRC. */
353 for (i = sregno; i < end_sregno; i++)
354 SET_REGNO_REG_SET (bb_uses, i);
357 emit_insn_after (PATTERN (insn), bb_note (bb));
358 delete_insn (insn);
359 return true;
362 /* Look for register copies in the first block of the function, and move
363 them down into successor blocks if the register is used only on one
364 path. This exposes more opportunities for shrink-wrapping. These
365 kinds of sets often occur when incoming argument registers are moved
366 to call-saved registers because their values are live across one or
367 more calls during the function. */
369 void
370 prepare_shrink_wrap (basic_block entry_block)
372 rtx_insn *insn, *curr;
373 rtx x;
374 HARD_REG_SET uses, defs;
375 df_ref def, use;
376 bool split_p = false;
378 if (JUMP_P (BB_END (entry_block)))
380 /* To have more shrink-wrapping opportunities, prepare_shrink_wrap tries
381 to sink the copies from parameter to callee saved register out of
382 entry block. copyprop_hardreg_forward_bb_without_debug_insn is called
383 to release some dependences. */
384 copyprop_hardreg_forward_bb_without_debug_insn (entry_block);
387 CLEAR_HARD_REG_SET (uses);
388 CLEAR_HARD_REG_SET (defs);
389 FOR_BB_INSNS_REVERSE_SAFE (entry_block, insn, curr)
390 if (NONDEBUG_INSN_P (insn)
391 && !move_insn_for_shrink_wrap (entry_block, insn, uses, defs,
392 &split_p))
394 /* Add all defined registers to DEFs. */
395 FOR_EACH_INSN_DEF (def, insn)
397 x = DF_REF_REG (def);
398 if (REG_P (x) && HARD_REGISTER_P (x))
399 SET_HARD_REG_BIT (defs, REGNO (x));
402 /* Add all used registers to USESs. */
403 FOR_EACH_INSN_USE (use, insn)
405 x = DF_REF_REG (use);
406 if (REG_P (x) && HARD_REGISTER_P (x))
407 SET_HARD_REG_BIT (uses, REGNO (x));
412 /* Create a copy of BB instructions and insert at BEFORE. Redirect
413 preds of BB to COPY_BB if they don't appear in NEED_PROLOGUE. */
414 void
415 dup_block_and_redirect (basic_block bb, basic_block copy_bb, rtx_insn *before,
416 bitmap_head *need_prologue)
418 edge_iterator ei;
419 edge e;
420 rtx_insn *insn = BB_END (bb);
422 /* We know BB has a single successor, so there is no need to copy a
423 simple jump at the end of BB. */
424 if (simplejump_p (insn))
425 insn = PREV_INSN (insn);
427 start_sequence ();
428 duplicate_insn_chain (BB_HEAD (bb), insn);
429 if (dump_file)
431 unsigned count = 0;
432 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
433 if (active_insn_p (insn))
434 ++count;
435 fprintf (dump_file, "Duplicating bb %d to bb %d, %u active insns.\n",
436 bb->index, copy_bb->index, count);
438 insn = get_insns ();
439 end_sequence ();
440 emit_insn_before (insn, before);
442 /* Redirect all the paths that need no prologue into copy_bb. */
443 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei));)
444 if (!bitmap_bit_p (need_prologue, e->src->index))
446 int freq = EDGE_FREQUENCY (e);
447 copy_bb->count += e->count;
448 copy_bb->frequency += EDGE_FREQUENCY (e);
449 e->dest->count -= e->count;
450 if (e->dest->count < 0)
451 e->dest->count = 0;
452 e->dest->frequency -= freq;
453 if (e->dest->frequency < 0)
454 e->dest->frequency = 0;
455 redirect_edge_and_branch_force (e, copy_bb);
456 continue;
458 else
459 ei_next (&ei);
463 /* Try to perform a kind of shrink-wrapping, making sure the
464 prologue/epilogue is emitted only around those parts of the
465 function that require it. */
467 void
468 try_shrink_wrapping (edge *entry_edge, edge orig_entry_edge,
469 bitmap_head *bb_flags, rtx_insn *prologue_seq)
471 edge e;
472 edge_iterator ei;
473 bool nonempty_prologue = false;
474 unsigned max_grow_size;
475 rtx_insn *seq;
477 for (seq = prologue_seq; seq; seq = NEXT_INSN (seq))
478 if (!NOTE_P (seq) || NOTE_KIND (seq) != NOTE_INSN_PROLOGUE_END)
480 nonempty_prologue = true;
481 break;
484 if (flag_shrink_wrap && HAVE_simple_return
485 && (targetm.profile_before_prologue () || !crtl->profile)
486 && nonempty_prologue && !crtl->calls_eh_return)
488 HARD_REG_SET prologue_clobbered, prologue_used, live_on_edge;
489 struct hard_reg_set_container set_up_by_prologue;
490 rtx_insn *p_insn;
491 vec<basic_block> vec;
492 basic_block bb;
493 bitmap_head bb_antic_flags;
494 bitmap_head bb_on_list;
495 bitmap_head bb_tail;
497 if (dump_file)
498 fprintf (dump_file, "Attempting shrink-wrapping optimization.\n");
500 /* Compute the registers set and used in the prologue. */
501 CLEAR_HARD_REG_SET (prologue_clobbered);
502 CLEAR_HARD_REG_SET (prologue_used);
503 for (p_insn = prologue_seq; p_insn; p_insn = NEXT_INSN (p_insn))
505 HARD_REG_SET this_used;
506 if (!NONDEBUG_INSN_P (p_insn))
507 continue;
509 CLEAR_HARD_REG_SET (this_used);
510 note_uses (&PATTERN (p_insn), record_hard_reg_uses,
511 &this_used);
512 AND_COMPL_HARD_REG_SET (this_used, prologue_clobbered);
513 IOR_HARD_REG_SET (prologue_used, this_used);
514 note_stores (PATTERN (p_insn), record_hard_reg_sets,
515 &prologue_clobbered);
518 prepare_shrink_wrap ((*entry_edge)->dest);
520 bitmap_initialize (&bb_antic_flags, &bitmap_default_obstack);
521 bitmap_initialize (&bb_on_list, &bitmap_default_obstack);
522 bitmap_initialize (&bb_tail, &bitmap_default_obstack);
524 /* Find the set of basic blocks that require a stack frame,
525 and blocks that are too big to be duplicated. */
527 vec.create (n_basic_blocks_for_fn (cfun));
529 CLEAR_HARD_REG_SET (set_up_by_prologue.set);
530 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
531 STACK_POINTER_REGNUM);
532 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode, ARG_POINTER_REGNUM);
533 if (frame_pointer_needed)
534 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
535 HARD_FRAME_POINTER_REGNUM);
536 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
537 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
538 PIC_OFFSET_TABLE_REGNUM);
539 if (crtl->drap_reg)
540 add_to_hard_reg_set (&set_up_by_prologue.set,
541 GET_MODE (crtl->drap_reg),
542 REGNO (crtl->drap_reg));
543 if (targetm.set_up_by_prologue)
544 targetm.set_up_by_prologue (&set_up_by_prologue);
546 /* We don't use a different max size depending on
547 optimize_bb_for_speed_p because increasing shrink-wrapping
548 opportunities by duplicating tail blocks can actually result
549 in an overall decrease in code size. */
550 max_grow_size = get_uncond_jump_length ();
551 max_grow_size *= PARAM_VALUE (PARAM_MAX_GROW_COPY_BB_INSNS);
553 FOR_EACH_BB_FN (bb, cfun)
555 rtx_insn *insn;
556 unsigned size = 0;
558 FOR_BB_INSNS (bb, insn)
559 if (NONDEBUG_INSN_P (insn))
561 if (requires_stack_frame_p (insn, prologue_used,
562 set_up_by_prologue.set))
564 if (bb == (*entry_edge)->dest)
565 goto fail_shrinkwrap;
566 bitmap_set_bit (bb_flags, bb->index);
567 vec.quick_push (bb);
568 break;
570 else if (size <= max_grow_size)
572 size += get_attr_min_length (insn);
573 if (size > max_grow_size)
574 bitmap_set_bit (&bb_on_list, bb->index);
579 /* Blocks that really need a prologue, or are too big for tails. */
580 bitmap_ior_into (&bb_on_list, bb_flags);
582 /* For every basic block that needs a prologue, mark all blocks
583 reachable from it, so as to ensure they are also seen as
584 requiring a prologue. */
585 while (!vec.is_empty ())
587 basic_block tmp_bb = vec.pop ();
589 FOR_EACH_EDGE (e, ei, tmp_bb->succs)
590 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
591 && bitmap_set_bit (bb_flags, e->dest->index))
592 vec.quick_push (e->dest);
595 /* Find the set of basic blocks that need no prologue, have a
596 single successor, can be duplicated, meet a max size
597 requirement, and go to the exit via like blocks. */
598 vec.quick_push (EXIT_BLOCK_PTR_FOR_FN (cfun));
599 while (!vec.is_empty ())
601 basic_block tmp_bb = vec.pop ();
603 FOR_EACH_EDGE (e, ei, tmp_bb->preds)
604 if (single_succ_p (e->src)
605 && !bitmap_bit_p (&bb_on_list, e->src->index)
606 && can_duplicate_block_p (e->src))
608 edge pe;
609 edge_iterator pei;
611 /* If there is predecessor of e->src which doesn't
612 need prologue and the edge is complex,
613 we might not be able to redirect the branch
614 to a copy of e->src. */
615 FOR_EACH_EDGE (pe, pei, e->src->preds)
616 if ((pe->flags & EDGE_COMPLEX) != 0
617 && !bitmap_bit_p (bb_flags, pe->src->index))
618 break;
619 if (pe == NULL && bitmap_set_bit (&bb_tail, e->src->index))
620 vec.quick_push (e->src);
624 /* Now walk backwards from every block that is marked as needing
625 a prologue to compute the bb_antic_flags bitmap. Exclude
626 tail blocks; They can be duplicated to be used on paths not
627 needing a prologue. */
628 bitmap_clear (&bb_on_list);
629 bitmap_and_compl (&bb_antic_flags, bb_flags, &bb_tail);
630 FOR_EACH_BB_FN (bb, cfun)
632 if (!bitmap_bit_p (&bb_antic_flags, bb->index))
633 continue;
634 FOR_EACH_EDGE (e, ei, bb->preds)
635 if (!bitmap_bit_p (&bb_antic_flags, e->src->index)
636 && bitmap_set_bit (&bb_on_list, e->src->index))
637 vec.quick_push (e->src);
639 while (!vec.is_empty ())
641 basic_block tmp_bb = vec.pop ();
642 bool all_set = true;
644 bitmap_clear_bit (&bb_on_list, tmp_bb->index);
645 FOR_EACH_EDGE (e, ei, tmp_bb->succs)
646 if (!bitmap_bit_p (&bb_antic_flags, e->dest->index))
648 all_set = false;
649 break;
652 if (all_set)
654 bitmap_set_bit (&bb_antic_flags, tmp_bb->index);
655 FOR_EACH_EDGE (e, ei, tmp_bb->preds)
656 if (!bitmap_bit_p (&bb_antic_flags, e->src->index)
657 && bitmap_set_bit (&bb_on_list, e->src->index))
658 vec.quick_push (e->src);
661 /* Find exactly one edge that leads to a block in ANTIC from
662 a block that isn't. */
663 if (!bitmap_bit_p (&bb_antic_flags, (*entry_edge)->dest->index))
664 FOR_EACH_BB_FN (bb, cfun)
666 if (!bitmap_bit_p (&bb_antic_flags, bb->index))
667 continue;
668 FOR_EACH_EDGE (e, ei, bb->preds)
669 if (!bitmap_bit_p (&bb_antic_flags, e->src->index))
671 if (*entry_edge != orig_entry_edge)
673 *entry_edge = orig_entry_edge;
674 if (dump_file)
675 fprintf (dump_file, "More than one candidate edge.\n");
676 goto fail_shrinkwrap;
678 if (dump_file)
679 fprintf (dump_file, "Found candidate edge for "
680 "shrink-wrapping, %d->%d.\n", e->src->index,
681 e->dest->index);
682 *entry_edge = e;
686 if (*entry_edge != orig_entry_edge)
688 /* Test whether the prologue is known to clobber any register
689 (other than FP or SP) which are live on the edge. */
690 CLEAR_HARD_REG_BIT (prologue_clobbered, STACK_POINTER_REGNUM);
691 if (frame_pointer_needed)
692 CLEAR_HARD_REG_BIT (prologue_clobbered, HARD_FRAME_POINTER_REGNUM);
693 REG_SET_TO_HARD_REG_SET (live_on_edge,
694 df_get_live_in ((*entry_edge)->dest));
695 if (hard_reg_set_intersect_p (live_on_edge, prologue_clobbered))
697 *entry_edge = orig_entry_edge;
698 if (dump_file)
699 fprintf (dump_file,
700 "Shrink-wrapping aborted due to clobber.\n");
703 if (*entry_edge != orig_entry_edge)
705 crtl->shrink_wrapped = true;
706 if (dump_file)
707 fprintf (dump_file, "Performing shrink-wrapping.\n");
709 /* Find tail blocks reachable from both blocks needing a
710 prologue and blocks not needing a prologue. */
711 if (!bitmap_empty_p (&bb_tail))
712 FOR_EACH_BB_FN (bb, cfun)
714 bool some_pro, some_no_pro;
715 if (!bitmap_bit_p (&bb_tail, bb->index))
716 continue;
717 some_pro = some_no_pro = false;
718 FOR_EACH_EDGE (e, ei, bb->preds)
720 if (bitmap_bit_p (bb_flags, e->src->index))
721 some_pro = true;
722 else
723 some_no_pro = true;
725 if (some_pro && some_no_pro)
726 vec.quick_push (bb);
727 else
728 bitmap_clear_bit (&bb_tail, bb->index);
730 /* Find the head of each tail. */
731 while (!vec.is_empty ())
733 basic_block tbb = vec.pop ();
735 if (!bitmap_bit_p (&bb_tail, tbb->index))
736 continue;
738 while (single_succ_p (tbb))
740 tbb = single_succ (tbb);
741 bitmap_clear_bit (&bb_tail, tbb->index);
744 /* Now duplicate the tails. */
745 if (!bitmap_empty_p (&bb_tail))
746 FOR_EACH_BB_REVERSE_FN (bb, cfun)
748 basic_block copy_bb, tbb;
749 rtx_insn *insert_point;
750 int eflags;
752 if (!bitmap_clear_bit (&bb_tail, bb->index))
753 continue;
755 /* Create a copy of BB, instructions and all, for
756 use on paths that don't need a prologue.
757 Ideal placement of the copy is on a fall-thru edge
758 or after a block that would jump to the copy. */
759 FOR_EACH_EDGE (e, ei, bb->preds)
760 if (!bitmap_bit_p (bb_flags, e->src->index)
761 && single_succ_p (e->src))
762 break;
763 if (e)
765 /* Make sure we insert after any barriers. */
766 rtx_insn *end = get_last_bb_insn (e->src);
767 copy_bb = create_basic_block (NEXT_INSN (end),
768 NULL_RTX, e->src);
769 BB_COPY_PARTITION (copy_bb, e->src);
771 else
773 /* Otherwise put the copy at the end of the function. */
774 copy_bb = create_basic_block (NULL_RTX, NULL_RTX,
775 EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb);
776 BB_COPY_PARTITION (copy_bb, bb);
779 insert_point = emit_note_after (NOTE_INSN_DELETED,
780 BB_END (copy_bb));
781 emit_barrier_after (BB_END (copy_bb));
783 tbb = bb;
784 while (1)
786 dup_block_and_redirect (tbb, copy_bb, insert_point,
787 bb_flags);
788 tbb = single_succ (tbb);
789 if (tbb == EXIT_BLOCK_PTR_FOR_FN (cfun))
790 break;
791 e = split_block (copy_bb, PREV_INSN (insert_point));
792 copy_bb = e->dest;
795 /* Quiet verify_flow_info by (ab)using EDGE_FAKE.
796 We have yet to add a simple_return to the tails,
797 as we'd like to first convert_jumps_to_returns in
798 case the block is no longer used after that. */
799 eflags = EDGE_FAKE;
800 if (CALL_P (PREV_INSN (insert_point))
801 && SIBLING_CALL_P (PREV_INSN (insert_point)))
802 eflags = EDGE_SIBCALL | EDGE_ABNORMAL;
803 make_single_succ_edge (copy_bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
804 eflags);
806 /* verify_flow_info doesn't like a note after a
807 sibling call. */
808 delete_insn (insert_point);
809 if (bitmap_empty_p (&bb_tail))
810 break;
814 fail_shrinkwrap:
815 bitmap_clear (&bb_tail);
816 bitmap_clear (&bb_antic_flags);
817 bitmap_clear (&bb_on_list);
818 vec.release ();
822 /* If we're allowed to generate a simple return instruction, then by
823 definition we don't need a full epilogue. If the last basic
824 block before the exit block does not contain active instructions,
825 examine its predecessors and try to emit (conditional) return
826 instructions. */
828 edge
829 get_unconverted_simple_return (edge exit_fallthru_edge, bitmap_head bb_flags,
830 vec<edge> *unconverted_simple_returns,
831 rtx_insn **returnjump)
833 if (optimize)
835 unsigned i, last;
837 /* convert_jumps_to_returns may add to preds of the exit block
838 (but won't remove). Stop at end of current preds. */
839 last = EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
840 for (i = 0; i < last; i++)
842 edge e = EDGE_I (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds, i);
843 if (LABEL_P (BB_HEAD (e->src))
844 && !bitmap_bit_p (&bb_flags, e->src->index)
845 && !active_insn_between (BB_HEAD (e->src), BB_END (e->src)))
846 *unconverted_simple_returns
847 = convert_jumps_to_returns (e->src, true,
848 *unconverted_simple_returns);
852 if (exit_fallthru_edge != NULL
853 && EDGE_COUNT (exit_fallthru_edge->src->preds) != 0
854 && !bitmap_bit_p (&bb_flags, exit_fallthru_edge->src->index))
856 basic_block last_bb;
858 last_bb = emit_return_for_exit (exit_fallthru_edge, true);
859 *returnjump = BB_END (last_bb);
860 exit_fallthru_edge = NULL;
862 return exit_fallthru_edge;
865 /* If there were branches to an empty LAST_BB which we tried to
866 convert to conditional simple_returns, but couldn't for some
867 reason, create a block to hold a simple_return insn and redirect
868 those remaining edges. */
870 void
871 convert_to_simple_return (edge entry_edge, edge orig_entry_edge,
872 bitmap_head bb_flags, rtx_insn *returnjump,
873 vec<edge> unconverted_simple_returns)
875 edge e;
876 edge_iterator ei;
878 if (!unconverted_simple_returns.is_empty ())
880 basic_block simple_return_block_hot = NULL;
881 basic_block simple_return_block_cold = NULL;
882 edge pending_edge_hot = NULL;
883 edge pending_edge_cold = NULL;
884 basic_block exit_pred;
885 int i;
887 gcc_assert (entry_edge != orig_entry_edge);
889 /* See if we can reuse the last insn that was emitted for the
890 epilogue. */
891 if (returnjump != NULL_RTX
892 && JUMP_LABEL (returnjump) == simple_return_rtx)
894 e = split_block (BLOCK_FOR_INSN (returnjump), PREV_INSN (returnjump));
895 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
896 simple_return_block_hot = e->dest;
897 else
898 simple_return_block_cold = e->dest;
901 /* Also check returns we might need to add to tail blocks. */
902 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
903 if (EDGE_COUNT (e->src->preds) != 0
904 && (e->flags & EDGE_FAKE) != 0
905 && !bitmap_bit_p (&bb_flags, e->src->index))
907 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
908 pending_edge_hot = e;
909 else
910 pending_edge_cold = e;
913 /* Save a pointer to the exit's predecessor BB for use in
914 inserting new BBs at the end of the function. Do this
915 after the call to split_block above which may split
916 the original exit pred. */
917 exit_pred = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
919 FOR_EACH_VEC_ELT (unconverted_simple_returns, i, e)
921 basic_block *pdest_bb;
922 edge pending;
924 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
926 pdest_bb = &simple_return_block_hot;
927 pending = pending_edge_hot;
929 else
931 pdest_bb = &simple_return_block_cold;
932 pending = pending_edge_cold;
935 if (*pdest_bb == NULL && pending != NULL)
937 emit_return_into_block (true, pending->src);
938 pending->flags &= ~(EDGE_FALLTHRU | EDGE_FAKE);
939 *pdest_bb = pending->src;
941 else if (*pdest_bb == NULL)
943 basic_block bb;
944 rtx_insn *start;
946 bb = create_basic_block (NULL, NULL, exit_pred);
947 BB_COPY_PARTITION (bb, e->src);
948 start = emit_jump_insn_after (gen_simple_return (),
949 BB_END (bb));
950 JUMP_LABEL (start) = simple_return_rtx;
951 emit_barrier_after (start);
953 *pdest_bb = bb;
954 make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
956 redirect_edge_and_branch_force (e, *pdest_bb);
958 unconverted_simple_returns.release ();
961 if (entry_edge != orig_entry_edge)
963 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
964 if (EDGE_COUNT (e->src->preds) != 0
965 && (e->flags & EDGE_FAKE) != 0
966 && !bitmap_bit_p (&bb_flags, e->src->index))
968 emit_return_into_block (true, e->src);
969 e->flags &= ~(EDGE_FALLTHRU | EDGE_FAKE);
974 #endif