re PR target/69140 (stack alignment + O1 breaks with Microsoft ABI)
[gcc.git] / gcc / shrink-wrap.c
1 /* Shrink-wrapping related optimizations.
2 Copyright (C) 1987-2016 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 "backend.h"
26 #include "target.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "cfghooks.h"
30 #include "df.h"
31 #include "tm_p.h"
32 #include "regs.h"
33 #include "emit-rtl.h"
34 #include "output.h"
35 #include "tree-pass.h"
36 #include "cfgrtl.h"
37 #include "params.h"
38 #include "bb-reorder.h"
39 #include "shrink-wrap.h"
40 #include "regcprop.h"
41 #include "rtl-iter.h"
42
43
44 /* Return true if INSN requires the stack frame to be set up.
45 PROLOGUE_USED contains the hard registers used in the function
46 prologue. SET_UP_BY_PROLOGUE is the set of registers we expect the
47 prologue to set up for the function. */
48 bool
49 requires_stack_frame_p (rtx_insn *insn, HARD_REG_SET prologue_used,
50 HARD_REG_SET set_up_by_prologue)
51 {
52 df_ref def, use;
53 HARD_REG_SET hardregs;
54 unsigned regno;
55
56 if (CALL_P (insn))
57 return !SIBLING_CALL_P (insn);
58
59 /* We need a frame to get the unique CFA expected by the unwinder. */
60 if (cfun->can_throw_non_call_exceptions && can_throw_internal (insn))
61 return true;
62
63 CLEAR_HARD_REG_SET (hardregs);
64 FOR_EACH_INSN_DEF (def, insn)
65 {
66 rtx dreg = DF_REF_REG (def);
67
68 if (!REG_P (dreg))
69 continue;
70
71 add_to_hard_reg_set (&hardregs, GET_MODE (dreg), REGNO (dreg));
72 }
73 if (hard_reg_set_intersect_p (hardregs, prologue_used))
74 return true;
75 AND_COMPL_HARD_REG_SET (hardregs, call_used_reg_set);
76 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
77 if (TEST_HARD_REG_BIT (hardregs, regno)
78 && df_regs_ever_live_p (regno))
79 return true;
80
81 FOR_EACH_INSN_USE (use, insn)
82 {
83 rtx reg = DF_REF_REG (use);
84
85 if (!REG_P (reg))
86 continue;
87
88 add_to_hard_reg_set (&hardregs, GET_MODE (reg),
89 REGNO (reg));
90 }
91 if (hard_reg_set_intersect_p (hardregs, set_up_by_prologue))
92 return true;
93
94 return false;
95 }
96
97 /* See whether there has a single live edge from BB, which dest uses
98 [REGNO, END_REGNO). Return the live edge if its dest bb has
99 one or two predecessors. Otherwise return NULL. */
100
101 static edge
102 live_edge_for_reg (basic_block bb, int regno, int end_regno)
103 {
104 edge e, live_edge;
105 edge_iterator ei;
106 bitmap live;
107 int i;
108
109 live_edge = NULL;
110 FOR_EACH_EDGE (e, ei, bb->succs)
111 {
112 live = df_get_live_in (e->dest);
113 for (i = regno; i < end_regno; i++)
114 if (REGNO_REG_SET_P (live, i))
115 {
116 if (live_edge && live_edge != e)
117 return NULL;
118 live_edge = e;
119 }
120 }
121
122 /* We can sometimes encounter dead code. Don't try to move it
123 into the exit block. */
124 if (!live_edge || live_edge->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
125 return NULL;
126
127 /* Reject targets of abnormal edges. This is needed for correctness
128 on ports like Alpha and MIPS, whose pic_offset_table_rtx can die on
129 exception edges even though it is generally treated as call-saved
130 for the majority of the compilation. Moving across abnormal edges
131 isn't going to be interesting for shrink-wrap usage anyway. */
132 if (live_edge->flags & EDGE_ABNORMAL)
133 return NULL;
134
135 /* When live_edge->dest->preds == 2, we can create a new block on
136 the edge to make it meet the requirement. */
137 if (EDGE_COUNT (live_edge->dest->preds) > 2)
138 return NULL;
139
140 return live_edge;
141 }
142
143 /* Try to move INSN from BB to a successor. Return true on success.
144 USES and DEFS are the set of registers that are used and defined
145 after INSN in BB. SPLIT_P indicates whether a live edge from BB
146 is splitted or not. */
147
148 static bool
149 move_insn_for_shrink_wrap (basic_block bb, rtx_insn *insn,
150 const HARD_REG_SET uses,
151 const HARD_REG_SET defs,
152 bool *split_p)
153 {
154 rtx set, src, dest;
155 bitmap live_out, live_in, bb_uses, bb_defs;
156 unsigned int i, dregno, end_dregno;
157 unsigned int sregno = FIRST_PSEUDO_REGISTER;
158 unsigned int end_sregno = FIRST_PSEUDO_REGISTER;
159 basic_block next_block;
160 edge live_edge;
161
162 /* Look for a simple register assignment. We don't use single_set here
163 because we can't deal with any CLOBBERs, USEs, or REG_UNUSED secondary
164 destinations. */
165 if (!INSN_P (insn))
166 return false;
167 set = PATTERN (insn);
168 if (GET_CODE (set) != SET)
169 return false;
170 src = SET_SRC (set);
171 dest = SET_DEST (set);
172
173 /* For the destination, we want only a register. Also disallow STACK
174 or FRAME related adjustments. They are likely part of the prologue,
175 so keep them in the entry block. */
176 if (!REG_P (dest)
177 || dest == stack_pointer_rtx
178 || dest == frame_pointer_rtx
179 || dest == hard_frame_pointer_rtx)
180 return false;
181
182 /* For the source, we want one of:
183 (1) A (non-overlapping) register
184 (2) A constant,
185 (3) An expression involving no more than one register.
186
187 That last point comes from the code following, which was originally
188 written to handle only register move operations, and still only handles
189 a single source register when checking for overlaps. Happily, the
190 same checks can be applied to expressions like (plus reg const). */
191
192 if (CONSTANT_P (src))
193 ;
194 else if (!REG_P (src))
195 {
196 rtx src_inner = NULL_RTX;
197
198 if (can_throw_internal (insn))
199 return false;
200
201 subrtx_var_iterator::array_type array;
202 FOR_EACH_SUBRTX_VAR (iter, array, src, ALL)
203 {
204 rtx x = *iter;
205 switch (GET_RTX_CLASS (GET_CODE (x)))
206 {
207 case RTX_CONST_OBJ:
208 case RTX_COMPARE:
209 case RTX_COMM_COMPARE:
210 case RTX_BIN_ARITH:
211 case RTX_COMM_ARITH:
212 case RTX_UNARY:
213 case RTX_TERNARY:
214 /* Constant or expression. Continue. */
215 break;
216
217 case RTX_OBJ:
218 case RTX_EXTRA:
219 switch (GET_CODE (x))
220 {
221 case UNSPEC:
222 case SUBREG:
223 case STRICT_LOW_PART:
224 case PC:
225 case LO_SUM:
226 /* Ok. Continue. */
227 break;
228
229 case REG:
230 /* Fail if we see a second inner register. */
231 if (src_inner != NULL)
232 return false;
233 src_inner = x;
234 break;
235
236 default:
237 return false;
238 }
239 break;
240
241 default:
242 return false;
243 }
244 }
245
246 if (src_inner != NULL)
247 src = src_inner;
248 }
249
250 /* Make sure that the source register isn't defined later in BB. */
251 if (REG_P (src))
252 {
253 sregno = REGNO (src);
254 end_sregno = END_REGNO (src);
255 if (overlaps_hard_reg_set_p (defs, GET_MODE (src), sregno))
256 return false;
257 }
258
259 /* Make sure that the destination register isn't referenced later in BB. */
260 dregno = REGNO (dest);
261 end_dregno = END_REGNO (dest);
262 if (overlaps_hard_reg_set_p (uses, GET_MODE (dest), dregno)
263 || overlaps_hard_reg_set_p (defs, GET_MODE (dest), dregno))
264 return false;
265
266 /* See whether there is a successor block to which we could move INSN. */
267 live_edge = live_edge_for_reg (bb, dregno, end_dregno);
268 if (!live_edge)
269 return false;
270
271 next_block = live_edge->dest;
272 /* Create a new basic block on the edge. */
273 if (EDGE_COUNT (next_block->preds) == 2)
274 {
275 /* split_edge for a block with only one successor is meaningless. */
276 if (EDGE_COUNT (bb->succs) == 1)
277 return false;
278
279 /* If DF_LIVE doesn't exist, i.e. at -O1, just give up. */
280 if (!df_live)
281 return false;
282
283 basic_block old_dest = live_edge->dest;
284 next_block = split_edge (live_edge);
285
286 /* We create a new basic block. Call df_grow_bb_info to make sure
287 all data structures are allocated. */
288 df_grow_bb_info (df_live);
289
290 bitmap_and (df_get_live_in (next_block), df_get_live_out (bb),
291 df_get_live_in (old_dest));
292 df_set_bb_dirty (next_block);
293
294 /* We should not split more than once for a function. */
295 if (*split_p)
296 return false;
297
298 *split_p = true;
299 }
300
301 /* At this point we are committed to moving INSN, but let's try to
302 move it as far as we can. */
303 do
304 {
305 live_out = df_get_live_out (bb);
306 live_in = df_get_live_in (next_block);
307 bb = next_block;
308
309 /* Check whether BB uses DEST or clobbers DEST. We need to add
310 INSN to BB if so. Either way, DEST is no longer live on entry,
311 except for any part that overlaps SRC (next loop). */
312 bb_uses = &DF_LR_BB_INFO (bb)->use;
313 bb_defs = &DF_LR_BB_INFO (bb)->def;
314 if (df_live)
315 {
316 for (i = dregno; i < end_dregno; i++)
317 {
318 if (*split_p
319 || REGNO_REG_SET_P (bb_uses, i)
320 || REGNO_REG_SET_P (bb_defs, i)
321 || REGNO_REG_SET_P (&DF_LIVE_BB_INFO (bb)->gen, i))
322 next_block = NULL;
323 CLEAR_REGNO_REG_SET (live_out, i);
324 CLEAR_REGNO_REG_SET (live_in, i);
325 }
326
327 /* Check whether BB clobbers SRC. We need to add INSN to BB if so.
328 Either way, SRC is now live on entry. */
329 for (i = sregno; i < end_sregno; i++)
330 {
331 if (*split_p
332 || REGNO_REG_SET_P (bb_defs, i)
333 || REGNO_REG_SET_P (&DF_LIVE_BB_INFO (bb)->gen, i))
334 next_block = NULL;
335 SET_REGNO_REG_SET (live_out, i);
336 SET_REGNO_REG_SET (live_in, i);
337 }
338 }
339 else
340 {
341 /* DF_LR_BB_INFO (bb)->def does not comprise the DF_REF_PARTIAL and
342 DF_REF_CONDITIONAL defs. So if DF_LIVE doesn't exist, i.e.
343 at -O1, just give up searching NEXT_BLOCK. */
344 next_block = NULL;
345 for (i = dregno; i < end_dregno; i++)
346 {
347 CLEAR_REGNO_REG_SET (live_out, i);
348 CLEAR_REGNO_REG_SET (live_in, i);
349 }
350
351 for (i = sregno; i < end_sregno; i++)
352 {
353 SET_REGNO_REG_SET (live_out, i);
354 SET_REGNO_REG_SET (live_in, i);
355 }
356 }
357
358 /* If we don't need to add the move to BB, look for a single
359 successor block. */
360 if (next_block)
361 {
362 live_edge = live_edge_for_reg (next_block, dregno, end_dregno);
363 if (!live_edge || EDGE_COUNT (live_edge->dest->preds) > 1)
364 break;
365 next_block = live_edge->dest;
366 }
367 }
368 while (next_block);
369
370 /* For the new created basic block, there is no dataflow info at all.
371 So skip the following dataflow update and check. */
372 if (!(*split_p))
373 {
374 /* BB now defines DEST. It only uses the parts of DEST that overlap SRC
375 (next loop). */
376 for (i = dregno; i < end_dregno; i++)
377 {
378 CLEAR_REGNO_REG_SET (bb_uses, i);
379 SET_REGNO_REG_SET (bb_defs, i);
380 }
381
382 /* BB now uses SRC. */
383 for (i = sregno; i < end_sregno; i++)
384 SET_REGNO_REG_SET (bb_uses, i);
385 }
386
387 emit_insn_after (PATTERN (insn), bb_note (bb));
388 delete_insn (insn);
389 return true;
390 }
391
392 /* Look for register copies in the first block of the function, and move
393 them down into successor blocks if the register is used only on one
394 path. This exposes more opportunities for shrink-wrapping. These
395 kinds of sets often occur when incoming argument registers are moved
396 to call-saved registers because their values are live across one or
397 more calls during the function. */
398
399 static void
400 prepare_shrink_wrap (basic_block entry_block)
401 {
402 rtx_insn *insn, *curr;
403 rtx x;
404 HARD_REG_SET uses, defs;
405 df_ref def, use;
406 bool split_p = false;
407
408 if (JUMP_P (BB_END (entry_block)))
409 {
410 /* To have more shrink-wrapping opportunities, prepare_shrink_wrap tries
411 to sink the copies from parameter to callee saved register out of
412 entry block. copyprop_hardreg_forward_bb_without_debug_insn is called
413 to release some dependences. */
414 copyprop_hardreg_forward_bb_without_debug_insn (entry_block);
415 }
416
417 CLEAR_HARD_REG_SET (uses);
418 CLEAR_HARD_REG_SET (defs);
419 FOR_BB_INSNS_REVERSE_SAFE (entry_block, insn, curr)
420 if (NONDEBUG_INSN_P (insn)
421 && !move_insn_for_shrink_wrap (entry_block, insn, uses, defs,
422 &split_p))
423 {
424 /* Add all defined registers to DEFs. */
425 FOR_EACH_INSN_DEF (def, insn)
426 {
427 x = DF_REF_REG (def);
428 if (REG_P (x) && HARD_REGISTER_P (x))
429 SET_HARD_REG_BIT (defs, REGNO (x));
430 }
431
432 /* Add all used registers to USESs. */
433 FOR_EACH_INSN_USE (use, insn)
434 {
435 x = DF_REF_REG (use);
436 if (REG_P (x) && HARD_REGISTER_P (x))
437 SET_HARD_REG_BIT (uses, REGNO (x));
438 }
439 }
440 }
441
442 /* Return whether basic block PRO can get the prologue. It can not if it
443 has incoming complex edges that need a prologue inserted (we make a new
444 block for the prologue, so those edges would need to be redirected, which
445 does not work). It also can not if there exist registers live on entry
446 to PRO that are clobbered by the prologue. */
447
448 static bool
449 can_get_prologue (basic_block pro, HARD_REG_SET prologue_clobbered)
450 {
451 edge e;
452 edge_iterator ei;
453 FOR_EACH_EDGE (e, ei, pro->preds)
454 if (e->flags & (EDGE_COMPLEX | EDGE_CROSSING)
455 && !dominated_by_p (CDI_DOMINATORS, e->src, pro))
456 return false;
457
458 HARD_REG_SET live;
459 REG_SET_TO_HARD_REG_SET (live, df_get_live_in (pro));
460 if (hard_reg_set_intersect_p (live, prologue_clobbered))
461 return false;
462
463 return true;
464 }
465
466 /* Return whether we can duplicate basic block BB for shrink wrapping. We
467 cannot if the block cannot be duplicated at all, or if any of its incoming
468 edges are complex and come from a block that does not require a prologue
469 (we cannot redirect such edges), or if the block is too big to copy.
470 PRO is the basic block before which we would put the prologue, MAX_SIZE is
471 the maximum size block we allow to be copied. */
472
473 static bool
474 can_dup_for_shrink_wrapping (basic_block bb, basic_block pro, unsigned max_size)
475 {
476 if (!can_duplicate_block_p (bb))
477 return false;
478
479 edge e;
480 edge_iterator ei;
481 FOR_EACH_EDGE (e, ei, bb->preds)
482 if (e->flags & (EDGE_COMPLEX | EDGE_CROSSING)
483 && !dominated_by_p (CDI_DOMINATORS, e->src, pro))
484 return false;
485
486 unsigned size = 0;
487
488 rtx_insn *insn;
489 FOR_BB_INSNS (bb, insn)
490 if (NONDEBUG_INSN_P (insn))
491 {
492 size += get_attr_min_length (insn);
493 if (size > max_size)
494 return false;
495 }
496
497 return true;
498 }
499
500 /* If the source of edge E has more than one successor, the verifier for
501 branch probabilities gets confused by the fake edges we make where
502 simple_return statements will be inserted later (because those are not
503 marked as fallthrough edges). Fix this by creating an extra block just
504 for that fallthrough. */
505
506 static edge
507 fix_fake_fallthrough_edge (edge e)
508 {
509 if (EDGE_COUNT (e->src->succs) <= 1)
510 return e;
511
512 basic_block old_bb = e->src;
513 rtx_insn *end = BB_END (old_bb);
514 rtx_note *note = emit_note_after (NOTE_INSN_DELETED, end);
515 basic_block new_bb = create_basic_block (note, note, old_bb);
516 BB_COPY_PARTITION (new_bb, old_bb);
517 BB_END (old_bb) = end;
518
519 redirect_edge_succ (e, new_bb);
520 e->flags |= EDGE_FALLTHRU;
521 e->flags &= ~EDGE_FAKE;
522
523 return make_edge (new_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FAKE);
524 }
525
526 /* Try to perform a kind of shrink-wrapping, making sure the
527 prologue/epilogue is emitted only around those parts of the
528 function that require it.
529
530 There will be exactly one prologue, and it will be executed either
531 zero or one time, on any path. Depending on where the prologue is
532 placed, some of the basic blocks can be reached via both paths with
533 and without a prologue. Such blocks will be duplicated here, and the
534 edges changed to match.
535
536 Paths that go to the exit without going through the prologue will use
537 a simple_return instead of the epilogue. We maximize the number of
538 those, making sure to only duplicate blocks that can be duplicated.
539 If the prologue can then still be placed in multiple locations, we
540 place it as early as possible.
541
542 An example, where we duplicate blocks with control flow (legend:
543 _B_egin, _R_eturn and _S_imple_return; edges without arrowhead should
544 be taken to point down or to the right, to simplify the diagram; here,
545 block 3 needs a prologue, the rest does not):
546
547
548 B B
549 | |
550 2 2
551 |\ |\
552 | 3 becomes | 3
553 |/ | \
554 4 7 4
555 |\ |\ |\
556 | 5 | 8 | 5
557 |/ |/ |/
558 6 9 6
559 | | |
560 R S R
561
562
563 (bb 4 is duplicated to 7, and so on; the prologue is inserted on the
564 edge 2->3).
565
566 Another example, where part of a loop is duplicated (again, bb 3 is
567 the only block that needs a prologue):
568
569
570 B 3<-- B ->3<--
571 | | | | | | |
572 | v | becomes | | v |
573 2---4--- 2---5-- 4---
574 | | |
575 R S R
576
577
578 (bb 4 is duplicated to 5; the prologue is inserted on the edge 5->3).
579
580 ENTRY_EDGE is the edge where the prologue will be placed, possibly
581 changed by this function. BB_WITH is a bitmap that, if we do shrink-
582 wrap, will on return contain the interesting blocks that run with
583 prologue. PROLOGUE_SEQ is the prologue we will insert. */
584
585 void
586 try_shrink_wrapping (edge *entry_edge, bitmap_head *bb_with,
587 rtx_insn *prologue_seq)
588 {
589 /* If we cannot shrink-wrap, are told not to shrink-wrap, or it makes
590 no sense to shrink-wrap: then do not shrink-wrap! */
591
592 if (!SHRINK_WRAPPING_ENABLED)
593 return;
594
595 if (crtl->profile && !targetm.profile_before_prologue ())
596 return;
597
598 if (crtl->calls_eh_return)
599 return;
600
601 bool empty_prologue = true;
602 for (rtx_insn *insn = prologue_seq; insn; insn = NEXT_INSN (insn))
603 if (!(NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END))
604 {
605 empty_prologue = false;
606 break;
607 }
608 if (empty_prologue)
609 return;
610
611 /* Move some code down to expose more shrink-wrapping opportunities. */
612
613 basic_block entry = (*entry_edge)->dest;
614 prepare_shrink_wrap (entry);
615
616 if (dump_file)
617 fprintf (dump_file, "Attempting shrink-wrapping optimization.\n");
618
619 /* Compute the registers set and used in the prologue. */
620
621 HARD_REG_SET prologue_clobbered, prologue_used;
622 CLEAR_HARD_REG_SET (prologue_clobbered);
623 CLEAR_HARD_REG_SET (prologue_used);
624 for (rtx_insn *insn = prologue_seq; insn; insn = NEXT_INSN (insn))
625 if (NONDEBUG_INSN_P (insn))
626 {
627 HARD_REG_SET this_used;
628 CLEAR_HARD_REG_SET (this_used);
629 note_uses (&PATTERN (insn), record_hard_reg_uses, &this_used);
630 AND_COMPL_HARD_REG_SET (this_used, prologue_clobbered);
631 IOR_HARD_REG_SET (prologue_used, this_used);
632 note_stores (PATTERN (insn), record_hard_reg_sets, &prologue_clobbered);
633 }
634 CLEAR_HARD_REG_BIT (prologue_clobbered, STACK_POINTER_REGNUM);
635 if (frame_pointer_needed)
636 CLEAR_HARD_REG_BIT (prologue_clobbered, HARD_FRAME_POINTER_REGNUM);
637
638 /* Find out what registers are set up by the prologue; any use of these
639 cannot happen before the prologue. */
640
641 struct hard_reg_set_container set_up_by_prologue;
642 CLEAR_HARD_REG_SET (set_up_by_prologue.set);
643 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode, STACK_POINTER_REGNUM);
644 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode, ARG_POINTER_REGNUM);
645 if (frame_pointer_needed)
646 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
647 HARD_FRAME_POINTER_REGNUM);
648 if (pic_offset_table_rtx
649 && (unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
650 add_to_hard_reg_set (&set_up_by_prologue.set, Pmode,
651 PIC_OFFSET_TABLE_REGNUM);
652 if (crtl->drap_reg)
653 add_to_hard_reg_set (&set_up_by_prologue.set,
654 GET_MODE (crtl->drap_reg),
655 REGNO (crtl->drap_reg));
656 if (targetm.set_up_by_prologue)
657 targetm.set_up_by_prologue (&set_up_by_prologue);
658
659 /* We will insert the prologue before the basic block PRO. PRO should
660 dominate all basic blocks that need the prologue to be executed
661 before them. First, make PRO the "tightest wrap" possible. */
662
663 calculate_dominance_info (CDI_DOMINATORS);
664
665 basic_block pro = 0;
666
667 basic_block bb;
668 edge e;
669 edge_iterator ei;
670 FOR_EACH_BB_FN (bb, cfun)
671 {
672 rtx_insn *insn;
673 FOR_BB_INSNS (bb, insn)
674 if (NONDEBUG_INSN_P (insn)
675 && requires_stack_frame_p (insn, prologue_used,
676 set_up_by_prologue.set))
677 {
678 if (dump_file)
679 fprintf (dump_file, "Block %d needs the prologue.\n", bb->index);
680 pro = nearest_common_dominator (CDI_DOMINATORS, pro, bb);
681 break;
682 }
683 }
684
685 /* If nothing needs a prologue, just put it at the start. This really
686 shouldn't happen, but we cannot fix it here. */
687
688 if (pro == 0)
689 {
690 if (dump_file)
691 fprintf(dump_file, "Nothing needs a prologue, but it isn't empty; "
692 "putting it at the start.\n");
693 pro = entry;
694 }
695
696 if (dump_file)
697 fprintf (dump_file, "After wrapping required blocks, PRO is now %d\n",
698 pro->index);
699
700 /* Now see if we can put the prologue at the start of PRO. Putting it
701 there might require duplicating a block that cannot be duplicated,
702 or in some cases we cannot insert the prologue there at all. If PRO
703 wont't do, try again with the immediate dominator of PRO, and so on.
704
705 The blocks that need duplicating are those reachable from PRO but
706 not dominated by it. We keep in BB_WITH a bitmap of the blocks
707 reachable from PRO that we already found, and in VEC a stack of
708 those we still need to consider (to find successors). */
709
710 bitmap_set_bit (bb_with, pro->index);
711
712 vec<basic_block> vec;
713 vec.create (n_basic_blocks_for_fn (cfun));
714 vec.quick_push (pro);
715
716 unsigned max_grow_size = get_uncond_jump_length ();
717 max_grow_size *= PARAM_VALUE (PARAM_MAX_GROW_COPY_BB_INSNS);
718
719 while (!vec.is_empty () && pro != entry)
720 {
721 while (pro != entry && !can_get_prologue (pro, prologue_clobbered))
722 {
723 pro = get_immediate_dominator (CDI_DOMINATORS, pro);
724
725 if (bitmap_set_bit (bb_with, pro->index))
726 vec.quick_push (pro);
727 }
728
729 basic_block bb = vec.pop ();
730 if (!can_dup_for_shrink_wrapping (bb, pro, max_grow_size))
731 while (!dominated_by_p (CDI_DOMINATORS, bb, pro))
732 {
733 gcc_assert (pro != entry);
734
735 pro = get_immediate_dominator (CDI_DOMINATORS, pro);
736
737 if (bitmap_set_bit (bb_with, pro->index))
738 vec.quick_push (pro);
739 }
740
741 FOR_EACH_EDGE (e, ei, bb->succs)
742 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
743 && bitmap_set_bit (bb_with, e->dest->index))
744 vec.quick_push (e->dest);
745 }
746
747 if (dump_file)
748 fprintf (dump_file, "Avoiding non-duplicatable blocks, PRO is now %d\n",
749 pro->index);
750
751 /* If we can move PRO back without having to duplicate more blocks, do so.
752 We do this because putting the prologue earlier is better for scheduling.
753 We can move back to a block PRE if every path from PRE will eventually
754 need a prologue, that is, PRO is a post-dominator of PRE. PRE needs
755 to dominate every block reachable from itself. */
756
757 if (pro != entry)
758 {
759 calculate_dominance_info (CDI_POST_DOMINATORS);
760
761 bitmap bb_tmp = BITMAP_ALLOC (NULL);
762 bitmap_copy (bb_tmp, bb_with);
763 basic_block last_ok = pro;
764 vec.truncate (0);
765
766 while (pro != entry)
767 {
768 basic_block pre = get_immediate_dominator (CDI_DOMINATORS, pro);
769 if (!dominated_by_p (CDI_POST_DOMINATORS, pre, pro))
770 break;
771
772 if (bitmap_set_bit (bb_tmp, pre->index))
773 vec.quick_push (pre);
774
775 bool ok = true;
776 while (!vec.is_empty ())
777 {
778 basic_block bb = vec.pop ();
779 bitmap_set_bit (bb_tmp, pre->index);
780
781 if (!dominated_by_p (CDI_DOMINATORS, bb, pre))
782 {
783 ok = false;
784 break;
785 }
786
787 FOR_EACH_EDGE (e, ei, bb->succs)
788 if (!bitmap_bit_p (bb_with, e->dest->index)
789 && bitmap_set_bit (bb_tmp, e->dest->index))
790 vec.quick_push (e->dest);
791 }
792
793 if (ok && can_get_prologue (pre, prologue_clobbered))
794 last_ok = pre;
795
796 pro = pre;
797 }
798
799 pro = last_ok;
800
801 BITMAP_FREE (bb_tmp);
802 free_dominance_info (CDI_POST_DOMINATORS);
803 }
804
805 vec.release ();
806
807 if (dump_file)
808 fprintf (dump_file, "Bumping back to anticipatable blocks, PRO is now %d\n",
809 pro->index);
810
811 if (pro == entry)
812 {
813 free_dominance_info (CDI_DOMINATORS);
814 return;
815 }
816
817 /* Compute what fraction of the frequency and count of the blocks that run
818 both with and without prologue are for running with prologue. This gives
819 the correct answer for reducible flow graphs; for irreducible flow graphs
820 our profile is messed up beyond repair anyway. */
821
822 gcov_type num = 0;
823 gcov_type den = 0;
824
825 FOR_EACH_EDGE (e, ei, pro->preds)
826 if (!dominated_by_p (CDI_DOMINATORS, e->src, pro))
827 {
828 num += EDGE_FREQUENCY (e);
829 den += e->src->frequency;
830 }
831
832 if (den == 0)
833 den = 1;
834
835 /* All is okay, so do it. */
836
837 crtl->shrink_wrapped = true;
838 if (dump_file)
839 fprintf (dump_file, "Performing shrink-wrapping.\n");
840
841 /* Copy the blocks that can run both with and without prologue. The
842 originals run with prologue, the copies without. Store a pointer to
843 the copy in the ->aux field of the original. */
844
845 FOR_EACH_BB_FN (bb, cfun)
846 if (bitmap_bit_p (bb_with, bb->index)
847 && !dominated_by_p (CDI_DOMINATORS, bb, pro))
848 {
849 basic_block dup = duplicate_block (bb, 0, 0);
850
851 bb->aux = dup;
852
853 if (JUMP_P (BB_END (dup)) && !any_condjump_p (BB_END (dup)))
854 emit_barrier_after_bb (dup);
855
856 if (EDGE_COUNT (dup->succs) == 0)
857 emit_barrier_after_bb (dup);
858
859 if (dump_file)
860 fprintf (dump_file, "Duplicated %d to %d\n", bb->index, dup->index);
861
862 bb->frequency = RDIV (num * bb->frequency, den);
863 dup->frequency -= bb->frequency;
864 bb->count = RDIV (num * bb->count, den);
865 dup->count -= bb->count;
866 }
867
868 /* Now change the edges to point to the copies, where appropriate. */
869
870 FOR_EACH_BB_FN (bb, cfun)
871 if (!dominated_by_p (CDI_DOMINATORS, bb, pro))
872 {
873 basic_block src = bb;
874 if (bitmap_bit_p (bb_with, bb->index))
875 src = (basic_block) bb->aux;
876
877 FOR_EACH_EDGE (e, ei, src->succs)
878 {
879 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
880 continue;
881
882 if (bitmap_bit_p (bb_with, e->dest->index)
883 && !dominated_by_p (CDI_DOMINATORS, e->dest, pro))
884 {
885 if (dump_file)
886 fprintf (dump_file, "Redirecting edge %d->%d to %d\n",
887 e->src->index, e->dest->index,
888 ((basic_block) e->dest->aux)->index);
889 redirect_edge_and_branch_force (e, (basic_block) e->dest->aux);
890 }
891 else if (e->flags & EDGE_FALLTHRU
892 && bitmap_bit_p (bb_with, bb->index))
893 force_nonfallthru (e);
894 }
895 }
896
897 /* Also redirect the function entry edge if necessary. */
898
899 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
900 if (bitmap_bit_p (bb_with, e->dest->index)
901 && !dominated_by_p (CDI_DOMINATORS, e->dest, pro))
902 {
903 basic_block split_bb = split_edge (e);
904 e = single_succ_edge (split_bb);
905 redirect_edge_and_branch_force (e, (basic_block) e->dest->aux);
906 }
907
908 /* Change all the exits that should get a simple_return to FAKE.
909 They will be converted later. */
910
911 FOR_EACH_BB_FN (bb, cfun)
912 if (!bitmap_bit_p (bb_with, bb->index))
913 FOR_EACH_EDGE (e, ei, bb->succs)
914 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
915 {
916 e = fix_fake_fallthrough_edge (e);
917
918 e->flags &= ~EDGE_FALLTHRU;
919 if (!(e->flags & EDGE_SIBCALL))
920 e->flags |= EDGE_FAKE;
921
922 emit_barrier_after_bb (e->src);
923 }
924
925 /* Finally, we want a single edge to put the prologue on. Make a new
926 block before the PRO block; the edge beteen them is the edge we want.
927 Then redirect those edges into PRO that come from blocks without the
928 prologue, to point to the new block instead. The new prologue block
929 is put at the end of the insn chain. */
930
931 basic_block new_bb = create_empty_bb (EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb);
932 BB_COPY_PARTITION (new_bb, pro);
933 if (dump_file)
934 fprintf (dump_file, "Made prologue block %d\n", new_bb->index);
935
936 for (ei = ei_start (pro->preds); (e = ei_safe_edge (ei)); )
937 {
938 if (bitmap_bit_p (bb_with, e->src->index)
939 || dominated_by_p (CDI_DOMINATORS, e->src, pro))
940 {
941 ei_next (&ei);
942 continue;
943 }
944
945 new_bb->count += RDIV (e->src->count * e->probability, REG_BR_PROB_BASE);
946 new_bb->frequency += EDGE_FREQUENCY (e);
947
948 redirect_edge_and_branch_force (e, new_bb);
949 if (dump_file)
950 fprintf (dump_file, "Redirected edge from %d\n", e->src->index);
951 }
952
953 *entry_edge = make_single_succ_edge (new_bb, pro, EDGE_FALLTHRU);
954 force_nonfallthru (*entry_edge);
955
956 free_dominance_info (CDI_DOMINATORS);
957 }
958
959 /* If we're allowed to generate a simple return instruction, then by
960 definition we don't need a full epilogue. If the last basic
961 block before the exit block does not contain active instructions,
962 examine its predecessors and try to emit (conditional) return
963 instructions. */
964
965 edge
966 get_unconverted_simple_return (edge exit_fallthru_edge, bitmap_head bb_flags,
967 vec<edge> *unconverted_simple_returns,
968 rtx_insn **returnjump)
969 {
970 if (optimize)
971 {
972 unsigned i, last;
973
974 /* convert_jumps_to_returns may add to preds of the exit block
975 (but won't remove). Stop at end of current preds. */
976 last = EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
977 for (i = 0; i < last; i++)
978 {
979 edge e = EDGE_I (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds, i);
980 if (LABEL_P (BB_HEAD (e->src))
981 && !bitmap_bit_p (&bb_flags, e->src->index)
982 && !active_insn_between (BB_HEAD (e->src), BB_END (e->src)))
983 *unconverted_simple_returns
984 = convert_jumps_to_returns (e->src, true,
985 *unconverted_simple_returns);
986 }
987 }
988
989 if (exit_fallthru_edge != NULL
990 && EDGE_COUNT (exit_fallthru_edge->src->preds) != 0
991 && !bitmap_bit_p (&bb_flags, exit_fallthru_edge->src->index))
992 {
993 basic_block last_bb;
994
995 last_bb = emit_return_for_exit (exit_fallthru_edge, true);
996 *returnjump = BB_END (last_bb);
997 exit_fallthru_edge = NULL;
998 }
999 return exit_fallthru_edge;
1000 }
1001
1002 /* If there were branches to an empty LAST_BB which we tried to
1003 convert to conditional simple_returns, but couldn't for some
1004 reason, create a block to hold a simple_return insn and redirect
1005 those remaining edges. */
1006
1007 void
1008 convert_to_simple_return (edge entry_edge, edge orig_entry_edge,
1009 bitmap_head bb_flags, rtx_insn *returnjump,
1010 vec<edge> unconverted_simple_returns)
1011 {
1012 edge e;
1013 edge_iterator ei;
1014
1015 if (!unconverted_simple_returns.is_empty ())
1016 {
1017 basic_block simple_return_block_hot = NULL;
1018 basic_block simple_return_block_cold = NULL;
1019 edge pending_edge_hot = NULL;
1020 edge pending_edge_cold = NULL;
1021 basic_block exit_pred;
1022 int i;
1023
1024 gcc_assert (entry_edge != orig_entry_edge);
1025
1026 /* See if we can reuse the last insn that was emitted for the
1027 epilogue. */
1028 if (returnjump != NULL_RTX
1029 && JUMP_LABEL (returnjump) == simple_return_rtx)
1030 {
1031 e = split_block (BLOCK_FOR_INSN (returnjump), PREV_INSN (returnjump));
1032 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
1033 simple_return_block_hot = e->dest;
1034 else
1035 simple_return_block_cold = e->dest;
1036 }
1037
1038 /* Also check returns we might need to add to tail blocks. */
1039 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
1040 if (EDGE_COUNT (e->src->preds) != 0
1041 && (e->flags & EDGE_FAKE) != 0
1042 && !bitmap_bit_p (&bb_flags, e->src->index))
1043 {
1044 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
1045 pending_edge_hot = e;
1046 else
1047 pending_edge_cold = e;
1048 }
1049
1050 /* Save a pointer to the exit's predecessor BB for use in
1051 inserting new BBs at the end of the function. Do this
1052 after the call to split_block above which may split
1053 the original exit pred. */
1054 exit_pred = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
1055
1056 FOR_EACH_VEC_ELT (unconverted_simple_returns, i, e)
1057 {
1058 basic_block *pdest_bb;
1059 edge pending;
1060
1061 if (BB_PARTITION (e->src) == BB_HOT_PARTITION)
1062 {
1063 pdest_bb = &simple_return_block_hot;
1064 pending = pending_edge_hot;
1065 }
1066 else
1067 {
1068 pdest_bb = &simple_return_block_cold;
1069 pending = pending_edge_cold;
1070 }
1071
1072 if (*pdest_bb == NULL && pending != NULL)
1073 {
1074 emit_return_into_block (true, pending->src);
1075 pending->flags &= ~(EDGE_FALLTHRU | EDGE_FAKE);
1076 *pdest_bb = pending->src;
1077 }
1078 else if (*pdest_bb == NULL)
1079 {
1080 basic_block bb;
1081
1082 bb = create_basic_block (NULL, NULL, exit_pred);
1083 BB_COPY_PARTITION (bb, e->src);
1084 rtx_insn *ret = targetm.gen_simple_return ();
1085 rtx_jump_insn *start = emit_jump_insn_after (ret, BB_END (bb));
1086 JUMP_LABEL (start) = simple_return_rtx;
1087 emit_barrier_after (start);
1088
1089 *pdest_bb = bb;
1090 make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1091 }
1092 redirect_edge_and_branch_force (e, *pdest_bb);
1093 }
1094 unconverted_simple_returns.release ();
1095 }
1096
1097 if (entry_edge != orig_entry_edge)
1098 {
1099 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
1100 if (EDGE_COUNT (e->src->preds) != 0
1101 && (e->flags & EDGE_FAKE) != 0
1102 && !bitmap_bit_p (&bb_flags, e->src->index))
1103 {
1104 e = fix_fake_fallthrough_edge (e);
1105
1106 emit_return_into_block (true, e->src);
1107 e->flags &= ~(EDGE_FALLTHRU | EDGE_FAKE);
1108 }
1109 }
1110 }