1 /* Code sinking for trees
2 Copyright (C) 2001, 2002, 2003, 2004, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Daniel Berlin <dan@dberlin.org>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
27 #include "basic-block.h"
28 #include "gimple-pretty-print.h"
29 #include "tree-inline.h"
30 #include "tree-flow.h"
33 #include "tree-iterator.h"
34 #include "alloc-pool.h"
35 #include "tree-pass.h"
42 1. Sinking store only using scalar promotion (IE without moving the RHS):
62 Store copy propagation will take care of the store elimination above.
65 2. Sinking using Partial Dead Code Elimination. */
70 /* The number of statements sunk down the flowgraph by code sinking. */
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,
81 find_bb_for_arg (gimple phi
, tree def
)
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
)
92 result
= gimple_phi_arg_edge (phi
, i
)->src
;
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. */
106 all_immediate_uses_same_place (gimple stmt
)
108 gimple firstuse
= NULL
;
110 imm_use_iterator imm_iter
;
114 FOR_EACH_SSA_TREE_OPERAND (var
, stmt
, op_iter
, SSA_OP_ALL_DEFS
)
116 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, var
)
118 if (is_gimple_debug (USE_STMT (use_p
)))
120 if (firstuse
== NULL
)
121 firstuse
= USE_STMT (use_p
);
123 if (firstuse
!= USE_STMT (use_p
))
131 /* Find the nearest common dominator of all of the immediate uses in IMM. */
134 nearest_common_dominator_of_uses (gimple stmt
, bool *debug_stmts
)
136 bitmap blocks
= BITMAP_ALLOC (NULL
);
137 basic_block commondom
;
141 imm_use_iterator imm_iter
;
145 bitmap_clear (blocks
);
146 FOR_EACH_SSA_TREE_OPERAND (var
, stmt
, op_iter
, SSA_OP_ALL_DEFS
)
148 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, var
)
150 gimple usestmt
= USE_STMT (use_p
);
151 basic_block useblock
;
153 if (gimple_code (usestmt
) == GIMPLE_PHI
)
155 int idx
= PHI_ARG_INDEX_FROM_USE (use_p
);
157 useblock
= gimple_phi_arg_edge (usestmt
, idx
)->src
;
159 else if (is_gimple_debug (usestmt
))
166 useblock
= gimple_bb (usestmt
);
169 /* Short circuit. Nothing dominates the entry block. */
170 if (useblock
== ENTRY_BLOCK_PTR
)
172 BITMAP_FREE (blocks
);
175 bitmap_set_bit (blocks
, useblock
->index
);
178 commondom
= BASIC_BLOCK (bitmap_first_set_bit (blocks
));
179 EXECUTE_IF_SET_IN_BITMAP (blocks
, 0, j
, bi
)
180 commondom
= nearest_common_dominator (CDI_DOMINATORS
, commondom
,
182 BITMAP_FREE (blocks
);
186 /* Given EARLY_BB and LATE_BB, two blocks in a path through the dominator
187 tree, return the best basic block between them (inclusive) to place
190 We want the most control dependent block in the shallowest loop nest.
192 If the resulting block is in a shallower loop nest, then use it. Else
193 only use the resulting block if it has significantly lower execution
194 frequency than EARLY_BB to avoid gratutious statement movement. We
195 consider statements with VOPS more desirable to move.
197 This pass would obviously benefit from PDO as it utilizes block
198 frequencies. It would also benefit from recomputing frequencies
199 if profile data is not available since frequencies often get out
200 of sync with reality. */
203 select_best_block (basic_block early_bb
,
207 basic_block best_bb
= late_bb
;
208 basic_block temp_bb
= late_bb
;
211 while (temp_bb
!= early_bb
)
213 /* If we've moved into a lower loop nest, then that becomes
215 if (bb_loop_depth (temp_bb
) < bb_loop_depth (best_bb
))
218 /* Walk up the dominator tree, hopefully we'll find a shallower
220 temp_bb
= get_immediate_dominator (CDI_DOMINATORS
, temp_bb
);
223 /* If we found a shallower loop nest, then we always consider that
224 a win. This will always give us the most control dependent block
225 within that loop nest. */
226 if (bb_loop_depth (best_bb
) < bb_loop_depth (early_bb
))
229 /* Get the sinking threshold. If the statement to be moved has memory
230 operands, then increase the threshold by 7% as those are even more
231 profitable to avoid, clamping at 100%. */
232 threshold
= PARAM_VALUE (PARAM_SINK_FREQUENCY_THRESHOLD
);
233 if (gimple_vuse (stmt
) || gimple_vdef (stmt
))
240 /* If BEST_BB is at the same nesting level, then require it to have
241 significantly lower execution frequency to avoid gratutious movement. */
242 if (bb_loop_depth (best_bb
) == bb_loop_depth (early_bb
)
243 && best_bb
->frequency
< (early_bb
->frequency
* threshold
/ 100.0))
246 /* No better block found, so return EARLY_BB, which happens to be the
247 statement's original block. */
251 /* Given a statement (STMT) and the basic block it is currently in (FROMBB),
252 determine the location to sink the statement to, if any.
253 Returns true if there is such location; in that case, TOGSI points to the
254 statement before that STMT should be moved. */
257 statement_sink_location (gimple stmt
, basic_block frombb
,
258 gimple_stmt_iterator
*togsi
)
261 use_operand_p one_use
= NULL_USE_OPERAND_P
;
266 imm_use_iterator imm_iter
;
268 /* We only can sink assignments. */
269 if (!is_gimple_assign (stmt
))
272 /* We only can sink stmts with a single definition. */
273 def_p
= single_ssa_def_operand (stmt
, SSA_OP_ALL_DEFS
);
274 if (def_p
== NULL_DEF_OPERAND_P
)
277 /* Return if there are no immediate uses of this stmt. */
278 if (has_zero_uses (DEF_FROM_PTR (def_p
)))
281 /* There are a few classes of things we can't or don't move, some because we
282 don't have code to handle it, some because it's not profitable and some
283 because it's not legal.
285 We can't sink things that may be global stores, at least not without
286 calculating a lot more information, because we may cause it to no longer
287 be seen by an external routine that needs it depending on where it gets
290 We don't want to sink loads from memory.
292 We can't sink statements that end basic blocks without splitting the
293 incoming edge for the sink location to place it there.
295 We can't sink statements that have volatile operands.
297 We don't want to sink dead code, so anything with 0 immediate uses is not
300 Don't sink BLKmode assignments if current function has any local explicit
301 register variables, as BLKmode assignments may involve memcpy or memset
302 calls or, on some targets, inline expansion thereof that sometimes need
303 to use specific hard registers.
306 if (stmt_ends_bb_p (stmt
)
307 || gimple_has_side_effects (stmt
)
308 || gimple_has_volatile_ops (stmt
)
309 || (gimple_vuse (stmt
) && !gimple_vdef (stmt
))
310 || (cfun
->has_local_explicit_reg_vars
311 && TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
))) == BLKmode
))
314 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (DEF_FROM_PTR (def_p
)))
317 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_ALL_USES
)
319 tree use
= USE_FROM_PTR (use_p
);
320 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use
))
326 /* If stmt is a store the one and only use needs to be the VOP
328 if (gimple_vdef (stmt
))
330 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, DEF_FROM_PTR (def_p
))
332 gimple use_stmt
= USE_STMT (use_p
);
334 /* A killing definition is not a use. */
335 if (gimple_assign_single_p (use_stmt
)
336 && gimple_vdef (use_stmt
)
337 && operand_equal_p (gimple_assign_lhs (stmt
),
338 gimple_assign_lhs (use_stmt
), 0))
341 if (gimple_code (use_stmt
) != GIMPLE_PHI
)
353 /* If all the immediate uses are not in the same place, find the nearest
354 common dominator of all the immediate uses. For PHI nodes, we have to
355 find the nearest common dominator of all of the predecessor blocks, since
356 that is where insertion would have to take place. */
357 else if (!all_immediate_uses_same_place (stmt
))
359 bool debug_stmts
= false;
360 basic_block commondom
= nearest_common_dominator_of_uses (stmt
,
363 if (commondom
== frombb
)
366 /* Our common dominator has to be dominated by frombb in order to be a
367 trivially safe place to put this statement, since it has multiple
369 if (!dominated_by_p (CDI_DOMINATORS
, commondom
, frombb
))
372 commondom
= select_best_block (frombb
, commondom
, stmt
);
374 if (commondom
== frombb
)
377 *togsi
= gsi_after_labels (commondom
);
383 FOR_EACH_IMM_USE_FAST (one_use
, imm_iter
, DEF_FROM_PTR (def_p
))
385 if (is_gimple_debug (USE_STMT (one_use
)))
389 use
= USE_STMT (one_use
);
391 if (gimple_code (use
) != GIMPLE_PHI
)
393 sinkbb
= gimple_bb (use
);
394 sinkbb
= select_best_block (frombb
, gimple_bb (use
), stmt
);
396 if (sinkbb
== frombb
)
399 *togsi
= gsi_for_stmt (use
);
405 sinkbb
= find_bb_for_arg (use
, DEF_FROM_PTR (def_p
));
407 /* This can happen if there are multiple uses in a PHI. */
411 sinkbb
= select_best_block (frombb
, sinkbb
, stmt
);
412 if (!sinkbb
|| sinkbb
== frombb
)
415 /* If the latch block is empty, don't make it non-empty by sinking
416 something into it. */
417 if (sinkbb
== frombb
->loop_father
->latch
418 && empty_block_p (sinkbb
))
421 *togsi
= gsi_after_labels (sinkbb
);
426 /* Perform code sinking on BB */
429 sink_code_in_bb (basic_block bb
)
432 gimple_stmt_iterator gsi
;
437 /* If this block doesn't dominate anything, there can't be any place to sink
438 the statements to. */
439 if (first_dom_son (CDI_DOMINATORS
, bb
) == NULL
)
442 /* We can't move things across abnormal edges, so don't try. */
443 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
444 if (e
->flags
& EDGE_ABNORMAL
)
447 for (gsi
= gsi_last_bb (bb
); !gsi_end_p (gsi
);)
449 gimple stmt
= gsi_stmt (gsi
);
450 gimple_stmt_iterator togsi
;
452 if (!statement_sink_location (stmt
, bb
, &togsi
))
454 if (!gsi_end_p (gsi
))
461 fprintf (dump_file
, "Sinking ");
462 print_gimple_stmt (dump_file
, stmt
, 0, TDF_VOPS
);
463 fprintf (dump_file
, " from bb %d to bb %d\n",
464 bb
->index
, (gsi_bb (togsi
))->index
);
467 /* Update virtual operands of statements in the path we
469 if (gimple_vdef (stmt
))
471 imm_use_iterator iter
;
475 FOR_EACH_IMM_USE_STMT (vuse_stmt
, iter
, gimple_vdef (stmt
))
476 if (gimple_code (vuse_stmt
) != GIMPLE_PHI
)
477 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
478 SET_USE (use_p
, gimple_vuse (stmt
));
481 /* If this is the end of the basic block, we need to insert at the end
482 of the basic block. */
483 if (gsi_end_p (togsi
))
484 gsi_move_to_bb_end (&gsi
, gsi_bb (togsi
));
486 gsi_move_before (&gsi
, &togsi
);
490 /* If we've just removed the last statement of the BB, the
491 gsi_end_p() test below would fail, but gsi_prev() would have
492 succeeded, and we want it to succeed. So we keep track of
493 whether we're at the last statement and pick up the new last
497 gsi
= gsi_last_bb (bb
);
502 if (!gsi_end_p (gsi
))
507 for (son
= first_dom_son (CDI_POST_DOMINATORS
, bb
);
509 son
= next_dom_son (CDI_POST_DOMINATORS
, son
))
511 sink_code_in_bb (son
);
515 /* Perform code sinking.
516 This moves code down the flowgraph when we know it would be
517 profitable to do so, or it wouldn't increase the number of
518 executions of the statement.
531 a_6 = PHI (a_5, a_1);
534 we'll transform this into:
545 a_6 = PHI (a_5, a_1);
548 Note that this reduces the number of computations of a = b + c to 1
549 when we take the else edge, instead of 2.
552 execute_sink_code (void)
554 loop_optimizer_init (LOOPS_NORMAL
);
556 connect_infinite_loops_to_exit ();
557 memset (&sink_stats
, 0, sizeof (sink_stats
));
558 calculate_dominance_info (CDI_DOMINATORS
);
559 calculate_dominance_info (CDI_POST_DOMINATORS
);
560 sink_code_in_bb (EXIT_BLOCK_PTR
);
561 statistics_counter_event (cfun
, "Sunk statements", sink_stats
.sunk
);
562 free_dominance_info (CDI_POST_DOMINATORS
);
563 remove_fake_exit_edges ();
564 loop_optimizer_finalize ();
567 /* Gate and execute functions for PRE. */
572 execute_sink_code ();
579 return flag_tree_sink
!= 0;
582 struct gimple_opt_pass pass_sink_code
=
587 gate_sink
, /* gate */
588 do_sink
, /* execute */
591 0, /* static_pass_number */
592 TV_TREE_SINK
, /* tv_id */
593 PROP_no_crit_edges
| PROP_cfg
594 | PROP_ssa
, /* properties_required */
595 0, /* properties_provided */
596 0, /* properties_destroyed */
597 0, /* todo_flags_start */
601 | TODO_ggc_collect
/* todo_flags_finish */