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 "diagnostic.h"
29 #include "gimple-pretty-print.h"
30 #include "tree-inline.h"
31 #include "tree-flow.h"
33 #include "tree-dump.h"
37 #include "tree-iterator.h"
38 #include "alloc-pool.h"
39 #include "tree-pass.h"
42 #include "langhooks.h"
46 1. Sinking store only using scalar promotion (IE without moving the RHS):
66 Store copy propagation will take care of the store elimination above.
69 2. Sinking using Partial Dead Code Elimination. */
74 /* The number of statements sunk down the flowgraph by code sinking. */
80 /* Given a PHI, and one of its arguments (DEF), find the edge for
81 that argument and return it. If the argument occurs twice in the PHI node,
85 find_bb_for_arg (gimple phi
, tree def
)
88 bool foundone
= false;
89 basic_block result
= NULL
;
90 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
91 if (PHI_ARG_DEF (phi
, i
) == def
)
96 result
= gimple_phi_arg_edge (phi
, i
)->src
;
101 /* When the first immediate use is in a statement, then return true if all
102 immediate uses in IMM are in the same statement.
103 We could also do the case where the first immediate use is in a phi node,
104 and all the other uses are in phis in the same basic block, but this
105 requires some expensive checking later (you have to make sure no def/vdef
106 in the statement occurs for multiple edges in the various phi nodes it's
107 used in, so that you only have one place you can sink it to. */
110 all_immediate_uses_same_place (gimple stmt
)
112 gimple firstuse
= NULL
;
114 imm_use_iterator imm_iter
;
118 FOR_EACH_SSA_TREE_OPERAND (var
, stmt
, op_iter
, SSA_OP_ALL_DEFS
)
120 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, var
)
122 if (is_gimple_debug (USE_STMT (use_p
)))
124 if (firstuse
== NULL
)
125 firstuse
= USE_STMT (use_p
);
127 if (firstuse
!= USE_STMT (use_p
))
135 /* Some global stores don't necessarily have VDEF's of global variables,
136 but we still must avoid moving them around. */
139 is_hidden_global_store (gimple stmt
)
141 /* Check virtual definitions. If we get here, the only virtual
142 definitions we should see are those generated by assignment or call
144 if (gimple_vdef (stmt
))
148 gcc_assert (is_gimple_assign (stmt
) || is_gimple_call (stmt
));
150 /* Note that we must not check the individual virtual operands
151 here. In particular, if this is an aliased store, we could
152 end up with something like the following (SSA notation
153 redacted for brevity):
158 p_1 = (i_2 > 3) ? &x : p;
166 Notice that the store to '*p_1' should be preserved, if we
167 were to check the virtual definitions in that store, we would
168 not mark it needed. This is because 'x' is not a global
171 Therefore, we check the base address of the LHS. If the
172 address is a pointer, we check if its name tag or symbol tag is
173 a global variable. Otherwise, we check if the base variable
175 lhs
= gimple_get_lhs (stmt
);
177 if (REFERENCE_CLASS_P (lhs
))
178 lhs
= get_base_address (lhs
);
180 if (lhs
== NULL_TREE
)
182 /* If LHS is NULL, it means that we couldn't get the base
183 address of the reference. In which case, we should not
187 else if (DECL_P (lhs
))
189 /* If the store is to a global symbol, we need to keep it. */
190 if (is_global_var (lhs
))
194 else if (INDIRECT_REF_P (lhs
))
195 return ptr_deref_may_alias_global_p (TREE_OPERAND (lhs
, 0));
203 /* Find the nearest common dominator of all of the immediate uses in IMM. */
206 nearest_common_dominator_of_uses (gimple stmt
, bool *debug_stmts
)
208 bitmap blocks
= BITMAP_ALLOC (NULL
);
209 basic_block commondom
;
213 imm_use_iterator imm_iter
;
217 bitmap_clear (blocks
);
218 FOR_EACH_SSA_TREE_OPERAND (var
, stmt
, op_iter
, SSA_OP_ALL_DEFS
)
220 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, var
)
222 gimple usestmt
= USE_STMT (use_p
);
223 basic_block useblock
;
225 if (gimple_code (usestmt
) == GIMPLE_PHI
)
227 int idx
= PHI_ARG_INDEX_FROM_USE (use_p
);
229 useblock
= gimple_phi_arg_edge (usestmt
, idx
)->src
;
231 else if (is_gimple_debug (usestmt
))
238 useblock
= gimple_bb (usestmt
);
241 /* Short circuit. Nothing dominates the entry block. */
242 if (useblock
== ENTRY_BLOCK_PTR
)
244 BITMAP_FREE (blocks
);
247 bitmap_set_bit (blocks
, useblock
->index
);
250 commondom
= BASIC_BLOCK (bitmap_first_set_bit (blocks
));
251 EXECUTE_IF_SET_IN_BITMAP (blocks
, 0, j
, bi
)
252 commondom
= nearest_common_dominator (CDI_DOMINATORS
, commondom
,
254 BITMAP_FREE (blocks
);
258 /* Given a statement (STMT) and the basic block it is currently in (FROMBB),
259 determine the location to sink the statement to, if any.
260 Returns true if there is such location; in that case, TOGSI points to the
261 statement before that STMT should be moved. */
264 statement_sink_location (gimple stmt
, basic_block frombb
,
265 gimple_stmt_iterator
*togsi
)
269 use_operand_p one_use
= NULL_USE_OPERAND_P
;
274 imm_use_iterator imm_iter
;
276 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_ALL_DEFS
)
278 FOR_EACH_IMM_USE_FAST (one_use
, imm_iter
, def
)
280 if (is_gimple_debug (USE_STMT (one_use
)))
285 if (one_use
!= NULL_USE_OPERAND_P
)
289 /* Return if there are no immediate uses of this stmt. */
290 if (one_use
== NULL_USE_OPERAND_P
)
293 if (gimple_code (stmt
) != GIMPLE_ASSIGN
)
296 /* There are a few classes of things we can't or don't move, some because we
297 don't have code to handle it, some because it's not profitable and some
298 because it's not legal.
300 We can't sink things that may be global stores, at least not without
301 calculating a lot more information, because we may cause it to no longer
302 be seen by an external routine that needs it depending on where it gets
305 We don't want to sink loads from memory.
307 We can't sink statements that end basic blocks without splitting the
308 incoming edge for the sink location to place it there.
310 We can't sink statements that have volatile operands.
312 We don't want to sink dead code, so anything with 0 immediate uses is not
315 Don't sink BLKmode assignments if current function has any local explicit
316 register variables, as BLKmode assignments may involve memcpy or memset
317 calls or, on some targets, inline expansion thereof that sometimes need
318 to use specific hard registers.
321 if (stmt_ends_bb_p (stmt
)
322 || gimple_has_side_effects (stmt
)
323 || is_hidden_global_store (stmt
)
324 || gimple_has_volatile_ops (stmt
)
325 || gimple_vuse (stmt
)
326 || (cfun
->has_local_explicit_reg_vars
327 && TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
))) == BLKmode
))
330 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, iter
, SSA_OP_ALL_DEFS
)
332 tree def
= DEF_FROM_PTR (def_p
);
333 if (is_global_var (SSA_NAME_VAR (def
))
334 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def
))
338 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_ALL_USES
)
340 tree use
= USE_FROM_PTR (use_p
);
341 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use
))
345 /* If all the immediate uses are not in the same place, find the nearest
346 common dominator of all the immediate uses. For PHI nodes, we have to
347 find the nearest common dominator of all of the predecessor blocks, since
348 that is where insertion would have to take place. */
349 if (!all_immediate_uses_same_place (stmt
))
351 bool debug_stmts
= false;
352 basic_block commondom
= nearest_common_dominator_of_uses (stmt
,
355 if (commondom
== frombb
)
358 /* Our common dominator has to be dominated by frombb in order to be a
359 trivially safe place to put this statement, since it has multiple
361 if (!dominated_by_p (CDI_DOMINATORS
, commondom
, frombb
))
364 /* It doesn't make sense to move to a dominator that post-dominates
365 frombb, because it means we've just moved it into a path that always
366 executes if frombb executes, instead of reducing the number of
368 if (dominated_by_p (CDI_POST_DOMINATORS
, frombb
, commondom
))
370 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
371 fprintf (dump_file
, "Not moving store, common dominator post-dominates from block.\n");
375 if (commondom
== frombb
|| commondom
->loop_depth
> frombb
->loop_depth
)
377 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
379 fprintf (dump_file
, "Common dominator of all uses is %d\n",
383 *togsi
= gsi_after_labels (commondom
);
388 use
= USE_STMT (one_use
);
389 if (gimple_code (use
) != GIMPLE_PHI
)
391 sinkbb
= gimple_bb (use
);
392 if (sinkbb
== frombb
|| sinkbb
->loop_depth
> frombb
->loop_depth
393 || sinkbb
->loop_father
!= frombb
->loop_father
)
396 /* Move the expression to a post dominator can't reduce the number of
398 if (dominated_by_p (CDI_POST_DOMINATORS
, frombb
, sinkbb
))
401 *togsi
= gsi_for_stmt (use
);
406 /* Note that at this point, all uses must be in the same statement, so it
407 doesn't matter which def op we choose, pick the first one. */
408 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_ALL_DEFS
)
411 sinkbb
= find_bb_for_arg (use
, def
);
415 /* This will happen when you have
416 a_3 = PHI <a_13, a_26>
420 If the use is a phi, and is in the same bb as the def,
423 if (gimple_bb (use
) == frombb
)
425 if (sinkbb
== frombb
|| sinkbb
->loop_depth
> frombb
->loop_depth
426 || sinkbb
->loop_father
!= frombb
->loop_father
)
429 /* Move the expression to a post dominator can't reduce the number of
431 if (dominated_by_p (CDI_POST_DOMINATORS
, frombb
, sinkbb
))
434 *togsi
= gsi_after_labels (sinkbb
);
439 /* Perform code sinking on BB */
442 sink_code_in_bb (basic_block bb
)
445 gimple_stmt_iterator gsi
;
450 /* If this block doesn't dominate anything, there can't be any place to sink
451 the statements to. */
452 if (first_dom_son (CDI_DOMINATORS
, bb
) == NULL
)
455 /* We can't move things across abnormal edges, so don't try. */
456 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
457 if (e
->flags
& EDGE_ABNORMAL
)
460 for (gsi
= gsi_last_bb (bb
); !gsi_end_p (gsi
);)
462 gimple stmt
= gsi_stmt (gsi
);
463 gimple_stmt_iterator togsi
;
465 if (!statement_sink_location (stmt
, bb
, &togsi
))
467 if (!gsi_end_p (gsi
))
474 fprintf (dump_file
, "Sinking ");
475 print_gimple_stmt (dump_file
, stmt
, 0, TDF_VOPS
);
476 fprintf (dump_file
, " from bb %d to bb %d\n",
477 bb
->index
, (gsi_bb (togsi
))->index
);
480 /* If this is the end of the basic block, we need to insert at the end
481 of the basic block. */
482 if (gsi_end_p (togsi
))
483 gsi_move_to_bb_end (&gsi
, gsi_bb (togsi
));
485 gsi_move_before (&gsi
, &togsi
);
489 /* If we've just removed the last statement of the BB, the
490 gsi_end_p() test below would fail, but gsi_prev() would have
491 succeeded, and we want it to succeed. So we keep track of
492 whether we're at the last statement and pick up the new last
496 gsi
= gsi_last_bb (bb
);
501 if (!gsi_end_p (gsi
))
506 for (son
= first_dom_son (CDI_POST_DOMINATORS
, bb
);
508 son
= next_dom_son (CDI_POST_DOMINATORS
, son
))
510 sink_code_in_bb (son
);
514 /* Perform code sinking.
515 This moves code down the flowgraph when we know it would be
516 profitable to do so, or it wouldn't increase the number of
517 executions of the statement.
530 a_6 = PHI (a_5, a_1);
533 we'll transform this into:
544 a_6 = PHI (a_5, a_1);
547 Note that this reduces the number of computations of a = b + c to 1
548 when we take the else edge, instead of 2.
551 execute_sink_code (void)
553 loop_optimizer_init (LOOPS_NORMAL
);
555 connect_infinite_loops_to_exit ();
556 memset (&sink_stats
, 0, sizeof (sink_stats
));
557 calculate_dominance_info (CDI_DOMINATORS
);
558 calculate_dominance_info (CDI_POST_DOMINATORS
);
559 sink_code_in_bb (EXIT_BLOCK_PTR
);
560 statistics_counter_event (cfun
, "Sunk statements", sink_stats
.sunk
);
561 free_dominance_info (CDI_POST_DOMINATORS
);
562 remove_fake_exit_edges ();
563 loop_optimizer_finalize ();
566 /* Gate and execute functions for PRE. */
571 execute_sink_code ();
578 return flag_tree_sink
!= 0;
581 struct gimple_opt_pass pass_sink_code
=
586 gate_sink
, /* gate */
587 do_sink
, /* execute */
590 0, /* static_pass_number */
591 TV_TREE_SINK
, /* tv_id */
592 PROP_no_crit_edges
| PROP_cfg
593 | PROP_ssa
, /* properties_required */
594 0, /* properties_provided */
595 0, /* properties_destroyed */
596 0, /* todo_flags_start */
600 | TODO_verify_ssa
/* todo_flags_finish */