OSDN Git Service

2004-04-09 Caroline Tice <ctice@apple.com>
[pf3gnuchains/gcc-fork.git] / gcc / cfgrtl.c
1 /* Control flow graph manipulation code for GNU compiler.
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22 /* This file contains low level functions to manipulate the CFG and analyze it
23    that are aware of the RTL intermediate language.
24
25    Available functionality:
26      - Basic CFG/RTL manipulation API documented in cfghooks.h
27      - CFG-aware instruction chain manipulation
28          delete_insn, delete_insn_chain
29      - Edge splitting and committing to edges
30          insert_insn_on_edge, commit_edge_insertions
31      - CFG updating after insn simplification
32          purge_dead_edges, purge_all_dead_edges
33
34    Functions not supposed for generic use:
35      - Infrastructure to determine quickly basic block for insn
36          compute_bb_for_insn, update_bb_for_insn, set_block_for_insn,
37      - Edge redirection with updating and optimizing of insn chain
38          block_label, tidy_fallthru_edge, force_nonfallthru  */
39 \f
40 #include "config.h"
41 #include "system.h"
42 #include "coretypes.h"
43 #include "tm.h"
44 #include "tree.h"
45 #include "rtl.h"
46 #include "hard-reg-set.h"
47 #include "basic-block.h"
48 #include "regs.h"
49 #include "flags.h"
50 #include "output.h"
51 #include "function.h"
52 #include "except.h"
53 #include "toplev.h"
54 #include "tm_p.h"
55 #include "obstack.h"
56 #include "insn-config.h"
57 #include "cfglayout.h"
58 #include "expr.h"
59
60 /* Stubs in case we don't have a return insn.  */
61 #ifndef HAVE_return
62 #define HAVE_return 0
63 #define gen_return() NULL_RTX
64 #endif
65
66 /* The labels mentioned in non-jump rtl.  Valid during find_basic_blocks.  */
67 /* ??? Should probably be using LABEL_NUSES instead.  It would take a
68    bit of surgery to be able to use or co-opt the routines in jump.  */
69 rtx label_value_list;
70 rtx tail_recursion_label_list;
71
72 static int can_delete_note_p (rtx);
73 static int can_delete_label_p (rtx);
74 static void commit_one_edge_insertion (edge, int);
75 static rtx last_loop_beg_note (rtx);
76 static bool back_edge_of_syntactic_loop_p (basic_block, basic_block);
77 basic_block force_nonfallthru_and_redirect (edge, basic_block);
78 static basic_block rtl_split_edge (edge);
79 static bool rtl_move_block_after (basic_block, basic_block);
80 static int rtl_verify_flow_info (void);
81 static basic_block cfg_layout_split_block (basic_block, void *);
82 static bool cfg_layout_redirect_edge_and_branch (edge, basic_block);
83 static basic_block cfg_layout_redirect_edge_and_branch_force (edge, basic_block);
84 static void cfg_layout_delete_block (basic_block);
85 static void rtl_delete_block (basic_block);
86 static basic_block rtl_redirect_edge_and_branch_force (edge, basic_block);
87 static bool rtl_redirect_edge_and_branch (edge, basic_block);
88 static basic_block rtl_split_block (basic_block, void *);
89 static void rtl_dump_bb (basic_block, FILE *, int);
90 static int rtl_verify_flow_info_1 (void);
91 static void mark_killed_regs (rtx, rtx, void *);
92 static void rtl_make_forwarder_block (edge);
93 \f
94 /* Return true if NOTE is not one of the ones that must be kept paired,
95    so that we may simply delete it.  */
96
97 static int
98 can_delete_note_p (rtx note)
99 {
100   return (NOTE_LINE_NUMBER (note) == NOTE_INSN_DELETED
101           || NOTE_LINE_NUMBER (note) == NOTE_INSN_BASIC_BLOCK
102           || NOTE_LINE_NUMBER (note) == NOTE_INSN_UNLIKELY_EXECUTED_CODE
103           || NOTE_LINE_NUMBER (note) == NOTE_INSN_PREDICTION);
104 }
105
106 /* True if a given label can be deleted.  */
107
108 static int
109 can_delete_label_p (rtx label)
110 {
111   return (!LABEL_PRESERVE_P (label)
112           /* User declared labels must be preserved.  */
113           && LABEL_NAME (label) == 0
114           && !in_expr_list_p (forced_labels, label)
115           && !in_expr_list_p (label_value_list, label));
116 }
117
118 /* Delete INSN by patching it out.  Return the next insn.  */
119
120 rtx
121 delete_insn (rtx insn)
122 {
123   rtx next = NEXT_INSN (insn);
124   rtx note;
125   bool really_delete = true;
126
127   if (GET_CODE (insn) == CODE_LABEL)
128     {
129       /* Some labels can't be directly removed from the INSN chain, as they
130          might be references via variables, constant pool etc.
131          Convert them to the special NOTE_INSN_DELETED_LABEL note.  */
132       if (! can_delete_label_p (insn))
133         {
134           const char *name = LABEL_NAME (insn);
135
136           really_delete = false;
137           PUT_CODE (insn, NOTE);
138           NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED_LABEL;
139           NOTE_SOURCE_FILE (insn) = name;
140         }
141
142       remove_node_from_expr_list (insn, &nonlocal_goto_handler_labels);
143     }
144
145   if (really_delete)
146     {
147       /* If this insn has already been deleted, something is very wrong.  */
148       if (INSN_DELETED_P (insn))
149         abort ();
150       remove_insn (insn);
151       INSN_DELETED_P (insn) = 1;
152     }
153
154   /* If deleting a jump, decrement the use count of the label.  Deleting
155      the label itself should happen in the normal course of block merging.  */
156   if (GET_CODE (insn) == JUMP_INSN
157       && JUMP_LABEL (insn)
158       && GET_CODE (JUMP_LABEL (insn)) == CODE_LABEL)
159     LABEL_NUSES (JUMP_LABEL (insn))--;
160
161   /* Also if deleting an insn that references a label.  */
162   else
163     {
164       while ((note = find_reg_note (insn, REG_LABEL, NULL_RTX)) != NULL_RTX
165              && GET_CODE (XEXP (note, 0)) == CODE_LABEL)
166         {
167           LABEL_NUSES (XEXP (note, 0))--;
168           remove_note (insn, note);
169         }
170     }
171
172   if (GET_CODE (insn) == JUMP_INSN
173       && (GET_CODE (PATTERN (insn)) == ADDR_VEC
174           || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
175     {
176       rtx pat = PATTERN (insn);
177       int diff_vec_p = GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC;
178       int len = XVECLEN (pat, diff_vec_p);
179       int i;
180
181       for (i = 0; i < len; i++)
182         {
183           rtx label = XEXP (XVECEXP (pat, diff_vec_p, i), 0);
184
185           /* When deleting code in bulk (e.g. removing many unreachable
186              blocks) we can delete a label that's a target of the vector
187              before deleting the vector itself.  */
188           if (GET_CODE (label) != NOTE)
189             LABEL_NUSES (label)--;
190         }
191     }
192
193   return next;
194 }
195
196 /* Like delete_insn but also purge dead edges from BB.  */
197 rtx
198 delete_insn_and_edges (rtx insn)
199 {
200   rtx x;
201   bool purge = false;
202
203   if (INSN_P (insn)
204       && BLOCK_FOR_INSN (insn)
205       && BB_END (BLOCK_FOR_INSN (insn)) == insn)
206     purge = true;
207   x = delete_insn (insn);
208   if (purge)
209     purge_dead_edges (BLOCK_FOR_INSN (insn));
210   return x;
211 }
212
213 /* Unlink a chain of insns between START and FINISH, leaving notes
214    that must be paired.  */
215
216 void
217 delete_insn_chain (rtx start, rtx finish)
218 {
219   rtx next;
220
221   /* Unchain the insns one by one.  It would be quicker to delete all of these
222      with a single unchaining, rather than one at a time, but we need to keep
223      the NOTE's.  */
224   while (1)
225     {
226       next = NEXT_INSN (start);
227       if (GET_CODE (start) == NOTE && !can_delete_note_p (start))
228         ;
229       else
230         next = delete_insn (start);
231
232       if (start == finish)
233         break;
234       start = next;
235     }
236 }
237
238 /* Like delete_insn but also purge dead edges from BB.  */
239 void
240 delete_insn_chain_and_edges (rtx first, rtx last)
241 {
242   bool purge = false;
243
244   if (INSN_P (last)
245       && BLOCK_FOR_INSN (last)
246       && BB_END (BLOCK_FOR_INSN (last)) == last)
247     purge = true;
248   delete_insn_chain (first, last);
249   if (purge)
250     purge_dead_edges (BLOCK_FOR_INSN (last));
251 }
252 \f
253 /* Create a new basic block consisting of the instructions between HEAD and END
254    inclusive.  This function is designed to allow fast BB construction - reuses
255    the note and basic block struct in BB_NOTE, if any and do not grow
256    BASIC_BLOCK chain and should be used directly only by CFG construction code.
257    END can be NULL in to create new empty basic block before HEAD.  Both END
258    and HEAD can be NULL to create basic block at the end of INSN chain.
259    AFTER is the basic block we should be put after.  */
260
261 basic_block
262 create_basic_block_structure (rtx head, rtx end, rtx bb_note, basic_block after)
263 {
264   basic_block bb;
265
266   if (bb_note
267       && ! RTX_INTEGRATED_P (bb_note)
268       && (bb = NOTE_BASIC_BLOCK (bb_note)) != NULL
269       && bb->aux == NULL)
270     {
271       /* If we found an existing note, thread it back onto the chain.  */
272
273       rtx after;
274
275       if (GET_CODE (head) == CODE_LABEL)
276         after = head;
277       else
278         {
279           after = PREV_INSN (head);
280           head = bb_note;
281         }
282
283       if (after != bb_note && NEXT_INSN (after) != bb_note)
284         reorder_insns_nobb (bb_note, bb_note, after);
285     }
286   else
287     {
288       /* Otherwise we must create a note and a basic block structure.  */
289
290       bb = alloc_block ();
291
292       if (!head && !end)
293         head = end = bb_note
294           = emit_note_after (NOTE_INSN_BASIC_BLOCK, get_last_insn ());
295       else if (GET_CODE (head) == CODE_LABEL && end)
296         {
297           bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, head);
298           if (head == end)
299             end = bb_note;
300         }
301       else
302         {
303           bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, head);
304           head = bb_note;
305           if (!end)
306             end = head;
307         }
308
309       NOTE_BASIC_BLOCK (bb_note) = bb;
310     }
311
312   /* Always include the bb note in the block.  */
313   if (NEXT_INSN (end) == bb_note)
314     end = bb_note;
315
316   BB_HEAD (bb) = head;
317   BB_END (bb) = end;
318   bb->index = last_basic_block++;
319   bb->flags = BB_NEW;
320   link_block (bb, after);
321   BASIC_BLOCK (bb->index) = bb;
322   update_bb_for_insn (bb);
323   bb->partition = UNPARTITIONED;
324
325   /* Tag the block so that we know it has been used when considering
326      other basic block notes.  */
327   bb->aux = bb;
328
329   return bb;
330 }
331
332 /* Create new basic block consisting of instructions in between HEAD and END
333    and place it to the BB chain after block AFTER.  END can be NULL in to
334    create new empty basic block before HEAD.  Both END and HEAD can be NULL to
335    create basic block at the end of INSN chain.  */
336
337 static basic_block
338 rtl_create_basic_block (void *headp, void *endp, basic_block after)
339 {
340   rtx head = headp, end = endp;
341   basic_block bb;
342
343   /* Grow the basic block array if needed.  */
344   if ((size_t) last_basic_block >= VARRAY_SIZE (basic_block_info))
345     {
346       size_t new_size = last_basic_block + (last_basic_block + 3) / 4;
347       VARRAY_GROW (basic_block_info, new_size);
348     }
349
350   n_basic_blocks++;
351
352   bb = create_basic_block_structure (head, end, NULL, after);
353   bb->aux = NULL;
354   return bb;
355 }
356
357 static basic_block
358 cfg_layout_create_basic_block (void *head, void *end, basic_block after)
359 {
360   basic_block newbb = rtl_create_basic_block (head, end, after);
361
362   cfg_layout_initialize_rbi (newbb);
363   return newbb;
364 }
365 \f
366 /* Delete the insns in a (non-live) block.  We physically delete every
367    non-deleted-note insn, and update the flow graph appropriately.
368
369    Return nonzero if we deleted an exception handler.  */
370
371 /* ??? Preserving all such notes strikes me as wrong.  It would be nice
372    to post-process the stream to remove empty blocks, loops, ranges, etc.  */
373
374 static void
375 rtl_delete_block (basic_block b)
376 {
377   rtx insn, end, tmp;
378
379   /* If the head of this block is a CODE_LABEL, then it might be the
380      label for an exception handler which can't be reached.
381
382      We need to remove the label from the exception_handler_label list
383      and remove the associated NOTE_INSN_EH_REGION_BEG and
384      NOTE_INSN_EH_REGION_END notes.  */
385
386   /* Get rid of all NOTE_INSN_PREDICTIONs and NOTE_INSN_LOOP_CONTs
387      hanging before the block.  */
388
389   for (insn = PREV_INSN (BB_HEAD (b)); insn; insn = PREV_INSN (insn))
390     {
391       if (GET_CODE (insn) != NOTE)
392         break;
393       if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_PREDICTION
394           || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT)
395         NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
396     }
397
398   insn = BB_HEAD (b);
399
400   never_reached_warning (insn, BB_END (b));
401
402   if (GET_CODE (insn) == CODE_LABEL)
403     maybe_remove_eh_handler (insn);
404
405   /* Include any jump table following the basic block.  */
406   end = BB_END (b);
407   if (tablejump_p (end, NULL, &tmp))
408     end = tmp;
409
410   /* Include any barrier that may follow the basic block.  */
411   tmp = next_nonnote_insn (end);
412   if (tmp && GET_CODE (tmp) == BARRIER)
413     end = tmp;
414
415   /* Selectively delete the entire chain.  */
416   BB_HEAD (b) = NULL;
417   delete_insn_chain (insn, end);
418 }
419 \f
420 /* Records the basic block struct in BLOCK_FOR_INSN for every insn.  */
421
422 void
423 compute_bb_for_insn (void)
424 {
425   basic_block bb;
426
427   FOR_EACH_BB (bb)
428     {
429       rtx end = BB_END (bb);
430       rtx insn;
431
432       for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
433         {
434           BLOCK_FOR_INSN (insn) = bb;
435           if (insn == end)
436             break;
437         }
438     }
439 }
440
441 /* Release the basic_block_for_insn array.  */
442
443 void
444 free_bb_for_insn (void)
445 {
446   rtx insn;
447   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
448     if (GET_CODE (insn) != BARRIER)
449       BLOCK_FOR_INSN (insn) = NULL;
450 }
451
452 /* Update insns block within BB.  */
453
454 void
455 update_bb_for_insn (basic_block bb)
456 {
457   rtx insn;
458
459   for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
460     {
461       if (GET_CODE (insn) != BARRIER)
462         set_block_for_insn (insn, bb);
463       if (insn == BB_END (bb))
464         break;
465     }
466 }
467 \f
468 /* Creates a new basic block just after basic block B by splitting
469    everything after specified instruction I.  */
470
471 static basic_block
472 rtl_split_block (basic_block bb, void *insnp)
473 {
474   basic_block new_bb;
475   rtx insn = insnp;
476   edge e;
477
478   if (!insn)
479     {
480       insn = first_insn_after_basic_block_note (bb);
481
482       if (insn)
483         insn = PREV_INSN (insn);
484       else
485         insn = get_last_insn ();
486     }
487
488   /* We probably should check type of the insn so that we do not create
489      inconsistent cfg.  It is checked in verify_flow_info anyway, so do not
490      bother.  */
491   if (insn == BB_END (bb))
492     emit_note_after (NOTE_INSN_DELETED, insn);
493
494   /* Create the new basic block.  */
495   new_bb = create_basic_block (NEXT_INSN (insn), BB_END (bb), bb);
496   BB_END (bb) = insn;
497
498   /* Redirect the outgoing edges.  */
499   new_bb->succ = bb->succ;
500   bb->succ = NULL;
501   for (e = new_bb->succ; e; e = e->succ_next)
502     e->src = new_bb;
503
504   if (bb->global_live_at_start)
505     {
506       new_bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (&flow_obstack);
507       new_bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (&flow_obstack);
508       COPY_REG_SET (new_bb->global_live_at_end, bb->global_live_at_end);
509
510       /* We now have to calculate which registers are live at the end
511          of the split basic block and at the start of the new basic
512          block.  Start with those registers that are known to be live
513          at the end of the original basic block and get
514          propagate_block to determine which registers are live.  */
515       COPY_REG_SET (new_bb->global_live_at_start, bb->global_live_at_end);
516       propagate_block (new_bb, new_bb->global_live_at_start, NULL, NULL, 0);
517       COPY_REG_SET (bb->global_live_at_end,
518                     new_bb->global_live_at_start);
519 #ifdef HAVE_conditional_execution
520       /* In the presence of conditional execution we are not able to update
521          liveness precisely.  */
522       if (reload_completed)
523         {
524           bb->flags |= BB_DIRTY;
525           new_bb->flags |= BB_DIRTY;
526         }
527 #endif
528     }
529
530   return new_bb;
531 }
532
533 /* Blocks A and B are to be merged into a single block A.  The insns
534    are already contiguous.  */
535
536 static void
537 rtl_merge_blocks (basic_block a, basic_block b)
538 {
539   rtx b_head = BB_HEAD (b), b_end = BB_END (b), a_end = BB_END (a);
540   rtx del_first = NULL_RTX, del_last = NULL_RTX;
541   int b_empty = 0;
542
543   /* If there was a CODE_LABEL beginning B, delete it.  */
544   if (GET_CODE (b_head) == CODE_LABEL)
545     {
546       /* Detect basic blocks with nothing but a label.  This can happen
547          in particular at the end of a function.  */
548       if (b_head == b_end)
549         b_empty = 1;
550
551       del_first = del_last = b_head;
552       b_head = NEXT_INSN (b_head);
553     }
554
555   /* Delete the basic block note and handle blocks containing just that
556      note.  */
557   if (NOTE_INSN_BASIC_BLOCK_P (b_head))
558     {
559       if (b_head == b_end)
560         b_empty = 1;
561       if (! del_last)
562         del_first = b_head;
563
564       del_last = b_head;
565       b_head = NEXT_INSN (b_head);
566     }
567
568   /* If there was a jump out of A, delete it.  */
569   if (GET_CODE (a_end) == JUMP_INSN)
570     {
571       rtx prev;
572
573       for (prev = PREV_INSN (a_end); ; prev = PREV_INSN (prev))
574         if (GET_CODE (prev) != NOTE
575             || NOTE_LINE_NUMBER (prev) == NOTE_INSN_BASIC_BLOCK
576             || prev == BB_HEAD (a))
577           break;
578
579       del_first = a_end;
580
581 #ifdef HAVE_cc0
582       /* If this was a conditional jump, we need to also delete
583          the insn that set cc0.  */
584       if (only_sets_cc0_p (prev))
585         {
586           rtx tmp = prev;
587
588           prev = prev_nonnote_insn (prev);
589           if (!prev)
590             prev = BB_HEAD (a);
591           del_first = tmp;
592         }
593 #endif
594
595       a_end = PREV_INSN (del_first);
596     }
597   else if (GET_CODE (NEXT_INSN (a_end)) == BARRIER)
598     del_first = NEXT_INSN (a_end);
599
600   /* Delete everything marked above as well as crap that might be
601      hanging out between the two blocks.  */
602   BB_HEAD (b) = NULL;
603   delete_insn_chain (del_first, del_last);
604
605   /* Reassociate the insns of B with A.  */
606   if (!b_empty)
607     {
608       rtx x;
609
610       for (x = a_end; x != b_end; x = NEXT_INSN (x))
611         set_block_for_insn (x, a);
612
613       set_block_for_insn (b_end, a);
614
615       a_end = b_end;
616     }
617
618   BB_END (a) = a_end;
619 }
620
621 /* Return true when block A and B can be merged.  */
622 static bool
623 rtl_can_merge_blocks (basic_block a,basic_block b)
624 {
625   bool partitions_ok = true;
626
627   /* If we are partitioning hot/cold basic blocks, we don't want to
628      mess up unconditional or indirect jumps that cross between hot
629      and cold sections.  */
630   
631   if (flag_reorder_blocks_and_partition
632       && (find_reg_note (BB_END (a), REG_CROSSING_JUMP, NULL_RTX)
633           || find_reg_note (BB_END (b), REG_CROSSING_JUMP, NULL_RTX)
634           || a->partition != b->partition))
635     partitions_ok = false;
636
637   /* There must be exactly one edge in between the blocks.  */
638   return (a->succ && !a->succ->succ_next && a->succ->dest == b
639           && !b->pred->pred_next && a != b
640           /* Must be simple edge.  */
641           && !(a->succ->flags & EDGE_COMPLEX)
642           && partitions_ok
643           && a->next_bb == b
644           && a != ENTRY_BLOCK_PTR && b != EXIT_BLOCK_PTR
645           /* If the jump insn has side effects,
646              we can't kill the edge.  */
647           && (GET_CODE (BB_END (a)) != JUMP_INSN
648               || (reload_completed
649                   ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
650 }
651 \f
652 /* Return the label in the head of basic block BLOCK.  Create one if it doesn't
653    exist.  */
654
655 rtx
656 block_label (basic_block block)
657 {
658   if (block == EXIT_BLOCK_PTR)
659     return NULL_RTX;
660
661   if (GET_CODE (BB_HEAD (block)) != CODE_LABEL)
662     {
663       BB_HEAD (block) = emit_label_before (gen_label_rtx (), BB_HEAD (block));
664     }
665
666   return BB_HEAD (block);
667 }
668
669 /* Attempt to perform edge redirection by replacing possibly complex jump
670    instruction by unconditional jump or removing jump completely.  This can
671    apply only if all edges now point to the same block.  The parameters and
672    return values are equivalent to redirect_edge_and_branch.  */
673
674 bool
675 try_redirect_by_replacing_jump (edge e, basic_block target, bool in_cfglayout)
676 {
677   basic_block src = e->src;
678   rtx insn = BB_END (src), kill_from;
679   edge tmp;
680   rtx set;
681   int fallthru = 0;
682
683
684   /* If we are partitioning hot/cold basic blocks, we don't want to
685      mess up unconditional or indirect jumps that cross between hot
686      and cold sections.  */
687   
688   if (flag_reorder_blocks_and_partition
689       && find_reg_note (insn, REG_CROSSING_JUMP, NULL_RTX))
690     return false;
691
692   /* Verify that all targets will be TARGET.  */
693   for (tmp = src->succ; tmp; tmp = tmp->succ_next)
694     if (tmp->dest != target && tmp != e)
695       break;
696
697   if (tmp || !onlyjump_p (insn))
698     return false;
699   if ((!optimize || reload_completed) && tablejump_p (insn, NULL, NULL))
700     return false;
701
702   /* Avoid removing branch with side effects.  */
703   set = single_set (insn);
704   if (!set || side_effects_p (set))
705     return false;
706
707   /* In case we zap a conditional jump, we'll need to kill
708      the cc0 setter too.  */
709   kill_from = insn;
710 #ifdef HAVE_cc0
711   if (reg_mentioned_p (cc0_rtx, PATTERN (insn)))
712     kill_from = PREV_INSN (insn);
713 #endif
714
715   /* See if we can create the fallthru edge.  */
716   if (in_cfglayout || can_fallthru (src, target))
717     {
718       if (dump_file)
719         fprintf (dump_file, "Removing jump %i.\n", INSN_UID (insn));
720       fallthru = 1;
721
722       /* Selectively unlink whole insn chain.  */
723       if (in_cfglayout)
724         {
725           rtx insn = src->rbi->footer;
726
727           delete_insn_chain (kill_from, BB_END (src));
728
729           /* Remove barriers but keep jumptables.  */
730           while (insn)
731             {
732               if (GET_CODE (insn) == BARRIER)
733                 {
734                   if (PREV_INSN (insn))
735                     NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
736                   else
737                     src->rbi->footer = NEXT_INSN (insn);
738                   if (NEXT_INSN (insn))
739                     PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
740                 }
741               if (GET_CODE (insn) == CODE_LABEL)
742                 break;
743               insn = NEXT_INSN (insn);
744             }
745         }
746       else
747         delete_insn_chain (kill_from, PREV_INSN (BB_HEAD (target)));
748     }
749
750   /* If this already is simplejump, redirect it.  */
751   else if (simplejump_p (insn))
752     {
753       if (e->dest == target)
754         return false;
755       if (dump_file)
756         fprintf (dump_file, "Redirecting jump %i from %i to %i.\n",
757                  INSN_UID (insn), e->dest->index, target->index);
758       if (!redirect_jump (insn, block_label (target), 0))
759         {
760           if (target == EXIT_BLOCK_PTR)
761             return false;
762           abort ();
763         }
764     }
765
766   /* Cannot do anything for target exit block.  */
767   else if (target == EXIT_BLOCK_PTR)
768     return false;
769
770   /* Or replace possibly complicated jump insn by simple jump insn.  */
771   else
772     {
773       rtx target_label = block_label (target);
774       rtx barrier, label, table;
775
776       emit_jump_insn_after (gen_jump (target_label), insn);
777       JUMP_LABEL (BB_END (src)) = target_label;
778       LABEL_NUSES (target_label)++;
779       if (dump_file)
780         fprintf (dump_file, "Replacing insn %i by jump %i\n",
781                  INSN_UID (insn), INSN_UID (BB_END (src)));
782
783
784       delete_insn_chain (kill_from, insn);
785
786       /* Recognize a tablejump that we are converting to a
787          simple jump and remove its associated CODE_LABEL
788          and ADDR_VEC or ADDR_DIFF_VEC.  */
789       if (tablejump_p (insn, &label, &table))
790         delete_insn_chain (label, table);
791
792       barrier = next_nonnote_insn (BB_END (src));
793       if (!barrier || GET_CODE (barrier) != BARRIER)
794         emit_barrier_after (BB_END (src));
795       else
796         {
797           if (barrier != NEXT_INSN (BB_END (src)))
798             {
799               /* Move the jump before barrier so that the notes
800                  which originally were or were created before jump table are
801                  inside the basic block.  */
802               rtx new_insn = BB_END (src);
803               rtx tmp;
804
805               for (tmp = NEXT_INSN (BB_END (src)); tmp != barrier;
806                    tmp = NEXT_INSN (tmp))
807                 set_block_for_insn (tmp, src);
808
809               NEXT_INSN (PREV_INSN (new_insn)) = NEXT_INSN (new_insn);
810               PREV_INSN (NEXT_INSN (new_insn)) = PREV_INSN (new_insn);
811
812               NEXT_INSN (new_insn) = barrier;
813               NEXT_INSN (PREV_INSN (barrier)) = new_insn;
814
815               PREV_INSN (new_insn) = PREV_INSN (barrier);
816               PREV_INSN (barrier) = new_insn;
817             }
818         }
819     }
820
821   /* Keep only one edge out and set proper flags.  */
822   while (src->succ->succ_next)
823     remove_edge (src->succ);
824   e = src->succ;
825   if (fallthru)
826     e->flags = EDGE_FALLTHRU;
827   else
828     e->flags = 0;
829
830   e->probability = REG_BR_PROB_BASE;
831   e->count = src->count;
832
833   /* We don't want a block to end on a line-number note since that has
834      the potential of changing the code between -g and not -g.  */
835   while (GET_CODE (BB_END (e->src)) == NOTE
836          && NOTE_LINE_NUMBER (BB_END (e->src)) >= 0)
837     delete_insn (BB_END (e->src));
838
839   if (e->dest != target)
840     redirect_edge_succ (e, target);
841
842   return true;
843 }
844
845 /* Return last loop_beg note appearing after INSN, before start of next
846    basic block.  Return INSN if there are no such notes.
847
848    When emitting jump to redirect a fallthru edge, it should always appear
849    after the LOOP_BEG notes, as loop optimizer expect loop to either start by
850    fallthru edge or jump following the LOOP_BEG note jumping to the loop exit
851    test.  */
852
853 static rtx
854 last_loop_beg_note (rtx insn)
855 {
856   rtx last = insn;
857
858   for (insn = NEXT_INSN (insn); insn && GET_CODE (insn) == NOTE
859        && NOTE_LINE_NUMBER (insn) != NOTE_INSN_BASIC_BLOCK;
860        insn = NEXT_INSN (insn))
861     if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
862       last = insn;
863
864   return last;
865 }
866
867 /* Redirect edge representing branch of (un)conditional jump or tablejump.  */
868 static bool
869 redirect_branch_edge (edge e, basic_block target)
870 {
871   rtx tmp;
872   rtx old_label = BB_HEAD (e->dest);
873   basic_block src = e->src;
874   rtx insn = BB_END (src);
875
876   /* We can only redirect non-fallthru edges of jump insn.  */
877   if (e->flags & EDGE_FALLTHRU)
878     return false;
879   else if (GET_CODE (insn) != JUMP_INSN)
880     return false;
881
882   /* Recognize a tablejump and adjust all matching cases.  */
883   if (tablejump_p (insn, NULL, &tmp))
884     {
885       rtvec vec;
886       int j;
887       rtx new_label = block_label (target);
888
889       if (target == EXIT_BLOCK_PTR)
890         return false;
891       if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
892         vec = XVEC (PATTERN (tmp), 0);
893       else
894         vec = XVEC (PATTERN (tmp), 1);
895
896       for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
897         if (XEXP (RTVEC_ELT (vec, j), 0) == old_label)
898           {
899             RTVEC_ELT (vec, j) = gen_rtx_LABEL_REF (Pmode, new_label);
900             --LABEL_NUSES (old_label);
901             ++LABEL_NUSES (new_label);
902           }
903
904       /* Handle casesi dispatch insns.  */
905       if ((tmp = single_set (insn)) != NULL
906           && SET_DEST (tmp) == pc_rtx
907           && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
908           && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF
909           && XEXP (XEXP (SET_SRC (tmp), 2), 0) == old_label)
910         {
911           XEXP (SET_SRC (tmp), 2) = gen_rtx_LABEL_REF (VOIDmode,
912                                                        new_label);
913           --LABEL_NUSES (old_label);
914           ++LABEL_NUSES (new_label);
915         }
916     }
917   else
918     {
919       /* ?? We may play the games with moving the named labels from
920          one basic block to the other in case only one computed_jump is
921          available.  */
922       if (computed_jump_p (insn)
923           /* A return instruction can't be redirected.  */
924           || returnjump_p (insn))
925         return false;
926
927       /* If the insn doesn't go where we think, we're confused.  */
928       if (JUMP_LABEL (insn) != old_label)
929         abort ();
930
931       /* If the substitution doesn't succeed, die.  This can happen
932          if the back end emitted unrecognizable instructions or if
933          target is exit block on some arches.  */
934       if (!redirect_jump (insn, block_label (target), 0))
935         {
936           if (target == EXIT_BLOCK_PTR)
937             return false;
938           abort ();
939         }
940     }
941
942   if (dump_file)
943     fprintf (dump_file, "Edge %i->%i redirected to %i\n",
944              e->src->index, e->dest->index, target->index);
945
946   if (e->dest != target)
947     redirect_edge_succ_nodup (e, target);
948   return true;
949 }
950
951 /* Attempt to change code to redirect edge E to TARGET.  Don't do that on
952    expense of adding new instructions or reordering basic blocks.
953
954    Function can be also called with edge destination equivalent to the TARGET.
955    Then it should try the simplifications and do nothing if none is possible.
956
957    Return true if transformation succeeded.  We still return false in case E
958    already destinated TARGET and we didn't managed to simplify instruction
959    stream.  */
960
961 static bool
962 rtl_redirect_edge_and_branch (edge e, basic_block target)
963 {
964   basic_block src = e->src;
965
966   if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
967     return false;
968
969   if (e->dest == target)
970     return true;
971
972   if (try_redirect_by_replacing_jump (e, target, false))
973     {
974       src->flags |= BB_DIRTY;
975       return true;
976     }
977
978   if (!redirect_branch_edge (e, target))
979     return false;
980
981   src->flags |= BB_DIRTY;
982   return true;
983 }
984
985 /* Like force_nonfallthru below, but additionally performs redirection
986    Used by redirect_edge_and_branch_force.  */
987
988 basic_block
989 force_nonfallthru_and_redirect (edge e, basic_block target)
990 {
991   basic_block jump_block, new_bb = NULL, src = e->src;
992   rtx note;
993   edge new_edge;
994   int abnormal_edge_flags = 0;
995
996   /* In the case the last instruction is conditional jump to the next
997      instruction, first redirect the jump itself and then continue
998      by creating a basic block afterwards to redirect fallthru edge.  */
999   if (e->src != ENTRY_BLOCK_PTR && e->dest != EXIT_BLOCK_PTR
1000       && any_condjump_p (BB_END (e->src))
1001       /* When called from cfglayout, fallthru edges do not
1002          necessarily go to the next block.  */
1003       && e->src->next_bb == e->dest
1004       && JUMP_LABEL (BB_END (e->src)) == BB_HEAD (e->dest))
1005     {
1006       rtx note;
1007       edge b = unchecked_make_edge (e->src, target, 0);
1008
1009       if (!redirect_jump (BB_END (e->src), block_label (target), 0))
1010         abort ();
1011       note = find_reg_note (BB_END (e->src), REG_BR_PROB, NULL_RTX);
1012       if (note)
1013         {
1014           int prob = INTVAL (XEXP (note, 0));
1015
1016           b->probability = prob;
1017           b->count = e->count * prob / REG_BR_PROB_BASE;
1018           e->probability -= e->probability;
1019           e->count -= b->count;
1020           if (e->probability < 0)
1021             e->probability = 0;
1022           if (e->count < 0)
1023             e->count = 0;
1024         }
1025     }
1026
1027   if (e->flags & EDGE_ABNORMAL)
1028     {
1029       /* Irritating special case - fallthru edge to the same block as abnormal
1030          edge.
1031          We can't redirect abnormal edge, but we still can split the fallthru
1032          one and create separate abnormal edge to original destination.
1033          This allows bb-reorder to make such edge non-fallthru.  */
1034       if (e->dest != target)
1035         abort ();
1036       abnormal_edge_flags = e->flags & ~(EDGE_FALLTHRU | EDGE_CAN_FALLTHRU);
1037       e->flags &= EDGE_FALLTHRU | EDGE_CAN_FALLTHRU;
1038     }
1039   else if (!(e->flags & EDGE_FALLTHRU))
1040     abort ();
1041   else if (e->src == ENTRY_BLOCK_PTR)
1042     {
1043       /* We can't redirect the entry block.  Create an empty block at the
1044          start of the function which we use to add the new jump.  */
1045       edge *pe1;
1046       basic_block bb = create_basic_block (BB_HEAD (e->dest), NULL, ENTRY_BLOCK_PTR);
1047
1048       /* Change the existing edge's source to be the new block, and add
1049          a new edge from the entry block to the new block.  */
1050       e->src = bb;
1051       for (pe1 = &ENTRY_BLOCK_PTR->succ; *pe1; pe1 = &(*pe1)->succ_next)
1052         if (*pe1 == e)
1053           {
1054             *pe1 = e->succ_next;
1055             break;
1056           }
1057       e->succ_next = 0;
1058       bb->succ = e;
1059       make_single_succ_edge (ENTRY_BLOCK_PTR, bb, EDGE_FALLTHRU);
1060     }
1061
1062   if (e->src->succ->succ_next || abnormal_edge_flags)
1063     {
1064       /* Create the new structures.  */
1065
1066       /* If the old block ended with a tablejump, skip its table
1067          by searching forward from there.  Otherwise start searching
1068          forward from the last instruction of the old block.  */
1069       if (!tablejump_p (BB_END (e->src), NULL, &note))
1070         note = BB_END (e->src);
1071
1072       /* Position the new block correctly relative to loop notes.  */
1073       note = last_loop_beg_note (note);
1074       note = NEXT_INSN (note);
1075
1076       jump_block = create_basic_block (note, NULL, e->src);
1077       jump_block->count = e->count;
1078       jump_block->frequency = EDGE_FREQUENCY (e);
1079       jump_block->loop_depth = target->loop_depth;
1080
1081       if (target->global_live_at_start)
1082         {
1083           jump_block->global_live_at_start
1084             = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1085           jump_block->global_live_at_end
1086             = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1087           COPY_REG_SET (jump_block->global_live_at_start,
1088                         target->global_live_at_start);
1089           COPY_REG_SET (jump_block->global_live_at_end,
1090                         target->global_live_at_start);
1091         }
1092
1093       /* Make sure new block ends up in correct hot/cold section.  */
1094
1095       jump_block->partition = e->src->partition;
1096       if (flag_reorder_blocks_and_partition)
1097         {
1098           if (e->src->partition == COLD_PARTITION)
1099             {
1100               rtx bb_note, new_note;
1101               for (bb_note = BB_HEAD (jump_block); 
1102                    bb_note && bb_note != NEXT_INSN (BB_END (jump_block));
1103                    bb_note = NEXT_INSN (bb_note))
1104                 if (GET_CODE (bb_note) == NOTE
1105                     && NOTE_LINE_NUMBER (bb_note) == NOTE_INSN_BASIC_BLOCK)
1106                   break;
1107               new_note = emit_note_after (NOTE_INSN_UNLIKELY_EXECUTED_CODE,
1108                                           bb_note);
1109               NOTE_BASIC_BLOCK (new_note) = jump_block; 
1110               jump_block->partition = COLD_PARTITION;
1111             }
1112           if (GET_CODE (BB_END (jump_block)) == JUMP_INSN
1113               && !any_condjump_p (BB_END (jump_block))
1114               && jump_block->succ->crossing_edge )
1115             REG_NOTES (BB_END (jump_block)) = gen_rtx_EXPR_LIST 
1116               (REG_CROSSING_JUMP, NULL_RTX, 
1117                REG_NOTES (BB_END (jump_block)));
1118         }
1119
1120       /* Wire edge in.  */
1121       new_edge = make_edge (e->src, jump_block, EDGE_FALLTHRU);
1122       new_edge->probability = e->probability;
1123       new_edge->count = e->count;
1124
1125       /* Redirect old edge.  */
1126       redirect_edge_pred (e, jump_block);
1127       e->probability = REG_BR_PROB_BASE;
1128
1129       new_bb = jump_block;
1130     }
1131   else
1132     jump_block = e->src;
1133
1134   e->flags &= ~EDGE_FALLTHRU;
1135   if (target == EXIT_BLOCK_PTR)
1136     {
1137       if (HAVE_return)
1138         emit_jump_insn_after (gen_return (), BB_END (jump_block));
1139       else
1140         abort ();
1141     }
1142   else
1143     {
1144       rtx label = block_label (target);
1145       emit_jump_insn_after (gen_jump (label), BB_END (jump_block));
1146       JUMP_LABEL (BB_END (jump_block)) = label;
1147       LABEL_NUSES (label)++;
1148     }
1149
1150   emit_barrier_after (BB_END (jump_block));
1151   redirect_edge_succ_nodup (e, target);
1152
1153   if (abnormal_edge_flags)
1154     make_edge (src, target, abnormal_edge_flags);
1155
1156   return new_bb;
1157 }
1158
1159 /* Edge E is assumed to be fallthru edge.  Emit needed jump instruction
1160    (and possibly create new basic block) to make edge non-fallthru.
1161    Return newly created BB or NULL if none.  */
1162
1163 basic_block
1164 force_nonfallthru (edge e)
1165 {
1166   return force_nonfallthru_and_redirect (e, e->dest);
1167 }
1168
1169 /* Redirect edge even at the expense of creating new jump insn or
1170    basic block.  Return new basic block if created, NULL otherwise.
1171    Abort if conversion is impossible.  */
1172
1173 static basic_block
1174 rtl_redirect_edge_and_branch_force (edge e, basic_block target)
1175 {
1176   if (redirect_edge_and_branch (e, target)
1177       || e->dest == target)
1178     return NULL;
1179
1180   /* In case the edge redirection failed, try to force it to be non-fallthru
1181      and redirect newly created simplejump.  */
1182   return force_nonfallthru_and_redirect (e, target);
1183 }
1184
1185 /* The given edge should potentially be a fallthru edge.  If that is in
1186    fact true, delete the jump and barriers that are in the way.  */
1187
1188 static void
1189 rtl_tidy_fallthru_edge (edge e)
1190 {
1191   rtx q;
1192   basic_block b = e->src, c = b->next_bb;
1193
1194   /* ??? In a late-running flow pass, other folks may have deleted basic
1195      blocks by nopping out blocks, leaving multiple BARRIERs between here
1196      and the target label. They ought to be chastized and fixed.
1197
1198      We can also wind up with a sequence of undeletable labels between
1199      one block and the next.
1200
1201      So search through a sequence of barriers, labels, and notes for
1202      the head of block C and assert that we really do fall through.  */
1203
1204   for (q = NEXT_INSN (BB_END (b)); q != BB_HEAD (c); q = NEXT_INSN (q))
1205     if (INSN_P (q))
1206       return;
1207
1208   /* Remove what will soon cease being the jump insn from the source block.
1209      If block B consisted only of this single jump, turn it into a deleted
1210      note.  */
1211   q = BB_END (b);
1212   if (GET_CODE (q) == JUMP_INSN
1213       && onlyjump_p (q)
1214       && (any_uncondjump_p (q)
1215           || (b->succ == e && e->succ_next == NULL)))
1216     {
1217 #ifdef HAVE_cc0
1218       /* If this was a conditional jump, we need to also delete
1219          the insn that set cc0.  */
1220       if (any_condjump_p (q) && only_sets_cc0_p (PREV_INSN (q)))
1221         q = PREV_INSN (q);
1222 #endif
1223
1224       q = PREV_INSN (q);
1225
1226       /* We don't want a block to end on a line-number note since that has
1227          the potential of changing the code between -g and not -g.  */
1228       while (GET_CODE (q) == NOTE && NOTE_LINE_NUMBER (q) >= 0)
1229         q = PREV_INSN (q);
1230     }
1231
1232   /* Selectively unlink the sequence.  */
1233   if (q != PREV_INSN (BB_HEAD (c)))
1234     delete_insn_chain (NEXT_INSN (q), PREV_INSN (BB_HEAD (c)));
1235
1236   e->flags |= EDGE_FALLTHRU;
1237 }
1238 \f
1239 /* Helper function for split_edge.  Return true in case edge BB2 to BB1
1240    is back edge of syntactic loop.  */
1241
1242 static bool
1243 back_edge_of_syntactic_loop_p (basic_block bb1, basic_block bb2)
1244 {
1245   rtx insn;
1246   int count = 0;
1247   basic_block bb;
1248
1249   if (bb1 == bb2)
1250     return true;
1251
1252   /* ??? Could we guarantee that bb indices are monotone, so that we could
1253      just compare them?  */
1254   for (bb = bb1; bb && bb != bb2; bb = bb->next_bb)
1255     continue;
1256
1257   if (!bb)
1258     return false;
1259
1260   for (insn = BB_END (bb1); insn != BB_HEAD (bb2) && count >= 0;
1261        insn = NEXT_INSN (insn))
1262     if (GET_CODE (insn) == NOTE)
1263       {
1264         if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
1265           count++;
1266         else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
1267           count--;
1268       }
1269
1270   return count >= 0;
1271 }
1272
1273 /* Should move basic block BB after basic block AFTER.  NIY.  */
1274
1275 static bool
1276 rtl_move_block_after (basic_block bb ATTRIBUTE_UNUSED,
1277                       basic_block after ATTRIBUTE_UNUSED)
1278 {
1279   return false;
1280 }
1281
1282 /* Split a (typically critical) edge.  Return the new block.
1283    Abort on abnormal edges.
1284
1285    ??? The code generally expects to be called on critical edges.
1286    The case of a block ending in an unconditional jump to a
1287    block with multiple predecessors is not handled optimally.  */
1288
1289 static basic_block
1290 rtl_split_edge (edge edge_in)
1291 {
1292   basic_block bb;
1293   rtx before;
1294
1295   /* Abnormal edges cannot be split.  */
1296   if ((edge_in->flags & EDGE_ABNORMAL) != 0)
1297     abort ();
1298
1299   /* We are going to place the new block in front of edge destination.
1300      Avoid existence of fallthru predecessors.  */
1301   if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1302     {
1303       edge e;
1304
1305       for (e = edge_in->dest->pred; e; e = e->pred_next)
1306         if (e->flags & EDGE_FALLTHRU)
1307           break;
1308
1309       if (e)
1310         force_nonfallthru (e);
1311     }
1312
1313   /* Create the basic block note.
1314
1315      Where we place the note can have a noticeable impact on the generated
1316      code.  Consider this cfg:
1317
1318                         E
1319                         |
1320                         0
1321                        / \
1322                    +->1-->2--->E
1323                    |  |
1324                    +--+
1325
1326       If we need to insert an insn on the edge from block 0 to block 1,
1327       we want to ensure the instructions we insert are outside of any
1328       loop notes that physically sit between block 0 and block 1.  Otherwise
1329       we confuse the loop optimizer into thinking the loop is a phony.  */
1330
1331   if (edge_in->dest != EXIT_BLOCK_PTR
1332       && PREV_INSN (BB_HEAD (edge_in->dest))
1333       && GET_CODE (PREV_INSN (BB_HEAD (edge_in->dest))) == NOTE
1334       && (NOTE_LINE_NUMBER (PREV_INSN (BB_HEAD (edge_in->dest)))
1335           == NOTE_INSN_LOOP_BEG)
1336       && !back_edge_of_syntactic_loop_p (edge_in->dest, edge_in->src))
1337     before = PREV_INSN (BB_HEAD (edge_in->dest));
1338   else if (edge_in->dest != EXIT_BLOCK_PTR)
1339     before = BB_HEAD (edge_in->dest);
1340   else
1341     before = NULL_RTX;
1342
1343   bb = create_basic_block (before, NULL, edge_in->dest->prev_bb);
1344
1345   /* ??? This info is likely going to be out of date very soon.  */
1346   if (edge_in->dest->global_live_at_start)
1347     {
1348       bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1349       bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1350       COPY_REG_SET (bb->global_live_at_start,
1351                     edge_in->dest->global_live_at_start);
1352       COPY_REG_SET (bb->global_live_at_end,
1353                     edge_in->dest->global_live_at_start);
1354     }
1355
1356   make_single_succ_edge (bb, edge_in->dest, EDGE_FALLTHRU);
1357
1358   /* For non-fallthru edges, we must adjust the predecessor's
1359      jump instruction to target our new block.  */
1360   if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1361     {
1362       if (!redirect_edge_and_branch (edge_in, bb))
1363         abort ();
1364     }
1365   else
1366     redirect_edge_succ (edge_in, bb);
1367
1368   return bb;
1369 }
1370
1371 /* Queue instructions for insertion on an edge between two basic blocks.
1372    The new instructions and basic blocks (if any) will not appear in the
1373    CFG until commit_edge_insertions is called.  */
1374
1375 void
1376 insert_insn_on_edge (rtx pattern, edge e)
1377 {
1378   /* We cannot insert instructions on an abnormal critical edge.
1379      It will be easier to find the culprit if we die now.  */
1380   if ((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e))
1381     abort ();
1382
1383   if (e->insns == NULL_RTX)
1384     start_sequence ();
1385   else
1386     push_to_sequence (e->insns);
1387
1388   emit_insn (pattern);
1389
1390   e->insns = get_insns ();
1391   end_sequence ();
1392 }
1393
1394 /* Called from safe_insert_insn_on_edge through note_stores, marks live
1395    registers that are killed by the store.  */
1396 static void
1397 mark_killed_regs (rtx reg, rtx set ATTRIBUTE_UNUSED, void *data)
1398 {
1399   regset killed = data;
1400   int regno, i;
1401
1402   if (GET_CODE (reg) == SUBREG)
1403     reg = SUBREG_REG (reg);
1404   if (!REG_P (reg))
1405     return;
1406   regno = REGNO (reg);
1407   if (regno >= FIRST_PSEUDO_REGISTER)
1408     SET_REGNO_REG_SET (killed, regno);
1409   else
1410     {
1411       for (i = 0; i < (int) hard_regno_nregs[regno][GET_MODE (reg)]; i++)
1412         SET_REGNO_REG_SET (killed, regno + i);
1413     }
1414 }
1415
1416 /* Similar to insert_insn_on_edge, tries to put INSN to edge E.  Additionally
1417    it checks whether this will not clobber the registers that are live on the
1418    edge (i.e. it requires liveness information to be up-to-date) and if there
1419    are some, then it tries to save and restore them.  Returns true if
1420    successful.  */
1421 bool
1422 safe_insert_insn_on_edge (rtx insn, edge e)
1423 {
1424   rtx x;
1425   regset_head killed_head;
1426   regset killed = INITIALIZE_REG_SET (killed_head);
1427   rtx save_regs = NULL_RTX;
1428   int regno, noccmode;
1429   enum machine_mode mode;
1430
1431 #ifdef AVOID_CCMODE_COPIES
1432   noccmode = true;
1433 #else
1434   noccmode = false;
1435 #endif
1436
1437   for (x = insn; x; x = NEXT_INSN (x))
1438     if (INSN_P (x))
1439       note_stores (PATTERN (x), mark_killed_regs, killed);
1440   bitmap_operation (killed, killed, e->dest->global_live_at_start,
1441                     BITMAP_AND);
1442
1443   EXECUTE_IF_SET_IN_REG_SET (killed, 0, regno,
1444     {
1445       mode = regno < FIRST_PSEUDO_REGISTER
1446               ? reg_raw_mode[regno]
1447               : GET_MODE (regno_reg_rtx[regno]);
1448       if (mode == VOIDmode)
1449         return false;
1450
1451       if (noccmode && mode == CCmode)
1452         return false;
1453         
1454       save_regs = alloc_EXPR_LIST (0,
1455                                    alloc_EXPR_LIST (0,
1456                                                     gen_reg_rtx (mode),
1457                                                     gen_raw_REG (mode, regno)),
1458                                    save_regs);
1459     });
1460
1461   if (save_regs)
1462     {
1463       rtx from, to;
1464
1465       start_sequence ();
1466       for (x = save_regs; x; x = XEXP (x, 1))
1467         {
1468           from = XEXP (XEXP (x, 0), 1);
1469           to = XEXP (XEXP (x, 0), 0);
1470           emit_move_insn (to, from);
1471         }
1472       emit_insn (insn);
1473       for (x = save_regs; x; x = XEXP (x, 1))
1474         {
1475           from = XEXP (XEXP (x, 0), 0);
1476           to = XEXP (XEXP (x, 0), 1);
1477           emit_move_insn (to, from);
1478         }
1479       insn = get_insns ();
1480       end_sequence ();
1481       free_EXPR_LIST_list (&save_regs);
1482     }
1483   insert_insn_on_edge (insn, e);
1484   
1485   FREE_REG_SET (killed);
1486   return true;
1487 }
1488
1489 /* Update the CFG for the instructions queued on edge E.  */
1490
1491 static void
1492 commit_one_edge_insertion (edge e, int watch_calls)
1493 {
1494   rtx before = NULL_RTX, after = NULL_RTX, insns, tmp, last;
1495   basic_block bb = NULL;
1496
1497   /* Pull the insns off the edge now since the edge might go away.  */
1498   insns = e->insns;
1499   e->insns = NULL_RTX;
1500
1501   /* Special case -- avoid inserting code between call and storing
1502      its return value.  */
1503   if (watch_calls && (e->flags & EDGE_FALLTHRU) && !e->dest->pred->pred_next
1504       && e->src != ENTRY_BLOCK_PTR
1505       && GET_CODE (BB_END (e->src)) == CALL_INSN)
1506     {
1507       rtx next = next_nonnote_insn (BB_END (e->src));
1508
1509       after = BB_HEAD (e->dest);
1510       /* The first insn after the call may be a stack pop, skip it.  */
1511       while (next
1512              && keep_with_call_p (next))
1513         {
1514           after = next;
1515           next = next_nonnote_insn (next);
1516         }
1517       bb = e->dest;
1518     }
1519   if (!before && !after)
1520     {
1521       /* Figure out where to put these things.  If the destination has
1522          one predecessor, insert there.  Except for the exit block.  */
1523       if (e->dest->pred->pred_next == NULL && e->dest != EXIT_BLOCK_PTR)
1524         {
1525           bb = e->dest;
1526
1527           /* Get the location correct wrt a code label, and "nice" wrt
1528              a basic block note, and before everything else.  */
1529           tmp = BB_HEAD (bb);
1530           if (GET_CODE (tmp) == CODE_LABEL)
1531             tmp = NEXT_INSN (tmp);
1532           if (NOTE_INSN_BASIC_BLOCK_P (tmp))
1533             tmp = NEXT_INSN (tmp);
1534           if (tmp 
1535               && GET_CODE (tmp) == NOTE
1536               && NOTE_LINE_NUMBER (tmp) == NOTE_INSN_UNLIKELY_EXECUTED_CODE)
1537             tmp = NEXT_INSN (tmp);
1538           if (tmp == BB_HEAD (bb))
1539             before = tmp;
1540           else if (tmp)
1541             after = PREV_INSN (tmp);
1542           else
1543             after = get_last_insn ();
1544         }
1545
1546       /* If the source has one successor and the edge is not abnormal,
1547          insert there.  Except for the entry block.  */
1548       else if ((e->flags & EDGE_ABNORMAL) == 0
1549                && e->src->succ->succ_next == NULL
1550                && e->src != ENTRY_BLOCK_PTR)
1551         {
1552           bb = e->src;
1553
1554           /* It is possible to have a non-simple jump here.  Consider a target
1555              where some forms of unconditional jumps clobber a register.  This
1556              happens on the fr30 for example.
1557
1558              We know this block has a single successor, so we can just emit
1559              the queued insns before the jump.  */
1560           if (GET_CODE (BB_END (bb)) == JUMP_INSN)
1561             for (before = BB_END (bb);
1562                  GET_CODE (PREV_INSN (before)) == NOTE
1563                  && NOTE_LINE_NUMBER (PREV_INSN (before)) ==
1564                  NOTE_INSN_LOOP_BEG; before = PREV_INSN (before))
1565               ;
1566           else
1567             {
1568               /* We'd better be fallthru, or we've lost track of what's what.  */
1569               if ((e->flags & EDGE_FALLTHRU) == 0)
1570                 abort ();
1571
1572               after = BB_END (bb);
1573             }
1574         }
1575       /* Otherwise we must split the edge.  */
1576       else
1577         {
1578           bb = split_edge (e);
1579           after = BB_END (bb);
1580
1581           /* If we are partitioning hot/cold basic blocks, we must make sure
1582              that the new basic block ends up in the correct section.  */
1583
1584           bb->partition = e->src->partition;
1585           if (flag_reorder_blocks_and_partition
1586               && e->src != ENTRY_BLOCK_PTR
1587               && e->src->partition == COLD_PARTITION)
1588             {
1589               rtx bb_note, new_note, cur_insn;
1590
1591               bb_note = NULL_RTX;
1592               for (cur_insn = BB_HEAD (bb); cur_insn != NEXT_INSN (BB_END (bb));
1593                    cur_insn = NEXT_INSN (cur_insn))
1594                 if (GET_CODE (cur_insn) == NOTE
1595                     && NOTE_LINE_NUMBER (cur_insn) == NOTE_INSN_BASIC_BLOCK)
1596                   {
1597                     bb_note = cur_insn;
1598                     break;
1599                   }
1600
1601               new_note = emit_note_after (NOTE_INSN_UNLIKELY_EXECUTED_CODE,
1602                                           bb_note);
1603               NOTE_BASIC_BLOCK (new_note) = bb;
1604               if (GET_CODE (BB_END (bb)) == JUMP_INSN
1605                   && !any_condjump_p (BB_END (bb))
1606                   && bb->succ->crossing_edge )
1607                 REG_NOTES (BB_END (bb)) = gen_rtx_EXPR_LIST 
1608                   (REG_CROSSING_JUMP, NULL_RTX, REG_NOTES (BB_END (bb)));
1609               if (after == bb_note)
1610                 after = new_note;
1611             }
1612         }
1613     }
1614
1615   /* Now that we've found the spot, do the insertion.  */
1616
1617   if (before)
1618     {
1619       emit_insn_before (insns, before);
1620       last = prev_nonnote_insn (before);
1621     }
1622   else
1623     last = emit_insn_after (insns, after);
1624
1625   if (returnjump_p (last))
1626     {
1627       /* ??? Remove all outgoing edges from BB and add one for EXIT.
1628          This is not currently a problem because this only happens
1629          for the (single) epilogue, which already has a fallthru edge
1630          to EXIT.  */
1631
1632       e = bb->succ;
1633       if (e->dest != EXIT_BLOCK_PTR
1634           || e->succ_next != NULL || (e->flags & EDGE_FALLTHRU) == 0)
1635         abort ();
1636
1637       e->flags &= ~EDGE_FALLTHRU;
1638       emit_barrier_after (last);
1639
1640       if (before)
1641         delete_insn (before);
1642     }
1643   else if (GET_CODE (last) == JUMP_INSN)
1644     abort ();
1645
1646   /* Mark the basic block for find_sub_basic_blocks.  */
1647   bb->aux = &bb->aux;
1648 }
1649
1650 /* Update the CFG for all queued instructions.  */
1651
1652 void
1653 commit_edge_insertions (void)
1654 {
1655   basic_block bb;
1656   sbitmap blocks;
1657   bool changed = false;
1658
1659 #ifdef ENABLE_CHECKING
1660   verify_flow_info ();
1661 #endif
1662
1663   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
1664     {
1665       edge e, next;
1666
1667       for (e = bb->succ; e; e = next)
1668         {
1669           next = e->succ_next;
1670           if (e->insns)
1671             {
1672                changed = true;
1673                commit_one_edge_insertion (e, false);
1674             }
1675         }
1676     }
1677
1678   if (!changed)
1679     return;
1680
1681   blocks = sbitmap_alloc (last_basic_block);
1682   sbitmap_zero (blocks);
1683   FOR_EACH_BB (bb)
1684     if (bb->aux)
1685       {
1686         SET_BIT (blocks, bb->index);
1687         /* Check for forgotten bb->aux values before commit_edge_insertions
1688            call.  */
1689         if (bb->aux != &bb->aux)
1690           abort ();
1691         bb->aux = NULL;
1692       }
1693   find_many_sub_basic_blocks (blocks);
1694   sbitmap_free (blocks);
1695 }
1696 \f
1697 /* Update the CFG for all queued instructions, taking special care of inserting
1698    code on edges between call and storing its return value.  */
1699
1700 void
1701 commit_edge_insertions_watch_calls (void)
1702 {
1703   basic_block bb;
1704   sbitmap blocks;
1705   bool changed = false;
1706
1707 #ifdef ENABLE_CHECKING
1708   verify_flow_info ();
1709 #endif
1710
1711   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
1712     {
1713       edge e, next;
1714
1715       for (e = bb->succ; e; e = next)
1716         {
1717           next = e->succ_next;
1718           if (e->insns)
1719             {
1720               changed = true;
1721               commit_one_edge_insertion (e, true);
1722             }
1723         }
1724     }
1725
1726   if (!changed)
1727     return;
1728
1729   blocks = sbitmap_alloc (last_basic_block);
1730   sbitmap_zero (blocks);
1731   FOR_EACH_BB (bb)
1732     if (bb->aux)
1733       {
1734         SET_BIT (blocks, bb->index);
1735         /* Check for forgotten bb->aux values before commit_edge_insertions
1736            call.  */
1737         if (bb->aux != &bb->aux)
1738           abort ();
1739         bb->aux = NULL;
1740       }
1741   find_many_sub_basic_blocks (blocks);
1742   sbitmap_free (blocks);
1743 }
1744 \f
1745 /* Print out RTL-specific basic block information (live information
1746    at start and end).  */
1747
1748 static void
1749 rtl_dump_bb (basic_block bb, FILE *outf, int indent)
1750 {
1751   rtx insn;
1752   rtx last;
1753   char *s_indent;
1754
1755   s_indent = alloca ((size_t) indent + 1);
1756   memset (s_indent, ' ', (size_t) indent);
1757   s_indent[indent] = '\0';
1758
1759   fprintf (outf, ";;%s Registers live at start: ", s_indent);
1760   dump_regset (bb->global_live_at_start, outf);
1761   putc ('\n', outf);
1762
1763   for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb)); insn != last;
1764        insn = NEXT_INSN (insn))
1765     print_rtl_single (outf, insn);
1766
1767   fprintf (outf, ";;%s Registers live at end: ", s_indent);
1768   dump_regset (bb->global_live_at_end, outf);
1769   putc ('\n', outf);
1770 }
1771 \f
1772 /* Like print_rtl, but also print out live information for the start of each
1773    basic block.  */
1774
1775 void
1776 print_rtl_with_bb (FILE *outf, rtx rtx_first)
1777 {
1778   rtx tmp_rtx;
1779
1780   if (rtx_first == 0)
1781     fprintf (outf, "(nil)\n");
1782   else
1783     {
1784       enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
1785       int max_uid = get_max_uid ();
1786       basic_block *start = xcalloc (max_uid, sizeof (basic_block));
1787       basic_block *end = xcalloc (max_uid, sizeof (basic_block));
1788       enum bb_state *in_bb_p = xcalloc (max_uid, sizeof (enum bb_state));
1789
1790       basic_block bb;
1791
1792       FOR_EACH_BB_REVERSE (bb)
1793         {
1794           rtx x;
1795
1796           start[INSN_UID (BB_HEAD (bb))] = bb;
1797           end[INSN_UID (BB_END (bb))] = bb;
1798           for (x = BB_HEAD (bb); x != NULL_RTX; x = NEXT_INSN (x))
1799             {
1800               enum bb_state state = IN_MULTIPLE_BB;
1801
1802               if (in_bb_p[INSN_UID (x)] == NOT_IN_BB)
1803                 state = IN_ONE_BB;
1804               in_bb_p[INSN_UID (x)] = state;
1805
1806               if (x == BB_END (bb))
1807                 break;
1808             }
1809         }
1810
1811       for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
1812         {
1813           int did_output;
1814
1815           if ((bb = start[INSN_UID (tmp_rtx)]) != NULL)
1816             {
1817               fprintf (outf, ";; Start of basic block %d, registers live:",
1818                        bb->index);
1819               dump_regset (bb->global_live_at_start, outf);
1820               putc ('\n', outf);
1821             }
1822
1823           if (in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB
1824               && GET_CODE (tmp_rtx) != NOTE
1825               && GET_CODE (tmp_rtx) != BARRIER)
1826             fprintf (outf, ";; Insn is not within a basic block\n");
1827           else if (in_bb_p[INSN_UID (tmp_rtx)] == IN_MULTIPLE_BB)
1828             fprintf (outf, ";; Insn is in multiple basic blocks\n");
1829
1830           did_output = print_rtl_single (outf, tmp_rtx);
1831
1832           if ((bb = end[INSN_UID (tmp_rtx)]) != NULL)
1833             {
1834               fprintf (outf, ";; End of basic block %d, registers live:\n",
1835                        bb->index);
1836               dump_regset (bb->global_live_at_end, outf);
1837               putc ('\n', outf);
1838             }
1839
1840           if (did_output)
1841             putc ('\n', outf);
1842         }
1843
1844       free (start);
1845       free (end);
1846       free (in_bb_p);
1847     }
1848
1849   if (current_function_epilogue_delay_list != 0)
1850     {
1851       fprintf (outf, "\n;; Insns in epilogue delay list:\n\n");
1852       for (tmp_rtx = current_function_epilogue_delay_list; tmp_rtx != 0;
1853            tmp_rtx = XEXP (tmp_rtx, 1))
1854         print_rtl_single (outf, XEXP (tmp_rtx, 0));
1855     }
1856 }
1857 \f
1858 void
1859 update_br_prob_note (basic_block bb)
1860 {
1861   rtx note;
1862   if (GET_CODE (BB_END (bb)) != JUMP_INSN)
1863     return;
1864   note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX);
1865   if (!note || INTVAL (XEXP (note, 0)) == BRANCH_EDGE (bb)->probability)
1866     return;
1867   XEXP (note, 0) = GEN_INT (BRANCH_EDGE (bb)->probability);
1868 }
1869 \f
1870 /* Verify the CFG and RTL consistency common for both underlying RTL and
1871    cfglayout RTL.
1872
1873    Currently it does following checks:
1874
1875    - test head/end pointers
1876    - overlapping of basic blocks
1877    - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note)
1878    - tails of basic blocks (ensure that boundary is necessary)
1879    - scans body of the basic block for JUMP_INSN, CODE_LABEL
1880      and NOTE_INSN_BASIC_BLOCK
1881    - verify that no fall_thru edge crosses hot/cold partition boundaries
1882
1883    In future it can be extended check a lot of other stuff as well
1884    (reachability of basic blocks, life information, etc. etc.).  */
1885
1886 static int
1887 rtl_verify_flow_info_1 (void)
1888 {
1889   const int max_uid = get_max_uid ();
1890   rtx last_head = get_last_insn ();
1891   basic_block *bb_info;
1892   rtx x;
1893   int err = 0;
1894   basic_block bb, last_bb_seen;
1895
1896   bb_info = xcalloc (max_uid, sizeof (basic_block));
1897
1898   /* Check bb chain & numbers.  */
1899   last_bb_seen = ENTRY_BLOCK_PTR;
1900
1901   FOR_EACH_BB_REVERSE (bb)
1902     {
1903       rtx head = BB_HEAD (bb);
1904       rtx end = BB_END (bb);
1905
1906       /* Verify the end of the basic block is in the INSN chain.  */
1907       for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
1908         if (x == end)
1909           break;
1910
1911       if (!x)
1912         {
1913           error ("end insn %d for block %d not found in the insn stream",
1914                  INSN_UID (end), bb->index);
1915           err = 1;
1916         }
1917
1918       /* Work backwards from the end to the head of the basic block
1919          to verify the head is in the RTL chain.  */
1920       for (; x != NULL_RTX; x = PREV_INSN (x))
1921         {
1922           /* While walking over the insn chain, verify insns appear
1923              in only one basic block and initialize the BB_INFO array
1924              used by other passes.  */
1925           if (bb_info[INSN_UID (x)] != NULL)
1926             {
1927               error ("insn %d is in multiple basic blocks (%d and %d)",
1928                      INSN_UID (x), bb->index, bb_info[INSN_UID (x)]->index);
1929               err = 1;
1930             }
1931
1932           bb_info[INSN_UID (x)] = bb;
1933
1934           if (x == head)
1935             break;
1936         }
1937       if (!x)
1938         {
1939           error ("head insn %d for block %d not found in the insn stream",
1940                  INSN_UID (head), bb->index);
1941           err = 1;
1942         }
1943
1944       last_head = x;
1945     }
1946
1947   /* Now check the basic blocks (boundaries etc.) */
1948   FOR_EACH_BB_REVERSE (bb)
1949     {
1950       int n_fallthru = 0, n_eh = 0, n_call = 0, n_abnormal = 0, n_branch = 0;
1951       edge e, fallthru = NULL;
1952       rtx note;
1953
1954       if (INSN_P (BB_END (bb))
1955           && (note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX))
1956           && bb->succ && bb->succ->succ_next
1957           && any_condjump_p (BB_END (bb)))
1958         {
1959           if (INTVAL (XEXP (note, 0)) != BRANCH_EDGE (bb)->probability)
1960             {
1961               error ("verify_flow_info: REG_BR_PROB does not match cfg %wi %i",
1962                      INTVAL (XEXP (note, 0)), BRANCH_EDGE (bb)->probability);
1963               err = 1;
1964             }
1965         }
1966       for (e = bb->succ; e; e = e->succ_next)
1967         {
1968           if (e->flags & EDGE_FALLTHRU)
1969             {
1970               n_fallthru++, fallthru = e;
1971               if (e->crossing_edge)
1972                 { 
1973                   error ("Fallthru edge crosses section boundary (bb %i)",
1974                          e->src->index);
1975                   err = 1;
1976                 }
1977             }
1978
1979           if ((e->flags & ~(EDGE_DFS_BACK
1980                             | EDGE_CAN_FALLTHRU
1981                             | EDGE_IRREDUCIBLE_LOOP
1982                             | EDGE_LOOP_EXIT)) == 0)
1983             n_branch++;
1984
1985           if (e->flags & EDGE_ABNORMAL_CALL)
1986             n_call++;
1987
1988           if (e->flags & EDGE_EH)
1989             n_eh++;
1990           else if (e->flags & EDGE_ABNORMAL)
1991             n_abnormal++;
1992         }
1993
1994       if (n_eh && GET_CODE (PATTERN (BB_END (bb))) != RESX
1995           && !find_reg_note (BB_END (bb), REG_EH_REGION, NULL_RTX))
1996         {
1997           error ("Missing REG_EH_REGION note in the end of bb %i", bb->index);
1998           err = 1;
1999         }
2000       if (n_branch
2001           && (GET_CODE (BB_END (bb)) != JUMP_INSN
2002               || (n_branch > 1 && (any_uncondjump_p (BB_END (bb))
2003                                    || any_condjump_p (BB_END (bb))))))
2004         {
2005           error ("Too many outgoing branch edges from bb %i", bb->index);
2006           err = 1;
2007         }
2008       if (n_fallthru && any_uncondjump_p (BB_END (bb)))
2009         {
2010           error ("Fallthru edge after unconditional jump %i", bb->index);
2011           err = 1;
2012         }
2013       if (n_branch != 1 && any_uncondjump_p (BB_END (bb)))
2014         {
2015           error ("Wrong amount of branch edges after unconditional jump %i", bb->index);
2016           err = 1;
2017         }
2018       if (n_branch != 1 && any_condjump_p (BB_END (bb))
2019           && JUMP_LABEL (BB_END (bb)) != BB_HEAD (fallthru->dest))
2020         {
2021           error ("Wrong amount of branch edges after conditional jump %i", bb->index);
2022           err = 1;
2023         }
2024       if (n_call && GET_CODE (BB_END (bb)) != CALL_INSN)
2025         {
2026           error ("Call edges for non-call insn in bb %i", bb->index);
2027           err = 1;
2028         }
2029       if (n_abnormal
2030           && (GET_CODE (BB_END (bb)) != CALL_INSN && n_call != n_abnormal)
2031           && (GET_CODE (BB_END (bb)) != JUMP_INSN
2032               || any_condjump_p (BB_END (bb))
2033               || any_uncondjump_p (BB_END (bb))))
2034         {
2035           error ("Abnormal edges for no purpose in bb %i", bb->index);
2036           err = 1;
2037         }
2038
2039       for (x = BB_HEAD (bb); x != NEXT_INSN (BB_END (bb)); x = NEXT_INSN (x))
2040         if (BLOCK_FOR_INSN (x) != bb)
2041           {
2042             debug_rtx (x);
2043             if (! BLOCK_FOR_INSN (x))
2044               error
2045                 ("insn %d inside basic block %d but block_for_insn is NULL",
2046                  INSN_UID (x), bb->index);
2047             else
2048               error
2049                 ("insn %d inside basic block %d but block_for_insn is %i",
2050                  INSN_UID (x), bb->index, BLOCK_FOR_INSN (x)->index);
2051
2052             err = 1;
2053           }
2054
2055       /* OK pointers are correct.  Now check the header of basic
2056          block.  It ought to contain optional CODE_LABEL followed
2057          by NOTE_BASIC_BLOCK.  */
2058       x = BB_HEAD (bb);
2059       if (GET_CODE (x) == CODE_LABEL)
2060         {
2061           if (BB_END (bb) == x)
2062             {
2063               error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
2064                      bb->index);
2065               err = 1;
2066             }
2067
2068           x = NEXT_INSN (x);
2069         }
2070
2071       if (!NOTE_INSN_BASIC_BLOCK_P (x) || NOTE_BASIC_BLOCK (x) != bb)
2072         {
2073           error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
2074                  bb->index);
2075           err = 1;
2076         }
2077
2078       if (BB_END (bb) == x)
2079         /* Do checks for empty blocks her. e */
2080         ;
2081       else
2082         for (x = NEXT_INSN (x); x; x = NEXT_INSN (x))
2083           {
2084             if (NOTE_INSN_BASIC_BLOCK_P (x))
2085               {
2086                 error ("NOTE_INSN_BASIC_BLOCK %d in middle of basic block %d",
2087                        INSN_UID (x), bb->index);
2088                 err = 1;
2089               }
2090
2091             if (x == BB_END (bb))
2092               break;
2093
2094             if (control_flow_insn_p (x))
2095               {
2096                 error ("in basic block %d:", bb->index);
2097                 fatal_insn ("flow control insn inside a basic block", x);
2098               }
2099           }
2100     }
2101
2102   /* Clean up.  */
2103   free (bb_info);
2104   return err;
2105 }
2106
2107 /* Verify the CFG and RTL consistency common for both underlying RTL and
2108    cfglayout RTL.
2109
2110    Currently it does following checks:
2111    - all checks of rtl_verify_flow_info_1
2112    - check that all insns are in the basic blocks
2113      (except the switch handling code, barriers and notes)
2114    - check that all returns are followed by barriers
2115    - check that all fallthru edge points to the adjacent blocks.  */
2116 static int
2117 rtl_verify_flow_info (void)
2118 {
2119   basic_block bb;
2120   int err = rtl_verify_flow_info_1 ();
2121   rtx x;
2122   int num_bb_notes;
2123   const rtx rtx_first = get_insns ();
2124   basic_block last_bb_seen = ENTRY_BLOCK_PTR, curr_bb = NULL;
2125
2126   FOR_EACH_BB_REVERSE (bb)
2127     {
2128       edge e;
2129       for (e = bb->succ; e; e = e->succ_next)
2130         if (e->flags & EDGE_FALLTHRU)
2131           break;
2132       if (!e)
2133         {
2134           rtx insn;
2135
2136           /* Ensure existence of barrier in BB with no fallthru edges.  */
2137           for (insn = BB_END (bb); !insn || GET_CODE (insn) != BARRIER;
2138                insn = NEXT_INSN (insn))
2139             if (!insn
2140                 || (GET_CODE (insn) == NOTE
2141                     && NOTE_LINE_NUMBER (insn) == NOTE_INSN_BASIC_BLOCK))
2142                 {
2143                   error ("missing barrier after block %i", bb->index);
2144                   err = 1;
2145                   break;
2146                 }
2147         }
2148       else if (e->src != ENTRY_BLOCK_PTR
2149                && e->dest != EXIT_BLOCK_PTR)
2150         {
2151           rtx insn;
2152
2153           if (e->src->next_bb != e->dest)
2154             {
2155               error
2156                 ("verify_flow_info: Incorrect blocks for fallthru %i->%i",
2157                  e->src->index, e->dest->index);
2158               err = 1;
2159             }
2160           else
2161             for (insn = NEXT_INSN (BB_END (e->src)); insn != BB_HEAD (e->dest);
2162                  insn = NEXT_INSN (insn))
2163               if (GET_CODE (insn) == BARRIER
2164 #ifndef CASE_DROPS_THROUGH
2165                   || INSN_P (insn)
2166 #else
2167                   || (INSN_P (insn) && ! JUMP_TABLE_DATA_P (insn))
2168 #endif
2169                   )
2170                 {
2171                   error ("verify_flow_info: Incorrect fallthru %i->%i",
2172                          e->src->index, e->dest->index);
2173                   fatal_insn ("wrong insn in the fallthru edge", insn);
2174                   err = 1;
2175                 }
2176         }
2177     }
2178
2179   num_bb_notes = 0;
2180   last_bb_seen = ENTRY_BLOCK_PTR;
2181
2182   for (x = rtx_first; x; x = NEXT_INSN (x))
2183     {
2184       if (NOTE_INSN_BASIC_BLOCK_P (x))
2185         {
2186           bb = NOTE_BASIC_BLOCK (x);
2187
2188           num_bb_notes++;
2189           if (bb != last_bb_seen->next_bb)
2190             internal_error ("basic blocks not laid down consecutively");
2191
2192           curr_bb = last_bb_seen = bb;
2193         }
2194
2195       if (!curr_bb)
2196         {
2197           switch (GET_CODE (x))
2198             {
2199             case BARRIER:
2200             case NOTE:
2201               break;
2202
2203             case CODE_LABEL:
2204               /* An addr_vec is placed outside any basic block.  */
2205               if (NEXT_INSN (x)
2206                   && GET_CODE (NEXT_INSN (x)) == JUMP_INSN
2207                   && (GET_CODE (PATTERN (NEXT_INSN (x))) == ADDR_DIFF_VEC
2208                       || GET_CODE (PATTERN (NEXT_INSN (x))) == ADDR_VEC))
2209                 x = NEXT_INSN (x);
2210
2211               /* But in any case, non-deletable labels can appear anywhere.  */
2212               break;
2213
2214             default:
2215               fatal_insn ("insn outside basic block", x);
2216             }
2217         }
2218
2219       if (INSN_P (x)
2220           && GET_CODE (x) == JUMP_INSN
2221           && returnjump_p (x) && ! condjump_p (x)
2222           && ! (NEXT_INSN (x) && GET_CODE (NEXT_INSN (x)) == BARRIER))
2223             fatal_insn ("return not followed by barrier", x);
2224       if (curr_bb && x == BB_END (curr_bb))
2225         curr_bb = NULL;
2226     }
2227
2228   if (num_bb_notes != n_basic_blocks)
2229     internal_error
2230       ("number of bb notes in insn chain (%d) != n_basic_blocks (%d)",
2231        num_bb_notes, n_basic_blocks);
2232
2233    return err;
2234 }
2235 \f
2236 /* Assume that the preceding pass has possibly eliminated jump instructions
2237    or converted the unconditional jumps.  Eliminate the edges from CFG.
2238    Return true if any edges are eliminated.  */
2239
2240 bool
2241 purge_dead_edges (basic_block bb)
2242 {
2243   edge e, next;
2244   rtx insn = BB_END (bb), note;
2245   bool purged = false;
2246
2247   /* If this instruction cannot trap, remove REG_EH_REGION notes.  */
2248   if (GET_CODE (insn) == INSN
2249       && (note = find_reg_note (insn, REG_EH_REGION, NULL)))
2250     {
2251       rtx eqnote;
2252
2253       if (! may_trap_p (PATTERN (insn))
2254           || ((eqnote = find_reg_equal_equiv_note (insn))
2255               && ! may_trap_p (XEXP (eqnote, 0))))
2256         remove_note (insn, note);
2257     }
2258
2259   /* Cleanup abnormal edges caused by exceptions or non-local gotos.  */
2260   for (e = bb->succ; e; e = next)
2261     {
2262       next = e->succ_next;
2263       if (e->flags & EDGE_EH)
2264         {
2265           if (can_throw_internal (BB_END (bb)))
2266             continue;
2267         }
2268       else if (e->flags & EDGE_ABNORMAL_CALL)
2269         {
2270           if (GET_CODE (BB_END (bb)) == CALL_INSN
2271               && (! (note = find_reg_note (insn, REG_EH_REGION, NULL))
2272                   || INTVAL (XEXP (note, 0)) >= 0))
2273             continue;
2274         }
2275       else
2276         continue;
2277
2278       remove_edge (e);
2279       bb->flags |= BB_DIRTY;
2280       purged = true;
2281     }
2282
2283   if (GET_CODE (insn) == JUMP_INSN)
2284     {
2285       rtx note;
2286       edge b,f;
2287
2288       /* We do care only about conditional jumps and simplejumps.  */
2289       if (!any_condjump_p (insn)
2290           && !returnjump_p (insn)
2291           && !simplejump_p (insn))
2292         return purged;
2293
2294       /* Branch probability/prediction notes are defined only for
2295          condjumps.  We've possibly turned condjump into simplejump.  */
2296       if (simplejump_p (insn))
2297         {
2298           note = find_reg_note (insn, REG_BR_PROB, NULL);
2299           if (note)
2300             remove_note (insn, note);
2301           while ((note = find_reg_note (insn, REG_BR_PRED, NULL)))
2302             remove_note (insn, note);
2303         }
2304
2305       for (e = bb->succ; e; e = next)
2306         {
2307           next = e->succ_next;
2308
2309           /* Avoid abnormal flags to leak from computed jumps turned
2310              into simplejumps.  */
2311
2312           e->flags &= ~EDGE_ABNORMAL;
2313
2314           /* See if this edge is one we should keep.  */
2315           if ((e->flags & EDGE_FALLTHRU) && any_condjump_p (insn))
2316             /* A conditional jump can fall through into the next
2317                block, so we should keep the edge.  */
2318             continue;
2319           else if (e->dest != EXIT_BLOCK_PTR
2320                    && BB_HEAD (e->dest) == JUMP_LABEL (insn))
2321             /* If the destination block is the target of the jump,
2322                keep the edge.  */
2323             continue;
2324           else if (e->dest == EXIT_BLOCK_PTR && returnjump_p (insn))
2325             /* If the destination block is the exit block, and this
2326                instruction is a return, then keep the edge.  */
2327             continue;
2328           else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
2329             /* Keep the edges that correspond to exceptions thrown by
2330                this instruction and rematerialize the EDGE_ABNORMAL
2331                flag we just cleared above.  */
2332             {
2333               e->flags |= EDGE_ABNORMAL;
2334               continue;
2335             }
2336
2337           /* We do not need this edge.  */
2338           bb->flags |= BB_DIRTY;
2339           purged = true;
2340           remove_edge (e);
2341         }
2342
2343       if (!bb->succ || !purged)
2344         return purged;
2345
2346       if (dump_file)
2347         fprintf (dump_file, "Purged edges from bb %i\n", bb->index);
2348
2349       if (!optimize)
2350         return purged;
2351
2352       /* Redistribute probabilities.  */
2353       if (!bb->succ->succ_next)
2354         {
2355           bb->succ->probability = REG_BR_PROB_BASE;
2356           bb->succ->count = bb->count;
2357         }
2358       else
2359         {
2360           note = find_reg_note (insn, REG_BR_PROB, NULL);
2361           if (!note)
2362             return purged;
2363
2364           b = BRANCH_EDGE (bb);
2365           f = FALLTHRU_EDGE (bb);
2366           b->probability = INTVAL (XEXP (note, 0));
2367           f->probability = REG_BR_PROB_BASE - b->probability;
2368           b->count = bb->count * b->probability / REG_BR_PROB_BASE;
2369           f->count = bb->count * f->probability / REG_BR_PROB_BASE;
2370         }
2371
2372       return purged;
2373     }
2374   else if (GET_CODE (insn) == CALL_INSN && SIBLING_CALL_P (insn))
2375     {
2376       /* First, there should not be any EH or ABCALL edges resulting
2377          from non-local gotos and the like.  If there were, we shouldn't
2378          have created the sibcall in the first place.  Second, there
2379          should of course never have been a fallthru edge.  */
2380       if (!bb->succ || bb->succ->succ_next)
2381         abort ();
2382       if (bb->succ->flags != (EDGE_SIBCALL | EDGE_ABNORMAL))
2383         abort ();
2384
2385       return 0;
2386     }
2387
2388   /* If we don't see a jump insn, we don't know exactly why the block would
2389      have been broken at this point.  Look for a simple, non-fallthru edge,
2390      as these are only created by conditional branches.  If we find such an
2391      edge we know that there used to be a jump here and can then safely
2392      remove all non-fallthru edges.  */
2393   for (e = bb->succ; e && (e->flags & (EDGE_COMPLEX | EDGE_FALLTHRU));
2394        e = e->succ_next)
2395     ;
2396
2397   if (!e)
2398     return purged;
2399
2400   for (e = bb->succ; e; e = next)
2401     {
2402       next = e->succ_next;
2403       if (!(e->flags & EDGE_FALLTHRU))
2404         {
2405           bb->flags |= BB_DIRTY;
2406           remove_edge (e);
2407           purged = true;
2408         }
2409     }
2410
2411   if (!bb->succ || bb->succ->succ_next)
2412     abort ();
2413
2414   bb->succ->probability = REG_BR_PROB_BASE;
2415   bb->succ->count = bb->count;
2416
2417   if (dump_file)
2418     fprintf (dump_file, "Purged non-fallthru edges from bb %i\n",
2419              bb->index);
2420   return purged;
2421 }
2422
2423 /* Search all basic blocks for potentially dead edges and purge them.  Return
2424    true if some edge has been eliminated.  */
2425
2426 bool
2427 purge_all_dead_edges (int update_life_p)
2428 {
2429   int purged = false;
2430   sbitmap blocks = 0;
2431   basic_block bb;
2432
2433   if (update_life_p)
2434     {
2435       blocks = sbitmap_alloc (last_basic_block);
2436       sbitmap_zero (blocks);
2437     }
2438
2439   FOR_EACH_BB (bb)
2440     {
2441       bool purged_here = purge_dead_edges (bb);
2442
2443       purged |= purged_here;
2444       if (purged_here && update_life_p)
2445         SET_BIT (blocks, bb->index);
2446     }
2447
2448   if (update_life_p && purged)
2449     update_life_info (blocks, UPDATE_LIFE_GLOBAL,
2450                       PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
2451                       | PROP_KILL_DEAD_CODE);
2452
2453   if (update_life_p)
2454     sbitmap_free (blocks);
2455   return purged;
2456 }
2457
2458 /* Same as split_block but update cfg_layout structures.  */
2459
2460 static basic_block
2461 cfg_layout_split_block (basic_block bb, void *insnp)
2462 {
2463   rtx insn = insnp;
2464   basic_block new_bb = rtl_split_block (bb, insn);
2465
2466   new_bb->rbi->footer = bb->rbi->footer;
2467   bb->rbi->footer = NULL;
2468
2469   return new_bb;
2470 }
2471
2472
2473 /* Redirect Edge to DEST.  */
2474 static bool
2475 cfg_layout_redirect_edge_and_branch (edge e, basic_block dest)
2476 {
2477   basic_block src = e->src;
2478   bool ret;
2479
2480   if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
2481     return false;
2482
2483   if (e->dest == dest)
2484     return true;
2485
2486   if (e->src != ENTRY_BLOCK_PTR
2487       && try_redirect_by_replacing_jump (e, dest, true))
2488     {
2489       src->flags |= BB_DIRTY;
2490       return true;
2491     }
2492
2493   if (e->src == ENTRY_BLOCK_PTR
2494       && (e->flags & EDGE_FALLTHRU) && !(e->flags & EDGE_COMPLEX))
2495     {
2496       if (dump_file)
2497         fprintf (dump_file, "Redirecting entry edge from bb %i to %i\n",
2498                  e->src->index, dest->index);
2499
2500       e->src->flags |= BB_DIRTY;
2501       redirect_edge_succ (e, dest);
2502       return true;
2503     }
2504
2505   /* Redirect_edge_and_branch may decide to turn branch into fallthru edge
2506      in the case the basic block appears to be in sequence.  Avoid this
2507      transformation.  */
2508
2509   if (e->flags & EDGE_FALLTHRU)
2510     {
2511       /* Redirect any branch edges unified with the fallthru one.  */
2512       if (GET_CODE (BB_END (src)) == JUMP_INSN
2513           && label_is_jump_target_p (BB_HEAD (e->dest),
2514                                      BB_END (src)))
2515         {
2516           if (dump_file)
2517             fprintf (dump_file, "Fallthru edge unified with branch "
2518                      "%i->%i redirected to %i\n",
2519                      e->src->index, e->dest->index, dest->index);
2520           e->flags &= ~EDGE_FALLTHRU;
2521           if (!redirect_branch_edge (e, dest))
2522             abort ();
2523           e->flags |= EDGE_FALLTHRU;
2524           e->src->flags |= BB_DIRTY;
2525           return true;
2526         }
2527       /* In case we are redirecting fallthru edge to the branch edge
2528          of conditional jump, remove it.  */
2529       if (src->succ->succ_next
2530           && !src->succ->succ_next->succ_next)
2531         {
2532           edge s = e->succ_next ? e->succ_next : src->succ;
2533           if (s->dest == dest
2534               && any_condjump_p (BB_END (src))
2535               && onlyjump_p (BB_END (src)))
2536             delete_insn (BB_END (src));
2537         }
2538
2539       if (dump_file)
2540         fprintf (dump_file, "Fallthru edge %i->%i redirected to %i\n",
2541                  e->src->index, e->dest->index, dest->index);
2542       redirect_edge_succ_nodup (e, dest);
2543
2544       ret = true;
2545     }
2546   else
2547     ret = redirect_branch_edge (e, dest);
2548
2549   /* We don't want simplejumps in the insn stream during cfglayout.  */
2550   if (simplejump_p (BB_END (src)))
2551     abort ();
2552
2553   src->flags |= BB_DIRTY;
2554   return ret;
2555 }
2556
2557 /* Simple wrapper as we always can redirect fallthru edges.  */
2558 static basic_block
2559 cfg_layout_redirect_edge_and_branch_force (edge e, basic_block dest)
2560 {
2561   if (!cfg_layout_redirect_edge_and_branch (e, dest))
2562     abort ();
2563   return NULL;
2564 }
2565
2566 /* Same as delete_basic_block but update cfg_layout structures.  */
2567
2568 static void
2569 cfg_layout_delete_block (basic_block bb)
2570 {
2571   rtx insn, next, prev = PREV_INSN (BB_HEAD (bb)), *to, remaints;
2572
2573   if (bb->rbi->header)
2574     {
2575       next = BB_HEAD (bb);
2576       if (prev)
2577         NEXT_INSN (prev) = bb->rbi->header;
2578       else
2579         set_first_insn (bb->rbi->header);
2580       PREV_INSN (bb->rbi->header) = prev;
2581       insn = bb->rbi->header;
2582       while (NEXT_INSN (insn))
2583         insn = NEXT_INSN (insn);
2584       NEXT_INSN (insn) = next;
2585       PREV_INSN (next) = insn;
2586     }
2587   next = NEXT_INSN (BB_END (bb));
2588   if (bb->rbi->footer)
2589     {
2590       insn = bb->rbi->footer;
2591       while (insn)
2592         {
2593           if (GET_CODE (insn) == BARRIER)
2594             {
2595               if (PREV_INSN (insn))
2596                 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2597               else
2598                 bb->rbi->footer = NEXT_INSN (insn);
2599               if (NEXT_INSN (insn))
2600                 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2601             }
2602           if (GET_CODE (insn) == CODE_LABEL)
2603             break;
2604           insn = NEXT_INSN (insn);
2605         }
2606       if (bb->rbi->footer)
2607         {
2608           insn = BB_END (bb);
2609           NEXT_INSN (insn) = bb->rbi->footer;
2610           PREV_INSN (bb->rbi->footer) = insn;
2611           while (NEXT_INSN (insn))
2612             insn = NEXT_INSN (insn);
2613           NEXT_INSN (insn) = next;
2614           if (next)
2615             PREV_INSN (next) = insn;
2616           else
2617             set_last_insn (insn);
2618         }
2619     }
2620   if (bb->next_bb != EXIT_BLOCK_PTR)
2621     to = &bb->next_bb->rbi->header;
2622   else
2623     to = &cfg_layout_function_footer;
2624   rtl_delete_block (bb);
2625
2626   if (prev)
2627     prev = NEXT_INSN (prev);
2628   else
2629     prev = get_insns ();
2630   if (next)
2631     next = PREV_INSN (next);
2632   else
2633     next = get_last_insn ();
2634
2635   if (next && NEXT_INSN (next) != prev)
2636     {
2637       remaints = unlink_insn_chain (prev, next);
2638       insn = remaints;
2639       while (NEXT_INSN (insn))
2640         insn = NEXT_INSN (insn);
2641       NEXT_INSN (insn) = *to;
2642       if (*to)
2643         PREV_INSN (*to) = insn;
2644       *to = remaints;
2645     }
2646 }
2647
2648 /* Return true when blocks A and B can be safely merged.  */
2649 static bool
2650 cfg_layout_can_merge_blocks_p (basic_block a, basic_block b)
2651 {
2652   bool partitions_ok = true;
2653
2654   /* If we are partitioning hot/cold basic blocks, we don't want to
2655      mess up unconditional or indirect jumps that cross between hot
2656      and cold sections.  */
2657   
2658   if (flag_reorder_blocks_and_partition
2659       && (find_reg_note (BB_END (a), REG_CROSSING_JUMP, NULL_RTX)
2660           || find_reg_note (BB_END (b), REG_CROSSING_JUMP, NULL_RTX)
2661           || a->partition != b->partition))
2662     partitions_ok = false;
2663
2664   /* There must be exactly one edge in between the blocks.  */
2665   return (a->succ && !a->succ->succ_next && a->succ->dest == b
2666           && !b->pred->pred_next && a != b
2667           /* Must be simple edge.  */
2668           && !(a->succ->flags & EDGE_COMPLEX)
2669           && partitions_ok
2670           && a != ENTRY_BLOCK_PTR && b != EXIT_BLOCK_PTR
2671           /* If the jump insn has side effects,
2672              we can't kill the edge.  */
2673           && (GET_CODE (BB_END (a)) != JUMP_INSN
2674               || (reload_completed
2675                   ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
2676 }
2677
2678 /* Merge block A and B, abort when it is not possible.  */
2679 static void
2680 cfg_layout_merge_blocks (basic_block a, basic_block b)
2681 {
2682 #ifdef ENABLE_CHECKING
2683   if (!cfg_layout_can_merge_blocks_p (a, b))
2684     abort ();
2685 #endif
2686
2687   /* If there was a CODE_LABEL beginning B, delete it.  */
2688   if (GET_CODE (BB_HEAD (b)) == CODE_LABEL)
2689     delete_insn (BB_HEAD (b));
2690
2691   /* We should have fallthru edge in a, or we can do dummy redirection to get
2692      it cleaned up.  */
2693   if (GET_CODE (BB_END (a)) == JUMP_INSN)
2694     try_redirect_by_replacing_jump (a->succ, b, true);
2695   if (GET_CODE (BB_END (a)) == JUMP_INSN)
2696     abort ();
2697
2698   /* Possible line number notes should appear in between.  */
2699   if (b->rbi->header)
2700     {
2701       rtx first = BB_END (a), last;
2702
2703       last = emit_insn_after (b->rbi->header, BB_END (a));
2704       delete_insn_chain (NEXT_INSN (first), last);
2705       b->rbi->header = NULL;
2706     }
2707
2708   /* In the case basic blocks are not adjacent, move them around.  */
2709   if (NEXT_INSN (BB_END (a)) != BB_HEAD (b))
2710     {
2711       rtx first = unlink_insn_chain (BB_HEAD (b), BB_END (b));
2712
2713       emit_insn_after (first, BB_END (a));
2714       /* Skip possible DELETED_LABEL insn.  */
2715       if (!NOTE_INSN_BASIC_BLOCK_P (first))
2716         first = NEXT_INSN (first);
2717       if (!NOTE_INSN_BASIC_BLOCK_P (first))
2718         abort ();
2719       BB_HEAD (b) = NULL;
2720       delete_insn (first);
2721     }
2722   /* Otherwise just re-associate the instructions.  */
2723   else
2724     {
2725       rtx insn;
2726
2727       for (insn = BB_HEAD (b);
2728            insn != NEXT_INSN (BB_END (b));
2729            insn = NEXT_INSN (insn))
2730         set_block_for_insn (insn, a);
2731       insn = BB_HEAD (b);
2732       /* Skip possible DELETED_LABEL insn.  */
2733       if (!NOTE_INSN_BASIC_BLOCK_P (insn))
2734         insn = NEXT_INSN (insn);
2735       if (!NOTE_INSN_BASIC_BLOCK_P (insn))
2736         abort ();
2737       BB_HEAD (b) = NULL;
2738       BB_END (a) = BB_END (b);
2739       delete_insn (insn);
2740     }
2741
2742   /* Possible tablejumps and barriers should appear after the block.  */
2743   if (b->rbi->footer)
2744     {
2745       if (!a->rbi->footer)
2746         a->rbi->footer = b->rbi->footer;
2747       else
2748         {
2749           rtx last = a->rbi->footer;
2750
2751           while (NEXT_INSN (last))
2752             last = NEXT_INSN (last);
2753           NEXT_INSN (last) = b->rbi->footer;
2754           PREV_INSN (b->rbi->footer) = last;
2755         }
2756       b->rbi->footer = NULL;
2757     }
2758
2759   if (dump_file)
2760     fprintf (dump_file, "Merged blocks %d and %d.\n",
2761              a->index, b->index);
2762 }
2763
2764 /* Split edge E.  */
2765
2766 static basic_block
2767 cfg_layout_split_edge (edge e)
2768 {
2769   edge new_e;
2770   basic_block new_bb =
2771     create_basic_block (e->src != ENTRY_BLOCK_PTR
2772                         ? NEXT_INSN (BB_END (e->src)) : get_insns (),
2773                         NULL_RTX, e->src);
2774
2775   new_e = make_edge (new_bb, e->dest, EDGE_FALLTHRU);
2776   redirect_edge_and_branch_force (e, new_bb);
2777
2778   return new_bb;
2779 }
2780
2781 /* Do postprocessing after making a forwarder block joined by edge FALLTHRU.  */
2782
2783 static void
2784 rtl_make_forwarder_block (edge fallthru ATTRIBUTE_UNUSED)
2785 {
2786 }
2787
2788 /* Implementation of CFG manipulation for linearized RTL.  */
2789 struct cfg_hooks rtl_cfg_hooks = {
2790   "rtl",
2791   rtl_verify_flow_info,
2792   rtl_dump_bb,
2793   rtl_create_basic_block,
2794   rtl_redirect_edge_and_branch,
2795   rtl_redirect_edge_and_branch_force,
2796   rtl_delete_block,
2797   rtl_split_block,
2798   rtl_move_block_after,
2799   rtl_can_merge_blocks,  /* can_merge_blocks_p */
2800   rtl_merge_blocks,
2801   rtl_split_edge,
2802   rtl_make_forwarder_block,
2803   rtl_tidy_fallthru_edge
2804 };
2805
2806 /* Implementation of CFG manipulation for cfg layout RTL, where
2807    basic block connected via fallthru edges does not have to be adjacent.
2808    This representation will hopefully become the default one in future
2809    version of the compiler.  */
2810 struct cfg_hooks cfg_layout_rtl_cfg_hooks = {
2811   "cfglayout mode",
2812   rtl_verify_flow_info_1,
2813   rtl_dump_bb,
2814   cfg_layout_create_basic_block,
2815   cfg_layout_redirect_edge_and_branch,
2816   cfg_layout_redirect_edge_and_branch_force,
2817   cfg_layout_delete_block,
2818   cfg_layout_split_block,
2819   rtl_move_block_after,
2820   cfg_layout_can_merge_blocks_p,
2821   cfg_layout_merge_blocks,
2822   cfg_layout_split_edge,
2823   rtl_make_forwarder_block,
2824   NULL
2825 };