]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-ssa-sink.c
Update Copyright years for files modified in 2011 and/or 2012.
[thirdparty/gcc.git] / gcc / tree-ssa-sink.c
CommitLineData
f3ec9a6c 1/* Code sinking for trees
71e45bc2 2 Copyright (C) 2001, 2002, 2003, 2004, 2007, 2008, 2009, 2010, 2011, 2012
e15deb4b 3 Free Software Foundation, Inc.
f3ec9a6c 4 Contributed by Daniel Berlin <dan@dberlin.org>
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
8c4c00c1 10the Free Software Foundation; either version 3, or (at your option)
f3ec9a6c 11any later version.
12
13GCC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
8c4c00c1 19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
f3ec9a6c 21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "tm.h"
f3ec9a6c 26#include "tree.h"
27#include "basic-block.h"
ce084dfc 28#include "gimple-pretty-print.h"
f3ec9a6c 29#include "tree-inline.h"
30#include "tree-flow.h"
75a70cf9 31#include "gimple.h"
f3ec9a6c 32#include "hashtab.h"
33#include "tree-iterator.h"
f3ec9a6c 34#include "alloc-pool.h"
35#include "tree-pass.h"
36#include "flags.h"
37#include "bitmap.h"
f3ec9a6c 38#include "cfgloop.h"
77ecaaba 39#include "params.h"
f3ec9a6c 40
41/* TODO:
42 1. Sinking store only using scalar promotion (IE without moving the RHS):
43
44 *q = p;
45 p = p + 1;
46 if (something)
47 *q = <not p>;
48 else
49 y = *q;
50
48e1416a 51
f3ec9a6c 52 should become
53 sinktemp = p;
54 p = p + 1;
55 if (something)
56 *q = <not p>;
57 else
58 {
59 *q = sinktemp;
60 y = *q
61 }
62 Store copy propagation will take care of the store elimination above.
48e1416a 63
f3ec9a6c 64
65 2. Sinking using Partial Dead Code Elimination. */
66
67
68static struct
48e1416a 69{
f7f07c95 70 /* The number of statements sunk down the flowgraph by code sinking. */
f3ec9a6c 71 int sunk;
48e1416a 72
f3ec9a6c 73} sink_stats;
74
75
fe24f256 76/* Given a PHI, and one of its arguments (DEF), find the edge for
f3ec9a6c 77 that argument and return it. If the argument occurs twice in the PHI node,
78 we return NULL. */
79
80static basic_block
75a70cf9 81find_bb_for_arg (gimple phi, tree def)
f3ec9a6c 82{
75a70cf9 83 size_t i;
f3ec9a6c 84 bool foundone = false;
85 basic_block result = NULL;
75a70cf9 86 for (i = 0; i < gimple_phi_num_args (phi); i++)
f3ec9a6c 87 if (PHI_ARG_DEF (phi, i) == def)
88 {
89 if (foundone)
90 return NULL;
91 foundone = true;
75a70cf9 92 result = gimple_phi_arg_edge (phi, i)->src;
f3ec9a6c 93 }
94 return result;
95}
96
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
f7f07c95 103 used in, so that you only have one place you can sink it to. */
f3ec9a6c 104
105static bool
75a70cf9 106all_immediate_uses_same_place (gimple stmt)
f3ec9a6c 107{
75a70cf9 108 gimple firstuse = NULL;
22aa74c4 109 ssa_op_iter op_iter;
110 imm_use_iterator imm_iter;
111 use_operand_p use_p;
112 tree var;
f3ec9a6c 113
22aa74c4 114 FOR_EACH_SSA_TREE_OPERAND (var, stmt, op_iter, SSA_OP_ALL_DEFS)
f3ec9a6c 115 {
22aa74c4 116 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, var)
117 {
9845d120 118 if (is_gimple_debug (USE_STMT (use_p)))
119 continue;
75a70cf9 120 if (firstuse == NULL)
22aa74c4 121 firstuse = USE_STMT (use_p);
122 else
123 if (firstuse != USE_STMT (use_p))
124 return false;
125 }
f3ec9a6c 126 }
22aa74c4 127
f3ec9a6c 128 return true;
129}
130
f3ec9a6c 131/* Find the nearest common dominator of all of the immediate uses in IMM. */
132
133static basic_block
9845d120 134nearest_common_dominator_of_uses (gimple stmt, bool *debug_stmts)
48e1416a 135{
f3ec9a6c 136 bitmap blocks = BITMAP_ALLOC (NULL);
137 basic_block commondom;
f3ec9a6c 138 unsigned int j;
139 bitmap_iterator bi;
22aa74c4 140 ssa_op_iter op_iter;
141 imm_use_iterator imm_iter;
142 use_operand_p use_p;
143 tree var;
144
f3ec9a6c 145 bitmap_clear (blocks);
22aa74c4 146 FOR_EACH_SSA_TREE_OPERAND (var, stmt, op_iter, SSA_OP_ALL_DEFS)
f3ec9a6c 147 {
22aa74c4 148 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, var)
149 {
75a70cf9 150 gimple usestmt = USE_STMT (use_p);
22aa74c4 151 basic_block useblock;
c52a36cc 152
75a70cf9 153 if (gimple_code (usestmt) == GIMPLE_PHI)
22aa74c4 154 {
737b5433 155 int idx = PHI_ARG_INDEX_FROM_USE (use_p);
c48d3403 156
75a70cf9 157 useblock = gimple_phi_arg_edge (usestmt, idx)->src;
22aa74c4 158 }
9845d120 159 else if (is_gimple_debug (usestmt))
160 {
161 *debug_stmts = true;
162 continue;
163 }
22aa74c4 164 else
f3ec9a6c 165 {
75a70cf9 166 useblock = gimple_bb (usestmt);
c52a36cc 167 }
22aa74c4 168
c52a36cc 169 /* Short circuit. Nothing dominates the entry block. */
170 if (useblock == ENTRY_BLOCK_PTR)
171 {
172 BITMAP_FREE (blocks);
173 return NULL;
f3ec9a6c 174 }
c52a36cc 175 bitmap_set_bit (blocks, useblock->index);
f3ec9a6c 176 }
f3ec9a6c 177 }
178 commondom = BASIC_BLOCK (bitmap_first_set_bit (blocks));
179 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, j, bi)
48e1416a 180 commondom = nearest_common_dominator (CDI_DOMINATORS, commondom,
f3ec9a6c 181 BASIC_BLOCK (j));
182 BITMAP_FREE (blocks);
183 return commondom;
184}
185
77ecaaba 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.
189
190 We want the most control dependent block in the shallowest loop nest.
191
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.
196
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. */
201
202static basic_block
203select_best_block (basic_block early_bb,
204 basic_block late_bb,
205 gimple stmt)
206{
207 basic_block best_bb = late_bb;
208 basic_block temp_bb = late_bb;
209 int threshold;
210
211 while (temp_bb != early_bb)
212 {
213 /* If we've moved into a lower loop nest, then that becomes
214 our best block. */
6b42039a 215 if (bb_loop_depth (temp_bb) < bb_loop_depth (best_bb))
77ecaaba 216 best_bb = temp_bb;
217
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);
221 }
222
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. */
6b42039a 226 if (bb_loop_depth (best_bb) < bb_loop_depth (early_bb))
77ecaaba 227 return best_bb;
228
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))
234 {
235 threshold += 7;
236 if (threshold > 100)
237 threshold = 100;
238 }
239
240 /* If BEST_BB is at the same nesting level, then require it to have
241 significantly lower execution frequency to avoid gratutious movement. */
6b42039a 242 if (bb_loop_depth (best_bb) == bb_loop_depth (early_bb)
77ecaaba 243 && best_bb->frequency < (early_bb->frequency * threshold / 100.0))
244 return best_bb;
245
246 /* No better block found, so return EARLY_BB, which happens to be the
247 statement's original block. */
248 return early_bb;
249}
250
48e1416a 251/* Given a statement (STMT) and the basic block it is currently in (FROMBB),
f3ec9a6c 252 determine the location to sink the statement to, if any.
75a70cf9 253 Returns true if there is such location; in that case, TOGSI points to the
254 statement before that STMT should be moved. */
f3ec9a6c 255
4122c2d4 256static bool
75a70cf9 257statement_sink_location (gimple stmt, basic_block frombb,
258 gimple_stmt_iterator *togsi)
f3ec9a6c 259{
75a70cf9 260 gimple use;
22aa74c4 261 use_operand_p one_use = NULL_USE_OPERAND_P;
f3ec9a6c 262 basic_block sinkbb;
263 use_operand_p use_p;
264 def_operand_p def_p;
265 ssa_op_iter iter;
22aa74c4 266 imm_use_iterator imm_iter;
267
2109076a 268 /* We only can sink assignments. */
269 if (!is_gimple_assign (stmt))
270 return false;
f3ec9a6c 271
2109076a 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)
4122c2d4 275 return false;
f3ec9a6c 276
2109076a 277 /* Return if there are no immediate uses of this stmt. */
278 if (has_zero_uses (DEF_FROM_PTR (def_p)))
4122c2d4 279 return false;
f3ec9a6c 280
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
48e1416a 283 because it's not legal.
284
f3ec9a6c 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
48e1416a 288 moved to.
289
f3ec9a6c 290 We don't want to sink loads from memory.
291
292 We can't sink statements that end basic blocks without splitting the
293 incoming edge for the sink location to place it there.
294
48e1416a 295 We can't sink statements that have volatile operands.
f3ec9a6c 296
297 We don't want to sink dead code, so anything with 0 immediate uses is not
e15deb4b 298 sunk.
299
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.
f3ec9a6c 304
305 */
b66731e8 306 if (stmt_ends_bb_p (stmt)
75a70cf9 307 || gimple_has_side_effects (stmt)
75a70cf9 308 || gimple_has_volatile_ops (stmt)
2109076a 309 || (gimple_vuse (stmt) && !gimple_vdef (stmt))
e15deb4b 310 || (cfun->has_local_explicit_reg_vars
311 && TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt))) == BLKmode))
4122c2d4 312 return false;
48e1416a 313
2109076a 314 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (DEF_FROM_PTR (def_p)))
315 return false;
48e1416a 316
f3ec9a6c 317 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
318 {
319 tree use = USE_FROM_PTR (use_p);
320 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use))
4122c2d4 321 return false;
f3ec9a6c 322 }
48e1416a 323
2109076a 324 use = NULL;
325
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))
329 {
330 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, DEF_FROM_PTR (def_p))
331 {
332 gimple use_stmt = USE_STMT (use_p);
333
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;
340
341 if (gimple_code (use_stmt) != GIMPLE_PHI)
342 return false;
343
344 if (use
345 && use != use_stmt)
346 return false;
347
348 use = use_stmt;
349 }
350 if (!use)
351 return false;
352 }
f3ec9a6c 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. */
2109076a 357 else if (!all_immediate_uses_same_place (stmt))
f3ec9a6c 358 {
9845d120 359 bool debug_stmts = false;
360 basic_block commondom = nearest_common_dominator_of_uses (stmt,
361 &debug_stmts);
48e1416a 362
f3ec9a6c 363 if (commondom == frombb)
4122c2d4 364 return false;
f3ec9a6c 365
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
48e1416a 368 uses. */
f3ec9a6c 369 if (!dominated_by_p (CDI_DOMINATORS, commondom, frombb))
4122c2d4 370 return false;
48e1416a 371
77ecaaba 372 commondom = select_best_block (frombb, commondom, stmt);
f3ec9a6c 373
77ecaaba 374 if (commondom == frombb)
375 return false;
9845d120 376
75a70cf9 377 *togsi = gsi_after_labels (commondom);
9845d120 378
4122c2d4 379 return true;
f3ec9a6c 380 }
2109076a 381 else
f3ec9a6c 382 {
2109076a 383 FOR_EACH_IMM_USE_FAST (one_use, imm_iter, DEF_FROM_PTR (def_p))
384 {
385 if (is_gimple_debug (USE_STMT (one_use)))
386 continue;
387 break;
388 }
389 use = USE_STMT (one_use);
75a70cf9 390
2109076a 391 if (gimple_code (use) != GIMPLE_PHI)
392 {
393 sinkbb = gimple_bb (use);
77ecaaba 394 sinkbb = select_best_block (frombb, gimple_bb (use), stmt);
258b3d02 395
77ecaaba 396 if (sinkbb == frombb)
2109076a 397 return false;
9845d120 398
2109076a 399 *togsi = gsi_for_stmt (use);
f3ec9a6c 400
2109076a 401 return true;
402 }
403 }
b66731e8 404
2109076a 405 sinkbb = find_bb_for_arg (use, DEF_FROM_PTR (def_p));
f3ec9a6c 406
77ecaaba 407 /* This can happen if there are multiple uses in a PHI. */
408 if (!sinkbb)
4122c2d4 409 return false;
77ecaaba 410
411 sinkbb = select_best_block (frombb, sinkbb, stmt);
412 if (!sinkbb || sinkbb == frombb)
4122c2d4 413 return false;
414
87e53408 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;
420
75a70cf9 421 *togsi = gsi_after_labels (sinkbb);
f3ec9a6c 422
4122c2d4 423 return true;
f3ec9a6c 424}
425
426/* Perform code sinking on BB */
427
428static void
429sink_code_in_bb (basic_block bb)
430{
431 basic_block son;
75a70cf9 432 gimple_stmt_iterator gsi;
f3ec9a6c 433 edge_iterator ei;
434 edge e;
5add5f91 435 bool last = true;
48e1416a 436
f3ec9a6c 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;
441
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;
446
75a70cf9 447 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);)
f3ec9a6c 448 {
48e1416a 449 gimple stmt = gsi_stmt (gsi);
75a70cf9 450 gimple_stmt_iterator togsi;
4122c2d4 451
75a70cf9 452 if (!statement_sink_location (stmt, bb, &togsi))
f3ec9a6c 453 {
75a70cf9 454 if (!gsi_end_p (gsi))
455 gsi_prev (&gsi);
5add5f91 456 last = false;
f3ec9a6c 457 continue;
48e1416a 458 }
f3ec9a6c 459 if (dump_file)
460 {
461 fprintf (dump_file, "Sinking ");
75a70cf9 462 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS);
f3ec9a6c 463 fprintf (dump_file, " from bb %d to bb %d\n",
75a70cf9 464 bb->index, (gsi_bb (togsi))->index);
f3ec9a6c 465 }
48e1416a 466
63eb2430 467 /* Update virtual operands of statements in the path we
468 do not sink to. */
2109076a 469 if (gimple_vdef (stmt))
470 {
63eb2430 471 imm_use_iterator iter;
472 use_operand_p use_p;
473 gimple vuse_stmt;
474
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));
2109076a 479 }
480
f3ec9a6c 481 /* If this is the end of the basic block, we need to insert at the end
482 of the basic block. */
75a70cf9 483 if (gsi_end_p (togsi))
484 gsi_move_to_bb_end (&gsi, gsi_bb (togsi));
f3ec9a6c 485 else
75a70cf9 486 gsi_move_before (&gsi, &togsi);
f3ec9a6c 487
488 sink_stats.sunk++;
5add5f91 489
490 /* If we've just removed the last statement of the BB, the
75a70cf9 491 gsi_end_p() test below would fail, but gsi_prev() would have
5add5f91 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)
496 {
75a70cf9 497 gsi = gsi_last_bb (bb);
5add5f91 498 continue;
499 }
500
501 last = false;
75a70cf9 502 if (!gsi_end_p (gsi))
503 gsi_prev (&gsi);
48e1416a 504
f3ec9a6c 505 }
506 earlyout:
507 for (son = first_dom_son (CDI_POST_DOMINATORS, bb);
508 son;
509 son = next_dom_son (CDI_POST_DOMINATORS, son))
510 {
511 sink_code_in_bb (son);
512 }
48e1416a 513}
f3ec9a6c 514
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.
519
520 IE given
48e1416a 521
f3ec9a6c 522 a_1 = b + c;
523 if (<something>)
524 {
525 }
526 else
527 {
528 foo (&b, &c);
529 a_5 = b + c;
530 }
531 a_6 = PHI (a_5, a_1);
532 USE a_6.
533
534 we'll transform this into:
535
536 if (<something>)
537 {
538 a_1 = b + c;
539 }
540 else
541 {
542 foo (&b, &c);
543 a_5 = b + c;
544 }
545 a_6 = PHI (a_5, a_1);
546 USE a_6.
547
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.
550*/
551static void
552execute_sink_code (void)
553{
88e6f696 554 loop_optimizer_init (LOOPS_NORMAL);
3f5be5f4 555
f3ec9a6c 556 connect_infinite_loops_to_exit ();
557 memset (&sink_stats, 0, sizeof (sink_stats));
f1456a87 558 calculate_dominance_info (CDI_DOMINATORS);
559 calculate_dominance_info (CDI_POST_DOMINATORS);
48e1416a 560 sink_code_in_bb (EXIT_BLOCK_PTR);
581f8050 561 statistics_counter_event (cfun, "Sunk statements", sink_stats.sunk);
f3ec9a6c 562 free_dominance_info (CDI_POST_DOMINATORS);
563 remove_fake_exit_edges ();
88e6f696 564 loop_optimizer_finalize ();
f3ec9a6c 565}
566
567/* Gate and execute functions for PRE. */
568
2a1990e9 569static unsigned int
f3ec9a6c 570do_sink (void)
571{
572 execute_sink_code ();
2a1990e9 573 return 0;
f3ec9a6c 574}
575
576static bool
577gate_sink (void)
578{
579 return flag_tree_sink != 0;
580}
581
20099e35 582struct gimple_opt_pass pass_sink_code =
f3ec9a6c 583{
20099e35 584 {
585 GIMPLE_PASS,
f3ec9a6c 586 "sink", /* name */
c7875731 587 OPTGROUP_NONE, /* optinfo_flags */
f3ec9a6c 588 gate_sink, /* gate */
589 do_sink, /* execute */
590 NULL, /* sub */
591 NULL, /* next */
592 0, /* static_pass_number */
593 TV_TREE_SINK, /* tv_id */
594 PROP_no_crit_edges | PROP_cfg
2f8eb909 595 | PROP_ssa, /* properties_required */
f3ec9a6c 596 0, /* properties_provided */
597 0, /* properties_destroyed */
598 0, /* todo_flags_start */
48e1416a 599 TODO_update_ssa
a2676c4f 600 | TODO_verify_ssa
601 | TODO_verify_flow
a2676c4f 602 | TODO_ggc_collect /* todo_flags_finish */
20099e35 603 }
f3ec9a6c 604};