* include/bits/regex_compiler.h (__detail::__has_contiguous_iter):
[official-gcc.git] / gcc / tree-ssa-copy.c
blob15c6896e945188383fee0f0ce738323e09c88c74
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 "gimple.h"
31 #include "gimple-ssa.h"
32 #include "tree-cfg.h"
33 #include "tree-phinodes.h"
34 #include "ssa-iterators.h"
35 #include "tree-ssanames.h"
36 #include "tree-pass.h"
37 #include "tree-ssa-propagate.h"
38 #include "langhooks.h"
39 #include "cfgloop.h"
40 #include "tree-scalar-evolution.h"
41 #include "tree-ssa-dom.h"
43 /* This file implements the copy propagation pass and provides a
44 handful of interfaces for performing const/copy propagation and
45 simple expression replacement which keep variable annotations
46 up-to-date.
48 We require that for any copy operation where the RHS and LHS have
49 a non-null memory tag the memory tag be the same. It is OK
50 for one or both of the memory tags to be NULL.
52 We also require tracking if a variable is dereferenced in a load or
53 store operation.
55 We enforce these requirements by having all copy propagation and
56 replacements of one SSA_NAME with a different SSA_NAME to use the
57 APIs defined in this file. */
59 /*---------------------------------------------------------------------------
60 Copy propagation
61 ---------------------------------------------------------------------------*/
62 /* Lattice for copy-propagation. The lattice is initialized to
63 UNDEFINED (value == NULL) for SSA names that can become a copy
64 of something or VARYING (value == self) if not (see get_copy_of_val
65 and stmt_may_generate_copy). Other values make the name a COPY
66 of that value.
68 When visiting a statement or PHI node the lattice value for an
69 SSA name can transition from UNDEFINED to COPY to VARYING. */
71 struct prop_value_d {
72 /* Copy-of value. */
73 tree value;
75 typedef struct prop_value_d prop_value_t;
77 static prop_value_t *copy_of;
78 static unsigned n_copy_of;
81 /* Return true if this statement may generate a useful copy. */
83 static bool
84 stmt_may_generate_copy (gimple stmt)
86 if (gimple_code (stmt) == GIMPLE_PHI)
87 return !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_phi_result (stmt));
89 if (gimple_code (stmt) != GIMPLE_ASSIGN)
90 return false;
92 /* If the statement has volatile operands, it won't generate a
93 useful copy. */
94 if (gimple_has_volatile_ops (stmt))
95 return false;
97 /* Statements with loads and/or stores will never generate a useful copy. */
98 if (gimple_vuse (stmt))
99 return false;
101 /* Otherwise, the only statements that generate useful copies are
102 assignments whose RHS is just an SSA name that doesn't flow
103 through abnormal edges. */
104 return ((gimple_assign_rhs_code (stmt) == SSA_NAME
105 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (stmt)))
106 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)));
110 /* Return the copy-of value for VAR. */
112 static inline prop_value_t *
113 get_copy_of_val (tree var)
115 prop_value_t *val = &copy_of[SSA_NAME_VERSION (var)];
117 if (val->value == NULL_TREE
118 && !stmt_may_generate_copy (SSA_NAME_DEF_STMT (var)))
120 /* If the variable will never generate a useful copy relation,
121 make it its own copy. */
122 val->value = var;
125 return val;
128 /* Return the variable VAR is a copy of or VAR if VAR isn't the result
129 of a copy. */
131 static inline tree
132 valueize_val (tree var)
134 if (TREE_CODE (var) == SSA_NAME)
136 tree val = get_copy_of_val (var)->value;
137 if (val)
138 return val;
140 return var;
143 /* Set VAL to be the copy of VAR. If that changed return true. */
145 static inline bool
146 set_copy_of_val (tree var, tree val)
148 unsigned int ver = SSA_NAME_VERSION (var);
149 tree old;
151 /* Set FIRST to be the first link in COPY_OF[DEST]. If that
152 changed, return true. */
153 old = copy_of[ver].value;
154 copy_of[ver].value = val;
156 if (old != val
157 || (val && !operand_equal_p (old, val, 0)))
158 return true;
160 return false;
164 /* Dump the copy-of value for variable VAR to FILE. */
166 static void
167 dump_copy_of (FILE *file, tree var)
169 tree val;
171 print_generic_expr (file, var, dump_flags);
172 if (TREE_CODE (var) != SSA_NAME)
173 return;
175 val = copy_of[SSA_NAME_VERSION (var)].value;
176 fprintf (file, " copy-of chain: ");
177 print_generic_expr (file, var, 0);
178 fprintf (file, " ");
179 if (!val)
180 fprintf (file, "[UNDEFINED]");
181 else if (val == var)
182 fprintf (file, "[NOT A COPY]");
183 else
185 fprintf (file, "-> ");
186 print_generic_expr (file, val, 0);
187 fprintf (file, " ");
188 fprintf (file, "[COPY]");
193 /* Evaluate the RHS of STMT. If it produces a valid copy, set the LHS
194 value and store the LHS into *RESULT_P. */
196 static enum ssa_prop_result
197 copy_prop_visit_assignment (gimple stmt, tree *result_p)
199 tree lhs, rhs;
201 lhs = gimple_assign_lhs (stmt);
202 rhs = valueize_val (gimple_assign_rhs1 (stmt));
204 if (TREE_CODE (lhs) == SSA_NAME)
206 /* Straight copy between two SSA names. First, make sure that
207 we can propagate the RHS into uses of LHS. */
208 if (!may_propagate_copy (lhs, rhs))
209 return SSA_PROP_VARYING;
211 *result_p = lhs;
212 if (set_copy_of_val (*result_p, rhs))
213 return SSA_PROP_INTERESTING;
214 else
215 return SSA_PROP_NOT_INTERESTING;
218 return SSA_PROP_VARYING;
222 /* Visit the GIMPLE_COND STMT. Return SSA_PROP_INTERESTING
223 if it can determine which edge will be taken. Otherwise, return
224 SSA_PROP_VARYING. */
226 static enum ssa_prop_result
227 copy_prop_visit_cond_stmt (gimple stmt, edge *taken_edge_p)
229 enum ssa_prop_result retval = SSA_PROP_VARYING;
230 location_t loc = gimple_location (stmt);
232 tree op0 = gimple_cond_lhs (stmt);
233 tree op1 = gimple_cond_rhs (stmt);
235 /* The only conditionals that we may be able to compute statically
236 are predicates involving two SSA_NAMEs. */
237 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
239 op0 = valueize_val (op0);
240 op1 = valueize_val (op1);
242 /* See if we can determine the predicate's value. */
243 if (dump_file && (dump_flags & TDF_DETAILS))
245 fprintf (dump_file, "Trying to determine truth value of ");
246 fprintf (dump_file, "predicate ");
247 print_gimple_stmt (dump_file, stmt, 0, 0);
250 /* We can fold COND and get a useful result only when we have
251 the same SSA_NAME on both sides of a comparison operator. */
252 if (op0 == op1)
254 tree folded_cond = fold_binary_loc (loc, gimple_cond_code (stmt),
255 boolean_type_node, op0, op1);
256 if (folded_cond)
258 basic_block bb = gimple_bb (stmt);
259 *taken_edge_p = find_taken_edge (bb, folded_cond);
260 if (*taken_edge_p)
261 retval = SSA_PROP_INTERESTING;
266 if (dump_file && (dump_flags & TDF_DETAILS) && *taken_edge_p)
267 fprintf (dump_file, "\nConditional will always take edge %d->%d\n",
268 (*taken_edge_p)->src->index, (*taken_edge_p)->dest->index);
270 return retval;
274 /* Evaluate statement STMT. If the statement produces a new output
275 value, return SSA_PROP_INTERESTING and store the SSA_NAME holding
276 the new value in *RESULT_P.
278 If STMT is a conditional branch and we can determine its truth
279 value, set *TAKEN_EDGE_P accordingly.
281 If the new value produced by STMT is varying, return
282 SSA_PROP_VARYING. */
284 static enum ssa_prop_result
285 copy_prop_visit_stmt (gimple stmt, edge *taken_edge_p, tree *result_p)
287 enum ssa_prop_result retval;
289 if (dump_file && (dump_flags & TDF_DETAILS))
291 fprintf (dump_file, "\nVisiting statement:\n");
292 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
293 fprintf (dump_file, "\n");
296 if (gimple_assign_single_p (stmt)
297 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME
298 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
299 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
301 /* If the statement is a copy assignment, evaluate its RHS to
302 see if the lattice value of its output has changed. */
303 retval = copy_prop_visit_assignment (stmt, result_p);
305 else if (gimple_code (stmt) == GIMPLE_COND)
307 /* See if we can determine which edge goes out of a conditional
308 jump. */
309 retval = copy_prop_visit_cond_stmt (stmt, taken_edge_p);
311 else
312 retval = SSA_PROP_VARYING;
314 if (retval == SSA_PROP_VARYING)
316 tree def;
317 ssa_op_iter i;
319 /* Any other kind of statement is not interesting for constant
320 propagation and, therefore, not worth simulating. */
321 if (dump_file && (dump_flags & TDF_DETAILS))
322 fprintf (dump_file, "No interesting values produced.\n");
324 /* The assignment is not a copy operation. Don't visit this
325 statement again and mark all the definitions in the statement
326 to be copies of nothing. */
327 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_ALL_DEFS)
328 set_copy_of_val (def, def);
331 return retval;
335 /* Visit PHI node PHI. If all the arguments produce the same value,
336 set it to be the value of the LHS of PHI. */
338 static enum ssa_prop_result
339 copy_prop_visit_phi_node (gimple phi)
341 enum ssa_prop_result retval;
342 unsigned i;
343 prop_value_t phi_val = { NULL_TREE };
345 tree lhs = gimple_phi_result (phi);
347 if (dump_file && (dump_flags & TDF_DETAILS))
349 fprintf (dump_file, "\nVisiting PHI node: ");
350 print_gimple_stmt (dump_file, phi, 0, dump_flags);
353 for (i = 0; i < gimple_phi_num_args (phi); i++)
355 prop_value_t *arg_val;
356 tree arg_value;
357 tree arg = gimple_phi_arg_def (phi, i);
358 edge e = gimple_phi_arg_edge (phi, i);
360 /* We don't care about values flowing through non-executable
361 edges. */
362 if (!(e->flags & EDGE_EXECUTABLE))
363 continue;
365 /* Names that flow through abnormal edges cannot be used to
366 derive copies. */
367 if (TREE_CODE (arg) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg))
369 phi_val.value = lhs;
370 break;
373 if (dump_file && (dump_flags & TDF_DETAILS))
375 fprintf (dump_file, "\tArgument #%d: ", i);
376 dump_copy_of (dump_file, arg);
377 fprintf (dump_file, "\n");
380 if (TREE_CODE (arg) == SSA_NAME)
382 arg_val = get_copy_of_val (arg);
384 /* If we didn't visit the definition of arg yet treat it as
385 UNDEFINED. This also handles PHI arguments that are the
386 same as lhs. We'll come here again. */
387 if (!arg_val->value)
388 continue;
390 arg_value = arg_val->value;
392 else
393 arg_value = valueize_val (arg);
395 /* Avoid copy propagation from an inner into an outer loop.
396 Otherwise, this may move loop variant variables outside of
397 their loops and prevent coalescing opportunities. If the
398 value was loop invariant, it will be hoisted by LICM and
399 exposed for copy propagation.
400 ??? The value will be always loop invariant.
401 In loop-closed SSA form do not copy-propagate through
402 PHI nodes in blocks with a loop exit edge predecessor. */
403 if (current_loops
404 && TREE_CODE (arg_value) == SSA_NAME
405 && (loop_depth_of_name (arg_value) > loop_depth_of_name (lhs)
406 || (loops_state_satisfies_p (LOOP_CLOSED_SSA)
407 && loop_exit_edge_p (e->src->loop_father, e))))
409 phi_val.value = lhs;
410 break;
413 /* If the LHS didn't have a value yet, make it a copy of the
414 first argument we find. */
415 if (phi_val.value == NULL_TREE)
417 phi_val.value = arg_value;
418 continue;
421 /* If PHI_VAL and ARG don't have a common copy-of chain, then
422 this PHI node cannot be a copy operation. */
423 if (phi_val.value != arg_value
424 && !operand_equal_p (phi_val.value, arg_value, 0))
426 phi_val.value = lhs;
427 break;
431 if (phi_val.value
432 && may_propagate_copy (lhs, phi_val.value)
433 && set_copy_of_val (lhs, phi_val.value))
434 retval = (phi_val.value != lhs) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
435 else
436 retval = SSA_PROP_NOT_INTERESTING;
438 if (dump_file && (dump_flags & TDF_DETAILS))
440 fprintf (dump_file, "PHI node ");
441 dump_copy_of (dump_file, lhs);
442 fprintf (dump_file, "\nTelling the propagator to ");
443 if (retval == SSA_PROP_INTERESTING)
444 fprintf (dump_file, "add SSA edges out of this PHI and continue.");
445 else if (retval == SSA_PROP_VARYING)
446 fprintf (dump_file, "add SSA edges out of this PHI and never visit again.");
447 else
448 fprintf (dump_file, "do nothing with SSA edges and keep iterating.");
449 fprintf (dump_file, "\n\n");
452 return retval;
456 /* Initialize structures used for copy propagation. */
458 static void
459 init_copy_prop (void)
461 basic_block bb;
463 n_copy_of = num_ssa_names;
464 copy_of = XCNEWVEC (prop_value_t, n_copy_of);
466 FOR_EACH_BB (bb)
468 gimple_stmt_iterator si;
469 int depth = bb_loop_depth (bb);
471 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
473 gimple stmt = gsi_stmt (si);
474 ssa_op_iter iter;
475 tree def;
477 /* The only statements that we care about are those that may
478 generate useful copies. We also need to mark conditional
479 jumps so that their outgoing edges are added to the work
480 lists of the propagator.
482 Avoid copy propagation from an inner into an outer loop.
483 Otherwise, this may move loop variant variables outside of
484 their loops and prevent coalescing opportunities. If the
485 value was loop invariant, it will be hoisted by LICM and
486 exposed for copy propagation.
487 ??? This doesn't make sense. */
488 if (stmt_ends_bb_p (stmt))
489 prop_set_simulate_again (stmt, true);
490 else if (stmt_may_generate_copy (stmt)
491 /* Since we are iterating over the statements in
492 BB, not the phi nodes, STMT will always be an
493 assignment. */
494 && loop_depth_of_name (gimple_assign_rhs1 (stmt)) <= depth)
495 prop_set_simulate_again (stmt, true);
496 else
497 prop_set_simulate_again (stmt, false);
499 /* Mark all the outputs of this statement as not being
500 the copy of anything. */
501 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
502 if (!prop_simulate_again_p (stmt))
503 set_copy_of_val (def, def);
506 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
508 gimple phi = gsi_stmt (si);
509 tree def;
511 def = gimple_phi_result (phi);
512 if (virtual_operand_p (def))
513 prop_set_simulate_again (phi, false);
514 else
515 prop_set_simulate_again (phi, true);
517 if (!prop_simulate_again_p (phi))
518 set_copy_of_val (def, def);
523 /* Callback for substitute_and_fold to get at the final copy-of values. */
525 static tree
526 get_value (tree name)
528 tree val;
529 if (SSA_NAME_VERSION (name) >= n_copy_of)
530 return NULL_TREE;
531 val = copy_of[SSA_NAME_VERSION (name)].value;
532 if (val && val != name)
533 return val;
534 return NULL_TREE;
537 /* Deallocate memory used in copy propagation and do final
538 substitution. */
540 static void
541 fini_copy_prop (void)
543 unsigned i;
545 /* Set the final copy-of value for each variable by traversing the
546 copy-of chains. */
547 for (i = 1; i < num_ssa_names; i++)
549 tree var = ssa_name (i);
550 if (!var
551 || !copy_of[i].value
552 || copy_of[i].value == var)
553 continue;
555 /* In theory the points-to solution of all members of the
556 copy chain is their intersection. For now we do not bother
557 to compute this but only make sure we do not lose points-to
558 information completely by setting the points-to solution
559 of the representative to the first solution we find if
560 it doesn't have one already. */
561 if (copy_of[i].value != var
562 && TREE_CODE (copy_of[i].value) == SSA_NAME)
564 if (POINTER_TYPE_P (TREE_TYPE (var))
565 && SSA_NAME_PTR_INFO (var)
566 && !SSA_NAME_PTR_INFO (copy_of[i].value))
567 duplicate_ssa_name_ptr_info (copy_of[i].value,
568 SSA_NAME_PTR_INFO (var));
569 else if (!POINTER_TYPE_P (TREE_TYPE (var))
570 && SSA_NAME_RANGE_INFO (var)
571 && !SSA_NAME_RANGE_INFO (copy_of[i].value))
572 duplicate_ssa_name_range_info (copy_of[i].value,
573 SSA_NAME_RANGE_INFO (var));
577 /* Don't do DCE if SCEV is initialized. It would destroy the scev cache. */
578 substitute_and_fold (get_value, NULL, !scev_initialized_p ());
580 free (copy_of);
584 /* Main entry point to the copy propagator.
586 PHIS_ONLY is true if we should only consider PHI nodes as generating
587 copy propagation opportunities.
589 The algorithm propagates the value COPY-OF using ssa_propagate. For
590 every variable X_i, COPY-OF(X_i) indicates which variable is X_i created
591 from. The following example shows how the algorithm proceeds at a
592 high level:
594 1 a_24 = x_1
595 2 a_2 = PHI <a_24, x_1>
596 3 a_5 = PHI <a_2>
597 4 x_1 = PHI <x_298, a_5, a_2>
599 The end result should be that a_2, a_5, a_24 and x_1 are a copy of
600 x_298. Propagation proceeds as follows.
602 Visit #1: a_24 is copy-of x_1. Value changed.
603 Visit #2: a_2 is copy-of x_1. Value changed.
604 Visit #3: a_5 is copy-of x_1. Value changed.
605 Visit #4: x_1 is copy-of x_298. Value changed.
606 Visit #1: a_24 is copy-of x_298. Value changed.
607 Visit #2: a_2 is copy-of x_298. Value changed.
608 Visit #3: a_5 is copy-of x_298. Value changed.
609 Visit #4: x_1 is copy-of x_298. Stable state reached.
611 When visiting PHI nodes, we only consider arguments that flow
612 through edges marked executable by the propagation engine. So,
613 when visiting statement #2 for the first time, we will only look at
614 the first argument (a_24) and optimistically assume that its value
615 is the copy of a_24 (x_1). */
617 static unsigned int
618 execute_copy_prop (void)
620 init_copy_prop ();
621 ssa_propagate (copy_prop_visit_stmt, copy_prop_visit_phi_node);
622 fini_copy_prop ();
623 return 0;
626 static bool
627 gate_copy_prop (void)
629 return flag_tree_copy_prop != 0;
632 namespace {
634 const pass_data pass_data_copy_prop =
636 GIMPLE_PASS, /* type */
637 "copyprop", /* name */
638 OPTGROUP_NONE, /* optinfo_flags */
639 true, /* has_gate */
640 true, /* has_execute */
641 TV_TREE_COPY_PROP, /* tv_id */
642 ( PROP_ssa | PROP_cfg ), /* properties_required */
643 0, /* properties_provided */
644 0, /* properties_destroyed */
645 0, /* todo_flags_start */
646 ( TODO_cleanup_cfg | TODO_verify_ssa
647 | TODO_update_ssa ), /* todo_flags_finish */
650 class pass_copy_prop : public gimple_opt_pass
652 public:
653 pass_copy_prop (gcc::context *ctxt)
654 : gimple_opt_pass (pass_data_copy_prop, ctxt)
657 /* opt_pass methods: */
658 opt_pass * clone () { return new pass_copy_prop (m_ctxt); }
659 bool gate () { return gate_copy_prop (); }
660 unsigned int execute () { return execute_copy_prop (); }
662 }; // class pass_copy_prop
664 } // anon namespace
666 gimple_opt_pass *
667 make_pass_copy_prop (gcc::context *ctxt)
669 return new pass_copy_prop (ctxt);