OSDN Git Service

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