Use MODE_BASE_REG_CLASS in legitimize macros.
[official-gcc.git] / gcc / cfgcleanup.c
blob5015c81494129bf133bd64bd2b8278bada628cdb
1 /* Control flow optimization code for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* This file contains optimizer of the control flow. The main entrypoint is
23 cleanup_cfg. Following optimizations are performed:
25 - Unreachable blocks removal
26 - Edge forwarding (edge to the forwarder block is forwarded to it's
27 successor. Simplification of the branch instruction is performed by
28 underlying infrastructure so branch can be converted to simplejump or
29 eliminated).
30 - Cross jumping (tail merging)
31 - Conditional jump-around-simplejump simplification
32 - Basic block merging. */
34 #include "config.h"
35 #include "system.h"
36 #include "rtl.h"
37 #include "hard-reg-set.h"
38 #include "basic-block.h"
39 #include "timevar.h"
40 #include "output.h"
41 #include "insn-config.h"
42 #include "flags.h"
43 #include "recog.h"
44 #include "toplev.h"
45 #include "cselib.h"
46 #include "tm_p.h"
48 #include "obstack.h"
50 /* cleanup_cfg maintains following flags for each basic block. */
52 enum bb_flags
54 /* Set if life info needs to be recomputed for given BB. */
55 BB_UPDATE_LIFE = 1,
56 /* Set if BB is the forwarder block to avoid too many
57 forwarder_block_p calls. */
58 BB_FORWARDER_BLOCK = 2
61 #define BB_FLAGS(BB) (enum bb_flags) (BB)->aux
62 #define BB_SET_FLAG(BB, FLAG) \
63 (BB)->aux = (void *) (long) ((enum bb_flags) (BB)->aux | (FLAG))
64 #define BB_CLEAR_FLAG(BB, FLAG) \
65 (BB)->aux = (void *) (long) ((enum bb_flags) (BB)->aux & ~(FLAG))
67 #define FORWARDER_BLOCK_P(BB) (BB_FLAGS (BB) & BB_FORWARDER_BLOCK)
69 static bool try_crossjump_to_edge PARAMS ((int, edge, edge));
70 static bool try_crossjump_bb PARAMS ((int, basic_block));
71 static bool outgoing_edges_match PARAMS ((int,
72 basic_block, basic_block));
73 static int flow_find_cross_jump PARAMS ((int, basic_block, basic_block,
74 rtx *, rtx *));
75 static bool insns_match_p PARAMS ((int, rtx, rtx));
77 static bool delete_unreachable_blocks PARAMS ((void));
78 static bool label_is_jump_target_p PARAMS ((rtx, rtx));
79 static bool tail_recursion_label_p PARAMS ((rtx));
80 static void merge_blocks_move_predecessor_nojumps PARAMS ((basic_block,
81 basic_block));
82 static void merge_blocks_move_successor_nojumps PARAMS ((basic_block,
83 basic_block));
84 static bool merge_blocks PARAMS ((edge,basic_block,basic_block,
85 int));
86 static bool try_optimize_cfg PARAMS ((int));
87 static bool try_simplify_condjump PARAMS ((basic_block));
88 static bool try_forward_edges PARAMS ((int, basic_block));
89 static edge thread_jump PARAMS ((int, edge, basic_block));
90 static bool mark_effect PARAMS ((rtx, bitmap));
91 static void notice_new_block PARAMS ((basic_block));
92 static void update_forwarder_flag PARAMS ((basic_block));
94 /* Set flags for newly created block. */
96 static void
97 notice_new_block (bb)
98 basic_block bb;
100 if (!bb)
101 return;
103 BB_SET_FLAG (bb, BB_UPDATE_LIFE);
104 if (forwarder_block_p (bb))
105 BB_SET_FLAG (bb, BB_FORWARDER_BLOCK);
108 /* Recompute forwarder flag after block has been modified. */
110 static void
111 update_forwarder_flag (bb)
112 basic_block bb;
114 if (forwarder_block_p (bb))
115 BB_SET_FLAG (bb, BB_FORWARDER_BLOCK);
116 else
117 BB_CLEAR_FLAG (bb, BB_FORWARDER_BLOCK);
120 /* Simplify a conditional jump around an unconditional jump.
121 Return true if something changed. */
123 static bool
124 try_simplify_condjump (cbranch_block)
125 basic_block cbranch_block;
127 basic_block jump_block, jump_dest_block, cbranch_dest_block;
128 edge cbranch_jump_edge, cbranch_fallthru_edge;
129 rtx cbranch_insn;
131 /* Verify that there are exactly two successors. */
132 if (!cbranch_block->succ
133 || !cbranch_block->succ->succ_next
134 || cbranch_block->succ->succ_next->succ_next)
135 return false;
137 /* Verify that we've got a normal conditional branch at the end
138 of the block. */
139 cbranch_insn = cbranch_block->end;
140 if (!any_condjump_p (cbranch_insn))
141 return false;
143 cbranch_fallthru_edge = FALLTHRU_EDGE (cbranch_block);
144 cbranch_jump_edge = BRANCH_EDGE (cbranch_block);
146 /* The next block must not have multiple predecessors, must not
147 be the last block in the function, and must contain just the
148 unconditional jump. */
149 jump_block = cbranch_fallthru_edge->dest;
150 if (jump_block->pred->pred_next
151 || jump_block->index == n_basic_blocks - 1
152 || !FORWARDER_BLOCK_P (jump_block))
153 return false;
154 jump_dest_block = jump_block->succ->dest;
156 /* The conditional branch must target the block after the
157 unconditional branch. */
158 cbranch_dest_block = cbranch_jump_edge->dest;
160 if (!can_fallthru (jump_block, cbranch_dest_block))
161 return false;
163 /* Invert the conditional branch. */
164 if (!invert_jump (cbranch_insn, block_label (jump_dest_block), 0))
165 return false;
167 if (rtl_dump_file)
168 fprintf (rtl_dump_file, "Simplifying condjump %i around jump %i\n",
169 INSN_UID (cbranch_insn), INSN_UID (jump_block->end));
171 /* Success. Update the CFG to match. Note that after this point
172 the edge variable names appear backwards; the redirection is done
173 this way to preserve edge profile data. */
174 cbranch_jump_edge = redirect_edge_succ_nodup (cbranch_jump_edge,
175 cbranch_dest_block);
176 cbranch_fallthru_edge = redirect_edge_succ_nodup (cbranch_fallthru_edge,
177 jump_dest_block);
178 cbranch_jump_edge->flags |= EDGE_FALLTHRU;
179 cbranch_fallthru_edge->flags &= ~EDGE_FALLTHRU;
180 update_br_prob_note (cbranch_block);
182 /* Delete the block with the unconditional jump, and clean up the mess. */
183 flow_delete_block (jump_block);
184 tidy_fallthru_edge (cbranch_jump_edge, cbranch_block, cbranch_dest_block);
186 return true;
189 /* Attempt to prove that operation is NOOP using CSElib or mark the effect
190 on register. Used by jump threading. */
192 static bool
193 mark_effect (exp, nonequal)
194 rtx exp;
195 regset nonequal;
197 int regno;
198 rtx dest;
199 switch (GET_CODE (exp))
201 /* In case we do clobber the register, mark it as equal, as we know the
202 value is dead so it don't have to match. */
203 case CLOBBER:
204 if (REG_P (XEXP (exp, 0)))
206 dest = XEXP (exp, 0);
207 regno = REGNO (dest);
208 CLEAR_REGNO_REG_SET (nonequal, regno);
209 if (regno < FIRST_PSEUDO_REGISTER)
211 int n = HARD_REGNO_NREGS (regno, GET_MODE (dest));
212 while (--n > 0)
213 CLEAR_REGNO_REG_SET (nonequal, regno + n);
216 return false;
218 case SET:
219 if (rtx_equal_for_cselib_p (SET_DEST (exp), SET_SRC (exp)))
220 return false;
221 dest = SET_DEST (exp);
222 if (dest == pc_rtx)
223 return false;
224 if (!REG_P (dest))
225 return true;
226 regno = REGNO (dest);
227 SET_REGNO_REG_SET (nonequal, regno);
228 if (regno < FIRST_PSEUDO_REGISTER)
230 int n = HARD_REGNO_NREGS (regno, GET_MODE (dest));
231 while (--n > 0)
232 SET_REGNO_REG_SET (nonequal, regno + n);
234 return false;
236 default:
237 return false;
240 /* Attempt to prove that the basic block B will have no side effects and
241 allways continues in the same edge if reached via E. Return the edge
242 if exist, NULL otherwise. */
244 static edge
245 thread_jump (mode, e, b)
246 int mode;
247 edge e;
248 basic_block b;
250 rtx set1, set2, cond1, cond2, insn;
251 enum rtx_code code1, code2, reversed_code2;
252 bool reverse1 = false;
253 int i;
254 regset nonequal;
255 bool failed = false;
257 /* At the moment, we do handle only conditional jumps, but later we may
258 want to extend this code to tablejumps and others. */
259 if (!e->src->succ->succ_next || e->src->succ->succ_next->succ_next)
260 return NULL;
261 if (!b->succ || !b->succ->succ_next || b->succ->succ_next->succ_next)
262 return NULL;
264 /* Second branch must end with onlyjump, as we will eliminate the jump. */
265 if (!any_condjump_p (e->src->end) || !any_condjump_p (b->end)
266 || !onlyjump_p (b->end))
267 return NULL;
269 set1 = pc_set (e->src->end);
270 set2 = pc_set (b->end);
271 if (((e->flags & EDGE_FALLTHRU) != 0)
272 != (XEXP (SET_SRC (set1), 1) == pc_rtx))
273 reverse1 = true;
275 cond1 = XEXP (SET_SRC (set1), 0);
276 cond2 = XEXP (SET_SRC (set2), 0);
277 if (reverse1)
278 code1 = reversed_comparison_code (cond1, e->src->end);
279 else
280 code1 = GET_CODE (cond1);
282 code2 = GET_CODE (cond2);
283 reversed_code2 = reversed_comparison_code (cond2, b->end);
285 if (!comparison_dominates_p (code1, code2)
286 && !comparison_dominates_p (code1, reversed_code2))
287 return NULL;
289 /* Ensure that the comparison operators are equivalent.
290 ??? This is far too pesimistic. We should allow swapped operands,
291 different CCmodes, or for example comparisons for interval, that
292 dominate even when operands are not equivalent. */
293 if (!rtx_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
294 || !rtx_equal_p (XEXP (cond1, 1), XEXP (cond2, 1)))
295 return NULL;
297 /* Short circuit cases where block B contains some side effects, as we can't
298 safely bypass it. */
299 for (insn = NEXT_INSN (b->head); insn != NEXT_INSN (b->end);
300 insn = NEXT_INSN (insn))
301 if (INSN_P (insn) && side_effects_p (PATTERN (insn)))
302 return NULL;
304 cselib_init ();
306 /* First process all values computed in the source basic block. */
307 for (insn = NEXT_INSN (e->src->head); insn != NEXT_INSN (e->src->end);
308 insn = NEXT_INSN (insn))
309 if (INSN_P (insn))
310 cselib_process_insn (insn);
312 nonequal = BITMAP_XMALLOC();
313 CLEAR_REG_SET (nonequal);
315 /* Now assume that we've continued by the edge E to B and continue
316 processing as if it were same basic block.
317 Our goal is to prove that whole block is an NOOP. */
319 for (insn = NEXT_INSN (b->head); insn != NEXT_INSN (b->end) && !failed;
320 insn = NEXT_INSN (insn))
322 if (INSN_P (insn))
324 rtx pat = PATTERN (insn);
326 if (GET_CODE (pat) == PARALLEL)
328 for (i = 0; i < XVECLEN (pat, 0); i++)
329 failed |= mark_effect (XVECEXP (pat, 0, i), nonequal);
331 else
332 failed |= mark_effect (pat, nonequal);
335 cselib_process_insn (insn);
338 /* Later we should clear nonequal of dead registers. So far we don't
339 have life information in cfg_cleanup. */
340 if (failed)
341 goto failed_exit;
343 /* In case liveness information is available, we need to prove equivalence
344 only of the live values. */
345 if (mode & CLEANUP_UPDATE_LIFE)
346 AND_REG_SET (nonequal, b->global_live_at_end);
348 EXECUTE_IF_SET_IN_REG_SET (nonequal, 0, i, goto failed_exit;);
350 BITMAP_XFREE (nonequal);
351 cselib_finish ();
352 if ((comparison_dominates_p (code1, code2) != 0)
353 != (XEXP (SET_SRC (set2), 1) == pc_rtx))
354 return BRANCH_EDGE (b);
355 else
356 return FALLTHRU_EDGE (b);
358 failed_exit:
359 BITMAP_XFREE (nonequal);
360 cselib_finish ();
361 return NULL;
364 /* Attempt to forward edges leaving basic block B.
365 Return true if successful. */
367 static bool
368 try_forward_edges (mode, b)
369 basic_block b;
370 int mode;
372 bool changed = false;
373 edge e, next, *threaded_edges = NULL;
375 for (e = b->succ; e; e = next)
377 basic_block target, first;
378 int counter;
379 bool threaded = false;
380 int nthreaded_edges = 0;
382 next = e->succ_next;
384 /* Skip complex edges because we don't know how to update them.
386 Still handle fallthru edges, as we can succeed to forward fallthru
387 edge to the same place as the branch edge of conditional branch
388 and turn conditional branch to an unconditional branch. */
389 if (e->flags & EDGE_COMPLEX)
390 continue;
392 target = first = e->dest;
393 counter = 0;
395 while (counter < n_basic_blocks)
397 basic_block new_target = NULL;
398 bool new_target_threaded = false;
400 if (FORWARDER_BLOCK_P (target)
401 && target->succ->dest != EXIT_BLOCK_PTR)
403 /* Bypass trivial infinite loops. */
404 if (target == target->succ->dest)
405 counter = n_basic_blocks;
406 new_target = target->succ->dest;
409 /* Allow to thread only over one edge at time to simplify updating
410 of probabilities. */
411 else if (mode & CLEANUP_THREADING)
413 edge t = thread_jump (mode, e, target);
414 if (t)
416 if (!threaded_edges)
417 threaded_edges = xmalloc (sizeof (*threaded_edges)
418 * n_basic_blocks);
419 else
421 int i;
423 /* Detect an infinite loop across blocks not
424 including the start block. */
425 for (i = 0; i < nthreaded_edges; ++i)
426 if (threaded_edges[i] == t)
427 break;
428 if (i < nthreaded_edges)
429 break;
432 /* Detect an infinite loop across the start block. */
433 if (t->dest == b)
434 break;
436 if (nthreaded_edges >= n_basic_blocks)
437 abort ();
438 threaded_edges[nthreaded_edges++] = t;
440 new_target = t->dest;
441 new_target_threaded = true;
445 if (!new_target)
446 break;
448 /* Avoid killing of loop pre-headers, as it is the place loop
449 optimizer wants to hoist code to.
451 For fallthru forwarders, the LOOP_BEG note must appear between
452 the header of block and CODE_LABEL of the loop, for non forwarders
453 it must appear before the JUMP_INSN. */
454 if (mode & CLEANUP_PRE_LOOP)
456 rtx insn = (target->succ->flags & EDGE_FALLTHRU
457 ? target->head : prev_nonnote_insn (target->end));
459 if (GET_CODE (insn) != NOTE)
460 insn = NEXT_INSN (insn);
462 for (; insn && GET_CODE (insn) != CODE_LABEL && !INSN_P (insn);
463 insn = NEXT_INSN (insn))
464 if (GET_CODE (insn) == NOTE
465 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
466 break;
468 if (GET_CODE (insn) == NOTE)
469 break;
472 counter++;
473 target = new_target;
474 threaded |= new_target_threaded;
477 if (counter >= n_basic_blocks)
479 if (rtl_dump_file)
480 fprintf (rtl_dump_file, "Infinite loop in BB %i.\n",
481 target->index);
483 else if (target == first)
484 ; /* We didn't do anything. */
485 else
487 /* Save the values now, as the edge may get removed. */
488 gcov_type edge_count = e->count;
489 int edge_probability = e->probability;
490 int edge_frequency;
491 int n = 0;
493 /* Don't force if target is exit block. */
494 if (threaded && target != EXIT_BLOCK_PTR)
496 notice_new_block (redirect_edge_and_branch_force (e, target));
497 if (rtl_dump_file)
498 fprintf (rtl_dump_file, "Conditionals threaded.\n");
500 else if (!redirect_edge_and_branch (e, target))
502 if (rtl_dump_file)
503 fprintf (rtl_dump_file,
504 "Forwarding edge %i->%i to %i failed.\n",
505 b->index, e->dest->index, target->index);
506 continue;
509 /* We successfully forwarded the edge. Now update profile
510 data: for each edge we traversed in the chain, remove
511 the original edge's execution count. */
512 edge_frequency = ((edge_probability * b->frequency
513 + REG_BR_PROB_BASE / 2)
514 / REG_BR_PROB_BASE);
516 if (!FORWARDER_BLOCK_P (b) && forwarder_block_p (b))
517 BB_SET_FLAG (b, BB_FORWARDER_BLOCK);
518 BB_SET_FLAG (b, BB_UPDATE_LIFE);
522 edge t;
524 first->count -= edge_count;
525 if (first->count < 0)
526 first->count = 0;
527 first->frequency -= edge_frequency;
528 if (first->frequency < 0)
529 first->frequency = 0;
530 if (first->succ->succ_next)
532 edge e;
533 int prob;
534 if (n >= nthreaded_edges)
535 abort ();
536 t = threaded_edges [n++];
537 if (t->src != first)
538 abort ();
539 if (first->frequency)
540 prob = edge_frequency * REG_BR_PROB_BASE / first->frequency;
541 else
542 prob = 0;
543 if (prob > t->probability)
544 prob = t->probability;
545 t->probability -= prob;
546 prob = REG_BR_PROB_BASE - prob;
547 if (prob <= 0)
549 first->succ->probability = REG_BR_PROB_BASE;
550 first->succ->succ_next->probability = 0;
552 else
553 for (e = first->succ; e; e = e->succ_next)
554 e->probability = ((e->probability * REG_BR_PROB_BASE)
555 / (double) prob);
556 update_br_prob_note (first);
558 else
560 /* It is possible that as the result of
561 threading we've removed edge as it is
562 threaded to the fallthru edge. Avoid
563 getting out of sync. */
564 if (n < nthreaded_edges
565 && first == threaded_edges [n]->src)
566 n++;
567 t = first->succ;
570 t->count -= edge_count;
571 if (t->count < 0)
572 t->count = 0;
573 first = t->dest;
575 while (first != target);
577 changed = true;
581 if (threaded_edges)
582 free (threaded_edges);
583 return changed;
586 /* Return true if LABEL is a target of JUMP_INSN. This applies only
587 to non-complex jumps. That is, direct unconditional, conditional,
588 and tablejumps, but not computed jumps or returns. It also does
589 not apply to the fallthru case of a conditional jump. */
591 static bool
592 label_is_jump_target_p (label, jump_insn)
593 rtx label, jump_insn;
595 rtx tmp = JUMP_LABEL (jump_insn);
597 if (label == tmp)
598 return true;
600 if (tmp != NULL_RTX
601 && (tmp = NEXT_INSN (tmp)) != NULL_RTX
602 && GET_CODE (tmp) == JUMP_INSN
603 && (tmp = PATTERN (tmp),
604 GET_CODE (tmp) == ADDR_VEC
605 || GET_CODE (tmp) == ADDR_DIFF_VEC))
607 rtvec vec = XVEC (tmp, GET_CODE (tmp) == ADDR_DIFF_VEC);
608 int i, veclen = GET_NUM_ELEM (vec);
610 for (i = 0; i < veclen; ++i)
611 if (XEXP (RTVEC_ELT (vec, i), 0) == label)
612 return true;
615 return false;
618 /* Return true if LABEL is used for tail recursion. */
620 static bool
621 tail_recursion_label_p (label)
622 rtx label;
624 rtx x;
626 for (x = tail_recursion_label_list; x; x = XEXP (x, 1))
627 if (label == XEXP (x, 0))
628 return true;
630 return false;
633 /* Blocks A and B are to be merged into a single block. A has no incoming
634 fallthru edge, so it can be moved before B without adding or modifying
635 any jumps (aside from the jump from A to B). */
637 static void
638 merge_blocks_move_predecessor_nojumps (a, b)
639 basic_block a, b;
641 rtx barrier;
642 int index;
644 barrier = next_nonnote_insn (a->end);
645 if (GET_CODE (barrier) != BARRIER)
646 abort ();
647 delete_insn (barrier);
649 /* Move block and loop notes out of the chain so that we do not
650 disturb their order.
652 ??? A better solution would be to squeeze out all the non-nested notes
653 and adjust the block trees appropriately. Even better would be to have
654 a tighter connection between block trees and rtl so that this is not
655 necessary. */
656 if (squeeze_notes (&a->head, &a->end))
657 abort ();
659 /* Scramble the insn chain. */
660 if (a->end != PREV_INSN (b->head))
661 reorder_insns_nobb (a->head, a->end, PREV_INSN (b->head));
662 BB_SET_FLAG (a, BB_UPDATE_LIFE);
664 if (rtl_dump_file)
665 fprintf (rtl_dump_file, "Moved block %d before %d and merged.\n",
666 a->index, b->index);
668 /* Swap the records for the two blocks around. Although we are deleting B,
669 A is now where B was and we want to compact the BB array from where
670 A used to be. */
671 BASIC_BLOCK (a->index) = b;
672 BASIC_BLOCK (b->index) = a;
673 index = a->index;
674 a->index = b->index;
675 b->index = index;
677 /* Now blocks A and B are contiguous. Merge them. */
678 merge_blocks_nomove (a, b);
681 /* Blocks A and B are to be merged into a single block. B has no outgoing
682 fallthru edge, so it can be moved after A without adding or modifying
683 any jumps (aside from the jump from A to B). */
685 static void
686 merge_blocks_move_successor_nojumps (a, b)
687 basic_block a, b;
689 rtx barrier, real_b_end;
691 real_b_end = b->end;
692 barrier = NEXT_INSN (b->end);
694 /* Recognize a jump table following block B. */
695 if (barrier
696 && GET_CODE (barrier) == CODE_LABEL
697 && NEXT_INSN (barrier)
698 && GET_CODE (NEXT_INSN (barrier)) == JUMP_INSN
699 && (GET_CODE (PATTERN (NEXT_INSN (barrier))) == ADDR_VEC
700 || GET_CODE (PATTERN (NEXT_INSN (barrier))) == ADDR_DIFF_VEC))
702 /* Temporarily add the table jump insn to b, so that it will also
703 be moved to the correct location. */
704 b->end = NEXT_INSN (barrier);
705 barrier = NEXT_INSN (b->end);
708 /* There had better have been a barrier there. Delete it. */
709 if (barrier && GET_CODE (barrier) == BARRIER)
710 delete_insn (barrier);
712 /* Move block and loop notes out of the chain so that we do not
713 disturb their order.
715 ??? A better solution would be to squeeze out all the non-nested notes
716 and adjust the block trees appropriately. Even better would be to have
717 a tighter connection between block trees and rtl so that this is not
718 necessary. */
719 if (squeeze_notes (&b->head, &b->end))
720 abort ();
722 /* Scramble the insn chain. */
723 reorder_insns_nobb (b->head, b->end, a->end);
725 /* Restore the real end of b. */
726 b->end = real_b_end;
728 /* Now blocks A and B are contiguous. Merge them. */
729 merge_blocks_nomove (a, b);
730 BB_SET_FLAG (a, BB_UPDATE_LIFE);
732 if (rtl_dump_file)
733 fprintf (rtl_dump_file, "Moved block %d after %d and merged.\n",
734 b->index, a->index);
737 /* Attempt to merge basic blocks that are potentially non-adjacent.
738 Return true iff the attempt succeeded. */
740 static bool
741 merge_blocks (e, b, c, mode)
742 edge e;
743 basic_block b, c;
744 int mode;
746 /* If C has a tail recursion label, do not merge. There is no
747 edge recorded from the call_placeholder back to this label, as
748 that would make optimize_sibling_and_tail_recursive_calls more
749 complex for no gain. */
750 if ((mode & CLEANUP_PRE_SIBCALL)
751 && GET_CODE (c->head) == CODE_LABEL
752 && tail_recursion_label_p (c->head))
753 return false;
755 /* If B has a fallthru edge to C, no need to move anything. */
756 if (e->flags & EDGE_FALLTHRU)
758 int b_index = b->index, c_index = c->index;
759 /* We need to update liveness in case C already has broken liveness
760 or B ends by conditional jump to next instructions that will be
761 removed. */
762 if ((BB_FLAGS (c) & BB_UPDATE_LIFE)
763 || GET_CODE (b->end) == JUMP_INSN)
764 BB_SET_FLAG (b, BB_UPDATE_LIFE);
765 merge_blocks_nomove (b, c);
766 update_forwarder_flag (b);
768 if (rtl_dump_file)
769 fprintf (rtl_dump_file, "Merged %d and %d without moving.\n",
770 b_index, c_index);
772 return true;
775 /* Otherwise we will need to move code around. Do that only if expensive
776 transformations are allowed. */
777 else if (mode & CLEANUP_EXPENSIVE)
779 edge tmp_edge, b_fallthru_edge;
780 bool c_has_outgoing_fallthru;
781 bool b_has_incoming_fallthru;
783 /* Avoid overactive code motion, as the forwarder blocks should be
784 eliminated by edge redirection instead. One exception might have
785 been if B is a forwarder block and C has no fallthru edge, but
786 that should be cleaned up by bb-reorder instead. */
787 if (FORWARDER_BLOCK_P (b) || FORWARDER_BLOCK_P (c))
788 return false;
790 /* We must make sure to not munge nesting of lexical blocks,
791 and loop notes. This is done by squeezing out all the notes
792 and leaving them there to lie. Not ideal, but functional. */
794 for (tmp_edge = c->succ; tmp_edge; tmp_edge = tmp_edge->succ_next)
795 if (tmp_edge->flags & EDGE_FALLTHRU)
796 break;
798 c_has_outgoing_fallthru = (tmp_edge != NULL);
800 for (tmp_edge = b->pred; tmp_edge; tmp_edge = tmp_edge->pred_next)
801 if (tmp_edge->flags & EDGE_FALLTHRU)
802 break;
804 b_has_incoming_fallthru = (tmp_edge != NULL);
805 b_fallthru_edge = tmp_edge;
807 /* Otherwise, we're going to try to move C after B. If C does
808 not have an outgoing fallthru, then it can be moved
809 immediately after B without introducing or modifying jumps. */
810 if (! c_has_outgoing_fallthru)
812 merge_blocks_move_successor_nojumps (b, c);
813 return true;
816 /* If B does not have an incoming fallthru, then it can be moved
817 immediately before C without introducing or modifying jumps.
818 C cannot be the first block, so we do not have to worry about
819 accessing a non-existent block. */
821 if (b_has_incoming_fallthru)
823 basic_block bb;
825 if (b_fallthru_edge->src == ENTRY_BLOCK_PTR)
826 return false;
827 bb = force_nonfallthru (b_fallthru_edge);
828 if (bb)
829 notice_new_block (bb);
830 else
831 BB_SET_FLAG (b_fallthru_edge->src, BB_UPDATE_LIFE);
834 merge_blocks_move_predecessor_nojumps (b, c);
835 return true;
838 return false;
842 /* Return true if I1 and I2 are equivalent and thus can be crossjumped. */
844 static bool
845 insns_match_p (mode, i1, i2)
846 int mode ATTRIBUTE_UNUSED;
847 rtx i1, i2;
849 rtx p1, p2;
851 /* Verify that I1 and I2 are equivalent. */
852 if (GET_CODE (i1) != GET_CODE (i2))
853 return false;
855 p1 = PATTERN (i1);
856 p2 = PATTERN (i2);
858 if (GET_CODE (p1) != GET_CODE (p2))
859 return false;
861 /* If this is a CALL_INSN, compare register usage information.
862 If we don't check this on stack register machines, the two
863 CALL_INSNs might be merged leaving reg-stack.c with mismatching
864 numbers of stack registers in the same basic block.
865 If we don't check this on machines with delay slots, a delay slot may
866 be filled that clobbers a parameter expected by the subroutine.
868 ??? We take the simple route for now and assume that if they're
869 equal, they were constructed identically. */
871 if (GET_CODE (i1) == CALL_INSN
872 && !rtx_equal_p (CALL_INSN_FUNCTION_USAGE (i1),
873 CALL_INSN_FUNCTION_USAGE (i2)))
874 return false;
876 #ifdef STACK_REGS
877 /* If cross_jump_death_matters is not 0, the insn's mode
878 indicates whether or not the insn contains any stack-like
879 regs. */
881 if ((mode & CLEANUP_POST_REGSTACK) && stack_regs_mentioned (i1))
883 /* If register stack conversion has already been done, then
884 death notes must also be compared before it is certain that
885 the two instruction streams match. */
887 rtx note;
888 HARD_REG_SET i1_regset, i2_regset;
890 CLEAR_HARD_REG_SET (i1_regset);
891 CLEAR_HARD_REG_SET (i2_regset);
893 for (note = REG_NOTES (i1); note; note = XEXP (note, 1))
894 if (REG_NOTE_KIND (note) == REG_DEAD && STACK_REG_P (XEXP (note, 0)))
895 SET_HARD_REG_BIT (i1_regset, REGNO (XEXP (note, 0)));
897 for (note = REG_NOTES (i2); note; note = XEXP (note, 1))
898 if (REG_NOTE_KIND (note) == REG_DEAD && STACK_REG_P (XEXP (note, 0)))
899 SET_HARD_REG_BIT (i2_regset, REGNO (XEXP (note, 0)));
901 GO_IF_HARD_REG_EQUAL (i1_regset, i2_regset, done);
903 return false;
905 done:
908 #endif
910 if (reload_completed
911 ? ! rtx_renumbered_equal_p (p1, p2) : ! rtx_equal_p (p1, p2))
913 /* The following code helps take care of G++ cleanups. */
914 rtx equiv1 = find_reg_equal_equiv_note (i1);
915 rtx equiv2 = find_reg_equal_equiv_note (i2);
917 if (equiv1 && equiv2
918 /* If the equivalences are not to a constant, they may
919 reference pseudos that no longer exist, so we can't
920 use them. */
921 && (! reload_completed
922 || (CONSTANT_P (XEXP (equiv1, 0))
923 && rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))))
925 rtx s1 = single_set (i1);
926 rtx s2 = single_set (i2);
927 if (s1 != 0 && s2 != 0
928 && rtx_renumbered_equal_p (SET_DEST (s1), SET_DEST (s2)))
930 validate_change (i1, &SET_SRC (s1), XEXP (equiv1, 0), 1);
931 validate_change (i2, &SET_SRC (s2), XEXP (equiv2, 0), 1);
932 if (! rtx_renumbered_equal_p (p1, p2))
933 cancel_changes (0);
934 else if (apply_change_group ())
935 return true;
939 return false;
942 return true;
945 /* Look through the insns at the end of BB1 and BB2 and find the longest
946 sequence that are equivalent. Store the first insns for that sequence
947 in *F1 and *F2 and return the sequence length.
949 To simplify callers of this function, if the blocks match exactly,
950 store the head of the blocks in *F1 and *F2. */
952 static int
953 flow_find_cross_jump (mode, bb1, bb2, f1, f2)
954 int mode ATTRIBUTE_UNUSED;
955 basic_block bb1, bb2;
956 rtx *f1, *f2;
958 rtx i1, i2, last1, last2, afterlast1, afterlast2;
959 int ninsns = 0;
961 /* Skip simple jumps at the end of the blocks. Complex jumps still
962 need to be compared for equivalence, which we'll do below. */
964 i1 = bb1->end;
965 last1 = afterlast1 = last2 = afterlast2 = NULL_RTX;
966 if (onlyjump_p (i1)
967 || (returnjump_p (i1) && !side_effects_p (PATTERN (i1))))
969 last1 = i1;
970 i1 = PREV_INSN (i1);
973 i2 = bb2->end;
974 if (onlyjump_p (i2)
975 || (returnjump_p (i2) && !side_effects_p (PATTERN (i2))))
977 last2 = i2;
978 /* Count everything except for unconditional jump as insn. */
979 if (!simplejump_p (i2) && !returnjump_p (i2) && last1)
980 ninsns++;
981 i2 = PREV_INSN (i2);
984 while (true)
986 /* Ignore notes. */
987 while (!active_insn_p (i1) && i1 != bb1->head)
988 i1 = PREV_INSN (i1);
990 while (!active_insn_p (i2) && i2 != bb2->head)
991 i2 = PREV_INSN (i2);
993 if (i1 == bb1->head || i2 == bb2->head)
994 break;
996 if (!insns_match_p (mode, i1, i2))
997 break;
999 /* Don't begin a cross-jump with a USE or CLOBBER insn. */
1000 if (active_insn_p (i1))
1002 /* If the merged insns have different REG_EQUAL notes, then
1003 remove them. */
1004 rtx equiv1 = find_reg_equal_equiv_note (i1);
1005 rtx equiv2 = find_reg_equal_equiv_note (i2);
1007 if (equiv1 && !equiv2)
1008 remove_note (i1, equiv1);
1009 else if (!equiv1 && equiv2)
1010 remove_note (i2, equiv2);
1011 else if (equiv1 && equiv2
1012 && !rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))
1014 remove_note (i1, equiv1);
1015 remove_note (i2, equiv2);
1018 afterlast1 = last1, afterlast2 = last2;
1019 last1 = i1, last2 = i2;
1020 ninsns++;
1023 i1 = PREV_INSN (i1);
1024 i2 = PREV_INSN (i2);
1027 #ifdef HAVE_cc0
1028 /* Don't allow the insn after a compare to be shared by
1029 cross-jumping unless the compare is also shared. */
1030 if (ninsns && reg_mentioned_p (cc0_rtx, last1) && ! sets_cc0_p (last1))
1031 last1 = afterlast1, last2 = afterlast2, ninsns--;
1032 #endif
1034 /* Include preceding notes and labels in the cross-jump. One,
1035 this may bring us to the head of the blocks as requested above.
1036 Two, it keeps line number notes as matched as may be. */
1037 if (ninsns)
1039 while (last1 != bb1->head && !active_insn_p (PREV_INSN (last1)))
1040 last1 = PREV_INSN (last1);
1042 if (last1 != bb1->head && GET_CODE (PREV_INSN (last1)) == CODE_LABEL)
1043 last1 = PREV_INSN (last1);
1045 while (last2 != bb2->head && !active_insn_p (PREV_INSN (last2)))
1046 last2 = PREV_INSN (last2);
1048 if (last2 != bb2->head && GET_CODE (PREV_INSN (last2)) == CODE_LABEL)
1049 last2 = PREV_INSN (last2);
1051 *f1 = last1;
1052 *f2 = last2;
1055 return ninsns;
1058 /* Return true iff outgoing edges of BB1 and BB2 match, together with
1059 the branch instruction. This means that if we commonize the control
1060 flow before end of the basic block, the semantic remains unchanged.
1062 We may assume that there exists one edge with a common destination. */
1064 static bool
1065 outgoing_edges_match (mode, bb1, bb2)
1066 int mode;
1067 basic_block bb1;
1068 basic_block bb2;
1070 int nehedges1 = 0, nehedges2 = 0;
1071 edge fallthru1 = 0, fallthru2 = 0;
1072 edge e1, e2;
1074 /* If BB1 has only one successor, we may be looking at either an
1075 unconditional jump, or a fake edge to exit. */
1076 if (bb1->succ && !bb1->succ->succ_next
1077 && !(bb1->succ->flags & (EDGE_COMPLEX | EDGE_FAKE)))
1078 return (bb2->succ && !bb2->succ->succ_next
1079 && (bb2->succ->flags & (EDGE_COMPLEX | EDGE_FAKE)) == 0);
1081 /* Match conditional jumps - this may get tricky when fallthru and branch
1082 edges are crossed. */
1083 if (bb1->succ
1084 && bb1->succ->succ_next
1085 && !bb1->succ->succ_next->succ_next
1086 && any_condjump_p (bb1->end)
1087 && onlyjump_p (bb1->end))
1089 edge b1, f1, b2, f2;
1090 bool reverse, match;
1091 rtx set1, set2, cond1, cond2;
1092 enum rtx_code code1, code2;
1094 if (!bb2->succ
1095 || !bb2->succ->succ_next
1096 || bb1->succ->succ_next->succ_next
1097 || !any_condjump_p (bb2->end)
1098 || !onlyjump_p (bb1->end))
1099 return false;
1101 b1 = BRANCH_EDGE (bb1);
1102 b2 = BRANCH_EDGE (bb2);
1103 f1 = FALLTHRU_EDGE (bb1);
1104 f2 = FALLTHRU_EDGE (bb2);
1106 /* Get around possible forwarders on fallthru edges. Other cases
1107 should be optimized out already. */
1108 if (FORWARDER_BLOCK_P (f1->dest))
1109 f1 = f1->dest->succ;
1111 if (FORWARDER_BLOCK_P (f2->dest))
1112 f2 = f2->dest->succ;
1114 /* To simplify use of this function, return false if there are
1115 unneeded forwarder blocks. These will get eliminated later
1116 during cleanup_cfg. */
1117 if (FORWARDER_BLOCK_P (f1->dest)
1118 || FORWARDER_BLOCK_P (f2->dest)
1119 || FORWARDER_BLOCK_P (b1->dest)
1120 || FORWARDER_BLOCK_P (b2->dest))
1121 return false;
1123 if (f1->dest == f2->dest && b1->dest == b2->dest)
1124 reverse = false;
1125 else if (f1->dest == b2->dest && b1->dest == f2->dest)
1126 reverse = true;
1127 else
1128 return false;
1130 set1 = pc_set (bb1->end);
1131 set2 = pc_set (bb2->end);
1132 if ((XEXP (SET_SRC (set1), 1) == pc_rtx)
1133 != (XEXP (SET_SRC (set2), 1) == pc_rtx))
1134 reverse = !reverse;
1136 cond1 = XEXP (SET_SRC (set1), 0);
1137 cond2 = XEXP (SET_SRC (set2), 0);
1138 code1 = GET_CODE (cond1);
1139 if (reverse)
1140 code2 = reversed_comparison_code (cond2, bb2->end);
1141 else
1142 code2 = GET_CODE (cond2);
1144 if (code2 == UNKNOWN)
1145 return false;
1147 /* Verify codes and operands match. */
1148 match = ((code1 == code2
1149 && rtx_renumbered_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
1150 && rtx_renumbered_equal_p (XEXP (cond1, 1), XEXP (cond2, 1)))
1151 || (code1 == swap_condition (code2)
1152 && rtx_renumbered_equal_p (XEXP (cond1, 1),
1153 XEXP (cond2, 0))
1154 && rtx_renumbered_equal_p (XEXP (cond1, 0),
1155 XEXP (cond2, 1))));
1157 /* If we return true, we will join the blocks. Which means that
1158 we will only have one branch prediction bit to work with. Thus
1159 we require the existing branches to have probabilities that are
1160 roughly similar. */
1161 if (match
1162 && !optimize_size
1163 && bb1->frequency > BB_FREQ_MAX / 1000
1164 && bb2->frequency > BB_FREQ_MAX / 1000)
1166 int prob2;
1168 if (b1->dest == b2->dest)
1169 prob2 = b2->probability;
1170 else
1171 /* Do not use f2 probability as f2 may be forwarded. */
1172 prob2 = REG_BR_PROB_BASE - b2->probability;
1174 /* Fail if the difference in probabilities is
1175 greater than 5%. */
1176 if (abs (b1->probability - prob2) > REG_BR_PROB_BASE / 20)
1178 if (rtl_dump_file)
1179 fprintf (rtl_dump_file,
1180 "Outcomes of branch in bb %i and %i differs to much (%i %i)\n",
1181 bb1->index, bb2->index, b1->probability, prob2);
1183 return false;
1187 if (rtl_dump_file && match)
1188 fprintf (rtl_dump_file, "Conditionals in bb %i and %i match.\n",
1189 bb1->index, bb2->index);
1191 return match;
1194 /* Generic case - we are seeing an computed jump, table jump or trapping
1195 instruction. */
1197 /* First ensure that the instructions match. There may be many outgoing
1198 edges so this test is generally cheaper.
1199 ??? Currently the tablejumps will never match, as they do have
1200 different tables. */
1201 if (!insns_match_p (mode, bb1->end, bb2->end))
1202 return false;
1204 /* Search the outgoing edges, ensure that the counts do match, find possible
1205 fallthru and exception handling edges since these needs more
1206 validation. */
1207 for (e1 = bb1->succ, e2 = bb2->succ; e1 && e2;
1208 e1 = e1->succ_next, e2 = e2->succ_next)
1210 if (e1->flags & EDGE_EH)
1211 nehedges1++;
1213 if (e2->flags & EDGE_EH)
1214 nehedges2++;
1216 if (e1->flags & EDGE_FALLTHRU)
1217 fallthru1 = e1;
1218 if (e2->flags & EDGE_FALLTHRU)
1219 fallthru2 = e2;
1222 /* If number of edges of various types does not match, fail. */
1223 if (e1 || e2
1224 || nehedges1 != nehedges2
1225 || (fallthru1 != 0) != (fallthru2 != 0))
1226 return false;
1228 /* fallthru edges must be forwarded to the same destination. */
1229 if (fallthru1)
1231 basic_block d1 = (forwarder_block_p (fallthru1->dest)
1232 ? fallthru1->dest->succ->dest: fallthru1->dest);
1233 basic_block d2 = (forwarder_block_p (fallthru2->dest)
1234 ? fallthru2->dest->succ->dest: fallthru2->dest);
1236 if (d1 != d2)
1237 return false;
1240 /* In case we do have EH edges, ensure we are in the same region. */
1241 if (nehedges1)
1243 rtx n1 = find_reg_note (bb1->end, REG_EH_REGION, 0);
1244 rtx n2 = find_reg_note (bb2->end, REG_EH_REGION, 0);
1246 if (XEXP (n1, 0) != XEXP (n2, 0))
1247 return false;
1250 /* We don't need to match the rest of edges as above checks should be enought
1251 to ensure that they are equivalent. */
1252 return true;
1255 /* E1 and E2 are edges with the same destination block. Search their
1256 predecessors for common code. If found, redirect control flow from
1257 (maybe the middle of) E1->SRC to (maybe the middle of) E2->SRC. */
1259 static bool
1260 try_crossjump_to_edge (mode, e1, e2)
1261 int mode;
1262 edge e1, e2;
1264 int nmatch;
1265 basic_block src1 = e1->src, src2 = e2->src;
1266 basic_block redirect_to;
1267 rtx newpos1, newpos2;
1268 edge s;
1269 rtx last;
1270 rtx label;
1272 /* Search backward through forwarder blocks. We don't need to worry
1273 about multiple entry or chained forwarders, as they will be optimized
1274 away. We do this to look past the unconditional jump following a
1275 conditional jump that is required due to the current CFG shape. */
1276 if (src1->pred
1277 && !src1->pred->pred_next
1278 && FORWARDER_BLOCK_P (src1))
1279 e1 = src1->pred, src1 = e1->src;
1281 if (src2->pred
1282 && !src2->pred->pred_next
1283 && FORWARDER_BLOCK_P (src2))
1284 e2 = src2->pred, src2 = e2->src;
1286 /* Nothing to do if we reach ENTRY, or a common source block. */
1287 if (src1 == ENTRY_BLOCK_PTR || src2 == ENTRY_BLOCK_PTR)
1288 return false;
1289 if (src1 == src2)
1290 return false;
1292 /* Seeing more than 1 forwarder blocks would confuse us later... */
1293 if (FORWARDER_BLOCK_P (e1->dest)
1294 && FORWARDER_BLOCK_P (e1->dest->succ->dest))
1295 return false;
1297 if (FORWARDER_BLOCK_P (e2->dest)
1298 && FORWARDER_BLOCK_P (e2->dest->succ->dest))
1299 return false;
1301 /* Likewise with dead code (possibly newly created by the other optimizations
1302 of cfg_cleanup). */
1303 if (!src1->pred || !src2->pred)
1304 return false;
1306 /* Look for the common insn sequence, part the first ... */
1307 if (!outgoing_edges_match (mode, src1, src2))
1308 return false;
1310 /* ... and part the second. */
1311 nmatch = flow_find_cross_jump (mode, src1, src2, &newpos1, &newpos2);
1312 if (!nmatch)
1313 return false;
1315 /* Avoid splitting if possible. */
1316 if (newpos2 == src2->head)
1317 redirect_to = src2;
1318 else
1320 if (rtl_dump_file)
1321 fprintf (rtl_dump_file, "Splitting bb %i before %i insns\n",
1322 src2->index, nmatch);
1323 redirect_to = split_block (src2, PREV_INSN (newpos2))->dest;
1326 if (rtl_dump_file)
1327 fprintf (rtl_dump_file,
1328 "Cross jumping from bb %i to bb %i; %i common insns\n",
1329 src1->index, src2->index, nmatch);
1331 redirect_to->count += src1->count;
1332 redirect_to->frequency += src1->frequency;
1334 /* Recompute the frequencies and counts of outgoing edges. */
1335 for (s = redirect_to->succ; s; s = s->succ_next)
1337 edge s2;
1338 basic_block d = s->dest;
1340 if (FORWARDER_BLOCK_P (d))
1341 d = d->succ->dest;
1343 for (s2 = src1->succ; ; s2 = s2->succ_next)
1345 basic_block d2 = s2->dest;
1346 if (FORWARDER_BLOCK_P (d2))
1347 d2 = d2->succ->dest;
1348 if (d == d2)
1349 break;
1352 s->count += s2->count;
1354 /* Take care to update possible forwarder blocks. We verified
1355 that there is no more than one in the chain, so we can't run
1356 into infinite loop. */
1357 if (FORWARDER_BLOCK_P (s->dest))
1359 s->dest->succ->count += s2->count;
1360 s->dest->count += s2->count;
1361 s->dest->frequency += EDGE_FREQUENCY (s);
1364 if (FORWARDER_BLOCK_P (s2->dest))
1366 s2->dest->succ->count -= s2->count;
1367 if (s2->dest->succ->count < 0)
1368 s2->dest->succ->count = 0;
1369 s2->dest->count -= s2->count;
1370 s2->dest->frequency -= EDGE_FREQUENCY (s);
1371 if (s2->dest->frequency < 0)
1372 s2->dest->frequency = 0;
1373 if (s2->dest->count < 0)
1374 s2->dest->count = 0;
1377 if (!redirect_to->frequency && !src1->frequency)
1378 s->probability = (s->probability + s2->probability) / 2;
1379 else
1380 s->probability
1381 = ((s->probability * redirect_to->frequency +
1382 s2->probability * src1->frequency)
1383 / (redirect_to->frequency + src1->frequency));
1386 update_br_prob_note (redirect_to);
1388 /* Edit SRC1 to go to REDIRECT_TO at NEWPOS1. */
1390 /* Skip possible basic block header. */
1391 if (GET_CODE (newpos1) == CODE_LABEL)
1392 newpos1 = NEXT_INSN (newpos1);
1394 if (GET_CODE (newpos1) == NOTE)
1395 newpos1 = NEXT_INSN (newpos1);
1396 last = src1->end;
1398 /* Emit the jump insn. */
1399 label = block_label (redirect_to);
1400 emit_jump_insn_after (gen_jump (label), src1->end);
1401 JUMP_LABEL (src1->end) = label;
1402 LABEL_NUSES (label)++;
1404 /* Delete the now unreachable instructions. */
1405 delete_insn_chain (newpos1, last);
1407 /* Make sure there is a barrier after the new jump. */
1408 last = next_nonnote_insn (src1->end);
1409 if (!last || GET_CODE (last) != BARRIER)
1410 emit_barrier_after (src1->end);
1412 /* Update CFG. */
1413 while (src1->succ)
1414 remove_edge (src1->succ);
1415 make_single_succ_edge (src1, redirect_to, 0);
1417 BB_SET_FLAG (src1, BB_UPDATE_LIFE);
1418 update_forwarder_flag (src1);
1420 return true;
1423 /* Search the predecessors of BB for common insn sequences. When found,
1424 share code between them by redirecting control flow. Return true if
1425 any changes made. */
1427 static bool
1428 try_crossjump_bb (mode, bb)
1429 int mode;
1430 basic_block bb;
1432 edge e, e2, nexte2, nexte, fallthru;
1433 bool changed;
1435 /* Nothing to do if there is not at least two incoming edges. */
1436 if (!bb->pred || !bb->pred->pred_next)
1437 return false;
1439 /* It is always cheapest to redirect a block that ends in a branch to
1440 a block that falls through into BB, as that adds no branches to the
1441 program. We'll try that combination first. */
1442 for (fallthru = bb->pred; fallthru; fallthru = fallthru->pred_next)
1443 if (fallthru->flags & EDGE_FALLTHRU)
1444 break;
1446 changed = false;
1447 for (e = bb->pred; e; e = nexte)
1449 nexte = e->pred_next;
1451 /* As noted above, first try with the fallthru predecessor. */
1452 if (fallthru)
1454 /* Don't combine the fallthru edge into anything else.
1455 If there is a match, we'll do it the other way around. */
1456 if (e == fallthru)
1457 continue;
1459 if (try_crossjump_to_edge (mode, e, fallthru))
1461 changed = true;
1462 nexte = bb->pred;
1463 continue;
1467 /* Non-obvious work limiting check: Recognize that we're going
1468 to call try_crossjump_bb on every basic block. So if we have
1469 two blocks with lots of outgoing edges (a switch) and they
1470 share lots of common destinations, then we would do the
1471 cross-jump check once for each common destination.
1473 Now, if the blocks actually are cross-jump candidates, then
1474 all of their destinations will be shared. Which means that
1475 we only need check them for cross-jump candidacy once. We
1476 can eliminate redundant checks of crossjump(A,B) by arbitrarily
1477 choosing to do the check from the block for which the edge
1478 in question is the first successor of A. */
1479 if (e->src->succ != e)
1480 continue;
1482 for (e2 = bb->pred; e2; e2 = nexte2)
1484 nexte2 = e2->pred_next;
1486 if (e2 == e)
1487 continue;
1489 /* We've already checked the fallthru edge above. */
1490 if (e2 == fallthru)
1491 continue;
1493 /* The "first successor" check above only prevents multiple
1494 checks of crossjump(A,B). In order to prevent redundant
1495 checks of crossjump(B,A), require that A be the block
1496 with the lowest index. */
1497 if (e->src->index > e2->src->index)
1498 continue;
1500 if (try_crossjump_to_edge (mode, e, e2))
1502 changed = true;
1503 nexte = bb->pred;
1504 break;
1509 return changed;
1512 /* Do simple CFG optimizations - basic block merging, simplifying of jump
1513 instructions etc. Return nonzero if changes were made. */
1515 static bool
1516 try_optimize_cfg (mode)
1517 int mode;
1519 int i;
1520 bool changed_overall = false;
1521 bool changed;
1522 int iterations = 0;
1523 sbitmap blocks;
1525 if (mode & CLEANUP_CROSSJUMP)
1526 add_noreturn_fake_exit_edges ();
1528 for (i = 0; i < n_basic_blocks; i++)
1529 update_forwarder_flag (BASIC_BLOCK (i));
1531 /* Attempt to merge blocks as made possible by edge removal. If a block
1532 has only one successor, and the successor has only one predecessor,
1533 they may be combined. */
1536 changed = false;
1537 iterations++;
1539 if (rtl_dump_file)
1540 fprintf (rtl_dump_file, "\n\ntry_optimize_cfg iteration %i\n\n",
1541 iterations);
1543 for (i = 0; i < n_basic_blocks;)
1545 basic_block c, b = BASIC_BLOCK (i);
1546 edge s;
1547 bool changed_here = false;
1549 /* Delete trivially dead basic blocks. */
1550 while (b->pred == NULL)
1552 c = BASIC_BLOCK (b->index - 1);
1553 if (rtl_dump_file)
1554 fprintf (rtl_dump_file, "Deleting block %i.\n", b->index);
1556 flow_delete_block (b);
1557 changed = true;
1558 b = c;
1561 /* Remove code labels no longer used. Don't do this before
1562 CALL_PLACEHOLDER is removed, as some branches may be hidden
1563 within. */
1564 if (b->pred->pred_next == NULL
1565 && (b->pred->flags & EDGE_FALLTHRU)
1566 && !(b->pred->flags & EDGE_COMPLEX)
1567 && GET_CODE (b->head) == CODE_LABEL
1568 && (!(mode & CLEANUP_PRE_SIBCALL)
1569 || !tail_recursion_label_p (b->head))
1570 /* If the previous block ends with a branch to this block,
1571 we can't delete the label. Normally this is a condjump
1572 that is yet to be simplified, but if CASE_DROPS_THRU,
1573 this can be a tablejump with some element going to the
1574 same place as the default (fallthru). */
1575 && (b->pred->src == ENTRY_BLOCK_PTR
1576 || GET_CODE (b->pred->src->end) != JUMP_INSN
1577 || ! label_is_jump_target_p (b->head, b->pred->src->end)))
1579 rtx label = b->head;
1581 b->head = NEXT_INSN (b->head);
1582 delete_insn_chain (label, label);
1583 if (rtl_dump_file)
1584 fprintf (rtl_dump_file, "Deleted label in block %i.\n",
1585 b->index);
1588 /* If we fall through an empty block, we can remove it. */
1589 if (b->pred->pred_next == NULL
1590 && (b->pred->flags & EDGE_FALLTHRU)
1591 && GET_CODE (b->head) != CODE_LABEL
1592 && FORWARDER_BLOCK_P (b)
1593 /* Note that forwarder_block_p true ensures that there
1594 is a successor for this block. */
1595 && (b->succ->flags & EDGE_FALLTHRU)
1596 && n_basic_blocks > 1)
1598 if (rtl_dump_file)
1599 fprintf (rtl_dump_file, "Deleting fallthru block %i.\n",
1600 b->index);
1602 c = BASIC_BLOCK (b->index ? b->index - 1 : 1);
1603 redirect_edge_succ_nodup (b->pred, b->succ->dest);
1604 flow_delete_block (b);
1605 changed = true;
1606 b = c;
1609 /* Merge blocks. Loop because chains of blocks might be
1610 combineable. */
1611 while ((s = b->succ) != NULL
1612 && s->succ_next == NULL
1613 && !(s->flags & EDGE_COMPLEX)
1614 && (c = s->dest) != EXIT_BLOCK_PTR
1615 && c->pred->pred_next == NULL
1616 /* If the jump insn has side effects,
1617 we can't kill the edge. */
1618 && (GET_CODE (b->end) != JUMP_INSN
1619 || onlyjump_p (b->end))
1620 && merge_blocks (s, b, c, mode))
1621 changed_here = true;
1623 /* Simplify branch over branch. */
1624 if ((mode & CLEANUP_EXPENSIVE) && try_simplify_condjump (b))
1626 BB_SET_FLAG (b, BB_UPDATE_LIFE);
1627 changed_here = true;
1630 /* If B has a single outgoing edge, but uses a non-trivial jump
1631 instruction without side-effects, we can either delete the
1632 jump entirely, or replace it with a simple unconditional jump.
1633 Use redirect_edge_and_branch to do the dirty work. */
1634 if (b->succ
1635 && ! b->succ->succ_next
1636 && b->succ->dest != EXIT_BLOCK_PTR
1637 && onlyjump_p (b->end)
1638 && redirect_edge_and_branch (b->succ, b->succ->dest))
1640 BB_SET_FLAG (b, BB_UPDATE_LIFE);
1641 update_forwarder_flag (b);
1642 changed_here = true;
1645 /* Simplify branch to branch. */
1646 if (try_forward_edges (mode, b))
1647 changed_here = true;
1649 /* Look for shared code between blocks. */
1650 if ((mode & CLEANUP_CROSSJUMP)
1651 && try_crossjump_bb (mode, b))
1652 changed_here = true;
1654 /* Don't get confused by the index shift caused by deleting
1655 blocks. */
1656 if (!changed_here)
1657 i = b->index + 1;
1658 else
1659 changed = true;
1662 if ((mode & CLEANUP_CROSSJUMP)
1663 && try_crossjump_bb (mode, EXIT_BLOCK_PTR))
1664 changed = true;
1666 #ifdef ENABLE_CHECKING
1667 if (changed)
1668 verify_flow_info ();
1669 #endif
1671 changed_overall |= changed;
1673 while (changed);
1675 if (mode & CLEANUP_CROSSJUMP)
1676 remove_fake_edges ();
1678 if ((mode & CLEANUP_UPDATE_LIFE) && changed_overall)
1680 bool found = 0;
1682 blocks = sbitmap_alloc (n_basic_blocks);
1683 sbitmap_zero (blocks);
1684 for (i = 0; i < n_basic_blocks; i++)
1685 if (BB_FLAGS (BASIC_BLOCK (i)) & BB_UPDATE_LIFE)
1687 found = 1;
1688 SET_BIT (blocks, i);
1691 if (found)
1692 update_life_info (blocks, UPDATE_LIFE_GLOBAL,
1693 PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
1694 | PROP_KILL_DEAD_CODE);
1695 sbitmap_free (blocks);
1698 for (i = 0; i < n_basic_blocks; i++)
1699 BASIC_BLOCK (i)->aux = NULL;
1701 return changed_overall;
1704 /* Delete all unreachable basic blocks. */
1706 static bool
1707 delete_unreachable_blocks ()
1709 int i;
1710 bool changed = false;
1712 find_unreachable_blocks ();
1714 /* Delete all unreachable basic blocks. Count down so that we
1715 don't interfere with the block renumbering that happens in
1716 flow_delete_block. */
1718 for (i = n_basic_blocks - 1; i >= 0; --i)
1720 basic_block b = BASIC_BLOCK (i);
1722 if (!(b->flags & BB_REACHABLE))
1723 flow_delete_block (b), changed = true;
1726 if (changed)
1727 tidy_fallthru_edges ();
1728 return changed;
1731 /* Tidy the CFG by deleting unreachable code and whatnot. */
1733 bool
1734 cleanup_cfg (mode)
1735 int mode;
1737 bool changed = false;
1739 timevar_push (TV_CLEANUP_CFG);
1740 changed = delete_unreachable_blocks ();
1741 if (try_optimize_cfg (mode))
1742 delete_unreachable_blocks (), changed = true;
1744 /* Kill the data we won't maintain. */
1745 free_EXPR_LIST_list (&label_value_list);
1746 free_EXPR_LIST_list (&tail_recursion_label_list);
1747 timevar_pop (TV_CLEANUP_CFG);
1749 return changed;