OSDN Git Service

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