c++: catch parm initialization tweak
[official-gcc.git] / gcc / tree-ssa-sink.cc
blob9213052017a20dd48ce4d31e2950b809d2477ab6
1 /* Code sinking for trees
2 Copyright (C) 2001-2022 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dan@dberlin.org>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "cfghooks.h"
28 #include "tree-pass.h"
29 #include "ssa.h"
30 #include "gimple-pretty-print.h"
31 #include "fold-const.h"
32 #include "stor-layout.h"
33 #include "cfganal.h"
34 #include "gimple-iterator.h"
35 #include "tree-cfg.h"
36 #include "cfgloop.h"
37 #include "tree-eh.h"
39 /* TODO:
40 1. Sinking store only using scalar promotion (IE without moving the RHS):
42 *q = p;
43 p = p + 1;
44 if (something)
45 *q = <not p>;
46 else
47 y = *q;
50 should become
51 sinktemp = p;
52 p = p + 1;
53 if (something)
54 *q = <not p>;
55 else
57 *q = sinktemp;
58 y = *q
60 Store copy propagation will take care of the store elimination above.
63 2. Sinking using Partial Dead Code Elimination. */
66 static struct
68 /* The number of statements sunk down the flowgraph by code sinking. */
69 int sunk;
71 /* The number of stores commoned and sunk down by store commoning. */
72 int commoned;
73 } sink_stats;
76 /* Given a PHI, and one of its arguments (DEF), find the edge for
77 that argument and return it. If the argument occurs twice in the PHI node,
78 we return NULL. */
80 static basic_block
81 find_bb_for_arg (gphi *phi, tree def)
83 size_t i;
84 bool foundone = false;
85 basic_block result = NULL;
86 for (i = 0; i < gimple_phi_num_args (phi); i++)
87 if (PHI_ARG_DEF (phi, i) == def)
89 if (foundone)
90 return NULL;
91 foundone = true;
92 result = gimple_phi_arg_edge (phi, i)->src;
94 return result;
97 /* When the first immediate use is in a statement, then return true if all
98 immediate uses in IMM are in the same statement.
99 We could also do the case where the first immediate use is in a phi node,
100 and all the other uses are in phis in the same basic block, but this
101 requires some expensive checking later (you have to make sure no def/vdef
102 in the statement occurs for multiple edges in the various phi nodes it's
103 used in, so that you only have one place you can sink it to. */
105 static bool
106 all_immediate_uses_same_place (def_operand_p def_p)
108 tree var = DEF_FROM_PTR (def_p);
109 imm_use_iterator imm_iter;
110 use_operand_p use_p;
112 gimple *firstuse = NULL;
113 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, var)
115 if (is_gimple_debug (USE_STMT (use_p)))
116 continue;
117 if (firstuse == NULL)
118 firstuse = USE_STMT (use_p);
119 else
120 if (firstuse != USE_STMT (use_p))
121 return false;
124 return true;
127 /* Find the nearest common dominator of all of the immediate uses in IMM. */
129 static basic_block
130 nearest_common_dominator_of_uses (def_operand_p def_p, bool *debug_stmts)
132 tree var = DEF_FROM_PTR (def_p);
133 auto_bitmap blocks;
134 basic_block commondom;
135 unsigned int j;
136 bitmap_iterator bi;
137 imm_use_iterator imm_iter;
138 use_operand_p use_p;
140 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, var)
142 gimple *usestmt = USE_STMT (use_p);
143 basic_block useblock;
145 if (gphi *phi = dyn_cast <gphi *> (usestmt))
147 int idx = PHI_ARG_INDEX_FROM_USE (use_p);
149 useblock = gimple_phi_arg_edge (phi, idx)->src;
151 else if (is_gimple_debug (usestmt))
153 *debug_stmts = true;
154 continue;
156 else
158 useblock = gimple_bb (usestmt);
161 /* Short circuit. Nothing dominates the entry block. */
162 if (useblock == ENTRY_BLOCK_PTR_FOR_FN (cfun))
163 return NULL;
165 bitmap_set_bit (blocks, useblock->index);
167 commondom = BASIC_BLOCK_FOR_FN (cfun, bitmap_first_set_bit (blocks));
168 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, j, bi)
169 commondom = nearest_common_dominator (CDI_DOMINATORS, commondom,
170 BASIC_BLOCK_FOR_FN (cfun, j));
171 return commondom;
174 /* Given EARLY_BB and LATE_BB, two blocks in a path through the dominator
175 tree, return the best basic block between them (inclusive) to place
176 statements.
178 We want the most control dependent block in the shallowest loop nest.
180 If the resulting block is in a shallower loop nest, then use it. Else
181 only use the resulting block if it has significantly lower execution
182 frequency than EARLY_BB to avoid gratuitous statement movement. We
183 consider statements with VOPS more desirable to move.
185 This pass would obviously benefit from PDO as it utilizes block
186 frequencies. It would also benefit from recomputing frequencies
187 if profile data is not available since frequencies often get out
188 of sync with reality. */
190 static basic_block
191 select_best_block (basic_block early_bb,
192 basic_block late_bb,
193 gimple *stmt)
195 basic_block best_bb = late_bb;
196 basic_block temp_bb = late_bb;
197 int threshold;
199 while (temp_bb != early_bb)
201 /* If we've moved into a lower loop nest, then that becomes
202 our best block. */
203 if (bb_loop_depth (temp_bb) < bb_loop_depth (best_bb))
204 best_bb = temp_bb;
206 /* Walk up the dominator tree, hopefully we'll find a shallower
207 loop nest. */
208 temp_bb = get_immediate_dominator (CDI_DOMINATORS, temp_bb);
211 /* Placing a statement before a setjmp-like function would be invalid
212 (it cannot be reevaluated when execution follows an abnormal edge).
213 If we selected a block with abnormal predecessors, just punt. */
214 if (bb_has_abnormal_pred (best_bb))
215 return early_bb;
217 /* If we found a shallower loop nest, then we always consider that
218 a win. This will always give us the most control dependent block
219 within that loop nest. */
220 if (bb_loop_depth (best_bb) < bb_loop_depth (early_bb))
221 return best_bb;
223 /* Get the sinking threshold. If the statement to be moved has memory
224 operands, then increase the threshold by 7% as those are even more
225 profitable to avoid, clamping at 100%. */
226 threshold = param_sink_frequency_threshold;
227 if (gimple_vuse (stmt) || gimple_vdef (stmt))
229 threshold += 7;
230 if (threshold > 100)
231 threshold = 100;
234 /* If BEST_BB is at the same nesting level, then require it to have
235 significantly lower execution frequency to avoid gratuitous movement. */
236 if (bb_loop_depth (best_bb) == bb_loop_depth (early_bb)
237 /* If result of comparsion is unknown, prefer EARLY_BB.
238 Thus use !(...>=..) rather than (...<...) */
239 && !(best_bb->count * 100 >= early_bb->count * threshold))
240 return best_bb;
242 /* No better block found, so return EARLY_BB, which happens to be the
243 statement's original block. */
244 return early_bb;
247 /* Given a statement (STMT) and the basic block it is currently in (FROMBB),
248 determine the location to sink the statement to, if any.
249 Returns true if there is such location; in that case, TOGSI points to the
250 statement before that STMT should be moved. */
252 static bool
253 statement_sink_location (gimple *stmt, basic_block frombb,
254 gimple_stmt_iterator *togsi, bool *zero_uses_p)
256 gimple *use;
257 use_operand_p one_use = NULL_USE_OPERAND_P;
258 basic_block sinkbb;
259 use_operand_p use_p;
260 def_operand_p def_p;
261 ssa_op_iter iter;
262 imm_use_iterator imm_iter;
264 *zero_uses_p = false;
266 /* We only can sink assignments and non-looping const/pure calls. */
267 int cf;
268 if (!is_gimple_assign (stmt)
269 && (!is_gimple_call (stmt)
270 || !((cf = gimple_call_flags (stmt)) & (ECF_CONST|ECF_PURE))
271 || (cf & ECF_LOOPING_CONST_OR_PURE)))
272 return false;
274 /* We only can sink stmts with a single definition. */
275 def_p = single_ssa_def_operand (stmt, SSA_OP_ALL_DEFS);
276 if (def_p == NULL_DEF_OPERAND_P)
277 return false;
279 /* There are a few classes of things we can't or don't move, some because we
280 don't have code to handle it, some because it's not profitable and some
281 because it's not legal.
283 We can't sink things that may be global stores, at least not without
284 calculating a lot more information, because we may cause it to no longer
285 be seen by an external routine that needs it depending on where it gets
286 moved to.
288 We can't sink statements that end basic blocks without splitting the
289 incoming edge for the sink location to place it there.
291 We can't sink statements that have volatile operands.
293 We don't want to sink dead code, so anything with 0 immediate uses is not
294 sunk.
296 Don't sink BLKmode assignments if current function has any local explicit
297 register variables, as BLKmode assignments may involve memcpy or memset
298 calls or, on some targets, inline expansion thereof that sometimes need
299 to use specific hard registers.
302 if (stmt_ends_bb_p (stmt)
303 || gimple_has_side_effects (stmt)
304 || (cfun->has_local_explicit_reg_vars
305 && TYPE_MODE (TREE_TYPE (gimple_get_lhs (stmt))) == BLKmode))
306 return false;
308 /* Return if there are no immediate uses of this stmt. */
309 if (has_zero_uses (DEF_FROM_PTR (def_p)))
311 *zero_uses_p = true;
312 return false;
315 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (DEF_FROM_PTR (def_p)))
316 return false;
318 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
320 tree use = USE_FROM_PTR (use_p);
321 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use))
322 return false;
325 use = NULL;
327 /* If stmt is a store the one and only use needs to be the VOP
328 merging PHI node. */
329 if (virtual_operand_p (DEF_FROM_PTR (def_p)))
331 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, DEF_FROM_PTR (def_p))
333 gimple *use_stmt = USE_STMT (use_p);
335 /* A killing definition is not a use. */
336 if ((gimple_has_lhs (use_stmt)
337 && operand_equal_p (gimple_get_lhs (stmt),
338 gimple_get_lhs (use_stmt), 0))
339 || stmt_kills_ref_p (use_stmt, gimple_get_lhs (stmt)))
341 /* If use_stmt is or might be a nop assignment then USE_STMT
342 acts as a use as well as definition. */
343 if (stmt != use_stmt
344 && ref_maybe_used_by_stmt_p (use_stmt,
345 gimple_get_lhs (stmt)))
346 return false;
347 continue;
350 if (gimple_code (use_stmt) != GIMPLE_PHI)
351 return false;
353 if (use
354 && use != use_stmt)
355 return false;
357 use = use_stmt;
359 if (!use)
360 return false;
362 /* If all the immediate uses are not in the same place, find the nearest
363 common dominator of all the immediate uses. For PHI nodes, we have to
364 find the nearest common dominator of all of the predecessor blocks, since
365 that is where insertion would have to take place. */
366 else if (gimple_vuse (stmt)
367 || !all_immediate_uses_same_place (def_p))
369 bool debug_stmts = false;
370 basic_block commondom = nearest_common_dominator_of_uses (def_p,
371 &debug_stmts);
373 if (commondom == frombb)
374 return false;
376 /* If this is a load then do not sink past any stores.
377 Look for virtual definitions in the path from frombb to the sink
378 location computed from the real uses and if found, adjust
379 that it a common dominator. */
380 if (gimple_vuse (stmt))
382 /* Do not sink loads from hard registers. */
383 if (gimple_assign_single_p (stmt)
384 && TREE_CODE (gimple_assign_rhs1 (stmt)) == VAR_DECL
385 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt)))
386 return false;
388 imm_use_iterator imm_iter;
389 use_operand_p use_p;
390 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, gimple_vuse (stmt))
392 gimple *use_stmt = USE_STMT (use_p);
393 basic_block bb = gimple_bb (use_stmt);
394 /* For PHI nodes the block we know sth about is the incoming block
395 with the use. */
396 if (gimple_code (use_stmt) == GIMPLE_PHI)
398 /* If the PHI defines the virtual operand, ignore it. */
399 if (gimple_phi_result (use_stmt) == gimple_vuse (stmt))
400 continue;
401 /* In case the PHI node post-dominates the current insert
402 location we can disregard it. But make sure it is not
403 dominating it as well as can happen in a CFG cycle. */
404 if (commondom != bb
405 && !dominated_by_p (CDI_DOMINATORS, commondom, bb)
406 && dominated_by_p (CDI_POST_DOMINATORS, commondom, bb)
407 /* If the blocks are possibly within the same irreducible
408 cycle the above check breaks down. */
409 && !((bb->flags & commondom->flags & BB_IRREDUCIBLE_LOOP)
410 && bb->loop_father == commondom->loop_father)
411 && !((commondom->flags & BB_IRREDUCIBLE_LOOP)
412 && flow_loop_nested_p (commondom->loop_father,
413 bb->loop_father))
414 && !((bb->flags & BB_IRREDUCIBLE_LOOP)
415 && flow_loop_nested_p (bb->loop_father,
416 commondom->loop_father)))
417 continue;
418 bb = EDGE_PRED (bb, PHI_ARG_INDEX_FROM_USE (use_p))->src;
420 else if (!gimple_vdef (use_stmt))
421 continue;
422 /* If the use is not dominated by the path entry it is not on
423 the path. */
424 if (!dominated_by_p (CDI_DOMINATORS, bb, frombb))
425 continue;
426 /* There is no easy way to disregard defs not on the path from
427 frombb to commondom so just consider them all. */
428 commondom = nearest_common_dominator (CDI_DOMINATORS,
429 bb, commondom);
430 if (commondom == frombb)
431 return false;
435 /* Our common dominator has to be dominated by frombb in order to be a
436 trivially safe place to put this statement, since it has multiple
437 uses. */
438 if (!dominated_by_p (CDI_DOMINATORS, commondom, frombb))
439 return false;
441 commondom = select_best_block (frombb, commondom, stmt);
443 if (commondom == frombb)
444 return false;
446 *togsi = gsi_after_labels (commondom);
448 return true;
450 else
452 FOR_EACH_IMM_USE_FAST (one_use, imm_iter, DEF_FROM_PTR (def_p))
454 if (is_gimple_debug (USE_STMT (one_use)))
455 continue;
456 break;
458 use = USE_STMT (one_use);
460 if (gimple_code (use) != GIMPLE_PHI)
462 sinkbb = select_best_block (frombb, gimple_bb (use), stmt);
464 if (sinkbb == frombb)
465 return false;
467 if (sinkbb == gimple_bb (use))
468 *togsi = gsi_for_stmt (use);
469 else
470 *togsi = gsi_after_labels (sinkbb);
472 return true;
476 sinkbb = find_bb_for_arg (as_a <gphi *> (use), DEF_FROM_PTR (def_p));
478 /* This can happen if there are multiple uses in a PHI. */
479 if (!sinkbb)
480 return false;
482 sinkbb = select_best_block (frombb, sinkbb, stmt);
483 if (!sinkbb || sinkbb == frombb)
484 return false;
486 /* If the latch block is empty, don't make it non-empty by sinking
487 something into it. */
488 if (sinkbb == frombb->loop_father->latch
489 && empty_block_p (sinkbb))
490 return false;
492 *togsi = gsi_after_labels (sinkbb);
494 return true;
497 /* Very simplistic code to sink common stores from the predecessor through
498 our virtual PHI. We do this before sinking stmts from BB as it might
499 expose sinking opportunities of the merged stores.
500 Once we have partial dead code elimination through sth like SSU-PRE this
501 should be moved there. */
503 static unsigned
504 sink_common_stores_to_bb (basic_block bb)
506 unsigned todo = 0;
507 gphi *phi;
509 if (EDGE_COUNT (bb->preds) > 1
510 && (phi = get_virtual_phi (bb)))
512 /* Repeat until no more common stores are found. */
513 while (1)
515 gimple *first_store = NULL;
516 auto_vec <tree, 5> vdefs;
517 gimple_stmt_iterator gsi;
519 /* Search for common stores defined by all virtual PHI args.
520 ??? Common stores not present in all predecessors could
521 be handled by inserting a forwarder to sink to. Generally
522 this involves deciding which stores to do this for if
523 multiple common stores are present for different sets of
524 predecessors. See PR11832 for an interesting case. */
525 for (unsigned i = 0; i < gimple_phi_num_args (phi); ++i)
527 tree arg = gimple_phi_arg_def (phi, i);
528 gimple *def = SSA_NAME_DEF_STMT (arg);
529 if (! is_gimple_assign (def)
530 || stmt_can_throw_internal (cfun, def)
531 || (gimple_phi_arg_edge (phi, i)->flags & EDGE_ABNORMAL))
533 /* ??? We could handle some cascading with the def being
534 another PHI. We'd have to insert multiple PHIs for
535 the rhs then though (if they are not all equal). */
536 first_store = NULL;
537 break;
539 /* ??? Do not try to do anything fancy with aliasing, thus
540 do not sink across non-aliased loads (or even stores,
541 so different store order will make the sinking fail). */
542 bool all_uses_on_phi = true;
543 imm_use_iterator iter;
544 use_operand_p use_p;
545 FOR_EACH_IMM_USE_FAST (use_p, iter, arg)
546 if (USE_STMT (use_p) != phi)
548 all_uses_on_phi = false;
549 break;
551 if (! all_uses_on_phi)
553 first_store = NULL;
554 break;
556 /* Check all stores are to the same LHS. */
557 if (! first_store)
558 first_store = def;
559 /* ??? We could handle differing SSA uses in the LHS by inserting
560 PHIs for them. */
561 else if (! operand_equal_p (gimple_assign_lhs (first_store),
562 gimple_assign_lhs (def), 0)
563 || (gimple_clobber_p (first_store)
564 != gimple_clobber_p (def)))
566 first_store = NULL;
567 break;
569 vdefs.safe_push (arg);
571 if (! first_store)
572 break;
574 /* Check if we need a PHI node to merge the stored values. */
575 bool allsame = true;
576 if (!gimple_clobber_p (first_store))
577 for (unsigned i = 1; i < vdefs.length (); ++i)
579 gimple *def = SSA_NAME_DEF_STMT (vdefs[i]);
580 if (! operand_equal_p (gimple_assign_rhs1 (first_store),
581 gimple_assign_rhs1 (def), 0))
583 allsame = false;
584 break;
588 /* We cannot handle aggregate values if we need to merge them. */
589 tree type = TREE_TYPE (gimple_assign_lhs (first_store));
590 if (! allsame
591 && ! is_gimple_reg_type (type))
592 break;
594 if (dump_enabled_p ())
596 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS,
597 first_store,
598 "sinking common stores %sto ",
599 allsame ? "with same value " : "");
600 dump_generic_expr (MSG_OPTIMIZED_LOCATIONS, TDF_SLIM,
601 gimple_assign_lhs (first_store));
602 dump_printf (MSG_OPTIMIZED_LOCATIONS, "\n");
605 /* Insert a PHI to merge differing stored values if necessary.
606 Note that in general inserting PHIs isn't a very good idea as
607 it makes the job of coalescing and register allocation harder.
608 Even common SSA uses on the rhs/lhs might extend their lifetime
609 across multiple edges by this code motion which makes
610 register allocation harder. */
611 tree from;
612 if (! allsame)
614 from = make_ssa_name (type);
615 gphi *newphi = create_phi_node (from, bb);
616 for (unsigned i = 0; i < vdefs.length (); ++i)
618 gimple *def = SSA_NAME_DEF_STMT (vdefs[i]);
619 add_phi_arg (newphi, gimple_assign_rhs1 (def),
620 EDGE_PRED (bb, i), UNKNOWN_LOCATION);
623 else
624 from = gimple_assign_rhs1 (first_store);
626 /* Remove all stores. */
627 for (unsigned i = 0; i < vdefs.length (); ++i)
628 TREE_VISITED (vdefs[i]) = 1;
629 for (unsigned i = 0; i < vdefs.length (); ++i)
630 /* If we have more than one use of a VDEF on the PHI make sure
631 we remove the defining stmt only once. */
632 if (TREE_VISITED (vdefs[i]))
634 TREE_VISITED (vdefs[i]) = 0;
635 gimple *def = SSA_NAME_DEF_STMT (vdefs[i]);
636 gsi = gsi_for_stmt (def);
637 unlink_stmt_vdef (def);
638 gsi_remove (&gsi, true);
639 release_defs (def);
642 /* Insert the first store at the beginning of the merge BB. */
643 gimple_set_vdef (first_store, gimple_phi_result (phi));
644 SSA_NAME_DEF_STMT (gimple_vdef (first_store)) = first_store;
645 gimple_phi_set_result (phi, make_ssa_name (gimple_vop (cfun)));
646 gimple_set_vuse (first_store, gimple_phi_result (phi));
647 gimple_assign_set_rhs1 (first_store, from);
648 /* ??? Should we reset first_stores location? */
649 gsi = gsi_after_labels (bb);
650 gsi_insert_before (&gsi, first_store, GSI_SAME_STMT);
651 sink_stats.commoned++;
653 todo |= TODO_cleanup_cfg;
656 /* We could now have empty predecessors that we could remove,
657 forming a proper CFG for further sinking. Note that even
658 CFG cleanup doesn't do this fully at the moment and it
659 doesn't preserve post-dominators in the process either.
660 The mergephi pass might do it though. gcc.dg/tree-ssa/ssa-sink-13.c
661 shows this nicely if you disable tail merging or (same effect)
662 make the stored values unequal. */
665 return todo;
668 /* Perform code sinking on BB */
670 static unsigned
671 sink_code_in_bb (basic_block bb)
673 basic_block son;
674 gimple_stmt_iterator gsi;
675 edge_iterator ei;
676 edge e;
677 bool last = true;
678 unsigned todo = 0;
680 /* Sink common stores from the predecessor through our virtual PHI. */
681 todo |= sink_common_stores_to_bb (bb);
683 /* If this block doesn't dominate anything, there can't be any place to sink
684 the statements to. */
685 if (first_dom_son (CDI_DOMINATORS, bb) == NULL)
686 goto earlyout;
688 /* We can't move things across abnormal edges, so don't try. */
689 FOR_EACH_EDGE (e, ei, bb->succs)
690 if (e->flags & EDGE_ABNORMAL)
691 goto earlyout;
693 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);)
695 gimple *stmt = gsi_stmt (gsi);
696 gimple_stmt_iterator togsi;
697 bool zero_uses_p;
699 if (!statement_sink_location (stmt, bb, &togsi, &zero_uses_p))
701 gimple_stmt_iterator saved = gsi;
702 if (!gsi_end_p (gsi))
703 gsi_prev (&gsi);
704 /* If we face a dead stmt remove it as it possibly blocks
705 sinking of uses. */
706 if (zero_uses_p
707 && !gimple_vdef (stmt)
708 && (cfun->can_delete_dead_exceptions
709 || !stmt_could_throw_p (cfun, stmt)))
711 gsi_remove (&saved, true);
712 release_defs (stmt);
714 else
715 last = false;
716 continue;
718 if (dump_file)
720 fprintf (dump_file, "Sinking ");
721 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS);
722 fprintf (dump_file, " from bb %d to bb %d\n",
723 bb->index, (gsi_bb (togsi))->index);
726 /* Update virtual operands of statements in the path we
727 do not sink to. */
728 if (gimple_vdef (stmt))
730 imm_use_iterator iter;
731 use_operand_p use_p;
732 gimple *vuse_stmt;
734 FOR_EACH_IMM_USE_STMT (vuse_stmt, iter, gimple_vdef (stmt))
735 if (gimple_code (vuse_stmt) != GIMPLE_PHI)
736 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
737 SET_USE (use_p, gimple_vuse (stmt));
740 /* If this is the end of the basic block, we need to insert at the end
741 of the basic block. */
742 if (gsi_end_p (togsi))
743 gsi_move_to_bb_end (&gsi, gsi_bb (togsi));
744 else
745 gsi_move_before (&gsi, &togsi);
747 sink_stats.sunk++;
749 /* If we've just removed the last statement of the BB, the
750 gsi_end_p() test below would fail, but gsi_prev() would have
751 succeeded, and we want it to succeed. So we keep track of
752 whether we're at the last statement and pick up the new last
753 statement. */
754 if (last)
756 gsi = gsi_last_bb (bb);
757 continue;
760 last = false;
761 if (!gsi_end_p (gsi))
762 gsi_prev (&gsi);
765 earlyout:
766 for (son = first_dom_son (CDI_POST_DOMINATORS, bb);
767 son;
768 son = next_dom_son (CDI_POST_DOMINATORS, son))
770 todo |= sink_code_in_bb (son);
773 return todo;
776 /* Perform code sinking.
777 This moves code down the flowgraph when we know it would be
778 profitable to do so, or it wouldn't increase the number of
779 executions of the statement.
781 IE given
783 a_1 = b + c;
784 if (<something>)
787 else
789 foo (&b, &c);
790 a_5 = b + c;
792 a_6 = PHI (a_5, a_1);
793 USE a_6.
795 we'll transform this into:
797 if (<something>)
799 a_1 = b + c;
801 else
803 foo (&b, &c);
804 a_5 = b + c;
806 a_6 = PHI (a_5, a_1);
807 USE a_6.
809 Note that this reduces the number of computations of a = b + c to 1
810 when we take the else edge, instead of 2.
812 namespace {
814 const pass_data pass_data_sink_code =
816 GIMPLE_PASS, /* type */
817 "sink", /* name */
818 OPTGROUP_NONE, /* optinfo_flags */
819 TV_TREE_SINK, /* tv_id */
820 /* PROP_no_crit_edges is ensured by running split_edges_for_insertion in
821 pass_data_sink_code::execute (). */
822 ( PROP_cfg | PROP_ssa ), /* properties_required */
823 0, /* properties_provided */
824 0, /* properties_destroyed */
825 0, /* todo_flags_start */
826 TODO_update_ssa, /* todo_flags_finish */
829 class pass_sink_code : public gimple_opt_pass
831 public:
832 pass_sink_code (gcc::context *ctxt)
833 : gimple_opt_pass (pass_data_sink_code, ctxt), unsplit_edges (false)
836 /* opt_pass methods: */
837 bool gate (function *) final override { return flag_tree_sink != 0; }
838 unsigned int execute (function *) final override;
839 opt_pass *clone (void) final override { return new pass_sink_code (m_ctxt); }
840 void set_pass_param (unsigned n, bool param) final override
842 gcc_assert (n == 0);
843 unsplit_edges = param;
846 private:
847 bool unsplit_edges;
848 }; // class pass_sink_code
850 unsigned int
851 pass_sink_code::execute (function *fun)
853 loop_optimizer_init (LOOPS_NORMAL);
854 split_edges_for_insertion ();
855 /* Arrange for the critical edge splitting to be undone if requested. */
856 unsigned todo = unsplit_edges ? TODO_cleanup_cfg : 0;
857 connect_infinite_loops_to_exit ();
858 memset (&sink_stats, 0, sizeof (sink_stats));
859 calculate_dominance_info (CDI_DOMINATORS);
860 calculate_dominance_info (CDI_POST_DOMINATORS);
861 todo |= sink_code_in_bb (EXIT_BLOCK_PTR_FOR_FN (fun));
862 statistics_counter_event (fun, "Sunk statements", sink_stats.sunk);
863 statistics_counter_event (fun, "Commoned stores", sink_stats.commoned);
864 free_dominance_info (CDI_POST_DOMINATORS);
865 remove_fake_exit_edges ();
866 loop_optimizer_finalize ();
868 return todo;
871 } // anon namespace
873 gimple_opt_pass *
874 make_pass_sink_code (gcc::context *ctxt)
876 return new pass_sink_code (ctxt);