]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/tree-ssa-sink.cc
c++, mingw: Fix up types of dtor hooks to __cxa_{,thread_}atexit/__cxa_throw on mingw...
[thirdparty/gcc.git] / gcc / tree-ssa-sink.cc
1 /* Code sinking for trees
2 Copyright (C) 2001-2024 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dan@dberlin.org>
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
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"
38 #include "tree-ssa-live.h"
39
40 /* TODO:
41 1. Sinking store only using scalar promotion (IE without moving the RHS):
42
43 *q = p;
44 p = p + 1;
45 if (something)
46 *q = <not p>;
47 else
48 y = *q;
49
50
51 should become
52 sinktemp = p;
53 p = p + 1;
54 if (something)
55 *q = <not p>;
56 else
57 {
58 *q = sinktemp;
59 y = *q
60 }
61 Store copy propagation will take care of the store elimination above.
62
63
64 2. Sinking using Partial Dead Code Elimination. */
65
66
67 static struct
68 {
69 /* The number of statements sunk down the flowgraph by code sinking. */
70 int sunk;
71
72 /* The number of stores commoned and sunk down by store commoning. */
73 int commoned;
74 } sink_stats;
75
76
77 /* Given a PHI, and one of its arguments (DEF), find the edge for
78 that argument and return it. If the argument occurs twice in the PHI node,
79 we return NULL. */
80
81 static basic_block
82 find_bb_for_arg (gphi *phi, tree def)
83 {
84 size_t i;
85 bool foundone = false;
86 basic_block result = NULL;
87 for (i = 0; i < gimple_phi_num_args (phi); i++)
88 if (PHI_ARG_DEF (phi, i) == def)
89 {
90 if (foundone)
91 return NULL;
92 foundone = true;
93 result = gimple_phi_arg_edge (phi, i)->src;
94 }
95 return result;
96 }
97
98 /* When the first immediate use is in a statement, then return true if all
99 immediate uses in IMM are in the same statement.
100 We could also do the case where the first immediate use is in a phi node,
101 and all the other uses are in phis in the same basic block, but this
102 requires some expensive checking later (you have to make sure no def/vdef
103 in the statement occurs for multiple edges in the various phi nodes it's
104 used in, so that you only have one place you can sink it to. */
105
106 static bool
107 all_immediate_uses_same_place (def_operand_p def_p)
108 {
109 tree var = DEF_FROM_PTR (def_p);
110 imm_use_iterator imm_iter;
111 use_operand_p use_p;
112
113 gimple *firstuse = NULL;
114 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, var)
115 {
116 if (is_gimple_debug (USE_STMT (use_p)))
117 continue;
118 if (firstuse == NULL)
119 firstuse = USE_STMT (use_p);
120 else
121 if (firstuse != USE_STMT (use_p))
122 return false;
123 }
124
125 return true;
126 }
127
128 /* Find the nearest common dominator of all of the immediate uses in IMM. */
129
130 static basic_block
131 nearest_common_dominator_of_uses (def_operand_p def_p, bool *debug_stmts)
132 {
133 tree var = DEF_FROM_PTR (def_p);
134 auto_bitmap blocks;
135 basic_block commondom;
136 unsigned int j;
137 bitmap_iterator bi;
138 imm_use_iterator imm_iter;
139 use_operand_p use_p;
140
141 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, var)
142 {
143 gimple *usestmt = USE_STMT (use_p);
144 basic_block useblock;
145
146 if (gphi *phi = dyn_cast <gphi *> (usestmt))
147 {
148 int idx = PHI_ARG_INDEX_FROM_USE (use_p);
149
150 useblock = gimple_phi_arg_edge (phi, idx)->src;
151 }
152 else if (is_gimple_debug (usestmt))
153 {
154 *debug_stmts = true;
155 continue;
156 }
157 else
158 {
159 useblock = gimple_bb (usestmt);
160 }
161
162 /* Short circuit. Nothing dominates the entry block. */
163 if (useblock == ENTRY_BLOCK_PTR_FOR_FN (cfun))
164 return NULL;
165
166 bitmap_set_bit (blocks, useblock->index);
167 }
168 commondom = BASIC_BLOCK_FOR_FN (cfun, bitmap_first_set_bit (blocks));
169 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, j, bi)
170 commondom = nearest_common_dominator (CDI_DOMINATORS, commondom,
171 BASIC_BLOCK_FOR_FN (cfun, j));
172 return commondom;
173 }
174
175 /* Given EARLY_BB and LATE_BB, two blocks in a path through the dominator
176 tree, return the best basic block between them (inclusive) to place
177 statements.
178
179 We want the most control dependent block in the shallowest loop nest.
180
181 If the resulting block is in a shallower loop nest, then use it. Else
182 only use the resulting block if it has significantly lower execution
183 frequency than EARLY_BB to avoid gratuitous statement movement. We
184 consider statements with VOPS more desirable to move.
185
186 This pass would obviously benefit from PDO as it utilizes block
187 frequencies. It would also benefit from recomputing frequencies
188 if profile data is not available since frequencies often get out
189 of sync with reality. */
190
191 static basic_block
192 select_best_block (basic_block early_bb,
193 basic_block late_bb,
194 gimple *stmt)
195 {
196 basic_block best_bb = late_bb;
197 basic_block temp_bb = late_bb;
198 int threshold;
199
200 while (temp_bb != early_bb)
201 {
202 /* If we've moved into a lower loop nest, then that becomes
203 our best block. */
204 if (bb_loop_depth (temp_bb) < bb_loop_depth (best_bb))
205 best_bb = temp_bb;
206
207 /* Walk up the dominator tree, hopefully we'll find a shallower
208 loop nest. */
209 temp_bb = get_immediate_dominator (CDI_DOMINATORS, temp_bb);
210 }
211
212 /* Placing a statement before a setjmp-like function would be invalid
213 (it cannot be reevaluated when execution follows an abnormal edge).
214 If we selected a block with abnormal predecessors, just punt. */
215 if (bb_has_abnormal_pred (best_bb))
216 return early_bb;
217
218 /* If we found a shallower loop nest, then we always consider that
219 a win. This will always give us the most control dependent block
220 within that loop nest. */
221 if (bb_loop_depth (best_bb) < bb_loop_depth (early_bb))
222 return best_bb;
223
224 /* Avoid turning an unconditional read into a conditional one when we
225 still might want to perform vectorization. */
226 if (best_bb->loop_father == early_bb->loop_father
227 && loop_outer (best_bb->loop_father)
228 && !best_bb->loop_father->inner
229 && gimple_vuse (stmt)
230 && flag_tree_loop_vectorize
231 && !(cfun->curr_properties & PROP_loop_opts_done)
232 && dominated_by_p (CDI_DOMINATORS, best_bb->loop_father->latch, early_bb)
233 && !dominated_by_p (CDI_DOMINATORS, best_bb->loop_father->latch, best_bb))
234 return early_bb;
235
236 /* Get the sinking threshold. If the statement to be moved has memory
237 operands, then increase the threshold by 7% as those are even more
238 profitable to avoid, clamping at 100%. */
239 threshold = param_sink_frequency_threshold;
240 if (gimple_vuse (stmt) || gimple_vdef (stmt))
241 {
242 threshold += 7;
243 if (threshold > 100)
244 threshold = 100;
245 }
246
247 /* If BEST_BB is at the same nesting level, then require it to have
248 significantly lower execution frequency to avoid gratuitous movement. */
249 if (bb_loop_depth (best_bb) == bb_loop_depth (early_bb)
250 /* If result of comparsion is unknown, prefer EARLY_BB.
251 Thus use !(...>=..) rather than (...<...) */
252 && !(best_bb->count * 100 >= early_bb->count * threshold))
253 return best_bb;
254
255 /* No better block found, so return EARLY_BB, which happens to be the
256 statement's original block. */
257 return early_bb;
258 }
259
260 /* Given a statement (STMT) and the basic block it is currently in (FROMBB),
261 determine the location to sink the statement to, if any.
262 Returns true if there is such location; in that case, TOGSI points to the
263 statement before that STMT should be moved. */
264
265 static bool
266 statement_sink_location (gimple *stmt, basic_block frombb,
267 gimple_stmt_iterator *togsi, bool *zero_uses_p,
268 virtual_operand_live &vop_live)
269 {
270 gimple *use;
271 use_operand_p one_use = NULL_USE_OPERAND_P;
272 basic_block sinkbb;
273 use_operand_p use_p;
274 def_operand_p def_p;
275 ssa_op_iter iter;
276 imm_use_iterator imm_iter;
277
278 *zero_uses_p = false;
279
280 /* We only can sink assignments and const/pure calls that are guaranteed
281 to return exactly once. */
282 int cf;
283 if (!is_gimple_assign (stmt)
284 && (!is_gimple_call (stmt)
285 || !((cf = gimple_call_flags (stmt)) & (ECF_CONST|ECF_PURE))
286 || (cf & (ECF_LOOPING_CONST_OR_PURE|ECF_RETURNS_TWICE))))
287 return false;
288
289 /* We only can sink stmts with a single definition. */
290 def_p = single_ssa_def_operand (stmt, SSA_OP_ALL_DEFS);
291 if (def_p == NULL_DEF_OPERAND_P)
292 return false;
293
294 /* There are a few classes of things we can't or don't move, some because we
295 don't have code to handle it, some because it's not profitable and some
296 because it's not legal.
297
298 We can't sink things that may be global stores, at least not without
299 calculating a lot more information, because we may cause it to no longer
300 be seen by an external routine that needs it depending on where it gets
301 moved to.
302
303 We can't sink statements that end basic blocks without splitting the
304 incoming edge for the sink location to place it there.
305
306 We can't sink statements that have volatile operands.
307
308 We don't want to sink dead code, so anything with 0 immediate uses is not
309 sunk.
310
311 Don't sink BLKmode assignments if current function has any local explicit
312 register variables, as BLKmode assignments may involve memcpy or memset
313 calls or, on some targets, inline expansion thereof that sometimes need
314 to use specific hard registers.
315
316 */
317 if (stmt_ends_bb_p (stmt)
318 || gimple_has_side_effects (stmt)
319 || (cfun->has_local_explicit_reg_vars
320 && TYPE_MODE (TREE_TYPE (gimple_get_lhs (stmt))) == BLKmode))
321 return false;
322
323 /* Return if there are no immediate uses of this stmt. */
324 if (has_zero_uses (DEF_FROM_PTR (def_p)))
325 {
326 *zero_uses_p = true;
327 return false;
328 }
329
330 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (DEF_FROM_PTR (def_p)))
331 return false;
332
333 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
334 {
335 tree use = USE_FROM_PTR (use_p);
336 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use))
337 return false;
338 }
339
340 use = NULL;
341
342 /* If stmt is a store the one and only use needs to be the VOP
343 merging PHI node. */
344 if (virtual_operand_p (DEF_FROM_PTR (def_p)))
345 {
346 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, DEF_FROM_PTR (def_p))
347 {
348 gimple *use_stmt = USE_STMT (use_p);
349
350 /* A killing definition is not a use. */
351 if ((gimple_has_lhs (use_stmt)
352 && operand_equal_p (gimple_get_lhs (stmt),
353 gimple_get_lhs (use_stmt), 0))
354 || stmt_kills_ref_p (use_stmt, gimple_get_lhs (stmt)))
355 {
356 /* If use_stmt is or might be a nop assignment then USE_STMT
357 acts as a use as well as definition. */
358 if (stmt != use_stmt
359 && ref_maybe_used_by_stmt_p (use_stmt,
360 gimple_get_lhs (stmt)))
361 return false;
362 continue;
363 }
364
365 if (gimple_code (use_stmt) != GIMPLE_PHI)
366 return false;
367
368 if (use
369 && use != use_stmt)
370 return false;
371
372 use = use_stmt;
373 }
374 if (!use)
375 return false;
376 }
377 /* If all the immediate uses are not in the same place, find the nearest
378 common dominator of all the immediate uses. For PHI nodes, we have to
379 find the nearest common dominator of all of the predecessor blocks, since
380 that is where insertion would have to take place. */
381 else if (gimple_vuse (stmt)
382 || !all_immediate_uses_same_place (def_p))
383 {
384 bool debug_stmts = false;
385 basic_block commondom = nearest_common_dominator_of_uses (def_p,
386 &debug_stmts);
387
388 if (commondom == frombb)
389 return false;
390
391 /* If this is a load then do not sink past any stores. */
392 if (gimple_vuse (stmt))
393 {
394 /* Do not sink loads from hard registers. */
395 if (gimple_assign_single_p (stmt)
396 && VAR_P (gimple_assign_rhs1 (stmt))
397 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt)))
398 return false;
399
400 /* When the live virtual operand at the intended sink location is
401 not the same as the one from the load walk up the dominator tree
402 for a new candidate location. */
403 while (commondom != frombb
404 && vop_live.get_live_in (commondom) != gimple_vuse (stmt))
405 commondom = get_immediate_dominator (CDI_DOMINATORS, commondom);
406 if (commondom == frombb)
407 return false;
408 }
409
410 /* Our common dominator has to be dominated by frombb in order to be a
411 trivially safe place to put this statement, since it has multiple
412 uses. */
413 if (!dominated_by_p (CDI_DOMINATORS, commondom, frombb))
414 return false;
415
416 commondom = select_best_block (frombb, commondom, stmt);
417
418 if (commondom == frombb)
419 return false;
420
421 *togsi = gsi_after_labels (commondom);
422
423 return true;
424 }
425 else
426 {
427 FOR_EACH_IMM_USE_FAST (one_use, imm_iter, DEF_FROM_PTR (def_p))
428 {
429 if (is_gimple_debug (USE_STMT (one_use)))
430 continue;
431 break;
432 }
433 use = USE_STMT (one_use);
434
435 if (gimple_code (use) != GIMPLE_PHI)
436 {
437 sinkbb = select_best_block (frombb, gimple_bb (use), stmt);
438
439 if (sinkbb == frombb)
440 return false;
441
442 *togsi = gsi_after_labels (sinkbb);
443
444 return true;
445 }
446 }
447
448 sinkbb = find_bb_for_arg (as_a <gphi *> (use), DEF_FROM_PTR (def_p));
449
450 /* This can happen if there are multiple uses in a PHI. */
451 if (!sinkbb)
452 return false;
453
454 sinkbb = select_best_block (frombb, sinkbb, stmt);
455 if (!sinkbb || sinkbb == frombb)
456 return false;
457
458 /* If the latch block is empty, don't make it non-empty by sinking
459 something into it. */
460 if (sinkbb == frombb->loop_father->latch
461 && empty_block_p (sinkbb))
462 return false;
463
464 *togsi = gsi_after_labels (sinkbb);
465
466 return true;
467 }
468
469 /* Very simplistic code to sink common stores from the predecessor through
470 our virtual PHI. We do this before sinking stmts from BB as it might
471 expose sinking opportunities of the merged stores.
472 Once we have partial dead code elimination through sth like SSU-PRE this
473 should be moved there. */
474
475 static unsigned
476 sink_common_stores_to_bb (basic_block bb)
477 {
478 unsigned todo = 0;
479 gphi *phi;
480
481 if (EDGE_COUNT (bb->preds) > 1
482 && (phi = get_virtual_phi (bb)))
483 {
484 /* Repeat until no more common stores are found. */
485 while (1)
486 {
487 gimple *first_store = NULL;
488 auto_vec <tree, 5> vdefs;
489 gimple_stmt_iterator gsi;
490
491 /* Search for common stores defined by all virtual PHI args.
492 ??? Common stores not present in all predecessors could
493 be handled by inserting a forwarder to sink to. Generally
494 this involves deciding which stores to do this for if
495 multiple common stores are present for different sets of
496 predecessors. See PR11832 for an interesting case. */
497 for (unsigned i = 0; i < gimple_phi_num_args (phi); ++i)
498 {
499 tree arg = gimple_phi_arg_def (phi, i);
500 gimple *def = SSA_NAME_DEF_STMT (arg);
501 if (! is_gimple_assign (def)
502 || stmt_can_throw_internal (cfun, def)
503 || (gimple_phi_arg_edge (phi, i)->flags & EDGE_ABNORMAL))
504 {
505 /* ??? We could handle some cascading with the def being
506 another PHI. We'd have to insert multiple PHIs for
507 the rhs then though (if they are not all equal). */
508 first_store = NULL;
509 break;
510 }
511 /* ??? Do not try to do anything fancy with aliasing, thus
512 do not sink across non-aliased loads (or even stores,
513 so different store order will make the sinking fail). */
514 bool all_uses_on_phi = true;
515 imm_use_iterator iter;
516 use_operand_p use_p;
517 FOR_EACH_IMM_USE_FAST (use_p, iter, arg)
518 if (USE_STMT (use_p) != phi)
519 {
520 all_uses_on_phi = false;
521 break;
522 }
523 if (! all_uses_on_phi)
524 {
525 first_store = NULL;
526 break;
527 }
528 /* Check all stores are to the same LHS. */
529 if (! first_store)
530 first_store = def;
531 /* ??? We could handle differing SSA uses in the LHS by inserting
532 PHIs for them. */
533 else if (! operand_equal_p (gimple_assign_lhs (first_store),
534 gimple_assign_lhs (def), 0)
535 || (gimple_clobber_p (first_store)
536 != gimple_clobber_p (def)))
537 {
538 first_store = NULL;
539 break;
540 }
541 vdefs.safe_push (arg);
542 }
543 if (! first_store)
544 break;
545
546 /* Check if we need a PHI node to merge the stored values. */
547 bool allsame = true;
548 if (!gimple_clobber_p (first_store))
549 for (unsigned i = 1; i < vdefs.length (); ++i)
550 {
551 gimple *def = SSA_NAME_DEF_STMT (vdefs[i]);
552 if (! operand_equal_p (gimple_assign_rhs1 (first_store),
553 gimple_assign_rhs1 (def), 0))
554 {
555 allsame = false;
556 break;
557 }
558 }
559
560 /* We cannot handle aggregate values if we need to merge them. */
561 tree type = TREE_TYPE (gimple_assign_lhs (first_store));
562 if (! allsame
563 && ! is_gimple_reg_type (type))
564 break;
565
566 if (dump_enabled_p ())
567 {
568 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS,
569 first_store,
570 "sinking common stores %sto ",
571 allsame ? "with same value " : "");
572 dump_generic_expr (MSG_OPTIMIZED_LOCATIONS, TDF_SLIM,
573 gimple_assign_lhs (first_store));
574 dump_printf (MSG_OPTIMIZED_LOCATIONS, "\n");
575 }
576
577 /* Insert a PHI to merge differing stored values if necessary.
578 Note that in general inserting PHIs isn't a very good idea as
579 it makes the job of coalescing and register allocation harder.
580 Even common SSA uses on the rhs/lhs might extend their lifetime
581 across multiple edges by this code motion which makes
582 register allocation harder. */
583 tree from;
584 if (! allsame)
585 {
586 from = make_ssa_name (type);
587 gphi *newphi = create_phi_node (from, bb);
588 for (unsigned i = 0; i < vdefs.length (); ++i)
589 {
590 gimple *def = SSA_NAME_DEF_STMT (vdefs[i]);
591 add_phi_arg (newphi, gimple_assign_rhs1 (def),
592 EDGE_PRED (bb, i), UNKNOWN_LOCATION);
593 }
594 }
595 else
596 from = gimple_assign_rhs1 (first_store);
597
598 /* Remove all stores. */
599 for (unsigned i = 0; i < vdefs.length (); ++i)
600 TREE_VISITED (vdefs[i]) = 1;
601 for (unsigned i = 0; i < vdefs.length (); ++i)
602 /* If we have more than one use of a VDEF on the PHI make sure
603 we remove the defining stmt only once. */
604 if (TREE_VISITED (vdefs[i]))
605 {
606 TREE_VISITED (vdefs[i]) = 0;
607 gimple *def = SSA_NAME_DEF_STMT (vdefs[i]);
608 gsi = gsi_for_stmt (def);
609 unlink_stmt_vdef (def);
610 gsi_remove (&gsi, true);
611 release_defs (def);
612 }
613
614 /* Insert the first store at the beginning of the merge BB. */
615 gimple_set_vdef (first_store, gimple_phi_result (phi));
616 SSA_NAME_DEF_STMT (gimple_vdef (first_store)) = first_store;
617 gimple_phi_set_result (phi, make_ssa_name (gimple_vop (cfun)));
618 gimple_set_vuse (first_store, gimple_phi_result (phi));
619 gimple_assign_set_rhs1 (first_store, from);
620 /* ??? Should we reset first_stores location? */
621 gsi = gsi_after_labels (bb);
622 gsi_insert_before (&gsi, first_store, GSI_SAME_STMT);
623 sink_stats.commoned++;
624
625 todo |= TODO_cleanup_cfg;
626 }
627
628 /* We could now have empty predecessors that we could remove,
629 forming a proper CFG for further sinking. Note that even
630 CFG cleanup doesn't do this fully at the moment and it
631 doesn't preserve post-dominators in the process either.
632 The mergephi pass might do it though. gcc.dg/tree-ssa/ssa-sink-13.c
633 shows this nicely if you disable tail merging or (same effect)
634 make the stored values unequal. */
635 }
636
637 return todo;
638 }
639
640 /* Perform code sinking on BB */
641
642 static unsigned
643 sink_code_in_bb (basic_block bb, virtual_operand_live &vop_live)
644 {
645 gimple_stmt_iterator gsi;
646 edge_iterator ei;
647 edge e;
648 bool last = true;
649 unsigned todo = 0;
650
651 /* Sink common stores from the predecessor through our virtual PHI. */
652 todo |= sink_common_stores_to_bb (bb);
653
654 /* If this block doesn't dominate anything, there can't be any place to sink
655 the statements to. */
656 if (first_dom_son (CDI_DOMINATORS, bb) == NULL)
657 return todo;
658
659 /* We can't move things across abnormal edges, so don't try. */
660 FOR_EACH_EDGE (e, ei, bb->succs)
661 if (e->flags & EDGE_ABNORMAL)
662 return todo;
663
664 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);)
665 {
666 gimple *stmt = gsi_stmt (gsi);
667 gimple_stmt_iterator togsi;
668 bool zero_uses_p;
669
670 if (!statement_sink_location (stmt, bb, &togsi, &zero_uses_p, vop_live))
671 {
672 gimple_stmt_iterator saved = gsi;
673 if (!gsi_end_p (gsi))
674 gsi_prev (&gsi);
675 /* If we face a dead stmt remove it as it possibly blocks
676 sinking of uses. */
677 if (zero_uses_p
678 && !gimple_vdef (stmt)
679 && (cfun->can_delete_dead_exceptions
680 || !stmt_could_throw_p (cfun, stmt)))
681 {
682 gsi_remove (&saved, true);
683 release_defs (stmt);
684 }
685 else
686 last = false;
687 continue;
688 }
689 if (dump_file)
690 {
691 fprintf (dump_file, "Sinking ");
692 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS);
693 fprintf (dump_file, " from bb %d to bb %d\n",
694 bb->index, (gsi_bb (togsi))->index);
695 }
696
697 /* Update virtual operands of statements in the path we
698 do not sink to. */
699 if (gimple_vdef (stmt))
700 {
701 imm_use_iterator iter;
702 use_operand_p use_p;
703 gimple *vuse_stmt;
704
705 FOR_EACH_IMM_USE_STMT (vuse_stmt, iter, gimple_vdef (stmt))
706 if (gimple_code (vuse_stmt) != GIMPLE_PHI)
707 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
708 SET_USE (use_p, gimple_vuse (stmt));
709 }
710
711 /* If this is the end of the basic block, we need to insert at the end
712 of the basic block. */
713 if (gsi_end_p (togsi))
714 gsi_move_to_bb_end (&gsi, gsi_bb (togsi));
715 else
716 gsi_move_before (&gsi, &togsi);
717
718 sink_stats.sunk++;
719
720 /* If we've just removed the last statement of the BB, the
721 gsi_end_p() test below would fail, but gsi_prev() would have
722 succeeded, and we want it to succeed. So we keep track of
723 whether we're at the last statement and pick up the new last
724 statement. */
725 if (last)
726 {
727 gsi = gsi_last_bb (bb);
728 continue;
729 }
730
731 last = false;
732 if (!gsi_end_p (gsi))
733 gsi_prev (&gsi);
734
735 }
736
737 return todo;
738 }
739
740 /* Perform code sinking.
741 This moves code down the flowgraph when we know it would be
742 profitable to do so, or it wouldn't increase the number of
743 executions of the statement.
744
745 IE given
746
747 a_1 = b + c;
748 if (<something>)
749 {
750 }
751 else
752 {
753 foo (&b, &c);
754 a_5 = b + c;
755 }
756 a_6 = PHI (a_5, a_1);
757 USE a_6.
758
759 we'll transform this into:
760
761 if (<something>)
762 {
763 a_1 = b + c;
764 }
765 else
766 {
767 foo (&b, &c);
768 a_5 = b + c;
769 }
770 a_6 = PHI (a_5, a_1);
771 USE a_6.
772
773 Note that this reduces the number of computations of a = b + c to 1
774 when we take the else edge, instead of 2.
775 */
776 namespace {
777
778 const pass_data pass_data_sink_code =
779 {
780 GIMPLE_PASS, /* type */
781 "sink", /* name */
782 OPTGROUP_NONE, /* optinfo_flags */
783 TV_TREE_SINK, /* tv_id */
784 /* PROP_no_crit_edges is ensured by running split_edges_for_insertion in
785 pass_data_sink_code::execute (). */
786 ( PROP_cfg | PROP_ssa ), /* properties_required */
787 0, /* properties_provided */
788 0, /* properties_destroyed */
789 0, /* todo_flags_start */
790 TODO_update_ssa, /* todo_flags_finish */
791 };
792
793 class pass_sink_code : public gimple_opt_pass
794 {
795 public:
796 pass_sink_code (gcc::context *ctxt)
797 : gimple_opt_pass (pass_data_sink_code, ctxt), unsplit_edges (false)
798 {}
799
800 /* opt_pass methods: */
801 bool gate (function *) final override { return flag_tree_sink != 0; }
802 unsigned int execute (function *) final override;
803 opt_pass *clone (void) final override { return new pass_sink_code (m_ctxt); }
804 void set_pass_param (unsigned n, bool param) final override
805 {
806 gcc_assert (n == 0);
807 unsplit_edges = param;
808 }
809
810 private:
811 bool unsplit_edges;
812 }; // class pass_sink_code
813
814 unsigned int
815 pass_sink_code::execute (function *fun)
816 {
817 loop_optimizer_init (LOOPS_NORMAL);
818 split_edges_for_insertion ();
819 /* Arrange for the critical edge splitting to be undone if requested. */
820 unsigned todo = unsplit_edges ? TODO_cleanup_cfg : 0;
821 connect_infinite_loops_to_exit ();
822 mark_dfs_back_edges (fun);
823 memset (&sink_stats, 0, sizeof (sink_stats));
824 calculate_dominance_info (CDI_DOMINATORS);
825
826 virtual_operand_live vop_live;
827
828 int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
829 int n = inverted_rev_post_order_compute (fun, rpo);
830 for (int i = 0; i < n; ++i)
831 todo |= sink_code_in_bb (BASIC_BLOCK_FOR_FN (fun, rpo[i]), vop_live);
832 free (rpo);
833
834 statistics_counter_event (fun, "Sunk statements", sink_stats.sunk);
835 statistics_counter_event (fun, "Commoned stores", sink_stats.commoned);
836 remove_fake_exit_edges ();
837 loop_optimizer_finalize ();
838
839 return todo;
840 }
841
842 } // anon namespace
843
844 gimple_opt_pass *
845 make_pass_sink_code (gcc::context *ctxt)
846 {
847 return new pass_sink_code (ctxt);
848 }