Branch data Line data Source code
1 : : /* CFG cleanup for trees.
2 : : Copyright (C) 2001-2025 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
7 : : it under the terms of the GNU General Public License as published by
8 : : the Free Software Foundation; either version 3, or (at your option)
9 : : any later version.
10 : :
11 : : GCC is distributed in the hope that it will be useful,
12 : : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : : GNU General Public License 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 : : #include "config.h"
21 : : #include "system.h"
22 : : #include "coretypes.h"
23 : : #include "backend.h"
24 : : #include "rtl.h"
25 : : #include "tree.h"
26 : : #include "gimple.h"
27 : : #include "cfghooks.h"
28 : : #include "tree-pass.h"
29 : : #include "ssa.h"
30 : : #include "diagnostic-core.h"
31 : : #include "fold-const.h"
32 : : #include "cfganal.h"
33 : : #include "cfgcleanup.h"
34 : : #include "tree-eh.h"
35 : : #include "gimplify.h"
36 : : #include "gimple-iterator.h"
37 : : #include "tree-cfg.h"
38 : : #include "tree-ssa-loop-manip.h"
39 : : #include "tree-dfa.h"
40 : : #include "tree-ssa.h"
41 : : #include "cfgloop.h"
42 : : #include "tree-scalar-evolution.h"
43 : : #include "gimple-match.h"
44 : : #include "gimple-fold.h"
45 : : #include "tree-ssa-loop-niter.h"
46 : : #include "cgraph.h"
47 : : #include "tree-into-ssa.h"
48 : : #include "tree-cfgcleanup.h"
49 : :
50 : :
51 : : /* The set of blocks in that at least one of the following changes happened:
52 : : -- the statement at the end of the block was changed
53 : : -- the block was newly created
54 : : -- the set of the predecessors of the block changed
55 : : -- the set of the successors of the block changed
56 : : ??? Maybe we could track these changes separately, since they determine
57 : : what cleanups it makes sense to try on the block. */
58 : : bitmap cfgcleanup_altered_bbs;
59 : :
60 : : /* Remove any fallthru edge from EV. Return true if an edge was removed. */
61 : :
62 : : static bool
63 : 21629284 : remove_fallthru_edge (vec<edge, va_gc> *ev)
64 : : {
65 : 21629284 : edge_iterator ei;
66 : 21629284 : edge e;
67 : :
68 : 24421406 : FOR_EACH_EDGE (e, ei, ev)
69 : 2828151 : if ((e->flags & EDGE_FALLTHRU) != 0)
70 : : {
71 : 36029 : if (e->flags & EDGE_COMPLEX)
72 : 0 : e->flags &= ~EDGE_FALLTHRU;
73 : : else
74 : 36029 : remove_edge_and_dominated_blocks (e);
75 : 36029 : return true;
76 : : }
77 : : return false;
78 : : }
79 : :
80 : : /* Convert a SWTCH with single non-default case to gcond and replace it
81 : : at GSI. */
82 : :
83 : : static bool
84 : 754423 : convert_single_case_switch (gswitch *swtch, gimple_stmt_iterator &gsi)
85 : : {
86 : 754423 : if (gimple_switch_num_labels (swtch) != 2)
87 : : return false;
88 : :
89 : 11187 : tree index = gimple_switch_index (swtch);
90 : 11187 : tree label = gimple_switch_label (swtch, 1);
91 : 11187 : tree low = CASE_LOW (label);
92 : 11187 : tree high = CASE_HIGH (label);
93 : :
94 : 11187 : basic_block default_bb = gimple_switch_default_bb (cfun, swtch);
95 : 11187 : basic_block case_bb = label_to_block (cfun, CASE_LABEL (label));
96 : :
97 : 11187 : basic_block bb = gimple_bb (swtch);
98 : 11187 : gcond *cond;
99 : :
100 : : /* Replace switch statement with condition statement. */
101 : 11187 : if (high)
102 : : {
103 : 996 : tree lhs, rhs;
104 : 996 : if (range_check_type (TREE_TYPE (index)) == NULL_TREE)
105 : 0 : return false;
106 : 996 : generate_range_test (bb, index, low, high, &lhs, &rhs);
107 : 996 : cond = gimple_build_cond (LE_EXPR, lhs, rhs, NULL_TREE, NULL_TREE);
108 : : }
109 : : else
110 : 10191 : cond = gimple_build_cond (EQ_EXPR, index,
111 : 10191 : fold_convert (TREE_TYPE (index), low),
112 : : NULL_TREE, NULL_TREE);
113 : :
114 : 11187 : gsi_replace (&gsi, cond, true);
115 : :
116 : : /* Update edges. */
117 : 11187 : edge case_edge = find_edge (bb, case_bb);
118 : 11187 : edge default_edge = find_edge (bb, default_bb);
119 : :
120 : 11187 : case_edge->flags |= EDGE_TRUE_VALUE;
121 : 11187 : default_edge->flags |= EDGE_FALSE_VALUE;
122 : 11187 : return true;
123 : : }
124 : :
125 : : /* Disconnect an unreachable block in the control expression starting
126 : : at block BB. */
127 : :
128 : : static bool
129 : 152367276 : cleanup_control_expr_graph (basic_block bb, gimple_stmt_iterator gsi)
130 : : {
131 : 152367276 : edge taken_edge;
132 : 152367276 : bool retval = false;
133 : 152367276 : gimple *stmt = gsi_stmt (gsi);
134 : :
135 : 152367276 : if (!single_succ_p (bb))
136 : : {
137 : 152361837 : edge e;
138 : 152361837 : edge_iterator ei;
139 : 152361837 : bool warned;
140 : 152361837 : tree val = NULL_TREE;
141 : :
142 : : /* Try to convert a switch with just a single non-default case to
143 : : GIMPLE condition. */
144 : 152361837 : if (gimple_code (stmt) == GIMPLE_SWITCH
145 : 152361837 : && convert_single_case_switch (as_a<gswitch *> (stmt), gsi))
146 : 11187 : stmt = gsi_stmt (gsi);
147 : :
148 : 152361837 : fold_defer_overflow_warnings ();
149 : 152361837 : switch (gimple_code (stmt))
150 : : {
151 : 151618601 : case GIMPLE_COND:
152 : 151618601 : {
153 : 151618601 : gimple_match_op res_op;
154 : 151618601 : if (gimple_simplify (stmt, &res_op, NULL, no_follow_ssa_edges,
155 : : no_follow_ssa_edges)
156 : 151618601 : && res_op.code == INTEGER_CST)
157 : 2228728 : val = res_op.ops[0];
158 : : }
159 : 151618601 : break;
160 : :
161 : 743236 : case GIMPLE_SWITCH:
162 : 743236 : val = gimple_switch_index (as_a <gswitch *> (stmt));
163 : 743236 : break;
164 : :
165 : 152361837 : default:
166 : 152361837 : ;
167 : : }
168 : 152361837 : taken_edge = find_taken_edge (bb, val);
169 : 152361837 : if (!taken_edge)
170 : : {
171 : 150122049 : fold_undefer_and_ignore_overflow_warnings ();
172 : 150122049 : return false;
173 : : }
174 : :
175 : : /* Remove all the edges except the one that is always executed. */
176 : 2239788 : warned = false;
177 : 6749016 : for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
178 : : {
179 : 4509228 : if (e != taken_edge)
180 : : {
181 : 2269440 : if (!warned)
182 : : {
183 : 2239788 : fold_undefer_overflow_warnings
184 : 2239788 : (true, stmt, WARN_STRICT_OVERFLOW_CONDITIONAL);
185 : 2239788 : warned = true;
186 : : }
187 : :
188 : 2269440 : taken_edge->probability += e->probability;
189 : 2269440 : remove_edge_and_dominated_blocks (e);
190 : 2269440 : retval = true;
191 : : }
192 : : else
193 : 2239788 : ei_next (&ei);
194 : : }
195 : 2239788 : if (!warned)
196 : 0 : fold_undefer_and_ignore_overflow_warnings ();
197 : : }
198 : : else
199 : 5439 : taken_edge = single_succ_edge (bb);
200 : :
201 : 2245227 : bitmap_set_bit (cfgcleanup_altered_bbs, bb->index);
202 : 2245227 : gsi_remove (&gsi, true);
203 : 2245227 : taken_edge->flags = EDGE_FALLTHRU;
204 : :
205 : 2245227 : return retval;
206 : : }
207 : :
208 : : /* Cleanup the GF_CALL_CTRL_ALTERING flag according to
209 : : to updated gimple_call_flags. */
210 : :
211 : : static void
212 : 347983339 : cleanup_call_ctrl_altering_flag (basic_block bb, gimple *bb_end)
213 : : {
214 : 347983339 : if (!is_gimple_call (bb_end)
215 : 66729027 : || !gimple_call_ctrl_altering_p (bb_end)
216 : 370107584 : || (/* IFN_UNIQUE should be the last insn, to make checking for it
217 : : as cheap as possible. */
218 : 22124245 : gimple_call_internal_p (bb_end)
219 : 463612 : && gimple_call_internal_unique_p (bb_end)))
220 : : return;
221 : :
222 : 21688828 : int flags = gimple_call_flags (bb_end);
223 : 21688828 : if (!(flags & ECF_NORETURN)
224 : 85907 : && (((flags & (ECF_CONST | ECF_PURE))
225 : 2111 : && !(flags & ECF_LOOPING_CONST_OR_PURE))
226 : 85779 : || (flags & ECF_LEAF)))
227 : 128 : gimple_call_set_ctrl_altering (bb_end, false);
228 : : else
229 : : {
230 : 21688700 : edge_iterator ei;
231 : 21688700 : edge e;
232 : 21688700 : bool found = false;
233 : 24554330 : FOR_EACH_EDGE (e, ei, bb->succs)
234 : 2988065 : if (e->flags & EDGE_FALLTHRU)
235 : : found = true;
236 : 2871052 : else if (e->flags & EDGE_ABNORMAL)
237 : : {
238 : : found = false;
239 : : break;
240 : : }
241 : : /* If there's no abnormal edge and a fallthru edge the call
242 : : isn't control-altering anymore. */
243 : 21688700 : if (found)
244 : 33539 : gimple_call_set_ctrl_altering (bb_end, false);
245 : : }
246 : : }
247 : :
248 : : /* Try to remove superfluous control structures in basic block BB. Returns
249 : : true if anything changes. */
250 : :
251 : : static bool
252 : 388546913 : cleanup_control_flow_bb (basic_block bb)
253 : : {
254 : 388546913 : gimple_stmt_iterator gsi;
255 : 388546913 : bool retval = false;
256 : 388546913 : gimple *stmt;
257 : :
258 : : /* If the last statement of the block could throw and now cannot,
259 : : we need to prune cfg. */
260 : 388546913 : retval |= gimple_purge_dead_eh_edges (bb);
261 : :
262 : 388546913 : gsi = gsi_last_nondebug_bb (bb);
263 : 388546913 : if (gsi_end_p (gsi))
264 : : return retval;
265 : :
266 : 347983339 : stmt = gsi_stmt (gsi);
267 : :
268 : : /* Try to cleanup ctrl altering flag for call which ends bb. */
269 : 347983339 : cleanup_call_ctrl_altering_flag (bb, stmt);
270 : :
271 : 347983339 : if (gimple_code (stmt) == GIMPLE_COND
272 : 347983339 : || gimple_code (stmt) == GIMPLE_SWITCH)
273 : : {
274 : 304734552 : gcc_checking_assert (gsi_stmt (gsi_last_bb (bb)) == stmt);
275 : 152367276 : retval |= cleanup_control_expr_graph (bb, gsi);
276 : : }
277 : 195616063 : else if (gimple_code (stmt) == GIMPLE_GOTO
278 : 6639 : && TREE_CODE (gimple_goto_dest (stmt)) == ADDR_EXPR
279 : 195616295 : && (TREE_CODE (TREE_OPERAND (gimple_goto_dest (stmt), 0))
280 : : == LABEL_DECL))
281 : : {
282 : : /* If we had a computed goto which has a compile-time determinable
283 : : destination, then we can eliminate the goto. */
284 : 163 : edge e;
285 : 163 : tree label;
286 : 163 : edge_iterator ei;
287 : 163 : basic_block target_block;
288 : :
289 : 326 : gcc_checking_assert (gsi_stmt (gsi_last_bb (bb)) == stmt);
290 : : /* First look at all the outgoing edges. Delete any outgoing
291 : : edges which do not go to the right block. For the one
292 : : edge which goes to the right block, fix up its flags. */
293 : 163 : label = TREE_OPERAND (gimple_goto_dest (stmt), 0);
294 : 163 : if (DECL_CONTEXT (label) != cfun->decl)
295 : 0 : return retval;
296 : 163 : target_block = label_to_block (cfun, label);
297 : 411 : for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
298 : : {
299 : 248 : if (e->dest != target_block)
300 : 91 : remove_edge_and_dominated_blocks (e);
301 : : else
302 : : {
303 : : /* Turn off the EDGE_ABNORMAL flag. */
304 : 157 : e->flags &= ~EDGE_ABNORMAL;
305 : :
306 : : /* And set EDGE_FALLTHRU. */
307 : 157 : e->flags |= EDGE_FALLTHRU;
308 : 157 : ei_next (&ei);
309 : : }
310 : : }
311 : :
312 : 163 : bitmap_set_bit (cfgcleanup_altered_bbs, bb->index);
313 : 163 : bitmap_set_bit (cfgcleanup_altered_bbs, target_block->index);
314 : :
315 : : /* Remove the GOTO_EXPR as it is not needed. The CFG has all the
316 : : relevant information we need. */
317 : 163 : gsi_remove (&gsi, true);
318 : 163 : retval = true;
319 : : }
320 : :
321 : : /* Check for indirect calls that have been turned into
322 : : noreturn calls. */
323 : 195615900 : else if (is_gimple_call (stmt)
324 : 195615900 : && gimple_call_noreturn_p (stmt))
325 : : {
326 : : /* If there are debug stmts after the noreturn call, remove them
327 : : now, they should be all unreachable anyway. */
328 : 21629461 : for (gsi_next (&gsi); !gsi_end_p (gsi); )
329 : 177 : gsi_remove (&gsi, true);
330 : 21629284 : if (remove_fallthru_edge (bb->succs))
331 : 36029 : retval = true;
332 : 21629284 : tree lhs = gimple_call_lhs (stmt);
333 : 21629284 : if (!lhs
334 : 21629284 : || !should_remove_lhs_p (lhs))
335 : 21629265 : gimple_call_set_ctrl_altering (stmt, true);
336 : : }
337 : :
338 : : return retval;
339 : : }
340 : :
341 : : /* Return true if basic block BB does nothing except pass control
342 : : flow to another block and that we can safely insert a label at
343 : : the start of the successor block.
344 : :
345 : : As a precondition, we require that BB be not equal to
346 : : the entry block. */
347 : :
348 : : static bool
349 : 413290473 : tree_forwarder_block_p (basic_block bb, bool phi_wanted)
350 : : {
351 : 413290473 : gimple_stmt_iterator gsi;
352 : 413290473 : location_t locus;
353 : :
354 : : /* BB must have a single outgoing edge. */
355 : 784721820 : if (single_succ_p (bb) != 1
356 : : /* If PHI_WANTED is false, BB must not have any PHI nodes.
357 : : Otherwise, BB must have PHI nodes. */
358 : 194412937 : || gimple_seq_empty_p (phi_nodes (bb)) == phi_wanted
359 : : /* BB may not be a predecessor of the exit block. */
360 : 147496405 : || single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun)
361 : : /* Nor should this be an infinite loop. */
362 : 126696664 : || single_succ (bb) == bb
363 : : /* BB may not have an abnormal outgoing edge. */
364 : 528267644 : || (single_succ_edge (bb)->flags & EDGE_ABNORMAL))
365 : : return false;
366 : :
367 : 126599606 : gcc_checking_assert (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun));
368 : :
369 : 126599606 : locus = single_succ_edge (bb)->goto_locus;
370 : :
371 : : /* There should not be an edge coming from entry, or an EH edge. */
372 : 126599606 : {
373 : 126599606 : edge_iterator ei;
374 : 126599606 : edge e;
375 : :
376 : 240577748 : FOR_EACH_EDGE (e, ei, bb->preds)
377 : 133810068 : if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun) || (e->flags & EDGE_EH))
378 : 19831926 : return false;
379 : : /* If goto_locus of any of the edges differs, prevent removing
380 : : the forwarder block when not optimizing. */
381 : 115154270 : else if (!optimize
382 : 4853259 : && (LOCATION_LOCUS (e->goto_locus) != UNKNOWN_LOCATION
383 : 4309752 : || LOCATION_LOCUS (locus) != UNKNOWN_LOCATION)
384 : 116498159 : && e->goto_locus != locus)
385 : : return false;
386 : : }
387 : :
388 : : /* Now walk through the statements backward. We can ignore labels,
389 : : anything else means this is not a forwarder block. */
390 : 393981396 : for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
391 : : {
392 : 165703013 : gimple *stmt = gsi_stmt (gsi);
393 : :
394 : 165703013 : switch (gimple_code (stmt))
395 : : {
396 : 700310 : case GIMPLE_LABEL:
397 : 700310 : if (DECL_NONLOCAL (gimple_label_label (as_a <glabel *> (stmt))))
398 : : return false;
399 : 697096 : if (!optimize
400 : 34141 : && (gimple_has_location (stmt)
401 : 5679 : || LOCATION_LOCUS (locus) != UNKNOWN_LOCATION)
402 : 728814 : && gimple_location (stmt) != locus)
403 : : return false;
404 : : break;
405 : :
406 : : /* ??? For now, hope there's a corresponding debug
407 : : assignment at the destination. */
408 : : case GIMPLE_DEBUG:
409 : : break;
410 : :
411 : : default:
412 : : return false;
413 : : }
414 : : }
415 : :
416 : 31287685 : if (current_loops)
417 : : {
418 : 30417080 : basic_block dest;
419 : : /* Protect loop headers. */
420 : 30417080 : if (bb_loop_header_p (bb))
421 : : return false;
422 : :
423 : 30342871 : dest = EDGE_SUCC (bb, 0)->dest;
424 : : /* Protect loop preheaders and latches if requested. */
425 : 30342871 : if (dest->loop_father->header == dest)
426 : : {
427 : 10303761 : if (bb->loop_father == dest->loop_father)
428 : : {
429 : 6521765 : if (loops_state_satisfies_p (LOOPS_HAVE_SIMPLE_LATCHES))
430 : : return false;
431 : : /* If bb doesn't have a single predecessor we'd make this
432 : : loop have multiple latches. Don't do that if that
433 : : would in turn require disambiguating them. */
434 : 5606990 : return (single_pred_p (bb)
435 : 5538510 : || loops_state_satisfies_p
436 : 68480 : (LOOPS_MAY_HAVE_MULTIPLE_LATCHES));
437 : : }
438 : 3781996 : else if (bb->loop_father == loop_outer (dest->loop_father))
439 : 3772163 : return !loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS);
440 : : /* Always preserve other edges into loop headers that are
441 : : not simple latches or preheaders. */
442 : : return false;
443 : : }
444 : : }
445 : :
446 : : return true;
447 : : }
448 : :
449 : : /* If all the PHI nodes in DEST have alternatives for E1 and E2 and
450 : : those alternatives are equal in each of the PHI nodes, then return
451 : : true, else return false. */
452 : :
453 : : bool
454 : 3242548 : phi_alternatives_equal (basic_block dest, edge e1, edge e2)
455 : : {
456 : 3242548 : int n1 = e1->dest_idx;
457 : 3242548 : int n2 = e2->dest_idx;
458 : 3242548 : gphi_iterator gsi;
459 : :
460 : 3516370 : for (gsi = gsi_start_phis (dest); !gsi_end_p (gsi); gsi_next (&gsi))
461 : : {
462 : 3476025 : gphi *phi = gsi.phi ();
463 : 3476025 : tree val1 = gimple_phi_arg_def (phi, n1);
464 : 3476025 : tree val2 = gimple_phi_arg_def (phi, n2);
465 : :
466 : 3476025 : gcc_assert (val1 != NULL_TREE);
467 : 3476025 : gcc_assert (val2 != NULL_TREE);
468 : :
469 : 3476025 : if (!operand_equal_for_phi_arg_p (val1, val2))
470 : : return false;
471 : : }
472 : :
473 : : return true;
474 : : }
475 : :
476 : : /* Move debug stmts from the forwarder block SRC to DEST or PRED. */
477 : :
478 : : static void
479 : 26701015 : move_debug_stmts_from_forwarder (basic_block src,
480 : : basic_block dest, bool dest_single_pred_p,
481 : : basic_block pred, bool pred_single_succ_p)
482 : : {
483 : 26701015 : if (!MAY_HAVE_DEBUG_STMTS)
484 : 10021941 : return;
485 : :
486 : : /* If we cannot move to the destination but to the predecessor do that. */
487 : 17117841 : if (!dest_single_pred_p && pred_single_succ_p)
488 : : {
489 : 438848 : gimple_stmt_iterator gsi_to = gsi_last_bb (pred);
490 : 438848 : if (gsi_end_p (gsi_to) || !stmt_ends_bb_p (gsi_stmt (gsi_to)))
491 : : {
492 : 438767 : for (gimple_stmt_iterator gsi = gsi_after_labels (src);
493 : 1514452 : !gsi_end_p (gsi);)
494 : : {
495 : 1075685 : gimple *debug = gsi_stmt (gsi);
496 : 1075685 : gcc_assert (is_gimple_debug (debug));
497 : 1075685 : gsi_move_after (&gsi, &gsi_to);
498 : : }
499 : 438767 : return;
500 : : }
501 : : }
502 : :
503 : : /* Else move to DEST or drop/reset them. */
504 : 16679074 : gimple_stmt_iterator gsi_to = gsi_after_labels (dest);
505 : 32584078 : for (gimple_stmt_iterator gsi = gsi_after_labels (src); !gsi_end_p (gsi);)
506 : : {
507 : 15905004 : gimple *debug = gsi_stmt (gsi);
508 : 15905004 : gcc_assert (is_gimple_debug (debug));
509 : : /* Move debug binds anyway, but not anything else like begin-stmt
510 : : markers unless they are always valid at the destination. */
511 : 15905004 : if (dest_single_pred_p
512 : 15905004 : || gimple_debug_bind_p (debug))
513 : : {
514 : 15117549 : gsi_move_before (&gsi, &gsi_to);
515 : : /* Reset debug-binds that are not always valid at the destination.
516 : : Simply dropping them can cause earlier values to become live,
517 : : generating wrong debug information.
518 : : ??? There are several things we could improve here. For
519 : : one we might be able to move stmts to the predecessor.
520 : : For anther, if the debug stmt is immediately followed by a
521 : : (debug) definition in the destination (on a post-dominated path?)
522 : : we can elide it without any bad effects. */
523 : 15117549 : if (!dest_single_pred_p)
524 : : {
525 : 3972744 : gimple_debug_bind_reset_value (debug);
526 : 3972744 : update_stmt (debug);
527 : : }
528 : : }
529 : : else
530 : 787455 : gsi_next (&gsi);
531 : : }
532 : : }
533 : :
534 : : /* Removes forwarder block BB. Returns false if this failed. */
535 : :
536 : : static bool
537 : 29401233 : remove_forwarder_block (basic_block bb)
538 : : {
539 : 29401233 : edge succ = single_succ_edge (bb), e, s;
540 : 29401233 : basic_block dest = succ->dest;
541 : 29401233 : gimple *stmt;
542 : 29401233 : edge_iterator ei;
543 : 29401233 : gimple_stmt_iterator gsi, gsi_to;
544 : :
545 : : /* We check for infinite loops already in tree_forwarder_block_p.
546 : : However it may happen that the infinite loop is created
547 : : afterwards due to removal of forwarders. */
548 : 29401233 : if (dest == bb)
549 : : return false;
550 : :
551 : : /* If the destination block consists of a nonlocal label or is a
552 : : EH landing pad, do not merge it. */
553 : 29401233 : stmt = first_stmt (dest);
554 : 29401233 : if (stmt)
555 : 26506408 : if (glabel *label_stmt = dyn_cast <glabel *> (stmt))
556 : 1156358 : if (DECL_NONLOCAL (gimple_label_label (label_stmt))
557 : 1156358 : || EH_LANDING_PAD_NR (gimple_label_label (label_stmt)) != 0)
558 : : return false;
559 : :
560 : : /* If there is an abnormal edge to basic block BB, but not into
561 : : dest, problems might occur during removal of the phi node at out
562 : : of ssa due to overlapping live ranges of registers.
563 : :
564 : : If there is an abnormal edge in DEST, the problems would occur
565 : : anyway since cleanup_dead_labels would then merge the labels for
566 : : two different eh regions, and rest of exception handling code
567 : : does not like it.
568 : :
569 : : So if there is an abnormal edge to BB, proceed only if there is
570 : : no abnormal edge to DEST and there are no phi nodes in DEST. */
571 : 29388518 : if (bb_has_abnormal_pred (bb)
572 : 29388518 : && (bb_has_abnormal_pred (dest)
573 : 2240 : || !gimple_seq_empty_p (phi_nodes (dest))))
574 : : return false;
575 : :
576 : : /* If there are phi nodes in DEST, and some of the blocks that are
577 : : predecessors of BB are also predecessors of DEST, check that the
578 : : phi node arguments match. */
579 : 29385746 : if (!gimple_seq_empty_p (phi_nodes (dest)))
580 : : {
581 : 44775580 : FOR_EACH_EDGE (e, ei, bb->preds)
582 : : {
583 : 24397003 : s = find_edge (e->src, dest);
584 : 24397003 : if (!s)
585 : 21270630 : continue;
586 : :
587 : 3126373 : if (!phi_alternatives_equal (dest, succ, s))
588 : : return false;
589 : : }
590 : : }
591 : :
592 : 26299679 : basic_block pred = NULL;
593 : 26299679 : if (single_pred_p (bb))
594 : 25520770 : pred = single_pred (bb);
595 : 26299679 : bool dest_single_pred_p = single_pred_p (dest);
596 : :
597 : : /* Redirect the edges. */
598 : 80097531 : for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
599 : : {
600 : 27498173 : bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index);
601 : :
602 : 27498173 : if (e->flags & EDGE_ABNORMAL)
603 : : {
604 : : /* If there is an abnormal edge, redirect it anyway, and
605 : : move the labels to the new block to make it legal. */
606 : 184 : s = redirect_edge_succ_nodup (e, dest);
607 : : }
608 : : else
609 : 27497989 : s = redirect_edge_and_branch (e, dest);
610 : :
611 : 27498173 : if (s == e)
612 : : {
613 : : /* Copy arguments for the phi nodes, since the edge was not
614 : : here before. */
615 : 27404509 : copy_phi_arg_into_existing_phi (succ, s);
616 : : }
617 : : }
618 : :
619 : : /* Move nonlocal labels and computed goto targets as well as user
620 : : defined labels and labels with an EH landing pad number to the
621 : : new block, so that the redirection of the abnormal edges works,
622 : : jump targets end up in a sane place and debug information for
623 : : labels is retained. */
624 : 26299679 : gsi_to = gsi_start_bb (dest);
625 : 52862726 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
626 : : {
627 : 3063938 : stmt = gsi_stmt (gsi);
628 : 3063938 : if (is_gimple_debug (stmt))
629 : : break;
630 : :
631 : : /* Forwarder blocks can only contain labels and debug stmts, and
632 : : labels must come first, so if we get to this point, we know
633 : : we're looking at a label. */
634 : 263368 : tree decl = gimple_label_label (as_a <glabel *> (stmt));
635 : 263368 : if (EH_LANDING_PAD_NR (decl) != 0
636 : 263368 : || DECL_NONLOCAL (decl)
637 : 263368 : || FORCED_LABEL (decl)
638 : 520531 : || !DECL_ARTIFICIAL (decl))
639 : 21102 : gsi_move_before (&gsi, &gsi_to);
640 : : else
641 : 242266 : gsi_next (&gsi);
642 : : }
643 : :
644 : : /* Move debug statements. Reset them if the destination does not
645 : : have a single predecessor. */
646 : 52599358 : move_debug_stmts_from_forwarder (bb, dest, dest_single_pred_p,
647 : 76379978 : pred, pred && single_succ_p (pred));
648 : :
649 : 26299679 : bitmap_set_bit (cfgcleanup_altered_bbs, dest->index);
650 : :
651 : : /* Update the dominators. */
652 : 26299679 : if (dom_info_available_p (CDI_DOMINATORS))
653 : : {
654 : 26299679 : basic_block dom, dombb, domdest;
655 : :
656 : 26299679 : dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
657 : 26299679 : domdest = get_immediate_dominator (CDI_DOMINATORS, dest);
658 : 26299679 : if (domdest == bb)
659 : : {
660 : : /* Shortcut to avoid calling (relatively expensive)
661 : : nearest_common_dominator unless necessary. */
662 : : dom = dombb;
663 : : }
664 : : else
665 : 19280810 : dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb);
666 : :
667 : 26299679 : set_immediate_dominator (CDI_DOMINATORS, dest, dom);
668 : : }
669 : :
670 : : /* Adjust latch infomation of BB's parent loop as otherwise
671 : : the cfg hook has a hard time not to kill the loop. */
672 : 26299679 : if (current_loops && bb->loop_father->latch == bb)
673 : 5443909 : bb->loop_father->latch = pred;
674 : :
675 : : /* And kill the forwarder block. */
676 : 26299679 : delete_basic_block (bb);
677 : :
678 : 26299679 : return true;
679 : : }
680 : :
681 : : /* STMT is a call that has been discovered noreturn. Split the
682 : : block to prepare fixing up the CFG and remove LHS.
683 : : Return true if cleanup-cfg needs to run. */
684 : :
685 : : bool
686 : 5730260 : fixup_noreturn_call (gimple *stmt)
687 : : {
688 : 5730260 : basic_block bb = gimple_bb (stmt);
689 : 5730260 : bool changed = false;
690 : :
691 : 5730260 : if (gimple_call_builtin_p (stmt, BUILT_IN_RETURN))
692 : : return false;
693 : :
694 : : /* First split basic block if stmt is not last. */
695 : 11456748 : if (stmt != gsi_stmt (gsi_last_bb (bb)))
696 : : {
697 : 39945 : if (stmt == gsi_stmt (gsi_last_nondebug_bb (bb)))
698 : : {
699 : : /* Don't split if there are only debug stmts
700 : : after stmt, that can result in -fcompare-debug
701 : : failures. Remove the debug stmts instead,
702 : : they should be all unreachable anyway. */
703 : 6015 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
704 : 43242 : for (gsi_next (&gsi); !gsi_end_p (gsi); )
705 : 37227 : gsi_remove (&gsi, true);
706 : : }
707 : : else
708 : : {
709 : 33930 : split_block (bb, stmt);
710 : 33930 : changed = true;
711 : : }
712 : : }
713 : :
714 : : /* If there is an LHS, remove it, but only if its type has fixed size.
715 : : The LHS will need to be recreated during RTL expansion and creating
716 : : temporaries of variable-sized types is not supported. Also don't
717 : : do this with TREE_ADDRESSABLE types, as assign_temp will abort.
718 : : Drop LHS regardless of TREE_ADDRESSABLE, if the function call
719 : : has been changed into a call that does not return a value, like
720 : : __builtin_unreachable or __cxa_pure_virtual. */
721 : 5728374 : tree lhs = gimple_call_lhs (stmt);
722 : 5728374 : if (lhs
723 : 5728374 : && (should_remove_lhs_p (lhs)
724 : 363 : || VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (stmt)))))
725 : : {
726 : 11576 : gimple_call_set_lhs (stmt, NULL_TREE);
727 : :
728 : : /* We need to fix up the SSA name to avoid checking errors. */
729 : 11576 : if (TREE_CODE (lhs) == SSA_NAME)
730 : : {
731 : 3680 : tree new_var = create_tmp_reg (TREE_TYPE (lhs));
732 : 3680 : SET_SSA_NAME_VAR_OR_IDENTIFIER (lhs, new_var);
733 : 3680 : SSA_NAME_DEF_STMT (lhs) = gimple_build_nop ();
734 : 3680 : set_ssa_default_def (cfun, new_var, lhs);
735 : : }
736 : :
737 : 11576 : update_stmt (stmt);
738 : : }
739 : :
740 : : /* Mark the call as altering control flow. */
741 : 5728374 : if (!gimple_call_ctrl_altering_p (stmt))
742 : : {
743 : 79208 : gimple_call_set_ctrl_altering (stmt, true);
744 : 79208 : changed = true;
745 : : }
746 : :
747 : : return changed;
748 : : }
749 : :
750 : : /* Return true if we want to merge BB1 and BB2 into a single block. */
751 : :
752 : : static bool
753 : 404745098 : want_merge_blocks_p (basic_block bb1, basic_block bb2)
754 : : {
755 : 404745098 : if (!can_merge_blocks_p (bb1, bb2))
756 : : return false;
757 : 21831572 : gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb1);
758 : 21831572 : if (gsi_end_p (gsi) || !stmt_can_terminate_bb_p (gsi_stmt (gsi)))
759 : 19115399 : return true;
760 : 2716173 : return bb1->count.ok_for_merging (bb2->count);
761 : : }
762 : :
763 : :
764 : : /* Tries to cleanup cfg in basic block BB by merging blocks. Returns
765 : : true if anything changes. */
766 : :
767 : : static bool
768 : 381809691 : cleanup_tree_cfg_bb (basic_block bb)
769 : : {
770 : 381809691 : if (tree_forwarder_block_p (bb, false)
771 : 381809691 : && remove_forwarder_block (bb))
772 : : return true;
773 : :
774 : : /* If there is a merge opportunity with the predecessor
775 : : do nothing now but wait until we process the predecessor.
776 : : This happens when we visit BBs in a non-optimal order and
777 : : avoids quadratic behavior with adjusting stmts BB pointer. */
778 : 355510012 : if (single_pred_p (bb)
779 : 611790794 : && want_merge_blocks_p (single_pred (bb), bb))
780 : : /* But make sure we _do_ visit it. When we remove unreachable paths
781 : : ending in a backedge we fail to mark the destinations predecessors
782 : : as changed. */
783 : 8103845 : bitmap_set_bit (cfgcleanup_altered_bbs, single_pred (bb)->index);
784 : :
785 : : /* Merging the blocks may create new opportunities for folding
786 : : conditional branches (due to the elimination of single-valued PHI
787 : : nodes). */
788 : 347406167 : else if (single_succ_p (bb)
789 : 485677121 : && want_merge_blocks_p (bb, single_succ (bb)))
790 : : {
791 : 13727649 : merge_blocks (bb, single_succ (bb));
792 : 13727649 : return true;
793 : : }
794 : :
795 : : return false;
796 : : }
797 : :
798 : : /* Return true if E is an EDGE_ABNORMAL edge for returns_twice calls,
799 : : i.e. one going from .ABNORMAL_DISPATCHER to basic block which doesn't
800 : : start with a forced or nonlocal label. Calls which return twice can return
801 : : the second time only if they are called normally the first time, so basic
802 : : blocks which can be only entered through these abnormal edges but not
803 : : normally are effectively unreachable as well. Additionally ignore
804 : : __builtin_setjmp_receiver starting blocks, which have one FORCED_LABEL
805 : : and which are always only reachable through EDGE_ABNORMAL edge. They are
806 : : handled in cleanup_control_flow_pre. */
807 : :
808 : : static bool
809 : 343138258 : maybe_dead_abnormal_edge_p (edge e)
810 : : {
811 : 343138258 : if ((e->flags & (EDGE_ABNORMAL | EDGE_EH)) != EDGE_ABNORMAL)
812 : : return false;
813 : :
814 : 152943 : gimple_stmt_iterator gsi = gsi_start_nondebug_after_labels_bb (e->src);
815 : 152943 : gimple *g = gsi_stmt (gsi);
816 : 152943 : if (!g || !gimple_call_internal_p (g, IFN_ABNORMAL_DISPATCHER))
817 : : return false;
818 : :
819 : 21935 : tree target = NULL_TREE;
820 : 57600 : for (gsi = gsi_start_bb (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
821 : 35658 : if (glabel *label_stmt = dyn_cast <glabel *> (gsi_stmt (gsi)))
822 : : {
823 : 20587 : tree this_target = gimple_label_label (label_stmt);
824 : 20587 : if (DECL_NONLOCAL (this_target))
825 : : return false;
826 : 13730 : if (FORCED_LABEL (this_target))
827 : : {
828 : 13730 : if (target)
829 : : return false;
830 : : target = this_target;
831 : : }
832 : : }
833 : : else
834 : : break;
835 : :
836 : 15078 : if (target)
837 : : {
838 : : /* If there was a single FORCED_LABEL, check for
839 : : __builtin_setjmp_receiver with address of that label. */
840 : 13730 : if (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
841 : 0 : gsi_next_nondebug (&gsi);
842 : 13730 : if (gsi_end_p (gsi))
843 : : return false;
844 : 13730 : if (!gimple_call_builtin_p (gsi_stmt (gsi), BUILT_IN_SETJMP_RECEIVER))
845 : : return false;
846 : :
847 : 13730 : tree arg = gimple_call_arg (gsi_stmt (gsi), 0);
848 : 13730 : if (TREE_CODE (arg) != ADDR_EXPR || TREE_OPERAND (arg, 0) != target)
849 : : return false;
850 : : }
851 : : return true;
852 : : }
853 : :
854 : : /* If BB is a basic block ending with __builtin_setjmp_setup, return edge
855 : : from .ABNORMAL_DISPATCHER basic block to corresponding
856 : : __builtin_setjmp_receiver basic block, otherwise return NULL. */
857 : : static edge
858 : 343136886 : builtin_setjmp_setup_bb (basic_block bb)
859 : : {
860 : 343136886 : if (EDGE_COUNT (bb->succs) != 2
861 : 333000263 : || ((EDGE_SUCC (bb, 0)->flags
862 : 151086305 : & (EDGE_ABNORMAL | EDGE_EH)) != EDGE_ABNORMAL
863 : 150991193 : && (EDGE_SUCC (bb, 1)->flags
864 : 150991193 : & (EDGE_ABNORMAL | EDGE_EH)) != EDGE_ABNORMAL))
865 : : return NULL;
866 : :
867 : 172597 : gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
868 : 172597 : if (gsi_end_p (gsi)
869 : 172597 : || !gimple_call_builtin_p (gsi_stmt (gsi), BUILT_IN_SETJMP_SETUP))
870 : 158891 : return NULL;
871 : :
872 : 13706 : tree arg = gimple_call_arg (gsi_stmt (gsi), 1);
873 : 13706 : if (TREE_CODE (arg) != ADDR_EXPR
874 : 13706 : || TREE_CODE (TREE_OPERAND (arg, 0)) != LABEL_DECL)
875 : : return NULL;
876 : :
877 : 13706 : basic_block recv_bb = label_to_block (cfun, TREE_OPERAND (arg, 0));
878 : 343136886 : if (EDGE_COUNT (recv_bb->preds) != 1
879 : 13706 : || (EDGE_PRED (recv_bb, 0)->flags
880 : 13706 : & (EDGE_ABNORMAL | EDGE_EH)) != EDGE_ABNORMAL
881 : 27412 : || (EDGE_SUCC (bb, 0)->dest != EDGE_PRED (recv_bb, 0)->src
882 : 13706 : && EDGE_SUCC (bb, 1)->dest != EDGE_PRED (recv_bb, 0)->src))
883 : : return NULL;
884 : :
885 : : /* EDGE_PRED (recv_bb, 0)->src should be the .ABNORMAL_DISPATCHER bb. */
886 : : return EDGE_PRED (recv_bb, 0);
887 : : }
888 : :
889 : : /* Do cleanup_control_flow_bb in PRE order. */
890 : :
891 : : static bool
892 : 27106544 : cleanup_control_flow_pre ()
893 : : {
894 : 27106544 : bool retval = false;
895 : :
896 : : /* We want remove_edge_and_dominated_blocks to only remove edges,
897 : : not dominated blocks which it does when dom info isn't available.
898 : : Pretend so. */
899 : 27106544 : dom_state saved_state = dom_info_state (CDI_DOMINATORS);
900 : 27106544 : set_dom_info_availability (CDI_DOMINATORS, DOM_NONE);
901 : :
902 : 27106544 : auto_vec<edge_iterator, 20> stack (n_basic_blocks_for_fn (cfun) + 2);
903 : 27106544 : auto_sbitmap visited (last_basic_block_for_fn (cfun));
904 : 27106544 : bitmap_clear (visited);
905 : :
906 : 27106544 : vec<edge, va_gc> *setjmp_vec = NULL;
907 : 27106544 : auto_vec<basic_block, 4> abnormal_dispatchers;
908 : :
909 : 27106544 : stack.quick_push (ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs));
910 : :
911 : 873624468 : while (! stack.is_empty ())
912 : : {
913 : : /* Look at the edge on the top of the stack. */
914 : 846517924 : edge_iterator ei = stack.last ();
915 : 846517924 : basic_block dest = ei_edge (ei)->dest;
916 : :
917 : 846517924 : if (dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
918 : 819849041 : && !bitmap_bit_p (visited, dest->index)
919 : 1189669888 : && (ei_container (ei) == setjmp_vec
920 : 343138258 : || !maybe_dead_abnormal_edge_p (ei_edge (ei))))
921 : : {
922 : 343136886 : bitmap_set_bit (visited, dest->index);
923 : : /* We only possibly remove edges from DEST here, leaving
924 : : possibly unreachable code in the IL. */
925 : 343136886 : retval |= cleanup_control_flow_bb (dest);
926 : :
927 : : /* Check for __builtin_setjmp_setup. Edges from .ABNORMAL_DISPATCH
928 : : to __builtin_setjmp_receiver will be normally ignored by
929 : : maybe_dead_abnormal_edge_p. If DEST is a visited
930 : : __builtin_setjmp_setup, queue edge from .ABNORMAL_DISPATCH
931 : : to __builtin_setjmp_receiver, so that it will be visited too. */
932 : 343136886 : if (edge e = builtin_setjmp_setup_bb (dest))
933 : : {
934 : 13706 : vec_safe_push (setjmp_vec, e);
935 : 13706 : if (vec_safe_length (setjmp_vec) == 1)
936 : 7191 : stack.quick_push (ei_start (setjmp_vec));
937 : : }
938 : :
939 : 343136886 : if ((ei_edge (ei)->flags
940 : 343136886 : & (EDGE_ABNORMAL | EDGE_EH)) == EDGE_ABNORMAL)
941 : : {
942 : 151571 : gimple_stmt_iterator gsi
943 : 151571 : = gsi_start_nondebug_after_labels_bb (dest);
944 : 151571 : gimple *g = gsi_stmt (gsi);
945 : 151571 : if (g && gimple_call_internal_p (g, IFN_ABNORMAL_DISPATCHER))
946 : 28189 : abnormal_dispatchers.safe_push (dest);
947 : : }
948 : :
949 : 1189654810 : if (EDGE_COUNT (dest->succs) > 0)
950 : 321785256 : stack.quick_push (ei_start (dest->succs));
951 : : }
952 : : else
953 : : {
954 : 503381038 : if (!ei_one_before_end_p (ei))
955 : 154482047 : ei_next (&stack.last ());
956 : : else
957 : : {
958 : 348898991 : if (ei_container (ei) == setjmp_vec)
959 : 7191 : vec_safe_truncate (setjmp_vec, 0);
960 : 348898991 : stack.pop ();
961 : : }
962 : : }
963 : : }
964 : :
965 : 27106544 : vec_free (setjmp_vec);
966 : :
967 : : /* If we've marked .ABNORMAL_DISPATCHER basic block(s) as visited
968 : : above, but haven't marked any of their successors as visited,
969 : : unmark them now, so that they can be removed as useless. */
970 : 81347821 : for (basic_block dispatcher_bb : abnormal_dispatchers)
971 : : {
972 : 28189 : edge e;
973 : 28189 : edge_iterator ei;
974 : 28280 : FOR_EACH_EDGE (e, ei, dispatcher_bb->succs)
975 : 28189 : if (bitmap_bit_p (visited, e->dest->index))
976 : : break;
977 : 28189 : if (e == NULL)
978 : 91 : bitmap_clear_bit (visited, dispatcher_bb->index);
979 : : }
980 : :
981 : 27106544 : set_dom_info_availability (CDI_DOMINATORS, saved_state);
982 : :
983 : : /* We are deleting BBs in non-reverse dominator order, make sure
984 : : insert_debug_temps_for_defs is prepared for that. */
985 : 27106544 : if (retval)
986 : 762387 : free_dominance_info (CDI_DOMINATORS);
987 : :
988 : : /* Remove all now (and previously) unreachable blocks. */
989 : 390964851 : for (int i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); ++i)
990 : : {
991 : 363858307 : basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
992 : 363858307 : if (bb && !bitmap_bit_p (visited, bb->index))
993 : : {
994 : 4022039 : if (!retval)
995 : 680412 : free_dominance_info (CDI_DOMINATORS);
996 : 4022039 : delete_basic_block (bb);
997 : 4022039 : retval = true;
998 : : }
999 : : }
1000 : :
1001 : 27106544 : return retval;
1002 : 27106544 : }
1003 : :
1004 : : static bool
1005 : 160953 : mfb_keep_latches (edge e)
1006 : : {
1007 : 160953 : return !((dom_info_available_p (CDI_DOMINATORS)
1008 : 44480 : && dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
1009 : 147296 : || (e->flags & EDGE_DFS_BACK));
1010 : : }
1011 : :
1012 : : /* Remove unreachable blocks and other miscellaneous clean up work.
1013 : : Return true if the flowgraph was modified, false otherwise. */
1014 : :
1015 : : static bool
1016 : 27106544 : cleanup_tree_cfg_noloop (unsigned ssa_update_flags)
1017 : : {
1018 : 27106544 : timevar_push (TV_TREE_CLEANUP_CFG);
1019 : :
1020 : : /* Ensure that we have single entries into loop headers. Otherwise
1021 : : if one of the entries is becoming a latch due to CFG cleanup
1022 : : (from formerly being part of an irreducible region) then we mess
1023 : : up loop fixup and associate the old loop with a different region
1024 : : which makes niter upper bounds invalid. See for example PR80549.
1025 : : This needs to be done before we remove trivially dead edges as
1026 : : we need to capture the dominance state before the pending transform. */
1027 : 27106544 : if (current_loops)
1028 : : {
1029 : : /* This needs backedges or dominators. */
1030 : 24393294 : if (!dom_info_available_p (CDI_DOMINATORS))
1031 : 6316329 : mark_dfs_back_edges ();
1032 : :
1033 : 98461823 : for (loop_p loop : *get_loops (cfun))
1034 : 49675235 : if (loop && loop->header)
1035 : : {
1036 : 42768684 : basic_block bb = loop->header;
1037 : 42768684 : edge_iterator ei;
1038 : 42768684 : edge e;
1039 : 42768684 : bool found_latch = false;
1040 : 42768684 : bool any_abnormal = false;
1041 : 42768684 : unsigned n = 0;
1042 : : /* We are only interested in preserving existing loops, but
1043 : : we need to check whether they are still real and of course
1044 : : if we need to add a preheader at all. */
1045 : 79693850 : FOR_EACH_EDGE (e, ei, bb->preds)
1046 : : {
1047 : 36925166 : if (e->flags & EDGE_ABNORMAL)
1048 : : {
1049 : : any_abnormal = true;
1050 : : break;
1051 : : }
1052 : 36925166 : if ((dom_info_available_p (CDI_DOMINATORS)
1053 : 27569330 : && dominated_by_p (CDI_DOMINATORS, e->src, bb))
1054 : 50693634 : || (e->flags & EDGE_DFS_BACK))
1055 : : {
1056 : 18489571 : found_latch = true;
1057 : 18489571 : continue;
1058 : : }
1059 : 18435595 : n++;
1060 : : }
1061 : : /* If we have more than one entry to the loop header
1062 : : create a forwarder. */
1063 : 42768684 : if (found_latch && ! any_abnormal && n > 1)
1064 : : {
1065 : 48552 : edge fallthru = make_forwarder_block (bb, mfb_keep_latches,
1066 : : NULL);
1067 : 48552 : loop->header = fallthru->dest;
1068 : 48552 : if (! loops_state_satisfies_p (LOOPS_NEED_FIXUP))
1069 : : {
1070 : : /* The loop updating from the CFG hook is incomplete
1071 : : when we have multiple latches, fixup manually. */
1072 : 23022 : remove_bb_from_loops (fallthru->src);
1073 : 23022 : loop_p cloop = loop;
1074 : 72849 : FOR_EACH_EDGE (e, ei, fallthru->src->preds)
1075 : 49827 : cloop = find_common_loop (cloop, e->src->loop_father);
1076 : 23022 : add_bb_to_loop (fallthru->src, cloop);
1077 : : }
1078 : : }
1079 : : }
1080 : : }
1081 : :
1082 : : /* Prepare the worklists of altered blocks. */
1083 : 27106544 : cfgcleanup_altered_bbs = BITMAP_ALLOC (NULL);
1084 : :
1085 : : /* Start by iterating over all basic blocks in PRE order looking for
1086 : : edge removal opportunities. Do this first because incoming SSA form
1087 : : may be invalid and we want to avoid performing SSA related tasks such
1088 : : as propgating out a PHI node during BB merging in that state. This
1089 : : also gets rid of unreachable blocks. */
1090 : 27106544 : bool changed = cleanup_control_flow_pre ();
1091 : :
1092 : : /* After doing the above SSA form should be valid (or an update SSA
1093 : : should be required). */
1094 : 27106544 : if (ssa_update_flags)
1095 : : {
1096 : 20573613 : timevar_pop (TV_TREE_CLEANUP_CFG);
1097 : 20573613 : update_ssa (ssa_update_flags);
1098 : 20573613 : timevar_push (TV_TREE_CLEANUP_CFG);
1099 : : }
1100 : :
1101 : : /* Compute dominator info which we need for the iterative process below.
1102 : : Avoid computing the fast query DFS numbers since any block merging
1103 : : done will invalidate them anyway. */
1104 : 27106544 : if (!dom_info_available_p (CDI_DOMINATORS))
1105 : 8031290 : calculate_dominance_info (CDI_DOMINATORS, false);
1106 : : else
1107 : 19075254 : checking_verify_dominators (CDI_DOMINATORS);
1108 : :
1109 : : /* During forwarder block cleanup, we may redirect edges out of
1110 : : SWITCH_EXPRs, which can get expensive. So we want to enable
1111 : : recording of edge to CASE_LABEL_EXPR. */
1112 : 27106544 : start_recording_case_labels ();
1113 : :
1114 : : /* Continue by iterating over all basic blocks looking for BB merging
1115 : : opportunities. We cannot use FOR_EACH_BB_FN for the BB iteration
1116 : : since the basic blocks may get removed. */
1117 : 27106544 : unsigned n = last_basic_block_for_fn (cfun);
1118 : 390964851 : for (unsigned i = NUM_FIXED_BLOCKS; i < n; i++)
1119 : : {
1120 : 363858307 : basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
1121 : 363858307 : if (bb)
1122 : 336399664 : changed |= cleanup_tree_cfg_bb (bb);
1123 : : }
1124 : :
1125 : : /* Now process the altered blocks, as long as any are available. */
1126 : 79529295 : while (!bitmap_empty_p (cfgcleanup_altered_bbs))
1127 : : {
1128 : 52422751 : unsigned i = bitmap_clear_first_set_bit (cfgcleanup_altered_bbs);
1129 : 52422751 : if (i < NUM_FIXED_BLOCKS)
1130 : 0 : continue;
1131 : :
1132 : 52422751 : basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
1133 : 52422751 : if (!bb)
1134 : 7012724 : continue;
1135 : :
1136 : : /* BB merging done by cleanup_tree_cfg_bb can end up propagating
1137 : : out single-argument PHIs which in turn can expose
1138 : : cleanup_control_flow_bb opportunities so we have to repeat
1139 : : that here. */
1140 : 45410027 : changed |= cleanup_control_flow_bb (bb);
1141 : 45410027 : changed |= cleanup_tree_cfg_bb (bb);
1142 : : }
1143 : :
1144 : 27106544 : end_recording_case_labels ();
1145 : 27106544 : BITMAP_FREE (cfgcleanup_altered_bbs);
1146 : :
1147 : 27106544 : gcc_assert (dom_info_available_p (CDI_DOMINATORS));
1148 : :
1149 : : /* Do not renumber blocks if the SCEV cache is active, it is indexed by
1150 : : basic-block numbers. */
1151 : 27106544 : if (! scev_initialized_p ())
1152 : 26828952 : compact_blocks ();
1153 : :
1154 : 27106544 : checking_verify_flow_info ();
1155 : :
1156 : 27106544 : timevar_pop (TV_TREE_CLEANUP_CFG);
1157 : :
1158 : 27106544 : if (changed && current_loops)
1159 : : {
1160 : : /* Removing edges and/or blocks may make recorded bounds refer
1161 : : to stale GIMPLE stmts now, so clear them. */
1162 : 5728221 : free_numbers_of_iterations_estimates (cfun);
1163 : 5728221 : loops_state_set (LOOPS_NEED_FIXUP);
1164 : : }
1165 : :
1166 : 27106544 : return changed;
1167 : : }
1168 : :
1169 : : /* Repairs loop structures. */
1170 : :
1171 : : static void
1172 : 7322913 : repair_loop_structures (void)
1173 : : {
1174 : 7322913 : bitmap changed_bbs;
1175 : 7322913 : unsigned n_new_or_deleted_loops;
1176 : :
1177 : 7322913 : calculate_dominance_info (CDI_DOMINATORS);
1178 : :
1179 : 7322913 : timevar_push (TV_REPAIR_LOOPS);
1180 : 7322913 : changed_bbs = BITMAP_ALLOC (NULL);
1181 : 7322913 : n_new_or_deleted_loops = fix_loop_structure (changed_bbs);
1182 : :
1183 : : /* This usually does nothing. But sometimes parts of cfg that originally
1184 : : were inside a loop get out of it due to edge removal (since they
1185 : : become unreachable by back edges from latch). Also a former
1186 : : irreducible loop can become reducible - in this case force a full
1187 : : rewrite into loop-closed SSA form. */
1188 : 7322913 : if (loops_state_satisfies_p (LOOP_CLOSED_SSA)
1189 : 7322913 : && (!bitmap_empty_p (changed_bbs) || n_new_or_deleted_loops))
1190 : 22473 : rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
1191 : :
1192 : 7322913 : BITMAP_FREE (changed_bbs);
1193 : :
1194 : 7322913 : checking_verify_loop_structure ();
1195 : 7322913 : scev_reset ();
1196 : :
1197 : 7322913 : timevar_pop (TV_REPAIR_LOOPS);
1198 : 7322913 : }
1199 : :
1200 : : /* Cleanup cfg and repair loop structures. */
1201 : :
1202 : : bool
1203 : 27106544 : cleanup_tree_cfg (unsigned ssa_update_flags)
1204 : : {
1205 : 27106544 : bool changed = cleanup_tree_cfg_noloop (ssa_update_flags);
1206 : :
1207 : 27106544 : if (current_loops != NULL
1208 : 27106544 : && loops_state_satisfies_p (LOOPS_NEED_FIXUP))
1209 : 7322913 : repair_loop_structures ();
1210 : :
1211 : 27106544 : return changed;
1212 : : }
1213 : :
1214 : : /* Tries to merge the PHI nodes at BB into those at BB's sole successor.
1215 : : Returns true if successful. */
1216 : :
1217 : : static bool
1218 : 401338 : remove_forwarder_block_with_phi (basic_block bb)
1219 : : {
1220 : 401338 : edge succ = single_succ_edge (bb);
1221 : 401338 : basic_block dest = succ->dest;
1222 : 401338 : gimple *label;
1223 : 401338 : basic_block dombb, domdest, dom;
1224 : :
1225 : : /* We check for infinite loops already in tree_forwarder_block_p.
1226 : : However it may happen that the infinite loop is created
1227 : : afterwards due to removal of forwarders. */
1228 : 401338 : if (dest == bb)
1229 : : return false;
1230 : :
1231 : : /* Removal of forwarders may expose new natural loops and thus
1232 : : a block may turn into a loop header. */
1233 : 401337 : if (current_loops && bb_loop_header_p (bb))
1234 : : return false;
1235 : :
1236 : : /* If the destination block consists of a nonlocal label, do not
1237 : : merge it. */
1238 : 401336 : label = first_stmt (dest);
1239 : 401336 : if (label)
1240 : 393217 : if (glabel *label_stmt = dyn_cast <glabel *> (label))
1241 : 12363 : if (DECL_NONLOCAL (gimple_label_label (label_stmt)))
1242 : : return false;
1243 : :
1244 : : /* Record BB's single pred in case we need to update the father
1245 : : loop's latch information later. */
1246 : 401336 : basic_block pred = NULL;
1247 : 401336 : if (single_pred_p (bb))
1248 : 79779 : pred = single_pred (bb);
1249 : 401336 : bool dest_single_pred_p = single_pred_p (dest);
1250 : :
1251 : : /* Redirect each incoming edge to BB to DEST. */
1252 : 1380672 : while (EDGE_COUNT (bb->preds) > 0)
1253 : : {
1254 : 979336 : edge e = EDGE_PRED (bb, 0), s;
1255 : 979336 : gphi_iterator gsi;
1256 : :
1257 : 979336 : s = find_edge (e->src, dest);
1258 : 979336 : if (s)
1259 : : {
1260 : : /* We already have an edge S from E->src to DEST. If S and
1261 : : E->dest's sole successor edge have the same PHI arguments
1262 : : at DEST, redirect S to DEST. */
1263 : 23675 : if (phi_alternatives_equal (dest, s, succ))
1264 : : {
1265 : 1 : e = redirect_edge_and_branch (e, dest);
1266 : 1 : redirect_edge_var_map_clear (e);
1267 : 1 : continue;
1268 : : }
1269 : :
1270 : : /* PHI arguments are different. Create a forwarder block by
1271 : : splitting E so that we can merge PHI arguments on E to
1272 : : DEST. */
1273 : 23674 : e = single_succ_edge (split_edge (e));
1274 : : }
1275 : : else
1276 : : {
1277 : : /* If we merge the forwarder into a loop header verify if we
1278 : : are creating another loop latch edge. If so, reset
1279 : : number of iteration information of the loop. */
1280 : 955661 : if (dest->loop_father->header == dest
1281 : 955661 : && dominated_by_p (CDI_DOMINATORS, e->src, dest))
1282 : : {
1283 : 180263 : dest->loop_father->any_upper_bound = false;
1284 : 180263 : dest->loop_father->any_likely_upper_bound = false;
1285 : 180263 : free_numbers_of_iterations_estimates (dest->loop_father);
1286 : : }
1287 : : }
1288 : :
1289 : 979335 : s = redirect_edge_and_branch (e, dest);
1290 : :
1291 : : /* redirect_edge_and_branch must not create a new edge. */
1292 : 979335 : gcc_assert (s == e);
1293 : :
1294 : : /* Add to the PHI nodes at DEST each PHI argument removed at the
1295 : : destination of E. */
1296 : 979335 : for (gsi = gsi_start_phis (dest);
1297 : 3104274 : !gsi_end_p (gsi);
1298 : 2124939 : gsi_next (&gsi))
1299 : : {
1300 : 2124939 : gphi *phi = gsi.phi ();
1301 : 2124939 : tree def = gimple_phi_arg_def (phi, succ->dest_idx);
1302 : 2124939 : location_t locus = gimple_phi_arg_location_from_edge (phi, succ);
1303 : :
1304 : 2124939 : if (TREE_CODE (def) == SSA_NAME)
1305 : : {
1306 : : /* If DEF is one of the results of PHI nodes removed during
1307 : : redirection, replace it with the PHI argument that used
1308 : : to be on E. */
1309 : 1813789 : vec<edge_var_map> *head = redirect_edge_var_map_vector (e);
1310 : 3627578 : size_t length = head ? head->length () : 0;
1311 : 3326862 : for (size_t i = 0; i < length; i++)
1312 : : {
1313 : 2838522 : edge_var_map *vm = &(*head)[i];
1314 : 2838522 : tree old_arg = redirect_edge_var_map_result (vm);
1315 : 2838522 : tree new_arg = redirect_edge_var_map_def (vm);
1316 : :
1317 : 2838522 : if (def == old_arg)
1318 : : {
1319 : 1325449 : def = new_arg;
1320 : 1325449 : locus = redirect_edge_var_map_location (vm);
1321 : 1325449 : break;
1322 : : }
1323 : : }
1324 : : }
1325 : :
1326 : 2124939 : add_phi_arg (phi, def, s, locus);
1327 : : }
1328 : :
1329 : 979335 : redirect_edge_var_map_clear (e);
1330 : : }
1331 : :
1332 : : /* Move debug statements. Reset them if the destination does not
1333 : : have a single predecessor. */
1334 : 802672 : move_debug_stmts_from_forwarder (bb, dest, dest_single_pred_p,
1335 : 882451 : pred, pred && single_succ_p (pred));
1336 : :
1337 : : /* Update the dominators. */
1338 : 401336 : dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
1339 : 401336 : domdest = get_immediate_dominator (CDI_DOMINATORS, dest);
1340 : 401336 : if (domdest == bb)
1341 : : {
1342 : : /* Shortcut to avoid calling (relatively expensive)
1343 : : nearest_common_dominator unless necessary. */
1344 : : dom = dombb;
1345 : : }
1346 : : else
1347 : 308162 : dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb);
1348 : :
1349 : 401336 : set_immediate_dominator (CDI_DOMINATORS, dest, dom);
1350 : :
1351 : : /* Adjust latch infomation of BB's parent loop as otherwise
1352 : : the cfg hook has a hard time not to kill the loop. */
1353 : 401336 : if (current_loops && bb->loop_father->latch == bb)
1354 : 74278 : bb->loop_father->latch = pred;
1355 : :
1356 : : /* Remove BB since all of BB's incoming edges have been redirected
1357 : : to DEST. */
1358 : 401336 : delete_basic_block (bb);
1359 : :
1360 : 401336 : return true;
1361 : : }
1362 : :
1363 : : /* This pass merges PHI nodes if one feeds into another. For example,
1364 : : suppose we have the following:
1365 : :
1366 : : goto <bb 9> (<L9>);
1367 : :
1368 : : <L8>:;
1369 : : tem_17 = foo ();
1370 : :
1371 : : # tem_6 = PHI <tem_17(8), tem_23(7)>;
1372 : : <L9>:;
1373 : :
1374 : : # tem_3 = PHI <tem_6(9), tem_2(5)>;
1375 : : <L10>:;
1376 : :
1377 : : Then we merge the first PHI node into the second one like so:
1378 : :
1379 : : goto <bb 9> (<L10>);
1380 : :
1381 : : <L8>:;
1382 : : tem_17 = foo ();
1383 : :
1384 : : # tem_3 = PHI <tem_23(7), tem_2(5), tem_17(8)>;
1385 : : <L10>:;
1386 : : */
1387 : :
1388 : : namespace {
1389 : :
1390 : : const pass_data pass_data_merge_phi =
1391 : : {
1392 : : GIMPLE_PASS, /* type */
1393 : : "mergephi", /* name */
1394 : : OPTGROUP_NONE, /* optinfo_flags */
1395 : : TV_TREE_MERGE_PHI, /* tv_id */
1396 : : ( PROP_cfg | PROP_ssa ), /* properties_required */
1397 : : 0, /* properties_provided */
1398 : : 0, /* properties_destroyed */
1399 : : 0, /* todo_flags_start */
1400 : : 0, /* todo_flags_finish */
1401 : : };
1402 : :
1403 : : class pass_merge_phi : public gimple_opt_pass
1404 : : {
1405 : : public:
1406 : 848859 : pass_merge_phi (gcc::context *ctxt)
1407 : 1697718 : : gimple_opt_pass (pass_data_merge_phi, ctxt)
1408 : : {}
1409 : :
1410 : : /* opt_pass methods: */
1411 : 565906 : opt_pass * clone () final override { return new pass_merge_phi (m_ctxt); }
1412 : : unsigned int execute (function *) final override;
1413 : :
1414 : : }; // class pass_merge_phi
1415 : :
1416 : : unsigned int
1417 : 4262994 : pass_merge_phi::execute (function *fun)
1418 : : {
1419 : 4262994 : basic_block *worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (fun));
1420 : 4262994 : basic_block *current = worklist;
1421 : 4262994 : basic_block bb;
1422 : :
1423 : 4262994 : calculate_dominance_info (CDI_DOMINATORS);
1424 : :
1425 : : /* Find all PHI nodes that we may be able to merge. */
1426 : 35743776 : FOR_EACH_BB_FN (bb, fun)
1427 : : {
1428 : 31480782 : basic_block dest;
1429 : :
1430 : : /* Look for a forwarder block with PHI nodes. */
1431 : 31480782 : if (!tree_forwarder_block_p (bb, true))
1432 : 31028522 : continue;
1433 : :
1434 : 452260 : dest = single_succ (bb);
1435 : :
1436 : : /* We have to feed into another basic block with PHI
1437 : : nodes. */
1438 : 452260 : if (gimple_seq_empty_p (phi_nodes (dest))
1439 : : /* We don't want to deal with a basic block with
1440 : : abnormal edges. */
1441 : 452260 : || bb_has_abnormal_pred (bb))
1442 : 524 : continue;
1443 : :
1444 : 451736 : if (!dominated_by_p (CDI_DOMINATORS, dest, bb))
1445 : : {
1446 : : /* If BB does not dominate DEST, then the PHI nodes at
1447 : : DEST must be the only users of the results of the PHI
1448 : : nodes at BB. */
1449 : 308163 : *current++ = bb;
1450 : : }
1451 : : else
1452 : : {
1453 : 143573 : gphi_iterator gsi;
1454 : 143573 : unsigned int dest_idx = single_succ_edge (bb)->dest_idx;
1455 : :
1456 : : /* BB dominates DEST. There may be many users of the PHI
1457 : : nodes in BB. However, there is still a trivial case we
1458 : : can handle. If the result of every PHI in BB is used
1459 : : only by a PHI in DEST, then we can trivially merge the
1460 : : PHI nodes from BB into DEST. */
1461 : 260524 : for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1462 : 116951 : gsi_next (&gsi))
1463 : : {
1464 : 167349 : gphi *phi = gsi.phi ();
1465 : 167349 : tree result = gimple_phi_result (phi);
1466 : 167349 : use_operand_p imm_use;
1467 : 167349 : gimple *use_stmt;
1468 : :
1469 : : /* If the PHI's result is never used, then we can just
1470 : : ignore it. */
1471 : 167349 : if (has_zero_uses (result))
1472 : 159 : continue;
1473 : :
1474 : : /* Get the single use of the result of this PHI node. */
1475 : 167190 : if (!single_imm_use (result, &imm_use, &use_stmt)
1476 : 145722 : || gimple_code (use_stmt) != GIMPLE_PHI
1477 : 120480 : || gimple_bb (use_stmt) != dest
1478 : 284237 : || gimple_phi_arg_def (use_stmt, dest_idx) != result)
1479 : : break;
1480 : : }
1481 : :
1482 : : /* If the loop above iterated through all the PHI nodes
1483 : : in BB, then we can merge the PHIs from BB into DEST. */
1484 : 143573 : if (gsi_end_p (gsi))
1485 : 93175 : *current++ = bb;
1486 : : }
1487 : : }
1488 : :
1489 : : /* Now let's drain WORKLIST. */
1490 : : bool changed = false;
1491 : 4664332 : while (current != worklist)
1492 : : {
1493 : 401338 : bb = *--current;
1494 : 401338 : changed |= remove_forwarder_block_with_phi (bb);
1495 : : }
1496 : 4262994 : free (worklist);
1497 : :
1498 : : /* Removing forwarder blocks can cause formerly irreducible loops
1499 : : to become reducible if we merged two entry blocks. */
1500 : 4262994 : if (changed
1501 : 214791 : && current_loops)
1502 : 214791 : loops_state_set (LOOPS_NEED_FIXUP);
1503 : :
1504 : 4262994 : return 0;
1505 : : }
1506 : :
1507 : : } // anon namespace
1508 : :
1509 : : gimple_opt_pass *
1510 : 282953 : make_pass_merge_phi (gcc::context *ctxt)
1511 : : {
1512 : 282953 : return new pass_merge_phi (ctxt);
1513 : : }
1514 : :
1515 : : /* Pass: cleanup the CFG just before expanding trees to RTL.
1516 : : This is just a round of label cleanups and case node grouping
1517 : : because after the tree optimizers have run such cleanups may
1518 : : be necessary. */
1519 : :
1520 : : static unsigned int
1521 : 1433833 : execute_cleanup_cfg_post_optimizing (void)
1522 : : {
1523 : 1433833 : unsigned int todo = execute_fixup_cfg ();
1524 : 1433833 : if (cleanup_tree_cfg ())
1525 : : {
1526 : 379555 : todo &= ~TODO_cleanup_cfg;
1527 : 379555 : todo |= TODO_update_ssa;
1528 : : }
1529 : 1433833 : maybe_remove_unreachable_handlers ();
1530 : 1433833 : cleanup_dead_labels ();
1531 : 1433833 : if (group_case_labels ())
1532 : 0 : todo |= TODO_cleanup_cfg;
1533 : 1433833 : if ((flag_compare_debug_opt || flag_compare_debug)
1534 : 3895 : && flag_dump_final_insns)
1535 : : {
1536 : 3895 : FILE *final_output = fopen (flag_dump_final_insns, "a");
1537 : :
1538 : 3895 : if (!final_output)
1539 : : {
1540 : 0 : error ("could not open final insn dump file %qs: %m",
1541 : : flag_dump_final_insns);
1542 : 0 : flag_dump_final_insns = NULL;
1543 : : }
1544 : : else
1545 : : {
1546 : 3895 : int save_unnumbered = flag_dump_unnumbered;
1547 : 3895 : int save_noaddr = flag_dump_noaddr;
1548 : :
1549 : 3895 : flag_dump_noaddr = flag_dump_unnumbered = 1;
1550 : 3895 : fprintf (final_output, "\n");
1551 : 3895 : dump_enumerated_decls (final_output,
1552 : : dump_flags | TDF_SLIM | TDF_NOUID);
1553 : 3895 : flag_dump_noaddr = save_noaddr;
1554 : 3895 : flag_dump_unnumbered = save_unnumbered;
1555 : 3895 : if (fclose (final_output))
1556 : : {
1557 : 0 : error ("could not close final insn dump file %qs: %m",
1558 : : flag_dump_final_insns);
1559 : 0 : flag_dump_final_insns = NULL;
1560 : : }
1561 : : }
1562 : : }
1563 : 1433833 : return todo;
1564 : : }
1565 : :
1566 : : namespace {
1567 : :
1568 : : const pass_data pass_data_cleanup_cfg_post_optimizing =
1569 : : {
1570 : : GIMPLE_PASS, /* type */
1571 : : "optimized", /* name */
1572 : : OPTGROUP_NONE, /* optinfo_flags */
1573 : : TV_TREE_CLEANUP_CFG, /* tv_id */
1574 : : PROP_cfg, /* properties_required */
1575 : : 0, /* properties_provided */
1576 : : 0, /* properties_destroyed */
1577 : : 0, /* todo_flags_start */
1578 : : TODO_remove_unused_locals, /* todo_flags_finish */
1579 : : };
1580 : :
1581 : : class pass_cleanup_cfg_post_optimizing : public gimple_opt_pass
1582 : : {
1583 : : public:
1584 : 282953 : pass_cleanup_cfg_post_optimizing (gcc::context *ctxt)
1585 : 565906 : : gimple_opt_pass (pass_data_cleanup_cfg_post_optimizing, ctxt)
1586 : : {}
1587 : :
1588 : : /* opt_pass methods: */
1589 : 1433833 : unsigned int execute (function *) final override
1590 : : {
1591 : 1433833 : return execute_cleanup_cfg_post_optimizing ();
1592 : : }
1593 : :
1594 : : }; // class pass_cleanup_cfg_post_optimizing
1595 : :
1596 : : } // anon namespace
1597 : :
1598 : : gimple_opt_pass *
1599 : 282953 : make_pass_cleanup_cfg_post_optimizing (gcc::context *ctxt)
1600 : : {
1601 : 282953 : return new pass_cleanup_cfg_post_optimizing (ctxt);
1602 : : }
1603 : :
1604 : :
1605 : : /* Delete all unreachable basic blocks and update callgraph.
1606 : : Doing so is somewhat nontrivial because we need to update all clones and
1607 : : remove inline function that become unreachable. */
1608 : :
1609 : : bool
1610 : 1391916 : delete_unreachable_blocks_update_callgraph (cgraph_node *dst_node,
1611 : : bool update_clones)
1612 : : {
1613 : 1391916 : bool changed = false;
1614 : 1391916 : basic_block b, next_bb;
1615 : :
1616 : 1391916 : find_unreachable_blocks ();
1617 : :
1618 : : /* Delete all unreachable basic blocks. */
1619 : :
1620 : 1391916 : for (b = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; b
1621 : 26132748 : != EXIT_BLOCK_PTR_FOR_FN (cfun); b = next_bb)
1622 : : {
1623 : 24740832 : next_bb = b->next_bb;
1624 : :
1625 : 24740832 : if (!(b->flags & BB_REACHABLE))
1626 : : {
1627 : 52895 : gimple_stmt_iterator bsi;
1628 : :
1629 : 211436 : for (bsi = gsi_start_bb (b); !gsi_end_p (bsi); gsi_next (&bsi))
1630 : : {
1631 : 105646 : struct cgraph_edge *e;
1632 : 105646 : struct cgraph_node *node;
1633 : :
1634 : 105646 : dst_node->remove_stmt_references (gsi_stmt (bsi));
1635 : :
1636 : 105646 : if (gimple_code (gsi_stmt (bsi)) == GIMPLE_CALL
1637 : 105646 : &&(e = dst_node->get_edge (gsi_stmt (bsi))) != NULL)
1638 : : {
1639 : 838 : if (!e->inline_failed)
1640 : 3 : e->callee->remove_symbol_and_inline_clones (dst_node);
1641 : : else
1642 : 835 : cgraph_edge::remove (e);
1643 : : }
1644 : 105646 : if (update_clones && dst_node->clones)
1645 : 0 : for (node = dst_node->clones; node != dst_node;)
1646 : : {
1647 : 0 : node->remove_stmt_references (gsi_stmt (bsi));
1648 : 0 : if (gimple_code (gsi_stmt (bsi)) == GIMPLE_CALL
1649 : 0 : && (e = node->get_edge (gsi_stmt (bsi))) != NULL)
1650 : : {
1651 : 0 : if (!e->inline_failed)
1652 : 0 : e->callee->remove_symbol_and_inline_clones (dst_node);
1653 : : else
1654 : 0 : cgraph_edge::remove (e);
1655 : : }
1656 : :
1657 : 0 : if (node->clones)
1658 : : node = node->clones;
1659 : 0 : else if (node->next_sibling_clone)
1660 : : node = node->next_sibling_clone;
1661 : : else
1662 : : {
1663 : 0 : while (node != dst_node && !node->next_sibling_clone)
1664 : 0 : node = node->clone_of;
1665 : 0 : if (node != dst_node)
1666 : 0 : node = node->next_sibling_clone;
1667 : : }
1668 : : }
1669 : : }
1670 : 52895 : delete_basic_block (b);
1671 : 52895 : changed = true;
1672 : : }
1673 : : }
1674 : :
1675 : 1391916 : return changed;
1676 : : }
1677 : :
|