OSDN Git Service

* cfgcleanup.c (flow_find_head_matching_sequence): Terminate when
[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, 2005, 2006, 2007, 2008, 2010
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
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 "regs.h"
41 #include "timevar.h"
42 #include "output.h"
43 #include "insn-config.h"
44 #include "flags.h"
45 #include "recog.h"
46 #include "diagnostic-core.h"
47 #include "toplev.h"
48 #include "cselib.h"
49 #include "params.h"
50 #include "tm_p.h"
51 #include "target.h"
52 #include "cfglayout.h"
53 #include "emit-rtl.h"
54 #include "tree-pass.h"
55 #include "cfgloop.h"
56 #include "expr.h"
57 #include "df.h"
58 #include "dce.h"
59 #include "dbgcnt.h"
60
61 #define FORWARDER_BLOCK_P(BB) ((BB)->flags & BB_FORWARDER_BLOCK)
62
63 /* Set to true when we are running first pass of try_optimize_cfg loop.  */
64 static bool first_pass;
65
66 /* Set to true if crossjumps occured in the latest run of try_optimize_cfg.  */
67 static bool crossjumps_occured;
68
69 /* Set to true if we couldn't run an optimization due to stale liveness
70    information; we should run df_analyze to enable more opportunities.  */
71 static bool block_was_dirty;
72
73 static bool try_crossjump_to_edge (int, edge, edge);
74 static bool try_crossjump_bb (int, basic_block);
75 static bool outgoing_edges_match (int, basic_block, basic_block);
76 static bool old_insns_match_p (int, rtx, rtx);
77
78 static void merge_blocks_move_predecessor_nojumps (basic_block, basic_block);
79 static void merge_blocks_move_successor_nojumps (basic_block, basic_block);
80 static bool try_optimize_cfg (int);
81 static bool try_simplify_condjump (basic_block);
82 static bool try_forward_edges (int, basic_block);
83 static edge thread_jump (edge, basic_block);
84 static bool mark_effect (rtx, bitmap);
85 static void notice_new_block (basic_block);
86 static void update_forwarder_flag (basic_block);
87 static int mentions_nonequal_regs (rtx *, void *);
88 static void merge_memattrs (rtx, rtx);
89 \f
90 /* Set flags for newly created block.  */
91
92 static void
93 notice_new_block (basic_block bb)
94 {
95   if (!bb)
96     return;
97
98   if (forwarder_block_p (bb))
99     bb->flags |= BB_FORWARDER_BLOCK;
100 }
101
102 /* Recompute forwarder flag after block has been modified.  */
103
104 static void
105 update_forwarder_flag (basic_block bb)
106 {
107   if (forwarder_block_p (bb))
108     bb->flags |= BB_FORWARDER_BLOCK;
109   else
110     bb->flags &= ~BB_FORWARDER_BLOCK;
111 }
112 \f
113 /* Simplify a conditional jump around an unconditional jump.
114    Return true if something changed.  */
115
116 static bool
117 try_simplify_condjump (basic_block cbranch_block)
118 {
119   basic_block jump_block, jump_dest_block, cbranch_dest_block;
120   edge cbranch_jump_edge, cbranch_fallthru_edge;
121   rtx cbranch_insn;
122
123   /* Verify that there are exactly two successors.  */
124   if (EDGE_COUNT (cbranch_block->succs) != 2)
125     return false;
126
127   /* Verify that we've got a normal conditional branch at the end
128      of the block.  */
129   cbranch_insn = BB_END (cbranch_block);
130   if (!any_condjump_p (cbranch_insn))
131     return false;
132
133   cbranch_fallthru_edge = FALLTHRU_EDGE (cbranch_block);
134   cbranch_jump_edge = BRANCH_EDGE (cbranch_block);
135
136   /* The next block must not have multiple predecessors, must not
137      be the last block in the function, and must contain just the
138      unconditional jump.  */
139   jump_block = cbranch_fallthru_edge->dest;
140   if (!single_pred_p (jump_block)
141       || jump_block->next_bb == EXIT_BLOCK_PTR
142       || !FORWARDER_BLOCK_P (jump_block))
143     return false;
144   jump_dest_block = single_succ (jump_block);
145
146   /* If we are partitioning hot/cold basic blocks, we don't want to
147      mess up unconditional or indirect jumps that cross between hot
148      and cold sections.
149
150      Basic block partitioning may result in some jumps that appear to
151      be optimizable (or blocks that appear to be mergeable), but which really
152      must be left untouched (they are required to make it safely across
153      partition boundaries).  See the comments at the top of
154      bb-reorder.c:partition_hot_cold_basic_blocks for complete details.  */
155
156   if (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 (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   unsigned i;
279   regset nonequal;
280   bool failed = false;
281   reg_set_iterator rsi;
282
283   if (b->flags & BB_NONTHREADABLE_BLOCK)
284     return NULL;
285
286   /* At the moment, we do handle only conditional jumps, but later we may
287      want to extend this code to tablejumps and others.  */
288   if (EDGE_COUNT (e->src->succs) != 2)
289     return NULL;
290   if (EDGE_COUNT (b->succs) != 2)
291     {
292       b->flags |= BB_NONTHREADABLE_BLOCK;
293       return NULL;
294     }
295
296   /* Second branch must end with onlyjump, as we will eliminate the jump.  */
297   if (!any_condjump_p (BB_END (e->src)))
298     return NULL;
299
300   if (!any_condjump_p (BB_END (b)) || !onlyjump_p (BB_END (b)))
301     {
302       b->flags |= BB_NONTHREADABLE_BLOCK;
303       return NULL;
304     }
305
306   set1 = pc_set (BB_END (e->src));
307   set2 = pc_set (BB_END (b));
308   if (((e->flags & EDGE_FALLTHRU) != 0)
309       != (XEXP (SET_SRC (set1), 1) == pc_rtx))
310     reverse1 = true;
311
312   cond1 = XEXP (SET_SRC (set1), 0);
313   cond2 = XEXP (SET_SRC (set2), 0);
314   if (reverse1)
315     code1 = reversed_comparison_code (cond1, BB_END (e->src));
316   else
317     code1 = GET_CODE (cond1);
318
319   code2 = GET_CODE (cond2);
320   reversed_code2 = reversed_comparison_code (cond2, BB_END (b));
321
322   if (!comparison_dominates_p (code1, code2)
323       && !comparison_dominates_p (code1, reversed_code2))
324     return NULL;
325
326   /* Ensure that the comparison operators are equivalent.
327      ??? This is far too pessimistic.  We should allow swapped operands,
328      different CCmodes, or for example comparisons for interval, that
329      dominate even when operands are not equivalent.  */
330   if (!rtx_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
331       || !rtx_equal_p (XEXP (cond1, 1), XEXP (cond2, 1)))
332     return NULL;
333
334   /* Short circuit cases where block B contains some side effects, as we can't
335      safely bypass it.  */
336   for (insn = NEXT_INSN (BB_HEAD (b)); insn != NEXT_INSN (BB_END (b));
337        insn = NEXT_INSN (insn))
338     if (INSN_P (insn) && side_effects_p (PATTERN (insn)))
339       {
340         b->flags |= BB_NONTHREADABLE_BLOCK;
341         return NULL;
342       }
343
344   cselib_init (0);
345
346   /* First process all values computed in the source basic block.  */
347   for (insn = NEXT_INSN (BB_HEAD (e->src));
348        insn != NEXT_INSN (BB_END (e->src));
349        insn = NEXT_INSN (insn))
350     if (INSN_P (insn))
351       cselib_process_insn (insn);
352
353   nonequal = BITMAP_ALLOC (NULL);
354   CLEAR_REG_SET (nonequal);
355
356   /* Now assume that we've continued by the edge E to B and continue
357      processing as if it were same basic block.
358      Our goal is to prove that whole block is an NOOP.  */
359
360   for (insn = NEXT_INSN (BB_HEAD (b));
361        insn != NEXT_INSN (BB_END (b)) && !failed;
362        insn = NEXT_INSN (insn))
363     {
364       if (INSN_P (insn))
365         {
366           rtx pat = PATTERN (insn);
367
368           if (GET_CODE (pat) == PARALLEL)
369             {
370               for (i = 0; i < (unsigned)XVECLEN (pat, 0); i++)
371                 failed |= mark_effect (XVECEXP (pat, 0, i), nonequal);
372             }
373           else
374             failed |= mark_effect (pat, nonequal);
375         }
376
377       cselib_process_insn (insn);
378     }
379
380   /* Later we should clear nonequal of dead registers.  So far we don't
381      have life information in cfg_cleanup.  */
382   if (failed)
383     {
384       b->flags |= BB_NONTHREADABLE_BLOCK;
385       goto failed_exit;
386     }
387
388   /* cond2 must not mention any register that is not equal to the
389      former block.  */
390   if (for_each_rtx (&cond2, mentions_nonequal_regs, nonequal))
391     goto failed_exit;
392
393   EXECUTE_IF_SET_IN_REG_SET (nonequal, 0, i, rsi)
394     goto failed_exit;
395
396   BITMAP_FREE (nonequal);
397   cselib_finish ();
398   if ((comparison_dominates_p (code1, code2) != 0)
399       != (XEXP (SET_SRC (set2), 1) == pc_rtx))
400     return BRANCH_EDGE (b);
401   else
402     return FALLTHRU_EDGE (b);
403
404 failed_exit:
405   BITMAP_FREE (nonequal);
406   cselib_finish ();
407   return NULL;
408 }
409 \f
410 /* Attempt to forward edges leaving basic block B.
411    Return true if successful.  */
412
413 static bool
414 try_forward_edges (int mode, basic_block b)
415 {
416   bool changed = false;
417   edge_iterator ei;
418   edge e, *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      Basic block partitioning may result in some jumps that appear to
425      be optimizable (or blocks that appear to be mergeable), but which really
426      must be left untouched (they are required to make it safely across
427      partition boundaries).  See the comments at the top of
428      bb-reorder.c:partition_hot_cold_basic_blocks for complete details.  */
429
430   if (find_reg_note (BB_END (b), REG_CROSSING_JUMP, NULL_RTX))
431     return false;
432
433   for (ei = ei_start (b->succs); (e = ei_safe_edge (ei)); )
434     {
435       basic_block target, first;
436       int counter, goto_locus;
437       bool threaded = false;
438       int nthreaded_edges = 0;
439       bool may_thread = first_pass || (b->flags & BB_MODIFIED) != 0;
440
441       /* Skip complex edges because we don't know how to update them.
442
443          Still handle fallthru edges, as we can succeed to forward fallthru
444          edge to the same place as the branch edge of conditional branch
445          and turn conditional branch to an unconditional branch.  */
446       if (e->flags & EDGE_COMPLEX)
447         {
448           ei_next (&ei);
449           continue;
450         }
451
452       target = first = e->dest;
453       counter = NUM_FIXED_BLOCKS;
454       goto_locus = e->goto_locus;
455
456       /* If we are partitioning hot/cold basic_blocks, we don't want to mess
457          up jumps that cross between hot/cold sections.
458
459          Basic block partitioning may result in some jumps that appear
460          to be optimizable (or blocks that appear to be mergeable), but which
461          really must be left untouched (they are required to make it safely
462          across partition boundaries).  See the comments at the top of
463          bb-reorder.c:partition_hot_cold_basic_blocks for complete
464          details.  */
465
466       if (first != EXIT_BLOCK_PTR
467           && find_reg_note (BB_END (first), REG_CROSSING_JUMP, NULL_RTX))
468         return false;
469
470       while (counter < n_basic_blocks)
471         {
472           basic_block new_target = NULL;
473           bool new_target_threaded = false;
474           may_thread |= (target->flags & BB_MODIFIED) != 0;
475
476           if (FORWARDER_BLOCK_P (target)
477               && !(single_succ_edge (target)->flags & EDGE_CROSSING)
478               && single_succ (target) != EXIT_BLOCK_PTR)
479             {
480               /* Bypass trivial infinite loops.  */
481               new_target = single_succ (target);
482               if (target == new_target)
483                 counter = n_basic_blocks;
484               else if (!optimize)
485                 {
486                   /* When not optimizing, ensure that edges or forwarder
487                      blocks with different locus are not optimized out.  */
488                   int locus = single_succ_edge (target)->goto_locus;
489
490                   if (locus && goto_locus && !locator_eq (locus, goto_locus))
491                     counter = n_basic_blocks;
492                   else if (locus)
493                     goto_locus = locus;
494
495                   if (INSN_P (BB_END (target)))
496                     {
497                       locus = INSN_LOCATOR (BB_END (target));
498
499                       if (locus && goto_locus
500                           && !locator_eq (locus, goto_locus))
501                         counter = n_basic_blocks;
502                       else if (locus)
503                         goto_locus = locus;
504                     }
505                 }
506             }
507
508           /* Allow to thread only over one edge at time to simplify updating
509              of probabilities.  */
510           else if ((mode & CLEANUP_THREADING) && may_thread)
511             {
512               edge t = thread_jump (e, target);
513               if (t)
514                 {
515                   if (!threaded_edges)
516                     threaded_edges = XNEWVEC (edge, n_basic_blocks);
517                   else
518                     {
519                       int i;
520
521                       /* Detect an infinite loop across blocks not
522                          including the start block.  */
523                       for (i = 0; i < nthreaded_edges; ++i)
524                         if (threaded_edges[i] == t)
525                           break;
526                       if (i < nthreaded_edges)
527                         {
528                           counter = n_basic_blocks;
529                           break;
530                         }
531                     }
532
533                   /* Detect an infinite loop across the start block.  */
534                   if (t->dest == b)
535                     break;
536
537                   gcc_assert (nthreaded_edges < n_basic_blocks - NUM_FIXED_BLOCKS);
538                   threaded_edges[nthreaded_edges++] = t;
539
540                   new_target = t->dest;
541                   new_target_threaded = true;
542                 }
543             }
544
545           if (!new_target)
546             break;
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           e->goto_locus = goto_locus;
570
571           /* Don't force if target is exit block.  */
572           if (threaded && target != EXIT_BLOCK_PTR)
573             {
574               notice_new_block (redirect_edge_and_branch_force (e, target));
575               if (dump_file)
576                 fprintf (dump_file, "Conditionals threaded.\n");
577             }
578           else if (!redirect_edge_and_branch (e, target))
579             {
580               if (dump_file)
581                 fprintf (dump_file,
582                          "Forwarding edge %i->%i to %i failed.\n",
583                          b->index, e->dest->index, target->index);
584               ei_next (&ei);
585               continue;
586             }
587
588           /* We successfully forwarded the edge.  Now update profile
589              data: for each edge we traversed in the chain, remove
590              the original edge's execution count.  */
591           edge_frequency = ((edge_probability * b->frequency
592                              + REG_BR_PROB_BASE / 2)
593                             / REG_BR_PROB_BASE);
594
595           if (!FORWARDER_BLOCK_P (b) && forwarder_block_p (b))
596             b->flags |= BB_FORWARDER_BLOCK;
597
598           do
599             {
600               edge t;
601
602               if (!single_succ_p (first))
603                 {
604                   gcc_assert (n < nthreaded_edges);
605                   t = threaded_edges [n++];
606                   gcc_assert (t->src == first);
607                   update_bb_profile_for_threading (first, edge_frequency,
608                                                    edge_count, t);
609                   update_br_prob_note (first);
610                 }
611               else
612                 {
613                   first->count -= edge_count;
614                   if (first->count < 0)
615                     first->count = 0;
616                   first->frequency -= edge_frequency;
617                   if (first->frequency < 0)
618                     first->frequency = 0;
619                   /* It is possible that as the result of
620                      threading we've removed edge as it is
621                      threaded to the fallthru edge.  Avoid
622                      getting out of sync.  */
623                   if (n < nthreaded_edges
624                       && first == threaded_edges [n]->src)
625                     n++;
626                   t = single_succ_edge (first);
627                 }
628
629               t->count -= edge_count;
630               if (t->count < 0)
631                 t->count = 0;
632               first = t->dest;
633             }
634           while (first != target);
635
636           changed = true;
637           continue;
638         }
639       ei_next (&ei);
640     }
641
642   if (threaded_edges)
643     free (threaded_edges);
644   return changed;
645 }
646 \f
647
648 /* Blocks A and B are to be merged into a single block.  A has no incoming
649    fallthru edge, so it can be moved before B without adding or modifying
650    any jumps (aside from the jump from A to B).  */
651
652 static void
653 merge_blocks_move_predecessor_nojumps (basic_block a, basic_block b)
654 {
655   rtx barrier;
656
657   /* If we are partitioning hot/cold basic blocks, we don't want to
658      mess up unconditional or indirect jumps that cross between hot
659      and cold sections.
660
661      Basic block partitioning may result in some jumps that appear to
662      be optimizable (or blocks that appear to be mergeable), but which really
663      must be left untouched (they are required to make it safely across
664      partition boundaries).  See the comments at the top of
665      bb-reorder.c:partition_hot_cold_basic_blocks for complete details.  */
666
667   if (BB_PARTITION (a) != BB_PARTITION (b))
668     return;
669
670   barrier = next_nonnote_insn (BB_END (a));
671   gcc_assert (BARRIER_P (barrier));
672   delete_insn (barrier);
673
674   /* Scramble the insn chain.  */
675   if (BB_END (a) != PREV_INSN (BB_HEAD (b)))
676     reorder_insns_nobb (BB_HEAD (a), BB_END (a), PREV_INSN (BB_HEAD (b)));
677   df_set_bb_dirty (a);
678
679   if (dump_file)
680     fprintf (dump_file, "Moved block %d before %d and merged.\n",
681              a->index, b->index);
682
683   /* Swap the records for the two blocks around.  */
684
685   unlink_block (a);
686   link_block (a, b->prev_bb);
687
688   /* Now blocks A and B are contiguous.  Merge them.  */
689   merge_blocks (a, b);
690 }
691
692 /* Blocks A and B are to be merged into a single block.  B has no outgoing
693    fallthru edge, so it can be moved after A without adding or modifying
694    any jumps (aside from the jump from A to B).  */
695
696 static void
697 merge_blocks_move_successor_nojumps (basic_block a, basic_block b)
698 {
699   rtx barrier, real_b_end;
700   rtx label, table;
701
702   /* If we are partitioning hot/cold basic blocks, we don't want to
703      mess up unconditional or indirect jumps that cross between hot
704      and cold sections.
705
706      Basic block partitioning may result in some jumps that appear to
707      be optimizable (or blocks that appear to be mergeable), but which really
708      must be left untouched (they are required to make it safely across
709      partition boundaries).  See the comments at the top of
710      bb-reorder.c:partition_hot_cold_basic_blocks for complete details.  */
711
712   if (BB_PARTITION (a) != BB_PARTITION (b))
713     return;
714
715   real_b_end = BB_END (b);
716
717   /* If there is a jump table following block B temporarily add the jump table
718      to block B so that it will also be moved to the correct location.  */
719   if (tablejump_p (BB_END (b), &label, &table)
720       && prev_active_insn (label) == BB_END (b))
721     {
722       BB_END (b) = table;
723     }
724
725   /* There had better have been a barrier there.  Delete it.  */
726   barrier = NEXT_INSN (BB_END (b));
727   if (barrier && BARRIER_P (barrier))
728     delete_insn (barrier);
729
730
731   /* Scramble the insn chain.  */
732   reorder_insns_nobb (BB_HEAD (b), BB_END (b), BB_END (a));
733
734   /* Restore the real end of b.  */
735   BB_END (b) = real_b_end;
736
737   if (dump_file)
738     fprintf (dump_file, "Moved block %d after %d and merged.\n",
739              b->index, a->index);
740
741   /* Now blocks A and B are contiguous.  Merge them.  */
742   merge_blocks (a, b);
743 }
744
745 /* Attempt to merge basic blocks that are potentially non-adjacent.
746    Return NULL iff the attempt failed, otherwise return basic block
747    where cleanup_cfg should continue.  Because the merging commonly
748    moves basic block away or introduces another optimization
749    possibility, return basic block just before B so cleanup_cfg don't
750    need to iterate.
751
752    It may be good idea to return basic block before C in the case
753    C has been moved after B and originally appeared earlier in the
754    insn sequence, but we have no information available about the
755    relative ordering of these two.  Hopefully it is not too common.  */
756
757 static basic_block
758 merge_blocks_move (edge e, basic_block b, basic_block c, int mode)
759 {
760   basic_block next;
761
762   /* If we are partitioning hot/cold basic blocks, we don't want to
763      mess up unconditional or indirect jumps that cross between hot
764      and cold sections.
765
766      Basic block partitioning may result in some jumps that appear to
767      be optimizable (or blocks that appear to be mergeable), but which really
768      must be left untouched (they are required to make it safely across
769      partition boundaries).  See the comments at the top of
770      bb-reorder.c:partition_hot_cold_basic_blocks for complete details.  */
771
772   if (BB_PARTITION (b) != BB_PARTITION (c))
773     return NULL;
774
775   /* If B has a fallthru edge to C, no need to move anything.  */
776   if (e->flags & EDGE_FALLTHRU)
777     {
778       int b_index = b->index, c_index = c->index;
779       merge_blocks (b, c);
780       update_forwarder_flag (b);
781
782       if (dump_file)
783         fprintf (dump_file, "Merged %d and %d without moving.\n",
784                  b_index, c_index);
785
786       return b->prev_bb == ENTRY_BLOCK_PTR ? b : b->prev_bb;
787     }
788
789   /* Otherwise we will need to move code around.  Do that only if expensive
790      transformations are allowed.  */
791   else if (mode & CLEANUP_EXPENSIVE)
792     {
793       edge tmp_edge, b_fallthru_edge;
794       bool c_has_outgoing_fallthru;
795       bool b_has_incoming_fallthru;
796       edge_iterator ei;
797
798       /* Avoid overactive code motion, as the forwarder blocks should be
799          eliminated by edge redirection instead.  One exception might have
800          been if B is a forwarder block and C has no fallthru edge, but
801          that should be cleaned up by bb-reorder instead.  */
802       if (FORWARDER_BLOCK_P (b) || FORWARDER_BLOCK_P (c))
803         return NULL;
804
805       /* We must make sure to not munge nesting of lexical blocks,
806          and loop notes.  This is done by squeezing out all the notes
807          and leaving them there to lie.  Not ideal, but functional.  */
808
809       FOR_EACH_EDGE (tmp_edge, ei, c->succs)
810         if (tmp_edge->flags & EDGE_FALLTHRU)
811           break;
812
813       c_has_outgoing_fallthru = (tmp_edge != NULL);
814
815       FOR_EACH_EDGE (tmp_edge, ei, b->preds)
816         if (tmp_edge->flags & EDGE_FALLTHRU)
817           break;
818
819       b_has_incoming_fallthru = (tmp_edge != NULL);
820       b_fallthru_edge = tmp_edge;
821       next = b->prev_bb;
822       if (next == c)
823         next = next->prev_bb;
824
825       /* Otherwise, we're going to try to move C after B.  If C does
826          not have an outgoing fallthru, then it can be moved
827          immediately after B without introducing or modifying jumps.  */
828       if (! c_has_outgoing_fallthru)
829         {
830           merge_blocks_move_successor_nojumps (b, c);
831           return next == ENTRY_BLOCK_PTR ? next->next_bb : next;
832         }
833
834       /* If B does not have an incoming fallthru, then it can be moved
835          immediately before C without introducing or modifying jumps.
836          C cannot be the first block, so we do not have to worry about
837          accessing a non-existent block.  */
838
839       if (b_has_incoming_fallthru)
840         {
841           basic_block bb;
842
843           if (b_fallthru_edge->src == ENTRY_BLOCK_PTR)
844             return NULL;
845           bb = force_nonfallthru (b_fallthru_edge);
846           if (bb)
847             notice_new_block (bb);
848         }
849
850       merge_blocks_move_predecessor_nojumps (b, c);
851       return next == ENTRY_BLOCK_PTR ? next->next_bb : next;
852     }
853
854   return NULL;
855 }
856 \f
857
858 /* Removes the memory attributes of MEM expression
859    if they are not equal.  */
860
861 void
862 merge_memattrs (rtx x, rtx y)
863 {
864   int i;
865   int j;
866   enum rtx_code code;
867   const char *fmt;
868
869   if (x == y)
870     return;
871   if (x == 0 || y == 0)
872     return;
873
874   code = GET_CODE (x);
875
876   if (code != GET_CODE (y))
877     return;
878
879   if (GET_MODE (x) != GET_MODE (y))
880     return;
881
882   if (code == MEM && MEM_ATTRS (x) != MEM_ATTRS (y))
883     {
884       if (! MEM_ATTRS (x))
885         MEM_ATTRS (y) = 0;
886       else if (! MEM_ATTRS (y))
887         MEM_ATTRS (x) = 0;
888       else
889         {
890           rtx mem_size;
891
892           if (MEM_ALIAS_SET (x) != MEM_ALIAS_SET (y))
893             {
894               set_mem_alias_set (x, 0);
895               set_mem_alias_set (y, 0);
896             }
897
898           if (! mem_expr_equal_p (MEM_EXPR (x), MEM_EXPR (y)))
899             {
900               set_mem_expr (x, 0);
901               set_mem_expr (y, 0);
902               set_mem_offset (x, 0);
903               set_mem_offset (y, 0);
904             }
905           else if (MEM_OFFSET (x) != MEM_OFFSET (y))
906             {
907               set_mem_offset (x, 0);
908               set_mem_offset (y, 0);
909             }
910
911           if (!MEM_SIZE (x))
912             mem_size = NULL_RTX;
913           else if (!MEM_SIZE (y))
914             mem_size = NULL_RTX;
915           else
916             mem_size = GEN_INT (MAX (INTVAL (MEM_SIZE (x)),
917                                      INTVAL (MEM_SIZE (y))));
918           set_mem_size (x, mem_size);
919           set_mem_size (y, mem_size);
920
921           set_mem_align (x, MIN (MEM_ALIGN (x), MEM_ALIGN (y)));
922           set_mem_align (y, MEM_ALIGN (x));
923         }
924     }
925
926   fmt = GET_RTX_FORMAT (code);
927   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
928     {
929       switch (fmt[i])
930         {
931         case 'E':
932           /* Two vectors must have the same length.  */
933           if (XVECLEN (x, i) != XVECLEN (y, i))
934             return;
935
936           for (j = 0; j < XVECLEN (x, i); j++)
937             merge_memattrs (XVECEXP (x, i, j), XVECEXP (y, i, j));
938
939           break;
940
941         case 'e':
942           merge_memattrs (XEXP (x, i), XEXP (y, i));
943         }
944     }
945   return;
946 }
947
948
949 /* Return true if I1 and I2 are equivalent and thus can be crossjumped.  */
950
951 static bool
952 old_insns_match_p (int mode ATTRIBUTE_UNUSED, rtx i1, rtx i2)
953 {
954   rtx p1, p2;
955
956   /* Verify that I1 and I2 are equivalent.  */
957   if (GET_CODE (i1) != GET_CODE (i2))
958     return false;
959
960   /* __builtin_unreachable() may lead to empty blocks (ending with
961      NOTE_INSN_BASIC_BLOCK).  They may be crossjumped. */
962   if (NOTE_INSN_BASIC_BLOCK_P (i1) && NOTE_INSN_BASIC_BLOCK_P (i2))
963     return true;
964
965   p1 = PATTERN (i1);
966   p2 = PATTERN (i2);
967
968   if (GET_CODE (p1) != GET_CODE (p2))
969     return false;
970
971   /* If this is a CALL_INSN, compare register usage information.
972      If we don't check this on stack register machines, the two
973      CALL_INSNs might be merged leaving reg-stack.c with mismatching
974      numbers of stack registers in the same basic block.
975      If we don't check this on machines with delay slots, a delay slot may
976      be filled that clobbers a parameter expected by the subroutine.
977
978      ??? We take the simple route for now and assume that if they're
979      equal, they were constructed identically.
980
981      Also check for identical exception regions.  */
982
983   if (CALL_P (i1))
984     {
985       /* Ensure the same EH region.  */
986       rtx n1 = find_reg_note (i1, REG_EH_REGION, 0);
987       rtx n2 = find_reg_note (i2, REG_EH_REGION, 0);
988
989       if (!n1 && n2)
990         return false;
991
992       if (n1 && (!n2 || XEXP (n1, 0) != XEXP (n2, 0)))
993         return false;
994
995       if (!rtx_equal_p (CALL_INSN_FUNCTION_USAGE (i1),
996                         CALL_INSN_FUNCTION_USAGE (i2))
997           || SIBLING_CALL_P (i1) != SIBLING_CALL_P (i2))
998         return false;
999     }
1000
1001 #ifdef STACK_REGS
1002   /* If cross_jump_death_matters is not 0, the insn's mode
1003      indicates whether or not the insn contains any stack-like
1004      regs.  */
1005
1006   if ((mode & CLEANUP_POST_REGSTACK) && stack_regs_mentioned (i1))
1007     {
1008       /* If register stack conversion has already been done, then
1009          death notes must also be compared before it is certain that
1010          the two instruction streams match.  */
1011
1012       rtx note;
1013       HARD_REG_SET i1_regset, i2_regset;
1014
1015       CLEAR_HARD_REG_SET (i1_regset);
1016       CLEAR_HARD_REG_SET (i2_regset);
1017
1018       for (note = REG_NOTES (i1); note; note = XEXP (note, 1))
1019         if (REG_NOTE_KIND (note) == REG_DEAD && STACK_REG_P (XEXP (note, 0)))
1020           SET_HARD_REG_BIT (i1_regset, REGNO (XEXP (note, 0)));
1021
1022       for (note = REG_NOTES (i2); note; note = XEXP (note, 1))
1023         if (REG_NOTE_KIND (note) == REG_DEAD && STACK_REG_P (XEXP (note, 0)))
1024           SET_HARD_REG_BIT (i2_regset, REGNO (XEXP (note, 0)));
1025
1026       if (!hard_reg_set_equal_p (i1_regset, i2_regset))
1027         return false;
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   return false;
1036 }
1037 \f
1038 /* When comparing insns I1 and I2 in flow_find_cross_jump or
1039    flow_find_head_matching_sequence, ensure the notes match.  */
1040
1041 static void
1042 merge_notes (rtx i1, rtx i2)
1043 {
1044   /* If the merged insns have different REG_EQUAL notes, then
1045      remove them.  */
1046   rtx equiv1 = find_reg_equal_equiv_note (i1);
1047   rtx equiv2 = find_reg_equal_equiv_note (i2);
1048
1049   if (equiv1 && !equiv2)
1050     remove_note (i1, equiv1);
1051   else if (!equiv1 && equiv2)
1052     remove_note (i2, equiv2);
1053   else if (equiv1 && equiv2
1054            && !rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))
1055     {
1056       remove_note (i1, equiv1);
1057       remove_note (i2, equiv2);
1058     }
1059 }
1060
1061 /* Look through the insns at the end of BB1 and BB2 and find the longest
1062    sequence that are equivalent.  Store the first insns for that sequence
1063    in *F1 and *F2 and return the sequence length.
1064
1065    To simplify callers of this function, if the blocks match exactly,
1066    store the head of the blocks in *F1 and *F2.  */
1067
1068 int
1069 flow_find_cross_jump (basic_block bb1, basic_block bb2, rtx *f1, rtx *f2)
1070 {
1071   rtx i1, i2, last1, last2, afterlast1, afterlast2;
1072   int ninsns = 0;
1073
1074   /* Skip simple jumps at the end of the blocks.  Complex jumps still
1075      need to be compared for equivalence, which we'll do below.  */
1076
1077   i1 = BB_END (bb1);
1078   last1 = afterlast1 = last2 = afterlast2 = NULL_RTX;
1079   if (onlyjump_p (i1)
1080       || (returnjump_p (i1) && !side_effects_p (PATTERN (i1))))
1081     {
1082       last1 = i1;
1083       i1 = PREV_INSN (i1);
1084     }
1085
1086   i2 = BB_END (bb2);
1087   if (onlyjump_p (i2)
1088       || (returnjump_p (i2) && !side_effects_p (PATTERN (i2))))
1089     {
1090       last2 = i2;
1091       /* Count everything except for unconditional jump as insn.  */
1092       if (!simplejump_p (i2) && !returnjump_p (i2) && last1)
1093         ninsns++;
1094       i2 = PREV_INSN (i2);
1095     }
1096
1097   while (true)
1098     {
1099       /* Ignore notes.  */
1100       while (!NONDEBUG_INSN_P (i1) && i1 != BB_HEAD (bb1))
1101         i1 = PREV_INSN (i1);
1102
1103       while (!NONDEBUG_INSN_P (i2) && i2 != BB_HEAD (bb2))
1104         i2 = PREV_INSN (i2);
1105
1106       if (i1 == BB_HEAD (bb1) || i2 == BB_HEAD (bb2))
1107         break;
1108
1109       if (!old_insns_match_p (0, i1, i2))
1110         break;
1111
1112       merge_memattrs (i1, i2);
1113
1114       /* Don't begin a cross-jump with a NOTE insn.  */
1115       if (INSN_P (i1))
1116         {
1117           merge_notes (i1, i2);
1118
1119           afterlast1 = last1, afterlast2 = last2;
1120           last1 = i1, last2 = i2;
1121           ninsns++;
1122         }
1123
1124       i1 = PREV_INSN (i1);
1125       i2 = PREV_INSN (i2);
1126     }
1127
1128 #ifdef HAVE_cc0
1129   /* Don't allow the insn after a compare to be shared by
1130      cross-jumping unless the compare is also shared.  */
1131   if (ninsns && reg_mentioned_p (cc0_rtx, last1) && ! sets_cc0_p (last1))
1132     last1 = afterlast1, last2 = afterlast2, ninsns--;
1133 #endif
1134
1135   /* Include preceding notes and labels in the cross-jump.  One,
1136      this may bring us to the head of the blocks as requested above.
1137      Two, it keeps line number notes as matched as may be.  */
1138   if (ninsns)
1139     {
1140       while (last1 != BB_HEAD (bb1) && !NONDEBUG_INSN_P (PREV_INSN (last1)))
1141         last1 = PREV_INSN (last1);
1142
1143       if (last1 != BB_HEAD (bb1) && LABEL_P (PREV_INSN (last1)))
1144         last1 = PREV_INSN (last1);
1145
1146       while (last2 != BB_HEAD (bb2) && !NONDEBUG_INSN_P (PREV_INSN (last2)))
1147         last2 = PREV_INSN (last2);
1148
1149       if (last2 != BB_HEAD (bb2) && LABEL_P (PREV_INSN (last2)))
1150         last2 = PREV_INSN (last2);
1151
1152       *f1 = last1;
1153       *f2 = last2;
1154     }
1155
1156   return ninsns;
1157 }
1158
1159 /* Like flow_find_cross_jump, except start looking for a matching sequence from
1160    the head of the two blocks.  Do not include jumps at the end.
1161    If STOP_AFTER is nonzero, stop after finding that many matching
1162    instructions.  */
1163
1164 int
1165 flow_find_head_matching_sequence (basic_block bb1, basic_block bb2, rtx *f1,
1166                                   rtx *f2, int stop_after)
1167 {
1168   rtx i1, i2, last1, last2, beforelast1, beforelast2;
1169   int ninsns = 0;
1170   edge e;
1171   edge_iterator ei;
1172   int nehedges1 = 0, nehedges2 = 0;
1173
1174   FOR_EACH_EDGE (e, ei, bb1->succs)
1175     if (e->flags & EDGE_EH)
1176       nehedges1++;
1177   FOR_EACH_EDGE (e, ei, bb2->succs)
1178     if (e->flags & EDGE_EH)
1179       nehedges2++;
1180
1181   i1 = BB_HEAD (bb1);
1182   i2 = BB_HEAD (bb2);
1183   last1 = beforelast1 = last2 = beforelast2 = NULL_RTX;
1184
1185   while (true)
1186     {
1187       /* Ignore notes.  */
1188       while (!NONDEBUG_INSN_P (i1) && i1 != BB_END (bb1))
1189         i1 = NEXT_INSN (i1);
1190
1191       while (!NONDEBUG_INSN_P (i2) && i2 != BB_END (bb2))
1192         i2 = NEXT_INSN (i2);
1193
1194       if ((i1 == BB_END (bb1) && !NONDEBUG_INSN_P (i1))
1195           || (i2 == BB_END (bb2) && !NONDEBUG_INSN_P (i2)))
1196         break;
1197
1198       if (NOTE_P (i1) || NOTE_P (i2)
1199           || JUMP_P (i1) || JUMP_P (i2))
1200         break;
1201
1202       /* A sanity check to make sure we're not merging insns with different
1203          effects on EH.  If only one of them ends a basic block, it shouldn't
1204          have an EH edge; if both end a basic block, there should be the same
1205          number of EH edges.  */
1206       if ((i1 == BB_END (bb1) && i2 != BB_END (bb2)
1207            && nehedges1 > 0)
1208           || (i2 == BB_END (bb2) && i1 != BB_END (bb1)
1209               && nehedges2 > 0)
1210           || (i1 == BB_END (bb1) && i2 == BB_END (bb2)
1211               && nehedges1 != nehedges2))
1212         break;
1213
1214       if (!old_insns_match_p (0, i1, i2))
1215         break;
1216
1217       merge_memattrs (i1, i2);
1218
1219       /* Don't begin a cross-jump with a NOTE insn.  */
1220       if (INSN_P (i1))
1221         {
1222           merge_notes (i1, i2);
1223
1224           beforelast1 = last1, beforelast2 = last2;
1225           last1 = i1, last2 = i2;
1226           ninsns++;
1227         }
1228
1229       if (i1 == BB_END (bb1) || i2 == BB_END (bb2)
1230           || (stop_after > 0 && ninsns == stop_after))
1231         break;
1232
1233       i1 = NEXT_INSN (i1);
1234       i2 = NEXT_INSN (i2);
1235     }
1236
1237 #ifdef HAVE_cc0
1238   /* Don't allow a compare to be shared by cross-jumping unless the insn
1239      after the compare is also shared.  */
1240   if (ninsns && reg_mentioned_p (cc0_rtx, last1) && sets_cc0_p (last1))
1241     last1 = beforelast1, last2 = beforelast2, ninsns--;
1242 #endif
1243
1244   if (ninsns)
1245     {
1246       *f1 = last1;
1247       *f2 = last2;
1248     }
1249
1250   return ninsns;
1251 }
1252
1253 /* Return true iff outgoing edges of BB1 and BB2 match, together with
1254    the branch instruction.  This means that if we commonize the control
1255    flow before end of the basic block, the semantic remains unchanged.
1256
1257    We may assume that there exists one edge with a common destination.  */
1258
1259 static bool
1260 outgoing_edges_match (int mode, basic_block bb1, basic_block bb2)
1261 {
1262   int nehedges1 = 0, nehedges2 = 0;
1263   edge fallthru1 = 0, fallthru2 = 0;
1264   edge e1, e2;
1265   edge_iterator ei;
1266
1267   /* If BB1 has only one successor, we may be looking at either an
1268      unconditional jump, or a fake edge to exit.  */
1269   if (single_succ_p (bb1)
1270       && (single_succ_edge (bb1)->flags & (EDGE_COMPLEX | EDGE_FAKE)) == 0
1271       && (!JUMP_P (BB_END (bb1)) || simplejump_p (BB_END (bb1))))
1272     return (single_succ_p (bb2)
1273             && (single_succ_edge (bb2)->flags
1274                 & (EDGE_COMPLEX | EDGE_FAKE)) == 0
1275             && (!JUMP_P (BB_END (bb2)) || simplejump_p (BB_END (bb2))));
1276
1277   /* Match conditional jumps - this may get tricky when fallthru and branch
1278      edges are crossed.  */
1279   if (EDGE_COUNT (bb1->succs) == 2
1280       && any_condjump_p (BB_END (bb1))
1281       && onlyjump_p (BB_END (bb1)))
1282     {
1283       edge b1, f1, b2, f2;
1284       bool reverse, match;
1285       rtx set1, set2, cond1, cond2;
1286       enum rtx_code code1, code2;
1287
1288       if (EDGE_COUNT (bb2->succs) != 2
1289           || !any_condjump_p (BB_END (bb2))
1290           || !onlyjump_p (BB_END (bb2)))
1291         return false;
1292
1293       b1 = BRANCH_EDGE (bb1);
1294       b2 = BRANCH_EDGE (bb2);
1295       f1 = FALLTHRU_EDGE (bb1);
1296       f2 = FALLTHRU_EDGE (bb2);
1297
1298       /* Get around possible forwarders on fallthru edges.  Other cases
1299          should be optimized out already.  */
1300       if (FORWARDER_BLOCK_P (f1->dest))
1301         f1 = single_succ_edge (f1->dest);
1302
1303       if (FORWARDER_BLOCK_P (f2->dest))
1304         f2 = single_succ_edge (f2->dest);
1305
1306       /* To simplify use of this function, return false if there are
1307          unneeded forwarder blocks.  These will get eliminated later
1308          during cleanup_cfg.  */
1309       if (FORWARDER_BLOCK_P (f1->dest)
1310           || FORWARDER_BLOCK_P (f2->dest)
1311           || FORWARDER_BLOCK_P (b1->dest)
1312           || FORWARDER_BLOCK_P (b2->dest))
1313         return false;
1314
1315       if (f1->dest == f2->dest && b1->dest == b2->dest)
1316         reverse = false;
1317       else if (f1->dest == b2->dest && b1->dest == f2->dest)
1318         reverse = true;
1319       else
1320         return false;
1321
1322       set1 = pc_set (BB_END (bb1));
1323       set2 = pc_set (BB_END (bb2));
1324       if ((XEXP (SET_SRC (set1), 1) == pc_rtx)
1325           != (XEXP (SET_SRC (set2), 1) == pc_rtx))
1326         reverse = !reverse;
1327
1328       cond1 = XEXP (SET_SRC (set1), 0);
1329       cond2 = XEXP (SET_SRC (set2), 0);
1330       code1 = GET_CODE (cond1);
1331       if (reverse)
1332         code2 = reversed_comparison_code (cond2, BB_END (bb2));
1333       else
1334         code2 = GET_CODE (cond2);
1335
1336       if (code2 == UNKNOWN)
1337         return false;
1338
1339       /* Verify codes and operands match.  */
1340       match = ((code1 == code2
1341                 && rtx_renumbered_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
1342                 && rtx_renumbered_equal_p (XEXP (cond1, 1), XEXP (cond2, 1)))
1343                || (code1 == swap_condition (code2)
1344                    && rtx_renumbered_equal_p (XEXP (cond1, 1),
1345                                               XEXP (cond2, 0))
1346                    && rtx_renumbered_equal_p (XEXP (cond1, 0),
1347                                               XEXP (cond2, 1))));
1348
1349       /* If we return true, we will join the blocks.  Which means that
1350          we will only have one branch prediction bit to work with.  Thus
1351          we require the existing branches to have probabilities that are
1352          roughly similar.  */
1353       if (match
1354           && optimize_bb_for_speed_p (bb1)
1355           && optimize_bb_for_speed_p (bb2))
1356         {
1357           int prob2;
1358
1359           if (b1->dest == b2->dest)
1360             prob2 = b2->probability;
1361           else
1362             /* Do not use f2 probability as f2 may be forwarded.  */
1363             prob2 = REG_BR_PROB_BASE - b2->probability;
1364
1365           /* Fail if the difference in probabilities is greater than 50%.
1366              This rules out two well-predicted branches with opposite
1367              outcomes.  */
1368           if (abs (b1->probability - prob2) > REG_BR_PROB_BASE / 2)
1369             {
1370               if (dump_file)
1371                 fprintf (dump_file,
1372                          "Outcomes of branch in bb %i and %i differ too much (%i %i)\n",
1373                          bb1->index, bb2->index, b1->probability, prob2);
1374
1375               return false;
1376             }
1377         }
1378
1379       if (dump_file && match)
1380         fprintf (dump_file, "Conditionals in bb %i and %i match.\n",
1381                  bb1->index, bb2->index);
1382
1383       return match;
1384     }
1385
1386   /* Generic case - we are seeing a computed jump, table jump or trapping
1387      instruction.  */
1388
1389   /* Check whether there are tablejumps in the end of BB1 and BB2.
1390      Return true if they are identical.  */
1391     {
1392       rtx label1, label2;
1393       rtx table1, table2;
1394
1395       if (tablejump_p (BB_END (bb1), &label1, &table1)
1396           && tablejump_p (BB_END (bb2), &label2, &table2)
1397           && GET_CODE (PATTERN (table1)) == GET_CODE (PATTERN (table2)))
1398         {
1399           /* The labels should never be the same rtx.  If they really are same
1400              the jump tables are same too. So disable crossjumping of blocks BB1
1401              and BB2 because when deleting the common insns in the end of BB1
1402              by delete_basic_block () the jump table would be deleted too.  */
1403           /* If LABEL2 is referenced in BB1->END do not do anything
1404              because we would loose information when replacing
1405              LABEL1 by LABEL2 and then LABEL2 by LABEL1 in BB1->END.  */
1406           if (label1 != label2 && !rtx_referenced_p (label2, BB_END (bb1)))
1407             {
1408               /* Set IDENTICAL to true when the tables are identical.  */
1409               bool identical = false;
1410               rtx p1, p2;
1411
1412               p1 = PATTERN (table1);
1413               p2 = PATTERN (table2);
1414               if (GET_CODE (p1) == ADDR_VEC && rtx_equal_p (p1, p2))
1415                 {
1416                   identical = true;
1417                 }
1418               else if (GET_CODE (p1) == ADDR_DIFF_VEC
1419                        && (XVECLEN (p1, 1) == XVECLEN (p2, 1))
1420                        && rtx_equal_p (XEXP (p1, 2), XEXP (p2, 2))
1421                        && rtx_equal_p (XEXP (p1, 3), XEXP (p2, 3)))
1422                 {
1423                   int i;
1424
1425                   identical = true;
1426                   for (i = XVECLEN (p1, 1) - 1; i >= 0 && identical; i--)
1427                     if (!rtx_equal_p (XVECEXP (p1, 1, i), XVECEXP (p2, 1, i)))
1428                       identical = false;
1429                 }
1430
1431               if (identical)
1432                 {
1433                   replace_label_data rr;
1434                   bool match;
1435
1436                   /* Temporarily replace references to LABEL1 with LABEL2
1437                      in BB1->END so that we could compare the instructions.  */
1438                   rr.r1 = label1;
1439                   rr.r2 = label2;
1440                   rr.update_label_nuses = false;
1441                   for_each_rtx (&BB_END (bb1), replace_label, &rr);
1442
1443                   match = old_insns_match_p (mode, BB_END (bb1), BB_END (bb2));
1444                   if (dump_file && match)
1445                     fprintf (dump_file,
1446                              "Tablejumps in bb %i and %i match.\n",
1447                              bb1->index, bb2->index);
1448
1449                   /* Set the original label in BB1->END because when deleting
1450                      a block whose end is a tablejump, the tablejump referenced
1451                      from the instruction is deleted too.  */
1452                   rr.r1 = label2;
1453                   rr.r2 = label1;
1454                   for_each_rtx (&BB_END (bb1), replace_label, &rr);
1455
1456                   return match;
1457                 }
1458             }
1459           return false;
1460         }
1461     }
1462
1463   /* First ensure that the instructions match.  There may be many outgoing
1464      edges so this test is generally cheaper.  */
1465   if (!old_insns_match_p (mode, BB_END (bb1), BB_END (bb2)))
1466     return false;
1467
1468   /* Search the outgoing edges, ensure that the counts do match, find possible
1469      fallthru and exception handling edges since these needs more
1470      validation.  */
1471   if (EDGE_COUNT (bb1->succs) != EDGE_COUNT (bb2->succs))
1472     return false;
1473
1474   FOR_EACH_EDGE (e1, ei, bb1->succs)
1475     {
1476       e2 = EDGE_SUCC (bb2, ei.index);
1477
1478       if (e1->flags & EDGE_EH)
1479         nehedges1++;
1480
1481       if (e2->flags & EDGE_EH)
1482         nehedges2++;
1483
1484       if (e1->flags & EDGE_FALLTHRU)
1485         fallthru1 = e1;
1486       if (e2->flags & EDGE_FALLTHRU)
1487         fallthru2 = e2;
1488     }
1489
1490   /* If number of edges of various types does not match, fail.  */
1491   if (nehedges1 != nehedges2
1492       || (fallthru1 != 0) != (fallthru2 != 0))
1493     return false;
1494
1495   /* fallthru edges must be forwarded to the same destination.  */
1496   if (fallthru1)
1497     {
1498       basic_block d1 = (forwarder_block_p (fallthru1->dest)
1499                         ? single_succ (fallthru1->dest): fallthru1->dest);
1500       basic_block d2 = (forwarder_block_p (fallthru2->dest)
1501                         ? single_succ (fallthru2->dest): fallthru2->dest);
1502
1503       if (d1 != d2)
1504         return false;
1505     }
1506
1507   /* Ensure the same EH region.  */
1508   {
1509     rtx n1 = find_reg_note (BB_END (bb1), REG_EH_REGION, 0);
1510     rtx n2 = find_reg_note (BB_END (bb2), REG_EH_REGION, 0);
1511
1512     if (!n1 && n2)
1513       return false;
1514
1515     if (n1 && (!n2 || XEXP (n1, 0) != XEXP (n2, 0)))
1516       return false;
1517   }
1518
1519   /* The same checks as in try_crossjump_to_edge. It is required for RTL
1520      version of sequence abstraction.  */
1521   FOR_EACH_EDGE (e1, ei, bb2->succs)
1522     {
1523       edge e2;
1524       edge_iterator ei;
1525       basic_block d1 = e1->dest;
1526
1527       if (FORWARDER_BLOCK_P (d1))
1528         d1 = EDGE_SUCC (d1, 0)->dest;
1529
1530       FOR_EACH_EDGE (e2, ei, bb1->succs)
1531         {
1532           basic_block d2 = e2->dest;
1533           if (FORWARDER_BLOCK_P (d2))
1534             d2 = EDGE_SUCC (d2, 0)->dest;
1535           if (d1 == d2)
1536             break;
1537         }
1538
1539       if (!e2)
1540         return false;
1541     }
1542
1543   return true;
1544 }
1545
1546 /* Returns true if BB basic block has a preserve label.  */
1547
1548 static bool
1549 block_has_preserve_label (basic_block bb)
1550 {
1551   return (bb
1552           && block_label (bb)
1553           && LABEL_PRESERVE_P (block_label (bb)));
1554 }
1555
1556 /* E1 and E2 are edges with the same destination block.  Search their
1557    predecessors for common code.  If found, redirect control flow from
1558    (maybe the middle of) E1->SRC to (maybe the middle of) E2->SRC.  */
1559
1560 static bool
1561 try_crossjump_to_edge (int mode, edge e1, edge e2)
1562 {
1563   int nmatch;
1564   basic_block src1 = e1->src, src2 = e2->src;
1565   basic_block redirect_to, redirect_from, to_remove;
1566   rtx newpos1, newpos2;
1567   edge s;
1568   edge_iterator ei;
1569
1570   newpos1 = newpos2 = NULL_RTX;
1571
1572   /* If we have partitioned hot/cold basic blocks, it is a bad idea
1573      to try this optimization.
1574
1575      Basic block partitioning may result in some jumps that appear to
1576      be optimizable (or blocks that appear to be mergeable), but which really
1577      must be left untouched (they are required to make it safely across
1578      partition boundaries).  See the comments at the top of
1579      bb-reorder.c:partition_hot_cold_basic_blocks for complete details.  */
1580
1581   if (flag_reorder_blocks_and_partition && reload_completed)
1582     return false;
1583
1584   /* Search backward through forwarder blocks.  We don't need to worry
1585      about multiple entry or chained forwarders, as they will be optimized
1586      away.  We do this to look past the unconditional jump following a
1587      conditional jump that is required due to the current CFG shape.  */
1588   if (single_pred_p (src1)
1589       && FORWARDER_BLOCK_P (src1))
1590     e1 = single_pred_edge (src1), src1 = e1->src;
1591
1592   if (single_pred_p (src2)
1593       && FORWARDER_BLOCK_P (src2))
1594     e2 = single_pred_edge (src2), src2 = e2->src;
1595
1596   /* Nothing to do if we reach ENTRY, or a common source block.  */
1597   if (src1 == ENTRY_BLOCK_PTR || src2 == ENTRY_BLOCK_PTR)
1598     return false;
1599   if (src1 == src2)
1600     return false;
1601
1602   /* Seeing more than 1 forwarder blocks would confuse us later...  */
1603   if (FORWARDER_BLOCK_P (e1->dest)
1604       && FORWARDER_BLOCK_P (single_succ (e1->dest)))
1605     return false;
1606
1607   if (FORWARDER_BLOCK_P (e2->dest)
1608       && FORWARDER_BLOCK_P (single_succ (e2->dest)))
1609     return false;
1610
1611   /* Likewise with dead code (possibly newly created by the other optimizations
1612      of cfg_cleanup).  */
1613   if (EDGE_COUNT (src1->preds) == 0 || EDGE_COUNT (src2->preds) == 0)
1614     return false;
1615
1616   /* Look for the common insn sequence, part the first ...  */
1617   if (!outgoing_edges_match (mode, src1, src2))
1618     return false;
1619
1620   /* ... and part the second.  */
1621   nmatch = flow_find_cross_jump (src1, src2, &newpos1, &newpos2);
1622
1623   /* Don't proceed with the crossjump unless we found a sufficient number
1624      of matching instructions or the 'from' block was totally matched
1625      (such that its predecessors will hopefully be redirected and the
1626      block removed).  */
1627   if ((nmatch < PARAM_VALUE (PARAM_MIN_CROSSJUMP_INSNS))
1628       && (newpos1 != BB_HEAD (src1)))
1629     return false;
1630
1631   /* Avoid deleting preserve label when redirecting ABNORMAL edges.  */
1632   if (block_has_preserve_label (e1->dest)
1633       && (e1->flags & EDGE_ABNORMAL))
1634     return false;
1635
1636   /* Here we know that the insns in the end of SRC1 which are common with SRC2
1637      will be deleted.
1638      If we have tablejumps in the end of SRC1 and SRC2
1639      they have been already compared for equivalence in outgoing_edges_match ()
1640      so replace the references to TABLE1 by references to TABLE2.  */
1641     {
1642       rtx label1, label2;
1643       rtx table1, table2;
1644
1645       if (tablejump_p (BB_END (src1), &label1, &table1)
1646           && tablejump_p (BB_END (src2), &label2, &table2)
1647           && label1 != label2)
1648         {
1649           replace_label_data rr;
1650           rtx insn;
1651
1652           /* Replace references to LABEL1 with LABEL2.  */
1653           rr.r1 = label1;
1654           rr.r2 = label2;
1655           rr.update_label_nuses = true;
1656           for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1657             {
1658               /* Do not replace the label in SRC1->END because when deleting
1659                  a block whose end is a tablejump, the tablejump referenced
1660                  from the instruction is deleted too.  */
1661               if (insn != BB_END (src1))
1662                 for_each_rtx (&insn, replace_label, &rr);
1663             }
1664         }
1665     }
1666
1667   /* Avoid splitting if possible.  We must always split when SRC2 has
1668      EH predecessor edges, or we may end up with basic blocks with both
1669      normal and EH predecessor edges.  */
1670   if (newpos2 == BB_HEAD (src2)
1671       && !(EDGE_PRED (src2, 0)->flags & EDGE_EH))
1672     redirect_to = src2;
1673   else
1674     {
1675       if (newpos2 == BB_HEAD (src2))
1676         {
1677           /* Skip possible basic block header.  */
1678           if (LABEL_P (newpos2))
1679             newpos2 = NEXT_INSN (newpos2);
1680           while (DEBUG_INSN_P (newpos2))
1681             newpos2 = NEXT_INSN (newpos2);
1682           if (NOTE_P (newpos2))
1683             newpos2 = NEXT_INSN (newpos2);
1684           while (DEBUG_INSN_P (newpos2))
1685             newpos2 = NEXT_INSN (newpos2);
1686         }
1687
1688       if (dump_file)
1689         fprintf (dump_file, "Splitting bb %i before %i insns\n",
1690                  src2->index, nmatch);
1691       redirect_to = split_block (src2, PREV_INSN (newpos2))->dest;
1692     }
1693
1694   if (dump_file)
1695     fprintf (dump_file,
1696              "Cross jumping from bb %i to bb %i; %i common insns\n",
1697              src1->index, src2->index, nmatch);
1698
1699   /* We may have some registers visible through the block.  */
1700   df_set_bb_dirty (redirect_to);
1701
1702   /* Recompute the frequencies and counts of outgoing edges.  */
1703   FOR_EACH_EDGE (s, ei, redirect_to->succs)
1704     {
1705       edge s2;
1706       edge_iterator ei;
1707       basic_block d = s->dest;
1708
1709       if (FORWARDER_BLOCK_P (d))
1710         d = single_succ (d);
1711
1712       FOR_EACH_EDGE (s2, ei, src1->succs)
1713         {
1714           basic_block d2 = s2->dest;
1715           if (FORWARDER_BLOCK_P (d2))
1716             d2 = single_succ (d2);
1717           if (d == d2)
1718             break;
1719         }
1720
1721       s->count += s2->count;
1722
1723       /* Take care to update possible forwarder blocks.  We verified
1724          that there is no more than one in the chain, so we can't run
1725          into infinite loop.  */
1726       if (FORWARDER_BLOCK_P (s->dest))
1727         {
1728           single_succ_edge (s->dest)->count += s2->count;
1729           s->dest->count += s2->count;
1730           s->dest->frequency += EDGE_FREQUENCY (s);
1731         }
1732
1733       if (FORWARDER_BLOCK_P (s2->dest))
1734         {
1735           single_succ_edge (s2->dest)->count -= s2->count;
1736           if (single_succ_edge (s2->dest)->count < 0)
1737             single_succ_edge (s2->dest)->count = 0;
1738           s2->dest->count -= s2->count;
1739           s2->dest->frequency -= EDGE_FREQUENCY (s);
1740           if (s2->dest->frequency < 0)
1741             s2->dest->frequency = 0;
1742           if (s2->dest->count < 0)
1743             s2->dest->count = 0;
1744         }
1745
1746       if (!redirect_to->frequency && !src1->frequency)
1747         s->probability = (s->probability + s2->probability) / 2;
1748       else
1749         s->probability
1750           = ((s->probability * redirect_to->frequency +
1751               s2->probability * src1->frequency)
1752              / (redirect_to->frequency + src1->frequency));
1753     }
1754
1755   /* Adjust count and frequency for the block.  An earlier jump
1756      threading pass may have left the profile in an inconsistent
1757      state (see update_bb_profile_for_threading) so we must be
1758      prepared for overflows.  */
1759   redirect_to->count += src1->count;
1760   redirect_to->frequency += src1->frequency;
1761   if (redirect_to->frequency > BB_FREQ_MAX)
1762     redirect_to->frequency = BB_FREQ_MAX;
1763   update_br_prob_note (redirect_to);
1764
1765   /* Edit SRC1 to go to REDIRECT_TO at NEWPOS1.  */
1766
1767   /* Skip possible basic block header.  */
1768   if (LABEL_P (newpos1))
1769     newpos1 = NEXT_INSN (newpos1);
1770
1771   while (DEBUG_INSN_P (newpos1))
1772     newpos1 = NEXT_INSN (newpos1);
1773
1774   if (NOTE_INSN_BASIC_BLOCK_P (newpos1))
1775     newpos1 = NEXT_INSN (newpos1);
1776
1777   while (DEBUG_INSN_P (newpos1))
1778     newpos1 = NEXT_INSN (newpos1);
1779
1780   redirect_from = split_block (src1, PREV_INSN (newpos1))->src;
1781   to_remove = single_succ (redirect_from);
1782
1783   redirect_edge_and_branch_force (single_succ_edge (redirect_from), redirect_to);
1784   delete_basic_block (to_remove);
1785
1786   update_forwarder_flag (redirect_from);
1787   if (redirect_to != src2)
1788     update_forwarder_flag (src2);
1789
1790   return true;
1791 }
1792
1793 /* Search the predecessors of BB for common insn sequences.  When found,
1794    share code between them by redirecting control flow.  Return true if
1795    any changes made.  */
1796
1797 static bool
1798 try_crossjump_bb (int mode, basic_block bb)
1799 {
1800   edge e, e2, fallthru;
1801   bool changed;
1802   unsigned max, ix, ix2;
1803   basic_block ev, ev2;
1804   edge_iterator ei;
1805
1806   /* Nothing to do if there is not at least two incoming edges.  */
1807   if (EDGE_COUNT (bb->preds) < 2)
1808     return false;
1809
1810   /* Don't crossjump if this block ends in a computed jump,
1811      unless we are optimizing for size.  */
1812   if (optimize_bb_for_size_p (bb)
1813       && bb != EXIT_BLOCK_PTR
1814       && computed_jump_p (BB_END (bb)))
1815     return false;
1816
1817   /* If we are partitioning hot/cold basic blocks, we don't want to
1818      mess up unconditional or indirect jumps that cross between hot
1819      and cold sections.
1820
1821      Basic block partitioning may result in some jumps that appear to
1822      be optimizable (or blocks that appear to be mergeable), but which really
1823      must be left untouched (they are required to make it safely across
1824      partition boundaries).  See the comments at the top of
1825      bb-reorder.c:partition_hot_cold_basic_blocks for complete details.  */
1826
1827   if (BB_PARTITION (EDGE_PRED (bb, 0)->src) !=
1828                                         BB_PARTITION (EDGE_PRED (bb, 1)->src)
1829       || (EDGE_PRED (bb, 0)->flags & EDGE_CROSSING))
1830     return false;
1831
1832   /* It is always cheapest to redirect a block that ends in a branch to
1833      a block that falls through into BB, as that adds no branches to the
1834      program.  We'll try that combination first.  */
1835   fallthru = NULL;
1836   max = PARAM_VALUE (PARAM_MAX_CROSSJUMP_EDGES);
1837
1838   if (EDGE_COUNT (bb->preds) > max)
1839     return false;
1840
1841   FOR_EACH_EDGE (e, ei, bb->preds)
1842     {
1843       if (e->flags & EDGE_FALLTHRU)
1844         {
1845           fallthru = e;
1846           break;
1847         }
1848     }
1849
1850   changed = false;
1851   for (ix = 0, ev = bb; ix < EDGE_COUNT (ev->preds); )
1852     {
1853       e = EDGE_PRED (ev, ix);
1854       ix++;
1855
1856       /* As noted above, first try with the fallthru predecessor (or, a
1857          fallthru predecessor if we are in cfglayout mode).  */
1858       if (fallthru)
1859         {
1860           /* Don't combine the fallthru edge into anything else.
1861              If there is a match, we'll do it the other way around.  */
1862           if (e == fallthru)
1863             continue;
1864           /* If nothing changed since the last attempt, there is nothing
1865              we can do.  */
1866           if (!first_pass
1867               && !((e->src->flags & BB_MODIFIED)
1868                    || (fallthru->src->flags & BB_MODIFIED)))
1869             continue;
1870
1871           if (try_crossjump_to_edge (mode, e, fallthru))
1872             {
1873               changed = true;
1874               ix = 0;
1875               ev = bb;
1876               continue;
1877             }
1878         }
1879
1880       /* Non-obvious work limiting check: Recognize that we're going
1881          to call try_crossjump_bb on every basic block.  So if we have
1882          two blocks with lots of outgoing edges (a switch) and they
1883          share lots of common destinations, then we would do the
1884          cross-jump check once for each common destination.
1885
1886          Now, if the blocks actually are cross-jump candidates, then
1887          all of their destinations will be shared.  Which means that
1888          we only need check them for cross-jump candidacy once.  We
1889          can eliminate redundant checks of crossjump(A,B) by arbitrarily
1890          choosing to do the check from the block for which the edge
1891          in question is the first successor of A.  */
1892       if (EDGE_SUCC (e->src, 0) != e)
1893         continue;
1894
1895       for (ix2 = 0, ev2 = bb; ix2 < EDGE_COUNT (ev2->preds); )
1896         {
1897           e2 = EDGE_PRED (ev2, ix2);
1898           ix2++;
1899
1900           if (e2 == e)
1901             continue;
1902
1903           /* We've already checked the fallthru edge above.  */
1904           if (e2 == fallthru)
1905             continue;
1906
1907           /* The "first successor" check above only prevents multiple
1908              checks of crossjump(A,B).  In order to prevent redundant
1909              checks of crossjump(B,A), require that A be the block
1910              with the lowest index.  */
1911           if (e->src->index > e2->src->index)
1912             continue;
1913
1914           /* If nothing changed since the last attempt, there is nothing
1915              we can do.  */
1916           if (!first_pass
1917               && !((e->src->flags & BB_MODIFIED)
1918                    || (e2->src->flags & BB_MODIFIED)))
1919             continue;
1920
1921           if (try_crossjump_to_edge (mode, e, e2))
1922             {
1923               changed = true;
1924               ev2 = bb;
1925               ix = 0;
1926               break;
1927             }
1928         }
1929     }
1930
1931   if (changed)
1932     crossjumps_occured = true;
1933
1934   return changed;
1935 }
1936
1937 /* Search the successors of BB for common insn sequences.  When found,
1938    share code between them by moving it across the basic block
1939    boundary.  Return true if any changes made.  */
1940
1941 static bool
1942 try_head_merge_bb (basic_block bb)
1943 {
1944   basic_block final_dest_bb = NULL;
1945   int max_match = INT_MAX;
1946   edge e0;
1947   rtx *headptr, *currptr;
1948   bool changed, moveall;
1949   unsigned ix;
1950   rtx e0_last_head, cond, move_before;
1951   unsigned nedges = EDGE_COUNT (bb->succs);
1952   rtx jump = BB_END (bb);
1953   regset live, live_union;
1954
1955   /* Nothing to do if there is not at least two outgoing edges.  */
1956   if (nedges < 2)
1957     return false;
1958
1959   /* Don't crossjump if this block ends in a computed jump,
1960      unless we are optimizing for size.  */
1961   if (optimize_bb_for_size_p (bb)
1962       && bb != EXIT_BLOCK_PTR
1963       && computed_jump_p (BB_END (bb)))
1964     return false;
1965
1966   cond = get_condition (jump, &move_before, true, false);
1967   if (cond == NULL_RTX)
1968     move_before = jump;
1969
1970   for (ix = 0; ix < nedges; ix++)
1971     if (EDGE_SUCC (bb, ix)->dest == EXIT_BLOCK_PTR)
1972       return false;
1973
1974   for (ix = 0; ix < nedges; ix++)
1975     {
1976       edge e = EDGE_SUCC (bb, ix);
1977       basic_block other_bb = e->dest;
1978
1979       if (df_get_bb_dirty (other_bb))
1980         {
1981           block_was_dirty = true;
1982           return false;
1983         }
1984
1985       if (e->flags & EDGE_ABNORMAL)
1986         return false;
1987
1988       /* Normally, all destination blocks must only be reachable from this
1989          block, i.e. they must have one incoming edge.
1990
1991          There is one special case we can handle, that of multiple consecutive
1992          jumps where the first jumps to one of the targets of the second jump.
1993          This happens frequently in switch statements for default labels.
1994          The structure is as follows:
1995          FINAL_DEST_BB
1996          ....
1997          if (cond) jump A;
1998          fall through
1999          BB
2000          jump with targets A, B, C, D...
2001          A
2002          has two incoming edges, from FINAL_DEST_BB and BB
2003
2004          In this case, we can try to move the insns through BB and into
2005          FINAL_DEST_BB.  */
2006       if (EDGE_COUNT (other_bb->preds) != 1)
2007         {
2008           edge incoming_edge, incoming_bb_other_edge;
2009           edge_iterator ei;
2010
2011           if (final_dest_bb != NULL
2012               || EDGE_COUNT (other_bb->preds) != 2)
2013             return false;
2014
2015           /* We must be able to move the insns across the whole block.  */
2016           move_before = BB_HEAD (bb);
2017           while (!NONDEBUG_INSN_P (move_before))
2018             move_before = NEXT_INSN (move_before);
2019
2020           if (EDGE_COUNT (bb->preds) != 1)
2021             return false;
2022           incoming_edge = EDGE_PRED (bb, 0);
2023           final_dest_bb = incoming_edge->src;
2024           if (EDGE_COUNT (final_dest_bb->succs) != 2)
2025             return false;
2026           FOR_EACH_EDGE (incoming_bb_other_edge, ei, final_dest_bb->succs)
2027             if (incoming_bb_other_edge != incoming_edge)
2028               break;
2029           if (incoming_bb_other_edge->dest != other_bb)
2030             return false;
2031         }
2032     }
2033
2034   e0 = EDGE_SUCC (bb, 0);
2035   e0_last_head = NULL_RTX;
2036   changed = false;
2037
2038   for (ix = 1; ix < nedges; ix++)
2039     {
2040       edge e = EDGE_SUCC (bb, ix);
2041       rtx e0_last, e_last;
2042       int nmatch;
2043
2044       nmatch = flow_find_head_matching_sequence (e0->dest, e->dest,
2045                                                  &e0_last, &e_last, 0);
2046       if (nmatch == 0)
2047         return false;
2048
2049       if (nmatch < max_match)
2050         {
2051           max_match = nmatch;
2052           e0_last_head = e0_last;
2053         }
2054     }
2055
2056   /* If we matched an entire block, we probably have to avoid moving the
2057      last insn.  */
2058   if (max_match > 0
2059       && e0_last_head == BB_END (e0->dest)
2060       && (find_reg_note (e0_last_head, REG_EH_REGION, 0)
2061           || control_flow_insn_p (e0_last_head)))
2062     {
2063       max_match--;
2064       if (max_match == 0)
2065         return false;
2066       do
2067         e0_last_head = prev_real_insn (e0_last_head);
2068       while (DEBUG_INSN_P (e0_last_head));
2069     }
2070
2071   if (max_match == 0)
2072     return false;
2073
2074   /* We must find a union of the live registers at each of the end points.  */
2075   live = BITMAP_ALLOC (NULL);
2076   live_union = BITMAP_ALLOC (NULL);
2077
2078   currptr = XNEWVEC (rtx, nedges);
2079   headptr = XNEWVEC (rtx, nedges);
2080
2081   for (ix = 0; ix < nedges; ix++)
2082     {
2083       int j;
2084       basic_block merge_bb = EDGE_SUCC (bb, ix)->dest;
2085       rtx head = BB_HEAD (merge_bb);
2086
2087       while (!NONDEBUG_INSN_P (head))
2088         head = NEXT_INSN (head);
2089       headptr[ix] = head;
2090       currptr[ix] = head;
2091
2092       /* Compute the end point and live information  */
2093       for (j = 1; j < max_match; j++)
2094         do
2095           head = NEXT_INSN (head);
2096         while (!NONDEBUG_INSN_P (head));
2097       simulate_backwards_to_point (merge_bb, live, head);
2098       IOR_REG_SET (live_union, live);
2099     }
2100
2101   /* If we're moving across two blocks, verify the validity of the
2102      first move, then adjust the target and let the loop below deal
2103      with the final move.  */
2104   if (final_dest_bb != NULL)
2105     {
2106       rtx move_upto;
2107
2108       moveall = can_move_insns_across (currptr[0], e0_last_head, move_before,
2109                                        jump, e0->dest, live_union,
2110                                        NULL, &move_upto);
2111       if (!moveall)
2112         e0_last_head = move_upto;
2113       if (e0_last_head == NULL_RTX)
2114         goto out;
2115
2116       jump = BB_END (final_dest_bb);
2117       cond = get_condition (jump, &move_before, true, false);
2118       if (cond == NULL_RTX)
2119         move_before = jump;
2120     }
2121
2122   do
2123     {
2124       rtx move_upto;
2125       moveall = can_move_insns_across (currptr[0], e0_last_head,
2126                                        move_before, jump, e0->dest, live_union,
2127                                        NULL, &move_upto);
2128       if (!moveall && move_upto == NULL_RTX)
2129         {
2130           if (jump == move_before)
2131             break;
2132
2133           /* Try again, using a different insertion point.  */
2134           move_before = jump;
2135           continue;
2136         }
2137
2138       if (final_dest_bb && !moveall)
2139         /* We haven't checked whether a partial move would be OK for the first
2140            move, so we have to fail this case.  */
2141         break;
2142
2143       changed = true;
2144       for (;;)
2145         {
2146           if (currptr[0] == move_upto)
2147             break;
2148           for (ix = 0; ix < nedges; ix++)
2149             {
2150               rtx curr = currptr[ix];
2151               do
2152                 curr = NEXT_INSN (curr);
2153               while (!NONDEBUG_INSN_P (curr));
2154               currptr[ix] = curr;
2155             }
2156         }
2157
2158       reorder_insns (headptr[0], currptr[0], PREV_INSN (move_before));
2159       df_set_bb_dirty (EDGE_SUCC (bb, 0)->dest);
2160       if (final_dest_bb != NULL)
2161         df_set_bb_dirty (final_dest_bb);
2162       df_set_bb_dirty (bb);
2163       for (ix = 1; ix < nedges; ix++)
2164         {
2165           df_set_bb_dirty (EDGE_SUCC (bb, ix)->dest);
2166           delete_insn_chain (headptr[ix], currptr[ix], false);
2167         }
2168       if (!moveall)
2169         {
2170           if (jump == move_before)
2171             break;
2172
2173           /* Try again, using a different insertion point.  */
2174           move_before = jump;
2175           for (ix = 0; ix < nedges; ix++)
2176             {
2177               rtx curr = currptr[ix];
2178               do
2179                 curr = NEXT_INSN (curr);
2180               while (!NONDEBUG_INSN_P (curr));
2181               currptr[ix] = headptr[ix] = curr;
2182             }
2183         }
2184     }
2185   while (!moveall);
2186
2187  out:
2188   free (currptr);
2189   free (headptr);
2190
2191   crossjumps_occured |= changed;
2192
2193   return changed;
2194 }
2195
2196 /* Return true if BB contains just bb note, or bb note followed
2197    by only DEBUG_INSNs.  */
2198
2199 static bool
2200 trivially_empty_bb_p (basic_block bb)
2201 {
2202   rtx insn = BB_END (bb);
2203
2204   while (1)
2205     {
2206       if (insn == BB_HEAD (bb))
2207         return true;
2208       if (!DEBUG_INSN_P (insn))
2209         return false;
2210       insn = PREV_INSN (insn);
2211     }
2212 }
2213
2214 /* Do simple CFG optimizations - basic block merging, simplifying of jump
2215    instructions etc.  Return nonzero if changes were made.  */
2216
2217 static bool
2218 try_optimize_cfg (int mode)
2219 {
2220   bool changed_overall = false;
2221   bool changed;
2222   int iterations = 0;
2223   basic_block bb, b, next;
2224
2225   if (mode & (CLEANUP_CROSSJUMP | CLEANUP_THREADING))
2226     clear_bb_flags ();
2227
2228   crossjumps_occured = false;
2229
2230   FOR_EACH_BB (bb)
2231     update_forwarder_flag (bb);
2232
2233   if (! targetm.cannot_modify_jumps_p ())
2234     {
2235       first_pass = true;
2236       /* Attempt to merge blocks as made possible by edge removal.  If
2237          a block has only one successor, and the successor has only
2238          one predecessor, they may be combined.  */
2239       do
2240         {
2241           block_was_dirty = false;
2242           changed = false;
2243           iterations++;
2244
2245           if (dump_file)
2246             fprintf (dump_file,
2247                      "\n\ntry_optimize_cfg iteration %i\n\n",
2248                      iterations);
2249
2250           for (b = ENTRY_BLOCK_PTR->next_bb; b != EXIT_BLOCK_PTR;)
2251             {
2252               basic_block c;
2253               edge s;
2254               bool changed_here = false;
2255
2256               /* Delete trivially dead basic blocks.  This is either
2257                  blocks with no predecessors, or empty blocks with no
2258                  successors.  However if the empty block with no
2259                  successors is the successor of the ENTRY_BLOCK, it is
2260                  kept.  This ensures that the ENTRY_BLOCK will have a
2261                  successor which is a precondition for many RTL
2262                  passes.  Empty blocks may result from expanding
2263                  __builtin_unreachable ().  */
2264               if (EDGE_COUNT (b->preds) == 0
2265                   || (EDGE_COUNT (b->succs) == 0
2266                       && trivially_empty_bb_p (b)
2267                       && single_succ_edge (ENTRY_BLOCK_PTR)->dest != b))
2268                 {
2269                   c = b->prev_bb;
2270                   if (EDGE_COUNT (b->preds) > 0)
2271                     {
2272                       edge e;
2273                       edge_iterator ei;
2274
2275                       if (current_ir_type () == IR_RTL_CFGLAYOUT)
2276                         {
2277                           if (b->il.rtl->footer
2278                               && BARRIER_P (b->il.rtl->footer))
2279                             FOR_EACH_EDGE (e, ei, b->preds)
2280                               if ((e->flags & EDGE_FALLTHRU)
2281                                   && e->src->il.rtl->footer == NULL)
2282                                 {
2283                                   if (b->il.rtl->footer)
2284                                     {
2285                                       e->src->il.rtl->footer = b->il.rtl->footer;
2286                                       b->il.rtl->footer = NULL;
2287                                     }
2288                                   else
2289                                     {
2290                                       start_sequence ();
2291                                       e->src->il.rtl->footer = emit_barrier ();
2292                                       end_sequence ();
2293                                     }
2294                                 }
2295                         }
2296                       else
2297                         {
2298                           rtx last = get_last_bb_insn (b);
2299                           if (last && BARRIER_P (last))
2300                             FOR_EACH_EDGE (e, ei, b->preds)
2301                               if ((e->flags & EDGE_FALLTHRU))
2302                                 emit_barrier_after (BB_END (e->src));
2303                         }
2304                     }
2305                   delete_basic_block (b);
2306                   if (!(mode & CLEANUP_CFGLAYOUT))
2307                     changed = true;
2308                   /* Avoid trying to remove ENTRY_BLOCK_PTR.  */
2309                   b = (c == ENTRY_BLOCK_PTR ? c->next_bb : c);
2310                   continue;
2311                 }
2312
2313               /* Remove code labels no longer used.  */
2314               if (single_pred_p (b)
2315                   && (single_pred_edge (b)->flags & EDGE_FALLTHRU)
2316                   && !(single_pred_edge (b)->flags & EDGE_COMPLEX)
2317                   && LABEL_P (BB_HEAD (b))
2318                   /* If the previous block ends with a branch to this
2319                      block, we can't delete the label.  Normally this
2320                      is a condjump that is yet to be simplified, but
2321                      if CASE_DROPS_THRU, this can be a tablejump with
2322                      some element going to the same place as the
2323                      default (fallthru).  */
2324                   && (single_pred (b) == ENTRY_BLOCK_PTR
2325                       || !JUMP_P (BB_END (single_pred (b)))
2326                       || ! label_is_jump_target_p (BB_HEAD (b),
2327                                                    BB_END (single_pred (b)))))
2328                 {
2329                   rtx label = BB_HEAD (b);
2330
2331                   delete_insn_chain (label, label, false);
2332                   /* If the case label is undeletable, move it after the
2333                      BASIC_BLOCK note.  */
2334                   if (NOTE_KIND (BB_HEAD (b)) == NOTE_INSN_DELETED_LABEL)
2335                     {
2336                       rtx bb_note = NEXT_INSN (BB_HEAD (b));
2337
2338                       reorder_insns_nobb (label, label, bb_note);
2339                       BB_HEAD (b) = bb_note;
2340                       if (BB_END (b) == bb_note)
2341                         BB_END (b) = label;
2342                     }
2343                   if (dump_file)
2344                     fprintf (dump_file, "Deleted label in block %i.\n",
2345                              b->index);
2346                 }
2347
2348               /* If we fall through an empty block, we can remove it.  */
2349               if (!(mode & CLEANUP_CFGLAYOUT)
2350                   && single_pred_p (b)
2351                   && (single_pred_edge (b)->flags & EDGE_FALLTHRU)
2352                   && !LABEL_P (BB_HEAD (b))
2353                   && FORWARDER_BLOCK_P (b)
2354                   /* Note that forwarder_block_p true ensures that
2355                      there is a successor for this block.  */
2356                   && (single_succ_edge (b)->flags & EDGE_FALLTHRU)
2357                   && n_basic_blocks > NUM_FIXED_BLOCKS + 1)
2358                 {
2359                   if (dump_file)
2360                     fprintf (dump_file,
2361                              "Deleting fallthru block %i.\n",
2362                              b->index);
2363
2364                   c = b->prev_bb == ENTRY_BLOCK_PTR ? b->next_bb : b->prev_bb;
2365                   redirect_edge_succ_nodup (single_pred_edge (b),
2366                                             single_succ (b));
2367                   delete_basic_block (b);
2368                   changed = true;
2369                   b = c;
2370                   continue;
2371                 }
2372
2373               if (single_succ_p (b)
2374                   && (s = single_succ_edge (b))
2375                   && !(s->flags & EDGE_COMPLEX)
2376                   && (c = s->dest) != EXIT_BLOCK_PTR
2377                   && single_pred_p (c)
2378                   && b != c)
2379                 {
2380                   /* When not in cfg_layout mode use code aware of reordering
2381                      INSN.  This code possibly creates new basic blocks so it
2382                      does not fit merge_blocks interface and is kept here in
2383                      hope that it will become useless once more of compiler
2384                      is transformed to use cfg_layout mode.  */
2385
2386                   if ((mode & CLEANUP_CFGLAYOUT)
2387                       && can_merge_blocks_p (b, c))
2388                     {
2389                       merge_blocks (b, c);
2390                       update_forwarder_flag (b);
2391                       changed_here = true;
2392                     }
2393                   else if (!(mode & CLEANUP_CFGLAYOUT)
2394                            /* If the jump insn has side effects,
2395                               we can't kill the edge.  */
2396                            && (!JUMP_P (BB_END (b))
2397                                || (reload_completed
2398                                    ? simplejump_p (BB_END (b))
2399                                    : (onlyjump_p (BB_END (b))
2400                                       && !tablejump_p (BB_END (b),
2401                                                        NULL, NULL))))
2402                            && (next = merge_blocks_move (s, b, c, mode)))
2403                       {
2404                         b = next;
2405                         changed_here = true;
2406                       }
2407                 }
2408
2409               /* Simplify branch over branch.  */
2410               if ((mode & CLEANUP_EXPENSIVE)
2411                    && !(mode & CLEANUP_CFGLAYOUT)
2412                    && try_simplify_condjump (b))
2413                 changed_here = true;
2414
2415               /* If B has a single outgoing edge, but uses a
2416                  non-trivial jump instruction without side-effects, we
2417                  can either delete the jump entirely, or replace it
2418                  with a simple unconditional jump.  */
2419               if (single_succ_p (b)
2420                   && single_succ (b) != EXIT_BLOCK_PTR
2421                   && onlyjump_p (BB_END (b))
2422                   && !find_reg_note (BB_END (b), REG_CROSSING_JUMP, NULL_RTX)
2423                   && try_redirect_by_replacing_jump (single_succ_edge (b),
2424                                                      single_succ (b),
2425                                                      (mode & CLEANUP_CFGLAYOUT) != 0))
2426                 {
2427                   update_forwarder_flag (b);
2428                   changed_here = true;
2429                 }
2430
2431               /* Simplify branch to branch.  */
2432               if (try_forward_edges (mode, b))
2433                 changed_here = true;
2434
2435               /* Look for shared code between blocks.  */
2436               if ((mode & CLEANUP_CROSSJUMP)
2437                   && try_crossjump_bb (mode, b))
2438                 changed_here = true;
2439
2440               if ((mode & CLEANUP_CROSSJUMP)
2441                   /* This can lengthen register lifetimes.  Do it only after
2442                      reload.  */
2443                   && reload_completed
2444                   && try_head_merge_bb (b))
2445                 changed_here = true;
2446
2447               /* Don't get confused by the index shift caused by
2448                  deleting blocks.  */
2449               if (!changed_here)
2450                 b = b->next_bb;
2451               else
2452                 changed = true;
2453             }
2454
2455           if ((mode & CLEANUP_CROSSJUMP)
2456               && try_crossjump_bb (mode, EXIT_BLOCK_PTR))
2457             changed = true;
2458
2459           if (block_was_dirty)
2460             {
2461               /* This should only be set by head-merging.  */
2462               gcc_assert (mode & CLEANUP_CROSSJUMP);
2463               df_analyze ();
2464             }
2465
2466 #ifdef ENABLE_CHECKING
2467           if (changed)
2468             verify_flow_info ();
2469 #endif
2470
2471           changed_overall |= changed;
2472           first_pass = false;
2473         }
2474       while (changed);
2475     }
2476
2477   FOR_ALL_BB (b)
2478     b->flags &= ~(BB_FORWARDER_BLOCK | BB_NONTHREADABLE_BLOCK);
2479
2480   return changed_overall;
2481 }
2482 \f
2483 /* Delete all unreachable basic blocks.  */
2484
2485 bool
2486 delete_unreachable_blocks (void)
2487 {
2488   bool changed = false;
2489   basic_block b, prev_bb;
2490
2491   find_unreachable_blocks ();
2492
2493   /* When we're in GIMPLE mode and there may be debug insns, we should
2494      delete blocks in reverse dominator order, so as to get a chance
2495      to substitute all released DEFs into debug stmts.  If we don't
2496      have dominators information, walking blocks backward gets us a
2497      better chance of retaining most debug information than
2498      otherwise.  */
2499   if (MAY_HAVE_DEBUG_STMTS && current_ir_type () == IR_GIMPLE
2500       && dom_info_available_p (CDI_DOMINATORS))
2501     {
2502       for (b = EXIT_BLOCK_PTR->prev_bb; b != ENTRY_BLOCK_PTR; b = prev_bb)
2503         {
2504           prev_bb = b->prev_bb;
2505
2506           if (!(b->flags & BB_REACHABLE))
2507             {
2508               /* Speed up the removal of blocks that don't dominate
2509                  others.  Walking backwards, this should be the common
2510                  case.  */
2511               if (!first_dom_son (CDI_DOMINATORS, b))
2512                 delete_basic_block (b);
2513               else
2514                 {
2515                   VEC (basic_block, heap) *h
2516                     = get_all_dominated_blocks (CDI_DOMINATORS, b);
2517
2518                   while (VEC_length (basic_block, h))
2519                     {
2520                       b = VEC_pop (basic_block, h);
2521
2522                       prev_bb = b->prev_bb;
2523
2524                       gcc_assert (!(b->flags & BB_REACHABLE));
2525
2526                       delete_basic_block (b);
2527                     }
2528
2529                   VEC_free (basic_block, heap, h);
2530                 }
2531
2532               changed = true;
2533             }
2534         }
2535     }
2536   else
2537     {
2538       for (b = EXIT_BLOCK_PTR->prev_bb; b != ENTRY_BLOCK_PTR; b = prev_bb)
2539         {
2540           prev_bb = b->prev_bb;
2541
2542           if (!(b->flags & BB_REACHABLE))
2543             {
2544               delete_basic_block (b);
2545               changed = true;
2546             }
2547         }
2548     }
2549
2550   if (changed)
2551     tidy_fallthru_edges ();
2552   return changed;
2553 }
2554
2555 /* Delete any jump tables never referenced.  We can't delete them at the
2556    time of removing tablejump insn as they are referenced by the preceding
2557    insns computing the destination, so we delay deleting and garbagecollect
2558    them once life information is computed.  */
2559 void
2560 delete_dead_jumptables (void)
2561 {
2562   basic_block bb;
2563
2564   /* A dead jump table does not belong to any basic block.  Scan insns
2565      between two adjacent basic blocks.  */
2566   FOR_EACH_BB (bb)
2567     {
2568       rtx insn, next;
2569
2570       for (insn = NEXT_INSN (BB_END (bb));
2571            insn && !NOTE_INSN_BASIC_BLOCK_P (insn);
2572            insn = next)
2573         {
2574           next = NEXT_INSN (insn);
2575           if (LABEL_P (insn)
2576               && LABEL_NUSES (insn) == LABEL_PRESERVE_P (insn)
2577               && JUMP_TABLE_DATA_P (next))
2578             {
2579               rtx label = insn, jump = next;
2580
2581               if (dump_file)
2582                 fprintf (dump_file, "Dead jumptable %i removed\n",
2583                          INSN_UID (insn));
2584
2585               next = NEXT_INSN (next);
2586               delete_insn (jump);
2587               delete_insn (label);
2588             }
2589         }
2590     }
2591 }
2592
2593 \f
2594 /* Tidy the CFG by deleting unreachable code and whatnot.  */
2595
2596 bool
2597 cleanup_cfg (int mode)
2598 {
2599   bool changed = false;
2600
2601   /* Set the cfglayout mode flag here.  We could update all the callers
2602      but that is just inconvenient, especially given that we eventually
2603      want to have cfglayout mode as the default.  */
2604   if (current_ir_type () == IR_RTL_CFGLAYOUT)
2605     mode |= CLEANUP_CFGLAYOUT;
2606
2607   timevar_push (TV_CLEANUP_CFG);
2608   if (delete_unreachable_blocks ())
2609     {
2610       changed = true;
2611       /* We've possibly created trivially dead code.  Cleanup it right
2612          now to introduce more opportunities for try_optimize_cfg.  */
2613       if (!(mode & (CLEANUP_NO_INSN_DEL))
2614           && !reload_completed)
2615         delete_trivially_dead_insns (get_insns (), max_reg_num ());
2616     }
2617
2618   compact_blocks ();
2619
2620   /* To tail-merge blocks ending in the same noreturn function (e.g.
2621      a call to abort) we have to insert fake edges to exit.  Do this
2622      here once.  The fake edges do not interfere with any other CFG
2623      cleanups.  */
2624   if (mode & CLEANUP_CROSSJUMP)
2625     add_noreturn_fake_exit_edges ();
2626
2627   if (!dbg_cnt (cfg_cleanup))
2628     return changed;
2629
2630   while (try_optimize_cfg (mode))
2631     {
2632       delete_unreachable_blocks (), changed = true;
2633       if (!(mode & CLEANUP_NO_INSN_DEL))
2634         {
2635           /* Try to remove some trivially dead insns when doing an expensive
2636              cleanup.  But delete_trivially_dead_insns doesn't work after
2637              reload (it only handles pseudos) and run_fast_dce is too costly
2638              to run in every iteration.
2639
2640              For effective cross jumping, we really want to run a fast DCE to
2641              clean up any dead conditions, or they get in the way of performing
2642              useful tail merges.
2643
2644              Other transformations in cleanup_cfg are not so sensitive to dead
2645              code, so delete_trivially_dead_insns or even doing nothing at all
2646              is good enough.  */
2647           if ((mode & CLEANUP_EXPENSIVE) && !reload_completed
2648               && !delete_trivially_dead_insns (get_insns (), max_reg_num ()))
2649             break;
2650           if ((mode & CLEANUP_CROSSJUMP) && crossjumps_occured)
2651             run_fast_dce ();
2652         }
2653       else
2654         break;
2655     }
2656
2657   if (mode & CLEANUP_CROSSJUMP)
2658     remove_fake_exit_edges ();
2659
2660   /* Don't call delete_dead_jumptables in cfglayout mode, because
2661      that function assumes that jump tables are in the insns stream.
2662      But we also don't _have_ to delete dead jumptables in cfglayout
2663      mode because we shouldn't even be looking at things that are
2664      not in a basic block.  Dead jumptables are cleaned up when
2665      going out of cfglayout mode.  */
2666   if (!(mode & CLEANUP_CFGLAYOUT))
2667     delete_dead_jumptables ();
2668
2669   timevar_pop (TV_CLEANUP_CFG);
2670
2671   return changed;
2672 }
2673 \f
2674 static unsigned int
2675 rest_of_handle_jump (void)
2676 {
2677   if (crtl->tail_call_emit)
2678     fixup_tail_calls ();
2679   return 0;
2680 }
2681
2682 struct rtl_opt_pass pass_jump =
2683 {
2684  {
2685   RTL_PASS,
2686   "sibling",                            /* name */
2687   NULL,                                 /* gate */
2688   rest_of_handle_jump,                  /* execute */
2689   NULL,                                 /* sub */
2690   NULL,                                 /* next */
2691   0,                                    /* static_pass_number */
2692   TV_JUMP,                              /* tv_id */
2693   0,                                    /* properties_required */
2694   0,                                    /* properties_provided */
2695   0,                                    /* properties_destroyed */
2696   TODO_ggc_collect,                     /* todo_flags_start */
2697   TODO_verify_flow,                     /* todo_flags_finish */
2698  }
2699 };
2700
2701
2702 static unsigned int
2703 rest_of_handle_jump2 (void)
2704 {
2705   delete_trivially_dead_insns (get_insns (), max_reg_num ());
2706   if (dump_file)
2707     dump_flow_info (dump_file, dump_flags);
2708   cleanup_cfg ((optimize ? CLEANUP_EXPENSIVE : 0)
2709                | (flag_thread_jumps ? CLEANUP_THREADING : 0));
2710   return 0;
2711 }
2712
2713
2714 struct rtl_opt_pass pass_jump2 =
2715 {
2716  {
2717   RTL_PASS,
2718   "jump",                               /* name */
2719   NULL,                                 /* gate */
2720   rest_of_handle_jump2,                 /* execute */
2721   NULL,                                 /* sub */
2722   NULL,                                 /* next */
2723   0,                                    /* static_pass_number */
2724   TV_JUMP,                              /* tv_id */
2725   0,                                    /* properties_required */
2726   0,                                    /* properties_provided */
2727   0,                                    /* properties_destroyed */
2728   TODO_ggc_collect,                     /* todo_flags_start */
2729   TODO_dump_func | TODO_verify_rtl_sharing,/* todo_flags_finish */
2730  }
2731 };
2732
2733