Daily bump.
[official-gcc.git] / gcc / tree-ssa-sink.c
blob2d2c370201fdcbc608b2e98bfcb90d4660498534
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)
11 any later version.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "basic-block.h"
28 #include "gimple-pretty-print.h"
29 #include "tree-inline.h"
30 #include "tree-flow.h"
31 #include "gimple.h"
32 #include "hashtab.h"
33 #include "tree-iterator.h"
34 #include "alloc-pool.h"
35 #include "tree-pass.h"
36 #include "flags.h"
37 #include "bitmap.h"
38 #include "cfgloop.h"
39 #include "params.h"
41 /* TODO:
42 1. Sinking store only using scalar promotion (IE without moving the RHS):
44 *q = p;
45 p = p + 1;
46 if (something)
47 *q = <not p>;
48 else
49 y = *q;
52 should become
53 sinktemp = p;
54 p = p + 1;
55 if (something)
56 *q = <not p>;
57 else
59 *q = sinktemp;
60 y = *q
62 Store copy propagation will take care of the store elimination above.
65 2. Sinking using Partial Dead Code Elimination. */
68 static struct
70 /* The number of statements sunk down the flowgraph by code sinking. */
71 int sunk;
73 } sink_stats;
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,
78 we return NULL. */
80 static basic_block
81 find_bb_for_arg (gimple phi, tree def)
83 size_t i;
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)
89 if (foundone)
90 return NULL;
91 foundone = true;
92 result = gimple_phi_arg_edge (phi, i)->src;
94 return result;
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. */
105 static bool
106 all_immediate_uses_same_place (gimple stmt)
108 gimple firstuse = NULL;
109 ssa_op_iter op_iter;
110 imm_use_iterator imm_iter;
111 use_operand_p use_p;
112 tree var;
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)))
119 continue;
120 if (firstuse == NULL)
121 firstuse = USE_STMT (use_p);
122 else
123 if (firstuse != USE_STMT (use_p))
124 return false;
128 return true;
131 /* Find the nearest common dominator of all of the immediate uses in IMM. */
133 static basic_block
134 nearest_common_dominator_of_uses (gimple stmt, bool *debug_stmts)
136 bitmap blocks = BITMAP_ALLOC (NULL);
137 basic_block commondom;
138 unsigned int j;
139 bitmap_iterator bi;
140 ssa_op_iter op_iter;
141 imm_use_iterator imm_iter;
142 use_operand_p use_p;
143 tree var;
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))
161 *debug_stmts = true;
162 continue;
164 else
166 useblock = gimple_bb (usestmt);
169 /* Short circuit. Nothing dominates the entry block. */
170 if (useblock == ENTRY_BLOCK_PTR)
172 BITMAP_FREE (blocks);
173 return NULL;
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,
181 BASIC_BLOCK (j));
182 BITMAP_FREE (blocks);
183 return commondom;
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
188 statements.
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. */
202 static basic_block
203 select_best_block (basic_block early_bb,
204 basic_block late_bb,
205 gimple stmt)
207 basic_block best_bb = late_bb;
208 basic_block temp_bb = late_bb;
209 int threshold;
211 while (temp_bb != early_bb)
213 /* If we've moved into a lower loop nest, then that becomes
214 our best block. */
215 if (bb_loop_depth (temp_bb) < bb_loop_depth (best_bb))
216 best_bb = temp_bb;
218 /* Walk up the dominator tree, hopefully we'll find a shallower
219 loop nest. */
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))
227 return best_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))
235 threshold += 7;
236 if (threshold > 100)
237 threshold = 100;
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))
244 return best_bb;
246 /* No better block found, so return EARLY_BB, which happens to be the
247 statement's original block. */
248 return early_bb;
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. */
256 static bool
257 statement_sink_location (gimple stmt, basic_block frombb,
258 gimple_stmt_iterator *togsi)
260 gimple use;
261 use_operand_p one_use = NULL_USE_OPERAND_P;
262 basic_block sinkbb;
263 use_operand_p use_p;
264 def_operand_p def_p;
265 ssa_op_iter iter;
266 imm_use_iterator imm_iter;
268 /* We only can sink assignments. */
269 if (!is_gimple_assign (stmt))
270 return false;
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)
275 return false;
277 /* Return if there are no immediate uses of this stmt. */
278 if (has_zero_uses (DEF_FROM_PTR (def_p)))
279 return false;
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
288 moved to.
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
298 sunk.
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))
312 return false;
314 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (DEF_FROM_PTR (def_p)))
315 return false;
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))
321 return false;
324 use = NULL;
326 /* If stmt is a store the one and only use needs to be the VOP
327 merging PHI node. */
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))
339 continue;
341 if (gimple_code (use_stmt) != GIMPLE_PHI)
342 return false;
344 if (use
345 && use != use_stmt)
346 return false;
348 use = use_stmt;
350 if (!use)
351 return false;
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,
361 &debug_stmts);
363 if (commondom == frombb)
364 return false;
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
368 uses. */
369 if (!dominated_by_p (CDI_DOMINATORS, commondom, frombb))
370 return false;
372 commondom = select_best_block (frombb, commondom, stmt);
374 if (commondom == frombb)
375 return false;
377 *togsi = gsi_after_labels (commondom);
379 return true;
381 else
383 FOR_EACH_IMM_USE_FAST (one_use, imm_iter, DEF_FROM_PTR (def_p))
385 if (is_gimple_debug (USE_STMT (one_use)))
386 continue;
387 break;
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)
397 return false;
399 *togsi = gsi_for_stmt (use);
401 return true;
405 sinkbb = find_bb_for_arg (use, DEF_FROM_PTR (def_p));
407 /* This can happen if there are multiple uses in a PHI. */
408 if (!sinkbb)
409 return false;
411 sinkbb = select_best_block (frombb, sinkbb, stmt);
412 if (!sinkbb || sinkbb == frombb)
413 return false;
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))
419 return false;
421 *togsi = gsi_after_labels (sinkbb);
423 return true;
426 /* Perform code sinking on BB */
428 static void
429 sink_code_in_bb (basic_block bb)
431 basic_block son;
432 gimple_stmt_iterator gsi;
433 edge_iterator ei;
434 edge e;
435 bool last = true;
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)
440 goto earlyout;
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)
445 goto earlyout;
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))
455 gsi_prev (&gsi);
456 last = false;
457 continue;
459 if (dump_file)
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
468 do not sink to. */
469 if (gimple_vdef (stmt))
471 imm_use_iterator iter;
472 use_operand_p use_p;
473 gimple vuse_stmt;
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));
485 else
486 gsi_move_before (&gsi, &togsi);
488 sink_stats.sunk++;
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
494 statement. */
495 if (last)
497 gsi = gsi_last_bb (bb);
498 continue;
501 last = false;
502 if (!gsi_end_p (gsi))
503 gsi_prev (&gsi);
506 earlyout:
507 for (son = first_dom_son (CDI_POST_DOMINATORS, bb);
508 son;
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.
520 IE given
522 a_1 = b + c;
523 if (<something>)
526 else
528 foo (&b, &c);
529 a_5 = b + c;
531 a_6 = PHI (a_5, a_1);
532 USE a_6.
534 we'll transform this into:
536 if (<something>)
538 a_1 = b + c;
540 else
542 foo (&b, &c);
543 a_5 = b + c;
545 a_6 = PHI (a_5, a_1);
546 USE a_6.
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.
551 static void
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. */
569 static unsigned int
570 do_sink (void)
572 execute_sink_code ();
573 return 0;
576 static bool
577 gate_sink (void)
579 return flag_tree_sink != 0;
582 struct gimple_opt_pass pass_sink_code =
585 GIMPLE_PASS,
586 "sink", /* name */
587 gate_sink, /* gate */
588 do_sink, /* execute */
589 NULL, /* sub */
590 NULL, /* next */
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 */
598 TODO_update_ssa
599 | TODO_verify_ssa
600 | TODO_verify_flow
601 | TODO_ggc_collect /* todo_flags_finish */