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 : 22773237 : remove_fallthru_edge (vec<edge, va_gc> *ev)
66 : : {
67 : 22773237 : edge_iterator ei;
68 : 22773237 : edge e;
69 : :
70 : 25557884 : FOR_EACH_EDGE (e, ei, ev)
71 : 2817396 : if ((e->flags & EDGE_FALLTHRU) != 0)
72 : : {
73 : 32749 : if (e->flags & EDGE_COMPLEX)
74 : 0 : e->flags &= ~EDGE_FALLTHRU;
75 : : else
76 : 32749 : remove_edge_and_dominated_blocks (e);
77 : 32749 : 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 : 895782 : convert_single_case_switch (gswitch *swtch, gimple_stmt_iterator &gsi)
87 : : {
88 : 895782 : if (gimple_switch_num_labels (swtch) != 2)
89 : : return false;
90 : :
91 : 11640 : tree index = gimple_switch_index (swtch);
92 : 11640 : tree label = gimple_switch_label (swtch, 1);
93 : 11640 : tree low = CASE_LOW (label);
94 : 11640 : tree high = CASE_HIGH (label);
95 : :
96 : 11640 : basic_block default_bb = gimple_switch_default_bb (cfun, swtch);
97 : 11640 : basic_block case_bb = label_to_block (cfun, CASE_LABEL (label));
98 : :
99 : 11640 : basic_block bb = gimple_bb (swtch);
100 : 11640 : gcond *cond;
101 : :
102 : : /* Replace switch statement with condition statement. */
103 : 11640 : if (high)
104 : : {
105 : 1102 : tree lhs, rhs;
106 : 1102 : if (range_check_type (TREE_TYPE (index)) == NULL_TREE)
107 : 0 : return false;
108 : 1102 : generate_range_test (bb, index, low, high, &lhs, &rhs);
109 : 1102 : cond = gimple_build_cond (LE_EXPR, lhs, rhs, NULL_TREE, NULL_TREE);
110 : : }
111 : : else
112 : 10538 : cond = gimple_build_cond (EQ_EXPR, index,
113 : 10538 : fold_convert (TREE_TYPE (index), low),
114 : : NULL_TREE, NULL_TREE);
115 : :
116 : 11640 : gsi_replace (&gsi, cond, true);
117 : :
118 : : /* Update edges. */
119 : 11640 : edge case_edge = find_edge (bb, case_bb);
120 : 11640 : edge default_edge = find_edge (bb, default_bb);
121 : :
122 : 11640 : case_edge->flags |= EDGE_TRUE_VALUE;
123 : 11640 : default_edge->flags |= EDGE_FALSE_VALUE;
124 : 11640 : 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 : 179733302 : canonicalize_bool_cond (gcond *stmt, basic_block bb)
131 : : {
132 : 179733302 : tree rhs1 = gimple_cond_lhs (stmt);
133 : 179733302 : tree rhs2 = gimple_cond_rhs (stmt);
134 : 179733302 : enum tree_code code = gimple_cond_code (stmt);
135 : 179733302 : if (code != EQ_EXPR && code != NE_EXPR)
136 : : return false;
137 : 135672761 : if (TREE_CODE (TREE_TYPE (rhs1)) != BOOLEAN_TYPE
138 : 135672761 : && (!INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
139 : 78375123 : || TYPE_PRECISION (TREE_TYPE (rhs1)) != 1))
140 : : return false;
141 : :
142 : : /* Canonicalize _Bool == 0 and _Bool != 1 to _Bool != 0 by swapping edges. */
143 : 24114913 : if (code == EQ_EXPR && !integer_zerop (rhs2))
144 : : return false;
145 : 23987778 : if (code == NE_EXPR && !integer_onep (rhs2))
146 : : return false;
147 : :
148 : 135965 : gimple_cond_set_code (stmt, NE_EXPR);
149 : 135965 : gimple_cond_set_rhs (stmt, build_zero_cst (TREE_TYPE (rhs1)));
150 : 135965 : EDGE_SUCC (bb, 0)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
151 : 135965 : EDGE_SUCC (bb, 1)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
152 : :
153 : 135965 : 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 : 161605273 : cleanup_control_expr_graph (basic_block bb, gimple_stmt_iterator gsi)
167 : : {
168 : 161605273 : edge taken_edge;
169 : 161605273 : bool retval = false;
170 : 161605273 : gimple *stmt = gsi_stmt (gsi);
171 : :
172 : 161605273 : if (!single_succ_p (bb))
173 : : {
174 : 161599456 : edge e;
175 : 161599456 : edge_iterator ei;
176 : 161599456 : bool warned;
177 : 161599456 : tree val = NULL_TREE;
178 : :
179 : : /* Try to convert a switch with just a single non-default case to
180 : : GIMPLE condition. */
181 : 161599456 : if (gimple_code (stmt) == GIMPLE_SWITCH
182 : 161599456 : && convert_single_case_switch (as_a<gswitch *> (stmt), gsi))
183 : 11640 : stmt = gsi_stmt (gsi);
184 : :
185 : 161599456 : if (gimple_code (stmt) == GIMPLE_COND)
186 : 160715314 : canonicalize_bool_cond (as_a<gcond*> (stmt), bb);
187 : :
188 : 161599456 : fold_defer_overflow_warnings ();
189 : 161599456 : switch (gimple_code (stmt))
190 : : {
191 : 160715314 : case GIMPLE_COND:
192 : 160715314 : {
193 : 160715314 : gimple_match_op res_op;
194 : 160715314 : if (gimple_simplify (stmt, &res_op, NULL, no_follow_ssa_edges,
195 : : no_follow_ssa_edges)
196 : 160715314 : && res_op.code == INTEGER_CST)
197 : 2544620 : val = res_op.ops[0];
198 : : }
199 : 160715314 : break;
200 : :
201 : 884142 : case GIMPLE_SWITCH:
202 : 884142 : val = gimple_switch_index (as_a <gswitch *> (stmt));
203 : 884142 : break;
204 : :
205 : 161599456 : default:
206 : 161599456 : ;
207 : : }
208 : 161599456 : taken_edge = find_taken_edge (bb, val);
209 : 161599456 : if (!taken_edge)
210 : : {
211 : 159040973 : fold_undefer_and_ignore_overflow_warnings ();
212 : 159040973 : return false;
213 : : }
214 : :
215 : : /* Remove all the edges except the one that is always executed. */
216 : 2558483 : warned = false;
217 : 7712491 : for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
218 : : {
219 : 5154008 : if (e != taken_edge)
220 : : {
221 : 2595525 : if (!warned)
222 : : {
223 : 2558483 : fold_undefer_overflow_warnings
224 : 2558483 : (true, stmt, WARN_STRICT_OVERFLOW_CONDITIONAL);
225 : 2558483 : warned = true;
226 : : }
227 : :
228 : 2595525 : taken_edge->probability += e->probability;
229 : 2595525 : remove_edge_and_dominated_blocks (e);
230 : 2595525 : retval = true;
231 : : }
232 : : else
233 : 2558483 : ei_next (&ei);
234 : : }
235 : 2558483 : if (!warned)
236 : 0 : fold_undefer_and_ignore_overflow_warnings ();
237 : : }
238 : : else
239 : 5817 : taken_edge = single_succ_edge (bb);
240 : :
241 : 2564300 : bitmap_set_bit (cfgcleanup_altered_bbs, bb->index);
242 : 2564300 : gsi_remove (&gsi, true);
243 : 2564300 : taken_edge->flags = EDGE_FALLTHRU;
244 : :
245 : 2564300 : 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 : 367286448 : cleanup_call_ctrl_altering_flag (basic_block bb, gimple *bb_end)
253 : : {
254 : 367286448 : if (!is_gimple_call (bb_end)
255 : 70319835 : || !gimple_call_ctrl_altering_p (bb_end)
256 : 390513146 : || (/* IFN_UNIQUE should be the last insn, to make checking for it
257 : : as cheap as possible. */
258 : 23226698 : gimple_call_internal_p (bb_end)
259 : 427765 : && gimple_call_internal_unique_p (bb_end)))
260 : : return;
261 : :
262 : 22824854 : int flags = gimple_call_flags (bb_end);
263 : 22824854 : if (!(flags & ECF_NORETURN)
264 : 81650 : && (((flags & (ECF_CONST | ECF_PURE))
265 : 1826 : && !(flags & ECF_LOOPING_CONST_OR_PURE))
266 : 81507 : || (flags & ECF_LEAF)))
267 : 143 : gimple_call_set_ctrl_altering (bb_end, false);
268 : : else
269 : : {
270 : 22824711 : edge_iterator ei;
271 : 22824711 : edge e;
272 : 22824711 : bool found = false;
273 : 25672192 : FOR_EACH_EDGE (e, ei, bb->succs)
274 : 2962856 : if (e->flags & EDGE_FALLTHRU)
275 : : found = true;
276 : 2853581 : 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 : 22824711 : if (found)
284 : 29948 : 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 : 411450424 : cleanup_control_flow_bb (basic_block bb)
293 : : {
294 : 411450424 : gimple_stmt_iterator gsi;
295 : 411450424 : bool retval = false;
296 : 411450424 : gimple *stmt;
297 : :
298 : : /* If the last statement of the block could throw and now cannot,
299 : : we need to prune cfg. */
300 : 411450424 : retval |= gimple_purge_dead_eh_edges (bb);
301 : :
302 : 411450424 : gsi = gsi_last_nondebug_bb (bb);
303 : 411450424 : if (gsi_end_p (gsi))
304 : : return retval;
305 : :
306 : 367286448 : stmt = gsi_stmt (gsi);
307 : :
308 : : /* Try to cleanup ctrl altering flag for call which ends bb. */
309 : 367286448 : cleanup_call_ctrl_altering_flag (bb, stmt);
310 : :
311 : 367286448 : if (gimple_code (stmt) == GIMPLE_COND
312 : 367286448 : || gimple_code (stmt) == GIMPLE_SWITCH)
313 : : {
314 : 323210546 : gcc_checking_assert (gsi_stmt (gsi_last_bb (bb)) == stmt);
315 : 161605273 : retval |= cleanup_control_expr_graph (bb, gsi);
316 : : }
317 : 205681175 : else if (gimple_code (stmt) == GIMPLE_GOTO
318 : 6101 : && TREE_CODE (gimple_goto_dest (stmt)) == ADDR_EXPR
319 : 205681404 : && (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 : 205681004 : else if (is_gimple_call (stmt)
364 : 205681004 : && 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 : 22773414 : for (gsi_next (&gsi); !gsi_end_p (gsi); )
369 : 177 : gsi_remove (&gsi, true);
370 : 22773237 : if (remove_fallthru_edge (bb->succs))
371 : 32749 : retval = true;
372 : 22773237 : tree lhs = gimple_call_lhs (stmt);
373 : 22773237 : if (!lhs
374 : 22773237 : || !should_remove_lhs_p (lhs))
375 : 22773218 : 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 : 438132267 : tree_forwarder_block_p (basic_block bb, bool phi_wanted)
390 : : {
391 : 438132267 : gimple_stmt_iterator gsi;
392 : 438132267 : location_t locus;
393 : :
394 : : /* BB must have a single outgoing edge. */
395 : 830666245 : 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 : 206094092 : || gimple_seq_empty_p (phi_nodes (bb)) == phi_wanted
399 : : /* BB may not be a predecessor of the exit block. */
400 : 155340397 : || single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun)
401 : : /* Nor should this be an infinite loop. */
402 : 135363466 : || single_succ (bb) == bb
403 : : /* BB may not have an abnormal outgoing edge. */
404 : 560917833 : || (single_succ_edge (bb)->flags & EDGE_ABNORMAL))
405 : : return false;
406 : :
407 : 135274653 : gcc_checking_assert (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun));
408 : :
409 : 135274653 : locus = single_succ_edge (bb)->goto_locus;
410 : :
411 : : /* There should not be an edge coming from entry, or an EH edge. */
412 : 135274653 : {
413 : 135274653 : edge_iterator ei;
414 : 135274653 : edge e;
415 : :
416 : 256466510 : FOR_EACH_EDGE (e, ei, bb->preds)
417 : 142953833 : if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun) || (e->flags & EDGE_EH))
418 : 21761976 : return false;
419 : : /* If goto_locus of any of the edges differs, prevent removing
420 : : the forwarder block when not optimizing. */
421 : 122501481 : else if (!optimize
422 : 4841484 : && (LOCATION_LOCUS (e->goto_locus) != UNKNOWN_LOCATION
423 : 4374680 : || LOCATION_LOCUS (locus) != UNKNOWN_LOCATION)
424 : 123811239 : && 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 : 437451812 : for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
431 : : {
432 : 184479128 : gimple *stmt = gsi_stmt (gsi);
433 : :
434 : 184479128 : switch (gimple_code (stmt))
435 : : {
436 : 852874 : case GIMPLE_LABEL:
437 : 852874 : if (DECL_NONLOCAL (gimple_label_label (as_a <glabel *> (stmt))))
438 : : return false;
439 : 845594 : if (!optimize
440 : 30677 : && (gimple_has_location (stmt)
441 : 2322 : || LOCATION_LOCUS (locus) != UNKNOWN_LOCATION)
442 : 873949 : && 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 : 34246778 : if (current_loops)
457 : : {
458 : 33260994 : basic_block dest;
459 : : /* Protect loop headers. */
460 : 33260994 : if (bb_loop_header_p (bb))
461 : : return false;
462 : :
463 : 33188657 : dest = EDGE_SUCC (bb, 0)->dest;
464 : : /* Protect loop preheaders and latches if requested. */
465 : 33188657 : if (dest->loop_father->header == dest)
466 : : {
467 : 10746233 : if (bb->loop_father == dest->loop_father)
468 : : {
469 : 6710246 : 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 : 5718578 : return (single_pred_p (bb)
475 : 5642011 : || loops_state_satisfies_p
476 : 76567 : (LOOPS_MAY_HAVE_MULTIPLE_LATCHES));
477 : : }
478 : 4035987 : else if (bb->loop_father == loop_outer (dest->loop_father))
479 : 4024945 : 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 : 3522035 : phi_alternatives_equal (basic_block dest, edge e1, edge e2)
495 : : {
496 : 3522035 : int n1 = e1->dest_idx;
497 : 3522035 : int n2 = e2->dest_idx;
498 : 3522035 : gphi_iterator gsi;
499 : :
500 : 3865526 : for (gsi = gsi_start_phis (dest); !gsi_end_p (gsi); gsi_next (&gsi))
501 : : {
502 : 3815329 : gphi *phi = gsi.phi ();
503 : 3815329 : tree val1 = gimple_phi_arg_def (phi, n1);
504 : 3815329 : tree val2 = gimple_phi_arg_def (phi, n2);
505 : :
506 : 3815329 : gcc_assert (val1 != NULL_TREE);
507 : 3815329 : gcc_assert (val2 != NULL_TREE);
508 : :
509 : 3815329 : 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 : 29272739 : 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 : 29272739 : if (!MAY_HAVE_DEBUG_STMTS)
524 : 10304647 : return;
525 : :
526 : : /* If we cannot move to the destination but to the predecessor do that. */
527 : 19499633 : if (!dest_single_pred_p && pred_single_succ_p)
528 : : {
529 : 531635 : gimple_stmt_iterator gsi_to = gsi_last_bb (pred);
530 : 531635 : if (gsi_end_p (gsi_to) || !stmt_ends_bb_p (gsi_stmt (gsi_to)))
531 : : {
532 : 531541 : for (gimple_stmt_iterator gsi = gsi_after_labels (src);
533 : 1860108 : !gsi_end_p (gsi);)
534 : : {
535 : 1328567 : gimple *debug = gsi_stmt (gsi);
536 : 1328567 : gcc_assert (is_gimple_debug (debug));
537 : 1328567 : gsi_move_after (&gsi, &gsi_to);
538 : : }
539 : 531541 : return;
540 : : }
541 : : }
542 : :
543 : : /* Else move to DEST or drop/reset them. */
544 : 18968092 : gimple_stmt_iterator gsi_to = gsi_after_labels (dest);
545 : 37461496 : for (gimple_stmt_iterator gsi = gsi_after_labels (src); !gsi_end_p (gsi);)
546 : : {
547 : 18493404 : gimple *debug = gsi_stmt (gsi);
548 : 18493404 : 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 : 18493404 : if (dest_single_pred_p
552 : 18493404 : || gimple_debug_bind_p (debug))
553 : : {
554 : 17564260 : 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 : 17564260 : if (!dest_single_pred_p)
564 : : {
565 : 5028127 : gimple_debug_bind_reset_value (debug);
566 : 5028127 : update_stmt (debug);
567 : : }
568 : : }
569 : : else
570 : 929144 : gsi_next (&gsi);
571 : : }
572 : : }
573 : :
574 : : /* Removes forwarder block BB. Returns false if this failed. */
575 : :
576 : : static bool
577 : 32202429 : remove_forwarder_block (basic_block bb)
578 : : {
579 : 32202429 : edge succ = single_succ_edge (bb), e, s;
580 : 32202429 : basic_block dest = succ->dest;
581 : 32202429 : gimple *stmt;
582 : 32202429 : edge_iterator ei;
583 : 32202429 : 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 : 32202429 : 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 : 32202429 : stmt = first_stmt (dest);
594 : 32202429 : if (stmt)
595 : 28967325 : if (glabel *label_stmt = dyn_cast <glabel *> (stmt))
596 : 1377338 : if (DECL_NONLOCAL (gimple_label_label (label_stmt))
597 : 1377338 : || 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 : 32187782 : if (bb_has_abnormal_pred (bb)
612 : 32187782 : && (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 : 32185369 : if (!gimple_seq_empty_p (phi_nodes (dest)))
620 : : {
621 : 48468024 : FOR_EACH_EDGE (e, ei, bb->preds)
622 : : {
623 : 26411064 : s = find_edge (e->src, dest);
624 : 26411064 : if (!s)
625 : 23011680 : continue;
626 : :
627 : 3399384 : if (!phi_alternatives_equal (dest, succ, s))
628 : : return false;
629 : : }
630 : : }
631 : :
632 : 28835233 : basic_block pred = NULL;
633 : 28835233 : if (single_pred_p (bb))
634 : 28000526 : pred = single_pred (bb);
635 : 28835233 : bool dest_single_pred_p = single_pred_p (dest);
636 : :
637 : : /* Redirect the edges. */
638 : 87776789 : for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
639 : : {
640 : 30106323 : bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index);
641 : :
642 : 30106323 : 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 : 30106139 : s = redirect_edge_and_branch (e, dest);
650 : :
651 : 30106323 : if (s == e)
652 : : {
653 : : /* Copy arguments for the phi nodes, since the edge was not
654 : : here before. */
655 : 29994986 : 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 : 28835233 : gsi_to = gsi_start_bb (dest);
665 : 57997692 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
666 : : {
667 : 3671426 : stmt = gsi_stmt (gsi);
668 : 3671426 : 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 : 327226 : tree decl = gimple_label_label (as_a <glabel *> (stmt));
675 : 327226 : if (EH_LANDING_PAD_NR (decl) != 0
676 : 327226 : || DECL_NONLOCAL (decl)
677 : 327226 : || FORCED_LABEL (decl)
678 : 649044 : || !DECL_ARTIFICIAL (decl))
679 : 23913 : gsi_move_before (&gsi, &gsi_to);
680 : : else
681 : 303313 : gsi_next (&gsi);
682 : : }
683 : :
684 : : /* Move debug statements. Reset them if the destination does not
685 : : have a single predecessor. */
686 : 57670466 : move_debug_stmts_from_forwarder (bb, dest, dest_single_pred_p,
687 : 83573811 : pred, pred && single_succ_p (pred));
688 : :
689 : 28835233 : bitmap_set_bit (cfgcleanup_altered_bbs, dest->index);
690 : :
691 : : /* Update the dominators. */
692 : 28835233 : if (dom_info_available_p (CDI_DOMINATORS))
693 : : {
694 : 28835233 : basic_block dom, dombb, domdest;
695 : :
696 : 28835233 : dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
697 : 28835233 : domdest = get_immediate_dominator (CDI_DOMINATORS, dest);
698 : 28835233 : if (domdest == bb)
699 : : {
700 : : /* Shortcut to avoid calling (relatively expensive)
701 : : nearest_common_dominator unless necessary. */
702 : : dom = dombb;
703 : : }
704 : : else
705 : 20930286 : dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb);
706 : :
707 : 28835233 : 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 : 28835233 : if (current_loops && bb->loop_father->latch == bb)
713 : 5538765 : bb->loop_father->latch = pred;
714 : :
715 : : /* And kill the forwarder block. */
716 : 28835233 : delete_basic_block (bb);
717 : :
718 : 28835233 : 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 : 6231178 : fixup_noreturn_call (gimple *stmt)
727 : : {
728 : 6231178 : basic_block bb = gimple_bb (stmt);
729 : 6231178 : bool changed = false;
730 : :
731 : 6231178 : if (gimple_call_builtin_p (stmt, BUILT_IN_RETURN))
732 : : return false;
733 : :
734 : : /* First split basic block if stmt is not last. */
735 : 12458572 : if (stmt != gsi_stmt (gsi_last_bb (bb)))
736 : : {
737 : 51791 : 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 : 6917 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
744 : 51531 : for (gsi_next (&gsi); !gsi_end_p (gsi); )
745 : 44614 : gsi_remove (&gsi, true);
746 : : }
747 : : else
748 : : {
749 : 44874 : split_block (bb, stmt);
750 : 44874 : 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 : 6229286 : tree lhs = gimple_call_lhs (stmt);
762 : 6229286 : if (lhs
763 : 6229286 : && (should_remove_lhs_p (lhs)
764 : 414 : || VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (stmt)))))
765 : : {
766 : 5382 : gimple_call_set_lhs (stmt, NULL_TREE);
767 : :
768 : : /* We need to fix up the SSA name to avoid checking errors. */
769 : 5382 : if (TREE_CODE (lhs) == SSA_NAME)
770 : : {
771 : 5181 : tree new_var = create_tmp_reg (TREE_TYPE (lhs));
772 : 5181 : SET_SSA_NAME_VAR_OR_IDENTIFIER (lhs, new_var);
773 : 5181 : SSA_NAME_DEF_STMT (lhs) = gimple_build_nop ();
774 : 5181 : set_ssa_default_def (cfun, new_var, lhs);
775 : : }
776 : :
777 : 5382 : update_stmt (stmt);
778 : : }
779 : :
780 : : /* Mark the call as altering control flow. */
781 : 6229286 : if (!gimple_call_ctrl_altering_p (stmt))
782 : : {
783 : 92380 : gimple_call_set_ctrl_altering (stmt, true);
784 : 92380 : 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 : 424577036 : want_merge_blocks_p (basic_block bb1, basic_block bb2)
794 : : {
795 : 424577036 : if (!can_merge_blocks_p (bb1, bb2))
796 : : return false;
797 : 25394082 : gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb1);
798 : 25394082 : if (gsi_end_p (gsi) || !stmt_can_terminate_bb_p (gsi_stmt (gsi)))
799 : 22378598 : return true;
800 : 3015484 : 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 : 403745805 : cleanup_tree_cfg_bb (basic_block bb)
809 : : {
810 : 403745805 : if (tree_forwarder_block_p (bb, false)
811 : 403745805 : && 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 : 374910572 : if (single_pred_p (bb)
819 : 644104919 : && 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 : 9484755 : 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 : 365425817 : else if (single_succ_p (bb)
829 : 509927178 : && want_merge_blocks_p (bb, single_succ (bb)))
830 : : {
831 : 15909273 : merge_blocks (bb, single_succ (bb));
832 : 15909273 : 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 : 361098309 : maybe_dead_abnormal_edge_p (edge e)
850 : : {
851 : 361098309 : if ((e->flags & (EDGE_ABNORMAL | EDGE_EH)) != EDGE_ABNORMAL)
852 : : return false;
853 : :
854 : 148317 : gimple_stmt_iterator gsi = gsi_start_nondebug_after_labels_bb (e->src);
855 : 148317 : gimple *g = gsi_stmt (gsi);
856 : 148317 : if (!g || !gimple_call_internal_p (g, IFN_ABNORMAL_DISPATCHER))
857 : : return false;
858 : :
859 : 20268 : tree target = NULL_TREE;
860 : 52943 : for (gsi = gsi_start_bb (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
861 : 32668 : if (glabel *label_stmt = dyn_cast <glabel *> (gsi_stmt (gsi)))
862 : : {
863 : 18999 : tree this_target = gimple_label_label (label_stmt);
864 : 18999 : 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 : 361097016 : builtin_setjmp_setup_bb (basic_block bb)
899 : : {
900 : 361097016 : if (EDGE_COUNT (bb->succs) != 2
901 : 350265897 : || ((EDGE_SUCC (bb, 0)->flags
902 : 158566333 : & (EDGE_ABNORMAL | EDGE_EH)) != EDGE_ABNORMAL
903 : 158465321 : && (EDGE_SUCC (bb, 1)->flags
904 : 158465321 : & (EDGE_ABNORMAL | EDGE_EH)) != EDGE_ABNORMAL))
905 : : return NULL;
906 : :
907 : 172662 : gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
908 : 172662 : if (gsi_end_p (gsi)
909 : 172662 : || !gimple_call_builtin_p (gsi_stmt (gsi), BUILT_IN_SETJMP_SETUP))
910 : 160279 : 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 : 361097016 : 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 : 25884504 : cleanup_control_flow_pre ()
933 : : {
934 : 25884504 : 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 : 25884504 : dom_state saved_state = dom_info_state (CDI_DOMINATORS);
940 : 25884504 : set_dom_info_availability (CDI_DOMINATORS, DOM_NONE);
941 : :
942 : 25884504 : auto_vec<edge_iterator, 20> stack (n_basic_blocks_for_fn (cfun) + 2);
943 : 25884504 : auto_sbitmap visited (last_basic_block_for_fn (cfun));
944 : 25884504 : bitmap_clear (visited);
945 : :
946 : 25884504 : vec<edge, va_gc> *setjmp_vec = NULL;
947 : 25884504 : auto_vec<basic_block, 4> abnormal_dispatchers;
948 : :
949 : 25884504 : stack.quick_push (ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs));
950 : :
951 : 913494685 : while (! stack.is_empty ())
952 : : {
953 : : /* Look at the edge on the top of the stack. */
954 : 887610181 : edge_iterator ei = stack.last ();
955 : 887610181 : basic_block dest = ei_edge (ei)->dest;
956 : :
957 : 887610181 : if (dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
958 : 862122871 : && !bitmap_bit_p (visited, dest->index)
959 : 1248720873 : && (ei_container (ei) == setjmp_vec
960 : 361098309 : || !maybe_dead_abnormal_edge_p (ei_edge (ei))))
961 : : {
962 : 361097016 : bitmap_set_bit (visited, dest->index);
963 : : /* We only possibly remove edges from DEST here, leaving
964 : : possibly unreachable code in the IL. */
965 : 361097016 : 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 : 361097016 : 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 : 361097016 : if ((ei_edge (ei)->flags
980 : 361097016 : & (EDGE_ABNORMAL | EDGE_EH)) == EDGE_ABNORMAL)
981 : : {
982 : 147024 : gimple_stmt_iterator gsi
983 : 147024 : = gsi_start_nondebug_after_labels_bb (dest);
984 : 147024 : gimple *g = gsi_stmt (gsi);
985 : 147024 : if (g && gimple_call_internal_p (g, IFN_ABNORMAL_DISPATCHER))
986 : 25829 : abnormal_dispatchers.safe_push (dest);
987 : : }
988 : :
989 : 1248707197 : if (EDGE_COUNT (dest->succs) > 0)
990 : 338393819 : stack.quick_push (ei_start (dest->succs));
991 : : }
992 : : else
993 : : {
994 : 526513165 : if (!ei_one_before_end_p (ei))
995 : 162228320 : ei_next (&stack.last ());
996 : : else
997 : : {
998 : 364284845 : if (ei_container (ei) == setjmp_vec)
999 : 6522 : vec_safe_truncate (setjmp_vec, 0);
1000 : 364284845 : stack.pop ();
1001 : : }
1002 : : }
1003 : : }
1004 : :
1005 : 25884504 : 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 : 77679341 : for (basic_block dispatcher_bb : abnormal_dispatchers)
1011 : : {
1012 : 25829 : edge e;
1013 : 25829 : edge_iterator ei;
1014 : 25920 : FOR_EACH_EDGE (e, ei, dispatcher_bb->succs)
1015 : 25829 : if (bitmap_bit_p (visited, e->dest->index))
1016 : : break;
1017 : 25829 : if (e == NULL)
1018 : 91 : bitmap_clear_bit (visited, dispatcher_bb->index);
1019 : : }
1020 : :
1021 : 25884504 : 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 : 25884504 : if (retval)
1026 : 849564 : free_dominance_info (CDI_DOMINATORS);
1027 : :
1028 : : /* Remove all now (and previously) unreachable blocks. */
1029 : 409489211 : for (int i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); ++i)
1030 : : {
1031 : 383604707 : basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
1032 : 383604707 : if (bb && !bitmap_bit_p (visited, bb->index))
1033 : : {
1034 : 4585174 : if (!retval)
1035 : 781543 : free_dominance_info (CDI_DOMINATORS);
1036 : 4585174 : delete_basic_block (bb);
1037 : 4585174 : retval = true;
1038 : : }
1039 : : }
1040 : :
1041 : 25884504 : return retval;
1042 : 25884504 : }
1043 : :
1044 : : static bool
1045 : 175249 : mfb_keep_latches (edge e)
1046 : : {
1047 : 175249 : return !((dom_info_available_p (CDI_DOMINATORS)
1048 : 43557 : && dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
1049 : 161817 : || (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 : 25884504 : cleanup_tree_cfg_noloop (unsigned ssa_update_flags)
1057 : : {
1058 : 25884504 : 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 : 25884504 : if (current_loops)
1068 : : {
1069 : : /* This needs backedges or dominators. */
1070 : 22904706 : if (!dom_info_available_p (CDI_DOMINATORS))
1071 : 6964606 : mark_dfs_back_edges ();
1072 : :
1073 : 95226961 : for (loop_p loop : *get_loops (cfun))
1074 : 49417549 : if (loop && loop->header)
1075 : : {
1076 : 42092728 : basic_block bb = loop->header;
1077 : 42092728 : edge_iterator ei;
1078 : 42092728 : edge e;
1079 : 42092728 : bool found_latch = false;
1080 : 42092728 : bool any_abnormal = false;
1081 : 42092728 : 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 : 80648639 : FOR_EACH_EDGE (e, ei, bb->preds)
1086 : : {
1087 : 38555911 : if (e->flags & EDGE_ABNORMAL)
1088 : : {
1089 : : any_abnormal = true;
1090 : : break;
1091 : : }
1092 : 38555911 : if ((dom_info_available_p (CDI_DOMINATORS)
1093 : 28107301 : && dominated_by_p (CDI_DOMINATORS, e->src, bb))
1094 : 52593777 : || (e->flags & EDGE_DFS_BACK))
1095 : : {
1096 : 19302969 : found_latch = true;
1097 : 19302969 : continue;
1098 : : }
1099 : 19252942 : n++;
1100 : : }
1101 : : /* If we have more than one entry to the loop header
1102 : : create a forwarder. */
1103 : 42092728 : if (found_latch && ! any_abnormal && n > 1)
1104 : : {
1105 : 53361 : edge fallthru = make_forwarder_block (bb, mfb_keep_latches,
1106 : : NULL);
1107 : 53361 : loop->header = fallthru->dest;
1108 : 53361 : 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 : 26398 : remove_bb_from_loops (fallthru->src);
1113 : 26398 : loop_p cloop = loop;
1114 : 82968 : FOR_EACH_EDGE (e, ei, fallthru->src->preds)
1115 : 56570 : cloop = find_common_loop (cloop, e->src->loop_father);
1116 : 26398 : add_bb_to_loop (fallthru->src, cloop);
1117 : : }
1118 : : }
1119 : : }
1120 : : }
1121 : :
1122 : : /* Prepare the worklists of altered blocks. */
1123 : 25884504 : 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 : 25884504 : 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 : 25884504 : if (ssa_update_flags)
1135 : : {
1136 : 17020632 : timevar_pop (TV_TREE_CLEANUP_CFG);
1137 : 17020632 : update_ssa (ssa_update_flags);
1138 : 17020632 : 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 : 25884504 : if (!dom_info_available_p (CDI_DOMINATORS))
1145 : 8775587 : calculate_dominance_info (CDI_DOMINATORS, false);
1146 : : else
1147 : 17108917 : 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 : 25884504 : 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 : 25884504 : unsigned n = last_basic_block_for_fn (cfun);
1158 : 409489211 : for (unsigned i = NUM_FIXED_BLOCKS; i < n; i++)
1159 : : {
1160 : 383604707 : basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
1161 : 383604707 : if (bb)
1162 : 353392397 : changed |= cleanup_tree_cfg_bb (bb);
1163 : : }
1164 : :
1165 : : /* Now process the altered blocks, as long as any are available. */
1166 : 84274227 : while (!bitmap_empty_p (cfgcleanup_altered_bbs))
1167 : : {
1168 : 58389723 : unsigned i = bitmap_clear_first_set_bit (cfgcleanup_altered_bbs);
1169 : 58389723 : if (i < NUM_FIXED_BLOCKS)
1170 : 0 : continue;
1171 : :
1172 : 58389723 : basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
1173 : 58389723 : if (!bb)
1174 : 8036315 : 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 : 50353408 : changed |= cleanup_control_flow_bb (bb);
1181 : 50353408 : changed |= cleanup_tree_cfg_bb (bb);
1182 : : }
1183 : :
1184 : 25884504 : end_recording_case_labels ();
1185 : 25884504 : BITMAP_FREE (cfgcleanup_altered_bbs);
1186 : :
1187 : 25884504 : 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 : 25884504 : if (! scev_initialized_p ())
1192 : 25594038 : compact_blocks ();
1193 : :
1194 : 25884504 : checking_verify_flow_info ();
1195 : :
1196 : 25884504 : timevar_pop (TV_TREE_CLEANUP_CFG);
1197 : :
1198 : 25884504 : 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 : 6138941 : free_numbers_of_iterations_estimates (cfun);
1203 : 6138941 : loops_state_set (LOOPS_NEED_FIXUP);
1204 : : }
1205 : :
1206 : 25884504 : return changed;
1207 : : }
1208 : :
1209 : : /* Repairs loop structures. */
1210 : :
1211 : : static void
1212 : 7883329 : repair_loop_structures (void)
1213 : : {
1214 : 7883329 : bitmap changed_bbs;
1215 : 7883329 : unsigned n_new_or_deleted_loops;
1216 : :
1217 : 7883329 : calculate_dominance_info (CDI_DOMINATORS);
1218 : :
1219 : 7883329 : timevar_push (TV_REPAIR_LOOPS);
1220 : 7883329 : changed_bbs = BITMAP_ALLOC (NULL);
1221 : 7883329 : 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 : 7883329 : if (loops_state_satisfies_p (LOOP_CLOSED_SSA)
1229 : 7883329 : && (!bitmap_empty_p (changed_bbs) || n_new_or_deleted_loops))
1230 : 23662 : rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
1231 : :
1232 : 7883329 : BITMAP_FREE (changed_bbs);
1233 : :
1234 : 7883329 : checking_verify_loop_structure ();
1235 : 7883329 : scev_reset ();
1236 : :
1237 : 7883329 : timevar_pop (TV_REPAIR_LOOPS);
1238 : 7883329 : }
1239 : :
1240 : : /* Cleanup cfg and repair loop structures. */
1241 : :
1242 : : bool
1243 : 25884504 : cleanup_tree_cfg (unsigned ssa_update_flags)
1244 : : {
1245 : 25884504 : bool changed = cleanup_tree_cfg_noloop (ssa_update_flags);
1246 : :
1247 : 25884504 : if (current_loops != NULL
1248 : 25884504 : && loops_state_satisfies_p (LOOPS_NEED_FIXUP))
1249 : 7883329 : repair_loop_structures ();
1250 : :
1251 : 25884504 : 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 : 437508 : remove_forwarder_block_with_phi (basic_block bb)
1259 : : {
1260 : 437508 : edge succ = single_succ_edge (bb);
1261 : 437508 : basic_block dest = succ->dest;
1262 : 437508 : gimple *label;
1263 : 437508 : 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 : 437508 : 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 : 437507 : 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 : 437506 : label = first_stmt (dest);
1279 : 437506 : if (label)
1280 : 429385 : if (glabel *label_stmt = dyn_cast <glabel *> (label))
1281 : 13850 : 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 : 437506 : basic_block pred = NULL;
1287 : 437506 : if (single_pred_p (bb))
1288 : 83116 : pred = single_pred (bb);
1289 : 437506 : bool dest_single_pred_p = single_pred_p (dest);
1290 : :
1291 : : /* Redirect each incoming edge to BB to DEST. */
1292 : 1493943 : while (EDGE_COUNT (bb->preds) > 0)
1293 : : {
1294 : 1056437 : edge e = EDGE_PRED (bb, 0), s;
1295 : 1056437 : gphi_iterator gsi;
1296 : :
1297 : 1056437 : s = find_edge (e->src, dest);
1298 : 1056437 : 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 : 26916 : 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 : 26915 : 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 : 1029521 : if (dest->loop_father->header == dest
1321 : 1029521 : && dominated_by_p (CDI_DOMINATORS, e->src, dest))
1322 : : {
1323 : 200623 : dest->loop_father->any_upper_bound = false;
1324 : 200623 : dest->loop_father->any_likely_upper_bound = false;
1325 : 200623 : free_numbers_of_iterations_estimates (dest->loop_father);
1326 : : }
1327 : : }
1328 : :
1329 : 1056436 : s = redirect_edge_and_branch (e, dest);
1330 : :
1331 : : /* redirect_edge_and_branch must not create a new edge. */
1332 : 1056436 : gcc_assert (s == e);
1333 : :
1334 : : /* Add to the PHI nodes at DEST each PHI argument removed at the
1335 : : destination of E. */
1336 : 1056436 : for (gsi = gsi_start_phis (dest);
1337 : 3334898 : !gsi_end_p (gsi);
1338 : 2278462 : gsi_next (&gsi))
1339 : : {
1340 : 2278462 : gphi *phi = gsi.phi ();
1341 : 2278462 : tree def = gimple_phi_arg_def (phi, succ->dest_idx);
1342 : 2278462 : location_t locus = gimple_phi_arg_location_from_edge (phi, succ);
1343 : :
1344 : 2278462 : 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 : 1941072 : vec<edge_var_map> *head = redirect_edge_var_map_vector (e);
1350 : 3882144 : size_t length = head ? head->length () : 0;
1351 : 3518725 : for (size_t i = 0; i < length; i++)
1352 : : {
1353 : 3005252 : edge_var_map *vm = &(*head)[i];
1354 : 3005252 : tree old_arg = redirect_edge_var_map_result (vm);
1355 : 3005252 : tree new_arg = redirect_edge_var_map_def (vm);
1356 : :
1357 : 3005252 : if (def == old_arg)
1358 : : {
1359 : 1427599 : def = new_arg;
1360 : 1427599 : locus = redirect_edge_var_map_location (vm);
1361 : 1427599 : break;
1362 : : }
1363 : : }
1364 : : }
1365 : :
1366 : 2278462 : add_phi_arg (phi, def, s, locus);
1367 : : }
1368 : :
1369 : 1056436 : 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 : 875012 : move_debug_stmts_from_forwarder (bb, dest, dest_single_pred_p,
1375 : 958128 : pred, pred && single_succ_p (pred));
1376 : :
1377 : : /* Update the dominators. */
1378 : 437506 : dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
1379 : 437506 : domdest = get_immediate_dominator (CDI_DOMINATORS, dest);
1380 : 437506 : if (domdest == bb)
1381 : : {
1382 : : /* Shortcut to avoid calling (relatively expensive)
1383 : : nearest_common_dominator unless necessary. */
1384 : : dom = dombb;
1385 : : }
1386 : : else
1387 : 340691 : dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb);
1388 : :
1389 : 437506 : 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 : 437506 : if (current_loops && bb->loop_father->latch == bb)
1394 : 82540 : bb->loop_father->latch = pred;
1395 : :
1396 : : /* Remove BB since all of BB's incoming edges have been redirected
1397 : : to DEST. */
1398 : 437506 : delete_basic_block (bb);
1399 : :
1400 : 437506 : 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 : 857067 : pass_merge_phi (gcc::context *ctxt)
1447 : 1714134 : : gimple_opt_pass (pass_data_merge_phi, ctxt)
1448 : : {}
1449 : :
1450 : : /* opt_pass methods: */
1451 : 571378 : 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 : 4597104 : pass_merge_phi::execute (function *fun)
1458 : : {
1459 : 4597104 : basic_block *worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (fun));
1460 : 4597104 : basic_block *current = worklist;
1461 : 4597104 : basic_block bb;
1462 : :
1463 : 4597104 : calculate_dominance_info (CDI_DOMINATORS);
1464 : :
1465 : : /* Find all PHI nodes that we may be able to merge. */
1466 : 38983566 : FOR_EACH_BB_FN (bb, fun)
1467 : : {
1468 : 34386462 : basic_block dest;
1469 : :
1470 : : /* Look for a forwarder block with PHI nodes. */
1471 : 34386462 : if (!tree_forwarder_block_p (bb, true))
1472 : 33900643 : continue;
1473 : :
1474 : 485819 : dest = single_succ (bb);
1475 : :
1476 : : /* We have to feed into another basic block with PHI
1477 : : nodes. */
1478 : 485819 : if (gimple_seq_empty_p (phi_nodes (dest))
1479 : : /* We don't want to deal with a basic block with
1480 : : abnormal edges. */
1481 : 485819 : || bb_has_abnormal_pred (bb))
1482 : 509 : continue;
1483 : :
1484 : 485310 : 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 : 340692 : *current++ = bb;
1490 : : }
1491 : : else
1492 : : {
1493 : 144618 : gphi_iterator gsi;
1494 : 144618 : 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 : 267519 : for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1502 : 122901 : gsi_next (&gsi))
1503 : : {
1504 : 170703 : gphi *phi = gsi.phi ();
1505 : 170703 : tree result = gimple_phi_result (phi);
1506 : 170703 : use_operand_p imm_use;
1507 : 170703 : gimple *use_stmt;
1508 : :
1509 : : /* If the PHI's result is never used, then we can just
1510 : : ignore it. */
1511 : 170703 : if (has_zero_uses (result))
1512 : 295 : continue;
1513 : :
1514 : : /* Get the single use of the result of this PHI node. */
1515 : 170408 : if (!single_imm_use (result, &imm_use, &use_stmt)
1516 : 148226 : || gimple_code (use_stmt) != GIMPLE_PHI
1517 : 126561 : || gimple_bb (use_stmt) != dest
1518 : 293269 : || 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 : 144618 : if (gsi_end_p (gsi))
1525 : 96816 : *current++ = bb;
1526 : : }
1527 : : }
1528 : :
1529 : : /* Now let's drain WORKLIST. */
1530 : : bool changed = false;
1531 : 5034612 : while (current != worklist)
1532 : : {
1533 : 437508 : bb = *--current;
1534 : 437508 : changed |= remove_forwarder_block_with_phi (bb);
1535 : : }
1536 : 4597104 : free (worklist);
1537 : :
1538 : : /* Removing forwarder blocks can cause formerly irreducible loops
1539 : : to become reducible if we merged two entry blocks. */
1540 : 4597104 : if (changed
1541 : 232571 : && current_loops)
1542 : 232571 : loops_state_set (LOOPS_NEED_FIXUP);
1543 : :
1544 : 4597104 : return 0;
1545 : : }
1546 : :
1547 : : } // anon namespace
1548 : :
1549 : : gimple_opt_pass *
1550 : 285689 : make_pass_merge_phi (gcc::context *ctxt)
1551 : : {
1552 : 285689 : 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 : 1482106 : execute_cleanup_cfg_post_optimizing (void)
1562 : : {
1563 : 1482106 : unsigned int todo = execute_fixup_cfg ();
1564 : 1482106 : if (cleanup_tree_cfg ())
1565 : : {
1566 : 395855 : todo &= ~TODO_cleanup_cfg;
1567 : 395855 : todo |= TODO_update_ssa;
1568 : : }
1569 : 1482106 : maybe_remove_unreachable_handlers ();
1570 : 1482106 : cleanup_dead_labels ();
1571 : 1482106 : if (group_case_labels ())
1572 : 0 : todo |= TODO_cleanup_cfg;
1573 : :
1574 : 1482106 : basic_block bb = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
1575 : 1482106 : 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 : 1482106 : if (!gsi_end_p (gsi)
1580 : 1482106 : && 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 : 1482106 : 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 : 1482106 : 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 : 285689 : pass_cleanup_cfg_post_optimizing (gcc::context *ctxt)
1648 : 571378 : : gimple_opt_pass (pass_data_cleanup_cfg_post_optimizing, ctxt)
1649 : : {}
1650 : :
1651 : : /* opt_pass methods: */
1652 : 1482106 : unsigned int execute (function *) final override
1653 : : {
1654 : 1482106 : 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 : 285689 : make_pass_cleanup_cfg_post_optimizing (gcc::context *ctxt)
1663 : : {
1664 : 285689 : 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 : 1640395 : delete_unreachable_blocks_update_callgraph (cgraph_node *dst_node,
1674 : : bool update_clones)
1675 : : {
1676 : 1640395 : bool changed = false;
1677 : 1640395 : basic_block b, next_bb;
1678 : :
1679 : 1640395 : find_unreachable_blocks ();
1680 : :
1681 : : /* Delete all unreachable basic blocks. */
1682 : :
1683 : 1640395 : for (b = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; b
1684 : 31216256 : != EXIT_BLOCK_PTR_FOR_FN (cfun); b = next_bb)
1685 : : {
1686 : 29575861 : next_bb = b->next_bb;
1687 : :
1688 : 29575861 : if (!(b->flags & BB_REACHABLE))
1689 : : {
1690 : 36714 : gimple_stmt_iterator bsi;
1691 : :
1692 : 146916 : for (bsi = gsi_start_bb (b); !gsi_end_p (bsi); gsi_next (&bsi))
1693 : : {
1694 : 73488 : struct cgraph_edge *e;
1695 : 73488 : struct cgraph_node *node;
1696 : :
1697 : 73488 : dst_node->remove_stmt_references (gsi_stmt (bsi));
1698 : :
1699 : 73488 : if (gimple_code (gsi_stmt (bsi)) == GIMPLE_CALL
1700 : 73488 : &&(e = dst_node->get_edge (gsi_stmt (bsi))) != NULL)
1701 : : {
1702 : 861 : if (!e->inline_failed)
1703 : 0 : e->callee->remove_symbol_and_inline_clones (dst_node);
1704 : : else
1705 : 861 : cgraph_edge::remove (e);
1706 : : }
1707 : 73488 : 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 : 36714 : delete_basic_block (b);
1734 : 36714 : changed = true;
1735 : : }
1736 : : }
1737 : :
1738 : 1640395 : return changed;
1739 : : }
1740 : :
|