OSDN Git Service

* builtins.c (expand_builtin_synchronize): Use gimple_build_asm_vec.
[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, 2005, 2006, 2007, 2008, 2009
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 /* This file contains 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-attr.h"
57 #include "insn-config.h"
58 #include "cfglayout.h"
59 #include "expr.h"
60 #include "target.h"
61 #include "cfgloop.h"
62 #include "ggc.h"
63 #include "tree-pass.h"
64 #include "df.h"
65
66 static int can_delete_note_p (const_rtx);
67 static int can_delete_label_p (const_rtx);
68 static basic_block rtl_split_edge (edge);
69 static bool rtl_move_block_after (basic_block, basic_block);
70 static int rtl_verify_flow_info (void);
71 static basic_block cfg_layout_split_block (basic_block, void *);
72 static edge cfg_layout_redirect_edge_and_branch (edge, basic_block);
73 static basic_block cfg_layout_redirect_edge_and_branch_force (edge, basic_block);
74 static void cfg_layout_delete_block (basic_block);
75 static void rtl_delete_block (basic_block);
76 static basic_block rtl_redirect_edge_and_branch_force (edge, basic_block);
77 static edge rtl_redirect_edge_and_branch (edge, basic_block);
78 static basic_block rtl_split_block (basic_block, void *);
79 static void rtl_dump_bb (basic_block, FILE *, int, int);
80 static int rtl_verify_flow_info_1 (void);
81 static void rtl_make_forwarder_block (edge);
82 \f
83 /* Return true if NOTE is not one of the ones that must be kept paired,
84    so that we may simply delete it.  */
85
86 static int
87 can_delete_note_p (const_rtx note)
88 {
89   switch (NOTE_KIND (note))
90     {
91     case NOTE_INSN_DELETED:
92     case NOTE_INSN_BASIC_BLOCK:
93     case NOTE_INSN_EPILOGUE_BEG:
94       return true;
95
96     default:
97       return false;
98     }
99 }
100
101 /* True if a given label can be deleted.  */
102
103 static int
104 can_delete_label_p (const_rtx label)
105 {
106   return (!LABEL_PRESERVE_P (label)
107           /* User declared labels must be preserved.  */
108           && LABEL_NAME (label) == 0
109           && !in_expr_list_p (forced_labels, label));
110 }
111
112 /* Delete INSN by patching it out.  Return the next insn.  */
113
114 rtx
115 delete_insn (rtx insn)
116 {
117   rtx next = NEXT_INSN (insn);
118   rtx note;
119   bool really_delete = true;
120
121   if (LABEL_P (insn))
122     {
123       /* Some labels can't be directly removed from the INSN chain, as they
124          might be references via variables, constant pool etc.
125          Convert them to the special NOTE_INSN_DELETED_LABEL note.  */
126       if (! can_delete_label_p (insn))
127         {
128           const char *name = LABEL_NAME (insn);
129
130           really_delete = false;
131           PUT_CODE (insn, NOTE);
132           NOTE_KIND (insn) = NOTE_INSN_DELETED_LABEL;
133           NOTE_DELETED_LABEL_NAME (insn) = name;
134         }
135
136       remove_node_from_expr_list (insn, &nonlocal_goto_handler_labels);
137     }
138
139   if (really_delete)
140     {
141       /* If this insn has already been deleted, something is very wrong.  */
142       gcc_assert (!INSN_DELETED_P (insn));
143       remove_insn (insn);
144       INSN_DELETED_P (insn) = 1;
145     }
146
147   /* If deleting a jump, decrement the use count of the label.  Deleting
148      the label itself should happen in the normal course of block merging.  */
149   if (JUMP_P (insn))
150     {
151       if (JUMP_LABEL (insn)
152           && LABEL_P (JUMP_LABEL (insn)))
153         LABEL_NUSES (JUMP_LABEL (insn))--;
154
155       /* If there are more targets, remove them too.  */
156       while ((note
157               = find_reg_note (insn, REG_LABEL_TARGET, NULL_RTX)) != NULL_RTX
158              && LABEL_P (XEXP (note, 0)))
159         {
160           LABEL_NUSES (XEXP (note, 0))--;
161           remove_note (insn, note);
162         }
163     }
164
165   /* Also if deleting any insn that references a label as an operand.  */
166   while ((note = find_reg_note (insn, REG_LABEL_OPERAND, NULL_RTX)) != NULL_RTX
167          && LABEL_P (XEXP (note, 0)))
168     {
169       LABEL_NUSES (XEXP (note, 0))--;
170       remove_note (insn, note);
171     }
172
173   if (JUMP_TABLE_DATA_P (insn))
174     {
175       rtx pat = PATTERN (insn);
176       int diff_vec_p = GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC;
177       int len = XVECLEN (pat, diff_vec_p);
178       int i;
179
180       for (i = 0; i < len; i++)
181         {
182           rtx label = XEXP (XVECEXP (pat, diff_vec_p, i), 0);
183
184           /* When deleting code in bulk (e.g. removing many unreachable
185              blocks) we can delete a label that's a target of the vector
186              before deleting the vector itself.  */
187           if (!NOTE_P (label))
188             LABEL_NUSES (label)--;
189         }
190     }
191
192   return next;
193 }
194
195 /* Like delete_insn but also purge dead edges from BB.  */
196
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.  If CLEAR_BB is true, we set bb field for
215    insns that cannot be removed to NULL.  */
216
217 void
218 delete_insn_chain (rtx start, rtx finish, bool clear_bb)
219 {
220   rtx next;
221
222   /* Unchain the insns one by one.  It would be quicker to delete all of these
223      with a single unchaining, rather than one at a time, but we need to keep
224      the NOTE's.  */
225   while (1)
226     {
227       next = NEXT_INSN (start);
228       if (NOTE_P (start) && !can_delete_note_p (start))
229         ;
230       else
231         next = delete_insn (start);
232
233       if (clear_bb && !INSN_DELETED_P (start))
234         set_block_for_insn (start, NULL);
235
236       if (start == finish)
237         break;
238       start = next;
239     }
240 }
241 \f
242 /* Create a new basic block consisting of the instructions between HEAD and END
243    inclusive.  This function is designed to allow fast BB construction - reuses
244    the note and basic block struct in BB_NOTE, if any and do not grow
245    BASIC_BLOCK chain and should be used directly only by CFG construction code.
246    END can be NULL in to create new empty basic block before HEAD.  Both END
247    and HEAD can be NULL to create basic block at the end of INSN chain.
248    AFTER is the basic block we should be put after.  */
249
250 basic_block
251 create_basic_block_structure (rtx head, rtx end, rtx bb_note, basic_block after)
252 {
253   basic_block bb;
254
255   if (bb_note
256       && (bb = NOTE_BASIC_BLOCK (bb_note)) != NULL
257       && bb->aux == NULL)
258     {
259       /* If we found an existing note, thread it back onto the chain.  */
260
261       rtx after;
262
263       if (LABEL_P (head))
264         after = head;
265       else
266         {
267           after = PREV_INSN (head);
268           head = bb_note;
269         }
270
271       if (after != bb_note && NEXT_INSN (after) != bb_note)
272         reorder_insns_nobb (bb_note, bb_note, after);
273     }
274   else
275     {
276       /* Otherwise we must create a note and a basic block structure.  */
277
278       bb = alloc_block ();
279
280       init_rtl_bb_info (bb);
281       if (!head && !end)
282         head = end = bb_note
283           = emit_note_after (NOTE_INSN_BASIC_BLOCK, get_last_insn ());
284       else if (LABEL_P (head) && end)
285         {
286           bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, head);
287           if (head == end)
288             end = bb_note;
289         }
290       else
291         {
292           bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, head);
293           head = bb_note;
294           if (!end)
295             end = head;
296         }
297
298       NOTE_BASIC_BLOCK (bb_note) = bb;
299     }
300
301   /* Always include the bb note in the block.  */
302   if (NEXT_INSN (end) == bb_note)
303     end = bb_note;
304
305   BB_HEAD (bb) = head;
306   BB_END (bb) = end;
307   bb->index = last_basic_block++;
308   bb->flags = BB_NEW | BB_RTL;
309   link_block (bb, after);
310   SET_BASIC_BLOCK (bb->index, bb);
311   df_bb_refs_record (bb->index, false);
312   update_bb_for_insn (bb);
313   BB_SET_PARTITION (bb, BB_UNPARTITIONED);
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 static basic_block
328 rtl_create_basic_block (void *headp, void *endp, basic_block after)
329 {
330   rtx head = (rtx) headp, end = (rtx) endp;
331   basic_block bb;
332
333   /* Grow the basic block array if needed.  */
334   if ((size_t) last_basic_block >= VEC_length (basic_block, basic_block_info))
335     {
336       size_t new_size = last_basic_block + (last_basic_block + 3) / 4;
337       VEC_safe_grow_cleared (basic_block, gc, basic_block_info, new_size);
338     }
339
340   n_basic_blocks++;
341
342   bb = create_basic_block_structure (head, end, NULL, after);
343   bb->aux = NULL;
344   return bb;
345 }
346
347 static basic_block
348 cfg_layout_create_basic_block (void *head, void *end, basic_block after)
349 {
350   basic_block newbb = rtl_create_basic_block (head, end, after);
351
352   return newbb;
353 }
354 \f
355 /* Delete the insns in a (non-live) block.  We physically delete every
356    non-deleted-note insn, and update the flow graph appropriately.
357
358    Return nonzero if we deleted an exception handler.  */
359
360 /* ??? Preserving all such notes strikes me as wrong.  It would be nice
361    to post-process the stream to remove empty blocks, loops, ranges, etc.  */
362
363 static void
364 rtl_delete_block (basic_block b)
365 {
366   rtx insn, end;
367
368   /* If the head of this block is a CODE_LABEL, then it might be the
369      label for an exception handler which can't be reached.  We need
370      to remove the label from the exception_handler_label list.  */
371   insn = BB_HEAD (b);
372
373   end = get_last_bb_insn (b);
374
375   /* Selectively delete the entire chain.  */
376   BB_HEAD (b) = NULL;
377   delete_insn_chain (insn, end, true);
378
379
380   if (dump_file)
381     fprintf (dump_file, "deleting block %d\n", b->index);
382   df_bb_delete (b->index);
383 }
384 \f
385 /* Records the basic block struct in BLOCK_FOR_INSN for every insn.  */
386
387 void
388 compute_bb_for_insn (void)
389 {
390   basic_block bb;
391
392   FOR_EACH_BB (bb)
393     {
394       rtx end = BB_END (bb);
395       rtx insn;
396
397       for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
398         {
399           BLOCK_FOR_INSN (insn) = bb;
400           if (insn == end)
401             break;
402         }
403     }
404 }
405
406 /* Release the basic_block_for_insn array.  */
407
408 unsigned int
409 free_bb_for_insn (void)
410 {
411   rtx insn;
412   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
413     if (!BARRIER_P (insn))
414       BLOCK_FOR_INSN (insn) = NULL;
415   return 0;
416 }
417
418 static unsigned int
419 rest_of_pass_free_cfg (void)
420 {
421 #ifdef DELAY_SLOTS
422   /* The resource.c machinery uses DF but the CFG isn't guaranteed to be
423      valid at that point so it would be too late to call df_analyze.  */
424   if (optimize > 0 && flag_delayed_branch)
425     df_analyze ();
426 #endif
427
428   free_bb_for_insn ();
429   return 0;
430 }
431
432 struct rtl_opt_pass pass_free_cfg =
433 {
434  {
435   RTL_PASS,
436   NULL,                                 /* name */
437   NULL,                                 /* gate */
438   rest_of_pass_free_cfg,                /* execute */
439   NULL,                                 /* sub */
440   NULL,                                 /* next */
441   0,                                    /* static_pass_number */
442   TV_NONE,                              /* tv_id */
443   0,                                    /* properties_required */
444   0,                                    /* properties_provided */
445   PROP_cfg,                             /* properties_destroyed */
446   0,                                    /* todo_flags_start */
447   0,                                    /* todo_flags_finish */
448  }
449 };
450
451 /* Return RTX to emit after when we want to emit code on the entry of function.  */
452 rtx
453 entry_of_function (void)
454 {
455   return (n_basic_blocks > NUM_FIXED_BLOCKS ?
456           BB_HEAD (ENTRY_BLOCK_PTR->next_bb) : get_insns ());
457 }
458
459 /* Emit INSN at the entry point of the function, ensuring that it is only
460    executed once per function.  */
461 void
462 emit_insn_at_entry (rtx insn)
463 {
464   edge_iterator ei = ei_start (ENTRY_BLOCK_PTR->succs);
465   edge e = ei_safe_edge (ei);
466   gcc_assert (e->flags & EDGE_FALLTHRU);
467
468   insert_insn_on_edge (insn, e);
469   commit_edge_insertions ();
470 }
471
472 /* Update BLOCK_FOR_INSN of insns between BEGIN and END
473    (or BARRIER if found) and notify df of the bb change. 
474    The insn chain range is inclusive
475    (i.e. both BEGIN and END will be updated. */
476
477 static void
478 update_bb_for_insn_chain (rtx begin, rtx end, basic_block bb)
479 {
480   rtx insn;
481
482   end = NEXT_INSN (end);
483   for (insn = begin; insn != end; insn = NEXT_INSN (insn))
484     if (!BARRIER_P (insn))
485       df_insn_change_bb (insn, bb);
486 }
487
488 /* Update BLOCK_FOR_INSN of insns in BB to BB,
489    and notify df of the change.  */
490
491 void
492 update_bb_for_insn (basic_block bb)
493 {
494   update_bb_for_insn_chain (BB_HEAD (bb), BB_END (bb), bb);
495 }
496
497 \f
498 /* Return the INSN immediately following the NOTE_INSN_BASIC_BLOCK
499    note associated with the BLOCK.  */
500
501 static rtx
502 first_insn_after_basic_block_note (basic_block block)
503 {
504   rtx insn;
505
506   /* Get the first instruction in the block.  */
507   insn = BB_HEAD (block);
508
509   if (insn == NULL_RTX)
510     return NULL_RTX;
511   if (LABEL_P (insn))
512     insn = NEXT_INSN (insn);
513   gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
514
515   return NEXT_INSN (insn);
516 }
517
518 /* Creates a new basic block just after basic block B by splitting
519    everything after specified instruction I.  */
520
521 static basic_block
522 rtl_split_block (basic_block bb, void *insnp)
523 {
524   basic_block new_bb;
525   rtx insn = (rtx) insnp;
526   edge e;
527   edge_iterator ei;
528
529   if (!insn)
530     {
531       insn = first_insn_after_basic_block_note (bb);
532
533       if (insn)
534         {
535           rtx next = insn;
536
537           insn = PREV_INSN (insn);
538
539           /* If the block contains only debug insns, insn would have
540              been NULL in a non-debug compilation, and then we'd end
541              up emitting a DELETED note.  For -fcompare-debug
542              stability, emit the note too.  */
543           if (insn != BB_END (bb)
544               && DEBUG_INSN_P (next)
545               && DEBUG_INSN_P (BB_END (bb)))
546             {
547               while (next != BB_END (bb) && DEBUG_INSN_P (next))
548                 next = NEXT_INSN (next);
549
550               if (next == BB_END (bb))
551                 emit_note_after (NOTE_INSN_DELETED, next);
552             }
553         }
554       else
555         insn = get_last_insn ();
556     }
557
558   /* We probably should check type of the insn so that we do not create
559      inconsistent cfg.  It is checked in verify_flow_info anyway, so do not
560      bother.  */
561   if (insn == BB_END (bb))
562     emit_note_after (NOTE_INSN_DELETED, insn);
563
564   /* Create the new basic block.  */
565   new_bb = create_basic_block (NEXT_INSN (insn), BB_END (bb), bb);
566   BB_COPY_PARTITION (new_bb, bb);
567   BB_END (bb) = insn;
568
569   /* Redirect the outgoing edges.  */
570   new_bb->succs = bb->succs;
571   bb->succs = NULL;
572   FOR_EACH_EDGE (e, ei, new_bb->succs)
573     e->src = new_bb;
574
575   /* The new block starts off being dirty.  */
576   df_set_bb_dirty (bb);
577   return new_bb;
578 }
579
580 /* Blocks A and B are to be merged into a single block A.  The insns
581    are already contiguous.  */
582
583 static void
584 rtl_merge_blocks (basic_block a, basic_block b)
585 {
586   rtx b_head = BB_HEAD (b), b_end = BB_END (b), a_end = BB_END (a);
587   rtx del_first = NULL_RTX, del_last = NULL_RTX;
588   rtx b_debug_start = b_end, b_debug_end = b_end;
589   int b_empty = 0;
590
591   if (dump_file)
592     fprintf (dump_file, "merging block %d into block %d\n", b->index, a->index);
593
594   while (DEBUG_INSN_P (b_end))
595     b_end = PREV_INSN (b_debug_start = b_end);
596
597   /* If there was a CODE_LABEL beginning B, delete it.  */
598   if (LABEL_P (b_head))
599     {
600       /* Detect basic blocks with nothing but a label.  This can happen
601          in particular at the end of a function.  */
602       if (b_head == b_end)
603         b_empty = 1;
604
605       del_first = del_last = b_head;
606       b_head = NEXT_INSN (b_head);
607     }
608
609   /* Delete the basic block note and handle blocks containing just that
610      note.  */
611   if (NOTE_INSN_BASIC_BLOCK_P (b_head))
612     {
613       if (b_head == b_end)
614         b_empty = 1;
615       if (! del_last)
616         del_first = b_head;
617
618       del_last = b_head;
619       b_head = NEXT_INSN (b_head);
620     }
621
622   /* If there was a jump out of A, delete it.  */
623   if (JUMP_P (a_end))
624     {
625       rtx prev;
626
627       for (prev = PREV_INSN (a_end); ; prev = PREV_INSN (prev))
628         if (!NOTE_P (prev)
629             || NOTE_INSN_BASIC_BLOCK_P (prev)
630             || prev == BB_HEAD (a))
631           break;
632
633       del_first = a_end;
634
635 #ifdef HAVE_cc0
636       /* If this was a conditional jump, we need to also delete
637          the insn that set cc0.  */
638       if (only_sets_cc0_p (prev))
639         {
640           rtx tmp = prev;
641
642           prev = prev_nonnote_insn (prev);
643           if (!prev)
644             prev = BB_HEAD (a);
645           del_first = tmp;
646         }
647 #endif
648
649       a_end = PREV_INSN (del_first);
650     }
651   else if (BARRIER_P (NEXT_INSN (a_end)))
652     del_first = NEXT_INSN (a_end);
653
654   /* Delete everything marked above as well as crap that might be
655      hanging out between the two blocks.  */
656   BB_HEAD (b) = NULL;
657   delete_insn_chain (del_first, del_last, true);
658
659   /* Reassociate the insns of B with A.  */
660   if (!b_empty)
661     {
662       update_bb_for_insn_chain (a_end, b_debug_end, a);
663
664       a_end = b_debug_end;
665     }
666   else if (b_end != b_debug_end)
667     {
668       /* Move any deleted labels and other notes between the end of A
669          and the debug insns that make up B after the debug insns,
670          bringing the debug insns into A while keeping the notes after
671          the end of A.  */
672       if (NEXT_INSN (a_end) != b_debug_start)
673         reorder_insns_nobb (NEXT_INSN (a_end), PREV_INSN (b_debug_start),
674                             b_debug_end);
675       update_bb_for_insn_chain (b_debug_start, b_debug_end, a);
676       a_end = b_debug_end;
677     }
678
679   df_bb_delete (b->index);
680   BB_END (a) = a_end;
681 }
682
683
684 /* Return true when block A and B can be merged.  */
685
686 static bool
687 rtl_can_merge_blocks (basic_block a, basic_block b)
688 {
689   /* If we are partitioning hot/cold basic blocks, we don't want to
690      mess up unconditional or indirect jumps that cross between hot
691      and cold sections.
692
693      Basic block partitioning may result in some jumps that appear to
694      be optimizable (or blocks that appear to be mergeable), but which really
695      must be left untouched (they are required to make it safely across
696      partition boundaries).  See  the comments at the top of
697      bb-reorder.c:partition_hot_cold_basic_blocks for complete details.  */
698
699   if (BB_PARTITION (a) != BB_PARTITION (b))
700     return false;
701
702   /* There must be exactly one edge in between the blocks.  */
703   return (single_succ_p (a)
704           && single_succ (a) == b
705           && single_pred_p (b)
706           && a != b
707           /* Must be simple edge.  */
708           && !(single_succ_edge (a)->flags & EDGE_COMPLEX)
709           && a->next_bb == b
710           && a != ENTRY_BLOCK_PTR && b != EXIT_BLOCK_PTR
711           /* If the jump insn has side effects,
712              we can't kill the edge.  */
713           && (!JUMP_P (BB_END (a))
714               || (reload_completed
715                   ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
716 }
717 \f
718 /* Return the label in the head of basic block BLOCK.  Create one if it doesn't
719    exist.  */
720
721 rtx
722 block_label (basic_block block)
723 {
724   if (block == EXIT_BLOCK_PTR)
725     return NULL_RTX;
726
727   if (!LABEL_P (BB_HEAD (block)))
728     {
729       BB_HEAD (block) = emit_label_before (gen_label_rtx (), BB_HEAD (block));
730     }
731
732   return BB_HEAD (block);
733 }
734
735 /* Attempt to perform edge redirection by replacing possibly complex jump
736    instruction by unconditional jump or removing jump completely.  This can
737    apply only if all edges now point to the same block.  The parameters and
738    return values are equivalent to redirect_edge_and_branch.  */
739
740 edge
741 try_redirect_by_replacing_jump (edge e, basic_block target, bool in_cfglayout)
742 {
743   basic_block src = e->src;
744   rtx insn = BB_END (src), kill_from;
745   rtx set;
746   int fallthru = 0;
747
748   /* If we are partitioning hot/cold basic blocks, we don't want to
749      mess up unconditional or indirect jumps that cross between hot
750      and cold sections.
751
752      Basic block partitioning may result in some jumps that appear to
753      be optimizable (or blocks that appear to be mergeable), but which really
754      must be left untouched (they are required to make it safely across
755      partition boundaries).  See  the comments at the top of
756      bb-reorder.c:partition_hot_cold_basic_blocks for complete details.  */
757
758   if (find_reg_note (insn, REG_CROSSING_JUMP, NULL_RTX)
759       || BB_PARTITION (src) != BB_PARTITION (target))
760     return NULL;
761
762   /* We can replace or remove a complex jump only when we have exactly
763      two edges.  Also, if we have exactly one outgoing edge, we can
764      redirect that.  */
765   if (EDGE_COUNT (src->succs) >= 3
766       /* Verify that all targets will be TARGET.  Specifically, the
767          edge that is not E must also go to TARGET.  */
768       || (EDGE_COUNT (src->succs) == 2
769           && EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target))
770     return NULL;
771
772   if (!onlyjump_p (insn))
773     return NULL;
774   if ((!optimize || reload_completed) && tablejump_p (insn, NULL, NULL))
775     return NULL;
776
777   /* Avoid removing branch with side effects.  */
778   set = single_set (insn);
779   if (!set || side_effects_p (set))
780     return NULL;
781
782   /* In case we zap a conditional jump, we'll need to kill
783      the cc0 setter too.  */
784   kill_from = insn;
785 #ifdef HAVE_cc0
786   if (reg_mentioned_p (cc0_rtx, PATTERN (insn))
787       && only_sets_cc0_p (PREV_INSN (insn)))
788     kill_from = PREV_INSN (insn);
789 #endif
790
791   /* See if we can create the fallthru edge.  */
792   if (in_cfglayout || can_fallthru (src, target))
793     {
794       if (dump_file)
795         fprintf (dump_file, "Removing jump %i.\n", INSN_UID (insn));
796       fallthru = 1;
797
798       /* Selectively unlink whole insn chain.  */
799       if (in_cfglayout)
800         {
801           rtx insn = src->il.rtl->footer;
802
803           delete_insn_chain (kill_from, BB_END (src), false);
804
805           /* Remove barriers but keep jumptables.  */
806           while (insn)
807             {
808               if (BARRIER_P (insn))
809                 {
810                   if (PREV_INSN (insn))
811                     NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
812                   else
813                     src->il.rtl->footer = NEXT_INSN (insn);
814                   if (NEXT_INSN (insn))
815                     PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
816                 }
817               if (LABEL_P (insn))
818                 break;
819               insn = NEXT_INSN (insn);
820             }
821         }
822       else
823         delete_insn_chain (kill_from, PREV_INSN (BB_HEAD (target)),
824                            false);
825     }
826
827   /* If this already is simplejump, redirect it.  */
828   else if (simplejump_p (insn))
829     {
830       if (e->dest == target)
831         return NULL;
832       if (dump_file)
833         fprintf (dump_file, "Redirecting jump %i from %i to %i.\n",
834                  INSN_UID (insn), e->dest->index, target->index);
835       if (!redirect_jump (insn, block_label (target), 0))
836         {
837           gcc_assert (target == EXIT_BLOCK_PTR);
838           return NULL;
839         }
840     }
841
842   /* Cannot do anything for target exit block.  */
843   else if (target == EXIT_BLOCK_PTR)
844     return NULL;
845
846   /* Or replace possibly complicated jump insn by simple jump insn.  */
847   else
848     {
849       rtx target_label = block_label (target);
850       rtx barrier, label, table;
851
852       emit_jump_insn_after_noloc (gen_jump (target_label), insn);
853       JUMP_LABEL (BB_END (src)) = target_label;
854       LABEL_NUSES (target_label)++;
855       if (dump_file)
856         fprintf (dump_file, "Replacing insn %i by jump %i\n",
857                  INSN_UID (insn), INSN_UID (BB_END (src)));
858
859
860       delete_insn_chain (kill_from, insn, false);
861
862       /* Recognize a tablejump that we are converting to a
863          simple jump and remove its associated CODE_LABEL
864          and ADDR_VEC or ADDR_DIFF_VEC.  */
865       if (tablejump_p (insn, &label, &table))
866         delete_insn_chain (label, table, false);
867
868       barrier = next_nonnote_insn (BB_END (src));
869       if (!barrier || !BARRIER_P (barrier))
870         emit_barrier_after (BB_END (src));
871       else
872         {
873           if (barrier != NEXT_INSN (BB_END (src)))
874             {
875               /* Move the jump before barrier so that the notes
876                  which originally were or were created before jump table are
877                  inside the basic block.  */
878               rtx new_insn = BB_END (src);
879
880               update_bb_for_insn_chain (NEXT_INSN (BB_END (src)),
881                                         PREV_INSN (barrier), src);
882
883               NEXT_INSN (PREV_INSN (new_insn)) = NEXT_INSN (new_insn);
884               PREV_INSN (NEXT_INSN (new_insn)) = PREV_INSN (new_insn);
885
886               NEXT_INSN (new_insn) = barrier;
887               NEXT_INSN (PREV_INSN (barrier)) = new_insn;
888
889               PREV_INSN (new_insn) = PREV_INSN (barrier);
890               PREV_INSN (barrier) = new_insn;
891             }
892         }
893     }
894
895   /* Keep only one edge out and set proper flags.  */
896   if (!single_succ_p (src))
897     remove_edge (e);
898   gcc_assert (single_succ_p (src));
899
900   e = single_succ_edge (src);
901   if (fallthru)
902     e->flags = EDGE_FALLTHRU;
903   else
904     e->flags = 0;
905
906   e->probability = REG_BR_PROB_BASE;
907   e->count = src->count;
908
909   if (e->dest != target)
910     redirect_edge_succ (e, target);
911   return e;
912 }
913
914 /* Subroutine of redirect_branch_edge that tries to patch the jump
915    instruction INSN so that it reaches block NEW.  Do this
916    only when it originally reached block OLD.  Return true if this
917    worked or the original target wasn't OLD, return false if redirection
918    doesn't work.  */
919
920 static bool
921 patch_jump_insn (rtx insn, rtx old_label, basic_block new_bb)
922 {
923   rtx tmp;
924   /* Recognize a tablejump and adjust all matching cases.  */
925   if (tablejump_p (insn, NULL, &tmp))
926     {
927       rtvec vec;
928       int j;
929       rtx new_label = block_label (new_bb);
930
931       if (new_bb == EXIT_BLOCK_PTR)
932         return false;
933       if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
934         vec = XVEC (PATTERN (tmp), 0);
935       else
936         vec = XVEC (PATTERN (tmp), 1);
937
938       for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
939         if (XEXP (RTVEC_ELT (vec, j), 0) == old_label)
940           {
941             RTVEC_ELT (vec, j) = gen_rtx_LABEL_REF (Pmode, new_label);
942             --LABEL_NUSES (old_label);
943             ++LABEL_NUSES (new_label);
944           }
945
946       /* Handle casesi dispatch insns.  */
947       if ((tmp = single_set (insn)) != NULL
948           && SET_DEST (tmp) == pc_rtx
949           && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
950           && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF
951           && XEXP (XEXP (SET_SRC (tmp), 2), 0) == old_label)
952         {
953           XEXP (SET_SRC (tmp), 2) = gen_rtx_LABEL_REF (Pmode,
954                                                        new_label);
955           --LABEL_NUSES (old_label);
956           ++LABEL_NUSES (new_label);
957         }
958     }
959   else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL)
960     {
961       int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp);
962       rtx new_label, note;
963
964       if (new_bb == EXIT_BLOCK_PTR)
965         return false;
966       new_label = block_label (new_bb);
967
968       for (i = 0; i < n; ++i)
969         {
970           rtx old_ref = ASM_OPERANDS_LABEL (tmp, i);
971           gcc_assert (GET_CODE (old_ref) == LABEL_REF);
972           if (XEXP (old_ref, 0) == old_label)
973             {
974               ASM_OPERANDS_LABEL (tmp, i)
975                 = gen_rtx_LABEL_REF (Pmode, new_label);
976               --LABEL_NUSES (old_label);
977               ++LABEL_NUSES (new_label);
978             }
979         }
980
981       if (JUMP_LABEL (insn) == old_label)
982         {
983           JUMP_LABEL (insn) = new_label;
984           note = find_reg_note (insn, REG_LABEL_TARGET, new_label);
985           if (note)
986             remove_note (insn, note);
987         }
988       else
989         {
990           note = find_reg_note (insn, REG_LABEL_TARGET, old_label);
991           if (note)
992             remove_note (insn, note);
993           if (JUMP_LABEL (insn) != new_label
994               && !find_reg_note (insn, REG_LABEL_TARGET, new_label))
995             add_reg_note (insn, REG_LABEL_TARGET, new_label);
996         }
997     }
998   else
999     {
1000       /* ?? We may play the games with moving the named labels from
1001          one basic block to the other in case only one computed_jump is
1002          available.  */
1003       if (computed_jump_p (insn)
1004           /* A return instruction can't be redirected.  */
1005           || returnjump_p (insn))
1006         return false;
1007
1008       if (!currently_expanding_to_rtl || JUMP_LABEL (insn) == old_label)
1009         {
1010           /* If the insn doesn't go where we think, we're confused.  */
1011           gcc_assert (JUMP_LABEL (insn) == old_label);
1012
1013           /* If the substitution doesn't succeed, die.  This can happen
1014              if the back end emitted unrecognizable instructions or if
1015              target is exit block on some arches.  */
1016           if (!redirect_jump (insn, block_label (new_bb), 0))
1017             {
1018               gcc_assert (new_bb == EXIT_BLOCK_PTR);
1019               return false;
1020             }
1021         }
1022     }
1023   return true;
1024 }
1025
1026
1027 /* Redirect edge representing branch of (un)conditional jump or tablejump,
1028    NULL on failure  */
1029 static edge
1030 redirect_branch_edge (edge e, basic_block target)
1031 {
1032   rtx old_label = BB_HEAD (e->dest);
1033   basic_block src = e->src;
1034   rtx insn = BB_END (src);
1035
1036   /* We can only redirect non-fallthru edges of jump insn.  */
1037   if (e->flags & EDGE_FALLTHRU)
1038     return NULL;
1039   else if (!JUMP_P (insn) && !currently_expanding_to_rtl)
1040     return NULL;
1041
1042   if (!currently_expanding_to_rtl)
1043     {
1044       if (!patch_jump_insn (insn, old_label, target))
1045         return NULL;
1046     }
1047   else
1048     /* When expanding this BB might actually contain multiple
1049        jumps (i.e. not yet split by find_many_sub_basic_blocks).
1050        Redirect all of those that match our label.  */
1051     for (insn = BB_HEAD (src); insn != NEXT_INSN (BB_END (src));
1052          insn = NEXT_INSN (insn))
1053       if (JUMP_P (insn) && !patch_jump_insn (insn, old_label, target))
1054         return NULL;
1055
1056   if (dump_file)
1057     fprintf (dump_file, "Edge %i->%i redirected to %i\n",
1058              e->src->index, e->dest->index, target->index);
1059
1060   if (e->dest != target)
1061     e = redirect_edge_succ_nodup (e, target);
1062
1063   return e;
1064 }
1065
1066 /* Attempt to change code to redirect edge E to TARGET.  Don't do that on
1067    expense of adding new instructions or reordering basic blocks.
1068
1069    Function can be also called with edge destination equivalent to the TARGET.
1070    Then it should try the simplifications and do nothing if none is possible.
1071
1072    Return edge representing the branch if transformation succeeded.  Return NULL
1073    on failure.
1074    We still return NULL in case E already destinated TARGET and we didn't
1075    managed to simplify instruction stream.  */
1076
1077 static edge
1078 rtl_redirect_edge_and_branch (edge e, basic_block target)
1079 {
1080   edge ret;
1081   basic_block src = e->src;
1082
1083   if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
1084     return NULL;
1085
1086   if (e->dest == target)
1087     return e;
1088
1089   if ((ret = try_redirect_by_replacing_jump (e, target, false)) != NULL)
1090     {
1091       df_set_bb_dirty (src);
1092       return ret;
1093     }
1094
1095   ret = redirect_branch_edge (e, target);
1096   if (!ret)
1097     return NULL;
1098
1099   df_set_bb_dirty (src);
1100   return ret;
1101 }
1102
1103 /* Like force_nonfallthru below, but additionally performs redirection
1104    Used by redirect_edge_and_branch_force.  */
1105
1106 static basic_block
1107 force_nonfallthru_and_redirect (edge e, basic_block target)
1108 {
1109   basic_block jump_block, new_bb = NULL, src = e->src;
1110   rtx note;
1111   edge new_edge;
1112   int abnormal_edge_flags = 0;
1113   int loc;
1114
1115   /* In the case the last instruction is conditional jump to the next
1116      instruction, first redirect the jump itself and then continue
1117      by creating a basic block afterwards to redirect fallthru edge.  */
1118   if (e->src != ENTRY_BLOCK_PTR && e->dest != EXIT_BLOCK_PTR
1119       && any_condjump_p (BB_END (e->src))
1120       && JUMP_LABEL (BB_END (e->src)) == BB_HEAD (e->dest))
1121     {
1122       rtx note;
1123       edge b = unchecked_make_edge (e->src, target, 0);
1124       bool redirected;
1125
1126       redirected = redirect_jump (BB_END (e->src), block_label (target), 0);
1127       gcc_assert (redirected);
1128
1129       note = find_reg_note (BB_END (e->src), REG_BR_PROB, NULL_RTX);
1130       if (note)
1131         {
1132           int prob = INTVAL (XEXP (note, 0));
1133
1134           b->probability = prob;
1135           b->count = e->count * prob / REG_BR_PROB_BASE;
1136           e->probability -= e->probability;
1137           e->count -= b->count;
1138           if (e->probability < 0)
1139             e->probability = 0;
1140           if (e->count < 0)
1141             e->count = 0;
1142         }
1143     }
1144
1145   if (e->flags & EDGE_ABNORMAL)
1146     {
1147       /* Irritating special case - fallthru edge to the same block as abnormal
1148          edge.
1149          We can't redirect abnormal edge, but we still can split the fallthru
1150          one and create separate abnormal edge to original destination.
1151          This allows bb-reorder to make such edge non-fallthru.  */
1152       gcc_assert (e->dest == target);
1153       abnormal_edge_flags = e->flags & ~(EDGE_FALLTHRU | EDGE_CAN_FALLTHRU);
1154       e->flags &= EDGE_FALLTHRU | EDGE_CAN_FALLTHRU;
1155     }
1156   else
1157     {
1158       gcc_assert (e->flags & EDGE_FALLTHRU);
1159       if (e->src == ENTRY_BLOCK_PTR)
1160         {
1161           /* We can't redirect the entry block.  Create an empty block
1162              at the start of the function which we use to add the new
1163              jump.  */
1164           edge tmp;
1165           edge_iterator ei;
1166           bool found = false;
1167
1168           basic_block bb = create_basic_block (BB_HEAD (e->dest), NULL, ENTRY_BLOCK_PTR);
1169
1170           /* Change the existing edge's source to be the new block, and add
1171              a new edge from the entry block to the new block.  */
1172           e->src = bb;
1173           for (ei = ei_start (ENTRY_BLOCK_PTR->succs); (tmp = ei_safe_edge (ei)); )
1174             {
1175               if (tmp == e)
1176                 {
1177                   VEC_unordered_remove (edge, ENTRY_BLOCK_PTR->succs, ei.index);
1178                   found = true;
1179                   break;
1180                 }
1181               else
1182                 ei_next (&ei);
1183             }
1184
1185           gcc_assert (found);
1186
1187           VEC_safe_push (edge, gc, bb->succs, e);
1188           make_single_succ_edge (ENTRY_BLOCK_PTR, bb, EDGE_FALLTHRU);
1189         }
1190     }
1191
1192   if (EDGE_COUNT (e->src->succs) >= 2 || abnormal_edge_flags)
1193     {
1194       /* Create the new structures.  */
1195
1196       /* If the old block ended with a tablejump, skip its table
1197          by searching forward from there.  Otherwise start searching
1198          forward from the last instruction of the old block.  */
1199       if (!tablejump_p (BB_END (e->src), NULL, &note))
1200         note = BB_END (e->src);
1201       note = NEXT_INSN (note);
1202
1203       jump_block = create_basic_block (note, NULL, e->src);
1204       jump_block->count = e->count;
1205       jump_block->frequency = EDGE_FREQUENCY (e);
1206       jump_block->loop_depth = target->loop_depth;
1207
1208       /* Make sure new block ends up in correct hot/cold section.  */
1209
1210       BB_COPY_PARTITION (jump_block, e->src);
1211       if (flag_reorder_blocks_and_partition
1212           && targetm.have_named_sections
1213           && JUMP_P (BB_END (jump_block))
1214           && !any_condjump_p (BB_END (jump_block))
1215           && (EDGE_SUCC (jump_block, 0)->flags & EDGE_CROSSING))
1216         add_reg_note (BB_END (jump_block), REG_CROSSING_JUMP, NULL_RTX);
1217
1218       /* Wire edge in.  */
1219       new_edge = make_edge (e->src, jump_block, EDGE_FALLTHRU);
1220       new_edge->probability = e->probability;
1221       new_edge->count = e->count;
1222
1223       /* Redirect old edge.  */
1224       redirect_edge_pred (e, jump_block);
1225       e->probability = REG_BR_PROB_BASE;
1226
1227       new_bb = jump_block;
1228     }
1229   else
1230     jump_block = e->src;
1231
1232   if (e->goto_locus && e->goto_block == NULL)
1233     loc = e->goto_locus;
1234   else
1235     loc = 0;
1236   e->flags &= ~EDGE_FALLTHRU;
1237   if (target == EXIT_BLOCK_PTR)
1238     {
1239 #ifdef HAVE_return
1240         emit_jump_insn_after_setloc (gen_return (), BB_END (jump_block), loc);
1241 #else
1242         gcc_unreachable ();
1243 #endif
1244     }
1245   else
1246     {
1247       rtx label = block_label (target);
1248       emit_jump_insn_after_setloc (gen_jump (label), BB_END (jump_block), loc);
1249       JUMP_LABEL (BB_END (jump_block)) = label;
1250       LABEL_NUSES (label)++;
1251     }
1252
1253   emit_barrier_after (BB_END (jump_block));
1254   redirect_edge_succ_nodup (e, target);
1255
1256   if (abnormal_edge_flags)
1257     make_edge (src, target, abnormal_edge_flags);
1258
1259   df_mark_solutions_dirty (); 
1260   return new_bb;
1261 }
1262
1263 /* Edge E is assumed to be fallthru edge.  Emit needed jump instruction
1264    (and possibly create new basic block) to make edge non-fallthru.
1265    Return newly created BB or NULL if none.  */
1266
1267 basic_block
1268 force_nonfallthru (edge e)
1269 {
1270   return force_nonfallthru_and_redirect (e, e->dest);
1271 }
1272
1273 /* Redirect edge even at the expense of creating new jump insn or
1274    basic block.  Return new basic block if created, NULL otherwise.
1275    Conversion must be possible.  */
1276
1277 static basic_block
1278 rtl_redirect_edge_and_branch_force (edge e, basic_block target)
1279 {
1280   if (redirect_edge_and_branch (e, target)
1281       || e->dest == target)
1282     return NULL;
1283
1284   /* In case the edge redirection failed, try to force it to be non-fallthru
1285      and redirect newly created simplejump.  */
1286   df_set_bb_dirty (e->src);
1287   return force_nonfallthru_and_redirect (e, target);
1288 }
1289
1290 /* The given edge should potentially be a fallthru edge.  If that is in
1291    fact true, delete the jump and barriers that are in the way.  */
1292
1293 static void
1294 rtl_tidy_fallthru_edge (edge e)
1295 {
1296   rtx q;
1297   basic_block b = e->src, c = b->next_bb;
1298
1299   /* ??? In a late-running flow pass, other folks may have deleted basic
1300      blocks by nopping out blocks, leaving multiple BARRIERs between here
1301      and the target label. They ought to be chastised and fixed.
1302
1303      We can also wind up with a sequence of undeletable labels between
1304      one block and the next.
1305
1306      So search through a sequence of barriers, labels, and notes for
1307      the head of block C and assert that we really do fall through.  */
1308
1309   for (q = NEXT_INSN (BB_END (b)); q != BB_HEAD (c); q = NEXT_INSN (q))
1310     if (INSN_P (q))
1311       return;
1312
1313   /* Remove what will soon cease being the jump insn from the source block.
1314      If block B consisted only of this single jump, turn it into a deleted
1315      note.  */
1316   q = BB_END (b);
1317   if (JUMP_P (q)
1318       && onlyjump_p (q)
1319       && (any_uncondjump_p (q)
1320           || single_succ_p (b)))
1321     {
1322 #ifdef HAVE_cc0
1323       /* If this was a conditional jump, we need to also delete
1324          the insn that set cc0.  */
1325       if (any_condjump_p (q) && only_sets_cc0_p (PREV_INSN (q)))
1326         q = PREV_INSN (q);
1327 #endif
1328
1329       q = PREV_INSN (q);
1330     }
1331
1332   /* Selectively unlink the sequence.  */
1333   if (q != PREV_INSN (BB_HEAD (c)))
1334     delete_insn_chain (NEXT_INSN (q), PREV_INSN (BB_HEAD (c)), false);
1335
1336   e->flags |= EDGE_FALLTHRU;
1337 }
1338 \f
1339 /* Should move basic block BB after basic block AFTER.  NIY.  */
1340
1341 static bool
1342 rtl_move_block_after (basic_block bb ATTRIBUTE_UNUSED,
1343                       basic_block after ATTRIBUTE_UNUSED)
1344 {
1345   return false;
1346 }
1347
1348 /* Split a (typically critical) edge.  Return the new block.
1349    The edge must not be abnormal.
1350
1351    ??? The code generally expects to be called on critical edges.
1352    The case of a block ending in an unconditional jump to a
1353    block with multiple predecessors is not handled optimally.  */
1354
1355 static basic_block
1356 rtl_split_edge (edge edge_in)
1357 {
1358   basic_block bb;
1359   rtx before;
1360
1361   /* Abnormal edges cannot be split.  */
1362   gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
1363
1364   /* We are going to place the new block in front of edge destination.
1365      Avoid existence of fallthru predecessors.  */
1366   if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1367     {
1368       edge e;
1369       edge_iterator ei;
1370
1371       FOR_EACH_EDGE (e, ei, edge_in->dest->preds)
1372         if (e->flags & EDGE_FALLTHRU)
1373           break;
1374
1375       if (e)
1376         force_nonfallthru (e);
1377     }
1378
1379   /* Create the basic block note.  */
1380   if (edge_in->dest != EXIT_BLOCK_PTR)
1381     before = BB_HEAD (edge_in->dest);
1382   else
1383     before = NULL_RTX;
1384
1385   /* If this is a fall through edge to the exit block, the blocks might be
1386      not adjacent, and the right place is the after the source.  */
1387   if (edge_in->flags & EDGE_FALLTHRU && edge_in->dest == EXIT_BLOCK_PTR)
1388     {
1389       before = NEXT_INSN (BB_END (edge_in->src));
1390       bb = create_basic_block (before, NULL, edge_in->src);
1391       BB_COPY_PARTITION (bb, edge_in->src);
1392     }
1393   else
1394     {
1395       bb = create_basic_block (before, NULL, edge_in->dest->prev_bb);
1396       /* ??? Why not edge_in->dest->prev_bb here?  */
1397       BB_COPY_PARTITION (bb, edge_in->dest);
1398     }
1399
1400   make_single_succ_edge (bb, edge_in->dest, EDGE_FALLTHRU);
1401
1402   /* For non-fallthru edges, we must adjust the predecessor's
1403      jump instruction to target our new block.  */
1404   if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1405     {
1406       edge redirected = redirect_edge_and_branch (edge_in, bb);
1407       gcc_assert (redirected);
1408     }
1409   else
1410     redirect_edge_succ (edge_in, bb);
1411
1412   return bb;
1413 }
1414
1415 /* Queue instructions for insertion on an edge between two basic blocks.
1416    The new instructions and basic blocks (if any) will not appear in the
1417    CFG until commit_edge_insertions is called.  */
1418
1419 void
1420 insert_insn_on_edge (rtx pattern, edge e)
1421 {
1422   /* We cannot insert instructions on an abnormal critical edge.
1423      It will be easier to find the culprit if we die now.  */
1424   gcc_assert (!((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e)));
1425
1426   if (e->insns.r == NULL_RTX)
1427     start_sequence ();
1428   else
1429     push_to_sequence (e->insns.r);
1430
1431   emit_insn (pattern);
1432
1433   e->insns.r = get_insns ();
1434   end_sequence ();
1435 }
1436
1437 /* Update the CFG for the instructions queued on edge E.  */
1438
1439 void
1440 commit_one_edge_insertion (edge e)
1441 {
1442   rtx before = NULL_RTX, after = NULL_RTX, insns, tmp, last;
1443   basic_block bb = NULL;
1444
1445   /* Pull the insns off the edge now since the edge might go away.  */
1446   insns = e->insns.r;
1447   e->insns.r = NULL_RTX;
1448
1449   if (!before && !after)
1450     {
1451       /* Figure out where to put these things.  If the destination has
1452          one predecessor, insert there.  Except for the exit block.  */
1453       if (single_pred_p (e->dest) && e->dest != EXIT_BLOCK_PTR)
1454         {
1455           bb = e->dest;
1456
1457           /* Get the location correct wrt a code label, and "nice" wrt
1458              a basic block note, and before everything else.  */
1459           tmp = BB_HEAD (bb);
1460           if (LABEL_P (tmp))
1461             tmp = NEXT_INSN (tmp);
1462           if (NOTE_INSN_BASIC_BLOCK_P (tmp))
1463             tmp = NEXT_INSN (tmp);
1464           if (tmp == BB_HEAD (bb))
1465             before = tmp;
1466           else if (tmp)
1467             after = PREV_INSN (tmp);
1468           else
1469             after = get_last_insn ();
1470         }
1471
1472       /* If the source has one successor and the edge is not abnormal,
1473          insert there.  Except for the entry block.  */
1474       else if ((e->flags & EDGE_ABNORMAL) == 0
1475                && single_succ_p (e->src)
1476                && e->src != ENTRY_BLOCK_PTR)
1477         {
1478           bb = e->src;
1479
1480           /* It is possible to have a non-simple jump here.  Consider a target
1481              where some forms of unconditional jumps clobber a register.  This
1482              happens on the fr30 for example.
1483
1484              We know this block has a single successor, so we can just emit
1485              the queued insns before the jump.  */
1486           if (JUMP_P (BB_END (bb)))
1487             before = BB_END (bb);
1488           else
1489             {
1490               /* We'd better be fallthru, or we've lost track of
1491                  what's what.  */
1492               gcc_assert (e->flags & EDGE_FALLTHRU);
1493
1494               after = BB_END (bb);
1495             }
1496         }
1497       /* Otherwise we must split the edge.  */
1498       else
1499         {
1500           bb = split_edge (e);
1501           after = BB_END (bb);
1502
1503           if (flag_reorder_blocks_and_partition
1504               && targetm.have_named_sections
1505               && e->src != ENTRY_BLOCK_PTR
1506               && BB_PARTITION (e->src) == BB_COLD_PARTITION
1507               && !(e->flags & EDGE_CROSSING))
1508             {
1509               rtx bb_note, cur_insn;
1510
1511               bb_note = NULL_RTX;
1512               for (cur_insn = BB_HEAD (bb); cur_insn != NEXT_INSN (BB_END (bb));
1513                    cur_insn = NEXT_INSN (cur_insn))
1514                 if (NOTE_INSN_BASIC_BLOCK_P (cur_insn))
1515                   {
1516                     bb_note = cur_insn;
1517                     break;
1518                   }
1519
1520               if (JUMP_P (BB_END (bb))
1521                   && !any_condjump_p (BB_END (bb))
1522                   && (single_succ_edge (bb)->flags & EDGE_CROSSING))
1523                 add_reg_note (BB_END (bb), REG_CROSSING_JUMP, NULL_RTX);
1524             }
1525         }
1526     }
1527
1528   /* Now that we've found the spot, do the insertion.  */
1529
1530   if (before)
1531     {
1532       emit_insn_before_noloc (insns, before, bb);
1533       last = prev_nonnote_insn (before);
1534     }
1535   else
1536     last = emit_insn_after_noloc (insns, after, bb);
1537
1538   if (returnjump_p (last))
1539     {
1540       /* ??? Remove all outgoing edges from BB and add one for EXIT.
1541          This is not currently a problem because this only happens
1542          for the (single) epilogue, which already has a fallthru edge
1543          to EXIT.  */
1544
1545       e = single_succ_edge (bb);
1546       gcc_assert (e->dest == EXIT_BLOCK_PTR
1547                   && single_succ_p (bb) && (e->flags & EDGE_FALLTHRU));
1548
1549       e->flags &= ~EDGE_FALLTHRU;
1550       emit_barrier_after (last);
1551
1552       if (before)
1553         delete_insn (before);
1554     }
1555   else
1556     gcc_assert (!JUMP_P (last));
1557
1558   /* Mark the basic block for find_many_sub_basic_blocks.  */
1559   if (current_ir_type () != IR_RTL_CFGLAYOUT)
1560     bb->aux = &bb->aux;
1561 }
1562
1563 /* Update the CFG for all queued instructions.  */
1564
1565 void
1566 commit_edge_insertions (void)
1567 {
1568   basic_block bb;
1569   sbitmap blocks;
1570   bool changed = false;
1571
1572 #ifdef ENABLE_CHECKING
1573   verify_flow_info ();
1574 #endif
1575
1576   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
1577     {
1578       edge e;
1579       edge_iterator ei;
1580
1581       FOR_EACH_EDGE (e, ei, bb->succs)
1582         if (e->insns.r)
1583           {
1584             changed = true;
1585             commit_one_edge_insertion (e);
1586           }
1587     }
1588
1589   if (!changed)
1590     return;
1591
1592   /* In the old rtl CFG API, it was OK to insert control flow on an
1593      edge, apparently?  In cfglayout mode, this will *not* work, and
1594      the caller is responsible for making sure that control flow is
1595      valid at all times.  */
1596   if (current_ir_type () == IR_RTL_CFGLAYOUT)
1597     return;
1598
1599   blocks = sbitmap_alloc (last_basic_block);
1600   sbitmap_zero (blocks);
1601   FOR_EACH_BB (bb)
1602     if (bb->aux)
1603       {
1604         SET_BIT (blocks, bb->index);
1605         /* Check for forgotten bb->aux values before commit_edge_insertions
1606            call.  */
1607         gcc_assert (bb->aux == &bb->aux);
1608         bb->aux = NULL;
1609       }
1610   find_many_sub_basic_blocks (blocks);
1611   sbitmap_free (blocks);
1612 }
1613 \f
1614
1615 /* Print out RTL-specific basic block information (live information
1616    at start and end).  */
1617
1618 static void
1619 rtl_dump_bb (basic_block bb, FILE *outf, int indent, int flags ATTRIBUTE_UNUSED)
1620 {
1621   rtx insn;
1622   rtx last;
1623   char *s_indent;
1624
1625   s_indent = (char *) alloca ((size_t) indent + 1);
1626   memset (s_indent, ' ', (size_t) indent);
1627   s_indent[indent] = '\0';
1628   
1629   if (df)
1630     {
1631       df_dump_top (bb, outf);
1632       putc ('\n', outf);
1633     }
1634
1635   for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb)); insn != last;
1636        insn = NEXT_INSN (insn))
1637     print_rtl_single (outf, insn);
1638
1639   if (df)
1640     {
1641       df_dump_bottom (bb, outf);
1642       putc ('\n', outf);
1643     }
1644
1645 }
1646 \f
1647 /* Like print_rtl, but also print out live information for the start of each
1648    basic block.  */
1649
1650 void
1651 print_rtl_with_bb (FILE *outf, const_rtx rtx_first)
1652 {
1653   const_rtx tmp_rtx;
1654   if (rtx_first == 0)
1655     fprintf (outf, "(nil)\n");
1656   else
1657     {
1658       enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
1659       int max_uid = get_max_uid ();
1660       basic_block *start = XCNEWVEC (basic_block, max_uid);
1661       basic_block *end = XCNEWVEC (basic_block, max_uid);
1662       enum bb_state *in_bb_p = XCNEWVEC (enum bb_state, max_uid);
1663
1664       basic_block bb;
1665
1666       if (df)
1667         df_dump_start (outf);
1668
1669       FOR_EACH_BB_REVERSE (bb)
1670         {
1671           rtx x;
1672
1673           start[INSN_UID (BB_HEAD (bb))] = bb;
1674           end[INSN_UID (BB_END (bb))] = bb;
1675           for (x = BB_HEAD (bb); x != NULL_RTX; x = NEXT_INSN (x))
1676             {
1677               enum bb_state state = IN_MULTIPLE_BB;
1678
1679               if (in_bb_p[INSN_UID (x)] == NOT_IN_BB)
1680                 state = IN_ONE_BB;
1681               in_bb_p[INSN_UID (x)] = state;
1682
1683               if (x == BB_END (bb))
1684                 break;
1685             }
1686         }
1687
1688       for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
1689         {
1690           int did_output;
1691           if ((bb = start[INSN_UID (tmp_rtx)]) != NULL)
1692             {
1693               edge e;
1694               edge_iterator ei;
1695               
1696               fprintf (outf, ";; Start of basic block (");
1697               FOR_EACH_EDGE (e, ei, bb->preds)
1698                 fprintf (outf, " %d", e->src->index);
1699               fprintf (outf, ") -> %d\n", bb->index);
1700
1701               if (df)
1702                 {
1703                   df_dump_top (bb, outf);
1704                   putc ('\n', outf);
1705                 }
1706               FOR_EACH_EDGE (e, ei, bb->preds)
1707                 {
1708                   fputs (";; Pred edge ", outf);
1709                   dump_edge_info (outf, e, 0);
1710                   fputc ('\n', outf);
1711                 }
1712             }
1713
1714           if (in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB
1715               && !NOTE_P (tmp_rtx)
1716               && !BARRIER_P (tmp_rtx))
1717             fprintf (outf, ";; Insn is not within a basic block\n");
1718           else if (in_bb_p[INSN_UID (tmp_rtx)] == IN_MULTIPLE_BB)
1719             fprintf (outf, ";; Insn is in multiple basic blocks\n");
1720
1721           did_output = print_rtl_single (outf, tmp_rtx);
1722
1723           if ((bb = end[INSN_UID (tmp_rtx)]) != NULL)
1724             {
1725               edge e;
1726               edge_iterator ei;
1727
1728               fprintf (outf, ";; End of basic block %d -> (", bb->index);
1729               FOR_EACH_EDGE (e, ei, bb->succs)
1730                 fprintf (outf, " %d", e->dest->index);
1731               fprintf (outf, ")\n");
1732
1733               if (df)
1734                 {
1735                   df_dump_bottom (bb, outf);
1736                   putc ('\n', outf);
1737                 }
1738               putc ('\n', outf);
1739               FOR_EACH_EDGE (e, ei, bb->succs)
1740                 {
1741                   fputs (";; Succ edge ", outf);
1742                   dump_edge_info (outf, e, 1);
1743                   fputc ('\n', outf);
1744                 }
1745             }
1746           if (did_output)
1747             putc ('\n', outf);
1748         }
1749
1750       free (start);
1751       free (end);
1752       free (in_bb_p);
1753     }
1754
1755   if (crtl->epilogue_delay_list != 0)
1756     {
1757       fprintf (outf, "\n;; Insns in epilogue delay list:\n\n");
1758       for (tmp_rtx = crtl->epilogue_delay_list; tmp_rtx != 0;
1759            tmp_rtx = XEXP (tmp_rtx, 1))
1760         print_rtl_single (outf, XEXP (tmp_rtx, 0));
1761     }
1762 }
1763 \f
1764 void
1765 update_br_prob_note (basic_block bb)
1766 {
1767   rtx note;
1768   if (!JUMP_P (BB_END (bb)))
1769     return;
1770   note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX);
1771   if (!note || INTVAL (XEXP (note, 0)) == BRANCH_EDGE (bb)->probability)
1772     return;
1773   XEXP (note, 0) = GEN_INT (BRANCH_EDGE (bb)->probability);
1774 }
1775
1776 /* Get the last insn associated with block BB (that includes barriers and
1777    tablejumps after BB).  */
1778 rtx
1779 get_last_bb_insn (basic_block bb)
1780 {
1781   rtx tmp;
1782   rtx end = BB_END (bb);
1783
1784   /* Include any jump table following the basic block.  */
1785   if (tablejump_p (end, NULL, &tmp))
1786     end = tmp;
1787
1788   /* Include any barriers that may follow the basic block.  */
1789   tmp = next_nonnote_insn_bb (end);
1790   while (tmp && BARRIER_P (tmp))
1791     {
1792       end = tmp;
1793       tmp = next_nonnote_insn_bb (end);
1794     }
1795
1796   return end;
1797 }
1798 \f
1799 /* Verify the CFG and RTL consistency common for both underlying RTL and
1800    cfglayout RTL.
1801
1802    Currently it does following checks:
1803
1804    - overlapping of basic blocks
1805    - insns with wrong BLOCK_FOR_INSN pointers
1806    - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note)
1807    - tails of basic blocks (ensure that boundary is necessary)
1808    - scans body of the basic block for JUMP_INSN, CODE_LABEL
1809      and NOTE_INSN_BASIC_BLOCK
1810    - verify that no fall_thru edge crosses hot/cold partition boundaries
1811    - verify that there are no pending RTL branch predictions
1812
1813    In future it can be extended check a lot of other stuff as well
1814    (reachability of basic blocks, life information, etc. etc.).  */
1815
1816 static int
1817 rtl_verify_flow_info_1 (void)
1818 {
1819   rtx x;
1820   int err = 0;
1821   basic_block bb;
1822
1823   /* Check the general integrity of the basic blocks.  */
1824   FOR_EACH_BB_REVERSE (bb)
1825     {
1826       rtx insn;
1827
1828       if (!(bb->flags & BB_RTL))
1829         {
1830           error ("BB_RTL flag not set for block %d", bb->index);
1831           err = 1;
1832         }
1833
1834       FOR_BB_INSNS (bb, insn)
1835         if (BLOCK_FOR_INSN (insn) != bb)
1836           {
1837             error ("insn %d basic block pointer is %d, should be %d",
1838                    INSN_UID (insn),
1839                    BLOCK_FOR_INSN (insn) ? BLOCK_FOR_INSN (insn)->index : 0,
1840                    bb->index);
1841             err = 1;
1842           }
1843
1844       for (insn = bb->il.rtl->header; insn; insn = NEXT_INSN (insn))
1845         if (!BARRIER_P (insn)
1846             && BLOCK_FOR_INSN (insn) != NULL)
1847           {
1848             error ("insn %d in header of bb %d has non-NULL basic block",
1849                    INSN_UID (insn), bb->index);
1850             err = 1;
1851           }
1852       for (insn = bb->il.rtl->footer; insn; insn = NEXT_INSN (insn))
1853         if (!BARRIER_P (insn)
1854             && BLOCK_FOR_INSN (insn) != NULL)
1855           {
1856             error ("insn %d in footer of bb %d has non-NULL basic block",
1857                    INSN_UID (insn), bb->index);
1858             err = 1;
1859           }
1860     }
1861
1862   /* Now check the basic blocks (boundaries etc.) */
1863   FOR_EACH_BB_REVERSE (bb)
1864     {
1865       int n_fallthru = 0, n_eh = 0, n_call = 0, n_abnormal = 0, n_branch = 0;
1866       edge e, fallthru = NULL;
1867       rtx note;
1868       edge_iterator ei;
1869
1870       if (JUMP_P (BB_END (bb))
1871           && (note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX))
1872           && EDGE_COUNT (bb->succs) >= 2
1873           && any_condjump_p (BB_END (bb)))
1874         {
1875           if (INTVAL (XEXP (note, 0)) != BRANCH_EDGE (bb)->probability
1876               && profile_status != PROFILE_ABSENT)
1877             {
1878               error ("verify_flow_info: REG_BR_PROB does not match cfg %wi %i",
1879                      INTVAL (XEXP (note, 0)), BRANCH_EDGE (bb)->probability);
1880               err = 1;
1881             }
1882         }
1883       FOR_EACH_EDGE (e, ei, bb->succs)
1884         {
1885           if (e->flags & EDGE_FALLTHRU)
1886             {
1887               n_fallthru++, fallthru = e;
1888               if ((e->flags & EDGE_CROSSING)
1889                   || (BB_PARTITION (e->src) != BB_PARTITION (e->dest)
1890                       && e->src != ENTRY_BLOCK_PTR
1891                       && e->dest != EXIT_BLOCK_PTR))
1892             {
1893                   error ("fallthru edge crosses section boundary (bb %i)",
1894                          e->src->index);
1895                   err = 1;
1896                 }
1897             }
1898
1899           if ((e->flags & ~(EDGE_DFS_BACK
1900                             | EDGE_CAN_FALLTHRU
1901                             | EDGE_IRREDUCIBLE_LOOP
1902                             | EDGE_LOOP_EXIT
1903                             | EDGE_CROSSING)) == 0)
1904             n_branch++;
1905
1906           if (e->flags & EDGE_ABNORMAL_CALL)
1907             n_call++;
1908
1909           if (e->flags & EDGE_EH)
1910             n_eh++;
1911           else if (e->flags & EDGE_ABNORMAL)
1912             n_abnormal++;
1913         }
1914
1915       if (n_eh && !find_reg_note (BB_END (bb), REG_EH_REGION, NULL_RTX))
1916         {
1917           error ("missing REG_EH_REGION note in the end of bb %i", bb->index);
1918           err = 1;
1919         }
1920       if (n_eh > 1)
1921         {
1922           error ("too many eh edges %i", bb->index);
1923           err = 1;
1924         }
1925       if (n_branch
1926           && (!JUMP_P (BB_END (bb))
1927               || (n_branch > 1 && (any_uncondjump_p (BB_END (bb))
1928                                    || any_condjump_p (BB_END (bb))))))
1929         {
1930           error ("too many outgoing branch edges from bb %i", bb->index);
1931           err = 1;
1932         }
1933       if (n_fallthru && any_uncondjump_p (BB_END (bb)))
1934         {
1935           error ("fallthru edge after unconditional jump %i", bb->index);
1936           err = 1;
1937         }
1938       if (n_branch != 1 && any_uncondjump_p (BB_END (bb)))
1939         {
1940           error ("wrong number of branch edges after unconditional jump %i",
1941                  bb->index);
1942           err = 1;
1943         }
1944       if (n_branch != 1 && any_condjump_p (BB_END (bb))
1945           && JUMP_LABEL (BB_END (bb)) != BB_HEAD (fallthru->dest))
1946         {
1947           error ("wrong amount of branch edges after conditional jump %i",
1948                  bb->index);
1949           err = 1;
1950         }
1951       if (n_call && !CALL_P (BB_END (bb)))
1952         {
1953           error ("call edges for non-call insn in bb %i", bb->index);
1954           err = 1;
1955         }
1956       if (n_abnormal
1957           && (!CALL_P (BB_END (bb)) && n_call != n_abnormal)
1958           && (!JUMP_P (BB_END (bb))
1959               || any_condjump_p (BB_END (bb))
1960               || any_uncondjump_p (BB_END (bb))))
1961         {
1962           error ("abnormal edges for no purpose in bb %i", bb->index);
1963           err = 1;
1964         }
1965
1966       for (x = BB_HEAD (bb); x != NEXT_INSN (BB_END (bb)); x = NEXT_INSN (x))
1967         /* We may have a barrier inside a basic block before dead code
1968            elimination.  There is no BLOCK_FOR_INSN field in a barrier.  */
1969         if (!BARRIER_P (x) && BLOCK_FOR_INSN (x) != bb)
1970           {
1971             debug_rtx (x);
1972             if (! BLOCK_FOR_INSN (x))
1973               error
1974                 ("insn %d inside basic block %d but block_for_insn is NULL",
1975                  INSN_UID (x), bb->index);
1976             else
1977               error
1978                 ("insn %d inside basic block %d but block_for_insn is %i",
1979                  INSN_UID (x), bb->index, BLOCK_FOR_INSN (x)->index);
1980
1981             err = 1;
1982           }
1983
1984       /* OK pointers are correct.  Now check the header of basic
1985          block.  It ought to contain optional CODE_LABEL followed
1986          by NOTE_BASIC_BLOCK.  */
1987       x = BB_HEAD (bb);
1988       if (LABEL_P (x))
1989         {
1990           if (BB_END (bb) == x)
1991             {
1992               error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
1993                      bb->index);
1994               err = 1;
1995             }
1996
1997           x = NEXT_INSN (x);
1998         }
1999
2000       if (!NOTE_INSN_BASIC_BLOCK_P (x) || NOTE_BASIC_BLOCK (x) != bb)
2001         {
2002           error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
2003                  bb->index);
2004           err = 1;
2005         }
2006
2007       if (BB_END (bb) == x)
2008         /* Do checks for empty blocks here.  */
2009         ;
2010       else
2011         for (x = NEXT_INSN (x); x; x = NEXT_INSN (x))
2012           {
2013             if (NOTE_INSN_BASIC_BLOCK_P (x))
2014               {
2015                 error ("NOTE_INSN_BASIC_BLOCK %d in middle of basic block %d",
2016                        INSN_UID (x), bb->index);
2017                 err = 1;
2018               }
2019
2020             if (x == BB_END (bb))
2021               break;
2022
2023             if (control_flow_insn_p (x))
2024               {
2025                 error ("in basic block %d:", bb->index);
2026                 fatal_insn ("flow control insn inside a basic block", x);
2027               }
2028           }
2029     }
2030
2031   /* Clean up.  */
2032   return err;
2033 }
2034
2035 /* Verify the CFG and RTL consistency common for both underlying RTL and
2036    cfglayout RTL.
2037
2038    Currently it does following checks:
2039    - all checks of rtl_verify_flow_info_1
2040    - test head/end pointers
2041    - check that all insns are in the basic blocks
2042      (except the switch handling code, barriers and notes)
2043    - check that all returns are followed by barriers
2044    - check that all fallthru edge points to the adjacent blocks.  */
2045
2046 static int
2047 rtl_verify_flow_info (void)
2048 {
2049   basic_block bb;
2050   int err = rtl_verify_flow_info_1 ();
2051   rtx x;
2052   rtx last_head = get_last_insn ();
2053   basic_block *bb_info;
2054   int num_bb_notes;
2055   const rtx rtx_first = get_insns ();
2056   basic_block last_bb_seen = ENTRY_BLOCK_PTR, curr_bb = NULL;
2057   const int max_uid = get_max_uid ();
2058
2059   bb_info = XCNEWVEC (basic_block, max_uid);
2060
2061   FOR_EACH_BB_REVERSE (bb)
2062     {
2063       edge e;
2064       edge_iterator ei;
2065       rtx head = BB_HEAD (bb);
2066       rtx end = BB_END (bb);
2067
2068       for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
2069         {
2070           /* Verify the end of the basic block is in the INSN chain.  */
2071           if (x == end)
2072             break;
2073
2074           /* And that the code outside of basic blocks has NULL bb field.  */
2075         if (!BARRIER_P (x)
2076             && BLOCK_FOR_INSN (x) != NULL)
2077           {
2078             error ("insn %d outside of basic blocks has non-NULL bb field",
2079                    INSN_UID (x));
2080             err = 1;
2081           }
2082         }
2083
2084       if (!x)
2085         {
2086           error ("end insn %d for block %d not found in the insn stream",
2087                  INSN_UID (end), bb->index);
2088           err = 1;
2089         }
2090
2091       /* Work backwards from the end to the head of the basic block
2092          to verify the head is in the RTL chain.  */
2093       for (; x != NULL_RTX; x = PREV_INSN (x))
2094         {
2095           /* While walking over the insn chain, verify insns appear
2096              in only one basic block.  */
2097           if (bb_info[INSN_UID (x)] != NULL)
2098             {
2099               error ("insn %d is in multiple basic blocks (%d and %d)",
2100                      INSN_UID (x), bb->index, bb_info[INSN_UID (x)]->index);
2101               err = 1;
2102             }
2103
2104           bb_info[INSN_UID (x)] = bb;
2105
2106           if (x == head)
2107             break;
2108         }
2109       if (!x)
2110         {
2111           error ("head insn %d for block %d not found in the insn stream",
2112                  INSN_UID (head), bb->index);
2113           err = 1;
2114         }
2115
2116       last_head = PREV_INSN (x);
2117
2118       FOR_EACH_EDGE (e, ei, bb->succs)
2119         if (e->flags & EDGE_FALLTHRU)
2120           break;
2121       if (!e)
2122         {
2123           rtx insn;
2124
2125           /* Ensure existence of barrier in BB with no fallthru edges.  */
2126           for (insn = NEXT_INSN (BB_END (bb)); ; insn = NEXT_INSN (insn))
2127             {
2128               if (!insn || NOTE_INSN_BASIC_BLOCK_P (insn))
2129                 {
2130                   error ("missing barrier after block %i", bb->index);
2131                   err = 1;
2132                   break;
2133                 }
2134               if (BARRIER_P (insn))
2135                 break;
2136             }
2137         }
2138       else if (e->src != ENTRY_BLOCK_PTR
2139                && e->dest != EXIT_BLOCK_PTR)
2140         {
2141           rtx insn;
2142
2143           if (e->src->next_bb != e->dest)
2144             {
2145               error
2146                 ("verify_flow_info: Incorrect blocks for fallthru %i->%i",
2147                  e->src->index, e->dest->index);
2148               err = 1;
2149             }
2150           else
2151             for (insn = NEXT_INSN (BB_END (e->src)); insn != BB_HEAD (e->dest);
2152                  insn = NEXT_INSN (insn))
2153               if (BARRIER_P (insn) || INSN_P (insn))
2154                 {
2155                   error ("verify_flow_info: Incorrect fallthru %i->%i",
2156                          e->src->index, e->dest->index);
2157                   fatal_insn ("wrong insn in the fallthru edge", insn);
2158                   err = 1;
2159                 }
2160         }
2161     }
2162
2163   for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
2164     {
2165       /* Check that the code before the first basic block has NULL
2166          bb field.  */
2167       if (!BARRIER_P (x)
2168           && BLOCK_FOR_INSN (x) != NULL)
2169         {
2170           error ("insn %d outside of basic blocks has non-NULL bb field",
2171                  INSN_UID (x));
2172           err = 1;
2173         }
2174     }
2175   free (bb_info);
2176
2177   num_bb_notes = 0;
2178   last_bb_seen = ENTRY_BLOCK_PTR;
2179
2180   for (x = rtx_first; x; x = NEXT_INSN (x))
2181     {
2182       if (NOTE_INSN_BASIC_BLOCK_P (x))
2183         {
2184           bb = NOTE_BASIC_BLOCK (x);
2185
2186           num_bb_notes++;
2187           if (bb != last_bb_seen->next_bb)
2188             internal_error ("basic blocks not laid down consecutively");
2189
2190           curr_bb = last_bb_seen = bb;
2191         }
2192
2193       if (!curr_bb)
2194         {
2195           switch (GET_CODE (x))
2196             {
2197             case BARRIER:
2198             case NOTE:
2199               break;
2200
2201             case CODE_LABEL:
2202               /* An addr_vec is placed outside any basic block.  */
2203               if (NEXT_INSN (x)
2204                   && JUMP_TABLE_DATA_P (NEXT_INSN (x)))
2205                 x = NEXT_INSN (x);
2206
2207               /* But in any case, non-deletable labels can appear anywhere.  */
2208               break;
2209
2210             default:
2211               fatal_insn ("insn outside basic block", x);
2212             }
2213         }
2214
2215       if (JUMP_P (x)
2216           && returnjump_p (x) && ! condjump_p (x)
2217           && ! (next_nonnote_insn (x) && BARRIER_P (next_nonnote_insn (x))))
2218             fatal_insn ("return not followed by barrier", x);
2219       if (curr_bb && x == BB_END (curr_bb))
2220         curr_bb = NULL;
2221     }
2222
2223   if (num_bb_notes != n_basic_blocks - NUM_FIXED_BLOCKS)
2224     internal_error
2225       ("number of bb notes in insn chain (%d) != n_basic_blocks (%d)",
2226        num_bb_notes, n_basic_blocks);
2227
2228    return err;
2229 }
2230 \f
2231 /* Assume that the preceding pass has possibly eliminated jump instructions
2232    or converted the unconditional jumps.  Eliminate the edges from CFG.
2233    Return true if any edges are eliminated.  */
2234
2235 bool
2236 purge_dead_edges (basic_block bb)
2237 {
2238   edge e;
2239   rtx insn = BB_END (bb), note;
2240   bool purged = false;
2241   bool found;
2242   edge_iterator ei;
2243
2244   if (DEBUG_INSN_P (insn) && insn != BB_HEAD (bb))
2245     do
2246       insn = PREV_INSN (insn);
2247     while ((DEBUG_INSN_P (insn) || NOTE_P (insn)) && insn != BB_HEAD (bb));
2248
2249   /* If this instruction cannot trap, remove REG_EH_REGION notes.  */
2250   if (NONJUMP_INSN_P (insn)
2251       && (note = find_reg_note (insn, REG_EH_REGION, NULL)))
2252     {
2253       rtx eqnote;
2254
2255       if (! may_trap_p (PATTERN (insn))
2256           || ((eqnote = find_reg_equal_equiv_note (insn))
2257               && ! may_trap_p (XEXP (eqnote, 0))))
2258         remove_note (insn, note);
2259     }
2260
2261   /* Cleanup abnormal edges caused by exceptions or non-local gotos.  */
2262   for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2263     {
2264       bool remove = false;
2265
2266       /* There are three types of edges we need to handle correctly here: EH
2267          edges, abnormal call EH edges, and abnormal call non-EH edges.  The
2268          latter can appear when nonlocal gotos are used.  */
2269       if (e->flags & EDGE_ABNORMAL_CALL)
2270         {
2271           if (!CALL_P (insn))
2272             remove = true;
2273           else if (can_nonlocal_goto (insn))
2274             ;
2275           else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
2276             ;
2277           else
2278             remove = true;
2279         }
2280       else if (e->flags & EDGE_EH)
2281         remove = !can_throw_internal (insn);
2282
2283       if (remove)
2284         {
2285           remove_edge (e);
2286           df_set_bb_dirty (bb);
2287           purged = true;
2288         }
2289       else
2290         ei_next (&ei);
2291     }
2292
2293   if (JUMP_P (insn))
2294     {
2295       rtx note;
2296       edge b,f;
2297       edge_iterator ei;
2298
2299       /* We do care only about conditional jumps and simplejumps.  */
2300       if (!any_condjump_p (insn)
2301           && !returnjump_p (insn)
2302           && !simplejump_p (insn))
2303         return purged;
2304
2305       /* Branch probability/prediction notes are defined only for
2306          condjumps.  We've possibly turned condjump into simplejump.  */
2307       if (simplejump_p (insn))
2308         {
2309           note = find_reg_note (insn, REG_BR_PROB, NULL);
2310           if (note)
2311             remove_note (insn, note);
2312           while ((note = find_reg_note (insn, REG_BR_PRED, NULL)))
2313             remove_note (insn, note);
2314         }
2315
2316       for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2317         {
2318           /* Avoid abnormal flags to leak from computed jumps turned
2319              into simplejumps.  */
2320
2321           e->flags &= ~EDGE_ABNORMAL;
2322
2323           /* See if this edge is one we should keep.  */
2324           if ((e->flags & EDGE_FALLTHRU) && any_condjump_p (insn))
2325             /* A conditional jump can fall through into the next
2326                block, so we should keep the edge.  */
2327             {
2328               ei_next (&ei);
2329               continue;
2330             }
2331           else if (e->dest != EXIT_BLOCK_PTR
2332                    && BB_HEAD (e->dest) == JUMP_LABEL (insn))
2333             /* If the destination block is the target of the jump,
2334                keep the edge.  */
2335             {
2336               ei_next (&ei);
2337               continue;
2338             }
2339           else if (e->dest == EXIT_BLOCK_PTR && returnjump_p (insn))
2340             /* If the destination block is the exit block, and this
2341                instruction is a return, then keep the edge.  */
2342             {
2343               ei_next (&ei);
2344               continue;
2345             }
2346           else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
2347             /* Keep the edges that correspond to exceptions thrown by
2348                this instruction and rematerialize the EDGE_ABNORMAL
2349                flag we just cleared above.  */
2350             {
2351               e->flags |= EDGE_ABNORMAL;
2352               ei_next (&ei);
2353               continue;
2354             }
2355
2356           /* We do not need this edge.  */
2357           df_set_bb_dirty (bb);
2358           purged = true;
2359           remove_edge (e);
2360         }
2361
2362       if (EDGE_COUNT (bb->succs) == 0 || !purged)
2363         return purged;
2364
2365       if (dump_file)
2366         fprintf (dump_file, "Purged edges from bb %i\n", bb->index);
2367
2368       if (!optimize)
2369         return purged;
2370
2371       /* Redistribute probabilities.  */
2372       if (single_succ_p (bb))
2373         {
2374           single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
2375           single_succ_edge (bb)->count = bb->count;
2376         }
2377       else
2378         {
2379           note = find_reg_note (insn, REG_BR_PROB, NULL);
2380           if (!note)
2381             return purged;
2382
2383           b = BRANCH_EDGE (bb);
2384           f = FALLTHRU_EDGE (bb);
2385           b->probability = INTVAL (XEXP (note, 0));
2386           f->probability = REG_BR_PROB_BASE - b->probability;
2387           b->count = bb->count * b->probability / REG_BR_PROB_BASE;
2388           f->count = bb->count * f->probability / REG_BR_PROB_BASE;
2389         }
2390
2391       return purged;
2392     }
2393   else if (CALL_P (insn) && SIBLING_CALL_P (insn))
2394     {
2395       /* First, there should not be any EH or ABCALL edges resulting
2396          from non-local gotos and the like.  If there were, we shouldn't
2397          have created the sibcall in the first place.  Second, there
2398          should of course never have been a fallthru edge.  */
2399       gcc_assert (single_succ_p (bb));
2400       gcc_assert (single_succ_edge (bb)->flags
2401                   == (EDGE_SIBCALL | EDGE_ABNORMAL));
2402
2403       return 0;
2404     }
2405
2406   /* If we don't see a jump insn, we don't know exactly why the block would
2407      have been broken at this point.  Look for a simple, non-fallthru edge,
2408      as these are only created by conditional branches.  If we find such an
2409      edge we know that there used to be a jump here and can then safely
2410      remove all non-fallthru edges.  */
2411   found = false;
2412   FOR_EACH_EDGE (e, ei, bb->succs)
2413     if (! (e->flags & (EDGE_COMPLEX | EDGE_FALLTHRU)))
2414       {
2415         found = true;
2416         break;
2417       }
2418
2419   if (!found)
2420     return purged;
2421
2422   /* Remove all but the fake and fallthru edges.  The fake edge may be
2423      the only successor for this block in the case of noreturn
2424      calls.  */
2425   for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2426     {
2427       if (!(e->flags & (EDGE_FALLTHRU | EDGE_FAKE)))
2428         {
2429           df_set_bb_dirty (bb);
2430           remove_edge (e);
2431           purged = true;
2432         }
2433       else
2434         ei_next (&ei);
2435     }
2436
2437   gcc_assert (single_succ_p (bb));
2438
2439   single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
2440   single_succ_edge (bb)->count = bb->count;
2441
2442   if (dump_file)
2443     fprintf (dump_file, "Purged non-fallthru edges from bb %i\n",
2444              bb->index);
2445   return purged;
2446 }
2447
2448 /* Search all basic blocks for potentially dead edges and purge them.  Return
2449    true if some edge has been eliminated.  */
2450
2451 bool
2452 purge_all_dead_edges (void)
2453 {
2454   int purged = false;
2455   basic_block bb;
2456
2457   FOR_EACH_BB (bb)
2458     {
2459       bool purged_here = purge_dead_edges (bb);
2460
2461       purged |= purged_here;
2462     }
2463
2464   return purged;
2465 }
2466
2467 /* Same as split_block but update cfg_layout structures.  */
2468
2469 static basic_block
2470 cfg_layout_split_block (basic_block bb, void *insnp)
2471 {
2472   rtx insn = (rtx) insnp;
2473   basic_block new_bb = rtl_split_block (bb, insn);
2474
2475   new_bb->il.rtl->footer = bb->il.rtl->footer;
2476   bb->il.rtl->footer = NULL;
2477
2478   return new_bb;
2479 }
2480
2481 /* Redirect Edge to DEST.  */
2482 static edge
2483 cfg_layout_redirect_edge_and_branch (edge e, basic_block dest)
2484 {
2485   basic_block src = e->src;
2486   edge ret;
2487
2488   if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
2489     return NULL;
2490
2491   if (e->dest == dest)
2492     return e;
2493
2494   if (e->src != ENTRY_BLOCK_PTR
2495       && (ret = try_redirect_by_replacing_jump (e, dest, true)))
2496     {
2497       df_set_bb_dirty (src);
2498       return ret;
2499     }
2500
2501   if (e->src == ENTRY_BLOCK_PTR
2502       && (e->flags & EDGE_FALLTHRU) && !(e->flags & EDGE_COMPLEX))
2503     {
2504       if (dump_file)
2505         fprintf (dump_file, "Redirecting entry edge from bb %i to %i\n",
2506                  e->src->index, dest->index);
2507
2508       df_set_bb_dirty (e->src);
2509       redirect_edge_succ (e, dest);
2510       return e;
2511     }
2512
2513   /* Redirect_edge_and_branch may decide to turn branch into fallthru edge
2514      in the case the basic block appears to be in sequence.  Avoid this
2515      transformation.  */
2516
2517   if (e->flags & EDGE_FALLTHRU)
2518     {
2519       /* Redirect any branch edges unified with the fallthru one.  */
2520       if (JUMP_P (BB_END (src))
2521           && label_is_jump_target_p (BB_HEAD (e->dest),
2522                                      BB_END (src)))
2523         {
2524           edge redirected;
2525
2526           if (dump_file)
2527             fprintf (dump_file, "Fallthru edge unified with branch "
2528                      "%i->%i redirected to %i\n",
2529                      e->src->index, e->dest->index, dest->index);
2530           e->flags &= ~EDGE_FALLTHRU;
2531           redirected = redirect_branch_edge (e, dest);
2532           gcc_assert (redirected);
2533           e->flags |= EDGE_FALLTHRU;
2534           df_set_bb_dirty (e->src);
2535           return e;
2536         }
2537       /* In case we are redirecting fallthru edge to the branch edge
2538          of conditional jump, remove it.  */
2539       if (EDGE_COUNT (src->succs) == 2)
2540         {
2541           /* Find the edge that is different from E.  */
2542           edge s = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e);
2543
2544           if (s->dest == dest
2545               && any_condjump_p (BB_END (src))
2546               && onlyjump_p (BB_END (src)))
2547             delete_insn (BB_END (src));
2548         }
2549       ret = redirect_edge_succ_nodup (e, dest);
2550       if (dump_file)
2551         fprintf (dump_file, "Fallthru edge %i->%i redirected to %i\n",
2552                  e->src->index, e->dest->index, dest->index);
2553     }
2554   else
2555     ret = redirect_branch_edge (e, dest);
2556
2557   /* We don't want simplejumps in the insn stream during cfglayout.  */
2558   gcc_assert (!simplejump_p (BB_END (src)));
2559
2560   df_set_bb_dirty (src);
2561   return ret;
2562 }
2563
2564 /* Simple wrapper as we always can redirect fallthru edges.  */
2565 static basic_block
2566 cfg_layout_redirect_edge_and_branch_force (edge e, basic_block dest)
2567 {
2568   edge redirected = cfg_layout_redirect_edge_and_branch (e, dest);
2569
2570   gcc_assert (redirected);
2571   return NULL;
2572 }
2573
2574 /* Same as delete_basic_block but update cfg_layout structures.  */
2575
2576 static void
2577 cfg_layout_delete_block (basic_block bb)
2578 {
2579   rtx insn, next, prev = PREV_INSN (BB_HEAD (bb)), *to, remaints;
2580
2581   if (bb->il.rtl->header)
2582     {
2583       next = BB_HEAD (bb);
2584       if (prev)
2585         NEXT_INSN (prev) = bb->il.rtl->header;
2586       else
2587         set_first_insn (bb->il.rtl->header);
2588       PREV_INSN (bb->il.rtl->header) = prev;
2589       insn = bb->il.rtl->header;
2590       while (NEXT_INSN (insn))
2591         insn = NEXT_INSN (insn);
2592       NEXT_INSN (insn) = next;
2593       PREV_INSN (next) = insn;
2594     }
2595   next = NEXT_INSN (BB_END (bb));
2596   if (bb->il.rtl->footer)
2597     {
2598       insn = bb->il.rtl->footer;
2599       while (insn)
2600         {
2601           if (BARRIER_P (insn))
2602             {
2603               if (PREV_INSN (insn))
2604                 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2605               else
2606                 bb->il.rtl->footer = NEXT_INSN (insn);
2607               if (NEXT_INSN (insn))
2608                 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2609             }
2610           if (LABEL_P (insn))
2611             break;
2612           insn = NEXT_INSN (insn);
2613         }
2614       if (bb->il.rtl->footer)
2615         {
2616           insn = BB_END (bb);
2617           NEXT_INSN (insn) = bb->il.rtl->footer;
2618           PREV_INSN (bb->il.rtl->footer) = insn;
2619           while (NEXT_INSN (insn))
2620             insn = NEXT_INSN (insn);
2621           NEXT_INSN (insn) = next;
2622           if (next)
2623             PREV_INSN (next) = insn;
2624           else
2625             set_last_insn (insn);
2626         }
2627     }
2628   if (bb->next_bb != EXIT_BLOCK_PTR)
2629     to = &bb->next_bb->il.rtl->header;
2630   else
2631     to = &cfg_layout_function_footer;
2632
2633   rtl_delete_block (bb);
2634
2635   if (prev)
2636     prev = NEXT_INSN (prev);
2637   else
2638     prev = get_insns ();
2639   if (next)
2640     next = PREV_INSN (next);
2641   else
2642     next = get_last_insn ();
2643
2644   if (next && NEXT_INSN (next) != prev)
2645     {
2646       remaints = unlink_insn_chain (prev, next);
2647       insn = remaints;
2648       while (NEXT_INSN (insn))
2649         insn = NEXT_INSN (insn);
2650       NEXT_INSN (insn) = *to;
2651       if (*to)
2652         PREV_INSN (*to) = insn;
2653       *to = remaints;
2654     }
2655 }
2656
2657 /* Return true when blocks A and B can be safely merged.  */
2658
2659 static bool
2660 cfg_layout_can_merge_blocks_p (basic_block a, basic_block b)
2661 {
2662   /* If we are partitioning hot/cold basic blocks, we don't want to
2663      mess up unconditional or indirect jumps that cross between hot
2664      and cold sections.
2665
2666      Basic block partitioning may result in some jumps that appear to
2667      be optimizable (or blocks that appear to be mergeable), but which really
2668      must be left untouched (they are required to make it safely across
2669      partition boundaries).  See  the comments at the top of
2670      bb-reorder.c:partition_hot_cold_basic_blocks for complete details.  */
2671
2672   if (BB_PARTITION (a) != BB_PARTITION (b))
2673     return false;
2674
2675   /* There must be exactly one edge in between the blocks.  */
2676   return (single_succ_p (a)
2677           && single_succ (a) == b
2678           && single_pred_p (b) == 1
2679           && a != b
2680           /* Must be simple edge.  */
2681           && !(single_succ_edge (a)->flags & EDGE_COMPLEX)
2682           && a != ENTRY_BLOCK_PTR && b != EXIT_BLOCK_PTR
2683           /* If the jump insn has side effects, we can't kill the edge.
2684              When not optimizing, try_redirect_by_replacing_jump will
2685              not allow us to redirect an edge by replacing a table jump.  */
2686           && (!JUMP_P (BB_END (a))
2687               || ((!optimize || reload_completed)
2688                   ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
2689 }
2690
2691 /* Merge block A and B.  The blocks must be mergeable.  */
2692
2693 static void
2694 cfg_layout_merge_blocks (basic_block a, basic_block b)
2695 {
2696 #ifdef ENABLE_CHECKING
2697   gcc_assert (cfg_layout_can_merge_blocks_p (a, b));
2698 #endif
2699
2700   if (dump_file)
2701     fprintf (dump_file, "merging block %d into block %d\n", b->index, a->index);
2702
2703   /* If there was a CODE_LABEL beginning B, delete it.  */
2704   if (LABEL_P (BB_HEAD (b)))
2705     {
2706       delete_insn (BB_HEAD (b));
2707     }
2708
2709   /* We should have fallthru edge in a, or we can do dummy redirection to get
2710      it cleaned up.  */
2711   if (JUMP_P (BB_END (a)))
2712     try_redirect_by_replacing_jump (EDGE_SUCC (a, 0), b, true);
2713   gcc_assert (!JUMP_P (BB_END (a)));
2714
2715   /* When not optimizing and the edge is the only place in RTL which holds
2716      some unique locus, emit a nop with that locus in between.  */
2717   if (!optimize && EDGE_SUCC (a, 0)->goto_locus)
2718     {
2719       rtx insn = BB_END (a), end = PREV_INSN (BB_HEAD (a));
2720       int goto_locus = EDGE_SUCC (a, 0)->goto_locus;
2721
2722       while (insn != end && (!INSN_P (insn) || INSN_LOCATOR (insn) == 0))
2723         insn = PREV_INSN (insn);
2724       if (insn != end && locator_eq (INSN_LOCATOR (insn), goto_locus))
2725         goto_locus = 0;
2726       else
2727         {
2728           insn = BB_HEAD (b);
2729           end = NEXT_INSN (BB_END (b));
2730           while (insn != end && !INSN_P (insn))
2731             insn = NEXT_INSN (insn);
2732           if (insn != end && INSN_LOCATOR (insn) != 0
2733               && locator_eq (INSN_LOCATOR (insn), goto_locus))
2734             goto_locus = 0;
2735         }
2736       if (goto_locus)
2737         {
2738           BB_END (a) = emit_insn_after_noloc (gen_nop (), BB_END (a), a);
2739           INSN_LOCATOR (BB_END (a)) = goto_locus;
2740         }
2741     }
2742
2743   /* Possible line number notes should appear in between.  */
2744   if (b->il.rtl->header)
2745     {
2746       rtx first = BB_END (a), last;
2747
2748       last = emit_insn_after_noloc (b->il.rtl->header, BB_END (a), a);
2749       delete_insn_chain (NEXT_INSN (first), last, false);
2750       b->il.rtl->header = NULL;
2751     }
2752
2753   /* In the case basic blocks are not adjacent, move them around.  */
2754   if (NEXT_INSN (BB_END (a)) != BB_HEAD (b))
2755     {
2756       rtx first = unlink_insn_chain (BB_HEAD (b), BB_END (b));
2757
2758       emit_insn_after_noloc (first, BB_END (a), a);
2759       /* Skip possible DELETED_LABEL insn.  */
2760       if (!NOTE_INSN_BASIC_BLOCK_P (first))
2761         first = NEXT_INSN (first);
2762       gcc_assert (NOTE_INSN_BASIC_BLOCK_P (first));
2763       BB_HEAD (b) = NULL;
2764
2765       /* emit_insn_after_noloc doesn't call df_insn_change_bb.
2766          We need to explicitly call. */
2767       update_bb_for_insn_chain (NEXT_INSN (first),
2768                                 BB_END (b),
2769                                 a);
2770
2771       delete_insn (first);
2772     }
2773   /* Otherwise just re-associate the instructions.  */
2774   else
2775     {
2776       rtx insn;
2777
2778       update_bb_for_insn_chain (BB_HEAD (b), BB_END (b), a);
2779
2780       insn = BB_HEAD (b);
2781       /* Skip possible DELETED_LABEL insn.  */
2782       if (!NOTE_INSN_BASIC_BLOCK_P (insn))
2783         insn = NEXT_INSN (insn);
2784       gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
2785       BB_HEAD (b) = NULL;
2786       BB_END (a) = BB_END (b);
2787       delete_insn (insn);
2788     }
2789
2790   df_bb_delete (b->index);
2791
2792   /* Possible tablejumps and barriers should appear after the block.  */
2793   if (b->il.rtl->footer)
2794     {
2795       if (!a->il.rtl->footer)
2796         a->il.rtl->footer = b->il.rtl->footer;
2797       else
2798         {
2799           rtx last = a->il.rtl->footer;
2800
2801           while (NEXT_INSN (last))
2802             last = NEXT_INSN (last);
2803           NEXT_INSN (last) = b->il.rtl->footer;
2804           PREV_INSN (b->il.rtl->footer) = last;
2805         }
2806       b->il.rtl->footer = NULL;
2807     }
2808
2809   if (dump_file)
2810     fprintf (dump_file, "Merged blocks %d and %d.\n",
2811              a->index, b->index);
2812 }
2813
2814 /* Split edge E.  */
2815
2816 static basic_block
2817 cfg_layout_split_edge (edge e)
2818 {
2819   basic_block new_bb =
2820     create_basic_block (e->src != ENTRY_BLOCK_PTR
2821                         ? NEXT_INSN (BB_END (e->src)) : get_insns (),
2822                         NULL_RTX, e->src);
2823
2824   if (e->dest == EXIT_BLOCK_PTR)
2825     BB_COPY_PARTITION (new_bb, e->src);
2826   else
2827     BB_COPY_PARTITION (new_bb, e->dest);
2828   make_edge (new_bb, e->dest, EDGE_FALLTHRU);
2829   redirect_edge_and_branch_force (e, new_bb);
2830
2831   return new_bb;
2832 }
2833
2834 /* Do postprocessing after making a forwarder block joined by edge FALLTHRU.  */
2835
2836 static void
2837 rtl_make_forwarder_block (edge fallthru ATTRIBUTE_UNUSED)
2838 {
2839 }
2840
2841 /* Return 1 if BB ends with a call, possibly followed by some
2842    instructions that must stay with the call, 0 otherwise.  */
2843
2844 static bool
2845 rtl_block_ends_with_call_p (basic_block bb)
2846 {
2847   rtx insn = BB_END (bb);
2848
2849   while (!CALL_P (insn)
2850          && insn != BB_HEAD (bb)
2851          && (keep_with_call_p (insn)
2852              || NOTE_P (insn)
2853              || DEBUG_INSN_P (insn)))
2854     insn = PREV_INSN (insn);
2855   return (CALL_P (insn));
2856 }
2857
2858 /* Return 1 if BB ends with a conditional branch, 0 otherwise.  */
2859
2860 static bool
2861 rtl_block_ends_with_condjump_p (const_basic_block bb)
2862 {
2863   return any_condjump_p (BB_END (bb));
2864 }
2865
2866 /* Return true if we need to add fake edge to exit.
2867    Helper function for rtl_flow_call_edges_add.  */
2868
2869 static bool
2870 need_fake_edge_p (const_rtx insn)
2871 {
2872   if (!INSN_P (insn))
2873     return false;
2874
2875   if ((CALL_P (insn)
2876        && !SIBLING_CALL_P (insn)
2877        && !find_reg_note (insn, REG_NORETURN, NULL)
2878        && !(RTL_CONST_OR_PURE_CALL_P (insn))))
2879     return true;
2880
2881   return ((GET_CODE (PATTERN (insn)) == ASM_OPERANDS
2882            && MEM_VOLATILE_P (PATTERN (insn)))
2883           || (GET_CODE (PATTERN (insn)) == PARALLEL
2884               && asm_noperands (insn) != -1
2885               && MEM_VOLATILE_P (XVECEXP (PATTERN (insn), 0, 0)))
2886           || GET_CODE (PATTERN (insn)) == ASM_INPUT);
2887 }
2888
2889 /* Add fake edges to the function exit for any non constant and non noreturn
2890    calls, volatile inline assembly in the bitmap of blocks specified by
2891    BLOCKS or to the whole CFG if BLOCKS is zero.  Return the number of blocks
2892    that were split.
2893
2894    The goal is to expose cases in which entering a basic block does not imply
2895    that all subsequent instructions must be executed.  */
2896
2897 static int
2898 rtl_flow_call_edges_add (sbitmap blocks)
2899 {
2900   int i;
2901   int blocks_split = 0;
2902   int last_bb = last_basic_block;
2903   bool check_last_block = false;
2904
2905   if (n_basic_blocks == NUM_FIXED_BLOCKS)
2906     return 0;
2907
2908   if (! blocks)
2909     check_last_block = true;
2910   else
2911     check_last_block = TEST_BIT (blocks, EXIT_BLOCK_PTR->prev_bb->index);
2912
2913   /* In the last basic block, before epilogue generation, there will be
2914      a fallthru edge to EXIT.  Special care is required if the last insn
2915      of the last basic block is a call because make_edge folds duplicate
2916      edges, which would result in the fallthru edge also being marked
2917      fake, which would result in the fallthru edge being removed by
2918      remove_fake_edges, which would result in an invalid CFG.
2919
2920      Moreover, we can't elide the outgoing fake edge, since the block
2921      profiler needs to take this into account in order to solve the minimal
2922      spanning tree in the case that the call doesn't return.
2923
2924      Handle this by adding a dummy instruction in a new last basic block.  */
2925   if (check_last_block)
2926     {
2927       basic_block bb = EXIT_BLOCK_PTR->prev_bb;
2928       rtx insn = BB_END (bb);
2929
2930       /* Back up past insns that must be kept in the same block as a call.  */
2931       while (insn != BB_HEAD (bb)
2932              && keep_with_call_p (insn))
2933         insn = PREV_INSN (insn);
2934
2935       if (need_fake_edge_p (insn))
2936         {
2937           edge e;
2938
2939           e = find_edge (bb, EXIT_BLOCK_PTR);
2940           if (e)
2941             {
2942               insert_insn_on_edge (gen_use (const0_rtx), e);
2943               commit_edge_insertions ();
2944             }
2945         }
2946     }
2947
2948   /* Now add fake edges to the function exit for any non constant
2949      calls since there is no way that we can determine if they will
2950      return or not...  */
2951
2952   for (i = NUM_FIXED_BLOCKS; i < last_bb; i++)
2953     {
2954       basic_block bb = BASIC_BLOCK (i);
2955       rtx insn;
2956       rtx prev_insn;
2957
2958       if (!bb)
2959         continue;
2960
2961       if (blocks && !TEST_BIT (blocks, i))
2962         continue;
2963
2964       for (insn = BB_END (bb); ; insn = prev_insn)
2965         {
2966           prev_insn = PREV_INSN (insn);
2967           if (need_fake_edge_p (insn))
2968             {
2969               edge e;
2970               rtx split_at_insn = insn;
2971
2972               /* Don't split the block between a call and an insn that should
2973                  remain in the same block as the call.  */
2974               if (CALL_P (insn))
2975                 while (split_at_insn != BB_END (bb)
2976                        && keep_with_call_p (NEXT_INSN (split_at_insn)))
2977                   split_at_insn = NEXT_INSN (split_at_insn);
2978
2979               /* The handling above of the final block before the epilogue
2980                  should be enough to verify that there is no edge to the exit
2981                  block in CFG already.  Calling make_edge in such case would
2982                  cause us to mark that edge as fake and remove it later.  */
2983
2984 #ifdef ENABLE_CHECKING
2985               if (split_at_insn == BB_END (bb))
2986                 {
2987                   e = find_edge (bb, EXIT_BLOCK_PTR);
2988                   gcc_assert (e == NULL);
2989                 }
2990 #endif
2991
2992               /* Note that the following may create a new basic block
2993                  and renumber the existing basic blocks.  */
2994               if (split_at_insn != BB_END (bb))
2995                 {
2996                   e = split_block (bb, split_at_insn);
2997                   if (e)
2998                     blocks_split++;
2999                 }
3000
3001               make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
3002             }
3003
3004           if (insn == BB_HEAD (bb))
3005             break;
3006         }
3007     }
3008
3009   if (blocks_split)
3010     verify_flow_info ();
3011
3012   return blocks_split;
3013 }
3014
3015 /* Add COMP_RTX as a condition at end of COND_BB.  FIRST_HEAD is
3016    the conditional branch target, SECOND_HEAD should be the fall-thru
3017    there is no need to handle this here the loop versioning code handles
3018    this.  the reason for SECON_HEAD is that it is needed for condition
3019    in trees, and this should be of the same type since it is a hook.  */
3020 static void
3021 rtl_lv_add_condition_to_bb (basic_block first_head ,
3022                             basic_block second_head ATTRIBUTE_UNUSED,
3023                             basic_block cond_bb, void *comp_rtx)
3024 {
3025   rtx label, seq, jump;
3026   rtx op0 = XEXP ((rtx)comp_rtx, 0);
3027   rtx op1 = XEXP ((rtx)comp_rtx, 1);
3028   enum rtx_code comp = GET_CODE ((rtx)comp_rtx);
3029   enum machine_mode mode;
3030
3031
3032   label = block_label (first_head);
3033   mode = GET_MODE (op0);
3034   if (mode == VOIDmode)
3035     mode = GET_MODE (op1);
3036
3037   start_sequence ();
3038   op0 = force_operand (op0, NULL_RTX);
3039   op1 = force_operand (op1, NULL_RTX);
3040   do_compare_rtx_and_jump (op0, op1, comp, 0,
3041                            mode, NULL_RTX, NULL_RTX, label);
3042   jump = get_last_insn ();
3043   JUMP_LABEL (jump) = label;
3044   LABEL_NUSES (label)++;
3045   seq = get_insns ();
3046   end_sequence ();
3047
3048   /* Add the new cond , in the new head.  */
3049   emit_insn_after(seq, BB_END(cond_bb));
3050 }
3051
3052
3053 /* Given a block B with unconditional branch at its end, get the
3054    store the return the branch edge and the fall-thru edge in
3055    BRANCH_EDGE and FALLTHRU_EDGE respectively.  */
3056 static void
3057 rtl_extract_cond_bb_edges (basic_block b, edge *branch_edge,
3058                            edge *fallthru_edge)
3059 {
3060   edge e = EDGE_SUCC (b, 0);
3061
3062   if (e->flags & EDGE_FALLTHRU)
3063     {
3064       *fallthru_edge = e;
3065       *branch_edge = EDGE_SUCC (b, 1);
3066     }
3067   else
3068     {
3069       *branch_edge = e;
3070       *fallthru_edge = EDGE_SUCC (b, 1);
3071     }
3072 }
3073
3074 void
3075 init_rtl_bb_info (basic_block bb)
3076 {
3077   gcc_assert (!bb->il.rtl);
3078   bb->il.rtl = GGC_CNEW (struct rtl_bb_info);
3079 }
3080
3081
3082 /* Add EXPR to the end of basic block BB.  */
3083
3084 rtx
3085 insert_insn_end_bb_new (rtx pat, basic_block bb)
3086 {
3087   rtx insn = BB_END (bb);
3088   rtx new_insn;
3089   rtx pat_end = pat;
3090
3091   while (NEXT_INSN (pat_end) != NULL_RTX)
3092     pat_end = NEXT_INSN (pat_end);
3093
3094   /* If the last insn is a jump, insert EXPR in front [taking care to
3095      handle cc0, etc. properly].  Similarly we need to care trapping
3096      instructions in presence of non-call exceptions.  */
3097
3098   if (JUMP_P (insn)
3099       || (NONJUMP_INSN_P (insn)
3100           && (!single_succ_p (bb)
3101               || single_succ_edge (bb)->flags & EDGE_ABNORMAL)))
3102     {
3103 #ifdef HAVE_cc0
3104       rtx note;
3105 #endif
3106       /* If this is a jump table, then we can't insert stuff here.  Since
3107          we know the previous real insn must be the tablejump, we insert
3108          the new instruction just before the tablejump.  */
3109       if (GET_CODE (PATTERN (insn)) == ADDR_VEC
3110           || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
3111         insn = prev_real_insn (insn);
3112
3113 #ifdef HAVE_cc0
3114       /* FIXME: 'twould be nice to call prev_cc0_setter here but it aborts
3115          if cc0 isn't set.  */
3116       note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
3117       if (note)
3118         insn = XEXP (note, 0);
3119       else
3120         {
3121           rtx maybe_cc0_setter = prev_nonnote_insn (insn);
3122           if (maybe_cc0_setter
3123               && INSN_P (maybe_cc0_setter)
3124               && sets_cc0_p (PATTERN (maybe_cc0_setter)))
3125             insn = maybe_cc0_setter;
3126         }
3127 #endif
3128       /* FIXME: What if something in cc0/jump uses value set in new
3129          insn?  */
3130       new_insn = emit_insn_before_noloc (pat, insn, bb);
3131     }
3132
3133   /* Likewise if the last insn is a call, as will happen in the presence
3134      of exception handling.  */
3135   else if (CALL_P (insn)
3136            && (!single_succ_p (bb)
3137                || single_succ_edge (bb)->flags & EDGE_ABNORMAL))
3138     {
3139       /* Keeping in mind SMALL_REGISTER_CLASSES and parameters in registers,
3140          we search backward and place the instructions before the first
3141          parameter is loaded.  Do this for everyone for consistency and a
3142          presumption that we'll get better code elsewhere as well.  */
3143
3144       /* Since different machines initialize their parameter registers
3145          in different orders, assume nothing.  Collect the set of all
3146          parameter registers.  */
3147       insn = find_first_parameter_load (insn, BB_HEAD (bb));
3148
3149       /* If we found all the parameter loads, then we want to insert
3150          before the first parameter load.
3151
3152          If we did not find all the parameter loads, then we might have
3153          stopped on the head of the block, which could be a CODE_LABEL.
3154          If we inserted before the CODE_LABEL, then we would be putting
3155          the insn in the wrong basic block.  In that case, put the insn
3156          after the CODE_LABEL.  Also, respect NOTE_INSN_BASIC_BLOCK.  */
3157       while (LABEL_P (insn)
3158              || NOTE_INSN_BASIC_BLOCK_P (insn))
3159         insn = NEXT_INSN (insn);
3160
3161       new_insn = emit_insn_before_noloc (pat, insn, bb);
3162     }
3163   else
3164     new_insn = emit_insn_after_noloc (pat, insn, bb);
3165
3166   return new_insn;
3167 }
3168
3169 /* Returns true if it is possible to remove edge E by redirecting
3170    it to the destination of the other edge from E->src.  */
3171
3172 static bool
3173 rtl_can_remove_branch_p (const_edge e)
3174 {
3175   const_basic_block src = e->src;
3176   const_basic_block target = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest;
3177   const_rtx insn = BB_END (src), set;
3178
3179   /* The conditions are taken from try_redirect_by_replacing_jump.  */
3180   if (target == EXIT_BLOCK_PTR)
3181     return false;
3182
3183   if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
3184     return false;
3185
3186   if (find_reg_note (insn, REG_CROSSING_JUMP, NULL_RTX)
3187       || BB_PARTITION (src) != BB_PARTITION (target))
3188     return false;
3189
3190   if (!onlyjump_p (insn)
3191       || tablejump_p (insn, NULL, NULL))
3192     return false;
3193
3194   set = single_set (insn);
3195   if (!set || side_effects_p (set))
3196     return false;
3197
3198   return true;
3199 }
3200
3201 /* Implementation of CFG manipulation for linearized RTL.  */
3202 struct cfg_hooks rtl_cfg_hooks = {
3203   "rtl",
3204   rtl_verify_flow_info,
3205   rtl_dump_bb,
3206   rtl_create_basic_block,
3207   rtl_redirect_edge_and_branch,
3208   rtl_redirect_edge_and_branch_force,
3209   rtl_can_remove_branch_p,
3210   rtl_delete_block,
3211   rtl_split_block,
3212   rtl_move_block_after,
3213   rtl_can_merge_blocks,  /* can_merge_blocks_p */
3214   rtl_merge_blocks,
3215   rtl_predict_edge,
3216   rtl_predicted_by_p,
3217   NULL, /* can_duplicate_block_p */
3218   NULL, /* duplicate_block */
3219   rtl_split_edge,
3220   rtl_make_forwarder_block,
3221   rtl_tidy_fallthru_edge,
3222   rtl_block_ends_with_call_p,
3223   rtl_block_ends_with_condjump_p,
3224   rtl_flow_call_edges_add,
3225   NULL, /* execute_on_growing_pred */
3226   NULL, /* execute_on_shrinking_pred */
3227   NULL, /* duplicate loop for trees */
3228   NULL, /* lv_add_condition_to_bb */
3229   NULL, /* lv_adjust_loop_header_phi*/
3230   NULL, /* extract_cond_bb_edges */
3231   NULL          /* flush_pending_stmts */
3232 };
3233
3234 /* Implementation of CFG manipulation for cfg layout RTL, where
3235    basic block connected via fallthru edges does not have to be adjacent.
3236    This representation will hopefully become the default one in future
3237    version of the compiler.  */
3238
3239 /* We do not want to declare these functions in a header file, since they
3240    should only be used through the cfghooks interface, and we do not want to
3241    move them here since it would require also moving quite a lot of related
3242    code.  They are in cfglayout.c.  */
3243 extern bool cfg_layout_can_duplicate_bb_p (const_basic_block);
3244 extern basic_block cfg_layout_duplicate_bb (basic_block);
3245
3246 struct cfg_hooks cfg_layout_rtl_cfg_hooks = {
3247   "cfglayout mode",
3248   rtl_verify_flow_info_1,
3249   rtl_dump_bb,
3250   cfg_layout_create_basic_block,
3251   cfg_layout_redirect_edge_and_branch,
3252   cfg_layout_redirect_edge_and_branch_force,
3253   rtl_can_remove_branch_p,
3254   cfg_layout_delete_block,
3255   cfg_layout_split_block,
3256   rtl_move_block_after,
3257   cfg_layout_can_merge_blocks_p,
3258   cfg_layout_merge_blocks,
3259   rtl_predict_edge,
3260   rtl_predicted_by_p,
3261   cfg_layout_can_duplicate_bb_p,
3262   cfg_layout_duplicate_bb,
3263   cfg_layout_split_edge,
3264   rtl_make_forwarder_block,
3265   NULL,
3266   rtl_block_ends_with_call_p,
3267   rtl_block_ends_with_condjump_p,
3268   rtl_flow_call_edges_add,
3269   NULL, /* execute_on_growing_pred */
3270   NULL, /* execute_on_shrinking_pred */
3271   duplicate_loop_to_header_edge, /* duplicate loop for trees */
3272   rtl_lv_add_condition_to_bb, /* lv_add_condition_to_bb */
3273   NULL, /* lv_adjust_loop_header_phi*/
3274   rtl_extract_cond_bb_edges, /* extract_cond_bb_edges */
3275   NULL          /* flush_pending_stmts */
3276 };