tree-ssa-sink.cc revision 1.1 1 /* Code sinking for trees
2 Copyright (C) 2001-2022 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dan (at) 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-dfa.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 /* If we found a shallower loop nest, then we always consider that
213 a win. This will always give us the most control dependent block
214 within that loop nest. */
215 if (bb_loop_depth (best_bb) < bb_loop_depth (early_bb))
216 return best_bb;
217
218 /* Get the sinking threshold. If the statement to be moved has memory
219 operands, then increase the threshold by 7% as those are even more
220 profitable to avoid, clamping at 100%. */
221 threshold = param_sink_frequency_threshold;
222 if (gimple_vuse (stmt) || gimple_vdef (stmt))
223 {
224 threshold += 7;
225 if (threshold > 100)
226 threshold = 100;
227 }
228
229 /* If BEST_BB is at the same nesting level, then require it to have
230 significantly lower execution frequency to avoid gratuitous movement. */
231 if (bb_loop_depth (best_bb) == bb_loop_depth (early_bb)
232 /* If result of comparsion is unknown, prefer EARLY_BB.
233 Thus use !(...>=..) rather than (...<...) */
234 && !(best_bb->count.apply_scale (100, 1)
235 >= early_bb->count.apply_scale (threshold, 1)))
236 return best_bb;
237
238 /* No better block found, so return EARLY_BB, which happens to be the
239 statement's original block. */
240 return early_bb;
241 }
242
243 /* Given a statement (STMT) and the basic block it is currently in (FROMBB),
244 determine the location to sink the statement to, if any.
245 Returns true if there is such location; in that case, TOGSI points to the
246 statement before that STMT should be moved. */
247
248 static bool
249 statement_sink_location (gimple *stmt, basic_block frombb,
250 gimple_stmt_iterator *togsi, bool *zero_uses_p)
251 {
252 gimple *use;
253 use_operand_p one_use = NULL_USE_OPERAND_P;
254 basic_block sinkbb;
255 use_operand_p use_p;
256 def_operand_p def_p;
257 ssa_op_iter iter;
258 imm_use_iterator imm_iter;
259
260 *zero_uses_p = false;
261
262 /* We only can sink assignments and non-looping const/pure calls. */
263 int cf;
264 if (!is_gimple_assign (stmt)
265 && (!is_gimple_call (stmt)
266 || !((cf = gimple_call_flags (stmt)) & (ECF_CONST|ECF_PURE))
267 || (cf & ECF_LOOPING_CONST_OR_PURE)))
268 return false;
269
270 /* We only can sink stmts with a single definition. */
271 def_p = single_ssa_def_operand (stmt, SSA_OP_ALL_DEFS);
272 if (def_p == NULL_DEF_OPERAND_P)
273 return false;
274
275 /* There are a few classes of things we can't or don't move, some because we
276 don't have code to handle it, some because it's not profitable and some
277 because it's not legal.
278
279 We can't sink things that may be global stores, at least not without
280 calculating a lot more information, because we may cause it to no longer
281 be seen by an external routine that needs it depending on where it gets
282 moved to.
283
284 We can't sink statements that end basic blocks without splitting the
285 incoming edge for the sink location to place it there.
286
287 We can't sink statements that have volatile operands.
288
289 We don't want to sink dead code, so anything with 0 immediate uses is not
290 sunk.
291
292 Don't sink BLKmode assignments if current function has any local explicit
293 register variables, as BLKmode assignments may involve memcpy or memset
294 calls or, on some targets, inline expansion thereof that sometimes need
295 to use specific hard registers.
296
297 */
298 if (stmt_ends_bb_p (stmt)
299 || gimple_has_side_effects (stmt)
300 || (cfun->has_local_explicit_reg_vars
301 && TYPE_MODE (TREE_TYPE (gimple_get_lhs (stmt))) == BLKmode))
302 return false;
303
304 /* Return if there are no immediate uses of this stmt. */
305 if (has_zero_uses (DEF_FROM_PTR (def_p)))
306 {
307 *zero_uses_p = true;
308 return false;
309 }
310
311 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (DEF_FROM_PTR (def_p)))
312 return false;
313
314 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
315 {
316 tree use = USE_FROM_PTR (use_p);
317 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use))
318 return false;
319 }
320
321 use = NULL;
322
323 /* If stmt is a store the one and only use needs to be the VOP
324 merging PHI node. */
325 if (virtual_operand_p (DEF_FROM_PTR (def_p)))
326 {
327 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, DEF_FROM_PTR (def_p))
328 {
329 gimple *use_stmt = USE_STMT (use_p);
330
331 /* A killing definition is not a use. */
332 if ((gimple_has_lhs (use_stmt)
333 && operand_equal_p (gimple_get_lhs (stmt),
334 gimple_get_lhs (use_stmt), 0))
335 || stmt_kills_ref_p (use_stmt, gimple_get_lhs (stmt)))
336 {
337 /* If use_stmt is or might be a nop assignment then USE_STMT
338 acts as a use as well as definition. */
339 if (stmt != use_stmt
340 && ref_maybe_used_by_stmt_p (use_stmt,
341 gimple_get_lhs (stmt)))
342 return false;
343 continue;
344 }
345
346 if (gimple_code (use_stmt) != GIMPLE_PHI)
347 return false;
348
349 if (use
350 && use != use_stmt)
351 return false;
352
353 use = use_stmt;
354 }
355 if (!use)
356 return false;
357 }
358 /* If all the immediate uses are not in the same place, find the nearest
359 common dominator of all the immediate uses. For PHI nodes, we have to
360 find the nearest common dominator of all of the predecessor blocks, since
361 that is where insertion would have to take place. */
362 else if (gimple_vuse (stmt)
363 || !all_immediate_uses_same_place (def_p))
364 {
365 bool debug_stmts = false;
366 basic_block commondom = nearest_common_dominator_of_uses (def_p,
367 &debug_stmts);
368
369 if (commondom == frombb)
370 return false;
371
372 /* If this is a load then do not sink past any stores.
373 Look for virtual definitions in the path from frombb to the sink
374 location computed from the real uses and if found, adjust
375 that it a common dominator. */
376 if (gimple_vuse (stmt))
377 {
378 /* Do not sink loads from hard registers. */
379 if (gimple_assign_single_p (stmt)
380 && TREE_CODE (gimple_assign_rhs1 (stmt)) == VAR_DECL
381 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt)))
382 return false;
383
384 imm_use_iterator imm_iter;
385 use_operand_p use_p;
386 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, gimple_vuse (stmt))
387 {
388 gimple *use_stmt = USE_STMT (use_p);
389 basic_block bb = gimple_bb (use_stmt);
390 /* For PHI nodes the block we know sth about is the incoming block
391 with the use. */
392 if (gimple_code (use_stmt) == GIMPLE_PHI)
393 {
394 /* If the PHI defines the virtual operand, ignore it. */
395 if (gimple_phi_result (use_stmt) == gimple_vuse (stmt))
396 continue;
397 /* In case the PHI node post-dominates the current insert
398 location we can disregard it. But make sure it is not
399 dominating it as well as can happen in a CFG cycle. */
400 if (commondom != bb
401 && !dominated_by_p (CDI_DOMINATORS, commondom, bb)
402 && dominated_by_p (CDI_POST_DOMINATORS, commondom, bb)
403 /* If the blocks are possibly within the same irreducible
404 cycle the above check breaks down. */
405 && !((bb->flags & commondom->flags & BB_IRREDUCIBLE_LOOP)
406 && bb->loop_father == commondom->loop_father)
407 && !((commondom->flags & BB_IRREDUCIBLE_LOOP)
408 && flow_loop_nested_p (commondom->loop_father,
409 bb->loop_father))
410 && !((bb->flags & BB_IRREDUCIBLE_LOOP)
411 && flow_loop_nested_p (bb->loop_father,
412 commondom->loop_father)))
413 continue;
414 bb = EDGE_PRED (bb, PHI_ARG_INDEX_FROM_USE (use_p))->src;
415 }
416 else if (!gimple_vdef (use_stmt))
417 continue;
418 /* If the use is not dominated by the path entry it is not on
419 the path. */
420 if (!dominated_by_p (CDI_DOMINATORS, bb, frombb))
421 continue;
422 /* There is no easy way to disregard defs not on the path from
423 frombb to commondom so just consider them all. */
424 commondom = nearest_common_dominator (CDI_DOMINATORS,
425 bb, commondom);
426 if (commondom == frombb)
427 return false;
428 }
429 }
430
431 /* Our common dominator has to be dominated by frombb in order to be a
432 trivially safe place to put this statement, since it has multiple
433 uses. */
434 if (!dominated_by_p (CDI_DOMINATORS, commondom, frombb))
435 return false;
436
437 commondom = select_best_block (frombb, commondom, stmt);
438
439 if (commondom == frombb)
440 return false;
441
442 *togsi = gsi_after_labels (commondom);
443
444 return true;
445 }
446 else
447 {
448 FOR_EACH_IMM_USE_FAST (one_use, imm_iter, DEF_FROM_PTR (def_p))
449 {
450 if (is_gimple_debug (USE_STMT (one_use)))
451 continue;
452 break;
453 }
454 use = USE_STMT (one_use);
455
456 if (gimple_code (use) != GIMPLE_PHI)
457 {
458 sinkbb = select_best_block (frombb, gimple_bb (use), stmt);
459
460 if (sinkbb == frombb)
461 return false;
462
463 if (sinkbb == gimple_bb (use))
464 *togsi = gsi_for_stmt (use);
465 else
466 *togsi = gsi_after_labels (sinkbb);
467
468 return true;
469 }
470 }
471
472 sinkbb = find_bb_for_arg (as_a <gphi *> (use), DEF_FROM_PTR (def_p));
473
474 /* This can happen if there are multiple uses in a PHI. */
475 if (!sinkbb)
476 return false;
477
478 sinkbb = select_best_block (frombb, sinkbb, stmt);
479 if (!sinkbb || sinkbb == frombb)
480 return false;
481
482 /* If the latch block is empty, don't make it non-empty by sinking
483 something into it. */
484 if (sinkbb == frombb->loop_father->latch
485 && empty_block_p (sinkbb))
486 return false;
487
488 *togsi = gsi_after_labels (sinkbb);
489
490 return true;
491 }
492
493 /* Very simplistic code to sink common stores from the predecessor through
494 our virtual PHI. We do this before sinking stmts from BB as it might
495 expose sinking opportunities of the merged stores.
496 Once we have partial dead code elimination through sth like SSU-PRE this
497 should be moved there. */
498
499 static unsigned
500 sink_common_stores_to_bb (basic_block bb)
501 {
502 unsigned todo = 0;
503 gphi *phi;
504
505 if (EDGE_COUNT (bb->preds) > 1
506 && (phi = get_virtual_phi (bb)))
507 {
508 /* Repeat until no more common stores are found. */
509 while (1)
510 {
511 gimple *first_store = NULL;
512 auto_vec <tree, 5> vdefs;
513 gimple_stmt_iterator gsi;
514
515 /* Search for common stores defined by all virtual PHI args.
516 ??? Common stores not present in all predecessors could
517 be handled by inserting a forwarder to sink to. Generally
518 this involves deciding which stores to do this for if
519 multiple common stores are present for different sets of
520 predecessors. See PR11832 for an interesting case. */
521 for (unsigned i = 0; i < gimple_phi_num_args (phi); ++i)
522 {
523 tree arg = gimple_phi_arg_def (phi, i);
524 gimple *def = SSA_NAME_DEF_STMT (arg);
525 if (! is_gimple_assign (def)
526 || stmt_can_throw_internal (cfun, def)
527 || (gimple_phi_arg_edge (phi, i)->flags & EDGE_ABNORMAL)
528 || stmt_references_abnormal_ssa_name (def))
529 {
530 /* ??? We could handle some cascading with the def being
531 another PHI. We'd have to insert multiple PHIs for
532 the rhs then though (if they are not all equal). */
533 first_store = NULL;
534 break;
535 }
536 /* ??? Do not try to do anything fancy with aliasing, thus
537 do not sink across non-aliased loads (or even stores,
538 so different store order will make the sinking fail). */
539 bool all_uses_on_phi = true;
540 imm_use_iterator iter;
541 use_operand_p use_p;
542 FOR_EACH_IMM_USE_FAST (use_p, iter, arg)
543 if (USE_STMT (use_p) != phi)
544 {
545 all_uses_on_phi = false;
546 break;
547 }
548 if (! all_uses_on_phi)
549 {
550 first_store = NULL;
551 break;
552 }
553 /* Check all stores are to the same LHS. */
554 if (! first_store)
555 first_store = def;
556 /* ??? We could handle differing SSA uses in the LHS by inserting
557 PHIs for them. */
558 else if (! operand_equal_p (gimple_assign_lhs (first_store),
559 gimple_assign_lhs (def), 0)
560 || (gimple_clobber_p (first_store)
561 != gimple_clobber_p (def)))
562 {
563 first_store = NULL;
564 break;
565 }
566 vdefs.safe_push (arg);
567 }
568 if (! first_store)
569 break;
570
571 /* Check if we need a PHI node to merge the stored values. */
572 bool allsame = true;
573 if (!gimple_clobber_p (first_store))
574 for (unsigned i = 1; i < vdefs.length (); ++i)
575 {
576 gimple *def = SSA_NAME_DEF_STMT (vdefs[i]);
577 if (! operand_equal_p (gimple_assign_rhs1 (first_store),
578 gimple_assign_rhs1 (def), 0))
579 {
580 allsame = false;
581 break;
582 }
583 }
584
585 /* We cannot handle aggregate values if we need to merge them. */
586 tree type = TREE_TYPE (gimple_assign_lhs (first_store));
587 if (! allsame
588 && ! is_gimple_reg_type (type))
589 break;
590
591 if (dump_enabled_p ())
592 {
593 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS,
594 first_store,
595 "sinking common stores %sto ",
596 allsame ? "with same value " : "");
597 dump_generic_expr (MSG_OPTIMIZED_LOCATIONS, TDF_SLIM,
598 gimple_assign_lhs (first_store));
599 dump_printf (MSG_OPTIMIZED_LOCATIONS, "\n");
600 }
601
602 /* Insert a PHI to merge differing stored values if necessary.
603 Note that in general inserting PHIs isn't a very good idea as
604 it makes the job of coalescing and register allocation harder.
605 Even common SSA uses on the rhs/lhs might extend their lifetime
606 across multiple edges by this code motion which makes
607 register allocation harder. */
608 tree from;
609 if (! allsame)
610 {
611 from = make_ssa_name (type);
612 gphi *newphi = create_phi_node (from, bb);
613 for (unsigned i = 0; i < vdefs.length (); ++i)
614 {
615 gimple *def = SSA_NAME_DEF_STMT (vdefs[i]);
616 add_phi_arg (newphi, gimple_assign_rhs1 (def),
617 EDGE_PRED (bb, i), UNKNOWN_LOCATION);
618 }
619 }
620 else
621 from = gimple_assign_rhs1 (first_store);
622
623 /* Remove all stores. */
624 for (unsigned i = 0; i < vdefs.length (); ++i)
625 TREE_VISITED (vdefs[i]) = 1;
626 for (unsigned i = 0; i < vdefs.length (); ++i)
627 /* If we have more than one use of a VDEF on the PHI make sure
628 we remove the defining stmt only once. */
629 if (TREE_VISITED (vdefs[i]))
630 {
631 TREE_VISITED (vdefs[i]) = 0;
632 gimple *def = SSA_NAME_DEF_STMT (vdefs[i]);
633 gsi = gsi_for_stmt (def);
634 unlink_stmt_vdef (def);
635 gsi_remove (&gsi, true);
636 release_defs (def);
637 }
638
639 /* Insert the first store at the beginning of the merge BB. */
640 gimple_set_vdef (first_store, gimple_phi_result (phi));
641 SSA_NAME_DEF_STMT (gimple_vdef (first_store)) = first_store;
642 gimple_phi_set_result (phi, make_ssa_name (gimple_vop (cfun)));
643 gimple_set_vuse (first_store, gimple_phi_result (phi));
644 gimple_assign_set_rhs1 (first_store, from);
645 /* ??? Should we reset first_stores location? */
646 gsi = gsi_after_labels (bb);
647 gsi_insert_before (&gsi, first_store, GSI_SAME_STMT);
648 sink_stats.commoned++;
649
650 todo |= TODO_cleanup_cfg;
651 }
652
653 /* We could now have empty predecessors that we could remove,
654 forming a proper CFG for further sinking. Note that even
655 CFG cleanup doesn't do this fully at the moment and it
656 doesn't preserve post-dominators in the process either.
657 The mergephi pass might do it though. gcc.dg/tree-ssa/ssa-sink-13.c
658 shows this nicely if you disable tail merging or (same effect)
659 make the stored values unequal. */
660 }
661
662 return todo;
663 }
664
665 /* Perform code sinking on BB */
666
667 static unsigned
668 sink_code_in_bb (basic_block bb)
669 {
670 basic_block son;
671 gimple_stmt_iterator gsi;
672 edge_iterator ei;
673 edge e;
674 bool last = true;
675 unsigned todo = 0;
676
677 /* Sink common stores from the predecessor through our virtual PHI. */
678 todo |= sink_common_stores_to_bb (bb);
679
680 /* If this block doesn't dominate anything, there can't be any place to sink
681 the statements to. */
682 if (first_dom_son (CDI_DOMINATORS, bb) == NULL)
683 goto earlyout;
684
685 /* We can't move things across abnormal edges, so don't try. */
686 FOR_EACH_EDGE (e, ei, bb->succs)
687 if (e->flags & EDGE_ABNORMAL)
688 goto earlyout;
689
690 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);)
691 {
692 gimple *stmt = gsi_stmt (gsi);
693 gimple_stmt_iterator togsi;
694 bool zero_uses_p;
695
696 if (!statement_sink_location (stmt, bb, &togsi, &zero_uses_p))
697 {
698 gimple_stmt_iterator saved = gsi;
699 if (!gsi_end_p (gsi))
700 gsi_prev (&gsi);
701 /* If we face a dead stmt remove it as it possibly blocks
702 sinking of uses. */
703 if (zero_uses_p
704 && !gimple_vdef (stmt)
705 && (cfun->can_delete_dead_exceptions
706 || !stmt_could_throw_p (cfun, stmt)))
707 {
708 gsi_remove (&saved, true);
709 release_defs (stmt);
710 }
711 else
712 last = false;
713 continue;
714 }
715 if (dump_file)
716 {
717 fprintf (dump_file, "Sinking ");
718 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS);
719 fprintf (dump_file, " from bb %d to bb %d\n",
720 bb->index, (gsi_bb (togsi))->index);
721 }
722
723 /* Update virtual operands of statements in the path we
724 do not sink to. */
725 if (gimple_vdef (stmt))
726 {
727 imm_use_iterator iter;
728 use_operand_p use_p;
729 gimple *vuse_stmt;
730
731 FOR_EACH_IMM_USE_STMT (vuse_stmt, iter, gimple_vdef (stmt))
732 if (gimple_code (vuse_stmt) != GIMPLE_PHI)
733 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
734 SET_USE (use_p, gimple_vuse (stmt));
735 }
736
737 /* If this is the end of the basic block, we need to insert at the end
738 of the basic block. */
739 if (gsi_end_p (togsi))
740 gsi_move_to_bb_end (&gsi, gsi_bb (togsi));
741 else
742 gsi_move_before (&gsi, &togsi);
743
744 sink_stats.sunk++;
745
746 /* If we've just removed the last statement of the BB, the
747 gsi_end_p() test below would fail, but gsi_prev() would have
748 succeeded, and we want it to succeed. So we keep track of
749 whether we're at the last statement and pick up the new last
750 statement. */
751 if (last)
752 {
753 gsi = gsi_last_bb (bb);
754 continue;
755 }
756
757 last = false;
758 if (!gsi_end_p (gsi))
759 gsi_prev (&gsi);
760
761 }
762 earlyout:
763 for (son = first_dom_son (CDI_POST_DOMINATORS, bb);
764 son;
765 son = next_dom_son (CDI_POST_DOMINATORS, son))
766 {
767 todo |= sink_code_in_bb (son);
768 }
769
770 return todo;
771 }
772
773 /* Perform code sinking.
774 This moves code down the flowgraph when we know it would be
775 profitable to do so, or it wouldn't increase the number of
776 executions of the statement.
777
778 IE given
779
780 a_1 = b + c;
781 if (<something>)
782 {
783 }
784 else
785 {
786 foo (&b, &c);
787 a_5 = b + c;
788 }
789 a_6 = PHI (a_5, a_1);
790 USE a_6.
791
792 we'll transform this into:
793
794 if (<something>)
795 {
796 a_1 = b + c;
797 }
798 else
799 {
800 foo (&b, &c);
801 a_5 = b + c;
802 }
803 a_6 = PHI (a_5, a_1);
804 USE a_6.
805
806 Note that this reduces the number of computations of a = b + c to 1
807 when we take the else edge, instead of 2.
808 */
809 namespace {
810
811 const pass_data pass_data_sink_code =
812 {
813 GIMPLE_PASS, /* type */
814 "sink", /* name */
815 OPTGROUP_NONE, /* optinfo_flags */
816 TV_TREE_SINK, /* tv_id */
817 /* PROP_no_crit_edges is ensured by running split_edges_for_insertion in
818 pass_data_sink_code::execute (). */
819 ( PROP_cfg | PROP_ssa ), /* properties_required */
820 0, /* properties_provided */
821 0, /* properties_destroyed */
822 0, /* todo_flags_start */
823 TODO_update_ssa, /* todo_flags_finish */
824 };
825
826 class pass_sink_code : public gimple_opt_pass
827 {
828 public:
829 pass_sink_code (gcc::context *ctxt)
830 : gimple_opt_pass (pass_data_sink_code, ctxt), unsplit_edges (false)
831 {}
832
833 /* opt_pass methods: */
834 virtual bool gate (function *) { return flag_tree_sink != 0; }
835 virtual unsigned int execute (function *);
836 opt_pass *clone (void) { return new pass_sink_code (m_ctxt); }
837 void set_pass_param (unsigned n, bool param)
838 {
839 gcc_assert (n == 0);
840 unsplit_edges = param;
841 }
842
843 private:
844 bool unsplit_edges;
845 }; // class pass_sink_code
846
847 unsigned int
848 pass_sink_code::execute (function *fun)
849 {
850 loop_optimizer_init (LOOPS_NORMAL);
851 split_edges_for_insertion ();
852 /* Arrange for the critical edge splitting to be undone if requested. */
853 unsigned todo = unsplit_edges ? TODO_cleanup_cfg : 0;
854 connect_infinite_loops_to_exit ();
855 memset (&sink_stats, 0, sizeof (sink_stats));
856 calculate_dominance_info (CDI_DOMINATORS);
857 calculate_dominance_info (CDI_POST_DOMINATORS);
858 todo |= sink_code_in_bb (EXIT_BLOCK_PTR_FOR_FN (fun));
859 statistics_counter_event (fun, "Sunk statements", sink_stats.sunk);
860 statistics_counter_event (fun, "Commoned stores", sink_stats.commoned);
861 free_dominance_info (CDI_POST_DOMINATORS);
862 remove_fake_exit_edges ();
863 loop_optimizer_finalize ();
864
865 return todo;
866 }
867
868 } // anon namespace
869
870 gimple_opt_pass *
871 make_pass_sink_code (gcc::context *ctxt)
872 {
873 return new pass_sink_code (ctxt);
874 }
875