[AArch64] PR target/65491: Classify V1TF vectors as AAPCS64 short vectors rather...
[official-gcc.git] / gcc / cfgcleanup.c
blobaff64efcca9967665873635035259acb9ef5621a
1 /* Control flow optimization code for GNU compiler.
2 Copyright (C) 1987-2015 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 "hash-set.h"
38 #include "machmode.h"
39 #include "vec.h"
40 #include "double-int.h"
41 #include "input.h"
42 #include "alias.h"
43 #include "symtab.h"
44 #include "wide-int.h"
45 #include "inchash.h"
46 #include "tree.h"
47 #include "hard-reg-set.h"
48 #include "regs.h"
49 #include "insn-config.h"
50 #include "flags.h"
51 #include "recog.h"
52 #include "diagnostic-core.h"
53 #include "cselib.h"
54 #include "params.h"
55 #include "tm_p.h"
56 #include "target.h"
57 #include "hashtab.h"
58 #include "function.h" /* For inline functions in emit-rtl.h they need crtl. */
59 #include "emit-rtl.h"
60 #include "tree-pass.h"
61 #include "cfgloop.h"
62 #include "function.h"
63 #include "statistics.h"
64 #include "real.h"
65 #include "fixed-value.h"
66 #include "expmed.h"
67 #include "dojump.h"
68 #include "explow.h"
69 #include "calls.h"
70 #include "varasm.h"
71 #include "stmt.h"
72 #include "expr.h"
73 #include "dominance.h"
74 #include "cfg.h"
75 #include "cfgrtl.h"
76 #include "cfganal.h"
77 #include "cfgbuild.h"
78 #include "cfgcleanup.h"
79 #include "predict.h"
80 #include "basic-block.h"
81 #include "df.h"
82 #include "dce.h"
83 #include "dbgcnt.h"
84 #include "rtl-iter.h"
86 #define FORWARDER_BLOCK_P(BB) ((BB)->flags & BB_FORWARDER_BLOCK)
88 /* Set to true when we are running first pass of try_optimize_cfg loop. */
89 static bool first_pass;
91 /* Set to true if crossjumps occurred in the latest run of try_optimize_cfg. */
92 static bool crossjumps_occured;
94 /* Set to true if we couldn't run an optimization due to stale liveness
95 information; we should run df_analyze to enable more opportunities. */
96 static bool block_was_dirty;
98 static bool try_crossjump_to_edge (int, edge, edge, enum replace_direction);
99 static bool try_crossjump_bb (int, basic_block);
100 static bool outgoing_edges_match (int, basic_block, basic_block);
101 static enum replace_direction old_insns_match_p (int, rtx_insn *, rtx_insn *);
103 static void merge_blocks_move_predecessor_nojumps (basic_block, basic_block);
104 static void merge_blocks_move_successor_nojumps (basic_block, basic_block);
105 static bool try_optimize_cfg (int);
106 static bool try_simplify_condjump (basic_block);
107 static bool try_forward_edges (int, basic_block);
108 static edge thread_jump (edge, basic_block);
109 static bool mark_effect (rtx, bitmap);
110 static void notice_new_block (basic_block);
111 static void update_forwarder_flag (basic_block);
112 static void merge_memattrs (rtx, rtx);
114 /* Set flags for newly created block. */
116 static void
117 notice_new_block (basic_block bb)
119 if (!bb)
120 return;
122 if (forwarder_block_p (bb))
123 bb->flags |= BB_FORWARDER_BLOCK;
126 /* Recompute forwarder flag after block has been modified. */
128 static void
129 update_forwarder_flag (basic_block bb)
131 if (forwarder_block_p (bb))
132 bb->flags |= BB_FORWARDER_BLOCK;
133 else
134 bb->flags &= ~BB_FORWARDER_BLOCK;
137 /* Simplify a conditional jump around an unconditional jump.
138 Return true if something changed. */
140 static bool
141 try_simplify_condjump (basic_block cbranch_block)
143 basic_block jump_block, jump_dest_block, cbranch_dest_block;
144 edge cbranch_jump_edge, cbranch_fallthru_edge;
145 rtx_insn *cbranch_insn;
147 /* Verify that there are exactly two successors. */
148 if (EDGE_COUNT (cbranch_block->succs) != 2)
149 return false;
151 /* Verify that we've got a normal conditional branch at the end
152 of the block. */
153 cbranch_insn = BB_END (cbranch_block);
154 if (!any_condjump_p (cbranch_insn))
155 return false;
157 cbranch_fallthru_edge = FALLTHRU_EDGE (cbranch_block);
158 cbranch_jump_edge = BRANCH_EDGE (cbranch_block);
160 /* The next block must not have multiple predecessors, must not
161 be the last block in the function, and must contain just the
162 unconditional jump. */
163 jump_block = cbranch_fallthru_edge->dest;
164 if (!single_pred_p (jump_block)
165 || jump_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
166 || !FORWARDER_BLOCK_P (jump_block))
167 return false;
168 jump_dest_block = single_succ (jump_block);
170 /* If we are partitioning hot/cold basic blocks, we don't want to
171 mess up unconditional or indirect jumps that cross between hot
172 and cold sections.
174 Basic block partitioning may result in some jumps that appear to
175 be optimizable (or blocks that appear to be mergeable), but which really
176 must be left untouched (they are required to make it safely across
177 partition boundaries). See the comments at the top of
178 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
180 if (BB_PARTITION (jump_block) != BB_PARTITION (jump_dest_block)
181 || (cbranch_jump_edge->flags & EDGE_CROSSING))
182 return false;
184 /* The conditional branch must target the block after the
185 unconditional branch. */
186 cbranch_dest_block = cbranch_jump_edge->dest;
188 if (cbranch_dest_block == EXIT_BLOCK_PTR_FOR_FN (cfun)
189 || !can_fallthru (jump_block, cbranch_dest_block))
190 return false;
192 /* Invert the conditional branch. */
193 if (!invert_jump (as_a <rtx_jump_insn *> (cbranch_insn),
194 block_label (jump_dest_block), 0))
195 return false;
197 if (dump_file)
198 fprintf (dump_file, "Simplifying condjump %i around jump %i\n",
199 INSN_UID (cbranch_insn), INSN_UID (BB_END (jump_block)));
201 /* Success. Update the CFG to match. Note that after this point
202 the edge variable names appear backwards; the redirection is done
203 this way to preserve edge profile data. */
204 cbranch_jump_edge = redirect_edge_succ_nodup (cbranch_jump_edge,
205 cbranch_dest_block);
206 cbranch_fallthru_edge = redirect_edge_succ_nodup (cbranch_fallthru_edge,
207 jump_dest_block);
208 cbranch_jump_edge->flags |= EDGE_FALLTHRU;
209 cbranch_fallthru_edge->flags &= ~EDGE_FALLTHRU;
210 update_br_prob_note (cbranch_block);
212 /* Delete the block with the unconditional jump, and clean up the mess. */
213 delete_basic_block (jump_block);
214 tidy_fallthru_edge (cbranch_jump_edge);
215 update_forwarder_flag (cbranch_block);
217 return true;
220 /* Attempt to prove that operation is NOOP using CSElib or mark the effect
221 on register. Used by jump threading. */
223 static bool
224 mark_effect (rtx exp, regset nonequal)
226 rtx dest;
227 switch (GET_CODE (exp))
229 /* In case we do clobber the register, mark it as equal, as we know the
230 value is dead so it don't have to match. */
231 case CLOBBER:
232 dest = XEXP (exp, 0);
233 if (REG_P (dest))
234 bitmap_clear_range (nonequal, REGNO (dest), REG_NREGS (dest));
235 return false;
237 case SET:
238 if (rtx_equal_for_cselib_p (SET_DEST (exp), SET_SRC (exp)))
239 return false;
240 dest = SET_DEST (exp);
241 if (dest == pc_rtx)
242 return false;
243 if (!REG_P (dest))
244 return true;
245 bitmap_set_range (nonequal, REGNO (dest), REG_NREGS (dest));
246 return false;
248 default:
249 return false;
253 /* Return true if X contains a register in NONEQUAL. */
254 static bool
255 mentions_nonequal_regs (const_rtx x, regset nonequal)
257 subrtx_iterator::array_type array;
258 FOR_EACH_SUBRTX (iter, array, x, NONCONST)
260 const_rtx x = *iter;
261 if (REG_P (x))
263 unsigned int end_regno = END_REGNO (x);
264 for (unsigned int regno = REGNO (x); regno < end_regno; ++regno)
265 if (REGNO_REG_SET_P (nonequal, regno))
266 return true;
269 return false;
272 /* Attempt to prove that the basic block B will have no side effects and
273 always continues in the same edge if reached via E. Return the edge
274 if exist, NULL otherwise. */
276 static edge
277 thread_jump (edge e, basic_block b)
279 rtx set1, set2, cond1, cond2;
280 rtx_insn *insn;
281 enum rtx_code code1, code2, reversed_code2;
282 bool reverse1 = false;
283 unsigned i;
284 regset nonequal;
285 bool failed = false;
286 reg_set_iterator rsi;
288 if (b->flags & BB_NONTHREADABLE_BLOCK)
289 return NULL;
291 /* At the moment, we do handle only conditional jumps, but later we may
292 want to extend this code to tablejumps and others. */
293 if (EDGE_COUNT (e->src->succs) != 2)
294 return NULL;
295 if (EDGE_COUNT (b->succs) != 2)
297 b->flags |= BB_NONTHREADABLE_BLOCK;
298 return NULL;
301 /* Second branch must end with onlyjump, as we will eliminate the jump. */
302 if (!any_condjump_p (BB_END (e->src)))
303 return NULL;
305 if (!any_condjump_p (BB_END (b)) || !onlyjump_p (BB_END (b)))
307 b->flags |= BB_NONTHREADABLE_BLOCK;
308 return NULL;
311 set1 = pc_set (BB_END (e->src));
312 set2 = pc_set (BB_END (b));
313 if (((e->flags & EDGE_FALLTHRU) != 0)
314 != (XEXP (SET_SRC (set1), 1) == pc_rtx))
315 reverse1 = true;
317 cond1 = XEXP (SET_SRC (set1), 0);
318 cond2 = XEXP (SET_SRC (set2), 0);
319 if (reverse1)
320 code1 = reversed_comparison_code (cond1, BB_END (e->src));
321 else
322 code1 = GET_CODE (cond1);
324 code2 = GET_CODE (cond2);
325 reversed_code2 = reversed_comparison_code (cond2, BB_END (b));
327 if (!comparison_dominates_p (code1, code2)
328 && !comparison_dominates_p (code1, reversed_code2))
329 return NULL;
331 /* Ensure that the comparison operators are equivalent.
332 ??? This is far too pessimistic. We should allow swapped operands,
333 different CCmodes, or for example comparisons for interval, that
334 dominate even when operands are not equivalent. */
335 if (!rtx_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
336 || !rtx_equal_p (XEXP (cond1, 1), XEXP (cond2, 1)))
337 return NULL;
339 /* Short circuit cases where block B contains some side effects, as we can't
340 safely bypass it. */
341 for (insn = NEXT_INSN (BB_HEAD (b)); insn != NEXT_INSN (BB_END (b));
342 insn = NEXT_INSN (insn))
343 if (INSN_P (insn) && side_effects_p (PATTERN (insn)))
345 b->flags |= BB_NONTHREADABLE_BLOCK;
346 return NULL;
349 cselib_init (0);
351 /* First process all values computed in the source basic block. */
352 for (insn = NEXT_INSN (BB_HEAD (e->src));
353 insn != NEXT_INSN (BB_END (e->src));
354 insn = NEXT_INSN (insn))
355 if (INSN_P (insn))
356 cselib_process_insn (insn);
358 nonequal = BITMAP_ALLOC (NULL);
359 CLEAR_REG_SET (nonequal);
361 /* Now assume that we've continued by the edge E to B and continue
362 processing as if it were same basic block.
363 Our goal is to prove that whole block is an NOOP. */
365 for (insn = NEXT_INSN (BB_HEAD (b));
366 insn != NEXT_INSN (BB_END (b)) && !failed;
367 insn = NEXT_INSN (insn))
369 if (INSN_P (insn))
371 rtx pat = PATTERN (insn);
373 if (GET_CODE (pat) == PARALLEL)
375 for (i = 0; i < (unsigned)XVECLEN (pat, 0); i++)
376 failed |= mark_effect (XVECEXP (pat, 0, i), nonequal);
378 else
379 failed |= mark_effect (pat, nonequal);
382 cselib_process_insn (insn);
385 /* Later we should clear nonequal of dead registers. So far we don't
386 have life information in cfg_cleanup. */
387 if (failed)
389 b->flags |= BB_NONTHREADABLE_BLOCK;
390 goto failed_exit;
393 /* cond2 must not mention any register that is not equal to the
394 former block. */
395 if (mentions_nonequal_regs (cond2, nonequal))
396 goto failed_exit;
398 EXECUTE_IF_SET_IN_REG_SET (nonequal, 0, i, rsi)
399 goto failed_exit;
401 BITMAP_FREE (nonequal);
402 cselib_finish ();
403 if ((comparison_dominates_p (code1, code2) != 0)
404 != (XEXP (SET_SRC (set2), 1) == pc_rtx))
405 return BRANCH_EDGE (b);
406 else
407 return FALLTHRU_EDGE (b);
409 failed_exit:
410 BITMAP_FREE (nonequal);
411 cselib_finish ();
412 return NULL;
415 /* Attempt to forward edges leaving basic block B.
416 Return true if successful. */
418 static bool
419 try_forward_edges (int mode, basic_block b)
421 bool changed = false;
422 edge_iterator ei;
423 edge e, *threaded_edges = NULL;
425 /* If we are partitioning hot/cold basic blocks, we don't want to
426 mess up unconditional or indirect jumps that cross between hot
427 and cold sections.
429 Basic block partitioning may result in some jumps that appear to
430 be optimizable (or blocks that appear to be mergeable), but which really
431 must be left untouched (they are required to make it safely across
432 partition boundaries). See the comments at the top of
433 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
435 if (JUMP_P (BB_END (b)) && CROSSING_JUMP_P (BB_END (b)))
436 return false;
438 for (ei = ei_start (b->succs); (e = ei_safe_edge (ei)); )
440 basic_block target, first;
441 location_t goto_locus;
442 int counter;
443 bool threaded = false;
444 int nthreaded_edges = 0;
445 bool may_thread = first_pass || (b->flags & BB_MODIFIED) != 0;
447 /* Skip complex edges because we don't know how to update them.
449 Still handle fallthru edges, as we can succeed to forward fallthru
450 edge to the same place as the branch edge of conditional branch
451 and turn conditional branch to an unconditional branch. */
452 if (e->flags & EDGE_COMPLEX)
454 ei_next (&ei);
455 continue;
458 target = first = e->dest;
459 counter = NUM_FIXED_BLOCKS;
460 goto_locus = e->goto_locus;
462 /* If we are partitioning hot/cold basic_blocks, we don't want to mess
463 up jumps that cross between hot/cold sections.
465 Basic block partitioning may result in some jumps that appear
466 to be optimizable (or blocks that appear to be mergeable), but which
467 really must be left untouched (they are required to make it safely
468 across partition boundaries). See the comments at the top of
469 bb-reorder.c:partition_hot_cold_basic_blocks for complete
470 details. */
472 if (first != EXIT_BLOCK_PTR_FOR_FN (cfun)
473 && JUMP_P (BB_END (first))
474 && CROSSING_JUMP_P (BB_END (first)))
475 return changed;
477 while (counter < n_basic_blocks_for_fn (cfun))
479 basic_block new_target = NULL;
480 bool new_target_threaded = false;
481 may_thread |= (target->flags & BB_MODIFIED) != 0;
483 if (FORWARDER_BLOCK_P (target)
484 && !(single_succ_edge (target)->flags & EDGE_CROSSING)
485 && single_succ (target) != EXIT_BLOCK_PTR_FOR_FN (cfun))
487 /* Bypass trivial infinite loops. */
488 new_target = single_succ (target);
489 if (target == new_target)
490 counter = n_basic_blocks_for_fn (cfun);
491 else if (!optimize)
493 /* When not optimizing, ensure that edges or forwarder
494 blocks with different locus are not optimized out. */
495 location_t new_locus = single_succ_edge (target)->goto_locus;
496 location_t locus = goto_locus;
498 if (LOCATION_LOCUS (new_locus) != UNKNOWN_LOCATION
499 && LOCATION_LOCUS (locus) != UNKNOWN_LOCATION
500 && new_locus != locus)
501 new_target = NULL;
502 else
504 if (LOCATION_LOCUS (new_locus) != UNKNOWN_LOCATION)
505 locus = new_locus;
507 rtx_insn *last = BB_END (target);
508 if (DEBUG_INSN_P (last))
509 last = prev_nondebug_insn (last);
510 if (last && INSN_P (last))
511 new_locus = INSN_LOCATION (last);
512 else
513 new_locus = UNKNOWN_LOCATION;
515 if (LOCATION_LOCUS (new_locus) != UNKNOWN_LOCATION
516 && LOCATION_LOCUS (locus) != UNKNOWN_LOCATION
517 && new_locus != locus)
518 new_target = NULL;
519 else
521 if (LOCATION_LOCUS (new_locus) != UNKNOWN_LOCATION)
522 locus = new_locus;
524 goto_locus = locus;
530 /* Allow to thread only over one edge at time to simplify updating
531 of probabilities. */
532 else if ((mode & CLEANUP_THREADING) && may_thread)
534 edge t = thread_jump (e, target);
535 if (t)
537 if (!threaded_edges)
538 threaded_edges = XNEWVEC (edge,
539 n_basic_blocks_for_fn (cfun));
540 else
542 int i;
544 /* Detect an infinite loop across blocks not
545 including the start block. */
546 for (i = 0; i < nthreaded_edges; ++i)
547 if (threaded_edges[i] == t)
548 break;
549 if (i < nthreaded_edges)
551 counter = n_basic_blocks_for_fn (cfun);
552 break;
556 /* Detect an infinite loop across the start block. */
557 if (t->dest == b)
558 break;
560 gcc_assert (nthreaded_edges
561 < (n_basic_blocks_for_fn (cfun)
562 - NUM_FIXED_BLOCKS));
563 threaded_edges[nthreaded_edges++] = t;
565 new_target = t->dest;
566 new_target_threaded = true;
570 if (!new_target)
571 break;
573 counter++;
574 target = new_target;
575 threaded |= new_target_threaded;
578 if (counter >= n_basic_blocks_for_fn (cfun))
580 if (dump_file)
581 fprintf (dump_file, "Infinite loop in BB %i.\n",
582 target->index);
584 else if (target == first)
585 ; /* We didn't do anything. */
586 else
588 /* Save the values now, as the edge may get removed. */
589 gcov_type edge_count = e->count;
590 int edge_probability = e->probability;
591 int edge_frequency;
592 int n = 0;
594 e->goto_locus = goto_locus;
596 /* Don't force if target is exit block. */
597 if (threaded && target != EXIT_BLOCK_PTR_FOR_FN (cfun))
599 notice_new_block (redirect_edge_and_branch_force (e, target));
600 if (dump_file)
601 fprintf (dump_file, "Conditionals threaded.\n");
603 else if (!redirect_edge_and_branch (e, target))
605 if (dump_file)
606 fprintf (dump_file,
607 "Forwarding edge %i->%i to %i failed.\n",
608 b->index, e->dest->index, target->index);
609 ei_next (&ei);
610 continue;
613 /* We successfully forwarded the edge. Now update profile
614 data: for each edge we traversed in the chain, remove
615 the original edge's execution count. */
616 edge_frequency = apply_probability (b->frequency, edge_probability);
620 edge t;
622 if (!single_succ_p (first))
624 gcc_assert (n < nthreaded_edges);
625 t = threaded_edges [n++];
626 gcc_assert (t->src == first);
627 update_bb_profile_for_threading (first, edge_frequency,
628 edge_count, t);
629 update_br_prob_note (first);
631 else
633 first->count -= edge_count;
634 if (first->count < 0)
635 first->count = 0;
636 first->frequency -= edge_frequency;
637 if (first->frequency < 0)
638 first->frequency = 0;
639 /* It is possible that as the result of
640 threading we've removed edge as it is
641 threaded to the fallthru edge. Avoid
642 getting out of sync. */
643 if (n < nthreaded_edges
644 && first == threaded_edges [n]->src)
645 n++;
646 t = single_succ_edge (first);
649 t->count -= edge_count;
650 if (t->count < 0)
651 t->count = 0;
652 first = t->dest;
654 while (first != target);
656 changed = true;
657 continue;
659 ei_next (&ei);
662 free (threaded_edges);
663 return changed;
667 /* Blocks A and B are to be merged into a single block. A has no incoming
668 fallthru edge, so it can be moved before B without adding or modifying
669 any jumps (aside from the jump from A to B). */
671 static void
672 merge_blocks_move_predecessor_nojumps (basic_block a, basic_block b)
674 rtx_insn *barrier;
676 /* If we are partitioning hot/cold basic blocks, we don't want to
677 mess up unconditional or indirect jumps that cross between hot
678 and cold sections.
680 Basic block partitioning may result in some jumps that appear to
681 be optimizable (or blocks that appear to be mergeable), but which really
682 must be left untouched (they are required to make it safely across
683 partition boundaries). See the comments at the top of
684 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
686 if (BB_PARTITION (a) != BB_PARTITION (b))
687 return;
689 barrier = next_nonnote_insn (BB_END (a));
690 gcc_assert (BARRIER_P (barrier));
691 delete_insn (barrier);
693 /* Scramble the insn chain. */
694 if (BB_END (a) != PREV_INSN (BB_HEAD (b)))
695 reorder_insns_nobb (BB_HEAD (a), BB_END (a), PREV_INSN (BB_HEAD (b)));
696 df_set_bb_dirty (a);
698 if (dump_file)
699 fprintf (dump_file, "Moved block %d before %d and merged.\n",
700 a->index, b->index);
702 /* Swap the records for the two blocks around. */
704 unlink_block (a);
705 link_block (a, b->prev_bb);
707 /* Now blocks A and B are contiguous. Merge them. */
708 merge_blocks (a, b);
711 /* Blocks A and B are to be merged into a single block. B has no outgoing
712 fallthru edge, so it can be moved after A without adding or modifying
713 any jumps (aside from the jump from A to B). */
715 static void
716 merge_blocks_move_successor_nojumps (basic_block a, basic_block b)
718 rtx_insn *barrier, *real_b_end;
719 rtx label;
720 rtx_jump_table_data *table;
722 /* If we are partitioning hot/cold basic blocks, we don't want to
723 mess up unconditional or indirect jumps that cross between hot
724 and cold sections.
726 Basic block partitioning may result in some jumps that appear to
727 be optimizable (or blocks that appear to be mergeable), but which really
728 must be left untouched (they are required to make it safely across
729 partition boundaries). See the comments at the top of
730 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
732 if (BB_PARTITION (a) != BB_PARTITION (b))
733 return;
735 real_b_end = BB_END (b);
737 /* If there is a jump table following block B temporarily add the jump table
738 to block B so that it will also be moved to the correct location. */
739 if (tablejump_p (BB_END (b), &label, &table)
740 && prev_active_insn (label) == BB_END (b))
742 BB_END (b) = table;
745 /* There had better have been a barrier there. Delete it. */
746 barrier = NEXT_INSN (BB_END (b));
747 if (barrier && BARRIER_P (barrier))
748 delete_insn (barrier);
751 /* Scramble the insn chain. */
752 reorder_insns_nobb (BB_HEAD (b), BB_END (b), BB_END (a));
754 /* Restore the real end of b. */
755 BB_END (b) = real_b_end;
757 if (dump_file)
758 fprintf (dump_file, "Moved block %d after %d and merged.\n",
759 b->index, a->index);
761 /* Now blocks A and B are contiguous. Merge them. */
762 merge_blocks (a, b);
765 /* Attempt to merge basic blocks that are potentially non-adjacent.
766 Return NULL iff the attempt failed, otherwise return basic block
767 where cleanup_cfg should continue. Because the merging commonly
768 moves basic block away or introduces another optimization
769 possibility, return basic block just before B so cleanup_cfg don't
770 need to iterate.
772 It may be good idea to return basic block before C in the case
773 C has been moved after B and originally appeared earlier in the
774 insn sequence, but we have no information available about the
775 relative ordering of these two. Hopefully it is not too common. */
777 static basic_block
778 merge_blocks_move (edge e, basic_block b, basic_block c, int mode)
780 basic_block next;
782 /* If we are partitioning hot/cold basic blocks, we don't want to
783 mess up unconditional or indirect jumps that cross between hot
784 and cold sections.
786 Basic block partitioning may result in some jumps that appear to
787 be optimizable (or blocks that appear to be mergeable), but which really
788 must be left untouched (they are required to make it safely across
789 partition boundaries). See the comments at the top of
790 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
792 if (BB_PARTITION (b) != BB_PARTITION (c))
793 return NULL;
795 /* If B has a fallthru edge to C, no need to move anything. */
796 if (e->flags & EDGE_FALLTHRU)
798 int b_index = b->index, c_index = c->index;
800 /* Protect the loop latches. */
801 if (current_loops && c->loop_father->latch == c)
802 return NULL;
804 merge_blocks (b, c);
805 update_forwarder_flag (b);
807 if (dump_file)
808 fprintf (dump_file, "Merged %d and %d without moving.\n",
809 b_index, c_index);
811 return b->prev_bb == ENTRY_BLOCK_PTR_FOR_FN (cfun) ? b : b->prev_bb;
814 /* Otherwise we will need to move code around. Do that only if expensive
815 transformations are allowed. */
816 else if (mode & CLEANUP_EXPENSIVE)
818 edge tmp_edge, b_fallthru_edge;
819 bool c_has_outgoing_fallthru;
820 bool b_has_incoming_fallthru;
822 /* Avoid overactive code motion, as the forwarder blocks should be
823 eliminated by edge redirection instead. One exception might have
824 been if B is a forwarder block and C has no fallthru edge, but
825 that should be cleaned up by bb-reorder instead. */
826 if (FORWARDER_BLOCK_P (b) || FORWARDER_BLOCK_P (c))
827 return NULL;
829 /* We must make sure to not munge nesting of lexical blocks,
830 and loop notes. This is done by squeezing out all the notes
831 and leaving them there to lie. Not ideal, but functional. */
833 tmp_edge = find_fallthru_edge (c->succs);
834 c_has_outgoing_fallthru = (tmp_edge != NULL);
836 tmp_edge = find_fallthru_edge (b->preds);
837 b_has_incoming_fallthru = (tmp_edge != NULL);
838 b_fallthru_edge = tmp_edge;
839 next = b->prev_bb;
840 if (next == c)
841 next = next->prev_bb;
843 /* Otherwise, we're going to try to move C after B. If C does
844 not have an outgoing fallthru, then it can be moved
845 immediately after B without introducing or modifying jumps. */
846 if (! c_has_outgoing_fallthru)
848 merge_blocks_move_successor_nojumps (b, c);
849 return next == ENTRY_BLOCK_PTR_FOR_FN (cfun) ? next->next_bb : next;
852 /* If B does not have an incoming fallthru, then it can be moved
853 immediately before C without introducing or modifying jumps.
854 C cannot be the first block, so we do not have to worry about
855 accessing a non-existent block. */
857 if (b_has_incoming_fallthru)
859 basic_block bb;
861 if (b_fallthru_edge->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
862 return NULL;
863 bb = force_nonfallthru (b_fallthru_edge);
864 if (bb)
865 notice_new_block (bb);
868 merge_blocks_move_predecessor_nojumps (b, c);
869 return next == ENTRY_BLOCK_PTR_FOR_FN (cfun) ? next->next_bb : next;
872 return NULL;
876 /* Removes the memory attributes of MEM expression
877 if they are not equal. */
879 static void
880 merge_memattrs (rtx x, rtx y)
882 int i;
883 int j;
884 enum rtx_code code;
885 const char *fmt;
887 if (x == y)
888 return;
889 if (x == 0 || y == 0)
890 return;
892 code = GET_CODE (x);
894 if (code != GET_CODE (y))
895 return;
897 if (GET_MODE (x) != GET_MODE (y))
898 return;
900 if (code == MEM && !mem_attrs_eq_p (MEM_ATTRS (x), MEM_ATTRS (y)))
902 if (! MEM_ATTRS (x))
903 MEM_ATTRS (y) = 0;
904 else if (! MEM_ATTRS (y))
905 MEM_ATTRS (x) = 0;
906 else
908 HOST_WIDE_INT mem_size;
910 if (MEM_ALIAS_SET (x) != MEM_ALIAS_SET (y))
912 set_mem_alias_set (x, 0);
913 set_mem_alias_set (y, 0);
916 if (! mem_expr_equal_p (MEM_EXPR (x), MEM_EXPR (y)))
918 set_mem_expr (x, 0);
919 set_mem_expr (y, 0);
920 clear_mem_offset (x);
921 clear_mem_offset (y);
923 else if (MEM_OFFSET_KNOWN_P (x) != MEM_OFFSET_KNOWN_P (y)
924 || (MEM_OFFSET_KNOWN_P (x)
925 && MEM_OFFSET (x) != MEM_OFFSET (y)))
927 clear_mem_offset (x);
928 clear_mem_offset (y);
931 if (MEM_SIZE_KNOWN_P (x) && MEM_SIZE_KNOWN_P (y))
933 mem_size = MAX (MEM_SIZE (x), MEM_SIZE (y));
934 set_mem_size (x, mem_size);
935 set_mem_size (y, mem_size);
937 else
939 clear_mem_size (x);
940 clear_mem_size (y);
943 set_mem_align (x, MIN (MEM_ALIGN (x), MEM_ALIGN (y)));
944 set_mem_align (y, MEM_ALIGN (x));
947 if (code == MEM)
949 if (MEM_READONLY_P (x) != MEM_READONLY_P (y))
951 MEM_READONLY_P (x) = 0;
952 MEM_READONLY_P (y) = 0;
954 if (MEM_NOTRAP_P (x) != MEM_NOTRAP_P (y))
956 MEM_NOTRAP_P (x) = 0;
957 MEM_NOTRAP_P (y) = 0;
959 if (MEM_VOLATILE_P (x) != MEM_VOLATILE_P (y))
961 MEM_VOLATILE_P (x) = 1;
962 MEM_VOLATILE_P (y) = 1;
966 fmt = GET_RTX_FORMAT (code);
967 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
969 switch (fmt[i])
971 case 'E':
972 /* Two vectors must have the same length. */
973 if (XVECLEN (x, i) != XVECLEN (y, i))
974 return;
976 for (j = 0; j < XVECLEN (x, i); j++)
977 merge_memattrs (XVECEXP (x, i, j), XVECEXP (y, i, j));
979 break;
981 case 'e':
982 merge_memattrs (XEXP (x, i), XEXP (y, i));
985 return;
989 /* Checks if patterns P1 and P2 are equivalent, apart from the possibly
990 different single sets S1 and S2. */
992 static bool
993 equal_different_set_p (rtx p1, rtx s1, rtx p2, rtx s2)
995 int i;
996 rtx e1, e2;
998 if (p1 == s1 && p2 == s2)
999 return true;
1001 if (GET_CODE (p1) != PARALLEL || GET_CODE (p2) != PARALLEL)
1002 return false;
1004 if (XVECLEN (p1, 0) != XVECLEN (p2, 0))
1005 return false;
1007 for (i = 0; i < XVECLEN (p1, 0); i++)
1009 e1 = XVECEXP (p1, 0, i);
1010 e2 = XVECEXP (p2, 0, i);
1011 if (e1 == s1 && e2 == s2)
1012 continue;
1013 if (reload_completed
1014 ? rtx_renumbered_equal_p (e1, e2) : rtx_equal_p (e1, e2))
1015 continue;
1017 return false;
1020 return true;
1024 /* NOTE1 is the REG_EQUAL note, if any, attached to an insn
1025 that is a single_set with a SET_SRC of SRC1. Similarly
1026 for NOTE2/SRC2.
1028 So effectively NOTE1/NOTE2 are an alternate form of
1029 SRC1/SRC2 respectively.
1031 Return nonzero if SRC1 or NOTE1 has the same constant
1032 integer value as SRC2 or NOTE2. Else return zero. */
1033 static int
1034 values_equal_p (rtx note1, rtx note2, rtx src1, rtx src2)
1036 if (note1
1037 && note2
1038 && CONST_INT_P (XEXP (note1, 0))
1039 && rtx_equal_p (XEXP (note1, 0), XEXP (note2, 0)))
1040 return 1;
1042 if (!note1
1043 && !note2
1044 && CONST_INT_P (src1)
1045 && CONST_INT_P (src2)
1046 && rtx_equal_p (src1, src2))
1047 return 1;
1049 if (note1
1050 && CONST_INT_P (src2)
1051 && rtx_equal_p (XEXP (note1, 0), src2))
1052 return 1;
1054 if (note2
1055 && CONST_INT_P (src1)
1056 && rtx_equal_p (XEXP (note2, 0), src1))
1057 return 1;
1059 return 0;
1062 /* Examine register notes on I1 and I2 and return:
1063 - dir_forward if I1 can be replaced by I2, or
1064 - dir_backward if I2 can be replaced by I1, or
1065 - dir_both if both are the case. */
1067 static enum replace_direction
1068 can_replace_by (rtx_insn *i1, rtx_insn *i2)
1070 rtx s1, s2, d1, d2, src1, src2, note1, note2;
1071 bool c1, c2;
1073 /* Check for 2 sets. */
1074 s1 = single_set (i1);
1075 s2 = single_set (i2);
1076 if (s1 == NULL_RTX || s2 == NULL_RTX)
1077 return dir_none;
1079 /* Check that the 2 sets set the same dest. */
1080 d1 = SET_DEST (s1);
1081 d2 = SET_DEST (s2);
1082 if (!(reload_completed
1083 ? rtx_renumbered_equal_p (d1, d2) : rtx_equal_p (d1, d2)))
1084 return dir_none;
1086 /* Find identical req_equiv or reg_equal note, which implies that the 2 sets
1087 set dest to the same value. */
1088 note1 = find_reg_equal_equiv_note (i1);
1089 note2 = find_reg_equal_equiv_note (i2);
1091 src1 = SET_SRC (s1);
1092 src2 = SET_SRC (s2);
1094 if (!values_equal_p (note1, note2, src1, src2))
1095 return dir_none;
1097 if (!equal_different_set_p (PATTERN (i1), s1, PATTERN (i2), s2))
1098 return dir_none;
1100 /* Although the 2 sets set dest to the same value, we cannot replace
1101 (set (dest) (const_int))
1103 (set (dest) (reg))
1104 because we don't know if the reg is live and has the same value at the
1105 location of replacement. */
1106 c1 = CONST_INT_P (src1);
1107 c2 = CONST_INT_P (src2);
1108 if (c1 && c2)
1109 return dir_both;
1110 else if (c2)
1111 return dir_forward;
1112 else if (c1)
1113 return dir_backward;
1115 return dir_none;
1118 /* Merges directions A and B. */
1120 static enum replace_direction
1121 merge_dir (enum replace_direction a, enum replace_direction b)
1123 /* Implements the following table:
1124 |bo fw bw no
1125 ---+-----------
1126 bo |bo fw bw no
1127 fw |-- fw no no
1128 bw |-- -- bw no
1129 no |-- -- -- no. */
1131 if (a == b)
1132 return a;
1134 if (a == dir_both)
1135 return b;
1136 if (b == dir_both)
1137 return a;
1139 return dir_none;
1142 /* Examine I1 and I2 and return:
1143 - dir_forward if I1 can be replaced by I2, or
1144 - dir_backward if I2 can be replaced by I1, or
1145 - dir_both if both are the case. */
1147 static enum replace_direction
1148 old_insns_match_p (int mode ATTRIBUTE_UNUSED, rtx_insn *i1, rtx_insn *i2)
1150 rtx p1, p2;
1152 /* Verify that I1 and I2 are equivalent. */
1153 if (GET_CODE (i1) != GET_CODE (i2))
1154 return dir_none;
1156 /* __builtin_unreachable() may lead to empty blocks (ending with
1157 NOTE_INSN_BASIC_BLOCK). They may be crossjumped. */
1158 if (NOTE_INSN_BASIC_BLOCK_P (i1) && NOTE_INSN_BASIC_BLOCK_P (i2))
1159 return dir_both;
1161 /* ??? Do not allow cross-jumping between different stack levels. */
1162 p1 = find_reg_note (i1, REG_ARGS_SIZE, NULL);
1163 p2 = find_reg_note (i2, REG_ARGS_SIZE, NULL);
1164 if (p1 && p2)
1166 p1 = XEXP (p1, 0);
1167 p2 = XEXP (p2, 0);
1168 if (!rtx_equal_p (p1, p2))
1169 return dir_none;
1171 /* ??? Worse, this adjustment had better be constant lest we
1172 have differing incoming stack levels. */
1173 if (!frame_pointer_needed
1174 && find_args_size_adjust (i1) == HOST_WIDE_INT_MIN)
1175 return dir_none;
1177 else if (p1 || p2)
1178 return dir_none;
1180 p1 = PATTERN (i1);
1181 p2 = PATTERN (i2);
1183 if (GET_CODE (p1) != GET_CODE (p2))
1184 return dir_none;
1186 /* If this is a CALL_INSN, compare register usage information.
1187 If we don't check this on stack register machines, the two
1188 CALL_INSNs might be merged leaving reg-stack.c with mismatching
1189 numbers of stack registers in the same basic block.
1190 If we don't check this on machines with delay slots, a delay slot may
1191 be filled that clobbers a parameter expected by the subroutine.
1193 ??? We take the simple route for now and assume that if they're
1194 equal, they were constructed identically.
1196 Also check for identical exception regions. */
1198 if (CALL_P (i1))
1200 /* Ensure the same EH region. */
1201 rtx n1 = find_reg_note (i1, REG_EH_REGION, 0);
1202 rtx n2 = find_reg_note (i2, REG_EH_REGION, 0);
1204 if (!n1 && n2)
1205 return dir_none;
1207 if (n1 && (!n2 || XEXP (n1, 0) != XEXP (n2, 0)))
1208 return dir_none;
1210 if (!rtx_equal_p (CALL_INSN_FUNCTION_USAGE (i1),
1211 CALL_INSN_FUNCTION_USAGE (i2))
1212 || SIBLING_CALL_P (i1) != SIBLING_CALL_P (i2))
1213 return dir_none;
1215 /* For address sanitizer, never crossjump __asan_report_* builtins,
1216 otherwise errors might be reported on incorrect lines. */
1217 if (flag_sanitize & SANITIZE_ADDRESS)
1219 rtx call = get_call_rtx_from (i1);
1220 if (call && GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF)
1222 rtx symbol = XEXP (XEXP (call, 0), 0);
1223 if (SYMBOL_REF_DECL (symbol)
1224 && TREE_CODE (SYMBOL_REF_DECL (symbol)) == FUNCTION_DECL)
1226 if ((DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol))
1227 == BUILT_IN_NORMAL)
1228 && DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol))
1229 >= BUILT_IN_ASAN_REPORT_LOAD1
1230 && DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol))
1231 <= BUILT_IN_ASAN_STOREN)
1232 return dir_none;
1238 #ifdef STACK_REGS
1239 /* If cross_jump_death_matters is not 0, the insn's mode
1240 indicates whether or not the insn contains any stack-like
1241 regs. */
1243 if ((mode & CLEANUP_POST_REGSTACK) && stack_regs_mentioned (i1))
1245 /* If register stack conversion has already been done, then
1246 death notes must also be compared before it is certain that
1247 the two instruction streams match. */
1249 rtx note;
1250 HARD_REG_SET i1_regset, i2_regset;
1252 CLEAR_HARD_REG_SET (i1_regset);
1253 CLEAR_HARD_REG_SET (i2_regset);
1255 for (note = REG_NOTES (i1); note; note = XEXP (note, 1))
1256 if (REG_NOTE_KIND (note) == REG_DEAD && STACK_REG_P (XEXP (note, 0)))
1257 SET_HARD_REG_BIT (i1_regset, REGNO (XEXP (note, 0)));
1259 for (note = REG_NOTES (i2); note; note = XEXP (note, 1))
1260 if (REG_NOTE_KIND (note) == REG_DEAD && STACK_REG_P (XEXP (note, 0)))
1261 SET_HARD_REG_BIT (i2_regset, REGNO (XEXP (note, 0)));
1263 if (!hard_reg_set_equal_p (i1_regset, i2_regset))
1264 return dir_none;
1266 #endif
1268 if (reload_completed
1269 ? rtx_renumbered_equal_p (p1, p2) : rtx_equal_p (p1, p2))
1270 return dir_both;
1272 return can_replace_by (i1, i2);
1275 /* When comparing insns I1 and I2 in flow_find_cross_jump or
1276 flow_find_head_matching_sequence, ensure the notes match. */
1278 static void
1279 merge_notes (rtx_insn *i1, rtx_insn *i2)
1281 /* If the merged insns have different REG_EQUAL notes, then
1282 remove them. */
1283 rtx equiv1 = find_reg_equal_equiv_note (i1);
1284 rtx equiv2 = find_reg_equal_equiv_note (i2);
1286 if (equiv1 && !equiv2)
1287 remove_note (i1, equiv1);
1288 else if (!equiv1 && equiv2)
1289 remove_note (i2, equiv2);
1290 else if (equiv1 && equiv2
1291 && !rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))
1293 remove_note (i1, equiv1);
1294 remove_note (i2, equiv2);
1298 /* Walks from I1 in BB1 backward till the next non-debug insn, and returns the
1299 resulting insn in I1, and the corresponding bb in BB1. At the head of a
1300 bb, if there is a predecessor bb that reaches this bb via fallthru, and
1301 FOLLOW_FALLTHRU, walks further in the predecessor bb and registers this in
1302 DID_FALLTHRU. Otherwise, stops at the head of the bb. */
1304 static void
1305 walk_to_nondebug_insn (rtx_insn **i1, basic_block *bb1, bool follow_fallthru,
1306 bool *did_fallthru)
1308 edge fallthru;
1310 *did_fallthru = false;
1312 /* Ignore notes. */
1313 while (!NONDEBUG_INSN_P (*i1))
1315 if (*i1 != BB_HEAD (*bb1))
1317 *i1 = PREV_INSN (*i1);
1318 continue;
1321 if (!follow_fallthru)
1322 return;
1324 fallthru = find_fallthru_edge ((*bb1)->preds);
1325 if (!fallthru || fallthru->src == ENTRY_BLOCK_PTR_FOR_FN (cfun)
1326 || !single_succ_p (fallthru->src))
1327 return;
1329 *bb1 = fallthru->src;
1330 *i1 = BB_END (*bb1);
1331 *did_fallthru = true;
1335 /* Look through the insns at the end of BB1 and BB2 and find the longest
1336 sequence that are either equivalent, or allow forward or backward
1337 replacement. Store the first insns for that sequence in *F1 and *F2 and
1338 return the sequence length.
1340 DIR_P indicates the allowed replacement direction on function entry, and
1341 the actual replacement direction on function exit. If NULL, only equivalent
1342 sequences are allowed.
1344 To simplify callers of this function, if the blocks match exactly,
1345 store the head of the blocks in *F1 and *F2. */
1348 flow_find_cross_jump (basic_block bb1, basic_block bb2, rtx_insn **f1,
1349 rtx_insn **f2, enum replace_direction *dir_p)
1351 rtx_insn *i1, *i2, *last1, *last2, *afterlast1, *afterlast2;
1352 int ninsns = 0;
1353 enum replace_direction dir, last_dir, afterlast_dir;
1354 bool follow_fallthru, did_fallthru;
1356 if (dir_p)
1357 dir = *dir_p;
1358 else
1359 dir = dir_both;
1360 afterlast_dir = dir;
1361 last_dir = afterlast_dir;
1363 /* Skip simple jumps at the end of the blocks. Complex jumps still
1364 need to be compared for equivalence, which we'll do below. */
1366 i1 = BB_END (bb1);
1367 last1 = afterlast1 = last2 = afterlast2 = NULL;
1368 if (onlyjump_p (i1)
1369 || (returnjump_p (i1) && !side_effects_p (PATTERN (i1))))
1371 last1 = i1;
1372 i1 = PREV_INSN (i1);
1375 i2 = BB_END (bb2);
1376 if (onlyjump_p (i2)
1377 || (returnjump_p (i2) && !side_effects_p (PATTERN (i2))))
1379 last2 = i2;
1380 /* Count everything except for unconditional jump as insn.
1381 Don't count any jumps if dir_p is NULL. */
1382 if (!simplejump_p (i2) && !returnjump_p (i2) && last1 && dir_p)
1383 ninsns++;
1384 i2 = PREV_INSN (i2);
1387 while (true)
1389 /* In the following example, we can replace all jumps to C by jumps to A.
1391 This removes 4 duplicate insns.
1392 [bb A] insn1 [bb C] insn1
1393 insn2 insn2
1394 [bb B] insn3 insn3
1395 insn4 insn4
1396 jump_insn jump_insn
1398 We could also replace all jumps to A by jumps to C, but that leaves B
1399 alive, and removes only 2 duplicate insns. In a subsequent crossjump
1400 step, all jumps to B would be replaced with jumps to the middle of C,
1401 achieving the same result with more effort.
1402 So we allow only the first possibility, which means that we don't allow
1403 fallthru in the block that's being replaced. */
1405 follow_fallthru = dir_p && dir != dir_forward;
1406 walk_to_nondebug_insn (&i1, &bb1, follow_fallthru, &did_fallthru);
1407 if (did_fallthru)
1408 dir = dir_backward;
1410 follow_fallthru = dir_p && dir != dir_backward;
1411 walk_to_nondebug_insn (&i2, &bb2, follow_fallthru, &did_fallthru);
1412 if (did_fallthru)
1413 dir = dir_forward;
1415 if (i1 == BB_HEAD (bb1) || i2 == BB_HEAD (bb2))
1416 break;
1418 dir = merge_dir (dir, old_insns_match_p (0, i1, i2));
1419 if (dir == dir_none || (!dir_p && dir != dir_both))
1420 break;
1422 merge_memattrs (i1, i2);
1424 /* Don't begin a cross-jump with a NOTE insn. */
1425 if (INSN_P (i1))
1427 merge_notes (i1, i2);
1429 afterlast1 = last1, afterlast2 = last2;
1430 last1 = i1, last2 = i2;
1431 afterlast_dir = last_dir;
1432 last_dir = dir;
1433 if (active_insn_p (i1))
1434 ninsns++;
1437 i1 = PREV_INSN (i1);
1438 i2 = PREV_INSN (i2);
1441 /* Don't allow the insn after a compare to be shared by
1442 cross-jumping unless the compare is also shared. */
1443 if (HAVE_cc0 && ninsns && reg_mentioned_p (cc0_rtx, last1)
1444 && ! sets_cc0_p (last1))
1445 last1 = afterlast1, last2 = afterlast2, last_dir = afterlast_dir, ninsns--;
1447 /* Include preceding notes and labels in the cross-jump. One,
1448 this may bring us to the head of the blocks as requested above.
1449 Two, it keeps line number notes as matched as may be. */
1450 if (ninsns)
1452 bb1 = BLOCK_FOR_INSN (last1);
1453 while (last1 != BB_HEAD (bb1) && !NONDEBUG_INSN_P (PREV_INSN (last1)))
1454 last1 = PREV_INSN (last1);
1456 if (last1 != BB_HEAD (bb1) && LABEL_P (PREV_INSN (last1)))
1457 last1 = PREV_INSN (last1);
1459 bb2 = BLOCK_FOR_INSN (last2);
1460 while (last2 != BB_HEAD (bb2) && !NONDEBUG_INSN_P (PREV_INSN (last2)))
1461 last2 = PREV_INSN (last2);
1463 if (last2 != BB_HEAD (bb2) && LABEL_P (PREV_INSN (last2)))
1464 last2 = PREV_INSN (last2);
1466 *f1 = last1;
1467 *f2 = last2;
1470 if (dir_p)
1471 *dir_p = last_dir;
1472 return ninsns;
1475 /* Like flow_find_cross_jump, except start looking for a matching sequence from
1476 the head of the two blocks. Do not include jumps at the end.
1477 If STOP_AFTER is nonzero, stop after finding that many matching
1478 instructions. If STOP_AFTER is zero, count all INSN_P insns, if it is
1479 non-zero, only count active insns. */
1482 flow_find_head_matching_sequence (basic_block bb1, basic_block bb2, rtx_insn **f1,
1483 rtx_insn **f2, int stop_after)
1485 rtx_insn *i1, *i2, *last1, *last2, *beforelast1, *beforelast2;
1486 int ninsns = 0;
1487 edge e;
1488 edge_iterator ei;
1489 int nehedges1 = 0, nehedges2 = 0;
1491 FOR_EACH_EDGE (e, ei, bb1->succs)
1492 if (e->flags & EDGE_EH)
1493 nehedges1++;
1494 FOR_EACH_EDGE (e, ei, bb2->succs)
1495 if (e->flags & EDGE_EH)
1496 nehedges2++;
1498 i1 = BB_HEAD (bb1);
1499 i2 = BB_HEAD (bb2);
1500 last1 = beforelast1 = last2 = beforelast2 = NULL;
1502 while (true)
1504 /* Ignore notes, except NOTE_INSN_EPILOGUE_BEG. */
1505 while (!NONDEBUG_INSN_P (i1) && i1 != BB_END (bb1))
1507 if (NOTE_P (i1) && NOTE_KIND (i1) == NOTE_INSN_EPILOGUE_BEG)
1508 break;
1509 i1 = NEXT_INSN (i1);
1512 while (!NONDEBUG_INSN_P (i2) && i2 != BB_END (bb2))
1514 if (NOTE_P (i2) && NOTE_KIND (i2) == NOTE_INSN_EPILOGUE_BEG)
1515 break;
1516 i2 = NEXT_INSN (i2);
1519 if ((i1 == BB_END (bb1) && !NONDEBUG_INSN_P (i1))
1520 || (i2 == BB_END (bb2) && !NONDEBUG_INSN_P (i2)))
1521 break;
1523 if (NOTE_P (i1) || NOTE_P (i2)
1524 || JUMP_P (i1) || JUMP_P (i2))
1525 break;
1527 /* A sanity check to make sure we're not merging insns with different
1528 effects on EH. If only one of them ends a basic block, it shouldn't
1529 have an EH edge; if both end a basic block, there should be the same
1530 number of EH edges. */
1531 if ((i1 == BB_END (bb1) && i2 != BB_END (bb2)
1532 && nehedges1 > 0)
1533 || (i2 == BB_END (bb2) && i1 != BB_END (bb1)
1534 && nehedges2 > 0)
1535 || (i1 == BB_END (bb1) && i2 == BB_END (bb2)
1536 && nehedges1 != nehedges2))
1537 break;
1539 if (old_insns_match_p (0, i1, i2) != dir_both)
1540 break;
1542 merge_memattrs (i1, i2);
1544 /* Don't begin a cross-jump with a NOTE insn. */
1545 if (INSN_P (i1))
1547 merge_notes (i1, i2);
1549 beforelast1 = last1, beforelast2 = last2;
1550 last1 = i1, last2 = i2;
1551 if (!stop_after || active_insn_p (i1))
1552 ninsns++;
1555 if (i1 == BB_END (bb1) || i2 == BB_END (bb2)
1556 || (stop_after > 0 && ninsns == stop_after))
1557 break;
1559 i1 = NEXT_INSN (i1);
1560 i2 = NEXT_INSN (i2);
1563 /* Don't allow a compare to be shared by cross-jumping unless the insn
1564 after the compare is also shared. */
1565 if (HAVE_cc0 && ninsns && reg_mentioned_p (cc0_rtx, last1)
1566 && sets_cc0_p (last1))
1567 last1 = beforelast1, last2 = beforelast2, ninsns--;
1569 if (ninsns)
1571 *f1 = last1;
1572 *f2 = last2;
1575 return ninsns;
1578 /* Return true iff outgoing edges of BB1 and BB2 match, together with
1579 the branch instruction. This means that if we commonize the control
1580 flow before end of the basic block, the semantic remains unchanged.
1582 We may assume that there exists one edge with a common destination. */
1584 static bool
1585 outgoing_edges_match (int mode, basic_block bb1, basic_block bb2)
1587 int nehedges1 = 0, nehedges2 = 0;
1588 edge fallthru1 = 0, fallthru2 = 0;
1589 edge e1, e2;
1590 edge_iterator ei;
1592 /* If we performed shrink-wrapping, edges to the exit block can
1593 only be distinguished for JUMP_INSNs. The two paths may differ in
1594 whether they went through the prologue. Sibcalls are fine, we know
1595 that we either didn't need or inserted an epilogue before them. */
1596 if (crtl->shrink_wrapped
1597 && single_succ_p (bb1)
1598 && single_succ (bb1) == EXIT_BLOCK_PTR_FOR_FN (cfun)
1599 && !JUMP_P (BB_END (bb1))
1600 && !(CALL_P (BB_END (bb1)) && SIBLING_CALL_P (BB_END (bb1))))
1601 return false;
1603 /* If BB1 has only one successor, we may be looking at either an
1604 unconditional jump, or a fake edge to exit. */
1605 if (single_succ_p (bb1)
1606 && (single_succ_edge (bb1)->flags & (EDGE_COMPLEX | EDGE_FAKE)) == 0
1607 && (!JUMP_P (BB_END (bb1)) || simplejump_p (BB_END (bb1))))
1608 return (single_succ_p (bb2)
1609 && (single_succ_edge (bb2)->flags
1610 & (EDGE_COMPLEX | EDGE_FAKE)) == 0
1611 && (!JUMP_P (BB_END (bb2)) || simplejump_p (BB_END (bb2))));
1613 /* Match conditional jumps - this may get tricky when fallthru and branch
1614 edges are crossed. */
1615 if (EDGE_COUNT (bb1->succs) == 2
1616 && any_condjump_p (BB_END (bb1))
1617 && onlyjump_p (BB_END (bb1)))
1619 edge b1, f1, b2, f2;
1620 bool reverse, match;
1621 rtx set1, set2, cond1, cond2;
1622 enum rtx_code code1, code2;
1624 if (EDGE_COUNT (bb2->succs) != 2
1625 || !any_condjump_p (BB_END (bb2))
1626 || !onlyjump_p (BB_END (bb2)))
1627 return false;
1629 b1 = BRANCH_EDGE (bb1);
1630 b2 = BRANCH_EDGE (bb2);
1631 f1 = FALLTHRU_EDGE (bb1);
1632 f2 = FALLTHRU_EDGE (bb2);
1634 /* Get around possible forwarders on fallthru edges. Other cases
1635 should be optimized out already. */
1636 if (FORWARDER_BLOCK_P (f1->dest))
1637 f1 = single_succ_edge (f1->dest);
1639 if (FORWARDER_BLOCK_P (f2->dest))
1640 f2 = single_succ_edge (f2->dest);
1642 /* To simplify use of this function, return false if there are
1643 unneeded forwarder blocks. These will get eliminated later
1644 during cleanup_cfg. */
1645 if (FORWARDER_BLOCK_P (f1->dest)
1646 || FORWARDER_BLOCK_P (f2->dest)
1647 || FORWARDER_BLOCK_P (b1->dest)
1648 || FORWARDER_BLOCK_P (b2->dest))
1649 return false;
1651 if (f1->dest == f2->dest && b1->dest == b2->dest)
1652 reverse = false;
1653 else if (f1->dest == b2->dest && b1->dest == f2->dest)
1654 reverse = true;
1655 else
1656 return false;
1658 set1 = pc_set (BB_END (bb1));
1659 set2 = pc_set (BB_END (bb2));
1660 if ((XEXP (SET_SRC (set1), 1) == pc_rtx)
1661 != (XEXP (SET_SRC (set2), 1) == pc_rtx))
1662 reverse = !reverse;
1664 cond1 = XEXP (SET_SRC (set1), 0);
1665 cond2 = XEXP (SET_SRC (set2), 0);
1666 code1 = GET_CODE (cond1);
1667 if (reverse)
1668 code2 = reversed_comparison_code (cond2, BB_END (bb2));
1669 else
1670 code2 = GET_CODE (cond2);
1672 if (code2 == UNKNOWN)
1673 return false;
1675 /* Verify codes and operands match. */
1676 match = ((code1 == code2
1677 && rtx_renumbered_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
1678 && rtx_renumbered_equal_p (XEXP (cond1, 1), XEXP (cond2, 1)))
1679 || (code1 == swap_condition (code2)
1680 && rtx_renumbered_equal_p (XEXP (cond1, 1),
1681 XEXP (cond2, 0))
1682 && rtx_renumbered_equal_p (XEXP (cond1, 0),
1683 XEXP (cond2, 1))));
1685 /* If we return true, we will join the blocks. Which means that
1686 we will only have one branch prediction bit to work with. Thus
1687 we require the existing branches to have probabilities that are
1688 roughly similar. */
1689 if (match
1690 && optimize_bb_for_speed_p (bb1)
1691 && optimize_bb_for_speed_p (bb2))
1693 int prob2;
1695 if (b1->dest == b2->dest)
1696 prob2 = b2->probability;
1697 else
1698 /* Do not use f2 probability as f2 may be forwarded. */
1699 prob2 = REG_BR_PROB_BASE - b2->probability;
1701 /* Fail if the difference in probabilities is greater than 50%.
1702 This rules out two well-predicted branches with opposite
1703 outcomes. */
1704 if (abs (b1->probability - prob2) > REG_BR_PROB_BASE / 2)
1706 if (dump_file)
1707 fprintf (dump_file,
1708 "Outcomes of branch in bb %i and %i differ too much (%i %i)\n",
1709 bb1->index, bb2->index, b1->probability, prob2);
1711 return false;
1715 if (dump_file && match)
1716 fprintf (dump_file, "Conditionals in bb %i and %i match.\n",
1717 bb1->index, bb2->index);
1719 return match;
1722 /* Generic case - we are seeing a computed jump, table jump or trapping
1723 instruction. */
1725 /* Check whether there are tablejumps in the end of BB1 and BB2.
1726 Return true if they are identical. */
1728 rtx label1, label2;
1729 rtx_jump_table_data *table1, *table2;
1731 if (tablejump_p (BB_END (bb1), &label1, &table1)
1732 && tablejump_p (BB_END (bb2), &label2, &table2)
1733 && GET_CODE (PATTERN (table1)) == GET_CODE (PATTERN (table2)))
1735 /* The labels should never be the same rtx. If they really are same
1736 the jump tables are same too. So disable crossjumping of blocks BB1
1737 and BB2 because when deleting the common insns in the end of BB1
1738 by delete_basic_block () the jump table would be deleted too. */
1739 /* If LABEL2 is referenced in BB1->END do not do anything
1740 because we would loose information when replacing
1741 LABEL1 by LABEL2 and then LABEL2 by LABEL1 in BB1->END. */
1742 if (label1 != label2 && !rtx_referenced_p (label2, BB_END (bb1)))
1744 /* Set IDENTICAL to true when the tables are identical. */
1745 bool identical = false;
1746 rtx p1, p2;
1748 p1 = PATTERN (table1);
1749 p2 = PATTERN (table2);
1750 if (GET_CODE (p1) == ADDR_VEC && rtx_equal_p (p1, p2))
1752 identical = true;
1754 else if (GET_CODE (p1) == ADDR_DIFF_VEC
1755 && (XVECLEN (p1, 1) == XVECLEN (p2, 1))
1756 && rtx_equal_p (XEXP (p1, 2), XEXP (p2, 2))
1757 && rtx_equal_p (XEXP (p1, 3), XEXP (p2, 3)))
1759 int i;
1761 identical = true;
1762 for (i = XVECLEN (p1, 1) - 1; i >= 0 && identical; i--)
1763 if (!rtx_equal_p (XVECEXP (p1, 1, i), XVECEXP (p2, 1, i)))
1764 identical = false;
1767 if (identical)
1769 bool match;
1771 /* Temporarily replace references to LABEL1 with LABEL2
1772 in BB1->END so that we could compare the instructions. */
1773 replace_label_in_insn (BB_END (bb1), label1, label2, false);
1775 match = (old_insns_match_p (mode, BB_END (bb1), BB_END (bb2))
1776 == dir_both);
1777 if (dump_file && match)
1778 fprintf (dump_file,
1779 "Tablejumps in bb %i and %i match.\n",
1780 bb1->index, bb2->index);
1782 /* Set the original label in BB1->END because when deleting
1783 a block whose end is a tablejump, the tablejump referenced
1784 from the instruction is deleted too. */
1785 replace_label_in_insn (BB_END (bb1), label2, label1, false);
1787 return match;
1790 return false;
1794 /* Find the last non-debug non-note instruction in each bb, except
1795 stop when we see the NOTE_INSN_BASIC_BLOCK, as old_insns_match_p
1796 handles that case specially. old_insns_match_p does not handle
1797 other types of instruction notes. */
1798 rtx_insn *last1 = BB_END (bb1);
1799 rtx_insn *last2 = BB_END (bb2);
1800 while (!NOTE_INSN_BASIC_BLOCK_P (last1) &&
1801 (DEBUG_INSN_P (last1) || NOTE_P (last1)))
1802 last1 = PREV_INSN (last1);
1803 while (!NOTE_INSN_BASIC_BLOCK_P (last2) &&
1804 (DEBUG_INSN_P (last2) || NOTE_P (last2)))
1805 last2 = PREV_INSN (last2);
1806 gcc_assert (last1 && last2);
1808 /* First ensure that the instructions match. There may be many outgoing
1809 edges so this test is generally cheaper. */
1810 if (old_insns_match_p (mode, last1, last2) != dir_both)
1811 return false;
1813 /* Search the outgoing edges, ensure that the counts do match, find possible
1814 fallthru and exception handling edges since these needs more
1815 validation. */
1816 if (EDGE_COUNT (bb1->succs) != EDGE_COUNT (bb2->succs))
1817 return false;
1819 bool nonfakeedges = false;
1820 FOR_EACH_EDGE (e1, ei, bb1->succs)
1822 e2 = EDGE_SUCC (bb2, ei.index);
1824 if ((e1->flags & EDGE_FAKE) == 0)
1825 nonfakeedges = true;
1827 if (e1->flags & EDGE_EH)
1828 nehedges1++;
1830 if (e2->flags & EDGE_EH)
1831 nehedges2++;
1833 if (e1->flags & EDGE_FALLTHRU)
1834 fallthru1 = e1;
1835 if (e2->flags & EDGE_FALLTHRU)
1836 fallthru2 = e2;
1839 /* If number of edges of various types does not match, fail. */
1840 if (nehedges1 != nehedges2
1841 || (fallthru1 != 0) != (fallthru2 != 0))
1842 return false;
1844 /* If !ACCUMULATE_OUTGOING_ARGS, bb1 (and bb2) have no successors
1845 and the last real insn doesn't have REG_ARGS_SIZE note, don't
1846 attempt to optimize, as the two basic blocks might have different
1847 REG_ARGS_SIZE depths. For noreturn calls and unconditional
1848 traps there should be REG_ARG_SIZE notes, they could be missing
1849 for __builtin_unreachable () uses though. */
1850 if (!nonfakeedges
1851 && !ACCUMULATE_OUTGOING_ARGS
1852 && (!INSN_P (last1)
1853 || !find_reg_note (last1, REG_ARGS_SIZE, NULL)))
1854 return false;
1856 /* fallthru edges must be forwarded to the same destination. */
1857 if (fallthru1)
1859 basic_block d1 = (forwarder_block_p (fallthru1->dest)
1860 ? single_succ (fallthru1->dest): fallthru1->dest);
1861 basic_block d2 = (forwarder_block_p (fallthru2->dest)
1862 ? single_succ (fallthru2->dest): fallthru2->dest);
1864 if (d1 != d2)
1865 return false;
1868 /* Ensure the same EH region. */
1870 rtx n1 = find_reg_note (BB_END (bb1), REG_EH_REGION, 0);
1871 rtx n2 = find_reg_note (BB_END (bb2), REG_EH_REGION, 0);
1873 if (!n1 && n2)
1874 return false;
1876 if (n1 && (!n2 || XEXP (n1, 0) != XEXP (n2, 0)))
1877 return false;
1880 /* The same checks as in try_crossjump_to_edge. It is required for RTL
1881 version of sequence abstraction. */
1882 FOR_EACH_EDGE (e1, ei, bb2->succs)
1884 edge e2;
1885 edge_iterator ei;
1886 basic_block d1 = e1->dest;
1888 if (FORWARDER_BLOCK_P (d1))
1889 d1 = EDGE_SUCC (d1, 0)->dest;
1891 FOR_EACH_EDGE (e2, ei, bb1->succs)
1893 basic_block d2 = e2->dest;
1894 if (FORWARDER_BLOCK_P (d2))
1895 d2 = EDGE_SUCC (d2, 0)->dest;
1896 if (d1 == d2)
1897 break;
1900 if (!e2)
1901 return false;
1904 return true;
1907 /* Returns true if BB basic block has a preserve label. */
1909 static bool
1910 block_has_preserve_label (basic_block bb)
1912 return (bb
1913 && block_label (bb)
1914 && LABEL_PRESERVE_P (block_label (bb)));
1917 /* E1 and E2 are edges with the same destination block. Search their
1918 predecessors for common code. If found, redirect control flow from
1919 (maybe the middle of) E1->SRC to (maybe the middle of) E2->SRC (dir_forward),
1920 or the other way around (dir_backward). DIR specifies the allowed
1921 replacement direction. */
1923 static bool
1924 try_crossjump_to_edge (int mode, edge e1, edge e2,
1925 enum replace_direction dir)
1927 int nmatch;
1928 basic_block src1 = e1->src, src2 = e2->src;
1929 basic_block redirect_to, redirect_from, to_remove;
1930 basic_block osrc1, osrc2, redirect_edges_to, tmp;
1931 rtx_insn *newpos1, *newpos2;
1932 edge s;
1933 edge_iterator ei;
1935 newpos1 = newpos2 = NULL;
1937 /* If we have partitioned hot/cold basic blocks, it is a bad idea
1938 to try this optimization.
1940 Basic block partitioning may result in some jumps that appear to
1941 be optimizable (or blocks that appear to be mergeable), but which really
1942 must be left untouched (they are required to make it safely across
1943 partition boundaries). See the comments at the top of
1944 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
1946 if (crtl->has_bb_partition && reload_completed)
1947 return false;
1949 /* Search backward through forwarder blocks. We don't need to worry
1950 about multiple entry or chained forwarders, as they will be optimized
1951 away. We do this to look past the unconditional jump following a
1952 conditional jump that is required due to the current CFG shape. */
1953 if (single_pred_p (src1)
1954 && FORWARDER_BLOCK_P (src1))
1955 e1 = single_pred_edge (src1), src1 = e1->src;
1957 if (single_pred_p (src2)
1958 && FORWARDER_BLOCK_P (src2))
1959 e2 = single_pred_edge (src2), src2 = e2->src;
1961 /* Nothing to do if we reach ENTRY, or a common source block. */
1962 if (src1 == ENTRY_BLOCK_PTR_FOR_FN (cfun) || src2
1963 == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1964 return false;
1965 if (src1 == src2)
1966 return false;
1968 /* Seeing more than 1 forwarder blocks would confuse us later... */
1969 if (FORWARDER_BLOCK_P (e1->dest)
1970 && FORWARDER_BLOCK_P (single_succ (e1->dest)))
1971 return false;
1973 if (FORWARDER_BLOCK_P (e2->dest)
1974 && FORWARDER_BLOCK_P (single_succ (e2->dest)))
1975 return false;
1977 /* Likewise with dead code (possibly newly created by the other optimizations
1978 of cfg_cleanup). */
1979 if (EDGE_COUNT (src1->preds) == 0 || EDGE_COUNT (src2->preds) == 0)
1980 return false;
1982 /* Look for the common insn sequence, part the first ... */
1983 if (!outgoing_edges_match (mode, src1, src2))
1984 return false;
1986 /* ... and part the second. */
1987 nmatch = flow_find_cross_jump (src1, src2, &newpos1, &newpos2, &dir);
1989 osrc1 = src1;
1990 osrc2 = src2;
1991 if (newpos1 != NULL_RTX)
1992 src1 = BLOCK_FOR_INSN (newpos1);
1993 if (newpos2 != NULL_RTX)
1994 src2 = BLOCK_FOR_INSN (newpos2);
1996 if (dir == dir_backward)
1998 #define SWAP(T, X, Y) do { T tmp = (X); (X) = (Y); (Y) = tmp; } while (0)
1999 SWAP (basic_block, osrc1, osrc2);
2000 SWAP (basic_block, src1, src2);
2001 SWAP (edge, e1, e2);
2002 SWAP (rtx_insn *, newpos1, newpos2);
2003 #undef SWAP
2006 /* Don't proceed with the crossjump unless we found a sufficient number
2007 of matching instructions or the 'from' block was totally matched
2008 (such that its predecessors will hopefully be redirected and the
2009 block removed). */
2010 if ((nmatch < PARAM_VALUE (PARAM_MIN_CROSSJUMP_INSNS))
2011 && (newpos1 != BB_HEAD (src1)))
2012 return false;
2014 /* Avoid deleting preserve label when redirecting ABNORMAL edges. */
2015 if (block_has_preserve_label (e1->dest)
2016 && (e1->flags & EDGE_ABNORMAL))
2017 return false;
2019 /* Here we know that the insns in the end of SRC1 which are common with SRC2
2020 will be deleted.
2021 If we have tablejumps in the end of SRC1 and SRC2
2022 they have been already compared for equivalence in outgoing_edges_match ()
2023 so replace the references to TABLE1 by references to TABLE2. */
2025 rtx label1, label2;
2026 rtx_jump_table_data *table1, *table2;
2028 if (tablejump_p (BB_END (osrc1), &label1, &table1)
2029 && tablejump_p (BB_END (osrc2), &label2, &table2)
2030 && label1 != label2)
2032 rtx_insn *insn;
2034 /* Replace references to LABEL1 with LABEL2. */
2035 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2037 /* Do not replace the label in SRC1->END because when deleting
2038 a block whose end is a tablejump, the tablejump referenced
2039 from the instruction is deleted too. */
2040 if (insn != BB_END (osrc1))
2041 replace_label_in_insn (insn, label1, label2, true);
2046 /* Avoid splitting if possible. We must always split when SRC2 has
2047 EH predecessor edges, or we may end up with basic blocks with both
2048 normal and EH predecessor edges. */
2049 if (newpos2 == BB_HEAD (src2)
2050 && !(EDGE_PRED (src2, 0)->flags & EDGE_EH))
2051 redirect_to = src2;
2052 else
2054 if (newpos2 == BB_HEAD (src2))
2056 /* Skip possible basic block header. */
2057 if (LABEL_P (newpos2))
2058 newpos2 = NEXT_INSN (newpos2);
2059 while (DEBUG_INSN_P (newpos2))
2060 newpos2 = NEXT_INSN (newpos2);
2061 if (NOTE_P (newpos2))
2062 newpos2 = NEXT_INSN (newpos2);
2063 while (DEBUG_INSN_P (newpos2))
2064 newpos2 = NEXT_INSN (newpos2);
2067 if (dump_file)
2068 fprintf (dump_file, "Splitting bb %i before %i insns\n",
2069 src2->index, nmatch);
2070 redirect_to = split_block (src2, PREV_INSN (newpos2))->dest;
2073 if (dump_file)
2074 fprintf (dump_file,
2075 "Cross jumping from bb %i to bb %i; %i common insns\n",
2076 src1->index, src2->index, nmatch);
2078 /* We may have some registers visible through the block. */
2079 df_set_bb_dirty (redirect_to);
2081 if (osrc2 == src2)
2082 redirect_edges_to = redirect_to;
2083 else
2084 redirect_edges_to = osrc2;
2086 /* Recompute the frequencies and counts of outgoing edges. */
2087 FOR_EACH_EDGE (s, ei, redirect_edges_to->succs)
2089 edge s2;
2090 edge_iterator ei;
2091 basic_block d = s->dest;
2093 if (FORWARDER_BLOCK_P (d))
2094 d = single_succ (d);
2096 FOR_EACH_EDGE (s2, ei, src1->succs)
2098 basic_block d2 = s2->dest;
2099 if (FORWARDER_BLOCK_P (d2))
2100 d2 = single_succ (d2);
2101 if (d == d2)
2102 break;
2105 s->count += s2->count;
2107 /* Take care to update possible forwarder blocks. We verified
2108 that there is no more than one in the chain, so we can't run
2109 into infinite loop. */
2110 if (FORWARDER_BLOCK_P (s->dest))
2112 single_succ_edge (s->dest)->count += s2->count;
2113 s->dest->count += s2->count;
2114 s->dest->frequency += EDGE_FREQUENCY (s);
2117 if (FORWARDER_BLOCK_P (s2->dest))
2119 single_succ_edge (s2->dest)->count -= s2->count;
2120 if (single_succ_edge (s2->dest)->count < 0)
2121 single_succ_edge (s2->dest)->count = 0;
2122 s2->dest->count -= s2->count;
2123 s2->dest->frequency -= EDGE_FREQUENCY (s);
2124 if (s2->dest->frequency < 0)
2125 s2->dest->frequency = 0;
2126 if (s2->dest->count < 0)
2127 s2->dest->count = 0;
2130 if (!redirect_edges_to->frequency && !src1->frequency)
2131 s->probability = (s->probability + s2->probability) / 2;
2132 else
2133 s->probability
2134 = ((s->probability * redirect_edges_to->frequency +
2135 s2->probability * src1->frequency)
2136 / (redirect_edges_to->frequency + src1->frequency));
2139 /* Adjust count and frequency for the block. An earlier jump
2140 threading pass may have left the profile in an inconsistent
2141 state (see update_bb_profile_for_threading) so we must be
2142 prepared for overflows. */
2143 tmp = redirect_to;
2146 tmp->count += src1->count;
2147 tmp->frequency += src1->frequency;
2148 if (tmp->frequency > BB_FREQ_MAX)
2149 tmp->frequency = BB_FREQ_MAX;
2150 if (tmp == redirect_edges_to)
2151 break;
2152 tmp = find_fallthru_edge (tmp->succs)->dest;
2154 while (true);
2155 update_br_prob_note (redirect_edges_to);
2157 /* Edit SRC1 to go to REDIRECT_TO at NEWPOS1. */
2159 /* Skip possible basic block header. */
2160 if (LABEL_P (newpos1))
2161 newpos1 = NEXT_INSN (newpos1);
2163 while (DEBUG_INSN_P (newpos1))
2164 newpos1 = NEXT_INSN (newpos1);
2166 if (NOTE_INSN_BASIC_BLOCK_P (newpos1))
2167 newpos1 = NEXT_INSN (newpos1);
2169 while (DEBUG_INSN_P (newpos1))
2170 newpos1 = NEXT_INSN (newpos1);
2172 redirect_from = split_block (src1, PREV_INSN (newpos1))->src;
2173 to_remove = single_succ (redirect_from);
2175 redirect_edge_and_branch_force (single_succ_edge (redirect_from), redirect_to);
2176 delete_basic_block (to_remove);
2178 update_forwarder_flag (redirect_from);
2179 if (redirect_to != src2)
2180 update_forwarder_flag (src2);
2182 return true;
2185 /* Search the predecessors of BB for common insn sequences. When found,
2186 share code between them by redirecting control flow. Return true if
2187 any changes made. */
2189 static bool
2190 try_crossjump_bb (int mode, basic_block bb)
2192 edge e, e2, fallthru;
2193 bool changed;
2194 unsigned max, ix, ix2;
2196 /* Nothing to do if there is not at least two incoming edges. */
2197 if (EDGE_COUNT (bb->preds) < 2)
2198 return false;
2200 /* Don't crossjump if this block ends in a computed jump,
2201 unless we are optimizing for size. */
2202 if (optimize_bb_for_size_p (bb)
2203 && bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
2204 && computed_jump_p (BB_END (bb)))
2205 return false;
2207 /* If we are partitioning hot/cold basic blocks, we don't want to
2208 mess up unconditional or indirect jumps that cross between hot
2209 and cold sections.
2211 Basic block partitioning may result in some jumps that appear to
2212 be optimizable (or blocks that appear to be mergeable), but which really
2213 must be left untouched (they are required to make it safely across
2214 partition boundaries). See the comments at the top of
2215 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
2217 if (BB_PARTITION (EDGE_PRED (bb, 0)->src) !=
2218 BB_PARTITION (EDGE_PRED (bb, 1)->src)
2219 || (EDGE_PRED (bb, 0)->flags & EDGE_CROSSING))
2220 return false;
2222 /* It is always cheapest to redirect a block that ends in a branch to
2223 a block that falls through into BB, as that adds no branches to the
2224 program. We'll try that combination first. */
2225 fallthru = NULL;
2226 max = PARAM_VALUE (PARAM_MAX_CROSSJUMP_EDGES);
2228 if (EDGE_COUNT (bb->preds) > max)
2229 return false;
2231 fallthru = find_fallthru_edge (bb->preds);
2233 changed = false;
2234 for (ix = 0; ix < EDGE_COUNT (bb->preds);)
2236 e = EDGE_PRED (bb, ix);
2237 ix++;
2239 /* As noted above, first try with the fallthru predecessor (or, a
2240 fallthru predecessor if we are in cfglayout mode). */
2241 if (fallthru)
2243 /* Don't combine the fallthru edge into anything else.
2244 If there is a match, we'll do it the other way around. */
2245 if (e == fallthru)
2246 continue;
2247 /* If nothing changed since the last attempt, there is nothing
2248 we can do. */
2249 if (!first_pass
2250 && !((e->src->flags & BB_MODIFIED)
2251 || (fallthru->src->flags & BB_MODIFIED)))
2252 continue;
2254 if (try_crossjump_to_edge (mode, e, fallthru, dir_forward))
2256 changed = true;
2257 ix = 0;
2258 continue;
2262 /* Non-obvious work limiting check: Recognize that we're going
2263 to call try_crossjump_bb on every basic block. So if we have
2264 two blocks with lots of outgoing edges (a switch) and they
2265 share lots of common destinations, then we would do the
2266 cross-jump check once for each common destination.
2268 Now, if the blocks actually are cross-jump candidates, then
2269 all of their destinations will be shared. Which means that
2270 we only need check them for cross-jump candidacy once. We
2271 can eliminate redundant checks of crossjump(A,B) by arbitrarily
2272 choosing to do the check from the block for which the edge
2273 in question is the first successor of A. */
2274 if (EDGE_SUCC (e->src, 0) != e)
2275 continue;
2277 for (ix2 = 0; ix2 < EDGE_COUNT (bb->preds); ix2++)
2279 e2 = EDGE_PRED (bb, ix2);
2281 if (e2 == e)
2282 continue;
2284 /* We've already checked the fallthru edge above. */
2285 if (e2 == fallthru)
2286 continue;
2288 /* The "first successor" check above only prevents multiple
2289 checks of crossjump(A,B). In order to prevent redundant
2290 checks of crossjump(B,A), require that A be the block
2291 with the lowest index. */
2292 if (e->src->index > e2->src->index)
2293 continue;
2295 /* If nothing changed since the last attempt, there is nothing
2296 we can do. */
2297 if (!first_pass
2298 && !((e->src->flags & BB_MODIFIED)
2299 || (e2->src->flags & BB_MODIFIED)))
2300 continue;
2302 /* Both e and e2 are not fallthru edges, so we can crossjump in either
2303 direction. */
2304 if (try_crossjump_to_edge (mode, e, e2, dir_both))
2306 changed = true;
2307 ix = 0;
2308 break;
2313 if (changed)
2314 crossjumps_occured = true;
2316 return changed;
2319 /* Search the successors of BB for common insn sequences. When found,
2320 share code between them by moving it across the basic block
2321 boundary. Return true if any changes made. */
2323 static bool
2324 try_head_merge_bb (basic_block bb)
2326 basic_block final_dest_bb = NULL;
2327 int max_match = INT_MAX;
2328 edge e0;
2329 rtx_insn **headptr, **currptr, **nextptr;
2330 bool changed, moveall;
2331 unsigned ix;
2332 rtx_insn *e0_last_head;
2333 rtx cond;
2334 rtx_insn *move_before;
2335 unsigned nedges = EDGE_COUNT (bb->succs);
2336 rtx_insn *jump = BB_END (bb);
2337 regset live, live_union;
2339 /* Nothing to do if there is not at least two outgoing edges. */
2340 if (nedges < 2)
2341 return false;
2343 /* Don't crossjump if this block ends in a computed jump,
2344 unless we are optimizing for size. */
2345 if (optimize_bb_for_size_p (bb)
2346 && bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
2347 && computed_jump_p (BB_END (bb)))
2348 return false;
2350 cond = get_condition (jump, &move_before, true, false);
2351 if (cond == NULL_RTX)
2353 if (HAVE_cc0 && reg_mentioned_p (cc0_rtx, jump))
2354 move_before = prev_nonnote_nondebug_insn (jump);
2355 else
2356 move_before = jump;
2359 for (ix = 0; ix < nedges; ix++)
2360 if (EDGE_SUCC (bb, ix)->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
2361 return false;
2363 for (ix = 0; ix < nedges; ix++)
2365 edge e = EDGE_SUCC (bb, ix);
2366 basic_block other_bb = e->dest;
2368 if (df_get_bb_dirty (other_bb))
2370 block_was_dirty = true;
2371 return false;
2374 if (e->flags & EDGE_ABNORMAL)
2375 return false;
2377 /* Normally, all destination blocks must only be reachable from this
2378 block, i.e. they must have one incoming edge.
2380 There is one special case we can handle, that of multiple consecutive
2381 jumps where the first jumps to one of the targets of the second jump.
2382 This happens frequently in switch statements for default labels.
2383 The structure is as follows:
2384 FINAL_DEST_BB
2385 ....
2386 if (cond) jump A;
2387 fall through
2389 jump with targets A, B, C, D...
2391 has two incoming edges, from FINAL_DEST_BB and BB
2393 In this case, we can try to move the insns through BB and into
2394 FINAL_DEST_BB. */
2395 if (EDGE_COUNT (other_bb->preds) != 1)
2397 edge incoming_edge, incoming_bb_other_edge;
2398 edge_iterator ei;
2400 if (final_dest_bb != NULL
2401 || EDGE_COUNT (other_bb->preds) != 2)
2402 return false;
2404 /* We must be able to move the insns across the whole block. */
2405 move_before = BB_HEAD (bb);
2406 while (!NONDEBUG_INSN_P (move_before))
2407 move_before = NEXT_INSN (move_before);
2409 if (EDGE_COUNT (bb->preds) != 1)
2410 return false;
2411 incoming_edge = EDGE_PRED (bb, 0);
2412 final_dest_bb = incoming_edge->src;
2413 if (EDGE_COUNT (final_dest_bb->succs) != 2)
2414 return false;
2415 FOR_EACH_EDGE (incoming_bb_other_edge, ei, final_dest_bb->succs)
2416 if (incoming_bb_other_edge != incoming_edge)
2417 break;
2418 if (incoming_bb_other_edge->dest != other_bb)
2419 return false;
2423 e0 = EDGE_SUCC (bb, 0);
2424 e0_last_head = NULL;
2425 changed = false;
2427 for (ix = 1; ix < nedges; ix++)
2429 edge e = EDGE_SUCC (bb, ix);
2430 rtx_insn *e0_last, *e_last;
2431 int nmatch;
2433 nmatch = flow_find_head_matching_sequence (e0->dest, e->dest,
2434 &e0_last, &e_last, 0);
2435 if (nmatch == 0)
2436 return false;
2438 if (nmatch < max_match)
2440 max_match = nmatch;
2441 e0_last_head = e0_last;
2445 /* If we matched an entire block, we probably have to avoid moving the
2446 last insn. */
2447 if (max_match > 0
2448 && e0_last_head == BB_END (e0->dest)
2449 && (find_reg_note (e0_last_head, REG_EH_REGION, 0)
2450 || control_flow_insn_p (e0_last_head)))
2452 max_match--;
2453 if (max_match == 0)
2454 return false;
2456 e0_last_head = prev_real_insn (e0_last_head);
2457 while (DEBUG_INSN_P (e0_last_head));
2460 if (max_match == 0)
2461 return false;
2463 /* We must find a union of the live registers at each of the end points. */
2464 live = BITMAP_ALLOC (NULL);
2465 live_union = BITMAP_ALLOC (NULL);
2467 currptr = XNEWVEC (rtx_insn *, nedges);
2468 headptr = XNEWVEC (rtx_insn *, nedges);
2469 nextptr = XNEWVEC (rtx_insn *, nedges);
2471 for (ix = 0; ix < nedges; ix++)
2473 int j;
2474 basic_block merge_bb = EDGE_SUCC (bb, ix)->dest;
2475 rtx_insn *head = BB_HEAD (merge_bb);
2477 while (!NONDEBUG_INSN_P (head))
2478 head = NEXT_INSN (head);
2479 headptr[ix] = head;
2480 currptr[ix] = head;
2482 /* Compute the end point and live information */
2483 for (j = 1; j < max_match; j++)
2485 head = NEXT_INSN (head);
2486 while (!NONDEBUG_INSN_P (head));
2487 simulate_backwards_to_point (merge_bb, live, head);
2488 IOR_REG_SET (live_union, live);
2491 /* If we're moving across two blocks, verify the validity of the
2492 first move, then adjust the target and let the loop below deal
2493 with the final move. */
2494 if (final_dest_bb != NULL)
2496 rtx_insn *move_upto;
2498 moveall = can_move_insns_across (currptr[0], e0_last_head, move_before,
2499 jump, e0->dest, live_union,
2500 NULL, &move_upto);
2501 if (!moveall)
2503 if (move_upto == NULL_RTX)
2504 goto out;
2506 while (e0_last_head != move_upto)
2508 df_simulate_one_insn_backwards (e0->dest, e0_last_head,
2509 live_union);
2510 e0_last_head = PREV_INSN (e0_last_head);
2513 if (e0_last_head == NULL_RTX)
2514 goto out;
2516 jump = BB_END (final_dest_bb);
2517 cond = get_condition (jump, &move_before, true, false);
2518 if (cond == NULL_RTX)
2520 if (HAVE_cc0 && reg_mentioned_p (cc0_rtx, jump))
2521 move_before = prev_nonnote_nondebug_insn (jump);
2522 else
2523 move_before = jump;
2529 rtx_insn *move_upto;
2530 moveall = can_move_insns_across (currptr[0], e0_last_head,
2531 move_before, jump, e0->dest, live_union,
2532 NULL, &move_upto);
2533 if (!moveall && move_upto == NULL_RTX)
2535 if (jump == move_before)
2536 break;
2538 /* Try again, using a different insertion point. */
2539 move_before = jump;
2541 /* Don't try moving before a cc0 user, as that may invalidate
2542 the cc0. */
2543 if (HAVE_cc0 && reg_mentioned_p (cc0_rtx, jump))
2544 break;
2546 continue;
2549 if (final_dest_bb && !moveall)
2550 /* We haven't checked whether a partial move would be OK for the first
2551 move, so we have to fail this case. */
2552 break;
2554 changed = true;
2555 for (;;)
2557 if (currptr[0] == move_upto)
2558 break;
2559 for (ix = 0; ix < nedges; ix++)
2561 rtx_insn *curr = currptr[ix];
2563 curr = NEXT_INSN (curr);
2564 while (!NONDEBUG_INSN_P (curr));
2565 currptr[ix] = curr;
2569 /* If we can't currently move all of the identical insns, remember
2570 each insn after the range that we'll merge. */
2571 if (!moveall)
2572 for (ix = 0; ix < nedges; ix++)
2574 rtx_insn *curr = currptr[ix];
2576 curr = NEXT_INSN (curr);
2577 while (!NONDEBUG_INSN_P (curr));
2578 nextptr[ix] = curr;
2581 reorder_insns (headptr[0], currptr[0], PREV_INSN (move_before));
2582 df_set_bb_dirty (EDGE_SUCC (bb, 0)->dest);
2583 if (final_dest_bb != NULL)
2584 df_set_bb_dirty (final_dest_bb);
2585 df_set_bb_dirty (bb);
2586 for (ix = 1; ix < nedges; ix++)
2588 df_set_bb_dirty (EDGE_SUCC (bb, ix)->dest);
2589 delete_insn_chain (headptr[ix], currptr[ix], false);
2591 if (!moveall)
2593 if (jump == move_before)
2594 break;
2596 /* For the unmerged insns, try a different insertion point. */
2597 move_before = jump;
2599 /* Don't try moving before a cc0 user, as that may invalidate
2600 the cc0. */
2601 if (HAVE_cc0 && reg_mentioned_p (cc0_rtx, jump))
2602 break;
2604 for (ix = 0; ix < nedges; ix++)
2605 currptr[ix] = headptr[ix] = nextptr[ix];
2608 while (!moveall);
2610 out:
2611 free (currptr);
2612 free (headptr);
2613 free (nextptr);
2615 crossjumps_occured |= changed;
2617 return changed;
2620 /* Return true if BB contains just bb note, or bb note followed
2621 by only DEBUG_INSNs. */
2623 static bool
2624 trivially_empty_bb_p (basic_block bb)
2626 rtx_insn *insn = BB_END (bb);
2628 while (1)
2630 if (insn == BB_HEAD (bb))
2631 return true;
2632 if (!DEBUG_INSN_P (insn))
2633 return false;
2634 insn = PREV_INSN (insn);
2638 /* Do simple CFG optimizations - basic block merging, simplifying of jump
2639 instructions etc. Return nonzero if changes were made. */
2641 static bool
2642 try_optimize_cfg (int mode)
2644 bool changed_overall = false;
2645 bool changed;
2646 int iterations = 0;
2647 basic_block bb, b, next;
2649 if (mode & (CLEANUP_CROSSJUMP | CLEANUP_THREADING))
2650 clear_bb_flags ();
2652 crossjumps_occured = false;
2654 FOR_EACH_BB_FN (bb, cfun)
2655 update_forwarder_flag (bb);
2657 if (! targetm.cannot_modify_jumps_p ())
2659 first_pass = true;
2660 /* Attempt to merge blocks as made possible by edge removal. If
2661 a block has only one successor, and the successor has only
2662 one predecessor, they may be combined. */
2665 block_was_dirty = false;
2666 changed = false;
2667 iterations++;
2669 if (dump_file)
2670 fprintf (dump_file,
2671 "\n\ntry_optimize_cfg iteration %i\n\n",
2672 iterations);
2674 for (b = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; b
2675 != EXIT_BLOCK_PTR_FOR_FN (cfun);)
2677 basic_block c;
2678 edge s;
2679 bool changed_here = false;
2681 /* Delete trivially dead basic blocks. This is either
2682 blocks with no predecessors, or empty blocks with no
2683 successors. However if the empty block with no
2684 successors is the successor of the ENTRY_BLOCK, it is
2685 kept. This ensures that the ENTRY_BLOCK will have a
2686 successor which is a precondition for many RTL
2687 passes. Empty blocks may result from expanding
2688 __builtin_unreachable (). */
2689 if (EDGE_COUNT (b->preds) == 0
2690 || (EDGE_COUNT (b->succs) == 0
2691 && trivially_empty_bb_p (b)
2692 && single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun))->dest
2693 != b))
2695 c = b->prev_bb;
2696 if (EDGE_COUNT (b->preds) > 0)
2698 edge e;
2699 edge_iterator ei;
2701 if (current_ir_type () == IR_RTL_CFGLAYOUT)
2703 if (BB_FOOTER (b)
2704 && BARRIER_P (BB_FOOTER (b)))
2705 FOR_EACH_EDGE (e, ei, b->preds)
2706 if ((e->flags & EDGE_FALLTHRU)
2707 && BB_FOOTER (e->src) == NULL)
2709 if (BB_FOOTER (b))
2711 BB_FOOTER (e->src) = BB_FOOTER (b);
2712 BB_FOOTER (b) = NULL;
2714 else
2716 start_sequence ();
2717 BB_FOOTER (e->src) = emit_barrier ();
2718 end_sequence ();
2722 else
2724 rtx_insn *last = get_last_bb_insn (b);
2725 if (last && BARRIER_P (last))
2726 FOR_EACH_EDGE (e, ei, b->preds)
2727 if ((e->flags & EDGE_FALLTHRU))
2728 emit_barrier_after (BB_END (e->src));
2731 delete_basic_block (b);
2732 changed = true;
2733 /* Avoid trying to remove the exit block. */
2734 b = (c == ENTRY_BLOCK_PTR_FOR_FN (cfun) ? c->next_bb : c);
2735 continue;
2738 /* Remove code labels no longer used. */
2739 if (single_pred_p (b)
2740 && (single_pred_edge (b)->flags & EDGE_FALLTHRU)
2741 && !(single_pred_edge (b)->flags & EDGE_COMPLEX)
2742 && LABEL_P (BB_HEAD (b))
2743 && !LABEL_PRESERVE_P (BB_HEAD (b))
2744 /* If the previous block ends with a branch to this
2745 block, we can't delete the label. Normally this
2746 is a condjump that is yet to be simplified, but
2747 if CASE_DROPS_THRU, this can be a tablejump with
2748 some element going to the same place as the
2749 default (fallthru). */
2750 && (single_pred (b) == ENTRY_BLOCK_PTR_FOR_FN (cfun)
2751 || !JUMP_P (BB_END (single_pred (b)))
2752 || ! label_is_jump_target_p (BB_HEAD (b),
2753 BB_END (single_pred (b)))))
2755 delete_insn (BB_HEAD (b));
2756 if (dump_file)
2757 fprintf (dump_file, "Deleted label in block %i.\n",
2758 b->index);
2761 /* If we fall through an empty block, we can remove it. */
2762 if (!(mode & (CLEANUP_CFGLAYOUT | CLEANUP_NO_INSN_DEL))
2763 && single_pred_p (b)
2764 && (single_pred_edge (b)->flags & EDGE_FALLTHRU)
2765 && !LABEL_P (BB_HEAD (b))
2766 && FORWARDER_BLOCK_P (b)
2767 /* Note that forwarder_block_p true ensures that
2768 there is a successor for this block. */
2769 && (single_succ_edge (b)->flags & EDGE_FALLTHRU)
2770 && n_basic_blocks_for_fn (cfun) > NUM_FIXED_BLOCKS + 1)
2772 if (dump_file)
2773 fprintf (dump_file,
2774 "Deleting fallthru block %i.\n",
2775 b->index);
2777 c = ((b->prev_bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
2778 ? b->next_bb : b->prev_bb);
2779 redirect_edge_succ_nodup (single_pred_edge (b),
2780 single_succ (b));
2781 delete_basic_block (b);
2782 changed = true;
2783 b = c;
2784 continue;
2787 /* Merge B with its single successor, if any. */
2788 if (single_succ_p (b)
2789 && (s = single_succ_edge (b))
2790 && !(s->flags & EDGE_COMPLEX)
2791 && (c = s->dest) != EXIT_BLOCK_PTR_FOR_FN (cfun)
2792 && single_pred_p (c)
2793 && b != c)
2795 /* When not in cfg_layout mode use code aware of reordering
2796 INSN. This code possibly creates new basic blocks so it
2797 does not fit merge_blocks interface and is kept here in
2798 hope that it will become useless once more of compiler
2799 is transformed to use cfg_layout mode. */
2801 if ((mode & CLEANUP_CFGLAYOUT)
2802 && can_merge_blocks_p (b, c))
2804 merge_blocks (b, c);
2805 update_forwarder_flag (b);
2806 changed_here = true;
2808 else if (!(mode & CLEANUP_CFGLAYOUT)
2809 /* If the jump insn has side effects,
2810 we can't kill the edge. */
2811 && (!JUMP_P (BB_END (b))
2812 || (reload_completed
2813 ? simplejump_p (BB_END (b))
2814 : (onlyjump_p (BB_END (b))
2815 && !tablejump_p (BB_END (b),
2816 NULL, NULL))))
2817 && (next = merge_blocks_move (s, b, c, mode)))
2819 b = next;
2820 changed_here = true;
2824 /* Simplify branch over branch. */
2825 if ((mode & CLEANUP_EXPENSIVE)
2826 && !(mode & CLEANUP_CFGLAYOUT)
2827 && try_simplify_condjump (b))
2828 changed_here = true;
2830 /* If B has a single outgoing edge, but uses a
2831 non-trivial jump instruction without side-effects, we
2832 can either delete the jump entirely, or replace it
2833 with a simple unconditional jump. */
2834 if (single_succ_p (b)
2835 && single_succ (b) != EXIT_BLOCK_PTR_FOR_FN (cfun)
2836 && onlyjump_p (BB_END (b))
2837 && !CROSSING_JUMP_P (BB_END (b))
2838 && try_redirect_by_replacing_jump (single_succ_edge (b),
2839 single_succ (b),
2840 (mode & CLEANUP_CFGLAYOUT) != 0))
2842 update_forwarder_flag (b);
2843 changed_here = true;
2846 /* Simplify branch to branch. */
2847 if (try_forward_edges (mode, b))
2849 update_forwarder_flag (b);
2850 changed_here = true;
2853 /* Look for shared code between blocks. */
2854 if ((mode & CLEANUP_CROSSJUMP)
2855 && try_crossjump_bb (mode, b))
2856 changed_here = true;
2858 if ((mode & CLEANUP_CROSSJUMP)
2859 /* This can lengthen register lifetimes. Do it only after
2860 reload. */
2861 && reload_completed
2862 && try_head_merge_bb (b))
2863 changed_here = true;
2865 /* Don't get confused by the index shift caused by
2866 deleting blocks. */
2867 if (!changed_here)
2868 b = b->next_bb;
2869 else
2870 changed = true;
2873 if ((mode & CLEANUP_CROSSJUMP)
2874 && try_crossjump_bb (mode, EXIT_BLOCK_PTR_FOR_FN (cfun)))
2875 changed = true;
2877 if (block_was_dirty)
2879 /* This should only be set by head-merging. */
2880 gcc_assert (mode & CLEANUP_CROSSJUMP);
2881 df_analyze ();
2884 if (changed)
2886 /* Edge forwarding in particular can cause hot blocks previously
2887 reached by both hot and cold blocks to become dominated only
2888 by cold blocks. This will cause the verification below to fail,
2889 and lead to now cold code in the hot section. This is not easy
2890 to detect and fix during edge forwarding, and in some cases
2891 is only visible after newly unreachable blocks are deleted,
2892 which will be done in fixup_partitions. */
2893 fixup_partitions ();
2895 #ifdef ENABLE_CHECKING
2896 verify_flow_info ();
2897 #endif
2900 changed_overall |= changed;
2901 first_pass = false;
2903 while (changed);
2906 FOR_ALL_BB_FN (b, cfun)
2907 b->flags &= ~(BB_FORWARDER_BLOCK | BB_NONTHREADABLE_BLOCK);
2909 return changed_overall;
2912 /* Delete all unreachable basic blocks. */
2914 bool
2915 delete_unreachable_blocks (void)
2917 bool changed = false;
2918 basic_block b, prev_bb;
2920 find_unreachable_blocks ();
2922 /* When we're in GIMPLE mode and there may be debug insns, we should
2923 delete blocks in reverse dominator order, so as to get a chance
2924 to substitute all released DEFs into debug stmts. If we don't
2925 have dominators information, walking blocks backward gets us a
2926 better chance of retaining most debug information than
2927 otherwise. */
2928 if (MAY_HAVE_DEBUG_INSNS && current_ir_type () == IR_GIMPLE
2929 && dom_info_available_p (CDI_DOMINATORS))
2931 for (b = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
2932 b != ENTRY_BLOCK_PTR_FOR_FN (cfun); b = prev_bb)
2934 prev_bb = b->prev_bb;
2936 if (!(b->flags & BB_REACHABLE))
2938 /* Speed up the removal of blocks that don't dominate
2939 others. Walking backwards, this should be the common
2940 case. */
2941 if (!first_dom_son (CDI_DOMINATORS, b))
2942 delete_basic_block (b);
2943 else
2945 vec<basic_block> h
2946 = get_all_dominated_blocks (CDI_DOMINATORS, b);
2948 while (h.length ())
2950 b = h.pop ();
2952 prev_bb = b->prev_bb;
2954 gcc_assert (!(b->flags & BB_REACHABLE));
2956 delete_basic_block (b);
2959 h.release ();
2962 changed = true;
2966 else
2968 for (b = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
2969 b != ENTRY_BLOCK_PTR_FOR_FN (cfun); b = prev_bb)
2971 prev_bb = b->prev_bb;
2973 if (!(b->flags & BB_REACHABLE))
2975 delete_basic_block (b);
2976 changed = true;
2981 if (changed)
2982 tidy_fallthru_edges ();
2983 return changed;
2986 /* Delete any jump tables never referenced. We can't delete them at the
2987 time of removing tablejump insn as they are referenced by the preceding
2988 insns computing the destination, so we delay deleting and garbagecollect
2989 them once life information is computed. */
2990 void
2991 delete_dead_jumptables (void)
2993 basic_block bb;
2995 /* A dead jump table does not belong to any basic block. Scan insns
2996 between two adjacent basic blocks. */
2997 FOR_EACH_BB_FN (bb, cfun)
2999 rtx_insn *insn, *next;
3001 for (insn = NEXT_INSN (BB_END (bb));
3002 insn && !NOTE_INSN_BASIC_BLOCK_P (insn);
3003 insn = next)
3005 next = NEXT_INSN (insn);
3006 if (LABEL_P (insn)
3007 && LABEL_NUSES (insn) == LABEL_PRESERVE_P (insn)
3008 && JUMP_TABLE_DATA_P (next))
3010 rtx_insn *label = insn, *jump = next;
3012 if (dump_file)
3013 fprintf (dump_file, "Dead jumptable %i removed\n",
3014 INSN_UID (insn));
3016 next = NEXT_INSN (next);
3017 delete_insn (jump);
3018 delete_insn (label);
3025 /* Tidy the CFG by deleting unreachable code and whatnot. */
3027 bool
3028 cleanup_cfg (int mode)
3030 bool changed = false;
3032 /* Set the cfglayout mode flag here. We could update all the callers
3033 but that is just inconvenient, especially given that we eventually
3034 want to have cfglayout mode as the default. */
3035 if (current_ir_type () == IR_RTL_CFGLAYOUT)
3036 mode |= CLEANUP_CFGLAYOUT;
3038 timevar_push (TV_CLEANUP_CFG);
3039 if (delete_unreachable_blocks ())
3041 changed = true;
3042 /* We've possibly created trivially dead code. Cleanup it right
3043 now to introduce more opportunities for try_optimize_cfg. */
3044 if (!(mode & (CLEANUP_NO_INSN_DEL))
3045 && !reload_completed)
3046 delete_trivially_dead_insns (get_insns (), max_reg_num ());
3049 compact_blocks ();
3051 /* To tail-merge blocks ending in the same noreturn function (e.g.
3052 a call to abort) we have to insert fake edges to exit. Do this
3053 here once. The fake edges do not interfere with any other CFG
3054 cleanups. */
3055 if (mode & CLEANUP_CROSSJUMP)
3056 add_noreturn_fake_exit_edges ();
3058 if (!dbg_cnt (cfg_cleanup))
3059 return changed;
3061 while (try_optimize_cfg (mode))
3063 delete_unreachable_blocks (), changed = true;
3064 if (!(mode & CLEANUP_NO_INSN_DEL))
3066 /* Try to remove some trivially dead insns when doing an expensive
3067 cleanup. But delete_trivially_dead_insns doesn't work after
3068 reload (it only handles pseudos) and run_fast_dce is too costly
3069 to run in every iteration.
3071 For effective cross jumping, we really want to run a fast DCE to
3072 clean up any dead conditions, or they get in the way of performing
3073 useful tail merges.
3075 Other transformations in cleanup_cfg are not so sensitive to dead
3076 code, so delete_trivially_dead_insns or even doing nothing at all
3077 is good enough. */
3078 if ((mode & CLEANUP_EXPENSIVE) && !reload_completed
3079 && !delete_trivially_dead_insns (get_insns (), max_reg_num ()))
3080 break;
3081 if ((mode & CLEANUP_CROSSJUMP) && crossjumps_occured)
3082 run_fast_dce ();
3084 else
3085 break;
3088 if (mode & CLEANUP_CROSSJUMP)
3089 remove_fake_exit_edges ();
3091 /* Don't call delete_dead_jumptables in cfglayout mode, because
3092 that function assumes that jump tables are in the insns stream.
3093 But we also don't _have_ to delete dead jumptables in cfglayout
3094 mode because we shouldn't even be looking at things that are
3095 not in a basic block. Dead jumptables are cleaned up when
3096 going out of cfglayout mode. */
3097 if (!(mode & CLEANUP_CFGLAYOUT))
3098 delete_dead_jumptables ();
3100 /* ??? We probably do this way too often. */
3101 if (current_loops
3102 && (changed
3103 || (mode & CLEANUP_CFG_CHANGED)))
3105 timevar_push (TV_REPAIR_LOOPS);
3106 /* The above doesn't preserve dominance info if available. */
3107 gcc_assert (!dom_info_available_p (CDI_DOMINATORS));
3108 calculate_dominance_info (CDI_DOMINATORS);
3109 fix_loop_structure (NULL);
3110 free_dominance_info (CDI_DOMINATORS);
3111 timevar_pop (TV_REPAIR_LOOPS);
3114 timevar_pop (TV_CLEANUP_CFG);
3116 return changed;
3119 namespace {
3121 const pass_data pass_data_jump =
3123 RTL_PASS, /* type */
3124 "jump", /* name */
3125 OPTGROUP_NONE, /* optinfo_flags */
3126 TV_JUMP, /* tv_id */
3127 0, /* properties_required */
3128 0, /* properties_provided */
3129 0, /* properties_destroyed */
3130 0, /* todo_flags_start */
3131 0, /* todo_flags_finish */
3134 class pass_jump : public rtl_opt_pass
3136 public:
3137 pass_jump (gcc::context *ctxt)
3138 : rtl_opt_pass (pass_data_jump, ctxt)
3141 /* opt_pass methods: */
3142 virtual unsigned int execute (function *);
3144 }; // class pass_jump
3146 unsigned int
3147 pass_jump::execute (function *)
3149 delete_trivially_dead_insns (get_insns (), max_reg_num ());
3150 if (dump_file)
3151 dump_flow_info (dump_file, dump_flags);
3152 cleanup_cfg ((optimize ? CLEANUP_EXPENSIVE : 0)
3153 | (flag_thread_jumps ? CLEANUP_THREADING : 0));
3154 return 0;
3157 } // anon namespace
3159 rtl_opt_pass *
3160 make_pass_jump (gcc::context *ctxt)
3162 return new pass_jump (ctxt);
3165 namespace {
3167 const pass_data pass_data_jump2 =
3169 RTL_PASS, /* type */
3170 "jump2", /* name */
3171 OPTGROUP_NONE, /* optinfo_flags */
3172 TV_JUMP, /* tv_id */
3173 0, /* properties_required */
3174 0, /* properties_provided */
3175 0, /* properties_destroyed */
3176 0, /* todo_flags_start */
3177 0, /* todo_flags_finish */
3180 class pass_jump2 : public rtl_opt_pass
3182 public:
3183 pass_jump2 (gcc::context *ctxt)
3184 : rtl_opt_pass (pass_data_jump2, ctxt)
3187 /* opt_pass methods: */
3188 virtual unsigned int execute (function *)
3190 cleanup_cfg (flag_crossjumping ? CLEANUP_CROSSJUMP : 0);
3191 return 0;
3194 }; // class pass_jump2
3196 } // anon namespace
3198 rtl_opt_pass *
3199 make_pass_jump2 (gcc::context *ctxt)
3201 return new pass_jump2 (ctxt);