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