]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-ssa-sink.c
re PR driver/54210 (gcc unable to detect -mprfchw flag in bulldozer machines)
[thirdparty/gcc.git] / gcc / tree-ssa-sink.c
CommitLineData
ec1e9f7c 1/* Code sinking for trees
cf835838 2 Copyright (C) 2001, 2002, 2003, 2004, 2007, 2008, 2009, 2010
fc3103e7 3 Free Software Foundation, Inc.
ec1e9f7c
DB
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
9dcd6f09 10the Free Software Foundation; either version 3, or (at your option)
ec1e9f7c
DB
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
9dcd6f09
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
ec1e9f7c
DB
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "tm.h"
ec1e9f7c
DB
26#include "tree.h"
27#include "basic-block.h"
cf835838 28#include "gimple-pretty-print.h"
ec1e9f7c
DB
29#include "tree-inline.h"
30#include "tree-flow.h"
726a989a 31#include "gimple.h"
ec1e9f7c
DB
32#include "hashtab.h"
33#include "tree-iterator.h"
ec1e9f7c
DB
34#include "alloc-pool.h"
35#include "tree-pass.h"
36#include "flags.h"
37#include "bitmap.h"
ec1e9f7c 38#include "cfgloop.h"
1cc17820 39#include "params.h"
ec1e9f7c
DB
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
b8698a0f 51
ec1e9f7c
DB
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.
b8698a0f 63
ec1e9f7c
DB
64
65 2. Sinking using Partial Dead Code Elimination. */
66
67
68static struct
b8698a0f 69{
6c6cfbfd 70 /* The number of statements sunk down the flowgraph by code sinking. */
ec1e9f7c 71 int sunk;
b8698a0f 72
ec1e9f7c
DB
73} sink_stats;
74
75
f652d14b 76/* Given a PHI, and one of its arguments (DEF), find the edge for
ec1e9f7c
DB
77 that argument and return it. If the argument occurs twice in the PHI node,
78 we return NULL. */
79
80static basic_block
726a989a 81find_bb_for_arg (gimple phi, tree def)
ec1e9f7c 82{
726a989a 83 size_t i;
ec1e9f7c
DB
84 bool foundone = false;
85 basic_block result = NULL;
726a989a 86 for (i = 0; i < gimple_phi_num_args (phi); i++)
ec1e9f7c
DB
87 if (PHI_ARG_DEF (phi, i) == def)
88 {
89 if (foundone)
90 return NULL;
91 foundone = true;
726a989a 92 result = gimple_phi_arg_edge (phi, i)->src;
ec1e9f7c
DB
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
6c6cfbfd 103 used in, so that you only have one place you can sink it to. */
ec1e9f7c
DB
104
105static bool
726a989a 106all_immediate_uses_same_place (gimple stmt)
ec1e9f7c 107{
726a989a 108 gimple firstuse = NULL;
f430bae8
AM
109 ssa_op_iter op_iter;
110 imm_use_iterator imm_iter;
111 use_operand_p use_p;
112 tree var;
ec1e9f7c 113
f430bae8 114 FOR_EACH_SSA_TREE_OPERAND (var, stmt, op_iter, SSA_OP_ALL_DEFS)
ec1e9f7c 115 {
f430bae8
AM
116 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, var)
117 {
b5b8b0ac
AO
118 if (is_gimple_debug (USE_STMT (use_p)))
119 continue;
726a989a 120 if (firstuse == NULL)
f430bae8
AM
121 firstuse = USE_STMT (use_p);
122 else
123 if (firstuse != USE_STMT (use_p))
124 return false;
125 }
ec1e9f7c 126 }
f430bae8 127
ec1e9f7c
DB
128 return true;
129}
130
ec1e9f7c
DB
131/* Find the nearest common dominator of all of the immediate uses in IMM. */
132
133static basic_block
b5b8b0ac 134nearest_common_dominator_of_uses (gimple stmt, bool *debug_stmts)
b8698a0f 135{
ec1e9f7c
DB
136 bitmap blocks = BITMAP_ALLOC (NULL);
137 basic_block commondom;
ec1e9f7c
DB
138 unsigned int j;
139 bitmap_iterator bi;
f430bae8
AM
140 ssa_op_iter op_iter;
141 imm_use_iterator imm_iter;
142 use_operand_p use_p;
143 tree var;
144
ec1e9f7c 145 bitmap_clear (blocks);
f430bae8 146 FOR_EACH_SSA_TREE_OPERAND (var, stmt, op_iter, SSA_OP_ALL_DEFS)
ec1e9f7c 147 {
f430bae8
AM
148 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, var)
149 {
726a989a 150 gimple usestmt = USE_STMT (use_p);
f430bae8 151 basic_block useblock;
000b62dc 152
726a989a 153 if (gimple_code (usestmt) == GIMPLE_PHI)
f430bae8 154 {
55b12f0d 155 int idx = PHI_ARG_INDEX_FROM_USE (use_p);
ab798313 156
726a989a 157 useblock = gimple_phi_arg_edge (usestmt, idx)->src;
f430bae8 158 }
b5b8b0ac
AO
159 else if (is_gimple_debug (usestmt))
160 {
161 *debug_stmts = true;
162 continue;
163 }
f430bae8 164 else
ec1e9f7c 165 {
726a989a 166 useblock = gimple_bb (usestmt);
000b62dc 167 }
f430bae8 168
000b62dc
KH
169 /* Short circuit. Nothing dominates the entry block. */
170 if (useblock == ENTRY_BLOCK_PTR)
171 {
172 BITMAP_FREE (blocks);
173 return NULL;
ec1e9f7c 174 }
000b62dc 175 bitmap_set_bit (blocks, useblock->index);
ec1e9f7c 176 }
ec1e9f7c
DB
177 }
178 commondom = BASIC_BLOCK (bitmap_first_set_bit (blocks));
179 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, j, bi)
b8698a0f 180 commondom = nearest_common_dominator (CDI_DOMINATORS, commondom,
ec1e9f7c
DB
181 BASIC_BLOCK (j));
182 BITMAP_FREE (blocks);
183 return commondom;
184}
185
1cc17820
JL
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. */
215 if (temp_bb->loop_depth < best_bb->loop_depth)
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. */
226 if (best_bb->loop_depth < early_bb->loop_depth)
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. */
242 if (best_bb->loop_depth == early_bb->loop_depth
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
b8698a0f 251/* Given a statement (STMT) and the basic block it is currently in (FROMBB),
ec1e9f7c 252 determine the location to sink the statement to, if any.
726a989a
RB
253 Returns true if there is such location; in that case, TOGSI points to the
254 statement before that STMT should be moved. */
ec1e9f7c 255
18965703 256static bool
726a989a
RB
257statement_sink_location (gimple stmt, basic_block frombb,
258 gimple_stmt_iterator *togsi)
ec1e9f7c 259{
726a989a 260 gimple use;
f430bae8 261 use_operand_p one_use = NULL_USE_OPERAND_P;
ec1e9f7c
DB
262 basic_block sinkbb;
263 use_operand_p use_p;
264 def_operand_p def_p;
265 ssa_op_iter iter;
f430bae8
AM
266 imm_use_iterator imm_iter;
267
e106efc7
RG
268 /* We only can sink assignments. */
269 if (!is_gimple_assign (stmt))
270 return false;
ec1e9f7c 271
e106efc7
RG
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)
18965703 275 return false;
ec1e9f7c 276
e106efc7
RG
277 /* Return if there are no immediate uses of this stmt. */
278 if (has_zero_uses (DEF_FROM_PTR (def_p)))
18965703 279 return false;
ec1e9f7c
DB
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
b8698a0f
L
283 because it's not legal.
284
ec1e9f7c
DB
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
b8698a0f
L
288 moved to.
289
ec1e9f7c
DB
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
b8698a0f 295 We can't sink statements that have volatile operands.
ec1e9f7c
DB
296
297 We don't want to sink dead code, so anything with 0 immediate uses is not
fc3103e7
JJ
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.
ec1e9f7c
DB
304
305 */
f47c96aa 306 if (stmt_ends_bb_p (stmt)
726a989a 307 || gimple_has_side_effects (stmt)
726a989a 308 || gimple_has_volatile_ops (stmt)
e106efc7 309 || (gimple_vuse (stmt) && !gimple_vdef (stmt))
fc3103e7
JJ
310 || (cfun->has_local_explicit_reg_vars
311 && TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt))) == BLKmode))
18965703 312 return false;
b8698a0f 313
e106efc7
RG
314 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (DEF_FROM_PTR (def_p)))
315 return false;
b8698a0f 316
ec1e9f7c
DB
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))
18965703 321 return false;
ec1e9f7c 322 }
b8698a0f 323
e106efc7
RG
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 }
ec1e9f7c
DB
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. */
e106efc7 357 else if (!all_immediate_uses_same_place (stmt))
ec1e9f7c 358 {
b5b8b0ac
AO
359 bool debug_stmts = false;
360 basic_block commondom = nearest_common_dominator_of_uses (stmt,
361 &debug_stmts);
b8698a0f 362
ec1e9f7c 363 if (commondom == frombb)
18965703 364 return false;
ec1e9f7c
DB
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
b8698a0f 368 uses. */
ec1e9f7c 369 if (!dominated_by_p (CDI_DOMINATORS, commondom, frombb))
18965703 370 return false;
b8698a0f 371
1cc17820 372 commondom = select_best_block (frombb, commondom, stmt);
ec1e9f7c 373
1cc17820
JL
374 if (commondom == frombb)
375 return false;
b5b8b0ac 376
726a989a 377 *togsi = gsi_after_labels (commondom);
b5b8b0ac 378
18965703 379 return true;
ec1e9f7c 380 }
e106efc7 381 else
ec1e9f7c 382 {
e106efc7
RG
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);
726a989a 390
e106efc7
RG
391 if (gimple_code (use) != GIMPLE_PHI)
392 {
393 sinkbb = gimple_bb (use);
1cc17820 394 sinkbb = select_best_block (frombb, gimple_bb (use), stmt);
791b59e3 395
1cc17820 396 if (sinkbb == frombb)
e106efc7 397 return false;
b5b8b0ac 398
e106efc7 399 *togsi = gsi_for_stmt (use);
ec1e9f7c 400
e106efc7
RG
401 return true;
402 }
403 }
f47c96aa 404
e106efc7 405 sinkbb = find_bb_for_arg (use, DEF_FROM_PTR (def_p));
ec1e9f7c 406
1cc17820
JL
407 /* This can happen if there are multiple uses in a PHI. */
408 if (!sinkbb)
18965703 409 return false;
1cc17820
JL
410
411 sinkbb = select_best_block (frombb, sinkbb, stmt);
412 if (!sinkbb || sinkbb == frombb)
18965703
ZD
413 return false;
414
3834917d
MM
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
726a989a 421 *togsi = gsi_after_labels (sinkbb);
ec1e9f7c 422
18965703 423 return true;
ec1e9f7c
DB
424}
425
426/* Perform code sinking on BB */
427
428static void
429sink_code_in_bb (basic_block bb)
430{
431 basic_block son;
726a989a 432 gimple_stmt_iterator gsi;
ec1e9f7c
DB
433 edge_iterator ei;
434 edge e;
9a287593 435 bool last = true;
b8698a0f 436
ec1e9f7c
DB
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
726a989a 447 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);)
ec1e9f7c 448 {
b8698a0f 449 gimple stmt = gsi_stmt (gsi);
726a989a 450 gimple_stmt_iterator togsi;
18965703 451
726a989a 452 if (!statement_sink_location (stmt, bb, &togsi))
ec1e9f7c 453 {
726a989a
RB
454 if (!gsi_end_p (gsi))
455 gsi_prev (&gsi);
9a287593 456 last = false;
ec1e9f7c 457 continue;
b8698a0f 458 }
ec1e9f7c
DB
459 if (dump_file)
460 {
461 fprintf (dump_file, "Sinking ");
726a989a 462 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS);
ec1e9f7c 463 fprintf (dump_file, " from bb %d to bb %d\n",
726a989a 464 bb->index, (gsi_bb (togsi))->index);
ec1e9f7c 465 }
b8698a0f 466
ef13324e
RG
467 /* Update virtual operands of statements in the path we
468 do not sink to. */
e106efc7
RG
469 if (gimple_vdef (stmt))
470 {
ef13324e
RG
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));
e106efc7
RG
479 }
480
ec1e9f7c
DB
481 /* If this is the end of the basic block, we need to insert at the end
482 of the basic block. */
726a989a
RB
483 if (gsi_end_p (togsi))
484 gsi_move_to_bb_end (&gsi, gsi_bb (togsi));
ec1e9f7c 485 else
726a989a 486 gsi_move_before (&gsi, &togsi);
ec1e9f7c
DB
487
488 sink_stats.sunk++;
9a287593
AO
489
490 /* If we've just removed the last statement of the BB, the
726a989a 491 gsi_end_p() test below would fail, but gsi_prev() would have
9a287593
AO
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 {
726a989a 497 gsi = gsi_last_bb (bb);
9a287593
AO
498 continue;
499 }
500
501 last = false;
726a989a
RB
502 if (!gsi_end_p (gsi))
503 gsi_prev (&gsi);
b8698a0f 504
ec1e9f7c
DB
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 }
b8698a0f 513}
ec1e9f7c
DB
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
b8698a0f 521
ec1e9f7c
DB
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{
598ec7bd 554 loop_optimizer_init (LOOPS_NORMAL);
10d22567 555
ec1e9f7c
DB
556 connect_infinite_loops_to_exit ();
557 memset (&sink_stats, 0, sizeof (sink_stats));
3b5ee6a4
RG
558 calculate_dominance_info (CDI_DOMINATORS);
559 calculate_dominance_info (CDI_POST_DOMINATORS);
b8698a0f 560 sink_code_in_bb (EXIT_BLOCK_PTR);
01902653 561 statistics_counter_event (cfun, "Sunk statements", sink_stats.sunk);
ec1e9f7c
DB
562 free_dominance_info (CDI_POST_DOMINATORS);
563 remove_fake_exit_edges ();
598ec7bd 564 loop_optimizer_finalize ();
ec1e9f7c
DB
565}
566
567/* Gate and execute functions for PRE. */
568
c2924966 569static unsigned int
ec1e9f7c
DB
570do_sink (void)
571{
572 execute_sink_code ();
c2924966 573 return 0;
ec1e9f7c
DB
574}
575
576static bool
577gate_sink (void)
578{
579 return flag_tree_sink != 0;
580}
581
8ddbbcae 582struct gimple_opt_pass pass_sink_code =
ec1e9f7c 583{
8ddbbcae
JH
584 {
585 GIMPLE_PASS,
ec1e9f7c
DB
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
4effdf02 594 | PROP_ssa, /* properties_required */
ec1e9f7c
DB
595 0, /* properties_provided */
596 0, /* properties_destroyed */
597 0, /* todo_flags_start */
b8698a0f 598 TODO_update_ssa
c7dd803e
EB
599 | TODO_verify_ssa
600 | TODO_verify_flow
c7dd803e 601 | TODO_ggc_collect /* todo_flags_finish */
8ddbbcae 602 }
ec1e9f7c 603};