recog.c (split_all_insns): Remove dead code.
[official-gcc.git] / gcc / cfgcleanup.c
blob881a4f34d184fc97a456bbfcfc04dc7fb5e4d538
1 /* Control flow optimization code for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This file contains optimizer of the control flow. The main entry point is
23 cleanup_cfg. Following optimizations are performed:
25 - Unreachable blocks removal
26 - Edge forwarding (edge to the forwarder block is forwarded to its
27 successor. Simplification of the branch instruction is performed by
28 underlying infrastructure so branch can be converted to simplejump or
29 eliminated).
30 - Cross jumping (tail merging)
31 - Conditional jump-around-simplejump simplification
32 - Basic block merging. */
34 #include "config.h"
35 #include "system.h"
36 #include "coretypes.h"
37 #include "tm.h"
38 #include "rtl.h"
39 #include "hard-reg-set.h"
40 #include "regs.h"
41 #include "timevar.h"
42 #include "output.h"
43 #include "insn-config.h"
44 #include "flags.h"
45 #include "recog.h"
46 #include "diagnostic-core.h"
47 #include "toplev.h"
48 #include "cselib.h"
49 #include "params.h"
50 #include "tm_p.h"
51 #include "target.h"
52 #include "cfglayout.h"
53 #include "emit-rtl.h"
54 #include "tree-pass.h"
55 #include "cfgloop.h"
56 #include "expr.h"
57 #include "df.h"
58 #include "dce.h"
59 #include "dbgcnt.h"
61 #define FORWARDER_BLOCK_P(BB) ((BB)->flags & BB_FORWARDER_BLOCK)
63 /* Set to true when we are running first pass of try_optimize_cfg loop. */
64 static bool first_pass;
66 /* Set to true if crossjumps occured in the latest run of try_optimize_cfg. */
67 static bool crossjumps_occured;
69 /* Set to true if we couldn't run an optimization due to stale liveness
70 information; we should run df_analyze to enable more opportunities. */
71 static bool block_was_dirty;
73 static bool try_crossjump_to_edge (int, edge, edge);
74 static bool try_crossjump_bb (int, basic_block);
75 static bool outgoing_edges_match (int, basic_block, basic_block);
76 static bool old_insns_match_p (int, rtx, rtx);
78 static void merge_blocks_move_predecessor_nojumps (basic_block, basic_block);
79 static void merge_blocks_move_successor_nojumps (basic_block, basic_block);
80 static bool try_optimize_cfg (int);
81 static bool try_simplify_condjump (basic_block);
82 static bool try_forward_edges (int, basic_block);
83 static edge thread_jump (edge, basic_block);
84 static bool mark_effect (rtx, bitmap);
85 static void notice_new_block (basic_block);
86 static void update_forwarder_flag (basic_block);
87 static int mentions_nonequal_regs (rtx *, void *);
88 static void merge_memattrs (rtx, rtx);
90 /* Set flags for newly created block. */
92 static void
93 notice_new_block (basic_block bb)
95 if (!bb)
96 return;
98 if (forwarder_block_p (bb))
99 bb->flags |= BB_FORWARDER_BLOCK;
102 /* Recompute forwarder flag after block has been modified. */
104 static void
105 update_forwarder_flag (basic_block bb)
107 if (forwarder_block_p (bb))
108 bb->flags |= BB_FORWARDER_BLOCK;
109 else
110 bb->flags &= ~BB_FORWARDER_BLOCK;
113 /* Simplify a conditional jump around an unconditional jump.
114 Return true if something changed. */
116 static bool
117 try_simplify_condjump (basic_block cbranch_block)
119 basic_block jump_block, jump_dest_block, cbranch_dest_block;
120 edge cbranch_jump_edge, cbranch_fallthru_edge;
121 rtx cbranch_insn;
123 /* Verify that there are exactly two successors. */
124 if (EDGE_COUNT (cbranch_block->succs) != 2)
125 return false;
127 /* Verify that we've got a normal conditional branch at the end
128 of the block. */
129 cbranch_insn = BB_END (cbranch_block);
130 if (!any_condjump_p (cbranch_insn))
131 return false;
133 cbranch_fallthru_edge = FALLTHRU_EDGE (cbranch_block);
134 cbranch_jump_edge = BRANCH_EDGE (cbranch_block);
136 /* The next block must not have multiple predecessors, must not
137 be the last block in the function, and must contain just the
138 unconditional jump. */
139 jump_block = cbranch_fallthru_edge->dest;
140 if (!single_pred_p (jump_block)
141 || jump_block->next_bb == EXIT_BLOCK_PTR
142 || !FORWARDER_BLOCK_P (jump_block))
143 return false;
144 jump_dest_block = single_succ (jump_block);
146 /* If we are partitioning hot/cold basic blocks, we don't want to
147 mess up unconditional or indirect jumps that cross between hot
148 and cold sections.
150 Basic block partitioning may result in some jumps that appear to
151 be optimizable (or blocks that appear to be mergeable), but which really
152 must be left untouched (they are required to make it safely across
153 partition boundaries). See the comments at the top of
154 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
156 if (BB_PARTITION (jump_block) != BB_PARTITION (jump_dest_block)
157 || (cbranch_jump_edge->flags & EDGE_CROSSING))
158 return false;
160 /* The conditional branch must target the block after the
161 unconditional branch. */
162 cbranch_dest_block = cbranch_jump_edge->dest;
164 if (cbranch_dest_block == EXIT_BLOCK_PTR
165 || !can_fallthru (jump_block, cbranch_dest_block))
166 return false;
168 /* Invert the conditional branch. */
169 if (!invert_jump (cbranch_insn, block_label (jump_dest_block), 0))
170 return false;
172 if (dump_file)
173 fprintf (dump_file, "Simplifying condjump %i around jump %i\n",
174 INSN_UID (cbranch_insn), INSN_UID (BB_END (jump_block)));
176 /* Success. Update the CFG to match. Note that after this point
177 the edge variable names appear backwards; the redirection is done
178 this way to preserve edge profile data. */
179 cbranch_jump_edge = redirect_edge_succ_nodup (cbranch_jump_edge,
180 cbranch_dest_block);
181 cbranch_fallthru_edge = redirect_edge_succ_nodup (cbranch_fallthru_edge,
182 jump_dest_block);
183 cbranch_jump_edge->flags |= EDGE_FALLTHRU;
184 cbranch_fallthru_edge->flags &= ~EDGE_FALLTHRU;
185 update_br_prob_note (cbranch_block);
187 /* Delete the block with the unconditional jump, and clean up the mess. */
188 delete_basic_block (jump_block);
189 tidy_fallthru_edge (cbranch_jump_edge);
190 update_forwarder_flag (cbranch_block);
192 return true;
195 /* Attempt to prove that operation is NOOP using CSElib or mark the effect
196 on register. Used by jump threading. */
198 static bool
199 mark_effect (rtx exp, regset nonequal)
201 int regno;
202 rtx dest;
203 switch (GET_CODE (exp))
205 /* In case we do clobber the register, mark it as equal, as we know the
206 value is dead so it don't have to match. */
207 case CLOBBER:
208 if (REG_P (XEXP (exp, 0)))
210 dest = XEXP (exp, 0);
211 regno = REGNO (dest);
212 CLEAR_REGNO_REG_SET (nonequal, regno);
213 if (regno < FIRST_PSEUDO_REGISTER)
215 int n = hard_regno_nregs[regno][GET_MODE (dest)];
216 while (--n > 0)
217 CLEAR_REGNO_REG_SET (nonequal, regno + n);
220 return false;
222 case SET:
223 if (rtx_equal_for_cselib_p (SET_DEST (exp), SET_SRC (exp)))
224 return false;
225 dest = SET_DEST (exp);
226 if (dest == pc_rtx)
227 return false;
228 if (!REG_P (dest))
229 return true;
230 regno = REGNO (dest);
231 SET_REGNO_REG_SET (nonequal, regno);
232 if (regno < FIRST_PSEUDO_REGISTER)
234 int n = hard_regno_nregs[regno][GET_MODE (dest)];
235 while (--n > 0)
236 SET_REGNO_REG_SET (nonequal, regno + n);
238 return false;
240 default:
241 return false;
245 /* Return nonzero if X is a register set in regset DATA.
246 Called via for_each_rtx. */
247 static int
248 mentions_nonequal_regs (rtx *x, void *data)
250 regset nonequal = (regset) data;
251 if (REG_P (*x))
253 int regno;
255 regno = REGNO (*x);
256 if (REGNO_REG_SET_P (nonequal, regno))
257 return 1;
258 if (regno < FIRST_PSEUDO_REGISTER)
260 int n = hard_regno_nregs[regno][GET_MODE (*x)];
261 while (--n > 0)
262 if (REGNO_REG_SET_P (nonequal, regno + n))
263 return 1;
266 return 0;
268 /* Attempt to prove that the basic block B will have no side effects and
269 always continues in the same edge if reached via E. Return the edge
270 if exist, NULL otherwise. */
272 static edge
273 thread_jump (edge e, basic_block b)
275 rtx set1, set2, cond1, cond2, insn;
276 enum rtx_code code1, code2, reversed_code2;
277 bool reverse1 = false;
278 unsigned i;
279 regset nonequal;
280 bool failed = false;
281 reg_set_iterator rsi;
283 if (b->flags & BB_NONTHREADABLE_BLOCK)
284 return NULL;
286 /* At the moment, we do handle only conditional jumps, but later we may
287 want to extend this code to tablejumps and others. */
288 if (EDGE_COUNT (e->src->succs) != 2)
289 return NULL;
290 if (EDGE_COUNT (b->succs) != 2)
292 b->flags |= BB_NONTHREADABLE_BLOCK;
293 return NULL;
296 /* Second branch must end with onlyjump, as we will eliminate the jump. */
297 if (!any_condjump_p (BB_END (e->src)))
298 return NULL;
300 if (!any_condjump_p (BB_END (b)) || !onlyjump_p (BB_END (b)))
302 b->flags |= BB_NONTHREADABLE_BLOCK;
303 return NULL;
306 set1 = pc_set (BB_END (e->src));
307 set2 = pc_set (BB_END (b));
308 if (((e->flags & EDGE_FALLTHRU) != 0)
309 != (XEXP (SET_SRC (set1), 1) == pc_rtx))
310 reverse1 = true;
312 cond1 = XEXP (SET_SRC (set1), 0);
313 cond2 = XEXP (SET_SRC (set2), 0);
314 if (reverse1)
315 code1 = reversed_comparison_code (cond1, BB_END (e->src));
316 else
317 code1 = GET_CODE (cond1);
319 code2 = GET_CODE (cond2);
320 reversed_code2 = reversed_comparison_code (cond2, BB_END (b));
322 if (!comparison_dominates_p (code1, code2)
323 && !comparison_dominates_p (code1, reversed_code2))
324 return NULL;
326 /* Ensure that the comparison operators are equivalent.
327 ??? This is far too pessimistic. We should allow swapped operands,
328 different CCmodes, or for example comparisons for interval, that
329 dominate even when operands are not equivalent. */
330 if (!rtx_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
331 || !rtx_equal_p (XEXP (cond1, 1), XEXP (cond2, 1)))
332 return NULL;
334 /* Short circuit cases where block B contains some side effects, as we can't
335 safely bypass it. */
336 for (insn = NEXT_INSN (BB_HEAD (b)); insn != NEXT_INSN (BB_END (b));
337 insn = NEXT_INSN (insn))
338 if (INSN_P (insn) && side_effects_p (PATTERN (insn)))
340 b->flags |= BB_NONTHREADABLE_BLOCK;
341 return NULL;
344 cselib_init (0);
346 /* First process all values computed in the source basic block. */
347 for (insn = NEXT_INSN (BB_HEAD (e->src));
348 insn != NEXT_INSN (BB_END (e->src));
349 insn = NEXT_INSN (insn))
350 if (INSN_P (insn))
351 cselib_process_insn (insn);
353 nonequal = BITMAP_ALLOC (NULL);
354 CLEAR_REG_SET (nonequal);
356 /* Now assume that we've continued by the edge E to B and continue
357 processing as if it were same basic block.
358 Our goal is to prove that whole block is an NOOP. */
360 for (insn = NEXT_INSN (BB_HEAD (b));
361 insn != NEXT_INSN (BB_END (b)) && !failed;
362 insn = NEXT_INSN (insn))
364 if (INSN_P (insn))
366 rtx pat = PATTERN (insn);
368 if (GET_CODE (pat) == PARALLEL)
370 for (i = 0; i < (unsigned)XVECLEN (pat, 0); i++)
371 failed |= mark_effect (XVECEXP (pat, 0, i), nonequal);
373 else
374 failed |= mark_effect (pat, nonequal);
377 cselib_process_insn (insn);
380 /* Later we should clear nonequal of dead registers. So far we don't
381 have life information in cfg_cleanup. */
382 if (failed)
384 b->flags |= BB_NONTHREADABLE_BLOCK;
385 goto failed_exit;
388 /* cond2 must not mention any register that is not equal to the
389 former block. */
390 if (for_each_rtx (&cond2, mentions_nonequal_regs, nonequal))
391 goto failed_exit;
393 EXECUTE_IF_SET_IN_REG_SET (nonequal, 0, i, rsi)
394 goto failed_exit;
396 BITMAP_FREE (nonequal);
397 cselib_finish ();
398 if ((comparison_dominates_p (code1, code2) != 0)
399 != (XEXP (SET_SRC (set2), 1) == pc_rtx))
400 return BRANCH_EDGE (b);
401 else
402 return FALLTHRU_EDGE (b);
404 failed_exit:
405 BITMAP_FREE (nonequal);
406 cselib_finish ();
407 return NULL;
410 /* Attempt to forward edges leaving basic block B.
411 Return true if successful. */
413 static bool
414 try_forward_edges (int mode, basic_block b)
416 bool changed = false;
417 edge_iterator ei;
418 edge e, *threaded_edges = NULL;
420 /* If we are partitioning hot/cold basic blocks, we don't want to
421 mess up unconditional or indirect jumps that cross between hot
422 and cold sections.
424 Basic block partitioning may result in some jumps that appear to
425 be optimizable (or blocks that appear to be mergeable), but which really
426 must be left untouched (they are required to make it safely across
427 partition boundaries). See the comments at the top of
428 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
430 if (find_reg_note (BB_END (b), REG_CROSSING_JUMP, NULL_RTX))
431 return false;
433 for (ei = ei_start (b->succs); (e = ei_safe_edge (ei)); )
435 basic_block target, first;
436 int counter, goto_locus;
437 bool threaded = false;
438 int nthreaded_edges = 0;
439 bool may_thread = first_pass || (b->flags & BB_MODIFIED) != 0;
441 /* Skip complex edges because we don't know how to update them.
443 Still handle fallthru edges, as we can succeed to forward fallthru
444 edge to the same place as the branch edge of conditional branch
445 and turn conditional branch to an unconditional branch. */
446 if (e->flags & EDGE_COMPLEX)
448 ei_next (&ei);
449 continue;
452 target = first = e->dest;
453 counter = NUM_FIXED_BLOCKS;
454 goto_locus = e->goto_locus;
456 /* If we are partitioning hot/cold basic_blocks, we don't want to mess
457 up jumps that cross between hot/cold sections.
459 Basic block partitioning may result in some jumps that appear
460 to be optimizable (or blocks that appear to be mergeable), but which
461 really must be left untouched (they are required to make it safely
462 across partition boundaries). See the comments at the top of
463 bb-reorder.c:partition_hot_cold_basic_blocks for complete
464 details. */
466 if (first != EXIT_BLOCK_PTR
467 && find_reg_note (BB_END (first), REG_CROSSING_JUMP, NULL_RTX))
468 return false;
470 while (counter < n_basic_blocks)
472 basic_block new_target = NULL;
473 bool new_target_threaded = false;
474 may_thread |= (target->flags & BB_MODIFIED) != 0;
476 if (FORWARDER_BLOCK_P (target)
477 && !(single_succ_edge (target)->flags & EDGE_CROSSING)
478 && single_succ (target) != EXIT_BLOCK_PTR)
480 /* Bypass trivial infinite loops. */
481 new_target = single_succ (target);
482 if (target == new_target)
483 counter = n_basic_blocks;
484 else if (!optimize)
486 /* When not optimizing, ensure that edges or forwarder
487 blocks with different locus are not optimized out. */
488 int new_locus = single_succ_edge (target)->goto_locus;
489 int locus = goto_locus;
491 if (new_locus && locus && !locator_eq (new_locus, locus))
492 new_target = NULL;
493 else
495 if (new_locus)
496 locus = new_locus;
498 new_locus = INSN_P (BB_END (target))
499 ? INSN_LOCATOR (BB_END (target)) : 0;
501 if (new_locus && locus && !locator_eq (new_locus, locus))
502 new_target = NULL;
503 else
505 if (new_locus)
506 locus = new_locus;
508 goto_locus = locus;
514 /* Allow to thread only over one edge at time to simplify updating
515 of probabilities. */
516 else if ((mode & CLEANUP_THREADING) && may_thread)
518 edge t = thread_jump (e, target);
519 if (t)
521 if (!threaded_edges)
522 threaded_edges = XNEWVEC (edge, n_basic_blocks);
523 else
525 int i;
527 /* Detect an infinite loop across blocks not
528 including the start block. */
529 for (i = 0; i < nthreaded_edges; ++i)
530 if (threaded_edges[i] == t)
531 break;
532 if (i < nthreaded_edges)
534 counter = n_basic_blocks;
535 break;
539 /* Detect an infinite loop across the start block. */
540 if (t->dest == b)
541 break;
543 gcc_assert (nthreaded_edges < n_basic_blocks - NUM_FIXED_BLOCKS);
544 threaded_edges[nthreaded_edges++] = t;
546 new_target = t->dest;
547 new_target_threaded = true;
551 if (!new_target)
552 break;
554 counter++;
555 target = new_target;
556 threaded |= new_target_threaded;
559 if (counter >= n_basic_blocks)
561 if (dump_file)
562 fprintf (dump_file, "Infinite loop in BB %i.\n",
563 target->index);
565 else if (target == first)
566 ; /* We didn't do anything. */
567 else
569 /* Save the values now, as the edge may get removed. */
570 gcov_type edge_count = e->count;
571 int edge_probability = e->probability;
572 int edge_frequency;
573 int n = 0;
575 e->goto_locus = goto_locus;
577 /* Don't force if target is exit block. */
578 if (threaded && target != EXIT_BLOCK_PTR)
580 notice_new_block (redirect_edge_and_branch_force (e, target));
581 if (dump_file)
582 fprintf (dump_file, "Conditionals threaded.\n");
584 else if (!redirect_edge_and_branch (e, target))
586 if (dump_file)
587 fprintf (dump_file,
588 "Forwarding edge %i->%i to %i failed.\n",
589 b->index, e->dest->index, target->index);
590 ei_next (&ei);
591 continue;
594 /* We successfully forwarded the edge. Now update profile
595 data: for each edge we traversed in the chain, remove
596 the original edge's execution count. */
597 edge_frequency = ((edge_probability * b->frequency
598 + REG_BR_PROB_BASE / 2)
599 / REG_BR_PROB_BASE);
601 if (!FORWARDER_BLOCK_P (b) && forwarder_block_p (b))
602 b->flags |= BB_FORWARDER_BLOCK;
606 edge t;
608 if (!single_succ_p (first))
610 gcc_assert (n < nthreaded_edges);
611 t = threaded_edges [n++];
612 gcc_assert (t->src == first);
613 update_bb_profile_for_threading (first, edge_frequency,
614 edge_count, t);
615 update_br_prob_note (first);
617 else
619 first->count -= edge_count;
620 if (first->count < 0)
621 first->count = 0;
622 first->frequency -= edge_frequency;
623 if (first->frequency < 0)
624 first->frequency = 0;
625 /* It is possible that as the result of
626 threading we've removed edge as it is
627 threaded to the fallthru edge. Avoid
628 getting out of sync. */
629 if (n < nthreaded_edges
630 && first == threaded_edges [n]->src)
631 n++;
632 t = single_succ_edge (first);
635 t->count -= edge_count;
636 if (t->count < 0)
637 t->count = 0;
638 first = t->dest;
640 while (first != target);
642 changed = true;
643 continue;
645 ei_next (&ei);
648 if (threaded_edges)
649 free (threaded_edges);
650 return changed;
654 /* Blocks A and B are to be merged into a single block. A has no incoming
655 fallthru edge, so it can be moved before B without adding or modifying
656 any jumps (aside from the jump from A to B). */
658 static void
659 merge_blocks_move_predecessor_nojumps (basic_block a, basic_block b)
661 rtx barrier;
663 /* If we are partitioning hot/cold basic blocks, we don't want to
664 mess up unconditional or indirect jumps that cross between hot
665 and cold sections.
667 Basic block partitioning may result in some jumps that appear to
668 be optimizable (or blocks that appear to be mergeable), but which really
669 must be left untouched (they are required to make it safely across
670 partition boundaries). See the comments at the top of
671 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
673 if (BB_PARTITION (a) != BB_PARTITION (b))
674 return;
676 barrier = next_nonnote_insn (BB_END (a));
677 gcc_assert (BARRIER_P (barrier));
678 delete_insn (barrier);
680 /* Scramble the insn chain. */
681 if (BB_END (a) != PREV_INSN (BB_HEAD (b)))
682 reorder_insns_nobb (BB_HEAD (a), BB_END (a), PREV_INSN (BB_HEAD (b)));
683 df_set_bb_dirty (a);
685 if (dump_file)
686 fprintf (dump_file, "Moved block %d before %d and merged.\n",
687 a->index, b->index);
689 /* Swap the records for the two blocks around. */
691 unlink_block (a);
692 link_block (a, b->prev_bb);
694 /* Now blocks A and B are contiguous. Merge them. */
695 merge_blocks (a, b);
698 /* Blocks A and B are to be merged into a single block. B has no outgoing
699 fallthru edge, so it can be moved after A without adding or modifying
700 any jumps (aside from the jump from A to B). */
702 static void
703 merge_blocks_move_successor_nojumps (basic_block a, basic_block b)
705 rtx barrier, real_b_end;
706 rtx label, table;
708 /* If we are partitioning hot/cold basic blocks, we don't want to
709 mess up unconditional or indirect jumps that cross between hot
710 and cold sections.
712 Basic block partitioning may result in some jumps that appear to
713 be optimizable (or blocks that appear to be mergeable), but which really
714 must be left untouched (they are required to make it safely across
715 partition boundaries). See the comments at the top of
716 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
718 if (BB_PARTITION (a) != BB_PARTITION (b))
719 return;
721 real_b_end = BB_END (b);
723 /* If there is a jump table following block B temporarily add the jump table
724 to block B so that it will also be moved to the correct location. */
725 if (tablejump_p (BB_END (b), &label, &table)
726 && prev_active_insn (label) == BB_END (b))
728 BB_END (b) = table;
731 /* There had better have been a barrier there. Delete it. */
732 barrier = NEXT_INSN (BB_END (b));
733 if (barrier && BARRIER_P (barrier))
734 delete_insn (barrier);
737 /* Scramble the insn chain. */
738 reorder_insns_nobb (BB_HEAD (b), BB_END (b), BB_END (a));
740 /* Restore the real end of b. */
741 BB_END (b) = real_b_end;
743 if (dump_file)
744 fprintf (dump_file, "Moved block %d after %d and merged.\n",
745 b->index, a->index);
747 /* Now blocks A and B are contiguous. Merge them. */
748 merge_blocks (a, b);
751 /* Attempt to merge basic blocks that are potentially non-adjacent.
752 Return NULL iff the attempt failed, otherwise return basic block
753 where cleanup_cfg should continue. Because the merging commonly
754 moves basic block away or introduces another optimization
755 possibility, return basic block just before B so cleanup_cfg don't
756 need to iterate.
758 It may be good idea to return basic block before C in the case
759 C has been moved after B and originally appeared earlier in the
760 insn sequence, but we have no information available about the
761 relative ordering of these two. Hopefully it is not too common. */
763 static basic_block
764 merge_blocks_move (edge e, basic_block b, basic_block c, int mode)
766 basic_block next;
768 /* If we are partitioning hot/cold basic blocks, we don't want to
769 mess up unconditional or indirect jumps that cross between hot
770 and cold sections.
772 Basic block partitioning may result in some jumps that appear to
773 be optimizable (or blocks that appear to be mergeable), but which really
774 must be left untouched (they are required to make it safely across
775 partition boundaries). See the comments at the top of
776 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
778 if (BB_PARTITION (b) != BB_PARTITION (c))
779 return NULL;
781 /* If B has a fallthru edge to C, no need to move anything. */
782 if (e->flags & EDGE_FALLTHRU)
784 int b_index = b->index, c_index = c->index;
785 merge_blocks (b, c);
786 update_forwarder_flag (b);
788 if (dump_file)
789 fprintf (dump_file, "Merged %d and %d without moving.\n",
790 b_index, c_index);
792 return b->prev_bb == ENTRY_BLOCK_PTR ? b : b->prev_bb;
795 /* Otherwise we will need to move code around. Do that only if expensive
796 transformations are allowed. */
797 else if (mode & CLEANUP_EXPENSIVE)
799 edge tmp_edge, b_fallthru_edge;
800 bool c_has_outgoing_fallthru;
801 bool b_has_incoming_fallthru;
803 /* Avoid overactive code motion, as the forwarder blocks should be
804 eliminated by edge redirection instead. One exception might have
805 been if B is a forwarder block and C has no fallthru edge, but
806 that should be cleaned up by bb-reorder instead. */
807 if (FORWARDER_BLOCK_P (b) || FORWARDER_BLOCK_P (c))
808 return NULL;
810 /* We must make sure to not munge nesting of lexical blocks,
811 and loop notes. This is done by squeezing out all the notes
812 and leaving them there to lie. Not ideal, but functional. */
814 tmp_edge = find_fallthru_edge (c->succs);
815 c_has_outgoing_fallthru = (tmp_edge != NULL);
817 tmp_edge = find_fallthru_edge (b->preds);
818 b_has_incoming_fallthru = (tmp_edge != NULL);
819 b_fallthru_edge = tmp_edge;
820 next = b->prev_bb;
821 if (next == c)
822 next = next->prev_bb;
824 /* Otherwise, we're going to try to move C after B. If C does
825 not have an outgoing fallthru, then it can be moved
826 immediately after B without introducing or modifying jumps. */
827 if (! c_has_outgoing_fallthru)
829 merge_blocks_move_successor_nojumps (b, c);
830 return next == ENTRY_BLOCK_PTR ? next->next_bb : next;
833 /* If B does not have an incoming fallthru, then it can be moved
834 immediately before C without introducing or modifying jumps.
835 C cannot be the first block, so we do not have to worry about
836 accessing a non-existent block. */
838 if (b_has_incoming_fallthru)
840 basic_block bb;
842 if (b_fallthru_edge->src == ENTRY_BLOCK_PTR)
843 return NULL;
844 bb = force_nonfallthru (b_fallthru_edge);
845 if (bb)
846 notice_new_block (bb);
849 merge_blocks_move_predecessor_nojumps (b, c);
850 return next == ENTRY_BLOCK_PTR ? next->next_bb : next;
853 return NULL;
857 /* Removes the memory attributes of MEM expression
858 if they are not equal. */
860 void
861 merge_memattrs (rtx x, rtx y)
863 int i;
864 int j;
865 enum rtx_code code;
866 const char *fmt;
868 if (x == y)
869 return;
870 if (x == 0 || y == 0)
871 return;
873 code = GET_CODE (x);
875 if (code != GET_CODE (y))
876 return;
878 if (GET_MODE (x) != GET_MODE (y))
879 return;
881 if (code == MEM && MEM_ATTRS (x) != MEM_ATTRS (y))
883 if (! MEM_ATTRS (x))
884 MEM_ATTRS (y) = 0;
885 else if (! MEM_ATTRS (y))
886 MEM_ATTRS (x) = 0;
887 else
889 rtx mem_size;
891 if (MEM_ALIAS_SET (x) != MEM_ALIAS_SET (y))
893 set_mem_alias_set (x, 0);
894 set_mem_alias_set (y, 0);
897 if (! mem_expr_equal_p (MEM_EXPR (x), MEM_EXPR (y)))
899 set_mem_expr (x, 0);
900 set_mem_expr (y, 0);
901 set_mem_offset (x, 0);
902 set_mem_offset (y, 0);
904 else if (MEM_OFFSET (x) != MEM_OFFSET (y))
906 set_mem_offset (x, 0);
907 set_mem_offset (y, 0);
910 if (!MEM_SIZE (x))
911 mem_size = NULL_RTX;
912 else if (!MEM_SIZE (y))
913 mem_size = NULL_RTX;
914 else
915 mem_size = GEN_INT (MAX (INTVAL (MEM_SIZE (x)),
916 INTVAL (MEM_SIZE (y))));
917 set_mem_size (x, mem_size);
918 set_mem_size (y, mem_size);
920 set_mem_align (x, MIN (MEM_ALIGN (x), MEM_ALIGN (y)));
921 set_mem_align (y, MEM_ALIGN (x));
925 fmt = GET_RTX_FORMAT (code);
926 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
928 switch (fmt[i])
930 case 'E':
931 /* Two vectors must have the same length. */
932 if (XVECLEN (x, i) != XVECLEN (y, i))
933 return;
935 for (j = 0; j < XVECLEN (x, i); j++)
936 merge_memattrs (XVECEXP (x, i, j), XVECEXP (y, i, j));
938 break;
940 case 'e':
941 merge_memattrs (XEXP (x, i), XEXP (y, i));
944 return;
948 /* Return true if I1 and I2 are equivalent and thus can be crossjumped. */
950 static bool
951 old_insns_match_p (int mode ATTRIBUTE_UNUSED, rtx i1, rtx i2)
953 rtx p1, p2;
955 /* Verify that I1 and I2 are equivalent. */
956 if (GET_CODE (i1) != GET_CODE (i2))
957 return false;
959 /* __builtin_unreachable() may lead to empty blocks (ending with
960 NOTE_INSN_BASIC_BLOCK). They may be crossjumped. */
961 if (NOTE_INSN_BASIC_BLOCK_P (i1) && NOTE_INSN_BASIC_BLOCK_P (i2))
962 return true;
964 p1 = PATTERN (i1);
965 p2 = PATTERN (i2);
967 if (GET_CODE (p1) != GET_CODE (p2))
968 return false;
970 /* If this is a CALL_INSN, compare register usage information.
971 If we don't check this on stack register machines, the two
972 CALL_INSNs might be merged leaving reg-stack.c with mismatching
973 numbers of stack registers in the same basic block.
974 If we don't check this on machines with delay slots, a delay slot may
975 be filled that clobbers a parameter expected by the subroutine.
977 ??? We take the simple route for now and assume that if they're
978 equal, they were constructed identically.
980 Also check for identical exception regions. */
982 if (CALL_P (i1))
984 /* Ensure the same EH region. */
985 rtx n1 = find_reg_note (i1, REG_EH_REGION, 0);
986 rtx n2 = find_reg_note (i2, REG_EH_REGION, 0);
988 if (!n1 && n2)
989 return false;
991 if (n1 && (!n2 || XEXP (n1, 0) != XEXP (n2, 0)))
992 return false;
994 if (!rtx_equal_p (CALL_INSN_FUNCTION_USAGE (i1),
995 CALL_INSN_FUNCTION_USAGE (i2))
996 || SIBLING_CALL_P (i1) != SIBLING_CALL_P (i2))
997 return false;
1000 #ifdef STACK_REGS
1001 /* If cross_jump_death_matters is not 0, the insn's mode
1002 indicates whether or not the insn contains any stack-like
1003 regs. */
1005 if ((mode & CLEANUP_POST_REGSTACK) && stack_regs_mentioned (i1))
1007 /* If register stack conversion has already been done, then
1008 death notes must also be compared before it is certain that
1009 the two instruction streams match. */
1011 rtx note;
1012 HARD_REG_SET i1_regset, i2_regset;
1014 CLEAR_HARD_REG_SET (i1_regset);
1015 CLEAR_HARD_REG_SET (i2_regset);
1017 for (note = REG_NOTES (i1); note; note = XEXP (note, 1))
1018 if (REG_NOTE_KIND (note) == REG_DEAD && STACK_REG_P (XEXP (note, 0)))
1019 SET_HARD_REG_BIT (i1_regset, REGNO (XEXP (note, 0)));
1021 for (note = REG_NOTES (i2); note; note = XEXP (note, 1))
1022 if (REG_NOTE_KIND (note) == REG_DEAD && STACK_REG_P (XEXP (note, 0)))
1023 SET_HARD_REG_BIT (i2_regset, REGNO (XEXP (note, 0)));
1025 if (!hard_reg_set_equal_p (i1_regset, i2_regset))
1026 return false;
1028 #endif
1030 if (reload_completed
1031 ? rtx_renumbered_equal_p (p1, p2) : rtx_equal_p (p1, p2))
1032 return true;
1034 return false;
1037 /* When comparing insns I1 and I2 in flow_find_cross_jump or
1038 flow_find_head_matching_sequence, ensure the notes match. */
1040 static void
1041 merge_notes (rtx i1, rtx i2)
1043 /* If the merged insns have different REG_EQUAL notes, then
1044 remove them. */
1045 rtx equiv1 = find_reg_equal_equiv_note (i1);
1046 rtx equiv2 = find_reg_equal_equiv_note (i2);
1048 if (equiv1 && !equiv2)
1049 remove_note (i1, equiv1);
1050 else if (!equiv1 && equiv2)
1051 remove_note (i2, equiv2);
1052 else if (equiv1 && equiv2
1053 && !rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))
1055 remove_note (i1, equiv1);
1056 remove_note (i2, equiv2);
1060 /* Look through the insns at the end of BB1 and BB2 and find the longest
1061 sequence that are equivalent. Store the first insns for that sequence
1062 in *F1 and *F2 and return the sequence length.
1064 To simplify callers of this function, if the blocks match exactly,
1065 store the head of the blocks in *F1 and *F2. */
1068 flow_find_cross_jump (basic_block bb1, basic_block bb2, rtx *f1, rtx *f2)
1070 rtx i1, i2, last1, last2, afterlast1, afterlast2;
1071 int ninsns = 0;
1073 /* Skip simple jumps at the end of the blocks. Complex jumps still
1074 need to be compared for equivalence, which we'll do below. */
1076 i1 = BB_END (bb1);
1077 last1 = afterlast1 = last2 = afterlast2 = NULL_RTX;
1078 if (onlyjump_p (i1)
1079 || (returnjump_p (i1) && !side_effects_p (PATTERN (i1))))
1081 last1 = i1;
1082 i1 = PREV_INSN (i1);
1085 i2 = BB_END (bb2);
1086 if (onlyjump_p (i2)
1087 || (returnjump_p (i2) && !side_effects_p (PATTERN (i2))))
1089 last2 = i2;
1090 /* Count everything except for unconditional jump as insn. */
1091 if (!simplejump_p (i2) && !returnjump_p (i2) && last1)
1092 ninsns++;
1093 i2 = PREV_INSN (i2);
1096 while (true)
1098 /* Ignore notes. */
1099 while (!NONDEBUG_INSN_P (i1) && i1 != BB_HEAD (bb1))
1100 i1 = PREV_INSN (i1);
1102 while (!NONDEBUG_INSN_P (i2) && i2 != BB_HEAD (bb2))
1103 i2 = PREV_INSN (i2);
1105 if (i1 == BB_HEAD (bb1) || i2 == BB_HEAD (bb2))
1106 break;
1108 if (!old_insns_match_p (0, i1, i2))
1109 break;
1111 merge_memattrs (i1, i2);
1113 /* Don't begin a cross-jump with a NOTE insn. */
1114 if (INSN_P (i1))
1116 merge_notes (i1, i2);
1118 afterlast1 = last1, afterlast2 = last2;
1119 last1 = i1, last2 = i2;
1120 ninsns++;
1123 i1 = PREV_INSN (i1);
1124 i2 = PREV_INSN (i2);
1127 #ifdef HAVE_cc0
1128 /* Don't allow the insn after a compare to be shared by
1129 cross-jumping unless the compare is also shared. */
1130 if (ninsns && reg_mentioned_p (cc0_rtx, last1) && ! sets_cc0_p (last1))
1131 last1 = afterlast1, last2 = afterlast2, ninsns--;
1132 #endif
1134 /* Include preceding notes and labels in the cross-jump. One,
1135 this may bring us to the head of the blocks as requested above.
1136 Two, it keeps line number notes as matched as may be. */
1137 if (ninsns)
1139 while (last1 != BB_HEAD (bb1) && !NONDEBUG_INSN_P (PREV_INSN (last1)))
1140 last1 = PREV_INSN (last1);
1142 if (last1 != BB_HEAD (bb1) && LABEL_P (PREV_INSN (last1)))
1143 last1 = PREV_INSN (last1);
1145 while (last2 != BB_HEAD (bb2) && !NONDEBUG_INSN_P (PREV_INSN (last2)))
1146 last2 = PREV_INSN (last2);
1148 if (last2 != BB_HEAD (bb2) && LABEL_P (PREV_INSN (last2)))
1149 last2 = PREV_INSN (last2);
1151 *f1 = last1;
1152 *f2 = last2;
1155 return ninsns;
1158 /* Like flow_find_cross_jump, except start looking for a matching sequence from
1159 the head of the two blocks. Do not include jumps at the end.
1160 If STOP_AFTER is nonzero, stop after finding that many matching
1161 instructions. */
1164 flow_find_head_matching_sequence (basic_block bb1, basic_block bb2, rtx *f1,
1165 rtx *f2, int stop_after)
1167 rtx i1, i2, last1, last2, beforelast1, beforelast2;
1168 int ninsns = 0;
1169 edge e;
1170 edge_iterator ei;
1171 int nehedges1 = 0, nehedges2 = 0;
1173 FOR_EACH_EDGE (e, ei, bb1->succs)
1174 if (e->flags & EDGE_EH)
1175 nehedges1++;
1176 FOR_EACH_EDGE (e, ei, bb2->succs)
1177 if (e->flags & EDGE_EH)
1178 nehedges2++;
1180 i1 = BB_HEAD (bb1);
1181 i2 = BB_HEAD (bb2);
1182 last1 = beforelast1 = last2 = beforelast2 = NULL_RTX;
1184 while (true)
1186 /* Ignore notes. */
1187 while (!NONDEBUG_INSN_P (i1) && i1 != BB_END (bb1))
1188 i1 = NEXT_INSN (i1);
1190 while (!NONDEBUG_INSN_P (i2) && i2 != BB_END (bb2))
1191 i2 = NEXT_INSN (i2);
1193 if ((i1 == BB_END (bb1) && !NONDEBUG_INSN_P (i1))
1194 || (i2 == BB_END (bb2) && !NONDEBUG_INSN_P (i2)))
1195 break;
1197 if (NOTE_P (i1) || NOTE_P (i2)
1198 || JUMP_P (i1) || JUMP_P (i2))
1199 break;
1201 /* A sanity check to make sure we're not merging insns with different
1202 effects on EH. If only one of them ends a basic block, it shouldn't
1203 have an EH edge; if both end a basic block, there should be the same
1204 number of EH edges. */
1205 if ((i1 == BB_END (bb1) && i2 != BB_END (bb2)
1206 && nehedges1 > 0)
1207 || (i2 == BB_END (bb2) && i1 != BB_END (bb1)
1208 && nehedges2 > 0)
1209 || (i1 == BB_END (bb1) && i2 == BB_END (bb2)
1210 && nehedges1 != nehedges2))
1211 break;
1213 if (!old_insns_match_p (0, i1, i2))
1214 break;
1216 merge_memattrs (i1, i2);
1218 /* Don't begin a cross-jump with a NOTE insn. */
1219 if (INSN_P (i1))
1221 merge_notes (i1, i2);
1223 beforelast1 = last1, beforelast2 = last2;
1224 last1 = i1, last2 = i2;
1225 ninsns++;
1228 if (i1 == BB_END (bb1) || i2 == BB_END (bb2)
1229 || (stop_after > 0 && ninsns == stop_after))
1230 break;
1232 i1 = NEXT_INSN (i1);
1233 i2 = NEXT_INSN (i2);
1236 #ifdef HAVE_cc0
1237 /* Don't allow a compare to be shared by cross-jumping unless the insn
1238 after the compare is also shared. */
1239 if (ninsns && reg_mentioned_p (cc0_rtx, last1) && sets_cc0_p (last1))
1240 last1 = beforelast1, last2 = beforelast2, ninsns--;
1241 #endif
1243 if (ninsns)
1245 *f1 = last1;
1246 *f2 = last2;
1249 return ninsns;
1252 /* Return true iff outgoing edges of BB1 and BB2 match, together with
1253 the branch instruction. This means that if we commonize the control
1254 flow before end of the basic block, the semantic remains unchanged.
1256 We may assume that there exists one edge with a common destination. */
1258 static bool
1259 outgoing_edges_match (int mode, basic_block bb1, basic_block bb2)
1261 int nehedges1 = 0, nehedges2 = 0;
1262 edge fallthru1 = 0, fallthru2 = 0;
1263 edge e1, e2;
1264 edge_iterator ei;
1266 /* If BB1 has only one successor, we may be looking at either an
1267 unconditional jump, or a fake edge to exit. */
1268 if (single_succ_p (bb1)
1269 && (single_succ_edge (bb1)->flags & (EDGE_COMPLEX | EDGE_FAKE)) == 0
1270 && (!JUMP_P (BB_END (bb1)) || simplejump_p (BB_END (bb1))))
1271 return (single_succ_p (bb2)
1272 && (single_succ_edge (bb2)->flags
1273 & (EDGE_COMPLEX | EDGE_FAKE)) == 0
1274 && (!JUMP_P (BB_END (bb2)) || simplejump_p (BB_END (bb2))));
1276 /* Match conditional jumps - this may get tricky when fallthru and branch
1277 edges are crossed. */
1278 if (EDGE_COUNT (bb1->succs) == 2
1279 && any_condjump_p (BB_END (bb1))
1280 && onlyjump_p (BB_END (bb1)))
1282 edge b1, f1, b2, f2;
1283 bool reverse, match;
1284 rtx set1, set2, cond1, cond2;
1285 enum rtx_code code1, code2;
1287 if (EDGE_COUNT (bb2->succs) != 2
1288 || !any_condjump_p (BB_END (bb2))
1289 || !onlyjump_p (BB_END (bb2)))
1290 return false;
1292 b1 = BRANCH_EDGE (bb1);
1293 b2 = BRANCH_EDGE (bb2);
1294 f1 = FALLTHRU_EDGE (bb1);
1295 f2 = FALLTHRU_EDGE (bb2);
1297 /* Get around possible forwarders on fallthru edges. Other cases
1298 should be optimized out already. */
1299 if (FORWARDER_BLOCK_P (f1->dest))
1300 f1 = single_succ_edge (f1->dest);
1302 if (FORWARDER_BLOCK_P (f2->dest))
1303 f2 = single_succ_edge (f2->dest);
1305 /* To simplify use of this function, return false if there are
1306 unneeded forwarder blocks. These will get eliminated later
1307 during cleanup_cfg. */
1308 if (FORWARDER_BLOCK_P (f1->dest)
1309 || FORWARDER_BLOCK_P (f2->dest)
1310 || FORWARDER_BLOCK_P (b1->dest)
1311 || FORWARDER_BLOCK_P (b2->dest))
1312 return false;
1314 if (f1->dest == f2->dest && b1->dest == b2->dest)
1315 reverse = false;
1316 else if (f1->dest == b2->dest && b1->dest == f2->dest)
1317 reverse = true;
1318 else
1319 return false;
1321 set1 = pc_set (BB_END (bb1));
1322 set2 = pc_set (BB_END (bb2));
1323 if ((XEXP (SET_SRC (set1), 1) == pc_rtx)
1324 != (XEXP (SET_SRC (set2), 1) == pc_rtx))
1325 reverse = !reverse;
1327 cond1 = XEXP (SET_SRC (set1), 0);
1328 cond2 = XEXP (SET_SRC (set2), 0);
1329 code1 = GET_CODE (cond1);
1330 if (reverse)
1331 code2 = reversed_comparison_code (cond2, BB_END (bb2));
1332 else
1333 code2 = GET_CODE (cond2);
1335 if (code2 == UNKNOWN)
1336 return false;
1338 /* Verify codes and operands match. */
1339 match = ((code1 == code2
1340 && rtx_renumbered_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
1341 && rtx_renumbered_equal_p (XEXP (cond1, 1), XEXP (cond2, 1)))
1342 || (code1 == swap_condition (code2)
1343 && rtx_renumbered_equal_p (XEXP (cond1, 1),
1344 XEXP (cond2, 0))
1345 && rtx_renumbered_equal_p (XEXP (cond1, 0),
1346 XEXP (cond2, 1))));
1348 /* If we return true, we will join the blocks. Which means that
1349 we will only have one branch prediction bit to work with. Thus
1350 we require the existing branches to have probabilities that are
1351 roughly similar. */
1352 if (match
1353 && optimize_bb_for_speed_p (bb1)
1354 && optimize_bb_for_speed_p (bb2))
1356 int prob2;
1358 if (b1->dest == b2->dest)
1359 prob2 = b2->probability;
1360 else
1361 /* Do not use f2 probability as f2 may be forwarded. */
1362 prob2 = REG_BR_PROB_BASE - b2->probability;
1364 /* Fail if the difference in probabilities is greater than 50%.
1365 This rules out two well-predicted branches with opposite
1366 outcomes. */
1367 if (abs (b1->probability - prob2) > REG_BR_PROB_BASE / 2)
1369 if (dump_file)
1370 fprintf (dump_file,
1371 "Outcomes of branch in bb %i and %i differ too much (%i %i)\n",
1372 bb1->index, bb2->index, b1->probability, prob2);
1374 return false;
1378 if (dump_file && match)
1379 fprintf (dump_file, "Conditionals in bb %i and %i match.\n",
1380 bb1->index, bb2->index);
1382 return match;
1385 /* Generic case - we are seeing a computed jump, table jump or trapping
1386 instruction. */
1388 /* Check whether there are tablejumps in the end of BB1 and BB2.
1389 Return true if they are identical. */
1391 rtx label1, label2;
1392 rtx table1, table2;
1394 if (tablejump_p (BB_END (bb1), &label1, &table1)
1395 && tablejump_p (BB_END (bb2), &label2, &table2)
1396 && GET_CODE (PATTERN (table1)) == GET_CODE (PATTERN (table2)))
1398 /* The labels should never be the same rtx. If they really are same
1399 the jump tables are same too. So disable crossjumping of blocks BB1
1400 and BB2 because when deleting the common insns in the end of BB1
1401 by delete_basic_block () the jump table would be deleted too. */
1402 /* If LABEL2 is referenced in BB1->END do not do anything
1403 because we would loose information when replacing
1404 LABEL1 by LABEL2 and then LABEL2 by LABEL1 in BB1->END. */
1405 if (label1 != label2 && !rtx_referenced_p (label2, BB_END (bb1)))
1407 /* Set IDENTICAL to true when the tables are identical. */
1408 bool identical = false;
1409 rtx p1, p2;
1411 p1 = PATTERN (table1);
1412 p2 = PATTERN (table2);
1413 if (GET_CODE (p1) == ADDR_VEC && rtx_equal_p (p1, p2))
1415 identical = true;
1417 else if (GET_CODE (p1) == ADDR_DIFF_VEC
1418 && (XVECLEN (p1, 1) == XVECLEN (p2, 1))
1419 && rtx_equal_p (XEXP (p1, 2), XEXP (p2, 2))
1420 && rtx_equal_p (XEXP (p1, 3), XEXP (p2, 3)))
1422 int i;
1424 identical = true;
1425 for (i = XVECLEN (p1, 1) - 1; i >= 0 && identical; i--)
1426 if (!rtx_equal_p (XVECEXP (p1, 1, i), XVECEXP (p2, 1, i)))
1427 identical = false;
1430 if (identical)
1432 replace_label_data rr;
1433 bool match;
1435 /* Temporarily replace references to LABEL1 with LABEL2
1436 in BB1->END so that we could compare the instructions. */
1437 rr.r1 = label1;
1438 rr.r2 = label2;
1439 rr.update_label_nuses = false;
1440 for_each_rtx (&BB_END (bb1), replace_label, &rr);
1442 match = old_insns_match_p (mode, BB_END (bb1), BB_END (bb2));
1443 if (dump_file && match)
1444 fprintf (dump_file,
1445 "Tablejumps in bb %i and %i match.\n",
1446 bb1->index, bb2->index);
1448 /* Set the original label in BB1->END because when deleting
1449 a block whose end is a tablejump, the tablejump referenced
1450 from the instruction is deleted too. */
1451 rr.r1 = label2;
1452 rr.r2 = label1;
1453 for_each_rtx (&BB_END (bb1), replace_label, &rr);
1455 return match;
1458 return false;
1462 /* First ensure that the instructions match. There may be many outgoing
1463 edges so this test is generally cheaper. */
1464 if (!old_insns_match_p (mode, BB_END (bb1), BB_END (bb2)))
1465 return false;
1467 /* Search the outgoing edges, ensure that the counts do match, find possible
1468 fallthru and exception handling edges since these needs more
1469 validation. */
1470 if (EDGE_COUNT (bb1->succs) != EDGE_COUNT (bb2->succs))
1471 return false;
1473 FOR_EACH_EDGE (e1, ei, bb1->succs)
1475 e2 = EDGE_SUCC (bb2, ei.index);
1477 if (e1->flags & EDGE_EH)
1478 nehedges1++;
1480 if (e2->flags & EDGE_EH)
1481 nehedges2++;
1483 if (e1->flags & EDGE_FALLTHRU)
1484 fallthru1 = e1;
1485 if (e2->flags & EDGE_FALLTHRU)
1486 fallthru2 = e2;
1489 /* If number of edges of various types does not match, fail. */
1490 if (nehedges1 != nehedges2
1491 || (fallthru1 != 0) != (fallthru2 != 0))
1492 return false;
1494 /* fallthru edges must be forwarded to the same destination. */
1495 if (fallthru1)
1497 basic_block d1 = (forwarder_block_p (fallthru1->dest)
1498 ? single_succ (fallthru1->dest): fallthru1->dest);
1499 basic_block d2 = (forwarder_block_p (fallthru2->dest)
1500 ? single_succ (fallthru2->dest): fallthru2->dest);
1502 if (d1 != d2)
1503 return false;
1506 /* Ensure the same EH region. */
1508 rtx n1 = find_reg_note (BB_END (bb1), REG_EH_REGION, 0);
1509 rtx n2 = find_reg_note (BB_END (bb2), REG_EH_REGION, 0);
1511 if (!n1 && n2)
1512 return false;
1514 if (n1 && (!n2 || XEXP (n1, 0) != XEXP (n2, 0)))
1515 return false;
1518 /* The same checks as in try_crossjump_to_edge. It is required for RTL
1519 version of sequence abstraction. */
1520 FOR_EACH_EDGE (e1, ei, bb2->succs)
1522 edge e2;
1523 edge_iterator ei;
1524 basic_block d1 = e1->dest;
1526 if (FORWARDER_BLOCK_P (d1))
1527 d1 = EDGE_SUCC (d1, 0)->dest;
1529 FOR_EACH_EDGE (e2, ei, bb1->succs)
1531 basic_block d2 = e2->dest;
1532 if (FORWARDER_BLOCK_P (d2))
1533 d2 = EDGE_SUCC (d2, 0)->dest;
1534 if (d1 == d2)
1535 break;
1538 if (!e2)
1539 return false;
1542 return true;
1545 /* Returns true if BB basic block has a preserve label. */
1547 static bool
1548 block_has_preserve_label (basic_block bb)
1550 return (bb
1551 && block_label (bb)
1552 && LABEL_PRESERVE_P (block_label (bb)));
1555 /* E1 and E2 are edges with the same destination block. Search their
1556 predecessors for common code. If found, redirect control flow from
1557 (maybe the middle of) E1->SRC to (maybe the middle of) E2->SRC. */
1559 static bool
1560 try_crossjump_to_edge (int mode, edge e1, edge e2)
1562 int nmatch;
1563 basic_block src1 = e1->src, src2 = e2->src;
1564 basic_block redirect_to, redirect_from, to_remove;
1565 rtx newpos1, newpos2;
1566 edge s;
1567 edge_iterator ei;
1569 newpos1 = newpos2 = NULL_RTX;
1571 /* If we have partitioned hot/cold basic blocks, it is a bad idea
1572 to try this optimization.
1574 Basic block partitioning may result in some jumps that appear to
1575 be optimizable (or blocks that appear to be mergeable), but which really
1576 must be left untouched (they are required to make it safely across
1577 partition boundaries). See the comments at the top of
1578 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
1580 if (flag_reorder_blocks_and_partition && reload_completed)
1581 return false;
1583 /* Search backward through forwarder blocks. We don't need to worry
1584 about multiple entry or chained forwarders, as they will be optimized
1585 away. We do this to look past the unconditional jump following a
1586 conditional jump that is required due to the current CFG shape. */
1587 if (single_pred_p (src1)
1588 && FORWARDER_BLOCK_P (src1))
1589 e1 = single_pred_edge (src1), src1 = e1->src;
1591 if (single_pred_p (src2)
1592 && FORWARDER_BLOCK_P (src2))
1593 e2 = single_pred_edge (src2), src2 = e2->src;
1595 /* Nothing to do if we reach ENTRY, or a common source block. */
1596 if (src1 == ENTRY_BLOCK_PTR || src2 == ENTRY_BLOCK_PTR)
1597 return false;
1598 if (src1 == src2)
1599 return false;
1601 /* Seeing more than 1 forwarder blocks would confuse us later... */
1602 if (FORWARDER_BLOCK_P (e1->dest)
1603 && FORWARDER_BLOCK_P (single_succ (e1->dest)))
1604 return false;
1606 if (FORWARDER_BLOCK_P (e2->dest)
1607 && FORWARDER_BLOCK_P (single_succ (e2->dest)))
1608 return false;
1610 /* Likewise with dead code (possibly newly created by the other optimizations
1611 of cfg_cleanup). */
1612 if (EDGE_COUNT (src1->preds) == 0 || EDGE_COUNT (src2->preds) == 0)
1613 return false;
1615 /* Look for the common insn sequence, part the first ... */
1616 if (!outgoing_edges_match (mode, src1, src2))
1617 return false;
1619 /* ... and part the second. */
1620 nmatch = flow_find_cross_jump (src1, src2, &newpos1, &newpos2);
1622 /* Don't proceed with the crossjump unless we found a sufficient number
1623 of matching instructions or the 'from' block was totally matched
1624 (such that its predecessors will hopefully be redirected and the
1625 block removed). */
1626 if ((nmatch < PARAM_VALUE (PARAM_MIN_CROSSJUMP_INSNS))
1627 && (newpos1 != BB_HEAD (src1)))
1628 return false;
1630 /* Avoid deleting preserve label when redirecting ABNORMAL edges. */
1631 if (block_has_preserve_label (e1->dest)
1632 && (e1->flags & EDGE_ABNORMAL))
1633 return false;
1635 /* Here we know that the insns in the end of SRC1 which are common with SRC2
1636 will be deleted.
1637 If we have tablejumps in the end of SRC1 and SRC2
1638 they have been already compared for equivalence in outgoing_edges_match ()
1639 so replace the references to TABLE1 by references to TABLE2. */
1641 rtx label1, label2;
1642 rtx table1, table2;
1644 if (tablejump_p (BB_END (src1), &label1, &table1)
1645 && tablejump_p (BB_END (src2), &label2, &table2)
1646 && label1 != label2)
1648 replace_label_data rr;
1649 rtx insn;
1651 /* Replace references to LABEL1 with LABEL2. */
1652 rr.r1 = label1;
1653 rr.r2 = label2;
1654 rr.update_label_nuses = true;
1655 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1657 /* Do not replace the label in SRC1->END because when deleting
1658 a block whose end is a tablejump, the tablejump referenced
1659 from the instruction is deleted too. */
1660 if (insn != BB_END (src1))
1661 for_each_rtx (&insn, replace_label, &rr);
1666 /* Avoid splitting if possible. We must always split when SRC2 has
1667 EH predecessor edges, or we may end up with basic blocks with both
1668 normal and EH predecessor edges. */
1669 if (newpos2 == BB_HEAD (src2)
1670 && !(EDGE_PRED (src2, 0)->flags & EDGE_EH))
1671 redirect_to = src2;
1672 else
1674 if (newpos2 == BB_HEAD (src2))
1676 /* Skip possible basic block header. */
1677 if (LABEL_P (newpos2))
1678 newpos2 = NEXT_INSN (newpos2);
1679 while (DEBUG_INSN_P (newpos2))
1680 newpos2 = NEXT_INSN (newpos2);
1681 if (NOTE_P (newpos2))
1682 newpos2 = NEXT_INSN (newpos2);
1683 while (DEBUG_INSN_P (newpos2))
1684 newpos2 = NEXT_INSN (newpos2);
1687 if (dump_file)
1688 fprintf (dump_file, "Splitting bb %i before %i insns\n",
1689 src2->index, nmatch);
1690 redirect_to = split_block (src2, PREV_INSN (newpos2))->dest;
1693 if (dump_file)
1694 fprintf (dump_file,
1695 "Cross jumping from bb %i to bb %i; %i common insns\n",
1696 src1->index, src2->index, nmatch);
1698 /* We may have some registers visible through the block. */
1699 df_set_bb_dirty (redirect_to);
1701 /* Recompute the frequencies and counts of outgoing edges. */
1702 FOR_EACH_EDGE (s, ei, redirect_to->succs)
1704 edge s2;
1705 edge_iterator ei;
1706 basic_block d = s->dest;
1708 if (FORWARDER_BLOCK_P (d))
1709 d = single_succ (d);
1711 FOR_EACH_EDGE (s2, ei, src1->succs)
1713 basic_block d2 = s2->dest;
1714 if (FORWARDER_BLOCK_P (d2))
1715 d2 = single_succ (d2);
1716 if (d == d2)
1717 break;
1720 s->count += s2->count;
1722 /* Take care to update possible forwarder blocks. We verified
1723 that there is no more than one in the chain, so we can't run
1724 into infinite loop. */
1725 if (FORWARDER_BLOCK_P (s->dest))
1727 single_succ_edge (s->dest)->count += s2->count;
1728 s->dest->count += s2->count;
1729 s->dest->frequency += EDGE_FREQUENCY (s);
1732 if (FORWARDER_BLOCK_P (s2->dest))
1734 single_succ_edge (s2->dest)->count -= s2->count;
1735 if (single_succ_edge (s2->dest)->count < 0)
1736 single_succ_edge (s2->dest)->count = 0;
1737 s2->dest->count -= s2->count;
1738 s2->dest->frequency -= EDGE_FREQUENCY (s);
1739 if (s2->dest->frequency < 0)
1740 s2->dest->frequency = 0;
1741 if (s2->dest->count < 0)
1742 s2->dest->count = 0;
1745 if (!redirect_to->frequency && !src1->frequency)
1746 s->probability = (s->probability + s2->probability) / 2;
1747 else
1748 s->probability
1749 = ((s->probability * redirect_to->frequency +
1750 s2->probability * src1->frequency)
1751 / (redirect_to->frequency + src1->frequency));
1754 /* Adjust count and frequency for the block. An earlier jump
1755 threading pass may have left the profile in an inconsistent
1756 state (see update_bb_profile_for_threading) so we must be
1757 prepared for overflows. */
1758 redirect_to->count += src1->count;
1759 redirect_to->frequency += src1->frequency;
1760 if (redirect_to->frequency > BB_FREQ_MAX)
1761 redirect_to->frequency = BB_FREQ_MAX;
1762 update_br_prob_note (redirect_to);
1764 /* Edit SRC1 to go to REDIRECT_TO at NEWPOS1. */
1766 /* Skip possible basic block header. */
1767 if (LABEL_P (newpos1))
1768 newpos1 = NEXT_INSN (newpos1);
1770 while (DEBUG_INSN_P (newpos1))
1771 newpos1 = NEXT_INSN (newpos1);
1773 if (NOTE_INSN_BASIC_BLOCK_P (newpos1))
1774 newpos1 = NEXT_INSN (newpos1);
1776 while (DEBUG_INSN_P (newpos1))
1777 newpos1 = NEXT_INSN (newpos1);
1779 redirect_from = split_block (src1, PREV_INSN (newpos1))->src;
1780 to_remove = single_succ (redirect_from);
1782 redirect_edge_and_branch_force (single_succ_edge (redirect_from), redirect_to);
1783 delete_basic_block (to_remove);
1785 update_forwarder_flag (redirect_from);
1786 if (redirect_to != src2)
1787 update_forwarder_flag (src2);
1789 return true;
1792 /* Search the predecessors of BB for common insn sequences. When found,
1793 share code between them by redirecting control flow. Return true if
1794 any changes made. */
1796 static bool
1797 try_crossjump_bb (int mode, basic_block bb)
1799 edge e, e2, fallthru;
1800 bool changed;
1801 unsigned max, ix, ix2;
1802 basic_block ev, ev2;
1804 /* Nothing to do if there is not at least two incoming edges. */
1805 if (EDGE_COUNT (bb->preds) < 2)
1806 return false;
1808 /* Don't crossjump if this block ends in a computed jump,
1809 unless we are optimizing for size. */
1810 if (optimize_bb_for_size_p (bb)
1811 && bb != EXIT_BLOCK_PTR
1812 && computed_jump_p (BB_END (bb)))
1813 return false;
1815 /* If we are partitioning hot/cold basic blocks, we don't want to
1816 mess up unconditional or indirect jumps that cross between hot
1817 and cold sections.
1819 Basic block partitioning may result in some jumps that appear to
1820 be optimizable (or blocks that appear to be mergeable), but which really
1821 must be left untouched (they are required to make it safely across
1822 partition boundaries). See the comments at the top of
1823 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
1825 if (BB_PARTITION (EDGE_PRED (bb, 0)->src) !=
1826 BB_PARTITION (EDGE_PRED (bb, 1)->src)
1827 || (EDGE_PRED (bb, 0)->flags & EDGE_CROSSING))
1828 return false;
1830 /* It is always cheapest to redirect a block that ends in a branch to
1831 a block that falls through into BB, as that adds no branches to the
1832 program. We'll try that combination first. */
1833 fallthru = NULL;
1834 max = PARAM_VALUE (PARAM_MAX_CROSSJUMP_EDGES);
1836 if (EDGE_COUNT (bb->preds) > max)
1837 return false;
1839 fallthru = find_fallthru_edge (bb->preds);
1841 changed = false;
1842 for (ix = 0, ev = bb; ix < EDGE_COUNT (ev->preds); )
1844 e = EDGE_PRED (ev, ix);
1845 ix++;
1847 /* As noted above, first try with the fallthru predecessor (or, a
1848 fallthru predecessor if we are in cfglayout mode). */
1849 if (fallthru)
1851 /* Don't combine the fallthru edge into anything else.
1852 If there is a match, we'll do it the other way around. */
1853 if (e == fallthru)
1854 continue;
1855 /* If nothing changed since the last attempt, there is nothing
1856 we can do. */
1857 if (!first_pass
1858 && !((e->src->flags & BB_MODIFIED)
1859 || (fallthru->src->flags & BB_MODIFIED)))
1860 continue;
1862 if (try_crossjump_to_edge (mode, e, fallthru))
1864 changed = true;
1865 ix = 0;
1866 ev = bb;
1867 continue;
1871 /* Non-obvious work limiting check: Recognize that we're going
1872 to call try_crossjump_bb on every basic block. So if we have
1873 two blocks with lots of outgoing edges (a switch) and they
1874 share lots of common destinations, then we would do the
1875 cross-jump check once for each common destination.
1877 Now, if the blocks actually are cross-jump candidates, then
1878 all of their destinations will be shared. Which means that
1879 we only need check them for cross-jump candidacy once. We
1880 can eliminate redundant checks of crossjump(A,B) by arbitrarily
1881 choosing to do the check from the block for which the edge
1882 in question is the first successor of A. */
1883 if (EDGE_SUCC (e->src, 0) != e)
1884 continue;
1886 for (ix2 = 0, ev2 = bb; ix2 < EDGE_COUNT (ev2->preds); )
1888 e2 = EDGE_PRED (ev2, ix2);
1889 ix2++;
1891 if (e2 == e)
1892 continue;
1894 /* We've already checked the fallthru edge above. */
1895 if (e2 == fallthru)
1896 continue;
1898 /* The "first successor" check above only prevents multiple
1899 checks of crossjump(A,B). In order to prevent redundant
1900 checks of crossjump(B,A), require that A be the block
1901 with the lowest index. */
1902 if (e->src->index > e2->src->index)
1903 continue;
1905 /* If nothing changed since the last attempt, there is nothing
1906 we can do. */
1907 if (!first_pass
1908 && !((e->src->flags & BB_MODIFIED)
1909 || (e2->src->flags & BB_MODIFIED)))
1910 continue;
1912 if (try_crossjump_to_edge (mode, e, e2))
1914 changed = true;
1915 ev2 = bb;
1916 ix = 0;
1917 break;
1922 if (changed)
1923 crossjumps_occured = true;
1925 return changed;
1928 /* Search the successors of BB for common insn sequences. When found,
1929 share code between them by moving it across the basic block
1930 boundary. Return true if any changes made. */
1932 static bool
1933 try_head_merge_bb (basic_block bb)
1935 basic_block final_dest_bb = NULL;
1936 int max_match = INT_MAX;
1937 edge e0;
1938 rtx *headptr, *currptr, *nextptr;
1939 bool changed, moveall;
1940 unsigned ix;
1941 rtx e0_last_head, cond, move_before;
1942 unsigned nedges = EDGE_COUNT (bb->succs);
1943 rtx jump = BB_END (bb);
1944 regset live, live_union;
1946 /* Nothing to do if there is not at least two outgoing edges. */
1947 if (nedges < 2)
1948 return false;
1950 /* Don't crossjump if this block ends in a computed jump,
1951 unless we are optimizing for size. */
1952 if (optimize_bb_for_size_p (bb)
1953 && bb != EXIT_BLOCK_PTR
1954 && computed_jump_p (BB_END (bb)))
1955 return false;
1957 cond = get_condition (jump, &move_before, true, false);
1958 if (cond == NULL_RTX)
1959 move_before = jump;
1961 for (ix = 0; ix < nedges; ix++)
1962 if (EDGE_SUCC (bb, ix)->dest == EXIT_BLOCK_PTR)
1963 return false;
1965 for (ix = 0; ix < nedges; ix++)
1967 edge e = EDGE_SUCC (bb, ix);
1968 basic_block other_bb = e->dest;
1970 if (df_get_bb_dirty (other_bb))
1972 block_was_dirty = true;
1973 return false;
1976 if (e->flags & EDGE_ABNORMAL)
1977 return false;
1979 /* Normally, all destination blocks must only be reachable from this
1980 block, i.e. they must have one incoming edge.
1982 There is one special case we can handle, that of multiple consecutive
1983 jumps where the first jumps to one of the targets of the second jump.
1984 This happens frequently in switch statements for default labels.
1985 The structure is as follows:
1986 FINAL_DEST_BB
1987 ....
1988 if (cond) jump A;
1989 fall through
1991 jump with targets A, B, C, D...
1993 has two incoming edges, from FINAL_DEST_BB and BB
1995 In this case, we can try to move the insns through BB and into
1996 FINAL_DEST_BB. */
1997 if (EDGE_COUNT (other_bb->preds) != 1)
1999 edge incoming_edge, incoming_bb_other_edge;
2000 edge_iterator ei;
2002 if (final_dest_bb != NULL
2003 || EDGE_COUNT (other_bb->preds) != 2)
2004 return false;
2006 /* We must be able to move the insns across the whole block. */
2007 move_before = BB_HEAD (bb);
2008 while (!NONDEBUG_INSN_P (move_before))
2009 move_before = NEXT_INSN (move_before);
2011 if (EDGE_COUNT (bb->preds) != 1)
2012 return false;
2013 incoming_edge = EDGE_PRED (bb, 0);
2014 final_dest_bb = incoming_edge->src;
2015 if (EDGE_COUNT (final_dest_bb->succs) != 2)
2016 return false;
2017 FOR_EACH_EDGE (incoming_bb_other_edge, ei, final_dest_bb->succs)
2018 if (incoming_bb_other_edge != incoming_edge)
2019 break;
2020 if (incoming_bb_other_edge->dest != other_bb)
2021 return false;
2025 e0 = EDGE_SUCC (bb, 0);
2026 e0_last_head = NULL_RTX;
2027 changed = false;
2029 for (ix = 1; ix < nedges; ix++)
2031 edge e = EDGE_SUCC (bb, ix);
2032 rtx e0_last, e_last;
2033 int nmatch;
2035 nmatch = flow_find_head_matching_sequence (e0->dest, e->dest,
2036 &e0_last, &e_last, 0);
2037 if (nmatch == 0)
2038 return false;
2040 if (nmatch < max_match)
2042 max_match = nmatch;
2043 e0_last_head = e0_last;
2047 /* If we matched an entire block, we probably have to avoid moving the
2048 last insn. */
2049 if (max_match > 0
2050 && e0_last_head == BB_END (e0->dest)
2051 && (find_reg_note (e0_last_head, REG_EH_REGION, 0)
2052 || control_flow_insn_p (e0_last_head)))
2054 max_match--;
2055 if (max_match == 0)
2056 return false;
2058 e0_last_head = prev_real_insn (e0_last_head);
2059 while (DEBUG_INSN_P (e0_last_head));
2062 if (max_match == 0)
2063 return false;
2065 /* We must find a union of the live registers at each of the end points. */
2066 live = BITMAP_ALLOC (NULL);
2067 live_union = BITMAP_ALLOC (NULL);
2069 currptr = XNEWVEC (rtx, nedges);
2070 headptr = XNEWVEC (rtx, nedges);
2071 nextptr = XNEWVEC (rtx, nedges);
2073 for (ix = 0; ix < nedges; ix++)
2075 int j;
2076 basic_block merge_bb = EDGE_SUCC (bb, ix)->dest;
2077 rtx head = BB_HEAD (merge_bb);
2079 while (!NONDEBUG_INSN_P (head))
2080 head = NEXT_INSN (head);
2081 headptr[ix] = head;
2082 currptr[ix] = head;
2084 /* Compute the end point and live information */
2085 for (j = 1; j < max_match; j++)
2087 head = NEXT_INSN (head);
2088 while (!NONDEBUG_INSN_P (head));
2089 simulate_backwards_to_point (merge_bb, live, head);
2090 IOR_REG_SET (live_union, live);
2093 /* If we're moving across two blocks, verify the validity of the
2094 first move, then adjust the target and let the loop below deal
2095 with the final move. */
2096 if (final_dest_bb != NULL)
2098 rtx move_upto;
2100 moveall = can_move_insns_across (currptr[0], e0_last_head, move_before,
2101 jump, e0->dest, live_union,
2102 NULL, &move_upto);
2103 if (!moveall)
2104 e0_last_head = move_upto;
2105 if (e0_last_head == NULL_RTX)
2106 goto out;
2108 jump = BB_END (final_dest_bb);
2109 cond = get_condition (jump, &move_before, true, false);
2110 if (cond == NULL_RTX)
2111 move_before = jump;
2116 rtx move_upto;
2117 moveall = can_move_insns_across (currptr[0], e0_last_head,
2118 move_before, jump, e0->dest, live_union,
2119 NULL, &move_upto);
2120 if (!moveall && move_upto == NULL_RTX)
2122 if (jump == move_before)
2123 break;
2125 /* Try again, using a different insertion point. */
2126 move_before = jump;
2128 #ifdef HAVE_cc0
2129 /* Don't try moving before a cc0 user, as that may invalidate
2130 the cc0. */
2131 if (reg_mentioned_p (cc0_rtx, jump))
2132 break;
2133 #endif
2135 continue;
2138 if (final_dest_bb && !moveall)
2139 /* We haven't checked whether a partial move would be OK for the first
2140 move, so we have to fail this case. */
2141 break;
2143 changed = true;
2144 for (;;)
2146 if (currptr[0] == move_upto)
2147 break;
2148 for (ix = 0; ix < nedges; ix++)
2150 rtx curr = currptr[ix];
2152 curr = NEXT_INSN (curr);
2153 while (!NONDEBUG_INSN_P (curr));
2154 currptr[ix] = curr;
2158 /* If we can't currently move all of the identical insns, remember
2159 each insn after the range that we'll merge. */
2160 if (!moveall)
2161 for (ix = 0; ix < nedges; ix++)
2163 rtx curr = currptr[ix];
2165 curr = NEXT_INSN (curr);
2166 while (!NONDEBUG_INSN_P (curr));
2167 nextptr[ix] = curr;
2170 reorder_insns (headptr[0], currptr[0], PREV_INSN (move_before));
2171 df_set_bb_dirty (EDGE_SUCC (bb, 0)->dest);
2172 if (final_dest_bb != NULL)
2173 df_set_bb_dirty (final_dest_bb);
2174 df_set_bb_dirty (bb);
2175 for (ix = 1; ix < nedges; ix++)
2177 df_set_bb_dirty (EDGE_SUCC (bb, ix)->dest);
2178 delete_insn_chain (headptr[ix], currptr[ix], false);
2180 if (!moveall)
2182 if (jump == move_before)
2183 break;
2185 /* For the unmerged insns, try a different insertion point. */
2186 move_before = jump;
2188 #ifdef HAVE_cc0
2189 /* Don't try moving before a cc0 user, as that may invalidate
2190 the cc0. */
2191 if (reg_mentioned_p (cc0_rtx, jump))
2192 break;
2193 #endif
2195 for (ix = 0; ix < nedges; ix++)
2196 currptr[ix] = headptr[ix] = nextptr[ix];
2199 while (!moveall);
2201 out:
2202 free (currptr);
2203 free (headptr);
2204 free (nextptr);
2206 crossjumps_occured |= changed;
2208 return changed;
2211 /* Return true if BB contains just bb note, or bb note followed
2212 by only DEBUG_INSNs. */
2214 static bool
2215 trivially_empty_bb_p (basic_block bb)
2217 rtx insn = BB_END (bb);
2219 while (1)
2221 if (insn == BB_HEAD (bb))
2222 return true;
2223 if (!DEBUG_INSN_P (insn))
2224 return false;
2225 insn = PREV_INSN (insn);
2229 /* Do simple CFG optimizations - basic block merging, simplifying of jump
2230 instructions etc. Return nonzero if changes were made. */
2232 static bool
2233 try_optimize_cfg (int mode)
2235 bool changed_overall = false;
2236 bool changed;
2237 int iterations = 0;
2238 basic_block bb, b, next;
2240 if (mode & (CLEANUP_CROSSJUMP | CLEANUP_THREADING))
2241 clear_bb_flags ();
2243 crossjumps_occured = false;
2245 FOR_EACH_BB (bb)
2246 update_forwarder_flag (bb);
2248 if (! targetm.cannot_modify_jumps_p ())
2250 first_pass = true;
2251 /* Attempt to merge blocks as made possible by edge removal. If
2252 a block has only one successor, and the successor has only
2253 one predecessor, they may be combined. */
2256 block_was_dirty = false;
2257 changed = false;
2258 iterations++;
2260 if (dump_file)
2261 fprintf (dump_file,
2262 "\n\ntry_optimize_cfg iteration %i\n\n",
2263 iterations);
2265 for (b = ENTRY_BLOCK_PTR->next_bb; b != EXIT_BLOCK_PTR;)
2267 basic_block c;
2268 edge s;
2269 bool changed_here = false;
2271 /* Delete trivially dead basic blocks. This is either
2272 blocks with no predecessors, or empty blocks with no
2273 successors. However if the empty block with no
2274 successors is the successor of the ENTRY_BLOCK, it is
2275 kept. This ensures that the ENTRY_BLOCK will have a
2276 successor which is a precondition for many RTL
2277 passes. Empty blocks may result from expanding
2278 __builtin_unreachable (). */
2279 if (EDGE_COUNT (b->preds) == 0
2280 || (EDGE_COUNT (b->succs) == 0
2281 && trivially_empty_bb_p (b)
2282 && single_succ_edge (ENTRY_BLOCK_PTR)->dest != b))
2284 c = b->prev_bb;
2285 if (EDGE_COUNT (b->preds) > 0)
2287 edge e;
2288 edge_iterator ei;
2290 if (current_ir_type () == IR_RTL_CFGLAYOUT)
2292 if (b->il.rtl->footer
2293 && BARRIER_P (b->il.rtl->footer))
2294 FOR_EACH_EDGE (e, ei, b->preds)
2295 if ((e->flags & EDGE_FALLTHRU)
2296 && e->src->il.rtl->footer == NULL)
2298 if (b->il.rtl->footer)
2300 e->src->il.rtl->footer = b->il.rtl->footer;
2301 b->il.rtl->footer = NULL;
2303 else
2305 start_sequence ();
2306 e->src->il.rtl->footer = emit_barrier ();
2307 end_sequence ();
2311 else
2313 rtx last = get_last_bb_insn (b);
2314 if (last && BARRIER_P (last))
2315 FOR_EACH_EDGE (e, ei, b->preds)
2316 if ((e->flags & EDGE_FALLTHRU))
2317 emit_barrier_after (BB_END (e->src));
2320 delete_basic_block (b);
2321 if (!(mode & CLEANUP_CFGLAYOUT))
2322 changed = true;
2323 /* Avoid trying to remove ENTRY_BLOCK_PTR. */
2324 b = (c == ENTRY_BLOCK_PTR ? c->next_bb : c);
2325 continue;
2328 /* Remove code labels no longer used. */
2329 if (single_pred_p (b)
2330 && (single_pred_edge (b)->flags & EDGE_FALLTHRU)
2331 && !(single_pred_edge (b)->flags & EDGE_COMPLEX)
2332 && LABEL_P (BB_HEAD (b))
2333 /* If the previous block ends with a branch to this
2334 block, we can't delete the label. Normally this
2335 is a condjump that is yet to be simplified, but
2336 if CASE_DROPS_THRU, this can be a tablejump with
2337 some element going to the same place as the
2338 default (fallthru). */
2339 && (single_pred (b) == ENTRY_BLOCK_PTR
2340 || !JUMP_P (BB_END (single_pred (b)))
2341 || ! label_is_jump_target_p (BB_HEAD (b),
2342 BB_END (single_pred (b)))))
2344 rtx label = BB_HEAD (b);
2346 delete_insn_chain (label, label, false);
2347 /* If the case label is undeletable, move it after the
2348 BASIC_BLOCK note. */
2349 if (NOTE_KIND (BB_HEAD (b)) == NOTE_INSN_DELETED_LABEL)
2351 rtx bb_note = NEXT_INSN (BB_HEAD (b));
2353 reorder_insns_nobb (label, label, bb_note);
2354 BB_HEAD (b) = bb_note;
2355 if (BB_END (b) == bb_note)
2356 BB_END (b) = label;
2358 if (dump_file)
2359 fprintf (dump_file, "Deleted label in block %i.\n",
2360 b->index);
2363 /* If we fall through an empty block, we can remove it. */
2364 if (!(mode & CLEANUP_CFGLAYOUT)
2365 && single_pred_p (b)
2366 && (single_pred_edge (b)->flags & EDGE_FALLTHRU)
2367 && !LABEL_P (BB_HEAD (b))
2368 && FORWARDER_BLOCK_P (b)
2369 /* Note that forwarder_block_p true ensures that
2370 there is a successor for this block. */
2371 && (single_succ_edge (b)->flags & EDGE_FALLTHRU)
2372 && n_basic_blocks > NUM_FIXED_BLOCKS + 1)
2374 if (dump_file)
2375 fprintf (dump_file,
2376 "Deleting fallthru block %i.\n",
2377 b->index);
2379 c = b->prev_bb == ENTRY_BLOCK_PTR ? b->next_bb : b->prev_bb;
2380 redirect_edge_succ_nodup (single_pred_edge (b),
2381 single_succ (b));
2382 delete_basic_block (b);
2383 changed = true;
2384 b = c;
2385 continue;
2388 /* Merge B with its single successor, if any. */
2389 if (single_succ_p (b)
2390 && (s = single_succ_edge (b))
2391 && !(s->flags & EDGE_COMPLEX)
2392 && (c = s->dest) != EXIT_BLOCK_PTR
2393 && single_pred_p (c)
2394 && b != c)
2396 /* When not in cfg_layout mode use code aware of reordering
2397 INSN. This code possibly creates new basic blocks so it
2398 does not fit merge_blocks interface and is kept here in
2399 hope that it will become useless once more of compiler
2400 is transformed to use cfg_layout mode. */
2402 if ((mode & CLEANUP_CFGLAYOUT)
2403 && can_merge_blocks_p (b, c))
2405 merge_blocks (b, c);
2406 update_forwarder_flag (b);
2407 changed_here = true;
2409 else if (!(mode & CLEANUP_CFGLAYOUT)
2410 /* If the jump insn has side effects,
2411 we can't kill the edge. */
2412 && (!JUMP_P (BB_END (b))
2413 || (reload_completed
2414 ? simplejump_p (BB_END (b))
2415 : (onlyjump_p (BB_END (b))
2416 && !tablejump_p (BB_END (b),
2417 NULL, NULL))))
2418 && (next = merge_blocks_move (s, b, c, mode)))
2420 b = next;
2421 changed_here = true;
2425 /* Simplify branch over branch. */
2426 if ((mode & CLEANUP_EXPENSIVE)
2427 && !(mode & CLEANUP_CFGLAYOUT)
2428 && try_simplify_condjump (b))
2429 changed_here = true;
2431 /* If B has a single outgoing edge, but uses a
2432 non-trivial jump instruction without side-effects, we
2433 can either delete the jump entirely, or replace it
2434 with a simple unconditional jump. */
2435 if (single_succ_p (b)
2436 && single_succ (b) != EXIT_BLOCK_PTR
2437 && onlyjump_p (BB_END (b))
2438 && !find_reg_note (BB_END (b), REG_CROSSING_JUMP, NULL_RTX)
2439 && try_redirect_by_replacing_jump (single_succ_edge (b),
2440 single_succ (b),
2441 (mode & CLEANUP_CFGLAYOUT) != 0))
2443 update_forwarder_flag (b);
2444 changed_here = true;
2447 /* Simplify branch to branch. */
2448 if (try_forward_edges (mode, b))
2449 changed_here = true;
2451 /* Look for shared code between blocks. */
2452 if ((mode & CLEANUP_CROSSJUMP)
2453 && try_crossjump_bb (mode, b))
2454 changed_here = true;
2456 if ((mode & CLEANUP_CROSSJUMP)
2457 /* This can lengthen register lifetimes. Do it only after
2458 reload. */
2459 && reload_completed
2460 && try_head_merge_bb (b))
2461 changed_here = true;
2463 /* Don't get confused by the index shift caused by
2464 deleting blocks. */
2465 if (!changed_here)
2466 b = b->next_bb;
2467 else
2468 changed = true;
2471 if ((mode & CLEANUP_CROSSJUMP)
2472 && try_crossjump_bb (mode, EXIT_BLOCK_PTR))
2473 changed = true;
2475 if (block_was_dirty)
2477 /* This should only be set by head-merging. */
2478 gcc_assert (mode & CLEANUP_CROSSJUMP);
2479 df_analyze ();
2482 #ifdef ENABLE_CHECKING
2483 if (changed)
2484 verify_flow_info ();
2485 #endif
2487 changed_overall |= changed;
2488 first_pass = false;
2490 while (changed);
2493 FOR_ALL_BB (b)
2494 b->flags &= ~(BB_FORWARDER_BLOCK | BB_NONTHREADABLE_BLOCK);
2496 return changed_overall;
2499 /* Delete all unreachable basic blocks. */
2501 bool
2502 delete_unreachable_blocks (void)
2504 bool changed = false;
2505 basic_block b, prev_bb;
2507 find_unreachable_blocks ();
2509 /* When we're in GIMPLE mode and there may be debug insns, we should
2510 delete blocks in reverse dominator order, so as to get a chance
2511 to substitute all released DEFs into debug stmts. If we don't
2512 have dominators information, walking blocks backward gets us a
2513 better chance of retaining most debug information than
2514 otherwise. */
2515 if (MAY_HAVE_DEBUG_STMTS && current_ir_type () == IR_GIMPLE
2516 && dom_info_available_p (CDI_DOMINATORS))
2518 for (b = EXIT_BLOCK_PTR->prev_bb; b != ENTRY_BLOCK_PTR; b = prev_bb)
2520 prev_bb = b->prev_bb;
2522 if (!(b->flags & BB_REACHABLE))
2524 /* Speed up the removal of blocks that don't dominate
2525 others. Walking backwards, this should be the common
2526 case. */
2527 if (!first_dom_son (CDI_DOMINATORS, b))
2528 delete_basic_block (b);
2529 else
2531 VEC (basic_block, heap) *h
2532 = get_all_dominated_blocks (CDI_DOMINATORS, b);
2534 while (VEC_length (basic_block, h))
2536 b = VEC_pop (basic_block, h);
2538 prev_bb = b->prev_bb;
2540 gcc_assert (!(b->flags & BB_REACHABLE));
2542 delete_basic_block (b);
2545 VEC_free (basic_block, heap, h);
2548 changed = true;
2552 else
2554 for (b = EXIT_BLOCK_PTR->prev_bb; b != ENTRY_BLOCK_PTR; b = prev_bb)
2556 prev_bb = b->prev_bb;
2558 if (!(b->flags & BB_REACHABLE))
2560 delete_basic_block (b);
2561 changed = true;
2566 if (changed)
2567 tidy_fallthru_edges ();
2568 return changed;
2571 /* Delete any jump tables never referenced. We can't delete them at the
2572 time of removing tablejump insn as they are referenced by the preceding
2573 insns computing the destination, so we delay deleting and garbagecollect
2574 them once life information is computed. */
2575 void
2576 delete_dead_jumptables (void)
2578 basic_block bb;
2580 /* A dead jump table does not belong to any basic block. Scan insns
2581 between two adjacent basic blocks. */
2582 FOR_EACH_BB (bb)
2584 rtx insn, next;
2586 for (insn = NEXT_INSN (BB_END (bb));
2587 insn && !NOTE_INSN_BASIC_BLOCK_P (insn);
2588 insn = next)
2590 next = NEXT_INSN (insn);
2591 if (LABEL_P (insn)
2592 && LABEL_NUSES (insn) == LABEL_PRESERVE_P (insn)
2593 && JUMP_TABLE_DATA_P (next))
2595 rtx label = insn, jump = next;
2597 if (dump_file)
2598 fprintf (dump_file, "Dead jumptable %i removed\n",
2599 INSN_UID (insn));
2601 next = NEXT_INSN (next);
2602 delete_insn (jump);
2603 delete_insn (label);
2610 /* Tidy the CFG by deleting unreachable code and whatnot. */
2612 bool
2613 cleanup_cfg (int mode)
2615 bool changed = false;
2617 /* Set the cfglayout mode flag here. We could update all the callers
2618 but that is just inconvenient, especially given that we eventually
2619 want to have cfglayout mode as the default. */
2620 if (current_ir_type () == IR_RTL_CFGLAYOUT)
2621 mode |= CLEANUP_CFGLAYOUT;
2623 timevar_push (TV_CLEANUP_CFG);
2624 if (delete_unreachable_blocks ())
2626 changed = true;
2627 /* We've possibly created trivially dead code. Cleanup it right
2628 now to introduce more opportunities for try_optimize_cfg. */
2629 if (!(mode & (CLEANUP_NO_INSN_DEL))
2630 && !reload_completed)
2631 delete_trivially_dead_insns (get_insns (), max_reg_num ());
2634 compact_blocks ();
2636 /* To tail-merge blocks ending in the same noreturn function (e.g.
2637 a call to abort) we have to insert fake edges to exit. Do this
2638 here once. The fake edges do not interfere with any other CFG
2639 cleanups. */
2640 if (mode & CLEANUP_CROSSJUMP)
2641 add_noreturn_fake_exit_edges ();
2643 if (!dbg_cnt (cfg_cleanup))
2644 return changed;
2646 while (try_optimize_cfg (mode))
2648 delete_unreachable_blocks (), changed = true;
2649 if (!(mode & CLEANUP_NO_INSN_DEL))
2651 /* Try to remove some trivially dead insns when doing an expensive
2652 cleanup. But delete_trivially_dead_insns doesn't work after
2653 reload (it only handles pseudos) and run_fast_dce is too costly
2654 to run in every iteration.
2656 For effective cross jumping, we really want to run a fast DCE to
2657 clean up any dead conditions, or they get in the way of performing
2658 useful tail merges.
2660 Other transformations in cleanup_cfg are not so sensitive to dead
2661 code, so delete_trivially_dead_insns or even doing nothing at all
2662 is good enough. */
2663 if ((mode & CLEANUP_EXPENSIVE) && !reload_completed
2664 && !delete_trivially_dead_insns (get_insns (), max_reg_num ()))
2665 break;
2666 if ((mode & CLEANUP_CROSSJUMP) && crossjumps_occured)
2667 run_fast_dce ();
2669 else
2670 break;
2673 if (mode & CLEANUP_CROSSJUMP)
2674 remove_fake_exit_edges ();
2676 /* Don't call delete_dead_jumptables in cfglayout mode, because
2677 that function assumes that jump tables are in the insns stream.
2678 But we also don't _have_ to delete dead jumptables in cfglayout
2679 mode because we shouldn't even be looking at things that are
2680 not in a basic block. Dead jumptables are cleaned up when
2681 going out of cfglayout mode. */
2682 if (!(mode & CLEANUP_CFGLAYOUT))
2683 delete_dead_jumptables ();
2685 timevar_pop (TV_CLEANUP_CFG);
2687 return changed;
2690 static unsigned int
2691 rest_of_handle_jump (void)
2693 if (crtl->tail_call_emit)
2694 fixup_tail_calls ();
2695 return 0;
2698 struct rtl_opt_pass pass_jump =
2701 RTL_PASS,
2702 "sibling", /* name */
2703 NULL, /* gate */
2704 rest_of_handle_jump, /* execute */
2705 NULL, /* sub */
2706 NULL, /* next */
2707 0, /* static_pass_number */
2708 TV_JUMP, /* tv_id */
2709 0, /* properties_required */
2710 0, /* properties_provided */
2711 0, /* properties_destroyed */
2712 TODO_ggc_collect, /* todo_flags_start */
2713 TODO_verify_flow, /* todo_flags_finish */
2718 static unsigned int
2719 rest_of_handle_jump2 (void)
2721 delete_trivially_dead_insns (get_insns (), max_reg_num ());
2722 if (dump_file)
2723 dump_flow_info (dump_file, dump_flags);
2724 cleanup_cfg ((optimize ? CLEANUP_EXPENSIVE : 0)
2725 | (flag_thread_jumps ? CLEANUP_THREADING : 0));
2726 return 0;
2730 struct rtl_opt_pass pass_jump2 =
2733 RTL_PASS,
2734 "jump", /* name */
2735 NULL, /* gate */
2736 rest_of_handle_jump2, /* execute */
2737 NULL, /* sub */
2738 NULL, /* next */
2739 0, /* static_pass_number */
2740 TV_JUMP, /* tv_id */
2741 0, /* properties_required */
2742 0, /* properties_provided */
2743 0, /* properties_destroyed */
2744 TODO_ggc_collect, /* todo_flags_start */
2745 TODO_dump_func | TODO_verify_rtl_sharing,/* todo_flags_finish */