]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cfgrtl.c
Remove trailing white spaces.
[thirdparty/gcc.git] / gcc / cfgrtl.c
CommitLineData
ca6c03ca
JH
1/* Control flow graph manipulation code for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
7072a650 3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
6fb5fa3c 4 Free Software Foundation, Inc.
ca6c03ca
JH
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
9dcd6f09 10Software Foundation; either version 3, or (at your option) any later
ca6c03ca
JH
11version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License
9dcd6f09
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
ca6c03ca 21
5f0d2358
RK
22/* This file contains low level functions to manipulate the CFG and analyze it
23 that are aware of the RTL intermediate language.
ca6c03ca
JH
24
25 Available functionality:
bc35512f 26 - Basic CFG/RTL manipulation API documented in cfghooks.h
5f0d2358 27 - CFG-aware instruction chain manipulation
ca6c03ca 28 delete_insn, delete_insn_chain
bc35512f
JH
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:
5f0d2358 35 - Infrastructure to determine quickly basic block for insn
ca6c03ca 36 compute_bb_for_insn, update_bb_for_insn, set_block_for_insn,
5f0d2358 37 - Edge redirection with updating and optimizing of insn chain
bc35512f 38 block_label, tidy_fallthru_edge, force_nonfallthru */
ca6c03ca
JH
39\f
40#include "config.h"
41#include "system.h"
4977bab6
ZW
42#include "coretypes.h"
43#include "tm.h"
ca6c03ca
JH
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"
2a34bece 56#include "insn-attr.h"
0a2ed1f1 57#include "insn-config.h"
9ee634e3 58#include "cfglayout.h"
ff25ef99 59#include "expr.h"
9fb32434 60#include "target.h"
1cb7dfc3 61#include "cfgloop.h"
5e2d947c 62#include "ggc.h"
ef330312 63#include "tree-pass.h"
6fb5fa3c 64#include "df.h"
ca6c03ca 65
9678086d
KG
66static int can_delete_note_p (const_rtx);
67static int can_delete_label_p (const_rtx);
d329e058 68static basic_block rtl_split_edge (edge);
f470c378 69static bool rtl_move_block_after (basic_block, basic_block);
d329e058 70static int rtl_verify_flow_info (void);
f470c378 71static basic_block cfg_layout_split_block (basic_block, void *);
6de9cd9a 72static edge cfg_layout_redirect_edge_and_branch (edge, basic_block);
d329e058
AJ
73static basic_block cfg_layout_redirect_edge_and_branch_force (edge, basic_block);
74static void cfg_layout_delete_block (basic_block);
75static void rtl_delete_block (basic_block);
76static basic_block rtl_redirect_edge_and_branch_force (edge, basic_block);
6de9cd9a 77static edge rtl_redirect_edge_and_branch (edge, basic_block);
f470c378 78static basic_block rtl_split_block (basic_block, void *);
726a989a 79static void rtl_dump_bb (basic_block, FILE *, int, int);
d329e058 80static int rtl_verify_flow_info_1 (void);
f470c378 81static void rtl_make_forwarder_block (edge);
ca6c03ca
JH
82\f
83/* Return true if NOTE is not one of the ones that must be kept paired,
5f0d2358 84 so that we may simply delete it. */
ca6c03ca
JH
85
86static int
9678086d 87can_delete_note_p (const_rtx note)
ca6c03ca 88{
cd9c1ca8
RH
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 }
ca6c03ca
JH
99}
100
101/* True if a given label can be deleted. */
102
103static int
9678086d 104can_delete_label_p (const_rtx label)
ca6c03ca 105{
5f0d2358
RK
106 return (!LABEL_PRESERVE_P (label)
107 /* User declared labels must be preserved. */
108 && LABEL_NAME (label) == 0
610d2478 109 && !in_expr_list_p (forced_labels, label));
ca6c03ca
JH
110}
111
112/* Delete INSN by patching it out. Return the next insn. */
113
114rtx
d329e058 115delete_insn (rtx insn)
ca6c03ca
JH
116{
117 rtx next = NEXT_INSN (insn);
118 rtx note;
119 bool really_delete = true;
120
4b4bf941 121 if (LABEL_P (insn))
ca6c03ca
JH
122 {
123 /* Some labels can't be directly removed from the INSN chain, as they
c22cacf3
MS
124 might be references via variables, constant pool etc.
125 Convert them to the special NOTE_INSN_DELETED_LABEL note. */
ca6c03ca
JH
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);
a38e7aa5 132 NOTE_KIND (insn) = NOTE_INSN_DELETED_LABEL;
6773e15f 133 NOTE_DELETED_LABEL_NAME (insn) = name;
ca6c03ca 134 }
5f0d2358 135
ca6c03ca
JH
136 remove_node_from_expr_list (insn, &nonlocal_goto_handler_labels);
137 }
138
139 if (really_delete)
140 {
cda94cbb 141 /* If this insn has already been deleted, something is very wrong. */
341c100f 142 gcc_assert (!INSN_DELETED_P (insn));
ca6c03ca
JH
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. */
cf7c4aa6 149 if (JUMP_P (insn))
9295a326 150 {
cf7c4aa6
HPN
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
4b4bf941 158 && LABEL_P (XEXP (note, 0)))
9295a326
JZ
159 {
160 LABEL_NUSES (XEXP (note, 0))--;
161 remove_note (insn, note);
162 }
163 }
ca6c03ca 164
cf7c4aa6
HPN
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
481683e1 173 if (JUMP_TABLE_DATA_P (insn))
ca6c03ca
JH
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++)
a124fcda
RH
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. */
4b4bf941 187 if (!NOTE_P (label))
a124fcda
RH
188 LABEL_NUSES (label)--;
189 }
ca6c03ca
JH
190 }
191
192 return next;
193}
194
3dec4024 195/* Like delete_insn but also purge dead edges from BB. */
9ed7b221 196
3dec4024 197rtx
d329e058 198delete_insn_and_edges (rtx insn)
3dec4024
JH
199{
200 rtx x;
201 bool purge = false;
202
ba4f7968 203 if (INSN_P (insn)
3dec4024 204 && BLOCK_FOR_INSN (insn)
a813c111 205 && BB_END (BLOCK_FOR_INSN (insn)) == insn)
3dec4024
JH
206 purge = true;
207 x = delete_insn (insn);
208 if (purge)
209 purge_dead_edges (BLOCK_FOR_INSN (insn));
210 return x;
211}
212
ca6c03ca 213/* Unlink a chain of insns between START and FINISH, leaving notes
a7b87f73
ZD
214 that must be paired. If CLEAR_BB is true, we set bb field for
215 insns that cannot be removed to NULL. */
ca6c03ca
JH
216
217void
a7b87f73 218delete_insn_chain (rtx start, rtx finish, bool clear_bb)
ca6c03ca 219{
ca6c03ca
JH
220 rtx next;
221
5f0d2358
RK
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. */
ca6c03ca
JH
225 while (1)
226 {
227 next = NEXT_INSN (start);
4b4bf941 228 if (NOTE_P (start) && !can_delete_note_p (start))
ca6c03ca
JH
229 ;
230 else
231 next = delete_insn (start);
232
a7b87f73
ZD
233 if (clear_bb && !INSN_DELETED_P (start))
234 set_block_for_insn (start, NULL);
235
ca6c03ca
JH
236 if (start == finish)
237 break;
238 start = next;
239 }
240}
241\f
5f0d2358
RK
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
918ed612
ZD
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. */
ca6c03ca
JH
249
250basic_block
d329e058 251create_basic_block_structure (rtx head, rtx end, rtx bb_note, basic_block after)
ca6c03ca
JH
252{
253 basic_block bb;
254
255 if (bb_note
ca6c03ca
JH
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
4b4bf941 263 if (LABEL_P (head))
ca6c03ca
JH
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)
ba4f7968 272 reorder_insns_nobb (bb_note, bb_note, after);
ca6c03ca
JH
273 }
274 else
275 {
276 /* Otherwise we must create a note and a basic block structure. */
277
278 bb = alloc_block ();
279
5e2d947c 280 init_rtl_bb_info (bb);
ca6c03ca 281 if (!head && !end)
5f0d2358
RK
282 head = end = bb_note
283 = emit_note_after (NOTE_INSN_BASIC_BLOCK, get_last_insn ());
4b4bf941 284 else if (LABEL_P (head) && end)
ca6c03ca
JH
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 }
5f0d2358 297
ca6c03ca
JH
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
a813c111
SB
305 BB_HEAD (bb) = head;
306 BB_END (bb) = end;
852c6ec7 307 bb->index = last_basic_block++;
5e2d947c 308 bb->flags = BB_NEW | BB_RTL;
918ed612 309 link_block (bb, after);
68f9b844 310 SET_BASIC_BLOCK (bb->index, bb);
6fb5fa3c 311 df_bb_refs_record (bb->index, false);
ba4f7968 312 update_bb_for_insn (bb);
076c7ab8 313 BB_SET_PARTITION (bb, BB_UNPARTITIONED);
ca6c03ca
JH
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
5f0d2358 322/* Create new basic block consisting of instructions in between HEAD and END
918ed612 323 and place it to the BB chain after block AFTER. END can be NULL in to
5f0d2358
RK
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. */
ca6c03ca 326
bc35512f
JH
327static basic_block
328rtl_create_basic_block (void *headp, void *endp, basic_block after)
ca6c03ca 329{
ae50c0cb 330 rtx head = (rtx) headp, end = (rtx) endp;
ca6c03ca 331 basic_block bb;
0b17ab2f 332
7eca0767 333 /* Grow the basic block array if needed. */
68f9b844 334 if ((size_t) last_basic_block >= VEC_length (basic_block, basic_block_info))
7eca0767
JH
335 {
336 size_t new_size = last_basic_block + (last_basic_block + 3) / 4;
a590ac65 337 VEC_safe_grow_cleared (basic_block, gc, basic_block_info, new_size);
7eca0767 338 }
0b17ab2f 339
bf77398c 340 n_basic_blocks++;
ca6c03ca 341
852c6ec7 342 bb = create_basic_block_structure (head, end, NULL, after);
ca6c03ca
JH
343 bb->aux = NULL;
344 return bb;
345}
bc35512f
JH
346
347static basic_block
348cfg_layout_create_basic_block (void *head, void *end, basic_block after)
349{
350 basic_block newbb = rtl_create_basic_block (head, end, after);
351
bc35512f
JH
352 return newbb;
353}
ca6c03ca
JH
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
f0fda11c 363static void
d329e058 364rtl_delete_block (basic_block b)
ca6c03ca 365{
96370780 366 rtx insn, end;
ca6c03ca
JH
367
368 /* If the head of this block is a CODE_LABEL, then it might be the
f39e46ba
SB
369 label for an exception handler which can't be reached. We need
370 to remove the label from the exception_handler_label list. */
a813c111 371 insn = BB_HEAD (b);
ca6c03ca 372
96370780 373 end = get_last_bb_insn (b);
ca6c03ca
JH
374
375 /* Selectively delete the entire chain. */
a813c111 376 BB_HEAD (b) = NULL;
a7b87f73
ZD
377 delete_insn_chain (insn, end, true);
378
6fb5fa3c
DB
379
380 if (dump_file)
381 fprintf (dump_file, "deleting block %d\n", b->index);
382 df_bb_delete (b->index);
ca6c03ca
JH
383}
384\f
852c6ec7 385/* Records the basic block struct in BLOCK_FOR_INSN for every insn. */
ca6c03ca
JH
386
387void
d329e058 388compute_bb_for_insn (void)
ca6c03ca 389{
e0082a72 390 basic_block bb;
ca6c03ca 391
e0082a72 392 FOR_EACH_BB (bb)
ca6c03ca 393 {
a813c111 394 rtx end = BB_END (bb);
5f0d2358 395 rtx insn;
ca6c03ca 396
a813c111 397 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
ca6c03ca 398 {
ba4f7968 399 BLOCK_FOR_INSN (insn) = bb;
ca6c03ca
JH
400 if (insn == end)
401 break;
ca6c03ca
JH
402 }
403 }
404}
405
406/* Release the basic_block_for_insn array. */
407
c2924966 408unsigned int
d329e058 409free_bb_for_insn (void)
ca6c03ca 410{
ba4f7968
JH
411 rtx insn;
412 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4b4bf941 413 if (!BARRIER_P (insn))
ba4f7968 414 BLOCK_FOR_INSN (insn) = NULL;
c2924966 415 return 0;
ca6c03ca
JH
416}
417
2a34bece
EB
418static unsigned int
419rest_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
8ddbbcae 432struct rtl_opt_pass pass_free_cfg =
ef330312 433{
8ddbbcae
JH
434 {
435 RTL_PASS,
e0a42b0f 436 "*free_cfg", /* name */
ef330312 437 NULL, /* gate */
2a34bece 438 rest_of_pass_free_cfg, /* execute */
ef330312
PB
439 NULL, /* sub */
440 NULL, /* next */
441 0, /* static_pass_number */
7072a650 442 TV_NONE, /* tv_id */
ef330312
PB
443 0, /* properties_required */
444 0, /* properties_provided */
445 PROP_cfg, /* properties_destroyed */
446 0, /* todo_flags_start */
447 0, /* todo_flags_finish */
8ddbbcae 448 }
ef330312
PB
449};
450
91278841
AP
451/* Return RTX to emit after when we want to emit code on the entry of function. */
452rtx
453entry_of_function (void)
454{
c22cacf3 455 return (n_basic_blocks > NUM_FIXED_BLOCKS ?
24bd1a0b 456 BB_HEAD (ENTRY_BLOCK_PTR->next_bb) : get_insns ());
91278841
AP
457}
458
11b904a1
BS
459/* Emit INSN at the entry point of the function, ensuring that it is only
460 executed once per function. */
461void
462emit_insn_at_entry (rtx insn)
463{
464 edge_iterator ei = ei_start (ENTRY_BLOCK_PTR->succs);
465 edge e = ei_safe_edge (ei);
5419bc7f 466 gcc_assert (e->flags & EDGE_FALLTHRU);
11b904a1
BS
467
468 insert_insn_on_edge (insn, e);
469 commit_edge_insertions ();
470}
471
6c3d0e31 472/* Update BLOCK_FOR_INSN of insns between BEGIN and END
b8698a0f 473 (or BARRIER if found) and notify df of the bb change.
6c3d0e31
SP
474 The insn chain range is inclusive
475 (i.e. both BEGIN and END will be updated. */
ca6c03ca 476
6c3d0e31
SP
477static void
478update_bb_for_insn_chain (rtx begin, rtx end, basic_block bb)
ca6c03ca
JH
479{
480 rtx insn;
481
6c3d0e31
SP
482 end = NEXT_INSN (end);
483 for (insn = begin; insn != end; insn = NEXT_INSN (insn))
63642d5a
AO
484 if (!BARRIER_P (insn))
485 df_insn_change_bb (insn, bb);
ca6c03ca 486}
6c3d0e31
SP
487
488/* Update BLOCK_FOR_INSN of insns in BB to BB,
489 and notify df of the change. */
490
491void
492update_bb_for_insn (basic_block bb)
493{
494 update_bb_for_insn_chain (BB_HEAD (bb), BB_END (bb), bb);
495}
496
ca6c03ca 497\f
6fb5fa3c
DB
498/* Return the INSN immediately following the NOTE_INSN_BASIC_BLOCK
499 note associated with the BLOCK. */
500
501static rtx
502first_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
f470c378
ZD
518/* Creates a new basic block just after basic block B by splitting
519 everything after specified instruction I. */
ca6c03ca 520
f470c378 521static basic_block
d329e058 522rtl_split_block (basic_block bb, void *insnp)
ca6c03ca
JH
523{
524 basic_block new_bb;
ae50c0cb 525 rtx insn = (rtx) insnp;
f470c378 526 edge e;
628f6a4e 527 edge_iterator ei;
ca6c03ca 528
f470c378
ZD
529 if (!insn)
530 {
531 insn = first_insn_after_basic_block_note (bb);
532
533 if (insn)
b5b8b0ac
AO
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 }
f470c378
ZD
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);
ca6c03ca
JH
563
564 /* Create the new basic block. */
a813c111 565 new_bb = create_basic_block (NEXT_INSN (insn), BB_END (bb), bb);
076c7ab8 566 BB_COPY_PARTITION (new_bb, bb);
a813c111 567 BB_END (bb) = insn;
ca6c03ca
JH
568
569 /* Redirect the outgoing edges. */
628f6a4e
BE
570 new_bb->succs = bb->succs;
571 bb->succs = NULL;
572 FOR_EACH_EDGE (e, ei, new_bb->succs)
ca6c03ca
JH
573 e->src = new_bb;
574
6fb5fa3c
DB
575 /* The new block starts off being dirty. */
576 df_set_bb_dirty (bb);
f470c378 577 return new_bb;
bc35512f
JH
578}
579
ca6c03ca 580/* Blocks A and B are to be merged into a single block A. The insns
bc35512f 581 are already contiguous. */
ca6c03ca 582
bc35512f
JH
583static void
584rtl_merge_blocks (basic_block a, basic_block b)
ca6c03ca 585{
a813c111 586 rtx b_head = BB_HEAD (b), b_end = BB_END (b), a_end = BB_END (a);
ca6c03ca 587 rtx del_first = NULL_RTX, del_last = NULL_RTX;
b5b8b0ac 588 rtx b_debug_start = b_end, b_debug_end = b_end;
ca6c03ca
JH
589 int b_empty = 0;
590
6fb5fa3c
DB
591 if (dump_file)
592 fprintf (dump_file, "merging block %d into block %d\n", b->index, a->index);
593
b5b8b0ac
AO
594 while (DEBUG_INSN_P (b_end))
595 b_end = PREV_INSN (b_debug_start = b_end);
596
ca6c03ca 597 /* If there was a CODE_LABEL beginning B, delete it. */
4b4bf941 598 if (LABEL_P (b_head))
ca6c03ca
JH
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;
5f0d2358 604
ca6c03ca
JH
605 del_first = del_last = b_head;
606 b_head = NEXT_INSN (b_head);
607 }
608
5f0d2358
RK
609 /* Delete the basic block note and handle blocks containing just that
610 note. */
ca6c03ca
JH
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;
5f0d2358 617
ca6c03ca
JH
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. */
4b4bf941 623 if (JUMP_P (a_end))
ca6c03ca
JH
624 {
625 rtx prev;
626
627 for (prev = PREV_INSN (a_end); ; prev = PREV_INSN (prev))
4b4bf941 628 if (!NOTE_P (prev)
a38e7aa5 629 || NOTE_INSN_BASIC_BLOCK_P (prev)
a813c111 630 || prev == BB_HEAD (a))
ca6c03ca
JH
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;
5f0d2358 641
ca6c03ca
JH
642 prev = prev_nonnote_insn (prev);
643 if (!prev)
a813c111 644 prev = BB_HEAD (a);
ca6c03ca
JH
645 del_first = tmp;
646 }
647#endif
648
649 a_end = PREV_INSN (del_first);
650 }
4b4bf941 651 else if (BARRIER_P (NEXT_INSN (a_end)))
ca6c03ca
JH
652 del_first = NEXT_INSN (a_end);
653
ca6c03ca
JH
654 /* Delete everything marked above as well as crap that might be
655 hanging out between the two blocks. */
f470c378 656 BB_HEAD (b) = NULL;
a7b87f73 657 delete_insn_chain (del_first, del_last, true);
ca6c03ca
JH
658
659 /* Reassociate the insns of B with A. */
660 if (!b_empty)
661 {
b5b8b0ac 662 update_bb_for_insn_chain (a_end, b_debug_end, a);
5f0d2358 663
b5b8b0ac
AO
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;
ca6c03ca 677 }
5f0d2358 678
6fb5fa3c 679 df_bb_delete (b->index);
a813c111 680 BB_END (a) = a_end;
ca6c03ca 681}
bc35512f 682
6fb5fa3c 683
bc35512f 684/* Return true when block A and B can be merged. */
9678086d 685
b48d0358
DN
686static bool
687rtl_can_merge_blocks (basic_block a, basic_block b)
bc35512f 688{
750054a2
CT
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
076c7ab8
ZW
691 and cold sections.
692
8e8d5162 693 Basic block partitioning may result in some jumps that appear to
c22cacf3
MS
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
8e8d5162 697 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
076c7ab8 698
87c8b4be 699 if (BB_PARTITION (a) != BB_PARTITION (b))
076c7ab8 700 return false;
750054a2 701
bc35512f 702 /* There must be exactly one edge in between the blocks. */
c5cbcccf
ZD
703 return (single_succ_p (a)
704 && single_succ (a) == b
705 && single_pred_p (b)
628f6a4e 706 && a != b
bc35512f 707 /* Must be simple edge. */
c5cbcccf 708 && !(single_succ_edge (a)->flags & EDGE_COMPLEX)
bc35512f
JH
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. */
4b4bf941 713 && (!JUMP_P (BB_END (a))
e24e7211 714 || (reload_completed
a813c111 715 ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
bc35512f 716}
ca6c03ca 717\f
5f0d2358
RK
718/* Return the label in the head of basic block BLOCK. Create one if it doesn't
719 exist. */
ca6c03ca
JH
720
721rtx
d329e058 722block_label (basic_block block)
ca6c03ca
JH
723{
724 if (block == EXIT_BLOCK_PTR)
725 return NULL_RTX;
5f0d2358 726
4b4bf941 727 if (!LABEL_P (BB_HEAD (block)))
ca6c03ca 728 {
a813c111 729 BB_HEAD (block) = emit_label_before (gen_label_rtx (), BB_HEAD (block));
ca6c03ca 730 }
5f0d2358 731
a813c111 732 return BB_HEAD (block);
ca6c03ca
JH
733}
734
735/* Attempt to perform edge redirection by replacing possibly complex jump
5f0d2358
RK
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. */
ca6c03ca 739
6de9cd9a 740edge
bc35512f 741try_redirect_by_replacing_jump (edge e, basic_block target, bool in_cfglayout)
ca6c03ca
JH
742{
743 basic_block src = e->src;
a813c111 744 rtx insn = BB_END (src), kill_from;
e1233a7d 745 rtx set;
ca6c03ca 746 int fallthru = 0;
750054a2
CT
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
8e8d5162
CT
750 and cold sections.
751
752 Basic block partitioning may result in some jumps that appear to
c22cacf3
MS
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
8e8d5162 756 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
c22cacf3 757
87c8b4be
CT
758 if (find_reg_note (insn, REG_CROSSING_JUMP, NULL_RTX)
759 || BB_PARTITION (src) != BB_PARTITION (target))
9cf84a3c 760 return NULL;
750054a2 761
6a66a8a7
KH
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;
5f0d2358 771
6a66a8a7 772 if (!onlyjump_p (insn))
6de9cd9a 773 return NULL;
3348b696 774 if ((!optimize || reload_completed) && tablejump_p (insn, NULL, NULL))
6de9cd9a 775 return NULL;
ca6c03ca
JH
776
777 /* Avoid removing branch with side effects. */
778 set = single_set (insn);
779 if (!set || side_effects_p (set))
6de9cd9a 780 return NULL;
ca6c03ca
JH
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
9caea4a7
RS
786 if (reg_mentioned_p (cc0_rtx, PATTERN (insn))
787 && only_sets_cc0_p (PREV_INSN (insn)))
ca6c03ca
JH
788 kill_from = PREV_INSN (insn);
789#endif
790
791 /* See if we can create the fallthru edge. */
bc35512f 792 if (in_cfglayout || can_fallthru (src, target))
ca6c03ca 793 {
c263766c
RH
794 if (dump_file)
795 fprintf (dump_file, "Removing jump %i.\n", INSN_UID (insn));
ca6c03ca
JH
796 fallthru = 1;
797
eaec9b3d 798 /* Selectively unlink whole insn chain. */
bc35512f
JH
799 if (in_cfglayout)
800 {
370369e1 801 rtx insn = src->il.rtl->footer;
bc35512f 802
a7b87f73 803 delete_insn_chain (kill_from, BB_END (src), false);
bc35512f
JH
804
805 /* Remove barriers but keep jumptables. */
806 while (insn)
807 {
4b4bf941 808 if (BARRIER_P (insn))
bc35512f
JH
809 {
810 if (PREV_INSN (insn))
811 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
812 else
370369e1 813 src->il.rtl->footer = NEXT_INSN (insn);
bc35512f
JH
814 if (NEXT_INSN (insn))
815 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
816 }
4b4bf941 817 if (LABEL_P (insn))
bc35512f
JH
818 break;
819 insn = NEXT_INSN (insn);
820 }
821 }
822 else
a7b87f73
ZD
823 delete_insn_chain (kill_from, PREV_INSN (BB_HEAD (target)),
824 false);
ca6c03ca 825 }
5f0d2358 826
ca6c03ca
JH
827 /* If this already is simplejump, redirect it. */
828 else if (simplejump_p (insn))
829 {
830 if (e->dest == target)
6de9cd9a 831 return NULL;
c263766c
RH
832 if (dump_file)
833 fprintf (dump_file, "Redirecting jump %i from %i to %i.\n",
0b17ab2f 834 INSN_UID (insn), e->dest->index, target->index);
6ee3c8e4
JJ
835 if (!redirect_jump (insn, block_label (target), 0))
836 {
341c100f
NS
837 gcc_assert (target == EXIT_BLOCK_PTR);
838 return NULL;
6ee3c8e4 839 }
ca6c03ca 840 }
5f0d2358 841
6ee3c8e4
JJ
842 /* Cannot do anything for target exit block. */
843 else if (target == EXIT_BLOCK_PTR)
6de9cd9a 844 return NULL;
6ee3c8e4 845
ca6c03ca
JH
846 /* Or replace possibly complicated jump insn by simple jump insn. */
847 else
848 {
849 rtx target_label = block_label (target);
eb5b8ad4 850 rtx barrier, label, table;
ca6c03ca 851
a7102479 852 emit_jump_insn_after_noloc (gen_jump (target_label), insn);
a813c111 853 JUMP_LABEL (BB_END (src)) = target_label;
ca6c03ca 854 LABEL_NUSES (target_label)++;
c263766c
RH
855 if (dump_file)
856 fprintf (dump_file, "Replacing insn %i by jump %i\n",
a813c111 857 INSN_UID (insn), INSN_UID (BB_END (src)));
ca6c03ca 858
4da2eb6b 859
ba4807a0
LB
860 delete_insn_chain (kill_from, insn, false);
861
4da2eb6b
RH
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. */
ba4807a0
LB
865 if (tablejump_p (insn, &label, &table))
866 delete_insn_chain (label, table, false);
eb5b8ad4 867
a813c111 868 barrier = next_nonnote_insn (BB_END (src));
4b4bf941 869 if (!barrier || !BARRIER_P (barrier))
a813c111 870 emit_barrier_after (BB_END (src));
5d693491
JZ
871 else
872 {
a813c111 873 if (barrier != NEXT_INSN (BB_END (src)))
5d693491
JZ
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. */
a813c111 878 rtx new_insn = BB_END (src);
5d693491 879
6c3d0e31
SP
880 update_bb_for_insn_chain (NEXT_INSN (BB_END (src)),
881 PREV_INSN (barrier), src);
5d693491
JZ
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 }
ca6c03ca
JH
893 }
894
895 /* Keep only one edge out and set proper flags. */
c5cbcccf 896 if (!single_succ_p (src))
628f6a4e 897 remove_edge (e);
c5cbcccf 898 gcc_assert (single_succ_p (src));
628f6a4e 899
c5cbcccf 900 e = single_succ_edge (src);
ca6c03ca
JH
901 if (fallthru)
902 e->flags = EDGE_FALLTHRU;
903 else
904 e->flags = 0;
5f0d2358 905
ca6c03ca
JH
906 e->probability = REG_BR_PROB_BASE;
907 e->count = src->count;
908
ca6c03ca
JH
909 if (e->dest != target)
910 redirect_edge_succ (e, target);
6de9cd9a 911 return e;
ca6c03ca
JH
912}
913
4e3825db
MM
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
920static bool
921patch_jump_insn (rtx insn, rtx old_label, basic_block new_bb)
ca6c03ca
JH
922{
923 rtx tmp;
ca6c03ca 924 /* Recognize a tablejump and adjust all matching cases. */
e1233a7d 925 if (tablejump_p (insn, NULL, &tmp))
ca6c03ca
JH
926 {
927 rtvec vec;
928 int j;
4e3825db 929 rtx new_label = block_label (new_bb);
ca6c03ca 930
4e3825db
MM
931 if (new_bb == EXIT_BLOCK_PTR)
932 return false;
ca6c03ca
JH
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
f9da5064 946 /* Handle casesi dispatch insns. */
ca6c03ca
JH
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 {
4c33cb26 953 XEXP (SET_SRC (tmp), 2) = gen_rtx_LABEL_REF (Pmode,
ca6c03ca
JH
954 new_label);
955 --LABEL_NUSES (old_label);
956 ++LABEL_NUSES (new_label);
957 }
958 }
1c384bf1
RH
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 }
ca6c03ca
JH
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. */
5f0d2358
RK
1003 if (computed_jump_p (insn)
1004 /* A return instruction can't be redirected. */
1005 || returnjump_p (insn))
4e3825db 1006 return false;
6ee3c8e4 1007
4e3825db 1008 if (!currently_expanding_to_rtl || JUMP_LABEL (insn) == old_label)
6ee3c8e4 1009 {
4e3825db
MM
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 }
6ee3c8e4 1021 }
ca6c03ca 1022 }
4e3825db
MM
1023 return true;
1024}
1025
1026
1027/* Redirect edge representing branch of (un)conditional jump or tablejump,
1028 NULL on failure */
1029static edge
1030redirect_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;
ca6c03ca 1055
c263766c
RH
1056 if (dump_file)
1057 fprintf (dump_file, "Edge %i->%i redirected to %i\n",
0b17ab2f 1058 e->src->index, e->dest->index, target->index);
5f0d2358 1059
ca6c03ca 1060 if (e->dest != target)
6de9cd9a 1061 e = redirect_edge_succ_nodup (e, target);
6fb5fa3c 1062
6de9cd9a 1063 return e;
bc35512f
JH
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
6de9cd9a
DN
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. */
bc35512f 1076
6de9cd9a 1077static edge
5671bf27 1078rtl_redirect_edge_and_branch (edge e, basic_block target)
bc35512f 1079{
6de9cd9a 1080 edge ret;
f345f21a
JH
1081 basic_block src = e->src;
1082
bc35512f 1083 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
6de9cd9a 1084 return NULL;
bc35512f 1085
3348b696 1086 if (e->dest == target)
6de9cd9a 1087 return e;
3348b696 1088
6de9cd9a 1089 if ((ret = try_redirect_by_replacing_jump (e, target, false)) != NULL)
f345f21a 1090 {
6fb5fa3c 1091 df_set_bb_dirty (src);
6de9cd9a 1092 return ret;
f345f21a 1093 }
bc35512f 1094
6de9cd9a
DN
1095 ret = redirect_branch_edge (e, target);
1096 if (!ret)
1097 return NULL;
5f0d2358 1098
6fb5fa3c 1099 df_set_bb_dirty (src);
6de9cd9a 1100 return ret;
ca6c03ca
JH
1101}
1102
4fe9b91c 1103/* Like force_nonfallthru below, but additionally performs redirection
ca6c03ca
JH
1104 Used by redirect_edge_and_branch_force. */
1105
9167e1c0 1106static basic_block
d329e058 1107force_nonfallthru_and_redirect (edge e, basic_block target)
ca6c03ca 1108{
a3716585 1109 basic_block jump_block, new_bb = NULL, src = e->src;
ca6c03ca
JH
1110 rtx note;
1111 edge new_edge;
a3716585 1112 int abnormal_edge_flags = 0;
7241571e 1113 int loc;
ca6c03ca 1114
cb9a1d9b
JH
1115 /* In the case the last instruction is conditional jump to the next
1116 instruction, first redirect the jump itself and then continue
b20b352b 1117 by creating a basic block afterwards to redirect fallthru edge. */
cb9a1d9b 1118 if (e->src != ENTRY_BLOCK_PTR && e->dest != EXIT_BLOCK_PTR
a813c111 1119 && any_condjump_p (BB_END (e->src))
a813c111 1120 && JUMP_LABEL (BB_END (e->src)) == BB_HEAD (e->dest))
cb9a1d9b
JH
1121 {
1122 rtx note;
58e6ae30 1123 edge b = unchecked_make_edge (e->src, target, 0);
341c100f 1124 bool redirected;
cb9a1d9b 1125
341c100f
NS
1126 redirected = redirect_jump (BB_END (e->src), block_label (target), 0);
1127 gcc_assert (redirected);
c22cacf3 1128
a813c111 1129 note = find_reg_note (BB_END (e->src), REG_BR_PROB, NULL_RTX);
cb9a1d9b
JH
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
ca6c03ca 1145 if (e->flags & EDGE_ABNORMAL)
a3716585
JH
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
d329e058 1150 one and create separate abnormal edge to original destination.
a3716585 1151 This allows bb-reorder to make such edge non-fallthru. */
341c100f 1152 gcc_assert (e->dest == target);
a3716585
JH
1153 abnormal_edge_flags = e->flags & ~(EDGE_FALLTHRU | EDGE_CAN_FALLTHRU);
1154 e->flags &= EDGE_FALLTHRU | EDGE_CAN_FALLTHRU;
1155 }
341c100f 1156 else
24c545ff 1157 {
341c100f
NS
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
628f6a4e
BE
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;
c22cacf3 1167
628f6a4e 1168 basic_block bb = create_basic_block (BB_HEAD (e->dest), NULL, ENTRY_BLOCK_PTR);
c22cacf3 1169
341c100f
NS
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;
628f6a4e
BE
1173 for (ei = ei_start (ENTRY_BLOCK_PTR->succs); (tmp = ei_safe_edge (ei)); )
1174 {
1175 if (tmp == e)
1176 {
865851d0 1177 VEC_unordered_remove (edge, ENTRY_BLOCK_PTR->succs, ei.index);
628f6a4e
BE
1178 found = true;
1179 break;
1180 }
1181 else
1182 ei_next (&ei);
1183 }
c22cacf3 1184
628f6a4e 1185 gcc_assert (found);
c22cacf3 1186
d4e6fecb 1187 VEC_safe_push (edge, gc, bb->succs, e);
341c100f
NS
1188 make_single_succ_edge (ENTRY_BLOCK_PTR, bb, EDGE_FALLTHRU);
1189 }
24c545ff
BS
1190 }
1191
628f6a4e 1192 if (EDGE_COUNT (e->src->succs) >= 2 || abnormal_edge_flags)
ca6c03ca
JH
1193 {
1194 /* Create the new structures. */
31a78298 1195
79019985
RH
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. */
a813c111
SB
1199 if (!tablejump_p (BB_END (e->src), NULL, &note))
1200 note = BB_END (e->src);
31a78298
RH
1201 note = NEXT_INSN (note);
1202
31a78298 1203 jump_block = create_basic_block (note, NULL, e->src);
ca6c03ca
JH
1204 jump_block->count = e->count;
1205 jump_block->frequency = EDGE_FREQUENCY (e);
1206 jump_block->loop_depth = target->loop_depth;
1207
750054a2
CT
1208 /* Make sure new block ends up in correct hot/cold section. */
1209
076c7ab8 1210 BB_COPY_PARTITION (jump_block, e->src);
9fb32434 1211 if (flag_reorder_blocks_and_partition
87c8b4be
CT
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))
65c5f2a6 1216 add_reg_note (BB_END (jump_block), REG_CROSSING_JUMP, NULL_RTX);
c22cacf3 1217
ca6c03ca
JH
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;
5f0d2358 1231
7241571e
JJ
1232 if (e->goto_locus && e->goto_block == NULL)
1233 loc = e->goto_locus;
1234 else
1235 loc = 0;
ca6c03ca
JH
1236 e->flags &= ~EDGE_FALLTHRU;
1237 if (target == EXIT_BLOCK_PTR)
1238 {
cf22ce3c 1239#ifdef HAVE_return
7241571e 1240 emit_jump_insn_after_setloc (gen_return (), BB_END (jump_block), loc);
cf22ce3c 1241#else
341c100f 1242 gcc_unreachable ();
cf22ce3c 1243#endif
ca6c03ca
JH
1244 }
1245 else
1246 {
1247 rtx label = block_label (target);
7241571e 1248 emit_jump_insn_after_setloc (gen_jump (label), BB_END (jump_block), loc);
a813c111 1249 JUMP_LABEL (BB_END (jump_block)) = label;
ca6c03ca
JH
1250 LABEL_NUSES (label)++;
1251 }
5f0d2358 1252
a813c111 1253 emit_barrier_after (BB_END (jump_block));
ca6c03ca
JH
1254 redirect_edge_succ_nodup (e, target);
1255
a3716585
JH
1256 if (abnormal_edge_flags)
1257 make_edge (src, target, abnormal_edge_flags);
1258
b8698a0f 1259 df_mark_solutions_dirty ();
ca6c03ca
JH
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. */
5f0d2358 1266
ca6c03ca 1267basic_block
d329e058 1268force_nonfallthru (edge e)
ca6c03ca
JH
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.
41806d92 1275 Conversion must be possible. */
ca6c03ca 1276
9ee634e3 1277static basic_block
d329e058 1278rtl_redirect_edge_and_branch_force (edge e, basic_block target)
ca6c03ca 1279{
5f0d2358
RK
1280 if (redirect_edge_and_branch (e, target)
1281 || e->dest == target)
ca6c03ca
JH
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. */
6fb5fa3c 1286 df_set_bb_dirty (e->src);
5f0d2358 1287 return force_nonfallthru_and_redirect (e, target);
ca6c03ca
JH
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
f470c378
ZD
1293static void
1294rtl_tidy_fallthru_edge (edge e)
ca6c03ca
JH
1295{
1296 rtx q;
f470c378
ZD
1297 basic_block b = e->src, c = b->next_bb;
1298
ca6c03ca
JH
1299 /* ??? In a late-running flow pass, other folks may have deleted basic
1300 blocks by nopping out blocks, leaving multiple BARRIERs between here
0fa2e4df 1301 and the target label. They ought to be chastised and fixed.
ca6c03ca
JH
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
a813c111 1309 for (q = NEXT_INSN (BB_END (b)); q != BB_HEAD (c); q = NEXT_INSN (q))
9c0a0632
RH
1310 if (INSN_P (q))
1311 return;
ca6c03ca
JH
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. */
a813c111 1316 q = BB_END (b);
4b4bf941 1317 if (JUMP_P (q)
ca6c03ca
JH
1318 && onlyjump_p (q)
1319 && (any_uncondjump_p (q)
c5cbcccf 1320 || single_succ_p (b)))
ca6c03ca
JH
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);
ca6c03ca
JH
1330 }
1331
1332 /* Selectively unlink the sequence. */
a813c111 1333 if (q != PREV_INSN (BB_HEAD (c)))
a7b87f73 1334 delete_insn_chain (NEXT_INSN (q), PREV_INSN (BB_HEAD (c)), false);
ca6c03ca
JH
1335
1336 e->flags |= EDGE_FALLTHRU;
1337}
ca6c03ca 1338\f
f470c378
ZD
1339/* Should move basic block BB after basic block AFTER. NIY. */
1340
1341static bool
1342rtl_move_block_after (basic_block bb ATTRIBUTE_UNUSED,
1343 basic_block after ATTRIBUTE_UNUSED)
1344{
1345 return false;
1346}
1347
ca6c03ca 1348/* Split a (typically critical) edge. Return the new block.
41806d92 1349 The edge must not be abnormal.
ca6c03ca
JH
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
8ce33230 1355static basic_block
d329e058 1356rtl_split_edge (edge edge_in)
ca6c03ca
JH
1357{
1358 basic_block bb;
ca6c03ca
JH
1359 rtx before;
1360
1361 /* Abnormal edges cannot be split. */
341c100f 1362 gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
ca6c03ca
JH
1363
1364 /* We are going to place the new block in front of edge destination.
eaec9b3d 1365 Avoid existence of fallthru predecessors. */
ca6c03ca
JH
1366 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1367 {
1368 edge e;
628f6a4e 1369 edge_iterator ei;
5f0d2358 1370
628f6a4e 1371 FOR_EACH_EDGE (e, ei, edge_in->dest->preds)
ca6c03ca
JH
1372 if (e->flags & EDGE_FALLTHRU)
1373 break;
1374
1375 if (e)
1376 force_nonfallthru (e);
1377 }
1378
96e82e0a
ZD
1379 /* Create the basic block note. */
1380 if (edge_in->dest != EXIT_BLOCK_PTR)
a813c111 1381 before = BB_HEAD (edge_in->dest);
ca6c03ca
JH
1382 else
1383 before = NULL_RTX;
1384
623a66fa
R
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));
623a66fa 1390 bb = create_basic_block (before, NULL, edge_in->src);
076c7ab8 1391 BB_COPY_PARTITION (bb, edge_in->src);
623a66fa
R
1392 }
1393 else
9fb32434
CT
1394 {
1395 bb = create_basic_block (before, NULL, edge_in->dest->prev_bb);
076c7ab8
ZW
1396 /* ??? Why not edge_in->dest->prev_bb here? */
1397 BB_COPY_PARTITION (bb, edge_in->dest);
9fb32434 1398 }
ca6c03ca 1399
4977bab6 1400 make_single_succ_edge (bb, edge_in->dest, EDGE_FALLTHRU);
ca6c03ca 1401
4d6922ee 1402 /* For non-fallthru edges, we must adjust the predecessor's
ca6c03ca
JH
1403 jump instruction to target our new block. */
1404 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1405 {
341c100f
NS
1406 edge redirected = redirect_edge_and_branch (edge_in, bb);
1407 gcc_assert (redirected);
ca6c03ca
JH
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
1419void
d329e058 1420insert_insn_on_edge (rtx pattern, edge e)
ca6c03ca
JH
1421{
1422 /* We cannot insert instructions on an abnormal critical edge.
1423 It will be easier to find the culprit if we die now. */
341c100f 1424 gcc_assert (!((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e)));
ca6c03ca 1425
6de9cd9a 1426 if (e->insns.r == NULL_RTX)
ca6c03ca
JH
1427 start_sequence ();
1428 else
6de9cd9a 1429 push_to_sequence (e->insns.r);
ca6c03ca
JH
1430
1431 emit_insn (pattern);
1432
6de9cd9a 1433 e->insns.r = get_insns ();
ca6c03ca
JH
1434 end_sequence ();
1435}
1436
1437/* Update the CFG for the instructions queued on edge E. */
1438
4e3825db 1439void
2ac66157 1440commit_one_edge_insertion (edge e)
ca6c03ca
JH
1441{
1442 rtx before = NULL_RTX, after = NULL_RTX, insns, tmp, last;
eae4bc56 1443 basic_block bb = NULL;
ca6c03ca
JH
1444
1445 /* Pull the insns off the edge now since the edge might go away. */
6de9cd9a
DN
1446 insns = e->insns.r;
1447 e->insns.r = NULL_RTX;
ca6c03ca 1448
3dec4024 1449 if (!before && !after)
ca6c03ca 1450 {
3dec4024 1451 /* Figure out where to put these things. If the destination has
c22cacf3 1452 one predecessor, insert there. Except for the exit block. */
c5cbcccf 1453 if (single_pred_p (e->dest) && e->dest != EXIT_BLOCK_PTR)
ca6c03ca 1454 {
3dec4024
JH
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. */
a813c111 1459 tmp = BB_HEAD (bb);
4b4bf941 1460 if (LABEL_P (tmp))
3dec4024
JH
1461 tmp = NEXT_INSN (tmp);
1462 if (NOTE_INSN_BASIC_BLOCK_P (tmp))
1463 tmp = NEXT_INSN (tmp);
a813c111 1464 if (tmp == BB_HEAD (bb))
3dec4024
JH
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,
c22cacf3 1473 insert there. Except for the entry block. */
3dec4024 1474 else if ((e->flags & EDGE_ABNORMAL) == 0
c5cbcccf 1475 && single_succ_p (e->src)
3dec4024
JH
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. */
4b4bf941 1486 if (JUMP_P (BB_END (bb)))
96e82e0a 1487 before = BB_END (bb);
3dec4024
JH
1488 else
1489 {
341c100f
NS
1490 /* We'd better be fallthru, or we've lost track of
1491 what's what. */
1492 gcc_assert (e->flags & EDGE_FALLTHRU);
ca6c03ca 1493
a813c111 1494 after = BB_END (bb);
3dec4024
JH
1495 }
1496 }
1497 /* Otherwise we must split the edge. */
1498 else
1499 {
1500 bb = split_edge (e);
a813c111 1501 after = BB_END (bb);
750054a2 1502
750054a2 1503 if (flag_reorder_blocks_and_partition
9fb32434 1504 && targetm.have_named_sections
750054a2 1505 && e->src != ENTRY_BLOCK_PTR
076c7ab8 1506 && BB_PARTITION (e->src) == BB_COLD_PARTITION
6e0b03f5
PC
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);
ca6c03ca
JH
1512 }
1513 }
1514
ca6c03ca
JH
1515 /* Now that we've found the spot, do the insertion. */
1516
1517 if (before)
1518 {
6fb5fa3c 1519 emit_insn_before_noloc (insns, before, bb);
ca6c03ca
JH
1520 last = prev_nonnote_insn (before);
1521 }
1522 else
6fb5fa3c 1523 last = emit_insn_after_noloc (insns, after, bb);
ca6c03ca
JH
1524
1525 if (returnjump_p (last))
1526 {
1527 /* ??? Remove all outgoing edges from BB and add one for EXIT.
c22cacf3
MS
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. */
ca6c03ca 1531
c5cbcccf 1532 e = single_succ_edge (bb);
341c100f 1533 gcc_assert (e->dest == EXIT_BLOCK_PTR
c5cbcccf 1534 && single_succ_p (bb) && (e->flags & EDGE_FALLTHRU));
ca6c03ca 1535
5f0d2358 1536 e->flags &= ~EDGE_FALLTHRU;
ca6c03ca 1537 emit_barrier_after (last);
0b17ab2f 1538
ca6c03ca
JH
1539 if (before)
1540 delete_insn (before);
1541 }
341c100f
NS
1542 else
1543 gcc_assert (!JUMP_P (last));
5f0d2358 1544
12eff7b7 1545 /* Mark the basic block for find_many_sub_basic_blocks. */
05549c96
SB
1546 if (current_ir_type () != IR_RTL_CFGLAYOUT)
1547 bb->aux = &bb->aux;
ca6c03ca
JH
1548}
1549
1550/* Update the CFG for all queued instructions. */
1551
1552void
d329e058 1553commit_edge_insertions (void)
ca6c03ca 1554{
ca6c03ca 1555 basic_block bb;
9dca2ad5 1556 sbitmap blocks;
9809a362 1557 bool changed = false;
ca6c03ca
JH
1558
1559#ifdef ENABLE_CHECKING
1560 verify_flow_info ();
1561#endif
1562
e0082a72 1563 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
ca6c03ca 1564 {
628f6a4e
BE
1565 edge e;
1566 edge_iterator ei;
ca6c03ca 1567
628f6a4e
BE
1568 FOR_EACH_EDGE (e, ei, bb->succs)
1569 if (e->insns.r)
1570 {
1571 changed = true;
2ac66157 1572 commit_one_edge_insertion (e);
628f6a4e 1573 }
3dec4024 1574 }
9dca2ad5 1575
9809a362
JH
1576 if (!changed)
1577 return;
1578
05549c96
SB
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
9809a362
JH
1586 blocks = sbitmap_alloc (last_basic_block);
1587 sbitmap_zero (blocks);
1588 FOR_EACH_BB (bb)
1589 if (bb->aux)
1590 {
c22cacf3 1591 SET_BIT (blocks, bb->index);
9809a362
JH
1592 /* Check for forgotten bb->aux values before commit_edge_insertions
1593 call. */
341c100f 1594 gcc_assert (bb->aux == &bb->aux);
9809a362
JH
1595 bb->aux = NULL;
1596 }
1597 find_many_sub_basic_blocks (blocks);
1598 sbitmap_free (blocks);
ca6c03ca
JH
1599}
1600\f
6fb5fa3c 1601
f470c378
ZD
1602/* Print out RTL-specific basic block information (live information
1603 at start and end). */
ca6c03ca 1604
10e9fecc 1605static void
726a989a 1606rtl_dump_bb (basic_block bb, FILE *outf, int indent, int flags ATTRIBUTE_UNUSED)
ca6c03ca
JH
1607{
1608 rtx insn;
1609 rtx last;
f470c378 1610 char *s_indent;
ca6c03ca 1611
ae50c0cb 1612 s_indent = (char *) alloca ((size_t) indent + 1);
400e39e3 1613 memset (s_indent, ' ', (size_t) indent);
f470c378 1614 s_indent[indent] = '\0';
b8698a0f 1615
6fb5fa3c
DB
1616 if (df)
1617 {
1618 df_dump_top (bb, outf);
1619 putc ('\n', outf);
1620 }
ca6c03ca 1621
a813c111 1622 for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb)); insn != last;
ca6c03ca
JH
1623 insn = NEXT_INSN (insn))
1624 print_rtl_single (outf, insn);
1625
6fb5fa3c
DB
1626 if (df)
1627 {
1628 df_dump_bottom (bb, outf);
1629 putc ('\n', outf);
1630 }
1631
ca6c03ca
JH
1632}
1633\f
1634/* Like print_rtl, but also print out live information for the start of each
1635 basic block. */
1636
1637void
22ea9ec0 1638print_rtl_with_bb (FILE *outf, const_rtx rtx_first)
ca6c03ca 1639{
22ea9ec0 1640 const_rtx tmp_rtx;
ca6c03ca
JH
1641 if (rtx_first == 0)
1642 fprintf (outf, "(nil)\n");
1643 else
1644 {
ca6c03ca
JH
1645 enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
1646 int max_uid = get_max_uid ();
5ed6ace5
MD
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);
ca6c03ca 1650
e0082a72
ZD
1651 basic_block bb;
1652
6fb5fa3c
DB
1653 if (df)
1654 df_dump_start (outf);
1655
e0082a72 1656 FOR_EACH_BB_REVERSE (bb)
ca6c03ca 1657 {
ca6c03ca
JH
1658 rtx x;
1659
a813c111
SB
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))
ca6c03ca
JH
1663 {
1664 enum bb_state state = IN_MULTIPLE_BB;
5f0d2358 1665
ca6c03ca
JH
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
a813c111 1670 if (x == BB_END (bb))
ca6c03ca
JH
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;
ca6c03ca
JH
1678 if ((bb = start[INSN_UID (tmp_rtx)]) != NULL)
1679 {
6fb5fa3c
DB
1680 edge e;
1681 edge_iterator ei;
b8698a0f 1682
6fb5fa3c
DB
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 }
e9ec5c6b
SB
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 }
ca6c03ca
JH
1699 }
1700
1701 if (in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB
4b4bf941
JQ
1702 && !NOTE_P (tmp_rtx)
1703 && !BARRIER_P (tmp_rtx))
ca6c03ca
JH
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 {
6fb5fa3c
DB
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 }
ca6c03ca 1725 putc ('\n', outf);
e9ec5c6b
SB
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 }
ca6c03ca 1732 }
ca6c03ca
JH
1733 if (did_output)
1734 putc ('\n', outf);
1735 }
1736
1737 free (start);
1738 free (end);
1739 free (in_bb_p);
1740 }
1741
cb91fab0 1742 if (crtl->epilogue_delay_list != 0)
ca6c03ca
JH
1743 {
1744 fprintf (outf, "\n;; Insns in epilogue delay list:\n\n");
cb91fab0 1745 for (tmp_rtx = crtl->epilogue_delay_list; tmp_rtx != 0;
ca6c03ca
JH
1746 tmp_rtx = XEXP (tmp_rtx, 1))
1747 print_rtl_single (outf, XEXP (tmp_rtx, 0));
1748 }
1749}
1750\f
b446e5a2 1751void
d329e058 1752update_br_prob_note (basic_block bb)
b446e5a2
JH
1753{
1754 rtx note;
4b4bf941 1755 if (!JUMP_P (BB_END (bb)))
b446e5a2 1756 return;
a813c111 1757 note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX);
b446e5a2
JH
1758 if (!note || INTVAL (XEXP (note, 0)) == BRANCH_EDGE (bb)->probability)
1759 return;
1760 XEXP (note, 0) = GEN_INT (BRANCH_EDGE (bb)->probability);
1761}
96370780
MK
1762
1763/* Get the last insn associated with block BB (that includes barriers and
1764 tablejumps after BB). */
1765rtx
1766get_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. */
1e211590 1776 tmp = next_nonnote_insn_bb (end);
96370780
MK
1777 while (tmp && BARRIER_P (tmp))
1778 {
1779 end = tmp;
1e211590 1780 tmp = next_nonnote_insn_bb (end);
96370780
MK
1781 }
1782
1783 return end;
1784}
b446e5a2 1785\f
10e9fecc
JH
1786/* Verify the CFG and RTL consistency common for both underlying RTL and
1787 cfglayout RTL.
ca6c03ca
JH
1788
1789 Currently it does following checks:
1790
ca6c03ca 1791 - overlapping of basic blocks
9eab6785 1792 - insns with wrong BLOCK_FOR_INSN pointers
ca6c03ca 1793 - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note)
f63d1bf7 1794 - tails of basic blocks (ensure that boundary is necessary)
ca6c03ca
JH
1795 - scans body of the basic block for JUMP_INSN, CODE_LABEL
1796 and NOTE_INSN_BASIC_BLOCK
750054a2 1797 - verify that no fall_thru edge crosses hot/cold partition boundaries
9eab6785 1798 - verify that there are no pending RTL branch predictions
ca6c03ca
JH
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.). */
f470c378 1802
10e9fecc 1803static int
d329e058 1804rtl_verify_flow_info_1 (void)
ca6c03ca 1805{
ca6c03ca 1806 rtx x;
10e9fecc 1807 int err = 0;
94eb5ddb 1808 basic_block bb;
ca6c03ca 1809
9eab6785 1810 /* Check the general integrity of the basic blocks. */
e0082a72 1811 FOR_EACH_BB_REVERSE (bb)
ca6c03ca 1812 {
9eab6785 1813 rtx insn;
5f0d2358 1814
5e2d947c
JH
1815 if (!(bb->flags & BB_RTL))
1816 {
1817 error ("BB_RTL flag not set for block %d", bb->index);
1818 err = 1;
1819 }
1820
9eab6785 1821 FOR_BB_INSNS (bb, insn)
8ce9fd5d 1822 if (BLOCK_FOR_INSN (insn) != bb)
9eab6785
SB
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 }
a7b87f73
ZD
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 }
ca6c03ca
JH
1847 }
1848
1849 /* Now check the basic blocks (boundaries etc.) */
e0082a72 1850 FOR_EACH_BB_REVERSE (bb)
ca6c03ca 1851 {
3dec4024 1852 int n_fallthru = 0, n_eh = 0, n_call = 0, n_abnormal = 0, n_branch = 0;
3cf54412 1853 edge e, fallthru = NULL;
5a1a3e5e 1854 rtx note;
628f6a4e 1855 edge_iterator ei;
ca6c03ca 1856
2085a21f 1857 if (JUMP_P (BB_END (bb))
a813c111 1858 && (note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX))
628f6a4e 1859 && EDGE_COUNT (bb->succs) >= 2
a813c111 1860 && any_condjump_p (BB_END (bb)))
5a1a3e5e 1861 {
e53de54d
JH
1862 if (INTVAL (XEXP (note, 0)) != BRANCH_EDGE (bb)->probability
1863 && profile_status != PROFILE_ABSENT)
5a1a3e5e 1864 {
0108ae51 1865 error ("verify_flow_info: REG_BR_PROB does not match cfg %wi %i",
5a1a3e5e
JH
1866 INTVAL (XEXP (note, 0)), BRANCH_EDGE (bb)->probability);
1867 err = 1;
1868 }
1869 }
628f6a4e 1870 FOR_EACH_EDGE (e, ei, bb->succs)
ca6c03ca 1871 {
ca6c03ca 1872 if (e->flags & EDGE_FALLTHRU)
750054a2
CT
1873 {
1874 n_fallthru++, fallthru = e;
bd454efd 1875 if ((e->flags & EDGE_CROSSING)
076c7ab8 1876 || (BB_PARTITION (e->src) != BB_PARTITION (e->dest)
9fb32434
CT
1877 && e->src != ENTRY_BLOCK_PTR
1878 && e->dest != EXIT_BLOCK_PTR))
c22cacf3 1879 {
ab532386 1880 error ("fallthru edge crosses section boundary (bb %i)",
750054a2
CT
1881 e->src->index);
1882 err = 1;
1883 }
1884 }
3dec4024 1885
65f43cdf
ZD
1886 if ((e->flags & ~(EDGE_DFS_BACK
1887 | EDGE_CAN_FALLTHRU
1888 | EDGE_IRREDUCIBLE_LOOP
9beb1c84
CT
1889 | EDGE_LOOP_EXIT
1890 | EDGE_CROSSING)) == 0)
3dec4024
JH
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++;
ca6c03ca 1900 }
5f0d2358 1901
1d65f45c 1902 if (n_eh && !find_reg_note (BB_END (bb), REG_EH_REGION, NULL_RTX))
3dec4024 1903 {
ab532386 1904 error ("missing REG_EH_REGION note in the end of bb %i", bb->index);
3dec4024
JH
1905 err = 1;
1906 }
1d65f45c
RH
1907 if (n_eh > 1)
1908 {
1909 error ("too many eh edges %i", bb->index);
1910 err = 1;
1911 }
3dec4024 1912 if (n_branch
4b4bf941 1913 && (!JUMP_P (BB_END (bb))
a813c111
SB
1914 || (n_branch > 1 && (any_uncondjump_p (BB_END (bb))
1915 || any_condjump_p (BB_END (bb))))))
3dec4024 1916 {
ab532386 1917 error ("too many outgoing branch edges from bb %i", bb->index);
3dec4024
JH
1918 err = 1;
1919 }
a813c111 1920 if (n_fallthru && any_uncondjump_p (BB_END (bb)))
3dec4024 1921 {
ab532386 1922 error ("fallthru edge after unconditional jump %i", bb->index);
3dec4024
JH
1923 err = 1;
1924 }
a813c111 1925 if (n_branch != 1 && any_uncondjump_p (BB_END (bb)))
3dec4024 1926 {
1d65f45c
RH
1927 error ("wrong number of branch edges after unconditional jump %i",
1928 bb->index);
3dec4024
JH
1929 err = 1;
1930 }
a813c111 1931 if (n_branch != 1 && any_condjump_p (BB_END (bb))
c11fd0b2 1932 && JUMP_LABEL (BB_END (bb)) != BB_HEAD (fallthru->dest))
3dec4024 1933 {
c11fd0b2
RG
1934 error ("wrong amount of branch edges after conditional jump %i",
1935 bb->index);
3dec4024
JH
1936 err = 1;
1937 }
4b4bf941 1938 if (n_call && !CALL_P (BB_END (bb)))
3dec4024 1939 {
ab532386 1940 error ("call edges for non-call insn in bb %i", bb->index);
3dec4024
JH
1941 err = 1;
1942 }
1943 if (n_abnormal
4b4bf941
JQ
1944 && (!CALL_P (BB_END (bb)) && n_call != n_abnormal)
1945 && (!JUMP_P (BB_END (bb))
a813c111
SB
1946 || any_condjump_p (BB_END (bb))
1947 || any_uncondjump_p (BB_END (bb))))
3dec4024 1948 {
ab532386 1949 error ("abnormal edges for no purpose in bb %i", bb->index);
3dec4024
JH
1950 err = 1;
1951 }
f87c27b4 1952
a813c111 1953 for (x = BB_HEAD (bb); x != NEXT_INSN (BB_END (bb)); x = NEXT_INSN (x))
0ca541aa 1954 /* We may have a barrier inside a basic block before dead code
9524880c
HPN
1955 elimination. There is no BLOCK_FOR_INSN field in a barrier. */
1956 if (!BARRIER_P (x) && BLOCK_FOR_INSN (x) != bb)
5f0d2358
RK
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",
0b17ab2f 1962 INSN_UID (x), bb->index);
5f0d2358
RK
1963 else
1964 error
1965 ("insn %d inside basic block %d but block_for_insn is %i",
0b17ab2f 1966 INSN_UID (x), bb->index, BLOCK_FOR_INSN (x)->index);
5f0d2358
RK
1967
1968 err = 1;
1969 }
ca6c03ca
JH
1970
1971 /* OK pointers are correct. Now check the header of basic
c22cacf3 1972 block. It ought to contain optional CODE_LABEL followed
ca6c03ca 1973 by NOTE_BASIC_BLOCK. */
a813c111 1974 x = BB_HEAD (bb);
4b4bf941 1975 if (LABEL_P (x))
ca6c03ca 1976 {
a813c111 1977 if (BB_END (bb) == x)
ca6c03ca
JH
1978 {
1979 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
0b17ab2f 1980 bb->index);
ca6c03ca
JH
1981 err = 1;
1982 }
5f0d2358 1983
ca6c03ca
JH
1984 x = NEXT_INSN (x);
1985 }
5f0d2358 1986
ca6c03ca
JH
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",
0b17ab2f 1990 bb->index);
ca6c03ca
JH
1991 err = 1;
1992 }
1993
a813c111 1994 if (BB_END (bb) == x)
49243025 1995 /* Do checks for empty blocks here. */
5f0d2358 1996 ;
ca6c03ca 1997 else
5f0d2358
RK
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",
0b17ab2f 2003 INSN_UID (x), bb->index);
5f0d2358
RK
2004 err = 1;
2005 }
2006
a813c111 2007 if (x == BB_END (bb))
5f0d2358 2008 break;
ca6c03ca 2009
83fd323c 2010 if (control_flow_insn_p (x))
5f0d2358 2011 {
0b17ab2f 2012 error ("in basic block %d:", bb->index);
5f0d2358
RK
2013 fatal_insn ("flow control insn inside a basic block", x);
2014 }
2015 }
ca6c03ca
JH
2016 }
2017
10e9fecc 2018 /* Clean up. */
10e9fecc
JH
2019 return err;
2020}
5f0d2358 2021
10e9fecc
JH
2022/* Verify the CFG and RTL consistency common for both underlying RTL and
2023 cfglayout RTL.
5f0d2358 2024
10e9fecc
JH
2025 Currently it does following checks:
2026 - all checks of rtl_verify_flow_info_1
9eab6785 2027 - test head/end pointers
10e9fecc
JH
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. */
9eab6785 2032
10e9fecc 2033static int
d329e058 2034rtl_verify_flow_info (void)
10e9fecc
JH
2035{
2036 basic_block bb;
2037 int err = rtl_verify_flow_info_1 ();
2038 rtx x;
9eab6785
SB
2039 rtx last_head = get_last_insn ();
2040 basic_block *bb_info;
10e9fecc
JH
2041 int num_bb_notes;
2042 const rtx rtx_first = get_insns ();
2043 basic_block last_bb_seen = ENTRY_BLOCK_PTR, curr_bb = NULL;
9eab6785
SB
2044 const int max_uid = get_max_uid ();
2045
2046 bb_info = XCNEWVEC (basic_block, max_uid);
ca6c03ca 2047
10e9fecc
JH
2048 FOR_EACH_BB_REVERSE (bb)
2049 {
2050 edge e;
628f6a4e 2051 edge_iterator ei;
9eab6785
SB
2052 rtx head = BB_HEAD (bb);
2053 rtx end = BB_END (bb);
628f6a4e 2054
9eab6785 2055 for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
a7b87f73
ZD
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 }
9eab6785
SB
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))
a00d11f0 2081 {
9eab6785
SB
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);
a00d11f0
JH
2100 err = 1;
2101 }
2102
a7b87f73 2103 last_head = PREV_INSN (x);
9eab6785 2104
628f6a4e 2105 FOR_EACH_EDGE (e, ei, bb->succs)
10e9fecc
JH
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. */
468059bc
DD
2113 for (insn = NEXT_INSN (BB_END (bb)); ; insn = NEXT_INSN (insn))
2114 {
2115 if (!insn || NOTE_INSN_BASIC_BLOCK_P (insn))
10e9fecc
JH
2116 {
2117 error ("missing barrier after block %i", bb->index);
2118 err = 1;
2119 break;
2120 }
468059bc
DD
2121 if (BARRIER_P (insn))
2122 break;
2123 }
10e9fecc
JH
2124 }
2125 else if (e->src != ENTRY_BLOCK_PTR
2126 && e->dest != EXIT_BLOCK_PTR)
c22cacf3 2127 {
10e9fecc
JH
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
a813c111 2138 for (insn = NEXT_INSN (BB_END (e->src)); insn != BB_HEAD (e->dest);
10e9fecc 2139 insn = NEXT_INSN (insn))
6be85b25 2140 if (BARRIER_P (insn) || INSN_P (insn))
10e9fecc
JH
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 }
c22cacf3 2147 }
10e9fecc 2148 }
ca6c03ca 2149
a7b87f73
ZD
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 }
9eab6785
SB
2162 free (bb_info);
2163
ca6c03ca 2164 num_bb_notes = 0;
e0082a72
ZD
2165 last_bb_seen = ENTRY_BLOCK_PTR;
2166
5f0d2358 2167 for (x = rtx_first; x; x = NEXT_INSN (x))
ca6c03ca
JH
2168 {
2169 if (NOTE_INSN_BASIC_BLOCK_P (x))
2170 {
bf77398c 2171 bb = NOTE_BASIC_BLOCK (x);
5f0d2358 2172
ca6c03ca 2173 num_bb_notes++;
e0082a72 2174 if (bb != last_bb_seen->next_bb)
10e9fecc 2175 internal_error ("basic blocks not laid down consecutively");
ca6c03ca 2176
10e9fecc 2177 curr_bb = last_bb_seen = bb;
ca6c03ca
JH
2178 }
2179
10e9fecc 2180 if (!curr_bb)
ca6c03ca
JH
2181 {
2182 switch (GET_CODE (x))
2183 {
2184 case BARRIER:
2185 case NOTE:
2186 break;
2187
2188 case CODE_LABEL:
666c27b9 2189 /* An addr_vec is placed outside any basic block. */
ca6c03ca 2190 if (NEXT_INSN (x)
481683e1 2191 && JUMP_TABLE_DATA_P (NEXT_INSN (x)))
5f0d2358 2192 x = NEXT_INSN (x);
ca6c03ca
JH
2193
2194 /* But in any case, non-deletable labels can appear anywhere. */
2195 break;
2196
2197 default:
1f978f5f 2198 fatal_insn ("insn outside basic block", x);
ca6c03ca
JH
2199 }
2200 }
2201
26cae194 2202 if (JUMP_P (x)
ca6c03ca 2203 && returnjump_p (x) && ! condjump_p (x)
15eb3a2e 2204 && ! (next_nonnote_insn (x) && BARRIER_P (next_nonnote_insn (x))))
1f978f5f 2205 fatal_insn ("return not followed by barrier", x);
a813c111 2206 if (curr_bb && x == BB_END (curr_bb))
10e9fecc 2207 curr_bb = NULL;
ca6c03ca
JH
2208 }
2209
24bd1a0b 2210 if (num_bb_notes != n_basic_blocks - NUM_FIXED_BLOCKS)
ca6c03ca 2211 internal_error
0b17ab2f
RH
2212 ("number of bb notes in insn chain (%d) != n_basic_blocks (%d)",
2213 num_bb_notes, n_basic_blocks);
ca6c03ca 2214
10e9fecc 2215 return err;
ca6c03ca
JH
2216}
2217\f
eaec9b3d 2218/* Assume that the preceding pass has possibly eliminated jump instructions
ca6c03ca
JH
2219 or converted the unconditional jumps. Eliminate the edges from CFG.
2220 Return true if any edges are eliminated. */
2221
2222bool
d329e058 2223purge_dead_edges (basic_block bb)
ca6c03ca 2224{
628f6a4e 2225 edge e;
a813c111 2226 rtx insn = BB_END (bb), note;
ca6c03ca 2227 bool purged = false;
628f6a4e
BE
2228 bool found;
2229 edge_iterator ei;
ca6c03ca 2230
b5b8b0ac
AO
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
70da1d03 2236 /* If this instruction cannot trap, remove REG_EH_REGION notes. */
4b4bf941 2237 if (NONJUMP_INSN_P (insn)
70da1d03
JH
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
546c093e 2248 /* Cleanup abnormal edges caused by exceptions or non-local gotos. */
628f6a4e 2249 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
546c093e 2250 {
1d65f45c
RH
2251 bool remove = false;
2252
e5f9a909
JW
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. */
1d65f45c 2256 if (e->flags & EDGE_ABNORMAL_CALL)
546c093e 2257 {
1d65f45c
RH
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;
546c093e 2266 }
1d65f45c
RH
2267 else if (e->flags & EDGE_EH)
2268 remove = !can_throw_internal (insn);
2269
2270 if (remove)
546c093e 2271 {
1d65f45c
RH
2272 remove_edge (e);
2273 df_set_bb_dirty (bb);
2274 purged = true;
546c093e
RH
2275 }
2276 else
1d65f45c 2277 ei_next (&ei);
546c093e 2278 }
5f0d2358 2279
4b4bf941 2280 if (JUMP_P (insn))
ca6c03ca
JH
2281 {
2282 rtx note;
2283 edge b,f;
628f6a4e 2284 edge_iterator ei;
5f0d2358 2285
ca6c03ca
JH
2286 /* We do care only about conditional jumps and simplejumps. */
2287 if (!any_condjump_p (insn)
2288 && !returnjump_p (insn)
2289 && !simplejump_p (insn))
c51d95ec 2290 return purged;
5f0d2358 2291
5a1a3e5e
JH
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
628f6a4e 2303 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
ca6c03ca 2304 {
7fcd7218
JH
2305 /* Avoid abnormal flags to leak from computed jumps turned
2306 into simplejumps. */
f87c27b4 2307
0e1638d4 2308 e->flags &= ~EDGE_ABNORMAL;
7fcd7218 2309
5a566bed
MM
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. */
628f6a4e
BE
2314 {
2315 ei_next (&ei);
2316 continue;
2317 }
5f0d2358 2318 else if (e->dest != EXIT_BLOCK_PTR
a813c111 2319 && BB_HEAD (e->dest) == JUMP_LABEL (insn))
5a566bed
MM
2320 /* If the destination block is the target of the jump,
2321 keep the edge. */
628f6a4e
BE
2322 {
2323 ei_next (&ei);
2324 continue;
2325 }
5a566bed
MM
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. */
628f6a4e
BE
2329 {
2330 ei_next (&ei);
2331 continue;
2332 }
5a566bed
MM
2333 else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
2334 /* Keep the edges that correspond to exceptions thrown by
0b75beaa
EB
2335 this instruction and rematerialize the EDGE_ABNORMAL
2336 flag we just cleared above. */
2337 {
2338 e->flags |= EDGE_ABNORMAL;
628f6a4e 2339 ei_next (&ei);
0b75beaa
EB
2340 continue;
2341 }
5f0d2358 2342
5a566bed 2343 /* We do not need this edge. */
6fb5fa3c 2344 df_set_bb_dirty (bb);
ca6c03ca
JH
2345 purged = true;
2346 remove_edge (e);
2347 }
5f0d2358 2348
628f6a4e 2349 if (EDGE_COUNT (bb->succs) == 0 || !purged)
c51d95ec 2350 return purged;
5f0d2358 2351
c263766c
RH
2352 if (dump_file)
2353 fprintf (dump_file, "Purged edges from bb %i\n", bb->index);
5f0d2358 2354
ca6c03ca
JH
2355 if (!optimize)
2356 return purged;
2357
2358 /* Redistribute probabilities. */
c5cbcccf 2359 if (single_succ_p (bb))
ca6c03ca 2360 {
c5cbcccf
ZD
2361 single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
2362 single_succ_edge (bb)->count = bb->count;
f87c27b4 2363 }
ca6c03ca
JH
2364 else
2365 {
2366 note = find_reg_note (insn, REG_BR_PROB, NULL);
2367 if (!note)
2368 return purged;
5f0d2358 2369
ca6c03ca
JH
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 }
5f0d2358 2377
ca6c03ca
JH
2378 return purged;
2379 }
4b4bf941 2380 else if (CALL_P (insn) && SIBLING_CALL_P (insn))
1722c2c8
RH
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. */
c5cbcccf
ZD
2386 gcc_assert (single_succ_p (bb));
2387 gcc_assert (single_succ_edge (bb)->flags
2388 == (EDGE_SIBCALL | EDGE_ABNORMAL));
1722c2c8
RH
2389
2390 return 0;
2391 }
ca6c03ca 2392
ca6c03ca
JH
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. */
628f6a4e
BE
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 }
5f0d2358 2405
628f6a4e 2406 if (!found)
ca6c03ca 2407 return purged;
5f0d2358 2408
2afa8dce
DB
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. */
628f6a4e 2412 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
ca6c03ca 2413 {
2afa8dce 2414 if (!(e->flags & (EDGE_FALLTHRU | EDGE_FAKE)))
c51d95ec 2415 {
6fb5fa3c 2416 df_set_bb_dirty (bb);
c51d95ec
JH
2417 remove_edge (e);
2418 purged = true;
2419 }
628f6a4e
BE
2420 else
2421 ei_next (&ei);
ca6c03ca 2422 }
5f0d2358 2423
c5cbcccf 2424 gcc_assert (single_succ_p (bb));
5f0d2358 2425
c5cbcccf
ZD
2426 single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
2427 single_succ_edge (bb)->count = bb->count;
ca6c03ca 2428
c263766c
RH
2429 if (dump_file)
2430 fprintf (dump_file, "Purged non-fallthru edges from bb %i\n",
0b17ab2f 2431 bb->index);
ca6c03ca
JH
2432 return purged;
2433}
2434
5f0d2358
RK
2435/* Search all basic blocks for potentially dead edges and purge them. Return
2436 true if some edge has been eliminated. */
ca6c03ca
JH
2437
2438bool
25cd19de 2439purge_all_dead_edges (void)
ca6c03ca 2440{
e0082a72 2441 int purged = false;
e0082a72 2442 basic_block bb;
473fb060 2443
e0082a72 2444 FOR_EACH_BB (bb)
473fb060 2445 {
e0082a72 2446 bool purged_here = purge_dead_edges (bb);
5f0d2358 2447
473fb060 2448 purged |= purged_here;
473fb060 2449 }
5f0d2358 2450
ca6c03ca
JH
2451 return purged;
2452}
9ee634e3
JH
2453
2454/* Same as split_block but update cfg_layout structures. */
f470c378
ZD
2455
2456static basic_block
d329e058 2457cfg_layout_split_block (basic_block bb, void *insnp)
9ee634e3 2458{
ae50c0cb 2459 rtx insn = (rtx) insnp;
f470c378 2460 basic_block new_bb = rtl_split_block (bb, insn);
9ee634e3 2461
370369e1
JH
2462 new_bb->il.rtl->footer = bb->il.rtl->footer;
2463 bb->il.rtl->footer = NULL;
9ee634e3 2464
f470c378 2465 return new_bb;
9ee634e3
JH
2466}
2467
9ee634e3 2468/* Redirect Edge to DEST. */
6de9cd9a 2469static edge
d329e058 2470cfg_layout_redirect_edge_and_branch (edge e, basic_block dest)
9ee634e3
JH
2471{
2472 basic_block src = e->src;
6de9cd9a 2473 edge ret;
9ee634e3 2474
bc35512f 2475 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
6de9cd9a 2476 return NULL;
bc35512f 2477
3348b696 2478 if (e->dest == dest)
6de9cd9a 2479 return e;
bc35512f 2480
3348b696 2481 if (e->src != ENTRY_BLOCK_PTR
6de9cd9a 2482 && (ret = try_redirect_by_replacing_jump (e, dest, true)))
f345f21a 2483 {
6fb5fa3c 2484 df_set_bb_dirty (src);
6de9cd9a 2485 return ret;
f345f21a 2486 }
bc35512f
JH
2487
2488 if (e->src == ENTRY_BLOCK_PTR
2489 && (e->flags & EDGE_FALLTHRU) && !(e->flags & EDGE_COMPLEX))
2490 {
c263766c
RH
2491 if (dump_file)
2492 fprintf (dump_file, "Redirecting entry edge from bb %i to %i\n",
bc35512f
JH
2493 e->src->index, dest->index);
2494
6fb5fa3c 2495 df_set_bb_dirty (e->src);
bc35512f 2496 redirect_edge_succ (e, dest);
6de9cd9a 2497 return e;
bc35512f
JH
2498 }
2499
9ee634e3
JH
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
9ee634e3
JH
2504 if (e->flags & EDGE_FALLTHRU)
2505 {
2506 /* Redirect any branch edges unified with the fallthru one. */
4b4bf941 2507 if (JUMP_P (BB_END (src))
432f982f
JH
2508 && label_is_jump_target_p (BB_HEAD (e->dest),
2509 BB_END (src)))
9ee634e3 2510 {
341c100f 2511 edge redirected;
c22cacf3 2512
c263766c
RH
2513 if (dump_file)
2514 fprintf (dump_file, "Fallthru edge unified with branch "
432f982f
JH
2515 "%i->%i redirected to %i\n",
2516 e->src->index, e->dest->index, dest->index);
2517 e->flags &= ~EDGE_FALLTHRU;
341c100f
NS
2518 redirected = redirect_branch_edge (e, dest);
2519 gcc_assert (redirected);
432f982f 2520 e->flags |= EDGE_FALLTHRU;
6fb5fa3c 2521 df_set_bb_dirty (e->src);
6de9cd9a 2522 return e;
9ee634e3
JH
2523 }
2524 /* In case we are redirecting fallthru edge to the branch edge
c22cacf3 2525 of conditional jump, remove it. */
628f6a4e 2526 if (EDGE_COUNT (src->succs) == 2)
9ee634e3 2527 {
03101c6f
KH
2528 /* Find the edge that is different from E. */
2529 edge s = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e);
628f6a4e 2530
9ee634e3 2531 if (s->dest == dest
a813c111
SB
2532 && any_condjump_p (BB_END (src))
2533 && onlyjump_p (BB_END (src)))
2534 delete_insn (BB_END (src));
9ee634e3 2535 }
6de9cd9a 2536 ret = redirect_edge_succ_nodup (e, dest);
c263766c
RH
2537 if (dump_file)
2538 fprintf (dump_file, "Fallthru edge %i->%i redirected to %i\n",
bc35512f 2539 e->src->index, e->dest->index, dest->index);
9ee634e3
JH
2540 }
2541 else
bc35512f 2542 ret = redirect_branch_edge (e, dest);
9ee634e3
JH
2543
2544 /* We don't want simplejumps in the insn stream during cfglayout. */
341c100f 2545 gcc_assert (!simplejump_p (BB_END (src)));
9ee634e3 2546
6fb5fa3c 2547 df_set_bb_dirty (src);
9ee634e3
JH
2548 return ret;
2549}
2550
2551/* Simple wrapper as we always can redirect fallthru edges. */
2552static basic_block
d329e058 2553cfg_layout_redirect_edge_and_branch_force (edge e, basic_block dest)
9ee634e3 2554{
341c100f
NS
2555 edge redirected = cfg_layout_redirect_edge_and_branch (e, dest);
2556
2557 gcc_assert (redirected);
9ee634e3
JH
2558 return NULL;
2559}
2560
f470c378
ZD
2561/* Same as delete_basic_block but update cfg_layout structures. */
2562
9ee634e3 2563static void
d329e058 2564cfg_layout_delete_block (basic_block bb)
9ee634e3 2565{
a813c111 2566 rtx insn, next, prev = PREV_INSN (BB_HEAD (bb)), *to, remaints;
9ee634e3 2567
370369e1 2568 if (bb->il.rtl->header)
9ee634e3 2569 {
a813c111 2570 next = BB_HEAD (bb);
9ee634e3 2571 if (prev)
370369e1 2572 NEXT_INSN (prev) = bb->il.rtl->header;
9ee634e3 2573 else
370369e1
JH
2574 set_first_insn (bb->il.rtl->header);
2575 PREV_INSN (bb->il.rtl->header) = prev;
2576 insn = bb->il.rtl->header;
9ee634e3
JH
2577 while (NEXT_INSN (insn))
2578 insn = NEXT_INSN (insn);
2579 NEXT_INSN (insn) = next;
2580 PREV_INSN (next) = insn;
2581 }
a813c111 2582 next = NEXT_INSN (BB_END (bb));
370369e1 2583 if (bb->il.rtl->footer)
9ee634e3 2584 {
370369e1 2585 insn = bb->il.rtl->footer;
bc35512f
JH
2586 while (insn)
2587 {
4b4bf941 2588 if (BARRIER_P (insn))
bc35512f
JH
2589 {
2590 if (PREV_INSN (insn))
2591 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2592 else
370369e1 2593 bb->il.rtl->footer = NEXT_INSN (insn);
bc35512f
JH
2594 if (NEXT_INSN (insn))
2595 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2596 }
4b4bf941 2597 if (LABEL_P (insn))
bc35512f
JH
2598 break;
2599 insn = NEXT_INSN (insn);
2600 }
370369e1 2601 if (bb->il.rtl->footer)
bc35512f 2602 {
a813c111 2603 insn = BB_END (bb);
370369e1
JH
2604 NEXT_INSN (insn) = bb->il.rtl->footer;
2605 PREV_INSN (bb->il.rtl->footer) = insn;
bc35512f
JH
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 }
9ee634e3
JH
2614 }
2615 if (bb->next_bb != EXIT_BLOCK_PTR)
370369e1 2616 to = &bb->next_bb->il.rtl->header;
9ee634e3
JH
2617 else
2618 to = &cfg_layout_function_footer;
997de8ed 2619
9ee634e3
JH
2620 rtl_delete_block (bb);
2621
2622 if (prev)
2623 prev = NEXT_INSN (prev);
d329e058 2624 else
9ee634e3
JH
2625 prev = get_insns ();
2626 if (next)
2627 next = PREV_INSN (next);
d329e058 2628 else
9ee634e3
JH
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
beb235f8 2644/* Return true when blocks A and B can be safely merged. */
b48d0358 2645
bc35512f 2646static bool
b48d0358 2647cfg_layout_can_merge_blocks_p (basic_block a, basic_block b)
bc35512f 2648{
750054a2
CT
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
076c7ab8
ZW
2651 and cold sections.
2652
8e8d5162 2653 Basic block partitioning may result in some jumps that appear to
c22cacf3
MS
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
8e8d5162
CT
2657 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
2658
87c8b4be 2659 if (BB_PARTITION (a) != BB_PARTITION (b))
076c7ab8 2660 return false;
750054a2 2661
bc35512f 2662 /* There must be exactly one edge in between the blocks. */
c5cbcccf
ZD
2663 return (single_succ_p (a)
2664 && single_succ (a) == b
2665 && single_pred_p (b) == 1
628f6a4e 2666 && a != b
bc35512f 2667 /* Must be simple edge. */
c5cbcccf 2668 && !(single_succ_edge (a)->flags & EDGE_COMPLEX)
bc35512f 2669 && a != ENTRY_BLOCK_PTR && b != EXIT_BLOCK_PTR
49ea3702
SB
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. */
4b4bf941 2673 && (!JUMP_P (BB_END (a))
49ea3702 2674 || ((!optimize || reload_completed)
a813c111 2675 ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
bc35512f
JH
2676}
2677
41806d92
NS
2678/* Merge block A and B. The blocks must be mergeable. */
2679
bc35512f
JH
2680static void
2681cfg_layout_merge_blocks (basic_block a, basic_block b)
2682{
2683#ifdef ENABLE_CHECKING
341c100f 2684 gcc_assert (cfg_layout_can_merge_blocks_p (a, b));
bc35512f
JH
2685#endif
2686
6fb5fa3c
DB
2687 if (dump_file)
2688 fprintf (dump_file, "merging block %d into block %d\n", b->index, a->index);
2689
bc35512f 2690 /* If there was a CODE_LABEL beginning B, delete it. */
4b4bf941 2691 if (LABEL_P (BB_HEAD (b)))
2c97f8e4 2692 {
2c97f8e4
RH
2693 delete_insn (BB_HEAD (b));
2694 }
bc35512f
JH
2695
2696 /* We should have fallthru edge in a, or we can do dummy redirection to get
2697 it cleaned up. */
4b4bf941 2698 if (JUMP_P (BB_END (a)))
628f6a4e 2699 try_redirect_by_replacing_jump (EDGE_SUCC (a, 0), b, true);
341c100f 2700 gcc_assert (!JUMP_P (BB_END (a)));
bc35512f 2701
7241571e
JJ
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 {
1692ae7e 2706 rtx insn = BB_END (a), end = PREV_INSN (BB_HEAD (a));
7241571e
JJ
2707 int goto_locus = EDGE_SUCC (a, 0)->goto_locus;
2708
1692ae7e
JJ
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))
7241571e
JJ
2712 goto_locus = 0;
2713 else
2714 {
2715 insn = BB_HEAD (b);
1692ae7e
JJ
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))
7241571e
JJ
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
bc35512f 2730 /* Possible line number notes should appear in between. */
370369e1 2731 if (b->il.rtl->header)
bc35512f 2732 {
a813c111 2733 rtx first = BB_END (a), last;
bc35512f 2734
6fb5fa3c 2735 last = emit_insn_after_noloc (b->il.rtl->header, BB_END (a), a);
a7b87f73 2736 delete_insn_chain (NEXT_INSN (first), last, false);
370369e1 2737 b->il.rtl->header = NULL;
bc35512f
JH
2738 }
2739
2740 /* In the case basic blocks are not adjacent, move them around. */
a813c111 2741 if (NEXT_INSN (BB_END (a)) != BB_HEAD (b))
bc35512f 2742 {
a813c111 2743 rtx first = unlink_insn_chain (BB_HEAD (b), BB_END (b));
bc35512f 2744
6fb5fa3c 2745 emit_insn_after_noloc (first, BB_END (a), a);
bc35512f
JH
2746 /* Skip possible DELETED_LABEL insn. */
2747 if (!NOTE_INSN_BASIC_BLOCK_P (first))
2748 first = NEXT_INSN (first);
341c100f 2749 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (first));
a813c111 2750 BB_HEAD (b) = NULL;
6c3d0e31
SP
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
bc35512f
JH
2758 delete_insn (first);
2759 }
2760 /* Otherwise just re-associate the instructions. */
2761 else
2762 {
2763 rtx insn;
2764
6c3d0e31 2765 update_bb_for_insn_chain (BB_HEAD (b), BB_END (b), a);
6fb5fa3c 2766
a813c111 2767 insn = BB_HEAD (b);
bc35512f
JH
2768 /* Skip possible DELETED_LABEL insn. */
2769 if (!NOTE_INSN_BASIC_BLOCK_P (insn))
2770 insn = NEXT_INSN (insn);
341c100f 2771 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
a813c111
SB
2772 BB_HEAD (b) = NULL;
2773 BB_END (a) = BB_END (b);
bc35512f
JH
2774 delete_insn (insn);
2775 }
2776
6fb5fa3c
DB
2777 df_bb_delete (b->index);
2778
bc35512f 2779 /* Possible tablejumps and barriers should appear after the block. */
370369e1 2780 if (b->il.rtl->footer)
bc35512f 2781 {
370369e1
JH
2782 if (!a->il.rtl->footer)
2783 a->il.rtl->footer = b->il.rtl->footer;
bc35512f
JH
2784 else
2785 {
370369e1 2786 rtx last = a->il.rtl->footer;
bc35512f
JH
2787
2788 while (NEXT_INSN (last))
2789 last = NEXT_INSN (last);
370369e1
JH
2790 NEXT_INSN (last) = b->il.rtl->footer;
2791 PREV_INSN (b->il.rtl->footer) = last;
bc35512f 2792 }
370369e1 2793 b->il.rtl->footer = NULL;
bc35512f
JH
2794 }
2795
c263766c
RH
2796 if (dump_file)
2797 fprintf (dump_file, "Merged blocks %d and %d.\n",
bc35512f 2798 a->index, b->index);
bc35512f
JH
2799}
2800
2801/* Split edge E. */
f470c378 2802
bc35512f
JH
2803static basic_block
2804cfg_layout_split_edge (edge e)
2805{
bc35512f
JH
2806 basic_block new_bb =
2807 create_basic_block (e->src != ENTRY_BLOCK_PTR
a813c111 2808 ? NEXT_INSN (BB_END (e->src)) : get_insns (),
bc35512f
JH
2809 NULL_RTX, e->src);
2810
3e7eb734
JJ
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);
a9b2ee88 2815 make_edge (new_bb, e->dest, EDGE_FALLTHRU);
bc35512f
JH
2816 redirect_edge_and_branch_force (e, new_bb);
2817
2818 return new_bb;
2819}
2820
f470c378
ZD
2821/* Do postprocessing after making a forwarder block joined by edge FALLTHRU. */
2822
2823static void
2824rtl_make_forwarder_block (edge fallthru ATTRIBUTE_UNUSED)
2825{
2826}
2827
6de9cd9a
DN
2828/* Return 1 if BB ends with a call, possibly followed by some
2829 instructions that must stay with the call, 0 otherwise. */
2830
2831static bool
b48d0358 2832rtl_block_ends_with_call_p (basic_block bb)
6de9cd9a
DN
2833{
2834 rtx insn = BB_END (bb);
2835
4b4bf941 2836 while (!CALL_P (insn)
6de9cd9a 2837 && insn != BB_HEAD (bb)
92ddef69 2838 && (keep_with_call_p (insn)
b5b8b0ac
AO
2839 || NOTE_P (insn)
2840 || DEBUG_INSN_P (insn)))
6de9cd9a 2841 insn = PREV_INSN (insn);
4b4bf941 2842 return (CALL_P (insn));
6de9cd9a
DN
2843}
2844
2845/* Return 1 if BB ends with a conditional branch, 0 otherwise. */
2846
2847static bool
9678086d 2848rtl_block_ends_with_condjump_p (const_basic_block bb)
6de9cd9a
DN
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
2856static bool
9678086d 2857need_fake_edge_p (const_rtx insn)
6de9cd9a
DN
2858{
2859 if (!INSN_P (insn))
2860 return false;
2861
4b4bf941 2862 if ((CALL_P (insn)
6de9cd9a
DN
2863 && !SIBLING_CALL_P (insn)
2864 && !find_reg_note (insn, REG_NORETURN, NULL)
becfd6e5 2865 && !(RTL_CONST_OR_PURE_CALL_P (insn))))
6de9cd9a
DN
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
2884static int
2885rtl_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
24bd1a0b 2892 if (n_basic_blocks == NUM_FIXED_BLOCKS)
6de9cd9a
DN
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
9ff3d2de
JL
2926 e = find_edge (bb, EXIT_BLOCK_PTR);
2927 if (e)
2928 {
c41c1387 2929 insert_insn_on_edge (gen_use (const0_rtx), e);
9ff3d2de
JL
2930 commit_edge_insertions ();
2931 }
6de9cd9a
DN
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
24bd1a0b 2939 for (i = NUM_FIXED_BLOCKS; i < last_bb; i++)
6de9cd9a
DN
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
c22cacf3 2960 remain in the same block as the call. */
4b4bf941 2961 if (CALL_P (insn))
6de9cd9a
DN
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
c22cacf3 2967 should be enough to verify that there is no edge to the exit
6de9cd9a
DN
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))
628f6a4e 2973 {
9ff3d2de
JL
2974 e = find_edge (bb, EXIT_BLOCK_PTR);
2975 gcc_assert (e == NULL);
628f6a4e 2976 }
6de9cd9a
DN
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
1cb7dfc3 3002/* Add COMP_RTX as a condition at end of COND_BB. FIRST_HEAD is
315682fb 3003 the conditional branch target, SECOND_HEAD should be the fall-thru
1cb7dfc3
MH
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. */
3007static void
3008rtl_lv_add_condition_to_bb (basic_block first_head ,
c22cacf3
MS
3009 basic_block second_head ATTRIBUTE_UNUSED,
3010 basic_block cond_bb, void *comp_rtx)
1cb7dfc3
MH
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. */
3043static void
3044rtl_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
5e2d947c
JH
3061void
3062init_rtl_bb_info (basic_block bb)
3063{
3064 gcc_assert (!bb->il.rtl);
ae50c0cb 3065 bb->il.rtl = GGC_CNEW (struct rtl_bb_info);
5e2d947c
JH
3066}
3067
1cb7dfc3 3068
8cd37d0b
RL
3069/* Add EXPR to the end of basic block BB. */
3070
3071rtx
3072insert_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? */
6fb5fa3c 3117 new_insn = emit_insn_before_noloc (pat, insn, bb);
8cd37d0b
RL
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
6fb5fa3c 3148 new_insn = emit_insn_before_noloc (pat, insn, bb);
8cd37d0b
RL
3149 }
3150 else
6fb5fa3c 3151 new_insn = emit_insn_after_noloc (pat, insn, bb);
8cd37d0b
RL
3152
3153 return new_insn;
3154}
3155
14fa2cc0
ZD
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
3159static bool
9678086d 3160rtl_can_remove_branch_p (const_edge e)
14fa2cc0 3161{
9678086d
KG
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;
14fa2cc0
ZD
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
9ee634e3
JH
3188/* Implementation of CFG manipulation for linearized RTL. */
3189struct cfg_hooks rtl_cfg_hooks = {
f470c378 3190 "rtl",
9ee634e3 3191 rtl_verify_flow_info,
10e9fecc 3192 rtl_dump_bb,
bc35512f 3193 rtl_create_basic_block,
9ee634e3
JH
3194 rtl_redirect_edge_and_branch,
3195 rtl_redirect_edge_and_branch_force,
14fa2cc0 3196 rtl_can_remove_branch_p,
9ee634e3
JH
3197 rtl_delete_block,
3198 rtl_split_block,
f470c378 3199 rtl_move_block_after,
bc35512f
JH
3200 rtl_can_merge_blocks, /* can_merge_blocks_p */
3201 rtl_merge_blocks,
6de9cd9a
DN
3202 rtl_predict_edge,
3203 rtl_predicted_by_p,
3204 NULL, /* can_duplicate_block_p */
3205 NULL, /* duplicate_block */
f470c378
ZD
3206 rtl_split_edge,
3207 rtl_make_forwarder_block,
6de9cd9a
DN
3208 rtl_tidy_fallthru_edge,
3209 rtl_block_ends_with_call_p,
3210 rtl_block_ends_with_condjump_p,
d9d4706f
KH
3211 rtl_flow_call_edges_add,
3212 NULL, /* execute_on_growing_pred */
1cb7dfc3
MH
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 */
c22cacf3 3218 NULL /* flush_pending_stmts */
9ee634e3
JH
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. */
6de9cd9a
DN
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
6fb5fa3c 3229 code. They are in cfglayout.c. */
9678086d 3230extern bool cfg_layout_can_duplicate_bb_p (const_basic_block);
6de9cd9a
DN
3231extern basic_block cfg_layout_duplicate_bb (basic_block);
3232
9ee634e3 3233struct cfg_hooks cfg_layout_rtl_cfg_hooks = {
f470c378 3234 "cfglayout mode",
bc35512f 3235 rtl_verify_flow_info_1,
10e9fecc 3236 rtl_dump_bb,
bc35512f 3237 cfg_layout_create_basic_block,
9ee634e3
JH
3238 cfg_layout_redirect_edge_and_branch,
3239 cfg_layout_redirect_edge_and_branch_force,
14fa2cc0 3240 rtl_can_remove_branch_p,
9ee634e3
JH
3241 cfg_layout_delete_block,
3242 cfg_layout_split_block,
f470c378 3243 rtl_move_block_after,
bc35512f
JH
3244 cfg_layout_can_merge_blocks_p,
3245 cfg_layout_merge_blocks,
6de9cd9a
DN
3246 rtl_predict_edge,
3247 rtl_predicted_by_p,
3248 cfg_layout_can_duplicate_bb_p,
3249 cfg_layout_duplicate_bb,
f470c378
ZD
3250 cfg_layout_split_edge,
3251 rtl_make_forwarder_block,
6de9cd9a
DN
3252 NULL,
3253 rtl_block_ends_with_call_p,
3254 rtl_block_ends_with_condjump_p,
d9d4706f
KH
3255 rtl_flow_call_edges_add,
3256 NULL, /* execute_on_growing_pred */
1cb7dfc3
MH
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 */
c22cacf3 3262 NULL /* flush_pending_stmts */
9ee634e3 3263};