OSDN Git Service

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