OSDN Git Service

PR rtl-optimization/28940
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-copy.c
1 /* Copy propagation and SSA_NAME replacement support routines.
2    Copyright (C) 2004, 2005, 2007 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>.  */
19
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 "rtl.h"
27 #include "tm_p.h"
28 #include "ggc.h"
29 #include "basic-block.h"
30 #include "output.h"
31 #include "expr.h"
32 #include "function.h"
33 #include "diagnostic.h"
34 #include "timevar.h"
35 #include "tree-dump.h"
36 #include "tree-flow.h"
37 #include "tree-pass.h"
38 #include "tree-ssa-propagate.h"
39 #include "langhooks.h"
40
41 /* This file implements the copy propagation pass and provides a
42    handful of interfaces for performing const/copy propagation and
43    simple expression replacement which keep variable annotations
44    up-to-date.
45
46    We require that for any copy operation where the RHS and LHS have
47    a non-null memory tag the memory tag be the same.   It is OK
48    for one or both of the memory tags to be NULL.
49
50    We also require tracking if a variable is dereferenced in a load or
51    store operation.
52
53    We enforce these requirements by having all copy propagation and
54    replacements of one SSA_NAME with a different SSA_NAME to use the
55    APIs defined in this file.  */
56
57 /* Return true if we may propagate ORIG into DEST, false otherwise.  */
58
59 bool
60 may_propagate_copy (tree dest, tree orig)
61 {
62   tree type_d = TREE_TYPE (dest);
63   tree type_o = TREE_TYPE (orig);
64
65   /* For memory partitions, copies are OK as long as the memory symbol
66      belongs to the partition.  */
67   if (TREE_CODE (dest) == SSA_NAME
68       && TREE_CODE (SSA_NAME_VAR (dest)) == MEMORY_PARTITION_TAG)
69     return (TREE_CODE (orig) == SSA_NAME
70             && !is_gimple_reg (orig)
71             && (SSA_NAME_VAR (dest) == SSA_NAME_VAR (orig)
72                 || bitmap_bit_p (MPT_SYMBOLS (SSA_NAME_VAR (dest)),
73                                  DECL_UID (SSA_NAME_VAR (orig)))));
74
75   if (TREE_CODE (orig) == SSA_NAME
76       && TREE_CODE (SSA_NAME_VAR (orig)) == MEMORY_PARTITION_TAG)
77     return (TREE_CODE (dest) == SSA_NAME
78             && !is_gimple_reg (dest)
79             && (SSA_NAME_VAR (dest) == SSA_NAME_VAR (orig)
80                 || bitmap_bit_p (MPT_SYMBOLS (SSA_NAME_VAR (orig)),
81                                  DECL_UID (SSA_NAME_VAR (dest)))));
82   
83   /* Do not copy between types for which we *do* need a conversion.  */
84   if (!useless_type_conversion_p (type_d, type_o))
85     return false;
86
87   /* FIXME.  GIMPLE is allowing pointer assignments and comparisons of
88      pointers that have different alias sets.  This means that these
89      pointers will have different memory tags associated to them.
90
91      If we allow copy propagation in these cases, statements de-referencing
92      the new pointer will now have a reference to a different memory tag
93      with potentially incorrect SSA information.
94
95      This was showing up in libjava/java/util/zip/ZipFile.java with code
96      like:
97
98         struct java.io.BufferedInputStream *T.660;
99         struct java.io.BufferedInputStream *T.647;
100         struct java.io.InputStream *is;
101         struct java.io.InputStream *is.662;
102         [ ... ]
103         T.660 = T.647;
104         is = T.660;     <-- This ought to be type-casted
105         is.662 = is;
106
107      Also, f/name.c exposed a similar problem with a COND_EXPR predicate
108      that was causing DOM to generate and equivalence with two pointers of
109      alias-incompatible types:
110
111         struct _ffename_space *n;
112         struct _ffename *ns;
113         [ ... ]
114         if (n == ns)
115           goto lab;
116         ...
117         lab:
118         return n;
119
120      I think that GIMPLE should emit the appropriate type-casts.  For the
121      time being, blocking copy-propagation in these cases is the safe thing
122      to do.  */
123   if (TREE_CODE (dest) == SSA_NAME
124       && TREE_CODE (orig) == SSA_NAME
125       && POINTER_TYPE_P (type_d)
126       && POINTER_TYPE_P (type_o))
127     {
128       tree mt_dest = symbol_mem_tag (SSA_NAME_VAR (dest));
129       tree mt_orig = symbol_mem_tag (SSA_NAME_VAR (orig));
130       if (mt_dest && mt_orig && mt_dest != mt_orig)
131         return false;
132       else if (!useless_type_conversion_p (type_d, type_o))
133         return false;
134       else if (get_alias_set (TREE_TYPE (type_d)) != 
135                get_alias_set (TREE_TYPE (type_o)))
136         return false;
137
138       /* Also verify flow-sensitive information is compatible.  */
139       if (SSA_NAME_PTR_INFO (orig) && SSA_NAME_PTR_INFO (dest))
140         {
141           struct ptr_info_def *orig_ptr_info = SSA_NAME_PTR_INFO (orig);
142           struct ptr_info_def *dest_ptr_info = SSA_NAME_PTR_INFO (dest);
143
144           if (orig_ptr_info->name_mem_tag
145               && dest_ptr_info->name_mem_tag
146               && orig_ptr_info->pt_vars
147               && dest_ptr_info->pt_vars
148               && !bitmap_intersect_p (dest_ptr_info->pt_vars,
149                                       orig_ptr_info->pt_vars))
150             return false;
151         }
152     }
153
154   /* If the destination is a SSA_NAME for a virtual operand, then we have
155      some special cases to handle.  */
156   if (TREE_CODE (dest) == SSA_NAME && !is_gimple_reg (dest))
157     {
158       /* If both operands are SSA_NAMEs referring to virtual operands, then
159          we can always propagate.  */
160       if (TREE_CODE (orig) == SSA_NAME
161           && !is_gimple_reg (orig))
162         return true;
163
164       /* We have a "copy" from something like a constant into a virtual
165          operand.  Reject these.  */
166       return false;
167     }
168
169   /* If ORIG flows in from an abnormal edge, it cannot be propagated.  */
170   if (TREE_CODE (orig) == SSA_NAME
171       && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig))
172     return false;
173
174   /* If DEST is an SSA_NAME that flows from an abnormal edge, then it
175      cannot be replaced.  */
176   if (TREE_CODE (dest) == SSA_NAME
177       && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (dest))
178     return false;
179
180   /* Anything else is OK.  */
181   return true;
182 }
183
184 /* Similarly, but we know that we're propagating into an ASM_EXPR.  */
185
186 bool
187 may_propagate_copy_into_asm (tree dest)
188 {
189   /* Hard register operands of asms are special.  Do not bypass.  */
190   return !(TREE_CODE (dest) == SSA_NAME
191            && TREE_CODE (SSA_NAME_VAR (dest)) == VAR_DECL
192            && DECL_HARD_REGISTER (SSA_NAME_VAR (dest)));
193 }
194
195
196 /* Given two SSA_NAMEs pointers ORIG and NEW such that we are copy
197    propagating NEW into ORIG, consolidate aliasing information so that
198    they both share the same memory tags.  */
199
200 void
201 merge_alias_info (tree orig_name, tree new_name)
202 {
203   tree new_sym = SSA_NAME_VAR (new_name);
204   tree orig_sym = SSA_NAME_VAR (orig_name);
205   var_ann_t new_ann = var_ann (new_sym);
206   var_ann_t orig_ann = var_ann (orig_sym);
207
208   /* No merging necessary when memory partitions are involved.  */
209   if (factoring_name_p (new_name))
210     {
211       gcc_assert (!is_gimple_reg (orig_sym));
212       return;
213     }
214   else if (factoring_name_p (orig_name))
215     {
216       gcc_assert (!is_gimple_reg (new_sym));
217       return;
218     }
219
220   gcc_assert (POINTER_TYPE_P (TREE_TYPE (orig_name)));
221   gcc_assert (POINTER_TYPE_P (TREE_TYPE (new_name)));
222
223 #if defined ENABLE_CHECKING
224   gcc_assert (useless_type_conversion_p (TREE_TYPE (orig_name),
225                                         TREE_TYPE (new_name)));
226
227   /* If the pointed-to alias sets are different, these two pointers
228      would never have the same memory tag.  In this case, NEW should
229      not have been propagated into ORIG.  */
230   gcc_assert (get_alias_set (TREE_TYPE (TREE_TYPE (new_sym)))
231               == get_alias_set (TREE_TYPE (TREE_TYPE (orig_sym))));
232 #endif
233
234   /* Synchronize the symbol tags.  If both pointers had a tag and they
235      are different, then something has gone wrong.  Symbol tags can
236      always be merged because they are flow insensitive, all the SSA
237      names of the same base DECL share the same symbol tag.  */
238   if (new_ann->symbol_mem_tag == NULL_TREE)
239     new_ann->symbol_mem_tag = orig_ann->symbol_mem_tag;
240   else if (orig_ann->symbol_mem_tag == NULL_TREE)
241     orig_ann->symbol_mem_tag = new_ann->symbol_mem_tag;
242   else
243     gcc_assert (new_ann->symbol_mem_tag == orig_ann->symbol_mem_tag);
244
245   /* Check that flow-sensitive information is compatible.  Notice that
246      we may not merge flow-sensitive information here.  This function
247      is called when propagating equivalences dictated by the IL, like
248      a copy operation P_i = Q_j, and from equivalences dictated by
249      control-flow, like if (P_i == Q_j).
250      
251      In the former case, P_i and Q_j are equivalent in every block
252      dominated by the assignment, so their flow-sensitive information
253      is always the same.  However, in the latter case, the pointers
254      P_i and Q_j are only equivalent in one of the sub-graphs out of
255      the predicate, so their flow-sensitive information is not the
256      same in every block dominated by the predicate.
257
258      Since we cannot distinguish one case from another in this
259      function, we can only make sure that if P_i and Q_j have
260      flow-sensitive information, they should be compatible.  */
261   if (SSA_NAME_PTR_INFO (orig_name) && SSA_NAME_PTR_INFO (new_name))
262     {
263       struct ptr_info_def *orig_ptr_info = SSA_NAME_PTR_INFO (orig_name);
264       struct ptr_info_def *new_ptr_info = SSA_NAME_PTR_INFO (new_name);
265
266       /* Note that pointer NEW and ORIG may actually have different
267          pointed-to variables (e.g., PR 18291 represented in
268          testsuite/gcc.c-torture/compile/pr18291.c).  However, since
269          NEW is being copy-propagated into ORIG, it must always be
270          true that the pointed-to set for pointer NEW is the same, or
271          a subset, of the pointed-to set for pointer ORIG.  If this
272          isn't the case, we shouldn't have been able to do the
273          propagation of NEW into ORIG.  */
274       if (orig_ptr_info->name_mem_tag
275           && new_ptr_info->name_mem_tag
276           && orig_ptr_info->pt_vars
277           && new_ptr_info->pt_vars)
278         gcc_assert (bitmap_intersect_p (new_ptr_info->pt_vars,
279                                         orig_ptr_info->pt_vars));
280     }
281 }   
282
283
284 /* Common code for propagate_value and replace_exp.
285
286    Replace use operand OP_P with VAL.  FOR_PROPAGATION indicates if the
287    replacement is done to propagate a value or not.  */
288
289 static void
290 replace_exp_1 (use_operand_p op_p, tree val,
291                bool for_propagation ATTRIBUTE_UNUSED)
292 {
293   tree op = USE_FROM_PTR (op_p);
294
295 #if defined ENABLE_CHECKING
296   gcc_assert (!(for_propagation
297                 && TREE_CODE (op) == SSA_NAME
298                 && TREE_CODE (val) == SSA_NAME
299                 && !may_propagate_copy (op, val)));
300 #endif
301
302   if (TREE_CODE (val) == SSA_NAME)
303     {
304       if (TREE_CODE (op) == SSA_NAME && POINTER_TYPE_P (TREE_TYPE (op)))
305         merge_alias_info (op, val);
306       SET_USE (op_p, val);
307     }
308   else
309     SET_USE (op_p, unsave_expr_now (val));
310 }
311
312
313 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
314    into the operand pointed to by OP_P.
315
316    Use this version for const/copy propagation as it will perform additional
317    checks to ensure validity of the const/copy propagation.  */
318
319 void
320 propagate_value (use_operand_p op_p, tree val)
321 {
322   replace_exp_1 (op_p, val, true);
323 }
324
325
326 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
327    into the tree pointed to by OP_P.
328
329    Use this version for const/copy propagation when SSA operands are not
330    available.  It will perform the additional checks to ensure validity of
331    the const/copy propagation, but will not update any operand information.
332    Be sure to mark the stmt as modified.  */
333
334 void
335 propagate_tree_value (tree *op_p, tree val)
336 {
337 #if defined ENABLE_CHECKING
338   gcc_assert (!(TREE_CODE (val) == SSA_NAME
339                 && TREE_CODE (*op_p) == SSA_NAME
340                 && !may_propagate_copy (*op_p, val)));
341 #endif
342
343   if (TREE_CODE (val) == SSA_NAME)
344     {
345       if (TREE_CODE (*op_p) == SSA_NAME && POINTER_TYPE_P (TREE_TYPE (*op_p)))
346         merge_alias_info (*op_p, val);
347       *op_p = val;
348     }
349   else
350     *op_p = unsave_expr_now (val);
351 }
352
353
354 /* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME).
355
356    Use this version when not const/copy propagating values.  For example,
357    PRE uses this version when building expressions as they would appear
358    in specific blocks taking into account actions of PHI nodes.  */
359
360 void
361 replace_exp (use_operand_p op_p, tree val)
362 {
363   replace_exp_1 (op_p, val, false);
364 }
365
366
367 /*---------------------------------------------------------------------------
368                                 Copy propagation
369 ---------------------------------------------------------------------------*/
370 /* During propagation, we keep chains of variables that are copies of
371    one another.  If variable X_i is a copy of X_j and X_j is a copy of
372    X_k, COPY_OF will contain:
373
374         COPY_OF[i].VALUE = X_j
375         COPY_OF[j].VALUE = X_k
376         COPY_OF[k].VALUE = X_k
377
378    After propagation, the copy-of value for each variable X_i is
379    converted into the final value by walking the copy-of chains and
380    updating COPY_OF[i].VALUE to be the last element of the chain.  */
381 static prop_value_t *copy_of;
382
383 /* Used in set_copy_of_val to determine if the last link of a copy-of
384    chain has changed.  */
385 static tree *cached_last_copy_of;
386
387
388 /* Return true if this statement may generate a useful copy.  */
389
390 static bool
391 stmt_may_generate_copy (tree stmt)
392 {
393   tree lhs, rhs;
394   stmt_ann_t ann;
395
396   if (TREE_CODE (stmt) == PHI_NODE)
397     return !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (stmt));
398
399   if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
400     return false;
401
402   lhs = GIMPLE_STMT_OPERAND (stmt, 0);
403   rhs = GIMPLE_STMT_OPERAND (stmt, 1);
404   ann = stmt_ann (stmt);
405
406   /* If the statement has volatile operands, it won't generate a
407      useful copy.  */
408   if (ann->has_volatile_ops)
409     return false;
410
411   /* Statements with loads and/or stores will never generate a useful copy.  */
412   if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
413     return false;
414
415   /* Otherwise, the only statements that generate useful copies are
416      assignments whose RHS is just an SSA name that doesn't flow
417      through abnormal edges.  */
418   return (TREE_CODE (rhs) == SSA_NAME
419           && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs));
420 }
421
422
423 /* Return the copy-of value for VAR.  */
424
425 static inline prop_value_t *
426 get_copy_of_val (tree var)
427 {
428   prop_value_t *val = &copy_of[SSA_NAME_VERSION (var)];
429
430   if (val->value == NULL_TREE
431       && !stmt_may_generate_copy (SSA_NAME_DEF_STMT (var)))
432     {
433       /* If the variable will never generate a useful copy relation,
434          make it its own copy.  */
435       val->value = var;
436     }
437
438   return val;
439 }
440
441
442 /* Return last link in the copy-of chain for VAR.  */
443
444 static tree
445 get_last_copy_of (tree var)
446 {
447   tree last;
448   int i;
449
450   /* Traverse COPY_OF starting at VAR until we get to the last
451      link in the chain.  Since it is possible to have cycles in PHI
452      nodes, the copy-of chain may also contain cycles.
453      
454      To avoid infinite loops and to avoid traversing lengthy copy-of
455      chains, we artificially limit the maximum number of chains we are
456      willing to traverse.
457
458      The value 5 was taken from a compiler and runtime library
459      bootstrap and a mixture of C and C++ code from various sources.
460      More than 82% of all copy-of chains were shorter than 5 links.  */
461 #define LIMIT   5
462
463   last = var;
464   for (i = 0; i < LIMIT; i++)
465     {
466       tree copy = copy_of[SSA_NAME_VERSION (last)].value;
467       if (copy == NULL_TREE || copy == last)
468         break;
469       last = copy;
470     }
471
472   /* If we have reached the limit, then we are either in a copy-of
473      cycle or the copy-of chain is too long.  In this case, just
474      return VAR so that it is not considered a copy of anything.  */
475   return (i < LIMIT ? last : var);
476 }
477
478
479 /* Set FIRST to be the first variable in the copy-of chain for DEST.
480    If DEST's copy-of value or its copy-of chain has changed, return
481    true.
482
483    MEM_REF is the memory reference where FIRST is stored.  This is
484    used when DEST is a non-register and we are copy propagating loads
485    and stores.  */
486
487 static inline bool
488 set_copy_of_val (tree dest, tree first)
489 {
490   unsigned int dest_ver = SSA_NAME_VERSION (dest);
491   tree old_first, old_last, new_last;
492   
493   /* Set FIRST to be the first link in COPY_OF[DEST].  If that
494      changed, return true.  */
495   old_first = copy_of[dest_ver].value;
496   copy_of[dest_ver].value = first;
497
498   if (old_first != first)
499     return true;
500
501   /* If FIRST and OLD_FIRST are the same, we need to check whether the
502      copy-of chain starting at FIRST ends in a different variable.  If
503      the copy-of chain starting at FIRST ends up in a different
504      variable than the last cached value we had for DEST, then return
505      true because DEST is now a copy of a different variable.
506
507      This test is necessary because even though the first link in the
508      copy-of chain may not have changed, if any of the variables in
509      the copy-of chain changed its final value, DEST will now be the
510      copy of a different variable, so we have to do another round of
511      propagation for everything that depends on DEST.  */
512   old_last = cached_last_copy_of[dest_ver];
513   new_last = get_last_copy_of (dest);
514   cached_last_copy_of[dest_ver] = new_last;
515
516   return (old_last != new_last);
517 }
518
519
520 /* Dump the copy-of value for variable VAR to FILE.  */
521
522 static void
523 dump_copy_of (FILE *file, tree var)
524 {
525   tree val;
526   sbitmap visited;
527
528   print_generic_expr (file, var, dump_flags);
529
530   if (TREE_CODE (var) != SSA_NAME)
531     return;
532     
533   visited = sbitmap_alloc (num_ssa_names);
534   sbitmap_zero (visited);
535   SET_BIT (visited, SSA_NAME_VERSION (var));
536   
537   fprintf (file, " copy-of chain: ");
538
539   val = var;
540   print_generic_expr (file, val, 0);
541   fprintf (file, " ");
542   while (copy_of[SSA_NAME_VERSION (val)].value)
543     {
544       fprintf (file, "-> ");
545       val = copy_of[SSA_NAME_VERSION (val)].value;
546       print_generic_expr (file, val, 0);
547       fprintf (file, " ");
548       if (TEST_BIT (visited, SSA_NAME_VERSION (val)))
549         break;
550       SET_BIT (visited, SSA_NAME_VERSION (val));
551     }
552
553   val = get_copy_of_val (var)->value;
554   if (val == NULL_TREE)
555     fprintf (file, "[UNDEFINED]");
556   else if (val != var)
557     fprintf (file, "[COPY]");
558   else
559     fprintf (file, "[NOT A COPY]");
560   
561   sbitmap_free (visited);
562 }
563
564
565 /* Evaluate the RHS of STMT.  If it produces a valid copy, set the LHS
566    value and store the LHS into *RESULT_P.  If STMT generates more
567    than one name (i.e., STMT is an aliased store), it is enough to
568    store the first name in the VDEF list into *RESULT_P.  After
569    all, the names generated will be VUSEd in the same statements.  */
570
571 static enum ssa_prop_result
572 copy_prop_visit_assignment (tree stmt, tree *result_p)
573 {
574   tree lhs, rhs;
575   prop_value_t *rhs_val;
576
577   lhs = GIMPLE_STMT_OPERAND (stmt, 0);
578   rhs = GIMPLE_STMT_OPERAND (stmt, 1);
579
580   gcc_assert (TREE_CODE (rhs) == SSA_NAME);
581
582   rhs_val = get_copy_of_val (rhs);
583
584   if (TREE_CODE (lhs) == SSA_NAME)
585     {
586       /* Straight copy between two SSA names.  First, make sure that
587          we can propagate the RHS into uses of LHS.  */
588       if (!may_propagate_copy (lhs, rhs))
589         return SSA_PROP_VARYING;
590
591       /* Notice that in the case of assignments, we make the LHS be a
592          copy of RHS's value, not of RHS itself.  This avoids keeping
593          unnecessary copy-of chains (assignments cannot be in a cycle
594          like PHI nodes), speeding up the propagation process.
595          This is different from what we do in copy_prop_visit_phi_node. 
596          In those cases, we are interested in the copy-of chains.  */
597       *result_p = lhs;
598       if (set_copy_of_val (*result_p, rhs_val->value))
599         return SSA_PROP_INTERESTING;
600       else
601         return SSA_PROP_NOT_INTERESTING;
602     }
603
604   return SSA_PROP_VARYING;
605 }
606
607
608 /* Visit the COND_EXPR STMT.  Return SSA_PROP_INTERESTING
609    if it can determine which edge will be taken.  Otherwise, return
610    SSA_PROP_VARYING.  */
611
612 static enum ssa_prop_result
613 copy_prop_visit_cond_stmt (tree stmt, edge *taken_edge_p)
614 {
615   enum ssa_prop_result retval;
616   tree cond;
617
618   cond = COND_EXPR_COND (stmt);
619   retval = SSA_PROP_VARYING;
620
621   /* The only conditionals that we may be able to compute statically
622      are predicates involving two SSA_NAMEs.  */
623   if (COMPARISON_CLASS_P (cond)
624       && TREE_CODE (TREE_OPERAND (cond, 0)) == SSA_NAME
625       && TREE_CODE (TREE_OPERAND (cond, 1)) == SSA_NAME)
626     {
627       tree op0 = get_last_copy_of (TREE_OPERAND (cond, 0));
628       tree op1 = get_last_copy_of (TREE_OPERAND (cond, 1));
629
630       /* See if we can determine the predicate's value.  */
631       if (dump_file && (dump_flags & TDF_DETAILS))
632         {
633           fprintf (dump_file, "Trying to determine truth value of ");
634           fprintf (dump_file, "predicate ");
635           print_generic_stmt (dump_file, cond, 0);
636         }
637
638       /* We can fold COND and get a useful result only when we have
639          the same SSA_NAME on both sides of a comparison operator.  */
640       if (op0 == op1)
641         {
642           tree folded_cond = fold_binary (TREE_CODE (cond), boolean_type_node,
643                                           op0, op1);
644           if (folded_cond)
645             {
646               basic_block bb = bb_for_stmt (stmt);
647               *taken_edge_p = find_taken_edge (bb, folded_cond);
648               if (*taken_edge_p)
649                 retval = SSA_PROP_INTERESTING;
650             }
651         }
652     }
653
654   if (dump_file && (dump_flags & TDF_DETAILS) && *taken_edge_p)
655     fprintf (dump_file, "\nConditional will always take edge %d->%d\n",
656              (*taken_edge_p)->src->index, (*taken_edge_p)->dest->index);
657
658   return retval;
659 }
660
661
662 /* Evaluate statement STMT.  If the statement produces a new output
663    value, return SSA_PROP_INTERESTING and store the SSA_NAME holding
664    the new value in *RESULT_P.
665
666    If STMT is a conditional branch and we can determine its truth
667    value, set *TAKEN_EDGE_P accordingly.
668
669    If the new value produced by STMT is varying, return
670    SSA_PROP_VARYING.  */
671
672 static enum ssa_prop_result
673 copy_prop_visit_stmt (tree stmt, edge *taken_edge_p, tree *result_p)
674 {
675   enum ssa_prop_result retval;
676
677   if (dump_file && (dump_flags & TDF_DETAILS))
678     {
679       fprintf (dump_file, "\nVisiting statement:\n");
680       print_generic_stmt (dump_file, stmt, dump_flags);
681       fprintf (dump_file, "\n");
682     }
683
684   if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
685       && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == SSA_NAME
686       && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 0)) == SSA_NAME)
687     {
688       /* If the statement is a copy assignment, evaluate its RHS to
689          see if the lattice value of its output has changed.  */
690       retval = copy_prop_visit_assignment (stmt, result_p);
691     }
692   else if (TREE_CODE (stmt) == COND_EXPR)
693     {
694       /* See if we can determine which edge goes out of a conditional
695          jump.  */
696       retval = copy_prop_visit_cond_stmt (stmt, taken_edge_p);
697     }
698   else
699     retval = SSA_PROP_VARYING;
700
701   if (retval == SSA_PROP_VARYING)
702     {
703       tree def;
704       ssa_op_iter i;
705
706       /* Any other kind of statement is not interesting for constant
707          propagation and, therefore, not worth simulating.  */
708       if (dump_file && (dump_flags & TDF_DETAILS))
709         fprintf (dump_file, "No interesting values produced.\n");
710
711       /* The assignment is not a copy operation.  Don't visit this
712          statement again and mark all the definitions in the statement
713          to be copies of nothing.  */
714       FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_ALL_DEFS)
715         set_copy_of_val (def, def);
716     }
717
718   return retval;
719 }
720
721
722 /* Visit PHI node PHI.  If all the arguments produce the same value,
723    set it to be the value of the LHS of PHI.  */
724
725 static enum ssa_prop_result
726 copy_prop_visit_phi_node (tree phi)
727 {
728   enum ssa_prop_result retval;
729   int i;
730   tree lhs;
731   prop_value_t phi_val = { 0, NULL_TREE, NULL_TREE };
732
733   lhs = PHI_RESULT (phi);
734
735   if (dump_file && (dump_flags & TDF_DETAILS))
736     {
737       fprintf (dump_file, "\nVisiting PHI node: ");
738       print_generic_expr (dump_file, phi, dump_flags);
739       fprintf (dump_file, "\n\n");
740     }
741
742   for (i = 0; i < PHI_NUM_ARGS (phi); i++)
743     {
744       prop_value_t *arg_val;
745       tree arg = PHI_ARG_DEF (phi, i);
746       edge e = PHI_ARG_EDGE (phi, i);
747
748       /* We don't care about values flowing through non-executable
749          edges.  */
750       if (!(e->flags & EDGE_EXECUTABLE))
751         continue;
752
753       /* Constants in the argument list never generate a useful copy.
754          Similarly, names that flow through abnormal edges cannot be
755          used to derive copies.  */
756       if (TREE_CODE (arg) != SSA_NAME || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg))
757         {
758           phi_val.value = lhs;
759           break;
760         }
761
762       /* Avoid copy propagation from an inner into an outer loop.
763          Otherwise, this may move loop variant variables outside of
764          their loops and prevent coalescing opportunities.  If the
765          value was loop invariant, it will be hoisted by LICM and
766          exposed for copy propagation.  */
767       if (loop_depth_of_name (arg) > loop_depth_of_name (lhs))
768         {
769           phi_val.value = lhs;
770           break;
771         }
772
773       /* If the LHS appears in the argument list, ignore it.  It is
774          irrelevant as a copy.  */
775       if (arg == lhs || get_last_copy_of (arg) == lhs)
776         continue;
777
778       if (dump_file && (dump_flags & TDF_DETAILS))
779         {
780           fprintf (dump_file, "\tArgument #%d: ", i);
781           dump_copy_of (dump_file, arg);
782           fprintf (dump_file, "\n");
783         }
784
785       arg_val = get_copy_of_val (arg);
786
787       /* If the LHS didn't have a value yet, make it a copy of the
788          first argument we find.  Notice that while we make the LHS be
789          a copy of the argument itself, we take the memory reference
790          from the argument's value so that we can compare it to the
791          memory reference of all the other arguments.  */
792       if (phi_val.value == NULL_TREE)
793         {
794           phi_val.value = arg;
795           continue;
796         }
797
798       /* If PHI_VAL and ARG don't have a common copy-of chain, then
799          this PHI node cannot be a copy operation.  Also, if we are
800          copy propagating stores and these two arguments came from
801          different memory references, they cannot be considered
802          copies.  */
803       if (get_last_copy_of (phi_val.value) != get_last_copy_of (arg))
804         {
805           phi_val.value = lhs;
806           break;
807         }
808     }
809
810   if (phi_val.value && set_copy_of_val (lhs, phi_val.value))
811     retval = (phi_val.value != lhs) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
812   else
813     retval = SSA_PROP_NOT_INTERESTING;
814
815   if (dump_file && (dump_flags & TDF_DETAILS))
816     {
817       fprintf (dump_file, "\nPHI node ");
818       dump_copy_of (dump_file, lhs);
819       fprintf (dump_file, "\nTelling the propagator to ");
820       if (retval == SSA_PROP_INTERESTING)
821         fprintf (dump_file, "add SSA edges out of this PHI and continue.");
822       else if (retval == SSA_PROP_VARYING)
823         fprintf (dump_file, "add SSA edges out of this PHI and never visit again.");
824       else
825         fprintf (dump_file, "do nothing with SSA edges and keep iterating.");
826       fprintf (dump_file, "\n\n");
827     }
828
829   return retval;
830 }
831
832
833 /* Initialize structures used for copy propagation.   PHIS_ONLY is true
834    if we should only consider PHI nodes as generating copy propagation
835    opportunities.  */
836
837 static void
838 init_copy_prop (void)
839 {
840   basic_block bb;
841
842   copy_of = XCNEWVEC (prop_value_t, num_ssa_names);
843
844   cached_last_copy_of = XCNEWVEC (tree, num_ssa_names);
845
846   FOR_EACH_BB (bb)
847     {
848       block_stmt_iterator si;
849       tree phi, def;
850       int depth = bb->loop_depth;
851
852       for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
853         {
854           tree stmt = bsi_stmt (si);
855           ssa_op_iter iter;
856
857           /* The only statements that we care about are those that may
858              generate useful copies.  We also need to mark conditional
859              jumps so that their outgoing edges are added to the work
860              lists of the propagator.
861
862              Avoid copy propagation from an inner into an outer loop.
863              Otherwise, this may move loop variant variables outside of
864              their loops and prevent coalescing opportunities.  If the
865              value was loop invariant, it will be hoisted by LICM and
866              exposed for copy propagation.  */
867           if (stmt_ends_bb_p (stmt))
868             DONT_SIMULATE_AGAIN (stmt) = false;
869           else if (stmt_may_generate_copy (stmt)
870                    && loop_depth_of_name (GIMPLE_STMT_OPERAND (stmt, 1)) <= depth)
871             DONT_SIMULATE_AGAIN (stmt) = false;
872           else
873             DONT_SIMULATE_AGAIN (stmt) = true;
874
875           /* Mark all the outputs of this statement as not being
876              the copy of anything.  */
877           FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
878             if (DONT_SIMULATE_AGAIN (stmt))
879               set_copy_of_val (def, def);
880             else
881               cached_last_copy_of[SSA_NAME_VERSION (def)] = def;
882         }
883
884       for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
885         {
886           def = PHI_RESULT (phi);
887           if (!is_gimple_reg (def))
888             DONT_SIMULATE_AGAIN (phi) = true;
889           else
890             DONT_SIMULATE_AGAIN (phi) = false;
891
892           if (DONT_SIMULATE_AGAIN (phi))
893             set_copy_of_val (def, def);
894           else
895             cached_last_copy_of[SSA_NAME_VERSION (def)] = def;
896         }
897     }
898 }
899
900
901 /* Deallocate memory used in copy propagation and do final
902    substitution.  */
903
904 static void
905 fini_copy_prop (void)
906 {
907   size_t i;
908   prop_value_t *tmp;
909   
910   /* Set the final copy-of value for each variable by traversing the
911      copy-of chains.  */
912   tmp = XCNEWVEC (prop_value_t, num_ssa_names);
913   for (i = 1; i < num_ssa_names; i++)
914     {
915       tree var = ssa_name (i);
916       if (var && copy_of[i].value && copy_of[i].value != var)
917         tmp[i].value = get_last_copy_of (var);
918     }
919
920   substitute_and_fold (tmp, false);
921
922   free (cached_last_copy_of);
923   free (copy_of);
924   free (tmp);
925 }
926
927
928 /* Main entry point to the copy propagator.
929
930    PHIS_ONLY is true if we should only consider PHI nodes as generating
931    copy propagation opportunities. 
932
933    The algorithm propagates the value COPY-OF using ssa_propagate.  For
934    every variable X_i, COPY-OF(X_i) indicates which variable is X_i created
935    from.  The following example shows how the algorithm proceeds at a
936    high level:
937
938             1   a_24 = x_1
939             2   a_2 = PHI <a_24, x_1>
940             3   a_5 = PHI <a_2>
941             4   x_1 = PHI <x_298, a_5, a_2>
942
943    The end result should be that a_2, a_5, a_24 and x_1 are a copy of
944    x_298.  Propagation proceeds as follows.
945
946    Visit #1: a_24 is copy-of x_1.  Value changed.
947    Visit #2: a_2 is copy-of x_1.  Value changed.
948    Visit #3: a_5 is copy-of x_1.  Value changed.
949    Visit #4: x_1 is copy-of x_298.  Value changed.
950    Visit #1: a_24 is copy-of x_298.  Value changed.
951    Visit #2: a_2 is copy-of x_298.  Value changed.
952    Visit #3: a_5 is copy-of x_298.  Value changed.
953    Visit #4: x_1 is copy-of x_298.  Stable state reached.
954    
955    When visiting PHI nodes, we only consider arguments that flow
956    through edges marked executable by the propagation engine.  So,
957    when visiting statement #2 for the first time, we will only look at
958    the first argument (a_24) and optimistically assume that its value
959    is the copy of a_24 (x_1).
960
961    The problem with this approach is that it may fail to discover copy
962    relations in PHI cycles.  Instead of propagating copy-of
963    values, we actually propagate copy-of chains.  For instance:
964
965                 A_3 = B_1;
966                 C_9 = A_3;
967                 D_4 = C_9;
968                 X_i = D_4;
969
970    In this code fragment, COPY-OF (X_i) = { D_4, C_9, A_3, B_1 }.
971    Obviously, we are only really interested in the last value of the
972    chain, however the propagator needs to access the copy-of chain
973    when visiting PHI nodes.
974
975    To represent the copy-of chain, we use the array COPY_CHAINS, which
976    holds the first link in the copy-of chain for every variable.
977    If variable X_i is a copy of X_j, which in turn is a copy of X_k,
978    the array will contain:
979
980                 COPY_CHAINS[i] = X_j
981                 COPY_CHAINS[j] = X_k
982                 COPY_CHAINS[k] = X_k
983
984    Keeping copy-of chains instead of copy-of values directly becomes
985    important when visiting PHI nodes.  Suppose that we had the
986    following PHI cycle, such that x_52 is already considered a copy of
987    x_53:
988
989             1   x_54 = PHI <x_53, x_52>
990             2   x_53 = PHI <x_898, x_54>
991    
992    Visit #1: x_54 is copy-of x_53 (because x_52 is copy-of x_53)
993    Visit #2: x_53 is copy-of x_898 (because x_54 is a copy of x_53,
994                                     so it is considered irrelevant
995                                     as a copy).
996    Visit #1: x_54 is copy-of nothing (x_53 is a copy-of x_898 and
997                                       x_52 is a copy of x_53, so
998                                       they don't match)
999    Visit #2: x_53 is copy-of nothing
1000
1001    This problem is avoided by keeping a chain of copies, instead of
1002    the final copy-of value.  Propagation will now only keep the first
1003    element of a variable's copy-of chain.  When visiting PHI nodes,
1004    arguments are considered equal if their copy-of chains end in the
1005    same variable.  So, as long as their copy-of chains overlap, we
1006    know that they will be a copy of the same variable, regardless of
1007    which variable that may be).
1008    
1009    Propagation would then proceed as follows (the notation a -> b
1010    means that a is a copy-of b):
1011
1012    Visit #1: x_54 = PHI <x_53, x_52>
1013                 x_53 -> x_53
1014                 x_52 -> x_53
1015                 Result: x_54 -> x_53.  Value changed.  Add SSA edges.
1016
1017    Visit #1: x_53 = PHI <x_898, x_54>
1018                 x_898 -> x_898
1019                 x_54 -> x_53
1020                 Result: x_53 -> x_898.  Value changed.  Add SSA edges.
1021
1022    Visit #2: x_54 = PHI <x_53, x_52>
1023                 x_53 -> x_898
1024                 x_52 -> x_53 -> x_898
1025                 Result: x_54 -> x_898.  Value changed.  Add SSA edges.
1026
1027    Visit #2: x_53 = PHI <x_898, x_54>
1028                 x_898 -> x_898
1029                 x_54 -> x_898
1030                 Result: x_53 -> x_898.  Value didn't change.  Stable state
1031
1032    Once the propagator stabilizes, we end up with the desired result
1033    x_53 and x_54 are both copies of x_898.  */
1034
1035 static unsigned int
1036 execute_copy_prop (void)
1037 {
1038   init_copy_prop ();
1039   ssa_propagate (copy_prop_visit_stmt, copy_prop_visit_phi_node);
1040   fini_copy_prop ();
1041   return 0;
1042 }
1043
1044 static bool
1045 gate_copy_prop (void)
1046 {
1047   return flag_tree_copy_prop != 0;
1048 }
1049
1050 struct tree_opt_pass pass_copy_prop =
1051 {
1052   "copyprop",                           /* name */
1053   gate_copy_prop,                       /* gate */
1054   execute_copy_prop,                    /* execute */
1055   NULL,                                 /* sub */
1056   NULL,                                 /* next */
1057   0,                                    /* static_pass_number */
1058   TV_TREE_COPY_PROP,                    /* tv_id */
1059   PROP_ssa | PROP_cfg,                  /* properties_required */
1060   0,                                    /* properties_provided */
1061   0,                                    /* properties_destroyed */
1062   0,                                    /* todo_flags_start */
1063   TODO_cleanup_cfg
1064     | TODO_dump_func
1065     | TODO_ggc_collect
1066     | TODO_verify_ssa
1067     | TODO_update_ssa,                  /* todo_flags_finish */
1068   0                                     /* letter */
1069 };
1070