2002-02-19 Philip Blundell <philb@gnu.org>
[official-gcc.git] / gcc / cfgcleanup.c
blobd9f9cf261e5c10afbe7dcfabd6a22d1b1f9b4f0b
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"
47 #include "target.h"
49 #include "obstack.h"
51 /* cleanup_cfg maintains following flags for each basic block. */
53 enum bb_flags
55 /* Set if life info needs to be recomputed for given BB. */
56 BB_UPDATE_LIFE = 1,
57 /* Set if BB is the forwarder block to avoid too many
58 forwarder_block_p calls. */
59 BB_FORWARDER_BLOCK = 2
62 #define BB_FLAGS(BB) (enum bb_flags) (BB)->aux
63 #define BB_SET_FLAG(BB, FLAG) \
64 (BB)->aux = (void *) (long) ((enum bb_flags) (BB)->aux | (FLAG))
65 #define BB_CLEAR_FLAG(BB, FLAG) \
66 (BB)->aux = (void *) (long) ((enum bb_flags) (BB)->aux & ~(FLAG))
68 #define FORWARDER_BLOCK_P(BB) (BB_FLAGS (BB) & BB_FORWARDER_BLOCK)
70 static bool try_crossjump_to_edge PARAMS ((int, edge, edge));
71 static bool try_crossjump_bb PARAMS ((int, basic_block));
72 static bool outgoing_edges_match PARAMS ((int,
73 basic_block, basic_block));
74 static int flow_find_cross_jump PARAMS ((int, basic_block, basic_block,
75 rtx *, rtx *));
76 static bool insns_match_p PARAMS ((int, rtx, rtx));
78 static bool delete_unreachable_blocks PARAMS ((void));
79 static bool label_is_jump_target_p PARAMS ((rtx, rtx));
80 static bool tail_recursion_label_p PARAMS ((rtx));
81 static void merge_blocks_move_predecessor_nojumps PARAMS ((basic_block,
82 basic_block));
83 static void merge_blocks_move_successor_nojumps PARAMS ((basic_block,
84 basic_block));
85 static bool merge_blocks PARAMS ((edge,basic_block,basic_block,
86 int));
87 static bool try_optimize_cfg PARAMS ((int));
88 static bool try_simplify_condjump PARAMS ((basic_block));
89 static bool try_forward_edges PARAMS ((int, basic_block));
90 static edge thread_jump PARAMS ((int, edge, basic_block));
91 static bool mark_effect PARAMS ((rtx, bitmap));
92 static void notice_new_block PARAMS ((basic_block));
93 static void update_forwarder_flag PARAMS ((basic_block));
95 /* Set flags for newly created block. */
97 static void
98 notice_new_block (bb)
99 basic_block bb;
101 if (!bb)
102 return;
104 BB_SET_FLAG (bb, BB_UPDATE_LIFE);
105 if (forwarder_block_p (bb))
106 BB_SET_FLAG (bb, BB_FORWARDER_BLOCK);
109 /* Recompute forwarder flag after block has been modified. */
111 static void
112 update_forwarder_flag (bb)
113 basic_block bb;
115 if (forwarder_block_p (bb))
116 BB_SET_FLAG (bb, BB_FORWARDER_BLOCK);
117 else
118 BB_CLEAR_FLAG (bb, BB_FORWARDER_BLOCK);
121 /* Simplify a conditional jump around an unconditional jump.
122 Return true if something changed. */
124 static bool
125 try_simplify_condjump (cbranch_block)
126 basic_block cbranch_block;
128 basic_block jump_block, jump_dest_block, cbranch_dest_block;
129 edge cbranch_jump_edge, cbranch_fallthru_edge;
130 rtx cbranch_insn;
132 /* Verify that there are exactly two successors. */
133 if (!cbranch_block->succ
134 || !cbranch_block->succ->succ_next
135 || cbranch_block->succ->succ_next->succ_next)
136 return false;
138 /* Verify that we've got a normal conditional branch at the end
139 of the block. */
140 cbranch_insn = cbranch_block->end;
141 if (!any_condjump_p (cbranch_insn))
142 return false;
144 cbranch_fallthru_edge = FALLTHRU_EDGE (cbranch_block);
145 cbranch_jump_edge = BRANCH_EDGE (cbranch_block);
147 /* The next block must not have multiple predecessors, must not
148 be the last block in the function, and must contain just the
149 unconditional jump. */
150 jump_block = cbranch_fallthru_edge->dest;
151 if (jump_block->pred->pred_next
152 || jump_block->index == n_basic_blocks - 1
153 || !FORWARDER_BLOCK_P (jump_block))
154 return false;
155 jump_dest_block = jump_block->succ->dest;
157 /* The conditional branch must target the block after the
158 unconditional branch. */
159 cbranch_dest_block = cbranch_jump_edge->dest;
161 if (!can_fallthru (jump_block, cbranch_dest_block))
162 return false;
164 /* Invert the conditional branch. */
165 if (!invert_jump (cbranch_insn, block_label (jump_dest_block), 0))
166 return false;
168 if (rtl_dump_file)
169 fprintf (rtl_dump_file, "Simplifying condjump %i around jump %i\n",
170 INSN_UID (cbranch_insn), INSN_UID (jump_block->end));
172 /* Success. Update the CFG to match. Note that after this point
173 the edge variable names appear backwards; the redirection is done
174 this way to preserve edge profile data. */
175 cbranch_jump_edge = redirect_edge_succ_nodup (cbranch_jump_edge,
176 cbranch_dest_block);
177 cbranch_fallthru_edge = redirect_edge_succ_nodup (cbranch_fallthru_edge,
178 jump_dest_block);
179 cbranch_jump_edge->flags |= EDGE_FALLTHRU;
180 cbranch_fallthru_edge->flags &= ~EDGE_FALLTHRU;
181 update_br_prob_note (cbranch_block);
183 /* Delete the block with the unconditional jump, and clean up the mess. */
184 flow_delete_block (jump_block);
185 tidy_fallthru_edge (cbranch_jump_edge, cbranch_block, cbranch_dest_block);
187 return true;
190 /* Attempt to prove that operation is NOOP using CSElib or mark the effect
191 on register. Used by jump threading. */
193 static bool
194 mark_effect (exp, nonequal)
195 rtx exp;
196 regset nonequal;
198 int regno;
199 rtx dest;
200 switch (GET_CODE (exp))
202 /* In case we do clobber the register, mark it as equal, as we know the
203 value is dead so it don't have to match. */
204 case CLOBBER:
205 if (REG_P (XEXP (exp, 0)))
207 dest = XEXP (exp, 0);
208 regno = REGNO (dest);
209 CLEAR_REGNO_REG_SET (nonequal, regno);
210 if (regno < FIRST_PSEUDO_REGISTER)
212 int n = HARD_REGNO_NREGS (regno, GET_MODE (dest));
213 while (--n > 0)
214 CLEAR_REGNO_REG_SET (nonequal, regno + n);
217 return false;
219 case SET:
220 if (rtx_equal_for_cselib_p (SET_DEST (exp), SET_SRC (exp)))
221 return false;
222 dest = SET_DEST (exp);
223 if (dest == pc_rtx)
224 return false;
225 if (!REG_P (dest))
226 return true;
227 regno = REGNO (dest);
228 SET_REGNO_REG_SET (nonequal, regno);
229 if (regno < FIRST_PSEUDO_REGISTER)
231 int n = HARD_REGNO_NREGS (regno, GET_MODE (dest));
232 while (--n > 0)
233 SET_REGNO_REG_SET (nonequal, regno + n);
235 return false;
237 default:
238 return false;
241 /* Attempt to prove that the basic block B will have no side effects and
242 allways continues in the same edge if reached via E. Return the edge
243 if exist, NULL otherwise. */
245 static edge
246 thread_jump (mode, e, b)
247 int mode;
248 edge e;
249 basic_block b;
251 rtx set1, set2, cond1, cond2, insn;
252 enum rtx_code code1, code2, reversed_code2;
253 bool reverse1 = false;
254 int i;
255 regset nonequal;
256 bool failed = false;
258 /* At the moment, we do handle only conditional jumps, but later we may
259 want to extend this code to tablejumps and others. */
260 if (!e->src->succ->succ_next || e->src->succ->succ_next->succ_next)
261 return NULL;
262 if (!b->succ || !b->succ->succ_next || b->succ->succ_next->succ_next)
263 return NULL;
265 /* Second branch must end with onlyjump, as we will eliminate the jump. */
266 if (!any_condjump_p (e->src->end) || !any_condjump_p (b->end)
267 || !onlyjump_p (b->end))
268 return NULL;
270 set1 = pc_set (e->src->end);
271 set2 = pc_set (b->end);
272 if (((e->flags & EDGE_FALLTHRU) != 0)
273 != (XEXP (SET_SRC (set1), 1) == pc_rtx))
274 reverse1 = true;
276 cond1 = XEXP (SET_SRC (set1), 0);
277 cond2 = XEXP (SET_SRC (set2), 0);
278 if (reverse1)
279 code1 = reversed_comparison_code (cond1, e->src->end);
280 else
281 code1 = GET_CODE (cond1);
283 code2 = GET_CODE (cond2);
284 reversed_code2 = reversed_comparison_code (cond2, b->end);
286 if (!comparison_dominates_p (code1, code2)
287 && !comparison_dominates_p (code1, reversed_code2))
288 return NULL;
290 /* Ensure that the comparison operators are equivalent.
291 ??? This is far too pesimistic. We should allow swapped operands,
292 different CCmodes, or for example comparisons for interval, that
293 dominate even when operands are not equivalent. */
294 if (!rtx_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
295 || !rtx_equal_p (XEXP (cond1, 1), XEXP (cond2, 1)))
296 return NULL;
298 /* Short circuit cases where block B contains some side effects, as we can't
299 safely bypass it. */
300 for (insn = NEXT_INSN (b->head); insn != NEXT_INSN (b->end);
301 insn = NEXT_INSN (insn))
302 if (INSN_P (insn) && side_effects_p (PATTERN (insn)))
303 return NULL;
305 cselib_init ();
307 /* First process all values computed in the source basic block. */
308 for (insn = NEXT_INSN (e->src->head); insn != NEXT_INSN (e->src->end);
309 insn = NEXT_INSN (insn))
310 if (INSN_P (insn))
311 cselib_process_insn (insn);
313 nonequal = BITMAP_XMALLOC();
314 CLEAR_REG_SET (nonequal);
316 /* Now assume that we've continued by the edge E to B and continue
317 processing as if it were same basic block.
318 Our goal is to prove that whole block is an NOOP. */
320 for (insn = NEXT_INSN (b->head); insn != NEXT_INSN (b->end) && !failed;
321 insn = NEXT_INSN (insn))
323 if (INSN_P (insn))
325 rtx pat = PATTERN (insn);
327 if (GET_CODE (pat) == PARALLEL)
329 for (i = 0; i < XVECLEN (pat, 0); i++)
330 failed |= mark_effect (XVECEXP (pat, 0, i), nonequal);
332 else
333 failed |= mark_effect (pat, nonequal);
336 cselib_process_insn (insn);
339 /* Later we should clear nonequal of dead registers. So far we don't
340 have life information in cfg_cleanup. */
341 if (failed)
342 goto failed_exit;
344 /* In case liveness information is available, we need to prove equivalence
345 only of the live values. */
346 if (mode & CLEANUP_UPDATE_LIFE)
347 AND_REG_SET (nonequal, b->global_live_at_end);
349 EXECUTE_IF_SET_IN_REG_SET (nonequal, 0, i, goto failed_exit;);
351 BITMAP_XFREE (nonequal);
352 cselib_finish ();
353 if ((comparison_dominates_p (code1, code2) != 0)
354 != (XEXP (SET_SRC (set2), 1) == pc_rtx))
355 return BRANCH_EDGE (b);
356 else
357 return FALLTHRU_EDGE (b);
359 failed_exit:
360 BITMAP_XFREE (nonequal);
361 cselib_finish ();
362 return NULL;
365 /* Attempt to forward edges leaving basic block B.
366 Return true if successful. */
368 static bool
369 try_forward_edges (mode, b)
370 basic_block b;
371 int mode;
373 bool changed = false;
374 edge e, next, *threaded_edges = NULL;
376 for (e = b->succ; e; e = next)
378 basic_block target, first;
379 int counter;
380 bool threaded = false;
381 int nthreaded_edges = 0;
383 next = e->succ_next;
385 /* Skip complex edges because we don't know how to update them.
387 Still handle fallthru edges, as we can succeed to forward fallthru
388 edge to the same place as the branch edge of conditional branch
389 and turn conditional branch to an unconditional branch. */
390 if (e->flags & EDGE_COMPLEX)
391 continue;
393 target = first = e->dest;
394 counter = 0;
396 while (counter < n_basic_blocks)
398 basic_block new_target = NULL;
399 bool new_target_threaded = false;
401 if (FORWARDER_BLOCK_P (target)
402 && target->succ->dest != EXIT_BLOCK_PTR)
404 /* Bypass trivial infinite loops. */
405 if (target == target->succ->dest)
406 counter = n_basic_blocks;
407 new_target = target->succ->dest;
410 /* Allow to thread only over one edge at time to simplify updating
411 of probabilities. */
412 else if (mode & CLEANUP_THREADING)
414 edge t = thread_jump (mode, e, target);
415 if (t)
417 if (!threaded_edges)
418 threaded_edges = xmalloc (sizeof (*threaded_edges)
419 * n_basic_blocks);
420 else
422 int i;
424 /* Detect an infinite loop across blocks not
425 including the start block. */
426 for (i = 0; i < nthreaded_edges; ++i)
427 if (threaded_edges[i] == t)
428 break;
429 if (i < nthreaded_edges)
431 counter = n_basic_blocks;
432 break;
436 /* Detect an infinite loop across the start block. */
437 if (t->dest == b)
438 break;
440 if (nthreaded_edges >= n_basic_blocks)
441 abort ();
442 threaded_edges[nthreaded_edges++] = t;
444 new_target = t->dest;
445 new_target_threaded = true;
449 if (!new_target)
450 break;
452 /* Avoid killing of loop pre-headers, as it is the place loop
453 optimizer wants to hoist code to.
455 For fallthru forwarders, the LOOP_BEG note must appear between
456 the header of block and CODE_LABEL of the loop, for non forwarders
457 it must appear before the JUMP_INSN. */
458 if (mode & CLEANUP_PRE_LOOP)
460 rtx insn = (target->succ->flags & EDGE_FALLTHRU
461 ? target->head : prev_nonnote_insn (target->end));
463 if (GET_CODE (insn) != NOTE)
464 insn = NEXT_INSN (insn);
466 for (; insn && GET_CODE (insn) != CODE_LABEL && !INSN_P (insn);
467 insn = NEXT_INSN (insn))
468 if (GET_CODE (insn) == NOTE
469 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
470 break;
472 if (GET_CODE (insn) == NOTE)
473 break;
476 counter++;
477 target = new_target;
478 threaded |= new_target_threaded;
481 if (counter >= n_basic_blocks)
483 if (rtl_dump_file)
484 fprintf (rtl_dump_file, "Infinite loop in BB %i.\n",
485 target->index);
487 else if (target == first)
488 ; /* We didn't do anything. */
489 else
491 /* Save the values now, as the edge may get removed. */
492 gcov_type edge_count = e->count;
493 int edge_probability = e->probability;
494 int edge_frequency;
495 int n = 0;
497 /* Don't force if target is exit block. */
498 if (threaded && target != EXIT_BLOCK_PTR)
500 notice_new_block (redirect_edge_and_branch_force (e, target));
501 if (rtl_dump_file)
502 fprintf (rtl_dump_file, "Conditionals threaded.\n");
504 else if (!redirect_edge_and_branch (e, target))
506 if (rtl_dump_file)
507 fprintf (rtl_dump_file,
508 "Forwarding edge %i->%i to %i failed.\n",
509 b->index, e->dest->index, target->index);
510 continue;
513 /* We successfully forwarded the edge. Now update profile
514 data: for each edge we traversed in the chain, remove
515 the original edge's execution count. */
516 edge_frequency = ((edge_probability * b->frequency
517 + REG_BR_PROB_BASE / 2)
518 / REG_BR_PROB_BASE);
520 if (!FORWARDER_BLOCK_P (b) && forwarder_block_p (b))
521 BB_SET_FLAG (b, BB_FORWARDER_BLOCK);
522 BB_SET_FLAG (b, BB_UPDATE_LIFE);
526 edge t;
528 first->count -= edge_count;
529 if (first->count < 0)
530 first->count = 0;
531 first->frequency -= edge_frequency;
532 if (first->frequency < 0)
533 first->frequency = 0;
534 if (first->succ->succ_next)
536 edge e;
537 int prob;
538 if (n >= nthreaded_edges)
539 abort ();
540 t = threaded_edges [n++];
541 if (t->src != first)
542 abort ();
543 if (first->frequency)
544 prob = edge_frequency * REG_BR_PROB_BASE / first->frequency;
545 else
546 prob = 0;
547 if (prob > t->probability)
548 prob = t->probability;
549 t->probability -= prob;
550 prob = REG_BR_PROB_BASE - prob;
551 if (prob <= 0)
553 first->succ->probability = REG_BR_PROB_BASE;
554 first->succ->succ_next->probability = 0;
556 else
557 for (e = first->succ; e; e = e->succ_next)
558 e->probability = ((e->probability * REG_BR_PROB_BASE)
559 / (double) prob);
560 update_br_prob_note (first);
562 else
564 /* It is possible that as the result of
565 threading we've removed edge as it is
566 threaded to the fallthru edge. Avoid
567 getting out of sync. */
568 if (n < nthreaded_edges
569 && first == threaded_edges [n]->src)
570 n++;
571 t = first->succ;
574 t->count -= edge_count;
575 if (t->count < 0)
576 t->count = 0;
577 first = t->dest;
579 while (first != target);
581 changed = true;
585 if (threaded_edges)
586 free (threaded_edges);
587 return changed;
590 /* Return true if LABEL is a target of JUMP_INSN. This applies only
591 to non-complex jumps. That is, direct unconditional, conditional,
592 and tablejumps, but not computed jumps or returns. It also does
593 not apply to the fallthru case of a conditional jump. */
595 static bool
596 label_is_jump_target_p (label, jump_insn)
597 rtx label, jump_insn;
599 rtx tmp = JUMP_LABEL (jump_insn);
601 if (label == tmp)
602 return true;
604 if (tmp != NULL_RTX
605 && (tmp = NEXT_INSN (tmp)) != NULL_RTX
606 && GET_CODE (tmp) == JUMP_INSN
607 && (tmp = PATTERN (tmp),
608 GET_CODE (tmp) == ADDR_VEC
609 || GET_CODE (tmp) == ADDR_DIFF_VEC))
611 rtvec vec = XVEC (tmp, GET_CODE (tmp) == ADDR_DIFF_VEC);
612 int i, veclen = GET_NUM_ELEM (vec);
614 for (i = 0; i < veclen; ++i)
615 if (XEXP (RTVEC_ELT (vec, i), 0) == label)
616 return true;
619 return false;
622 /* Return true if LABEL is used for tail recursion. */
624 static bool
625 tail_recursion_label_p (label)
626 rtx label;
628 rtx x;
630 for (x = tail_recursion_label_list; x; x = XEXP (x, 1))
631 if (label == XEXP (x, 0))
632 return true;
634 return false;
637 /* Blocks A and B are to be merged into a single block. A has no incoming
638 fallthru edge, so it can be moved before B without adding or modifying
639 any jumps (aside from the jump from A to B). */
641 static void
642 merge_blocks_move_predecessor_nojumps (a, b)
643 basic_block a, b;
645 rtx barrier;
646 int index;
648 barrier = next_nonnote_insn (a->end);
649 if (GET_CODE (barrier) != BARRIER)
650 abort ();
651 delete_insn (barrier);
653 /* Move block and loop notes out of the chain so that we do not
654 disturb their order.
656 ??? A better solution would be to squeeze out all the non-nested notes
657 and adjust the block trees appropriately. Even better would be to have
658 a tighter connection between block trees and rtl so that this is not
659 necessary. */
660 if (squeeze_notes (&a->head, &a->end))
661 abort ();
663 /* Scramble the insn chain. */
664 if (a->end != PREV_INSN (b->head))
665 reorder_insns_nobb (a->head, a->end, PREV_INSN (b->head));
666 BB_SET_FLAG (a, BB_UPDATE_LIFE);
668 if (rtl_dump_file)
669 fprintf (rtl_dump_file, "Moved block %d before %d and merged.\n",
670 a->index, b->index);
672 /* Swap the records for the two blocks around. Although we are deleting B,
673 A is now where B was and we want to compact the BB array from where
674 A used to be. */
675 BASIC_BLOCK (a->index) = b;
676 BASIC_BLOCK (b->index) = a;
677 index = a->index;
678 a->index = b->index;
679 b->index = index;
681 /* Now blocks A and B are contiguous. Merge them. */
682 merge_blocks_nomove (a, b);
685 /* Blocks A and B are to be merged into a single block. B has no outgoing
686 fallthru edge, so it can be moved after A without adding or modifying
687 any jumps (aside from the jump from A to B). */
689 static void
690 merge_blocks_move_successor_nojumps (a, b)
691 basic_block a, b;
693 rtx barrier, real_b_end;
695 real_b_end = b->end;
696 barrier = NEXT_INSN (b->end);
698 /* Recognize a jump table following block B. */
699 if (barrier
700 && GET_CODE (barrier) == CODE_LABEL
701 && NEXT_INSN (barrier)
702 && GET_CODE (NEXT_INSN (barrier)) == JUMP_INSN
703 && (GET_CODE (PATTERN (NEXT_INSN (barrier))) == ADDR_VEC
704 || GET_CODE (PATTERN (NEXT_INSN (barrier))) == ADDR_DIFF_VEC))
706 /* Temporarily add the table jump insn to b, so that it will also
707 be moved to the correct location. */
708 b->end = NEXT_INSN (barrier);
709 barrier = NEXT_INSN (b->end);
712 /* There had better have been a barrier there. Delete it. */
713 if (barrier && GET_CODE (barrier) == BARRIER)
714 delete_insn (barrier);
716 /* Move block and loop notes out of the chain so that we do not
717 disturb their order.
719 ??? A better solution would be to squeeze out all the non-nested notes
720 and adjust the block trees appropriately. Even better would be to have
721 a tighter connection between block trees and rtl so that this is not
722 necessary. */
723 if (squeeze_notes (&b->head, &b->end))
724 abort ();
726 /* Scramble the insn chain. */
727 reorder_insns_nobb (b->head, b->end, a->end);
729 /* Restore the real end of b. */
730 b->end = real_b_end;
732 /* Now blocks A and B are contiguous. Merge them. */
733 merge_blocks_nomove (a, b);
734 BB_SET_FLAG (a, BB_UPDATE_LIFE);
736 if (rtl_dump_file)
737 fprintf (rtl_dump_file, "Moved block %d after %d and merged.\n",
738 b->index, a->index);
741 /* Attempt to merge basic blocks that are potentially non-adjacent.
742 Return true iff the attempt succeeded. */
744 static bool
745 merge_blocks (e, b, c, mode)
746 edge e;
747 basic_block b, c;
748 int mode;
750 /* If C has a tail recursion label, do not merge. There is no
751 edge recorded from the call_placeholder back to this label, as
752 that would make optimize_sibling_and_tail_recursive_calls more
753 complex for no gain. */
754 if ((mode & CLEANUP_PRE_SIBCALL)
755 && GET_CODE (c->head) == CODE_LABEL
756 && tail_recursion_label_p (c->head))
757 return false;
759 /* If B has a fallthru edge to C, no need to move anything. */
760 if (e->flags & EDGE_FALLTHRU)
762 int b_index = b->index, c_index = c->index;
763 /* We need to update liveness in case C already has broken liveness
764 or B ends by conditional jump to next instructions that will be
765 removed. */
766 if ((BB_FLAGS (c) & BB_UPDATE_LIFE)
767 || GET_CODE (b->end) == JUMP_INSN)
768 BB_SET_FLAG (b, BB_UPDATE_LIFE);
769 merge_blocks_nomove (b, c);
770 update_forwarder_flag (b);
772 if (rtl_dump_file)
773 fprintf (rtl_dump_file, "Merged %d and %d without moving.\n",
774 b_index, c_index);
776 return true;
779 /* Otherwise we will need to move code around. Do that only if expensive
780 transformations are allowed. */
781 else if (mode & CLEANUP_EXPENSIVE)
783 edge tmp_edge, b_fallthru_edge;
784 bool c_has_outgoing_fallthru;
785 bool b_has_incoming_fallthru;
787 /* Avoid overactive code motion, as the forwarder blocks should be
788 eliminated by edge redirection instead. One exception might have
789 been if B is a forwarder block and C has no fallthru edge, but
790 that should be cleaned up by bb-reorder instead. */
791 if (FORWARDER_BLOCK_P (b) || FORWARDER_BLOCK_P (c))
792 return false;
794 /* We must make sure to not munge nesting of lexical blocks,
795 and loop notes. This is done by squeezing out all the notes
796 and leaving them there to lie. Not ideal, but functional. */
798 for (tmp_edge = c->succ; tmp_edge; tmp_edge = tmp_edge->succ_next)
799 if (tmp_edge->flags & EDGE_FALLTHRU)
800 break;
802 c_has_outgoing_fallthru = (tmp_edge != NULL);
804 for (tmp_edge = b->pred; tmp_edge; tmp_edge = tmp_edge->pred_next)
805 if (tmp_edge->flags & EDGE_FALLTHRU)
806 break;
808 b_has_incoming_fallthru = (tmp_edge != NULL);
809 b_fallthru_edge = tmp_edge;
811 /* Otherwise, we're going to try to move C after B. If C does
812 not have an outgoing fallthru, then it can be moved
813 immediately after B without introducing or modifying jumps. */
814 if (! c_has_outgoing_fallthru)
816 merge_blocks_move_successor_nojumps (b, c);
817 return true;
820 /* If B does not have an incoming fallthru, then it can be moved
821 immediately before C without introducing or modifying jumps.
822 C cannot be the first block, so we do not have to worry about
823 accessing a non-existent block. */
825 if (b_has_incoming_fallthru)
827 basic_block bb;
829 if (b_fallthru_edge->src == ENTRY_BLOCK_PTR)
830 return false;
831 bb = force_nonfallthru (b_fallthru_edge);
832 if (bb)
833 notice_new_block (bb);
834 else
835 BB_SET_FLAG (b_fallthru_edge->src, BB_UPDATE_LIFE);
838 merge_blocks_move_predecessor_nojumps (b, c);
839 return true;
842 return false;
846 /* Return true if I1 and I2 are equivalent and thus can be crossjumped. */
848 static bool
849 insns_match_p (mode, i1, i2)
850 int mode ATTRIBUTE_UNUSED;
851 rtx i1, i2;
853 rtx p1, p2;
855 /* Verify that I1 and I2 are equivalent. */
856 if (GET_CODE (i1) != GET_CODE (i2))
857 return false;
859 p1 = PATTERN (i1);
860 p2 = PATTERN (i2);
862 if (GET_CODE (p1) != GET_CODE (p2))
863 return false;
865 /* If this is a CALL_INSN, compare register usage information.
866 If we don't check this on stack register machines, the two
867 CALL_INSNs might be merged leaving reg-stack.c with mismatching
868 numbers of stack registers in the same basic block.
869 If we don't check this on machines with delay slots, a delay slot may
870 be filled that clobbers a parameter expected by the subroutine.
872 ??? We take the simple route for now and assume that if they're
873 equal, they were constructed identically. */
875 if (GET_CODE (i1) == CALL_INSN
876 && !rtx_equal_p (CALL_INSN_FUNCTION_USAGE (i1),
877 CALL_INSN_FUNCTION_USAGE (i2)))
878 return false;
880 #ifdef STACK_REGS
881 /* If cross_jump_death_matters is not 0, the insn's mode
882 indicates whether or not the insn contains any stack-like
883 regs. */
885 if ((mode & CLEANUP_POST_REGSTACK) && stack_regs_mentioned (i1))
887 /* If register stack conversion has already been done, then
888 death notes must also be compared before it is certain that
889 the two instruction streams match. */
891 rtx note;
892 HARD_REG_SET i1_regset, i2_regset;
894 CLEAR_HARD_REG_SET (i1_regset);
895 CLEAR_HARD_REG_SET (i2_regset);
897 for (note = REG_NOTES (i1); note; note = XEXP (note, 1))
898 if (REG_NOTE_KIND (note) == REG_DEAD && STACK_REG_P (XEXP (note, 0)))
899 SET_HARD_REG_BIT (i1_regset, REGNO (XEXP (note, 0)));
901 for (note = REG_NOTES (i2); note; note = XEXP (note, 1))
902 if (REG_NOTE_KIND (note) == REG_DEAD && STACK_REG_P (XEXP (note, 0)))
903 SET_HARD_REG_BIT (i2_regset, REGNO (XEXP (note, 0)));
905 GO_IF_HARD_REG_EQUAL (i1_regset, i2_regset, done);
907 return false;
909 done:
912 #endif
914 if (reload_completed
915 ? ! rtx_renumbered_equal_p (p1, p2) : ! rtx_equal_p (p1, p2))
917 /* The following code helps take care of G++ cleanups. */
918 rtx equiv1 = find_reg_equal_equiv_note (i1);
919 rtx equiv2 = find_reg_equal_equiv_note (i2);
921 if (equiv1 && equiv2
922 /* If the equivalences are not to a constant, they may
923 reference pseudos that no longer exist, so we can't
924 use them. */
925 && (! reload_completed
926 || (CONSTANT_P (XEXP (equiv1, 0))
927 && rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))))
929 rtx s1 = single_set (i1);
930 rtx s2 = single_set (i2);
931 if (s1 != 0 && s2 != 0
932 && rtx_renumbered_equal_p (SET_DEST (s1), SET_DEST (s2)))
934 validate_change (i1, &SET_SRC (s1), XEXP (equiv1, 0), 1);
935 validate_change (i2, &SET_SRC (s2), XEXP (equiv2, 0), 1);
936 if (! rtx_renumbered_equal_p (p1, p2))
937 cancel_changes (0);
938 else if (apply_change_group ())
939 return true;
943 return false;
946 return true;
949 /* Look through the insns at the end of BB1 and BB2 and find the longest
950 sequence that are equivalent. Store the first insns for that sequence
951 in *F1 and *F2 and return the sequence length.
953 To simplify callers of this function, if the blocks match exactly,
954 store the head of the blocks in *F1 and *F2. */
956 static int
957 flow_find_cross_jump (mode, bb1, bb2, f1, f2)
958 int mode ATTRIBUTE_UNUSED;
959 basic_block bb1, bb2;
960 rtx *f1, *f2;
962 rtx i1, i2, last1, last2, afterlast1, afterlast2;
963 int ninsns = 0;
965 /* Skip simple jumps at the end of the blocks. Complex jumps still
966 need to be compared for equivalence, which we'll do below. */
968 i1 = bb1->end;
969 last1 = afterlast1 = last2 = afterlast2 = NULL_RTX;
970 if (onlyjump_p (i1)
971 || (returnjump_p (i1) && !side_effects_p (PATTERN (i1))))
973 last1 = i1;
974 i1 = PREV_INSN (i1);
977 i2 = bb2->end;
978 if (onlyjump_p (i2)
979 || (returnjump_p (i2) && !side_effects_p (PATTERN (i2))))
981 last2 = i2;
982 /* Count everything except for unconditional jump as insn. */
983 if (!simplejump_p (i2) && !returnjump_p (i2) && last1)
984 ninsns++;
985 i2 = PREV_INSN (i2);
988 while (true)
990 /* Ignore notes. */
991 while (!active_insn_p (i1) && i1 != bb1->head)
992 i1 = PREV_INSN (i1);
994 while (!active_insn_p (i2) && i2 != bb2->head)
995 i2 = PREV_INSN (i2);
997 if (i1 == bb1->head || i2 == bb2->head)
998 break;
1000 if (!insns_match_p (mode, i1, i2))
1001 break;
1003 /* Don't begin a cross-jump with a USE or CLOBBER insn. */
1004 if (active_insn_p (i1))
1006 /* If the merged insns have different REG_EQUAL notes, then
1007 remove them. */
1008 rtx equiv1 = find_reg_equal_equiv_note (i1);
1009 rtx equiv2 = find_reg_equal_equiv_note (i2);
1011 if (equiv1 && !equiv2)
1012 remove_note (i1, equiv1);
1013 else if (!equiv1 && equiv2)
1014 remove_note (i2, equiv2);
1015 else if (equiv1 && equiv2
1016 && !rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))
1018 remove_note (i1, equiv1);
1019 remove_note (i2, equiv2);
1022 afterlast1 = last1, afterlast2 = last2;
1023 last1 = i1, last2 = i2;
1024 ninsns++;
1027 i1 = PREV_INSN (i1);
1028 i2 = PREV_INSN (i2);
1031 #ifdef HAVE_cc0
1032 /* Don't allow the insn after a compare to be shared by
1033 cross-jumping unless the compare is also shared. */
1034 if (ninsns && reg_mentioned_p (cc0_rtx, last1) && ! sets_cc0_p (last1))
1035 last1 = afterlast1, last2 = afterlast2, ninsns--;
1036 #endif
1038 /* Include preceding notes and labels in the cross-jump. One,
1039 this may bring us to the head of the blocks as requested above.
1040 Two, it keeps line number notes as matched as may be. */
1041 if (ninsns)
1043 while (last1 != bb1->head && !active_insn_p (PREV_INSN (last1)))
1044 last1 = PREV_INSN (last1);
1046 if (last1 != bb1->head && GET_CODE (PREV_INSN (last1)) == CODE_LABEL)
1047 last1 = PREV_INSN (last1);
1049 while (last2 != bb2->head && !active_insn_p (PREV_INSN (last2)))
1050 last2 = PREV_INSN (last2);
1052 if (last2 != bb2->head && GET_CODE (PREV_INSN (last2)) == CODE_LABEL)
1053 last2 = PREV_INSN (last2);
1055 *f1 = last1;
1056 *f2 = last2;
1059 return ninsns;
1062 /* Return true iff outgoing edges of BB1 and BB2 match, together with
1063 the branch instruction. This means that if we commonize the control
1064 flow before end of the basic block, the semantic remains unchanged.
1066 We may assume that there exists one edge with a common destination. */
1068 static bool
1069 outgoing_edges_match (mode, bb1, bb2)
1070 int mode;
1071 basic_block bb1;
1072 basic_block bb2;
1074 int nehedges1 = 0, nehedges2 = 0;
1075 edge fallthru1 = 0, fallthru2 = 0;
1076 edge e1, e2;
1078 /* If BB1 has only one successor, we may be looking at either an
1079 unconditional jump, or a fake edge to exit. */
1080 if (bb1->succ && !bb1->succ->succ_next
1081 && !(bb1->succ->flags & (EDGE_COMPLEX | EDGE_FAKE)))
1082 return (bb2->succ && !bb2->succ->succ_next
1083 && (bb2->succ->flags & (EDGE_COMPLEX | EDGE_FAKE)) == 0);
1085 /* Match conditional jumps - this may get tricky when fallthru and branch
1086 edges are crossed. */
1087 if (bb1->succ
1088 && bb1->succ->succ_next
1089 && !bb1->succ->succ_next->succ_next
1090 && any_condjump_p (bb1->end)
1091 && onlyjump_p (bb1->end))
1093 edge b1, f1, b2, f2;
1094 bool reverse, match;
1095 rtx set1, set2, cond1, cond2;
1096 enum rtx_code code1, code2;
1098 if (!bb2->succ
1099 || !bb2->succ->succ_next
1100 || bb1->succ->succ_next->succ_next
1101 || !any_condjump_p (bb2->end)
1102 || !onlyjump_p (bb1->end))
1103 return false;
1105 b1 = BRANCH_EDGE (bb1);
1106 b2 = BRANCH_EDGE (bb2);
1107 f1 = FALLTHRU_EDGE (bb1);
1108 f2 = FALLTHRU_EDGE (bb2);
1110 /* Get around possible forwarders on fallthru edges. Other cases
1111 should be optimized out already. */
1112 if (FORWARDER_BLOCK_P (f1->dest))
1113 f1 = f1->dest->succ;
1115 if (FORWARDER_BLOCK_P (f2->dest))
1116 f2 = f2->dest->succ;
1118 /* To simplify use of this function, return false if there are
1119 unneeded forwarder blocks. These will get eliminated later
1120 during cleanup_cfg. */
1121 if (FORWARDER_BLOCK_P (f1->dest)
1122 || FORWARDER_BLOCK_P (f2->dest)
1123 || FORWARDER_BLOCK_P (b1->dest)
1124 || FORWARDER_BLOCK_P (b2->dest))
1125 return false;
1127 if (f1->dest == f2->dest && b1->dest == b2->dest)
1128 reverse = false;
1129 else if (f1->dest == b2->dest && b1->dest == f2->dest)
1130 reverse = true;
1131 else
1132 return false;
1134 set1 = pc_set (bb1->end);
1135 set2 = pc_set (bb2->end);
1136 if ((XEXP (SET_SRC (set1), 1) == pc_rtx)
1137 != (XEXP (SET_SRC (set2), 1) == pc_rtx))
1138 reverse = !reverse;
1140 cond1 = XEXP (SET_SRC (set1), 0);
1141 cond2 = XEXP (SET_SRC (set2), 0);
1142 code1 = GET_CODE (cond1);
1143 if (reverse)
1144 code2 = reversed_comparison_code (cond2, bb2->end);
1145 else
1146 code2 = GET_CODE (cond2);
1148 if (code2 == UNKNOWN)
1149 return false;
1151 /* Verify codes and operands match. */
1152 match = ((code1 == code2
1153 && rtx_renumbered_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
1154 && rtx_renumbered_equal_p (XEXP (cond1, 1), XEXP (cond2, 1)))
1155 || (code1 == swap_condition (code2)
1156 && rtx_renumbered_equal_p (XEXP (cond1, 1),
1157 XEXP (cond2, 0))
1158 && rtx_renumbered_equal_p (XEXP (cond1, 0),
1159 XEXP (cond2, 1))));
1161 /* If we return true, we will join the blocks. Which means that
1162 we will only have one branch prediction bit to work with. Thus
1163 we require the existing branches to have probabilities that are
1164 roughly similar. */
1165 if (match
1166 && !optimize_size
1167 && bb1->frequency > BB_FREQ_MAX / 1000
1168 && bb2->frequency > BB_FREQ_MAX / 1000)
1170 int prob2;
1172 if (b1->dest == b2->dest)
1173 prob2 = b2->probability;
1174 else
1175 /* Do not use f2 probability as f2 may be forwarded. */
1176 prob2 = REG_BR_PROB_BASE - b2->probability;
1178 /* Fail if the difference in probabilities is
1179 greater than 5%. */
1180 if (abs (b1->probability - prob2) > REG_BR_PROB_BASE / 20)
1182 if (rtl_dump_file)
1183 fprintf (rtl_dump_file,
1184 "Outcomes of branch in bb %i and %i differs to much (%i %i)\n",
1185 bb1->index, bb2->index, b1->probability, prob2);
1187 return false;
1191 if (rtl_dump_file && match)
1192 fprintf (rtl_dump_file, "Conditionals in bb %i and %i match.\n",
1193 bb1->index, bb2->index);
1195 return match;
1198 /* Generic case - we are seeing an computed jump, table jump or trapping
1199 instruction. */
1201 /* First ensure that the instructions match. There may be many outgoing
1202 edges so this test is generally cheaper.
1203 ??? Currently the tablejumps will never match, as they do have
1204 different tables. */
1205 if (!insns_match_p (mode, bb1->end, bb2->end))
1206 return false;
1208 /* Search the outgoing edges, ensure that the counts do match, find possible
1209 fallthru and exception handling edges since these needs more
1210 validation. */
1211 for (e1 = bb1->succ, e2 = bb2->succ; e1 && e2;
1212 e1 = e1->succ_next, e2 = e2->succ_next)
1214 if (e1->flags & EDGE_EH)
1215 nehedges1++;
1217 if (e2->flags & EDGE_EH)
1218 nehedges2++;
1220 if (e1->flags & EDGE_FALLTHRU)
1221 fallthru1 = e1;
1222 if (e2->flags & EDGE_FALLTHRU)
1223 fallthru2 = e2;
1226 /* If number of edges of various types does not match, fail. */
1227 if (e1 || e2
1228 || nehedges1 != nehedges2
1229 || (fallthru1 != 0) != (fallthru2 != 0))
1230 return false;
1232 /* fallthru edges must be forwarded to the same destination. */
1233 if (fallthru1)
1235 basic_block d1 = (forwarder_block_p (fallthru1->dest)
1236 ? fallthru1->dest->succ->dest: fallthru1->dest);
1237 basic_block d2 = (forwarder_block_p (fallthru2->dest)
1238 ? fallthru2->dest->succ->dest: fallthru2->dest);
1240 if (d1 != d2)
1241 return false;
1244 /* In case we do have EH edges, ensure we are in the same region. */
1245 if (nehedges1)
1247 rtx n1 = find_reg_note (bb1->end, REG_EH_REGION, 0);
1248 rtx n2 = find_reg_note (bb2->end, REG_EH_REGION, 0);
1250 if (XEXP (n1, 0) != XEXP (n2, 0))
1251 return false;
1254 /* We don't need to match the rest of edges as above checks should be enought
1255 to ensure that they are equivalent. */
1256 return true;
1259 /* E1 and E2 are edges with the same destination block. Search their
1260 predecessors for common code. If found, redirect control flow from
1261 (maybe the middle of) E1->SRC to (maybe the middle of) E2->SRC. */
1263 static bool
1264 try_crossjump_to_edge (mode, e1, e2)
1265 int mode;
1266 edge e1, e2;
1268 int nmatch;
1269 basic_block src1 = e1->src, src2 = e2->src;
1270 basic_block redirect_to;
1271 rtx newpos1, newpos2;
1272 edge s;
1273 rtx last;
1274 rtx label;
1276 /* Search backward through forwarder blocks. We don't need to worry
1277 about multiple entry or chained forwarders, as they will be optimized
1278 away. We do this to look past the unconditional jump following a
1279 conditional jump that is required due to the current CFG shape. */
1280 if (src1->pred
1281 && !src1->pred->pred_next
1282 && FORWARDER_BLOCK_P (src1))
1283 e1 = src1->pred, src1 = e1->src;
1285 if (src2->pred
1286 && !src2->pred->pred_next
1287 && FORWARDER_BLOCK_P (src2))
1288 e2 = src2->pred, src2 = e2->src;
1290 /* Nothing to do if we reach ENTRY, or a common source block. */
1291 if (src1 == ENTRY_BLOCK_PTR || src2 == ENTRY_BLOCK_PTR)
1292 return false;
1293 if (src1 == src2)
1294 return false;
1296 /* Seeing more than 1 forwarder blocks would confuse us later... */
1297 if (FORWARDER_BLOCK_P (e1->dest)
1298 && FORWARDER_BLOCK_P (e1->dest->succ->dest))
1299 return false;
1301 if (FORWARDER_BLOCK_P (e2->dest)
1302 && FORWARDER_BLOCK_P (e2->dest->succ->dest))
1303 return false;
1305 /* Likewise with dead code (possibly newly created by the other optimizations
1306 of cfg_cleanup). */
1307 if (!src1->pred || !src2->pred)
1308 return false;
1310 /* Look for the common insn sequence, part the first ... */
1311 if (!outgoing_edges_match (mode, src1, src2))
1312 return false;
1314 /* ... and part the second. */
1315 nmatch = flow_find_cross_jump (mode, src1, src2, &newpos1, &newpos2);
1316 if (!nmatch)
1317 return false;
1319 /* Avoid splitting if possible. */
1320 if (newpos2 == src2->head)
1321 redirect_to = src2;
1322 else
1324 if (rtl_dump_file)
1325 fprintf (rtl_dump_file, "Splitting bb %i before %i insns\n",
1326 src2->index, nmatch);
1327 redirect_to = split_block (src2, PREV_INSN (newpos2))->dest;
1330 if (rtl_dump_file)
1331 fprintf (rtl_dump_file,
1332 "Cross jumping from bb %i to bb %i; %i common insns\n",
1333 src1->index, src2->index, nmatch);
1335 redirect_to->count += src1->count;
1336 redirect_to->frequency += src1->frequency;
1338 /* Recompute the frequencies and counts of outgoing edges. */
1339 for (s = redirect_to->succ; s; s = s->succ_next)
1341 edge s2;
1342 basic_block d = s->dest;
1344 if (FORWARDER_BLOCK_P (d))
1345 d = d->succ->dest;
1347 for (s2 = src1->succ; ; s2 = s2->succ_next)
1349 basic_block d2 = s2->dest;
1350 if (FORWARDER_BLOCK_P (d2))
1351 d2 = d2->succ->dest;
1352 if (d == d2)
1353 break;
1356 s->count += s2->count;
1358 /* Take care to update possible forwarder blocks. We verified
1359 that there is no more than one in the chain, so we can't run
1360 into infinite loop. */
1361 if (FORWARDER_BLOCK_P (s->dest))
1363 s->dest->succ->count += s2->count;
1364 s->dest->count += s2->count;
1365 s->dest->frequency += EDGE_FREQUENCY (s);
1368 if (FORWARDER_BLOCK_P (s2->dest))
1370 s2->dest->succ->count -= s2->count;
1371 if (s2->dest->succ->count < 0)
1372 s2->dest->succ->count = 0;
1373 s2->dest->count -= s2->count;
1374 s2->dest->frequency -= EDGE_FREQUENCY (s);
1375 if (s2->dest->frequency < 0)
1376 s2->dest->frequency = 0;
1377 if (s2->dest->count < 0)
1378 s2->dest->count = 0;
1381 if (!redirect_to->frequency && !src1->frequency)
1382 s->probability = (s->probability + s2->probability) / 2;
1383 else
1384 s->probability
1385 = ((s->probability * redirect_to->frequency +
1386 s2->probability * src1->frequency)
1387 / (redirect_to->frequency + src1->frequency));
1390 update_br_prob_note (redirect_to);
1392 /* Edit SRC1 to go to REDIRECT_TO at NEWPOS1. */
1394 /* Skip possible basic block header. */
1395 if (GET_CODE (newpos1) == CODE_LABEL)
1396 newpos1 = NEXT_INSN (newpos1);
1398 if (GET_CODE (newpos1) == NOTE)
1399 newpos1 = NEXT_INSN (newpos1);
1400 last = src1->end;
1402 /* Emit the jump insn. */
1403 label = block_label (redirect_to);
1404 emit_jump_insn_after (gen_jump (label), src1->end);
1405 JUMP_LABEL (src1->end) = label;
1406 LABEL_NUSES (label)++;
1408 /* Delete the now unreachable instructions. */
1409 delete_insn_chain (newpos1, last);
1411 /* Make sure there is a barrier after the new jump. */
1412 last = next_nonnote_insn (src1->end);
1413 if (!last || GET_CODE (last) != BARRIER)
1414 emit_barrier_after (src1->end);
1416 /* Update CFG. */
1417 while (src1->succ)
1418 remove_edge (src1->succ);
1419 make_single_succ_edge (src1, redirect_to, 0);
1421 BB_SET_FLAG (src1, BB_UPDATE_LIFE);
1422 update_forwarder_flag (src1);
1424 return true;
1427 /* Search the predecessors of BB for common insn sequences. When found,
1428 share code between them by redirecting control flow. Return true if
1429 any changes made. */
1431 static bool
1432 try_crossjump_bb (mode, bb)
1433 int mode;
1434 basic_block bb;
1436 edge e, e2, nexte2, nexte, fallthru;
1437 bool changed;
1439 /* Nothing to do if there is not at least two incoming edges. */
1440 if (!bb->pred || !bb->pred->pred_next)
1441 return false;
1443 /* It is always cheapest to redirect a block that ends in a branch to
1444 a block that falls through into BB, as that adds no branches to the
1445 program. We'll try that combination first. */
1446 for (fallthru = bb->pred; fallthru; fallthru = fallthru->pred_next)
1447 if (fallthru->flags & EDGE_FALLTHRU)
1448 break;
1450 changed = false;
1451 for (e = bb->pred; e; e = nexte)
1453 nexte = e->pred_next;
1455 /* As noted above, first try with the fallthru predecessor. */
1456 if (fallthru)
1458 /* Don't combine the fallthru edge into anything else.
1459 If there is a match, we'll do it the other way around. */
1460 if (e == fallthru)
1461 continue;
1463 if (try_crossjump_to_edge (mode, e, fallthru))
1465 changed = true;
1466 nexte = bb->pred;
1467 continue;
1471 /* Non-obvious work limiting check: Recognize that we're going
1472 to call try_crossjump_bb on every basic block. So if we have
1473 two blocks with lots of outgoing edges (a switch) and they
1474 share lots of common destinations, then we would do the
1475 cross-jump check once for each common destination.
1477 Now, if the blocks actually are cross-jump candidates, then
1478 all of their destinations will be shared. Which means that
1479 we only need check them for cross-jump candidacy once. We
1480 can eliminate redundant checks of crossjump(A,B) by arbitrarily
1481 choosing to do the check from the block for which the edge
1482 in question is the first successor of A. */
1483 if (e->src->succ != e)
1484 continue;
1486 for (e2 = bb->pred; e2; e2 = nexte2)
1488 nexte2 = e2->pred_next;
1490 if (e2 == e)
1491 continue;
1493 /* We've already checked the fallthru edge above. */
1494 if (e2 == fallthru)
1495 continue;
1497 /* The "first successor" check above only prevents multiple
1498 checks of crossjump(A,B). In order to prevent redundant
1499 checks of crossjump(B,A), require that A be the block
1500 with the lowest index. */
1501 if (e->src->index > e2->src->index)
1502 continue;
1504 if (try_crossjump_to_edge (mode, e, e2))
1506 changed = true;
1507 nexte = bb->pred;
1508 break;
1513 return changed;
1516 /* Do simple CFG optimizations - basic block merging, simplifying of jump
1517 instructions etc. Return nonzero if changes were made. */
1519 static bool
1520 try_optimize_cfg (mode)
1521 int mode;
1523 int i;
1524 bool changed_overall = false;
1525 bool changed;
1526 int iterations = 0;
1527 sbitmap blocks;
1529 if (mode & CLEANUP_CROSSJUMP)
1530 add_noreturn_fake_exit_edges ();
1532 for (i = 0; i < n_basic_blocks; i++)
1533 update_forwarder_flag (BASIC_BLOCK (i));
1535 if (! (* targetm.cannot_modify_jumps_p) ())
1537 /* Attempt to merge blocks as made possible by edge removal. If
1538 a block has only one successor, and the successor has only
1539 one predecessor, they may be combined. */
1542 changed = false;
1543 iterations++;
1545 if (rtl_dump_file)
1546 fprintf (rtl_dump_file,
1547 "\n\ntry_optimize_cfg iteration %i\n\n",
1548 iterations);
1550 for (i = 0; i < n_basic_blocks;)
1552 basic_block c, b = BASIC_BLOCK (i);
1553 edge s;
1554 bool changed_here = false;
1556 /* Delete trivially dead basic blocks. */
1557 while (b->pred == NULL)
1559 c = BASIC_BLOCK (b->index - 1);
1560 if (rtl_dump_file)
1561 fprintf (rtl_dump_file, "Deleting block %i.\n",
1562 b->index);
1564 flow_delete_block (b);
1565 changed = true;
1566 b = c;
1569 /* Remove code labels no longer used. Don't do this
1570 before CALL_PLACEHOLDER is removed, as some branches
1571 may be hidden within. */
1572 if (b->pred->pred_next == NULL
1573 && (b->pred->flags & EDGE_FALLTHRU)
1574 && !(b->pred->flags & EDGE_COMPLEX)
1575 && GET_CODE (b->head) == CODE_LABEL
1576 && (!(mode & CLEANUP_PRE_SIBCALL)
1577 || !tail_recursion_label_p (b->head))
1578 /* If the previous block ends with a branch to this
1579 block, we can't delete the label. Normally this
1580 is a condjump that is yet to be simplified, but
1581 if CASE_DROPS_THRU, this can be a tablejump with
1582 some element going to the same place as the
1583 default (fallthru). */
1584 && (b->pred->src == ENTRY_BLOCK_PTR
1585 || GET_CODE (b->pred->src->end) != JUMP_INSN
1586 || ! label_is_jump_target_p (b->head,
1587 b->pred->src->end)))
1589 rtx label = b->head;
1591 b->head = NEXT_INSN (b->head);
1592 delete_insn_chain (label, label);
1593 if (rtl_dump_file)
1594 fprintf (rtl_dump_file, "Deleted label in block %i.\n",
1595 b->index);
1598 /* If we fall through an empty block, we can remove it. */
1599 if (b->pred->pred_next == NULL
1600 && (b->pred->flags & EDGE_FALLTHRU)
1601 && GET_CODE (b->head) != CODE_LABEL
1602 && FORWARDER_BLOCK_P (b)
1603 /* Note that forwarder_block_p true ensures that
1604 there is a successor for this block. */
1605 && (b->succ->flags & EDGE_FALLTHRU)
1606 && n_basic_blocks > 1)
1608 if (rtl_dump_file)
1609 fprintf (rtl_dump_file,
1610 "Deleting fallthru block %i.\n",
1611 b->index);
1613 c = BASIC_BLOCK (b->index ? b->index - 1 : 1);
1614 redirect_edge_succ_nodup (b->pred, b->succ->dest);
1615 flow_delete_block (b);
1616 changed = true;
1617 b = c;
1620 /* Merge blocks. Loop because chains of blocks might be
1621 combineable. */
1622 while ((s = b->succ) != NULL
1623 && s->succ_next == NULL
1624 && !(s->flags & EDGE_COMPLEX)
1625 && (c = s->dest) != EXIT_BLOCK_PTR
1626 && c->pred->pred_next == NULL
1627 /* If the jump insn has side effects,
1628 we can't kill the edge. */
1629 && (GET_CODE (b->end) != JUMP_INSN
1630 || onlyjump_p (b->end))
1631 && merge_blocks (s, b, c, mode))
1632 changed_here = true;
1634 /* Simplify branch over branch. */
1635 if ((mode & CLEANUP_EXPENSIVE) && try_simplify_condjump (b))
1637 BB_SET_FLAG (b, BB_UPDATE_LIFE);
1638 changed_here = true;
1641 /* If B has a single outgoing edge, but uses a
1642 non-trivial jump instruction without side-effects, we
1643 can either delete the jump entirely, or replace it
1644 with a simple unconditional jump. Use
1645 redirect_edge_and_branch to do the dirty work. */
1646 if (b->succ
1647 && ! b->succ->succ_next
1648 && b->succ->dest != EXIT_BLOCK_PTR
1649 && onlyjump_p (b->end)
1650 && redirect_edge_and_branch (b->succ, b->succ->dest))
1652 BB_SET_FLAG (b, BB_UPDATE_LIFE);
1653 update_forwarder_flag (b);
1654 changed_here = true;
1657 /* Simplify branch to branch. */
1658 if (try_forward_edges (mode, b))
1659 changed_here = true;
1661 /* Look for shared code between blocks. */
1662 if ((mode & CLEANUP_CROSSJUMP)
1663 && try_crossjump_bb (mode, b))
1664 changed_here = true;
1666 /* Don't get confused by the index shift caused by
1667 deleting blocks. */
1668 if (!changed_here)
1669 i = b->index + 1;
1670 else
1671 changed = true;
1674 if ((mode & CLEANUP_CROSSJUMP)
1675 && try_crossjump_bb (mode, EXIT_BLOCK_PTR))
1676 changed = true;
1678 #ifdef ENABLE_CHECKING
1679 if (changed)
1680 verify_flow_info ();
1681 #endif
1683 changed_overall |= changed;
1685 while (changed);
1688 if (mode & CLEANUP_CROSSJUMP)
1689 remove_fake_edges ();
1691 if ((mode & CLEANUP_UPDATE_LIFE) && changed_overall)
1693 bool found = 0;
1695 blocks = sbitmap_alloc (n_basic_blocks);
1696 sbitmap_zero (blocks);
1697 for (i = 0; i < n_basic_blocks; i++)
1698 if (BB_FLAGS (BASIC_BLOCK (i)) & BB_UPDATE_LIFE)
1700 found = 1;
1701 SET_BIT (blocks, i);
1704 if (found)
1705 update_life_info (blocks, UPDATE_LIFE_GLOBAL,
1706 PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
1707 | PROP_KILL_DEAD_CODE);
1708 sbitmap_free (blocks);
1711 for (i = 0; i < n_basic_blocks; i++)
1712 BASIC_BLOCK (i)->aux = NULL;
1714 return changed_overall;
1717 /* Delete all unreachable basic blocks. */
1719 static bool
1720 delete_unreachable_blocks ()
1722 int i;
1723 bool changed = false;
1725 find_unreachable_blocks ();
1727 /* Delete all unreachable basic blocks. Count down so that we
1728 don't interfere with the block renumbering that happens in
1729 flow_delete_block. */
1731 for (i = n_basic_blocks - 1; i >= 0; --i)
1733 basic_block b = BASIC_BLOCK (i);
1735 if (!(b->flags & BB_REACHABLE))
1736 flow_delete_block (b), changed = true;
1739 if (changed)
1740 tidy_fallthru_edges ();
1741 return changed;
1744 /* Tidy the CFG by deleting unreachable code and whatnot. */
1746 bool
1747 cleanup_cfg (mode)
1748 int mode;
1750 bool changed = false;
1752 timevar_push (TV_CLEANUP_CFG);
1753 changed = delete_unreachable_blocks ();
1754 if (try_optimize_cfg (mode))
1755 delete_unreachable_blocks (), changed = true;
1757 /* Kill the data we won't maintain. */
1758 free_EXPR_LIST_list (&label_value_list);
1759 free_EXPR_LIST_list (&tail_recursion_label_list);
1760 timevar_pop (TV_CLEANUP_CFG);
1762 return changed;