1 /* Code sinking for trees
2 Copyright (C) 2001-2018 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)
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/>. */
23 #include "coretypes.h"
28 #include "tree-pass.h"
30 #include "gimple-pretty-print.h"
31 #include "fold-const.h"
32 #include "stor-layout.h"
34 #include "gimple-iterator.h"
40 1. Sinking store only using scalar promotion (IE without moving the RHS):
60 Store copy propagation will take care of the store elimination above.
63 2. Sinking using Partial Dead Code Elimination. */
68 /* The number of statements sunk down the flowgraph by code sinking. */
74 /* Given a PHI, and one of its arguments (DEF), find the edge for
75 that argument and return it. If the argument occurs twice in the PHI node,
79 find_bb_for_arg (gphi
*phi
, tree def
)
82 bool foundone
= false;
83 basic_block result
= NULL
;
84 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
85 if (PHI_ARG_DEF (phi
, i
) == def
)
90 result
= gimple_phi_arg_edge (phi
, i
)->src
;
95 /* When the first immediate use is in a statement, then return true if all
96 immediate uses in IMM are in the same statement.
97 We could also do the case where the first immediate use is in a phi node,
98 and all the other uses are in phis in the same basic block, but this
99 requires some expensive checking later (you have to make sure no def/vdef
100 in the statement occurs for multiple edges in the various phi nodes it's
101 used in, so that you only have one place you can sink it to. */
104 all_immediate_uses_same_place (def_operand_p def_p
)
106 tree var
= DEF_FROM_PTR (def_p
);
107 imm_use_iterator imm_iter
;
110 gimple
*firstuse
= NULL
;
111 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, var
)
113 if (is_gimple_debug (USE_STMT (use_p
)))
115 if (firstuse
== NULL
)
116 firstuse
= USE_STMT (use_p
);
118 if (firstuse
!= USE_STMT (use_p
))
125 /* Find the nearest common dominator of all of the immediate uses in IMM. */
128 nearest_common_dominator_of_uses (def_operand_p def_p
, bool *debug_stmts
)
130 tree var
= DEF_FROM_PTR (def_p
);
132 basic_block commondom
;
135 imm_use_iterator imm_iter
;
138 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, var
)
140 gimple
*usestmt
= USE_STMT (use_p
);
141 basic_block useblock
;
143 if (gphi
*phi
= dyn_cast
<gphi
*> (usestmt
))
145 int idx
= PHI_ARG_INDEX_FROM_USE (use_p
);
147 useblock
= gimple_phi_arg_edge (phi
, idx
)->src
;
149 else if (is_gimple_debug (usestmt
))
156 useblock
= gimple_bb (usestmt
);
159 /* Short circuit. Nothing dominates the entry block. */
160 if (useblock
== ENTRY_BLOCK_PTR_FOR_FN (cfun
))
163 bitmap_set_bit (blocks
, useblock
->index
);
165 commondom
= BASIC_BLOCK_FOR_FN (cfun
, bitmap_first_set_bit (blocks
));
166 EXECUTE_IF_SET_IN_BITMAP (blocks
, 0, j
, bi
)
167 commondom
= nearest_common_dominator (CDI_DOMINATORS
, commondom
,
168 BASIC_BLOCK_FOR_FN (cfun
, j
));
172 /* Given EARLY_BB and LATE_BB, two blocks in a path through the dominator
173 tree, return the best basic block between them (inclusive) to place
176 We want the most control dependent block in the shallowest loop nest.
178 If the resulting block is in a shallower loop nest, then use it. Else
179 only use the resulting block if it has significantly lower execution
180 frequency than EARLY_BB to avoid gratutious statement movement. We
181 consider statements with VOPS more desirable to move.
183 This pass would obviously benefit from PDO as it utilizes block
184 frequencies. It would also benefit from recomputing frequencies
185 if profile data is not available since frequencies often get out
186 of sync with reality. */
189 select_best_block (basic_block early_bb
,
193 basic_block best_bb
= late_bb
;
194 basic_block temp_bb
= late_bb
;
197 while (temp_bb
!= early_bb
)
199 /* If we've moved into a lower loop nest, then that becomes
201 if (bb_loop_depth (temp_bb
) < bb_loop_depth (best_bb
))
204 /* Walk up the dominator tree, hopefully we'll find a shallower
206 temp_bb
= get_immediate_dominator (CDI_DOMINATORS
, temp_bb
);
209 /* If we found a shallower loop nest, then we always consider that
210 a win. This will always give us the most control dependent block
211 within that loop nest. */
212 if (bb_loop_depth (best_bb
) < bb_loop_depth (early_bb
))
215 /* Get the sinking threshold. If the statement to be moved has memory
216 operands, then increase the threshold by 7% as those are even more
217 profitable to avoid, clamping at 100%. */
218 threshold
= PARAM_VALUE (PARAM_SINK_FREQUENCY_THRESHOLD
);
219 if (gimple_vuse (stmt
) || gimple_vdef (stmt
))
226 /* If BEST_BB is at the same nesting level, then require it to have
227 significantly lower execution frequency to avoid gratutious movement. */
228 if (bb_loop_depth (best_bb
) == bb_loop_depth (early_bb
)
229 /* If result of comparsion is unknown, preffer EARLY_BB.
230 Thus use !(...>=..) rather than (...<...) */
231 && !(best_bb
->count
.apply_scale (100, 1)
232 > (early_bb
->count
.apply_scale (threshold
, 1))))
235 /* No better block found, so return EARLY_BB, which happens to be the
236 statement's original block. */
240 /* Given a statement (STMT) and the basic block it is currently in (FROMBB),
241 determine the location to sink the statement to, if any.
242 Returns true if there is such location; in that case, TOGSI points to the
243 statement before that STMT should be moved. */
246 statement_sink_location (gimple
*stmt
, basic_block frombb
,
247 gimple_stmt_iterator
*togsi
, bool *zero_uses_p
)
250 use_operand_p one_use
= NULL_USE_OPERAND_P
;
255 imm_use_iterator imm_iter
;
257 *zero_uses_p
= false;
259 /* We only can sink assignments and non-looping const/pure calls. */
261 if (!is_gimple_assign (stmt
)
262 && (!is_gimple_call (stmt
)
263 || !((cf
= gimple_call_flags (stmt
)) & (ECF_CONST
|ECF_PURE
))
264 || (cf
& ECF_LOOPING_CONST_OR_PURE
)))
267 /* We only can sink stmts with a single definition. */
268 def_p
= single_ssa_def_operand (stmt
, SSA_OP_ALL_DEFS
);
269 if (def_p
== NULL_DEF_OPERAND_P
)
272 /* There are a few classes of things we can't or don't move, some because we
273 don't have code to handle it, some because it's not profitable and some
274 because it's not legal.
276 We can't sink things that may be global stores, at least not without
277 calculating a lot more information, because we may cause it to no longer
278 be seen by an external routine that needs it depending on where it gets
281 We can't sink statements that end basic blocks without splitting the
282 incoming edge for the sink location to place it there.
284 We can't sink statements that have volatile operands.
286 We don't want to sink dead code, so anything with 0 immediate uses is not
289 Don't sink BLKmode assignments if current function has any local explicit
290 register variables, as BLKmode assignments may involve memcpy or memset
291 calls or, on some targets, inline expansion thereof that sometimes need
292 to use specific hard registers.
295 if (stmt_ends_bb_p (stmt
)
296 || gimple_has_side_effects (stmt
)
297 || (cfun
->has_local_explicit_reg_vars
298 && TYPE_MODE (TREE_TYPE (gimple_get_lhs (stmt
))) == BLKmode
))
301 /* Return if there are no immediate uses of this stmt. */
302 if (has_zero_uses (DEF_FROM_PTR (def_p
)))
308 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (DEF_FROM_PTR (def_p
)))
311 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_ALL_USES
)
313 tree use
= USE_FROM_PTR (use_p
);
314 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use
))
320 /* If stmt is a store the one and only use needs to be the VOP
322 if (virtual_operand_p (DEF_FROM_PTR (def_p
)))
324 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, DEF_FROM_PTR (def_p
))
326 gimple
*use_stmt
= USE_STMT (use_p
);
328 /* A killing definition is not a use. */
329 if ((gimple_has_lhs (use_stmt
)
330 && operand_equal_p (gimple_get_lhs (stmt
),
331 gimple_get_lhs (use_stmt
), 0))
332 || stmt_kills_ref_p (use_stmt
, gimple_get_lhs (stmt
)))
334 /* If use_stmt is or might be a nop assignment then USE_STMT
335 acts as a use as well as definition. */
337 && ref_maybe_used_by_stmt_p (use_stmt
,
338 gimple_get_lhs (stmt
)))
343 if (gimple_code (use_stmt
) != GIMPLE_PHI
)
355 /* If all the immediate uses are not in the same place, find the nearest
356 common dominator of all the immediate uses. For PHI nodes, we have to
357 find the nearest common dominator of all of the predecessor blocks, since
358 that is where insertion would have to take place. */
359 else if (gimple_vuse (stmt
)
360 || !all_immediate_uses_same_place (def_p
))
362 bool debug_stmts
= false;
363 basic_block commondom
= nearest_common_dominator_of_uses (def_p
,
366 if (commondom
== frombb
)
369 /* If this is a load then do not sink past any stores.
370 ??? This is overly simple but cheap. We basically look
371 for an existing load with the same VUSE in the path to one
372 of the sink candidate blocks and we adjust commondom to the
373 nearest to commondom. */
374 if (gimple_vuse (stmt
))
376 /* Do not sink loads from hard registers. */
377 if (gimple_assign_single_p (stmt
)
378 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == VAR_DECL
379 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt
)))
382 imm_use_iterator imm_iter
;
384 basic_block found
= NULL
;
385 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, gimple_vuse (stmt
))
387 gimple
*use_stmt
= USE_STMT (use_p
);
388 basic_block bb
= gimple_bb (use_stmt
);
389 /* For PHI nodes the block we know sth about
390 is the incoming block with the use. */
391 if (gimple_code (use_stmt
) == GIMPLE_PHI
)
392 bb
= EDGE_PRED (bb
, PHI_ARG_INDEX_FROM_USE (use_p
))->src
;
393 /* Any dominator of commondom would be ok with
394 adjusting commondom to that block. */
395 bb
= nearest_common_dominator (CDI_DOMINATORS
, bb
, commondom
);
398 else if (dominated_by_p (CDI_DOMINATORS
, bb
, found
))
400 /* If we can't improve, stop. */
401 if (found
== commondom
)
405 if (commondom
== frombb
)
409 /* Our common dominator has to be dominated by frombb in order to be a
410 trivially safe place to put this statement, since it has multiple
412 if (!dominated_by_p (CDI_DOMINATORS
, commondom
, frombb
))
415 commondom
= select_best_block (frombb
, commondom
, stmt
);
417 if (commondom
== frombb
)
420 *togsi
= gsi_after_labels (commondom
);
426 FOR_EACH_IMM_USE_FAST (one_use
, imm_iter
, DEF_FROM_PTR (def_p
))
428 if (is_gimple_debug (USE_STMT (one_use
)))
432 use
= USE_STMT (one_use
);
434 if (gimple_code (use
) != GIMPLE_PHI
)
436 sinkbb
= gimple_bb (use
);
437 sinkbb
= select_best_block (frombb
, gimple_bb (use
), stmt
);
439 if (sinkbb
== frombb
)
442 *togsi
= gsi_for_stmt (use
);
448 sinkbb
= find_bb_for_arg (as_a
<gphi
*> (use
), DEF_FROM_PTR (def_p
));
450 /* This can happen if there are multiple uses in a PHI. */
454 sinkbb
= select_best_block (frombb
, sinkbb
, stmt
);
455 if (!sinkbb
|| sinkbb
== frombb
)
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
))
464 *togsi
= gsi_after_labels (sinkbb
);
469 /* Perform code sinking on BB */
472 sink_code_in_bb (basic_block bb
)
475 gimple_stmt_iterator gsi
;
480 /* If this block doesn't dominate anything, there can't be any place to sink
481 the statements to. */
482 if (first_dom_son (CDI_DOMINATORS
, bb
) == NULL
)
485 /* We can't move things across abnormal edges, so don't try. */
486 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
487 if (e
->flags
& EDGE_ABNORMAL
)
490 for (gsi
= gsi_last_bb (bb
); !gsi_end_p (gsi
);)
492 gimple
*stmt
= gsi_stmt (gsi
);
493 gimple_stmt_iterator togsi
;
496 if (!statement_sink_location (stmt
, bb
, &togsi
, &zero_uses_p
))
498 gimple_stmt_iterator saved
= gsi
;
499 if (!gsi_end_p (gsi
))
501 /* If we face a dead stmt remove it as it possibly blocks
504 && ! gimple_vdef (stmt
))
506 gsi_remove (&saved
, true);
515 fprintf (dump_file
, "Sinking ");
516 print_gimple_stmt (dump_file
, stmt
, 0, TDF_VOPS
);
517 fprintf (dump_file
, " from bb %d to bb %d\n",
518 bb
->index
, (gsi_bb (togsi
))->index
);
521 /* Update virtual operands of statements in the path we
523 if (gimple_vdef (stmt
))
525 imm_use_iterator iter
;
529 FOR_EACH_IMM_USE_STMT (vuse_stmt
, iter
, gimple_vdef (stmt
))
530 if (gimple_code (vuse_stmt
) != GIMPLE_PHI
)
531 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
532 SET_USE (use_p
, gimple_vuse (stmt
));
535 /* If this is the end of the basic block, we need to insert at the end
536 of the basic block. */
537 if (gsi_end_p (togsi
))
538 gsi_move_to_bb_end (&gsi
, gsi_bb (togsi
));
540 gsi_move_before (&gsi
, &togsi
);
544 /* If we've just removed the last statement of the BB, the
545 gsi_end_p() test below would fail, but gsi_prev() would have
546 succeeded, and we want it to succeed. So we keep track of
547 whether we're at the last statement and pick up the new last
551 gsi
= gsi_last_bb (bb
);
556 if (!gsi_end_p (gsi
))
561 for (son
= first_dom_son (CDI_POST_DOMINATORS
, bb
);
563 son
= next_dom_son (CDI_POST_DOMINATORS
, son
))
565 sink_code_in_bb (son
);
569 /* Perform code sinking.
570 This moves code down the flowgraph when we know it would be
571 profitable to do so, or it wouldn't increase the number of
572 executions of the statement.
585 a_6 = PHI (a_5, a_1);
588 we'll transform this into:
599 a_6 = PHI (a_5, a_1);
602 Note that this reduces the number of computations of a = b + c to 1
603 when we take the else edge, instead of 2.
607 const pass_data pass_data_sink_code
=
609 GIMPLE_PASS
, /* type */
611 OPTGROUP_NONE
, /* optinfo_flags */
612 TV_TREE_SINK
, /* tv_id */
613 /* PROP_no_crit_edges is ensured by running split_critical_edges in
614 pass_data_sink_code::execute (). */
615 ( PROP_cfg
| PROP_ssa
), /* properties_required */
616 0, /* properties_provided */
617 0, /* properties_destroyed */
618 0, /* todo_flags_start */
619 TODO_update_ssa
, /* todo_flags_finish */
622 class pass_sink_code
: public gimple_opt_pass
625 pass_sink_code (gcc::context
*ctxt
)
626 : gimple_opt_pass (pass_data_sink_code
, ctxt
)
629 /* opt_pass methods: */
630 virtual bool gate (function
*) { return flag_tree_sink
!= 0; }
631 virtual unsigned int execute (function
*);
633 }; // class pass_sink_code
636 pass_sink_code::execute (function
*fun
)
638 loop_optimizer_init (LOOPS_NORMAL
);
639 split_critical_edges ();
640 connect_infinite_loops_to_exit ();
641 memset (&sink_stats
, 0, sizeof (sink_stats
));
642 calculate_dominance_info (CDI_DOMINATORS
);
643 calculate_dominance_info (CDI_POST_DOMINATORS
);
644 sink_code_in_bb (EXIT_BLOCK_PTR_FOR_FN (fun
));
645 statistics_counter_event (fun
, "Sunk statements", sink_stats
.sunk
);
646 free_dominance_info (CDI_POST_DOMINATORS
);
647 remove_fake_exit_edges ();
648 loop_optimizer_finalize ();
656 make_pass_sink_code (gcc::context
*ctxt
)
658 return new pass_sink_code (ctxt
);