PR libstdc++/66354
[official-gcc.git] / gcc / shrink-wrap.c
blob6c59da95133c66553d983a612251d75590f4eeb3
1 /* Shrink-wrapping related optimizations.
2 Copyright (C) 1987-2015 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 "hash-set.h"
28 #include "machmode.h"
29 #include "vec.h"
30 #include "double-int.h"
31 #include "input.h"
32 #include "alias.h"
33 #include "symtab.h"
34 #include "wide-int.h"
35 #include "inchash.h"
36 #include "tree.h"
37 #include "fold-const.h"
38 #include "stor-layout.h"
39 #include "varasm.h"
40 #include "stringpool.h"
41 #include "flags.h"
42 #include "except.h"
43 #include "hard-reg-set.h"
44 #include "function.h"
45 #include "hashtab.h"
46 #include "rtl.h"
47 #include "statistics.h"
48 #include "real.h"
49 #include "fixed-value.h"
50 #include "insn-config.h"
51 #include "expmed.h"
52 #include "dojump.h"
53 #include "explow.h"
54 #include "calls.h"
55 #include "emit-rtl.h"
56 #include "stmt.h"
57 #include "expr.h"
58 #include "insn-codes.h"
59 #include "optabs.h"
60 #include "libfuncs.h"
61 #include "regs.h"
62 #include "recog.h"
63 #include "output.h"
64 #include "tm_p.h"
65 #include "langhooks.h"
66 #include "target.h"
67 #include "common/common-target.h"
68 #include "gimple-expr.h"
69 #include "gimplify.h"
70 #include "tree-pass.h"
71 #include "predict.h"
72 #include "dominance.h"
73 #include "cfg.h"
74 #include "cfgrtl.h"
75 #include "basic-block.h"
76 #include "df.h"
77 #include "params.h"
78 #include "bb-reorder.h"
79 #include "shrink-wrap.h"
80 #include "regcprop.h"
81 #include "rtl-iter.h"
84 /* Return true if INSN requires the stack frame to be set up.
85 PROLOGUE_USED contains the hard registers used in the function
86 prologue. SET_UP_BY_PROLOGUE is the set of registers we expect the
87 prologue to set up for the function. */
88 bool
89 requires_stack_frame_p (rtx_insn *insn, HARD_REG_SET prologue_used,
90 HARD_REG_SET set_up_by_prologue)
92 df_ref def, use;
93 HARD_REG_SET hardregs;
94 unsigned regno;
96 if (CALL_P (insn))
97 return !SIBLING_CALL_P (insn);
99 /* We need a frame to get the unique CFA expected by the unwinder. */
100 if (cfun->can_throw_non_call_exceptions && can_throw_internal (insn))
101 return true;
103 CLEAR_HARD_REG_SET (hardregs);
104 FOR_EACH_INSN_DEF (def, insn)
106 rtx dreg = DF_REF_REG (def);
108 if (!REG_P (dreg))
109 continue;
111 add_to_hard_reg_set (&hardregs, GET_MODE (dreg),
112 REGNO (dreg));
114 if (hard_reg_set_intersect_p (hardregs, prologue_used))
115 return true;
116 AND_COMPL_HARD_REG_SET (hardregs, call_used_reg_set);
117 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
118 if (TEST_HARD_REG_BIT (hardregs, regno)
119 && df_regs_ever_live_p (regno))
120 return true;
122 FOR_EACH_INSN_USE (use, insn)
124 rtx reg = DF_REF_REG (use);
126 if (!REG_P (reg))
127 continue;
129 add_to_hard_reg_set (&hardregs, GET_MODE (reg),
130 REGNO (reg));
132 if (hard_reg_set_intersect_p (hardregs, set_up_by_prologue))
133 return true;
135 return false;
138 /* See whether there has a single live edge from BB, which dest uses
139 [REGNO, END_REGNO). Return the live edge if its dest bb has
140 one or two predecessors. Otherwise return NULL. */
142 static edge
143 live_edge_for_reg (basic_block bb, int regno, int end_regno)
145 edge e, live_edge;
146 edge_iterator ei;
147 bitmap live;
148 int i;
150 live_edge = NULL;
151 FOR_EACH_EDGE (e, ei, bb->succs)
153 live = df_get_live_in (e->dest);
154 for (i = regno; i < end_regno; i++)
155 if (REGNO_REG_SET_P (live, i))
157 if (live_edge && live_edge != e)
158 return NULL;
159 live_edge = e;
163 /* We can sometimes encounter dead code. Don't try to move it
164 into the exit block. */
165 if (!live_edge || live_edge->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
166 return NULL;
168 /* Reject targets of abnormal edges. This is needed for correctness
169 on ports like Alpha and MIPS, whose pic_offset_table_rtx can die on
170 exception edges even though it is generally treated as call-saved
171 for the majority of the compilation. Moving across abnormal edges
172 isn't going to be interesting for shrink-wrap usage anyway. */
173 if (live_edge->flags & EDGE_ABNORMAL)
174 return NULL;
176 /* When live_edge->dest->preds == 2, we can create a new block on
177 the edge to make it meet the requirement. */
178 if (EDGE_COUNT (live_edge->dest->preds) > 2)
179 return NULL;
181 return live_edge;
184 /* Try to move INSN from BB to a successor. Return true on success.
185 USES and DEFS are the set of registers that are used and defined
186 after INSN in BB. SPLIT_P indicates whether a live edge from BB
187 is splitted or not. */
189 static bool
190 move_insn_for_shrink_wrap (basic_block bb, rtx_insn *insn,
191 const HARD_REG_SET uses,
192 const HARD_REG_SET defs,
193 bool *split_p)
195 rtx set, src, dest;
196 bitmap live_out, live_in, bb_uses, bb_defs;
197 unsigned int i, dregno, end_dregno;
198 unsigned int sregno = FIRST_PSEUDO_REGISTER;
199 unsigned int end_sregno = FIRST_PSEUDO_REGISTER;
200 basic_block next_block;
201 edge live_edge;
203 /* Look for a simple register assignment. We don't use single_set here
204 because we can't deal with any CLOBBERs, USEs, or REG_UNUSED secondary
205 destinations. */
206 if (!INSN_P (insn))
207 return false;
208 set = PATTERN (insn);
209 if (GET_CODE (set) != SET)
210 return false;
211 src = SET_SRC (set);
212 dest = SET_DEST (set);
214 /* For the destination, we want only a register. Also disallow STACK
215 or FRAME related adjustments. They are likely part of the prologue,
216 so keep them in the entry block. */
217 if (!REG_P (dest)
218 || dest == stack_pointer_rtx
219 || dest == frame_pointer_rtx
220 || dest == hard_frame_pointer_rtx)
221 return false;
223 /* For the source, we want one of:
224 (1) A (non-overlapping) register
225 (2) A constant,
226 (3) An expression involving no more than one register.
228 That last point comes from the code following, which was originally
229 written to handle only register move operations, and still only handles
230 a single source register when checking for overlaps. Happily, the
231 same checks can be applied to expressions like (plus reg const). */
233 if (CONSTANT_P (src))
235 else if (!REG_P (src))
237 rtx src_inner = NULL_RTX;
239 if (can_throw_internal (insn))
240 return false;
242 subrtx_var_iterator::array_type array;
243 FOR_EACH_SUBRTX_VAR (iter, array, src, ALL)
245 rtx x = *iter;
246 switch (GET_RTX_CLASS (GET_CODE (x)))
248 case RTX_CONST_OBJ:
249 case RTX_COMPARE:
250 case RTX_COMM_COMPARE:
251 case RTX_BIN_ARITH:
252 case RTX_COMM_ARITH:
253 case RTX_UNARY:
254 case RTX_TERNARY:
255 /* Constant or expression. Continue. */
256 break;
258 case RTX_OBJ:
259 case RTX_EXTRA:
260 switch (GET_CODE (x))
262 case UNSPEC:
263 case SUBREG:
264 case STRICT_LOW_PART:
265 case PC:
266 case LO_SUM:
267 /* Ok. Continue. */
268 break;
270 case REG:
271 /* Fail if we see a second inner register. */
272 if (src_inner != NULL)
273 return false;
274 src_inner = x;
275 break;
277 default:
278 return false;
280 break;
282 default:
283 return false;
287 if (src_inner != NULL)
288 src = src_inner;
291 /* Make sure that the source register isn't defined later in BB. */
292 if (REG_P (src))
294 sregno = REGNO (src);
295 end_sregno = END_REGNO (src);
296 if (overlaps_hard_reg_set_p (defs, GET_MODE (src), sregno))
297 return false;
300 /* Make sure that the destination register isn't referenced later in BB. */
301 dregno = REGNO (dest);
302 end_dregno = END_REGNO (dest);
303 if (overlaps_hard_reg_set_p (uses, GET_MODE (dest), dregno)
304 || overlaps_hard_reg_set_p (defs, GET_MODE (dest), dregno))
305 return false;
307 /* See whether there is a successor block to which we could move INSN. */
308 live_edge = live_edge_for_reg (bb, dregno, end_dregno);
309 if (!live_edge)
310 return false;
312 next_block = live_edge->dest;
313 /* Create a new basic block on the edge. */
314 if (EDGE_COUNT (next_block->preds) == 2)
316 /* split_edge for a block with only one successor is meaningless. */
317 if (EDGE_COUNT (bb->succs) == 1)
318 return false;
320 /* If DF_LIVE doesn't exist, i.e. at -O1, just give up. */
321 if (!df_live)
322 return false;
324 basic_block old_dest = live_edge->dest;
325 next_block = split_edge (live_edge);
327 /* We create a new basic block. Call df_grow_bb_info to make sure
328 all data structures are allocated. */
329 df_grow_bb_info (df_live);
331 bitmap_and (df_get_live_in (next_block), df_get_live_out (bb),
332 df_get_live_in (old_dest));
333 df_set_bb_dirty (next_block);
335 /* We should not split more than once for a function. */
336 if (*split_p)
337 return false;
339 *split_p = true;
342 /* At this point we are committed to moving INSN, but let's try to
343 move it as far as we can. */
346 live_out = df_get_live_out (bb);
347 live_in = df_get_live_in (next_block);
348 bb = next_block;
350 /* Check whether BB uses DEST or clobbers DEST. We need to add
351 INSN to BB if so. Either way, DEST is no longer live on entry,
352 except for any part that overlaps SRC (next loop). */
353 bb_uses = &DF_LR_BB_INFO (bb)->use;
354 bb_defs = &DF_LR_BB_INFO (bb)->def;
355 if (df_live)
357 for (i = dregno; i < end_dregno; i++)
359 if (*split_p
360 || REGNO_REG_SET_P (bb_uses, i)
361 || REGNO_REG_SET_P (bb_defs, i)
362 || REGNO_REG_SET_P (&DF_LIVE_BB_INFO (bb)->gen, i))
363 next_block = NULL;
364 CLEAR_REGNO_REG_SET (live_out, i);
365 CLEAR_REGNO_REG_SET (live_in, i);
368 /* Check whether BB clobbers SRC. We need to add INSN to BB if so.
369 Either way, SRC is now live on entry. */
370 for (i = sregno; i < end_sregno; i++)
372 if (*split_p
373 || REGNO_REG_SET_P (bb_defs, i)
374 || REGNO_REG_SET_P (&DF_LIVE_BB_INFO (bb)->gen, i))
375 next_block = NULL;
376 SET_REGNO_REG_SET (live_out, i);
377 SET_REGNO_REG_SET (live_in, i);
380 else
382 /* DF_LR_BB_INFO (bb)->def does not comprise the DF_REF_PARTIAL and
383 DF_REF_CONDITIONAL defs. So if DF_LIVE doesn't exist, i.e.
384 at -O1, just give up searching NEXT_BLOCK. */
385 next_block = NULL;
386 for (i = dregno; i < end_dregno; i++)
388 CLEAR_REGNO_REG_SET (live_out, i);
389 CLEAR_REGNO_REG_SET (live_in, i);
392 for (i = sregno; i < end_sregno; i++)
394 SET_REGNO_REG_SET (live_out, i);
395 SET_REGNO_REG_SET (live_in, i);
399 /* If we don't need to add the move to BB, look for a single
400 successor block. */
401 if (next_block)
403 live_edge = live_edge_for_reg (next_block, dregno, end_dregno);
404 if (!live_edge || EDGE_COUNT (live_edge->dest->preds) > 1)
405 break;
406 next_block = live_edge->dest;
409 while (next_block);
411 /* For the new created basic block, there is no dataflow info at all.
412 So skip the following dataflow update and check. */
413 if (!(*split_p))
415 /* BB now defines DEST. It only uses the parts of DEST that overlap SRC
416 (next loop). */
417 for (i = dregno; i < end_dregno; i++)
419 CLEAR_REGNO_REG_SET (bb_uses, i);
420 SET_REGNO_REG_SET (bb_defs, i);
423 /* BB now uses SRC. */
424 for (i = sregno; i < end_sregno; i++)
425 SET_REGNO_REG_SET (bb_uses, i);
428 emit_insn_after (PATTERN (insn), bb_note (bb));
429 delete_insn (insn);
430 return true;
433 /* Look for register copies in the first block of the function, and move
434 them down into successor blocks if the register is used only on one
435 path. This exposes more opportunities for shrink-wrapping. These
436 kinds of sets often occur when incoming argument registers are moved
437 to call-saved registers because their values are live across one or
438 more calls during the function. */
440 void
441 prepare_shrink_wrap (basic_block entry_block)
443 rtx_insn *insn, *curr;
444 rtx x;
445 HARD_REG_SET uses, defs;
446 df_ref def, use;
447 bool split_p = false;
449 if (JUMP_P (BB_END (entry_block)))
451 /* To have more shrink-wrapping opportunities, prepare_shrink_wrap tries
452 to sink the copies from parameter to callee saved register out of
453 entry block. copyprop_hardreg_forward_bb_without_debug_insn is called
454 to release some dependences. */
455 copyprop_hardreg_forward_bb_without_debug_insn (entry_block);
458 CLEAR_HARD_REG_SET (uses);
459 CLEAR_HARD_REG_SET (defs);
460 FOR_BB_INSNS_REVERSE_SAFE (entry_block, insn, curr)
461 if (NONDEBUG_INSN_P (insn)
462 && !move_insn_for_shrink_wrap (entry_block, insn, uses, defs,
463 &split_p))
465 /* Add all defined registers to DEFs. */
466 FOR_EACH_INSN_DEF (def, insn)
468 x = DF_REF_REG (def);
469 if (REG_P (x) && HARD_REGISTER_P (x))
470 SET_HARD_REG_BIT (defs, REGNO (x));
473 /* Add all used registers to USESs. */
474 FOR_EACH_INSN_USE (use, insn)
476 x = DF_REF_REG (use);
477 if (REG_P (x) && HARD_REGISTER_P (x))
478 SET_HARD_REG_BIT (uses, REGNO (x));
483 /* Create a copy of BB instructions and insert at BEFORE. Redirect
484 preds of BB to COPY_BB if they don't appear in NEED_PROLOGUE. */
485 void
486 dup_block_and_redirect (basic_block bb, basic_block copy_bb, rtx_insn *before,
487 bitmap_head *need_prologue)
489 edge_iterator ei;
490 edge e;
491 rtx_insn *insn = BB_END (bb);
493 /* We know BB has a single successor, so there is no need to copy a
494 simple jump at the end of BB. */
495 if (simplejump_p (insn))
496 insn = PREV_INSN (insn);
498 start_sequence ();
499 duplicate_insn_chain (BB_HEAD (bb), insn);
500 if (dump_file)
502 unsigned count = 0;
503 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
504 if (active_insn_p (insn))
505 ++count;
506 fprintf (dump_file, "Duplicating bb %d to bb %d, %u active insns.\n",
507 bb->index, copy_bb->index, count);
509 insn = get_insns ();
510 end_sequence ();
511 emit_insn_before (insn, before);
513 /* Redirect all the paths that need no prologue into copy_bb. */
514 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei));)
515 if (!bitmap_bit_p (need_prologue, e->src->index))
517 int freq = EDGE_FREQUENCY (e);
518 copy_bb->count += e->count;
519 copy_bb->frequency += EDGE_FREQUENCY (e);
520 e->dest->count -= e->count;
521 if (e->dest->count < 0)
522 e->dest->count = 0;
523 e->dest->frequency -= freq;
524 if (e->dest->frequency < 0)
525 e->dest->frequency = 0;
526 redirect_edge_and_branch_force (e, copy_bb);
527 continue;
529 else
530 ei_next (&ei);
534 /* Try to perform a kind of shrink-wrapping, making sure the
535 prologue/epilogue is emitted only around those parts of the
536 function that require it. */
538 void
539 try_shrink_wrapping (edge *entry_edge, edge orig_entry_edge,
540 bitmap_head *bb_flags, rtx_insn *prologue_seq)
542 edge e;
543 edge_iterator ei;
544 bool nonempty_prologue = false;
545 unsigned max_grow_size;
546 rtx_insn *seq;
548 for (seq = prologue_seq; seq; seq = NEXT_INSN (seq))
549 if (!NOTE_P (seq) || NOTE_KIND (seq) != NOTE_INSN_PROLOGUE_END)
551 nonempty_prologue = true;
552 break;
555 if (flag_shrink_wrap && HAVE_simple_return
556 && (targetm.profile_before_prologue () || !crtl->profile)
557 && nonempty_prologue && !crtl->calls_eh_return)
559 HARD_REG_SET prologue_clobbered, prologue_used, live_on_edge;
560 struct hard_reg_set_container set_up_by_prologue;
561 rtx_insn *p_insn;
562 vec<basic_block> vec;
563 basic_block bb;
564 bitmap_head bb_antic_flags;
565 bitmap_head bb_on_list;
566 bitmap_head bb_tail;
568 if (dump_file)
569 fprintf (dump_file, "Attempting shrink-wrapping optimization.\n");
571 /* Compute the registers set and used in the prologue. */
572 CLEAR_HARD_REG_SET (prologue_clobbered);
573 CLEAR_HARD_REG_SET (prologue_used);
574 for (p_insn = prologue_seq; p_insn; p_insn = NEXT_INSN (p_insn))
576 HARD_REG_SET this_used;
577 if (!NONDEBUG_INSN_P (p_insn))
578 continue;
580 CLEAR_HARD_REG_SET (this_used);
581 note_uses (&PATTERN (p_insn), record_hard_reg_uses,
582 &this_used);
583 AND_COMPL_HARD_REG_SET (this_used, prologue_clobbered);
584 IOR_HARD_REG_SET (prologue_used, this_used);
585 note_stores (PATTERN (p_insn), record_hard_reg_sets,
586 &prologue_clobbered);
589 prepare_shrink_wrap ((*entry_edge)->dest);
591 bitmap_initialize (&bb_antic_flags, &bitmap_default_obstack);
592 bitmap_initialize (&bb_on_list, &bitmap_default_obstack);
593 bitmap_initialize (&bb_tail, &bitmap_default_obstack);
595 /* Find the set of basic blocks that require a stack frame,
596 and blocks that are too big to be duplicated. */
598 vec.create (n_basic_blocks_for_fn (cfun));
600 CLEAR_HARD_REG_SET (set_up_by_prologue.set);
601 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
602 STACK_POINTER_REGNUM);
603 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode, ARG_POINTER_REGNUM);
604 if (frame_pointer_needed)
605 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
606 HARD_FRAME_POINTER_REGNUM);
607 if (pic_offset_table_rtx
608 && (unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
609 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
610 PIC_OFFSET_TABLE_REGNUM);
611 if (crtl->drap_reg)
612 add_to_hard_reg_set (&set_up_by_prologue.set,
613 GET_MODE (crtl->drap_reg),
614 REGNO (crtl->drap_reg));
615 if (targetm.set_up_by_prologue)
616 targetm.set_up_by_prologue (&set_up_by_prologue);
618 /* We don't use a different max size depending on
619 optimize_bb_for_speed_p because increasing shrink-wrapping
620 opportunities by duplicating tail blocks can actually result
621 in an overall decrease in code size. */
622 max_grow_size = get_uncond_jump_length ();
623 max_grow_size *= PARAM_VALUE (PARAM_MAX_GROW_COPY_BB_INSNS);
625 FOR_EACH_BB_FN (bb, cfun)
627 rtx_insn *insn;
628 unsigned size = 0;
630 FOR_BB_INSNS (bb, insn)
631 if (NONDEBUG_INSN_P (insn))
633 if (requires_stack_frame_p (insn, prologue_used,
634 set_up_by_prologue.set))
636 if (bb == (*entry_edge)->dest)
637 goto fail_shrinkwrap;
638 bitmap_set_bit (bb_flags, bb->index);
639 vec.quick_push (bb);
640 break;
642 else if (size <= max_grow_size)
644 size += get_attr_min_length (insn);
645 if (size > max_grow_size)
646 bitmap_set_bit (&bb_on_list, bb->index);
651 /* Blocks that really need a prologue, or are too big for tails. */
652 bitmap_ior_into (&bb_on_list, bb_flags);
654 /* For every basic block that needs a prologue, mark all blocks
655 reachable from it, so as to ensure they are also seen as
656 requiring a prologue. */
657 while (!vec.is_empty ())
659 basic_block tmp_bb = vec.pop ();
661 FOR_EACH_EDGE (e, ei, tmp_bb->succs)
662 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
663 && bitmap_set_bit (bb_flags, e->dest->index))
664 vec.quick_push (e->dest);
667 /* Find the set of basic blocks that need no prologue, have a
668 single successor, can be duplicated, meet a max size
669 requirement, and go to the exit via like blocks. */
670 vec.quick_push (EXIT_BLOCK_PTR_FOR_FN (cfun));
671 while (!vec.is_empty ())
673 basic_block tmp_bb = vec.pop ();
675 FOR_EACH_EDGE (e, ei, tmp_bb->preds)
676 if (single_succ_p (e->src)
677 && !bitmap_bit_p (&bb_on_list, e->src->index)
678 && can_duplicate_block_p (e->src))
680 edge pe;
681 edge_iterator pei;
683 /* If there is predecessor of e->src which doesn't
684 need prologue and the edge is complex,
685 we might not be able to redirect the branch
686 to a copy of e->src. */
687 FOR_EACH_EDGE (pe, pei, e->src->preds)
688 if ((pe->flags & EDGE_COMPLEX) != 0
689 && !bitmap_bit_p (bb_flags, pe->src->index))
690 break;
691 if (pe == NULL && bitmap_set_bit (&bb_tail, e->src->index))
692 vec.quick_push (e->src);
696 /* Now walk backwards from every block that is marked as needing
697 a prologue to compute the bb_antic_flags bitmap. Exclude
698 tail blocks; They can be duplicated to be used on paths not
699 needing a prologue. */
700 bitmap_clear (&bb_on_list);
701 bitmap_and_compl (&bb_antic_flags, bb_flags, &bb_tail);
702 FOR_EACH_BB_FN (bb, cfun)
704 if (!bitmap_bit_p (&bb_antic_flags, bb->index))
705 continue;
706 FOR_EACH_EDGE (e, ei, bb->preds)
707 if (!bitmap_bit_p (&bb_antic_flags, e->src->index)
708 && bitmap_set_bit (&bb_on_list, e->src->index))
709 vec.quick_push (e->src);
711 while (!vec.is_empty ())
713 basic_block tmp_bb = vec.pop ();
714 bool all_set = true;
716 bitmap_clear_bit (&bb_on_list, tmp_bb->index);
717 FOR_EACH_EDGE (e, ei, tmp_bb->succs)
718 if (!bitmap_bit_p (&bb_antic_flags, e->dest->index))
720 all_set = false;
721 break;
724 if (all_set)
726 bitmap_set_bit (&bb_antic_flags, tmp_bb->index);
727 FOR_EACH_EDGE (e, ei, tmp_bb->preds)
728 if (!bitmap_bit_p (&bb_antic_flags, e->src->index)
729 && bitmap_set_bit (&bb_on_list, e->src->index))
730 vec.quick_push (e->src);
733 /* Find exactly one edge that leads to a block in ANTIC from
734 a block that isn't. */
735 if (!bitmap_bit_p (&bb_antic_flags, (*entry_edge)->dest->index))
736 FOR_EACH_BB_FN (bb, cfun)
738 if (!bitmap_bit_p (&bb_antic_flags, bb->index))
739 continue;
740 FOR_EACH_EDGE (e, ei, bb->preds)
741 if (!bitmap_bit_p (&bb_antic_flags, e->src->index))
743 if (*entry_edge != orig_entry_edge)
745 *entry_edge = orig_entry_edge;
746 if (dump_file)
747 fprintf (dump_file, "More than one candidate edge.\n");
748 goto fail_shrinkwrap;
750 if (dump_file)
751 fprintf (dump_file, "Found candidate edge for "
752 "shrink-wrapping, %d->%d.\n", e->src->index,
753 e->dest->index);
754 *entry_edge = e;
758 if (*entry_edge != orig_entry_edge)
760 /* Test whether the prologue is known to clobber any register
761 (other than FP or SP) which are live on the edge. */
762 CLEAR_HARD_REG_BIT (prologue_clobbered, STACK_POINTER_REGNUM);
763 if (frame_pointer_needed)
764 CLEAR_HARD_REG_BIT (prologue_clobbered, HARD_FRAME_POINTER_REGNUM);
765 REG_SET_TO_HARD_REG_SET (live_on_edge,
766 df_get_live_in ((*entry_edge)->dest));
767 if (hard_reg_set_intersect_p (live_on_edge, prologue_clobbered))
769 *entry_edge = orig_entry_edge;
770 if (dump_file)
771 fprintf (dump_file,
772 "Shrink-wrapping aborted due to clobber.\n");
775 if (*entry_edge != orig_entry_edge)
777 crtl->shrink_wrapped = true;
778 if (dump_file)
779 fprintf (dump_file, "Performing shrink-wrapping.\n");
781 /* Find tail blocks reachable from both blocks needing a
782 prologue and blocks not needing a prologue. */
783 if (!bitmap_empty_p (&bb_tail))
784 FOR_EACH_BB_FN (bb, cfun)
786 bool some_pro, some_no_pro;
787 if (!bitmap_bit_p (&bb_tail, bb->index))
788 continue;
789 some_pro = some_no_pro = false;
790 FOR_EACH_EDGE (e, ei, bb->preds)
792 if (bitmap_bit_p (bb_flags, e->src->index))
793 some_pro = true;
794 else
795 some_no_pro = true;
797 if (some_pro && some_no_pro)
798 vec.quick_push (bb);
799 else
800 bitmap_clear_bit (&bb_tail, bb->index);
802 /* Find the head of each tail. */
803 while (!vec.is_empty ())
805 basic_block tbb = vec.pop ();
807 if (!bitmap_bit_p (&bb_tail, tbb->index))
808 continue;
810 while (single_succ_p (tbb))
812 tbb = single_succ (tbb);
813 bitmap_clear_bit (&bb_tail, tbb->index);
816 /* Now duplicate the tails. */
817 if (!bitmap_empty_p (&bb_tail))
818 FOR_EACH_BB_REVERSE_FN (bb, cfun)
820 basic_block copy_bb, tbb;
821 rtx_insn *insert_point;
822 int eflags;
824 if (!bitmap_clear_bit (&bb_tail, bb->index))
825 continue;
827 /* Create a copy of BB, instructions and all, for
828 use on paths that don't need a prologue.
829 Ideal placement of the copy is on a fall-thru edge
830 or after a block that would jump to the copy. */
831 FOR_EACH_EDGE (e, ei, bb->preds)
832 if (!bitmap_bit_p (bb_flags, e->src->index)
833 && single_succ_p (e->src))
834 break;
835 if (e)
837 /* Make sure we insert after any barriers. */
838 rtx_insn *end = get_last_bb_insn (e->src);
839 copy_bb = create_basic_block (NEXT_INSN (end),
840 NULL_RTX, e->src);
841 BB_COPY_PARTITION (copy_bb, e->src);
843 else
845 /* Otherwise put the copy at the end of the function. */
846 copy_bb = create_basic_block (NULL_RTX, NULL_RTX,
847 EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb);
848 BB_COPY_PARTITION (copy_bb, bb);
851 insert_point = emit_note_after (NOTE_INSN_DELETED,
852 BB_END (copy_bb));
853 emit_barrier_after (BB_END (copy_bb));
855 tbb = bb;
856 while (1)
858 dup_block_and_redirect (tbb, copy_bb, insert_point,
859 bb_flags);
860 tbb = single_succ (tbb);
861 if (tbb == EXIT_BLOCK_PTR_FOR_FN (cfun))
862 break;
863 e = split_block (copy_bb, PREV_INSN (insert_point));
864 copy_bb = e->dest;
867 /* Quiet verify_flow_info by (ab)using EDGE_FAKE.
868 We have yet to add a simple_return to the tails,
869 as we'd like to first convert_jumps_to_returns in
870 case the block is no longer used after that. */
871 eflags = EDGE_FAKE;
872 if (CALL_P (PREV_INSN (insert_point))
873 && SIBLING_CALL_P (PREV_INSN (insert_point)))
874 eflags = EDGE_SIBCALL | EDGE_ABNORMAL;
875 make_single_succ_edge (copy_bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
876 eflags);
878 /* verify_flow_info doesn't like a note after a
879 sibling call. */
880 delete_insn (insert_point);
881 if (bitmap_empty_p (&bb_tail))
882 break;
886 fail_shrinkwrap:
887 bitmap_clear (&bb_tail);
888 bitmap_clear (&bb_antic_flags);
889 bitmap_clear (&bb_on_list);
890 vec.release ();
894 /* If we're allowed to generate a simple return instruction, then by
895 definition we don't need a full epilogue. If the last basic
896 block before the exit block does not contain active instructions,
897 examine its predecessors and try to emit (conditional) return
898 instructions. */
900 edge
901 get_unconverted_simple_return (edge exit_fallthru_edge, bitmap_head bb_flags,
902 vec<edge> *unconverted_simple_returns,
903 rtx_insn **returnjump)
905 if (optimize)
907 unsigned i, last;
909 /* convert_jumps_to_returns may add to preds of the exit block
910 (but won't remove). Stop at end of current preds. */
911 last = EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
912 for (i = 0; i < last; i++)
914 edge e = EDGE_I (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds, i);
915 if (LABEL_P (BB_HEAD (e->src))
916 && !bitmap_bit_p (&bb_flags, e->src->index)
917 && !active_insn_between (BB_HEAD (e->src), BB_END (e->src)))
918 *unconverted_simple_returns
919 = convert_jumps_to_returns (e->src, true,
920 *unconverted_simple_returns);
924 if (exit_fallthru_edge != NULL
925 && EDGE_COUNT (exit_fallthru_edge->src->preds) != 0
926 && !bitmap_bit_p (&bb_flags, exit_fallthru_edge->src->index))
928 basic_block last_bb;
930 last_bb = emit_return_for_exit (exit_fallthru_edge, true);
931 *returnjump = BB_END (last_bb);
932 exit_fallthru_edge = NULL;
934 return exit_fallthru_edge;
937 /* If there were branches to an empty LAST_BB which we tried to
938 convert to conditional simple_returns, but couldn't for some
939 reason, create a block to hold a simple_return insn and redirect
940 those remaining edges. */
942 void
943 convert_to_simple_return (edge entry_edge, edge orig_entry_edge,
944 bitmap_head bb_flags, rtx_insn *returnjump,
945 vec<edge> unconverted_simple_returns)
947 edge e;
948 edge_iterator ei;
950 if (!unconverted_simple_returns.is_empty ())
952 basic_block simple_return_block_hot = NULL;
953 basic_block simple_return_block_cold = NULL;
954 edge pending_edge_hot = NULL;
955 edge pending_edge_cold = NULL;
956 basic_block exit_pred;
957 int i;
959 gcc_assert (entry_edge != orig_entry_edge);
961 /* See if we can reuse the last insn that was emitted for the
962 epilogue. */
963 if (returnjump != NULL_RTX
964 && JUMP_LABEL (returnjump) == simple_return_rtx)
966 e = split_block (BLOCK_FOR_INSN (returnjump), PREV_INSN (returnjump));
967 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
968 simple_return_block_hot = e->dest;
969 else
970 simple_return_block_cold = e->dest;
973 /* Also check returns we might need to add to tail blocks. */
974 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
975 if (EDGE_COUNT (e->src->preds) != 0
976 && (e->flags & EDGE_FAKE) != 0
977 && !bitmap_bit_p (&bb_flags, e->src->index))
979 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
980 pending_edge_hot = e;
981 else
982 pending_edge_cold = e;
985 /* Save a pointer to the exit's predecessor BB for use in
986 inserting new BBs at the end of the function. Do this
987 after the call to split_block above which may split
988 the original exit pred. */
989 exit_pred = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
991 FOR_EACH_VEC_ELT (unconverted_simple_returns, i, e)
993 basic_block *pdest_bb;
994 edge pending;
996 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
998 pdest_bb = &simple_return_block_hot;
999 pending = pending_edge_hot;
1001 else
1003 pdest_bb = &simple_return_block_cold;
1004 pending = pending_edge_cold;
1007 if (*pdest_bb == NULL && pending != NULL)
1009 emit_return_into_block (true, pending->src);
1010 pending->flags &= ~(EDGE_FALLTHRU | EDGE_FAKE);
1011 *pdest_bb = pending->src;
1013 else if (*pdest_bb == NULL)
1015 basic_block bb;
1016 rtx_insn *start;
1018 bb = create_basic_block (NULL, NULL, exit_pred);
1019 BB_COPY_PARTITION (bb, e->src);
1020 start = emit_jump_insn_after (gen_simple_return (),
1021 BB_END (bb));
1022 JUMP_LABEL (start) = simple_return_rtx;
1023 emit_barrier_after (start);
1025 *pdest_bb = bb;
1026 make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1028 redirect_edge_and_branch_force (e, *pdest_bb);
1030 unconverted_simple_returns.release ();
1033 if (entry_edge != orig_entry_edge)
1035 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
1036 if (EDGE_COUNT (e->src->preds) != 0
1037 && (e->flags & EDGE_FAKE) != 0
1038 && !bitmap_bit_p (&bb_flags, e->src->index))
1040 emit_return_into_block (true, e->src);
1041 e->flags &= ~(EDGE_FALLTHRU | EDGE_FAKE);