OSDN Git Service

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