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