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