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