libgomp: Use pthread mutexes in the nvptx plugin.
[official-gcc.git] / gcc / shrink-wrap.c
blob0c7a64c2e4f2f67c4c10ae26a5574fc62f076351
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 "input.h"
45 #include "function.h"
46 #include "expr.h"
47 #include "insn-codes.h"
48 #include "optabs.h"
49 #include "libfuncs.h"
50 #include "regs.h"
51 #include "insn-config.h"
52 #include "recog.h"
53 #include "output.h"
54 #include "tm_p.h"
55 #include "langhooks.h"
56 #include "target.h"
57 #include "common/common-target.h"
58 #include "gimple-expr.h"
59 #include "gimplify.h"
60 #include "tree-pass.h"
61 #include "predict.h"
62 #include "dominance.h"
63 #include "cfg.h"
64 #include "cfgrtl.h"
65 #include "basic-block.h"
66 #include "df.h"
67 #include "params.h"
68 #include "bb-reorder.h"
69 #include "shrink-wrap.h"
70 #include "regcprop.h"
71 #include "rtl-iter.h"
73 #ifdef HAVE_simple_return
75 /* Return true if INSN requires the stack frame to be set up.
76 PROLOGUE_USED contains the hard registers used in the function
77 prologue. SET_UP_BY_PROLOGUE is the set of registers we expect the
78 prologue to set up for the function. */
79 bool
80 requires_stack_frame_p (rtx_insn *insn, HARD_REG_SET prologue_used,
81 HARD_REG_SET set_up_by_prologue)
83 df_ref def, use;
84 HARD_REG_SET hardregs;
85 unsigned regno;
87 if (CALL_P (insn))
88 return !SIBLING_CALL_P (insn);
90 /* We need a frame to get the unique CFA expected by the unwinder. */
91 if (cfun->can_throw_non_call_exceptions && can_throw_internal (insn))
92 return true;
94 CLEAR_HARD_REG_SET (hardregs);
95 FOR_EACH_INSN_DEF (def, insn)
97 rtx dreg = DF_REF_REG (def);
99 if (!REG_P (dreg))
100 continue;
102 add_to_hard_reg_set (&hardregs, GET_MODE (dreg),
103 REGNO (dreg));
105 if (hard_reg_set_intersect_p (hardregs, prologue_used))
106 return true;
107 AND_COMPL_HARD_REG_SET (hardregs, call_used_reg_set);
108 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
109 if (TEST_HARD_REG_BIT (hardregs, regno)
110 && df_regs_ever_live_p (regno))
111 return true;
113 FOR_EACH_INSN_USE (use, insn)
115 rtx reg = DF_REF_REG (use);
117 if (!REG_P (reg))
118 continue;
120 add_to_hard_reg_set (&hardregs, GET_MODE (reg),
121 REGNO (reg));
123 if (hard_reg_set_intersect_p (hardregs, set_up_by_prologue))
124 return true;
126 return false;
129 /* See whether there has a single live edge from BB, which dest uses
130 [REGNO, END_REGNO). Return the live edge if its dest bb has
131 one or two predecessors. Otherwise return NULL. */
133 static edge
134 live_edge_for_reg (basic_block bb, int regno, int end_regno)
136 edge e, live_edge;
137 edge_iterator ei;
138 bitmap live;
139 int i;
141 live_edge = NULL;
142 FOR_EACH_EDGE (e, ei, bb->succs)
144 live = df_get_live_in (e->dest);
145 for (i = regno; i < end_regno; i++)
146 if (REGNO_REG_SET_P (live, i))
148 if (live_edge && live_edge != e)
149 return NULL;
150 live_edge = e;
154 /* We can sometimes encounter dead code. Don't try to move it
155 into the exit block. */
156 if (!live_edge || live_edge->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
157 return NULL;
159 /* Reject targets of abnormal edges. This is needed for correctness
160 on ports like Alpha and MIPS, whose pic_offset_table_rtx can die on
161 exception edges even though it is generally treated as call-saved
162 for the majority of the compilation. Moving across abnormal edges
163 isn't going to be interesting for shrink-wrap usage anyway. */
164 if (live_edge->flags & EDGE_ABNORMAL)
165 return NULL;
167 /* When live_edge->dest->preds == 2, we can create a new block on
168 the edge to make it meet the requirement. */
169 if (EDGE_COUNT (live_edge->dest->preds) > 2)
170 return NULL;
172 return live_edge;
175 /* Try to move INSN from BB to a successor. Return true on success.
176 USES and DEFS are the set of registers that are used and defined
177 after INSN in BB. SPLIT_P indicates whether a live edge from BB
178 is splitted or not. */
180 static bool
181 move_insn_for_shrink_wrap (basic_block bb, rtx_insn *insn,
182 const HARD_REG_SET uses,
183 const HARD_REG_SET defs,
184 bool *split_p)
186 rtx set, src, dest;
187 bitmap live_out, live_in, bb_uses, bb_defs;
188 unsigned int i, dregno, end_dregno;
189 unsigned int sregno = FIRST_PSEUDO_REGISTER;
190 unsigned int end_sregno = FIRST_PSEUDO_REGISTER;
191 basic_block next_block;
192 edge live_edge;
194 /* Look for a simple register assignment. We don't use single_set here
195 because we can't deal with any CLOBBERs, USEs, or REG_UNUSED secondary
196 destinations. */
197 if (!INSN_P (insn))
198 return false;
199 set = PATTERN (insn);
200 if (GET_CODE (set) != SET)
201 return false;
202 src = SET_SRC (set);
203 dest = SET_DEST (set);
205 /* For the destination, we want only a register. Also disallow STACK
206 or FRAME related adjustments. They are likely part of the prologue,
207 so keep them in the entry block. */
208 if (!REG_P (dest)
209 || dest == stack_pointer_rtx
210 || dest == frame_pointer_rtx
211 || dest == hard_frame_pointer_rtx)
212 return false;
214 /* For the source, we want one of:
215 (1) A (non-overlapping) register
216 (2) A constant,
217 (3) An expression involving no more than one register.
219 That last point comes from the code following, which was originally
220 written to handle only register move operations, and still only handles
221 a single source register when checking for overlaps. Happily, the
222 same checks can be applied to expressions like (plus reg const). */
224 if (CONSTANT_P (src))
226 else if (!REG_P (src))
228 rtx src_inner = NULL_RTX;
230 if (can_throw_internal (insn))
231 return false;
233 subrtx_var_iterator::array_type array;
234 FOR_EACH_SUBRTX_VAR (iter, array, src, ALL)
236 rtx x = *iter;
237 switch (GET_RTX_CLASS (GET_CODE (x)))
239 case RTX_CONST_OBJ:
240 case RTX_COMPARE:
241 case RTX_COMM_COMPARE:
242 case RTX_BIN_ARITH:
243 case RTX_COMM_ARITH:
244 case RTX_UNARY:
245 case RTX_TERNARY:
246 /* Constant or expression. Continue. */
247 break;
249 case RTX_OBJ:
250 case RTX_EXTRA:
251 switch (GET_CODE (x))
253 case UNSPEC:
254 case SUBREG:
255 case STRICT_LOW_PART:
256 case PC:
257 case LO_SUM:
258 /* Ok. Continue. */
259 break;
261 case REG:
262 /* Fail if we see a second inner register. */
263 if (src_inner != NULL)
264 return false;
265 src_inner = x;
266 break;
268 default:
269 return false;
271 break;
273 default:
274 return false;
278 if (src_inner != NULL)
279 src = src_inner;
282 /* Make sure that the source register isn't defined later in BB. */
283 if (REG_P (src))
285 sregno = REGNO (src);
286 end_sregno = END_REGNO (src);
287 if (overlaps_hard_reg_set_p (defs, GET_MODE (src), sregno))
288 return false;
291 /* Make sure that the destination register isn't referenced later in BB. */
292 dregno = REGNO (dest);
293 end_dregno = END_REGNO (dest);
294 if (overlaps_hard_reg_set_p (uses, GET_MODE (dest), dregno)
295 || overlaps_hard_reg_set_p (defs, GET_MODE (dest), dregno))
296 return false;
298 /* See whether there is a successor block to which we could move INSN. */
299 live_edge = live_edge_for_reg (bb, dregno, end_dregno);
300 if (!live_edge)
301 return false;
303 next_block = live_edge->dest;
304 /* Create a new basic block on the edge. */
305 if (EDGE_COUNT (next_block->preds) == 2)
307 /* split_edge for a block with only one successor is meaningless. */
308 if (EDGE_COUNT (bb->succs) == 1)
309 return false;
311 /* If DF_LIVE doesn't exist, i.e. at -O1, just give up. */
312 if (!df_live)
313 return false;
315 basic_block old_dest = live_edge->dest;
316 next_block = split_edge (live_edge);
318 /* We create a new basic block. Call df_grow_bb_info to make sure
319 all data structures are allocated. */
320 df_grow_bb_info (df_live);
322 bitmap_and (df_get_live_in (next_block), df_get_live_out (bb),
323 df_get_live_in (old_dest));
324 df_set_bb_dirty (next_block);
326 /* We should not split more than once for a function. */
327 if (*split_p)
328 return false;
330 *split_p = true;
333 /* At this point we are committed to moving INSN, but let's try to
334 move it as far as we can. */
337 live_out = df_get_live_out (bb);
338 live_in = df_get_live_in (next_block);
339 bb = next_block;
341 /* Check whether BB uses DEST or clobbers DEST. We need to add
342 INSN to BB if so. Either way, DEST is no longer live on entry,
343 except for any part that overlaps SRC (next loop). */
344 bb_uses = &DF_LR_BB_INFO (bb)->use;
345 bb_defs = &DF_LR_BB_INFO (bb)->def;
346 if (df_live)
348 for (i = dregno; i < end_dregno; i++)
350 if (*split_p
351 || REGNO_REG_SET_P (bb_uses, i)
352 || REGNO_REG_SET_P (bb_defs, i)
353 || REGNO_REG_SET_P (&DF_LIVE_BB_INFO (bb)->gen, i))
354 next_block = NULL;
355 CLEAR_REGNO_REG_SET (live_out, i);
356 CLEAR_REGNO_REG_SET (live_in, i);
359 /* Check whether BB clobbers SRC. We need to add INSN to BB if so.
360 Either way, SRC is now live on entry. */
361 for (i = sregno; i < end_sregno; i++)
363 if (*split_p
364 || REGNO_REG_SET_P (bb_defs, i)
365 || REGNO_REG_SET_P (&DF_LIVE_BB_INFO (bb)->gen, i))
366 next_block = NULL;
367 SET_REGNO_REG_SET (live_out, i);
368 SET_REGNO_REG_SET (live_in, i);
371 else
373 /* DF_LR_BB_INFO (bb)->def does not comprise the DF_REF_PARTIAL and
374 DF_REF_CONDITIONAL defs. So if DF_LIVE doesn't exist, i.e.
375 at -O1, just give up searching NEXT_BLOCK. */
376 next_block = NULL;
377 for (i = dregno; i < end_dregno; i++)
379 CLEAR_REGNO_REG_SET (live_out, i);
380 CLEAR_REGNO_REG_SET (live_in, i);
383 for (i = sregno; i < end_sregno; i++)
385 SET_REGNO_REG_SET (live_out, i);
386 SET_REGNO_REG_SET (live_in, i);
390 /* If we don't need to add the move to BB, look for a single
391 successor block. */
392 if (next_block)
394 live_edge = live_edge_for_reg (next_block, dregno, end_dregno);
395 if (!live_edge || EDGE_COUNT (live_edge->dest->preds) > 1)
396 break;
397 next_block = live_edge->dest;
400 while (next_block);
402 /* For the new created basic block, there is no dataflow info at all.
403 So skip the following dataflow update and check. */
404 if (!(*split_p))
406 /* BB now defines DEST. It only uses the parts of DEST that overlap SRC
407 (next loop). */
408 for (i = dregno; i < end_dregno; i++)
410 CLEAR_REGNO_REG_SET (bb_uses, i);
411 SET_REGNO_REG_SET (bb_defs, i);
414 /* BB now uses SRC. */
415 for (i = sregno; i < end_sregno; i++)
416 SET_REGNO_REG_SET (bb_uses, i);
419 emit_insn_after (PATTERN (insn), bb_note (bb));
420 delete_insn (insn);
421 return true;
424 /* Look for register copies in the first block of the function, and move
425 them down into successor blocks if the register is used only on one
426 path. This exposes more opportunities for shrink-wrapping. These
427 kinds of sets often occur when incoming argument registers are moved
428 to call-saved registers because their values are live across one or
429 more calls during the function. */
431 void
432 prepare_shrink_wrap (basic_block entry_block)
434 rtx_insn *insn, *curr;
435 rtx x;
436 HARD_REG_SET uses, defs;
437 df_ref def, use;
438 bool split_p = false;
440 if (JUMP_P (BB_END (entry_block)))
442 /* To have more shrink-wrapping opportunities, prepare_shrink_wrap tries
443 to sink the copies from parameter to callee saved register out of
444 entry block. copyprop_hardreg_forward_bb_without_debug_insn is called
445 to release some dependences. */
446 copyprop_hardreg_forward_bb_without_debug_insn (entry_block);
449 CLEAR_HARD_REG_SET (uses);
450 CLEAR_HARD_REG_SET (defs);
451 FOR_BB_INSNS_REVERSE_SAFE (entry_block, insn, curr)
452 if (NONDEBUG_INSN_P (insn)
453 && !move_insn_for_shrink_wrap (entry_block, insn, uses, defs,
454 &split_p))
456 /* Add all defined registers to DEFs. */
457 FOR_EACH_INSN_DEF (def, insn)
459 x = DF_REF_REG (def);
460 if (REG_P (x) && HARD_REGISTER_P (x))
461 SET_HARD_REG_BIT (defs, REGNO (x));
464 /* Add all used registers to USESs. */
465 FOR_EACH_INSN_USE (use, insn)
467 x = DF_REF_REG (use);
468 if (REG_P (x) && HARD_REGISTER_P (x))
469 SET_HARD_REG_BIT (uses, REGNO (x));
474 /* Create a copy of BB instructions and insert at BEFORE. Redirect
475 preds of BB to COPY_BB if they don't appear in NEED_PROLOGUE. */
476 void
477 dup_block_and_redirect (basic_block bb, basic_block copy_bb, rtx_insn *before,
478 bitmap_head *need_prologue)
480 edge_iterator ei;
481 edge e;
482 rtx_insn *insn = BB_END (bb);
484 /* We know BB has a single successor, so there is no need to copy a
485 simple jump at the end of BB. */
486 if (simplejump_p (insn))
487 insn = PREV_INSN (insn);
489 start_sequence ();
490 duplicate_insn_chain (BB_HEAD (bb), insn);
491 if (dump_file)
493 unsigned count = 0;
494 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
495 if (active_insn_p (insn))
496 ++count;
497 fprintf (dump_file, "Duplicating bb %d to bb %d, %u active insns.\n",
498 bb->index, copy_bb->index, count);
500 insn = get_insns ();
501 end_sequence ();
502 emit_insn_before (insn, before);
504 /* Redirect all the paths that need no prologue into copy_bb. */
505 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei));)
506 if (!bitmap_bit_p (need_prologue, e->src->index))
508 int freq = EDGE_FREQUENCY (e);
509 copy_bb->count += e->count;
510 copy_bb->frequency += EDGE_FREQUENCY (e);
511 e->dest->count -= e->count;
512 if (e->dest->count < 0)
513 e->dest->count = 0;
514 e->dest->frequency -= freq;
515 if (e->dest->frequency < 0)
516 e->dest->frequency = 0;
517 redirect_edge_and_branch_force (e, copy_bb);
518 continue;
520 else
521 ei_next (&ei);
525 /* Try to perform a kind of shrink-wrapping, making sure the
526 prologue/epilogue is emitted only around those parts of the
527 function that require it. */
529 void
530 try_shrink_wrapping (edge *entry_edge, edge orig_entry_edge,
531 bitmap_head *bb_flags, rtx_insn *prologue_seq)
533 edge e;
534 edge_iterator ei;
535 bool nonempty_prologue = false;
536 unsigned max_grow_size;
537 rtx_insn *seq;
539 for (seq = prologue_seq; seq; seq = NEXT_INSN (seq))
540 if (!NOTE_P (seq) || NOTE_KIND (seq) != NOTE_INSN_PROLOGUE_END)
542 nonempty_prologue = true;
543 break;
546 if (flag_shrink_wrap && HAVE_simple_return
547 && (targetm.profile_before_prologue () || !crtl->profile)
548 && nonempty_prologue && !crtl->calls_eh_return)
550 HARD_REG_SET prologue_clobbered, prologue_used, live_on_edge;
551 struct hard_reg_set_container set_up_by_prologue;
552 rtx_insn *p_insn;
553 vec<basic_block> vec;
554 basic_block bb;
555 bitmap_head bb_antic_flags;
556 bitmap_head bb_on_list;
557 bitmap_head bb_tail;
559 if (dump_file)
560 fprintf (dump_file, "Attempting shrink-wrapping optimization.\n");
562 /* Compute the registers set and used in the prologue. */
563 CLEAR_HARD_REG_SET (prologue_clobbered);
564 CLEAR_HARD_REG_SET (prologue_used);
565 for (p_insn = prologue_seq; p_insn; p_insn = NEXT_INSN (p_insn))
567 HARD_REG_SET this_used;
568 if (!NONDEBUG_INSN_P (p_insn))
569 continue;
571 CLEAR_HARD_REG_SET (this_used);
572 note_uses (&PATTERN (p_insn), record_hard_reg_uses,
573 &this_used);
574 AND_COMPL_HARD_REG_SET (this_used, prologue_clobbered);
575 IOR_HARD_REG_SET (prologue_used, this_used);
576 note_stores (PATTERN (p_insn), record_hard_reg_sets,
577 &prologue_clobbered);
580 prepare_shrink_wrap ((*entry_edge)->dest);
582 bitmap_initialize (&bb_antic_flags, &bitmap_default_obstack);
583 bitmap_initialize (&bb_on_list, &bitmap_default_obstack);
584 bitmap_initialize (&bb_tail, &bitmap_default_obstack);
586 /* Find the set of basic blocks that require a stack frame,
587 and blocks that are too big to be duplicated. */
589 vec.create (n_basic_blocks_for_fn (cfun));
591 CLEAR_HARD_REG_SET (set_up_by_prologue.set);
592 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
593 STACK_POINTER_REGNUM);
594 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode, ARG_POINTER_REGNUM);
595 if (frame_pointer_needed)
596 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
597 HARD_FRAME_POINTER_REGNUM);
598 if (pic_offset_table_rtx
599 && (unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
600 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
601 PIC_OFFSET_TABLE_REGNUM);
602 if (crtl->drap_reg)
603 add_to_hard_reg_set (&set_up_by_prologue.set,
604 GET_MODE (crtl->drap_reg),
605 REGNO (crtl->drap_reg));
606 if (targetm.set_up_by_prologue)
607 targetm.set_up_by_prologue (&set_up_by_prologue);
609 /* We don't use a different max size depending on
610 optimize_bb_for_speed_p because increasing shrink-wrapping
611 opportunities by duplicating tail blocks can actually result
612 in an overall decrease in code size. */
613 max_grow_size = get_uncond_jump_length ();
614 max_grow_size *= PARAM_VALUE (PARAM_MAX_GROW_COPY_BB_INSNS);
616 FOR_EACH_BB_FN (bb, cfun)
618 rtx_insn *insn;
619 unsigned size = 0;
621 FOR_BB_INSNS (bb, insn)
622 if (NONDEBUG_INSN_P (insn))
624 if (requires_stack_frame_p (insn, prologue_used,
625 set_up_by_prologue.set))
627 if (bb == (*entry_edge)->dest)
628 goto fail_shrinkwrap;
629 bitmap_set_bit (bb_flags, bb->index);
630 vec.quick_push (bb);
631 break;
633 else if (size <= max_grow_size)
635 size += get_attr_min_length (insn);
636 if (size > max_grow_size)
637 bitmap_set_bit (&bb_on_list, bb->index);
642 /* Blocks that really need a prologue, or are too big for tails. */
643 bitmap_ior_into (&bb_on_list, bb_flags);
645 /* For every basic block that needs a prologue, mark all blocks
646 reachable from it, so as to ensure they are also seen as
647 requiring a prologue. */
648 while (!vec.is_empty ())
650 basic_block tmp_bb = vec.pop ();
652 FOR_EACH_EDGE (e, ei, tmp_bb->succs)
653 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
654 && bitmap_set_bit (bb_flags, e->dest->index))
655 vec.quick_push (e->dest);
658 /* Find the set of basic blocks that need no prologue, have a
659 single successor, can be duplicated, meet a max size
660 requirement, and go to the exit via like blocks. */
661 vec.quick_push (EXIT_BLOCK_PTR_FOR_FN (cfun));
662 while (!vec.is_empty ())
664 basic_block tmp_bb = vec.pop ();
666 FOR_EACH_EDGE (e, ei, tmp_bb->preds)
667 if (single_succ_p (e->src)
668 && !bitmap_bit_p (&bb_on_list, e->src->index)
669 && can_duplicate_block_p (e->src))
671 edge pe;
672 edge_iterator pei;
674 /* If there is predecessor of e->src which doesn't
675 need prologue and the edge is complex,
676 we might not be able to redirect the branch
677 to a copy of e->src. */
678 FOR_EACH_EDGE (pe, pei, e->src->preds)
679 if ((pe->flags & EDGE_COMPLEX) != 0
680 && !bitmap_bit_p (bb_flags, pe->src->index))
681 break;
682 if (pe == NULL && bitmap_set_bit (&bb_tail, e->src->index))
683 vec.quick_push (e->src);
687 /* Now walk backwards from every block that is marked as needing
688 a prologue to compute the bb_antic_flags bitmap. Exclude
689 tail blocks; They can be duplicated to be used on paths not
690 needing a prologue. */
691 bitmap_clear (&bb_on_list);
692 bitmap_and_compl (&bb_antic_flags, bb_flags, &bb_tail);
693 FOR_EACH_BB_FN (bb, cfun)
695 if (!bitmap_bit_p (&bb_antic_flags, bb->index))
696 continue;
697 FOR_EACH_EDGE (e, ei, bb->preds)
698 if (!bitmap_bit_p (&bb_antic_flags, e->src->index)
699 && bitmap_set_bit (&bb_on_list, e->src->index))
700 vec.quick_push (e->src);
702 while (!vec.is_empty ())
704 basic_block tmp_bb = vec.pop ();
705 bool all_set = true;
707 bitmap_clear_bit (&bb_on_list, tmp_bb->index);
708 FOR_EACH_EDGE (e, ei, tmp_bb->succs)
709 if (!bitmap_bit_p (&bb_antic_flags, e->dest->index))
711 all_set = false;
712 break;
715 if (all_set)
717 bitmap_set_bit (&bb_antic_flags, tmp_bb->index);
718 FOR_EACH_EDGE (e, ei, tmp_bb->preds)
719 if (!bitmap_bit_p (&bb_antic_flags, e->src->index)
720 && bitmap_set_bit (&bb_on_list, e->src->index))
721 vec.quick_push (e->src);
724 /* Find exactly one edge that leads to a block in ANTIC from
725 a block that isn't. */
726 if (!bitmap_bit_p (&bb_antic_flags, (*entry_edge)->dest->index))
727 FOR_EACH_BB_FN (bb, cfun)
729 if (!bitmap_bit_p (&bb_antic_flags, bb->index))
730 continue;
731 FOR_EACH_EDGE (e, ei, bb->preds)
732 if (!bitmap_bit_p (&bb_antic_flags, e->src->index))
734 if (*entry_edge != orig_entry_edge)
736 *entry_edge = orig_entry_edge;
737 if (dump_file)
738 fprintf (dump_file, "More than one candidate edge.\n");
739 goto fail_shrinkwrap;
741 if (dump_file)
742 fprintf (dump_file, "Found candidate edge for "
743 "shrink-wrapping, %d->%d.\n", e->src->index,
744 e->dest->index);
745 *entry_edge = e;
749 if (*entry_edge != orig_entry_edge)
751 /* Test whether the prologue is known to clobber any register
752 (other than FP or SP) which are live on the edge. */
753 CLEAR_HARD_REG_BIT (prologue_clobbered, STACK_POINTER_REGNUM);
754 if (frame_pointer_needed)
755 CLEAR_HARD_REG_BIT (prologue_clobbered, HARD_FRAME_POINTER_REGNUM);
756 REG_SET_TO_HARD_REG_SET (live_on_edge,
757 df_get_live_in ((*entry_edge)->dest));
758 if (hard_reg_set_intersect_p (live_on_edge, prologue_clobbered))
760 *entry_edge = orig_entry_edge;
761 if (dump_file)
762 fprintf (dump_file,
763 "Shrink-wrapping aborted due to clobber.\n");
766 if (*entry_edge != orig_entry_edge)
768 crtl->shrink_wrapped = true;
769 if (dump_file)
770 fprintf (dump_file, "Performing shrink-wrapping.\n");
772 /* Find tail blocks reachable from both blocks needing a
773 prologue and blocks not needing a prologue. */
774 if (!bitmap_empty_p (&bb_tail))
775 FOR_EACH_BB_FN (bb, cfun)
777 bool some_pro, some_no_pro;
778 if (!bitmap_bit_p (&bb_tail, bb->index))
779 continue;
780 some_pro = some_no_pro = false;
781 FOR_EACH_EDGE (e, ei, bb->preds)
783 if (bitmap_bit_p (bb_flags, e->src->index))
784 some_pro = true;
785 else
786 some_no_pro = true;
788 if (some_pro && some_no_pro)
789 vec.quick_push (bb);
790 else
791 bitmap_clear_bit (&bb_tail, bb->index);
793 /* Find the head of each tail. */
794 while (!vec.is_empty ())
796 basic_block tbb = vec.pop ();
798 if (!bitmap_bit_p (&bb_tail, tbb->index))
799 continue;
801 while (single_succ_p (tbb))
803 tbb = single_succ (tbb);
804 bitmap_clear_bit (&bb_tail, tbb->index);
807 /* Now duplicate the tails. */
808 if (!bitmap_empty_p (&bb_tail))
809 FOR_EACH_BB_REVERSE_FN (bb, cfun)
811 basic_block copy_bb, tbb;
812 rtx_insn *insert_point;
813 int eflags;
815 if (!bitmap_clear_bit (&bb_tail, bb->index))
816 continue;
818 /* Create a copy of BB, instructions and all, for
819 use on paths that don't need a prologue.
820 Ideal placement of the copy is on a fall-thru edge
821 or after a block that would jump to the copy. */
822 FOR_EACH_EDGE (e, ei, bb->preds)
823 if (!bitmap_bit_p (bb_flags, e->src->index)
824 && single_succ_p (e->src))
825 break;
826 if (e)
828 /* Make sure we insert after any barriers. */
829 rtx_insn *end = get_last_bb_insn (e->src);
830 copy_bb = create_basic_block (NEXT_INSN (end),
831 NULL_RTX, e->src);
832 BB_COPY_PARTITION (copy_bb, e->src);
834 else
836 /* Otherwise put the copy at the end of the function. */
837 copy_bb = create_basic_block (NULL_RTX, NULL_RTX,
838 EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb);
839 BB_COPY_PARTITION (copy_bb, bb);
842 insert_point = emit_note_after (NOTE_INSN_DELETED,
843 BB_END (copy_bb));
844 emit_barrier_after (BB_END (copy_bb));
846 tbb = bb;
847 while (1)
849 dup_block_and_redirect (tbb, copy_bb, insert_point,
850 bb_flags);
851 tbb = single_succ (tbb);
852 if (tbb == EXIT_BLOCK_PTR_FOR_FN (cfun))
853 break;
854 e = split_block (copy_bb, PREV_INSN (insert_point));
855 copy_bb = e->dest;
858 /* Quiet verify_flow_info by (ab)using EDGE_FAKE.
859 We have yet to add a simple_return to the tails,
860 as we'd like to first convert_jumps_to_returns in
861 case the block is no longer used after that. */
862 eflags = EDGE_FAKE;
863 if (CALL_P (PREV_INSN (insert_point))
864 && SIBLING_CALL_P (PREV_INSN (insert_point)))
865 eflags = EDGE_SIBCALL | EDGE_ABNORMAL;
866 make_single_succ_edge (copy_bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
867 eflags);
869 /* verify_flow_info doesn't like a note after a
870 sibling call. */
871 delete_insn (insert_point);
872 if (bitmap_empty_p (&bb_tail))
873 break;
877 fail_shrinkwrap:
878 bitmap_clear (&bb_tail);
879 bitmap_clear (&bb_antic_flags);
880 bitmap_clear (&bb_on_list);
881 vec.release ();
885 /* If we're allowed to generate a simple return instruction, then by
886 definition we don't need a full epilogue. If the last basic
887 block before the exit block does not contain active instructions,
888 examine its predecessors and try to emit (conditional) return
889 instructions. */
891 edge
892 get_unconverted_simple_return (edge exit_fallthru_edge, bitmap_head bb_flags,
893 vec<edge> *unconverted_simple_returns,
894 rtx_insn **returnjump)
896 if (optimize)
898 unsigned i, last;
900 /* convert_jumps_to_returns may add to preds of the exit block
901 (but won't remove). Stop at end of current preds. */
902 last = EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
903 for (i = 0; i < last; i++)
905 edge e = EDGE_I (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds, i);
906 if (LABEL_P (BB_HEAD (e->src))
907 && !bitmap_bit_p (&bb_flags, e->src->index)
908 && !active_insn_between (BB_HEAD (e->src), BB_END (e->src)))
909 *unconverted_simple_returns
910 = convert_jumps_to_returns (e->src, true,
911 *unconverted_simple_returns);
915 if (exit_fallthru_edge != NULL
916 && EDGE_COUNT (exit_fallthru_edge->src->preds) != 0
917 && !bitmap_bit_p (&bb_flags, exit_fallthru_edge->src->index))
919 basic_block last_bb;
921 last_bb = emit_return_for_exit (exit_fallthru_edge, true);
922 *returnjump = BB_END (last_bb);
923 exit_fallthru_edge = NULL;
925 return exit_fallthru_edge;
928 /* If there were branches to an empty LAST_BB which we tried to
929 convert to conditional simple_returns, but couldn't for some
930 reason, create a block to hold a simple_return insn and redirect
931 those remaining edges. */
933 void
934 convert_to_simple_return (edge entry_edge, edge orig_entry_edge,
935 bitmap_head bb_flags, rtx_insn *returnjump,
936 vec<edge> unconverted_simple_returns)
938 edge e;
939 edge_iterator ei;
941 if (!unconverted_simple_returns.is_empty ())
943 basic_block simple_return_block_hot = NULL;
944 basic_block simple_return_block_cold = NULL;
945 edge pending_edge_hot = NULL;
946 edge pending_edge_cold = NULL;
947 basic_block exit_pred;
948 int i;
950 gcc_assert (entry_edge != orig_entry_edge);
952 /* See if we can reuse the last insn that was emitted for the
953 epilogue. */
954 if (returnjump != NULL_RTX
955 && JUMP_LABEL (returnjump) == simple_return_rtx)
957 e = split_block (BLOCK_FOR_INSN (returnjump), PREV_INSN (returnjump));
958 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
959 simple_return_block_hot = e->dest;
960 else
961 simple_return_block_cold = e->dest;
964 /* Also check returns we might need to add to tail blocks. */
965 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
966 if (EDGE_COUNT (e->src->preds) != 0
967 && (e->flags & EDGE_FAKE) != 0
968 && !bitmap_bit_p (&bb_flags, e->src->index))
970 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
971 pending_edge_hot = e;
972 else
973 pending_edge_cold = e;
976 /* Save a pointer to the exit's predecessor BB for use in
977 inserting new BBs at the end of the function. Do this
978 after the call to split_block above which may split
979 the original exit pred. */
980 exit_pred = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
982 FOR_EACH_VEC_ELT (unconverted_simple_returns, i, e)
984 basic_block *pdest_bb;
985 edge pending;
987 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
989 pdest_bb = &simple_return_block_hot;
990 pending = pending_edge_hot;
992 else
994 pdest_bb = &simple_return_block_cold;
995 pending = pending_edge_cold;
998 if (*pdest_bb == NULL && pending != NULL)
1000 emit_return_into_block (true, pending->src);
1001 pending->flags &= ~(EDGE_FALLTHRU | EDGE_FAKE);
1002 *pdest_bb = pending->src;
1004 else if (*pdest_bb == NULL)
1006 basic_block bb;
1007 rtx_insn *start;
1009 bb = create_basic_block (NULL, NULL, exit_pred);
1010 BB_COPY_PARTITION (bb, e->src);
1011 start = emit_jump_insn_after (gen_simple_return (),
1012 BB_END (bb));
1013 JUMP_LABEL (start) = simple_return_rtx;
1014 emit_barrier_after (start);
1016 *pdest_bb = bb;
1017 make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1019 redirect_edge_and_branch_force (e, *pdest_bb);
1021 unconverted_simple_returns.release ();
1024 if (entry_edge != orig_entry_edge)
1026 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
1027 if (EDGE_COUNT (e->src->preds) != 0
1028 && (e->flags & EDGE_FAKE) != 0
1029 && !bitmap_bit_p (&bb_flags, e->src->index))
1031 emit_return_into_block (true, e->src);
1032 e->flags &= ~(EDGE_FALLTHRU | EDGE_FAKE);
1037 #endif