OSDN Git Service

2011-08-30 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, 2007, 2008, 2009, 2010, 2011
3    Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "tm_p.h"
27 #include "basic-block.h"
28 #include "timevar.h"
29 #include "gimple-pretty-print.h"
30 #include "tree-flow.h"
31 #include "tree-pass.h"
32 #include "tree-dump.h"
33 #include "langhooks.h"
34 #include "flags.h"
35 #include "gimple.h"
36 #include "expr.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    One class of common cases we handle is forward propagating a single use
44    variable into a COND_EXPR.
45
46      bb0:
47        x = a COND b;
48        if (x) goto ... else goto ...
49
50    Will be transformed into:
51
52      bb0:
53        if (a COND b) goto ... else goto ...
54
55    Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
56
57    Or (assuming c1 and c2 are constants):
58
59      bb0:
60        x = a + c1;
61        if (x EQ/NEQ c2) goto ... else goto ...
62
63    Will be transformed into:
64
65      bb0:
66         if (a EQ/NEQ (c2 - c1)) goto ... else goto ...
67
68    Similarly for x = a - c1.
69
70    Or
71
72      bb0:
73        x = !a
74        if (x) goto ... else goto ...
75
76    Will be transformed into:
77
78      bb0:
79         if (a == 0) goto ... else goto ...
80
81    Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
82    For these cases, we propagate A into all, possibly more than one,
83    COND_EXPRs that use X.
84
85    Or
86
87      bb0:
88        x = (typecast) a
89        if (x) goto ... else goto ...
90
91    Will be transformed into:
92
93      bb0:
94         if (a != 0) goto ... else goto ...
95
96    (Assuming a is an integral type and x is a boolean or x is an
97     integral and a is a boolean.)
98
99    Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
100    For these cases, we propagate A into all, possibly more than one,
101    COND_EXPRs that use X.
102
103    In addition to eliminating the variable and the statement which assigns
104    a value to the variable, we may be able to later thread the jump without
105    adding insane complexity in the dominator optimizer.
106
107    Also note these transformations can cascade.  We handle this by having
108    a worklist of COND_EXPR statements to examine.  As we make a change to
109    a statement, we put it back on the worklist to examine on the next
110    iteration of the main loop.
111
112    A second class of propagation opportunities arises for ADDR_EXPR
113    nodes.
114
115      ptr = &x->y->z;
116      res = *ptr;
117
118    Will get turned into
119
120      res = x->y->z;
121
122    Or
123      ptr = (type1*)&type2var;
124      res = *ptr
125
126    Will get turned into (if type1 and type2 are the same size
127    and neither have volatile on them):
128      res = VIEW_CONVERT_EXPR<type1>(type2var)
129
130    Or
131
132      ptr = &x[0];
133      ptr2 = ptr + <constant>;
134
135    Will get turned into
136
137      ptr2 = &x[constant/elementsize];
138
139   Or
140
141      ptr = &x[0];
142      offset = index * element_size;
143      offset_p = (pointer) offset;
144      ptr2 = ptr + offset_p
145
146   Will get turned into:
147
148      ptr2 = &x[index];
149
150   Or
151     ssa = (int) decl
152     res = ssa & 1
153
154   Provided that decl has known alignment >= 2, will get turned into
155
156     res = 0
157
158   We also propagate casts into SWITCH_EXPR and COND_EXPR conditions to
159   allow us to remove the cast and {NOT_EXPR,NEG_EXPR} into a subsequent
160   {NOT_EXPR,NEG_EXPR}.
161
162    This will (of course) be extended as other needs arise.  */
163
164 static bool forward_propagate_addr_expr (tree name, tree rhs);
165
166 /* Set to true if we delete EH edges during the optimization.  */
167 static bool cfg_changed;
168
169 static tree rhs_to_tree (tree type, gimple stmt);
170
171 /* Get the next statement we can propagate NAME's value into skipping
172    trivial copies.  Returns the statement that is suitable as a
173    propagation destination or NULL_TREE if there is no such one.
174    This only returns destinations in a single-use chain.  FINAL_NAME_P
175    if non-NULL is written to the ssa name that represents the use.  */
176
177 static gimple
178 get_prop_dest_stmt (tree name, tree *final_name_p)
179 {
180   use_operand_p use;
181   gimple use_stmt;
182
183   do {
184     /* If name has multiple uses, bail out.  */
185     if (!single_imm_use (name, &use, &use_stmt))
186       return NULL;
187
188     /* If this is not a trivial copy, we found it.  */
189     if (!gimple_assign_ssa_name_copy_p (use_stmt)
190         || gimple_assign_rhs1 (use_stmt) != name)
191       break;
192
193     /* Continue searching uses of the copy destination.  */
194     name = gimple_assign_lhs (use_stmt);
195   } while (1);
196
197   if (final_name_p)
198     *final_name_p = name;
199
200   return use_stmt;
201 }
202
203 /* Get the statement we can propagate from into NAME skipping
204    trivial copies.  Returns the statement which defines the
205    propagation source or NULL_TREE if there is no such one.
206    If SINGLE_USE_ONLY is set considers only sources which have
207    a single use chain up to NAME.  If SINGLE_USE_P is non-null,
208    it is set to whether the chain to NAME is a single use chain
209    or not.  SINGLE_USE_P is not written to if SINGLE_USE_ONLY is set.  */
210
211 static gimple
212 get_prop_source_stmt (tree name, bool single_use_only, bool *single_use_p)
213 {
214   bool single_use = true;
215
216   do {
217     gimple def_stmt = SSA_NAME_DEF_STMT (name);
218
219     if (!has_single_use (name))
220       {
221         single_use = false;
222         if (single_use_only)
223           return NULL;
224       }
225
226     /* If name is defined by a PHI node or is the default def, bail out.  */
227     if (!is_gimple_assign (def_stmt))
228       return NULL;
229
230     /* If def_stmt is not a simple copy, we possibly found it.  */
231     if (!gimple_assign_ssa_name_copy_p (def_stmt))
232       {
233         tree rhs;
234
235         if (!single_use_only && single_use_p)
236           *single_use_p = single_use;
237
238         /* We can look through pointer conversions in the search
239            for a useful stmt for the comparison folding.  */
240         rhs = gimple_assign_rhs1 (def_stmt);
241         if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
242             && TREE_CODE (rhs) == SSA_NAME
243             && POINTER_TYPE_P (TREE_TYPE (gimple_assign_lhs (def_stmt)))
244             && POINTER_TYPE_P (TREE_TYPE (rhs)))
245           name = rhs;
246         else
247           return def_stmt;
248       }
249     else
250       {
251         /* Continue searching the def of the copy source name.  */
252         name = gimple_assign_rhs1 (def_stmt);
253       }
254   } while (1);
255 }
256
257 /* Checks if the destination ssa name in DEF_STMT can be used as
258    propagation source.  Returns true if so, otherwise false.  */
259
260 static bool
261 can_propagate_from (gimple def_stmt)
262 {
263   gcc_assert (is_gimple_assign (def_stmt));
264
265   /* If the rhs has side-effects we cannot propagate from it.  */
266   if (gimple_has_volatile_ops (def_stmt))
267     return false;
268
269   /* If the rhs is a load we cannot propagate from it.  */
270   if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_reference
271       || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_declaration)
272     return false;
273
274   /* Constants can be always propagated.  */
275   if (gimple_assign_single_p (def_stmt)
276       && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
277     return true;
278
279   /* We cannot propagate ssa names that occur in abnormal phi nodes.  */
280   if (stmt_references_abnormal_ssa_name (def_stmt))
281     return false;
282
283   /* If the definition is a conversion of a pointer to a function type,
284      then we can not apply optimizations as some targets require
285      function pointers to be canonicalized and in this case this
286      optimization could eliminate a necessary canonicalization.  */
287   if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
288     {
289       tree rhs = gimple_assign_rhs1 (def_stmt);
290       if (POINTER_TYPE_P (TREE_TYPE (rhs))
291           && TREE_CODE (TREE_TYPE (TREE_TYPE (rhs))) == FUNCTION_TYPE)
292         return false;
293     }
294
295   return true;
296 }
297
298 /* Remove a chain of dead statements starting at the definition of
299    NAME.  The chain is linked via the first operand of the defining statements.
300    If NAME was replaced in its only use then this function can be used
301    to clean up dead stmts.  The function handles already released SSA
302    names gracefully.
303    Returns true if cleanup-cfg has to run.  */
304
305 static bool
306 remove_prop_source_from_use (tree name)
307 {
308   gimple_stmt_iterator gsi;
309   gimple stmt;
310   bool cfg_changed = false;
311
312   do {
313     basic_block bb;
314
315     if (SSA_NAME_IN_FREE_LIST (name)
316         || SSA_NAME_IS_DEFAULT_DEF (name)
317         || !has_zero_uses (name))
318       return cfg_changed;
319
320     stmt = SSA_NAME_DEF_STMT (name);
321     if (gimple_code (stmt) == GIMPLE_PHI
322         || gimple_has_side_effects (stmt))
323       return cfg_changed;
324
325     bb = gimple_bb (stmt);
326     gsi = gsi_for_stmt (stmt);
327     unlink_stmt_vdef (stmt);
328     gsi_remove (&gsi, true);
329     release_defs (stmt);
330     cfg_changed |= gimple_purge_dead_eh_edges (bb);
331
332     name = is_gimple_assign (stmt) ? gimple_assign_rhs1 (stmt) : NULL_TREE;
333   } while (name && TREE_CODE (name) == SSA_NAME);
334
335   return cfg_changed;
336 }
337
338 /* Return the rhs of a gimple_assign STMT in a form of a single tree,
339    converted to type TYPE.
340
341    This should disappear, but is needed so we can combine expressions and use
342    the fold() interfaces. Long term, we need to develop folding and combine
343    routines that deal with gimple exclusively . */
344
345 static tree
346 rhs_to_tree (tree type, gimple stmt)
347 {
348   location_t loc = gimple_location (stmt);
349   enum tree_code code = gimple_assign_rhs_code (stmt);
350   if (get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS)
351     return fold_build3_loc (loc, code, type, gimple_assign_rhs1 (stmt),
352                             gimple_assign_rhs2 (stmt),
353                             gimple_assign_rhs3 (stmt));
354   else if (get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS)
355     return fold_build2_loc (loc, code, type, gimple_assign_rhs1 (stmt),
356                         gimple_assign_rhs2 (stmt));
357   else if (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS)
358     return build1 (code, type, gimple_assign_rhs1 (stmt));
359   else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
360     return gimple_assign_rhs1 (stmt);
361   else
362     gcc_unreachable ();
363 }
364
365 /* Combine OP0 CODE OP1 in the context of a COND_EXPR.  Returns
366    the folded result in a form suitable for COND_EXPR_COND or
367    NULL_TREE, if there is no suitable simplified form.  If
368    INVARIANT_ONLY is true only gimple_min_invariant results are
369    considered simplified.  */
370
371 static tree
372 combine_cond_expr_cond (gimple stmt, enum tree_code code, tree type,
373                         tree op0, tree op1, bool invariant_only)
374 {
375   tree t;
376
377   gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
378
379   fold_defer_overflow_warnings ();
380   t = fold_binary_loc (gimple_location (stmt), code, type, op0, op1);
381   if (!t)
382     {
383       fold_undefer_overflow_warnings (false, NULL, 0);
384       return NULL_TREE;
385     }
386
387   /* Require that we got a boolean type out if we put one in.  */
388   gcc_assert (TREE_CODE (TREE_TYPE (t)) == TREE_CODE (type));
389
390   /* Canonicalize the combined condition for use in a COND_EXPR.  */
391   t = canonicalize_cond_expr_cond (t);
392
393   /* Bail out if we required an invariant but didn't get one.  */
394   if (!t || (invariant_only && !is_gimple_min_invariant (t)))
395     {
396       fold_undefer_overflow_warnings (false, NULL, 0);
397       return NULL_TREE;
398     }
399
400   fold_undefer_overflow_warnings (!gimple_no_warning_p (stmt), stmt, 0);
401
402   return t;
403 }
404
405 /* Combine the comparison OP0 CODE OP1 at LOC with the defining statements
406    of its operand.  Return a new comparison tree or NULL_TREE if there
407    were no simplifying combines.  */
408
409 static tree
410 forward_propagate_into_comparison_1 (gimple stmt,
411                                      enum tree_code code, tree type,
412                                      tree op0, tree op1)
413 {
414   tree tmp = NULL_TREE;
415   tree rhs0 = NULL_TREE, rhs1 = NULL_TREE;
416   bool single_use0_p = false, single_use1_p = false;
417
418   /* For comparisons use the first operand, that is likely to
419      simplify comparisons against constants.  */
420   if (TREE_CODE (op0) == SSA_NAME)
421     {
422       gimple def_stmt = get_prop_source_stmt (op0, false, &single_use0_p);
423       if (def_stmt && can_propagate_from (def_stmt))
424         {
425           rhs0 = rhs_to_tree (TREE_TYPE (op1), def_stmt);
426           tmp = combine_cond_expr_cond (stmt, code, type,
427                                         rhs0, op1, !single_use0_p);
428           if (tmp)
429             return tmp;
430         }
431     }
432
433   /* If that wasn't successful, try the second operand.  */
434   if (TREE_CODE (op1) == SSA_NAME)
435     {
436       gimple def_stmt = get_prop_source_stmt (op1, false, &single_use1_p);
437       if (def_stmt && can_propagate_from (def_stmt))
438         {
439           rhs1 = rhs_to_tree (TREE_TYPE (op0), def_stmt);
440           tmp = combine_cond_expr_cond (stmt, code, type,
441                                         op0, rhs1, !single_use1_p);
442           if (tmp)
443             return tmp;
444         }
445     }
446
447   /* If that wasn't successful either, try both operands.  */
448   if (rhs0 != NULL_TREE
449       && rhs1 != NULL_TREE)
450     tmp = combine_cond_expr_cond (stmt, code, type,
451                                   rhs0, rhs1,
452                                   !(single_use0_p && single_use1_p));
453
454   return tmp;
455 }
456
457 /* Propagate from the ssa name definition statements of the assignment
458    from a comparison at *GSI into the conditional if that simplifies it.
459    Returns 1 if the stmt was modified and 2 if the CFG needs cleanup,
460    otherwise returns 0.  */
461
462 static int 
463 forward_propagate_into_comparison (gimple_stmt_iterator *gsi)
464 {
465   gimple stmt = gsi_stmt (*gsi);
466   tree tmp;
467   bool cfg_changed = false;
468   tree rhs1 = gimple_assign_rhs1 (stmt);
469   tree rhs2 = gimple_assign_rhs2 (stmt);
470
471   /* Combine the comparison with defining statements.  */
472   tmp = forward_propagate_into_comparison_1 (stmt,
473                                              gimple_assign_rhs_code (stmt),
474                                              TREE_TYPE
475                                                (gimple_assign_lhs (stmt)),
476                                              rhs1, rhs2);
477   if (tmp)
478     {
479       gimple_assign_set_rhs_from_tree (gsi, tmp);
480       fold_stmt_inplace (stmt);
481       update_stmt (stmt);
482
483       if (TREE_CODE (rhs1) == SSA_NAME)
484         cfg_changed |= remove_prop_source_from_use (rhs1);
485       if (TREE_CODE (rhs2) == SSA_NAME)
486         cfg_changed |= remove_prop_source_from_use (rhs2);
487       return cfg_changed ? 2 : 1;
488     }
489
490   return 0;
491 }
492
493 /* Propagate from the ssa name definition statements of COND_EXPR
494    in GIMPLE_COND statement STMT into the conditional if that simplifies it.
495    Returns zero if no statement was changed, one if there were
496    changes and two if cfg_cleanup needs to run.
497
498    This must be kept in sync with forward_propagate_into_cond.  */
499
500 static int
501 forward_propagate_into_gimple_cond (gimple stmt)
502 {
503   tree tmp;
504   enum tree_code code = gimple_cond_code (stmt);
505   bool cfg_changed = false;
506   tree rhs1 = gimple_cond_lhs (stmt);
507   tree rhs2 = gimple_cond_rhs (stmt);
508
509   /* We can do tree combining on SSA_NAME and comparison expressions.  */
510   if (TREE_CODE_CLASS (gimple_cond_code (stmt)) != tcc_comparison)
511     return 0;
512
513   tmp = forward_propagate_into_comparison_1 (stmt, code,
514                                              boolean_type_node,
515                                              rhs1, rhs2);
516   if (tmp)
517     {
518       if (dump_file && tmp)
519         {
520           fprintf (dump_file, "  Replaced '");
521           print_gimple_expr (dump_file, stmt, 0, 0);
522           fprintf (dump_file, "' with '");
523           print_generic_expr (dump_file, tmp, 0);
524           fprintf (dump_file, "'\n");
525         }
526
527       gimple_cond_set_condition_from_tree (stmt, unshare_expr (tmp));
528       update_stmt (stmt);
529
530       if (TREE_CODE (rhs1) == SSA_NAME)
531         cfg_changed |= remove_prop_source_from_use (rhs1);
532       if (TREE_CODE (rhs2) == SSA_NAME)
533         cfg_changed |= remove_prop_source_from_use (rhs2);
534       return (cfg_changed || is_gimple_min_invariant (tmp)) ? 2 : 1;
535     }
536
537   return 0;
538 }
539
540
541 /* Propagate from the ssa name definition statements of COND_EXPR
542    in the rhs of statement STMT into the conditional if that simplifies it.
543    Returns zero if no statement was changed, one if there were
544    changes and two if cfg_cleanup needs to run.
545
546    This must be kept in sync with forward_propagate_into_gimple_cond.  */
547
548 static int
549 forward_propagate_into_cond (gimple_stmt_iterator *gsi_p)
550 {
551   gimple stmt = gsi_stmt (*gsi_p);
552   tree tmp = NULL_TREE;
553   tree cond = gimple_assign_rhs1 (stmt);
554
555   /* We can do tree combining on SSA_NAME and comparison expressions.  */
556   if (COMPARISON_CLASS_P (cond))
557     tmp = forward_propagate_into_comparison_1 (stmt, TREE_CODE (cond),
558                                                boolean_type_node,
559                                                TREE_OPERAND (cond, 0),
560                                                TREE_OPERAND (cond, 1));
561   else if (TREE_CODE (cond) == SSA_NAME)
562     {
563       tree name = cond, rhs0;
564       gimple def_stmt = get_prop_source_stmt (name, true, NULL);
565       if (!def_stmt || !can_propagate_from (def_stmt))
566         return 0;
567
568       rhs0 = gimple_assign_rhs1 (def_stmt);
569       tmp = combine_cond_expr_cond (stmt, NE_EXPR, boolean_type_node, rhs0,
570                                     build_int_cst (TREE_TYPE (rhs0), 0),
571                                     false);
572     }
573
574   if (tmp)
575     {
576       if (dump_file && tmp)
577         {
578           fprintf (dump_file, "  Replaced '");
579           print_generic_expr (dump_file, cond, 0);
580           fprintf (dump_file, "' with '");
581           print_generic_expr (dump_file, tmp, 0);
582           fprintf (dump_file, "'\n");
583         }
584
585       gimple_assign_set_rhs_from_tree (gsi_p, unshare_expr (tmp));
586       stmt = gsi_stmt (*gsi_p);
587       update_stmt (stmt);
588
589       return is_gimple_min_invariant (tmp) ? 2 : 1;
590     }
591
592   return 0;
593 }
594
595 /* We've just substituted an ADDR_EXPR into stmt.  Update all the
596    relevant data structures to match.  */
597
598 static void
599 tidy_after_forward_propagate_addr (gimple stmt)
600 {
601   /* We may have turned a trapping insn into a non-trapping insn.  */
602   if (maybe_clean_or_replace_eh_stmt (stmt, stmt)
603       && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
604     cfg_changed = true;
605
606   if (TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
607      recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
608 }
609
610 /* DEF_RHS contains the address of the 0th element in an array.
611    USE_STMT uses type of DEF_RHS to compute the address of an
612    arbitrary element within the array.  The (variable) byte offset
613    of the element is contained in OFFSET.
614
615    We walk back through the use-def chains of OFFSET to verify that
616    it is indeed computing the offset of an element within the array
617    and extract the index corresponding to the given byte offset.
618
619    We then try to fold the entire address expression into a form
620    &array[index].
621
622    If we are successful, we replace the right hand side of USE_STMT
623    with the new address computation.  */
624
625 static bool
626 forward_propagate_addr_into_variable_array_index (tree offset,
627                                                   tree def_rhs,
628                                                   gimple_stmt_iterator *use_stmt_gsi)
629 {
630   tree index, tunit;
631   gimple offset_def, use_stmt = gsi_stmt (*use_stmt_gsi);
632   tree new_rhs, tmp;
633
634   if (TREE_CODE (TREE_OPERAND (def_rhs, 0)) == ARRAY_REF)
635     tunit = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (def_rhs)));
636   else if (TREE_CODE (TREE_TYPE (TREE_OPERAND (def_rhs, 0))) == ARRAY_TYPE)
637     tunit = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (TREE_TYPE (def_rhs))));
638   else
639     return false;
640   if (!host_integerp (tunit, 1))
641     return false;
642
643   /* Get the offset's defining statement.  */
644   offset_def = SSA_NAME_DEF_STMT (offset);
645
646   /* Try to find an expression for a proper index.  This is either a
647      multiplication expression by the element size or just the ssa name we came
648      along in case the element size is one. In that case, however, we do not
649      allow multiplications because they can be computing index to a higher
650      level dimension (PR 37861). */
651   if (integer_onep (tunit))
652     {
653       if (is_gimple_assign (offset_def)
654           && gimple_assign_rhs_code (offset_def) == MULT_EXPR)
655         return false;
656
657       index = offset;
658     }
659   else
660     {
661       /* The statement which defines OFFSET before type conversion
662          must be a simple GIMPLE_ASSIGN.  */
663       if (!is_gimple_assign (offset_def))
664         return false;
665
666       /* The RHS of the statement which defines OFFSET must be a
667          multiplication of an object by the size of the array elements.
668          This implicitly verifies that the size of the array elements
669          is constant.  */
670      if (gimple_assign_rhs_code (offset_def) == MULT_EXPR
671          && TREE_CODE (gimple_assign_rhs2 (offset_def)) == INTEGER_CST
672          && tree_int_cst_equal (gimple_assign_rhs2 (offset_def), tunit))
673        {
674          /* The first operand to the MULT_EXPR is the desired index.  */
675          index = gimple_assign_rhs1 (offset_def);
676        }
677      /* If we have idx * tunit + CST * tunit re-associate that.  */
678      else if ((gimple_assign_rhs_code (offset_def) == PLUS_EXPR
679                || gimple_assign_rhs_code (offset_def) == MINUS_EXPR)
680               && TREE_CODE (gimple_assign_rhs1 (offset_def)) == SSA_NAME
681               && TREE_CODE (gimple_assign_rhs2 (offset_def)) == INTEGER_CST
682               && (tmp = div_if_zero_remainder (EXACT_DIV_EXPR,
683                                                gimple_assign_rhs2 (offset_def),
684                                                tunit)) != NULL_TREE)
685        {
686          gimple offset_def2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (offset_def));
687          if (is_gimple_assign (offset_def2)
688              && gimple_assign_rhs_code (offset_def2) == MULT_EXPR
689              && TREE_CODE (gimple_assign_rhs2 (offset_def2)) == INTEGER_CST
690              && tree_int_cst_equal (gimple_assign_rhs2 (offset_def2), tunit))
691            {
692              index = fold_build2 (gimple_assign_rhs_code (offset_def),
693                                   TREE_TYPE (offset),
694                                   gimple_assign_rhs1 (offset_def2), tmp);
695            }
696          else
697            return false;
698        }
699      else
700         return false;
701     }
702
703   /* Replace the pointer addition with array indexing.  */
704   index = force_gimple_operand_gsi (use_stmt_gsi, index, true, NULL_TREE,
705                                     true, GSI_SAME_STMT);
706   if (TREE_CODE (TREE_OPERAND (def_rhs, 0)) == ARRAY_REF)
707     {
708       new_rhs = unshare_expr (def_rhs);
709       TREE_OPERAND (TREE_OPERAND (new_rhs, 0), 1) = index;
710     }
711   else
712     {
713       new_rhs = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (TREE_TYPE (def_rhs))),
714                         unshare_expr (TREE_OPERAND (def_rhs, 0)),
715                         index, integer_zero_node, NULL_TREE);
716       new_rhs = build_fold_addr_expr (new_rhs);
717       if (!useless_type_conversion_p (TREE_TYPE (gimple_assign_lhs (use_stmt)),
718                                       TREE_TYPE (new_rhs)))
719         {
720           new_rhs = force_gimple_operand_gsi (use_stmt_gsi, new_rhs, true,
721                                               NULL_TREE, true, GSI_SAME_STMT);
722           new_rhs = fold_convert (TREE_TYPE (gimple_assign_lhs (use_stmt)),
723                                   new_rhs);
724         }
725     }
726   gimple_assign_set_rhs_from_tree (use_stmt_gsi, new_rhs);
727   use_stmt = gsi_stmt (*use_stmt_gsi);
728
729   /* That should have created gimple, so there is no need to
730      record information to undo the propagation.  */
731   fold_stmt_inplace (use_stmt);
732   tidy_after_forward_propagate_addr (use_stmt);
733   return true;
734 }
735
736 /* NAME is a SSA_NAME representing DEF_RHS which is of the form
737    ADDR_EXPR <whatever>.
738
739    Try to forward propagate the ADDR_EXPR into the use USE_STMT.
740    Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
741    node or for recovery of array indexing from pointer arithmetic.
742
743    Return true if the propagation was successful (the propagation can
744    be not totally successful, yet things may have been changed).  */
745
746 static bool
747 forward_propagate_addr_expr_1 (tree name, tree def_rhs,
748                                gimple_stmt_iterator *use_stmt_gsi,
749                                bool single_use_p)
750 {
751   tree lhs, rhs, rhs2, array_ref;
752   gimple use_stmt = gsi_stmt (*use_stmt_gsi);
753   enum tree_code rhs_code;
754   bool res = true;
755
756   gcc_assert (TREE_CODE (def_rhs) == ADDR_EXPR);
757
758   lhs = gimple_assign_lhs (use_stmt);
759   rhs_code = gimple_assign_rhs_code (use_stmt);
760   rhs = gimple_assign_rhs1 (use_stmt);
761
762   /* Trivial cases.  The use statement could be a trivial copy or a
763      useless conversion.  Recurse to the uses of the lhs as copyprop does
764      not copy through different variant pointers and FRE does not catch
765      all useless conversions.  Treat the case of a single-use name and
766      a conversion to def_rhs type separate, though.  */
767   if (TREE_CODE (lhs) == SSA_NAME
768       && ((rhs_code == SSA_NAME && rhs == name)
769           || CONVERT_EXPR_CODE_P (rhs_code)))
770     {
771       /* Only recurse if we don't deal with a single use or we cannot
772          do the propagation to the current statement.  In particular
773          we can end up with a conversion needed for a non-invariant
774          address which we cannot do in a single statement.  */
775       if (!single_use_p
776           || (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs))
777               && (!is_gimple_min_invariant (def_rhs)
778                   || (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
779                       && POINTER_TYPE_P (TREE_TYPE (def_rhs))
780                       && (TYPE_PRECISION (TREE_TYPE (lhs))
781                           > TYPE_PRECISION (TREE_TYPE (def_rhs)))))))
782         return forward_propagate_addr_expr (lhs, def_rhs);
783
784       gimple_assign_set_rhs1 (use_stmt, unshare_expr (def_rhs));
785       if (useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs)))
786         gimple_assign_set_rhs_code (use_stmt, TREE_CODE (def_rhs));
787       else
788         gimple_assign_set_rhs_code (use_stmt, NOP_EXPR);
789       return true;
790     }
791
792   /* Propagate through constant pointer adjustments.  */
793   if (TREE_CODE (lhs) == SSA_NAME
794       && rhs_code == POINTER_PLUS_EXPR
795       && rhs == name
796       && TREE_CODE (gimple_assign_rhs2 (use_stmt)) == INTEGER_CST)
797     {
798       tree new_def_rhs;
799       /* As we come here with non-invariant addresses in def_rhs we need
800          to make sure we can build a valid constant offsetted address
801          for further propagation.  Simply rely on fold building that
802          and check after the fact.  */
803       new_def_rhs = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (rhs)),
804                                  def_rhs,
805                                  fold_convert (ptr_type_node,
806                                                gimple_assign_rhs2 (use_stmt)));
807       if (TREE_CODE (new_def_rhs) == MEM_REF
808           && !is_gimple_mem_ref_addr (TREE_OPERAND (new_def_rhs, 0)))
809         return false;
810       new_def_rhs = build_fold_addr_expr_with_type (new_def_rhs,
811                                                     TREE_TYPE (rhs));
812
813       /* Recurse.  If we could propagate into all uses of lhs do not
814          bother to replace into the current use but just pretend we did.  */
815       if (TREE_CODE (new_def_rhs) == ADDR_EXPR
816           && forward_propagate_addr_expr (lhs, new_def_rhs))
817         return true;
818
819       if (useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (new_def_rhs)))
820         gimple_assign_set_rhs_with_ops (use_stmt_gsi, TREE_CODE (new_def_rhs),
821                                         new_def_rhs, NULL_TREE);
822       else if (is_gimple_min_invariant (new_def_rhs))
823         gimple_assign_set_rhs_with_ops (use_stmt_gsi, NOP_EXPR,
824                                         new_def_rhs, NULL_TREE);
825       else
826         return false;
827       gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt);
828       update_stmt (use_stmt);
829       return true;
830     }
831
832   /* Now strip away any outer COMPONENT_REF/ARRAY_REF nodes from the LHS.
833      ADDR_EXPR will not appear on the LHS.  */
834   lhs = gimple_assign_lhs (use_stmt);
835   while (handled_component_p (lhs))
836     lhs = TREE_OPERAND (lhs, 0);
837
838   /* Now see if the LHS node is a MEM_REF using NAME.  If so,
839      propagate the ADDR_EXPR into the use of NAME and fold the result.  */
840   if (TREE_CODE (lhs) == MEM_REF
841       && TREE_OPERAND (lhs, 0) == name)
842     {
843       tree def_rhs_base;
844       HOST_WIDE_INT def_rhs_offset;
845       /* If the address is invariant we can always fold it.  */
846       if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
847                                                          &def_rhs_offset)))
848         {
849           double_int off = mem_ref_offset (lhs);
850           tree new_ptr;
851           off = double_int_add (off,
852                                 shwi_to_double_int (def_rhs_offset));
853           if (TREE_CODE (def_rhs_base) == MEM_REF)
854             {
855               off = double_int_add (off, mem_ref_offset (def_rhs_base));
856               new_ptr = TREE_OPERAND (def_rhs_base, 0);
857             }
858           else
859             new_ptr = build_fold_addr_expr (def_rhs_base);
860           TREE_OPERAND (lhs, 0) = new_ptr;
861           TREE_OPERAND (lhs, 1)
862             = double_int_to_tree (TREE_TYPE (TREE_OPERAND (lhs, 1)), off);
863           tidy_after_forward_propagate_addr (use_stmt);
864           /* Continue propagating into the RHS if this was not the only use.  */
865           if (single_use_p)
866             return true;
867         }
868       /* If the LHS is a plain dereference and the value type is the same as
869          that of the pointed-to type of the address we can put the
870          dereferenced address on the LHS preserving the original alias-type.  */
871       else if (gimple_assign_lhs (use_stmt) == lhs
872                && useless_type_conversion_p
873                     (TREE_TYPE (TREE_OPERAND (def_rhs, 0)),
874                      TREE_TYPE (gimple_assign_rhs1 (use_stmt))))
875         {
876           tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
877           tree new_offset, new_base, saved;
878           while (handled_component_p (*def_rhs_basep))
879             def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
880           saved = *def_rhs_basep;
881           if (TREE_CODE (*def_rhs_basep) == MEM_REF)
882             {
883               new_base = TREE_OPERAND (*def_rhs_basep, 0);
884               new_offset
885                 = int_const_binop (PLUS_EXPR, TREE_OPERAND (lhs, 1),
886                                    TREE_OPERAND (*def_rhs_basep, 1));
887             }
888           else
889             {
890               new_base = build_fold_addr_expr (*def_rhs_basep);
891               new_offset = TREE_OPERAND (lhs, 1);
892             }
893           *def_rhs_basep = build2 (MEM_REF, TREE_TYPE (*def_rhs_basep),
894                                    new_base, new_offset);
895           TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (lhs);
896           TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (lhs);
897           gimple_assign_set_lhs (use_stmt,
898                                  unshare_expr (TREE_OPERAND (def_rhs, 0)));
899           *def_rhs_basep = saved;
900           tidy_after_forward_propagate_addr (use_stmt);
901           /* Continue propagating into the RHS if this was not the
902              only use.  */
903           if (single_use_p)
904             return true;
905         }
906       else
907         /* We can have a struct assignment dereferencing our name twice.
908            Note that we didn't propagate into the lhs to not falsely
909            claim we did when propagating into the rhs.  */
910         res = false;
911     }
912
913   /* Strip away any outer COMPONENT_REF, ARRAY_REF or ADDR_EXPR
914      nodes from the RHS.  */
915   rhs = gimple_assign_rhs1 (use_stmt);
916   if (TREE_CODE (rhs) == ADDR_EXPR)
917     rhs = TREE_OPERAND (rhs, 0);
918   while (handled_component_p (rhs))
919     rhs = TREE_OPERAND (rhs, 0);
920
921   /* Now see if the RHS node is a MEM_REF using NAME.  If so,
922      propagate the ADDR_EXPR into the use of NAME and fold the result.  */
923   if (TREE_CODE (rhs) == MEM_REF
924       && TREE_OPERAND (rhs, 0) == name)
925     {
926       tree def_rhs_base;
927       HOST_WIDE_INT def_rhs_offset;
928       if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
929                                                          &def_rhs_offset)))
930         {
931           double_int off = mem_ref_offset (rhs);
932           tree new_ptr;
933           off = double_int_add (off,
934                                 shwi_to_double_int (def_rhs_offset));
935           if (TREE_CODE (def_rhs_base) == MEM_REF)
936             {
937               off = double_int_add (off, mem_ref_offset (def_rhs_base));
938               new_ptr = TREE_OPERAND (def_rhs_base, 0);
939             }
940           else
941             new_ptr = build_fold_addr_expr (def_rhs_base);
942           TREE_OPERAND (rhs, 0) = new_ptr;
943           TREE_OPERAND (rhs, 1)
944             = double_int_to_tree (TREE_TYPE (TREE_OPERAND (rhs, 1)), off);
945           fold_stmt_inplace (use_stmt);
946           tidy_after_forward_propagate_addr (use_stmt);
947           return res;
948         }
949       /* If the RHS is a plain dereference and the value type is the same as
950          that of the pointed-to type of the address we can put the
951          dereferenced address on the RHS preserving the original alias-type.  */
952       else if (gimple_assign_rhs1 (use_stmt) == rhs
953                && useless_type_conversion_p
954                     (TREE_TYPE (gimple_assign_lhs (use_stmt)),
955                      TREE_TYPE (TREE_OPERAND (def_rhs, 0))))
956         {
957           tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
958           tree new_offset, new_base, saved;
959           while (handled_component_p (*def_rhs_basep))
960             def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
961           saved = *def_rhs_basep;
962           if (TREE_CODE (*def_rhs_basep) == MEM_REF)
963             {
964               new_base = TREE_OPERAND (*def_rhs_basep, 0);
965               new_offset
966                 = int_const_binop (PLUS_EXPR, TREE_OPERAND (rhs, 1),
967                                    TREE_OPERAND (*def_rhs_basep, 1));
968             }
969           else
970             {
971               new_base = build_fold_addr_expr (*def_rhs_basep);
972               new_offset = TREE_OPERAND (rhs, 1);
973             }
974           *def_rhs_basep = build2 (MEM_REF, TREE_TYPE (*def_rhs_basep),
975                                    new_base, new_offset);
976           TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (rhs);
977           TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (rhs);
978           gimple_assign_set_rhs1 (use_stmt,
979                                   unshare_expr (TREE_OPERAND (def_rhs, 0)));
980           *def_rhs_basep = saved;
981           fold_stmt_inplace (use_stmt);
982           tidy_after_forward_propagate_addr (use_stmt);
983           return res;
984         }
985     }
986
987   /* If the use of the ADDR_EXPR is not a POINTER_PLUS_EXPR, there
988      is nothing to do. */
989   if (gimple_assign_rhs_code (use_stmt) != POINTER_PLUS_EXPR
990       || gimple_assign_rhs1 (use_stmt) != name)
991     return false;
992
993   /* The remaining cases are all for turning pointer arithmetic into
994      array indexing.  They only apply when we have the address of
995      element zero in an array.  If that is not the case then there
996      is nothing to do.  */
997   array_ref = TREE_OPERAND (def_rhs, 0);
998   if ((TREE_CODE (array_ref) != ARRAY_REF
999        || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref, 0))) != ARRAY_TYPE
1000        || TREE_CODE (TREE_OPERAND (array_ref, 1)) != INTEGER_CST)
1001       && TREE_CODE (TREE_TYPE (array_ref)) != ARRAY_TYPE)
1002     return false;
1003
1004   rhs2 = gimple_assign_rhs2 (use_stmt);
1005   /* Optimize &x[C1] p+ C2 to  &x p+ C3 with C3 = C1 * element_size + C2.  */
1006   if (TREE_CODE (rhs2) == INTEGER_CST)
1007     {
1008       tree new_rhs = build1_loc (gimple_location (use_stmt),
1009                                  ADDR_EXPR, TREE_TYPE (def_rhs),
1010                                  fold_build2 (MEM_REF,
1011                                               TREE_TYPE (TREE_TYPE (def_rhs)),
1012                                               unshare_expr (def_rhs),
1013                                               fold_convert (ptr_type_node,
1014                                                             rhs2)));
1015       gimple_assign_set_rhs_from_tree (use_stmt_gsi, new_rhs);
1016       use_stmt = gsi_stmt (*use_stmt_gsi);
1017       update_stmt (use_stmt);
1018       tidy_after_forward_propagate_addr (use_stmt);
1019       return true;
1020     }
1021
1022   /* Try to optimize &x[0] p+ OFFSET where OFFSET is defined by
1023      converting a multiplication of an index by the size of the
1024      array elements, then the result is converted into the proper
1025      type for the arithmetic.  */
1026   if (TREE_CODE (rhs2) == SSA_NAME
1027       && (TREE_CODE (array_ref) != ARRAY_REF
1028           || integer_zerop (TREE_OPERAND (array_ref, 1)))
1029       && useless_type_conversion_p (TREE_TYPE (name), TREE_TYPE (def_rhs))
1030       /* Avoid problems with IVopts creating PLUS_EXPRs with a
1031          different type than their operands.  */
1032       && useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs)))
1033     return forward_propagate_addr_into_variable_array_index (rhs2, def_rhs,
1034                                                              use_stmt_gsi);
1035   return false;
1036 }
1037
1038 /* STMT is a statement of the form SSA_NAME = ADDR_EXPR <whatever>.
1039
1040    Try to forward propagate the ADDR_EXPR into all uses of the SSA_NAME.
1041    Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
1042    node or for recovery of array indexing from pointer arithmetic.
1043    Returns true, if all uses have been propagated into.  */
1044
1045 static bool
1046 forward_propagate_addr_expr (tree name, tree rhs)
1047 {
1048   int stmt_loop_depth = gimple_bb (SSA_NAME_DEF_STMT (name))->loop_depth;
1049   imm_use_iterator iter;
1050   gimple use_stmt;
1051   bool all = true;
1052   bool single_use_p = has_single_use (name);
1053
1054   FOR_EACH_IMM_USE_STMT (use_stmt, iter, name)
1055     {
1056       bool result;
1057       tree use_rhs;
1058
1059       /* If the use is not in a simple assignment statement, then
1060          there is nothing we can do.  */
1061       if (gimple_code (use_stmt) != GIMPLE_ASSIGN)
1062         {
1063           if (!is_gimple_debug (use_stmt))
1064             all = false;
1065           continue;
1066         }
1067
1068       /* If the use is in a deeper loop nest, then we do not want
1069          to propagate non-invariant ADDR_EXPRs into the loop as that
1070          is likely adding expression evaluations into the loop.  */
1071       if (gimple_bb (use_stmt)->loop_depth > stmt_loop_depth
1072           && !is_gimple_min_invariant (rhs))
1073         {
1074           all = false;
1075           continue;
1076         }
1077
1078       {
1079         gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
1080         result = forward_propagate_addr_expr_1 (name, rhs, &gsi,
1081                                                 single_use_p);
1082         /* If the use has moved to a different statement adjust
1083            the update machinery for the old statement too.  */
1084         if (use_stmt != gsi_stmt (gsi))
1085           {
1086             update_stmt (use_stmt);
1087             use_stmt = gsi_stmt (gsi);
1088           }
1089
1090         update_stmt (use_stmt);
1091       }
1092       all &= result;
1093
1094       /* Remove intermediate now unused copy and conversion chains.  */
1095       use_rhs = gimple_assign_rhs1 (use_stmt);
1096       if (result
1097           && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
1098           && TREE_CODE (use_rhs) == SSA_NAME
1099           && has_zero_uses (gimple_assign_lhs (use_stmt)))
1100         {
1101           gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
1102           release_defs (use_stmt);
1103           gsi_remove (&gsi, true);
1104         }
1105     }
1106
1107   return all && has_zero_uses (name);
1108 }
1109
1110
1111 /* Forward propagate the comparison defined in STMT like
1112    cond_1 = x CMP y to uses of the form
1113      a_1 = (T')cond_1
1114      a_1 = !cond_1
1115      a_1 = cond_1 != 0
1116    Returns true if stmt is now unused.  */
1117
1118 static bool
1119 forward_propagate_comparison (gimple stmt)
1120 {
1121   tree name = gimple_assign_lhs (stmt);
1122   gimple use_stmt;
1123   tree tmp = NULL_TREE;
1124   gimple_stmt_iterator gsi;
1125   enum tree_code code;
1126   tree lhs;
1127
1128   /* Don't propagate ssa names that occur in abnormal phis.  */
1129   if ((TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
1130        && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (stmt)))
1131       || (TREE_CODE (gimple_assign_rhs2 (stmt)) == SSA_NAME
1132         && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs2 (stmt))))
1133     return false;
1134
1135   /* Do not un-cse comparisons.  But propagate through copies.  */
1136   use_stmt = get_prop_dest_stmt (name, &name);
1137   if (!use_stmt
1138       || !is_gimple_assign (use_stmt))
1139     return false;
1140
1141   code = gimple_assign_rhs_code (use_stmt);
1142   lhs = gimple_assign_lhs (use_stmt);
1143   if (!INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
1144     return false;
1145
1146   /* We can propagate the condition into a statement that
1147      computes the logical negation of the comparison result.  */
1148   if ((code == BIT_NOT_EXPR
1149        && TYPE_PRECISION (TREE_TYPE (lhs)) == 1)
1150       || (code == BIT_XOR_EXPR
1151           && integer_onep (gimple_assign_rhs2 (use_stmt))))
1152     {
1153       tree type = TREE_TYPE (gimple_assign_rhs1 (stmt));
1154       bool nans = HONOR_NANS (TYPE_MODE (type));
1155       enum tree_code inv_code;
1156       inv_code = invert_tree_comparison (gimple_assign_rhs_code (stmt), nans);
1157       if (inv_code == ERROR_MARK)
1158         return false;
1159
1160       tmp = build2 (inv_code, TREE_TYPE (lhs), gimple_assign_rhs1 (stmt),
1161                     gimple_assign_rhs2 (stmt));
1162     }
1163   else
1164     return false;
1165
1166   gsi = gsi_for_stmt (use_stmt);
1167   gimple_assign_set_rhs_from_tree (&gsi, unshare_expr (tmp));
1168   use_stmt = gsi_stmt (gsi);
1169   update_stmt (use_stmt);
1170
1171   if (dump_file && (dump_flags & TDF_DETAILS))
1172     {
1173       fprintf (dump_file, "  Replaced '");
1174       print_gimple_expr (dump_file, stmt, 0, dump_flags);
1175       fprintf (dump_file, "' with '");
1176       print_gimple_expr (dump_file, use_stmt, 0, dump_flags);
1177       fprintf (dump_file, "'\n");
1178     }
1179
1180   /* Remove defining statements.  */
1181   return remove_prop_source_from_use (name);
1182 }
1183
1184
1185 /* If we have lhs = ~x (STMT), look and see if earlier we had x = ~y.
1186    If so, we can change STMT into lhs = y which can later be copy
1187    propagated.  Similarly for negation.
1188
1189    This could trivially be formulated as a forward propagation
1190    to immediate uses.  However, we already had an implementation
1191    from DOM which used backward propagation via the use-def links.
1192
1193    It turns out that backward propagation is actually faster as
1194    there's less work to do for each NOT/NEG expression we find.
1195    Backwards propagation needs to look at the statement in a single
1196    backlink.  Forward propagation needs to look at potentially more
1197    than one forward link.
1198
1199    Returns true when the statement was changed.  */
1200
1201 static bool 
1202 simplify_not_neg_expr (gimple_stmt_iterator *gsi_p)
1203 {
1204   gimple stmt = gsi_stmt (*gsi_p);
1205   tree rhs = gimple_assign_rhs1 (stmt);
1206   gimple rhs_def_stmt = SSA_NAME_DEF_STMT (rhs);
1207
1208   /* See if the RHS_DEF_STMT has the same form as our statement.  */
1209   if (is_gimple_assign (rhs_def_stmt)
1210       && gimple_assign_rhs_code (rhs_def_stmt) == gimple_assign_rhs_code (stmt))
1211     {
1212       tree rhs_def_operand = gimple_assign_rhs1 (rhs_def_stmt);
1213
1214       /* Verify that RHS_DEF_OPERAND is a suitable SSA_NAME.  */
1215       if (TREE_CODE (rhs_def_operand) == SSA_NAME
1216           && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs_def_operand))
1217         {
1218           gimple_assign_set_rhs_from_tree (gsi_p, rhs_def_operand);
1219           stmt = gsi_stmt (*gsi_p);
1220           update_stmt (stmt);
1221           return true;
1222         }
1223     }
1224
1225   return false;
1226 }
1227
1228 /* STMT is a SWITCH_EXPR for which we attempt to find equivalent forms of
1229    the condition which we may be able to optimize better.  */
1230
1231 static bool
1232 simplify_gimple_switch (gimple stmt)
1233 {
1234   tree cond = gimple_switch_index (stmt);
1235   tree def, to, ti;
1236   gimple def_stmt;
1237
1238   /* The optimization that we really care about is removing unnecessary
1239      casts.  That will let us do much better in propagating the inferred
1240      constant at the switch target.  */
1241   if (TREE_CODE (cond) == SSA_NAME)
1242     {
1243       def_stmt = SSA_NAME_DEF_STMT (cond);
1244       if (is_gimple_assign (def_stmt))
1245         {
1246           if (gimple_assign_rhs_code (def_stmt) == NOP_EXPR)
1247             {
1248               int need_precision;
1249               bool fail;
1250
1251               def = gimple_assign_rhs1 (def_stmt);
1252
1253               /* ??? Why was Jeff testing this?  We are gimple...  */
1254               gcc_checking_assert (is_gimple_val (def));
1255
1256               to = TREE_TYPE (cond);
1257               ti = TREE_TYPE (def);
1258
1259               /* If we have an extension that preserves value, then we
1260                  can copy the source value into the switch.  */
1261
1262               need_precision = TYPE_PRECISION (ti);
1263               fail = false;
1264               if (! INTEGRAL_TYPE_P (ti))
1265                 fail = true;
1266               else if (TYPE_UNSIGNED (to) && !TYPE_UNSIGNED (ti))
1267                 fail = true;
1268               else if (!TYPE_UNSIGNED (to) && TYPE_UNSIGNED (ti))
1269                 need_precision += 1;
1270               if (TYPE_PRECISION (to) < need_precision)
1271                 fail = true;
1272
1273               if (!fail)
1274                 {
1275                   gimple_switch_set_index (stmt, def);
1276                   update_stmt (stmt);
1277                   return true;
1278                 }
1279             }
1280         }
1281     }
1282
1283   return false;
1284 }
1285
1286 /* For pointers p2 and p1 return p2 - p1 if the
1287    difference is known and constant, otherwise return NULL.  */
1288
1289 static tree
1290 constant_pointer_difference (tree p1, tree p2)
1291 {
1292   int i, j;
1293 #define CPD_ITERATIONS 5
1294   tree exps[2][CPD_ITERATIONS];
1295   tree offs[2][CPD_ITERATIONS];
1296   int cnt[2];
1297
1298   for (i = 0; i < 2; i++)
1299     {
1300       tree p = i ? p1 : p2;
1301       tree off = size_zero_node;
1302       gimple stmt;
1303       enum tree_code code;
1304
1305       /* For each of p1 and p2 we need to iterate at least
1306          twice, to handle ADDR_EXPR directly in p1/p2,
1307          SSA_NAME with ADDR_EXPR or POINTER_PLUS_EXPR etc.
1308          on definition's stmt RHS.  Iterate a few extra times.  */
1309       j = 0;
1310       do
1311         {
1312           if (!POINTER_TYPE_P (TREE_TYPE (p)))
1313             break;
1314           if (TREE_CODE (p) == ADDR_EXPR)
1315             {
1316               tree q = TREE_OPERAND (p, 0);
1317               HOST_WIDE_INT offset;
1318               tree base = get_addr_base_and_unit_offset (q, &offset);
1319               if (base)
1320                 {
1321                   q = base;
1322                   if (offset)
1323                     off = size_binop (PLUS_EXPR, off, size_int (offset));
1324                 }
1325               if (TREE_CODE (q) == MEM_REF
1326                   && TREE_CODE (TREE_OPERAND (q, 0)) == SSA_NAME)
1327                 {
1328                   p = TREE_OPERAND (q, 0);
1329                   off = size_binop (PLUS_EXPR, off,
1330                                     double_int_to_tree (sizetype,
1331                                                         mem_ref_offset (q)));
1332                 }
1333               else
1334                 {
1335                   exps[i][j] = q;
1336                   offs[i][j++] = off;
1337                   break;
1338                 }
1339             }
1340           if (TREE_CODE (p) != SSA_NAME)
1341             break;
1342           exps[i][j] = p;
1343           offs[i][j++] = off;
1344           if (j == CPD_ITERATIONS)
1345             break;
1346           stmt = SSA_NAME_DEF_STMT (p);
1347           if (!is_gimple_assign (stmt) || gimple_assign_lhs (stmt) != p)
1348             break;
1349           code = gimple_assign_rhs_code (stmt);
1350           if (code == POINTER_PLUS_EXPR)
1351             {
1352               if (TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
1353                 break;
1354               off = size_binop (PLUS_EXPR, off, gimple_assign_rhs2 (stmt));
1355               p = gimple_assign_rhs1 (stmt);
1356             }
1357           else if (code == ADDR_EXPR || code == NOP_EXPR)
1358             p = gimple_assign_rhs1 (stmt);
1359           else
1360             break;
1361         }
1362       while (1);
1363       cnt[i] = j;
1364     }
1365
1366   for (i = 0; i < cnt[0]; i++)
1367     for (j = 0; j < cnt[1]; j++)
1368       if (exps[0][i] == exps[1][j])
1369         return size_binop (MINUS_EXPR, offs[0][i], offs[1][j]);
1370
1371   return NULL_TREE;
1372 }
1373
1374 /* *GSI_P is a GIMPLE_CALL to a builtin function.
1375    Optimize
1376    memcpy (p, "abcd", 4);
1377    memset (p + 4, ' ', 3);
1378    into
1379    memcpy (p, "abcd   ", 7);
1380    call if the latter can be stored by pieces during expansion.  */
1381
1382 static bool
1383 simplify_builtin_call (gimple_stmt_iterator *gsi_p, tree callee2)
1384 {
1385   gimple stmt1, stmt2 = gsi_stmt (*gsi_p);
1386   tree vuse = gimple_vuse (stmt2);
1387   if (vuse == NULL)
1388     return false;
1389   stmt1 = SSA_NAME_DEF_STMT (vuse);
1390
1391   switch (DECL_FUNCTION_CODE (callee2))
1392     {
1393     case BUILT_IN_MEMSET:
1394       if (gimple_call_num_args (stmt2) != 3
1395           || gimple_call_lhs (stmt2)
1396           || CHAR_BIT != 8
1397           || BITS_PER_UNIT != 8)
1398         break;
1399       else
1400         {
1401           tree callee1;
1402           tree ptr1, src1, str1, off1, len1, lhs1;
1403           tree ptr2 = gimple_call_arg (stmt2, 0);
1404           tree val2 = gimple_call_arg (stmt2, 1);
1405           tree len2 = gimple_call_arg (stmt2, 2);
1406           tree diff, vdef, new_str_cst;
1407           gimple use_stmt;
1408           unsigned int ptr1_align;
1409           unsigned HOST_WIDE_INT src_len;
1410           char *src_buf;
1411           use_operand_p use_p;
1412
1413           if (!host_integerp (val2, 0)
1414               || !host_integerp (len2, 1))
1415             break;
1416           if (is_gimple_call (stmt1))
1417             {
1418               /* If first stmt is a call, it needs to be memcpy
1419                  or mempcpy, with string literal as second argument and
1420                  constant length.  */
1421               callee1 = gimple_call_fndecl (stmt1);
1422               if (callee1 == NULL_TREE
1423                   || DECL_BUILT_IN_CLASS (callee1) != BUILT_IN_NORMAL
1424                   || gimple_call_num_args (stmt1) != 3)
1425                 break;
1426               if (DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMCPY
1427                   && DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMPCPY)
1428                 break;
1429               ptr1 = gimple_call_arg (stmt1, 0);
1430               src1 = gimple_call_arg (stmt1, 1);
1431               len1 = gimple_call_arg (stmt1, 2);
1432               lhs1 = gimple_call_lhs (stmt1);
1433               if (!host_integerp (len1, 1))
1434                 break;
1435               str1 = string_constant (src1, &off1);
1436               if (str1 == NULL_TREE)
1437                 break;
1438               if (!host_integerp (off1, 1)
1439                   || compare_tree_int (off1, TREE_STRING_LENGTH (str1) - 1) > 0
1440                   || compare_tree_int (len1, TREE_STRING_LENGTH (str1)
1441                                              - tree_low_cst (off1, 1)) > 0
1442                   || TREE_CODE (TREE_TYPE (str1)) != ARRAY_TYPE
1443                   || TYPE_MODE (TREE_TYPE (TREE_TYPE (str1)))
1444                      != TYPE_MODE (char_type_node))
1445                 break;
1446             }
1447           else if (gimple_assign_single_p (stmt1))
1448             {
1449               /* Otherwise look for length 1 memcpy optimized into
1450                  assignment.  */
1451               ptr1 = gimple_assign_lhs (stmt1);
1452               src1 = gimple_assign_rhs1 (stmt1);
1453               if (TREE_CODE (ptr1) != MEM_REF
1454                   || TYPE_MODE (TREE_TYPE (ptr1)) != TYPE_MODE (char_type_node)
1455                   || !host_integerp (src1, 0))
1456                 break;
1457               ptr1 = build_fold_addr_expr (ptr1);
1458               callee1 = NULL_TREE;
1459               len1 = size_one_node;
1460               lhs1 = NULL_TREE;
1461               off1 = size_zero_node;
1462               str1 = NULL_TREE;
1463             }
1464           else
1465             break;
1466
1467           diff = constant_pointer_difference (ptr1, ptr2);
1468           if (diff == NULL && lhs1 != NULL)
1469             {
1470               diff = constant_pointer_difference (lhs1, ptr2);
1471               if (DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY
1472                   && diff != NULL)
1473                 diff = size_binop (PLUS_EXPR, diff,
1474                                    fold_convert (sizetype, len1));
1475             }
1476           /* If the difference between the second and first destination pointer
1477              is not constant, or is bigger than memcpy length, bail out.  */
1478           if (diff == NULL
1479               || !host_integerp (diff, 1)
1480               || tree_int_cst_lt (len1, diff))
1481             break;
1482
1483           /* Use maximum of difference plus memset length and memcpy length
1484              as the new memcpy length, if it is too big, bail out.  */
1485           src_len = tree_low_cst (diff, 1);
1486           src_len += tree_low_cst (len2, 1);
1487           if (src_len < (unsigned HOST_WIDE_INT) tree_low_cst (len1, 1))
1488             src_len = tree_low_cst (len1, 1);
1489           if (src_len > 1024)
1490             break;
1491
1492           /* If mempcpy value is used elsewhere, bail out, as mempcpy
1493              with bigger length will return different result.  */
1494           if (lhs1 != NULL_TREE
1495               && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY
1496               && (TREE_CODE (lhs1) != SSA_NAME
1497                   || !single_imm_use (lhs1, &use_p, &use_stmt)
1498                   || use_stmt != stmt2))
1499             break;
1500
1501           /* If anything reads memory in between memcpy and memset
1502              call, the modified memcpy call might change it.  */
1503           vdef = gimple_vdef (stmt1);
1504           if (vdef != NULL
1505               && (!single_imm_use (vdef, &use_p, &use_stmt)
1506                   || use_stmt != stmt2))
1507             break;
1508
1509           ptr1_align = get_pointer_alignment (ptr1);
1510           /* Construct the new source string literal.  */
1511           src_buf = XALLOCAVEC (char, src_len + 1);
1512           if (callee1)
1513             memcpy (src_buf,
1514                     TREE_STRING_POINTER (str1) + tree_low_cst (off1, 1),
1515                     tree_low_cst (len1, 1));
1516           else
1517             src_buf[0] = tree_low_cst (src1, 0);
1518           memset (src_buf + tree_low_cst (diff, 1),
1519                   tree_low_cst (val2, 1), tree_low_cst (len2, 1));
1520           src_buf[src_len] = '\0';
1521           /* Neither builtin_strncpy_read_str nor builtin_memcpy_read_str
1522              handle embedded '\0's.  */
1523           if (strlen (src_buf) != src_len)
1524             break;
1525           rtl_profile_for_bb (gimple_bb (stmt2));
1526           /* If the new memcpy wouldn't be emitted by storing the literal
1527              by pieces, this optimization might enlarge .rodata too much,
1528              as commonly used string literals couldn't be shared any
1529              longer.  */
1530           if (!can_store_by_pieces (src_len,
1531                                     builtin_strncpy_read_str,
1532                                     src_buf, ptr1_align, false))
1533             break;
1534
1535           new_str_cst = build_string_literal (src_len, src_buf);
1536           if (callee1)
1537             {
1538               /* If STMT1 is a mem{,p}cpy call, adjust it and remove
1539                  memset call.  */
1540               if (lhs1 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY)
1541                 gimple_call_set_lhs (stmt1, NULL_TREE);
1542               gimple_call_set_arg (stmt1, 1, new_str_cst);
1543               gimple_call_set_arg (stmt1, 2,
1544                                    build_int_cst (TREE_TYPE (len1), src_len));
1545               update_stmt (stmt1);
1546               unlink_stmt_vdef (stmt2);
1547               gsi_remove (gsi_p, true);
1548               release_defs (stmt2);
1549               if (lhs1 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY)
1550                 release_ssa_name (lhs1);
1551               return true;
1552             }
1553           else
1554             {
1555               /* Otherwise, if STMT1 is length 1 memcpy optimized into
1556                  assignment, remove STMT1 and change memset call into
1557                  memcpy call.  */
1558               gimple_stmt_iterator gsi = gsi_for_stmt (stmt1);
1559
1560               if (!is_gimple_val (ptr1))
1561                 ptr1 = force_gimple_operand_gsi (gsi_p, ptr1, true, NULL_TREE,
1562                                                  true, GSI_SAME_STMT);
1563               gimple_call_set_fndecl (stmt2, built_in_decls [BUILT_IN_MEMCPY]);
1564               gimple_call_set_arg (stmt2, 0, ptr1);
1565               gimple_call_set_arg (stmt2, 1, new_str_cst);
1566               gimple_call_set_arg (stmt2, 2,
1567                                    build_int_cst (TREE_TYPE (len2), src_len));
1568               unlink_stmt_vdef (stmt1);
1569               gsi_remove (&gsi, true);
1570               release_defs (stmt1);
1571               update_stmt (stmt2);
1572               return false;
1573             }
1574         }
1575       break;
1576     default:
1577       break;
1578     }
1579   return false;
1580 }
1581
1582 /* Checks if expression has type of one-bit precision, or is a known
1583    truth-valued expression.  */
1584 static bool
1585 truth_valued_ssa_name (tree name)
1586 {
1587   gimple def;
1588   tree type = TREE_TYPE (name);
1589
1590   if (!INTEGRAL_TYPE_P (type))
1591     return false;
1592   /* Don't check here for BOOLEAN_TYPE as the precision isn't
1593      necessarily one and so ~X is not equal to !X.  */
1594   if (TYPE_PRECISION (type) == 1)
1595     return true;
1596   def = SSA_NAME_DEF_STMT (name);
1597   if (is_gimple_assign (def))
1598     return truth_value_p (gimple_assign_rhs_code (def));
1599   return false;
1600 }
1601
1602 /* Helper routine for simplify_bitwise_binary_1 function.
1603    Return for the SSA name NAME the expression X if it mets condition
1604    NAME = !X. Otherwise return NULL_TREE.
1605    Detected patterns for NAME = !X are:
1606      !X and X == 0 for X with integral type.
1607      X ^ 1, X != 1,or ~X for X with integral type with precision of one.  */
1608 static tree
1609 lookup_logical_inverted_value (tree name)
1610 {
1611   tree op1, op2;
1612   enum tree_code code;
1613   gimple def;
1614
1615   /* If name has none-intergal type, or isn't a SSA_NAME, then
1616      return.  */
1617   if (TREE_CODE (name) != SSA_NAME
1618       || !INTEGRAL_TYPE_P (TREE_TYPE (name)))
1619     return NULL_TREE;
1620   def = SSA_NAME_DEF_STMT (name);
1621   if (!is_gimple_assign (def))
1622     return NULL_TREE;
1623
1624   code = gimple_assign_rhs_code (def);
1625   op1 = gimple_assign_rhs1 (def);
1626   op2 = NULL_TREE;
1627
1628   /* Get for EQ_EXPR or BIT_XOR_EXPR operation the second operand.
1629      If CODE isn't an EQ_EXPR, BIT_XOR_EXPR, or BIT_NOT_EXPR, then return.  */
1630   if (code == EQ_EXPR || code == NE_EXPR
1631       || code == BIT_XOR_EXPR)
1632     op2 = gimple_assign_rhs2 (def);
1633
1634   switch (code)
1635     {
1636     case BIT_NOT_EXPR:
1637       if (truth_valued_ssa_name (name))
1638         return op1;
1639       break;
1640     case EQ_EXPR:
1641       /* Check if we have X == 0 and X has an integral type.  */
1642       if (!INTEGRAL_TYPE_P (TREE_TYPE (op1)))
1643         break;
1644       if (integer_zerop (op2))
1645         return op1;
1646       break;
1647     case NE_EXPR:
1648       /* Check if we have X != 1 and X is a truth-valued.  */
1649       if (!INTEGRAL_TYPE_P (TREE_TYPE (op1)))
1650         break;
1651       if (integer_onep (op2) && truth_valued_ssa_name (op1))
1652         return op1;
1653       break;
1654     case BIT_XOR_EXPR:
1655       /* Check if we have X ^ 1 and X is truth valued.  */
1656       if (integer_onep (op2) && truth_valued_ssa_name (op1))
1657         return op1;
1658       break;
1659     default:
1660       break;
1661     }
1662
1663   return NULL_TREE;
1664 }
1665
1666 /* Optimize ARG1 CODE ARG2 to a constant for bitwise binary
1667    operations CODE, if one operand has the logically inverted
1668    value of the other.  */
1669 static tree
1670 simplify_bitwise_binary_1 (enum tree_code code, tree type,
1671                            tree arg1, tree arg2)
1672 {
1673   tree anot;
1674
1675   /* If CODE isn't a bitwise binary operation, return NULL_TREE.  */
1676   if (code != BIT_AND_EXPR && code != BIT_IOR_EXPR
1677       && code != BIT_XOR_EXPR)
1678     return NULL_TREE;
1679
1680   /* First check if operands ARG1 and ARG2 are equal.  If so
1681      return NULL_TREE as this optimization is handled fold_stmt.  */
1682   if (arg1 == arg2)
1683     return NULL_TREE;
1684   /* See if we have in arguments logical-not patterns.  */
1685   if (((anot = lookup_logical_inverted_value (arg1)) == NULL_TREE
1686        || anot != arg2)
1687       && ((anot = lookup_logical_inverted_value (arg2)) == NULL_TREE
1688           || anot != arg1))
1689     return NULL_TREE;
1690
1691   /* X & !X -> 0.  */
1692   if (code == BIT_AND_EXPR)
1693     return fold_convert (type, integer_zero_node);
1694   /* X | !X -> 1 and X ^ !X -> 1, if X is truth-valued.  */
1695   if (truth_valued_ssa_name (anot))
1696     return fold_convert (type, integer_one_node);
1697
1698   /* ??? Otherwise result is (X != 0 ? X : 1).  not handled.  */
1699   return NULL_TREE;
1700 }
1701
1702 /* Simplify bitwise binary operations.
1703    Return true if a transformation applied, otherwise return false.  */
1704
1705 static bool
1706 simplify_bitwise_binary (gimple_stmt_iterator *gsi)
1707 {
1708   gimple stmt = gsi_stmt (*gsi);
1709   tree arg1 = gimple_assign_rhs1 (stmt);
1710   tree arg2 = gimple_assign_rhs2 (stmt);
1711   enum tree_code code = gimple_assign_rhs_code (stmt);
1712   tree res;
1713   gimple def1 = NULL, def2 = NULL;
1714   tree def1_arg1, def2_arg1;
1715   enum tree_code def1_code, def2_code;
1716
1717   def1_code = TREE_CODE (arg1);
1718   def1_arg1 = arg1;
1719   if (TREE_CODE (arg1) == SSA_NAME)
1720     {
1721       def1 = SSA_NAME_DEF_STMT (arg1);
1722       if (is_gimple_assign (def1))
1723         {
1724           def1_code = gimple_assign_rhs_code (def1);
1725           def1_arg1 = gimple_assign_rhs1 (def1);
1726         }
1727     }
1728
1729   def2_code = TREE_CODE (arg2);
1730   def2_arg1 = arg2;
1731   if (TREE_CODE (arg2) == SSA_NAME)
1732     {
1733       def2 = SSA_NAME_DEF_STMT (arg2);
1734       if (is_gimple_assign (def2))
1735         {
1736           def2_code = gimple_assign_rhs_code (def2);
1737           def2_arg1 = gimple_assign_rhs1 (def2);
1738         }
1739     }
1740
1741   /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST)).  */
1742   if (TREE_CODE (arg2) == INTEGER_CST
1743       && CONVERT_EXPR_CODE_P (def1_code)
1744       && INTEGRAL_TYPE_P (TREE_TYPE (def1_arg1))
1745       && int_fits_type_p (arg2, TREE_TYPE (def1_arg1)))
1746     {
1747       gimple newop;
1748       tree tem = create_tmp_reg (TREE_TYPE (def1_arg1), NULL);
1749       newop =
1750         gimple_build_assign_with_ops (code, tem, def1_arg1,
1751                                       fold_convert_loc (gimple_location (stmt),
1752                                                         TREE_TYPE (def1_arg1),
1753                                                         arg2));
1754       tem = make_ssa_name (tem, newop);
1755       gimple_assign_set_lhs (newop, tem);
1756       gimple_set_location (newop, gimple_location (stmt));
1757       gsi_insert_before (gsi, newop, GSI_SAME_STMT);
1758       gimple_assign_set_rhs_with_ops_1 (gsi, NOP_EXPR,
1759                                         tem, NULL_TREE, NULL_TREE);
1760       update_stmt (gsi_stmt (*gsi));
1761       return true;
1762     }
1763
1764   /* For bitwise binary operations apply operand conversions to the
1765      binary operation result instead of to the operands.  This allows
1766      to combine successive conversions and bitwise binary operations.  */
1767   if (CONVERT_EXPR_CODE_P (def1_code)
1768       && CONVERT_EXPR_CODE_P (def2_code)
1769       && types_compatible_p (TREE_TYPE (def1_arg1), TREE_TYPE (def2_arg1))
1770       /* Make sure that the conversion widens the operands, or has same
1771          precision,  or that it changes the operation to a bitfield
1772          precision.  */
1773       && ((TYPE_PRECISION (TREE_TYPE (def1_arg1))
1774            <= TYPE_PRECISION (TREE_TYPE (arg1)))
1775           || (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (arg1)))
1776               != MODE_INT)
1777           || (TYPE_PRECISION (TREE_TYPE (arg1))
1778               != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg1))))))
1779     {
1780       gimple newop;
1781       tree tem = create_tmp_reg (TREE_TYPE (def1_arg1),
1782                                  NULL);
1783       newop = gimple_build_assign_with_ops (code, tem, def1_arg1, def2_arg1);
1784       tem = make_ssa_name (tem, newop);
1785       gimple_assign_set_lhs (newop, tem);
1786       gimple_set_location (newop, gimple_location (stmt));
1787       gsi_insert_before (gsi, newop, GSI_SAME_STMT);
1788       gimple_assign_set_rhs_with_ops_1 (gsi, NOP_EXPR,
1789                                         tem, NULL_TREE, NULL_TREE);
1790       update_stmt (gsi_stmt (*gsi));
1791       return true;
1792     }
1793
1794   /* (a | CST1) & CST2  ->  (a & CST2) | (CST1 & CST2).  */
1795   if (code == BIT_AND_EXPR
1796       && def1_code == BIT_IOR_EXPR
1797       && TREE_CODE (arg2) == INTEGER_CST
1798       && TREE_CODE (gimple_assign_rhs2 (def1)) == INTEGER_CST)
1799     {
1800       tree cst = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg2),
1801                               arg2, gimple_assign_rhs2 (def1));
1802       tree tem;
1803       gimple newop;
1804       if (integer_zerop (cst))
1805         {
1806           gimple_assign_set_rhs1 (stmt, def1_arg1);
1807           update_stmt (stmt);
1808           return true;
1809         }
1810       tem = create_tmp_reg (TREE_TYPE (arg2), NULL);
1811       newop = gimple_build_assign_with_ops (BIT_AND_EXPR,
1812                                             tem, def1_arg1, arg2);
1813       tem = make_ssa_name (tem, newop);
1814       gimple_assign_set_lhs (newop, tem);
1815       gimple_set_location (newop, gimple_location (stmt));
1816       /* Make sure to re-process the new stmt as it's walking upwards.  */
1817       gsi_insert_before (gsi, newop, GSI_NEW_STMT);
1818       gimple_assign_set_rhs1 (stmt, tem);
1819       gimple_assign_set_rhs2 (stmt, cst);
1820       gimple_assign_set_rhs_code (stmt, BIT_IOR_EXPR);
1821       update_stmt (stmt);
1822       return true;
1823     }
1824
1825   /* Combine successive equal operations with constants.  */
1826   if ((code == BIT_AND_EXPR
1827        || code == BIT_IOR_EXPR
1828        || code == BIT_XOR_EXPR)
1829       && def1_code == code 
1830       && TREE_CODE (arg2) == INTEGER_CST
1831       && TREE_CODE (gimple_assign_rhs2 (def1)) == INTEGER_CST)
1832     {
1833       tree cst = fold_build2 (code, TREE_TYPE (arg2),
1834                               arg2, gimple_assign_rhs2 (def1));
1835       gimple_assign_set_rhs1 (stmt, def1_arg1);
1836       gimple_assign_set_rhs2 (stmt, cst);
1837       update_stmt (stmt);
1838       return true;
1839     }
1840
1841   /* Canonicalize X ^ ~0 to ~X.  */
1842   if (code == BIT_XOR_EXPR
1843       && TREE_CODE (arg2) == INTEGER_CST
1844       && integer_all_onesp (arg2))
1845     {
1846       gimple_assign_set_rhs_with_ops (gsi, BIT_NOT_EXPR, arg1, NULL_TREE);
1847       gcc_assert (gsi_stmt (*gsi) == stmt);
1848       update_stmt (stmt);
1849       return true;
1850     }
1851
1852   /* Try simple folding for X op !X, and X op X.  */
1853   res = simplify_bitwise_binary_1 (code, TREE_TYPE (arg1), arg1, arg2);
1854   if (res != NULL_TREE)
1855     {
1856       gimple_assign_set_rhs_from_tree (gsi, res);
1857       update_stmt (gsi_stmt (*gsi));
1858       return true;
1859     }
1860
1861   return false;
1862 }
1863
1864
1865 /* Perform re-associations of the plus or minus statement STMT that are
1866    always permitted.  Returns true if the CFG was changed.  */
1867
1868 static bool
1869 associate_plusminus (gimple stmt)
1870 {
1871   tree rhs1 = gimple_assign_rhs1 (stmt);
1872   tree rhs2 = gimple_assign_rhs2 (stmt);
1873   enum tree_code code = gimple_assign_rhs_code (stmt);
1874   gimple_stmt_iterator gsi;
1875   bool changed;
1876
1877   /* We can't reassociate at all for saturating types.  */
1878   if (TYPE_SATURATING (TREE_TYPE (rhs1)))
1879     return false;
1880
1881   /* First contract negates.  */
1882   do
1883     {
1884       changed = false;
1885
1886       /* A +- (-B) -> A -+ B.  */
1887       if (TREE_CODE (rhs2) == SSA_NAME)
1888         {
1889           gimple def_stmt = SSA_NAME_DEF_STMT (rhs2);
1890           if (is_gimple_assign (def_stmt)
1891               && gimple_assign_rhs_code (def_stmt) == NEGATE_EXPR
1892               && can_propagate_from (def_stmt))
1893             {
1894               code = (code == MINUS_EXPR) ? PLUS_EXPR : MINUS_EXPR;
1895               gimple_assign_set_rhs_code (stmt, code);
1896               rhs2 = gimple_assign_rhs1 (def_stmt);
1897               gimple_assign_set_rhs2 (stmt, rhs2);
1898               gimple_set_modified (stmt, true);
1899               changed = true;
1900             }
1901         }
1902
1903       /* (-A) + B -> B - A.  */
1904       if (TREE_CODE (rhs1) == SSA_NAME
1905           && code == PLUS_EXPR)
1906         {
1907           gimple def_stmt = SSA_NAME_DEF_STMT (rhs1);
1908           if (is_gimple_assign (def_stmt)
1909               && gimple_assign_rhs_code (def_stmt) == NEGATE_EXPR
1910               && can_propagate_from (def_stmt))
1911             {
1912               code = MINUS_EXPR;
1913               gimple_assign_set_rhs_code (stmt, code);
1914               rhs1 = rhs2;
1915               gimple_assign_set_rhs1 (stmt, rhs1);
1916               rhs2 = gimple_assign_rhs1 (def_stmt);
1917               gimple_assign_set_rhs2 (stmt, rhs2);
1918               gimple_set_modified (stmt, true);
1919               changed = true;
1920             }
1921         }
1922     }
1923   while (changed);
1924
1925   /* We can't reassociate floating-point or fixed-point plus or minus
1926      because of saturation to +-Inf.  */
1927   if (FLOAT_TYPE_P (TREE_TYPE (rhs1))
1928       || FIXED_POINT_TYPE_P (TREE_TYPE (rhs1)))
1929     goto out;
1930
1931   /* Second match patterns that allow contracting a plus-minus pair
1932      irrespective of overflow issues.
1933
1934         (A +- B) - A       ->  +- B
1935         (A +- B) -+ B      ->  A
1936         (CST +- A) +- CST  ->  CST +- A
1937         (A + CST) +- CST   ->  A + CST
1938         ~A + A             ->  -1
1939         ~A + 1             ->  -A 
1940         A - (A +- B)       ->  -+ B
1941         A +- (B +- A)      ->  +- B
1942         CST +- (CST +- A)  ->  CST +- A
1943         CST +- (A +- CST)  ->  CST +- A
1944         A + ~A             ->  -1
1945
1946      via commutating the addition and contracting operations to zero
1947      by reassociation.  */
1948
1949   gsi = gsi_for_stmt (stmt);
1950   if (TREE_CODE (rhs1) == SSA_NAME)
1951     {
1952       gimple def_stmt = SSA_NAME_DEF_STMT (rhs1);
1953       if (is_gimple_assign (def_stmt) && can_propagate_from (def_stmt))
1954         {
1955           enum tree_code def_code = gimple_assign_rhs_code (def_stmt);
1956           if (def_code == PLUS_EXPR
1957               || def_code == MINUS_EXPR)
1958             {
1959               tree def_rhs1 = gimple_assign_rhs1 (def_stmt);
1960               tree def_rhs2 = gimple_assign_rhs2 (def_stmt);
1961               if (operand_equal_p (def_rhs1, rhs2, 0)
1962                   && code == MINUS_EXPR)
1963                 {
1964                   /* (A +- B) - A -> +- B.  */
1965                   code = ((def_code == PLUS_EXPR)
1966                           ? TREE_CODE (def_rhs2) : NEGATE_EXPR);
1967                   rhs1 = def_rhs2;
1968                   rhs2 = NULL_TREE;
1969                   gimple_assign_set_rhs_with_ops (&gsi, code, rhs1, NULL_TREE);
1970                   gcc_assert (gsi_stmt (gsi) == stmt);
1971                   gimple_set_modified (stmt, true);
1972                 }
1973               else if (operand_equal_p (def_rhs2, rhs2, 0)
1974                        && code != def_code)
1975                 {
1976                   /* (A +- B) -+ B -> A.  */
1977                   code = TREE_CODE (def_rhs1);
1978                   rhs1 = def_rhs1;
1979                   rhs2 = NULL_TREE;
1980                   gimple_assign_set_rhs_with_ops (&gsi, code, rhs1, NULL_TREE);
1981                   gcc_assert (gsi_stmt (gsi) == stmt);
1982                   gimple_set_modified (stmt, true);
1983                 }
1984               else if (TREE_CODE (rhs2) == INTEGER_CST
1985                        && TREE_CODE (def_rhs1) == INTEGER_CST)
1986                 {
1987                   /* (CST +- A) +- CST -> CST +- A.  */
1988                   tree cst = fold_binary (code, TREE_TYPE (rhs1),
1989                                           def_rhs1, rhs2);
1990                   if (cst && !TREE_OVERFLOW (cst))
1991                     {
1992                       code = def_code;
1993                       gimple_assign_set_rhs_code (stmt, code);
1994                       rhs1 = cst;
1995                       gimple_assign_set_rhs1 (stmt, rhs1);
1996                       rhs2 = def_rhs2;
1997                       gimple_assign_set_rhs2 (stmt, rhs2);
1998                       gimple_set_modified (stmt, true);
1999                     }
2000                 }
2001               else if (TREE_CODE (rhs2) == INTEGER_CST
2002                        && TREE_CODE (def_rhs2) == INTEGER_CST
2003                        && def_code == PLUS_EXPR)
2004                 {
2005                   /* (A + CST) +- CST -> A + CST.  */
2006                   tree cst = fold_binary (code, TREE_TYPE (rhs1),
2007                                           def_rhs2, rhs2);
2008                   if (cst && !TREE_OVERFLOW (cst))
2009                     {
2010                       code = PLUS_EXPR;
2011                       gimple_assign_set_rhs_code (stmt, code);
2012                       rhs1 = def_rhs1;
2013                       gimple_assign_set_rhs1 (stmt, rhs1);
2014                       rhs2 = cst;
2015                       gimple_assign_set_rhs2 (stmt, rhs2);
2016                       gimple_set_modified (stmt, true);
2017                     }
2018                 }
2019             }
2020           else if (def_code == BIT_NOT_EXPR
2021                    && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
2022             {
2023               tree def_rhs1 = gimple_assign_rhs1 (def_stmt);
2024               if (code == PLUS_EXPR
2025                   && operand_equal_p (def_rhs1, rhs2, 0))
2026                 {
2027                   /* ~A + A -> -1.  */
2028                   code = INTEGER_CST;
2029                   rhs1 = build_int_cst_type (TREE_TYPE (rhs2), -1);
2030                   rhs2 = NULL_TREE;
2031                   gimple_assign_set_rhs_with_ops (&gsi, code, rhs1, NULL_TREE);
2032                   gcc_assert (gsi_stmt (gsi) == stmt);
2033                   gimple_set_modified (stmt, true);
2034                 }
2035               else if (code == PLUS_EXPR
2036                        && integer_onep (rhs1))
2037                 {
2038                   /* ~A + 1 -> -A.  */
2039                   code = NEGATE_EXPR;
2040                   rhs1 = def_rhs1;
2041                   rhs2 = NULL_TREE;
2042                   gimple_assign_set_rhs_with_ops (&gsi, code, rhs1, NULL_TREE);
2043                   gcc_assert (gsi_stmt (gsi) == stmt);
2044                   gimple_set_modified (stmt, true);
2045                 }
2046             }
2047         }
2048     }
2049
2050   if (rhs2 && TREE_CODE (rhs2) == SSA_NAME)
2051     {
2052       gimple def_stmt = SSA_NAME_DEF_STMT (rhs2);
2053       if (is_gimple_assign (def_stmt) && can_propagate_from (def_stmt))
2054         {
2055           enum tree_code def_code = gimple_assign_rhs_code (def_stmt);
2056           if (def_code == PLUS_EXPR
2057               || def_code == MINUS_EXPR)
2058             {
2059               tree def_rhs1 = gimple_assign_rhs1 (def_stmt);
2060               tree def_rhs2 = gimple_assign_rhs2 (def_stmt);
2061               if (operand_equal_p (def_rhs1, rhs1, 0)
2062                   && code == MINUS_EXPR)
2063                 {
2064                   /* A - (A +- B) -> -+ B.  */
2065                   code = ((def_code == PLUS_EXPR)
2066                           ? NEGATE_EXPR : TREE_CODE (def_rhs2));
2067                   rhs1 = def_rhs2;
2068                   rhs2 = NULL_TREE;
2069                   gimple_assign_set_rhs_with_ops (&gsi, code, rhs1, NULL_TREE);
2070                   gcc_assert (gsi_stmt (gsi) == stmt);
2071                   gimple_set_modified (stmt, true);
2072                 }
2073               else if (operand_equal_p (def_rhs2, rhs1, 0)
2074                        && code != def_code)
2075                 {
2076                   /* A +- (B +- A) -> +- B.  */
2077                   code = ((code == PLUS_EXPR)
2078                           ? TREE_CODE (def_rhs1) : NEGATE_EXPR);
2079                   rhs1 = def_rhs1;
2080                   rhs2 = NULL_TREE;
2081                   gimple_assign_set_rhs_with_ops (&gsi, code, rhs1, NULL_TREE);
2082                   gcc_assert (gsi_stmt (gsi) == stmt);
2083                   gimple_set_modified (stmt, true);
2084                 }
2085               else if (TREE_CODE (rhs1) == INTEGER_CST
2086                        && TREE_CODE (def_rhs1) == INTEGER_CST)
2087                 {
2088                   /* CST +- (CST +- A) -> CST +- A.  */
2089                   tree cst = fold_binary (code, TREE_TYPE (rhs2),
2090                                           rhs1, def_rhs1);
2091                   if (cst && !TREE_OVERFLOW (cst))
2092                     {
2093                       code = (code == def_code ? PLUS_EXPR : MINUS_EXPR);
2094                       gimple_assign_set_rhs_code (stmt, code);
2095                       rhs1 = cst;
2096                       gimple_assign_set_rhs1 (stmt, rhs1);
2097                       rhs2 = def_rhs2;
2098                       gimple_assign_set_rhs2 (stmt, rhs2);
2099                       gimple_set_modified (stmt, true);
2100                     }
2101                 }
2102               else if (TREE_CODE (rhs1) == INTEGER_CST
2103                        && TREE_CODE (def_rhs2) == INTEGER_CST)
2104                 {
2105                   /* CST +- (A +- CST) -> CST +- A.  */
2106                   tree cst = fold_binary (def_code == code
2107                                           ? PLUS_EXPR : MINUS_EXPR,
2108                                           TREE_TYPE (rhs2),
2109                                           rhs1, def_rhs2);
2110                   if (cst && !TREE_OVERFLOW (cst))
2111                     {
2112                       rhs1 = cst;
2113                       gimple_assign_set_rhs1 (stmt, rhs1);
2114                       rhs2 = def_rhs1;
2115                       gimple_assign_set_rhs2 (stmt, rhs2);
2116                       gimple_set_modified (stmt, true);
2117                     }
2118                 }
2119             }
2120           else if (def_code == BIT_NOT_EXPR
2121                    && INTEGRAL_TYPE_P (TREE_TYPE (rhs2)))
2122             {
2123               tree def_rhs1 = gimple_assign_rhs1 (def_stmt);
2124               if (code == PLUS_EXPR
2125                   && operand_equal_p (def_rhs1, rhs1, 0))
2126                 {
2127                   /* A + ~A -> -1.  */
2128                   code = INTEGER_CST;
2129                   rhs1 = build_int_cst_type (TREE_TYPE (rhs1), -1);
2130                   rhs2 = NULL_TREE;
2131                   gimple_assign_set_rhs_with_ops (&gsi, code, rhs1, NULL_TREE);
2132                   gcc_assert (gsi_stmt (gsi) == stmt);
2133                   gimple_set_modified (stmt, true);
2134                 }
2135             }
2136         }
2137     }
2138
2139 out:
2140   if (gimple_modified_p (stmt))
2141     {
2142       fold_stmt_inplace (stmt);
2143       update_stmt (stmt);
2144       if (maybe_clean_or_replace_eh_stmt (stmt, stmt)
2145           && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
2146         return true;
2147     }
2148
2149   return false;
2150 }
2151
2152 /* Combine two conversions in a row for the second conversion at *GSI.
2153    Returns 1 if there were any changes made, 2 if cfg-cleanup needs to
2154    run.  Else it returns 0.  */
2155  
2156 static int
2157 combine_conversions (gimple_stmt_iterator *gsi)
2158 {
2159   gimple stmt = gsi_stmt (*gsi);
2160   gimple def_stmt;
2161   tree op0, lhs;
2162   enum tree_code code = gimple_assign_rhs_code (stmt);
2163
2164   gcc_checking_assert (CONVERT_EXPR_CODE_P (code)
2165                        || code == FLOAT_EXPR
2166                        || code == FIX_TRUNC_EXPR);
2167
2168   lhs = gimple_assign_lhs (stmt);
2169   op0 = gimple_assign_rhs1 (stmt);
2170   if (useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0)))
2171     {
2172       gimple_assign_set_rhs_code (stmt, TREE_CODE (op0));
2173       return 1;
2174     }
2175
2176   if (TREE_CODE (op0) != SSA_NAME)
2177     return 0;
2178
2179   def_stmt = SSA_NAME_DEF_STMT (op0);
2180   if (!is_gimple_assign (def_stmt))
2181     return 0;
2182
2183   if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
2184     {
2185       tree defop0 = gimple_assign_rhs1 (def_stmt);
2186       tree type = TREE_TYPE (lhs);
2187       tree inside_type = TREE_TYPE (defop0);
2188       tree inter_type = TREE_TYPE (op0);
2189       int inside_int = INTEGRAL_TYPE_P (inside_type);
2190       int inside_ptr = POINTER_TYPE_P (inside_type);
2191       int inside_float = FLOAT_TYPE_P (inside_type);
2192       int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
2193       unsigned int inside_prec = TYPE_PRECISION (inside_type);
2194       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2195       int inter_int = INTEGRAL_TYPE_P (inter_type);
2196       int inter_ptr = POINTER_TYPE_P (inter_type);
2197       int inter_float = FLOAT_TYPE_P (inter_type);
2198       int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
2199       unsigned int inter_prec = TYPE_PRECISION (inter_type);
2200       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2201       int final_int = INTEGRAL_TYPE_P (type);
2202       int final_ptr = POINTER_TYPE_P (type);
2203       int final_float = FLOAT_TYPE_P (type);
2204       int final_vec = TREE_CODE (type) == VECTOR_TYPE;
2205       unsigned int final_prec = TYPE_PRECISION (type);
2206       int final_unsignedp = TYPE_UNSIGNED (type);
2207
2208       /* In addition to the cases of two conversions in a row
2209          handled below, if we are converting something to its own
2210          type via an object of identical or wider precision, neither
2211          conversion is needed.  */
2212       if (useless_type_conversion_p (type, inside_type)
2213           && (((inter_int || inter_ptr) && final_int)
2214               || (inter_float && final_float))
2215           && inter_prec >= final_prec)
2216         {
2217           gimple_assign_set_rhs1 (stmt, unshare_expr (defop0));
2218           gimple_assign_set_rhs_code (stmt, TREE_CODE (defop0));
2219           update_stmt (stmt);
2220           return remove_prop_source_from_use (op0) ? 2 : 1;
2221         }
2222
2223       /* Likewise, if the intermediate and initial types are either both
2224          float or both integer, we don't need the middle conversion if the
2225          former is wider than the latter and doesn't change the signedness
2226          (for integers).  Avoid this if the final type is a pointer since
2227          then we sometimes need the middle conversion.  Likewise if the
2228          final type has a precision not equal to the size of its mode.  */
2229       if (((inter_int && inside_int)
2230            || (inter_float && inside_float)
2231            || (inter_vec && inside_vec))
2232           && inter_prec >= inside_prec
2233           && (inter_float || inter_vec
2234               || inter_unsignedp == inside_unsignedp)
2235           && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
2236                 && TYPE_MODE (type) == TYPE_MODE (inter_type))
2237           && ! final_ptr
2238           && (! final_vec || inter_prec == inside_prec))
2239         {
2240           gimple_assign_set_rhs1 (stmt, defop0);
2241           update_stmt (stmt);
2242           return remove_prop_source_from_use (op0) ? 2 : 1;
2243         }
2244
2245       /* If we have a sign-extension of a zero-extended value, we can
2246          replace that by a single zero-extension.  */
2247       if (inside_int && inter_int && final_int
2248           && inside_prec < inter_prec && inter_prec < final_prec
2249           && inside_unsignedp && !inter_unsignedp)
2250         {
2251           gimple_assign_set_rhs1 (stmt, defop0);
2252           update_stmt (stmt);
2253           return remove_prop_source_from_use (op0) ? 2 : 1;
2254         }
2255
2256       /* Two conversions in a row are not needed unless:
2257          - some conversion is floating-point (overstrict for now), or
2258          - some conversion is a vector (overstrict for now), or
2259          - the intermediate type is narrower than both initial and
2260          final, or
2261          - the intermediate type and innermost type differ in signedness,
2262          and the outermost type is wider than the intermediate, or
2263          - the initial type is a pointer type and the precisions of the
2264          intermediate and final types differ, or
2265          - the final type is a pointer type and the precisions of the
2266          initial and intermediate types differ.  */
2267       if (! inside_float && ! inter_float && ! final_float
2268           && ! inside_vec && ! inter_vec && ! final_vec
2269           && (inter_prec >= inside_prec || inter_prec >= final_prec)
2270           && ! (inside_int && inter_int
2271                 && inter_unsignedp != inside_unsignedp
2272                 && inter_prec < final_prec)
2273           && ((inter_unsignedp && inter_prec > inside_prec)
2274               == (final_unsignedp && final_prec > inter_prec))
2275           && ! (inside_ptr && inter_prec != final_prec)
2276           && ! (final_ptr && inside_prec != inter_prec)
2277           && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
2278                 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
2279         {
2280           gimple_assign_set_rhs1 (stmt, defop0);
2281           update_stmt (stmt);
2282           return remove_prop_source_from_use (op0) ? 2 : 1;
2283         }
2284
2285       /* A truncation to an unsigned type should be canonicalized as
2286          bitwise and of a mask.  */
2287       if (final_int && inter_int && inside_int
2288           && final_prec == inside_prec
2289           && final_prec > inter_prec
2290           && inter_unsignedp)
2291         {
2292           tree tem;
2293           tem = fold_build2 (BIT_AND_EXPR, inside_type,
2294                              defop0,
2295                              double_int_to_tree
2296                                (inside_type, double_int_mask (inter_prec)));
2297           if (!useless_type_conversion_p (type, inside_type))
2298             {
2299               tem = force_gimple_operand_gsi (gsi, tem, true, NULL_TREE, true,
2300                                               GSI_SAME_STMT);
2301               gimple_assign_set_rhs1 (stmt, tem);
2302             }
2303           else
2304             gimple_assign_set_rhs_from_tree (gsi, tem);
2305           update_stmt (gsi_stmt (*gsi));
2306           return 1;
2307         }
2308     }
2309
2310   return 0;
2311 }
2312
2313 /* Main entry point for the forward propagation and statement combine
2314    optimizer.  */
2315
2316 static unsigned int
2317 ssa_forward_propagate_and_combine (void)
2318 {
2319   basic_block bb;
2320   unsigned int todoflags = 0;
2321
2322   cfg_changed = false;
2323
2324   FOR_EACH_BB (bb)
2325     {
2326       gimple_stmt_iterator gsi, prev;
2327       bool prev_initialized;
2328
2329       /* Apply forward propagation to all stmts in the basic-block.
2330          Note we update GSI within the loop as necessary.  */
2331       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
2332         {
2333           gimple stmt = gsi_stmt (gsi);
2334           tree lhs, rhs;
2335           enum tree_code code;
2336
2337           if (!is_gimple_assign (stmt))
2338             {
2339               gsi_next (&gsi);
2340               continue;
2341             }
2342
2343           lhs = gimple_assign_lhs (stmt);
2344           rhs = gimple_assign_rhs1 (stmt);
2345           code = gimple_assign_rhs_code (stmt);
2346           if (TREE_CODE (lhs) != SSA_NAME
2347               || has_zero_uses (lhs))
2348             {
2349               gsi_next (&gsi);
2350               continue;
2351             }
2352
2353           /* If this statement sets an SSA_NAME to an address,
2354              try to propagate the address into the uses of the SSA_NAME.  */
2355           if (code == ADDR_EXPR
2356               /* Handle pointer conversions on invariant addresses
2357                  as well, as this is valid gimple.  */
2358               || (CONVERT_EXPR_CODE_P (code)
2359                   && TREE_CODE (rhs) == ADDR_EXPR
2360                   && POINTER_TYPE_P (TREE_TYPE (lhs))))
2361             {
2362               tree base = get_base_address (TREE_OPERAND (rhs, 0));
2363               if ((!base
2364                    || !DECL_P (base)
2365                    || decl_address_invariant_p (base))
2366                   && !stmt_references_abnormal_ssa_name (stmt)
2367                   && forward_propagate_addr_expr (lhs, rhs))
2368                 {
2369                   release_defs (stmt);
2370                   todoflags |= TODO_remove_unused_locals;
2371                   gsi_remove (&gsi, true);
2372                 }
2373               else
2374                 gsi_next (&gsi);
2375             }
2376           else if (code == POINTER_PLUS_EXPR && can_propagate_from (stmt))
2377             {
2378               if (TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST
2379                   /* ???  Better adjust the interface to that function
2380                      instead of building new trees here.  */
2381                   && forward_propagate_addr_expr
2382                   (lhs,
2383                    build1 (ADDR_EXPR,
2384                            TREE_TYPE (rhs),
2385                            fold_build2 (MEM_REF,
2386                                         TREE_TYPE (TREE_TYPE (rhs)),
2387                                         rhs,
2388                                         fold_convert
2389                                         (ptr_type_node,
2390                                          gimple_assign_rhs2 (stmt))))))
2391                 {
2392                   release_defs (stmt);
2393                   todoflags |= TODO_remove_unused_locals;
2394                   gsi_remove (&gsi, true);
2395                 }
2396               else if (is_gimple_min_invariant (rhs))
2397                 {
2398                   /* Make sure to fold &a[0] + off_1 here.  */
2399                   fold_stmt_inplace (stmt);
2400                   update_stmt (stmt);
2401                   if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
2402                     gsi_next (&gsi);
2403                 }
2404               else
2405                 gsi_next (&gsi);
2406             }
2407           else if (TREE_CODE_CLASS (code) == tcc_comparison)
2408             {
2409               if (forward_propagate_comparison (stmt))
2410                 cfg_changed = true;
2411               gsi_next (&gsi);
2412             }
2413           else
2414             gsi_next (&gsi);
2415         }
2416
2417       /* Combine stmts with the stmts defining their operands.
2418          Note we update GSI within the loop as necessary.  */
2419       prev_initialized = false;
2420       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
2421         {
2422           gimple stmt = gsi_stmt (gsi);
2423           bool changed = false;
2424
2425           switch (gimple_code (stmt))
2426             {
2427             case GIMPLE_ASSIGN:
2428               {
2429                 tree rhs1 = gimple_assign_rhs1 (stmt);
2430                 enum tree_code code = gimple_assign_rhs_code (stmt);
2431
2432                 if ((code == BIT_NOT_EXPR
2433                      || code == NEGATE_EXPR)
2434                     && TREE_CODE (rhs1) == SSA_NAME)
2435                   changed = simplify_not_neg_expr (&gsi);
2436                 else if (code == COND_EXPR)
2437                   {
2438                     /* In this case the entire COND_EXPR is in rhs1. */
2439                     int did_something;
2440                     did_something = forward_propagate_into_cond (&gsi);
2441                     stmt = gsi_stmt (gsi);
2442                     if (did_something == 2)
2443                       cfg_changed = true;
2444                     changed = did_something != 0;
2445                   }
2446                 else if (TREE_CODE_CLASS (code) == tcc_comparison)
2447                   {
2448                     int did_something;
2449                     did_something = forward_propagate_into_comparison (&gsi);
2450                     if (did_something == 2)
2451                       cfg_changed = true;
2452                     changed = did_something != 0;
2453                   }
2454                 else if (code == BIT_AND_EXPR
2455                          || code == BIT_IOR_EXPR
2456                          || code == BIT_XOR_EXPR)
2457                   changed = simplify_bitwise_binary (&gsi);
2458                 else if (code == PLUS_EXPR
2459                          || code == MINUS_EXPR)
2460                   changed = associate_plusminus (stmt);
2461                 else if (CONVERT_EXPR_CODE_P (code)
2462                          || code == FLOAT_EXPR
2463                          || code == FIX_TRUNC_EXPR)
2464                   {
2465                     int did_something = combine_conversions (&gsi);
2466                     if (did_something == 2)
2467                       cfg_changed = true;
2468                     changed = did_something != 0;
2469                   }
2470                 break;
2471               }
2472
2473             case GIMPLE_SWITCH:
2474               changed = simplify_gimple_switch (stmt);
2475               break;
2476
2477             case GIMPLE_COND:
2478               {
2479                 int did_something;
2480                 did_something = forward_propagate_into_gimple_cond (stmt);
2481                 if (did_something == 2)
2482                   cfg_changed = true;
2483                 changed = did_something != 0;
2484                 break;
2485               }
2486
2487             case GIMPLE_CALL:
2488               {
2489                 tree callee = gimple_call_fndecl (stmt);
2490                 if (callee != NULL_TREE
2491                     && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
2492                   changed = simplify_builtin_call (&gsi, callee);
2493                 break;
2494               }
2495
2496             default:;
2497             }
2498
2499           if (changed)
2500             {
2501               /* If the stmt changed then re-visit it and the statements
2502                  inserted before it.  */
2503               if (!prev_initialized)
2504                 gsi = gsi_start_bb (bb);
2505               else
2506                 {
2507                   gsi = prev;
2508                   gsi_next (&gsi);
2509                 }
2510             }
2511           else
2512             {
2513               prev = gsi;
2514               prev_initialized = true;
2515               gsi_next (&gsi);
2516             }
2517         }
2518     }
2519
2520   if (cfg_changed)
2521     todoflags |= TODO_cleanup_cfg;
2522
2523   return todoflags;
2524 }
2525
2526
2527 static bool
2528 gate_forwprop (void)
2529 {
2530   return flag_tree_forwprop;
2531 }
2532
2533 struct gimple_opt_pass pass_forwprop =
2534 {
2535  {
2536   GIMPLE_PASS,
2537   "forwprop",                   /* name */
2538   gate_forwprop,                /* gate */
2539   ssa_forward_propagate_and_combine,    /* execute */
2540   NULL,                         /* sub */
2541   NULL,                         /* next */
2542   0,                            /* static_pass_number */
2543   TV_TREE_FORWPROP,             /* tv_id */
2544   PROP_cfg | PROP_ssa,          /* properties_required */
2545   0,                            /* properties_provided */
2546   0,                            /* properties_destroyed */
2547   0,                            /* todo_flags_start */
2548   TODO_ggc_collect
2549   | TODO_update_ssa
2550   | TODO_verify_ssa             /* todo_flags_finish */
2551  }
2552 };