OSDN Git Service

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