]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-ssa-sink.c
tree-ssa-structalias.c: Remove tm_p.h from include.
[thirdparty/gcc.git] / gcc / tree-ssa-sink.c
CommitLineData
ec1e9f7c 1/* Code sinking for trees
66647d44 2 Copyright (C) 2001, 2002, 2003, 2004, 2007, 2008, 2009
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"
28#include "diagnostic.h"
29#include "tree-inline.h"
30#include "tree-flow.h"
726a989a 31#include "gimple.h"
ec1e9f7c
DB
32#include "tree-dump.h"
33#include "timevar.h"
34#include "fibheap.h"
35#include "hashtab.h"
36#include "tree-iterator.h"
ec1e9f7c
DB
37#include "alloc-pool.h"
38#include "tree-pass.h"
39#include "flags.h"
40#include "bitmap.h"
41#include "langhooks.h"
42#include "cfgloop.h"
43
44/* TODO:
45 1. Sinking store only using scalar promotion (IE without moving the RHS):
46
47 *q = p;
48 p = p + 1;
49 if (something)
50 *q = <not p>;
51 else
52 y = *q;
53
b8698a0f 54
ec1e9f7c
DB
55 should become
56 sinktemp = p;
57 p = p + 1;
58 if (something)
59 *q = <not p>;
60 else
61 {
62 *q = sinktemp;
63 y = *q
64 }
65 Store copy propagation will take care of the store elimination above.
b8698a0f 66
ec1e9f7c
DB
67
68 2. Sinking using Partial Dead Code Elimination. */
69
70
71static struct
b8698a0f 72{
6c6cfbfd 73 /* The number of statements sunk down the flowgraph by code sinking. */
ec1e9f7c 74 int sunk;
b8698a0f 75
ec1e9f7c
DB
76} sink_stats;
77
78
f652d14b 79/* Given a PHI, and one of its arguments (DEF), find the edge for
ec1e9f7c
DB
80 that argument and return it. If the argument occurs twice in the PHI node,
81 we return NULL. */
82
83static basic_block
726a989a 84find_bb_for_arg (gimple phi, tree def)
ec1e9f7c 85{
726a989a 86 size_t i;
ec1e9f7c
DB
87 bool foundone = false;
88 basic_block result = NULL;
726a989a 89 for (i = 0; i < gimple_phi_num_args (phi); i++)
ec1e9f7c
DB
90 if (PHI_ARG_DEF (phi, i) == def)
91 {
92 if (foundone)
93 return NULL;
94 foundone = true;
726a989a 95 result = gimple_phi_arg_edge (phi, i)->src;
ec1e9f7c
DB
96 }
97 return result;
98}
99
100/* When the first immediate use is in a statement, then return true if all
101 immediate uses in IMM are in the same statement.
102 We could also do the case where the first immediate use is in a phi node,
103 and all the other uses are in phis in the same basic block, but this
104 requires some expensive checking later (you have to make sure no def/vdef
105 in the statement occurs for multiple edges in the various phi nodes it's
6c6cfbfd 106 used in, so that you only have one place you can sink it to. */
ec1e9f7c
DB
107
108static bool
726a989a 109all_immediate_uses_same_place (gimple stmt)
ec1e9f7c 110{
726a989a 111 gimple firstuse = NULL;
f430bae8
AM
112 ssa_op_iter op_iter;
113 imm_use_iterator imm_iter;
114 use_operand_p use_p;
115 tree var;
ec1e9f7c 116
f430bae8 117 FOR_EACH_SSA_TREE_OPERAND (var, stmt, op_iter, SSA_OP_ALL_DEFS)
ec1e9f7c 118 {
f430bae8
AM
119 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, var)
120 {
b5b8b0ac
AO
121 if (is_gimple_debug (USE_STMT (use_p)))
122 continue;
726a989a 123 if (firstuse == NULL)
f430bae8
AM
124 firstuse = USE_STMT (use_p);
125 else
126 if (firstuse != USE_STMT (use_p))
127 return false;
128 }
ec1e9f7c 129 }
f430bae8 130
ec1e9f7c
DB
131 return true;
132}
133
38635499 134/* Some global stores don't necessarily have VDEF's of global variables,
ec1e9f7c
DB
135 but we still must avoid moving them around. */
136
137bool
726a989a 138is_hidden_global_store (gimple stmt)
ec1e9f7c 139{
ec1e9f7c 140 /* Check virtual definitions. If we get here, the only virtual
726a989a 141 definitions we should see are those generated by assignment or call
ec1e9f7c 142 statements. */
5006671f 143 if (gimple_vdef (stmt))
ec1e9f7c
DB
144 {
145 tree lhs;
146
726a989a 147 gcc_assert (is_gimple_assign (stmt) || is_gimple_call (stmt));
ec1e9f7c
DB
148
149 /* Note that we must not check the individual virtual operands
150 here. In particular, if this is an aliased store, we could
151 end up with something like the following (SSA notation
152 redacted for brevity):
153
154 foo (int *p, int i)
155 {
156 int x;
157 p_1 = (i_2 > 3) ? &x : p;
158
38635499 159 # x_4 = VDEF <x_3>
ec1e9f7c
DB
160 *p_1 = 5;
161
162 return 2;
163 }
164
165 Notice that the store to '*p_1' should be preserved, if we
166 were to check the virtual definitions in that store, we would
167 not mark it needed. This is because 'x' is not a global
168 variable.
169
170 Therefore, we check the base address of the LHS. If the
18cd8a03 171 address is a pointer, we check if its name tag or symbol tag is
ec1e9f7c
DB
172 a global variable. Otherwise, we check if the base variable
173 is a global. */
726a989a
RB
174 lhs = gimple_get_lhs (stmt);
175
ec1e9f7c
DB
176 if (REFERENCE_CLASS_P (lhs))
177 lhs = get_base_address (lhs);
178
179 if (lhs == NULL_TREE)
180 {
181 /* If LHS is NULL, it means that we couldn't get the base
182 address of the reference. In which case, we should not
183 move this store. */
184 return true;
185 }
186 else if (DECL_P (lhs))
187 {
188 /* If the store is to a global symbol, we need to keep it. */
189 if (is_global_var (lhs))
190 return true;
191
192 }
193 else if (INDIRECT_REF_P (lhs))
5006671f 194 return ptr_deref_may_alias_global_p (TREE_OPERAND (lhs, 0));
ec1e9f7c
DB
195 else
196 gcc_unreachable ();
197 }
38635499 198
ec1e9f7c
DB
199 return false;
200}
201
202/* Find the nearest common dominator of all of the immediate uses in IMM. */
203
204static basic_block
b5b8b0ac 205nearest_common_dominator_of_uses (gimple stmt, bool *debug_stmts)
b8698a0f 206{
ec1e9f7c
DB
207 bitmap blocks = BITMAP_ALLOC (NULL);
208 basic_block commondom;
ec1e9f7c
DB
209 unsigned int j;
210 bitmap_iterator bi;
f430bae8
AM
211 ssa_op_iter op_iter;
212 imm_use_iterator imm_iter;
213 use_operand_p use_p;
214 tree var;
215
ec1e9f7c 216 bitmap_clear (blocks);
f430bae8 217 FOR_EACH_SSA_TREE_OPERAND (var, stmt, op_iter, SSA_OP_ALL_DEFS)
ec1e9f7c 218 {
f430bae8
AM
219 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, var)
220 {
726a989a 221 gimple usestmt = USE_STMT (use_p);
f430bae8 222 basic_block useblock;
000b62dc 223
726a989a 224 if (gimple_code (usestmt) == GIMPLE_PHI)
f430bae8 225 {
55b12f0d 226 int idx = PHI_ARG_INDEX_FROM_USE (use_p);
ab798313 227
726a989a 228 useblock = gimple_phi_arg_edge (usestmt, idx)->src;
f430bae8 229 }
b5b8b0ac
AO
230 else if (is_gimple_debug (usestmt))
231 {
232 *debug_stmts = true;
233 continue;
234 }
f430bae8 235 else
ec1e9f7c 236 {
726a989a 237 useblock = gimple_bb (usestmt);
000b62dc 238 }
f430bae8 239
000b62dc
KH
240 /* Short circuit. Nothing dominates the entry block. */
241 if (useblock == ENTRY_BLOCK_PTR)
242 {
243 BITMAP_FREE (blocks);
244 return NULL;
ec1e9f7c 245 }
000b62dc 246 bitmap_set_bit (blocks, useblock->index);
ec1e9f7c 247 }
ec1e9f7c
DB
248 }
249 commondom = BASIC_BLOCK (bitmap_first_set_bit (blocks));
250 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, j, bi)
b8698a0f 251 commondom = nearest_common_dominator (CDI_DOMINATORS, commondom,
ec1e9f7c
DB
252 BASIC_BLOCK (j));
253 BITMAP_FREE (blocks);
254 return commondom;
255}
256
b8698a0f 257/* Given a statement (STMT) and the basic block it is currently in (FROMBB),
ec1e9f7c 258 determine the location to sink the statement to, if any.
726a989a
RB
259 Returns true if there is such location; in that case, TOGSI points to the
260 statement before that STMT should be moved. */
ec1e9f7c 261
18965703 262static bool
726a989a
RB
263statement_sink_location (gimple stmt, basic_block frombb,
264 gimple_stmt_iterator *togsi)
ec1e9f7c 265{
726a989a
RB
266 gimple use;
267 tree def;
f430bae8 268 use_operand_p one_use = NULL_USE_OPERAND_P;
ec1e9f7c
DB
269 basic_block sinkbb;
270 use_operand_p use_p;
271 def_operand_p def_p;
272 ssa_op_iter iter;
f430bae8
AM
273 imm_use_iterator imm_iter;
274
275 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
276 {
277 FOR_EACH_IMM_USE_FAST (one_use, imm_iter, def)
278 {
b5b8b0ac
AO
279 if (is_gimple_debug (USE_STMT (one_use)))
280 continue;
281
f430bae8
AM
282 break;
283 }
284 if (one_use != NULL_USE_OPERAND_P)
285 break;
286 }
ec1e9f7c 287
f430bae8
AM
288 /* Return if there are no immediate uses of this stmt. */
289 if (one_use == NULL_USE_OPERAND_P)
18965703 290 return false;
ec1e9f7c 291
726a989a 292 if (gimple_code (stmt) != GIMPLE_ASSIGN)
18965703 293 return false;
ec1e9f7c
DB
294
295 /* There are a few classes of things we can't or don't move, some because we
296 don't have code to handle it, some because it's not profitable and some
b8698a0f
L
297 because it's not legal.
298
ec1e9f7c
DB
299 We can't sink things that may be global stores, at least not without
300 calculating a lot more information, because we may cause it to no longer
301 be seen by an external routine that needs it depending on where it gets
b8698a0f
L
302 moved to.
303
ec1e9f7c
DB
304 We don't want to sink loads from memory.
305
306 We can't sink statements that end basic blocks without splitting the
307 incoming edge for the sink location to place it there.
308
b8698a0f 309 We can't sink statements that have volatile operands.
ec1e9f7c
DB
310
311 We don't want to sink dead code, so anything with 0 immediate uses is not
fc3103e7
JJ
312 sunk.
313
314 Don't sink BLKmode assignments if current function has any local explicit
315 register variables, as BLKmode assignments may involve memcpy or memset
316 calls or, on some targets, inline expansion thereof that sometimes need
317 to use specific hard registers.
ec1e9f7c
DB
318
319 */
f47c96aa 320 if (stmt_ends_bb_p (stmt)
726a989a 321 || gimple_has_side_effects (stmt)
ec1e9f7c 322 || is_hidden_global_store (stmt)
726a989a 323 || gimple_has_volatile_ops (stmt)
5006671f 324 || gimple_vuse (stmt)
fc3103e7
JJ
325 || (cfun->has_local_explicit_reg_vars
326 && TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt))) == BLKmode))
18965703 327 return false;
b8698a0f 328
ec1e9f7c
DB
329 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
330 {
331 tree def = DEF_FROM_PTR (def_p);
332 if (is_global_var (SSA_NAME_VAR (def))
333 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
18965703 334 return false;
ec1e9f7c 335 }
b8698a0f 336
ec1e9f7c
DB
337 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
338 {
339 tree use = USE_FROM_PTR (use_p);
340 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use))
18965703 341 return false;
ec1e9f7c 342 }
b8698a0f 343
ec1e9f7c
DB
344 /* If all the immediate uses are not in the same place, find the nearest
345 common dominator of all the immediate uses. For PHI nodes, we have to
346 find the nearest common dominator of all of the predecessor blocks, since
347 that is where insertion would have to take place. */
f430bae8 348 if (!all_immediate_uses_same_place (stmt))
ec1e9f7c 349 {
b5b8b0ac
AO
350 bool debug_stmts = false;
351 basic_block commondom = nearest_common_dominator_of_uses (stmt,
352 &debug_stmts);
b8698a0f 353
ec1e9f7c 354 if (commondom == frombb)
18965703 355 return false;
ec1e9f7c
DB
356
357 /* Our common dominator has to be dominated by frombb in order to be a
358 trivially safe place to put this statement, since it has multiple
b8698a0f 359 uses. */
ec1e9f7c 360 if (!dominated_by_p (CDI_DOMINATORS, commondom, frombb))
18965703 361 return false;
b8698a0f 362
ec1e9f7c
DB
363 /* It doesn't make sense to move to a dominator that post-dominates
364 frombb, because it means we've just moved it into a path that always
365 executes if frombb executes, instead of reducing the number of
366 executions . */
367 if (dominated_by_p (CDI_POST_DOMINATORS, frombb, commondom))
368 {
369 if (dump_file && (dump_flags & TDF_DETAILS))
370 fprintf (dump_file, "Not moving store, common dominator post-dominates from block.\n");
18965703 371 return false;
ec1e9f7c
DB
372 }
373
374 if (commondom == frombb || commondom->loop_depth > frombb->loop_depth)
18965703 375 return false;
ec1e9f7c
DB
376 if (dump_file && (dump_flags & TDF_DETAILS))
377 {
378 fprintf (dump_file, "Common dominator of all uses is %d\n",
379 commondom->index);
380 }
b5b8b0ac 381
726a989a 382 *togsi = gsi_after_labels (commondom);
b5b8b0ac 383
18965703 384 return true;
ec1e9f7c
DB
385 }
386
f430bae8 387 use = USE_STMT (one_use);
726a989a 388 if (gimple_code (use) != GIMPLE_PHI)
ec1e9f7c 389 {
726a989a 390 sinkbb = gimple_bb (use);
ec1e9f7c
DB
391 if (sinkbb == frombb || sinkbb->loop_depth > frombb->loop_depth
392 || sinkbb->loop_father != frombb->loop_father)
18965703 393 return false;
726a989a 394
791b59e3
WG
395 /* Move the expression to a post dominator can't reduce the number of
396 executions. */
397 if (dominated_by_p (CDI_POST_DOMINATORS, frombb, sinkbb))
398 return false;
399
726a989a 400 *togsi = gsi_for_stmt (use);
b5b8b0ac 401
18965703 402 return true;
ec1e9f7c
DB
403 }
404
405 /* Note that at this point, all uses must be in the same statement, so it
f47c96aa
AM
406 doesn't matter which def op we choose, pick the first one. */
407 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
408 break;
409
ec1e9f7c
DB
410 sinkbb = find_bb_for_arg (use, def);
411 if (!sinkbb)
18965703 412 return false;
ec1e9f7c
DB
413
414 /* This will happen when you have
415 a_3 = PHI <a_13, a_26>
ec1e9f7c 416
b8698a0f
L
417 a_26 = VDEF <a_3>
418
419 If the use is a phi, and is in the same bb as the def,
ec1e9f7c
DB
420 we can't sink it. */
421
726a989a 422 if (gimple_bb (use) == frombb)
18965703 423 return false;
ec1e9f7c
DB
424 if (sinkbb == frombb || sinkbb->loop_depth > frombb->loop_depth
425 || sinkbb->loop_father != frombb->loop_father)
18965703
ZD
426 return false;
427
791b59e3
WG
428 /* Move the expression to a post dominator can't reduce the number of
429 executions. */
430 if (dominated_by_p (CDI_POST_DOMINATORS, frombb, sinkbb))
431 return false;
432
726a989a 433 *togsi = gsi_after_labels (sinkbb);
ec1e9f7c 434
18965703 435 return true;
ec1e9f7c
DB
436}
437
438/* Perform code sinking on BB */
439
440static void
441sink_code_in_bb (basic_block bb)
442{
443 basic_block son;
726a989a 444 gimple_stmt_iterator gsi;
ec1e9f7c
DB
445 edge_iterator ei;
446 edge e;
9a287593 447 bool last = true;
b8698a0f 448
ec1e9f7c
DB
449 /* If this block doesn't dominate anything, there can't be any place to sink
450 the statements to. */
451 if (first_dom_son (CDI_DOMINATORS, bb) == NULL)
452 goto earlyout;
453
454 /* We can't move things across abnormal edges, so don't try. */
455 FOR_EACH_EDGE (e, ei, bb->succs)
456 if (e->flags & EDGE_ABNORMAL)
457 goto earlyout;
458
726a989a 459 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);)
ec1e9f7c 460 {
b8698a0f 461 gimple stmt = gsi_stmt (gsi);
726a989a 462 gimple_stmt_iterator togsi;
18965703 463
726a989a 464 if (!statement_sink_location (stmt, bb, &togsi))
ec1e9f7c 465 {
726a989a
RB
466 if (!gsi_end_p (gsi))
467 gsi_prev (&gsi);
9a287593 468 last = false;
ec1e9f7c 469 continue;
b8698a0f 470 }
ec1e9f7c
DB
471 if (dump_file)
472 {
473 fprintf (dump_file, "Sinking ");
726a989a 474 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS);
ec1e9f7c 475 fprintf (dump_file, " from bb %d to bb %d\n",
726a989a 476 bb->index, (gsi_bb (togsi))->index);
ec1e9f7c 477 }
b8698a0f 478
ec1e9f7c
DB
479 /* If this is the end of the basic block, we need to insert at the end
480 of the basic block. */
726a989a
RB
481 if (gsi_end_p (togsi))
482 gsi_move_to_bb_end (&gsi, gsi_bb (togsi));
ec1e9f7c 483 else
726a989a 484 gsi_move_before (&gsi, &togsi);
ec1e9f7c
DB
485
486 sink_stats.sunk++;
9a287593
AO
487
488 /* If we've just removed the last statement of the BB, the
726a989a 489 gsi_end_p() test below would fail, but gsi_prev() would have
9a287593
AO
490 succeeded, and we want it to succeed. So we keep track of
491 whether we're at the last statement and pick up the new last
492 statement. */
493 if (last)
494 {
726a989a 495 gsi = gsi_last_bb (bb);
9a287593
AO
496 continue;
497 }
498
499 last = false;
726a989a
RB
500 if (!gsi_end_p (gsi))
501 gsi_prev (&gsi);
b8698a0f 502
ec1e9f7c
DB
503 }
504 earlyout:
505 for (son = first_dom_son (CDI_POST_DOMINATORS, bb);
506 son;
507 son = next_dom_son (CDI_POST_DOMINATORS, son))
508 {
509 sink_code_in_bb (son);
510 }
b8698a0f 511}
ec1e9f7c
DB
512
513/* Perform code sinking.
514 This moves code down the flowgraph when we know it would be
515 profitable to do so, or it wouldn't increase the number of
516 executions of the statement.
517
518 IE given
b8698a0f 519
ec1e9f7c
DB
520 a_1 = b + c;
521 if (<something>)
522 {
523 }
524 else
525 {
526 foo (&b, &c);
527 a_5 = b + c;
528 }
529 a_6 = PHI (a_5, a_1);
530 USE a_6.
531
532 we'll transform this into:
533
534 if (<something>)
535 {
536 a_1 = b + c;
537 }
538 else
539 {
540 foo (&b, &c);
541 a_5 = b + c;
542 }
543 a_6 = PHI (a_5, a_1);
544 USE a_6.
545
546 Note that this reduces the number of computations of a = b + c to 1
547 when we take the else edge, instead of 2.
548*/
549static void
550execute_sink_code (void)
551{
598ec7bd 552 loop_optimizer_init (LOOPS_NORMAL);
10d22567 553
ec1e9f7c
DB
554 connect_infinite_loops_to_exit ();
555 memset (&sink_stats, 0, sizeof (sink_stats));
3b5ee6a4
RG
556 calculate_dominance_info (CDI_DOMINATORS);
557 calculate_dominance_info (CDI_POST_DOMINATORS);
b8698a0f 558 sink_code_in_bb (EXIT_BLOCK_PTR);
01902653 559 statistics_counter_event (cfun, "Sunk statements", sink_stats.sunk);
ec1e9f7c
DB
560 free_dominance_info (CDI_POST_DOMINATORS);
561 remove_fake_exit_edges ();
598ec7bd 562 loop_optimizer_finalize ();
ec1e9f7c
DB
563}
564
565/* Gate and execute functions for PRE. */
566
c2924966 567static unsigned int
ec1e9f7c
DB
568do_sink (void)
569{
570 execute_sink_code ();
c2924966 571 return 0;
ec1e9f7c
DB
572}
573
574static bool
575gate_sink (void)
576{
577 return flag_tree_sink != 0;
578}
579
8ddbbcae 580struct gimple_opt_pass pass_sink_code =
ec1e9f7c 581{
8ddbbcae
JH
582 {
583 GIMPLE_PASS,
ec1e9f7c
DB
584 "sink", /* name */
585 gate_sink, /* gate */
586 do_sink, /* execute */
587 NULL, /* sub */
588 NULL, /* next */
589 0, /* static_pass_number */
590 TV_TREE_SINK, /* tv_id */
591 PROP_no_crit_edges | PROP_cfg
4effdf02 592 | PROP_ssa, /* properties_required */
ec1e9f7c
DB
593 0, /* properties_provided */
594 0, /* properties_destroyed */
595 0, /* todo_flags_start */
b8698a0f 596 TODO_update_ssa
0bca51f0
DN
597 | TODO_dump_func
598 | TODO_ggc_collect
8ddbbcae
JH
599 | TODO_verify_ssa /* todo_flags_finish */
600 }
ec1e9f7c 601};