* gcc.dg/store-motion-fgcse-sm.c (dg-final): Cleanup
[official-gcc.git] / gcc / cfgcleanup.c
blob2ea0126a49f79285888a4f3543523b40963b76d3
1 /* Control flow optimization code for GNU compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file contains optimizer of the control flow. The main entry point is
21 cleanup_cfg. Following optimizations are performed:
23 - Unreachable blocks removal
24 - Edge forwarding (edge to the forwarder block is forwarded to its
25 successor. Simplification of the branch instruction is performed by
26 underlying infrastructure so branch can be converted to simplejump or
27 eliminated).
28 - Cross jumping (tail merging)
29 - Conditional jump-around-simplejump simplification
30 - Basic block merging. */
32 #include "config.h"
33 #include "system.h"
34 #include "coretypes.h"
35 #include "tm.h"
36 #include "rtl.h"
37 #include "tree.h"
38 #include "hard-reg-set.h"
39 #include "regs.h"
40 #include "insn-config.h"
41 #include "flags.h"
42 #include "recog.h"
43 #include "diagnostic-core.h"
44 #include "cselib.h"
45 #include "params.h"
46 #include "tm_p.h"
47 #include "target.h"
48 #include "hashtab.h"
49 #include "hash-set.h"
50 #include "vec.h"
51 #include "machmode.h"
52 #include "input.h"
53 #include "function.h" /* For inline functions in emit-rtl.h they need crtl. */
54 #include "emit-rtl.h"
55 #include "tree-pass.h"
56 #include "cfgloop.h"
57 #include "expr.h"
58 #include "dominance.h"
59 #include "cfg.h"
60 #include "cfgrtl.h"
61 #include "cfganal.h"
62 #include "cfgbuild.h"
63 #include "cfgcleanup.h"
64 #include "predict.h"
65 #include "basic-block.h"
66 #include "df.h"
67 #include "dce.h"
68 #include "dbgcnt.h"
69 #include "emit-rtl.h"
70 #include "rtl-iter.h"
72 #define FORWARDER_BLOCK_P(BB) ((BB)->flags & BB_FORWARDER_BLOCK)
74 /* Set to true when we are running first pass of try_optimize_cfg loop. */
75 static bool first_pass;
77 /* Set to true if crossjumps occurred in the latest run of try_optimize_cfg. */
78 static bool crossjumps_occured;
80 /* Set to true if we couldn't run an optimization due to stale liveness
81 information; we should run df_analyze to enable more opportunities. */
82 static bool block_was_dirty;
84 static bool try_crossjump_to_edge (int, edge, edge, enum replace_direction);
85 static bool try_crossjump_bb (int, basic_block);
86 static bool outgoing_edges_match (int, basic_block, basic_block);
87 static enum replace_direction old_insns_match_p (int, rtx_insn *, rtx_insn *);
89 static void merge_blocks_move_predecessor_nojumps (basic_block, basic_block);
90 static void merge_blocks_move_successor_nojumps (basic_block, basic_block);
91 static bool try_optimize_cfg (int);
92 static bool try_simplify_condjump (basic_block);
93 static bool try_forward_edges (int, basic_block);
94 static edge thread_jump (edge, basic_block);
95 static bool mark_effect (rtx, bitmap);
96 static void notice_new_block (basic_block);
97 static void update_forwarder_flag (basic_block);
98 static void merge_memattrs (rtx, rtx);
100 /* Set flags for newly created block. */
102 static void
103 notice_new_block (basic_block bb)
105 if (!bb)
106 return;
108 if (forwarder_block_p (bb))
109 bb->flags |= BB_FORWARDER_BLOCK;
112 /* Recompute forwarder flag after block has been modified. */
114 static void
115 update_forwarder_flag (basic_block bb)
117 if (forwarder_block_p (bb))
118 bb->flags |= BB_FORWARDER_BLOCK;
119 else
120 bb->flags &= ~BB_FORWARDER_BLOCK;
123 /* Simplify a conditional jump around an unconditional jump.
124 Return true if something changed. */
126 static bool
127 try_simplify_condjump (basic_block cbranch_block)
129 basic_block jump_block, jump_dest_block, cbranch_dest_block;
130 edge cbranch_jump_edge, cbranch_fallthru_edge;
131 rtx_insn *cbranch_insn;
133 /* Verify that there are exactly two successors. */
134 if (EDGE_COUNT (cbranch_block->succs) != 2)
135 return false;
137 /* Verify that we've got a normal conditional branch at the end
138 of the block. */
139 cbranch_insn = BB_END (cbranch_block);
140 if (!any_condjump_p (cbranch_insn))
141 return false;
143 cbranch_fallthru_edge = FALLTHRU_EDGE (cbranch_block);
144 cbranch_jump_edge = BRANCH_EDGE (cbranch_block);
146 /* The next block must not have multiple predecessors, must not
147 be the last block in the function, and must contain just the
148 unconditional jump. */
149 jump_block = cbranch_fallthru_edge->dest;
150 if (!single_pred_p (jump_block)
151 || jump_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
152 || !FORWARDER_BLOCK_P (jump_block))
153 return false;
154 jump_dest_block = single_succ (jump_block);
156 /* If we are partitioning hot/cold basic blocks, we don't want to
157 mess up unconditional or indirect jumps that cross between hot
158 and cold sections.
160 Basic block partitioning may result in some jumps that appear to
161 be optimizable (or blocks that appear to be mergeable), but which really
162 must be left untouched (they are required to make it safely across
163 partition boundaries). See the comments at the top of
164 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
166 if (BB_PARTITION (jump_block) != BB_PARTITION (jump_dest_block)
167 || (cbranch_jump_edge->flags & EDGE_CROSSING))
168 return false;
170 /* The conditional branch must target the block after the
171 unconditional branch. */
172 cbranch_dest_block = cbranch_jump_edge->dest;
174 if (cbranch_dest_block == EXIT_BLOCK_PTR_FOR_FN (cfun)
175 || !can_fallthru (jump_block, cbranch_dest_block))
176 return false;
178 /* Invert the conditional branch. */
179 if (!invert_jump (cbranch_insn, block_label (jump_dest_block), 0))
180 return false;
182 if (dump_file)
183 fprintf (dump_file, "Simplifying condjump %i around jump %i\n",
184 INSN_UID (cbranch_insn), INSN_UID (BB_END (jump_block)));
186 /* Success. Update the CFG to match. Note that after this point
187 the edge variable names appear backwards; the redirection is done
188 this way to preserve edge profile data. */
189 cbranch_jump_edge = redirect_edge_succ_nodup (cbranch_jump_edge,
190 cbranch_dest_block);
191 cbranch_fallthru_edge = redirect_edge_succ_nodup (cbranch_fallthru_edge,
192 jump_dest_block);
193 cbranch_jump_edge->flags |= EDGE_FALLTHRU;
194 cbranch_fallthru_edge->flags &= ~EDGE_FALLTHRU;
195 update_br_prob_note (cbranch_block);
197 /* Delete the block with the unconditional jump, and clean up the mess. */
198 delete_basic_block (jump_block);
199 tidy_fallthru_edge (cbranch_jump_edge);
200 update_forwarder_flag (cbranch_block);
202 return true;
205 /* Attempt to prove that operation is NOOP using CSElib or mark the effect
206 on register. Used by jump threading. */
208 static bool
209 mark_effect (rtx exp, regset nonequal)
211 int regno;
212 rtx dest;
213 switch (GET_CODE (exp))
215 /* In case we do clobber the register, mark it as equal, as we know the
216 value is dead so it don't have to match. */
217 case CLOBBER:
218 if (REG_P (XEXP (exp, 0)))
220 dest = XEXP (exp, 0);
221 regno = REGNO (dest);
222 if (HARD_REGISTER_NUM_P (regno))
223 bitmap_clear_range (nonequal, regno,
224 hard_regno_nregs[regno][GET_MODE (dest)]);
225 else
226 bitmap_clear_bit (nonequal, regno);
228 return false;
230 case SET:
231 if (rtx_equal_for_cselib_p (SET_DEST (exp), SET_SRC (exp)))
232 return false;
233 dest = SET_DEST (exp);
234 if (dest == pc_rtx)
235 return false;
236 if (!REG_P (dest))
237 return true;
238 regno = REGNO (dest);
239 if (HARD_REGISTER_NUM_P (regno))
240 bitmap_set_range (nonequal, regno,
241 hard_regno_nregs[regno][GET_MODE (dest)]);
242 else
243 bitmap_set_bit (nonequal, regno);
244 return false;
246 default:
247 return false;
251 /* Return true if X contains a register in NONEQUAL. */
252 static bool
253 mentions_nonequal_regs (const_rtx x, regset nonequal)
255 subrtx_iterator::array_type array;
256 FOR_EACH_SUBRTX (iter, array, x, NONCONST)
258 const_rtx x = *iter;
259 if (REG_P (x))
261 unsigned int regno = REGNO (x);
262 if (REGNO_REG_SET_P (nonequal, regno))
263 return true;
264 if (regno < FIRST_PSEUDO_REGISTER)
266 int n = hard_regno_nregs[regno][GET_MODE (x)];
267 while (--n > 0)
268 if (REGNO_REG_SET_P (nonequal, regno + n))
269 return true;
273 return false;
276 /* Attempt to prove that the basic block B will have no side effects and
277 always continues in the same edge if reached via E. Return the edge
278 if exist, NULL otherwise. */
280 static edge
281 thread_jump (edge e, basic_block b)
283 rtx set1, set2, cond1, cond2;
284 rtx_insn *insn;
285 enum rtx_code code1, code2, reversed_code2;
286 bool reverse1 = false;
287 unsigned i;
288 regset nonequal;
289 bool failed = false;
290 reg_set_iterator rsi;
292 if (b->flags & BB_NONTHREADABLE_BLOCK)
293 return NULL;
295 /* At the moment, we do handle only conditional jumps, but later we may
296 want to extend this code to tablejumps and others. */
297 if (EDGE_COUNT (e->src->succs) != 2)
298 return NULL;
299 if (EDGE_COUNT (b->succs) != 2)
301 b->flags |= BB_NONTHREADABLE_BLOCK;
302 return NULL;
305 /* Second branch must end with onlyjump, as we will eliminate the jump. */
306 if (!any_condjump_p (BB_END (e->src)))
307 return NULL;
309 if (!any_condjump_p (BB_END (b)) || !onlyjump_p (BB_END (b)))
311 b->flags |= BB_NONTHREADABLE_BLOCK;
312 return NULL;
315 set1 = pc_set (BB_END (e->src));
316 set2 = pc_set (BB_END (b));
317 if (((e->flags & EDGE_FALLTHRU) != 0)
318 != (XEXP (SET_SRC (set1), 1) == pc_rtx))
319 reverse1 = true;
321 cond1 = XEXP (SET_SRC (set1), 0);
322 cond2 = XEXP (SET_SRC (set2), 0);
323 if (reverse1)
324 code1 = reversed_comparison_code (cond1, BB_END (e->src));
325 else
326 code1 = GET_CODE (cond1);
328 code2 = GET_CODE (cond2);
329 reversed_code2 = reversed_comparison_code (cond2, BB_END (b));
331 if (!comparison_dominates_p (code1, code2)
332 && !comparison_dominates_p (code1, reversed_code2))
333 return NULL;
335 /* Ensure that the comparison operators are equivalent.
336 ??? This is far too pessimistic. We should allow swapped operands,
337 different CCmodes, or for example comparisons for interval, that
338 dominate even when operands are not equivalent. */
339 if (!rtx_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
340 || !rtx_equal_p (XEXP (cond1, 1), XEXP (cond2, 1)))
341 return NULL;
343 /* Short circuit cases where block B contains some side effects, as we can't
344 safely bypass it. */
345 for (insn = NEXT_INSN (BB_HEAD (b)); insn != NEXT_INSN (BB_END (b));
346 insn = NEXT_INSN (insn))
347 if (INSN_P (insn) && side_effects_p (PATTERN (insn)))
349 b->flags |= BB_NONTHREADABLE_BLOCK;
350 return NULL;
353 cselib_init (0);
355 /* First process all values computed in the source basic block. */
356 for (insn = NEXT_INSN (BB_HEAD (e->src));
357 insn != NEXT_INSN (BB_END (e->src));
358 insn = NEXT_INSN (insn))
359 if (INSN_P (insn))
360 cselib_process_insn (insn);
362 nonequal = BITMAP_ALLOC (NULL);
363 CLEAR_REG_SET (nonequal);
365 /* Now assume that we've continued by the edge E to B and continue
366 processing as if it were same basic block.
367 Our goal is to prove that whole block is an NOOP. */
369 for (insn = NEXT_INSN (BB_HEAD (b));
370 insn != NEXT_INSN (BB_END (b)) && !failed;
371 insn = NEXT_INSN (insn))
373 if (INSN_P (insn))
375 rtx pat = PATTERN (insn);
377 if (GET_CODE (pat) == PARALLEL)
379 for (i = 0; i < (unsigned)XVECLEN (pat, 0); i++)
380 failed |= mark_effect (XVECEXP (pat, 0, i), nonequal);
382 else
383 failed |= mark_effect (pat, nonequal);
386 cselib_process_insn (insn);
389 /* Later we should clear nonequal of dead registers. So far we don't
390 have life information in cfg_cleanup. */
391 if (failed)
393 b->flags |= BB_NONTHREADABLE_BLOCK;
394 goto failed_exit;
397 /* cond2 must not mention any register that is not equal to the
398 former block. */
399 if (mentions_nonequal_regs (cond2, nonequal))
400 goto failed_exit;
402 EXECUTE_IF_SET_IN_REG_SET (nonequal, 0, i, rsi)
403 goto failed_exit;
405 BITMAP_FREE (nonequal);
406 cselib_finish ();
407 if ((comparison_dominates_p (code1, code2) != 0)
408 != (XEXP (SET_SRC (set2), 1) == pc_rtx))
409 return BRANCH_EDGE (b);
410 else
411 return FALLTHRU_EDGE (b);
413 failed_exit:
414 BITMAP_FREE (nonequal);
415 cselib_finish ();
416 return NULL;
419 /* Attempt to forward edges leaving basic block B.
420 Return true if successful. */
422 static bool
423 try_forward_edges (int mode, basic_block b)
425 bool changed = false;
426 edge_iterator ei;
427 edge e, *threaded_edges = NULL;
429 /* If we are partitioning hot/cold basic blocks, we don't want to
430 mess up unconditional or indirect jumps that cross between hot
431 and cold sections.
433 Basic block partitioning may result in some jumps that appear to
434 be optimizable (or blocks that appear to be mergeable), but which really
435 must be left untouched (they are required to make it safely across
436 partition boundaries). See the comments at the top of
437 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
439 if (JUMP_P (BB_END (b)) && CROSSING_JUMP_P (BB_END (b)))
440 return false;
442 for (ei = ei_start (b->succs); (e = ei_safe_edge (ei)); )
444 basic_block target, first;
445 location_t goto_locus;
446 int counter;
447 bool threaded = false;
448 int nthreaded_edges = 0;
449 bool may_thread = first_pass || (b->flags & BB_MODIFIED) != 0;
451 /* Skip complex edges because we don't know how to update them.
453 Still handle fallthru edges, as we can succeed to forward fallthru
454 edge to the same place as the branch edge of conditional branch
455 and turn conditional branch to an unconditional branch. */
456 if (e->flags & EDGE_COMPLEX)
458 ei_next (&ei);
459 continue;
462 target = first = e->dest;
463 counter = NUM_FIXED_BLOCKS;
464 goto_locus = e->goto_locus;
466 /* If we are partitioning hot/cold basic_blocks, we don't want to mess
467 up jumps that cross between hot/cold sections.
469 Basic block partitioning may result in some jumps that appear
470 to be optimizable (or blocks that appear to be mergeable), but which
471 really must be left untouched (they are required to make it safely
472 across partition boundaries). See the comments at the top of
473 bb-reorder.c:partition_hot_cold_basic_blocks for complete
474 details. */
476 if (first != EXIT_BLOCK_PTR_FOR_FN (cfun)
477 && JUMP_P (BB_END (first))
478 && CROSSING_JUMP_P (BB_END (first)))
479 return changed;
481 while (counter < n_basic_blocks_for_fn (cfun))
483 basic_block new_target = NULL;
484 bool new_target_threaded = false;
485 may_thread |= (target->flags & BB_MODIFIED) != 0;
487 if (FORWARDER_BLOCK_P (target)
488 && !(single_succ_edge (target)->flags & EDGE_CROSSING)
489 && single_succ (target) != EXIT_BLOCK_PTR_FOR_FN (cfun))
491 /* Bypass trivial infinite loops. */
492 new_target = single_succ (target);
493 if (target == new_target)
494 counter = n_basic_blocks_for_fn (cfun);
495 else if (!optimize)
497 /* When not optimizing, ensure that edges or forwarder
498 blocks with different locus are not optimized out. */
499 location_t new_locus = single_succ_edge (target)->goto_locus;
500 location_t locus = goto_locus;
502 if (LOCATION_LOCUS (new_locus) != UNKNOWN_LOCATION
503 && LOCATION_LOCUS (locus) != UNKNOWN_LOCATION
504 && new_locus != locus)
505 new_target = NULL;
506 else
508 if (LOCATION_LOCUS (new_locus) != UNKNOWN_LOCATION)
509 locus = new_locus;
511 rtx_insn *last = BB_END (target);
512 if (DEBUG_INSN_P (last))
513 last = prev_nondebug_insn (last);
514 if (last && INSN_P (last))
515 new_locus = INSN_LOCATION (last);
516 else
517 new_locus = UNKNOWN_LOCATION;
519 if (LOCATION_LOCUS (new_locus) != UNKNOWN_LOCATION
520 && LOCATION_LOCUS (locus) != UNKNOWN_LOCATION
521 && new_locus != locus)
522 new_target = NULL;
523 else
525 if (LOCATION_LOCUS (new_locus) != UNKNOWN_LOCATION)
526 locus = new_locus;
528 goto_locus = locus;
534 /* Allow to thread only over one edge at time to simplify updating
535 of probabilities. */
536 else if ((mode & CLEANUP_THREADING) && may_thread)
538 edge t = thread_jump (e, target);
539 if (t)
541 if (!threaded_edges)
542 threaded_edges = XNEWVEC (edge,
543 n_basic_blocks_for_fn (cfun));
544 else
546 int i;
548 /* Detect an infinite loop across blocks not
549 including the start block. */
550 for (i = 0; i < nthreaded_edges; ++i)
551 if (threaded_edges[i] == t)
552 break;
553 if (i < nthreaded_edges)
555 counter = n_basic_blocks_for_fn (cfun);
556 break;
560 /* Detect an infinite loop across the start block. */
561 if (t->dest == b)
562 break;
564 gcc_assert (nthreaded_edges
565 < (n_basic_blocks_for_fn (cfun)
566 - NUM_FIXED_BLOCKS));
567 threaded_edges[nthreaded_edges++] = t;
569 new_target = t->dest;
570 new_target_threaded = true;
574 if (!new_target)
575 break;
577 counter++;
578 target = new_target;
579 threaded |= new_target_threaded;
582 if (counter >= n_basic_blocks_for_fn (cfun))
584 if (dump_file)
585 fprintf (dump_file, "Infinite loop in BB %i.\n",
586 target->index);
588 else if (target == first)
589 ; /* We didn't do anything. */
590 else
592 /* Save the values now, as the edge may get removed. */
593 gcov_type edge_count = e->count;
594 int edge_probability = e->probability;
595 int edge_frequency;
596 int n = 0;
598 e->goto_locus = goto_locus;
600 /* Don't force if target is exit block. */
601 if (threaded && target != EXIT_BLOCK_PTR_FOR_FN (cfun))
603 notice_new_block (redirect_edge_and_branch_force (e, target));
604 if (dump_file)
605 fprintf (dump_file, "Conditionals threaded.\n");
607 else if (!redirect_edge_and_branch (e, target))
609 if (dump_file)
610 fprintf (dump_file,
611 "Forwarding edge %i->%i to %i failed.\n",
612 b->index, e->dest->index, target->index);
613 ei_next (&ei);
614 continue;
617 /* We successfully forwarded the edge. Now update profile
618 data: for each edge we traversed in the chain, remove
619 the original edge's execution count. */
620 edge_frequency = apply_probability (b->frequency, edge_probability);
624 edge t;
626 if (!single_succ_p (first))
628 gcc_assert (n < nthreaded_edges);
629 t = threaded_edges [n++];
630 gcc_assert (t->src == first);
631 update_bb_profile_for_threading (first, edge_frequency,
632 edge_count, t);
633 update_br_prob_note (first);
635 else
637 first->count -= edge_count;
638 if (first->count < 0)
639 first->count = 0;
640 first->frequency -= edge_frequency;
641 if (first->frequency < 0)
642 first->frequency = 0;
643 /* It is possible that as the result of
644 threading we've removed edge as it is
645 threaded to the fallthru edge. Avoid
646 getting out of sync. */
647 if (n < nthreaded_edges
648 && first == threaded_edges [n]->src)
649 n++;
650 t = single_succ_edge (first);
653 t->count -= edge_count;
654 if (t->count < 0)
655 t->count = 0;
656 first = t->dest;
658 while (first != target);
660 changed = true;
661 continue;
663 ei_next (&ei);
666 free (threaded_edges);
667 return changed;
671 /* Blocks A and B are to be merged into a single block. A has no incoming
672 fallthru edge, so it can be moved before B without adding or modifying
673 any jumps (aside from the jump from A to B). */
675 static void
676 merge_blocks_move_predecessor_nojumps (basic_block a, basic_block b)
678 rtx_insn *barrier;
680 /* If we are partitioning hot/cold basic blocks, we don't want to
681 mess up unconditional or indirect jumps that cross between hot
682 and cold sections.
684 Basic block partitioning may result in some jumps that appear to
685 be optimizable (or blocks that appear to be mergeable), but which really
686 must be left untouched (they are required to make it safely across
687 partition boundaries). See the comments at the top of
688 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
690 if (BB_PARTITION (a) != BB_PARTITION (b))
691 return;
693 barrier = next_nonnote_insn (BB_END (a));
694 gcc_assert (BARRIER_P (barrier));
695 delete_insn (barrier);
697 /* Scramble the insn chain. */
698 if (BB_END (a) != PREV_INSN (BB_HEAD (b)))
699 reorder_insns_nobb (BB_HEAD (a), BB_END (a), PREV_INSN (BB_HEAD (b)));
700 df_set_bb_dirty (a);
702 if (dump_file)
703 fprintf (dump_file, "Moved block %d before %d and merged.\n",
704 a->index, b->index);
706 /* Swap the records for the two blocks around. */
708 unlink_block (a);
709 link_block (a, b->prev_bb);
711 /* Now blocks A and B are contiguous. Merge them. */
712 merge_blocks (a, b);
715 /* Blocks A and B are to be merged into a single block. B has no outgoing
716 fallthru edge, so it can be moved after A without adding or modifying
717 any jumps (aside from the jump from A to B). */
719 static void
720 merge_blocks_move_successor_nojumps (basic_block a, basic_block b)
722 rtx_insn *barrier, *real_b_end;
723 rtx label;
724 rtx_jump_table_data *table;
726 /* If we are partitioning hot/cold basic blocks, we don't want to
727 mess up unconditional or indirect jumps that cross between hot
728 and cold sections.
730 Basic block partitioning may result in some jumps that appear to
731 be optimizable (or blocks that appear to be mergeable), but which really
732 must be left untouched (they are required to make it safely across
733 partition boundaries). See the comments at the top of
734 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
736 if (BB_PARTITION (a) != BB_PARTITION (b))
737 return;
739 real_b_end = BB_END (b);
741 /* If there is a jump table following block B temporarily add the jump table
742 to block B so that it will also be moved to the correct location. */
743 if (tablejump_p (BB_END (b), &label, &table)
744 && prev_active_insn (label) == BB_END (b))
746 BB_END (b) = table;
749 /* There had better have been a barrier there. Delete it. */
750 barrier = NEXT_INSN (BB_END (b));
751 if (barrier && BARRIER_P (barrier))
752 delete_insn (barrier);
755 /* Scramble the insn chain. */
756 reorder_insns_nobb (BB_HEAD (b), BB_END (b), BB_END (a));
758 /* Restore the real end of b. */
759 BB_END (b) = real_b_end;
761 if (dump_file)
762 fprintf (dump_file, "Moved block %d after %d and merged.\n",
763 b->index, a->index);
765 /* Now blocks A and B are contiguous. Merge them. */
766 merge_blocks (a, b);
769 /* Attempt to merge basic blocks that are potentially non-adjacent.
770 Return NULL iff the attempt failed, otherwise return basic block
771 where cleanup_cfg should continue. Because the merging commonly
772 moves basic block away or introduces another optimization
773 possibility, return basic block just before B so cleanup_cfg don't
774 need to iterate.
776 It may be good idea to return basic block before C in the case
777 C has been moved after B and originally appeared earlier in the
778 insn sequence, but we have no information available about the
779 relative ordering of these two. Hopefully it is not too common. */
781 static basic_block
782 merge_blocks_move (edge e, basic_block b, basic_block c, int mode)
784 basic_block next;
786 /* If we are partitioning hot/cold basic blocks, we don't want to
787 mess up unconditional or indirect jumps that cross between hot
788 and cold sections.
790 Basic block partitioning may result in some jumps that appear to
791 be optimizable (or blocks that appear to be mergeable), but which really
792 must be left untouched (they are required to make it safely across
793 partition boundaries). See the comments at the top of
794 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
796 if (BB_PARTITION (b) != BB_PARTITION (c))
797 return NULL;
799 /* If B has a fallthru edge to C, no need to move anything. */
800 if (e->flags & EDGE_FALLTHRU)
802 int b_index = b->index, c_index = c->index;
804 /* Protect the loop latches. */
805 if (current_loops && c->loop_father->latch == c)
806 return NULL;
808 merge_blocks (b, c);
809 update_forwarder_flag (b);
811 if (dump_file)
812 fprintf (dump_file, "Merged %d and %d without moving.\n",
813 b_index, c_index);
815 return b->prev_bb == ENTRY_BLOCK_PTR_FOR_FN (cfun) ? b : b->prev_bb;
818 /* Otherwise we will need to move code around. Do that only if expensive
819 transformations are allowed. */
820 else if (mode & CLEANUP_EXPENSIVE)
822 edge tmp_edge, b_fallthru_edge;
823 bool c_has_outgoing_fallthru;
824 bool b_has_incoming_fallthru;
826 /* Avoid overactive code motion, as the forwarder blocks should be
827 eliminated by edge redirection instead. One exception might have
828 been if B is a forwarder block and C has no fallthru edge, but
829 that should be cleaned up by bb-reorder instead. */
830 if (FORWARDER_BLOCK_P (b) || FORWARDER_BLOCK_P (c))
831 return NULL;
833 /* We must make sure to not munge nesting of lexical blocks,
834 and loop notes. This is done by squeezing out all the notes
835 and leaving them there to lie. Not ideal, but functional. */
837 tmp_edge = find_fallthru_edge (c->succs);
838 c_has_outgoing_fallthru = (tmp_edge != NULL);
840 tmp_edge = find_fallthru_edge (b->preds);
841 b_has_incoming_fallthru = (tmp_edge != NULL);
842 b_fallthru_edge = tmp_edge;
843 next = b->prev_bb;
844 if (next == c)
845 next = next->prev_bb;
847 /* Otherwise, we're going to try to move C after B. If C does
848 not have an outgoing fallthru, then it can be moved
849 immediately after B without introducing or modifying jumps. */
850 if (! c_has_outgoing_fallthru)
852 merge_blocks_move_successor_nojumps (b, c);
853 return next == ENTRY_BLOCK_PTR_FOR_FN (cfun) ? next->next_bb : next;
856 /* If B does not have an incoming fallthru, then it can be moved
857 immediately before C without introducing or modifying jumps.
858 C cannot be the first block, so we do not have to worry about
859 accessing a non-existent block. */
861 if (b_has_incoming_fallthru)
863 basic_block bb;
865 if (b_fallthru_edge->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
866 return NULL;
867 bb = force_nonfallthru (b_fallthru_edge);
868 if (bb)
869 notice_new_block (bb);
872 merge_blocks_move_predecessor_nojumps (b, c);
873 return next == ENTRY_BLOCK_PTR_FOR_FN (cfun) ? next->next_bb : next;
876 return NULL;
880 /* Removes the memory attributes of MEM expression
881 if they are not equal. */
883 static void
884 merge_memattrs (rtx x, rtx y)
886 int i;
887 int j;
888 enum rtx_code code;
889 const char *fmt;
891 if (x == y)
892 return;
893 if (x == 0 || y == 0)
894 return;
896 code = GET_CODE (x);
898 if (code != GET_CODE (y))
899 return;
901 if (GET_MODE (x) != GET_MODE (y))
902 return;
904 if (code == MEM && !mem_attrs_eq_p (MEM_ATTRS (x), MEM_ATTRS (y)))
906 if (! MEM_ATTRS (x))
907 MEM_ATTRS (y) = 0;
908 else if (! MEM_ATTRS (y))
909 MEM_ATTRS (x) = 0;
910 else
912 HOST_WIDE_INT mem_size;
914 if (MEM_ALIAS_SET (x) != MEM_ALIAS_SET (y))
916 set_mem_alias_set (x, 0);
917 set_mem_alias_set (y, 0);
920 if (! mem_expr_equal_p (MEM_EXPR (x), MEM_EXPR (y)))
922 set_mem_expr (x, 0);
923 set_mem_expr (y, 0);
924 clear_mem_offset (x);
925 clear_mem_offset (y);
927 else if (MEM_OFFSET_KNOWN_P (x) != MEM_OFFSET_KNOWN_P (y)
928 || (MEM_OFFSET_KNOWN_P (x)
929 && MEM_OFFSET (x) != MEM_OFFSET (y)))
931 clear_mem_offset (x);
932 clear_mem_offset (y);
935 if (MEM_SIZE_KNOWN_P (x) && MEM_SIZE_KNOWN_P (y))
937 mem_size = MAX (MEM_SIZE (x), MEM_SIZE (y));
938 set_mem_size (x, mem_size);
939 set_mem_size (y, mem_size);
941 else
943 clear_mem_size (x);
944 clear_mem_size (y);
947 set_mem_align (x, MIN (MEM_ALIGN (x), MEM_ALIGN (y)));
948 set_mem_align (y, MEM_ALIGN (x));
951 if (code == MEM)
953 if (MEM_READONLY_P (x) != MEM_READONLY_P (y))
955 MEM_READONLY_P (x) = 0;
956 MEM_READONLY_P (y) = 0;
958 if (MEM_NOTRAP_P (x) != MEM_NOTRAP_P (y))
960 MEM_NOTRAP_P (x) = 0;
961 MEM_NOTRAP_P (y) = 0;
963 if (MEM_VOLATILE_P (x) != MEM_VOLATILE_P (y))
965 MEM_VOLATILE_P (x) = 1;
966 MEM_VOLATILE_P (y) = 1;
970 fmt = GET_RTX_FORMAT (code);
971 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
973 switch (fmt[i])
975 case 'E':
976 /* Two vectors must have the same length. */
977 if (XVECLEN (x, i) != XVECLEN (y, i))
978 return;
980 for (j = 0; j < XVECLEN (x, i); j++)
981 merge_memattrs (XVECEXP (x, i, j), XVECEXP (y, i, j));
983 break;
985 case 'e':
986 merge_memattrs (XEXP (x, i), XEXP (y, i));
989 return;
993 /* Checks if patterns P1 and P2 are equivalent, apart from the possibly
994 different single sets S1 and S2. */
996 static bool
997 equal_different_set_p (rtx p1, rtx s1, rtx p2, rtx s2)
999 int i;
1000 rtx e1, e2;
1002 if (p1 == s1 && p2 == s2)
1003 return true;
1005 if (GET_CODE (p1) != PARALLEL || GET_CODE (p2) != PARALLEL)
1006 return false;
1008 if (XVECLEN (p1, 0) != XVECLEN (p2, 0))
1009 return false;
1011 for (i = 0; i < XVECLEN (p1, 0); i++)
1013 e1 = XVECEXP (p1, 0, i);
1014 e2 = XVECEXP (p2, 0, i);
1015 if (e1 == s1 && e2 == s2)
1016 continue;
1017 if (reload_completed
1018 ? rtx_renumbered_equal_p (e1, e2) : rtx_equal_p (e1, e2))
1019 continue;
1021 return false;
1024 return true;
1027 /* Examine register notes on I1 and I2 and return:
1028 - dir_forward if I1 can be replaced by I2, or
1029 - dir_backward if I2 can be replaced by I1, or
1030 - dir_both if both are the case. */
1032 static enum replace_direction
1033 can_replace_by (rtx_insn *i1, rtx_insn *i2)
1035 rtx s1, s2, d1, d2, src1, src2, note1, note2;
1036 bool c1, c2;
1038 /* Check for 2 sets. */
1039 s1 = single_set (i1);
1040 s2 = single_set (i2);
1041 if (s1 == NULL_RTX || s2 == NULL_RTX)
1042 return dir_none;
1044 /* Check that the 2 sets set the same dest. */
1045 d1 = SET_DEST (s1);
1046 d2 = SET_DEST (s2);
1047 if (!(reload_completed
1048 ? rtx_renumbered_equal_p (d1, d2) : rtx_equal_p (d1, d2)))
1049 return dir_none;
1051 /* Find identical req_equiv or reg_equal note, which implies that the 2 sets
1052 set dest to the same value. */
1053 note1 = find_reg_equal_equiv_note (i1);
1054 note2 = find_reg_equal_equiv_note (i2);
1055 if (!note1 || !note2 || !rtx_equal_p (XEXP (note1, 0), XEXP (note2, 0))
1056 || !CONST_INT_P (XEXP (note1, 0)))
1057 return dir_none;
1059 if (!equal_different_set_p (PATTERN (i1), s1, PATTERN (i2), s2))
1060 return dir_none;
1062 /* Although the 2 sets set dest to the same value, we cannot replace
1063 (set (dest) (const_int))
1065 (set (dest) (reg))
1066 because we don't know if the reg is live and has the same value at the
1067 location of replacement. */
1068 src1 = SET_SRC (s1);
1069 src2 = SET_SRC (s2);
1070 c1 = CONST_INT_P (src1);
1071 c2 = CONST_INT_P (src2);
1072 if (c1 && c2)
1073 return dir_both;
1074 else if (c2)
1075 return dir_forward;
1076 else if (c1)
1077 return dir_backward;
1079 return dir_none;
1082 /* Merges directions A and B. */
1084 static enum replace_direction
1085 merge_dir (enum replace_direction a, enum replace_direction b)
1087 /* Implements the following table:
1088 |bo fw bw no
1089 ---+-----------
1090 bo |bo fw bw no
1091 fw |-- fw no no
1092 bw |-- -- bw no
1093 no |-- -- -- no. */
1095 if (a == b)
1096 return a;
1098 if (a == dir_both)
1099 return b;
1100 if (b == dir_both)
1101 return a;
1103 return dir_none;
1106 /* Examine I1 and I2 and return:
1107 - dir_forward if I1 can be replaced by I2, or
1108 - dir_backward if I2 can be replaced by I1, or
1109 - dir_both if both are the case. */
1111 static enum replace_direction
1112 old_insns_match_p (int mode ATTRIBUTE_UNUSED, rtx_insn *i1, rtx_insn *i2)
1114 rtx p1, p2;
1116 /* Verify that I1 and I2 are equivalent. */
1117 if (GET_CODE (i1) != GET_CODE (i2))
1118 return dir_none;
1120 /* __builtin_unreachable() may lead to empty blocks (ending with
1121 NOTE_INSN_BASIC_BLOCK). They may be crossjumped. */
1122 if (NOTE_INSN_BASIC_BLOCK_P (i1) && NOTE_INSN_BASIC_BLOCK_P (i2))
1123 return dir_both;
1125 /* ??? Do not allow cross-jumping between different stack levels. */
1126 p1 = find_reg_note (i1, REG_ARGS_SIZE, NULL);
1127 p2 = find_reg_note (i2, REG_ARGS_SIZE, NULL);
1128 if (p1 && p2)
1130 p1 = XEXP (p1, 0);
1131 p2 = XEXP (p2, 0);
1132 if (!rtx_equal_p (p1, p2))
1133 return dir_none;
1135 /* ??? Worse, this adjustment had better be constant lest we
1136 have differing incoming stack levels. */
1137 if (!frame_pointer_needed
1138 && find_args_size_adjust (i1) == HOST_WIDE_INT_MIN)
1139 return dir_none;
1141 else if (p1 || p2)
1142 return dir_none;
1144 p1 = PATTERN (i1);
1145 p2 = PATTERN (i2);
1147 if (GET_CODE (p1) != GET_CODE (p2))
1148 return dir_none;
1150 /* If this is a CALL_INSN, compare register usage information.
1151 If we don't check this on stack register machines, the two
1152 CALL_INSNs might be merged leaving reg-stack.c with mismatching
1153 numbers of stack registers in the same basic block.
1154 If we don't check this on machines with delay slots, a delay slot may
1155 be filled that clobbers a parameter expected by the subroutine.
1157 ??? We take the simple route for now and assume that if they're
1158 equal, they were constructed identically.
1160 Also check for identical exception regions. */
1162 if (CALL_P (i1))
1164 /* Ensure the same EH region. */
1165 rtx n1 = find_reg_note (i1, REG_EH_REGION, 0);
1166 rtx n2 = find_reg_note (i2, REG_EH_REGION, 0);
1168 if (!n1 && n2)
1169 return dir_none;
1171 if (n1 && (!n2 || XEXP (n1, 0) != XEXP (n2, 0)))
1172 return dir_none;
1174 if (!rtx_equal_p (CALL_INSN_FUNCTION_USAGE (i1),
1175 CALL_INSN_FUNCTION_USAGE (i2))
1176 || SIBLING_CALL_P (i1) != SIBLING_CALL_P (i2))
1177 return dir_none;
1179 /* For address sanitizer, never crossjump __asan_report_* builtins,
1180 otherwise errors might be reported on incorrect lines. */
1181 if (flag_sanitize & SANITIZE_ADDRESS)
1183 rtx call = get_call_rtx_from (i1);
1184 if (call && GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF)
1186 rtx symbol = XEXP (XEXP (call, 0), 0);
1187 if (SYMBOL_REF_DECL (symbol)
1188 && TREE_CODE (SYMBOL_REF_DECL (symbol)) == FUNCTION_DECL)
1190 if ((DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol))
1191 == BUILT_IN_NORMAL)
1192 && DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol))
1193 >= BUILT_IN_ASAN_REPORT_LOAD1
1194 && DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol))
1195 <= BUILT_IN_ASAN_STOREN)
1196 return dir_none;
1202 #ifdef STACK_REGS
1203 /* If cross_jump_death_matters is not 0, the insn's mode
1204 indicates whether or not the insn contains any stack-like
1205 regs. */
1207 if ((mode & CLEANUP_POST_REGSTACK) && stack_regs_mentioned (i1))
1209 /* If register stack conversion has already been done, then
1210 death notes must also be compared before it is certain that
1211 the two instruction streams match. */
1213 rtx note;
1214 HARD_REG_SET i1_regset, i2_regset;
1216 CLEAR_HARD_REG_SET (i1_regset);
1217 CLEAR_HARD_REG_SET (i2_regset);
1219 for (note = REG_NOTES (i1); note; note = XEXP (note, 1))
1220 if (REG_NOTE_KIND (note) == REG_DEAD && STACK_REG_P (XEXP (note, 0)))
1221 SET_HARD_REG_BIT (i1_regset, REGNO (XEXP (note, 0)));
1223 for (note = REG_NOTES (i2); note; note = XEXP (note, 1))
1224 if (REG_NOTE_KIND (note) == REG_DEAD && STACK_REG_P (XEXP (note, 0)))
1225 SET_HARD_REG_BIT (i2_regset, REGNO (XEXP (note, 0)));
1227 if (!hard_reg_set_equal_p (i1_regset, i2_regset))
1228 return dir_none;
1230 #endif
1232 if (reload_completed
1233 ? rtx_renumbered_equal_p (p1, p2) : rtx_equal_p (p1, p2))
1234 return dir_both;
1236 return can_replace_by (i1, i2);
1239 /* When comparing insns I1 and I2 in flow_find_cross_jump or
1240 flow_find_head_matching_sequence, ensure the notes match. */
1242 static void
1243 merge_notes (rtx_insn *i1, rtx_insn *i2)
1245 /* If the merged insns have different REG_EQUAL notes, then
1246 remove them. */
1247 rtx equiv1 = find_reg_equal_equiv_note (i1);
1248 rtx equiv2 = find_reg_equal_equiv_note (i2);
1250 if (equiv1 && !equiv2)
1251 remove_note (i1, equiv1);
1252 else if (!equiv1 && equiv2)
1253 remove_note (i2, equiv2);
1254 else if (equiv1 && equiv2
1255 && !rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))
1257 remove_note (i1, equiv1);
1258 remove_note (i2, equiv2);
1262 /* Walks from I1 in BB1 backward till the next non-debug insn, and returns the
1263 resulting insn in I1, and the corresponding bb in BB1. At the head of a
1264 bb, if there is a predecessor bb that reaches this bb via fallthru, and
1265 FOLLOW_FALLTHRU, walks further in the predecessor bb and registers this in
1266 DID_FALLTHRU. Otherwise, stops at the head of the bb. */
1268 static void
1269 walk_to_nondebug_insn (rtx_insn **i1, basic_block *bb1, bool follow_fallthru,
1270 bool *did_fallthru)
1272 edge fallthru;
1274 *did_fallthru = false;
1276 /* Ignore notes. */
1277 while (!NONDEBUG_INSN_P (*i1))
1279 if (*i1 != BB_HEAD (*bb1))
1281 *i1 = PREV_INSN (*i1);
1282 continue;
1285 if (!follow_fallthru)
1286 return;
1288 fallthru = find_fallthru_edge ((*bb1)->preds);
1289 if (!fallthru || fallthru->src == ENTRY_BLOCK_PTR_FOR_FN (cfun)
1290 || !single_succ_p (fallthru->src))
1291 return;
1293 *bb1 = fallthru->src;
1294 *i1 = BB_END (*bb1);
1295 *did_fallthru = true;
1299 /* Look through the insns at the end of BB1 and BB2 and find the longest
1300 sequence that are either equivalent, or allow forward or backward
1301 replacement. Store the first insns for that sequence in *F1 and *F2 and
1302 return the sequence length.
1304 DIR_P indicates the allowed replacement direction on function entry, and
1305 the actual replacement direction on function exit. If NULL, only equivalent
1306 sequences are allowed.
1308 To simplify callers of this function, if the blocks match exactly,
1309 store the head of the blocks in *F1 and *F2. */
1312 flow_find_cross_jump (basic_block bb1, basic_block bb2, rtx_insn **f1,
1313 rtx_insn **f2, enum replace_direction *dir_p)
1315 rtx_insn *i1, *i2, *last1, *last2, *afterlast1, *afterlast2;
1316 int ninsns = 0;
1317 enum replace_direction dir, last_dir, afterlast_dir;
1318 bool follow_fallthru, did_fallthru;
1320 if (dir_p)
1321 dir = *dir_p;
1322 else
1323 dir = dir_both;
1324 afterlast_dir = dir;
1325 last_dir = afterlast_dir;
1327 /* Skip simple jumps at the end of the blocks. Complex jumps still
1328 need to be compared for equivalence, which we'll do below. */
1330 i1 = BB_END (bb1);
1331 last1 = afterlast1 = last2 = afterlast2 = NULL;
1332 if (onlyjump_p (i1)
1333 || (returnjump_p (i1) && !side_effects_p (PATTERN (i1))))
1335 last1 = i1;
1336 i1 = PREV_INSN (i1);
1339 i2 = BB_END (bb2);
1340 if (onlyjump_p (i2)
1341 || (returnjump_p (i2) && !side_effects_p (PATTERN (i2))))
1343 last2 = i2;
1344 /* Count everything except for unconditional jump as insn.
1345 Don't count any jumps if dir_p is NULL. */
1346 if (!simplejump_p (i2) && !returnjump_p (i2) && last1 && dir_p)
1347 ninsns++;
1348 i2 = PREV_INSN (i2);
1351 while (true)
1353 /* In the following example, we can replace all jumps to C by jumps to A.
1355 This removes 4 duplicate insns.
1356 [bb A] insn1 [bb C] insn1
1357 insn2 insn2
1358 [bb B] insn3 insn3
1359 insn4 insn4
1360 jump_insn jump_insn
1362 We could also replace all jumps to A by jumps to C, but that leaves B
1363 alive, and removes only 2 duplicate insns. In a subsequent crossjump
1364 step, all jumps to B would be replaced with jumps to the middle of C,
1365 achieving the same result with more effort.
1366 So we allow only the first possibility, which means that we don't allow
1367 fallthru in the block that's being replaced. */
1369 follow_fallthru = dir_p && dir != dir_forward;
1370 walk_to_nondebug_insn (&i1, &bb1, follow_fallthru, &did_fallthru);
1371 if (did_fallthru)
1372 dir = dir_backward;
1374 follow_fallthru = dir_p && dir != dir_backward;
1375 walk_to_nondebug_insn (&i2, &bb2, follow_fallthru, &did_fallthru);
1376 if (did_fallthru)
1377 dir = dir_forward;
1379 if (i1 == BB_HEAD (bb1) || i2 == BB_HEAD (bb2))
1380 break;
1382 dir = merge_dir (dir, old_insns_match_p (0, i1, i2));
1383 if (dir == dir_none || (!dir_p && dir != dir_both))
1384 break;
1386 merge_memattrs (i1, i2);
1388 /* Don't begin a cross-jump with a NOTE insn. */
1389 if (INSN_P (i1))
1391 merge_notes (i1, i2);
1393 afterlast1 = last1, afterlast2 = last2;
1394 last1 = i1, last2 = i2;
1395 afterlast_dir = last_dir;
1396 last_dir = dir;
1397 if (active_insn_p (i1))
1398 ninsns++;
1401 i1 = PREV_INSN (i1);
1402 i2 = PREV_INSN (i2);
1405 #ifdef HAVE_cc0
1406 /* Don't allow the insn after a compare to be shared by
1407 cross-jumping unless the compare is also shared. */
1408 if (ninsns && reg_mentioned_p (cc0_rtx, last1) && ! sets_cc0_p (last1))
1409 last1 = afterlast1, last2 = afterlast2, last_dir = afterlast_dir, ninsns--;
1410 #endif
1412 /* Include preceding notes and labels in the cross-jump. One,
1413 this may bring us to the head of the blocks as requested above.
1414 Two, it keeps line number notes as matched as may be. */
1415 if (ninsns)
1417 bb1 = BLOCK_FOR_INSN (last1);
1418 while (last1 != BB_HEAD (bb1) && !NONDEBUG_INSN_P (PREV_INSN (last1)))
1419 last1 = PREV_INSN (last1);
1421 if (last1 != BB_HEAD (bb1) && LABEL_P (PREV_INSN (last1)))
1422 last1 = PREV_INSN (last1);
1424 bb2 = BLOCK_FOR_INSN (last2);
1425 while (last2 != BB_HEAD (bb2) && !NONDEBUG_INSN_P (PREV_INSN (last2)))
1426 last2 = PREV_INSN (last2);
1428 if (last2 != BB_HEAD (bb2) && LABEL_P (PREV_INSN (last2)))
1429 last2 = PREV_INSN (last2);
1431 *f1 = last1;
1432 *f2 = last2;
1435 if (dir_p)
1436 *dir_p = last_dir;
1437 return ninsns;
1440 /* Like flow_find_cross_jump, except start looking for a matching sequence from
1441 the head of the two blocks. Do not include jumps at the end.
1442 If STOP_AFTER is nonzero, stop after finding that many matching
1443 instructions. If STOP_AFTER is zero, count all INSN_P insns, if it is
1444 non-zero, only count active insns. */
1447 flow_find_head_matching_sequence (basic_block bb1, basic_block bb2, rtx_insn **f1,
1448 rtx_insn **f2, int stop_after)
1450 rtx_insn *i1, *i2, *last1, *last2, *beforelast1, *beforelast2;
1451 int ninsns = 0;
1452 edge e;
1453 edge_iterator ei;
1454 int nehedges1 = 0, nehedges2 = 0;
1456 FOR_EACH_EDGE (e, ei, bb1->succs)
1457 if (e->flags & EDGE_EH)
1458 nehedges1++;
1459 FOR_EACH_EDGE (e, ei, bb2->succs)
1460 if (e->flags & EDGE_EH)
1461 nehedges2++;
1463 i1 = BB_HEAD (bb1);
1464 i2 = BB_HEAD (bb2);
1465 last1 = beforelast1 = last2 = beforelast2 = NULL;
1467 while (true)
1469 /* Ignore notes, except NOTE_INSN_EPILOGUE_BEG. */
1470 while (!NONDEBUG_INSN_P (i1) && i1 != BB_END (bb1))
1472 if (NOTE_P (i1) && NOTE_KIND (i1) == NOTE_INSN_EPILOGUE_BEG)
1473 break;
1474 i1 = NEXT_INSN (i1);
1477 while (!NONDEBUG_INSN_P (i2) && i2 != BB_END (bb2))
1479 if (NOTE_P (i2) && NOTE_KIND (i2) == NOTE_INSN_EPILOGUE_BEG)
1480 break;
1481 i2 = NEXT_INSN (i2);
1484 if ((i1 == BB_END (bb1) && !NONDEBUG_INSN_P (i1))
1485 || (i2 == BB_END (bb2) && !NONDEBUG_INSN_P (i2)))
1486 break;
1488 if (NOTE_P (i1) || NOTE_P (i2)
1489 || JUMP_P (i1) || JUMP_P (i2))
1490 break;
1492 /* A sanity check to make sure we're not merging insns with different
1493 effects on EH. If only one of them ends a basic block, it shouldn't
1494 have an EH edge; if both end a basic block, there should be the same
1495 number of EH edges. */
1496 if ((i1 == BB_END (bb1) && i2 != BB_END (bb2)
1497 && nehedges1 > 0)
1498 || (i2 == BB_END (bb2) && i1 != BB_END (bb1)
1499 && nehedges2 > 0)
1500 || (i1 == BB_END (bb1) && i2 == BB_END (bb2)
1501 && nehedges1 != nehedges2))
1502 break;
1504 if (old_insns_match_p (0, i1, i2) != dir_both)
1505 break;
1507 merge_memattrs (i1, i2);
1509 /* Don't begin a cross-jump with a NOTE insn. */
1510 if (INSN_P (i1))
1512 merge_notes (i1, i2);
1514 beforelast1 = last1, beforelast2 = last2;
1515 last1 = i1, last2 = i2;
1516 if (!stop_after || active_insn_p (i1))
1517 ninsns++;
1520 if (i1 == BB_END (bb1) || i2 == BB_END (bb2)
1521 || (stop_after > 0 && ninsns == stop_after))
1522 break;
1524 i1 = NEXT_INSN (i1);
1525 i2 = NEXT_INSN (i2);
1528 #ifdef HAVE_cc0
1529 /* Don't allow a compare to be shared by cross-jumping unless the insn
1530 after the compare is also shared. */
1531 if (ninsns && reg_mentioned_p (cc0_rtx, last1) && sets_cc0_p (last1))
1532 last1 = beforelast1, last2 = beforelast2, ninsns--;
1533 #endif
1535 if (ninsns)
1537 *f1 = last1;
1538 *f2 = last2;
1541 return ninsns;
1544 /* Return true iff outgoing edges of BB1 and BB2 match, together with
1545 the branch instruction. This means that if we commonize the control
1546 flow before end of the basic block, the semantic remains unchanged.
1548 We may assume that there exists one edge with a common destination. */
1550 static bool
1551 outgoing_edges_match (int mode, basic_block bb1, basic_block bb2)
1553 int nehedges1 = 0, nehedges2 = 0;
1554 edge fallthru1 = 0, fallthru2 = 0;
1555 edge e1, e2;
1556 edge_iterator ei;
1558 /* If we performed shrink-wrapping, edges to the exit block can
1559 only be distinguished for JUMP_INSNs. The two paths may differ in
1560 whether they went through the prologue. Sibcalls are fine, we know
1561 that we either didn't need or inserted an epilogue before them. */
1562 if (crtl->shrink_wrapped
1563 && single_succ_p (bb1)
1564 && single_succ (bb1) == EXIT_BLOCK_PTR_FOR_FN (cfun)
1565 && !JUMP_P (BB_END (bb1))
1566 && !(CALL_P (BB_END (bb1)) && SIBLING_CALL_P (BB_END (bb1))))
1567 return false;
1569 /* If BB1 has only one successor, we may be looking at either an
1570 unconditional jump, or a fake edge to exit. */
1571 if (single_succ_p (bb1)
1572 && (single_succ_edge (bb1)->flags & (EDGE_COMPLEX | EDGE_FAKE)) == 0
1573 && (!JUMP_P (BB_END (bb1)) || simplejump_p (BB_END (bb1))))
1574 return (single_succ_p (bb2)
1575 && (single_succ_edge (bb2)->flags
1576 & (EDGE_COMPLEX | EDGE_FAKE)) == 0
1577 && (!JUMP_P (BB_END (bb2)) || simplejump_p (BB_END (bb2))));
1579 /* Match conditional jumps - this may get tricky when fallthru and branch
1580 edges are crossed. */
1581 if (EDGE_COUNT (bb1->succs) == 2
1582 && any_condjump_p (BB_END (bb1))
1583 && onlyjump_p (BB_END (bb1)))
1585 edge b1, f1, b2, f2;
1586 bool reverse, match;
1587 rtx set1, set2, cond1, cond2;
1588 enum rtx_code code1, code2;
1590 if (EDGE_COUNT (bb2->succs) != 2
1591 || !any_condjump_p (BB_END (bb2))
1592 || !onlyjump_p (BB_END (bb2)))
1593 return false;
1595 b1 = BRANCH_EDGE (bb1);
1596 b2 = BRANCH_EDGE (bb2);
1597 f1 = FALLTHRU_EDGE (bb1);
1598 f2 = FALLTHRU_EDGE (bb2);
1600 /* Get around possible forwarders on fallthru edges. Other cases
1601 should be optimized out already. */
1602 if (FORWARDER_BLOCK_P (f1->dest))
1603 f1 = single_succ_edge (f1->dest);
1605 if (FORWARDER_BLOCK_P (f2->dest))
1606 f2 = single_succ_edge (f2->dest);
1608 /* To simplify use of this function, return false if there are
1609 unneeded forwarder blocks. These will get eliminated later
1610 during cleanup_cfg. */
1611 if (FORWARDER_BLOCK_P (f1->dest)
1612 || FORWARDER_BLOCK_P (f2->dest)
1613 || FORWARDER_BLOCK_P (b1->dest)
1614 || FORWARDER_BLOCK_P (b2->dest))
1615 return false;
1617 if (f1->dest == f2->dest && b1->dest == b2->dest)
1618 reverse = false;
1619 else if (f1->dest == b2->dest && b1->dest == f2->dest)
1620 reverse = true;
1621 else
1622 return false;
1624 set1 = pc_set (BB_END (bb1));
1625 set2 = pc_set (BB_END (bb2));
1626 if ((XEXP (SET_SRC (set1), 1) == pc_rtx)
1627 != (XEXP (SET_SRC (set2), 1) == pc_rtx))
1628 reverse = !reverse;
1630 cond1 = XEXP (SET_SRC (set1), 0);
1631 cond2 = XEXP (SET_SRC (set2), 0);
1632 code1 = GET_CODE (cond1);
1633 if (reverse)
1634 code2 = reversed_comparison_code (cond2, BB_END (bb2));
1635 else
1636 code2 = GET_CODE (cond2);
1638 if (code2 == UNKNOWN)
1639 return false;
1641 /* Verify codes and operands match. */
1642 match = ((code1 == code2
1643 && rtx_renumbered_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
1644 && rtx_renumbered_equal_p (XEXP (cond1, 1), XEXP (cond2, 1)))
1645 || (code1 == swap_condition (code2)
1646 && rtx_renumbered_equal_p (XEXP (cond1, 1),
1647 XEXP (cond2, 0))
1648 && rtx_renumbered_equal_p (XEXP (cond1, 0),
1649 XEXP (cond2, 1))));
1651 /* If we return true, we will join the blocks. Which means that
1652 we will only have one branch prediction bit to work with. Thus
1653 we require the existing branches to have probabilities that are
1654 roughly similar. */
1655 if (match
1656 && optimize_bb_for_speed_p (bb1)
1657 && optimize_bb_for_speed_p (bb2))
1659 int prob2;
1661 if (b1->dest == b2->dest)
1662 prob2 = b2->probability;
1663 else
1664 /* Do not use f2 probability as f2 may be forwarded. */
1665 prob2 = REG_BR_PROB_BASE - b2->probability;
1667 /* Fail if the difference in probabilities is greater than 50%.
1668 This rules out two well-predicted branches with opposite
1669 outcomes. */
1670 if (abs (b1->probability - prob2) > REG_BR_PROB_BASE / 2)
1672 if (dump_file)
1673 fprintf (dump_file,
1674 "Outcomes of branch in bb %i and %i differ too much (%i %i)\n",
1675 bb1->index, bb2->index, b1->probability, prob2);
1677 return false;
1681 if (dump_file && match)
1682 fprintf (dump_file, "Conditionals in bb %i and %i match.\n",
1683 bb1->index, bb2->index);
1685 return match;
1688 /* Generic case - we are seeing a computed jump, table jump or trapping
1689 instruction. */
1691 /* Check whether there are tablejumps in the end of BB1 and BB2.
1692 Return true if they are identical. */
1694 rtx label1, label2;
1695 rtx_jump_table_data *table1, *table2;
1697 if (tablejump_p (BB_END (bb1), &label1, &table1)
1698 && tablejump_p (BB_END (bb2), &label2, &table2)
1699 && GET_CODE (PATTERN (table1)) == GET_CODE (PATTERN (table2)))
1701 /* The labels should never be the same rtx. If they really are same
1702 the jump tables are same too. So disable crossjumping of blocks BB1
1703 and BB2 because when deleting the common insns in the end of BB1
1704 by delete_basic_block () the jump table would be deleted too. */
1705 /* If LABEL2 is referenced in BB1->END do not do anything
1706 because we would loose information when replacing
1707 LABEL1 by LABEL2 and then LABEL2 by LABEL1 in BB1->END. */
1708 if (label1 != label2 && !rtx_referenced_p (label2, BB_END (bb1)))
1710 /* Set IDENTICAL to true when the tables are identical. */
1711 bool identical = false;
1712 rtx p1, p2;
1714 p1 = PATTERN (table1);
1715 p2 = PATTERN (table2);
1716 if (GET_CODE (p1) == ADDR_VEC && rtx_equal_p (p1, p2))
1718 identical = true;
1720 else if (GET_CODE (p1) == ADDR_DIFF_VEC
1721 && (XVECLEN (p1, 1) == XVECLEN (p2, 1))
1722 && rtx_equal_p (XEXP (p1, 2), XEXP (p2, 2))
1723 && rtx_equal_p (XEXP (p1, 3), XEXP (p2, 3)))
1725 int i;
1727 identical = true;
1728 for (i = XVECLEN (p1, 1) - 1; i >= 0 && identical; i--)
1729 if (!rtx_equal_p (XVECEXP (p1, 1, i), XVECEXP (p2, 1, i)))
1730 identical = false;
1733 if (identical)
1735 bool match;
1737 /* Temporarily replace references to LABEL1 with LABEL2
1738 in BB1->END so that we could compare the instructions. */
1739 replace_label_in_insn (BB_END (bb1), label1, label2, false);
1741 match = (old_insns_match_p (mode, BB_END (bb1), BB_END (bb2))
1742 == dir_both);
1743 if (dump_file && match)
1744 fprintf (dump_file,
1745 "Tablejumps in bb %i and %i match.\n",
1746 bb1->index, bb2->index);
1748 /* Set the original label in BB1->END because when deleting
1749 a block whose end is a tablejump, the tablejump referenced
1750 from the instruction is deleted too. */
1751 replace_label_in_insn (BB_END (bb1), label2, label1, false);
1753 return match;
1756 return false;
1760 /* Find the last non-debug non-note instruction in each bb, except
1761 stop when we see the NOTE_INSN_BASIC_BLOCK, as old_insns_match_p
1762 handles that case specially. old_insns_match_p does not handle
1763 other types of instruction notes. */
1764 rtx_insn *last1 = BB_END (bb1);
1765 rtx_insn *last2 = BB_END (bb2);
1766 while (!NOTE_INSN_BASIC_BLOCK_P (last1) &&
1767 (DEBUG_INSN_P (last1) || NOTE_P (last1)))
1768 last1 = PREV_INSN (last1);
1769 while (!NOTE_INSN_BASIC_BLOCK_P (last2) &&
1770 (DEBUG_INSN_P (last2) || NOTE_P (last2)))
1771 last2 = PREV_INSN (last2);
1772 gcc_assert (last1 && last2);
1774 /* First ensure that the instructions match. There may be many outgoing
1775 edges so this test is generally cheaper. */
1776 if (old_insns_match_p (mode, last1, last2) != dir_both)
1777 return false;
1779 /* Search the outgoing edges, ensure that the counts do match, find possible
1780 fallthru and exception handling edges since these needs more
1781 validation. */
1782 if (EDGE_COUNT (bb1->succs) != EDGE_COUNT (bb2->succs))
1783 return false;
1785 bool nonfakeedges = false;
1786 FOR_EACH_EDGE (e1, ei, bb1->succs)
1788 e2 = EDGE_SUCC (bb2, ei.index);
1790 if ((e1->flags & EDGE_FAKE) == 0)
1791 nonfakeedges = true;
1793 if (e1->flags & EDGE_EH)
1794 nehedges1++;
1796 if (e2->flags & EDGE_EH)
1797 nehedges2++;
1799 if (e1->flags & EDGE_FALLTHRU)
1800 fallthru1 = e1;
1801 if (e2->flags & EDGE_FALLTHRU)
1802 fallthru2 = e2;
1805 /* If number of edges of various types does not match, fail. */
1806 if (nehedges1 != nehedges2
1807 || (fallthru1 != 0) != (fallthru2 != 0))
1808 return false;
1810 /* If !ACCUMULATE_OUTGOING_ARGS, bb1 (and bb2) have no successors
1811 and the last real insn doesn't have REG_ARGS_SIZE note, don't
1812 attempt to optimize, as the two basic blocks might have different
1813 REG_ARGS_SIZE depths. For noreturn calls and unconditional
1814 traps there should be REG_ARG_SIZE notes, they could be missing
1815 for __builtin_unreachable () uses though. */
1816 if (!nonfakeedges
1817 && !ACCUMULATE_OUTGOING_ARGS
1818 && (!INSN_P (last1)
1819 || !find_reg_note (last1, REG_ARGS_SIZE, NULL)))
1820 return false;
1822 /* fallthru edges must be forwarded to the same destination. */
1823 if (fallthru1)
1825 basic_block d1 = (forwarder_block_p (fallthru1->dest)
1826 ? single_succ (fallthru1->dest): fallthru1->dest);
1827 basic_block d2 = (forwarder_block_p (fallthru2->dest)
1828 ? single_succ (fallthru2->dest): fallthru2->dest);
1830 if (d1 != d2)
1831 return false;
1834 /* Ensure the same EH region. */
1836 rtx n1 = find_reg_note (BB_END (bb1), REG_EH_REGION, 0);
1837 rtx n2 = find_reg_note (BB_END (bb2), REG_EH_REGION, 0);
1839 if (!n1 && n2)
1840 return false;
1842 if (n1 && (!n2 || XEXP (n1, 0) != XEXP (n2, 0)))
1843 return false;
1846 /* The same checks as in try_crossjump_to_edge. It is required for RTL
1847 version of sequence abstraction. */
1848 FOR_EACH_EDGE (e1, ei, bb2->succs)
1850 edge e2;
1851 edge_iterator ei;
1852 basic_block d1 = e1->dest;
1854 if (FORWARDER_BLOCK_P (d1))
1855 d1 = EDGE_SUCC (d1, 0)->dest;
1857 FOR_EACH_EDGE (e2, ei, bb1->succs)
1859 basic_block d2 = e2->dest;
1860 if (FORWARDER_BLOCK_P (d2))
1861 d2 = EDGE_SUCC (d2, 0)->dest;
1862 if (d1 == d2)
1863 break;
1866 if (!e2)
1867 return false;
1870 return true;
1873 /* Returns true if BB basic block has a preserve label. */
1875 static bool
1876 block_has_preserve_label (basic_block bb)
1878 return (bb
1879 && block_label (bb)
1880 && LABEL_PRESERVE_P (block_label (bb)));
1883 /* E1 and E2 are edges with the same destination block. Search their
1884 predecessors for common code. If found, redirect control flow from
1885 (maybe the middle of) E1->SRC to (maybe the middle of) E2->SRC (dir_forward),
1886 or the other way around (dir_backward). DIR specifies the allowed
1887 replacement direction. */
1889 static bool
1890 try_crossjump_to_edge (int mode, edge e1, edge e2,
1891 enum replace_direction dir)
1893 int nmatch;
1894 basic_block src1 = e1->src, src2 = e2->src;
1895 basic_block redirect_to, redirect_from, to_remove;
1896 basic_block osrc1, osrc2, redirect_edges_to, tmp;
1897 rtx_insn *newpos1, *newpos2;
1898 edge s;
1899 edge_iterator ei;
1901 newpos1 = newpos2 = NULL;
1903 /* If we have partitioned hot/cold basic blocks, it is a bad idea
1904 to try this optimization.
1906 Basic block partitioning may result in some jumps that appear to
1907 be optimizable (or blocks that appear to be mergeable), but which really
1908 must be left untouched (they are required to make it safely across
1909 partition boundaries). See the comments at the top of
1910 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
1912 if (crtl->has_bb_partition && reload_completed)
1913 return false;
1915 /* Search backward through forwarder blocks. We don't need to worry
1916 about multiple entry or chained forwarders, as they will be optimized
1917 away. We do this to look past the unconditional jump following a
1918 conditional jump that is required due to the current CFG shape. */
1919 if (single_pred_p (src1)
1920 && FORWARDER_BLOCK_P (src1))
1921 e1 = single_pred_edge (src1), src1 = e1->src;
1923 if (single_pred_p (src2)
1924 && FORWARDER_BLOCK_P (src2))
1925 e2 = single_pred_edge (src2), src2 = e2->src;
1927 /* Nothing to do if we reach ENTRY, or a common source block. */
1928 if (src1 == ENTRY_BLOCK_PTR_FOR_FN (cfun) || src2
1929 == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1930 return false;
1931 if (src1 == src2)
1932 return false;
1934 /* Seeing more than 1 forwarder blocks would confuse us later... */
1935 if (FORWARDER_BLOCK_P (e1->dest)
1936 && FORWARDER_BLOCK_P (single_succ (e1->dest)))
1937 return false;
1939 if (FORWARDER_BLOCK_P (e2->dest)
1940 && FORWARDER_BLOCK_P (single_succ (e2->dest)))
1941 return false;
1943 /* Likewise with dead code (possibly newly created by the other optimizations
1944 of cfg_cleanup). */
1945 if (EDGE_COUNT (src1->preds) == 0 || EDGE_COUNT (src2->preds) == 0)
1946 return false;
1948 /* Look for the common insn sequence, part the first ... */
1949 if (!outgoing_edges_match (mode, src1, src2))
1950 return false;
1952 /* ... and part the second. */
1953 nmatch = flow_find_cross_jump (src1, src2, &newpos1, &newpos2, &dir);
1955 osrc1 = src1;
1956 osrc2 = src2;
1957 if (newpos1 != NULL_RTX)
1958 src1 = BLOCK_FOR_INSN (newpos1);
1959 if (newpos2 != NULL_RTX)
1960 src2 = BLOCK_FOR_INSN (newpos2);
1962 if (dir == dir_backward)
1964 #define SWAP(T, X, Y) do { T tmp = (X); (X) = (Y); (Y) = tmp; } while (0)
1965 SWAP (basic_block, osrc1, osrc2);
1966 SWAP (basic_block, src1, src2);
1967 SWAP (edge, e1, e2);
1968 SWAP (rtx_insn *, newpos1, newpos2);
1969 #undef SWAP
1972 /* Don't proceed with the crossjump unless we found a sufficient number
1973 of matching instructions or the 'from' block was totally matched
1974 (such that its predecessors will hopefully be redirected and the
1975 block removed). */
1976 if ((nmatch < PARAM_VALUE (PARAM_MIN_CROSSJUMP_INSNS))
1977 && (newpos1 != BB_HEAD (src1)))
1978 return false;
1980 /* Avoid deleting preserve label when redirecting ABNORMAL edges. */
1981 if (block_has_preserve_label (e1->dest)
1982 && (e1->flags & EDGE_ABNORMAL))
1983 return false;
1985 /* Here we know that the insns in the end of SRC1 which are common with SRC2
1986 will be deleted.
1987 If we have tablejumps in the end of SRC1 and SRC2
1988 they have been already compared for equivalence in outgoing_edges_match ()
1989 so replace the references to TABLE1 by references to TABLE2. */
1991 rtx label1, label2;
1992 rtx_jump_table_data *table1, *table2;
1994 if (tablejump_p (BB_END (osrc1), &label1, &table1)
1995 && tablejump_p (BB_END (osrc2), &label2, &table2)
1996 && label1 != label2)
1998 rtx_insn *insn;
2000 /* Replace references to LABEL1 with LABEL2. */
2001 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2003 /* Do not replace the label in SRC1->END because when deleting
2004 a block whose end is a tablejump, the tablejump referenced
2005 from the instruction is deleted too. */
2006 if (insn != BB_END (osrc1))
2007 replace_label_in_insn (insn, label1, label2, true);
2012 /* Avoid splitting if possible. We must always split when SRC2 has
2013 EH predecessor edges, or we may end up with basic blocks with both
2014 normal and EH predecessor edges. */
2015 if (newpos2 == BB_HEAD (src2)
2016 && !(EDGE_PRED (src2, 0)->flags & EDGE_EH))
2017 redirect_to = src2;
2018 else
2020 if (newpos2 == BB_HEAD (src2))
2022 /* Skip possible basic block header. */
2023 if (LABEL_P (newpos2))
2024 newpos2 = NEXT_INSN (newpos2);
2025 while (DEBUG_INSN_P (newpos2))
2026 newpos2 = NEXT_INSN (newpos2);
2027 if (NOTE_P (newpos2))
2028 newpos2 = NEXT_INSN (newpos2);
2029 while (DEBUG_INSN_P (newpos2))
2030 newpos2 = NEXT_INSN (newpos2);
2033 if (dump_file)
2034 fprintf (dump_file, "Splitting bb %i before %i insns\n",
2035 src2->index, nmatch);
2036 redirect_to = split_block (src2, PREV_INSN (newpos2))->dest;
2039 if (dump_file)
2040 fprintf (dump_file,
2041 "Cross jumping from bb %i to bb %i; %i common insns\n",
2042 src1->index, src2->index, nmatch);
2044 /* We may have some registers visible through the block. */
2045 df_set_bb_dirty (redirect_to);
2047 if (osrc2 == src2)
2048 redirect_edges_to = redirect_to;
2049 else
2050 redirect_edges_to = osrc2;
2052 /* Recompute the frequencies and counts of outgoing edges. */
2053 FOR_EACH_EDGE (s, ei, redirect_edges_to->succs)
2055 edge s2;
2056 edge_iterator ei;
2057 basic_block d = s->dest;
2059 if (FORWARDER_BLOCK_P (d))
2060 d = single_succ (d);
2062 FOR_EACH_EDGE (s2, ei, src1->succs)
2064 basic_block d2 = s2->dest;
2065 if (FORWARDER_BLOCK_P (d2))
2066 d2 = single_succ (d2);
2067 if (d == d2)
2068 break;
2071 s->count += s2->count;
2073 /* Take care to update possible forwarder blocks. We verified
2074 that there is no more than one in the chain, so we can't run
2075 into infinite loop. */
2076 if (FORWARDER_BLOCK_P (s->dest))
2078 single_succ_edge (s->dest)->count += s2->count;
2079 s->dest->count += s2->count;
2080 s->dest->frequency += EDGE_FREQUENCY (s);
2083 if (FORWARDER_BLOCK_P (s2->dest))
2085 single_succ_edge (s2->dest)->count -= s2->count;
2086 if (single_succ_edge (s2->dest)->count < 0)
2087 single_succ_edge (s2->dest)->count = 0;
2088 s2->dest->count -= s2->count;
2089 s2->dest->frequency -= EDGE_FREQUENCY (s);
2090 if (s2->dest->frequency < 0)
2091 s2->dest->frequency = 0;
2092 if (s2->dest->count < 0)
2093 s2->dest->count = 0;
2096 if (!redirect_edges_to->frequency && !src1->frequency)
2097 s->probability = (s->probability + s2->probability) / 2;
2098 else
2099 s->probability
2100 = ((s->probability * redirect_edges_to->frequency +
2101 s2->probability * src1->frequency)
2102 / (redirect_edges_to->frequency + src1->frequency));
2105 /* Adjust count and frequency for the block. An earlier jump
2106 threading pass may have left the profile in an inconsistent
2107 state (see update_bb_profile_for_threading) so we must be
2108 prepared for overflows. */
2109 tmp = redirect_to;
2112 tmp->count += src1->count;
2113 tmp->frequency += src1->frequency;
2114 if (tmp->frequency > BB_FREQ_MAX)
2115 tmp->frequency = BB_FREQ_MAX;
2116 if (tmp == redirect_edges_to)
2117 break;
2118 tmp = find_fallthru_edge (tmp->succs)->dest;
2120 while (true);
2121 update_br_prob_note (redirect_edges_to);
2123 /* Edit SRC1 to go to REDIRECT_TO at NEWPOS1. */
2125 /* Skip possible basic block header. */
2126 if (LABEL_P (newpos1))
2127 newpos1 = NEXT_INSN (newpos1);
2129 while (DEBUG_INSN_P (newpos1))
2130 newpos1 = NEXT_INSN (newpos1);
2132 if (NOTE_INSN_BASIC_BLOCK_P (newpos1))
2133 newpos1 = NEXT_INSN (newpos1);
2135 while (DEBUG_INSN_P (newpos1))
2136 newpos1 = NEXT_INSN (newpos1);
2138 redirect_from = split_block (src1, PREV_INSN (newpos1))->src;
2139 to_remove = single_succ (redirect_from);
2141 redirect_edge_and_branch_force (single_succ_edge (redirect_from), redirect_to);
2142 delete_basic_block (to_remove);
2144 update_forwarder_flag (redirect_from);
2145 if (redirect_to != src2)
2146 update_forwarder_flag (src2);
2148 return true;
2151 /* Search the predecessors of BB for common insn sequences. When found,
2152 share code between them by redirecting control flow. Return true if
2153 any changes made. */
2155 static bool
2156 try_crossjump_bb (int mode, basic_block bb)
2158 edge e, e2, fallthru;
2159 bool changed;
2160 unsigned max, ix, ix2;
2162 /* Nothing to do if there is not at least two incoming edges. */
2163 if (EDGE_COUNT (bb->preds) < 2)
2164 return false;
2166 /* Don't crossjump if this block ends in a computed jump,
2167 unless we are optimizing for size. */
2168 if (optimize_bb_for_size_p (bb)
2169 && bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
2170 && computed_jump_p (BB_END (bb)))
2171 return false;
2173 /* If we are partitioning hot/cold basic blocks, we don't want to
2174 mess up unconditional or indirect jumps that cross between hot
2175 and cold sections.
2177 Basic block partitioning may result in some jumps that appear to
2178 be optimizable (or blocks that appear to be mergeable), but which really
2179 must be left untouched (they are required to make it safely across
2180 partition boundaries). See the comments at the top of
2181 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
2183 if (BB_PARTITION (EDGE_PRED (bb, 0)->src) !=
2184 BB_PARTITION (EDGE_PRED (bb, 1)->src)
2185 || (EDGE_PRED (bb, 0)->flags & EDGE_CROSSING))
2186 return false;
2188 /* It is always cheapest to redirect a block that ends in a branch to
2189 a block that falls through into BB, as that adds no branches to the
2190 program. We'll try that combination first. */
2191 fallthru = NULL;
2192 max = PARAM_VALUE (PARAM_MAX_CROSSJUMP_EDGES);
2194 if (EDGE_COUNT (bb->preds) > max)
2195 return false;
2197 fallthru = find_fallthru_edge (bb->preds);
2199 changed = false;
2200 for (ix = 0; ix < EDGE_COUNT (bb->preds);)
2202 e = EDGE_PRED (bb, ix);
2203 ix++;
2205 /* As noted above, first try with the fallthru predecessor (or, a
2206 fallthru predecessor if we are in cfglayout mode). */
2207 if (fallthru)
2209 /* Don't combine the fallthru edge into anything else.
2210 If there is a match, we'll do it the other way around. */
2211 if (e == fallthru)
2212 continue;
2213 /* If nothing changed since the last attempt, there is nothing
2214 we can do. */
2215 if (!first_pass
2216 && !((e->src->flags & BB_MODIFIED)
2217 || (fallthru->src->flags & BB_MODIFIED)))
2218 continue;
2220 if (try_crossjump_to_edge (mode, e, fallthru, dir_forward))
2222 changed = true;
2223 ix = 0;
2224 continue;
2228 /* Non-obvious work limiting check: Recognize that we're going
2229 to call try_crossjump_bb on every basic block. So if we have
2230 two blocks with lots of outgoing edges (a switch) and they
2231 share lots of common destinations, then we would do the
2232 cross-jump check once for each common destination.
2234 Now, if the blocks actually are cross-jump candidates, then
2235 all of their destinations will be shared. Which means that
2236 we only need check them for cross-jump candidacy once. We
2237 can eliminate redundant checks of crossjump(A,B) by arbitrarily
2238 choosing to do the check from the block for which the edge
2239 in question is the first successor of A. */
2240 if (EDGE_SUCC (e->src, 0) != e)
2241 continue;
2243 for (ix2 = 0; ix2 < EDGE_COUNT (bb->preds); ix2++)
2245 e2 = EDGE_PRED (bb, ix2);
2247 if (e2 == e)
2248 continue;
2250 /* We've already checked the fallthru edge above. */
2251 if (e2 == fallthru)
2252 continue;
2254 /* The "first successor" check above only prevents multiple
2255 checks of crossjump(A,B). In order to prevent redundant
2256 checks of crossjump(B,A), require that A be the block
2257 with the lowest index. */
2258 if (e->src->index > e2->src->index)
2259 continue;
2261 /* If nothing changed since the last attempt, there is nothing
2262 we can do. */
2263 if (!first_pass
2264 && !((e->src->flags & BB_MODIFIED)
2265 || (e2->src->flags & BB_MODIFIED)))
2266 continue;
2268 /* Both e and e2 are not fallthru edges, so we can crossjump in either
2269 direction. */
2270 if (try_crossjump_to_edge (mode, e, e2, dir_both))
2272 changed = true;
2273 ix = 0;
2274 break;
2279 if (changed)
2280 crossjumps_occured = true;
2282 return changed;
2285 /* Search the successors of BB for common insn sequences. When found,
2286 share code between them by moving it across the basic block
2287 boundary. Return true if any changes made. */
2289 static bool
2290 try_head_merge_bb (basic_block bb)
2292 basic_block final_dest_bb = NULL;
2293 int max_match = INT_MAX;
2294 edge e0;
2295 rtx_insn **headptr, **currptr, **nextptr;
2296 bool changed, moveall;
2297 unsigned ix;
2298 rtx_insn *e0_last_head;
2299 rtx cond;
2300 rtx_insn *move_before;
2301 unsigned nedges = EDGE_COUNT (bb->succs);
2302 rtx_insn *jump = BB_END (bb);
2303 regset live, live_union;
2305 /* Nothing to do if there is not at least two outgoing edges. */
2306 if (nedges < 2)
2307 return false;
2309 /* Don't crossjump if this block ends in a computed jump,
2310 unless we are optimizing for size. */
2311 if (optimize_bb_for_size_p (bb)
2312 && bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
2313 && computed_jump_p (BB_END (bb)))
2314 return false;
2316 cond = get_condition (jump, &move_before, true, false);
2317 if (cond == NULL_RTX)
2319 #ifdef HAVE_cc0
2320 if (reg_mentioned_p (cc0_rtx, jump))
2321 move_before = prev_nonnote_nondebug_insn (jump);
2322 else
2323 #endif
2324 move_before = jump;
2327 for (ix = 0; ix < nedges; ix++)
2328 if (EDGE_SUCC (bb, ix)->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
2329 return false;
2331 for (ix = 0; ix < nedges; ix++)
2333 edge e = EDGE_SUCC (bb, ix);
2334 basic_block other_bb = e->dest;
2336 if (df_get_bb_dirty (other_bb))
2338 block_was_dirty = true;
2339 return false;
2342 if (e->flags & EDGE_ABNORMAL)
2343 return false;
2345 /* Normally, all destination blocks must only be reachable from this
2346 block, i.e. they must have one incoming edge.
2348 There is one special case we can handle, that of multiple consecutive
2349 jumps where the first jumps to one of the targets of the second jump.
2350 This happens frequently in switch statements for default labels.
2351 The structure is as follows:
2352 FINAL_DEST_BB
2353 ....
2354 if (cond) jump A;
2355 fall through
2357 jump with targets A, B, C, D...
2359 has two incoming edges, from FINAL_DEST_BB and BB
2361 In this case, we can try to move the insns through BB and into
2362 FINAL_DEST_BB. */
2363 if (EDGE_COUNT (other_bb->preds) != 1)
2365 edge incoming_edge, incoming_bb_other_edge;
2366 edge_iterator ei;
2368 if (final_dest_bb != NULL
2369 || EDGE_COUNT (other_bb->preds) != 2)
2370 return false;
2372 /* We must be able to move the insns across the whole block. */
2373 move_before = BB_HEAD (bb);
2374 while (!NONDEBUG_INSN_P (move_before))
2375 move_before = NEXT_INSN (move_before);
2377 if (EDGE_COUNT (bb->preds) != 1)
2378 return false;
2379 incoming_edge = EDGE_PRED (bb, 0);
2380 final_dest_bb = incoming_edge->src;
2381 if (EDGE_COUNT (final_dest_bb->succs) != 2)
2382 return false;
2383 FOR_EACH_EDGE (incoming_bb_other_edge, ei, final_dest_bb->succs)
2384 if (incoming_bb_other_edge != incoming_edge)
2385 break;
2386 if (incoming_bb_other_edge->dest != other_bb)
2387 return false;
2391 e0 = EDGE_SUCC (bb, 0);
2392 e0_last_head = NULL;
2393 changed = false;
2395 for (ix = 1; ix < nedges; ix++)
2397 edge e = EDGE_SUCC (bb, ix);
2398 rtx_insn *e0_last, *e_last;
2399 int nmatch;
2401 nmatch = flow_find_head_matching_sequence (e0->dest, e->dest,
2402 &e0_last, &e_last, 0);
2403 if (nmatch == 0)
2404 return false;
2406 if (nmatch < max_match)
2408 max_match = nmatch;
2409 e0_last_head = e0_last;
2413 /* If we matched an entire block, we probably have to avoid moving the
2414 last insn. */
2415 if (max_match > 0
2416 && e0_last_head == BB_END (e0->dest)
2417 && (find_reg_note (e0_last_head, REG_EH_REGION, 0)
2418 || control_flow_insn_p (e0_last_head)))
2420 max_match--;
2421 if (max_match == 0)
2422 return false;
2424 e0_last_head = prev_real_insn (e0_last_head);
2425 while (DEBUG_INSN_P (e0_last_head));
2428 if (max_match == 0)
2429 return false;
2431 /* We must find a union of the live registers at each of the end points. */
2432 live = BITMAP_ALLOC (NULL);
2433 live_union = BITMAP_ALLOC (NULL);
2435 currptr = XNEWVEC (rtx_insn *, nedges);
2436 headptr = XNEWVEC (rtx_insn *, nedges);
2437 nextptr = XNEWVEC (rtx_insn *, nedges);
2439 for (ix = 0; ix < nedges; ix++)
2441 int j;
2442 basic_block merge_bb = EDGE_SUCC (bb, ix)->dest;
2443 rtx_insn *head = BB_HEAD (merge_bb);
2445 while (!NONDEBUG_INSN_P (head))
2446 head = NEXT_INSN (head);
2447 headptr[ix] = head;
2448 currptr[ix] = head;
2450 /* Compute the end point and live information */
2451 for (j = 1; j < max_match; j++)
2453 head = NEXT_INSN (head);
2454 while (!NONDEBUG_INSN_P (head));
2455 simulate_backwards_to_point (merge_bb, live, head);
2456 IOR_REG_SET (live_union, live);
2459 /* If we're moving across two blocks, verify the validity of the
2460 first move, then adjust the target and let the loop below deal
2461 with the final move. */
2462 if (final_dest_bb != NULL)
2464 rtx_insn *move_upto;
2466 moveall = can_move_insns_across (currptr[0], e0_last_head, move_before,
2467 jump, e0->dest, live_union,
2468 NULL, &move_upto);
2469 if (!moveall)
2471 if (move_upto == NULL_RTX)
2472 goto out;
2474 while (e0_last_head != move_upto)
2476 df_simulate_one_insn_backwards (e0->dest, e0_last_head,
2477 live_union);
2478 e0_last_head = PREV_INSN (e0_last_head);
2481 if (e0_last_head == NULL_RTX)
2482 goto out;
2484 jump = BB_END (final_dest_bb);
2485 cond = get_condition (jump, &move_before, true, false);
2486 if (cond == NULL_RTX)
2488 #ifdef HAVE_cc0
2489 if (reg_mentioned_p (cc0_rtx, jump))
2490 move_before = prev_nonnote_nondebug_insn (jump);
2491 else
2492 #endif
2493 move_before = jump;
2499 rtx_insn *move_upto;
2500 moveall = can_move_insns_across (currptr[0], e0_last_head,
2501 move_before, jump, e0->dest, live_union,
2502 NULL, &move_upto);
2503 if (!moveall && move_upto == NULL_RTX)
2505 if (jump == move_before)
2506 break;
2508 /* Try again, using a different insertion point. */
2509 move_before = jump;
2511 #ifdef HAVE_cc0
2512 /* Don't try moving before a cc0 user, as that may invalidate
2513 the cc0. */
2514 if (reg_mentioned_p (cc0_rtx, jump))
2515 break;
2516 #endif
2518 continue;
2521 if (final_dest_bb && !moveall)
2522 /* We haven't checked whether a partial move would be OK for the first
2523 move, so we have to fail this case. */
2524 break;
2526 changed = true;
2527 for (;;)
2529 if (currptr[0] == move_upto)
2530 break;
2531 for (ix = 0; ix < nedges; ix++)
2533 rtx_insn *curr = currptr[ix];
2535 curr = NEXT_INSN (curr);
2536 while (!NONDEBUG_INSN_P (curr));
2537 currptr[ix] = curr;
2541 /* If we can't currently move all of the identical insns, remember
2542 each insn after the range that we'll merge. */
2543 if (!moveall)
2544 for (ix = 0; ix < nedges; ix++)
2546 rtx_insn *curr = currptr[ix];
2548 curr = NEXT_INSN (curr);
2549 while (!NONDEBUG_INSN_P (curr));
2550 nextptr[ix] = curr;
2553 reorder_insns (headptr[0], currptr[0], PREV_INSN (move_before));
2554 df_set_bb_dirty (EDGE_SUCC (bb, 0)->dest);
2555 if (final_dest_bb != NULL)
2556 df_set_bb_dirty (final_dest_bb);
2557 df_set_bb_dirty (bb);
2558 for (ix = 1; ix < nedges; ix++)
2560 df_set_bb_dirty (EDGE_SUCC (bb, ix)->dest);
2561 delete_insn_chain (headptr[ix], currptr[ix], false);
2563 if (!moveall)
2565 if (jump == move_before)
2566 break;
2568 /* For the unmerged insns, try a different insertion point. */
2569 move_before = jump;
2571 #ifdef HAVE_cc0
2572 /* Don't try moving before a cc0 user, as that may invalidate
2573 the cc0. */
2574 if (reg_mentioned_p (cc0_rtx, jump))
2575 break;
2576 #endif
2578 for (ix = 0; ix < nedges; ix++)
2579 currptr[ix] = headptr[ix] = nextptr[ix];
2582 while (!moveall);
2584 out:
2585 free (currptr);
2586 free (headptr);
2587 free (nextptr);
2589 crossjumps_occured |= changed;
2591 return changed;
2594 /* Return true if BB contains just bb note, or bb note followed
2595 by only DEBUG_INSNs. */
2597 static bool
2598 trivially_empty_bb_p (basic_block bb)
2600 rtx_insn *insn = BB_END (bb);
2602 while (1)
2604 if (insn == BB_HEAD (bb))
2605 return true;
2606 if (!DEBUG_INSN_P (insn))
2607 return false;
2608 insn = PREV_INSN (insn);
2612 /* Do simple CFG optimizations - basic block merging, simplifying of jump
2613 instructions etc. Return nonzero if changes were made. */
2615 static bool
2616 try_optimize_cfg (int mode)
2618 bool changed_overall = false;
2619 bool changed;
2620 int iterations = 0;
2621 basic_block bb, b, next;
2623 if (mode & (CLEANUP_CROSSJUMP | CLEANUP_THREADING))
2624 clear_bb_flags ();
2626 crossjumps_occured = false;
2628 FOR_EACH_BB_FN (bb, cfun)
2629 update_forwarder_flag (bb);
2631 if (! targetm.cannot_modify_jumps_p ())
2633 first_pass = true;
2634 /* Attempt to merge blocks as made possible by edge removal. If
2635 a block has only one successor, and the successor has only
2636 one predecessor, they may be combined. */
2639 block_was_dirty = false;
2640 changed = false;
2641 iterations++;
2643 if (dump_file)
2644 fprintf (dump_file,
2645 "\n\ntry_optimize_cfg iteration %i\n\n",
2646 iterations);
2648 for (b = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; b
2649 != EXIT_BLOCK_PTR_FOR_FN (cfun);)
2651 basic_block c;
2652 edge s;
2653 bool changed_here = false;
2655 /* Delete trivially dead basic blocks. This is either
2656 blocks with no predecessors, or empty blocks with no
2657 successors. However if the empty block with no
2658 successors is the successor of the ENTRY_BLOCK, it is
2659 kept. This ensures that the ENTRY_BLOCK will have a
2660 successor which is a precondition for many RTL
2661 passes. Empty blocks may result from expanding
2662 __builtin_unreachable (). */
2663 if (EDGE_COUNT (b->preds) == 0
2664 || (EDGE_COUNT (b->succs) == 0
2665 && trivially_empty_bb_p (b)
2666 && single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun))->dest
2667 != b))
2669 c = b->prev_bb;
2670 if (EDGE_COUNT (b->preds) > 0)
2672 edge e;
2673 edge_iterator ei;
2675 if (current_ir_type () == IR_RTL_CFGLAYOUT)
2677 if (BB_FOOTER (b)
2678 && BARRIER_P (BB_FOOTER (b)))
2679 FOR_EACH_EDGE (e, ei, b->preds)
2680 if ((e->flags & EDGE_FALLTHRU)
2681 && BB_FOOTER (e->src) == NULL)
2683 if (BB_FOOTER (b))
2685 BB_FOOTER (e->src) = BB_FOOTER (b);
2686 BB_FOOTER (b) = NULL;
2688 else
2690 start_sequence ();
2691 BB_FOOTER (e->src) = emit_barrier ();
2692 end_sequence ();
2696 else
2698 rtx_insn *last = get_last_bb_insn (b);
2699 if (last && BARRIER_P (last))
2700 FOR_EACH_EDGE (e, ei, b->preds)
2701 if ((e->flags & EDGE_FALLTHRU))
2702 emit_barrier_after (BB_END (e->src));
2705 delete_basic_block (b);
2706 changed = true;
2707 /* Avoid trying to remove the exit block. */
2708 b = (c == ENTRY_BLOCK_PTR_FOR_FN (cfun) ? c->next_bb : c);
2709 continue;
2712 /* Remove code labels no longer used. */
2713 if (single_pred_p (b)
2714 && (single_pred_edge (b)->flags & EDGE_FALLTHRU)
2715 && !(single_pred_edge (b)->flags & EDGE_COMPLEX)
2716 && LABEL_P (BB_HEAD (b))
2717 && !LABEL_PRESERVE_P (BB_HEAD (b))
2718 /* If the previous block ends with a branch to this
2719 block, we can't delete the label. Normally this
2720 is a condjump that is yet to be simplified, but
2721 if CASE_DROPS_THRU, this can be a tablejump with
2722 some element going to the same place as the
2723 default (fallthru). */
2724 && (single_pred (b) == ENTRY_BLOCK_PTR_FOR_FN (cfun)
2725 || !JUMP_P (BB_END (single_pred (b)))
2726 || ! label_is_jump_target_p (BB_HEAD (b),
2727 BB_END (single_pred (b)))))
2729 delete_insn (BB_HEAD (b));
2730 if (dump_file)
2731 fprintf (dump_file, "Deleted label in block %i.\n",
2732 b->index);
2735 /* If we fall through an empty block, we can remove it. */
2736 if (!(mode & (CLEANUP_CFGLAYOUT | CLEANUP_NO_INSN_DEL))
2737 && single_pred_p (b)
2738 && (single_pred_edge (b)->flags & EDGE_FALLTHRU)
2739 && !LABEL_P (BB_HEAD (b))
2740 && FORWARDER_BLOCK_P (b)
2741 /* Note that forwarder_block_p true ensures that
2742 there is a successor for this block. */
2743 && (single_succ_edge (b)->flags & EDGE_FALLTHRU)
2744 && n_basic_blocks_for_fn (cfun) > NUM_FIXED_BLOCKS + 1)
2746 if (dump_file)
2747 fprintf (dump_file,
2748 "Deleting fallthru block %i.\n",
2749 b->index);
2751 c = ((b->prev_bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
2752 ? b->next_bb : b->prev_bb);
2753 redirect_edge_succ_nodup (single_pred_edge (b),
2754 single_succ (b));
2755 delete_basic_block (b);
2756 changed = true;
2757 b = c;
2758 continue;
2761 /* Merge B with its single successor, if any. */
2762 if (single_succ_p (b)
2763 && (s = single_succ_edge (b))
2764 && !(s->flags & EDGE_COMPLEX)
2765 && (c = s->dest) != EXIT_BLOCK_PTR_FOR_FN (cfun)
2766 && single_pred_p (c)
2767 && b != c)
2769 /* When not in cfg_layout mode use code aware of reordering
2770 INSN. This code possibly creates new basic blocks so it
2771 does not fit merge_blocks interface and is kept here in
2772 hope that it will become useless once more of compiler
2773 is transformed to use cfg_layout mode. */
2775 if ((mode & CLEANUP_CFGLAYOUT)
2776 && can_merge_blocks_p (b, c))
2778 merge_blocks (b, c);
2779 update_forwarder_flag (b);
2780 changed_here = true;
2782 else if (!(mode & CLEANUP_CFGLAYOUT)
2783 /* If the jump insn has side effects,
2784 we can't kill the edge. */
2785 && (!JUMP_P (BB_END (b))
2786 || (reload_completed
2787 ? simplejump_p (BB_END (b))
2788 : (onlyjump_p (BB_END (b))
2789 && !tablejump_p (BB_END (b),
2790 NULL, NULL))))
2791 && (next = merge_blocks_move (s, b, c, mode)))
2793 b = next;
2794 changed_here = true;
2798 /* Simplify branch over branch. */
2799 if ((mode & CLEANUP_EXPENSIVE)
2800 && !(mode & CLEANUP_CFGLAYOUT)
2801 && try_simplify_condjump (b))
2802 changed_here = true;
2804 /* If B has a single outgoing edge, but uses a
2805 non-trivial jump instruction without side-effects, we
2806 can either delete the jump entirely, or replace it
2807 with a simple unconditional jump. */
2808 if (single_succ_p (b)
2809 && single_succ (b) != EXIT_BLOCK_PTR_FOR_FN (cfun)
2810 && onlyjump_p (BB_END (b))
2811 && !CROSSING_JUMP_P (BB_END (b))
2812 && try_redirect_by_replacing_jump (single_succ_edge (b),
2813 single_succ (b),
2814 (mode & CLEANUP_CFGLAYOUT) != 0))
2816 update_forwarder_flag (b);
2817 changed_here = true;
2820 /* Simplify branch to branch. */
2821 if (try_forward_edges (mode, b))
2823 update_forwarder_flag (b);
2824 changed_here = true;
2827 /* Look for shared code between blocks. */
2828 if ((mode & CLEANUP_CROSSJUMP)
2829 && try_crossjump_bb (mode, b))
2830 changed_here = true;
2832 if ((mode & CLEANUP_CROSSJUMP)
2833 /* This can lengthen register lifetimes. Do it only after
2834 reload. */
2835 && reload_completed
2836 && try_head_merge_bb (b))
2837 changed_here = true;
2839 /* Don't get confused by the index shift caused by
2840 deleting blocks. */
2841 if (!changed_here)
2842 b = b->next_bb;
2843 else
2844 changed = true;
2847 if ((mode & CLEANUP_CROSSJUMP)
2848 && try_crossjump_bb (mode, EXIT_BLOCK_PTR_FOR_FN (cfun)))
2849 changed = true;
2851 if (block_was_dirty)
2853 /* This should only be set by head-merging. */
2854 gcc_assert (mode & CLEANUP_CROSSJUMP);
2855 df_analyze ();
2858 if (changed)
2860 /* Edge forwarding in particular can cause hot blocks previously
2861 reached by both hot and cold blocks to become dominated only
2862 by cold blocks. This will cause the verification below to fail,
2863 and lead to now cold code in the hot section. This is not easy
2864 to detect and fix during edge forwarding, and in some cases
2865 is only visible after newly unreachable blocks are deleted,
2866 which will be done in fixup_partitions. */
2867 fixup_partitions ();
2869 #ifdef ENABLE_CHECKING
2870 verify_flow_info ();
2871 #endif
2874 changed_overall |= changed;
2875 first_pass = false;
2877 while (changed);
2880 FOR_ALL_BB_FN (b, cfun)
2881 b->flags &= ~(BB_FORWARDER_BLOCK | BB_NONTHREADABLE_BLOCK);
2883 return changed_overall;
2886 /* Delete all unreachable basic blocks. */
2888 bool
2889 delete_unreachable_blocks (void)
2891 bool changed = false;
2892 basic_block b, prev_bb;
2894 find_unreachable_blocks ();
2896 /* When we're in GIMPLE mode and there may be debug insns, we should
2897 delete blocks in reverse dominator order, so as to get a chance
2898 to substitute all released DEFs into debug stmts. If we don't
2899 have dominators information, walking blocks backward gets us a
2900 better chance of retaining most debug information than
2901 otherwise. */
2902 if (MAY_HAVE_DEBUG_INSNS && current_ir_type () == IR_GIMPLE
2903 && dom_info_available_p (CDI_DOMINATORS))
2905 for (b = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
2906 b != ENTRY_BLOCK_PTR_FOR_FN (cfun); b = prev_bb)
2908 prev_bb = b->prev_bb;
2910 if (!(b->flags & BB_REACHABLE))
2912 /* Speed up the removal of blocks that don't dominate
2913 others. Walking backwards, this should be the common
2914 case. */
2915 if (!first_dom_son (CDI_DOMINATORS, b))
2916 delete_basic_block (b);
2917 else
2919 vec<basic_block> h
2920 = get_all_dominated_blocks (CDI_DOMINATORS, b);
2922 while (h.length ())
2924 b = h.pop ();
2926 prev_bb = b->prev_bb;
2928 gcc_assert (!(b->flags & BB_REACHABLE));
2930 delete_basic_block (b);
2933 h.release ();
2936 changed = true;
2940 else
2942 for (b = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
2943 b != ENTRY_BLOCK_PTR_FOR_FN (cfun); b = prev_bb)
2945 prev_bb = b->prev_bb;
2947 if (!(b->flags & BB_REACHABLE))
2949 delete_basic_block (b);
2950 changed = true;
2955 if (changed)
2956 tidy_fallthru_edges ();
2957 return changed;
2960 /* Delete any jump tables never referenced. We can't delete them at the
2961 time of removing tablejump insn as they are referenced by the preceding
2962 insns computing the destination, so we delay deleting and garbagecollect
2963 them once life information is computed. */
2964 void
2965 delete_dead_jumptables (void)
2967 basic_block bb;
2969 /* A dead jump table does not belong to any basic block. Scan insns
2970 between two adjacent basic blocks. */
2971 FOR_EACH_BB_FN (bb, cfun)
2973 rtx_insn *insn, *next;
2975 for (insn = NEXT_INSN (BB_END (bb));
2976 insn && !NOTE_INSN_BASIC_BLOCK_P (insn);
2977 insn = next)
2979 next = NEXT_INSN (insn);
2980 if (LABEL_P (insn)
2981 && LABEL_NUSES (insn) == LABEL_PRESERVE_P (insn)
2982 && JUMP_TABLE_DATA_P (next))
2984 rtx_insn *label = insn, *jump = next;
2986 if (dump_file)
2987 fprintf (dump_file, "Dead jumptable %i removed\n",
2988 INSN_UID (insn));
2990 next = NEXT_INSN (next);
2991 delete_insn (jump);
2992 delete_insn (label);
2999 /* Tidy the CFG by deleting unreachable code and whatnot. */
3001 bool
3002 cleanup_cfg (int mode)
3004 bool changed = false;
3006 /* Set the cfglayout mode flag here. We could update all the callers
3007 but that is just inconvenient, especially given that we eventually
3008 want to have cfglayout mode as the default. */
3009 if (current_ir_type () == IR_RTL_CFGLAYOUT)
3010 mode |= CLEANUP_CFGLAYOUT;
3012 timevar_push (TV_CLEANUP_CFG);
3013 if (delete_unreachable_blocks ())
3015 changed = true;
3016 /* We've possibly created trivially dead code. Cleanup it right
3017 now to introduce more opportunities for try_optimize_cfg. */
3018 if (!(mode & (CLEANUP_NO_INSN_DEL))
3019 && !reload_completed)
3020 delete_trivially_dead_insns (get_insns (), max_reg_num ());
3023 compact_blocks ();
3025 /* To tail-merge blocks ending in the same noreturn function (e.g.
3026 a call to abort) we have to insert fake edges to exit. Do this
3027 here once. The fake edges do not interfere with any other CFG
3028 cleanups. */
3029 if (mode & CLEANUP_CROSSJUMP)
3030 add_noreturn_fake_exit_edges ();
3032 if (!dbg_cnt (cfg_cleanup))
3033 return changed;
3035 while (try_optimize_cfg (mode))
3037 delete_unreachable_blocks (), changed = true;
3038 if (!(mode & CLEANUP_NO_INSN_DEL))
3040 /* Try to remove some trivially dead insns when doing an expensive
3041 cleanup. But delete_trivially_dead_insns doesn't work after
3042 reload (it only handles pseudos) and run_fast_dce is too costly
3043 to run in every iteration.
3045 For effective cross jumping, we really want to run a fast DCE to
3046 clean up any dead conditions, or they get in the way of performing
3047 useful tail merges.
3049 Other transformations in cleanup_cfg are not so sensitive to dead
3050 code, so delete_trivially_dead_insns or even doing nothing at all
3051 is good enough. */
3052 if ((mode & CLEANUP_EXPENSIVE) && !reload_completed
3053 && !delete_trivially_dead_insns (get_insns (), max_reg_num ()))
3054 break;
3055 if ((mode & CLEANUP_CROSSJUMP) && crossjumps_occured)
3056 run_fast_dce ();
3058 else
3059 break;
3062 if (mode & CLEANUP_CROSSJUMP)
3063 remove_fake_exit_edges ();
3065 /* Don't call delete_dead_jumptables in cfglayout mode, because
3066 that function assumes that jump tables are in the insns stream.
3067 But we also don't _have_ to delete dead jumptables in cfglayout
3068 mode because we shouldn't even be looking at things that are
3069 not in a basic block. Dead jumptables are cleaned up when
3070 going out of cfglayout mode. */
3071 if (!(mode & CLEANUP_CFGLAYOUT))
3072 delete_dead_jumptables ();
3074 /* ??? We probably do this way too often. */
3075 if (current_loops
3076 && (changed
3077 || (mode & CLEANUP_CFG_CHANGED)))
3079 timevar_push (TV_REPAIR_LOOPS);
3080 /* The above doesn't preserve dominance info if available. */
3081 gcc_assert (!dom_info_available_p (CDI_DOMINATORS));
3082 calculate_dominance_info (CDI_DOMINATORS);
3083 fix_loop_structure (NULL);
3084 free_dominance_info (CDI_DOMINATORS);
3085 timevar_pop (TV_REPAIR_LOOPS);
3088 timevar_pop (TV_CLEANUP_CFG);
3090 return changed;
3093 namespace {
3095 const pass_data pass_data_jump =
3097 RTL_PASS, /* type */
3098 "jump", /* name */
3099 OPTGROUP_NONE, /* optinfo_flags */
3100 TV_JUMP, /* tv_id */
3101 0, /* properties_required */
3102 0, /* properties_provided */
3103 0, /* properties_destroyed */
3104 0, /* todo_flags_start */
3105 0, /* todo_flags_finish */
3108 class pass_jump : public rtl_opt_pass
3110 public:
3111 pass_jump (gcc::context *ctxt)
3112 : rtl_opt_pass (pass_data_jump, ctxt)
3115 /* opt_pass methods: */
3116 virtual unsigned int execute (function *);
3118 }; // class pass_jump
3120 unsigned int
3121 pass_jump::execute (function *)
3123 delete_trivially_dead_insns (get_insns (), max_reg_num ());
3124 if (dump_file)
3125 dump_flow_info (dump_file, dump_flags);
3126 cleanup_cfg ((optimize ? CLEANUP_EXPENSIVE : 0)
3127 | (flag_thread_jumps ? CLEANUP_THREADING : 0));
3128 return 0;
3131 } // anon namespace
3133 rtl_opt_pass *
3134 make_pass_jump (gcc::context *ctxt)
3136 return new pass_jump (ctxt);
3139 namespace {
3141 const pass_data pass_data_jump2 =
3143 RTL_PASS, /* type */
3144 "jump2", /* name */
3145 OPTGROUP_NONE, /* optinfo_flags */
3146 TV_JUMP, /* tv_id */
3147 0, /* properties_required */
3148 0, /* properties_provided */
3149 0, /* properties_destroyed */
3150 0, /* todo_flags_start */
3151 0, /* todo_flags_finish */
3154 class pass_jump2 : public rtl_opt_pass
3156 public:
3157 pass_jump2 (gcc::context *ctxt)
3158 : rtl_opt_pass (pass_data_jump2, ctxt)
3161 /* opt_pass methods: */
3162 virtual unsigned int execute (function *)
3164 cleanup_cfg (flag_crossjumping ? CLEANUP_CROSSJUMP : 0);
3165 return 0;
3168 }; // class pass_jump2
3170 } // anon namespace
3172 rtl_opt_pass *
3173 make_pass_jump2 (gcc::context *ctxt)
3175 return new pass_jump2 (ctxt);