]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cfgrtl.c
make_insn_raw returns an rtx_insn
[thirdparty/gcc.git] / gcc / cfgrtl.c
CommitLineData
ca6c03ca 1/* Control flow graph manipulation code for GNU compiler.
23a5b65a 2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
ca6c03ca
JH
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
ca6c03ca
JH
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
ca6c03ca 19
5f0d2358
RK
20/* This file contains low level functions to manipulate the CFG and analyze it
21 that are aware of the RTL intermediate language.
ca6c03ca
JH
22
23 Available functionality:
bc35512f 24 - Basic CFG/RTL manipulation API documented in cfghooks.h
5f0d2358 25 - CFG-aware instruction chain manipulation
ca6c03ca 26 delete_insn, delete_insn_chain
bc35512f
JH
27 - Edge splitting and committing to edges
28 insert_insn_on_edge, commit_edge_insertions
29 - CFG updating after insn simplification
30 purge_dead_edges, purge_all_dead_edges
ba5e9aca
EB
31 - CFG fixing after coarse manipulation
32 fixup_abnormal_edges
bc35512f
JH
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 44#include "tree.h"
ca6c03ca
JH
45#include "hard-reg-set.h"
46#include "basic-block.h"
3371a64f 47#include "bb-reorder.h"
ca6c03ca
JH
48#include "regs.h"
49#include "flags.h"
ca6c03ca
JH
50#include "function.h"
51#include "except.h"
0cbd9993 52#include "rtl-error.h"
ca6c03ca
JH
53#include "tm_p.h"
54#include "obstack.h"
2a34bece 55#include "insn-attr.h"
0a2ed1f1 56#include "insn-config.h"
ff25ef99 57#include "expr.h"
9fb32434 58#include "target.h"
677f3fa8 59#include "common/common-target.h"
1cb7dfc3 60#include "cfgloop.h"
5e2d947c 61#include "ggc.h"
ef330312 62#include "tree-pass.h"
6fb5fa3c 63#include "df.h"
ca6c03ca 64
78bde837
SB
65/* Holds the interesting leading and trailing notes for the function.
66 Only applicable if the CFG is in cfglayout mode. */
67static GTY(()) rtx cfg_layout_function_footer;
68static GTY(()) rtx cfg_layout_function_header;
69
70static rtx skip_insns_after_block (basic_block);
71static void record_effective_endpoints (void);
72static rtx label_for_bb (basic_block);
73static void fixup_reorder_chain (void);
74
75void verify_insn_chain (void);
76static void fixup_fallthru_exit_predecessor (void);
9678086d
KG
77static int can_delete_note_p (const_rtx);
78static int can_delete_label_p (const_rtx);
d329e058 79static basic_block rtl_split_edge (edge);
f470c378 80static bool rtl_move_block_after (basic_block, basic_block);
d329e058 81static int rtl_verify_flow_info (void);
f470c378 82static basic_block cfg_layout_split_block (basic_block, void *);
6de9cd9a 83static edge cfg_layout_redirect_edge_and_branch (edge, basic_block);
d329e058
AJ
84static basic_block cfg_layout_redirect_edge_and_branch_force (edge, basic_block);
85static void cfg_layout_delete_block (basic_block);
86static void rtl_delete_block (basic_block);
87static basic_block rtl_redirect_edge_and_branch_force (edge, basic_block);
6de9cd9a 88static edge rtl_redirect_edge_and_branch (edge, basic_block);
f470c378 89static basic_block rtl_split_block (basic_block, void *);
a315c44c 90static void rtl_dump_bb (FILE *, basic_block, int, int);
d329e058 91static int rtl_verify_flow_info_1 (void);
f470c378 92static void rtl_make_forwarder_block (edge);
ca6c03ca
JH
93\f
94/* Return true if NOTE is not one of the ones that must be kept paired,
5f0d2358 95 so that we may simply delete it. */
ca6c03ca
JH
96
97static int
9678086d 98can_delete_note_p (const_rtx note)
ca6c03ca 99{
cd9c1ca8
RH
100 switch (NOTE_KIND (note))
101 {
102 case NOTE_INSN_DELETED:
103 case NOTE_INSN_BASIC_BLOCK:
104 case NOTE_INSN_EPILOGUE_BEG:
105 return true;
106
107 default:
108 return false;
109 }
ca6c03ca
JH
110}
111
112/* True if a given label can be deleted. */
113
114static int
9678086d 115can_delete_label_p (const_rtx label)
ca6c03ca 116{
5f0d2358
RK
117 return (!LABEL_PRESERVE_P (label)
118 /* User declared labels must be preserved. */
119 && LABEL_NAME (label) == 0
610d2478 120 && !in_expr_list_p (forced_labels, label));
ca6c03ca
JH
121}
122
03fbe718 123/* Delete INSN by patching it out. */
ca6c03ca 124
03fbe718 125void
d329e058 126delete_insn (rtx insn)
ca6c03ca 127{
ca6c03ca
JH
128 rtx note;
129 bool really_delete = true;
130
4b4bf941 131 if (LABEL_P (insn))
ca6c03ca
JH
132 {
133 /* Some labels can't be directly removed from the INSN chain, as they
c22cacf3
MS
134 might be references via variables, constant pool etc.
135 Convert them to the special NOTE_INSN_DELETED_LABEL note. */
ca6c03ca
JH
136 if (! can_delete_label_p (insn))
137 {
138 const char *name = LABEL_NAME (insn);
5e5df392 139 basic_block bb = BLOCK_FOR_INSN (insn);
03fbe718 140 rtx bb_note = NEXT_INSN (insn);
ca6c03ca
JH
141
142 really_delete = false;
143 PUT_CODE (insn, NOTE);
a38e7aa5 144 NOTE_KIND (insn) = NOTE_INSN_DELETED_LABEL;
6773e15f 145 NOTE_DELETED_LABEL_NAME (insn) = name;
03fbe718 146
0f33baa9 147 /* If the note following the label starts a basic block, and the
5e5df392 148 label is a member of the same basic block, interchange the two. */
0f33baa9
TV
149 if (bb_note != NULL_RTX
150 && NOTE_INSN_BASIC_BLOCK_P (bb_note)
5e5df392
TV
151 && bb != NULL
152 && bb == BLOCK_FOR_INSN (bb_note))
03fbe718
TV
153 {
154 reorder_insns_nobb (insn, insn, bb_note);
190bea87 155 SET_BB_HEAD (bb) = bb_note;
03fbe718 156 if (BB_END (bb) == bb_note)
190bea87 157 SET_BB_END (bb) = insn;
03fbe718 158 }
ca6c03ca 159 }
5f0d2358 160
ca6c03ca
JH
161 remove_node_from_expr_list (insn, &nonlocal_goto_handler_labels);
162 }
163
164 if (really_delete)
165 {
cda94cbb 166 /* If this insn has already been deleted, something is very wrong. */
341c100f 167 gcc_assert (!INSN_DELETED_P (insn));
1f397f45
SB
168 if (INSN_P (insn))
169 df_insn_delete (insn);
ca6c03ca
JH
170 remove_insn (insn);
171 INSN_DELETED_P (insn) = 1;
172 }
173
174 /* If deleting a jump, decrement the use count of the label. Deleting
175 the label itself should happen in the normal course of block merging. */
cf7c4aa6 176 if (JUMP_P (insn))
9295a326 177 {
cf7c4aa6
HPN
178 if (JUMP_LABEL (insn)
179 && LABEL_P (JUMP_LABEL (insn)))
180 LABEL_NUSES (JUMP_LABEL (insn))--;
181
182 /* If there are more targets, remove them too. */
183 while ((note
184 = find_reg_note (insn, REG_LABEL_TARGET, NULL_RTX)) != NULL_RTX
4b4bf941 185 && LABEL_P (XEXP (note, 0)))
9295a326
JZ
186 {
187 LABEL_NUSES (XEXP (note, 0))--;
188 remove_note (insn, note);
189 }
190 }
ca6c03ca 191
cf7c4aa6
HPN
192 /* Also if deleting any insn that references a label as an operand. */
193 while ((note = find_reg_note (insn, REG_LABEL_OPERAND, NULL_RTX)) != NULL_RTX
194 && LABEL_P (XEXP (note, 0)))
195 {
196 LABEL_NUSES (XEXP (note, 0))--;
197 remove_note (insn, note);
198 }
199
481683e1 200 if (JUMP_TABLE_DATA_P (insn))
ca6c03ca
JH
201 {
202 rtx pat = PATTERN (insn);
203 int diff_vec_p = GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC;
204 int len = XVECLEN (pat, diff_vec_p);
205 int i;
206
207 for (i = 0; i < len; i++)
a124fcda
RH
208 {
209 rtx label = XEXP (XVECEXP (pat, diff_vec_p, i), 0);
210
211 /* When deleting code in bulk (e.g. removing many unreachable
212 blocks) we can delete a label that's a target of the vector
213 before deleting the vector itself. */
4b4bf941 214 if (!NOTE_P (label))
a124fcda
RH
215 LABEL_NUSES (label)--;
216 }
ca6c03ca 217 }
ca6c03ca
JH
218}
219
3dec4024 220/* Like delete_insn but also purge dead edges from BB. */
9ed7b221 221
03fbe718 222void
d329e058 223delete_insn_and_edges (rtx insn)
3dec4024 224{
3dec4024
JH
225 bool purge = false;
226
ba4f7968 227 if (INSN_P (insn)
3dec4024 228 && BLOCK_FOR_INSN (insn)
a813c111 229 && BB_END (BLOCK_FOR_INSN (insn)) == insn)
3dec4024 230 purge = true;
03fbe718 231 delete_insn (insn);
3dec4024
JH
232 if (purge)
233 purge_dead_edges (BLOCK_FOR_INSN (insn));
3dec4024
JH
234}
235
ca6c03ca 236/* Unlink a chain of insns between START and FINISH, leaving notes
a7b87f73
ZD
237 that must be paired. If CLEAR_BB is true, we set bb field for
238 insns that cannot be removed to NULL. */
ca6c03ca
JH
239
240void
a7b87f73 241delete_insn_chain (rtx start, rtx finish, bool clear_bb)
ca6c03ca 242{
03fbe718 243 rtx prev, current;
ca6c03ca 244
5f0d2358
RK
245 /* Unchain the insns one by one. It would be quicker to delete all of these
246 with a single unchaining, rather than one at a time, but we need to keep
247 the NOTE's. */
03fbe718 248 current = finish;
ca6c03ca
JH
249 while (1)
250 {
03fbe718
TV
251 prev = PREV_INSN (current);
252 if (NOTE_P (current) && !can_delete_note_p (current))
ca6c03ca
JH
253 ;
254 else
03fbe718 255 delete_insn (current);
ca6c03ca 256
03fbe718
TV
257 if (clear_bb && !INSN_DELETED_P (current))
258 set_block_for_insn (current, NULL);
a7b87f73 259
03fbe718 260 if (current == start)
ca6c03ca 261 break;
03fbe718 262 current = prev;
ca6c03ca
JH
263 }
264}
265\f
5f0d2358
RK
266/* Create a new basic block consisting of the instructions between HEAD and END
267 inclusive. This function is designed to allow fast BB construction - reuses
268 the note and basic block struct in BB_NOTE, if any and do not grow
269 BASIC_BLOCK chain and should be used directly only by CFG construction code.
270 END can be NULL in to create new empty basic block before HEAD. Both END
918ed612
ZD
271 and HEAD can be NULL to create basic block at the end of INSN chain.
272 AFTER is the basic block we should be put after. */
ca6c03ca
JH
273
274basic_block
d329e058 275create_basic_block_structure (rtx head, rtx end, rtx bb_note, basic_block after)
ca6c03ca
JH
276{
277 basic_block bb;
278
279 if (bb_note
ca6c03ca
JH
280 && (bb = NOTE_BASIC_BLOCK (bb_note)) != NULL
281 && bb->aux == NULL)
282 {
283 /* If we found an existing note, thread it back onto the chain. */
284
285 rtx after;
286
4b4bf941 287 if (LABEL_P (head))
ca6c03ca
JH
288 after = head;
289 else
290 {
291 after = PREV_INSN (head);
292 head = bb_note;
293 }
294
295 if (after != bb_note && NEXT_INSN (after) != bb_note)
ba4f7968 296 reorder_insns_nobb (bb_note, bb_note, after);
ca6c03ca
JH
297 }
298 else
299 {
300 /* Otherwise we must create a note and a basic block structure. */
301
302 bb = alloc_block ();
303
5e2d947c 304 init_rtl_bb_info (bb);
ca6c03ca 305 if (!head && !end)
5f0d2358
RK
306 head = end = bb_note
307 = emit_note_after (NOTE_INSN_BASIC_BLOCK, get_last_insn ());
4b4bf941 308 else if (LABEL_P (head) && end)
ca6c03ca
JH
309 {
310 bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, head);
311 if (head == end)
312 end = bb_note;
313 }
314 else
315 {
316 bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, head);
317 head = bb_note;
318 if (!end)
319 end = head;
320 }
5f0d2358 321
ca6c03ca
JH
322 NOTE_BASIC_BLOCK (bb_note) = bb;
323 }
324
325 /* Always include the bb note in the block. */
326 if (NEXT_INSN (end) == bb_note)
327 end = bb_note;
328
190bea87
DM
329 SET_BB_HEAD (bb) = head;
330 SET_BB_END (bb) = end;
8b1c6fd7 331 bb->index = last_basic_block_for_fn (cfun)++;
5e2d947c 332 bb->flags = BB_NEW | BB_RTL;
918ed612 333 link_block (bb, after);
557c4b49 334 SET_BASIC_BLOCK_FOR_FN (cfun, bb->index, bb);
6fb5fa3c 335 df_bb_refs_record (bb->index, false);
ba4f7968 336 update_bb_for_insn (bb);
076c7ab8 337 BB_SET_PARTITION (bb, BB_UNPARTITIONED);
ca6c03ca
JH
338
339 /* Tag the block so that we know it has been used when considering
340 other basic block notes. */
341 bb->aux = bb;
342
343 return bb;
344}
345
5f0d2358 346/* Create new basic block consisting of instructions in between HEAD and END
ffe14686
AM
347 and place it to the BB chain after block AFTER. END can be NULL to
348 create a new empty basic block before HEAD. Both END and HEAD can be
349 NULL to create basic block at the end of INSN chain. */
ca6c03ca 350
bc35512f
JH
351static basic_block
352rtl_create_basic_block (void *headp, void *endp, basic_block after)
ca6c03ca 353{
ae50c0cb 354 rtx head = (rtx) headp, end = (rtx) endp;
ca6c03ca 355 basic_block bb;
0b17ab2f 356
7eca0767 357 /* Grow the basic block array if needed. */
8b1c6fd7
DM
358 if ((size_t) last_basic_block_for_fn (cfun)
359 >= basic_block_info_for_fn (cfun)->length ())
7eca0767 360 {
8b1c6fd7
DM
361 size_t new_size =
362 (last_basic_block_for_fn (cfun)
363 + (last_basic_block_for_fn (cfun) + 3) / 4);
c5f05f68 364 vec_safe_grow_cleared (basic_block_info_for_fn (cfun), new_size);
7eca0767 365 }
0b17ab2f 366
0cae8d31 367 n_basic_blocks_for_fn (cfun)++;
ca6c03ca 368
852c6ec7 369 bb = create_basic_block_structure (head, end, NULL, after);
ca6c03ca
JH
370 bb->aux = NULL;
371 return bb;
372}
bc35512f
JH
373
374static basic_block
375cfg_layout_create_basic_block (void *head, void *end, basic_block after)
376{
377 basic_block newbb = rtl_create_basic_block (head, end, after);
378
bc35512f
JH
379 return newbb;
380}
ca6c03ca
JH
381\f
382/* Delete the insns in a (non-live) block. We physically delete every
383 non-deleted-note insn, and update the flow graph appropriately.
384
385 Return nonzero if we deleted an exception handler. */
386
387/* ??? Preserving all such notes strikes me as wrong. It would be nice
388 to post-process the stream to remove empty blocks, loops, ranges, etc. */
389
f0fda11c 390static void
d329e058 391rtl_delete_block (basic_block b)
ca6c03ca 392{
96370780 393 rtx insn, end;
ca6c03ca
JH
394
395 /* If the head of this block is a CODE_LABEL, then it might be the
f39e46ba
SB
396 label for an exception handler which can't be reached. We need
397 to remove the label from the exception_handler_label list. */
a813c111 398 insn = BB_HEAD (b);
ca6c03ca 399
96370780 400 end = get_last_bb_insn (b);
ca6c03ca
JH
401
402 /* Selectively delete the entire chain. */
190bea87 403 SET_BB_HEAD (b) = NULL;
a7b87f73
ZD
404 delete_insn_chain (insn, end, true);
405
6fb5fa3c
DB
406
407 if (dump_file)
408 fprintf (dump_file, "deleting block %d\n", b->index);
409 df_bb_delete (b->index);
ca6c03ca
JH
410}
411\f
852c6ec7 412/* Records the basic block struct in BLOCK_FOR_INSN for every insn. */
ca6c03ca
JH
413
414void
d329e058 415compute_bb_for_insn (void)
ca6c03ca 416{
e0082a72 417 basic_block bb;
ca6c03ca 418
11cd3bed 419 FOR_EACH_BB_FN (bb, cfun)
ca6c03ca 420 {
a813c111 421 rtx end = BB_END (bb);
5f0d2358 422 rtx insn;
ca6c03ca 423
a813c111 424 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
ca6c03ca 425 {
ba4f7968 426 BLOCK_FOR_INSN (insn) = bb;
ca6c03ca
JH
427 if (insn == end)
428 break;
ca6c03ca
JH
429 }
430 }
431}
432
433/* Release the basic_block_for_insn array. */
434
c2924966 435unsigned int
d329e058 436free_bb_for_insn (void)
ca6c03ca 437{
ba4f7968
JH
438 rtx insn;
439 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4b4bf941 440 if (!BARRIER_P (insn))
ba4f7968 441 BLOCK_FOR_INSN (insn) = NULL;
c2924966 442 return 0;
ca6c03ca
JH
443}
444
27a4cd48
DM
445namespace {
446
447const pass_data pass_data_free_cfg =
448{
449 RTL_PASS, /* type */
450 "*free_cfg", /* name */
451 OPTGROUP_NONE, /* optinfo_flags */
27a4cd48
DM
452 TV_NONE, /* tv_id */
453 0, /* properties_required */
454 0, /* properties_provided */
455 PROP_cfg, /* properties_destroyed */
456 0, /* todo_flags_start */
457 0, /* todo_flags_finish */
ef330312
PB
458};
459
27a4cd48
DM
460class pass_free_cfg : public rtl_opt_pass
461{
462public:
c3284718
RS
463 pass_free_cfg (gcc::context *ctxt)
464 : rtl_opt_pass (pass_data_free_cfg, ctxt)
27a4cd48
DM
465 {}
466
467 /* opt_pass methods: */
be55bfe6 468 virtual unsigned int execute (function *);
27a4cd48
DM
469
470}; // class pass_free_cfg
471
be55bfe6
TS
472unsigned int
473pass_free_cfg::execute (function *)
474{
475#ifdef DELAY_SLOTS
476 /* The resource.c machinery uses DF but the CFG isn't guaranteed to be
477 valid at that point so it would be too late to call df_analyze. */
478 if (optimize > 0 && flag_delayed_branch)
479 {
480 df_note_add_problem ();
481 df_analyze ();
482 }
483#endif
484
485 if (crtl->has_bb_partition)
486 insert_section_boundary_note ();
487
488 free_bb_for_insn ();
489 return 0;
490}
491
27a4cd48
DM
492} // anon namespace
493
494rtl_opt_pass *
495make_pass_free_cfg (gcc::context *ctxt)
496{
497 return new pass_free_cfg (ctxt);
498}
499
91278841 500/* Return RTX to emit after when we want to emit code on the entry of function. */
8a2e8325 501rtx_insn *
91278841
AP
502entry_of_function (void)
503{
0cae8d31 504 return (n_basic_blocks_for_fn (cfun) > NUM_FIXED_BLOCKS ?
fefa31b5 505 BB_HEAD (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) : get_insns ());
91278841
AP
506}
507
11b904a1
BS
508/* Emit INSN at the entry point of the function, ensuring that it is only
509 executed once per function. */
510void
511emit_insn_at_entry (rtx insn)
512{
fefa31b5 513 edge_iterator ei = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
11b904a1 514 edge e = ei_safe_edge (ei);
5419bc7f 515 gcc_assert (e->flags & EDGE_FALLTHRU);
11b904a1
BS
516
517 insert_insn_on_edge (insn, e);
518 commit_edge_insertions ();
519}
520
6c3d0e31 521/* Update BLOCK_FOR_INSN of insns between BEGIN and END
b8698a0f 522 (or BARRIER if found) and notify df of the bb change.
6c3d0e31
SP
523 The insn chain range is inclusive
524 (i.e. both BEGIN and END will be updated. */
ca6c03ca 525
6c3d0e31
SP
526static void
527update_bb_for_insn_chain (rtx begin, rtx end, basic_block bb)
ca6c03ca
JH
528{
529 rtx insn;
530
6c3d0e31
SP
531 end = NEXT_INSN (end);
532 for (insn = begin; insn != end; insn = NEXT_INSN (insn))
63642d5a
AO
533 if (!BARRIER_P (insn))
534 df_insn_change_bb (insn, bb);
ca6c03ca 535}
6c3d0e31
SP
536
537/* Update BLOCK_FOR_INSN of insns in BB to BB,
538 and notify df of the change. */
539
540void
541update_bb_for_insn (basic_block bb)
542{
543 update_bb_for_insn_chain (BB_HEAD (bb), BB_END (bb), bb);
544}
545
532aafad
SB
546\f
547/* Like active_insn_p, except keep the return value clobber around
548 even after reload. */
549
550static bool
551flow_active_insn_p (const_rtx insn)
552{
553 if (active_insn_p (insn))
554 return true;
555
556 /* A clobber of the function return value exists for buggy
557 programs that fail to return a value. Its effect is to
558 keep the return value from being live across the entire
559 function. If we allow it to be skipped, we introduce the
560 possibility for register lifetime confusion. */
561 if (GET_CODE (PATTERN (insn)) == CLOBBER
562 && REG_P (XEXP (PATTERN (insn), 0))
563 && REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))
564 return true;
565
566 return false;
567}
568
569/* Return true if the block has no effect and only forwards control flow to
570 its single destination. */
532aafad
SB
571
572bool
2407343c 573contains_no_active_insn_p (const_basic_block bb)
532aafad
SB
574{
575 rtx insn;
576
fefa31b5 577 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun) || bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)
532aafad
SB
578 || !single_succ_p (bb))
579 return false;
580
2407343c
JR
581 for (insn = BB_HEAD (bb); insn != BB_END (bb); insn = NEXT_INSN (insn))
582 if (INSN_P (insn) && flow_active_insn_p (insn))
583 return false;
584
585 return (!INSN_P (insn)
586 || (JUMP_P (insn) && simplejump_p (insn))
587 || !flow_active_insn_p (insn));
588}
589
590/* Likewise, but protect loop latches, headers and preheaders. */
591/* FIXME: Make this a cfg hook. */
592
593bool
594forwarder_block_p (const_basic_block bb)
595{
596 if (!contains_no_active_insn_p (bb))
597 return false;
598
532aafad
SB
599 /* Protect loop latches, headers and preheaders. */
600 if (current_loops)
601 {
602 basic_block dest;
603 if (bb->loop_father->header == bb)
604 return false;
605 dest = EDGE_SUCC (bb, 0)->dest;
606 if (dest->loop_father->header == dest)
607 return false;
608 }
609
2407343c 610 return true;
532aafad
SB
611}
612
613/* Return nonzero if we can reach target from src by falling through. */
d7b6661b 614/* FIXME: Make this a cfg hook, the result is only valid in cfgrtl mode. */
532aafad
SB
615
616bool
617can_fallthru (basic_block src, basic_block target)
618{
619 rtx insn = BB_END (src);
620 rtx insn2;
621 edge e;
622 edge_iterator ei;
623
fefa31b5 624 if (target == EXIT_BLOCK_PTR_FOR_FN (cfun))
532aafad
SB
625 return true;
626 if (src->next_bb != target)
d7b6661b
SB
627 return false;
628
629 /* ??? Later we may add code to move jump tables offline. */
630 if (tablejump_p (insn, NULL, NULL))
631 return false;
632
532aafad 633 FOR_EACH_EDGE (e, ei, src->succs)
fefa31b5 634 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
532aafad 635 && e->flags & EDGE_FALLTHRU)
d7b6661b 636 return false;
532aafad
SB
637
638 insn2 = BB_HEAD (target);
d7b6661b 639 if (!active_insn_p (insn2))
532aafad
SB
640 insn2 = next_active_insn (insn2);
641
532aafad
SB
642 return next_active_insn (insn) == insn2;
643}
644
645/* Return nonzero if we could reach target from src by falling through,
646 if the target was made adjacent. If we already have a fall-through
647 edge to the exit block, we can't do that. */
648static bool
649could_fall_through (basic_block src, basic_block target)
650{
651 edge e;
652 edge_iterator ei;
653
fefa31b5 654 if (target == EXIT_BLOCK_PTR_FOR_FN (cfun))
532aafad
SB
655 return true;
656 FOR_EACH_EDGE (e, ei, src->succs)
fefa31b5 657 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
532aafad
SB
658 && e->flags & EDGE_FALLTHRU)
659 return 0;
660 return true;
661}
ca6c03ca 662\f
ef2be249
RS
663/* Return the NOTE_INSN_BASIC_BLOCK of BB. */
664rtx
665bb_note (basic_block bb)
666{
667 rtx note;
668
669 note = BB_HEAD (bb);
670 if (LABEL_P (note))
671 note = NEXT_INSN (note);
672
673 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
674 return note;
675}
676
6fb5fa3c
DB
677/* Return the INSN immediately following the NOTE_INSN_BASIC_BLOCK
678 note associated with the BLOCK. */
679
680static rtx
681first_insn_after_basic_block_note (basic_block block)
682{
683 rtx insn;
684
685 /* Get the first instruction in the block. */
686 insn = BB_HEAD (block);
687
688 if (insn == NULL_RTX)
689 return NULL_RTX;
690 if (LABEL_P (insn))
691 insn = NEXT_INSN (insn);
692 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
693
694 return NEXT_INSN (insn);
695}
696
f470c378
ZD
697/* Creates a new basic block just after basic block B by splitting
698 everything after specified instruction I. */
ca6c03ca 699
f470c378 700static basic_block
d329e058 701rtl_split_block (basic_block bb, void *insnp)
ca6c03ca
JH
702{
703 basic_block new_bb;
ae50c0cb 704 rtx insn = (rtx) insnp;
f470c378 705 edge e;
628f6a4e 706 edge_iterator ei;
ca6c03ca 707
f470c378
ZD
708 if (!insn)
709 {
710 insn = first_insn_after_basic_block_note (bb);
711
712 if (insn)
b5b8b0ac
AO
713 {
714 rtx next = insn;
715
716 insn = PREV_INSN (insn);
717
718 /* If the block contains only debug insns, insn would have
719 been NULL in a non-debug compilation, and then we'd end
720 up emitting a DELETED note. For -fcompare-debug
721 stability, emit the note too. */
722 if (insn != BB_END (bb)
723 && DEBUG_INSN_P (next)
724 && DEBUG_INSN_P (BB_END (bb)))
725 {
726 while (next != BB_END (bb) && DEBUG_INSN_P (next))
727 next = NEXT_INSN (next);
728
729 if (next == BB_END (bb))
730 emit_note_after (NOTE_INSN_DELETED, next);
731 }
732 }
f470c378
ZD
733 else
734 insn = get_last_insn ();
735 }
736
737 /* We probably should check type of the insn so that we do not create
738 inconsistent cfg. It is checked in verify_flow_info anyway, so do not
739 bother. */
740 if (insn == BB_END (bb))
741 emit_note_after (NOTE_INSN_DELETED, insn);
ca6c03ca
JH
742
743 /* Create the new basic block. */
a813c111 744 new_bb = create_basic_block (NEXT_INSN (insn), BB_END (bb), bb);
076c7ab8 745 BB_COPY_PARTITION (new_bb, bb);
190bea87 746 SET_BB_END (bb) = insn;
ca6c03ca
JH
747
748 /* Redirect the outgoing edges. */
628f6a4e
BE
749 new_bb->succs = bb->succs;
750 bb->succs = NULL;
751 FOR_EACH_EDGE (e, ei, new_bb->succs)
ca6c03ca
JH
752 e->src = new_bb;
753
6fb5fa3c
DB
754 /* The new block starts off being dirty. */
755 df_set_bb_dirty (bb);
f470c378 756 return new_bb;
bc35512f
JH
757}
758
9be94227
EB
759/* Return true if the single edge between blocks A and B is the only place
760 in RTL which holds some unique locus. */
761
762static bool
763unique_locus_on_edge_between_p (basic_block a, basic_block b)
764{
5368224f 765 const location_t goto_locus = EDGE_SUCC (a, 0)->goto_locus;
9be94227
EB
766 rtx insn, end;
767
2f13f2de 768 if (LOCATION_LOCUS (goto_locus) == UNKNOWN_LOCATION)
9be94227
EB
769 return false;
770
771 /* First scan block A backward. */
772 insn = BB_END (a);
773 end = PREV_INSN (BB_HEAD (a));
5368224f 774 while (insn != end && (!NONDEBUG_INSN_P (insn) || !INSN_HAS_LOCATION (insn)))
9be94227
EB
775 insn = PREV_INSN (insn);
776
5368224f 777 if (insn != end && INSN_LOCATION (insn) == goto_locus)
9be94227
EB
778 return false;
779
780 /* Then scan block B forward. */
781 insn = BB_HEAD (b);
782 if (insn)
783 {
784 end = NEXT_INSN (BB_END (b));
785 while (insn != end && !NONDEBUG_INSN_P (insn))
786 insn = NEXT_INSN (insn);
787
5368224f
DC
788 if (insn != end && INSN_HAS_LOCATION (insn)
789 && INSN_LOCATION (insn) == goto_locus)
9be94227
EB
790 return false;
791 }
792
793 return true;
794}
795
796/* If the single edge between blocks A and B is the only place in RTL which
797 holds some unique locus, emit a nop with that locus between the blocks. */
798
799static void
800emit_nop_for_unique_locus_between (basic_block a, basic_block b)
801{
802 if (!unique_locus_on_edge_between_p (a, b))
803 return;
804
190bea87 805 SET_BB_END (a) = emit_insn_after_noloc (gen_nop (), BB_END (a), a);
5368224f 806 INSN_LOCATION (BB_END (a)) = EDGE_SUCC (a, 0)->goto_locus;
9be94227
EB
807}
808
ca6c03ca 809/* Blocks A and B are to be merged into a single block A. The insns
bc35512f 810 are already contiguous. */
ca6c03ca 811
bc35512f
JH
812static void
813rtl_merge_blocks (basic_block a, basic_block b)
ca6c03ca 814{
a813c111 815 rtx b_head = BB_HEAD (b), b_end = BB_END (b), a_end = BB_END (a);
ca6c03ca 816 rtx del_first = NULL_RTX, del_last = NULL_RTX;
b5b8b0ac 817 rtx b_debug_start = b_end, b_debug_end = b_end;
50a36e42 818 bool forwarder_p = (b->flags & BB_FORWARDER_BLOCK) != 0;
ca6c03ca
JH
819 int b_empty = 0;
820
6fb5fa3c 821 if (dump_file)
50a36e42
EB
822 fprintf (dump_file, "Merging block %d into block %d...\n", b->index,
823 a->index);
6fb5fa3c 824
b5b8b0ac
AO
825 while (DEBUG_INSN_P (b_end))
826 b_end = PREV_INSN (b_debug_start = b_end);
827
ca6c03ca 828 /* If there was a CODE_LABEL beginning B, delete it. */
4b4bf941 829 if (LABEL_P (b_head))
ca6c03ca
JH
830 {
831 /* Detect basic blocks with nothing but a label. This can happen
832 in particular at the end of a function. */
833 if (b_head == b_end)
834 b_empty = 1;
5f0d2358 835
ca6c03ca
JH
836 del_first = del_last = b_head;
837 b_head = NEXT_INSN (b_head);
838 }
839
5f0d2358
RK
840 /* Delete the basic block note and handle blocks containing just that
841 note. */
ca6c03ca
JH
842 if (NOTE_INSN_BASIC_BLOCK_P (b_head))
843 {
844 if (b_head == b_end)
845 b_empty = 1;
846 if (! del_last)
847 del_first = b_head;
5f0d2358 848
ca6c03ca
JH
849 del_last = b_head;
850 b_head = NEXT_INSN (b_head);
851 }
852
853 /* If there was a jump out of A, delete it. */
4b4bf941 854 if (JUMP_P (a_end))
ca6c03ca
JH
855 {
856 rtx prev;
857
858 for (prev = PREV_INSN (a_end); ; prev = PREV_INSN (prev))
4b4bf941 859 if (!NOTE_P (prev)
a38e7aa5 860 || NOTE_INSN_BASIC_BLOCK_P (prev)
a813c111 861 || prev == BB_HEAD (a))
ca6c03ca
JH
862 break;
863
864 del_first = a_end;
865
866#ifdef HAVE_cc0
867 /* If this was a conditional jump, we need to also delete
868 the insn that set cc0. */
869 if (only_sets_cc0_p (prev))
870 {
871 rtx tmp = prev;
5f0d2358 872
ca6c03ca
JH
873 prev = prev_nonnote_insn (prev);
874 if (!prev)
a813c111 875 prev = BB_HEAD (a);
ca6c03ca
JH
876 del_first = tmp;
877 }
878#endif
879
880 a_end = PREV_INSN (del_first);
881 }
4b4bf941 882 else if (BARRIER_P (NEXT_INSN (a_end)))
ca6c03ca
JH
883 del_first = NEXT_INSN (a_end);
884
ca6c03ca
JH
885 /* Delete everything marked above as well as crap that might be
886 hanging out between the two blocks. */
190bea87
DM
887 SET_BB_END (a) = a_end;
888 SET_BB_HEAD (b) = b_empty ? NULL_RTX : b_head;
a7b87f73 889 delete_insn_chain (del_first, del_last, true);
ca6c03ca 890
8a829274 891 /* When not optimizing and the edge is the only place in RTL which holds
9be94227
EB
892 some unique locus, emit a nop with that locus in between. */
893 if (!optimize)
894 {
895 emit_nop_for_unique_locus_between (a, b);
896 a_end = BB_END (a);
897 }
898
ca6c03ca
JH
899 /* Reassociate the insns of B with A. */
900 if (!b_empty)
901 {
b5b8b0ac 902 update_bb_for_insn_chain (a_end, b_debug_end, a);
5f0d2358 903
190bea87
DM
904 SET_BB_END (a) = b_debug_end;
905 SET_BB_HEAD (b) = NULL_RTX;
b5b8b0ac
AO
906 }
907 else if (b_end != b_debug_end)
908 {
909 /* Move any deleted labels and other notes between the end of A
910 and the debug insns that make up B after the debug insns,
911 bringing the debug insns into A while keeping the notes after
912 the end of A. */
913 if (NEXT_INSN (a_end) != b_debug_start)
914 reorder_insns_nobb (NEXT_INSN (a_end), PREV_INSN (b_debug_start),
915 b_debug_end);
916 update_bb_for_insn_chain (b_debug_start, b_debug_end, a);
190bea87 917 SET_BB_END (a) = b_debug_end;
ca6c03ca 918 }
5f0d2358 919
6fb5fa3c 920 df_bb_delete (b->index);
50a36e42
EB
921
922 /* If B was a forwarder block, propagate the locus on the edge. */
fbc68f2a
DC
923 if (forwarder_p
924 && LOCATION_LOCUS (EDGE_SUCC (b, 0)->goto_locus) == UNKNOWN_LOCATION)
50a36e42
EB
925 EDGE_SUCC (b, 0)->goto_locus = EDGE_SUCC (a, 0)->goto_locus;
926
927 if (dump_file)
928 fprintf (dump_file, "Merged blocks %d and %d.\n", a->index, b->index);
ca6c03ca 929}
bc35512f 930
6fb5fa3c 931
bc35512f 932/* Return true when block A and B can be merged. */
9678086d 933
b48d0358
DN
934static bool
935rtl_can_merge_blocks (basic_block a, basic_block b)
bc35512f 936{
750054a2
CT
937 /* If we are partitioning hot/cold basic blocks, we don't want to
938 mess up unconditional or indirect jumps that cross between hot
076c7ab8
ZW
939 and cold sections.
940
8e8d5162 941 Basic block partitioning may result in some jumps that appear to
c22cacf3
MS
942 be optimizable (or blocks that appear to be mergeable), but which really
943 must be left untouched (they are required to make it safely across
944 partition boundaries). See the comments at the top of
8e8d5162 945 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
076c7ab8 946
87c8b4be 947 if (BB_PARTITION (a) != BB_PARTITION (b))
076c7ab8 948 return false;
750054a2 949
7d776ee2
RG
950 /* Protect the loop latches. */
951 if (current_loops && b->loop_father->latch == b)
952 return false;
953
bc35512f 954 /* There must be exactly one edge in between the blocks. */
c5cbcccf
ZD
955 return (single_succ_p (a)
956 && single_succ (a) == b
957 && single_pred_p (b)
628f6a4e 958 && a != b
bc35512f 959 /* Must be simple edge. */
c5cbcccf 960 && !(single_succ_edge (a)->flags & EDGE_COMPLEX)
bc35512f 961 && a->next_bb == b
fefa31b5
DM
962 && a != ENTRY_BLOCK_PTR_FOR_FN (cfun)
963 && b != EXIT_BLOCK_PTR_FOR_FN (cfun)
bc35512f
JH
964 /* If the jump insn has side effects,
965 we can't kill the edge. */
4b4bf941 966 && (!JUMP_P (BB_END (a))
e24e7211 967 || (reload_completed
a813c111 968 ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
bc35512f 969}
ca6c03ca 970\f
5f0d2358
RK
971/* Return the label in the head of basic block BLOCK. Create one if it doesn't
972 exist. */
ca6c03ca
JH
973
974rtx
d329e058 975block_label (basic_block block)
ca6c03ca 976{
fefa31b5 977 if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
ca6c03ca 978 return NULL_RTX;
5f0d2358 979
4b4bf941 980 if (!LABEL_P (BB_HEAD (block)))
ca6c03ca 981 {
190bea87 982 SET_BB_HEAD (block) = emit_label_before (gen_label_rtx (), BB_HEAD (block));
ca6c03ca 983 }
5f0d2358 984
a813c111 985 return BB_HEAD (block);
ca6c03ca
JH
986}
987
988/* Attempt to perform edge redirection by replacing possibly complex jump
5f0d2358
RK
989 instruction by unconditional jump or removing jump completely. This can
990 apply only if all edges now point to the same block. The parameters and
991 return values are equivalent to redirect_edge_and_branch. */
ca6c03ca 992
6de9cd9a 993edge
bc35512f 994try_redirect_by_replacing_jump (edge e, basic_block target, bool in_cfglayout)
ca6c03ca
JH
995{
996 basic_block src = e->src;
a813c111 997 rtx insn = BB_END (src), kill_from;
e1233a7d 998 rtx set;
ca6c03ca 999 int fallthru = 0;
750054a2
CT
1000
1001 /* If we are partitioning hot/cold basic blocks, we don't want to
1002 mess up unconditional or indirect jumps that cross between hot
8e8d5162
CT
1003 and cold sections.
1004
1005 Basic block partitioning may result in some jumps that appear to
c22cacf3
MS
1006 be optimizable (or blocks that appear to be mergeable), but which really
1007 must be left untouched (they are required to make it safely across
1008 partition boundaries). See the comments at the top of
8e8d5162 1009 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
c22cacf3 1010
3371a64f 1011 if (BB_PARTITION (src) != BB_PARTITION (target))
9cf84a3c 1012 return NULL;
750054a2 1013
6a66a8a7
KH
1014 /* We can replace or remove a complex jump only when we have exactly
1015 two edges. Also, if we have exactly one outgoing edge, we can
1016 redirect that. */
1017 if (EDGE_COUNT (src->succs) >= 3
1018 /* Verify that all targets will be TARGET. Specifically, the
1019 edge that is not E must also go to TARGET. */
1020 || (EDGE_COUNT (src->succs) == 2
1021 && EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target))
1022 return NULL;
5f0d2358 1023
6a66a8a7 1024 if (!onlyjump_p (insn))
6de9cd9a 1025 return NULL;
3348b696 1026 if ((!optimize || reload_completed) && tablejump_p (insn, NULL, NULL))
6de9cd9a 1027 return NULL;
ca6c03ca
JH
1028
1029 /* Avoid removing branch with side effects. */
1030 set = single_set (insn);
1031 if (!set || side_effects_p (set))
6de9cd9a 1032 return NULL;
ca6c03ca
JH
1033
1034 /* In case we zap a conditional jump, we'll need to kill
1035 the cc0 setter too. */
1036 kill_from = insn;
1037#ifdef HAVE_cc0
9caea4a7
RS
1038 if (reg_mentioned_p (cc0_rtx, PATTERN (insn))
1039 && only_sets_cc0_p (PREV_INSN (insn)))
ca6c03ca
JH
1040 kill_from = PREV_INSN (insn);
1041#endif
1042
1043 /* See if we can create the fallthru edge. */
bc35512f 1044 if (in_cfglayout || can_fallthru (src, target))
ca6c03ca 1045 {
c263766c
RH
1046 if (dump_file)
1047 fprintf (dump_file, "Removing jump %i.\n", INSN_UID (insn));
ca6c03ca
JH
1048 fallthru = 1;
1049
eaec9b3d 1050 /* Selectively unlink whole insn chain. */
bc35512f
JH
1051 if (in_cfglayout)
1052 {
bcc708fc 1053 rtx insn = BB_FOOTER (src);
bc35512f 1054
a7b87f73 1055 delete_insn_chain (kill_from, BB_END (src), false);
bc35512f
JH
1056
1057 /* Remove barriers but keep jumptables. */
1058 while (insn)
1059 {
4b4bf941 1060 if (BARRIER_P (insn))
bc35512f
JH
1061 {
1062 if (PREV_INSN (insn))
0f82e5c9 1063 SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
bc35512f 1064 else
190bea87 1065 SET_BB_FOOTER (src) = NEXT_INSN (insn);
bc35512f 1066 if (NEXT_INSN (insn))
0f82e5c9 1067 SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
bc35512f 1068 }
4b4bf941 1069 if (LABEL_P (insn))
bc35512f
JH
1070 break;
1071 insn = NEXT_INSN (insn);
1072 }
1073 }
1074 else
a7b87f73
ZD
1075 delete_insn_chain (kill_from, PREV_INSN (BB_HEAD (target)),
1076 false);
ca6c03ca 1077 }
5f0d2358 1078
ca6c03ca
JH
1079 /* If this already is simplejump, redirect it. */
1080 else if (simplejump_p (insn))
1081 {
1082 if (e->dest == target)
6de9cd9a 1083 return NULL;
c263766c
RH
1084 if (dump_file)
1085 fprintf (dump_file, "Redirecting jump %i from %i to %i.\n",
0b17ab2f 1086 INSN_UID (insn), e->dest->index, target->index);
6ee3c8e4
JJ
1087 if (!redirect_jump (insn, block_label (target), 0))
1088 {
fefa31b5 1089 gcc_assert (target == EXIT_BLOCK_PTR_FOR_FN (cfun));
341c100f 1090 return NULL;
6ee3c8e4 1091 }
ca6c03ca 1092 }
5f0d2358 1093
6ee3c8e4 1094 /* Cannot do anything for target exit block. */
fefa31b5 1095 else if (target == EXIT_BLOCK_PTR_FOR_FN (cfun))
6de9cd9a 1096 return NULL;
6ee3c8e4 1097
ca6c03ca
JH
1098 /* Or replace possibly complicated jump insn by simple jump insn. */
1099 else
1100 {
1101 rtx target_label = block_label (target);
8942ee0f
DM
1102 rtx barrier, label;
1103 rtx_jump_table_data *table;
ca6c03ca 1104
a7102479 1105 emit_jump_insn_after_noloc (gen_jump (target_label), insn);
a813c111 1106 JUMP_LABEL (BB_END (src)) = target_label;
ca6c03ca 1107 LABEL_NUSES (target_label)++;
c263766c
RH
1108 if (dump_file)
1109 fprintf (dump_file, "Replacing insn %i by jump %i\n",
a813c111 1110 INSN_UID (insn), INSN_UID (BB_END (src)));
ca6c03ca 1111
4da2eb6b 1112
ba4807a0
LB
1113 delete_insn_chain (kill_from, insn, false);
1114
4da2eb6b
RH
1115 /* Recognize a tablejump that we are converting to a
1116 simple jump and remove its associated CODE_LABEL
1117 and ADDR_VEC or ADDR_DIFF_VEC. */
ba4807a0
LB
1118 if (tablejump_p (insn, &label, &table))
1119 delete_insn_chain (label, table, false);
eb5b8ad4 1120
a813c111 1121 barrier = next_nonnote_insn (BB_END (src));
4b4bf941 1122 if (!barrier || !BARRIER_P (barrier))
a813c111 1123 emit_barrier_after (BB_END (src));
5d693491
JZ
1124 else
1125 {
a813c111 1126 if (barrier != NEXT_INSN (BB_END (src)))
5d693491
JZ
1127 {
1128 /* Move the jump before barrier so that the notes
1129 which originally were or were created before jump table are
1130 inside the basic block. */
a813c111 1131 rtx new_insn = BB_END (src);
5d693491 1132
6c3d0e31
SP
1133 update_bb_for_insn_chain (NEXT_INSN (BB_END (src)),
1134 PREV_INSN (barrier), src);
5d693491 1135
0f82e5c9
DM
1136 SET_NEXT_INSN (PREV_INSN (new_insn)) = NEXT_INSN (new_insn);
1137 SET_PREV_INSN (NEXT_INSN (new_insn)) = PREV_INSN (new_insn);
5d693491 1138
0f82e5c9
DM
1139 SET_NEXT_INSN (new_insn) = barrier;
1140 SET_NEXT_INSN (PREV_INSN (barrier)) = new_insn;
5d693491 1141
0f82e5c9
DM
1142 SET_PREV_INSN (new_insn) = PREV_INSN (barrier);
1143 SET_PREV_INSN (barrier) = new_insn;
5d693491
JZ
1144 }
1145 }
ca6c03ca
JH
1146 }
1147
1148 /* Keep only one edge out and set proper flags. */
c5cbcccf 1149 if (!single_succ_p (src))
628f6a4e 1150 remove_edge (e);
c5cbcccf 1151 gcc_assert (single_succ_p (src));
628f6a4e 1152
c5cbcccf 1153 e = single_succ_edge (src);
ca6c03ca
JH
1154 if (fallthru)
1155 e->flags = EDGE_FALLTHRU;
1156 else
1157 e->flags = 0;
5f0d2358 1158
ca6c03ca
JH
1159 e->probability = REG_BR_PROB_BASE;
1160 e->count = src->count;
1161
ca6c03ca
JH
1162 if (e->dest != target)
1163 redirect_edge_succ (e, target);
6de9cd9a 1164 return e;
ca6c03ca
JH
1165}
1166
4e3825db
MM
1167/* Subroutine of redirect_branch_edge that tries to patch the jump
1168 instruction INSN so that it reaches block NEW. Do this
1169 only when it originally reached block OLD. Return true if this
1170 worked or the original target wasn't OLD, return false if redirection
1171 doesn't work. */
1172
1173static bool
1174patch_jump_insn (rtx insn, rtx old_label, basic_block new_bb)
ca6c03ca 1175{
8942ee0f 1176 rtx_jump_table_data *table;
ca6c03ca 1177 rtx tmp;
ca6c03ca 1178 /* Recognize a tablejump and adjust all matching cases. */
8942ee0f 1179 if (tablejump_p (insn, NULL, &table))
ca6c03ca
JH
1180 {
1181 rtvec vec;
1182 int j;
4e3825db 1183 rtx new_label = block_label (new_bb);
ca6c03ca 1184
fefa31b5 1185 if (new_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
4e3825db 1186 return false;
8942ee0f
DM
1187 if (GET_CODE (PATTERN (table)) == ADDR_VEC)
1188 vec = XVEC (PATTERN (table), 0);
ca6c03ca 1189 else
8942ee0f 1190 vec = XVEC (PATTERN (table), 1);
ca6c03ca
JH
1191
1192 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
1193 if (XEXP (RTVEC_ELT (vec, j), 0) == old_label)
1194 {
1195 RTVEC_ELT (vec, j) = gen_rtx_LABEL_REF (Pmode, new_label);
1196 --LABEL_NUSES (old_label);
1197 ++LABEL_NUSES (new_label);
1198 }
1199
f9da5064 1200 /* Handle casesi dispatch insns. */
ca6c03ca
JH
1201 if ((tmp = single_set (insn)) != NULL
1202 && SET_DEST (tmp) == pc_rtx
1203 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
1204 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF
1205 && XEXP (XEXP (SET_SRC (tmp), 2), 0) == old_label)
1206 {
4c33cb26 1207 XEXP (SET_SRC (tmp), 2) = gen_rtx_LABEL_REF (Pmode,
ca6c03ca
JH
1208 new_label);
1209 --LABEL_NUSES (old_label);
1210 ++LABEL_NUSES (new_label);
1211 }
1212 }
1c384bf1
RH
1213 else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL)
1214 {
1215 int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp);
1216 rtx new_label, note;
1217
fefa31b5 1218 if (new_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1c384bf1
RH
1219 return false;
1220 new_label = block_label (new_bb);
1221
1222 for (i = 0; i < n; ++i)
1223 {
1224 rtx old_ref = ASM_OPERANDS_LABEL (tmp, i);
1225 gcc_assert (GET_CODE (old_ref) == LABEL_REF);
1226 if (XEXP (old_ref, 0) == old_label)
1227 {
1228 ASM_OPERANDS_LABEL (tmp, i)
1229 = gen_rtx_LABEL_REF (Pmode, new_label);
1230 --LABEL_NUSES (old_label);
1231 ++LABEL_NUSES (new_label);
1232 }
1233 }
1234
1235 if (JUMP_LABEL (insn) == old_label)
1236 {
1237 JUMP_LABEL (insn) = new_label;
1238 note = find_reg_note (insn, REG_LABEL_TARGET, new_label);
1239 if (note)
1240 remove_note (insn, note);
1241 }
1242 else
1243 {
1244 note = find_reg_note (insn, REG_LABEL_TARGET, old_label);
1245 if (note)
1246 remove_note (insn, note);
1247 if (JUMP_LABEL (insn) != new_label
1248 && !find_reg_note (insn, REG_LABEL_TARGET, new_label))
1249 add_reg_note (insn, REG_LABEL_TARGET, new_label);
1250 }
3b5fda81
JJ
1251 while ((note = find_reg_note (insn, REG_LABEL_OPERAND, old_label))
1252 != NULL_RTX)
1253 XEXP (note, 0) = new_label;
1c384bf1 1254 }
ca6c03ca
JH
1255 else
1256 {
1257 /* ?? We may play the games with moving the named labels from
1258 one basic block to the other in case only one computed_jump is
1259 available. */
5f0d2358
RK
1260 if (computed_jump_p (insn)
1261 /* A return instruction can't be redirected. */
1262 || returnjump_p (insn))
4e3825db 1263 return false;
6ee3c8e4 1264
4e3825db 1265 if (!currently_expanding_to_rtl || JUMP_LABEL (insn) == old_label)
6ee3c8e4 1266 {
4e3825db
MM
1267 /* If the insn doesn't go where we think, we're confused. */
1268 gcc_assert (JUMP_LABEL (insn) == old_label);
1269
1270 /* If the substitution doesn't succeed, die. This can happen
1271 if the back end emitted unrecognizable instructions or if
1272 target is exit block on some arches. */
1273 if (!redirect_jump (insn, block_label (new_bb), 0))
1274 {
fefa31b5 1275 gcc_assert (new_bb == EXIT_BLOCK_PTR_FOR_FN (cfun));
4e3825db
MM
1276 return false;
1277 }
6ee3c8e4 1278 }
ca6c03ca 1279 }
4e3825db
MM
1280 return true;
1281}
1282
1283
1284/* Redirect edge representing branch of (un)conditional jump or tablejump,
1285 NULL on failure */
1286static edge
1287redirect_branch_edge (edge e, basic_block target)
1288{
1289 rtx old_label = BB_HEAD (e->dest);
1290 basic_block src = e->src;
1291 rtx insn = BB_END (src);
1292
1293 /* We can only redirect non-fallthru edges of jump insn. */
1294 if (e->flags & EDGE_FALLTHRU)
1295 return NULL;
1296 else if (!JUMP_P (insn) && !currently_expanding_to_rtl)
1297 return NULL;
1298
1299 if (!currently_expanding_to_rtl)
1300 {
1301 if (!patch_jump_insn (insn, old_label, target))
1302 return NULL;
1303 }
1304 else
1305 /* When expanding this BB might actually contain multiple
1306 jumps (i.e. not yet split by find_many_sub_basic_blocks).
1307 Redirect all of those that match our label. */
0f346928 1308 FOR_BB_INSNS (src, insn)
4e3825db
MM
1309 if (JUMP_P (insn) && !patch_jump_insn (insn, old_label, target))
1310 return NULL;
ca6c03ca 1311
c263766c
RH
1312 if (dump_file)
1313 fprintf (dump_file, "Edge %i->%i redirected to %i\n",
0b17ab2f 1314 e->src->index, e->dest->index, target->index);
5f0d2358 1315
ca6c03ca 1316 if (e->dest != target)
6de9cd9a 1317 e = redirect_edge_succ_nodup (e, target);
6fb5fa3c 1318
6de9cd9a 1319 return e;
bc35512f
JH
1320}
1321
3371a64f
TJ
1322/* Called when edge E has been redirected to a new destination,
1323 in order to update the region crossing flag on the edge and
1324 jump. */
1325
1326static void
1327fixup_partition_crossing (edge e)
1328{
fefa31b5
DM
1329 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun) || e->dest
1330 == EXIT_BLOCK_PTR_FOR_FN (cfun))
3371a64f
TJ
1331 return;
1332 /* If we redirected an existing edge, it may already be marked
1333 crossing, even though the new src is missing a reg crossing note.
1334 But make sure reg crossing note doesn't already exist before
1335 inserting. */
1336 if (BB_PARTITION (e->src) != BB_PARTITION (e->dest))
1337 {
1338 e->flags |= EDGE_CROSSING;
3371a64f 1339 if (JUMP_P (BB_END (e->src))
339ba33b
RS
1340 && !CROSSING_JUMP_P (BB_END (e->src)))
1341 CROSSING_JUMP_P (BB_END (e->src)) = 1;
3371a64f
TJ
1342 }
1343 else if (BB_PARTITION (e->src) == BB_PARTITION (e->dest))
1344 {
1345 e->flags &= ~EDGE_CROSSING;
1346 /* Remove the section crossing note from jump at end of
1347 src if it exists, and if no other successors are
1348 still crossing. */
339ba33b 1349 if (JUMP_P (BB_END (e->src)) && CROSSING_JUMP_P (BB_END (e->src)))
3371a64f
TJ
1350 {
1351 bool has_crossing_succ = false;
1352 edge e2;
1353 edge_iterator ei;
1354 FOR_EACH_EDGE (e2, ei, e->src->succs)
1355 {
1356 has_crossing_succ |= (e2->flags & EDGE_CROSSING);
1357 if (has_crossing_succ)
1358 break;
1359 }
1360 if (!has_crossing_succ)
339ba33b 1361 CROSSING_JUMP_P (BB_END (e->src)) = 0;
3371a64f
TJ
1362 }
1363 }
1364}
1365
600b5b1d
TJ
1366/* Called when block BB has been reassigned to the cold partition,
1367 because it is now dominated by another cold block,
1368 to ensure that the region crossing attributes are updated. */
1369
1370static void
1371fixup_new_cold_bb (basic_block bb)
1372{
1373 edge e;
1374 edge_iterator ei;
1375
1376 /* This is called when a hot bb is found to now be dominated
1377 by a cold bb and therefore needs to become cold. Therefore,
1378 its preds will no longer be region crossing. Any non-dominating
1379 preds that were previously hot would also have become cold
1380 in the caller for the same region. Any preds that were previously
1381 region-crossing will be adjusted in fixup_partition_crossing. */
1382 FOR_EACH_EDGE (e, ei, bb->preds)
1383 {
1384 fixup_partition_crossing (e);
1385 }
1386
1387 /* Possibly need to make bb's successor edges region crossing,
1388 or remove stale region crossing. */
1389 FOR_EACH_EDGE (e, ei, bb->succs)
1390 {
1391 /* We can't have fall-through edges across partition boundaries.
1392 Note that force_nonfallthru will do any necessary partition
1393 boundary fixup by calling fixup_partition_crossing itself. */
1394 if ((e->flags & EDGE_FALLTHRU)
1395 && BB_PARTITION (bb) != BB_PARTITION (e->dest)
fefa31b5 1396 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
600b5b1d
TJ
1397 force_nonfallthru (e);
1398 else
1399 fixup_partition_crossing (e);
1400 }
1401}
1402
bc35512f
JH
1403/* Attempt to change code to redirect edge E to TARGET. Don't do that on
1404 expense of adding new instructions or reordering basic blocks.
1405
1406 Function can be also called with edge destination equivalent to the TARGET.
1407 Then it should try the simplifications and do nothing if none is possible.
1408
6de9cd9a
DN
1409 Return edge representing the branch if transformation succeeded. Return NULL
1410 on failure.
1411 We still return NULL in case E already destinated TARGET and we didn't
1412 managed to simplify instruction stream. */
bc35512f 1413
6de9cd9a 1414static edge
5671bf27 1415rtl_redirect_edge_and_branch (edge e, basic_block target)
bc35512f 1416{
6de9cd9a 1417 edge ret;
f345f21a 1418 basic_block src = e->src;
3371a64f 1419 basic_block dest = e->dest;
f345f21a 1420
bc35512f 1421 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
6de9cd9a 1422 return NULL;
bc35512f 1423
3371a64f 1424 if (dest == target)
6de9cd9a 1425 return e;
3348b696 1426
6de9cd9a 1427 if ((ret = try_redirect_by_replacing_jump (e, target, false)) != NULL)
f345f21a 1428 {
6fb5fa3c 1429 df_set_bb_dirty (src);
3371a64f 1430 fixup_partition_crossing (ret);
6de9cd9a 1431 return ret;
f345f21a 1432 }
bc35512f 1433
6de9cd9a
DN
1434 ret = redirect_branch_edge (e, target);
1435 if (!ret)
1436 return NULL;
5f0d2358 1437
6fb5fa3c 1438 df_set_bb_dirty (src);
3371a64f 1439 fixup_partition_crossing (ret);
6de9cd9a 1440 return ret;
ca6c03ca
JH
1441}
1442
3371a64f
TJ
1443/* Emit a barrier after BB, into the footer if we are in CFGLAYOUT mode. */
1444
1445void
1446emit_barrier_after_bb (basic_block bb)
1447{
1448 rtx barrier = emit_barrier_after (BB_END (bb));
c3284718 1449 gcc_assert (current_ir_type () == IR_RTL_CFGRTL
3371a64f
TJ
1450 || current_ir_type () == IR_RTL_CFGLAYOUT);
1451 if (current_ir_type () == IR_RTL_CFGLAYOUT)
190bea87 1452 SET_BB_FOOTER (bb) = unlink_insn_chain (barrier, barrier);
3371a64f
TJ
1453}
1454
4fe9b91c 1455/* Like force_nonfallthru below, but additionally performs redirection
26898771
BS
1456 Used by redirect_edge_and_branch_force. JUMP_LABEL is used only
1457 when redirecting to the EXIT_BLOCK, it is either ret_rtx or
1458 simple_return_rtx, indicating which kind of returnjump to create.
1459 It should be NULL otherwise. */
ca6c03ca 1460
dc0ff1c8 1461basic_block
26898771 1462force_nonfallthru_and_redirect (edge e, basic_block target, rtx jump_label)
ca6c03ca 1463{
a3716585 1464 basic_block jump_block, new_bb = NULL, src = e->src;
ca6c03ca
JH
1465 rtx note;
1466 edge new_edge;
a3716585 1467 int abnormal_edge_flags = 0;
a3e6a37b 1468 bool asm_goto_edge = false;
7241571e 1469 int loc;
ca6c03ca 1470
cb9a1d9b
JH
1471 /* In the case the last instruction is conditional jump to the next
1472 instruction, first redirect the jump itself and then continue
b20b352b 1473 by creating a basic block afterwards to redirect fallthru edge. */
fefa31b5
DM
1474 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
1475 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
a813c111 1476 && any_condjump_p (BB_END (e->src))
a813c111 1477 && JUMP_LABEL (BB_END (e->src)) == BB_HEAD (e->dest))
cb9a1d9b
JH
1478 {
1479 rtx note;
58e6ae30 1480 edge b = unchecked_make_edge (e->src, target, 0);
341c100f 1481 bool redirected;
cb9a1d9b 1482
341c100f
NS
1483 redirected = redirect_jump (BB_END (e->src), block_label (target), 0);
1484 gcc_assert (redirected);
c22cacf3 1485
a813c111 1486 note = find_reg_note (BB_END (e->src), REG_BR_PROB, NULL_RTX);
cb9a1d9b
JH
1487 if (note)
1488 {
e5af9ddd 1489 int prob = XINT (note, 0);
cb9a1d9b
JH
1490
1491 b->probability = prob;
8ddb5a29 1492 /* Update this to use GCOV_COMPUTE_SCALE. */
cb9a1d9b
JH
1493 b->count = e->count * prob / REG_BR_PROB_BASE;
1494 e->probability -= e->probability;
1495 e->count -= b->count;
1496 if (e->probability < 0)
1497 e->probability = 0;
1498 if (e->count < 0)
1499 e->count = 0;
1500 }
1501 }
1502
ca6c03ca 1503 if (e->flags & EDGE_ABNORMAL)
a3716585
JH
1504 {
1505 /* Irritating special case - fallthru edge to the same block as abnormal
1506 edge.
1507 We can't redirect abnormal edge, but we still can split the fallthru
d329e058 1508 one and create separate abnormal edge to original destination.
a3716585 1509 This allows bb-reorder to make such edge non-fallthru. */
341c100f 1510 gcc_assert (e->dest == target);
ee44c28d
SB
1511 abnormal_edge_flags = e->flags & ~EDGE_FALLTHRU;
1512 e->flags &= EDGE_FALLTHRU;
a3716585 1513 }
341c100f 1514 else
24c545ff 1515 {
341c100f 1516 gcc_assert (e->flags & EDGE_FALLTHRU);
fefa31b5 1517 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
341c100f
NS
1518 {
1519 /* We can't redirect the entry block. Create an empty block
628f6a4e
BE
1520 at the start of the function which we use to add the new
1521 jump. */
1522 edge tmp;
1523 edge_iterator ei;
1524 bool found = false;
c22cacf3 1525
fefa31b5
DM
1526 basic_block bb = create_basic_block (BB_HEAD (e->dest), NULL,
1527 ENTRY_BLOCK_PTR_FOR_FN (cfun));
c22cacf3 1528
341c100f
NS
1529 /* Change the existing edge's source to be the new block, and add
1530 a new edge from the entry block to the new block. */
1531 e->src = bb;
fefa31b5
DM
1532 for (ei = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
1533 (tmp = ei_safe_edge (ei)); )
628f6a4e
BE
1534 {
1535 if (tmp == e)
1536 {
fefa31b5 1537 ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs->unordered_remove (ei.index);
628f6a4e
BE
1538 found = true;
1539 break;
1540 }
1541 else
1542 ei_next (&ei);
1543 }
c22cacf3 1544
628f6a4e 1545 gcc_assert (found);
c22cacf3 1546
9771b263 1547 vec_safe_push (bb->succs, e);
fefa31b5
DM
1548 make_single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb,
1549 EDGE_FALLTHRU);
341c100f 1550 }
24c545ff
BS
1551 }
1552
a3e6a37b 1553 /* If e->src ends with asm goto, see if any of the ASM_OPERANDS_LABELs
891ca07d 1554 don't point to the target or fallthru label. */
a3e6a37b 1555 if (JUMP_P (BB_END (e->src))
fefa31b5 1556 && target != EXIT_BLOCK_PTR_FOR_FN (cfun)
a3e6a37b
JJ
1557 && (e->flags & EDGE_FALLTHRU)
1558 && (note = extract_asm_operands (PATTERN (BB_END (e->src)))))
ca6c03ca 1559 {
a3e6a37b 1560 int i, n = ASM_OPERANDS_LABEL_LENGTH (note);
5d68b7e6 1561 bool adjust_jump_target = false;
a3e6a37b
JJ
1562
1563 for (i = 0; i < n; ++i)
891ca07d
JJ
1564 {
1565 if (XEXP (ASM_OPERANDS_LABEL (note, i), 0) == BB_HEAD (e->dest))
5d68b7e6
JJ
1566 {
1567 LABEL_NUSES (XEXP (ASM_OPERANDS_LABEL (note, i), 0))--;
1568 XEXP (ASM_OPERANDS_LABEL (note, i), 0) = block_label (target);
1569 LABEL_NUSES (XEXP (ASM_OPERANDS_LABEL (note, i), 0))++;
1570 adjust_jump_target = true;
1571 }
891ca07d 1572 if (XEXP (ASM_OPERANDS_LABEL (note, i), 0) == BB_HEAD (target))
a3e6a37b 1573 asm_goto_edge = true;
891ca07d 1574 }
5d68b7e6
JJ
1575 if (adjust_jump_target)
1576 {
1577 rtx insn = BB_END (e->src), note;
1578 rtx old_label = BB_HEAD (e->dest);
1579 rtx new_label = BB_HEAD (target);
1580
1581 if (JUMP_LABEL (insn) == old_label)
1582 {
1583 JUMP_LABEL (insn) = new_label;
1584 note = find_reg_note (insn, REG_LABEL_TARGET, new_label);
1585 if (note)
1586 remove_note (insn, note);
1587 }
1588 else
1589 {
1590 note = find_reg_note (insn, REG_LABEL_TARGET, old_label);
1591 if (note)
1592 remove_note (insn, note);
1593 if (JUMP_LABEL (insn) != new_label
1594 && !find_reg_note (insn, REG_LABEL_TARGET, new_label))
1595 add_reg_note (insn, REG_LABEL_TARGET, new_label);
1596 }
1597 while ((note = find_reg_note (insn, REG_LABEL_OPERAND, old_label))
1598 != NULL_RTX)
1599 XEXP (note, 0) = new_label;
1600 }
a3e6a37b
JJ
1601 }
1602
1603 if (EDGE_COUNT (e->src->succs) >= 2 || abnormal_edge_flags || asm_goto_edge)
1604 {
1605 gcov_type count = e->count;
1606 int probability = e->probability;
ca6c03ca 1607 /* Create the new structures. */
31a78298 1608
79019985
RH
1609 /* If the old block ended with a tablejump, skip its table
1610 by searching forward from there. Otherwise start searching
1611 forward from the last instruction of the old block. */
8942ee0f
DM
1612 rtx_jump_table_data *table;
1613 if (tablejump_p (BB_END (e->src), NULL, &table))
1614 note = table;
1615 else
a813c111 1616 note = BB_END (e->src);
31a78298
RH
1617 note = NEXT_INSN (note);
1618
31a78298 1619 jump_block = create_basic_block (note, NULL, e->src);
a3e6a37b 1620 jump_block->count = count;
ca6c03ca 1621 jump_block->frequency = EDGE_FREQUENCY (e);
ca6c03ca 1622
750054a2
CT
1623 /* Make sure new block ends up in correct hot/cold section. */
1624
076c7ab8 1625 BB_COPY_PARTITION (jump_block, e->src);
c22cacf3 1626
ca6c03ca
JH
1627 /* Wire edge in. */
1628 new_edge = make_edge (e->src, jump_block, EDGE_FALLTHRU);
a3e6a37b
JJ
1629 new_edge->probability = probability;
1630 new_edge->count = count;
ca6c03ca
JH
1631
1632 /* Redirect old edge. */
1633 redirect_edge_pred (e, jump_block);
1634 e->probability = REG_BR_PROB_BASE;
1635
3371a64f
TJ
1636 /* If e->src was previously region crossing, it no longer is
1637 and the reg crossing note should be removed. */
1638 fixup_partition_crossing (new_edge);
1639
a3e6a37b
JJ
1640 /* If asm goto has any label refs to target's label,
1641 add also edge from asm goto bb to target. */
1642 if (asm_goto_edge)
1643 {
1644 new_edge->probability /= 2;
1645 new_edge->count /= 2;
1646 jump_block->count /= 2;
1647 jump_block->frequency /= 2;
1648 new_edge = make_edge (new_edge->src, target,
1649 e->flags & ~EDGE_FALLTHRU);
1650 new_edge->probability = probability - probability / 2;
1651 new_edge->count = count - count / 2;
1652 }
1653
ca6c03ca
JH
1654 new_bb = jump_block;
1655 }
1656 else
1657 jump_block = e->src;
5f0d2358 1658
2f13f2de 1659 loc = e->goto_locus;
ca6c03ca 1660 e->flags &= ~EDGE_FALLTHRU;
fefa31b5 1661 if (target == EXIT_BLOCK_PTR_FOR_FN (cfun))
ca6c03ca 1662 {
26898771
BS
1663 if (jump_label == ret_rtx)
1664 {
cf22ce3c 1665#ifdef HAVE_return
26898771 1666 emit_jump_insn_after_setloc (gen_return (), BB_END (jump_block), loc);
cf22ce3c 1667#else
26898771 1668 gcc_unreachable ();
cf22ce3c 1669#endif
26898771
BS
1670 }
1671 else
1672 {
1673 gcc_assert (jump_label == simple_return_rtx);
1674#ifdef HAVE_simple_return
1675 emit_jump_insn_after_setloc (gen_simple_return (),
1676 BB_END (jump_block), loc);
1677#else
1678 gcc_unreachable ();
1679#endif
1680 }
387748de 1681 set_return_jump_label (BB_END (jump_block));
ca6c03ca
JH
1682 }
1683 else
1684 {
1685 rtx label = block_label (target);
7241571e 1686 emit_jump_insn_after_setloc (gen_jump (label), BB_END (jump_block), loc);
a813c111 1687 JUMP_LABEL (BB_END (jump_block)) = label;
ca6c03ca
JH
1688 LABEL_NUSES (label)++;
1689 }
5f0d2358 1690
3371a64f
TJ
1691 /* We might be in cfg layout mode, and if so, the following routine will
1692 insert the barrier correctly. */
1693 emit_barrier_after_bb (jump_block);
ca6c03ca
JH
1694 redirect_edge_succ_nodup (e, target);
1695
a3716585
JH
1696 if (abnormal_edge_flags)
1697 make_edge (src, target, abnormal_edge_flags);
1698
b8698a0f 1699 df_mark_solutions_dirty ();
3371a64f 1700 fixup_partition_crossing (e);
ca6c03ca
JH
1701 return new_bb;
1702}
1703
1704/* Edge E is assumed to be fallthru edge. Emit needed jump instruction
1705 (and possibly create new basic block) to make edge non-fallthru.
1706 Return newly created BB or NULL if none. */
5f0d2358 1707
cf103ca4
EB
1708static basic_block
1709rtl_force_nonfallthru (edge e)
ca6c03ca 1710{
26898771 1711 return force_nonfallthru_and_redirect (e, e->dest, NULL_RTX);
ca6c03ca
JH
1712}
1713
1714/* Redirect edge even at the expense of creating new jump insn or
1715 basic block. Return new basic block if created, NULL otherwise.
41806d92 1716 Conversion must be possible. */
ca6c03ca 1717
9ee634e3 1718static basic_block
d329e058 1719rtl_redirect_edge_and_branch_force (edge e, basic_block target)
ca6c03ca 1720{
5f0d2358
RK
1721 if (redirect_edge_and_branch (e, target)
1722 || e->dest == target)
ca6c03ca
JH
1723 return NULL;
1724
1725 /* In case the edge redirection failed, try to force it to be non-fallthru
1726 and redirect newly created simplejump. */
6fb5fa3c 1727 df_set_bb_dirty (e->src);
26898771 1728 return force_nonfallthru_and_redirect (e, target, NULL_RTX);
ca6c03ca
JH
1729}
1730
1731/* The given edge should potentially be a fallthru edge. If that is in
1732 fact true, delete the jump and barriers that are in the way. */
1733
f470c378
ZD
1734static void
1735rtl_tidy_fallthru_edge (edge e)
ca6c03ca
JH
1736{
1737 rtx q;
f470c378
ZD
1738 basic_block b = e->src, c = b->next_bb;
1739
ca6c03ca
JH
1740 /* ??? In a late-running flow pass, other folks may have deleted basic
1741 blocks by nopping out blocks, leaving multiple BARRIERs between here
0fa2e4df 1742 and the target label. They ought to be chastised and fixed.
ca6c03ca
JH
1743
1744 We can also wind up with a sequence of undeletable labels between
1745 one block and the next.
1746
1747 So search through a sequence of barriers, labels, and notes for
1748 the head of block C and assert that we really do fall through. */
1749
a813c111 1750 for (q = NEXT_INSN (BB_END (b)); q != BB_HEAD (c); q = NEXT_INSN (q))
9c0a0632
RH
1751 if (INSN_P (q))
1752 return;
ca6c03ca
JH
1753
1754 /* Remove what will soon cease being the jump insn from the source block.
1755 If block B consisted only of this single jump, turn it into a deleted
1756 note. */
a813c111 1757 q = BB_END (b);
4b4bf941 1758 if (JUMP_P (q)
ca6c03ca
JH
1759 && onlyjump_p (q)
1760 && (any_uncondjump_p (q)
c5cbcccf 1761 || single_succ_p (b)))
ca6c03ca
JH
1762 {
1763#ifdef HAVE_cc0
1764 /* If this was a conditional jump, we need to also delete
1765 the insn that set cc0. */
1766 if (any_condjump_p (q) && only_sets_cc0_p (PREV_INSN (q)))
1767 q = PREV_INSN (q);
1768#endif
1769
1770 q = PREV_INSN (q);
ca6c03ca
JH
1771 }
1772
1773 /* Selectively unlink the sequence. */
a813c111 1774 if (q != PREV_INSN (BB_HEAD (c)))
a7b87f73 1775 delete_insn_chain (NEXT_INSN (q), PREV_INSN (BB_HEAD (c)), false);
ca6c03ca
JH
1776
1777 e->flags |= EDGE_FALLTHRU;
1778}
ca6c03ca 1779\f
f470c378
ZD
1780/* Should move basic block BB after basic block AFTER. NIY. */
1781
1782static bool
1783rtl_move_block_after (basic_block bb ATTRIBUTE_UNUSED,
1784 basic_block after ATTRIBUTE_UNUSED)
1785{
1786 return false;
1787}
1788
3371a64f
TJ
1789/* Locate the last bb in the same partition as START_BB. */
1790
1791static basic_block
1792last_bb_in_partition (basic_block start_bb)
1793{
1794 basic_block bb;
fefa31b5 1795 FOR_BB_BETWEEN (bb, start_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
3371a64f
TJ
1796 {
1797 if (BB_PARTITION (start_bb) != BB_PARTITION (bb->next_bb))
1798 return bb;
1799 }
6626665f 1800 /* Return bb before the exit block. */
3371a64f
TJ
1801 return bb->prev_bb;
1802}
1803
ca6c03ca 1804/* Split a (typically critical) edge. Return the new block.
41806d92 1805 The edge must not be abnormal.
ca6c03ca
JH
1806
1807 ??? The code generally expects to be called on critical edges.
1808 The case of a block ending in an unconditional jump to a
1809 block with multiple predecessors is not handled optimally. */
1810
8ce33230 1811static basic_block
d329e058 1812rtl_split_edge (edge edge_in)
ca6c03ca 1813{
3371a64f 1814 basic_block bb, new_bb;
ca6c03ca
JH
1815 rtx before;
1816
1817 /* Abnormal edges cannot be split. */
341c100f 1818 gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
ca6c03ca
JH
1819
1820 /* We are going to place the new block in front of edge destination.
eaec9b3d 1821 Avoid existence of fallthru predecessors. */
ca6c03ca
JH
1822 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1823 {
0fd4b31d 1824 edge e = find_fallthru_edge (edge_in->dest->preds);
ca6c03ca
JH
1825
1826 if (e)
1827 force_nonfallthru (e);
1828 }
1829
96e82e0a 1830 /* Create the basic block note. */
fefa31b5 1831 if (edge_in->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
a813c111 1832 before = BB_HEAD (edge_in->dest);
ca6c03ca
JH
1833 else
1834 before = NULL_RTX;
1835
623a66fa 1836 /* If this is a fall through edge to the exit block, the blocks might be
ffe14686 1837 not adjacent, and the right place is after the source. */
fefa31b5
DM
1838 if ((edge_in->flags & EDGE_FALLTHRU)
1839 && edge_in->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
623a66fa
R
1840 {
1841 before = NEXT_INSN (BB_END (edge_in->src));
623a66fa 1842 bb = create_basic_block (before, NULL, edge_in->src);
076c7ab8 1843 BB_COPY_PARTITION (bb, edge_in->src);
623a66fa
R
1844 }
1845 else
9fb32434 1846 {
fefa31b5 1847 if (edge_in->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
3371a64f
TJ
1848 {
1849 bb = create_basic_block (before, NULL, edge_in->dest->prev_bb);
1850 BB_COPY_PARTITION (bb, edge_in->dest);
1851 }
1852 else
1853 {
1854 basic_block after = edge_in->dest->prev_bb;
1855 /* If this is post-bb reordering, and the edge crosses a partition
1856 boundary, the new block needs to be inserted in the bb chain
1857 at the end of the src partition (since we put the new bb into
1858 that partition, see below). Otherwise we may end up creating
1859 an extra partition crossing in the chain, which is illegal.
1860 It can't go after the src, because src may have a fall-through
1861 to a different block. */
1862 if (crtl->bb_reorder_complete
1863 && (edge_in->flags & EDGE_CROSSING))
1864 {
1865 after = last_bb_in_partition (edge_in->src);
1866 before = NEXT_INSN (BB_END (after));
1867 /* The instruction following the last bb in partition should
1868 be a barrier, since it cannot end in a fall-through. */
1869 gcc_checking_assert (BARRIER_P (before));
1870 before = NEXT_INSN (before);
1871 }
1872 bb = create_basic_block (before, NULL, after);
1873 /* Put the split bb into the src partition, to avoid creating
1874 a situation where a cold bb dominates a hot bb, in the case
1875 where src is cold and dest is hot. The src will dominate
1876 the new bb (whereas it might not have dominated dest). */
1877 BB_COPY_PARTITION (bb, edge_in->src);
1878 }
9fb32434 1879 }
ca6c03ca 1880
4977bab6 1881 make_single_succ_edge (bb, edge_in->dest, EDGE_FALLTHRU);
ca6c03ca 1882
3371a64f
TJ
1883 /* Can't allow a region crossing edge to be fallthrough. */
1884 if (BB_PARTITION (bb) != BB_PARTITION (edge_in->dest)
fefa31b5 1885 && edge_in->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
3371a64f
TJ
1886 {
1887 new_bb = force_nonfallthru (single_succ_edge (bb));
1888 gcc_assert (!new_bb);
1889 }
1890
4d6922ee 1891 /* For non-fallthru edges, we must adjust the predecessor's
ca6c03ca
JH
1892 jump instruction to target our new block. */
1893 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1894 {
341c100f
NS
1895 edge redirected = redirect_edge_and_branch (edge_in, bb);
1896 gcc_assert (redirected);
ca6c03ca
JH
1897 }
1898 else
3b5fda81 1899 {
fefa31b5 1900 if (edge_in->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
3b5fda81
JJ
1901 {
1902 /* For asm goto even splitting of fallthru edge might
1903 need insn patching, as other labels might point to the
1904 old label. */
1905 rtx last = BB_END (edge_in->src);
1906 if (last
1907 && JUMP_P (last)
fefa31b5 1908 && edge_in->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
3b5fda81
JJ
1909 && extract_asm_operands (PATTERN (last)) != NULL_RTX
1910 && patch_jump_insn (last, before, bb))
1911 df_set_bb_dirty (edge_in->src);
1912 }
1913 redirect_edge_succ (edge_in, bb);
1914 }
ca6c03ca
JH
1915
1916 return bb;
1917}
1918
1919/* Queue instructions for insertion on an edge between two basic blocks.
1920 The new instructions and basic blocks (if any) will not appear in the
1921 CFG until commit_edge_insertions is called. */
1922
1923void
d329e058 1924insert_insn_on_edge (rtx pattern, edge e)
ca6c03ca
JH
1925{
1926 /* We cannot insert instructions on an abnormal critical edge.
1927 It will be easier to find the culprit if we die now. */
341c100f 1928 gcc_assert (!((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e)));
ca6c03ca 1929
6de9cd9a 1930 if (e->insns.r == NULL_RTX)
ca6c03ca
JH
1931 start_sequence ();
1932 else
6de9cd9a 1933 push_to_sequence (e->insns.r);
ca6c03ca
JH
1934
1935 emit_insn (pattern);
1936
6de9cd9a 1937 e->insns.r = get_insns ();
ca6c03ca
JH
1938 end_sequence ();
1939}
1940
1941/* Update the CFG for the instructions queued on edge E. */
1942
4e3825db 1943void
2ac66157 1944commit_one_edge_insertion (edge e)
ca6c03ca
JH
1945{
1946 rtx before = NULL_RTX, after = NULL_RTX, insns, tmp, last;
898c90c0 1947 basic_block bb;
ca6c03ca
JH
1948
1949 /* Pull the insns off the edge now since the edge might go away. */
6de9cd9a
DN
1950 insns = e->insns.r;
1951 e->insns.r = NULL_RTX;
ca6c03ca 1952
898c90c0
EB
1953 /* Figure out where to put these insns. If the destination has
1954 one predecessor, insert there. Except for the exit block. */
fefa31b5 1955 if (single_pred_p (e->dest) && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
ca6c03ca 1956 {
898c90c0
EB
1957 bb = e->dest;
1958
1959 /* Get the location correct wrt a code label, and "nice" wrt
1960 a basic block note, and before everything else. */
1961 tmp = BB_HEAD (bb);
1962 if (LABEL_P (tmp))
1963 tmp = NEXT_INSN (tmp);
1964 if (NOTE_INSN_BASIC_BLOCK_P (tmp))
1965 tmp = NEXT_INSN (tmp);
1966 if (tmp == BB_HEAD (bb))
1967 before = tmp;
1968 else if (tmp)
1969 after = PREV_INSN (tmp);
1970 else
1971 after = get_last_insn ();
1972 }
3dec4024 1973
898c90c0 1974 /* If the source has one successor and the edge is not abnormal,
a1d50386
JJ
1975 insert there. Except for the entry block.
1976 Don't do this if the predecessor ends in a jump other than
1977 unconditional simple jump. E.g. for asm goto that points all
1978 its labels at the fallthru basic block, we can't insert instructions
1979 before the asm goto, as the asm goto can have various of side effects,
1980 and can't emit instructions after the asm goto, as it must end
1981 the basic block. */
898c90c0
EB
1982 else if ((e->flags & EDGE_ABNORMAL) == 0
1983 && single_succ_p (e->src)
fefa31b5 1984 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
a1d50386
JJ
1985 && (!JUMP_P (BB_END (e->src))
1986 || simplejump_p (BB_END (e->src))))
898c90c0
EB
1987 {
1988 bb = e->src;
3dec4024 1989
898c90c0
EB
1990 /* It is possible to have a non-simple jump here. Consider a target
1991 where some forms of unconditional jumps clobber a register. This
1992 happens on the fr30 for example.
ca6c03ca 1993
898c90c0
EB
1994 We know this block has a single successor, so we can just emit
1995 the queued insns before the jump. */
1996 if (JUMP_P (BB_END (bb)))
1997 before = BB_END (bb);
3dec4024
JH
1998 else
1999 {
898c90c0
EB
2000 /* We'd better be fallthru, or we've lost track of what's what. */
2001 gcc_assert (e->flags & EDGE_FALLTHRU);
750054a2 2002
898c90c0 2003 after = BB_END (bb);
ca6c03ca
JH
2004 }
2005 }
2006
898c90c0
EB
2007 /* Otherwise we must split the edge. */
2008 else
2009 {
2010 bb = split_edge (e);
3371a64f
TJ
2011
2012 /* If E crossed a partition boundary, we needed to make bb end in
2013 a region-crossing jump, even though it was originally fallthru. */
2014 if (JUMP_P (BB_END (bb)))
2015 before = BB_END (bb);
2016 else
2017 after = BB_END (bb);
898c90c0
EB
2018 }
2019
2020 /* Now that we've found the spot, do the insertion. */
ca6c03ca
JH
2021 if (before)
2022 {
6fb5fa3c 2023 emit_insn_before_noloc (insns, before, bb);
ca6c03ca
JH
2024 last = prev_nonnote_insn (before);
2025 }
2026 else
6fb5fa3c 2027 last = emit_insn_after_noloc (insns, after, bb);
ca6c03ca
JH
2028
2029 if (returnjump_p (last))
2030 {
2031 /* ??? Remove all outgoing edges from BB and add one for EXIT.
c22cacf3
MS
2032 This is not currently a problem because this only happens
2033 for the (single) epilogue, which already has a fallthru edge
2034 to EXIT. */
ca6c03ca 2035
c5cbcccf 2036 e = single_succ_edge (bb);
fefa31b5 2037 gcc_assert (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
c5cbcccf 2038 && single_succ_p (bb) && (e->flags & EDGE_FALLTHRU));
ca6c03ca 2039
5f0d2358 2040 e->flags &= ~EDGE_FALLTHRU;
ca6c03ca 2041 emit_barrier_after (last);
0b17ab2f 2042
ca6c03ca
JH
2043 if (before)
2044 delete_insn (before);
2045 }
341c100f
NS
2046 else
2047 gcc_assert (!JUMP_P (last));
ca6c03ca
JH
2048}
2049
2050/* Update the CFG for all queued instructions. */
2051
2052void
d329e058 2053commit_edge_insertions (void)
ca6c03ca 2054{
ca6c03ca
JH
2055 basic_block bb;
2056
600b5b1d
TJ
2057 /* Optimization passes that invoke this routine can cause hot blocks
2058 previously reached by both hot and cold blocks to become dominated only
2059 by cold blocks. This will cause the verification below to fail,
2060 and lead to now cold code in the hot section. In some cases this
2061 may only be visible after newly unreachable blocks are deleted,
2062 which will be done by fixup_partitions. */
2063 fixup_partitions ();
2064
ca6c03ca
JH
2065#ifdef ENABLE_CHECKING
2066 verify_flow_info ();
2067#endif
2068
fefa31b5
DM
2069 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
2070 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
ca6c03ca 2071 {
628f6a4e
BE
2072 edge e;
2073 edge_iterator ei;
ca6c03ca 2074
628f6a4e
BE
2075 FOR_EACH_EDGE (e, ei, bb->succs)
2076 if (e->insns.r)
cf103ca4 2077 commit_one_edge_insertion (e);
3dec4024 2078 }
ca6c03ca
JH
2079}
2080\f
6fb5fa3c 2081
f470c378 2082/* Print out RTL-specific basic block information (live information
a315c44c
SB
2083 at start and end with TDF_DETAILS). FLAGS are the TDF_* masks
2084 documented in dumpfile.h. */
ca6c03ca 2085
10e9fecc 2086static void
a315c44c 2087rtl_dump_bb (FILE *outf, basic_block bb, int indent, int flags)
ca6c03ca
JH
2088{
2089 rtx insn;
2090 rtx last;
f470c378 2091 char *s_indent;
ca6c03ca 2092
ae50c0cb 2093 s_indent = (char *) alloca ((size_t) indent + 1);
400e39e3 2094 memset (s_indent, ' ', (size_t) indent);
f470c378 2095 s_indent[indent] = '\0';
b8698a0f 2096
a315c44c 2097 if (df && (flags & TDF_DETAILS))
6fb5fa3c
DB
2098 {
2099 df_dump_top (bb, outf);
2100 putc ('\n', outf);
2101 }
ca6c03ca 2102
68cc3174
EB
2103 if (bb->index != ENTRY_BLOCK && bb->index != EXIT_BLOCK)
2104 for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb)); insn != last;
2105 insn = NEXT_INSN (insn))
a315c44c 2106 {
7b19209f
SB
2107 if (flags & TDF_DETAILS)
2108 df_dump_insn_top (insn, outf);
a315c44c
SB
2109 if (! (flags & TDF_SLIM))
2110 print_rtl_single (outf, insn);
2111 else
2112 dump_insn_slim (outf, insn);
7b19209f
SB
2113 if (flags & TDF_DETAILS)
2114 df_dump_insn_bottom (insn, outf);
a315c44c
SB
2115 }
2116
2117 if (df && (flags & TDF_DETAILS))
6fb5fa3c
DB
2118 {
2119 df_dump_bottom (bb, outf);
2120 putc ('\n', outf);
2121 }
2122
ca6c03ca
JH
2123}
2124\f
c4669594
SB
2125/* Like dump_function_to_file, but for RTL. Print out dataflow information
2126 for the start of each basic block. FLAGS are the TDF_* masks documented
2127 in dumpfile.h. */
ca6c03ca
JH
2128
2129void
a315c44c 2130print_rtl_with_bb (FILE *outf, const_rtx rtx_first, int flags)
ca6c03ca 2131{
22ea9ec0 2132 const_rtx tmp_rtx;
ca6c03ca
JH
2133 if (rtx_first == 0)
2134 fprintf (outf, "(nil)\n");
2135 else
2136 {
ca6c03ca
JH
2137 enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
2138 int max_uid = get_max_uid ();
5ed6ace5
MD
2139 basic_block *start = XCNEWVEC (basic_block, max_uid);
2140 basic_block *end = XCNEWVEC (basic_block, max_uid);
2141 enum bb_state *in_bb_p = XCNEWVEC (enum bb_state, max_uid);
e0082a72
ZD
2142 basic_block bb;
2143
c4669594
SB
2144 /* After freeing the CFG, we still have BLOCK_FOR_INSN set on most
2145 insns, but the CFG is not maintained so the basic block info
2146 is not reliable. Therefore it's omitted from the dumps. */
2147 if (! (cfun->curr_properties & PROP_cfg))
2148 flags &= ~TDF_BLOCKS;
2149
6fb5fa3c
DB
2150 if (df)
2151 df_dump_start (outf);
2152
c4669594 2153 if (flags & TDF_BLOCKS)
ca6c03ca 2154 {
4f42035e 2155 FOR_EACH_BB_REVERSE_FN (bb, cfun)
ca6c03ca 2156 {
c4669594
SB
2157 rtx x;
2158
2159 start[INSN_UID (BB_HEAD (bb))] = bb;
2160 end[INSN_UID (BB_END (bb))] = bb;
2161 for (x = BB_HEAD (bb); x != NULL_RTX; x = NEXT_INSN (x))
2162 {
2163 enum bb_state state = IN_MULTIPLE_BB;
5f0d2358 2164
c4669594
SB
2165 if (in_bb_p[INSN_UID (x)] == NOT_IN_BB)
2166 state = IN_ONE_BB;
2167 in_bb_p[INSN_UID (x)] = state;
ca6c03ca 2168
c4669594
SB
2169 if (x == BB_END (bb))
2170 break;
2171 }
ca6c03ca
JH
2172 }
2173 }
2174
2175 for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
2176 {
c4669594
SB
2177 if (flags & TDF_BLOCKS)
2178 {
2179 bb = start[INSN_UID (tmp_rtx)];
2180 if (bb != NULL)
2181 {
2182 dump_bb_info (outf, bb, 0, dump_flags | TDF_COMMENT, true, false);
2183 if (df && (flags & TDF_DETAILS))
2184 df_dump_top (bb, outf);
2185 }
ca6c03ca 2186
c4669594
SB
2187 if (in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB
2188 && !NOTE_P (tmp_rtx)
2189 && !BARRIER_P (tmp_rtx))
2190 fprintf (outf, ";; Insn is not within a basic block\n");
2191 else if (in_bb_p[INSN_UID (tmp_rtx)] == IN_MULTIPLE_BB)
2192 fprintf (outf, ";; Insn is in multiple basic blocks\n");
2193 }
ca6c03ca 2194
7b19209f
SB
2195 if (flags & TDF_DETAILS)
2196 df_dump_insn_top (tmp_rtx, outf);
a315c44c
SB
2197 if (! (flags & TDF_SLIM))
2198 print_rtl_single (outf, tmp_rtx);
2199 else
2200 dump_insn_slim (outf, tmp_rtx);
7b19209f
SB
2201 if (flags & TDF_DETAILS)
2202 df_dump_insn_bottom (tmp_rtx, outf);
ca6c03ca 2203
c4669594
SB
2204 if (flags & TDF_BLOCKS)
2205 {
2206 bb = end[INSN_UID (tmp_rtx)];
2207 if (bb != NULL)
2208 {
2209 dump_bb_info (outf, bb, 0, dump_flags | TDF_COMMENT, false, true);
2210 if (df && (flags & TDF_DETAILS))
2211 df_dump_bottom (bb, outf);
1b03a96d 2212 putc ('\n', outf);
c4669594
SB
2213 }
2214 }
ca6c03ca
JH
2215 }
2216
2217 free (start);
2218 free (end);
2219 free (in_bb_p);
2220 }
ca6c03ca
JH
2221}
2222\f
532aafad
SB
2223/* Update the branch probability of BB if a REG_BR_PROB is present. */
2224
b446e5a2 2225void
d329e058 2226update_br_prob_note (basic_block bb)
b446e5a2
JH
2227{
2228 rtx note;
4b4bf941 2229 if (!JUMP_P (BB_END (bb)))
b446e5a2 2230 return;
a813c111 2231 note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX);
e5af9ddd 2232 if (!note || XINT (note, 0) == BRANCH_EDGE (bb)->probability)
b446e5a2 2233 return;
e5af9ddd 2234 XINT (note, 0) = BRANCH_EDGE (bb)->probability;
b446e5a2 2235}
96370780
MK
2236
2237/* Get the last insn associated with block BB (that includes barriers and
2238 tablejumps after BB). */
2239rtx
2240get_last_bb_insn (basic_block bb)
2241{
8942ee0f 2242 rtx_jump_table_data *table;
96370780
MK
2243 rtx tmp;
2244 rtx end = BB_END (bb);
2245
2246 /* Include any jump table following the basic block. */
8942ee0f
DM
2247 if (tablejump_p (end, NULL, &table))
2248 end = table;
96370780
MK
2249
2250 /* Include any barriers that may follow the basic block. */
1e211590 2251 tmp = next_nonnote_insn_bb (end);
96370780
MK
2252 while (tmp && BARRIER_P (tmp))
2253 {
2254 end = tmp;
1e211590 2255 tmp = next_nonnote_insn_bb (end);
96370780
MK
2256 }
2257
2258 return end;
2259}
af205f67 2260
600b5b1d
TJ
2261/* Sanity check partition hotness to ensure that basic blocks in
2262   the cold partition don't dominate basic blocks in the hot partition.
2263 If FLAG_ONLY is true, report violations as errors. Otherwise
2264 re-mark the dominated blocks as cold, since this is run after
2265 cfg optimizations that may make hot blocks previously reached
2266 by both hot and cold blocks now only reachable along cold paths. */
2267
2268static vec<basic_block>
2269find_partition_fixes (bool flag_only)
2270{
2271 basic_block bb;
2272 vec<basic_block> bbs_in_cold_partition = vNULL;
2273 vec<basic_block> bbs_to_fix = vNULL;
2274
2275 /* Callers check this. */
2276 gcc_checking_assert (crtl->has_bb_partition);
2277
11cd3bed 2278 FOR_EACH_BB_FN (bb, cfun)
600b5b1d
TJ
2279 if ((BB_PARTITION (bb) == BB_COLD_PARTITION))
2280 bbs_in_cold_partition.safe_push (bb);
2281
2282 if (bbs_in_cold_partition.is_empty ())
2283 return vNULL;
2284
2285 bool dom_calculated_here = !dom_info_available_p (CDI_DOMINATORS);
2286
2287 if (dom_calculated_here)
2288 calculate_dominance_info (CDI_DOMINATORS);
2289
2290 while (! bbs_in_cold_partition.is_empty ())
2291 {
2292 bb = bbs_in_cold_partition.pop ();
2293 /* Any blocks dominated by a block in the cold section
2294 must also be cold. */
2295 basic_block son;
2296 for (son = first_dom_son (CDI_DOMINATORS, bb);
2297 son;
2298 son = next_dom_son (CDI_DOMINATORS, son))
2299 {
2300 /* If son is not yet cold, then mark it cold here and
2301 enqueue it for further processing. */
2302 if ((BB_PARTITION (son) != BB_COLD_PARTITION))
2303 {
2304 if (flag_only)
2305 error ("non-cold basic block %d dominated "
2306 "by a block in the cold partition (%d)", son->index, bb->index);
2307 else
2308 BB_SET_PARTITION (son, BB_COLD_PARTITION);
2309 bbs_to_fix.safe_push (son);
2310 bbs_in_cold_partition.safe_push (son);
2311 }
2312 }
2313 }
2314
2315 if (dom_calculated_here)
2316 free_dominance_info (CDI_DOMINATORS);
2317
2318 return bbs_to_fix;
2319}
2320
2321/* Perform cleanup on the hot/cold bb partitioning after optimization
2322 passes that modify the cfg. */
2323
2324void
2325fixup_partitions (void)
2326{
2327 basic_block bb;
2328
2329 if (!crtl->has_bb_partition)
2330 return;
2331
2332 /* Delete any blocks that became unreachable and weren't
2333 already cleaned up, for example during edge forwarding
2334 and convert_jumps_to_returns. This will expose more
2335 opportunities for fixing the partition boundaries here.
2336 Also, the calculation of the dominance graph during verification
2337 will assert if there are unreachable nodes. */
2338 delete_unreachable_blocks ();
2339
2340 /* If there are partitions, do a sanity check on them: A basic block in
2341   a cold partition cannot dominate a basic block in a hot partition.
2342 Fixup any that now violate this requirement, as a result of edge
2343 forwarding and unreachable block deletion.  */
2344 vec<basic_block> bbs_to_fix = find_partition_fixes (false);
2345
2346 /* Do the partition fixup after all necessary blocks have been converted to
2347 cold, so that we only update the region crossings the minimum number of
2348 places, which can require forcing edges to be non fallthru. */
2349 while (! bbs_to_fix.is_empty ())
2350 {
2351 bb = bbs_to_fix.pop ();
2352 fixup_new_cold_bb (bb);
2353 }
2354}
2355
af205f67
TJ
2356/* Verify, in the basic block chain, that there is at most one switch
2357 between hot/cold partitions. This condition will not be true until
2358 after reorder_basic_blocks is called. */
2359
251a41b9 2360static int
af205f67
TJ
2361verify_hot_cold_block_grouping (void)
2362{
2363 basic_block bb;
2364 int err = 0;
2365 bool switched_sections = false;
2366 int current_partition = BB_UNPARTITIONED;
2367
3371a64f
TJ
2368 /* Even after bb reordering is complete, we go into cfglayout mode
2369 again (in compgoto). Ensure we don't call this before going back
2370 into linearized RTL when any layout fixes would have been committed. */
2371 if (!crtl->bb_reorder_complete
c3284718 2372 || current_ir_type () != IR_RTL_CFGRTL)
251a41b9 2373 return err;
af205f67 2374
11cd3bed 2375 FOR_EACH_BB_FN (bb, cfun)
af205f67
TJ
2376 {
2377 if (current_partition != BB_UNPARTITIONED
2378 && BB_PARTITION (bb) != current_partition)
2379 {
2380 if (switched_sections)
2381 {
2382 error ("multiple hot/cold transitions found (bb %i)",
2383 bb->index);
2384 err = 1;
2385 }
2386 else
2387 switched_sections = true;
2388
2389 if (!crtl->has_bb_partition)
2390 error ("partition found but function partition flag not set");
2391 }
2392 current_partition = BB_PARTITION (bb);
2393 }
2394
251a41b9 2395 return err;
af205f67 2396}
b446e5a2 2397\f
ca6c03ca 2398
251a41b9
TJ
2399/* Perform several checks on the edges out of each block, such as
2400 the consistency of the branch probabilities, the correctness
2401 of hot/cold partition crossing edges, and the number of expected
600b5b1d
TJ
2402 successor edges. Also verify that the dominance relationship
2403 between hot/cold blocks is sane. */
f470c378 2404
10e9fecc 2405static int
251a41b9 2406rtl_verify_edges (void)
ca6c03ca 2407{
10e9fecc 2408 int err = 0;
94eb5ddb 2409 basic_block bb;
ca6c03ca 2410
4f42035e 2411 FOR_EACH_BB_REVERSE_FN (bb, cfun)
ca6c03ca 2412 {
f99ffaa3
EB
2413 int n_fallthru = 0, n_branch = 0, n_abnormal_call = 0, n_sibcall = 0;
2414 int n_eh = 0, n_abnormal = 0;
3cf54412 2415 edge e, fallthru = NULL;
628f6a4e 2416 edge_iterator ei;
251a41b9 2417 rtx note;
3371a64f 2418 bool has_crossing_edge = false;
ca6c03ca 2419
2085a21f 2420 if (JUMP_P (BB_END (bb))
a813c111 2421 && (note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX))
628f6a4e 2422 && EDGE_COUNT (bb->succs) >= 2
a813c111 2423 && any_condjump_p (BB_END (bb)))
5a1a3e5e 2424 {
e5af9ddd 2425 if (XINT (note, 0) != BRANCH_EDGE (bb)->probability
0a6a6ac9 2426 && profile_status_for_fn (cfun) != PROFILE_ABSENT)
5a1a3e5e 2427 {
e5af9ddd
RS
2428 error ("verify_flow_info: REG_BR_PROB does not match cfg %i %i",
2429 XINT (note, 0), BRANCH_EDGE (bb)->probability);
5a1a3e5e
JH
2430 err = 1;
2431 }
2432 }
251a41b9 2433
628f6a4e 2434 FOR_EACH_EDGE (e, ei, bb->succs)
ca6c03ca 2435 {
0be7e7a6
RH
2436 bool is_crossing;
2437
ca6c03ca 2438 if (e->flags & EDGE_FALLTHRU)
0be7e7a6
RH
2439 n_fallthru++, fallthru = e;
2440
2441 is_crossing = (BB_PARTITION (e->src) != BB_PARTITION (e->dest)
fefa31b5
DM
2442 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
2443 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun));
3371a64f 2444 has_crossing_edge |= is_crossing;
0be7e7a6 2445 if (e->flags & EDGE_CROSSING)
750054a2 2446 {
0be7e7a6
RH
2447 if (!is_crossing)
2448 {
2449 error ("EDGE_CROSSING incorrectly set across same section");
2450 err = 1;
2451 }
2452 if (e->flags & EDGE_FALLTHRU)
2453 {
f99ffaa3 2454 error ("fallthru edge crosses section boundary in bb %i",
750054a2
CT
2455 e->src->index);
2456 err = 1;
2457 }
0be7e7a6
RH
2458 if (e->flags & EDGE_EH)
2459 {
f99ffaa3 2460 error ("EH edge crosses section boundary in bb %i",
0be7e7a6
RH
2461 e->src->index);
2462 err = 1;
2463 }
339ba33b 2464 if (JUMP_P (BB_END (bb)) && !CROSSING_JUMP_P (BB_END (bb)))
3371a64f
TJ
2465 {
2466 error ("No region crossing jump at section boundary in bb %i",
2467 bb->index);
2468 err = 1;
2469 }
0be7e7a6
RH
2470 }
2471 else if (is_crossing)
2472 {
2473 error ("EDGE_CROSSING missing across section boundary");
2474 err = 1;
750054a2 2475 }
3dec4024 2476
65f43cdf
ZD
2477 if ((e->flags & ~(EDGE_DFS_BACK
2478 | EDGE_CAN_FALLTHRU
2479 | EDGE_IRREDUCIBLE_LOOP
9beb1c84 2480 | EDGE_LOOP_EXIT
7e872b90
JJ
2481 | EDGE_CROSSING
2482 | EDGE_PRESERVE)) == 0)
3dec4024
JH
2483 n_branch++;
2484
2485 if (e->flags & EDGE_ABNORMAL_CALL)
f99ffaa3
EB
2486 n_abnormal_call++;
2487
2488 if (e->flags & EDGE_SIBCALL)
2489 n_sibcall++;
3dec4024
JH
2490
2491 if (e->flags & EDGE_EH)
2492 n_eh++;
f99ffaa3
EB
2493
2494 if (e->flags & EDGE_ABNORMAL)
3dec4024 2495 n_abnormal++;
ca6c03ca 2496 }
5f0d2358 2497
3371a64f 2498 if (!has_crossing_edge
339ba33b
RS
2499 && JUMP_P (BB_END (bb))
2500 && CROSSING_JUMP_P (BB_END (bb)))
3371a64f
TJ
2501 {
2502 print_rtl_with_bb (stderr, get_insns (), TDF_RTL | TDF_BLOCKS | TDF_DETAILS);
2503 error ("Region crossing jump across same section in bb %i",
2504 bb->index);
2505 err = 1;
2506 }
2507
1d65f45c 2508 if (n_eh && !find_reg_note (BB_END (bb), REG_EH_REGION, NULL_RTX))
3dec4024 2509 {
f99ffaa3 2510 error ("missing REG_EH_REGION note at the end of bb %i", bb->index);
3dec4024
JH
2511 err = 1;
2512 }
1d65f45c
RH
2513 if (n_eh > 1)
2514 {
f99ffaa3 2515 error ("too many exception handling edges in bb %i", bb->index);
1d65f45c
RH
2516 err = 1;
2517 }
3dec4024 2518 if (n_branch
4b4bf941 2519 && (!JUMP_P (BB_END (bb))
a813c111
SB
2520 || (n_branch > 1 && (any_uncondjump_p (BB_END (bb))
2521 || any_condjump_p (BB_END (bb))))))
3dec4024 2522 {
ab532386 2523 error ("too many outgoing branch edges from bb %i", bb->index);
3dec4024
JH
2524 err = 1;
2525 }
a813c111 2526 if (n_fallthru && any_uncondjump_p (BB_END (bb)))
3dec4024 2527 {
f99ffaa3 2528 error ("fallthru edge after unconditional jump in bb %i", bb->index);
3dec4024
JH
2529 err = 1;
2530 }
a813c111 2531 if (n_branch != 1 && any_uncondjump_p (BB_END (bb)))
3dec4024 2532 {
f99ffaa3
EB
2533 error ("wrong number of branch edges after unconditional jump"
2534 " in bb %i", bb->index);
3dec4024
JH
2535 err = 1;
2536 }
a813c111 2537 if (n_branch != 1 && any_condjump_p (BB_END (bb))
c11fd0b2 2538 && JUMP_LABEL (BB_END (bb)) != BB_HEAD (fallthru->dest))
3dec4024 2539 {
f99ffaa3
EB
2540 error ("wrong amount of branch edges after conditional jump"
2541 " in bb %i", bb->index);
2542 err = 1;
2543 }
2544 if (n_abnormal_call && !CALL_P (BB_END (bb)))
2545 {
2546 error ("abnormal call edges for non-call insn in bb %i", bb->index);
3dec4024
JH
2547 err = 1;
2548 }
f99ffaa3 2549 if (n_sibcall && !CALL_P (BB_END (bb)))
3dec4024 2550 {
f99ffaa3 2551 error ("sibcall edges for non-call insn in bb %i", bb->index);
3dec4024
JH
2552 err = 1;
2553 }
f99ffaa3
EB
2554 if (n_abnormal > n_eh
2555 && !(CALL_P (BB_END (bb))
2556 && n_abnormal == n_abnormal_call + n_sibcall)
4b4bf941 2557 && (!JUMP_P (BB_END (bb))
a813c111
SB
2558 || any_condjump_p (BB_END (bb))
2559 || any_uncondjump_p (BB_END (bb))))
3dec4024 2560 {
ab532386 2561 error ("abnormal edges for no purpose in bb %i", bb->index);
3dec4024
JH
2562 err = 1;
2563 }
251a41b9 2564 }
f87c27b4 2565
600b5b1d
TJ
2566 /* If there are partitions, do a sanity check on them: A basic block in
2567   a cold partition cannot dominate a basic block in a hot partition.  */
2568 if (crtl->has_bb_partition && !err)
2569 {
2570 vec<basic_block> bbs_to_fix = find_partition_fixes (true);
2571 err = !bbs_to_fix.is_empty ();
2572 }
2573
251a41b9
TJ
2574 /* Clean up. */
2575 return err;
2576}
5f0d2358 2577
251a41b9
TJ
2578/* Checks on the instructions within blocks. Currently checks that each
2579 block starts with a basic block note, and that basic block notes and
2580 control flow jumps are not found in the middle of the block. */
ca6c03ca 2581
251a41b9
TJ
2582static int
2583rtl_verify_bb_insns (void)
2584{
2585 rtx x;
2586 int err = 0;
2587 basic_block bb;
2588
4f42035e 2589 FOR_EACH_BB_REVERSE_FN (bb, cfun)
251a41b9
TJ
2590 {
2591 /* Now check the header of basic
c22cacf3 2592 block. It ought to contain optional CODE_LABEL followed
ca6c03ca 2593 by NOTE_BASIC_BLOCK. */
a813c111 2594 x = BB_HEAD (bb);
4b4bf941 2595 if (LABEL_P (x))
ca6c03ca 2596 {
a813c111 2597 if (BB_END (bb) == x)
ca6c03ca
JH
2598 {
2599 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
0b17ab2f 2600 bb->index);
ca6c03ca
JH
2601 err = 1;
2602 }
5f0d2358 2603
ca6c03ca
JH
2604 x = NEXT_INSN (x);
2605 }
5f0d2358 2606
ca6c03ca
JH
2607 if (!NOTE_INSN_BASIC_BLOCK_P (x) || NOTE_BASIC_BLOCK (x) != bb)
2608 {
2609 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
0b17ab2f 2610 bb->index);
ca6c03ca
JH
2611 err = 1;
2612 }
2613
a813c111 2614 if (BB_END (bb) == x)
49243025 2615 /* Do checks for empty blocks here. */
5f0d2358 2616 ;
ca6c03ca 2617 else
5f0d2358
RK
2618 for (x = NEXT_INSN (x); x; x = NEXT_INSN (x))
2619 {
2620 if (NOTE_INSN_BASIC_BLOCK_P (x))
2621 {
2622 error ("NOTE_INSN_BASIC_BLOCK %d in middle of basic block %d",
0b17ab2f 2623 INSN_UID (x), bb->index);
5f0d2358
RK
2624 err = 1;
2625 }
2626
a813c111 2627 if (x == BB_END (bb))
5f0d2358 2628 break;
ca6c03ca 2629
83fd323c 2630 if (control_flow_insn_p (x))
5f0d2358 2631 {
0b17ab2f 2632 error ("in basic block %d:", bb->index);
5f0d2358
RK
2633 fatal_insn ("flow control insn inside a basic block", x);
2634 }
2635 }
ca6c03ca
JH
2636 }
2637
251a41b9
TJ
2638 /* Clean up. */
2639 return err;
2640}
2641
2642/* Verify that block pointers for instructions in basic blocks, headers and
2643 footers are set appropriately. */
2644
2645static int
2646rtl_verify_bb_pointers (void)
2647{
2648 int err = 0;
2649 basic_block bb;
2650
2651 /* Check the general integrity of the basic blocks. */
4f42035e 2652 FOR_EACH_BB_REVERSE_FN (bb, cfun)
251a41b9
TJ
2653 {
2654 rtx insn;
2655
2656 if (!(bb->flags & BB_RTL))
2657 {
2658 error ("BB_RTL flag not set for block %d", bb->index);
2659 err = 1;
2660 }
2661
2662 FOR_BB_INSNS (bb, insn)
2663 if (BLOCK_FOR_INSN (insn) != bb)
2664 {
2665 error ("insn %d basic block pointer is %d, should be %d",
2666 INSN_UID (insn),
2667 BLOCK_FOR_INSN (insn) ? BLOCK_FOR_INSN (insn)->index : 0,
2668 bb->index);
2669 err = 1;
2670 }
2671
2672 for (insn = BB_HEADER (bb); insn; insn = NEXT_INSN (insn))
2673 if (!BARRIER_P (insn)
2674 && BLOCK_FOR_INSN (insn) != NULL)
2675 {
2676 error ("insn %d in header of bb %d has non-NULL basic block",
2677 INSN_UID (insn), bb->index);
2678 err = 1;
2679 }
2680 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2681 if (!BARRIER_P (insn)
2682 && BLOCK_FOR_INSN (insn) != NULL)
2683 {
2684 error ("insn %d in footer of bb %d has non-NULL basic block",
2685 INSN_UID (insn), bb->index);
2686 err = 1;
2687 }
2688 }
af205f67 2689
10e9fecc 2690 /* Clean up. */
10e9fecc
JH
2691 return err;
2692}
5f0d2358 2693
10e9fecc
JH
2694/* Verify the CFG and RTL consistency common for both underlying RTL and
2695 cfglayout RTL.
5f0d2358 2696
10e9fecc 2697 Currently it does following checks:
251a41b9
TJ
2698
2699 - overlapping of basic blocks
2700 - insns with wrong BLOCK_FOR_INSN pointers
2701 - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note)
2702 - tails of basic blocks (ensure that boundary is necessary)
2703 - scans body of the basic block for JUMP_INSN, CODE_LABEL
2704 and NOTE_INSN_BASIC_BLOCK
2705 - verify that no fall_thru edge crosses hot/cold partition boundaries
2706 - verify that there are no pending RTL branch predictions
600b5b1d 2707 - verify that hot blocks are not dominated by cold blocks
251a41b9
TJ
2708
2709 In future it can be extended check a lot of other stuff as well
2710 (reachability of basic blocks, life information, etc. etc.). */
9eab6785 2711
10e9fecc 2712static int
251a41b9
TJ
2713rtl_verify_flow_info_1 (void)
2714{
2715 int err = 0;
2716
2717 err |= rtl_verify_bb_pointers ();
2718
2719 err |= rtl_verify_bb_insns ();
2720
2721 err |= rtl_verify_edges ();
2722
251a41b9
TJ
2723 return err;
2724}
2725
2726/* Walk the instruction chain and verify that bb head/end pointers
2727 are correct, and that instructions are in exactly one bb and have
2728 correct block pointers. */
2729
2730static int
2731rtl_verify_bb_insn_chain (void)
10e9fecc
JH
2732{
2733 basic_block bb;
251a41b9 2734 int err = 0;
10e9fecc 2735 rtx x;
9eab6785
SB
2736 rtx last_head = get_last_insn ();
2737 basic_block *bb_info;
9eab6785
SB
2738 const int max_uid = get_max_uid ();
2739
2740 bb_info = XCNEWVEC (basic_block, max_uid);
ca6c03ca 2741
4f42035e 2742 FOR_EACH_BB_REVERSE_FN (bb, cfun)
10e9fecc 2743 {
9eab6785
SB
2744 rtx head = BB_HEAD (bb);
2745 rtx end = BB_END (bb);
628f6a4e 2746
9eab6785 2747 for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
a7b87f73
ZD
2748 {
2749 /* Verify the end of the basic block is in the INSN chain. */
2750 if (x == end)
2751 break;
2752
251a41b9
TJ
2753 /* And that the code outside of basic blocks has NULL bb field. */
2754 if (!BARRIER_P (x)
2755 && BLOCK_FOR_INSN (x) != NULL)
2756 {
2757 error ("insn %d outside of basic blocks has non-NULL bb field",
2758 INSN_UID (x));
2759 err = 1;
2760 }
a7b87f73 2761 }
9eab6785
SB
2762
2763 if (!x)
2764 {
2765 error ("end insn %d for block %d not found in the insn stream",
2766 INSN_UID (end), bb->index);
2767 err = 1;
2768 }
2769
2770 /* Work backwards from the end to the head of the basic block
2771 to verify the head is in the RTL chain. */
2772 for (; x != NULL_RTX; x = PREV_INSN (x))
a00d11f0 2773 {
9eab6785
SB
2774 /* While walking over the insn chain, verify insns appear
2775 in only one basic block. */
2776 if (bb_info[INSN_UID (x)] != NULL)
2777 {
2778 error ("insn %d is in multiple basic blocks (%d and %d)",
2779 INSN_UID (x), bb->index, bb_info[INSN_UID (x)]->index);
2780 err = 1;
2781 }
2782
2783 bb_info[INSN_UID (x)] = bb;
2784
2785 if (x == head)
2786 break;
2787 }
2788 if (!x)
2789 {
2790 error ("head insn %d for block %d not found in the insn stream",
2791 INSN_UID (head), bb->index);
a00d11f0
JH
2792 err = 1;
2793 }
2794
a7b87f73 2795 last_head = PREV_INSN (x);
251a41b9
TJ
2796 }
2797
2798 for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
2799 {
2800 /* Check that the code before the first basic block has NULL
2801 bb field. */
2802 if (!BARRIER_P (x)
2803 && BLOCK_FOR_INSN (x) != NULL)
2804 {
2805 error ("insn %d outside of basic blocks has non-NULL bb field",
2806 INSN_UID (x));
2807 err = 1;
2808 }
2809 }
2810 free (bb_info);
2811
2812 return err;
2813}
2814
2815/* Verify that fallthru edges point to adjacent blocks in layout order and
2816 that barriers exist after non-fallthru blocks. */
2817
2818static int
2819rtl_verify_fallthru (void)
2820{
2821 basic_block bb;
2822 int err = 0;
2823
4f42035e 2824 FOR_EACH_BB_REVERSE_FN (bb, cfun)
251a41b9
TJ
2825 {
2826 edge e;
9eab6785 2827
0fd4b31d 2828 e = find_fallthru_edge (bb->succs);
10e9fecc
JH
2829 if (!e)
2830 {
2831 rtx insn;
2832
2833 /* Ensure existence of barrier in BB with no fallthru edges. */
468059bc
DD
2834 for (insn = NEXT_INSN (BB_END (bb)); ; insn = NEXT_INSN (insn))
2835 {
2836 if (!insn || NOTE_INSN_BASIC_BLOCK_P (insn))
10e9fecc
JH
2837 {
2838 error ("missing barrier after block %i", bb->index);
2839 err = 1;
2840 break;
2841 }
468059bc
DD
2842 if (BARRIER_P (insn))
2843 break;
2844 }
10e9fecc 2845 }
fefa31b5
DM
2846 else if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
2847 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
c22cacf3 2848 {
10e9fecc
JH
2849 rtx insn;
2850
2851 if (e->src->next_bb != e->dest)
2852 {
2853 error
2854 ("verify_flow_info: Incorrect blocks for fallthru %i->%i",
2855 e->src->index, e->dest->index);
2856 err = 1;
2857 }
2858 else
a813c111 2859 for (insn = NEXT_INSN (BB_END (e->src)); insn != BB_HEAD (e->dest);
10e9fecc 2860 insn = NEXT_INSN (insn))
6be85b25 2861 if (BARRIER_P (insn) || INSN_P (insn))
10e9fecc
JH
2862 {
2863 error ("verify_flow_info: Incorrect fallthru %i->%i",
2864 e->src->index, e->dest->index);
2865 fatal_insn ("wrong insn in the fallthru edge", insn);
2866 err = 1;
2867 }
c22cacf3 2868 }
10e9fecc 2869 }
ca6c03ca 2870
251a41b9
TJ
2871 return err;
2872}
2873
2874/* Verify that blocks are laid out in consecutive order. While walking the
2875 instructions, verify that all expected instructions are inside the basic
2876 blocks, and that all returns are followed by barriers. */
2877
2878static int
2879rtl_verify_bb_layout (void)
2880{
2881 basic_block bb;
2882 int err = 0;
2883 rtx x;
2884 int num_bb_notes;
2885 const rtx rtx_first = get_insns ();
fefa31b5 2886 basic_block last_bb_seen = ENTRY_BLOCK_PTR_FOR_FN (cfun), curr_bb = NULL;
9eab6785 2887
ca6c03ca 2888 num_bb_notes = 0;
fefa31b5 2889 last_bb_seen = ENTRY_BLOCK_PTR_FOR_FN (cfun);
e0082a72 2890
5f0d2358 2891 for (x = rtx_first; x; x = NEXT_INSN (x))
ca6c03ca
JH
2892 {
2893 if (NOTE_INSN_BASIC_BLOCK_P (x))
2894 {
bf77398c 2895 bb = NOTE_BASIC_BLOCK (x);
5f0d2358 2896
ca6c03ca 2897 num_bb_notes++;
e0082a72 2898 if (bb != last_bb_seen->next_bb)
10e9fecc 2899 internal_error ("basic blocks not laid down consecutively");
ca6c03ca 2900
10e9fecc 2901 curr_bb = last_bb_seen = bb;
ca6c03ca
JH
2902 }
2903
10e9fecc 2904 if (!curr_bb)
ca6c03ca
JH
2905 {
2906 switch (GET_CODE (x))
2907 {
2908 case BARRIER:
2909 case NOTE:
2910 break;
2911
2912 case CODE_LABEL:
39718607 2913 /* An ADDR_VEC is placed outside any basic block. */
ca6c03ca 2914 if (NEXT_INSN (x)
481683e1 2915 && JUMP_TABLE_DATA_P (NEXT_INSN (x)))
5f0d2358 2916 x = NEXT_INSN (x);
ca6c03ca
JH
2917
2918 /* But in any case, non-deletable labels can appear anywhere. */
2919 break;
2920
2921 default:
1f978f5f 2922 fatal_insn ("insn outside basic block", x);
ca6c03ca
JH
2923 }
2924 }
2925
26cae194 2926 if (JUMP_P (x)
ca6c03ca 2927 && returnjump_p (x) && ! condjump_p (x)
15eb3a2e 2928 && ! (next_nonnote_insn (x) && BARRIER_P (next_nonnote_insn (x))))
1f978f5f 2929 fatal_insn ("return not followed by barrier", x);
251a41b9 2930
a813c111 2931 if (curr_bb && x == BB_END (curr_bb))
10e9fecc 2932 curr_bb = NULL;
ca6c03ca
JH
2933 }
2934
0cae8d31 2935 if (num_bb_notes != n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS)
ca6c03ca 2936 internal_error
0b17ab2f 2937 ("number of bb notes in insn chain (%d) != n_basic_blocks (%d)",
0cae8d31 2938 num_bb_notes, n_basic_blocks_for_fn (cfun));
ca6c03ca 2939
10e9fecc 2940 return err;
ca6c03ca 2941}
251a41b9
TJ
2942
2943/* Verify the CFG and RTL consistency common for both underlying RTL and
2944 cfglayout RTL, plus consistency checks specific to linearized RTL mode.
2945
2946 Currently it does following checks:
2947 - all checks of rtl_verify_flow_info_1
2948 - test head/end pointers
2949 - check that blocks are laid out in consecutive order
2950 - check that all insns are in the basic blocks
2951 (except the switch handling code, barriers and notes)
2952 - check that all returns are followed by barriers
600b5b1d
TJ
2953 - check that all fallthru edge points to the adjacent blocks
2954 - verify that there is a single hot/cold partition boundary after bbro */
251a41b9
TJ
2955
2956static int
2957rtl_verify_flow_info (void)
2958{
2959 int err = 0;
2960
2961 err |= rtl_verify_flow_info_1 ();
2962
2963 err |= rtl_verify_bb_insn_chain ();
2964
2965 err |= rtl_verify_fallthru ();
2966
2967 err |= rtl_verify_bb_layout ();
2968
3371a64f
TJ
2969 err |= verify_hot_cold_block_grouping ();
2970
251a41b9
TJ
2971 return err;
2972}
ca6c03ca 2973\f
eaec9b3d 2974/* Assume that the preceding pass has possibly eliminated jump instructions
ca6c03ca
JH
2975 or converted the unconditional jumps. Eliminate the edges from CFG.
2976 Return true if any edges are eliminated. */
2977
2978bool
d329e058 2979purge_dead_edges (basic_block bb)
ca6c03ca 2980{
628f6a4e 2981 edge e;
a813c111 2982 rtx insn = BB_END (bb), note;
ca6c03ca 2983 bool purged = false;
628f6a4e
BE
2984 bool found;
2985 edge_iterator ei;
ca6c03ca 2986
b5b8b0ac
AO
2987 if (DEBUG_INSN_P (insn) && insn != BB_HEAD (bb))
2988 do
2989 insn = PREV_INSN (insn);
2990 while ((DEBUG_INSN_P (insn) || NOTE_P (insn)) && insn != BB_HEAD (bb));
2991
70da1d03 2992 /* If this instruction cannot trap, remove REG_EH_REGION notes. */
4b4bf941 2993 if (NONJUMP_INSN_P (insn)
70da1d03
JH
2994 && (note = find_reg_note (insn, REG_EH_REGION, NULL)))
2995 {
2996 rtx eqnote;
2997
2998 if (! may_trap_p (PATTERN (insn))
2999 || ((eqnote = find_reg_equal_equiv_note (insn))
3000 && ! may_trap_p (XEXP (eqnote, 0))))
3001 remove_note (insn, note);
3002 }
3003
546c093e 3004 /* Cleanup abnormal edges caused by exceptions or non-local gotos. */
628f6a4e 3005 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
546c093e 3006 {
1d65f45c
RH
3007 bool remove = false;
3008
e5f9a909
JW
3009 /* There are three types of edges we need to handle correctly here: EH
3010 edges, abnormal call EH edges, and abnormal call non-EH edges. The
3011 latter can appear when nonlocal gotos are used. */
1d65f45c 3012 if (e->flags & EDGE_ABNORMAL_CALL)
546c093e 3013 {
1d65f45c
RH
3014 if (!CALL_P (insn))
3015 remove = true;
3016 else if (can_nonlocal_goto (insn))
3017 ;
3018 else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
3019 ;
0a35513e
AH
3020 else if (flag_tm && find_reg_note (insn, REG_TM, NULL))
3021 ;
1d65f45c
RH
3022 else
3023 remove = true;
546c093e 3024 }
1d65f45c
RH
3025 else if (e->flags & EDGE_EH)
3026 remove = !can_throw_internal (insn);
3027
3028 if (remove)
546c093e 3029 {
1d65f45c
RH
3030 remove_edge (e);
3031 df_set_bb_dirty (bb);
3032 purged = true;
546c093e
RH
3033 }
3034 else
1d65f45c 3035 ei_next (&ei);
546c093e 3036 }
5f0d2358 3037
4b4bf941 3038 if (JUMP_P (insn))
ca6c03ca
JH
3039 {
3040 rtx note;
3041 edge b,f;
628f6a4e 3042 edge_iterator ei;
5f0d2358 3043
ca6c03ca
JH
3044 /* We do care only about conditional jumps and simplejumps. */
3045 if (!any_condjump_p (insn)
3046 && !returnjump_p (insn)
3047 && !simplejump_p (insn))
c51d95ec 3048 return purged;
5f0d2358 3049
5a1a3e5e
JH
3050 /* Branch probability/prediction notes are defined only for
3051 condjumps. We've possibly turned condjump into simplejump. */
3052 if (simplejump_p (insn))
3053 {
3054 note = find_reg_note (insn, REG_BR_PROB, NULL);
3055 if (note)
3056 remove_note (insn, note);
3057 while ((note = find_reg_note (insn, REG_BR_PRED, NULL)))
3058 remove_note (insn, note);
3059 }
3060
628f6a4e 3061 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
ca6c03ca 3062 {
7fcd7218
JH
3063 /* Avoid abnormal flags to leak from computed jumps turned
3064 into simplejumps. */
f87c27b4 3065
0e1638d4 3066 e->flags &= ~EDGE_ABNORMAL;
7fcd7218 3067
5a566bed
MM
3068 /* See if this edge is one we should keep. */
3069 if ((e->flags & EDGE_FALLTHRU) && any_condjump_p (insn))
3070 /* A conditional jump can fall through into the next
3071 block, so we should keep the edge. */
628f6a4e
BE
3072 {
3073 ei_next (&ei);
3074 continue;
3075 }
fefa31b5 3076 else if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
a813c111 3077 && BB_HEAD (e->dest) == JUMP_LABEL (insn))
5a566bed
MM
3078 /* If the destination block is the target of the jump,
3079 keep the edge. */
628f6a4e
BE
3080 {
3081 ei_next (&ei);
3082 continue;
3083 }
fefa31b5
DM
3084 else if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
3085 && returnjump_p (insn))
5a566bed
MM
3086 /* If the destination block is the exit block, and this
3087 instruction is a return, then keep the edge. */
628f6a4e
BE
3088 {
3089 ei_next (&ei);
3090 continue;
3091 }
5a566bed
MM
3092 else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
3093 /* Keep the edges that correspond to exceptions thrown by
0b75beaa
EB
3094 this instruction and rematerialize the EDGE_ABNORMAL
3095 flag we just cleared above. */
3096 {
3097 e->flags |= EDGE_ABNORMAL;
628f6a4e 3098 ei_next (&ei);
0b75beaa
EB
3099 continue;
3100 }
5f0d2358 3101
5a566bed 3102 /* We do not need this edge. */
6fb5fa3c 3103 df_set_bb_dirty (bb);
ca6c03ca
JH
3104 purged = true;
3105 remove_edge (e);
3106 }
5f0d2358 3107
628f6a4e 3108 if (EDGE_COUNT (bb->succs) == 0 || !purged)
c51d95ec 3109 return purged;
5f0d2358 3110
c263766c
RH
3111 if (dump_file)
3112 fprintf (dump_file, "Purged edges from bb %i\n", bb->index);
5f0d2358 3113
ca6c03ca
JH
3114 if (!optimize)
3115 return purged;
3116
3117 /* Redistribute probabilities. */
c5cbcccf 3118 if (single_succ_p (bb))
ca6c03ca 3119 {
c5cbcccf
ZD
3120 single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
3121 single_succ_edge (bb)->count = bb->count;
f87c27b4 3122 }
ca6c03ca
JH
3123 else
3124 {
3125 note = find_reg_note (insn, REG_BR_PROB, NULL);
3126 if (!note)
3127 return purged;
5f0d2358 3128
ca6c03ca
JH
3129 b = BRANCH_EDGE (bb);
3130 f = FALLTHRU_EDGE (bb);
e5af9ddd 3131 b->probability = XINT (note, 0);
ca6c03ca 3132 f->probability = REG_BR_PROB_BASE - b->probability;
8ddb5a29 3133 /* Update these to use GCOV_COMPUTE_SCALE. */
ca6c03ca
JH
3134 b->count = bb->count * b->probability / REG_BR_PROB_BASE;
3135 f->count = bb->count * f->probability / REG_BR_PROB_BASE;
3136 }
5f0d2358 3137
ca6c03ca
JH
3138 return purged;
3139 }
4b4bf941 3140 else if (CALL_P (insn) && SIBLING_CALL_P (insn))
1722c2c8
RH
3141 {
3142 /* First, there should not be any EH or ABCALL edges resulting
3143 from non-local gotos and the like. If there were, we shouldn't
3144 have created the sibcall in the first place. Second, there
3145 should of course never have been a fallthru edge. */
c5cbcccf
ZD
3146 gcc_assert (single_succ_p (bb));
3147 gcc_assert (single_succ_edge (bb)->flags
3148 == (EDGE_SIBCALL | EDGE_ABNORMAL));
1722c2c8
RH
3149
3150 return 0;
3151 }
ca6c03ca 3152
ca6c03ca
JH
3153 /* If we don't see a jump insn, we don't know exactly why the block would
3154 have been broken at this point. Look for a simple, non-fallthru edge,
3155 as these are only created by conditional branches. If we find such an
3156 edge we know that there used to be a jump here and can then safely
3157 remove all non-fallthru edges. */
628f6a4e
BE
3158 found = false;
3159 FOR_EACH_EDGE (e, ei, bb->succs)
3160 if (! (e->flags & (EDGE_COMPLEX | EDGE_FALLTHRU)))
3161 {
3162 found = true;
3163 break;
3164 }
5f0d2358 3165
628f6a4e 3166 if (!found)
ca6c03ca 3167 return purged;
5f0d2358 3168
2afa8dce
DB
3169 /* Remove all but the fake and fallthru edges. The fake edge may be
3170 the only successor for this block in the case of noreturn
3171 calls. */
628f6a4e 3172 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
ca6c03ca 3173 {
2afa8dce 3174 if (!(e->flags & (EDGE_FALLTHRU | EDGE_FAKE)))
c51d95ec 3175 {
6fb5fa3c 3176 df_set_bb_dirty (bb);
c51d95ec
JH
3177 remove_edge (e);
3178 purged = true;
3179 }
628f6a4e
BE
3180 else
3181 ei_next (&ei);
ca6c03ca 3182 }
5f0d2358 3183
c5cbcccf 3184 gcc_assert (single_succ_p (bb));
5f0d2358 3185
c5cbcccf
ZD
3186 single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
3187 single_succ_edge (bb)->count = bb->count;
ca6c03ca 3188
c263766c
RH
3189 if (dump_file)
3190 fprintf (dump_file, "Purged non-fallthru edges from bb %i\n",
0b17ab2f 3191 bb->index);
ca6c03ca
JH
3192 return purged;
3193}
3194
5f0d2358
RK
3195/* Search all basic blocks for potentially dead edges and purge them. Return
3196 true if some edge has been eliminated. */
ca6c03ca
JH
3197
3198bool
25cd19de 3199purge_all_dead_edges (void)
ca6c03ca 3200{
e0082a72 3201 int purged = false;
e0082a72 3202 basic_block bb;
473fb060 3203
11cd3bed 3204 FOR_EACH_BB_FN (bb, cfun)
473fb060 3205 {
e0082a72 3206 bool purged_here = purge_dead_edges (bb);
5f0d2358 3207
473fb060 3208 purged |= purged_here;
473fb060 3209 }
5f0d2358 3210
ca6c03ca
JH
3211 return purged;
3212}
9ee634e3 3213
ba5e9aca
EB
3214/* This is used by a few passes that emit some instructions after abnormal
3215 calls, moving the basic block's end, while they in fact do want to emit
3216 them on the fallthru edge. Look for abnormal call edges, find backward
3217 the call in the block and insert the instructions on the edge instead.
3218
3219 Similarly, handle instructions throwing exceptions internally.
3220
3221 Return true when instructions have been found and inserted on edges. */
3222
3223bool
3224fixup_abnormal_edges (void)
3225{
3226 bool inserted = false;
3227 basic_block bb;
3228
11cd3bed 3229 FOR_EACH_BB_FN (bb, cfun)
ba5e9aca
EB
3230 {
3231 edge e;
3232 edge_iterator ei;
3233
3234 /* Look for cases we are interested in - calls or instructions causing
3235 exceptions. */
3236 FOR_EACH_EDGE (e, ei, bb->succs)
3237 if ((e->flags & EDGE_ABNORMAL_CALL)
3238 || ((e->flags & (EDGE_ABNORMAL | EDGE_EH))
3239 == (EDGE_ABNORMAL | EDGE_EH)))
3240 break;
3241
3242 if (e && !CALL_P (BB_END (bb)) && !can_throw_internal (BB_END (bb)))
3243 {
3244 rtx insn;
3245
3246 /* Get past the new insns generated. Allow notes, as the insns
3247 may be already deleted. */
3248 insn = BB_END (bb);
3249 while ((NONJUMP_INSN_P (insn) || NOTE_P (insn))
3250 && !can_throw_internal (insn)
3251 && insn != BB_HEAD (bb))
3252 insn = PREV_INSN (insn);
3253
3254 if (CALL_P (insn) || can_throw_internal (insn))
3255 {
3256 rtx stop, next;
3257
3258 e = find_fallthru_edge (bb->succs);
3259
3260 stop = NEXT_INSN (BB_END (bb));
190bea87 3261 SET_BB_END (bb) = insn;
ba5e9aca
EB
3262
3263 for (insn = NEXT_INSN (insn); insn != stop; insn = next)
3264 {
3265 next = NEXT_INSN (insn);
3266 if (INSN_P (insn))
3267 {
3268 delete_insn (insn);
3269
3270 /* Sometimes there's still the return value USE.
3271 If it's placed after a trapping call (i.e. that
3272 call is the last insn anyway), we have no fallthru
3273 edge. Simply delete this use and don't try to insert
3274 on the non-existent edge. */
3275 if (GET_CODE (PATTERN (insn)) != USE)
3276 {
3277 /* We're not deleting it, we're moving it. */
3278 INSN_DELETED_P (insn) = 0;
0f82e5c9
DM
3279 SET_PREV_INSN (insn) = NULL_RTX;
3280 SET_NEXT_INSN (insn) = NULL_RTX;
ba5e9aca
EB
3281
3282 insert_insn_on_edge (insn, e);
3283 inserted = true;
3284 }
3285 }
3286 else if (!BARRIER_P (insn))
3287 set_block_for_insn (insn, NULL);
3288 }
3289 }
3290
3291 /* It may be that we don't find any trapping insn. In this
3292 case we discovered quite late that the insn that had been
3293 marked as can_throw_internal in fact couldn't trap at all.
3294 So we should in fact delete the EH edges out of the block. */
3295 else
3296 purge_dead_edges (bb);
3297 }
3298 }
3299
3300 return inserted;
3301}
78bde837
SB
3302\f
3303/* Cut the insns from FIRST to LAST out of the insns stream. */
3304
3305rtx
3306unlink_insn_chain (rtx first, rtx last)
3307{
3308 rtx prevfirst = PREV_INSN (first);
3309 rtx nextlast = NEXT_INSN (last);
3310
0f82e5c9
DM
3311 SET_PREV_INSN (first) = NULL;
3312 SET_NEXT_INSN (last) = NULL;
78bde837 3313 if (prevfirst)
0f82e5c9 3314 SET_NEXT_INSN (prevfirst) = nextlast;
78bde837 3315 if (nextlast)
0f82e5c9 3316 SET_PREV_INSN (nextlast) = prevfirst;
78bde837
SB
3317 else
3318 set_last_insn (prevfirst);
3319 if (!prevfirst)
3320 set_first_insn (nextlast);
3321 return first;
3322}
3323\f
3324/* Skip over inter-block insns occurring after BB which are typically
3325 associated with BB (e.g., barriers). If there are any such insns,
3326 we return the last one. Otherwise, we return the end of BB. */
3327
3328static rtx
3329skip_insns_after_block (basic_block bb)
3330{
3331 rtx insn, last_insn, next_head, prev;
3332
3333 next_head = NULL_RTX;
fefa31b5 3334 if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
78bde837
SB
3335 next_head = BB_HEAD (bb->next_bb);
3336
3337 for (last_insn = insn = BB_END (bb); (insn = NEXT_INSN (insn)) != 0; )
3338 {
3339 if (insn == next_head)
3340 break;
3341
3342 switch (GET_CODE (insn))
3343 {
3344 case BARRIER:
3345 last_insn = insn;
3346 continue;
3347
3348 case NOTE:
3349 switch (NOTE_KIND (insn))
3350 {
3351 case NOTE_INSN_BLOCK_END:
3352 gcc_unreachable ();
3353 continue;
3354 default:
3355 continue;
3356 break;
3357 }
3358 break;
3359
3360 case CODE_LABEL:
3361 if (NEXT_INSN (insn)
3362 && JUMP_TABLE_DATA_P (NEXT_INSN (insn)))
3363 {
3364 insn = NEXT_INSN (insn);
3365 last_insn = insn;
3366 continue;
3367 }
3368 break;
3369
3370 default:
3371 break;
3372 }
3373
3374 break;
3375 }
3376
3377 /* It is possible to hit contradictory sequence. For instance:
3378
3379 jump_insn
3380 NOTE_INSN_BLOCK_BEG
3381 barrier
3382
3383 Where barrier belongs to jump_insn, but the note does not. This can be
3384 created by removing the basic block originally following
3385 NOTE_INSN_BLOCK_BEG. In such case reorder the notes. */
3386
3387 for (insn = last_insn; insn != BB_END (bb); insn = prev)
3388 {
3389 prev = PREV_INSN (insn);
3390 if (NOTE_P (insn))
3391 switch (NOTE_KIND (insn))
3392 {
3393 case NOTE_INSN_BLOCK_END:
3394 gcc_unreachable ();
3395 break;
3396 case NOTE_INSN_DELETED:
3397 case NOTE_INSN_DELETED_LABEL:
3398 case NOTE_INSN_DELETED_DEBUG_LABEL:
3399 continue;
3400 default:
3401 reorder_insns (insn, insn, last_insn);
3402 }
3403 }
3404
3405 return last_insn;
3406}
3407
3408/* Locate or create a label for a given basic block. */
3409
3410static rtx
3411label_for_bb (basic_block bb)
3412{
3413 rtx label = BB_HEAD (bb);
3414
3415 if (!LABEL_P (label))
3416 {
3417 if (dump_file)
3418 fprintf (dump_file, "Emitting label for block %d\n", bb->index);
3419
3420 label = block_label (bb);
3421 }
3422
3423 return label;
3424}
3425
3426/* Locate the effective beginning and end of the insn chain for each
3427 block, as defined by skip_insns_after_block above. */
3428
3429static void
3430record_effective_endpoints (void)
3431{
3432 rtx next_insn;
3433 basic_block bb;
3434 rtx insn;
3435
3436 for (insn = get_insns ();
3437 insn
3438 && NOTE_P (insn)
3439 && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK;
3440 insn = NEXT_INSN (insn))
3441 continue;
3442 /* No basic blocks at all? */
3443 gcc_assert (insn);
3444
3445 if (PREV_INSN (insn))
3446 cfg_layout_function_header =
3447 unlink_insn_chain (get_insns (), PREV_INSN (insn));
3448 else
3449 cfg_layout_function_header = NULL_RTX;
3450
3451 next_insn = get_insns ();
11cd3bed 3452 FOR_EACH_BB_FN (bb, cfun)
78bde837
SB
3453 {
3454 rtx end;
3455
3456 if (PREV_INSN (BB_HEAD (bb)) && next_insn != BB_HEAD (bb))
190bea87
DM
3457 SET_BB_HEADER (bb) = unlink_insn_chain (next_insn,
3458 PREV_INSN (BB_HEAD (bb)));
78bde837
SB
3459 end = skip_insns_after_block (bb);
3460 if (NEXT_INSN (BB_END (bb)) && BB_END (bb) != end)
190bea87 3461 SET_BB_FOOTER (bb) = unlink_insn_chain (NEXT_INSN (BB_END (bb)), end);
78bde837
SB
3462 next_insn = NEXT_INSN (BB_END (bb));
3463 }
3464
3465 cfg_layout_function_footer = next_insn;
3466 if (cfg_layout_function_footer)
3467 cfg_layout_function_footer = unlink_insn_chain (cfg_layout_function_footer, get_last_insn ());
3468}
3469\f
27a4cd48
DM
3470namespace {
3471
3472const pass_data pass_data_into_cfg_layout_mode =
3473{
3474 RTL_PASS, /* type */
3475 "into_cfglayout", /* name */
3476 OPTGROUP_NONE, /* optinfo_flags */
27a4cd48
DM
3477 TV_CFG, /* tv_id */
3478 0, /* properties_required */
3479 PROP_cfglayout, /* properties_provided */
3480 0, /* properties_destroyed */
3481 0, /* todo_flags_start */
3482 0, /* todo_flags_finish */
78bde837
SB
3483};
3484
27a4cd48
DM
3485class pass_into_cfg_layout_mode : public rtl_opt_pass
3486{
3487public:
c3284718
RS
3488 pass_into_cfg_layout_mode (gcc::context *ctxt)
3489 : rtl_opt_pass (pass_data_into_cfg_layout_mode, ctxt)
27a4cd48
DM
3490 {}
3491
3492 /* opt_pass methods: */
be55bfe6
TS
3493 virtual unsigned int execute (function *)
3494 {
3495 cfg_layout_initialize (0);
3496 return 0;
3497 }
27a4cd48
DM
3498
3499}; // class pass_into_cfg_layout_mode
3500
3501} // anon namespace
3502
3503rtl_opt_pass *
3504make_pass_into_cfg_layout_mode (gcc::context *ctxt)
3505{
3506 return new pass_into_cfg_layout_mode (ctxt);
3507}
3508
3509namespace {
3510
3511const pass_data pass_data_outof_cfg_layout_mode =
3512{
3513 RTL_PASS, /* type */
3514 "outof_cfglayout", /* name */
3515 OPTGROUP_NONE, /* optinfo_flags */
27a4cd48
DM
3516 TV_CFG, /* tv_id */
3517 0, /* properties_required */
3518 0, /* properties_provided */
3519 PROP_cfglayout, /* properties_destroyed */
3520 0, /* todo_flags_start */
3521 0, /* todo_flags_finish */
78bde837 3522};
27a4cd48
DM
3523
3524class pass_outof_cfg_layout_mode : public rtl_opt_pass
3525{
3526public:
c3284718
RS
3527 pass_outof_cfg_layout_mode (gcc::context *ctxt)
3528 : rtl_opt_pass (pass_data_outof_cfg_layout_mode, ctxt)
27a4cd48
DM
3529 {}
3530
3531 /* opt_pass methods: */
be55bfe6 3532 virtual unsigned int execute (function *);
27a4cd48
DM
3533
3534}; // class pass_outof_cfg_layout_mode
3535
be55bfe6
TS
3536unsigned int
3537pass_outof_cfg_layout_mode::execute (function *fun)
3538{
3539 basic_block bb;
3540
3541 FOR_EACH_BB_FN (bb, fun)
3542 if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (fun))
3543 bb->aux = bb->next_bb;
3544
3545 cfg_layout_finalize ();
3546
3547 return 0;
3548}
3549
27a4cd48
DM
3550} // anon namespace
3551
3552rtl_opt_pass *
3553make_pass_outof_cfg_layout_mode (gcc::context *ctxt)
3554{
3555 return new pass_outof_cfg_layout_mode (ctxt);
3556}
78bde837
SB
3557\f
3558
3559/* Link the basic blocks in the correct order, compacting the basic
3560 block queue while at it. If STAY_IN_CFGLAYOUT_MODE is false, this
3561 function also clears the basic block header and footer fields.
3562
3563 This function is usually called after a pass (e.g. tracer) finishes
3564 some transformations while in cfglayout mode. The required sequence
3565 of the basic blocks is in a linked list along the bb->aux field.
3566 This functions re-links the basic block prev_bb and next_bb pointers
532aafad
SB
3567 accordingly, and it compacts and renumbers the blocks.
3568
3569 FIXME: This currently works only for RTL, but the only RTL-specific
3570 bits are the STAY_IN_CFGLAYOUT_MODE bits. The tracer pass was moved
3571 to GIMPLE a long time ago, but it doesn't relink the basic block
3572 chain. It could do that (to give better initial RTL) if this function
3573 is made IR-agnostic (and moved to cfganal.c or cfg.c while at it). */
78bde837
SB
3574
3575void
3576relink_block_chain (bool stay_in_cfglayout_mode)
3577{
3578 basic_block bb, prev_bb;
3579 int index;
3580
3581 /* Maybe dump the re-ordered sequence. */
3582 if (dump_file)
3583 {
3584 fprintf (dump_file, "Reordered sequence:\n");
fefa31b5
DM
3585 for (bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb, index =
3586 NUM_FIXED_BLOCKS;
78bde837
SB
3587 bb;
3588 bb = (basic_block) bb->aux, index++)
3589 {
3590 fprintf (dump_file, " %i ", index);
3591 if (get_bb_original (bb))
3592 fprintf (dump_file, "duplicate of %i ",
3593 get_bb_original (bb)->index);
3594 else if (forwarder_block_p (bb)
3595 && !LABEL_P (BB_HEAD (bb)))
3596 fprintf (dump_file, "compensation ");
3597 else
3598 fprintf (dump_file, "bb %i ", bb->index);
3599 fprintf (dump_file, " [%i]\n", bb->frequency);
3600 }
3601 }
3602
3603 /* Now reorder the blocks. */
fefa31b5
DM
3604 prev_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
3605 bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb;
78bde837
SB
3606 for (; bb; prev_bb = bb, bb = (basic_block) bb->aux)
3607 {
3608 bb->prev_bb = prev_bb;
3609 prev_bb->next_bb = bb;
3610 }
fefa31b5
DM
3611 prev_bb->next_bb = EXIT_BLOCK_PTR_FOR_FN (cfun);
3612 EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb = prev_bb;
78bde837
SB
3613
3614 /* Then, clean up the aux fields. */
04a90bec 3615 FOR_ALL_BB_FN (bb, cfun)
78bde837
SB
3616 {
3617 bb->aux = NULL;
3618 if (!stay_in_cfglayout_mode)
190bea87 3619 SET_BB_HEADER (bb) = SET_BB_FOOTER (bb) = NULL;
78bde837
SB
3620 }
3621
3622 /* Maybe reset the original copy tables, they are not valid anymore
3623 when we renumber the basic blocks in compact_blocks. If we are
3624 are going out of cfglayout mode, don't re-allocate the tables. */
3625 free_original_copy_tables ();
3626 if (stay_in_cfglayout_mode)
3627 initialize_original_copy_tables ();
3628
3629 /* Finally, put basic_block_info in the new order. */
3630 compact_blocks ();
3631}
3632\f
3633
3634/* Given a reorder chain, rearrange the code to match. */
3635
3636static void
3637fixup_reorder_chain (void)
3638{
3639 basic_block bb;
3640 rtx insn = NULL;
3641
3642 if (cfg_layout_function_header)
3643 {
3644 set_first_insn (cfg_layout_function_header);
3645 insn = cfg_layout_function_header;
3646 while (NEXT_INSN (insn))
3647 insn = NEXT_INSN (insn);
3648 }
3649
3650 /* First do the bulk reordering -- rechain the blocks without regard to
3651 the needed changes to jumps and labels. */
3652
fefa31b5
DM
3653 for (bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; bb; bb = (basic_block)
3654 bb->aux)
78bde837
SB
3655 {
3656 if (BB_HEADER (bb))
3657 {
3658 if (insn)
0f82e5c9 3659 SET_NEXT_INSN (insn) = BB_HEADER (bb);
78bde837
SB
3660 else
3661 set_first_insn (BB_HEADER (bb));
0f82e5c9 3662 SET_PREV_INSN (BB_HEADER (bb)) = insn;
78bde837
SB
3663 insn = BB_HEADER (bb);
3664 while (NEXT_INSN (insn))
3665 insn = NEXT_INSN (insn);
3666 }
3667 if (insn)
0f82e5c9 3668 SET_NEXT_INSN (insn) = BB_HEAD (bb);
78bde837
SB
3669 else
3670 set_first_insn (BB_HEAD (bb));
0f82e5c9 3671 SET_PREV_INSN (BB_HEAD (bb)) = insn;
78bde837
SB
3672 insn = BB_END (bb);
3673 if (BB_FOOTER (bb))
3674 {
0f82e5c9
DM
3675 SET_NEXT_INSN (insn) = BB_FOOTER (bb);
3676 SET_PREV_INSN (BB_FOOTER (bb)) = insn;
78bde837
SB
3677 while (NEXT_INSN (insn))
3678 insn = NEXT_INSN (insn);
3679 }
3680 }
3681
0f82e5c9 3682 SET_NEXT_INSN (insn) = cfg_layout_function_footer;
78bde837 3683 if (cfg_layout_function_footer)
0f82e5c9 3684 SET_PREV_INSN (cfg_layout_function_footer) = insn;
78bde837
SB
3685
3686 while (NEXT_INSN (insn))
3687 insn = NEXT_INSN (insn);
3688
3689 set_last_insn (insn);
3690#ifdef ENABLE_CHECKING
3691 verify_insn_chain ();
3692#endif
3693
3694 /* Now add jumps and labels as needed to match the blocks new
29f3fd5b 3695 outgoing edges. */
78bde837 3696
29f3fd5b
SB
3697 for (bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; bb ; bb = (basic_block)
3698 bb->aux)
78bde837
SB
3699 {
3700 edge e_fall, e_taken, e;
3701 rtx bb_end_insn;
3702 rtx ret_label = NULL_RTX;
3371a64f 3703 basic_block nb;
78bde837
SB
3704 edge_iterator ei;
3705
3706 if (EDGE_COUNT (bb->succs) == 0)
3707 continue;
3708
3709 /* Find the old fallthru edge, and another non-EH edge for
3710 a taken jump. */
3711 e_taken = e_fall = NULL;
3712
3713 FOR_EACH_EDGE (e, ei, bb->succs)
3714 if (e->flags & EDGE_FALLTHRU)
3715 e_fall = e;
3716 else if (! (e->flags & EDGE_EH))
3717 e_taken = e;
3718
3719 bb_end_insn = BB_END (bb);
3720 if (JUMP_P (bb_end_insn))
3721 {
3722 ret_label = JUMP_LABEL (bb_end_insn);
3723 if (any_condjump_p (bb_end_insn))
3724 {
3725 /* This might happen if the conditional jump has side
3726 effects and could therefore not be optimized away.
3727 Make the basic block to end with a barrier in order
3728 to prevent rtl_verify_flow_info from complaining. */
3729 if (!e_fall)
3730 {
3731 gcc_assert (!onlyjump_p (bb_end_insn)
91af97c3
TJ
3732 || returnjump_p (bb_end_insn)
3733 || (e_taken->flags & EDGE_CROSSING));
39718607 3734 emit_barrier_after (bb_end_insn);
78bde837
SB
3735 continue;
3736 }
3737
3738 /* If the old fallthru is still next, nothing to do. */
3739 if (bb->aux == e_fall->dest
fefa31b5 3740 || e_fall->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
78bde837
SB
3741 continue;
3742
3743 /* The degenerated case of conditional jump jumping to the next
3744 instruction can happen for jumps with side effects. We need
3745 to construct a forwarder block and this will be done just
3746 fine by force_nonfallthru below. */
3747 if (!e_taken)
3748 ;
3749
3750 /* There is another special case: if *neither* block is next,
3751 such as happens at the very end of a function, then we'll
3752 need to add a new unconditional jump. Choose the taken
3753 edge based on known or assumed probability. */
3754 else if (bb->aux != e_taken->dest)
3755 {
3756 rtx note = find_reg_note (bb_end_insn, REG_BR_PROB, 0);
3757
3758 if (note
e5af9ddd 3759 && XINT (note, 0) < REG_BR_PROB_BASE / 2
78bde837 3760 && invert_jump (bb_end_insn,
fefa31b5
DM
3761 (e_fall->dest
3762 == EXIT_BLOCK_PTR_FOR_FN (cfun)
78bde837
SB
3763 ? NULL_RTX
3764 : label_for_bb (e_fall->dest)), 0))
3765 {
3766 e_fall->flags &= ~EDGE_FALLTHRU;
3767 gcc_checking_assert (could_fall_through
3768 (e_taken->src, e_taken->dest));
3769 e_taken->flags |= EDGE_FALLTHRU;
3770 update_br_prob_note (bb);
3771 e = e_fall, e_fall = e_taken, e_taken = e;
3772 }
3773 }
3774
3775 /* If the "jumping" edge is a crossing edge, and the fall
3776 through edge is non-crossing, leave things as they are. */
3777 else if ((e_taken->flags & EDGE_CROSSING)
3778 && !(e_fall->flags & EDGE_CROSSING))
3779 continue;
3780
3781 /* Otherwise we can try to invert the jump. This will
3782 basically never fail, however, keep up the pretense. */
3783 else if (invert_jump (bb_end_insn,
fefa31b5
DM
3784 (e_fall->dest
3785 == EXIT_BLOCK_PTR_FOR_FN (cfun)
78bde837
SB
3786 ? NULL_RTX
3787 : label_for_bb (e_fall->dest)), 0))
3788 {
3789 e_fall->flags &= ~EDGE_FALLTHRU;
3790 gcc_checking_assert (could_fall_through
3791 (e_taken->src, e_taken->dest));
3792 e_taken->flags |= EDGE_FALLTHRU;
3793 update_br_prob_note (bb);
3794 if (LABEL_NUSES (ret_label) == 0
3795 && single_pred_p (e_taken->dest))
3796 delete_insn (ret_label);
3797 continue;
3798 }
3799 }
3800 else if (extract_asm_operands (PATTERN (bb_end_insn)) != NULL)
3801 {
3802 /* If the old fallthru is still next or if
3803 asm goto doesn't have a fallthru (e.g. when followed by
3804 __builtin_unreachable ()), nothing to do. */
3805 if (! e_fall
3806 || bb->aux == e_fall->dest
fefa31b5 3807 || e_fall->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
78bde837
SB
3808 continue;
3809
3810 /* Otherwise we'll have to use the fallthru fixup below. */
3811 }
3812 else
3813 {
3814 /* Otherwise we have some return, switch or computed
3815 jump. In the 99% case, there should not have been a
3816 fallthru edge. */
3817 gcc_assert (returnjump_p (bb_end_insn) || !e_fall);
3818 continue;
3819 }
3820 }
3821 else
3822 {
3823 /* No fallthru implies a noreturn function with EH edges, or
3824 something similarly bizarre. In any case, we don't need to
3825 do anything. */
3826 if (! e_fall)
3827 continue;
3828
3829 /* If the fallthru block is still next, nothing to do. */
3830 if (bb->aux == e_fall->dest)
3831 continue;
3832
3833 /* A fallthru to exit block. */
fefa31b5 3834 if (e_fall->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
78bde837
SB
3835 continue;
3836 }
3837
3838 /* We got here if we need to add a new jump insn.
3839 Note force_nonfallthru can delete E_FALL and thus we have to
3840 save E_FALL->src prior to the call to force_nonfallthru. */
78bde837
SB
3841 nb = force_nonfallthru_and_redirect (e_fall, e_fall->dest, ret_label);
3842 if (nb)
3843 {
3844 nb->aux = bb->aux;
3845 bb->aux = nb;
3846 /* Don't process this new block. */
3847 bb = nb;
78bde837
SB
3848 }
3849 }
3850
3851 relink_block_chain (/*stay_in_cfglayout_mode=*/false);
3852
29f3fd5b 3853 /* Annoying special case - jump around dead jumptables left in the code. */
11cd3bed 3854 FOR_EACH_BB_FN (bb, cfun)
78bde837
SB
3855 {
3856 edge e = find_fallthru_edge (bb->succs);
3857
29f3fd5b
SB
3858 if (e && !can_fallthru (e->src, e->dest))
3859 force_nonfallthru (e);
78bde837
SB
3860 }
3861
3862 /* Ensure goto_locus from edges has some instructions with that locus
3863 in RTL. */
3864 if (!optimize)
11cd3bed 3865 FOR_EACH_BB_FN (bb, cfun)
78bde837
SB
3866 {
3867 edge e;
3868 edge_iterator ei;
3869
3870 FOR_EACH_EDGE (e, ei, bb->succs)
2f13f2de 3871 if (LOCATION_LOCUS (e->goto_locus) != UNKNOWN_LOCATION
5368224f 3872 && !(e->flags & EDGE_ABNORMAL))
78bde837
SB
3873 {
3874 edge e2;
3875 edge_iterator ei2;
3876 basic_block dest, nb;
3877 rtx end;
3878
3879 insn = BB_END (e->src);
3880 end = PREV_INSN (BB_HEAD (e->src));
3881 while (insn != end
5368224f 3882 && (!NONDEBUG_INSN_P (insn) || !INSN_HAS_LOCATION (insn)))
78bde837
SB
3883 insn = PREV_INSN (insn);
3884 if (insn != end
5368224f 3885 && INSN_LOCATION (insn) == e->goto_locus)
78bde837
SB
3886 continue;
3887 if (simplejump_p (BB_END (e->src))
5368224f 3888 && !INSN_HAS_LOCATION (BB_END (e->src)))
78bde837 3889 {
5368224f 3890 INSN_LOCATION (BB_END (e->src)) = e->goto_locus;
78bde837
SB
3891 continue;
3892 }
3893 dest = e->dest;
fefa31b5 3894 if (dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
78bde837
SB
3895 {
3896 /* Non-fallthru edges to the exit block cannot be split. */
3897 if (!(e->flags & EDGE_FALLTHRU))
3898 continue;
3899 }
3900 else
3901 {
3902 insn = BB_HEAD (dest);
3903 end = NEXT_INSN (BB_END (dest));
3904 while (insn != end && !NONDEBUG_INSN_P (insn))
3905 insn = NEXT_INSN (insn);
5368224f
DC
3906 if (insn != end && INSN_HAS_LOCATION (insn)
3907 && INSN_LOCATION (insn) == e->goto_locus)
78bde837
SB
3908 continue;
3909 }
3910 nb = split_edge (e);
3911 if (!INSN_P (BB_END (nb)))
190bea87
DM
3912 SET_BB_END (nb) = emit_insn_after_noloc (gen_nop (), BB_END (nb),
3913 nb);
5368224f 3914 INSN_LOCATION (BB_END (nb)) = e->goto_locus;
78bde837
SB
3915
3916 /* If there are other incoming edges to the destination block
3917 with the same goto locus, redirect them to the new block as
3918 well, this can prevent other such blocks from being created
3919 in subsequent iterations of the loop. */
3920 for (ei2 = ei_start (dest->preds); (e2 = ei_safe_edge (ei2)); )
2f13f2de 3921 if (LOCATION_LOCUS (e2->goto_locus) != UNKNOWN_LOCATION
78bde837 3922 && !(e2->flags & (EDGE_ABNORMAL | EDGE_FALLTHRU))
5368224f 3923 && e->goto_locus == e2->goto_locus)
78bde837
SB
3924 redirect_edge_and_branch (e2, nb);
3925 else
3926 ei_next (&ei2);
3927 }
3928 }
3929}
3930\f
3931/* Perform sanity checks on the insn chain.
3932 1. Check that next/prev pointers are consistent in both the forward and
3933 reverse direction.
3934 2. Count insns in chain, going both directions, and check if equal.
3935 3. Check that get_last_insn () returns the actual end of chain. */
3936
3937DEBUG_FUNCTION void
3938verify_insn_chain (void)
3939{
3940 rtx x, prevx, nextx;
3941 int insn_cnt1, insn_cnt2;
3942
3943 for (prevx = NULL, insn_cnt1 = 1, x = get_insns ();
3944 x != 0;
3945 prevx = x, insn_cnt1++, x = NEXT_INSN (x))
3946 gcc_assert (PREV_INSN (x) == prevx);
3947
3948 gcc_assert (prevx == get_last_insn ());
3949
3950 for (nextx = NULL, insn_cnt2 = 1, x = get_last_insn ();
3951 x != 0;
3952 nextx = x, insn_cnt2++, x = PREV_INSN (x))
3953 gcc_assert (NEXT_INSN (x) == nextx);
3954
3955 gcc_assert (insn_cnt1 == insn_cnt2);
3956}
3957\f
3958/* If we have assembler epilogues, the block falling through to exit must
3959 be the last one in the reordered chain when we reach final. Ensure
3960 that this condition is met. */
3961static void
3962fixup_fallthru_exit_predecessor (void)
3963{
3964 edge e;
3965 basic_block bb = NULL;
3966
3967 /* This transformation is not valid before reload, because we might
3968 separate a call from the instruction that copies the return
3969 value. */
3970 gcc_assert (reload_completed);
3971
fefa31b5 3972 e = find_fallthru_edge (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
78bde837
SB
3973 if (e)
3974 bb = e->src;
3975
3976 if (bb && bb->aux)
3977 {
fefa31b5 3978 basic_block c = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb;
78bde837
SB
3979
3980 /* If the very first block is the one with the fall-through exit
3981 edge, we have to split that block. */
3982 if (c == bb)
3983 {
3984 bb = split_block (bb, NULL)->dest;
3985 bb->aux = c->aux;
3986 c->aux = bb;
190bea87
DM
3987 SET_BB_FOOTER (bb) = BB_FOOTER (c);
3988 SET_BB_FOOTER (c) = NULL;
78bde837
SB
3989 }
3990
3991 while (c->aux != bb)
3992 c = (basic_block) c->aux;
3993
3994 c->aux = bb->aux;
3995 while (c->aux)
3996 c = (basic_block) c->aux;
3997
3998 c->aux = bb;
3999 bb->aux = NULL;
4000 }
4001}
4002
4003/* In case there are more than one fallthru predecessors of exit, force that
4004 there is only one. */
4005
4006static void
4007force_one_exit_fallthru (void)
4008{
4009 edge e, predecessor = NULL;
4010 bool more = false;
4011 edge_iterator ei;
4012 basic_block forwarder, bb;
4013
fefa31b5 4014 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
78bde837
SB
4015 if (e->flags & EDGE_FALLTHRU)
4016 {
4017 if (predecessor == NULL)
4018 predecessor = e;
4019 else
4020 {
4021 more = true;
4022 break;
4023 }
4024 }
4025
4026 if (!more)
4027 return;
4028
4029 /* Exit has several fallthru predecessors. Create a forwarder block for
4030 them. */
4031 forwarder = split_edge (predecessor);
fefa31b5
DM
4032 for (ei = ei_start (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
4033 (e = ei_safe_edge (ei)); )
78bde837
SB
4034 {
4035 if (e->src == forwarder
4036 || !(e->flags & EDGE_FALLTHRU))
4037 ei_next (&ei);
4038 else
4039 redirect_edge_and_branch_force (e, forwarder);
4040 }
4041
4042 /* Fix up the chain of blocks -- make FORWARDER immediately precede the
4043 exit block. */
11cd3bed 4044 FOR_EACH_BB_FN (bb, cfun)
78bde837
SB
4045 {
4046 if (bb->aux == NULL && bb != forwarder)
4047 {
4048 bb->aux = forwarder;
4049 break;
4050 }
4051 }
4052}
4053\f
4054/* Return true in case it is possible to duplicate the basic block BB. */
4055
4056static bool
4057cfg_layout_can_duplicate_bb_p (const_basic_block bb)
4058{
4059 /* Do not attempt to duplicate tablejumps, as we need to unshare
4060 the dispatch table. This is difficult to do, as the instructions
4061 computing jump destination may be hoisted outside the basic block. */
4062 if (tablejump_p (BB_END (bb), NULL, NULL))
4063 return false;
4064
4065 /* Do not duplicate blocks containing insns that can't be copied. */
4066 if (targetm.cannot_copy_insn_p)
4067 {
4068 rtx insn = BB_HEAD (bb);
4069 while (1)
4070 {
4071 if (INSN_P (insn) && targetm.cannot_copy_insn_p (insn))
4072 return false;
4073 if (insn == BB_END (bb))
4074 break;
4075 insn = NEXT_INSN (insn);
4076 }
4077 }
4078
4079 return true;
4080}
4081
4082rtx
4083duplicate_insn_chain (rtx from, rtx to)
4084{
39718607 4085 rtx insn, next, last, copy;
78bde837
SB
4086
4087 /* Avoid updating of boundaries of previous basic block. The
4088 note will get removed from insn stream in fixup. */
4089 last = emit_note (NOTE_INSN_DELETED);
4090
4091 /* Create copy at the end of INSN chain. The chain will
4092 be reordered later. */
4093 for (insn = from; insn != NEXT_INSN (to); insn = NEXT_INSN (insn))
4094 {
4095 switch (GET_CODE (insn))
4096 {
4097 case DEBUG_INSN:
4098 /* Don't duplicate label debug insns. */
4099 if (TREE_CODE (INSN_VAR_LOCATION_DECL (insn)) == LABEL_DECL)
4100 break;
4101 /* FALLTHRU */
4102 case INSN:
4103 case CALL_INSN:
4104 case JUMP_INSN:
78bde837
SB
4105 copy = emit_copy_of_insn_after (insn, get_last_insn ());
4106 if (JUMP_P (insn) && JUMP_LABEL (insn) != NULL_RTX
4107 && ANY_RETURN_P (JUMP_LABEL (insn)))
4108 JUMP_LABEL (copy) = JUMP_LABEL (insn);
4109 maybe_copy_prologue_epilogue_insn (insn, copy);
4110 break;
4111
39718607
SB
4112 case JUMP_TABLE_DATA:
4113 /* Avoid copying of dispatch tables. We never duplicate
4114 tablejumps, so this can hit only in case the table got
4115 moved far from original jump.
4116 Avoid copying following barrier as well if any
4117 (and debug insns in between). */
4118 for (next = NEXT_INSN (insn);
4119 next != NEXT_INSN (to);
4120 next = NEXT_INSN (next))
4121 if (!DEBUG_INSN_P (next))
4122 break;
4123 if (next != NEXT_INSN (to) && BARRIER_P (next))
4124 insn = next;
4125 break;
4126
78bde837
SB
4127 case CODE_LABEL:
4128 break;
4129
4130 case BARRIER:
4131 emit_barrier ();
4132 break;
4133
4134 case NOTE:
4135 switch (NOTE_KIND (insn))
4136 {
4137 /* In case prologue is empty and function contain label
4138 in first BB, we may want to copy the block. */
4139 case NOTE_INSN_PROLOGUE_END:
4140
4141 case NOTE_INSN_DELETED:
4142 case NOTE_INSN_DELETED_LABEL:
4143 case NOTE_INSN_DELETED_DEBUG_LABEL:
4144 /* No problem to strip these. */
4145 case NOTE_INSN_FUNCTION_BEG:
4146 /* There is always just single entry to function. */
4147 case NOTE_INSN_BASIC_BLOCK:
3371a64f
TJ
4148 /* We should only switch text sections once. */
4149 case NOTE_INSN_SWITCH_TEXT_SECTIONS:
78bde837
SB
4150 break;
4151
4152 case NOTE_INSN_EPILOGUE_BEG:
78bde837
SB
4153 emit_note_copy (insn);
4154 break;
4155
4156 default:
4157 /* All other notes should have already been eliminated. */
4158 gcc_unreachable ();
4159 }
4160 break;
4161 default:
4162 gcc_unreachable ();
4163 }
4164 }
4165 insn = NEXT_INSN (last);
4166 delete_insn (last);
4167 return insn;
4168}
4169
4170/* Create a duplicate of the basic block BB. */
4171
4172static basic_block
4173cfg_layout_duplicate_bb (basic_block bb)
4174{
4175 rtx insn;
4176 basic_block new_bb;
4177
4178 insn = duplicate_insn_chain (BB_HEAD (bb), BB_END (bb));
4179 new_bb = create_basic_block (insn,
4180 insn ? get_last_insn () : NULL,
fefa31b5 4181 EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb);
78bde837
SB
4182
4183 BB_COPY_PARTITION (new_bb, bb);
4184 if (BB_HEADER (bb))
4185 {
4186 insn = BB_HEADER (bb);
4187 while (NEXT_INSN (insn))
4188 insn = NEXT_INSN (insn);
4189 insn = duplicate_insn_chain (BB_HEADER (bb), insn);
4190 if (insn)
190bea87 4191 SET_BB_HEADER (new_bb) = unlink_insn_chain (insn, get_last_insn ());
78bde837
SB
4192 }
4193
4194 if (BB_FOOTER (bb))
4195 {
4196 insn = BB_FOOTER (bb);
4197 while (NEXT_INSN (insn))
4198 insn = NEXT_INSN (insn);
4199 insn = duplicate_insn_chain (BB_FOOTER (bb), insn);
4200 if (insn)
190bea87 4201 SET_BB_FOOTER (new_bb) = unlink_insn_chain (insn, get_last_insn ());
78bde837
SB
4202 }
4203
4204 return new_bb;
4205}
4206
4207\f
4208/* Main entry point to this module - initialize the datastructures for
4209 CFG layout changes. It keeps LOOPS up-to-date if not null.
4210
4211 FLAGS is a set of additional flags to pass to cleanup_cfg(). */
4212
4213void
4214cfg_layout_initialize (unsigned int flags)
4215{
4216 rtx x;
4217 basic_block bb;
4218
8a9e6b45
BS
4219 /* Once bb partitioning is complete, cfg layout mode should not be
4220 re-entered. Entering cfg layout mode may require fixups. As an
4221 example, if edge forwarding performed when optimizing the cfg
4222 layout required moving a block from the hot to the cold
4223 section. This would create an illegal partitioning unless some
4224 manual fixup was performed. */
4225 gcc_assert (!(crtl->bb_reorder_complete
4226 && flag_reorder_blocks_and_partition));
4ca19309 4227
78bde837
SB
4228 initialize_original_copy_tables ();
4229
4230 cfg_layout_rtl_register_cfg_hooks ();
4231
4232 record_effective_endpoints ();
4233
4234 /* Make sure that the targets of non local gotos are marked. */
4235 for (x = nonlocal_goto_handler_labels; x; x = XEXP (x, 1))
4236 {
4237 bb = BLOCK_FOR_INSN (XEXP (x, 0));
4238 bb->flags |= BB_NON_LOCAL_GOTO_TARGET;
4239 }
4240
4241 cleanup_cfg (CLEANUP_CFGLAYOUT | flags);
4242}
4243
4244/* Splits superblocks. */
4245void
4246break_superblocks (void)
4247{
4248 sbitmap superblocks;
4249 bool need = false;
4250 basic_block bb;
4251
8b1c6fd7 4252 superblocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
f61e445a 4253 bitmap_clear (superblocks);
78bde837 4254
11cd3bed 4255 FOR_EACH_BB_FN (bb, cfun)
78bde837
SB
4256 if (bb->flags & BB_SUPERBLOCK)
4257 {
4258 bb->flags &= ~BB_SUPERBLOCK;
d7c028c0 4259 bitmap_set_bit (superblocks, bb->index);
78bde837
SB
4260 need = true;
4261 }
4262
4263 if (need)
4264 {
4265 rebuild_jump_labels (get_insns ());
4266 find_many_sub_basic_blocks (superblocks);
4267 }
4268
4269 free (superblocks);
4270}
4271
4272/* Finalize the changes: reorder insn list according to the sequence specified
29f3fd5b 4273 by aux pointers, enter compensation code, rebuild scope forest. */
78bde837
SB
4274
4275void
4276cfg_layout_finalize (void)
4277{
4278#ifdef ENABLE_CHECKING
4279 verify_flow_info ();
4280#endif
4281 force_one_exit_fallthru ();
4282 rtl_register_cfg_hooks ();
4283 if (reload_completed
4284#ifdef HAVE_epilogue
4285 && !HAVE_epilogue
4286#endif
4287 )
4288 fixup_fallthru_exit_predecessor ();
4289 fixup_reorder_chain ();
4290
29f3fd5b
SB
4291 rebuild_jump_labels (get_insns ());
4292 delete_dead_jumptables ();
4293
78bde837
SB
4294#ifdef ENABLE_CHECKING
4295 verify_insn_chain ();
4296 verify_flow_info ();
4297#endif
4298}
4299
ba5e9aca 4300
9ee634e3 4301/* Same as split_block but update cfg_layout structures. */
f470c378
ZD
4302
4303static basic_block
d329e058 4304cfg_layout_split_block (basic_block bb, void *insnp)
9ee634e3 4305{
ae50c0cb 4306 rtx insn = (rtx) insnp;
f470c378 4307 basic_block new_bb = rtl_split_block (bb, insn);
9ee634e3 4308
190bea87
DM
4309 SET_BB_FOOTER (new_bb) = BB_FOOTER (bb);
4310 SET_BB_FOOTER (bb) = NULL;
9ee634e3 4311
f470c378 4312 return new_bb;
9ee634e3
JH
4313}
4314
9ee634e3 4315/* Redirect Edge to DEST. */
6de9cd9a 4316static edge
d329e058 4317cfg_layout_redirect_edge_and_branch (edge e, basic_block dest)
9ee634e3
JH
4318{
4319 basic_block src = e->src;
6de9cd9a 4320 edge ret;
9ee634e3 4321
bc35512f 4322 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
6de9cd9a 4323 return NULL;
bc35512f 4324
3348b696 4325 if (e->dest == dest)
6de9cd9a 4326 return e;
bc35512f 4327
fefa31b5 4328 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
6de9cd9a 4329 && (ret = try_redirect_by_replacing_jump (e, dest, true)))
f345f21a 4330 {
6fb5fa3c 4331 df_set_bb_dirty (src);
6de9cd9a 4332 return ret;
f345f21a 4333 }
bc35512f 4334
fefa31b5 4335 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun)
bc35512f
JH
4336 && (e->flags & EDGE_FALLTHRU) && !(e->flags & EDGE_COMPLEX))
4337 {
c263766c
RH
4338 if (dump_file)
4339 fprintf (dump_file, "Redirecting entry edge from bb %i to %i\n",
bc35512f
JH
4340 e->src->index, dest->index);
4341
6fb5fa3c 4342 df_set_bb_dirty (e->src);
bc35512f 4343 redirect_edge_succ (e, dest);
6de9cd9a 4344 return e;
bc35512f
JH
4345 }
4346
9ee634e3
JH
4347 /* Redirect_edge_and_branch may decide to turn branch into fallthru edge
4348 in the case the basic block appears to be in sequence. Avoid this
4349 transformation. */
4350
9ee634e3
JH
4351 if (e->flags & EDGE_FALLTHRU)
4352 {
4353 /* Redirect any branch edges unified with the fallthru one. */
4b4bf941 4354 if (JUMP_P (BB_END (src))
432f982f
JH
4355 && label_is_jump_target_p (BB_HEAD (e->dest),
4356 BB_END (src)))
9ee634e3 4357 {
341c100f 4358 edge redirected;
c22cacf3 4359
c263766c
RH
4360 if (dump_file)
4361 fprintf (dump_file, "Fallthru edge unified with branch "
432f982f
JH
4362 "%i->%i redirected to %i\n",
4363 e->src->index, e->dest->index, dest->index);
4364 e->flags &= ~EDGE_FALLTHRU;
341c100f
NS
4365 redirected = redirect_branch_edge (e, dest);
4366 gcc_assert (redirected);
0c617be4
JL
4367 redirected->flags |= EDGE_FALLTHRU;
4368 df_set_bb_dirty (redirected->src);
4369 return redirected;
9ee634e3
JH
4370 }
4371 /* In case we are redirecting fallthru edge to the branch edge
c22cacf3 4372 of conditional jump, remove it. */
628f6a4e 4373 if (EDGE_COUNT (src->succs) == 2)
9ee634e3 4374 {
03101c6f
KH
4375 /* Find the edge that is different from E. */
4376 edge s = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e);
628f6a4e 4377
9ee634e3 4378 if (s->dest == dest
a813c111
SB
4379 && any_condjump_p (BB_END (src))
4380 && onlyjump_p (BB_END (src)))
4381 delete_insn (BB_END (src));
9ee634e3 4382 }
c263766c 4383 if (dump_file)
dc764d10 4384 fprintf (dump_file, "Redirecting fallthru edge %i->%i to %i\n",
bc35512f 4385 e->src->index, e->dest->index, dest->index);
0c617be4 4386 ret = redirect_edge_succ_nodup (e, dest);
9ee634e3
JH
4387 }
4388 else
bc35512f 4389 ret = redirect_branch_edge (e, dest);
9ee634e3
JH
4390
4391 /* We don't want simplejumps in the insn stream during cfglayout. */
341c100f 4392 gcc_assert (!simplejump_p (BB_END (src)));
9ee634e3 4393
6fb5fa3c 4394 df_set_bb_dirty (src);
9ee634e3
JH
4395 return ret;
4396}
4397
4398/* Simple wrapper as we always can redirect fallthru edges. */
4399static basic_block
d329e058 4400cfg_layout_redirect_edge_and_branch_force (edge e, basic_block dest)
9ee634e3 4401{
341c100f
NS
4402 edge redirected = cfg_layout_redirect_edge_and_branch (e, dest);
4403
4404 gcc_assert (redirected);
9ee634e3
JH
4405 return NULL;
4406}
4407
f470c378
ZD
4408/* Same as delete_basic_block but update cfg_layout structures. */
4409
9ee634e3 4410static void
d329e058 4411cfg_layout_delete_block (basic_block bb)
9ee634e3 4412{
a813c111 4413 rtx insn, next, prev = PREV_INSN (BB_HEAD (bb)), *to, remaints;
9ee634e3 4414
bcc708fc 4415 if (BB_HEADER (bb))
9ee634e3 4416 {
a813c111 4417 next = BB_HEAD (bb);
9ee634e3 4418 if (prev)
0f82e5c9 4419 SET_NEXT_INSN (prev) = BB_HEADER (bb);
9ee634e3 4420 else
bcc708fc 4421 set_first_insn (BB_HEADER (bb));
0f82e5c9 4422 SET_PREV_INSN (BB_HEADER (bb)) = prev;
bcc708fc 4423 insn = BB_HEADER (bb);
9ee634e3
JH
4424 while (NEXT_INSN (insn))
4425 insn = NEXT_INSN (insn);
0f82e5c9
DM
4426 SET_NEXT_INSN (insn) = next;
4427 SET_PREV_INSN (next) = insn;
9ee634e3 4428 }
a813c111 4429 next = NEXT_INSN (BB_END (bb));
bcc708fc 4430 if (BB_FOOTER (bb))
9ee634e3 4431 {
bcc708fc 4432 insn = BB_FOOTER (bb);
bc35512f
JH
4433 while (insn)
4434 {
4b4bf941 4435 if (BARRIER_P (insn))
bc35512f
JH
4436 {
4437 if (PREV_INSN (insn))
0f82e5c9 4438 SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
bc35512f 4439 else
190bea87 4440 SET_BB_FOOTER (bb) = NEXT_INSN (insn);
bc35512f 4441 if (NEXT_INSN (insn))
0f82e5c9 4442 SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
bc35512f 4443 }
4b4bf941 4444 if (LABEL_P (insn))
bc35512f
JH
4445 break;
4446 insn = NEXT_INSN (insn);
4447 }
bcc708fc 4448 if (BB_FOOTER (bb))
bc35512f 4449 {
a813c111 4450 insn = BB_END (bb);
0f82e5c9
DM
4451 SET_NEXT_INSN (insn) = BB_FOOTER (bb);
4452 SET_PREV_INSN (BB_FOOTER (bb)) = insn;
bc35512f
JH
4453 while (NEXT_INSN (insn))
4454 insn = NEXT_INSN (insn);
0f82e5c9 4455 SET_NEXT_INSN (insn) = next;
bc35512f 4456 if (next)
0f82e5c9 4457 SET_PREV_INSN (next) = insn;
bc35512f
JH
4458 else
4459 set_last_insn (insn);
4460 }
9ee634e3 4461 }
fefa31b5 4462 if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
190bea87 4463 to = &SET_BB_HEADER (bb->next_bb);
9ee634e3
JH
4464 else
4465 to = &cfg_layout_function_footer;
997de8ed 4466
9ee634e3
JH
4467 rtl_delete_block (bb);
4468
4469 if (prev)
4470 prev = NEXT_INSN (prev);
d329e058 4471 else
9ee634e3
JH
4472 prev = get_insns ();
4473 if (next)
4474 next = PREV_INSN (next);
d329e058 4475 else
9ee634e3
JH
4476 next = get_last_insn ();
4477
4478 if (next && NEXT_INSN (next) != prev)
4479 {
4480 remaints = unlink_insn_chain (prev, next);
4481 insn = remaints;
4482 while (NEXT_INSN (insn))
4483 insn = NEXT_INSN (insn);
0f82e5c9 4484 SET_NEXT_INSN (insn) = *to;
9ee634e3 4485 if (*to)
0f82e5c9 4486 SET_PREV_INSN (*to) = insn;
9ee634e3
JH
4487 *to = remaints;
4488 }
4489}
4490
beb235f8 4491/* Return true when blocks A and B can be safely merged. */
b48d0358 4492
bc35512f 4493static bool
b48d0358 4494cfg_layout_can_merge_blocks_p (basic_block a, basic_block b)
bc35512f 4495{
750054a2
CT
4496 /* If we are partitioning hot/cold basic blocks, we don't want to
4497 mess up unconditional or indirect jumps that cross between hot
076c7ab8
ZW
4498 and cold sections.
4499
8e8d5162 4500 Basic block partitioning may result in some jumps that appear to
c22cacf3
MS
4501 be optimizable (or blocks that appear to be mergeable), but which really
4502 must be left untouched (they are required to make it safely across
4503 partition boundaries). See the comments at the top of
8e8d5162
CT
4504 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
4505
87c8b4be 4506 if (BB_PARTITION (a) != BB_PARTITION (b))
076c7ab8 4507 return false;
750054a2 4508
7d776ee2
RG
4509 /* Protect the loop latches. */
4510 if (current_loops && b->loop_father->latch == b)
4511 return false;
4512
894a84b5
BS
4513 /* If we would end up moving B's instructions, make sure it doesn't fall
4514 through into the exit block, since we cannot recover from a fallthrough
4515 edge into the exit block occurring in the middle of a function. */
4516 if (NEXT_INSN (BB_END (a)) != BB_HEAD (b))
4517 {
4518 edge e = find_fallthru_edge (b->succs);
fefa31b5 4519 if (e && e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
894a84b5
BS
4520 return false;
4521 }
4522
bc35512f 4523 /* There must be exactly one edge in between the blocks. */
c5cbcccf
ZD
4524 return (single_succ_p (a)
4525 && single_succ (a) == b
4526 && single_pred_p (b) == 1
628f6a4e 4527 && a != b
bc35512f 4528 /* Must be simple edge. */
c5cbcccf 4529 && !(single_succ_edge (a)->flags & EDGE_COMPLEX)
fefa31b5
DM
4530 && a != ENTRY_BLOCK_PTR_FOR_FN (cfun)
4531 && b != EXIT_BLOCK_PTR_FOR_FN (cfun)
49ea3702
SB
4532 /* If the jump insn has side effects, we can't kill the edge.
4533 When not optimizing, try_redirect_by_replacing_jump will
4534 not allow us to redirect an edge by replacing a table jump. */
4b4bf941 4535 && (!JUMP_P (BB_END (a))
49ea3702 4536 || ((!optimize || reload_completed)
a813c111 4537 ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
bc35512f
JH
4538}
4539
41806d92
NS
4540/* Merge block A and B. The blocks must be mergeable. */
4541
bc35512f
JH
4542static void
4543cfg_layout_merge_blocks (basic_block a, basic_block b)
4544{
50a36e42 4545 bool forwarder_p = (b->flags & BB_FORWARDER_BLOCK) != 0;
f9df6f16 4546 rtx insn;
50a36e42 4547
77a74ed7 4548 gcc_checking_assert (cfg_layout_can_merge_blocks_p (a, b));
bc35512f 4549
6fb5fa3c 4550 if (dump_file)
50a36e42
EB
4551 fprintf (dump_file, "Merging block %d into block %d...\n", b->index,
4552 a->index);
6fb5fa3c 4553
bc35512f 4554 /* If there was a CODE_LABEL beginning B, delete it. */
4b4bf941 4555 if (LABEL_P (BB_HEAD (b)))
2c97f8e4 4556 {
2c97f8e4
RH
4557 delete_insn (BB_HEAD (b));
4558 }
bc35512f
JH
4559
4560 /* We should have fallthru edge in a, or we can do dummy redirection to get
4561 it cleaned up. */
4b4bf941 4562 if (JUMP_P (BB_END (a)))
628f6a4e 4563 try_redirect_by_replacing_jump (EDGE_SUCC (a, 0), b, true);
341c100f 4564 gcc_assert (!JUMP_P (BB_END (a)));
bc35512f 4565
8a829274 4566 /* When not optimizing and the edge is the only place in RTL which holds
7241571e 4567 some unique locus, emit a nop with that locus in between. */
9be94227
EB
4568 if (!optimize)
4569 emit_nop_for_unique_locus_between (a, b);
7241571e 4570
4c8af858
SB
4571 /* Move things from b->footer after a->footer. */
4572 if (BB_FOOTER (b))
bc35512f 4573 {
4c8af858 4574 if (!BB_FOOTER (a))
190bea87 4575 SET_BB_FOOTER (a) = SET_BB_FOOTER (b);
4c8af858
SB
4576 else
4577 {
4578 rtx last = BB_FOOTER (a);
4579
4580 while (NEXT_INSN (last))
4581 last = NEXT_INSN (last);
0f82e5c9
DM
4582 SET_NEXT_INSN (last) = BB_FOOTER (b);
4583 SET_PREV_INSN (BB_FOOTER (b)) = last;
4c8af858 4584 }
190bea87 4585 SET_BB_FOOTER (b) = NULL;
4c8af858
SB
4586 }
4587
4588 /* Move things from b->header before a->footer.
4589 Note that this may include dead tablejump data, but we don't clean
4590 those up until we go out of cfglayout mode. */
4591 if (BB_HEADER (b))
4592 {
4593 if (! BB_FOOTER (a))
190bea87 4594 SET_BB_FOOTER (a) = BB_HEADER (b);
4c8af858
SB
4595 else
4596 {
4597 rtx last = BB_HEADER (b);
4598
4599 while (NEXT_INSN (last))
4600 last = NEXT_INSN (last);
0f82e5c9
DM
4601 SET_NEXT_INSN (last) = BB_FOOTER (a);
4602 SET_PREV_INSN (BB_FOOTER (a)) = last;
190bea87 4603 SET_BB_FOOTER (a) = BB_HEADER (b);
4c8af858 4604 }
190bea87 4605 SET_BB_HEADER (b) = NULL;
bc35512f
JH
4606 }
4607
4608 /* In the case basic blocks are not adjacent, move them around. */
a813c111 4609 if (NEXT_INSN (BB_END (a)) != BB_HEAD (b))
bc35512f 4610 {
f9df6f16 4611 insn = unlink_insn_chain (BB_HEAD (b), BB_END (b));
6c3d0e31 4612
f9df6f16 4613 emit_insn_after_noloc (insn, BB_END (a), a);
bc35512f
JH
4614 }
4615 /* Otherwise just re-associate the instructions. */
4616 else
4617 {
a813c111 4618 insn = BB_HEAD (b);
190bea87 4619 SET_BB_END (a) = BB_END (b);
bc35512f
JH
4620 }
4621
f9df6f16
JJ
4622 /* emit_insn_after_noloc doesn't call df_insn_change_bb.
4623 We need to explicitly call. */
4624 update_bb_for_insn_chain (insn, BB_END (b), a);
4625
4626 /* Skip possible DELETED_LABEL insn. */
4627 if (!NOTE_INSN_BASIC_BLOCK_P (insn))
4628 insn = NEXT_INSN (insn);
4629 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
190bea87 4630 SET_BB_HEAD (b) = SET_BB_END (b) = NULL;
f9df6f16
JJ
4631 delete_insn (insn);
4632
6fb5fa3c
DB
4633 df_bb_delete (b->index);
4634
50a36e42 4635 /* If B was a forwarder block, propagate the locus on the edge. */
2f13f2de 4636 if (forwarder_p
fbc68f2a 4637 && LOCATION_LOCUS (EDGE_SUCC (b, 0)->goto_locus) == UNKNOWN_LOCATION)
50a36e42
EB
4638 EDGE_SUCC (b, 0)->goto_locus = EDGE_SUCC (a, 0)->goto_locus;
4639
c263766c 4640 if (dump_file)
50a36e42 4641 fprintf (dump_file, "Merged blocks %d and %d.\n", a->index, b->index);
bc35512f
JH
4642}
4643
4644/* Split edge E. */
f470c378 4645
bc35512f
JH
4646static basic_block
4647cfg_layout_split_edge (edge e)
4648{
bc35512f 4649 basic_block new_bb =
fefa31b5 4650 create_basic_block (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
a813c111 4651 ? NEXT_INSN (BB_END (e->src)) : get_insns (),
bc35512f
JH
4652 NULL_RTX, e->src);
4653
fefa31b5 4654 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
3e7eb734
JJ
4655 BB_COPY_PARTITION (new_bb, e->src);
4656 else
4657 BB_COPY_PARTITION (new_bb, e->dest);
a9b2ee88 4658 make_edge (new_bb, e->dest, EDGE_FALLTHRU);
bc35512f
JH
4659 redirect_edge_and_branch_force (e, new_bb);
4660
4661 return new_bb;
4662}
4663
f470c378
ZD
4664/* Do postprocessing after making a forwarder block joined by edge FALLTHRU. */
4665
4666static void
4667rtl_make_forwarder_block (edge fallthru ATTRIBUTE_UNUSED)
4668{
4669}
4670
df92c640
SB
4671/* Return true if BB contains only labels or non-executable
4672 instructions. */
4673
4674static bool
4675rtl_block_empty_p (basic_block bb)
4676{
4677 rtx insn;
4678
fefa31b5
DM
4679 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)
4680 || bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
df92c640
SB
4681 return true;
4682
4683 FOR_BB_INSNS (bb, insn)
4684 if (NONDEBUG_INSN_P (insn) && !any_uncondjump_p (insn))
4685 return false;
4686
4687 return true;
4688}
4689
4690/* Split a basic block if it ends with a conditional branch and if
4691 the other part of the block is not empty. */
4692
4693static basic_block
4694rtl_split_block_before_cond_jump (basic_block bb)
4695{
4696 rtx insn;
4697 rtx split_point = NULL;
4698 rtx last = NULL;
4699 bool found_code = false;
4700
4701 FOR_BB_INSNS (bb, insn)
4702 {
4703 if (any_condjump_p (insn))
4704 split_point = last;
4705 else if (NONDEBUG_INSN_P (insn))
4706 found_code = true;
4707 last = insn;
4708 }
4709
4710 /* Did not find everything. */
4711 if (found_code && split_point)
4712 return split_block (bb, split_point)->dest;
4713 else
4714 return NULL;
4715}
4716
6de9cd9a
DN
4717/* Return 1 if BB ends with a call, possibly followed by some
4718 instructions that must stay with the call, 0 otherwise. */
4719
4720static bool
b48d0358 4721rtl_block_ends_with_call_p (basic_block bb)
6de9cd9a
DN
4722{
4723 rtx insn = BB_END (bb);
4724
4b4bf941 4725 while (!CALL_P (insn)
6de9cd9a 4726 && insn != BB_HEAD (bb)
92ddef69 4727 && (keep_with_call_p (insn)
b5b8b0ac
AO
4728 || NOTE_P (insn)
4729 || DEBUG_INSN_P (insn)))
6de9cd9a 4730 insn = PREV_INSN (insn);
4b4bf941 4731 return (CALL_P (insn));
6de9cd9a
DN
4732}
4733
4734/* Return 1 if BB ends with a conditional branch, 0 otherwise. */
4735
4736static bool
9678086d 4737rtl_block_ends_with_condjump_p (const_basic_block bb)
6de9cd9a
DN
4738{
4739 return any_condjump_p (BB_END (bb));
4740}
4741
4742/* Return true if we need to add fake edge to exit.
4743 Helper function for rtl_flow_call_edges_add. */
4744
4745static bool
9678086d 4746need_fake_edge_p (const_rtx insn)
6de9cd9a
DN
4747{
4748 if (!INSN_P (insn))
4749 return false;
4750
4b4bf941 4751 if ((CALL_P (insn)
6de9cd9a
DN
4752 && !SIBLING_CALL_P (insn)
4753 && !find_reg_note (insn, REG_NORETURN, NULL)
becfd6e5 4754 && !(RTL_CONST_OR_PURE_CALL_P (insn))))
6de9cd9a
DN
4755 return true;
4756
4757 return ((GET_CODE (PATTERN (insn)) == ASM_OPERANDS
4758 && MEM_VOLATILE_P (PATTERN (insn)))
4759 || (GET_CODE (PATTERN (insn)) == PARALLEL
4760 && asm_noperands (insn) != -1
4761 && MEM_VOLATILE_P (XVECEXP (PATTERN (insn), 0, 0)))
4762 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
4763}
4764
4765/* Add fake edges to the function exit for any non constant and non noreturn
4766 calls, volatile inline assembly in the bitmap of blocks specified by
4767 BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
4768 that were split.
4769
4770 The goal is to expose cases in which entering a basic block does not imply
4771 that all subsequent instructions must be executed. */
4772
4773static int
4774rtl_flow_call_edges_add (sbitmap blocks)
4775{
4776 int i;
4777 int blocks_split = 0;
8b1c6fd7 4778 int last_bb = last_basic_block_for_fn (cfun);
6de9cd9a
DN
4779 bool check_last_block = false;
4780
0cae8d31 4781 if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS)
6de9cd9a
DN
4782 return 0;
4783
4784 if (! blocks)
4785 check_last_block = true;
4786 else
fefa31b5
DM
4787 check_last_block = bitmap_bit_p (blocks,
4788 EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb->index);
6de9cd9a
DN
4789
4790 /* In the last basic block, before epilogue generation, there will be
4791 a fallthru edge to EXIT. Special care is required if the last insn
4792 of the last basic block is a call because make_edge folds duplicate
4793 edges, which would result in the fallthru edge also being marked
4794 fake, which would result in the fallthru edge being removed by
4795 remove_fake_edges, which would result in an invalid CFG.
4796
4797 Moreover, we can't elide the outgoing fake edge, since the block
4798 profiler needs to take this into account in order to solve the minimal
4799 spanning tree in the case that the call doesn't return.
4800
4801 Handle this by adding a dummy instruction in a new last basic block. */
4802 if (check_last_block)
4803 {
fefa31b5 4804 basic_block bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
6de9cd9a
DN
4805 rtx insn = BB_END (bb);
4806
4807 /* Back up past insns that must be kept in the same block as a call. */
4808 while (insn != BB_HEAD (bb)
4809 && keep_with_call_p (insn))
4810 insn = PREV_INSN (insn);
4811
4812 if (need_fake_edge_p (insn))
4813 {
4814 edge e;
4815
fefa31b5 4816 e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
9ff3d2de
JL
4817 if (e)
4818 {
c41c1387 4819 insert_insn_on_edge (gen_use (const0_rtx), e);
9ff3d2de
JL
4820 commit_edge_insertions ();
4821 }
6de9cd9a
DN
4822 }
4823 }
4824
4825 /* Now add fake edges to the function exit for any non constant
4826 calls since there is no way that we can determine if they will
4827 return or not... */
4828
24bd1a0b 4829 for (i = NUM_FIXED_BLOCKS; i < last_bb; i++)
6de9cd9a 4830 {
06e28de2 4831 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
6de9cd9a
DN
4832 rtx insn;
4833 rtx prev_insn;
4834
4835 if (!bb)
4836 continue;
4837
d7c028c0 4838 if (blocks && !bitmap_bit_p (blocks, i))
6de9cd9a
DN
4839 continue;
4840
4841 for (insn = BB_END (bb); ; insn = prev_insn)
4842 {
4843 prev_insn = PREV_INSN (insn);
4844 if (need_fake_edge_p (insn))
4845 {
4846 edge e;
4847 rtx split_at_insn = insn;
4848
4849 /* Don't split the block between a call and an insn that should
c22cacf3 4850 remain in the same block as the call. */
4b4bf941 4851 if (CALL_P (insn))
6de9cd9a
DN
4852 while (split_at_insn != BB_END (bb)
4853 && keep_with_call_p (NEXT_INSN (split_at_insn)))
4854 split_at_insn = NEXT_INSN (split_at_insn);
4855
4856 /* The handling above of the final block before the epilogue
c22cacf3 4857 should be enough to verify that there is no edge to the exit
6de9cd9a
DN
4858 block in CFG already. Calling make_edge in such case would
4859 cause us to mark that edge as fake and remove it later. */
4860
4861#ifdef ENABLE_CHECKING
4862 if (split_at_insn == BB_END (bb))
628f6a4e 4863 {
fefa31b5 4864 e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
9ff3d2de 4865 gcc_assert (e == NULL);
628f6a4e 4866 }
6de9cd9a
DN
4867#endif
4868
4869 /* Note that the following may create a new basic block
4870 and renumber the existing basic blocks. */
4871 if (split_at_insn != BB_END (bb))
4872 {
4873 e = split_block (bb, split_at_insn);
4874 if (e)
4875 blocks_split++;
4876 }
4877
fefa31b5 4878 make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FAKE);
6de9cd9a
DN
4879 }
4880
4881 if (insn == BB_HEAD (bb))
4882 break;
4883 }
4884 }
4885
4886 if (blocks_split)
4887 verify_flow_info ();
4888
4889 return blocks_split;
4890}
4891
1cb7dfc3 4892/* Add COMP_RTX as a condition at end of COND_BB. FIRST_HEAD is
315682fb 4893 the conditional branch target, SECOND_HEAD should be the fall-thru
1cb7dfc3
MH
4894 there is no need to handle this here the loop versioning code handles
4895 this. the reason for SECON_HEAD is that it is needed for condition
4896 in trees, and this should be of the same type since it is a hook. */
4897static void
4898rtl_lv_add_condition_to_bb (basic_block first_head ,
c22cacf3
MS
4899 basic_block second_head ATTRIBUTE_UNUSED,
4900 basic_block cond_bb, void *comp_rtx)
1cb7dfc3
MH
4901{
4902 rtx label, seq, jump;
4903 rtx op0 = XEXP ((rtx)comp_rtx, 0);
4904 rtx op1 = XEXP ((rtx)comp_rtx, 1);
4905 enum rtx_code comp = GET_CODE ((rtx)comp_rtx);
4906 enum machine_mode mode;
4907
4908
4909 label = block_label (first_head);
4910 mode = GET_MODE (op0);
4911 if (mode == VOIDmode)
4912 mode = GET_MODE (op1);
4913
4914 start_sequence ();
4915 op0 = force_operand (op0, NULL_RTX);
4916 op1 = force_operand (op1, NULL_RTX);
4917 do_compare_rtx_and_jump (op0, op1, comp, 0,
40e90eac 4918 mode, NULL_RTX, NULL_RTX, label, -1);
1cb7dfc3
MH
4919 jump = get_last_insn ();
4920 JUMP_LABEL (jump) = label;
4921 LABEL_NUSES (label)++;
4922 seq = get_insns ();
4923 end_sequence ();
4924
4925 /* Add the new cond , in the new head. */
c3284718 4926 emit_insn_after (seq, BB_END (cond_bb));
1cb7dfc3
MH
4927}
4928
4929
4930/* Given a block B with unconditional branch at its end, get the
4931 store the return the branch edge and the fall-thru edge in
4932 BRANCH_EDGE and FALLTHRU_EDGE respectively. */
4933static void
4934rtl_extract_cond_bb_edges (basic_block b, edge *branch_edge,
4935 edge *fallthru_edge)
4936{
4937 edge e = EDGE_SUCC (b, 0);
4938
4939 if (e->flags & EDGE_FALLTHRU)
4940 {
4941 *fallthru_edge = e;
4942 *branch_edge = EDGE_SUCC (b, 1);
4943 }
4944 else
4945 {
4946 *branch_edge = e;
4947 *fallthru_edge = EDGE_SUCC (b, 1);
4948 }
4949}
4950
5e2d947c
JH
4951void
4952init_rtl_bb_info (basic_block bb)
4953{
bcc708fc
MM
4954 gcc_assert (!bb->il.x.rtl);
4955 bb->il.x.head_ = NULL;
766090c2 4956 bb->il.x.rtl = ggc_cleared_alloc<rtl_bb_info> ();
5e2d947c
JH
4957}
4958
14fa2cc0
ZD
4959/* Returns true if it is possible to remove edge E by redirecting
4960 it to the destination of the other edge from E->src. */
4961
4962static bool
9678086d 4963rtl_can_remove_branch_p (const_edge e)
14fa2cc0 4964{
9678086d
KG
4965 const_basic_block src = e->src;
4966 const_basic_block target = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest;
4967 const_rtx insn = BB_END (src), set;
14fa2cc0
ZD
4968
4969 /* The conditions are taken from try_redirect_by_replacing_jump. */
fefa31b5 4970 if (target == EXIT_BLOCK_PTR_FOR_FN (cfun))
14fa2cc0
ZD
4971 return false;
4972
4973 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
4974 return false;
4975
3371a64f 4976 if (BB_PARTITION (src) != BB_PARTITION (target))
14fa2cc0
ZD
4977 return false;
4978
4979 if (!onlyjump_p (insn)
4980 || tablejump_p (insn, NULL, NULL))
4981 return false;
4982
4983 set = single_set (insn);
4984 if (!set || side_effects_p (set))
4985 return false;
4986
4987 return true;
4988}
4989
ffe14686
AM
4990static basic_block
4991rtl_duplicate_bb (basic_block bb)
4992{
4993 bb = cfg_layout_duplicate_bb (bb);
4994 bb->aux = NULL;
4995 return bb;
4996}
4997
aa4723d7
SB
4998/* Do book-keeping of basic block BB for the profile consistency checker.
4999 If AFTER_PASS is 0, do pre-pass accounting, or if AFTER_PASS is 1
5000 then do post-pass accounting. Store the counting in RECORD. */
5001static void
5002rtl_account_profile_record (basic_block bb, int after_pass,
5003 struct profile_record *record)
5004{
5005 rtx insn;
5006 FOR_BB_INSNS (bb, insn)
5007 if (INSN_P (insn))
5008 {
5009 record->size[after_pass]
5010 += insn_rtx_cost (PATTERN (insn), false);
0a6a6ac9 5011 if (profile_status_for_fn (cfun) == PROFILE_READ)
aa4723d7
SB
5012 record->time[after_pass]
5013 += insn_rtx_cost (PATTERN (insn), true) * bb->count;
0a6a6ac9 5014 else if (profile_status_for_fn (cfun) == PROFILE_GUESSED)
aa4723d7
SB
5015 record->time[after_pass]
5016 += insn_rtx_cost (PATTERN (insn), true) * bb->frequency;
5017 }
5018}
5019
9ee634e3
JH
5020/* Implementation of CFG manipulation for linearized RTL. */
5021struct cfg_hooks rtl_cfg_hooks = {
f470c378 5022 "rtl",
9ee634e3 5023 rtl_verify_flow_info,
10e9fecc 5024 rtl_dump_bb,
2c895bd1 5025 rtl_dump_bb_for_graph,
bc35512f 5026 rtl_create_basic_block,
9ee634e3
JH
5027 rtl_redirect_edge_and_branch,
5028 rtl_redirect_edge_and_branch_force,
14fa2cc0 5029 rtl_can_remove_branch_p,
9ee634e3
JH
5030 rtl_delete_block,
5031 rtl_split_block,
f470c378 5032 rtl_move_block_after,
bc35512f
JH
5033 rtl_can_merge_blocks, /* can_merge_blocks_p */
5034 rtl_merge_blocks,
6de9cd9a
DN
5035 rtl_predict_edge,
5036 rtl_predicted_by_p,
ffe14686
AM
5037 cfg_layout_can_duplicate_bb_p,
5038 rtl_duplicate_bb,
f470c378
ZD
5039 rtl_split_edge,
5040 rtl_make_forwarder_block,
6de9cd9a 5041 rtl_tidy_fallthru_edge,
cf103ca4 5042 rtl_force_nonfallthru,
6de9cd9a
DN
5043 rtl_block_ends_with_call_p,
5044 rtl_block_ends_with_condjump_p,
d9d4706f
KH
5045 rtl_flow_call_edges_add,
5046 NULL, /* execute_on_growing_pred */
1cb7dfc3
MH
5047 NULL, /* execute_on_shrinking_pred */
5048 NULL, /* duplicate loop for trees */
5049 NULL, /* lv_add_condition_to_bb */
5050 NULL, /* lv_adjust_loop_header_phi*/
5051 NULL, /* extract_cond_bb_edges */
df92c640
SB
5052 NULL, /* flush_pending_stmts */
5053 rtl_block_empty_p, /* block_empty_p */
5054 rtl_split_block_before_cond_jump, /* split_block_before_cond_jump */
aa4723d7 5055 rtl_account_profile_record,
9ee634e3
JH
5056};
5057
5058/* Implementation of CFG manipulation for cfg layout RTL, where
5059 basic block connected via fallthru edges does not have to be adjacent.
5060 This representation will hopefully become the default one in future
5061 version of the compiler. */
6de9cd9a 5062
9ee634e3 5063struct cfg_hooks cfg_layout_rtl_cfg_hooks = {
f470c378 5064 "cfglayout mode",
bc35512f 5065 rtl_verify_flow_info_1,
10e9fecc 5066 rtl_dump_bb,
2c895bd1 5067 rtl_dump_bb_for_graph,
bc35512f 5068 cfg_layout_create_basic_block,
9ee634e3
JH
5069 cfg_layout_redirect_edge_and_branch,
5070 cfg_layout_redirect_edge_and_branch_force,
14fa2cc0 5071 rtl_can_remove_branch_p,
9ee634e3
JH
5072 cfg_layout_delete_block,
5073 cfg_layout_split_block,
f470c378 5074 rtl_move_block_after,
bc35512f
JH
5075 cfg_layout_can_merge_blocks_p,
5076 cfg_layout_merge_blocks,
6de9cd9a
DN
5077 rtl_predict_edge,
5078 rtl_predicted_by_p,
5079 cfg_layout_can_duplicate_bb_p,
5080 cfg_layout_duplicate_bb,
f470c378
ZD
5081 cfg_layout_split_edge,
5082 rtl_make_forwarder_block,
cf103ca4
EB
5083 NULL, /* tidy_fallthru_edge */
5084 rtl_force_nonfallthru,
6de9cd9a
DN
5085 rtl_block_ends_with_call_p,
5086 rtl_block_ends_with_condjump_p,
d9d4706f
KH
5087 rtl_flow_call_edges_add,
5088 NULL, /* execute_on_growing_pred */
1cb7dfc3
MH
5089 NULL, /* execute_on_shrinking_pred */
5090 duplicate_loop_to_header_edge, /* duplicate loop for trees */
5091 rtl_lv_add_condition_to_bb, /* lv_add_condition_to_bb */
5092 NULL, /* lv_adjust_loop_header_phi*/
5093 rtl_extract_cond_bb_edges, /* extract_cond_bb_edges */
df92c640
SB
5094 NULL, /* flush_pending_stmts */
5095 rtl_block_empty_p, /* block_empty_p */
5096 rtl_split_block_before_cond_jump, /* split_block_before_cond_jump */
aa4723d7 5097 rtl_account_profile_record,
9ee634e3 5098};
78bde837 5099
ef0fe503
DM
5100/* BB_HEAD as an rvalue. */
5101
5102rtx_insn *BB_HEAD (const_basic_block bb)
5103{
5104 rtx insn = bb->il.x.head_;
5105 return safe_as_a <rtx_insn *> (insn);
5106}
5107
5108/* BB_HEAD for use as an lvalue. */
5109
5110rtx& SET_BB_HEAD (basic_block bb)
5111{
5112 return bb->il.x.head_;
5113}
5114
5115/* BB_END as an rvalue. */
5116
5117rtx_insn *BB_END (const_basic_block bb)
5118{
5119 rtx insn = bb->il.x.rtl->end_;
5120 return safe_as_a <rtx_insn *> (insn);
5121}
5122
5123/* BB_END as an lvalue. */
5124
5125rtx& SET_BB_END (basic_block bb)
5126{
5127 return bb->il.x.rtl->end_;
5128}
5129
5130/* BB_HEADER as an rvalue. */
5131
5132rtx_insn *BB_HEADER (const_basic_block bb)
5133{
5134 rtx insn = bb->il.x.rtl->header_;
5135 return safe_as_a <rtx_insn *> (insn);
5136}
5137
5138/* BB_HEADER as an lvalue. */
5139
5140rtx& SET_BB_HEADER (basic_block bb)
5141{
5142 return bb->il.x.rtl->header_;
5143}
5144
5145/* BB_FOOTER as an rvalue. */
5146
5147rtx_insn *BB_FOOTER (const_basic_block bb)
5148{
5149 rtx insn = bb->il.x.rtl->footer_;
5150 return safe_as_a <rtx_insn *> (insn);
5151}
5152
5153/* BB_FOOTER as an lvalue. */
5154
5155rtx& SET_BB_FOOTER (basic_block bb)
5156{
5157 return bb->il.x.rtl->footer_;
5158}
5159
78bde837 5160#include "gt-cfgrtl.h"