2015-01-15 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / cfgcleanup.c
blobf26458e3680bfbd557f02c1227e778cd50e0e467
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 "hash-set.h"
59 #include "vec.h"
60 #include "machmode.h"
61 #include "input.h"
62 #include "function.h" /* For inline functions in emit-rtl.h they need crtl. */
63 #include "emit-rtl.h"
64 #include "tree-pass.h"
65 #include "cfgloop.h"
66 #include "expr.h"
67 #include "dominance.h"
68 #include "cfg.h"
69 #include "cfgrtl.h"
70 #include "cfganal.h"
71 #include "cfgbuild.h"
72 #include "cfgcleanup.h"
73 #include "predict.h"
74 #include "basic-block.h"
75 #include "df.h"
76 #include "dce.h"
77 #include "dbgcnt.h"
78 #include "emit-rtl.h"
79 #include "rtl-iter.h"
81 #define FORWARDER_BLOCK_P(BB) ((BB)->flags & BB_FORWARDER_BLOCK)
83 /* Set to true when we are running first pass of try_optimize_cfg loop. */
84 static bool first_pass;
86 /* Set to true if crossjumps occurred in the latest run of try_optimize_cfg. */
87 static bool crossjumps_occured;
89 /* Set to true if we couldn't run an optimization due to stale liveness
90 information; we should run df_analyze to enable more opportunities. */
91 static bool block_was_dirty;
93 static bool try_crossjump_to_edge (int, edge, edge, enum replace_direction);
94 static bool try_crossjump_bb (int, basic_block);
95 static bool outgoing_edges_match (int, basic_block, basic_block);
96 static enum replace_direction old_insns_match_p (int, rtx_insn *, rtx_insn *);
98 static void merge_blocks_move_predecessor_nojumps (basic_block, basic_block);
99 static void merge_blocks_move_successor_nojumps (basic_block, basic_block);
100 static bool try_optimize_cfg (int);
101 static bool try_simplify_condjump (basic_block);
102 static bool try_forward_edges (int, basic_block);
103 static edge thread_jump (edge, basic_block);
104 static bool mark_effect (rtx, bitmap);
105 static void notice_new_block (basic_block);
106 static void update_forwarder_flag (basic_block);
107 static void merge_memattrs (rtx, rtx);
109 /* Set flags for newly created block. */
111 static void
112 notice_new_block (basic_block bb)
114 if (!bb)
115 return;
117 if (forwarder_block_p (bb))
118 bb->flags |= BB_FORWARDER_BLOCK;
121 /* Recompute forwarder flag after block has been modified. */
123 static void
124 update_forwarder_flag (basic_block bb)
126 if (forwarder_block_p (bb))
127 bb->flags |= BB_FORWARDER_BLOCK;
128 else
129 bb->flags &= ~BB_FORWARDER_BLOCK;
132 /* Simplify a conditional jump around an unconditional jump.
133 Return true if something changed. */
135 static bool
136 try_simplify_condjump (basic_block cbranch_block)
138 basic_block jump_block, jump_dest_block, cbranch_dest_block;
139 edge cbranch_jump_edge, cbranch_fallthru_edge;
140 rtx_insn *cbranch_insn;
142 /* Verify that there are exactly two successors. */
143 if (EDGE_COUNT (cbranch_block->succs) != 2)
144 return false;
146 /* Verify that we've got a normal conditional branch at the end
147 of the block. */
148 cbranch_insn = BB_END (cbranch_block);
149 if (!any_condjump_p (cbranch_insn))
150 return false;
152 cbranch_fallthru_edge = FALLTHRU_EDGE (cbranch_block);
153 cbranch_jump_edge = BRANCH_EDGE (cbranch_block);
155 /* The next block must not have multiple predecessors, must not
156 be the last block in the function, and must contain just the
157 unconditional jump. */
158 jump_block = cbranch_fallthru_edge->dest;
159 if (!single_pred_p (jump_block)
160 || jump_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
161 || !FORWARDER_BLOCK_P (jump_block))
162 return false;
163 jump_dest_block = single_succ (jump_block);
165 /* If we are partitioning hot/cold basic blocks, we don't want to
166 mess up unconditional or indirect jumps that cross between hot
167 and cold sections.
169 Basic block partitioning may result in some jumps that appear to
170 be optimizable (or blocks that appear to be mergeable), but which really
171 must be left untouched (they are required to make it safely across
172 partition boundaries). See the comments at the top of
173 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
175 if (BB_PARTITION (jump_block) != BB_PARTITION (jump_dest_block)
176 || (cbranch_jump_edge->flags & EDGE_CROSSING))
177 return false;
179 /* The conditional branch must target the block after the
180 unconditional branch. */
181 cbranch_dest_block = cbranch_jump_edge->dest;
183 if (cbranch_dest_block == EXIT_BLOCK_PTR_FOR_FN (cfun)
184 || !can_fallthru (jump_block, cbranch_dest_block))
185 return false;
187 /* Invert the conditional branch. */
188 if (!invert_jump (cbranch_insn, block_label (jump_dest_block), 0))
189 return false;
191 if (dump_file)
192 fprintf (dump_file, "Simplifying condjump %i around jump %i\n",
193 INSN_UID (cbranch_insn), INSN_UID (BB_END (jump_block)));
195 /* Success. Update the CFG to match. Note that after this point
196 the edge variable names appear backwards; the redirection is done
197 this way to preserve edge profile data. */
198 cbranch_jump_edge = redirect_edge_succ_nodup (cbranch_jump_edge,
199 cbranch_dest_block);
200 cbranch_fallthru_edge = redirect_edge_succ_nodup (cbranch_fallthru_edge,
201 jump_dest_block);
202 cbranch_jump_edge->flags |= EDGE_FALLTHRU;
203 cbranch_fallthru_edge->flags &= ~EDGE_FALLTHRU;
204 update_br_prob_note (cbranch_block);
206 /* Delete the block with the unconditional jump, and clean up the mess. */
207 delete_basic_block (jump_block);
208 tidy_fallthru_edge (cbranch_jump_edge);
209 update_forwarder_flag (cbranch_block);
211 return true;
214 /* Attempt to prove that operation is NOOP using CSElib or mark the effect
215 on register. Used by jump threading. */
217 static bool
218 mark_effect (rtx exp, regset nonequal)
220 int regno;
221 rtx dest;
222 switch (GET_CODE (exp))
224 /* In case we do clobber the register, mark it as equal, as we know the
225 value is dead so it don't have to match. */
226 case CLOBBER:
227 if (REG_P (XEXP (exp, 0)))
229 dest = XEXP (exp, 0);
230 regno = REGNO (dest);
231 if (HARD_REGISTER_NUM_P (regno))
232 bitmap_clear_range (nonequal, regno,
233 hard_regno_nregs[regno][GET_MODE (dest)]);
234 else
235 bitmap_clear_bit (nonequal, regno);
237 return false;
239 case SET:
240 if (rtx_equal_for_cselib_p (SET_DEST (exp), SET_SRC (exp)))
241 return false;
242 dest = SET_DEST (exp);
243 if (dest == pc_rtx)
244 return false;
245 if (!REG_P (dest))
246 return true;
247 regno = REGNO (dest);
248 if (HARD_REGISTER_NUM_P (regno))
249 bitmap_set_range (nonequal, regno,
250 hard_regno_nregs[regno][GET_MODE (dest)]);
251 else
252 bitmap_set_bit (nonequal, regno);
253 return false;
255 default:
256 return false;
260 /* Return true if X contains a register in NONEQUAL. */
261 static bool
262 mentions_nonequal_regs (const_rtx x, regset nonequal)
264 subrtx_iterator::array_type array;
265 FOR_EACH_SUBRTX (iter, array, x, NONCONST)
267 const_rtx x = *iter;
268 if (REG_P (x))
270 unsigned int regno = REGNO (x);
271 if (REGNO_REG_SET_P (nonequal, regno))
272 return true;
273 if (regno < FIRST_PSEUDO_REGISTER)
275 int n = hard_regno_nregs[regno][GET_MODE (x)];
276 while (--n > 0)
277 if (REGNO_REG_SET_P (nonequal, regno + n))
278 return true;
282 return false;
285 /* Attempt to prove that the basic block B will have no side effects and
286 always continues in the same edge if reached via E. Return the edge
287 if exist, NULL otherwise. */
289 static edge
290 thread_jump (edge e, basic_block b)
292 rtx set1, set2, cond1, cond2;
293 rtx_insn *insn;
294 enum rtx_code code1, code2, reversed_code2;
295 bool reverse1 = false;
296 unsigned i;
297 regset nonequal;
298 bool failed = false;
299 reg_set_iterator rsi;
301 if (b->flags & BB_NONTHREADABLE_BLOCK)
302 return NULL;
304 /* At the moment, we do handle only conditional jumps, but later we may
305 want to extend this code to tablejumps and others. */
306 if (EDGE_COUNT (e->src->succs) != 2)
307 return NULL;
308 if (EDGE_COUNT (b->succs) != 2)
310 b->flags |= BB_NONTHREADABLE_BLOCK;
311 return NULL;
314 /* Second branch must end with onlyjump, as we will eliminate the jump. */
315 if (!any_condjump_p (BB_END (e->src)))
316 return NULL;
318 if (!any_condjump_p (BB_END (b)) || !onlyjump_p (BB_END (b)))
320 b->flags |= BB_NONTHREADABLE_BLOCK;
321 return NULL;
324 set1 = pc_set (BB_END (e->src));
325 set2 = pc_set (BB_END (b));
326 if (((e->flags & EDGE_FALLTHRU) != 0)
327 != (XEXP (SET_SRC (set1), 1) == pc_rtx))
328 reverse1 = true;
330 cond1 = XEXP (SET_SRC (set1), 0);
331 cond2 = XEXP (SET_SRC (set2), 0);
332 if (reverse1)
333 code1 = reversed_comparison_code (cond1, BB_END (e->src));
334 else
335 code1 = GET_CODE (cond1);
337 code2 = GET_CODE (cond2);
338 reversed_code2 = reversed_comparison_code (cond2, BB_END (b));
340 if (!comparison_dominates_p (code1, code2)
341 && !comparison_dominates_p (code1, reversed_code2))
342 return NULL;
344 /* Ensure that the comparison operators are equivalent.
345 ??? This is far too pessimistic. We should allow swapped operands,
346 different CCmodes, or for example comparisons for interval, that
347 dominate even when operands are not equivalent. */
348 if (!rtx_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
349 || !rtx_equal_p (XEXP (cond1, 1), XEXP (cond2, 1)))
350 return NULL;
352 /* Short circuit cases where block B contains some side effects, as we can't
353 safely bypass it. */
354 for (insn = NEXT_INSN (BB_HEAD (b)); insn != NEXT_INSN (BB_END (b));
355 insn = NEXT_INSN (insn))
356 if (INSN_P (insn) && side_effects_p (PATTERN (insn)))
358 b->flags |= BB_NONTHREADABLE_BLOCK;
359 return NULL;
362 cselib_init (0);
364 /* First process all values computed in the source basic block. */
365 for (insn = NEXT_INSN (BB_HEAD (e->src));
366 insn != NEXT_INSN (BB_END (e->src));
367 insn = NEXT_INSN (insn))
368 if (INSN_P (insn))
369 cselib_process_insn (insn);
371 nonequal = BITMAP_ALLOC (NULL);
372 CLEAR_REG_SET (nonequal);
374 /* Now assume that we've continued by the edge E to B and continue
375 processing as if it were same basic block.
376 Our goal is to prove that whole block is an NOOP. */
378 for (insn = NEXT_INSN (BB_HEAD (b));
379 insn != NEXT_INSN (BB_END (b)) && !failed;
380 insn = NEXT_INSN (insn))
382 if (INSN_P (insn))
384 rtx pat = PATTERN (insn);
386 if (GET_CODE (pat) == PARALLEL)
388 for (i = 0; i < (unsigned)XVECLEN (pat, 0); i++)
389 failed |= mark_effect (XVECEXP (pat, 0, i), nonequal);
391 else
392 failed |= mark_effect (pat, nonequal);
395 cselib_process_insn (insn);
398 /* Later we should clear nonequal of dead registers. So far we don't
399 have life information in cfg_cleanup. */
400 if (failed)
402 b->flags |= BB_NONTHREADABLE_BLOCK;
403 goto failed_exit;
406 /* cond2 must not mention any register that is not equal to the
407 former block. */
408 if (mentions_nonequal_regs (cond2, nonequal))
409 goto failed_exit;
411 EXECUTE_IF_SET_IN_REG_SET (nonequal, 0, i, rsi)
412 goto failed_exit;
414 BITMAP_FREE (nonequal);
415 cselib_finish ();
416 if ((comparison_dominates_p (code1, code2) != 0)
417 != (XEXP (SET_SRC (set2), 1) == pc_rtx))
418 return BRANCH_EDGE (b);
419 else
420 return FALLTHRU_EDGE (b);
422 failed_exit:
423 BITMAP_FREE (nonequal);
424 cselib_finish ();
425 return NULL;
428 /* Attempt to forward edges leaving basic block B.
429 Return true if successful. */
431 static bool
432 try_forward_edges (int mode, basic_block b)
434 bool changed = false;
435 edge_iterator ei;
436 edge e, *threaded_edges = NULL;
438 /* If we are partitioning hot/cold basic blocks, we don't want to
439 mess up unconditional or indirect jumps that cross between hot
440 and cold sections.
442 Basic block partitioning may result in some jumps that appear to
443 be optimizable (or blocks that appear to be mergeable), but which really
444 must be left untouched (they are required to make it safely across
445 partition boundaries). See the comments at the top of
446 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
448 if (JUMP_P (BB_END (b)) && CROSSING_JUMP_P (BB_END (b)))
449 return false;
451 for (ei = ei_start (b->succs); (e = ei_safe_edge (ei)); )
453 basic_block target, first;
454 location_t goto_locus;
455 int counter;
456 bool threaded = false;
457 int nthreaded_edges = 0;
458 bool may_thread = first_pass || (b->flags & BB_MODIFIED) != 0;
460 /* Skip complex edges because we don't know how to update them.
462 Still handle fallthru edges, as we can succeed to forward fallthru
463 edge to the same place as the branch edge of conditional branch
464 and turn conditional branch to an unconditional branch. */
465 if (e->flags & EDGE_COMPLEX)
467 ei_next (&ei);
468 continue;
471 target = first = e->dest;
472 counter = NUM_FIXED_BLOCKS;
473 goto_locus = e->goto_locus;
475 /* If we are partitioning hot/cold basic_blocks, we don't want to mess
476 up jumps that cross between hot/cold sections.
478 Basic block partitioning may result in some jumps that appear
479 to be optimizable (or blocks that appear to be mergeable), but which
480 really must be left untouched (they are required to make it safely
481 across partition boundaries). See the comments at the top of
482 bb-reorder.c:partition_hot_cold_basic_blocks for complete
483 details. */
485 if (first != EXIT_BLOCK_PTR_FOR_FN (cfun)
486 && JUMP_P (BB_END (first))
487 && CROSSING_JUMP_P (BB_END (first)))
488 return changed;
490 while (counter < n_basic_blocks_for_fn (cfun))
492 basic_block new_target = NULL;
493 bool new_target_threaded = false;
494 may_thread |= (target->flags & BB_MODIFIED) != 0;
496 if (FORWARDER_BLOCK_P (target)
497 && !(single_succ_edge (target)->flags & EDGE_CROSSING)
498 && single_succ (target) != EXIT_BLOCK_PTR_FOR_FN (cfun))
500 /* Bypass trivial infinite loops. */
501 new_target = single_succ (target);
502 if (target == new_target)
503 counter = n_basic_blocks_for_fn (cfun);
504 else if (!optimize)
506 /* When not optimizing, ensure that edges or forwarder
507 blocks with different locus are not optimized out. */
508 location_t new_locus = single_succ_edge (target)->goto_locus;
509 location_t locus = goto_locus;
511 if (LOCATION_LOCUS (new_locus) != UNKNOWN_LOCATION
512 && LOCATION_LOCUS (locus) != UNKNOWN_LOCATION
513 && new_locus != locus)
514 new_target = NULL;
515 else
517 if (LOCATION_LOCUS (new_locus) != UNKNOWN_LOCATION)
518 locus = new_locus;
520 rtx_insn *last = BB_END (target);
521 if (DEBUG_INSN_P (last))
522 last = prev_nondebug_insn (last);
523 if (last && INSN_P (last))
524 new_locus = INSN_LOCATION (last);
525 else
526 new_locus = UNKNOWN_LOCATION;
528 if (LOCATION_LOCUS (new_locus) != UNKNOWN_LOCATION
529 && LOCATION_LOCUS (locus) != UNKNOWN_LOCATION
530 && new_locus != locus)
531 new_target = NULL;
532 else
534 if (LOCATION_LOCUS (new_locus) != UNKNOWN_LOCATION)
535 locus = new_locus;
537 goto_locus = locus;
543 /* Allow to thread only over one edge at time to simplify updating
544 of probabilities. */
545 else if ((mode & CLEANUP_THREADING) && may_thread)
547 edge t = thread_jump (e, target);
548 if (t)
550 if (!threaded_edges)
551 threaded_edges = XNEWVEC (edge,
552 n_basic_blocks_for_fn (cfun));
553 else
555 int i;
557 /* Detect an infinite loop across blocks not
558 including the start block. */
559 for (i = 0; i < nthreaded_edges; ++i)
560 if (threaded_edges[i] == t)
561 break;
562 if (i < nthreaded_edges)
564 counter = n_basic_blocks_for_fn (cfun);
565 break;
569 /* Detect an infinite loop across the start block. */
570 if (t->dest == b)
571 break;
573 gcc_assert (nthreaded_edges
574 < (n_basic_blocks_for_fn (cfun)
575 - NUM_FIXED_BLOCKS));
576 threaded_edges[nthreaded_edges++] = t;
578 new_target = t->dest;
579 new_target_threaded = true;
583 if (!new_target)
584 break;
586 counter++;
587 target = new_target;
588 threaded |= new_target_threaded;
591 if (counter >= n_basic_blocks_for_fn (cfun))
593 if (dump_file)
594 fprintf (dump_file, "Infinite loop in BB %i.\n",
595 target->index);
597 else if (target == first)
598 ; /* We didn't do anything. */
599 else
601 /* Save the values now, as the edge may get removed. */
602 gcov_type edge_count = e->count;
603 int edge_probability = e->probability;
604 int edge_frequency;
605 int n = 0;
607 e->goto_locus = goto_locus;
609 /* Don't force if target is exit block. */
610 if (threaded && target != EXIT_BLOCK_PTR_FOR_FN (cfun))
612 notice_new_block (redirect_edge_and_branch_force (e, target));
613 if (dump_file)
614 fprintf (dump_file, "Conditionals threaded.\n");
616 else if (!redirect_edge_and_branch (e, target))
618 if (dump_file)
619 fprintf (dump_file,
620 "Forwarding edge %i->%i to %i failed.\n",
621 b->index, e->dest->index, target->index);
622 ei_next (&ei);
623 continue;
626 /* We successfully forwarded the edge. Now update profile
627 data: for each edge we traversed in the chain, remove
628 the original edge's execution count. */
629 edge_frequency = apply_probability (b->frequency, edge_probability);
633 edge t;
635 if (!single_succ_p (first))
637 gcc_assert (n < nthreaded_edges);
638 t = threaded_edges [n++];
639 gcc_assert (t->src == first);
640 update_bb_profile_for_threading (first, edge_frequency,
641 edge_count, t);
642 update_br_prob_note (first);
644 else
646 first->count -= edge_count;
647 if (first->count < 0)
648 first->count = 0;
649 first->frequency -= edge_frequency;
650 if (first->frequency < 0)
651 first->frequency = 0;
652 /* It is possible that as the result of
653 threading we've removed edge as it is
654 threaded to the fallthru edge. Avoid
655 getting out of sync. */
656 if (n < nthreaded_edges
657 && first == threaded_edges [n]->src)
658 n++;
659 t = single_succ_edge (first);
662 t->count -= edge_count;
663 if (t->count < 0)
664 t->count = 0;
665 first = t->dest;
667 while (first != target);
669 changed = true;
670 continue;
672 ei_next (&ei);
675 free (threaded_edges);
676 return changed;
680 /* Blocks A and B are to be merged into a single block. A has no incoming
681 fallthru edge, so it can be moved before B without adding or modifying
682 any jumps (aside from the jump from A to B). */
684 static void
685 merge_blocks_move_predecessor_nojumps (basic_block a, basic_block b)
687 rtx_insn *barrier;
689 /* If we are partitioning hot/cold basic blocks, we don't want to
690 mess up unconditional or indirect jumps that cross between hot
691 and cold sections.
693 Basic block partitioning may result in some jumps that appear to
694 be optimizable (or blocks that appear to be mergeable), but which really
695 must be left untouched (they are required to make it safely across
696 partition boundaries). See the comments at the top of
697 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
699 if (BB_PARTITION (a) != BB_PARTITION (b))
700 return;
702 barrier = next_nonnote_insn (BB_END (a));
703 gcc_assert (BARRIER_P (barrier));
704 delete_insn (barrier);
706 /* Scramble the insn chain. */
707 if (BB_END (a) != PREV_INSN (BB_HEAD (b)))
708 reorder_insns_nobb (BB_HEAD (a), BB_END (a), PREV_INSN (BB_HEAD (b)));
709 df_set_bb_dirty (a);
711 if (dump_file)
712 fprintf (dump_file, "Moved block %d before %d and merged.\n",
713 a->index, b->index);
715 /* Swap the records for the two blocks around. */
717 unlink_block (a);
718 link_block (a, b->prev_bb);
720 /* Now blocks A and B are contiguous. Merge them. */
721 merge_blocks (a, b);
724 /* Blocks A and B are to be merged into a single block. B has no outgoing
725 fallthru edge, so it can be moved after A without adding or modifying
726 any jumps (aside from the jump from A to B). */
728 static void
729 merge_blocks_move_successor_nojumps (basic_block a, basic_block b)
731 rtx_insn *barrier, *real_b_end;
732 rtx label;
733 rtx_jump_table_data *table;
735 /* If we are partitioning hot/cold basic blocks, we don't want to
736 mess up unconditional or indirect jumps that cross between hot
737 and cold sections.
739 Basic block partitioning may result in some jumps that appear to
740 be optimizable (or blocks that appear to be mergeable), but which really
741 must be left untouched (they are required to make it safely across
742 partition boundaries). See the comments at the top of
743 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
745 if (BB_PARTITION (a) != BB_PARTITION (b))
746 return;
748 real_b_end = BB_END (b);
750 /* If there is a jump table following block B temporarily add the jump table
751 to block B so that it will also be moved to the correct location. */
752 if (tablejump_p (BB_END (b), &label, &table)
753 && prev_active_insn (label) == BB_END (b))
755 BB_END (b) = table;
758 /* There had better have been a barrier there. Delete it. */
759 barrier = NEXT_INSN (BB_END (b));
760 if (barrier && BARRIER_P (barrier))
761 delete_insn (barrier);
764 /* Scramble the insn chain. */
765 reorder_insns_nobb (BB_HEAD (b), BB_END (b), BB_END (a));
767 /* Restore the real end of b. */
768 BB_END (b) = real_b_end;
770 if (dump_file)
771 fprintf (dump_file, "Moved block %d after %d and merged.\n",
772 b->index, a->index);
774 /* Now blocks A and B are contiguous. Merge them. */
775 merge_blocks (a, b);
778 /* Attempt to merge basic blocks that are potentially non-adjacent.
779 Return NULL iff the attempt failed, otherwise return basic block
780 where cleanup_cfg should continue. Because the merging commonly
781 moves basic block away or introduces another optimization
782 possibility, return basic block just before B so cleanup_cfg don't
783 need to iterate.
785 It may be good idea to return basic block before C in the case
786 C has been moved after B and originally appeared earlier in the
787 insn sequence, but we have no information available about the
788 relative ordering of these two. Hopefully it is not too common. */
790 static basic_block
791 merge_blocks_move (edge e, basic_block b, basic_block c, int mode)
793 basic_block next;
795 /* If we are partitioning hot/cold basic blocks, we don't want to
796 mess up unconditional or indirect jumps that cross between hot
797 and cold sections.
799 Basic block partitioning may result in some jumps that appear to
800 be optimizable (or blocks that appear to be mergeable), but which really
801 must be left untouched (they are required to make it safely across
802 partition boundaries). See the comments at the top of
803 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
805 if (BB_PARTITION (b) != BB_PARTITION (c))
806 return NULL;
808 /* If B has a fallthru edge to C, no need to move anything. */
809 if (e->flags & EDGE_FALLTHRU)
811 int b_index = b->index, c_index = c->index;
813 /* Protect the loop latches. */
814 if (current_loops && c->loop_father->latch == c)
815 return NULL;
817 merge_blocks (b, c);
818 update_forwarder_flag (b);
820 if (dump_file)
821 fprintf (dump_file, "Merged %d and %d without moving.\n",
822 b_index, c_index);
824 return b->prev_bb == ENTRY_BLOCK_PTR_FOR_FN (cfun) ? b : b->prev_bb;
827 /* Otherwise we will need to move code around. Do that only if expensive
828 transformations are allowed. */
829 else if (mode & CLEANUP_EXPENSIVE)
831 edge tmp_edge, b_fallthru_edge;
832 bool c_has_outgoing_fallthru;
833 bool b_has_incoming_fallthru;
835 /* Avoid overactive code motion, as the forwarder blocks should be
836 eliminated by edge redirection instead. One exception might have
837 been if B is a forwarder block and C has no fallthru edge, but
838 that should be cleaned up by bb-reorder instead. */
839 if (FORWARDER_BLOCK_P (b) || FORWARDER_BLOCK_P (c))
840 return NULL;
842 /* We must make sure to not munge nesting of lexical blocks,
843 and loop notes. This is done by squeezing out all the notes
844 and leaving them there to lie. Not ideal, but functional. */
846 tmp_edge = find_fallthru_edge (c->succs);
847 c_has_outgoing_fallthru = (tmp_edge != NULL);
849 tmp_edge = find_fallthru_edge (b->preds);
850 b_has_incoming_fallthru = (tmp_edge != NULL);
851 b_fallthru_edge = tmp_edge;
852 next = b->prev_bb;
853 if (next == c)
854 next = next->prev_bb;
856 /* Otherwise, we're going to try to move C after B. If C does
857 not have an outgoing fallthru, then it can be moved
858 immediately after B without introducing or modifying jumps. */
859 if (! c_has_outgoing_fallthru)
861 merge_blocks_move_successor_nojumps (b, c);
862 return next == ENTRY_BLOCK_PTR_FOR_FN (cfun) ? next->next_bb : next;
865 /* If B does not have an incoming fallthru, then it can be moved
866 immediately before C without introducing or modifying jumps.
867 C cannot be the first block, so we do not have to worry about
868 accessing a non-existent block. */
870 if (b_has_incoming_fallthru)
872 basic_block bb;
874 if (b_fallthru_edge->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
875 return NULL;
876 bb = force_nonfallthru (b_fallthru_edge);
877 if (bb)
878 notice_new_block (bb);
881 merge_blocks_move_predecessor_nojumps (b, c);
882 return next == ENTRY_BLOCK_PTR_FOR_FN (cfun) ? next->next_bb : next;
885 return NULL;
889 /* Removes the memory attributes of MEM expression
890 if they are not equal. */
892 static void
893 merge_memattrs (rtx x, rtx y)
895 int i;
896 int j;
897 enum rtx_code code;
898 const char *fmt;
900 if (x == y)
901 return;
902 if (x == 0 || y == 0)
903 return;
905 code = GET_CODE (x);
907 if (code != GET_CODE (y))
908 return;
910 if (GET_MODE (x) != GET_MODE (y))
911 return;
913 if (code == MEM && !mem_attrs_eq_p (MEM_ATTRS (x), MEM_ATTRS (y)))
915 if (! MEM_ATTRS (x))
916 MEM_ATTRS (y) = 0;
917 else if (! MEM_ATTRS (y))
918 MEM_ATTRS (x) = 0;
919 else
921 HOST_WIDE_INT mem_size;
923 if (MEM_ALIAS_SET (x) != MEM_ALIAS_SET (y))
925 set_mem_alias_set (x, 0);
926 set_mem_alias_set (y, 0);
929 if (! mem_expr_equal_p (MEM_EXPR (x), MEM_EXPR (y)))
931 set_mem_expr (x, 0);
932 set_mem_expr (y, 0);
933 clear_mem_offset (x);
934 clear_mem_offset (y);
936 else if (MEM_OFFSET_KNOWN_P (x) != MEM_OFFSET_KNOWN_P (y)
937 || (MEM_OFFSET_KNOWN_P (x)
938 && MEM_OFFSET (x) != MEM_OFFSET (y)))
940 clear_mem_offset (x);
941 clear_mem_offset (y);
944 if (MEM_SIZE_KNOWN_P (x) && MEM_SIZE_KNOWN_P (y))
946 mem_size = MAX (MEM_SIZE (x), MEM_SIZE (y));
947 set_mem_size (x, mem_size);
948 set_mem_size (y, mem_size);
950 else
952 clear_mem_size (x);
953 clear_mem_size (y);
956 set_mem_align (x, MIN (MEM_ALIGN (x), MEM_ALIGN (y)));
957 set_mem_align (y, MEM_ALIGN (x));
960 if (code == MEM)
962 if (MEM_READONLY_P (x) != MEM_READONLY_P (y))
964 MEM_READONLY_P (x) = 0;
965 MEM_READONLY_P (y) = 0;
967 if (MEM_NOTRAP_P (x) != MEM_NOTRAP_P (y))
969 MEM_NOTRAP_P (x) = 0;
970 MEM_NOTRAP_P (y) = 0;
972 if (MEM_VOLATILE_P (x) != MEM_VOLATILE_P (y))
974 MEM_VOLATILE_P (x) = 1;
975 MEM_VOLATILE_P (y) = 1;
979 fmt = GET_RTX_FORMAT (code);
980 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
982 switch (fmt[i])
984 case 'E':
985 /* Two vectors must have the same length. */
986 if (XVECLEN (x, i) != XVECLEN (y, i))
987 return;
989 for (j = 0; j < XVECLEN (x, i); j++)
990 merge_memattrs (XVECEXP (x, i, j), XVECEXP (y, i, j));
992 break;
994 case 'e':
995 merge_memattrs (XEXP (x, i), XEXP (y, i));
998 return;
1002 /* Checks if patterns P1 and P2 are equivalent, apart from the possibly
1003 different single sets S1 and S2. */
1005 static bool
1006 equal_different_set_p (rtx p1, rtx s1, rtx p2, rtx s2)
1008 int i;
1009 rtx e1, e2;
1011 if (p1 == s1 && p2 == s2)
1012 return true;
1014 if (GET_CODE (p1) != PARALLEL || GET_CODE (p2) != PARALLEL)
1015 return false;
1017 if (XVECLEN (p1, 0) != XVECLEN (p2, 0))
1018 return false;
1020 for (i = 0; i < XVECLEN (p1, 0); i++)
1022 e1 = XVECEXP (p1, 0, i);
1023 e2 = XVECEXP (p2, 0, i);
1024 if (e1 == s1 && e2 == s2)
1025 continue;
1026 if (reload_completed
1027 ? rtx_renumbered_equal_p (e1, e2) : rtx_equal_p (e1, e2))
1028 continue;
1030 return false;
1033 return true;
1036 /* Examine register notes on I1 and I2 and return:
1037 - dir_forward if I1 can be replaced by I2, or
1038 - dir_backward if I2 can be replaced by I1, or
1039 - dir_both if both are the case. */
1041 static enum replace_direction
1042 can_replace_by (rtx_insn *i1, rtx_insn *i2)
1044 rtx s1, s2, d1, d2, src1, src2, note1, note2;
1045 bool c1, c2;
1047 /* Check for 2 sets. */
1048 s1 = single_set (i1);
1049 s2 = single_set (i2);
1050 if (s1 == NULL_RTX || s2 == NULL_RTX)
1051 return dir_none;
1053 /* Check that the 2 sets set the same dest. */
1054 d1 = SET_DEST (s1);
1055 d2 = SET_DEST (s2);
1056 if (!(reload_completed
1057 ? rtx_renumbered_equal_p (d1, d2) : rtx_equal_p (d1, d2)))
1058 return dir_none;
1060 /* Find identical req_equiv or reg_equal note, which implies that the 2 sets
1061 set dest to the same value. */
1062 note1 = find_reg_equal_equiv_note (i1);
1063 note2 = find_reg_equal_equiv_note (i2);
1064 if (!note1 || !note2 || !rtx_equal_p (XEXP (note1, 0), XEXP (note2, 0))
1065 || !CONST_INT_P (XEXP (note1, 0)))
1066 return dir_none;
1068 if (!equal_different_set_p (PATTERN (i1), s1, PATTERN (i2), s2))
1069 return dir_none;
1071 /* Although the 2 sets set dest to the same value, we cannot replace
1072 (set (dest) (const_int))
1074 (set (dest) (reg))
1075 because we don't know if the reg is live and has the same value at the
1076 location of replacement. */
1077 src1 = SET_SRC (s1);
1078 src2 = SET_SRC (s2);
1079 c1 = CONST_INT_P (src1);
1080 c2 = CONST_INT_P (src2);
1081 if (c1 && c2)
1082 return dir_both;
1083 else if (c2)
1084 return dir_forward;
1085 else if (c1)
1086 return dir_backward;
1088 return dir_none;
1091 /* Merges directions A and B. */
1093 static enum replace_direction
1094 merge_dir (enum replace_direction a, enum replace_direction b)
1096 /* Implements the following table:
1097 |bo fw bw no
1098 ---+-----------
1099 bo |bo fw bw no
1100 fw |-- fw no no
1101 bw |-- -- bw no
1102 no |-- -- -- no. */
1104 if (a == b)
1105 return a;
1107 if (a == dir_both)
1108 return b;
1109 if (b == dir_both)
1110 return a;
1112 return dir_none;
1115 /* Examine I1 and I2 and return:
1116 - dir_forward if I1 can be replaced by I2, or
1117 - dir_backward if I2 can be replaced by I1, or
1118 - dir_both if both are the case. */
1120 static enum replace_direction
1121 old_insns_match_p (int mode ATTRIBUTE_UNUSED, rtx_insn *i1, rtx_insn *i2)
1123 rtx p1, p2;
1125 /* Verify that I1 and I2 are equivalent. */
1126 if (GET_CODE (i1) != GET_CODE (i2))
1127 return dir_none;
1129 /* __builtin_unreachable() may lead to empty blocks (ending with
1130 NOTE_INSN_BASIC_BLOCK). They may be crossjumped. */
1131 if (NOTE_INSN_BASIC_BLOCK_P (i1) && NOTE_INSN_BASIC_BLOCK_P (i2))
1132 return dir_both;
1134 /* ??? Do not allow cross-jumping between different stack levels. */
1135 p1 = find_reg_note (i1, REG_ARGS_SIZE, NULL);
1136 p2 = find_reg_note (i2, REG_ARGS_SIZE, NULL);
1137 if (p1 && p2)
1139 p1 = XEXP (p1, 0);
1140 p2 = XEXP (p2, 0);
1141 if (!rtx_equal_p (p1, p2))
1142 return dir_none;
1144 /* ??? Worse, this adjustment had better be constant lest we
1145 have differing incoming stack levels. */
1146 if (!frame_pointer_needed
1147 && find_args_size_adjust (i1) == HOST_WIDE_INT_MIN)
1148 return dir_none;
1150 else if (p1 || p2)
1151 return dir_none;
1153 p1 = PATTERN (i1);
1154 p2 = PATTERN (i2);
1156 if (GET_CODE (p1) != GET_CODE (p2))
1157 return dir_none;
1159 /* If this is a CALL_INSN, compare register usage information.
1160 If we don't check this on stack register machines, the two
1161 CALL_INSNs might be merged leaving reg-stack.c with mismatching
1162 numbers of stack registers in the same basic block.
1163 If we don't check this on machines with delay slots, a delay slot may
1164 be filled that clobbers a parameter expected by the subroutine.
1166 ??? We take the simple route for now and assume that if they're
1167 equal, they were constructed identically.
1169 Also check for identical exception regions. */
1171 if (CALL_P (i1))
1173 /* Ensure the same EH region. */
1174 rtx n1 = find_reg_note (i1, REG_EH_REGION, 0);
1175 rtx n2 = find_reg_note (i2, REG_EH_REGION, 0);
1177 if (!n1 && n2)
1178 return dir_none;
1180 if (n1 && (!n2 || XEXP (n1, 0) != XEXP (n2, 0)))
1181 return dir_none;
1183 if (!rtx_equal_p (CALL_INSN_FUNCTION_USAGE (i1),
1184 CALL_INSN_FUNCTION_USAGE (i2))
1185 || SIBLING_CALL_P (i1) != SIBLING_CALL_P (i2))
1186 return dir_none;
1188 /* For address sanitizer, never crossjump __asan_report_* builtins,
1189 otherwise errors might be reported on incorrect lines. */
1190 if (flag_sanitize & SANITIZE_ADDRESS)
1192 rtx call = get_call_rtx_from (i1);
1193 if (call && GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF)
1195 rtx symbol = XEXP (XEXP (call, 0), 0);
1196 if (SYMBOL_REF_DECL (symbol)
1197 && TREE_CODE (SYMBOL_REF_DECL (symbol)) == FUNCTION_DECL)
1199 if ((DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol))
1200 == BUILT_IN_NORMAL)
1201 && DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol))
1202 >= BUILT_IN_ASAN_REPORT_LOAD1
1203 && DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol))
1204 <= BUILT_IN_ASAN_STOREN)
1205 return dir_none;
1211 #ifdef STACK_REGS
1212 /* If cross_jump_death_matters is not 0, the insn's mode
1213 indicates whether or not the insn contains any stack-like
1214 regs. */
1216 if ((mode & CLEANUP_POST_REGSTACK) && stack_regs_mentioned (i1))
1218 /* If register stack conversion has already been done, then
1219 death notes must also be compared before it is certain that
1220 the two instruction streams match. */
1222 rtx note;
1223 HARD_REG_SET i1_regset, i2_regset;
1225 CLEAR_HARD_REG_SET (i1_regset);
1226 CLEAR_HARD_REG_SET (i2_regset);
1228 for (note = REG_NOTES (i1); note; note = XEXP (note, 1))
1229 if (REG_NOTE_KIND (note) == REG_DEAD && STACK_REG_P (XEXP (note, 0)))
1230 SET_HARD_REG_BIT (i1_regset, REGNO (XEXP (note, 0)));
1232 for (note = REG_NOTES (i2); note; note = XEXP (note, 1))
1233 if (REG_NOTE_KIND (note) == REG_DEAD && STACK_REG_P (XEXP (note, 0)))
1234 SET_HARD_REG_BIT (i2_regset, REGNO (XEXP (note, 0)));
1236 if (!hard_reg_set_equal_p (i1_regset, i2_regset))
1237 return dir_none;
1239 #endif
1241 if (reload_completed
1242 ? rtx_renumbered_equal_p (p1, p2) : rtx_equal_p (p1, p2))
1243 return dir_both;
1245 return can_replace_by (i1, i2);
1248 /* When comparing insns I1 and I2 in flow_find_cross_jump or
1249 flow_find_head_matching_sequence, ensure the notes match. */
1251 static void
1252 merge_notes (rtx_insn *i1, rtx_insn *i2)
1254 /* If the merged insns have different REG_EQUAL notes, then
1255 remove them. */
1256 rtx equiv1 = find_reg_equal_equiv_note (i1);
1257 rtx equiv2 = find_reg_equal_equiv_note (i2);
1259 if (equiv1 && !equiv2)
1260 remove_note (i1, equiv1);
1261 else if (!equiv1 && equiv2)
1262 remove_note (i2, equiv2);
1263 else if (equiv1 && equiv2
1264 && !rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))
1266 remove_note (i1, equiv1);
1267 remove_note (i2, equiv2);
1271 /* Walks from I1 in BB1 backward till the next non-debug insn, and returns the
1272 resulting insn in I1, and the corresponding bb in BB1. At the head of a
1273 bb, if there is a predecessor bb that reaches this bb via fallthru, and
1274 FOLLOW_FALLTHRU, walks further in the predecessor bb and registers this in
1275 DID_FALLTHRU. Otherwise, stops at the head of the bb. */
1277 static void
1278 walk_to_nondebug_insn (rtx_insn **i1, basic_block *bb1, bool follow_fallthru,
1279 bool *did_fallthru)
1281 edge fallthru;
1283 *did_fallthru = false;
1285 /* Ignore notes. */
1286 while (!NONDEBUG_INSN_P (*i1))
1288 if (*i1 != BB_HEAD (*bb1))
1290 *i1 = PREV_INSN (*i1);
1291 continue;
1294 if (!follow_fallthru)
1295 return;
1297 fallthru = find_fallthru_edge ((*bb1)->preds);
1298 if (!fallthru || fallthru->src == ENTRY_BLOCK_PTR_FOR_FN (cfun)
1299 || !single_succ_p (fallthru->src))
1300 return;
1302 *bb1 = fallthru->src;
1303 *i1 = BB_END (*bb1);
1304 *did_fallthru = true;
1308 /* Look through the insns at the end of BB1 and BB2 and find the longest
1309 sequence that are either equivalent, or allow forward or backward
1310 replacement. Store the first insns for that sequence in *F1 and *F2 and
1311 return the sequence length.
1313 DIR_P indicates the allowed replacement direction on function entry, and
1314 the actual replacement direction on function exit. If NULL, only equivalent
1315 sequences are allowed.
1317 To simplify callers of this function, if the blocks match exactly,
1318 store the head of the blocks in *F1 and *F2. */
1321 flow_find_cross_jump (basic_block bb1, basic_block bb2, rtx_insn **f1,
1322 rtx_insn **f2, enum replace_direction *dir_p)
1324 rtx_insn *i1, *i2, *last1, *last2, *afterlast1, *afterlast2;
1325 int ninsns = 0;
1326 enum replace_direction dir, last_dir, afterlast_dir;
1327 bool follow_fallthru, did_fallthru;
1329 if (dir_p)
1330 dir = *dir_p;
1331 else
1332 dir = dir_both;
1333 afterlast_dir = dir;
1334 last_dir = afterlast_dir;
1336 /* Skip simple jumps at the end of the blocks. Complex jumps still
1337 need to be compared for equivalence, which we'll do below. */
1339 i1 = BB_END (bb1);
1340 last1 = afterlast1 = last2 = afterlast2 = NULL;
1341 if (onlyjump_p (i1)
1342 || (returnjump_p (i1) && !side_effects_p (PATTERN (i1))))
1344 last1 = i1;
1345 i1 = PREV_INSN (i1);
1348 i2 = BB_END (bb2);
1349 if (onlyjump_p (i2)
1350 || (returnjump_p (i2) && !side_effects_p (PATTERN (i2))))
1352 last2 = i2;
1353 /* Count everything except for unconditional jump as insn.
1354 Don't count any jumps if dir_p is NULL. */
1355 if (!simplejump_p (i2) && !returnjump_p (i2) && last1 && dir_p)
1356 ninsns++;
1357 i2 = PREV_INSN (i2);
1360 while (true)
1362 /* In the following example, we can replace all jumps to C by jumps to A.
1364 This removes 4 duplicate insns.
1365 [bb A] insn1 [bb C] insn1
1366 insn2 insn2
1367 [bb B] insn3 insn3
1368 insn4 insn4
1369 jump_insn jump_insn
1371 We could also replace all jumps to A by jumps to C, but that leaves B
1372 alive, and removes only 2 duplicate insns. In a subsequent crossjump
1373 step, all jumps to B would be replaced with jumps to the middle of C,
1374 achieving the same result with more effort.
1375 So we allow only the first possibility, which means that we don't allow
1376 fallthru in the block that's being replaced. */
1378 follow_fallthru = dir_p && dir != dir_forward;
1379 walk_to_nondebug_insn (&i1, &bb1, follow_fallthru, &did_fallthru);
1380 if (did_fallthru)
1381 dir = dir_backward;
1383 follow_fallthru = dir_p && dir != dir_backward;
1384 walk_to_nondebug_insn (&i2, &bb2, follow_fallthru, &did_fallthru);
1385 if (did_fallthru)
1386 dir = dir_forward;
1388 if (i1 == BB_HEAD (bb1) || i2 == BB_HEAD (bb2))
1389 break;
1391 dir = merge_dir (dir, old_insns_match_p (0, i1, i2));
1392 if (dir == dir_none || (!dir_p && dir != dir_both))
1393 break;
1395 merge_memattrs (i1, i2);
1397 /* Don't begin a cross-jump with a NOTE insn. */
1398 if (INSN_P (i1))
1400 merge_notes (i1, i2);
1402 afterlast1 = last1, afterlast2 = last2;
1403 last1 = i1, last2 = i2;
1404 afterlast_dir = last_dir;
1405 last_dir = dir;
1406 if (active_insn_p (i1))
1407 ninsns++;
1410 i1 = PREV_INSN (i1);
1411 i2 = PREV_INSN (i2);
1414 #ifdef HAVE_cc0
1415 /* Don't allow the insn after a compare to be shared by
1416 cross-jumping unless the compare is also shared. */
1417 if (ninsns && reg_mentioned_p (cc0_rtx, last1) && ! sets_cc0_p (last1))
1418 last1 = afterlast1, last2 = afterlast2, last_dir = afterlast_dir, ninsns--;
1419 #endif
1421 /* Include preceding notes and labels in the cross-jump. One,
1422 this may bring us to the head of the blocks as requested above.
1423 Two, it keeps line number notes as matched as may be. */
1424 if (ninsns)
1426 bb1 = BLOCK_FOR_INSN (last1);
1427 while (last1 != BB_HEAD (bb1) && !NONDEBUG_INSN_P (PREV_INSN (last1)))
1428 last1 = PREV_INSN (last1);
1430 if (last1 != BB_HEAD (bb1) && LABEL_P (PREV_INSN (last1)))
1431 last1 = PREV_INSN (last1);
1433 bb2 = BLOCK_FOR_INSN (last2);
1434 while (last2 != BB_HEAD (bb2) && !NONDEBUG_INSN_P (PREV_INSN (last2)))
1435 last2 = PREV_INSN (last2);
1437 if (last2 != BB_HEAD (bb2) && LABEL_P (PREV_INSN (last2)))
1438 last2 = PREV_INSN (last2);
1440 *f1 = last1;
1441 *f2 = last2;
1444 if (dir_p)
1445 *dir_p = last_dir;
1446 return ninsns;
1449 /* Like flow_find_cross_jump, except start looking for a matching sequence from
1450 the head of the two blocks. Do not include jumps at the end.
1451 If STOP_AFTER is nonzero, stop after finding that many matching
1452 instructions. If STOP_AFTER is zero, count all INSN_P insns, if it is
1453 non-zero, only count active insns. */
1456 flow_find_head_matching_sequence (basic_block bb1, basic_block bb2, rtx_insn **f1,
1457 rtx_insn **f2, int stop_after)
1459 rtx_insn *i1, *i2, *last1, *last2, *beforelast1, *beforelast2;
1460 int ninsns = 0;
1461 edge e;
1462 edge_iterator ei;
1463 int nehedges1 = 0, nehedges2 = 0;
1465 FOR_EACH_EDGE (e, ei, bb1->succs)
1466 if (e->flags & EDGE_EH)
1467 nehedges1++;
1468 FOR_EACH_EDGE (e, ei, bb2->succs)
1469 if (e->flags & EDGE_EH)
1470 nehedges2++;
1472 i1 = BB_HEAD (bb1);
1473 i2 = BB_HEAD (bb2);
1474 last1 = beforelast1 = last2 = beforelast2 = NULL;
1476 while (true)
1478 /* Ignore notes, except NOTE_INSN_EPILOGUE_BEG. */
1479 while (!NONDEBUG_INSN_P (i1) && i1 != BB_END (bb1))
1481 if (NOTE_P (i1) && NOTE_KIND (i1) == NOTE_INSN_EPILOGUE_BEG)
1482 break;
1483 i1 = NEXT_INSN (i1);
1486 while (!NONDEBUG_INSN_P (i2) && i2 != BB_END (bb2))
1488 if (NOTE_P (i2) && NOTE_KIND (i2) == NOTE_INSN_EPILOGUE_BEG)
1489 break;
1490 i2 = NEXT_INSN (i2);
1493 if ((i1 == BB_END (bb1) && !NONDEBUG_INSN_P (i1))
1494 || (i2 == BB_END (bb2) && !NONDEBUG_INSN_P (i2)))
1495 break;
1497 if (NOTE_P (i1) || NOTE_P (i2)
1498 || JUMP_P (i1) || JUMP_P (i2))
1499 break;
1501 /* A sanity check to make sure we're not merging insns with different
1502 effects on EH. If only one of them ends a basic block, it shouldn't
1503 have an EH edge; if both end a basic block, there should be the same
1504 number of EH edges. */
1505 if ((i1 == BB_END (bb1) && i2 != BB_END (bb2)
1506 && nehedges1 > 0)
1507 || (i2 == BB_END (bb2) && i1 != BB_END (bb1)
1508 && nehedges2 > 0)
1509 || (i1 == BB_END (bb1) && i2 == BB_END (bb2)
1510 && nehedges1 != nehedges2))
1511 break;
1513 if (old_insns_match_p (0, i1, i2) != dir_both)
1514 break;
1516 merge_memattrs (i1, i2);
1518 /* Don't begin a cross-jump with a NOTE insn. */
1519 if (INSN_P (i1))
1521 merge_notes (i1, i2);
1523 beforelast1 = last1, beforelast2 = last2;
1524 last1 = i1, last2 = i2;
1525 if (!stop_after || active_insn_p (i1))
1526 ninsns++;
1529 if (i1 == BB_END (bb1) || i2 == BB_END (bb2)
1530 || (stop_after > 0 && ninsns == stop_after))
1531 break;
1533 i1 = NEXT_INSN (i1);
1534 i2 = NEXT_INSN (i2);
1537 #ifdef HAVE_cc0
1538 /* Don't allow a compare to be shared by cross-jumping unless the insn
1539 after the compare is also shared. */
1540 if (ninsns && reg_mentioned_p (cc0_rtx, last1) && sets_cc0_p (last1))
1541 last1 = beforelast1, last2 = beforelast2, ninsns--;
1542 #endif
1544 if (ninsns)
1546 *f1 = last1;
1547 *f2 = last2;
1550 return ninsns;
1553 /* Return true iff outgoing edges of BB1 and BB2 match, together with
1554 the branch instruction. This means that if we commonize the control
1555 flow before end of the basic block, the semantic remains unchanged.
1557 We may assume that there exists one edge with a common destination. */
1559 static bool
1560 outgoing_edges_match (int mode, basic_block bb1, basic_block bb2)
1562 int nehedges1 = 0, nehedges2 = 0;
1563 edge fallthru1 = 0, fallthru2 = 0;
1564 edge e1, e2;
1565 edge_iterator ei;
1567 /* If we performed shrink-wrapping, edges to the exit block can
1568 only be distinguished for JUMP_INSNs. The two paths may differ in
1569 whether they went through the prologue. Sibcalls are fine, we know
1570 that we either didn't need or inserted an epilogue before them. */
1571 if (crtl->shrink_wrapped
1572 && single_succ_p (bb1)
1573 && single_succ (bb1) == EXIT_BLOCK_PTR_FOR_FN (cfun)
1574 && !JUMP_P (BB_END (bb1))
1575 && !(CALL_P (BB_END (bb1)) && SIBLING_CALL_P (BB_END (bb1))))
1576 return false;
1578 /* If BB1 has only one successor, we may be looking at either an
1579 unconditional jump, or a fake edge to exit. */
1580 if (single_succ_p (bb1)
1581 && (single_succ_edge (bb1)->flags & (EDGE_COMPLEX | EDGE_FAKE)) == 0
1582 && (!JUMP_P (BB_END (bb1)) || simplejump_p (BB_END (bb1))))
1583 return (single_succ_p (bb2)
1584 && (single_succ_edge (bb2)->flags
1585 & (EDGE_COMPLEX | EDGE_FAKE)) == 0
1586 && (!JUMP_P (BB_END (bb2)) || simplejump_p (BB_END (bb2))));
1588 /* Match conditional jumps - this may get tricky when fallthru and branch
1589 edges are crossed. */
1590 if (EDGE_COUNT (bb1->succs) == 2
1591 && any_condjump_p (BB_END (bb1))
1592 && onlyjump_p (BB_END (bb1)))
1594 edge b1, f1, b2, f2;
1595 bool reverse, match;
1596 rtx set1, set2, cond1, cond2;
1597 enum rtx_code code1, code2;
1599 if (EDGE_COUNT (bb2->succs) != 2
1600 || !any_condjump_p (BB_END (bb2))
1601 || !onlyjump_p (BB_END (bb2)))
1602 return false;
1604 b1 = BRANCH_EDGE (bb1);
1605 b2 = BRANCH_EDGE (bb2);
1606 f1 = FALLTHRU_EDGE (bb1);
1607 f2 = FALLTHRU_EDGE (bb2);
1609 /* Get around possible forwarders on fallthru edges. Other cases
1610 should be optimized out already. */
1611 if (FORWARDER_BLOCK_P (f1->dest))
1612 f1 = single_succ_edge (f1->dest);
1614 if (FORWARDER_BLOCK_P (f2->dest))
1615 f2 = single_succ_edge (f2->dest);
1617 /* To simplify use of this function, return false if there are
1618 unneeded forwarder blocks. These will get eliminated later
1619 during cleanup_cfg. */
1620 if (FORWARDER_BLOCK_P (f1->dest)
1621 || FORWARDER_BLOCK_P (f2->dest)
1622 || FORWARDER_BLOCK_P (b1->dest)
1623 || FORWARDER_BLOCK_P (b2->dest))
1624 return false;
1626 if (f1->dest == f2->dest && b1->dest == b2->dest)
1627 reverse = false;
1628 else if (f1->dest == b2->dest && b1->dest == f2->dest)
1629 reverse = true;
1630 else
1631 return false;
1633 set1 = pc_set (BB_END (bb1));
1634 set2 = pc_set (BB_END (bb2));
1635 if ((XEXP (SET_SRC (set1), 1) == pc_rtx)
1636 != (XEXP (SET_SRC (set2), 1) == pc_rtx))
1637 reverse = !reverse;
1639 cond1 = XEXP (SET_SRC (set1), 0);
1640 cond2 = XEXP (SET_SRC (set2), 0);
1641 code1 = GET_CODE (cond1);
1642 if (reverse)
1643 code2 = reversed_comparison_code (cond2, BB_END (bb2));
1644 else
1645 code2 = GET_CODE (cond2);
1647 if (code2 == UNKNOWN)
1648 return false;
1650 /* Verify codes and operands match. */
1651 match = ((code1 == code2
1652 && rtx_renumbered_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
1653 && rtx_renumbered_equal_p (XEXP (cond1, 1), XEXP (cond2, 1)))
1654 || (code1 == swap_condition (code2)
1655 && rtx_renumbered_equal_p (XEXP (cond1, 1),
1656 XEXP (cond2, 0))
1657 && rtx_renumbered_equal_p (XEXP (cond1, 0),
1658 XEXP (cond2, 1))));
1660 /* If we return true, we will join the blocks. Which means that
1661 we will only have one branch prediction bit to work with. Thus
1662 we require the existing branches to have probabilities that are
1663 roughly similar. */
1664 if (match
1665 && optimize_bb_for_speed_p (bb1)
1666 && optimize_bb_for_speed_p (bb2))
1668 int prob2;
1670 if (b1->dest == b2->dest)
1671 prob2 = b2->probability;
1672 else
1673 /* Do not use f2 probability as f2 may be forwarded. */
1674 prob2 = REG_BR_PROB_BASE - b2->probability;
1676 /* Fail if the difference in probabilities is greater than 50%.
1677 This rules out two well-predicted branches with opposite
1678 outcomes. */
1679 if (abs (b1->probability - prob2) > REG_BR_PROB_BASE / 2)
1681 if (dump_file)
1682 fprintf (dump_file,
1683 "Outcomes of branch in bb %i and %i differ too much (%i %i)\n",
1684 bb1->index, bb2->index, b1->probability, prob2);
1686 return false;
1690 if (dump_file && match)
1691 fprintf (dump_file, "Conditionals in bb %i and %i match.\n",
1692 bb1->index, bb2->index);
1694 return match;
1697 /* Generic case - we are seeing a computed jump, table jump or trapping
1698 instruction. */
1700 /* Check whether there are tablejumps in the end of BB1 and BB2.
1701 Return true if they are identical. */
1703 rtx label1, label2;
1704 rtx_jump_table_data *table1, *table2;
1706 if (tablejump_p (BB_END (bb1), &label1, &table1)
1707 && tablejump_p (BB_END (bb2), &label2, &table2)
1708 && GET_CODE (PATTERN (table1)) == GET_CODE (PATTERN (table2)))
1710 /* The labels should never be the same rtx. If they really are same
1711 the jump tables are same too. So disable crossjumping of blocks BB1
1712 and BB2 because when deleting the common insns in the end of BB1
1713 by delete_basic_block () the jump table would be deleted too. */
1714 /* If LABEL2 is referenced in BB1->END do not do anything
1715 because we would loose information when replacing
1716 LABEL1 by LABEL2 and then LABEL2 by LABEL1 in BB1->END. */
1717 if (label1 != label2 && !rtx_referenced_p (label2, BB_END (bb1)))
1719 /* Set IDENTICAL to true when the tables are identical. */
1720 bool identical = false;
1721 rtx p1, p2;
1723 p1 = PATTERN (table1);
1724 p2 = PATTERN (table2);
1725 if (GET_CODE (p1) == ADDR_VEC && rtx_equal_p (p1, p2))
1727 identical = true;
1729 else if (GET_CODE (p1) == ADDR_DIFF_VEC
1730 && (XVECLEN (p1, 1) == XVECLEN (p2, 1))
1731 && rtx_equal_p (XEXP (p1, 2), XEXP (p2, 2))
1732 && rtx_equal_p (XEXP (p1, 3), XEXP (p2, 3)))
1734 int i;
1736 identical = true;
1737 for (i = XVECLEN (p1, 1) - 1; i >= 0 && identical; i--)
1738 if (!rtx_equal_p (XVECEXP (p1, 1, i), XVECEXP (p2, 1, i)))
1739 identical = false;
1742 if (identical)
1744 bool match;
1746 /* Temporarily replace references to LABEL1 with LABEL2
1747 in BB1->END so that we could compare the instructions. */
1748 replace_label_in_insn (BB_END (bb1), label1, label2, false);
1750 match = (old_insns_match_p (mode, BB_END (bb1), BB_END (bb2))
1751 == dir_both);
1752 if (dump_file && match)
1753 fprintf (dump_file,
1754 "Tablejumps in bb %i and %i match.\n",
1755 bb1->index, bb2->index);
1757 /* Set the original label in BB1->END because when deleting
1758 a block whose end is a tablejump, the tablejump referenced
1759 from the instruction is deleted too. */
1760 replace_label_in_insn (BB_END (bb1), label2, label1, false);
1762 return match;
1765 return false;
1769 /* Find the last non-debug non-note instruction in each bb, except
1770 stop when we see the NOTE_INSN_BASIC_BLOCK, as old_insns_match_p
1771 handles that case specially. old_insns_match_p does not handle
1772 other types of instruction notes. */
1773 rtx_insn *last1 = BB_END (bb1);
1774 rtx_insn *last2 = BB_END (bb2);
1775 while (!NOTE_INSN_BASIC_BLOCK_P (last1) &&
1776 (DEBUG_INSN_P (last1) || NOTE_P (last1)))
1777 last1 = PREV_INSN (last1);
1778 while (!NOTE_INSN_BASIC_BLOCK_P (last2) &&
1779 (DEBUG_INSN_P (last2) || NOTE_P (last2)))
1780 last2 = PREV_INSN (last2);
1781 gcc_assert (last1 && last2);
1783 /* First ensure that the instructions match. There may be many outgoing
1784 edges so this test is generally cheaper. */
1785 if (old_insns_match_p (mode, last1, last2) != dir_both)
1786 return false;
1788 /* Search the outgoing edges, ensure that the counts do match, find possible
1789 fallthru and exception handling edges since these needs more
1790 validation. */
1791 if (EDGE_COUNT (bb1->succs) != EDGE_COUNT (bb2->succs))
1792 return false;
1794 bool nonfakeedges = false;
1795 FOR_EACH_EDGE (e1, ei, bb1->succs)
1797 e2 = EDGE_SUCC (bb2, ei.index);
1799 if ((e1->flags & EDGE_FAKE) == 0)
1800 nonfakeedges = true;
1802 if (e1->flags & EDGE_EH)
1803 nehedges1++;
1805 if (e2->flags & EDGE_EH)
1806 nehedges2++;
1808 if (e1->flags & EDGE_FALLTHRU)
1809 fallthru1 = e1;
1810 if (e2->flags & EDGE_FALLTHRU)
1811 fallthru2 = e2;
1814 /* If number of edges of various types does not match, fail. */
1815 if (nehedges1 != nehedges2
1816 || (fallthru1 != 0) != (fallthru2 != 0))
1817 return false;
1819 /* If !ACCUMULATE_OUTGOING_ARGS, bb1 (and bb2) have no successors
1820 and the last real insn doesn't have REG_ARGS_SIZE note, don't
1821 attempt to optimize, as the two basic blocks might have different
1822 REG_ARGS_SIZE depths. For noreturn calls and unconditional
1823 traps there should be REG_ARG_SIZE notes, they could be missing
1824 for __builtin_unreachable () uses though. */
1825 if (!nonfakeedges
1826 && !ACCUMULATE_OUTGOING_ARGS
1827 && (!INSN_P (last1)
1828 || !find_reg_note (last1, REG_ARGS_SIZE, NULL)))
1829 return false;
1831 /* fallthru edges must be forwarded to the same destination. */
1832 if (fallthru1)
1834 basic_block d1 = (forwarder_block_p (fallthru1->dest)
1835 ? single_succ (fallthru1->dest): fallthru1->dest);
1836 basic_block d2 = (forwarder_block_p (fallthru2->dest)
1837 ? single_succ (fallthru2->dest): fallthru2->dest);
1839 if (d1 != d2)
1840 return false;
1843 /* Ensure the same EH region. */
1845 rtx n1 = find_reg_note (BB_END (bb1), REG_EH_REGION, 0);
1846 rtx n2 = find_reg_note (BB_END (bb2), REG_EH_REGION, 0);
1848 if (!n1 && n2)
1849 return false;
1851 if (n1 && (!n2 || XEXP (n1, 0) != XEXP (n2, 0)))
1852 return false;
1855 /* The same checks as in try_crossjump_to_edge. It is required for RTL
1856 version of sequence abstraction. */
1857 FOR_EACH_EDGE (e1, ei, bb2->succs)
1859 edge e2;
1860 edge_iterator ei;
1861 basic_block d1 = e1->dest;
1863 if (FORWARDER_BLOCK_P (d1))
1864 d1 = EDGE_SUCC (d1, 0)->dest;
1866 FOR_EACH_EDGE (e2, ei, bb1->succs)
1868 basic_block d2 = e2->dest;
1869 if (FORWARDER_BLOCK_P (d2))
1870 d2 = EDGE_SUCC (d2, 0)->dest;
1871 if (d1 == d2)
1872 break;
1875 if (!e2)
1876 return false;
1879 return true;
1882 /* Returns true if BB basic block has a preserve label. */
1884 static bool
1885 block_has_preserve_label (basic_block bb)
1887 return (bb
1888 && block_label (bb)
1889 && LABEL_PRESERVE_P (block_label (bb)));
1892 /* E1 and E2 are edges with the same destination block. Search their
1893 predecessors for common code. If found, redirect control flow from
1894 (maybe the middle of) E1->SRC to (maybe the middle of) E2->SRC (dir_forward),
1895 or the other way around (dir_backward). DIR specifies the allowed
1896 replacement direction. */
1898 static bool
1899 try_crossjump_to_edge (int mode, edge e1, edge e2,
1900 enum replace_direction dir)
1902 int nmatch;
1903 basic_block src1 = e1->src, src2 = e2->src;
1904 basic_block redirect_to, redirect_from, to_remove;
1905 basic_block osrc1, osrc2, redirect_edges_to, tmp;
1906 rtx_insn *newpos1, *newpos2;
1907 edge s;
1908 edge_iterator ei;
1910 newpos1 = newpos2 = NULL;
1912 /* If we have partitioned hot/cold basic blocks, it is a bad idea
1913 to try this optimization.
1915 Basic block partitioning may result in some jumps that appear to
1916 be optimizable (or blocks that appear to be mergeable), but which really
1917 must be left untouched (they are required to make it safely across
1918 partition boundaries). See the comments at the top of
1919 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
1921 if (crtl->has_bb_partition && reload_completed)
1922 return false;
1924 /* Search backward through forwarder blocks. We don't need to worry
1925 about multiple entry or chained forwarders, as they will be optimized
1926 away. We do this to look past the unconditional jump following a
1927 conditional jump that is required due to the current CFG shape. */
1928 if (single_pred_p (src1)
1929 && FORWARDER_BLOCK_P (src1))
1930 e1 = single_pred_edge (src1), src1 = e1->src;
1932 if (single_pred_p (src2)
1933 && FORWARDER_BLOCK_P (src2))
1934 e2 = single_pred_edge (src2), src2 = e2->src;
1936 /* Nothing to do if we reach ENTRY, or a common source block. */
1937 if (src1 == ENTRY_BLOCK_PTR_FOR_FN (cfun) || src2
1938 == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1939 return false;
1940 if (src1 == src2)
1941 return false;
1943 /* Seeing more than 1 forwarder blocks would confuse us later... */
1944 if (FORWARDER_BLOCK_P (e1->dest)
1945 && FORWARDER_BLOCK_P (single_succ (e1->dest)))
1946 return false;
1948 if (FORWARDER_BLOCK_P (e2->dest)
1949 && FORWARDER_BLOCK_P (single_succ (e2->dest)))
1950 return false;
1952 /* Likewise with dead code (possibly newly created by the other optimizations
1953 of cfg_cleanup). */
1954 if (EDGE_COUNT (src1->preds) == 0 || EDGE_COUNT (src2->preds) == 0)
1955 return false;
1957 /* Look for the common insn sequence, part the first ... */
1958 if (!outgoing_edges_match (mode, src1, src2))
1959 return false;
1961 /* ... and part the second. */
1962 nmatch = flow_find_cross_jump (src1, src2, &newpos1, &newpos2, &dir);
1964 osrc1 = src1;
1965 osrc2 = src2;
1966 if (newpos1 != NULL_RTX)
1967 src1 = BLOCK_FOR_INSN (newpos1);
1968 if (newpos2 != NULL_RTX)
1969 src2 = BLOCK_FOR_INSN (newpos2);
1971 if (dir == dir_backward)
1973 #define SWAP(T, X, Y) do { T tmp = (X); (X) = (Y); (Y) = tmp; } while (0)
1974 SWAP (basic_block, osrc1, osrc2);
1975 SWAP (basic_block, src1, src2);
1976 SWAP (edge, e1, e2);
1977 SWAP (rtx_insn *, newpos1, newpos2);
1978 #undef SWAP
1981 /* Don't proceed with the crossjump unless we found a sufficient number
1982 of matching instructions or the 'from' block was totally matched
1983 (such that its predecessors will hopefully be redirected and the
1984 block removed). */
1985 if ((nmatch < PARAM_VALUE (PARAM_MIN_CROSSJUMP_INSNS))
1986 && (newpos1 != BB_HEAD (src1)))
1987 return false;
1989 /* Avoid deleting preserve label when redirecting ABNORMAL edges. */
1990 if (block_has_preserve_label (e1->dest)
1991 && (e1->flags & EDGE_ABNORMAL))
1992 return false;
1994 /* Here we know that the insns in the end of SRC1 which are common with SRC2
1995 will be deleted.
1996 If we have tablejumps in the end of SRC1 and SRC2
1997 they have been already compared for equivalence in outgoing_edges_match ()
1998 so replace the references to TABLE1 by references to TABLE2. */
2000 rtx label1, label2;
2001 rtx_jump_table_data *table1, *table2;
2003 if (tablejump_p (BB_END (osrc1), &label1, &table1)
2004 && tablejump_p (BB_END (osrc2), &label2, &table2)
2005 && label1 != label2)
2007 rtx_insn *insn;
2009 /* Replace references to LABEL1 with LABEL2. */
2010 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2012 /* Do not replace the label in SRC1->END because when deleting
2013 a block whose end is a tablejump, the tablejump referenced
2014 from the instruction is deleted too. */
2015 if (insn != BB_END (osrc1))
2016 replace_label_in_insn (insn, label1, label2, true);
2021 /* Avoid splitting if possible. We must always split when SRC2 has
2022 EH predecessor edges, or we may end up with basic blocks with both
2023 normal and EH predecessor edges. */
2024 if (newpos2 == BB_HEAD (src2)
2025 && !(EDGE_PRED (src2, 0)->flags & EDGE_EH))
2026 redirect_to = src2;
2027 else
2029 if (newpos2 == BB_HEAD (src2))
2031 /* Skip possible basic block header. */
2032 if (LABEL_P (newpos2))
2033 newpos2 = NEXT_INSN (newpos2);
2034 while (DEBUG_INSN_P (newpos2))
2035 newpos2 = NEXT_INSN (newpos2);
2036 if (NOTE_P (newpos2))
2037 newpos2 = NEXT_INSN (newpos2);
2038 while (DEBUG_INSN_P (newpos2))
2039 newpos2 = NEXT_INSN (newpos2);
2042 if (dump_file)
2043 fprintf (dump_file, "Splitting bb %i before %i insns\n",
2044 src2->index, nmatch);
2045 redirect_to = split_block (src2, PREV_INSN (newpos2))->dest;
2048 if (dump_file)
2049 fprintf (dump_file,
2050 "Cross jumping from bb %i to bb %i; %i common insns\n",
2051 src1->index, src2->index, nmatch);
2053 /* We may have some registers visible through the block. */
2054 df_set_bb_dirty (redirect_to);
2056 if (osrc2 == src2)
2057 redirect_edges_to = redirect_to;
2058 else
2059 redirect_edges_to = osrc2;
2061 /* Recompute the frequencies and counts of outgoing edges. */
2062 FOR_EACH_EDGE (s, ei, redirect_edges_to->succs)
2064 edge s2;
2065 edge_iterator ei;
2066 basic_block d = s->dest;
2068 if (FORWARDER_BLOCK_P (d))
2069 d = single_succ (d);
2071 FOR_EACH_EDGE (s2, ei, src1->succs)
2073 basic_block d2 = s2->dest;
2074 if (FORWARDER_BLOCK_P (d2))
2075 d2 = single_succ (d2);
2076 if (d == d2)
2077 break;
2080 s->count += s2->count;
2082 /* Take care to update possible forwarder blocks. We verified
2083 that there is no more than one in the chain, so we can't run
2084 into infinite loop. */
2085 if (FORWARDER_BLOCK_P (s->dest))
2087 single_succ_edge (s->dest)->count += s2->count;
2088 s->dest->count += s2->count;
2089 s->dest->frequency += EDGE_FREQUENCY (s);
2092 if (FORWARDER_BLOCK_P (s2->dest))
2094 single_succ_edge (s2->dest)->count -= s2->count;
2095 if (single_succ_edge (s2->dest)->count < 0)
2096 single_succ_edge (s2->dest)->count = 0;
2097 s2->dest->count -= s2->count;
2098 s2->dest->frequency -= EDGE_FREQUENCY (s);
2099 if (s2->dest->frequency < 0)
2100 s2->dest->frequency = 0;
2101 if (s2->dest->count < 0)
2102 s2->dest->count = 0;
2105 if (!redirect_edges_to->frequency && !src1->frequency)
2106 s->probability = (s->probability + s2->probability) / 2;
2107 else
2108 s->probability
2109 = ((s->probability * redirect_edges_to->frequency +
2110 s2->probability * src1->frequency)
2111 / (redirect_edges_to->frequency + src1->frequency));
2114 /* Adjust count and frequency for the block. An earlier jump
2115 threading pass may have left the profile in an inconsistent
2116 state (see update_bb_profile_for_threading) so we must be
2117 prepared for overflows. */
2118 tmp = redirect_to;
2121 tmp->count += src1->count;
2122 tmp->frequency += src1->frequency;
2123 if (tmp->frequency > BB_FREQ_MAX)
2124 tmp->frequency = BB_FREQ_MAX;
2125 if (tmp == redirect_edges_to)
2126 break;
2127 tmp = find_fallthru_edge (tmp->succs)->dest;
2129 while (true);
2130 update_br_prob_note (redirect_edges_to);
2132 /* Edit SRC1 to go to REDIRECT_TO at NEWPOS1. */
2134 /* Skip possible basic block header. */
2135 if (LABEL_P (newpos1))
2136 newpos1 = NEXT_INSN (newpos1);
2138 while (DEBUG_INSN_P (newpos1))
2139 newpos1 = NEXT_INSN (newpos1);
2141 if (NOTE_INSN_BASIC_BLOCK_P (newpos1))
2142 newpos1 = NEXT_INSN (newpos1);
2144 while (DEBUG_INSN_P (newpos1))
2145 newpos1 = NEXT_INSN (newpos1);
2147 redirect_from = split_block (src1, PREV_INSN (newpos1))->src;
2148 to_remove = single_succ (redirect_from);
2150 redirect_edge_and_branch_force (single_succ_edge (redirect_from), redirect_to);
2151 delete_basic_block (to_remove);
2153 update_forwarder_flag (redirect_from);
2154 if (redirect_to != src2)
2155 update_forwarder_flag (src2);
2157 return true;
2160 /* Search the predecessors of BB for common insn sequences. When found,
2161 share code between them by redirecting control flow. Return true if
2162 any changes made. */
2164 static bool
2165 try_crossjump_bb (int mode, basic_block bb)
2167 edge e, e2, fallthru;
2168 bool changed;
2169 unsigned max, ix, ix2;
2171 /* Nothing to do if there is not at least two incoming edges. */
2172 if (EDGE_COUNT (bb->preds) < 2)
2173 return false;
2175 /* Don't crossjump if this block ends in a computed jump,
2176 unless we are optimizing for size. */
2177 if (optimize_bb_for_size_p (bb)
2178 && bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
2179 && computed_jump_p (BB_END (bb)))
2180 return false;
2182 /* If we are partitioning hot/cold basic blocks, we don't want to
2183 mess up unconditional or indirect jumps that cross between hot
2184 and cold sections.
2186 Basic block partitioning may result in some jumps that appear to
2187 be optimizable (or blocks that appear to be mergeable), but which really
2188 must be left untouched (they are required to make it safely across
2189 partition boundaries). See the comments at the top of
2190 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
2192 if (BB_PARTITION (EDGE_PRED (bb, 0)->src) !=
2193 BB_PARTITION (EDGE_PRED (bb, 1)->src)
2194 || (EDGE_PRED (bb, 0)->flags & EDGE_CROSSING))
2195 return false;
2197 /* It is always cheapest to redirect a block that ends in a branch to
2198 a block that falls through into BB, as that adds no branches to the
2199 program. We'll try that combination first. */
2200 fallthru = NULL;
2201 max = PARAM_VALUE (PARAM_MAX_CROSSJUMP_EDGES);
2203 if (EDGE_COUNT (bb->preds) > max)
2204 return false;
2206 fallthru = find_fallthru_edge (bb->preds);
2208 changed = false;
2209 for (ix = 0; ix < EDGE_COUNT (bb->preds);)
2211 e = EDGE_PRED (bb, ix);
2212 ix++;
2214 /* As noted above, first try with the fallthru predecessor (or, a
2215 fallthru predecessor if we are in cfglayout mode). */
2216 if (fallthru)
2218 /* Don't combine the fallthru edge into anything else.
2219 If there is a match, we'll do it the other way around. */
2220 if (e == fallthru)
2221 continue;
2222 /* If nothing changed since the last attempt, there is nothing
2223 we can do. */
2224 if (!first_pass
2225 && !((e->src->flags & BB_MODIFIED)
2226 || (fallthru->src->flags & BB_MODIFIED)))
2227 continue;
2229 if (try_crossjump_to_edge (mode, e, fallthru, dir_forward))
2231 changed = true;
2232 ix = 0;
2233 continue;
2237 /* Non-obvious work limiting check: Recognize that we're going
2238 to call try_crossjump_bb on every basic block. So if we have
2239 two blocks with lots of outgoing edges (a switch) and they
2240 share lots of common destinations, then we would do the
2241 cross-jump check once for each common destination.
2243 Now, if the blocks actually are cross-jump candidates, then
2244 all of their destinations will be shared. Which means that
2245 we only need check them for cross-jump candidacy once. We
2246 can eliminate redundant checks of crossjump(A,B) by arbitrarily
2247 choosing to do the check from the block for which the edge
2248 in question is the first successor of A. */
2249 if (EDGE_SUCC (e->src, 0) != e)
2250 continue;
2252 for (ix2 = 0; ix2 < EDGE_COUNT (bb->preds); ix2++)
2254 e2 = EDGE_PRED (bb, ix2);
2256 if (e2 == e)
2257 continue;
2259 /* We've already checked the fallthru edge above. */
2260 if (e2 == fallthru)
2261 continue;
2263 /* The "first successor" check above only prevents multiple
2264 checks of crossjump(A,B). In order to prevent redundant
2265 checks of crossjump(B,A), require that A be the block
2266 with the lowest index. */
2267 if (e->src->index > e2->src->index)
2268 continue;
2270 /* If nothing changed since the last attempt, there is nothing
2271 we can do. */
2272 if (!first_pass
2273 && !((e->src->flags & BB_MODIFIED)
2274 || (e2->src->flags & BB_MODIFIED)))
2275 continue;
2277 /* Both e and e2 are not fallthru edges, so we can crossjump in either
2278 direction. */
2279 if (try_crossjump_to_edge (mode, e, e2, dir_both))
2281 changed = true;
2282 ix = 0;
2283 break;
2288 if (changed)
2289 crossjumps_occured = true;
2291 return changed;
2294 /* Search the successors of BB for common insn sequences. When found,
2295 share code between them by moving it across the basic block
2296 boundary. Return true if any changes made. */
2298 static bool
2299 try_head_merge_bb (basic_block bb)
2301 basic_block final_dest_bb = NULL;
2302 int max_match = INT_MAX;
2303 edge e0;
2304 rtx_insn **headptr, **currptr, **nextptr;
2305 bool changed, moveall;
2306 unsigned ix;
2307 rtx_insn *e0_last_head;
2308 rtx cond;
2309 rtx_insn *move_before;
2310 unsigned nedges = EDGE_COUNT (bb->succs);
2311 rtx_insn *jump = BB_END (bb);
2312 regset live, live_union;
2314 /* Nothing to do if there is not at least two outgoing edges. */
2315 if (nedges < 2)
2316 return false;
2318 /* Don't crossjump if this block ends in a computed jump,
2319 unless we are optimizing for size. */
2320 if (optimize_bb_for_size_p (bb)
2321 && bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
2322 && computed_jump_p (BB_END (bb)))
2323 return false;
2325 cond = get_condition (jump, &move_before, true, false);
2326 if (cond == NULL_RTX)
2328 #ifdef HAVE_cc0
2329 if (reg_mentioned_p (cc0_rtx, jump))
2330 move_before = prev_nonnote_nondebug_insn (jump);
2331 else
2332 #endif
2333 move_before = jump;
2336 for (ix = 0; ix < nedges; ix++)
2337 if (EDGE_SUCC (bb, ix)->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
2338 return false;
2340 for (ix = 0; ix < nedges; ix++)
2342 edge e = EDGE_SUCC (bb, ix);
2343 basic_block other_bb = e->dest;
2345 if (df_get_bb_dirty (other_bb))
2347 block_was_dirty = true;
2348 return false;
2351 if (e->flags & EDGE_ABNORMAL)
2352 return false;
2354 /* Normally, all destination blocks must only be reachable from this
2355 block, i.e. they must have one incoming edge.
2357 There is one special case we can handle, that of multiple consecutive
2358 jumps where the first jumps to one of the targets of the second jump.
2359 This happens frequently in switch statements for default labels.
2360 The structure is as follows:
2361 FINAL_DEST_BB
2362 ....
2363 if (cond) jump A;
2364 fall through
2366 jump with targets A, B, C, D...
2368 has two incoming edges, from FINAL_DEST_BB and BB
2370 In this case, we can try to move the insns through BB and into
2371 FINAL_DEST_BB. */
2372 if (EDGE_COUNT (other_bb->preds) != 1)
2374 edge incoming_edge, incoming_bb_other_edge;
2375 edge_iterator ei;
2377 if (final_dest_bb != NULL
2378 || EDGE_COUNT (other_bb->preds) != 2)
2379 return false;
2381 /* We must be able to move the insns across the whole block. */
2382 move_before = BB_HEAD (bb);
2383 while (!NONDEBUG_INSN_P (move_before))
2384 move_before = NEXT_INSN (move_before);
2386 if (EDGE_COUNT (bb->preds) != 1)
2387 return false;
2388 incoming_edge = EDGE_PRED (bb, 0);
2389 final_dest_bb = incoming_edge->src;
2390 if (EDGE_COUNT (final_dest_bb->succs) != 2)
2391 return false;
2392 FOR_EACH_EDGE (incoming_bb_other_edge, ei, final_dest_bb->succs)
2393 if (incoming_bb_other_edge != incoming_edge)
2394 break;
2395 if (incoming_bb_other_edge->dest != other_bb)
2396 return false;
2400 e0 = EDGE_SUCC (bb, 0);
2401 e0_last_head = NULL;
2402 changed = false;
2404 for (ix = 1; ix < nedges; ix++)
2406 edge e = EDGE_SUCC (bb, ix);
2407 rtx_insn *e0_last, *e_last;
2408 int nmatch;
2410 nmatch = flow_find_head_matching_sequence (e0->dest, e->dest,
2411 &e0_last, &e_last, 0);
2412 if (nmatch == 0)
2413 return false;
2415 if (nmatch < max_match)
2417 max_match = nmatch;
2418 e0_last_head = e0_last;
2422 /* If we matched an entire block, we probably have to avoid moving the
2423 last insn. */
2424 if (max_match > 0
2425 && e0_last_head == BB_END (e0->dest)
2426 && (find_reg_note (e0_last_head, REG_EH_REGION, 0)
2427 || control_flow_insn_p (e0_last_head)))
2429 max_match--;
2430 if (max_match == 0)
2431 return false;
2433 e0_last_head = prev_real_insn (e0_last_head);
2434 while (DEBUG_INSN_P (e0_last_head));
2437 if (max_match == 0)
2438 return false;
2440 /* We must find a union of the live registers at each of the end points. */
2441 live = BITMAP_ALLOC (NULL);
2442 live_union = BITMAP_ALLOC (NULL);
2444 currptr = XNEWVEC (rtx_insn *, nedges);
2445 headptr = XNEWVEC (rtx_insn *, nedges);
2446 nextptr = XNEWVEC (rtx_insn *, nedges);
2448 for (ix = 0; ix < nedges; ix++)
2450 int j;
2451 basic_block merge_bb = EDGE_SUCC (bb, ix)->dest;
2452 rtx_insn *head = BB_HEAD (merge_bb);
2454 while (!NONDEBUG_INSN_P (head))
2455 head = NEXT_INSN (head);
2456 headptr[ix] = head;
2457 currptr[ix] = head;
2459 /* Compute the end point and live information */
2460 for (j = 1; j < max_match; j++)
2462 head = NEXT_INSN (head);
2463 while (!NONDEBUG_INSN_P (head));
2464 simulate_backwards_to_point (merge_bb, live, head);
2465 IOR_REG_SET (live_union, live);
2468 /* If we're moving across two blocks, verify the validity of the
2469 first move, then adjust the target and let the loop below deal
2470 with the final move. */
2471 if (final_dest_bb != NULL)
2473 rtx_insn *move_upto;
2475 moveall = can_move_insns_across (currptr[0], e0_last_head, move_before,
2476 jump, e0->dest, live_union,
2477 NULL, &move_upto);
2478 if (!moveall)
2480 if (move_upto == NULL_RTX)
2481 goto out;
2483 while (e0_last_head != move_upto)
2485 df_simulate_one_insn_backwards (e0->dest, e0_last_head,
2486 live_union);
2487 e0_last_head = PREV_INSN (e0_last_head);
2490 if (e0_last_head == NULL_RTX)
2491 goto out;
2493 jump = BB_END (final_dest_bb);
2494 cond = get_condition (jump, &move_before, true, false);
2495 if (cond == NULL_RTX)
2497 #ifdef HAVE_cc0
2498 if (reg_mentioned_p (cc0_rtx, jump))
2499 move_before = prev_nonnote_nondebug_insn (jump);
2500 else
2501 #endif
2502 move_before = jump;
2508 rtx_insn *move_upto;
2509 moveall = can_move_insns_across (currptr[0], e0_last_head,
2510 move_before, jump, e0->dest, live_union,
2511 NULL, &move_upto);
2512 if (!moveall && move_upto == NULL_RTX)
2514 if (jump == move_before)
2515 break;
2517 /* Try again, using a different insertion point. */
2518 move_before = jump;
2520 #ifdef HAVE_cc0
2521 /* Don't try moving before a cc0 user, as that may invalidate
2522 the cc0. */
2523 if (reg_mentioned_p (cc0_rtx, jump))
2524 break;
2525 #endif
2527 continue;
2530 if (final_dest_bb && !moveall)
2531 /* We haven't checked whether a partial move would be OK for the first
2532 move, so we have to fail this case. */
2533 break;
2535 changed = true;
2536 for (;;)
2538 if (currptr[0] == move_upto)
2539 break;
2540 for (ix = 0; ix < nedges; ix++)
2542 rtx_insn *curr = currptr[ix];
2544 curr = NEXT_INSN (curr);
2545 while (!NONDEBUG_INSN_P (curr));
2546 currptr[ix] = curr;
2550 /* If we can't currently move all of the identical insns, remember
2551 each insn after the range that we'll merge. */
2552 if (!moveall)
2553 for (ix = 0; ix < nedges; ix++)
2555 rtx_insn *curr = currptr[ix];
2557 curr = NEXT_INSN (curr);
2558 while (!NONDEBUG_INSN_P (curr));
2559 nextptr[ix] = curr;
2562 reorder_insns (headptr[0], currptr[0], PREV_INSN (move_before));
2563 df_set_bb_dirty (EDGE_SUCC (bb, 0)->dest);
2564 if (final_dest_bb != NULL)
2565 df_set_bb_dirty (final_dest_bb);
2566 df_set_bb_dirty (bb);
2567 for (ix = 1; ix < nedges; ix++)
2569 df_set_bb_dirty (EDGE_SUCC (bb, ix)->dest);
2570 delete_insn_chain (headptr[ix], currptr[ix], false);
2572 if (!moveall)
2574 if (jump == move_before)
2575 break;
2577 /* For the unmerged insns, try a different insertion point. */
2578 move_before = jump;
2580 #ifdef HAVE_cc0
2581 /* Don't try moving before a cc0 user, as that may invalidate
2582 the cc0. */
2583 if (reg_mentioned_p (cc0_rtx, jump))
2584 break;
2585 #endif
2587 for (ix = 0; ix < nedges; ix++)
2588 currptr[ix] = headptr[ix] = nextptr[ix];
2591 while (!moveall);
2593 out:
2594 free (currptr);
2595 free (headptr);
2596 free (nextptr);
2598 crossjumps_occured |= changed;
2600 return changed;
2603 /* Return true if BB contains just bb note, or bb note followed
2604 by only DEBUG_INSNs. */
2606 static bool
2607 trivially_empty_bb_p (basic_block bb)
2609 rtx_insn *insn = BB_END (bb);
2611 while (1)
2613 if (insn == BB_HEAD (bb))
2614 return true;
2615 if (!DEBUG_INSN_P (insn))
2616 return false;
2617 insn = PREV_INSN (insn);
2621 /* Do simple CFG optimizations - basic block merging, simplifying of jump
2622 instructions etc. Return nonzero if changes were made. */
2624 static bool
2625 try_optimize_cfg (int mode)
2627 bool changed_overall = false;
2628 bool changed;
2629 int iterations = 0;
2630 basic_block bb, b, next;
2632 if (mode & (CLEANUP_CROSSJUMP | CLEANUP_THREADING))
2633 clear_bb_flags ();
2635 crossjumps_occured = false;
2637 FOR_EACH_BB_FN (bb, cfun)
2638 update_forwarder_flag (bb);
2640 if (! targetm.cannot_modify_jumps_p ())
2642 first_pass = true;
2643 /* Attempt to merge blocks as made possible by edge removal. If
2644 a block has only one successor, and the successor has only
2645 one predecessor, they may be combined. */
2648 block_was_dirty = false;
2649 changed = false;
2650 iterations++;
2652 if (dump_file)
2653 fprintf (dump_file,
2654 "\n\ntry_optimize_cfg iteration %i\n\n",
2655 iterations);
2657 for (b = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; b
2658 != EXIT_BLOCK_PTR_FOR_FN (cfun);)
2660 basic_block c;
2661 edge s;
2662 bool changed_here = false;
2664 /* Delete trivially dead basic blocks. This is either
2665 blocks with no predecessors, or empty blocks with no
2666 successors. However if the empty block with no
2667 successors is the successor of the ENTRY_BLOCK, it is
2668 kept. This ensures that the ENTRY_BLOCK will have a
2669 successor which is a precondition for many RTL
2670 passes. Empty blocks may result from expanding
2671 __builtin_unreachable (). */
2672 if (EDGE_COUNT (b->preds) == 0
2673 || (EDGE_COUNT (b->succs) == 0
2674 && trivially_empty_bb_p (b)
2675 && single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun))->dest
2676 != b))
2678 c = b->prev_bb;
2679 if (EDGE_COUNT (b->preds) > 0)
2681 edge e;
2682 edge_iterator ei;
2684 if (current_ir_type () == IR_RTL_CFGLAYOUT)
2686 if (BB_FOOTER (b)
2687 && BARRIER_P (BB_FOOTER (b)))
2688 FOR_EACH_EDGE (e, ei, b->preds)
2689 if ((e->flags & EDGE_FALLTHRU)
2690 && BB_FOOTER (e->src) == NULL)
2692 if (BB_FOOTER (b))
2694 BB_FOOTER (e->src) = BB_FOOTER (b);
2695 BB_FOOTER (b) = NULL;
2697 else
2699 start_sequence ();
2700 BB_FOOTER (e->src) = emit_barrier ();
2701 end_sequence ();
2705 else
2707 rtx_insn *last = get_last_bb_insn (b);
2708 if (last && BARRIER_P (last))
2709 FOR_EACH_EDGE (e, ei, b->preds)
2710 if ((e->flags & EDGE_FALLTHRU))
2711 emit_barrier_after (BB_END (e->src));
2714 delete_basic_block (b);
2715 changed = true;
2716 /* Avoid trying to remove the exit block. */
2717 b = (c == ENTRY_BLOCK_PTR_FOR_FN (cfun) ? c->next_bb : c);
2718 continue;
2721 /* Remove code labels no longer used. */
2722 if (single_pred_p (b)
2723 && (single_pred_edge (b)->flags & EDGE_FALLTHRU)
2724 && !(single_pred_edge (b)->flags & EDGE_COMPLEX)
2725 && LABEL_P (BB_HEAD (b))
2726 && !LABEL_PRESERVE_P (BB_HEAD (b))
2727 /* If the previous block ends with a branch to this
2728 block, we can't delete the label. Normally this
2729 is a condjump that is yet to be simplified, but
2730 if CASE_DROPS_THRU, this can be a tablejump with
2731 some element going to the same place as the
2732 default (fallthru). */
2733 && (single_pred (b) == ENTRY_BLOCK_PTR_FOR_FN (cfun)
2734 || !JUMP_P (BB_END (single_pred (b)))
2735 || ! label_is_jump_target_p (BB_HEAD (b),
2736 BB_END (single_pred (b)))))
2738 delete_insn (BB_HEAD (b));
2739 if (dump_file)
2740 fprintf (dump_file, "Deleted label in block %i.\n",
2741 b->index);
2744 /* If we fall through an empty block, we can remove it. */
2745 if (!(mode & (CLEANUP_CFGLAYOUT | CLEANUP_NO_INSN_DEL))
2746 && single_pred_p (b)
2747 && (single_pred_edge (b)->flags & EDGE_FALLTHRU)
2748 && !LABEL_P (BB_HEAD (b))
2749 && FORWARDER_BLOCK_P (b)
2750 /* Note that forwarder_block_p true ensures that
2751 there is a successor for this block. */
2752 && (single_succ_edge (b)->flags & EDGE_FALLTHRU)
2753 && n_basic_blocks_for_fn (cfun) > NUM_FIXED_BLOCKS + 1)
2755 if (dump_file)
2756 fprintf (dump_file,
2757 "Deleting fallthru block %i.\n",
2758 b->index);
2760 c = ((b->prev_bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
2761 ? b->next_bb : b->prev_bb);
2762 redirect_edge_succ_nodup (single_pred_edge (b),
2763 single_succ (b));
2764 delete_basic_block (b);
2765 changed = true;
2766 b = c;
2767 continue;
2770 /* Merge B with its single successor, if any. */
2771 if (single_succ_p (b)
2772 && (s = single_succ_edge (b))
2773 && !(s->flags & EDGE_COMPLEX)
2774 && (c = s->dest) != EXIT_BLOCK_PTR_FOR_FN (cfun)
2775 && single_pred_p (c)
2776 && b != c)
2778 /* When not in cfg_layout mode use code aware of reordering
2779 INSN. This code possibly creates new basic blocks so it
2780 does not fit merge_blocks interface and is kept here in
2781 hope that it will become useless once more of compiler
2782 is transformed to use cfg_layout mode. */
2784 if ((mode & CLEANUP_CFGLAYOUT)
2785 && can_merge_blocks_p (b, c))
2787 merge_blocks (b, c);
2788 update_forwarder_flag (b);
2789 changed_here = true;
2791 else if (!(mode & CLEANUP_CFGLAYOUT)
2792 /* If the jump insn has side effects,
2793 we can't kill the edge. */
2794 && (!JUMP_P (BB_END (b))
2795 || (reload_completed
2796 ? simplejump_p (BB_END (b))
2797 : (onlyjump_p (BB_END (b))
2798 && !tablejump_p (BB_END (b),
2799 NULL, NULL))))
2800 && (next = merge_blocks_move (s, b, c, mode)))
2802 b = next;
2803 changed_here = true;
2807 /* Simplify branch over branch. */
2808 if ((mode & CLEANUP_EXPENSIVE)
2809 && !(mode & CLEANUP_CFGLAYOUT)
2810 && try_simplify_condjump (b))
2811 changed_here = true;
2813 /* If B has a single outgoing edge, but uses a
2814 non-trivial jump instruction without side-effects, we
2815 can either delete the jump entirely, or replace it
2816 with a simple unconditional jump. */
2817 if (single_succ_p (b)
2818 && single_succ (b) != EXIT_BLOCK_PTR_FOR_FN (cfun)
2819 && onlyjump_p (BB_END (b))
2820 && !CROSSING_JUMP_P (BB_END (b))
2821 && try_redirect_by_replacing_jump (single_succ_edge (b),
2822 single_succ (b),
2823 (mode & CLEANUP_CFGLAYOUT) != 0))
2825 update_forwarder_flag (b);
2826 changed_here = true;
2829 /* Simplify branch to branch. */
2830 if (try_forward_edges (mode, b))
2832 update_forwarder_flag (b);
2833 changed_here = true;
2836 /* Look for shared code between blocks. */
2837 if ((mode & CLEANUP_CROSSJUMP)
2838 && try_crossjump_bb (mode, b))
2839 changed_here = true;
2841 if ((mode & CLEANUP_CROSSJUMP)
2842 /* This can lengthen register lifetimes. Do it only after
2843 reload. */
2844 && reload_completed
2845 && try_head_merge_bb (b))
2846 changed_here = true;
2848 /* Don't get confused by the index shift caused by
2849 deleting blocks. */
2850 if (!changed_here)
2851 b = b->next_bb;
2852 else
2853 changed = true;
2856 if ((mode & CLEANUP_CROSSJUMP)
2857 && try_crossjump_bb (mode, EXIT_BLOCK_PTR_FOR_FN (cfun)))
2858 changed = true;
2860 if (block_was_dirty)
2862 /* This should only be set by head-merging. */
2863 gcc_assert (mode & CLEANUP_CROSSJUMP);
2864 df_analyze ();
2867 if (changed)
2869 /* Edge forwarding in particular can cause hot blocks previously
2870 reached by both hot and cold blocks to become dominated only
2871 by cold blocks. This will cause the verification below to fail,
2872 and lead to now cold code in the hot section. This is not easy
2873 to detect and fix during edge forwarding, and in some cases
2874 is only visible after newly unreachable blocks are deleted,
2875 which will be done in fixup_partitions. */
2876 fixup_partitions ();
2878 #ifdef ENABLE_CHECKING
2879 verify_flow_info ();
2880 #endif
2883 changed_overall |= changed;
2884 first_pass = false;
2886 while (changed);
2889 FOR_ALL_BB_FN (b, cfun)
2890 b->flags &= ~(BB_FORWARDER_BLOCK | BB_NONTHREADABLE_BLOCK);
2892 return changed_overall;
2895 /* Delete all unreachable basic blocks. */
2897 bool
2898 delete_unreachable_blocks (void)
2900 bool changed = false;
2901 basic_block b, prev_bb;
2903 find_unreachable_blocks ();
2905 /* When we're in GIMPLE mode and there may be debug insns, we should
2906 delete blocks in reverse dominator order, so as to get a chance
2907 to substitute all released DEFs into debug stmts. If we don't
2908 have dominators information, walking blocks backward gets us a
2909 better chance of retaining most debug information than
2910 otherwise. */
2911 if (MAY_HAVE_DEBUG_INSNS && current_ir_type () == IR_GIMPLE
2912 && dom_info_available_p (CDI_DOMINATORS))
2914 for (b = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
2915 b != ENTRY_BLOCK_PTR_FOR_FN (cfun); b = prev_bb)
2917 prev_bb = b->prev_bb;
2919 if (!(b->flags & BB_REACHABLE))
2921 /* Speed up the removal of blocks that don't dominate
2922 others. Walking backwards, this should be the common
2923 case. */
2924 if (!first_dom_son (CDI_DOMINATORS, b))
2925 delete_basic_block (b);
2926 else
2928 vec<basic_block> h
2929 = get_all_dominated_blocks (CDI_DOMINATORS, b);
2931 while (h.length ())
2933 b = h.pop ();
2935 prev_bb = b->prev_bb;
2937 gcc_assert (!(b->flags & BB_REACHABLE));
2939 delete_basic_block (b);
2942 h.release ();
2945 changed = true;
2949 else
2951 for (b = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
2952 b != ENTRY_BLOCK_PTR_FOR_FN (cfun); b = prev_bb)
2954 prev_bb = b->prev_bb;
2956 if (!(b->flags & BB_REACHABLE))
2958 delete_basic_block (b);
2959 changed = true;
2964 if (changed)
2965 tidy_fallthru_edges ();
2966 return changed;
2969 /* Delete any jump tables never referenced. We can't delete them at the
2970 time of removing tablejump insn as they are referenced by the preceding
2971 insns computing the destination, so we delay deleting and garbagecollect
2972 them once life information is computed. */
2973 void
2974 delete_dead_jumptables (void)
2976 basic_block bb;
2978 /* A dead jump table does not belong to any basic block. Scan insns
2979 between two adjacent basic blocks. */
2980 FOR_EACH_BB_FN (bb, cfun)
2982 rtx_insn *insn, *next;
2984 for (insn = NEXT_INSN (BB_END (bb));
2985 insn && !NOTE_INSN_BASIC_BLOCK_P (insn);
2986 insn = next)
2988 next = NEXT_INSN (insn);
2989 if (LABEL_P (insn)
2990 && LABEL_NUSES (insn) == LABEL_PRESERVE_P (insn)
2991 && JUMP_TABLE_DATA_P (next))
2993 rtx_insn *label = insn, *jump = next;
2995 if (dump_file)
2996 fprintf (dump_file, "Dead jumptable %i removed\n",
2997 INSN_UID (insn));
2999 next = NEXT_INSN (next);
3000 delete_insn (jump);
3001 delete_insn (label);
3008 /* Tidy the CFG by deleting unreachable code and whatnot. */
3010 bool
3011 cleanup_cfg (int mode)
3013 bool changed = false;
3015 /* Set the cfglayout mode flag here. We could update all the callers
3016 but that is just inconvenient, especially given that we eventually
3017 want to have cfglayout mode as the default. */
3018 if (current_ir_type () == IR_RTL_CFGLAYOUT)
3019 mode |= CLEANUP_CFGLAYOUT;
3021 timevar_push (TV_CLEANUP_CFG);
3022 if (delete_unreachable_blocks ())
3024 changed = true;
3025 /* We've possibly created trivially dead code. Cleanup it right
3026 now to introduce more opportunities for try_optimize_cfg. */
3027 if (!(mode & (CLEANUP_NO_INSN_DEL))
3028 && !reload_completed)
3029 delete_trivially_dead_insns (get_insns (), max_reg_num ());
3032 compact_blocks ();
3034 /* To tail-merge blocks ending in the same noreturn function (e.g.
3035 a call to abort) we have to insert fake edges to exit. Do this
3036 here once. The fake edges do not interfere with any other CFG
3037 cleanups. */
3038 if (mode & CLEANUP_CROSSJUMP)
3039 add_noreturn_fake_exit_edges ();
3041 if (!dbg_cnt (cfg_cleanup))
3042 return changed;
3044 while (try_optimize_cfg (mode))
3046 delete_unreachable_blocks (), changed = true;
3047 if (!(mode & CLEANUP_NO_INSN_DEL))
3049 /* Try to remove some trivially dead insns when doing an expensive
3050 cleanup. But delete_trivially_dead_insns doesn't work after
3051 reload (it only handles pseudos) and run_fast_dce is too costly
3052 to run in every iteration.
3054 For effective cross jumping, we really want to run a fast DCE to
3055 clean up any dead conditions, or they get in the way of performing
3056 useful tail merges.
3058 Other transformations in cleanup_cfg are not so sensitive to dead
3059 code, so delete_trivially_dead_insns or even doing nothing at all
3060 is good enough. */
3061 if ((mode & CLEANUP_EXPENSIVE) && !reload_completed
3062 && !delete_trivially_dead_insns (get_insns (), max_reg_num ()))
3063 break;
3064 if ((mode & CLEANUP_CROSSJUMP) && crossjumps_occured)
3065 run_fast_dce ();
3067 else
3068 break;
3071 if (mode & CLEANUP_CROSSJUMP)
3072 remove_fake_exit_edges ();
3074 /* Don't call delete_dead_jumptables in cfglayout mode, because
3075 that function assumes that jump tables are in the insns stream.
3076 But we also don't _have_ to delete dead jumptables in cfglayout
3077 mode because we shouldn't even be looking at things that are
3078 not in a basic block. Dead jumptables are cleaned up when
3079 going out of cfglayout mode. */
3080 if (!(mode & CLEANUP_CFGLAYOUT))
3081 delete_dead_jumptables ();
3083 /* ??? We probably do this way too often. */
3084 if (current_loops
3085 && (changed
3086 || (mode & CLEANUP_CFG_CHANGED)))
3088 timevar_push (TV_REPAIR_LOOPS);
3089 /* The above doesn't preserve dominance info if available. */
3090 gcc_assert (!dom_info_available_p (CDI_DOMINATORS));
3091 calculate_dominance_info (CDI_DOMINATORS);
3092 fix_loop_structure (NULL);
3093 free_dominance_info (CDI_DOMINATORS);
3094 timevar_pop (TV_REPAIR_LOOPS);
3097 timevar_pop (TV_CLEANUP_CFG);
3099 return changed;
3102 namespace {
3104 const pass_data pass_data_jump =
3106 RTL_PASS, /* type */
3107 "jump", /* name */
3108 OPTGROUP_NONE, /* optinfo_flags */
3109 TV_JUMP, /* tv_id */
3110 0, /* properties_required */
3111 0, /* properties_provided */
3112 0, /* properties_destroyed */
3113 0, /* todo_flags_start */
3114 0, /* todo_flags_finish */
3117 class pass_jump : public rtl_opt_pass
3119 public:
3120 pass_jump (gcc::context *ctxt)
3121 : rtl_opt_pass (pass_data_jump, ctxt)
3124 /* opt_pass methods: */
3125 virtual unsigned int execute (function *);
3127 }; // class pass_jump
3129 unsigned int
3130 pass_jump::execute (function *)
3132 delete_trivially_dead_insns (get_insns (), max_reg_num ());
3133 if (dump_file)
3134 dump_flow_info (dump_file, dump_flags);
3135 cleanup_cfg ((optimize ? CLEANUP_EXPENSIVE : 0)
3136 | (flag_thread_jumps ? CLEANUP_THREADING : 0));
3137 return 0;
3140 } // anon namespace
3142 rtl_opt_pass *
3143 make_pass_jump (gcc::context *ctxt)
3145 return new pass_jump (ctxt);
3148 namespace {
3150 const pass_data pass_data_jump2 =
3152 RTL_PASS, /* type */
3153 "jump2", /* name */
3154 OPTGROUP_NONE, /* optinfo_flags */
3155 TV_JUMP, /* tv_id */
3156 0, /* properties_required */
3157 0, /* properties_provided */
3158 0, /* properties_destroyed */
3159 0, /* todo_flags_start */
3160 0, /* todo_flags_finish */
3163 class pass_jump2 : public rtl_opt_pass
3165 public:
3166 pass_jump2 (gcc::context *ctxt)
3167 : rtl_opt_pass (pass_data_jump2, ctxt)
3170 /* opt_pass methods: */
3171 virtual unsigned int execute (function *)
3173 cleanup_cfg (flag_crossjumping ? CLEANUP_CROSSJUMP : 0);
3174 return 0;
3177 }; // class pass_jump2
3179 } // anon namespace
3181 rtl_opt_pass *
3182 make_pass_jump2 (gcc::context *ctxt)
3184 return new pass_jump2 (ctxt);