OSDN Git Service

2002-01-06 H.J. Lu <hjl@gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / cfgcleanup.c
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.
4
5 This file is part of GCC.
6
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.
11
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.
16
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.  */
21
22 /* This file contains optimizer of the control flow.  The main entrypoint is
23    cleanup_cfg.  Following optimizations are performed:
24
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.  */
33
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
48 #include "obstack.h"
49
50 /* cleanup_cfg maintains following flags for each basic block.  */
51
52 enum bb_flags
53 {
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
59 };
60
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))
66
67 #define FORWARDER_BLOCK_P(BB) (BB_FLAGS (BB) & BB_FORWARDER_BLOCK)
68
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));
76
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));
93 \f
94 /* Set flags for newly created block.  */
95
96 static void
97 notice_new_block (bb)
98      basic_block bb;
99 {
100   if (!bb)
101     return;
102
103   BB_SET_FLAG (bb, BB_UPDATE_LIFE);
104   if (forwarder_block_p (bb))
105     BB_SET_FLAG (bb, BB_FORWARDER_BLOCK);
106 }
107
108 /* Recompute forwarder flag after block has been modified.  */
109
110 static void
111 update_forwarder_flag (bb)
112      basic_block bb;
113 {
114   if (forwarder_block_p (bb))
115     BB_SET_FLAG (bb, BB_FORWARDER_BLOCK);
116   else
117     BB_CLEAR_FLAG (bb, BB_FORWARDER_BLOCK);
118 }
119 \f
120 /* Simplify a conditional jump around an unconditional jump.
121    Return true if something changed.  */
122
123 static bool
124 try_simplify_condjump (cbranch_block)
125      basic_block cbranch_block;
126 {
127   basic_block jump_block, jump_dest_block, cbranch_dest_block;
128   edge cbranch_jump_edge, cbranch_fallthru_edge;
129   rtx cbranch_insn;
130
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;
136
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;
142
143   cbranch_fallthru_edge = FALLTHRU_EDGE (cbranch_block);
144   cbranch_jump_edge = BRANCH_EDGE (cbranch_block);
145
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;
155
156   /* The conditional branch must target the block after the
157      unconditional branch.  */
158   cbranch_dest_block = cbranch_jump_edge->dest;
159
160   if (!can_fallthru (jump_block, cbranch_dest_block))
161     return false;
162
163   /* Invert the conditional branch.  */
164   if (!invert_jump (cbranch_insn, block_label (jump_dest_block), 0))
165     return false;
166
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));
170
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
181   /* Delete the block with the unconditional jump, and clean up the mess.  */
182   flow_delete_block (jump_block);
183   tidy_fallthru_edge (cbranch_jump_edge, cbranch_block, cbranch_dest_block);
184
185   return true;
186 }
187 \f
188 /* Attempt to prove that operation is NOOP using CSElib or mark the effect
189    on register.  Used by jump threading.  */
190
191 static bool
192 mark_effect (exp, nonequal)
193   rtx exp;
194   regset nonequal;
195 {
196   int regno;
197   rtx dest;
198   switch (GET_CODE (exp))
199     {
200       /* In case we do clobber the register, mark it as equal, as we know the
201          value is dead so it don't have to match.  */
202       case CLOBBER:
203         if (REG_P (XEXP (exp, 0)))
204           {
205             dest = XEXP (exp, 0);
206             regno = REGNO (dest);
207             CLEAR_REGNO_REG_SET (nonequal, regno);
208             if (regno < FIRST_PSEUDO_REGISTER)
209               {
210                 int n = HARD_REGNO_NREGS (regno, GET_MODE (dest));
211                 while (--n > 0)
212                   CLEAR_REGNO_REG_SET (nonequal, regno + n);
213               }
214           }
215         return false;
216
217       case SET:
218         if (rtx_equal_for_cselib_p (SET_DEST (exp), SET_SRC (exp)))
219           return false;
220         dest = SET_DEST (exp);
221         if (dest == pc_rtx)
222           return false;
223         if (!REG_P (dest))
224           return true;
225         regno = REGNO (dest);
226         SET_REGNO_REG_SET (nonequal, regno);
227         if (regno < FIRST_PSEUDO_REGISTER)
228           {
229             int n = HARD_REGNO_NREGS (regno, GET_MODE (dest));
230             while (--n > 0)
231               SET_REGNO_REG_SET (nonequal, regno + n);
232           }
233         return false;
234
235       default:
236         return false;
237     }
238 }
239 /* Attempt to prove that the basic block B will have no side effects and
240    allways continues in the same edge if reached via E.  Return the edge
241    if exist, NULL otherwise.  */
242
243 static edge
244 thread_jump (mode, e, b)
245      int mode;
246      edge e;
247      basic_block b;
248 {
249   rtx set1, set2, cond1, cond2, insn;
250   enum rtx_code code1, code2, reversed_code2;
251   bool reverse1 = false;
252   int i;
253   regset nonequal;
254   bool failed = false;
255
256   /* At the moment, we do handle only conditional jumps, but later we may
257      want to extend this code to tablejumps and others.  */
258   if (!e->src->succ->succ_next || e->src->succ->succ_next->succ_next)
259     return NULL;
260   if (!b->succ || !b->succ->succ_next || b->succ->succ_next->succ_next)
261     return NULL;
262
263   /* Second branch must end with onlyjump, as we will eliminate the jump.  */
264   if (!any_condjump_p (e->src->end) || !any_condjump_p (b->end)
265       || !onlyjump_p (b->end))
266     return NULL;
267
268   set1 = pc_set (e->src->end);
269   set2 = pc_set (b->end);
270   if (((e->flags & EDGE_FALLTHRU) != 0)
271       != (XEXP (SET_SRC (set1), 1) == pc_rtx))
272     reverse1 = true;
273
274   cond1 = XEXP (SET_SRC (set1), 0);
275   cond2 = XEXP (SET_SRC (set2), 0);
276   if (reverse1)
277     code1 = reversed_comparison_code (cond1, e->src->end);
278   else
279     code1 = GET_CODE (cond1);
280
281   code2 = GET_CODE (cond2);
282   reversed_code2 = reversed_comparison_code (cond2, b->end);
283
284   if (!comparison_dominates_p (code1, code2)
285       && !comparison_dominates_p (code1, reversed_code2))
286     return NULL;
287
288   /* Ensure that the comparison operators are equivalent.
289      ??? This is far too pesimistic.  We should allow swapped operands,
290      different CCmodes, or for example comparisons for interval, that
291      dominate even when operands are not equivalent.  */
292   if (!rtx_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
293       || !rtx_equal_p (XEXP (cond1, 1), XEXP (cond2, 1)))
294     return NULL;
295
296   /* Short circuit cases where block B contains some side effects, as we can't
297      safely bypass it.  */
298   for (insn = NEXT_INSN (b->head); insn != NEXT_INSN (b->end);
299        insn = NEXT_INSN (insn))
300     if (INSN_P (insn) && side_effects_p (PATTERN (insn)))
301       return NULL;
302
303   cselib_init ();
304
305   /* First process all values computed in the source basic block.  */
306   for (insn = NEXT_INSN (e->src->head); insn != NEXT_INSN (e->src->end);
307        insn = NEXT_INSN (insn))
308     if (INSN_P (insn))
309       cselib_process_insn (insn);
310
311   nonequal = BITMAP_XMALLOC();
312   CLEAR_REG_SET (nonequal);
313
314   /* Now assume that we've continued by the edge E to B and continue
315      processing as if it were same basic block.
316      Our goal is to prove that whole block is an NOOP.  */
317
318   for (insn = NEXT_INSN (b->head); insn != NEXT_INSN (b->end) && !failed;
319        insn = NEXT_INSN (insn))
320   {
321     if (INSN_P (insn))
322       {
323         rtx pat = PATTERN (insn);
324
325         if (GET_CODE (pat) == PARALLEL)
326           {
327             for (i = 0; i < XVECLEN (pat, 0); i++)
328               failed |= mark_effect (XVECEXP (pat, 0, i), nonequal);
329           }
330         else
331           failed |= mark_effect (pat, nonequal);
332       }
333
334     cselib_process_insn (insn);
335   }
336
337   /* Later we should clear nonequal of dead registers.  So far we don't
338      have life information in cfg_cleanup.  */
339   if (failed)
340     goto failed_exit;
341
342   /* In case liveness information is available, we need to prove equivalence
343      only of the live values.  */
344   if (mode & CLEANUP_UPDATE_LIFE)
345     AND_REG_SET (nonequal, b->global_live_at_end);
346
347   EXECUTE_IF_SET_IN_REG_SET (nonequal, 0, i, goto failed_exit;);
348
349   BITMAP_XFREE (nonequal);
350   cselib_finish ();
351   if ((comparison_dominates_p (code1, code2) != 0)
352       != (XEXP (SET_SRC (set2), 1) == pc_rtx))
353     return BRANCH_EDGE (b);
354   else
355     return FALLTHRU_EDGE (b);
356
357 failed_exit:
358   BITMAP_XFREE (nonequal);
359   cselib_finish ();
360   return NULL;
361 }
362 \f
363 /* Attempt to forward edges leaving basic block B.
364    Return true if successful.  */
365
366 static bool
367 try_forward_edges (mode, b)
368      basic_block b;
369      int mode;
370 {
371   bool changed = false;
372   edge e, next, *threaded_edges = NULL;
373   int nthreaded_edges = 0;
374
375   for (e = b->succ; e; e = next)
376     {
377       basic_block target, first;
378       int counter;
379       bool threaded = false;
380
381       next = e->succ_next;
382
383       /* Skip complex edges because we don't know how to update them.
384
385          Still handle fallthru edges, as we can succeed to forward fallthru
386          edge to the same place as the branch edge of conditional branch
387          and turn conditional branch to an unconditional branch.  */
388       if (e->flags & EDGE_COMPLEX)
389         continue;
390
391       target = first = e->dest;
392       counter = 0;
393
394       while (counter < n_basic_blocks)
395         {
396           basic_block new_target = NULL;
397           bool new_target_threaded = false;
398
399           if (FORWARDER_BLOCK_P (target)
400               && target->succ->dest != EXIT_BLOCK_PTR)
401             {
402               /* Bypass trivial infinite loops.  */
403               if (target == target->succ->dest)
404                 counter = n_basic_blocks;
405               new_target = target->succ->dest;
406             }
407
408           /* Allow to thread only over one edge at time to simplify updating
409              of probabilities.  */
410           else if (mode & CLEANUP_THREADING)
411             {
412               edge t = thread_jump (mode, e, target);
413               if (t)
414                 {
415                   if (!nthreaded_edges)
416                     threaded_edges = xmalloc (sizeof (*threaded_edges)
417                                               * n_basic_blocks);
418                   else
419                     {
420                       int i;
421
422                       /* Detect an infinite loop across blocks not
423                          including the start block.  */
424                       for (i = 0; i < nthreaded_edges; ++i)
425                         if (threaded_edges[i] == t)
426                           break;
427                       if (i < nthreaded_edges)
428                         break;
429                     }
430
431                   /* Detect an infinite loop across the start block.  */
432                   if (t->dest == b)
433                     break;
434
435                   if (nthreaded_edges >= n_basic_blocks)
436                     abort ();
437                   threaded_edges[nthreaded_edges++] = t;
438
439                   new_target = t->dest;
440                   new_target_threaded = true;
441                 }
442             }
443
444           if (!new_target)
445             break;
446
447           /* Avoid killing of loop pre-headers, as it is the place loop
448              optimizer wants to hoist code to.
449
450              For fallthru forwarders, the LOOP_BEG note must appear between
451              the header of block and CODE_LABEL of the loop, for non forwarders
452              it must appear before the JUMP_INSN.  */
453           if (mode & CLEANUP_PRE_LOOP)
454             {
455               rtx insn = (target->succ->flags & EDGE_FALLTHRU
456                           ? target->head : prev_nonnote_insn (target->end));
457
458               if (GET_CODE (insn) != NOTE)
459                 insn = NEXT_INSN (insn);
460
461               for (; insn && GET_CODE (insn) != CODE_LABEL && !INSN_P (insn);
462                    insn = NEXT_INSN (insn))
463                 if (GET_CODE (insn) == NOTE
464                     && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
465                   break;
466
467               if (GET_CODE (insn) == NOTE)
468                 break;
469             }
470
471           counter++;
472           target = new_target;
473           threaded |= new_target_threaded;
474         }
475
476       if (counter >= n_basic_blocks)
477         {
478           if (rtl_dump_file)
479             fprintf (rtl_dump_file, "Infinite loop in BB %i.\n",
480                      target->index);
481         }
482       else if (target == first)
483         ; /* We didn't do anything.  */
484       else
485         {
486           /* Save the values now, as the edge may get removed.  */
487           gcov_type edge_count = e->count;
488           int edge_probability = e->probability;
489           int edge_frequency;
490           int n = 0;
491
492           /* Don't force if target is exit block.  */
493           if (threaded && target != EXIT_BLOCK_PTR)
494             {
495               notice_new_block (redirect_edge_and_branch_force (e, target));
496               if (rtl_dump_file)
497                 fprintf (rtl_dump_file, "Conditionals threaded.\n");
498             }
499           else if (!redirect_edge_and_branch (e, target))
500             {
501               if (rtl_dump_file)
502                 fprintf (rtl_dump_file,
503                          "Forwarding edge %i->%i to %i failed.\n",
504                          b->index, e->dest->index, target->index);
505               continue;
506             }
507
508           /* We successfully forwarded the edge.  Now update profile
509              data: for each edge we traversed in the chain, remove
510              the original edge's execution count.  */
511           edge_frequency = ((edge_probability * b->frequency
512                              + REG_BR_PROB_BASE / 2)
513                             / REG_BR_PROB_BASE);
514
515           if (!FORWARDER_BLOCK_P (b) && forwarder_block_p (b))
516             BB_SET_FLAG (b, BB_FORWARDER_BLOCK);
517           BB_SET_FLAG (b, BB_UPDATE_LIFE);
518
519           do
520             {
521               edge t;
522
523               first->count -= edge_count;
524               first->succ->count -= edge_count;
525               first->frequency -= edge_frequency;
526               if (first->succ->succ_next)
527                 {
528                   if (n >= nthreaded_edges)
529                     abort ();
530                   t = threaded_edges [n++];
531                 }
532               else
533                 t = first->succ;
534
535               first = t->dest;
536             }
537           while (first != target);
538
539           changed = true;
540         }
541     }
542
543   if (threaded_edges)
544     free (threaded_edges);
545   return changed;
546 }
547 \f
548 /* Return true if LABEL is a target of JUMP_INSN.  This applies only
549    to non-complex jumps.  That is, direct unconditional, conditional,
550    and tablejumps, but not computed jumps or returns.  It also does
551    not apply to the fallthru case of a conditional jump.  */
552
553 static bool
554 label_is_jump_target_p (label, jump_insn)
555      rtx label, jump_insn;
556 {
557   rtx tmp = JUMP_LABEL (jump_insn);
558
559   if (label == tmp)
560     return true;
561
562   if (tmp != NULL_RTX
563       && (tmp = NEXT_INSN (tmp)) != NULL_RTX
564       && GET_CODE (tmp) == JUMP_INSN
565       && (tmp = PATTERN (tmp),
566           GET_CODE (tmp) == ADDR_VEC
567           || GET_CODE (tmp) == ADDR_DIFF_VEC))
568     {
569       rtvec vec = XVEC (tmp, GET_CODE (tmp) == ADDR_DIFF_VEC);
570       int i, veclen = GET_NUM_ELEM (vec);
571
572       for (i = 0; i < veclen; ++i)
573         if (XEXP (RTVEC_ELT (vec, i), 0) == label)
574           return true;
575     }
576
577   return false;
578 }
579
580 /* Return true if LABEL is used for tail recursion.  */
581
582 static bool
583 tail_recursion_label_p (label)
584      rtx label;
585 {
586   rtx x;
587
588   for (x = tail_recursion_label_list; x; x = XEXP (x, 1))
589     if (label == XEXP (x, 0))
590       return true;
591
592   return false;
593 }
594
595 /* Blocks A and B are to be merged into a single block.  A has no incoming
596    fallthru edge, so it can be moved before B without adding or modifying
597    any jumps (aside from the jump from A to B).  */
598
599 static void
600 merge_blocks_move_predecessor_nojumps (a, b)
601      basic_block a, b;
602 {
603   rtx barrier;
604   int index;
605
606   barrier = next_nonnote_insn (a->end);
607   if (GET_CODE (barrier) != BARRIER)
608     abort ();
609   delete_insn (barrier);
610
611   /* Move block and loop notes out of the chain so that we do not
612      disturb their order.
613
614      ??? A better solution would be to squeeze out all the non-nested notes
615      and adjust the block trees appropriately.   Even better would be to have
616      a tighter connection between block trees and rtl so that this is not
617      necessary.  */
618   if (squeeze_notes (&a->head, &a->end))
619     abort ();
620
621   /* Scramble the insn chain.  */
622   if (a->end != PREV_INSN (b->head))
623     reorder_insns_nobb (a->head, a->end, PREV_INSN (b->head));
624   BB_SET_FLAG (a, BB_UPDATE_LIFE);
625
626   if (rtl_dump_file)
627     fprintf (rtl_dump_file, "Moved block %d before %d and merged.\n",
628              a->index, b->index);
629
630   /* Swap the records for the two blocks around.  Although we are deleting B,
631      A is now where B was and we want to compact the BB array from where
632      A used to be.  */
633   BASIC_BLOCK (a->index) = b;
634   BASIC_BLOCK (b->index) = a;
635   index = a->index;
636   a->index = b->index;
637   b->index = index;
638
639   /* Now blocks A and B are contiguous.  Merge them.  */
640   merge_blocks_nomove (a, b);
641 }
642
643 /* Blocks A and B are to be merged into a single block.  B has no outgoing
644    fallthru edge, so it can be moved after A without adding or modifying
645    any jumps (aside from the jump from A to B).  */
646
647 static void
648 merge_blocks_move_successor_nojumps (a, b)
649      basic_block a, b;
650 {
651   rtx barrier, real_b_end;
652
653   real_b_end = b->end;
654   barrier = NEXT_INSN (b->end);
655
656   /* Recognize a jump table following block B.  */
657   if (barrier
658       && GET_CODE (barrier) == CODE_LABEL
659       && NEXT_INSN (barrier)
660       && GET_CODE (NEXT_INSN (barrier)) == JUMP_INSN
661       && (GET_CODE (PATTERN (NEXT_INSN (barrier))) == ADDR_VEC
662           || GET_CODE (PATTERN (NEXT_INSN (barrier))) == ADDR_DIFF_VEC))
663     {
664       /* Temporarily add the table jump insn to b, so that it will also
665          be moved to the correct location.  */
666       b->end = NEXT_INSN (barrier);
667       barrier = NEXT_INSN (b->end);
668     }
669
670   /* There had better have been a barrier there.  Delete it.  */
671   if (barrier && GET_CODE (barrier) == BARRIER)
672     delete_insn (barrier);
673
674   /* Move block and loop notes out of the chain so that we do not
675      disturb their order.
676
677      ??? A better solution would be to squeeze out all the non-nested notes
678      and adjust the block trees appropriately.   Even better would be to have
679      a tighter connection between block trees and rtl so that this is not
680      necessary.  */
681   if (squeeze_notes (&b->head, &b->end))
682     abort ();
683
684   /* Scramble the insn chain.  */
685   reorder_insns_nobb (b->head, b->end, a->end);
686
687   /* Restore the real end of b.  */
688   b->end = real_b_end;
689
690   /* Now blocks A and B are contiguous.  Merge them.  */
691   merge_blocks_nomove (a, b);
692   BB_SET_FLAG (a, BB_UPDATE_LIFE);
693
694   if (rtl_dump_file)
695     fprintf (rtl_dump_file, "Moved block %d after %d and merged.\n",
696              b->index, a->index);
697 }
698
699 /* Attempt to merge basic blocks that are potentially non-adjacent.
700    Return true iff the attempt succeeded.  */
701
702 static bool
703 merge_blocks (e, b, c, mode)
704      edge e;
705      basic_block b, c;
706      int mode;
707 {
708   /* If C has a tail recursion label, do not merge.  There is no
709      edge recorded from the call_placeholder back to this label, as
710      that would make optimize_sibling_and_tail_recursive_calls more
711      complex for no gain.  */
712   if ((mode & CLEANUP_PRE_SIBCALL)
713       && GET_CODE (c->head) == CODE_LABEL
714       && tail_recursion_label_p (c->head))
715     return false;
716
717   /* If B has a fallthru edge to C, no need to move anything.  */
718   if (e->flags & EDGE_FALLTHRU)
719     {
720       /* We need to update liveness in case C already has broken liveness
721          or B ends by conditional jump to next instructions that will be
722          removed.  */
723       if ((BB_FLAGS (c) & BB_UPDATE_LIFE)
724           || GET_CODE (b->end) == JUMP_INSN)
725         BB_SET_FLAG (b, BB_UPDATE_LIFE);
726       merge_blocks_nomove (b, c);
727       update_forwarder_flag (b);
728
729       if (rtl_dump_file)
730         fprintf (rtl_dump_file, "Merged %d and %d without moving.\n",
731                  b->index, c->index);
732
733       return true;
734     }
735
736   /* Otherwise we will need to move code around.  Do that only if expensive
737      transformations are allowed.  */
738   else if (mode & CLEANUP_EXPENSIVE)
739     {
740       edge tmp_edge, b_fallthru_edge;
741       bool c_has_outgoing_fallthru;
742       bool b_has_incoming_fallthru;
743
744       /* Avoid overactive code motion, as the forwarder blocks should be
745          eliminated by edge redirection instead.  One exception might have
746          been if B is a forwarder block and C has no fallthru edge, but
747          that should be cleaned up by bb-reorder instead.  */
748       if (FORWARDER_BLOCK_P (b) || FORWARDER_BLOCK_P (c))
749         return false;
750
751       /* We must make sure to not munge nesting of lexical blocks,
752          and loop notes.  This is done by squeezing out all the notes
753          and leaving them there to lie.  Not ideal, but functional.  */
754
755       for (tmp_edge = c->succ; tmp_edge; tmp_edge = tmp_edge->succ_next)
756         if (tmp_edge->flags & EDGE_FALLTHRU)
757           break;
758
759       c_has_outgoing_fallthru = (tmp_edge != NULL);
760
761       for (tmp_edge = b->pred; tmp_edge; tmp_edge = tmp_edge->pred_next)
762         if (tmp_edge->flags & EDGE_FALLTHRU)
763           break;
764
765       b_has_incoming_fallthru = (tmp_edge != NULL);
766       b_fallthru_edge = tmp_edge;
767
768       /* Otherwise, we're going to try to move C after B.  If C does
769          not have an outgoing fallthru, then it can be moved
770          immediately after B without introducing or modifying jumps.  */
771       if (! c_has_outgoing_fallthru)
772         {
773           merge_blocks_move_successor_nojumps (b, c);
774           return true;
775         }
776
777       /* If B does not have an incoming fallthru, then it can be moved
778          immediately before C without introducing or modifying jumps.
779          C cannot be the first block, so we do not have to worry about
780          accessing a non-existent block.  */
781
782       if (b_has_incoming_fallthru)
783         {
784           basic_block bb;
785
786           if (b_fallthru_edge->src == ENTRY_BLOCK_PTR)
787             return false;
788           bb = force_nonfallthru (b_fallthru_edge);
789           if (bb)
790             notice_new_block (bb);
791           else
792             BB_SET_FLAG (b_fallthru_edge->src, BB_UPDATE_LIFE);
793         }
794
795       merge_blocks_move_predecessor_nojumps (b, c);
796       return true;
797     }
798
799   return false;
800 }
801 \f
802
803 /* Return true if I1 and I2 are equivalent and thus can be crossjumped.  */
804
805 static bool
806 insns_match_p (mode, i1, i2)
807         int mode ATTRIBUTE_UNUSED;
808         rtx i1, i2;
809 {
810   rtx p1, p2;
811
812   /* Verify that I1 and I2 are equivalent.  */
813   if (GET_CODE (i1) != GET_CODE (i2))
814     return false;
815
816   p1 = PATTERN (i1);
817   p2 = PATTERN (i2);
818
819   if (GET_CODE (p1) != GET_CODE (p2))
820     return false;
821
822   /* If this is a CALL_INSN, compare register usage information.
823      If we don't check this on stack register machines, the two
824      CALL_INSNs might be merged leaving reg-stack.c with mismatching
825      numbers of stack registers in the same basic block.
826      If we don't check this on machines with delay slots, a delay slot may
827      be filled that clobbers a parameter expected by the subroutine.
828
829      ??? We take the simple route for now and assume that if they're
830      equal, they were constructed identically.  */
831
832   if (GET_CODE (i1) == CALL_INSN
833       && !rtx_equal_p (CALL_INSN_FUNCTION_USAGE (i1),
834                        CALL_INSN_FUNCTION_USAGE (i2)))
835     return false;
836
837 #ifdef STACK_REGS
838   /* If cross_jump_death_matters is not 0, the insn's mode
839      indicates whether or not the insn contains any stack-like
840      regs.  */
841
842   if ((mode & CLEANUP_POST_REGSTACK) && stack_regs_mentioned (i1))
843     {
844       /* If register stack conversion has already been done, then
845          death notes must also be compared before it is certain that
846          the two instruction streams match.  */
847
848       rtx note;
849       HARD_REG_SET i1_regset, i2_regset;
850
851       CLEAR_HARD_REG_SET (i1_regset);
852       CLEAR_HARD_REG_SET (i2_regset);
853
854       for (note = REG_NOTES (i1); note; note = XEXP (note, 1))
855         if (REG_NOTE_KIND (note) == REG_DEAD && STACK_REG_P (XEXP (note, 0)))
856           SET_HARD_REG_BIT (i1_regset, REGNO (XEXP (note, 0)));
857
858       for (note = REG_NOTES (i2); note; note = XEXP (note, 1))
859         if (REG_NOTE_KIND (note) == REG_DEAD && STACK_REG_P (XEXP (note, 0)))
860           SET_HARD_REG_BIT (i2_regset, REGNO (XEXP (note, 0)));
861
862       GO_IF_HARD_REG_EQUAL (i1_regset, i2_regset, done);
863
864       return false;
865
866     done:
867       ;
868     }
869 #endif
870
871   if (reload_completed
872       ? ! rtx_renumbered_equal_p (p1, p2) : ! rtx_equal_p (p1, p2))
873     {
874       /* The following code helps take care of G++ cleanups.  */
875       rtx equiv1 = find_reg_equal_equiv_note (i1);
876       rtx equiv2 = find_reg_equal_equiv_note (i2);
877
878       if (equiv1 && equiv2
879           /* If the equivalences are not to a constant, they may
880              reference pseudos that no longer exist, so we can't
881              use them.  */
882           && (! reload_completed
883               || (CONSTANT_P (XEXP (equiv1, 0))
884                   && rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))))
885         {
886           rtx s1 = single_set (i1);
887           rtx s2 = single_set (i2);
888           if (s1 != 0 && s2 != 0
889               && rtx_renumbered_equal_p (SET_DEST (s1), SET_DEST (s2)))
890             {
891               validate_change (i1, &SET_SRC (s1), XEXP (equiv1, 0), 1);
892               validate_change (i2, &SET_SRC (s2), XEXP (equiv2, 0), 1);
893               if (! rtx_renumbered_equal_p (p1, p2))
894                 cancel_changes (0);
895               else if (apply_change_group ())
896                 return true;
897             }
898         }
899
900       return false;
901     }
902
903   return true;
904 }
905 \f
906 /* Look through the insns at the end of BB1 and BB2 and find the longest
907    sequence that are equivalent.  Store the first insns for that sequence
908    in *F1 and *F2 and return the sequence length.
909
910    To simplify callers of this function, if the blocks match exactly,
911    store the head of the blocks in *F1 and *F2.  */
912
913 static int
914 flow_find_cross_jump (mode, bb1, bb2, f1, f2)
915      int mode ATTRIBUTE_UNUSED;
916      basic_block bb1, bb2;
917      rtx *f1, *f2;
918 {
919   rtx i1, i2, last1, last2, afterlast1, afterlast2;
920   int ninsns = 0;
921
922   /* Skip simple jumps at the end of the blocks.  Complex jumps still
923      need to be compared for equivalence, which we'll do below.  */
924
925   i1 = bb1->end;
926   last1 = afterlast1 = last2 = afterlast2 = NULL_RTX;
927   if (onlyjump_p (i1)
928       || (returnjump_p (i1) && !side_effects_p (PATTERN (i1))))
929     {
930       last1 = i1;
931       i1 = PREV_INSN (i1);
932     }
933
934   i2 = bb2->end;
935   if (onlyjump_p (i2)
936       || (returnjump_p (i2) && !side_effects_p (PATTERN (i2))))
937     {
938       last2 = i2;
939       /* Count everything except for unconditional jump as insn.  */
940       if (!simplejump_p (i2) && !returnjump_p (i2) && last1)
941         ninsns++;
942       i2 = PREV_INSN (i2);
943     }
944
945   while (true)
946     {
947       /* Ignore notes.  */
948       while (!active_insn_p (i1) && i1 != bb1->head)
949         i1 = PREV_INSN (i1);
950
951       while (!active_insn_p (i2) && i2 != bb2->head)
952         i2 = PREV_INSN (i2);
953
954       if (i1 == bb1->head || i2 == bb2->head)
955         break;
956
957       if (!insns_match_p (mode, i1, i2))
958         break;
959
960       /* Don't begin a cross-jump with a USE or CLOBBER insn.  */
961       if (active_insn_p (i1))
962         {
963           /* If the merged insns have different REG_EQUAL notes, then
964              remove them.  */
965           rtx equiv1 = find_reg_equal_equiv_note (i1);
966           rtx equiv2 = find_reg_equal_equiv_note (i2);
967
968           if (equiv1 && !equiv2)
969             remove_note (i1, equiv1);
970           else if (!equiv1 && equiv2)
971             remove_note (i2, equiv2);
972           else if (equiv1 && equiv2
973                    && !rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))
974             {
975               remove_note (i1, equiv1);
976               remove_note (i2, equiv2);
977             }
978              
979           afterlast1 = last1, afterlast2 = last2;
980           last1 = i1, last2 = i2;
981           ninsns++;
982         }
983
984       i1 = PREV_INSN (i1);
985       i2 = PREV_INSN (i2);
986     }
987
988 #ifdef HAVE_cc0
989   /* Don't allow the insn after a compare to be shared by
990      cross-jumping unless the compare is also shared.  */
991   if (ninsns && reg_mentioned_p (cc0_rtx, last1) && ! sets_cc0_p (last1))
992     last1 = afterlast1, last2 = afterlast2, ninsns--;
993 #endif
994
995   /* Include preceding notes and labels in the cross-jump.  One,
996      this may bring us to the head of the blocks as requested above.
997      Two, it keeps line number notes as matched as may be.  */
998   if (ninsns)
999     {
1000       while (last1 != bb1->head && !active_insn_p (PREV_INSN (last1)))
1001         last1 = PREV_INSN (last1);
1002
1003       if (last1 != bb1->head && GET_CODE (PREV_INSN (last1)) == CODE_LABEL)
1004         last1 = PREV_INSN (last1);
1005
1006       while (last2 != bb2->head && !active_insn_p (PREV_INSN (last2)))
1007         last2 = PREV_INSN (last2);
1008
1009       if (last2 != bb2->head && GET_CODE (PREV_INSN (last2)) == CODE_LABEL)
1010         last2 = PREV_INSN (last2);
1011
1012       *f1 = last1;
1013       *f2 = last2;
1014     }
1015
1016   return ninsns;
1017 }
1018
1019 /* Return true iff outgoing edges of BB1 and BB2 match, together with
1020    the branch instruction.  This means that if we commonize the control
1021    flow before end of the basic block, the semantic remains unchanged.
1022
1023    We may assume that there exists one edge with a common destination.  */
1024
1025 static bool
1026 outgoing_edges_match (mode, bb1, bb2)
1027      int mode;
1028      basic_block bb1;
1029      basic_block bb2;
1030 {
1031   int nehedges1 = 0, nehedges2 = 0;
1032   edge fallthru1 = 0, fallthru2 = 0;
1033   edge e1, e2;
1034
1035   /* If BB1 has only one successor, we may be looking at either an
1036      unconditional jump, or a fake edge to exit.  */
1037   if (bb1->succ && !bb1->succ->succ_next
1038       && !(bb1->succ->flags & (EDGE_COMPLEX | EDGE_FAKE)))
1039     return (bb2->succ &&  !bb2->succ->succ_next
1040             && (bb2->succ->flags & (EDGE_COMPLEX | EDGE_FAKE)) == 0);
1041
1042   /* Match conditional jumps - this may get tricky when fallthru and branch
1043      edges are crossed.  */
1044   if (bb1->succ
1045       && bb1->succ->succ_next
1046       && !bb1->succ->succ_next->succ_next
1047       && any_condjump_p (bb1->end)
1048       && onlyjump_p (bb1->end))
1049     {
1050       edge b1, f1, b2, f2;
1051       bool reverse, match;
1052       rtx set1, set2, cond1, cond2;
1053       enum rtx_code code1, code2;
1054
1055       if (!bb2->succ
1056           || !bb2->succ->succ_next
1057           || bb1->succ->succ_next->succ_next
1058           || !any_condjump_p (bb2->end)
1059           || !onlyjump_p (bb1->end))
1060         return false;
1061
1062       b1 = BRANCH_EDGE (bb1);
1063       b2 = BRANCH_EDGE (bb2);
1064       f1 = FALLTHRU_EDGE (bb1);
1065       f2 = FALLTHRU_EDGE (bb2);
1066
1067       /* Get around possible forwarders on fallthru edges.  Other cases
1068          should be optimized out already.  */
1069       if (FORWARDER_BLOCK_P (f1->dest))
1070         f1 = f1->dest->succ;
1071
1072       if (FORWARDER_BLOCK_P (f2->dest))
1073         f2 = f2->dest->succ;
1074
1075       /* To simplify use of this function, return false if there are
1076          unneeded forwarder blocks.  These will get eliminated later
1077          during cleanup_cfg.  */
1078       if (FORWARDER_BLOCK_P (f1->dest)
1079           || FORWARDER_BLOCK_P (f2->dest)
1080           || FORWARDER_BLOCK_P (b1->dest)
1081           || FORWARDER_BLOCK_P (b2->dest))
1082         return false;
1083
1084       if (f1->dest == f2->dest && b1->dest == b2->dest)
1085         reverse = false;
1086       else if (f1->dest == b2->dest && b1->dest == f2->dest)
1087         reverse = true;
1088       else
1089         return false;
1090
1091       set1 = pc_set (bb1->end);
1092       set2 = pc_set (bb2->end);
1093       if ((XEXP (SET_SRC (set1), 1) == pc_rtx)
1094           != (XEXP (SET_SRC (set2), 1) == pc_rtx))
1095         reverse = !reverse;
1096
1097       cond1 = XEXP (SET_SRC (set1), 0);
1098       cond2 = XEXP (SET_SRC (set2), 0);
1099       code1 = GET_CODE (cond1);
1100       if (reverse)
1101         code2 = reversed_comparison_code (cond2, bb2->end);
1102       else
1103         code2 = GET_CODE (cond2);
1104
1105       if (code2 == UNKNOWN)
1106         return false;
1107
1108       /* Verify codes and operands match.  */
1109       match = ((code1 == code2
1110                 && rtx_renumbered_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
1111                 && rtx_renumbered_equal_p (XEXP (cond1, 1), XEXP (cond2, 1)))
1112                || (code1 == swap_condition (code2)
1113                    && rtx_renumbered_equal_p (XEXP (cond1, 1),
1114                                               XEXP (cond2, 0))
1115                    && rtx_renumbered_equal_p (XEXP (cond1, 0),
1116                                               XEXP (cond2, 1))));
1117
1118       /* If we return true, we will join the blocks.  Which means that
1119          we will only have one branch prediction bit to work with.  Thus
1120          we require the existing branches to have probabilities that are
1121          roughly similar.  */
1122       /* ??? We should use bb->frequency to allow merging in infrequently
1123          executed blocks, but at the moment it is not available when
1124          cleanup_cfg is run.  */
1125       if (match && !optimize_size)
1126         {
1127           rtx note1, note2;
1128           int prob1, prob2;
1129
1130           note1 = find_reg_note (bb1->end, REG_BR_PROB, 0);
1131           note2 = find_reg_note (bb2->end, REG_BR_PROB, 0);
1132
1133           if (note1 && note2)
1134             {
1135               prob1 = INTVAL (XEXP (note1, 0));
1136               prob2 = INTVAL (XEXP (note2, 0));
1137               if (reverse)
1138                 prob2 = REG_BR_PROB_BASE - prob2;
1139
1140               /* Fail if the difference in probabilities is
1141                  greater than 5%.  */
1142               if (abs (prob1 - prob2) > REG_BR_PROB_BASE / 20)
1143                 return false;
1144             }
1145
1146           else if (note1 || note2)
1147             return false;
1148         }
1149
1150       if (rtl_dump_file && match)
1151         fprintf (rtl_dump_file, "Conditionals in bb %i and %i match.\n",
1152                  bb1->index, bb2->index);
1153
1154       return match;
1155     }
1156
1157   /* Generic case - we are seeing an computed jump, table jump or trapping
1158      instruction.  */
1159
1160   /* First ensure that the instructions match.  There may be many outgoing
1161      edges so this test is generally cheaper.
1162      ??? Currently the tablejumps will never match, as they do have
1163      different tables.  */
1164   if (!insns_match_p (mode, bb1->end, bb2->end))
1165     return false;
1166
1167   /* Search the outgoing edges, ensure that the counts do match, find possible
1168      fallthru and exception handling edges since these needs more
1169      validation.  */
1170   for (e1 = bb1->succ, e2 = bb2->succ; e1 && e2;
1171        e1 = e1->succ_next, e2 = e2->succ_next)
1172     {
1173       if (e1->flags & EDGE_EH)
1174         nehedges1++;
1175
1176       if (e2->flags & EDGE_EH)
1177         nehedges2++;
1178
1179       if (e1->flags & EDGE_FALLTHRU)
1180         fallthru1 = e1;
1181       if (e2->flags & EDGE_FALLTHRU)
1182         fallthru2 = e2;
1183     }
1184
1185   /* If number of edges of various types does not match, fail.  */
1186   if (e1 || e2
1187       || nehedges1 != nehedges2
1188       || (fallthru1 != 0) != (fallthru2 != 0))
1189     return false;
1190
1191   /* fallthru edges must be forwarded to the same destination.  */
1192   if (fallthru1)
1193     {
1194       basic_block d1 = (forwarder_block_p (fallthru1->dest)
1195                         ? fallthru1->dest->succ->dest: fallthru1->dest);
1196       basic_block d2 = (forwarder_block_p (fallthru2->dest)
1197                         ? fallthru2->dest->succ->dest: fallthru2->dest);
1198
1199       if (d1 != d2)
1200         return false;
1201     }
1202
1203   /* In case we do have EH edges, ensure we are in the same region.  */
1204   if (nehedges1)
1205     {
1206       rtx n1 = find_reg_note (bb1->end, REG_EH_REGION, 0);
1207       rtx n2 = find_reg_note (bb2->end, REG_EH_REGION, 0);
1208
1209       if (XEXP (n1, 0) != XEXP (n2, 0))
1210         return false;
1211     }
1212
1213   /* We don't need to match the rest of edges as above checks should be enought
1214      to ensure that they are equivalent.  */
1215   return true;
1216 }
1217
1218 /* E1 and E2 are edges with the same destination block.  Search their
1219    predecessors for common code.  If found, redirect control flow from
1220    (maybe the middle of) E1->SRC to (maybe the middle of) E2->SRC.  */
1221
1222 static bool
1223 try_crossjump_to_edge (mode, e1, e2)
1224      int mode;
1225      edge e1, e2;
1226 {
1227   int nmatch;
1228   basic_block src1 = e1->src, src2 = e2->src;
1229   basic_block redirect_to;
1230   rtx newpos1, newpos2;
1231   edge s;
1232   rtx last;
1233   rtx label;
1234   rtx note;
1235
1236   /* Search backward through forwarder blocks.  We don't need to worry
1237      about multiple entry or chained forwarders, as they will be optimized
1238      away.  We do this to look past the unconditional jump following a
1239      conditional jump that is required due to the current CFG shape.  */
1240   if (src1->pred
1241       && !src1->pred->pred_next
1242       && FORWARDER_BLOCK_P (src1))
1243     e1 = src1->pred, src1 = e1->src;
1244
1245   if (src2->pred
1246       && !src2->pred->pred_next
1247       && FORWARDER_BLOCK_P (src2))
1248     e2 = src2->pred, src2 = e2->src;
1249
1250   /* Nothing to do if we reach ENTRY, or a common source block.  */
1251   if (src1 == ENTRY_BLOCK_PTR || src2 == ENTRY_BLOCK_PTR)
1252     return false;
1253   if (src1 == src2)
1254     return false;
1255
1256   /* Seeing more than 1 forwarder blocks would confuse us later...  */
1257   if (FORWARDER_BLOCK_P (e1->dest)
1258       && FORWARDER_BLOCK_P (e1->dest->succ->dest))
1259     return false;
1260
1261   if (FORWARDER_BLOCK_P (e2->dest)
1262       && FORWARDER_BLOCK_P (e2->dest->succ->dest))
1263     return false;
1264
1265   /* Likewise with dead code (possibly newly created by the other optimizations
1266      of cfg_cleanup).  */
1267   if (!src1->pred || !src2->pred)
1268     return false;
1269
1270   /* Look for the common insn sequence, part the first ...  */
1271   if (!outgoing_edges_match (mode, src1, src2))
1272     return false;
1273
1274   /* ... and part the second.  */
1275   nmatch = flow_find_cross_jump (mode, src1, src2, &newpos1, &newpos2);
1276   if (!nmatch)
1277     return false;
1278
1279   /* Avoid splitting if possible.  */
1280   if (newpos2 == src2->head)
1281     redirect_to = src2;
1282   else
1283     {
1284       if (rtl_dump_file)
1285         fprintf (rtl_dump_file, "Splitting bb %i before %i insns\n",
1286                  src2->index, nmatch);
1287       redirect_to = split_block (src2, PREV_INSN (newpos2))->dest;
1288     }
1289
1290   if (rtl_dump_file)
1291     fprintf (rtl_dump_file,
1292              "Cross jumping from bb %i to bb %i; %i common insns\n",
1293              src1->index, src2->index, nmatch);
1294
1295   redirect_to->count += src1->count;
1296   redirect_to->frequency += src1->frequency;
1297
1298   /* Recompute the frequencies and counts of outgoing edges.  */
1299   for (s = redirect_to->succ; s; s = s->succ_next)
1300     {
1301       edge s2;
1302       basic_block d = s->dest;
1303
1304       if (FORWARDER_BLOCK_P (d))
1305         d = d->succ->dest;
1306
1307       for (s2 = src1->succ; ; s2 = s2->succ_next)
1308         {
1309           basic_block d2 = s2->dest;
1310           if (FORWARDER_BLOCK_P (d2))
1311             d2 = d2->succ->dest;
1312           if (d == d2)
1313             break;
1314         }
1315
1316       s->count += s2->count;
1317
1318       /* Take care to update possible forwarder blocks.  We verified
1319          that there is no more than one in the chain, so we can't run
1320          into infinite loop.  */
1321       if (FORWARDER_BLOCK_P (s->dest))
1322         {
1323           s->dest->succ->count += s2->count;
1324           s->dest->count += s2->count;
1325           s->dest->frequency += EDGE_FREQUENCY (s);
1326         }
1327
1328       if (FORWARDER_BLOCK_P (s2->dest))
1329         {
1330           s2->dest->succ->count -= s2->count;
1331           s2->dest->count -= s2->count;
1332           s2->dest->frequency -= EDGE_FREQUENCY (s);
1333         }
1334
1335       if (!redirect_to->frequency && !src1->frequency)
1336         s->probability = (s->probability + s2->probability) / 2;
1337       else
1338         s->probability
1339           = ((s->probability * redirect_to->frequency +
1340               s2->probability * src1->frequency)
1341              / (redirect_to->frequency + src1->frequency));
1342     }
1343
1344   note = find_reg_note (redirect_to->end, REG_BR_PROB, 0);
1345   if (note)
1346     XEXP (note, 0) = GEN_INT (BRANCH_EDGE (redirect_to)->probability);
1347
1348   /* Edit SRC1 to go to REDIRECT_TO at NEWPOS1.  */
1349
1350   /* Skip possible basic block header.  */
1351   if (GET_CODE (newpos1) == CODE_LABEL)
1352     newpos1 = NEXT_INSN (newpos1);
1353
1354   if (GET_CODE (newpos1) == NOTE)
1355     newpos1 = NEXT_INSN (newpos1);
1356   last = src1->end;
1357
1358   /* Emit the jump insn.  */
1359   label = block_label (redirect_to);
1360   emit_jump_insn_after (gen_jump (label), src1->end);
1361   JUMP_LABEL (src1->end) = label;
1362   LABEL_NUSES (label)++;
1363
1364   /* Delete the now unreachable instructions.  */
1365   delete_insn_chain (newpos1, last);
1366
1367   /* Make sure there is a barrier after the new jump.  */
1368   last = next_nonnote_insn (src1->end);
1369   if (!last || GET_CODE (last) != BARRIER)
1370     emit_barrier_after (src1->end);
1371
1372   /* Update CFG.  */
1373   while (src1->succ)
1374     remove_edge (src1->succ);
1375   make_single_succ_edge (src1, redirect_to, 0);
1376
1377   BB_SET_FLAG (src1, BB_UPDATE_LIFE);
1378   update_forwarder_flag (src1);
1379
1380   return true;
1381 }
1382
1383 /* Search the predecessors of BB for common insn sequences.  When found,
1384    share code between them by redirecting control flow.  Return true if
1385    any changes made.  */
1386
1387 static bool
1388 try_crossjump_bb (mode, bb)
1389      int mode;
1390      basic_block bb;
1391 {
1392   edge e, e2, nexte2, nexte, fallthru;
1393   bool changed;
1394
1395   /* Nothing to do if there is not at least two incoming edges.  */
1396   if (!bb->pred || !bb->pred->pred_next)
1397     return false;
1398
1399   /* It is always cheapest to redirect a block that ends in a branch to
1400      a block that falls through into BB, as that adds no branches to the
1401      program.  We'll try that combination first.  */
1402   for (fallthru = bb->pred; fallthru; fallthru = fallthru->pred_next)
1403     if (fallthru->flags & EDGE_FALLTHRU)
1404       break;
1405
1406   changed = false;
1407   for (e = bb->pred; e; e = nexte)
1408     {
1409       nexte = e->pred_next;
1410
1411       /* As noted above, first try with the fallthru predecessor.  */
1412       if (fallthru)
1413         {
1414           /* Don't combine the fallthru edge into anything else.
1415              If there is a match, we'll do it the other way around.  */
1416           if (e == fallthru)
1417             continue;
1418
1419           if (try_crossjump_to_edge (mode, e, fallthru))
1420             {
1421               changed = true;
1422               nexte = bb->pred;
1423               continue;
1424             }
1425         }
1426
1427       /* Non-obvious work limiting check: Recognize that we're going
1428          to call try_crossjump_bb on every basic block.  So if we have
1429          two blocks with lots of outgoing edges (a switch) and they
1430          share lots of common destinations, then we would do the
1431          cross-jump check once for each common destination.
1432
1433          Now, if the blocks actually are cross-jump candidates, then
1434          all of their destinations will be shared.  Which means that
1435          we only need check them for cross-jump candidacy once.  We
1436          can eliminate redundant checks of crossjump(A,B) by arbitrarily
1437          choosing to do the check from the block for which the edge
1438          in question is the first successor of A.  */
1439       if (e->src->succ != e)
1440         continue;
1441
1442       for (e2 = bb->pred; e2; e2 = nexte2)
1443         {
1444           nexte2 = e2->pred_next;
1445
1446           if (e2 == e)
1447             continue;
1448
1449           /* We've already checked the fallthru edge above.  */
1450           if (e2 == fallthru)
1451             continue;
1452
1453           /* The "first successor" check above only prevents multiple
1454              checks of crossjump(A,B).  In order to prevent redundant
1455              checks of crossjump(B,A), require that A be the block
1456              with the lowest index.  */
1457           if (e->src->index > e2->src->index)
1458             continue;
1459
1460           if (try_crossjump_to_edge (mode, e, e2))
1461             {
1462               changed = true;
1463               nexte = bb->pred;
1464               break;
1465             }
1466         }
1467     }
1468
1469   return changed;
1470 }
1471
1472 /* Do simple CFG optimizations - basic block merging, simplifying of jump
1473    instructions etc.  Return nonzero if changes were made.  */
1474
1475 static bool
1476 try_optimize_cfg (mode)
1477      int mode;
1478 {
1479   int i;
1480   bool changed_overall = false;
1481   bool changed;
1482   int iterations = 0;
1483   sbitmap blocks;
1484
1485   if (mode & CLEANUP_CROSSJUMP)
1486     add_noreturn_fake_exit_edges ();
1487
1488   for (i = 0; i < n_basic_blocks; i++)
1489     update_forwarder_flag (BASIC_BLOCK (i));
1490
1491   /* Attempt to merge blocks as made possible by edge removal.  If a block
1492      has only one successor, and the successor has only one predecessor,
1493      they may be combined.  */
1494   do
1495     {
1496       changed = false;
1497       iterations++;
1498
1499       if (rtl_dump_file)
1500         fprintf (rtl_dump_file, "\n\ntry_optimize_cfg iteration %i\n\n",
1501                  iterations);
1502
1503       for (i = 0; i < n_basic_blocks;)
1504         {
1505           basic_block c, b = BASIC_BLOCK (i);
1506           edge s;
1507           bool changed_here = false;
1508
1509           /* Delete trivially dead basic blocks.  */
1510           while (b->pred == NULL)
1511             {
1512               c = BASIC_BLOCK (b->index - 1);
1513               if (rtl_dump_file)
1514                 fprintf (rtl_dump_file, "Deleting block %i.\n", b->index);
1515
1516               flow_delete_block (b);
1517               changed = true;
1518               b = c;
1519             }
1520
1521           /* Remove code labels no longer used.  Don't do this before
1522              CALL_PLACEHOLDER is removed, as some branches may be hidden
1523              within.  */
1524           if (b->pred->pred_next == NULL
1525               && (b->pred->flags & EDGE_FALLTHRU)
1526               && !(b->pred->flags & EDGE_COMPLEX)
1527               && GET_CODE (b->head) == CODE_LABEL
1528               && (!(mode & CLEANUP_PRE_SIBCALL)
1529                   || !tail_recursion_label_p (b->head))
1530               /* If the previous block ends with a branch to this block,
1531                  we can't delete the label.  Normally this is a condjump
1532                  that is yet to be simplified, but if CASE_DROPS_THRU,
1533                  this can be a tablejump with some element going to the
1534                  same place as the default (fallthru).  */
1535               && (b->pred->src == ENTRY_BLOCK_PTR
1536                   || GET_CODE (b->pred->src->end) != JUMP_INSN
1537                   || ! label_is_jump_target_p (b->head, b->pred->src->end)))
1538             {
1539               rtx label = b->head;
1540
1541               b->head = NEXT_INSN (b->head);
1542               delete_insn_chain (label, label);
1543               if (rtl_dump_file)
1544                 fprintf (rtl_dump_file, "Deleted label in block %i.\n",
1545                          b->index);
1546             }
1547
1548           /* If we fall through an empty block, we can remove it.  */
1549           if (b->pred->pred_next == NULL
1550               && (b->pred->flags & EDGE_FALLTHRU)
1551               && GET_CODE (b->head) != CODE_LABEL
1552               && FORWARDER_BLOCK_P (b)
1553               /* Note that forwarder_block_p true ensures that there
1554                  is a successor for this block.  */
1555               && (b->succ->flags & EDGE_FALLTHRU)
1556               && n_basic_blocks > 1)
1557             {
1558               if (rtl_dump_file)
1559                 fprintf (rtl_dump_file, "Deleting fallthru block %i.\n",
1560                          b->index);
1561
1562               c = BASIC_BLOCK (b->index ? b->index - 1 : 1);
1563               redirect_edge_succ_nodup (b->pred, b->succ->dest);
1564               flow_delete_block (b);
1565               changed = true;
1566               b = c;
1567             }
1568
1569           /* Merge blocks.  Loop because chains of blocks might be
1570              combineable.  */
1571           while ((s = b->succ) != NULL
1572                  && s->succ_next == NULL
1573                  && !(s->flags & EDGE_COMPLEX)
1574                  && (c = s->dest) != EXIT_BLOCK_PTR
1575                  && c->pred->pred_next == NULL
1576                  /* If the jump insn has side effects,
1577                     we can't kill the edge.  */
1578                  && (GET_CODE (b->end) != JUMP_INSN
1579                      || onlyjump_p (b->end))
1580                  && merge_blocks (s, b, c, mode))
1581             changed_here = true;
1582
1583           /* Simplify branch over branch.  */
1584           if ((mode & CLEANUP_EXPENSIVE) && try_simplify_condjump (b))
1585             {
1586               BB_SET_FLAG (b, BB_UPDATE_LIFE);
1587               changed_here = true;
1588             }
1589
1590           /* If B has a single outgoing edge, but uses a non-trivial jump
1591              instruction without side-effects, we can either delete the
1592              jump entirely, or replace it with a simple unconditional jump.
1593              Use redirect_edge_and_branch to do the dirty work.  */
1594           if (b->succ
1595               && ! b->succ->succ_next
1596               && b->succ->dest != EXIT_BLOCK_PTR
1597               && onlyjump_p (b->end)
1598               && redirect_edge_and_branch (b->succ, b->succ->dest))
1599             {
1600               BB_SET_FLAG (b, BB_UPDATE_LIFE);
1601               update_forwarder_flag (b);
1602               changed_here = true;
1603             }
1604
1605           /* Simplify branch to branch.  */
1606           if (try_forward_edges (mode, b))
1607             changed_here = true;
1608
1609           /* Look for shared code between blocks.  */
1610           if ((mode & CLEANUP_CROSSJUMP)
1611               && try_crossjump_bb (mode, b))
1612             changed_here = true;
1613
1614           /* Don't get confused by the index shift caused by deleting
1615              blocks.  */
1616           if (!changed_here)
1617             i = b->index + 1;
1618           else
1619             changed = true;
1620         }
1621
1622       if ((mode & CLEANUP_CROSSJUMP)
1623           && try_crossjump_bb (mode, EXIT_BLOCK_PTR))
1624         changed = true;
1625
1626 #ifdef ENABLE_CHECKING
1627       if (changed)
1628         verify_flow_info ();
1629 #endif
1630
1631       changed_overall |= changed;
1632     }
1633   while (changed);
1634
1635   if (mode & CLEANUP_CROSSJUMP)
1636     remove_fake_edges ();
1637
1638   if ((mode & CLEANUP_UPDATE_LIFE) && changed_overall)
1639     {
1640       bool found = 0;
1641
1642       blocks = sbitmap_alloc (n_basic_blocks);
1643       sbitmap_zero (blocks);
1644       for (i = 0; i < n_basic_blocks; i++)
1645         if (BB_FLAGS (BASIC_BLOCK (i)) & BB_UPDATE_LIFE)
1646           {
1647             found = 1;
1648             SET_BIT (blocks, i);
1649           }
1650
1651       if (found)
1652         update_life_info (blocks, UPDATE_LIFE_GLOBAL,
1653                           PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
1654                           | PROP_KILL_DEAD_CODE);
1655       sbitmap_free (blocks);
1656     }
1657
1658   for (i = 0; i < n_basic_blocks; i++)
1659     BASIC_BLOCK (i)->aux = NULL;
1660
1661   return changed_overall;
1662 }
1663 \f
1664 /* Delete all unreachable basic blocks.  */
1665
1666 static bool
1667 delete_unreachable_blocks ()
1668 {
1669   int i;
1670   bool changed = false;
1671
1672   find_unreachable_blocks ();
1673
1674   /* Delete all unreachable basic blocks.  Count down so that we
1675      don't interfere with the block renumbering that happens in
1676      flow_delete_block.  */
1677
1678   for (i = n_basic_blocks - 1; i >= 0; --i)
1679     {
1680       basic_block b = BASIC_BLOCK (i);
1681
1682       if (!(b->flags & BB_REACHABLE))
1683         flow_delete_block (b), changed = true;
1684     }
1685
1686   if (changed)
1687     tidy_fallthru_edges ();
1688   return changed;
1689 }
1690 \f
1691 /* Tidy the CFG by deleting unreachable code and whatnot.  */
1692
1693 bool
1694 cleanup_cfg (mode)
1695      int mode;
1696 {
1697   bool changed = false;
1698
1699   timevar_push (TV_CLEANUP_CFG);
1700   changed = delete_unreachable_blocks ();
1701   if (try_optimize_cfg (mode))
1702     delete_unreachable_blocks (), changed = true;
1703
1704   /* Kill the data we won't maintain.  */
1705   free_EXPR_LIST_list (&label_value_list);
1706   free_EXPR_LIST_list (&tail_recursion_label_list);
1707   timevar_pop (TV_CLEANUP_CFG);
1708
1709   return changed;
1710 }