* doc/invoke.texi (Optimize Options): For -fno-toplevel-reorder,
[official-gcc.git] / gcc / tree-ssa-copy.c
blob4ec941bcbf0bbd7e1c1d1232a8dad3e4ff31ffb4
1 /* Copy propagation and SSA_NAME replacement support routines.
2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "flags.h"
26 #include "tm_p.h"
27 #include "basic-block.h"
28 #include "function.h"
29 #include "gimple-pretty-print.h"
30 #include "tree-ssa.h"
31 #include "tree-pass.h"
32 #include "tree-ssa-propagate.h"
33 #include "langhooks.h"
34 #include "cfgloop.h"
35 #include "tree-scalar-evolution.h"
36 #include "tree-ssa-dom.h"
38 /* This file implements the copy propagation pass and provides a
39 handful of interfaces for performing const/copy propagation and
40 simple expression replacement which keep variable annotations
41 up-to-date.
43 We require that for any copy operation where the RHS and LHS have
44 a non-null memory tag the memory tag be the same. It is OK
45 for one or both of the memory tags to be NULL.
47 We also require tracking if a variable is dereferenced in a load or
48 store operation.
50 We enforce these requirements by having all copy propagation and
51 replacements of one SSA_NAME with a different SSA_NAME to use the
52 APIs defined in this file. */
54 /*---------------------------------------------------------------------------
55 Copy propagation
56 ---------------------------------------------------------------------------*/
57 /* Lattice for copy-propagation. The lattice is initialized to
58 UNDEFINED (value == NULL) for SSA names that can become a copy
59 of something or VARYING (value == self) if not (see get_copy_of_val
60 and stmt_may_generate_copy). Other values make the name a COPY
61 of that value.
63 When visiting a statement or PHI node the lattice value for an
64 SSA name can transition from UNDEFINED to COPY to VARYING. */
66 struct prop_value_d {
67 /* Copy-of value. */
68 tree value;
70 typedef struct prop_value_d prop_value_t;
72 static prop_value_t *copy_of;
73 static unsigned n_copy_of;
76 /* Return true if this statement may generate a useful copy. */
78 static bool
79 stmt_may_generate_copy (gimple stmt)
81 if (gimple_code (stmt) == GIMPLE_PHI)
82 return !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_phi_result (stmt));
84 if (gimple_code (stmt) != GIMPLE_ASSIGN)
85 return false;
87 /* If the statement has volatile operands, it won't generate a
88 useful copy. */
89 if (gimple_has_volatile_ops (stmt))
90 return false;
92 /* Statements with loads and/or stores will never generate a useful copy. */
93 if (gimple_vuse (stmt))
94 return false;
96 /* Otherwise, the only statements that generate useful copies are
97 assignments whose RHS is just an SSA name that doesn't flow
98 through abnormal edges. */
99 return ((gimple_assign_rhs_code (stmt) == SSA_NAME
100 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (stmt)))
101 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)));
105 /* Return the copy-of value for VAR. */
107 static inline prop_value_t *
108 get_copy_of_val (tree var)
110 prop_value_t *val = &copy_of[SSA_NAME_VERSION (var)];
112 if (val->value == NULL_TREE
113 && !stmt_may_generate_copy (SSA_NAME_DEF_STMT (var)))
115 /* If the variable will never generate a useful copy relation,
116 make it its own copy. */
117 val->value = var;
120 return val;
123 /* Return the variable VAR is a copy of or VAR if VAR isn't the result
124 of a copy. */
126 static inline tree
127 valueize_val (tree var)
129 if (TREE_CODE (var) == SSA_NAME)
131 tree val = get_copy_of_val (var)->value;
132 if (val)
133 return val;
135 return var;
138 /* Set VAL to be the copy of VAR. If that changed return true. */
140 static inline bool
141 set_copy_of_val (tree var, tree val)
143 unsigned int ver = SSA_NAME_VERSION (var);
144 tree old;
146 /* Set FIRST to be the first link in COPY_OF[DEST]. If that
147 changed, return true. */
148 old = copy_of[ver].value;
149 copy_of[ver].value = val;
151 if (old != val
152 || (val && !operand_equal_p (old, val, 0)))
153 return true;
155 return false;
159 /* Dump the copy-of value for variable VAR to FILE. */
161 static void
162 dump_copy_of (FILE *file, tree var)
164 tree val;
166 print_generic_expr (file, var, dump_flags);
167 if (TREE_CODE (var) != SSA_NAME)
168 return;
170 val = copy_of[SSA_NAME_VERSION (var)].value;
171 fprintf (file, " copy-of chain: ");
172 print_generic_expr (file, var, 0);
173 fprintf (file, " ");
174 if (!val)
175 fprintf (file, "[UNDEFINED]");
176 else if (val == var)
177 fprintf (file, "[NOT A COPY]");
178 else
180 fprintf (file, "-> ");
181 print_generic_expr (file, val, 0);
182 fprintf (file, " ");
183 fprintf (file, "[COPY]");
188 /* Evaluate the RHS of STMT. If it produces a valid copy, set the LHS
189 value and store the LHS into *RESULT_P. */
191 static enum ssa_prop_result
192 copy_prop_visit_assignment (gimple stmt, tree *result_p)
194 tree lhs, rhs;
196 lhs = gimple_assign_lhs (stmt);
197 rhs = valueize_val (gimple_assign_rhs1 (stmt));
199 if (TREE_CODE (lhs) == SSA_NAME)
201 /* Straight copy between two SSA names. First, make sure that
202 we can propagate the RHS into uses of LHS. */
203 if (!may_propagate_copy (lhs, rhs))
204 return SSA_PROP_VARYING;
206 *result_p = lhs;
207 if (set_copy_of_val (*result_p, rhs))
208 return SSA_PROP_INTERESTING;
209 else
210 return SSA_PROP_NOT_INTERESTING;
213 return SSA_PROP_VARYING;
217 /* Visit the GIMPLE_COND STMT. Return SSA_PROP_INTERESTING
218 if it can determine which edge will be taken. Otherwise, return
219 SSA_PROP_VARYING. */
221 static enum ssa_prop_result
222 copy_prop_visit_cond_stmt (gimple stmt, edge *taken_edge_p)
224 enum ssa_prop_result retval = SSA_PROP_VARYING;
225 location_t loc = gimple_location (stmt);
227 tree op0 = gimple_cond_lhs (stmt);
228 tree op1 = gimple_cond_rhs (stmt);
230 /* The only conditionals that we may be able to compute statically
231 are predicates involving two SSA_NAMEs. */
232 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
234 op0 = valueize_val (op0);
235 op1 = valueize_val (op1);
237 /* See if we can determine the predicate's value. */
238 if (dump_file && (dump_flags & TDF_DETAILS))
240 fprintf (dump_file, "Trying to determine truth value of ");
241 fprintf (dump_file, "predicate ");
242 print_gimple_stmt (dump_file, stmt, 0, 0);
245 /* We can fold COND and get a useful result only when we have
246 the same SSA_NAME on both sides of a comparison operator. */
247 if (op0 == op1)
249 tree folded_cond = fold_binary_loc (loc, gimple_cond_code (stmt),
250 boolean_type_node, op0, op1);
251 if (folded_cond)
253 basic_block bb = gimple_bb (stmt);
254 *taken_edge_p = find_taken_edge (bb, folded_cond);
255 if (*taken_edge_p)
256 retval = SSA_PROP_INTERESTING;
261 if (dump_file && (dump_flags & TDF_DETAILS) && *taken_edge_p)
262 fprintf (dump_file, "\nConditional will always take edge %d->%d\n",
263 (*taken_edge_p)->src->index, (*taken_edge_p)->dest->index);
265 return retval;
269 /* Evaluate statement STMT. If the statement produces a new output
270 value, return SSA_PROP_INTERESTING and store the SSA_NAME holding
271 the new value in *RESULT_P.
273 If STMT is a conditional branch and we can determine its truth
274 value, set *TAKEN_EDGE_P accordingly.
276 If the new value produced by STMT is varying, return
277 SSA_PROP_VARYING. */
279 static enum ssa_prop_result
280 copy_prop_visit_stmt (gimple stmt, edge *taken_edge_p, tree *result_p)
282 enum ssa_prop_result retval;
284 if (dump_file && (dump_flags & TDF_DETAILS))
286 fprintf (dump_file, "\nVisiting statement:\n");
287 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
288 fprintf (dump_file, "\n");
291 if (gimple_assign_single_p (stmt)
292 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME
293 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
294 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
296 /* If the statement is a copy assignment, evaluate its RHS to
297 see if the lattice value of its output has changed. */
298 retval = copy_prop_visit_assignment (stmt, result_p);
300 else if (gimple_code (stmt) == GIMPLE_COND)
302 /* See if we can determine which edge goes out of a conditional
303 jump. */
304 retval = copy_prop_visit_cond_stmt (stmt, taken_edge_p);
306 else
307 retval = SSA_PROP_VARYING;
309 if (retval == SSA_PROP_VARYING)
311 tree def;
312 ssa_op_iter i;
314 /* Any other kind of statement is not interesting for constant
315 propagation and, therefore, not worth simulating. */
316 if (dump_file && (dump_flags & TDF_DETAILS))
317 fprintf (dump_file, "No interesting values produced.\n");
319 /* The assignment is not a copy operation. Don't visit this
320 statement again and mark all the definitions in the statement
321 to be copies of nothing. */
322 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_ALL_DEFS)
323 set_copy_of_val (def, def);
326 return retval;
330 /* Visit PHI node PHI. If all the arguments produce the same value,
331 set it to be the value of the LHS of PHI. */
333 static enum ssa_prop_result
334 copy_prop_visit_phi_node (gimple phi)
336 enum ssa_prop_result retval;
337 unsigned i;
338 prop_value_t phi_val = { NULL_TREE };
340 tree lhs = gimple_phi_result (phi);
342 if (dump_file && (dump_flags & TDF_DETAILS))
344 fprintf (dump_file, "\nVisiting PHI node: ");
345 print_gimple_stmt (dump_file, phi, 0, dump_flags);
348 for (i = 0; i < gimple_phi_num_args (phi); i++)
350 prop_value_t *arg_val;
351 tree arg_value;
352 tree arg = gimple_phi_arg_def (phi, i);
353 edge e = gimple_phi_arg_edge (phi, i);
355 /* We don't care about values flowing through non-executable
356 edges. */
357 if (!(e->flags & EDGE_EXECUTABLE))
358 continue;
360 /* Names that flow through abnormal edges cannot be used to
361 derive copies. */
362 if (TREE_CODE (arg) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg))
364 phi_val.value = lhs;
365 break;
368 if (dump_file && (dump_flags & TDF_DETAILS))
370 fprintf (dump_file, "\tArgument #%d: ", i);
371 dump_copy_of (dump_file, arg);
372 fprintf (dump_file, "\n");
375 if (TREE_CODE (arg) == SSA_NAME)
377 arg_val = get_copy_of_val (arg);
379 /* If we didn't visit the definition of arg yet treat it as
380 UNDEFINED. This also handles PHI arguments that are the
381 same as lhs. We'll come here again. */
382 if (!arg_val->value)
383 continue;
385 arg_value = arg_val->value;
387 else
388 arg_value = valueize_val (arg);
390 /* Avoid copy propagation from an inner into an outer loop.
391 Otherwise, this may move loop variant variables outside of
392 their loops and prevent coalescing opportunities. If the
393 value was loop invariant, it will be hoisted by LICM and
394 exposed for copy propagation.
395 ??? The value will be always loop invariant.
396 In loop-closed SSA form do not copy-propagate through
397 PHI nodes in blocks with a loop exit edge predecessor. */
398 if (current_loops
399 && TREE_CODE (arg_value) == SSA_NAME
400 && (loop_depth_of_name (arg_value) > loop_depth_of_name (lhs)
401 || (loops_state_satisfies_p (LOOP_CLOSED_SSA)
402 && loop_exit_edge_p (e->src->loop_father, e))))
404 phi_val.value = lhs;
405 break;
408 /* If the LHS didn't have a value yet, make it a copy of the
409 first argument we find. */
410 if (phi_val.value == NULL_TREE)
412 phi_val.value = arg_value;
413 continue;
416 /* If PHI_VAL and ARG don't have a common copy-of chain, then
417 this PHI node cannot be a copy operation. */
418 if (phi_val.value != arg_value
419 && !operand_equal_p (phi_val.value, arg_value, 0))
421 phi_val.value = lhs;
422 break;
426 if (phi_val.value
427 && may_propagate_copy (lhs, phi_val.value)
428 && set_copy_of_val (lhs, phi_val.value))
429 retval = (phi_val.value != lhs) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
430 else
431 retval = SSA_PROP_NOT_INTERESTING;
433 if (dump_file && (dump_flags & TDF_DETAILS))
435 fprintf (dump_file, "PHI node ");
436 dump_copy_of (dump_file, lhs);
437 fprintf (dump_file, "\nTelling the propagator to ");
438 if (retval == SSA_PROP_INTERESTING)
439 fprintf (dump_file, "add SSA edges out of this PHI and continue.");
440 else if (retval == SSA_PROP_VARYING)
441 fprintf (dump_file, "add SSA edges out of this PHI and never visit again.");
442 else
443 fprintf (dump_file, "do nothing with SSA edges and keep iterating.");
444 fprintf (dump_file, "\n\n");
447 return retval;
451 /* Initialize structures used for copy propagation. */
453 static void
454 init_copy_prop (void)
456 basic_block bb;
458 n_copy_of = num_ssa_names;
459 copy_of = XCNEWVEC (prop_value_t, n_copy_of);
461 FOR_EACH_BB (bb)
463 gimple_stmt_iterator si;
464 int depth = bb_loop_depth (bb);
466 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
468 gimple stmt = gsi_stmt (si);
469 ssa_op_iter iter;
470 tree def;
472 /* The only statements that we care about are those that may
473 generate useful copies. We also need to mark conditional
474 jumps so that their outgoing edges are added to the work
475 lists of the propagator.
477 Avoid copy propagation from an inner into an outer loop.
478 Otherwise, this may move loop variant variables outside of
479 their loops and prevent coalescing opportunities. If the
480 value was loop invariant, it will be hoisted by LICM and
481 exposed for copy propagation.
482 ??? This doesn't make sense. */
483 if (stmt_ends_bb_p (stmt))
484 prop_set_simulate_again (stmt, true);
485 else if (stmt_may_generate_copy (stmt)
486 /* Since we are iterating over the statements in
487 BB, not the phi nodes, STMT will always be an
488 assignment. */
489 && loop_depth_of_name (gimple_assign_rhs1 (stmt)) <= depth)
490 prop_set_simulate_again (stmt, true);
491 else
492 prop_set_simulate_again (stmt, false);
494 /* Mark all the outputs of this statement as not being
495 the copy of anything. */
496 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
497 if (!prop_simulate_again_p (stmt))
498 set_copy_of_val (def, def);
501 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
503 gimple phi = gsi_stmt (si);
504 tree def;
506 def = gimple_phi_result (phi);
507 if (virtual_operand_p (def))
508 prop_set_simulate_again (phi, false);
509 else
510 prop_set_simulate_again (phi, true);
512 if (!prop_simulate_again_p (phi))
513 set_copy_of_val (def, def);
518 /* Callback for substitute_and_fold to get at the final copy-of values. */
520 static tree
521 get_value (tree name)
523 tree val;
524 if (SSA_NAME_VERSION (name) >= n_copy_of)
525 return NULL_TREE;
526 val = copy_of[SSA_NAME_VERSION (name)].value;
527 if (val && val != name)
528 return val;
529 return NULL_TREE;
532 /* Deallocate memory used in copy propagation and do final
533 substitution. */
535 static void
536 fini_copy_prop (void)
538 unsigned i;
540 /* Set the final copy-of value for each variable by traversing the
541 copy-of chains. */
542 for (i = 1; i < num_ssa_names; i++)
544 tree var = ssa_name (i);
545 if (!var
546 || !copy_of[i].value
547 || copy_of[i].value == var)
548 continue;
550 /* In theory the points-to solution of all members of the
551 copy chain is their intersection. For now we do not bother
552 to compute this but only make sure we do not lose points-to
553 information completely by setting the points-to solution
554 of the representative to the first solution we find if
555 it doesn't have one already. */
556 if (copy_of[i].value != var
557 && TREE_CODE (copy_of[i].value) == SSA_NAME)
559 if (POINTER_TYPE_P (TREE_TYPE (var))
560 && SSA_NAME_PTR_INFO (var)
561 && !SSA_NAME_PTR_INFO (copy_of[i].value))
562 duplicate_ssa_name_ptr_info (copy_of[i].value,
563 SSA_NAME_PTR_INFO (var));
564 else if (!POINTER_TYPE_P (TREE_TYPE (var))
565 && SSA_NAME_RANGE_INFO (var)
566 && !SSA_NAME_RANGE_INFO (copy_of[i].value))
567 duplicate_ssa_name_range_info (copy_of[i].value,
568 SSA_NAME_RANGE_INFO (var));
572 /* Don't do DCE if SCEV is initialized. It would destroy the scev cache. */
573 substitute_and_fold (get_value, NULL, !scev_initialized_p ());
575 free (copy_of);
579 /* Main entry point to the copy propagator.
581 PHIS_ONLY is true if we should only consider PHI nodes as generating
582 copy propagation opportunities.
584 The algorithm propagates the value COPY-OF using ssa_propagate. For
585 every variable X_i, COPY-OF(X_i) indicates which variable is X_i created
586 from. The following example shows how the algorithm proceeds at a
587 high level:
589 1 a_24 = x_1
590 2 a_2 = PHI <a_24, x_1>
591 3 a_5 = PHI <a_2>
592 4 x_1 = PHI <x_298, a_5, a_2>
594 The end result should be that a_2, a_5, a_24 and x_1 are a copy of
595 x_298. Propagation proceeds as follows.
597 Visit #1: a_24 is copy-of x_1. Value changed.
598 Visit #2: a_2 is copy-of x_1. Value changed.
599 Visit #3: a_5 is copy-of x_1. Value changed.
600 Visit #4: x_1 is copy-of x_298. Value changed.
601 Visit #1: a_24 is copy-of x_298. Value changed.
602 Visit #2: a_2 is copy-of x_298. Value changed.
603 Visit #3: a_5 is copy-of x_298. Value changed.
604 Visit #4: x_1 is copy-of x_298. Stable state reached.
606 When visiting PHI nodes, we only consider arguments that flow
607 through edges marked executable by the propagation engine. So,
608 when visiting statement #2 for the first time, we will only look at
609 the first argument (a_24) and optimistically assume that its value
610 is the copy of a_24 (x_1). */
612 static unsigned int
613 execute_copy_prop (void)
615 init_copy_prop ();
616 ssa_propagate (copy_prop_visit_stmt, copy_prop_visit_phi_node);
617 fini_copy_prop ();
618 return 0;
621 static bool
622 gate_copy_prop (void)
624 return flag_tree_copy_prop != 0;
627 namespace {
629 const pass_data pass_data_copy_prop =
631 GIMPLE_PASS, /* type */
632 "copyprop", /* name */
633 OPTGROUP_NONE, /* optinfo_flags */
634 true, /* has_gate */
635 true, /* has_execute */
636 TV_TREE_COPY_PROP, /* tv_id */
637 ( PROP_ssa | PROP_cfg ), /* properties_required */
638 0, /* properties_provided */
639 0, /* properties_destroyed */
640 0, /* todo_flags_start */
641 ( TODO_cleanup_cfg | TODO_verify_ssa
642 | TODO_update_ssa ), /* todo_flags_finish */
645 class pass_copy_prop : public gimple_opt_pass
647 public:
648 pass_copy_prop (gcc::context *ctxt)
649 : gimple_opt_pass (pass_data_copy_prop, ctxt)
652 /* opt_pass methods: */
653 opt_pass * clone () { return new pass_copy_prop (m_ctxt); }
654 bool gate () { return gate_copy_prop (); }
655 unsigned int execute () { return execute_copy_prop (); }
657 }; // class pass_copy_prop
659 } // anon namespace
661 gimple_opt_pass *
662 make_pass_copy_prop (gcc::context *ctxt)
664 return new pass_copy_prop (ctxt);