* arm.c (FL_WBUF): Define.
[official-gcc.git] / gcc / tree-ssa-copy.c
blobbaca99c061cb6da6cc859bfde87458f9ee724cf1
1 /* Copy propagation and SSA_NAME replacement support routines.
2 Copyright (C) 2004, 2005 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 2, 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 COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "ggc.h"
30 #include "basic-block.h"
31 #include "output.h"
32 #include "errors.h"
33 #include "expr.h"
34 #include "function.h"
35 #include "diagnostic.h"
36 #include "timevar.h"
37 #include "tree-dump.h"
38 #include "tree-flow.h"
39 #include "tree-pass.h"
40 #include "tree-ssa-propagate.h"
41 #include "langhooks.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 /* Return true if we may propagate ORIG into DEST, false otherwise. */
61 bool
62 may_propagate_copy (tree dest, tree orig)
64 tree type_d = TREE_TYPE (dest);
65 tree type_o = TREE_TYPE (orig);
67 /* Do not copy between types for which we *do* need a conversion. */
68 if (!tree_ssa_useless_type_conversion_1 (type_d, type_o))
69 return false;
71 /* FIXME. GIMPLE is allowing pointer assignments and comparisons of
72 pointers that have different alias sets. This means that these
73 pointers will have different memory tags associated to them.
75 If we allow copy propagation in these cases, statements de-referencing
76 the new pointer will now have a reference to a different memory tag
77 with potentially incorrect SSA information.
79 This was showing up in libjava/java/util/zip/ZipFile.java with code
80 like:
82 struct java.io.BufferedInputStream *T.660;
83 struct java.io.BufferedInputStream *T.647;
84 struct java.io.InputStream *is;
85 struct java.io.InputStream *is.662;
86 [ ... ]
87 T.660 = T.647;
88 is = T.660; <-- This ought to be type-casted
89 is.662 = is;
91 Also, f/name.c exposed a similar problem with a COND_EXPR predicate
92 that was causing DOM to generate and equivalence with two pointers of
93 alias-incompatible types:
95 struct _ffename_space *n;
96 struct _ffename *ns;
97 [ ... ]
98 if (n == ns)
99 goto lab;
101 lab:
102 return n;
104 I think that GIMPLE should emit the appropriate type-casts. For the
105 time being, blocking copy-propagation in these cases is the safe thing
106 to do. */
107 if (TREE_CODE (dest) == SSA_NAME
108 && TREE_CODE (orig) == SSA_NAME
109 && POINTER_TYPE_P (type_d)
110 && POINTER_TYPE_P (type_o))
112 tree mt_dest = var_ann (SSA_NAME_VAR (dest))->type_mem_tag;
113 tree mt_orig = var_ann (SSA_NAME_VAR (orig))->type_mem_tag;
114 if (mt_dest && mt_orig && mt_dest != mt_orig)
115 return false;
116 else if (!lang_hooks.types_compatible_p (type_d, type_o))
117 return false;
118 else if (get_alias_set (TREE_TYPE (type_d)) !=
119 get_alias_set (TREE_TYPE (type_o)))
120 return false;
123 /* If the destination is a SSA_NAME for a virtual operand, then we have
124 some special cases to handle. */
125 if (TREE_CODE (dest) == SSA_NAME && !is_gimple_reg (dest))
127 /* If both operands are SSA_NAMEs referring to virtual operands, then
128 we can always propagate. */
129 if (TREE_CODE (orig) == SSA_NAME
130 && !is_gimple_reg (orig))
131 return true;
133 /* We have a "copy" from something like a constant into a virtual
134 operand. Reject these. */
135 return false;
138 /* If ORIG flows in from an abnormal edge, it cannot be propagated. */
139 if (TREE_CODE (orig) == SSA_NAME
140 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig))
141 return false;
143 /* If DEST is an SSA_NAME that flows from an abnormal edge, then it
144 cannot be replaced. */
145 if (TREE_CODE (dest) == SSA_NAME
146 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (dest))
147 return false;
149 /* Anything else is OK. */
150 return true;
153 /* Similarly, but we know that we're propagating into an ASM_EXPR. */
155 bool
156 may_propagate_copy_into_asm (tree dest)
158 /* Hard register operands of asms are special. Do not bypass. */
159 return !(TREE_CODE (dest) == SSA_NAME
160 && TREE_CODE (SSA_NAME_VAR (dest)) == VAR_DECL
161 && DECL_HARD_REGISTER (SSA_NAME_VAR (dest)));
165 /* Given two SSA_NAMEs pointers ORIG and NEW such that we are copy
166 propagating NEW into ORIG, consolidate aliasing information so that
167 they both share the same memory tags. */
169 static void
170 merge_alias_info (tree orig, tree new)
172 tree new_sym = SSA_NAME_VAR (new);
173 tree orig_sym = SSA_NAME_VAR (orig);
174 var_ann_t new_ann = var_ann (new_sym);
175 var_ann_t orig_ann = var_ann (orig_sym);
177 gcc_assert (POINTER_TYPE_P (TREE_TYPE (orig)));
178 gcc_assert (POINTER_TYPE_P (TREE_TYPE (new)));
180 #if defined ENABLE_CHECKING
181 gcc_assert (lang_hooks.types_compatible_p (TREE_TYPE (orig),
182 TREE_TYPE (new)));
184 /* If the pointed-to alias sets are different, these two pointers
185 would never have the same memory tag. In this case, NEW should
186 not have been propagated into ORIG. */
187 gcc_assert (get_alias_set (TREE_TYPE (TREE_TYPE (new_sym)))
188 == get_alias_set (TREE_TYPE (TREE_TYPE (orig_sym))));
189 #endif
191 /* Synchronize the type tags. If both pointers had a tag and they
192 are different, then something has gone wrong. */
193 if (new_ann->type_mem_tag == NULL_TREE)
194 new_ann->type_mem_tag = orig_ann->type_mem_tag;
195 else if (orig_ann->type_mem_tag == NULL_TREE)
196 orig_ann->type_mem_tag = new_ann->type_mem_tag;
197 else
198 gcc_assert (new_ann->type_mem_tag == orig_ann->type_mem_tag);
200 /* Synchronize the name tags. If NEW did not have a name tag, get
201 it from ORIG. This happens when NEW is a compiler generated
202 temporary which still hasn't had its points-to information filled
203 in. */
204 if (SSA_NAME_PTR_INFO (orig))
206 struct ptr_info_def *orig_ptr_info = SSA_NAME_PTR_INFO (orig);
207 struct ptr_info_def *new_ptr_info = SSA_NAME_PTR_INFO (new);
209 if (new_ptr_info == NULL)
210 duplicate_ssa_name_ptr_info (new, orig_ptr_info);
211 else if (orig_ptr_info->name_mem_tag
212 && new_ptr_info->name_mem_tag
213 && orig_ptr_info->pt_vars
214 && new_ptr_info->pt_vars)
216 /* Note that pointer NEW may actually have a different set
217 of pointed-to variables. However, since NEW is being
218 copy-propagated into ORIG, it must always be true that
219 the pointed-to set for pointer NEW is the same, or a
220 subset, of the pointed-to set for pointer ORIG. If this
221 isn't the case, we shouldn't have been able to do the
222 propagation of NEW into ORIG. */
223 gcc_assert (bitmap_intersect_p (new_ptr_info->pt_vars,
224 orig_ptr_info->pt_vars));
230 /* Common code for propagate_value and replace_exp.
232 Replace use operand OP_P with VAL. FOR_PROPAGATION indicates if the
233 replacement is done to propagate a value or not. */
235 static void
236 replace_exp_1 (use_operand_p op_p, tree val,
237 bool for_propagation ATTRIBUTE_UNUSED)
239 tree op = USE_FROM_PTR (op_p);
241 #if defined ENABLE_CHECKING
242 gcc_assert (!(for_propagation
243 && TREE_CODE (op) == SSA_NAME
244 && TREE_CODE (val) == SSA_NAME
245 && !may_propagate_copy (op, val)));
246 #endif
248 if (TREE_CODE (val) == SSA_NAME)
250 if (TREE_CODE (op) == SSA_NAME && POINTER_TYPE_P (TREE_TYPE (op)))
251 merge_alias_info (op, val);
252 SET_USE (op_p, val);
254 else
255 SET_USE (op_p, unsave_expr_now (val));
259 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
260 into the operand pointed by OP_P.
262 Use this version for const/copy propagation as it will perform additional
263 checks to ensure validity of the const/copy propagation. */
265 void
266 propagate_value (use_operand_p op_p, tree val)
268 replace_exp_1 (op_p, val, true);
272 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
273 into the tree pointed by OP_P.
275 Use this version for const/copy propagation when SSA operands are not
276 available. It will perform the additional checks to ensure validity of
277 the const/copy propagation, but will not update any operand information.
278 Be sure to mark the stmt as modified. */
280 void
281 propagate_tree_value (tree *op_p, tree val)
283 #if defined ENABLE_CHECKING
284 gcc_assert (!(TREE_CODE (val) == SSA_NAME
285 && TREE_CODE (*op_p) == SSA_NAME
286 && !may_propagate_copy (*op_p, val)));
287 #endif
289 if (TREE_CODE (val) == SSA_NAME)
291 if (TREE_CODE (*op_p) == SSA_NAME && POINTER_TYPE_P (TREE_TYPE (*op_p)))
292 merge_alias_info (*op_p, val);
293 *op_p = val;
295 else
296 *op_p = unsave_expr_now (val);
300 /* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME).
302 Use this version when not const/copy propagating values. For example,
303 PRE uses this version when building expressions as they would appear
304 in specific blocks taking into account actions of PHI nodes. */
306 void
307 replace_exp (use_operand_p op_p, tree val)
309 replace_exp_1 (op_p, val, false);
313 /*---------------------------------------------------------------------------
314 Copy propagation
315 ---------------------------------------------------------------------------*/
316 /* During propagation, we keep chains of variables that are copies of
317 one another. If variable X_i is a copy of X_j and X_j is a copy of
318 X_k, COPY_OF will contain:
320 COPY_OF[i].VALUE = X_j
321 COPY_OF[j].VALUE = X_k
322 COPY_OF[k].VALUE = X_k
324 After propagation, the copy-of value for each variable X_i is
325 converted into the final value by walking the copy-of chains and
326 updating COPY_OF[i].VALUE to be the last element of the chain. */
327 static prop_value_t *copy_of;
329 /* Used in set_copy_of_val to determine if the last link of a copy-of
330 chain has changed. */
331 static tree *cached_last_copy_of;
333 /* True if we are doing copy propagation on loads and stores. */
334 static bool do_store_copy_prop;
337 /* Return true if this statement may generate a useful copy. */
339 static bool
340 stmt_may_generate_copy (tree stmt)
342 tree lhs, rhs;
343 stmt_ann_t ann;
345 if (TREE_CODE (stmt) == PHI_NODE)
346 return !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (stmt));
348 if (TREE_CODE (stmt) != MODIFY_EXPR)
349 return false;
351 lhs = TREE_OPERAND (stmt, 0);
352 rhs = TREE_OPERAND (stmt, 1);
353 ann = stmt_ann (stmt);
355 /* If the statement has volatile operands, it won't generate a
356 useful copy. */
357 if (ann->has_volatile_ops)
358 return false;
360 /* If we are not doing store copy-prop, statements with loads and/or
361 stores will never generate a useful copy. */
362 if (!do_store_copy_prop
363 && (NUM_VUSES (VUSE_OPS (ann)) > 0
364 || NUM_V_MAY_DEFS (V_MAY_DEF_OPS (ann)) > 0
365 || NUM_V_MUST_DEFS (V_MUST_DEF_OPS (ann)) > 0))
366 return false;
368 /* Otherwise, the only statements that generate useful copies are
369 assignments whose RHS is just an SSA name that doesn't flow
370 through abnormal edges. */
371 return TREE_CODE (rhs) == SSA_NAME && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs);
375 /* Return the copy-of value for VAR. */
377 static inline prop_value_t *
378 get_copy_of_val (tree var)
380 prop_value_t *val = &copy_of[SSA_NAME_VERSION (var)];
382 if (val->value == NULL_TREE
383 && !stmt_may_generate_copy (SSA_NAME_DEF_STMT (var)))
385 /* If the variable will never generate a useful copy relation,
386 make it its own copy. */
387 val->value = var;
388 val->mem_ref = NULL_TREE;
391 return val;
395 /* Return last link in the copy-of chain for VAR. */
397 static tree
398 get_last_copy_of (tree var)
400 tree last;
401 int i;
403 /* Traverse COPY_OF starting at VAR until we get to the last
404 link in the chain. Since it is possible to have cycles in PHI
405 nodes, the copy-of chain may also contain cycles.
407 To avoid infinite loops and to avoid traversing lengthy copy-of
408 chains, we artificially limit the maximum number of chains we are
409 willing to traverse.
411 The value 5 was taken from a compiler and runtime library
412 bootstrap and a mixture of C and C++ code from various sources.
413 More than 82% of all copy-of chains were shorter than 5 links. */
414 #define LIMIT 5
416 last = var;
417 for (i = 0; i < LIMIT; i++)
419 tree copy = copy_of[SSA_NAME_VERSION (last)].value;
420 if (copy == NULL_TREE || copy == last)
421 break;
422 last = copy;
425 /* If we have reached the limit, then we are either in a copy-of
426 cycle or the copy-of chain is too long. In this case, just
427 return VAR so that it is not considered a copy of anything. */
428 return (i < LIMIT ? last : var);
432 /* Set FIRST to be the first variable in the copy-of chain for DEST.
433 If DEST's copy-of value or its copy-of chain have changed, return
434 true.
436 MEM_REF is the memory reference where FIRST is stored. This is
437 used when DEST is a non-register and we are copy propagating loads
438 and stores. */
440 static inline bool
441 set_copy_of_val (tree dest, tree first, tree mem_ref)
443 unsigned int dest_ver = SSA_NAME_VERSION (dest);
444 tree old_first, old_last, new_last;
446 /* Set FIRST to be the first link in COPY_OF[DEST]. If that
447 changed, return true. */
448 old_first = copy_of[dest_ver].value;
449 copy_of[dest_ver].value = first;
450 copy_of[dest_ver].mem_ref = mem_ref;
452 if (old_first != first)
453 return true;
455 /* If FIRST and OLD_FIRST are the same, we need to check whether the
456 copy-of chain starting at FIRST ends in a different variable. If
457 the copy-of chain starting at FIRST ends up in a different
458 variable than the last cached value we had for DEST, then return
459 true because DEST is now a copy of a different variable.
461 This test is necessary because even though the first link in the
462 copy-of chain may not have changed, if any of the variables in
463 the copy-of chain changed its final value, DEST will now be the
464 copy of a different variable, so we have to do another round of
465 propagation for everything that depends on DEST. */
466 old_last = cached_last_copy_of[dest_ver];
467 new_last = get_last_copy_of (dest);
468 cached_last_copy_of[dest_ver] = new_last;
470 return (old_last != new_last);
474 /* Dump the copy-of value for variable VAR to DUMP_FILE. */
476 static void
477 dump_copy_of (FILE *dump_file, tree var)
479 tree val;
481 print_generic_expr (dump_file, var, dump_flags);
483 if (TREE_CODE (var) != SSA_NAME)
484 return;
486 fprintf (dump_file, " copy-of chain: ");
488 val = var;
489 print_generic_expr (dump_file, val, 0);
490 fprintf (dump_file, " ");
491 while (copy_of[SSA_NAME_VERSION (val)].value
492 && copy_of[SSA_NAME_VERSION (val)].value != val)
494 fprintf (dump_file, "-> ");
495 val = copy_of[SSA_NAME_VERSION (val)].value;
496 print_generic_expr (dump_file, val, 0);
497 fprintf (dump_file, " ");
500 val = get_copy_of_val (var)->value;
501 if (val == NULL_TREE)
502 fprintf (dump_file, "[UNDEFINED]");
503 else if (val != var)
504 fprintf (dump_file, "[COPY]");
505 else
506 fprintf (dump_file, "[NOT A COPY]");
510 /* Evaluate the RHS of STMT. If it produces a valid copy, set the LHS
511 value and store the LHS into *RESULT_P. If STMT generates more
512 than one name (i.e., STMT is an aliased store), it is enough to
513 store the first name in the V_MAY_DEF list into *RESULT_P. After
514 all, the names generated will be VUSEd in the same statements. */
516 static enum ssa_prop_result
517 copy_prop_visit_assignment (tree stmt, tree *result_p)
519 tree lhs, rhs;
520 prop_value_t *rhs_val;
522 lhs = TREE_OPERAND (stmt, 0);
523 rhs = TREE_OPERAND (stmt, 1);
525 gcc_assert (TREE_CODE (rhs) == SSA_NAME);
527 rhs_val = get_copy_of_val (rhs);
529 if (TREE_CODE (lhs) == SSA_NAME)
531 /* Straight copy between two SSA names. First, make sure that
532 we can propagate the RHS into uses of LHS. */
533 if (!may_propagate_copy (lhs, rhs))
534 return SSA_PROP_VARYING;
536 /* Avoid copy propagation from an inner into an outer loop.
537 Otherwise, this may move loop variant variables outside of
538 their loops and prevent coalescing opportunities. If the
539 value was loop invariant, it will be hoisted by LICM and
540 exposed for copy propagation. */
541 if (loop_depth_of_name (rhs) > loop_depth_of_name (lhs))
542 return SSA_PROP_VARYING;
544 /* Notice that in the case of assignments, we make the LHS be a
545 copy of RHS's value, not of RHS itself. This avoids keeping
546 unnecessary copy-of chains (assignments cannot be in a cycle
547 like PHI nodes), speeding up the propagation process.
548 This is different from what we do in copy_prop_visit_phi_node.
549 In those cases, we are interested in the copy-of chains. */
550 *result_p = lhs;
551 if (set_copy_of_val (*result_p, rhs_val->value, rhs_val->mem_ref))
552 return SSA_PROP_INTERESTING;
553 else
554 return SSA_PROP_NOT_INTERESTING;
556 else if (stmt_makes_single_store (stmt))
558 /* Otherwise, set the names in V_MAY_DEF/V_MUST_DEF operands
559 to be a copy of RHS. */
560 ssa_op_iter i;
561 tree vdef;
562 bool changed;
564 /* This should only be executed when doing store copy-prop. */
565 gcc_assert (do_store_copy_prop);
567 /* Set the value of every VDEF to RHS_VAL. */
568 changed = false;
569 FOR_EACH_SSA_TREE_OPERAND (vdef, stmt, i, SSA_OP_VIRTUAL_DEFS)
570 changed |= set_copy_of_val (vdef, rhs_val->value, lhs);
572 /* Note that for propagation purposes, we are only interested in
573 visiting statements that load the exact same memory reference
574 stored here. Those statements will have the exact same list
575 of virtual uses, so it is enough to set the output of this
576 statement to be its first virtual definition. */
577 *result_p = first_vdef (stmt);
579 if (changed)
580 return SSA_PROP_INTERESTING;
581 else
582 return SSA_PROP_NOT_INTERESTING;
586 return SSA_PROP_VARYING;
590 /* Visit the COND_EXPR STMT. Return SSA_PROP_INTERESTING
591 if it can determine which edge will be taken. Otherwise, return
592 SSA_PROP_VARYING. */
594 static enum ssa_prop_result
595 copy_prop_visit_cond_stmt (tree stmt, edge *taken_edge_p)
597 enum ssa_prop_result retval;
598 tree cond;
599 use_optype uses;
601 cond = COND_EXPR_COND (stmt);
602 uses = STMT_USE_OPS (stmt);
603 retval = SSA_PROP_VARYING;
605 /* The only conditionals that we may be able to compute statically
606 are predicates involving at least one SSA_NAME. */
607 if (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison
608 && NUM_USES (uses) >= 1)
610 unsigned i;
611 tree *orig;
613 /* Save the original operands. */
614 orig = xmalloc (sizeof (tree) * NUM_USES (uses));
615 for (i = 0; i < NUM_USES (uses); i++)
617 orig[i] = USE_OP (uses, i);
618 SET_USE_OP (uses, i, get_last_copy_of (USE_OP (uses, i)));
621 /* See if we can determine the predicate's value. */
622 if (dump_file && (dump_flags & TDF_DETAILS))
624 fprintf (dump_file, "Trying to determine truth value of ");
625 fprintf (dump_file, "predicate ");
626 print_generic_stmt (dump_file, cond, 0);
629 *taken_edge_p = find_taken_edge (bb_for_stmt (stmt), cond);
630 if (*taken_edge_p)
631 retval = SSA_PROP_INTERESTING;
633 /* Restore the original operands. */
634 for (i = 0; i < NUM_USES (uses); i++)
635 SET_USE_OP (uses, i, orig[i]);
636 free (orig);
639 if (dump_file && (dump_flags & TDF_DETAILS) && *taken_edge_p)
640 fprintf (dump_file, "\nConditional will always take edge %d->%d\n",
641 (*taken_edge_p)->src->index, (*taken_edge_p)->dest->index);
643 return retval;
647 /* Evaluate statement STMT. If the statement produces a new output
648 value, return SSA_PROP_INTERESTING and store the SSA_NAME holding
649 the new value in *RESULT_P.
651 If STMT is a conditional branch and we can determine its truth
652 value, set *TAKEN_EDGE_P accordingly.
654 If the new value produced by STMT is varying, return
655 SSA_PROP_VARYING. */
657 static enum ssa_prop_result
658 copy_prop_visit_stmt (tree stmt, edge *taken_edge_p, tree *result_p)
660 stmt_ann_t ann;
661 enum ssa_prop_result retval;
663 if (dump_file && (dump_flags & TDF_DETAILS))
665 fprintf (dump_file, "\nVisiting statement:\n");
666 print_generic_stmt (dump_file, stmt, dump_flags);
667 fprintf (dump_file, "\n");
670 ann = stmt_ann (stmt);
672 if (TREE_CODE (stmt) == MODIFY_EXPR
673 && TREE_CODE (TREE_OPERAND (stmt, 1)) == SSA_NAME
674 && (do_store_copy_prop
675 || TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME))
677 /* If the statement is a copy assignment, evaluate its RHS to
678 see if the lattice value of its output has changed. */
679 retval = copy_prop_visit_assignment (stmt, result_p);
681 else if (TREE_CODE (stmt) == COND_EXPR)
683 /* See if we can determine which edge goes out of a conditional
684 jump. */
685 retval = copy_prop_visit_cond_stmt (stmt, taken_edge_p);
687 else
688 retval = SSA_PROP_VARYING;
690 if (retval == SSA_PROP_VARYING)
692 tree def;
693 ssa_op_iter i;
695 /* Any other kind of statement is not interesting for constant
696 propagation and, therefore, not worth simulating. */
697 if (dump_file && (dump_flags & TDF_DETAILS))
698 fprintf (dump_file, "No interesting values produced.\n");
700 /* The assignment is not a copy operation. Don't visit this
701 statement again and mark all the definitions in the statement
702 to be copies of nothing. */
703 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_ALL_DEFS)
704 set_copy_of_val (def, def, NULL_TREE);
707 return retval;
711 /* Visit PHI node PHI. If all the arguments produce the same value,
712 set it to be the value of the LHS of PHI. */
714 static enum ssa_prop_result
715 copy_prop_visit_phi_node (tree phi)
717 enum ssa_prop_result retval;
718 int i;
719 tree lhs;
720 prop_value_t phi_val = { 0, NULL_TREE, NULL_TREE };
722 lhs = PHI_RESULT (phi);
724 if (dump_file && (dump_flags & TDF_DETAILS))
726 fprintf (dump_file, "\nVisiting PHI node: ");
727 print_generic_expr (dump_file, phi, dump_flags);
728 fprintf (dump_file, "\n\n");
731 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
733 prop_value_t *arg_val;
734 tree arg = PHI_ARG_DEF (phi, i);
735 edge e = PHI_ARG_EDGE (phi, i);
737 /* We don't care about values flowing through non-executable
738 edges. */
739 if (!(e->flags & EDGE_EXECUTABLE))
740 continue;
742 /* Constants in the argument list never generate a useful copy.
743 Similarly, names that flow through abnormal edges cannot be
744 used to derive copies. */
745 if (TREE_CODE (arg) != SSA_NAME || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg))
747 phi_val.value = lhs;
748 break;
751 /* Avoid copy propagation from an inner into an outer loop.
752 Otherwise, this may move loop variant variables outside of
753 their loops and prevent coalescing opportunities. If the
754 value was loop invariant, it will be hoisted by LICM and
755 exposed for copy propagation. */
756 if (loop_depth_of_name (arg) > loop_depth_of_name (lhs))
758 phi_val.value = lhs;
759 break;
762 /* If the LHS appears in the argument list, ignore it. It is
763 irrelevant as a copy. */
764 if (arg == lhs || get_last_copy_of (arg) == lhs)
765 continue;
767 if (dump_file && (dump_flags & TDF_DETAILS))
769 fprintf (dump_file, "\tArgument #%d: ", i);
770 dump_copy_of (dump_file, arg);
771 fprintf (dump_file, "\n");
774 arg_val = get_copy_of_val (arg);
776 /* If the LHS didn't have a value yet, make it a copy of the
777 first argument we find. Notice that while we make the LHS be
778 a copy of the argument itself, we take the memory reference
779 from the argument's value so that we can compare it to the
780 memory reference of all the other arguments. */
781 if (phi_val.value == NULL_TREE)
783 phi_val.value = arg;
784 phi_val.mem_ref = arg_val->mem_ref;
785 continue;
788 /* If PHI_VAL and ARG don't have a common copy-of chain, then
789 this PHI node cannot be a copy operation. Also, if we are
790 copy propagating stores and these two arguments came from
791 different memory references, they cannot be considered
792 copies. */
793 if (get_last_copy_of (phi_val.value) != get_last_copy_of (arg)
794 || (do_store_copy_prop
795 && phi_val.mem_ref
796 && arg_val->mem_ref
797 && simple_cst_equal (phi_val.mem_ref, arg_val->mem_ref) != 1))
799 phi_val.value = lhs;
800 break;
804 if (phi_val.value && set_copy_of_val (lhs, phi_val.value, phi_val.mem_ref))
805 retval = (phi_val.value != lhs) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
806 else
807 retval = SSA_PROP_NOT_INTERESTING;
809 if (dump_file && (dump_flags & TDF_DETAILS))
811 fprintf (dump_file, "\nPHI node ");
812 dump_copy_of (dump_file, lhs);
813 fprintf (dump_file, "\nTelling the propagator to ");
814 if (retval == SSA_PROP_INTERESTING)
815 fprintf (dump_file, "add SSA edges out of this PHI and continue.");
816 else if (retval == SSA_PROP_VARYING)
817 fprintf (dump_file, "add SSA edges out of this PHI and never visit again.");
818 else
819 fprintf (dump_file, "do nothing with SSA edges and keep iterating.");
820 fprintf (dump_file, "\n\n");
823 return retval;
827 /* Initialize structures used for copy propagation. */
829 static void
830 init_copy_prop (void)
832 basic_block bb;
834 copy_of = xmalloc (num_ssa_names * sizeof (*copy_of));
835 memset (copy_of, 0, num_ssa_names * sizeof (*copy_of));
837 cached_last_copy_of = xmalloc (num_ssa_names * sizeof (*cached_last_copy_of));
838 memset (cached_last_copy_of, 0, num_ssa_names * sizeof (*cached_last_copy_of));
840 FOR_EACH_BB (bb)
842 block_stmt_iterator si;
843 tree phi;
845 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
847 tree stmt = bsi_stmt (si);
849 /* The only statements that we care about are those that may
850 generate useful copies. We also need to mark conditional
851 jumps so that their outgoing edges are added to the work
852 lists of the propagator. */
853 if (stmt_ends_bb_p (stmt))
854 DONT_SIMULATE_AGAIN (stmt) = false;
855 else if (stmt_may_generate_copy (stmt))
856 DONT_SIMULATE_AGAIN (stmt) = false;
857 else
859 tree def;
860 ssa_op_iter iter;
862 /* No need to simulate this statement anymore. */
863 DONT_SIMULATE_AGAIN (stmt) = true;
865 /* Mark all the outputs of this statement as not being
866 the copy of anything. */
867 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
868 set_copy_of_val (def, def, NULL_TREE);
872 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
873 DONT_SIMULATE_AGAIN (phi) = false;
878 /* Deallocate memory used in copy propagation and do final
879 substitution. */
881 static void
882 fini_copy_prop (void)
884 size_t i;
886 /* Set the final copy-of value for each variable by traversing the
887 copy-of chains. */
888 for (i = 1; i < num_ssa_names; i++)
890 tree var = ssa_name (i);
891 if (var && copy_of[i].value && copy_of[i].value != var)
892 copy_of[i].value = get_last_copy_of (var);
895 substitute_and_fold (copy_of);
897 free (copy_of);
901 /* Main entry point to the copy propagator. The algorithm propagates
902 the value COPY-OF using ssa_propagate. For every variable X_i,
903 COPY-OF(X_i) indicates which variable is X_i created from. The
904 following example shows how the algorithm proceeds at a high level:
906 1 a_24 = x_1
907 2 a_2 = PHI <a_24, x_1>
908 3 a_5 = PHI <a_2>
909 4 x_1 = PHI <x_298, a_5, a_2>
911 The end result should be that a_2, a_5, a_24 and x_1 are a copy of
912 x_298. Propagation proceeds as follows.
914 Visit #1: a_24 is copy-of x_1. Value changed.
915 Visit #2: a_2 is copy-of x_1. Value changed.
916 Visit #3: a_5 is copy-of x_1. Value changed.
917 Visit #4: x_1 is copy-of x_298. Value changed.
918 Visit #1: a_24 is copy-of x_298. Value changed.
919 Visit #2: a_2 is copy-of x_298. Value changed.
920 Visit #3: a_5 is copy-of x_298. Value changed.
921 Visit #4: x_1 is copy-of x_298. Stable state reached.
923 When visiting PHI nodes, we only consider arguments that flow
924 through edges marked executable by the propagation engine. So,
925 when visiting statement #2 for the first time, we will only look at
926 the first argument (a_24) and optimistically assume that its value
927 is the copy of a_24 (x_1).
929 The problem with this approach is that it may fail to discover copy
930 relations in PHI cycles. Instead of propagating copy-of
931 values, we actually propagate copy-of chains. For instance:
933 A_3 = B_1;
934 C_9 = A_3;
935 D_4 = C_9;
936 X_i = D_4;
938 In this code fragment, COPY-OF (X_i) = { D_4, C_9, A_3, B_1 }.
939 Obviously, we are only really interested in the last value of the
940 chain, however the propagator needs to access the copy-of chain
941 when visiting PHI nodes.
943 To represent the copy-of chain, we use the array COPY_CHAINS, which
944 holds the first link in the copy-of chain for every variable.
945 If variable X_i is a copy of X_j, which in turn is a copy of X_k,
946 the array will contain:
948 COPY_CHAINS[i] = X_j
949 COPY_CHAINS[j] = X_k
950 COPY_CHAINS[k] = X_k
952 Keeping copy-of chains instead of copy-of values directly becomes
953 important when visiting PHI nodes. Suppose that we had the
954 following PHI cycle, such that x_52 is already considered a copy of
955 x_53:
957 1 x_54 = PHI <x_53, x_52>
958 2 x_53 = PHI <x_898, x_54>
960 Visit #1: x_54 is copy-of x_53 (because x_52 is copy-of x_53)
961 Visit #2: x_53 is copy-of x_898 (because x_54 is a copy of x_53,
962 so it is considered irrelevant
963 as a copy).
964 Visit #1: x_54 is copy-of nothing (x_53 is a copy-of x_898 and
965 x_52 is a copy of x_53, so
966 they don't match)
967 Visit #2: x_53 is copy-of nothing
969 This problem is avoided by keeping a chain of copies, instead of
970 the final copy-of value. Propagation will now only keep the first
971 element of a variable's copy-of chain. When visiting PHI nodes,
972 arguments are considered equal if their copy-of chains end in the
973 same variable. So, as long as their copy-of chains overlap, we
974 know that they will be a copy of the same variable, regardless of
975 which variable that may be).
977 Propagation would then proceed as follows (the notation a -> b
978 means that a is a copy-of b):
980 Visit #1: x_54 = PHI <x_53, x_52>
981 x_53 -> x_53
982 x_52 -> x_53
983 Result: x_54 -> x_53. Value changed. Add SSA edges.
985 Visit #1: x_53 = PHI <x_898, x_54>
986 x_898 -> x_898
987 x_54 -> x_53
988 Result: x_53 -> x_898. Value changed. Add SSA edges.
990 Visit #2: x_54 = PHI <x_53, x_52>
991 x_53 -> x_898
992 x_52 -> x_53 -> x_898
993 Result: x_54 -> x_898. Value changed. Add SSA edges.
995 Visit #2: x_53 = PHI <x_898, x_54>
996 x_898 -> x_898
997 x_54 -> x_898
998 Result: x_53 -> x_898. Value didn't change. Stable state
1000 Once the propagator stabilizes, we end up with the desired result
1001 x_53 and x_54 are both copies of x_898. */
1003 static void
1004 execute_copy_prop (bool store_copy_prop)
1006 do_store_copy_prop = store_copy_prop;
1007 init_copy_prop ();
1008 ssa_propagate (copy_prop_visit_stmt, copy_prop_visit_phi_node);
1009 fini_copy_prop ();
1013 static bool
1014 gate_copy_prop (void)
1016 return flag_tree_copy_prop != 0;
1019 static void
1020 do_copy_prop (void)
1022 execute_copy_prop (false);
1025 struct tree_opt_pass pass_copy_prop =
1027 "copyprop", /* name */
1028 gate_copy_prop, /* gate */
1029 do_copy_prop, /* execute */
1030 NULL, /* sub */
1031 NULL, /* next */
1032 0, /* static_pass_number */
1033 TV_TREE_COPY_PROP, /* tv_id */
1034 PROP_ssa | PROP_alias | PROP_cfg, /* properties_required */
1035 0, /* properties_provided */
1036 0, /* properties_destroyed */
1037 0, /* todo_flags_start */
1038 TODO_cleanup_cfg
1039 | TODO_dump_func
1040 | TODO_ggc_collect
1041 | TODO_verify_ssa
1042 | TODO_update_ssa, /* todo_flags_finish */
1043 0 /* letter */
1047 static bool
1048 gate_store_copy_prop (void)
1050 /* STORE-COPY-PROP is enabled only with -ftree-store-copy-prop, but
1051 when -fno-tree-store-copy-prop is specified, we should run
1052 regular COPY-PROP. That's why the pass is enabled with either
1053 flag. */
1054 return flag_tree_store_copy_prop != 0 || flag_tree_copy_prop != 0;
1057 static void
1058 store_copy_prop (void)
1060 /* If STORE-COPY-PROP is not enabled, we just run regular COPY-PROP. */
1061 execute_copy_prop (flag_tree_store_copy_prop != 0);
1064 struct tree_opt_pass pass_store_copy_prop =
1066 "store_copyprop", /* name */
1067 gate_store_copy_prop, /* gate */
1068 store_copy_prop, /* execute */
1069 NULL, /* sub */
1070 NULL, /* next */
1071 0, /* static_pass_number */
1072 TV_TREE_STORE_COPY_PROP, /* tv_id */
1073 PROP_ssa | PROP_alias | PROP_cfg, /* properties_required */
1074 0, /* properties_provided */
1075 0, /* properties_destroyed */
1076 0, /* todo_flags_start */
1077 TODO_dump_func
1078 | TODO_cleanup_cfg
1079 | TODO_ggc_collect
1080 | TODO_verify_ssa
1081 | TODO_update_ssa, /* todo_flags_finish */
1082 0 /* letter */