OSDN Git Service

* basic-block.h (struct basic_block_def): Reorder fields to
[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, 2002, 2003, 2004 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 entry point is
23    cleanup_cfg.  Following optimizations are performed:
24
25    - Unreachable blocks removal
26    - Edge forwarding (edge to the forwarder block is forwarded to its
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 "coretypes.h"
37 #include "tm.h"
38 #include "rtl.h"
39 #include "hard-reg-set.h"
40 #include "basic-block.h"
41 #include "timevar.h"
42 #include "output.h"
43 #include "insn-config.h"
44 #include "flags.h"
45 #include "recog.h"
46 #include "toplev.h"
47 #include "cselib.h"
48 #include "params.h"
49 #include "tm_p.h"
50 #include "target.h"
51 #include "regs.h"
52 #include "cfglayout.h"
53 #include "emit-rtl.h"
54
55 /* cleanup_cfg maintains following flags for each basic block.  */
56
57 enum bb_flags
58 {
59     /* Set if BB is the forwarder block to avoid too many
60        forwarder_block_p calls.  */
61     BB_FORWARDER_BLOCK = 1,
62     BB_NONTHREADABLE_BLOCK = 2
63 };
64
65 #define BB_FLAGS(BB) (enum bb_flags) (BB)->aux
66 #define BB_SET_FLAG(BB, FLAG) \
67   (BB)->aux = (void *) (long) ((enum bb_flags) (BB)->aux | (FLAG))
68 #define BB_CLEAR_FLAG(BB, FLAG) \
69   (BB)->aux = (void *) (long) ((enum bb_flags) (BB)->aux & ~(FLAG))
70
71 #define FORWARDER_BLOCK_P(BB) (BB_FLAGS (BB) & BB_FORWARDER_BLOCK)
72
73 /* Set to true when we are running first pass of try_optimize_cfg loop.  */
74 static bool first_pass;
75 static bool try_crossjump_to_edge (int, edge, edge);
76 static bool try_crossjump_bb (int, basic_block);
77 static bool outgoing_edges_match (int, basic_block, basic_block);
78 static int flow_find_cross_jump (int, basic_block, basic_block, rtx *, rtx *);
79 static bool insns_match_p (int, rtx, rtx);
80
81 static void merge_blocks_move_predecessor_nojumps (basic_block, basic_block);
82 static void merge_blocks_move_successor_nojumps (basic_block, basic_block);
83 static bool try_optimize_cfg (int);
84 static bool try_simplify_condjump (basic_block);
85 static bool try_forward_edges (int, basic_block);
86 static edge thread_jump (int, edge, basic_block);
87 static bool mark_effect (rtx, bitmap);
88 static void notice_new_block (basic_block);
89 static void update_forwarder_flag (basic_block);
90 static int mentions_nonequal_regs (rtx *, void *);
91 static void merge_memattrs (rtx, rtx);
92 \f
93 /* Set flags for newly created block.  */
94
95 static void
96 notice_new_block (basic_block bb)
97 {
98   if (!bb)
99     return;
100
101   if (forwarder_block_p (bb))
102     BB_SET_FLAG (bb, BB_FORWARDER_BLOCK);
103 }
104
105 /* Recompute forwarder flag after block has been modified.  */
106
107 static void
108 update_forwarder_flag (basic_block bb)
109 {
110   if (forwarder_block_p (bb))
111     BB_SET_FLAG (bb, BB_FORWARDER_BLOCK);
112   else
113     BB_CLEAR_FLAG (bb, BB_FORWARDER_BLOCK);
114 }
115 \f
116 /* Simplify a conditional jump around an unconditional jump.
117    Return true if something changed.  */
118
119 static bool
120 try_simplify_condjump (basic_block cbranch_block)
121 {
122   basic_block jump_block, jump_dest_block, cbranch_dest_block;
123   edge cbranch_jump_edge, cbranch_fallthru_edge;
124   rtx cbranch_insn;
125
126   /* Verify that there are exactly two successors.  */
127   if (!cbranch_block->succ
128       || !cbranch_block->succ->succ_next
129       || cbranch_block->succ->succ_next->succ_next)
130     return false;
131
132   /* Verify that we've got a normal conditional branch at the end
133      of the block.  */
134   cbranch_insn = BB_END (cbranch_block);
135   if (!any_condjump_p (cbranch_insn))
136     return false;
137
138   cbranch_fallthru_edge = FALLTHRU_EDGE (cbranch_block);
139   cbranch_jump_edge = BRANCH_EDGE (cbranch_block);
140
141   /* The next block must not have multiple predecessors, must not
142      be the last block in the function, and must contain just the
143      unconditional jump.  */
144   jump_block = cbranch_fallthru_edge->dest;
145   if (jump_block->pred->pred_next
146       || jump_block->next_bb == EXIT_BLOCK_PTR
147       || !FORWARDER_BLOCK_P (jump_block))
148     return false;
149   jump_dest_block = jump_block->succ->dest;
150
151   /* If we are partitioning hot/cold basic blocks, we don't want to
152      mess up unconditional or indirect jumps that cross between hot
153      and cold sections.  */
154
155   if (flag_reorder_blocks_and_partition
156       && (BB_PARTITION (jump_block) != BB_PARTITION (jump_dest_block)
157           || (cbranch_jump_edge->flags & EDGE_CROSSING)))
158     return false;
159
160   /* The conditional branch must target the block after the
161      unconditional branch.  */
162   cbranch_dest_block = cbranch_jump_edge->dest;
163
164   if (cbranch_dest_block == EXIT_BLOCK_PTR
165       || !can_fallthru (jump_block, cbranch_dest_block))
166     return false;
167
168   /* Invert the conditional branch.  */
169   if (!invert_jump (cbranch_insn, block_label (jump_dest_block), 0))
170     return false;
171
172   if (dump_file)
173     fprintf (dump_file, "Simplifying condjump %i around jump %i\n",
174              INSN_UID (cbranch_insn), INSN_UID (BB_END (jump_block)));
175
176   /* Success.  Update the CFG to match.  Note that after this point
177      the edge variable names appear backwards; the redirection is done
178      this way to preserve edge profile data.  */
179   cbranch_jump_edge = redirect_edge_succ_nodup (cbranch_jump_edge,
180                                                 cbranch_dest_block);
181   cbranch_fallthru_edge = redirect_edge_succ_nodup (cbranch_fallthru_edge,
182                                                     jump_dest_block);
183   cbranch_jump_edge->flags |= EDGE_FALLTHRU;
184   cbranch_fallthru_edge->flags &= ~EDGE_FALLTHRU;
185   update_br_prob_note (cbranch_block);
186
187   /* Delete the block with the unconditional jump, and clean up the mess.  */
188   delete_basic_block (jump_block);
189   tidy_fallthru_edge (cbranch_jump_edge);
190   update_forwarder_flag (cbranch_block);
191
192   return true;
193 }
194 \f
195 /* Attempt to prove that operation is NOOP using CSElib or mark the effect
196    on register.  Used by jump threading.  */
197
198 static bool
199 mark_effect (rtx exp, regset nonequal)
200 {
201   int regno;
202   rtx dest;
203   switch (GET_CODE (exp))
204     {
205       /* In case we do clobber the register, mark it as equal, as we know the
206          value is dead so it don't have to match.  */
207     case CLOBBER:
208       if (REG_P (XEXP (exp, 0)))
209         {
210           dest = XEXP (exp, 0);
211           regno = REGNO (dest);
212           CLEAR_REGNO_REG_SET (nonequal, regno);
213           if (regno < FIRST_PSEUDO_REGISTER)
214             {
215               int n = hard_regno_nregs[regno][GET_MODE (dest)];
216               while (--n > 0)
217                 CLEAR_REGNO_REG_SET (nonequal, regno + n);
218             }
219         }
220       return false;
221
222     case SET:
223       if (rtx_equal_for_cselib_p (SET_DEST (exp), SET_SRC (exp)))
224         return false;
225       dest = SET_DEST (exp);
226       if (dest == pc_rtx)
227         return false;
228       if (!REG_P (dest))
229         return true;
230       regno = REGNO (dest);
231       SET_REGNO_REG_SET (nonequal, regno);
232       if (regno < FIRST_PSEUDO_REGISTER)
233         {
234           int n = hard_regno_nregs[regno][GET_MODE (dest)];
235           while (--n > 0)
236             SET_REGNO_REG_SET (nonequal, regno + n);
237         }
238       return false;
239
240     default:
241       return false;
242     }
243 }
244
245 /* Return nonzero if X is a register set in regset DATA.
246    Called via for_each_rtx.  */
247 static int
248 mentions_nonequal_regs (rtx *x, void *data)
249 {
250   regset nonequal = (regset) data;
251   if (REG_P (*x))
252     {
253       int regno;
254
255       regno = REGNO (*x);
256       if (REGNO_REG_SET_P (nonequal, regno))
257         return 1;
258       if (regno < FIRST_PSEUDO_REGISTER)
259         {
260           int n = hard_regno_nregs[regno][GET_MODE (*x)];
261           while (--n > 0)
262             if (REGNO_REG_SET_P (nonequal, regno + n))
263               return 1;
264         }
265     }
266   return 0;
267 }
268 /* Attempt to prove that the basic block B will have no side effects and
269    always continues in the same edge if reached via E.  Return the edge
270    if exist, NULL otherwise.  */
271
272 static edge
273 thread_jump (int mode, edge e, basic_block b)
274 {
275   rtx set1, set2, cond1, cond2, insn;
276   enum rtx_code code1, code2, reversed_code2;
277   bool reverse1 = false;
278   int i;
279   regset nonequal;
280   bool failed = false;
281
282   if (BB_FLAGS (b) & BB_NONTHREADABLE_BLOCK)
283     return NULL;
284
285   /* At the moment, we do handle only conditional jumps, but later we may
286      want to extend this code to tablejumps and others.  */
287   if (!e->src->succ->succ_next || e->src->succ->succ_next->succ_next)
288     return NULL;
289   if (!b->succ || !b->succ->succ_next || b->succ->succ_next->succ_next)
290     {
291       BB_SET_FLAG (b, BB_NONTHREADABLE_BLOCK);
292       return NULL;
293     }
294
295   /* Second branch must end with onlyjump, as we will eliminate the jump.  */
296   if (!any_condjump_p (BB_END (e->src)))
297     return NULL;
298
299   if (!any_condjump_p (BB_END (b)) || !onlyjump_p (BB_END (b)))
300     {
301       BB_SET_FLAG (b, BB_NONTHREADABLE_BLOCK);
302       return NULL;
303     }
304
305   set1 = pc_set (BB_END (e->src));
306   set2 = pc_set (BB_END (b));
307   if (((e->flags & EDGE_FALLTHRU) != 0)
308       != (XEXP (SET_SRC (set1), 1) == pc_rtx))
309     reverse1 = true;
310
311   cond1 = XEXP (SET_SRC (set1), 0);
312   cond2 = XEXP (SET_SRC (set2), 0);
313   if (reverse1)
314     code1 = reversed_comparison_code (cond1, BB_END (e->src));
315   else
316     code1 = GET_CODE (cond1);
317
318   code2 = GET_CODE (cond2);
319   reversed_code2 = reversed_comparison_code (cond2, BB_END (b));
320
321   if (!comparison_dominates_p (code1, code2)
322       && !comparison_dominates_p (code1, reversed_code2))
323     return NULL;
324
325   /* Ensure that the comparison operators are equivalent.
326      ??? This is far too pessimistic.  We should allow swapped operands,
327      different CCmodes, or for example comparisons for interval, that
328      dominate even when operands are not equivalent.  */
329   if (!rtx_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
330       || !rtx_equal_p (XEXP (cond1, 1), XEXP (cond2, 1)))
331     return NULL;
332
333   /* Short circuit cases where block B contains some side effects, as we can't
334      safely bypass it.  */
335   for (insn = NEXT_INSN (BB_HEAD (b)); insn != NEXT_INSN (BB_END (b));
336        insn = NEXT_INSN (insn))
337     if (INSN_P (insn) && side_effects_p (PATTERN (insn)))
338       {
339         BB_SET_FLAG (b, BB_NONTHREADABLE_BLOCK);
340         return NULL;
341       }
342
343   cselib_init (false);
344
345   /* First process all values computed in the source basic block.  */
346   for (insn = NEXT_INSN (BB_HEAD (e->src)); insn != NEXT_INSN (BB_END (e->src));
347        insn = NEXT_INSN (insn))
348     if (INSN_P (insn))
349       cselib_process_insn (insn);
350
351   nonequal = BITMAP_XMALLOC();
352   CLEAR_REG_SET (nonequal);
353
354   /* Now assume that we've continued by the edge E to B and continue
355      processing as if it were same basic block.
356      Our goal is to prove that whole block is an NOOP.  */
357
358   for (insn = NEXT_INSN (BB_HEAD (b)); insn != NEXT_INSN (BB_END (b)) && !failed;
359        insn = NEXT_INSN (insn))
360     {
361       if (INSN_P (insn))
362         {
363           rtx pat = PATTERN (insn);
364
365           if (GET_CODE (pat) == PARALLEL)
366             {
367               for (i = 0; i < XVECLEN (pat, 0); i++)
368                 failed |= mark_effect (XVECEXP (pat, 0, i), nonequal);
369             }
370           else
371             failed |= mark_effect (pat, nonequal);
372         }
373
374       cselib_process_insn (insn);
375     }
376
377   /* Later we should clear nonequal of dead registers.  So far we don't
378      have life information in cfg_cleanup.  */
379   if (failed)
380     {
381       BB_SET_FLAG (b, BB_NONTHREADABLE_BLOCK);
382       goto failed_exit;
383     }
384
385   /* cond2 must not mention any register that is not equal to the
386      former block.  */
387   if (for_each_rtx (&cond2, mentions_nonequal_regs, nonequal))
388     goto failed_exit;
389
390   /* In case liveness information is available, we need to prove equivalence
391      only of the live values.  */
392   if (mode & CLEANUP_UPDATE_LIFE)
393     AND_REG_SET (nonequal, b->global_live_at_end);
394
395   EXECUTE_IF_SET_IN_REG_SET (nonequal, 0, i, goto failed_exit;);
396
397   BITMAP_XFREE (nonequal);
398   cselib_finish ();
399   if ((comparison_dominates_p (code1, code2) != 0)
400       != (XEXP (SET_SRC (set2), 1) == pc_rtx))
401     return BRANCH_EDGE (b);
402   else
403     return FALLTHRU_EDGE (b);
404
405 failed_exit:
406   BITMAP_XFREE (nonequal);
407   cselib_finish ();
408   return NULL;
409 }
410 \f
411 /* Attempt to forward edges leaving basic block B.
412    Return true if successful.  */
413
414 static bool
415 try_forward_edges (int mode, basic_block b)
416 {
417   bool changed = false;
418   edge e, next, *threaded_edges = NULL;
419
420   /* If we are partitioning hot/cold basic blocks, we don't want to
421      mess up unconditional or indirect jumps that cross between hot
422      and cold sections.  */
423   
424   if (flag_reorder_blocks_and_partition
425       && find_reg_note (BB_END (b), REG_CROSSING_JUMP, NULL_RTX))
426     return false;
427
428   for (e = b->succ; e; e = next)
429     {
430       basic_block target, first;
431       int counter;
432       bool threaded = false;
433       int nthreaded_edges = 0;
434       bool may_thread = first_pass | (b->flags & BB_DIRTY);
435
436       next = e->succ_next;
437
438       /* Skip complex edges because we don't know how to update them.
439
440          Still handle fallthru edges, as we can succeed to forward fallthru
441          edge to the same place as the branch edge of conditional branch
442          and turn conditional branch to an unconditional branch.  */
443       if (e->flags & EDGE_COMPLEX)
444         continue;
445
446       target = first = e->dest;
447       counter = 0;
448
449       /* If we are partitioning hot/cold basic_blocks, we don't want to mess
450          up jumps that cross between hot/cold sections.  */
451
452       if (flag_reorder_blocks_and_partition
453           && first != EXIT_BLOCK_PTR
454           && find_reg_note (BB_END (first), REG_CROSSING_JUMP, NULL_RTX))
455         return false;
456
457       while (counter < n_basic_blocks)
458         {
459           basic_block new_target = NULL;
460           bool new_target_threaded = false;
461           may_thread |= target->flags & BB_DIRTY;
462
463           if (FORWARDER_BLOCK_P (target)
464               && !(target->succ->flags & EDGE_CROSSING)
465               && target->succ->dest != EXIT_BLOCK_PTR)
466             {
467               /* Bypass trivial infinite loops.  */
468               if (target == target->succ->dest)
469                 counter = n_basic_blocks;
470               new_target = target->succ->dest;
471             }
472
473           /* Allow to thread only over one edge at time to simplify updating
474              of probabilities.  */
475           else if ((mode & CLEANUP_THREADING) && may_thread)
476             {
477               edge t = thread_jump (mode, e, target);
478               if (t)
479                 {
480                   if (!threaded_edges)
481                     threaded_edges = xmalloc (sizeof (*threaded_edges)
482                                               * n_basic_blocks);
483                   else
484                     {
485                       int i;
486
487                       /* Detect an infinite loop across blocks not
488                          including the start block.  */
489                       for (i = 0; i < nthreaded_edges; ++i)
490                         if (threaded_edges[i] == t)
491                           break;
492                       if (i < nthreaded_edges)
493                         {
494                           counter = n_basic_blocks;
495                           break;
496                         }
497                     }
498
499                   /* Detect an infinite loop across the start block.  */
500                   if (t->dest == b)
501                     break;
502
503                   if (nthreaded_edges >= n_basic_blocks)
504                     abort ();
505                   threaded_edges[nthreaded_edges++] = t;
506
507                   new_target = t->dest;
508                   new_target_threaded = true;
509                 }
510             }
511
512           if (!new_target)
513             break;
514
515           /* Avoid killing of loop pre-headers, as it is the place loop
516              optimizer wants to hoist code to.
517
518              For fallthru forwarders, the LOOP_BEG note must appear between
519              the header of block and CODE_LABEL of the loop, for non forwarders
520              it must appear before the JUMP_INSN.  */
521           if ((mode & CLEANUP_PRE_LOOP) && optimize)
522             {
523               rtx insn = (target->succ->flags & EDGE_FALLTHRU
524                           ? BB_HEAD (target) : prev_nonnote_insn (BB_END (target)));
525
526               if (!NOTE_P (insn))
527                 insn = NEXT_INSN (insn);
528
529               for (; insn && !LABEL_P (insn) && !INSN_P (insn);
530                    insn = NEXT_INSN (insn))
531                 if (NOTE_P (insn)
532                     && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
533                   break;
534
535               if (NOTE_P (insn))
536                 break;
537
538               /* Do not clean up branches to just past the end of a loop
539                  at this time; it can mess up the loop optimizer's
540                  recognition of some patterns.  */
541
542               insn = PREV_INSN (BB_HEAD (target));
543               if (insn && NOTE_P (insn)
544                     && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
545                 break;
546             }
547
548           counter++;
549           target = new_target;
550           threaded |= new_target_threaded;
551         }
552
553       if (counter >= n_basic_blocks)
554         {
555           if (dump_file)
556             fprintf (dump_file, "Infinite loop in BB %i.\n",
557                      target->index);
558         }
559       else if (target == first)
560         ; /* We didn't do anything.  */
561       else
562         {
563           /* Save the values now, as the edge may get removed.  */
564           gcov_type edge_count = e->count;
565           int edge_probability = e->probability;
566           int edge_frequency;
567           int n = 0;
568
569           /* Don't force if target is exit block.  */
570           if (threaded && target != EXIT_BLOCK_PTR)
571             {
572               notice_new_block (redirect_edge_and_branch_force (e, target));
573               if (dump_file)
574                 fprintf (dump_file, "Conditionals threaded.\n");
575             }
576           else if (!redirect_edge_and_branch (e, target))
577             {
578               if (dump_file)
579                 fprintf (dump_file,
580                          "Forwarding edge %i->%i to %i failed.\n",
581                          b->index, e->dest->index, target->index);
582               continue;
583             }
584
585           /* We successfully forwarded the edge.  Now update profile
586              data: for each edge we traversed in the chain, remove
587              the original edge's execution count.  */
588           edge_frequency = ((edge_probability * b->frequency
589                              + REG_BR_PROB_BASE / 2)
590                             / REG_BR_PROB_BASE);
591
592           if (!FORWARDER_BLOCK_P (b) && forwarder_block_p (b))
593             BB_SET_FLAG (b, BB_FORWARDER_BLOCK);
594
595           do
596             {
597               edge t;
598
599               first->count -= edge_count;
600               if (first->count < 0)
601                 first->count = 0;
602               first->frequency -= edge_frequency;
603               if (first->frequency < 0)
604                 first->frequency = 0;
605               if (first->succ->succ_next)
606                 {
607                   edge e;
608                   int prob;
609                   if (n >= nthreaded_edges)
610                     abort ();
611                   t = threaded_edges [n++];
612                   if (t->src != first)
613                     abort ();
614                   if (first->frequency)
615                     prob = edge_frequency * REG_BR_PROB_BASE / first->frequency;
616                   else
617                     prob = 0;
618                   if (prob > t->probability)
619                     prob = t->probability;
620                   t->probability -= prob;
621                   prob = REG_BR_PROB_BASE - prob;
622                   if (prob <= 0)
623                     {
624                       first->succ->probability = REG_BR_PROB_BASE;
625                       first->succ->succ_next->probability = 0;
626                     }
627                   else
628                     for (e = first->succ; e; e = e->succ_next)
629                       e->probability = ((e->probability * REG_BR_PROB_BASE)
630                                         / (double) prob);
631                   update_br_prob_note (first);
632                 }
633               else
634                 {
635                   /* It is possible that as the result of
636                      threading we've removed edge as it is
637                      threaded to the fallthru edge.  Avoid
638                      getting out of sync.  */
639                   if (n < nthreaded_edges
640                       && first == threaded_edges [n]->src)
641                     n++;
642                   t = first->succ;
643                 }
644
645               t->count -= edge_count;
646               if (t->count < 0)
647                 t->count = 0;
648               first = t->dest;
649             }
650           while (first != target);
651
652           changed = true;
653         }
654     }
655
656   if (threaded_edges)
657     free (threaded_edges);
658   return changed;
659 }
660 \f
661
662 /* Blocks A and B are to be merged into a single block.  A has no incoming
663    fallthru edge, so it can be moved before B without adding or modifying
664    any jumps (aside from the jump from A to B).  */
665
666 static void
667 merge_blocks_move_predecessor_nojumps (basic_block a, basic_block b)
668 {
669   rtx barrier;
670
671   /* If we are partitioning hot/cold basic blocks, we don't want to
672      mess up unconditional or indirect jumps that cross between hot
673      and cold sections.  */
674   
675   if (flag_reorder_blocks_and_partition
676       && (BB_PARTITION (a) != BB_PARTITION (b)
677           || find_reg_note (BB_END (a), REG_CROSSING_JUMP, NULL_RTX)))
678     return;
679
680   barrier = next_nonnote_insn (BB_END (a));
681   if (!BARRIER_P (barrier))
682     abort ();
683   delete_insn (barrier);
684
685   /* Move block and loop notes out of the chain so that we do not
686      disturb their order.
687
688      ??? A better solution would be to squeeze out all the non-nested notes
689      and adjust the block trees appropriately.   Even better would be to have
690      a tighter connection between block trees and rtl so that this is not
691      necessary.  */
692   if (squeeze_notes (&BB_HEAD (a), &BB_END (a)))
693     abort ();
694
695   /* Scramble the insn chain.  */
696   if (BB_END (a) != PREV_INSN (BB_HEAD (b)))
697     reorder_insns_nobb (BB_HEAD (a), BB_END (a), PREV_INSN (BB_HEAD (b)));
698   a->flags |= BB_DIRTY;
699
700   if (dump_file)
701     fprintf (dump_file, "Moved block %d before %d and merged.\n",
702              a->index, b->index);
703
704   /* Swap the records for the two blocks around.  */
705
706   unlink_block (a);
707   link_block (a, b->prev_bb);
708
709   /* Now blocks A and B are contiguous.  Merge them.  */
710   merge_blocks (a, b);
711 }
712
713 /* Blocks A and B are to be merged into a single block.  B has no outgoing
714    fallthru edge, so it can be moved after A without adding or modifying
715    any jumps (aside from the jump from A to B).  */
716
717 static void
718 merge_blocks_move_successor_nojumps (basic_block a, basic_block b)
719 {
720   rtx barrier, real_b_end;
721   rtx label, table;
722
723   /* If we are partitioning hot/cold basic blocks, we don't want to
724      mess up unconditional or indirect jumps that cross between hot
725      and cold sections.  */
726   
727   if (flag_reorder_blocks_and_partition
728       && (find_reg_note (BB_END (a), REG_CROSSING_JUMP, NULL_RTX)
729           || BB_PARTITION (a) != BB_PARTITION (b)))
730     return;
731
732   real_b_end = BB_END (b);
733
734   /* If there is a jump table following block B temporarily add the jump table
735      to block B so that it will also be moved to the correct location.  */
736   if (tablejump_p (BB_END (b), &label, &table)
737       && prev_active_insn (label) == BB_END (b))
738     {
739       BB_END (b) = table;
740     }
741
742   /* There had better have been a barrier there.  Delete it.  */
743   barrier = NEXT_INSN (BB_END (b));
744   if (barrier && BARRIER_P (barrier))
745     delete_insn (barrier);
746
747   /* Move block and loop notes out of the chain so that we do not
748      disturb their order.
749
750      ??? A better solution would be to squeeze out all the non-nested notes
751      and adjust the block trees appropriately.   Even better would be to have
752      a tighter connection between block trees and rtl so that this is not
753      necessary.  */
754   if (squeeze_notes (&BB_HEAD (b), &BB_END (b)))
755     abort ();
756
757   /* Scramble the insn chain.  */
758   reorder_insns_nobb (BB_HEAD (b), BB_END (b), BB_END (a));
759
760   /* Restore the real end of b.  */
761   BB_END (b) = real_b_end;
762
763   if (dump_file)
764     fprintf (dump_file, "Moved block %d after %d and merged.\n",
765              b->index, a->index);
766
767   /* Now blocks A and B are contiguous.  Merge them.  */
768   merge_blocks (a, b);
769 }
770
771 /* Attempt to merge basic blocks that are potentially non-adjacent.
772    Return NULL iff the attempt failed, otherwise return basic block
773    where cleanup_cfg should continue.  Because the merging commonly
774    moves basic block away or introduces another optimization
775    possibility, return basic block just before B so cleanup_cfg don't
776    need to iterate.
777
778    It may be good idea to return basic block before C in the case
779    C has been moved after B and originally appeared earlier in the
780    insn sequence, but we have no information available about the
781    relative ordering of these two.  Hopefully it is not too common.  */
782
783 static basic_block
784 merge_blocks_move (edge e, basic_block b, basic_block c, int mode)
785 {
786   basic_block next;
787
788   /* If we are partitioning hot/cold basic blocks, we don't want to
789      mess up unconditional or indirect jumps that cross between hot
790      and cold sections.  */
791   
792   if (flag_reorder_blocks_and_partition
793       && (find_reg_note (BB_END (b), REG_CROSSING_JUMP, NULL_RTX)
794           || find_reg_note (BB_END (c), REG_CROSSING_JUMP, NULL_RTX)
795           || BB_PARTITION (b) != BB_PARTITION (c)))
796     return NULL;
797       
798     
799
800   /* If B has a fallthru edge to C, no need to move anything.  */
801   if (e->flags & EDGE_FALLTHRU)
802     {
803       int b_index = b->index, c_index = c->index;
804       merge_blocks (b, c);
805       update_forwarder_flag (b);
806
807       if (dump_file)
808         fprintf (dump_file, "Merged %d and %d without moving.\n",
809                  b_index, c_index);
810
811       return b->prev_bb == ENTRY_BLOCK_PTR ? b : b->prev_bb;
812     }
813
814   /* Otherwise we will need to move code around.  Do that only if expensive
815      transformations are allowed.  */
816   else if (mode & CLEANUP_EXPENSIVE)
817     {
818       edge tmp_edge, b_fallthru_edge;
819       bool c_has_outgoing_fallthru;
820       bool b_has_incoming_fallthru;
821
822       /* Avoid overactive code motion, as the forwarder blocks should be
823          eliminated by edge redirection instead.  One exception might have
824          been if B is a forwarder block and C has no fallthru edge, but
825          that should be cleaned up by bb-reorder instead.  */
826       if (FORWARDER_BLOCK_P (b) || FORWARDER_BLOCK_P (c))
827         return NULL;
828
829       /* We must make sure to not munge nesting of lexical blocks,
830          and loop notes.  This is done by squeezing out all the notes
831          and leaving them there to lie.  Not ideal, but functional.  */
832
833       for (tmp_edge = c->succ; tmp_edge; tmp_edge = tmp_edge->succ_next)
834         if (tmp_edge->flags & EDGE_FALLTHRU)
835           break;
836
837       c_has_outgoing_fallthru = (tmp_edge != NULL);
838
839       for (tmp_edge = b->pred; tmp_edge; tmp_edge = tmp_edge->pred_next)
840         if (tmp_edge->flags & EDGE_FALLTHRU)
841           break;
842
843       b_has_incoming_fallthru = (tmp_edge != NULL);
844       b_fallthru_edge = tmp_edge;
845       next = b->prev_bb;
846       if (next == c)
847         next = next->prev_bb;
848
849       /* Otherwise, we're going to try to move C after B.  If C does
850          not have an outgoing fallthru, then it can be moved
851          immediately after B without introducing or modifying jumps.  */
852       if (! c_has_outgoing_fallthru)
853         {
854           merge_blocks_move_successor_nojumps (b, c);
855           return next == ENTRY_BLOCK_PTR ? next->next_bb : next;
856         }
857
858       /* If B does not have an incoming fallthru, then it can be moved
859          immediately before C without introducing or modifying jumps.
860          C cannot be the first block, so we do not have to worry about
861          accessing a non-existent block.  */
862
863       if (b_has_incoming_fallthru)
864         {
865           basic_block bb;
866
867           if (b_fallthru_edge->src == ENTRY_BLOCK_PTR)
868             return NULL;
869           bb = force_nonfallthru (b_fallthru_edge);
870           if (bb)
871             notice_new_block (bb);
872         }
873
874       merge_blocks_move_predecessor_nojumps (b, c);
875       return next == ENTRY_BLOCK_PTR ? next->next_bb : next;
876     }
877
878   return NULL;
879 }
880 \f
881
882 /* Removes the memory attributes of MEM expression
883    if they are not equal.  */
884
885 void
886 merge_memattrs (rtx x, rtx y)
887 {
888   int i;
889   int j;
890   enum rtx_code code;
891   const char *fmt;
892
893   if (x == y)
894     return;
895   if (x == 0 || y == 0)
896     return;
897
898   code = GET_CODE (x);
899
900   if (code != GET_CODE (y))
901     return;
902
903   if (GET_MODE (x) != GET_MODE (y))
904     return;
905
906   if (code == MEM && MEM_ATTRS (x) != MEM_ATTRS (y))
907     {
908       if (! MEM_ATTRS (x))
909         MEM_ATTRS (y) = 0;
910       else if (! MEM_ATTRS (y))
911         MEM_ATTRS (x) = 0;
912       else 
913         {
914           if (MEM_ALIAS_SET (x) != MEM_ALIAS_SET (y))
915             {
916               set_mem_alias_set (x, 0);
917               set_mem_alias_set (y, 0);
918             }
919           
920           if (! mem_expr_equal_p (MEM_EXPR (x), MEM_EXPR (y)))
921             {
922               set_mem_expr (x, 0);
923               set_mem_expr (y, 0);
924               set_mem_offset (x, 0);
925               set_mem_offset (y, 0);
926             }
927           else if (MEM_OFFSET (x) != MEM_OFFSET (y))
928             {
929               set_mem_offset (x, 0);
930               set_mem_offset (y, 0);
931             }
932           
933           set_mem_size (x, MAX (MEM_SIZE (x), MEM_SIZE (y)));
934           set_mem_size (y, MEM_SIZE (x));
935
936           set_mem_align (x, MIN (MEM_ALIGN (x), MEM_ALIGN (y)));
937           set_mem_align (y, MEM_ALIGN (x));
938         }
939     }
940   
941   fmt = GET_RTX_FORMAT (code);
942   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
943     {
944       switch (fmt[i])
945         {
946         case 'E':
947           /* Two vectors must have the same length.  */
948           if (XVECLEN (x, i) != XVECLEN (y, i))
949             return;
950
951           for (j = 0; j < XVECLEN (x, i); j++)
952             merge_memattrs (XVECEXP (x, i, j), XVECEXP (y, i, j));
953
954           break;
955
956         case 'e':
957           merge_memattrs (XEXP (x, i), XEXP (y, i));
958         }
959     }
960   return;
961 }
962
963
964 /* Return true if I1 and I2 are equivalent and thus can be crossjumped.  */
965
966 static bool
967 insns_match_p (int mode ATTRIBUTE_UNUSED, rtx i1, rtx i2)
968 {
969   rtx p1, p2;
970
971   /* Verify that I1 and I2 are equivalent.  */
972   if (GET_CODE (i1) != GET_CODE (i2))
973     return false;
974
975   p1 = PATTERN (i1);
976   p2 = PATTERN (i2);
977
978   if (GET_CODE (p1) != GET_CODE (p2))
979     return false;
980
981   /* If this is a CALL_INSN, compare register usage information.
982      If we don't check this on stack register machines, the two
983      CALL_INSNs might be merged leaving reg-stack.c with mismatching
984      numbers of stack registers in the same basic block.
985      If we don't check this on machines with delay slots, a delay slot may
986      be filled that clobbers a parameter expected by the subroutine.
987
988      ??? We take the simple route for now and assume that if they're
989      equal, they were constructed identically.  */
990
991   if (CALL_P (i1)
992       && (!rtx_equal_p (CALL_INSN_FUNCTION_USAGE (i1),
993                         CALL_INSN_FUNCTION_USAGE (i2))
994           || SIBLING_CALL_P (i1) != SIBLING_CALL_P (i2)))
995     return false;
996
997 #ifdef STACK_REGS
998   /* If cross_jump_death_matters is not 0, the insn's mode
999      indicates whether or not the insn contains any stack-like
1000      regs.  */
1001
1002   if ((mode & CLEANUP_POST_REGSTACK) && stack_regs_mentioned (i1))
1003     {
1004       /* If register stack conversion has already been done, then
1005          death notes must also be compared before it is certain that
1006          the two instruction streams match.  */
1007
1008       rtx note;
1009       HARD_REG_SET i1_regset, i2_regset;
1010
1011       CLEAR_HARD_REG_SET (i1_regset);
1012       CLEAR_HARD_REG_SET (i2_regset);
1013
1014       for (note = REG_NOTES (i1); note; note = XEXP (note, 1))
1015         if (REG_NOTE_KIND (note) == REG_DEAD && STACK_REG_P (XEXP (note, 0)))
1016           SET_HARD_REG_BIT (i1_regset, REGNO (XEXP (note, 0)));
1017
1018       for (note = REG_NOTES (i2); note; note = XEXP (note, 1))
1019         if (REG_NOTE_KIND (note) == REG_DEAD && STACK_REG_P (XEXP (note, 0)))
1020           SET_HARD_REG_BIT (i2_regset, REGNO (XEXP (note, 0)));
1021
1022       GO_IF_HARD_REG_EQUAL (i1_regset, i2_regset, done);
1023
1024       return false;
1025
1026     done:
1027       ;
1028     }
1029 #endif
1030
1031   if (reload_completed
1032       ? rtx_renumbered_equal_p (p1, p2) : rtx_equal_p (p1, p2))
1033     return true;
1034
1035   /* Do not do EQUIV substitution after reload.  First, we're undoing the
1036      work of reload_cse.  Second, we may be undoing the work of the post-
1037      reload splitting pass.  */
1038   /* ??? Possibly add a new phase switch variable that can be used by
1039      targets to disallow the troublesome insns after splitting.  */
1040   if (!reload_completed)
1041     {
1042       /* The following code helps take care of G++ cleanups.  */
1043       rtx equiv1 = find_reg_equal_equiv_note (i1);
1044       rtx equiv2 = find_reg_equal_equiv_note (i2);
1045
1046       if (equiv1 && equiv2
1047           /* If the equivalences are not to a constant, they may
1048              reference pseudos that no longer exist, so we can't
1049              use them.  */
1050           && (! reload_completed
1051               || (CONSTANT_P (XEXP (equiv1, 0))
1052                   && rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))))
1053         {
1054           rtx s1 = single_set (i1);
1055           rtx s2 = single_set (i2);
1056           if (s1 != 0 && s2 != 0
1057               && rtx_renumbered_equal_p (SET_DEST (s1), SET_DEST (s2)))
1058             {
1059               validate_change (i1, &SET_SRC (s1), XEXP (equiv1, 0), 1);
1060               validate_change (i2, &SET_SRC (s2), XEXP (equiv2, 0), 1);
1061               if (! rtx_renumbered_equal_p (p1, p2))
1062                 cancel_changes (0);
1063               else if (apply_change_group ())
1064                 return true;
1065             }
1066         }
1067     }
1068
1069   return false;
1070 }
1071 \f
1072 /* Look through the insns at the end of BB1 and BB2 and find the longest
1073    sequence that are equivalent.  Store the first insns for that sequence
1074    in *F1 and *F2 and return the sequence length.
1075
1076    To simplify callers of this function, if the blocks match exactly,
1077    store the head of the blocks in *F1 and *F2.  */
1078
1079 static int
1080 flow_find_cross_jump (int mode ATTRIBUTE_UNUSED, basic_block bb1,
1081                       basic_block bb2, rtx *f1, rtx *f2)
1082 {
1083   rtx i1, i2, last1, last2, afterlast1, afterlast2;
1084   int ninsns = 0;
1085
1086   /* Skip simple jumps at the end of the blocks.  Complex jumps still
1087      need to be compared for equivalence, which we'll do below.  */
1088
1089   i1 = BB_END (bb1);
1090   last1 = afterlast1 = last2 = afterlast2 = NULL_RTX;
1091   if (onlyjump_p (i1)
1092       || (returnjump_p (i1) && !side_effects_p (PATTERN (i1))))
1093     {
1094       last1 = i1;
1095       i1 = PREV_INSN (i1);
1096     }
1097
1098   i2 = BB_END (bb2);
1099   if (onlyjump_p (i2)
1100       || (returnjump_p (i2) && !side_effects_p (PATTERN (i2))))
1101     {
1102       last2 = i2;
1103       /* Count everything except for unconditional jump as insn.  */
1104       if (!simplejump_p (i2) && !returnjump_p (i2) && last1)
1105         ninsns++;
1106       i2 = PREV_INSN (i2);
1107     }
1108
1109   while (true)
1110     {
1111       /* Ignore notes.  */
1112       while (!INSN_P (i1) && i1 != BB_HEAD (bb1))
1113         i1 = PREV_INSN (i1);
1114
1115       while (!INSN_P (i2) && i2 != BB_HEAD (bb2))
1116         i2 = PREV_INSN (i2);
1117
1118       if (i1 == BB_HEAD (bb1) || i2 == BB_HEAD (bb2))
1119         break;
1120
1121       if (!insns_match_p (mode, i1, i2))
1122         break;
1123
1124       merge_memattrs (i1, i2);
1125
1126       /* Don't begin a cross-jump with a NOTE insn.  */
1127       if (INSN_P (i1))
1128         {
1129           /* If the merged insns have different REG_EQUAL notes, then
1130              remove them.  */
1131           rtx equiv1 = find_reg_equal_equiv_note (i1);
1132           rtx equiv2 = find_reg_equal_equiv_note (i2);
1133
1134           if (equiv1 && !equiv2)
1135             remove_note (i1, equiv1);
1136           else if (!equiv1 && equiv2)
1137             remove_note (i2, equiv2);
1138           else if (equiv1 && equiv2
1139                    && !rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))
1140             {
1141               remove_note (i1, equiv1);
1142               remove_note (i2, equiv2);
1143             }
1144
1145           afterlast1 = last1, afterlast2 = last2;
1146           last1 = i1, last2 = i2;
1147           ninsns++;
1148         }
1149
1150       i1 = PREV_INSN (i1);
1151       i2 = PREV_INSN (i2);
1152     }
1153
1154 #ifdef HAVE_cc0
1155   /* Don't allow the insn after a compare to be shared by
1156      cross-jumping unless the compare is also shared.  */
1157   if (ninsns && reg_mentioned_p (cc0_rtx, last1) && ! sets_cc0_p (last1))
1158     last1 = afterlast1, last2 = afterlast2, ninsns--;
1159 #endif
1160
1161   /* Include preceding notes and labels in the cross-jump.  One,
1162      this may bring us to the head of the blocks as requested above.
1163      Two, it keeps line number notes as matched as may be.  */
1164   if (ninsns)
1165     {
1166       while (last1 != BB_HEAD (bb1) && !INSN_P (PREV_INSN (last1)))
1167         last1 = PREV_INSN (last1);
1168
1169       if (last1 != BB_HEAD (bb1) && LABEL_P (PREV_INSN (last1)))
1170         last1 = PREV_INSN (last1);
1171
1172       while (last2 != BB_HEAD (bb2) && !INSN_P (PREV_INSN (last2)))
1173         last2 = PREV_INSN (last2);
1174
1175       if (last2 != BB_HEAD (bb2) && LABEL_P (PREV_INSN (last2)))
1176         last2 = PREV_INSN (last2);
1177
1178       *f1 = last1;
1179       *f2 = last2;
1180     }
1181
1182   return ninsns;
1183 }
1184
1185 /* Return true iff outgoing edges of BB1 and BB2 match, together with
1186    the branch instruction.  This means that if we commonize the control
1187    flow before end of the basic block, the semantic remains unchanged.
1188
1189    We may assume that there exists one edge with a common destination.  */
1190
1191 static bool
1192 outgoing_edges_match (int mode, basic_block bb1, basic_block bb2)
1193 {
1194   int nehedges1 = 0, nehedges2 = 0;
1195   edge fallthru1 = 0, fallthru2 = 0;
1196   edge e1, e2;
1197
1198   /* If BB1 has only one successor, we may be looking at either an
1199      unconditional jump, or a fake edge to exit.  */
1200   if (bb1->succ && !bb1->succ->succ_next
1201       && (bb1->succ->flags & (EDGE_COMPLEX | EDGE_FAKE)) == 0
1202       && (!JUMP_P (BB_END (bb1)) || simplejump_p (BB_END (bb1))))
1203     return (bb2->succ &&  !bb2->succ->succ_next
1204             && (bb2->succ->flags & (EDGE_COMPLEX | EDGE_FAKE)) == 0
1205             && (!JUMP_P (BB_END (bb2)) || simplejump_p (BB_END (bb2))));
1206
1207   /* Match conditional jumps - this may get tricky when fallthru and branch
1208      edges are crossed.  */
1209   if (bb1->succ
1210       && bb1->succ->succ_next
1211       && !bb1->succ->succ_next->succ_next
1212       && any_condjump_p (BB_END (bb1))
1213       && onlyjump_p (BB_END (bb1)))
1214     {
1215       edge b1, f1, b2, f2;
1216       bool reverse, match;
1217       rtx set1, set2, cond1, cond2;
1218       enum rtx_code code1, code2;
1219
1220       if (!bb2->succ
1221           || !bb2->succ->succ_next
1222           || bb2->succ->succ_next->succ_next
1223           || !any_condjump_p (BB_END (bb2))
1224           || !onlyjump_p (BB_END (bb2)))
1225         return false;
1226
1227       b1 = BRANCH_EDGE (bb1);
1228       b2 = BRANCH_EDGE (bb2);
1229       f1 = FALLTHRU_EDGE (bb1);
1230       f2 = FALLTHRU_EDGE (bb2);
1231
1232       /* Get around possible forwarders on fallthru edges.  Other cases
1233          should be optimized out already.  */
1234       if (FORWARDER_BLOCK_P (f1->dest))
1235         f1 = f1->dest->succ;
1236
1237       if (FORWARDER_BLOCK_P (f2->dest))
1238         f2 = f2->dest->succ;
1239
1240       /* To simplify use of this function, return false if there are
1241          unneeded forwarder blocks.  These will get eliminated later
1242          during cleanup_cfg.  */
1243       if (FORWARDER_BLOCK_P (f1->dest)
1244           || FORWARDER_BLOCK_P (f2->dest)
1245           || FORWARDER_BLOCK_P (b1->dest)
1246           || FORWARDER_BLOCK_P (b2->dest))
1247         return false;
1248
1249       if (f1->dest == f2->dest && b1->dest == b2->dest)
1250         reverse = false;
1251       else if (f1->dest == b2->dest && b1->dest == f2->dest)
1252         reverse = true;
1253       else
1254         return false;
1255
1256       set1 = pc_set (BB_END (bb1));
1257       set2 = pc_set (BB_END (bb2));
1258       if ((XEXP (SET_SRC (set1), 1) == pc_rtx)
1259           != (XEXP (SET_SRC (set2), 1) == pc_rtx))
1260         reverse = !reverse;
1261
1262       cond1 = XEXP (SET_SRC (set1), 0);
1263       cond2 = XEXP (SET_SRC (set2), 0);
1264       code1 = GET_CODE (cond1);
1265       if (reverse)
1266         code2 = reversed_comparison_code (cond2, BB_END (bb2));
1267       else
1268         code2 = GET_CODE (cond2);
1269
1270       if (code2 == UNKNOWN)
1271         return false;
1272
1273       /* Verify codes and operands match.  */
1274       match = ((code1 == code2
1275                 && rtx_renumbered_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
1276                 && rtx_renumbered_equal_p (XEXP (cond1, 1), XEXP (cond2, 1)))
1277                || (code1 == swap_condition (code2)
1278                    && rtx_renumbered_equal_p (XEXP (cond1, 1),
1279                                               XEXP (cond2, 0))
1280                    && rtx_renumbered_equal_p (XEXP (cond1, 0),
1281                                               XEXP (cond2, 1))));
1282
1283       /* If we return true, we will join the blocks.  Which means that
1284          we will only have one branch prediction bit to work with.  Thus
1285          we require the existing branches to have probabilities that are
1286          roughly similar.  */
1287       if (match
1288           && !optimize_size
1289           && maybe_hot_bb_p (bb1)
1290           && maybe_hot_bb_p (bb2))
1291         {
1292           int prob2;
1293
1294           if (b1->dest == b2->dest)
1295             prob2 = b2->probability;
1296           else
1297             /* Do not use f2 probability as f2 may be forwarded.  */
1298             prob2 = REG_BR_PROB_BASE - b2->probability;
1299
1300           /* Fail if the difference in probabilities is greater than 50%.
1301              This rules out two well-predicted branches with opposite
1302              outcomes.  */
1303           if (abs (b1->probability - prob2) > REG_BR_PROB_BASE / 2)
1304             {
1305               if (dump_file)
1306                 fprintf (dump_file,
1307                          "Outcomes of branch in bb %i and %i differs to much (%i %i)\n",
1308                          bb1->index, bb2->index, b1->probability, prob2);
1309
1310               return false;
1311             }
1312         }
1313
1314       if (dump_file && match)
1315         fprintf (dump_file, "Conditionals in bb %i and %i match.\n",
1316                  bb1->index, bb2->index);
1317
1318       return match;
1319     }
1320
1321   /* Generic case - we are seeing a computed jump, table jump or trapping
1322      instruction.  */
1323
1324 #ifndef CASE_DROPS_THROUGH
1325   /* Check whether there are tablejumps in the end of BB1 and BB2.
1326      Return true if they are identical.  */
1327     {
1328       rtx label1, label2;
1329       rtx table1, table2;
1330
1331       if (tablejump_p (BB_END (bb1), &label1, &table1)
1332           && tablejump_p (BB_END (bb2), &label2, &table2)
1333           && GET_CODE (PATTERN (table1)) == GET_CODE (PATTERN (table2)))
1334         {
1335           /* The labels should never be the same rtx.  If they really are same
1336              the jump tables are same too. So disable crossjumping of blocks BB1
1337              and BB2 because when deleting the common insns in the end of BB1
1338              by delete_basic_block () the jump table would be deleted too.  */
1339           /* If LABEL2 is referenced in BB1->END do not do anything
1340              because we would loose information when replacing
1341              LABEL1 by LABEL2 and then LABEL2 by LABEL1 in BB1->END.  */
1342           if (label1 != label2 && !rtx_referenced_p (label2, BB_END (bb1)))
1343             {
1344               /* Set IDENTICAL to true when the tables are identical.  */
1345               bool identical = false;
1346               rtx p1, p2;
1347
1348               p1 = PATTERN (table1);
1349               p2 = PATTERN (table2);
1350               if (GET_CODE (p1) == ADDR_VEC && rtx_equal_p (p1, p2))
1351                 {
1352                   identical = true;
1353                 }
1354               else if (GET_CODE (p1) == ADDR_DIFF_VEC
1355                        && (XVECLEN (p1, 1) == XVECLEN (p2, 1))
1356                        && rtx_equal_p (XEXP (p1, 2), XEXP (p2, 2))
1357                        && rtx_equal_p (XEXP (p1, 3), XEXP (p2, 3)))
1358                 {
1359                   int i;
1360
1361                   identical = true;
1362                   for (i = XVECLEN (p1, 1) - 1; i >= 0 && identical; i--)
1363                     if (!rtx_equal_p (XVECEXP (p1, 1, i), XVECEXP (p2, 1, i)))
1364                       identical = false;
1365                 }
1366
1367               if (identical)
1368                 {
1369                   replace_label_data rr;
1370                   bool match;
1371
1372                   /* Temporarily replace references to LABEL1 with LABEL2
1373                      in BB1->END so that we could compare the instructions.  */
1374                   rr.r1 = label1;
1375                   rr.r2 = label2;
1376                   rr.update_label_nuses = false;
1377                   for_each_rtx (&BB_END (bb1), replace_label, &rr);
1378
1379                   match = insns_match_p (mode, BB_END (bb1), BB_END (bb2));
1380                   if (dump_file && match)
1381                     fprintf (dump_file,
1382                              "Tablejumps in bb %i and %i match.\n",
1383                              bb1->index, bb2->index);
1384
1385                   /* Set the original label in BB1->END because when deleting
1386                      a block whose end is a tablejump, the tablejump referenced
1387                      from the instruction is deleted too.  */
1388                   rr.r1 = label2;
1389                   rr.r2 = label1;
1390                   for_each_rtx (&BB_END (bb1), replace_label, &rr);
1391
1392                   return match;
1393                 }
1394             }
1395           return false;
1396         }
1397     }
1398 #endif
1399
1400   /* First ensure that the instructions match.  There may be many outgoing
1401      edges so this test is generally cheaper.  */
1402   if (!insns_match_p (mode, BB_END (bb1), BB_END (bb2)))
1403     return false;
1404
1405   /* Search the outgoing edges, ensure that the counts do match, find possible
1406      fallthru and exception handling edges since these needs more
1407      validation.  */
1408   for (e1 = bb1->succ, e2 = bb2->succ; e1 && e2;
1409        e1 = e1->succ_next, e2 = e2->succ_next)
1410     {
1411       if (e1->flags & EDGE_EH)
1412         nehedges1++;
1413
1414       if (e2->flags & EDGE_EH)
1415         nehedges2++;
1416
1417       if (e1->flags & EDGE_FALLTHRU)
1418         fallthru1 = e1;
1419       if (e2->flags & EDGE_FALLTHRU)
1420         fallthru2 = e2;
1421     }
1422
1423   /* If number of edges of various types does not match, fail.  */
1424   if (e1 || e2
1425       || nehedges1 != nehedges2
1426       || (fallthru1 != 0) != (fallthru2 != 0))
1427     return false;
1428
1429   /* fallthru edges must be forwarded to the same destination.  */
1430   if (fallthru1)
1431     {
1432       basic_block d1 = (forwarder_block_p (fallthru1->dest)
1433                         ? fallthru1->dest->succ->dest: fallthru1->dest);
1434       basic_block d2 = (forwarder_block_p (fallthru2->dest)
1435                         ? fallthru2->dest->succ->dest: fallthru2->dest);
1436
1437       if (d1 != d2)
1438         return false;
1439     }
1440
1441   /* Ensure the same EH region.  */
1442   {
1443     rtx n1 = find_reg_note (BB_END (bb1), REG_EH_REGION, 0);
1444     rtx n2 = find_reg_note (BB_END (bb2), REG_EH_REGION, 0);
1445
1446     if (!n1 && n2)
1447       return false;
1448
1449     if (n1 && (!n2 || XEXP (n1, 0) != XEXP (n2, 0)))
1450       return false;
1451   }
1452
1453   /* We don't need to match the rest of edges as above checks should be enough
1454      to ensure that they are equivalent.  */
1455   return true;
1456 }
1457
1458 /* E1 and E2 are edges with the same destination block.  Search their
1459    predecessors for common code.  If found, redirect control flow from
1460    (maybe the middle of) E1->SRC to (maybe the middle of) E2->SRC.  */
1461
1462 static bool
1463 try_crossjump_to_edge (int mode, edge e1, edge e2)
1464 {
1465   int nmatch;
1466   basic_block src1 = e1->src, src2 = e2->src;
1467   basic_block redirect_to, redirect_from, to_remove;
1468   rtx newpos1, newpos2;
1469   edge s;
1470
1471   newpos1 = newpos2 = NULL_RTX;
1472
1473   /* If we have partitioned hot/cold basic blocks, it is a bad idea
1474      to try this optimization.  */
1475
1476   if (flag_reorder_blocks_and_partition && no_new_pseudos)
1477     return false;
1478
1479   /* Search backward through forwarder blocks.  We don't need to worry
1480      about multiple entry or chained forwarders, as they will be optimized
1481      away.  We do this to look past the unconditional jump following a
1482      conditional jump that is required due to the current CFG shape.  */
1483   if (src1->pred
1484       && !src1->pred->pred_next
1485       && FORWARDER_BLOCK_P (src1))
1486     e1 = src1->pred, src1 = e1->src;
1487
1488   if (src2->pred
1489       && !src2->pred->pred_next
1490       && FORWARDER_BLOCK_P (src2))
1491     e2 = src2->pred, src2 = e2->src;
1492
1493   /* Nothing to do if we reach ENTRY, or a common source block.  */
1494   if (src1 == ENTRY_BLOCK_PTR || src2 == ENTRY_BLOCK_PTR)
1495     return false;
1496   if (src1 == src2)
1497     return false;
1498
1499   /* Seeing more than 1 forwarder blocks would confuse us later...  */
1500   if (FORWARDER_BLOCK_P (e1->dest)
1501       && FORWARDER_BLOCK_P (e1->dest->succ->dest))
1502     return false;
1503
1504   if (FORWARDER_BLOCK_P (e2->dest)
1505       && FORWARDER_BLOCK_P (e2->dest->succ->dest))
1506     return false;
1507
1508   /* Likewise with dead code (possibly newly created by the other optimizations
1509      of cfg_cleanup).  */
1510   if (!src1->pred || !src2->pred)
1511     return false;
1512
1513   /* Look for the common insn sequence, part the first ...  */
1514   if (!outgoing_edges_match (mode, src1, src2))
1515     return false;
1516
1517   /* ... and part the second.  */
1518   nmatch = flow_find_cross_jump (mode, src1, src2, &newpos1, &newpos2);
1519
1520   /* Don't proceed with the crossjump unless we found a sufficient number
1521      of matching instructions or the 'from' block was totally matched
1522      (such that its predecessors will hopefully be redirected and the
1523      block removed).  */
1524   if ((nmatch < PARAM_VALUE (PARAM_MIN_CROSSJUMP_INSNS))
1525       && (newpos1 != BB_HEAD (src1)))
1526     return false;
1527
1528 #ifndef CASE_DROPS_THROUGH
1529   /* Here we know that the insns in the end of SRC1 which are common with SRC2
1530      will be deleted.
1531      If we have tablejumps in the end of SRC1 and SRC2
1532      they have been already compared for equivalence in outgoing_edges_match ()
1533      so replace the references to TABLE1 by references to TABLE2.  */
1534     {
1535       rtx label1, label2;
1536       rtx table1, table2;
1537
1538       if (tablejump_p (BB_END (src1), &label1, &table1)
1539           && tablejump_p (BB_END (src2), &label2, &table2)
1540           && label1 != label2)
1541         {
1542           replace_label_data rr;
1543           rtx insn;
1544
1545           /* Replace references to LABEL1 with LABEL2.  */
1546           rr.r1 = label1;
1547           rr.r2 = label2;
1548           rr.update_label_nuses = true;
1549           for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1550             {
1551               /* Do not replace the label in SRC1->END because when deleting
1552                  a block whose end is a tablejump, the tablejump referenced
1553                  from the instruction is deleted too.  */
1554               if (insn != BB_END (src1))
1555                 for_each_rtx (&insn, replace_label, &rr);
1556             }
1557         }
1558     }
1559 #endif
1560
1561   /* Avoid splitting if possible.  */
1562   if (newpos2 == BB_HEAD (src2))
1563     redirect_to = src2;
1564   else
1565     {
1566       if (dump_file)
1567         fprintf (dump_file, "Splitting bb %i before %i insns\n",
1568                  src2->index, nmatch);
1569       redirect_to = split_block (src2, PREV_INSN (newpos2))->dest;
1570     }
1571
1572   if (dump_file)
1573     fprintf (dump_file,
1574              "Cross jumping from bb %i to bb %i; %i common insns\n",
1575              src1->index, src2->index, nmatch);
1576
1577   redirect_to->count += src1->count;
1578   redirect_to->frequency += src1->frequency;
1579   /* We may have some registers visible trought the block.  */
1580   redirect_to->flags |= BB_DIRTY;
1581
1582   /* Recompute the frequencies and counts of outgoing edges.  */
1583   for (s = redirect_to->succ; s; s = s->succ_next)
1584     {
1585       edge s2;
1586       basic_block d = s->dest;
1587
1588       if (FORWARDER_BLOCK_P (d))
1589         d = d->succ->dest;
1590
1591       for (s2 = src1->succ; ; s2 = s2->succ_next)
1592         {
1593           basic_block d2 = s2->dest;
1594           if (FORWARDER_BLOCK_P (d2))
1595             d2 = d2->succ->dest;
1596           if (d == d2)
1597             break;
1598         }
1599
1600       s->count += s2->count;
1601
1602       /* Take care to update possible forwarder blocks.  We verified
1603          that there is no more than one in the chain, so we can't run
1604          into infinite loop.  */
1605       if (FORWARDER_BLOCK_P (s->dest))
1606         {
1607           s->dest->succ->count += s2->count;
1608           s->dest->count += s2->count;
1609           s->dest->frequency += EDGE_FREQUENCY (s);
1610         }
1611
1612       if (FORWARDER_BLOCK_P (s2->dest))
1613         {
1614           s2->dest->succ->count -= s2->count;
1615           if (s2->dest->succ->count < 0)
1616             s2->dest->succ->count = 0;
1617           s2->dest->count -= s2->count;
1618           s2->dest->frequency -= EDGE_FREQUENCY (s);
1619           if (s2->dest->frequency < 0)
1620             s2->dest->frequency = 0;
1621           if (s2->dest->count < 0)
1622             s2->dest->count = 0;
1623         }
1624
1625       if (!redirect_to->frequency && !src1->frequency)
1626         s->probability = (s->probability + s2->probability) / 2;
1627       else
1628         s->probability
1629           = ((s->probability * redirect_to->frequency +
1630               s2->probability * src1->frequency)
1631              / (redirect_to->frequency + src1->frequency));
1632     }
1633
1634   update_br_prob_note (redirect_to);
1635
1636   /* Edit SRC1 to go to REDIRECT_TO at NEWPOS1.  */
1637
1638   /* Skip possible basic block header.  */
1639   if (LABEL_P (newpos1))
1640     newpos1 = NEXT_INSN (newpos1);
1641
1642   if (NOTE_P (newpos1))
1643     newpos1 = NEXT_INSN (newpos1);
1644
1645   redirect_from = split_block (src1, PREV_INSN (newpos1))->src;
1646   to_remove = redirect_from->succ->dest;
1647
1648   redirect_edge_and_branch_force (redirect_from->succ, redirect_to);
1649   delete_basic_block (to_remove);
1650
1651   update_forwarder_flag (redirect_from);
1652
1653   return true;
1654 }
1655
1656 /* Search the predecessors of BB for common insn sequences.  When found,
1657    share code between them by redirecting control flow.  Return true if
1658    any changes made.  */
1659
1660 static bool
1661 try_crossjump_bb (int mode, basic_block bb)
1662 {
1663   edge e, e2, nexte2, nexte, fallthru;
1664   bool changed;
1665   int n = 0, max;
1666
1667   /* Nothing to do if there is not at least two incoming edges.  */
1668   if (!bb->pred || !bb->pred->pred_next)
1669     return false;
1670
1671   /* If we are partitioning hot/cold basic blocks, we don't want to
1672      mess up unconditional or indirect jumps that cross between hot
1673      and cold sections.  */
1674   
1675   if (flag_reorder_blocks_and_partition
1676       && (BB_PARTITION (bb->pred->src) != BB_PARTITION (bb->pred->pred_next->src)
1677           || (bb->pred->flags & EDGE_CROSSING)))
1678     return false;
1679
1680   /* It is always cheapest to redirect a block that ends in a branch to
1681      a block that falls through into BB, as that adds no branches to the
1682      program.  We'll try that combination first.  */
1683   fallthru = NULL;
1684   max = PARAM_VALUE (PARAM_MAX_CROSSJUMP_EDGES);
1685   for (e = bb->pred; e ; e = e->pred_next, n++)
1686     {
1687       if (e->flags & EDGE_FALLTHRU)
1688         fallthru = e;
1689       if (n > max)
1690         return false;
1691     }
1692
1693   changed = false;
1694   for (e = bb->pred; e; e = nexte)
1695     {
1696       nexte = e->pred_next;
1697
1698       /* As noted above, first try with the fallthru predecessor.  */
1699       if (fallthru)
1700         {
1701           /* Don't combine the fallthru edge into anything else.
1702              If there is a match, we'll do it the other way around.  */
1703           if (e == fallthru)
1704             continue;
1705           /* If nothing changed since the last attempt, there is nothing
1706              we can do.  */
1707           if (!first_pass
1708               && (!(e->src->flags & BB_DIRTY)
1709                   && !(fallthru->src->flags & BB_DIRTY)))
1710             continue;
1711
1712           if (try_crossjump_to_edge (mode, e, fallthru))
1713             {
1714               changed = true;
1715               nexte = bb->pred;
1716               continue;
1717             }
1718         }
1719
1720       /* Non-obvious work limiting check: Recognize that we're going
1721          to call try_crossjump_bb on every basic block.  So if we have
1722          two blocks with lots of outgoing edges (a switch) and they
1723          share lots of common destinations, then we would do the
1724          cross-jump check once for each common destination.
1725
1726          Now, if the blocks actually are cross-jump candidates, then
1727          all of their destinations will be shared.  Which means that
1728          we only need check them for cross-jump candidacy once.  We
1729          can eliminate redundant checks of crossjump(A,B) by arbitrarily
1730          choosing to do the check from the block for which the edge
1731          in question is the first successor of A.  */
1732       if (e->src->succ != e)
1733         continue;
1734
1735       for (e2 = bb->pred; e2; e2 = nexte2)
1736         {
1737           nexte2 = e2->pred_next;
1738
1739           if (e2 == e)
1740             continue;
1741
1742           /* We've already checked the fallthru edge above.  */
1743           if (e2 == fallthru)
1744             continue;
1745
1746           /* The "first successor" check above only prevents multiple
1747              checks of crossjump(A,B).  In order to prevent redundant
1748              checks of crossjump(B,A), require that A be the block
1749              with the lowest index.  */
1750           if (e->src->index > e2->src->index)
1751             continue;
1752
1753           /* If nothing changed since the last attempt, there is nothing
1754              we can do.  */
1755           if (!first_pass
1756               && (!(e->src->flags & BB_DIRTY)
1757                   && !(e2->src->flags & BB_DIRTY)))
1758             continue;
1759
1760           if (try_crossjump_to_edge (mode, e, e2))
1761             {
1762               changed = true;
1763               nexte = bb->pred;
1764               break;
1765             }
1766         }
1767     }
1768
1769   return changed;
1770 }
1771
1772 /* Do simple CFG optimizations - basic block merging, simplifying of jump
1773    instructions etc.  Return nonzero if changes were made.  */
1774
1775 static bool
1776 try_optimize_cfg (int mode)
1777 {
1778   bool changed_overall = false;
1779   bool changed;
1780   int iterations = 0;
1781   basic_block bb, b, next;
1782
1783   if (mode & CLEANUP_CROSSJUMP)
1784     add_noreturn_fake_exit_edges ();
1785
1786   FOR_EACH_BB (bb)
1787     update_forwarder_flag (bb);
1788
1789   if (mode & (CLEANUP_UPDATE_LIFE | CLEANUP_CROSSJUMP | CLEANUP_THREADING))
1790     clear_bb_flags ();
1791
1792   if (! targetm.cannot_modify_jumps_p ())
1793     {
1794       first_pass = true;
1795       /* Attempt to merge blocks as made possible by edge removal.  If
1796          a block has only one successor, and the successor has only
1797          one predecessor, they may be combined.  */
1798       do
1799         {
1800           changed = false;
1801           iterations++;
1802
1803           if (dump_file)
1804             fprintf (dump_file,
1805                      "\n\ntry_optimize_cfg iteration %i\n\n",
1806                      iterations);
1807
1808           for (b = ENTRY_BLOCK_PTR->next_bb; b != EXIT_BLOCK_PTR;)
1809             {
1810               basic_block c;
1811               edge s;
1812               bool changed_here = false;
1813
1814               /* Delete trivially dead basic blocks.  */
1815               while (b->pred == NULL)
1816                 {
1817                   c = b->prev_bb;
1818                   if (dump_file)
1819                     fprintf (dump_file, "Deleting block %i.\n",
1820                              b->index);
1821
1822                   delete_basic_block (b);
1823                   if (!(mode & CLEANUP_CFGLAYOUT))
1824                     changed = true;
1825                   b = c;
1826                 }
1827
1828               /* Remove code labels no longer used.  */
1829               if (b->pred->pred_next == NULL
1830                   && (b->pred->flags & EDGE_FALLTHRU)
1831                   && !(b->pred->flags & EDGE_COMPLEX)
1832                   && LABEL_P (BB_HEAD (b))
1833                   /* If the previous block ends with a branch to this
1834                      block, we can't delete the label.  Normally this
1835                      is a condjump that is yet to be simplified, but
1836                      if CASE_DROPS_THRU, this can be a tablejump with
1837                      some element going to the same place as the
1838                      default (fallthru).  */
1839                   && (b->pred->src == ENTRY_BLOCK_PTR
1840                       || !JUMP_P (BB_END (b->pred->src))
1841                       || ! label_is_jump_target_p (BB_HEAD (b),
1842                                                    BB_END (b->pred->src))))
1843                 {
1844                   rtx label = BB_HEAD (b);
1845
1846                   delete_insn_chain (label, label);
1847                   /* In the case label is undeletable, move it after the
1848                      BASIC_BLOCK note.  */
1849                   if (NOTE_LINE_NUMBER (BB_HEAD (b)) == NOTE_INSN_DELETED_LABEL)
1850                     {
1851                       rtx bb_note = NEXT_INSN (BB_HEAD (b));
1852
1853                       reorder_insns_nobb (label, label, bb_note);
1854                       BB_HEAD (b) = bb_note;
1855                     }
1856                   if (dump_file)
1857                     fprintf (dump_file, "Deleted label in block %i.\n",
1858                              b->index);
1859                 }
1860
1861               /* If we fall through an empty block, we can remove it.  */
1862               if (!(mode & CLEANUP_CFGLAYOUT)
1863                   && b->pred->pred_next == NULL
1864                   && (b->pred->flags & EDGE_FALLTHRU)
1865                   && !LABEL_P (BB_HEAD (b))
1866                   && FORWARDER_BLOCK_P (b)
1867                   /* Note that forwarder_block_p true ensures that
1868                      there is a successor for this block.  */
1869                   && (b->succ->flags & EDGE_FALLTHRU)
1870                   && n_basic_blocks > 1)
1871                 {
1872                   if (dump_file)
1873                     fprintf (dump_file,
1874                              "Deleting fallthru block %i.\n",
1875                              b->index);
1876
1877                   c = b->prev_bb == ENTRY_BLOCK_PTR ? b->next_bb : b->prev_bb;
1878                   redirect_edge_succ_nodup (b->pred, b->succ->dest);
1879                   delete_basic_block (b);
1880                   changed = true;
1881                   b = c;
1882                 }
1883
1884               if ((s = b->succ) != NULL
1885                   && s->succ_next == NULL
1886                   && !(s->flags & EDGE_COMPLEX)
1887                   && (c = s->dest) != EXIT_BLOCK_PTR
1888                   && c->pred->pred_next == NULL
1889                   && b != c)
1890                 {
1891                   /* When not in cfg_layout mode use code aware of reordering
1892                      INSN.  This code possibly creates new basic blocks so it
1893                      does not fit merge_blocks interface and is kept here in
1894                      hope that it will become useless once more of compiler
1895                      is transformed to use cfg_layout mode.  */
1896                      
1897                   if ((mode & CLEANUP_CFGLAYOUT)
1898                       && can_merge_blocks_p (b, c))
1899                     {
1900                       merge_blocks (b, c);
1901                       update_forwarder_flag (b);
1902                       changed_here = true;
1903                     }
1904                   else if (!(mode & CLEANUP_CFGLAYOUT)
1905                            /* If the jump insn has side effects,
1906                               we can't kill the edge.  */
1907                            && (!JUMP_P (BB_END (b))
1908                                || (reload_completed
1909                                    ? simplejump_p (BB_END (b))
1910                                    : (onlyjump_p (BB_END (b))
1911                                       && !tablejump_p (BB_END (b),
1912                                                        NULL, NULL))))
1913                            && (next = merge_blocks_move (s, b, c, mode)))
1914                       {
1915                         b = next;
1916                         changed_here = true;
1917                       }
1918                 }
1919
1920               /* Simplify branch over branch.  */
1921               if ((mode & CLEANUP_EXPENSIVE)
1922                    && !(mode & CLEANUP_CFGLAYOUT)
1923                    && try_simplify_condjump (b))
1924                 changed_here = true;
1925
1926               /* If B has a single outgoing edge, but uses a
1927                  non-trivial jump instruction without side-effects, we
1928                  can either delete the jump entirely, or replace it
1929                  with a simple unconditional jump.  */
1930               if (b->succ
1931                   && ! b->succ->succ_next
1932                   && b->succ->dest != EXIT_BLOCK_PTR
1933                   && onlyjump_p (BB_END (b))
1934                   && !find_reg_note (BB_END (b), REG_CROSSING_JUMP, NULL_RTX)
1935                   && try_redirect_by_replacing_jump (b->succ, b->succ->dest,
1936                                                      (mode & CLEANUP_CFGLAYOUT) != 0))
1937                 {
1938                   update_forwarder_flag (b);
1939                   changed_here = true;
1940                 }
1941
1942               /* Simplify branch to branch.  */
1943               if (try_forward_edges (mode, b))
1944                 changed_here = true;
1945
1946               /* Look for shared code between blocks.  */
1947               if ((mode & CLEANUP_CROSSJUMP)
1948                   && try_crossjump_bb (mode, b))
1949                 changed_here = true;
1950
1951               /* Don't get confused by the index shift caused by
1952                  deleting blocks.  */
1953               if (!changed_here)
1954                 b = b->next_bb;
1955               else
1956                 changed = true;
1957             }
1958
1959           if ((mode & CLEANUP_CROSSJUMP)
1960               && try_crossjump_bb (mode, EXIT_BLOCK_PTR))
1961             changed = true;
1962
1963 #ifdef ENABLE_CHECKING
1964           if (changed)
1965             verify_flow_info ();
1966 #endif
1967
1968           changed_overall |= changed;
1969           first_pass = false;
1970         }
1971       while (changed);
1972     }
1973
1974   if (mode & CLEANUP_CROSSJUMP)
1975     remove_fake_exit_edges ();
1976
1977   clear_aux_for_blocks ();
1978
1979   return changed_overall;
1980 }
1981 \f
1982 /* Delete all unreachable basic blocks.  */
1983
1984 bool
1985 delete_unreachable_blocks (void)
1986 {
1987   bool changed = false;
1988   basic_block b, next_bb;
1989
1990   find_unreachable_blocks ();
1991
1992   /* Delete all unreachable basic blocks.  */
1993
1994   for (b = ENTRY_BLOCK_PTR->next_bb; b != EXIT_BLOCK_PTR; b = next_bb)
1995     {
1996       next_bb = b->next_bb;
1997
1998       if (!(b->flags & BB_REACHABLE))
1999         {
2000           delete_basic_block (b);
2001           changed = true;
2002         }
2003     }
2004
2005   if (changed)
2006     tidy_fallthru_edges ();
2007   return changed;
2008 }
2009
2010 /* Merges sequential blocks if possible.  */
2011
2012 bool
2013 merge_seq_blocks (void)
2014 {
2015   basic_block bb;
2016   bool changed = false;
2017
2018   for (bb = ENTRY_BLOCK_PTR->next_bb; bb != EXIT_BLOCK_PTR; )
2019     {
2020       if (bb->succ
2021           && !bb->succ->succ_next
2022           && can_merge_blocks_p (bb, bb->succ->dest))
2023         {
2024           /* Merge the blocks and retry.  */
2025           merge_blocks (bb, bb->succ->dest);
2026           changed = true;
2027           continue;
2028         }
2029
2030       bb = bb->next_bb;
2031     }
2032
2033   return changed;
2034 }
2035 \f
2036 /* Tidy the CFG by deleting unreachable code and whatnot.  */
2037
2038 bool
2039 cleanup_cfg (int mode)
2040 {
2041   bool changed = false;
2042
2043   timevar_push (TV_CLEANUP_CFG);
2044   if (delete_unreachable_blocks ())
2045     {
2046       changed = true;
2047       /* We've possibly created trivially dead code.  Cleanup it right
2048          now to introduce more opportunities for try_optimize_cfg.  */
2049       if (!(mode & (CLEANUP_NO_INSN_DEL | CLEANUP_UPDATE_LIFE))
2050           && !reload_completed)
2051         delete_trivially_dead_insns (get_insns(), max_reg_num ());
2052     }
2053
2054   compact_blocks ();
2055
2056   while (try_optimize_cfg (mode))
2057     {
2058       delete_unreachable_blocks (), changed = true;
2059       if (mode & CLEANUP_UPDATE_LIFE)
2060         {
2061           /* Cleaning up CFG introduces more opportunities for dead code
2062              removal that in turn may introduce more opportunities for
2063              cleaning up the CFG.  */
2064           if (!update_life_info_in_dirty_blocks (UPDATE_LIFE_GLOBAL_RM_NOTES,
2065                                                  PROP_DEATH_NOTES
2066                                                  | PROP_SCAN_DEAD_CODE
2067                                                  | PROP_KILL_DEAD_CODE
2068                                                  | ((mode & CLEANUP_LOG_LINKS)
2069                                                     ? PROP_LOG_LINKS : 0)))
2070             break;
2071         }
2072       else if (!(mode & CLEANUP_NO_INSN_DEL)
2073                && (mode & CLEANUP_EXPENSIVE)
2074                && !reload_completed)
2075         {
2076           if (!delete_trivially_dead_insns (get_insns(), max_reg_num ()))
2077             break;
2078         }
2079       else
2080         break;
2081       delete_dead_jumptables ();
2082     }
2083
2084   /* Kill the data we won't maintain.  */
2085   free_EXPR_LIST_list (&label_value_list);
2086   timevar_pop (TV_CLEANUP_CFG);
2087
2088   return changed;
2089 }