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