Line data Source code
1 : /* Control flow graph manipulation code for GNU compiler.
2 : Copyright (C) 1987-2026 Free Software Foundation, Inc.
3 :
4 : This file is part of GCC.
5 :
6 : GCC is free software; you can redistribute it and/or modify it under
7 : the terms of the GNU General Public License as published by the Free
8 : Software Foundation; either version 3, or (at your option) any later
9 : version.
10 :
11 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with GCC; see the file COPYING3. If not see
18 : <http://www.gnu.org/licenses/>. */
19 :
20 : /* This file contains low level functions to manipulate the CFG and analyze it
21 : that are aware of the RTL intermediate language.
22 :
23 : Available functionality:
24 : - Basic CFG/RTL manipulation API documented in cfghooks.h
25 : - CFG-aware instruction chain manipulation
26 : delete_insn, delete_insn_chain
27 : - Edge splitting and committing to edges
28 : insert_insn_on_edge, prepend_insn_to_edge, commit_edge_insertions
29 : - CFG updating after insn simplification
30 : purge_dead_edges, purge_all_dead_edges
31 : - CFG fixing after coarse manipulation
32 : fixup_abnormal_edges
33 :
34 : Functions not supposed for generic use:
35 : - Infrastructure to determine quickly basic block for insn
36 : compute_bb_for_insn, update_bb_for_insn, set_block_for_insn,
37 : - Edge redirection with updating and optimizing of insn chain
38 : block_label, tidy_fallthru_edge, force_nonfallthru */
39 :
40 : #include "config.h"
41 : #include "system.h"
42 : #include "coretypes.h"
43 : #include "backend.h"
44 : #include "target.h"
45 : #include "rtl.h"
46 : #include "tree.h"
47 : #include "cfghooks.h"
48 : #include "df.h"
49 : #include "insn-config.h"
50 : #include "memmodel.h"
51 : #include "emit-rtl.h"
52 : #include "cfgrtl.h"
53 : #include "cfganal.h"
54 : #include "cfgbuild.h"
55 : #include "cfgcleanup.h"
56 : #include "bb-reorder.h"
57 : #include "rtl-error.h"
58 : #include "insn-attr.h"
59 : #include "dojump.h"
60 : #include "expr.h"
61 : #include "cfgloop.h"
62 : #include "tree-pass.h"
63 : #include "print-rtl.h"
64 : #include "rtl-iter.h"
65 : #include "profile.h"
66 : #include "sreal.h"
67 :
68 : /* Disable warnings about missing quoting in GCC diagnostics. */
69 : #if __GNUC__ >= 10
70 : # pragma GCC diagnostic push
71 : # pragma GCC diagnostic ignored "-Wformat-diag"
72 : #endif
73 :
74 : /* Holds the interesting leading and trailing notes for the function.
75 : Only applicable if the CFG is in cfglayout mode. */
76 : static GTY(()) rtx_insn *cfg_layout_function_footer;
77 : static GTY(()) rtx_insn *cfg_layout_function_header;
78 :
79 : static rtx_insn *skip_insns_after_block (basic_block);
80 : static void record_effective_endpoints (void);
81 : static void fixup_reorder_chain (void);
82 :
83 : void verify_insn_chain (void);
84 : static void fixup_fallthru_exit_predecessor (void);
85 : static bool can_delete_note_p (const rtx_note *);
86 : static bool can_delete_label_p (const rtx_code_label *);
87 : static basic_block rtl_split_edge (edge);
88 : static bool rtl_move_block_after (basic_block, basic_block);
89 : static bool rtl_verify_flow_info (void);
90 : static basic_block cfg_layout_split_block (basic_block, void *);
91 : static edge cfg_layout_redirect_edge_and_branch (edge, basic_block);
92 : static basic_block cfg_layout_redirect_edge_and_branch_force (edge, basic_block);
93 : static void cfg_layout_delete_block (basic_block);
94 : static void rtl_delete_block (basic_block);
95 : static basic_block rtl_redirect_edge_and_branch_force (edge, basic_block);
96 : static edge rtl_redirect_edge_and_branch (edge, basic_block);
97 : static basic_block rtl_split_block (basic_block, void *);
98 : static void rtl_dump_bb (FILE *, basic_block, int, dump_flags_t);
99 : static bool rtl_verify_flow_info_1 (void);
100 : static void rtl_make_forwarder_block (edge);
101 : static bool rtl_bb_info_initialized_p (basic_block bb);
102 :
103 : /* Return true if NOTE is not one of the ones that must be kept paired,
104 : so that we may simply delete it. */
105 :
106 : static bool
107 11045956 : can_delete_note_p (const rtx_note *note)
108 : {
109 0 : switch (NOTE_KIND (note))
110 : {
111 : case NOTE_INSN_DELETED:
112 : case NOTE_INSN_BASIC_BLOCK:
113 : case NOTE_INSN_EPILOGUE_BEG:
114 : return true;
115 :
116 0 : default:
117 0 : return false;
118 : }
119 : }
120 :
121 : /* True if a given label can be deleted. */
122 :
123 : static bool
124 7911604 : can_delete_label_p (const rtx_code_label *label)
125 : {
126 7911604 : return (!LABEL_PRESERVE_P (label)
127 : /* User declared labels must be preserved. */
128 7902742 : && LABEL_NAME (label) == 0
129 7911604 : && !vec_safe_contains<rtx_insn *> (forced_labels,
130 7851892 : const_cast<rtx_code_label *> (label)));
131 : }
132 :
133 : /* Delete INSN by patching it out. */
134 :
135 : void
136 134028393 : delete_insn (rtx_insn *insn)
137 : {
138 134028393 : rtx note;
139 134028393 : bool really_delete = true;
140 :
141 134028393 : if (rtx_code_label *label = dyn_cast<rtx_code_label *> (insn))
142 : {
143 : /* Some labels can't be directly removed from the INSN chain, as they
144 : might be references via variables, constant pool etc.
145 : Convert them to the special NOTE_INSN_DELETED_LABEL note. */
146 7911604 : if (! can_delete_label_p (label))
147 : {
148 59712 : const char *name = LABEL_NAME (label);
149 59712 : basic_block bb = BLOCK_FOR_INSN (insn);
150 59712 : rtx_insn *bb_note = NEXT_INSN (insn);
151 :
152 59712 : really_delete = false;
153 59712 : PUT_CODE (insn, NOTE);
154 59712 : NOTE_KIND (insn) = NOTE_INSN_DELETED_LABEL;
155 59712 : NOTE_DELETED_LABEL_NAME (insn) = name;
156 :
157 : /* If the note following the label starts a basic block, and the
158 : label is a member of the same basic block, interchange the two. */
159 59712 : if (bb_note != NULL_RTX
160 7926279 : && NOTE_INSN_BASIC_BLOCK_P (bb_note)
161 14675 : && bb != NULL
162 74387 : && bb == BLOCK_FOR_INSN (bb_note))
163 : {
164 11789 : reorder_insns_nobb (insn, insn, bb_note);
165 11789 : BB_HEAD (bb) = bb_note;
166 11789 : if (BB_END (bb) == bb_note)
167 1623 : BB_END (bb) = insn;
168 : }
169 : }
170 :
171 7911604 : remove_node_from_insn_list (insn, &nonlocal_goto_handler_labels);
172 : }
173 :
174 7911604 : if (really_delete)
175 : {
176 : /* If this insn has already been deleted, something is very wrong. */
177 133968681 : gcc_assert (!insn->deleted ());
178 133968681 : if (INSN_P (insn))
179 98972797 : df_insn_delete (insn);
180 133968681 : remove_insn (insn);
181 133968681 : insn->set_deleted ();
182 : }
183 :
184 : /* If deleting a jump, decrement the use count of the label. Deleting
185 : the label itself should happen in the normal course of block merging. */
186 134028393 : if (JUMP_P (insn))
187 : {
188 6980338 : if (JUMP_LABEL (insn)
189 6980110 : && LABEL_P (JUMP_LABEL (insn)))
190 6970322 : LABEL_NUSES (JUMP_LABEL (insn))--;
191 :
192 : /* If there are more targets, remove them too. */
193 : while ((note
194 13960676 : = find_reg_note (insn, REG_LABEL_TARGET, NULL_RTX)) != NULL_RTX
195 6980338 : && LABEL_P (XEXP (note, 0)))
196 : {
197 0 : LABEL_NUSES (XEXP (note, 0))--;
198 0 : remove_note (insn, note);
199 : }
200 : }
201 :
202 : /* Also if deleting any insn that references a label as an operand. */
203 268075184 : while ((note = find_reg_note (insn, REG_LABEL_OPERAND, NULL_RTX)) != NULL_RTX
204 134037592 : && LABEL_P (XEXP (note, 0)))
205 : {
206 9199 : LABEL_NUSES (XEXP (note, 0))--;
207 9199 : remove_note (insn, note);
208 : }
209 :
210 134028393 : if (rtx_jump_table_data *table = dyn_cast <rtx_jump_table_data *> (insn))
211 : {
212 191 : rtvec vec = table->get_labels ();
213 191 : int len = GET_NUM_ELEM (vec);
214 191 : int i;
215 :
216 2287 : for (i = 0; i < len; i++)
217 : {
218 2096 : rtx_insn *label = label_ref_label (RTVEC_ELT (vec, i));
219 :
220 : /* When deleting code in bulk (e.g. removing many unreachable
221 : blocks) we can delete a label that's a target of the vector
222 : before deleting the vector itself. */
223 2096 : if (!NOTE_P (label))
224 2096 : LABEL_NUSES (label)--;
225 : }
226 : }
227 134028393 : }
228 :
229 : /* Like delete_insn but also purge dead edges from BB.
230 : Return true if any edges are eliminated. */
231 :
232 : bool
233 13096024 : delete_insn_and_edges (rtx_insn *insn)
234 : {
235 13096024 : bool purge = false;
236 :
237 13096024 : if (NONDEBUG_INSN_P (insn) && BLOCK_FOR_INSN (insn))
238 : {
239 12640653 : basic_block bb = BLOCK_FOR_INSN (insn);
240 12640653 : if (BB_END (bb) == insn)
241 : purge = true;
242 11981886 : else if (DEBUG_INSN_P (BB_END (bb)))
243 1048398 : for (rtx_insn *dinsn = NEXT_INSN (insn);
244 1048398 : DEBUG_INSN_P (dinsn); dinsn = NEXT_INSN (dinsn))
245 691372 : if (BB_END (bb) == dinsn)
246 : {
247 : purge = true;
248 : break;
249 : }
250 : }
251 13096024 : delete_insn (insn);
252 13096024 : if (purge)
253 705373 : return purge_dead_edges (BLOCK_FOR_INSN (insn));
254 : return false;
255 : }
256 :
257 : /* Return true if INSN is a shared jump table or its adjacent label. */
258 :
259 : static bool
260 32040903 : shared_jump_table_component_p (rtx_insn *insn)
261 : {
262 32040903 : rtx_insn *table = insn;
263 32040903 : if (LABEL_P (insn))
264 5407760 : table = NEXT_INSN (insn);
265 :
266 31485332 : return (table && JUMP_TABLE_DATA_P (table)
267 5407951 : && JUMP_TABLE_SHARED_P (PATTERN (table)));
268 : }
269 :
270 : /* Return true if the half-open range from FIRST to LAST contains part of a
271 : shared jump table. */
272 :
273 : static bool
274 17795271 : shared_jump_table_between_p (rtx_insn *first, rtx_insn *last)
275 : {
276 18724113 : for (rtx_insn *insn = first; insn != last; insn = NEXT_INSN (insn))
277 928842 : if (shared_jump_table_component_p (insn))
278 : return true;
279 : return false;
280 : }
281 :
282 : /* Return the shared jump table that BB hosts: BB ends in a jump through the
283 : table and the table's label follows BB_END, possibly after barriers.
284 : Return NULL otherwise. */
285 :
286 : static rtx_jump_table_data *
287 7018011 : hosted_shared_table (basic_block bb)
288 : {
289 7018011 : rtx_insn *label;
290 7018011 : rtx_jump_table_data *table;
291 7018011 : if (!shared_tablejump_p (BB_END (bb), &label, &table))
292 : return NULL;
293 :
294 337 : rtx_insn *insn = next_nonnote_nondebug_insn_bb (BB_END (bb));
295 1066 : while (insn && BARRIER_P (insn))
296 392 : insn = next_nonnote_nondebug_insn_bb (insn);
297 337 : return insn == label ? table : NULL;
298 : }
299 :
300 : /* Return a block other than BB that ends in a jump through the table whose
301 : label is LABEL, or NULL if there is none. */
302 :
303 : static basic_block
304 85 : other_table_user (basic_block bb, rtx_insn *label)
305 : {
306 85 : basic_block other;
307 660 : FOR_EACH_BB_FN (other, cfun)
308 660 : if (other != bb
309 654 : && JUMP_P (BB_END (other))
310 543 : && JUMP_LABEL (BB_END (other)) == label)
311 : return other;
312 : return NULL;
313 : }
314 :
315 : /* Move the jump table TABLE, with its label, right after BB_END (BB), which
316 : is a jump through it. */
317 :
318 : static void
319 85 : move_table_after (rtx_jump_table_data *table, basic_block bb)
320 : {
321 85 : rtx_insn *label = PREV_INSN (table);
322 85 : gcc_checking_assert (LABEL_P (label) && JUMP_LABEL (BB_END (bb)) == label);
323 :
324 85 : if (current_ir_type () == IR_RTL_CFGLAYOUT)
325 : {
326 : /* The insns that follow BB are in its footer, outside the chain. */
327 0 : unlink_insn_chain (label, table);
328 0 : SET_NEXT_INSN (table) = BB_FOOTER (bb);
329 0 : if (BB_FOOTER (bb))
330 0 : SET_PREV_INSN (BB_FOOTER (bb)) = table;
331 0 : BB_FOOTER (bb) = label;
332 : }
333 : else
334 85 : reorder_insns_nobb (label, table, BB_END (bb));
335 85 : }
336 :
337 : /* Unlink a chain of insns between START and FINISH, leaving notes
338 : that must be paired. If CLEAR_BB is true, we set bb field for
339 : insns that cannot be removed to NULL. */
340 :
341 : void
342 18099207 : delete_insn_chain (rtx_insn *start, rtx_insn *finish, bool clear_bb)
343 : {
344 : /* Unchain the insns one by one. It would be quicker to delete all of these
345 : with a single unchaining, rather than one at a time, but we need to keep
346 : the NOTE's. */
347 18099207 : rtx_insn *current = finish;
348 31079847 : while (1)
349 : {
350 31079847 : rtx_insn *prev = PREV_INSN (current);
351 31079847 : gcc_checking_assert (!shared_jump_table_component_p (current));
352 31079847 : if (NOTE_P (current) && !can_delete_note_p (as_a <rtx_note *> (current)))
353 : ;
354 : else
355 31073887 : delete_insn (current);
356 :
357 31079847 : if (clear_bb && !current->deleted ())
358 52491 : set_block_for_insn (current, NULL);
359 :
360 31079847 : if (current == start)
361 : break;
362 : current = prev;
363 : }
364 18099207 : }
365 :
366 : /* Create a new basic block consisting of the instructions between HEAD and END
367 : inclusive. This function is designed to allow fast BB construction - reuses
368 : the note and basic block struct in BB_NOTE, if any and do not grow
369 : BASIC_BLOCK chain and should be used directly only by CFG construction code.
370 : END can be NULL in to create new empty basic block before HEAD. Both END
371 : and HEAD can be NULL to create basic block at the end of INSN chain.
372 : AFTER is the basic block we should be put after. */
373 :
374 : basic_block
375 13417669 : create_basic_block_structure (rtx_insn *head, rtx_insn *end, rtx_note *bb_note,
376 : basic_block after)
377 : {
378 13417669 : basic_block bb;
379 :
380 13417669 : if (bb_note
381 0 : && (bb = NOTE_BASIC_BLOCK (bb_note)) != NULL
382 13417669 : && bb->aux == NULL)
383 : {
384 : /* If we found an existing note, thread it back onto the chain. */
385 :
386 0 : rtx_insn *after;
387 :
388 0 : if (LABEL_P (head))
389 : after = head;
390 : else
391 : {
392 0 : after = PREV_INSN (head);
393 0 : head = bb_note;
394 : }
395 :
396 0 : if (after != bb_note && NEXT_INSN (after) != bb_note)
397 0 : reorder_insns_nobb (bb_note, bb_note, after);
398 : }
399 : else
400 : {
401 : /* Otherwise we must create a note and a basic block structure. */
402 :
403 13417669 : bb = alloc_block ();
404 :
405 13417669 : init_rtl_bb_info (bb);
406 13417669 : if (!head && !end)
407 484025 : head = end = bb_note
408 484025 : = emit_note_after (NOTE_INSN_BASIC_BLOCK, get_last_insn ());
409 12933644 : else if (LABEL_P (head) && end)
410 : {
411 2792162 : bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, head);
412 2792162 : if (head == end)
413 961181 : end = bb_note;
414 : }
415 : else
416 : {
417 10141482 : bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, head);
418 10141482 : head = bb_note;
419 10141482 : if (!end)
420 5646076 : end = head;
421 : }
422 :
423 13417669 : NOTE_BASIC_BLOCK (bb_note) = bb;
424 : }
425 :
426 : /* Always include the bb note in the block. */
427 13417669 : if (NEXT_INSN (end) == bb_note)
428 0 : end = bb_note;
429 :
430 13417669 : BB_HEAD (bb) = head;
431 13417669 : BB_END (bb) = end;
432 13417669 : bb->index = last_basic_block_for_fn (cfun)++;
433 13417669 : bb->flags = BB_NEW | BB_RTL;
434 13417669 : link_block (bb, after);
435 13417669 : SET_BASIC_BLOCK_FOR_FN (cfun, bb->index, bb);
436 13417669 : df_bb_refs_record (bb->index, false);
437 13417669 : update_bb_for_insn (bb);
438 13417669 : BB_SET_PARTITION (bb, BB_UNPARTITIONED);
439 :
440 : /* Tag the block so that we know it has been used when considering
441 : other basic block notes. */
442 13417669 : bb->aux = bb;
443 :
444 13417669 : return bb;
445 : }
446 :
447 : /* Create new basic block consisting of instructions in between HEAD and END
448 : and place it to the BB chain after block AFTER. END can be NULL to
449 : create a new empty basic block before HEAD. Both END and HEAD can be
450 : NULL to create basic block at the end of INSN chain. */
451 :
452 : static basic_block
453 13417669 : rtl_create_basic_block (void *headp, void *endp, basic_block after)
454 : {
455 13417669 : rtx_insn *head = (rtx_insn *) headp;
456 13417669 : rtx_insn *end = (rtx_insn *) endp;
457 13417669 : basic_block bb;
458 :
459 : /* Grow the basic block array if needed. */
460 13417669 : if ((size_t) last_basic_block_for_fn (cfun)
461 13417669 : >= basic_block_info_for_fn (cfun)->length ())
462 909261 : vec_safe_grow_cleared (basic_block_info_for_fn (cfun),
463 909261 : last_basic_block_for_fn (cfun) + 1);
464 :
465 13417669 : n_basic_blocks_for_fn (cfun)++;
466 :
467 13417669 : bb = create_basic_block_structure (head, end, NULL, after);
468 13417669 : bb->aux = NULL;
469 13417669 : return bb;
470 : }
471 :
472 : static basic_block
473 4391069 : cfg_layout_create_basic_block (void *head, void *end, basic_block after)
474 : {
475 4391069 : basic_block newbb = rtl_create_basic_block (head, end, after);
476 :
477 4391069 : return newbb;
478 : }
479 :
480 : /* Delete the insns in a (non-live) block. We physically delete every
481 : non-deleted-note insn, and update the flow graph appropriately.
482 :
483 : Return nonzero if we deleted an exception handler. */
484 :
485 : /* ??? Preserving all such notes strikes me as wrong. It would be nice
486 : to post-process the stream to remove empty blocks, loops, ranges, etc. */
487 :
488 : static void
489 7017800 : rtl_delete_block (basic_block b)
490 : {
491 7017800 : rtx_insn *insn, *end;
492 :
493 : /* If the head of this block is a CODE_LABEL, then it might be the
494 : label for an exception handler which can't be reached. We need
495 : to remove the label from the exception_handler_label list. */
496 7017800 : insn = BB_HEAD (b);
497 :
498 7017800 : end = get_last_bb_insn (b);
499 :
500 : /* A shared table that B hosts has to go on following one of the jumps
501 : through it. If B has the last such jump, leave the dead table to
502 : delete_dead_jumptables. */
503 7017800 : if (rtx_jump_table_data *table = hosted_shared_table (b))
504 : {
505 85 : if (basic_block user = other_table_user (b, PREV_INSN (table)))
506 : {
507 85 : move_table_after (table, user);
508 85 : end = get_last_bb_insn (b);
509 : }
510 : else
511 0 : end = PREV_INSN (PREV_INSN (table));
512 : }
513 :
514 : /* Selectively delete the entire chain. */
515 7017800 : BB_HEAD (b) = NULL;
516 7017800 : delete_insn_chain (insn, end, true);
517 :
518 :
519 7017800 : if (dump_file)
520 770 : fprintf (dump_file, "deleting block %d\n", b->index);
521 7017800 : df_bb_delete (b->index);
522 7017800 : }
523 :
524 : /* Records the basic block struct in BLOCK_FOR_INSN for every insn. */
525 :
526 : void
527 1531182 : compute_bb_for_insn (void)
528 : {
529 1531182 : basic_block bb;
530 :
531 16177819 : FOR_EACH_BB_FN (bb, cfun)
532 : {
533 14646637 : rtx_insn *end = BB_END (bb);
534 14646637 : rtx_insn *insn;
535 :
536 181435058 : for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
537 : {
538 181435058 : BLOCK_FOR_INSN (insn) = bb;
539 181435058 : if (insn == end)
540 : break;
541 : }
542 : }
543 1531182 : }
544 :
545 : /* Release the basic_block_for_insn array. */
546 :
547 : void
548 3063078 : free_bb_for_insn (void)
549 : {
550 3063078 : rtx_insn *insn;
551 218507732 : for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
552 215444654 : if (!BARRIER_P (insn))
553 210535038 : BLOCK_FOR_INSN (insn) = NULL;
554 3063078 : }
555 :
556 : namespace {
557 :
558 : const pass_data pass_data_free_cfg =
559 : {
560 : RTL_PASS, /* type */
561 : "*free_cfg", /* name */
562 : OPTGROUP_NONE, /* optinfo_flags */
563 : TV_NONE, /* tv_id */
564 : 0, /* properties_required */
565 : 0, /* properties_provided */
566 : PROP_cfg, /* properties_destroyed */
567 : 0, /* todo_flags_start */
568 : 0, /* todo_flags_finish */
569 : };
570 :
571 : class pass_free_cfg : public rtl_opt_pass
572 : {
573 : public:
574 302985 : pass_free_cfg (gcc::context *ctxt)
575 605970 : : rtl_opt_pass (pass_data_free_cfg, ctxt)
576 : {}
577 :
578 : /* opt_pass methods: */
579 : unsigned int execute (function *) final override;
580 :
581 : }; // class pass_free_cfg
582 :
583 : unsigned int
584 1531124 : pass_free_cfg::execute (function *)
585 : {
586 : /* The resource.cc machinery uses DF but the CFG isn't guaranteed to be
587 : valid at that point so it would be too late to call df_analyze. */
588 1531124 : if (DELAY_SLOTS && optimize > 0 && flag_delayed_branch)
589 : {
590 : df_note_add_problem ();
591 : df_analyze ();
592 : }
593 :
594 1531124 : if (crtl->has_bb_partition)
595 65691 : insert_section_boundary_note ();
596 :
597 1531124 : free_bb_for_insn ();
598 1531124 : return 0;
599 : }
600 :
601 : } // anon namespace
602 :
603 : rtl_opt_pass *
604 302985 : make_pass_free_cfg (gcc::context *ctxt)
605 : {
606 302985 : return new pass_free_cfg (ctxt);
607 : }
608 :
609 : /* Return RTX to emit after when we want to emit code on the entry of function. */
610 : rtx_insn *
611 7915 : entry_of_function (void)
612 : {
613 7915 : return (n_basic_blocks_for_fn (cfun) > NUM_FIXED_BLOCKS ?
614 7915 : BB_HEAD (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) : get_insns ());
615 : }
616 :
617 : /* Emit INSN at the entry point of the function, ensuring that it is only
618 : executed once per function. */
619 : void
620 0 : emit_insn_at_entry (rtx insn)
621 : {
622 0 : edge_iterator ei = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
623 0 : edge e = ei_safe_edge (ei);
624 0 : gcc_assert (e->flags & EDGE_FALLTHRU);
625 :
626 0 : insert_insn_on_edge (insn, e);
627 0 : commit_edge_insertions ();
628 0 : }
629 :
630 : /* Update BLOCK_FOR_INSN of insns between BEGIN and END
631 : (or BARRIER if found) and notify df of the bb change.
632 : The insn chain range is inclusive
633 : (i.e. both BEGIN and END will be updated. */
634 :
635 : void
636 34188011 : update_bb_for_insn_chain (rtx_insn *begin, rtx_insn *end, basic_block bb)
637 : {
638 34188011 : rtx_insn *insn;
639 :
640 34188011 : end = NEXT_INSN (end);
641 363682388 : for (insn = begin; insn != end; insn = NEXT_INSN (insn))
642 295306366 : if (!BARRIER_P (insn))
643 292742502 : df_insn_change_bb (insn, bb);
644 34188011 : }
645 :
646 : /* Update BLOCK_FOR_INSN of insns in BB to BB,
647 : and notify df of the change. */
648 :
649 : void
650 17103361 : update_bb_for_insn (basic_block bb)
651 : {
652 17103361 : update_bb_for_insn_chain (BB_HEAD (bb), BB_END (bb), bb);
653 17103361 : }
654 :
655 :
656 : /* Return true if adding or removing instances of INSN might affect the
657 : semantics of the RTL. */
658 :
659 : static bool
660 342839105 : flow_active_insn_p (const rtx_insn *insn)
661 : {
662 342839105 : if (active_insn_p (insn))
663 : return true;
664 :
665 : /* We cannot add new clobbers to a path without first proving that
666 : the clobbered thing is dead.
667 :
668 : For example:
669 :
670 : (code_label L1)
671 : (clobber X)
672 : (set (pc) (label_ref L2))
673 :
674 : is a forwarder block in the sense that a jump to L1 can be replaced
675 : with a jump to L2, although perhaps with some loss of dataflow precision.
676 : However, any attempt to merge a jump to L1 with a jump to L2 would be an
677 : asymmetric operation, in that the merged code must jump to L2 rather than
678 : to L1. Our current definition of "forwarder block" does not allow for
679 : this distinction and so we need to take a conservatively correct
680 : approach. */
681 213943173 : if (GET_CODE (PATTERN (insn)) == CLOBBER)
682 : return true;
683 :
684 : /* Keep a USE of the function return value, otherwise the USE is dropped and
685 : we could fail to thread a jump if USE appears on some paths and not on
686 : others, see PR90257. */
687 213878139 : if (GET_CODE (PATTERN (insn)) == USE
688 590318 : && REG_P (XEXP (PATTERN (insn), 0))
689 214468457 : && REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))
690 584744 : return true;
691 :
692 : return false;
693 : }
694 :
695 : /* Return true if the block has no effect and only forwards control flow to
696 : its single destination. */
697 :
698 : bool
699 321243497 : contains_no_active_insn_p (const_basic_block bb)
700 : {
701 321243497 : rtx_insn *insn;
702 :
703 321243497 : if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
704 321243497 : || bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)
705 319940960 : || !single_succ_p (bb)
706 469991010 : || (single_succ_edge (bb)->flags & EDGE_FAKE) != 0)
707 : return false;
708 :
709 580312247 : for (insn = BB_HEAD (bb); insn != BB_END (bb); insn = NEXT_INSN (insn))
710 533401596 : if (INSN_P (insn) && flow_active_insn_p (insn))
711 : return false;
712 :
713 46910651 : return (!INSN_P (insn)
714 36006618 : || (JUMP_P (insn) && simplejump_p (insn))
715 75628476 : || !flow_active_insn_p (insn));
716 : }
717 :
718 : /* Likewise, but protect loop latches, headers and preheaders. */
719 : /* FIXME: Make this a cfg hook. */
720 :
721 : bool
722 321243497 : forwarder_block_p (const_basic_block bb)
723 : {
724 321243497 : if (!contains_no_active_insn_p (bb))
725 : return false;
726 :
727 : /* Protect loop latches, headers and preheaders. */
728 18531737 : if (current_loops)
729 : {
730 9327950 : basic_block dest;
731 9327950 : if (bb->loop_father->header == bb)
732 : return false;
733 9227329 : dest = EDGE_SUCC (bb, 0)->dest;
734 9227329 : if (dest->loop_father->header == dest)
735 1161018 : return false;
736 : }
737 :
738 : return true;
739 : }
740 :
741 : /* Return nonzero if we can reach target from src by falling through. */
742 : /* FIXME: Make this a cfg hook, the result is only valid in cfgrtl mode. */
743 :
744 : bool
745 45271126 : can_fallthru (basic_block src, basic_block target)
746 : {
747 45271126 : rtx_insn *insn = BB_END (src);
748 45271126 : rtx_insn *insn2;
749 45271126 : edge e;
750 45271126 : edge_iterator ei;
751 :
752 45271126 : if (target == EXIT_BLOCK_PTR_FOR_FN (cfun))
753 : return true;
754 43845611 : if (src->next_bb != target)
755 : return false;
756 :
757 17795205 : if (shared_jump_table_between_p (NEXT_INSN (insn), BB_HEAD (target)))
758 : return false;
759 :
760 : /* ??? Later we may add code to move jump tables offline. */
761 17795205 : if (tablejump_p (insn, NULL, NULL))
762 : return false;
763 :
764 48365650 : FOR_EACH_EDGE (e, ei, src->succs)
765 30570445 : if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
766 0 : && e->flags & EDGE_FALLTHRU)
767 : return false;
768 :
769 17795205 : insn2 = BB_HEAD (target);
770 17795205 : if (!active_insn_p (insn2))
771 17795205 : insn2 = next_active_insn (insn2);
772 :
773 17795205 : return next_active_insn (insn) == insn2;
774 : }
775 :
776 : /* Return nonzero if we could reach target from src by falling through,
777 : if the target was made adjacent. If we already have a fall-through
778 : edge to the exit block, we can't do that. */
779 : static bool
780 2294166 : could_fall_through (basic_block src, basic_block target)
781 : {
782 2294166 : edge e;
783 2294166 : edge_iterator ei;
784 :
785 2294166 : if (target == EXIT_BLOCK_PTR_FOR_FN (cfun))
786 : return true;
787 6882498 : FOR_EACH_EDGE (e, ei, src->succs)
788 4588332 : if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
789 0 : && e->flags & EDGE_FALLTHRU)
790 : return 0;
791 : return true;
792 : }
793 :
794 : /* Return the NOTE_INSN_BASIC_BLOCK of BB. */
795 : rtx_note *
796 76403768 : bb_note (basic_block bb)
797 : {
798 76403768 : rtx_insn *note;
799 :
800 76403768 : note = BB_HEAD (bb);
801 76403768 : if (LABEL_P (note))
802 36652451 : note = NEXT_INSN (note);
803 :
804 76403768 : gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
805 76403768 : return as_a <rtx_note *> (note);
806 : }
807 :
808 : /* Return the INSN immediately following the NOTE_INSN_BASIC_BLOCK
809 : note associated with the BLOCK. */
810 :
811 : static rtx_insn *
812 7983 : first_insn_after_basic_block_note (basic_block block)
813 : {
814 7983 : rtx_insn *insn;
815 :
816 : /* Get the first instruction in the block. */
817 7983 : insn = BB_HEAD (block);
818 :
819 7983 : if (insn == NULL_RTX)
820 : return NULL;
821 7983 : if (LABEL_P (insn))
822 7983 : insn = NEXT_INSN (insn);
823 7983 : gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
824 :
825 7983 : return NEXT_INSN (insn);
826 : }
827 :
828 : /* Creates a new basic block just after basic block BB by splitting
829 : everything after specified instruction INSNP. */
830 :
831 : static basic_block
832 3042909 : rtl_split_block (basic_block bb, void *insnp)
833 : {
834 3042909 : basic_block new_bb;
835 3042909 : rtx_insn *insn = (rtx_insn *) insnp;
836 3042909 : edge e;
837 3042909 : edge_iterator ei;
838 :
839 3042909 : if (!insn)
840 : {
841 7983 : insn = first_insn_after_basic_block_note (bb);
842 :
843 7983 : if (insn)
844 : {
845 7963 : rtx_insn *next = insn;
846 :
847 7963 : insn = PREV_INSN (insn);
848 :
849 : /* If the block contains only debug insns, insn would have
850 : been NULL in a non-debug compilation, and then we'd end
851 : up emitting a DELETED note. For -fcompare-debug
852 : stability, emit the note too. */
853 7963 : if (insn != BB_END (bb)
854 7701 : && DEBUG_INSN_P (next)
855 605 : && DEBUG_INSN_P (BB_END (bb)))
856 : {
857 276 : while (next != BB_END (bb) && DEBUG_INSN_P (next))
858 185 : next = NEXT_INSN (next);
859 :
860 91 : if (next == BB_END (bb))
861 90 : emit_note_after (NOTE_INSN_DELETED, next);
862 : }
863 : }
864 : else
865 20 : insn = get_last_insn ();
866 : }
867 :
868 : /* We probably should check type of the insn so that we do not create
869 : inconsistent cfg. It is checked in verify_flow_info anyway, so do not
870 : bother. */
871 3042909 : if (insn == BB_END (bb))
872 282 : emit_note_after (NOTE_INSN_DELETED, insn);
873 :
874 : /* Create the new basic block. */
875 3042909 : new_bb = create_basic_block (NEXT_INSN (insn), BB_END (bb), bb);
876 3042909 : BB_COPY_PARTITION (new_bb, bb);
877 3042909 : BB_END (bb) = insn;
878 :
879 : /* Redirect the outgoing edges. */
880 3042909 : new_bb->succs = bb->succs;
881 3042909 : bb->succs = NULL;
882 8394038 : FOR_EACH_EDGE (e, ei, new_bb->succs)
883 5351129 : e->src = new_bb;
884 :
885 : /* The new block starts off being dirty. */
886 3042909 : df_set_bb_dirty (bb);
887 3042909 : return new_bb;
888 : }
889 :
890 : /* Return true if LOC1 and LOC2 are equivalent for
891 : unique_locus_on_edge_between_p purposes. */
892 :
893 : static bool
894 1492928 : loc_equal (location_t loc1, location_t loc2)
895 : {
896 1492928 : if (loc1 == loc2)
897 : return true;
898 :
899 704900 : expanded_location loce1 = expand_location (loc1);
900 704900 : expanded_location loce2 = expand_location (loc2);
901 :
902 704900 : if (loce1.line != loce2.line
903 458114 : || loce1.column != loce2.column
904 319721 : || loce1.data != loce2.data)
905 : return false;
906 278711 : if (loce1.file == loce2.file)
907 : return true;
908 0 : return (loce1.file != NULL
909 0 : && loce2.file != NULL
910 0 : && filename_cmp (loce1.file, loce2.file) == 0);
911 : }
912 :
913 : /* Return true if the single edge between blocks A and B is the only place
914 : in RTL which holds some unique locus. */
915 :
916 : static bool
917 1195665 : unique_locus_on_edge_between_p (basic_block a, basic_block b)
918 : {
919 1195665 : const location_t goto_locus = EDGE_SUCC (a, 0)->goto_locus;
920 1195665 : rtx_insn *insn, *end;
921 :
922 1195665 : if (LOCATION_LOCUS (goto_locus) == UNKNOWN_LOCATION)
923 : return false;
924 :
925 : /* First scan block A backward. */
926 543351 : insn = BB_END (a);
927 543351 : end = PREV_INSN (BB_HEAD (a));
928 1290206 : while (insn != end && (!NONDEBUG_INSN_P (insn) || !insn->has_location ()))
929 203504 : insn = PREV_INSN (insn);
930 :
931 543351 : if (insn != end && loc_equal (insn->location (), goto_locus))
932 : return false;
933 :
934 : /* Then scan block B forward. */
935 202621 : insn = BB_HEAD (b);
936 202621 : if (insn)
937 : {
938 192766 : end = NEXT_INSN (BB_END (b));
939 386956 : while (insn != end && !NONDEBUG_INSN_P (insn))
940 1424 : insn = NEXT_INSN (insn);
941 :
942 192507 : if (insn != end && insn->has_location ()
943 384204 : && loc_equal (insn->location (), goto_locus))
944 179607 : return false;
945 : }
946 :
947 : return true;
948 : }
949 :
950 : /* If the single edge between blocks A and B is the only place in RTL which
951 : holds some unique locus, emit a nop with that locus between the blocks. */
952 :
953 : static void
954 1195665 : emit_nop_for_unique_locus_between (basic_block a, basic_block b)
955 : {
956 1195665 : if (!unique_locus_on_edge_between_p (a, b))
957 : return;
958 :
959 23014 : BB_END (a) = emit_insn_after_noloc (gen_nop (), BB_END (a), a);
960 23014 : BB_END (a)->set_location (EDGE_SUCC (a, 0)->goto_locus);
961 : }
962 :
963 : /* Blocks A and B are to be merged into a single block A. The insns
964 : are already contiguous. */
965 :
966 : static void
967 3900180 : rtl_merge_blocks (basic_block a, basic_block b)
968 : {
969 : /* If B is a forwarder block whose outgoing edge has no location, we'll
970 : propagate the locus of the edge between A and B onto it. */
971 3900180 : const bool forward_edge_locus
972 3900180 : = (b->flags & BB_FORWARDER_BLOCK) != 0
973 3900180 : && LOCATION_LOCUS (EDGE_SUCC (b, 0)->goto_locus) == UNKNOWN_LOCATION;
974 3900180 : rtx_insn *b_head = BB_HEAD (b), *b_end = BB_END (b), *a_end = BB_END (a);
975 3900180 : rtx_insn *del_first = NULL, *del_last = NULL;
976 3900180 : rtx_insn *b_debug_start = b_end, *b_debug_end = b_end;
977 3900180 : bool b_empty = false;
978 :
979 3900180 : if (dump_file)
980 1005 : fprintf (dump_file, "Merging block %d into block %d...\n", b->index,
981 : a->index);
982 :
983 4366633 : while (DEBUG_INSN_P (b_end))
984 466453 : b_end = PREV_INSN (b_debug_start = b_end);
985 :
986 : /* If there was a CODE_LABEL beginning B, delete it. */
987 3900180 : if (LABEL_P (b_head))
988 : {
989 : /* Detect basic blocks with nothing but a label. This can happen
990 : in particular at the end of a function. */
991 1804532 : if (b_head == b_end)
992 0 : b_empty = true;
993 :
994 1804532 : del_first = del_last = b_head;
995 1804532 : b_head = NEXT_INSN (b_head);
996 : }
997 :
998 : /* Delete the basic block note and handle blocks containing just that
999 : note. */
1000 3900180 : if (NOTE_INSN_BASIC_BLOCK_P (b_head))
1001 : {
1002 3900180 : if (b_head == b_end)
1003 737032 : b_empty = true;
1004 3900180 : if (! del_last)
1005 2095648 : del_first = b_head;
1006 :
1007 3900180 : del_last = b_head;
1008 3900180 : b_head = NEXT_INSN (b_head);
1009 : }
1010 :
1011 : /* If there was a jump out of A, delete it. */
1012 3900180 : if (JUMP_P (a_end))
1013 : {
1014 53719 : rtx_insn *prev;
1015 :
1016 54804 : for (prev = PREV_INSN (a_end); ; prev = PREV_INSN (prev))
1017 54804 : if (!NOTE_P (prev)
1018 3901265 : || NOTE_INSN_BASIC_BLOCK_P (prev)
1019 55889 : || prev == BB_HEAD (a))
1020 : break;
1021 :
1022 : del_first = a_end;
1023 :
1024 : a_end = PREV_INSN (del_first);
1025 : }
1026 3846461 : else if (BARRIER_P (NEXT_INSN (a_end)))
1027 0 : del_first = NEXT_INSN (a_end);
1028 :
1029 : /* Delete everything marked above as well as crap that might be
1030 : hanging out between the two blocks. */
1031 3900180 : BB_END (a) = a_end;
1032 3900180 : BB_HEAD (b) = b_empty ? NULL : b_head;
1033 3900180 : delete_insn_chain (del_first, del_last, true);
1034 :
1035 : /* If not optimizing, preserve the locus of the single edge between
1036 : blocks A and B if necessary by emitting a nop. */
1037 3900180 : if (!optimize
1038 1468972 : && !forward_edge_locus
1039 5096827 : && !DECL_IGNORED_P (current_function_decl))
1040 : {
1041 1193355 : emit_nop_for_unique_locus_between (a, b);
1042 1193355 : a_end = BB_END (a);
1043 : }
1044 :
1045 : /* Reassociate the insns of B with A. */
1046 3900180 : if (!b_empty)
1047 : {
1048 3163148 : update_bb_for_insn_chain (a_end, b_debug_end, a);
1049 :
1050 3163148 : BB_END (a) = b_debug_end;
1051 3163148 : BB_HEAD (b) = NULL;
1052 : }
1053 737032 : else if (b_end != b_debug_end)
1054 : {
1055 : /* Move any deleted labels and other notes between the end of A
1056 : and the debug insns that make up B after the debug insns,
1057 : bringing the debug insns into A while keeping the notes after
1058 : the end of A. */
1059 21889 : if (NEXT_INSN (a_end) != b_debug_start)
1060 149 : reorder_insns_nobb (NEXT_INSN (a_end), PREV_INSN (b_debug_start),
1061 : b_debug_end);
1062 21889 : update_bb_for_insn_chain (b_debug_start, b_debug_end, a);
1063 21889 : BB_END (a) = b_debug_end;
1064 : }
1065 :
1066 3900180 : df_bb_delete (b->index);
1067 :
1068 3900180 : if (forward_edge_locus)
1069 697542 : EDGE_SUCC (b, 0)->goto_locus = EDGE_SUCC (a, 0)->goto_locus;
1070 :
1071 3900180 : if (dump_file)
1072 1005 : fprintf (dump_file, "Merged blocks %d and %d.\n", a->index, b->index);
1073 3900180 : }
1074 :
1075 :
1076 : /* Return true when block A and B can be merged. */
1077 :
1078 : static bool
1079 66 : rtl_can_merge_blocks (basic_block a, basic_block b)
1080 : {
1081 : /* If we are partitioning hot/cold basic blocks, we don't want to
1082 : mess up unconditional or indirect jumps that cross between hot
1083 : and cold sections.
1084 :
1085 : Basic block partitioning may result in some jumps that appear to
1086 : be optimizable (or blocks that appear to be mergeable), but which really
1087 : must be left untouched (they are required to make it safely across
1088 : partition boundaries). See the comments at the top of
1089 : bb-reorder.cc:partition_hot_cold_basic_blocks for complete details. */
1090 :
1091 66 : if (BB_PARTITION (a) != BB_PARTITION (b))
1092 : return false;
1093 :
1094 : /* Protect the loop latches. */
1095 66 : if (current_loops && b->loop_father->latch == b)
1096 : return false;
1097 :
1098 : /* A shared table between the blocks is live data, not disposable material
1099 : that can become part of the merged block. */
1100 66 : if (a->next_bb == b
1101 66 : && a != ENTRY_BLOCK_PTR_FOR_FN (cfun)
1102 66 : && b != EXIT_BLOCK_PTR_FOR_FN (cfun)
1103 132 : && shared_jump_table_between_p (NEXT_INSN (BB_END (a)), BB_HEAD (b)))
1104 : return false;
1105 :
1106 : /* There must be exactly one edge in between the blocks. */
1107 66 : return (single_succ_p (a)
1108 64 : && single_succ (a) == b
1109 53 : && single_pred_p (b)
1110 53 : && a != b
1111 : /* Must be simple edge. */
1112 53 : && !(single_succ_edge (a)->flags & EDGE_COMPLEX)
1113 53 : && a->next_bb == b
1114 53 : && a != ENTRY_BLOCK_PTR_FOR_FN (cfun)
1115 53 : && b != EXIT_BLOCK_PTR_FOR_FN (cfun)
1116 : /* If the jump insn has side effects,
1117 : we can't kill the edge. */
1118 119 : && (!JUMP_P (BB_END (a))
1119 0 : || (reload_completed
1120 0 : ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
1121 : }
1122 :
1123 : /* Return the label in the head of basic block BLOCK. Create one if it doesn't
1124 : exist. */
1125 :
1126 : rtx_code_label *
1127 15741671 : block_label (basic_block block)
1128 : {
1129 15741671 : if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
1130 : return NULL;
1131 :
1132 15687150 : if (!LABEL_P (BB_HEAD (block)))
1133 : {
1134 5800567 : BB_HEAD (block) = emit_label_before (gen_label_rtx (), BB_HEAD (block));
1135 : }
1136 :
1137 15687150 : return as_a <rtx_code_label *> (BB_HEAD (block));
1138 : }
1139 :
1140 : /* Remove all barriers from BB_FOOTER of a BB. */
1141 :
1142 : static void
1143 3906623 : remove_barriers_from_footer (basic_block bb)
1144 : {
1145 3906623 : rtx_insn *insn = BB_FOOTER (bb);
1146 :
1147 : /* Remove barriers but keep jumptables. */
1148 7533118 : while (insn)
1149 : {
1150 3626495 : if (BARRIER_P (insn))
1151 : {
1152 3626473 : if (PREV_INSN (insn))
1153 11 : SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
1154 : else
1155 3626462 : BB_FOOTER (bb) = NEXT_INSN (insn);
1156 3626473 : if (NEXT_INSN (insn))
1157 62006 : SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
1158 : }
1159 3626495 : if (LABEL_P (insn))
1160 : return;
1161 3626495 : insn = NEXT_INSN (insn);
1162 : }
1163 : }
1164 :
1165 : /* Attempt to perform edge redirection by replacing possibly complex jump
1166 : instruction by unconditional jump or removing jump completely. This can
1167 : apply only if all edges now point to the same block. The parameters and
1168 : return values are equivalent to redirect_edge_and_branch. */
1169 :
1170 : edge
1171 49135993 : try_redirect_by_replacing_jump (edge e, basic_block target, bool in_cfglayout)
1172 : {
1173 49135993 : basic_block src = e->src;
1174 49135993 : rtx_insn *insn = BB_END (src);
1175 49135993 : rtx set;
1176 49135993 : bool fallthru = false;
1177 :
1178 : /* If we are partitioning hot/cold basic blocks, we don't want to
1179 : mess up unconditional or indirect jumps that cross between hot
1180 : and cold sections.
1181 :
1182 : Basic block partitioning may result in some jumps that appear to
1183 : be optimizable (or blocks that appear to be mergeable), but which really
1184 : must be left untouched (they are required to make it safely across
1185 : partition boundaries). See the comments at the top of
1186 : bb-reorder.cc:partition_hot_cold_basic_blocks for complete details. */
1187 :
1188 49135993 : if (BB_PARTITION (src) != BB_PARTITION (target))
1189 : return NULL;
1190 :
1191 : /* We can replace or remove a complex jump only when we have exactly
1192 : two edges. Also, if we have exactly one outgoing edge, we can
1193 : redirect that. */
1194 49080083 : if (EDGE_COUNT (src->succs) >= 3
1195 : /* Verify that all targets will be TARGET. Specifically, the
1196 : edge that is not E must also go to TARGET. */
1197 49080083 : || (EDGE_COUNT (src->succs) == 2
1198 18325948 : && EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target))
1199 : return NULL;
1200 :
1201 30889206 : if (!onlyjump_p (insn))
1202 : return NULL;
1203 :
1204 29356970 : rtx_jump_table_data *insn_table;
1205 29356970 : if (tablejump_p (insn, NULL, &insn_table)
1206 29356970 : && (!optimize
1207 110 : || reload_completed
1208 : /* A shared table is still needed by the other jumps that dispatch
1209 : through it, so it cannot be removed along with INSN. */
1210 2 : || JUMP_TABLE_SHARED_P (PATTERN (insn_table))))
1211 : return NULL;
1212 :
1213 : /* Avoid removing branch with side effects. */
1214 29356862 : set = single_set (insn);
1215 29356862 : if (!set || side_effects_p (set))
1216 : return NULL;
1217 :
1218 : /* See if we can create the fallthru edge. */
1219 29356862 : if (in_cfglayout || can_fallthru (src, target))
1220 : {
1221 4789771 : if (dump_file)
1222 810 : fprintf (dump_file, "Removing jump %i.\n", INSN_UID (insn));
1223 4789771 : fallthru = true;
1224 :
1225 : /* Selectively unlink whole insn chain. */
1226 4789771 : if (in_cfglayout)
1227 : {
1228 3906623 : delete_insn_chain (insn, BB_END (src), false);
1229 3906623 : remove_barriers_from_footer (src);
1230 : }
1231 : else
1232 883148 : delete_insn_chain (insn, PREV_INSN (BB_HEAD (target)), false);
1233 : }
1234 :
1235 : /* If this already is simplejump, redirect it. */
1236 24567091 : else if (simplejump_p (insn))
1237 : {
1238 24564742 : if (e->dest == target)
1239 : return NULL;
1240 427531 : if (dump_file)
1241 111 : fprintf (dump_file, "Redirecting jump %i from %i to %i.\n",
1242 111 : INSN_UID (insn), e->dest->index, target->index);
1243 427531 : if (!redirect_jump (as_a <rtx_jump_insn *> (insn),
1244 427531 : block_label (target), 0))
1245 : {
1246 0 : gcc_assert (target == EXIT_BLOCK_PTR_FOR_FN (cfun));
1247 : return NULL;
1248 : }
1249 : }
1250 :
1251 : /* Cannot do anything for target exit block. */
1252 2349 : else if (target == EXIT_BLOCK_PTR_FOR_FN (cfun))
1253 : return NULL;
1254 :
1255 : /* Or replace possibly complicated jump insn by simple jump insn. */
1256 : else
1257 : {
1258 2349 : rtx_code_label *target_label = block_label (target);
1259 2349 : rtx_insn *barrier;
1260 2349 : rtx_insn *label;
1261 2349 : rtx_jump_table_data *table;
1262 :
1263 2349 : emit_jump_insn_after_noloc (targetm.gen_jump (target_label), insn);
1264 2349 : JUMP_LABEL (BB_END (src)) = target_label;
1265 2349 : LABEL_NUSES (target_label)++;
1266 2349 : if (dump_file)
1267 0 : fprintf (dump_file, "Replacing insn %i by jump %i\n",
1268 0 : INSN_UID (insn), INSN_UID (BB_END (src)));
1269 :
1270 :
1271 2349 : delete_insn_chain (insn, insn, false);
1272 :
1273 : /* Recognize a tablejump that we are converting to a
1274 : simple jump and remove its associated CODE_LABEL
1275 : and ADDR_VEC or ADDR_DIFF_VEC. */
1276 2349 : if (tablejump_p (insn, &label, &table))
1277 2 : delete_insn_chain (label, table, false);
1278 :
1279 2349 : barrier = next_nonnote_nondebug_insn (BB_END (src));
1280 2349 : if (!barrier || !BARRIER_P (barrier))
1281 2194 : emit_barrier_after (BB_END (src));
1282 : else
1283 : {
1284 155 : if (barrier != NEXT_INSN (BB_END (src)))
1285 : {
1286 : /* Move the jump before barrier so that the notes
1287 : which originally were or were created before jump table are
1288 : inside the basic block. */
1289 0 : rtx_insn *new_insn = BB_END (src);
1290 :
1291 0 : update_bb_for_insn_chain (NEXT_INSN (BB_END (src)),
1292 : PREV_INSN (barrier), src);
1293 :
1294 0 : SET_NEXT_INSN (PREV_INSN (new_insn)) = NEXT_INSN (new_insn);
1295 0 : SET_PREV_INSN (NEXT_INSN (new_insn)) = PREV_INSN (new_insn);
1296 :
1297 0 : SET_NEXT_INSN (new_insn) = barrier;
1298 0 : SET_NEXT_INSN (PREV_INSN (barrier)) = new_insn;
1299 :
1300 0 : SET_PREV_INSN (new_insn) = PREV_INSN (barrier);
1301 0 : SET_PREV_INSN (barrier) = new_insn;
1302 : }
1303 : }
1304 : }
1305 :
1306 : /* Keep only one edge out and set proper flags. */
1307 5219651 : if (!single_succ_p (src))
1308 155720 : remove_edge (e);
1309 5219651 : gcc_assert (single_succ_p (src));
1310 :
1311 5219651 : e = single_succ_edge (src);
1312 5219651 : if (fallthru)
1313 : e->flags = EDGE_FALLTHRU;
1314 : else
1315 429880 : e->flags = 0;
1316 :
1317 5219651 : e->probability = profile_probability::always ();
1318 :
1319 5219651 : if (e->dest != target)
1320 665473 : redirect_edge_succ (e, target);
1321 : return e;
1322 : }
1323 :
1324 : /* Subroutine of redirect_branch_edge that tries to patch the jump
1325 : instruction INSN so that it reaches block NEW. Do this
1326 : only when it originally reached block OLD. Return true if this
1327 : worked or the original target wasn't OLD, return false if redirection
1328 : doesn't work. */
1329 :
1330 : static bool
1331 7064457 : patch_jump_insn (rtx_insn *insn, rtx_insn *old_label, basic_block new_bb)
1332 : {
1333 7064457 : rtx_jump_table_data *table;
1334 7064457 : rtx tmp;
1335 : /* Recognize a tablejump and adjust all matching cases. */
1336 7064457 : if (tablejump_p (insn, NULL, &table))
1337 : {
1338 16961 : rtvec vec;
1339 16961 : int j;
1340 :
1341 16961 : if (new_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1342 : return false;
1343 :
1344 : /* The table also describes the successors of the other jumps that
1345 : dispatch through it, which this edge says nothing about. */
1346 16961 : if (JUMP_TABLE_SHARED_P (PATTERN (table)))
1347 : return false;
1348 :
1349 16961 : rtx_code_label *new_label = block_label (new_bb);
1350 16961 : vec = table->get_labels ();
1351 :
1352 451361 : for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
1353 434400 : if (XEXP (RTVEC_ELT (vec, j), 0) == old_label)
1354 : {
1355 74931 : RTVEC_ELT (vec, j) = gen_rtx_LABEL_REF (Pmode, new_label);
1356 67215 : --LABEL_NUSES (old_label);
1357 67215 : ++LABEL_NUSES (new_label);
1358 : }
1359 :
1360 : /* Handle casesi dispatch insns. */
1361 16961 : if ((tmp = tablejump_casesi_pattern (insn)) != NULL_RTX
1362 16961 : && label_ref_label (XEXP (SET_SRC (tmp), 2)) == old_label)
1363 : {
1364 0 : XEXP (SET_SRC (tmp), 2) = gen_rtx_LABEL_REF (Pmode,
1365 : new_label);
1366 0 : --LABEL_NUSES (old_label);
1367 0 : ++LABEL_NUSES (new_label);
1368 : }
1369 : }
1370 7047496 : else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL)
1371 : {
1372 448 : int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp);
1373 448 : rtx note;
1374 :
1375 448 : if (new_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1376 : return false;
1377 448 : rtx_code_label *new_label = block_label (new_bb);
1378 :
1379 1639 : for (i = 0; i < n; ++i)
1380 : {
1381 743 : rtx old_ref = ASM_OPERANDS_LABEL (tmp, i);
1382 743 : gcc_assert (GET_CODE (old_ref) == LABEL_REF);
1383 743 : if (XEXP (old_ref, 0) == old_label)
1384 : {
1385 872 : ASM_OPERANDS_LABEL (tmp, i)
1386 436 : = gen_rtx_LABEL_REF (Pmode, new_label);
1387 436 : --LABEL_NUSES (old_label);
1388 436 : ++LABEL_NUSES (new_label);
1389 : }
1390 : }
1391 :
1392 448 : if (JUMP_LABEL (insn) == old_label)
1393 : {
1394 332 : JUMP_LABEL (insn) = new_label;
1395 332 : note = find_reg_note (insn, REG_LABEL_TARGET, new_label);
1396 332 : if (note)
1397 0 : remove_note (insn, note);
1398 : }
1399 : else
1400 : {
1401 116 : note = find_reg_note (insn, REG_LABEL_TARGET, old_label);
1402 116 : if (note)
1403 99 : remove_note (insn, note);
1404 116 : if (JUMP_LABEL (insn) != new_label
1405 116 : && !find_reg_note (insn, REG_LABEL_TARGET, new_label))
1406 109 : add_reg_note (insn, REG_LABEL_TARGET, new_label);
1407 : }
1408 896 : while ((note = find_reg_note (insn, REG_LABEL_OPERAND, old_label))
1409 448 : != NULL_RTX)
1410 0 : XEXP (note, 0) = new_label;
1411 : }
1412 : else
1413 : {
1414 : /* ?? We may play the games with moving the named labels from
1415 : one basic block to the other in case only one computed_jump is
1416 : available. */
1417 7047048 : if (computed_jump_p (insn)
1418 : /* A return instruction can't be redirected. */
1419 7047048 : || returnjump_p (insn))
1420 : return false;
1421 :
1422 7047048 : if (!currently_expanding_to_rtl || JUMP_LABEL (insn) == old_label)
1423 : {
1424 : /* If the insn doesn't go where we think, we're confused. */
1425 7012348 : gcc_assert (JUMP_LABEL (insn) == old_label);
1426 :
1427 : /* If the substitution doesn't succeed, die. This can happen
1428 : if the back end emitted unrecognizable instructions or if
1429 : target is exit block on some arches. Or for crossing
1430 : jumps. */
1431 7012348 : if (!redirect_jump (as_a <rtx_jump_insn *> (insn),
1432 7012348 : block_label (new_bb), 0))
1433 : {
1434 0 : gcc_assert (new_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
1435 : || CROSSING_JUMP_P (insn));
1436 : return false;
1437 : }
1438 : }
1439 : }
1440 : return true;
1441 : }
1442 :
1443 :
1444 : /* Redirect edge representing branch of (un)conditional jump or tablejump,
1445 : NULL on failure */
1446 : static edge
1447 14898969 : redirect_branch_edge (edge e, basic_block target)
1448 : {
1449 14898969 : rtx_insn *old_label = BB_HEAD (e->dest);
1450 14898969 : basic_block src = e->src;
1451 14898969 : rtx_insn *insn = BB_END (src);
1452 :
1453 : /* We can only redirect non-fallthru edges of jump insn. */
1454 14898969 : if (e->flags & EDGE_FALLTHRU)
1455 : return NULL;
1456 7018131 : else if (!JUMP_P (insn) && !currently_expanding_to_rtl)
1457 : return NULL;
1458 :
1459 7018131 : if (!currently_expanding_to_rtl)
1460 : {
1461 6580870 : if (!patch_jump_insn (as_a <rtx_jump_insn *> (insn), old_label, target))
1462 : return NULL;
1463 : }
1464 : else
1465 : /* When expanding this BB might actually contain multiple
1466 : jumps (i.e. not yet split by find_many_sub_basic_blocks).
1467 : Redirect all of those that match our label. */
1468 7191580 : FOR_BB_INSNS (src, insn)
1469 6754319 : if (JUMP_P (insn) && !patch_jump_insn (as_a <rtx_jump_insn *> (insn),
1470 : old_label, target))
1471 : return NULL;
1472 :
1473 7018131 : if (dump_file)
1474 796 : fprintf (dump_file, "Edge %i->%i redirected to %i\n",
1475 796 : e->src->index, e->dest->index, target->index);
1476 :
1477 7018131 : if (e->dest != target)
1478 7018131 : e = redirect_edge_succ_nodup (e, target);
1479 :
1480 : return e;
1481 : }
1482 :
1483 : /* Called when edge E has been redirected to a new destination,
1484 : in order to update the region crossing flag on the edge and
1485 : jump. */
1486 :
1487 : static void
1488 18764151 : fixup_partition_crossing (edge e)
1489 : {
1490 18764151 : if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun) || e->dest
1491 18764151 : == EXIT_BLOCK_PTR_FOR_FN (cfun))
1492 : return;
1493 : /* If we redirected an existing edge, it may already be marked
1494 : crossing, even though the new src is missing a reg crossing note.
1495 : But make sure reg crossing note doesn't already exist before
1496 : inserting. */
1497 18763272 : if (BB_PARTITION (e->src) != BB_PARTITION (e->dest))
1498 : {
1499 110999 : e->flags |= EDGE_CROSSING;
1500 110999 : if (JUMP_P (BB_END (e->src)))
1501 110999 : set_crossing_jump (BB_END (e->src), true);
1502 : }
1503 18652273 : else if (BB_PARTITION (e->src) == BB_PARTITION (e->dest))
1504 : {
1505 18652273 : e->flags &= ~EDGE_CROSSING;
1506 : /* Remove the section crossing note from jump at end of
1507 : src if it exists, and if no other successors are
1508 : still crossing. */
1509 18652273 : if (JUMP_P (BB_END (e->src)) && CROSSING_JUMP_P (BB_END (e->src)))
1510 : {
1511 195236 : bool has_crossing_succ = false;
1512 195236 : edge e2;
1513 195236 : edge_iterator ei;
1514 324438 : FOR_EACH_EDGE (e2, ei, e->src->succs)
1515 : {
1516 272163 : has_crossing_succ |= (e2->flags & EDGE_CROSSING);
1517 272163 : if (has_crossing_succ)
1518 : break;
1519 : }
1520 195236 : if (!has_crossing_succ)
1521 52275 : set_crossing_jump (BB_END (e->src), false);
1522 : }
1523 : }
1524 : }
1525 :
1526 : /* Called when block BB has been reassigned to the cold partition,
1527 : because it is now dominated by another cold block,
1528 : to ensure that the region crossing attributes are updated. */
1529 :
1530 : static void
1531 319 : fixup_new_cold_bb (basic_block bb)
1532 : {
1533 319 : edge e;
1534 319 : edge_iterator ei;
1535 :
1536 : /* This is called when a hot bb is found to now be dominated
1537 : by a cold bb and therefore needs to become cold. Therefore,
1538 : its preds will no longer be region crossing. Any non-dominating
1539 : preds that were previously hot would also have become cold
1540 : in the caller for the same region. Any preds that were previously
1541 : region-crossing will be adjusted in fixup_partition_crossing. */
1542 651 : FOR_EACH_EDGE (e, ei, bb->preds)
1543 : {
1544 332 : fixup_partition_crossing (e);
1545 : }
1546 :
1547 : /* Possibly need to make bb's successor edges region crossing,
1548 : or remove stale region crossing. */
1549 653 : FOR_EACH_EDGE (e, ei, bb->succs)
1550 : {
1551 : /* We can't have fall-through edges across partition boundaries.
1552 : Note that force_nonfallthru will do any necessary partition
1553 : boundary fixup by calling fixup_partition_crossing itself. */
1554 334 : if ((e->flags & EDGE_FALLTHRU)
1555 294 : && BB_PARTITION (bb) != BB_PARTITION (e->dest)
1556 270 : && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1557 0 : force_nonfallthru (e);
1558 : else
1559 334 : fixup_partition_crossing (e);
1560 : }
1561 319 : }
1562 :
1563 : /* Attempt to change code to redirect edge E to TARGET. Don't do that on
1564 : expense of adding new instructions or reordering basic blocks.
1565 :
1566 : Function can be also called with edge destination equivalent to the TARGET.
1567 : Then it should try the simplifications and do nothing if none is possible.
1568 :
1569 : Return edge representing the branch if transformation succeeded. Return NULL
1570 : on failure.
1571 : We still return NULL in case E already destinated TARGET and we didn't
1572 : managed to simplify instruction stream. */
1573 :
1574 : static edge
1575 10069448 : rtl_redirect_edge_and_branch (edge e, basic_block target)
1576 : {
1577 10069448 : edge ret;
1578 10069448 : basic_block src = e->src;
1579 10069448 : basic_block dest = e->dest;
1580 :
1581 10069448 : if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
1582 : return NULL;
1583 :
1584 10069448 : if (dest == target)
1585 : return e;
1586 :
1587 10069442 : if ((ret = try_redirect_by_replacing_jump (e, target, false)) != NULL)
1588 : {
1589 488520 : df_set_bb_dirty (src);
1590 488520 : fixup_partition_crossing (ret);
1591 488520 : return ret;
1592 : }
1593 :
1594 9580922 : ret = redirect_branch_edge (e, target);
1595 9580922 : if (!ret)
1596 : return NULL;
1597 :
1598 1700084 : df_set_bb_dirty (src);
1599 1700084 : fixup_partition_crossing (ret);
1600 1700084 : return ret;
1601 : }
1602 :
1603 : /* Emit a barrier after BB, into the footer if we are in CFGLAYOUT mode. */
1604 :
1605 : void
1606 5523958 : emit_barrier_after_bb (basic_block bb)
1607 : {
1608 5523958 : rtx_barrier *barrier = emit_barrier_after (BB_END (bb));
1609 5523958 : gcc_assert (current_ir_type () == IR_RTL_CFGRTL
1610 : || current_ir_type () == IR_RTL_CFGLAYOUT);
1611 5523958 : if (current_ir_type () == IR_RTL_CFGLAYOUT)
1612 : {
1613 73894 : rtx_insn *insn = unlink_insn_chain (barrier, barrier);
1614 :
1615 73894 : if (BB_FOOTER (bb))
1616 : {
1617 : rtx_insn *footer_tail = BB_FOOTER (bb);
1618 :
1619 3098 : while (NEXT_INSN (footer_tail))
1620 : footer_tail = NEXT_INSN (footer_tail);
1621 3098 : if (!BARRIER_P (footer_tail))
1622 : {
1623 0 : SET_NEXT_INSN (footer_tail) = insn;
1624 0 : SET_PREV_INSN (insn) = footer_tail;
1625 : }
1626 : }
1627 : else
1628 70796 : BB_FOOTER (bb) = insn;
1629 : }
1630 5523958 : }
1631 :
1632 : /* Like force_nonfallthru below, but additionally performs redirection
1633 : Used by redirect_edge_and_branch_force. JUMP_LABEL is used only
1634 : when redirecting to the EXIT_BLOCK, it is either ret_rtx or
1635 : simple_return_rtx, indicating which kind of returnjump to create.
1636 : It should be NULL otherwise. */
1637 :
1638 : basic_block
1639 5386415 : force_nonfallthru_and_redirect (edge e, basic_block target, rtx jump_label)
1640 : {
1641 5386415 : basic_block jump_block, new_bb = NULL, src = e->src;
1642 5386415 : rtx note;
1643 5386415 : edge new_edge;
1644 5386415 : int abnormal_edge_flags = 0;
1645 5386415 : bool asm_goto_edge = false;
1646 :
1647 : /* In the case the last instruction is conditional jump to the next
1648 : instruction, first redirect the jump itself and then continue
1649 : by creating a basic block afterwards to redirect fallthru edge. */
1650 5386415 : if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
1651 5386407 : && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
1652 5386407 : && any_condjump_p (BB_END (e->src))
1653 6177961 : && JUMP_LABEL (BB_END (e->src)) == BB_HEAD (e->dest))
1654 : {
1655 0 : edge b = unchecked_make_edge (e->src, target, 0);
1656 0 : bool redirected;
1657 :
1658 0 : redirected = redirect_jump (as_a <rtx_jump_insn *> (BB_END (e->src)),
1659 0 : block_label (target), 0);
1660 0 : gcc_assert (redirected);
1661 :
1662 0 : if (rtx note = find_reg_note (BB_END (e->src), REG_BR_PROB, NULL_RTX))
1663 : {
1664 0 : int prob = XINT (note, 0);
1665 :
1666 0 : b->probability = profile_probability::from_reg_br_prob_note (prob);
1667 0 : e->probability -= e->probability;
1668 : }
1669 : }
1670 :
1671 5386415 : if (e->flags & EDGE_ABNORMAL)
1672 : {
1673 : /* Irritating special case - fallthru edge to the same block as abnormal
1674 : edge.
1675 : We can't redirect abnormal edge, but we still can split the fallthru
1676 : one and create separate abnormal edge to original destination.
1677 : This allows bb-reorder to make such edge non-fallthru. */
1678 0 : gcc_assert (e->dest == target);
1679 0 : abnormal_edge_flags = e->flags & ~EDGE_FALLTHRU;
1680 0 : e->flags &= EDGE_FALLTHRU;
1681 : }
1682 : else
1683 : {
1684 5386415 : gcc_assert (e->flags & EDGE_FALLTHRU);
1685 5386415 : if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1686 : {
1687 : /* We can't redirect the entry block. Create an empty block
1688 : at the start of the function which we use to add the new
1689 : jump. */
1690 8 : edge tmp;
1691 8 : edge_iterator ei;
1692 8 : bool found = false;
1693 :
1694 8 : basic_block bb = create_basic_block (BB_HEAD (e->dest), NULL,
1695 : ENTRY_BLOCK_PTR_FOR_FN (cfun));
1696 8 : bb->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
1697 :
1698 : /* Make sure new block ends up in correct hot/cold section. */
1699 8 : BB_COPY_PARTITION (bb, e->dest);
1700 :
1701 : /* Change the existing edge's source to be the new block, and add
1702 : a new edge from the entry block to the new block. */
1703 8 : e->src = bb;
1704 8 : for (ei = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
1705 8 : (tmp = ei_safe_edge (ei)); )
1706 : {
1707 8 : if (tmp == e)
1708 : {
1709 8 : ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs->unordered_remove (ei.index);
1710 8 : found = true;
1711 8 : break;
1712 : }
1713 : else
1714 0 : ei_next (&ei);
1715 : }
1716 :
1717 0 : gcc_assert (found);
1718 :
1719 8 : vec_safe_push (bb->succs, e);
1720 8 : make_single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb,
1721 : EDGE_FALLTHRU);
1722 : }
1723 : }
1724 :
1725 : /* If e->src ends with asm goto, see if any of the ASM_OPERANDS_LABELs
1726 : don't point to the fallthru label. */
1727 5386415 : if (JUMP_P (BB_END (e->src))
1728 791734 : && target != EXIT_BLOCK_PTR_FOR_FN (cfun)
1729 791365 : && (e->flags & EDGE_FALLTHRU)
1730 6177780 : && (note = extract_asm_operands (PATTERN (BB_END (e->src)))))
1731 : {
1732 188 : int n = ASM_OPERANDS_LABEL_LENGTH (note);
1733 :
1734 347 : for (int i = 0; i < n; ++i)
1735 227 : if (XEXP (ASM_OPERANDS_LABEL (note, i), 0) == BB_HEAD (e->dest))
1736 : {
1737 : asm_goto_edge = true;
1738 : break;
1739 : }
1740 : }
1741 :
1742 5386415 : if (EDGE_COUNT (e->src->succs) >= 2 || abnormal_edge_flags || asm_goto_edge)
1743 : {
1744 990526 : rtx_insn *new_head;
1745 990526 : profile_count count = e->count ();
1746 990526 : profile_probability probability = e->probability;
1747 : /* Create the new structures. */
1748 :
1749 : /* If the old block ended with a tablejump, skip its table
1750 : by searching forward from there. Otherwise start searching
1751 : forward from the last instruction of the old block. A shared
1752 : table follows only one of the jumps through it. */
1753 990526 : rtx_jump_table_data *table;
1754 990526 : if (tablejump_p (BB_END (e->src), NULL, &table)
1755 990526 : && (!JUMP_TABLE_SHARED_P (PATTERN (table))
1756 0 : || hosted_shared_table (e->src) == table))
1757 0 : new_head = table;
1758 : else
1759 990526 : new_head = BB_END (e->src);
1760 990526 : new_head = NEXT_INSN (new_head);
1761 :
1762 990526 : jump_block = create_basic_block (new_head, NULL, e->src);
1763 990526 : jump_block->count = count;
1764 :
1765 : /* Make sure new block ends up in correct hot/cold section. */
1766 :
1767 990526 : BB_COPY_PARTITION (jump_block, e->src);
1768 :
1769 : /* Wire edge in. */
1770 990526 : new_edge = make_edge (e->src, jump_block, EDGE_FALLTHRU);
1771 990526 : new_edge->probability = probability;
1772 :
1773 : /* Redirect old edge. */
1774 990526 : redirect_edge_pred (e, jump_block);
1775 990526 : e->probability = profile_probability::always ();
1776 :
1777 : /* If e->src was previously region crossing, it no longer is
1778 : and the reg crossing note should be removed. */
1779 990526 : fixup_partition_crossing (new_edge);
1780 :
1781 : /* If asm goto has any label refs to e->dest, change them to point
1782 : to jump_block instead. */
1783 990526 : if (asm_goto_edge)
1784 : {
1785 68 : int n = ASM_OPERANDS_LABEL_LENGTH (note);
1786 :
1787 174 : for (int i = 0; i < n; ++i)
1788 106 : if (label_ref_label (ASM_OPERANDS_LABEL (note, i))
1789 106 : == BB_HEAD (e->dest))
1790 : {
1791 69 : LABEL_NUSES (label_ref_label (ASM_OPERANDS_LABEL (note, i)))--;
1792 69 : set_label_ref_label (ASM_OPERANDS_LABEL (note, i),
1793 69 : block_label (jump_block));
1794 69 : LABEL_NUSES (label_ref_label (ASM_OPERANDS_LABEL (note, i)))++;
1795 : }
1796 :
1797 68 : rtx_insn *insn = BB_END (new_edge->src);
1798 68 : rtx_insn *old_label = BB_HEAD (e->dest);
1799 68 : rtx_insn *new_label = BB_HEAD (jump_block);
1800 :
1801 68 : if (JUMP_LABEL (insn) == old_label)
1802 : {
1803 55 : JUMP_LABEL (insn) = new_label;
1804 55 : if (rtx note = find_reg_note (insn, REG_LABEL_TARGET, new_label))
1805 0 : remove_note (insn, note);
1806 : }
1807 : else
1808 : {
1809 13 : if (rtx note = find_reg_note (insn, REG_LABEL_TARGET, old_label))
1810 13 : remove_note (insn, note);
1811 13 : if (JUMP_LABEL (insn) != new_label
1812 13 : && !find_reg_note (insn, REG_LABEL_TARGET, new_label))
1813 13 : add_reg_note (insn, REG_LABEL_TARGET, new_label);
1814 : }
1815 68 : while (rtx note = find_reg_note (insn, REG_LABEL_OPERAND, old_label))
1816 0 : XEXP (note, 0) = new_label;
1817 : }
1818 :
1819 990526 : new_bb = jump_block;
1820 : }
1821 : else
1822 : jump_block = e->src;
1823 :
1824 5386415 : const location_t loc = e->goto_locus;
1825 5386415 : e->flags &= ~EDGE_FALLTHRU;
1826 5386415 : if (target == EXIT_BLOCK_PTR_FOR_FN (cfun))
1827 : {
1828 593 : if (jump_label == ret_rtx)
1829 0 : emit_jump_insn_after_setloc (targetm.gen_return (),
1830 0 : BB_END (jump_block), loc);
1831 : else
1832 : {
1833 593 : gcc_assert (jump_label == simple_return_rtx);
1834 593 : emit_jump_insn_after_setloc (targetm.gen_simple_return (),
1835 593 : BB_END (jump_block), loc);
1836 : }
1837 593 : set_return_jump_label (BB_END (jump_block));
1838 : }
1839 : else
1840 : {
1841 5385822 : rtx_code_label *label = block_label (target);
1842 5385822 : emit_jump_insn_after_setloc (targetm.gen_jump (label),
1843 5385822 : BB_END (jump_block), loc);
1844 5385822 : JUMP_LABEL (BB_END (jump_block)) = label;
1845 5385822 : LABEL_NUSES (label)++;
1846 : }
1847 :
1848 : /* We might be in cfg layout mode, and if so, the following routine will
1849 : insert the barrier correctly. */
1850 5386415 : emit_barrier_after_bb (jump_block);
1851 5386415 : redirect_edge_succ_nodup (e, target);
1852 :
1853 5386415 : if (abnormal_edge_flags)
1854 0 : make_edge (src, target, abnormal_edge_flags);
1855 :
1856 5386415 : df_mark_solutions_dirty ();
1857 5386415 : fixup_partition_crossing (e);
1858 5386415 : return new_bb;
1859 : }
1860 :
1861 : /* Edge E is assumed to be fallthru edge. Emit needed jump instruction
1862 : (and possibly create new basic block) to make edge non-fallthru.
1863 : Return newly created BB or NULL if none. */
1864 :
1865 : static basic_block
1866 777693 : rtl_force_nonfallthru (edge e)
1867 : {
1868 777693 : return force_nonfallthru_and_redirect (e, e->dest, NULL_RTX);
1869 : }
1870 :
1871 : /* Redirect edge even at the expense of creating new jump insn or
1872 : basic block. Return new basic block if created, NULL otherwise.
1873 : Conversion must be possible. */
1874 :
1875 : static basic_block
1876 497054 : rtl_redirect_edge_and_branch_force (edge e, basic_block target)
1877 : {
1878 497054 : if (redirect_edge_and_branch (e, target)
1879 497054 : || e->dest == target)
1880 : return NULL;
1881 :
1882 : /* In case the edge redirection failed, try to force it to be non-fallthru
1883 : and redirect newly created simplejump. */
1884 404470 : df_set_bb_dirty (e->src);
1885 404470 : return force_nonfallthru_and_redirect (e, target, NULL_RTX);
1886 : }
1887 :
1888 : /* The given edge should potentially be a fallthru edge. If that is in
1889 : fact true, delete the jump and barriers that are in the way. */
1890 :
1891 : static void
1892 823179 : rtl_tidy_fallthru_edge (edge e)
1893 : {
1894 823179 : rtx_insn *q;
1895 823179 : basic_block b = e->src, c = b->next_bb;
1896 :
1897 : /* ??? In a late-running flow pass, other folks may have deleted basic
1898 : blocks by nopping out blocks, leaving multiple BARRIERs between here
1899 : and the target label. They ought to be chastised and fixed.
1900 :
1901 : We can also wind up with a sequence of undeletable labels between
1902 : one block and the next.
1903 :
1904 : So search through a sequence of barriers, labels, and notes for
1905 : the head of block C and assert that we really do fall through. */
1906 :
1907 1678572 : for (q = NEXT_INSN (BB_END (b)); q != BB_HEAD (c); q = NEXT_INSN (q))
1908 32214 : if (NONDEBUG_INSN_P (q) || shared_jump_table_component_p (q))
1909 : return;
1910 :
1911 : /* Remove what will soon cease being the jump insn from the source block.
1912 : If block B consisted only of this single jump, turn it into a deleted
1913 : note. */
1914 823179 : q = BB_END (b);
1915 823179 : if (JUMP_P (q)
1916 40493 : && onlyjump_p (q)
1917 863660 : && (any_uncondjump_p (q)
1918 9050 : || single_succ_p (b)))
1919 : {
1920 33099 : rtx_insn *label;
1921 33099 : rtx_jump_table_data *table;
1922 :
1923 : /* A shared table is still needed by the other jumps that dispatch
1924 : through it; deleting this jump alone just drops one reference. */
1925 33099 : if (tablejump_p (q, &label, &table)
1926 33099 : && !JUMP_TABLE_SHARED_P (PATTERN (table)))
1927 : {
1928 : /* The label is likely mentioned in some instruction before
1929 : the tablejump and might not be DCEd, so turn it into
1930 : a note instead and move before the tablejump that is going to
1931 : be deleted. */
1932 0 : const char *name = LABEL_NAME (label);
1933 0 : PUT_CODE (label, NOTE);
1934 0 : NOTE_KIND (label) = NOTE_INSN_DELETED_LABEL;
1935 0 : NOTE_DELETED_LABEL_NAME (label) = name;
1936 0 : reorder_insns (label, label, PREV_INSN (q));
1937 0 : delete_insn (table);
1938 : }
1939 :
1940 33099 : q = PREV_INSN (q);
1941 : }
1942 : /* Unconditional jumps with side-effects (i.e. which we can't just delete
1943 : together with the barrier) should never have a fallthru edge. */
1944 790080 : else if (JUMP_P (q) && any_uncondjump_p (q))
1945 : return;
1946 :
1947 : /* Selectively unlink the sequence. */
1948 823179 : if (q != PREV_INSN (BB_HEAD (c)))
1949 33480 : delete_insn_chain (NEXT_INSN (q), PREV_INSN (BB_HEAD (c)), false);
1950 :
1951 823179 : e->flags |= EDGE_FALLTHRU;
1952 : }
1953 :
1954 : /* Should move basic block BB after basic block AFTER. NIY. */
1955 :
1956 : static bool
1957 708383 : rtl_move_block_after (basic_block bb ATTRIBUTE_UNUSED,
1958 : basic_block after ATTRIBUTE_UNUSED)
1959 : {
1960 708383 : return false;
1961 : }
1962 :
1963 : /* Locate the last bb in the same partition as START_BB. */
1964 :
1965 : static basic_block
1966 34200 : last_bb_in_partition (basic_block start_bb)
1967 : {
1968 34200 : basic_block bb;
1969 21621073 : FOR_BB_BETWEEN (bb, start_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
1970 : {
1971 21621073 : if (BB_PARTITION (start_bb) != BB_PARTITION (bb->next_bb))
1972 : return bb;
1973 : }
1974 : /* Return bb before the exit block. */
1975 0 : return bb->prev_bb;
1976 : }
1977 :
1978 : /* Split a (typically critical) edge. Return the new block.
1979 : The edge must not be abnormal.
1980 :
1981 : ??? The code generally expects to be called on critical edges.
1982 : The case of a block ending in an unconditional jump to a
1983 : block with multiple predecessors is not handled optimally. */
1984 :
1985 : static basic_block
1986 1224411 : rtl_split_edge (edge edge_in)
1987 : {
1988 1224411 : basic_block bb, new_bb;
1989 1224411 : rtx_insn *before;
1990 :
1991 : /* Abnormal edges cannot be split. */
1992 1224411 : gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
1993 :
1994 : /* We are going to place the new block in front of edge destination.
1995 : Avoid existence of fallthru predecessors. */
1996 1224411 : if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1997 : {
1998 696051 : edge e = find_fallthru_edge (edge_in->dest->preds);
1999 :
2000 696051 : if (e)
2001 526478 : force_nonfallthru (e);
2002 : }
2003 :
2004 : /* Create the basic block note. */
2005 1224411 : if (edge_in->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
2006 966320 : before = BB_HEAD (edge_in->dest);
2007 : else
2008 : before = NULL;
2009 :
2010 : /* If this is a fall through edge to the exit block, the blocks might be
2011 : not adjacent, and the right place is after the source. */
2012 1224411 : if ((edge_in->flags & EDGE_FALLTHRU)
2013 528360 : && edge_in->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
2014 : {
2015 258091 : before = NEXT_INSN (BB_END (edge_in->src));
2016 258091 : bb = create_basic_block (before, NULL, edge_in->src);
2017 258091 : BB_COPY_PARTITION (bb, edge_in->src);
2018 258091 : }
2019 : else
2020 : {
2021 966320 : if (edge_in->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
2022 : {
2023 79567 : bb = create_basic_block (before, NULL, edge_in->dest->prev_bb);
2024 79567 : BB_COPY_PARTITION (bb, edge_in->dest);
2025 : }
2026 : else
2027 : {
2028 886753 : basic_block after = edge_in->dest->prev_bb;
2029 : /* If this is post-bb reordering, and the edge crosses a partition
2030 : boundary, the new block needs to be inserted in the bb chain
2031 : at the end of the src partition (since we put the new bb into
2032 : that partition, see below). Otherwise we may end up creating
2033 : an extra partition crossing in the chain, which is illegal.
2034 : It can't go after the src, because src may have a fall-through
2035 : to a different block. */
2036 886753 : if (crtl->bb_reorder_complete
2037 60305 : && (edge_in->flags & EDGE_CROSSING))
2038 : {
2039 34200 : after = last_bb_in_partition (edge_in->src);
2040 34200 : before = get_last_bb_insn (after);
2041 : /* The instruction following the last bb in partition should
2042 : be a barrier, since it cannot end in a fall-through. */
2043 34200 : gcc_checking_assert (BARRIER_P (before));
2044 34200 : before = NEXT_INSN (before);
2045 : }
2046 886753 : bb = create_basic_block (before, NULL, after);
2047 : /* Put the split bb into the src partition, to avoid creating
2048 : a situation where a cold bb dominates a hot bb, in the case
2049 : where src is cold and dest is hot. The src will dominate
2050 : the new bb (whereas it might not have dominated dest). */
2051 886753 : BB_COPY_PARTITION (bb, edge_in->src);
2052 : }
2053 : }
2054 :
2055 1224411 : make_single_succ_edge (bb, edge_in->dest, EDGE_FALLTHRU);
2056 :
2057 : /* Can't allow a region crossing edge to be fallthrough. */
2058 1224411 : if (BB_PARTITION (bb) != BB_PARTITION (edge_in->dest)
2059 72996 : && edge_in->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
2060 : {
2061 41168 : new_bb = force_nonfallthru (single_succ_edge (bb));
2062 41168 : gcc_assert (!new_bb);
2063 : }
2064 :
2065 : /* For non-fallthru edges, we must adjust the predecessor's
2066 : jump instruction to target our new block. */
2067 1224411 : if ((edge_in->flags & EDGE_FALLTHRU) == 0)
2068 : {
2069 696051 : edge redirected = redirect_edge_and_branch (edge_in, bb);
2070 696051 : gcc_assert (redirected);
2071 : }
2072 : else
2073 : {
2074 528360 : if (edge_in->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
2075 : {
2076 : /* For asm goto even splitting of fallthru edge might
2077 : need insn patching, as other labels might point to the
2078 : old label. */
2079 448793 : rtx_insn *last = BB_END (edge_in->src);
2080 448793 : if (last
2081 448793 : && JUMP_P (last)
2082 205395 : && edge_in->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
2083 179637 : && (extract_asm_operands (PATTERN (last))
2084 179628 : || JUMP_LABEL (last) == before)
2085 448802 : && patch_jump_insn (last, before, bb))
2086 9 : df_set_bb_dirty (edge_in->src);
2087 : }
2088 528360 : redirect_edge_succ (edge_in, bb);
2089 : }
2090 :
2091 1224411 : return bb;
2092 : }
2093 :
2094 : /* Queue instructions for insertion on an edge between two basic blocks.
2095 : The new instructions and basic blocks (if any) will not appear in the
2096 : CFG until commit_edge_insertions is called. If there are already
2097 : queued instructions on the edge, PATTERN is appended to them. */
2098 :
2099 : void
2100 5686161 : insert_insn_on_edge (rtx pattern, edge e)
2101 : {
2102 : /* We cannot insert instructions on an abnormal critical edge.
2103 : It will be easier to find the culprit if we die now. */
2104 5686161 : gcc_assert (!((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e)));
2105 :
2106 5686161 : if (e->insns.r == NULL_RTX)
2107 4758827 : start_sequence ();
2108 : else
2109 927334 : push_to_sequence (e->insns.r);
2110 :
2111 5686161 : emit_insn (pattern);
2112 :
2113 5686161 : e->insns.r = end_sequence ();
2114 5686161 : }
2115 :
2116 : /* Like insert_insn_on_edge, but if there are already queued instructions
2117 : on the edge, PATTERN is prepended to them. */
2118 :
2119 : void
2120 349 : prepend_insn_to_edge (rtx pattern, edge e)
2121 : {
2122 : /* We cannot insert instructions on an abnormal critical edge.
2123 : It will be easier to find the culprit if we die now. */
2124 349 : gcc_assert (!((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e)));
2125 :
2126 349 : start_sequence ();
2127 :
2128 349 : emit_insn (pattern);
2129 349 : emit_insn (e->insns.r);
2130 :
2131 349 : e->insns.r = end_sequence ();
2132 349 : }
2133 :
2134 : /* Update the CFG for the instructions queued on edge E. */
2135 :
2136 : void
2137 4727376 : commit_one_edge_insertion (edge e)
2138 : {
2139 4727376 : rtx_insn *before = NULL, *after = NULL, *insns, *tmp, *last;
2140 4727376 : basic_block bb;
2141 :
2142 : /* Pull the insns off the edge now since the edge might go away. */
2143 4727376 : insns = e->insns.r;
2144 4727376 : e->insns.r = NULL;
2145 :
2146 : /* Allow the sequence to contain internal jumps, such as a memcpy loop
2147 : or an allocation loop. If such a sequence is emitted during RTL
2148 : expansion, we'll create the appropriate basic blocks later,
2149 : at the end of the pass. But if such a sequence is emitted after
2150 : initial expansion, we'll need to find the subblocks ourselves. */
2151 4727376 : bool contains_jump = false;
2152 4727376 : if (!currently_expanding_to_rtl)
2153 13565290 : for (rtx_insn *insn = insns; insn; insn = NEXT_INSN (insn))
2154 11761589 : if (JUMP_P (insn))
2155 : {
2156 1660511 : rebuild_jump_labels_chain (insns);
2157 1660511 : contains_jump = true;
2158 1660511 : break;
2159 : }
2160 :
2161 : /* Figure out where to put these insns. If the destination has
2162 : one predecessor, insert there. Except for the exit block. */
2163 4727376 : if (single_pred_p (e->dest) && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
2164 : {
2165 1654992 : bb = e->dest;
2166 :
2167 : /* Get the location correct wrt a code label, and "nice" wrt
2168 : a basic block note, and before everything else. */
2169 1654992 : tmp = BB_HEAD (bb);
2170 1654992 : if (LABEL_P (tmp))
2171 74364 : tmp = NEXT_INSN (tmp);
2172 1654992 : if (NOTE_INSN_BASIC_BLOCK_P (tmp))
2173 1654992 : tmp = NEXT_INSN (tmp);
2174 1654992 : if (tmp == BB_HEAD (bb))
2175 : before = tmp;
2176 1654992 : else if (tmp)
2177 1654992 : after = PREV_INSN (tmp);
2178 : else
2179 0 : after = get_last_insn ();
2180 : }
2181 :
2182 : /* If the source has one successor and the edge is not abnormal,
2183 : insert there. Except for the entry block.
2184 : Don't do this if the predecessor ends in a jump other than
2185 : unconditional simple jump. E.g. for asm goto that points all
2186 : its labels at the fallthru basic block, we can't insert instructions
2187 : before the asm goto, as the asm goto can have various of side effects,
2188 : and can't emit instructions after the asm goto, as it must end
2189 : the basic block. */
2190 3072384 : else if ((e->flags & EDGE_ABNORMAL) == 0
2191 3072384 : && single_succ_p (e->src)
2192 2207142 : && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
2193 5278038 : && (!JUMP_P (BB_END (e->src))
2194 379829 : || simplejump_p (BB_END (e->src))))
2195 : {
2196 2205654 : bb = e->src;
2197 :
2198 : /* It is possible to have a non-simple jump here. Consider a target
2199 : where some forms of unconditional jumps clobber a register. This
2200 : happens on the fr30 for example.
2201 :
2202 : We know this block has a single successor, so we can just emit
2203 : the queued insns before the jump. */
2204 2205654 : if (JUMP_P (BB_END (bb)))
2205 : before = BB_END (bb);
2206 : else
2207 : {
2208 : /* We'd better be fallthru, or we've lost track of what's what. */
2209 1825825 : gcc_assert (e->flags & EDGE_FALLTHRU);
2210 :
2211 : after = BB_END (bb);
2212 : }
2213 : }
2214 :
2215 : /* Otherwise we must split the edge. */
2216 : else
2217 : {
2218 866730 : bb = split_edge (e);
2219 :
2220 : /* If E crossed a partition boundary, we needed to make bb end in
2221 : a region-crossing jump, even though it was originally fallthru. */
2222 866730 : if (JUMP_P (BB_END (bb)))
2223 : before = BB_END (bb);
2224 : else
2225 : after = BB_END (bb);
2226 : }
2227 :
2228 : /* Now that we've found the spot, do the insertion. */
2229 1654992 : if (before)
2230 : {
2231 420997 : emit_insn_before_noloc (insns, before, bb);
2232 420997 : last = prev_nonnote_insn (before);
2233 : }
2234 : else
2235 4306379 : last = emit_insn_after_noloc (insns, after, bb);
2236 :
2237 4727376 : if (returnjump_p (last))
2238 : {
2239 : /* ??? Remove all outgoing edges from BB and add one for EXIT.
2240 : This is not currently a problem because this only happens
2241 : for the (single) epilogue, which already has a fallthru edge
2242 : to EXIT. */
2243 :
2244 1400561 : e = single_succ_edge (bb);
2245 2801122 : gcc_assert (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
2246 : && single_succ_p (bb) && (e->flags & EDGE_FALLTHRU));
2247 :
2248 1400561 : e->flags &= ~EDGE_FALLTHRU;
2249 1400561 : emit_barrier_after (last);
2250 :
2251 1400561 : if (before)
2252 0 : delete_insn (before);
2253 : }
2254 : else
2255 : /* Sequences inserted after RTL expansion are expected to be SESE,
2256 : with only internal branches allowed. If the sequence jumps outside
2257 : itself then we do not know how to add the associated edges here. */
2258 3326815 : gcc_assert (!JUMP_P (last) || currently_expanding_to_rtl);
2259 :
2260 4727376 : if (contains_jump)
2261 1660511 : find_sub_basic_blocks (bb);
2262 4727376 : }
2263 :
2264 : /* Update the CFG for all queued instructions. */
2265 :
2266 : void
2267 4718027 : commit_edge_insertions (void)
2268 : {
2269 4718027 : basic_block bb;
2270 :
2271 : /* Optimization passes that invoke this routine can cause hot blocks
2272 : previously reached by both hot and cold blocks to become dominated only
2273 : by cold blocks. This will cause the verification below to fail,
2274 : and lead to now cold code in the hot section. In some cases this
2275 : may only be visible after newly unreachable blocks are deleted,
2276 : which will be done by fixup_partitions. */
2277 4718027 : fixup_partitions ();
2278 :
2279 4718027 : if (!currently_expanding_to_rtl)
2280 3186139 : checking_verify_flow_info ();
2281 :
2282 68727991 : FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
2283 : EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
2284 : {
2285 64009964 : edge e;
2286 64009964 : edge_iterator ei;
2287 :
2288 152964733 : FOR_EACH_EDGE (e, ei, bb->succs)
2289 88954769 : if (e->insns.r)
2290 : {
2291 4686842 : if (currently_expanding_to_rtl)
2292 1263164 : rebuild_jump_labels_chain (e->insns.r);
2293 4686842 : commit_one_edge_insertion (e);
2294 : }
2295 : }
2296 4718027 : }
2297 :
2298 :
2299 : /* Print out RTL-specific basic block information (live information
2300 : at start and end with TDF_DETAILS). FLAGS are the TDF_* masks
2301 : documented in dumpfile.h. */
2302 :
2303 : static void
2304 2776 : rtl_dump_bb (FILE *outf, basic_block bb, int indent, dump_flags_t flags)
2305 : {
2306 2776 : char *s_indent;
2307 :
2308 2776 : s_indent = (char *) alloca ((size_t) indent + 1);
2309 2776 : memset (s_indent, ' ', (size_t) indent);
2310 2776 : s_indent[indent] = '\0';
2311 :
2312 2776 : if (df && (flags & TDF_DETAILS))
2313 : {
2314 773 : df_dump_top (bb, outf);
2315 773 : putc ('\n', outf);
2316 : }
2317 :
2318 2776 : if (bb->index != ENTRY_BLOCK && bb->index != EXIT_BLOCK
2319 2776 : && rtl_bb_info_initialized_p (bb))
2320 : {
2321 2192 : rtx_insn *last = BB_END (bb);
2322 2192 : if (last)
2323 2192 : last = NEXT_INSN (last);
2324 15948 : for (rtx_insn *insn = BB_HEAD (bb); insn != last; insn = NEXT_INSN (insn))
2325 : {
2326 13756 : if (flags & TDF_DETAILS)
2327 4392 : df_dump_insn_top (insn, outf);
2328 13756 : if (! (flags & TDF_SLIM))
2329 13724 : print_rtl_single (outf, insn);
2330 : else
2331 32 : dump_insn_slim (outf, insn);
2332 13756 : if (flags & TDF_DETAILS)
2333 4392 : df_dump_insn_bottom (insn, outf);
2334 : }
2335 : }
2336 :
2337 2776 : if (df && (flags & TDF_DETAILS))
2338 : {
2339 773 : df_dump_bottom (bb, outf);
2340 773 : putc ('\n', outf);
2341 : }
2342 :
2343 2776 : }
2344 :
2345 : /* Like dump_function_to_file, but for RTL. Print out dataflow information
2346 : for the start of each basic block. FLAGS are the TDF_* masks documented
2347 : in dumpfile.h. */
2348 :
2349 : void
2350 4418 : print_rtl_with_bb (FILE *outf, const rtx_insn *rtx_first, dump_flags_t flags)
2351 : {
2352 4418 : const rtx_insn *tmp_rtx;
2353 4418 : if (rtx_first == 0)
2354 0 : fprintf (outf, "(nil)\n");
2355 : else
2356 : {
2357 4418 : enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
2358 4418 : int max_uid = get_max_uid ();
2359 4418 : basic_block *start = XCNEWVEC (basic_block, max_uid);
2360 4418 : basic_block *end = XCNEWVEC (basic_block, max_uid);
2361 4418 : enum bb_state *in_bb_p = XCNEWVEC (enum bb_state, max_uid);
2362 4418 : basic_block bb;
2363 :
2364 : /* After freeing the CFG, we still have BLOCK_FOR_INSN set on most
2365 : insns, but the CFG is not maintained so the basic block info
2366 : is not reliable. Therefore it's omitted from the dumps. */
2367 4418 : if (! (cfun->curr_properties & PROP_cfg))
2368 1090 : flags &= ~TDF_BLOCKS;
2369 :
2370 4418 : if (df)
2371 3379 : df_dump_start (outf);
2372 :
2373 4418 : if (cfun->curr_properties & PROP_cfg)
2374 : {
2375 22637 : FOR_EACH_BB_REVERSE_FN (bb, cfun)
2376 : {
2377 19309 : rtx_insn *x;
2378 :
2379 19309 : start[INSN_UID (BB_HEAD (bb))] = bb;
2380 19309 : end[INSN_UID (BB_END (bb))] = bb;
2381 19309 : if (flags & TDF_BLOCKS)
2382 : {
2383 5312 : for (x = BB_HEAD (bb); x != NULL_RTX; x = NEXT_INSN (x))
2384 : {
2385 5312 : enum bb_state state = IN_MULTIPLE_BB;
2386 :
2387 5312 : if (in_bb_p[INSN_UID (x)] == NOT_IN_BB)
2388 5312 : state = IN_ONE_BB;
2389 5312 : in_bb_p[INSN_UID (x)] = state;
2390 :
2391 5312 : if (x == BB_END (bb))
2392 : break;
2393 : }
2394 : }
2395 : }
2396 : }
2397 :
2398 174529 : for (tmp_rtx = rtx_first; tmp_rtx != NULL; tmp_rtx = NEXT_INSN (tmp_rtx))
2399 : {
2400 170111 : if (flags & TDF_BLOCKS)
2401 : {
2402 6096 : bb = start[INSN_UID (tmp_rtx)];
2403 6096 : if (bb != NULL)
2404 : {
2405 878 : dump_bb_info (outf, bb, 0, dump_flags, true, false);
2406 878 : if (df && (flags & TDF_DETAILS))
2407 582 : df_dump_top (bb, outf);
2408 : }
2409 :
2410 6096 : if (in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB
2411 784 : && !NOTE_P (tmp_rtx)
2412 6305 : && !BARRIER_P (tmp_rtx))
2413 0 : fprintf (outf, ";; Insn is not within a basic block\n");
2414 6096 : else if (in_bb_p[INSN_UID (tmp_rtx)] == IN_MULTIPLE_BB)
2415 0 : fprintf (outf, ";; Insn is in multiple basic blocks\n");
2416 : }
2417 :
2418 170111 : if (flags & TDF_DETAILS)
2419 19822 : df_dump_insn_top (tmp_rtx, outf);
2420 170111 : if (! (flags & TDF_SLIM))
2421 169575 : print_rtl_single (outf, tmp_rtx);
2422 : else
2423 536 : dump_insn_slim (outf, tmp_rtx);
2424 170111 : if (flags & TDF_DETAILS)
2425 19822 : df_dump_insn_bottom (tmp_rtx, outf);
2426 :
2427 170111 : bb = end[INSN_UID (tmp_rtx)];
2428 170111 : if (bb != NULL)
2429 : {
2430 19309 : if (flags & TDF_BLOCKS)
2431 : {
2432 878 : dump_bb_info (outf, bb, 0, dump_flags, false, true);
2433 878 : if (df && (flags & TDF_DETAILS))
2434 582 : df_dump_bottom (bb, outf);
2435 878 : putc ('\n', outf);
2436 : }
2437 : /* Emit a hint if the fallthrough target of current basic block
2438 : isn't the one placed right next. */
2439 188478 : else if (EDGE_COUNT (bb->succs) > 0)
2440 : {
2441 18367 : gcc_assert (BB_END (bb) == tmp_rtx);
2442 18367 : const rtx_insn *ninsn = NEXT_INSN (tmp_rtx);
2443 : /* Bypass intervening deleted-insn notes and debug insns. */
2444 18367 : while (ninsn
2445 19975 : && !NONDEBUG_INSN_P (ninsn)
2446 42772 : && !start[INSN_UID (ninsn)])
2447 4430 : ninsn = NEXT_INSN (ninsn);
2448 18367 : edge e = find_fallthru_edge (bb->succs);
2449 18367 : if (e && ninsn)
2450 : {
2451 13252 : basic_block dest = e->dest;
2452 13252 : if (start[INSN_UID (ninsn)] != dest)
2453 2222 : fprintf (outf, "%s ; pc falls through to BB %d\n",
2454 : print_rtx_head, dest->index);
2455 : }
2456 : }
2457 : }
2458 : }
2459 :
2460 4418 : free (start);
2461 4418 : free (end);
2462 4418 : free (in_bb_p);
2463 : }
2464 4418 : }
2465 :
2466 : /* Update the branch probability of BB if a REG_BR_PROB is present. */
2467 :
2468 : void
2469 9769262 : update_br_prob_note (basic_block bb)
2470 : {
2471 9769262 : rtx note;
2472 9769262 : note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX);
2473 9769262 : if (!JUMP_P (BB_END (bb)) || !BRANCH_EDGE (bb)->probability.initialized_p ())
2474 : {
2475 256582 : if (note)
2476 0 : remove_note (BB_END (bb), note);
2477 : return;
2478 : }
2479 9512680 : if (!note
2480 9512680 : || XINT (note, 0) == BRANCH_EDGE (bb)->probability.to_reg_br_prob_note ())
2481 : return;
2482 210862 : XINT (note, 0) = BRANCH_EDGE (bb)->probability.to_reg_br_prob_note ();
2483 : }
2484 :
2485 : /* Get the last insn associated with block BB (that includes barriers and
2486 : tablejumps after BB). */
2487 : rtx_insn *
2488 7052991 : get_last_bb_insn (basic_block bb)
2489 : {
2490 7052991 : rtx_jump_table_data *table;
2491 7052991 : rtx_insn *tmp;
2492 7052991 : rtx_insn *end = BB_END (bb);
2493 :
2494 : /* Include any jump table following the basic block. A table that other
2495 : jumps dispatch through as well follows only one of them. */
2496 7052991 : if (tablejump_p (end, NULL, &table)
2497 7052991 : && (!JUMP_TABLE_SHARED_P (PATTERN (table))
2498 211 : || hosted_shared_table (bb) == table))
2499 274 : end = table;
2500 :
2501 : /* Include any barriers that may follow the basic block. */
2502 7052991 : tmp = next_nonnote_nondebug_insn_bb (end);
2503 14715332 : while (tmp && BARRIER_P (tmp))
2504 : {
2505 609350 : end = tmp;
2506 609350 : tmp = next_nonnote_nondebug_insn_bb (end);
2507 : }
2508 :
2509 7052991 : return end;
2510 : }
2511 :
2512 : /* Add all BBs reachable from entry via hot paths into the SET. */
2513 :
2514 : void
2515 574440 : find_bbs_reachable_by_hot_paths (hash_set<basic_block> *set)
2516 : {
2517 574440 : auto_vec<basic_block, 64> worklist;
2518 :
2519 574440 : set->add (ENTRY_BLOCK_PTR_FOR_FN (cfun));
2520 574440 : worklist.safe_push (ENTRY_BLOCK_PTR_FOR_FN (cfun));
2521 :
2522 19778752 : while (worklist.length () > 0)
2523 : {
2524 18629872 : basic_block bb = worklist.pop ();
2525 18629872 : edge_iterator ei;
2526 18629872 : edge e;
2527 :
2528 47274950 : FOR_EACH_EDGE (e, ei, bb->succs)
2529 28645078 : if (BB_PARTITION (e->dest) != BB_COLD_PARTITION
2530 28645078 : && !set->add (e->dest))
2531 18055432 : worklist.safe_push (e->dest);
2532 : }
2533 574440 : }
2534 :
2535 : /* Sanity check partition hotness to ensure that basic blocks in
2536 : Â the cold partition don't dominate basic blocks in the hot partition.
2537 : If FLAG_ONLY is true, report violations as errors. Otherwise
2538 : re-mark the dominated blocks as cold, since this is run after
2539 : cfg optimizations that may make hot blocks previously reached
2540 : by both hot and cold blocks now only reachable along cold paths. */
2541 :
2542 : static auto_vec<basic_block>
2543 508353 : find_partition_fixes (bool flag_only)
2544 : {
2545 508353 : basic_block bb;
2546 508353 : auto_vec<basic_block> bbs_to_fix;
2547 508353 : hash_set<basic_block> set;
2548 :
2549 : /* Callers check this. */
2550 508353 : gcc_checking_assert (crtl->has_bb_partition);
2551 :
2552 508353 : find_bbs_reachable_by_hot_paths (&set);
2553 :
2554 18550304 : FOR_EACH_BB_FN (bb, cfun)
2555 18041951 : if (!set.contains (bb)
2556 18041951 : && BB_PARTITION (bb) != BB_COLD_PARTITION)
2557 : {
2558 319 : if (flag_only)
2559 0 : error ("non-cold basic block %d reachable only "
2560 : "by paths crossing the cold partition", bb->index);
2561 : else
2562 319 : BB_SET_PARTITION (bb, BB_COLD_PARTITION);
2563 319 : bbs_to_fix.safe_push (bb);
2564 : }
2565 :
2566 508353 : return bbs_to_fix;
2567 508353 : }
2568 :
2569 : /* Perform cleanup on the hot/cold bb partitioning after optimization
2570 : passes that modify the cfg. */
2571 :
2572 : void
2573 13250223 : fixup_partitions (void)
2574 : {
2575 13250223 : if (!crtl->has_bb_partition)
2576 12868070 : return;
2577 :
2578 : /* Delete any blocks that became unreachable and weren't
2579 : already cleaned up, for example during edge forwarding
2580 : and convert_jumps_to_returns. This will expose more
2581 : opportunities for fixing the partition boundaries here.
2582 : Also, the calculation of the dominance graph during verification
2583 : will assert if there are unreachable nodes. */
2584 382153 : delete_unreachable_blocks ();
2585 :
2586 : /* If there are partitions, do a sanity check on them: A basic block in
2587 : Â a cold partition cannot dominate a basic block in a hot partition.
2588 : Fixup any that now violate this requirement, as a result of edge
2589 : forwarding and unreachable block deletion. Â */
2590 382153 : auto_vec<basic_block> bbs_to_fix = find_partition_fixes (false);
2591 :
2592 : /* Do the partition fixup after all necessary blocks have been converted to
2593 : cold, so that we only update the region crossings the minimum number of
2594 : places, which can require forcing edges to be non fallthru. */
2595 382433 : if (! bbs_to_fix.is_empty ())
2596 : {
2597 319 : do
2598 : {
2599 319 : basic_block bb = bbs_to_fix.pop ();
2600 319 : fixup_new_cold_bb (bb);
2601 : }
2602 599 : while (! bbs_to_fix.is_empty ());
2603 :
2604 : /* Fix up hot cold block grouping if needed. */
2605 280 : if (crtl->bb_reorder_complete && current_ir_type () == IR_RTL_CFGRTL)
2606 : {
2607 0 : basic_block bb, first = NULL, second = NULL;
2608 0 : int current_partition = BB_UNPARTITIONED;
2609 :
2610 0 : FOR_EACH_BB_FN (bb, cfun)
2611 : {
2612 0 : if (current_partition != BB_UNPARTITIONED
2613 0 : && BB_PARTITION (bb) != current_partition)
2614 : {
2615 0 : if (first == NULL)
2616 : first = bb;
2617 0 : else if (second == NULL)
2618 : second = bb;
2619 : else
2620 : {
2621 : /* If we switch partitions for the 3rd, 5th etc. time,
2622 : move bbs first (inclusive) .. second (exclusive) right
2623 : before bb. */
2624 0 : basic_block prev_first = first->prev_bb;
2625 0 : basic_block prev_second = second->prev_bb;
2626 0 : basic_block prev_bb = bb->prev_bb;
2627 0 : prev_first->next_bb = second;
2628 0 : second->prev_bb = prev_first;
2629 0 : prev_second->next_bb = bb;
2630 0 : bb->prev_bb = prev_second;
2631 0 : prev_bb->next_bb = first;
2632 0 : first->prev_bb = prev_bb;
2633 0 : rtx_insn *prev_first_insn = PREV_INSN (BB_HEAD (first));
2634 0 : rtx_insn *prev_second_insn
2635 0 : = PREV_INSN (BB_HEAD (second));
2636 0 : rtx_insn *prev_bb_insn = PREV_INSN (BB_HEAD (bb));
2637 0 : SET_NEXT_INSN (prev_first_insn) = BB_HEAD (second);
2638 0 : SET_PREV_INSN (BB_HEAD (second)) = prev_first_insn;
2639 0 : SET_NEXT_INSN (prev_second_insn) = BB_HEAD (bb);
2640 0 : SET_PREV_INSN (BB_HEAD (bb)) = prev_second_insn;
2641 0 : SET_NEXT_INSN (prev_bb_insn) = BB_HEAD (first);
2642 0 : SET_PREV_INSN (BB_HEAD (first)) = prev_bb_insn;
2643 0 : second = NULL;
2644 : }
2645 : }
2646 0 : current_partition = BB_PARTITION (bb);
2647 : }
2648 0 : gcc_assert (!second);
2649 : }
2650 : }
2651 382153 : }
2652 :
2653 : /* Verify, in the basic block chain, that there is at most one switch
2654 : between hot/cold partitions. This condition will not be true until
2655 : after reorder_basic_blocks is called. */
2656 :
2657 : static bool
2658 63311248 : verify_hot_cold_block_grouping (void)
2659 : {
2660 63311248 : basic_block bb;
2661 63311248 : bool err = false;
2662 63311248 : bool switched_sections = false;
2663 63311248 : int current_partition = BB_UNPARTITIONED;
2664 :
2665 : /* Even after bb reordering is complete, we go into cfglayout mode
2666 : again (in compgoto). Ensure we don't call this before going back
2667 : into linearized RTL when any layout fixes would have been committed. */
2668 63311248 : if (!crtl->bb_reorder_complete
2669 63311248 : || current_ir_type () != IR_RTL_CFGRTL)
2670 : return err;
2671 :
2672 121548301 : FOR_EACH_BB_FN (bb, cfun)
2673 : {
2674 114726220 : if (current_partition != BB_UNPARTITIONED
2675 47734190 : && BB_PARTITION (bb) != current_partition)
2676 : {
2677 678746 : if (switched_sections)
2678 : {
2679 0 : error ("multiple hot/cold transitions found (bb %i)",
2680 : bb->index);
2681 0 : err = true;
2682 : }
2683 : else
2684 : switched_sections = true;
2685 :
2686 678746 : if (!crtl->has_bb_partition)
2687 0 : error ("partition found but function partition flag not set");
2688 : }
2689 114726220 : current_partition = BB_PARTITION (bb);
2690 : }
2691 :
2692 : return err;
2693 : }
2694 :
2695 :
2696 : /* Perform several checks on the edges out of each block, such as
2697 : the consistency of the branch probabilities, the correctness
2698 : of hot/cold partition crossing edges, and the number of expected
2699 : successor edges. Also verify that the dominance relationship
2700 : between hot/cold blocks is sane. */
2701 :
2702 : static bool
2703 100648343 : rtl_verify_edges (void)
2704 : {
2705 100648343 : bool err = false;
2706 100648343 : basic_block bb;
2707 100648343 : auto_bitmap succ_dests;
2708 100648343 : auto_bitmap table_dests;
2709 :
2710 1193288441 : FOR_EACH_BB_REVERSE_FN (bb, cfun)
2711 : {
2712 1092640098 : int n_fallthru = 0, n_branch = 0, n_abnormal_call = 0, n_sibcall = 0;
2713 1092640098 : int n_eh = 0, n_abnormal = 0;
2714 1092640098 : edge e, fallthru = NULL;
2715 1092640098 : edge_iterator ei;
2716 1092640098 : rtx note;
2717 1092640098 : bool has_crossing_edge = false;
2718 :
2719 1092640098 : if (JUMP_P (BB_END (bb))
2720 639221608 : && (note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX))
2721 441545600 : && EDGE_COUNT (bb->succs) >= 2
2722 1534185698 : && any_condjump_p (BB_END (bb)))
2723 : {
2724 441545600 : if (!BRANCH_EDGE (bb)->probability.initialized_p ())
2725 : {
2726 166674 : if (profile_status_for_fn (cfun) != PROFILE_ABSENT)
2727 : {
2728 0 : error ("verify_flow_info: "
2729 : "REG_BR_PROB is set but cfg probability is not");
2730 0 : err = true;
2731 : }
2732 : }
2733 441378926 : else if (XINT (note, 0)
2734 441378926 : != BRANCH_EDGE (bb)->probability.to_reg_br_prob_note ()
2735 441378926 : && profile_status_for_fn (cfun) != PROFILE_ABSENT)
2736 : {
2737 0 : error ("verify_flow_info: REG_BR_PROB does not match cfg %i %i",
2738 : XINT (note, 0),
2739 0 : BRANCH_EDGE (bb)->probability.to_reg_br_prob_note ());
2740 0 : err = true;
2741 : }
2742 : }
2743 :
2744 2684460395 : FOR_EACH_EDGE (e, ei, bb->succs)
2745 : {
2746 1591820297 : bool is_crossing;
2747 :
2748 1591820297 : if (e->flags & EDGE_FALLTHRU)
2749 870694550 : n_fallthru++, fallthru = e;
2750 :
2751 3183640594 : is_crossing = (BB_PARTITION (e->src) != BB_PARTITION (e->dest)
2752 36165281 : && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
2753 1627985578 : && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun));
2754 1591820297 : has_crossing_edge |= is_crossing;
2755 1591820297 : if (e->flags & EDGE_CROSSING)
2756 : {
2757 20288960 : if (!is_crossing)
2758 : {
2759 0 : error ("EDGE_CROSSING incorrectly set across same section");
2760 0 : err = true;
2761 : }
2762 20288960 : if (e->flags & EDGE_FALLTHRU)
2763 : {
2764 0 : error ("fallthru edge crosses section boundary in bb %i",
2765 0 : e->src->index);
2766 0 : err = true;
2767 : }
2768 20288960 : if (e->flags & EDGE_EH)
2769 : {
2770 0 : error ("EH edge crosses section boundary in bb %i",
2771 0 : e->src->index);
2772 0 : err = true;
2773 : }
2774 20288960 : if (JUMP_P (BB_END (bb)) && !CROSSING_JUMP_P (BB_END (bb)))
2775 : {
2776 0 : error ("No region crossing jump at section boundary in bb %i",
2777 : bb->index);
2778 0 : err = true;
2779 : }
2780 : }
2781 1571531337 : else if (is_crossing)
2782 : {
2783 0 : error ("EDGE_CROSSING missing across section boundary");
2784 0 : err = true;
2785 : }
2786 :
2787 1591820297 : if ((e->flags & ~(EDGE_DFS_BACK
2788 : | EDGE_CAN_FALLTHRU
2789 : | EDGE_IRREDUCIBLE_LOOP
2790 : | EDGE_LOOP_EXIT
2791 : | EDGE_CROSSING
2792 : | EDGE_PRESERVE)) == 0)
2793 646105515 : n_branch++;
2794 :
2795 1591820297 : if (e->flags & EDGE_ABNORMAL_CALL)
2796 53173411 : n_abnormal_call++;
2797 :
2798 1591820297 : if (e->flags & EDGE_SIBCALL)
2799 10530985 : n_sibcall++;
2800 :
2801 1591820297 : if (e->flags & EDGE_EH)
2802 62635210 : n_eh++;
2803 :
2804 1591820297 : if (e->flags & EDGE_ABNORMAL)
2805 74601638 : n_abnormal++;
2806 : }
2807 :
2808 1092640098 : if (!has_crossing_edge
2809 1072367483 : && JUMP_P (BB_END (bb))
2810 1711593395 : && CROSSING_JUMP_P (BB_END (bb)))
2811 : {
2812 0 : print_rtl_with_bb (stderr, get_insns (), TDF_BLOCKS | TDF_DETAILS);
2813 0 : error ("Region crossing jump across same section in bb %i",
2814 : bb->index);
2815 0 : err = true;
2816 : }
2817 :
2818 1092640098 : if (n_eh && !find_reg_note (BB_END (bb), REG_EH_REGION, NULL_RTX))
2819 : {
2820 0 : error ("missing REG_EH_REGION note at the end of bb %i", bb->index);
2821 0 : err = true;
2822 : }
2823 1092640098 : if (n_eh > 1)
2824 : {
2825 0 : error ("too many exception handling edges in bb %i", bb->index);
2826 0 : err = true;
2827 : }
2828 1092640098 : if (n_branch
2829 1092640098 : && (!JUMP_P (BB_END (bb))
2830 639116159 : || (n_branch > 1 && (any_uncondjump_p (BB_END (bb))
2831 723020 : || any_condjump_p (BB_END (bb))))))
2832 : {
2833 0 : error ("too many outgoing branch edges from bb %i", bb->index);
2834 0 : err = true;
2835 : }
2836 1092640098 : if (n_fallthru && any_uncondjump_p (BB_END (bb)))
2837 : {
2838 0 : error ("fallthru edge after unconditional jump in bb %i", bb->index);
2839 0 : err = true;
2840 : }
2841 :
2842 : /* Compare each shared dispatch's normal successors with its table
2843 : entries and optional out-of-range destination. */
2844 1092640098 : rtx_jump_table_data *table;
2845 1092640098 : if (shared_tablejump_p (BB_END (bb), NULL, &table))
2846 : {
2847 49368 : bitmap_clear (succ_dests);
2848 49368 : bitmap_clear (table_dests);
2849 619995 : FOR_EACH_EDGE (e, ei, bb->succs)
2850 : {
2851 570627 : bitmap_set_bit (succ_dests, e->dest->index);
2852 570627 : if (e->flags & (EDGE_ABNORMAL | EDGE_EH))
2853 0 : bitmap_set_bit (table_dests, e->dest->index);
2854 : }
2855 :
2856 1126291 : auto add_destination = [&] (rtx lab)
2857 : {
2858 1076923 : basic_block dest = (LABEL_P (lab)
2859 1076923 : ? BLOCK_FOR_INSN (as_a <rtx_insn *> (lab))
2860 1076923 : : NULL);
2861 1076923 : if (dest)
2862 1076923 : bitmap_set_bit (table_dests, dest->index);
2863 : else
2864 : {
2865 0 : error ("jump table of bb %i has an invalid destination",
2866 0 : bb->index);
2867 0 : err = true;
2868 : }
2869 1126291 : };
2870 49368 : rtvec vec = table->get_labels ();
2871 1126291 : for (int j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
2872 1076923 : add_destination (XEXP (RTVEC_ELT (vec, j), 0));
2873 :
2874 49368 : rtx casesi = tablejump_casesi_pattern (BB_END (bb));
2875 49368 : if (casesi)
2876 0 : add_destination (label_ref_label (XEXP (SET_SRC (casesi), 2)));
2877 :
2878 49368 : if (!bitmap_equal_p (succ_dests, table_dests))
2879 : {
2880 0 : error ("jump table of bb %i does not match its successors",
2881 : bb->index);
2882 0 : err = true;
2883 : }
2884 : }
2885 1092640098 : if (n_branch != 1 && any_uncondjump_p (BB_END (bb)))
2886 : {
2887 0 : error ("wrong number of branch edges after unconditional jump"
2888 : " in bb %i", bb->index);
2889 0 : err = true;
2890 : }
2891 454246959 : if (n_branch != 1 && any_condjump_p (BB_END (bb))
2892 1092641399 : && JUMP_LABEL (BB_END (bb)) != BB_HEAD (fallthru->dest))
2893 : {
2894 0 : error ("wrong amount of branch edges after conditional jump"
2895 : " in bb %i", bb->index);
2896 0 : err = true;
2897 : }
2898 1092640098 : if (n_abnormal_call && !CALL_P (BB_END (bb)))
2899 : {
2900 0 : error ("abnormal call edges for non-call insn in bb %i", bb->index);
2901 0 : err = true;
2902 : }
2903 1092640098 : if (n_sibcall && !CALL_P (BB_END (bb)))
2904 : {
2905 0 : error ("sibcall edges for non-call insn in bb %i", bb->index);
2906 0 : err = true;
2907 : }
2908 1092640098 : if (n_abnormal > n_eh
2909 10738259 : && !(CALL_P (BB_END (bb))
2910 10714685 : && n_abnormal == n_abnormal_call + n_sibcall)
2911 1092663672 : && (!JUMP_P (BB_END (bb))
2912 23574 : || any_condjump_p (BB_END (bb))
2913 23574 : || any_uncondjump_p (BB_END (bb))))
2914 : {
2915 0 : error ("abnormal edges for no purpose in bb %i", bb->index);
2916 0 : err = true;
2917 : }
2918 :
2919 1092640098 : int has_eh = -1;
2920 2678179242 : FOR_EACH_EDGE (e, ei, bb->preds)
2921 : {
2922 1585539144 : if (has_eh == -1)
2923 1092518658 : has_eh = (e->flags & EDGE_EH);
2924 1585539144 : if ((e->flags & EDGE_EH) == has_eh)
2925 1585539144 : continue;
2926 0 : error ("EH incoming edge mixed with non-EH incoming edges "
2927 : "in bb %i", bb->index);
2928 0 : err = true;
2929 0 : break;
2930 : }
2931 : }
2932 :
2933 : /* If there are partitions, do a sanity check on them: A basic block in
2934 : Â a cold partition cannot dominate a basic block in a hot partition. Â */
2935 2979830 : if (crtl->has_bb_partition && !err
2936 103628173 : && current_ir_type () == IR_RTL_CFGLAYOUT)
2937 : {
2938 126200 : auto_vec<basic_block> bbs_to_fix = find_partition_fixes (true);
2939 252400 : err = !bbs_to_fix.is_empty ();
2940 126200 : }
2941 :
2942 : /* Clean up. */
2943 100648343 : return err;
2944 100648343 : }
2945 :
2946 : /* Checks on the instructions within blocks. Currently checks that each
2947 : block starts with a basic block note, and that basic block notes and
2948 : control flow jumps are not found in the middle of the block. */
2949 :
2950 : static bool
2951 100648343 : rtl_verify_bb_insns (void)
2952 : {
2953 100648343 : rtx_insn *x;
2954 100648343 : bool err = false;
2955 100648343 : basic_block bb;
2956 :
2957 1193288441 : FOR_EACH_BB_REVERSE_FN (bb, cfun)
2958 : {
2959 : /* Now check the header of basic
2960 : block. It ought to contain optional CODE_LABEL followed
2961 : by NOTE_BASIC_BLOCK. */
2962 1092640098 : x = BB_HEAD (bb);
2963 1092640098 : if (LABEL_P (x))
2964 : {
2965 526537061 : if (BB_END (bb) == x)
2966 : {
2967 0 : error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
2968 : bb->index);
2969 0 : err = true;
2970 : }
2971 :
2972 526537061 : x = NEXT_INSN (x);
2973 : }
2974 :
2975 2185280196 : if (!NOTE_INSN_BASIC_BLOCK_P (x) || NOTE_BASIC_BLOCK (x) != bb)
2976 : {
2977 0 : error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
2978 : bb->index);
2979 0 : err = true;
2980 : }
2981 :
2982 1092640098 : if (BB_END (bb) == x)
2983 : /* Do checks for empty blocks here. */
2984 : ;
2985 : else
2986 12521497290 : for (x = NEXT_INSN (x); x; x = NEXT_INSN (x))
2987 : {
2988 11447894821 : if (NOTE_INSN_BASIC_BLOCK_P (x))
2989 : {
2990 0 : error ("NOTE_INSN_BASIC_BLOCK %d in middle of basic block %d",
2991 0 : INSN_UID (x), bb->index);
2992 0 : err = true;
2993 : }
2994 :
2995 11447894821 : if (x == BB_END (bb))
2996 : break;
2997 :
2998 10374292352 : if (control_flow_insn_p (x))
2999 : {
3000 0 : error ("in basic block %d:", bb->index);
3001 0 : fatal_insn ("flow control insn inside a basic block", x);
3002 : }
3003 : }
3004 : }
3005 :
3006 : /* Clean up. */
3007 100648343 : return err;
3008 : }
3009 :
3010 : /* Verify that block pointers for instructions in basic blocks, headers and
3011 : footers are set appropriately. */
3012 :
3013 : static bool
3014 100648343 : rtl_verify_bb_pointers (void)
3015 : {
3016 100648343 : bool err = false;
3017 100648343 : basic_block bb;
3018 :
3019 : /* Check the general integrity of the basic blocks. */
3020 1193288441 : FOR_EACH_BB_REVERSE_FN (bb, cfun)
3021 : {
3022 1092640098 : rtx_insn *insn;
3023 :
3024 1092640098 : if (!(bb->flags & BB_RTL))
3025 : {
3026 0 : error ("BB_RTL flag not set for block %d", bb->index);
3027 0 : err = true;
3028 : }
3029 :
3030 14159712078 : FOR_BB_INSNS (bb, insn)
3031 13067071980 : if (BLOCK_FOR_INSN (insn) != bb)
3032 : {
3033 0 : error ("insn %d basic block pointer is %d, should be %d",
3034 0 : INSN_UID (insn),
3035 0 : BLOCK_FOR_INSN (insn) ? BLOCK_FOR_INSN (insn)->index : 0,
3036 : bb->index);
3037 0 : err = true;
3038 : }
3039 :
3040 1093249813 : for (insn = BB_HEADER (bb); insn; insn = NEXT_INSN (insn))
3041 609715 : if (!BARRIER_P (insn)
3042 609715 : && BLOCK_FOR_INSN (insn) != NULL)
3043 : {
3044 0 : error ("insn %d in header of bb %d has non-NULL basic block",
3045 0 : INSN_UID (insn), bb->index);
3046 0 : err = true;
3047 : }
3048 1122417267 : for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
3049 29777169 : if (!BARRIER_P (insn)
3050 29777169 : && BLOCK_FOR_INSN (insn) != NULL)
3051 : {
3052 0 : error ("insn %d in footer of bb %d has non-NULL basic block",
3053 0 : INSN_UID (insn), bb->index);
3054 0 : err = true;
3055 : }
3056 : }
3057 :
3058 : /* Clean up. */
3059 100648343 : return err;
3060 : }
3061 :
3062 : /* Verify the CFG and RTL consistency common for both underlying RTL and
3063 : cfglayout RTL.
3064 :
3065 : Currently it does following checks:
3066 :
3067 : - overlapping of basic blocks
3068 : - insns with wrong BLOCK_FOR_INSN pointers
3069 : - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note)
3070 : - tails of basic blocks (ensure that boundary is necessary)
3071 : - scans body of the basic block for JUMP_INSN, CODE_LABEL
3072 : and NOTE_INSN_BASIC_BLOCK
3073 : - verify that no fall_thru edge crosses hot/cold partition boundaries
3074 : - verify that there are no pending RTL branch predictions
3075 : - verify that hot blocks are not dominated by cold blocks
3076 :
3077 : In future it can be extended check a lot of other stuff as well
3078 : (reachability of basic blocks, life information, etc. etc.). */
3079 :
3080 : static bool
3081 100648343 : rtl_verify_flow_info_1 (void)
3082 : {
3083 100648343 : bool err = false;
3084 :
3085 100648343 : if (rtl_verify_bb_pointers ())
3086 : err = true;
3087 :
3088 100648343 : if (rtl_verify_bb_insns ())
3089 0 : err = true;
3090 :
3091 100648343 : if (rtl_verify_edges ())
3092 0 : err = true;
3093 :
3094 100648343 : return err;
3095 : }
3096 :
3097 : /* Walk the instruction chain and verify that bb head/end pointers
3098 : are correct, and that instructions are in exactly one bb and have
3099 : correct block pointers. */
3100 :
3101 : static bool
3102 63311248 : rtl_verify_bb_insn_chain (void)
3103 : {
3104 63311248 : basic_block bb;
3105 63311248 : bool err = false;
3106 63311248 : rtx_insn *x;
3107 63311248 : rtx_insn *last_head = get_last_insn ();
3108 63311248 : basic_block *bb_info;
3109 63311248 : const int max_uid = get_max_uid ();
3110 :
3111 63311248 : bb_info = XCNEWVEC (basic_block, max_uid);
3112 :
3113 727116162 : FOR_EACH_BB_REVERSE_FN (bb, cfun)
3114 : {
3115 663804914 : rtx_insn *head = BB_HEAD (bb);
3116 663804914 : rtx_insn *end = BB_END (bb);
3117 :
3118 923639273 : for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
3119 : {
3120 : /* Verify the end of the basic block is in the INSN chain. */
3121 923639273 : if (x == end)
3122 : break;
3123 :
3124 : /* And that the code outside of basic blocks has NULL bb field. */
3125 259834359 : if (!BARRIER_P (x)
3126 259834359 : && BLOCK_FOR_INSN (x) != NULL)
3127 : {
3128 0 : error ("insn %d outside of basic blocks has non-NULL bb field",
3129 0 : INSN_UID (x));
3130 0 : err = true;
3131 : }
3132 : }
3133 :
3134 663804914 : if (!x)
3135 : {
3136 0 : error ("end insn %d for block %d not found in the insn stream",
3137 0 : INSN_UID (end), bb->index);
3138 0 : err = true;
3139 : }
3140 :
3141 : /* Work backwards from the end to the head of the basic block
3142 : to verify the head is in the RTL chain. */
3143 7899537637 : for (; x != NULL_RTX; x = PREV_INSN (x))
3144 : {
3145 : /* While walking over the insn chain, verify insns appear
3146 : in only one basic block. */
3147 7899537637 : if (bb_info[INSN_UID (x)] != NULL)
3148 : {
3149 0 : error ("insn %d is in multiple basic blocks (%d and %d)",
3150 : INSN_UID (x), bb->index, bb_info[INSN_UID (x)]->index);
3151 0 : err = true;
3152 : }
3153 :
3154 7899537637 : bb_info[INSN_UID (x)] = bb;
3155 :
3156 7899537637 : if (x == head)
3157 : break;
3158 : }
3159 663804914 : if (!x)
3160 : {
3161 0 : error ("head insn %d for block %d not found in the insn stream",
3162 0 : INSN_UID (head), bb->index);
3163 0 : err = true;
3164 : }
3165 :
3166 663804914 : last_head = PREV_INSN (x);
3167 : }
3168 :
3169 127650261 : for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
3170 : {
3171 : /* Check that the code before the first basic block has NULL
3172 : bb field. */
3173 64339013 : if (!BARRIER_P (x)
3174 64339013 : && BLOCK_FOR_INSN (x) != NULL)
3175 : {
3176 0 : error ("insn %d outside of basic blocks has non-NULL bb field",
3177 0 : INSN_UID (x));
3178 0 : err = true;
3179 : }
3180 : }
3181 63311248 : free (bb_info);
3182 :
3183 63311248 : return err;
3184 : }
3185 :
3186 : /* Verify that fallthru edges point to adjacent blocks in layout order and
3187 : that barriers exist after non-fallthru blocks. */
3188 :
3189 : static bool
3190 63311248 : rtl_verify_fallthru (void)
3191 : {
3192 63311248 : basic_block bb;
3193 63311248 : bool err = false;
3194 :
3195 727116162 : FOR_EACH_BB_REVERSE_FN (bb, cfun)
3196 : {
3197 663804914 : edge e;
3198 :
3199 663804914 : e = find_fallthru_edge (bb->succs);
3200 663804914 : if (!e)
3201 : {
3202 193033522 : rtx_insn *insn;
3203 :
3204 : /* Ensure existence of barrier in BB with no fallthru edges. */
3205 193596340 : for (insn = NEXT_INSN (BB_END (bb)); ; insn = NEXT_INSN (insn))
3206 : {
3207 193596340 : if (!insn || NOTE_INSN_BASIC_BLOCK_P (insn))
3208 : {
3209 0 : error ("missing barrier after block %i", bb->index);
3210 0 : err = true;
3211 0 : break;
3212 : }
3213 193596340 : if (BARRIER_P (insn))
3214 : break;
3215 : }
3216 : }
3217 470771392 : else if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
3218 470771392 : && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
3219 : {
3220 439564627 : rtx_insn *insn;
3221 :
3222 439564627 : if (e->src->next_bb != e->dest)
3223 : {
3224 0 : error
3225 0 : ("verify_flow_info: Incorrect blocks for fallthru %i->%i",
3226 : e->src->index, e->dest->index);
3227 0 : err = true;
3228 : }
3229 : else
3230 885917398 : for (insn = NEXT_INSN (BB_END (e->src)); insn != BB_HEAD (e->dest);
3231 6788144 : insn = NEXT_INSN (insn))
3232 6788144 : if (BARRIER_P (insn) || NONDEBUG_INSN_P (insn))
3233 : {
3234 0 : error ("verify_flow_info: Incorrect fallthru %i->%i",
3235 0 : e->src->index, e->dest->index);
3236 0 : error ("wrong insn in the fallthru edge");
3237 0 : debug_rtx (insn);
3238 0 : err = true;
3239 : }
3240 : }
3241 : }
3242 :
3243 63311248 : return err;
3244 : }
3245 :
3246 : /* Verify that blocks are laid out in consecutive order. While walking the
3247 : instructions, verify that all expected instructions are inside the basic
3248 : blocks, and that all returns are followed by barriers. */
3249 :
3250 : static bool
3251 63311248 : rtl_verify_bb_layout (void)
3252 : {
3253 63311248 : basic_block bb;
3254 63311248 : bool err = false;
3255 63311248 : rtx_insn *x, *y;
3256 63311248 : int num_bb_notes;
3257 63311248 : rtx_insn * const rtx_first = get_insns ();
3258 63311248 : basic_block last_bb_seen = ENTRY_BLOCK_PTR_FOR_FN (cfun), curr_bb = NULL;
3259 :
3260 63311248 : num_bb_notes = 0;
3261 :
3262 8286633717 : for (x = rtx_first; x; x = NEXT_INSN (x))
3263 : {
3264 8223322469 : if (NOTE_INSN_BASIC_BLOCK_P (x))
3265 : {
3266 663804914 : bb = NOTE_BASIC_BLOCK (x);
3267 :
3268 663804914 : num_bb_notes++;
3269 663804914 : if (bb != last_bb_seen->next_bb)
3270 0 : internal_error ("basic blocks not laid down consecutively");
3271 :
3272 : curr_bb = last_bb_seen = bb;
3273 : }
3274 :
3275 8223322469 : if (!curr_bb)
3276 : {
3277 637600597 : switch (GET_CODE (x))
3278 : {
3279 : case BARRIER:
3280 : case NOTE:
3281 : break;
3282 :
3283 314204305 : case CODE_LABEL:
3284 : /* An ADDR_VEC is placed outside any basic block. */
3285 314204305 : if (NEXT_INSN (x)
3286 314204305 : && JUMP_TABLE_DATA_P (NEXT_INSN (x)))
3287 : x = NEXT_INSN (x);
3288 :
3289 : /* But in any case, non-deletable labels can appear anywhere. */
3290 : break;
3291 :
3292 0 : default:
3293 0 : fatal_insn ("insn outside basic block", x);
3294 : }
3295 : }
3296 :
3297 8223322469 : if (JUMP_P (x)
3298 442301465 : && returnjump_p (x) && ! condjump_p (x)
3299 8254017435 : && ! ((y = next_nonnote_nondebug_insn (x))
3300 30694966 : && BARRIER_P (y)))
3301 0 : fatal_insn ("return not followed by barrier", x);
3302 :
3303 8223322469 : if (curr_bb && x == BB_END (curr_bb))
3304 1301405511 : curr_bb = NULL;
3305 : }
3306 :
3307 63311248 : if (num_bb_notes != n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS)
3308 0 : internal_error
3309 0 : ("number of bb notes in insn chain (%d) != n_basic_blocks (%d)",
3310 : num_bb_notes, n_basic_blocks_for_fn (cfun));
3311 :
3312 63311248 : return err;
3313 : }
3314 :
3315 : /* Verify the CFG and RTL consistency common for both underlying RTL and
3316 : cfglayout RTL, plus consistency checks specific to linearized RTL mode.
3317 :
3318 : Currently it does following checks:
3319 : - all checks of rtl_verify_flow_info_1
3320 : - test head/end pointers
3321 : - check that blocks are laid out in consecutive order
3322 : - check that all insns are in the basic blocks
3323 : (except the switch handling code, barriers and notes)
3324 : - check that all returns are followed by barriers
3325 : - check that all fallthru edge points to the adjacent blocks
3326 : - verify that there is a single hot/cold partition boundary after bbro */
3327 :
3328 : static bool
3329 63311248 : rtl_verify_flow_info (void)
3330 : {
3331 63311248 : bool err = false;
3332 :
3333 63311248 : if (rtl_verify_flow_info_1 ())
3334 : err = true;
3335 :
3336 63311248 : if (rtl_verify_bb_insn_chain ())
3337 0 : err = true;
3338 :
3339 63311248 : if (rtl_verify_fallthru ())
3340 0 : err = true;
3341 :
3342 63311248 : if (rtl_verify_bb_layout ())
3343 : err = true;
3344 :
3345 63311248 : if (verify_hot_cold_block_grouping ())
3346 0 : err = true;
3347 :
3348 63311248 : return err;
3349 : }
3350 :
3351 : /* Assume that the preceding pass has possibly eliminated jump instructions
3352 : or converted the unconditional jumps. Eliminate the edges from CFG.
3353 : Return true if any edges are eliminated. */
3354 :
3355 : bool
3356 76104216 : purge_dead_edges (basic_block bb)
3357 : {
3358 76104216 : edge e;
3359 76104216 : rtx_insn *insn = BB_END (bb);
3360 76104216 : rtx note;
3361 76104216 : bool purged = false;
3362 76104216 : bool found;
3363 76104216 : edge_iterator ei;
3364 :
3365 76104216 : if ((DEBUG_INSN_P (insn) || NOTE_P (insn)) && insn != BB_HEAD (bb))
3366 17151344 : do
3367 17151344 : insn = PREV_INSN (insn);
3368 17151344 : while ((DEBUG_INSN_P (insn) || NOTE_P (insn)) && insn != BB_HEAD (bb));
3369 :
3370 : /* If this instruction cannot trap, remove REG_EH_REGION notes. */
3371 76104216 : if (NONJUMP_INSN_P (insn)
3372 76104216 : && (note = find_reg_note (insn, REG_EH_REGION, NULL)))
3373 : {
3374 1363055 : rtx eqnote;
3375 :
3376 1363055 : if (! may_trap_p (PATTERN (insn))
3377 1363055 : || ((eqnote = find_reg_equal_equiv_note (insn))
3378 198 : && ! may_trap_p (XEXP (eqnote, 0))))
3379 43924 : remove_note (insn, note);
3380 : }
3381 : /* A tail call cannot trap either. The tailc/musttail pass could have
3382 : allowed a tail call if it could throw internally, but perform no
3383 : actual statements and then caused the exception to be thrown externally
3384 : in the hope that it is cleaned up later. If it is not, just
3385 : remove REG_EH_REGION note. While the call maybe can throw, the
3386 : current function's frame will not be there anymore when it does. */
3387 76104216 : auto call = dyn_cast<rtx_call_insn *> (insn);
3388 9513588 : if (call
3389 9513588 : && SIBLING_CALL_P (call)
3390 522653 : && (note = find_reg_note (call, REG_EH_REGION, NULL)))
3391 83645 : remove_note (call, note);
3392 :
3393 : /* Cleanup abnormal edges caused by exceptions or non-local gotos. */
3394 182760642 : for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3395 : {
3396 106656426 : bool remove = false;
3397 :
3398 : /* There are three types of edges we need to handle correctly here: EH
3399 : edges, abnormal call EH edges, and abnormal call non-EH edges. The
3400 : latter can appear when nonlocal gotos are used. */
3401 106656426 : if (e->flags & EDGE_ABNORMAL_CALL)
3402 : {
3403 3497841 : if (!CALL_P (insn))
3404 : remove = true;
3405 3497836 : else if (can_nonlocal_goto (insn))
3406 : ;
3407 3421693 : else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
3408 : ;
3409 0 : else if (flag_tm && find_reg_note (insn, REG_TM, NULL))
3410 : ;
3411 : else
3412 : remove = true;
3413 : }
3414 103158585 : else if (e->flags & EDGE_EH)
3415 1985407 : remove = !can_throw_internal (insn);
3416 :
3417 1985407 : if (remove)
3418 : {
3419 402101 : remove_edge (e);
3420 402101 : df_set_bb_dirty (bb);
3421 402101 : purged = true;
3422 : }
3423 : else
3424 106254325 : ei_next (&ei);
3425 : }
3426 :
3427 76104216 : if (JUMP_P (insn))
3428 : {
3429 41081980 : rtx note;
3430 41081980 : edge b,f;
3431 41081980 : edge_iterator ei;
3432 :
3433 : /* We do care only about conditional jumps and simplejumps. */
3434 41081980 : if (!any_condjump_p (insn)
3435 13267368 : && !returnjump_p (insn)
3436 50639621 : && !simplejump_p (insn))
3437 : return purged;
3438 :
3439 : /* Branch probability/prediction notes are defined only for
3440 : condjumps. We've possibly turned condjump into simplejump. */
3441 41039228 : if (simplejump_p (insn))
3442 : {
3443 9514889 : note = find_reg_note (insn, REG_BR_PROB, NULL);
3444 9514889 : if (note)
3445 3704 : remove_note (insn, note);
3446 9514889 : while ((note = find_reg_note (insn, REG_BR_PRED, NULL)))
3447 0 : remove_note (insn, note);
3448 : }
3449 :
3450 109904654 : for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3451 : {
3452 : /* Avoid abnormal flags to leak from computed jumps turned
3453 : into simplejumps. */
3454 :
3455 68865426 : e->flags &= ~EDGE_ABNORMAL;
3456 :
3457 : /* See if this edge is one we should keep. */
3458 68865426 : if ((e->flags & EDGE_FALLTHRU) && any_condjump_p (insn))
3459 : /* A conditional jump can fall through into the next
3460 : block, so we should keep the edge. */
3461 : {
3462 27814612 : ei_next (&ei);
3463 27814612 : continue;
3464 : }
3465 41050814 : else if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
3466 37341087 : && BB_HEAD (e->dest) == JUMP_LABEL (insn))
3467 : /* If the destination block is the target of the jump,
3468 : keep the edge. */
3469 : {
3470 37327162 : ei_next (&ei);
3471 37327162 : continue;
3472 : }
3473 7433379 : else if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
3474 3723652 : && returnjump_p (insn))
3475 : /* If the destination block is the exit block, and this
3476 : instruction is a return, then keep the edge. */
3477 : {
3478 3709727 : ei_next (&ei);
3479 3709727 : continue;
3480 : }
3481 13925 : else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
3482 : /* Keep the edges that correspond to exceptions thrown by
3483 : this instruction and rematerialize the EDGE_ABNORMAL
3484 : flag we just cleared above. */
3485 : {
3486 0 : e->flags |= EDGE_ABNORMAL;
3487 0 : ei_next (&ei);
3488 0 : continue;
3489 : }
3490 :
3491 : /* We do not need this edge. */
3492 13925 : df_set_bb_dirty (bb);
3493 13925 : purged = true;
3494 13925 : remove_edge (e);
3495 : }
3496 :
3497 41233205 : if (EDGE_COUNT (bb->succs) == 0 || !purged)
3498 : return purged;
3499 :
3500 151225 : if (dump_file)
3501 0 : fprintf (dump_file, "Purged edges from bb %i\n", bb->index);
3502 :
3503 151225 : if (!optimize)
3504 : return purged;
3505 :
3506 : /* Redistribute probabilities. */
3507 150617 : if (single_succ_p (bb))
3508 : {
3509 150617 : single_succ_edge (bb)->probability = profile_probability::always ();
3510 : }
3511 : else
3512 : {
3513 0 : note = find_reg_note (insn, REG_BR_PROB, NULL);
3514 0 : if (!note)
3515 : return purged;
3516 :
3517 0 : b = BRANCH_EDGE (bb);
3518 0 : f = FALLTHRU_EDGE (bb);
3519 0 : b->probability = profile_probability::from_reg_br_prob_note
3520 0 : (XINT (note, 0));
3521 0 : f->probability = b->probability.invert ();
3522 : }
3523 :
3524 : return purged;
3525 : }
3526 35022236 : else if (CALL_P (insn) && SIBLING_CALL_P (insn))
3527 : {
3528 : /* First, there should not be any EH or ABCALL edges resulting
3529 : from non-local gotos and the like. If there were, we shouldn't
3530 : have created the sibcall in the first place. Second, there
3531 : should of course never have been a fallthru edge. */
3532 522653 : gcc_assert (single_succ_p (bb));
3533 522653 : gcc_assert (single_succ_edge (bb)->flags
3534 : == (EDGE_SIBCALL | EDGE_ABNORMAL));
3535 :
3536 : return false;
3537 : }
3538 :
3539 : /* If we don't see a jump insn, we don't know exactly why the block would
3540 : have been broken at this point. Look for a simple, non-fallthru edge,
3541 : as these are only created by conditional branches. If we find such an
3542 : edge we know that there used to be a jump here and can then safely
3543 : remove all non-fallthru edges. */
3544 34499583 : found = false;
3545 70543168 : FOR_EACH_EDGE (e, ei, bb->succs)
3546 36341572 : if (! (e->flags & (EDGE_COMPLEX | EDGE_FALLTHRU)))
3547 : {
3548 : found = true;
3549 : break;
3550 : }
3551 :
3552 34499583 : if (!found)
3553 : return purged;
3554 :
3555 : /* Remove all but the fake and fallthru edges. The fake edge may be
3556 : the only successor for this block in the case of noreturn
3557 : calls. */
3558 894093 : for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3559 : {
3560 596106 : if (!(e->flags & (EDGE_FALLTHRU | EDGE_FAKE)))
3561 : {
3562 298119 : df_set_bb_dirty (bb);
3563 298119 : remove_edge (e);
3564 298119 : purged = true;
3565 : }
3566 : else
3567 297987 : ei_next (&ei);
3568 : }
3569 :
3570 297987 : gcc_assert (single_succ_p (bb));
3571 :
3572 297987 : single_succ_edge (bb)->probability = profile_probability::always ();
3573 :
3574 297987 : if (dump_file)
3575 1 : fprintf (dump_file, "Purged non-fallthru edges from bb %i\n",
3576 : bb->index);
3577 : return purged;
3578 : }
3579 :
3580 : /* Search all basic blocks for potentially dead edges and purge them. Return
3581 : true if some edge has been eliminated. */
3582 :
3583 : bool
3584 3444376 : purge_all_dead_edges (void)
3585 : {
3586 3444376 : bool purged = false;
3587 3444376 : basic_block bb;
3588 :
3589 50533646 : FOR_EACH_BB_FN (bb, cfun)
3590 47089270 : if (purge_dead_edges (bb))
3591 2459 : purged = true;
3592 :
3593 3444376 : return purged;
3594 : }
3595 :
3596 : /* This is used by a few passes that emit some instructions after abnormal
3597 : calls, moving the basic block's end, while they in fact do want to emit
3598 : them on the fallthru edge. Look for abnormal call edges, find backward
3599 : the call in the block and insert the instructions on the edge instead.
3600 :
3601 : Similarly, handle instructions throwing exceptions internally.
3602 :
3603 : Return true when instructions have been found and inserted on edges. */
3604 :
3605 : bool
3606 1566386 : fixup_abnormal_edges (void)
3607 : {
3608 1566386 : bool inserted = false;
3609 1566386 : basic_block bb;
3610 :
3611 17607405 : FOR_EACH_BB_FN (bb, cfun)
3612 : {
3613 16041019 : edge e;
3614 16041019 : edge_iterator ei;
3615 :
3616 : /* Look for cases we are interested in - calls or instructions causing
3617 : exceptions. */
3618 37752990 : FOR_EACH_EDGE (e, ei, bb->succs)
3619 22434603 : if ((e->flags & EDGE_ABNORMAL_CALL)
3620 21822646 : || ((e->flags & (EDGE_ABNORMAL | EDGE_EH))
3621 : == (EDGE_ABNORMAL | EDGE_EH)))
3622 : break;
3623 :
3624 16041019 : if (e && !CALL_P (BB_END (bb)) && !can_throw_internal (BB_END (bb)))
3625 : {
3626 9745 : rtx_insn *insn;
3627 :
3628 : /* Get past the new insns generated. Allow notes, as the insns
3629 : may be already deleted. */
3630 9745 : insn = BB_END (bb);
3631 16683 : while ((NONJUMP_INSN_P (insn) || NOTE_P (insn))
3632 15617 : && !can_throw_internal (insn)
3633 29945 : && insn != BB_HEAD (bb))
3634 8515 : insn = PREV_INSN (insn);
3635 :
3636 9745 : if (CALL_P (insn) || can_throw_internal (insn))
3637 : {
3638 6570 : rtx_insn *stop, *next;
3639 :
3640 6570 : e = find_fallthru_edge (bb->succs);
3641 :
3642 6570 : stop = NEXT_INSN (BB_END (bb));
3643 6570 : BB_END (bb) = insn;
3644 :
3645 21480 : for (insn = NEXT_INSN (insn); insn != stop; insn = next)
3646 : {
3647 8340 : next = NEXT_INSN (insn);
3648 8340 : if (INSN_P (insn))
3649 : {
3650 7255 : delete_insn (insn);
3651 :
3652 : /* Sometimes there's still the return value USE.
3653 : If it's placed after a trapping call (i.e. that
3654 : call is the last insn anyway), we have no fallthru
3655 : edge. Simply delete this use and don't try to insert
3656 : on the non-existent edge.
3657 : Similarly, sometimes a call that can throw is
3658 : followed in the source with __builtin_unreachable (),
3659 : meaning that there is UB if the call returns rather
3660 : than throws. If there weren't any instructions
3661 : following such calls before, supposedly even the ones
3662 : we've deleted aren't significant and can be
3663 : removed. */
3664 7255 : if (e)
3665 : {
3666 : /* We're not deleting it, we're moving it. */
3667 7254 : insn->set_undeleted ();
3668 7254 : SET_PREV_INSN (insn) = NULL_RTX;
3669 7254 : SET_NEXT_INSN (insn) = NULL_RTX;
3670 :
3671 7254 : insert_insn_on_edge (insn, e);
3672 7254 : inserted = true;
3673 : }
3674 : }
3675 1085 : else if (!BARRIER_P (insn))
3676 1085 : set_block_for_insn (insn, NULL);
3677 : }
3678 : }
3679 :
3680 : /* It may be that we don't find any trapping insn. In this
3681 : case we discovered quite late that the insn that had been
3682 : marked as can_throw_internal in fact couldn't trap at all.
3683 : So we should in fact delete the EH edges out of the block. */
3684 : else
3685 3175 : purge_dead_edges (bb);
3686 : }
3687 : }
3688 :
3689 1566386 : return inserted;
3690 : }
3691 :
3692 : /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
3693 : Note that the INSN should be deleted *after* removing dead edges, so
3694 : that the kept edge is the fallthrough edge for a (set (pc) (pc))
3695 : but not for a (set (pc) (label_ref FOO)). */
3696 :
3697 : void
3698 4288 : update_cfg_for_uncondjump (rtx_insn *insn)
3699 : {
3700 4288 : basic_block bb = BLOCK_FOR_INSN (insn);
3701 4288 : gcc_assert (BB_END (bb) == insn);
3702 :
3703 4288 : purge_dead_edges (bb);
3704 :
3705 4288 : if (current_ir_type () != IR_RTL_CFGLAYOUT)
3706 : {
3707 20 : if (!find_fallthru_edge (bb->succs))
3708 : {
3709 20 : auto barrier = next_nonnote_nondebug_insn (insn);
3710 20 : if (!barrier || !BARRIER_P (barrier))
3711 20 : emit_barrier_after (insn);
3712 : }
3713 : return;
3714 : }
3715 :
3716 4268 : delete_insn (insn);
3717 4268 : if (EDGE_COUNT (bb->succs) == 1)
3718 : {
3719 2117 : rtx_insn *insn;
3720 :
3721 2117 : single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
3722 :
3723 : /* Remove barriers from the footer if there are any. */
3724 2122 : for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
3725 5 : if (BARRIER_P (insn))
3726 : {
3727 0 : if (PREV_INSN (insn))
3728 0 : SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
3729 : else
3730 0 : BB_FOOTER (bb) = NEXT_INSN (insn);
3731 0 : if (NEXT_INSN (insn))
3732 0 : SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
3733 : }
3734 5 : else if (LABEL_P (insn))
3735 : break;
3736 : }
3737 : }
3738 :
3739 : /* Cut the insns from FIRST to LAST out of the insns stream. */
3740 :
3741 : rtx_insn *
3742 12184139 : unlink_insn_chain (rtx_insn *first, rtx_insn *last)
3743 : {
3744 12184139 : rtx_insn *prevfirst = PREV_INSN (first);
3745 12184139 : rtx_insn *nextlast = NEXT_INSN (last);
3746 :
3747 12184139 : SET_PREV_INSN (first) = NULL;
3748 12184139 : SET_NEXT_INSN (last) = NULL;
3749 12184139 : if (prevfirst)
3750 9582522 : SET_NEXT_INSN (prevfirst) = nextlast;
3751 12184139 : if (nextlast)
3752 10701130 : SET_PREV_INSN (nextlast) = prevfirst;
3753 : else
3754 1483009 : set_last_insn (prevfirst);
3755 12184139 : if (!prevfirst)
3756 2601617 : set_first_insn (nextlast);
3757 12184139 : return first;
3758 : }
3759 :
3760 : /* Skip over inter-block insns occurring after BB which are typically
3761 : associated with BB (e.g., barriers). If there are any such insns,
3762 : we return the last one. Otherwise, we return the end of BB. */
3763 :
3764 : static rtx_insn *
3765 27236703 : skip_insns_after_block (basic_block bb)
3766 : {
3767 27236703 : rtx_insn *insn, *last_insn, *next_head, *prev;
3768 :
3769 27236703 : next_head = NULL;
3770 27236703 : if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3771 24635057 : next_head = BB_HEAD (bb->next_bb);
3772 :
3773 36266832 : for (last_insn = insn = BB_END (bb); (insn = NEXT_INSN (insn)) != 0; )
3774 : {
3775 33665186 : if (insn == next_head)
3776 : break;
3777 :
3778 9030129 : switch (GET_CODE (insn))
3779 : {
3780 7907753 : case BARRIER:
3781 7907753 : last_insn = insn;
3782 7907753 : continue;
3783 :
3784 1107277 : case NOTE:
3785 1107277 : gcc_assert (NOTE_KIND (insn) != NOTE_INSN_BLOCK_END);
3786 1107277 : continue;
3787 :
3788 15099 : case CODE_LABEL:
3789 15099 : if (NEXT_INSN (insn)
3790 15099 : && JUMP_TABLE_DATA_P (NEXT_INSN (insn)))
3791 : {
3792 15099 : insn = NEXT_INSN (insn);
3793 15099 : last_insn = insn;
3794 15099 : continue;
3795 : }
3796 : break;
3797 :
3798 : default:
3799 : break;
3800 : }
3801 :
3802 : break;
3803 : }
3804 :
3805 : /* It is possible to hit contradictory sequence. For instance:
3806 :
3807 : jump_insn
3808 : NOTE_INSN_BLOCK_BEG
3809 : barrier
3810 :
3811 : Where barrier belongs to jump_insn, but the note does not. This can be
3812 : created by removing the basic block originally following
3813 : NOTE_INSN_BLOCK_BEG. In such case reorder the notes. */
3814 :
3815 35175710 : for (insn = last_insn; insn != BB_END (bb); insn = prev)
3816 : {
3817 7939007 : prev = PREV_INSN (insn);
3818 7939007 : if (NOTE_P (insn))
3819 1056 : switch (NOTE_KIND (insn))
3820 : {
3821 0 : case NOTE_INSN_BLOCK_END:
3822 0 : gcc_unreachable ();
3823 1056 : break;
3824 1056 : case NOTE_INSN_DELETED:
3825 1056 : case NOTE_INSN_DELETED_LABEL:
3826 1056 : case NOTE_INSN_DELETED_DEBUG_LABEL:
3827 1056 : continue;
3828 0 : default:
3829 0 : reorder_insns (insn, insn, last_insn);
3830 : }
3831 : }
3832 :
3833 27236703 : return last_insn;
3834 : }
3835 :
3836 : /* Locate or create a label for a given basic block. */
3837 :
3838 : static rtx_insn *
3839 2294166 : label_for_bb (basic_block bb)
3840 : {
3841 2294166 : rtx_insn *label = BB_HEAD (bb);
3842 :
3843 2294166 : if (!LABEL_P (label))
3844 : {
3845 1449330 : if (dump_file)
3846 8 : fprintf (dump_file, "Emitting label for block %d\n", bb->index);
3847 :
3848 1449330 : label = block_label (bb);
3849 : }
3850 :
3851 2294166 : return label;
3852 : }
3853 :
3854 : /* Locate the effective beginning and end of the insn chain for each
3855 : block, as defined by skip_insns_after_block above. */
3856 :
3857 : static void
3858 2601646 : record_effective_endpoints (void)
3859 : {
3860 2601646 : rtx_insn *next_insn;
3861 2601646 : basic_block bb;
3862 2601646 : rtx_insn *insn;
3863 :
3864 2601646 : for (insn = get_insns ();
3865 : insn
3866 5205908 : && NOTE_P (insn)
3867 10410693 : && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK;
3868 2604262 : insn = NEXT_INSN (insn))
3869 2604262 : continue;
3870 : /* No basic blocks at all? */
3871 2601646 : gcc_assert (insn);
3872 :
3873 2601646 : if (PREV_INSN (insn))
3874 2601617 : cfg_layout_function_header =
3875 2601617 : unlink_insn_chain (get_insns (), PREV_INSN (insn));
3876 : else
3877 : cfg_layout_function_header = NULL;
3878 :
3879 2601646 : next_insn = get_insns ();
3880 29838349 : FOR_EACH_BB_FN (bb, cfun)
3881 : {
3882 27236703 : rtx_insn *end;
3883 :
3884 27236703 : if (PREV_INSN (BB_HEAD (bb)) && next_insn != BB_HEAD (bb))
3885 47083 : BB_HEADER (bb) = unlink_insn_chain (next_insn,
3886 47083 : PREV_INSN (BB_HEAD (bb)));
3887 27236703 : end = skip_insns_after_block (bb);
3888 27236703 : if (NEXT_INSN (BB_END (bb)) && BB_END (bb) != end)
3889 7827485 : BB_FOOTER (bb) = unlink_insn_chain (NEXT_INSN (BB_END (bb)), end);
3890 27236703 : next_insn = NEXT_INSN (BB_END (bb));
3891 : }
3892 :
3893 2601646 : cfg_layout_function_footer = next_insn;
3894 2601646 : if (cfg_layout_function_footer)
3895 1045423 : cfg_layout_function_footer = unlink_insn_chain (cfg_layout_function_footer, get_last_insn ());
3896 2604262 : }
3897 :
3898 : namespace {
3899 :
3900 : const pass_data pass_data_into_cfg_layout_mode =
3901 : {
3902 : RTL_PASS, /* type */
3903 : "into_cfglayout", /* name */
3904 : OPTGROUP_NONE, /* optinfo_flags */
3905 : TV_CFG, /* tv_id */
3906 : 0, /* properties_required */
3907 : PROP_cfglayout, /* properties_provided */
3908 : 0, /* properties_destroyed */
3909 : 0, /* todo_flags_start */
3910 : 0, /* todo_flags_finish */
3911 : };
3912 :
3913 : class pass_into_cfg_layout_mode : public rtl_opt_pass
3914 : {
3915 : public:
3916 302985 : pass_into_cfg_layout_mode (gcc::context *ctxt)
3917 605970 : : rtl_opt_pass (pass_data_into_cfg_layout_mode, ctxt)
3918 : {}
3919 :
3920 : /* opt_pass methods: */
3921 1531106 : unsigned int execute (function *) final override
3922 : {
3923 1531106 : cfg_layout_initialize (0);
3924 1531106 : return 0;
3925 : }
3926 :
3927 : }; // class pass_into_cfg_layout_mode
3928 :
3929 : } // anon namespace
3930 :
3931 : rtl_opt_pass *
3932 302985 : make_pass_into_cfg_layout_mode (gcc::context *ctxt)
3933 : {
3934 302985 : return new pass_into_cfg_layout_mode (ctxt);
3935 : }
3936 :
3937 : namespace {
3938 :
3939 : const pass_data pass_data_outof_cfg_layout_mode =
3940 : {
3941 : RTL_PASS, /* type */
3942 : "outof_cfglayout", /* name */
3943 : OPTGROUP_NONE, /* optinfo_flags */
3944 : TV_CFG, /* tv_id */
3945 : 0, /* properties_required */
3946 : 0, /* properties_provided */
3947 : PROP_cfglayout, /* properties_destroyed */
3948 : 0, /* todo_flags_start */
3949 : 0, /* todo_flags_finish */
3950 : };
3951 :
3952 : class pass_outof_cfg_layout_mode : public rtl_opt_pass
3953 : {
3954 : public:
3955 302985 : pass_outof_cfg_layout_mode (gcc::context *ctxt)
3956 605970 : : rtl_opt_pass (pass_data_outof_cfg_layout_mode, ctxt)
3957 : {}
3958 :
3959 : /* opt_pass methods: */
3960 : unsigned int execute (function *) final override;
3961 :
3962 : }; // class pass_outof_cfg_layout_mode
3963 :
3964 : unsigned int
3965 1531121 : pass_outof_cfg_layout_mode::execute (function *fun)
3966 : {
3967 1531121 : basic_block bb;
3968 :
3969 15484529 : FOR_EACH_BB_FN (bb, fun)
3970 13953408 : if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (fun))
3971 12422287 : bb->aux = bb->next_bb;
3972 :
3973 1531121 : cfg_layout_finalize ();
3974 :
3975 1531121 : return 0;
3976 : }
3977 :
3978 : } // anon namespace
3979 :
3980 : rtl_opt_pass *
3981 302985 : make_pass_outof_cfg_layout_mode (gcc::context *ctxt)
3982 : {
3983 302985 : return new pass_outof_cfg_layout_mode (ctxt);
3984 : }
3985 :
3986 :
3987 : /* Link the basic blocks in the correct order, compacting the basic
3988 : block queue while at it. If STAY_IN_CFGLAYOUT_MODE is false, this
3989 : function also clears the basic block header and footer fields.
3990 :
3991 : This function is usually called after a pass (e.g. tracer) finishes
3992 : some transformations while in cfglayout mode. The required sequence
3993 : of the basic blocks is in a linked list along the bb->aux field.
3994 : This functions re-links the basic block prev_bb and next_bb pointers
3995 : accordingly, and it compacts and renumbers the blocks.
3996 :
3997 : FIXME: This currently works only for RTL, but the only RTL-specific
3998 : bits are the STAY_IN_CFGLAYOUT_MODE bits. The tracer pass was moved
3999 : to GIMPLE a long time ago, but it doesn't relink the basic block
4000 : chain. It could do that (to give better initial RTL) if this function
4001 : is made IR-agnostic (and moved to cfganal.cc or cfg.cc while at it). */
4002 :
4003 : void
4004 3248748 : relink_block_chain (bool stay_in_cfglayout_mode)
4005 : {
4006 3248748 : basic_block bb, prev_bb;
4007 3248748 : int index;
4008 :
4009 : /* Maybe dump the re-ordered sequence. */
4010 3248748 : if (dump_file)
4011 : {
4012 168 : fprintf (dump_file, "Reordered sequence:\n");
4013 168 : for (bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb, index =
4014 : NUM_FIXED_BLOCKS;
4015 1093 : bb;
4016 925 : bb = (basic_block) bb->aux, index++)
4017 : {
4018 925 : fprintf (dump_file, " %i ", index);
4019 925 : if (get_bb_original (bb))
4020 28 : fprintf (dump_file, "duplicate of %i\n",
4021 28 : get_bb_original (bb)->index);
4022 897 : else if (forwarder_block_p (bb)
4023 897 : && !LABEL_P (BB_HEAD (bb)))
4024 16 : fprintf (dump_file, "compensation\n");
4025 : else
4026 881 : fprintf (dump_file, "bb %i\n", bb->index);
4027 : }
4028 : }
4029 :
4030 : /* Now reorder the blocks. */
4031 3248748 : prev_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
4032 3248748 : bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb;
4033 39787334 : for (; bb; prev_bb = bb, bb = (basic_block) bb->aux)
4034 : {
4035 36538586 : bb->prev_bb = prev_bb;
4036 36538586 : prev_bb->next_bb = bb;
4037 : }
4038 3248748 : prev_bb->next_bb = EXIT_BLOCK_PTR_FOR_FN (cfun);
4039 3248748 : EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb = prev_bb;
4040 :
4041 : /* Then, clean up the aux fields. */
4042 46284830 : FOR_ALL_BB_FN (bb, cfun)
4043 : {
4044 43036082 : bb->aux = NULL;
4045 43036082 : if (!stay_in_cfglayout_mode)
4046 30896436 : BB_HEADER (bb) = BB_FOOTER (bb) = NULL;
4047 : }
4048 :
4049 : /* Maybe reset the original copy tables, they are not valid anymore
4050 : when we renumber the basic blocks in compact_blocks. If we are
4051 : are going out of cfglayout mode, don't re-allocate the tables. */
4052 3248748 : if (original_copy_tables_initialized_p ())
4053 3248733 : free_original_copy_tables ();
4054 3248748 : if (stay_in_cfglayout_mode)
4055 647087 : initialize_original_copy_tables ();
4056 :
4057 : /* Finally, put basic_block_info in the new order. */
4058 3248748 : compact_blocks ();
4059 3248748 : }
4060 :
4061 :
4062 : /* Given a reorder chain, rearrange the code to match. */
4063 :
4064 : static void
4065 2601661 : fixup_reorder_chain (void)
4066 : {
4067 2601661 : basic_block bb;
4068 2601661 : rtx_insn *insn = NULL;
4069 :
4070 2601661 : if (cfg_layout_function_header)
4071 : {
4072 2601617 : set_first_insn (cfg_layout_function_header);
4073 2601617 : insn = cfg_layout_function_header;
4074 2604262 : while (NEXT_INSN (insn))
4075 : insn = NEXT_INSN (insn);
4076 : }
4077 :
4078 : /* First do the bulk reordering -- rechain the blocks without regard to
4079 : the needed changes to jumps and labels. */
4080 :
4081 27537937 : for (bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; bb; bb = (basic_block)
4082 : bb->aux)
4083 : {
4084 24936276 : if (BB_HEADER (bb))
4085 : {
4086 46159 : if (insn)
4087 46159 : SET_NEXT_INSN (insn) = BB_HEADER (bb);
4088 : else
4089 0 : set_first_insn (BB_HEADER (bb));
4090 46159 : SET_PREV_INSN (BB_HEADER (bb)) = insn;
4091 46159 : insn = BB_HEADER (bb);
4092 59405 : while (NEXT_INSN (insn))
4093 : insn = NEXT_INSN (insn);
4094 : }
4095 24936276 : if (insn)
4096 24936232 : SET_NEXT_INSN (insn) = BB_HEAD (bb);
4097 : else
4098 44 : set_first_insn (BB_HEAD (bb));
4099 24936276 : SET_PREV_INSN (BB_HEAD (bb)) = insn;
4100 24936276 : insn = BB_END (bb);
4101 24936276 : if (BB_FOOTER (bb))
4102 : {
4103 3088934 : SET_NEXT_INSN (insn) = BB_FOOTER (bb);
4104 3088934 : SET_PREV_INSN (BB_FOOTER (bb)) = insn;
4105 6228799 : while (NEXT_INSN (insn))
4106 : insn = NEXT_INSN (insn);
4107 : }
4108 : }
4109 :
4110 2601661 : SET_NEXT_INSN (insn) = cfg_layout_function_footer;
4111 2601661 : if (cfg_layout_function_footer)
4112 1045445 : SET_PREV_INSN (cfg_layout_function_footer) = insn;
4113 :
4114 3648014 : while (NEXT_INSN (insn))
4115 : insn = NEXT_INSN (insn);
4116 :
4117 2601661 : set_last_insn (insn);
4118 2601661 : if (flag_checking)
4119 2601623 : verify_insn_chain ();
4120 :
4121 : /* Now add jumps and labels as needed to match the blocks new
4122 : outgoing edges. */
4123 :
4124 2601661 : bool remove_unreachable_blocks = false;
4125 27537937 : for (bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; bb ; bb = (basic_block)
4126 : bb->aux)
4127 : {
4128 24936276 : edge e_fall, e_taken, e;
4129 24936276 : rtx_insn *bb_end_insn;
4130 24936276 : rtx ret_label = NULL_RTX;
4131 24936276 : basic_block nb;
4132 24936276 : edge_iterator ei;
4133 24936276 : bool asm_goto = false;
4134 :
4135 24936276 : if (EDGE_COUNT (bb->succs) == 0)
4136 20732024 : continue;
4137 :
4138 : /* Find the old fallthru edge, and another non-EH edge for
4139 : a taken jump. */
4140 23599645 : e_taken = e_fall = NULL;
4141 :
4142 60214590 : FOR_EACH_EDGE (e, ei, bb->succs)
4143 36614945 : if (e->flags & EDGE_FALLTHRU)
4144 : e_fall = e;
4145 14766428 : else if (! (e->flags & EDGE_EH))
4146 13420611 : e_taken = e;
4147 :
4148 23599645 : bb_end_insn = BB_END (bb);
4149 23599645 : if (rtx_jump_insn *bb_end_jump = dyn_cast <rtx_jump_insn *> (bb_end_insn))
4150 : {
4151 12957678 : ret_label = JUMP_LABEL (bb_end_jump);
4152 12957678 : if (any_condjump_p (bb_end_jump))
4153 : {
4154 : /* This might happen if the conditional jump has side
4155 : effects and could therefore not be optimized away.
4156 : Make the basic block to end with a barrier in order
4157 : to prevent rtl_verify_flow_info from complaining. */
4158 11571939 : if (!e_fall)
4159 : {
4160 0 : gcc_assert (!onlyjump_p (bb_end_jump)
4161 : || returnjump_p (bb_end_jump)
4162 : || (e_taken->flags & EDGE_CROSSING));
4163 0 : emit_barrier_after (bb_end_jump);
4164 0 : continue;
4165 : }
4166 :
4167 : /* If the old fallthru is still next, nothing to do. */
4168 11571939 : if (bb->aux == e_fall->dest
4169 2739430 : || e_fall->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
4170 8832521 : continue;
4171 :
4172 : /* The degenerated case of conditional jump jumping to the next
4173 : instruction can happen for jumps with side effects. We need
4174 : to construct a forwarder block and this will be done just
4175 : fine by force_nonfallthru below. */
4176 2739418 : if (!e_taken)
4177 : ;
4178 :
4179 : /* There is another special case: if *neither* block is next,
4180 : such as happens at the very end of a function, then we'll
4181 : need to add a new unconditional jump. Choose the taken
4182 : edge based on known or assumed probability. */
4183 2739418 : else if (bb->aux != e_taken->dest)
4184 : {
4185 570442 : rtx note = find_reg_note (bb_end_jump, REG_BR_PROB, 0);
4186 :
4187 570442 : if (note
4188 : && profile_probability::from_reg_br_prob_note
4189 547801 : (XINT (note, 0)) < profile_probability::even ()
4190 766607 : && invert_jump (bb_end_jump,
4191 196165 : (e_fall->dest
4192 196165 : == EXIT_BLOCK_PTR_FOR_FN (cfun)
4193 : ? NULL_RTX
4194 196165 : : label_for_bb (e_fall->dest)), 0))
4195 : {
4196 196165 : e_fall->flags &= ~EDGE_FALLTHRU;
4197 196165 : gcc_checking_assert (could_fall_through
4198 : (e_taken->src, e_taken->dest));
4199 196165 : e_taken->flags |= EDGE_FALLTHRU;
4200 196165 : update_br_prob_note (bb);
4201 196165 : e = e_fall, e_fall = e_taken, e_taken = e;
4202 : }
4203 : }
4204 :
4205 : /* If the "jumping" edge is a crossing edge, and the fall
4206 : through edge is non-crossing, leave things as they are. */
4207 2168976 : else if ((e_taken->flags & EDGE_CROSSING)
4208 70975 : && !(e_fall->flags & EDGE_CROSSING))
4209 70975 : continue;
4210 :
4211 : /* Otherwise we can try to invert the jump. This will
4212 : basically never fail, however, keep up the pretense. */
4213 2098001 : else if (invert_jump (bb_end_jump,
4214 : (e_fall->dest
4215 : == EXIT_BLOCK_PTR_FOR_FN (cfun)
4216 : ? NULL_RTX
4217 2098001 : : label_for_bb (e_fall->dest)), 0))
4218 : {
4219 2098001 : e_fall->flags &= ~EDGE_FALLTHRU;
4220 2098001 : gcc_checking_assert (could_fall_through
4221 : (e_taken->src, e_taken->dest));
4222 2098001 : e_taken->flags |= EDGE_FALLTHRU;
4223 2098001 : update_br_prob_note (bb);
4224 2098001 : if (LABEL_NUSES (ret_label) == 0
4225 2098001 : && single_pred_p (e_taken->dest))
4226 1297285 : delete_insn (as_a<rtx_insn *> (ret_label));
4227 2098001 : continue;
4228 : }
4229 : }
4230 1385739 : else if (extract_asm_operands (PATTERN (bb_end_insn)) != NULL)
4231 : {
4232 : /* If the old fallthru is still next or if
4233 : asm goto doesn't have a fallthru (e.g. when followed by
4234 : __builtin_unreachable ()), nothing to do. */
4235 1261 : if (! e_fall
4236 1195 : || bb->aux == e_fall->dest
4237 137 : || e_fall->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
4238 1124 : continue;
4239 :
4240 : /* Otherwise we'll have to use the fallthru fixup below.
4241 : But avoid redirecting asm goto to EXIT. */
4242 : asm_goto = true;
4243 : }
4244 : else
4245 : {
4246 : /* Otherwise we have some return, switch or computed
4247 : jump. In the 99% case, there should not have been a
4248 : fallthru edge. */
4249 1384478 : gcc_assert (returnjump_p (bb_end_insn) || !e_fall);
4250 1384478 : continue;
4251 : }
4252 : }
4253 : else
4254 : {
4255 : /* No fallthru implies a noreturn function with EH edges, or
4256 : something similarly bizarre. In any case, we don't need to
4257 : do anything. */
4258 10641967 : if (! e_fall)
4259 366584 : continue;
4260 :
4261 : /* If the fallthru block is still next, nothing to do. */
4262 10275383 : if (bb->aux == e_fall->dest)
4263 5216207 : continue;
4264 :
4265 : /* A fallthru to exit block. */
4266 5059176 : if (e_fall->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
4267 1425503 : continue;
4268 : }
4269 :
4270 : /* If E_FALL->dest is just a return block, then we can emit a
4271 : return rather than a jump to the return block. */
4272 570442 : rtx_insn *ret, *use;
4273 570442 : basic_block dest;
4274 570442 : if (!asm_goto
4275 4204115 : && bb_is_just_return (e_fall->dest, &ret, &use)
4276 593 : && ((PATTERN (ret) == simple_return_rtx && targetm.have_simple_return ())
4277 0 : || (PATTERN (ret) == ret_rtx && targetm.have_return ())))
4278 : {
4279 593 : ret_label = PATTERN (ret);
4280 593 : dest = EXIT_BLOCK_PTR_FOR_FN (cfun);
4281 :
4282 593 : e_fall->flags &= ~EDGE_CROSSING;
4283 : /* E_FALL->dest might become unreachable as a result of
4284 : replacing the jump with a return. So arrange to remove
4285 : unreachable blocks. */
4286 593 : remove_unreachable_blocks = true;
4287 : }
4288 : else
4289 : {
4290 4203659 : dest = e_fall->dest;
4291 : }
4292 :
4293 : /* We got here if we need to add a new jump insn.
4294 : Note force_nonfallthru can delete E_FALL and thus we have to
4295 : save E_FALL->src prior to the call to force_nonfallthru. */
4296 4204252 : nb = force_nonfallthru_and_redirect (e_fall, dest, ret_label);
4297 4204252 : if (nb)
4298 : {
4299 756838 : nb->aux = bb->aux;
4300 756838 : bb->aux = nb;
4301 : /* Don't process this new block. */
4302 756838 : bb = nb;
4303 : }
4304 : }
4305 :
4306 2601661 : relink_block_chain (/*stay_in_cfglayout_mode=*/false);
4307 :
4308 : /* Annoying special case - jump around dead jumptables left in the code. */
4309 28365750 : FOR_EACH_BB_FN (bb, cfun)
4310 : {
4311 25764089 : edge e = find_fallthru_edge (bb->succs);
4312 :
4313 25764089 : if (e && !can_fallthru (e->src, e->dest))
4314 70975 : force_nonfallthru (e);
4315 : }
4316 :
4317 : /* Ensure goto_locus from edges has some instructions with that locus in RTL
4318 : when not optimizing. */
4319 2601661 : if (!optimize && !DECL_IGNORED_P (current_function_decl))
4320 4016768 : FOR_EACH_BB_FN (bb, cfun)
4321 : {
4322 3558073 : edge e;
4323 3558073 : edge_iterator ei;
4324 :
4325 8440170 : FOR_EACH_EDGE (e, ei, bb->succs)
4326 4882097 : if (LOCATION_LOCUS (e->goto_locus) != UNKNOWN_LOCATION
4327 4882097 : && !(e->flags & EDGE_ABNORMAL))
4328 : {
4329 777728 : edge e2;
4330 777728 : edge_iterator ei2;
4331 777728 : basic_block dest, nb;
4332 777728 : rtx_insn *end;
4333 :
4334 777728 : insn = BB_END (e->src);
4335 777728 : end = PREV_INSN (BB_HEAD (e->src));
4336 777728 : while (insn != end
4337 952905 : && (!NONDEBUG_INSN_P (insn) || !insn->has_location ()))
4338 175177 : insn = PREV_INSN (insn);
4339 1307676 : if (insn != end
4340 777728 : && loc_equal (insn->location (), e->goto_locus))
4341 546402 : continue;
4342 247780 : if (simplejump_p (BB_END (e->src))
4343 247780 : && !BB_END (e->src)->has_location ())
4344 : {
4345 0 : BB_END (e->src)->set_location (e->goto_locus);
4346 0 : continue;
4347 : }
4348 247780 : dest = e->dest;
4349 247780 : if (dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
4350 : {
4351 : /* Non-fallthru edges to the exit block cannot be split. */
4352 160158 : if (!(e->flags & EDGE_FALLTHRU))
4353 0 : continue;
4354 : }
4355 : else
4356 : {
4357 87622 : insn = BB_HEAD (dest);
4358 87622 : end = NEXT_INSN (BB_END (dest));
4359 345833 : while (insn != end && !NONDEBUG_INSN_P (insn))
4360 170589 : insn = NEXT_INSN (insn);
4361 56988 : if (insn != end && insn->has_location ()
4362 141937 : && loc_equal (insn->location (), e->goto_locus))
4363 16454 : continue;
4364 : }
4365 231326 : nb = split_edge (e);
4366 231326 : if (!INSN_P (BB_END (nb)))
4367 231326 : BB_END (nb) = emit_insn_after_noloc (gen_nop (), BB_END (nb),
4368 : nb);
4369 231326 : BB_END (nb)->set_location (e->goto_locus);
4370 :
4371 : /* If there are other incoming edges to the destination block
4372 : with the same goto locus, redirect them to the new block as
4373 : well, this can prevent other such blocks from being created
4374 : in subsequent iterations of the loop. */
4375 575752 : for (ei2 = ei_start (dest->preds); (e2 = ei_safe_edge (ei2)); )
4376 344426 : if (LOCATION_LOCUS (e2->goto_locus) != UNKNOWN_LOCATION
4377 69164 : && !(e2->flags & (EDGE_ABNORMAL | EDGE_FALLTHRU))
4378 413590 : && e->goto_locus == e2->goto_locus)
4379 16536 : redirect_edge_and_branch (e2, nb);
4380 : else
4381 327890 : ei_next (&ei2);
4382 : }
4383 : }
4384 :
4385 : /* Replacing a jump with a return may have exposed an unreachable
4386 : block. Conditionally remove them if such transformations were
4387 : made. */
4388 2601661 : if (remove_unreachable_blocks)
4389 581 : delete_unreachable_blocks ();
4390 2601661 : }
4391 :
4392 : /* Perform sanity checks on the insn chain.
4393 : 1. Check that next/prev pointers are consistent in both the forward and
4394 : reverse direction.
4395 : 2. Count insns in chain, going both directions, and check if equal.
4396 : 3. Check that get_last_insn () returns the actual end of chain. */
4397 :
4398 : DEBUG_FUNCTION void
4399 5203246 : verify_insn_chain (void)
4400 : {
4401 5203246 : rtx_insn *x, *prevx, *nextx;
4402 5203246 : int insn_cnt1, insn_cnt2;
4403 :
4404 5203246 : for (prevx = NULL, insn_cnt1 = 1, x = get_insns ();
4405 635400527 : x != 0;
4406 630197281 : prevx = x, insn_cnt1++, x = NEXT_INSN (x))
4407 630197281 : gcc_assert (PREV_INSN (x) == prevx);
4408 :
4409 5203246 : gcc_assert (prevx == get_last_insn ());
4410 :
4411 : for (nextx = NULL, insn_cnt2 = 1, x = get_last_insn ();
4412 635400527 : x != 0;
4413 630197281 : nextx = x, insn_cnt2++, x = PREV_INSN (x))
4414 630197281 : gcc_assert (NEXT_INSN (x) == nextx);
4415 :
4416 5203246 : gcc_assert (insn_cnt1 == insn_cnt2);
4417 5203246 : }
4418 :
4419 : /* If we have assembler epilogues, the block falling through to exit must
4420 : be the last one in the reordered chain when we reach final. Ensure
4421 : that this condition is met. */
4422 : static void
4423 0 : fixup_fallthru_exit_predecessor (void)
4424 : {
4425 0 : edge e;
4426 0 : basic_block bb = NULL;
4427 :
4428 : /* This transformation is not valid before reload, because we might
4429 : separate a call from the instruction that copies the return
4430 : value. */
4431 0 : gcc_assert (reload_completed);
4432 :
4433 0 : e = find_fallthru_edge (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
4434 0 : if (e)
4435 0 : bb = e->src;
4436 :
4437 0 : if (bb && bb->aux)
4438 : {
4439 0 : basic_block c = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb;
4440 :
4441 : /* If the very first block is the one with the fall-through exit
4442 : edge, we have to split that block. */
4443 0 : if (c == bb)
4444 : {
4445 0 : bb = split_block_after_labels (bb)->dest;
4446 0 : bb->aux = c->aux;
4447 0 : c->aux = bb;
4448 0 : BB_FOOTER (bb) = BB_FOOTER (c);
4449 0 : BB_FOOTER (c) = NULL;
4450 : }
4451 :
4452 0 : while (c->aux != bb)
4453 : c = (basic_block) c->aux;
4454 :
4455 0 : c->aux = bb->aux;
4456 0 : while (c->aux)
4457 : c = (basic_block) c->aux;
4458 :
4459 0 : c->aux = bb;
4460 0 : bb->aux = NULL;
4461 : }
4462 0 : }
4463 :
4464 : /* In case there are more than one fallthru predecessors of exit, force that
4465 : there is only one. */
4466 :
4467 : static void
4468 2601661 : force_one_exit_fallthru (void)
4469 : {
4470 2601661 : edge e, predecessor = NULL;
4471 2601661 : bool more = false;
4472 2601661 : edge_iterator ei;
4473 2601661 : basic_block forwarder, bb;
4474 :
4475 5493747 : FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
4476 2892087 : if (e->flags & EDGE_FALLTHRU)
4477 : {
4478 1425516 : if (predecessor == NULL)
4479 : predecessor = e;
4480 : else
4481 : {
4482 : more = true;
4483 : break;
4484 : }
4485 : }
4486 :
4487 2601661 : if (!more)
4488 2601660 : return;
4489 :
4490 : /* Exit has several fallthru predecessors. Create a forwarder block for
4491 : them. */
4492 1 : forwarder = split_edge (predecessor);
4493 1 : for (ei = ei_start (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
4494 3 : (e = ei_safe_edge (ei)); )
4495 : {
4496 2 : if (e->src == forwarder
4497 1 : || !(e->flags & EDGE_FALLTHRU))
4498 1 : ei_next (&ei);
4499 : else
4500 1 : redirect_edge_and_branch_force (e, forwarder);
4501 : }
4502 :
4503 : /* Fix up the chain of blocks -- make FORWARDER immediately precede the
4504 : exit block. */
4505 5 : FOR_EACH_BB_FN (bb, cfun)
4506 : {
4507 5 : if (bb->aux == NULL && bb != forwarder)
4508 : {
4509 1 : bb->aux = forwarder;
4510 1 : break;
4511 : }
4512 : }
4513 : }
4514 :
4515 : /* Return true if BB can be duplicated. A tablejump can be duplicated only
4516 : when PROP_rtl_allow_tablejump_duplication is set. Its copy then shares the
4517 : table with BB, see duplicate_insn_chain. */
4518 :
4519 : static bool
4520 8523977 : cfg_layout_can_duplicate_bb_p (const_basic_block bb)
4521 : {
4522 : /* A copy of a tablejump shares the dispatch table with the original.
4523 : Unsharing it is not an option, as the instructions computing the jump
4524 : destination may have been hoisted outside the basic block. The property
4525 : is set while a pass can make the table immutable. */
4526 8523977 : if (!(cfun->curr_properties & PROP_rtl_allow_tablejump_duplication)
4527 8523977 : && tablejump_p (BB_END (bb), NULL, NULL))
4528 : return false;
4529 :
4530 : /* Do not duplicate blocks containing insns that can't be copied. */
4531 8520387 : if (targetm.cannot_copy_insn_p)
4532 : {
4533 0 : rtx_insn *insn = BB_HEAD (bb);
4534 0 : while (1)
4535 : {
4536 0 : if (INSN_P (insn) && targetm.cannot_copy_insn_p (insn))
4537 : return false;
4538 0 : if (insn == BB_END (bb))
4539 : break;
4540 0 : insn = NEXT_INSN (insn);
4541 : }
4542 : }
4543 :
4544 : return true;
4545 : }
4546 :
4547 : rtx_insn *
4548 918960 : duplicate_insn_chain (rtx_insn *from, rtx_insn *to,
4549 : class loop *loop, copy_bb_data *id)
4550 : {
4551 918960 : rtx_insn *insn, *next, *copy;
4552 918960 : rtx_note *last;
4553 :
4554 : /* Avoid updating of boundaries of previous basic block. The
4555 : note will get removed from insn stream in fixup. */
4556 918960 : last = emit_note (NOTE_INSN_DELETED);
4557 :
4558 : /* Create copy at the end of INSN chain. The chain will
4559 : be reordered later. */
4560 7298840 : for (insn = from; insn != NEXT_INSN (to); insn = NEXT_INSN (insn))
4561 : {
4562 5460920 : switch (GET_CODE (insn))
4563 : {
4564 1409495 : case DEBUG_INSN:
4565 : /* Don't duplicate label debug insns. */
4566 1409495 : if (DEBUG_BIND_INSN_P (insn)
4567 1077602 : && TREE_CODE (INSN_VAR_LOCATION_DECL (insn)) == LABEL_DECL)
4568 : break;
4569 : /* FALLTHRU */
4570 3663242 : case INSN:
4571 3663242 : case CALL_INSN:
4572 3663242 : case JUMP_INSN:
4573 3663242 : copy = emit_copy_of_insn_after (insn, get_last_insn ());
4574 3663242 : if (JUMP_P (insn) && JUMP_LABEL (insn) != NULL_RTX
4575 503772 : && ANY_RETURN_P (JUMP_LABEL (insn)))
4576 179536 : JUMP_LABEL (copy) = JUMP_LABEL (insn);
4577 3483706 : else if (JUMP_P (insn))
4578 : {
4579 324464 : rtx_insn *table_label;
4580 324464 : rtx_jump_table_data *table;
4581 324464 : if (tablejump_p (insn, &table_label, &table))
4582 : {
4583 : /* The copy dispatches through the very same table. Mark it:
4584 : its entries now describe the successors of several jumps
4585 : at once and so must not be edited in place. The reference
4586 : itself is recorded by emit_copy_of_insn_after through
4587 : mark_jump_label. */
4588 1728 : gcc_assert (JUMP_LABEL (copy) == table_label);
4589 1728 : JUMP_TABLE_SHARED_P (PATTERN (table)) = 1;
4590 1728 : gcc_assert (tablejump_p (copy, NULL, NULL));
4591 : }
4592 : }
4593 3663242 : maybe_copy_prologue_epilogue_insn (insn, copy);
4594 : /* If requested remap dependence info of cliques brought in
4595 : via inlining. */
4596 3663242 : if (id)
4597 : {
4598 1913350 : subrtx_iterator::array_type array;
4599 11743393 : FOR_EACH_SUBRTX (iter, array, PATTERN (insn), ALL)
4600 9834683 : if (MEM_P (*iter) && MEM_EXPR (*iter))
4601 : {
4602 302710 : tree op = MEM_EXPR (*iter);
4603 302710 : if (TREE_CODE (op) == WITH_SIZE_EXPR)
4604 0 : op = TREE_OPERAND (op, 0);
4605 330804 : while (handled_component_p (op))
4606 28094 : op = TREE_OPERAND (op, 0);
4607 302710 : if ((TREE_CODE (op) == MEM_REF
4608 302710 : || TREE_CODE (op) == TARGET_MEM_REF)
4609 284014 : && MR_DEPENDENCE_CLIQUE (op) > 1
4610 316624 : && (!loop
4611 13914 : || (MR_DEPENDENCE_CLIQUE (op)
4612 13914 : != loop->owned_clique)))
4613 : {
4614 2146 : if (!id->dependence_map)
4615 1183 : id->dependence_map = new hash_map<dependence_hash,
4616 : unsigned short>;
4617 2146 : bool existed;
4618 2146 : unsigned short &newc = id->dependence_map->get_or_insert
4619 2146 : (MR_DEPENDENCE_CLIQUE (op), &existed);
4620 2146 : if (!existed)
4621 : {
4622 1193 : gcc_assert
4623 : (MR_DEPENDENCE_CLIQUE (op) <= cfun->last_clique);
4624 2386 : newc = get_new_clique (cfun);
4625 : }
4626 : /* We cannot adjust MR_DEPENDENCE_CLIQUE in-place
4627 : since MEM_EXPR is shared so make a copy and
4628 : walk to the subtree again. */
4629 2146 : tree new_expr = unshare_expr (MEM_EXPR (*iter));
4630 2146 : tree orig_new_expr = new_expr;
4631 2146 : if (TREE_CODE (new_expr) == WITH_SIZE_EXPR)
4632 0 : new_expr = TREE_OPERAND (new_expr, 0);
4633 2704 : while (handled_component_p (new_expr))
4634 558 : new_expr = TREE_OPERAND (new_expr, 0);
4635 2146 : MR_DEPENDENCE_CLIQUE (new_expr) = newc;
4636 2146 : set_mem_expr (const_cast <rtx> (*iter), orig_new_expr);
4637 : }
4638 : }
4639 1913350 : }
4640 : break;
4641 :
4642 0 : case JUMP_TABLE_DATA:
4643 : /* Avoid copying dispatch tables. A duplicated tablejump continues
4644 : to use the original table. Avoid copying the following barrier as
4645 : well, if any, and debug insns in between. */
4646 0 : for (next = NEXT_INSN (insn);
4647 0 : next != NEXT_INSN (to);
4648 0 : next = NEXT_INSN (next))
4649 0 : if (!DEBUG_INSN_P (next))
4650 : break;
4651 0 : if (next != NEXT_INSN (to) && BARRIER_P (next))
4652 5460920 : insn = next;
4653 : break;
4654 :
4655 : case CODE_LABEL:
4656 : break;
4657 :
4658 180830 : case BARRIER:
4659 180830 : emit_barrier ();
4660 180830 : break;
4661 :
4662 952704 : case NOTE:
4663 952704 : switch (NOTE_KIND (insn))
4664 : {
4665 : /* In case prologue is empty and function contain label
4666 : in first BB, we may want to copy the block. */
4667 : case NOTE_INSN_PROLOGUE_END:
4668 :
4669 : case NOTE_INSN_DELETED:
4670 : case NOTE_INSN_DELETED_LABEL:
4671 : case NOTE_INSN_DELETED_DEBUG_LABEL:
4672 : /* No problem to strip these. */
4673 : case NOTE_INSN_FUNCTION_BEG:
4674 : /* There is always just single entry to function. */
4675 : case NOTE_INSN_BASIC_BLOCK:
4676 : /* We should only switch text sections once. */
4677 : case NOTE_INSN_SWITCH_TEXT_SECTIONS:
4678 : break;
4679 :
4680 176948 : case NOTE_INSN_EPILOGUE_BEG:
4681 176948 : case NOTE_INSN_UPDATE_SJLJ_CONTEXT:
4682 176948 : emit_note_copy (as_a <rtx_note *> (insn));
4683 176948 : break;
4684 :
4685 0 : default:
4686 : /* All other notes should have already been eliminated. */
4687 0 : gcc_unreachable ();
4688 : }
4689 : break;
4690 0 : default:
4691 0 : gcc_unreachable ();
4692 : }
4693 : }
4694 918960 : insn = NEXT_INSN (last);
4695 918960 : delete_insn (last);
4696 918960 : return insn;
4697 : }
4698 :
4699 : /* Create a duplicate of the basic block BB. */
4700 :
4701 : static basic_block
4702 735540 : cfg_layout_duplicate_bb (basic_block bb, copy_bb_data *id)
4703 : {
4704 735540 : rtx_insn *insn;
4705 735540 : basic_block new_bb;
4706 :
4707 735540 : class loop *loop = (id && current_loops) ? bb->loop_father : NULL;
4708 :
4709 735540 : insn = duplicate_insn_chain (BB_HEAD (bb), BB_END (bb), loop, id);
4710 735540 : new_bb = create_basic_block (insn,
4711 : insn ? get_last_insn () : NULL,
4712 735540 : EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb);
4713 :
4714 735540 : BB_COPY_PARTITION (new_bb, bb);
4715 735540 : if (BB_HEADER (bb))
4716 : {
4717 : insn = BB_HEADER (bb);
4718 3466 : while (NEXT_INSN (insn))
4719 : insn = NEXT_INSN (insn);
4720 2468 : insn = duplicate_insn_chain (BB_HEADER (bb), insn, loop, id);
4721 2468 : if (insn)
4722 0 : BB_HEADER (new_bb) = unlink_insn_chain (insn, get_last_insn ());
4723 : }
4724 :
4725 735540 : if (BB_FOOTER (bb))
4726 : {
4727 : insn = BB_FOOTER (bb);
4728 180857 : while (NEXT_INSN (insn))
4729 : insn = NEXT_INSN (insn);
4730 180740 : insn = duplicate_insn_chain (BB_FOOTER (bb), insn, loop, id);
4731 180740 : if (insn)
4732 180740 : BB_FOOTER (new_bb) = unlink_insn_chain (insn, get_last_insn ());
4733 : }
4734 :
4735 735540 : return new_bb;
4736 : }
4737 :
4738 :
4739 : /* Main entry point to this module - initialize the datastructures for
4740 : CFG layout changes. It keeps LOOPS up-to-date if not null.
4741 :
4742 : FLAGS is a set of additional flags to pass to cleanup_cfg(). */
4743 :
4744 : void
4745 2601646 : cfg_layout_initialize (int flags)
4746 : {
4747 2601646 : rtx_insn_list *x;
4748 2601646 : basic_block bb;
4749 :
4750 : /* Once bb partitioning is complete, cfg layout mode should not be
4751 : re-entered. Entering cfg layout mode may require fixups. As an
4752 : example, if edge forwarding performed when optimizing the cfg
4753 : layout required moving a block from the hot to the cold
4754 : section. This would create an illegal partitioning unless some
4755 : manual fixup was performed. */
4756 2601646 : gcc_assert (!crtl->bb_reorder_complete || !crtl->has_bb_partition);
4757 :
4758 2601646 : initialize_original_copy_tables ();
4759 :
4760 2601646 : cfg_layout_rtl_register_cfg_hooks ();
4761 :
4762 2601646 : record_effective_endpoints ();
4763 :
4764 : /* Make sure that the targets of non local gotos are marked. */
4765 2604066 : for (x = nonlocal_goto_handler_labels; x; x = x->next ())
4766 : {
4767 2420 : bb = BLOCK_FOR_INSN (x->insn ());
4768 2420 : bb->flags |= BB_NON_LOCAL_GOTO_TARGET;
4769 : }
4770 :
4771 2601646 : cleanup_cfg (CLEANUP_CFGLAYOUT | flags);
4772 2601646 : }
4773 :
4774 : /* Splits superblocks. */
4775 : void
4776 66048 : break_superblocks (void)
4777 : {
4778 66048 : bool need = false;
4779 66048 : basic_block bb;
4780 :
4781 66048 : auto_sbitmap superblocks (last_basic_block_for_fn (cfun));
4782 66048 : bitmap_clear (superblocks);
4783 :
4784 3769087 : FOR_EACH_BB_FN (bb, cfun)
4785 3703039 : if (bb->flags & BB_SUPERBLOCK)
4786 : {
4787 220578 : bb->flags &= ~BB_SUPERBLOCK;
4788 220578 : bitmap_set_bit (superblocks, bb->index);
4789 220578 : need = true;
4790 : }
4791 :
4792 66048 : if (need)
4793 : {
4794 56515 : rebuild_jump_labels (get_insns ());
4795 56515 : find_many_sub_basic_blocks (superblocks);
4796 : }
4797 66048 : }
4798 :
4799 : /* Finalize the changes: reorder insn list according to the sequence specified
4800 : by aux pointers, enter compensation code, rebuild scope forest. */
4801 :
4802 : void
4803 2601661 : cfg_layout_finalize (void)
4804 : {
4805 2601661 : free_dominance_info (CDI_DOMINATORS);
4806 2601661 : force_one_exit_fallthru ();
4807 2601661 : rtl_register_cfg_hooks ();
4808 2601661 : if (reload_completed && !targetm.have_epilogue ())
4809 0 : fixup_fallthru_exit_predecessor ();
4810 2601661 : fixup_reorder_chain ();
4811 :
4812 2601661 : rebuild_jump_labels (get_insns ());
4813 2601661 : delete_dead_jumptables ();
4814 :
4815 2601661 : if (flag_checking)
4816 2601623 : verify_insn_chain ();
4817 2601661 : checking_verify_flow_info ();
4818 2601661 : }
4819 :
4820 :
4821 : /* Same as split_block but update cfg_layout structures. */
4822 :
4823 : static basic_block
4824 17592 : cfg_layout_split_block (basic_block bb, void *insnp)
4825 : {
4826 17592 : rtx insn = (rtx) insnp;
4827 17592 : basic_block new_bb = rtl_split_block (bb, insn);
4828 :
4829 17592 : BB_FOOTER (new_bb) = BB_FOOTER (bb);
4830 17592 : BB_FOOTER (bb) = NULL;
4831 :
4832 17592 : return new_bb;
4833 : }
4834 :
4835 : /* Redirect Edge to DEST. */
4836 : static edge
4837 10530783 : cfg_layout_redirect_edge_and_branch (edge e, basic_block dest)
4838 : {
4839 10530783 : basic_block src = e->src;
4840 10530783 : edge ret;
4841 :
4842 10530783 : if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
4843 : return NULL;
4844 :
4845 10530783 : if (e->dest == dest)
4846 : return e;
4847 :
4848 10530783 : if (e->flags & EDGE_CROSSING
4849 5 : && BB_PARTITION (e->src) == BB_PARTITION (dest)
4850 10530783 : && simplejump_p (BB_END (src)))
4851 : {
4852 0 : if (dump_file)
4853 0 : fprintf (dump_file,
4854 : "Removing crossing jump while redirecting edge form %i to %i\n",
4855 0 : e->src->index, dest->index);
4856 0 : delete_insn (BB_END (src));
4857 0 : remove_barriers_from_footer (src);
4858 0 : e->flags |= EDGE_FALLTHRU;
4859 : }
4860 :
4861 10530783 : if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
4862 10530783 : && (ret = try_redirect_by_replacing_jump (e, dest, true)))
4863 : {
4864 332673 : df_set_bb_dirty (src);
4865 332673 : return ret;
4866 : }
4867 :
4868 10198110 : if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun)
4869 0 : && (e->flags & EDGE_FALLTHRU) && !(e->flags & EDGE_COMPLEX))
4870 : {
4871 0 : if (dump_file)
4872 0 : fprintf (dump_file, "Redirecting entry edge from bb %i to %i\n",
4873 : e->src->index, dest->index);
4874 :
4875 0 : df_set_bb_dirty (e->src);
4876 0 : redirect_edge_succ (e, dest);
4877 0 : return e;
4878 : }
4879 :
4880 : /* Redirect_edge_and_branch may decide to turn branch into fallthru edge
4881 : in the case the basic block appears to be in sequence. Avoid this
4882 : transformation. */
4883 :
4884 10198110 : if (e->flags & EDGE_FALLTHRU)
4885 : {
4886 : /* Redirect any branch edges unified with the fallthru one. */
4887 4880233 : if (JUMP_P (BB_END (src))
4888 4880233 : && label_is_jump_target_p (BB_HEAD (e->dest),
4889 : BB_END (src)))
4890 : {
4891 170 : edge redirected;
4892 :
4893 170 : if (dump_file)
4894 0 : fprintf (dump_file, "Fallthru edge unified with branch "
4895 : "%i->%i redirected to %i\n",
4896 0 : e->src->index, e->dest->index, dest->index);
4897 170 : e->flags &= ~EDGE_FALLTHRU;
4898 170 : redirected = redirect_branch_edge (e, dest);
4899 170 : gcc_assert (redirected);
4900 170 : redirected->flags |= EDGE_FALLTHRU;
4901 170 : df_set_bb_dirty (redirected->src);
4902 170 : return redirected;
4903 : }
4904 : /* In case we are redirecting fallthru edge to the branch edge
4905 : of conditional jump, remove it. */
4906 4880063 : if (EDGE_COUNT (src->succs) == 2)
4907 : {
4908 : /* Find the edge that is different from E. */
4909 3779011 : edge s = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e);
4910 :
4911 3779011 : if (s->dest == dest
4912 2 : && any_condjump_p (BB_END (src))
4913 3779011 : && onlyjump_p (BB_END (src)))
4914 0 : delete_insn (BB_END (src));
4915 : }
4916 4880063 : if (dump_file)
4917 2229 : fprintf (dump_file, "Redirecting fallthru edge %i->%i to %i\n",
4918 2229 : e->src->index, e->dest->index, dest->index);
4919 4880063 : ret = redirect_edge_succ_nodup (e, dest);
4920 : }
4921 : else
4922 5317877 : ret = redirect_branch_edge (e, dest);
4923 :
4924 10197940 : if (!ret)
4925 : return NULL;
4926 :
4927 10197940 : fixup_partition_crossing (ret);
4928 : /* We don't want simplejumps in the insn stream during cfglayout. */
4929 10197940 : gcc_assert (!simplejump_p (BB_END (src)) || CROSSING_JUMP_P (BB_END (src)));
4930 :
4931 10197940 : df_set_bb_dirty (src);
4932 10197940 : return ret;
4933 : }
4934 :
4935 : /* Simple wrapper as we always can redirect fallthru edges. */
4936 : static basic_block
4937 5012718 : cfg_layout_redirect_edge_and_branch_force (edge e, basic_block dest)
4938 : {
4939 5012718 : edge redirected = cfg_layout_redirect_edge_and_branch (e, dest);
4940 :
4941 5012718 : gcc_assert (redirected);
4942 5012718 : return NULL;
4943 : }
4944 :
4945 : /* Same as delete_basic_block but update cfg_layout structures. */
4946 :
4947 : static void
4948 5817821 : cfg_layout_delete_block (basic_block bb)
4949 : {
4950 5817821 : rtx_insn *insn, *next, *prev = PREV_INSN (BB_HEAD (bb)), *remaints;
4951 5817821 : rtx_insn **to;
4952 :
4953 5817821 : if (BB_HEADER (bb))
4954 : {
4955 101 : next = BB_HEAD (bb);
4956 101 : if (prev)
4957 101 : SET_NEXT_INSN (prev) = BB_HEADER (bb);
4958 : else
4959 0 : set_first_insn (BB_HEADER (bb));
4960 101 : SET_PREV_INSN (BB_HEADER (bb)) = prev;
4961 101 : insn = BB_HEADER (bb);
4962 142 : while (NEXT_INSN (insn))
4963 : insn = NEXT_INSN (insn);
4964 101 : SET_NEXT_INSN (insn) = next;
4965 101 : SET_PREV_INSN (next) = insn;
4966 : }
4967 5817821 : next = NEXT_INSN (BB_END (bb));
4968 5817821 : if (BB_FOOTER (bb))
4969 : {
4970 : insn = BB_FOOTER (bb);
4971 2853644 : while (insn)
4972 : {
4973 1426833 : if (BARRIER_P (insn))
4974 : {
4975 1426808 : if (PREV_INSN (insn))
4976 13 : SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
4977 : else
4978 1426795 : BB_FOOTER (bb) = NEXT_INSN (insn);
4979 1426808 : if (NEXT_INSN (insn))
4980 13 : SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
4981 : }
4982 1426833 : if (LABEL_P (insn))
4983 : break;
4984 1426833 : insn = NEXT_INSN (insn);
4985 : }
4986 1426811 : if (BB_FOOTER (bb))
4987 : {
4988 25 : insn = BB_END (bb);
4989 25 : SET_NEXT_INSN (insn) = BB_FOOTER (bb);
4990 25 : SET_PREV_INSN (BB_FOOTER (bb)) = insn;
4991 50 : while (NEXT_INSN (insn))
4992 : insn = NEXT_INSN (insn);
4993 25 : SET_NEXT_INSN (insn) = next;
4994 25 : if (next)
4995 16 : SET_PREV_INSN (next) = insn;
4996 : else
4997 9 : set_last_insn (insn);
4998 : }
4999 : }
5000 5817821 : if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
5001 5784994 : to = &BB_HEADER (bb->next_bb);
5002 : else
5003 : to = &cfg_layout_function_footer;
5004 :
5005 5817821 : rtl_delete_block (bb);
5006 :
5007 5817821 : if (prev)
5008 5817821 : prev = NEXT_INSN (prev);
5009 : else
5010 0 : prev = get_insns ();
5011 5817821 : if (next)
5012 5784991 : next = PREV_INSN (next);
5013 : else
5014 32830 : next = get_last_insn ();
5015 :
5016 11635642 : if (next && NEXT_INSN (next) != prev)
5017 : {
5018 1839 : remaints = unlink_insn_chain (prev, next);
5019 1839 : insn = remaints;
5020 4046 : while (NEXT_INSN (insn))
5021 : insn = NEXT_INSN (insn);
5022 1839 : SET_NEXT_INSN (insn) = *to;
5023 1839 : if (*to)
5024 359 : SET_PREV_INSN (*to) = insn;
5025 1839 : *to = remaints;
5026 : }
5027 5817821 : }
5028 :
5029 : /* Return true when blocks A and B can be safely merged. */
5030 :
5031 : static bool
5032 2989896 : cfg_layout_can_merge_blocks_p (basic_block a, basic_block b)
5033 : {
5034 : /* If we are partitioning hot/cold basic blocks, we don't want to
5035 : mess up unconditional or indirect jumps that cross between hot
5036 : and cold sections.
5037 :
5038 : Basic block partitioning may result in some jumps that appear to
5039 : be optimizable (or blocks that appear to be mergeable), but which really
5040 : must be left untouched (they are required to make it safely across
5041 : partition boundaries). See the comments at the top of
5042 : bb-reorder.cc:partition_hot_cold_basic_blocks for complete details. */
5043 :
5044 2989896 : if (BB_PARTITION (a) != BB_PARTITION (b))
5045 : return false;
5046 :
5047 : /* Protect the loop latches. */
5048 2827778 : if (current_loops && b->loop_father->latch == b)
5049 : return false;
5050 :
5051 : /* If we would end up moving B's instructions, make sure it doesn't fall
5052 : through into the exit block, since we cannot recover from a fallthrough
5053 : edge into the exit block occurring in the middle of a function. */
5054 2449710 : if (NEXT_INSN (BB_END (a)) != BB_HEAD (b))
5055 : {
5056 1497802 : edge e = find_fallthru_edge (b->succs);
5057 1497802 : if (e && e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
5058 : return false;
5059 : }
5060 :
5061 : /* There must be exactly one edge in between the blocks. */
5062 1775593 : return (single_succ_p (a)
5063 1775593 : && single_succ (a) == b
5064 1775593 : && single_pred_p (b) == 1
5065 1750209 : && a != b
5066 : /* Must be simple edge. */
5067 1750209 : && !(single_succ_edge (a)->flags & EDGE_COMPLEX)
5068 1750209 : && a != ENTRY_BLOCK_PTR_FOR_FN (cfun)
5069 1750209 : && b != EXIT_BLOCK_PTR_FOR_FN (cfun)
5070 : /* If the jump insn has side effects, we can't kill the edge.
5071 : When not optimizing, try_redirect_by_replacing_jump will
5072 : not allow us to redirect an edge by replacing a table jump. */
5073 3525802 : && (!JUMP_P (BB_END (a))
5074 64143 : || ((!optimize || reload_completed)
5075 53577 : ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
5076 : }
5077 :
5078 : /* Merge block A and B. The blocks must be mergeable. */
5079 :
5080 : static void
5081 873692 : cfg_layout_merge_blocks (basic_block a, basic_block b)
5082 : {
5083 : /* If B is a forwarder block whose outgoing edge has no location, we'll
5084 : propagate the locus of the edge between A and B onto it. */
5085 873692 : const bool forward_edge_locus
5086 873692 : = (b->flags & BB_FORWARDER_BLOCK) != 0
5087 873692 : && LOCATION_LOCUS (EDGE_SUCC (b, 0)->goto_locus) == UNKNOWN_LOCATION;
5088 873692 : rtx_insn *insn;
5089 :
5090 873692 : gcc_checking_assert (cfg_layout_can_merge_blocks_p (a, b));
5091 :
5092 873692 : if (dump_file)
5093 78 : fprintf (dump_file, "Merging block %d into block %d...\n", b->index,
5094 : a->index);
5095 :
5096 : /* If there was a CODE_LABEL beginning B, delete it. */
5097 873692 : if (LABEL_P (BB_HEAD (b)))
5098 : {
5099 382029 : delete_insn (BB_HEAD (b));
5100 : }
5101 :
5102 : /* We should have fallthru edge in a, or we can do dummy redirection to get
5103 : it cleaned up. */
5104 873692 : if (JUMP_P (BB_END (a)))
5105 25376 : try_redirect_by_replacing_jump (EDGE_SUCC (a, 0), b, true);
5106 873692 : gcc_assert (!JUMP_P (BB_END (a)));
5107 :
5108 : /* If not optimizing, preserve the locus of the single edge between
5109 : blocks A and B if necessary by emitting a nop. */
5110 873692 : if (!optimize
5111 2777 : && !forward_edge_locus
5112 876002 : && !DECL_IGNORED_P (current_function_decl))
5113 2310 : emit_nop_for_unique_locus_between (a, b);
5114 :
5115 : /* Move things from b->footer after a->footer. */
5116 873692 : if (BB_FOOTER (b))
5117 : {
5118 192285 : if (!BB_FOOTER (a))
5119 192284 : BB_FOOTER (a) = BB_FOOTER (b);
5120 : else
5121 : {
5122 : rtx_insn *last = BB_FOOTER (a);
5123 :
5124 1 : while (NEXT_INSN (last))
5125 : last = NEXT_INSN (last);
5126 1 : SET_NEXT_INSN (last) = BB_FOOTER (b);
5127 1 : SET_PREV_INSN (BB_FOOTER (b)) = last;
5128 : }
5129 192285 : BB_FOOTER (b) = NULL;
5130 : }
5131 :
5132 : /* Move things from b->header before a->footer.
5133 : Note that this may include dead tablejump data, but we don't clean
5134 : those up until we go out of cfglayout mode. */
5135 873692 : if (BB_HEADER (b))
5136 : {
5137 2291 : if (! BB_FOOTER (a))
5138 1186 : BB_FOOTER (a) = BB_HEADER (b);
5139 : else
5140 : {
5141 : rtx_insn *last = BB_HEADER (b);
5142 :
5143 1243 : while (NEXT_INSN (last))
5144 : last = NEXT_INSN (last);
5145 1105 : SET_NEXT_INSN (last) = BB_FOOTER (a);
5146 1105 : SET_PREV_INSN (BB_FOOTER (a)) = last;
5147 1105 : BB_FOOTER (a) = BB_HEADER (b);
5148 : }
5149 2291 : BB_HEADER (b) = NULL;
5150 : }
5151 :
5152 : /* In the case basic blocks are not adjacent, move them around. */
5153 873692 : if (NEXT_INSN (BB_END (a)) != BB_HEAD (b))
5154 : {
5155 406058 : insn = unlink_insn_chain (BB_HEAD (b), BB_END (b));
5156 :
5157 406058 : emit_insn_after_noloc (insn, BB_END (a), a);
5158 : }
5159 : /* Otherwise just re-associate the instructions. */
5160 : else
5161 : {
5162 467634 : insn = BB_HEAD (b);
5163 467634 : BB_END (a) = BB_END (b);
5164 : }
5165 :
5166 : /* emit_insn_after_noloc doesn't call df_insn_change_bb.
5167 : We need to explicitly call. */
5168 873692 : update_bb_for_insn_chain (insn, BB_END (b), a);
5169 :
5170 : /* Skip possible DELETED_LABEL insn. */
5171 873692 : if (!NOTE_INSN_BASIC_BLOCK_P (insn))
5172 0 : insn = NEXT_INSN (insn);
5173 873692 : gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
5174 873692 : BB_HEAD (b) = BB_END (b) = NULL;
5175 873692 : delete_insn (insn);
5176 :
5177 873692 : df_bb_delete (b->index);
5178 :
5179 873692 : if (forward_edge_locus)
5180 162962 : EDGE_SUCC (b, 0)->goto_locus = EDGE_SUCC (a, 0)->goto_locus;
5181 :
5182 873692 : if (dump_file)
5183 78 : fprintf (dump_file, "Merged blocks %d and %d.\n", a->index, b->index);
5184 873692 : }
5185 :
5186 : /* Split edge E. */
5187 :
5188 : static basic_block
5189 3672850 : cfg_layout_split_edge (edge e)
5190 : {
5191 3672850 : basic_block new_bb =
5192 7345700 : create_basic_block (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
5193 3672850 : ? NEXT_INSN (BB_END (e->src)) : get_insns (),
5194 : NULL_RTX, e->src);
5195 :
5196 3672850 : if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
5197 1 : BB_COPY_PARTITION (new_bb, e->src);
5198 : else
5199 3672849 : BB_COPY_PARTITION (new_bb, e->dest);
5200 3672850 : make_edge (new_bb, e->dest, EDGE_FALLTHRU);
5201 3672850 : redirect_edge_and_branch_force (e, new_bb);
5202 :
5203 3672850 : return new_bb;
5204 : }
5205 :
5206 : /* Do postprocessing after making a forwarder block joined by edge FALLTHRU. */
5207 :
5208 : static void
5209 1458 : rtl_make_forwarder_block (edge fallthru ATTRIBUTE_UNUSED)
5210 : {
5211 1458 : }
5212 :
5213 : /* Return true if BB contains only labels or non-executable
5214 : instructions. */
5215 :
5216 : static bool
5217 0 : rtl_block_empty_p (basic_block bb)
5218 : {
5219 0 : rtx_insn *insn;
5220 :
5221 0 : if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)
5222 0 : || bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
5223 : return true;
5224 :
5225 0 : FOR_BB_INSNS (bb, insn)
5226 0 : if (NONDEBUG_INSN_P (insn)
5227 0 : && (!any_uncondjump_p (insn) || !onlyjump_p (insn)))
5228 : return false;
5229 :
5230 : return true;
5231 : }
5232 :
5233 : /* Split a basic block if it ends with a conditional branch and if
5234 : the other part of the block is not empty. */
5235 :
5236 : static basic_block
5237 0 : rtl_split_block_before_cond_jump (basic_block bb)
5238 : {
5239 0 : rtx_insn *insn;
5240 0 : rtx_insn *split_point = NULL;
5241 0 : rtx_insn *last = NULL;
5242 0 : bool found_code = false;
5243 :
5244 0 : FOR_BB_INSNS (bb, insn)
5245 : {
5246 0 : if (any_condjump_p (insn))
5247 : split_point = last;
5248 0 : else if (NONDEBUG_INSN_P (insn))
5249 0 : found_code = true;
5250 0 : last = insn;
5251 : }
5252 :
5253 : /* Did not find everything. */
5254 0 : if (found_code && split_point)
5255 0 : return split_block (bb, split_point)->dest;
5256 : else
5257 : return NULL;
5258 : }
5259 :
5260 : /* Return true if BB ends with a call, possibly followed by some
5261 : instructions that must stay with the call, false otherwise. */
5262 :
5263 : static bool
5264 10033705 : rtl_block_ends_with_call_p (basic_block bb)
5265 : {
5266 10033705 : rtx_insn *insn = BB_END (bb);
5267 :
5268 10033705 : while (!CALL_P (insn)
5269 11413038 : && insn != BB_HEAD (bb)
5270 24157777 : && (keep_with_call_p (insn)
5271 11357997 : || NOTE_P (insn)
5272 11232975 : || DEBUG_INSN_P (insn)))
5273 2765714 : insn = PREV_INSN (insn);
5274 10033705 : return (CALL_P (insn));
5275 : }
5276 :
5277 : /* Return true if BB ends with a conditional branch, false otherwise. */
5278 :
5279 : static bool
5280 0 : rtl_block_ends_with_condjump_p (const_basic_block bb)
5281 : {
5282 0 : return any_condjump_p (BB_END (bb));
5283 : }
5284 :
5285 : /* Return true if we need to add fake edge to exit.
5286 : Helper function for rtl_flow_call_edges_add. */
5287 :
5288 : static bool
5289 0 : need_fake_edge_p (const rtx_insn *insn)
5290 : {
5291 0 : if (!INSN_P (insn))
5292 : return false;
5293 :
5294 0 : auto call = dyn_cast<const rtx_call_insn *> (insn);
5295 0 : if ((call
5296 0 : && !SIBLING_CALL_P (call)
5297 0 : && !find_reg_note (call, REG_NORETURN, NULL)
5298 0 : && !(RTL_CONST_OR_PURE_CALL_P (call))))
5299 : return true;
5300 :
5301 0 : return ((GET_CODE (PATTERN (insn)) == ASM_OPERANDS
5302 0 : && MEM_VOLATILE_P (PATTERN (insn)))
5303 0 : || (GET_CODE (PATTERN (insn)) == PARALLEL
5304 0 : && asm_noperands (insn) != -1
5305 0 : && MEM_VOLATILE_P (XVECEXP (PATTERN (insn), 0, 0)))
5306 0 : || GET_CODE (PATTERN (insn)) == ASM_INPUT);
5307 : }
5308 :
5309 : /* Add fake edges to the function exit for any non constant and non noreturn
5310 : calls, volatile inline assembly in the bitmap of blocks specified by
5311 : BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
5312 : that were split.
5313 :
5314 : The goal is to expose cases in which entering a basic block does not imply
5315 : that all subsequent instructions must be executed. */
5316 :
5317 : static int
5318 0 : rtl_flow_call_edges_add (sbitmap blocks)
5319 : {
5320 0 : int i;
5321 0 : int blocks_split = 0;
5322 0 : int last_bb = last_basic_block_for_fn (cfun);
5323 0 : bool check_last_block = false;
5324 :
5325 0 : if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS)
5326 : return 0;
5327 :
5328 0 : if (! blocks)
5329 : check_last_block = true;
5330 : else
5331 0 : check_last_block = bitmap_bit_p (blocks,
5332 0 : EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb->index);
5333 :
5334 : /* In the last basic block, before epilogue generation, there will be
5335 : a fallthru edge to EXIT. Special care is required if the last insn
5336 : of the last basic block is a call because make_edge folds duplicate
5337 : edges, which would result in the fallthru edge also being marked
5338 : fake, which would result in the fallthru edge being removed by
5339 : remove_fake_edges, which would result in an invalid CFG.
5340 :
5341 : Moreover, we can't elide the outgoing fake edge, since the block
5342 : profiler needs to take this into account in order to solve the minimal
5343 : spanning tree in the case that the call doesn't return.
5344 :
5345 : Handle this by adding a dummy instruction in a new last basic block. */
5346 0 : if (check_last_block)
5347 : {
5348 0 : basic_block bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
5349 0 : rtx_insn *insn = BB_END (bb);
5350 :
5351 : /* Back up past insns that must be kept in the same block as a call. */
5352 0 : while (insn != BB_HEAD (bb)
5353 0 : && keep_with_call_p (insn))
5354 0 : insn = PREV_INSN (insn);
5355 :
5356 0 : if (need_fake_edge_p (insn))
5357 : {
5358 0 : edge e;
5359 :
5360 0 : e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
5361 0 : if (e)
5362 : {
5363 0 : insert_insn_on_edge (gen_use (const0_rtx), e);
5364 0 : commit_edge_insertions ();
5365 : }
5366 : }
5367 : }
5368 :
5369 : /* Now add fake edges to the function exit for any non constant
5370 : calls since there is no way that we can determine if they will
5371 : return or not... */
5372 :
5373 0 : for (i = NUM_FIXED_BLOCKS; i < last_bb; i++)
5374 : {
5375 0 : basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
5376 0 : rtx_insn *insn;
5377 0 : rtx_insn *prev_insn;
5378 :
5379 0 : if (!bb)
5380 0 : continue;
5381 :
5382 0 : if (blocks && !bitmap_bit_p (blocks, i))
5383 0 : continue;
5384 :
5385 0 : for (insn = BB_END (bb); ; insn = prev_insn)
5386 : {
5387 0 : prev_insn = PREV_INSN (insn);
5388 0 : if (need_fake_edge_p (insn))
5389 : {
5390 0 : edge e;
5391 0 : rtx_insn *split_at_insn = insn;
5392 :
5393 : /* Don't split the block between a call and an insn that should
5394 : remain in the same block as the call. */
5395 0 : if (CALL_P (insn))
5396 0 : while (split_at_insn != BB_END (bb)
5397 0 : && keep_with_call_p (NEXT_INSN (split_at_insn)))
5398 0 : split_at_insn = NEXT_INSN (split_at_insn);
5399 :
5400 : /* The handling above of the final block before the epilogue
5401 : should be enough to verify that there is no edge to the exit
5402 : block in CFG already. Calling make_edge in such case would
5403 : cause us to mark that edge as fake and remove it later. */
5404 :
5405 0 : if (flag_checking && split_at_insn == BB_END (bb))
5406 : {
5407 0 : e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
5408 0 : gcc_assert (e == NULL);
5409 : }
5410 :
5411 : /* Note that the following may create a new basic block
5412 : and renumber the existing basic blocks. */
5413 0 : if (split_at_insn != BB_END (bb))
5414 : {
5415 0 : e = split_block (bb, split_at_insn);
5416 0 : if (e)
5417 0 : blocks_split++;
5418 : }
5419 :
5420 0 : edge ne = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FAKE);
5421 0 : ne->probability = profile_probability::guessed_never ();
5422 : }
5423 :
5424 0 : if (insn == BB_HEAD (bb))
5425 : break;
5426 : }
5427 : }
5428 :
5429 0 : if (blocks_split)
5430 0 : verify_flow_info ();
5431 :
5432 : return blocks_split;
5433 : }
5434 :
5435 : /* Add COMP_RTX as a condition at end of COND_BB. FIRST_HEAD is
5436 : the conditional branch target, SECOND_HEAD should be the fall-thru
5437 : there is no need to handle this here the loop versioning code handles
5438 : this. the reason for SECON_HEAD is that it is needed for condition
5439 : in trees, and this should be of the same type since it is a hook. */
5440 : static void
5441 0 : rtl_lv_add_condition_to_bb (basic_block first_head ,
5442 : basic_block second_head ATTRIBUTE_UNUSED,
5443 : basic_block cond_bb, void *comp_rtx)
5444 : {
5445 0 : rtx_code_label *label;
5446 0 : rtx_insn *seq, *jump;
5447 0 : rtx op0 = XEXP ((rtx)comp_rtx, 0);
5448 0 : rtx op1 = XEXP ((rtx)comp_rtx, 1);
5449 0 : enum rtx_code comp = GET_CODE ((rtx)comp_rtx);
5450 0 : machine_mode mode;
5451 :
5452 :
5453 0 : label = block_label (first_head);
5454 0 : mode = GET_MODE (op0);
5455 0 : if (mode == VOIDmode)
5456 0 : mode = GET_MODE (op1);
5457 :
5458 0 : start_sequence ();
5459 0 : op0 = force_operand (op0, NULL_RTX);
5460 0 : op1 = force_operand (op1, NULL_RTX);
5461 0 : do_compare_rtx_and_jump (op0, op1, comp, 0, mode, NULL_RTX, NULL, label,
5462 : profile_probability::uninitialized ());
5463 0 : jump = get_last_insn ();
5464 0 : JUMP_LABEL (jump) = label;
5465 0 : LABEL_NUSES (label)++;
5466 0 : seq = end_sequence ();
5467 :
5468 : /* Add the new cond, in the new head. */
5469 0 : emit_insn_after (seq, BB_END (cond_bb));
5470 0 : }
5471 :
5472 :
5473 : /* Given a block B with unconditional branch at its end, get the
5474 : store the return the branch edge and the fall-thru edge in
5475 : BRANCH_EDGE and FALLTHRU_EDGE respectively. */
5476 : static void
5477 0 : rtl_extract_cond_bb_edges (basic_block b, edge *branch_edge,
5478 : edge *fallthru_edge)
5479 : {
5480 0 : edge e = EDGE_SUCC (b, 0);
5481 :
5482 0 : if (e->flags & EDGE_FALLTHRU)
5483 : {
5484 0 : *fallthru_edge = e;
5485 0 : *branch_edge = EDGE_SUCC (b, 1);
5486 : }
5487 : else
5488 : {
5489 0 : *branch_edge = e;
5490 0 : *fallthru_edge = EDGE_SUCC (b, 1);
5491 : }
5492 0 : }
5493 :
5494 : void
5495 29507765 : init_rtl_bb_info (basic_block bb)
5496 : {
5497 29507765 : gcc_assert (!bb->il.x.rtl);
5498 29507765 : bb->il.x.head_ = NULL;
5499 29507765 : bb->il.x.rtl = ggc_cleared_alloc<rtl_bb_info> ();
5500 29507765 : }
5501 :
5502 : static bool
5503 2192 : rtl_bb_info_initialized_p (basic_block bb)
5504 : {
5505 2192 : return bb->il.x.rtl;
5506 : }
5507 :
5508 : /* Returns true if it is possible to remove edge E by redirecting
5509 : it to the destination of the other edge from E->src. */
5510 :
5511 : static bool
5512 151981 : rtl_can_remove_branch_p (const_edge e)
5513 : {
5514 151981 : const_basic_block src = e->src;
5515 151981 : const_basic_block target = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest;
5516 151981 : const rtx_insn *insn = BB_END (src);
5517 151981 : rtx set;
5518 :
5519 : /* The conditions are taken from try_redirect_by_replacing_jump. */
5520 151981 : if (target == EXIT_BLOCK_PTR_FOR_FN (cfun))
5521 : return false;
5522 :
5523 151981 : if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
5524 : return false;
5525 :
5526 151981 : if (BB_PARTITION (src) != BB_PARTITION (target))
5527 : return false;
5528 :
5529 151981 : if (!onlyjump_p (insn)
5530 151981 : || tablejump_p (insn, NULL, NULL))
5531 : return false;
5532 :
5533 151981 : set = single_set (insn);
5534 151981 : if (!set || side_effects_p (set))
5535 0 : return false;
5536 :
5537 : return true;
5538 : }
5539 :
5540 : static basic_block
5541 44475 : rtl_duplicate_bb (basic_block bb, copy_bb_data *id)
5542 : {
5543 44475 : bb = cfg_layout_duplicate_bb (bb, id);
5544 44475 : bb->aux = NULL;
5545 44475 : return bb;
5546 : }
5547 :
5548 : /* Do book-keeping of basic block BB for the profile consistency checker.
5549 : Store the counting in RECORD. */
5550 : static void
5551 0 : rtl_account_profile_record (basic_block bb, struct profile_record *record)
5552 : {
5553 0 : rtx_insn *insn;
5554 0 : FOR_BB_INSNS (bb, insn)
5555 0 : if (INSN_P (insn))
5556 : {
5557 0 : record->size += insn_cost (insn, false);
5558 0 : if (profile_info)
5559 : {
5560 0 : if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.ipa ().initialized_p ()
5561 0 : && ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.ipa ().nonzero_p ()
5562 0 : && bb->count.ipa ().initialized_p ())
5563 0 : record->time
5564 0 : += insn_cost (insn, true) * bb->count.ipa ().to_gcov_type ();
5565 : }
5566 0 : else if (bb->count.initialized_p ()
5567 0 : && ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.initialized_p ())
5568 0 : record->time
5569 0 : += insn_cost (insn, true)
5570 0 : * bb->count.to_sreal_scale
5571 0 : (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count).to_double ();
5572 : else
5573 0 : record->time += insn_cost (insn, true);
5574 : }
5575 0 : }
5576 :
5577 : /* Implementation of CFG manipulation for linearized RTL. */
5578 : const struct cfg_hooks rtl_cfg_hooks = {
5579 : IR_RTL_CFGRTL,
5580 : rtl_verify_flow_info,
5581 : rtl_dump_bb,
5582 : rtl_dump_bb_for_graph,
5583 : rtl_dump_bb_as_sarif_properties,
5584 : rtl_create_basic_block,
5585 : rtl_redirect_edge_and_branch,
5586 : rtl_redirect_edge_and_branch_force,
5587 : rtl_can_remove_branch_p,
5588 : rtl_delete_block,
5589 : rtl_split_block,
5590 : rtl_move_block_after,
5591 : rtl_can_merge_blocks, /* can_merge_blocks_p */
5592 : rtl_merge_blocks,
5593 : rtl_predict_edge,
5594 : rtl_predicted_by_p,
5595 : cfg_layout_can_duplicate_bb_p,
5596 : rtl_duplicate_bb,
5597 : rtl_split_edge,
5598 : rtl_make_forwarder_block,
5599 : rtl_tidy_fallthru_edge,
5600 : rtl_force_nonfallthru,
5601 : rtl_block_ends_with_call_p,
5602 : rtl_block_ends_with_condjump_p,
5603 : rtl_flow_call_edges_add,
5604 : NULL, /* execute_on_growing_pred */
5605 : NULL, /* execute_on_shrinking_pred */
5606 : NULL, /* duplicate loop for trees */
5607 : NULL, /* lv_add_condition_to_bb */
5608 : NULL, /* lv_adjust_loop_header_phi*/
5609 : NULL, /* extract_cond_bb_edges */
5610 : NULL, /* flush_pending_stmts */
5611 : rtl_block_empty_p, /* block_empty_p */
5612 : rtl_split_block_before_cond_jump, /* split_block_before_cond_jump */
5613 : rtl_account_profile_record,
5614 : };
5615 :
5616 : /* Implementation of CFG manipulation for cfg layout RTL, where
5617 : basic block connected via fallthru edges does not have to be adjacent.
5618 : This representation will hopefully become the default one in future
5619 : version of the compiler. */
5620 :
5621 : const struct cfg_hooks cfg_layout_rtl_cfg_hooks = {
5622 : IR_RTL_CFGLAYOUT,
5623 : rtl_verify_flow_info_1,
5624 : rtl_dump_bb,
5625 : rtl_dump_bb_for_graph,
5626 : rtl_dump_bb_as_sarif_properties,
5627 : cfg_layout_create_basic_block,
5628 : cfg_layout_redirect_edge_and_branch,
5629 : cfg_layout_redirect_edge_and_branch_force,
5630 : rtl_can_remove_branch_p,
5631 : cfg_layout_delete_block,
5632 : cfg_layout_split_block,
5633 : rtl_move_block_after,
5634 : cfg_layout_can_merge_blocks_p,
5635 : cfg_layout_merge_blocks,
5636 : rtl_predict_edge,
5637 : rtl_predicted_by_p,
5638 : cfg_layout_can_duplicate_bb_p,
5639 : cfg_layout_duplicate_bb,
5640 : cfg_layout_split_edge,
5641 : rtl_make_forwarder_block,
5642 : NULL, /* tidy_fallthru_edge */
5643 : rtl_force_nonfallthru,
5644 : rtl_block_ends_with_call_p,
5645 : rtl_block_ends_with_condjump_p,
5646 : rtl_flow_call_edges_add,
5647 : NULL, /* execute_on_growing_pred */
5648 : NULL, /* execute_on_shrinking_pred */
5649 : duplicate_loop_body_to_header_edge, /* duplicate loop for rtl */
5650 : rtl_lv_add_condition_to_bb, /* lv_add_condition_to_bb */
5651 : NULL, /* lv_adjust_loop_header_phi*/
5652 : rtl_extract_cond_bb_edges, /* extract_cond_bb_edges */
5653 : NULL, /* flush_pending_stmts */
5654 : rtl_block_empty_p, /* block_empty_p */
5655 : rtl_split_block_before_cond_jump, /* split_block_before_cond_jump */
5656 : rtl_account_profile_record,
5657 : };
5658 :
5659 : #include "gt-cfgrtl.h"
5660 :
5661 : #if __GNUC__ >= 10
5662 : # pragma GCC diagnostic pop
5663 : #endif
|