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