OSDN Git Service

2007-06-06 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-forwprop.c
1 /* Forward propagation of expressions for single use variables.
2    Copyright (C) 2004, 2005 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 2, 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 COPYING.  If not, write to
18 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "ggc.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "basic-block.h"
30 #include "timevar.h"
31 #include "diagnostic.h"
32 #include "tree-flow.h"
33 #include "tree-pass.h"
34 #include "tree-dump.h"
35 #include "langhooks.h"
36 #include "flags.h"
37
38 /* This pass propagates the RHS of assignment statements into use
39    sites of the LHS of the assignment.  It's basically a specialized
40    form of tree combination.   It is hoped all of this can disappear
41    when we have a generalized tree combiner.
42
43    Note carefully that after propagation the resulting statement
44    must still be a proper gimple statement.  Right now we simply
45    only perform propagations we know will result in valid gimple
46    code.  One day we'll want to generalize this code.
47
48    One class of common cases we handle is forward propagating a single use
49    variable into a COND_EXPR.  
50
51      bb0:
52        x = a COND b;
53        if (x) goto ... else goto ...
54
55    Will be transformed into:
56
57      bb0:
58        if (a COND b) goto ... else goto ...
59  
60    Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
61
62    Or (assuming c1 and c2 are constants):
63
64      bb0:
65        x = a + c1;  
66        if (x EQ/NEQ c2) goto ... else goto ...
67
68    Will be transformed into:
69
70      bb0:
71         if (a EQ/NEQ (c2 - c1)) goto ... else goto ...
72
73    Similarly for x = a - c1.
74     
75    Or
76
77      bb0:
78        x = !a
79        if (x) goto ... else goto ...
80
81    Will be transformed into:
82
83      bb0:
84         if (a == 0) goto ... else goto ...
85
86    Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
87    For these cases, we propagate A into all, possibly more than one,
88    COND_EXPRs that use X.
89
90    Or
91
92      bb0:
93        x = (typecast) a
94        if (x) goto ... else goto ...
95
96    Will be transformed into:
97
98      bb0:
99         if (a != 0) goto ... else goto ...
100
101    (Assuming a is an integral type and x is a boolean or x is an
102     integral and a is a boolean.)
103
104    Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
105    For these cases, we propagate A into all, possibly more than one,
106    COND_EXPRs that use X.
107
108    In addition to eliminating the variable and the statement which assigns
109    a value to the variable, we may be able to later thread the jump without
110    adding insane complexity in the dominator optimizer.
111
112    Also note these transformations can cascade.  We handle this by having
113    a worklist of COND_EXPR statements to examine.  As we make a change to
114    a statement, we put it back on the worklist to examine on the next
115    iteration of the main loop.
116
117    A second class of propagation opportunities arises for ADDR_EXPR
118    nodes.
119
120      ptr = &x->y->z;
121      res = *ptr;
122
123    Will get turned into
124
125      res = x->y->z;
126
127    Or
128
129      ptr = &x[0];
130      ptr2 = ptr + <constant>;
131
132    Will get turned into
133
134      ptr2 = &x[constant/elementsize];
135
136   Or
137
138      ptr = &x[0];
139      offset = index * element_size;
140      offset_p = (pointer) offset;
141      ptr2 = ptr + offset_p
142
143   Will get turned into:
144
145      ptr2 = &x[index];
146
147   We also propagate casts into SWITCH_EXPR and COND_EXPR conditions to
148   allow us to remove the cast and {NOT_EXPR,NEG_EXPR} into a subsequent
149   {NOT_EXPR,NEG_EXPR}.
150
151    This will (of course) be extended as other needs arise.  */
152
153 static bool forward_propagate_addr_expr (tree name, tree rhs);
154
155 /* Set to true if we delete EH edges during the optimization.  */
156 static bool cfg_changed;
157
158
159 /* Get the next statement we can propagate NAME's value into skipping
160    trivial copies.  Returns the statement that is suitable as a
161    propagation destination or NULL_TREE if there is no such one.
162    This only returns destinations in a single-use chain.  FINAL_NAME_P
163    if non-NULL is written to the ssa name that represents the use.  */
164
165 static tree
166 get_prop_dest_stmt (tree name, tree *final_name_p)
167 {
168   use_operand_p use;
169   tree use_stmt;
170
171   do {
172     /* If name has multiple uses, bail out.  */
173     if (!single_imm_use (name, &use, &use_stmt))
174       return NULL_TREE;
175
176     /* If this is not a trivial copy, we found it.  */
177     if (TREE_CODE (use_stmt) != GIMPLE_MODIFY_STMT
178         || TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 0)) != SSA_NAME
179         || GIMPLE_STMT_OPERAND (use_stmt, 1) != name)
180       break;
181
182     /* Continue searching uses of the copy destination.  */
183     name = GIMPLE_STMT_OPERAND (use_stmt, 0);
184   } while (1);
185
186   if (final_name_p)
187     *final_name_p = name;
188
189   return use_stmt;
190 }
191
192 /* Get the statement we can propagate from into NAME skipping
193    trivial copies.  Returns the statement which defines the
194    propagation source or NULL_TREE if there is no such one.
195    If SINGLE_USE_ONLY is set considers only sources which have
196    a single use chain up to NAME.  If SINGLE_USE_P is non-null,
197    it is set to whether the chain to NAME is a single use chain
198    or not.  SINGLE_USE_P is not written to if SINGLE_USE_ONLY is set.  */
199
200 static tree
201 get_prop_source_stmt (tree name, bool single_use_only, bool *single_use_p)
202 {
203   bool single_use = true;
204
205   do {
206     tree def_stmt = SSA_NAME_DEF_STMT (name);
207
208     if (!has_single_use (name))
209       {
210         single_use = false;
211         if (single_use_only)
212           return NULL_TREE;
213       }
214
215     /* If name is defined by a PHI node or is the default def, bail out.  */
216     if (TREE_CODE (def_stmt) != GIMPLE_MODIFY_STMT)
217       return NULL_TREE;
218
219     /* If name is not a simple copy destination, we found it.  */
220     if (TREE_CODE (GIMPLE_STMT_OPERAND (def_stmt, 1)) != SSA_NAME)
221       {
222         if (!single_use_only && single_use_p)
223           *single_use_p = single_use;
224
225         return def_stmt;
226       }
227
228     /* Continue searching the def of the copy source name.  */
229     name = GIMPLE_STMT_OPERAND (def_stmt, 1);
230   } while (1);
231 }
232
233 /* Checks if the destination ssa name in DEF_STMT can be used as
234    propagation source.  Returns true if so, otherwise false.  */
235
236 static bool
237 can_propagate_from (tree def_stmt)
238 {
239   tree rhs = GIMPLE_STMT_OPERAND (def_stmt, 1);
240
241   /* We cannot propagate ssa names that occur in abnormal phi nodes.  */
242   switch (TREE_CODE_LENGTH (TREE_CODE (rhs)))
243     {
244     case 3:
245       if (TREE_OPERAND (rhs, 2) != NULL_TREE
246           && TREE_CODE (TREE_OPERAND (rhs, 2)) == SSA_NAME
247           && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (rhs, 2)))
248         return false;
249     case 2:
250       if (TREE_OPERAND (rhs, 1) != NULL_TREE
251           && TREE_CODE (TREE_OPERAND (rhs, 1)) == SSA_NAME
252           && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (rhs, 1)))
253         return false;
254     case 1:
255       if (TREE_OPERAND (rhs, 0) != NULL_TREE
256           && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
257           && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (rhs, 0)))
258         return false;
259       break;
260
261     default:
262       return false;
263     }
264
265   /* If the definition is a conversion of a pointer to a function type,
266      then we can not apply optimizations as some targets require function
267      pointers to be canonicalized and in this case this optimization could
268      eliminate a necessary canonicalization.  */
269   if ((TREE_CODE (rhs) == NOP_EXPR
270        || TREE_CODE (rhs) == CONVERT_EXPR)
271       && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0)))
272       && TREE_CODE (TREE_TYPE (TREE_TYPE
273                                 (TREE_OPERAND (rhs, 0)))) == FUNCTION_TYPE)
274     return false;
275
276   return true;
277 }
278
279 /* Remove a copy chain ending in NAME along the defs but not
280    further or including UP_TO_STMT.  If NAME was replaced in
281    its only use then this function can be used to clean up
282    dead stmts.  Returns true if UP_TO_STMT can be removed
283    as well, otherwise false.  */
284
285 static bool
286 remove_prop_source_from_use (tree name, tree up_to_stmt)
287 {
288   block_stmt_iterator bsi;
289   tree stmt;
290
291   do {
292     if (!has_zero_uses (name))
293       return false;
294
295     stmt = SSA_NAME_DEF_STMT (name);
296     if (stmt == up_to_stmt)
297       return true;
298
299     bsi = bsi_for_stmt (stmt);
300     release_defs (stmt);
301     bsi_remove (&bsi, true);
302
303     name = GIMPLE_STMT_OPERAND (stmt, 1);
304   } while (TREE_CODE (name) == SSA_NAME);
305
306   return false;
307 }
308
309 /* Combine OP0 CODE OP1 in the context of a COND_EXPR.  Returns
310    the folded result in a form suitable for COND_EXPR_COND or
311    NULL_TREE, if there is no suitable simplified form.  If
312    INVARIANT_ONLY is true only gimple_min_invariant results are
313    considered simplified.  */
314
315 static tree
316 combine_cond_expr_cond (enum tree_code code, tree type,
317                         tree op0, tree op1, bool invariant_only)
318 {
319   tree t;
320
321   gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
322
323   t = fold_binary (code, type, op0, op1);
324   if (!t)
325     return NULL_TREE;
326
327   /* Require that we got a boolean type out if we put one in.  */
328   gcc_assert (TREE_CODE (TREE_TYPE (t)) == TREE_CODE (type));
329
330   /* For (bool)x use x != 0.  */
331   if (TREE_CODE (t) == NOP_EXPR
332       && TREE_TYPE (t) == boolean_type_node)
333     {
334       tree top0 = TREE_OPERAND (t, 0);
335       t = build2 (NE_EXPR, type,
336                   top0, build_int_cst (TREE_TYPE (top0), 0));
337     }
338   /* For !x use x == 0.  */
339   else if (TREE_CODE (t) == TRUTH_NOT_EXPR)
340     {
341       tree top0 = TREE_OPERAND (t, 0);
342       t = build2 (EQ_EXPR, type,
343                   top0, build_int_cst (TREE_TYPE (top0), 0));
344     }
345   /* For cmp ? 1 : 0 use cmp.  */
346   else if (TREE_CODE (t) == COND_EXPR
347            && COMPARISON_CLASS_P (TREE_OPERAND (t, 0))
348            && integer_onep (TREE_OPERAND (t, 1))
349            && integer_zerop (TREE_OPERAND (t, 2)))
350     {
351       tree top0 = TREE_OPERAND (t, 0);
352       t = build2 (TREE_CODE (top0), type,
353                   TREE_OPERAND (top0, 0), TREE_OPERAND (top0, 1));
354     }
355
356   /* Bail out if we required an invariant but didn't get one.  */
357   if (invariant_only
358       && !is_gimple_min_invariant (t))
359     return NULL_TREE;
360
361   /* A valid conditional for a COND_EXPR is either a gimple value
362      or a comparison with two gimple value operands.  */
363   if (is_gimple_val (t)
364       || (COMPARISON_CLASS_P (t)
365           && is_gimple_val (TREE_OPERAND (t, 0))
366           && is_gimple_val (TREE_OPERAND (t, 1))))
367     return t;
368
369   return NULL_TREE;
370 }
371
372 /* Propagate from the ssa name definition statements of COND_EXPR
373    in statement STMT into the conditional if that simplifies it.
374    Returns zero if no statement was changed, one if there were
375    changes and two if cfg_cleanup needs to run.  */
376
377 static int
378 forward_propagate_into_cond (tree cond_expr, tree stmt)
379 {
380   int did_something = 0;
381
382   do {
383     tree tmp = NULL_TREE;
384     tree cond = COND_EXPR_COND (cond_expr);
385     tree name, def_stmt, rhs;
386     bool single_use_p;
387
388     /* We can do tree combining on SSA_NAME and comparison expressions.  */
389     if (COMPARISON_CLASS_P (cond)
390         && TREE_CODE (TREE_OPERAND (cond, 0)) == SSA_NAME)
391       {
392         /* For comparisons use the first operand, that is likely to
393            simplify comparisons against constants.  */
394         name = TREE_OPERAND (cond, 0);
395         def_stmt = get_prop_source_stmt (name, false, &single_use_p);
396         if (def_stmt != NULL_TREE
397             && can_propagate_from (def_stmt))
398           {
399             tree op1 = TREE_OPERAND (cond, 1);
400             rhs = GIMPLE_STMT_OPERAND (def_stmt, 1);
401             tmp = combine_cond_expr_cond (TREE_CODE (cond), boolean_type_node,
402                                           fold_convert (TREE_TYPE (op1), rhs),
403                                           op1, !single_use_p);
404           }
405         /* If that wasn't successful, try the second operand.  */
406         if (tmp == NULL_TREE
407             && TREE_CODE (TREE_OPERAND (cond, 1)) == SSA_NAME)
408           {
409             tree op0 = TREE_OPERAND (cond, 0);
410             name = TREE_OPERAND (cond, 1);
411             def_stmt = get_prop_source_stmt (name, false, &single_use_p);
412             if (def_stmt == NULL_TREE
413                 || !can_propagate_from (def_stmt))
414               return did_something;
415
416             rhs = GIMPLE_STMT_OPERAND (def_stmt, 1);
417             tmp = combine_cond_expr_cond (TREE_CODE (cond), boolean_type_node,
418                                           op0,
419                                           fold_convert (TREE_TYPE (op0), rhs),
420                                           !single_use_p);
421           }
422       }
423     else if (TREE_CODE (cond) == SSA_NAME)
424       {
425         name = cond;
426         def_stmt = get_prop_source_stmt (name, true, NULL);
427         if (def_stmt == NULL_TREE
428             || !can_propagate_from (def_stmt))
429           return did_something;
430
431         rhs = GIMPLE_STMT_OPERAND (def_stmt, 1);
432         tmp = combine_cond_expr_cond (NE_EXPR, boolean_type_node, rhs,
433                                       build_int_cst (TREE_TYPE (rhs), 0),
434                                       false);
435       }
436
437     if (tmp)
438       {
439         if (dump_file && tmp)
440           {
441             fprintf (dump_file, "  Replaced '");
442             print_generic_expr (dump_file, cond, 0);
443             fprintf (dump_file, "' with '");
444             print_generic_expr (dump_file, tmp, 0);
445             fprintf (dump_file, "'\n");
446           }
447
448         COND_EXPR_COND (cond_expr) = unshare_expr (tmp);
449         update_stmt (stmt);
450
451         /* Remove defining statements.  */
452         remove_prop_source_from_use (name, NULL);
453
454         if (is_gimple_min_invariant (tmp))
455           did_something = 2;
456         else if (did_something == 0)
457           did_something = 1;
458
459         /* Continue combining.  */
460         continue;
461       }
462
463     break;
464   } while (1);
465
466   return did_something;
467 }
468
469 /* We've just substituted an ADDR_EXPR into stmt.  Update all the 
470    relevant data structures to match.  */
471
472 static void
473 tidy_after_forward_propagate_addr (tree stmt)
474 {
475   /* We may have turned a trapping insn into a non-trapping insn.  */
476   if (maybe_clean_or_replace_eh_stmt (stmt, stmt)
477       && tree_purge_dead_eh_edges (bb_for_stmt (stmt)))
478     cfg_changed = true;
479
480   if (TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == ADDR_EXPR)
481      recompute_tree_invariant_for_addr_expr (GIMPLE_STMT_OPERAND (stmt, 1));
482
483   mark_symbols_for_renaming (stmt);
484 }
485
486 /* DEF_RHS contains the address of the 0th element in an array. 
487    USE_STMT uses type of DEF_RHS to compute the address of an
488    arbitrary element within the array.  The (variable) byte offset
489    of the element is contained in OFFSET.
490
491    We walk back through the use-def chains of OFFSET to verify that
492    it is indeed computing the offset of an element within the array
493    and extract the index corresponding to the given byte offset.
494
495    We then try to fold the entire address expression into a form
496    &array[index].
497
498    If we are successful, we replace the right hand side of USE_STMT
499    with the new address computation.  */
500
501 static bool
502 forward_propagate_addr_into_variable_array_index (tree offset,
503                                                   tree def_rhs, tree use_stmt)
504 {
505   tree index;
506
507   /* The offset must be defined by a simple GIMPLE_MODIFY_STMT statement.  */
508   if (TREE_CODE (offset) != GIMPLE_MODIFY_STMT)
509     return false;
510
511   /* The RHS of the statement which defines OFFSET must be a gimple
512      cast of another SSA_NAME.  */
513   offset = GIMPLE_STMT_OPERAND (offset, 1);
514   if (!is_gimple_cast (offset))
515     return false;
516
517   offset = TREE_OPERAND (offset, 0);
518   if (TREE_CODE (offset) != SSA_NAME)
519     return false;
520
521   /* Try to find an expression for a proper index.  This is either
522      a multiplication expression by the element size or just the
523      ssa name we came along in case the element size is one.  */
524   if (integer_onep (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (def_rhs)))))
525     index = offset;
526   else
527     {
528       offset = SSA_NAME_DEF_STMT (offset);
529
530       /* The RHS of the statement which defines OFFSET must be a
531          multiplication of an object by the size of the array elements.  */
532       if (TREE_CODE (offset) != GIMPLE_MODIFY_STMT)
533         return false;
534
535       offset = GIMPLE_STMT_OPERAND (offset, 1);
536       if (TREE_CODE (offset) != MULT_EXPR
537           || TREE_CODE (TREE_OPERAND (offset, 1)) != INTEGER_CST
538           || !simple_cst_equal (TREE_OPERAND (offset, 1),
539                                 TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (def_rhs)))))
540         return false;
541
542       /* The first operand to the MULT_EXPR is the desired index.  */
543       index = TREE_OPERAND (offset, 0);
544     }
545
546   /* Replace the pointer addition with array indexing.  */
547   GIMPLE_STMT_OPERAND (use_stmt, 1) = unshare_expr (def_rhs);
548   TREE_OPERAND (TREE_OPERAND (GIMPLE_STMT_OPERAND (use_stmt, 1), 0), 1)
549     = index;
550
551   /* That should have created gimple, so there is no need to
552      record information to undo the propagation.  */
553   fold_stmt_inplace (use_stmt);
554   tidy_after_forward_propagate_addr (use_stmt);
555   return true;
556 }
557
558 /* NAME is a SSA_NAME representing DEF_RHS which is of the form
559    ADDR_EXPR <whatever>.
560
561    Try to forward propagate the ADDR_EXPR into the use USE_STMT.
562    Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
563    node or for recovery of array indexing from pointer arithmetic.
564    
565    Return true if the propagation was successful (the propagation can
566    be not totally successful, yet things may have been changed).  */
567
568 static bool
569 forward_propagate_addr_expr_1 (tree name, tree def_rhs, tree use_stmt,
570                                bool single_use_p)
571 {
572   tree lhs, rhs, array_ref;
573
574   /* Strip away any outer COMPONENT_REF/ARRAY_REF nodes from the LHS. 
575      ADDR_EXPR will not appear on the LHS.  */
576   lhs = GIMPLE_STMT_OPERAND (use_stmt, 0);
577   while (handled_component_p (lhs))
578     lhs = TREE_OPERAND (lhs, 0);
579
580   rhs = GIMPLE_STMT_OPERAND (use_stmt, 1);
581
582   /* Now see if the LHS node is an INDIRECT_REF using NAME.  If so, 
583      propagate the ADDR_EXPR into the use of NAME and fold the result.  */
584   if (TREE_CODE (lhs) == INDIRECT_REF && TREE_OPERAND (lhs, 0) == name)
585     {
586       /* This should always succeed in creating gimple, so there is
587          no need to save enough state to undo this propagation.  */
588       TREE_OPERAND (lhs, 0) = unshare_expr (def_rhs);
589       fold_stmt_inplace (use_stmt);
590       tidy_after_forward_propagate_addr (use_stmt);
591
592       /* Continue propagating into the RHS.  */
593     }
594
595   /* Trivial cases.  The use statement could be a trivial copy or a
596      useless conversion.  Recurse to the uses of the lhs as copyprop does
597      not copy through differen variant pointers and FRE does not catch
598      all useless conversions.  Treat the case of a single-use name and
599      a conversion to def_rhs type separate, though.  */
600   else if (TREE_CODE (lhs) == SSA_NAME
601            && (TREE_CODE (rhs) == NOP_EXPR
602                || TREE_CODE (rhs) == CONVERT_EXPR)
603            && TREE_TYPE (rhs) == TREE_TYPE (def_rhs)
604            && single_use_p)
605     {
606       GIMPLE_STMT_OPERAND (use_stmt, 1) = unshare_expr (def_rhs);
607       return true;
608     }
609   else if ((TREE_CODE (lhs) == SSA_NAME
610             && rhs == name)
611            || ((TREE_CODE (rhs) == NOP_EXPR
612                 || TREE_CODE (rhs) == CONVERT_EXPR)
613                && tree_ssa_useless_type_conversion_1 (TREE_TYPE (rhs),
614                                                       TREE_TYPE (def_rhs))))
615     return forward_propagate_addr_expr (lhs, def_rhs);
616
617   /* Strip away any outer COMPONENT_REF, ARRAY_REF or ADDR_EXPR
618      nodes from the RHS.  */
619   while (handled_component_p (rhs)
620          || TREE_CODE (rhs) == ADDR_EXPR)
621     rhs = TREE_OPERAND (rhs, 0);
622
623   /* Now see if the RHS node is an INDIRECT_REF using NAME.  If so, 
624      propagate the ADDR_EXPR into the use of NAME and fold the result.  */
625   if (TREE_CODE (rhs) == INDIRECT_REF && TREE_OPERAND (rhs, 0) == name)
626     {
627       /* This should always succeed in creating gimple, so there is
628          no need to save enough state to undo this propagation.  */
629       TREE_OPERAND (rhs, 0) = unshare_expr (def_rhs);
630       fold_stmt_inplace (use_stmt);
631       tidy_after_forward_propagate_addr (use_stmt);
632       return true;
633     }
634
635   /* The remaining cases are all for turning pointer arithmetic into
636      array indexing.  They only apply when we have the address of
637      element zero in an array.  If that is not the case then there
638      is nothing to do.  */
639   array_ref = TREE_OPERAND (def_rhs, 0);
640   if (TREE_CODE (array_ref) != ARRAY_REF
641       || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref, 0))) != ARRAY_TYPE
642       || !integer_zerop (TREE_OPERAND (array_ref, 1)))
643     return false;
644
645   /* If the use of the ADDR_EXPR must be a PLUS_EXPR, or else there
646      is nothing to do. */
647   if (TREE_CODE (rhs) != PLUS_EXPR)
648     return false;
649
650   /* Try to optimize &x[0] + C where C is a multiple of the size
651      of the elements in X into &x[C/element size].  */
652   if (TREE_OPERAND (rhs, 0) == name
653       && TREE_CODE (TREE_OPERAND (rhs, 1)) == INTEGER_CST)
654     {
655       tree orig = unshare_expr (rhs);
656       TREE_OPERAND (rhs, 0) = unshare_expr (def_rhs);
657
658       /* If folding succeeds, then we have just exposed new variables
659          in USE_STMT which will need to be renamed.  If folding fails,
660          then we need to put everything back the way it was.  */
661       if (fold_stmt_inplace (use_stmt))
662         {
663           tidy_after_forward_propagate_addr (use_stmt);
664           return true;
665         }
666       else
667         {
668           GIMPLE_STMT_OPERAND (use_stmt, 1) = orig;
669           update_stmt (use_stmt);
670           return false;
671         }
672     }
673
674   /* Try to optimize &x[0] + OFFSET where OFFSET is defined by
675      converting a multiplication of an index by the size of the
676      array elements, then the result is converted into the proper
677      type for the arithmetic.  */
678   if (TREE_OPERAND (rhs, 0) == name
679       && TREE_CODE (TREE_OPERAND (rhs, 1)) == SSA_NAME
680       /* Avoid problems with IVopts creating PLUS_EXPRs with a
681          different type than their operands.  */
682       && lang_hooks.types_compatible_p (TREE_TYPE (name), TREE_TYPE (rhs)))
683     {
684       bool res;
685       tree offset_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 1));
686       
687       res = forward_propagate_addr_into_variable_array_index (offset_stmt,
688                                                               def_rhs, use_stmt);
689       return res;
690     }
691               
692   /* Same as the previous case, except the operands of the PLUS_EXPR
693      were reversed.  */
694   if (TREE_OPERAND (rhs, 1) == name
695       && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
696       /* Avoid problems with IVopts creating PLUS_EXPRs with a
697          different type than their operands.  */
698       && lang_hooks.types_compatible_p (TREE_TYPE (name), TREE_TYPE (rhs)))
699     {
700       bool res;
701       tree offset_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 0));
702       res = forward_propagate_addr_into_variable_array_index (offset_stmt,
703                                                               def_rhs, use_stmt);
704       return res;
705     }
706   return false;
707 }
708
709 /* STMT is a statement of the form SSA_NAME = ADDR_EXPR <whatever>.
710
711    Try to forward propagate the ADDR_EXPR into all uses of the SSA_NAME.
712    Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
713    node or for recovery of array indexing from pointer arithmetic.
714    Returns true, if all uses have been propagated into.  */
715
716 static bool
717 forward_propagate_addr_expr (tree name, tree rhs)
718 {
719   int stmt_loop_depth = bb_for_stmt (SSA_NAME_DEF_STMT (name))->loop_depth;
720   imm_use_iterator iter;
721   tree use_stmt;
722   bool all = true;
723   bool single_use_p = has_single_use (name);
724
725   FOR_EACH_IMM_USE_STMT (use_stmt, iter, name)
726     {
727       bool result;
728
729       /* If the use is not in a simple assignment statement, then
730          there is nothing we can do.  */
731       if (TREE_CODE (use_stmt) != GIMPLE_MODIFY_STMT)
732         {
733           all = false;
734           continue;
735         }
736
737       /* If the use is in a deeper loop nest, then we do not want
738         to propagate the ADDR_EXPR into the loop as that is likely
739         adding expression evaluations into the loop.  */
740       if (bb_for_stmt (use_stmt)->loop_depth > stmt_loop_depth)
741         {
742           all = false;
743           continue;
744         }
745
746       /* If the use_stmt has side-effects, don't propagate into it.  */
747       if (stmt_ann (use_stmt)->has_volatile_ops)
748         {
749           all = false;
750           continue;
751         }
752
753       push_stmt_changes (&use_stmt);
754
755       result = forward_propagate_addr_expr_1 (name, rhs, use_stmt,
756                                               single_use_p);
757       all &= result;
758
759       pop_stmt_changes (&use_stmt);
760
761       /* Remove intermediate now unused copy and conversion chains.  */
762       if (result
763           && TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 0)) == SSA_NAME
764           && (TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 1)) == SSA_NAME
765               || TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 1)) == NOP_EXPR
766               || TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 1)) == CONVERT_EXPR))
767         {
768           block_stmt_iterator bsi = bsi_for_stmt (use_stmt);
769           release_defs (use_stmt);
770           bsi_remove (&bsi, true);
771         }
772     }
773
774   return all;
775 }
776
777 /* Forward propagate the comparison COND defined in STMT like
778    cond_1 = x CMP y to uses of the form
779      a_1 = (T')cond_1
780      a_1 = !cond_1
781      a_1 = cond_1 != 0
782    Returns true if stmt is now unused.  */
783
784 static bool
785 forward_propagate_comparison (tree cond, tree stmt)
786 {
787   tree name = GIMPLE_STMT_OPERAND (stmt, 0);
788   tree use_stmt, tmp = NULL_TREE;
789
790   /* Don't propagate ssa names that occur in abnormal phis.  */
791   if ((TREE_CODE (TREE_OPERAND (cond, 0)) == SSA_NAME
792        && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (cond, 0)))
793       || (TREE_CODE (TREE_OPERAND (cond, 1)) == SSA_NAME
794           && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (cond, 1))))
795     return false;
796
797   /* Do not un-cse comparisons.  But propagate through copies.  */
798   use_stmt = get_prop_dest_stmt (name, &name);
799   if (use_stmt == NULL_TREE)
800     return false;
801
802   /* Conversion of the condition result to another integral type.  */
803   if (TREE_CODE (use_stmt) == GIMPLE_MODIFY_STMT
804       && (TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 1)) == CONVERT_EXPR
805           || TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 1)) == NOP_EXPR
806           || COMPARISON_CLASS_P (GIMPLE_STMT_OPERAND (use_stmt, 1))
807           || TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 1)) == TRUTH_NOT_EXPR)
808       && INTEGRAL_TYPE_P (TREE_TYPE (GIMPLE_STMT_OPERAND (use_stmt, 0))))
809     {
810       tree lhs = GIMPLE_STMT_OPERAND (use_stmt, 0);
811       tree rhs = GIMPLE_STMT_OPERAND (use_stmt, 1);
812
813       /* We can propagate the condition into a conversion.  */
814       if (TREE_CODE (rhs) == CONVERT_EXPR
815           || TREE_CODE (rhs) == NOP_EXPR)
816         {
817           /* Avoid using fold here as that may create a COND_EXPR with
818              non-boolean condition as canonical form.  */
819           tmp = build2 (TREE_CODE (cond), TREE_TYPE (lhs),
820                         TREE_OPERAND (cond, 0), TREE_OPERAND (cond, 1));
821         }
822       /* We can propagate the condition into X op CST where op
823          is EQ_EXRP or NE_EXPR and CST is either one or zero.  */
824       else if (COMPARISON_CLASS_P (rhs)
825                && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
826                && TREE_CODE (TREE_OPERAND (rhs, 1)) == INTEGER_CST)
827         {
828           enum tree_code code = TREE_CODE (rhs);
829           tree cst = TREE_OPERAND (rhs, 1);
830
831           tmp = combine_cond_expr_cond (code, TREE_TYPE (lhs),
832                                         fold_convert (TREE_TYPE (cst), cond),
833                                         cst, false);
834           if (tmp == NULL_TREE)
835             return false;
836         }
837       /* We can propagate the condition into a statement that
838          computes the logical negation of the comparison result.  */
839       else if (TREE_CODE (rhs) == TRUTH_NOT_EXPR)
840         {
841           tree type = TREE_TYPE (TREE_OPERAND (cond, 0));
842           bool nans = HONOR_NANS (TYPE_MODE (type));
843           enum tree_code code;
844           code = invert_tree_comparison (TREE_CODE (cond), nans);
845           if (code == ERROR_MARK)
846             return false;
847
848           tmp = build2 (code, TREE_TYPE (lhs), TREE_OPERAND (cond, 0),
849                         TREE_OPERAND (cond, 1));
850         }
851       else
852         return false;
853
854       GIMPLE_STMT_OPERAND (use_stmt, 1) = unshare_expr (tmp);
855       update_stmt (use_stmt);
856
857       /* Remove defining statements.  */
858       remove_prop_source_from_use (name, stmt);
859
860       if (dump_file && (dump_flags & TDF_DETAILS))
861         {
862           fprintf (dump_file, "  Replaced '");
863           print_generic_expr (dump_file, rhs, dump_flags);
864           fprintf (dump_file, "' with '");
865           print_generic_expr (dump_file, tmp, dump_flags);
866           fprintf (dump_file, "'\n");
867         }
868
869       return true;
870     }
871
872   return false;
873 }
874
875 /* If we have lhs = ~x (STMT), look and see if earlier we had x = ~y.
876    If so, we can change STMT into lhs = y which can later be copy
877    propagated.  Similarly for negation. 
878
879    This could trivially be formulated as a forward propagation 
880    to immediate uses.  However, we already had an implementation
881    from DOM which used backward propagation via the use-def links.
882
883    It turns out that backward propagation is actually faster as
884    there's less work to do for each NOT/NEG expression we find.
885    Backwards propagation needs to look at the statement in a single
886    backlink.  Forward propagation needs to look at potentially more
887    than one forward link.  */
888
889 static void
890 simplify_not_neg_expr (tree stmt)
891 {
892   tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
893   tree rhs_def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 0));
894
895   /* See if the RHS_DEF_STMT has the same form as our statement.  */
896   if (TREE_CODE (rhs_def_stmt) == GIMPLE_MODIFY_STMT
897       && TREE_CODE (GIMPLE_STMT_OPERAND (rhs_def_stmt, 1)) == TREE_CODE (rhs))
898     {
899       tree rhs_def_operand =
900         TREE_OPERAND (GIMPLE_STMT_OPERAND (rhs_def_stmt, 1), 0);
901
902       /* Verify that RHS_DEF_OPERAND is a suitable SSA_NAME.  */
903       if (TREE_CODE (rhs_def_operand) == SSA_NAME
904           && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs_def_operand))
905         {
906           GIMPLE_STMT_OPERAND (stmt, 1) = rhs_def_operand;
907           update_stmt (stmt);
908         }
909     }
910 }
911
912 /* STMT is a SWITCH_EXPR for which we attempt to find equivalent forms of
913    the condition which we may be able to optimize better.  */
914
915 static void
916 simplify_switch_expr (tree stmt)
917 {
918   tree cond = SWITCH_COND (stmt);
919   tree def, to, ti;
920
921   /* The optimization that we really care about is removing unnecessary
922      casts.  That will let us do much better in propagating the inferred
923      constant at the switch target.  */
924   if (TREE_CODE (cond) == SSA_NAME)
925     {
926       def = SSA_NAME_DEF_STMT (cond);
927       if (TREE_CODE (def) == GIMPLE_MODIFY_STMT)
928         {
929           def = GIMPLE_STMT_OPERAND (def, 1);
930           if (TREE_CODE (def) == NOP_EXPR)
931             {
932               int need_precision;
933               bool fail;
934
935               def = TREE_OPERAND (def, 0);
936
937 #ifdef ENABLE_CHECKING
938               /* ??? Why was Jeff testing this?  We are gimple...  */
939               gcc_assert (is_gimple_val (def));
940 #endif
941
942               to = TREE_TYPE (cond);
943               ti = TREE_TYPE (def);
944
945               /* If we have an extension that preserves value, then we
946                  can copy the source value into the switch.  */
947
948               need_precision = TYPE_PRECISION (ti);
949               fail = false;
950               if (! INTEGRAL_TYPE_P (ti))
951                 fail = true;
952               else if (TYPE_UNSIGNED (to) && !TYPE_UNSIGNED (ti))
953                 fail = true;
954               else if (!TYPE_UNSIGNED (to) && TYPE_UNSIGNED (ti))
955                 need_precision += 1;
956               if (TYPE_PRECISION (to) < need_precision)
957                 fail = true;
958
959               if (!fail)
960                 {
961                   SWITCH_COND (stmt) = def;
962                   update_stmt (stmt);
963                 }
964             }
965         }
966     }
967 }
968
969 /* Main entry point for the forward propagation optimizer.  */
970
971 static unsigned int
972 tree_ssa_forward_propagate_single_use_vars (void)
973 {
974   basic_block bb;
975   unsigned int todoflags = 0;
976
977   cfg_changed = false;
978
979   FOR_EACH_BB (bb)
980     {
981       block_stmt_iterator bsi;
982
983       /* Note we update BSI within the loop as necessary.  */
984       for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
985         {
986           tree stmt = bsi_stmt (bsi);
987
988           /* If this statement sets an SSA_NAME to an address,
989              try to propagate the address into the uses of the SSA_NAME.  */
990           if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
991             {
992               tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
993               tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
994
995
996               if (TREE_CODE (lhs) != SSA_NAME)
997                 {
998                   bsi_next (&bsi);
999                   continue;
1000                 }
1001
1002               if (TREE_CODE (rhs) == ADDR_EXPR)
1003                 {
1004                   if (forward_propagate_addr_expr (lhs, rhs))
1005                     {
1006                       release_defs (stmt);
1007                       todoflags |= TODO_remove_unused_locals;
1008                       bsi_remove (&bsi, true);
1009                     }
1010                   else
1011                     bsi_next (&bsi);
1012                 }
1013               else if ((TREE_CODE (rhs) == BIT_NOT_EXPR
1014                         || TREE_CODE (rhs) == NEGATE_EXPR)
1015                        && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
1016                 {
1017                   simplify_not_neg_expr (stmt);
1018                   bsi_next (&bsi);
1019                 }
1020               else if (TREE_CODE (rhs) == COND_EXPR)
1021                 {
1022                   int did_something;
1023                   fold_defer_overflow_warnings ();
1024                   did_something = forward_propagate_into_cond (rhs, stmt);
1025                   if (did_something == 2)
1026                     cfg_changed = true;
1027                   fold_undefer_overflow_warnings (!TREE_NO_WARNING (rhs)
1028                     && did_something, stmt, WARN_STRICT_OVERFLOW_CONDITIONAL);
1029                   bsi_next (&bsi);
1030                 }
1031               else if (COMPARISON_CLASS_P (rhs))
1032                 {
1033                   if (forward_propagate_comparison (rhs, stmt))
1034                     {
1035                       release_defs (stmt);
1036                       todoflags |= TODO_remove_unused_locals;
1037                       bsi_remove (&bsi, true);
1038                     }
1039                   else
1040                     bsi_next (&bsi);
1041                 }
1042               else
1043                 bsi_next (&bsi);
1044             }
1045           else if (TREE_CODE (stmt) == SWITCH_EXPR)
1046             {
1047               simplify_switch_expr (stmt);
1048               bsi_next (&bsi);
1049             }
1050           else if (TREE_CODE (stmt) == COND_EXPR)
1051             {
1052               int did_something;
1053               fold_defer_overflow_warnings ();
1054               did_something = forward_propagate_into_cond (stmt, stmt);
1055               if (did_something == 2)
1056                 cfg_changed = true;
1057               fold_undefer_overflow_warnings (!TREE_NO_WARNING (stmt)
1058                                               && did_something, stmt,
1059                                               WARN_STRICT_OVERFLOW_CONDITIONAL);
1060               bsi_next (&bsi);
1061             }
1062           else
1063             bsi_next (&bsi);
1064         }
1065     }
1066
1067   if (cfg_changed)
1068     todoflags |= TODO_cleanup_cfg;
1069   return todoflags;
1070 }
1071
1072
1073 static bool
1074 gate_forwprop (void)
1075 {
1076   return 1;
1077 }
1078
1079 struct tree_opt_pass pass_forwprop = {
1080   "forwprop",                   /* name */
1081   gate_forwprop,                /* gate */
1082   tree_ssa_forward_propagate_single_use_vars,   /* execute */
1083   NULL,                         /* sub */
1084   NULL,                         /* next */
1085   0,                            /* static_pass_number */
1086   TV_TREE_FORWPROP,             /* tv_id */
1087   PROP_cfg | PROP_ssa,          /* properties_required */
1088   0,                            /* properties_provided */
1089   0,                            /* properties_destroyed */
1090   0,                            /* todo_flags_start */
1091   TODO_dump_func
1092   | TODO_ggc_collect
1093   | TODO_update_ssa
1094   | TODO_verify_ssa,            /* todo_flags_finish */
1095   0                             /* letter */
1096 };
1097
1098
1099 /* Structure to keep track of the value of a dereferenced PHI result
1100    and the set of virtual operands used for that dereference.  */
1101
1102 struct phiprop_d
1103 {
1104   tree value;
1105   tree vop_stmt;
1106 };
1107
1108 /* Verify if the value recorded for NAME in PHIVN is still valid at
1109    the start of basic block BB.  */
1110
1111 static bool
1112 phivn_valid_p (struct phiprop_d *phivn, tree name, basic_block bb)
1113 {
1114   tree vop_stmt = phivn[SSA_NAME_VERSION (name)].vop_stmt;
1115   ssa_op_iter ui;
1116   tree vuse;
1117
1118   /* The def stmts of all virtual uses need to be post-dominated
1119      by bb.  */
1120   FOR_EACH_SSA_TREE_OPERAND (vuse, vop_stmt, ui, SSA_OP_VUSE)
1121     {
1122       tree use_stmt;
1123       imm_use_iterator ui2;
1124       bool ok = true;
1125
1126       FOR_EACH_IMM_USE_STMT (use_stmt, ui2, vuse)
1127         {
1128           /* If BB does not dominate a VDEF, the value is invalid.  */
1129           if (((TREE_CODE (use_stmt) == GIMPLE_MODIFY_STMT
1130                 && !ZERO_SSA_OPERANDS (use_stmt, SSA_OP_VDEF))
1131                || TREE_CODE (use_stmt) == PHI_NODE)
1132               && !dominated_by_p (CDI_DOMINATORS, bb_for_stmt (use_stmt), bb))
1133             {
1134               ok = false;
1135               BREAK_FROM_IMM_USE_STMT (ui2);
1136             }
1137         }
1138       if (!ok)
1139         return false;
1140     }
1141
1142   return true;
1143 }
1144
1145 /* Insert a new phi node for the dereference of PHI at basic_block
1146    BB with the virtual operands from USE_STMT.  */
1147
1148 static tree
1149 phiprop_insert_phi (basic_block bb, tree phi, tree use_stmt,
1150                     struct phiprop_d *phivn, size_t n)
1151 {
1152   tree res, new_phi;
1153   edge_iterator ei;
1154   edge e;
1155
1156   /* Build a new PHI node to replace the definition of
1157      the indirect reference lhs.  */
1158   res = GIMPLE_STMT_OPERAND (use_stmt, 0);
1159   SSA_NAME_DEF_STMT (res) = new_phi = create_phi_node (res, bb);
1160
1161   /* Add PHI arguments for each edge inserting loads of the
1162      addressable operands.  */
1163   FOR_EACH_EDGE (e, ei, bb->preds)
1164     {
1165       tree old_arg, new_var, tmp;
1166
1167       old_arg = PHI_ARG_DEF_FROM_EDGE (phi, e);
1168       while (TREE_CODE (old_arg) == SSA_NAME
1169              && (SSA_NAME_VERSION (old_arg) >= n
1170                  || phivn[SSA_NAME_VERSION (old_arg)].value == NULL_TREE))
1171         {
1172           tree def_stmt = SSA_NAME_DEF_STMT (old_arg);
1173           old_arg = GIMPLE_STMT_OPERAND (def_stmt, 1);
1174         }
1175
1176       if (TREE_CODE (old_arg) == SSA_NAME)
1177         /* Reuse a formerly created dereference.  */
1178         new_var = phivn[SSA_NAME_VERSION (old_arg)].value;
1179       else
1180         {
1181           old_arg = TREE_OPERAND (old_arg, 0);
1182           new_var = create_tmp_var (TREE_TYPE (old_arg), NULL);
1183           tmp = build2 (GIMPLE_MODIFY_STMT, void_type_node,
1184                         NULL_TREE, unshare_expr (old_arg));
1185           if (TREE_CODE (TREE_TYPE (old_arg)) == COMPLEX_TYPE
1186               || TREE_CODE (TREE_TYPE (old_arg)) == VECTOR_TYPE)
1187             DECL_GIMPLE_REG_P (new_var) = 1;
1188           add_referenced_var (new_var);
1189           new_var = make_ssa_name (new_var, tmp);
1190           GIMPLE_STMT_OPERAND (tmp, 0) = new_var;
1191
1192           bsi_insert_on_edge (e, tmp);
1193
1194           update_stmt (tmp);
1195           mark_symbols_for_renaming (tmp);
1196         }
1197
1198       add_phi_arg (new_phi, new_var, e);
1199     }
1200
1201   update_stmt (new_phi);
1202
1203   return res;
1204 }
1205
1206 /* Propagate between the phi node arguments of PHI in BB and phi result
1207    users.  For now this matches
1208         # p_2 = PHI <&x, &y>
1209       <Lx>:;
1210         p_3 = p_2;
1211         z_2 = *p_3;
1212    and converts it to
1213         # z_2 = PHI <x, y>
1214       <Lx>:;
1215    Returns true if a transformation was done and edge insertions
1216    need to be committed.  Global data PHIVN and N is used to track
1217    past transformation results.  We need to be especially careful here
1218    with aliasing issues as we are moving memory reads.  */
1219
1220 static bool
1221 propagate_with_phi (basic_block bb, tree phi, struct phiprop_d *phivn, size_t n)
1222 {
1223   tree ptr = PHI_RESULT (phi);
1224   tree use_stmt, res = NULL_TREE;
1225   block_stmt_iterator bsi;
1226   imm_use_iterator ui;
1227   use_operand_p arg_p, use;
1228   ssa_op_iter i;
1229   bool phi_inserted;
1230
1231   if (MTAG_P (SSA_NAME_VAR (ptr))
1232       || !POINTER_TYPE_P (TREE_TYPE (ptr))
1233       || !is_gimple_reg_type (TREE_TYPE (TREE_TYPE (ptr))))
1234     return false;
1235
1236   /* Check if we can "cheaply" dereference all phi arguments.  */
1237   FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
1238     {
1239       tree arg = USE_FROM_PTR (arg_p);
1240       /* Walk the ssa chain until we reach a ssa name we already
1241          created a value for or we reach a definition of the form
1242          ssa_name_n = &var;  */
1243       while (TREE_CODE (arg) == SSA_NAME
1244              && !SSA_NAME_IS_DEFAULT_DEF (arg)
1245              && (SSA_NAME_VERSION (arg) >= n
1246                  || phivn[SSA_NAME_VERSION (arg)].value == NULL_TREE))
1247         {
1248           tree def_stmt = SSA_NAME_DEF_STMT (arg);
1249           if (TREE_CODE (def_stmt) != GIMPLE_MODIFY_STMT)
1250             return false;
1251           arg = GIMPLE_STMT_OPERAND (def_stmt, 1);
1252         }
1253       if ((TREE_CODE (arg) != ADDR_EXPR
1254            /* Avoid to have to decay *&a to a[0] later.  */
1255            || !is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (arg, 0))))
1256           && !(TREE_CODE (arg) == SSA_NAME
1257                && phivn[SSA_NAME_VERSION (arg)].value != NULL_TREE
1258                && phivn_valid_p (phivn, arg, bb)))
1259         return false;
1260     }
1261
1262   /* Find a dereferencing use.  First follow (single use) ssa
1263      copy chains for ptr.  */
1264   while (single_imm_use (ptr, &use, &use_stmt)
1265          && TREE_CODE (use_stmt) == GIMPLE_MODIFY_STMT
1266          && GIMPLE_STMT_OPERAND (use_stmt, 1) == ptr
1267          && TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 0)) == SSA_NAME)
1268     ptr = GIMPLE_STMT_OPERAND (use_stmt, 0);
1269
1270   /* Replace the first dereference of *ptr if there is one and if we
1271      can move the loads to the place of the ptr phi node.  */
1272   phi_inserted = false;
1273   FOR_EACH_IMM_USE_STMT (use_stmt, ui, ptr)
1274     {
1275       ssa_op_iter ui2;
1276       tree vuse;
1277
1278       /* Check whether this is a load of *ptr.  */
1279       if (!(TREE_CODE (use_stmt) == GIMPLE_MODIFY_STMT
1280             && TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 0)) == SSA_NAME 
1281             && TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 1)) == INDIRECT_REF
1282             && TREE_OPERAND (GIMPLE_STMT_OPERAND (use_stmt, 1), 0) == ptr
1283             /* We cannot replace a load that may throw or is volatile.  */
1284             && !tree_can_throw_internal (use_stmt)))
1285         continue;
1286
1287       /* Check if we can move the loads.  The def stmts of all virtual uses
1288          need to be post-dominated by bb.  */
1289       FOR_EACH_SSA_TREE_OPERAND (vuse, use_stmt, ui2, SSA_OP_VUSE)
1290         {
1291           tree def_stmt = SSA_NAME_DEF_STMT (vuse);
1292           if (!SSA_NAME_IS_DEFAULT_DEF (vuse)
1293               && (bb_for_stmt (def_stmt) == bb
1294                   || !dominated_by_p (CDI_DOMINATORS,
1295                                       bb, bb_for_stmt (def_stmt))))
1296             goto next;
1297         }
1298
1299       /* Found a proper dereference.  Insert a phi node if this
1300          is the first load transformation.  */
1301       if (!phi_inserted)
1302         {
1303           res = phiprop_insert_phi (bb, phi, use_stmt, phivn, n);
1304
1305           /* Remember the value we created for *ptr.  */
1306           phivn[SSA_NAME_VERSION (ptr)].value = res;
1307           phivn[SSA_NAME_VERSION (ptr)].vop_stmt = use_stmt;
1308
1309           /* Remove old stmt.  The phi is taken care of by DCE, if we
1310              want to delete it here we also have to delete all intermediate
1311              copies.  */
1312           bsi = bsi_for_stmt (use_stmt);
1313           bsi_remove (&bsi, 0);
1314
1315           phi_inserted = true;
1316         }
1317       else
1318         {
1319           /* Further replacements are easy, just make a copy out of the
1320              load.  */
1321           GIMPLE_STMT_OPERAND (use_stmt, 1) = res;
1322           update_stmt (use_stmt);
1323         }
1324
1325 next:;
1326       /* Continue searching for a proper dereference.  */
1327     }
1328
1329   return phi_inserted;
1330 }
1331
1332 /* Helper walking the dominator tree starting from BB and processing
1333    phi nodes with global data PHIVN and N.  */
1334
1335 static bool
1336 tree_ssa_phiprop_1 (basic_block bb, struct phiprop_d *phivn, size_t n)
1337 {
1338   bool did_something = false; 
1339   basic_block son;
1340   tree phi;
1341
1342   for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1343     did_something |= propagate_with_phi (bb, phi, phivn, n);
1344
1345   for (son = first_dom_son (CDI_DOMINATORS, bb);
1346        son;
1347        son = next_dom_son (CDI_DOMINATORS, son))
1348     did_something |= tree_ssa_phiprop_1 (son, phivn, n);
1349
1350   return did_something;
1351 }
1352
1353 /* Main entry for phiprop pass.  */
1354
1355 static unsigned int
1356 tree_ssa_phiprop (void)
1357 {
1358   struct phiprop_d *phivn;
1359
1360   calculate_dominance_info (CDI_DOMINATORS);
1361
1362   phivn = XCNEWVEC (struct phiprop_d, num_ssa_names);
1363
1364   if (tree_ssa_phiprop_1 (ENTRY_BLOCK_PTR, phivn, num_ssa_names))
1365     bsi_commit_edge_inserts ();
1366
1367   free (phivn);
1368
1369   return 0;
1370 }
1371
1372 static bool
1373 gate_phiprop (void)
1374 {
1375   return 1;
1376 }
1377
1378 struct tree_opt_pass pass_phiprop = {
1379   "phiprop",                    /* name */
1380   gate_phiprop,                 /* gate */
1381   tree_ssa_phiprop,             /* execute */
1382   NULL,                         /* sub */
1383   NULL,                         /* next */
1384   0,                            /* static_pass_number */
1385   TV_TREE_FORWPROP,             /* tv_id */
1386   PROP_cfg | PROP_ssa,          /* properties_required */
1387   0,                            /* properties_provided */
1388   0,                            /* properties_destroyed */
1389   0,                            /* todo_flags_start */
1390   TODO_dump_func
1391   | TODO_ggc_collect
1392   | TODO_update_ssa
1393   | TODO_verify_ssa,            /* todo_flags_finish */
1394   0                             /* letter */
1395 };