common.opt (fuse-linker-plugin): New option.
[official-gcc.git] / gcc / tree-ssa-sink.c
blobbe3fb7145fdadbe027cb5f9d019b48429ec49be0
1 /* Code sinking for trees
2 Copyright (C) 2001, 2002, 2003, 2004, 2007, 2008, 2009
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 "ggc.h"
27 #include "tree.h"
28 #include "basic-block.h"
29 #include "diagnostic.h"
30 #include "tree-inline.h"
31 #include "tree-flow.h"
32 #include "gimple.h"
33 #include "tree-dump.h"
34 #include "timevar.h"
35 #include "fibheap.h"
36 #include "hashtab.h"
37 #include "tree-iterator.h"
38 #include "real.h"
39 #include "alloc-pool.h"
40 #include "tree-pass.h"
41 #include "flags.h"
42 #include "bitmap.h"
43 #include "langhooks.h"
44 #include "cfgloop.h"
46 /* TODO:
47 1. Sinking store only using scalar promotion (IE without moving the RHS):
49 *q = p;
50 p = p + 1;
51 if (something)
52 *q = <not p>;
53 else
54 y = *q;
57 should become
58 sinktemp = p;
59 p = p + 1;
60 if (something)
61 *q = <not p>;
62 else
64 *q = sinktemp;
65 y = *q
67 Store copy propagation will take care of the store elimination above.
70 2. Sinking using Partial Dead Code Elimination. */
73 static struct
75 /* The number of statements sunk down the flowgraph by code sinking. */
76 int sunk;
78 } sink_stats;
81 /* Given a PHI, and one of its arguments (DEF), find the edge for
82 that argument and return it. If the argument occurs twice in the PHI node,
83 we return NULL. */
85 static basic_block
86 find_bb_for_arg (gimple phi, tree def)
88 size_t i;
89 bool foundone = false;
90 basic_block result = NULL;
91 for (i = 0; i < gimple_phi_num_args (phi); i++)
92 if (PHI_ARG_DEF (phi, i) == def)
94 if (foundone)
95 return NULL;
96 foundone = true;
97 result = gimple_phi_arg_edge (phi, i)->src;
99 return result;
102 /* When the first immediate use is in a statement, then return true if all
103 immediate uses in IMM are in the same statement.
104 We could also do the case where the first immediate use is in a phi node,
105 and all the other uses are in phis in the same basic block, but this
106 requires some expensive checking later (you have to make sure no def/vdef
107 in the statement occurs for multiple edges in the various phi nodes it's
108 used in, so that you only have one place you can sink it to. */
110 static bool
111 all_immediate_uses_same_place (gimple stmt)
113 gimple firstuse = NULL;
114 ssa_op_iter op_iter;
115 imm_use_iterator imm_iter;
116 use_operand_p use_p;
117 tree var;
119 FOR_EACH_SSA_TREE_OPERAND (var, stmt, op_iter, SSA_OP_ALL_DEFS)
121 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, var)
123 if (is_gimple_debug (USE_STMT (use_p)))
124 continue;
125 if (firstuse == NULL)
126 firstuse = USE_STMT (use_p);
127 else
128 if (firstuse != USE_STMT (use_p))
129 return false;
133 return true;
136 /* Some global stores don't necessarily have VDEF's of global variables,
137 but we still must avoid moving them around. */
139 bool
140 is_hidden_global_store (gimple stmt)
142 /* Check virtual definitions. If we get here, the only virtual
143 definitions we should see are those generated by assignment or call
144 statements. */
145 if (gimple_vdef (stmt))
147 tree lhs;
149 gcc_assert (is_gimple_assign (stmt) || is_gimple_call (stmt));
151 /* Note that we must not check the individual virtual operands
152 here. In particular, if this is an aliased store, we could
153 end up with something like the following (SSA notation
154 redacted for brevity):
156 foo (int *p, int i)
158 int x;
159 p_1 = (i_2 > 3) ? &x : p;
161 # x_4 = VDEF <x_3>
162 *p_1 = 5;
164 return 2;
167 Notice that the store to '*p_1' should be preserved, if we
168 were to check the virtual definitions in that store, we would
169 not mark it needed. This is because 'x' is not a global
170 variable.
172 Therefore, we check the base address of the LHS. If the
173 address is a pointer, we check if its name tag or symbol tag is
174 a global variable. Otherwise, we check if the base variable
175 is a global. */
176 lhs = gimple_get_lhs (stmt);
178 if (REFERENCE_CLASS_P (lhs))
179 lhs = get_base_address (lhs);
181 if (lhs == NULL_TREE)
183 /* If LHS is NULL, it means that we couldn't get the base
184 address of the reference. In which case, we should not
185 move this store. */
186 return true;
188 else if (DECL_P (lhs))
190 /* If the store is to a global symbol, we need to keep it. */
191 if (is_global_var (lhs))
192 return true;
195 else if (INDIRECT_REF_P (lhs))
196 return ptr_deref_may_alias_global_p (TREE_OPERAND (lhs, 0));
197 else
198 gcc_unreachable ();
201 return false;
204 /* Find the nearest common dominator of all of the immediate uses in IMM. */
206 static basic_block
207 nearest_common_dominator_of_uses (gimple stmt, bool *debug_stmts)
209 bitmap blocks = BITMAP_ALLOC (NULL);
210 basic_block commondom;
211 unsigned int j;
212 bitmap_iterator bi;
213 ssa_op_iter op_iter;
214 imm_use_iterator imm_iter;
215 use_operand_p use_p;
216 tree var;
218 bitmap_clear (blocks);
219 FOR_EACH_SSA_TREE_OPERAND (var, stmt, op_iter, SSA_OP_ALL_DEFS)
221 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, var)
223 gimple usestmt = USE_STMT (use_p);
224 basic_block useblock;
226 if (gimple_code (usestmt) == GIMPLE_PHI)
228 int idx = PHI_ARG_INDEX_FROM_USE (use_p);
230 useblock = gimple_phi_arg_edge (usestmt, idx)->src;
232 else if (is_gimple_debug (usestmt))
234 *debug_stmts = true;
235 continue;
237 else
239 useblock = gimple_bb (usestmt);
242 /* Short circuit. Nothing dominates the entry block. */
243 if (useblock == ENTRY_BLOCK_PTR)
245 BITMAP_FREE (blocks);
246 return NULL;
248 bitmap_set_bit (blocks, useblock->index);
251 commondom = BASIC_BLOCK (bitmap_first_set_bit (blocks));
252 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, j, bi)
253 commondom = nearest_common_dominator (CDI_DOMINATORS, commondom,
254 BASIC_BLOCK (j));
255 BITMAP_FREE (blocks);
256 return commondom;
259 /* Given a statement (STMT) and the basic block it is currently in (FROMBB),
260 determine the location to sink the statement to, if any.
261 Returns true if there is such location; in that case, TOGSI points to the
262 statement before that STMT should be moved. */
264 static bool
265 statement_sink_location (gimple stmt, basic_block frombb,
266 gimple_stmt_iterator *togsi)
268 gimple use;
269 tree def;
270 use_operand_p one_use = NULL_USE_OPERAND_P;
271 basic_block sinkbb;
272 use_operand_p use_p;
273 def_operand_p def_p;
274 ssa_op_iter iter;
275 imm_use_iterator imm_iter;
276 enum tree_code code;
278 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
280 FOR_EACH_IMM_USE_FAST (one_use, imm_iter, def)
282 if (is_gimple_debug (USE_STMT (one_use)))
283 continue;
285 break;
287 if (one_use != NULL_USE_OPERAND_P)
288 break;
291 /* Return if there are no immediate uses of this stmt. */
292 if (one_use == NULL_USE_OPERAND_P)
293 return false;
295 if (gimple_code (stmt) != GIMPLE_ASSIGN)
296 return false;
298 /* There are a few classes of things we can't or don't move, some because we
299 don't have code to handle it, some because it's not profitable and some
300 because it's not legal.
302 We can't sink things that may be global stores, at least not without
303 calculating a lot more information, because we may cause it to no longer
304 be seen by an external routine that needs it depending on where it gets
305 moved to.
307 We don't want to sink loads from memory.
309 We can't sink statements that end basic blocks without splitting the
310 incoming edge for the sink location to place it there.
312 We can't sink statements that have volatile operands.
314 We don't want to sink dead code, so anything with 0 immediate uses is not
315 sunk.
317 Don't sink BLKmode assignments if current function has any local explicit
318 register variables, as BLKmode assignments may involve memcpy or memset
319 calls or, on some targets, inline expansion thereof that sometimes need
320 to use specific hard registers.
323 code = gimple_assign_rhs_code (stmt);
324 if (stmt_ends_bb_p (stmt)
325 || gimple_has_side_effects (stmt)
326 || is_hidden_global_store (stmt)
327 || gimple_has_volatile_ops (stmt)
328 || gimple_vuse (stmt)
329 || (cfun->has_local_explicit_reg_vars
330 && TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt))) == BLKmode))
331 return false;
333 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
335 tree def = DEF_FROM_PTR (def_p);
336 if (is_global_var (SSA_NAME_VAR (def))
337 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
338 return false;
341 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
343 tree use = USE_FROM_PTR (use_p);
344 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use))
345 return false;
348 /* If all the immediate uses are not in the same place, find the nearest
349 common dominator of all the immediate uses. For PHI nodes, we have to
350 find the nearest common dominator of all of the predecessor blocks, since
351 that is where insertion would have to take place. */
352 if (!all_immediate_uses_same_place (stmt))
354 bool debug_stmts = false;
355 basic_block commondom = nearest_common_dominator_of_uses (stmt,
356 &debug_stmts);
358 if (commondom == frombb)
359 return false;
361 /* Our common dominator has to be dominated by frombb in order to be a
362 trivially safe place to put this statement, since it has multiple
363 uses. */
364 if (!dominated_by_p (CDI_DOMINATORS, commondom, frombb))
365 return false;
367 /* It doesn't make sense to move to a dominator that post-dominates
368 frombb, because it means we've just moved it into a path that always
369 executes if frombb executes, instead of reducing the number of
370 executions . */
371 if (dominated_by_p (CDI_POST_DOMINATORS, frombb, commondom))
373 if (dump_file && (dump_flags & TDF_DETAILS))
374 fprintf (dump_file, "Not moving store, common dominator post-dominates from block.\n");
375 return false;
378 if (commondom == frombb || commondom->loop_depth > frombb->loop_depth)
379 return false;
380 if (dump_file && (dump_flags & TDF_DETAILS))
382 fprintf (dump_file, "Common dominator of all uses is %d\n",
383 commondom->index);
386 *togsi = gsi_after_labels (commondom);
388 return true;
391 use = USE_STMT (one_use);
392 if (gimple_code (use) != GIMPLE_PHI)
394 sinkbb = gimple_bb (use);
395 if (sinkbb == frombb || sinkbb->loop_depth > frombb->loop_depth
396 || sinkbb->loop_father != frombb->loop_father)
397 return false;
399 /* Move the expression to a post dominator can't reduce the number of
400 executions. */
401 if (dominated_by_p (CDI_POST_DOMINATORS, frombb, sinkbb))
402 return false;
404 *togsi = gsi_for_stmt (use);
406 return true;
409 /* Note that at this point, all uses must be in the same statement, so it
410 doesn't matter which def op we choose, pick the first one. */
411 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
412 break;
414 sinkbb = find_bb_for_arg (use, def);
415 if (!sinkbb)
416 return false;
418 /* This will happen when you have
419 a_3 = PHI <a_13, a_26>
421 a_26 = VDEF <a_3>
423 If the use is a phi, and is in the same bb as the def,
424 we can't sink it. */
426 if (gimple_bb (use) == frombb)
427 return false;
428 if (sinkbb == frombb || sinkbb->loop_depth > frombb->loop_depth
429 || sinkbb->loop_father != frombb->loop_father)
430 return false;
432 /* Move the expression to a post dominator can't reduce the number of
433 executions. */
434 if (dominated_by_p (CDI_POST_DOMINATORS, frombb, sinkbb))
435 return false;
437 *togsi = gsi_after_labels (sinkbb);
439 return true;
442 /* Perform code sinking on BB */
444 static void
445 sink_code_in_bb (basic_block bb)
447 basic_block son;
448 gimple_stmt_iterator gsi;
449 edge_iterator ei;
450 edge e;
451 bool last = true;
453 /* If this block doesn't dominate anything, there can't be any place to sink
454 the statements to. */
455 if (first_dom_son (CDI_DOMINATORS, bb) == NULL)
456 goto earlyout;
458 /* We can't move things across abnormal edges, so don't try. */
459 FOR_EACH_EDGE (e, ei, bb->succs)
460 if (e->flags & EDGE_ABNORMAL)
461 goto earlyout;
463 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);)
465 gimple stmt = gsi_stmt (gsi);
466 gimple_stmt_iterator togsi;
468 if (!statement_sink_location (stmt, bb, &togsi))
470 if (!gsi_end_p (gsi))
471 gsi_prev (&gsi);
472 last = false;
473 continue;
475 if (dump_file)
477 fprintf (dump_file, "Sinking ");
478 print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS);
479 fprintf (dump_file, " from bb %d to bb %d\n",
480 bb->index, (gsi_bb (togsi))->index);
483 /* If this is the end of the basic block, we need to insert at the end
484 of the basic block. */
485 if (gsi_end_p (togsi))
486 gsi_move_to_bb_end (&gsi, gsi_bb (togsi));
487 else
488 gsi_move_before (&gsi, &togsi);
490 sink_stats.sunk++;
492 /* If we've just removed the last statement of the BB, the
493 gsi_end_p() test below would fail, but gsi_prev() would have
494 succeeded, and we want it to succeed. So we keep track of
495 whether we're at the last statement and pick up the new last
496 statement. */
497 if (last)
499 gsi = gsi_last_bb (bb);
500 continue;
503 last = false;
504 if (!gsi_end_p (gsi))
505 gsi_prev (&gsi);
508 earlyout:
509 for (son = first_dom_son (CDI_POST_DOMINATORS, bb);
510 son;
511 son = next_dom_son (CDI_POST_DOMINATORS, son))
513 sink_code_in_bb (son);
517 /* Perform code sinking.
518 This moves code down the flowgraph when we know it would be
519 profitable to do so, or it wouldn't increase the number of
520 executions of the statement.
522 IE given
524 a_1 = b + c;
525 if (<something>)
528 else
530 foo (&b, &c);
531 a_5 = b + c;
533 a_6 = PHI (a_5, a_1);
534 USE a_6.
536 we'll transform this into:
538 if (<something>)
540 a_1 = b + c;
542 else
544 foo (&b, &c);
545 a_5 = b + c;
547 a_6 = PHI (a_5, a_1);
548 USE a_6.
550 Note that this reduces the number of computations of a = b + c to 1
551 when we take the else edge, instead of 2.
553 static void
554 execute_sink_code (void)
556 loop_optimizer_init (LOOPS_NORMAL);
558 connect_infinite_loops_to_exit ();
559 memset (&sink_stats, 0, sizeof (sink_stats));
560 calculate_dominance_info (CDI_DOMINATORS);
561 calculate_dominance_info (CDI_POST_DOMINATORS);
562 sink_code_in_bb (EXIT_BLOCK_PTR);
563 statistics_counter_event (cfun, "Sunk statements", sink_stats.sunk);
564 free_dominance_info (CDI_POST_DOMINATORS);
565 remove_fake_exit_edges ();
566 loop_optimizer_finalize ();
569 /* Gate and execute functions for PRE. */
571 static unsigned int
572 do_sink (void)
574 execute_sink_code ();
575 return 0;
578 static bool
579 gate_sink (void)
581 return flag_tree_sink != 0;
584 struct gimple_opt_pass pass_sink_code =
587 GIMPLE_PASS,
588 "sink", /* name */
589 gate_sink, /* gate */
590 do_sink, /* execute */
591 NULL, /* sub */
592 NULL, /* next */
593 0, /* static_pass_number */
594 TV_TREE_SINK, /* tv_id */
595 PROP_no_crit_edges | PROP_cfg
596 | PROP_ssa, /* properties_required */
597 0, /* properties_provided */
598 0, /* properties_destroyed */
599 0, /* todo_flags_start */
600 TODO_update_ssa
601 | TODO_dump_func
602 | TODO_ggc_collect
603 | TODO_verify_ssa /* todo_flags_finish */