[obvious] Fix typos above expand_cond_expr_using_cmove
[official-gcc.git] / gcc / tree-ssa-sink.c
blob8108a163b58a0cf222f602481d90d3bfa2853d49
1 /* Code sinking for trees
2 Copyright (C) 2001-2015 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)
10 any later version.
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/>. */
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 "hard-reg-set.h"
28 #include "ssa.h"
29 #include "alias.h"
30 #include "fold-const.h"
31 #include "stor-layout.h"
32 #include "cfganal.h"
33 #include "gimple-pretty-print.h"
34 #include "tree-inline.h"
35 #include "internal-fn.h"
36 #include "gimple-iterator.h"
37 #include "tree-cfg.h"
38 #include "tree-iterator.h"
39 #include "alloc-pool.h"
40 #include "tree-pass.h"
41 #include "flags.h"
42 #include "cfgloop.h"
43 #include "params.h"
45 /* TODO:
46 1. Sinking store only using scalar promotion (IE without moving the RHS):
48 *q = p;
49 p = p + 1;
50 if (something)
51 *q = <not p>;
52 else
53 y = *q;
56 should become
57 sinktemp = p;
58 p = p + 1;
59 if (something)
60 *q = <not p>;
61 else
63 *q = sinktemp;
64 y = *q
66 Store copy propagation will take care of the store elimination above.
69 2. Sinking using Partial Dead Code Elimination. */
72 static struct
74 /* The number of statements sunk down the flowgraph by code sinking. */
75 int sunk;
77 } sink_stats;
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,
82 we return NULL. */
84 static basic_block
85 find_bb_for_arg (gphi *phi, tree def)
87 size_t i;
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)
93 if (foundone)
94 return NULL;
95 foundone = true;
96 result = gimple_phi_arg_edge (phi, i)->src;
98 return result;
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. */
109 static bool
110 all_immediate_uses_same_place (def_operand_p def_p)
112 tree var = DEF_FROM_PTR (def_p);
113 imm_use_iterator imm_iter;
114 use_operand_p use_p;
116 gimple firstuse = NULL;
117 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, var)
119 if (is_gimple_debug (USE_STMT (use_p)))
120 continue;
121 if (firstuse == NULL)
122 firstuse = USE_STMT (use_p);
123 else
124 if (firstuse != USE_STMT (use_p))
125 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 (def_operand_p def_p, bool *debug_stmts)
136 tree var = DEF_FROM_PTR (def_p);
137 bitmap blocks = BITMAP_ALLOC (NULL);
138 basic_block commondom;
139 unsigned int j;
140 bitmap_iterator bi;
141 imm_use_iterator imm_iter;
142 use_operand_p use_p;
144 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, var)
146 gimple usestmt = USE_STMT (use_p);
147 basic_block useblock;
149 if (gphi *phi = dyn_cast <gphi *> (usestmt))
151 int idx = PHI_ARG_INDEX_FROM_USE (use_p);
153 useblock = gimple_phi_arg_edge (phi, idx)->src;
155 else if (is_gimple_debug (usestmt))
157 *debug_stmts = true;
158 continue;
160 else
162 useblock = gimple_bb (usestmt);
165 /* Short circuit. Nothing dominates the entry block. */
166 if (useblock == ENTRY_BLOCK_PTR_FOR_FN (cfun))
168 BITMAP_FREE (blocks);
169 return NULL;
171 bitmap_set_bit (blocks, useblock->index);
173 commondom = BASIC_BLOCK_FOR_FN (cfun, bitmap_first_set_bit (blocks));
174 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, j, bi)
175 commondom = nearest_common_dominator (CDI_DOMINATORS, commondom,
176 BASIC_BLOCK_FOR_FN (cfun, j));
177 BITMAP_FREE (blocks);
178 return commondom;
181 /* Given EARLY_BB and LATE_BB, two blocks in a path through the dominator
182 tree, return the best basic block between them (inclusive) to place
183 statements.
185 We want the most control dependent block in the shallowest loop nest.
187 If the resulting block is in a shallower loop nest, then use it. Else
188 only use the resulting block if it has significantly lower execution
189 frequency than EARLY_BB to avoid gratutious statement movement. We
190 consider statements with VOPS more desirable to move.
192 This pass would obviously benefit from PDO as it utilizes block
193 frequencies. It would also benefit from recomputing frequencies
194 if profile data is not available since frequencies often get out
195 of sync with reality. */
197 static basic_block
198 select_best_block (basic_block early_bb,
199 basic_block late_bb,
200 gimple stmt)
202 basic_block best_bb = late_bb;
203 basic_block temp_bb = late_bb;
204 int threshold;
206 while (temp_bb != early_bb)
208 /* If we've moved into a lower loop nest, then that becomes
209 our best block. */
210 if (bb_loop_depth (temp_bb) < bb_loop_depth (best_bb))
211 best_bb = temp_bb;
213 /* Walk up the dominator tree, hopefully we'll find a shallower
214 loop nest. */
215 temp_bb = get_immediate_dominator (CDI_DOMINATORS, temp_bb);
218 /* If we found a shallower loop nest, then we always consider that
219 a win. This will always give us the most control dependent block
220 within that loop nest. */
221 if (bb_loop_depth (best_bb) < bb_loop_depth (early_bb))
222 return best_bb;
224 /* Get the sinking threshold. If the statement to be moved has memory
225 operands, then increase the threshold by 7% as those are even more
226 profitable to avoid, clamping at 100%. */
227 threshold = PARAM_VALUE (PARAM_SINK_FREQUENCY_THRESHOLD);
228 if (gimple_vuse (stmt) || gimple_vdef (stmt))
230 threshold += 7;
231 if (threshold > 100)
232 threshold = 100;
235 /* If BEST_BB is at the same nesting level, then require it to have
236 significantly lower execution frequency to avoid gratutious movement. */
237 if (bb_loop_depth (best_bb) == bb_loop_depth (early_bb)
238 && best_bb->frequency < (early_bb->frequency * threshold / 100.0))
239 return best_bb;
241 /* No better block found, so return EARLY_BB, which happens to be the
242 statement's original block. */
243 return early_bb;
246 /* Given a statement (STMT) and the basic block it is currently in (FROMBB),
247 determine the location to sink the statement to, if any.
248 Returns true if there is such location; in that case, TOGSI points to the
249 statement before that STMT should be moved. */
251 static bool
252 statement_sink_location (gimple stmt, basic_block frombb,
253 gimple_stmt_iterator *togsi)
255 gimple use;
256 use_operand_p one_use = NULL_USE_OPERAND_P;
257 basic_block sinkbb;
258 use_operand_p use_p;
259 def_operand_p def_p;
260 ssa_op_iter iter;
261 imm_use_iterator imm_iter;
263 /* We only can sink assignments. */
264 if (!is_gimple_assign (stmt))
265 return false;
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)
270 return false;
272 /* Return if there are no immediate uses of this stmt. */
273 if (has_zero_uses (DEF_FROM_PTR (def_p)))
274 return false;
276 /* There are a few classes of things we can't or don't move, some because we
277 don't have code to handle it, some because it's not profitable and some
278 because it's not legal.
280 We can't sink things that may be global stores, at least not without
281 calculating a lot more information, because we may cause it to no longer
282 be seen by an external routine that needs it depending on where it gets
283 moved to.
285 We can't sink statements that end basic blocks without splitting the
286 incoming edge for the sink location to place it there.
288 We can't sink statements that have volatile operands.
290 We don't want to sink dead code, so anything with 0 immediate uses is not
291 sunk.
293 Don't sink BLKmode assignments if current function has any local explicit
294 register variables, as BLKmode assignments may involve memcpy or memset
295 calls or, on some targets, inline expansion thereof that sometimes need
296 to use specific hard registers.
299 if (stmt_ends_bb_p (stmt)
300 || gimple_has_side_effects (stmt)
301 || gimple_has_volatile_ops (stmt)
302 || (cfun->has_local_explicit_reg_vars
303 && TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt))) == BLKmode))
304 return false;
306 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (DEF_FROM_PTR (def_p)))
307 return false;
309 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
311 tree use = USE_FROM_PTR (use_p);
312 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use))
313 return false;
316 use = NULL;
318 /* If stmt is a store the one and only use needs to be the VOP
319 merging PHI node. */
320 if (virtual_operand_p (DEF_FROM_PTR (def_p)))
322 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, DEF_FROM_PTR (def_p))
324 gimple use_stmt = USE_STMT (use_p);
326 /* A killing definition is not a use. */
327 if ((gimple_has_lhs (use_stmt)
328 && operand_equal_p (gimple_assign_lhs (stmt),
329 gimple_get_lhs (use_stmt), 0))
330 || stmt_kills_ref_p (use_stmt, gimple_assign_lhs (stmt)))
332 /* If use_stmt is or might be a nop assignment then USE_STMT
333 acts as a use as well as definition. */
334 if (stmt != use_stmt
335 && ref_maybe_used_by_stmt_p (use_stmt,
336 gimple_assign_lhs (stmt)))
337 return false;
338 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 (gimple_vuse (stmt)
358 || !all_immediate_uses_same_place (def_p))
360 bool debug_stmts = false;
361 basic_block commondom = nearest_common_dominator_of_uses (def_p,
362 &debug_stmts);
364 if (commondom == frombb)
365 return false;
367 /* If this is a load then do not sink past any stores.
368 ??? This is overly simple but cheap. We basically look
369 for an existing load with the same VUSE in the path to one
370 of the sink candidate blocks and we adjust commondom to the
371 nearest to commondom. */
372 if (gimple_vuse (stmt))
374 /* Do not sink loads from hard registers. */
375 if (gimple_assign_single_p (stmt)
376 && TREE_CODE (gimple_assign_rhs1 (stmt)) == VAR_DECL
377 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt)))
378 return false;
380 imm_use_iterator imm_iter;
381 use_operand_p use_p;
382 basic_block found = NULL;
383 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, gimple_vuse (stmt))
385 gimple use_stmt = USE_STMT (use_p);
386 basic_block bb = gimple_bb (use_stmt);
387 /* For PHI nodes the block we know sth about
388 is the incoming block with the use. */
389 if (gimple_code (use_stmt) == GIMPLE_PHI)
390 bb = EDGE_PRED (bb, PHI_ARG_INDEX_FROM_USE (use_p))->src;
391 /* Any dominator of commondom would be ok with
392 adjusting commondom to that block. */
393 bb = nearest_common_dominator (CDI_DOMINATORS, bb, commondom);
394 if (!found)
395 found = bb;
396 else if (dominated_by_p (CDI_DOMINATORS, bb, found))
397 found = bb;
398 /* If we can't improve, stop. */
399 if (found == commondom)
400 break;
402 commondom = found;
403 if (commondom == frombb)
404 return false;
407 /* Our common dominator has to be dominated by frombb in order to be a
408 trivially safe place to put this statement, since it has multiple
409 uses. */
410 if (!dominated_by_p (CDI_DOMINATORS, commondom, frombb))
411 return false;
413 commondom = select_best_block (frombb, commondom, stmt);
415 if (commondom == frombb)
416 return false;
418 *togsi = gsi_after_labels (commondom);
420 return true;
422 else
424 FOR_EACH_IMM_USE_FAST (one_use, imm_iter, DEF_FROM_PTR (def_p))
426 if (is_gimple_debug (USE_STMT (one_use)))
427 continue;
428 break;
430 use = USE_STMT (one_use);
432 if (gimple_code (use) != GIMPLE_PHI)
434 sinkbb = gimple_bb (use);
435 sinkbb = select_best_block (frombb, gimple_bb (use), stmt);
437 if (sinkbb == frombb)
438 return false;
440 *togsi = gsi_for_stmt (use);
442 return true;
446 sinkbb = find_bb_for_arg (as_a <gphi *> (use), DEF_FROM_PTR (def_p));
448 /* This can happen if there are multiple uses in a PHI. */
449 if (!sinkbb)
450 return false;
452 sinkbb = select_best_block (frombb, sinkbb, stmt);
453 if (!sinkbb || sinkbb == frombb)
454 return false;
456 /* If the latch block is empty, don't make it non-empty by sinking
457 something into it. */
458 if (sinkbb == frombb->loop_father->latch
459 && empty_block_p (sinkbb))
460 return false;
462 *togsi = gsi_after_labels (sinkbb);
464 return true;
467 /* Perform code sinking on BB */
469 static void
470 sink_code_in_bb (basic_block bb)
472 basic_block son;
473 gimple_stmt_iterator gsi;
474 edge_iterator ei;
475 edge e;
476 bool last = true;
478 /* If this block doesn't dominate anything, there can't be any place to sink
479 the statements to. */
480 if (first_dom_son (CDI_DOMINATORS, bb) == NULL)
481 goto earlyout;
483 /* We can't move things across abnormal edges, so don't try. */
484 FOR_EACH_EDGE (e, ei, bb->succs)
485 if (e->flags & EDGE_ABNORMAL)
486 goto earlyout;
488 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);)
490 gimple stmt = gsi_stmt (gsi);
491 gimple_stmt_iterator togsi;
493 if (!statement_sink_location (stmt, bb, &togsi))
495 if (!gsi_end_p (gsi))
496 gsi_prev (&gsi);
497 last = false;
498 continue;
500 if (dump_file)
502 fprintf (dump_file, "Sinking ");
503 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS);
504 fprintf (dump_file, " from bb %d to bb %d\n",
505 bb->index, (gsi_bb (togsi))->index);
508 /* Update virtual operands of statements in the path we
509 do not sink to. */
510 if (gimple_vdef (stmt))
512 imm_use_iterator iter;
513 use_operand_p use_p;
514 gimple vuse_stmt;
516 FOR_EACH_IMM_USE_STMT (vuse_stmt, iter, gimple_vdef (stmt))
517 if (gimple_code (vuse_stmt) != GIMPLE_PHI)
518 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
519 SET_USE (use_p, gimple_vuse (stmt));
522 /* If this is the end of the basic block, we need to insert at the end
523 of the basic block. */
524 if (gsi_end_p (togsi))
525 gsi_move_to_bb_end (&gsi, gsi_bb (togsi));
526 else
527 gsi_move_before (&gsi, &togsi);
529 sink_stats.sunk++;
531 /* If we've just removed the last statement of the BB, the
532 gsi_end_p() test below would fail, but gsi_prev() would have
533 succeeded, and we want it to succeed. So we keep track of
534 whether we're at the last statement and pick up the new last
535 statement. */
536 if (last)
538 gsi = gsi_last_bb (bb);
539 continue;
542 last = false;
543 if (!gsi_end_p (gsi))
544 gsi_prev (&gsi);
547 earlyout:
548 for (son = first_dom_son (CDI_POST_DOMINATORS, bb);
549 son;
550 son = next_dom_son (CDI_POST_DOMINATORS, son))
552 sink_code_in_bb (son);
556 /* Perform code sinking.
557 This moves code down the flowgraph when we know it would be
558 profitable to do so, or it wouldn't increase the number of
559 executions of the statement.
561 IE given
563 a_1 = b + c;
564 if (<something>)
567 else
569 foo (&b, &c);
570 a_5 = b + c;
572 a_6 = PHI (a_5, a_1);
573 USE a_6.
575 we'll transform this into:
577 if (<something>)
579 a_1 = b + c;
581 else
583 foo (&b, &c);
584 a_5 = b + c;
586 a_6 = PHI (a_5, a_1);
587 USE a_6.
589 Note that this reduces the number of computations of a = b + c to 1
590 when we take the else edge, instead of 2.
592 namespace {
594 const pass_data pass_data_sink_code =
596 GIMPLE_PASS, /* type */
597 "sink", /* name */
598 OPTGROUP_NONE, /* optinfo_flags */
599 TV_TREE_SINK, /* tv_id */
600 /* PROP_no_crit_edges is ensured by running split_critical_edges in
601 pass_data_sink_code::execute (). */
602 ( PROP_cfg | PROP_ssa ), /* properties_required */
603 0, /* properties_provided */
604 0, /* properties_destroyed */
605 0, /* todo_flags_start */
606 TODO_update_ssa, /* todo_flags_finish */
609 class pass_sink_code : public gimple_opt_pass
611 public:
612 pass_sink_code (gcc::context *ctxt)
613 : gimple_opt_pass (pass_data_sink_code, ctxt)
616 /* opt_pass methods: */
617 virtual bool gate (function *) { return flag_tree_sink != 0; }
618 virtual unsigned int execute (function *);
620 }; // class pass_sink_code
622 unsigned int
623 pass_sink_code::execute (function *fun)
625 loop_optimizer_init (LOOPS_NORMAL);
626 split_critical_edges ();
627 connect_infinite_loops_to_exit ();
628 memset (&sink_stats, 0, sizeof (sink_stats));
629 calculate_dominance_info (CDI_DOMINATORS);
630 calculate_dominance_info (CDI_POST_DOMINATORS);
631 sink_code_in_bb (EXIT_BLOCK_PTR_FOR_FN (fun));
632 statistics_counter_event (fun, "Sunk statements", sink_stats.sunk);
633 free_dominance_info (CDI_POST_DOMINATORS);
634 remove_fake_exit_edges ();
635 loop_optimizer_finalize ();
637 return 0;
640 } // anon namespace
642 gimple_opt_pass *
643 make_pass_sink_code (gcc::context *ctxt)
645 return new pass_sink_code (ctxt);