Makefile.in: add shrink-wrap.o.
[gcc.git] / gcc / shrink-wrap.c
1 /* Expands front end tree to back end RTL for GCC.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
20 /* This file handles shrink-wrapping related optimizations. */
21
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
56
57 #ifdef HAVE_simple_return
58
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, HARD_REG_SET prologue_used,
65 HARD_REG_SET set_up_by_prologue)
66 {
67 df_ref *df_rec;
68 HARD_REG_SET hardregs;
69 unsigned regno;
70
71 if (CALL_P (insn))
72 return !SIBLING_CALL_P (insn);
73
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;
77
78 CLEAR_HARD_REG_SET (hardregs);
79 for (df_rec = DF_INSN_DEFS (insn); *df_rec; df_rec++)
80 {
81 rtx dreg = DF_REF_REG (*df_rec);
82
83 if (!REG_P (dreg))
84 continue;
85
86 add_to_hard_reg_set (&hardregs, GET_MODE (dreg),
87 REGNO (dreg));
88 }
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;
96
97 for (df_rec = DF_INSN_USES (insn); *df_rec; df_rec++)
98 {
99 rtx reg = DF_REF_REG (*df_rec);
100
101 if (!REG_P (reg))
102 continue;
103
104 add_to_hard_reg_set (&hardregs, GET_MODE (reg),
105 REGNO (reg));
106 }
107 if (hard_reg_set_intersect_p (hardregs, set_up_by_prologue))
108 return true;
109
110 return false;
111 }
112
113 /* See whether BB has a single successor that uses [REGNO, END_REGNO),
114 and if BB is its only predecessor. Return that block if so,
115 otherwise return null. */
116
117 static basic_block
118 next_block_for_reg (basic_block bb, int regno, int end_regno)
119 {
120 edge e, live_edge;
121 edge_iterator ei;
122 bitmap live;
123 int i;
124
125 live_edge = NULL;
126 FOR_EACH_EDGE (e, ei, bb->succs)
127 {
128 live = df_get_live_in (e->dest);
129 for (i = regno; i < end_regno; i++)
130 if (REGNO_REG_SET_P (live, i))
131 {
132 if (live_edge && live_edge != e)
133 return NULL;
134 live_edge = e;
135 }
136 }
137
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;
142
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;
150
151 if (EDGE_COUNT (live_edge->dest->preds) > 1)
152 return NULL;
153
154 return live_edge->dest;
155 }
156
157 /* Try to move INSN from BB to a successor. Return true on success.
158 USES and DEFS are the set of registers that are used and defined
159 after INSN in BB. */
160
161 static bool
162 move_insn_for_shrink_wrap (basic_block bb, rtx insn,
163 const HARD_REG_SET uses,
164 const HARD_REG_SET defs)
165 {
166 rtx set, src, dest;
167 bitmap live_out, live_in, bb_uses, bb_defs;
168 unsigned int i, dregno, end_dregno, sregno, end_sregno;
169 basic_block next_block;
170
171 /* Look for a simple register copy. */
172 set = single_set (insn);
173 if (!set)
174 return false;
175 src = SET_SRC (set);
176 dest = SET_DEST (set);
177 if (!REG_P (dest) || !REG_P (src))
178 return false;
179
180 /* Make sure that the source register isn't defined later in BB. */
181 sregno = REGNO (src);
182 end_sregno = END_REGNO (src);
183 if (overlaps_hard_reg_set_p (defs, GET_MODE (src), sregno))
184 return false;
185
186 /* Make sure that the destination register isn't referenced later in BB. */
187 dregno = REGNO (dest);
188 end_dregno = END_REGNO (dest);
189 if (overlaps_hard_reg_set_p (uses, GET_MODE (dest), dregno)
190 || overlaps_hard_reg_set_p (defs, GET_MODE (dest), dregno))
191 return false;
192
193 /* See whether there is a successor block to which we could move INSN. */
194 next_block = next_block_for_reg (bb, dregno, end_dregno);
195 if (!next_block)
196 return false;
197
198 /* At this point we are committed to moving INSN, but let's try to
199 move it as far as we can. */
200 do
201 {
202 live_out = df_get_live_out (bb);
203 live_in = df_get_live_in (next_block);
204 bb = next_block;
205
206 /* Check whether BB uses DEST or clobbers DEST. We need to add
207 INSN to BB if so. Either way, DEST is no longer live on entry,
208 except for any part that overlaps SRC (next loop). */
209 bb_uses = &DF_LR_BB_INFO (bb)->use;
210 bb_defs = &DF_LR_BB_INFO (bb)->def;
211 if (df_live)
212 {
213 for (i = dregno; i < end_dregno; i++)
214 {
215 if (REGNO_REG_SET_P (bb_uses, i) || REGNO_REG_SET_P (bb_defs, i)
216 || REGNO_REG_SET_P (&DF_LIVE_BB_INFO (bb)->gen, i))
217 next_block = NULL;
218 CLEAR_REGNO_REG_SET (live_out, i);
219 CLEAR_REGNO_REG_SET (live_in, i);
220 }
221
222 /* Check whether BB clobbers SRC. We need to add INSN to BB if so.
223 Either way, SRC is now live on entry. */
224 for (i = sregno; i < end_sregno; i++)
225 {
226 if (REGNO_REG_SET_P (bb_defs, i)
227 || REGNO_REG_SET_P (&DF_LIVE_BB_INFO (bb)->gen, i))
228 next_block = NULL;
229 SET_REGNO_REG_SET (live_out, i);
230 SET_REGNO_REG_SET (live_in, i);
231 }
232 }
233 else
234 {
235 /* DF_LR_BB_INFO (bb)->def does not comprise the DF_REF_PARTIAL and
236 DF_REF_CONDITIONAL defs. So if DF_LIVE doesn't exist, i.e.
237 at -O1, just give up searching NEXT_BLOCK. */
238 next_block = NULL;
239 for (i = dregno; i < end_dregno; i++)
240 {
241 CLEAR_REGNO_REG_SET (live_out, i);
242 CLEAR_REGNO_REG_SET (live_in, i);
243 }
244
245 for (i = sregno; i < end_sregno; i++)
246 {
247 SET_REGNO_REG_SET (live_out, i);
248 SET_REGNO_REG_SET (live_in, i);
249 }
250 }
251
252 /* If we don't need to add the move to BB, look for a single
253 successor block. */
254 if (next_block)
255 next_block = next_block_for_reg (next_block, dregno, end_dregno);
256 }
257 while (next_block);
258
259 /* BB now defines DEST. It only uses the parts of DEST that overlap SRC
260 (next loop). */
261 for (i = dregno; i < end_dregno; i++)
262 {
263 CLEAR_REGNO_REG_SET (bb_uses, i);
264 SET_REGNO_REG_SET (bb_defs, i);
265 }
266
267 /* BB now uses SRC. */
268 for (i = sregno; i < end_sregno; i++)
269 SET_REGNO_REG_SET (bb_uses, i);
270
271 emit_insn_after (PATTERN (insn), bb_note (bb));
272 delete_insn (insn);
273 return true;
274 }
275
276 /* Look for register copies in the first block of the function, and move
277 them down into successor blocks if the register is used only on one
278 path. This exposes more opportunities for shrink-wrapping. These
279 kinds of sets often occur when incoming argument registers are moved
280 to call-saved registers because their values are live across one or
281 more calls during the function. */
282
283 void
284 prepare_shrink_wrap (basic_block entry_block)
285 {
286 rtx insn, curr, x;
287 HARD_REG_SET uses, defs;
288 df_ref *ref;
289
290 CLEAR_HARD_REG_SET (uses);
291 CLEAR_HARD_REG_SET (defs);
292 FOR_BB_INSNS_REVERSE_SAFE (entry_block, insn, curr)
293 if (NONDEBUG_INSN_P (insn)
294 && !move_insn_for_shrink_wrap (entry_block, insn, uses, defs))
295 {
296 /* Add all defined registers to DEFs. */
297 for (ref = DF_INSN_DEFS (insn); *ref; ref++)
298 {
299 x = DF_REF_REG (*ref);
300 if (REG_P (x) && HARD_REGISTER_P (x))
301 SET_HARD_REG_BIT (defs, REGNO (x));
302 }
303
304 /* Add all used registers to USESs. */
305 for (ref = DF_INSN_USES (insn); *ref; ref++)
306 {
307 x = DF_REF_REG (*ref);
308 if (REG_P (x) && HARD_REGISTER_P (x))
309 SET_HARD_REG_BIT (uses, REGNO (x));
310 }
311 }
312 }
313
314 /* Create a copy of BB instructions and insert at BEFORE. Redirect
315 preds of BB to COPY_BB if they don't appear in NEED_PROLOGUE. */
316 void
317 dup_block_and_redirect (basic_block bb, basic_block copy_bb, rtx before,
318 bitmap_head *need_prologue)
319 {
320 edge_iterator ei;
321 edge e;
322 rtx insn = BB_END (bb);
323
324 /* We know BB has a single successor, so there is no need to copy a
325 simple jump at the end of BB. */
326 if (simplejump_p (insn))
327 insn = PREV_INSN (insn);
328
329 start_sequence ();
330 duplicate_insn_chain (BB_HEAD (bb), insn);
331 if (dump_file)
332 {
333 unsigned count = 0;
334 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
335 if (active_insn_p (insn))
336 ++count;
337 fprintf (dump_file, "Duplicating bb %d to bb %d, %u active insns.\n",
338 bb->index, copy_bb->index, count);
339 }
340 insn = get_insns ();
341 end_sequence ();
342 emit_insn_before (insn, before);
343
344 /* Redirect all the paths that need no prologue into copy_bb. */
345 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei));)
346 if (!bitmap_bit_p (need_prologue, e->src->index))
347 {
348 int freq = EDGE_FREQUENCY (e);
349 copy_bb->count += e->count;
350 copy_bb->frequency += EDGE_FREQUENCY (e);
351 e->dest->count -= e->count;
352 if (e->dest->count < 0)
353 e->dest->count = 0;
354 e->dest->frequency -= freq;
355 if (e->dest->frequency < 0)
356 e->dest->frequency = 0;
357 redirect_edge_and_branch_force (e, copy_bb);
358 continue;
359 }
360 else
361 ei_next (&ei);
362 }
363
364
365 /* Try to perform a kind of shrink-wrapping, making sure the
366 prologue/epilogue is emitted only around those parts of the
367 function that require it. */
368
369 void
370 try_shrink_wrapping (edge *entry_edge, edge orig_entry_edge,
371 bitmap_head *bb_flags, rtx prologue_seq)
372 {
373 edge e;
374 edge_iterator ei;
375 bool nonempty_prologue = false;
376 unsigned max_grow_size;
377 rtx seq;
378
379 for (seq = prologue_seq; seq; seq = NEXT_INSN (seq))
380 if (!NOTE_P (seq) || NOTE_KIND (seq) != NOTE_INSN_PROLOGUE_END)
381 {
382 nonempty_prologue = true;
383 break;
384 }
385
386 if (flag_shrink_wrap && HAVE_simple_return
387 && (targetm.profile_before_prologue () || !crtl->profile)
388 && nonempty_prologue && !crtl->calls_eh_return)
389 {
390 HARD_REG_SET prologue_clobbered, prologue_used, live_on_edge;
391 struct hard_reg_set_container set_up_by_prologue;
392 rtx p_insn;
393 vec<basic_block> vec;
394 basic_block bb;
395 bitmap_head bb_antic_flags;
396 bitmap_head bb_on_list;
397 bitmap_head bb_tail;
398
399 if (dump_file)
400 fprintf (dump_file, "Attempting shrink-wrapping optimization.\n");
401
402 /* Compute the registers set and used in the prologue. */
403 CLEAR_HARD_REG_SET (prologue_clobbered);
404 CLEAR_HARD_REG_SET (prologue_used);
405 for (p_insn = prologue_seq; p_insn; p_insn = NEXT_INSN (p_insn))
406 {
407 HARD_REG_SET this_used;
408 if (!NONDEBUG_INSN_P (p_insn))
409 continue;
410
411 CLEAR_HARD_REG_SET (this_used);
412 note_uses (&PATTERN (p_insn), record_hard_reg_uses,
413 &this_used);
414 AND_COMPL_HARD_REG_SET (this_used, prologue_clobbered);
415 IOR_HARD_REG_SET (prologue_used, this_used);
416 note_stores (PATTERN (p_insn), record_hard_reg_sets,
417 &prologue_clobbered);
418 }
419
420 prepare_shrink_wrap ((*entry_edge)->dest);
421
422 bitmap_initialize (&bb_antic_flags, &bitmap_default_obstack);
423 bitmap_initialize (&bb_on_list, &bitmap_default_obstack);
424 bitmap_initialize (&bb_tail, &bitmap_default_obstack);
425
426 /* Find the set of basic blocks that require a stack frame,
427 and blocks that are too big to be duplicated. */
428
429 vec.create (n_basic_blocks_for_fn (cfun));
430
431 CLEAR_HARD_REG_SET (set_up_by_prologue.set);
432 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
433 STACK_POINTER_REGNUM);
434 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode, ARG_POINTER_REGNUM);
435 if (frame_pointer_needed)
436 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
437 HARD_FRAME_POINTER_REGNUM);
438 if (pic_offset_table_rtx)
439 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
440 PIC_OFFSET_TABLE_REGNUM);
441 if (crtl->drap_reg)
442 add_to_hard_reg_set (&set_up_by_prologue.set,
443 GET_MODE (crtl->drap_reg),
444 REGNO (crtl->drap_reg));
445 if (targetm.set_up_by_prologue)
446 targetm.set_up_by_prologue (&set_up_by_prologue);
447
448 /* We don't use a different max size depending on
449 optimize_bb_for_speed_p because increasing shrink-wrapping
450 opportunities by duplicating tail blocks can actually result
451 in an overall decrease in code size. */
452 max_grow_size = get_uncond_jump_length ();
453 max_grow_size *= PARAM_VALUE (PARAM_MAX_GROW_COPY_BB_INSNS);
454
455 FOR_EACH_BB_FN (bb, cfun)
456 {
457 rtx insn;
458 unsigned size = 0;
459
460 FOR_BB_INSNS (bb, insn)
461 if (NONDEBUG_INSN_P (insn))
462 {
463 if (requires_stack_frame_p (insn, prologue_used,
464 set_up_by_prologue.set))
465 {
466 if (bb == (*entry_edge)->dest)
467 goto fail_shrinkwrap;
468 bitmap_set_bit (bb_flags, bb->index);
469 vec.quick_push (bb);
470 break;
471 }
472 else if (size <= max_grow_size)
473 {
474 size += get_attr_min_length (insn);
475 if (size > max_grow_size)
476 bitmap_set_bit (&bb_on_list, bb->index);
477 }
478 }
479 }
480
481 /* Blocks that really need a prologue, or are too big for tails. */
482 bitmap_ior_into (&bb_on_list, bb_flags);
483
484 /* For every basic block that needs a prologue, mark all blocks
485 reachable from it, so as to ensure they are also seen as
486 requiring a prologue. */
487 while (!vec.is_empty ())
488 {
489 basic_block tmp_bb = vec.pop ();
490
491 FOR_EACH_EDGE (e, ei, tmp_bb->succs)
492 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
493 && bitmap_set_bit (bb_flags, e->dest->index))
494 vec.quick_push (e->dest);
495 }
496
497 /* Find the set of basic blocks that need no prologue, have a
498 single successor, can be duplicated, meet a max size
499 requirement, and go to the exit via like blocks. */
500 vec.quick_push (EXIT_BLOCK_PTR_FOR_FN (cfun));
501 while (!vec.is_empty ())
502 {
503 basic_block tmp_bb = vec.pop ();
504
505 FOR_EACH_EDGE (e, ei, tmp_bb->preds)
506 if (single_succ_p (e->src)
507 && !bitmap_bit_p (&bb_on_list, e->src->index)
508 && can_duplicate_block_p (e->src))
509 {
510 edge pe;
511 edge_iterator pei;
512
513 /* If there is predecessor of e->src which doesn't
514 need prologue and the edge is complex,
515 we might not be able to redirect the branch
516 to a copy of e->src. */
517 FOR_EACH_EDGE (pe, pei, e->src->preds)
518 if ((pe->flags & EDGE_COMPLEX) != 0
519 && !bitmap_bit_p (bb_flags, pe->src->index))
520 break;
521 if (pe == NULL && bitmap_set_bit (&bb_tail, e->src->index))
522 vec.quick_push (e->src);
523 }
524 }
525
526 /* Now walk backwards from every block that is marked as needing
527 a prologue to compute the bb_antic_flags bitmap. Exclude
528 tail blocks; They can be duplicated to be used on paths not
529 needing a prologue. */
530 bitmap_clear (&bb_on_list);
531 bitmap_and_compl (&bb_antic_flags, bb_flags, &bb_tail);
532 FOR_EACH_BB_FN (bb, cfun)
533 {
534 if (!bitmap_bit_p (&bb_antic_flags, bb->index))
535 continue;
536 FOR_EACH_EDGE (e, ei, bb->preds)
537 if (!bitmap_bit_p (&bb_antic_flags, e->src->index)
538 && bitmap_set_bit (&bb_on_list, e->src->index))
539 vec.quick_push (e->src);
540 }
541 while (!vec.is_empty ())
542 {
543 basic_block tmp_bb = vec.pop ();
544 bool all_set = true;
545
546 bitmap_clear_bit (&bb_on_list, tmp_bb->index);
547 FOR_EACH_EDGE (e, ei, tmp_bb->succs)
548 if (!bitmap_bit_p (&bb_antic_flags, e->dest->index))
549 {
550 all_set = false;
551 break;
552 }
553
554 if (all_set)
555 {
556 bitmap_set_bit (&bb_antic_flags, tmp_bb->index);
557 FOR_EACH_EDGE (e, ei, tmp_bb->preds)
558 if (!bitmap_bit_p (&bb_antic_flags, e->src->index)
559 && bitmap_set_bit (&bb_on_list, e->src->index))
560 vec.quick_push (e->src);
561 }
562 }
563 /* Find exactly one edge that leads to a block in ANTIC from
564 a block that isn't. */
565 if (!bitmap_bit_p (&bb_antic_flags, (*entry_edge)->dest->index))
566 FOR_EACH_BB_FN (bb, cfun)
567 {
568 if (!bitmap_bit_p (&bb_antic_flags, bb->index))
569 continue;
570 FOR_EACH_EDGE (e, ei, bb->preds)
571 if (!bitmap_bit_p (&bb_antic_flags, e->src->index))
572 {
573 if (*entry_edge != orig_entry_edge)
574 {
575 *entry_edge = orig_entry_edge;
576 if (dump_file)
577 fprintf (dump_file, "More than one candidate edge.\n");
578 goto fail_shrinkwrap;
579 }
580 if (dump_file)
581 fprintf (dump_file, "Found candidate edge for "
582 "shrink-wrapping, %d->%d.\n", e->src->index,
583 e->dest->index);
584 *entry_edge = e;
585 }
586 }
587
588 if (*entry_edge != orig_entry_edge)
589 {
590 /* Test whether the prologue is known to clobber any register
591 (other than FP or SP) which are live on the edge. */
592 CLEAR_HARD_REG_BIT (prologue_clobbered, STACK_POINTER_REGNUM);
593 if (frame_pointer_needed)
594 CLEAR_HARD_REG_BIT (prologue_clobbered, HARD_FRAME_POINTER_REGNUM);
595 REG_SET_TO_HARD_REG_SET (live_on_edge,
596 df_get_live_in ((*entry_edge)->dest));
597 if (hard_reg_set_intersect_p (live_on_edge, prologue_clobbered))
598 {
599 *entry_edge = orig_entry_edge;
600 if (dump_file)
601 fprintf (dump_file,
602 "Shrink-wrapping aborted due to clobber.\n");
603 }
604 }
605 if (*entry_edge != orig_entry_edge)
606 {
607 crtl->shrink_wrapped = true;
608 if (dump_file)
609 fprintf (dump_file, "Performing shrink-wrapping.\n");
610
611 /* Find tail blocks reachable from both blocks needing a
612 prologue and blocks not needing a prologue. */
613 if (!bitmap_empty_p (&bb_tail))
614 FOR_EACH_BB_FN (bb, cfun)
615 {
616 bool some_pro, some_no_pro;
617 if (!bitmap_bit_p (&bb_tail, bb->index))
618 continue;
619 some_pro = some_no_pro = false;
620 FOR_EACH_EDGE (e, ei, bb->preds)
621 {
622 if (bitmap_bit_p (bb_flags, e->src->index))
623 some_pro = true;
624 else
625 some_no_pro = true;
626 }
627 if (some_pro && some_no_pro)
628 vec.quick_push (bb);
629 else
630 bitmap_clear_bit (&bb_tail, bb->index);
631 }
632 /* Find the head of each tail. */
633 while (!vec.is_empty ())
634 {
635 basic_block tbb = vec.pop ();
636
637 if (!bitmap_bit_p (&bb_tail, tbb->index))
638 continue;
639
640 while (single_succ_p (tbb))
641 {
642 tbb = single_succ (tbb);
643 bitmap_clear_bit (&bb_tail, tbb->index);
644 }
645 }
646 /* Now duplicate the tails. */
647 if (!bitmap_empty_p (&bb_tail))
648 FOR_EACH_BB_REVERSE_FN (bb, cfun)
649 {
650 basic_block copy_bb, tbb;
651 rtx insert_point;
652 int eflags;
653
654 if (!bitmap_clear_bit (&bb_tail, bb->index))
655 continue;
656
657 /* Create a copy of BB, instructions and all, for
658 use on paths that don't need a prologue.
659 Ideal placement of the copy is on a fall-thru edge
660 or after a block that would jump to the copy. */
661 FOR_EACH_EDGE (e, ei, bb->preds)
662 if (!bitmap_bit_p (bb_flags, e->src->index)
663 && single_succ_p (e->src))
664 break;
665 if (e)
666 {
667 /* Make sure we insert after any barriers. */
668 rtx end = get_last_bb_insn (e->src);
669 copy_bb = create_basic_block (NEXT_INSN (end),
670 NULL_RTX, e->src);
671 BB_COPY_PARTITION (copy_bb, e->src);
672 }
673 else
674 {
675 /* Otherwise put the copy at the end of the function. */
676 copy_bb = create_basic_block (NULL_RTX, NULL_RTX,
677 EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb);
678 BB_COPY_PARTITION (copy_bb, bb);
679 }
680
681 insert_point = emit_note_after (NOTE_INSN_DELETED,
682 BB_END (copy_bb));
683 emit_barrier_after (BB_END (copy_bb));
684
685 tbb = bb;
686 while (1)
687 {
688 dup_block_and_redirect (tbb, copy_bb, insert_point,
689 bb_flags);
690 tbb = single_succ (tbb);
691 if (tbb == EXIT_BLOCK_PTR_FOR_FN (cfun))
692 break;
693 e = split_block (copy_bb, PREV_INSN (insert_point));
694 copy_bb = e->dest;
695 }
696
697 /* Quiet verify_flow_info by (ab)using EDGE_FAKE.
698 We have yet to add a simple_return to the tails,
699 as we'd like to first convert_jumps_to_returns in
700 case the block is no longer used after that. */
701 eflags = EDGE_FAKE;
702 if (CALL_P (PREV_INSN (insert_point))
703 && SIBLING_CALL_P (PREV_INSN (insert_point)))
704 eflags = EDGE_SIBCALL | EDGE_ABNORMAL;
705 make_single_succ_edge (copy_bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
706 eflags);
707
708 /* verify_flow_info doesn't like a note after a
709 sibling call. */
710 delete_insn (insert_point);
711 if (bitmap_empty_p (&bb_tail))
712 break;
713 }
714 }
715
716 fail_shrinkwrap:
717 bitmap_clear (&bb_tail);
718 bitmap_clear (&bb_antic_flags);
719 bitmap_clear (&bb_on_list);
720 vec.release ();
721 }
722 }
723
724 /* If we're allowed to generate a simple return instruction, then by
725 definition we don't need a full epilogue. If the last basic
726 block before the exit block does not contain active instructions,
727 examine its predecessors and try to emit (conditional) return
728 instructions. */
729
730 edge
731 get_unconverted_simple_return (edge exit_fallthru_edge, bitmap_head bb_flags,
732 vec<edge> *unconverted_simple_returns,
733 rtx *returnjump)
734 {
735 if (optimize)
736 {
737 unsigned i, last;
738
739 /* convert_jumps_to_returns may add to preds of the exit block
740 (but won't remove). Stop at end of current preds. */
741 last = EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
742 for (i = 0; i < last; i++)
743 {
744 edge e = EDGE_I (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds, i);
745 if (LABEL_P (BB_HEAD (e->src))
746 && !bitmap_bit_p (&bb_flags, e->src->index)
747 && !active_insn_between (BB_HEAD (e->src), BB_END (e->src)))
748 *unconverted_simple_returns
749 = convert_jumps_to_returns (e->src, true,
750 *unconverted_simple_returns);
751 }
752 }
753
754 if (exit_fallthru_edge != NULL
755 && EDGE_COUNT (exit_fallthru_edge->src->preds) != 0
756 && !bitmap_bit_p (&bb_flags, exit_fallthru_edge->src->index))
757 {
758 basic_block last_bb;
759
760 last_bb = emit_return_for_exit (exit_fallthru_edge, true);
761 *returnjump = BB_END (last_bb);
762 exit_fallthru_edge = NULL;
763 }
764 return exit_fallthru_edge;
765 }
766
767 /* If there were branches to an empty LAST_BB which we tried to
768 convert to conditional simple_returns, but couldn't for some
769 reason, create a block to hold a simple_return insn and redirect
770 those remaining edges. */
771
772 void
773 convert_to_simple_return (edge entry_edge, edge orig_entry_edge,
774 bitmap_head bb_flags, rtx returnjump,
775 vec<edge> unconverted_simple_returns)
776 {
777 edge e;
778 edge_iterator ei;
779
780 if (!unconverted_simple_returns.is_empty ())
781 {
782 basic_block simple_return_block_hot = NULL;
783 basic_block simple_return_block_cold = NULL;
784 edge pending_edge_hot = NULL;
785 edge pending_edge_cold = NULL;
786 basic_block exit_pred;
787 int i;
788
789 gcc_assert (entry_edge != orig_entry_edge);
790
791 /* See if we can reuse the last insn that was emitted for the
792 epilogue. */
793 if (returnjump != NULL_RTX
794 && JUMP_LABEL (returnjump) == simple_return_rtx)
795 {
796 e = split_block (BLOCK_FOR_INSN (returnjump), PREV_INSN (returnjump));
797 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
798 simple_return_block_hot = e->dest;
799 else
800 simple_return_block_cold = e->dest;
801 }
802
803 /* Also check returns we might need to add to tail blocks. */
804 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
805 if (EDGE_COUNT (e->src->preds) != 0
806 && (e->flags & EDGE_FAKE) != 0
807 && !bitmap_bit_p (&bb_flags, e->src->index))
808 {
809 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
810 pending_edge_hot = e;
811 else
812 pending_edge_cold = e;
813 }
814
815 /* Save a pointer to the exit's predecessor BB for use in
816 inserting new BBs at the end of the function. Do this
817 after the call to split_block above which may split
818 the original exit pred. */
819 exit_pred = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
820
821 FOR_EACH_VEC_ELT (unconverted_simple_returns, i, e)
822 {
823 basic_block *pdest_bb;
824 edge pending;
825
826 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
827 {
828 pdest_bb = &simple_return_block_hot;
829 pending = pending_edge_hot;
830 }
831 else
832 {
833 pdest_bb = &simple_return_block_cold;
834 pending = pending_edge_cold;
835 }
836
837 if (*pdest_bb == NULL && pending != NULL)
838 {
839 emit_return_into_block (true, pending->src);
840 pending->flags &= ~(EDGE_FALLTHRU | EDGE_FAKE);
841 *pdest_bb = pending->src;
842 }
843 else if (*pdest_bb == NULL)
844 {
845 basic_block bb;
846 rtx start;
847
848 bb = create_basic_block (NULL, NULL, exit_pred);
849 BB_COPY_PARTITION (bb, e->src);
850 start = emit_jump_insn_after (gen_simple_return (),
851 BB_END (bb));
852 JUMP_LABEL (start) = simple_return_rtx;
853 emit_barrier_after (start);
854
855 *pdest_bb = bb;
856 make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
857 }
858 redirect_edge_and_branch_force (e, *pdest_bb);
859 }
860 unconverted_simple_returns.release ();
861 }
862
863 if (entry_edge != orig_entry_edge)
864 {
865 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
866 if (EDGE_COUNT (e->src->preds) != 0
867 && (e->flags & EDGE_FAKE) != 0
868 && !bitmap_bit_p (&bb_flags, e->src->index))
869 {
870 emit_return_into_block (true, e->src);
871 e->flags &= ~(EDGE_FALLTHRU | EDGE_FAKE);
872 }
873 }
874 }
875
876 #endif