OSDN Git Service

2007-04-27 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
375 static void
376 forward_propagate_into_cond (tree cond_expr, tree stmt)
377 {
378   do {
379     tree tmp = NULL_TREE;
380     tree cond = COND_EXPR_COND (cond_expr);
381     tree name, def_stmt, rhs;
382     bool single_use_p;
383
384     /* We can do tree combining on SSA_NAME and comparison expressions.  */
385     if (COMPARISON_CLASS_P (cond)
386         && TREE_CODE (TREE_OPERAND (cond, 0)) == SSA_NAME)
387       {
388         /* For comparisons use the first operand, that is likely to
389            simplify comparisons against constants.  */
390         name = TREE_OPERAND (cond, 0);
391         def_stmt = get_prop_source_stmt (name, false, &single_use_p);
392         if (def_stmt != NULL_TREE
393             && can_propagate_from (def_stmt))
394           {
395             tree op1 = TREE_OPERAND (cond, 1);
396             rhs = GIMPLE_STMT_OPERAND (def_stmt, 1);
397             tmp = combine_cond_expr_cond (TREE_CODE (cond), boolean_type_node,
398                                           fold_convert (TREE_TYPE (op1), rhs),
399                                           op1, !single_use_p);
400           }
401         /* If that wasn't successful, try the second operand.  */
402         if (tmp == NULL_TREE
403             && TREE_CODE (TREE_OPERAND (cond, 1)) == SSA_NAME)
404           {
405             tree op0 = TREE_OPERAND (cond, 0);
406             name = TREE_OPERAND (cond, 1);
407             def_stmt = get_prop_source_stmt (name, false, &single_use_p);
408             if (def_stmt == NULL_TREE
409                 || !can_propagate_from (def_stmt))
410               return;
411
412             rhs = GIMPLE_STMT_OPERAND (def_stmt, 1);
413             tmp = combine_cond_expr_cond (TREE_CODE (cond), boolean_type_node,
414                                           op0,
415                                           fold_convert (TREE_TYPE (op0), rhs),
416                                           !single_use_p);
417           }
418       }
419     else if (TREE_CODE (cond) == SSA_NAME)
420       {
421         name = cond;
422         def_stmt = get_prop_source_stmt (name, true, NULL);
423         if (def_stmt == NULL_TREE
424             || !can_propagate_from (def_stmt))
425           return;
426
427         rhs = GIMPLE_STMT_OPERAND (def_stmt, 1);
428         tmp = combine_cond_expr_cond (NE_EXPR, boolean_type_node, rhs,
429                                       build_int_cst (TREE_TYPE (rhs), 0),
430                                       false);
431       }
432
433     if (tmp)
434       {
435         if (dump_file && tmp)
436           {
437             fprintf (dump_file, "  Replaced '");
438             print_generic_expr (dump_file, cond, 0);
439             fprintf (dump_file, "' with '");
440             print_generic_expr (dump_file, tmp, 0);
441             fprintf (dump_file, "'\n");
442           }
443
444         COND_EXPR_COND (cond_expr) = unshare_expr (tmp);
445         update_stmt (stmt);
446
447         /* Remove defining statements.  */
448         remove_prop_source_from_use (name, NULL);
449
450         /* Continue combining.  */
451         continue;
452       }
453
454     break;
455   } while (1);
456 }
457
458 /* We've just substituted an ADDR_EXPR into stmt.  Update all the 
459    relevant data structures to match.  */
460
461 static void
462 tidy_after_forward_propagate_addr (tree stmt)
463 {
464   /* We may have turned a trapping insn into a non-trapping insn.  */
465   if (maybe_clean_or_replace_eh_stmt (stmt, stmt)
466       && tree_purge_dead_eh_edges (bb_for_stmt (stmt)))
467     cfg_changed = true;
468
469   if (TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == ADDR_EXPR)
470      recompute_tree_invariant_for_addr_expr (GIMPLE_STMT_OPERAND (stmt, 1));
471
472   mark_symbols_for_renaming (stmt);
473 }
474
475 /* DEF_RHS defines LHS which is contains the address of the 0th element
476    in an array.  USE_STMT uses LHS to compute the address of an
477    arbitrary element within the array.  The (variable) byte offset
478    of the element is contained in OFFSET.
479
480    We walk back through the use-def chains of OFFSET to verify that
481    it is indeed computing the offset of an element within the array
482    and extract the index corresponding to the given byte offset.
483
484    We then try to fold the entire address expression into a form
485    &array[index].
486
487    If we are successful, we replace the right hand side of USE_STMT
488    with the new address computation.  */
489
490 static bool
491 forward_propagate_addr_into_variable_array_index (tree offset, tree lhs,
492                                                   tree def_rhs, tree use_stmt)
493 {
494   tree index;
495
496   /* The offset must be defined by a simple GIMPLE_MODIFY_STMT statement.  */
497   if (TREE_CODE (offset) != GIMPLE_MODIFY_STMT)
498     return false;
499
500   /* The RHS of the statement which defines OFFSET must be a gimple
501      cast of another SSA_NAME.  */
502   offset = GIMPLE_STMT_OPERAND (offset, 1);
503   if (!is_gimple_cast (offset))
504     return false;
505
506   offset = TREE_OPERAND (offset, 0);
507   if (TREE_CODE (offset) != SSA_NAME)
508     return false;
509
510   /* Get the defining statement of the offset before type
511      conversion.  */
512   offset = SSA_NAME_DEF_STMT (offset);
513
514   /* The statement which defines OFFSET before type conversion
515      must be a simple GIMPLE_MODIFY_STMT.  */
516   if (TREE_CODE (offset) != GIMPLE_MODIFY_STMT)
517     return false;
518
519   /* The RHS of the statement which defines OFFSET must be a
520      multiplication of an object by the size of the array elements. 
521      This implicitly verifies that the size of the array elements
522      is constant.  */
523   offset = GIMPLE_STMT_OPERAND (offset, 1);
524   if (TREE_CODE (offset) != MULT_EXPR
525       || TREE_CODE (TREE_OPERAND (offset, 1)) != INTEGER_CST
526       || !simple_cst_equal (TREE_OPERAND (offset, 1),
527                             TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (lhs)))))
528     return false;
529
530   /* The first operand to the MULT_EXPR is the desired index.  */
531   index = TREE_OPERAND (offset, 0);
532
533   /* Replace the pointer addition with array indexing.  */
534   GIMPLE_STMT_OPERAND (use_stmt, 1) = unshare_expr (def_rhs);
535   TREE_OPERAND (TREE_OPERAND (GIMPLE_STMT_OPERAND (use_stmt, 1), 0), 1)
536     = index;
537
538   /* That should have created gimple, so there is no need to
539      record information to undo the propagation.  */
540   fold_stmt_inplace (use_stmt);
541   tidy_after_forward_propagate_addr (use_stmt);
542   return true;
543 }
544
545 /* NAME is a SSA_NAME representing DEF_RHS which is of the form
546    ADDR_EXPR <whatever>.
547
548    Try to forward propagate the ADDR_EXPR into the use USE_STMT.
549    Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
550    node or for recovery of array indexing from pointer arithmetic.
551    
552    Return true if the propagation was successful (the propagation can
553    be not totally successful, yet things may have been changed).  */
554
555 static bool
556 forward_propagate_addr_expr_1 (tree name, tree def_rhs, tree use_stmt)
557 {
558   tree lhs, rhs, array_ref;
559
560   /* Strip away any outer COMPONENT_REF/ARRAY_REF nodes from the LHS. 
561      ADDR_EXPR will not appear on the LHS.  */
562   lhs = GIMPLE_STMT_OPERAND (use_stmt, 0);
563   while (handled_component_p (lhs))
564     lhs = TREE_OPERAND (lhs, 0);
565
566   rhs = GIMPLE_STMT_OPERAND (use_stmt, 1);
567
568   /* Now see if the LHS node is an INDIRECT_REF using NAME.  If so, 
569      propagate the ADDR_EXPR into the use of NAME and fold the result.  */
570   if (TREE_CODE (lhs) == INDIRECT_REF && TREE_OPERAND (lhs, 0) == name)
571     {
572       /* This should always succeed in creating gimple, so there is
573          no need to save enough state to undo this propagation.  */
574       TREE_OPERAND (lhs, 0) = unshare_expr (def_rhs);
575       fold_stmt_inplace (use_stmt);
576       tidy_after_forward_propagate_addr (use_stmt);
577
578       /* Continue propagating into the RHS.  */
579     }
580
581   /* Trivial case.  The use statement could be a trivial copy or a
582      useless conversion.  Recurse to the uses of the lhs as copyprop does
583      not copy through differen variant pointers and FRE does not catch
584      all useless conversions.  */
585   else if ((TREE_CODE (lhs) == SSA_NAME
586             && rhs == name)
587            || ((TREE_CODE (rhs) == NOP_EXPR
588                 || TREE_CODE (rhs) == CONVERT_EXPR)
589                && tree_ssa_useless_type_conversion_1 (TREE_TYPE (rhs),
590                                                       TREE_TYPE (def_rhs))))
591     return forward_propagate_addr_expr (lhs, def_rhs);
592
593   /* Strip away any outer COMPONENT_REF, ARRAY_REF or ADDR_EXPR
594      nodes from the RHS.  */
595   while (handled_component_p (rhs)
596          || TREE_CODE (rhs) == ADDR_EXPR)
597     rhs = TREE_OPERAND (rhs, 0);
598
599   /* Now see if the RHS node is an INDIRECT_REF using NAME.  If so, 
600      propagate the ADDR_EXPR into the use of NAME and fold the result.  */
601   if (TREE_CODE (rhs) == INDIRECT_REF && TREE_OPERAND (rhs, 0) == name)
602     {
603       /* This should always succeed in creating gimple, so there is
604          no need to save enough state to undo this propagation.  */
605       TREE_OPERAND (rhs, 0) = unshare_expr (def_rhs);
606       fold_stmt_inplace (use_stmt);
607       tidy_after_forward_propagate_addr (use_stmt);
608       return true;
609     }
610
611   /* The remaining cases are all for turning pointer arithmetic into
612      array indexing.  They only apply when we have the address of
613      element zero in an array.  If that is not the case then there
614      is nothing to do.  */
615   array_ref = TREE_OPERAND (def_rhs, 0);
616   if (TREE_CODE (array_ref) != ARRAY_REF
617       || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref, 0))) != ARRAY_TYPE
618       || !integer_zerop (TREE_OPERAND (array_ref, 1)))
619     return false;
620
621   /* If the use of the ADDR_EXPR must be a PLUS_EXPR, or else there
622      is nothing to do. */
623   if (TREE_CODE (rhs) != PLUS_EXPR)
624     return false;
625
626   /* Try to optimize &x[0] + C where C is a multiple of the size
627      of the elements in X into &x[C/element size].  */
628   if (TREE_OPERAND (rhs, 0) == name
629       && TREE_CODE (TREE_OPERAND (rhs, 1)) == INTEGER_CST)
630     {
631       tree orig = unshare_expr (rhs);
632       TREE_OPERAND (rhs, 0) = unshare_expr (def_rhs);
633
634       /* If folding succeeds, then we have just exposed new variables
635          in USE_STMT which will need to be renamed.  If folding fails,
636          then we need to put everything back the way it was.  */
637       if (fold_stmt_inplace (use_stmt))
638         {
639           tidy_after_forward_propagate_addr (use_stmt);
640           return true;
641         }
642       else
643         {
644           GIMPLE_STMT_OPERAND (use_stmt, 1) = orig;
645           update_stmt (use_stmt);
646           return false;
647         }
648     }
649
650   /* Try to optimize &x[0] + OFFSET where OFFSET is defined by
651      converting a multiplication of an index by the size of the
652      array elements, then the result is converted into the proper
653      type for the arithmetic.  */
654   if (TREE_OPERAND (rhs, 0) == name
655       && TREE_CODE (TREE_OPERAND (rhs, 1)) == SSA_NAME
656       /* Avoid problems with IVopts creating PLUS_EXPRs with a
657          different type than their operands.  */
658       && lang_hooks.types_compatible_p (TREE_TYPE (name), TREE_TYPE (rhs)))
659     {
660       bool res;
661       tree offset_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 1));
662       
663       res = forward_propagate_addr_into_variable_array_index (offset_stmt, lhs,
664                                                               def_rhs, use_stmt);
665       return res;
666     }
667               
668   /* Same as the previous case, except the operands of the PLUS_EXPR
669      were reversed.  */
670   if (TREE_OPERAND (rhs, 1) == name
671       && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
672       /* Avoid problems with IVopts creating PLUS_EXPRs with a
673          different type than their operands.  */
674       && lang_hooks.types_compatible_p (TREE_TYPE (name), TREE_TYPE (rhs)))
675     {
676       bool res;
677       tree offset_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 0));
678       res = forward_propagate_addr_into_variable_array_index (offset_stmt, lhs,
679                                                               def_rhs, use_stmt);
680       return res;
681     }
682   return false;
683 }
684
685 /* STMT is a statement of the form SSA_NAME = ADDR_EXPR <whatever>.
686
687    Try to forward propagate the ADDR_EXPR into all uses of the SSA_NAME.
688    Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
689    node or for recovery of array indexing from pointer arithmetic.
690    Returns true, if all uses have been propagated into.  */
691
692 static bool
693 forward_propagate_addr_expr (tree name, tree rhs)
694 {
695   int stmt_loop_depth = bb_for_stmt (SSA_NAME_DEF_STMT (name))->loop_depth;
696   imm_use_iterator iter;
697   tree use_stmt;
698   bool all = true;
699
700   FOR_EACH_IMM_USE_STMT (use_stmt, iter, name)
701     {
702       bool result;
703
704       /* If the use is not in a simple assignment statement, then
705          there is nothing we can do.  */
706       if (TREE_CODE (use_stmt) != GIMPLE_MODIFY_STMT)
707         {
708           all = false;
709           continue;
710         }
711
712      /* If the use is in a deeper loop nest, then we do not want
713         to propagate the ADDR_EXPR into the loop as that is likely
714         adding expression evaluations into the loop.  */
715       if (bb_for_stmt (use_stmt)->loop_depth > stmt_loop_depth)
716         {
717           all = false;
718           continue;
719         }
720       
721       push_stmt_changes (&use_stmt);
722
723       result = forward_propagate_addr_expr_1 (name, rhs, use_stmt);
724       all &= result;
725
726       pop_stmt_changes (&use_stmt);
727
728       /* Remove intermediate now unused copy and conversion chains.  */
729       if (result
730           && TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 0)) == SSA_NAME
731           && (TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 1)) == SSA_NAME
732               || TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 1)) == NOP_EXPR
733               || TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 1)) == CONVERT_EXPR))
734         {
735           block_stmt_iterator bsi = bsi_for_stmt (use_stmt);
736           release_defs (use_stmt);
737           bsi_remove (&bsi, true);
738         }
739     }
740
741   return all;
742 }
743
744 /* Forward propagate the comparison COND defined in STMT like
745    cond_1 = x CMP y to uses of the form
746      a_1 = (T')cond_1
747      a_1 = !cond_1
748      a_1 = cond_1 != 0
749    Returns true if stmt is now unused.  */
750
751 static bool
752 forward_propagate_comparison (tree cond, tree stmt)
753 {
754   tree name = GIMPLE_STMT_OPERAND (stmt, 0);
755   tree use_stmt, tmp = NULL_TREE;
756
757   /* Don't propagate ssa names that occur in abnormal phis.  */
758   if ((TREE_CODE (TREE_OPERAND (cond, 0)) == SSA_NAME
759        && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (cond, 0)))
760       || (TREE_CODE (TREE_OPERAND (cond, 1)) == SSA_NAME
761           && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (cond, 1))))
762     return false;
763
764   /* Do not un-cse comparisons.  But propagate through copies.  */
765   use_stmt = get_prop_dest_stmt (name, &name);
766   if (use_stmt == NULL_TREE)
767     return false;
768
769   /* Conversion of the condition result to another integral type.  */
770   if (TREE_CODE (use_stmt) == GIMPLE_MODIFY_STMT
771       && (TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 1)) == CONVERT_EXPR
772           || TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 1)) == NOP_EXPR
773           || COMPARISON_CLASS_P (GIMPLE_STMT_OPERAND (use_stmt, 1))
774           || TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 1)) == TRUTH_NOT_EXPR)
775       && INTEGRAL_TYPE_P (TREE_TYPE (GIMPLE_STMT_OPERAND (use_stmt, 0))))
776     {
777       tree lhs = GIMPLE_STMT_OPERAND (use_stmt, 0);
778       tree rhs = GIMPLE_STMT_OPERAND (use_stmt, 1);
779
780       /* We can propagate the condition into a conversion.  */
781       if (TREE_CODE (rhs) == CONVERT_EXPR
782           || TREE_CODE (rhs) == NOP_EXPR)
783         {
784           /* Avoid using fold here as that may create a COND_EXPR with
785              non-boolean condition as canonical form.  */
786           tmp = build2 (TREE_CODE (cond), TREE_TYPE (lhs),
787                         TREE_OPERAND (cond, 0), TREE_OPERAND (cond, 1));
788         }
789       /* We can propagate the condition into X op CST where op
790          is EQ_EXRP or NE_EXPR and CST is either one or zero.  */
791       else if (COMPARISON_CLASS_P (rhs)
792                && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
793                && TREE_CODE (TREE_OPERAND (rhs, 1)) == INTEGER_CST)
794         {
795           enum tree_code code = TREE_CODE (rhs);
796           tree cst = TREE_OPERAND (rhs, 1);
797
798           tmp = combine_cond_expr_cond (code, TREE_TYPE (lhs),
799                                         fold_convert (TREE_TYPE (cst), cond),
800                                         cst, false);
801           if (tmp == NULL_TREE)
802             return false;
803         }
804       /* We can propagate the condition into a statement that
805          computes the logical negation of the comparison result.  */
806       else if (TREE_CODE (rhs) == TRUTH_NOT_EXPR)
807         {
808           tree type = TREE_TYPE (TREE_OPERAND (cond, 0));
809           bool nans = HONOR_NANS (TYPE_MODE (type));
810           enum tree_code code;
811           code = invert_tree_comparison (TREE_CODE (cond), nans);
812           if (code == ERROR_MARK)
813             return false;
814
815           tmp = build2 (code, TREE_TYPE (lhs), TREE_OPERAND (cond, 0),
816                         TREE_OPERAND (cond, 1));
817         }
818       else
819         return false;
820
821       GIMPLE_STMT_OPERAND (use_stmt, 1) = unshare_expr (tmp);
822       update_stmt (use_stmt);
823
824       /* Remove defining statements.  */
825       remove_prop_source_from_use (name, stmt);
826
827       if (dump_file && (dump_flags & TDF_DETAILS))
828         {
829           fprintf (dump_file, "  Replaced '");
830           print_generic_expr (dump_file, rhs, dump_flags);
831           fprintf (dump_file, "' with '");
832           print_generic_expr (dump_file, tmp, dump_flags);
833           fprintf (dump_file, "'\n");
834         }
835
836       return true;
837     }
838
839   return false;
840 }
841
842 /* If we have lhs = ~x (STMT), look and see if earlier we had x = ~y.
843    If so, we can change STMT into lhs = y which can later be copy
844    propagated.  Similarly for negation. 
845
846    This could trivially be formulated as a forward propagation 
847    to immediate uses.  However, we already had an implementation
848    from DOM which used backward propagation via the use-def links.
849
850    It turns out that backward propagation is actually faster as
851    there's less work to do for each NOT/NEG expression we find.
852    Backwards propagation needs to look at the statement in a single
853    backlink.  Forward propagation needs to look at potentially more
854    than one forward link.  */
855
856 static void
857 simplify_not_neg_expr (tree stmt)
858 {
859   tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
860   tree rhs_def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 0));
861
862   /* See if the RHS_DEF_STMT has the same form as our statement.  */
863   if (TREE_CODE (rhs_def_stmt) == GIMPLE_MODIFY_STMT
864       && TREE_CODE (GIMPLE_STMT_OPERAND (rhs_def_stmt, 1)) == TREE_CODE (rhs))
865     {
866       tree rhs_def_operand =
867         TREE_OPERAND (GIMPLE_STMT_OPERAND (rhs_def_stmt, 1), 0);
868
869       /* Verify that RHS_DEF_OPERAND is a suitable SSA_NAME.  */
870       if (TREE_CODE (rhs_def_operand) == SSA_NAME
871           && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs_def_operand))
872         {
873           GIMPLE_STMT_OPERAND (stmt, 1) = rhs_def_operand;
874           update_stmt (stmt);
875         }
876     }
877 }
878
879 /* STMT is a SWITCH_EXPR for which we attempt to find equivalent forms of
880    the condition which we may be able to optimize better.  */
881
882 static void
883 simplify_switch_expr (tree stmt)
884 {
885   tree cond = SWITCH_COND (stmt);
886   tree def, to, ti;
887
888   /* The optimization that we really care about is removing unnecessary
889      casts.  That will let us do much better in propagating the inferred
890      constant at the switch target.  */
891   if (TREE_CODE (cond) == SSA_NAME)
892     {
893       def = SSA_NAME_DEF_STMT (cond);
894       if (TREE_CODE (def) == GIMPLE_MODIFY_STMT)
895         {
896           def = GIMPLE_STMT_OPERAND (def, 1);
897           if (TREE_CODE (def) == NOP_EXPR)
898             {
899               int need_precision;
900               bool fail;
901
902               def = TREE_OPERAND (def, 0);
903
904 #ifdef ENABLE_CHECKING
905               /* ??? Why was Jeff testing this?  We are gimple...  */
906               gcc_assert (is_gimple_val (def));
907 #endif
908
909               to = TREE_TYPE (cond);
910               ti = TREE_TYPE (def);
911
912               /* If we have an extension that preserves value, then we
913                  can copy the source value into the switch.  */
914
915               need_precision = TYPE_PRECISION (ti);
916               fail = false;
917               if (! INTEGRAL_TYPE_P (ti))
918                 fail = true;
919               else if (TYPE_UNSIGNED (to) && !TYPE_UNSIGNED (ti))
920                 fail = true;
921               else if (!TYPE_UNSIGNED (to) && TYPE_UNSIGNED (ti))
922                 need_precision += 1;
923               if (TYPE_PRECISION (to) < need_precision)
924                 fail = true;
925
926               if (!fail)
927                 {
928                   SWITCH_COND (stmt) = def;
929                   update_stmt (stmt);
930                 }
931             }
932         }
933     }
934 }
935
936 /* Main entry point for the forward propagation optimizer.  */
937
938 static unsigned int
939 tree_ssa_forward_propagate_single_use_vars (void)
940 {
941   basic_block bb;
942   unsigned int todoflags = 0;
943
944   cfg_changed = false;
945
946   FOR_EACH_BB (bb)
947     {
948       block_stmt_iterator bsi;
949
950       /* Note we update BSI within the loop as necessary.  */
951       for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
952         {
953           tree stmt = bsi_stmt (bsi);
954
955           /* If this statement sets an SSA_NAME to an address,
956              try to propagate the address into the uses of the SSA_NAME.  */
957           if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
958             {
959               tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
960               tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
961
962
963               if (TREE_CODE (lhs) != SSA_NAME)
964                 {
965                   bsi_next (&bsi);
966                   continue;
967                 }
968
969               if (TREE_CODE (rhs) == ADDR_EXPR)
970                 {
971                   if (forward_propagate_addr_expr (lhs, rhs))
972                     {
973                       release_defs (stmt);
974                       todoflags |= TODO_remove_unused_locals;
975                       bsi_remove (&bsi, true);
976                     }
977                   else
978                     bsi_next (&bsi);
979                 }
980               else if ((TREE_CODE (rhs) == BIT_NOT_EXPR
981                         || TREE_CODE (rhs) == NEGATE_EXPR)
982                        && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
983                 {
984                   simplify_not_neg_expr (stmt);
985                   bsi_next (&bsi);
986                 }
987               else if (TREE_CODE (rhs) == COND_EXPR)
988                 {
989                   forward_propagate_into_cond (rhs, stmt);
990                   bsi_next (&bsi);
991                 }
992               else if (COMPARISON_CLASS_P (rhs))
993                 {
994                   if (forward_propagate_comparison (rhs, stmt))
995                     {
996                       release_defs (stmt);
997                       todoflags |= TODO_remove_unused_locals;
998                       bsi_remove (&bsi, true);
999                     }
1000                   else
1001                     bsi_next (&bsi);
1002                 }
1003               else
1004                 bsi_next (&bsi);
1005             }
1006           else if (TREE_CODE (stmt) == SWITCH_EXPR)
1007             {
1008               simplify_switch_expr (stmt);
1009               bsi_next (&bsi);
1010             }
1011           else if (TREE_CODE (stmt) == COND_EXPR)
1012             {
1013               forward_propagate_into_cond (stmt, stmt);
1014               bsi_next (&bsi);
1015             }
1016           else
1017             bsi_next (&bsi);
1018         }
1019     }
1020
1021   if (cfg_changed)
1022     todoflags |= TODO_cleanup_cfg;
1023   return todoflags;
1024 }
1025
1026
1027 static bool
1028 gate_forwprop (void)
1029 {
1030   return 1;
1031 }
1032
1033 struct tree_opt_pass pass_forwprop = {
1034   "forwprop",                   /* name */
1035   gate_forwprop,                /* gate */
1036   tree_ssa_forward_propagate_single_use_vars,   /* execute */
1037   NULL,                         /* sub */
1038   NULL,                         /* next */
1039   0,                            /* static_pass_number */
1040   TV_TREE_FORWPROP,             /* tv_id */
1041   PROP_cfg | PROP_ssa,          /* properties_required */
1042   0,                            /* properties_provided */
1043   0,                            /* properties_destroyed */
1044   0,                            /* todo_flags_start */
1045   TODO_dump_func
1046   | TODO_ggc_collect
1047   | TODO_update_ssa
1048   | TODO_verify_ssa,            /* todo_flags_finish */
1049   0                             /* letter */
1050 };
1051
1052
1053 /* Structure to keep track of the value of a dereferenced PHI result
1054    and the set of virtual operands used for that dereference.  */
1055
1056 struct phiprop_d
1057 {
1058   tree value;
1059   tree vop_stmt;
1060 };
1061
1062 /* Verify if the value recorded for NAME in PHIVN is still valid at
1063    the start of basic block BB.  */
1064
1065 static bool
1066 phivn_valid_p (struct phiprop_d *phivn, tree name, basic_block bb)
1067 {
1068   tree vop_stmt = phivn[SSA_NAME_VERSION (name)].vop_stmt;
1069   ssa_op_iter ui;
1070   tree vuse;
1071
1072   /* The def stmts of all virtual uses need to be post-dominated
1073      by bb.  */
1074   FOR_EACH_SSA_TREE_OPERAND (vuse, vop_stmt, ui, SSA_OP_VUSE)
1075     {
1076       tree use_stmt;
1077       imm_use_iterator ui2;
1078       bool ok = true;
1079
1080       FOR_EACH_IMM_USE_STMT (use_stmt, ui2, vuse)
1081         {
1082           /* If BB does not dominate a VDEF, the value is invalid.  */
1083           if (((TREE_CODE (use_stmt) == GIMPLE_MODIFY_STMT
1084                 && !ZERO_SSA_OPERANDS (use_stmt, SSA_OP_VDEF))
1085                || TREE_CODE (use_stmt) == PHI_NODE)
1086               && !dominated_by_p (CDI_DOMINATORS, bb_for_stmt (use_stmt), bb))
1087             {
1088               ok = false;
1089               BREAK_FROM_IMM_USE_STMT (ui2);
1090             }
1091         }
1092       if (!ok)
1093         return false;
1094     }
1095
1096   return true;
1097 }
1098
1099 /* Insert a new phi node for the dereference of PHI at basic_block
1100    BB with the virtual operands from USE_STMT.  */
1101
1102 static tree
1103 phiprop_insert_phi (basic_block bb, tree phi, tree use_stmt,
1104                     struct phiprop_d *phivn, size_t n)
1105 {
1106   tree res, new_phi;
1107   edge_iterator ei;
1108   edge e;
1109
1110   /* Build a new PHI node to replace the definition of
1111      the indirect reference lhs.  */
1112   res = GIMPLE_STMT_OPERAND (use_stmt, 0);
1113   SSA_NAME_DEF_STMT (res) = new_phi = create_phi_node (res, bb);
1114
1115   /* Add PHI arguments for each edge inserting loads of the
1116      addressable operands.  */
1117   FOR_EACH_EDGE (e, ei, bb->preds)
1118     {
1119       tree old_arg, new_var, tmp;
1120
1121       old_arg = PHI_ARG_DEF_FROM_EDGE (phi, e);
1122       while (TREE_CODE (old_arg) == SSA_NAME
1123              && (SSA_NAME_VERSION (old_arg) >= n
1124                  || phivn[SSA_NAME_VERSION (old_arg)].value == NULL_TREE))
1125         {
1126           tree def_stmt = SSA_NAME_DEF_STMT (old_arg);
1127           old_arg = GIMPLE_STMT_OPERAND (def_stmt, 1);
1128         }
1129
1130       if (TREE_CODE (old_arg) == SSA_NAME)
1131         /* Reuse a formely created dereference.  */
1132         new_var = phivn[SSA_NAME_VERSION (old_arg)].value;
1133       else
1134         {
1135           old_arg = TREE_OPERAND (old_arg, 0);
1136           new_var = create_tmp_var (TREE_TYPE (old_arg), NULL);
1137           tmp = build2 (GIMPLE_MODIFY_STMT, void_type_node,
1138                         NULL_TREE, unshare_expr (old_arg));
1139           if (TREE_CODE (TREE_TYPE (old_arg)) == COMPLEX_TYPE
1140               || TREE_CODE (TREE_TYPE (old_arg)) == VECTOR_TYPE)
1141             DECL_GIMPLE_REG_P (new_var) = 1;
1142           add_referenced_var (new_var);
1143           new_var = make_ssa_name (new_var, tmp);
1144           GIMPLE_STMT_OPERAND (tmp, 0) = new_var;
1145
1146           bsi_insert_on_edge (e, tmp);
1147
1148           update_stmt (tmp);
1149           mark_symbols_for_renaming (tmp);
1150         }
1151
1152       add_phi_arg (new_phi, new_var, e);
1153     }
1154
1155   update_stmt (new_phi);
1156
1157   return res;
1158 }
1159
1160 /* Propagate between the phi node arguments of PHI in BB and phi result
1161    users.  For now this matches
1162         # p_2 = PHI <&x, &y>
1163       <Lx>:;
1164         p_3 = p_2;
1165         z_2 = *p_3;
1166    and converts it to
1167         # z_2 = PHI <x, y>
1168       <Lx>:;
1169    Returns true if a transformation was done and edge insertions
1170    need to be committed.  Global data PHIVN and N is used to track
1171    past transformation results.  We need to be especially careful here
1172    with aliasing issues as we are moving memory reads.  */
1173
1174 static bool
1175 propagate_with_phi (basic_block bb, tree phi, struct phiprop_d *phivn, size_t n)
1176 {
1177   tree ptr = PHI_RESULT (phi);
1178   tree use_stmt, res = NULL_TREE;
1179   block_stmt_iterator bsi;
1180   imm_use_iterator ui;
1181   use_operand_p arg_p, use;
1182   ssa_op_iter i;
1183   bool phi_inserted;
1184
1185   if (MTAG_P (SSA_NAME_VAR (ptr))
1186       || !POINTER_TYPE_P (TREE_TYPE (ptr))
1187       || !is_gimple_reg_type (TREE_TYPE (TREE_TYPE (ptr))))
1188     return false;
1189
1190   /* Check if we can "cheaply" dereference all phi arguments.  */
1191   FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
1192     {
1193       tree arg = USE_FROM_PTR (arg_p);
1194       /* Walk the ssa chain until we reach a ssa name we already
1195          created a value for or we reach a definition of the form
1196          ssa_name_n = &var;  */
1197       while (TREE_CODE (arg) == SSA_NAME
1198              && !SSA_NAME_IS_DEFAULT_DEF (arg)
1199              && (SSA_NAME_VERSION (arg) >= n
1200                  || phivn[SSA_NAME_VERSION (arg)].value == NULL_TREE))
1201         {
1202           tree def_stmt = SSA_NAME_DEF_STMT (arg);
1203           if (TREE_CODE (def_stmt) != GIMPLE_MODIFY_STMT)
1204             return false;
1205           arg = GIMPLE_STMT_OPERAND (def_stmt, 1);
1206         }
1207       if ((TREE_CODE (arg) != ADDR_EXPR
1208            /* Avoid to have to decay *&a to a[0] later.  */
1209            || !is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (arg, 0))))
1210           && !(TREE_CODE (arg) == SSA_NAME
1211                && phivn[SSA_NAME_VERSION (arg)].value != NULL_TREE
1212                && phivn_valid_p (phivn, arg, bb)))
1213         return false;
1214     }
1215
1216   /* Find a dereferencing use.  First follow (single use) ssa
1217      copy chains for ptr.  */
1218   while (single_imm_use (ptr, &use, &use_stmt)
1219          && TREE_CODE (use_stmt) == GIMPLE_MODIFY_STMT
1220          && GIMPLE_STMT_OPERAND (use_stmt, 1) == ptr
1221          && TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 0)) == SSA_NAME)
1222     ptr = GIMPLE_STMT_OPERAND (use_stmt, 0);
1223
1224   /* Replace the first dereference of *ptr if there is one and if we
1225      can move the loads to the place of the ptr phi node.  */
1226   phi_inserted = false;
1227   FOR_EACH_IMM_USE_STMT (use_stmt, ui, ptr)
1228     {
1229       ssa_op_iter ui2;
1230       tree vuse;
1231
1232       /* Check whether this is a load of *ptr.  */
1233       if (!(TREE_CODE (use_stmt) == GIMPLE_MODIFY_STMT
1234             && TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 0)) == SSA_NAME 
1235             && TREE_CODE (GIMPLE_STMT_OPERAND (use_stmt, 1)) == INDIRECT_REF
1236             && TREE_OPERAND (GIMPLE_STMT_OPERAND (use_stmt, 1), 0) == ptr
1237             /* We cannot replace a load that may throw or is volatile.  */
1238             && !tree_can_throw_internal (use_stmt)))
1239         continue;
1240
1241       /* Check if we can move the loads.  The def stmts of all virtual uses
1242          need to be post-dominated by bb.  */
1243       FOR_EACH_SSA_TREE_OPERAND (vuse, use_stmt, ui2, SSA_OP_VUSE)
1244         {
1245           tree def_stmt = SSA_NAME_DEF_STMT (vuse);
1246           if (!SSA_NAME_IS_DEFAULT_DEF (vuse)
1247               && (bb_for_stmt (def_stmt) == bb
1248                   || !dominated_by_p (CDI_DOMINATORS,
1249                                       bb, bb_for_stmt (def_stmt))))
1250             goto next;
1251         }
1252
1253       /* Found a proper dereference.  Insert a phi node if this
1254          is the first load transformation.  */
1255       if (!phi_inserted)
1256         {
1257           res = phiprop_insert_phi (bb, phi, use_stmt, phivn, n);
1258
1259           /* Remember the value we created for *ptr.  */
1260           phivn[SSA_NAME_VERSION (ptr)].value = res;
1261           phivn[SSA_NAME_VERSION (ptr)].vop_stmt = use_stmt;
1262
1263           /* Remove old stmt.  The phi is taken care of by DCE, if we
1264              want to delete it here we also have to delete all intermediate
1265              copies.  */
1266           bsi = bsi_for_stmt (use_stmt);
1267           bsi_remove (&bsi, 0);
1268
1269           phi_inserted = true;
1270         }
1271       else
1272         {
1273           /* Further replacements are easy, just make a copy out of the
1274              load.  */
1275           GIMPLE_STMT_OPERAND (use_stmt, 1) = res;
1276           update_stmt (use_stmt);
1277         }
1278
1279 next:;
1280       /* Continue searching for a proper dereference.  */
1281     }
1282
1283   return phi_inserted;
1284 }
1285
1286 /* Helper walking the dominator tree starting from BB and processing
1287    phi nodes with global data PHIVN and N.  */
1288
1289 static bool
1290 tree_ssa_phiprop_1 (basic_block bb, struct phiprop_d *phivn, size_t n)
1291 {
1292   bool did_something = false; 
1293   basic_block son;
1294   tree phi;
1295
1296   for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1297     did_something |= propagate_with_phi (bb, phi, phivn, n);
1298
1299   for (son = first_dom_son (CDI_DOMINATORS, bb);
1300        son;
1301        son = next_dom_son (CDI_DOMINATORS, son))
1302     did_something |= tree_ssa_phiprop_1 (son, phivn, n);
1303
1304   return did_something;
1305 }
1306
1307 /* Main entry for phiprop pass.  */
1308
1309 static unsigned int
1310 tree_ssa_phiprop (void)
1311 {
1312   struct phiprop_d *phivn;
1313
1314   calculate_dominance_info (CDI_DOMINATORS);
1315
1316   phivn = XCNEWVEC (struct phiprop_d, num_ssa_names);
1317
1318   if (tree_ssa_phiprop_1 (ENTRY_BLOCK_PTR, phivn, num_ssa_names))
1319     bsi_commit_edge_inserts ();
1320
1321   free (phivn);
1322
1323   return 0;
1324 }
1325
1326 static bool
1327 gate_phiprop (void)
1328 {
1329   return 1;
1330 }
1331
1332 struct tree_opt_pass pass_phiprop = {
1333   "phiprop",                    /* name */
1334   gate_phiprop,                 /* gate */
1335   tree_ssa_phiprop,             /* execute */
1336   NULL,                         /* sub */
1337   NULL,                         /* next */
1338   0,                            /* static_pass_number */
1339   TV_TREE_FORWPROP,             /* tv_id */
1340   PROP_cfg | PROP_ssa,          /* properties_required */
1341   0,                            /* properties_provided */
1342   0,                            /* properties_destroyed */
1343   0,                            /* todo_flags_start */
1344   TODO_dump_func
1345   | TODO_ggc_collect
1346   | TODO_update_ssa
1347   | TODO_verify_ssa,            /* todo_flags_finish */
1348   0                             /* letter */
1349 };